blob: c892ec8154a71c0d610a770b7e7af14daec9a5c1 [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>
Mike Lockwoodba83b012010-04-16 10:39:22 -040037#include <linux/usb/f_mtp.h>
38
Benoit Gobyaab96812011-04-19 20:37:33 -070039#define MTP_BULK_BUFFER_SIZE 16384
Mike Lockwood1de4d4d2010-07-06 19:27:52 -040040#define INTR_BUFFER_SIZE 28
Mike Lockwoodba83b012010-04-16 10:39:22 -040041
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
Mike Lockwoodba83b012010-04-16 10:39:22 -040056/* ID for Microsoft MTP OS String */
57#define MTP_OS_STRING_ID 0xEE
58
59/* MTP class reqeusts */
60#define MTP_REQ_CANCEL 0x64
61#define MTP_REQ_GET_EXT_EVENT_DATA 0x65
62#define MTP_REQ_RESET 0x66
63#define MTP_REQ_GET_DEVICE_STATUS 0x67
64
65/* constants for device status */
66#define MTP_RESPONSE_OK 0x2001
67#define MTP_RESPONSE_DEVICE_BUSY 0x2019
68
Benoit Gobyaab96812011-04-19 20:37:33 -070069static const char mtp_shortname[] = "mtp_usb";
Mike Lockwoodba83b012010-04-16 10:39:22 -040070
71struct mtp_dev {
72 struct usb_function function;
73 struct usb_composite_dev *cdev;
74 spinlock_t lock;
75
Mike Lockwood1de4d4d2010-07-06 19:27:52 -040076 /* appear as MTP or PTP when enumerating */
Mike Lockwoodba83b012010-04-16 10:39:22 -040077 int interface_mode;
78
79 struct usb_ep *ep_in;
80 struct usb_ep *ep_out;
81 struct usb_ep *ep_intr;
82
83 int state;
84
Mike Lockwood1de4d4d2010-07-06 19:27:52 -040085 /* synchronize access to our device file */
Mike Lockwoodba83b012010-04-16 10:39:22 -040086 atomic_t open_excl;
Mike Lockwood491d4182010-11-08 10:41:31 -050087 /* to enforce only one ioctl at a time */
88 atomic_t ioctl_excl;
Mike Lockwoodba83b012010-04-16 10:39:22 -040089
90 struct list_head tx_idle;
91
92 wait_queue_head_t read_wq;
93 wait_queue_head_t write_wq;
94 struct usb_request *rx_req[RX_REQ_MAX];
Mike Lockwood1de4d4d2010-07-06 19:27:52 -040095 struct usb_request *intr_req;
Mike Lockwoodba83b012010-04-16 10:39:22 -040096 int rx_done;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -040097 /* true if interrupt endpoint is busy */
98 int intr_busy;
99
Mike Lockwood491d4182010-11-08 10:41:31 -0500100 /* for processing MTP_SEND_FILE and MTP_RECEIVE_FILE
101 * ioctls on a work queue
102 */
103 struct workqueue_struct *wq;
104 struct work_struct send_file_work;
105 struct work_struct receive_file_work;
106 struct file *xfer_file;
107 loff_t xfer_file_offset;
Mike Lockwood3e800b62010-11-16 17:14:32 -0500108 int64_t xfer_file_length;
Mike Lockwood491d4182010-11-08 10:41:31 -0500109 int xfer_result;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400110};
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,
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400167 .wMaxPacketSize = __constant_cpu_to_le16(INTR_BUFFER_SIZE),
Mike Lockwoodba83b012010-04-16 10:39:22 -0400168 .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
Benoit Gobyaab96812011-04-19 20:37:33 -0700275static inline struct mtp_dev *func_to_mtp(struct usb_function *f)
Mike Lockwoodba83b012010-04-16 10:39:22 -0400276{
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
Benoit Gobyaab96812011-04-19 20:37:33 -0700304static inline int mtp_lock(atomic_t *excl)
Mike Lockwoodba83b012010-04-16 10:39:22 -0400305{
306 if (atomic_inc_return(excl) == 1) {
307 return 0;
308 } else {
309 atomic_dec(excl);
310 return -1;
311 }
312}
313
Benoit Gobyaab96812011-04-19 20:37:33 -0700314static inline void mtp_unlock(atomic_t *excl)
Mike Lockwoodba83b012010-04-16 10:39:22 -0400315{
316 atomic_dec(excl);
317}
318
319/* add a request to the tail of a list */
Benoit Gobyaab96812011-04-19 20:37:33 -0700320static void mtp_req_put(struct mtp_dev *dev, struct list_head *head,
Mike Lockwoodba83b012010-04-16 10:39:22 -0400321 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 */
Benoit Gobyaab96812011-04-19 20:37:33 -0700331static struct usb_request
332*mtp_req_get(struct mtp_dev *dev, struct list_head *head)
Mike Lockwoodba83b012010-04-16 10:39:22 -0400333{
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
Benoit Gobyaab96812011-04-19 20:37:33 -0700355 mtp_req_put(dev, &dev->tx_idle, req);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400356
357 wake_up(&dev->write_wq);
358}
359
360static void mtp_complete_out(struct usb_ep *ep, struct usb_request *req)
361{
362 struct mtp_dev *dev = _mtp_dev;
363
364 dev->rx_done = 1;
365 if (req->status != 0)
366 dev->state = STATE_ERROR;
367
368 wake_up(&dev->read_wq);
369}
370
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400371static void mtp_complete_intr(struct usb_ep *ep, struct usb_request *req)
372{
373 struct mtp_dev *dev = _mtp_dev;
374
Mike Lockwood292b9632011-02-10 11:54:53 -0500375 DBG(dev->cdev, "mtp_complete_intr status: %d actual: %d\n",
376 req->status, req->actual);
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400377 dev->intr_busy = 0;
378 if (req->status != 0)
379 dev->state = STATE_ERROR;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400380}
381
Benoit Gobyaab96812011-04-19 20:37:33 -0700382static int mtp_create_bulk_endpoints(struct mtp_dev *dev,
Mike Lockwoodba83b012010-04-16 10:39:22 -0400383 struct usb_endpoint_descriptor *in_desc,
384 struct usb_endpoint_descriptor *out_desc,
385 struct usb_endpoint_descriptor *intr_desc)
386{
387 struct usb_composite_dev *cdev = dev->cdev;
388 struct usb_request *req;
389 struct usb_ep *ep;
390 int i;
391
392 DBG(cdev, "create_bulk_endpoints dev: %p\n", dev);
393
394 ep = usb_ep_autoconfig(cdev->gadget, in_desc);
395 if (!ep) {
396 DBG(cdev, "usb_ep_autoconfig for ep_in failed\n");
397 return -ENODEV;
398 }
399 DBG(cdev, "usb_ep_autoconfig for ep_in got %s\n", ep->name);
400 ep->driver_data = dev; /* claim the endpoint */
401 dev->ep_in = ep;
402
403 ep = usb_ep_autoconfig(cdev->gadget, out_desc);
404 if (!ep) {
405 DBG(cdev, "usb_ep_autoconfig for ep_out failed\n");
406 return -ENODEV;
407 }
408 DBG(cdev, "usb_ep_autoconfig for mtp ep_out got %s\n", ep->name);
409 ep->driver_data = dev; /* claim the endpoint */
410 dev->ep_out = ep;
411
412 ep = usb_ep_autoconfig(cdev->gadget, out_desc);
413 if (!ep) {
414 DBG(cdev, "usb_ep_autoconfig for ep_out failed\n");
415 return -ENODEV;
416 }
417 DBG(cdev, "usb_ep_autoconfig for mtp ep_out got %s\n", ep->name);
418 ep->driver_data = dev; /* claim the endpoint */
419 dev->ep_out = ep;
420
421 ep = usb_ep_autoconfig(cdev->gadget, intr_desc);
422 if (!ep) {
423 DBG(cdev, "usb_ep_autoconfig for ep_intr failed\n");
424 return -ENODEV;
425 }
426 DBG(cdev, "usb_ep_autoconfig for mtp ep_intr got %s\n", ep->name);
427 ep->driver_data = dev; /* claim the endpoint */
428 dev->ep_intr = ep;
429
430 /* now allocate requests for our endpoints */
431 for (i = 0; i < TX_REQ_MAX; i++) {
Benoit Gobyaab96812011-04-19 20:37:33 -0700432 req = mtp_request_new(dev->ep_in, MTP_BULK_BUFFER_SIZE);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400433 if (!req)
434 goto fail;
435 req->complete = mtp_complete_in;
Benoit Gobyaab96812011-04-19 20:37:33 -0700436 mtp_req_put(dev, &dev->tx_idle, req);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400437 }
438 for (i = 0; i < RX_REQ_MAX; i++) {
Benoit Gobyaab96812011-04-19 20:37:33 -0700439 req = mtp_request_new(dev->ep_out, MTP_BULK_BUFFER_SIZE);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400440 if (!req)
441 goto fail;
442 req->complete = mtp_complete_out;
443 dev->rx_req[i] = req;
444 }
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400445 req = mtp_request_new(dev->ep_intr, INTR_BUFFER_SIZE);
446 if (!req)
447 goto fail;
448 req->complete = mtp_complete_intr;
449 dev->intr_req = req;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400450
451 return 0;
452
453fail:
454 printk(KERN_ERR "mtp_bind() could not allocate requests\n");
455 return -1;
456}
457
458static ssize_t mtp_read(struct file *fp, char __user *buf,
459 size_t count, loff_t *pos)
460{
461 struct mtp_dev *dev = fp->private_data;
462 struct usb_composite_dev *cdev = dev->cdev;
463 struct usb_request *req;
464 int r = count, xfer;
465 int ret = 0;
466
467 DBG(cdev, "mtp_read(%d)\n", count);
468
Benoit Gobyaab96812011-04-19 20:37:33 -0700469 if (count > MTP_BULK_BUFFER_SIZE)
Mike Lockwoodba83b012010-04-16 10:39:22 -0400470 return -EINVAL;
471
472 /* we will block until we're online */
473 DBG(cdev, "mtp_read: waiting for online state\n");
474 ret = wait_event_interruptible(dev->read_wq,
475 dev->state != STATE_OFFLINE);
476 if (ret < 0) {
477 r = ret;
478 goto done;
479 }
480 spin_lock_irq(&dev->lock);
481 if (dev->state == STATE_CANCELED) {
482 /* report cancelation to userspace */
483 dev->state = STATE_READY;
484 spin_unlock_irq(&dev->lock);
485 return -ECANCELED;
486 }
487 dev->state = STATE_BUSY;
488 spin_unlock_irq(&dev->lock);
489
490requeue_req:
491 /* queue a request */
492 req = dev->rx_req[0];
493 req->length = count;
494 dev->rx_done = 0;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400495 ret = usb_ep_queue(dev->ep_out, req, GFP_KERNEL);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400496 if (ret < 0) {
497 r = -EIO;
498 goto done;
499 } else {
500 DBG(cdev, "rx %p queue\n", req);
501 }
502
503 /* wait for a request to complete */
504 ret = wait_event_interruptible(dev->read_wq, dev->rx_done);
505 if (ret < 0) {
506 r = ret;
Mike Lockwood43f3dc82011-02-19 15:33:17 -0500507 usb_ep_dequeue(dev->ep_out, req);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400508 goto done;
509 }
510 if (dev->state == STATE_BUSY) {
511 /* If we got a 0-len packet, throw it back and try again. */
512 if (req->actual == 0)
513 goto requeue_req;
514
515 DBG(cdev, "rx %p %d\n", req, req->actual);
516 xfer = (req->actual < count) ? req->actual : count;
517 r = xfer;
518 if (copy_to_user(buf, req->buf, xfer))
519 r = -EFAULT;
520 } else
521 r = -EIO;
522
523done:
524 spin_lock_irq(&dev->lock);
525 if (dev->state == STATE_CANCELED)
526 r = -ECANCELED;
527 else if (dev->state != STATE_OFFLINE)
528 dev->state = STATE_READY;
529 spin_unlock_irq(&dev->lock);
530
531 DBG(cdev, "mtp_read returning %d\n", r);
532 return r;
533}
534
535static ssize_t mtp_write(struct file *fp, const char __user *buf,
536 size_t count, loff_t *pos)
537{
538 struct mtp_dev *dev = fp->private_data;
539 struct usb_composite_dev *cdev = dev->cdev;
540 struct usb_request *req = 0;
541 int r = count, xfer;
Mike Lockwood16c08c22010-11-17 11:16:35 -0500542 int sendZLP = 0;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400543 int ret;
544
545 DBG(cdev, "mtp_write(%d)\n", count);
546
547 spin_lock_irq(&dev->lock);
548 if (dev->state == STATE_CANCELED) {
549 /* report cancelation to userspace */
550 dev->state = STATE_READY;
551 spin_unlock_irq(&dev->lock);
552 return -ECANCELED;
553 }
554 if (dev->state == STATE_OFFLINE) {
555 spin_unlock_irq(&dev->lock);
556 return -ENODEV;
557 }
558 dev->state = STATE_BUSY;
559 spin_unlock_irq(&dev->lock);
560
Mike Lockwood16c08c22010-11-17 11:16:35 -0500561 /* we need to send a zero length packet to signal the end of transfer
562 * if the transfer size is aligned to a packet boundary.
563 */
564 if ((count & (dev->ep_in->maxpacket - 1)) == 0) {
565 sendZLP = 1;
566 }
567
568 while (count > 0 || sendZLP) {
569 /* so we exit after sending ZLP */
570 if (count == 0)
571 sendZLP = 0;
572
Mike Lockwoodba83b012010-04-16 10:39:22 -0400573 if (dev->state != STATE_BUSY) {
574 DBG(cdev, "mtp_write dev->error\n");
575 r = -EIO;
576 break;
577 }
578
579 /* get an idle tx request to use */
580 req = 0;
581 ret = wait_event_interruptible(dev->write_wq,
Benoit Gobyaab96812011-04-19 20:37:33 -0700582 ((req = mtp_req_get(dev, &dev->tx_idle))
Mike Lockwoodba83b012010-04-16 10:39:22 -0400583 || dev->state != STATE_BUSY));
584 if (!req) {
585 r = ret;
586 break;
587 }
588
Benoit Gobyaab96812011-04-19 20:37:33 -0700589 if (count > MTP_BULK_BUFFER_SIZE)
590 xfer = MTP_BULK_BUFFER_SIZE;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400591 else
592 xfer = count;
Mike Lockwood3e800b62010-11-16 17:14:32 -0500593 if (xfer && copy_from_user(req->buf, buf, xfer)) {
Mike Lockwoodba83b012010-04-16 10:39:22 -0400594 r = -EFAULT;
595 break;
596 }
597
598 req->length = xfer;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400599 ret = usb_ep_queue(dev->ep_in, req, GFP_KERNEL);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400600 if (ret < 0) {
601 DBG(cdev, "mtp_write: xfer error %d\n", ret);
602 r = -EIO;
603 break;
604 }
605
606 buf += xfer;
607 count -= xfer;
608
609 /* zero this so we don't try to free it on error exit */
610 req = 0;
Mike Lockwood16c08c22010-11-17 11:16:35 -0500611 }
Mike Lockwoodba83b012010-04-16 10:39:22 -0400612
613 if (req)
Benoit Gobyaab96812011-04-19 20:37:33 -0700614 mtp_req_put(dev, &dev->tx_idle, req);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400615
616 spin_lock_irq(&dev->lock);
617 if (dev->state == STATE_CANCELED)
618 r = -ECANCELED;
619 else if (dev->state != STATE_OFFLINE)
620 dev->state = STATE_READY;
621 spin_unlock_irq(&dev->lock);
622
623 DBG(cdev, "mtp_write returning %d\n", r);
624 return r;
625}
626
Mike Lockwood491d4182010-11-08 10:41:31 -0500627/* read from a local file and write to USB */
628static void send_file_work(struct work_struct *data) {
629 struct mtp_dev *dev = container_of(data, struct mtp_dev, send_file_work);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400630 struct usb_composite_dev *cdev = dev->cdev;
631 struct usb_request *req = 0;
Mike Lockwood491d4182010-11-08 10:41:31 -0500632 struct file *filp;
633 loff_t offset;
Mike Lockwood3e800b62010-11-16 17:14:32 -0500634 int64_t count;
Mike Lockwood76ac6552010-11-15 15:22:21 -0500635 int xfer, ret;
636 int r = 0;
Mike Lockwood3e800b62010-11-16 17:14:32 -0500637 int sendZLP = 0;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400638
Mike Lockwood491d4182010-11-08 10:41:31 -0500639 /* read our parameters */
640 smp_rmb();
641 filp = dev->xfer_file;
642 offset = dev->xfer_file_offset;
Mike Lockwood76ac6552010-11-15 15:22:21 -0500643 count = dev->xfer_file_length;
Mike Lockwood491d4182010-11-08 10:41:31 -0500644
Mike Lockwood3e800b62010-11-16 17:14:32 -0500645 DBG(cdev, "send_file_work(%lld %lld)\n", offset, count);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400646
Mike Lockwood3e800b62010-11-16 17:14:32 -0500647 /* we need to send a zero length packet to signal the end of transfer
Mike Lockwood16c08c22010-11-17 11:16:35 -0500648 * if the transfer size is aligned to a packet boundary.
Mike Lockwood3e800b62010-11-16 17:14:32 -0500649 */
Mike Lockwood16c08c22010-11-17 11:16:35 -0500650 if ((dev->xfer_file_length & (dev->ep_in->maxpacket - 1)) == 0) {
Mike Lockwood3e800b62010-11-16 17:14:32 -0500651 sendZLP = 1;
652 }
653
654 while (count > 0 || sendZLP) {
655 /* so we exit after sending ZLP */
656 if (count == 0)
657 sendZLP = 0;
658
Mike Lockwoodba83b012010-04-16 10:39:22 -0400659 /* get an idle tx request to use */
660 req = 0;
661 ret = wait_event_interruptible(dev->write_wq,
Benoit Gobyaab96812011-04-19 20:37:33 -0700662 (req = mtp_req_get(dev, &dev->tx_idle))
Mike Lockwoodba83b012010-04-16 10:39:22 -0400663 || dev->state != STATE_BUSY);
Mike Lockwood090cbc42011-02-07 11:51:07 -0500664 if (dev->state == STATE_CANCELED) {
665 r = -ECANCELED;
666 break;
667 }
Mike Lockwoodba83b012010-04-16 10:39:22 -0400668 if (!req) {
669 r = ret;
670 break;
671 }
672
Benoit Gobyaab96812011-04-19 20:37:33 -0700673 if (count > MTP_BULK_BUFFER_SIZE)
674 xfer = MTP_BULK_BUFFER_SIZE;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400675 else
676 xfer = count;
677 ret = vfs_read(filp, req->buf, xfer, &offset);
678 if (ret < 0) {
679 r = ret;
680 break;
681 }
682 xfer = ret;
683
684 req->length = xfer;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400685 ret = usb_ep_queue(dev->ep_in, req, GFP_KERNEL);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400686 if (ret < 0) {
Mike Lockwood491d4182010-11-08 10:41:31 -0500687 DBG(cdev, "send_file_work: xfer error %d\n", ret);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400688 dev->state = STATE_ERROR;
689 r = -EIO;
690 break;
691 }
692
693 count -= xfer;
694
695 /* zero this so we don't try to free it on error exit */
696 req = 0;
697 }
698
699 if (req)
Benoit Gobyaab96812011-04-19 20:37:33 -0700700 mtp_req_put(dev, &dev->tx_idle, req);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400701
Mike Lockwood491d4182010-11-08 10:41:31 -0500702 DBG(cdev, "send_file_work returning %d\n", r);
703 /* write the result */
704 dev->xfer_result = r;
705 smp_wmb();
Mike Lockwoodba83b012010-04-16 10:39:22 -0400706}
707
Mike Lockwood491d4182010-11-08 10:41:31 -0500708/* read from USB and write to a local file */
709static void receive_file_work(struct work_struct *data)
Mike Lockwoodba83b012010-04-16 10:39:22 -0400710{
Mike Lockwood491d4182010-11-08 10:41:31 -0500711 struct mtp_dev *dev = container_of(data, struct mtp_dev, receive_file_work);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400712 struct usb_composite_dev *cdev = dev->cdev;
713 struct usb_request *read_req = NULL, *write_req = NULL;
Mike Lockwood491d4182010-11-08 10:41:31 -0500714 struct file *filp;
715 loff_t offset;
Mike Lockwood3e800b62010-11-16 17:14:32 -0500716 int64_t count;
Mike Lockwood76ac6552010-11-15 15:22:21 -0500717 int ret, cur_buf = 0;
718 int r = 0;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400719
Mike Lockwood491d4182010-11-08 10:41:31 -0500720 /* read our parameters */
721 smp_rmb();
722 filp = dev->xfer_file;
723 offset = dev->xfer_file_offset;
Mike Lockwood76ac6552010-11-15 15:22:21 -0500724 count = dev->xfer_file_length;
Mike Lockwood491d4182010-11-08 10:41:31 -0500725
Mike Lockwood3e800b62010-11-16 17:14:32 -0500726 DBG(cdev, "receive_file_work(%lld)\n", count);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400727
728 while (count > 0 || write_req) {
729 if (count > 0) {
730 /* queue a request */
731 read_req = dev->rx_req[cur_buf];
732 cur_buf = (cur_buf + 1) % RX_REQ_MAX;
733
Benoit Gobyaab96812011-04-19 20:37:33 -0700734 read_req->length = (count > MTP_BULK_BUFFER_SIZE
735 ? MTP_BULK_BUFFER_SIZE : count);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400736 dev->rx_done = 0;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400737 ret = usb_ep_queue(dev->ep_out, read_req, GFP_KERNEL);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400738 if (ret < 0) {
739 r = -EIO;
740 dev->state = STATE_ERROR;
741 break;
742 }
Mike Lockwoodba83b012010-04-16 10:39:22 -0400743 }
744
745 if (write_req) {
746 DBG(cdev, "rx %p %d\n", write_req, write_req->actual);
747 ret = vfs_write(filp, write_req->buf, write_req->actual,
748 &offset);
749 DBG(cdev, "vfs_write %d\n", ret);
750 if (ret != write_req->actual) {
751 r = -EIO;
752 dev->state = STATE_ERROR;
753 break;
754 }
755 write_req = NULL;
756 }
757
758 if (read_req) {
759 /* wait for our last read to complete */
760 ret = wait_event_interruptible(dev->read_wq,
761 dev->rx_done || dev->state != STATE_BUSY);
Mike Lockwood50fe49a2011-01-13 16:19:57 -0500762 if (dev->state == STATE_CANCELED) {
763 r = -ECANCELED;
764 if (!dev->rx_done)
765 usb_ep_dequeue(dev->ep_out, read_req);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400766 break;
767 }
Mike Lockwood3e800b62010-11-16 17:14:32 -0500768 /* if xfer_file_length is 0xFFFFFFFF, then we read until
769 * we get a zero length packet
770 */
771 if (count != 0xFFFFFFFF)
772 count -= read_req->actual;
773 if (read_req->actual < read_req->length) {
774 /* short packet is used to signal EOF for sizes > 4 gig */
775 DBG(cdev, "got short packet\n");
776 count = 0;
777 }
778
Mike Lockwoodba83b012010-04-16 10:39:22 -0400779 write_req = read_req;
780 read_req = NULL;
781 }
782 }
783
Mike Lockwood491d4182010-11-08 10:41:31 -0500784 DBG(cdev, "receive_file_work returning %d\n", r);
785 /* write the result */
786 dev->xfer_result = r;
787 smp_wmb();
Mike Lockwoodba83b012010-04-16 10:39:22 -0400788}
789
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400790static int mtp_send_event(struct mtp_dev *dev, struct mtp_event *event)
791{
792 struct usb_request *req;
793 int ret;
794 int length = event->length;
795
796 DBG(dev->cdev, "mtp_send_event(%d)\n", event->length);
797
798 if (length < 0 || length > INTR_BUFFER_SIZE)
799 return -EINVAL;
Mike Lockwood491d4182010-11-08 10:41:31 -0500800 if (dev->state == STATE_OFFLINE)
801 return -ENODEV;
Mike Lockwood292b9632011-02-10 11:54:53 -0500802 /* unfortunately an interrupt request might hang indefinitely if the host
803 * is not listening on the interrupt endpoint, so instead of waiting,
804 * we just fail if the endpoint is busy.
805 */
806 if (dev->intr_busy)
807 return -EBUSY;
808
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400809 req = dev->intr_req;
Mike Lockwood491d4182010-11-08 10:41:31 -0500810 if (copy_from_user(req->buf, (void __user *)event->data, length))
811 return -EFAULT;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400812 req->length = length;
813 dev->intr_busy = 1;
814 ret = usb_ep_queue(dev->ep_intr, req, GFP_KERNEL);
815 if (ret)
816 dev->intr_busy = 0;
817
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400818 return ret;
819}
820
Mike Lockwoodba83b012010-04-16 10:39:22 -0400821static long mtp_ioctl(struct file *fp, unsigned code, unsigned long value)
822{
823 struct mtp_dev *dev = fp->private_data;
824 struct file *filp = NULL;
825 int ret = -EINVAL;
826
Benoit Gobyaab96812011-04-19 20:37:33 -0700827 if (mtp_lock(&dev->ioctl_excl))
Mike Lockwood491d4182010-11-08 10:41:31 -0500828 return -EBUSY;
829
Mike Lockwoodba83b012010-04-16 10:39:22 -0400830 switch (code) {
831 case MTP_SEND_FILE:
832 case MTP_RECEIVE_FILE:
833 {
834 struct mtp_file_range mfr;
Mike Lockwood491d4182010-11-08 10:41:31 -0500835 struct work_struct *work;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400836
837 spin_lock_irq(&dev->lock);
838 if (dev->state == STATE_CANCELED) {
839 /* report cancelation to userspace */
840 dev->state = STATE_READY;
841 spin_unlock_irq(&dev->lock);
Mike Lockwood491d4182010-11-08 10:41:31 -0500842 ret = -ECANCELED;
843 goto out;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400844 }
845 if (dev->state == STATE_OFFLINE) {
846 spin_unlock_irq(&dev->lock);
Mike Lockwood491d4182010-11-08 10:41:31 -0500847 ret = -ENODEV;
848 goto out;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400849 }
850 dev->state = STATE_BUSY;
851 spin_unlock_irq(&dev->lock);
852
853 if (copy_from_user(&mfr, (void __user *)value, sizeof(mfr))) {
854 ret = -EFAULT;
855 goto fail;
856 }
Mike Lockwood491d4182010-11-08 10:41:31 -0500857 /* hold a reference to the file while we are working with it */
Mike Lockwoodba83b012010-04-16 10:39:22 -0400858 filp = fget(mfr.fd);
859 if (!filp) {
860 ret = -EBADF;
861 goto fail;
862 }
863
Mike Lockwood491d4182010-11-08 10:41:31 -0500864 /* write the parameters */
865 dev->xfer_file = filp;
866 dev->xfer_file_offset = mfr.offset;
867 dev->xfer_file_length = mfr.length;
868 smp_wmb();
Mike Lockwoodba83b012010-04-16 10:39:22 -0400869
870 if (code == MTP_SEND_FILE)
Mike Lockwood491d4182010-11-08 10:41:31 -0500871 work = &dev->send_file_work;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400872 else
Mike Lockwood491d4182010-11-08 10:41:31 -0500873 work = &dev->receive_file_work;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400874
Mike Lockwood491d4182010-11-08 10:41:31 -0500875 /* We do the file transfer on a work queue so it will run
876 * in kernel context, which is necessary for vfs_read and
877 * vfs_write to use our buffers in the kernel address space.
878 */
879 queue_work(dev->wq, work);
880 /* wait for operation to complete */
881 flush_workqueue(dev->wq);
882 fput(filp);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400883
Mike Lockwood491d4182010-11-08 10:41:31 -0500884 /* read the result */
885 smp_rmb();
886 ret = dev->xfer_result;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400887 break;
888 }
889 case MTP_SET_INTERFACE_MODE:
890 if (value == MTP_INTERFACE_MODE_MTP ||
891 value == MTP_INTERFACE_MODE_PTP) {
892 dev->interface_mode = value;
893 if (value == MTP_INTERFACE_MODE_PTP) {
894 dev->function.descriptors = fs_ptp_descs;
895 dev->function.hs_descriptors = hs_ptp_descs;
896 } else {
897 dev->function.descriptors = fs_mtp_descs;
898 dev->function.hs_descriptors = hs_mtp_descs;
899 }
900 ret = 0;
901 }
902 break;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400903 case MTP_SEND_EVENT:
904 {
905 struct mtp_event event;
906 /* return here so we don't change dev->state below,
907 * which would interfere with bulk transfer state.
908 */
909 if (copy_from_user(&event, (void __user *)value, sizeof(event)))
Mike Lockwood491d4182010-11-08 10:41:31 -0500910 ret = -EFAULT;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400911 else
Mike Lockwood491d4182010-11-08 10:41:31 -0500912 ret = mtp_send_event(dev, &event);
913 goto out;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400914 }
Mike Lockwoodba83b012010-04-16 10:39:22 -0400915 }
916
917fail:
Mike Lockwoodba83b012010-04-16 10:39:22 -0400918 spin_lock_irq(&dev->lock);
919 if (dev->state == STATE_CANCELED)
920 ret = -ECANCELED;
921 else if (dev->state != STATE_OFFLINE)
922 dev->state = STATE_READY;
923 spin_unlock_irq(&dev->lock);
Mike Lockwood491d4182010-11-08 10:41:31 -0500924out:
Benoit Gobyaab96812011-04-19 20:37:33 -0700925 mtp_unlock(&dev->ioctl_excl);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400926 DBG(dev->cdev, "ioctl returning %d\n", ret);
927 return ret;
928}
929
930static int mtp_open(struct inode *ip, struct file *fp)
931{
932 printk(KERN_INFO "mtp_open\n");
Benoit Gobyaab96812011-04-19 20:37:33 -0700933 if (mtp_lock(&_mtp_dev->open_excl))
Mike Lockwoodba83b012010-04-16 10:39:22 -0400934 return -EBUSY;
935
Mike Lockwoodba83b012010-04-16 10:39:22 -0400936 /* clear any error condition */
937 if (_mtp_dev->state != STATE_OFFLINE)
938 _mtp_dev->state = STATE_READY;
939
940 fp->private_data = _mtp_dev;
941 return 0;
942}
943
944static int mtp_release(struct inode *ip, struct file *fp)
945{
946 printk(KERN_INFO "mtp_release\n");
947
Benoit Gobyaab96812011-04-19 20:37:33 -0700948 mtp_unlock(&_mtp_dev->open_excl);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400949 return 0;
950}
951
952/* file operations for /dev/mtp_usb */
953static const struct file_operations mtp_fops = {
954 .owner = THIS_MODULE,
955 .read = mtp_read,
956 .write = mtp_write,
957 .unlocked_ioctl = mtp_ioctl,
958 .open = mtp_open,
959 .release = mtp_release,
960};
961
962static struct miscdevice mtp_device = {
963 .minor = MISC_DYNAMIC_MINOR,
Benoit Gobyaab96812011-04-19 20:37:33 -0700964 .name = mtp_shortname,
Mike Lockwoodba83b012010-04-16 10:39:22 -0400965 .fops = &mtp_fops,
966};
967
Benoit Gobyaab96812011-04-19 20:37:33 -0700968static int mtp_ctrlrequest(struct usb_composite_dev *cdev,
969 const struct usb_ctrlrequest *ctrl)
970{
971 struct mtp_dev *dev = _mtp_dev;
972 int value = -EOPNOTSUPP;
973 u16 w_index = le16_to_cpu(ctrl->wIndex);
974 u16 w_value = le16_to_cpu(ctrl->wValue);
975 u16 w_length = le16_to_cpu(ctrl->wLength);
976
977 VDBG(cdev, "mtp_ctrlrequest "
978 "%02x.%02x v%04x i%04x l%u\n",
979 ctrl->bRequestType, ctrl->bRequest,
980 w_value, w_index, w_length);
981
982 /* Handle MTP OS string */
983 if (dev->interface_mode == MTP_INTERFACE_MODE_MTP
984 && ctrl->bRequestType ==
985 (USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE)
986 && ctrl->bRequest == USB_REQ_GET_DESCRIPTOR
987 && (w_value >> 8) == USB_DT_STRING
988 && (w_value & 0xFF) == MTP_OS_STRING_ID) {
989 value = (w_length < sizeof(mtp_os_string)
990 ? w_length : sizeof(mtp_os_string));
991 memcpy(cdev->req->buf, mtp_os_string, value);
992 }
993 /* respond with data transfer or status phase? */
994 if (value >= 0) {
995 int rc;
996 cdev->req->zero = value < w_length;
997 cdev->req->length = value;
998 rc = usb_ep_queue(cdev->gadget->ep0, cdev->req, GFP_ATOMIC);
999 if (rc < 0)
1000 ERROR(cdev, "%s setup response queue error\n", __func__);
1001 }
1002 return value;
1003}
1004
Mike Lockwoodba83b012010-04-16 10:39:22 -04001005static int
1006mtp_function_bind(struct usb_configuration *c, struct usb_function *f)
1007{
1008 struct usb_composite_dev *cdev = c->cdev;
Benoit Gobyaab96812011-04-19 20:37:33 -07001009 struct mtp_dev *dev = func_to_mtp(f);
Mike Lockwoodba83b012010-04-16 10:39:22 -04001010 int id;
1011 int ret;
1012
1013 dev->cdev = cdev;
1014 DBG(cdev, "mtp_function_bind dev: %p\n", dev);
1015
1016 /* allocate interface ID(s) */
1017 id = usb_interface_id(c, f);
1018 if (id < 0)
1019 return id;
1020 mtp_interface_desc.bInterfaceNumber = id;
1021
1022 /* allocate endpoints */
Benoit Gobyaab96812011-04-19 20:37:33 -07001023 ret = mtp_create_bulk_endpoints(dev, &mtp_fullspeed_in_desc,
Mike Lockwoodba83b012010-04-16 10:39:22 -04001024 &mtp_fullspeed_out_desc, &mtp_intr_desc);
1025 if (ret)
1026 return ret;
1027
1028 /* support high speed hardware */
1029 if (gadget_is_dualspeed(c->cdev->gadget)) {
1030 mtp_highspeed_in_desc.bEndpointAddress =
1031 mtp_fullspeed_in_desc.bEndpointAddress;
1032 mtp_highspeed_out_desc.bEndpointAddress =
1033 mtp_fullspeed_out_desc.bEndpointAddress;
1034 }
1035
1036 DBG(cdev, "%s speed %s: IN/%s, OUT/%s\n",
1037 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
1038 f->name, dev->ep_in->name, dev->ep_out->name);
1039 return 0;
1040}
1041
1042static void
1043mtp_function_unbind(struct usb_configuration *c, struct usb_function *f)
1044{
Benoit Gobyaab96812011-04-19 20:37:33 -07001045 struct mtp_dev *dev = func_to_mtp(f);
Mike Lockwoodba83b012010-04-16 10:39:22 -04001046 struct usb_request *req;
1047 int i;
1048
Benoit Gobyaab96812011-04-19 20:37:33 -07001049 while ((req = mtp_req_get(dev, &dev->tx_idle)))
Mike Lockwoodba83b012010-04-16 10:39:22 -04001050 mtp_request_free(req, dev->ep_in);
1051 for (i = 0; i < RX_REQ_MAX; i++)
1052 mtp_request_free(dev->rx_req[i], dev->ep_out);
Mike Lockwood1de4d4d2010-07-06 19:27:52 -04001053 mtp_request_free(dev->intr_req, dev->ep_intr);
Mike Lockwoodba83b012010-04-16 10:39:22 -04001054 dev->state = STATE_OFFLINE;
Mike Lockwoodba83b012010-04-16 10:39:22 -04001055}
1056
1057static int mtp_function_setup(struct usb_function *f,
1058 const struct usb_ctrlrequest *ctrl)
1059{
Benoit Gobyaab96812011-04-19 20:37:33 -07001060 struct mtp_dev *dev = func_to_mtp(f);
Mike Lockwoodba83b012010-04-16 10:39:22 -04001061 struct usb_composite_dev *cdev = dev->cdev;
1062 int value = -EOPNOTSUPP;
1063 u16 w_index = le16_to_cpu(ctrl->wIndex);
1064 u16 w_value = le16_to_cpu(ctrl->wValue);
1065 u16 w_length = le16_to_cpu(ctrl->wLength);
1066 unsigned long flags;
1067
Mike Lockwoodba83b012010-04-16 10:39:22 -04001068 VDBG(cdev, "mtp_function_setup "
1069 "%02x.%02x v%04x i%04x l%u\n",
1070 ctrl->bRequestType, ctrl->bRequest,
1071 w_value, w_index, w_length);
1072
Mike Lockwoodba83b012010-04-16 10:39:22 -04001073 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_VENDOR) {
1074 /* Handle MTP OS descriptor */
1075 DBG(cdev, "vendor request: %d index: %d value: %d length: %d\n",
1076 ctrl->bRequest, w_index, w_value, w_length);
1077
1078 if (dev->interface_mode == MTP_INTERFACE_MODE_MTP
1079 && ctrl->bRequest == 1
1080 && (ctrl->bRequestType & USB_DIR_IN)
1081 && (w_index == 4 || w_index == 5)) {
1082 value = (w_length < sizeof(mtp_ext_config_desc) ?
1083 w_length : sizeof(mtp_ext_config_desc));
1084 memcpy(cdev->req->buf, &mtp_ext_config_desc, value);
1085 }
1086 }
1087 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_CLASS) {
1088 DBG(cdev, "class request: %d index: %d value: %d length: %d\n",
1089 ctrl->bRequest, w_index, w_value, w_length);
1090
1091 if (ctrl->bRequest == MTP_REQ_CANCEL && w_index == 0
1092 && w_value == 0) {
1093 DBG(cdev, "MTP_REQ_CANCEL\n");
1094
1095 spin_lock_irqsave(&dev->lock, flags);
1096 if (dev->state == STATE_BUSY) {
1097 dev->state = STATE_CANCELED;
1098 wake_up(&dev->read_wq);
1099 wake_up(&dev->write_wq);
1100 }
1101 spin_unlock_irqrestore(&dev->lock, flags);
1102
1103 /* We need to queue a request to read the remaining
1104 * bytes, but we don't actually need to look at
1105 * the contents.
1106 */
1107 value = w_length;
1108 } else if (ctrl->bRequest == MTP_REQ_GET_DEVICE_STATUS
1109 && w_index == 0 && w_value == 0) {
1110 struct mtp_device_status *status = cdev->req->buf;
1111 status->wLength =
1112 __constant_cpu_to_le16(sizeof(*status));
1113
1114 DBG(cdev, "MTP_REQ_GET_DEVICE_STATUS\n");
1115 spin_lock_irqsave(&dev->lock, flags);
1116 /* device status is "busy" until we report
1117 * the cancelation to userspace
1118 */
Mike Lockwood090cbc42011-02-07 11:51:07 -05001119 if (dev->state == STATE_CANCELED)
Mike Lockwoodba83b012010-04-16 10:39:22 -04001120 status->wCode =
1121 __cpu_to_le16(MTP_RESPONSE_DEVICE_BUSY);
1122 else
1123 status->wCode =
1124 __cpu_to_le16(MTP_RESPONSE_OK);
Mike Lockwood090cbc42011-02-07 11:51:07 -05001125 spin_unlock_irqrestore(&dev->lock, flags);
Mike Lockwoodba83b012010-04-16 10:39:22 -04001126 value = sizeof(*status);
1127 }
1128 }
1129
1130 /* respond with data transfer or status phase? */
1131 if (value >= 0) {
1132 int rc;
1133 cdev->req->zero = value < w_length;
1134 cdev->req->length = value;
1135 rc = usb_ep_queue(cdev->gadget->ep0, cdev->req, GFP_ATOMIC);
1136 if (rc < 0)
1137 ERROR(cdev, "%s setup response queue error\n", __func__);
1138 }
1139
1140 if (value == -EOPNOTSUPP)
1141 VDBG(cdev,
1142 "unknown class-specific control req "
1143 "%02x.%02x v%04x i%04x l%u\n",
1144 ctrl->bRequestType, ctrl->bRequest,
1145 w_value, w_index, w_length);
1146 return value;
1147}
1148
1149static int mtp_function_set_alt(struct usb_function *f,
1150 unsigned intf, unsigned alt)
1151{
Benoit Gobyaab96812011-04-19 20:37:33 -07001152 struct mtp_dev *dev = func_to_mtp(f);
Mike Lockwoodba83b012010-04-16 10:39:22 -04001153 struct usb_composite_dev *cdev = f->config->cdev;
1154 int ret;
1155
1156 DBG(cdev, "mtp_function_set_alt intf: %d alt: %d\n", intf, alt);
1157 ret = usb_ep_enable(dev->ep_in,
1158 ep_choose(cdev->gadget,
1159 &mtp_highspeed_in_desc,
1160 &mtp_fullspeed_in_desc));
1161 if (ret)
1162 return ret;
1163 ret = usb_ep_enable(dev->ep_out,
1164 ep_choose(cdev->gadget,
1165 &mtp_highspeed_out_desc,
1166 &mtp_fullspeed_out_desc));
1167 if (ret) {
1168 usb_ep_disable(dev->ep_in);
1169 return ret;
1170 }
1171 ret = usb_ep_enable(dev->ep_intr, &mtp_intr_desc);
1172 if (ret) {
1173 usb_ep_disable(dev->ep_out);
1174 usb_ep_disable(dev->ep_in);
1175 return ret;
1176 }
1177 dev->state = STATE_READY;
1178
1179 /* readers may be blocked waiting for us to go online */
1180 wake_up(&dev->read_wq);
1181 return 0;
1182}
1183
1184static void mtp_function_disable(struct usb_function *f)
1185{
Benoit Gobyaab96812011-04-19 20:37:33 -07001186 struct mtp_dev *dev = func_to_mtp(f);
Mike Lockwoodba83b012010-04-16 10:39:22 -04001187 struct usb_composite_dev *cdev = dev->cdev;
1188
1189 DBG(cdev, "mtp_function_disable\n");
1190 dev->state = STATE_OFFLINE;
1191 usb_ep_disable(dev->ep_in);
1192 usb_ep_disable(dev->ep_out);
1193 usb_ep_disable(dev->ep_intr);
1194
1195 /* readers may be blocked waiting for us to go online */
1196 wake_up(&dev->read_wq);
1197
1198 VDBG(cdev, "%s disabled\n", dev->function.name);
1199}
1200
1201static int mtp_bind_config(struct usb_configuration *c)
1202{
Benoit Gobyaab96812011-04-19 20:37:33 -07001203 struct mtp_dev *dev = _mtp_dev;
Mike Lockwood090cbc42011-02-07 11:51:07 -05001204 int ret = 0;
Mike Lockwoodba83b012010-04-16 10:39:22 -04001205
1206 printk(KERN_INFO "mtp_bind_config\n");
1207
Mike Lockwoodba83b012010-04-16 10:39:22 -04001208 /* allocate a string ID for our interface */
1209 if (mtp_string_defs[INTERFACE_STRING_INDEX].id == 0) {
1210 ret = usb_string_id(c->cdev);
1211 if (ret < 0)
1212 return ret;
1213 mtp_string_defs[INTERFACE_STRING_INDEX].id = ret;
1214 mtp_interface_desc.iInterface = ret;
1215 }
1216
Mike Lockwoodba83b012010-04-16 10:39:22 -04001217 dev->cdev = c->cdev;
1218 dev->function.name = "mtp";
1219 dev->function.strings = mtp_strings,
1220 dev->function.descriptors = fs_mtp_descs;
1221 dev->function.hs_descriptors = hs_mtp_descs;
1222 dev->function.bind = mtp_function_bind;
1223 dev->function.unbind = mtp_function_unbind;
1224 dev->function.setup = mtp_function_setup;
1225 dev->function.set_alt = mtp_function_set_alt;
1226 dev->function.disable = mtp_function_disable;
1227
1228 /* MTP mode by default */
1229 dev->interface_mode = MTP_INTERFACE_MODE_MTP;
1230
Benoit Gobyaab96812011-04-19 20:37:33 -07001231 return usb_add_function(c, &dev->function);
1232}
1233
1234static int mtp_setup(void)
1235{
1236 struct mtp_dev *dev;
1237 int ret;
1238
1239 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1240 if (!dev)
1241 return -ENOMEM;
1242
1243 spin_lock_init(&dev->lock);
1244 init_waitqueue_head(&dev->read_wq);
1245 init_waitqueue_head(&dev->write_wq);
1246 atomic_set(&dev->open_excl, 0);
1247 atomic_set(&dev->ioctl_excl, 0);
1248 INIT_LIST_HEAD(&dev->tx_idle);
1249
1250 dev->wq = create_singlethread_workqueue("f_mtp");
1251 if (!dev->wq) {
1252 ret = -ENOMEM;
1253 goto err1;
1254 }
1255 INIT_WORK(&dev->send_file_work, send_file_work);
1256 INIT_WORK(&dev->receive_file_work, receive_file_work);
1257
Mike Lockwoodba83b012010-04-16 10:39:22 -04001258 _mtp_dev = dev;
1259
1260 ret = misc_register(&mtp_device);
1261 if (ret)
Mike Lockwoodba83b012010-04-16 10:39:22 -04001262 goto err2;
1263
1264 return 0;
1265
1266err2:
Benoit Gobyaab96812011-04-19 20:37:33 -07001267 destroy_workqueue(dev->wq);
Mike Lockwoodba83b012010-04-16 10:39:22 -04001268err1:
Benoit Gobyaab96812011-04-19 20:37:33 -07001269 _mtp_dev = NULL;
Mike Lockwoodba83b012010-04-16 10:39:22 -04001270 kfree(dev);
1271 printk(KERN_ERR "mtp gadget driver failed to initialize\n");
1272 return ret;
1273}
1274
Benoit Gobyaab96812011-04-19 20:37:33 -07001275static void mtp_cleanup(void)
Mike Lockwoodba83b012010-04-16 10:39:22 -04001276{
Benoit Gobyaab96812011-04-19 20:37:33 -07001277 struct mtp_dev *dev = _mtp_dev;
1278
1279 if (!dev)
1280 return;
1281
1282 misc_deregister(&mtp_device);
1283 destroy_workqueue(dev->wq);
1284 _mtp_dev = NULL;
1285 kfree(dev);
Mike Lockwoodba83b012010-04-16 10:39:22 -04001286}