2 * drivers/usb/gadget/f_mtp_slp.c
4 * Function Driver for USB MTP,
5 * mtpg.c -- MTP Driver, for MTP development,
7 * Copyright (C) 2009 by Samsung Electronics,
8 * Author:Deepak M.G. <deepak.guru@samsung.com>,
9 * Author:Madhukar.J <madhukar.j@samsung.com>,
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
23 * f_mtp.c file is the driver for MTP device. Totally three
24 * EndPoints will be configured in which 2 Bulk End Points
25 * and 1 Interrupt End point. This driver will also register as
26 * misc driver and exposes file operation funtions to user space.
30 #include <linux/module.h>
31 #include <linux/init.h>
32 #include <linux/poll.h>
33 #include <linux/delay.h>
34 #include <linux/wait.h>
35 #include <linux/err.h>
36 #include <linux/interrupt.h>
37 #include <linux/types.h>
38 #include <linux/device.h>
39 #include <linux/miscdevice.h>
40 #include <linux/kernel.h>
41 #include <linux/kref.h>
42 #include <linux/spinlock.h>
43 #include <linux/string.h>
44 #include <linux/usb.h>
45 #include <linux/usb_usual.h>
46 #include <linux/usb/ch9.h>
47 #include <linux/usb/composite.h>
48 #include <linux/usb/gadget.h>
49 #include <linux/ioctl.h>
50 #include <linux/printk.h>
52 #include <linux/sched.h>
53 #include <asm-generic/siginfo.h>
55 #include "gadget_chips.h"
57 /*-------------------------------------------------------------------------*/
63 #define DEBUG_MTP_SETUP
64 #define DEBUG_MTP_READ
65 #define DEBUG_MTP_WRITE
68 #undef DEBUG_MTP_SETUP
70 #undef DEBUG_MTP_WRITE
73 #ifdef DEBUG_MTP_SETUP
74 #define DEBUG_MTPB(fmt, args...) printk(KERN_INFO fmt, ##args)
76 #define DEBUG_MTPB(fmt, args...) do {} while (0)
80 #define DEBUG_MTPR(fmt, args...) printk(KERN_INFO fmt, ##args)
82 #define DEBUG_MTPR(fmt, args...) do {} while (0)
85 #ifdef DEBUG_MTP_WRITE
86 #define DEBUG_MTPW(fmt, args...) printk(KERN_INFO fmt, ##args)
88 #define DEBUG_MTPW(fmt, args...) do {} while (0)
90 /*-------------------------------------------------------------------------*/
92 #define MTP_BULK_BUFFER_SIZE 32768
93 #define MTP_INTR_BUFFER_SIZE 22
95 /* number of rx and tx requests to allocate */
96 #define MTP_RX_REQ_MAX 4
97 #define MTP_TX_REQ_MAX 4
98 #define MTP_INT_REQ_MAX 2
100 #define MTP_DRIVER_NAME "usb_mtp_gadget"
102 #define MTP_IOCTL_LETTER 'Z'
103 #define GET_HIGH_FULL_SPEED _IOR(MTP_IOCTL_LETTER, 1, int)
104 #define MTP_DISABLE _IO(MTP_IOCTL_LETTER, 2)
105 #define MTP_CLEAR_HALT _IO(MTP_IOCTL_LETTER, 3)
106 #define MTP_WRITE_INT_DATA _IOW(MTP_IOCTL_LETTER, 4, char *)
107 #define SET_MTP_USER_PID _IOW(MTP_IOCTL_LETTER, 5, int)
108 #define GET_SETUP_DATA _IOR(MTP_IOCTL_LETTER, 6, char *)
109 #define SET_SETUP_DATA _IOW(MTP_IOCTL_LETTER, 7, char *)
110 #define SEND_RESET_ACK _IO(MTP_IOCTL_LETTER, 8)
111 #define SET_ZLP_DATA _IO(MTP_IOCTL_LETTER, 9)
114 /*PIMA15740-2000 spec*/
115 #define USB_PTPREQUEST_CANCELIO 0x64 /* Cancel request */
116 #define USB_PTPREQUEST_GETEVENT 0x65 /* Get extened event data */
117 #define USB_PTPREQUEST_RESET 0x66 /* Reset Device */
118 #define USB_PTPREQUEST_GETSTATUS 0x67 /* Get Device Status */
119 #define USB_PTPREQUEST_CANCELIO_SIZE 6
120 #define USB_PTPREQUEST_GETSTATUS_SIZE 12
122 static const char mtp_longname[] = "mtp";
124 static DEFINE_MUTEX(mtp_lock);
125 static const char mtp_shortname[] = MTP_DRIVER_NAME;
126 static pid_t mtp_pid;
128 struct mtp_ep_descs {
129 struct usb_endpoint_descriptor *bulk_in;
130 struct usb_endpoint_descriptor *bulk_out;
131 struct usb_endpoint_descriptor *int_in;
135 struct usb_function function;
136 unsigned allocated_intf_num;
138 struct mtp_ep_descs fs;
139 struct mtp_ep_descs hs;
141 struct usb_ep *bulk_in;
142 struct usb_ep *bulk_out;
143 struct usb_ep *int_in;
145 struct list_head bulk_in_q;
146 struct list_head bulk_out_q;
147 struct list_head int_in_q;
150 /* MTP Device Structure*/
152 struct f_mtp *mtp_func;
158 wait_queue_head_t read_wq;
159 wait_queue_head_t write_wq;
160 wait_queue_head_t intr_wq;
162 struct usb_request *read_req;
163 unsigned char *read_buf;
170 atomic_t wintfd_excl;
172 struct list_head *int_idle;
173 struct list_head *tx_idle;
174 struct list_head *rx_idle;
175 struct list_head rx_done;
177 char cancel_io_buf[USB_PTPREQUEST_CANCELIO_SIZE + 1];
180 struct usb_mtp_ctrlrequest {
181 struct usb_ctrlrequest setup;
184 static inline struct f_mtp *func_to_mtp(struct usb_function *f)
186 return container_of(f, struct f_mtp, function);
189 /* Global mtpg_dev Structure
190 * the_mtpg variable be used between mtpg_open() and mtpg_function_bind() */
191 static struct mtpg_dev *the_mtpg;
193 /* Three full-speed and high-speed endpoint descriptors: bulk-in, bulk-out,
194 * and interrupt-in. */
196 static struct usb_interface_descriptor mtpg_interface_desc = {
197 .bLength = sizeof mtpg_interface_desc,
198 .bDescriptorType = USB_DT_INTERFACE,
200 .bInterfaceClass = USB_CLASS_STILL_IMAGE,
201 .bInterfaceSubClass = 01,
202 .bInterfaceProtocol = 01,
205 static struct usb_endpoint_descriptor fs_mtpg_in_desc = {
206 .bLength = USB_DT_ENDPOINT_SIZE,
207 .bDescriptorType = USB_DT_ENDPOINT,
208 .bEndpointAddress = USB_DIR_IN,
209 .bmAttributes = USB_ENDPOINT_XFER_BULK,
210 /* wMaxPacketSize set by autoconfiguration */
213 static struct usb_endpoint_descriptor fs_mtpg_out_desc = {
214 .bLength = USB_DT_ENDPOINT_SIZE,
215 .bDescriptorType = USB_DT_ENDPOINT,
216 .bEndpointAddress = USB_DIR_OUT,
217 .bmAttributes = USB_ENDPOINT_XFER_BULK,
218 /* wMaxPacketSize set by autoconfiguration */
221 static struct usb_endpoint_descriptor int_fs_notify_desc = {
222 .bLength = USB_DT_ENDPOINT_SIZE,
223 .bDescriptorType = USB_DT_ENDPOINT,
224 .bEndpointAddress = USB_DIR_IN,
225 .bmAttributes = USB_ENDPOINT_XFER_INT,
226 .wMaxPacketSize = __constant_cpu_to_le16(64),
230 static struct usb_descriptor_header *fs_mtpg_desc[] = {
231 (struct usb_descriptor_header *)&mtpg_interface_desc,
232 (struct usb_descriptor_header *)&fs_mtpg_in_desc,
233 (struct usb_descriptor_header *)&fs_mtpg_out_desc,
234 (struct usb_descriptor_header *)&int_fs_notify_desc,
238 static struct usb_endpoint_descriptor hs_mtpg_in_desc = {
239 .bLength = USB_DT_ENDPOINT_SIZE,
240 .bDescriptorType = USB_DT_ENDPOINT,
241 /* bEndpointAddress = DYNAMIC */
242 .bmAttributes = USB_ENDPOINT_XFER_BULK,
243 .wMaxPacketSize = __constant_cpu_to_le16(512),
246 static struct usb_endpoint_descriptor hs_mtpg_out_desc = {
247 .bLength = USB_DT_ENDPOINT_SIZE,
248 .bDescriptorType = USB_DT_ENDPOINT,
249 /* bEndpointAddress = DYNAMIC */
250 .bmAttributes = USB_ENDPOINT_XFER_BULK,
251 .wMaxPacketSize = __constant_cpu_to_le16(512),
254 static struct usb_endpoint_descriptor int_hs_notify_desc = {
255 .bLength = USB_DT_ENDPOINT_SIZE,
256 .bDescriptorType = USB_DT_ENDPOINT,
257 .bEndpointAddress = USB_DIR_IN,
258 .bmAttributes = USB_ENDPOINT_XFER_INT,
259 .wMaxPacketSize = __constant_cpu_to_le16(64),
263 static struct usb_descriptor_header *hs_mtpg_desc[] = {
264 (struct usb_descriptor_header *)&mtpg_interface_desc,
265 (struct usb_descriptor_header *)&hs_mtpg_in_desc,
266 (struct usb_descriptor_header *)&hs_mtpg_out_desc,
267 (struct usb_descriptor_header *)&int_hs_notify_desc,
271 /* string IDs are assigned dynamically */
273 #define MTP_STRING_PRODUCT_IDX 1
274 #define MTP_STRING_SERIAL_IDX 2
276 /* default mtp_serial number takes at least two packets */
277 static char mtp_serial[36] = "0123456789.0123456789.0123456789";
279 static struct usb_string strings_dev_mtp[] = {
280 [F_MTP_IDX].s = "Samsung MTP",
281 [MTP_STRING_PRODUCT_IDX].s = mtp_longname,
282 [MTP_STRING_SERIAL_IDX].s = mtp_serial,
283 {}, /* end of list */
286 static struct usb_gadget_strings stringtab_mtp = {
287 .language = 0x0409, /* en-us */
288 .strings = strings_dev_mtp,
291 static struct usb_gadget_strings *mtp_dev_strings[] = {
296 /* -------------------------------------------------------------------------
297 * Main Functionalities Start!
298 * ------------------------------------------------------------------------- */
300 static inline int _mtp_lock(atomic_t *excl)
302 DEBUG_MTPB("[%s] \tline = [%d]\n", __func__, __LINE__);
303 if (atomic_inc_return(excl) == 1) {
311 static inline void _mtp_unlock(atomic_t *excl)
316 /* add a request to the tail of a list */
317 static inline void mtp_req_put(struct mtpg_dev *dev, struct list_head *head,
318 struct usb_request *req)
325 DEBUG_MTPB("[%s] \tline = [%d]\n", __func__, __LINE__);
326 spin_lock_irqsave(&dev->lock, flags);
328 list_add_tail(&req->list, head);
329 spin_unlock_irqrestore(&dev->lock, flags);
332 /* remove a request from the head of a list */
333 static struct usb_request *mtp_req_get(struct mtpg_dev *dev,
334 struct list_head *head)
337 struct usb_request *req;
342 DEBUG_MTPB("[%s] \tline = [%d]\n", __func__, __LINE__);
344 spin_lock_irqsave(&dev->lock, flags);
348 if (list_empty(head)) {
351 req = list_first_entry(head, struct usb_request, list);
352 list_del(&req->list);
355 spin_unlock_irqrestore(&dev->lock, flags);
360 static int mtp_send_signal(int value)
364 struct task_struct *t;
365 memset(&info, 0, sizeof(struct siginfo));
366 info.si_signo = SIG_SETUP;
367 info.si_code = SI_QUEUE;
370 t = find_task_by_vpid(mtp_pid);
372 pr_err("no such pid\n");
378 ret = send_sig_info(SIG_SETUP, &info, t);
380 pr_err("error sending signal !!!!!!!!\n");
387 static int mtpg_open(struct inode *ip, struct file *fp)
389 DEBUG_MTPB("[%s] \tline = [%d]\n", __func__, __LINE__);
391 if (_mtp_lock(&the_mtpg->open_excl)) {
392 pr_err("mtpg_open fn -- mtpg device busy\n");
396 fp->private_data = the_mtpg;
398 /* clear the error latch */
400 DEBUG_MTPB("[%s] mtpg_open and clearing the error = 0\n", __func__);
407 static ssize_t mtpg_read(struct file *fp, char __user *buf,
408 size_t count, loff_t *pos)
410 struct mtpg_dev *dev = fp->private_data;
411 struct usb_request *req;
415 DEBUG_MTPR("*******[%s] and count = (%d)\n", __func__, count);
417 if (_mtp_lock(&dev->read_excl)) {
418 pr_err("mtpg_read fn -- mtpg device busy\n");
422 while (!(dev->online || dev->error)) {
423 DEBUG_MTPR("******[%s] and line is = %d\n",
425 ret = wait_event_interruptible(dev->read_wq,
426 (dev->online || dev->error));
428 _mtp_unlock(&dev->read_excl);
434 DEBUG_MTPR("*********[%s] and line is = %d\n",
439 pr_err("*******[%s]\t%d: dev->error so break r=%d\n",
440 __func__, __LINE__, r);
444 /* if we have idle read requests, get them queued */
445 while (!!(req = mtp_req_get(dev, dev->rx_idle))) {
447 mutex_lock(&mtp_lock);
451 req->length = MTP_BULK_BUFFER_SIZE;
452 ret = usb_ep_queue(dev->mtp_func->bulk_out,
455 mutex_unlock(&mtp_lock);
460 mtp_req_put(dev, dev->rx_idle, req);
463 DEBUG_MTPR("********* [%s] rx req queue %p\n",
468 DEBUG_MTPR("*******[%s]\t%d: read_count = %d\n", __func__,
469 __LINE__, dev->read_count);
471 /* if we have data pending, give it to userspace */
472 if (dev->read_count > 0) {
473 DEBUG_MTPR("*******[%s]\t%d: read_count = %d\n",
474 __func__, __LINE__, dev->read_count);
475 if (dev->read_count < count)
476 xfer = dev->read_count;
480 mutex_lock(&mtp_lock);
481 if (!dev->mtp_func) {
482 mutex_unlock(&mtp_lock);
485 } else if (copy_to_user(buf, dev->read_buf, xfer)) {
486 mutex_unlock(&mtp_lock);
490 mutex_unlock(&mtp_lock);
492 dev->read_buf += xfer;
493 dev->read_count -= xfer;
497 /* if we've emptied the buffer, release the request */
498 if (dev->read_count == 0) {
499 DEBUG_MTPR("******[%s] and line is = %d\n",
501 mtp_req_put(dev, dev->rx_idle, dev->read_req);
502 dev->read_req = NULL;
506 DEBUG_MTPR("***** [%s] \t %d: returning lenght %d\n",
507 __func__, __LINE__, r);
511 /* wait for a request to complete */
513 ret = wait_event_interruptible(dev->read_wq,
514 (!!(req = mtp_req_get(dev, &dev->rx_done))
517 DEBUG_MTPR("*******[%s]\t%d: dev->error %d and req = %p\n",
518 __func__, __LINE__, dev->error, req);
521 /* if we got a 0-len one we need to put it back into
522 * service. if we made it the current read req we'd
525 if (req->actual == 0)
529 dev->read_count = req->actual;
530 dev->read_buf = req->buf;
540 _mtp_unlock(&dev->read_excl);
544 static ssize_t mtpg_write(struct file *fp, const char __user *buf,
545 size_t count, loff_t *pos)
547 struct mtpg_dev *dev = fp->private_data;
548 struct usb_request *req = 0;
552 if (_mtp_lock(&dev->write_excl))
561 /* get an idle tx request to use */
564 ret = wait_event_interruptible(dev->write_wq,
565 (!!(req = mtp_req_get(dev, dev->tx_idle))
568 DEBUG_MTPW("[%s]\t%d: count : %d, dev->error = %d\n",
569 __func__, __LINE__, count, dev->error);
577 if (count > MTP_BULK_BUFFER_SIZE)
578 xfer = MTP_BULK_BUFFER_SIZE;
582 DEBUG_MTPW("***** [%s]\t%d copy_from_user length %d\n",
583 __func__, __LINE__, xfer);
585 mutex_lock(&mtp_lock);
586 if (!dev->mtp_func) {
587 mutex_unlock(&mtp_lock);
590 } else if (copy_from_user(req->buf, buf, xfer)) {
591 mutex_unlock(&mtp_lock);
597 ret = usb_ep_queue(dev->mtp_func->bulk_in,
599 mutex_unlock(&mtp_lock);
610 /* zero this so we don't try to free it on error exit */
616 DEBUG_MTPW("[%s] \t%d mtp_req_put\n", __func__, __LINE__);
617 mtp_req_put(dev, dev->tx_idle, req);
620 _mtp_unlock(&dev->write_excl);
624 /*Fixme for Interrupt Transfer*/
625 static void interrupt_complete(struct usb_ep *ep, struct usb_request *req)
627 struct mtpg_dev *dev = the_mtpg;
628 struct f_mtp *mtp_func = ep->driver_data;
630 DEBUG_MTPB("[%s] \t %d req->status is = %d\n",
631 __func__, __LINE__, req->status);
633 if (req->status != 0)
636 mtp_req_put(dev, &mtp_func->int_in_q, req);
637 wake_up(&dev->intr_wq);
640 /*interrupt_write() is special write function for fixed size*/
641 static int interrupt_write(struct mtpg_dev *dev, const char __user *buf)
643 struct usb_request *req = NULL;
644 int count = MTP_INTR_BUFFER_SIZE;
647 DEBUG_MTPB("[%s] \tline = [%d]\n", __func__, __LINE__);
649 if (_mtp_lock(&dev->wintfd_excl)) {
650 pr_err("wintfd_excl lock failed for interrupt_write\n");
654 /* get an idle tx request to use */
655 ret = wait_event_interruptible(dev->intr_wq,
656 (!!(req = mtp_req_get(dev, dev->int_idle))
659 DEBUG_MTPW("[%s]\t%d: count : %d, dev->error = %d\n",
660 __func__, __LINE__, count, dev->error);
663 pr_err("[%s]%d wait_event return fail\n",
665 goto interrupt_write_fail;
666 } else if (dev->error) {
668 pr_err("[%s]%d dev->error so brk\n",
670 goto interrupt_write_fail;
673 /* we've got a free requtest from int_idle_queue*/
674 DEBUG_MTPW("***** [%s]\t%d copy_from_user length %d\n",
675 __func__, __LINE__, count);
677 mutex_lock(&mtp_lock);
678 if (!dev->mtp_func) {
679 mutex_unlock(&mtp_lock);
681 goto interrupt_write_fail;
682 } else if (copy_from_user(req->buf, buf, count)) {
683 mutex_unlock(&mtp_lock);
685 goto interrupt_write_fail;
689 ret = usb_ep_queue(dev->mtp_func->int_in,
691 mutex_unlock(&mtp_lock);
698 interrupt_write_fail:
700 mtp_req_put(dev, dev->int_idle, req);
701 _mtp_unlock(&dev->wintfd_excl);
706 /*Fixme for enabling and disabling the MTP*/
707 static long mtpg_ioctl(struct file *fd, unsigned int code, unsigned long arg)
710 struct mtpg_dev *dev = fd->private_data;
711 struct usb_composite_dev *cdev;
712 struct usb_request *req;
713 struct usb_ep *bulk_in;
714 struct usb_ep *bulk_out;
717 void __user *ubuf = (void __user *)arg;
718 char buf[USB_PTPREQUEST_GETSTATUS_SIZE + 1] = { 0 };
720 DEBUG_MTPB("[%s] with cmd:[%04x]\n", __func__, code);
722 if (code == SET_MTP_USER_PID) {
723 if (copy_from_user(&mtp_pid, ubuf, sizeof(mtp_pid)))
726 pr_info("[%s] SET_MTP_USER_PID; pid = %d \tline = [%d]\n",
727 __func__, mtp_pid, __LINE__);
729 } else if (code == MTP_WRITE_INT_DATA) {
730 return interrupt_write(dev, ubuf);
733 mutex_lock(&mtp_lock);
734 if (!dev->mtp_func) {
735 pr_info("mtpg_ioctl fail, usb not yet enabled for MTP\n");
736 mutex_unlock(&mtp_lock);
739 cdev = dev->mtp_func->function.config->cdev;
741 bulk_in = dev->mtp_func->bulk_in;
742 bulk_out = dev->mtp_func->bulk_out;
746 if (cdev && cdev->gadget) {
747 pr_info("mtpg_ioctl for MTP_DISABLE, usb_gadget_dicon/connect!!\n");
748 usb_gadget_disconnect(cdev->gadget);
750 usb_gadget_connect(cdev->gadget);
755 status = usb_ep_clear_halt(bulk_in);
757 status = usb_ep_clear_halt(bulk_out);
761 if (copy_to_user(ubuf, dev->cancel_io_buf,
762 USB_PTPREQUEST_CANCELIO_SIZE))
768 status = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
770 DEBUG_MTPB("[%s] Error at usb_ep_queue\tline = [%d]\n",
775 if (copy_from_user(buf, ubuf,
776 USB_PTPREQUEST_GETSTATUS_SIZE)) {
778 DEBUG_MTPR("*****[%s]\t%d: copy-from-user failed!!!!\n",
783 DEBUG_MTPB("[SET_SETUP_DATA]check data(%d):%x, %x, %x, %x\n",
784 size, buf[0], buf[1], buf[2], buf[3]);
785 memcpy(req->buf, buf, size);
788 status = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
790 DEBUG_MTPB("[%s] Error at usb_ep_queue\tline = [%d]\n",
795 status = wait_event_interruptible(dev->write_wq,
796 (!!(req = mtp_req_get(dev, dev->tx_idle))
798 if (status < 0 || dev->error == 1) {
799 pr_err("***** [%s]\t%d status = %d !!!!!\n",
800 __func__, __LINE__, status);
804 status = usb_ep_queue(bulk_in, req, GFP_ATOMIC);
806 pr_err("[%s] Error at usb_ep_queue\tline = [%d]\n",
810 case GET_HIGH_FULL_SPEED:
811 if (!bulk_in->driver_data) {
812 pr_info("USB speed does not negotiate with host\n");
815 int maxpacket = bulk_in->maxpacket;
816 if (copy_to_user(ubuf, &maxpacket, sizeof(int)))
824 mutex_unlock(&mtp_lock);
828 static int mtpg_release_device(struct inode *ip, struct file *fp)
830 DEBUG_MTPB("[%s] \tline = [%d]\n", __func__, __LINE__);
832 if (the_mtpg != NULL)
833 _mtp_unlock(&the_mtpg->open_excl);
838 /* file operations for MTP device /dev/usb_mtp_gadget */
839 static const struct file_operations mtpg_fops = {
840 .owner = THIS_MODULE,
844 .unlocked_ioctl = mtpg_ioctl,
845 .release = mtpg_release_device,
848 static struct miscdevice mtpg_device = {
849 .minor = MISC_DYNAMIC_MINOR,
850 .name = mtp_shortname,
854 static void mtpg_request_free(struct usb_request *req, struct usb_ep *ep)
857 DEBUG_MTPB("[%s] \tline = [%d]\n", __func__, __LINE__);
861 usb_ep_free_request(ep, req);
865 static struct usb_request *mtpg_request_new(struct usb_ep *ep, int buffer_size)
868 struct usb_request *req = usb_ep_alloc_request(ep, GFP_KERNEL);
870 DEBUG_MTPB("[%s] \tline = [%d]\n", __func__, __LINE__);
873 pr_err("******* %s \t line %d ERROR !!!\n",
878 /* now allocate buffers for the requests */
879 req->buf = kmalloc(buffer_size, GFP_KERNEL);
881 usb_ep_free_request(ep, req);
888 static void mtpg_complete_in(struct usb_ep *ep, struct usb_request *req)
890 struct mtpg_dev *dev = the_mtpg;
891 struct f_mtp *mtp_func = ep->driver_data;
893 DEBUG_MTPB("[%s] \t %d req->status is = %d\n",
894 __func__, __LINE__, req->status);
896 if (req->status != 0)
899 mtp_req_put(dev, &mtp_func->bulk_in_q, req);
900 wake_up(&dev->write_wq);
903 static void mtpg_complete_out(struct usb_ep *ep, struct usb_request *req)
905 struct mtpg_dev *dev = the_mtpg;
906 struct f_mtp *mtp_func = ep->driver_data;
908 DEBUG_MTPB("[%s] \tline = [%d]req->status is = %d\n",
909 __func__, __LINE__, req->status);
910 if (req->status != 0) {
913 DEBUG_MTPB(" [%s] \t %d dev->error is = %d for rx_idle\n",
914 __func__, __LINE__, dev->error);
915 mtp_req_put(dev, &mtp_func->bulk_out_q, req);
917 DEBUG_MTPB("[%s] \t %d for rx_done\n", __func__, __LINE__);
918 mtp_req_put(dev, &dev->rx_done, req);
920 wake_up(&dev->read_wq);
923 /* mtp_create_bulk_endpoints() must be used after function binded */
924 static int mtp_create_bulk_endpoints(struct f_mtp *mtp_func,
925 struct usb_endpoint_descriptor *in_desc,
926 struct usb_endpoint_descriptor *out_desc,
927 struct usb_endpoint_descriptor *intr_desc)
929 struct usb_composite_dev *cdev = mtp_func->function.config->cdev;
930 struct usb_request *req;
931 struct mtpg_dev *dev = the_mtpg;
935 ep = usb_ep_autoconfig(cdev->gadget, in_desc);
937 pr_err("Error in usb_ep_autoconfig for IN DESC Failed !!!!!!!!!!\n");
940 ep->driver_data = cdev; /* claim the endpoint */
941 mtp_func->bulk_in = ep;
943 ep = usb_ep_autoconfig(cdev->gadget, out_desc);
945 pr_err("Error in usb_ep_autoconfig for OUT DESC Failed !!!!!!!!!!\n");
948 ep->driver_data = cdev; /* claim the endpoint */
949 mtp_func->bulk_out = ep;
951 /* Interrupt Support for MTP */
952 ep = usb_ep_autoconfig(cdev->gadget, intr_desc);
954 pr_err("Error in usb_ep_autoconfig for INT IN DESC Failed !!!!!!!!!!\n");
957 ep->driver_data = cdev;
958 mtp_func->int_in = ep;
960 for (i = 0; i < MTP_INT_REQ_MAX; i++) {
961 req = mtpg_request_new(mtp_func->int_in,
962 MTP_INTR_BUFFER_SIZE);
964 goto ep_alloc_fail_intr;
965 req->complete = interrupt_complete;
966 mtp_req_put(dev, &mtp_func->int_in_q, req);
969 for (i = 0; i < MTP_RX_REQ_MAX; i++) {
970 req = mtpg_request_new(mtp_func->bulk_out,
971 MTP_BULK_BUFFER_SIZE);
973 goto ep_alloc_fail_out;
974 req->complete = mtpg_complete_out;
975 mtp_req_put(dev, &mtp_func->bulk_out_q, req);
978 for (i = 0; i < MTP_TX_REQ_MAX; i++) {
979 req = mtpg_request_new(mtp_func->bulk_in,
980 MTP_BULK_BUFFER_SIZE);
982 goto ep_alloc_fail_all;
983 req->complete = mtpg_complete_in;
984 mtp_req_put(dev, &mtp_func->bulk_in_q, req);
990 while (!!(req = mtp_req_get(dev, &mtp_func->bulk_in_q)))
991 mtpg_request_free(req, mtp_func->bulk_in);
994 while (!!(req = mtp_req_get(dev, &mtp_func->bulk_out_q)))
995 mtpg_request_free(req, mtp_func->bulk_out);
998 while (!!(req = mtp_req_get(dev, &mtp_func->int_in_q)))
999 mtpg_request_free(req, mtp_func->int_in);
1004 static void mtpg_function_unbind(struct usb_configuration *c,
1005 struct usb_function *f)
1007 struct mtpg_dev *dev = the_mtpg;
1008 struct f_mtp *mtp_func = func_to_mtp(f);
1009 struct usb_request *req;
1011 DEBUG_MTPB("[%s] \tline = [%d]\n", __func__, __LINE__);
1016 if (gadget_is_dualspeed(c->cdev->gadget))
1017 usb_free_descriptors(f->hs_descriptors);
1018 usb_free_descriptors(f->descriptors);
1020 mutex_lock(&mtp_lock);
1022 while (!!(req = mtp_req_get(dev, &mtp_func->int_in_q)))
1023 mtpg_request_free(req, mtp_func->int_in);
1025 while (!!(req = mtp_req_get(dev, &mtp_func->bulk_out_q)))
1026 mtpg_request_free(req, mtp_func->bulk_out);
1028 while (!!(req = mtp_req_get(dev, &mtp_func->bulk_in_q)))
1029 mtpg_request_free(req, mtp_func->bulk_in);
1031 while (!!(req = mtp_req_get(dev, &dev->rx_done)))
1032 mtpg_request_free(req, mtp_func->bulk_out);
1035 dev->mtp_func = NULL;
1036 dev->read_req = NULL;
1037 dev->read_buf = NULL;
1038 dev->read_count = 0;
1040 mutex_unlock(&mtp_lock);
1042 wake_up(&dev->read_wq);
1043 wake_up(&dev->write_wq);
1044 wake_up(&dev->intr_wq);
1047 static int mtpg_function_bind(struct usb_configuration *c,
1048 struct usb_function *f)
1050 struct usb_composite_dev *cdev = c->cdev;
1051 struct f_mtp *mtp_func = func_to_mtp(f);
1054 /* Allocate string descriptor numbers ... note that string
1055 * contents can be overridden by the composite_dev glue.
1058 DEBUG_MTPB("[%s] \tline = [%d]\n", __func__, __LINE__);
1060 id = usb_interface_id(c, f);
1062 pr_err("Error in usb_string_id Failed !!!\n");
1066 mtpg_interface_desc.bInterfaceNumber = id;
1067 mtp_func->allocated_intf_num = id;
1069 rc = mtp_create_bulk_endpoints(mtp_func, &fs_mtpg_in_desc,
1070 &fs_mtpg_out_desc, &int_fs_notify_desc);
1072 pr_err("mtpg unable to autoconfigure all endpoints\n");
1076 f->descriptors = usb_copy_descriptors(fs_mtpg_desc);
1077 if (!f->descriptors)
1078 goto desc_alloc_fail;
1080 mtp_func->fs.bulk_in = usb_find_endpoint(fs_mtpg_desc, f->descriptors,
1082 mtp_func->fs.bulk_out = usb_find_endpoint(fs_mtpg_desc, f->descriptors,
1084 mtp_func->fs.int_in = usb_find_endpoint(fs_mtpg_desc, f->descriptors,
1085 &int_fs_notify_desc);
1087 if (gadget_is_dualspeed(cdev->gadget)) {
1089 DEBUG_MTPB("[%s] \tdual speed line = [%d]\n", __func__,
1092 /* Assume endpoint addresses are the same for both speeds */
1093 hs_mtpg_in_desc.bEndpointAddress =
1094 fs_mtpg_in_desc.bEndpointAddress;
1095 hs_mtpg_out_desc.bEndpointAddress =
1096 fs_mtpg_out_desc.bEndpointAddress;
1097 int_hs_notify_desc.bEndpointAddress =
1098 int_fs_notify_desc.bEndpointAddress;
1100 f->hs_descriptors = usb_copy_descriptors(hs_mtpg_desc);
1101 if (!f->hs_descriptors)
1102 goto desc_alloc_fail;
1104 mtp_func->hs.bulk_in =
1105 usb_find_endpoint(hs_mtpg_desc, f->hs_descriptors,
1107 mtp_func->hs.bulk_out =
1108 usb_find_endpoint(hs_mtpg_desc, f->hs_descriptors,
1110 mtp_func->hs.int_in =
1111 usb_find_endpoint(hs_mtpg_desc, f->hs_descriptors,
1112 &int_hs_notify_desc);
1119 usb_free_descriptors(f->descriptors);
1124 static int mtpg_function_set_alt(struct usb_function *f,
1125 unsigned intf, unsigned alt)
1127 struct f_mtp *mtp_func = func_to_mtp(f);
1128 struct usb_composite_dev *cdev = f->config->cdev;
1129 struct mtpg_dev *dev = the_mtpg;
1132 if (intf == mtp_func->allocated_intf_num) {
1134 if (mtp_func->int_in->driver_data)
1135 usb_ep_disable(mtp_func->int_in);
1137 ret = usb_ep_enable(mtp_func->int_in, ep_choose(cdev->gadget,
1138 mtp_func->hs.int_in,
1139 mtp_func->fs.int_in));
1141 usb_ep_disable(mtp_func->int_in);
1142 pr_err("[%s] Enable Int-In EP error!!!(%d)\n",
1146 mtp_func->int_in->driver_data = mtp_func;
1148 if (mtp_func->bulk_in->driver_data)
1149 usb_ep_disable(mtp_func->bulk_in);
1151 ret = usb_ep_enable(mtp_func->bulk_in, ep_choose(cdev->gadget,
1152 mtp_func->hs.bulk_in,
1153 mtp_func->fs.bulk_in));
1155 usb_ep_disable(mtp_func->bulk_in);
1156 pr_err("[%s] Enable Bulk-In EP error!!!(%d)\n",
1160 mtp_func->bulk_in->driver_data = mtp_func;
1162 if (mtp_func->bulk_out->driver_data)
1163 usb_ep_disable(mtp_func->bulk_out);
1165 ret = usb_ep_enable(mtp_func->bulk_out, ep_choose(cdev->gadget,
1166 mtp_func->hs.bulk_out,
1167 mtp_func->fs.bulk_out));
1169 usb_ep_disable(mtp_func->bulk_out);
1170 pr_err("[%s] Enable Bulk-Out EP error!!!(%d)\n",
1174 mtp_func->bulk_out->driver_data = mtp_func;
1176 dev->int_idle = &mtp_func->int_in_q;
1177 dev->tx_idle = &mtp_func->bulk_in_q;
1178 dev->rx_idle = &mtp_func->bulk_out_q;
1179 dev->mtp_func = mtp_func;
1182 /* readers may be blocked waiting for us to go online */
1183 wake_up(&dev->read_wq);
1187 pr_err("[%s] error , intf = %u , alt = %u",
1188 __func__, intf, alt);
1194 static void mtpg_function_disable(struct usb_function *f)
1197 struct mtpg_dev *dev = the_mtpg;
1198 struct f_mtp *mtp_func = func_to_mtp(f);
1200 DEBUG_MTPB("[%s] \tline = [%d]\n", __func__, __LINE__);
1205 spin_lock(&dev->lock);
1206 dev->int_idle = NULL;
1207 dev->tx_idle = NULL;
1208 dev->rx_idle = NULL;
1209 spin_unlock(&dev->lock);
1211 usb_ep_disable(mtp_func->int_in);
1212 mtp_func->int_in->driver_data = NULL;
1214 usb_ep_disable(mtp_func->bulk_in);
1215 mtp_func->bulk_in->driver_data = NULL;
1217 usb_ep_disable(mtp_func->bulk_out);
1218 mtp_func->bulk_out->driver_data = NULL;
1220 wake_up(&dev->read_wq);
1221 wake_up(&dev->write_wq);
1222 wake_up(&dev->intr_wq);
1225 /*PIMA15740-2000 spec: Class specific setup request for MTP*/
1226 static void mtp_complete_cancel_io(struct usb_ep *ep, struct usb_request *req)
1228 struct mtpg_dev *dev = ep->driver_data;
1229 DEBUG_MTPB("[%s] \tline = [%d]\n", __func__, __LINE__);
1230 if (req->status != 0) {
1231 DEBUG_MTPB("[%s] req->status !=0 \tline = [%d]\n",
1232 __func__, __LINE__);
1236 if (req->length != USB_PTPREQUEST_CANCELIO_SIZE)
1237 usb_ep_set_halt(ep);
1239 memset(dev->cancel_io_buf, 0, USB_PTPREQUEST_CANCELIO_SIZE + 1);
1240 memcpy(dev->cancel_io_buf, req->buf,
1241 USB_PTPREQUEST_CANCELIO_SIZE);
1242 mtp_send_signal(USB_PTPREQUEST_CANCELIO);
1246 static int mtpg_function_setup(struct usb_function *f,
1247 const struct usb_ctrlrequest *ctrl)
1249 struct mtpg_dev *dev = the_mtpg;
1250 struct usb_composite_dev *cdev = f->config->cdev;
1251 struct usb_request *req = cdev->req;
1252 int signal_request = 0;
1253 int value = -EOPNOTSUPP;
1254 u16 w_index = le16_to_cpu(ctrl->wIndex);
1255 u16 w_value = le16_to_cpu(ctrl->wValue);
1256 u16 w_length = le16_to_cpu(ctrl->wLength);
1258 DEBUG_MTPB("[%s] \tline = [%d]\n", __func__, __LINE__);
1259 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
1260 case (((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
1261 | USB_PTPREQUEST_CANCELIO):
1262 DEBUG_MTPB("[%s] USB_PTPREQUEST_CANCELIO \tline = [%d]\n",
1263 __func__, __LINE__);
1265 && w_index == mtpg_interface_desc.bInterfaceNumber
1266 && w_length == 0x06) {
1268 cdev->gadget->ep0->driver_data = dev;
1269 req->complete = mtp_complete_cancel_io;
1271 req->length = value;
1273 usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
1277 case (((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
1278 | USB_PTPREQUEST_RESET):
1279 DEBUG_MTPB("[%s] USB_PTPREQUEST_RESET \tline = [%d]\n",
1280 __func__, __LINE__);
1281 signal_request = USB_PTPREQUEST_RESET;
1284 case (((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
1285 | USB_PTPREQUEST_GETSTATUS):
1286 DEBUG_MTPB("[%s] USB_PTPREQUEST_GETSTATUS \tline = [%d]\n",
1287 __func__, __LINE__);
1288 signal_request = USB_PTPREQUEST_GETSTATUS;
1291 case (((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
1292 | USB_PTPREQUEST_GETEVENT):
1293 DEBUG_MTPB("[%s] USB_PTPREQUEST_GETEVENT \tline = [%d]\n",
1294 __func__, __LINE__);
1295 signal_request = USB_PTPREQUEST_GETEVENT;
1298 DEBUG_MTPB("[%s] INVALID REQUEST \tline = [%d]\n",
1299 __func__, __LINE__);
1302 value = mtp_send_signal(signal_request);
1306 static int mtp_bind_config(struct usb_configuration *c)
1309 struct f_mtp *mtp_func;
1311 DEBUG_MTPB("[%s] \tline = [%d]\n", __func__, __LINE__);
1314 pr_err("Error There is no the_mtpg!!\n");
1318 mtp_func = kzalloc(sizeof(*mtp_func), GFP_KERNEL);
1320 pr_err("mtp_func memory alloc failed !!!\n");
1324 INIT_LIST_HEAD(&mtp_func->bulk_out_q);
1325 INIT_LIST_HEAD(&mtp_func->bulk_in_q);
1326 INIT_LIST_HEAD(&mtp_func->int_in_q);
1328 mtp_func->function.name = mtp_longname;
1329 mtp_func->function.strings = mtp_dev_strings;
1331 mtp_func->function.bind = mtpg_function_bind;
1332 mtp_func->function.unbind = mtpg_function_unbind;
1333 mtp_func->function.set_alt = mtpg_function_set_alt;
1334 mtp_func->function.setup = mtpg_function_setup;
1335 mtp_func->function.disable = mtpg_function_disable;
1337 rc = usb_add_function(c, &mtp_func->function);
1339 pr_err("Error in usb_add_function Failed !!!\n");
1344 static int mtp_setup(struct usb_composite_dev *cdev)
1346 struct mtpg_dev *mtpg;
1349 mtpg = kzalloc(sizeof(*mtpg), GFP_KERNEL);
1351 pr_err("mtpg_dev_alloc memory failed !!!\n");
1355 if (strings_dev_mtp[F_MTP_IDX].id == 0) {
1356 status = usb_string_id(cdev);
1361 strings_dev_mtp[F_MTP_IDX].id = status;
1362 mtpg_interface_desc.iInterface = status;
1365 spin_lock_init(&mtpg->lock);
1366 init_waitqueue_head(&mtpg->read_wq);
1367 init_waitqueue_head(&mtpg->write_wq);
1368 init_waitqueue_head(&mtpg->intr_wq);
1370 atomic_set(&mtpg->open_excl, 0);
1371 atomic_set(&mtpg->read_excl, 0);
1372 atomic_set(&mtpg->write_excl, 0);
1373 atomic_set(&mtpg->wintfd_excl, 0);
1375 INIT_LIST_HEAD(&mtpg->rx_done);
1377 /* the_mtpg must be set before calling usb_gadget_register_driver */
1380 status = misc_register(&mtpg_device);
1382 pr_err("Error in misc_register of mtpg_device Failed !!!\n");
1390 static void mtp_cleanup(void)
1392 struct mtpg_dev *mtpg = the_mtpg;
1394 misc_deregister(&mtpg_device);
1402 MODULE_AUTHOR("Deepak And Madhukar");
1403 MODULE_LICENSE("GPL");