blob: 447e8150c219666293224500829a0c4c33c3d1c8 [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 Lockwoodba83b012010-04-16 10:39:22 -040077 struct usb_ep *ep_in;
78 struct usb_ep *ep_out;
79 struct usb_ep *ep_intr;
80
81 int state;
82
Mike Lockwood1de4d4d2010-07-06 19:27:52 -040083 /* synchronize access to our device file */
Mike Lockwoodba83b012010-04-16 10:39:22 -040084 atomic_t open_excl;
Mike Lockwood491d4182010-11-08 10:41:31 -050085 /* to enforce only one ioctl at a time */
86 atomic_t ioctl_excl;
Mike Lockwoodba83b012010-04-16 10:39:22 -040087
88 struct list_head tx_idle;
Mike Lockwoodba3673b2011-05-01 20:36:19 -040089 struct list_head intr_idle;
Mike Lockwoodba83b012010-04-16 10:39:22 -040090
91 wait_queue_head_t read_wq;
92 wait_queue_head_t write_wq;
Mike Lockwoodba3673b2011-05-01 20:36:19 -040093 wait_queue_head_t intr_wq;
Mike Lockwoodba83b012010-04-16 10:39:22 -040094 struct usb_request *rx_req[RX_REQ_MAX];
95 int rx_done;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -040096
Mike Lockwoodce4022b2011-07-14 19:42:42 -040097 /* for processing MTP_SEND_FILE, MTP_RECEIVE_FILE and
98 * MTP_SEND_FILE_WITH_HEADER ioctls on a work queue
Mike Lockwood491d4182010-11-08 10:41:31 -050099 */
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;
Mike Lockwood3e800b62010-11-16 17:14:32 -0500105 int64_t xfer_file_length;
Mike Lockwoodce4022b2011-07-14 19:42:42 -0400106 unsigned xfer_send_header;
107 uint16_t xfer_command;
108 uint32_t xfer_transaction_id;
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 Lockwoodce4022b2011-07-14 19:42:42 -0400635 struct mtp_data_header *header;
Mike Lockwood491d4182010-11-08 10:41:31 -0500636 struct file *filp;
637 loff_t offset;
Mike Lockwood3e800b62010-11-16 17:14:32 -0500638 int64_t count;
Mike Lockwoodce4022b2011-07-14 19:42:42 -0400639 int xfer, ret, hdr_size;
Mike Lockwood76ac6552010-11-15 15:22:21 -0500640 int r = 0;
Mike Lockwood3e800b62010-11-16 17:14:32 -0500641 int sendZLP = 0;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400642
Mike Lockwood491d4182010-11-08 10:41:31 -0500643 /* read our parameters */
644 smp_rmb();
645 filp = dev->xfer_file;
646 offset = dev->xfer_file_offset;
Mike Lockwood76ac6552010-11-15 15:22:21 -0500647 count = dev->xfer_file_length;
Mike Lockwood491d4182010-11-08 10:41:31 -0500648
Mike Lockwood3e800b62010-11-16 17:14:32 -0500649 DBG(cdev, "send_file_work(%lld %lld)\n", offset, count);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400650
Mike Lockwoodce4022b2011-07-14 19:42:42 -0400651 if (dev->xfer_send_header) {
652 hdr_size = sizeof(struct mtp_data_header);
653 count += hdr_size;
654 } else {
655 hdr_size = 0;
656 }
657
Mike Lockwood3e800b62010-11-16 17:14:32 -0500658 /* we need to send a zero length packet to signal the end of transfer
Mike Lockwood16c08c22010-11-17 11:16:35 -0500659 * if the transfer size is aligned to a packet boundary.
Mike Lockwood3e800b62010-11-16 17:14:32 -0500660 */
Mike Lockwoodce4022b2011-07-14 19:42:42 -0400661 if ((count & (dev->ep_in->maxpacket - 1)) == 0) {
Mike Lockwood3e800b62010-11-16 17:14:32 -0500662 sendZLP = 1;
663 }
664
665 while (count > 0 || sendZLP) {
666 /* so we exit after sending ZLP */
667 if (count == 0)
668 sendZLP = 0;
669
Mike Lockwoodba83b012010-04-16 10:39:22 -0400670 /* get an idle tx request to use */
671 req = 0;
672 ret = wait_event_interruptible(dev->write_wq,
Benoit Gobyaab96812011-04-19 20:37:33 -0700673 (req = mtp_req_get(dev, &dev->tx_idle))
Mike Lockwoodba83b012010-04-16 10:39:22 -0400674 || dev->state != STATE_BUSY);
Mike Lockwood090cbc42011-02-07 11:51:07 -0500675 if (dev->state == STATE_CANCELED) {
676 r = -ECANCELED;
677 break;
678 }
Mike Lockwoodba83b012010-04-16 10:39:22 -0400679 if (!req) {
680 r = ret;
681 break;
682 }
683
Benoit Gobyaab96812011-04-19 20:37:33 -0700684 if (count > MTP_BULK_BUFFER_SIZE)
685 xfer = MTP_BULK_BUFFER_SIZE;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400686 else
687 xfer = count;
Mike Lockwoodce4022b2011-07-14 19:42:42 -0400688
689 if (hdr_size) {
690 /* prepend MTP data header */
691 header = (struct mtp_data_header *)req->buf;
692 header->length = __cpu_to_le32(count);
693 header->type = __cpu_to_le16(2); /* data packet */
694 header->command = __cpu_to_le16(dev->xfer_command);
695 header->transaction_id = __cpu_to_le32(dev->xfer_transaction_id);
696 }
697
698 ret = vfs_read(filp, req->buf + hdr_size, xfer - hdr_size, &offset);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400699 if (ret < 0) {
700 r = ret;
701 break;
702 }
Mike Lockwoodce4022b2011-07-14 19:42:42 -0400703 xfer = ret + hdr_size;
704 hdr_size = 0;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400705
706 req->length = xfer;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400707 ret = usb_ep_queue(dev->ep_in, req, GFP_KERNEL);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400708 if (ret < 0) {
Mike Lockwood491d4182010-11-08 10:41:31 -0500709 DBG(cdev, "send_file_work: xfer error %d\n", ret);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400710 dev->state = STATE_ERROR;
711 r = -EIO;
712 break;
713 }
714
715 count -= xfer;
716
717 /* zero this so we don't try to free it on error exit */
718 req = 0;
719 }
720
721 if (req)
Benoit Gobyaab96812011-04-19 20:37:33 -0700722 mtp_req_put(dev, &dev->tx_idle, req);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400723
Mike Lockwood491d4182010-11-08 10:41:31 -0500724 DBG(cdev, "send_file_work returning %d\n", r);
725 /* write the result */
726 dev->xfer_result = r;
727 smp_wmb();
Mike Lockwoodba83b012010-04-16 10:39:22 -0400728}
729
Mike Lockwood491d4182010-11-08 10:41:31 -0500730/* read from USB and write to a local file */
731static void receive_file_work(struct work_struct *data)
Mike Lockwoodba83b012010-04-16 10:39:22 -0400732{
Mike Lockwood491d4182010-11-08 10:41:31 -0500733 struct mtp_dev *dev = container_of(data, struct mtp_dev, receive_file_work);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400734 struct usb_composite_dev *cdev = dev->cdev;
735 struct usb_request *read_req = NULL, *write_req = NULL;
Mike Lockwood491d4182010-11-08 10:41:31 -0500736 struct file *filp;
737 loff_t offset;
Mike Lockwood3e800b62010-11-16 17:14:32 -0500738 int64_t count;
Mike Lockwood76ac6552010-11-15 15:22:21 -0500739 int ret, cur_buf = 0;
740 int r = 0;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400741
Mike Lockwood491d4182010-11-08 10:41:31 -0500742 /* read our parameters */
743 smp_rmb();
744 filp = dev->xfer_file;
745 offset = dev->xfer_file_offset;
Mike Lockwood76ac6552010-11-15 15:22:21 -0500746 count = dev->xfer_file_length;
Mike Lockwood491d4182010-11-08 10:41:31 -0500747
Mike Lockwood3e800b62010-11-16 17:14:32 -0500748 DBG(cdev, "receive_file_work(%lld)\n", count);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400749
750 while (count > 0 || write_req) {
751 if (count > 0) {
752 /* queue a request */
753 read_req = dev->rx_req[cur_buf];
754 cur_buf = (cur_buf + 1) % RX_REQ_MAX;
755
Benoit Gobyaab96812011-04-19 20:37:33 -0700756 read_req->length = (count > MTP_BULK_BUFFER_SIZE
757 ? MTP_BULK_BUFFER_SIZE : count);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400758 dev->rx_done = 0;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400759 ret = usb_ep_queue(dev->ep_out, read_req, GFP_KERNEL);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400760 if (ret < 0) {
761 r = -EIO;
762 dev->state = STATE_ERROR;
763 break;
764 }
Mike Lockwoodba83b012010-04-16 10:39:22 -0400765 }
766
767 if (write_req) {
768 DBG(cdev, "rx %p %d\n", write_req, write_req->actual);
769 ret = vfs_write(filp, write_req->buf, write_req->actual,
770 &offset);
771 DBG(cdev, "vfs_write %d\n", ret);
772 if (ret != write_req->actual) {
773 r = -EIO;
774 dev->state = STATE_ERROR;
775 break;
776 }
777 write_req = NULL;
778 }
779
780 if (read_req) {
781 /* wait for our last read to complete */
782 ret = wait_event_interruptible(dev->read_wq,
783 dev->rx_done || dev->state != STATE_BUSY);
Mike Lockwood50fe49a2011-01-13 16:19:57 -0500784 if (dev->state == STATE_CANCELED) {
785 r = -ECANCELED;
786 if (!dev->rx_done)
787 usb_ep_dequeue(dev->ep_out, read_req);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400788 break;
789 }
Mike Lockwood3e800b62010-11-16 17:14:32 -0500790 /* if xfer_file_length is 0xFFFFFFFF, then we read until
791 * we get a zero length packet
792 */
793 if (count != 0xFFFFFFFF)
794 count -= read_req->actual;
795 if (read_req->actual < read_req->length) {
796 /* short packet is used to signal EOF for sizes > 4 gig */
797 DBG(cdev, "got short packet\n");
798 count = 0;
799 }
800
Mike Lockwoodba83b012010-04-16 10:39:22 -0400801 write_req = read_req;
802 read_req = NULL;
803 }
804 }
805
Mike Lockwood491d4182010-11-08 10:41:31 -0500806 DBG(cdev, "receive_file_work returning %d\n", r);
807 /* write the result */
808 dev->xfer_result = r;
809 smp_wmb();
Mike Lockwoodba83b012010-04-16 10:39:22 -0400810}
811
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400812static int mtp_send_event(struct mtp_dev *dev, struct mtp_event *event)
813{
Mike Lockwoodba3673b2011-05-01 20:36:19 -0400814 struct usb_request *req= NULL;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400815 int ret;
816 int length = event->length;
817
818 DBG(dev->cdev, "mtp_send_event(%d)\n", event->length);
819
820 if (length < 0 || length > INTR_BUFFER_SIZE)
821 return -EINVAL;
Mike Lockwood491d4182010-11-08 10:41:31 -0500822 if (dev->state == STATE_OFFLINE)
823 return -ENODEV;
Mike Lockwood292b9632011-02-10 11:54:53 -0500824
Mike Lockwoodba3673b2011-05-01 20:36:19 -0400825 ret = wait_event_interruptible_timeout(dev->intr_wq,
826 (req = mtp_req_get(dev, &dev->intr_idle)), msecs_to_jiffies(1000));
827 if (!req)
828 return -ETIME;
829
830 if (copy_from_user(req->buf, (void __user *)event->data, length)) {
831 mtp_req_put(dev, &dev->intr_idle, req);
Mike Lockwood491d4182010-11-08 10:41:31 -0500832 return -EFAULT;
Mike Lockwoodba3673b2011-05-01 20:36:19 -0400833 }
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400834 req->length = length;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400835 ret = usb_ep_queue(dev->ep_intr, req, GFP_KERNEL);
836 if (ret)
Mike Lockwoodba3673b2011-05-01 20:36:19 -0400837 mtp_req_put(dev, &dev->intr_idle, req);
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400838
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400839 return ret;
840}
841
Mike Lockwoodba83b012010-04-16 10:39:22 -0400842static long mtp_ioctl(struct file *fp, unsigned code, unsigned long value)
843{
844 struct mtp_dev *dev = fp->private_data;
845 struct file *filp = NULL;
846 int ret = -EINVAL;
847
Benoit Gobyaab96812011-04-19 20:37:33 -0700848 if (mtp_lock(&dev->ioctl_excl))
Mike Lockwood491d4182010-11-08 10:41:31 -0500849 return -EBUSY;
850
Mike Lockwoodba83b012010-04-16 10:39:22 -0400851 switch (code) {
852 case MTP_SEND_FILE:
853 case MTP_RECEIVE_FILE:
Mike Lockwoodce4022b2011-07-14 19:42:42 -0400854 case MTP_SEND_FILE_WITH_HEADER:
Mike Lockwoodba83b012010-04-16 10:39:22 -0400855 {
856 struct mtp_file_range mfr;
Mike Lockwood491d4182010-11-08 10:41:31 -0500857 struct work_struct *work;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400858
859 spin_lock_irq(&dev->lock);
860 if (dev->state == STATE_CANCELED) {
861 /* report cancelation to userspace */
862 dev->state = STATE_READY;
863 spin_unlock_irq(&dev->lock);
Mike Lockwood491d4182010-11-08 10:41:31 -0500864 ret = -ECANCELED;
865 goto out;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400866 }
867 if (dev->state == STATE_OFFLINE) {
868 spin_unlock_irq(&dev->lock);
Mike Lockwood491d4182010-11-08 10:41:31 -0500869 ret = -ENODEV;
870 goto out;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400871 }
872 dev->state = STATE_BUSY;
873 spin_unlock_irq(&dev->lock);
874
875 if (copy_from_user(&mfr, (void __user *)value, sizeof(mfr))) {
876 ret = -EFAULT;
877 goto fail;
878 }
Mike Lockwood491d4182010-11-08 10:41:31 -0500879 /* hold a reference to the file while we are working with it */
Mike Lockwoodba83b012010-04-16 10:39:22 -0400880 filp = fget(mfr.fd);
881 if (!filp) {
882 ret = -EBADF;
883 goto fail;
884 }
885
Mike Lockwood491d4182010-11-08 10:41:31 -0500886 /* write the parameters */
887 dev->xfer_file = filp;
888 dev->xfer_file_offset = mfr.offset;
889 dev->xfer_file_length = mfr.length;
890 smp_wmb();
Mike Lockwoodba83b012010-04-16 10:39:22 -0400891
Mike Lockwoodce4022b2011-07-14 19:42:42 -0400892 if (code == MTP_SEND_FILE_WITH_HEADER) {
Mike Lockwood491d4182010-11-08 10:41:31 -0500893 work = &dev->send_file_work;
Mike Lockwoodce4022b2011-07-14 19:42:42 -0400894 dev->xfer_send_header = 1;
895 dev->xfer_command = mfr.command;
896 dev->xfer_transaction_id = mfr.transaction_id;
897 } else if (code == MTP_SEND_FILE) {
898 work = &dev->send_file_work;
899 dev->xfer_send_header = 0;
900 } else {
Mike Lockwood491d4182010-11-08 10:41:31 -0500901 work = &dev->receive_file_work;
Mike Lockwoodce4022b2011-07-14 19:42:42 -0400902 }
Mike Lockwoodba83b012010-04-16 10:39:22 -0400903
Mike Lockwood491d4182010-11-08 10:41:31 -0500904 /* We do the file transfer on a work queue so it will run
905 * in kernel context, which is necessary for vfs_read and
906 * vfs_write to use our buffers in the kernel address space.
907 */
908 queue_work(dev->wq, work);
909 /* wait for operation to complete */
910 flush_workqueue(dev->wq);
911 fput(filp);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400912
Mike Lockwood491d4182010-11-08 10:41:31 -0500913 /* read the result */
914 smp_rmb();
915 ret = dev->xfer_result;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400916 break;
917 }
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400918 case MTP_SEND_EVENT:
919 {
920 struct mtp_event event;
921 /* return here so we don't change dev->state below,
922 * which would interfere with bulk transfer state.
923 */
924 if (copy_from_user(&event, (void __user *)value, sizeof(event)))
Mike Lockwood491d4182010-11-08 10:41:31 -0500925 ret = -EFAULT;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400926 else
Mike Lockwood491d4182010-11-08 10:41:31 -0500927 ret = mtp_send_event(dev, &event);
928 goto out;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400929 }
Mike Lockwoodba83b012010-04-16 10:39:22 -0400930 }
931
932fail:
Mike Lockwoodba83b012010-04-16 10:39:22 -0400933 spin_lock_irq(&dev->lock);
934 if (dev->state == STATE_CANCELED)
935 ret = -ECANCELED;
936 else if (dev->state != STATE_OFFLINE)
937 dev->state = STATE_READY;
938 spin_unlock_irq(&dev->lock);
Mike Lockwood491d4182010-11-08 10:41:31 -0500939out:
Benoit Gobyaab96812011-04-19 20:37:33 -0700940 mtp_unlock(&dev->ioctl_excl);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400941 DBG(dev->cdev, "ioctl returning %d\n", ret);
942 return ret;
943}
944
945static int mtp_open(struct inode *ip, struct file *fp)
946{
947 printk(KERN_INFO "mtp_open\n");
Benoit Gobyaab96812011-04-19 20:37:33 -0700948 if (mtp_lock(&_mtp_dev->open_excl))
Mike Lockwoodba83b012010-04-16 10:39:22 -0400949 return -EBUSY;
950
Mike Lockwoodba83b012010-04-16 10:39:22 -0400951 /* clear any error condition */
952 if (_mtp_dev->state != STATE_OFFLINE)
953 _mtp_dev->state = STATE_READY;
954
955 fp->private_data = _mtp_dev;
956 return 0;
957}
958
959static int mtp_release(struct inode *ip, struct file *fp)
960{
961 printk(KERN_INFO "mtp_release\n");
962
Benoit Gobyaab96812011-04-19 20:37:33 -0700963 mtp_unlock(&_mtp_dev->open_excl);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400964 return 0;
965}
966
967/* file operations for /dev/mtp_usb */
968static const struct file_operations mtp_fops = {
969 .owner = THIS_MODULE,
970 .read = mtp_read,
971 .write = mtp_write,
972 .unlocked_ioctl = mtp_ioctl,
973 .open = mtp_open,
974 .release = mtp_release,
975};
976
977static struct miscdevice mtp_device = {
978 .minor = MISC_DYNAMIC_MINOR,
Benoit Gobyaab96812011-04-19 20:37:33 -0700979 .name = mtp_shortname,
Mike Lockwoodba83b012010-04-16 10:39:22 -0400980 .fops = &mtp_fops,
981};
982
Benoit Gobyaab96812011-04-19 20:37:33 -0700983static int mtp_ctrlrequest(struct usb_composite_dev *cdev,
984 const struct usb_ctrlrequest *ctrl)
985{
Mike Lockwoodd74348c2011-07-20 17:08:28 -0700986 struct mtp_dev *dev = _mtp_dev;
Benoit Gobyaab96812011-04-19 20:37:33 -0700987 int value = -EOPNOTSUPP;
988 u16 w_index = le16_to_cpu(ctrl->wIndex);
989 u16 w_value = le16_to_cpu(ctrl->wValue);
990 u16 w_length = le16_to_cpu(ctrl->wLength);
Mike Lockwoodd74348c2011-07-20 17:08:28 -0700991 unsigned long flags;
Benoit Gobyaab96812011-04-19 20:37:33 -0700992
993 VDBG(cdev, "mtp_ctrlrequest "
994 "%02x.%02x v%04x i%04x l%u\n",
995 ctrl->bRequestType, ctrl->bRequest,
996 w_value, w_index, w_length);
997
998 /* Handle MTP OS string */
Mike Lockwoodcf7addf2011-06-01 22:17:36 -0400999 if (ctrl->bRequestType ==
Benoit Gobyaab96812011-04-19 20:37:33 -07001000 (USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE)
1001 && ctrl->bRequest == USB_REQ_GET_DESCRIPTOR
1002 && (w_value >> 8) == USB_DT_STRING
1003 && (w_value & 0xFF) == MTP_OS_STRING_ID) {
1004 value = (w_length < sizeof(mtp_os_string)
1005 ? w_length : sizeof(mtp_os_string));
1006 memcpy(cdev->req->buf, mtp_os_string, value);
Mike Lockwoodd74348c2011-07-20 17:08:28 -07001007 } else if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_VENDOR) {
1008 /* Handle MTP OS descriptor */
1009 DBG(cdev, "vendor request: %d index: %d value: %d length: %d\n",
1010 ctrl->bRequest, w_index, w_value, w_length);
1011
1012 if (ctrl->bRequest == 1
1013 && (ctrl->bRequestType & USB_DIR_IN)
1014 && (w_index == 4 || w_index == 5)) {
1015 value = (w_length < sizeof(mtp_ext_config_desc) ?
1016 w_length : sizeof(mtp_ext_config_desc));
1017 memcpy(cdev->req->buf, &mtp_ext_config_desc, value);
1018 }
1019 } else if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_CLASS) {
1020 DBG(cdev, "class request: %d index: %d value: %d length: %d\n",
1021 ctrl->bRequest, w_index, w_value, w_length);
1022
1023 if (ctrl->bRequest == MTP_REQ_CANCEL && w_index == 0
1024 && w_value == 0) {
1025 DBG(cdev, "MTP_REQ_CANCEL\n");
1026
1027 spin_lock_irqsave(&dev->lock, flags);
1028 if (dev->state == STATE_BUSY) {
1029 dev->state = STATE_CANCELED;
1030 wake_up(&dev->read_wq);
1031 wake_up(&dev->write_wq);
1032 }
1033 spin_unlock_irqrestore(&dev->lock, flags);
1034
1035 /* We need to queue a request to read the remaining
1036 * bytes, but we don't actually need to look at
1037 * the contents.
1038 */
1039 value = w_length;
1040 } else if (ctrl->bRequest == MTP_REQ_GET_DEVICE_STATUS
1041 && w_index == 0 && w_value == 0) {
1042 struct mtp_device_status *status = cdev->req->buf;
1043 status->wLength =
1044 __constant_cpu_to_le16(sizeof(*status));
1045
1046 DBG(cdev, "MTP_REQ_GET_DEVICE_STATUS\n");
1047 spin_lock_irqsave(&dev->lock, flags);
1048 /* device status is "busy" until we report
1049 * the cancelation to userspace
1050 */
1051 if (dev->state == STATE_CANCELED)
1052 status->wCode =
1053 __cpu_to_le16(MTP_RESPONSE_DEVICE_BUSY);
1054 else
1055 status->wCode =
1056 __cpu_to_le16(MTP_RESPONSE_OK);
1057 spin_unlock_irqrestore(&dev->lock, flags);
1058 value = sizeof(*status);
1059 }
Benoit Gobyaab96812011-04-19 20:37:33 -07001060 }
Mike Lockwoodd74348c2011-07-20 17:08:28 -07001061
Benoit Gobyaab96812011-04-19 20:37:33 -07001062 /* respond with data transfer or status phase? */
1063 if (value >= 0) {
1064 int rc;
1065 cdev->req->zero = value < w_length;
1066 cdev->req->length = value;
1067 rc = usb_ep_queue(cdev->gadget->ep0, cdev->req, GFP_ATOMIC);
1068 if (rc < 0)
1069 ERROR(cdev, "%s setup response queue error\n", __func__);
1070 }
1071 return value;
1072}
1073
Mike Lockwoodba83b012010-04-16 10:39:22 -04001074static int
1075mtp_function_bind(struct usb_configuration *c, struct usb_function *f)
1076{
1077 struct usb_composite_dev *cdev = c->cdev;
Benoit Gobyaab96812011-04-19 20:37:33 -07001078 struct mtp_dev *dev = func_to_mtp(f);
Mike Lockwoodba83b012010-04-16 10:39:22 -04001079 int id;
1080 int ret;
1081
1082 dev->cdev = cdev;
1083 DBG(cdev, "mtp_function_bind dev: %p\n", dev);
1084
1085 /* allocate interface ID(s) */
1086 id = usb_interface_id(c, f);
1087 if (id < 0)
1088 return id;
1089 mtp_interface_desc.bInterfaceNumber = id;
1090
1091 /* allocate endpoints */
Benoit Gobyaab96812011-04-19 20:37:33 -07001092 ret = mtp_create_bulk_endpoints(dev, &mtp_fullspeed_in_desc,
Mike Lockwoodba83b012010-04-16 10:39:22 -04001093 &mtp_fullspeed_out_desc, &mtp_intr_desc);
1094 if (ret)
1095 return ret;
1096
1097 /* support high speed hardware */
1098 if (gadget_is_dualspeed(c->cdev->gadget)) {
1099 mtp_highspeed_in_desc.bEndpointAddress =
1100 mtp_fullspeed_in_desc.bEndpointAddress;
1101 mtp_highspeed_out_desc.bEndpointAddress =
1102 mtp_fullspeed_out_desc.bEndpointAddress;
1103 }
1104
1105 DBG(cdev, "%s speed %s: IN/%s, OUT/%s\n",
1106 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
1107 f->name, dev->ep_in->name, dev->ep_out->name);
1108 return 0;
1109}
1110
1111static void
1112mtp_function_unbind(struct usb_configuration *c, struct usb_function *f)
1113{
Benoit Gobyaab96812011-04-19 20:37:33 -07001114 struct mtp_dev *dev = func_to_mtp(f);
Mike Lockwoodba83b012010-04-16 10:39:22 -04001115 struct usb_request *req;
1116 int i;
1117
Benoit Gobyaab96812011-04-19 20:37:33 -07001118 while ((req = mtp_req_get(dev, &dev->tx_idle)))
Mike Lockwoodba83b012010-04-16 10:39:22 -04001119 mtp_request_free(req, dev->ep_in);
1120 for (i = 0; i < RX_REQ_MAX; i++)
1121 mtp_request_free(dev->rx_req[i], dev->ep_out);
Mike Lockwoodba3673b2011-05-01 20:36:19 -04001122 while ((req = mtp_req_get(dev, &dev->intr_idle)))
1123 mtp_request_free(req, dev->ep_intr);
Mike Lockwoodba83b012010-04-16 10:39:22 -04001124 dev->state = STATE_OFFLINE;
Mike Lockwoodba83b012010-04-16 10:39:22 -04001125}
1126
Mike Lockwoodba83b012010-04-16 10:39:22 -04001127static int mtp_function_set_alt(struct usb_function *f,
1128 unsigned intf, unsigned alt)
1129{
Benoit Gobyaab96812011-04-19 20:37:33 -07001130 struct mtp_dev *dev = func_to_mtp(f);
Mike Lockwoodba83b012010-04-16 10:39:22 -04001131 struct usb_composite_dev *cdev = f->config->cdev;
1132 int ret;
1133
1134 DBG(cdev, "mtp_function_set_alt intf: %d alt: %d\n", intf, alt);
Tatyana Brokhman9168a352011-06-28 16:33:48 +03001135
Tatyana Brokhmanebd3f392011-06-28 16:33:50 +03001136 ret = config_ep_by_speed(cdev->gadget, f, dev->ep_in);
Mike Lockwoodba83b012010-04-16 10:39:22 -04001137 if (ret) {
Tatyana Brokhmanebd3f392011-06-28 16:33:50 +03001138 dev->ep_in->desc = NULL;
1139 ERROR(cdev, "config_ep_by_speed failes for ep %s, result %d\n",
1140 dev->ep_in->name, ret);
1141 return ret;
1142 }
1143 ret = usb_ep_enable(dev->ep_in);
1144 if (ret) {
1145 ERROR(cdev, "failed to enable ep %s, result %d\n",
1146 dev->ep_in->name, ret);
1147 return ret;
1148 }
1149
1150 ret = config_ep_by_speed(cdev->gadget, f, dev->ep_out);
1151 if (ret) {
1152 dev->ep_out->desc = NULL;
1153 ERROR(cdev, "config_ep_by_speed failes for ep %s, result %d\n",
1154 dev->ep_out->name, ret);
1155 usb_ep_disable(dev->ep_in);
1156 return ret;
1157 }
1158 ret = usb_ep_enable(dev->ep_out);
1159 if (ret) {
1160 ERROR(cdev, "failed to enable ep %s, result %d\n",
1161 dev->ep_out->name, ret);
Mike Lockwoodba83b012010-04-16 10:39:22 -04001162 usb_ep_disable(dev->ep_in);
1163 return ret;
1164 }
Tatyana Brokhman9168a352011-06-28 16:33:48 +03001165 dev->ep_intr->desc = &mtp_intr_desc;
1166 ret = usb_ep_enable(dev->ep_intr);
Mike Lockwoodba83b012010-04-16 10:39:22 -04001167 if (ret) {
1168 usb_ep_disable(dev->ep_out);
1169 usb_ep_disable(dev->ep_in);
1170 return ret;
1171 }
1172 dev->state = STATE_READY;
1173
1174 /* readers may be blocked waiting for us to go online */
1175 wake_up(&dev->read_wq);
1176 return 0;
1177}
1178
1179static void mtp_function_disable(struct usb_function *f)
1180{
Benoit Gobyaab96812011-04-19 20:37:33 -07001181 struct mtp_dev *dev = func_to_mtp(f);
Mike Lockwoodba83b012010-04-16 10:39:22 -04001182 struct usb_composite_dev *cdev = dev->cdev;
1183
1184 DBG(cdev, "mtp_function_disable\n");
1185 dev->state = STATE_OFFLINE;
1186 usb_ep_disable(dev->ep_in);
1187 usb_ep_disable(dev->ep_out);
1188 usb_ep_disable(dev->ep_intr);
1189
1190 /* readers may be blocked waiting for us to go online */
1191 wake_up(&dev->read_wq);
1192
1193 VDBG(cdev, "%s disabled\n", dev->function.name);
1194}
1195
Mike Lockwoodcf7addf2011-06-01 22:17:36 -04001196static int mtp_bind_config(struct usb_configuration *c, bool ptp_config)
Mike Lockwoodba83b012010-04-16 10:39:22 -04001197{
Benoit Gobyaab96812011-04-19 20:37:33 -07001198 struct mtp_dev *dev = _mtp_dev;
Mike Lockwood090cbc42011-02-07 11:51:07 -05001199 int ret = 0;
Mike Lockwoodba83b012010-04-16 10:39:22 -04001200
1201 printk(KERN_INFO "mtp_bind_config\n");
1202
Mike Lockwoodba83b012010-04-16 10:39:22 -04001203 /* allocate a string ID for our interface */
1204 if (mtp_string_defs[INTERFACE_STRING_INDEX].id == 0) {
1205 ret = usb_string_id(c->cdev);
1206 if (ret < 0)
1207 return ret;
1208 mtp_string_defs[INTERFACE_STRING_INDEX].id = ret;
1209 mtp_interface_desc.iInterface = ret;
1210 }
1211
Mike Lockwoodba83b012010-04-16 10:39:22 -04001212 dev->cdev = c->cdev;
1213 dev->function.name = "mtp";
Mike Lockwoodcf7addf2011-06-01 22:17:36 -04001214 dev->function.strings = mtp_strings;
1215 if (ptp_config) {
1216 dev->function.descriptors = fs_ptp_descs;
1217 dev->function.hs_descriptors = hs_ptp_descs;
1218 } else {
1219 dev->function.descriptors = fs_mtp_descs;
1220 dev->function.hs_descriptors = hs_mtp_descs;
1221 }
Mike Lockwoodba83b012010-04-16 10:39:22 -04001222 dev->function.bind = mtp_function_bind;
1223 dev->function.unbind = mtp_function_unbind;
Mike Lockwoodba83b012010-04-16 10:39:22 -04001224 dev->function.set_alt = mtp_function_set_alt;
1225 dev->function.disable = mtp_function_disable;
1226
Benoit Gobyaab96812011-04-19 20:37:33 -07001227 return usb_add_function(c, &dev->function);
1228}
1229
1230static int mtp_setup(void)
1231{
1232 struct mtp_dev *dev;
1233 int ret;
1234
1235 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1236 if (!dev)
1237 return -ENOMEM;
1238
1239 spin_lock_init(&dev->lock);
1240 init_waitqueue_head(&dev->read_wq);
1241 init_waitqueue_head(&dev->write_wq);
Mike Lockwoodba3673b2011-05-01 20:36:19 -04001242 init_waitqueue_head(&dev->intr_wq);
Benoit Gobyaab96812011-04-19 20:37:33 -07001243 atomic_set(&dev->open_excl, 0);
1244 atomic_set(&dev->ioctl_excl, 0);
1245 INIT_LIST_HEAD(&dev->tx_idle);
Mike Lockwoodba3673b2011-05-01 20:36:19 -04001246 INIT_LIST_HEAD(&dev->intr_idle);
Benoit Gobyaab96812011-04-19 20:37:33 -07001247
1248 dev->wq = create_singlethread_workqueue("f_mtp");
1249 if (!dev->wq) {
1250 ret = -ENOMEM;
1251 goto err1;
1252 }
1253 INIT_WORK(&dev->send_file_work, send_file_work);
1254 INIT_WORK(&dev->receive_file_work, receive_file_work);
1255
Mike Lockwoodba83b012010-04-16 10:39:22 -04001256 _mtp_dev = dev;
1257
1258 ret = misc_register(&mtp_device);
1259 if (ret)
Mike Lockwoodba83b012010-04-16 10:39:22 -04001260 goto err2;
1261
1262 return 0;
1263
1264err2:
Benoit Gobyaab96812011-04-19 20:37:33 -07001265 destroy_workqueue(dev->wq);
Mike Lockwoodba83b012010-04-16 10:39:22 -04001266err1:
Benoit Gobyaab96812011-04-19 20:37:33 -07001267 _mtp_dev = NULL;
Mike Lockwoodba83b012010-04-16 10:39:22 -04001268 kfree(dev);
1269 printk(KERN_ERR "mtp gadget driver failed to initialize\n");
1270 return ret;
1271}
1272
Benoit Gobyaab96812011-04-19 20:37:33 -07001273static void mtp_cleanup(void)
Mike Lockwoodba83b012010-04-16 10:39:22 -04001274{
Benoit Gobyaab96812011-04-19 20:37:33 -07001275 struct mtp_dev *dev = _mtp_dev;
1276
1277 if (!dev)
1278 return;
1279
1280 misc_deregister(&mtp_device);
1281 destroy_workqueue(dev->wq);
1282 _mtp_dev = NULL;
1283 kfree(dev);
Mike Lockwoodba83b012010-04-16 10:39:22 -04001284}