blob: 230dc8d788d95a8d55e7d3721524202cb57ef9f4 [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
Mike Lockwoodba3673b2011-05-01 20:36:19 -040055#define INTR_REQ_MAX 5
Mike Lockwoodba83b012010-04-16 10:39:22 -040056
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
Benoit Gobyaab96812011-04-19 20:37:33 -070070static const char mtp_shortname[] = "mtp_usb";
Mike Lockwoodba83b012010-04-16 10:39:22 -040071
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;
Mike Lockwoodba3673b2011-05-01 20:36:19 -040092 struct list_head intr_idle;
Mike Lockwoodba83b012010-04-16 10:39:22 -040093
94 wait_queue_head_t read_wq;
95 wait_queue_head_t write_wq;
Mike Lockwoodba3673b2011-05-01 20:36:19 -040096 wait_queue_head_t intr_wq;
Mike Lockwoodba83b012010-04-16 10:39:22 -040097 struct usb_request *rx_req[RX_REQ_MAX];
98 int rx_done;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -040099
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 Lockwood1de4d4d2010-07-06 19:27:52 -0400375 if (req->status != 0)
376 dev->state = STATE_ERROR;
Mike Lockwoodba3673b2011-05-01 20:36:19 -0400377
378 mtp_req_put(dev, &dev->intr_idle, req);
379
380 wake_up(&dev->intr_wq);
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400381}
382
Benoit Gobyaab96812011-04-19 20:37:33 -0700383static int mtp_create_bulk_endpoints(struct mtp_dev *dev,
Mike Lockwoodba83b012010-04-16 10:39:22 -0400384 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
413 ep = usb_ep_autoconfig(cdev->gadget, out_desc);
414 if (!ep) {
415 DBG(cdev, "usb_ep_autoconfig for ep_out failed\n");
416 return -ENODEV;
417 }
418 DBG(cdev, "usb_ep_autoconfig for mtp ep_out got %s\n", ep->name);
419 ep->driver_data = dev; /* claim the endpoint */
420 dev->ep_out = ep;
421
422 ep = usb_ep_autoconfig(cdev->gadget, intr_desc);
423 if (!ep) {
424 DBG(cdev, "usb_ep_autoconfig for ep_intr failed\n");
425 return -ENODEV;
426 }
427 DBG(cdev, "usb_ep_autoconfig for mtp ep_intr got %s\n", ep->name);
428 ep->driver_data = dev; /* claim the endpoint */
429 dev->ep_intr = ep;
430
431 /* now allocate requests for our endpoints */
432 for (i = 0; i < TX_REQ_MAX; i++) {
Benoit Gobyaab96812011-04-19 20:37:33 -0700433 req = mtp_request_new(dev->ep_in, MTP_BULK_BUFFER_SIZE);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400434 if (!req)
435 goto fail;
436 req->complete = mtp_complete_in;
Benoit Gobyaab96812011-04-19 20:37:33 -0700437 mtp_req_put(dev, &dev->tx_idle, req);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400438 }
439 for (i = 0; i < RX_REQ_MAX; i++) {
Benoit Gobyaab96812011-04-19 20:37:33 -0700440 req = mtp_request_new(dev->ep_out, MTP_BULK_BUFFER_SIZE);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400441 if (!req)
442 goto fail;
443 req->complete = mtp_complete_out;
444 dev->rx_req[i] = req;
445 }
Mike Lockwoodba3673b2011-05-01 20:36:19 -0400446 for (i = 0; i < INTR_REQ_MAX; i++) {
447 req = mtp_request_new(dev->ep_intr, INTR_BUFFER_SIZE);
448 if (!req)
449 goto fail;
450 req->complete = mtp_complete_intr;
451 mtp_req_put(dev, &dev->intr_idle, req);
452 }
Mike Lockwoodba83b012010-04-16 10:39:22 -0400453
454 return 0;
455
456fail:
457 printk(KERN_ERR "mtp_bind() could not allocate requests\n");
458 return -1;
459}
460
461static ssize_t mtp_read(struct file *fp, char __user *buf,
462 size_t count, loff_t *pos)
463{
464 struct mtp_dev *dev = fp->private_data;
465 struct usb_composite_dev *cdev = dev->cdev;
466 struct usb_request *req;
467 int r = count, xfer;
468 int ret = 0;
469
470 DBG(cdev, "mtp_read(%d)\n", count);
471
Benoit Gobyaab96812011-04-19 20:37:33 -0700472 if (count > MTP_BULK_BUFFER_SIZE)
Mike Lockwoodba83b012010-04-16 10:39:22 -0400473 return -EINVAL;
474
475 /* we will block until we're online */
476 DBG(cdev, "mtp_read: waiting for online state\n");
477 ret = wait_event_interruptible(dev->read_wq,
478 dev->state != STATE_OFFLINE);
479 if (ret < 0) {
480 r = ret;
481 goto done;
482 }
483 spin_lock_irq(&dev->lock);
484 if (dev->state == STATE_CANCELED) {
485 /* report cancelation to userspace */
486 dev->state = STATE_READY;
487 spin_unlock_irq(&dev->lock);
488 return -ECANCELED;
489 }
490 dev->state = STATE_BUSY;
491 spin_unlock_irq(&dev->lock);
492
493requeue_req:
494 /* queue a request */
495 req = dev->rx_req[0];
496 req->length = count;
497 dev->rx_done = 0;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400498 ret = usb_ep_queue(dev->ep_out, req, GFP_KERNEL);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400499 if (ret < 0) {
500 r = -EIO;
501 goto done;
502 } else {
503 DBG(cdev, "rx %p queue\n", req);
504 }
505
506 /* wait for a request to complete */
507 ret = wait_event_interruptible(dev->read_wq, dev->rx_done);
508 if (ret < 0) {
509 r = ret;
Mike Lockwood43f3dc82011-02-19 15:33:17 -0500510 usb_ep_dequeue(dev->ep_out, req);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400511 goto done;
512 }
513 if (dev->state == STATE_BUSY) {
514 /* If we got a 0-len packet, throw it back and try again. */
515 if (req->actual == 0)
516 goto requeue_req;
517
518 DBG(cdev, "rx %p %d\n", req, req->actual);
519 xfer = (req->actual < count) ? req->actual : count;
520 r = xfer;
521 if (copy_to_user(buf, req->buf, xfer))
522 r = -EFAULT;
523 } else
524 r = -EIO;
525
526done:
527 spin_lock_irq(&dev->lock);
528 if (dev->state == STATE_CANCELED)
529 r = -ECANCELED;
530 else if (dev->state != STATE_OFFLINE)
531 dev->state = STATE_READY;
532 spin_unlock_irq(&dev->lock);
533
534 DBG(cdev, "mtp_read returning %d\n", r);
535 return r;
536}
537
538static ssize_t mtp_write(struct file *fp, const char __user *buf,
539 size_t count, loff_t *pos)
540{
541 struct mtp_dev *dev = fp->private_data;
542 struct usb_composite_dev *cdev = dev->cdev;
543 struct usb_request *req = 0;
544 int r = count, xfer;
Mike Lockwood16c08c22010-11-17 11:16:35 -0500545 int sendZLP = 0;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400546 int ret;
547
548 DBG(cdev, "mtp_write(%d)\n", count);
549
550 spin_lock_irq(&dev->lock);
551 if (dev->state == STATE_CANCELED) {
552 /* report cancelation to userspace */
553 dev->state = STATE_READY;
554 spin_unlock_irq(&dev->lock);
555 return -ECANCELED;
556 }
557 if (dev->state == STATE_OFFLINE) {
558 spin_unlock_irq(&dev->lock);
559 return -ENODEV;
560 }
561 dev->state = STATE_BUSY;
562 spin_unlock_irq(&dev->lock);
563
Mike Lockwood16c08c22010-11-17 11:16:35 -0500564 /* we need to send a zero length packet to signal the end of transfer
565 * if the transfer size is aligned to a packet boundary.
566 */
567 if ((count & (dev->ep_in->maxpacket - 1)) == 0) {
568 sendZLP = 1;
569 }
570
571 while (count > 0 || sendZLP) {
572 /* so we exit after sending ZLP */
573 if (count == 0)
574 sendZLP = 0;
575
Mike Lockwoodba83b012010-04-16 10:39:22 -0400576 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,
Benoit Gobyaab96812011-04-19 20:37:33 -0700585 ((req = mtp_req_get(dev, &dev->tx_idle))
Mike Lockwoodba83b012010-04-16 10:39:22 -0400586 || dev->state != STATE_BUSY));
587 if (!req) {
588 r = ret;
589 break;
590 }
591
Benoit Gobyaab96812011-04-19 20:37:33 -0700592 if (count > MTP_BULK_BUFFER_SIZE)
593 xfer = MTP_BULK_BUFFER_SIZE;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400594 else
595 xfer = count;
Mike Lockwood3e800b62010-11-16 17:14:32 -0500596 if (xfer && copy_from_user(req->buf, buf, xfer)) {
Mike Lockwoodba83b012010-04-16 10:39:22 -0400597 r = -EFAULT;
598 break;
599 }
600
601 req->length = xfer;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400602 ret = usb_ep_queue(dev->ep_in, req, GFP_KERNEL);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400603 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;
Mike Lockwood16c08c22010-11-17 11:16:35 -0500614 }
Mike Lockwoodba83b012010-04-16 10:39:22 -0400615
616 if (req)
Benoit Gobyaab96812011-04-19 20:37:33 -0700617 mtp_req_put(dev, &dev->tx_idle, req);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400618
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
Mike Lockwood491d4182010-11-08 10:41:31 -0500630/* read from a local file and write to USB */
631static void send_file_work(struct work_struct *data) {
632 struct mtp_dev *dev = container_of(data, struct mtp_dev, send_file_work);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400633 struct usb_composite_dev *cdev = dev->cdev;
634 struct usb_request *req = 0;
Mike Lockwood491d4182010-11-08 10:41:31 -0500635 struct file *filp;
636 loff_t offset;
Mike Lockwood3e800b62010-11-16 17:14:32 -0500637 int64_t count;
Mike Lockwood76ac6552010-11-15 15:22:21 -0500638 int xfer, ret;
639 int r = 0;
Mike Lockwood3e800b62010-11-16 17:14:32 -0500640 int sendZLP = 0;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400641
Mike Lockwood491d4182010-11-08 10:41:31 -0500642 /* read our parameters */
643 smp_rmb();
644 filp = dev->xfer_file;
645 offset = dev->xfer_file_offset;
Mike Lockwood76ac6552010-11-15 15:22:21 -0500646 count = dev->xfer_file_length;
Mike Lockwood491d4182010-11-08 10:41:31 -0500647
Mike Lockwood3e800b62010-11-16 17:14:32 -0500648 DBG(cdev, "send_file_work(%lld %lld)\n", offset, count);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400649
Mike Lockwood3e800b62010-11-16 17:14:32 -0500650 /* we need to send a zero length packet to signal the end of transfer
Mike Lockwood16c08c22010-11-17 11:16:35 -0500651 * if the transfer size is aligned to a packet boundary.
Mike Lockwood3e800b62010-11-16 17:14:32 -0500652 */
Mike Lockwood16c08c22010-11-17 11:16:35 -0500653 if ((dev->xfer_file_length & (dev->ep_in->maxpacket - 1)) == 0) {
Mike Lockwood3e800b62010-11-16 17:14:32 -0500654 sendZLP = 1;
655 }
656
657 while (count > 0 || sendZLP) {
658 /* so we exit after sending ZLP */
659 if (count == 0)
660 sendZLP = 0;
661
Mike Lockwoodba83b012010-04-16 10:39:22 -0400662 /* get an idle tx request to use */
663 req = 0;
664 ret = wait_event_interruptible(dev->write_wq,
Benoit Gobyaab96812011-04-19 20:37:33 -0700665 (req = mtp_req_get(dev, &dev->tx_idle))
Mike Lockwoodba83b012010-04-16 10:39:22 -0400666 || dev->state != STATE_BUSY);
Mike Lockwood090cbc42011-02-07 11:51:07 -0500667 if (dev->state == STATE_CANCELED) {
668 r = -ECANCELED;
669 break;
670 }
Mike Lockwoodba83b012010-04-16 10:39:22 -0400671 if (!req) {
672 r = ret;
673 break;
674 }
675
Benoit Gobyaab96812011-04-19 20:37:33 -0700676 if (count > MTP_BULK_BUFFER_SIZE)
677 xfer = MTP_BULK_BUFFER_SIZE;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400678 else
679 xfer = count;
680 ret = vfs_read(filp, req->buf, xfer, &offset);
681 if (ret < 0) {
682 r = ret;
683 break;
684 }
685 xfer = ret;
686
687 req->length = xfer;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400688 ret = usb_ep_queue(dev->ep_in, req, GFP_KERNEL);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400689 if (ret < 0) {
Mike Lockwood491d4182010-11-08 10:41:31 -0500690 DBG(cdev, "send_file_work: xfer error %d\n", ret);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400691 dev->state = STATE_ERROR;
692 r = -EIO;
693 break;
694 }
695
696 count -= xfer;
697
698 /* zero this so we don't try to free it on error exit */
699 req = 0;
700 }
701
702 if (req)
Benoit Gobyaab96812011-04-19 20:37:33 -0700703 mtp_req_put(dev, &dev->tx_idle, req);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400704
Mike Lockwood491d4182010-11-08 10:41:31 -0500705 DBG(cdev, "send_file_work returning %d\n", r);
706 /* write the result */
707 dev->xfer_result = r;
708 smp_wmb();
Mike Lockwoodba83b012010-04-16 10:39:22 -0400709}
710
Mike Lockwood491d4182010-11-08 10:41:31 -0500711/* read from USB and write to a local file */
712static void receive_file_work(struct work_struct *data)
Mike Lockwoodba83b012010-04-16 10:39:22 -0400713{
Mike Lockwood491d4182010-11-08 10:41:31 -0500714 struct mtp_dev *dev = container_of(data, struct mtp_dev, receive_file_work);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400715 struct usb_composite_dev *cdev = dev->cdev;
716 struct usb_request *read_req = NULL, *write_req = NULL;
Mike Lockwood491d4182010-11-08 10:41:31 -0500717 struct file *filp;
718 loff_t offset;
Mike Lockwood3e800b62010-11-16 17:14:32 -0500719 int64_t count;
Mike Lockwood76ac6552010-11-15 15:22:21 -0500720 int ret, cur_buf = 0;
721 int r = 0;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400722
Mike Lockwood491d4182010-11-08 10:41:31 -0500723 /* read our parameters */
724 smp_rmb();
725 filp = dev->xfer_file;
726 offset = dev->xfer_file_offset;
Mike Lockwood76ac6552010-11-15 15:22:21 -0500727 count = dev->xfer_file_length;
Mike Lockwood491d4182010-11-08 10:41:31 -0500728
Mike Lockwood3e800b62010-11-16 17:14:32 -0500729 DBG(cdev, "receive_file_work(%lld)\n", count);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400730
731 while (count > 0 || write_req) {
732 if (count > 0) {
733 /* queue a request */
734 read_req = dev->rx_req[cur_buf];
735 cur_buf = (cur_buf + 1) % RX_REQ_MAX;
736
Benoit Gobyaab96812011-04-19 20:37:33 -0700737 read_req->length = (count > MTP_BULK_BUFFER_SIZE
738 ? MTP_BULK_BUFFER_SIZE : count);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400739 dev->rx_done = 0;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400740 ret = usb_ep_queue(dev->ep_out, read_req, GFP_KERNEL);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400741 if (ret < 0) {
742 r = -EIO;
743 dev->state = STATE_ERROR;
744 break;
745 }
Mike Lockwoodba83b012010-04-16 10:39:22 -0400746 }
747
748 if (write_req) {
749 DBG(cdev, "rx %p %d\n", write_req, write_req->actual);
750 ret = vfs_write(filp, write_req->buf, write_req->actual,
751 &offset);
752 DBG(cdev, "vfs_write %d\n", ret);
753 if (ret != write_req->actual) {
754 r = -EIO;
755 dev->state = STATE_ERROR;
756 break;
757 }
758 write_req = NULL;
759 }
760
761 if (read_req) {
762 /* wait for our last read to complete */
763 ret = wait_event_interruptible(dev->read_wq,
764 dev->rx_done || dev->state != STATE_BUSY);
Mike Lockwood50fe49a2011-01-13 16:19:57 -0500765 if (dev->state == STATE_CANCELED) {
766 r = -ECANCELED;
767 if (!dev->rx_done)
768 usb_ep_dequeue(dev->ep_out, read_req);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400769 break;
770 }
Mike Lockwood3e800b62010-11-16 17:14:32 -0500771 /* if xfer_file_length is 0xFFFFFFFF, then we read until
772 * we get a zero length packet
773 */
774 if (count != 0xFFFFFFFF)
775 count -= read_req->actual;
776 if (read_req->actual < read_req->length) {
777 /* short packet is used to signal EOF for sizes > 4 gig */
778 DBG(cdev, "got short packet\n");
779 count = 0;
780 }
781
Mike Lockwoodba83b012010-04-16 10:39:22 -0400782 write_req = read_req;
783 read_req = NULL;
784 }
785 }
786
Mike Lockwood491d4182010-11-08 10:41:31 -0500787 DBG(cdev, "receive_file_work returning %d\n", r);
788 /* write the result */
789 dev->xfer_result = r;
790 smp_wmb();
Mike Lockwoodba83b012010-04-16 10:39:22 -0400791}
792
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400793static int mtp_send_event(struct mtp_dev *dev, struct mtp_event *event)
794{
Mike Lockwoodba3673b2011-05-01 20:36:19 -0400795 struct usb_request *req= NULL;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400796 int ret;
797 int length = event->length;
798
799 DBG(dev->cdev, "mtp_send_event(%d)\n", event->length);
800
801 if (length < 0 || length > INTR_BUFFER_SIZE)
802 return -EINVAL;
Mike Lockwood491d4182010-11-08 10:41:31 -0500803 if (dev->state == STATE_OFFLINE)
804 return -ENODEV;
Mike Lockwood292b9632011-02-10 11:54:53 -0500805
Mike Lockwoodba3673b2011-05-01 20:36:19 -0400806 ret = wait_event_interruptible_timeout(dev->intr_wq,
807 (req = mtp_req_get(dev, &dev->intr_idle)), msecs_to_jiffies(1000));
808 if (!req)
809 return -ETIME;
810
811 if (copy_from_user(req->buf, (void __user *)event->data, length)) {
812 mtp_req_put(dev, &dev->intr_idle, req);
Mike Lockwood491d4182010-11-08 10:41:31 -0500813 return -EFAULT;
Mike Lockwoodba3673b2011-05-01 20:36:19 -0400814 }
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400815 req->length = length;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400816 ret = usb_ep_queue(dev->ep_intr, req, GFP_KERNEL);
817 if (ret)
Mike Lockwoodba3673b2011-05-01 20:36:19 -0400818 mtp_req_put(dev, &dev->intr_idle, req);
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400819
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400820 return ret;
821}
822
Mike Lockwoodba83b012010-04-16 10:39:22 -0400823static long mtp_ioctl(struct file *fp, unsigned code, unsigned long value)
824{
825 struct mtp_dev *dev = fp->private_data;
826 struct file *filp = NULL;
827 int ret = -EINVAL;
828
Benoit Gobyaab96812011-04-19 20:37:33 -0700829 if (mtp_lock(&dev->ioctl_excl))
Mike Lockwood491d4182010-11-08 10:41:31 -0500830 return -EBUSY;
831
Mike Lockwoodba83b012010-04-16 10:39:22 -0400832 switch (code) {
833 case MTP_SEND_FILE:
834 case MTP_RECEIVE_FILE:
835 {
836 struct mtp_file_range mfr;
Mike Lockwood491d4182010-11-08 10:41:31 -0500837 struct work_struct *work;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400838
839 spin_lock_irq(&dev->lock);
840 if (dev->state == STATE_CANCELED) {
841 /* report cancelation to userspace */
842 dev->state = STATE_READY;
843 spin_unlock_irq(&dev->lock);
Mike Lockwood491d4182010-11-08 10:41:31 -0500844 ret = -ECANCELED;
845 goto out;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400846 }
847 if (dev->state == STATE_OFFLINE) {
848 spin_unlock_irq(&dev->lock);
Mike Lockwood491d4182010-11-08 10:41:31 -0500849 ret = -ENODEV;
850 goto out;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400851 }
852 dev->state = STATE_BUSY;
853 spin_unlock_irq(&dev->lock);
854
855 if (copy_from_user(&mfr, (void __user *)value, sizeof(mfr))) {
856 ret = -EFAULT;
857 goto fail;
858 }
Mike Lockwood491d4182010-11-08 10:41:31 -0500859 /* hold a reference to the file while we are working with it */
Mike Lockwoodba83b012010-04-16 10:39:22 -0400860 filp = fget(mfr.fd);
861 if (!filp) {
862 ret = -EBADF;
863 goto fail;
864 }
865
Mike Lockwood491d4182010-11-08 10:41:31 -0500866 /* write the parameters */
867 dev->xfer_file = filp;
868 dev->xfer_file_offset = mfr.offset;
869 dev->xfer_file_length = mfr.length;
870 smp_wmb();
Mike Lockwoodba83b012010-04-16 10:39:22 -0400871
872 if (code == MTP_SEND_FILE)
Mike Lockwood491d4182010-11-08 10:41:31 -0500873 work = &dev->send_file_work;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400874 else
Mike Lockwood491d4182010-11-08 10:41:31 -0500875 work = &dev->receive_file_work;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400876
Mike Lockwood491d4182010-11-08 10:41:31 -0500877 /* We do the file transfer on a work queue so it will run
878 * in kernel context, which is necessary for vfs_read and
879 * vfs_write to use our buffers in the kernel address space.
880 */
881 queue_work(dev->wq, work);
882 /* wait for operation to complete */
883 flush_workqueue(dev->wq);
884 fput(filp);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400885
Mike Lockwood491d4182010-11-08 10:41:31 -0500886 /* read the result */
887 smp_rmb();
888 ret = dev->xfer_result;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400889 break;
890 }
891 case MTP_SET_INTERFACE_MODE:
892 if (value == MTP_INTERFACE_MODE_MTP ||
893 value == MTP_INTERFACE_MODE_PTP) {
894 dev->interface_mode = value;
895 if (value == MTP_INTERFACE_MODE_PTP) {
896 dev->function.descriptors = fs_ptp_descs;
897 dev->function.hs_descriptors = hs_ptp_descs;
898 } else {
899 dev->function.descriptors = fs_mtp_descs;
900 dev->function.hs_descriptors = hs_mtp_descs;
901 }
902 ret = 0;
903 }
904 break;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400905 case MTP_SEND_EVENT:
906 {
907 struct mtp_event event;
908 /* return here so we don't change dev->state below,
909 * which would interfere with bulk transfer state.
910 */
911 if (copy_from_user(&event, (void __user *)value, sizeof(event)))
Mike Lockwood491d4182010-11-08 10:41:31 -0500912 ret = -EFAULT;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400913 else
Mike Lockwood491d4182010-11-08 10:41:31 -0500914 ret = mtp_send_event(dev, &event);
915 goto out;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400916 }
Mike Lockwoodba83b012010-04-16 10:39:22 -0400917 }
918
919fail:
Mike Lockwoodba83b012010-04-16 10:39:22 -0400920 spin_lock_irq(&dev->lock);
921 if (dev->state == STATE_CANCELED)
922 ret = -ECANCELED;
923 else if (dev->state != STATE_OFFLINE)
924 dev->state = STATE_READY;
925 spin_unlock_irq(&dev->lock);
Mike Lockwood491d4182010-11-08 10:41:31 -0500926out:
Benoit Gobyaab96812011-04-19 20:37:33 -0700927 mtp_unlock(&dev->ioctl_excl);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400928 DBG(dev->cdev, "ioctl returning %d\n", ret);
929 return ret;
930}
931
932static int mtp_open(struct inode *ip, struct file *fp)
933{
934 printk(KERN_INFO "mtp_open\n");
Benoit Gobyaab96812011-04-19 20:37:33 -0700935 if (mtp_lock(&_mtp_dev->open_excl))
Mike Lockwoodba83b012010-04-16 10:39:22 -0400936 return -EBUSY;
937
Mike Lockwoodba83b012010-04-16 10:39:22 -0400938 /* clear any error condition */
939 if (_mtp_dev->state != STATE_OFFLINE)
940 _mtp_dev->state = STATE_READY;
941
942 fp->private_data = _mtp_dev;
943 return 0;
944}
945
946static int mtp_release(struct inode *ip, struct file *fp)
947{
948 printk(KERN_INFO "mtp_release\n");
949
Benoit Gobyaab96812011-04-19 20:37:33 -0700950 mtp_unlock(&_mtp_dev->open_excl);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400951 return 0;
952}
953
954/* file operations for /dev/mtp_usb */
955static const struct file_operations mtp_fops = {
956 .owner = THIS_MODULE,
957 .read = mtp_read,
958 .write = mtp_write,
959 .unlocked_ioctl = mtp_ioctl,
960 .open = mtp_open,
961 .release = mtp_release,
962};
963
964static struct miscdevice mtp_device = {
965 .minor = MISC_DYNAMIC_MINOR,
Benoit Gobyaab96812011-04-19 20:37:33 -0700966 .name = mtp_shortname,
Mike Lockwoodba83b012010-04-16 10:39:22 -0400967 .fops = &mtp_fops,
968};
969
Benoit Gobyaab96812011-04-19 20:37:33 -0700970static int mtp_ctrlrequest(struct usb_composite_dev *cdev,
971 const struct usb_ctrlrequest *ctrl)
972{
973 struct mtp_dev *dev = _mtp_dev;
974 int value = -EOPNOTSUPP;
975 u16 w_index = le16_to_cpu(ctrl->wIndex);
976 u16 w_value = le16_to_cpu(ctrl->wValue);
977 u16 w_length = le16_to_cpu(ctrl->wLength);
978
979 VDBG(cdev, "mtp_ctrlrequest "
980 "%02x.%02x v%04x i%04x l%u\n",
981 ctrl->bRequestType, ctrl->bRequest,
982 w_value, w_index, w_length);
983
984 /* Handle MTP OS string */
985 if (dev->interface_mode == MTP_INTERFACE_MODE_MTP
986 && ctrl->bRequestType ==
987 (USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE)
988 && ctrl->bRequest == USB_REQ_GET_DESCRIPTOR
989 && (w_value >> 8) == USB_DT_STRING
990 && (w_value & 0xFF) == MTP_OS_STRING_ID) {
991 value = (w_length < sizeof(mtp_os_string)
992 ? w_length : sizeof(mtp_os_string));
993 memcpy(cdev->req->buf, mtp_os_string, value);
994 }
995 /* respond with data transfer or status phase? */
996 if (value >= 0) {
997 int rc;
998 cdev->req->zero = value < w_length;
999 cdev->req->length = value;
1000 rc = usb_ep_queue(cdev->gadget->ep0, cdev->req, GFP_ATOMIC);
1001 if (rc < 0)
1002 ERROR(cdev, "%s setup response queue error\n", __func__);
1003 }
1004 return value;
1005}
1006
Mike Lockwoodba83b012010-04-16 10:39:22 -04001007static int
1008mtp_function_bind(struct usb_configuration *c, struct usb_function *f)
1009{
1010 struct usb_composite_dev *cdev = c->cdev;
Benoit Gobyaab96812011-04-19 20:37:33 -07001011 struct mtp_dev *dev = func_to_mtp(f);
Mike Lockwoodba83b012010-04-16 10:39:22 -04001012 int id;
1013 int ret;
1014
1015 dev->cdev = cdev;
1016 DBG(cdev, "mtp_function_bind dev: %p\n", dev);
1017
1018 /* allocate interface ID(s) */
1019 id = usb_interface_id(c, f);
1020 if (id < 0)
1021 return id;
1022 mtp_interface_desc.bInterfaceNumber = id;
1023
1024 /* allocate endpoints */
Benoit Gobyaab96812011-04-19 20:37:33 -07001025 ret = mtp_create_bulk_endpoints(dev, &mtp_fullspeed_in_desc,
Mike Lockwoodba83b012010-04-16 10:39:22 -04001026 &mtp_fullspeed_out_desc, &mtp_intr_desc);
1027 if (ret)
1028 return ret;
1029
1030 /* support high speed hardware */
1031 if (gadget_is_dualspeed(c->cdev->gadget)) {
1032 mtp_highspeed_in_desc.bEndpointAddress =
1033 mtp_fullspeed_in_desc.bEndpointAddress;
1034 mtp_highspeed_out_desc.bEndpointAddress =
1035 mtp_fullspeed_out_desc.bEndpointAddress;
1036 }
1037
1038 DBG(cdev, "%s speed %s: IN/%s, OUT/%s\n",
1039 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
1040 f->name, dev->ep_in->name, dev->ep_out->name);
1041 return 0;
1042}
1043
1044static void
1045mtp_function_unbind(struct usb_configuration *c, struct usb_function *f)
1046{
Benoit Gobyaab96812011-04-19 20:37:33 -07001047 struct mtp_dev *dev = func_to_mtp(f);
Mike Lockwoodba83b012010-04-16 10:39:22 -04001048 struct usb_request *req;
1049 int i;
1050
Benoit Gobyaab96812011-04-19 20:37:33 -07001051 while ((req = mtp_req_get(dev, &dev->tx_idle)))
Mike Lockwoodba83b012010-04-16 10:39:22 -04001052 mtp_request_free(req, dev->ep_in);
1053 for (i = 0; i < RX_REQ_MAX; i++)
1054 mtp_request_free(dev->rx_req[i], dev->ep_out);
Mike Lockwoodba3673b2011-05-01 20:36:19 -04001055 while ((req = mtp_req_get(dev, &dev->intr_idle)))
1056 mtp_request_free(req, dev->ep_intr);
Mike Lockwoodba83b012010-04-16 10:39:22 -04001057 dev->state = STATE_OFFLINE;
Mike Lockwoodba83b012010-04-16 10:39:22 -04001058}
1059
1060static int mtp_function_setup(struct usb_function *f,
1061 const struct usb_ctrlrequest *ctrl)
1062{
Benoit Gobyaab96812011-04-19 20:37:33 -07001063 struct mtp_dev *dev = func_to_mtp(f);
Mike Lockwoodba83b012010-04-16 10:39:22 -04001064 struct usb_composite_dev *cdev = dev->cdev;
1065 int value = -EOPNOTSUPP;
1066 u16 w_index = le16_to_cpu(ctrl->wIndex);
1067 u16 w_value = le16_to_cpu(ctrl->wValue);
1068 u16 w_length = le16_to_cpu(ctrl->wLength);
1069 unsigned long flags;
1070
Mike Lockwoodba83b012010-04-16 10:39:22 -04001071 VDBG(cdev, "mtp_function_setup "
1072 "%02x.%02x v%04x i%04x l%u\n",
1073 ctrl->bRequestType, ctrl->bRequest,
1074 w_value, w_index, w_length);
1075
Mike Lockwoodba83b012010-04-16 10:39:22 -04001076 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_VENDOR) {
1077 /* Handle MTP OS descriptor */
1078 DBG(cdev, "vendor request: %d index: %d value: %d length: %d\n",
1079 ctrl->bRequest, w_index, w_value, w_length);
1080
1081 if (dev->interface_mode == MTP_INTERFACE_MODE_MTP
1082 && ctrl->bRequest == 1
1083 && (ctrl->bRequestType & USB_DIR_IN)
1084 && (w_index == 4 || w_index == 5)) {
1085 value = (w_length < sizeof(mtp_ext_config_desc) ?
1086 w_length : sizeof(mtp_ext_config_desc));
1087 memcpy(cdev->req->buf, &mtp_ext_config_desc, value);
1088 }
1089 }
1090 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_CLASS) {
1091 DBG(cdev, "class request: %d index: %d value: %d length: %d\n",
1092 ctrl->bRequest, w_index, w_value, w_length);
1093
1094 if (ctrl->bRequest == MTP_REQ_CANCEL && w_index == 0
1095 && w_value == 0) {
1096 DBG(cdev, "MTP_REQ_CANCEL\n");
1097
1098 spin_lock_irqsave(&dev->lock, flags);
1099 if (dev->state == STATE_BUSY) {
1100 dev->state = STATE_CANCELED;
1101 wake_up(&dev->read_wq);
1102 wake_up(&dev->write_wq);
1103 }
1104 spin_unlock_irqrestore(&dev->lock, flags);
1105
1106 /* We need to queue a request to read the remaining
1107 * bytes, but we don't actually need to look at
1108 * the contents.
1109 */
1110 value = w_length;
1111 } else if (ctrl->bRequest == MTP_REQ_GET_DEVICE_STATUS
1112 && w_index == 0 && w_value == 0) {
1113 struct mtp_device_status *status = cdev->req->buf;
1114 status->wLength =
1115 __constant_cpu_to_le16(sizeof(*status));
1116
1117 DBG(cdev, "MTP_REQ_GET_DEVICE_STATUS\n");
1118 spin_lock_irqsave(&dev->lock, flags);
1119 /* device status is "busy" until we report
1120 * the cancelation to userspace
1121 */
Mike Lockwood090cbc42011-02-07 11:51:07 -05001122 if (dev->state == STATE_CANCELED)
Mike Lockwoodba83b012010-04-16 10:39:22 -04001123 status->wCode =
1124 __cpu_to_le16(MTP_RESPONSE_DEVICE_BUSY);
1125 else
1126 status->wCode =
1127 __cpu_to_le16(MTP_RESPONSE_OK);
Mike Lockwood090cbc42011-02-07 11:51:07 -05001128 spin_unlock_irqrestore(&dev->lock, flags);
Mike Lockwoodba83b012010-04-16 10:39:22 -04001129 value = sizeof(*status);
1130 }
1131 }
1132
1133 /* respond with data transfer or status phase? */
1134 if (value >= 0) {
1135 int rc;
1136 cdev->req->zero = value < w_length;
1137 cdev->req->length = value;
1138 rc = usb_ep_queue(cdev->gadget->ep0, cdev->req, GFP_ATOMIC);
1139 if (rc < 0)
1140 ERROR(cdev, "%s setup response queue error\n", __func__);
1141 }
1142
1143 if (value == -EOPNOTSUPP)
1144 VDBG(cdev,
1145 "unknown class-specific control req "
1146 "%02x.%02x v%04x i%04x l%u\n",
1147 ctrl->bRequestType, ctrl->bRequest,
1148 w_value, w_index, w_length);
1149 return value;
1150}
1151
1152static int mtp_function_set_alt(struct usb_function *f,
1153 unsigned intf, unsigned alt)
1154{
Benoit Gobyaab96812011-04-19 20:37:33 -07001155 struct mtp_dev *dev = func_to_mtp(f);
Mike Lockwoodba83b012010-04-16 10:39:22 -04001156 struct usb_composite_dev *cdev = f->config->cdev;
1157 int ret;
1158
1159 DBG(cdev, "mtp_function_set_alt intf: %d alt: %d\n", intf, alt);
1160 ret = usb_ep_enable(dev->ep_in,
1161 ep_choose(cdev->gadget,
1162 &mtp_highspeed_in_desc,
1163 &mtp_fullspeed_in_desc));
1164 if (ret)
1165 return ret;
1166 ret = usb_ep_enable(dev->ep_out,
1167 ep_choose(cdev->gadget,
1168 &mtp_highspeed_out_desc,
1169 &mtp_fullspeed_out_desc));
1170 if (ret) {
1171 usb_ep_disable(dev->ep_in);
1172 return ret;
1173 }
1174 ret = usb_ep_enable(dev->ep_intr, &mtp_intr_desc);
1175 if (ret) {
1176 usb_ep_disable(dev->ep_out);
1177 usb_ep_disable(dev->ep_in);
1178 return ret;
1179 }
1180 dev->state = STATE_READY;
1181
1182 /* readers may be blocked waiting for us to go online */
1183 wake_up(&dev->read_wq);
1184 return 0;
1185}
1186
1187static void mtp_function_disable(struct usb_function *f)
1188{
Benoit Gobyaab96812011-04-19 20:37:33 -07001189 struct mtp_dev *dev = func_to_mtp(f);
Mike Lockwoodba83b012010-04-16 10:39:22 -04001190 struct usb_composite_dev *cdev = dev->cdev;
1191
1192 DBG(cdev, "mtp_function_disable\n");
1193 dev->state = STATE_OFFLINE;
1194 usb_ep_disable(dev->ep_in);
1195 usb_ep_disable(dev->ep_out);
1196 usb_ep_disable(dev->ep_intr);
1197
1198 /* readers may be blocked waiting for us to go online */
1199 wake_up(&dev->read_wq);
1200
1201 VDBG(cdev, "%s disabled\n", dev->function.name);
1202}
1203
1204static int mtp_bind_config(struct usb_configuration *c)
1205{
Benoit Gobyaab96812011-04-19 20:37:33 -07001206 struct mtp_dev *dev = _mtp_dev;
Mike Lockwood090cbc42011-02-07 11:51:07 -05001207 int ret = 0;
Mike Lockwoodba83b012010-04-16 10:39:22 -04001208
1209 printk(KERN_INFO "mtp_bind_config\n");
1210
Mike Lockwoodba83b012010-04-16 10:39:22 -04001211 /* allocate a string ID for our interface */
1212 if (mtp_string_defs[INTERFACE_STRING_INDEX].id == 0) {
1213 ret = usb_string_id(c->cdev);
1214 if (ret < 0)
1215 return ret;
1216 mtp_string_defs[INTERFACE_STRING_INDEX].id = ret;
1217 mtp_interface_desc.iInterface = ret;
1218 }
1219
Mike Lockwoodba83b012010-04-16 10:39:22 -04001220 dev->cdev = c->cdev;
1221 dev->function.name = "mtp";
1222 dev->function.strings = mtp_strings,
1223 dev->function.descriptors = fs_mtp_descs;
1224 dev->function.hs_descriptors = hs_mtp_descs;
1225 dev->function.bind = mtp_function_bind;
1226 dev->function.unbind = mtp_function_unbind;
1227 dev->function.setup = mtp_function_setup;
1228 dev->function.set_alt = mtp_function_set_alt;
1229 dev->function.disable = mtp_function_disable;
1230
1231 /* MTP mode by default */
1232 dev->interface_mode = MTP_INTERFACE_MODE_MTP;
1233
Benoit Gobyaab96812011-04-19 20:37:33 -07001234 return usb_add_function(c, &dev->function);
1235}
1236
1237static int mtp_setup(void)
1238{
1239 struct mtp_dev *dev;
1240 int ret;
1241
1242 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1243 if (!dev)
1244 return -ENOMEM;
1245
1246 spin_lock_init(&dev->lock);
1247 init_waitqueue_head(&dev->read_wq);
1248 init_waitqueue_head(&dev->write_wq);
Mike Lockwoodba3673b2011-05-01 20:36:19 -04001249 init_waitqueue_head(&dev->intr_wq);
Benoit Gobyaab96812011-04-19 20:37:33 -07001250 atomic_set(&dev->open_excl, 0);
1251 atomic_set(&dev->ioctl_excl, 0);
1252 INIT_LIST_HEAD(&dev->tx_idle);
Mike Lockwoodba3673b2011-05-01 20:36:19 -04001253 INIT_LIST_HEAD(&dev->intr_idle);
Benoit Gobyaab96812011-04-19 20:37:33 -07001254
1255 dev->wq = create_singlethread_workqueue("f_mtp");
1256 if (!dev->wq) {
1257 ret = -ENOMEM;
1258 goto err1;
1259 }
1260 INIT_WORK(&dev->send_file_work, send_file_work);
1261 INIT_WORK(&dev->receive_file_work, receive_file_work);
1262
Mike Lockwoodba83b012010-04-16 10:39:22 -04001263 _mtp_dev = dev;
1264
1265 ret = misc_register(&mtp_device);
1266 if (ret)
Mike Lockwoodba83b012010-04-16 10:39:22 -04001267 goto err2;
1268
1269 return 0;
1270
1271err2:
Benoit Gobyaab96812011-04-19 20:37:33 -07001272 destroy_workqueue(dev->wq);
Mike Lockwoodba83b012010-04-16 10:39:22 -04001273err1:
Benoit Gobyaab96812011-04-19 20:37:33 -07001274 _mtp_dev = NULL;
Mike Lockwoodba83b012010-04-16 10:39:22 -04001275 kfree(dev);
1276 printk(KERN_ERR "mtp gadget driver failed to initialize\n");
1277 return ret;
1278}
1279
Benoit Gobyaab96812011-04-19 20:37:33 -07001280static void mtp_cleanup(void)
Mike Lockwoodba83b012010-04-16 10:39:22 -04001281{
Benoit Gobyaab96812011-04-19 20:37:33 -07001282 struct mtp_dev *dev = _mtp_dev;
1283
1284 if (!dev)
1285 return;
1286
1287 misc_deregister(&mtp_device);
1288 destroy_workqueue(dev->wq);
1289 _mtp_dev = NULL;
1290 kfree(dev);
Mike Lockwoodba83b012010-04-16 10:39:22 -04001291}