blob: 82ffbbae2935dceeaa23ed84776091437b944ea4 [file] [log] [blame]
Benoit Gobyf0fbc482011-12-19 14:37:50 -08001/*
2 * Gadget Function Driver for MTP
3 *
4 * Copyright (C) 2010 Google, Inc.
5 * Author: Mike Lockwood <lockwood@android.com>
6 *
7 * This software is licensed under the terms of the GNU General Public
8 * License version 2, as published by the Free Software Foundation, and
9 * may be copied, distributed, and modified under those terms.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 */
17
18/* #define DEBUG */
19/* #define VERBOSE_DEBUG */
20
21#include <linux/module.h>
22#include <linux/init.h>
23#include <linux/poll.h>
24#include <linux/delay.h>
25#include <linux/wait.h>
26#include <linux/err.h>
27#include <linux/interrupt.h>
28
29#include <linux/types.h>
30#include <linux/file.h>
31#include <linux/device.h>
32#include <linux/miscdevice.h>
33
34#include <linux/usb.h>
35#include <linux/usb_usual.h>
36#include <linux/usb/ch9.h>
37#include <linux/usb/f_mtp.h>
38
39#define MTP_BULK_BUFFER_SIZE 16384
40#define INTR_BUFFER_SIZE 28
41
42/* String IDs */
43#define INTERFACE_STRING_INDEX 0
44
45/* values for mtp_dev.state */
46#define STATE_OFFLINE 0 /* initial state, disconnected */
47#define STATE_READY 1 /* ready for userspace calls */
48#define STATE_BUSY 2 /* processing userspace calls */
49#define STATE_CANCELED 3 /* transaction canceled by host */
50#define STATE_ERROR 4 /* error from completion routine */
51
52/* number of tx and rx requests to allocate */
53#define TX_REQ_MAX 4
54#define RX_REQ_MAX 2
55#define INTR_REQ_MAX 5
56
57/* ID for Microsoft MTP OS String */
58#define MTP_OS_STRING_ID 0xEE
59
60/* MTP class reqeusts */
61#define MTP_REQ_CANCEL 0x64
62#define MTP_REQ_GET_EXT_EVENT_DATA 0x65
63#define MTP_REQ_RESET 0x66
64#define MTP_REQ_GET_DEVICE_STATUS 0x67
65
66/* constants for device status */
67#define MTP_RESPONSE_OK 0x2001
68#define MTP_RESPONSE_DEVICE_BUSY 0x2019
69
70static const char mtp_shortname[] = "mtp_usb";
71
72struct mtp_dev {
73 struct usb_function function;
74 struct usb_composite_dev *cdev;
75 spinlock_t lock;
76
77 struct usb_ep *ep_in;
78 struct usb_ep *ep_out;
79 struct usb_ep *ep_intr;
80
81 int state;
82
83 /* synchronize access to our device file */
84 atomic_t open_excl;
85 /* to enforce only one ioctl at a time */
86 atomic_t ioctl_excl;
87
88 struct list_head tx_idle;
89 struct list_head intr_idle;
90
91 wait_queue_head_t read_wq;
92 wait_queue_head_t write_wq;
93 wait_queue_head_t intr_wq;
94 struct usb_request *rx_req[RX_REQ_MAX];
95 int rx_done;
96
97 /* for processing MTP_SEND_FILE, MTP_RECEIVE_FILE and
98 * MTP_SEND_FILE_WITH_HEADER ioctls on a work queue
99 */
100 struct workqueue_struct *wq;
101 struct work_struct send_file_work;
102 struct work_struct receive_file_work;
103 struct file *xfer_file;
104 loff_t xfer_file_offset;
105 int64_t xfer_file_length;
106 unsigned xfer_send_header;
107 uint16_t xfer_command;
108 uint32_t xfer_transaction_id;
109 int xfer_result;
110};
111
112static struct usb_interface_descriptor mtp_interface_desc = {
113 .bLength = USB_DT_INTERFACE_SIZE,
114 .bDescriptorType = USB_DT_INTERFACE,
115 .bInterfaceNumber = 0,
116 .bNumEndpoints = 3,
117 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
118 .bInterfaceSubClass = USB_SUBCLASS_VENDOR_SPEC,
119 .bInterfaceProtocol = 0,
120};
121
122static struct usb_interface_descriptor ptp_interface_desc = {
123 .bLength = USB_DT_INTERFACE_SIZE,
124 .bDescriptorType = USB_DT_INTERFACE,
125 .bInterfaceNumber = 0,
126 .bNumEndpoints = 3,
127 .bInterfaceClass = USB_CLASS_STILL_IMAGE,
128 .bInterfaceSubClass = 1,
129 .bInterfaceProtocol = 1,
130};
131
Pavankumar Kondeti6f94bc92012-08-03 09:34:32 +0530132static struct usb_endpoint_descriptor mtp_superspeed_in_desc = {
133 .bLength = USB_DT_ENDPOINT_SIZE,
134 .bDescriptorType = USB_DT_ENDPOINT,
135 .bEndpointAddress = USB_DIR_IN,
136 .bmAttributes = USB_ENDPOINT_XFER_BULK,
137 .wMaxPacketSize = __constant_cpu_to_le16(1024),
138};
139
140static struct usb_ss_ep_comp_descriptor mtp_superspeed_in_comp_desc = {
141 .bLength = sizeof mtp_superspeed_in_comp_desc,
142 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
143
144 /* the following 2 values can be tweaked if necessary */
145 /* .bMaxBurst = 0, */
146 /* .bmAttributes = 0, */
147};
148
149static struct usb_endpoint_descriptor mtp_superspeed_out_desc = {
150 .bLength = USB_DT_ENDPOINT_SIZE,
151 .bDescriptorType = USB_DT_ENDPOINT,
152 .bEndpointAddress = USB_DIR_OUT,
153 .bmAttributes = USB_ENDPOINT_XFER_BULK,
154 .wMaxPacketSize = __constant_cpu_to_le16(1024),
155};
156
157static struct usb_ss_ep_comp_descriptor mtp_superspeed_out_comp_desc = {
158 .bLength = sizeof mtp_superspeed_out_comp_desc,
159 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
160
161 /* the following 2 values can be tweaked if necessary */
162 /* .bMaxBurst = 0, */
163 /* .bmAttributes = 0, */
164};
165
Benoit Gobyf0fbc482011-12-19 14:37:50 -0800166static struct usb_endpoint_descriptor mtp_highspeed_in_desc = {
167 .bLength = USB_DT_ENDPOINT_SIZE,
168 .bDescriptorType = USB_DT_ENDPOINT,
169 .bEndpointAddress = USB_DIR_IN,
170 .bmAttributes = USB_ENDPOINT_XFER_BULK,
171 .wMaxPacketSize = __constant_cpu_to_le16(512),
172};
173
174static struct usb_endpoint_descriptor mtp_highspeed_out_desc = {
175 .bLength = USB_DT_ENDPOINT_SIZE,
176 .bDescriptorType = USB_DT_ENDPOINT,
177 .bEndpointAddress = USB_DIR_OUT,
178 .bmAttributes = USB_ENDPOINT_XFER_BULK,
179 .wMaxPacketSize = __constant_cpu_to_le16(512),
180};
181
182static struct usb_endpoint_descriptor mtp_fullspeed_in_desc = {
183 .bLength = USB_DT_ENDPOINT_SIZE,
184 .bDescriptorType = USB_DT_ENDPOINT,
185 .bEndpointAddress = USB_DIR_IN,
186 .bmAttributes = USB_ENDPOINT_XFER_BULK,
187};
188
189static struct usb_endpoint_descriptor mtp_fullspeed_out_desc = {
190 .bLength = USB_DT_ENDPOINT_SIZE,
191 .bDescriptorType = USB_DT_ENDPOINT,
192 .bEndpointAddress = USB_DIR_OUT,
193 .bmAttributes = USB_ENDPOINT_XFER_BULK,
194};
195
196static struct usb_endpoint_descriptor mtp_intr_desc = {
197 .bLength = USB_DT_ENDPOINT_SIZE,
198 .bDescriptorType = USB_DT_ENDPOINT,
199 .bEndpointAddress = USB_DIR_IN,
200 .bmAttributes = USB_ENDPOINT_XFER_INT,
201 .wMaxPacketSize = __constant_cpu_to_le16(INTR_BUFFER_SIZE),
202 .bInterval = 6,
203};
204
Pavankumar Kondeti6f94bc92012-08-03 09:34:32 +0530205static struct usb_ss_ep_comp_descriptor mtp_superspeed_intr_comp_desc = {
206 .bLength = sizeof mtp_superspeed_intr_comp_desc,
207 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
208
209 /* the following 3 values can be tweaked if necessary */
210 /* .bMaxBurst = 0, */
211 /* .bmAttributes = 0, */
212 .wBytesPerInterval = cpu_to_le16(INTR_BUFFER_SIZE),
213};
214
Benoit Gobyf0fbc482011-12-19 14:37:50 -0800215static struct usb_descriptor_header *fs_mtp_descs[] = {
216 (struct usb_descriptor_header *) &mtp_interface_desc,
217 (struct usb_descriptor_header *) &mtp_fullspeed_in_desc,
218 (struct usb_descriptor_header *) &mtp_fullspeed_out_desc,
219 (struct usb_descriptor_header *) &mtp_intr_desc,
220 NULL,
221};
222
223static struct usb_descriptor_header *hs_mtp_descs[] = {
224 (struct usb_descriptor_header *) &mtp_interface_desc,
225 (struct usb_descriptor_header *) &mtp_highspeed_in_desc,
226 (struct usb_descriptor_header *) &mtp_highspeed_out_desc,
227 (struct usb_descriptor_header *) &mtp_intr_desc,
228 NULL,
229};
230
Pavankumar Kondeti6f94bc92012-08-03 09:34:32 +0530231static struct usb_descriptor_header *ss_mtp_descs[] = {
232 (struct usb_descriptor_header *) &mtp_interface_desc,
233 (struct usb_descriptor_header *) &mtp_superspeed_in_desc,
234 (struct usb_descriptor_header *) &mtp_superspeed_in_comp_desc,
235 (struct usb_descriptor_header *) &mtp_superspeed_out_desc,
236 (struct usb_descriptor_header *) &mtp_superspeed_out_comp_desc,
237 (struct usb_descriptor_header *) &mtp_intr_desc,
238 (struct usb_descriptor_header *) &mtp_superspeed_intr_comp_desc,
239 NULL,
240};
241
Benoit Gobyf0fbc482011-12-19 14:37:50 -0800242static struct usb_descriptor_header *fs_ptp_descs[] = {
243 (struct usb_descriptor_header *) &ptp_interface_desc,
244 (struct usb_descriptor_header *) &mtp_fullspeed_in_desc,
245 (struct usb_descriptor_header *) &mtp_fullspeed_out_desc,
246 (struct usb_descriptor_header *) &mtp_intr_desc,
247 NULL,
248};
249
250static struct usb_descriptor_header *hs_ptp_descs[] = {
251 (struct usb_descriptor_header *) &ptp_interface_desc,
252 (struct usb_descriptor_header *) &mtp_highspeed_in_desc,
253 (struct usb_descriptor_header *) &mtp_highspeed_out_desc,
254 (struct usb_descriptor_header *) &mtp_intr_desc,
255 NULL,
256};
257
Pavankumar Kondeti6f94bc92012-08-03 09:34:32 +0530258static struct usb_descriptor_header *ss_ptp_descs[] = {
259 (struct usb_descriptor_header *) &ptp_interface_desc,
260 (struct usb_descriptor_header *) &mtp_superspeed_in_desc,
261 (struct usb_descriptor_header *) &mtp_superspeed_in_comp_desc,
262 (struct usb_descriptor_header *) &mtp_superspeed_out_desc,
263 (struct usb_descriptor_header *) &mtp_superspeed_out_comp_desc,
264 (struct usb_descriptor_header *) &mtp_intr_desc,
265 (struct usb_descriptor_header *) &mtp_superspeed_intr_comp_desc,
266 NULL,
267};
268
Benoit Gobyf0fbc482011-12-19 14:37:50 -0800269static struct usb_string mtp_string_defs[] = {
270 /* Naming interface "MTP" so libmtp will recognize us */
271 [INTERFACE_STRING_INDEX].s = "MTP",
272 { }, /* end of list */
273};
274
275static struct usb_gadget_strings mtp_string_table = {
276 .language = 0x0409, /* en-US */
277 .strings = mtp_string_defs,
278};
279
280static struct usb_gadget_strings *mtp_strings[] = {
281 &mtp_string_table,
282 NULL,
283};
284
285/* Microsoft MTP OS String */
286static u8 mtp_os_string[] = {
287 18, /* sizeof(mtp_os_string) */
288 USB_DT_STRING,
289 /* Signature field: "MSFT100" */
290 'M', 0, 'S', 0, 'F', 0, 'T', 0, '1', 0, '0', 0, '0', 0,
291 /* vendor code */
292 1,
293 /* padding */
294 0
295};
296
297/* Microsoft Extended Configuration Descriptor Header Section */
298struct mtp_ext_config_desc_header {
299 __le32 dwLength;
300 __u16 bcdVersion;
301 __le16 wIndex;
302 __u8 bCount;
303 __u8 reserved[7];
304};
305
306/* Microsoft Extended Configuration Descriptor Function Section */
307struct mtp_ext_config_desc_function {
308 __u8 bFirstInterfaceNumber;
309 __u8 bInterfaceCount;
310 __u8 compatibleID[8];
311 __u8 subCompatibleID[8];
312 __u8 reserved[6];
313};
314
315/* MTP Extended Configuration Descriptor */
316struct {
317 struct mtp_ext_config_desc_header header;
318 struct mtp_ext_config_desc_function function;
319} mtp_ext_config_desc = {
320 .header = {
321 .dwLength = __constant_cpu_to_le32(sizeof(mtp_ext_config_desc)),
322 .bcdVersion = __constant_cpu_to_le16(0x0100),
323 .wIndex = __constant_cpu_to_le16(4),
324 .bCount = __constant_cpu_to_le16(1),
325 },
326 .function = {
327 .bFirstInterfaceNumber = 0,
328 .bInterfaceCount = 1,
329 .compatibleID = { 'M', 'T', 'P' },
330 },
331};
332
333struct mtp_device_status {
334 __le16 wLength;
335 __le16 wCode;
336};
337
338/* temporary variable used between mtp_open() and mtp_gadget_bind() */
339static struct mtp_dev *_mtp_dev;
340
341static inline struct mtp_dev *func_to_mtp(struct usb_function *f)
342{
343 return container_of(f, struct mtp_dev, function);
344}
345
346static struct usb_request *mtp_request_new(struct usb_ep *ep, int buffer_size)
347{
348 struct usb_request *req = usb_ep_alloc_request(ep, GFP_KERNEL);
349 if (!req)
350 return NULL;
351
352 /* now allocate buffers for the requests */
353 req->buf = kmalloc(buffer_size, GFP_KERNEL);
354 if (!req->buf) {
355 usb_ep_free_request(ep, req);
356 return NULL;
357 }
358
359 return req;
360}
361
362static void mtp_request_free(struct usb_request *req, struct usb_ep *ep)
363{
364 if (req) {
365 kfree(req->buf);
366 usb_ep_free_request(ep, req);
367 }
368}
369
370static inline int mtp_lock(atomic_t *excl)
371{
372 if (atomic_inc_return(excl) == 1) {
373 return 0;
374 } else {
375 atomic_dec(excl);
376 return -1;
377 }
378}
379
380static inline void mtp_unlock(atomic_t *excl)
381{
382 atomic_dec(excl);
383}
384
385/* add a request to the tail of a list */
386static void mtp_req_put(struct mtp_dev *dev, struct list_head *head,
387 struct usb_request *req)
388{
389 unsigned long flags;
390
391 spin_lock_irqsave(&dev->lock, flags);
392 list_add_tail(&req->list, head);
393 spin_unlock_irqrestore(&dev->lock, flags);
394}
395
396/* remove a request from the head of a list */
397static struct usb_request
398*mtp_req_get(struct mtp_dev *dev, struct list_head *head)
399{
400 unsigned long flags;
401 struct usb_request *req;
402
403 spin_lock_irqsave(&dev->lock, flags);
404 if (list_empty(head)) {
405 req = 0;
406 } else {
407 req = list_first_entry(head, struct usb_request, list);
408 list_del(&req->list);
409 }
410 spin_unlock_irqrestore(&dev->lock, flags);
411 return req;
412}
413
414static void mtp_complete_in(struct usb_ep *ep, struct usb_request *req)
415{
416 struct mtp_dev *dev = _mtp_dev;
417
418 if (req->status != 0)
419 dev->state = STATE_ERROR;
420
421 mtp_req_put(dev, &dev->tx_idle, req);
422
423 wake_up(&dev->write_wq);
424}
425
426static void mtp_complete_out(struct usb_ep *ep, struct usb_request *req)
427{
428 struct mtp_dev *dev = _mtp_dev;
429
430 dev->rx_done = 1;
431 if (req->status != 0)
432 dev->state = STATE_ERROR;
433
434 wake_up(&dev->read_wq);
435}
436
437static void mtp_complete_intr(struct usb_ep *ep, struct usb_request *req)
438{
439 struct mtp_dev *dev = _mtp_dev;
440
441 if (req->status != 0)
442 dev->state = STATE_ERROR;
443
444 mtp_req_put(dev, &dev->intr_idle, req);
445
446 wake_up(&dev->intr_wq);
447}
448
449static int mtp_create_bulk_endpoints(struct mtp_dev *dev,
450 struct usb_endpoint_descriptor *in_desc,
451 struct usb_endpoint_descriptor *out_desc,
452 struct usb_endpoint_descriptor *intr_desc)
453{
454 struct usb_composite_dev *cdev = dev->cdev;
455 struct usb_request *req;
456 struct usb_ep *ep;
457 int i;
458
459 DBG(cdev, "create_bulk_endpoints dev: %p\n", dev);
460
461 ep = usb_ep_autoconfig(cdev->gadget, in_desc);
462 if (!ep) {
463 DBG(cdev, "usb_ep_autoconfig for ep_in failed\n");
464 return -ENODEV;
465 }
466 DBG(cdev, "usb_ep_autoconfig for ep_in got %s\n", ep->name);
467 ep->driver_data = dev; /* claim the endpoint */
468 dev->ep_in = ep;
469
470 ep = usb_ep_autoconfig(cdev->gadget, out_desc);
471 if (!ep) {
472 DBG(cdev, "usb_ep_autoconfig for ep_out failed\n");
473 return -ENODEV;
474 }
475 DBG(cdev, "usb_ep_autoconfig for mtp ep_out got %s\n", ep->name);
476 ep->driver_data = dev; /* claim the endpoint */
477 dev->ep_out = ep;
478
Benoit Gobyf0fbc482011-12-19 14:37:50 -0800479 ep = usb_ep_autoconfig(cdev->gadget, intr_desc);
480 if (!ep) {
481 DBG(cdev, "usb_ep_autoconfig for ep_intr failed\n");
482 return -ENODEV;
483 }
484 DBG(cdev, "usb_ep_autoconfig for mtp ep_intr got %s\n", ep->name);
485 ep->driver_data = dev; /* claim the endpoint */
486 dev->ep_intr = ep;
487
488 /* now allocate requests for our endpoints */
489 for (i = 0; i < TX_REQ_MAX; i++) {
490 req = mtp_request_new(dev->ep_in, MTP_BULK_BUFFER_SIZE);
491 if (!req)
492 goto fail;
493 req->complete = mtp_complete_in;
494 mtp_req_put(dev, &dev->tx_idle, req);
495 }
496 for (i = 0; i < RX_REQ_MAX; i++) {
497 req = mtp_request_new(dev->ep_out, MTP_BULK_BUFFER_SIZE);
498 if (!req)
499 goto fail;
500 req->complete = mtp_complete_out;
501 dev->rx_req[i] = req;
502 }
503 for (i = 0; i < INTR_REQ_MAX; i++) {
504 req = mtp_request_new(dev->ep_intr, INTR_BUFFER_SIZE);
505 if (!req)
506 goto fail;
507 req->complete = mtp_complete_intr;
508 mtp_req_put(dev, &dev->intr_idle, req);
509 }
510
511 return 0;
512
513fail:
514 printk(KERN_ERR "mtp_bind() could not allocate requests\n");
515 return -1;
516}
517
518static ssize_t mtp_read(struct file *fp, char __user *buf,
519 size_t count, loff_t *pos)
520{
521 struct mtp_dev *dev = fp->private_data;
522 struct usb_composite_dev *cdev = dev->cdev;
523 struct usb_request *req;
524 int r = count, xfer;
525 int ret = 0;
526
527 DBG(cdev, "mtp_read(%d)\n", count);
528
529 if (count > MTP_BULK_BUFFER_SIZE)
530 return -EINVAL;
531
Manu Gautambe803b42012-10-03 18:49:33 +0530532 if (!IS_ALIGNED(count, dev->ep_out->maxpacket))
533 DBG(cdev, "%s - count(%d) not multiple of mtu(%d)\n", __func__,
534 count, dev->ep_out->maxpacket);
535
Benoit Gobyf0fbc482011-12-19 14:37:50 -0800536 /* we will block until we're online */
537 DBG(cdev, "mtp_read: waiting for online state\n");
538 ret = wait_event_interruptible(dev->read_wq,
539 dev->state != STATE_OFFLINE);
540 if (ret < 0) {
541 r = ret;
542 goto done;
543 }
544 spin_lock_irq(&dev->lock);
545 if (dev->state == STATE_CANCELED) {
546 /* report cancelation to userspace */
547 dev->state = STATE_READY;
548 spin_unlock_irq(&dev->lock);
549 return -ECANCELED;
550 }
551 dev->state = STATE_BUSY;
552 spin_unlock_irq(&dev->lock);
553
554requeue_req:
555 /* queue a request */
556 req = dev->rx_req[0];
Manu Gautambe803b42012-10-03 18:49:33 +0530557 req->length = MTP_BULK_BUFFER_SIZE;
Benoit Gobyf0fbc482011-12-19 14:37:50 -0800558 dev->rx_done = 0;
559 ret = usb_ep_queue(dev->ep_out, req, GFP_KERNEL);
560 if (ret < 0) {
561 r = -EIO;
562 goto done;
563 } else {
564 DBG(cdev, "rx %p queue\n", req);
565 }
566
567 /* wait for a request to complete */
Rajkumar Raghupathy20298e02012-03-09 12:22:36 +0530568 ret = wait_event_interruptible(dev->read_wq,
569 dev->rx_done || dev->state != STATE_BUSY);
570 if (dev->state == STATE_CANCELED) {
571 r = -ECANCELED;
572 if (!dev->rx_done)
573 usb_ep_dequeue(dev->ep_out, req);
574 spin_lock_irq(&dev->lock);
575 dev->state = STATE_CANCELED;
576 spin_unlock_irq(&dev->lock);
577 goto done;
578 }
Benoit Gobyf0fbc482011-12-19 14:37:50 -0800579 if (ret < 0) {
580 r = ret;
581 usb_ep_dequeue(dev->ep_out, req);
582 goto done;
583 }
584 if (dev->state == STATE_BUSY) {
585 /* If we got a 0-len packet, throw it back and try again. */
586 if (req->actual == 0)
587 goto requeue_req;
588
589 DBG(cdev, "rx %p %d\n", req, req->actual);
590 xfer = (req->actual < count) ? req->actual : count;
591 r = xfer;
592 if (copy_to_user(buf, req->buf, xfer))
593 r = -EFAULT;
594 } else
595 r = -EIO;
596
597done:
598 spin_lock_irq(&dev->lock);
599 if (dev->state == STATE_CANCELED)
600 r = -ECANCELED;
601 else if (dev->state != STATE_OFFLINE)
602 dev->state = STATE_READY;
603 spin_unlock_irq(&dev->lock);
604
605 DBG(cdev, "mtp_read returning %d\n", r);
606 return r;
607}
608
609static ssize_t mtp_write(struct file *fp, const char __user *buf,
610 size_t count, loff_t *pos)
611{
612 struct mtp_dev *dev = fp->private_data;
613 struct usb_composite_dev *cdev = dev->cdev;
614 struct usb_request *req = 0;
615 int r = count, xfer;
616 int sendZLP = 0;
617 int ret;
618
619 DBG(cdev, "mtp_write(%d)\n", count);
620
621 spin_lock_irq(&dev->lock);
622 if (dev->state == STATE_CANCELED) {
623 /* report cancelation to userspace */
624 dev->state = STATE_READY;
625 spin_unlock_irq(&dev->lock);
626 return -ECANCELED;
627 }
628 if (dev->state == STATE_OFFLINE) {
629 spin_unlock_irq(&dev->lock);
630 return -ENODEV;
631 }
632 dev->state = STATE_BUSY;
633 spin_unlock_irq(&dev->lock);
634
635 /* we need to send a zero length packet to signal the end of transfer
636 * if the transfer size is aligned to a packet boundary.
637 */
638 if ((count & (dev->ep_in->maxpacket - 1)) == 0)
639 sendZLP = 1;
640
641 while (count > 0 || sendZLP) {
642 /* so we exit after sending ZLP */
643 if (count == 0)
644 sendZLP = 0;
645
646 if (dev->state != STATE_BUSY) {
647 DBG(cdev, "mtp_write dev->error\n");
648 r = -EIO;
649 break;
650 }
651
652 /* get an idle tx request to use */
653 req = 0;
654 ret = wait_event_interruptible(dev->write_wq,
655 ((req = mtp_req_get(dev, &dev->tx_idle))
656 || dev->state != STATE_BUSY));
657 if (!req) {
658 r = ret;
659 break;
660 }
661
662 if (count > MTP_BULK_BUFFER_SIZE)
663 xfer = MTP_BULK_BUFFER_SIZE;
664 else
665 xfer = count;
666 if (xfer && copy_from_user(req->buf, buf, xfer)) {
667 r = -EFAULT;
668 break;
669 }
670
671 req->length = xfer;
672 ret = usb_ep_queue(dev->ep_in, req, GFP_KERNEL);
673 if (ret < 0) {
674 DBG(cdev, "mtp_write: xfer error %d\n", ret);
675 r = -EIO;
676 break;
677 }
678
679 buf += xfer;
680 count -= xfer;
681
682 /* zero this so we don't try to free it on error exit */
683 req = 0;
684 }
685
686 if (req)
687 mtp_req_put(dev, &dev->tx_idle, req);
688
689 spin_lock_irq(&dev->lock);
690 if (dev->state == STATE_CANCELED)
691 r = -ECANCELED;
692 else if (dev->state != STATE_OFFLINE)
693 dev->state = STATE_READY;
694 spin_unlock_irq(&dev->lock);
695
696 DBG(cdev, "mtp_write returning %d\n", r);
697 return r;
698}
699
700/* read from a local file and write to USB */
701static void send_file_work(struct work_struct *data)
702{
703 struct mtp_dev *dev = container_of(data, struct mtp_dev,
704 send_file_work);
705 struct usb_composite_dev *cdev = dev->cdev;
706 struct usb_request *req = 0;
707 struct mtp_data_header *header;
708 struct file *filp;
709 loff_t offset;
710 int64_t count;
711 int xfer, ret, hdr_size;
712 int r = 0;
713 int sendZLP = 0;
714
715 /* read our parameters */
716 smp_rmb();
717 filp = dev->xfer_file;
718 offset = dev->xfer_file_offset;
719 count = dev->xfer_file_length;
720
721 DBG(cdev, "send_file_work(%lld %lld)\n", offset, count);
722
723 if (dev->xfer_send_header) {
724 hdr_size = sizeof(struct mtp_data_header);
725 count += hdr_size;
726 } else {
727 hdr_size = 0;
728 }
729
730 /* we need to send a zero length packet to signal the end of transfer
731 * if the transfer size is aligned to a packet boundary.
732 */
733 if ((count & (dev->ep_in->maxpacket - 1)) == 0)
734 sendZLP = 1;
735
736 while (count > 0 || sendZLP) {
737 /* so we exit after sending ZLP */
738 if (count == 0)
739 sendZLP = 0;
740
741 /* get an idle tx request to use */
742 req = 0;
743 ret = wait_event_interruptible(dev->write_wq,
744 (req = mtp_req_get(dev, &dev->tx_idle))
745 || dev->state != STATE_BUSY);
746 if (dev->state == STATE_CANCELED) {
747 r = -ECANCELED;
748 break;
749 }
750 if (!req) {
751 r = ret;
752 break;
753 }
754
755 if (count > MTP_BULK_BUFFER_SIZE)
756 xfer = MTP_BULK_BUFFER_SIZE;
757 else
758 xfer = count;
759
760 if (hdr_size) {
761 /* prepend MTP data header */
762 header = (struct mtp_data_header *)req->buf;
763 header->length = __cpu_to_le32(count);
764 header->type = __cpu_to_le16(2); /* data packet */
765 header->command = __cpu_to_le16(dev->xfer_command);
766 header->transaction_id =
767 __cpu_to_le32(dev->xfer_transaction_id);
768 }
769
770 ret = vfs_read(filp, req->buf + hdr_size, xfer - hdr_size,
771 &offset);
772 if (ret < 0) {
773 r = ret;
774 break;
775 }
776 xfer = ret + hdr_size;
777 hdr_size = 0;
778
779 req->length = xfer;
780 ret = usb_ep_queue(dev->ep_in, req, GFP_KERNEL);
781 if (ret < 0) {
782 DBG(cdev, "send_file_work: xfer error %d\n", ret);
Vijayavardhan Vennapusa00972232012-05-18 11:18:40 +0530783 if (dev->state != STATE_OFFLINE)
784 dev->state = STATE_ERROR;
Benoit Gobyf0fbc482011-12-19 14:37:50 -0800785 r = -EIO;
786 break;
787 }
788
789 count -= xfer;
790
791 /* zero this so we don't try to free it on error exit */
792 req = 0;
793 }
794
795 if (req)
796 mtp_req_put(dev, &dev->tx_idle, req);
797
798 DBG(cdev, "send_file_work returning %d\n", r);
799 /* write the result */
800 dev->xfer_result = r;
801 smp_wmb();
802}
803
804/* read from USB and write to a local file */
805static void receive_file_work(struct work_struct *data)
806{
807 struct mtp_dev *dev = container_of(data, struct mtp_dev,
808 receive_file_work);
809 struct usb_composite_dev *cdev = dev->cdev;
810 struct usb_request *read_req = NULL, *write_req = NULL;
811 struct file *filp;
812 loff_t offset;
813 int64_t count;
814 int ret, cur_buf = 0;
815 int r = 0;
816
817 /* read our parameters */
818 smp_rmb();
819 filp = dev->xfer_file;
820 offset = dev->xfer_file_offset;
821 count = dev->xfer_file_length;
822
823 DBG(cdev, "receive_file_work(%lld)\n", count);
Manu Gautambe803b42012-10-03 18:49:33 +0530824 if (!IS_ALIGNED(count, dev->ep_out->maxpacket))
825 DBG(cdev, "%s- count(%lld) not multiple of mtu(%d)\n", __func__,
826 count, dev->ep_out->maxpacket);
Benoit Gobyf0fbc482011-12-19 14:37:50 -0800827
828 while (count > 0 || write_req) {
829 if (count > 0) {
830 /* queue a request */
831 read_req = dev->rx_req[cur_buf];
832 cur_buf = (cur_buf + 1) % RX_REQ_MAX;
833
Manu Gautambe803b42012-10-03 18:49:33 +0530834 /* some h/w expects size to be aligned to ep's MTU */
835 read_req->length = MTP_BULK_BUFFER_SIZE;
836
Benoit Gobyf0fbc482011-12-19 14:37:50 -0800837 dev->rx_done = 0;
838 ret = usb_ep_queue(dev->ep_out, read_req, GFP_KERNEL);
839 if (ret < 0) {
840 r = -EIO;
Vijayavardhan Vennapusa00972232012-05-18 11:18:40 +0530841 if (dev->state != STATE_OFFLINE)
842 dev->state = STATE_ERROR;
Benoit Gobyf0fbc482011-12-19 14:37:50 -0800843 break;
844 }
845 }
846
847 if (write_req) {
848 DBG(cdev, "rx %p %d\n", write_req, write_req->actual);
849 ret = vfs_write(filp, write_req->buf, write_req->actual,
850 &offset);
851 DBG(cdev, "vfs_write %d\n", ret);
852 if (ret != write_req->actual) {
853 r = -EIO;
Vijayavardhan Vennapusa00972232012-05-18 11:18:40 +0530854 if (dev->state != STATE_OFFLINE)
855 dev->state = STATE_ERROR;
Benoit Gobyf0fbc482011-12-19 14:37:50 -0800856 break;
857 }
858 write_req = NULL;
859 }
860
861 if (read_req) {
862 /* wait for our last read to complete */
863 ret = wait_event_interruptible(dev->read_wq,
864 dev->rx_done || dev->state != STATE_BUSY);
Rajkumar Raghupathy7c3c45b2012-07-24 16:13:36 +0530865 if (dev->state == STATE_CANCELED
866 || dev->state == STATE_OFFLINE) {
Benoit Gobyf0fbc482011-12-19 14:37:50 -0800867 r = -ECANCELED;
868 if (!dev->rx_done)
869 usb_ep_dequeue(dev->ep_out, read_req);
870 break;
871 }
Manu Gautambe803b42012-10-03 18:49:33 +0530872 /* Check if we aligned the size due to MTU constraint */
873 if (count < read_req->length)
874 read_req->actual = (read_req->actual > count ?
875 count : read_req->actual);
Benoit Gobyf0fbc482011-12-19 14:37:50 -0800876 /* if xfer_file_length is 0xFFFFFFFF, then we read until
877 * we get a zero length packet
878 */
879 if (count != 0xFFFFFFFF)
880 count -= read_req->actual;
881 if (read_req->actual < read_req->length) {
882 /*
883 * short packet is used to signal EOF for
884 * sizes > 4 gig
885 */
886 DBG(cdev, "got short packet\n");
887 count = 0;
888 }
889
890 write_req = read_req;
891 read_req = NULL;
892 }
893 }
894
895 DBG(cdev, "receive_file_work returning %d\n", r);
896 /* write the result */
897 dev->xfer_result = r;
898 smp_wmb();
899}
900
901static int mtp_send_event(struct mtp_dev *dev, struct mtp_event *event)
902{
903 struct usb_request *req = NULL;
904 int ret;
905 int length = event->length;
906
907 DBG(dev->cdev, "mtp_send_event(%d)\n", event->length);
908
909 if (length < 0 || length > INTR_BUFFER_SIZE)
910 return -EINVAL;
911 if (dev->state == STATE_OFFLINE)
912 return -ENODEV;
913
914 ret = wait_event_interruptible_timeout(dev->intr_wq,
915 (req = mtp_req_get(dev, &dev->intr_idle)),
916 msecs_to_jiffies(1000));
917 if (!req)
918 return -ETIME;
919
920 if (copy_from_user(req->buf, (void __user *)event->data, length)) {
921 mtp_req_put(dev, &dev->intr_idle, req);
922 return -EFAULT;
923 }
924 req->length = length;
925 ret = usb_ep_queue(dev->ep_intr, req, GFP_KERNEL);
926 if (ret)
927 mtp_req_put(dev, &dev->intr_idle, req);
928
929 return ret;
930}
931
932static long mtp_ioctl(struct file *fp, unsigned code, unsigned long value)
933{
934 struct mtp_dev *dev = fp->private_data;
935 struct file *filp = NULL;
936 int ret = -EINVAL;
937
938 if (mtp_lock(&dev->ioctl_excl))
939 return -EBUSY;
940
941 switch (code) {
942 case MTP_SEND_FILE:
943 case MTP_RECEIVE_FILE:
944 case MTP_SEND_FILE_WITH_HEADER:
945 {
946 struct mtp_file_range mfr;
947 struct work_struct *work;
948
949 spin_lock_irq(&dev->lock);
950 if (dev->state == STATE_CANCELED) {
951 /* report cancelation to userspace */
952 dev->state = STATE_READY;
953 spin_unlock_irq(&dev->lock);
954 ret = -ECANCELED;
955 goto out;
956 }
957 if (dev->state == STATE_OFFLINE) {
958 spin_unlock_irq(&dev->lock);
959 ret = -ENODEV;
960 goto out;
961 }
962 dev->state = STATE_BUSY;
963 spin_unlock_irq(&dev->lock);
964
965 if (copy_from_user(&mfr, (void __user *)value, sizeof(mfr))) {
966 ret = -EFAULT;
967 goto fail;
968 }
969 /* hold a reference to the file while we are working with it */
970 filp = fget(mfr.fd);
971 if (!filp) {
972 ret = -EBADF;
973 goto fail;
974 }
975
976 /* write the parameters */
977 dev->xfer_file = filp;
978 dev->xfer_file_offset = mfr.offset;
979 dev->xfer_file_length = mfr.length;
980 smp_wmb();
981
982 if (code == MTP_SEND_FILE_WITH_HEADER) {
983 work = &dev->send_file_work;
984 dev->xfer_send_header = 1;
985 dev->xfer_command = mfr.command;
986 dev->xfer_transaction_id = mfr.transaction_id;
987 } else if (code == MTP_SEND_FILE) {
988 work = &dev->send_file_work;
989 dev->xfer_send_header = 0;
990 } else {
991 work = &dev->receive_file_work;
992 }
993
994 /* We do the file transfer on a work queue so it will run
995 * in kernel context, which is necessary for vfs_read and
996 * vfs_write to use our buffers in the kernel address space.
997 */
998 queue_work(dev->wq, work);
999 /* wait for operation to complete */
1000 flush_workqueue(dev->wq);
1001 fput(filp);
1002
1003 /* read the result */
1004 smp_rmb();
1005 ret = dev->xfer_result;
1006 break;
1007 }
1008 case MTP_SEND_EVENT:
1009 {
1010 struct mtp_event event;
1011 /* return here so we don't change dev->state below,
1012 * which would interfere with bulk transfer state.
1013 */
1014 if (copy_from_user(&event, (void __user *)value, sizeof(event)))
1015 ret = -EFAULT;
1016 else
1017 ret = mtp_send_event(dev, &event);
1018 goto out;
1019 }
1020 }
1021
1022fail:
1023 spin_lock_irq(&dev->lock);
1024 if (dev->state == STATE_CANCELED)
1025 ret = -ECANCELED;
1026 else if (dev->state != STATE_OFFLINE)
1027 dev->state = STATE_READY;
1028 spin_unlock_irq(&dev->lock);
1029out:
1030 mtp_unlock(&dev->ioctl_excl);
1031 DBG(dev->cdev, "ioctl returning %d\n", ret);
1032 return ret;
1033}
1034
1035static int mtp_open(struct inode *ip, struct file *fp)
1036{
1037 printk(KERN_INFO "mtp_open\n");
1038 if (mtp_lock(&_mtp_dev->open_excl))
1039 return -EBUSY;
1040
1041 /* clear any error condition */
1042 if (_mtp_dev->state != STATE_OFFLINE)
1043 _mtp_dev->state = STATE_READY;
1044
1045 fp->private_data = _mtp_dev;
1046 return 0;
1047}
1048
1049static int mtp_release(struct inode *ip, struct file *fp)
1050{
1051 printk(KERN_INFO "mtp_release\n");
1052
1053 mtp_unlock(&_mtp_dev->open_excl);
1054 return 0;
1055}
1056
1057/* file operations for /dev/mtp_usb */
1058static const struct file_operations mtp_fops = {
1059 .owner = THIS_MODULE,
1060 .read = mtp_read,
1061 .write = mtp_write,
1062 .unlocked_ioctl = mtp_ioctl,
1063 .open = mtp_open,
1064 .release = mtp_release,
1065};
1066
1067static struct miscdevice mtp_device = {
1068 .minor = MISC_DYNAMIC_MINOR,
1069 .name = mtp_shortname,
1070 .fops = &mtp_fops,
1071};
1072
1073static int mtp_ctrlrequest(struct usb_composite_dev *cdev,
1074 const struct usb_ctrlrequest *ctrl)
1075{
1076 struct mtp_dev *dev = _mtp_dev;
1077 int value = -EOPNOTSUPP;
1078 u16 w_index = le16_to_cpu(ctrl->wIndex);
1079 u16 w_value = le16_to_cpu(ctrl->wValue);
1080 u16 w_length = le16_to_cpu(ctrl->wLength);
1081 unsigned long flags;
1082
1083 VDBG(cdev, "mtp_ctrlrequest "
1084 "%02x.%02x v%04x i%04x l%u\n",
1085 ctrl->bRequestType, ctrl->bRequest,
1086 w_value, w_index, w_length);
1087
1088 /* Handle MTP OS string */
1089 if (ctrl->bRequestType ==
1090 (USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE)
1091 && ctrl->bRequest == USB_REQ_GET_DESCRIPTOR
1092 && (w_value >> 8) == USB_DT_STRING
1093 && (w_value & 0xFF) == MTP_OS_STRING_ID) {
1094 value = (w_length < sizeof(mtp_os_string)
1095 ? w_length : sizeof(mtp_os_string));
1096 memcpy(cdev->req->buf, mtp_os_string, value);
1097 } else if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_VENDOR) {
1098 /* Handle MTP OS descriptor */
1099 DBG(cdev, "vendor request: %d index: %d value: %d length: %d\n",
1100 ctrl->bRequest, w_index, w_value, w_length);
1101
1102 if (ctrl->bRequest == 1
1103 && (ctrl->bRequestType & USB_DIR_IN)
1104 && (w_index == 4 || w_index == 5)) {
1105 value = (w_length < sizeof(mtp_ext_config_desc) ?
1106 w_length : sizeof(mtp_ext_config_desc));
1107 memcpy(cdev->req->buf, &mtp_ext_config_desc, value);
1108 }
1109 } else if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_CLASS) {
1110 DBG(cdev, "class request: %d index: %d value: %d length: %d\n",
1111 ctrl->bRequest, w_index, w_value, w_length);
1112
1113 if (ctrl->bRequest == MTP_REQ_CANCEL && w_index == 0
1114 && w_value == 0) {
1115 DBG(cdev, "MTP_REQ_CANCEL\n");
1116
1117 spin_lock_irqsave(&dev->lock, flags);
1118 if (dev->state == STATE_BUSY) {
1119 dev->state = STATE_CANCELED;
1120 wake_up(&dev->read_wq);
1121 wake_up(&dev->write_wq);
1122 }
1123 spin_unlock_irqrestore(&dev->lock, flags);
1124
1125 /* We need to queue a request to read the remaining
1126 * bytes, but we don't actually need to look at
1127 * the contents.
1128 */
1129 value = w_length;
1130 } else if (ctrl->bRequest == MTP_REQ_GET_DEVICE_STATUS
1131 && w_index == 0 && w_value == 0) {
1132 struct mtp_device_status *status = cdev->req->buf;
1133 status->wLength =
1134 __constant_cpu_to_le16(sizeof(*status));
1135
1136 DBG(cdev, "MTP_REQ_GET_DEVICE_STATUS\n");
1137 spin_lock_irqsave(&dev->lock, flags);
1138 /* device status is "busy" until we report
1139 * the cancelation to userspace
1140 */
1141 if (dev->state == STATE_CANCELED)
1142 status->wCode =
1143 __cpu_to_le16(MTP_RESPONSE_DEVICE_BUSY);
1144 else
1145 status->wCode =
1146 __cpu_to_le16(MTP_RESPONSE_OK);
1147 spin_unlock_irqrestore(&dev->lock, flags);
1148 value = sizeof(*status);
1149 }
1150 }
1151
1152 /* respond with data transfer or status phase? */
1153 if (value >= 0) {
1154 int rc;
1155 cdev->req->zero = value < w_length;
1156 cdev->req->length = value;
1157 rc = usb_ep_queue(cdev->gadget->ep0, cdev->req, GFP_ATOMIC);
1158 if (rc < 0)
1159 ERROR(cdev, "%s: response queue error\n", __func__);
1160 }
1161 return value;
1162}
1163
1164static int
1165mtp_function_bind(struct usb_configuration *c, struct usb_function *f)
1166{
1167 struct usb_composite_dev *cdev = c->cdev;
1168 struct mtp_dev *dev = func_to_mtp(f);
1169 int id;
1170 int ret;
1171
1172 dev->cdev = cdev;
1173 DBG(cdev, "mtp_function_bind dev: %p\n", dev);
1174
1175 /* allocate interface ID(s) */
1176 id = usb_interface_id(c, f);
1177 if (id < 0)
1178 return id;
1179 mtp_interface_desc.bInterfaceNumber = id;
1180
1181 /* allocate endpoints */
1182 ret = mtp_create_bulk_endpoints(dev, &mtp_fullspeed_in_desc,
1183 &mtp_fullspeed_out_desc, &mtp_intr_desc);
1184 if (ret)
1185 return ret;
1186
1187 /* support high speed hardware */
1188 if (gadget_is_dualspeed(c->cdev->gadget)) {
1189 mtp_highspeed_in_desc.bEndpointAddress =
1190 mtp_fullspeed_in_desc.bEndpointAddress;
1191 mtp_highspeed_out_desc.bEndpointAddress =
1192 mtp_fullspeed_out_desc.bEndpointAddress;
1193 }
1194
Pavankumar Kondeti6f94bc92012-08-03 09:34:32 +05301195 /* support super speed hardware */
1196 if (gadget_is_superspeed(c->cdev->gadget)) {
1197 mtp_superspeed_in_desc.bEndpointAddress =
1198 mtp_fullspeed_in_desc.bEndpointAddress;
1199 mtp_superspeed_out_desc.bEndpointAddress =
1200 mtp_fullspeed_out_desc.bEndpointAddress;
1201 }
1202
Benoit Gobyf0fbc482011-12-19 14:37:50 -08001203 DBG(cdev, "%s speed %s: IN/%s, OUT/%s\n",
1204 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
1205 f->name, dev->ep_in->name, dev->ep_out->name);
1206 return 0;
1207}
1208
1209static void
1210mtp_function_unbind(struct usb_configuration *c, struct usb_function *f)
1211{
1212 struct mtp_dev *dev = func_to_mtp(f);
1213 struct usb_request *req;
1214 int i;
1215
1216 while ((req = mtp_req_get(dev, &dev->tx_idle)))
1217 mtp_request_free(req, dev->ep_in);
1218 for (i = 0; i < RX_REQ_MAX; i++)
1219 mtp_request_free(dev->rx_req[i], dev->ep_out);
1220 while ((req = mtp_req_get(dev, &dev->intr_idle)))
1221 mtp_request_free(req, dev->ep_intr);
1222 dev->state = STATE_OFFLINE;
1223}
1224
1225static int mtp_function_set_alt(struct usb_function *f,
1226 unsigned intf, unsigned alt)
1227{
1228 struct mtp_dev *dev = func_to_mtp(f);
1229 struct usb_composite_dev *cdev = f->config->cdev;
1230 int ret;
1231
1232 DBG(cdev, "mtp_function_set_alt intf: %d alt: %d\n", intf, alt);
1233
1234 ret = config_ep_by_speed(cdev->gadget, f, dev->ep_in);
Benoit Gobyf0fbc482011-12-19 14:37:50 -08001235 if (ret) {
Tatyana Brokhman31ac3522011-06-28 15:33:50 +02001236 dev->ep_in->desc = NULL;
1237 ERROR(cdev, "config_ep_by_speed failes for ep %s, result %d\n",
1238 dev->ep_in->name, ret);
1239 return ret;
1240 }
1241 ret = usb_ep_enable(dev->ep_in);
1242 if (ret) {
1243 ERROR(cdev, "failed to enable ep %s, result %d\n",
1244 dev->ep_in->name, ret);
Benoit Gobyf0fbc482011-12-19 14:37:50 -08001245 return ret;
1246 }
1247
Tatyana Brokhman31ac3522011-06-28 15:33:50 +02001248 ret = config_ep_by_speed(cdev->gadget, f, dev->ep_out);
1249 if (ret) {
1250 dev->ep_out->desc = NULL;
1251 ERROR(cdev, "config_ep_by_speed failes for ep %s, result %d\n",
1252 dev->ep_out->name, ret);
Tatyana Brokhmanebd3f392011-06-28 16:33:50 +03001253 usb_ep_disable(dev->ep_in);
Benoit Gobyf0fbc482011-12-19 14:37:50 -08001254 return ret;
Tatyana Brokhmanebd3f392011-06-28 16:33:50 +03001255 }
Tatyana Brokhman31ac3522011-06-28 15:33:50 +02001256 ret = usb_ep_enable(dev->ep_out);
1257 if (ret) {
1258 ERROR(cdev, "failed to enable ep %s, result %d\n",
1259 dev->ep_out->name, ret);
Tatyana Brokhmanebd3f392011-06-28 16:33:50 +03001260 usb_ep_disable(dev->ep_in);
1261 return ret;
1262 }
Tatyana Brokhmancf709c12011-06-28 16:33:48 +03001263 dev->ep_intr->desc = &mtp_intr_desc;
Benoit Gobyf0fbc482011-12-19 14:37:50 -08001264 ret = usb_ep_enable(dev->ep_intr);
1265 if (ret) {
1266 usb_ep_disable(dev->ep_out);
1267 usb_ep_disable(dev->ep_in);
1268 return ret;
1269 }
1270 dev->state = STATE_READY;
1271
1272 /* readers may be blocked waiting for us to go online */
1273 wake_up(&dev->read_wq);
1274 return 0;
1275}
1276
1277static void mtp_function_disable(struct usb_function *f)
1278{
1279 struct mtp_dev *dev = func_to_mtp(f);
1280 struct usb_composite_dev *cdev = dev->cdev;
1281
1282 DBG(cdev, "mtp_function_disable\n");
1283 dev->state = STATE_OFFLINE;
1284 usb_ep_disable(dev->ep_in);
1285 usb_ep_disable(dev->ep_out);
1286 usb_ep_disable(dev->ep_intr);
1287
1288 /* readers may be blocked waiting for us to go online */
1289 wake_up(&dev->read_wq);
1290
1291 VDBG(cdev, "%s disabled\n", dev->function.name);
1292}
1293
1294static int mtp_bind_config(struct usb_configuration *c, bool ptp_config)
1295{
1296 struct mtp_dev *dev = _mtp_dev;
1297 int ret = 0;
1298
1299 printk(KERN_INFO "mtp_bind_config\n");
1300
1301 /* allocate a string ID for our interface */
1302 if (mtp_string_defs[INTERFACE_STRING_INDEX].id == 0) {
1303 ret = usb_string_id(c->cdev);
1304 if (ret < 0)
1305 return ret;
1306 mtp_string_defs[INTERFACE_STRING_INDEX].id = ret;
1307 mtp_interface_desc.iInterface = ret;
1308 }
1309
1310 dev->cdev = c->cdev;
1311 dev->function.name = "mtp";
1312 dev->function.strings = mtp_strings;
1313 if (ptp_config) {
1314 dev->function.descriptors = fs_ptp_descs;
1315 dev->function.hs_descriptors = hs_ptp_descs;
Pavankumar Kondeti6f94bc92012-08-03 09:34:32 +05301316 if (gadget_is_superspeed(c->cdev->gadget))
1317 dev->function.ss_descriptors = ss_ptp_descs;
Benoit Gobyf0fbc482011-12-19 14:37:50 -08001318 } else {
1319 dev->function.descriptors = fs_mtp_descs;
1320 dev->function.hs_descriptors = hs_mtp_descs;
Pavankumar Kondeti6f94bc92012-08-03 09:34:32 +05301321 if (gadget_is_superspeed(c->cdev->gadget))
1322 dev->function.ss_descriptors = ss_mtp_descs;
Benoit Gobyf0fbc482011-12-19 14:37:50 -08001323 }
1324 dev->function.bind = mtp_function_bind;
1325 dev->function.unbind = mtp_function_unbind;
1326 dev->function.set_alt = mtp_function_set_alt;
1327 dev->function.disable = mtp_function_disable;
1328
1329 return usb_add_function(c, &dev->function);
1330}
1331
1332static int mtp_setup(void)
1333{
1334 struct mtp_dev *dev;
1335 int ret;
1336
1337 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1338 if (!dev)
1339 return -ENOMEM;
1340
1341 spin_lock_init(&dev->lock);
1342 init_waitqueue_head(&dev->read_wq);
1343 init_waitqueue_head(&dev->write_wq);
1344 init_waitqueue_head(&dev->intr_wq);
1345 atomic_set(&dev->open_excl, 0);
1346 atomic_set(&dev->ioctl_excl, 0);
1347 INIT_LIST_HEAD(&dev->tx_idle);
1348 INIT_LIST_HEAD(&dev->intr_idle);
1349
1350 dev->wq = create_singlethread_workqueue("f_mtp");
1351 if (!dev->wq) {
1352 ret = -ENOMEM;
1353 goto err1;
1354 }
1355 INIT_WORK(&dev->send_file_work, send_file_work);
1356 INIT_WORK(&dev->receive_file_work, receive_file_work);
1357
1358 _mtp_dev = dev;
1359
1360 ret = misc_register(&mtp_device);
1361 if (ret)
1362 goto err2;
1363
1364 return 0;
1365
1366err2:
1367 destroy_workqueue(dev->wq);
1368err1:
1369 _mtp_dev = NULL;
1370 kfree(dev);
1371 printk(KERN_ERR "mtp gadget driver failed to initialize\n");
1372 return ret;
1373}
1374
1375static void mtp_cleanup(void)
1376{
1377 struct mtp_dev *dev = _mtp_dev;
1378
1379 if (!dev)
1380 return;
1381
1382 misc_deregister(&mtp_device);
1383 destroy_workqueue(dev->wq);
1384 _mtp_dev = NULL;
1385 kfree(dev);
1386}