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