blob: 64fe3b3cd9e643f8b19e07f8639be1364b021776 [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;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -040095 wait_queue_head_t intr_wq;
Mike Lockwoodba83b012010-04-16 10:39:22 -040096 struct usb_request *rx_req[RX_REQ_MAX];
Mike Lockwood1de4d4d2010-07-06 19:27:52 -040097 struct usb_request *intr_req;
Mike Lockwoodba83b012010-04-16 10:39:22 -040098 int rx_done;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -040099 /* true if interrupt endpoint is busy */
100 int intr_busy;
101
Mike Lockwood491d4182010-11-08 10:41:31 -0500102 /* for processing MTP_SEND_FILE and MTP_RECEIVE_FILE
103 * ioctls on a work queue
104 */
105 struct workqueue_struct *wq;
106 struct work_struct send_file_work;
107 struct work_struct receive_file_work;
108 struct file *xfer_file;
109 loff_t xfer_file_offset;
Mike Lockwood3e800b62010-11-16 17:14:32 -0500110 int64_t xfer_file_length;
Mike Lockwood491d4182010-11-08 10:41:31 -0500111 int xfer_result;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400112};
113
114static struct usb_interface_descriptor mtp_interface_desc = {
115 .bLength = USB_DT_INTERFACE_SIZE,
116 .bDescriptorType = USB_DT_INTERFACE,
117 .bInterfaceNumber = 0,
118 .bNumEndpoints = 3,
119 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
120 .bInterfaceSubClass = USB_SUBCLASS_VENDOR_SPEC,
121 .bInterfaceProtocol = 0,
122};
123
124static struct usb_interface_descriptor ptp_interface_desc = {
125 .bLength = USB_DT_INTERFACE_SIZE,
126 .bDescriptorType = USB_DT_INTERFACE,
127 .bInterfaceNumber = 0,
128 .bNumEndpoints = 3,
129 .bInterfaceClass = USB_CLASS_STILL_IMAGE,
130 .bInterfaceSubClass = 1,
131 .bInterfaceProtocol = 1,
132};
133
134static struct usb_endpoint_descriptor mtp_highspeed_in_desc = {
135 .bLength = USB_DT_ENDPOINT_SIZE,
136 .bDescriptorType = USB_DT_ENDPOINT,
137 .bEndpointAddress = USB_DIR_IN,
138 .bmAttributes = USB_ENDPOINT_XFER_BULK,
139 .wMaxPacketSize = __constant_cpu_to_le16(512),
140};
141
142static struct usb_endpoint_descriptor mtp_highspeed_out_desc = {
143 .bLength = USB_DT_ENDPOINT_SIZE,
144 .bDescriptorType = USB_DT_ENDPOINT,
145 .bEndpointAddress = USB_DIR_OUT,
146 .bmAttributes = USB_ENDPOINT_XFER_BULK,
147 .wMaxPacketSize = __constant_cpu_to_le16(512),
148};
149
150static struct usb_endpoint_descriptor mtp_fullspeed_in_desc = {
151 .bLength = USB_DT_ENDPOINT_SIZE,
152 .bDescriptorType = USB_DT_ENDPOINT,
153 .bEndpointAddress = USB_DIR_IN,
154 .bmAttributes = USB_ENDPOINT_XFER_BULK,
155};
156
157static struct usb_endpoint_descriptor mtp_fullspeed_out_desc = {
158 .bLength = USB_DT_ENDPOINT_SIZE,
159 .bDescriptorType = USB_DT_ENDPOINT,
160 .bEndpointAddress = USB_DIR_OUT,
161 .bmAttributes = USB_ENDPOINT_XFER_BULK,
162};
163
164static struct usb_endpoint_descriptor mtp_intr_desc = {
165 .bLength = USB_DT_ENDPOINT_SIZE,
166 .bDescriptorType = USB_DT_ENDPOINT,
167 .bEndpointAddress = USB_DIR_IN,
168 .bmAttributes = USB_ENDPOINT_XFER_INT,
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400169 .wMaxPacketSize = __constant_cpu_to_le16(INTR_BUFFER_SIZE),
Mike Lockwoodba83b012010-04-16 10:39:22 -0400170 .bInterval = 6,
171};
172
173static struct usb_descriptor_header *fs_mtp_descs[] = {
174 (struct usb_descriptor_header *) &mtp_interface_desc,
175 (struct usb_descriptor_header *) &mtp_fullspeed_in_desc,
176 (struct usb_descriptor_header *) &mtp_fullspeed_out_desc,
177 (struct usb_descriptor_header *) &mtp_intr_desc,
178 NULL,
179};
180
181static struct usb_descriptor_header *hs_mtp_descs[] = {
182 (struct usb_descriptor_header *) &mtp_interface_desc,
183 (struct usb_descriptor_header *) &mtp_highspeed_in_desc,
184 (struct usb_descriptor_header *) &mtp_highspeed_out_desc,
185 (struct usb_descriptor_header *) &mtp_intr_desc,
186 NULL,
187};
188
189static struct usb_descriptor_header *fs_ptp_descs[] = {
190 (struct usb_descriptor_header *) &ptp_interface_desc,
191 (struct usb_descriptor_header *) &mtp_fullspeed_in_desc,
192 (struct usb_descriptor_header *) &mtp_fullspeed_out_desc,
193 (struct usb_descriptor_header *) &mtp_intr_desc,
194 NULL,
195};
196
197static struct usb_descriptor_header *hs_ptp_descs[] = {
198 (struct usb_descriptor_header *) &ptp_interface_desc,
199 (struct usb_descriptor_header *) &mtp_highspeed_in_desc,
200 (struct usb_descriptor_header *) &mtp_highspeed_out_desc,
201 (struct usb_descriptor_header *) &mtp_intr_desc,
202 NULL,
203};
204
205static struct usb_string mtp_string_defs[] = {
206 /* Naming interface "MTP" so libmtp will recognize us */
207 [INTERFACE_STRING_INDEX].s = "MTP",
208 { }, /* end of list */
209};
210
211static struct usb_gadget_strings mtp_string_table = {
212 .language = 0x0409, /* en-US */
213 .strings = mtp_string_defs,
214};
215
216static struct usb_gadget_strings *mtp_strings[] = {
217 &mtp_string_table,
218 NULL,
219};
220
221/* Microsoft MTP OS String */
222static u8 mtp_os_string[] = {
223 18, /* sizeof(mtp_os_string) */
224 USB_DT_STRING,
225 /* Signature field: "MSFT100" */
226 'M', 0, 'S', 0, 'F', 0, 'T', 0, '1', 0, '0', 0, '0', 0,
227 /* vendor code */
228 1,
229 /* padding */
230 0
231};
232
233/* Microsoft Extended Configuration Descriptor Header Section */
234struct mtp_ext_config_desc_header {
235 __le32 dwLength;
236 __u16 bcdVersion;
237 __le16 wIndex;
238 __u8 bCount;
239 __u8 reserved[7];
240};
241
242/* Microsoft Extended Configuration Descriptor Function Section */
243struct mtp_ext_config_desc_function {
244 __u8 bFirstInterfaceNumber;
245 __u8 bInterfaceCount;
246 __u8 compatibleID[8];
247 __u8 subCompatibleID[8];
248 __u8 reserved[6];
249};
250
251/* MTP Extended Configuration Descriptor */
252struct {
253 struct mtp_ext_config_desc_header header;
254 struct mtp_ext_config_desc_function function;
255} mtp_ext_config_desc = {
256 .header = {
257 .dwLength = __constant_cpu_to_le32(sizeof(mtp_ext_config_desc)),
258 .bcdVersion = __constant_cpu_to_le16(0x0100),
259 .wIndex = __constant_cpu_to_le16(4),
260 .bCount = __constant_cpu_to_le16(1),
261 },
262 .function = {
263 .bFirstInterfaceNumber = 0,
264 .bInterfaceCount = 1,
265 .compatibleID = { 'M', 'T', 'P' },
266 },
267};
268
269struct mtp_device_status {
270 __le16 wLength;
271 __le16 wCode;
272};
273
274/* temporary variable used between mtp_open() and mtp_gadget_bind() */
275static struct mtp_dev *_mtp_dev;
276
277static inline struct mtp_dev *func_to_dev(struct usb_function *f)
278{
279 return container_of(f, struct mtp_dev, function);
280}
281
282static struct usb_request *mtp_request_new(struct usb_ep *ep, int buffer_size)
283{
284 struct usb_request *req = usb_ep_alloc_request(ep, GFP_KERNEL);
285 if (!req)
286 return NULL;
287
288 /* now allocate buffers for the requests */
289 req->buf = kmalloc(buffer_size, GFP_KERNEL);
290 if (!req->buf) {
291 usb_ep_free_request(ep, req);
292 return NULL;
293 }
294
295 return req;
296}
297
298static void mtp_request_free(struct usb_request *req, struct usb_ep *ep)
299{
300 if (req) {
301 kfree(req->buf);
302 usb_ep_free_request(ep, req);
303 }
304}
305
306static inline int _lock(atomic_t *excl)
307{
308 if (atomic_inc_return(excl) == 1) {
309 return 0;
310 } else {
311 atomic_dec(excl);
312 return -1;
313 }
314}
315
316static inline void _unlock(atomic_t *excl)
317{
318 atomic_dec(excl);
319}
320
321/* add a request to the tail of a list */
322static void req_put(struct mtp_dev *dev, struct list_head *head,
323 struct usb_request *req)
324{
325 unsigned long flags;
326
327 spin_lock_irqsave(&dev->lock, flags);
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 */
333static struct usb_request *req_get(struct mtp_dev *dev, struct list_head *head)
334{
335 unsigned long flags;
336 struct usb_request *req;
337
338 spin_lock_irqsave(&dev->lock, flags);
339 if (list_empty(head)) {
340 req = 0;
341 } else {
342 req = list_first_entry(head, struct usb_request, list);
343 list_del(&req->list);
344 }
345 spin_unlock_irqrestore(&dev->lock, flags);
346 return req;
347}
348
349static void mtp_complete_in(struct usb_ep *ep, struct usb_request *req)
350{
351 struct mtp_dev *dev = _mtp_dev;
352
353 if (req->status != 0)
354 dev->state = STATE_ERROR;
355
356 req_put(dev, &dev->tx_idle, req);
357
358 wake_up(&dev->write_wq);
359}
360
361static void mtp_complete_out(struct usb_ep *ep, struct usb_request *req)
362{
363 struct mtp_dev *dev = _mtp_dev;
364
365 dev->rx_done = 1;
366 if (req->status != 0)
367 dev->state = STATE_ERROR;
368
369 wake_up(&dev->read_wq);
370}
371
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400372static void mtp_complete_intr(struct usb_ep *ep, struct usb_request *req)
373{
374 struct mtp_dev *dev = _mtp_dev;
375
376 DBG(dev->cdev, "mtp_complete_intr status: %d actual: %d\n", req->status, req->actual);
377 dev->intr_busy = 0;
378 if (req->status != 0)
379 dev->state = STATE_ERROR;
380
381 wake_up(&dev->intr_wq);
382}
383
Mike Lockwoodba83b012010-04-16 10:39:22 -0400384static int __init create_bulk_endpoints(struct mtp_dev *dev,
385 struct usb_endpoint_descriptor *in_desc,
386 struct usb_endpoint_descriptor *out_desc,
387 struct usb_endpoint_descriptor *intr_desc)
388{
389 struct usb_composite_dev *cdev = dev->cdev;
390 struct usb_request *req;
391 struct usb_ep *ep;
392 int i;
393
394 DBG(cdev, "create_bulk_endpoints dev: %p\n", dev);
395
396 ep = usb_ep_autoconfig(cdev->gadget, in_desc);
397 if (!ep) {
398 DBG(cdev, "usb_ep_autoconfig for ep_in failed\n");
399 return -ENODEV;
400 }
401 DBG(cdev, "usb_ep_autoconfig for ep_in got %s\n", ep->name);
402 ep->driver_data = dev; /* claim the endpoint */
403 dev->ep_in = ep;
404
405 ep = usb_ep_autoconfig(cdev->gadget, out_desc);
406 if (!ep) {
407 DBG(cdev, "usb_ep_autoconfig for ep_out failed\n");
408 return -ENODEV;
409 }
410 DBG(cdev, "usb_ep_autoconfig for mtp ep_out got %s\n", ep->name);
411 ep->driver_data = dev; /* claim the endpoint */
412 dev->ep_out = ep;
413
414 ep = usb_ep_autoconfig(cdev->gadget, out_desc);
415 if (!ep) {
416 DBG(cdev, "usb_ep_autoconfig for ep_out failed\n");
417 return -ENODEV;
418 }
419 DBG(cdev, "usb_ep_autoconfig for mtp ep_out got %s\n", ep->name);
420 ep->driver_data = dev; /* claim the endpoint */
421 dev->ep_out = ep;
422
423 ep = usb_ep_autoconfig(cdev->gadget, intr_desc);
424 if (!ep) {
425 DBG(cdev, "usb_ep_autoconfig for ep_intr failed\n");
426 return -ENODEV;
427 }
428 DBG(cdev, "usb_ep_autoconfig for mtp ep_intr got %s\n", ep->name);
429 ep->driver_data = dev; /* claim the endpoint */
430 dev->ep_intr = ep;
431
432 /* now allocate requests for our endpoints */
433 for (i = 0; i < TX_REQ_MAX; i++) {
434 req = mtp_request_new(dev->ep_in, BULK_BUFFER_SIZE);
435 if (!req)
436 goto fail;
437 req->complete = mtp_complete_in;
438 req_put(dev, &dev->tx_idle, req);
439 }
440 for (i = 0; i < RX_REQ_MAX; i++) {
441 req = mtp_request_new(dev->ep_out, BULK_BUFFER_SIZE);
442 if (!req)
443 goto fail;
444 req->complete = mtp_complete_out;
445 dev->rx_req[i] = req;
446 }
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400447 req = mtp_request_new(dev->ep_intr, INTR_BUFFER_SIZE);
448 if (!req)
449 goto fail;
450 req->complete = mtp_complete_intr;
451 dev->intr_req = req;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400452
453 return 0;
454
455fail:
456 printk(KERN_ERR "mtp_bind() could not allocate requests\n");
457 return -1;
458}
459
460static ssize_t mtp_read(struct file *fp, char __user *buf,
461 size_t count, loff_t *pos)
462{
463 struct mtp_dev *dev = fp->private_data;
464 struct usb_composite_dev *cdev = dev->cdev;
465 struct usb_request *req;
466 int r = count, xfer;
467 int ret = 0;
468
469 DBG(cdev, "mtp_read(%d)\n", count);
470
471 if (count > BULK_BUFFER_SIZE)
472 return -EINVAL;
473
474 /* we will block until we're online */
475 DBG(cdev, "mtp_read: waiting for online state\n");
476 ret = wait_event_interruptible(dev->read_wq,
477 dev->state != STATE_OFFLINE);
478 if (ret < 0) {
479 r = ret;
480 goto done;
481 }
482 spin_lock_irq(&dev->lock);
483 if (dev->state == STATE_CANCELED) {
484 /* report cancelation to userspace */
485 dev->state = STATE_READY;
486 spin_unlock_irq(&dev->lock);
487 return -ECANCELED;
488 }
489 dev->state = STATE_BUSY;
490 spin_unlock_irq(&dev->lock);
491
492requeue_req:
493 /* queue a request */
494 req = dev->rx_req[0];
495 req->length = count;
496 dev->rx_done = 0;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400497 ret = usb_ep_queue(dev->ep_out, req, GFP_KERNEL);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400498 if (ret < 0) {
499 r = -EIO;
500 goto done;
501 } else {
502 DBG(cdev, "rx %p queue\n", req);
503 }
504
505 /* wait for a request to complete */
506 ret = wait_event_interruptible(dev->read_wq, dev->rx_done);
507 if (ret < 0) {
508 r = ret;
509 goto done;
510 }
511 if (dev->state == STATE_BUSY) {
512 /* If we got a 0-len packet, throw it back and try again. */
513 if (req->actual == 0)
514 goto requeue_req;
515
516 DBG(cdev, "rx %p %d\n", req, req->actual);
517 xfer = (req->actual < count) ? req->actual : count;
518 r = xfer;
519 if (copy_to_user(buf, req->buf, xfer))
520 r = -EFAULT;
521 } else
522 r = -EIO;
523
524done:
525 spin_lock_irq(&dev->lock);
526 if (dev->state == STATE_CANCELED)
527 r = -ECANCELED;
528 else if (dev->state != STATE_OFFLINE)
529 dev->state = STATE_READY;
530 spin_unlock_irq(&dev->lock);
531
532 DBG(cdev, "mtp_read returning %d\n", r);
533 return r;
534}
535
536static ssize_t mtp_write(struct file *fp, const char __user *buf,
537 size_t count, loff_t *pos)
538{
539 struct mtp_dev *dev = fp->private_data;
540 struct usb_composite_dev *cdev = dev->cdev;
541 struct usb_request *req = 0;
542 int r = count, xfer;
Mike Lockwood16c08c22010-11-17 11:16:35 -0500543 int sendZLP = 0;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400544 int ret;
545
546 DBG(cdev, "mtp_write(%d)\n", count);
547
548 spin_lock_irq(&dev->lock);
549 if (dev->state == STATE_CANCELED) {
550 /* report cancelation to userspace */
551 dev->state = STATE_READY;
552 spin_unlock_irq(&dev->lock);
553 return -ECANCELED;
554 }
555 if (dev->state == STATE_OFFLINE) {
556 spin_unlock_irq(&dev->lock);
557 return -ENODEV;
558 }
559 dev->state = STATE_BUSY;
560 spin_unlock_irq(&dev->lock);
561
Mike Lockwood16c08c22010-11-17 11:16:35 -0500562 /* we need to send a zero length packet to signal the end of transfer
563 * if the transfer size is aligned to a packet boundary.
564 */
565 if ((count & (dev->ep_in->maxpacket - 1)) == 0) {
566 sendZLP = 1;
567 }
568
569 while (count > 0 || sendZLP) {
570 /* so we exit after sending ZLP */
571 if (count == 0)
572 sendZLP = 0;
573
Mike Lockwoodba83b012010-04-16 10:39:22 -0400574 if (dev->state != STATE_BUSY) {
575 DBG(cdev, "mtp_write dev->error\n");
576 r = -EIO;
577 break;
578 }
579
580 /* get an idle tx request to use */
581 req = 0;
582 ret = wait_event_interruptible(dev->write_wq,
583 ((req = req_get(dev, &dev->tx_idle))
584 || dev->state != STATE_BUSY));
585 if (!req) {
586 r = ret;
587 break;
588 }
589
590 if (count > BULK_BUFFER_SIZE)
591 xfer = BULK_BUFFER_SIZE;
592 else
593 xfer = count;
Mike Lockwood3e800b62010-11-16 17:14:32 -0500594 if (xfer && copy_from_user(req->buf, buf, xfer)) {
Mike Lockwoodba83b012010-04-16 10:39:22 -0400595 r = -EFAULT;
596 break;
597 }
598
599 req->length = xfer;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400600 ret = usb_ep_queue(dev->ep_in, req, GFP_KERNEL);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400601 if (ret < 0) {
602 DBG(cdev, "mtp_write: xfer error %d\n", ret);
603 r = -EIO;
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 = 0;
Mike Lockwood16c08c22010-11-17 11:16:35 -0500612 }
Mike Lockwoodba83b012010-04-16 10:39:22 -0400613
614 if (req)
615 req_put(dev, &dev->tx_idle, req);
616
617 spin_lock_irq(&dev->lock);
618 if (dev->state == STATE_CANCELED)
619 r = -ECANCELED;
620 else if (dev->state != STATE_OFFLINE)
621 dev->state = STATE_READY;
622 spin_unlock_irq(&dev->lock);
623
624 DBG(cdev, "mtp_write returning %d\n", r);
625 return r;
626}
627
Mike Lockwood491d4182010-11-08 10:41:31 -0500628/* read from a local file and write to USB */
629static void send_file_work(struct work_struct *data) {
630 struct mtp_dev *dev = container_of(data, struct mtp_dev, send_file_work);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400631 struct usb_composite_dev *cdev = dev->cdev;
632 struct usb_request *req = 0;
Mike Lockwood491d4182010-11-08 10:41:31 -0500633 struct file *filp;
634 loff_t offset;
Mike Lockwood3e800b62010-11-16 17:14:32 -0500635 int64_t count;
Mike Lockwood76ac6552010-11-15 15:22:21 -0500636 int xfer, ret;
637 int r = 0;
Mike Lockwood3e800b62010-11-16 17:14:32 -0500638 int sendZLP = 0;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400639
Mike Lockwood491d4182010-11-08 10:41:31 -0500640 /* read our parameters */
641 smp_rmb();
642 filp = dev->xfer_file;
643 offset = dev->xfer_file_offset;
Mike Lockwood76ac6552010-11-15 15:22:21 -0500644 count = dev->xfer_file_length;
Mike Lockwood491d4182010-11-08 10:41:31 -0500645
Mike Lockwood3e800b62010-11-16 17:14:32 -0500646 DBG(cdev, "send_file_work(%lld %lld)\n", offset, count);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400647
Mike Lockwood3e800b62010-11-16 17:14:32 -0500648 /* we need to send a zero length packet to signal the end of transfer
Mike Lockwood16c08c22010-11-17 11:16:35 -0500649 * if the transfer size is aligned to a packet boundary.
Mike Lockwood3e800b62010-11-16 17:14:32 -0500650 */
Mike Lockwood16c08c22010-11-17 11:16:35 -0500651 if ((dev->xfer_file_length & (dev->ep_in->maxpacket - 1)) == 0) {
Mike Lockwood3e800b62010-11-16 17:14:32 -0500652 sendZLP = 1;
653 }
654
655 while (count > 0 || sendZLP) {
656 /* so we exit after sending ZLP */
657 if (count == 0)
658 sendZLP = 0;
659
Mike Lockwoodba83b012010-04-16 10:39:22 -0400660 /* get an idle tx request to use */
661 req = 0;
662 ret = wait_event_interruptible(dev->write_wq,
663 (req = req_get(dev, &dev->tx_idle))
664 || dev->state != STATE_BUSY);
665 if (!req) {
666 r = ret;
667 break;
668 }
669
670 if (count > BULK_BUFFER_SIZE)
671 xfer = BULK_BUFFER_SIZE;
672 else
673 xfer = count;
674 ret = vfs_read(filp, req->buf, xfer, &offset);
675 if (ret < 0) {
676 r = ret;
677 break;
678 }
679 xfer = ret;
680
681 req->length = xfer;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400682 ret = usb_ep_queue(dev->ep_in, req, GFP_KERNEL);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400683 if (ret < 0) {
Mike Lockwood491d4182010-11-08 10:41:31 -0500684 DBG(cdev, "send_file_work: xfer error %d\n", ret);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400685 dev->state = STATE_ERROR;
686 r = -EIO;
687 break;
688 }
689
690 count -= xfer;
691
692 /* zero this so we don't try to free it on error exit */
693 req = 0;
694 }
695
696 if (req)
697 req_put(dev, &dev->tx_idle, req);
698
Mike Lockwood491d4182010-11-08 10:41:31 -0500699 DBG(cdev, "send_file_work returning %d\n", r);
700 /* write the result */
701 dev->xfer_result = r;
702 smp_wmb();
Mike Lockwoodba83b012010-04-16 10:39:22 -0400703}
704
Mike Lockwood491d4182010-11-08 10:41:31 -0500705/* read from USB and write to a local file */
706static void receive_file_work(struct work_struct *data)
Mike Lockwoodba83b012010-04-16 10:39:22 -0400707{
Mike Lockwood491d4182010-11-08 10:41:31 -0500708 struct mtp_dev *dev = container_of(data, struct mtp_dev, receive_file_work);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400709 struct usb_composite_dev *cdev = dev->cdev;
710 struct usb_request *read_req = NULL, *write_req = NULL;
Mike Lockwood491d4182010-11-08 10:41:31 -0500711 struct file *filp;
712 loff_t offset;
Mike Lockwood3e800b62010-11-16 17:14:32 -0500713 int64_t count;
Mike Lockwood76ac6552010-11-15 15:22:21 -0500714 int ret, cur_buf = 0;
715 int r = 0;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400716
Mike Lockwood491d4182010-11-08 10:41:31 -0500717 /* read our parameters */
718 smp_rmb();
719 filp = dev->xfer_file;
720 offset = dev->xfer_file_offset;
Mike Lockwood76ac6552010-11-15 15:22:21 -0500721 count = dev->xfer_file_length;
Mike Lockwood491d4182010-11-08 10:41:31 -0500722
Mike Lockwood3e800b62010-11-16 17:14:32 -0500723 DBG(cdev, "receive_file_work(%lld)\n", count);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400724
725 while (count > 0 || write_req) {
726 if (count > 0) {
727 /* queue a request */
728 read_req = dev->rx_req[cur_buf];
729 cur_buf = (cur_buf + 1) % RX_REQ_MAX;
730
731 read_req->length = (count > BULK_BUFFER_SIZE
732 ? BULK_BUFFER_SIZE : count);
733 dev->rx_done = 0;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400734 ret = usb_ep_queue(dev->ep_out, read_req, GFP_KERNEL);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400735 if (ret < 0) {
736 r = -EIO;
737 dev->state = STATE_ERROR;
738 break;
739 }
Mike Lockwoodba83b012010-04-16 10:39:22 -0400740 }
741
742 if (write_req) {
743 DBG(cdev, "rx %p %d\n", write_req, write_req->actual);
744 ret = vfs_write(filp, write_req->buf, write_req->actual,
745 &offset);
746 DBG(cdev, "vfs_write %d\n", ret);
747 if (ret != write_req->actual) {
748 r = -EIO;
749 dev->state = STATE_ERROR;
750 break;
751 }
752 write_req = NULL;
753 }
754
755 if (read_req) {
756 /* wait for our last read to complete */
757 ret = wait_event_interruptible(dev->read_wq,
758 dev->rx_done || dev->state != STATE_BUSY);
759 if (ret < 0 || dev->state != STATE_BUSY) {
760 r = ret;
761 break;
762 }
Mike Lockwood3e800b62010-11-16 17:14:32 -0500763 /* if xfer_file_length is 0xFFFFFFFF, then we read until
764 * we get a zero length packet
765 */
766 if (count != 0xFFFFFFFF)
767 count -= read_req->actual;
768 if (read_req->actual < read_req->length) {
769 /* short packet is used to signal EOF for sizes > 4 gig */
770 DBG(cdev, "got short packet\n");
771 count = 0;
772 }
773
Mike Lockwoodba83b012010-04-16 10:39:22 -0400774 write_req = read_req;
775 read_req = NULL;
776 }
777 }
778
Mike Lockwood491d4182010-11-08 10:41:31 -0500779 DBG(cdev, "receive_file_work returning %d\n", r);
780 /* write the result */
781 dev->xfer_result = r;
782 smp_wmb();
Mike Lockwoodba83b012010-04-16 10:39:22 -0400783}
784
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400785static int mtp_send_event(struct mtp_dev *dev, struct mtp_event *event)
786{
787 struct usb_request *req;
788 int ret;
789 int length = event->length;
790
791 DBG(dev->cdev, "mtp_send_event(%d)\n", event->length);
792
793 if (length < 0 || length > INTR_BUFFER_SIZE)
794 return -EINVAL;
795
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400796 /* wait for a request to complete */
797 ret = wait_event_interruptible(dev->intr_wq, !dev->intr_busy || dev->state == STATE_OFFLINE);
798 if (ret < 0)
Mike Lockwood491d4182010-11-08 10:41:31 -0500799 return ret;
800 if (dev->state == STATE_OFFLINE)
801 return -ENODEV;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400802 req = dev->intr_req;
Mike Lockwood491d4182010-11-08 10:41:31 -0500803 if (copy_from_user(req->buf, (void __user *)event->data, length))
804 return -EFAULT;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400805 req->length = length;
806 dev->intr_busy = 1;
807 ret = usb_ep_queue(dev->ep_intr, req, GFP_KERNEL);
808 if (ret)
809 dev->intr_busy = 0;
810
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400811 return ret;
812}
813
Mike Lockwoodba83b012010-04-16 10:39:22 -0400814static long mtp_ioctl(struct file *fp, unsigned code, unsigned long value)
815{
816 struct mtp_dev *dev = fp->private_data;
817 struct file *filp = NULL;
818 int ret = -EINVAL;
819
Mike Lockwood491d4182010-11-08 10:41:31 -0500820 if (_lock(&dev->ioctl_excl))
821 return -EBUSY;
822
Mike Lockwoodba83b012010-04-16 10:39:22 -0400823 switch (code) {
824 case MTP_SEND_FILE:
825 case MTP_RECEIVE_FILE:
826 {
827 struct mtp_file_range mfr;
Mike Lockwood491d4182010-11-08 10:41:31 -0500828 struct work_struct *work;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400829
830 spin_lock_irq(&dev->lock);
831 if (dev->state == STATE_CANCELED) {
832 /* report cancelation to userspace */
833 dev->state = STATE_READY;
834 spin_unlock_irq(&dev->lock);
Mike Lockwood491d4182010-11-08 10:41:31 -0500835 ret = -ECANCELED;
836 goto out;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400837 }
838 if (dev->state == STATE_OFFLINE) {
839 spin_unlock_irq(&dev->lock);
Mike Lockwood491d4182010-11-08 10:41:31 -0500840 ret = -ENODEV;
841 goto out;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400842 }
843 dev->state = STATE_BUSY;
844 spin_unlock_irq(&dev->lock);
845
846 if (copy_from_user(&mfr, (void __user *)value, sizeof(mfr))) {
847 ret = -EFAULT;
848 goto fail;
849 }
Mike Lockwood491d4182010-11-08 10:41:31 -0500850 /* hold a reference to the file while we are working with it */
Mike Lockwoodba83b012010-04-16 10:39:22 -0400851 filp = fget(mfr.fd);
852 if (!filp) {
853 ret = -EBADF;
854 goto fail;
855 }
856
Mike Lockwood491d4182010-11-08 10:41:31 -0500857 /* write the parameters */
858 dev->xfer_file = filp;
859 dev->xfer_file_offset = mfr.offset;
860 dev->xfer_file_length = mfr.length;
861 smp_wmb();
Mike Lockwoodba83b012010-04-16 10:39:22 -0400862
863 if (code == MTP_SEND_FILE)
Mike Lockwood491d4182010-11-08 10:41:31 -0500864 work = &dev->send_file_work;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400865 else
Mike Lockwood491d4182010-11-08 10:41:31 -0500866 work = &dev->receive_file_work;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400867
Mike Lockwood491d4182010-11-08 10:41:31 -0500868 /* We do the file transfer on a work queue so it will run
869 * in kernel context, which is necessary for vfs_read and
870 * vfs_write to use our buffers in the kernel address space.
871 */
872 queue_work(dev->wq, work);
873 /* wait for operation to complete */
874 flush_workqueue(dev->wq);
875 fput(filp);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400876
Mike Lockwood491d4182010-11-08 10:41:31 -0500877 /* read the result */
878 smp_rmb();
879 ret = dev->xfer_result;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400880 break;
881 }
882 case MTP_SET_INTERFACE_MODE:
883 if (value == MTP_INTERFACE_MODE_MTP ||
884 value == MTP_INTERFACE_MODE_PTP) {
885 dev->interface_mode = value;
886 if (value == MTP_INTERFACE_MODE_PTP) {
887 dev->function.descriptors = fs_ptp_descs;
888 dev->function.hs_descriptors = hs_ptp_descs;
889 } else {
890 dev->function.descriptors = fs_mtp_descs;
891 dev->function.hs_descriptors = hs_mtp_descs;
892 }
893 ret = 0;
894 }
895 break;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400896 case MTP_SEND_EVENT:
897 {
898 struct mtp_event event;
899 /* return here so we don't change dev->state below,
900 * which would interfere with bulk transfer state.
901 */
902 if (copy_from_user(&event, (void __user *)value, sizeof(event)))
Mike Lockwood491d4182010-11-08 10:41:31 -0500903 ret = -EFAULT;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400904 else
Mike Lockwood491d4182010-11-08 10:41:31 -0500905 ret = mtp_send_event(dev, &event);
906 goto out;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400907 }
Mike Lockwoodba83b012010-04-16 10:39:22 -0400908 }
909
910fail:
Mike Lockwoodba83b012010-04-16 10:39:22 -0400911 spin_lock_irq(&dev->lock);
912 if (dev->state == STATE_CANCELED)
913 ret = -ECANCELED;
914 else if (dev->state != STATE_OFFLINE)
915 dev->state = STATE_READY;
916 spin_unlock_irq(&dev->lock);
Mike Lockwood491d4182010-11-08 10:41:31 -0500917out:
918 _unlock(&dev->ioctl_excl);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400919 DBG(dev->cdev, "ioctl returning %d\n", ret);
920 return ret;
921}
922
923static int mtp_open(struct inode *ip, struct file *fp)
924{
925 printk(KERN_INFO "mtp_open\n");
926 if (_lock(&_mtp_dev->open_excl))
927 return -EBUSY;
928
Mike Lockwoodba83b012010-04-16 10:39:22 -0400929 /* clear any error condition */
930 if (_mtp_dev->state != STATE_OFFLINE)
931 _mtp_dev->state = STATE_READY;
932
933 fp->private_data = _mtp_dev;
934 return 0;
935}
936
937static int mtp_release(struct inode *ip, struct file *fp)
938{
939 printk(KERN_INFO "mtp_release\n");
940
Mike Lockwoodba83b012010-04-16 10:39:22 -0400941 _unlock(&_mtp_dev->open_excl);
942 return 0;
943}
944
945/* file operations for /dev/mtp_usb */
946static const struct file_operations mtp_fops = {
947 .owner = THIS_MODULE,
948 .read = mtp_read,
949 .write = mtp_write,
950 .unlocked_ioctl = mtp_ioctl,
951 .open = mtp_open,
952 .release = mtp_release,
953};
954
955static struct miscdevice mtp_device = {
956 .minor = MISC_DYNAMIC_MINOR,
957 .name = shortname,
958 .fops = &mtp_fops,
959};
960
961static int
962mtp_function_bind(struct usb_configuration *c, struct usb_function *f)
963{
964 struct usb_composite_dev *cdev = c->cdev;
965 struct mtp_dev *dev = func_to_dev(f);
966 int id;
967 int ret;
968
969 dev->cdev = cdev;
970 DBG(cdev, "mtp_function_bind dev: %p\n", dev);
971
972 /* allocate interface ID(s) */
973 id = usb_interface_id(c, f);
974 if (id < 0)
975 return id;
976 mtp_interface_desc.bInterfaceNumber = id;
977
978 /* allocate endpoints */
979 ret = create_bulk_endpoints(dev, &mtp_fullspeed_in_desc,
980 &mtp_fullspeed_out_desc, &mtp_intr_desc);
981 if (ret)
982 return ret;
983
984 /* support high speed hardware */
985 if (gadget_is_dualspeed(c->cdev->gadget)) {
986 mtp_highspeed_in_desc.bEndpointAddress =
987 mtp_fullspeed_in_desc.bEndpointAddress;
988 mtp_highspeed_out_desc.bEndpointAddress =
989 mtp_fullspeed_out_desc.bEndpointAddress;
990 }
991
992 DBG(cdev, "%s speed %s: IN/%s, OUT/%s\n",
993 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
994 f->name, dev->ep_in->name, dev->ep_out->name);
995 return 0;
996}
997
998static void
999mtp_function_unbind(struct usb_configuration *c, struct usb_function *f)
1000{
1001 struct mtp_dev *dev = func_to_dev(f);
1002 struct usb_request *req;
1003 int i;
1004
1005 spin_lock_irq(&dev->lock);
1006 while ((req = req_get(dev, &dev->tx_idle)))
1007 mtp_request_free(req, dev->ep_in);
1008 for (i = 0; i < RX_REQ_MAX; i++)
1009 mtp_request_free(dev->rx_req[i], dev->ep_out);
Mike Lockwood1de4d4d2010-07-06 19:27:52 -04001010 mtp_request_free(dev->intr_req, dev->ep_intr);
Mike Lockwoodba83b012010-04-16 10:39:22 -04001011 dev->state = STATE_OFFLINE;
1012 spin_unlock_irq(&dev->lock);
Mike Lockwood1de4d4d2010-07-06 19:27:52 -04001013 wake_up(&dev->intr_wq);
Mike Lockwoodba83b012010-04-16 10:39:22 -04001014
1015 misc_deregister(&mtp_device);
1016 kfree(_mtp_dev);
1017 _mtp_dev = NULL;
1018}
1019
1020static int mtp_function_setup(struct usb_function *f,
1021 const struct usb_ctrlrequest *ctrl)
1022{
1023 struct mtp_dev *dev = func_to_dev(f);
1024 struct usb_composite_dev *cdev = dev->cdev;
1025 int value = -EOPNOTSUPP;
1026 u16 w_index = le16_to_cpu(ctrl->wIndex);
1027 u16 w_value = le16_to_cpu(ctrl->wValue);
1028 u16 w_length = le16_to_cpu(ctrl->wLength);
1029 unsigned long flags;
1030
1031 /* do nothing if we are disabled */
1032 if (dev->function.disabled)
1033 return value;
1034
1035 VDBG(cdev, "mtp_function_setup "
1036 "%02x.%02x v%04x i%04x l%u\n",
1037 ctrl->bRequestType, ctrl->bRequest,
1038 w_value, w_index, w_length);
1039
1040 /* Handle MTP OS string */
1041 if (dev->interface_mode == MTP_INTERFACE_MODE_MTP
1042 && ctrl->bRequestType ==
1043 (USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE)
1044 && ctrl->bRequest == USB_REQ_GET_DESCRIPTOR
1045 && (w_value >> 8) == USB_DT_STRING
1046 && (w_value & 0xFF) == MTP_OS_STRING_ID) {
1047 value = (w_length < sizeof(mtp_os_string)
1048 ? w_length : sizeof(mtp_os_string));
1049 memcpy(cdev->req->buf, mtp_os_string, value);
1050 /* return here since composite.c will send for us */
1051 return value;
1052 }
1053 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_VENDOR) {
1054 /* Handle MTP OS descriptor */
1055 DBG(cdev, "vendor request: %d index: %d value: %d length: %d\n",
1056 ctrl->bRequest, w_index, w_value, w_length);
1057
1058 if (dev->interface_mode == MTP_INTERFACE_MODE_MTP
1059 && ctrl->bRequest == 1
1060 && (ctrl->bRequestType & USB_DIR_IN)
1061 && (w_index == 4 || w_index == 5)) {
1062 value = (w_length < sizeof(mtp_ext_config_desc) ?
1063 w_length : sizeof(mtp_ext_config_desc));
1064 memcpy(cdev->req->buf, &mtp_ext_config_desc, value);
1065 }
1066 }
1067 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_CLASS) {
1068 DBG(cdev, "class request: %d index: %d value: %d length: %d\n",
1069 ctrl->bRequest, w_index, w_value, w_length);
1070
1071 if (ctrl->bRequest == MTP_REQ_CANCEL && w_index == 0
1072 && w_value == 0) {
1073 DBG(cdev, "MTP_REQ_CANCEL\n");
1074
1075 spin_lock_irqsave(&dev->lock, flags);
1076 if (dev->state == STATE_BUSY) {
1077 dev->state = STATE_CANCELED;
1078 wake_up(&dev->read_wq);
1079 wake_up(&dev->write_wq);
1080 }
1081 spin_unlock_irqrestore(&dev->lock, flags);
1082
1083 /* We need to queue a request to read the remaining
1084 * bytes, but we don't actually need to look at
1085 * the contents.
1086 */
1087 value = w_length;
1088 } else if (ctrl->bRequest == MTP_REQ_GET_DEVICE_STATUS
1089 && w_index == 0 && w_value == 0) {
1090 struct mtp_device_status *status = cdev->req->buf;
1091 status->wLength =
1092 __constant_cpu_to_le16(sizeof(*status));
1093
1094 DBG(cdev, "MTP_REQ_GET_DEVICE_STATUS\n");
1095 spin_lock_irqsave(&dev->lock, flags);
1096 /* device status is "busy" until we report
1097 * the cancelation to userspace
1098 */
1099 if (dev->state == STATE_BUSY
1100 || dev->state == STATE_CANCELED)
1101 status->wCode =
1102 __cpu_to_le16(MTP_RESPONSE_DEVICE_BUSY);
1103 else
1104 status->wCode =
1105 __cpu_to_le16(MTP_RESPONSE_OK);
1106 spin_unlock_irqrestore(&dev->lock, flags);
1107 value = sizeof(*status);
1108 }
1109 }
1110
1111 /* respond with data transfer or status phase? */
1112 if (value >= 0) {
1113 int rc;
1114 cdev->req->zero = value < w_length;
1115 cdev->req->length = value;
1116 rc = usb_ep_queue(cdev->gadget->ep0, cdev->req, GFP_ATOMIC);
1117 if (rc < 0)
1118 ERROR(cdev, "%s setup response queue error\n", __func__);
1119 }
1120
1121 if (value == -EOPNOTSUPP)
1122 VDBG(cdev,
1123 "unknown class-specific control req "
1124 "%02x.%02x v%04x i%04x l%u\n",
1125 ctrl->bRequestType, ctrl->bRequest,
1126 w_value, w_index, w_length);
1127 return value;
1128}
1129
1130static int mtp_function_set_alt(struct usb_function *f,
1131 unsigned intf, unsigned alt)
1132{
1133 struct mtp_dev *dev = func_to_dev(f);
1134 struct usb_composite_dev *cdev = f->config->cdev;
1135 int ret;
1136
1137 DBG(cdev, "mtp_function_set_alt intf: %d alt: %d\n", intf, alt);
1138 ret = usb_ep_enable(dev->ep_in,
1139 ep_choose(cdev->gadget,
1140 &mtp_highspeed_in_desc,
1141 &mtp_fullspeed_in_desc));
1142 if (ret)
1143 return ret;
1144 ret = usb_ep_enable(dev->ep_out,
1145 ep_choose(cdev->gadget,
1146 &mtp_highspeed_out_desc,
1147 &mtp_fullspeed_out_desc));
1148 if (ret) {
1149 usb_ep_disable(dev->ep_in);
1150 return ret;
1151 }
1152 ret = usb_ep_enable(dev->ep_intr, &mtp_intr_desc);
1153 if (ret) {
1154 usb_ep_disable(dev->ep_out);
1155 usb_ep_disable(dev->ep_in);
1156 return ret;
1157 }
1158 dev->state = STATE_READY;
1159
1160 /* readers may be blocked waiting for us to go online */
1161 wake_up(&dev->read_wq);
1162 return 0;
1163}
1164
1165static void mtp_function_disable(struct usb_function *f)
1166{
1167 struct mtp_dev *dev = func_to_dev(f);
1168 struct usb_composite_dev *cdev = dev->cdev;
1169
1170 DBG(cdev, "mtp_function_disable\n");
1171 dev->state = STATE_OFFLINE;
1172 usb_ep_disable(dev->ep_in);
1173 usb_ep_disable(dev->ep_out);
1174 usb_ep_disable(dev->ep_intr);
1175
1176 /* readers may be blocked waiting for us to go online */
1177 wake_up(&dev->read_wq);
Mike Lockwood1de4d4d2010-07-06 19:27:52 -04001178 wake_up(&dev->intr_wq);
Mike Lockwoodba83b012010-04-16 10:39:22 -04001179
1180 VDBG(cdev, "%s disabled\n", dev->function.name);
1181}
1182
1183static int mtp_bind_config(struct usb_configuration *c)
1184{
1185 struct mtp_dev *dev;
1186 int ret;
1187
1188 printk(KERN_INFO "mtp_bind_config\n");
1189
1190 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1191 if (!dev)
1192 return -ENOMEM;
1193
1194 /* allocate a string ID for our interface */
1195 if (mtp_string_defs[INTERFACE_STRING_INDEX].id == 0) {
1196 ret = usb_string_id(c->cdev);
1197 if (ret < 0)
1198 return ret;
1199 mtp_string_defs[INTERFACE_STRING_INDEX].id = ret;
1200 mtp_interface_desc.iInterface = ret;
1201 }
1202
1203 spin_lock_init(&dev->lock);
Mike Lockwoodba83b012010-04-16 10:39:22 -04001204 init_waitqueue_head(&dev->read_wq);
1205 init_waitqueue_head(&dev->write_wq);
Mike Lockwood1de4d4d2010-07-06 19:27:52 -04001206 init_waitqueue_head(&dev->intr_wq);
Mike Lockwoodba83b012010-04-16 10:39:22 -04001207 atomic_set(&dev->open_excl, 0);
Mike Lockwood491d4182010-11-08 10:41:31 -05001208 atomic_set(&dev->ioctl_excl, 0);
Mike Lockwoodba83b012010-04-16 10:39:22 -04001209 INIT_LIST_HEAD(&dev->tx_idle);
Mike Lockwood491d4182010-11-08 10:41:31 -05001210
1211 dev->wq = create_singlethread_workqueue("f_mtp");
1212 if (!dev->wq)
1213 goto err1;
1214 INIT_WORK(&dev->send_file_work, send_file_work);
1215 INIT_WORK(&dev->receive_file_work, receive_file_work);
Mike Lockwoodba83b012010-04-16 10:39:22 -04001216
1217 dev->cdev = c->cdev;
1218 dev->function.name = "mtp";
1219 dev->function.strings = mtp_strings,
1220 dev->function.descriptors = fs_mtp_descs;
1221 dev->function.hs_descriptors = hs_mtp_descs;
1222 dev->function.bind = mtp_function_bind;
1223 dev->function.unbind = mtp_function_unbind;
1224 dev->function.setup = mtp_function_setup;
1225 dev->function.set_alt = mtp_function_set_alt;
1226 dev->function.disable = mtp_function_disable;
1227
1228 /* MTP mode by default */
1229 dev->interface_mode = MTP_INTERFACE_MODE_MTP;
1230
1231 /* _mtp_dev must be set before calling usb_gadget_register_driver */
1232 _mtp_dev = dev;
1233
1234 ret = misc_register(&mtp_device);
1235 if (ret)
1236 goto err1;
1237
1238 ret = usb_add_function(c, &dev->function);
1239 if (ret)
1240 goto err2;
1241
1242 return 0;
1243
1244err2:
1245 misc_deregister(&mtp_device);
1246err1:
Mike Lockwood491d4182010-11-08 10:41:31 -05001247 if (dev->wq)
1248 destroy_workqueue(dev->wq);
Mike Lockwoodba83b012010-04-16 10:39:22 -04001249 kfree(dev);
1250 printk(KERN_ERR "mtp gadget driver failed to initialize\n");
1251 return ret;
1252}
1253
1254static struct android_usb_function mtp_function = {
1255 .name = "mtp",
1256 .bind_config = mtp_bind_config,
1257};
1258
1259static int __init init(void)
1260{
1261 printk(KERN_INFO "f_mtp init\n");
1262 android_register_function(&mtp_function);
1263 return 0;
1264}
1265module_init(init);