blob: 81cc01868397017777a0375e48465bd03d3ddfea [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;
110 size_t xfer_file_length;
111 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;
543 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
561 while (count > 0) {
562 if (dev->state != STATE_BUSY) {
563 DBG(cdev, "mtp_write dev->error\n");
564 r = -EIO;
565 break;
566 }
567
568 /* get an idle tx request to use */
569 req = 0;
570 ret = wait_event_interruptible(dev->write_wq,
571 ((req = req_get(dev, &dev->tx_idle))
572 || dev->state != STATE_BUSY));
573 if (!req) {
574 r = ret;
575 break;
576 }
577
578 if (count > BULK_BUFFER_SIZE)
579 xfer = BULK_BUFFER_SIZE;
580 else
581 xfer = count;
582 if (copy_from_user(req->buf, buf, xfer)) {
583 r = -EFAULT;
584 break;
585 }
586
587 req->length = xfer;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400588 ret = usb_ep_queue(dev->ep_in, req, GFP_KERNEL);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400589 if (ret < 0) {
590 DBG(cdev, "mtp_write: xfer error %d\n", ret);
591 r = -EIO;
592 break;
593 }
594
595 buf += xfer;
596 count -= xfer;
597
598 /* zero this so we don't try to free it on error exit */
599 req = 0;
600 }
601
602 if (req)
603 req_put(dev, &dev->tx_idle, req);
604
605 spin_lock_irq(&dev->lock);
606 if (dev->state == STATE_CANCELED)
607 r = -ECANCELED;
608 else if (dev->state != STATE_OFFLINE)
609 dev->state = STATE_READY;
610 spin_unlock_irq(&dev->lock);
611
612 DBG(cdev, "mtp_write returning %d\n", r);
613 return r;
614}
615
Mike Lockwood491d4182010-11-08 10:41:31 -0500616/* read from a local file and write to USB */
617static void send_file_work(struct work_struct *data) {
618 struct mtp_dev *dev = container_of(data, struct mtp_dev, send_file_work);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400619 struct usb_composite_dev *cdev = dev->cdev;
620 struct usb_request *req = 0;
Mike Lockwood491d4182010-11-08 10:41:31 -0500621 struct file *filp;
622 loff_t offset;
623 size_t count;
Mike Lockwood76ac6552010-11-15 15:22:21 -0500624 int xfer, ret;
625 int r = 0;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400626
Mike Lockwood491d4182010-11-08 10:41:31 -0500627 /* read our parameters */
628 smp_rmb();
629 filp = dev->xfer_file;
630 offset = dev->xfer_file_offset;
Mike Lockwood76ac6552010-11-15 15:22:21 -0500631 count = dev->xfer_file_length;
Mike Lockwood491d4182010-11-08 10:41:31 -0500632
Mike Lockwood76ac6552010-11-15 15:22:21 -0500633 DBG(cdev, "send_file_work(%lld %u)\n", offset, count);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400634
635 while (count > 0) {
636 /* get an idle tx request to use */
637 req = 0;
638 ret = wait_event_interruptible(dev->write_wq,
639 (req = req_get(dev, &dev->tx_idle))
640 || dev->state != STATE_BUSY);
641 if (!req) {
642 r = ret;
643 break;
644 }
645
646 if (count > BULK_BUFFER_SIZE)
647 xfer = BULK_BUFFER_SIZE;
648 else
649 xfer = count;
650 ret = vfs_read(filp, req->buf, xfer, &offset);
651 if (ret < 0) {
652 r = ret;
653 break;
654 }
655 xfer = ret;
656
657 req->length = xfer;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400658 ret = usb_ep_queue(dev->ep_in, req, GFP_KERNEL);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400659 if (ret < 0) {
Mike Lockwood491d4182010-11-08 10:41:31 -0500660 DBG(cdev, "send_file_work: xfer error %d\n", ret);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400661 dev->state = STATE_ERROR;
662 r = -EIO;
663 break;
664 }
665
666 count -= xfer;
667
668 /* zero this so we don't try to free it on error exit */
669 req = 0;
670 }
671
672 if (req)
673 req_put(dev, &dev->tx_idle, req);
674
Mike Lockwood491d4182010-11-08 10:41:31 -0500675 DBG(cdev, "send_file_work returning %d\n", r);
676 /* write the result */
677 dev->xfer_result = r;
678 smp_wmb();
Mike Lockwoodba83b012010-04-16 10:39:22 -0400679}
680
Mike Lockwood491d4182010-11-08 10:41:31 -0500681/* read from USB and write to a local file */
682static void receive_file_work(struct work_struct *data)
Mike Lockwoodba83b012010-04-16 10:39:22 -0400683{
Mike Lockwood491d4182010-11-08 10:41:31 -0500684 struct mtp_dev *dev = container_of(data, struct mtp_dev, receive_file_work);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400685 struct usb_composite_dev *cdev = dev->cdev;
686 struct usb_request *read_req = NULL, *write_req = NULL;
Mike Lockwood491d4182010-11-08 10:41:31 -0500687 struct file *filp;
688 loff_t offset;
689 size_t count;
Mike Lockwood76ac6552010-11-15 15:22:21 -0500690 int ret, cur_buf = 0;
691 int r = 0;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400692
Mike Lockwood491d4182010-11-08 10:41:31 -0500693 /* read our parameters */
694 smp_rmb();
695 filp = dev->xfer_file;
696 offset = dev->xfer_file_offset;
Mike Lockwood76ac6552010-11-15 15:22:21 -0500697 count = dev->xfer_file_length;
Mike Lockwood491d4182010-11-08 10:41:31 -0500698
Mike Lockwood76ac6552010-11-15 15:22:21 -0500699 DBG(cdev, "receive_file_work(%u)\n", count);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400700
701 while (count > 0 || write_req) {
702 if (count > 0) {
703 /* queue a request */
704 read_req = dev->rx_req[cur_buf];
705 cur_buf = (cur_buf + 1) % RX_REQ_MAX;
706
707 read_req->length = (count > BULK_BUFFER_SIZE
708 ? BULK_BUFFER_SIZE : count);
709 dev->rx_done = 0;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400710 ret = usb_ep_queue(dev->ep_out, read_req, GFP_KERNEL);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400711 if (ret < 0) {
712 r = -EIO;
713 dev->state = STATE_ERROR;
714 break;
715 }
716 count -= ret;
717 }
718
719 if (write_req) {
720 DBG(cdev, "rx %p %d\n", write_req, write_req->actual);
721 ret = vfs_write(filp, write_req->buf, write_req->actual,
722 &offset);
723 DBG(cdev, "vfs_write %d\n", ret);
724 if (ret != write_req->actual) {
725 r = -EIO;
726 dev->state = STATE_ERROR;
727 break;
728 }
729 write_req = NULL;
730 }
731
732 if (read_req) {
733 /* wait for our last read to complete */
734 ret = wait_event_interruptible(dev->read_wq,
735 dev->rx_done || dev->state != STATE_BUSY);
736 if (ret < 0 || dev->state != STATE_BUSY) {
737 r = ret;
738 break;
739 }
740 count -= read_req->actual;
741 write_req = read_req;
742 read_req = NULL;
743 }
744 }
745
Mike Lockwood491d4182010-11-08 10:41:31 -0500746 DBG(cdev, "receive_file_work returning %d\n", r);
747 /* write the result */
748 dev->xfer_result = r;
749 smp_wmb();
Mike Lockwoodba83b012010-04-16 10:39:22 -0400750}
751
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400752static int mtp_send_event(struct mtp_dev *dev, struct mtp_event *event)
753{
754 struct usb_request *req;
755 int ret;
756 int length = event->length;
757
758 DBG(dev->cdev, "mtp_send_event(%d)\n", event->length);
759
760 if (length < 0 || length > INTR_BUFFER_SIZE)
761 return -EINVAL;
762
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400763 /* wait for a request to complete */
764 ret = wait_event_interruptible(dev->intr_wq, !dev->intr_busy || dev->state == STATE_OFFLINE);
765 if (ret < 0)
Mike Lockwood491d4182010-11-08 10:41:31 -0500766 return ret;
767 if (dev->state == STATE_OFFLINE)
768 return -ENODEV;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400769 req = dev->intr_req;
Mike Lockwood491d4182010-11-08 10:41:31 -0500770 if (copy_from_user(req->buf, (void __user *)event->data, length))
771 return -EFAULT;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400772 req->length = length;
773 dev->intr_busy = 1;
774 ret = usb_ep_queue(dev->ep_intr, req, GFP_KERNEL);
775 if (ret)
776 dev->intr_busy = 0;
777
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400778 return ret;
779}
780
Mike Lockwoodba83b012010-04-16 10:39:22 -0400781static long mtp_ioctl(struct file *fp, unsigned code, unsigned long value)
782{
783 struct mtp_dev *dev = fp->private_data;
784 struct file *filp = NULL;
785 int ret = -EINVAL;
786
Mike Lockwood491d4182010-11-08 10:41:31 -0500787 if (_lock(&dev->ioctl_excl))
788 return -EBUSY;
789
Mike Lockwoodba83b012010-04-16 10:39:22 -0400790 switch (code) {
791 case MTP_SEND_FILE:
792 case MTP_RECEIVE_FILE:
793 {
794 struct mtp_file_range mfr;
Mike Lockwood491d4182010-11-08 10:41:31 -0500795 struct work_struct *work;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400796
797 spin_lock_irq(&dev->lock);
798 if (dev->state == STATE_CANCELED) {
799 /* report cancelation to userspace */
800 dev->state = STATE_READY;
801 spin_unlock_irq(&dev->lock);
Mike Lockwood491d4182010-11-08 10:41:31 -0500802 ret = -ECANCELED;
803 goto out;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400804 }
805 if (dev->state == STATE_OFFLINE) {
806 spin_unlock_irq(&dev->lock);
Mike Lockwood491d4182010-11-08 10:41:31 -0500807 ret = -ENODEV;
808 goto out;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400809 }
810 dev->state = STATE_BUSY;
811 spin_unlock_irq(&dev->lock);
812
813 if (copy_from_user(&mfr, (void __user *)value, sizeof(mfr))) {
814 ret = -EFAULT;
815 goto fail;
816 }
Mike Lockwood491d4182010-11-08 10:41:31 -0500817 /* hold a reference to the file while we are working with it */
Mike Lockwoodba83b012010-04-16 10:39:22 -0400818 filp = fget(mfr.fd);
819 if (!filp) {
820 ret = -EBADF;
821 goto fail;
822 }
823
Mike Lockwood491d4182010-11-08 10:41:31 -0500824 /* write the parameters */
825 dev->xfer_file = filp;
826 dev->xfer_file_offset = mfr.offset;
827 dev->xfer_file_length = mfr.length;
828 smp_wmb();
Mike Lockwoodba83b012010-04-16 10:39:22 -0400829
830 if (code == MTP_SEND_FILE)
Mike Lockwood491d4182010-11-08 10:41:31 -0500831 work = &dev->send_file_work;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400832 else
Mike Lockwood491d4182010-11-08 10:41:31 -0500833 work = &dev->receive_file_work;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400834
Mike Lockwood491d4182010-11-08 10:41:31 -0500835 /* We do the file transfer on a work queue so it will run
836 * in kernel context, which is necessary for vfs_read and
837 * vfs_write to use our buffers in the kernel address space.
838 */
839 queue_work(dev->wq, work);
840 /* wait for operation to complete */
841 flush_workqueue(dev->wq);
842 fput(filp);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400843
Mike Lockwood491d4182010-11-08 10:41:31 -0500844 /* read the result */
845 smp_rmb();
846 ret = dev->xfer_result;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400847 break;
848 }
849 case MTP_SET_INTERFACE_MODE:
850 if (value == MTP_INTERFACE_MODE_MTP ||
851 value == MTP_INTERFACE_MODE_PTP) {
852 dev->interface_mode = value;
853 if (value == MTP_INTERFACE_MODE_PTP) {
854 dev->function.descriptors = fs_ptp_descs;
855 dev->function.hs_descriptors = hs_ptp_descs;
856 } else {
857 dev->function.descriptors = fs_mtp_descs;
858 dev->function.hs_descriptors = hs_mtp_descs;
859 }
860 ret = 0;
861 }
862 break;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400863 case MTP_SEND_EVENT:
864 {
865 struct mtp_event event;
866 /* return here so we don't change dev->state below,
867 * which would interfere with bulk transfer state.
868 */
869 if (copy_from_user(&event, (void __user *)value, sizeof(event)))
Mike Lockwood491d4182010-11-08 10:41:31 -0500870 ret = -EFAULT;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400871 else
Mike Lockwood491d4182010-11-08 10:41:31 -0500872 ret = mtp_send_event(dev, &event);
873 goto out;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400874 }
Mike Lockwoodba83b012010-04-16 10:39:22 -0400875 }
876
877fail:
Mike Lockwoodba83b012010-04-16 10:39:22 -0400878 spin_lock_irq(&dev->lock);
879 if (dev->state == STATE_CANCELED)
880 ret = -ECANCELED;
881 else if (dev->state != STATE_OFFLINE)
882 dev->state = STATE_READY;
883 spin_unlock_irq(&dev->lock);
Mike Lockwood491d4182010-11-08 10:41:31 -0500884out:
885 _unlock(&dev->ioctl_excl);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400886 DBG(dev->cdev, "ioctl returning %d\n", ret);
887 return ret;
888}
889
890static int mtp_open(struct inode *ip, struct file *fp)
891{
892 printk(KERN_INFO "mtp_open\n");
893 if (_lock(&_mtp_dev->open_excl))
894 return -EBUSY;
895
Mike Lockwoodba83b012010-04-16 10:39:22 -0400896 /* clear any error condition */
897 if (_mtp_dev->state != STATE_OFFLINE)
898 _mtp_dev->state = STATE_READY;
899
900 fp->private_data = _mtp_dev;
901 return 0;
902}
903
904static int mtp_release(struct inode *ip, struct file *fp)
905{
906 printk(KERN_INFO "mtp_release\n");
907
Mike Lockwoodba83b012010-04-16 10:39:22 -0400908 _unlock(&_mtp_dev->open_excl);
909 return 0;
910}
911
912/* file operations for /dev/mtp_usb */
913static const struct file_operations mtp_fops = {
914 .owner = THIS_MODULE,
915 .read = mtp_read,
916 .write = mtp_write,
917 .unlocked_ioctl = mtp_ioctl,
918 .open = mtp_open,
919 .release = mtp_release,
920};
921
922static struct miscdevice mtp_device = {
923 .minor = MISC_DYNAMIC_MINOR,
924 .name = shortname,
925 .fops = &mtp_fops,
926};
927
928static int
929mtp_function_bind(struct usb_configuration *c, struct usb_function *f)
930{
931 struct usb_composite_dev *cdev = c->cdev;
932 struct mtp_dev *dev = func_to_dev(f);
933 int id;
934 int ret;
935
936 dev->cdev = cdev;
937 DBG(cdev, "mtp_function_bind dev: %p\n", dev);
938
939 /* allocate interface ID(s) */
940 id = usb_interface_id(c, f);
941 if (id < 0)
942 return id;
943 mtp_interface_desc.bInterfaceNumber = id;
944
945 /* allocate endpoints */
946 ret = create_bulk_endpoints(dev, &mtp_fullspeed_in_desc,
947 &mtp_fullspeed_out_desc, &mtp_intr_desc);
948 if (ret)
949 return ret;
950
951 /* support high speed hardware */
952 if (gadget_is_dualspeed(c->cdev->gadget)) {
953 mtp_highspeed_in_desc.bEndpointAddress =
954 mtp_fullspeed_in_desc.bEndpointAddress;
955 mtp_highspeed_out_desc.bEndpointAddress =
956 mtp_fullspeed_out_desc.bEndpointAddress;
957 }
958
959 DBG(cdev, "%s speed %s: IN/%s, OUT/%s\n",
960 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
961 f->name, dev->ep_in->name, dev->ep_out->name);
962 return 0;
963}
964
965static void
966mtp_function_unbind(struct usb_configuration *c, struct usb_function *f)
967{
968 struct mtp_dev *dev = func_to_dev(f);
969 struct usb_request *req;
970 int i;
971
972 spin_lock_irq(&dev->lock);
973 while ((req = req_get(dev, &dev->tx_idle)))
974 mtp_request_free(req, dev->ep_in);
975 for (i = 0; i < RX_REQ_MAX; i++)
976 mtp_request_free(dev->rx_req[i], dev->ep_out);
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400977 mtp_request_free(dev->intr_req, dev->ep_intr);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400978 dev->state = STATE_OFFLINE;
979 spin_unlock_irq(&dev->lock);
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400980 wake_up(&dev->intr_wq);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400981
982 misc_deregister(&mtp_device);
983 kfree(_mtp_dev);
984 _mtp_dev = NULL;
985}
986
987static int mtp_function_setup(struct usb_function *f,
988 const struct usb_ctrlrequest *ctrl)
989{
990 struct mtp_dev *dev = func_to_dev(f);
991 struct usb_composite_dev *cdev = dev->cdev;
992 int value = -EOPNOTSUPP;
993 u16 w_index = le16_to_cpu(ctrl->wIndex);
994 u16 w_value = le16_to_cpu(ctrl->wValue);
995 u16 w_length = le16_to_cpu(ctrl->wLength);
996 unsigned long flags;
997
998 /* do nothing if we are disabled */
999 if (dev->function.disabled)
1000 return value;
1001
1002 VDBG(cdev, "mtp_function_setup "
1003 "%02x.%02x v%04x i%04x l%u\n",
1004 ctrl->bRequestType, ctrl->bRequest,
1005 w_value, w_index, w_length);
1006
1007 /* Handle MTP OS string */
1008 if (dev->interface_mode == MTP_INTERFACE_MODE_MTP
1009 && ctrl->bRequestType ==
1010 (USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE)
1011 && ctrl->bRequest == USB_REQ_GET_DESCRIPTOR
1012 && (w_value >> 8) == USB_DT_STRING
1013 && (w_value & 0xFF) == MTP_OS_STRING_ID) {
1014 value = (w_length < sizeof(mtp_os_string)
1015 ? w_length : sizeof(mtp_os_string));
1016 memcpy(cdev->req->buf, mtp_os_string, value);
1017 /* return here since composite.c will send for us */
1018 return value;
1019 }
1020 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_VENDOR) {
1021 /* Handle MTP OS descriptor */
1022 DBG(cdev, "vendor request: %d index: %d value: %d length: %d\n",
1023 ctrl->bRequest, w_index, w_value, w_length);
1024
1025 if (dev->interface_mode == MTP_INTERFACE_MODE_MTP
1026 && ctrl->bRequest == 1
1027 && (ctrl->bRequestType & USB_DIR_IN)
1028 && (w_index == 4 || w_index == 5)) {
1029 value = (w_length < sizeof(mtp_ext_config_desc) ?
1030 w_length : sizeof(mtp_ext_config_desc));
1031 memcpy(cdev->req->buf, &mtp_ext_config_desc, value);
1032 }
1033 }
1034 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_CLASS) {
1035 DBG(cdev, "class request: %d index: %d value: %d length: %d\n",
1036 ctrl->bRequest, w_index, w_value, w_length);
1037
1038 if (ctrl->bRequest == MTP_REQ_CANCEL && w_index == 0
1039 && w_value == 0) {
1040 DBG(cdev, "MTP_REQ_CANCEL\n");
1041
1042 spin_lock_irqsave(&dev->lock, flags);
1043 if (dev->state == STATE_BUSY) {
1044 dev->state = STATE_CANCELED;
1045 wake_up(&dev->read_wq);
1046 wake_up(&dev->write_wq);
1047 }
1048 spin_unlock_irqrestore(&dev->lock, flags);
1049
1050 /* We need to queue a request to read the remaining
1051 * bytes, but we don't actually need to look at
1052 * the contents.
1053 */
1054 value = w_length;
1055 } else if (ctrl->bRequest == MTP_REQ_GET_DEVICE_STATUS
1056 && w_index == 0 && w_value == 0) {
1057 struct mtp_device_status *status = cdev->req->buf;
1058 status->wLength =
1059 __constant_cpu_to_le16(sizeof(*status));
1060
1061 DBG(cdev, "MTP_REQ_GET_DEVICE_STATUS\n");
1062 spin_lock_irqsave(&dev->lock, flags);
1063 /* device status is "busy" until we report
1064 * the cancelation to userspace
1065 */
1066 if (dev->state == STATE_BUSY
1067 || dev->state == STATE_CANCELED)
1068 status->wCode =
1069 __cpu_to_le16(MTP_RESPONSE_DEVICE_BUSY);
1070 else
1071 status->wCode =
1072 __cpu_to_le16(MTP_RESPONSE_OK);
1073 spin_unlock_irqrestore(&dev->lock, flags);
1074 value = sizeof(*status);
1075 }
1076 }
1077
1078 /* respond with data transfer or status phase? */
1079 if (value >= 0) {
1080 int rc;
1081 cdev->req->zero = value < w_length;
1082 cdev->req->length = value;
1083 rc = usb_ep_queue(cdev->gadget->ep0, cdev->req, GFP_ATOMIC);
1084 if (rc < 0)
1085 ERROR(cdev, "%s setup response queue error\n", __func__);
1086 }
1087
1088 if (value == -EOPNOTSUPP)
1089 VDBG(cdev,
1090 "unknown class-specific control req "
1091 "%02x.%02x v%04x i%04x l%u\n",
1092 ctrl->bRequestType, ctrl->bRequest,
1093 w_value, w_index, w_length);
1094 return value;
1095}
1096
1097static int mtp_function_set_alt(struct usb_function *f,
1098 unsigned intf, unsigned alt)
1099{
1100 struct mtp_dev *dev = func_to_dev(f);
1101 struct usb_composite_dev *cdev = f->config->cdev;
1102 int ret;
1103
1104 DBG(cdev, "mtp_function_set_alt intf: %d alt: %d\n", intf, alt);
1105 ret = usb_ep_enable(dev->ep_in,
1106 ep_choose(cdev->gadget,
1107 &mtp_highspeed_in_desc,
1108 &mtp_fullspeed_in_desc));
1109 if (ret)
1110 return ret;
1111 ret = usb_ep_enable(dev->ep_out,
1112 ep_choose(cdev->gadget,
1113 &mtp_highspeed_out_desc,
1114 &mtp_fullspeed_out_desc));
1115 if (ret) {
1116 usb_ep_disable(dev->ep_in);
1117 return ret;
1118 }
1119 ret = usb_ep_enable(dev->ep_intr, &mtp_intr_desc);
1120 if (ret) {
1121 usb_ep_disable(dev->ep_out);
1122 usb_ep_disable(dev->ep_in);
1123 return ret;
1124 }
1125 dev->state = STATE_READY;
1126
1127 /* readers may be blocked waiting for us to go online */
1128 wake_up(&dev->read_wq);
1129 return 0;
1130}
1131
1132static void mtp_function_disable(struct usb_function *f)
1133{
1134 struct mtp_dev *dev = func_to_dev(f);
1135 struct usb_composite_dev *cdev = dev->cdev;
1136
1137 DBG(cdev, "mtp_function_disable\n");
1138 dev->state = STATE_OFFLINE;
1139 usb_ep_disable(dev->ep_in);
1140 usb_ep_disable(dev->ep_out);
1141 usb_ep_disable(dev->ep_intr);
1142
1143 /* readers may be blocked waiting for us to go online */
1144 wake_up(&dev->read_wq);
Mike Lockwood1de4d4d2010-07-06 19:27:52 -04001145 wake_up(&dev->intr_wq);
Mike Lockwoodba83b012010-04-16 10:39:22 -04001146
1147 VDBG(cdev, "%s disabled\n", dev->function.name);
1148}
1149
1150static int mtp_bind_config(struct usb_configuration *c)
1151{
1152 struct mtp_dev *dev;
1153 int ret;
1154
1155 printk(KERN_INFO "mtp_bind_config\n");
1156
1157 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1158 if (!dev)
1159 return -ENOMEM;
1160
1161 /* allocate a string ID for our interface */
1162 if (mtp_string_defs[INTERFACE_STRING_INDEX].id == 0) {
1163 ret = usb_string_id(c->cdev);
1164 if (ret < 0)
1165 return ret;
1166 mtp_string_defs[INTERFACE_STRING_INDEX].id = ret;
1167 mtp_interface_desc.iInterface = ret;
1168 }
1169
1170 spin_lock_init(&dev->lock);
Mike Lockwoodba83b012010-04-16 10:39:22 -04001171 init_waitqueue_head(&dev->read_wq);
1172 init_waitqueue_head(&dev->write_wq);
Mike Lockwood1de4d4d2010-07-06 19:27:52 -04001173 init_waitqueue_head(&dev->intr_wq);
Mike Lockwoodba83b012010-04-16 10:39:22 -04001174 atomic_set(&dev->open_excl, 0);
Mike Lockwood491d4182010-11-08 10:41:31 -05001175 atomic_set(&dev->ioctl_excl, 0);
Mike Lockwoodba83b012010-04-16 10:39:22 -04001176 INIT_LIST_HEAD(&dev->tx_idle);
Mike Lockwood491d4182010-11-08 10:41:31 -05001177
1178 dev->wq = create_singlethread_workqueue("f_mtp");
1179 if (!dev->wq)
1180 goto err1;
1181 INIT_WORK(&dev->send_file_work, send_file_work);
1182 INIT_WORK(&dev->receive_file_work, receive_file_work);
Mike Lockwoodba83b012010-04-16 10:39:22 -04001183
1184 dev->cdev = c->cdev;
1185 dev->function.name = "mtp";
1186 dev->function.strings = mtp_strings,
1187 dev->function.descriptors = fs_mtp_descs;
1188 dev->function.hs_descriptors = hs_mtp_descs;
1189 dev->function.bind = mtp_function_bind;
1190 dev->function.unbind = mtp_function_unbind;
1191 dev->function.setup = mtp_function_setup;
1192 dev->function.set_alt = mtp_function_set_alt;
1193 dev->function.disable = mtp_function_disable;
1194
1195 /* MTP mode by default */
1196 dev->interface_mode = MTP_INTERFACE_MODE_MTP;
1197
1198 /* _mtp_dev must be set before calling usb_gadget_register_driver */
1199 _mtp_dev = dev;
1200
1201 ret = misc_register(&mtp_device);
1202 if (ret)
1203 goto err1;
1204
1205 ret = usb_add_function(c, &dev->function);
1206 if (ret)
1207 goto err2;
1208
1209 return 0;
1210
1211err2:
1212 misc_deregister(&mtp_device);
1213err1:
Mike Lockwood491d4182010-11-08 10:41:31 -05001214 if (dev->wq)
1215 destroy_workqueue(dev->wq);
Mike Lockwoodba83b012010-04-16 10:39:22 -04001216 kfree(dev);
1217 printk(KERN_ERR "mtp gadget driver failed to initialize\n");
1218 return ret;
1219}
1220
1221static struct android_usb_function mtp_function = {
1222 .name = "mtp",
1223 .bind_config = mtp_bind_config,
1224};
1225
1226static int __init init(void)
1227{
1228 printk(KERN_INFO "f_mtp init\n");
1229 android_register_function(&mtp_function);
1230 return 0;
1231}
1232module_init(init);