blob: 03ea4a7066caac04a332a9e827683a8f34c032c6 [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;
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
Mike Lockwood3e800b62010-11-16 17:14:32 -0500561 /* condition check at bottom to allow zero length packet write */
562 do {
Mike Lockwoodba83b012010-04-16 10:39:22 -0400563 if (dev->state != STATE_BUSY) {
564 DBG(cdev, "mtp_write dev->error\n");
565 r = -EIO;
566 break;
567 }
568
569 /* get an idle tx request to use */
570 req = 0;
571 ret = wait_event_interruptible(dev->write_wq,
572 ((req = req_get(dev, &dev->tx_idle))
573 || dev->state != STATE_BUSY));
574 if (!req) {
575 r = ret;
576 break;
577 }
578
579 if (count > BULK_BUFFER_SIZE)
580 xfer = BULK_BUFFER_SIZE;
581 else
582 xfer = count;
Mike Lockwood3e800b62010-11-16 17:14:32 -0500583 if (xfer && copy_from_user(req->buf, buf, xfer)) {
Mike Lockwoodba83b012010-04-16 10:39:22 -0400584 r = -EFAULT;
585 break;
586 }
587
588 req->length = xfer;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400589 ret = usb_ep_queue(dev->ep_in, req, GFP_KERNEL);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400590 if (ret < 0) {
591 DBG(cdev, "mtp_write: xfer error %d\n", ret);
592 r = -EIO;
593 break;
594 }
595
596 buf += xfer;
597 count -= xfer;
598
599 /* zero this so we don't try to free it on error exit */
600 req = 0;
Mike Lockwood3e800b62010-11-16 17:14:32 -0500601 } while (count > 0);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400602
603 if (req)
604 req_put(dev, &dev->tx_idle, req);
605
606 spin_lock_irq(&dev->lock);
607 if (dev->state == STATE_CANCELED)
608 r = -ECANCELED;
609 else if (dev->state != STATE_OFFLINE)
610 dev->state = STATE_READY;
611 spin_unlock_irq(&dev->lock);
612
613 DBG(cdev, "mtp_write returning %d\n", r);
614 return r;
615}
616
Mike Lockwood491d4182010-11-08 10:41:31 -0500617/* read from a local file and write to USB */
618static void send_file_work(struct work_struct *data) {
619 struct mtp_dev *dev = container_of(data, struct mtp_dev, send_file_work);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400620 struct usb_composite_dev *cdev = dev->cdev;
621 struct usb_request *req = 0;
Mike Lockwood491d4182010-11-08 10:41:31 -0500622 struct file *filp;
623 loff_t offset;
Mike Lockwood3e800b62010-11-16 17:14:32 -0500624 int64_t count;
Mike Lockwood76ac6552010-11-15 15:22:21 -0500625 int xfer, ret;
626 int r = 0;
Mike Lockwood3e800b62010-11-16 17:14:32 -0500627 int sendZLP = 0;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400628
Mike Lockwood491d4182010-11-08 10:41:31 -0500629 /* read our parameters */
630 smp_rmb();
631 filp = dev->xfer_file;
632 offset = dev->xfer_file_offset;
Mike Lockwood76ac6552010-11-15 15:22:21 -0500633 count = dev->xfer_file_length;
Mike Lockwood491d4182010-11-08 10:41:31 -0500634
Mike Lockwood3e800b62010-11-16 17:14:32 -0500635 DBG(cdev, "send_file_work(%lld %lld)\n", offset, count);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400636
Mike Lockwood3e800b62010-11-16 17:14:32 -0500637 /* we need to send a zero length packet to signal the end of transfer
638 * if the length is > 4 gig and the last packet is aligned to a
639 * packet boundary.
640 */
641 if (dev->xfer_file_length >= 0xFFFFFFFF
642 && (dev->xfer_file_length & (dev->ep_in->maxpacket - 1)) == 0) {
643 sendZLP = 1;
644 }
645
646 while (count > 0 || sendZLP) {
647 /* so we exit after sending ZLP */
648 if (count == 0)
649 sendZLP = 0;
650
Mike Lockwoodba83b012010-04-16 10:39:22 -0400651 /* get an idle tx request to use */
652 req = 0;
653 ret = wait_event_interruptible(dev->write_wq,
654 (req = req_get(dev, &dev->tx_idle))
655 || dev->state != STATE_BUSY);
656 if (!req) {
657 r = ret;
658 break;
659 }
660
661 if (count > BULK_BUFFER_SIZE)
662 xfer = BULK_BUFFER_SIZE;
663 else
664 xfer = count;
665 ret = vfs_read(filp, req->buf, xfer, &offset);
666 if (ret < 0) {
667 r = ret;
668 break;
669 }
670 xfer = ret;
671
672 req->length = xfer;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400673 ret = usb_ep_queue(dev->ep_in, req, GFP_KERNEL);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400674 if (ret < 0) {
Mike Lockwood491d4182010-11-08 10:41:31 -0500675 DBG(cdev, "send_file_work: xfer error %d\n", ret);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400676 dev->state = STATE_ERROR;
677 r = -EIO;
678 break;
679 }
680
681 count -= xfer;
682
683 /* zero this so we don't try to free it on error exit */
684 req = 0;
685 }
686
687 if (req)
688 req_put(dev, &dev->tx_idle, req);
689
Mike Lockwood491d4182010-11-08 10:41:31 -0500690 DBG(cdev, "send_file_work returning %d\n", r);
691 /* write the result */
692 dev->xfer_result = r;
693 smp_wmb();
Mike Lockwoodba83b012010-04-16 10:39:22 -0400694}
695
Mike Lockwood491d4182010-11-08 10:41:31 -0500696/* read from USB and write to a local file */
697static void receive_file_work(struct work_struct *data)
Mike Lockwoodba83b012010-04-16 10:39:22 -0400698{
Mike Lockwood491d4182010-11-08 10:41:31 -0500699 struct mtp_dev *dev = container_of(data, struct mtp_dev, receive_file_work);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400700 struct usb_composite_dev *cdev = dev->cdev;
701 struct usb_request *read_req = NULL, *write_req = NULL;
Mike Lockwood491d4182010-11-08 10:41:31 -0500702 struct file *filp;
703 loff_t offset;
Mike Lockwood3e800b62010-11-16 17:14:32 -0500704 int64_t count;
Mike Lockwood76ac6552010-11-15 15:22:21 -0500705 int ret, cur_buf = 0;
706 int r = 0;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400707
Mike Lockwood491d4182010-11-08 10:41:31 -0500708 /* read our parameters */
709 smp_rmb();
710 filp = dev->xfer_file;
711 offset = dev->xfer_file_offset;
Mike Lockwood76ac6552010-11-15 15:22:21 -0500712 count = dev->xfer_file_length;
Mike Lockwood491d4182010-11-08 10:41:31 -0500713
Mike Lockwood3e800b62010-11-16 17:14:32 -0500714 DBG(cdev, "receive_file_work(%lld)\n", count);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400715
716 while (count > 0 || write_req) {
717 if (count > 0) {
718 /* queue a request */
719 read_req = dev->rx_req[cur_buf];
720 cur_buf = (cur_buf + 1) % RX_REQ_MAX;
721
722 read_req->length = (count > BULK_BUFFER_SIZE
723 ? BULK_BUFFER_SIZE : count);
724 dev->rx_done = 0;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400725 ret = usb_ep_queue(dev->ep_out, read_req, GFP_KERNEL);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400726 if (ret < 0) {
727 r = -EIO;
728 dev->state = STATE_ERROR;
729 break;
730 }
Mike Lockwoodba83b012010-04-16 10:39:22 -0400731 }
732
733 if (write_req) {
734 DBG(cdev, "rx %p %d\n", write_req, write_req->actual);
735 ret = vfs_write(filp, write_req->buf, write_req->actual,
736 &offset);
737 DBG(cdev, "vfs_write %d\n", ret);
738 if (ret != write_req->actual) {
739 r = -EIO;
740 dev->state = STATE_ERROR;
741 break;
742 }
743 write_req = NULL;
744 }
745
746 if (read_req) {
747 /* wait for our last read to complete */
748 ret = wait_event_interruptible(dev->read_wq,
749 dev->rx_done || dev->state != STATE_BUSY);
750 if (ret < 0 || dev->state != STATE_BUSY) {
751 r = ret;
752 break;
753 }
Mike Lockwood3e800b62010-11-16 17:14:32 -0500754 /* if xfer_file_length is 0xFFFFFFFF, then we read until
755 * we get a zero length packet
756 */
757 if (count != 0xFFFFFFFF)
758 count -= read_req->actual;
759 if (read_req->actual < read_req->length) {
760 /* short packet is used to signal EOF for sizes > 4 gig */
761 DBG(cdev, "got short packet\n");
762 count = 0;
763 }
764
Mike Lockwoodba83b012010-04-16 10:39:22 -0400765 write_req = read_req;
766 read_req = NULL;
767 }
768 }
769
Mike Lockwood491d4182010-11-08 10:41:31 -0500770 DBG(cdev, "receive_file_work returning %d\n", r);
771 /* write the result */
772 dev->xfer_result = r;
773 smp_wmb();
Mike Lockwoodba83b012010-04-16 10:39:22 -0400774}
775
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400776static int mtp_send_event(struct mtp_dev *dev, struct mtp_event *event)
777{
778 struct usb_request *req;
779 int ret;
780 int length = event->length;
781
782 DBG(dev->cdev, "mtp_send_event(%d)\n", event->length);
783
784 if (length < 0 || length > INTR_BUFFER_SIZE)
785 return -EINVAL;
786
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400787 /* wait for a request to complete */
788 ret = wait_event_interruptible(dev->intr_wq, !dev->intr_busy || dev->state == STATE_OFFLINE);
789 if (ret < 0)
Mike Lockwood491d4182010-11-08 10:41:31 -0500790 return ret;
791 if (dev->state == STATE_OFFLINE)
792 return -ENODEV;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400793 req = dev->intr_req;
Mike Lockwood491d4182010-11-08 10:41:31 -0500794 if (copy_from_user(req->buf, (void __user *)event->data, length))
795 return -EFAULT;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400796 req->length = length;
797 dev->intr_busy = 1;
798 ret = usb_ep_queue(dev->ep_intr, req, GFP_KERNEL);
799 if (ret)
800 dev->intr_busy = 0;
801
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400802 return ret;
803}
804
Mike Lockwoodba83b012010-04-16 10:39:22 -0400805static long mtp_ioctl(struct file *fp, unsigned code, unsigned long value)
806{
807 struct mtp_dev *dev = fp->private_data;
808 struct file *filp = NULL;
809 int ret = -EINVAL;
810
Mike Lockwood491d4182010-11-08 10:41:31 -0500811 if (_lock(&dev->ioctl_excl))
812 return -EBUSY;
813
Mike Lockwoodba83b012010-04-16 10:39:22 -0400814 switch (code) {
815 case MTP_SEND_FILE:
816 case MTP_RECEIVE_FILE:
817 {
818 struct mtp_file_range mfr;
Mike Lockwood491d4182010-11-08 10:41:31 -0500819 struct work_struct *work;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400820
821 spin_lock_irq(&dev->lock);
822 if (dev->state == STATE_CANCELED) {
823 /* report cancelation to userspace */
824 dev->state = STATE_READY;
825 spin_unlock_irq(&dev->lock);
Mike Lockwood491d4182010-11-08 10:41:31 -0500826 ret = -ECANCELED;
827 goto out;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400828 }
829 if (dev->state == STATE_OFFLINE) {
830 spin_unlock_irq(&dev->lock);
Mike Lockwood491d4182010-11-08 10:41:31 -0500831 ret = -ENODEV;
832 goto out;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400833 }
834 dev->state = STATE_BUSY;
835 spin_unlock_irq(&dev->lock);
836
837 if (copy_from_user(&mfr, (void __user *)value, sizeof(mfr))) {
838 ret = -EFAULT;
839 goto fail;
840 }
Mike Lockwood491d4182010-11-08 10:41:31 -0500841 /* hold a reference to the file while we are working with it */
Mike Lockwoodba83b012010-04-16 10:39:22 -0400842 filp = fget(mfr.fd);
843 if (!filp) {
844 ret = -EBADF;
845 goto fail;
846 }
847
Mike Lockwood491d4182010-11-08 10:41:31 -0500848 /* write the parameters */
849 dev->xfer_file = filp;
850 dev->xfer_file_offset = mfr.offset;
851 dev->xfer_file_length = mfr.length;
852 smp_wmb();
Mike Lockwoodba83b012010-04-16 10:39:22 -0400853
854 if (code == MTP_SEND_FILE)
Mike Lockwood491d4182010-11-08 10:41:31 -0500855 work = &dev->send_file_work;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400856 else
Mike Lockwood491d4182010-11-08 10:41:31 -0500857 work = &dev->receive_file_work;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400858
Mike Lockwood491d4182010-11-08 10:41:31 -0500859 /* We do the file transfer on a work queue so it will run
860 * in kernel context, which is necessary for vfs_read and
861 * vfs_write to use our buffers in the kernel address space.
862 */
863 queue_work(dev->wq, work);
864 /* wait for operation to complete */
865 flush_workqueue(dev->wq);
866 fput(filp);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400867
Mike Lockwood491d4182010-11-08 10:41:31 -0500868 /* read the result */
869 smp_rmb();
870 ret = dev->xfer_result;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400871 break;
872 }
873 case MTP_SET_INTERFACE_MODE:
874 if (value == MTP_INTERFACE_MODE_MTP ||
875 value == MTP_INTERFACE_MODE_PTP) {
876 dev->interface_mode = value;
877 if (value == MTP_INTERFACE_MODE_PTP) {
878 dev->function.descriptors = fs_ptp_descs;
879 dev->function.hs_descriptors = hs_ptp_descs;
880 } else {
881 dev->function.descriptors = fs_mtp_descs;
882 dev->function.hs_descriptors = hs_mtp_descs;
883 }
884 ret = 0;
885 }
886 break;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400887 case MTP_SEND_EVENT:
888 {
889 struct mtp_event event;
890 /* return here so we don't change dev->state below,
891 * which would interfere with bulk transfer state.
892 */
893 if (copy_from_user(&event, (void __user *)value, sizeof(event)))
Mike Lockwood491d4182010-11-08 10:41:31 -0500894 ret = -EFAULT;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400895 else
Mike Lockwood491d4182010-11-08 10:41:31 -0500896 ret = mtp_send_event(dev, &event);
897 goto out;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400898 }
Mike Lockwoodba83b012010-04-16 10:39:22 -0400899 }
900
901fail:
Mike Lockwoodba83b012010-04-16 10:39:22 -0400902 spin_lock_irq(&dev->lock);
903 if (dev->state == STATE_CANCELED)
904 ret = -ECANCELED;
905 else if (dev->state != STATE_OFFLINE)
906 dev->state = STATE_READY;
907 spin_unlock_irq(&dev->lock);
Mike Lockwood491d4182010-11-08 10:41:31 -0500908out:
909 _unlock(&dev->ioctl_excl);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400910 DBG(dev->cdev, "ioctl returning %d\n", ret);
911 return ret;
912}
913
914static int mtp_open(struct inode *ip, struct file *fp)
915{
916 printk(KERN_INFO "mtp_open\n");
917 if (_lock(&_mtp_dev->open_excl))
918 return -EBUSY;
919
Mike Lockwoodba83b012010-04-16 10:39:22 -0400920 /* clear any error condition */
921 if (_mtp_dev->state != STATE_OFFLINE)
922 _mtp_dev->state = STATE_READY;
923
924 fp->private_data = _mtp_dev;
925 return 0;
926}
927
928static int mtp_release(struct inode *ip, struct file *fp)
929{
930 printk(KERN_INFO "mtp_release\n");
931
Mike Lockwoodba83b012010-04-16 10:39:22 -0400932 _unlock(&_mtp_dev->open_excl);
933 return 0;
934}
935
936/* file operations for /dev/mtp_usb */
937static const struct file_operations mtp_fops = {
938 .owner = THIS_MODULE,
939 .read = mtp_read,
940 .write = mtp_write,
941 .unlocked_ioctl = mtp_ioctl,
942 .open = mtp_open,
943 .release = mtp_release,
944};
945
946static struct miscdevice mtp_device = {
947 .minor = MISC_DYNAMIC_MINOR,
948 .name = shortname,
949 .fops = &mtp_fops,
950};
951
952static int
953mtp_function_bind(struct usb_configuration *c, struct usb_function *f)
954{
955 struct usb_composite_dev *cdev = c->cdev;
956 struct mtp_dev *dev = func_to_dev(f);
957 int id;
958 int ret;
959
960 dev->cdev = cdev;
961 DBG(cdev, "mtp_function_bind dev: %p\n", dev);
962
963 /* allocate interface ID(s) */
964 id = usb_interface_id(c, f);
965 if (id < 0)
966 return id;
967 mtp_interface_desc.bInterfaceNumber = id;
968
969 /* allocate endpoints */
970 ret = create_bulk_endpoints(dev, &mtp_fullspeed_in_desc,
971 &mtp_fullspeed_out_desc, &mtp_intr_desc);
972 if (ret)
973 return ret;
974
975 /* support high speed hardware */
976 if (gadget_is_dualspeed(c->cdev->gadget)) {
977 mtp_highspeed_in_desc.bEndpointAddress =
978 mtp_fullspeed_in_desc.bEndpointAddress;
979 mtp_highspeed_out_desc.bEndpointAddress =
980 mtp_fullspeed_out_desc.bEndpointAddress;
981 }
982
983 DBG(cdev, "%s speed %s: IN/%s, OUT/%s\n",
984 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
985 f->name, dev->ep_in->name, dev->ep_out->name);
986 return 0;
987}
988
989static void
990mtp_function_unbind(struct usb_configuration *c, struct usb_function *f)
991{
992 struct mtp_dev *dev = func_to_dev(f);
993 struct usb_request *req;
994 int i;
995
996 spin_lock_irq(&dev->lock);
997 while ((req = req_get(dev, &dev->tx_idle)))
998 mtp_request_free(req, dev->ep_in);
999 for (i = 0; i < RX_REQ_MAX; i++)
1000 mtp_request_free(dev->rx_req[i], dev->ep_out);
Mike Lockwood1de4d4d2010-07-06 19:27:52 -04001001 mtp_request_free(dev->intr_req, dev->ep_intr);
Mike Lockwoodba83b012010-04-16 10:39:22 -04001002 dev->state = STATE_OFFLINE;
1003 spin_unlock_irq(&dev->lock);
Mike Lockwood1de4d4d2010-07-06 19:27:52 -04001004 wake_up(&dev->intr_wq);
Mike Lockwoodba83b012010-04-16 10:39:22 -04001005
1006 misc_deregister(&mtp_device);
1007 kfree(_mtp_dev);
1008 _mtp_dev = NULL;
1009}
1010
1011static int mtp_function_setup(struct usb_function *f,
1012 const struct usb_ctrlrequest *ctrl)
1013{
1014 struct mtp_dev *dev = func_to_dev(f);
1015 struct usb_composite_dev *cdev = dev->cdev;
1016 int value = -EOPNOTSUPP;
1017 u16 w_index = le16_to_cpu(ctrl->wIndex);
1018 u16 w_value = le16_to_cpu(ctrl->wValue);
1019 u16 w_length = le16_to_cpu(ctrl->wLength);
1020 unsigned long flags;
1021
1022 /* do nothing if we are disabled */
1023 if (dev->function.disabled)
1024 return value;
1025
1026 VDBG(cdev, "mtp_function_setup "
1027 "%02x.%02x v%04x i%04x l%u\n",
1028 ctrl->bRequestType, ctrl->bRequest,
1029 w_value, w_index, w_length);
1030
1031 /* Handle MTP OS string */
1032 if (dev->interface_mode == MTP_INTERFACE_MODE_MTP
1033 && ctrl->bRequestType ==
1034 (USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE)
1035 && ctrl->bRequest == USB_REQ_GET_DESCRIPTOR
1036 && (w_value >> 8) == USB_DT_STRING
1037 && (w_value & 0xFF) == MTP_OS_STRING_ID) {
1038 value = (w_length < sizeof(mtp_os_string)
1039 ? w_length : sizeof(mtp_os_string));
1040 memcpy(cdev->req->buf, mtp_os_string, value);
1041 /* return here since composite.c will send for us */
1042 return value;
1043 }
1044 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_VENDOR) {
1045 /* Handle MTP OS descriptor */
1046 DBG(cdev, "vendor request: %d index: %d value: %d length: %d\n",
1047 ctrl->bRequest, w_index, w_value, w_length);
1048
1049 if (dev->interface_mode == MTP_INTERFACE_MODE_MTP
1050 && ctrl->bRequest == 1
1051 && (ctrl->bRequestType & USB_DIR_IN)
1052 && (w_index == 4 || w_index == 5)) {
1053 value = (w_length < sizeof(mtp_ext_config_desc) ?
1054 w_length : sizeof(mtp_ext_config_desc));
1055 memcpy(cdev->req->buf, &mtp_ext_config_desc, value);
1056 }
1057 }
1058 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_CLASS) {
1059 DBG(cdev, "class request: %d index: %d value: %d length: %d\n",
1060 ctrl->bRequest, w_index, w_value, w_length);
1061
1062 if (ctrl->bRequest == MTP_REQ_CANCEL && w_index == 0
1063 && w_value == 0) {
1064 DBG(cdev, "MTP_REQ_CANCEL\n");
1065
1066 spin_lock_irqsave(&dev->lock, flags);
1067 if (dev->state == STATE_BUSY) {
1068 dev->state = STATE_CANCELED;
1069 wake_up(&dev->read_wq);
1070 wake_up(&dev->write_wq);
1071 }
1072 spin_unlock_irqrestore(&dev->lock, flags);
1073
1074 /* We need to queue a request to read the remaining
1075 * bytes, but we don't actually need to look at
1076 * the contents.
1077 */
1078 value = w_length;
1079 } else if (ctrl->bRequest == MTP_REQ_GET_DEVICE_STATUS
1080 && w_index == 0 && w_value == 0) {
1081 struct mtp_device_status *status = cdev->req->buf;
1082 status->wLength =
1083 __constant_cpu_to_le16(sizeof(*status));
1084
1085 DBG(cdev, "MTP_REQ_GET_DEVICE_STATUS\n");
1086 spin_lock_irqsave(&dev->lock, flags);
1087 /* device status is "busy" until we report
1088 * the cancelation to userspace
1089 */
1090 if (dev->state == STATE_BUSY
1091 || dev->state == STATE_CANCELED)
1092 status->wCode =
1093 __cpu_to_le16(MTP_RESPONSE_DEVICE_BUSY);
1094 else
1095 status->wCode =
1096 __cpu_to_le16(MTP_RESPONSE_OK);
1097 spin_unlock_irqrestore(&dev->lock, flags);
1098 value = sizeof(*status);
1099 }
1100 }
1101
1102 /* respond with data transfer or status phase? */
1103 if (value >= 0) {
1104 int rc;
1105 cdev->req->zero = value < w_length;
1106 cdev->req->length = value;
1107 rc = usb_ep_queue(cdev->gadget->ep0, cdev->req, GFP_ATOMIC);
1108 if (rc < 0)
1109 ERROR(cdev, "%s setup response queue error\n", __func__);
1110 }
1111
1112 if (value == -EOPNOTSUPP)
1113 VDBG(cdev,
1114 "unknown class-specific control req "
1115 "%02x.%02x v%04x i%04x l%u\n",
1116 ctrl->bRequestType, ctrl->bRequest,
1117 w_value, w_index, w_length);
1118 return value;
1119}
1120
1121static int mtp_function_set_alt(struct usb_function *f,
1122 unsigned intf, unsigned alt)
1123{
1124 struct mtp_dev *dev = func_to_dev(f);
1125 struct usb_composite_dev *cdev = f->config->cdev;
1126 int ret;
1127
1128 DBG(cdev, "mtp_function_set_alt intf: %d alt: %d\n", intf, alt);
1129 ret = usb_ep_enable(dev->ep_in,
1130 ep_choose(cdev->gadget,
1131 &mtp_highspeed_in_desc,
1132 &mtp_fullspeed_in_desc));
1133 if (ret)
1134 return ret;
1135 ret = usb_ep_enable(dev->ep_out,
1136 ep_choose(cdev->gadget,
1137 &mtp_highspeed_out_desc,
1138 &mtp_fullspeed_out_desc));
1139 if (ret) {
1140 usb_ep_disable(dev->ep_in);
1141 return ret;
1142 }
1143 ret = usb_ep_enable(dev->ep_intr, &mtp_intr_desc);
1144 if (ret) {
1145 usb_ep_disable(dev->ep_out);
1146 usb_ep_disable(dev->ep_in);
1147 return ret;
1148 }
1149 dev->state = STATE_READY;
1150
1151 /* readers may be blocked waiting for us to go online */
1152 wake_up(&dev->read_wq);
1153 return 0;
1154}
1155
1156static void mtp_function_disable(struct usb_function *f)
1157{
1158 struct mtp_dev *dev = func_to_dev(f);
1159 struct usb_composite_dev *cdev = dev->cdev;
1160
1161 DBG(cdev, "mtp_function_disable\n");
1162 dev->state = STATE_OFFLINE;
1163 usb_ep_disable(dev->ep_in);
1164 usb_ep_disable(dev->ep_out);
1165 usb_ep_disable(dev->ep_intr);
1166
1167 /* readers may be blocked waiting for us to go online */
1168 wake_up(&dev->read_wq);
Mike Lockwood1de4d4d2010-07-06 19:27:52 -04001169 wake_up(&dev->intr_wq);
Mike Lockwoodba83b012010-04-16 10:39:22 -04001170
1171 VDBG(cdev, "%s disabled\n", dev->function.name);
1172}
1173
1174static int mtp_bind_config(struct usb_configuration *c)
1175{
1176 struct mtp_dev *dev;
1177 int ret;
1178
1179 printk(KERN_INFO "mtp_bind_config\n");
1180
1181 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1182 if (!dev)
1183 return -ENOMEM;
1184
1185 /* allocate a string ID for our interface */
1186 if (mtp_string_defs[INTERFACE_STRING_INDEX].id == 0) {
1187 ret = usb_string_id(c->cdev);
1188 if (ret < 0)
1189 return ret;
1190 mtp_string_defs[INTERFACE_STRING_INDEX].id = ret;
1191 mtp_interface_desc.iInterface = ret;
1192 }
1193
1194 spin_lock_init(&dev->lock);
Mike Lockwoodba83b012010-04-16 10:39:22 -04001195 init_waitqueue_head(&dev->read_wq);
1196 init_waitqueue_head(&dev->write_wq);
Mike Lockwood1de4d4d2010-07-06 19:27:52 -04001197 init_waitqueue_head(&dev->intr_wq);
Mike Lockwoodba83b012010-04-16 10:39:22 -04001198 atomic_set(&dev->open_excl, 0);
Mike Lockwood491d4182010-11-08 10:41:31 -05001199 atomic_set(&dev->ioctl_excl, 0);
Mike Lockwoodba83b012010-04-16 10:39:22 -04001200 INIT_LIST_HEAD(&dev->tx_idle);
Mike Lockwood491d4182010-11-08 10:41:31 -05001201
1202 dev->wq = create_singlethread_workqueue("f_mtp");
1203 if (!dev->wq)
1204 goto err1;
1205 INIT_WORK(&dev->send_file_work, send_file_work);
1206 INIT_WORK(&dev->receive_file_work, receive_file_work);
Mike Lockwoodba83b012010-04-16 10:39:22 -04001207
1208 dev->cdev = c->cdev;
1209 dev->function.name = "mtp";
1210 dev->function.strings = mtp_strings,
1211 dev->function.descriptors = fs_mtp_descs;
1212 dev->function.hs_descriptors = hs_mtp_descs;
1213 dev->function.bind = mtp_function_bind;
1214 dev->function.unbind = mtp_function_unbind;
1215 dev->function.setup = mtp_function_setup;
1216 dev->function.set_alt = mtp_function_set_alt;
1217 dev->function.disable = mtp_function_disable;
1218
1219 /* MTP mode by default */
1220 dev->interface_mode = MTP_INTERFACE_MODE_MTP;
1221
1222 /* _mtp_dev must be set before calling usb_gadget_register_driver */
1223 _mtp_dev = dev;
1224
1225 ret = misc_register(&mtp_device);
1226 if (ret)
1227 goto err1;
1228
1229 ret = usb_add_function(c, &dev->function);
1230 if (ret)
1231 goto err2;
1232
1233 return 0;
1234
1235err2:
1236 misc_deregister(&mtp_device);
1237err1:
Mike Lockwood491d4182010-11-08 10:41:31 -05001238 if (dev->wq)
1239 destroy_workqueue(dev->wq);
Mike Lockwoodba83b012010-04-16 10:39:22 -04001240 kfree(dev);
1241 printk(KERN_ERR "mtp gadget driver failed to initialize\n");
1242 return ret;
1243}
1244
1245static struct android_usb_function mtp_function = {
1246 .name = "mtp",
1247 .bind_config = mtp_bind_config,
1248};
1249
1250static int __init init(void)
1251{
1252 printk(KERN_INFO "f_mtp init\n");
1253 android_register_function(&mtp_function);
1254 return 0;
1255}
1256module_init(init);