[COMMON] usb: otg/functions: add otg and fucntion driver
authorKisang Lee <kisang80.lee@samsung.com>
Thu, 17 May 2018 11:47:04 +0000 (20:47 +0900)
committerKisang Lee <kisang80.lee@samsung.com>
Thu, 17 May 2018 12:22:41 +0000 (21:22 +0900)
Change-Id: I4f7a54858b8a9ea8483a21bc4953248dcddd543e
Signed-off-by: Kisang Lee <kisang80.lee@samsung.com>
12 files changed:
drivers/usb/dwc3/otg.c [new file with mode: 0644]
drivers/usb/dwc3/otg.h [new file with mode: 0644]
drivers/usb/gadget/function/f_adb.c [new file with mode: 0644]
drivers/usb/gadget/function/f_dm.c [new file with mode: 0644]
drivers/usb/gadget/function/f_mtp.c [new file with mode: 0644]
drivers/usb/gadget/function/f_mtp.h [new file with mode: 0644]
drivers/usb/gadget/function/f_ptp.c [new file with mode: 0644]
drivers/usb/gadget/function/f_uts.c [new file with mode: 0644]
include/linux/f_mtp.h [new file with mode: 0644]
include/linux/usb/f_mtp.h [new file with mode: 0644]
include/linux/usb/otg-fsm.h
include/uapi/linux/usb/f_mtp.h [new file with mode: 0644]

diff --git a/drivers/usb/dwc3/otg.c b/drivers/usb/dwc3/otg.c
new file mode 100644 (file)
index 0000000..aa21ba4
--- /dev/null
@@ -0,0 +1,750 @@
+/**\r
+ * otg.c - DesignWare USB3 DRD Controller OTG\r
+ *\r
+ * Copyright (c) 2012, Code Aurora Forum. All rights reserved.\r
+ * Copyright (c) 2013 Samsung Electronics Co., Ltd.\r
+ *             http://www.samsung.com\r
+ *\r
+ * Authors: Ido Shayevitz <idos@codeaurora.org>\r
+ *         Anton Tikhomirov <av.tikhomirov@samsung.com>\r
+ *         Minho Lee <minho55.lee@samsung.com>\r
+ *\r
+ * This program is free software: you can redistribute it and/or modify\r
+ * it under the terms of the GNU General Public License version 2  of\r
+ * the License as published by the Free Software Foundation.\r
+ *\r
+ * This program is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+ * GNU General Public License for more details.\r
+ */\r
+\r
+#include <linux/mutex.h>\r
+#include <linux/platform_device.h>\r
+#include <linux/regulator/consumer.h>\r
+#include <linux/pm_runtime.h>\r
+#include <linux/usb/samsung_usb.h>\r
+#include <linux/mfd/samsung/s2mps18-private.h>\r
+#ifdef CONFIG_EXYNOS_PD\r
+#include <soc/samsung/exynos-pm.h>\r
+#endif\r
+\r
+#include "core.h"\r
+#include "otg.h"\r
+#include "io.h"\r
+\r
+/* -------------------------------------------------------------------------- */\r
+int otg_connection;\r
+static int dwc3_otg_statemachine(struct otg_fsm *fsm)\r
+{\r
+       struct usb_otg *otg = fsm->otg;\r
+       enum usb_otg_state prev_state = otg->state;\r
+       int ret = 0;\r
+\r
+       if (fsm->reset) {\r
+               if (otg->state == OTG_STATE_A_HOST) {\r
+                       otg_drv_vbus(fsm, 0);\r
+                       otg_start_host(fsm, 0);\r
+               } else if (otg->state == OTG_STATE_B_PERIPHERAL) {\r
+                       otg_start_gadget(fsm, 0);\r
+               }\r
+\r
+               otg->state = OTG_STATE_UNDEFINED;\r
+               goto exit;\r
+       }\r
+\r
+       switch (otg->state) {\r
+       case OTG_STATE_UNDEFINED:\r
+               if (fsm->id)\r
+                       otg->state = OTG_STATE_B_IDLE;\r
+               else\r
+                       otg->state = OTG_STATE_A_IDLE;\r
+               break;\r
+       case OTG_STATE_B_IDLE:\r
+               if (!fsm->id) {\r
+                       otg->state = OTG_STATE_A_IDLE;\r
+               } else if (fsm->b_sess_vld) {\r
+                       ret = otg_start_gadget(fsm, 1);\r
+                       if (!ret)\r
+                               otg->state = OTG_STATE_B_PERIPHERAL;\r
+                       else\r
+                               pr_err("OTG SM: cannot start gadget\n");\r
+               }\r
+               break;\r
+       case OTG_STATE_B_PERIPHERAL:\r
+               if (!fsm->id || !fsm->b_sess_vld) {\r
+                       ret = otg_start_gadget(fsm, 0);\r
+                       if (!ret)\r
+                               otg->state = OTG_STATE_B_IDLE;\r
+                       else\r
+                               pr_err("OTG SM: cannot stop gadget\n");\r
+               }\r
+               break;\r
+       case OTG_STATE_A_IDLE:\r
+               if (fsm->id) {\r
+                       otg->state = OTG_STATE_B_IDLE;\r
+               } else {\r
+                       ret = otg_start_host(fsm, 1);\r
+                       if (!ret) {\r
+                               otg_drv_vbus(fsm, 1);\r
+                               otg->state = OTG_STATE_A_HOST;\r
+                       } else {\r
+                               pr_err("OTG SM: cannot start host\n");\r
+                       }\r
+               }\r
+               break;\r
+       case OTG_STATE_A_HOST:\r
+               if (fsm->id) {\r
+                       otg_drv_vbus(fsm, 0);\r
+                       ret = otg_start_host(fsm, 0);\r
+                       if (!ret)\r
+                               otg->state = OTG_STATE_A_IDLE;\r
+                       else\r
+                               pr_err("OTG SM: cannot stop host\n");\r
+               }\r
+               break;\r
+       default:\r
+               pr_err("OTG SM: invalid state\n");\r
+       }\r
+\r
+exit:\r
+       if (!ret)\r
+               ret = (otg->state != prev_state);\r
+\r
+       pr_debug("OTG SM: %s => %s\n", usb_otg_state_string(prev_state),\r
+               (ret > 0) ? usb_otg_state_string(otg->state) : "(no change)");\r
+\r
+       return ret;\r
+}\r
+\r
+/* -------------------------------------------------------------------------- */\r
+\r
+static struct dwc3_ext_otg_ops *dwc3_otg_exynos_rsw_probe(struct dwc3 *dwc)\r
+{\r
+       struct dwc3_ext_otg_ops *ops;\r
+       bool ext_otg;\r
+\r
+       ext_otg = dwc3_exynos_rsw_available(dwc->dev->parent);\r
+       if (!ext_otg)\r
+               return NULL;\r
+\r
+       /* Allocate and init otg instance */\r
+       ops = devm_kzalloc(dwc->dev, sizeof(struct dwc3_ext_otg_ops),\r
+                       GFP_KERNEL);\r
+       if (!ops) {\r
+                dev_err(dwc->dev, "unable to allocate dwc3_ext_otg_ops\n");\r
+                return NULL;\r
+       }\r
+\r
+       ops->setup = dwc3_exynos_rsw_setup;\r
+       ops->exit = dwc3_exynos_rsw_exit;\r
+       ops->start = dwc3_exynos_rsw_start;\r
+       ops->stop = dwc3_exynos_rsw_stop;\r
+\r
+       return ops;\r
+}\r
+\r
+static void dwc3_otg_set_host_mode(struct dwc3_otg *dotg)\r
+{\r
+       struct dwc3 *dwc = dotg->dwc;\r
+       u32 reg;\r
+\r
+       if (dotg->regs) {\r
+               reg = dwc3_readl(dotg->regs, DWC3_OCTL);\r
+               reg &= ~DWC3_OTG_OCTL_PERIMODE;\r
+               dwc3_writel(dotg->regs, DWC3_OCTL, reg);\r
+       } else {\r
+               dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_HOST);\r
+       }\r
+}\r
+\r
+static void dwc3_otg_set_peripheral_mode(struct dwc3_otg *dotg)\r
+{\r
+       struct dwc3 *dwc = dotg->dwc;\r
+       u32 reg;\r
+\r
+       if (dotg->regs) {\r
+               reg = dwc3_readl(dotg->regs, DWC3_OCTL);\r
+               reg |= DWC3_OTG_OCTL_PERIMODE;\r
+               dwc3_writel(dotg->regs, DWC3_OCTL, reg);\r
+       } else {\r
+               dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_DEVICE);\r
+       }\r
+}\r
+\r
+static void dwc3_otg_drv_vbus(struct otg_fsm *fsm, int on)\r
+{\r
+       struct dwc3_otg *dotg = container_of(fsm, struct dwc3_otg, fsm);\r
+       int ret;\r
+\r
+       if (IS_ERR(dotg->vbus_reg)) {\r
+               dev_err(dotg->dwc->dev, "vbus regulator is not available\n");\r
+               return;\r
+       }\r
+\r
+       if (on)\r
+               ret = regulator_enable(dotg->vbus_reg);\r
+       else\r
+               ret = regulator_disable(dotg->vbus_reg);\r
+\r
+       if (ret)\r
+               dev_err(dotg->dwc->dev, "failed to turn Vbus %s\n",\r
+                                               on ? "on" : "off");\r
+}\r
+\r
+static void dwc3_otg_ldo_control(struct otg_fsm *fsm, int on)\r
+{\r
+#if defined(OTG_LDO_CONTROL_ENABLED)\r
+       struct usb_otg  *otg = fsm->otg;\r
+       struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg);\r
+       struct device   *dev = dotg->dwc->dev;\r
+       int i;\r
+\r
+       dev_info(dev, "Turn %s LDO\n", on ? "on" : "off");\r
+\r
+       if (on) {\r
+               for (i = 0; i < dotg->ldos; i++)\r
+                       s2m_ldo_set_mode(dotg->ldo_num[i], 0x3);\r
+       } else {\r
+               for (i = 0; i < dotg->ldos; i++)\r
+                       s2m_ldo_set_mode(dotg->ldo_num[i], 0x1);\r
+       }\r
+#endif\r
+\r
+       return;\r
+}\r
+static int dwc3_otg_start_host(struct otg_fsm *fsm, int on)\r
+{\r
+       struct usb_otg  *otg = fsm->otg;\r
+       struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg);\r
+       struct dwc3     *dwc = dotg->dwc;\r
+       struct device   *dev = dotg->dwc->dev;\r
+       int ret = 0;\r
+\r
+       if (!dotg->dwc->xhci) {\r
+               dev_err(dev, "%s: does not have any xhci\n", __func__);\r
+               return -EINVAL;\r
+       }\r
+\r
+       dev_info(dev, "Turn %s host\n", on ? "on" : "off");\r
+       if (on) {\r
+               otg_connection = 1;\r
+               dwc3_otg_ldo_control(fsm, 1);\r
+               pm_runtime_get_sync(dev);\r
+               ret = dwc3_phy_setup(dwc);\r
+               if (ret) {\r
+                       dev_err(dwc->dev, "%s: failed to setup phy\n",\r
+                                       __func__);\r
+                       goto err1;\r
+               }\r
+               ret = dwc3_core_init(dwc);\r
+               if (ret) {\r
+                       dev_err(dwc->dev, "%s: failed to reinitialize core\n",\r
+                                       __func__);\r
+                       goto err1;\r
+               }\r
+\r
+               phy_conn(dwc->usb2_generic_phy, 1);\r
+\r
+               /**\r
+                * In case there is not a resistance to detect VBUS,\r
+                * DP/DM controls by S/W are needed at this point.\r
+                */\r
+               if (dwc->is_not_vbus_pad) {\r
+                       phy_set(dwc->usb2_generic_phy,\r
+                                       SET_DPDM_PULLDOWN, NULL);\r
+                       phy_set(dwc->usb3_generic_phy,\r
+                                       SET_DPDM_PULLDOWN, NULL);\r
+               }\r
+\r
+               dwc3_otg_set_host_mode(dotg);\r
+               ret = platform_device_add(dwc->xhci);\r
+               if (ret) {\r
+                       dev_err(dev, "%s: cannot add xhci\n", __func__);\r
+                       goto err2;\r
+               }\r
+       } else {\r
+               otg_connection = 0;\r
+               platform_device_del(dwc->xhci);\r
+err2:\r
+               phy_conn(dwc->usb2_generic_phy, 0);\r
+\r
+               dwc3_core_exit(dwc);\r
+err1:\r
+               pm_runtime_put_sync(dev);\r
+               dwc3_otg_ldo_control(fsm, 0);\r
+       }\r
+\r
+       return ret;\r
+}\r
+\r
+static int dwc3_otg_start_gadget(struct otg_fsm *fsm, int on)\r
+{\r
+       struct usb_otg  *otg = fsm->otg;\r
+       struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg);\r
+       struct dwc3     *dwc = dotg->dwc;\r
+       struct device   *dev = dotg->dwc->dev;\r
+       int ret = 0;\r
+\r
+       if (!otg->gadget) {\r
+               dev_err(dev, "%s does not have any gadget\n", __func__);\r
+               return -EINVAL;\r
+       }\r
+\r
+       dev_info(dev, "Turn %s gadget %s\n",\r
+                       on ? "on" : "off", otg->gadget->name);\r
+\r
+       if (on) {\r
+               wake_lock(&dotg->wakelock);\r
+               ret = pm_runtime_get_sync(dev);\r
+               dev_info(dev, "%s pm_runtime_get_sync = %d\n",\r
+                               __func__, ret);\r
+               ret = dwc3_phy_setup(dwc);\r
+               if (ret) {\r
+                       dev_err(dwc->dev, "%s: failed to setup phy\n",\r
+                                       __func__);\r
+                       goto err1;\r
+               }\r
+\r
+               ret = dwc3_core_init(dwc);\r
+               if (ret) {\r
+                       dev_err(dwc->dev, "%s: failed to reinitialize core\n",\r
+                                       __func__);\r
+                       goto err1;\r
+               }\r
+\r
+               dwc3_otg_set_peripheral_mode(dotg);\r
+               ret = usb_gadget_vbus_connect(otg->gadget);\r
+               if (ret) {\r
+                       dev_err(dwc->dev, "%s: vbus connect failed\n",\r
+                                       __func__);\r
+                       goto err2;\r
+               }\r
+\r
+       } else {\r
+               if (dwc->is_not_vbus_pad)\r
+                       dwc3_gadget_disconnect_proc(dwc);\r
+               /* avoid missing disconnect interrupt */\r
+               ret = wait_for_completion_timeout(&dwc->disconnect,\r
+                               msecs_to_jiffies(200));\r
+               if (!ret) {\r
+                       dev_err(dwc->dev, "%s: disconnect completion timeout\n",\r
+                                       __func__);\r
+                       return ret;\r
+               }\r
+               ret = usb_gadget_vbus_disconnect(otg->gadget);\r
+               if (ret)\r
+                       dev_err(dwc->dev, "%s: vbus disconnect failed\n",\r
+                                       __func__);\r
+err2:\r
+               dwc3_core_exit(dwc);\r
+err1:\r
+               pm_runtime_put_sync(dev);\r
+               wake_unlock(&dotg->wakelock);\r
+       }\r
+\r
+       return ret;\r
+}\r
+\r
+static struct otg_fsm_ops dwc3_otg_fsm_ops = {\r
+       .drv_vbus       = dwc3_otg_drv_vbus,\r
+       .start_host     = dwc3_otg_start_host,\r
+       .start_gadget   = dwc3_otg_start_gadget,\r
+};\r
+\r
+/* -------------------------------------------------------------------------- */\r
+\r
+void dwc3_otg_run_sm(struct otg_fsm *fsm)\r
+{\r
+       struct dwc3_otg *dotg = container_of(fsm, struct dwc3_otg, fsm);\r
+       int             state_changed;\r
+\r
+       /* Prevent running SM on early system resume */\r
+       if (!dotg->ready)\r
+               return;\r
+\r
+       mutex_lock(&fsm->lock);\r
+\r
+       do {\r
+               state_changed = dwc3_otg_statemachine(fsm);\r
+       } while (state_changed > 0);\r
+\r
+       mutex_unlock(&fsm->lock);\r
+}\r
+\r
+/* Bind/Unbind the peripheral controller driver */\r
+static int dwc3_otg_set_peripheral(struct usb_otg *otg,\r
+                               struct usb_gadget *gadget)\r
+{\r
+       struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg);\r
+       struct otg_fsm  *fsm = &dotg->fsm;\r
+       struct device   *dev = dotg->dwc->dev;\r
+\r
+       if (gadget) {\r
+               dev_info(dev, "Binding gadget %s\n", gadget->name);\r
+\r
+               otg->gadget = gadget;\r
+       } else {\r
+               dev_info(dev, "Unbinding gadget\n");\r
+\r
+               mutex_lock(&fsm->lock);\r
+\r
+               if (otg->state == OTG_STATE_B_PERIPHERAL) {\r
+                       /* Reset OTG Statemachine */\r
+                       fsm->reset = 1;\r
+                       dwc3_otg_statemachine(fsm);\r
+                       fsm->reset = 0;\r
+               }\r
+               otg->gadget = NULL;\r
+\r
+               mutex_unlock(&fsm->lock);\r
+\r
+               dwc3_otg_run_sm(fsm);\r
+       }\r
+\r
+       return 0;\r
+}\r
+\r
+static int dwc3_otg_get_id_state(struct dwc3_otg *dotg)\r
+{\r
+       u32 reg = dwc3_readl(dotg->regs, DWC3_OSTS);\r
+\r
+       return !!(reg & DWC3_OTG_OSTS_CONIDSTS);\r
+}\r
+\r
+static int dwc3_otg_get_b_sess_state(struct dwc3_otg *dotg)\r
+{\r
+       u32 reg = dwc3_readl(dotg->regs, DWC3_OSTS);\r
+\r
+       return !!(reg & DWC3_OTG_OSTS_BSESVALID);\r
+}\r
+\r
+static irqreturn_t dwc3_otg_interrupt(int irq, void *_dotg)\r
+{\r
+       struct dwc3_otg *dotg = (struct dwc3_otg *)_dotg;\r
+       struct otg_fsm  *fsm = &dotg->fsm;\r
+       u32 oevt, handled_events = 0;\r
+       irqreturn_t ret = IRQ_NONE;\r
+\r
+       oevt = dwc3_readl(dotg->regs, DWC3_OEVT);\r
+\r
+       /* ID */\r
+       if (oevt & DWC3_OEVTEN_OTGCONIDSTSCHNGEVNT) {\r
+               fsm->id = dwc3_otg_get_id_state(dotg);\r
+               handled_events |= DWC3_OEVTEN_OTGCONIDSTSCHNGEVNT;\r
+       }\r
+\r
+       /* VBus */\r
+       if (oevt & DWC3_OEVTEN_OTGBDEVVBUSCHNGEVNT) {\r
+               fsm->b_sess_vld = dwc3_otg_get_b_sess_state(dotg);\r
+               handled_events |= DWC3_OEVTEN_OTGBDEVVBUSCHNGEVNT;\r
+       }\r
+\r
+       if (handled_events) {\r
+               dwc3_writel(dotg->regs, DWC3_OEVT, handled_events);\r
+               ret = IRQ_WAKE_THREAD;\r
+       }\r
+\r
+       return ret;\r
+}\r
+\r
+static irqreturn_t dwc3_otg_thread_interrupt(int irq, void *_dotg)\r
+{\r
+       struct dwc3_otg *dotg = (struct dwc3_otg *)_dotg;\r
+\r
+       dwc3_otg_run_sm(&dotg->fsm);\r
+\r
+       return IRQ_HANDLED;\r
+}\r
+\r
+static void dwc3_otg_enable_irq(struct dwc3_otg *dotg)\r
+{\r
+       /* Enable only connector ID status & VBUS change events */\r
+       dwc3_writel(dotg->regs, DWC3_OEVTEN,\r
+                       DWC3_OEVTEN_OTGCONIDSTSCHNGEVNT |\r
+                       DWC3_OEVTEN_OTGBDEVVBUSCHNGEVNT);\r
+}\r
+\r
+static void dwc3_otg_disable_irq(struct dwc3_otg *dotg)\r
+{\r
+       dwc3_writel(dotg->regs, DWC3_OEVTEN, 0x0);\r
+}\r
+\r
+static void dwc3_otg_reset(struct dwc3_otg *dotg)\r
+{\r
+       /*\r
+        * OCFG[2] - OTG-Version = 0\r
+        * OCFG[1] - HNPCap = 0\r
+        * OCFG[0] - SRPCap = 0\r
+        */\r
+       dwc3_writel(dotg->regs, DWC3_OCFG, 0x0);\r
+\r
+       /*\r
+        * OCTL[6] - PeriMode = 1\r
+        * OCTL[5] - PrtPwrCtl = 0\r
+        * OCTL[4] - HNPReq = 0\r
+        * OCTL[3] - SesReq = 0\r
+        * OCTL[2] - TermSelDLPulse = 0\r
+        * OCTL[1] - DevSetHNPEn = 0\r
+        * OCTL[0] - HstSetHNPEn = 0\r
+        */\r
+       dwc3_writel(dotg->regs, DWC3_OCTL, DWC3_OTG_OCTL_PERIMODE);\r
+\r
+       /* Clear all otg events (interrupts) indications  */\r
+       dwc3_writel(dotg->regs, DWC3_OEVT, DWC3_OEVT_CLEAR_ALL);\r
+}\r
+\r
+/* -------------------------------------------------------------------------- */\r
+\r
+static ssize_t\r
+dwc3_otg_show_state(struct device *dev,\r
+               struct device_attribute *attr, char *buf)\r
+{\r
+       struct dwc3     *dwc = dev_get_drvdata(dev);\r
+       struct usb_otg  *otg = &dwc->dotg->otg;\r
+\r
+       return snprintf(buf, PAGE_SIZE, "%s\n",\r
+                       usb_otg_state_string(otg->state));\r
+}\r
+\r
+static DEVICE_ATTR(state, S_IRUSR | S_IRGRP,\r
+       dwc3_otg_show_state, NULL);\r
+\r
+static ssize_t\r
+dwc3_otg_show_b_sess(struct device *dev,\r
+               struct device_attribute *attr, char *buf)\r
+{\r
+       struct dwc3     *dwc = dev_get_drvdata(dev);\r
+       struct otg_fsm  *fsm = &dwc->dotg->fsm;\r
+\r
+       return snprintf(buf, PAGE_SIZE, "%d\n", fsm->b_sess_vld);\r
+}\r
+\r
+static ssize_t\r
+dwc3_otg_store_b_sess(struct device *dev,\r
+               struct device_attribute *attr, const char *buf, size_t n)\r
+{\r
+       struct dwc3     *dwc = dev_get_drvdata(dev);\r
+       struct otg_fsm  *fsm = &dwc->dotg->fsm;\r
+       int             b_sess_vld;\r
+\r
+       if (sscanf(buf, "%d", &b_sess_vld) != 1)\r
+               return -EINVAL;\r
+\r
+       fsm->b_sess_vld = !!b_sess_vld;\r
+\r
+       dwc3_otg_run_sm(fsm);\r
+\r
+       return n;\r
+}\r
+\r
+static DEVICE_ATTR(b_sess, S_IWUSR | S_IRUSR | S_IRGRP,\r
+       dwc3_otg_show_b_sess, dwc3_otg_store_b_sess);\r
+\r
+static ssize_t\r
+dwc3_otg_show_id(struct device *dev,\r
+               struct device_attribute *attr, char *buf)\r
+{\r
+       struct dwc3     *dwc = dev_get_drvdata(dev);\r
+       struct otg_fsm  *fsm = &dwc->dotg->fsm;\r
+\r
+       return snprintf(buf, PAGE_SIZE, "%d\n", fsm->id);\r
+}\r
+\r
+static ssize_t\r
+dwc3_otg_store_id(struct device *dev,\r
+               struct device_attribute *attr, const char *buf, size_t n)\r
+{\r
+       struct dwc3     *dwc = dev_get_drvdata(dev);\r
+       struct otg_fsm  *fsm = &dwc->dotg->fsm;\r
+       int id;\r
+\r
+       if (sscanf(buf, "%d", &id) != 1)\r
+               return -EINVAL;\r
+\r
+       fsm->id = !!id;\r
+\r
+       dwc3_otg_run_sm(fsm);\r
+\r
+       return n;\r
+}\r
+\r
+static DEVICE_ATTR(id, S_IWUSR | S_IRUSR | S_IRGRP,\r
+       dwc3_otg_show_id, dwc3_otg_store_id);\r
+\r
+static struct attribute *dwc3_otg_attributes[] = {\r
+       &dev_attr_id.attr,\r
+       &dev_attr_b_sess.attr,\r
+       &dev_attr_state.attr,\r
+       NULL\r
+};\r
+\r
+static const struct attribute_group dwc3_otg_attr_group = {\r
+       .attrs = dwc3_otg_attributes,\r
+};\r
+\r
+/**\r
+ * dwc3_otg_start\r
+ * @dwc: pointer to our controller context structure\r
+ */\r
+int dwc3_otg_start(struct dwc3 *dwc)\r
+{\r
+       struct dwc3_otg *dotg = dwc->dotg;\r
+       struct otg_fsm  *fsm = &dotg->fsm;\r
+       int             ret;\r
+\r
+       if (dotg->ext_otg_ops) {\r
+               ret = dwc3_ext_otg_start(dotg);\r
+               if (ret) {\r
+                       dev_err(dwc->dev, "failed to start external OTG\n");\r
+                       return ret;\r
+               }\r
+       } else {\r
+               dotg->regs = dwc->regs;\r
+\r
+               dwc3_otg_reset(dotg);\r
+\r
+               dotg->fsm.id = dwc3_otg_get_id_state(dotg);\r
+               dotg->fsm.b_sess_vld = dwc3_otg_get_b_sess_state(dotg);\r
+\r
+               dotg->irq = platform_get_irq(to_platform_device(dwc->dev), 0);\r
+               ret = devm_request_threaded_irq(dwc->dev, dotg->irq,\r
+                               dwc3_otg_interrupt,\r
+                               dwc3_otg_thread_interrupt,\r
+                               IRQF_SHARED, "dwc3-otg", dotg);\r
+               if (ret) {\r
+                       dev_err(dwc->dev, "failed to request irq #%d --> %d\n",\r
+                                       dotg->irq, ret);\r
+                       return ret;\r
+               }\r
+\r
+               dwc3_otg_enable_irq(dotg);\r
+       }\r
+\r
+       dotg->ready = 1;\r
+\r
+       dwc3_otg_run_sm(fsm);\r
+\r
+       return 0;\r
+}\r
+\r
+/**\r
+ * dwc3_otg_stop\r
+ * @dwc: pointer to our controller context structure\r
+ */\r
+void dwc3_otg_stop(struct dwc3 *dwc)\r
+{\r
+       struct dwc3_otg         *dotg = dwc->dotg;\r
+\r
+       if (dotg->ext_otg_ops) {\r
+               dwc3_ext_otg_stop(dotg);\r
+       } else {\r
+               dwc3_otg_disable_irq(dotg);\r
+               free_irq(dotg->irq, dotg);\r
+       }\r
+\r
+       dotg->ready = 0;\r
+}\r
+\r
+/* -------------------------------------------------------------------------- */\r
+bool otg_is_connect(void)\r
+{\r
+       if (otg_connection)\r
+               return true;\r
+       else\r
+               return false;\r
+}\r
+EXPORT_SYMBOL_GPL(otg_is_connect);\r
+\r
+int dwc3_otg_init(struct dwc3 *dwc)\r
+{\r
+       struct dwc3_otg *dotg;\r
+       struct dwc3_ext_otg_ops *ops = NULL;\r
+       int ret = 0;\r
+\r
+       dev_info(dwc->dev, "%s\n", __func__);\r
+\r
+       /* EXYNOS SoCs don't have HW OTG, but support SW OTG. */\r
+       ops = dwc3_otg_exynos_rsw_probe(dwc);\r
+       if (!ops)\r
+               return 0;\r
+\r
+       /* Allocate and init otg instance */\r
+       dotg = devm_kzalloc(dwc->dev, sizeof(struct dwc3_otg), GFP_KERNEL);\r
+       if (!dotg) {\r
+               dev_err(dwc->dev, "unable to allocate dwc3_otg\n");\r
+               return -ENOMEM;\r
+       }\r
+\r
+       /* This reference is used by dwc3 modules for checking otg existance */\r
+       dwc->dotg = dotg;\r
+       dotg->dwc = dwc;\r
+\r
+       ret = of_property_read_u32(dwc->dev->of_node,"ldos", &dotg->ldos);\r
+       if (ret < 0) {\r
+               dev_err(dwc->dev, "can't get ldo information\n");\r
+               return -EINVAL;\r
+       } else {\r
+               if (dotg->ldos) {\r
+                       dev_info(dwc->dev, "have %d LDOs for supporting USB L2 suspend \n", dotg->ldos);\r
+                       dotg->ldo_num = (int *)devm_kmalloc(dwc->dev, sizeof(int) * (dotg->ldos),\r
+                               GFP_KERNEL);\r
+                       ret = of_property_read_u32_array(dwc->dev->of_node,\r
+                                       "ldo_number", dotg->ldo_num, dotg->ldos);\r
+               } else {\r
+                       dev_info(dwc->dev, "don't have LDOs for supporting USB L2 suspend \n");\r
+               }\r
+       }\r
+\r
+       dotg->ext_otg_ops = ops;\r
+\r
+       dotg->otg.set_peripheral = dwc3_otg_set_peripheral;\r
+       dotg->otg.set_host = NULL;\r
+\r
+       dotg->otg.state = OTG_STATE_UNDEFINED;\r
+\r
+       mutex_init(&dotg->fsm.lock);\r
+       dotg->fsm.ops = &dwc3_otg_fsm_ops;\r
+       dotg->fsm.otg = &dotg->otg;\r
+\r
+       dotg->vbus_reg = devm_regulator_get(dwc->dev->parent, "dwc3-vbus");\r
+       if (IS_ERR(dotg->vbus_reg))\r
+               dev_err(dwc->dev, "failed to obtain vbus regulator\n");\r
+\r
+       if (dotg->ext_otg_ops) {\r
+               ret = dwc3_ext_otg_setup(dotg);\r
+               if (ret) {\r
+                       dev_err(dwc->dev, "failed to setup OTG\n");\r
+                       return ret;\r
+               }\r
+       }\r
+\r
+       wake_lock_init(&dotg->wakelock, WAKE_LOCK_SUSPEND, "dwc3-otg");\r
+\r
+       ret = sysfs_create_group(&dwc->dev->kobj, &dwc3_otg_attr_group);\r
+       if (ret)\r
+               dev_err(dwc->dev, "failed to create dwc3 otg attributes\n");\r
+\r
+#ifdef CONFIG_SOC_EXYNOS9810\r
+       register_usb_is_connect(NULL);\r
+#endif\r
+\r
+       return 0;\r
+}\r
+\r
+void dwc3_otg_exit(struct dwc3 *dwc)\r
+{\r
+       struct dwc3_otg *dotg = dwc->dotg;\r
+\r
+       if (!dotg->ext_otg_ops)\r
+               return;\r
+\r
+       dwc3_ext_otg_exit(dotg);\r
+\r
+       sysfs_remove_group(&dwc->dev->kobj, &dwc3_otg_attr_group);\r
+       wake_lock_destroy(&dotg->wakelock);\r
+       free_irq(dotg->irq, dotg);\r
+       dotg->otg.state = OTG_STATE_UNDEFINED;\r
+       kfree(dotg);\r
+       dwc->dotg = NULL;\r
+}\r
diff --git a/drivers/usb/dwc3/otg.h b/drivers/usb/dwc3/otg.h
new file mode 100644 (file)
index 0000000..d51dafd
--- /dev/null
@@ -0,0 +1,108 @@
+/**\r
+ * otg.c - DesignWare USB3 DRD Controller OTG\r
+ *\r
+ * Copyright (c) 2012, Code Aurora Forum. All rights reserved.\r
+ * Copyright (c) 2013 Samsung Electronics Co., Ltd.\r
+ *             http://www.samsung.com\r
+ *\r
+ * Authors: Ido Shayevitz <idos@codeaurora.org>\r
+ *         Anton Tikhomirov <av.tikhomirov@samsung.com>\r
+ *\r
+ * This program is free software: you can redistribute it and/or modify\r
+ * it under the terms of the GNU General Public License version 2  of\r
+ * the License as published by the Free Software Foundation.\r
+ *\r
+ * This program is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+ * GNU General Public License for more details.\r
+ */\r
+\r
+#ifndef __LINUX_USB_DWC3_OTG_H\r
+#define __LINUX_USB_DWC3_OTG_H\r
+#include <linux/wakelock.h>\r
+#include <linux/usb/otg-fsm.h>\r
+\r
+struct dwc3_ext_otg_ops {\r
+       int     (*setup)(struct device *dev, struct otg_fsm *fsm);\r
+       void    (*exit)(struct device *dev);\r
+       int     (*start) (struct device *dev);\r
+       void    (*stop)(struct device *dev);\r
+};\r
+\r
+/**\r
+ * struct dwc3_otg: OTG driver data. Shared by HCD and DCD.\r
+ * @otg: USB OTG Transceiver structure.\r
+ * @fsm: OTG Final State Machine.\r
+ * @dwc: pointer to our controller context structure.\r
+ * @irq: IRQ number assigned for HSUSB controller.\r
+ * @regs: ioremapped register base address.\r
+ * @wakelock: prevents the system from entering suspend while\r
+ *             host or peripheral mode is active.\r
+ * @vbus_reg: Vbus regulator.\r
+ * @ready: is one when OTG is ready for operation.\r
+ * @ext_otg_ops: external OTG engine ops.\r
+ */\r
+struct dwc3_otg {\r
+       struct usb_otg          otg;\r
+       struct otg_fsm          fsm;\r
+       struct dwc3             *dwc;\r
+       int                     irq;\r
+       void __iomem            *regs;\r
+       struct wake_lock        wakelock;\r
+\r
+       unsigned                ready:1;\r
+       int                     otg_connection;\r
+\r
+       struct regulator        *vbus_reg;\r
+       int                     *ldo_num;\r
+       int                     ldos;\r
+\r
+       struct dwc3_ext_otg_ops *ext_otg_ops;\r
+};\r
+\r
+static inline int dwc3_ext_otg_setup(struct dwc3_otg *dotg)\r
+{\r
+       struct device *dev = dotg->dwc->dev->parent;\r
+\r
+       if (!dotg->ext_otg_ops->setup)\r
+               return -EOPNOTSUPP;\r
+       return dotg->ext_otg_ops->setup(dev, &dotg->fsm);\r
+}\r
+\r
+static inline int dwc3_ext_otg_exit(struct dwc3_otg *dotg)\r
+{\r
+       struct device *dev = dotg->dwc->dev->parent;\r
+\r
+       if (!dotg->ext_otg_ops->exit)\r
+               return -EOPNOTSUPP;\r
+       dotg->ext_otg_ops->exit(dev);\r
+       return 0;\r
+}\r
+\r
+static inline int dwc3_ext_otg_start(struct dwc3_otg *dotg)\r
+{\r
+       struct device *dev = dotg->dwc->dev->parent;\r
+\r
+       if (!dotg->ext_otg_ops->start)\r
+               return -EOPNOTSUPP;\r
+       return dotg->ext_otg_ops->start(dev);\r
+}\r
+\r
+static inline int dwc3_ext_otg_stop(struct dwc3_otg *dotg)\r
+{\r
+       struct device *dev = dotg->dwc->dev->parent;\r
+\r
+       if (!dotg->ext_otg_ops->stop)\r
+               return -EOPNOTSUPP;\r
+       dotg->ext_otg_ops->stop(dev);\r
+       return 0;\r
+}\r
+\r
+bool dwc3_exynos_rsw_available(struct device *dev);\r
+int dwc3_exynos_rsw_setup(struct device *dev, struct otg_fsm *fsm);\r
+void dwc3_exynos_rsw_exit(struct device *dev);\r
+int dwc3_exynos_rsw_start(struct device *dev);\r
+void dwc3_exynos_rsw_stop(struct device *dev);\r
+\r
+#endif /* __LINUX_USB_DWC3_OTG_H */\r
diff --git a/drivers/usb/gadget/function/f_adb.c b/drivers/usb/gadget/function/f_adb.c
new file mode 100644 (file)
index 0000000..a8829e2
--- /dev/null
@@ -0,0 +1,827 @@
+/*\r
+ * Gadget Driver for Android ADB\r
+ *\r
+ * Copyright (C) 2008 Google, Inc.\r
+ * Author: Mike Lockwood <lockwood@android.com>\r
+ *\r
+ * This software is licensed under the terms of the GNU General Public\r
+ * License version 2, as published by the Free Software Foundation, and\r
+ * may be copied, distributed, and modified under those terms.\r
+ *\r
+ * This program is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+ * GNU General Public License for more details.\r
+ *\r
+ */\r
+\r
+#include <linux/module.h>\r
+#include <linux/init.h>\r
+#include <linux/poll.h>\r
+#include <linux/delay.h>\r
+#include <linux/wait.h>\r
+#include <linux/err.h>\r
+#include <linux/interrupt.h>\r
+#include <linux/sched.h>\r
+#include <linux/types.h>\r
+#include <linux/device.h>\r
+#include <linux/miscdevice.h>\r
+#include <linux/configfs.h>\r
+#include <linux/usb/composite.h>\r
+\r
+#include "../configfs.h"\r
+\r
+#define ADB_BULK_BUFFER_SIZE           4096\r
+#define MAX_INST_NAME_LEN              40\r
+\r
+/* number of tx requests to allocate */\r
+#define TX_REQ_MAX 4\r
+\r
+static const char adb_shortname[] = "android_adb";\r
+\r
+struct adb_dev {\r
+       struct usb_function function;\r
+       struct usb_composite_dev *cdev;\r
+       spinlock_t lock;\r
+\r
+       struct usb_ep *ep_in;\r
+       struct usb_ep *ep_out;\r
+\r
+       int online;\r
+       int error;\r
+\r
+       atomic_t read_excl;\r
+       atomic_t write_excl;\r
+       atomic_t open_excl;\r
+\r
+       struct list_head tx_idle;\r
+\r
+       wait_queue_head_t read_wq;\r
+       wait_queue_head_t write_wq;\r
+       struct usb_request *rx_req;\r
+       int rx_done;\r
+};\r
+\r
+struct adb_instance {\r
+       const char *name;\r
+       struct usb_function_instance func_inst;\r
+       struct adb_dev *dev;\r
+};\r
+\r
+static struct usb_interface_descriptor adb_interface_desc = {\r
+       .bLength                = USB_DT_INTERFACE_SIZE,\r
+       .bDescriptorType        = USB_DT_INTERFACE,\r
+       .bInterfaceNumber       = 0,\r
+       .bNumEndpoints          = 2,\r
+       .bInterfaceClass        = 0xFF,\r
+       .bInterfaceSubClass     = 0x42,\r
+       .bInterfaceProtocol     = 1,\r
+};\r
+\r
+static struct usb_endpoint_descriptor adb_superspeed_in_desc = {\r
+       .bLength                = USB_DT_ENDPOINT_SIZE,\r
+       .bDescriptorType        = USB_DT_ENDPOINT,\r
+       .bEndpointAddress       = USB_DIR_IN,\r
+       .bmAttributes           = USB_ENDPOINT_XFER_BULK,\r
+       .wMaxPacketSize         = __constant_cpu_to_le16(1024),\r
+};\r
+\r
+static struct usb_endpoint_descriptor adb_superspeed_out_desc = {\r
+       .bLength                = USB_DT_ENDPOINT_SIZE,\r
+       .bDescriptorType        = USB_DT_ENDPOINT,\r
+       .bEndpointAddress       = USB_DIR_OUT,\r
+       .bmAttributes           = USB_ENDPOINT_XFER_BULK,\r
+       .wMaxPacketSize         = __constant_cpu_to_le16(1024),\r
+};\r
+\r
+static struct usb_ss_ep_comp_descriptor adb_superspeed_bulk_comp_desc = {\r
+       .bLength =              sizeof adb_superspeed_bulk_comp_desc,\r
+       .bDescriptorType =      USB_DT_SS_ENDPOINT_COMP,\r
+\r
+       /* the following 2 values can be tweaked if necessary */\r
+       /* .bMaxBurst =         0, */\r
+       /* .bmAttributes =      0, */\r
+};\r
+\r
+static struct usb_endpoint_descriptor adb_highspeed_in_desc = {\r
+       .bLength                = USB_DT_ENDPOINT_SIZE,\r
+       .bDescriptorType        = USB_DT_ENDPOINT,\r
+       .bEndpointAddress       = USB_DIR_IN,\r
+       .bmAttributes           = USB_ENDPOINT_XFER_BULK,\r
+       .wMaxPacketSize         = __constant_cpu_to_le16(512),\r
+};\r
+\r
+static struct usb_endpoint_descriptor adb_highspeed_out_desc = {\r
+       .bLength                = USB_DT_ENDPOINT_SIZE,\r
+       .bDescriptorType        = USB_DT_ENDPOINT,\r
+       .bEndpointAddress       = USB_DIR_OUT,\r
+       .bmAttributes           = USB_ENDPOINT_XFER_BULK,\r
+       .wMaxPacketSize         = __constant_cpu_to_le16(512),\r
+};\r
+\r
+static struct usb_endpoint_descriptor adb_fullspeed_in_desc = {\r
+       .bLength                = USB_DT_ENDPOINT_SIZE,\r
+       .bDescriptorType        = USB_DT_ENDPOINT,\r
+       .bEndpointAddress       = USB_DIR_IN,\r
+       .bmAttributes           = USB_ENDPOINT_XFER_BULK,\r
+};\r
+\r
+static struct usb_endpoint_descriptor adb_fullspeed_out_desc = {\r
+       .bLength                = USB_DT_ENDPOINT_SIZE,\r
+       .bDescriptorType        = USB_DT_ENDPOINT,\r
+       .bEndpointAddress       = USB_DIR_OUT,\r
+       .bmAttributes           = USB_ENDPOINT_XFER_BULK,\r
+};\r
+\r
+static struct usb_descriptor_header *fs_adb_descs[] = {\r
+       (struct usb_descriptor_header *) &adb_interface_desc,\r
+       (struct usb_descriptor_header *) &adb_fullspeed_in_desc,\r
+       (struct usb_descriptor_header *) &adb_fullspeed_out_desc,\r
+       NULL,\r
+};\r
+\r
+static struct usb_descriptor_header *hs_adb_descs[] = {\r
+       (struct usb_descriptor_header *) &adb_interface_desc,\r
+       (struct usb_descriptor_header *) &adb_highspeed_in_desc,\r
+       (struct usb_descriptor_header *) &adb_highspeed_out_desc,\r
+       NULL,\r
+};\r
+\r
+static struct usb_descriptor_header *ss_adb_descs[] = {\r
+       (struct usb_descriptor_header *) &adb_interface_desc,\r
+       (struct usb_descriptor_header *) &adb_superspeed_in_desc,\r
+       (struct usb_descriptor_header *) &adb_superspeed_bulk_comp_desc,\r
+       (struct usb_descriptor_header *) &adb_superspeed_out_desc,\r
+       (struct usb_descriptor_header *) &adb_superspeed_bulk_comp_desc,\r
+       NULL,\r
+};\r
+\r
+#ifndef CONFIG_USB_CONFIGFS_UEVENT\r
+static void adb_ready_callback(void);\r
+static void adb_closed_callback(void);\r
+#endif\r
+\r
+/* temporary variable used between adb_open() and adb_gadget_bind() */\r
+static struct adb_dev *_adb_dev;\r
+\r
+static inline struct adb_dev *func_to_adb(struct usb_function *f)\r
+{\r
+       return container_of(f, struct adb_dev, function);\r
+}\r
+\r
+\r
+static struct usb_request *adb_request_new(struct usb_ep *ep, int buffer_size)\r
+{\r
+       struct usb_request *req = usb_ep_alloc_request(ep, GFP_KERNEL);\r
+       if (!req)\r
+               return NULL;\r
+\r
+       /* now allocate buffers for the requests */\r
+       req->buf = kmalloc(buffer_size, GFP_KERNEL);\r
+       if (!req->buf) {\r
+               usb_ep_free_request(ep, req);\r
+               return NULL;\r
+       }\r
+\r
+       return req;\r
+}\r
+\r
+static void adb_request_free(struct usb_request *req, struct usb_ep *ep)\r
+{\r
+       if (req) {\r
+               kfree(req->buf);\r
+               usb_ep_free_request(ep, req);\r
+       }\r
+}\r
+\r
+static inline int adb_lock(atomic_t *excl)\r
+{\r
+       if (atomic_inc_return(excl) == 1) {\r
+               return 0;\r
+       } else {\r
+               atomic_dec(excl);\r
+               return -1;\r
+       }\r
+}\r
+\r
+static inline void adb_unlock(atomic_t *excl)\r
+{\r
+       atomic_dec(excl);\r
+}\r
+\r
+/* add a request to the tail of a list */\r
+void adb_req_put(struct adb_dev *dev, struct list_head *head,\r
+               struct usb_request *req)\r
+{\r
+       unsigned long flags;\r
+\r
+       spin_lock_irqsave(&dev->lock, flags);\r
+       list_add_tail(&req->list, head);\r
+       spin_unlock_irqrestore(&dev->lock, flags);\r
+}\r
+\r
+/* remove a request from the head of a list */\r
+struct usb_request *adb_req_get(struct adb_dev *dev, struct list_head *head)\r
+{\r
+       unsigned long flags;\r
+       struct usb_request *req;\r
+\r
+       spin_lock_irqsave(&dev->lock, flags);\r
+       if (list_empty(head)) {\r
+               req = 0;\r
+       } else {\r
+               req = list_first_entry(head, struct usb_request, list);\r
+               list_del(&req->list);\r
+       }\r
+       spin_unlock_irqrestore(&dev->lock, flags);\r
+       return req;\r
+}\r
+\r
+static void adb_complete_in(struct usb_ep *ep, struct usb_request *req)\r
+{\r
+       struct adb_dev *dev = _adb_dev;\r
+\r
+       if (req->status != 0)\r
+               dev->error = 1;\r
+\r
+       adb_req_put(dev, &dev->tx_idle, req);\r
+\r
+       wake_up(&dev->write_wq);\r
+}\r
+\r
+static void adb_complete_out(struct usb_ep *ep, struct usb_request *req)\r
+{\r
+       struct adb_dev *dev = _adb_dev;\r
+\r
+       dev->rx_done = 1;\r
+       if (req->status != 0 && req->status != -ECONNRESET)\r
+               dev->error = 1;\r
+\r
+       wake_up(&dev->read_wq);\r
+}\r
+\r
+static int adb_create_bulk_endpoints(struct adb_dev *dev,\r
+                               struct usb_endpoint_descriptor *in_desc,\r
+                               struct usb_endpoint_descriptor *out_desc)\r
+{\r
+       struct usb_composite_dev *cdev = dev->cdev;\r
+       struct usb_request *req;\r
+       struct usb_ep *ep;\r
+       int i;\r
+\r
+       DBG(cdev, "create_bulk_endpoints dev: %p\n", dev);\r
+\r
+       ep = usb_ep_autoconfig(cdev->gadget, in_desc);\r
+       if (!ep) {\r
+               DBG(cdev, "usb_ep_autoconfig for ep_in failed\n");\r
+               return -ENODEV;\r
+       }\r
+       DBG(cdev, "usb_ep_autoconfig for ep_in got %s\n", ep->name);\r
+       ep->driver_data = dev;          /* claim the endpoint */\r
+       dev->ep_in = ep;\r
+\r
+       ep = usb_ep_autoconfig(cdev->gadget, out_desc);\r
+       if (!ep) {\r
+               DBG(cdev, "usb_ep_autoconfig for ep_out failed\n");\r
+               return -ENODEV;\r
+       }\r
+       DBG(cdev, "usb_ep_autoconfig for adb ep_out got %s\n", ep->name);\r
+       ep->driver_data = dev;          /* claim the endpoint */\r
+       dev->ep_out = ep;\r
+\r
+       /* now allocate requests for our endpoints */\r
+       req = adb_request_new(dev->ep_out, ADB_BULK_BUFFER_SIZE);\r
+       if (!req)\r
+               goto fail;\r
+       req->complete = adb_complete_out;\r
+       dev->rx_req = req;\r
+\r
+       for (i = 0; i < TX_REQ_MAX; i++) {\r
+               req = adb_request_new(dev->ep_in, ADB_BULK_BUFFER_SIZE);\r
+               if (!req)\r
+                       goto fail;\r
+               req->complete = adb_complete_in;\r
+               adb_req_put(dev, &dev->tx_idle, req);\r
+       }\r
+\r
+       return 0;\r
+\r
+fail:\r
+       printk(KERN_ERR "adb_bind() could not allocate requests\n");\r
+       return -1;\r
+}\r
+\r
+static ssize_t adb_read(struct file *fp, char __user *buf,\r
+                               size_t count, loff_t *pos)\r
+{\r
+       struct adb_dev *dev = fp->private_data;\r
+       struct usb_request *req;\r
+       int r = count, xfer;\r
+       int maxp;\r
+       int ret;\r
+\r
+       pr_debug("adb_read(%d)\n", (int)count);\r
+       if (!_adb_dev)\r
+               return -ENODEV;\r
+\r
+       if (adb_lock(&dev->read_excl))\r
+               return -EBUSY;\r
+\r
+       /* we will block until we're online */\r
+       while (!(dev->online || dev->error)) {\r
+               pr_debug("adb_read: waiting for online state\n");\r
+               ret = wait_event_interruptible(dev->read_wq,\r
+                               (dev->online || dev->error));\r
+               if (ret < 0) {\r
+                       adb_unlock(&dev->read_excl);\r
+                       return ret;\r
+               }\r
+       }\r
+       if (dev->error) {\r
+               r = -EIO;\r
+               goto done;\r
+       }\r
+\r
+       maxp = usb_endpoint_maxp(dev->ep_out->desc);\r
+       count = round_up(count, maxp);\r
+\r
+       if (count > ADB_BULK_BUFFER_SIZE)\r
+               return -EINVAL;\r
+\r
+requeue_req:\r
+       /* queue a request */\r
+       req = dev->rx_req;\r
+       req->length = count;\r
+       dev->rx_done = 0;\r
+       ret = usb_ep_queue(dev->ep_out, req, GFP_ATOMIC);\r
+       if (ret < 0) {\r
+               pr_debug("adb_read: failed to queue req %p (%d)\n", req, ret);\r
+               r = -EIO;\r
+               dev->error = 1;\r
+               goto done;\r
+       } else {\r
+               pr_debug("rx %p queue\n", req);\r
+       }\r
+\r
+       /* wait for a request to complete */\r
+       ret = wait_event_interruptible(dev->read_wq, dev->rx_done);\r
+       if (ret < 0) {\r
+               if (ret != -ERESTARTSYS)\r
+                       dev->error = 1;\r
+               r = ret;\r
+               usb_ep_dequeue(dev->ep_out, req);\r
+               goto done;\r
+       }\r
+       if (!dev->error) {\r
+               /* If we got a 0-len packet, throw it back and try again. */\r
+               if (req->actual == 0)\r
+                       goto requeue_req;\r
+\r
+               pr_debug("rx %p %d\n", req, req->actual);\r
+               xfer = (req->actual < count) ? req->actual : count;\r
+               if (copy_to_user(buf, req->buf, xfer))\r
+                       r = -EFAULT;\r
+\r
+       } else\r
+               r = -EIO;\r
+\r
+done:\r
+       adb_unlock(&dev->read_excl);\r
+       pr_debug("adb_read returning %d\n", r);\r
+       return r;\r
+}\r
+\r
+static ssize_t adb_write(struct file *fp, const char __user *buf,\r
+                                size_t count, loff_t *pos)\r
+{\r
+       struct adb_dev *dev = fp->private_data;\r
+       struct usb_request *req = 0;\r
+       int r = count, xfer;\r
+       int ret;\r
+\r
+       if (!_adb_dev)\r
+               return -ENODEV;\r
+       pr_debug("adb_write(%d)\n", (int)count);\r
+\r
+       if (adb_lock(&dev->write_excl))\r
+               return -EBUSY;\r
+\r
+       while (count > 0) {\r
+               if (dev->error) {\r
+                       pr_debug("adb_write dev->error\n");\r
+                       r = -EIO;\r
+                       break;\r
+               }\r
+\r
+               /* get an idle tx request to use */\r
+               req = 0;\r
+               ret = wait_event_interruptible(dev->write_wq,\r
+                       (req = adb_req_get(dev, &dev->tx_idle)) || dev->error);\r
+\r
+               if (ret < 0) {\r
+                       r = ret;\r
+                       break;\r
+               }\r
+\r
+               if (req != 0) {\r
+                       if (count > ADB_BULK_BUFFER_SIZE)\r
+                               xfer = ADB_BULK_BUFFER_SIZE;\r
+                       else\r
+                               xfer = count;\r
+                       if (copy_from_user(req->buf, buf, xfer)) {\r
+                               r = -EFAULT;\r
+                               break;\r
+                       }\r
+\r
+                       req->length = xfer;\r
+                       ret = usb_ep_queue(dev->ep_in, req, GFP_ATOMIC);\r
+                       if (ret < 0) {\r
+                               pr_debug("adb_write: xfer error %d\n", ret);\r
+                               dev->error = 1;\r
+                               r = -EIO;\r
+                               break;\r
+                       }\r
+\r
+                       buf += xfer;\r
+                       count -= xfer;\r
+\r
+                       /* zero this so we don't try to free it on error exit */\r
+                       req = 0;\r
+               }\r
+       }\r
+\r
+       if (req)\r
+               adb_req_put(dev, &dev->tx_idle, req);\r
+\r
+       adb_unlock(&dev->write_excl);\r
+       pr_debug("adb_write returning %d\n", r);\r
+       return r;\r
+}\r
+\r
+static int adb_open(struct inode *ip, struct file *fp)\r
+{\r
+       pr_info("adb_open\n");\r
+       if (!_adb_dev)\r
+               return -ENODEV;\r
+\r
+       if (adb_lock(&_adb_dev->open_excl))\r
+               return -EBUSY;\r
+\r
+       fp->private_data = _adb_dev;\r
+\r
+       /* clear the error latch */\r
+       _adb_dev->error = 0;\r
+\r
+#ifndef CONFIG_USB_CONFIGFS_UEVENT\r
+       adb_ready_callback();\r
+#endif\r
+\r
+       return 0;\r
+}\r
+\r
+static int adb_release(struct inode *ip, struct file *fp)\r
+{\r
+       pr_info("adb_release\n");\r
+\r
+#ifndef CONFIG_USB_CONFIGFS_UEVENT\r
+       adb_closed_callback();\r
+#endif\r
+\r
+       adb_unlock(&_adb_dev->open_excl);\r
+       return 0;\r
+}\r
+\r
+/* file operations for ADB device /dev/android_adb */\r
+static const struct file_operations adb_fops = {\r
+       .owner = THIS_MODULE,\r
+       .read = adb_read,\r
+       .write = adb_write,\r
+       .open = adb_open,\r
+       .release = adb_release,\r
+};\r
+\r
+static struct miscdevice adb_device = {\r
+       .minor = MISC_DYNAMIC_MINOR,\r
+       .name = adb_shortname,\r
+       .fops = &adb_fops,\r
+};\r
+\r
+\r
+\r
+\r
+static int\r
+adb_function_bind(struct usb_configuration *c, struct usb_function *f)\r
+{\r
+       struct usb_composite_dev *cdev = c->cdev;\r
+       struct adb_dev  *dev = func_to_adb(f);\r
+       int                     id;\r
+       int                     ret;\r
+\r
+       dev->cdev = cdev;\r
+       DBG(cdev, "adb_function_bind dev: %p\n", dev);\r
+\r
+       /* allocate interface ID(s) */\r
+       id = usb_interface_id(c, f);\r
+       if (id < 0)\r
+               return id;\r
+       adb_interface_desc.bInterfaceNumber = id;\r
+\r
+       /* allocate endpoints */\r
+       ret = adb_create_bulk_endpoints(dev, &adb_fullspeed_in_desc,\r
+                       &adb_fullspeed_out_desc);\r
+       if (ret)\r
+               return ret;\r
+\r
+       /* support high speed hardware */\r
+       if (gadget_is_dualspeed(c->cdev->gadget)) {\r
+               adb_highspeed_in_desc.bEndpointAddress =\r
+                       adb_fullspeed_in_desc.bEndpointAddress;\r
+               adb_highspeed_out_desc.bEndpointAddress =\r
+                       adb_fullspeed_out_desc.bEndpointAddress;\r
+       }\r
+\r
+       /* support super speed hardware */\r
+       if (gadget_is_superspeed(c->cdev->gadget)) {\r
+               adb_superspeed_in_desc.bEndpointAddress =\r
+                       adb_fullspeed_in_desc.bEndpointAddress;\r
+               adb_superspeed_out_desc.bEndpointAddress =\r
+                       adb_fullspeed_out_desc.bEndpointAddress;\r
+       }\r
+\r
+       DBG(cdev, "%s speed %s: IN/%s, OUT/%s\n",\r
+                       gadget_is_superspeed(c->cdev->gadget) ? "super" :\r
+                       gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",\r
+                       f->name, dev->ep_in->name, dev->ep_out->name);\r
+       return 0;\r
+}\r
+\r
+static void\r
+adb_function_unbind(struct usb_configuration *c, struct usb_function *f)\r
+{\r
+       struct adb_dev  *dev = func_to_adb(f);\r
+       struct usb_request *req;\r
+\r
+\r
+       dev->online = 0;\r
+       dev->error = 1;\r
+\r
+       wake_up(&dev->read_wq);\r
+\r
+       adb_request_free(dev->rx_req, dev->ep_out);\r
+       while ((req = adb_req_get(dev, &dev->tx_idle)))\r
+               adb_request_free(req, dev->ep_in);\r
+}\r
+\r
+static int adb_function_set_alt(struct usb_function *f,\r
+               unsigned intf, unsigned alt)\r
+{\r
+       struct adb_dev  *dev = func_to_adb(f);\r
+       struct usb_composite_dev *cdev = f->config->cdev;\r
+       int ret;\r
+\r
+       DBG(cdev, "adb_function_set_alt intf: %d alt: %d\n", intf, alt);\r
+\r
+       ret = config_ep_by_speed(cdev->gadget, f, dev->ep_in);\r
+       if (ret)\r
+               return ret;\r
+\r
+       ret = usb_ep_enable(dev->ep_in);\r
+       if (ret)\r
+               return ret;\r
+\r
+       ret = config_ep_by_speed(cdev->gadget, f, dev->ep_out);\r
+       if (ret)\r
+               return ret;\r
+\r
+       ret = usb_ep_enable(dev->ep_out);\r
+       if (ret) {\r
+               usb_ep_disable(dev->ep_in);\r
+               return ret;\r
+       }\r
+       dev->online = 1;\r
+\r
+       /* readers may be blocked waiting for us to go online */\r
+       wake_up(&dev->read_wq);\r
+       return 0;\r
+}\r
+\r
+static void adb_function_disable(struct usb_function *f)\r
+{\r
+       struct adb_dev  *dev = func_to_adb(f);\r
+       struct usb_composite_dev        *cdev = dev->cdev;\r
+\r
+       DBG(cdev, "adb_function_disable cdev %p\n", cdev);\r
+       dev->online = 0;\r
+       dev->error = 1;\r
+       usb_ep_disable(dev->ep_in);\r
+       usb_ep_disable(dev->ep_out);\r
+\r
+       /* readers may be blocked waiting for us to go online */\r
+       wake_up(&dev->read_wq);\r
+\r
+       VDBG(cdev, "%s disabled\n", dev->function.name);\r
+}\r
+\r
+#ifndef CONFIG_USB_CONFIGFS_UEVENT\r
+static int adb_bind_config(struct usb_configuration *c)\r
+{\r
+       struct adb_dev *dev = _adb_dev;\r
+\r
+       printk(KERN_INFO "adb_bind_config\n");\r
+\r
+       dev->cdev = c->cdev;\r
+       dev->function.name = "adb";\r
+       dev->function.fs_descriptors = fs_adb_descs;\r
+       dev->function.hs_descriptors = hs_adb_descs;\r
+       dev->function.ss_descriptors = ss_adb_descs;\r
+       dev->function.bind = adb_function_bind;\r
+       dev->function.unbind = adb_function_unbind;\r
+       dev->function.set_alt = adb_function_set_alt;\r
+       dev->function.disable = adb_function_disable;\r
+\r
+       return usb_add_function(c, &dev->function);\r
+}\r
+#endif\r
+\r
+static int __adb_setup(struct adb_instance *fi_adb)\r
+{\r
+       struct adb_dev *dev;\r
+       int ret;\r
+\r
+       dev = kzalloc(sizeof(*dev), GFP_KERNEL);\r
+       if (!dev)\r
+               return -ENOMEM;\r
+\r
+       if (fi_adb != NULL)\r
+               fi_adb->dev = dev;\r
+\r
+       spin_lock_init(&dev->lock);\r
+\r
+       init_waitqueue_head(&dev->read_wq);\r
+       init_waitqueue_head(&dev->write_wq);\r
+\r
+       atomic_set(&dev->open_excl, 0);\r
+       atomic_set(&dev->read_excl, 0);\r
+       atomic_set(&dev->write_excl, 0);\r
+\r
+       INIT_LIST_HEAD(&dev->tx_idle);\r
+\r
+       _adb_dev = dev;\r
+\r
+       ret = misc_register(&adb_device);\r
+       if (ret)\r
+               goto err;\r
+\r
+       return 0;\r
+\r
+err:\r
+       kfree(dev);\r
+       printk(KERN_ERR "adb gadget driver failed to initialize\n");\r
+       return ret;\r
+}\r
+\r
+static int adb_setup_configfs(struct adb_instance *fi_adb)\r
+{\r
+       return __adb_setup(fi_adb);\r
+}\r
+\r
+#ifndef CONFIG_USB_CONFIGFS_UEVENT\r
+static int adb_setup(void)\r
+{\r
+       return __adb_setup(NULL);\r
+\r
+}\r
+#endif\r
+\r
+static void adb_cleanup(void)\r
+{\r
+       misc_deregister(&adb_device);\r
+\r
+       kfree(_adb_dev);\r
+       _adb_dev = NULL;\r
+}\r
+\r
+static struct adb_instance *to_adb_instance(struct config_item *item)\r
+{\r
+       return container_of(to_config_group(item), struct adb_instance,\r
+                                                       func_inst.group);\r
+}\r
+\r
+static void adb_attr_release(struct config_item *item)\r
+{\r
+       struct adb_instance *fi_adb = to_adb_instance(item);\r
+       usb_put_function_instance(&fi_adb->func_inst);\r
+}\r
+\r
+static struct configfs_item_operations adb_item_ops = {\r
+       .release        = adb_attr_release,\r
+};\r
+\r
+static struct config_item_type adb_func_type = {\r
+       .ct_item_ops    = &adb_item_ops,\r
+       .ct_owner       = THIS_MODULE,\r
+};\r
+\r
+static struct adb_instance *to_fi_adb(struct usb_function_instance *fi)\r
+{\r
+       return container_of(fi, struct adb_instance, func_inst);\r
+}\r
+\r
+static int adb_set_inst_name(struct usb_function_instance *fi,\r
+                                               const char *name)\r
+{\r
+       struct adb_instance *fi_adb;\r
+       char *ptr;\r
+       int name_len;\r
+\r
+       name_len = strlen(name) + 1;\r
+       if (name_len > MAX_INST_NAME_LEN)\r
+               return -ENAMETOOLONG;\r
+\r
+       ptr = kstrndup(name, name_len, GFP_KERNEL);\r
+       if (!ptr)\r
+               return -ENOMEM;\r
+\r
+       fi_adb = to_fi_adb(fi);\r
+       fi_adb->name = ptr;\r
+\r
+       return 0;\r
+}\r
+\r
+static void adb_free_inst(struct usb_function_instance *fi)\r
+{\r
+       struct adb_instance *fi_adb;\r
+\r
+       fi_adb = to_fi_adb(fi);\r
+       kfree(fi_adb->name);\r
+       adb_cleanup();\r
+       kfree(fi_adb);\r
+}\r
+\r
+struct usb_function_instance *alloc_inst_adb(bool adb_config)\r
+{\r
+       struct adb_instance *fi_adb;\r
+       int ret = 0;\r
+\r
+       fi_adb = kzalloc(sizeof(*fi_adb), GFP_KERNEL);\r
+       if (!fi_adb)\r
+               return ERR_PTR(-ENOMEM);\r
+       fi_adb->func_inst.set_inst_name = adb_set_inst_name;\r
+       fi_adb->func_inst.free_func_inst = adb_free_inst;\r
+\r
+       if (adb_config) {\r
+               ret = adb_setup_configfs(fi_adb);\r
+               if (ret) {\r
+                       kfree(fi_adb);\r
+                       pr_err("Error setting adb\n");\r
+                       return ERR_PTR(ret);\r
+               }\r
+       } else\r
+               fi_adb->dev = _adb_dev;\r
+\r
+       config_group_init_type_name(&fi_adb->func_inst.group, "",\r
+                                               &adb_func_type);\r
+\r
+       return  &fi_adb->func_inst;\r
+}\r
+EXPORT_SYMBOL_GPL(alloc_inst_adb);\r
+\r
+static struct usb_function_instance *adb_alloc_inst(void)\r
+{\r
+       return alloc_inst_adb(true);\r
+}\r
+\r
+static void adb_free(struct usb_function *f)\r
+{\r
+       /*NO-OP: no function specific resource allocation in adb_alloc*/\r
+}\r
+\r
+struct usb_function *function_alloc_adb(struct usb_function_instance *fi,\r
+                                       bool adb_config)\r
+{\r
+       struct adb_instance *fi_adb = to_fi_adb(fi);\r
+       struct adb_dev *dev = fi_adb->dev;\r
+\r
+       dev->function.name = "adb";\r
+\r
+       dev->function.fs_descriptors = fs_adb_descs;\r
+       dev->function.hs_descriptors = hs_adb_descs;\r
+       dev->function.ss_descriptors = ss_adb_descs;\r
+\r
+       dev->function.bind = adb_function_bind;\r
+       dev->function.unbind = adb_function_unbind;\r
+       dev->function.set_alt = adb_function_set_alt;\r
+       dev->function.disable = adb_function_disable;\r
+       dev->function.free_func = adb_free;\r
+\r
+       return &dev->function;\r
+}\r
+EXPORT_SYMBOL_GPL(function_alloc_adb);\r
+\r
+static struct usb_function *adb_alloc(struct usb_function_instance *fi)\r
+{\r
+       return function_alloc_adb(fi, true);\r
+}\r
+\r
+DECLARE_USB_FUNCTION_INIT(adb, adb_alloc_inst, adb_alloc);\r
+MODULE_LICENSE("GPL");\r
diff --git a/drivers/usb/gadget/function/f_dm.c b/drivers/usb/gadget/function/f_dm.c
new file mode 100644 (file)
index 0000000..36537db
--- /dev/null
@@ -0,0 +1,501 @@
+/*\r
+ * f_dm.c - generic USB serial function driver\r
+ * modified from f_serial.c and f_diag.c\r
+ * ttyGS*\r
+ *\r
+ * Copyright (C) 2003 Al Borchers (alborchers@steinerpoint.com)\r
+ * Copyright (C) 2008 by David Brownell\r
+ * Copyright (C) 2008 by Nokia Corporation\r
+ *\r
+ * This software is distributed under the terms of the GNU General\r
+ * Public License ("GPL") as published by the Free Software Foundation,\r
+ * either version 2 of that License or (at your option) any later version.\r
+ */\r
+\r
+#include <linux/module.h>\r
+#include <linux/init.h>\r
+#include <linux/kernel.h>\r
+#include <linux/device.h>\r
+\r
+#include <linux/configfs.h>\r
+#include <linux/usb/composite.h>\r
+\r
+#include "../configfs.h"\r
+#include "u_serial.h"\r
+\r
+#define MAX_INST_NAME_LEN              40\r
+/*  DM_PORT NUM : /dev/ttyGS* port number */\r
+#define DM_PORT_NUM                    1\r
+/*\r
+ * This function packages a simple "generic serial" port with no real\r
+ * control mechanisms, just raw data transfer over two bulk endpoints.\r
+ *\r
+ * Because it's not standardized, this isn't as interoperable as the\r
+ * CDC ACM driver.  However, for many purposes it's just as functional\r
+ * if you can arrange appropriate host side drivers.\r
+ */\r
+\r
+struct dm_descs {\r
+       struct usb_endpoint_descriptor  *in;\r
+       struct usb_endpoint_descriptor  *out;\r
+};\r
+\r
+struct f_dm {\r
+       struct gserial                  port;\r
+       u8                              data_id;\r
+       u8                              port_num;\r
+\r
+       struct dm_descs                 fs;\r
+       struct dm_descs                 hs;\r
+};\r
+\r
+static inline struct f_dm *func_to_dm(struct usb_function *f)\r
+{\r
+       return container_of(f, struct f_dm, port.func);\r
+}\r
+\r
+/*-------------------------------------------------------------------------*/\r
+\r
+/* interface descriptor: */\r
+static struct usb_interface_descriptor dm_interface_desc  = {\r
+       .bLength =              USB_DT_INTERFACE_SIZE,\r
+       .bDescriptorType =      USB_DT_INTERFACE,\r
+       /* .bInterfaceNumber = DYNAMIC */\r
+       .bNumEndpoints =        2,\r
+       .bInterfaceClass =      USB_CLASS_VENDOR_SPEC,\r
+       .bInterfaceSubClass =   0x10,\r
+       .bInterfaceProtocol =   0x01,\r
+       /* .iInterface = DYNAMIC */\r
+};\r
+\r
+/* full speed support: */\r
+\r
+static struct usb_endpoint_descriptor dm_fs_in_desc  = {\r
+       .bLength =              USB_DT_ENDPOINT_SIZE,\r
+       .bDescriptorType =      USB_DT_ENDPOINT,\r
+       .bEndpointAddress =     USB_DIR_IN,\r
+       .bmAttributes =         USB_ENDPOINT_XFER_BULK,\r
+};\r
+\r
+static struct usb_endpoint_descriptor dm_fs_out_desc  = {\r
+       .bLength =              USB_DT_ENDPOINT_SIZE,\r
+       .bDescriptorType =      USB_DT_ENDPOINT,\r
+       .bEndpointAddress =     USB_DIR_OUT,\r
+       .bmAttributes =         USB_ENDPOINT_XFER_BULK,\r
+};\r
+\r
+static struct usb_descriptor_header *dm_fs_function[]  = {\r
+       (struct usb_descriptor_header *) &dm_interface_desc,\r
+       (struct usb_descriptor_header *) &dm_fs_in_desc,\r
+       (struct usb_descriptor_header *) &dm_fs_out_desc,\r
+       NULL,\r
+};\r
+\r
+/* high speed support: */\r
+\r
+static struct usb_endpoint_descriptor dm_hs_in_desc  = {\r
+       .bLength =              USB_DT_ENDPOINT_SIZE,\r
+       .bDescriptorType =      USB_DT_ENDPOINT,\r
+       .bmAttributes =         USB_ENDPOINT_XFER_BULK,\r
+       .wMaxPacketSize =       __constant_cpu_to_le16(512),\r
+};\r
+\r
+static struct usb_endpoint_descriptor dm_hs_out_desc  = {\r
+       .bLength =              USB_DT_ENDPOINT_SIZE,\r
+       .bDescriptorType =      USB_DT_ENDPOINT,\r
+       .bmAttributes =         USB_ENDPOINT_XFER_BULK,\r
+       .wMaxPacketSize =       __constant_cpu_to_le16(512),\r
+};\r
+static struct usb_descriptor_header *dm_hs_function[]  = {\r
+       (struct usb_descriptor_header *) &dm_interface_desc,\r
+       (struct usb_descriptor_header *) &dm_hs_in_desc,\r
+       (struct usb_descriptor_header *) &dm_hs_out_desc,\r
+       NULL,\r
+};\r
+\r
+static struct usb_endpoint_descriptor dm_ss_in_desc = {\r
+       .bLength =              USB_DT_ENDPOINT_SIZE,\r
+       .bDescriptorType =      USB_DT_ENDPOINT,\r
+       .bmAttributes =         USB_ENDPOINT_XFER_BULK,\r
+       .wMaxPacketSize =       cpu_to_le16(1024),\r
+};\r
+\r
+static struct usb_endpoint_descriptor dm_ss_out_desc = {\r
+       .bLength =              USB_DT_ENDPOINT_SIZE,\r
+       .bDescriptorType =      USB_DT_ENDPOINT,\r
+       .bmAttributes =         USB_ENDPOINT_XFER_BULK,\r
+       .wMaxPacketSize =       cpu_to_le16(1024),\r
+};\r
+\r
+static struct usb_ss_ep_comp_descriptor dm_ss_bulk_comp_desc = {\r
+       .bLength =              sizeof dm_ss_bulk_comp_desc,\r
+       .bDescriptorType =      USB_DT_SS_ENDPOINT_COMP,\r
+};\r
+\r
+static struct usb_descriptor_header *dm_ss_function[] = {\r
+       (struct usb_descriptor_header *) &dm_interface_desc,\r
+       (struct usb_descriptor_header *) &dm_ss_in_desc,\r
+       (struct usb_descriptor_header *) &dm_ss_bulk_comp_desc,\r
+       (struct usb_descriptor_header *) &dm_ss_out_desc,\r
+       (struct usb_descriptor_header *) &dm_ss_bulk_comp_desc,\r
+       NULL,\r
+};\r
+\r
+\r
+/* string descriptors: */\r
+#define F_DM_IDX       0\r
+\r
+static struct usb_string dm_string_defs[] = {\r
+       [F_DM_IDX].s = "Samsung Android DM",\r
+       {  /* ZEROES END LIST */ },\r
+};\r
+\r
+static struct usb_gadget_strings dm_string_table = {\r
+       .language =             0x0409, /* en-us */\r
+       .strings =              dm_string_defs,\r
+};\r
+\r
+static struct usb_gadget_strings *dm_strings[] = {\r
+       &dm_string_table,\r
+       NULL,\r
+};\r
+\r
+struct dm_instance {\r
+       struct usb_function_instance func_inst;\r
+       const char *name;\r
+       struct f_dm *dm;\r
+};\r
+/*-------------------------------------------------------------------------*/\r
+\r
+static int dm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)\r
+{\r
+       struct f_dm             *dm = func_to_dm(f);\r
+       struct usb_composite_dev *cdev = f->config->cdev;\r
+       int status;\r
+\r
+       /* we know alt == 0, so this is an activation or a reset */\r
+\r
+       if (dm->port.in->driver_data) {\r
+               DBG(cdev, "reset generic ttyGS%d\n", dm->port_num);\r
+               gserial_disconnect(&dm->port);\r
+       } else {\r
+               DBG(cdev, "activate generic ttyGS%d\n", dm->port_num);\r
+       }\r
+       if (!dm->port.in->desc || !dm->port.out->desc) {\r
+                       DBG(cdev, "activate dm ttyGS%d\n", dm->port_num);\r
+                       if (config_ep_by_speed(cdev->gadget, f,\r
+                                              dm->port.in) ||\r
+                           config_ep_by_speed(cdev->gadget, f,\r
+                                              dm->port.out)) {\r
+                               dm->port.in->desc = NULL;\r
+                               dm->port.out->desc = NULL;\r
+                               return -EINVAL;\r
+                       }\r
+       }\r
+\r
+\r
+       status = gserial_connect(&dm->port, dm->port_num);\r
+       printk(KERN_DEBUG "usb: %s run generic_connect(%d)", __func__,\r
+                       dm->port_num);\r
+\r
+       if (status < 0) {\r
+               printk(KERN_DEBUG "fail to activate generic ttyGS%d\n",\r
+                               dm->port_num);\r
+\r
+               return status;\r
+       }\r
+\r
+       return 0;\r
+}\r
+\r
+static void dm_disable(struct usb_function *f)\r
+{\r
+       struct f_dm     *dm = func_to_dm(f);\r
+\r
+       printk(KERN_DEBUG "usb: %s generic ttyGS%d deactivated\n", __func__,\r
+                       dm->port_num);\r
+       gserial_disconnect(&dm->port);\r
+}\r
+\r
+/*-------------------------------------------------------------------------*/\r
+\r
+/* serial function driver setup/binding */\r
+\r
+static int\r
+dm_bind(struct usb_configuration *c, struct usb_function *f)\r
+{\r
+       struct usb_composite_dev *cdev = c->cdev;\r
+       struct f_dm             *dm = func_to_dm(f);\r
+       int                     status;\r
+       struct usb_ep           *ep;\r
+\r
+       /* maybe allocate device-global string ID */\r
+       if (dm_string_defs[F_DM_IDX].id == 0) {\r
+               status = usb_string_id(c->cdev);\r
+               if (status < 0)\r
+                       return status;\r
+               dm_string_defs[F_DM_IDX].id = status;\r
+       }\r
+       /* allocate instance-specific interface IDs */\r
+       status = usb_interface_id(c, f);\r
+       if (status < 0)\r
+               goto fail;\r
+       dm->data_id = status;\r
+       dm_interface_desc.bInterfaceNumber = status;\r
+\r
+       status = -ENODEV;\r
+\r
+       /* allocate instance-specific endpoints */\r
+       ep = usb_ep_autoconfig(cdev->gadget, &dm_fs_in_desc);\r
+       if (!ep)\r
+               goto fail;\r
+       dm->port.in = ep;\r
+       ep->driver_data = cdev; /* claim */\r
+\r
+       ep = usb_ep_autoconfig(cdev->gadget, &dm_fs_out_desc);\r
+       if (!ep)\r
+               goto fail;\r
+       dm->port.out = ep;\r
+       ep->driver_data = cdev; /* claim */\r
+       printk(KERN_INFO "[%s]   in =0x%p , out =0x%p\n", __func__,\r
+                               dm->port.in, dm->port.out);\r
+\r
+       /* copy descriptors, and track endpoint copies */\r
+       f->fs_descriptors = usb_copy_descriptors(dm_fs_function);\r
+\r
+\r
+\r
+\r
+       /* support all relevant hardware speeds... we expect that when\r
+        * hardware is dual speed, all bulk-capable endpoints work at\r
+        * both speeds\r
+        */\r
+       if (gadget_is_dualspeed(c->cdev->gadget)) {\r
+               dm_hs_in_desc.bEndpointAddress =\r
+                               dm_fs_in_desc.bEndpointAddress;\r
+               dm_hs_out_desc.bEndpointAddress =\r
+                               dm_fs_out_desc.bEndpointAddress;\r
+\r
+               /* copy descriptors, and track endpoint copies */\r
+               f->hs_descriptors = usb_copy_descriptors(dm_hs_function);\r
+       }\r
+       if (gadget_is_superspeed(c->cdev->gadget)) {\r
+               dm_ss_in_desc.bEndpointAddress =\r
+                       dm_fs_in_desc.bEndpointAddress;\r
+               dm_ss_out_desc.bEndpointAddress =\r
+                       dm_fs_out_desc.bEndpointAddress;\r
+\r
+               /* copy descriptors, and track endpoint copies */\r
+               f->ss_descriptors = usb_copy_descriptors(dm_ss_function);\r
+               if (!f->ss_descriptors)\r
+                       goto fail;\r
+       }\r
+\r
+       printk("usb: [%s] generic ttyGS%d: %s speed IN/%s OUT/%s\n",\r
+                       __func__,\r
+                       dm->port_num,\r
+                       gadget_is_superspeed(c->cdev->gadget) ? "super" :\r
+                       gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",\r
+                       dm->port.in->name, dm->port.out->name);\r
+       return 0;\r
+\r
+fail:\r
+       /* we might as well release our claims on endpoints */\r
+       if (dm->port.out)\r
+               dm->port.out->driver_data = NULL;\r
+       if (dm->port.in)\r
+               dm->port.in->driver_data = NULL;\r
+\r
+       printk(KERN_ERR "%s: can't bind, err %d\n", f->name, status);\r
+\r
+       return status;\r
+}\r
+\r
+static void\r
+dm_unbind(struct usb_configuration *c, struct usb_function *f)\r
+{\r
+       if (gadget_is_dualspeed(c->cdev->gadget))\r
+               usb_free_descriptors(f->hs_descriptors);\r
+       usb_free_descriptors(f->fs_descriptors);\r
+       printk(KERN_DEBUG "usb: %s\n", __func__);\r
+}\r
+\r
+/*\r
+ * dm_bind_config - add a generic serial function to a configuration\r
+ * @c: the configuration to support the serial instance\r
+ * @port_num: /dev/ttyGS* port this interface will use\r
+ * Context: single threaded during gadget setup\r
+ *\r
+ * Returns zero on success, else negative errno.\r
+ *\r
+ * Caller must have called @gserial_setup() with enough ports to\r
+ * handle all the ones it binds.  Caller is also responsible\r
+ * for calling @gserial_cleanup() before module unload.\r
+ */\r
+int dm_bind_config(struct usb_configuration *c, u8 port_num)\r
+{\r
+       struct f_dm     *dm;\r
+       int             status;\r
+\r
+       /* REVISIT might want instance-specific strings to help\r
+        * distinguish instances ...\r
+        */\r
+\r
+       /* maybe allocate device-global string ID */\r
+       if (dm_string_defs[F_DM_IDX].id == 0) {\r
+               status = usb_string_id(c->cdev);\r
+               if (status < 0)\r
+                       return status;\r
+               dm_string_defs[F_DM_IDX].id = status;\r
+       }\r
+\r
+       /* allocate and initialize one new instance */\r
+       dm = kzalloc(sizeof *dm, GFP_KERNEL);\r
+       if (!dm)\r
+               return -ENOMEM;\r
+\r
+       dm->port_num = DM_PORT_NUM;\r
+\r
+       dm->port.func.name = "dm";\r
+       dm->port.func.strings = dm_strings;\r
+       dm->port.func.bind = dm_bind;\r
+       dm->port.func.unbind = dm_unbind;\r
+       dm->port.func.set_alt = dm_set_alt;\r
+       dm->port.func.disable = dm_disable;\r
+\r
+       status = usb_add_function(c, &dm->port.func);\r
+       if (status)\r
+               kfree(dm);\r
+       return status;\r
+}\r
+static struct dm_instance *to_dm_instance(struct config_item *item)\r
+{\r
+       return container_of(to_config_group(item), struct dm_instance,\r
+               func_inst.group);\r
+}\r
+static void dm_attr_release(struct config_item *item)\r
+{\r
+       struct dm_instance *fi_dm = to_dm_instance(item);\r
+       usb_put_function_instance(&fi_dm->func_inst);\r
+}\r
+\r
+static struct configfs_item_operations dm_item_ops = {\r
+       .release        = dm_attr_release,\r
+};\r
+\r
+static struct config_item_type dm_func_type = {\r
+       .ct_item_ops    = &dm_item_ops,\r
+       .ct_owner       = THIS_MODULE,\r
+};\r
+\r
+\r
+static struct dm_instance *to_fi_dm(struct usb_function_instance *fi)\r
+{\r
+       return container_of(fi, struct dm_instance, func_inst);\r
+}\r
+\r
+static int dm_set_inst_name(struct usb_function_instance *fi, const char *name)\r
+{\r
+       struct dm_instance *fi_dm;\r
+       char *ptr;\r
+       int name_len;\r
+\r
+       name_len = strlen(name) + 1;\r
+       if (name_len > MAX_INST_NAME_LEN)\r
+               return -ENAMETOOLONG;\r
+\r
+       ptr = kstrndup(name, name_len, GFP_KERNEL);\r
+       if (!ptr)\r
+               return -ENOMEM;\r
+\r
+       fi_dm = to_fi_dm(fi);\r
+       fi_dm->name = ptr;\r
+\r
+       return 0;\r
+}\r
+\r
+static void dm_free_inst(struct usb_function_instance *fi)\r
+{\r
+       struct dm_instance *fi_dm;\r
+\r
+       fi_dm = to_fi_dm(fi);\r
+       kfree(fi_dm->name);\r
+       kfree(fi_dm);\r
+}\r
+\r
+struct usb_function_instance *alloc_inst_dm(bool dm_config)\r
+{\r
+       struct dm_instance *fi_dm;\r
+\r
+       fi_dm = kzalloc(sizeof(*fi_dm), GFP_KERNEL);\r
+       if (!fi_dm)\r
+               return ERR_PTR(-ENOMEM);\r
+       fi_dm->func_inst.set_inst_name = dm_set_inst_name;\r
+       fi_dm->func_inst.free_func_inst = dm_free_inst;\r
+\r
+\r
+       config_group_init_type_name(&fi_dm->func_inst.group,\r
+                                       "", &dm_func_type);\r
+\r
+       return  &fi_dm->func_inst;\r
+}\r
+EXPORT_SYMBOL_GPL(alloc_inst_dm);\r
+\r
+static struct usb_function_instance *dm_alloc_inst(void)\r
+{\r
+               return alloc_inst_dm(true);\r
+}\r
+\r
+static void dm_free(struct usb_function *f)\r
+{\r
+       struct f_dm     *dm = func_to_dm(f);\r
+\r
+       kfree(dm);\r
+}\r
+\r
+struct usb_function *function_alloc_dm(struct usb_function_instance *fi, bool dm_config)\r
+{\r
+\r
+       struct dm_instance *fi_dm = to_fi_dm(fi);\r
+       struct f_dm     *dm;\r
+       int             ret;\r
+\r
+       /* REVISIT might want instance-specific strings to help\r
+        * distinguish instances ...\r
+        */\r
+\r
+       /* allocate and initialize one new instance */\r
+       dm = kzalloc(sizeof *dm, GFP_KERNEL);\r
+       if (!dm)\r
+               return ERR_PTR(-ENOMEM);\r
+\r
+\r
+       dm->port_num = DM_PORT_NUM;\r
+\r
+       dm->port.func.name = "dm";\r
+       dm->port.func.strings = dm_strings;\r
+       dm->port.func.bind = dm_bind;\r
+       dm->port.func.unbind = dm_unbind;\r
+       dm->port.func.set_alt = dm_set_alt;\r
+       dm->port.func.disable = dm_disable;\r
+       dm->port.func.free_func = dm_free;\r
+\r
+       fi_dm->dm = dm;\r
+\r
+       ret = gserial_alloc_line(&dm->port_num);\r
+       if (ret) {\r
+               kfree(dm);\r
+               return ERR_PTR(ret);\r
+       }\r
+\r
+       return &dm->port.func;\r
+}\r
+EXPORT_SYMBOL_GPL(function_alloc_dm);\r
+\r
+static struct usb_function *dm_alloc(struct usb_function_instance *fi)\r
+{\r
+       return function_alloc_dm(fi, true);\r
+}\r
+\r
+DECLARE_USB_FUNCTION_INIT(dm, dm_alloc_inst, dm_alloc);\r
+MODULE_LICENSE("GPL");\r
diff --git a/drivers/usb/gadget/function/f_mtp.c b/drivers/usb/gadget/function/f_mtp.c
new file mode 100644 (file)
index 0000000..9b8b6f4
--- /dev/null
@@ -0,0 +1,1605 @@
+/*\r
+ * Gadget Function Driver for MTP\r
+ *\r
+ * Copyright (C) 2010 Google, Inc.\r
+ * Author: Mike Lockwood <lockwood@android.com>\r
+ *\r
+ * This software is licensed under the terms of the GNU General Public\r
+ * License version 2, as published by the Free Software Foundation, and\r
+ * may be copied, distributed, and modified under those terms.\r
+ *\r
+ * This program is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+ * GNU General Public License for more details.\r
+ *\r
+ */\r
+\r
+/* #define DEBUG */\r
+/* #define VERBOSE_DEBUG */\r
+\r
+#include <linux/module.h>\r
+#include <linux/init.h>\r
+#include <linux/poll.h>\r
+#include <linux/delay.h>\r
+#include <linux/wait.h>\r
+#include <linux/err.h>\r
+#include <linux/interrupt.h>\r
+\r
+#include <linux/types.h>\r
+#include <linux/file.h>\r
+#include <linux/device.h>\r
+#include <linux/miscdevice.h>\r
+\r
+#include <linux/usb.h>\r
+#include <linux/usb_usual.h>\r
+#include <linux/usb/ch9.h>\r
+#include <linux/usb/f_mtp.h>\r
+#include <linux/configfs.h>\r
+#include <linux/usb/composite.h>\r
+\r
+#include "configfs.h"\r
+\r
+#define MTP_BULK_BUFFER_SIZE       16384\r
+#define INTR_BUFFER_SIZE           28\r
+#define MAX_INST_NAME_LEN          40\r
+#define MTP_MAX_FILE_SIZE          0xFFFFFFFFL\r
+\r
+/* String IDs */\r
+#define INTERFACE_STRING_INDEX 0\r
+\r
+/* values for mtp_dev.state */\r
+#define STATE_OFFLINE               0   /* initial state, disconnected */\r
+#define STATE_READY                 1   /* ready for userspace calls */\r
+#define STATE_BUSY                  2   /* processing userspace calls */\r
+#define STATE_CANCELED              3   /* transaction canceled by host */\r
+#define STATE_ERROR                 4   /* error from completion routine */\r
+\r
+/* number of tx and rx requests to allocate */\r
+#define TX_REQ_MAX 4\r
+#define RX_REQ_MAX 2\r
+#define INTR_REQ_MAX 5\r
+\r
+/* ID for Microsoft MTP OS String */\r
+#define MTP_OS_STRING_ID   0xEE\r
+\r
+/* MTP class reqeusts */\r
+#define MTP_REQ_CANCEL              0x64\r
+#define MTP_REQ_GET_EXT_EVENT_DATA  0x65\r
+#define MTP_REQ_RESET               0x66\r
+#define MTP_REQ_GET_DEVICE_STATUS   0x67\r
+\r
+/* constants for device status */\r
+#define MTP_RESPONSE_OK             0x2001\r
+#define MTP_RESPONSE_DEVICE_BUSY    0x2019\r
+#define DRIVER_NAME "mtp"\r
+#if IS_ENABLED(CONFIG_USB_CONFIGFS_UEVENT)\r
+#define DRIVER_NAME_PTP "ptp"\r
+#endif\r
+\r
+static const char mtp_shortname[] = DRIVER_NAME "_usb";\r
+\r
+struct mtp_dev {\r
+       struct usb_function function;\r
+#if IS_ENABLED(CONFIG_USB_CONFIGFS_UEVENT)\r
+       struct usb_function function_ptp;\r
+#endif\r
+       struct usb_composite_dev *cdev;\r
+       spinlock_t lock;\r
+\r
+       struct usb_ep *ep_in;\r
+       struct usb_ep *ep_out;\r
+       struct usb_ep *ep_intr;\r
+\r
+       int state;\r
+\r
+       /* synchronize access to our device file */\r
+       atomic_t open_excl;\r
+       /* to enforce only one ioctl at a time */\r
+       atomic_t ioctl_excl;\r
+\r
+       struct list_head tx_idle;\r
+       struct list_head intr_idle;\r
+\r
+       wait_queue_head_t read_wq;\r
+       wait_queue_head_t write_wq;\r
+       wait_queue_head_t intr_wq;\r
+       struct usb_request *rx_req[RX_REQ_MAX];\r
+       int rx_done;\r
+\r
+       /* for processing MTP_SEND_FILE, MTP_RECEIVE_FILE and\r
+        * MTP_SEND_FILE_WITH_HEADER ioctls on a work queue\r
+        */\r
+       struct workqueue_struct *wq;\r
+       struct work_struct send_file_work;\r
+       struct work_struct receive_file_work;\r
+       struct file *xfer_file;\r
+       loff_t xfer_file_offset;\r
+       int64_t xfer_file_length;\r
+       unsigned xfer_send_header;\r
+       uint16_t xfer_command;\r
+       uint32_t xfer_transaction_id;\r
+       int xfer_result;\r
+};\r
+\r
+static struct usb_interface_descriptor mtp_interface_desc = {\r
+       .bLength                = USB_DT_INTERFACE_SIZE,\r
+       .bDescriptorType        = USB_DT_INTERFACE,\r
+       .bInterfaceNumber       = 0,\r
+       .bNumEndpoints          = 3,\r
+       .bInterfaceClass        = USB_CLASS_VENDOR_SPEC,\r
+       .bInterfaceSubClass     = USB_SUBCLASS_VENDOR_SPEC,\r
+       .bInterfaceProtocol     = 0,\r
+};\r
+\r
+static struct usb_interface_descriptor ptp_interface_desc = {\r
+       .bLength                = USB_DT_INTERFACE_SIZE,\r
+       .bDescriptorType        = USB_DT_INTERFACE,\r
+       .bInterfaceNumber       = 0,\r
+       .bNumEndpoints          = 3,\r
+       .bInterfaceClass        = USB_CLASS_STILL_IMAGE,\r
+       .bInterfaceSubClass     = 1,\r
+       .bInterfaceProtocol     = 1,\r
+};\r
+\r
+static struct usb_endpoint_descriptor mtp_ss_in_desc = {\r
+       .bLength                = USB_DT_ENDPOINT_SIZE,\r
+       .bDescriptorType        = USB_DT_ENDPOINT,\r
+       .bEndpointAddress       = USB_DIR_IN,\r
+       .bmAttributes           = USB_ENDPOINT_XFER_BULK,\r
+       .wMaxPacketSize         = __constant_cpu_to_le16(1024),\r
+};\r
+\r
+static struct usb_ss_ep_comp_descriptor mtp_ss_in_comp_desc = {\r
+       .bLength                = sizeof(mtp_ss_in_comp_desc),\r
+       .bDescriptorType        = USB_DT_SS_ENDPOINT_COMP,\r
+       /* .bMaxBurst           = DYNAMIC, */\r
+};\r
+\r
+static struct usb_endpoint_descriptor mtp_ss_out_desc = {\r
+       .bLength                = USB_DT_ENDPOINT_SIZE,\r
+       .bDescriptorType        = USB_DT_ENDPOINT,\r
+       .bEndpointAddress       = USB_DIR_OUT,\r
+       .bmAttributes           = USB_ENDPOINT_XFER_BULK,\r
+       .wMaxPacketSize         = __constant_cpu_to_le16(1024),\r
+};\r
+\r
+static struct usb_ss_ep_comp_descriptor mtp_ss_out_comp_desc = {\r
+       .bLength                = sizeof(mtp_ss_out_comp_desc),\r
+       .bDescriptorType        = USB_DT_SS_ENDPOINT_COMP,\r
+       /* .bMaxBurst           = DYNAMIC, */\r
+};\r
+\r
+static struct usb_endpoint_descriptor mtp_highspeed_in_desc = {\r
+       .bLength                = USB_DT_ENDPOINT_SIZE,\r
+       .bDescriptorType        = USB_DT_ENDPOINT,\r
+       .bEndpointAddress       = USB_DIR_IN,\r
+       .bmAttributes           = USB_ENDPOINT_XFER_BULK,\r
+       .wMaxPacketSize         = __constant_cpu_to_le16(512),\r
+};\r
+\r
+static struct usb_endpoint_descriptor mtp_highspeed_out_desc = {\r
+       .bLength                = USB_DT_ENDPOINT_SIZE,\r
+       .bDescriptorType        = USB_DT_ENDPOINT,\r
+       .bEndpointAddress       = USB_DIR_OUT,\r
+       .bmAttributes           = USB_ENDPOINT_XFER_BULK,\r
+       .wMaxPacketSize         = __constant_cpu_to_le16(512),\r
+};\r
+\r
+static struct usb_endpoint_descriptor mtp_fullspeed_in_desc = {\r
+       .bLength                = USB_DT_ENDPOINT_SIZE,\r
+       .bDescriptorType        = USB_DT_ENDPOINT,\r
+       .bEndpointAddress       = USB_DIR_IN,\r
+       .bmAttributes           = USB_ENDPOINT_XFER_BULK,\r
+};\r
+\r
+static struct usb_endpoint_descriptor mtp_fullspeed_out_desc = {\r
+       .bLength                = USB_DT_ENDPOINT_SIZE,\r
+       .bDescriptorType        = USB_DT_ENDPOINT,\r
+       .bEndpointAddress       = USB_DIR_OUT,\r
+       .bmAttributes           = USB_ENDPOINT_XFER_BULK,\r
+};\r
+\r
+static struct usb_endpoint_descriptor mtp_intr_desc = {\r
+       .bLength                = USB_DT_ENDPOINT_SIZE,\r
+       .bDescriptorType        = USB_DT_ENDPOINT,\r
+       .bEndpointAddress       = USB_DIR_IN,\r
+       .bmAttributes           = USB_ENDPOINT_XFER_INT,\r
+       .wMaxPacketSize         = __constant_cpu_to_le16(INTR_BUFFER_SIZE),\r
+       .bInterval              = 6,\r
+};\r
+\r
+static struct usb_ss_ep_comp_descriptor mtp_intr_ss_comp_desc = {\r
+       .bLength                = sizeof(mtp_intr_ss_comp_desc),\r
+       .bDescriptorType        = USB_DT_SS_ENDPOINT_COMP,\r
+       .wBytesPerInterval      = cpu_to_le16(INTR_BUFFER_SIZE),\r
+};\r
+\r
+static struct usb_descriptor_header *fs_mtp_descs[] = {\r
+       (struct usb_descriptor_header *) &mtp_interface_desc,\r
+       (struct usb_descriptor_header *) &mtp_fullspeed_in_desc,\r
+       (struct usb_descriptor_header *) &mtp_fullspeed_out_desc,\r
+       (struct usb_descriptor_header *) &mtp_intr_desc,\r
+       NULL,\r
+};\r
+\r
+static struct usb_descriptor_header *hs_mtp_descs[] = {\r
+       (struct usb_descriptor_header *) &mtp_interface_desc,\r
+       (struct usb_descriptor_header *) &mtp_highspeed_in_desc,\r
+       (struct usb_descriptor_header *) &mtp_highspeed_out_desc,\r
+       (struct usb_descriptor_header *) &mtp_intr_desc,\r
+       NULL,\r
+};\r
+\r
+static struct usb_descriptor_header *ss_mtp_descs[] = {\r
+       (struct usb_descriptor_header *) &mtp_interface_desc,\r
+       (struct usb_descriptor_header *) &mtp_ss_in_desc,\r
+       (struct usb_descriptor_header *) &mtp_ss_in_comp_desc,\r
+       (struct usb_descriptor_header *) &mtp_ss_out_desc,\r
+       (struct usb_descriptor_header *) &mtp_ss_out_comp_desc,\r
+       (struct usb_descriptor_header *) &mtp_intr_desc,\r
+       (struct usb_descriptor_header *) &mtp_intr_ss_comp_desc,\r
+       NULL,\r
+};\r
+\r
+static struct usb_descriptor_header *fs_ptp_descs[] = {\r
+       (struct usb_descriptor_header *) &ptp_interface_desc,\r
+       (struct usb_descriptor_header *) &mtp_fullspeed_in_desc,\r
+       (struct usb_descriptor_header *) &mtp_fullspeed_out_desc,\r
+       (struct usb_descriptor_header *) &mtp_intr_desc,\r
+       NULL,\r
+};\r
+\r
+static struct usb_descriptor_header *hs_ptp_descs[] = {\r
+       (struct usb_descriptor_header *) &ptp_interface_desc,\r
+       (struct usb_descriptor_header *) &mtp_highspeed_in_desc,\r
+       (struct usb_descriptor_header *) &mtp_highspeed_out_desc,\r
+       (struct usb_descriptor_header *) &mtp_intr_desc,\r
+       NULL,\r
+};\r
+\r
+static struct usb_descriptor_header *ss_ptp_descs[] = {\r
+       (struct usb_descriptor_header *) &ptp_interface_desc,\r
+       (struct usb_descriptor_header *) &mtp_ss_in_desc,\r
+       (struct usb_descriptor_header *) &mtp_ss_in_comp_desc,\r
+       (struct usb_descriptor_header *) &mtp_ss_out_desc,\r
+       (struct usb_descriptor_header *) &mtp_ss_out_comp_desc,\r
+       (struct usb_descriptor_header *) &mtp_intr_desc,\r
+       (struct usb_descriptor_header *) &mtp_intr_ss_comp_desc,\r
+       NULL,\r
+};\r
+\r
+static struct usb_string mtp_string_defs[] = {\r
+       /* Naming interface "MTP" so libmtp will recognize us */\r
+       [INTERFACE_STRING_INDEX].s      = "MTP",\r
+       {  },   /* end of list */\r
+};\r
+\r
+static struct usb_gadget_strings mtp_string_table = {\r
+       .language               = 0x0409,       /* en-US */\r
+       .strings                = mtp_string_defs,\r
+};\r
+\r
+static struct usb_gadget_strings *mtp_strings[] = {\r
+       &mtp_string_table,\r
+       NULL,\r
+};\r
+\r
+/* Microsoft MTP OS String */\r
+static u8 mtp_os_string[] = {\r
+       18, /* sizeof(mtp_os_string) */\r
+       USB_DT_STRING,\r
+       /* Signature field: "MSFT100" */\r
+       'M', 0, 'S', 0, 'F', 0, 'T', 0, '1', 0, '0', 0, '0', 0,\r
+       /* vendor code */\r
+       1,\r
+       /* padding */\r
+       0\r
+};\r
+\r
+/* Microsoft Extended Configuration Descriptor Header Section */\r
+struct mtp_ext_config_desc_header {\r
+       __le32  dwLength;\r
+       __u16   bcdVersion;\r
+       __le16  wIndex;\r
+       __u8    bCount;\r
+       __u8    reserved[7];\r
+};\r
+\r
+/* Microsoft Extended Configuration Descriptor Function Section */\r
+struct mtp_ext_config_desc_function {\r
+       __u8    bFirstInterfaceNumber;\r
+       __u8    bInterfaceCount;\r
+       __u8    compatibleID[8];\r
+       __u8    subCompatibleID[8];\r
+       __u8    reserved[6];\r
+};\r
+\r
+/* MTP Extended Configuration Descriptor */\r
+struct {\r
+       struct mtp_ext_config_desc_header       header;\r
+       struct mtp_ext_config_desc_function    function;\r
+} mtp_ext_config_desc = {\r
+       .header = {\r
+               .dwLength = __constant_cpu_to_le32(sizeof(mtp_ext_config_desc)),\r
+               .bcdVersion = __constant_cpu_to_le16(0x0100),\r
+               .wIndex = __constant_cpu_to_le16(4),\r
+               .bCount = 1,\r
+       },\r
+       .function = {\r
+               .bFirstInterfaceNumber = 0,\r
+               .bInterfaceCount = 1,\r
+               .compatibleID = { 'M', 'T', 'P' },\r
+       },\r
+};\r
+\r
+struct mtp_device_status {\r
+       __le16  wLength;\r
+       __le16  wCode;\r
+};\r
+\r
+struct mtp_data_header {\r
+       /* length of packet, including this header */\r
+       __le32  length;\r
+       /* container type (2 for data packet) */\r
+       __le16  type;\r
+       /* MTP command code */\r
+       __le16  command;\r
+       /* MTP transaction ID */\r
+       __le32  transaction_id;\r
+};\r
+\r
+struct mtp_instance {\r
+       struct usb_function_instance func_inst;\r
+       const char *name;\r
+       struct mtp_dev *dev;\r
+       char mtp_ext_compat_id[16];\r
+       struct usb_os_desc mtp_os_desc;\r
+};\r
+\r
+/* temporary variable used between mtp_open() and mtp_gadget_bind() */\r
+static struct mtp_dev *_mtp_dev;\r
+\r
+static inline struct mtp_dev *func_to_mtp(struct usb_function *f)\r
+{\r
+#if IS_ENABLED(CONFIG_USB_CONFIGFS_UEVENT)\r
+       if (!strcmp(f->name, DRIVER_NAME_PTP))\r
+               return container_of(f, struct mtp_dev, function_ptp);\r
+#endif\r
+       return container_of(f, struct mtp_dev, function);\r
+}\r
+\r
+static struct usb_request *mtp_request_new(struct usb_ep *ep, int buffer_size)\r
+{\r
+       struct usb_request *req = usb_ep_alloc_request(ep, GFP_KERNEL);\r
+\r
+       if (!req)\r
+               return NULL;\r
+\r
+       /* now allocate buffers for the requests */\r
+       req->buf = kmalloc(buffer_size, GFP_KERNEL);\r
+       if (!req->buf) {\r
+               usb_ep_free_request(ep, req);\r
+               return NULL;\r
+       }\r
+\r
+       return req;\r
+}\r
+\r
+static void mtp_request_free(struct usb_request *req, struct usb_ep *ep)\r
+{\r
+       if (req) {\r
+               kfree(req->buf);\r
+               usb_ep_free_request(ep, req);\r
+       }\r
+}\r
+\r
+static inline int mtp_lock(atomic_t *excl)\r
+{\r
+       if (atomic_inc_return(excl) == 1) {\r
+               return 0;\r
+       } else {\r
+               atomic_dec(excl);\r
+               return -1;\r
+       }\r
+}\r
+\r
+static inline void mtp_unlock(atomic_t *excl)\r
+{\r
+       atomic_dec(excl);\r
+}\r
+\r
+/* add a request to the tail of a list */\r
+static void mtp_req_put(struct mtp_dev *dev, struct list_head *head,\r
+               struct usb_request *req)\r
+{\r
+       unsigned long flags;\r
+\r
+       spin_lock_irqsave(&dev->lock, flags);\r
+       list_add_tail(&req->list, head);\r
+       spin_unlock_irqrestore(&dev->lock, flags);\r
+}\r
+\r
+/* remove a request from the head of a list */\r
+static struct usb_request\r
+*mtp_req_get(struct mtp_dev *dev, struct list_head *head)\r
+{\r
+       unsigned long flags;\r
+       struct usb_request *req;\r
+\r
+       spin_lock_irqsave(&dev->lock, flags);\r
+       if (list_empty(head)) {\r
+               req = 0;\r
+       } else {\r
+               req = list_first_entry(head, struct usb_request, list);\r
+               list_del(&req->list);\r
+       }\r
+       spin_unlock_irqrestore(&dev->lock, flags);\r
+       return req;\r
+}\r
+\r
+/* Make bulk-out requests be divisible by the maxpacket size */\r
+static void set_read_req_length(struct usb_request *req)\r
+{\r
+       struct mtp_dev *dev = _mtp_dev;\r
+       unsigned int    rem;\r
+\r
+       rem = req->length % dev->ep_out->maxpacket;\r
+       if (rem > 0)\r
+               req->length += dev->ep_out->maxpacket - rem;\r
+}\r
+\r
+static void mtp_complete_in(struct usb_ep *ep, struct usb_request *req)\r
+{\r
+       struct mtp_dev *dev = _mtp_dev;\r
+\r
+       if (req->status != 0)\r
+               dev->state = STATE_ERROR;\r
+\r
+       mtp_req_put(dev, &dev->tx_idle, req);\r
+\r
+       wake_up(&dev->write_wq);\r
+}\r
+\r
+static void mtp_complete_out(struct usb_ep *ep, struct usb_request *req)\r
+{\r
+       struct mtp_dev *dev = _mtp_dev;\r
+\r
+       dev->rx_done = 1;\r
+       if (req->status != 0)\r
+               dev->state = STATE_ERROR;\r
+\r
+       wake_up(&dev->read_wq);\r
+}\r
+\r
+static void mtp_complete_intr(struct usb_ep *ep, struct usb_request *req)\r
+{\r
+       struct mtp_dev *dev = _mtp_dev;\r
+\r
+       if (req->status != 0)\r
+               dev->state = STATE_ERROR;\r
+\r
+       mtp_req_put(dev, &dev->intr_idle, req);\r
+\r
+       wake_up(&dev->intr_wq);\r
+}\r
+\r
+static int mtp_create_bulk_endpoints(struct mtp_dev *dev,\r
+                               struct usb_endpoint_descriptor *in_desc,\r
+                               struct usb_endpoint_descriptor *out_desc,\r
+                               struct usb_endpoint_descriptor *intr_desc)\r
+{\r
+       struct usb_composite_dev *cdev = dev->cdev;\r
+       struct usb_request *req;\r
+       struct usb_ep *ep;\r
+       int i;\r
+\r
+       DBG(cdev, "create_bulk_endpoints dev: %p\n", dev);\r
+\r
+       ep = usb_ep_autoconfig(cdev->gadget, in_desc);\r
+       if (!ep) {\r
+               DBG(cdev, "usb_ep_autoconfig for ep_in failed\n");\r
+               return -ENODEV;\r
+       }\r
+       DBG(cdev, "usb_ep_autoconfig for ep_in got %s\n", ep->name);\r
+       ep->driver_data = dev;          /* claim the endpoint */\r
+       dev->ep_in = ep;\r
+\r
+       ep = usb_ep_autoconfig(cdev->gadget, out_desc);\r
+       if (!ep) {\r
+               DBG(cdev, "usb_ep_autoconfig for ep_out failed\n");\r
+               return -ENODEV;\r
+       }\r
+       DBG(cdev, "usb_ep_autoconfig for mtp ep_out got %s\n", ep->name);\r
+       ep->driver_data = dev;          /* claim the endpoint */\r
+       dev->ep_out = ep;\r
+\r
+       ep = usb_ep_autoconfig(cdev->gadget, intr_desc);\r
+       if (!ep) {\r
+               DBG(cdev, "usb_ep_autoconfig for ep_intr failed\n");\r
+               return -ENODEV;\r
+       }\r
+       DBG(cdev, "usb_ep_autoconfig for mtp ep_intr got %s\n", ep->name);\r
+       ep->driver_data = dev;          /* claim the endpoint */\r
+       dev->ep_intr = ep;\r
+\r
+       /* now allocate requests for our endpoints */\r
+       for (i = 0; i < TX_REQ_MAX; i++) {\r
+               req = mtp_request_new(dev->ep_in, MTP_BULK_BUFFER_SIZE);\r
+               if (!req)\r
+                       goto fail;\r
+               req->complete = mtp_complete_in;\r
+               mtp_req_put(dev, &dev->tx_idle, req);\r
+       }\r
+       for (i = 0; i < RX_REQ_MAX; i++) {\r
+               req = mtp_request_new(dev->ep_out, MTP_BULK_BUFFER_SIZE);\r
+               if (!req)\r
+                       goto fail;\r
+               req->complete = mtp_complete_out;\r
+               dev->rx_req[i] = req;\r
+       }\r
+       for (i = 0; i < INTR_REQ_MAX; i++) {\r
+               req = mtp_request_new(dev->ep_intr, INTR_BUFFER_SIZE);\r
+               if (!req)\r
+                       goto fail;\r
+               req->complete = mtp_complete_intr;\r
+               mtp_req_put(dev, &dev->intr_idle, req);\r
+       }\r
+\r
+       return 0;\r
+\r
+fail:\r
+       pr_err("mtp_bind() could not allocate requests\n");\r
+       return -1;\r
+}\r
+\r
+static ssize_t mtp_read(struct file *fp, char __user *buf,\r
+       size_t count, loff_t *pos)\r
+{\r
+       struct mtp_dev *dev = fp->private_data;\r
+       struct usb_composite_dev *cdev = dev->cdev;\r
+       struct usb_request *req;\r
+       ssize_t r = count;\r
+       unsigned xfer;\r
+       int ret = 0;\r
+       size_t len = 0;\r
+\r
+       DBG(cdev, "mtp_read(%zu)\n", count);\r
+\r
+       /* we will block until we're online */\r
+       DBG(cdev, "mtp_read: waiting for online state\n");\r
+       ret = wait_event_interruptible(dev->read_wq,\r
+               dev->state != STATE_OFFLINE);\r
+       if (ret < 0) {\r
+               r = ret;\r
+               goto done;\r
+       }\r
+       spin_lock_irq(&dev->lock);\r
+       if (dev->ep_out->desc) {\r
+               len = usb_ep_align_maybe(cdev->gadget, dev->ep_out, count);\r
+               if (len > MTP_BULK_BUFFER_SIZE) {\r
+                       spin_unlock_irq(&dev->lock);\r
+                       return -EINVAL;\r
+               }\r
+       }\r
+\r
+       if (dev->state == STATE_CANCELED) {\r
+               /* report cancelation to userspace */\r
+               dev->state = STATE_READY;\r
+               spin_unlock_irq(&dev->lock);\r
+               return -ECANCELED;\r
+       }\r
+       dev->state = STATE_BUSY;\r
+       spin_unlock_irq(&dev->lock);\r
+\r
+requeue_req:\r
+       /* queue a request */\r
+       req = dev->rx_req[0];\r
+       req->length = len;\r
+       dev->rx_done = 0;\r
+       set_read_req_length(req);\r
+       ret = usb_ep_queue(dev->ep_out, req, GFP_KERNEL);\r
+       if (ret < 0) {\r
+               r = -EIO;\r
+               goto done;\r
+       } else {\r
+               DBG(cdev, "rx %p queue\n", req);\r
+       }\r
+\r
+       /* wait for a request to complete */\r
+       ret = wait_event_interruptible(dev->read_wq, dev->rx_done);\r
+       if (ret < 0) {\r
+               r = ret;\r
+               usb_ep_dequeue(dev->ep_out, req);\r
+               goto done;\r
+       }\r
+       if (dev->state == STATE_BUSY) {\r
+               /* If we got a 0-len packet, throw it back and try again. */\r
+               if (req->actual == 0)\r
+                       goto requeue_req;\r
+\r
+               DBG(cdev, "rx %p %d\n", req, req->actual);\r
+               xfer = (req->actual < count) ? req->actual : count;\r
+               r = xfer;\r
+               if (copy_to_user(buf, req->buf, xfer))\r
+                       r = -EFAULT;\r
+       } else\r
+               r = -EIO;\r
+\r
+done:\r
+       spin_lock_irq(&dev->lock);\r
+       if (dev->state == STATE_CANCELED)\r
+               r = -ECANCELED;\r
+       else if (dev->state != STATE_OFFLINE)\r
+               dev->state = STATE_READY;\r
+       spin_unlock_irq(&dev->lock);\r
+\r
+       DBG(cdev, "mtp_read returning %zd\n", r);\r
+       return r;\r
+}\r
+\r
+static ssize_t mtp_write(struct file *fp, const char __user *buf,\r
+       size_t count, loff_t *pos)\r
+{\r
+       struct mtp_dev *dev = fp->private_data;\r
+       struct usb_composite_dev *cdev = dev->cdev;\r
+       struct usb_request *req = 0;\r
+       ssize_t r = count;\r
+       unsigned xfer;\r
+       int sendZLP = 0;\r
+       int ret;\r
+\r
+       DBG(cdev, "mtp_write(%zu)\n", count);\r
+\r
+       spin_lock_irq(&dev->lock);\r
+       if (dev->state == STATE_CANCELED) {\r
+               /* report cancelation to userspace */\r
+               dev->state = STATE_READY;\r
+               spin_unlock_irq(&dev->lock);\r
+               return -ECANCELED;\r
+       }\r
+       if (dev->state == STATE_OFFLINE) {\r
+               spin_unlock_irq(&dev->lock);\r
+               return -ENODEV;\r
+       }\r
+       dev->state = STATE_BUSY;\r
+       spin_unlock_irq(&dev->lock);\r
+\r
+       /* we need to send a zero length packet to signal the end of transfer\r
+        * if the transfer size is aligned to a packet boundary.\r
+        */\r
+       if ((count & (dev->ep_in->maxpacket - 1)) == 0)\r
+               sendZLP = 1;\r
+\r
+       while (count > 0 || sendZLP) {\r
+               /* so we exit after sending ZLP */\r
+               if (count == 0)\r
+                       sendZLP = 0;\r
+\r
+               if (dev->state != STATE_BUSY) {\r
+                       DBG(cdev, "mtp_write dev->error\n");\r
+                       r = -EIO;\r
+                       break;\r
+               }\r
+\r
+               /* get an idle tx request to use */\r
+               req = 0;\r
+               ret = wait_event_interruptible(dev->write_wq,\r
+                       ((req = mtp_req_get(dev, &dev->tx_idle))\r
+                               || dev->state != STATE_BUSY));\r
+               if (!req) {\r
+                       r = ret;\r
+                       break;\r
+               }\r
+\r
+               if (count > MTP_BULK_BUFFER_SIZE)\r
+                       xfer = MTP_BULK_BUFFER_SIZE;\r
+               else\r
+                       xfer = count;\r
+               if (xfer && copy_from_user(req->buf, buf, xfer)) {\r
+                       r = -EFAULT;\r
+                       break;\r
+               }\r
+\r
+               req->length = xfer;\r
+               ret = usb_ep_queue(dev->ep_in, req, GFP_KERNEL);\r
+               if (ret < 0) {\r
+                       DBG(cdev, "mtp_write: xfer error %d\n", ret);\r
+                       r = -EIO;\r
+                       break;\r
+               }\r
+\r
+               buf += xfer;\r
+               count -= xfer;\r
+\r
+               /* zero this so we don't try to free it on error exit */\r
+               req = 0;\r
+       }\r
+\r
+       if (req)\r
+               mtp_req_put(dev, &dev->tx_idle, req);\r
+\r
+       spin_lock_irq(&dev->lock);\r
+       if (dev->state == STATE_CANCELED)\r
+               r = -ECANCELED;\r
+       else if (dev->state != STATE_OFFLINE)\r
+               dev->state = STATE_READY;\r
+       spin_unlock_irq(&dev->lock);\r
+\r
+       DBG(cdev, "mtp_write returning %zd\n", r);\r
+       return r;\r
+}\r
+\r
+/* read from a local file and write to USB */\r
+static void send_file_work(struct work_struct *data)\r
+{\r
+       struct mtp_dev *dev = container_of(data, struct mtp_dev,\r
+                                               send_file_work);\r
+       struct usb_composite_dev *cdev = dev->cdev;\r
+       struct usb_request *req = 0;\r
+       struct mtp_data_header *header;\r
+       struct file *filp;\r
+       loff_t offset;\r
+       int64_t count;\r
+       int xfer, ret, hdr_size;\r
+       int r = 0;\r
+       int sendZLP = 0;\r
+\r
+       /* read our parameters */\r
+       smp_rmb();\r
+       filp = dev->xfer_file;\r
+       offset = dev->xfer_file_offset;\r
+       count = dev->xfer_file_length;\r
+\r
+       DBG(cdev, "send_file_work(%lld %lld)\n", offset, count);\r
+\r
+       if (dev->xfer_send_header) {\r
+               hdr_size = sizeof(struct mtp_data_header);\r
+               count += hdr_size;\r
+       } else {\r
+               hdr_size = 0;\r
+       }\r
+\r
+       /* we need to send a zero length packet to signal the end of transfer\r
+        * if the transfer size is aligned to a packet boundary.\r
+        */\r
+       if ((count & (dev->ep_in->maxpacket - 1)) == 0)\r
+               sendZLP = 1;\r
+\r
+       while (count > 0 || sendZLP) {\r
+               /* so we exit after sending ZLP */\r
+               if (count == 0)\r
+                       sendZLP = 0;\r
+\r
+               /* get an idle tx request to use */\r
+               req = 0;\r
+               ret = wait_event_interruptible(dev->write_wq,\r
+                       (req = mtp_req_get(dev, &dev->tx_idle))\r
+                       || dev->state != STATE_BUSY);\r
+               if (dev->state == STATE_CANCELED) {\r
+                       r = -ECANCELED;\r
+                       break;\r
+               }\r
+               if (!req) {\r
+                       r = ret;\r
+                       break;\r
+               }\r
+\r
+               if (count > MTP_BULK_BUFFER_SIZE)\r
+                       xfer = MTP_BULK_BUFFER_SIZE;\r
+               else\r
+                       xfer = count;\r
+\r
+               if (hdr_size) {\r
+                       /* prepend MTP data header */\r
+                       header = (struct mtp_data_header *)req->buf;\r
+                       /*\r
+                         * set file size with header according to\r
+                         * MTP Specification v1.0\r
+                         */\r
+                       header->length = (count > MTP_MAX_FILE_SIZE) ?\r
+                               MTP_MAX_FILE_SIZE : __cpu_to_le32(count);\r
+                       header->type = __cpu_to_le16(2); /* data packet */\r
+                       header->command = __cpu_to_le16(dev->xfer_command);\r
+                       header->transaction_id =\r
+                                       __cpu_to_le32(dev->xfer_transaction_id);\r
+               }\r
+\r
+               ret = vfs_read(filp, req->buf + hdr_size, xfer - hdr_size,\r
+                                                               &offset);\r
+               if (ret < 0) {\r
+                       r = ret;\r
+                       break;\r
+               }\r
+               xfer = ret + hdr_size;\r
+               hdr_size = 0;\r
+\r
+               req->length = xfer;\r
+               ret = usb_ep_queue(dev->ep_in, req, GFP_KERNEL);\r
+               if (ret < 0) {\r
+                       DBG(cdev, "send_file_work: xfer error %d\n", ret);\r
+                       dev->state = STATE_ERROR;\r
+                       r = -EIO;\r
+                       break;\r
+               }\r
+\r
+               count -= xfer;\r
+\r
+               /* zero this so we don't try to free it on error exit */\r
+               req = 0;\r
+       }\r
+\r
+       if (req)\r
+               mtp_req_put(dev, &dev->tx_idle, req);\r
+\r
+       DBG(cdev, "send_file_work returning %d\n", r);\r
+       /* write the result */\r
+       dev->xfer_result = r;\r
+       smp_wmb();\r
+}\r
+\r
+/* read from USB and write to a local file */\r
+static void receive_file_work(struct work_struct *data)\r
+{\r
+       struct mtp_dev *dev = container_of(data, struct mtp_dev,\r
+                                               receive_file_work);\r
+       struct usb_composite_dev *cdev = dev->cdev;\r
+       struct usb_request *read_req = NULL, *write_req = NULL;\r
+       struct file *filp;\r
+       loff_t offset;\r
+       int64_t count;\r
+       int ret, cur_buf = 0;\r
+       int r = 0;\r
+\r
+       /* read our parameters */\r
+       smp_rmb();\r
+       filp = dev->xfer_file;\r
+       offset = dev->xfer_file_offset;\r
+       count = dev->xfer_file_length;\r
+\r
+       DBG(cdev, "receive_file_work(%lld)\n", count);\r
+\r
+       while (count > 0 || write_req) {\r
+               if (count > 0) {\r
+                       /* queue a request */\r
+                       read_req = dev->rx_req[cur_buf];\r
+                       cur_buf = (cur_buf + 1) % RX_REQ_MAX;\r
+\r
+                       read_req->length = (count > MTP_BULK_BUFFER_SIZE\r
+                                       ? MTP_BULK_BUFFER_SIZE : count);\r
+                       dev->rx_done = 0;\r
+\r
+                       set_read_req_length(read_req);\r
+                       ret = usb_ep_queue(dev->ep_out, read_req, GFP_KERNEL);\r
+                       if (ret < 0) {\r
+                               r = -EIO;\r
+                               dev->state = STATE_ERROR;\r
+                               break;\r
+                       }\r
+               }\r
+\r
+               if (write_req) {\r
+                       DBG(cdev, "rx %p %d\n", write_req, write_req->actual);\r
+                       ret = vfs_write(filp, write_req->buf, write_req->actual,\r
+                               &offset);\r
+                       DBG(cdev, "vfs_write %d\n", ret);\r
+                       if (ret != write_req->actual) {\r
+                               r = -EIO;\r
+                               dev->state = STATE_ERROR;\r
+                               break;\r
+                       }\r
+                       write_req = NULL;\r
+               }\r
+\r
+               if (read_req) {\r
+                       /* wait for our last read to complete */\r
+                       ret = wait_event_interruptible(dev->read_wq,\r
+                               dev->rx_done || dev->state != STATE_BUSY);\r
+                       if (dev->state == STATE_CANCELED) {\r
+                               r = -ECANCELED;\r
+                               if (!dev->rx_done)\r
+                                       usb_ep_dequeue(dev->ep_out, read_req);\r
+                               break;\r
+                       }\r
+                       if (read_req->status) {\r
+                               r = read_req->status;\r
+                               break;\r
+                       }\r
+                       /* if xfer_file_length is 0xFFFFFFFF, then we read until\r
+                        * we get a zero length packet\r
+                        */\r
+                       if (count != 0xFFFFFFFF)\r
+                               count -= read_req->actual;\r
+                       if (read_req->actual < read_req->length) {\r
+                               /*\r
+                                * short packet is used to signal EOF for\r
+                                * sizes > 4 gig\r
+                                */\r
+                               DBG(cdev, "got short packet\n");\r
+                               count = 0;\r
+                       }\r
+\r
+                       write_req = read_req;\r
+                       read_req = NULL;\r
+               }\r
+       }\r
+\r
+       DBG(cdev, "receive_file_work returning %d\n", r);\r
+       /* write the result */\r
+       dev->xfer_result = r;\r
+       smp_wmb();\r
+}\r
+\r
+static int mtp_send_event(struct mtp_dev *dev, struct mtp_event *event)\r
+{\r
+       struct usb_request *req = NULL;\r
+       int ret;\r
+       int length = event->length;\r
+\r
+       DBG(dev->cdev, "mtp_send_event(%zu)\n", event->length);\r
+\r
+       if (length < 0 || length > INTR_BUFFER_SIZE)\r
+               return -EINVAL;\r
+       if (dev->state == STATE_OFFLINE)\r
+               return -ENODEV;\r
+\r
+       ret = wait_event_interruptible_timeout(dev->intr_wq,\r
+                       (req = mtp_req_get(dev, &dev->intr_idle)),\r
+                       msecs_to_jiffies(1000));\r
+       if (!req)\r
+               return -ETIME;\r
+\r
+       if (copy_from_user(req->buf, (void __user *)event->data, length)) {\r
+               mtp_req_put(dev, &dev->intr_idle, req);\r
+               return -EFAULT;\r
+       }\r
+       req->length = length;\r
+       ret = usb_ep_queue(dev->ep_intr, req, GFP_KERNEL);\r
+       if (ret)\r
+               mtp_req_put(dev, &dev->intr_idle, req);\r
+\r
+       return ret;\r
+}\r
+\r
+static long mtp_ioctl(struct file *fp, unsigned code, unsigned long value)\r
+{\r
+       struct mtp_dev *dev = fp->private_data;\r
+       struct file *filp = NULL;\r
+       int ret = -EINVAL;\r
+\r
+       if (mtp_lock(&dev->ioctl_excl))\r
+               return -EBUSY;\r
+\r
+       switch (code) {\r
+       case MTP_SEND_FILE:\r
+       case MTP_RECEIVE_FILE:\r
+       case MTP_SEND_FILE_WITH_HEADER:\r
+       {\r
+               struct mtp_file_range   mfr;\r
+               struct work_struct *work;\r
+\r
+               spin_lock_irq(&dev->lock);\r
+               if (dev->state == STATE_CANCELED) {\r
+                       /* report cancelation to userspace */\r
+                       dev->state = STATE_READY;\r
+                       spin_unlock_irq(&dev->lock);\r
+                       ret = -ECANCELED;\r
+                       goto out;\r
+               }\r
+               if (dev->state == STATE_OFFLINE) {\r
+                       spin_unlock_irq(&dev->lock);\r
+                       ret = -ENODEV;\r
+                       goto out;\r
+               }\r
+               dev->state = STATE_BUSY;\r
+               spin_unlock_irq(&dev->lock);\r
+\r
+               if (copy_from_user(&mfr, (void __user *)value, sizeof(mfr))) {\r
+                       ret = -EFAULT;\r
+                       goto fail;\r
+               }\r
+               /* hold a reference to the file while we are working with it */\r
+               filp = fget(mfr.fd);\r
+               if (!filp) {\r
+                       ret = -EBADF;\r
+                       goto fail;\r
+               }\r
+\r
+               /* write the parameters */\r
+               dev->xfer_file = filp;\r
+               dev->xfer_file_offset = mfr.offset;\r
+               dev->xfer_file_length = mfr.length;\r
+               smp_wmb();\r
+\r
+               if (code == MTP_SEND_FILE_WITH_HEADER) {\r
+                       work = &dev->send_file_work;\r
+                       dev->xfer_send_header = 1;\r
+                       dev->xfer_command = mfr.command;\r
+                       dev->xfer_transaction_id = mfr.transaction_id;\r
+               } else if (code == MTP_SEND_FILE) {\r
+                       work = &dev->send_file_work;\r
+                       dev->xfer_send_header = 0;\r
+               } else {\r
+                       work = &dev->receive_file_work;\r
+               }\r
+\r
+               /* We do the file transfer on a work queue so it will run\r
+                * in kernel context, which is necessary for vfs_read and\r
+                * vfs_write to use our buffers in the kernel address space.\r
+                */\r
+               queue_work(dev->wq, work);\r
+               /* wait for operation to complete */\r
+               flush_workqueue(dev->wq);\r
+               fput(filp);\r
+\r
+               /* read the result */\r
+               smp_rmb();\r
+               ret = dev->xfer_result;\r
+               break;\r
+       }\r
+       case MTP_SEND_EVENT:\r
+       {\r
+               struct mtp_event        event;\r
+               /* return here so we don't change dev->state below,\r
+                * which would interfere with bulk transfer state.\r
+                */\r
+               if (copy_from_user(&event, (void __user *)value, sizeof(event)))\r
+                       ret = -EFAULT;\r
+               else\r
+                       ret = mtp_send_event(dev, &event);\r
+               goto out;\r
+       }\r
+       }\r
+\r
+fail:\r
+       spin_lock_irq(&dev->lock);\r
+       if (dev->state == STATE_CANCELED)\r
+               ret = -ECANCELED;\r
+       else if (dev->state != STATE_OFFLINE)\r
+               dev->state = STATE_READY;\r
+       spin_unlock_irq(&dev->lock);\r
+out:\r
+       mtp_unlock(&dev->ioctl_excl);\r
+       DBG(dev->cdev, "ioctl returning %d\n", ret);\r
+       return ret;\r
+}\r
+\r
+static int mtp_open(struct inode *ip, struct file *fp)\r
+{\r
+       printk(KERN_INFO "mtp_open\n");\r
+       if (mtp_lock(&_mtp_dev->open_excl))\r
+               return -EBUSY;\r
+\r
+       /* clear any error condition */\r
+       if (_mtp_dev->state != STATE_OFFLINE)\r
+               _mtp_dev->state = STATE_READY;\r
+\r
+       fp->private_data = _mtp_dev;\r
+       return 0;\r
+}\r
+\r
+static int mtp_release(struct inode *ip, struct file *fp)\r
+{\r
+       printk(KERN_INFO "mtp_release\n");\r
+\r
+       mtp_unlock(&_mtp_dev->open_excl);\r
+       return 0;\r
+}\r
+\r
+/* file operations for /dev/mtp_usb */\r
+static const struct file_operations mtp_fops = {\r
+       .owner = THIS_MODULE,\r
+       .read = mtp_read,\r
+       .write = mtp_write,\r
+       .unlocked_ioctl = mtp_ioctl,\r
+       .open = mtp_open,\r
+       .release = mtp_release,\r
+};\r
+\r
+static struct miscdevice mtp_device = {\r
+       .minor = MISC_DYNAMIC_MINOR,\r
+       .name = mtp_shortname,\r
+       .fops = &mtp_fops,\r
+};\r
+\r
+static int mtp_ctrlrequest(struct usb_composite_dev *cdev,\r
+                               const struct usb_ctrlrequest *ctrl)\r
+{\r
+       struct mtp_dev *dev = _mtp_dev;\r
+       int     value = -EOPNOTSUPP;\r
+       u16     w_index = le16_to_cpu(ctrl->wIndex);\r
+       u16     w_value = le16_to_cpu(ctrl->wValue);\r
+       u16     w_length = le16_to_cpu(ctrl->wLength);\r
+       unsigned long   flags;\r
+\r
+       VDBG(cdev, "mtp_ctrlrequest "\r
+                       "%02x.%02x v%04x i%04x l%u\n",\r
+                       ctrl->bRequestType, ctrl->bRequest,\r
+                       w_value, w_index, w_length);\r
+\r
+       /* Handle MTP OS string */\r
+       if (ctrl->bRequestType ==\r
+                       (USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE)\r
+                       && ctrl->bRequest == USB_REQ_GET_DESCRIPTOR\r
+                       && (w_value >> 8) == USB_DT_STRING\r
+                       && (w_value & 0xFF) == MTP_OS_STRING_ID) {\r
+               value = (w_length < sizeof(mtp_os_string)\r
+                               ? w_length : sizeof(mtp_os_string));\r
+               memcpy(cdev->req->buf, mtp_os_string, value);\r
+       } else if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_VENDOR) {\r
+               /* Handle MTP OS descriptor */\r
+               DBG(cdev, "vendor request: %d index: %d value: %d length: %d\n",\r
+                       ctrl->bRequest, w_index, w_value, w_length);\r
+\r
+               if (ctrl->bRequest == 1\r
+                               && (ctrl->bRequestType & USB_DIR_IN)\r
+                               && (w_index == 4 || w_index == 5)) {\r
+                       value = (w_length < sizeof(mtp_ext_config_desc) ?\r
+                                       w_length : sizeof(mtp_ext_config_desc));\r
+                       memcpy(cdev->req->buf, &mtp_ext_config_desc, value);\r
+               }\r
+       } else if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_CLASS) {\r
+               DBG(cdev, "class request: %d index: %d value: %d length: %d\n",\r
+                       ctrl->bRequest, w_index, w_value, w_length);\r
+\r
+               if (ctrl->bRequest == MTP_REQ_CANCEL && w_index == 0\r
+                               && w_value == 0) {\r
+                       DBG(cdev, "MTP_REQ_CANCEL\n");\r
+\r
+                       spin_lock_irqsave(&dev->lock, flags);\r
+                       if (dev->state == STATE_BUSY) {\r
+                               dev->state = STATE_CANCELED;\r
+                               wake_up(&dev->read_wq);\r
+                               wake_up(&dev->write_wq);\r
+                       }\r
+                       spin_unlock_irqrestore(&dev->lock, flags);\r
+\r
+                       /* We need to queue a request to read the remaining\r
+                        *  bytes, but we don't actually need to look at\r
+                        * the contents.\r
+                        */\r
+                       value = w_length;\r
+               } else if (ctrl->bRequest == MTP_REQ_GET_DEVICE_STATUS\r
+                               && w_index == 0 && w_value == 0) {\r
+                       struct mtp_device_status *status = cdev->req->buf;\r
+\r
+                       status->wLength =\r
+                               __constant_cpu_to_le16(sizeof(*status));\r
+\r
+                       DBG(cdev, "MTP_REQ_GET_DEVICE_STATUS\n");\r
+                       spin_lock_irqsave(&dev->lock, flags);\r
+                       /* device status is "busy" until we report\r
+                        * the cancelation to userspace\r
+                        */\r
+                       if (dev->state == STATE_CANCELED)\r
+                               status->wCode =\r
+                                       __cpu_to_le16(MTP_RESPONSE_DEVICE_BUSY);\r
+                       else\r
+                               status->wCode =\r
+                                       __cpu_to_le16(MTP_RESPONSE_OK);\r
+                       spin_unlock_irqrestore(&dev->lock, flags);\r
+                       value = sizeof(*status);\r
+               }\r
+       }\r
+\r
+       /* respond with data transfer or status phase? */\r
+       if (value >= 0) {\r
+               int rc;\r
+\r
+               cdev->req->zero = value < w_length;\r
+               cdev->req->length = value;\r
+               rc = usb_ep_queue(cdev->gadget->ep0, cdev->req, GFP_ATOMIC);\r
+               if (rc < 0)\r
+                       ERROR(cdev, "%s: response queue error\n", __func__);\r
+       }\r
+       return value;\r
+}\r
+\r
+static int\r
+mtp_function_bind(struct usb_configuration *c, struct usb_function *f)\r
+{\r
+       struct usb_composite_dev *cdev = c->cdev;\r
+       struct mtp_dev  *dev = func_to_mtp(f);\r
+       int                     id;\r
+       int                     ret;\r
+       struct mtp_instance *fi_mtp;\r
+\r
+       dev->cdev = cdev;\r
+       DBG(cdev, "mtp_function_bind dev: %p\n", dev);\r
+\r
+       /* allocate interface ID(s) */\r
+       id = usb_interface_id(c, f);\r
+       if (id < 0)\r
+               return id;\r
+       mtp_interface_desc.bInterfaceNumber = id;\r
+\r
+       if (mtp_string_defs[INTERFACE_STRING_INDEX].id == 0) {\r
+               ret = usb_string_id(c->cdev);\r
+               if (ret < 0)\r
+                       return ret;\r
+               mtp_string_defs[INTERFACE_STRING_INDEX].id = ret;\r
+               mtp_interface_desc.iInterface = ret;\r
+       }\r
+\r
+       fi_mtp = container_of(f->fi, struct mtp_instance, func_inst);\r
+\r
+       if (cdev->use_os_string) {\r
+               f->os_desc_table = kzalloc(sizeof(*f->os_desc_table),\r
+                                       GFP_KERNEL);\r
+               if (!f->os_desc_table)\r
+                       return -ENOMEM;\r
+               f->os_desc_n = 1;\r
+               f->os_desc_table[0].os_desc = &fi_mtp->mtp_os_desc;\r
+       }\r
+\r
+       /* allocate endpoints */\r
+       ret = mtp_create_bulk_endpoints(dev, &mtp_fullspeed_in_desc,\r
+                       &mtp_fullspeed_out_desc, &mtp_intr_desc);\r
+       if (ret)\r
+               return ret;\r
+\r
+       /* support high speed hardware */\r
+       if (gadget_is_dualspeed(c->cdev->gadget)) {\r
+               mtp_highspeed_in_desc.bEndpointAddress =\r
+                       mtp_fullspeed_in_desc.bEndpointAddress;\r
+               mtp_highspeed_out_desc.bEndpointAddress =\r
+                       mtp_fullspeed_out_desc.bEndpointAddress;\r
+       }\r
+       /* support super speed hardware */\r
+       if (gadget_is_superspeed(c->cdev->gadget)) {\r
+               unsigned max_burst;\r
+\r
+               /* Calculate bMaxBurst, we know packet size is 1024 */\r
+               max_burst = min_t(unsigned, MTP_BULK_BUFFER_SIZE / 1024, 15);\r
+               mtp_ss_in_desc.bEndpointAddress =\r
+                       mtp_fullspeed_in_desc.bEndpointAddress;\r
+               mtp_ss_in_comp_desc.bMaxBurst = max_burst;\r
+               mtp_ss_out_desc.bEndpointAddress =\r
+                       mtp_fullspeed_out_desc.bEndpointAddress;\r
+               mtp_ss_out_comp_desc.bMaxBurst = max_burst;\r
+       }\r
+\r
+       DBG(cdev, "%s speed %s: IN/%s, OUT/%s\n",\r
+               gadget_is_superspeed(c->cdev->gadget) ? "super" :\r
+               (gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full"),\r
+               f->name, dev->ep_in->name, dev->ep_out->name);\r
+       return 0;\r
+}\r
+\r
+static void\r
+mtp_function_unbind(struct usb_configuration *c, struct usb_function *f)\r
+{\r
+       struct mtp_dev  *dev = func_to_mtp(f);\r
+       struct usb_request *req;\r
+       int i;\r
+\r
+       mtp_string_defs[INTERFACE_STRING_INDEX].id = 0;\r
+       while ((req = mtp_req_get(dev, &dev->tx_idle)))\r
+               mtp_request_free(req, dev->ep_in);\r
+       for (i = 0; i < RX_REQ_MAX; i++)\r
+               mtp_request_free(dev->rx_req[i], dev->ep_out);\r
+       while ((req = mtp_req_get(dev, &dev->intr_idle)))\r
+               mtp_request_free(req, dev->ep_intr);\r
+       dev->state = STATE_OFFLINE;\r
+       kfree(f->os_desc_table);\r
+       f->os_desc_n = 0;\r
+}\r
+\r
+static int mtp_function_set_alt(struct usb_function *f,\r
+               unsigned intf, unsigned alt)\r
+{\r
+       struct mtp_dev  *dev = func_to_mtp(f);\r
+       struct usb_composite_dev *cdev = f->config->cdev;\r
+       int ret;\r
+\r
+       DBG(cdev, "mtp_function_set_alt intf: %d alt: %d\n", intf, alt);\r
+\r
+       ret = config_ep_by_speed(cdev->gadget, f, dev->ep_in);\r
+       if (ret)\r
+               return ret;\r
+\r
+       ret = usb_ep_enable(dev->ep_in);\r
+       if (ret)\r
+               return ret;\r
+\r
+       ret = config_ep_by_speed(cdev->gadget, f, dev->ep_out);\r
+       if (ret)\r
+               return ret;\r
+\r
+       ret = usb_ep_enable(dev->ep_out);\r
+       if (ret) {\r
+               usb_ep_disable(dev->ep_in);\r
+               return ret;\r
+       }\r
+\r
+       ret = config_ep_by_speed(cdev->gadget, f, dev->ep_intr);\r
+       if (ret)\r
+               return ret;\r
+\r
+       ret = usb_ep_enable(dev->ep_intr);\r
+       if (ret) {\r
+               usb_ep_disable(dev->ep_out);\r
+               usb_ep_disable(dev->ep_in);\r
+               return ret;\r
+       }\r
+       dev->state = STATE_READY;\r
+\r
+       /* readers may be blocked waiting for us to go online */\r
+       wake_up(&dev->read_wq);\r
+       return 0;\r
+}\r
+\r
+static void mtp_function_disable(struct usb_function *f)\r
+{\r
+       struct mtp_dev  *dev = func_to_mtp(f);\r
+       struct usb_composite_dev        *cdev = dev->cdev;\r
+\r
+       DBG(cdev, "mtp_function_disable\n");\r
+       dev->state = STATE_OFFLINE;\r
+       usb_ep_disable(dev->ep_in);\r
+       usb_ep_disable(dev->ep_out);\r
+       usb_ep_disable(dev->ep_intr);\r
+\r
+       /* readers may be blocked waiting for us to go online */\r
+       wake_up(&dev->read_wq);\r
+\r
+       VDBG(cdev, "%s disabled\n", dev->function.name);\r
+}\r
+\r
+static int __mtp_setup(struct mtp_instance *fi_mtp)\r
+{\r
+       struct mtp_dev *dev;\r
+       int ret;\r
+\r
+       dev = kzalloc(sizeof(*dev), GFP_KERNEL);\r
+\r
+       if (fi_mtp != NULL)\r
+               fi_mtp->dev = dev;\r
+\r
+       if (!dev)\r
+               return -ENOMEM;\r
+\r
+       spin_lock_init(&dev->lock);\r
+       init_waitqueue_head(&dev->read_wq);\r
+       init_waitqueue_head(&dev->write_wq);\r
+       init_waitqueue_head(&dev->intr_wq);\r
+       atomic_set(&dev->open_excl, 0);\r
+       atomic_set(&dev->ioctl_excl, 0);\r
+       INIT_LIST_HEAD(&dev->tx_idle);\r
+       INIT_LIST_HEAD(&dev->intr_idle);\r
+\r
+       dev->wq = create_singlethread_workqueue("f_mtp");\r
+       if (!dev->wq) {\r
+               ret = -ENOMEM;\r
+               goto err1;\r
+       }\r
+       INIT_WORK(&dev->send_file_work, send_file_work);\r
+       INIT_WORK(&dev->receive_file_work, receive_file_work);\r
+\r
+       _mtp_dev = dev;\r
+\r
+       ret = misc_register(&mtp_device);\r
+       if (ret)\r
+               goto err2;\r
+\r
+       return 0;\r
+\r
+err2:\r
+       destroy_workqueue(dev->wq);\r
+err1:\r
+       _mtp_dev = NULL;\r
+       kfree(dev);\r
+       printk(KERN_ERR "mtp gadget driver failed to initialize\n");\r
+       return ret;\r
+}\r
+\r
+static int mtp_setup_configfs(struct mtp_instance *fi_mtp)\r
+{\r
+       return __mtp_setup(fi_mtp);\r
+}\r
+\r
+\r
+static void mtp_cleanup(void)\r
+{\r
+       struct mtp_dev *dev = _mtp_dev;\r
+\r
+       if (!dev)\r
+               return;\r
+\r
+       misc_deregister(&mtp_device);\r
+       destroy_workqueue(dev->wq);\r
+       _mtp_dev = NULL;\r
+       kfree(dev);\r
+}\r
+\r
+static struct mtp_instance *to_mtp_instance(struct config_item *item)\r
+{\r
+       return container_of(to_config_group(item), struct mtp_instance,\r
+               func_inst.group);\r
+}\r
+\r
+static void mtp_attr_release(struct config_item *item)\r
+{\r
+       struct mtp_instance *fi_mtp = to_mtp_instance(item);\r
+\r
+       usb_put_function_instance(&fi_mtp->func_inst);\r
+}\r
+\r
+static struct configfs_item_operations mtp_item_ops = {\r
+       .release        = mtp_attr_release,\r
+};\r
+\r
+static struct config_item_type mtp_func_type = {\r
+       .ct_item_ops    = &mtp_item_ops,\r
+       .ct_owner       = THIS_MODULE,\r
+};\r
+\r
+\r
+static struct mtp_instance *to_fi_mtp(struct usb_function_instance *fi)\r
+{\r
+       return container_of(fi, struct mtp_instance, func_inst);\r
+}\r
+\r
+static int mtp_set_inst_name(struct usb_function_instance *fi, const char *name)\r
+{\r
+       struct mtp_instance *fi_mtp;\r
+       char *ptr;\r
+       int name_len;\r
+\r
+       name_len = strlen(name) + 1;\r
+       if (name_len > MAX_INST_NAME_LEN)\r
+               return -ENAMETOOLONG;\r
+\r
+       ptr = kstrndup(name, name_len, GFP_KERNEL);\r
+       if (!ptr)\r
+               return -ENOMEM;\r
+\r
+       fi_mtp = to_fi_mtp(fi);\r
+       fi_mtp->name = ptr;\r
+\r
+       return 0;\r
+}\r
+\r
+static void mtp_free_inst(struct usb_function_instance *fi)\r
+{\r
+       struct mtp_instance *fi_mtp;\r
+\r
+       fi_mtp = to_fi_mtp(fi);\r
+       kfree(fi_mtp->name);\r
+       mtp_cleanup();\r
+       kfree(fi_mtp);\r
+}\r
+\r
+struct usb_function_instance *alloc_inst_mtp_ptp(bool mtp_config)\r
+{\r
+       struct mtp_instance *fi_mtp;\r
+       int ret = 0;\r
+       struct usb_os_desc *descs[1];\r
+       char *names[1];\r
+\r
+       fi_mtp = kzalloc(sizeof(*fi_mtp), GFP_KERNEL);\r
+       if (!fi_mtp)\r
+               return ERR_PTR(-ENOMEM);\r
+       fi_mtp->func_inst.set_inst_name = mtp_set_inst_name;\r
+       fi_mtp->func_inst.free_func_inst = mtp_free_inst;\r
+\r
+       fi_mtp->mtp_os_desc.ext_compat_id = fi_mtp->mtp_ext_compat_id;\r
+       INIT_LIST_HEAD(&fi_mtp->mtp_os_desc.ext_prop);\r
+       descs[0] = &fi_mtp->mtp_os_desc;\r
+       names[0] = "MTP";\r
+\r
+       if (mtp_config) {\r
+               ret = mtp_setup_configfs(fi_mtp);\r
+               if (ret) {\r
+                       kfree(fi_mtp);\r
+                       pr_err("Error setting MTP\n");\r
+                       return ERR_PTR(ret);\r
+               }\r
+       } else\r
+               fi_mtp->dev = _mtp_dev;\r
+\r
+       config_group_init_type_name(&fi_mtp->func_inst.group,\r
+                                       "", &mtp_func_type);\r
+       usb_os_desc_prepare_interf_dir(&fi_mtp->func_inst.group, 1,\r
+                                       descs, names, THIS_MODULE);\r
+\r
+       return  &fi_mtp->func_inst;\r
+}\r
+EXPORT_SYMBOL_GPL(alloc_inst_mtp_ptp);\r
+\r
+static struct usb_function_instance *mtp_alloc_inst(void)\r
+{\r
+               return alloc_inst_mtp_ptp(true);\r
+}\r
+\r
+static int mtp_ctrlreq_configfs(struct usb_function *f,\r
+                               const struct usb_ctrlrequest *ctrl)\r
+{\r
+       return mtp_ctrlrequest(f->config->cdev, ctrl);\r
+}\r
+\r
+static void mtp_free(struct usb_function *f)\r
+{\r
+       /*NO-OP: no function specific resource allocation in mtp_alloc*/\r
+}\r
+\r
+struct usb_function *function_alloc_mtp_ptp(struct usb_function_instance *fi,\r
+                                       bool mtp_config)\r
+{\r
+       struct mtp_instance *fi_mtp = to_fi_mtp(fi);\r
+       struct mtp_dev *dev;\r
+#ifdef CONFIG_USB_CONFIGFS_UEVENT\r
+       struct usb_function *function;\r
+#endif\r
+\r
+       /*\r
+        * PTP piggybacks on MTP function so make sure we have\r
+        * created MTP function before we associate this PTP\r
+        * function with a gadget configuration.\r
+        */\r
+       if (fi_mtp->dev == NULL) {\r
+               pr_err("Error: Create MTP function before linking"\r
+                               " PTP function with a gadget configuration\n");\r
+               pr_err("\t1: Delete existing PTP function if any\n");\r
+               pr_err("\t2: Create MTP function\n");\r
+               pr_err("\t3: Create and symlink PTP function"\r
+                               " with a gadget configuration\n");\r
+               return ERR_PTR(-EINVAL); /* Invalid Configuration */\r
+       }\r
+\r
+       dev = fi_mtp->dev;\r
+#ifdef CONFIG_USB_CONFIGFS_UEVENT\r
+       if (mtp_config) {\r
+               function = &dev->function;\r
+               function->name = DRIVER_NAME;\r
+               function->fs_descriptors = fs_mtp_descs;\r
+               function->hs_descriptors = hs_mtp_descs;\r
+               function->ss_descriptors = ss_mtp_descs;\r
+       } else {\r
+               function = &dev->function_ptp;\r
+               function->name = DRIVER_NAME_PTP;\r
+               function->fs_descriptors = fs_ptp_descs;\r
+               function->hs_descriptors = hs_ptp_descs;\r
+               function->ss_descriptors = ss_ptp_descs;\r
+       }\r
+\r
+       function->strings = mtp_strings;\r
+       function->bind = mtp_function_bind;\r
+       function->unbind = mtp_function_unbind;\r
+       function->set_alt = mtp_function_set_alt;\r
+       function->disable = mtp_function_disable;\r
+       function->setup = mtp_ctrlreq_configfs;\r
+       function->free_func = mtp_free;\r
+\r
+       return function;\r
+#else\r
+       dev->function.name = DRIVER_NAME;\r
+       dev->function.strings = mtp_strings;\r
+       if (mtp_config) {\r
+               dev->function.fs_descriptors = fs_mtp_descs;\r
+               dev->function.hs_descriptors = hs_mtp_descs;\r
+               dev->function.ss_descriptors = ss_mtp_descs;\r
+       } else {\r
+               dev->function.fs_descriptors = fs_ptp_descs;\r
+               dev->function.hs_descriptors = hs_ptp_descs;\r
+               dev->function.ss_descriptors = ss_ptp_descs;\r
+       }\r
+       dev->function.bind = mtp_function_bind;\r
+       dev->function.unbind = mtp_function_unbind;\r
+       dev->function.set_alt = mtp_function_set_alt;\r
+       dev->function.disable = mtp_function_disable;\r
+       dev->function.setup = mtp_ctrlreq_configfs;\r
+       dev->function.free_func = mtp_free;\r
+\r
+       return &dev->function;\r
+#endif\r
+}\r
+EXPORT_SYMBOL_GPL(function_alloc_mtp_ptp);\r
+\r
+static struct usb_function *mtp_alloc(struct usb_function_instance *fi)\r
+{\r
+       return function_alloc_mtp_ptp(fi, true);\r
+}\r
+\r
+DECLARE_USB_FUNCTION_INIT(mtp, mtp_alloc_inst, mtp_alloc);\r
+MODULE_LICENSE("GPL");\r
diff --git a/drivers/usb/gadget/function/f_mtp.h b/drivers/usb/gadget/function/f_mtp.h
new file mode 100644 (file)
index 0000000..739d99a
--- /dev/null
@@ -0,0 +1,18 @@
+/*\r
+ * Copyright (C) 2014 Google, Inc.\r
+ * Author: Badhri Jagan Sridharan <badhri@android.com>\r
+ *\r
+ * This software is licensed under the terms of the GNU General Public\r
+ * License version 2, as published by the Free Software Foundation, and\r
+ * may be copied, distributed, and modified under those terms.\r
+ *\r
+ * This program is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+ * GNU General Public License for more details.\r
+ *\r
+ */\r
+\r
+extern struct usb_function_instance *alloc_inst_mtp_ptp(bool mtp_config);\r
+extern struct usb_function *function_alloc_mtp_ptp(\r
+                       struct usb_function_instance *fi, bool mtp_config);\r
diff --git a/drivers/usb/gadget/function/f_ptp.c b/drivers/usb/gadget/function/f_ptp.c
new file mode 100644 (file)
index 0000000..efcc5c5
--- /dev/null
@@ -0,0 +1,38 @@
+/*\r
+ * Gadget Function Driver for PTP\r
+ *\r
+ * Copyright (C) 2014 Google, Inc.\r
+ * Author: Badhri Jagan Sridharan <badhri@android.com>\r
+ *\r
+ * This software is licensed under the terms of the GNU General Public\r
+ * License version 2, as published by the Free Software Foundation, and\r
+ * may be copied, distributed, and modified under those terms.\r
+ *\r
+ * This program is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+ * GNU General Public License for more details.\r
+ *\r
+ */\r
+\r
+#include <linux/module.h>\r
+#include <linux/types.h>\r
+\r
+#include <linux/configfs.h>\r
+#include <linux/usb/composite.h>\r
+\r
+#include "f_mtp.h"\r
+\r
+static struct usb_function_instance *ptp_alloc_inst(void)\r
+{\r
+       return alloc_inst_mtp_ptp(false);\r
+}\r
+\r
+static struct usb_function *ptp_alloc(struct usb_function_instance *fi)\r
+{\r
+       return function_alloc_mtp_ptp(fi, false);\r
+}\r
+\r
+DECLARE_USB_FUNCTION_INIT(ptp, ptp_alloc_inst, ptp_alloc);\r
+MODULE_LICENSE("GPL");\r
+MODULE_AUTHOR("Badhri Jagan Sridharan");\r
diff --git a/drivers/usb/gadget/function/f_uts.c b/drivers/usb/gadget/function/f_uts.c
new file mode 100644 (file)
index 0000000..8c34236
--- /dev/null
@@ -0,0 +1,827 @@
+/*\r
+ * f_uts.c -- USB CDC serial (ACM) function driver\r
+ *\r
+ * Copyright (C) 2003 Al Borchers (alborchers@steinerpoint.com)\r
+ * Copyright (C) 2008 by David Brownell\r
+ * Copyright (C) 2008 by Nokia Corporation\r
+ * Copyright (C) 2009 by Samsung Electronics\r
+ * Author: Michal Nazarewicz (mina86@mina86.com)\r
+ *\r
+ * This software is distributed under the terms of the GNU General\r
+ * Public License ("GPL") as published by the Free Software Foundation,\r
+ * either version 2 of that License or (at your option) any later version.\r
+ */\r
+\r
+/* #define VERBOSE_DEBUG */\r
+\r
+#include <linux/slab.h>\r
+#include <linux/kernel.h>\r
+#include <linux/module.h>\r
+#include <linux/device.h>\r
+#include <linux/err.h>\r
+\r
+#include "u_serial.h"\r
+\r
+\r
+/*\r
+ * This CDC ACM function support just wraps control functions and\r
+ * notifications around the generic serial-over-usb code.\r
+ *\r
+ * Because CDC ACM is standardized by the USB-IF, many host operating\r
+ * systems have drivers for it.  Accordingly, ACM is the preferred\r
+ * interop solution for serial-port type connections.  The control\r
+ * models are often not necessary, and in any case don't do much in\r
+ * this bare-bones implementation.\r
+ *\r
+ * Note that even MS-Windows has some support for ACM.  However, that\r
+ * support is somewhat broken because when you use ACM in a composite\r
+ * device, having multiple interfaces confuses the poor OS.  It doesn't\r
+ * seem to understand CDC Union descriptors.  The new "association"\r
+ * descriptors (roughly equivalent to CDC Unions) may sometimes help.\r
+ */\r
+\r
+/* UTS_PORT NUM : /dev/ttyGS* port number */\r
+#define UTS_PORT_NUM                   2\r
+\r
+struct f_uts {\r
+       struct gserial                  port;\r
+       u8                              ctrl_id, data_id;\r
+       u8                              port_num;\r
+\r
+       u8                              pending;\r
+\r
+       /* lock is mostly for pending and notify_req ... they get accessed\r
+        * by callbacks both from tty (open/close/break) under its spinlock,\r
+        * and notify_req.complete() which can't use that lock.\r
+        */\r
+       spinlock_t                      lock;\r
+\r
+       struct usb_ep                   *notify;\r
+       struct usb_request              *notify_req;\r
+\r
+       struct usb_cdc_line_coding      port_line_coding;       /* 8-N-1 etc */\r
+\r
+       /* SetControlLineState request -- CDC 1.1 section 6.2.14 (INPUT) */\r
+       u16                             port_handshake_bits;\r
+#define ACM_CTRL_RTS   (1 << 1)        /* unused with full duplex */\r
+#define ACM_CTRL_DTR   (1 << 0)        /* host is ready for data r/w */\r
+\r
+       /* SerialState notification -- CDC 1.1 section 6.3.5 (OUTPUT) */\r
+       u16                             serial_state;\r
+#define ACM_CTRL_OVERRUN       (1 << 6)\r
+#define ACM_CTRL_PARITY                (1 << 5)\r
+#define ACM_CTRL_FRAMING       (1 << 4)\r
+#define ACM_CTRL_RI            (1 << 3)\r
+#define ACM_CTRL_BRK           (1 << 2)\r
+#define ACM_CTRL_DSR           (1 << 1)\r
+#define ACM_CTRL_DCD           (1 << 0)\r
+};\r
+\r
+static inline struct f_uts *func_to_uts(struct usb_function *f)\r
+{\r
+       return container_of(f, struct f_uts, port.func);\r
+}\r
+\r
+static inline struct f_uts *port_to_uts(struct gserial *p)\r
+{\r
+       return container_of(p, struct f_uts, port);\r
+}\r
+\r
+/*-------------------------------------------------------------------------*/\r
+\r
+/* notification endpoint uses smallish and infrequent fixed-size messages */\r
+\r
+#define GS_NOTIFY_INTERVAL_MS          32\r
+#define GS_NOTIFY_MAXPACKET            10      /* notification + 2 bytes */\r
+\r
+/* interface and class descriptors: */\r
+\r
+static struct usb_interface_assoc_descriptor\r
+uts_iad_descriptor = {\r
+       .bLength =              sizeof(uts_iad_descriptor),\r
+       .bDescriptorType =      USB_DT_INTERFACE_ASSOCIATION,\r
+\r
+       /* .bFirstInterface =   DYNAMIC, */\r
+       .bInterfaceCount = 2,   // control + data\r
+       .bFunctionClass =       USB_CLASS_COMM,\r
+       .bFunctionSubClass =    USB_CDC_SUBCLASS_ACM,\r
+       .bFunctionProtocol =    USB_CDC_ACM_PROTO_AT_V25TER,\r
+       /* .iFunction =         DYNAMIC */\r
+};\r
+\r
+\r
+static struct usb_interface_descriptor uts_control_interface_desc = {\r
+       .bLength =              USB_DT_INTERFACE_SIZE,\r
+       .bDescriptorType =      USB_DT_INTERFACE,\r
+       /* .bInterfaceNumber = DYNAMIC */\r
+       .bNumEndpoints =        1,\r
+       .bInterfaceClass =      USB_CLASS_COMM,\r
+       .bInterfaceSubClass =   USB_CDC_SUBCLASS_ACM,\r
+       .bInterfaceProtocol =   USB_CDC_ACM_PROTO_AT_V25TER,\r
+       /* .iInterface = DYNAMIC */\r
+};\r
+\r
+static struct usb_interface_descriptor uts_data_interface_desc = {\r
+       .bLength =              USB_DT_INTERFACE_SIZE,\r
+       .bDescriptorType =      USB_DT_INTERFACE,\r
+       /* .bInterfaceNumber = DYNAMIC */\r
+       .bNumEndpoints =        2,\r
+       .bInterfaceClass =      USB_CLASS_CDC_DATA,\r
+       .bInterfaceSubClass =   0,\r
+       .bInterfaceProtocol =   0,\r
+       /* .iInterface = DYNAMIC */\r
+};\r
+\r
+static struct usb_cdc_header_desc uts_header_desc = {\r
+       .bLength =              sizeof(uts_header_desc),\r
+       .bDescriptorType =      USB_DT_CS_INTERFACE,\r
+       .bDescriptorSubType =   USB_CDC_HEADER_TYPE,\r
+       .bcdCDC =               cpu_to_le16(0x0110),\r
+};\r
+\r
+static struct usb_cdc_call_mgmt_descriptor\r
+uts_call_mgmt_descriptor = {\r
+       .bLength =              sizeof(uts_call_mgmt_descriptor),\r
+       .bDescriptorType =      USB_DT_CS_INTERFACE,\r
+       .bDescriptorSubType =   USB_CDC_CALL_MANAGEMENT_TYPE,\r
+       .bmCapabilities =       0,\r
+       /* .bDataInterface = DYNAMIC */\r
+};\r
+\r
+static struct usb_cdc_acm_descriptor uts_descriptor = {\r
+       .bLength =              sizeof(uts_descriptor),\r
+       .bDescriptorType =      USB_DT_CS_INTERFACE,\r
+       .bDescriptorSubType =   USB_CDC_ACM_TYPE,\r
+       .bmCapabilities =       USB_CDC_CAP_LINE,\r
+};\r
+\r
+static struct usb_cdc_union_desc uts_union_desc = {\r
+       .bLength =              sizeof(uts_union_desc),\r
+       .bDescriptorType =      USB_DT_CS_INTERFACE,\r
+       .bDescriptorSubType =   USB_CDC_UNION_TYPE,\r
+       /* .bMasterInterface0 = DYNAMIC */\r
+       /* .bSlaveInterface0 =  DYNAMIC */\r
+};\r
+\r
+/* full speed support: */\r
+\r
+static struct usb_endpoint_descriptor uts_fs_notify_desc = {\r
+       .bLength =              USB_DT_ENDPOINT_SIZE,\r
+       .bDescriptorType =      USB_DT_ENDPOINT,\r
+       .bEndpointAddress =     USB_DIR_IN,\r
+       .bmAttributes =         USB_ENDPOINT_XFER_INT,\r
+       .wMaxPacketSize =       cpu_to_le16(GS_NOTIFY_MAXPACKET),\r
+       .bInterval =            GS_NOTIFY_INTERVAL_MS,\r
+};\r
+\r
+static struct usb_endpoint_descriptor uts_fs_in_desc = {\r
+       .bLength =              USB_DT_ENDPOINT_SIZE,\r
+       .bDescriptorType =      USB_DT_ENDPOINT,\r
+       .bEndpointAddress =     USB_DIR_IN,\r
+       .bmAttributes =         USB_ENDPOINT_XFER_BULK,\r
+};\r
+\r
+static struct usb_endpoint_descriptor uts_fs_out_desc = {\r
+       .bLength =              USB_DT_ENDPOINT_SIZE,\r
+       .bDescriptorType =      USB_DT_ENDPOINT,\r
+       .bEndpointAddress =     USB_DIR_OUT,\r
+       .bmAttributes =         USB_ENDPOINT_XFER_BULK,\r
+};\r
+\r
+static struct usb_descriptor_header *uts_fs_function[] = {\r
+       (struct usb_descriptor_header *) &uts_iad_descriptor,\r
+       (struct usb_descriptor_header *) &uts_control_interface_desc,\r
+       (struct usb_descriptor_header *) &uts_header_desc,\r
+       (struct usb_descriptor_header *) &uts_call_mgmt_descriptor,\r
+       (struct usb_descriptor_header *) &uts_descriptor,\r
+       (struct usb_descriptor_header *) &uts_union_desc,\r
+       (struct usb_descriptor_header *) &uts_fs_notify_desc,\r
+       (struct usb_descriptor_header *) &uts_data_interface_desc,\r
+       (struct usb_descriptor_header *) &uts_fs_in_desc,\r
+       (struct usb_descriptor_header *) &uts_fs_out_desc,\r
+       NULL,\r
+};\r
+\r
+/* high speed support: */\r
+static struct usb_endpoint_descriptor uts_hs_notify_desc = {\r
+       .bLength =              USB_DT_ENDPOINT_SIZE,\r
+       .bDescriptorType =      USB_DT_ENDPOINT,\r
+       .bEndpointAddress =     USB_DIR_IN,\r
+       .bmAttributes =         USB_ENDPOINT_XFER_INT,\r
+       .wMaxPacketSize =       cpu_to_le16(GS_NOTIFY_MAXPACKET),\r
+       .bInterval =            USB_MS_TO_HS_INTERVAL(GS_NOTIFY_INTERVAL_MS),\r
+};\r
+\r
+static struct usb_endpoint_descriptor uts_hs_in_desc = {\r
+       .bLength =              USB_DT_ENDPOINT_SIZE,\r
+       .bDescriptorType =      USB_DT_ENDPOINT,\r
+       .bmAttributes =         USB_ENDPOINT_XFER_BULK,\r
+       .wMaxPacketSize =       cpu_to_le16(512),\r
+};\r
+\r
+static struct usb_endpoint_descriptor uts_hs_out_desc = {\r
+       .bLength =              USB_DT_ENDPOINT_SIZE,\r
+       .bDescriptorType =      USB_DT_ENDPOINT,\r
+       .bmAttributes =         USB_ENDPOINT_XFER_BULK,\r
+       .wMaxPacketSize =       cpu_to_le16(512),\r
+};\r
+\r
+static struct usb_descriptor_header *uts_hs_function[] = {\r
+       (struct usb_descriptor_header *) &uts_iad_descriptor,\r
+       (struct usb_descriptor_header *) &uts_control_interface_desc,\r
+       (struct usb_descriptor_header *) &uts_header_desc,\r
+       (struct usb_descriptor_header *) &uts_call_mgmt_descriptor,\r
+       (struct usb_descriptor_header *) &uts_descriptor,\r
+       (struct usb_descriptor_header *) &uts_union_desc,\r
+       (struct usb_descriptor_header *) &uts_hs_notify_desc,\r
+       (struct usb_descriptor_header *) &uts_data_interface_desc,\r
+       (struct usb_descriptor_header *) &uts_hs_in_desc,\r
+       (struct usb_descriptor_header *) &uts_hs_out_desc,\r
+       NULL,\r
+};\r
+\r
+static struct usb_endpoint_descriptor uts_ss_in_desc = {\r
+       .bLength =              USB_DT_ENDPOINT_SIZE,\r
+       .bDescriptorType =      USB_DT_ENDPOINT,\r
+       .bmAttributes =         USB_ENDPOINT_XFER_BULK,\r
+       .wMaxPacketSize =       cpu_to_le16(1024),\r
+};\r
+\r
+static struct usb_endpoint_descriptor uts_ss_out_desc = {\r
+       .bLength =              USB_DT_ENDPOINT_SIZE,\r
+       .bDescriptorType =      USB_DT_ENDPOINT,\r
+       .bmAttributes =         USB_ENDPOINT_XFER_BULK,\r
+       .wMaxPacketSize =       cpu_to_le16(1024),\r
+};\r
+\r
+static struct usb_ss_ep_comp_descriptor uts_ss_bulk_comp_desc = {\r
+       .bLength =              sizeof(uts_ss_bulk_comp_desc),\r
+       .bDescriptorType =      USB_DT_SS_ENDPOINT_COMP,\r
+};\r
+\r
+static struct usb_descriptor_header *uts_ss_function[] = {\r
+       (struct usb_descriptor_header *) &uts_iad_descriptor,\r
+       (struct usb_descriptor_header *) &uts_control_interface_desc,\r
+       (struct usb_descriptor_header *) &uts_header_desc,\r
+       (struct usb_descriptor_header *) &uts_call_mgmt_descriptor,\r
+       (struct usb_descriptor_header *) &uts_descriptor,\r
+       (struct usb_descriptor_header *) &uts_union_desc,\r
+       (struct usb_descriptor_header *) &uts_hs_notify_desc,\r
+       (struct usb_descriptor_header *) &uts_ss_bulk_comp_desc,\r
+       (struct usb_descriptor_header *) &uts_data_interface_desc,\r
+       (struct usb_descriptor_header *) &uts_ss_in_desc,\r
+       (struct usb_descriptor_header *) &uts_ss_bulk_comp_desc,\r
+       (struct usb_descriptor_header *) &uts_ss_out_desc,\r
+       (struct usb_descriptor_header *) &uts_ss_bulk_comp_desc,\r
+       NULL,\r
+};\r
+\r
+/* string descriptors: */\r
+\r
+#define ACM_CTRL_IDX   0\r
+#define ACM_DATA_IDX   1\r
+#define ACM_IAD_IDX    2\r
+\r
+/* static strings, in UTF-8 */\r
+static struct usb_string uts_string_defs[] = {\r
+       [ACM_CTRL_IDX].s = "CDC Abstract Control Model (ACM2)",\r
+       [ACM_DATA_IDX].s = "CDC ACM2 Data",\r
+       [ACM_IAD_IDX].s = "CDC Serial",\r
+       {  } /* end of list */\r
+};\r
+\r
+static struct usb_gadget_strings uts_string_table = {\r
+       .language =             0x0409, /* en-us */\r
+       .strings =              uts_string_defs,\r
+};\r
+\r
+static struct usb_gadget_strings *uts_strings[] = {\r
+       &uts_string_table,\r
+       NULL,\r
+};\r
+\r
+/*-------------------------------------------------------------------------*/\r
+\r
+/* ACM control ... data handling is delegated to tty library code.\r
+ * The main task of this function is to activate and deactivate\r
+ * that code based on device state; track parameters like line\r
+ * speed, handshake state, and so on; and issue notifications.\r
+ */\r
+\r
+static void uts_complete_set_line_coding(struct usb_ep *ep,\r
+               struct usb_request *req)\r
+{\r
+       struct f_uts    *uts = ep->driver_data;\r
+       struct usb_composite_dev *cdev = uts->port.func.config->cdev;\r
+\r
+       if (req->status != 0) {\r
+               dev_dbg(&cdev->gadget->dev, "uts ttyGS%d completion, err %d\n",\r
+                       uts->port_num, req->status);\r
+               return;\r
+       }\r
+\r
+       /* normal completion */\r
+       if (req->actual != sizeof(uts->port_line_coding)) {\r
+               dev_dbg(&cdev->gadget->dev, "uts ttyGS%d short resp, len %d\n",\r
+                       uts->port_num, req->actual);\r
+               usb_ep_set_halt(ep);\r
+       } else {\r
+               struct usb_cdc_line_coding      *value = req->buf;\r
+\r
+               /* REVISIT:  we currently just remember this data.\r
+                * If we change that, (a) validate it first, then\r
+                * (b) update whatever hardware needs updating,\r
+                * (c) worry about locking.  This is information on\r
+                * the order of 9600-8-N-1 ... most of which means\r
+                * nothing unless we control a real RS232 line.\r
+                */\r
+               uts->port_line_coding = *value;\r
+       }\r
+}\r
+\r
+static int uts_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)\r
+{\r
+       struct f_uts            *uts = func_to_uts(f);\r
+       struct usb_composite_dev *cdev = f->config->cdev;\r
+       struct usb_request      *req = cdev->req;\r
+       int                     value = -EOPNOTSUPP;\r
+       u16                     w_index = le16_to_cpu(ctrl->wIndex);\r
+       u16                     w_value = le16_to_cpu(ctrl->wValue);\r
+       u16                     w_length = le16_to_cpu(ctrl->wLength);\r
+\r
+       /* composite driver infrastructure handles everything except\r
+        * CDC class messages; interface activation uses set_alt().\r
+        *\r
+        * Note CDC spec table 4 lists the ACM request profile.  It requires\r
+        * encapsulated command support ... we don't handle any, and respond\r
+        * to them by stalling.  Options include get/set/clear comm features\r
+        * (not that useful) and SEND_BREAK.\r
+        */\r
+       switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {\r
+\r
+       /* SET_LINE_CODING ... just read and save what the host sends */\r
+       case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)\r
+                       | USB_CDC_REQ_SET_LINE_CODING:\r
+               if (w_length != sizeof(struct usb_cdc_line_coding)\r
+                               || w_index != uts->ctrl_id)\r
+                       goto invalid;\r
+\r
+               value = w_length;\r
+               cdev->gadget->ep0->driver_data = uts;\r
+               req->complete = uts_complete_set_line_coding;\r
+               break;\r
+\r
+       /* GET_LINE_CODING ... return what host sent, or initial value */\r
+       case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)\r
+                       | USB_CDC_REQ_GET_LINE_CODING:\r
+               if (w_index != uts->ctrl_id)\r
+                       goto invalid;\r
+\r
+               value = min_t(unsigned int, w_length,\r
+                               sizeof(struct usb_cdc_line_coding));\r
+               memcpy(req->buf, &uts->port_line_coding, value);\r
+               break;\r
+\r
+       /* SET_CONTROL_LINE_STATE ... save what the host sent */\r
+       case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)\r
+                       | USB_CDC_REQ_SET_CONTROL_LINE_STATE:\r
+               if (w_index != uts->ctrl_id)\r
+                       goto invalid;\r
+\r
+               value = 0;\r
+\r
+               /* FIXME we should not allow data to flow until the\r
+                * host sets the ACM_CTRL_DTR bit; and when it clears\r
+                * that bit, we should return to that no-flow state.\r
+                */\r
+               uts->port_handshake_bits = w_value;\r
+               break;\r
+\r
+       default:\r
+invalid:\r
+               dev_vdbg(&cdev->gadget->dev,\r
+                        "invalid control req%02x.%02x v%04x i%04x l%d\n",\r
+                        ctrl->bRequestType, ctrl->bRequest,\r
+                        w_value, w_index, w_length);\r
+       }\r
+\r
+       /* respond with data transfer or status phase? */\r
+       if (value >= 0) {\r
+               dev_dbg(&cdev->gadget->dev,\r
+                       "uts ttyGS%d req%02x.%02x v%04x i%04x l%d\n",\r
+                       uts->port_num, ctrl->bRequestType, ctrl->bRequest,\r
+                       w_value, w_index, w_length);\r
+               req->zero = 0;\r
+               req->length = value;\r
+               value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);\r
+               if (value < 0)\r
+                       ERROR(cdev, "uts response on ttyGS%d, err %d\n",\r
+                                       uts->port_num, value);\r
+       }\r
+\r
+       /* device either stalls (value < 0) or reports success */\r
+       return value;\r
+}\r
+\r
+static int uts_set_alt(struct usb_function *f, unsigned int intf, unsigned int alt)\r
+{\r
+       struct f_uts            *uts = func_to_uts(f);\r
+       struct usb_composite_dev *cdev = f->config->cdev;\r
+\r
+       /* we know alt == 0, so this is an activation or a reset */\r
+\r
+       if (intf == uts->ctrl_id) {\r
+               dev_vdbg(&cdev->gadget->dev,\r
+                               "reset uts control interface %d\n", intf);\r
+               usb_ep_disable(uts->notify);\r
+\r
+               if (!uts->notify->desc)\r
+                       if (config_ep_by_speed(cdev->gadget, f, uts->notify))\r
+                               return -EINVAL;\r
+\r
+               usb_ep_enable(uts->notify);\r
+\r
+       } else if (intf == uts->data_id) {\r
+               if (uts->notify->enabled) {\r
+                       dev_dbg(&cdev->gadget->dev,\r
+                               "reset uts ttyGS%d\n", uts->port_num);\r
+                       gserial_disconnect(&uts->port);\r
+               }\r
+               if (!uts->port.in->desc || !uts->port.out->desc) {\r
+                       dev_dbg(&cdev->gadget->dev,\r
+                               "activate uts ttyGS%d\n", uts->port_num);\r
+                       if (config_ep_by_speed(cdev->gadget, f,\r
+                                              uts->port.in) ||\r
+                           config_ep_by_speed(cdev->gadget, f,\r
+                                              uts->port.out)) {\r
+                               uts->port.in->desc = NULL;\r
+                               uts->port.out->desc = NULL;\r
+                               return -EINVAL;\r
+                       }\r
+               }\r
+               gserial_connect(&uts->port, uts->port_num);\r
+\r
+       } else\r
+               return -EINVAL;\r
+\r
+       return 0;\r
+}\r
+\r
+static void uts_disable(struct usb_function *f)\r
+{\r
+       struct f_uts    *uts = func_to_uts(f);\r
+       struct usb_composite_dev *cdev = f->config->cdev;\r
+\r
+       dev_dbg(&cdev->gadget->dev, "uts ttyGS%d deactivated\n", uts->port_num);\r
+       gserial_disconnect(&uts->port);\r
+       usb_ep_disable(uts->notify);\r
+}\r
+\r
+/*-------------------------------------------------------------------------*/\r
+\r
+/**\r
+ * uts_cdc_notify - issue CDC notification to host\r
+ * @uts: wraps host to be notified\r
+ * @type: notification type\r
+ * @value: Refer to cdc specs, wValue field.\r
+ * @data: data to be sent\r
+ * @length: size of data\r
+ * Context: irqs blocked, uts->lock held, uts_notify_req non-null\r
+ *\r
+ * Returns zero on success or a negative errno.\r
+ *\r
+ * See section 6.3.5 of the CDC 1.1 specification for information\r
+ * about the only notification we issue:  SerialState change.\r
+ */\r
+static int uts_cdc_notify(struct f_uts *uts, u8 type, u16 value,\r
+               void *data, unsigned int length)\r
+{\r
+       struct usb_ep                   *ep = uts->notify;\r
+       struct usb_request              *req;\r
+       struct usb_cdc_notification     *notify;\r
+       const unsigned int              len = sizeof(*notify) + length;\r
+       void                            *buf;\r
+       int                             status;\r
+\r
+       req = uts->notify_req;\r
+       uts->notify_req = NULL;\r
+       uts->pending = false;\r
+\r
+       req->length = len;\r
+       notify = req->buf;\r
+       buf = notify + 1;\r
+\r
+       notify->bmRequestType = USB_DIR_IN | USB_TYPE_CLASS\r
+                       | USB_RECIP_INTERFACE;\r
+       notify->bNotificationType = type;\r
+       notify->wValue = cpu_to_le16(value);\r
+       notify->wIndex = cpu_to_le16(uts->ctrl_id);\r
+       notify->wLength = cpu_to_le16(length);\r
+       memcpy(buf, data, length);\r
+\r
+       /* ep_queue() can complete immediately if it fills the fifo... */\r
+       spin_unlock(&uts->lock);\r
+       status = usb_ep_queue(ep, req, GFP_ATOMIC);\r
+       spin_lock(&uts->lock);\r
+\r
+       if (status < 0) {\r
+               ERROR(uts->port.func.config->cdev,\r
+                               "uts ttyGS%d can't notify serial state, %d\n",\r
+                               uts->port_num, status);\r
+               uts->notify_req = req;\r
+       }\r
+\r
+       return status;\r
+}\r
+\r
+static int uts_notify_serial_state(struct f_uts *uts)\r
+{\r
+       struct usb_composite_dev *cdev = uts->port.func.config->cdev;\r
+       int                     status;\r
+       __le16                  serial_state;\r
+\r
+       spin_lock(&uts->lock);\r
+       if (uts->notify_req) {\r
+               dev_dbg(&cdev->gadget->dev, "uts ttyGS%d serial state %04x\n",\r
+                       uts->port_num, uts->serial_state);\r
+               serial_state = cpu_to_le16(uts->serial_state);\r
+               status = uts_cdc_notify(uts, USB_CDC_NOTIFY_SERIAL_STATE,\r
+                               0, &serial_state, sizeof(uts->serial_state));\r
+       } else {\r
+               uts->pending = true;\r
+               status = 0;\r
+       }\r
+       spin_unlock(&uts->lock);\r
+       return status;\r
+}\r
+\r
+static void uts_cdc_notify_complete(struct usb_ep *ep, struct usb_request *req)\r
+{\r
+       struct f_uts            *uts = req->context;\r
+       u8                      doit = false;\r
+\r
+       /* on this call path we do NOT hold the port spinlock,\r
+        * which is why ACM needs its own spinlock\r
+        */\r
+       spin_lock(&uts->lock);\r
+       if (req->status != -ESHUTDOWN)\r
+               doit = uts->pending;\r
+       uts->notify_req = req;\r
+       spin_unlock(&uts->lock);\r
+\r
+       if (doit)\r
+               uts_notify_serial_state(uts);\r
+}\r
+\r
+/* connect == the TTY link is open */\r
+\r
+static void uts_connect(struct gserial *port)\r
+{\r
+       struct f_uts            *uts = port_to_uts(port);\r
+\r
+       uts->serial_state |= ACM_CTRL_DSR | ACM_CTRL_DCD;\r
+       uts_notify_serial_state(uts);\r
+}\r
+\r
+static void uts_disconnect(struct gserial *port)\r
+{\r
+       struct f_uts            *uts = port_to_uts(port);\r
+\r
+       uts->serial_state &= ~(ACM_CTRL_DSR | ACM_CTRL_DCD);\r
+       uts_notify_serial_state(uts);\r
+}\r
+\r
+static int uts_send_break(struct gserial *port, int duration)\r
+{\r
+       struct f_uts            *uts = port_to_uts(port);\r
+       u16                     state;\r
+\r
+       state = uts->serial_state;\r
+       state &= ~ACM_CTRL_BRK;\r
+       if (duration)\r
+               state |= ACM_CTRL_BRK;\r
+\r
+       uts->serial_state = state;\r
+       return uts_notify_serial_state(uts);\r
+}\r
+\r
+/*-------------------------------------------------------------------------*/\r
+\r
+/* ACM function driver setup/binding */\r
+static int\r
+uts_bind(struct usb_configuration *c, struct usb_function *f)\r
+{\r
+       struct usb_composite_dev *cdev = c->cdev;\r
+       struct f_uts            *uts = func_to_uts(f);\r
+       struct usb_string       *us;\r
+       int                     status;\r
+       struct usb_ep           *ep;\r
+\r
+       /* REVISIT might want instance-specific strings to help\r
+        * distinguish instances ...\r
+        */\r
+\r
+       /* maybe allocate device-global string IDs, and patch descriptors */\r
+       us = usb_gstrings_attach(cdev, uts_strings,\r
+                       ARRAY_SIZE(uts_string_defs));\r
+       if (IS_ERR(us))\r
+               return PTR_ERR(us);\r
+       uts_control_interface_desc.iInterface = us[ACM_CTRL_IDX].id;\r
+       uts_data_interface_desc.iInterface = us[ACM_DATA_IDX].id;\r
+       uts_iad_descriptor.iFunction = us[ACM_IAD_IDX].id;\r
+\r
+       /* allocate instance-specific interface IDs, and patch descriptors */\r
+       status = usb_interface_id(c, f);\r
+       if (status < 0)\r
+               goto fail;\r
+       uts->ctrl_id = status;\r
+       uts_iad_descriptor.bFirstInterface = status;\r
+\r
+       uts_control_interface_desc.bInterfaceNumber = status;\r
+       uts_union_desc .bMasterInterface0 = status;\r
+\r
+       status = usb_interface_id(c, f);\r
+       if (status < 0)\r
+               goto fail;\r
+       uts->data_id = status;\r
+\r
+       uts_data_interface_desc.bInterfaceNumber = status;\r
+       uts_union_desc.bSlaveInterface0 = status;\r
+       uts_call_mgmt_descriptor.bDataInterface = status;\r
+\r
+       status = -ENODEV;\r
+\r
+       /* allocate instance-specific endpoints */\r
+       ep = usb_ep_autoconfig(cdev->gadget, &uts_fs_in_desc);\r
+       if (!ep)\r
+               goto fail;\r
+       uts->port.in = ep;\r
+\r
+       ep = usb_ep_autoconfig(cdev->gadget, &uts_fs_out_desc);\r
+       if (!ep)\r
+               goto fail;\r
+       uts->port.out = ep;\r
+\r
+       ep = usb_ep_autoconfig(cdev->gadget, &uts_fs_notify_desc);\r
+       if (!ep)\r
+               goto fail;\r
+       uts->notify = ep;\r
+\r
+       /* allocate notification */\r
+       uts->notify_req = gs_alloc_req(ep,\r
+                       sizeof(struct usb_cdc_notification) + 2,\r
+                       GFP_KERNEL);\r
+       if (!uts->notify_req)\r
+               goto fail;\r
+\r
+       uts->notify_req->complete = uts_cdc_notify_complete;\r
+       uts->notify_req->context = uts;\r
+\r
+       /* support all relevant hardware speeds... we expect that when\r
+        * hardware is dual speed, all bulk-capable endpoints work at\r
+        * both speeds\r
+        */\r
+       uts_hs_in_desc.bEndpointAddress = uts_fs_in_desc.bEndpointAddress;\r
+       uts_hs_out_desc.bEndpointAddress = uts_fs_out_desc.bEndpointAddress;\r
+       uts_hs_notify_desc.bEndpointAddress =\r
+               uts_fs_notify_desc.bEndpointAddress;\r
+\r
+       uts_ss_in_desc.bEndpointAddress = uts_fs_in_desc.bEndpointAddress;\r
+       uts_ss_out_desc.bEndpointAddress = uts_fs_out_desc.bEndpointAddress;\r
+\r
+       status = usb_assign_descriptors(f, uts_fs_function, uts_hs_function,\r
+                       uts_ss_function, NULL);\r
+       if (status)\r
+               goto fail;\r
+\r
+       dev_dbg(&cdev->gadget->dev,\r
+               "uts ttyGS%d: %s speed IN/%s OUT/%s NOTIFY/%s\n",\r
+               uts->port_num,\r
+               gadget_is_superspeed(c->cdev->gadget) ? "super" :\r
+               gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",\r
+               uts->port.in->name, uts->port.out->name,\r
+               uts->notify->name);\r
+       return 0;\r
+\r
+fail:\r
+       if (uts->notify_req)\r
+               gs_free_req(uts->notify, uts->notify_req);\r
+\r
+       ERROR(cdev, "%s/%p: can't bind, err %d\n", f->name, f, status);\r
+\r
+       return status;\r
+}\r
+\r
+static void uts_unbind(struct usb_configuration *c, struct usb_function *f)\r
+{\r
+       struct f_uts            *uts = func_to_uts(f);\r
+\r
+       uts_string_defs[0].id = 0;\r
+       usb_free_all_descriptors(f);\r
+       if (uts->notify_req)\r
+               gs_free_req(uts->notify, uts->notify_req);\r
+}\r
+\r
+static void uts_free_func(struct usb_function *f)\r
+{\r
+       struct f_uts            *uts = func_to_uts(f);\r
+\r
+       kfree(uts);\r
+}\r
+\r
+static struct usb_function *uts_alloc_func(struct usb_function_instance *fi)\r
+{\r
+       struct f_serial_opts *opts;\r
+       struct f_uts *uts;\r
+\r
+       uts = kzalloc(sizeof(*uts), GFP_KERNEL);\r
+       if (!uts)\r
+               return ERR_PTR(-ENOMEM);\r
+\r
+       spin_lock_init(&uts->lock);\r
+\r
+       uts->port.connect = uts_connect;\r
+       uts->port.disconnect = uts_disconnect;\r
+       uts->port.send_break = uts_send_break;\r
+\r
+       uts->port.func.name = "uts";\r
+       uts->port.func.strings = uts_strings;\r
+       /* descriptors are per-instance copies */\r
+       uts->port.func.bind = uts_bind;\r
+       uts->port.func.set_alt = uts_set_alt;\r
+       uts->port.func.setup = uts_setup;\r
+       uts->port.func.disable = uts_disable;\r
+\r
+       opts = container_of(fi, struct f_serial_opts, func_inst);\r
+       uts->port_num = opts->port_num;\r
+       uts->port.func.unbind = uts_unbind;\r
+       uts->port.func.free_func = uts_free_func;\r
+\r
+       return &uts->port.func;\r
+}\r
+\r
+static inline struct f_serial_opts *to_f_serial_opts(struct config_item *item)\r
+{\r
+       return container_of(to_config_group(item), struct f_serial_opts,\r
+                       func_inst.group);\r
+}\r
+\r
+static void uts_attr_release(struct config_item *item)\r
+{\r
+       struct f_serial_opts *opts = to_f_serial_opts(item);\r
+\r
+       usb_put_function_instance(&opts->func_inst);\r
+}\r
+\r
+static struct configfs_item_operations uts_item_ops = {\r
+       .release                = uts_attr_release,\r
+};\r
+\r
+static ssize_t f_uts_port_num_show(struct config_item *item, char *page)\r
+{\r
+       return sprintf(page, "%u\n", to_f_serial_opts(item)->port_num);\r
+}\r
+\r
+CONFIGFS_ATTR_RO(f_uts_, port_num);\r
+\r
+static struct configfs_attribute *uts_attrs[] = {\r
+       &f_uts_attr_port_num,\r
+       NULL,\r
+};\r
+\r
+static struct config_item_type uts_func_type = {\r
+       .ct_item_ops    = &uts_item_ops,\r
+       .ct_attrs       = uts_attrs,\r
+       .ct_owner       = THIS_MODULE,\r
+};\r
+\r
+static void uts_free_instance(struct usb_function_instance *fi)\r
+{\r
+       struct f_serial_opts *opts;\r
+\r
+       opts = container_of(fi, struct f_serial_opts, func_inst);\r
+       gserial_free_line(opts->port_num);\r
+       kfree(opts);\r
+}\r
+\r
+static struct usb_function_instance *uts_alloc_instance(void)\r
+{\r
+       struct f_serial_opts *opts;\r
+       int ret;\r
+\r
+       opts = kzalloc(sizeof(*opts), GFP_KERNEL);\r
+       if (!opts)\r
+               return ERR_PTR(-ENOMEM);\r
+       opts->func_inst.free_func_inst = uts_free_instance;\r
+       opts->port_num = UTS_PORT_NUM;\r
+       ret = gserial_alloc_line(&opts->port_num);\r
+       if (ret) {\r
+               kfree(opts);\r
+               return ERR_PTR(ret);\r
+       }\r
+       config_group_init_type_name(&opts->func_inst.group, "",\r
+                       &uts_func_type);\r
+       return &opts->func_inst;\r
+}\r
+DECLARE_USB_FUNCTION_INIT(uts, uts_alloc_instance, uts_alloc_func);\r
+MODULE_LICENSE("GPL");\r
diff --git a/include/linux/f_mtp.h b/include/linux/f_mtp.h
new file mode 100644 (file)
index 0000000..4e84177
--- /dev/null
@@ -0,0 +1,23 @@
+/*
+ * Gadget Function Driver for MTP
+ *
+ * Copyright (C) 2010 Google, Inc.
+ * Author: Mike Lockwood <lockwood@android.com>
+ *
+ * This software is licensed under the terms of the GNU General Public
+ * License version 2, as published by the Free Software Foundation, and
+ * may be copied, distributed, and modified under those terms.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ */
+
+#ifndef __LINUX_USB_F_MTP_H
+#define __LINUX_USB_F_MTP_H
+
+#include <uapi/linux/usb/f_mtp.h>
+
+#endif /* __LINUX_USB_F_MTP_H */
diff --git a/include/linux/usb/f_mtp.h b/include/linux/usb/f_mtp.h
new file mode 100644 (file)
index 0000000..4e84177
--- /dev/null
@@ -0,0 +1,23 @@
+/*
+ * Gadget Function Driver for MTP
+ *
+ * Copyright (C) 2010 Google, Inc.
+ * Author: Mike Lockwood <lockwood@android.com>
+ *
+ * This software is licensed under the terms of the GNU General Public
+ * License version 2, as published by the Free Software Foundation, and
+ * may be copied, distributed, and modified under those terms.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ */
+
+#ifndef __LINUX_USB_F_MTP_H
+#define __LINUX_USB_F_MTP_H
+
+#include <uapi/linux/usb/f_mtp.h>
+
+#endif /* __LINUX_USB_F_MTP_H */
index a0a8f878503c959442608d09e1f0495a6319e03a..a893e4b51155a8f961df080ffbb0f2ac3583ff94 100644 (file)
@@ -134,6 +134,8 @@ enum otg_fsm_timer {
  *             to a_wait_vfall
  */
 struct otg_fsm {
+       int reset;
+
        /* Input */
        int id;
        int adp_change;
diff --git a/include/uapi/linux/usb/f_mtp.h b/include/uapi/linux/usb/f_mtp.h
new file mode 100644 (file)
index 0000000..5032918
--- /dev/null
@@ -0,0 +1,61 @@
+/*
+ * Gadget Function Driver for MTP
+ *
+ * Copyright (C) 2010 Google, Inc.
+ * Author: Mike Lockwood <lockwood@android.com>
+ *
+ * This software is licensed under the terms of the GNU General Public
+ * License version 2, as published by the Free Software Foundation, and
+ * may be copied, distributed, and modified under those terms.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ */
+
+#ifndef _UAPI_LINUX_USB_F_MTP_H
+#define _UAPI_LINUX_USB_F_MTP_H
+
+#include <linux/ioctl.h>
+#include <linux/types.h>
+
+struct mtp_file_range {
+       /* file descriptor for file to transfer */
+       int                     fd;
+       /* offset in file for start of transfer */
+       loff_t          offset;
+       /* number of bytes to transfer */
+       int64_t         length;
+       /* MTP command ID for data header,
+        * used only for MTP_SEND_FILE_WITH_HEADER
+        */
+       uint16_t        command;
+       /* MTP transaction ID for data header,
+        * used only for MTP_SEND_FILE_WITH_HEADER
+        */
+       uint32_t        transaction_id;
+};
+
+struct mtp_event {
+       /* size of the event */
+       size_t          length;
+       /* event data to send */
+       void            *data;
+};
+
+/* Sends the specified file range to the host */
+#define MTP_SEND_FILE              _IOW('M', 0, struct mtp_file_range)
+/* Receives data from the host and writes it to a file.
+ * The file is created if it does not exist.
+ */
+#define MTP_RECEIVE_FILE           _IOW('M', 1, struct mtp_file_range)
+/* Sends an event to the host via the interrupt endpoint */
+#define MTP_SEND_EVENT             _IOW('M', 3, struct mtp_event)
+/* Sends the specified file range to the host,
+ * with a 12 byte MTP data packet header at the beginning.
+ */
+#define MTP_SEND_FILE_WITH_HEADER  _IOW('M', 4, struct mtp_file_range)
+
+#endif /* _UAPI_LINUX_USB_F_MTP_H */