blob: 0e619e6c52c678a966c572fe73976a1c251b7b78 [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);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400711 dev->state = STATE_ERROR;
712 r = -EIO;
713 break;
714 }
715
716 count -= xfer;
717
718 /* zero this so we don't try to free it on error exit */
719 req = 0;
720 }
721
722 if (req)
Benoit Gobyaab96812011-04-19 20:37:33 -0700723 mtp_req_put(dev, &dev->tx_idle, req);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400724
Mike Lockwood491d4182010-11-08 10:41:31 -0500725 DBG(cdev, "send_file_work returning %d\n", r);
726 /* write the result */
727 dev->xfer_result = r;
728 smp_wmb();
Mike Lockwoodba83b012010-04-16 10:39:22 -0400729}
730
Mike Lockwood491d4182010-11-08 10:41:31 -0500731/* read from USB and write to a local file */
732static void receive_file_work(struct work_struct *data)
Mike Lockwoodba83b012010-04-16 10:39:22 -0400733{
Mike Lockwood491d4182010-11-08 10:41:31 -0500734 struct mtp_dev *dev = container_of(data, struct mtp_dev, receive_file_work);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400735 struct usb_composite_dev *cdev = dev->cdev;
736 struct usb_request *read_req = NULL, *write_req = NULL;
Mike Lockwood491d4182010-11-08 10:41:31 -0500737 struct file *filp;
738 loff_t offset;
Mike Lockwood3e800b62010-11-16 17:14:32 -0500739 int64_t count;
Mike Lockwood76ac6552010-11-15 15:22:21 -0500740 int ret, cur_buf = 0;
741 int r = 0;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400742
Mike Lockwood491d4182010-11-08 10:41:31 -0500743 /* read our parameters */
744 smp_rmb();
745 filp = dev->xfer_file;
746 offset = dev->xfer_file_offset;
Mike Lockwood76ac6552010-11-15 15:22:21 -0500747 count = dev->xfer_file_length;
Mike Lockwood491d4182010-11-08 10:41:31 -0500748
Mike Lockwood3e800b62010-11-16 17:14:32 -0500749 DBG(cdev, "receive_file_work(%lld)\n", count);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400750
751 while (count > 0 || write_req) {
752 if (count > 0) {
753 /* queue a request */
754 read_req = dev->rx_req[cur_buf];
755 cur_buf = (cur_buf + 1) % RX_REQ_MAX;
756
Benoit Gobyaab96812011-04-19 20:37:33 -0700757 read_req->length = (count > MTP_BULK_BUFFER_SIZE
758 ? MTP_BULK_BUFFER_SIZE : count);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400759 dev->rx_done = 0;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400760 ret = usb_ep_queue(dev->ep_out, read_req, GFP_KERNEL);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400761 if (ret < 0) {
762 r = -EIO;
763 dev->state = STATE_ERROR;
764 break;
765 }
Mike Lockwoodba83b012010-04-16 10:39:22 -0400766 }
767
768 if (write_req) {
769 DBG(cdev, "rx %p %d\n", write_req, write_req->actual);
770 ret = vfs_write(filp, write_req->buf, write_req->actual,
771 &offset);
772 DBG(cdev, "vfs_write %d\n", ret);
773 if (ret != write_req->actual) {
774 r = -EIO;
775 dev->state = STATE_ERROR;
776 break;
777 }
778 write_req = NULL;
779 }
780
781 if (read_req) {
782 /* wait for our last read to complete */
783 ret = wait_event_interruptible(dev->read_wq,
784 dev->rx_done || dev->state != STATE_BUSY);
Mike Lockwood50fe49a2011-01-13 16:19:57 -0500785 if (dev->state == STATE_CANCELED) {
786 r = -ECANCELED;
787 if (!dev->rx_done)
788 usb_ep_dequeue(dev->ep_out, read_req);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400789 break;
790 }
Mike Lockwood3e800b62010-11-16 17:14:32 -0500791 /* if xfer_file_length is 0xFFFFFFFF, then we read until
792 * we get a zero length packet
793 */
794 if (count != 0xFFFFFFFF)
795 count -= read_req->actual;
796 if (read_req->actual < read_req->length) {
797 /* short packet is used to signal EOF for sizes > 4 gig */
798 DBG(cdev, "got short packet\n");
799 count = 0;
800 }
801
Mike Lockwoodba83b012010-04-16 10:39:22 -0400802 write_req = read_req;
803 read_req = NULL;
804 }
805 }
806
Mike Lockwood491d4182010-11-08 10:41:31 -0500807 DBG(cdev, "receive_file_work returning %d\n", r);
808 /* write the result */
809 dev->xfer_result = r;
810 smp_wmb();
Mike Lockwoodba83b012010-04-16 10:39:22 -0400811}
812
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400813static int mtp_send_event(struct mtp_dev *dev, struct mtp_event *event)
814{
Mike Lockwoodba3673b2011-05-01 20:36:19 -0400815 struct usb_request *req= NULL;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400816 int ret;
817 int length = event->length;
818
819 DBG(dev->cdev, "mtp_send_event(%d)\n", event->length);
820
821 if (length < 0 || length > INTR_BUFFER_SIZE)
822 return -EINVAL;
Mike Lockwood491d4182010-11-08 10:41:31 -0500823 if (dev->state == STATE_OFFLINE)
824 return -ENODEV;
Mike Lockwood292b9632011-02-10 11:54:53 -0500825
Mike Lockwoodba3673b2011-05-01 20:36:19 -0400826 ret = wait_event_interruptible_timeout(dev->intr_wq,
827 (req = mtp_req_get(dev, &dev->intr_idle)), msecs_to_jiffies(1000));
828 if (!req)
829 return -ETIME;
830
831 if (copy_from_user(req->buf, (void __user *)event->data, length)) {
832 mtp_req_put(dev, &dev->intr_idle, req);
Mike Lockwood491d4182010-11-08 10:41:31 -0500833 return -EFAULT;
Mike Lockwoodba3673b2011-05-01 20:36:19 -0400834 }
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400835 req->length = length;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400836 ret = usb_ep_queue(dev->ep_intr, req, GFP_KERNEL);
837 if (ret)
Mike Lockwoodba3673b2011-05-01 20:36:19 -0400838 mtp_req_put(dev, &dev->intr_idle, req);
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400839
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400840 return ret;
841}
842
Mike Lockwoodba83b012010-04-16 10:39:22 -0400843static long mtp_ioctl(struct file *fp, unsigned code, unsigned long value)
844{
845 struct mtp_dev *dev = fp->private_data;
846 struct file *filp = NULL;
847 int ret = -EINVAL;
848
Benoit Gobyaab96812011-04-19 20:37:33 -0700849 if (mtp_lock(&dev->ioctl_excl))
Mike Lockwood491d4182010-11-08 10:41:31 -0500850 return -EBUSY;
851
Mike Lockwoodba83b012010-04-16 10:39:22 -0400852 switch (code) {
853 case MTP_SEND_FILE:
854 case MTP_RECEIVE_FILE:
Mike Lockwoodce4022b2011-07-14 19:42:42 -0400855 case MTP_SEND_FILE_WITH_HEADER:
Mike Lockwoodba83b012010-04-16 10:39:22 -0400856 {
857 struct mtp_file_range mfr;
Mike Lockwood491d4182010-11-08 10:41:31 -0500858 struct work_struct *work;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400859
860 spin_lock_irq(&dev->lock);
861 if (dev->state == STATE_CANCELED) {
862 /* report cancelation to userspace */
863 dev->state = STATE_READY;
864 spin_unlock_irq(&dev->lock);
Mike Lockwood491d4182010-11-08 10:41:31 -0500865 ret = -ECANCELED;
866 goto out;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400867 }
868 if (dev->state == STATE_OFFLINE) {
869 spin_unlock_irq(&dev->lock);
Mike Lockwood491d4182010-11-08 10:41:31 -0500870 ret = -ENODEV;
871 goto out;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400872 }
873 dev->state = STATE_BUSY;
874 spin_unlock_irq(&dev->lock);
875
876 if (copy_from_user(&mfr, (void __user *)value, sizeof(mfr))) {
877 ret = -EFAULT;
878 goto fail;
879 }
Mike Lockwood491d4182010-11-08 10:41:31 -0500880 /* hold a reference to the file while we are working with it */
Mike Lockwoodba83b012010-04-16 10:39:22 -0400881 filp = fget(mfr.fd);
882 if (!filp) {
883 ret = -EBADF;
884 goto fail;
885 }
886
Mike Lockwood491d4182010-11-08 10:41:31 -0500887 /* write the parameters */
888 dev->xfer_file = filp;
889 dev->xfer_file_offset = mfr.offset;
890 dev->xfer_file_length = mfr.length;
891 smp_wmb();
Mike Lockwoodba83b012010-04-16 10:39:22 -0400892
Mike Lockwoodce4022b2011-07-14 19:42:42 -0400893 if (code == MTP_SEND_FILE_WITH_HEADER) {
Mike Lockwood491d4182010-11-08 10:41:31 -0500894 work = &dev->send_file_work;
Mike Lockwoodce4022b2011-07-14 19:42:42 -0400895 dev->xfer_send_header = 1;
896 dev->xfer_command = mfr.command;
897 dev->xfer_transaction_id = mfr.transaction_id;
898 } else if (code == MTP_SEND_FILE) {
899 work = &dev->send_file_work;
900 dev->xfer_send_header = 0;
901 } else {
Mike Lockwood491d4182010-11-08 10:41:31 -0500902 work = &dev->receive_file_work;
Mike Lockwoodce4022b2011-07-14 19:42:42 -0400903 }
Mike Lockwoodba83b012010-04-16 10:39:22 -0400904
Mike Lockwood491d4182010-11-08 10:41:31 -0500905 /* We do the file transfer on a work queue so it will run
906 * in kernel context, which is necessary for vfs_read and
907 * vfs_write to use our buffers in the kernel address space.
908 */
909 queue_work(dev->wq, work);
910 /* wait for operation to complete */
911 flush_workqueue(dev->wq);
912 fput(filp);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400913
Mike Lockwood491d4182010-11-08 10:41:31 -0500914 /* read the result */
915 smp_rmb();
916 ret = dev->xfer_result;
Mike Lockwoodba83b012010-04-16 10:39:22 -0400917 break;
918 }
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400919 case MTP_SEND_EVENT:
920 {
921 struct mtp_event event;
922 /* return here so we don't change dev->state below,
923 * which would interfere with bulk transfer state.
924 */
925 if (copy_from_user(&event, (void __user *)value, sizeof(event)))
Mike Lockwood491d4182010-11-08 10:41:31 -0500926 ret = -EFAULT;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400927 else
Mike Lockwood491d4182010-11-08 10:41:31 -0500928 ret = mtp_send_event(dev, &event);
929 goto out;
Mike Lockwood1de4d4d2010-07-06 19:27:52 -0400930 }
Mike Lockwoodba83b012010-04-16 10:39:22 -0400931 }
932
933fail:
Mike Lockwoodba83b012010-04-16 10:39:22 -0400934 spin_lock_irq(&dev->lock);
935 if (dev->state == STATE_CANCELED)
936 ret = -ECANCELED;
937 else if (dev->state != STATE_OFFLINE)
938 dev->state = STATE_READY;
939 spin_unlock_irq(&dev->lock);
Mike Lockwood491d4182010-11-08 10:41:31 -0500940out:
Benoit Gobyaab96812011-04-19 20:37:33 -0700941 mtp_unlock(&dev->ioctl_excl);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400942 DBG(dev->cdev, "ioctl returning %d\n", ret);
943 return ret;
944}
945
946static int mtp_open(struct inode *ip, struct file *fp)
947{
948 printk(KERN_INFO "mtp_open\n");
Benoit Gobyaab96812011-04-19 20:37:33 -0700949 if (mtp_lock(&_mtp_dev->open_excl))
Mike Lockwoodba83b012010-04-16 10:39:22 -0400950 return -EBUSY;
951
Mike Lockwoodba83b012010-04-16 10:39:22 -0400952 /* clear any error condition */
953 if (_mtp_dev->state != STATE_OFFLINE)
954 _mtp_dev->state = STATE_READY;
955
956 fp->private_data = _mtp_dev;
957 return 0;
958}
959
960static int mtp_release(struct inode *ip, struct file *fp)
961{
962 printk(KERN_INFO "mtp_release\n");
963
Benoit Gobyaab96812011-04-19 20:37:33 -0700964 mtp_unlock(&_mtp_dev->open_excl);
Mike Lockwoodba83b012010-04-16 10:39:22 -0400965 return 0;
966}
967
968/* file operations for /dev/mtp_usb */
969static const struct file_operations mtp_fops = {
970 .owner = THIS_MODULE,
971 .read = mtp_read,
972 .write = mtp_write,
973 .unlocked_ioctl = mtp_ioctl,
974 .open = mtp_open,
975 .release = mtp_release,
976};
977
978static struct miscdevice mtp_device = {
979 .minor = MISC_DYNAMIC_MINOR,
Benoit Gobyaab96812011-04-19 20:37:33 -0700980 .name = mtp_shortname,
Mike Lockwoodba83b012010-04-16 10:39:22 -0400981 .fops = &mtp_fops,
982};
983
Benoit Gobyaab96812011-04-19 20:37:33 -0700984static int mtp_ctrlrequest(struct usb_composite_dev *cdev,
985 const struct usb_ctrlrequest *ctrl)
986{
Mike Lockwoodd74348c2011-07-20 17:08:28 -0700987 struct mtp_dev *dev = _mtp_dev;
Benoit Gobyaab96812011-04-19 20:37:33 -0700988 int value = -EOPNOTSUPP;
989 u16 w_index = le16_to_cpu(ctrl->wIndex);
990 u16 w_value = le16_to_cpu(ctrl->wValue);
991 u16 w_length = le16_to_cpu(ctrl->wLength);
Mike Lockwoodd74348c2011-07-20 17:08:28 -0700992 unsigned long flags;
Benoit Gobyaab96812011-04-19 20:37:33 -0700993
994 VDBG(cdev, "mtp_ctrlrequest "
995 "%02x.%02x v%04x i%04x l%u\n",
996 ctrl->bRequestType, ctrl->bRequest,
997 w_value, w_index, w_length);
998
999 /* Handle MTP OS string */
Mike Lockwoodcf7addf2011-06-01 22:17:36 -04001000 if (ctrl->bRequestType ==
Benoit Gobyaab96812011-04-19 20:37:33 -07001001 (USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE)
1002 && ctrl->bRequest == USB_REQ_GET_DESCRIPTOR
1003 && (w_value >> 8) == USB_DT_STRING
1004 && (w_value & 0xFF) == MTP_OS_STRING_ID) {
1005 value = (w_length < sizeof(mtp_os_string)
1006 ? w_length : sizeof(mtp_os_string));
1007 memcpy(cdev->req->buf, mtp_os_string, value);
Mike Lockwoodd74348c2011-07-20 17:08:28 -07001008 } else if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_VENDOR) {
1009 /* Handle MTP OS descriptor */
1010 DBG(cdev, "vendor request: %d index: %d value: %d length: %d\n",
1011 ctrl->bRequest, w_index, w_value, w_length);
1012
1013 if (ctrl->bRequest == 1
1014 && (ctrl->bRequestType & USB_DIR_IN)
1015 && (w_index == 4 || w_index == 5)) {
1016 value = (w_length < sizeof(mtp_ext_config_desc) ?
1017 w_length : sizeof(mtp_ext_config_desc));
1018 memcpy(cdev->req->buf, &mtp_ext_config_desc, value);
1019 }
1020 } else if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_CLASS) {
1021 DBG(cdev, "class request: %d index: %d value: %d length: %d\n",
1022 ctrl->bRequest, w_index, w_value, w_length);
1023
1024 if (ctrl->bRequest == MTP_REQ_CANCEL && w_index == 0
1025 && w_value == 0) {
1026 DBG(cdev, "MTP_REQ_CANCEL\n");
1027
1028 spin_lock_irqsave(&dev->lock, flags);
1029 if (dev->state == STATE_BUSY) {
1030 dev->state = STATE_CANCELED;
1031 wake_up(&dev->read_wq);
1032 wake_up(&dev->write_wq);
1033 }
1034 spin_unlock_irqrestore(&dev->lock, flags);
1035
1036 /* We need to queue a request to read the remaining
1037 * bytes, but we don't actually need to look at
1038 * the contents.
1039 */
1040 value = w_length;
1041 } else if (ctrl->bRequest == MTP_REQ_GET_DEVICE_STATUS
1042 && w_index == 0 && w_value == 0) {
1043 struct mtp_device_status *status = cdev->req->buf;
1044 status->wLength =
1045 __constant_cpu_to_le16(sizeof(*status));
1046
1047 DBG(cdev, "MTP_REQ_GET_DEVICE_STATUS\n");
1048 spin_lock_irqsave(&dev->lock, flags);
1049 /* device status is "busy" until we report
1050 * the cancelation to userspace
1051 */
1052 if (dev->state == STATE_CANCELED)
1053 status->wCode =
1054 __cpu_to_le16(MTP_RESPONSE_DEVICE_BUSY);
1055 else
1056 status->wCode =
1057 __cpu_to_le16(MTP_RESPONSE_OK);
1058 spin_unlock_irqrestore(&dev->lock, flags);
1059 value = sizeof(*status);
1060 }
Benoit Gobyaab96812011-04-19 20:37:33 -07001061 }
Mike Lockwoodd74348c2011-07-20 17:08:28 -07001062
Benoit Gobyaab96812011-04-19 20:37:33 -07001063 /* respond with data transfer or status phase? */
1064 if (value >= 0) {
1065 int rc;
1066 cdev->req->zero = value < w_length;
1067 cdev->req->length = value;
1068 rc = usb_ep_queue(cdev->gadget->ep0, cdev->req, GFP_ATOMIC);
1069 if (rc < 0)
1070 ERROR(cdev, "%s setup response queue error\n", __func__);
1071 }
1072 return value;
1073}
1074
Mike Lockwoodba83b012010-04-16 10:39:22 -04001075static int
1076mtp_function_bind(struct usb_configuration *c, struct usb_function *f)
1077{
1078 struct usb_composite_dev *cdev = c->cdev;
Benoit Gobyaab96812011-04-19 20:37:33 -07001079 struct mtp_dev *dev = func_to_mtp(f);
Mike Lockwoodba83b012010-04-16 10:39:22 -04001080 int id;
1081 int ret;
1082
1083 dev->cdev = cdev;
1084 DBG(cdev, "mtp_function_bind dev: %p\n", dev);
1085
1086 /* allocate interface ID(s) */
1087 id = usb_interface_id(c, f);
1088 if (id < 0)
1089 return id;
1090 mtp_interface_desc.bInterfaceNumber = id;
1091
1092 /* allocate endpoints */
Benoit Gobyaab96812011-04-19 20:37:33 -07001093 ret = mtp_create_bulk_endpoints(dev, &mtp_fullspeed_in_desc,
Mike Lockwoodba83b012010-04-16 10:39:22 -04001094 &mtp_fullspeed_out_desc, &mtp_intr_desc);
1095 if (ret)
1096 return ret;
1097
1098 /* support high speed hardware */
1099 if (gadget_is_dualspeed(c->cdev->gadget)) {
1100 mtp_highspeed_in_desc.bEndpointAddress =
1101 mtp_fullspeed_in_desc.bEndpointAddress;
1102 mtp_highspeed_out_desc.bEndpointAddress =
1103 mtp_fullspeed_out_desc.bEndpointAddress;
1104 }
1105
1106 DBG(cdev, "%s speed %s: IN/%s, OUT/%s\n",
1107 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
1108 f->name, dev->ep_in->name, dev->ep_out->name);
1109 return 0;
1110}
1111
1112static void
1113mtp_function_unbind(struct usb_configuration *c, struct usb_function *f)
1114{
Benoit Gobyaab96812011-04-19 20:37:33 -07001115 struct mtp_dev *dev = func_to_mtp(f);
Mike Lockwoodba83b012010-04-16 10:39:22 -04001116 struct usb_request *req;
1117 int i;
1118
Benoit Gobyaab96812011-04-19 20:37:33 -07001119 while ((req = mtp_req_get(dev, &dev->tx_idle)))
Mike Lockwoodba83b012010-04-16 10:39:22 -04001120 mtp_request_free(req, dev->ep_in);
1121 for (i = 0; i < RX_REQ_MAX; i++)
1122 mtp_request_free(dev->rx_req[i], dev->ep_out);
Mike Lockwoodba3673b2011-05-01 20:36:19 -04001123 while ((req = mtp_req_get(dev, &dev->intr_idle)))
1124 mtp_request_free(req, dev->ep_intr);
Mike Lockwoodba83b012010-04-16 10:39:22 -04001125 dev->state = STATE_OFFLINE;
Mike Lockwoodba83b012010-04-16 10:39:22 -04001126}
1127
Mike Lockwoodba83b012010-04-16 10:39:22 -04001128static int mtp_function_set_alt(struct usb_function *f,
1129 unsigned intf, unsigned alt)
1130{
Benoit Gobyaab96812011-04-19 20:37:33 -07001131 struct mtp_dev *dev = func_to_mtp(f);
Mike Lockwoodba83b012010-04-16 10:39:22 -04001132 struct usb_composite_dev *cdev = f->config->cdev;
1133 int ret;
1134
1135 DBG(cdev, "mtp_function_set_alt intf: %d alt: %d\n", intf, alt);
Tatyana Brokhmancf709c12011-06-28 16:33:48 +03001136
Tatyana Brokhman31ac3522011-06-28 15:33:50 +02001137 ret = config_ep_by_speed(cdev->gadget, f, dev->ep_in);
Tatyana Brokhmanebd3f392011-06-28 16:33:50 +03001138 if (ret) {
Tatyana Brokhman31ac3522011-06-28 15:33:50 +02001139 dev->ep_in->desc = NULL;
1140 ERROR(cdev, "config_ep_by_speed failes for ep %s, result %d\n",
1141 dev->ep_in->name, ret);
1142 return ret;
1143 }
1144 ret = usb_ep_enable(dev->ep_in);
1145 if (ret) {
1146 ERROR(cdev, "failed to enable ep %s, result %d\n",
1147 dev->ep_in->name, ret);
1148 return ret;
1149 }
1150
1151 ret = config_ep_by_speed(cdev->gadget, f, dev->ep_out);
1152 if (ret) {
1153 dev->ep_out->desc = NULL;
1154 ERROR(cdev, "config_ep_by_speed failes for ep %s, result %d\n",
1155 dev->ep_out->name, ret);
Tatyana Brokhmanebd3f392011-06-28 16:33:50 +03001156 usb_ep_disable(dev->ep_in);
1157 return ret;
1158 }
Tatyana Brokhman31ac3522011-06-28 15:33:50 +02001159 ret = usb_ep_enable(dev->ep_out);
1160 if (ret) {
1161 ERROR(cdev, "failed to enable ep %s, result %d\n",
1162 dev->ep_out->name, ret);
Tatyana Brokhmanebd3f392011-06-28 16:33:50 +03001163 usb_ep_disable(dev->ep_in);
1164 return ret;
1165 }
Tatyana Brokhmancf709c12011-06-28 16:33:48 +03001166 dev->ep_intr->desc = &mtp_intr_desc;
1167 ret = usb_ep_enable(dev->ep_intr);
Mike Lockwoodba83b012010-04-16 10:39:22 -04001168 if (ret) {
1169 usb_ep_disable(dev->ep_out);
1170 usb_ep_disable(dev->ep_in);
1171 return ret;
1172 }
1173 dev->state = STATE_READY;
1174
1175 /* readers may be blocked waiting for us to go online */
1176 wake_up(&dev->read_wq);
1177 return 0;
1178}
1179
1180static void mtp_function_disable(struct usb_function *f)
1181{
Benoit Gobyaab96812011-04-19 20:37:33 -07001182 struct mtp_dev *dev = func_to_mtp(f);
Mike Lockwoodba83b012010-04-16 10:39:22 -04001183 struct usb_composite_dev *cdev = dev->cdev;
1184
1185 DBG(cdev, "mtp_function_disable\n");
1186 dev->state = STATE_OFFLINE;
1187 usb_ep_disable(dev->ep_in);
1188 usb_ep_disable(dev->ep_out);
1189 usb_ep_disable(dev->ep_intr);
1190
1191 /* readers may be blocked waiting for us to go online */
1192 wake_up(&dev->read_wq);
1193
1194 VDBG(cdev, "%s disabled\n", dev->function.name);
1195}
1196
Mike Lockwoodcf7addf2011-06-01 22:17:36 -04001197static int mtp_bind_config(struct usb_configuration *c, bool ptp_config)
Mike Lockwoodba83b012010-04-16 10:39:22 -04001198{
Benoit Gobyaab96812011-04-19 20:37:33 -07001199 struct mtp_dev *dev = _mtp_dev;
Mike Lockwood090cbc42011-02-07 11:51:07 -05001200 int ret = 0;
Mike Lockwoodba83b012010-04-16 10:39:22 -04001201
1202 printk(KERN_INFO "mtp_bind_config\n");
1203
Mike Lockwoodba83b012010-04-16 10:39:22 -04001204 /* allocate a string ID for our interface */
1205 if (mtp_string_defs[INTERFACE_STRING_INDEX].id == 0) {
1206 ret = usb_string_id(c->cdev);
1207 if (ret < 0)
1208 return ret;
1209 mtp_string_defs[INTERFACE_STRING_INDEX].id = ret;
1210 mtp_interface_desc.iInterface = ret;
1211 }
1212
Mike Lockwoodba83b012010-04-16 10:39:22 -04001213 dev->cdev = c->cdev;
1214 dev->function.name = "mtp";
Mike Lockwoodcf7addf2011-06-01 22:17:36 -04001215 dev->function.strings = mtp_strings;
1216 if (ptp_config) {
1217 dev->function.descriptors = fs_ptp_descs;
1218 dev->function.hs_descriptors = hs_ptp_descs;
1219 } else {
1220 dev->function.descriptors = fs_mtp_descs;
1221 dev->function.hs_descriptors = hs_mtp_descs;
1222 }
Mike Lockwoodba83b012010-04-16 10:39:22 -04001223 dev->function.bind = mtp_function_bind;
1224 dev->function.unbind = mtp_function_unbind;
Mike Lockwoodba83b012010-04-16 10:39:22 -04001225 dev->function.set_alt = mtp_function_set_alt;
1226 dev->function.disable = mtp_function_disable;
1227
Benoit Gobyaab96812011-04-19 20:37:33 -07001228 return usb_add_function(c, &dev->function);
1229}
1230
1231static int mtp_setup(void)
1232{
1233 struct mtp_dev *dev;
1234 int ret;
1235
1236 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1237 if (!dev)
1238 return -ENOMEM;
1239
1240 spin_lock_init(&dev->lock);
1241 init_waitqueue_head(&dev->read_wq);
1242 init_waitqueue_head(&dev->write_wq);
Mike Lockwoodba3673b2011-05-01 20:36:19 -04001243 init_waitqueue_head(&dev->intr_wq);
Benoit Gobyaab96812011-04-19 20:37:33 -07001244 atomic_set(&dev->open_excl, 0);
1245 atomic_set(&dev->ioctl_excl, 0);
1246 INIT_LIST_HEAD(&dev->tx_idle);
Mike Lockwoodba3673b2011-05-01 20:36:19 -04001247 INIT_LIST_HEAD(&dev->intr_idle);
Benoit Gobyaab96812011-04-19 20:37:33 -07001248
1249 dev->wq = create_singlethread_workqueue("f_mtp");
1250 if (!dev->wq) {
1251 ret = -ENOMEM;
1252 goto err1;
1253 }
1254 INIT_WORK(&dev->send_file_work, send_file_work);
1255 INIT_WORK(&dev->receive_file_work, receive_file_work);
1256
Mike Lockwoodba83b012010-04-16 10:39:22 -04001257 _mtp_dev = dev;
1258
1259 ret = misc_register(&mtp_device);
1260 if (ret)
Mike Lockwoodba83b012010-04-16 10:39:22 -04001261 goto err2;
1262
1263 return 0;
1264
1265err2:
Benoit Gobyaab96812011-04-19 20:37:33 -07001266 destroy_workqueue(dev->wq);
Mike Lockwoodba83b012010-04-16 10:39:22 -04001267err1:
Benoit Gobyaab96812011-04-19 20:37:33 -07001268 _mtp_dev = NULL;
Mike Lockwoodba83b012010-04-16 10:39:22 -04001269 kfree(dev);
1270 printk(KERN_ERR "mtp gadget driver failed to initialize\n");
1271 return ret;
1272}
1273
Benoit Gobyaab96812011-04-19 20:37:33 -07001274static void mtp_cleanup(void)
Mike Lockwoodba83b012010-04-16 10:39:22 -04001275{
Benoit Gobyaab96812011-04-19 20:37:33 -07001276 struct mtp_dev *dev = _mtp_dev;
1277
1278 if (!dev)
1279 return;
1280
1281 misc_deregister(&mtp_device);
1282 destroy_workqueue(dev->wq);
1283 _mtp_dev = NULL;
1284 kfree(dev);
Mike Lockwoodba83b012010-04-16 10:39:22 -04001285}