blob: 92dd89b340bcf5b64c1ca31bc94658931f3f51c3 [file] [log] [blame]
Mike Lockwoodba83b012010-04-16 10:39:22 -04001/*
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>
Mike Lockwoodba83b012010-04-16 10:39:22 -040028
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/android_composite.h>
38#include <linux/usb/f_mtp.h>
39
40#define BULK_BUFFER_SIZE 16384
Mike Lockwood1de4d4d2010-07-06 19:27:52 -040041#define INTR_BUFFER_SIZE 28
Mike Lockwoodba83b012010-04-16 10:39:22 -040042
43/* String IDs */
44#define INTERFACE_STRING_INDEX 0
45
46/* values for mtp_dev.state */
47#define STATE_OFFLINE 0 /* initial state, disconnected */
48#define STATE_READY 1 /* ready for userspace calls */
49#define STATE_BUSY 2 /* processing userspace calls */
50#define STATE_CANCELED 3 /* transaction canceled by host */
51#define STATE_ERROR 4 /* error from completion routine */
52
53/* number of tx and rx requests to allocate */
54#define TX_REQ_MAX 4
55#define RX_REQ_MAX 2
56
Mike Lockwoodba83b012010-04-16 10:39:22 -040057/* 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 shortname[] = "mtp_usb";
71
72struct mtp_dev {
73 struct usb_function function;
74 struct usb_composite_dev *cdev;
75 spinlock_t lock;
76
Mike Lockwood1de4d4d2010-07-06 19:27:52 -040077 /* appear as MTP or PTP when enumerating */
Mike Lockwoodba83b012010-04-16 10:39:22 -040078 int interface_mode;
79
80 struct usb_ep *ep_in;
81 struct usb_ep *ep_out;
82 struct usb_ep *ep_intr;
83
84 int state;
85
Mike Lockwood1de4d4d2010-07-06 19:27:52 -040086 /* synchronize access to our device file */
Mike Lockwoodba83b012010-04-16 10:39:22 -040087 atomic_t open_excl;
Mike Lockwood491d4182010-11-08 10:41:31 -050088 /* to enforce only one ioctl at a time */
89 atomic_t ioctl_excl;
Mike Lockwoodba83b012010-04-16 10:39:22 -040090
91 struct list_head tx_idle;
92
93 wait_queue_head_t read_wq;
94 wait_queue_head_t write_wq;
95 struct usb_request *rx_req[RX_REQ_MAX];
Mike Lockwood1de4d4d2010-07-06 19:27:52 -040096 struct usb_request *intr_req;
Mike Lockwoodba83b012010-04-16 10:39:22 -040097 int rx_done;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -040098 /* true if interrupt endpoint is busy */
99 int intr_busy;
100
Mike Lockwood491d4182010-11-08 10:41:31 -0500101 /* for processing MTP_SEND_FILE and MTP_RECEIVE_FILE
102 * ioctls on a work queue
103 */
104 struct workqueue_struct *wq;
105 struct work_struct send_file_work;
106 struct work_struct receive_file_work;
107 struct file *xfer_file;
108 loff_t xfer_file_offset;
Mike Lockwood3e800b62010-11-16 17:14:32 -0500109 int64_t xfer_file_length;
Mike Lockwood491d4182010-11-08 10:41:31 -0500110 int xfer_result;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400111};
112
113static struct usb_interface_descriptor mtp_interface_desc = {
114 .bLength = USB_DT_INTERFACE_SIZE,
115 .bDescriptorType = USB_DT_INTERFACE,
116 .bInterfaceNumber = 0,
117 .bNumEndpoints = 3,
118 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
119 .bInterfaceSubClass = USB_SUBCLASS_VENDOR_SPEC,
120 .bInterfaceProtocol = 0,
121};
122
123static struct usb_interface_descriptor ptp_interface_desc = {
124 .bLength = USB_DT_INTERFACE_SIZE,
125 .bDescriptorType = USB_DT_INTERFACE,
126 .bInterfaceNumber = 0,
127 .bNumEndpoints = 3,
128 .bInterfaceClass = USB_CLASS_STILL_IMAGE,
129 .bInterfaceSubClass = 1,
130 .bInterfaceProtocol = 1,
131};
132
133static struct usb_endpoint_descriptor mtp_highspeed_in_desc = {
134 .bLength = USB_DT_ENDPOINT_SIZE,
135 .bDescriptorType = USB_DT_ENDPOINT,
136 .bEndpointAddress = USB_DIR_IN,
137 .bmAttributes = USB_ENDPOINT_XFER_BULK,
138 .wMaxPacketSize = __constant_cpu_to_le16(512),
139};
140
141static struct usb_endpoint_descriptor mtp_highspeed_out_desc = {
142 .bLength = USB_DT_ENDPOINT_SIZE,
143 .bDescriptorType = USB_DT_ENDPOINT,
144 .bEndpointAddress = USB_DIR_OUT,
145 .bmAttributes = USB_ENDPOINT_XFER_BULK,
146 .wMaxPacketSize = __constant_cpu_to_le16(512),
147};
148
149static struct usb_endpoint_descriptor mtp_fullspeed_in_desc = {
150 .bLength = USB_DT_ENDPOINT_SIZE,
151 .bDescriptorType = USB_DT_ENDPOINT,
152 .bEndpointAddress = USB_DIR_IN,
153 .bmAttributes = USB_ENDPOINT_XFER_BULK,
154};
155
156static struct usb_endpoint_descriptor mtp_fullspeed_out_desc = {
157 .bLength = USB_DT_ENDPOINT_SIZE,
158 .bDescriptorType = USB_DT_ENDPOINT,
159 .bEndpointAddress = USB_DIR_OUT,
160 .bmAttributes = USB_ENDPOINT_XFER_BULK,
161};
162
163static struct usb_endpoint_descriptor mtp_intr_desc = {
164 .bLength = USB_DT_ENDPOINT_SIZE,
165 .bDescriptorType = USB_DT_ENDPOINT,
166 .bEndpointAddress = USB_DIR_IN,
167 .bmAttributes = USB_ENDPOINT_XFER_INT,
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400168 .wMaxPacketSize = __constant_cpu_to_le16(INTR_BUFFER_SIZE),
Mike Lockwoodba83b012010-04-16 10:39:22 -0400169 .bInterval = 6,
170};
171
172static struct usb_descriptor_header *fs_mtp_descs[] = {
173 (struct usb_descriptor_header *) &mtp_interface_desc,
174 (struct usb_descriptor_header *) &mtp_fullspeed_in_desc,
175 (struct usb_descriptor_header *) &mtp_fullspeed_out_desc,
176 (struct usb_descriptor_header *) &mtp_intr_desc,
177 NULL,
178};
179
180static struct usb_descriptor_header *hs_mtp_descs[] = {
181 (struct usb_descriptor_header *) &mtp_interface_desc,
182 (struct usb_descriptor_header *) &mtp_highspeed_in_desc,
183 (struct usb_descriptor_header *) &mtp_highspeed_out_desc,
184 (struct usb_descriptor_header *) &mtp_intr_desc,
185 NULL,
186};
187
188static struct usb_descriptor_header *fs_ptp_descs[] = {
189 (struct usb_descriptor_header *) &ptp_interface_desc,
190 (struct usb_descriptor_header *) &mtp_fullspeed_in_desc,
191 (struct usb_descriptor_header *) &mtp_fullspeed_out_desc,
192 (struct usb_descriptor_header *) &mtp_intr_desc,
193 NULL,
194};
195
196static struct usb_descriptor_header *hs_ptp_descs[] = {
197 (struct usb_descriptor_header *) &ptp_interface_desc,
198 (struct usb_descriptor_header *) &mtp_highspeed_in_desc,
199 (struct usb_descriptor_header *) &mtp_highspeed_out_desc,
200 (struct usb_descriptor_header *) &mtp_intr_desc,
201 NULL,
202};
203
204static struct usb_string mtp_string_defs[] = {
205 /* Naming interface "MTP" so libmtp will recognize us */
206 [INTERFACE_STRING_INDEX].s = "MTP",
207 { }, /* end of list */
208};
209
210static struct usb_gadget_strings mtp_string_table = {
211 .language = 0x0409, /* en-US */
212 .strings = mtp_string_defs,
213};
214
215static struct usb_gadget_strings *mtp_strings[] = {
216 &mtp_string_table,
217 NULL,
218};
219
220/* Microsoft MTP OS String */
221static u8 mtp_os_string[] = {
222 18, /* sizeof(mtp_os_string) */
223 USB_DT_STRING,
224 /* Signature field: "MSFT100" */
225 'M', 0, 'S', 0, 'F', 0, 'T', 0, '1', 0, '0', 0, '0', 0,
226 /* vendor code */
227 1,
228 /* padding */
229 0
230};
231
232/* Microsoft Extended Configuration Descriptor Header Section */
233struct mtp_ext_config_desc_header {
234 __le32 dwLength;
235 __u16 bcdVersion;
236 __le16 wIndex;
237 __u8 bCount;
238 __u8 reserved[7];
239};
240
241/* Microsoft Extended Configuration Descriptor Function Section */
242struct mtp_ext_config_desc_function {
243 __u8 bFirstInterfaceNumber;
244 __u8 bInterfaceCount;
245 __u8 compatibleID[8];
246 __u8 subCompatibleID[8];
247 __u8 reserved[6];
248};
249
250/* MTP Extended Configuration Descriptor */
251struct {
252 struct mtp_ext_config_desc_header header;
253 struct mtp_ext_config_desc_function function;
254} mtp_ext_config_desc = {
255 .header = {
256 .dwLength = __constant_cpu_to_le32(sizeof(mtp_ext_config_desc)),
257 .bcdVersion = __constant_cpu_to_le16(0x0100),
258 .wIndex = __constant_cpu_to_le16(4),
259 .bCount = __constant_cpu_to_le16(1),
260 },
261 .function = {
262 .bFirstInterfaceNumber = 0,
263 .bInterfaceCount = 1,
264 .compatibleID = { 'M', 'T', 'P' },
265 },
266};
267
268struct mtp_device_status {
269 __le16 wLength;
270 __le16 wCode;
271};
272
273/* temporary variable used between mtp_open() and mtp_gadget_bind() */
274static struct mtp_dev *_mtp_dev;
275
276static inline struct mtp_dev *func_to_dev(struct usb_function *f)
277{
278 return container_of(f, struct mtp_dev, function);
279}
280
281static struct usb_request *mtp_request_new(struct usb_ep *ep, int buffer_size)
282{
283 struct usb_request *req = usb_ep_alloc_request(ep, GFP_KERNEL);
284 if (!req)
285 return NULL;
286
287 /* now allocate buffers for the requests */
288 req->buf = kmalloc(buffer_size, GFP_KERNEL);
289 if (!req->buf) {
290 usb_ep_free_request(ep, req);
291 return NULL;
292 }
293
294 return req;
295}
296
297static void mtp_request_free(struct usb_request *req, struct usb_ep *ep)
298{
299 if (req) {
300 kfree(req->buf);
301 usb_ep_free_request(ep, req);
302 }
303}
304
305static inline int _lock(atomic_t *excl)
306{
307 if (atomic_inc_return(excl) == 1) {
308 return 0;
309 } else {
310 atomic_dec(excl);
311 return -1;
312 }
313}
314
315static inline void _unlock(atomic_t *excl)
316{
317 atomic_dec(excl);
318}
319
320/* add a request to the tail of a list */
321static void req_put(struct mtp_dev *dev, struct list_head *head,
322 struct usb_request *req)
323{
324 unsigned long flags;
325
326 spin_lock_irqsave(&dev->lock, flags);
327 list_add_tail(&req->list, head);
328 spin_unlock_irqrestore(&dev->lock, flags);
329}
330
331/* remove a request from the head of a list */
332static struct usb_request *req_get(struct mtp_dev *dev, struct list_head *head)
333{
334 unsigned long flags;
335 struct usb_request *req;
336
337 spin_lock_irqsave(&dev->lock, flags);
338 if (list_empty(head)) {
339 req = 0;
340 } else {
341 req = list_first_entry(head, struct usb_request, list);
342 list_del(&req->list);
343 }
344 spin_unlock_irqrestore(&dev->lock, flags);
345 return req;
346}
347
348static void mtp_complete_in(struct usb_ep *ep, struct usb_request *req)
349{
350 struct mtp_dev *dev = _mtp_dev;
351
352 if (req->status != 0)
353 dev->state = STATE_ERROR;
354
355 req_put(dev, &dev->tx_idle, req);
356
357 wake_up(&dev->write_wq);
358}
359
360static void mtp_complete_out(struct usb_ep *ep, struct usb_request *req)
361{
362 struct mtp_dev *dev = _mtp_dev;
363
364 dev->rx_done = 1;
365 if (req->status != 0)
366 dev->state = STATE_ERROR;
367
368 wake_up(&dev->read_wq);
369}
370
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400371static void mtp_complete_intr(struct usb_ep *ep, struct usb_request *req)
372{
373 struct mtp_dev *dev = _mtp_dev;
374
Mike Lockwood292b9632011-02-10 11:54:53 -0500375 DBG(dev->cdev, "mtp_complete_intr status: %d actual: %d\n",
376 req->status, req->actual);
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400377 dev->intr_busy = 0;
378 if (req->status != 0)
379 dev->state = STATE_ERROR;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400380}
381
Mike Lockwoodba83b012010-04-16 10:39:22 -0400382static int __init create_bulk_endpoints(struct mtp_dev *dev,
383 struct usb_endpoint_descriptor *in_desc,
384 struct usb_endpoint_descriptor *out_desc,
385 struct usb_endpoint_descriptor *intr_desc)
386{
387 struct usb_composite_dev *cdev = dev->cdev;
388 struct usb_request *req;
389 struct usb_ep *ep;
390 int i;
391
392 DBG(cdev, "create_bulk_endpoints dev: %p\n", dev);
393
394 ep = usb_ep_autoconfig(cdev->gadget, in_desc);
395 if (!ep) {
396 DBG(cdev, "usb_ep_autoconfig for ep_in failed\n");
397 return -ENODEV;
398 }
399 DBG(cdev, "usb_ep_autoconfig for ep_in got %s\n", ep->name);
400 ep->driver_data = dev; /* claim the endpoint */
401 dev->ep_in = ep;
402
403 ep = usb_ep_autoconfig(cdev->gadget, out_desc);
404 if (!ep) {
405 DBG(cdev, "usb_ep_autoconfig for ep_out failed\n");
406 return -ENODEV;
407 }
408 DBG(cdev, "usb_ep_autoconfig for mtp ep_out got %s\n", ep->name);
409 ep->driver_data = dev; /* claim the endpoint */
410 dev->ep_out = ep;
411
412 ep = usb_ep_autoconfig(cdev->gadget, out_desc);
413 if (!ep) {
414 DBG(cdev, "usb_ep_autoconfig for ep_out failed\n");
415 return -ENODEV;
416 }
417 DBG(cdev, "usb_ep_autoconfig for mtp ep_out got %s\n", ep->name);
418 ep->driver_data = dev; /* claim the endpoint */
419 dev->ep_out = ep;
420
421 ep = usb_ep_autoconfig(cdev->gadget, intr_desc);
422 if (!ep) {
423 DBG(cdev, "usb_ep_autoconfig for ep_intr failed\n");
424 return -ENODEV;
425 }
426 DBG(cdev, "usb_ep_autoconfig for mtp ep_intr got %s\n", ep->name);
427 ep->driver_data = dev; /* claim the endpoint */
428 dev->ep_intr = ep;
429
430 /* now allocate requests for our endpoints */
431 for (i = 0; i < TX_REQ_MAX; i++) {
432 req = mtp_request_new(dev->ep_in, BULK_BUFFER_SIZE);
433 if (!req)
434 goto fail;
435 req->complete = mtp_complete_in;
436 req_put(dev, &dev->tx_idle, req);
437 }
438 for (i = 0; i < RX_REQ_MAX; i++) {
439 req = mtp_request_new(dev->ep_out, BULK_BUFFER_SIZE);
440 if (!req)
441 goto fail;
442 req->complete = mtp_complete_out;
443 dev->rx_req[i] = req;
444 }
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400445 req = mtp_request_new(dev->ep_intr, INTR_BUFFER_SIZE);
446 if (!req)
447 goto fail;
448 req->complete = mtp_complete_intr;
449 dev->intr_req = req;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400450
451 return 0;
452
453fail:
454 printk(KERN_ERR "mtp_bind() could not allocate requests\n");
455 return -1;
456}
457
458static ssize_t mtp_read(struct file *fp, char __user *buf,
459 size_t count, loff_t *pos)
460{
461 struct mtp_dev *dev = fp->private_data;
462 struct usb_composite_dev *cdev = dev->cdev;
463 struct usb_request *req;
464 int r = count, xfer;
465 int ret = 0;
466
467 DBG(cdev, "mtp_read(%d)\n", count);
468
469 if (count > BULK_BUFFER_SIZE)
470 return -EINVAL;
471
472 /* we will block until we're online */
473 DBG(cdev, "mtp_read: waiting for online state\n");
474 ret = wait_event_interruptible(dev->read_wq,
475 dev->state != STATE_OFFLINE);
476 if (ret < 0) {
477 r = ret;
478 goto done;
479 }
480 spin_lock_irq(&dev->lock);
481 if (dev->state == STATE_CANCELED) {
482 /* report cancelation to userspace */
483 dev->state = STATE_READY;
484 spin_unlock_irq(&dev->lock);
485 return -ECANCELED;
486 }
487 dev->state = STATE_BUSY;
488 spin_unlock_irq(&dev->lock);
489
490requeue_req:
491 /* queue a request */
492 req = dev->rx_req[0];
493 req->length = count;
494 dev->rx_done = 0;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400495 ret = usb_ep_queue(dev->ep_out, req, GFP_KERNEL);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400496 if (ret < 0) {
497 r = -EIO;
498 goto done;
499 } else {
500 DBG(cdev, "rx %p queue\n", req);
501 }
502
503 /* wait for a request to complete */
504 ret = wait_event_interruptible(dev->read_wq, dev->rx_done);
505 if (ret < 0) {
506 r = ret;
Mike Lockwood43f3dc82011-02-19 15:33:17 -0500507 usb_ep_dequeue(dev->ep_out, req);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400508 goto done;
509 }
510 if (dev->state == STATE_BUSY) {
511 /* If we got a 0-len packet, throw it back and try again. */
512 if (req->actual == 0)
513 goto requeue_req;
514
515 DBG(cdev, "rx %p %d\n", req, req->actual);
516 xfer = (req->actual < count) ? req->actual : count;
517 r = xfer;
518 if (copy_to_user(buf, req->buf, xfer))
519 r = -EFAULT;
520 } else
521 r = -EIO;
522
523done:
524 spin_lock_irq(&dev->lock);
525 if (dev->state == STATE_CANCELED)
526 r = -ECANCELED;
527 else if (dev->state != STATE_OFFLINE)
528 dev->state = STATE_READY;
529 spin_unlock_irq(&dev->lock);
530
531 DBG(cdev, "mtp_read returning %d\n", r);
532 return r;
533}
534
535static ssize_t mtp_write(struct file *fp, const char __user *buf,
536 size_t count, loff_t *pos)
537{
538 struct mtp_dev *dev = fp->private_data;
539 struct usb_composite_dev *cdev = dev->cdev;
540 struct usb_request *req = 0;
541 int r = count, xfer;
Mike Lockwood16c08c22010-11-17 11:16:35 -0500542 int sendZLP = 0;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400543 int ret;
544
545 DBG(cdev, "mtp_write(%d)\n", count);
546
547 spin_lock_irq(&dev->lock);
548 if (dev->state == STATE_CANCELED) {
549 /* report cancelation to userspace */
550 dev->state = STATE_READY;
551 spin_unlock_irq(&dev->lock);
552 return -ECANCELED;
553 }
554 if (dev->state == STATE_OFFLINE) {
555 spin_unlock_irq(&dev->lock);
556 return -ENODEV;
557 }
558 dev->state = STATE_BUSY;
559 spin_unlock_irq(&dev->lock);
560
Mike Lockwood16c08c22010-11-17 11:16:35 -0500561 /* we need to send a zero length packet to signal the end of transfer
562 * if the transfer size is aligned to a packet boundary.
563 */
564 if ((count & (dev->ep_in->maxpacket - 1)) == 0) {
565 sendZLP = 1;
566 }
567
568 while (count > 0 || sendZLP) {
569 /* so we exit after sending ZLP */
570 if (count == 0)
571 sendZLP = 0;
572
Mike Lockwoodba83b012010-04-16 10:39:22 -0400573 if (dev->state != STATE_BUSY) {
574 DBG(cdev, "mtp_write dev->error\n");
575 r = -EIO;
576 break;
577 }
578
579 /* get an idle tx request to use */
580 req = 0;
581 ret = wait_event_interruptible(dev->write_wq,
582 ((req = req_get(dev, &dev->tx_idle))
583 || dev->state != STATE_BUSY));
584 if (!req) {
585 r = ret;
586 break;
587 }
588
589 if (count > BULK_BUFFER_SIZE)
590 xfer = BULK_BUFFER_SIZE;
591 else
592 xfer = count;
Mike Lockwood3e800b62010-11-16 17:14:32 -0500593 if (xfer && copy_from_user(req->buf, buf, xfer)) {
Mike Lockwoodba83b012010-04-16 10:39:22 -0400594 r = -EFAULT;
595 break;
596 }
597
598 req->length = xfer;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400599 ret = usb_ep_queue(dev->ep_in, req, GFP_KERNEL);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400600 if (ret < 0) {
601 DBG(cdev, "mtp_write: xfer error %d\n", ret);
602 r = -EIO;
603 break;
604 }
605
606 buf += xfer;
607 count -= xfer;
608
609 /* zero this so we don't try to free it on error exit */
610 req = 0;
Mike Lockwood16c08c22010-11-17 11:16:35 -0500611 }
Mike Lockwoodba83b012010-04-16 10:39:22 -0400612
613 if (req)
614 req_put(dev, &dev->tx_idle, req);
615
616 spin_lock_irq(&dev->lock);
617 if (dev->state == STATE_CANCELED)
618 r = -ECANCELED;
619 else if (dev->state != STATE_OFFLINE)
620 dev->state = STATE_READY;
621 spin_unlock_irq(&dev->lock);
622
623 DBG(cdev, "mtp_write returning %d\n", r);
624 return r;
625}
626
Mike Lockwood491d4182010-11-08 10:41:31 -0500627/* read from a local file and write to USB */
628static void send_file_work(struct work_struct *data) {
629 struct mtp_dev *dev = container_of(data, struct mtp_dev, send_file_work);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400630 struct usb_composite_dev *cdev = dev->cdev;
631 struct usb_request *req = 0;
Mike Lockwood491d4182010-11-08 10:41:31 -0500632 struct file *filp;
633 loff_t offset;
Mike Lockwood3e800b62010-11-16 17:14:32 -0500634 int64_t count;
Mike Lockwood76ac6552010-11-15 15:22:21 -0500635 int xfer, ret;
636 int r = 0;
Mike Lockwood3e800b62010-11-16 17:14:32 -0500637 int sendZLP = 0;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400638
Mike Lockwood491d4182010-11-08 10:41:31 -0500639 /* read our parameters */
640 smp_rmb();
641 filp = dev->xfer_file;
642 offset = dev->xfer_file_offset;
Mike Lockwood76ac6552010-11-15 15:22:21 -0500643 count = dev->xfer_file_length;
Mike Lockwood491d4182010-11-08 10:41:31 -0500644
Mike Lockwood3e800b62010-11-16 17:14:32 -0500645 DBG(cdev, "send_file_work(%lld %lld)\n", offset, count);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400646
Mike Lockwood3e800b62010-11-16 17:14:32 -0500647 /* we need to send a zero length packet to signal the end of transfer
Mike Lockwood16c08c22010-11-17 11:16:35 -0500648 * if the transfer size is aligned to a packet boundary.
Mike Lockwood3e800b62010-11-16 17:14:32 -0500649 */
Mike Lockwood16c08c22010-11-17 11:16:35 -0500650 if ((dev->xfer_file_length & (dev->ep_in->maxpacket - 1)) == 0) {
Mike Lockwood3e800b62010-11-16 17:14:32 -0500651 sendZLP = 1;
652 }
653
654 while (count > 0 || sendZLP) {
655 /* so we exit after sending ZLP */
656 if (count == 0)
657 sendZLP = 0;
658
Mike Lockwoodba83b012010-04-16 10:39:22 -0400659 /* get an idle tx request to use */
660 req = 0;
661 ret = wait_event_interruptible(dev->write_wq,
662 (req = req_get(dev, &dev->tx_idle))
663 || dev->state != STATE_BUSY);
Mike Lockwood090cbc42011-02-07 11:51:07 -0500664 if (dev->state == STATE_CANCELED) {
665 r = -ECANCELED;
666 break;
667 }
Mike Lockwoodba83b012010-04-16 10:39:22 -0400668 if (!req) {
669 r = ret;
670 break;
671 }
672
673 if (count > BULK_BUFFER_SIZE)
674 xfer = BULK_BUFFER_SIZE;
675 else
676 xfer = count;
677 ret = vfs_read(filp, req->buf, xfer, &offset);
678 if (ret < 0) {
679 r = ret;
680 break;
681 }
682 xfer = ret;
683
684 req->length = xfer;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400685 ret = usb_ep_queue(dev->ep_in, req, GFP_KERNEL);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400686 if (ret < 0) {
Mike Lockwood491d4182010-11-08 10:41:31 -0500687 DBG(cdev, "send_file_work: xfer error %d\n", ret);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400688 dev->state = STATE_ERROR;
689 r = -EIO;
690 break;
691 }
692
693 count -= xfer;
694
695 /* zero this so we don't try to free it on error exit */
696 req = 0;
697 }
698
699 if (req)
700 req_put(dev, &dev->tx_idle, req);
701
Mike Lockwood491d4182010-11-08 10:41:31 -0500702 DBG(cdev, "send_file_work returning %d\n", r);
703 /* write the result */
704 dev->xfer_result = r;
705 smp_wmb();
Mike Lockwoodba83b012010-04-16 10:39:22 -0400706}
707
Mike Lockwood491d4182010-11-08 10:41:31 -0500708/* read from USB and write to a local file */
709static void receive_file_work(struct work_struct *data)
Mike Lockwoodba83b012010-04-16 10:39:22 -0400710{
Mike Lockwood491d4182010-11-08 10:41:31 -0500711 struct mtp_dev *dev = container_of(data, struct mtp_dev, receive_file_work);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400712 struct usb_composite_dev *cdev = dev->cdev;
713 struct usb_request *read_req = NULL, *write_req = NULL;
Mike Lockwood491d4182010-11-08 10:41:31 -0500714 struct file *filp;
715 loff_t offset;
Mike Lockwood3e800b62010-11-16 17:14:32 -0500716 int64_t count;
Mike Lockwood76ac6552010-11-15 15:22:21 -0500717 int ret, cur_buf = 0;
718 int r = 0;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400719
Mike Lockwood491d4182010-11-08 10:41:31 -0500720 /* read our parameters */
721 smp_rmb();
722 filp = dev->xfer_file;
723 offset = dev->xfer_file_offset;
Mike Lockwood76ac6552010-11-15 15:22:21 -0500724 count = dev->xfer_file_length;
Mike Lockwood491d4182010-11-08 10:41:31 -0500725
Mike Lockwood3e800b62010-11-16 17:14:32 -0500726 DBG(cdev, "receive_file_work(%lld)\n", count);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400727
728 while (count > 0 || write_req) {
729 if (count > 0) {
730 /* queue a request */
731 read_req = dev->rx_req[cur_buf];
732 cur_buf = (cur_buf + 1) % RX_REQ_MAX;
733
734 read_req->length = (count > BULK_BUFFER_SIZE
735 ? BULK_BUFFER_SIZE : count);
736 dev->rx_done = 0;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400737 ret = usb_ep_queue(dev->ep_out, read_req, GFP_KERNEL);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400738 if (ret < 0) {
739 r = -EIO;
740 dev->state = STATE_ERROR;
741 break;
742 }
Mike Lockwoodba83b012010-04-16 10:39:22 -0400743 }
744
745 if (write_req) {
746 DBG(cdev, "rx %p %d\n", write_req, write_req->actual);
747 ret = vfs_write(filp, write_req->buf, write_req->actual,
748 &offset);
749 DBG(cdev, "vfs_write %d\n", ret);
750 if (ret != write_req->actual) {
751 r = -EIO;
752 dev->state = STATE_ERROR;
753 break;
754 }
755 write_req = NULL;
756 }
757
758 if (read_req) {
759 /* wait for our last read to complete */
760 ret = wait_event_interruptible(dev->read_wq,
761 dev->rx_done || dev->state != STATE_BUSY);
Mike Lockwood50fe49a2011-01-13 16:19:57 -0500762 if (dev->state == STATE_CANCELED) {
763 r = -ECANCELED;
764 if (!dev->rx_done)
765 usb_ep_dequeue(dev->ep_out, read_req);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400766 break;
767 }
Mike Lockwood3e800b62010-11-16 17:14:32 -0500768 /* if xfer_file_length is 0xFFFFFFFF, then we read until
769 * we get a zero length packet
770 */
771 if (count != 0xFFFFFFFF)
772 count -= read_req->actual;
773 if (read_req->actual < read_req->length) {
774 /* short packet is used to signal EOF for sizes > 4 gig */
775 DBG(cdev, "got short packet\n");
776 count = 0;
777 }
778
Mike Lockwoodba83b012010-04-16 10:39:22 -0400779 write_req = read_req;
780 read_req = NULL;
781 }
782 }
783
Mike Lockwood491d4182010-11-08 10:41:31 -0500784 DBG(cdev, "receive_file_work returning %d\n", r);
785 /* write the result */
786 dev->xfer_result = r;
787 smp_wmb();
Mike Lockwoodba83b012010-04-16 10:39:22 -0400788}
789
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400790static int mtp_send_event(struct mtp_dev *dev, struct mtp_event *event)
791{
792 struct usb_request *req;
793 int ret;
794 int length = event->length;
795
796 DBG(dev->cdev, "mtp_send_event(%d)\n", event->length);
797
798 if (length < 0 || length > INTR_BUFFER_SIZE)
799 return -EINVAL;
Mike Lockwood491d4182010-11-08 10:41:31 -0500800 if (dev->state == STATE_OFFLINE)
801 return -ENODEV;
Mike Lockwood292b9632011-02-10 11:54:53 -0500802 /* unfortunately an interrupt request might hang indefinitely if the host
803 * is not listening on the interrupt endpoint, so instead of waiting,
804 * we just fail if the endpoint is busy.
805 */
806 if (dev->intr_busy)
807 return -EBUSY;
808
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400809 req = dev->intr_req;
Mike Lockwood491d4182010-11-08 10:41:31 -0500810 if (copy_from_user(req->buf, (void __user *)event->data, length))
811 return -EFAULT;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400812 req->length = length;
813 dev->intr_busy = 1;
814 ret = usb_ep_queue(dev->ep_intr, req, GFP_KERNEL);
815 if (ret)
816 dev->intr_busy = 0;
817
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400818 return ret;
819}
820
Mike Lockwoodba83b012010-04-16 10:39:22 -0400821static long mtp_ioctl(struct file *fp, unsigned code, unsigned long value)
822{
823 struct mtp_dev *dev = fp->private_data;
824 struct file *filp = NULL;
825 int ret = -EINVAL;
826
Mike Lockwood491d4182010-11-08 10:41:31 -0500827 if (_lock(&dev->ioctl_excl))
828 return -EBUSY;
829
Mike Lockwoodba83b012010-04-16 10:39:22 -0400830 switch (code) {
831 case MTP_SEND_FILE:
832 case MTP_RECEIVE_FILE:
833 {
834 struct mtp_file_range mfr;
Mike Lockwood491d4182010-11-08 10:41:31 -0500835 struct work_struct *work;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400836
837 spin_lock_irq(&dev->lock);
838 if (dev->state == STATE_CANCELED) {
839 /* report cancelation to userspace */
840 dev->state = STATE_READY;
841 spin_unlock_irq(&dev->lock);
Mike Lockwood491d4182010-11-08 10:41:31 -0500842 ret = -ECANCELED;
843 goto out;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400844 }
845 if (dev->state == STATE_OFFLINE) {
846 spin_unlock_irq(&dev->lock);
Mike Lockwood491d4182010-11-08 10:41:31 -0500847 ret = -ENODEV;
848 goto out;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400849 }
850 dev->state = STATE_BUSY;
851 spin_unlock_irq(&dev->lock);
852
853 if (copy_from_user(&mfr, (void __user *)value, sizeof(mfr))) {
854 ret = -EFAULT;
855 goto fail;
856 }
Mike Lockwood491d4182010-11-08 10:41:31 -0500857 /* hold a reference to the file while we are working with it */
Mike Lockwoodba83b012010-04-16 10:39:22 -0400858 filp = fget(mfr.fd);
859 if (!filp) {
860 ret = -EBADF;
861 goto fail;
862 }
863
Mike Lockwood491d4182010-11-08 10:41:31 -0500864 /* write the parameters */
865 dev->xfer_file = filp;
866 dev->xfer_file_offset = mfr.offset;
867 dev->xfer_file_length = mfr.length;
868 smp_wmb();
Mike Lockwoodba83b012010-04-16 10:39:22 -0400869
870 if (code == MTP_SEND_FILE)
Mike Lockwood491d4182010-11-08 10:41:31 -0500871 work = &dev->send_file_work;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400872 else
Mike Lockwood491d4182010-11-08 10:41:31 -0500873 work = &dev->receive_file_work;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400874
Mike Lockwood491d4182010-11-08 10:41:31 -0500875 /* We do the file transfer on a work queue so it will run
876 * in kernel context, which is necessary for vfs_read and
877 * vfs_write to use our buffers in the kernel address space.
878 */
879 queue_work(dev->wq, work);
880 /* wait for operation to complete */
881 flush_workqueue(dev->wq);
882 fput(filp);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400883
Mike Lockwood491d4182010-11-08 10:41:31 -0500884 /* read the result */
885 smp_rmb();
886 ret = dev->xfer_result;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400887 break;
888 }
889 case MTP_SET_INTERFACE_MODE:
890 if (value == MTP_INTERFACE_MODE_MTP ||
891 value == MTP_INTERFACE_MODE_PTP) {
892 dev->interface_mode = value;
893 if (value == MTP_INTERFACE_MODE_PTP) {
894 dev->function.descriptors = fs_ptp_descs;
895 dev->function.hs_descriptors = hs_ptp_descs;
896 } else {
897 dev->function.descriptors = fs_mtp_descs;
898 dev->function.hs_descriptors = hs_mtp_descs;
899 }
900 ret = 0;
901 }
902 break;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400903 case MTP_SEND_EVENT:
904 {
905 struct mtp_event event;
906 /* return here so we don't change dev->state below,
907 * which would interfere with bulk transfer state.
908 */
909 if (copy_from_user(&event, (void __user *)value, sizeof(event)))
Mike Lockwood491d4182010-11-08 10:41:31 -0500910 ret = -EFAULT;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400911 else
Mike Lockwood491d4182010-11-08 10:41:31 -0500912 ret = mtp_send_event(dev, &event);
913 goto out;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400914 }
Mike Lockwoodba83b012010-04-16 10:39:22 -0400915 }
916
917fail:
Mike Lockwoodba83b012010-04-16 10:39:22 -0400918 spin_lock_irq(&dev->lock);
919 if (dev->state == STATE_CANCELED)
920 ret = -ECANCELED;
921 else if (dev->state != STATE_OFFLINE)
922 dev->state = STATE_READY;
923 spin_unlock_irq(&dev->lock);
Mike Lockwood491d4182010-11-08 10:41:31 -0500924out:
925 _unlock(&dev->ioctl_excl);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400926 DBG(dev->cdev, "ioctl returning %d\n", ret);
927 return ret;
928}
929
930static int mtp_open(struct inode *ip, struct file *fp)
931{
932 printk(KERN_INFO "mtp_open\n");
933 if (_lock(&_mtp_dev->open_excl))
934 return -EBUSY;
935
Mike Lockwoodba83b012010-04-16 10:39:22 -0400936 /* clear any error condition */
937 if (_mtp_dev->state != STATE_OFFLINE)
938 _mtp_dev->state = STATE_READY;
939
940 fp->private_data = _mtp_dev;
941 return 0;
942}
943
944static int mtp_release(struct inode *ip, struct file *fp)
945{
946 printk(KERN_INFO "mtp_release\n");
947
Mike Lockwoodba83b012010-04-16 10:39:22 -0400948 _unlock(&_mtp_dev->open_excl);
949 return 0;
950}
951
952/* file operations for /dev/mtp_usb */
953static const struct file_operations mtp_fops = {
954 .owner = THIS_MODULE,
955 .read = mtp_read,
956 .write = mtp_write,
957 .unlocked_ioctl = mtp_ioctl,
958 .open = mtp_open,
959 .release = mtp_release,
960};
961
962static struct miscdevice mtp_device = {
963 .minor = MISC_DYNAMIC_MINOR,
964 .name = shortname,
965 .fops = &mtp_fops,
966};
967
968static int
969mtp_function_bind(struct usb_configuration *c, struct usb_function *f)
970{
971 struct usb_composite_dev *cdev = c->cdev;
972 struct mtp_dev *dev = func_to_dev(f);
973 int id;
974 int ret;
975
976 dev->cdev = cdev;
977 DBG(cdev, "mtp_function_bind dev: %p\n", dev);
978
979 /* allocate interface ID(s) */
980 id = usb_interface_id(c, f);
981 if (id < 0)
982 return id;
983 mtp_interface_desc.bInterfaceNumber = id;
984
985 /* allocate endpoints */
986 ret = create_bulk_endpoints(dev, &mtp_fullspeed_in_desc,
987 &mtp_fullspeed_out_desc, &mtp_intr_desc);
988 if (ret)
989 return ret;
990
991 /* support high speed hardware */
992 if (gadget_is_dualspeed(c->cdev->gadget)) {
993 mtp_highspeed_in_desc.bEndpointAddress =
994 mtp_fullspeed_in_desc.bEndpointAddress;
995 mtp_highspeed_out_desc.bEndpointAddress =
996 mtp_fullspeed_out_desc.bEndpointAddress;
997 }
998
999 DBG(cdev, "%s speed %s: IN/%s, OUT/%s\n",
1000 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
1001 f->name, dev->ep_in->name, dev->ep_out->name);
1002 return 0;
1003}
1004
1005static void
1006mtp_function_unbind(struct usb_configuration *c, struct usb_function *f)
1007{
1008 struct mtp_dev *dev = func_to_dev(f);
1009 struct usb_request *req;
1010 int i;
1011
1012 spin_lock_irq(&dev->lock);
1013 while ((req = req_get(dev, &dev->tx_idle)))
1014 mtp_request_free(req, dev->ep_in);
1015 for (i = 0; i < RX_REQ_MAX; i++)
1016 mtp_request_free(dev->rx_req[i], dev->ep_out);
Mike Lockwood1de4d4d2010-07-06 19:27:52 -04001017 mtp_request_free(dev->intr_req, dev->ep_intr);
Mike Lockwoodba83b012010-04-16 10:39:22 -04001018 dev->state = STATE_OFFLINE;
1019 spin_unlock_irq(&dev->lock);
1020
1021 misc_deregister(&mtp_device);
1022 kfree(_mtp_dev);
1023 _mtp_dev = NULL;
1024}
1025
1026static int mtp_function_setup(struct usb_function *f,
1027 const struct usb_ctrlrequest *ctrl)
1028{
1029 struct mtp_dev *dev = func_to_dev(f);
1030 struct usb_composite_dev *cdev = dev->cdev;
1031 int value = -EOPNOTSUPP;
1032 u16 w_index = le16_to_cpu(ctrl->wIndex);
1033 u16 w_value = le16_to_cpu(ctrl->wValue);
1034 u16 w_length = le16_to_cpu(ctrl->wLength);
1035 unsigned long flags;
1036
1037 /* do nothing if we are disabled */
1038 if (dev->function.disabled)
1039 return value;
1040
1041 VDBG(cdev, "mtp_function_setup "
1042 "%02x.%02x v%04x i%04x l%u\n",
1043 ctrl->bRequestType, ctrl->bRequest,
1044 w_value, w_index, w_length);
1045
1046 /* Handle MTP OS string */
1047 if (dev->interface_mode == MTP_INTERFACE_MODE_MTP
1048 && ctrl->bRequestType ==
1049 (USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE)
1050 && ctrl->bRequest == USB_REQ_GET_DESCRIPTOR
1051 && (w_value >> 8) == USB_DT_STRING
1052 && (w_value & 0xFF) == MTP_OS_STRING_ID) {
1053 value = (w_length < sizeof(mtp_os_string)
1054 ? w_length : sizeof(mtp_os_string));
1055 memcpy(cdev->req->buf, mtp_os_string, value);
1056 /* return here since composite.c will send for us */
1057 return value;
1058 }
1059 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_VENDOR) {
1060 /* Handle MTP OS descriptor */
1061 DBG(cdev, "vendor request: %d index: %d value: %d length: %d\n",
1062 ctrl->bRequest, w_index, w_value, w_length);
1063
1064 if (dev->interface_mode == MTP_INTERFACE_MODE_MTP
1065 && ctrl->bRequest == 1
1066 && (ctrl->bRequestType & USB_DIR_IN)
1067 && (w_index == 4 || w_index == 5)) {
1068 value = (w_length < sizeof(mtp_ext_config_desc) ?
1069 w_length : sizeof(mtp_ext_config_desc));
1070 memcpy(cdev->req->buf, &mtp_ext_config_desc, value);
1071 }
1072 }
1073 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_CLASS) {
1074 DBG(cdev, "class request: %d index: %d value: %d length: %d\n",
1075 ctrl->bRequest, w_index, w_value, w_length);
1076
1077 if (ctrl->bRequest == MTP_REQ_CANCEL && w_index == 0
1078 && w_value == 0) {
1079 DBG(cdev, "MTP_REQ_CANCEL\n");
1080
1081 spin_lock_irqsave(&dev->lock, flags);
1082 if (dev->state == STATE_BUSY) {
1083 dev->state = STATE_CANCELED;
1084 wake_up(&dev->read_wq);
1085 wake_up(&dev->write_wq);
1086 }
1087 spin_unlock_irqrestore(&dev->lock, flags);
1088
1089 /* We need to queue a request to read the remaining
1090 * bytes, but we don't actually need to look at
1091 * the contents.
1092 */
1093 value = w_length;
1094 } else if (ctrl->bRequest == MTP_REQ_GET_DEVICE_STATUS
1095 && w_index == 0 && w_value == 0) {
1096 struct mtp_device_status *status = cdev->req->buf;
1097 status->wLength =
1098 __constant_cpu_to_le16(sizeof(*status));
1099
1100 DBG(cdev, "MTP_REQ_GET_DEVICE_STATUS\n");
1101 spin_lock_irqsave(&dev->lock, flags);
1102 /* device status is "busy" until we report
1103 * the cancelation to userspace
1104 */
Mike Lockwood090cbc42011-02-07 11:51:07 -05001105 if (dev->state == STATE_CANCELED)
Mike Lockwoodba83b012010-04-16 10:39:22 -04001106 status->wCode =
1107 __cpu_to_le16(MTP_RESPONSE_DEVICE_BUSY);
1108 else
1109 status->wCode =
1110 __cpu_to_le16(MTP_RESPONSE_OK);
Mike Lockwood090cbc42011-02-07 11:51:07 -05001111 spin_unlock_irqrestore(&dev->lock, flags);
Mike Lockwoodba83b012010-04-16 10:39:22 -04001112 value = sizeof(*status);
1113 }
1114 }
1115
1116 /* respond with data transfer or status phase? */
1117 if (value >= 0) {
1118 int rc;
1119 cdev->req->zero = value < w_length;
1120 cdev->req->length = value;
1121 rc = usb_ep_queue(cdev->gadget->ep0, cdev->req, GFP_ATOMIC);
1122 if (rc < 0)
1123 ERROR(cdev, "%s setup response queue error\n", __func__);
1124 }
1125
1126 if (value == -EOPNOTSUPP)
1127 VDBG(cdev,
1128 "unknown class-specific control req "
1129 "%02x.%02x v%04x i%04x l%u\n",
1130 ctrl->bRequestType, ctrl->bRequest,
1131 w_value, w_index, w_length);
1132 return value;
1133}
1134
1135static int mtp_function_set_alt(struct usb_function *f,
1136 unsigned intf, unsigned alt)
1137{
1138 struct mtp_dev *dev = func_to_dev(f);
1139 struct usb_composite_dev *cdev = f->config->cdev;
1140 int ret;
1141
1142 DBG(cdev, "mtp_function_set_alt intf: %d alt: %d\n", intf, alt);
1143 ret = usb_ep_enable(dev->ep_in,
1144 ep_choose(cdev->gadget,
1145 &mtp_highspeed_in_desc,
1146 &mtp_fullspeed_in_desc));
1147 if (ret)
1148 return ret;
1149 ret = usb_ep_enable(dev->ep_out,
1150 ep_choose(cdev->gadget,
1151 &mtp_highspeed_out_desc,
1152 &mtp_fullspeed_out_desc));
1153 if (ret) {
1154 usb_ep_disable(dev->ep_in);
1155 return ret;
1156 }
1157 ret = usb_ep_enable(dev->ep_intr, &mtp_intr_desc);
1158 if (ret) {
1159 usb_ep_disable(dev->ep_out);
1160 usb_ep_disable(dev->ep_in);
1161 return ret;
1162 }
1163 dev->state = STATE_READY;
1164
1165 /* readers may be blocked waiting for us to go online */
1166 wake_up(&dev->read_wq);
1167 return 0;
1168}
1169
1170static void mtp_function_disable(struct usb_function *f)
1171{
1172 struct mtp_dev *dev = func_to_dev(f);
1173 struct usb_composite_dev *cdev = dev->cdev;
1174
1175 DBG(cdev, "mtp_function_disable\n");
1176 dev->state = STATE_OFFLINE;
1177 usb_ep_disable(dev->ep_in);
1178 usb_ep_disable(dev->ep_out);
1179 usb_ep_disable(dev->ep_intr);
1180
1181 /* readers may be blocked waiting for us to go online */
1182 wake_up(&dev->read_wq);
1183
1184 VDBG(cdev, "%s disabled\n", dev->function.name);
1185}
1186
1187static int mtp_bind_config(struct usb_configuration *c)
1188{
1189 struct mtp_dev *dev;
Mike Lockwood090cbc42011-02-07 11:51:07 -05001190 int ret = 0;
Mike Lockwoodba83b012010-04-16 10:39:22 -04001191
1192 printk(KERN_INFO "mtp_bind_config\n");
1193
1194 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1195 if (!dev)
1196 return -ENOMEM;
1197
1198 /* allocate a string ID for our interface */
1199 if (mtp_string_defs[INTERFACE_STRING_INDEX].id == 0) {
1200 ret = usb_string_id(c->cdev);
1201 if (ret < 0)
1202 return ret;
1203 mtp_string_defs[INTERFACE_STRING_INDEX].id = ret;
1204 mtp_interface_desc.iInterface = ret;
1205 }
1206
1207 spin_lock_init(&dev->lock);
Mike Lockwoodba83b012010-04-16 10:39:22 -04001208 init_waitqueue_head(&dev->read_wq);
1209 init_waitqueue_head(&dev->write_wq);
1210 atomic_set(&dev->open_excl, 0);
Mike Lockwood491d4182010-11-08 10:41:31 -05001211 atomic_set(&dev->ioctl_excl, 0);
Mike Lockwoodba83b012010-04-16 10:39:22 -04001212 INIT_LIST_HEAD(&dev->tx_idle);
Mike Lockwood491d4182010-11-08 10:41:31 -05001213
1214 dev->wq = create_singlethread_workqueue("f_mtp");
1215 if (!dev->wq)
1216 goto err1;
1217 INIT_WORK(&dev->send_file_work, send_file_work);
1218 INIT_WORK(&dev->receive_file_work, receive_file_work);
Mike Lockwoodba83b012010-04-16 10:39:22 -04001219
1220 dev->cdev = c->cdev;
1221 dev->function.name = "mtp";
1222 dev->function.strings = mtp_strings,
1223 dev->function.descriptors = fs_mtp_descs;
1224 dev->function.hs_descriptors = hs_mtp_descs;
1225 dev->function.bind = mtp_function_bind;
1226 dev->function.unbind = mtp_function_unbind;
1227 dev->function.setup = mtp_function_setup;
1228 dev->function.set_alt = mtp_function_set_alt;
1229 dev->function.disable = mtp_function_disable;
1230
1231 /* MTP mode by default */
1232 dev->interface_mode = MTP_INTERFACE_MODE_MTP;
1233
1234 /* _mtp_dev must be set before calling usb_gadget_register_driver */
1235 _mtp_dev = dev;
1236
1237 ret = misc_register(&mtp_device);
1238 if (ret)
1239 goto err1;
1240
1241 ret = usb_add_function(c, &dev->function);
1242 if (ret)
1243 goto err2;
1244
1245 return 0;
1246
1247err2:
1248 misc_deregister(&mtp_device);
1249err1:
Mike Lockwood491d4182010-11-08 10:41:31 -05001250 if (dev->wq)
1251 destroy_workqueue(dev->wq);
Mike Lockwoodba83b012010-04-16 10:39:22 -04001252 kfree(dev);
1253 printk(KERN_ERR "mtp gadget driver failed to initialize\n");
1254 return ret;
1255}
1256
1257static struct android_usb_function mtp_function = {
1258 .name = "mtp",
1259 .bind_config = mtp_bind_config,
1260};
1261
1262static int __init init(void)
1263{
1264 printk(KERN_INFO "f_mtp init\n");
1265 android_register_function(&mtp_function);
1266 return 0;
1267}
1268module_init(init);