blob: 87244e91014aca3924899edfa8a3c3f5520882d1 [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
Mike Lockwoodba83b012010-04-16 10:39:22 -0400413 ep = usb_ep_autoconfig(cdev->gadget, intr_desc);
414 if (!ep) {
415 DBG(cdev, "usb_ep_autoconfig for ep_intr failed\n");
416 return -ENODEV;
417 }
418 DBG(cdev, "usb_ep_autoconfig for mtp ep_intr got %s\n", ep->name);
419 ep->driver_data = dev; /* claim the endpoint */
420 dev->ep_intr = ep;
421
422 /* now allocate requests for our endpoints */
423 for (i = 0; i < TX_REQ_MAX; i++) {
Benoit Gobyaab96812011-04-19 20:37:33 -0700424 req = mtp_request_new(dev->ep_in, MTP_BULK_BUFFER_SIZE);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400425 if (!req)
426 goto fail;
427 req->complete = mtp_complete_in;
Benoit Gobyaab96812011-04-19 20:37:33 -0700428 mtp_req_put(dev, &dev->tx_idle, req);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400429 }
430 for (i = 0; i < RX_REQ_MAX; i++) {
Benoit Gobyaab96812011-04-19 20:37:33 -0700431 req = mtp_request_new(dev->ep_out, MTP_BULK_BUFFER_SIZE);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400432 if (!req)
433 goto fail;
434 req->complete = mtp_complete_out;
435 dev->rx_req[i] = req;
436 }
Mike Lockwoodba3673b2011-05-01 20:36:19 -0400437 for (i = 0; i < INTR_REQ_MAX; i++) {
438 req = mtp_request_new(dev->ep_intr, INTR_BUFFER_SIZE);
439 if (!req)
440 goto fail;
441 req->complete = mtp_complete_intr;
442 mtp_req_put(dev, &dev->intr_idle, req);
443 }
Mike Lockwoodba83b012010-04-16 10:39:22 -0400444
445 return 0;
446
447fail:
448 printk(KERN_ERR "mtp_bind() could not allocate requests\n");
449 return -1;
450}
451
452static ssize_t mtp_read(struct file *fp, char __user *buf,
453 size_t count, loff_t *pos)
454{
455 struct mtp_dev *dev = fp->private_data;
456 struct usb_composite_dev *cdev = dev->cdev;
457 struct usb_request *req;
458 int r = count, xfer;
459 int ret = 0;
460
461 DBG(cdev, "mtp_read(%d)\n", count);
462
Benoit Gobyaab96812011-04-19 20:37:33 -0700463 if (count > MTP_BULK_BUFFER_SIZE)
Mike Lockwoodba83b012010-04-16 10:39:22 -0400464 return -EINVAL;
465
466 /* we will block until we're online */
467 DBG(cdev, "mtp_read: waiting for online state\n");
468 ret = wait_event_interruptible(dev->read_wq,
469 dev->state != STATE_OFFLINE);
470 if (ret < 0) {
471 r = ret;
472 goto done;
473 }
474 spin_lock_irq(&dev->lock);
475 if (dev->state == STATE_CANCELED) {
476 /* report cancelation to userspace */
477 dev->state = STATE_READY;
478 spin_unlock_irq(&dev->lock);
479 return -ECANCELED;
480 }
481 dev->state = STATE_BUSY;
482 spin_unlock_irq(&dev->lock);
483
484requeue_req:
485 /* queue a request */
486 req = dev->rx_req[0];
487 req->length = count;
488 dev->rx_done = 0;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400489 ret = usb_ep_queue(dev->ep_out, req, GFP_KERNEL);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400490 if (ret < 0) {
491 r = -EIO;
492 goto done;
493 } else {
494 DBG(cdev, "rx %p queue\n", req);
495 }
496
497 /* wait for a request to complete */
Rajkumar Raghupathy20298e02012-03-09 12:22:36 +0530498 ret = wait_event_interruptible(dev->read_wq,
499 dev->rx_done || dev->state != STATE_BUSY);
500 if (dev->state == STATE_CANCELED) {
501 r = -ECANCELED;
502 if (!dev->rx_done)
503 usb_ep_dequeue(dev->ep_out, req);
504 spin_lock_irq(&dev->lock);
505 dev->state = STATE_CANCELED;
506 spin_unlock_irq(&dev->lock);
507 goto done;
508 }
Mike Lockwoodba83b012010-04-16 10:39:22 -0400509 if (ret < 0) {
510 r = ret;
Mike Lockwood43f3dc82011-02-19 15:33:17 -0500511 usb_ep_dequeue(dev->ep_out, req);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400512 goto done;
513 }
514 if (dev->state == STATE_BUSY) {
515 /* If we got a 0-len packet, throw it back and try again. */
516 if (req->actual == 0)
517 goto requeue_req;
518
519 DBG(cdev, "rx %p %d\n", req, req->actual);
520 xfer = (req->actual < count) ? req->actual : count;
521 r = xfer;
522 if (copy_to_user(buf, req->buf, xfer))
523 r = -EFAULT;
524 } else
525 r = -EIO;
526
527done:
528 spin_lock_irq(&dev->lock);
529 if (dev->state == STATE_CANCELED)
530 r = -ECANCELED;
531 else if (dev->state != STATE_OFFLINE)
532 dev->state = STATE_READY;
533 spin_unlock_irq(&dev->lock);
534
535 DBG(cdev, "mtp_read returning %d\n", r);
536 return r;
537}
538
539static ssize_t mtp_write(struct file *fp, const char __user *buf,
540 size_t count, loff_t *pos)
541{
542 struct mtp_dev *dev = fp->private_data;
543 struct usb_composite_dev *cdev = dev->cdev;
544 struct usb_request *req = 0;
545 int r = count, xfer;
Mike Lockwood16c08c22010-11-17 11:16:35 -0500546 int sendZLP = 0;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400547 int ret;
548
549 DBG(cdev, "mtp_write(%d)\n", count);
550
551 spin_lock_irq(&dev->lock);
552 if (dev->state == STATE_CANCELED) {
553 /* report cancelation to userspace */
554 dev->state = STATE_READY;
555 spin_unlock_irq(&dev->lock);
556 return -ECANCELED;
557 }
558 if (dev->state == STATE_OFFLINE) {
559 spin_unlock_irq(&dev->lock);
560 return -ENODEV;
561 }
562 dev->state = STATE_BUSY;
563 spin_unlock_irq(&dev->lock);
564
Mike Lockwood16c08c22010-11-17 11:16:35 -0500565 /* we need to send a zero length packet to signal the end of transfer
566 * if the transfer size is aligned to a packet boundary.
567 */
568 if ((count & (dev->ep_in->maxpacket - 1)) == 0) {
569 sendZLP = 1;
570 }
571
572 while (count > 0 || sendZLP) {
573 /* so we exit after sending ZLP */
574 if (count == 0)
575 sendZLP = 0;
576
Mike Lockwoodba83b012010-04-16 10:39:22 -0400577 if (dev->state != STATE_BUSY) {
578 DBG(cdev, "mtp_write dev->error\n");
579 r = -EIO;
580 break;
581 }
582
583 /* get an idle tx request to use */
584 req = 0;
585 ret = wait_event_interruptible(dev->write_wq,
Benoit Gobyaab96812011-04-19 20:37:33 -0700586 ((req = mtp_req_get(dev, &dev->tx_idle))
Mike Lockwoodba83b012010-04-16 10:39:22 -0400587 || dev->state != STATE_BUSY));
588 if (!req) {
589 r = ret;
590 break;
591 }
592
Benoit Gobyaab96812011-04-19 20:37:33 -0700593 if (count > MTP_BULK_BUFFER_SIZE)
594 xfer = MTP_BULK_BUFFER_SIZE;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400595 else
596 xfer = count;
Mike Lockwood3e800b62010-11-16 17:14:32 -0500597 if (xfer && copy_from_user(req->buf, buf, xfer)) {
Mike Lockwoodba83b012010-04-16 10:39:22 -0400598 r = -EFAULT;
599 break;
600 }
601
602 req->length = xfer;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400603 ret = usb_ep_queue(dev->ep_in, req, GFP_KERNEL);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400604 if (ret < 0) {
605 DBG(cdev, "mtp_write: xfer error %d\n", ret);
606 r = -EIO;
607 break;
608 }
609
610 buf += xfer;
611 count -= xfer;
612
613 /* zero this so we don't try to free it on error exit */
614 req = 0;
Mike Lockwood16c08c22010-11-17 11:16:35 -0500615 }
Mike Lockwoodba83b012010-04-16 10:39:22 -0400616
617 if (req)
Benoit Gobyaab96812011-04-19 20:37:33 -0700618 mtp_req_put(dev, &dev->tx_idle, req);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400619
620 spin_lock_irq(&dev->lock);
621 if (dev->state == STATE_CANCELED)
622 r = -ECANCELED;
623 else if (dev->state != STATE_OFFLINE)
624 dev->state = STATE_READY;
625 spin_unlock_irq(&dev->lock);
626
627 DBG(cdev, "mtp_write returning %d\n", r);
628 return r;
629}
630
Mike Lockwood491d4182010-11-08 10:41:31 -0500631/* read from a local file and write to USB */
632static void send_file_work(struct work_struct *data) {
633 struct mtp_dev *dev = container_of(data, struct mtp_dev, send_file_work);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400634 struct usb_composite_dev *cdev = dev->cdev;
635 struct usb_request *req = 0;
Mike Lockwoodce4022b2011-07-14 19:42:42 -0400636 struct mtp_data_header *header;
Mike Lockwood491d4182010-11-08 10:41:31 -0500637 struct file *filp;
638 loff_t offset;
Mike Lockwood3e800b62010-11-16 17:14:32 -0500639 int64_t count;
Mike Lockwoodce4022b2011-07-14 19:42:42 -0400640 int xfer, ret, hdr_size;
Mike Lockwood76ac6552010-11-15 15:22:21 -0500641 int r = 0;
Mike Lockwood3e800b62010-11-16 17:14:32 -0500642 int sendZLP = 0;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400643
Mike Lockwood491d4182010-11-08 10:41:31 -0500644 /* read our parameters */
645 smp_rmb();
646 filp = dev->xfer_file;
647 offset = dev->xfer_file_offset;
Mike Lockwood76ac6552010-11-15 15:22:21 -0500648 count = dev->xfer_file_length;
Mike Lockwood491d4182010-11-08 10:41:31 -0500649
Mike Lockwood3e800b62010-11-16 17:14:32 -0500650 DBG(cdev, "send_file_work(%lld %lld)\n", offset, count);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400651
Mike Lockwoodce4022b2011-07-14 19:42:42 -0400652 if (dev->xfer_send_header) {
653 hdr_size = sizeof(struct mtp_data_header);
654 count += hdr_size;
655 } else {
656 hdr_size = 0;
657 }
658
Mike Lockwood3e800b62010-11-16 17:14:32 -0500659 /* we need to send a zero length packet to signal the end of transfer
Mike Lockwood16c08c22010-11-17 11:16:35 -0500660 * if the transfer size is aligned to a packet boundary.
Mike Lockwood3e800b62010-11-16 17:14:32 -0500661 */
Mike Lockwoodce4022b2011-07-14 19:42:42 -0400662 if ((count & (dev->ep_in->maxpacket - 1)) == 0) {
Mike Lockwood3e800b62010-11-16 17:14:32 -0500663 sendZLP = 1;
664 }
665
666 while (count > 0 || sendZLP) {
667 /* so we exit after sending ZLP */
668 if (count == 0)
669 sendZLP = 0;
670
Mike Lockwoodba83b012010-04-16 10:39:22 -0400671 /* get an idle tx request to use */
672 req = 0;
673 ret = wait_event_interruptible(dev->write_wq,
Benoit Gobyaab96812011-04-19 20:37:33 -0700674 (req = mtp_req_get(dev, &dev->tx_idle))
Mike Lockwoodba83b012010-04-16 10:39:22 -0400675 || dev->state != STATE_BUSY);
Mike Lockwood090cbc42011-02-07 11:51:07 -0500676 if (dev->state == STATE_CANCELED) {
677 r = -ECANCELED;
678 break;
679 }
Mike Lockwoodba83b012010-04-16 10:39:22 -0400680 if (!req) {
681 r = ret;
682 break;
683 }
684
Benoit Gobyaab96812011-04-19 20:37:33 -0700685 if (count > MTP_BULK_BUFFER_SIZE)
686 xfer = MTP_BULK_BUFFER_SIZE;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400687 else
688 xfer = count;
Mike Lockwoodce4022b2011-07-14 19:42:42 -0400689
690 if (hdr_size) {
691 /* prepend MTP data header */
692 header = (struct mtp_data_header *)req->buf;
693 header->length = __cpu_to_le32(count);
694 header->type = __cpu_to_le16(2); /* data packet */
695 header->command = __cpu_to_le16(dev->xfer_command);
696 header->transaction_id = __cpu_to_le32(dev->xfer_transaction_id);
697 }
698
699 ret = vfs_read(filp, req->buf + hdr_size, xfer - hdr_size, &offset);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400700 if (ret < 0) {
701 r = ret;
702 break;
703 }
Mike Lockwoodce4022b2011-07-14 19:42:42 -0400704 xfer = ret + hdr_size;
705 hdr_size = 0;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400706
707 req->length = xfer;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400708 ret = usb_ep_queue(dev->ep_in, req, GFP_KERNEL);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400709 if (ret < 0) {
Mike Lockwood491d4182010-11-08 10:41:31 -0500710 DBG(cdev, "send_file_work: xfer error %d\n", ret);
Vijayavardhan Vennapusa00972232012-05-18 11:18:40 +0530711 if (dev->state != STATE_OFFLINE)
712 dev->state = STATE_ERROR;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400713 r = -EIO;
714 break;
715 }
716
717 count -= xfer;
718
719 /* zero this so we don't try to free it on error exit */
720 req = 0;
721 }
722
723 if (req)
Benoit Gobyaab96812011-04-19 20:37:33 -0700724 mtp_req_put(dev, &dev->tx_idle, req);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400725
Mike Lockwood491d4182010-11-08 10:41:31 -0500726 DBG(cdev, "send_file_work returning %d\n", r);
727 /* write the result */
728 dev->xfer_result = r;
729 smp_wmb();
Mike Lockwoodba83b012010-04-16 10:39:22 -0400730}
731
Mike Lockwood491d4182010-11-08 10:41:31 -0500732/* read from USB and write to a local file */
733static void receive_file_work(struct work_struct *data)
Mike Lockwoodba83b012010-04-16 10:39:22 -0400734{
Mike Lockwood491d4182010-11-08 10:41:31 -0500735 struct mtp_dev *dev = container_of(data, struct mtp_dev, receive_file_work);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400736 struct usb_composite_dev *cdev = dev->cdev;
737 struct usb_request *read_req = NULL, *write_req = NULL;
Mike Lockwood491d4182010-11-08 10:41:31 -0500738 struct file *filp;
739 loff_t offset;
Mike Lockwood3e800b62010-11-16 17:14:32 -0500740 int64_t count;
Mike Lockwood76ac6552010-11-15 15:22:21 -0500741 int ret, cur_buf = 0;
742 int r = 0;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400743
Mike Lockwood491d4182010-11-08 10:41:31 -0500744 /* read our parameters */
745 smp_rmb();
746 filp = dev->xfer_file;
747 offset = dev->xfer_file_offset;
Mike Lockwood76ac6552010-11-15 15:22:21 -0500748 count = dev->xfer_file_length;
Mike Lockwood491d4182010-11-08 10:41:31 -0500749
Mike Lockwood3e800b62010-11-16 17:14:32 -0500750 DBG(cdev, "receive_file_work(%lld)\n", count);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400751
752 while (count > 0 || write_req) {
753 if (count > 0) {
754 /* queue a request */
755 read_req = dev->rx_req[cur_buf];
756 cur_buf = (cur_buf + 1) % RX_REQ_MAX;
757
Benoit Gobyaab96812011-04-19 20:37:33 -0700758 read_req->length = (count > MTP_BULK_BUFFER_SIZE
759 ? MTP_BULK_BUFFER_SIZE : count);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400760 dev->rx_done = 0;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400761 ret = usb_ep_queue(dev->ep_out, read_req, GFP_KERNEL);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400762 if (ret < 0) {
763 r = -EIO;
Vijayavardhan Vennapusa00972232012-05-18 11:18:40 +0530764 if (dev->state != STATE_OFFLINE)
765 dev->state = STATE_ERROR;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400766 break;
767 }
Mike Lockwoodba83b012010-04-16 10:39:22 -0400768 }
769
770 if (write_req) {
771 DBG(cdev, "rx %p %d\n", write_req, write_req->actual);
772 ret = vfs_write(filp, write_req->buf, write_req->actual,
773 &offset);
774 DBG(cdev, "vfs_write %d\n", ret);
775 if (ret != write_req->actual) {
776 r = -EIO;
Vijayavardhan Vennapusa00972232012-05-18 11:18:40 +0530777 if (dev->state != STATE_OFFLINE)
778 dev->state = STATE_ERROR;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400779 break;
780 }
781 write_req = NULL;
782 }
783
784 if (read_req) {
785 /* wait for our last read to complete */
786 ret = wait_event_interruptible(dev->read_wq,
787 dev->rx_done || dev->state != STATE_BUSY);
Mike Lockwood50fe49a2011-01-13 16:19:57 -0500788 if (dev->state == STATE_CANCELED) {
789 r = -ECANCELED;
790 if (!dev->rx_done)
791 usb_ep_dequeue(dev->ep_out, read_req);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400792 break;
793 }
Mike Lockwood3e800b62010-11-16 17:14:32 -0500794 /* if xfer_file_length is 0xFFFFFFFF, then we read until
795 * we get a zero length packet
796 */
797 if (count != 0xFFFFFFFF)
798 count -= read_req->actual;
799 if (read_req->actual < read_req->length) {
800 /* short packet is used to signal EOF for sizes > 4 gig */
801 DBG(cdev, "got short packet\n");
802 count = 0;
803 }
804
Mike Lockwoodba83b012010-04-16 10:39:22 -0400805 write_req = read_req;
806 read_req = NULL;
807 }
808 }
809
Mike Lockwood491d4182010-11-08 10:41:31 -0500810 DBG(cdev, "receive_file_work returning %d\n", r);
811 /* write the result */
812 dev->xfer_result = r;
813 smp_wmb();
Mike Lockwoodba83b012010-04-16 10:39:22 -0400814}
815
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400816static int mtp_send_event(struct mtp_dev *dev, struct mtp_event *event)
817{
Mike Lockwoodba3673b2011-05-01 20:36:19 -0400818 struct usb_request *req= NULL;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400819 int ret;
820 int length = event->length;
821
822 DBG(dev->cdev, "mtp_send_event(%d)\n", event->length);
823
824 if (length < 0 || length > INTR_BUFFER_SIZE)
825 return -EINVAL;
Mike Lockwood491d4182010-11-08 10:41:31 -0500826 if (dev->state == STATE_OFFLINE)
827 return -ENODEV;
Mike Lockwood292b9632011-02-10 11:54:53 -0500828
Mike Lockwoodba3673b2011-05-01 20:36:19 -0400829 ret = wait_event_interruptible_timeout(dev->intr_wq,
830 (req = mtp_req_get(dev, &dev->intr_idle)), msecs_to_jiffies(1000));
831 if (!req)
832 return -ETIME;
833
834 if (copy_from_user(req->buf, (void __user *)event->data, length)) {
835 mtp_req_put(dev, &dev->intr_idle, req);
Mike Lockwood491d4182010-11-08 10:41:31 -0500836 return -EFAULT;
Mike Lockwoodba3673b2011-05-01 20:36:19 -0400837 }
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400838 req->length = length;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400839 ret = usb_ep_queue(dev->ep_intr, req, GFP_KERNEL);
840 if (ret)
Mike Lockwoodba3673b2011-05-01 20:36:19 -0400841 mtp_req_put(dev, &dev->intr_idle, req);
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400842
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400843 return ret;
844}
845
Mike Lockwoodba83b012010-04-16 10:39:22 -0400846static long mtp_ioctl(struct file *fp, unsigned code, unsigned long value)
847{
848 struct mtp_dev *dev = fp->private_data;
849 struct file *filp = NULL;
850 int ret = -EINVAL;
851
Benoit Gobyaab96812011-04-19 20:37:33 -0700852 if (mtp_lock(&dev->ioctl_excl))
Mike Lockwood491d4182010-11-08 10:41:31 -0500853 return -EBUSY;
854
Mike Lockwoodba83b012010-04-16 10:39:22 -0400855 switch (code) {
856 case MTP_SEND_FILE:
857 case MTP_RECEIVE_FILE:
Mike Lockwoodce4022b2011-07-14 19:42:42 -0400858 case MTP_SEND_FILE_WITH_HEADER:
Mike Lockwoodba83b012010-04-16 10:39:22 -0400859 {
860 struct mtp_file_range mfr;
Mike Lockwood491d4182010-11-08 10:41:31 -0500861 struct work_struct *work;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400862
863 spin_lock_irq(&dev->lock);
864 if (dev->state == STATE_CANCELED) {
865 /* report cancelation to userspace */
866 dev->state = STATE_READY;
867 spin_unlock_irq(&dev->lock);
Mike Lockwood491d4182010-11-08 10:41:31 -0500868 ret = -ECANCELED;
869 goto out;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400870 }
871 if (dev->state == STATE_OFFLINE) {
872 spin_unlock_irq(&dev->lock);
Mike Lockwood491d4182010-11-08 10:41:31 -0500873 ret = -ENODEV;
874 goto out;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400875 }
876 dev->state = STATE_BUSY;
877 spin_unlock_irq(&dev->lock);
878
879 if (copy_from_user(&mfr, (void __user *)value, sizeof(mfr))) {
880 ret = -EFAULT;
881 goto fail;
882 }
Mike Lockwood491d4182010-11-08 10:41:31 -0500883 /* hold a reference to the file while we are working with it */
Mike Lockwoodba83b012010-04-16 10:39:22 -0400884 filp = fget(mfr.fd);
885 if (!filp) {
886 ret = -EBADF;
887 goto fail;
888 }
889
Mike Lockwood491d4182010-11-08 10:41:31 -0500890 /* write the parameters */
891 dev->xfer_file = filp;
892 dev->xfer_file_offset = mfr.offset;
893 dev->xfer_file_length = mfr.length;
894 smp_wmb();
Mike Lockwoodba83b012010-04-16 10:39:22 -0400895
Mike Lockwoodce4022b2011-07-14 19:42:42 -0400896 if (code == MTP_SEND_FILE_WITH_HEADER) {
Mike Lockwood491d4182010-11-08 10:41:31 -0500897 work = &dev->send_file_work;
Mike Lockwoodce4022b2011-07-14 19:42:42 -0400898 dev->xfer_send_header = 1;
899 dev->xfer_command = mfr.command;
900 dev->xfer_transaction_id = mfr.transaction_id;
901 } else if (code == MTP_SEND_FILE) {
902 work = &dev->send_file_work;
903 dev->xfer_send_header = 0;
904 } else {
Mike Lockwood491d4182010-11-08 10:41:31 -0500905 work = &dev->receive_file_work;
Mike Lockwoodce4022b2011-07-14 19:42:42 -0400906 }
Mike Lockwoodba83b012010-04-16 10:39:22 -0400907
Mike Lockwood491d4182010-11-08 10:41:31 -0500908 /* We do the file transfer on a work queue so it will run
909 * in kernel context, which is necessary for vfs_read and
910 * vfs_write to use our buffers in the kernel address space.
911 */
912 queue_work(dev->wq, work);
913 /* wait for operation to complete */
914 flush_workqueue(dev->wq);
915 fput(filp);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400916
Mike Lockwood491d4182010-11-08 10:41:31 -0500917 /* read the result */
918 smp_rmb();
919 ret = dev->xfer_result;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400920 break;
921 }
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400922 case MTP_SEND_EVENT:
923 {
924 struct mtp_event event;
925 /* return here so we don't change dev->state below,
926 * which would interfere with bulk transfer state.
927 */
928 if (copy_from_user(&event, (void __user *)value, sizeof(event)))
Mike Lockwood491d4182010-11-08 10:41:31 -0500929 ret = -EFAULT;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400930 else
Mike Lockwood491d4182010-11-08 10:41:31 -0500931 ret = mtp_send_event(dev, &event);
932 goto out;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400933 }
Mike Lockwoodba83b012010-04-16 10:39:22 -0400934 }
935
936fail:
Mike Lockwoodba83b012010-04-16 10:39:22 -0400937 spin_lock_irq(&dev->lock);
938 if (dev->state == STATE_CANCELED)
939 ret = -ECANCELED;
940 else if (dev->state != STATE_OFFLINE)
941 dev->state = STATE_READY;
942 spin_unlock_irq(&dev->lock);
Mike Lockwood491d4182010-11-08 10:41:31 -0500943out:
Benoit Gobyaab96812011-04-19 20:37:33 -0700944 mtp_unlock(&dev->ioctl_excl);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400945 DBG(dev->cdev, "ioctl returning %d\n", ret);
946 return ret;
947}
948
949static int mtp_open(struct inode *ip, struct file *fp)
950{
951 printk(KERN_INFO "mtp_open\n");
Benoit Gobyaab96812011-04-19 20:37:33 -0700952 if (mtp_lock(&_mtp_dev->open_excl))
Mike Lockwoodba83b012010-04-16 10:39:22 -0400953 return -EBUSY;
954
Mike Lockwoodba83b012010-04-16 10:39:22 -0400955 /* clear any error condition */
956 if (_mtp_dev->state != STATE_OFFLINE)
957 _mtp_dev->state = STATE_READY;
958
959 fp->private_data = _mtp_dev;
960 return 0;
961}
962
963static int mtp_release(struct inode *ip, struct file *fp)
964{
965 printk(KERN_INFO "mtp_release\n");
966
Benoit Gobyaab96812011-04-19 20:37:33 -0700967 mtp_unlock(&_mtp_dev->open_excl);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400968 return 0;
969}
970
971/* file operations for /dev/mtp_usb */
972static const struct file_operations mtp_fops = {
973 .owner = THIS_MODULE,
974 .read = mtp_read,
975 .write = mtp_write,
976 .unlocked_ioctl = mtp_ioctl,
977 .open = mtp_open,
978 .release = mtp_release,
979};
980
981static struct miscdevice mtp_device = {
982 .minor = MISC_DYNAMIC_MINOR,
Benoit Gobyaab96812011-04-19 20:37:33 -0700983 .name = mtp_shortname,
Mike Lockwoodba83b012010-04-16 10:39:22 -0400984 .fops = &mtp_fops,
985};
986
Benoit Gobyaab96812011-04-19 20:37:33 -0700987static int mtp_ctrlrequest(struct usb_composite_dev *cdev,
988 const struct usb_ctrlrequest *ctrl)
989{
Mike Lockwoodd74348c2011-07-20 17:08:28 -0700990 struct mtp_dev *dev = _mtp_dev;
Benoit Gobyaab96812011-04-19 20:37:33 -0700991 int value = -EOPNOTSUPP;
992 u16 w_index = le16_to_cpu(ctrl->wIndex);
993 u16 w_value = le16_to_cpu(ctrl->wValue);
994 u16 w_length = le16_to_cpu(ctrl->wLength);
Mike Lockwoodd74348c2011-07-20 17:08:28 -0700995 unsigned long flags;
Benoit Gobyaab96812011-04-19 20:37:33 -0700996
997 VDBG(cdev, "mtp_ctrlrequest "
998 "%02x.%02x v%04x i%04x l%u\n",
999 ctrl->bRequestType, ctrl->bRequest,
1000 w_value, w_index, w_length);
1001
1002 /* Handle MTP OS string */
Mike Lockwoodcf7addf2011-06-01 22:17:36 -04001003 if (ctrl->bRequestType ==
Benoit Gobyaab96812011-04-19 20:37:33 -07001004 (USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE)
1005 && ctrl->bRequest == USB_REQ_GET_DESCRIPTOR
1006 && (w_value >> 8) == USB_DT_STRING
1007 && (w_value & 0xFF) == MTP_OS_STRING_ID) {
1008 value = (w_length < sizeof(mtp_os_string)
1009 ? w_length : sizeof(mtp_os_string));
1010 memcpy(cdev->req->buf, mtp_os_string, value);
Mike Lockwoodd74348c2011-07-20 17:08:28 -07001011 } else if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_VENDOR) {
1012 /* Handle MTP OS descriptor */
1013 DBG(cdev, "vendor request: %d index: %d value: %d length: %d\n",
1014 ctrl->bRequest, w_index, w_value, w_length);
1015
1016 if (ctrl->bRequest == 1
1017 && (ctrl->bRequestType & USB_DIR_IN)
1018 && (w_index == 4 || w_index == 5)) {
1019 value = (w_length < sizeof(mtp_ext_config_desc) ?
1020 w_length : sizeof(mtp_ext_config_desc));
1021 memcpy(cdev->req->buf, &mtp_ext_config_desc, value);
1022 }
1023 } else if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_CLASS) {
1024 DBG(cdev, "class request: %d index: %d value: %d length: %d\n",
1025 ctrl->bRequest, w_index, w_value, w_length);
1026
1027 if (ctrl->bRequest == MTP_REQ_CANCEL && w_index == 0
1028 && w_value == 0) {
1029 DBG(cdev, "MTP_REQ_CANCEL\n");
1030
1031 spin_lock_irqsave(&dev->lock, flags);
1032 if (dev->state == STATE_BUSY) {
1033 dev->state = STATE_CANCELED;
1034 wake_up(&dev->read_wq);
1035 wake_up(&dev->write_wq);
1036 }
1037 spin_unlock_irqrestore(&dev->lock, flags);
1038
1039 /* We need to queue a request to read the remaining
1040 * bytes, but we don't actually need to look at
1041 * the contents.
1042 */
1043 value = w_length;
1044 } else if (ctrl->bRequest == MTP_REQ_GET_DEVICE_STATUS
1045 && w_index == 0 && w_value == 0) {
1046 struct mtp_device_status *status = cdev->req->buf;
1047 status->wLength =
1048 __constant_cpu_to_le16(sizeof(*status));
1049
1050 DBG(cdev, "MTP_REQ_GET_DEVICE_STATUS\n");
1051 spin_lock_irqsave(&dev->lock, flags);
1052 /* device status is "busy" until we report
1053 * the cancelation to userspace
1054 */
1055 if (dev->state == STATE_CANCELED)
1056 status->wCode =
1057 __cpu_to_le16(MTP_RESPONSE_DEVICE_BUSY);
1058 else
1059 status->wCode =
1060 __cpu_to_le16(MTP_RESPONSE_OK);
1061 spin_unlock_irqrestore(&dev->lock, flags);
1062 value = sizeof(*status);
1063 }
Benoit Gobyaab96812011-04-19 20:37:33 -07001064 }
Mike Lockwoodd74348c2011-07-20 17:08:28 -07001065
Benoit Gobyaab96812011-04-19 20:37:33 -07001066 /* respond with data transfer or status phase? */
1067 if (value >= 0) {
1068 int rc;
1069 cdev->req->zero = value < w_length;
1070 cdev->req->length = value;
1071 rc = usb_ep_queue(cdev->gadget->ep0, cdev->req, GFP_ATOMIC);
1072 if (rc < 0)
1073 ERROR(cdev, "%s setup response queue error\n", __func__);
1074 }
1075 return value;
1076}
1077
Mike Lockwoodba83b012010-04-16 10:39:22 -04001078static int
1079mtp_function_bind(struct usb_configuration *c, struct usb_function *f)
1080{
1081 struct usb_composite_dev *cdev = c->cdev;
Benoit Gobyaab96812011-04-19 20:37:33 -07001082 struct mtp_dev *dev = func_to_mtp(f);
Mike Lockwoodba83b012010-04-16 10:39:22 -04001083 int id;
1084 int ret;
1085
1086 dev->cdev = cdev;
1087 DBG(cdev, "mtp_function_bind dev: %p\n", dev);
1088
1089 /* allocate interface ID(s) */
1090 id = usb_interface_id(c, f);
1091 if (id < 0)
1092 return id;
1093 mtp_interface_desc.bInterfaceNumber = id;
1094
1095 /* allocate endpoints */
Benoit Gobyaab96812011-04-19 20:37:33 -07001096 ret = mtp_create_bulk_endpoints(dev, &mtp_fullspeed_in_desc,
Mike Lockwoodba83b012010-04-16 10:39:22 -04001097 &mtp_fullspeed_out_desc, &mtp_intr_desc);
1098 if (ret)
1099 return ret;
1100
1101 /* support high speed hardware */
1102 if (gadget_is_dualspeed(c->cdev->gadget)) {
1103 mtp_highspeed_in_desc.bEndpointAddress =
1104 mtp_fullspeed_in_desc.bEndpointAddress;
1105 mtp_highspeed_out_desc.bEndpointAddress =
1106 mtp_fullspeed_out_desc.bEndpointAddress;
1107 }
1108
1109 DBG(cdev, "%s speed %s: IN/%s, OUT/%s\n",
1110 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
1111 f->name, dev->ep_in->name, dev->ep_out->name);
1112 return 0;
1113}
1114
1115static void
1116mtp_function_unbind(struct usb_configuration *c, struct usb_function *f)
1117{
Benoit Gobyaab96812011-04-19 20:37:33 -07001118 struct mtp_dev *dev = func_to_mtp(f);
Mike Lockwoodba83b012010-04-16 10:39:22 -04001119 struct usb_request *req;
1120 int i;
1121
Benoit Gobyaab96812011-04-19 20:37:33 -07001122 while ((req = mtp_req_get(dev, &dev->tx_idle)))
Mike Lockwoodba83b012010-04-16 10:39:22 -04001123 mtp_request_free(req, dev->ep_in);
1124 for (i = 0; i < RX_REQ_MAX; i++)
1125 mtp_request_free(dev->rx_req[i], dev->ep_out);
Mike Lockwoodba3673b2011-05-01 20:36:19 -04001126 while ((req = mtp_req_get(dev, &dev->intr_idle)))
1127 mtp_request_free(req, dev->ep_intr);
Mike Lockwoodba83b012010-04-16 10:39:22 -04001128 dev->state = STATE_OFFLINE;
Mike Lockwoodba83b012010-04-16 10:39:22 -04001129}
1130
Mike Lockwoodba83b012010-04-16 10:39:22 -04001131static int mtp_function_set_alt(struct usb_function *f,
1132 unsigned intf, unsigned alt)
1133{
Benoit Gobyaab96812011-04-19 20:37:33 -07001134 struct mtp_dev *dev = func_to_mtp(f);
Mike Lockwoodba83b012010-04-16 10:39:22 -04001135 struct usb_composite_dev *cdev = f->config->cdev;
1136 int ret;
1137
1138 DBG(cdev, "mtp_function_set_alt intf: %d alt: %d\n", intf, alt);
Tatyana Brokhmancf709c12011-06-28 16:33:48 +03001139
Tatyana Brokhman31ac3522011-06-28 15:33:50 +02001140 ret = config_ep_by_speed(cdev->gadget, f, dev->ep_in);
Tatyana Brokhmanebd3f392011-06-28 16:33:50 +03001141 if (ret) {
Tatyana Brokhman31ac3522011-06-28 15:33:50 +02001142 dev->ep_in->desc = NULL;
1143 ERROR(cdev, "config_ep_by_speed failes for ep %s, result %d\n",
1144 dev->ep_in->name, ret);
1145 return ret;
1146 }
1147 ret = usb_ep_enable(dev->ep_in);
1148 if (ret) {
1149 ERROR(cdev, "failed to enable ep %s, result %d\n",
1150 dev->ep_in->name, ret);
1151 return ret;
1152 }
1153
1154 ret = config_ep_by_speed(cdev->gadget, f, dev->ep_out);
1155 if (ret) {
1156 dev->ep_out->desc = NULL;
1157 ERROR(cdev, "config_ep_by_speed failes for ep %s, result %d\n",
1158 dev->ep_out->name, ret);
Tatyana Brokhmanebd3f392011-06-28 16:33:50 +03001159 usb_ep_disable(dev->ep_in);
1160 return ret;
1161 }
Tatyana Brokhman31ac3522011-06-28 15:33:50 +02001162 ret = usb_ep_enable(dev->ep_out);
1163 if (ret) {
1164 ERROR(cdev, "failed to enable ep %s, result %d\n",
1165 dev->ep_out->name, ret);
Tatyana Brokhmanebd3f392011-06-28 16:33:50 +03001166 usb_ep_disable(dev->ep_in);
1167 return ret;
1168 }
Tatyana Brokhmancf709c12011-06-28 16:33:48 +03001169 dev->ep_intr->desc = &mtp_intr_desc;
1170 ret = usb_ep_enable(dev->ep_intr);
Mike Lockwoodba83b012010-04-16 10:39:22 -04001171 if (ret) {
1172 usb_ep_disable(dev->ep_out);
1173 usb_ep_disable(dev->ep_in);
1174 return ret;
1175 }
1176 dev->state = STATE_READY;
1177
1178 /* readers may be blocked waiting for us to go online */
1179 wake_up(&dev->read_wq);
1180 return 0;
1181}
1182
1183static void mtp_function_disable(struct usb_function *f)
1184{
Benoit Gobyaab96812011-04-19 20:37:33 -07001185 struct mtp_dev *dev = func_to_mtp(f);
Mike Lockwoodba83b012010-04-16 10:39:22 -04001186 struct usb_composite_dev *cdev = dev->cdev;
1187
1188 DBG(cdev, "mtp_function_disable\n");
1189 dev->state = STATE_OFFLINE;
1190 usb_ep_disable(dev->ep_in);
1191 usb_ep_disable(dev->ep_out);
1192 usb_ep_disable(dev->ep_intr);
1193
1194 /* readers may be blocked waiting for us to go online */
1195 wake_up(&dev->read_wq);
1196
1197 VDBG(cdev, "%s disabled\n", dev->function.name);
1198}
1199
Mike Lockwoodcf7addf2011-06-01 22:17:36 -04001200static int mtp_bind_config(struct usb_configuration *c, bool ptp_config)
Mike Lockwoodba83b012010-04-16 10:39:22 -04001201{
Benoit Gobyaab96812011-04-19 20:37:33 -07001202 struct mtp_dev *dev = _mtp_dev;
Mike Lockwood090cbc42011-02-07 11:51:07 -05001203 int ret = 0;
Mike Lockwoodba83b012010-04-16 10:39:22 -04001204
1205 printk(KERN_INFO "mtp_bind_config\n");
1206
Mike Lockwoodba83b012010-04-16 10:39:22 -04001207 /* allocate a string ID for our interface */
1208 if (mtp_string_defs[INTERFACE_STRING_INDEX].id == 0) {
1209 ret = usb_string_id(c->cdev);
1210 if (ret < 0)
1211 return ret;
1212 mtp_string_defs[INTERFACE_STRING_INDEX].id = ret;
1213 mtp_interface_desc.iInterface = ret;
1214 }
1215
Mike Lockwoodba83b012010-04-16 10:39:22 -04001216 dev->cdev = c->cdev;
1217 dev->function.name = "mtp";
Mike Lockwoodcf7addf2011-06-01 22:17:36 -04001218 dev->function.strings = mtp_strings;
1219 if (ptp_config) {
1220 dev->function.descriptors = fs_ptp_descs;
1221 dev->function.hs_descriptors = hs_ptp_descs;
1222 } else {
1223 dev->function.descriptors = fs_mtp_descs;
1224 dev->function.hs_descriptors = hs_mtp_descs;
1225 }
Mike Lockwoodba83b012010-04-16 10:39:22 -04001226 dev->function.bind = mtp_function_bind;
1227 dev->function.unbind = mtp_function_unbind;
Mike Lockwoodba83b012010-04-16 10:39:22 -04001228 dev->function.set_alt = mtp_function_set_alt;
1229 dev->function.disable = mtp_function_disable;
1230
Benoit Gobyaab96812011-04-19 20:37:33 -07001231 return usb_add_function(c, &dev->function);
1232}
1233
1234static int mtp_setup(void)
1235{
1236 struct mtp_dev *dev;
1237 int ret;
1238
1239 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1240 if (!dev)
1241 return -ENOMEM;
1242
1243 spin_lock_init(&dev->lock);
1244 init_waitqueue_head(&dev->read_wq);
1245 init_waitqueue_head(&dev->write_wq);
Mike Lockwoodba3673b2011-05-01 20:36:19 -04001246 init_waitqueue_head(&dev->intr_wq);
Benoit Gobyaab96812011-04-19 20:37:33 -07001247 atomic_set(&dev->open_excl, 0);
1248 atomic_set(&dev->ioctl_excl, 0);
1249 INIT_LIST_HEAD(&dev->tx_idle);
Mike Lockwoodba3673b2011-05-01 20:36:19 -04001250 INIT_LIST_HEAD(&dev->intr_idle);
Benoit Gobyaab96812011-04-19 20:37:33 -07001251
1252 dev->wq = create_singlethread_workqueue("f_mtp");
1253 if (!dev->wq) {
1254 ret = -ENOMEM;
1255 goto err1;
1256 }
1257 INIT_WORK(&dev->send_file_work, send_file_work);
1258 INIT_WORK(&dev->receive_file_work, receive_file_work);
1259
Mike Lockwoodba83b012010-04-16 10:39:22 -04001260 _mtp_dev = dev;
1261
1262 ret = misc_register(&mtp_device);
1263 if (ret)
Mike Lockwoodba83b012010-04-16 10:39:22 -04001264 goto err2;
1265
1266 return 0;
1267
1268err2:
Benoit Gobyaab96812011-04-19 20:37:33 -07001269 destroy_workqueue(dev->wq);
Mike Lockwoodba83b012010-04-16 10:39:22 -04001270err1:
Benoit Gobyaab96812011-04-19 20:37:33 -07001271 _mtp_dev = NULL;
Mike Lockwoodba83b012010-04-16 10:39:22 -04001272 kfree(dev);
1273 printk(KERN_ERR "mtp gadget driver failed to initialize\n");
1274 return ret;
1275}
1276
Benoit Gobyaab96812011-04-19 20:37:33 -07001277static void mtp_cleanup(void)
Mike Lockwoodba83b012010-04-16 10:39:22 -04001278{
Benoit Gobyaab96812011-04-19 20:37:33 -07001279 struct mtp_dev *dev = _mtp_dev;
1280
1281 if (!dev)
1282 return;
1283
1284 misc_deregister(&mtp_device);
1285 destroy_workqueue(dev->wq);
1286 _mtp_dev = NULL;
1287 kfree(dev);
Mike Lockwoodba83b012010-04-16 10:39:22 -04001288}