Initial commit
[kernel/linux-3.0.git] / drivers / usb / gadget / f_mtp_slp.c
1 /*
2  * drivers/usb/gadget/f_mtp_slp.c
3  *
4  * Function Driver for USB MTP,
5  * mtpg.c -- MTP Driver, for MTP development,
6  *
7  * Copyright (C) 2009 by Samsung Electronics,
8  * Author:Deepak M.G. <deepak.guru@samsung.com>,
9  * Author:Madhukar.J  <madhukar.j@samsung.com>,
10  *
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.
15  *
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.
20  */
21
22 /*
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.
27  */
28
29 /* Includes */
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>
51
52 #include <linux/sched.h>
53 #include <asm-generic/siginfo.h>
54
55 #include "gadget_chips.h"
56
57 /*-------------------------------------------------------------------------*/
58 /*Only for Debug*/
59 #define DEBUG_MTP 0
60 /*#define CSY_TEST */
61
62 #if DEBUG_MTP
63 #define DEBUG_MTP_SETUP
64 #define DEBUG_MTP_READ
65 #define DEBUG_MTP_WRITE
66
67 #else
68 #undef DEBUG_MTP_SETUP
69 #undef DEBUG_MTP_READ
70 #undef DEBUG_MTP_WRITE
71 #endif
72
73 #ifdef DEBUG_MTP_SETUP
74 #define DEBUG_MTPB(fmt, args...) printk(KERN_INFO fmt, ##args)
75 #else
76 #define DEBUG_MTPB(fmt, args...) do {} while (0)
77 #endif
78
79 #ifdef DEBUG_MTP_READ
80 #define DEBUG_MTPR(fmt, args...) printk(KERN_INFO fmt, ##args)
81 #else
82 #define DEBUG_MTPR(fmt, args...) do {} while (0)
83 #endif
84
85 #ifdef DEBUG_MTP_WRITE
86 #define DEBUG_MTPW(fmt, args...) printk(KERN_INFO fmt, ##args)
87 #else
88 #define DEBUG_MTPW(fmt, args...) do {} while (0)
89 #endif
90 /*-------------------------------------------------------------------------*/
91
92 #define MTP_BULK_BUFFER_SIZE     32768
93 #define MTP_INTR_BUFFER_SIZE     22
94
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
99
100 #define MTP_DRIVER_NAME          "usb_mtp_gadget"
101
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)
112 #define SIG_SETUP                       44
113
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
121
122 static const char mtp_longname[] = "mtp";
123
124 static DEFINE_MUTEX(mtp_lock);
125 static const char mtp_shortname[] = MTP_DRIVER_NAME;
126 static pid_t mtp_pid;
127
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;
132 };
133
134 struct f_mtp {
135         struct usb_function function;
136         unsigned allocated_intf_num;
137
138         struct mtp_ep_descs fs;
139         struct mtp_ep_descs hs;
140
141         struct usb_ep *bulk_in;
142         struct usb_ep *bulk_out;
143         struct usb_ep *int_in;
144
145         struct list_head bulk_in_q;
146         struct list_head bulk_out_q;
147         struct list_head int_in_q;
148 };
149
150 /* MTP Device Structure*/
151 struct mtpg_dev {
152         struct f_mtp *mtp_func;
153         spinlock_t lock;
154
155         int online;
156         int error;
157
158         wait_queue_head_t read_wq;
159         wait_queue_head_t write_wq;
160         wait_queue_head_t intr_wq;
161
162         struct usb_request *read_req;
163         unsigned char *read_buf;
164         unsigned read_count;
165
166         atomic_t read_excl;
167         atomic_t write_excl;
168         atomic_t ioctl_excl;
169         atomic_t open_excl;
170         atomic_t wintfd_excl;
171
172         struct list_head *int_idle;
173         struct list_head *tx_idle;
174         struct list_head *rx_idle;
175         struct list_head rx_done;
176
177         char cancel_io_buf[USB_PTPREQUEST_CANCELIO_SIZE + 1];
178 };
179
180 struct usb_mtp_ctrlrequest {
181         struct usb_ctrlrequest  setup;
182 };
183
184 static inline struct f_mtp *func_to_mtp(struct usb_function *f)
185 {
186         return container_of(f, struct f_mtp, function);
187 }
188
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;
192
193 /* Three full-speed and high-speed endpoint descriptors: bulk-in, bulk-out,
194  * and interrupt-in. */
195
196 static struct usb_interface_descriptor mtpg_interface_desc = {
197         .bLength = sizeof mtpg_interface_desc,
198         .bDescriptorType = USB_DT_INTERFACE,
199         .bNumEndpoints = 3,
200         .bInterfaceClass = USB_CLASS_STILL_IMAGE,
201         .bInterfaceSubClass = 01,
202         .bInterfaceProtocol = 01,
203 };
204
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 */
211 };
212
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 */
219 };
220
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),
227         .bInterval = 6,
228 };
229
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,
235         NULL,
236 };
237
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),
244 };
245
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),
252 };
253
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),
260         .bInterval = 6,
261 };
262
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,
268         NULL
269 };
270
271 /* string IDs are assigned dynamically */
272 #define F_MTP_IDX                       0
273 #define MTP_STRING_PRODUCT_IDX          1
274 #define MTP_STRING_SERIAL_IDX           2
275
276 /* default mtp_serial number takes at least two packets */
277 static char mtp_serial[36] = "0123456789.0123456789.0123456789";
278
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 */
284 };
285
286 static struct usb_gadget_strings stringtab_mtp = {
287         .language = 0x0409,     /* en-us */
288         .strings = strings_dev_mtp,
289 };
290
291 static struct usb_gadget_strings *mtp_dev_strings[] = {
292         &stringtab_mtp,
293         NULL,
294 };
295
296 /* -------------------------------------------------------------------------
297  *      Main Functionalities Start!
298  * ------------------------------------------------------------------------- */
299
300 static inline int _mtp_lock(atomic_t *excl)
301 {
302         DEBUG_MTPB("[%s] \tline = [%d]\n", __func__, __LINE__);
303         if (atomic_inc_return(excl) == 1) {
304                 return 0;
305         } else {
306                 atomic_dec(excl);
307                 return -1;
308         }
309 }
310
311 static inline void _mtp_unlock(atomic_t *excl)
312 {
313         atomic_dec(excl);
314 }
315
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)
319 {
320         unsigned long flags;
321
322         if (!dev || !req)
323                 return;
324
325         DEBUG_MTPB("[%s] \tline = [%d]\n", __func__, __LINE__);
326         spin_lock_irqsave(&dev->lock, flags);
327         if (head)
328                 list_add_tail(&req->list, head);
329         spin_unlock_irqrestore(&dev->lock, flags);
330 }
331
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)
335 {
336         unsigned long flags;
337         struct usb_request *req;
338
339         if (!dev)
340                 return 0;
341
342         DEBUG_MTPB("[%s] \tline = [%d]\n", __func__, __LINE__);
343
344         spin_lock_irqsave(&dev->lock, flags);
345         if (!head)
346                 req = NULL;
347         else {
348                 if (list_empty(head)) {
349                         req = NULL;
350                 } else {
351                         req = list_first_entry(head, struct usb_request, list);
352                         list_del(&req->list);
353                 }
354         }
355         spin_unlock_irqrestore(&dev->lock, flags);
356
357         return req;
358 }
359
360 static int mtp_send_signal(int value)
361 {
362         int ret;
363         struct siginfo info;
364         struct task_struct *t;
365         memset(&info, 0, sizeof(struct siginfo));
366         info.si_signo = SIG_SETUP;
367         info.si_code = SI_QUEUE;
368         info.si_int = value;
369         rcu_read_lock();
370         t = find_task_by_vpid(mtp_pid);
371         if (t == NULL) {
372                 pr_err("no such pid\n");
373                 rcu_read_unlock();
374                 return -ENODEV;
375         }
376
377         rcu_read_unlock();
378         ret = send_sig_info(SIG_SETUP, &info, t);
379         if (ret < 0) {
380                 pr_err("error sending signal !!!!!!!!\n");
381                 return ret;
382         }
383         return 0;
384
385 }
386
387 static int mtpg_open(struct inode *ip, struct file *fp)
388 {
389         DEBUG_MTPB("[%s] \tline = [%d]\n", __func__, __LINE__);
390
391         if (_mtp_lock(&the_mtpg->open_excl)) {
392                 pr_err("mtpg_open fn -- mtpg device busy\n");
393                 return -EBUSY;
394         }
395
396         fp->private_data = the_mtpg;
397
398         /* clear the error latch */
399
400         DEBUG_MTPB("[%s] mtpg_open and clearing the error = 0\n", __func__);
401
402         the_mtpg->error = 0;
403
404         return 0;
405 }
406
407 static ssize_t mtpg_read(struct file *fp, char __user *buf,
408                          size_t count, loff_t *pos)
409 {
410         struct mtpg_dev *dev = fp->private_data;
411         struct usb_request *req;
412         int r = count, xfer;
413         int ret;
414
415         DEBUG_MTPR("*******[%s] and count = (%d)\n", __func__, count);
416
417         if (_mtp_lock(&dev->read_excl)) {
418                 pr_err("mtpg_read fn -- mtpg device busy\n");
419                 return -EBUSY;
420         }
421
422         while (!(dev->online || dev->error)) {
423                 DEBUG_MTPR("******[%s] and line is = %d\n",
424                         __func__, __LINE__);
425                 ret = wait_event_interruptible(dev->read_wq,
426                                 (dev->online || dev->error));
427                 if (ret < 0) {
428                         _mtp_unlock(&dev->read_excl);
429                         return ret;
430                 }
431         }
432
433         while (count > 0) {
434                 DEBUG_MTPR("*********[%s] and line is = %d\n",
435                         __func__, __LINE__);
436
437                 if (dev->error) {
438                         r = -EIO;
439                         pr_err("*******[%s]\t%d: dev->error so break r=%d\n",
440                                 __func__, __LINE__, r);
441                         break;
442                 }
443
444                 /* if we have idle read requests, get them queued */
445                 while (!!(req = mtp_req_get(dev, dev->rx_idle))) {
446  requeue_req:
447                         mutex_lock(&mtp_lock);
448                         if (!dev->mtp_func)
449                                 ret = -ENODEV;
450                         else {
451                                 req->length = MTP_BULK_BUFFER_SIZE;
452                                 ret = usb_ep_queue(dev->mtp_func->bulk_out,
453                                                         req, GFP_ATOMIC);
454                         }
455                         mutex_unlock(&mtp_lock);
456
457                         if (ret < 0) {
458                                 r = ret;
459                                 dev->error = 1;
460                                 mtp_req_put(dev, dev->rx_idle, req);
461                                 goto fail;
462                         } else {
463                                 DEBUG_MTPR("********* [%s] rx req queue %p\n",
464                                    __func__, req);
465                         }
466                 }
467
468                 DEBUG_MTPR("*******[%s]\t%d: read_count = %d\n", __func__,
469                            __LINE__, dev->read_count);
470
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;
477                         else
478                                 xfer = count;
479
480                         mutex_lock(&mtp_lock);
481                         if (!dev->mtp_func) {
482                                 mutex_unlock(&mtp_lock);
483                                 r = -ENODEV;
484                                 break;
485                         } else if (copy_to_user(buf, dev->read_buf, xfer)) {
486                                 mutex_unlock(&mtp_lock);
487                                 r = -EFAULT;
488                                 break;
489                         }
490                         mutex_unlock(&mtp_lock);
491
492                         dev->read_buf += xfer;
493                         dev->read_count -= xfer;
494                         buf += xfer;
495                         count -= xfer;
496
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",
500                                            __func__, __LINE__);
501                                 mtp_req_put(dev, dev->rx_idle, dev->read_req);
502                                 dev->read_req = NULL;
503                         }
504
505                         r = xfer;
506                         DEBUG_MTPR("***** [%s] \t %d: returning lenght %d\n",
507                                    __func__, __LINE__, r);
508                         goto fail;
509                 }
510
511                 /* wait for a request to complete */
512                 req = 0;
513                 ret = wait_event_interruptible(dev->read_wq,
514                              (!!(req = mtp_req_get(dev, &dev->rx_done))
515                               || dev->error));
516
517                 DEBUG_MTPR("*******[%s]\t%d: dev->error %d and req = %p\n",
518                            __func__, __LINE__, dev->error, req);
519
520                 if (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
523                          * be stuck forever
524                          */
525                         if (req->actual == 0)
526                                 goto requeue_req;
527
528                         dev->read_req = req;
529                         dev->read_count = req->actual;
530                         dev->read_buf = req->buf;
531                 }
532
533                 if (ret < 0) {
534                         r = ret;
535                         break;
536                 }
537         }
538
539  fail:
540         _mtp_unlock(&dev->read_excl);
541         return r;
542 }
543
544 static ssize_t mtpg_write(struct file *fp, const char __user *buf,
545                           size_t count, loff_t *pos)
546 {
547         struct mtpg_dev *dev = fp->private_data;
548         struct usb_request *req = 0;
549         int r = count, xfer;
550         int ret;
551
552         if (_mtp_lock(&dev->write_excl))
553                 return -EBUSY;
554
555         while (count > 0) {
556                 if (dev->error) {
557                         r = -EIO;
558                         break;
559                 }
560
561                 /* get an idle tx request to use */
562                 req = 0;
563
564                 ret = wait_event_interruptible(dev->write_wq,
565                              (!!(req = mtp_req_get(dev, dev->tx_idle))
566                               || dev->error));
567
568                 DEBUG_MTPW("[%s]\t%d: count : %d, dev->error = %d\n",
569                            __func__, __LINE__, count, dev->error);
570
571                 if (ret < 0) {
572                         r = ret;
573                         break;
574                 }
575
576                 if (req) {
577                         if (count > MTP_BULK_BUFFER_SIZE)
578                                 xfer = MTP_BULK_BUFFER_SIZE;
579                         else
580                                 xfer = count;
581
582                         DEBUG_MTPW("***** [%s]\t%d copy_from_user length %d\n",
583                                    __func__, __LINE__, xfer);
584
585                         mutex_lock(&mtp_lock);
586                         if (!dev->mtp_func) {
587                                 mutex_unlock(&mtp_lock);
588                                 r = -ENODEV;
589                                 break;
590                         } else if (copy_from_user(req->buf, buf, xfer)) {
591                                 mutex_unlock(&mtp_lock);
592                                 r = -EFAULT;
593                                 break;
594                         }
595
596                         req->length = xfer;
597                         ret = usb_ep_queue(dev->mtp_func->bulk_in,
598                                                 req, GFP_ATOMIC);
599                         mutex_unlock(&mtp_lock);
600
601                         if (ret < 0) {
602                                 dev->error = 1;
603                                 r = ret;
604                                 break;
605                         }
606
607                         buf += xfer;
608                         count -= xfer;
609
610                         /* zero this so we don't try to free it on error exit */
611                         req = NULL;
612                 }
613         }
614
615         if (req) {
616                 DEBUG_MTPW("[%s] \t%d  mtp_req_put\n", __func__, __LINE__);
617                 mtp_req_put(dev, dev->tx_idle, req);
618         }
619
620         _mtp_unlock(&dev->write_excl);
621         return r;
622 }
623
624 /*Fixme for Interrupt Transfer*/
625 static void interrupt_complete(struct usb_ep *ep, struct usb_request *req)
626 {
627         struct mtpg_dev *dev = the_mtpg;
628         struct f_mtp *mtp_func = ep->driver_data;
629
630         DEBUG_MTPB("[%s] \t %d req->status is = %d\n",
631                 __func__, __LINE__, req->status);
632
633         if (req->status != 0)
634                 dev->error = 1;
635
636         mtp_req_put(dev, &mtp_func->int_in_q, req);
637         wake_up(&dev->intr_wq);
638 }
639
640 /*interrupt_write() is special write function for fixed size*/
641 static int interrupt_write(struct mtpg_dev *dev, const char __user *buf)
642 {
643         struct usb_request *req = NULL;
644         int count = MTP_INTR_BUFFER_SIZE;
645         int ret;
646
647         DEBUG_MTPB("[%s] \tline = [%d]\n", __func__, __LINE__);
648
649         if (_mtp_lock(&dev->wintfd_excl)) {
650                 pr_err("wintfd_excl lock failed for interrupt_write\n");
651                 return -EBUSY;
652         }
653
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))
657                       || dev->error));
658
659         DEBUG_MTPW("[%s]\t%d: count : %d, dev->error = %d\n",
660                    __func__, __LINE__, count, dev->error);
661
662         if (ret < 0) {
663                 pr_err("[%s]%d wait_event return fail\n",
664                                                  __func__, __LINE__);
665                 goto interrupt_write_fail;
666         } else if (dev->error) {
667                 ret = -EIO;
668                 pr_err("[%s]%d dev->error so brk\n",
669                                                  __func__, __LINE__);
670                 goto interrupt_write_fail;
671         }
672
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);
676
677         mutex_lock(&mtp_lock);
678         if (!dev->mtp_func) {
679                 mutex_unlock(&mtp_lock);
680                 ret = -ENODEV;
681                 goto interrupt_write_fail;
682         } else if (copy_from_user(req->buf, buf, count)) {
683                 mutex_unlock(&mtp_lock);
684                 ret = -EFAULT;
685                 goto interrupt_write_fail;
686         }
687
688         req->length = count;
689         ret = usb_ep_queue(dev->mtp_func->int_in,
690                                 req, GFP_ATOMIC);
691         mutex_unlock(&mtp_lock);
692
693         if (ret < 0)
694                 dev->error = 1;
695         else
696                 req = NULL;
697
698 interrupt_write_fail:
699         if (req)
700                 mtp_req_put(dev, dev->int_idle, req);
701         _mtp_unlock(&dev->wintfd_excl);
702
703         return ret;
704 }
705
706 /*Fixme for enabling and disabling the MTP*/
707 static long mtpg_ioctl(struct file *fd, unsigned int code, unsigned long arg)
708 {
709
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;
715         int status = 0;
716         int size = 0;
717         void __user *ubuf = (void __user *)arg;
718         char buf[USB_PTPREQUEST_GETSTATUS_SIZE + 1] = { 0 };
719
720         DEBUG_MTPB("[%s] with cmd:[%04x]\n", __func__, code);
721
722         if (code == SET_MTP_USER_PID) {
723                 if (copy_from_user(&mtp_pid, ubuf, sizeof(mtp_pid)))
724                         status = -EFAULT;
725                 else
726                         pr_info("[%s] SET_MTP_USER_PID; pid = %d \tline = [%d]\n",
727                            __func__, mtp_pid, __LINE__);
728                 return status;
729         } else if (code ==  MTP_WRITE_INT_DATA) {
730                 return interrupt_write(dev, ubuf);
731         }
732
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);
737                 return -ENODEV;
738         }
739         cdev = dev->mtp_func->function.config->cdev;
740         req = cdev->req;
741         bulk_in = dev->mtp_func->bulk_in;
742         bulk_out = dev->mtp_func->bulk_out;
743
744         switch (code) {
745         case MTP_DISABLE:
746                 if (cdev && cdev->gadget) {
747                         pr_info("mtpg_ioctl for MTP_DISABLE, usb_gadget_dicon/connect!!\n");
748                         usb_gadget_disconnect(cdev->gadget);
749                         mdelay(5);
750                         usb_gadget_connect(cdev->gadget);
751                 }
752                 break;
753
754         case MTP_CLEAR_HALT:
755                 status = usb_ep_clear_halt(bulk_in);
756                 if (!status)
757                         status = usb_ep_clear_halt(bulk_out);
758                 break;
759
760         case GET_SETUP_DATA:
761                 if (copy_to_user(ubuf, dev->cancel_io_buf,
762                      USB_PTPREQUEST_CANCELIO_SIZE))
763                         status = -EIO;
764                 break;
765
766         case SEND_RESET_ACK:
767                 req->length = 0;
768                 status = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
769                 if (status < 0)
770                         DEBUG_MTPB("[%s] Error at usb_ep_queue\tline = [%d]\n",
771                                    __func__, __LINE__);
772                 break;
773
774         case SET_SETUP_DATA:
775                 if (copy_from_user(buf, ubuf,
776                                 USB_PTPREQUEST_GETSTATUS_SIZE)) {
777                         status = -EIO;
778                         DEBUG_MTPR("*****[%s]\t%d: copy-from-user failed!!!!\n",
779                                    __func__, __LINE__);
780                         break;
781                 }
782                 size = buf[0];
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);
786                 req->zero = 0;
787                 req->length = size;
788                 status = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
789                 if (status < 0)
790                         DEBUG_MTPB("[%s] Error at usb_ep_queue\tline = [%d]\n",
791                                    __func__, __LINE__);
792                 break;
793
794         case SET_ZLP_DATA:
795                 status = wait_event_interruptible(dev->write_wq,
796                                 (!!(req = mtp_req_get(dev, dev->tx_idle))
797                                  || dev->error));
798                 if (status < 0 || dev->error == 1) {
799                         pr_err("***** [%s]\t%d status = %d !!!!!\n",
800                                                 __func__, __LINE__, status);
801                         break;
802                 }
803                 req->length = 0;
804                 status = usb_ep_queue(bulk_in, req, GFP_ATOMIC);
805                 if (status < 0)
806                         pr_err("[%s] Error at usb_ep_queue\tline = [%d]\n",
807                                __func__, __LINE__);
808                 break;
809
810         case GET_HIGH_FULL_SPEED:
811                 if (!bulk_in->driver_data) {
812                         pr_info("USB speed does not negotiate with host\n");
813                         status = -ENODEV;
814                 } else {
815                         int maxpacket = bulk_in->maxpacket;
816                         if (copy_to_user(ubuf, &maxpacket, sizeof(int)))
817                                 status = -EIO;
818                 }
819                 break;
820
821         default:
822                 status = -ENOTTY;
823         }
824         mutex_unlock(&mtp_lock);
825         return status;
826 }
827
828 static int mtpg_release_device(struct inode *ip, struct file *fp)
829 {
830         DEBUG_MTPB("[%s] \tline = [%d]\n", __func__, __LINE__);
831
832         if (the_mtpg != NULL)
833                 _mtp_unlock(&the_mtpg->open_excl);
834
835         return 0;
836 }
837
838 /* file operations for MTP device /dev/usb_mtp_gadget */
839 static const struct file_operations mtpg_fops = {
840         .owner = THIS_MODULE,
841         .read = mtpg_read,
842         .write = mtpg_write,
843         .open = mtpg_open,
844         .unlocked_ioctl = mtpg_ioctl,
845         .release = mtpg_release_device,
846 };
847
848 static struct miscdevice mtpg_device = {
849         .minor = MISC_DYNAMIC_MINOR,
850         .name = mtp_shortname,
851         .fops = &mtpg_fops,
852 };
853
854 static void mtpg_request_free(struct usb_request *req, struct usb_ep *ep)
855 {
856
857         DEBUG_MTPB("[%s] \tline = [%d]\n", __func__, __LINE__);
858
859         if (req) {
860                 kfree(req->buf);
861                 usb_ep_free_request(ep, req);
862         }
863 }
864
865 static struct usb_request *mtpg_request_new(struct usb_ep *ep, int buffer_size)
866 {
867
868         struct usb_request *req = usb_ep_alloc_request(ep, GFP_KERNEL);
869
870         DEBUG_MTPB("[%s] \tline = [%d]\n", __func__, __LINE__);
871
872         if (!req) {
873                 pr_err("******* %s \t line %d ERROR !!!\n",
874                         __func__, __LINE__);
875                 return NULL;
876         }
877
878         /* now allocate buffers for the requests */
879         req->buf = kmalloc(buffer_size, GFP_KERNEL);
880         if (!req->buf) {
881                 usb_ep_free_request(ep, req);
882                 return NULL;
883         }
884
885         return req;
886 }
887
888 static void mtpg_complete_in(struct usb_ep *ep, struct usb_request *req)
889 {
890         struct mtpg_dev *dev = the_mtpg;
891         struct f_mtp *mtp_func = ep->driver_data;
892
893         DEBUG_MTPB("[%s] \t %d req->status is = %d\n",
894                 __func__, __LINE__, req->status);
895
896         if (req->status != 0)
897                 dev->error = 1;
898
899         mtp_req_put(dev, &mtp_func->bulk_in_q, req);
900         wake_up(&dev->write_wq);
901 }
902
903 static void mtpg_complete_out(struct usb_ep *ep, struct usb_request *req)
904 {
905         struct mtpg_dev *dev = the_mtpg;
906         struct f_mtp *mtp_func = ep->driver_data;
907
908         DEBUG_MTPB("[%s] \tline = [%d]req->status is = %d\n",
909                 __func__, __LINE__, req->status);
910         if (req->status != 0) {
911                 dev->error = 1;
912
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);
916         } else {
917                 DEBUG_MTPB("[%s] \t %d for rx_done\n", __func__, __LINE__);
918                 mtp_req_put(dev, &dev->rx_done, req);
919         }
920         wake_up(&dev->read_wq);
921 }
922
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)
928 {
929         struct usb_composite_dev *cdev = mtp_func->function.config->cdev;
930         struct usb_request *req;
931         struct mtpg_dev *dev = the_mtpg;
932         struct usb_ep *ep;
933         int i;
934
935         ep = usb_ep_autoconfig(cdev->gadget, in_desc);
936         if (!ep) {
937                 pr_err("Error in usb_ep_autoconfig for IN DESC Failed !!!!!!!!!!\n");
938                 return -ENODEV;
939         }
940         ep->driver_data = cdev; /* claim the endpoint */
941         mtp_func->bulk_in = ep;
942
943         ep = usb_ep_autoconfig(cdev->gadget, out_desc);
944         if (!ep) {
945                 pr_err("Error in usb_ep_autoconfig for OUT DESC Failed !!!!!!!!!!\n");
946                 return -ENODEV;
947         }
948         ep->driver_data = cdev; /* claim the endpoint */
949         mtp_func->bulk_out = ep;
950
951         /* Interrupt Support for MTP */
952         ep = usb_ep_autoconfig(cdev->gadget, intr_desc);
953         if (!ep) {
954                 pr_err("Error in usb_ep_autoconfig for INT IN DESC Failed !!!!!!!!!!\n");
955                 return -ENODEV;
956         }
957         ep->driver_data = cdev;
958         mtp_func->int_in = ep;
959
960         for (i = 0; i < MTP_INT_REQ_MAX; i++) {
961                 req = mtpg_request_new(mtp_func->int_in,
962                                         MTP_INTR_BUFFER_SIZE);
963                 if (!req)
964                         goto ep_alloc_fail_intr;
965                 req->complete = interrupt_complete;
966                 mtp_req_put(dev, &mtp_func->int_in_q, req);
967         }
968
969         for (i = 0; i < MTP_RX_REQ_MAX; i++) {
970                 req = mtpg_request_new(mtp_func->bulk_out,
971                                         MTP_BULK_BUFFER_SIZE);
972                 if (!req)
973                         goto ep_alloc_fail_out;
974                 req->complete = mtpg_complete_out;
975                 mtp_req_put(dev, &mtp_func->bulk_out_q, req);
976         }
977
978         for (i = 0; i < MTP_TX_REQ_MAX; i++) {
979                 req = mtpg_request_new(mtp_func->bulk_in,
980                                         MTP_BULK_BUFFER_SIZE);
981                 if (!req)
982                         goto ep_alloc_fail_all;
983                 req->complete = mtpg_complete_in;
984                 mtp_req_put(dev, &mtp_func->bulk_in_q, req);
985         }
986
987         return 0;
988
989  ep_alloc_fail_all:
990         while (!!(req = mtp_req_get(dev, &mtp_func->bulk_in_q)))
991                 mtpg_request_free(req, mtp_func->bulk_in);
992
993  ep_alloc_fail_out:
994         while (!!(req = mtp_req_get(dev, &mtp_func->bulk_out_q)))
995                 mtpg_request_free(req, mtp_func->bulk_out);
996
997  ep_alloc_fail_intr:
998         while (!!(req = mtp_req_get(dev, &mtp_func->int_in_q)))
999                 mtpg_request_free(req, mtp_func->int_in);
1000
1001         return -ENOMEM;
1002 }
1003
1004 static void mtpg_function_unbind(struct usb_configuration *c,
1005                                  struct usb_function *f)
1006 {
1007         struct mtpg_dev *dev = the_mtpg;
1008         struct f_mtp *mtp_func = func_to_mtp(f);
1009         struct usb_request *req;
1010
1011         DEBUG_MTPB("[%s] \tline = [%d]\n", __func__, __LINE__);
1012
1013         dev->online = 0;
1014         dev->error = 1;
1015
1016         if (gadget_is_dualspeed(c->cdev->gadget))
1017                 usb_free_descriptors(f->hs_descriptors);
1018         usb_free_descriptors(f->descriptors);
1019
1020         mutex_lock(&mtp_lock);
1021
1022         while (!!(req = mtp_req_get(dev, &mtp_func->int_in_q)))
1023                 mtpg_request_free(req, mtp_func->int_in);
1024
1025         while (!!(req = mtp_req_get(dev, &mtp_func->bulk_out_q)))
1026                 mtpg_request_free(req, mtp_func->bulk_out);
1027
1028         while (!!(req = mtp_req_get(dev, &mtp_func->bulk_in_q)))
1029                 mtpg_request_free(req, mtp_func->bulk_in);
1030
1031         while (!!(req = mtp_req_get(dev, &dev->rx_done)))
1032                 mtpg_request_free(req, mtp_func->bulk_out);
1033
1034         kfree(mtp_func);
1035         dev->mtp_func = NULL;
1036         dev->read_req = NULL;
1037         dev->read_buf = NULL;
1038         dev->read_count = 0;
1039
1040         mutex_unlock(&mtp_lock);
1041
1042         wake_up(&dev->read_wq);
1043         wake_up(&dev->write_wq);
1044         wake_up(&dev->intr_wq);
1045 }
1046
1047 static int mtpg_function_bind(struct usb_configuration *c,
1048                               struct usb_function *f)
1049 {
1050         struct usb_composite_dev *cdev = c->cdev;
1051         struct f_mtp *mtp_func = func_to_mtp(f);
1052         int rc, id;
1053
1054         /* Allocate string descriptor numbers ... note that string
1055          * contents can be overridden by the composite_dev glue.
1056          */
1057
1058         DEBUG_MTPB("[%s] \tline = [%d]\n", __func__, __LINE__);
1059
1060         id = usb_interface_id(c, f);
1061         if (id < 0) {
1062                 pr_err("Error in usb_string_id Failed !!!\n");
1063                 return id;
1064         }
1065
1066         mtpg_interface_desc.bInterfaceNumber = id;
1067         mtp_func->allocated_intf_num = id;
1068
1069         rc = mtp_create_bulk_endpoints(mtp_func, &fs_mtpg_in_desc,
1070                                    &fs_mtpg_out_desc, &int_fs_notify_desc);
1071         if (rc) {
1072                 pr_err("mtpg unable to autoconfigure all endpoints\n");
1073                 return rc;
1074         }
1075
1076         f->descriptors = usb_copy_descriptors(fs_mtpg_desc);
1077         if (!f->descriptors)
1078                 goto desc_alloc_fail;
1079
1080         mtp_func->fs.bulk_in = usb_find_endpoint(fs_mtpg_desc, f->descriptors,
1081                                                  &fs_mtpg_in_desc);
1082         mtp_func->fs.bulk_out = usb_find_endpoint(fs_mtpg_desc, f->descriptors,
1083                                                   &fs_mtpg_out_desc);
1084         mtp_func->fs.int_in = usb_find_endpoint(fs_mtpg_desc, f->descriptors,
1085                                                 &int_fs_notify_desc);
1086
1087         if (gadget_is_dualspeed(cdev->gadget)) {
1088
1089                 DEBUG_MTPB("[%s] \tdual speed line = [%d]\n", __func__,
1090                            __LINE__);
1091
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;
1099
1100                 f->hs_descriptors = usb_copy_descriptors(hs_mtpg_desc);
1101                 if (!f->hs_descriptors)
1102                         goto desc_alloc_fail;
1103
1104                 mtp_func->hs.bulk_in =
1105                     usb_find_endpoint(hs_mtpg_desc, f->hs_descriptors,
1106                                       &hs_mtpg_in_desc);
1107                 mtp_func->hs.bulk_out =
1108                     usb_find_endpoint(hs_mtpg_desc, f->hs_descriptors,
1109                                       &hs_mtpg_out_desc);
1110                 mtp_func->hs.int_in =
1111                     usb_find_endpoint(hs_mtpg_desc, f->hs_descriptors,
1112                                       &int_hs_notify_desc);
1113         }
1114
1115         return 0;
1116
1117  desc_alloc_fail:
1118         if (f->descriptors)
1119                 usb_free_descriptors(f->descriptors);
1120
1121         return -ENOMEM;
1122 }
1123
1124 static int mtpg_function_set_alt(struct usb_function *f,
1125                                  unsigned intf, unsigned alt)
1126 {
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;
1130         int ret;
1131
1132         if (intf == mtp_func->allocated_intf_num) {
1133
1134                 if (mtp_func->int_in->driver_data)
1135                         usb_ep_disable(mtp_func->int_in);
1136
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));
1140                 if (ret) {
1141                         usb_ep_disable(mtp_func->int_in);
1142                         pr_err("[%s] Enable Int-In EP error!!!(%d)\n",
1143                                __func__, ret);
1144                         return ret;
1145                 }
1146                 mtp_func->int_in->driver_data = mtp_func;
1147
1148                 if (mtp_func->bulk_in->driver_data)
1149                         usb_ep_disable(mtp_func->bulk_in);
1150
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));
1154                 if (ret) {
1155                         usb_ep_disable(mtp_func->bulk_in);
1156                         pr_err("[%s] Enable Bulk-In EP error!!!(%d)\n",
1157                                __func__, ret);
1158                         return ret;
1159                 }
1160                 mtp_func->bulk_in->driver_data = mtp_func;
1161
1162                 if (mtp_func->bulk_out->driver_data)
1163                         usb_ep_disable(mtp_func->bulk_out);
1164
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));
1168                 if (ret) {
1169                         usb_ep_disable(mtp_func->bulk_out);
1170                         pr_err("[%s] Enable Bulk-Out EP error!!!(%d)\n",
1171                                __func__, ret);
1172                         return ret;
1173                 }
1174                 mtp_func->bulk_out->driver_data = mtp_func;
1175
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;
1180                 dev->online = 1;
1181
1182                 /* readers may be blocked waiting for us to go online */
1183                 wake_up(&dev->read_wq);
1184
1185                 return 0;
1186         } else {
1187                 pr_err("[%s] error , intf = %u , alt = %u",
1188                         __func__, intf, alt);
1189                 return -EINVAL;
1190         }
1191
1192 }
1193
1194 static void mtpg_function_disable(struct usb_function *f)
1195 {
1196
1197         struct mtpg_dev *dev = the_mtpg;
1198         struct f_mtp *mtp_func = func_to_mtp(f);
1199
1200         DEBUG_MTPB("[%s] \tline = [%d]\n", __func__, __LINE__);
1201
1202         dev->online = 0;
1203         dev->error = 1;
1204
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);
1210
1211         usb_ep_disable(mtp_func->int_in);
1212         mtp_func->int_in->driver_data = NULL;
1213
1214         usb_ep_disable(mtp_func->bulk_in);
1215         mtp_func->bulk_in->driver_data = NULL;
1216
1217         usb_ep_disable(mtp_func->bulk_out);
1218         mtp_func->bulk_out->driver_data = NULL;
1219
1220         wake_up(&dev->read_wq);
1221         wake_up(&dev->write_wq);
1222         wake_up(&dev->intr_wq);
1223 }
1224
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)
1227 {
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__);
1233                 return;
1234         }
1235
1236         if (req->length != USB_PTPREQUEST_CANCELIO_SIZE)
1237                 usb_ep_set_halt(ep);
1238         else {
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);
1243         }
1244 }
1245
1246 static int mtpg_function_setup(struct usb_function *f,
1247                                const struct usb_ctrlrequest *ctrl)
1248 {
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);
1257
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__);
1264                 if (w_value == 0x00
1265                     && w_index == mtpg_interface_desc.bInterfaceNumber
1266                     && w_length == 0x06) {
1267                         value = w_length;
1268                         cdev->gadget->ep0->driver_data = dev;
1269                         req->complete = mtp_complete_cancel_io;
1270                         req->zero = 0;
1271                         req->length = value;
1272                         value =
1273                             usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
1274                 }
1275                 return value;
1276                 break;
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;
1282                 break;
1283
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;
1289                 break;
1290
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;
1296                 break;
1297         default:
1298                 DEBUG_MTPB("[%s] INVALID REQUEST \tline = [%d]\n",
1299                         __func__, __LINE__);
1300                 return value;
1301         }
1302         value = mtp_send_signal(signal_request);
1303         return value;
1304 }
1305
1306 static int mtp_bind_config(struct usb_configuration *c)
1307 {
1308         int rc;
1309         struct f_mtp *mtp_func;
1310
1311         DEBUG_MTPB("[%s] \tline = [%d]\n", __func__, __LINE__);
1312
1313         if (!the_mtpg) {
1314                 pr_err("Error There is no the_mtpg!!\n");
1315                 return -ENODEV;
1316         }
1317
1318         mtp_func = kzalloc(sizeof(*mtp_func), GFP_KERNEL);
1319         if (!mtp_func) {
1320                 pr_err("mtp_func memory alloc failed !!!\n");
1321                 return -ENOMEM;
1322         }
1323
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);
1327
1328         mtp_func->function.name = mtp_longname;
1329         mtp_func->function.strings = mtp_dev_strings;
1330
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;
1336
1337         rc = usb_add_function(c, &mtp_func->function);
1338         if (rc != 0)
1339                 pr_err("Error in usb_add_function Failed !!!\n");
1340
1341         return rc;
1342 }
1343
1344 static int mtp_setup(struct usb_composite_dev *cdev)
1345 {
1346         struct mtpg_dev *mtpg;
1347         int status;
1348
1349         mtpg = kzalloc(sizeof(*mtpg), GFP_KERNEL);
1350         if (!mtpg) {
1351                 pr_err("mtpg_dev_alloc memory  failed !!!\n");
1352                 return -ENOMEM;
1353         }
1354
1355         if (strings_dev_mtp[F_MTP_IDX].id == 0) {
1356                 status = usb_string_id(cdev);
1357                 if (status < 0) {
1358                         kfree(mtpg);
1359                         return status;
1360                 }
1361                 strings_dev_mtp[F_MTP_IDX].id = status;
1362                 mtpg_interface_desc.iInterface = status;
1363         }
1364
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);
1369
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);
1374
1375         INIT_LIST_HEAD(&mtpg->rx_done);
1376
1377         /* the_mtpg must be set before calling usb_gadget_register_driver */
1378         the_mtpg = mtpg;
1379
1380         status = misc_register(&mtpg_device);
1381         if (status != 0) {
1382                 pr_err("Error in misc_register of mtpg_device Failed !!!\n");
1383                 kfree(mtpg);
1384                 the_mtpg = NULL;
1385         }
1386
1387         return status;
1388 }
1389
1390 static void mtp_cleanup(void)
1391 {
1392         struct mtpg_dev *mtpg = the_mtpg;
1393
1394         misc_deregister(&mtpg_device);
1395
1396         if (!mtpg)
1397                 return;
1398         the_mtpg = NULL;
1399         kfree(mtpg);
1400 }
1401
1402 MODULE_AUTHOR("Deepak And Madhukar");
1403 MODULE_LICENSE("GPL");