blob: 0c0d58af7ce3046e630d5465d36b8e03ae59d03d [file] [log] [blame]
Benoit Gobyf0fbc482011-12-19 14:37:50 -08001/*
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>
28
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>
37#include <linux/usb/f_mtp.h>
38
39#define MTP_BULK_BUFFER_SIZE 16384
40#define INTR_BUFFER_SIZE 28
41
42/* String IDs */
43#define INTERFACE_STRING_INDEX 0
44
45/* values for mtp_dev.state */
46#define STATE_OFFLINE 0 /* initial state, disconnected */
47#define STATE_READY 1 /* ready for userspace calls */
48#define STATE_BUSY 2 /* processing userspace calls */
49#define STATE_CANCELED 3 /* transaction canceled by host */
50#define STATE_ERROR 4 /* error from completion routine */
51
52/* number of tx and rx requests to allocate */
53#define TX_REQ_MAX 4
54#define RX_REQ_MAX 2
55#define INTR_REQ_MAX 5
56
57/* 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
Pavankumar Kondetie79aa682012-12-19 20:19:35 +053070unsigned int mtp_rx_req_len = MTP_BULK_BUFFER_SIZE;
71module_param(mtp_rx_req_len, uint, S_IRUGO | S_IWUSR);
72
Benoit Gobyf0fbc482011-12-19 14:37:50 -080073static const char mtp_shortname[] = "mtp_usb";
74
75struct mtp_dev {
76 struct usb_function function;
77 struct usb_composite_dev *cdev;
78 spinlock_t lock;
79
80 struct usb_ep *ep_in;
81 struct usb_ep *ep_out;
82 struct usb_ep *ep_intr;
83
84 int state;
85
86 /* synchronize access to our device file */
87 atomic_t open_excl;
88 /* to enforce only one ioctl at a time */
89 atomic_t ioctl_excl;
90
91 struct list_head tx_idle;
92 struct list_head intr_idle;
93
94 wait_queue_head_t read_wq;
95 wait_queue_head_t write_wq;
96 wait_queue_head_t intr_wq;
97 struct usb_request *rx_req[RX_REQ_MAX];
98 int rx_done;
99
100 /* for processing MTP_SEND_FILE, MTP_RECEIVE_FILE and
101 * MTP_SEND_FILE_WITH_HEADER ioctls on a work queue
102 */
103 struct workqueue_struct *wq;
104 struct work_struct send_file_work;
105 struct work_struct receive_file_work;
106 struct file *xfer_file;
107 loff_t xfer_file_offset;
108 int64_t xfer_file_length;
109 unsigned xfer_send_header;
110 uint16_t xfer_command;
111 uint32_t xfer_transaction_id;
112 int xfer_result;
113};
114
115static struct usb_interface_descriptor mtp_interface_desc = {
116 .bLength = USB_DT_INTERFACE_SIZE,
117 .bDescriptorType = USB_DT_INTERFACE,
118 .bInterfaceNumber = 0,
119 .bNumEndpoints = 3,
120 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
121 .bInterfaceSubClass = USB_SUBCLASS_VENDOR_SPEC,
122 .bInterfaceProtocol = 0,
123};
124
125static struct usb_interface_descriptor ptp_interface_desc = {
126 .bLength = USB_DT_INTERFACE_SIZE,
127 .bDescriptorType = USB_DT_INTERFACE,
128 .bInterfaceNumber = 0,
129 .bNumEndpoints = 3,
130 .bInterfaceClass = USB_CLASS_STILL_IMAGE,
131 .bInterfaceSubClass = 1,
132 .bInterfaceProtocol = 1,
133};
134
Pavankumar Kondeti6f94bc92012-08-03 09:34:32 +0530135static struct usb_endpoint_descriptor mtp_superspeed_in_desc = {
136 .bLength = USB_DT_ENDPOINT_SIZE,
137 .bDescriptorType = USB_DT_ENDPOINT,
138 .bEndpointAddress = USB_DIR_IN,
139 .bmAttributes = USB_ENDPOINT_XFER_BULK,
140 .wMaxPacketSize = __constant_cpu_to_le16(1024),
141};
142
143static struct usb_ss_ep_comp_descriptor mtp_superspeed_in_comp_desc = {
144 .bLength = sizeof mtp_superspeed_in_comp_desc,
145 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
146
147 /* the following 2 values can be tweaked if necessary */
148 /* .bMaxBurst = 0, */
149 /* .bmAttributes = 0, */
150};
151
152static struct usb_endpoint_descriptor mtp_superspeed_out_desc = {
153 .bLength = USB_DT_ENDPOINT_SIZE,
154 .bDescriptorType = USB_DT_ENDPOINT,
155 .bEndpointAddress = USB_DIR_OUT,
156 .bmAttributes = USB_ENDPOINT_XFER_BULK,
157 .wMaxPacketSize = __constant_cpu_to_le16(1024),
158};
159
160static struct usb_ss_ep_comp_descriptor mtp_superspeed_out_comp_desc = {
161 .bLength = sizeof mtp_superspeed_out_comp_desc,
162 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
163
164 /* the following 2 values can be tweaked if necessary */
165 /* .bMaxBurst = 0, */
166 /* .bmAttributes = 0, */
167};
168
Benoit Gobyf0fbc482011-12-19 14:37:50 -0800169static struct usb_endpoint_descriptor mtp_highspeed_in_desc = {
170 .bLength = USB_DT_ENDPOINT_SIZE,
171 .bDescriptorType = USB_DT_ENDPOINT,
172 .bEndpointAddress = USB_DIR_IN,
173 .bmAttributes = USB_ENDPOINT_XFER_BULK,
174 .wMaxPacketSize = __constant_cpu_to_le16(512),
175};
176
177static struct usb_endpoint_descriptor mtp_highspeed_out_desc = {
178 .bLength = USB_DT_ENDPOINT_SIZE,
179 .bDescriptorType = USB_DT_ENDPOINT,
180 .bEndpointAddress = USB_DIR_OUT,
181 .bmAttributes = USB_ENDPOINT_XFER_BULK,
182 .wMaxPacketSize = __constant_cpu_to_le16(512),
183};
184
185static struct usb_endpoint_descriptor mtp_fullspeed_in_desc = {
186 .bLength = USB_DT_ENDPOINT_SIZE,
187 .bDescriptorType = USB_DT_ENDPOINT,
188 .bEndpointAddress = USB_DIR_IN,
189 .bmAttributes = USB_ENDPOINT_XFER_BULK,
190};
191
192static struct usb_endpoint_descriptor mtp_fullspeed_out_desc = {
193 .bLength = USB_DT_ENDPOINT_SIZE,
194 .bDescriptorType = USB_DT_ENDPOINT,
195 .bEndpointAddress = USB_DIR_OUT,
196 .bmAttributes = USB_ENDPOINT_XFER_BULK,
197};
198
199static struct usb_endpoint_descriptor mtp_intr_desc = {
200 .bLength = USB_DT_ENDPOINT_SIZE,
201 .bDescriptorType = USB_DT_ENDPOINT,
202 .bEndpointAddress = USB_DIR_IN,
203 .bmAttributes = USB_ENDPOINT_XFER_INT,
204 .wMaxPacketSize = __constant_cpu_to_le16(INTR_BUFFER_SIZE),
205 .bInterval = 6,
206};
207
Pavankumar Kondeti6f94bc92012-08-03 09:34:32 +0530208static struct usb_ss_ep_comp_descriptor mtp_superspeed_intr_comp_desc = {
209 .bLength = sizeof mtp_superspeed_intr_comp_desc,
210 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
211
212 /* the following 3 values can be tweaked if necessary */
213 /* .bMaxBurst = 0, */
214 /* .bmAttributes = 0, */
215 .wBytesPerInterval = cpu_to_le16(INTR_BUFFER_SIZE),
216};
217
Benoit Gobyf0fbc482011-12-19 14:37:50 -0800218static struct usb_descriptor_header *fs_mtp_descs[] = {
219 (struct usb_descriptor_header *) &mtp_interface_desc,
220 (struct usb_descriptor_header *) &mtp_fullspeed_in_desc,
221 (struct usb_descriptor_header *) &mtp_fullspeed_out_desc,
222 (struct usb_descriptor_header *) &mtp_intr_desc,
223 NULL,
224};
225
226static struct usb_descriptor_header *hs_mtp_descs[] = {
227 (struct usb_descriptor_header *) &mtp_interface_desc,
228 (struct usb_descriptor_header *) &mtp_highspeed_in_desc,
229 (struct usb_descriptor_header *) &mtp_highspeed_out_desc,
230 (struct usb_descriptor_header *) &mtp_intr_desc,
231 NULL,
232};
233
Pavankumar Kondeti6f94bc92012-08-03 09:34:32 +0530234static struct usb_descriptor_header *ss_mtp_descs[] = {
235 (struct usb_descriptor_header *) &mtp_interface_desc,
236 (struct usb_descriptor_header *) &mtp_superspeed_in_desc,
237 (struct usb_descriptor_header *) &mtp_superspeed_in_comp_desc,
238 (struct usb_descriptor_header *) &mtp_superspeed_out_desc,
239 (struct usb_descriptor_header *) &mtp_superspeed_out_comp_desc,
240 (struct usb_descriptor_header *) &mtp_intr_desc,
241 (struct usb_descriptor_header *) &mtp_superspeed_intr_comp_desc,
242 NULL,
243};
244
Benoit Gobyf0fbc482011-12-19 14:37:50 -0800245static struct usb_descriptor_header *fs_ptp_descs[] = {
246 (struct usb_descriptor_header *) &ptp_interface_desc,
247 (struct usb_descriptor_header *) &mtp_fullspeed_in_desc,
248 (struct usb_descriptor_header *) &mtp_fullspeed_out_desc,
249 (struct usb_descriptor_header *) &mtp_intr_desc,
250 NULL,
251};
252
253static struct usb_descriptor_header *hs_ptp_descs[] = {
254 (struct usb_descriptor_header *) &ptp_interface_desc,
255 (struct usb_descriptor_header *) &mtp_highspeed_in_desc,
256 (struct usb_descriptor_header *) &mtp_highspeed_out_desc,
257 (struct usb_descriptor_header *) &mtp_intr_desc,
258 NULL,
259};
260
Pavankumar Kondeti6f94bc92012-08-03 09:34:32 +0530261static struct usb_descriptor_header *ss_ptp_descs[] = {
262 (struct usb_descriptor_header *) &ptp_interface_desc,
263 (struct usb_descriptor_header *) &mtp_superspeed_in_desc,
264 (struct usb_descriptor_header *) &mtp_superspeed_in_comp_desc,
265 (struct usb_descriptor_header *) &mtp_superspeed_out_desc,
266 (struct usb_descriptor_header *) &mtp_superspeed_out_comp_desc,
267 (struct usb_descriptor_header *) &mtp_intr_desc,
268 (struct usb_descriptor_header *) &mtp_superspeed_intr_comp_desc,
269 NULL,
270};
271
Benoit Gobyf0fbc482011-12-19 14:37:50 -0800272static struct usb_string mtp_string_defs[] = {
273 /* Naming interface "MTP" so libmtp will recognize us */
274 [INTERFACE_STRING_INDEX].s = "MTP",
275 { }, /* end of list */
276};
277
278static struct usb_gadget_strings mtp_string_table = {
279 .language = 0x0409, /* en-US */
280 .strings = mtp_string_defs,
281};
282
283static struct usb_gadget_strings *mtp_strings[] = {
284 &mtp_string_table,
285 NULL,
286};
287
288/* Microsoft MTP OS String */
289static u8 mtp_os_string[] = {
290 18, /* sizeof(mtp_os_string) */
291 USB_DT_STRING,
292 /* Signature field: "MSFT100" */
293 'M', 0, 'S', 0, 'F', 0, 'T', 0, '1', 0, '0', 0, '0', 0,
294 /* vendor code */
295 1,
296 /* padding */
297 0
298};
299
300/* Microsoft Extended Configuration Descriptor Header Section */
301struct mtp_ext_config_desc_header {
302 __le32 dwLength;
303 __u16 bcdVersion;
304 __le16 wIndex;
305 __u8 bCount;
306 __u8 reserved[7];
307};
308
309/* Microsoft Extended Configuration Descriptor Function Section */
310struct mtp_ext_config_desc_function {
311 __u8 bFirstInterfaceNumber;
312 __u8 bInterfaceCount;
313 __u8 compatibleID[8];
314 __u8 subCompatibleID[8];
315 __u8 reserved[6];
316};
317
318/* MTP Extended Configuration Descriptor */
319struct {
320 struct mtp_ext_config_desc_header header;
321 struct mtp_ext_config_desc_function function;
322} mtp_ext_config_desc = {
323 .header = {
324 .dwLength = __constant_cpu_to_le32(sizeof(mtp_ext_config_desc)),
325 .bcdVersion = __constant_cpu_to_le16(0x0100),
326 .wIndex = __constant_cpu_to_le16(4),
327 .bCount = __constant_cpu_to_le16(1),
328 },
329 .function = {
330 .bFirstInterfaceNumber = 0,
331 .bInterfaceCount = 1,
332 .compatibleID = { 'M', 'T', 'P' },
333 },
334};
335
336struct mtp_device_status {
337 __le16 wLength;
338 __le16 wCode;
339};
340
341/* temporary variable used between mtp_open() and mtp_gadget_bind() */
342static struct mtp_dev *_mtp_dev;
343
344static inline struct mtp_dev *func_to_mtp(struct usb_function *f)
345{
346 return container_of(f, struct mtp_dev, function);
347}
348
349static struct usb_request *mtp_request_new(struct usb_ep *ep, int buffer_size)
350{
351 struct usb_request *req = usb_ep_alloc_request(ep, GFP_KERNEL);
352 if (!req)
353 return NULL;
354
355 /* now allocate buffers for the requests */
356 req->buf = kmalloc(buffer_size, GFP_KERNEL);
357 if (!req->buf) {
358 usb_ep_free_request(ep, req);
359 return NULL;
360 }
361
362 return req;
363}
364
365static void mtp_request_free(struct usb_request *req, struct usb_ep *ep)
366{
367 if (req) {
368 kfree(req->buf);
369 usb_ep_free_request(ep, req);
370 }
371}
372
373static inline int mtp_lock(atomic_t *excl)
374{
375 if (atomic_inc_return(excl) == 1) {
376 return 0;
377 } else {
378 atomic_dec(excl);
379 return -1;
380 }
381}
382
383static inline void mtp_unlock(atomic_t *excl)
384{
385 atomic_dec(excl);
386}
387
388/* add a request to the tail of a list */
389static void mtp_req_put(struct mtp_dev *dev, struct list_head *head,
390 struct usb_request *req)
391{
392 unsigned long flags;
393
394 spin_lock_irqsave(&dev->lock, flags);
395 list_add_tail(&req->list, head);
396 spin_unlock_irqrestore(&dev->lock, flags);
397}
398
399/* remove a request from the head of a list */
400static struct usb_request
401*mtp_req_get(struct mtp_dev *dev, struct list_head *head)
402{
403 unsigned long flags;
404 struct usb_request *req;
405
406 spin_lock_irqsave(&dev->lock, flags);
407 if (list_empty(head)) {
408 req = 0;
409 } else {
410 req = list_first_entry(head, struct usb_request, list);
411 list_del(&req->list);
412 }
413 spin_unlock_irqrestore(&dev->lock, flags);
414 return req;
415}
416
417static void mtp_complete_in(struct usb_ep *ep, struct usb_request *req)
418{
419 struct mtp_dev *dev = _mtp_dev;
420
421 if (req->status != 0)
422 dev->state = STATE_ERROR;
423
424 mtp_req_put(dev, &dev->tx_idle, req);
425
426 wake_up(&dev->write_wq);
427}
428
429static void mtp_complete_out(struct usb_ep *ep, struct usb_request *req)
430{
431 struct mtp_dev *dev = _mtp_dev;
432
433 dev->rx_done = 1;
434 if (req->status != 0)
435 dev->state = STATE_ERROR;
436
437 wake_up(&dev->read_wq);
438}
439
440static void mtp_complete_intr(struct usb_ep *ep, struct usb_request *req)
441{
442 struct mtp_dev *dev = _mtp_dev;
443
444 if (req->status != 0)
445 dev->state = STATE_ERROR;
446
447 mtp_req_put(dev, &dev->intr_idle, req);
448
449 wake_up(&dev->intr_wq);
450}
451
452static int mtp_create_bulk_endpoints(struct mtp_dev *dev,
453 struct usb_endpoint_descriptor *in_desc,
454 struct usb_endpoint_descriptor *out_desc,
455 struct usb_endpoint_descriptor *intr_desc)
456{
457 struct usb_composite_dev *cdev = dev->cdev;
458 struct usb_request *req;
459 struct usb_ep *ep;
460 int i;
461
462 DBG(cdev, "create_bulk_endpoints dev: %p\n", dev);
463
464 ep = usb_ep_autoconfig(cdev->gadget, in_desc);
465 if (!ep) {
466 DBG(cdev, "usb_ep_autoconfig for ep_in failed\n");
467 return -ENODEV;
468 }
469 DBG(cdev, "usb_ep_autoconfig for ep_in got %s\n", ep->name);
470 ep->driver_data = dev; /* claim the endpoint */
471 dev->ep_in = ep;
472
473 ep = usb_ep_autoconfig(cdev->gadget, out_desc);
474 if (!ep) {
475 DBG(cdev, "usb_ep_autoconfig for ep_out failed\n");
476 return -ENODEV;
477 }
478 DBG(cdev, "usb_ep_autoconfig for mtp ep_out got %s\n", ep->name);
479 ep->driver_data = dev; /* claim the endpoint */
480 dev->ep_out = ep;
481
Benoit Gobyf0fbc482011-12-19 14:37:50 -0800482 ep = usb_ep_autoconfig(cdev->gadget, intr_desc);
483 if (!ep) {
484 DBG(cdev, "usb_ep_autoconfig for ep_intr failed\n");
485 return -ENODEV;
486 }
487 DBG(cdev, "usb_ep_autoconfig for mtp ep_intr got %s\n", ep->name);
488 ep->driver_data = dev; /* claim the endpoint */
489 dev->ep_intr = ep;
490
491 /* now allocate requests for our endpoints */
492 for (i = 0; i < TX_REQ_MAX; i++) {
493 req = mtp_request_new(dev->ep_in, MTP_BULK_BUFFER_SIZE);
494 if (!req)
495 goto fail;
496 req->complete = mtp_complete_in;
497 mtp_req_put(dev, &dev->tx_idle, req);
498 }
Pavankumar Kondetie79aa682012-12-19 20:19:35 +0530499
500 /*
501 * The RX buffer should be aligned to EP max packet for
502 * some controllers. At bind time, we don't know the
503 * operational speed. Hence assuming super speed max
504 * packet size.
505 */
506 if (mtp_rx_req_len % 1024)
507 mtp_rx_req_len = MTP_BULK_BUFFER_SIZE;
508
509retry_rx_alloc:
Benoit Gobyf0fbc482011-12-19 14:37:50 -0800510 for (i = 0; i < RX_REQ_MAX; i++) {
Pavankumar Kondetie79aa682012-12-19 20:19:35 +0530511 req = mtp_request_new(dev->ep_out, mtp_rx_req_len);
512 if (!req) {
513 if (mtp_rx_req_len <= MTP_BULK_BUFFER_SIZE)
514 goto fail;
515 for (; i > 0; i--)
516 mtp_request_free(dev->rx_req[i], dev->ep_out);
517 mtp_rx_req_len = MTP_BULK_BUFFER_SIZE;
518 goto retry_rx_alloc;
519 }
Benoit Gobyf0fbc482011-12-19 14:37:50 -0800520 req->complete = mtp_complete_out;
521 dev->rx_req[i] = req;
522 }
523 for (i = 0; i < INTR_REQ_MAX; i++) {
524 req = mtp_request_new(dev->ep_intr, INTR_BUFFER_SIZE);
525 if (!req)
526 goto fail;
527 req->complete = mtp_complete_intr;
528 mtp_req_put(dev, &dev->intr_idle, req);
529 }
530
531 return 0;
532
533fail:
534 printk(KERN_ERR "mtp_bind() could not allocate requests\n");
535 return -1;
536}
537
538static ssize_t mtp_read(struct file *fp, char __user *buf,
539 size_t count, loff_t *pos)
540{
541 struct mtp_dev *dev = fp->private_data;
542 struct usb_composite_dev *cdev = dev->cdev;
543 struct usb_request *req;
544 int r = count, xfer;
545 int ret = 0;
546
547 DBG(cdev, "mtp_read(%d)\n", count);
548
Pavankumar Kondetie79aa682012-12-19 20:19:35 +0530549 if (count > mtp_rx_req_len)
Benoit Gobyf0fbc482011-12-19 14:37:50 -0800550 return -EINVAL;
551
Manu Gautambe803b42012-10-03 18:49:33 +0530552 if (!IS_ALIGNED(count, dev->ep_out->maxpacket))
553 DBG(cdev, "%s - count(%d) not multiple of mtu(%d)\n", __func__,
554 count, dev->ep_out->maxpacket);
555
Benoit Gobyf0fbc482011-12-19 14:37:50 -0800556 /* we will block until we're online */
557 DBG(cdev, "mtp_read: waiting for online state\n");
558 ret = wait_event_interruptible(dev->read_wq,
559 dev->state != STATE_OFFLINE);
560 if (ret < 0) {
561 r = ret;
562 goto done;
563 }
564 spin_lock_irq(&dev->lock);
565 if (dev->state == STATE_CANCELED) {
566 /* report cancelation to userspace */
567 dev->state = STATE_READY;
568 spin_unlock_irq(&dev->lock);
569 return -ECANCELED;
570 }
571 dev->state = STATE_BUSY;
572 spin_unlock_irq(&dev->lock);
573
574requeue_req:
575 /* queue a request */
576 req = dev->rx_req[0];
Pavankumar Kondetie79aa682012-12-19 20:19:35 +0530577 req->length = mtp_rx_req_len;
Benoit Gobyf0fbc482011-12-19 14:37:50 -0800578 dev->rx_done = 0;
579 ret = usb_ep_queue(dev->ep_out, req, GFP_KERNEL);
580 if (ret < 0) {
581 r = -EIO;
582 goto done;
583 } else {
584 DBG(cdev, "rx %p queue\n", req);
585 }
586
587 /* wait for a request to complete */
Rajkumar Raghupathy20298e02012-03-09 12:22:36 +0530588 ret = wait_event_interruptible(dev->read_wq,
589 dev->rx_done || dev->state != STATE_BUSY);
590 if (dev->state == STATE_CANCELED) {
591 r = -ECANCELED;
592 if (!dev->rx_done)
593 usb_ep_dequeue(dev->ep_out, req);
594 spin_lock_irq(&dev->lock);
595 dev->state = STATE_CANCELED;
596 spin_unlock_irq(&dev->lock);
597 goto done;
598 }
Benoit Gobyf0fbc482011-12-19 14:37:50 -0800599 if (ret < 0) {
600 r = ret;
601 usb_ep_dequeue(dev->ep_out, req);
602 goto done;
603 }
604 if (dev->state == STATE_BUSY) {
605 /* If we got a 0-len packet, throw it back and try again. */
606 if (req->actual == 0)
607 goto requeue_req;
608
609 DBG(cdev, "rx %p %d\n", req, req->actual);
610 xfer = (req->actual < count) ? req->actual : count;
611 r = xfer;
612 if (copy_to_user(buf, req->buf, xfer))
613 r = -EFAULT;
614 } else
615 r = -EIO;
616
617done:
618 spin_lock_irq(&dev->lock);
619 if (dev->state == STATE_CANCELED)
620 r = -ECANCELED;
621 else if (dev->state != STATE_OFFLINE)
622 dev->state = STATE_READY;
623 spin_unlock_irq(&dev->lock);
624
625 DBG(cdev, "mtp_read returning %d\n", r);
626 return r;
627}
628
629static ssize_t mtp_write(struct file *fp, const char __user *buf,
630 size_t count, loff_t *pos)
631{
632 struct mtp_dev *dev = fp->private_data;
633 struct usb_composite_dev *cdev = dev->cdev;
634 struct usb_request *req = 0;
635 int r = count, xfer;
636 int sendZLP = 0;
637 int ret;
638
639 DBG(cdev, "mtp_write(%d)\n", count);
640
641 spin_lock_irq(&dev->lock);
642 if (dev->state == STATE_CANCELED) {
643 /* report cancelation to userspace */
644 dev->state = STATE_READY;
645 spin_unlock_irq(&dev->lock);
646 return -ECANCELED;
647 }
648 if (dev->state == STATE_OFFLINE) {
649 spin_unlock_irq(&dev->lock);
650 return -ENODEV;
651 }
652 dev->state = STATE_BUSY;
653 spin_unlock_irq(&dev->lock);
654
655 /* we need to send a zero length packet to signal the end of transfer
656 * if the transfer size is aligned to a packet boundary.
657 */
658 if ((count & (dev->ep_in->maxpacket - 1)) == 0)
659 sendZLP = 1;
660
661 while (count > 0 || sendZLP) {
662 /* so we exit after sending ZLP */
663 if (count == 0)
664 sendZLP = 0;
665
666 if (dev->state != STATE_BUSY) {
667 DBG(cdev, "mtp_write dev->error\n");
668 r = -EIO;
669 break;
670 }
671
672 /* get an idle tx request to use */
673 req = 0;
674 ret = wait_event_interruptible(dev->write_wq,
675 ((req = mtp_req_get(dev, &dev->tx_idle))
676 || dev->state != STATE_BUSY));
677 if (!req) {
678 r = ret;
679 break;
680 }
681
682 if (count > MTP_BULK_BUFFER_SIZE)
683 xfer = MTP_BULK_BUFFER_SIZE;
684 else
685 xfer = count;
686 if (xfer && copy_from_user(req->buf, buf, xfer)) {
687 r = -EFAULT;
688 break;
689 }
690
691 req->length = xfer;
692 ret = usb_ep_queue(dev->ep_in, req, GFP_KERNEL);
693 if (ret < 0) {
694 DBG(cdev, "mtp_write: xfer error %d\n", ret);
695 r = -EIO;
696 break;
697 }
698
699 buf += xfer;
700 count -= xfer;
701
702 /* zero this so we don't try to free it on error exit */
703 req = 0;
704 }
705
706 if (req)
707 mtp_req_put(dev, &dev->tx_idle, req);
708
709 spin_lock_irq(&dev->lock);
710 if (dev->state == STATE_CANCELED)
711 r = -ECANCELED;
712 else if (dev->state != STATE_OFFLINE)
713 dev->state = STATE_READY;
714 spin_unlock_irq(&dev->lock);
715
716 DBG(cdev, "mtp_write returning %d\n", r);
717 return r;
718}
719
720/* read from a local file and write to USB */
721static void send_file_work(struct work_struct *data)
722{
723 struct mtp_dev *dev = container_of(data, struct mtp_dev,
724 send_file_work);
725 struct usb_composite_dev *cdev = dev->cdev;
726 struct usb_request *req = 0;
727 struct mtp_data_header *header;
728 struct file *filp;
729 loff_t offset;
730 int64_t count;
731 int xfer, ret, hdr_size;
732 int r = 0;
733 int sendZLP = 0;
734
735 /* read our parameters */
736 smp_rmb();
737 filp = dev->xfer_file;
738 offset = dev->xfer_file_offset;
739 count = dev->xfer_file_length;
740
741 DBG(cdev, "send_file_work(%lld %lld)\n", offset, count);
742
743 if (dev->xfer_send_header) {
744 hdr_size = sizeof(struct mtp_data_header);
745 count += hdr_size;
746 } else {
747 hdr_size = 0;
748 }
749
750 /* we need to send a zero length packet to signal the end of transfer
751 * if the transfer size is aligned to a packet boundary.
752 */
753 if ((count & (dev->ep_in->maxpacket - 1)) == 0)
754 sendZLP = 1;
755
756 while (count > 0 || sendZLP) {
757 /* so we exit after sending ZLP */
758 if (count == 0)
759 sendZLP = 0;
760
761 /* get an idle tx request to use */
762 req = 0;
763 ret = wait_event_interruptible(dev->write_wq,
764 (req = mtp_req_get(dev, &dev->tx_idle))
765 || dev->state != STATE_BUSY);
766 if (dev->state == STATE_CANCELED) {
767 r = -ECANCELED;
768 break;
769 }
770 if (!req) {
771 r = ret;
772 break;
773 }
774
775 if (count > MTP_BULK_BUFFER_SIZE)
776 xfer = MTP_BULK_BUFFER_SIZE;
777 else
778 xfer = count;
779
780 if (hdr_size) {
781 /* prepend MTP data header */
782 header = (struct mtp_data_header *)req->buf;
783 header->length = __cpu_to_le32(count);
784 header->type = __cpu_to_le16(2); /* data packet */
785 header->command = __cpu_to_le16(dev->xfer_command);
786 header->transaction_id =
787 __cpu_to_le32(dev->xfer_transaction_id);
788 }
789
790 ret = vfs_read(filp, req->buf + hdr_size, xfer - hdr_size,
791 &offset);
792 if (ret < 0) {
793 r = ret;
794 break;
795 }
796 xfer = ret + hdr_size;
797 hdr_size = 0;
798
799 req->length = xfer;
800 ret = usb_ep_queue(dev->ep_in, req, GFP_KERNEL);
801 if (ret < 0) {
802 DBG(cdev, "send_file_work: xfer error %d\n", ret);
Vijayavardhan Vennapusa00972232012-05-18 11:18:40 +0530803 if (dev->state != STATE_OFFLINE)
804 dev->state = STATE_ERROR;
Benoit Gobyf0fbc482011-12-19 14:37:50 -0800805 r = -EIO;
806 break;
807 }
808
809 count -= xfer;
810
811 /* zero this so we don't try to free it on error exit */
812 req = 0;
813 }
814
815 if (req)
816 mtp_req_put(dev, &dev->tx_idle, req);
817
818 DBG(cdev, "send_file_work returning %d\n", r);
819 /* write the result */
820 dev->xfer_result = r;
821 smp_wmb();
822}
823
824/* read from USB and write to a local file */
825static void receive_file_work(struct work_struct *data)
826{
827 struct mtp_dev *dev = container_of(data, struct mtp_dev,
828 receive_file_work);
829 struct usb_composite_dev *cdev = dev->cdev;
830 struct usb_request *read_req = NULL, *write_req = NULL;
831 struct file *filp;
832 loff_t offset;
833 int64_t count;
834 int ret, cur_buf = 0;
835 int r = 0;
836
837 /* read our parameters */
838 smp_rmb();
839 filp = dev->xfer_file;
840 offset = dev->xfer_file_offset;
841 count = dev->xfer_file_length;
842
843 DBG(cdev, "receive_file_work(%lld)\n", count);
Manu Gautambe803b42012-10-03 18:49:33 +0530844 if (!IS_ALIGNED(count, dev->ep_out->maxpacket))
845 DBG(cdev, "%s- count(%lld) not multiple of mtu(%d)\n", __func__,
846 count, dev->ep_out->maxpacket);
Benoit Gobyf0fbc482011-12-19 14:37:50 -0800847
848 while (count > 0 || write_req) {
849 if (count > 0) {
850 /* queue a request */
851 read_req = dev->rx_req[cur_buf];
852 cur_buf = (cur_buf + 1) % RX_REQ_MAX;
853
Manu Gautambe803b42012-10-03 18:49:33 +0530854 /* some h/w expects size to be aligned to ep's MTU */
Pavankumar Kondetie79aa682012-12-19 20:19:35 +0530855 read_req->length = mtp_rx_req_len;
Manu Gautambe803b42012-10-03 18:49:33 +0530856
Benoit Gobyf0fbc482011-12-19 14:37:50 -0800857 dev->rx_done = 0;
858 ret = usb_ep_queue(dev->ep_out, read_req, GFP_KERNEL);
859 if (ret < 0) {
860 r = -EIO;
Vijayavardhan Vennapusa00972232012-05-18 11:18:40 +0530861 if (dev->state != STATE_OFFLINE)
862 dev->state = STATE_ERROR;
Benoit Gobyf0fbc482011-12-19 14:37:50 -0800863 break;
864 }
865 }
866
867 if (write_req) {
868 DBG(cdev, "rx %p %d\n", write_req, write_req->actual);
869 ret = vfs_write(filp, write_req->buf, write_req->actual,
870 &offset);
871 DBG(cdev, "vfs_write %d\n", ret);
872 if (ret != write_req->actual) {
873 r = -EIO;
Vijayavardhan Vennapusa00972232012-05-18 11:18:40 +0530874 if (dev->state != STATE_OFFLINE)
875 dev->state = STATE_ERROR;
Benoit Gobyf0fbc482011-12-19 14:37:50 -0800876 break;
877 }
878 write_req = NULL;
879 }
880
881 if (read_req) {
882 /* wait for our last read to complete */
883 ret = wait_event_interruptible(dev->read_wq,
884 dev->rx_done || dev->state != STATE_BUSY);
Rajkumar Raghupathy7c3c45b2012-07-24 16:13:36 +0530885 if (dev->state == STATE_CANCELED
886 || dev->state == STATE_OFFLINE) {
Benoit Gobyf0fbc482011-12-19 14:37:50 -0800887 r = -ECANCELED;
888 if (!dev->rx_done)
889 usb_ep_dequeue(dev->ep_out, read_req);
890 break;
891 }
Manu Gautambe803b42012-10-03 18:49:33 +0530892 /* Check if we aligned the size due to MTU constraint */
893 if (count < read_req->length)
894 read_req->actual = (read_req->actual > count ?
895 count : read_req->actual);
Benoit Gobyf0fbc482011-12-19 14:37:50 -0800896 /* if xfer_file_length is 0xFFFFFFFF, then we read until
897 * we get a zero length packet
898 */
899 if (count != 0xFFFFFFFF)
900 count -= read_req->actual;
901 if (read_req->actual < read_req->length) {
902 /*
903 * short packet is used to signal EOF for
904 * sizes > 4 gig
905 */
906 DBG(cdev, "got short packet\n");
907 count = 0;
908 }
909
910 write_req = read_req;
911 read_req = NULL;
912 }
913 }
914
915 DBG(cdev, "receive_file_work returning %d\n", r);
916 /* write the result */
917 dev->xfer_result = r;
918 smp_wmb();
919}
920
921static int mtp_send_event(struct mtp_dev *dev, struct mtp_event *event)
922{
923 struct usb_request *req = NULL;
924 int ret;
925 int length = event->length;
926
927 DBG(dev->cdev, "mtp_send_event(%d)\n", event->length);
928
929 if (length < 0 || length > INTR_BUFFER_SIZE)
930 return -EINVAL;
931 if (dev->state == STATE_OFFLINE)
932 return -ENODEV;
933
934 ret = wait_event_interruptible_timeout(dev->intr_wq,
935 (req = mtp_req_get(dev, &dev->intr_idle)),
936 msecs_to_jiffies(1000));
937 if (!req)
938 return -ETIME;
939
940 if (copy_from_user(req->buf, (void __user *)event->data, length)) {
941 mtp_req_put(dev, &dev->intr_idle, req);
942 return -EFAULT;
943 }
944 req->length = length;
945 ret = usb_ep_queue(dev->ep_intr, req, GFP_KERNEL);
946 if (ret)
947 mtp_req_put(dev, &dev->intr_idle, req);
948
949 return ret;
950}
951
952static long mtp_ioctl(struct file *fp, unsigned code, unsigned long value)
953{
954 struct mtp_dev *dev = fp->private_data;
955 struct file *filp = NULL;
956 int ret = -EINVAL;
957
958 if (mtp_lock(&dev->ioctl_excl))
959 return -EBUSY;
960
961 switch (code) {
962 case MTP_SEND_FILE:
963 case MTP_RECEIVE_FILE:
964 case MTP_SEND_FILE_WITH_HEADER:
965 {
966 struct mtp_file_range mfr;
967 struct work_struct *work;
968
969 spin_lock_irq(&dev->lock);
970 if (dev->state == STATE_CANCELED) {
971 /* report cancelation to userspace */
972 dev->state = STATE_READY;
973 spin_unlock_irq(&dev->lock);
974 ret = -ECANCELED;
975 goto out;
976 }
977 if (dev->state == STATE_OFFLINE) {
978 spin_unlock_irq(&dev->lock);
979 ret = -ENODEV;
980 goto out;
981 }
982 dev->state = STATE_BUSY;
983 spin_unlock_irq(&dev->lock);
984
985 if (copy_from_user(&mfr, (void __user *)value, sizeof(mfr))) {
986 ret = -EFAULT;
987 goto fail;
988 }
989 /* hold a reference to the file while we are working with it */
990 filp = fget(mfr.fd);
991 if (!filp) {
992 ret = -EBADF;
993 goto fail;
994 }
995
996 /* write the parameters */
997 dev->xfer_file = filp;
998 dev->xfer_file_offset = mfr.offset;
999 dev->xfer_file_length = mfr.length;
1000 smp_wmb();
1001
1002 if (code == MTP_SEND_FILE_WITH_HEADER) {
1003 work = &dev->send_file_work;
1004 dev->xfer_send_header = 1;
1005 dev->xfer_command = mfr.command;
1006 dev->xfer_transaction_id = mfr.transaction_id;
1007 } else if (code == MTP_SEND_FILE) {
1008 work = &dev->send_file_work;
1009 dev->xfer_send_header = 0;
1010 } else {
1011 work = &dev->receive_file_work;
1012 }
1013
1014 /* We do the file transfer on a work queue so it will run
1015 * in kernel context, which is necessary for vfs_read and
1016 * vfs_write to use our buffers in the kernel address space.
1017 */
1018 queue_work(dev->wq, work);
1019 /* wait for operation to complete */
1020 flush_workqueue(dev->wq);
1021 fput(filp);
1022
1023 /* read the result */
1024 smp_rmb();
1025 ret = dev->xfer_result;
1026 break;
1027 }
1028 case MTP_SEND_EVENT:
1029 {
1030 struct mtp_event event;
1031 /* return here so we don't change dev->state below,
1032 * which would interfere with bulk transfer state.
1033 */
1034 if (copy_from_user(&event, (void __user *)value, sizeof(event)))
1035 ret = -EFAULT;
1036 else
1037 ret = mtp_send_event(dev, &event);
1038 goto out;
1039 }
1040 }
1041
1042fail:
1043 spin_lock_irq(&dev->lock);
1044 if (dev->state == STATE_CANCELED)
1045 ret = -ECANCELED;
1046 else if (dev->state != STATE_OFFLINE)
1047 dev->state = STATE_READY;
1048 spin_unlock_irq(&dev->lock);
1049out:
1050 mtp_unlock(&dev->ioctl_excl);
1051 DBG(dev->cdev, "ioctl returning %d\n", ret);
1052 return ret;
1053}
1054
1055static int mtp_open(struct inode *ip, struct file *fp)
1056{
1057 printk(KERN_INFO "mtp_open\n");
1058 if (mtp_lock(&_mtp_dev->open_excl))
1059 return -EBUSY;
1060
1061 /* clear any error condition */
1062 if (_mtp_dev->state != STATE_OFFLINE)
1063 _mtp_dev->state = STATE_READY;
1064
1065 fp->private_data = _mtp_dev;
1066 return 0;
1067}
1068
1069static int mtp_release(struct inode *ip, struct file *fp)
1070{
1071 printk(KERN_INFO "mtp_release\n");
1072
1073 mtp_unlock(&_mtp_dev->open_excl);
1074 return 0;
1075}
1076
1077/* file operations for /dev/mtp_usb */
1078static const struct file_operations mtp_fops = {
1079 .owner = THIS_MODULE,
1080 .read = mtp_read,
1081 .write = mtp_write,
1082 .unlocked_ioctl = mtp_ioctl,
1083 .open = mtp_open,
1084 .release = mtp_release,
1085};
1086
1087static struct miscdevice mtp_device = {
1088 .minor = MISC_DYNAMIC_MINOR,
1089 .name = mtp_shortname,
1090 .fops = &mtp_fops,
1091};
1092
1093static int mtp_ctrlrequest(struct usb_composite_dev *cdev,
1094 const struct usb_ctrlrequest *ctrl)
1095{
1096 struct mtp_dev *dev = _mtp_dev;
1097 int value = -EOPNOTSUPP;
1098 u16 w_index = le16_to_cpu(ctrl->wIndex);
1099 u16 w_value = le16_to_cpu(ctrl->wValue);
1100 u16 w_length = le16_to_cpu(ctrl->wLength);
1101 unsigned long flags;
1102
1103 VDBG(cdev, "mtp_ctrlrequest "
1104 "%02x.%02x v%04x i%04x l%u\n",
1105 ctrl->bRequestType, ctrl->bRequest,
1106 w_value, w_index, w_length);
1107
1108 /* Handle MTP OS string */
1109 if (ctrl->bRequestType ==
1110 (USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE)
1111 && ctrl->bRequest == USB_REQ_GET_DESCRIPTOR
1112 && (w_value >> 8) == USB_DT_STRING
1113 && (w_value & 0xFF) == MTP_OS_STRING_ID) {
1114 value = (w_length < sizeof(mtp_os_string)
1115 ? w_length : sizeof(mtp_os_string));
1116 memcpy(cdev->req->buf, mtp_os_string, value);
1117 } else if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_VENDOR) {
1118 /* Handle MTP OS descriptor */
1119 DBG(cdev, "vendor request: %d index: %d value: %d length: %d\n",
1120 ctrl->bRequest, w_index, w_value, w_length);
1121
1122 if (ctrl->bRequest == 1
1123 && (ctrl->bRequestType & USB_DIR_IN)
1124 && (w_index == 4 || w_index == 5)) {
1125 value = (w_length < sizeof(mtp_ext_config_desc) ?
1126 w_length : sizeof(mtp_ext_config_desc));
1127 memcpy(cdev->req->buf, &mtp_ext_config_desc, value);
1128 }
1129 } else if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_CLASS) {
1130 DBG(cdev, "class request: %d index: %d value: %d length: %d\n",
1131 ctrl->bRequest, w_index, w_value, w_length);
1132
1133 if (ctrl->bRequest == MTP_REQ_CANCEL && w_index == 0
1134 && w_value == 0) {
1135 DBG(cdev, "MTP_REQ_CANCEL\n");
1136
1137 spin_lock_irqsave(&dev->lock, flags);
1138 if (dev->state == STATE_BUSY) {
1139 dev->state = STATE_CANCELED;
1140 wake_up(&dev->read_wq);
1141 wake_up(&dev->write_wq);
1142 }
1143 spin_unlock_irqrestore(&dev->lock, flags);
1144
1145 /* We need to queue a request to read the remaining
1146 * bytes, but we don't actually need to look at
1147 * the contents.
1148 */
1149 value = w_length;
1150 } else if (ctrl->bRequest == MTP_REQ_GET_DEVICE_STATUS
1151 && w_index == 0 && w_value == 0) {
1152 struct mtp_device_status *status = cdev->req->buf;
1153 status->wLength =
1154 __constant_cpu_to_le16(sizeof(*status));
1155
1156 DBG(cdev, "MTP_REQ_GET_DEVICE_STATUS\n");
1157 spin_lock_irqsave(&dev->lock, flags);
1158 /* device status is "busy" until we report
1159 * the cancelation to userspace
1160 */
1161 if (dev->state == STATE_CANCELED)
1162 status->wCode =
1163 __cpu_to_le16(MTP_RESPONSE_DEVICE_BUSY);
1164 else
1165 status->wCode =
1166 __cpu_to_le16(MTP_RESPONSE_OK);
1167 spin_unlock_irqrestore(&dev->lock, flags);
1168 value = sizeof(*status);
1169 }
1170 }
1171
1172 /* respond with data transfer or status phase? */
1173 if (value >= 0) {
1174 int rc;
1175 cdev->req->zero = value < w_length;
1176 cdev->req->length = value;
1177 rc = usb_ep_queue(cdev->gadget->ep0, cdev->req, GFP_ATOMIC);
1178 if (rc < 0)
1179 ERROR(cdev, "%s: response queue error\n", __func__);
1180 }
1181 return value;
1182}
1183
1184static int
1185mtp_function_bind(struct usb_configuration *c, struct usb_function *f)
1186{
1187 struct usb_composite_dev *cdev = c->cdev;
1188 struct mtp_dev *dev = func_to_mtp(f);
1189 int id;
1190 int ret;
1191
1192 dev->cdev = cdev;
1193 DBG(cdev, "mtp_function_bind dev: %p\n", dev);
1194
1195 /* allocate interface ID(s) */
1196 id = usb_interface_id(c, f);
1197 if (id < 0)
1198 return id;
1199 mtp_interface_desc.bInterfaceNumber = id;
1200
1201 /* allocate endpoints */
1202 ret = mtp_create_bulk_endpoints(dev, &mtp_fullspeed_in_desc,
1203 &mtp_fullspeed_out_desc, &mtp_intr_desc);
1204 if (ret)
1205 return ret;
1206
1207 /* support high speed hardware */
1208 if (gadget_is_dualspeed(c->cdev->gadget)) {
1209 mtp_highspeed_in_desc.bEndpointAddress =
1210 mtp_fullspeed_in_desc.bEndpointAddress;
1211 mtp_highspeed_out_desc.bEndpointAddress =
1212 mtp_fullspeed_out_desc.bEndpointAddress;
1213 }
1214
Pavankumar Kondeti6f94bc92012-08-03 09:34:32 +05301215 /* support super speed hardware */
1216 if (gadget_is_superspeed(c->cdev->gadget)) {
1217 mtp_superspeed_in_desc.bEndpointAddress =
1218 mtp_fullspeed_in_desc.bEndpointAddress;
1219 mtp_superspeed_out_desc.bEndpointAddress =
1220 mtp_fullspeed_out_desc.bEndpointAddress;
1221 }
1222
Benoit Gobyf0fbc482011-12-19 14:37:50 -08001223 DBG(cdev, "%s speed %s: IN/%s, OUT/%s\n",
1224 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
1225 f->name, dev->ep_in->name, dev->ep_out->name);
1226 return 0;
1227}
1228
1229static void
1230mtp_function_unbind(struct usb_configuration *c, struct usb_function *f)
1231{
1232 struct mtp_dev *dev = func_to_mtp(f);
1233 struct usb_request *req;
1234 int i;
1235
1236 while ((req = mtp_req_get(dev, &dev->tx_idle)))
1237 mtp_request_free(req, dev->ep_in);
1238 for (i = 0; i < RX_REQ_MAX; i++)
1239 mtp_request_free(dev->rx_req[i], dev->ep_out);
1240 while ((req = mtp_req_get(dev, &dev->intr_idle)))
1241 mtp_request_free(req, dev->ep_intr);
1242 dev->state = STATE_OFFLINE;
1243}
1244
1245static int mtp_function_set_alt(struct usb_function *f,
1246 unsigned intf, unsigned alt)
1247{
1248 struct mtp_dev *dev = func_to_mtp(f);
1249 struct usb_composite_dev *cdev = f->config->cdev;
1250 int ret;
1251
1252 DBG(cdev, "mtp_function_set_alt intf: %d alt: %d\n", intf, alt);
1253
1254 ret = config_ep_by_speed(cdev->gadget, f, dev->ep_in);
Benoit Gobyf0fbc482011-12-19 14:37:50 -08001255 if (ret) {
Tatyana Brokhman31ac3522011-06-28 15:33:50 +02001256 dev->ep_in->desc = NULL;
1257 ERROR(cdev, "config_ep_by_speed failes for ep %s, result %d\n",
1258 dev->ep_in->name, ret);
1259 return ret;
1260 }
1261 ret = usb_ep_enable(dev->ep_in);
1262 if (ret) {
1263 ERROR(cdev, "failed to enable ep %s, result %d\n",
1264 dev->ep_in->name, ret);
Benoit Gobyf0fbc482011-12-19 14:37:50 -08001265 return ret;
1266 }
1267
Tatyana Brokhman31ac3522011-06-28 15:33:50 +02001268 ret = config_ep_by_speed(cdev->gadget, f, dev->ep_out);
1269 if (ret) {
1270 dev->ep_out->desc = NULL;
1271 ERROR(cdev, "config_ep_by_speed failes for ep %s, result %d\n",
1272 dev->ep_out->name, ret);
Tatyana Brokhmanebd3f392011-06-28 16:33:50 +03001273 usb_ep_disable(dev->ep_in);
Benoit Gobyf0fbc482011-12-19 14:37:50 -08001274 return ret;
Tatyana Brokhmanebd3f392011-06-28 16:33:50 +03001275 }
Tatyana Brokhman31ac3522011-06-28 15:33:50 +02001276 ret = usb_ep_enable(dev->ep_out);
1277 if (ret) {
1278 ERROR(cdev, "failed to enable ep %s, result %d\n",
1279 dev->ep_out->name, ret);
Tatyana Brokhmanebd3f392011-06-28 16:33:50 +03001280 usb_ep_disable(dev->ep_in);
1281 return ret;
1282 }
Tatyana Brokhmancf709c12011-06-28 16:33:48 +03001283 dev->ep_intr->desc = &mtp_intr_desc;
Benoit Gobyf0fbc482011-12-19 14:37:50 -08001284 ret = usb_ep_enable(dev->ep_intr);
1285 if (ret) {
1286 usb_ep_disable(dev->ep_out);
1287 usb_ep_disable(dev->ep_in);
1288 return ret;
1289 }
1290 dev->state = STATE_READY;
1291
1292 /* readers may be blocked waiting for us to go online */
1293 wake_up(&dev->read_wq);
1294 return 0;
1295}
1296
1297static void mtp_function_disable(struct usb_function *f)
1298{
1299 struct mtp_dev *dev = func_to_mtp(f);
1300 struct usb_composite_dev *cdev = dev->cdev;
1301
1302 DBG(cdev, "mtp_function_disable\n");
1303 dev->state = STATE_OFFLINE;
1304 usb_ep_disable(dev->ep_in);
1305 usb_ep_disable(dev->ep_out);
1306 usb_ep_disable(dev->ep_intr);
1307
1308 /* readers may be blocked waiting for us to go online */
1309 wake_up(&dev->read_wq);
1310
1311 VDBG(cdev, "%s disabled\n", dev->function.name);
1312}
1313
1314static int mtp_bind_config(struct usb_configuration *c, bool ptp_config)
1315{
1316 struct mtp_dev *dev = _mtp_dev;
1317 int ret = 0;
1318
1319 printk(KERN_INFO "mtp_bind_config\n");
1320
1321 /* allocate a string ID for our interface */
1322 if (mtp_string_defs[INTERFACE_STRING_INDEX].id == 0) {
1323 ret = usb_string_id(c->cdev);
1324 if (ret < 0)
1325 return ret;
1326 mtp_string_defs[INTERFACE_STRING_INDEX].id = ret;
1327 mtp_interface_desc.iInterface = ret;
1328 }
1329
1330 dev->cdev = c->cdev;
1331 dev->function.name = "mtp";
1332 dev->function.strings = mtp_strings;
1333 if (ptp_config) {
1334 dev->function.descriptors = fs_ptp_descs;
1335 dev->function.hs_descriptors = hs_ptp_descs;
Pavankumar Kondeti6f94bc92012-08-03 09:34:32 +05301336 if (gadget_is_superspeed(c->cdev->gadget))
1337 dev->function.ss_descriptors = ss_ptp_descs;
Benoit Gobyf0fbc482011-12-19 14:37:50 -08001338 } else {
1339 dev->function.descriptors = fs_mtp_descs;
1340 dev->function.hs_descriptors = hs_mtp_descs;
Pavankumar Kondeti6f94bc92012-08-03 09:34:32 +05301341 if (gadget_is_superspeed(c->cdev->gadget))
1342 dev->function.ss_descriptors = ss_mtp_descs;
Benoit Gobyf0fbc482011-12-19 14:37:50 -08001343 }
1344 dev->function.bind = mtp_function_bind;
1345 dev->function.unbind = mtp_function_unbind;
1346 dev->function.set_alt = mtp_function_set_alt;
1347 dev->function.disable = mtp_function_disable;
1348
1349 return usb_add_function(c, &dev->function);
1350}
1351
1352static int mtp_setup(void)
1353{
1354 struct mtp_dev *dev;
1355 int ret;
1356
1357 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1358 if (!dev)
1359 return -ENOMEM;
1360
1361 spin_lock_init(&dev->lock);
1362 init_waitqueue_head(&dev->read_wq);
1363 init_waitqueue_head(&dev->write_wq);
1364 init_waitqueue_head(&dev->intr_wq);
1365 atomic_set(&dev->open_excl, 0);
1366 atomic_set(&dev->ioctl_excl, 0);
1367 INIT_LIST_HEAD(&dev->tx_idle);
1368 INIT_LIST_HEAD(&dev->intr_idle);
1369
1370 dev->wq = create_singlethread_workqueue("f_mtp");
1371 if (!dev->wq) {
1372 ret = -ENOMEM;
1373 goto err1;
1374 }
1375 INIT_WORK(&dev->send_file_work, send_file_work);
1376 INIT_WORK(&dev->receive_file_work, receive_file_work);
1377
1378 _mtp_dev = dev;
1379
1380 ret = misc_register(&mtp_device);
1381 if (ret)
1382 goto err2;
1383
1384 return 0;
1385
1386err2:
1387 destroy_workqueue(dev->wq);
1388err1:
1389 _mtp_dev = NULL;
1390 kfree(dev);
1391 printk(KERN_ERR "mtp gadget driver failed to initialize\n");
1392 return ret;
1393}
1394
1395static void mtp_cleanup(void)
1396{
1397 struct mtp_dev *dev = _mtp_dev;
1398
1399 if (!dev)
1400 return;
1401
1402 misc_deregister(&mtp_device);
1403 destroy_workqueue(dev->wq);
1404 _mtp_dev = NULL;
1405 kfree(dev);
1406}