blob: e21d3e05a4af72fda449cb92673531915431bbee [file] [log] [blame]
Benoit Goby7a6d39e2011-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>
Badhri Jagan Sridharaneab46c42014-11-17 21:11:23 -080038#include <linux/configfs.h>
39#include <linux/usb/composite.h>
40
41#include "configfs.h"
Benoit Goby7a6d39e2011-12-19 14:37:50 -080042
43#define MTP_BULK_BUFFER_SIZE 16384
44#define INTR_BUFFER_SIZE 28
Badhri Jagan Sridharaneab46c42014-11-17 21:11:23 -080045#define MAX_INST_NAME_LEN 40
Benoit Goby7a6d39e2011-12-19 14:37:50 -080046
47/* String IDs */
48#define INTERFACE_STRING_INDEX 0
49
50/* values for mtp_dev.state */
51#define STATE_OFFLINE 0 /* initial state, disconnected */
52#define STATE_READY 1 /* ready for userspace calls */
53#define STATE_BUSY 2 /* processing userspace calls */
54#define STATE_CANCELED 3 /* transaction canceled by host */
55#define STATE_ERROR 4 /* error from completion routine */
56
57/* number of tx and rx requests to allocate */
58#define TX_REQ_MAX 4
59#define RX_REQ_MAX 2
60#define INTR_REQ_MAX 5
61
62/* ID for Microsoft MTP OS String */
63#define MTP_OS_STRING_ID 0xEE
64
65/* MTP class reqeusts */
66#define MTP_REQ_CANCEL 0x64
67#define MTP_REQ_GET_EXT_EVENT_DATA 0x65
68#define MTP_REQ_RESET 0x66
69#define MTP_REQ_GET_DEVICE_STATUS 0x67
70
71/* constants for device status */
72#define MTP_RESPONSE_OK 0x2001
73#define MTP_RESPONSE_DEVICE_BUSY 0x2019
Badhri Jagan Sridharaneab46c42014-11-17 21:11:23 -080074#define DRIVER_NAME "mtp"
Benoit Goby7a6d39e2011-12-19 14:37:50 -080075
Badhri Jagan Sridharaneab46c42014-11-17 21:11:23 -080076static const char mtp_shortname[] = DRIVER_NAME "_usb";
Benoit Goby7a6d39e2011-12-19 14:37:50 -080077
78struct mtp_dev {
79 struct usb_function function;
80 struct usb_composite_dev *cdev;
81 spinlock_t lock;
82
83 struct usb_ep *ep_in;
84 struct usb_ep *ep_out;
85 struct usb_ep *ep_intr;
86
87 int state;
88
89 /* synchronize access to our device file */
90 atomic_t open_excl;
91 /* to enforce only one ioctl at a time */
92 atomic_t ioctl_excl;
93
94 struct list_head tx_idle;
95 struct list_head intr_idle;
96
97 wait_queue_head_t read_wq;
98 wait_queue_head_t write_wq;
99 wait_queue_head_t intr_wq;
100 struct usb_request *rx_req[RX_REQ_MAX];
101 int rx_done;
102
103 /* for processing MTP_SEND_FILE, MTP_RECEIVE_FILE and
104 * MTP_SEND_FILE_WITH_HEADER ioctls on a work queue
105 */
106 struct workqueue_struct *wq;
107 struct work_struct send_file_work;
108 struct work_struct receive_file_work;
109 struct file *xfer_file;
110 loff_t xfer_file_offset;
111 int64_t xfer_file_length;
112 unsigned xfer_send_header;
113 uint16_t xfer_command;
114 uint32_t xfer_transaction_id;
115 int xfer_result;
116};
117
118static struct usb_interface_descriptor mtp_interface_desc = {
119 .bLength = USB_DT_INTERFACE_SIZE,
120 .bDescriptorType = USB_DT_INTERFACE,
121 .bInterfaceNumber = 0,
122 .bNumEndpoints = 3,
123 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
124 .bInterfaceSubClass = USB_SUBCLASS_VENDOR_SPEC,
125 .bInterfaceProtocol = 0,
126};
127
128static struct usb_interface_descriptor ptp_interface_desc = {
129 .bLength = USB_DT_INTERFACE_SIZE,
130 .bDescriptorType = USB_DT_INTERFACE,
131 .bInterfaceNumber = 0,
132 .bNumEndpoints = 3,
133 .bInterfaceClass = USB_CLASS_STILL_IMAGE,
134 .bInterfaceSubClass = 1,
135 .bInterfaceProtocol = 1,
136};
137
Mark Kuob115a6f2015-08-20 13:01:46 +0800138static struct usb_endpoint_descriptor mtp_ss_in_desc = {
139 .bLength = USB_DT_ENDPOINT_SIZE,
140 .bDescriptorType = USB_DT_ENDPOINT,
141 .bEndpointAddress = USB_DIR_IN,
142 .bmAttributes = USB_ENDPOINT_XFER_BULK,
143 .wMaxPacketSize = __constant_cpu_to_le16(1024),
144};
145
146static struct usb_ss_ep_comp_descriptor mtp_ss_in_comp_desc = {
147 .bLength = sizeof(mtp_ss_in_comp_desc),
148 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
149 /* .bMaxBurst = DYNAMIC, */
150};
151
152static struct usb_endpoint_descriptor mtp_ss_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_ss_out_comp_desc = {
161 .bLength = sizeof(mtp_ss_out_comp_desc),
162 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
163 /* .bMaxBurst = DYNAMIC, */
164};
165
Benoit Goby7a6d39e2011-12-19 14:37:50 -0800166static struct usb_endpoint_descriptor mtp_highspeed_in_desc = {
167 .bLength = USB_DT_ENDPOINT_SIZE,
168 .bDescriptorType = USB_DT_ENDPOINT,
169 .bEndpointAddress = USB_DIR_IN,
170 .bmAttributes = USB_ENDPOINT_XFER_BULK,
171 .wMaxPacketSize = __constant_cpu_to_le16(512),
172};
173
174static struct usb_endpoint_descriptor mtp_highspeed_out_desc = {
175 .bLength = USB_DT_ENDPOINT_SIZE,
176 .bDescriptorType = USB_DT_ENDPOINT,
177 .bEndpointAddress = USB_DIR_OUT,
178 .bmAttributes = USB_ENDPOINT_XFER_BULK,
179 .wMaxPacketSize = __constant_cpu_to_le16(512),
180};
181
182static struct usb_endpoint_descriptor mtp_fullspeed_in_desc = {
183 .bLength = USB_DT_ENDPOINT_SIZE,
184 .bDescriptorType = USB_DT_ENDPOINT,
185 .bEndpointAddress = USB_DIR_IN,
186 .bmAttributes = USB_ENDPOINT_XFER_BULK,
187};
188
189static struct usb_endpoint_descriptor mtp_fullspeed_out_desc = {
190 .bLength = USB_DT_ENDPOINT_SIZE,
191 .bDescriptorType = USB_DT_ENDPOINT,
192 .bEndpointAddress = USB_DIR_OUT,
193 .bmAttributes = USB_ENDPOINT_XFER_BULK,
194};
195
196static struct usb_endpoint_descriptor mtp_intr_desc = {
197 .bLength = USB_DT_ENDPOINT_SIZE,
198 .bDescriptorType = USB_DT_ENDPOINT,
199 .bEndpointAddress = USB_DIR_IN,
200 .bmAttributes = USB_ENDPOINT_XFER_INT,
201 .wMaxPacketSize = __constant_cpu_to_le16(INTR_BUFFER_SIZE),
202 .bInterval = 6,
203};
204
Mark Kuob115a6f2015-08-20 13:01:46 +0800205static struct usb_ss_ep_comp_descriptor mtp_intr_ss_comp_desc = {
206 .bLength = sizeof(mtp_intr_ss_comp_desc),
207 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
Mark Kuo09cd36b2015-09-11 16:12:59 +0800208 .wBytesPerInterval = cpu_to_le16(INTR_BUFFER_SIZE),
Mark Kuob115a6f2015-08-20 13:01:46 +0800209};
210
Benoit Goby7a6d39e2011-12-19 14:37:50 -0800211static struct usb_descriptor_header *fs_mtp_descs[] = {
212 (struct usb_descriptor_header *) &mtp_interface_desc,
213 (struct usb_descriptor_header *) &mtp_fullspeed_in_desc,
214 (struct usb_descriptor_header *) &mtp_fullspeed_out_desc,
215 (struct usb_descriptor_header *) &mtp_intr_desc,
216 NULL,
217};
218
219static struct usb_descriptor_header *hs_mtp_descs[] = {
220 (struct usb_descriptor_header *) &mtp_interface_desc,
221 (struct usb_descriptor_header *) &mtp_highspeed_in_desc,
222 (struct usb_descriptor_header *) &mtp_highspeed_out_desc,
223 (struct usb_descriptor_header *) &mtp_intr_desc,
224 NULL,
225};
226
Mark Kuob115a6f2015-08-20 13:01:46 +0800227static struct usb_descriptor_header *ss_mtp_descs[] = {
228 (struct usb_descriptor_header *) &mtp_interface_desc,
229 (struct usb_descriptor_header *) &mtp_ss_in_desc,
230 (struct usb_descriptor_header *) &mtp_ss_in_comp_desc,
231 (struct usb_descriptor_header *) &mtp_ss_out_desc,
232 (struct usb_descriptor_header *) &mtp_ss_out_comp_desc,
233 (struct usb_descriptor_header *) &mtp_intr_desc,
234 (struct usb_descriptor_header *) &mtp_intr_ss_comp_desc,
235 NULL,
236};
237
Benoit Goby7a6d39e2011-12-19 14:37:50 -0800238static struct usb_descriptor_header *fs_ptp_descs[] = {
239 (struct usb_descriptor_header *) &ptp_interface_desc,
240 (struct usb_descriptor_header *) &mtp_fullspeed_in_desc,
241 (struct usb_descriptor_header *) &mtp_fullspeed_out_desc,
242 (struct usb_descriptor_header *) &mtp_intr_desc,
243 NULL,
244};
245
246static struct usb_descriptor_header *hs_ptp_descs[] = {
247 (struct usb_descriptor_header *) &ptp_interface_desc,
248 (struct usb_descriptor_header *) &mtp_highspeed_in_desc,
249 (struct usb_descriptor_header *) &mtp_highspeed_out_desc,
250 (struct usb_descriptor_header *) &mtp_intr_desc,
251 NULL,
252};
253
Mark Kuob115a6f2015-08-20 13:01:46 +0800254static struct usb_descriptor_header *ss_ptp_descs[] = {
255 (struct usb_descriptor_header *) &ptp_interface_desc,
256 (struct usb_descriptor_header *) &mtp_ss_in_desc,
257 (struct usb_descriptor_header *) &mtp_ss_in_comp_desc,
258 (struct usb_descriptor_header *) &mtp_ss_out_desc,
259 (struct usb_descriptor_header *) &mtp_ss_out_comp_desc,
260 (struct usb_descriptor_header *) &mtp_intr_desc,
261 (struct usb_descriptor_header *) &mtp_intr_ss_comp_desc,
262 NULL,
263};
264
Benoit Goby7a6d39e2011-12-19 14:37:50 -0800265static struct usb_string mtp_string_defs[] = {
266 /* Naming interface "MTP" so libmtp will recognize us */
267 [INTERFACE_STRING_INDEX].s = "MTP",
268 { }, /* end of list */
269};
270
271static struct usb_gadget_strings mtp_string_table = {
272 .language = 0x0409, /* en-US */
273 .strings = mtp_string_defs,
274};
275
276static struct usb_gadget_strings *mtp_strings[] = {
277 &mtp_string_table,
278 NULL,
279};
280
281/* Microsoft MTP OS String */
282static u8 mtp_os_string[] = {
283 18, /* sizeof(mtp_os_string) */
284 USB_DT_STRING,
285 /* Signature field: "MSFT100" */
286 'M', 0, 'S', 0, 'F', 0, 'T', 0, '1', 0, '0', 0, '0', 0,
287 /* vendor code */
288 1,
289 /* padding */
290 0
291};
292
293/* Microsoft Extended Configuration Descriptor Header Section */
294struct mtp_ext_config_desc_header {
295 __le32 dwLength;
296 __u16 bcdVersion;
297 __le16 wIndex;
298 __u8 bCount;
299 __u8 reserved[7];
300};
301
302/* Microsoft Extended Configuration Descriptor Function Section */
303struct mtp_ext_config_desc_function {
304 __u8 bFirstInterfaceNumber;
305 __u8 bInterfaceCount;
306 __u8 compatibleID[8];
307 __u8 subCompatibleID[8];
308 __u8 reserved[6];
309};
310
311/* MTP Extended Configuration Descriptor */
312struct {
313 struct mtp_ext_config_desc_header header;
314 struct mtp_ext_config_desc_function function;
315} mtp_ext_config_desc = {
316 .header = {
317 .dwLength = __constant_cpu_to_le32(sizeof(mtp_ext_config_desc)),
318 .bcdVersion = __constant_cpu_to_le16(0x0100),
319 .wIndex = __constant_cpu_to_le16(4),
Brian Norris557d80f2016-02-29 17:44:51 -0800320 .bCount = 1,
Benoit Goby7a6d39e2011-12-19 14:37:50 -0800321 },
322 .function = {
323 .bFirstInterfaceNumber = 0,
324 .bInterfaceCount = 1,
325 .compatibleID = { 'M', 'T', 'P' },
326 },
327};
328
329struct mtp_device_status {
330 __le16 wLength;
331 __le16 wCode;
332};
333
Colin Cross2453dad2013-11-07 13:08:15 -0800334struct mtp_data_header {
335 /* length of packet, including this header */
336 __le32 length;
337 /* container type (2 for data packet) */
338 __le16 type;
339 /* MTP command code */
340 __le16 command;
341 /* MTP transaction ID */
342 __le32 transaction_id;
343};
344
Badhri Jagan Sridharaneab46c42014-11-17 21:11:23 -0800345struct mtp_instance {
346 struct usb_function_instance func_inst;
347 const char *name;
348 struct mtp_dev *dev;
Badhri Jagan Sridharan38333b02015-10-06 20:32:01 -0700349 char mtp_ext_compat_id[16];
350 struct usb_os_desc mtp_os_desc;
Badhri Jagan Sridharaneab46c42014-11-17 21:11:23 -0800351};
352
Benoit Goby7a6d39e2011-12-19 14:37:50 -0800353/* temporary variable used between mtp_open() and mtp_gadget_bind() */
354static struct mtp_dev *_mtp_dev;
355
356static inline struct mtp_dev *func_to_mtp(struct usb_function *f)
357{
358 return container_of(f, struct mtp_dev, function);
359}
360
361static struct usb_request *mtp_request_new(struct usb_ep *ep, int buffer_size)
362{
363 struct usb_request *req = usb_ep_alloc_request(ep, GFP_KERNEL);
364 if (!req)
365 return NULL;
366
367 /* now allocate buffers for the requests */
368 req->buf = kmalloc(buffer_size, GFP_KERNEL);
369 if (!req->buf) {
370 usb_ep_free_request(ep, req);
371 return NULL;
372 }
373
374 return req;
375}
376
377static void mtp_request_free(struct usb_request *req, struct usb_ep *ep)
378{
379 if (req) {
380 kfree(req->buf);
381 usb_ep_free_request(ep, req);
382 }
383}
384
385static inline int mtp_lock(atomic_t *excl)
386{
387 if (atomic_inc_return(excl) == 1) {
388 return 0;
389 } else {
390 atomic_dec(excl);
391 return -1;
392 }
393}
394
395static inline void mtp_unlock(atomic_t *excl)
396{
397 atomic_dec(excl);
398}
399
400/* add a request to the tail of a list */
401static void mtp_req_put(struct mtp_dev *dev, struct list_head *head,
402 struct usb_request *req)
403{
404 unsigned long flags;
405
406 spin_lock_irqsave(&dev->lock, flags);
407 list_add_tail(&req->list, head);
408 spin_unlock_irqrestore(&dev->lock, flags);
409}
410
411/* remove a request from the head of a list */
412static struct usb_request
413*mtp_req_get(struct mtp_dev *dev, struct list_head *head)
414{
415 unsigned long flags;
416 struct usb_request *req;
417
418 spin_lock_irqsave(&dev->lock, flags);
419 if (list_empty(head)) {
420 req = 0;
421 } else {
422 req = list_first_entry(head, struct usb_request, list);
423 list_del(&req->list);
424 }
425 spin_unlock_irqrestore(&dev->lock, flags);
426 return req;
427}
428
429static void mtp_complete_in(struct usb_ep *ep, struct usb_request *req)
430{
431 struct mtp_dev *dev = _mtp_dev;
432
433 if (req->status != 0)
434 dev->state = STATE_ERROR;
435
436 mtp_req_put(dev, &dev->tx_idle, req);
437
438 wake_up(&dev->write_wq);
439}
440
441static void mtp_complete_out(struct usb_ep *ep, struct usb_request *req)
442{
443 struct mtp_dev *dev = _mtp_dev;
444
445 dev->rx_done = 1;
446 if (req->status != 0)
447 dev->state = STATE_ERROR;
448
449 wake_up(&dev->read_wq);
450}
451
452static void mtp_complete_intr(struct usb_ep *ep, struct usb_request *req)
453{
454 struct mtp_dev *dev = _mtp_dev;
455
456 if (req->status != 0)
457 dev->state = STATE_ERROR;
458
459 mtp_req_put(dev, &dev->intr_idle, req);
460
461 wake_up(&dev->intr_wq);
462}
463
464static int mtp_create_bulk_endpoints(struct mtp_dev *dev,
465 struct usb_endpoint_descriptor *in_desc,
466 struct usb_endpoint_descriptor *out_desc,
467 struct usb_endpoint_descriptor *intr_desc)
468{
469 struct usb_composite_dev *cdev = dev->cdev;
470 struct usb_request *req;
471 struct usb_ep *ep;
472 int i;
473
474 DBG(cdev, "create_bulk_endpoints dev: %p\n", dev);
475
476 ep = usb_ep_autoconfig(cdev->gadget, in_desc);
477 if (!ep) {
478 DBG(cdev, "usb_ep_autoconfig for ep_in failed\n");
479 return -ENODEV;
480 }
481 DBG(cdev, "usb_ep_autoconfig for ep_in got %s\n", ep->name);
482 ep->driver_data = dev; /* claim the endpoint */
483 dev->ep_in = ep;
484
485 ep = usb_ep_autoconfig(cdev->gadget, out_desc);
486 if (!ep) {
487 DBG(cdev, "usb_ep_autoconfig for ep_out failed\n");
488 return -ENODEV;
489 }
490 DBG(cdev, "usb_ep_autoconfig for mtp ep_out got %s\n", ep->name);
491 ep->driver_data = dev; /* claim the endpoint */
492 dev->ep_out = ep;
493
Benoit Goby7a6d39e2011-12-19 14:37:50 -0800494 ep = usb_ep_autoconfig(cdev->gadget, intr_desc);
495 if (!ep) {
496 DBG(cdev, "usb_ep_autoconfig for ep_intr failed\n");
497 return -ENODEV;
498 }
499 DBG(cdev, "usb_ep_autoconfig for mtp ep_intr got %s\n", ep->name);
500 ep->driver_data = dev; /* claim the endpoint */
501 dev->ep_intr = ep;
502
503 /* now allocate requests for our endpoints */
504 for (i = 0; i < TX_REQ_MAX; i++) {
505 req = mtp_request_new(dev->ep_in, MTP_BULK_BUFFER_SIZE);
506 if (!req)
507 goto fail;
508 req->complete = mtp_complete_in;
509 mtp_req_put(dev, &dev->tx_idle, req);
510 }
511 for (i = 0; i < RX_REQ_MAX; i++) {
512 req = mtp_request_new(dev->ep_out, MTP_BULK_BUFFER_SIZE);
513 if (!req)
514 goto fail;
515 req->complete = mtp_complete_out;
516 dev->rx_req[i] = req;
517 }
518 for (i = 0; i < INTR_REQ_MAX; i++) {
519 req = mtp_request_new(dev->ep_intr, INTR_BUFFER_SIZE);
520 if (!req)
521 goto fail;
522 req->complete = mtp_complete_intr;
523 mtp_req_put(dev, &dev->intr_idle, req);
524 }
525
526 return 0;
527
528fail:
Badhri Jagan Sridharaneab46c42014-11-17 21:11:23 -0800529 pr_err("mtp_bind() could not allocate requests\n");
Benoit Goby7a6d39e2011-12-19 14:37:50 -0800530 return -1;
531}
532
533static ssize_t mtp_read(struct file *fp, char __user *buf,
534 size_t count, loff_t *pos)
535{
536 struct mtp_dev *dev = fp->private_data;
537 struct usb_composite_dev *cdev = dev->cdev;
538 struct usb_request *req;
Greg Hackmann1b07ec72014-02-24 10:19:13 -0800539 ssize_t r = count;
540 unsigned xfer;
Benoit Goby7a6d39e2011-12-19 14:37:50 -0800541 int ret = 0;
542
Greg Hackmann1b07ec72014-02-24 10:19:13 -0800543 DBG(cdev, "mtp_read(%zu)\n", count);
Benoit Goby7a6d39e2011-12-19 14:37:50 -0800544
545 if (count > MTP_BULK_BUFFER_SIZE)
546 return -EINVAL;
547
548 /* we will block until we're online */
549 DBG(cdev, "mtp_read: waiting for online state\n");
550 ret = wait_event_interruptible(dev->read_wq,
551 dev->state != STATE_OFFLINE);
552 if (ret < 0) {
553 r = ret;
554 goto done;
555 }
556 spin_lock_irq(&dev->lock);
557 if (dev->state == STATE_CANCELED) {
558 /* report cancelation to userspace */
559 dev->state = STATE_READY;
560 spin_unlock_irq(&dev->lock);
561 return -ECANCELED;
562 }
563 dev->state = STATE_BUSY;
564 spin_unlock_irq(&dev->lock);
565
566requeue_req:
567 /* queue a request */
568 req = dev->rx_req[0];
569 req->length = count;
570 dev->rx_done = 0;
571 ret = usb_ep_queue(dev->ep_out, req, GFP_KERNEL);
572 if (ret < 0) {
573 r = -EIO;
574 goto done;
575 } else {
576 DBG(cdev, "rx %p queue\n", req);
577 }
578
579 /* wait for a request to complete */
580 ret = wait_event_interruptible(dev->read_wq, dev->rx_done);
581 if (ret < 0) {
582 r = ret;
583 usb_ep_dequeue(dev->ep_out, req);
584 goto done;
585 }
586 if (dev->state == STATE_BUSY) {
587 /* If we got a 0-len packet, throw it back and try again. */
588 if (req->actual == 0)
589 goto requeue_req;
590
591 DBG(cdev, "rx %p %d\n", req, req->actual);
592 xfer = (req->actual < count) ? req->actual : count;
593 r = xfer;
594 if (copy_to_user(buf, req->buf, xfer))
595 r = -EFAULT;
596 } else
597 r = -EIO;
598
599done:
600 spin_lock_irq(&dev->lock);
601 if (dev->state == STATE_CANCELED)
602 r = -ECANCELED;
603 else if (dev->state != STATE_OFFLINE)
604 dev->state = STATE_READY;
605 spin_unlock_irq(&dev->lock);
606
Greg Hackmann1b07ec72014-02-24 10:19:13 -0800607 DBG(cdev, "mtp_read returning %zd\n", r);
Benoit Goby7a6d39e2011-12-19 14:37:50 -0800608 return r;
609}
610
611static ssize_t mtp_write(struct file *fp, const char __user *buf,
612 size_t count, loff_t *pos)
613{
614 struct mtp_dev *dev = fp->private_data;
615 struct usb_composite_dev *cdev = dev->cdev;
616 struct usb_request *req = 0;
Greg Hackmann1b07ec72014-02-24 10:19:13 -0800617 ssize_t r = count;
618 unsigned xfer;
Benoit Goby7a6d39e2011-12-19 14:37:50 -0800619 int sendZLP = 0;
620 int ret;
621
Greg Hackmann1b07ec72014-02-24 10:19:13 -0800622 DBG(cdev, "mtp_write(%zu)\n", count);
Benoit Goby7a6d39e2011-12-19 14:37:50 -0800623
624 spin_lock_irq(&dev->lock);
625 if (dev->state == STATE_CANCELED) {
626 /* report cancelation to userspace */
627 dev->state = STATE_READY;
628 spin_unlock_irq(&dev->lock);
629 return -ECANCELED;
630 }
631 if (dev->state == STATE_OFFLINE) {
632 spin_unlock_irq(&dev->lock);
633 return -ENODEV;
634 }
635 dev->state = STATE_BUSY;
636 spin_unlock_irq(&dev->lock);
637
638 /* we need to send a zero length packet to signal the end of transfer
639 * if the transfer size is aligned to a packet boundary.
640 */
641 if ((count & (dev->ep_in->maxpacket - 1)) == 0)
642 sendZLP = 1;
643
644 while (count > 0 || sendZLP) {
645 /* so we exit after sending ZLP */
646 if (count == 0)
647 sendZLP = 0;
648
649 if (dev->state != STATE_BUSY) {
650 DBG(cdev, "mtp_write dev->error\n");
651 r = -EIO;
652 break;
653 }
654
655 /* get an idle tx request to use */
656 req = 0;
657 ret = wait_event_interruptible(dev->write_wq,
658 ((req = mtp_req_get(dev, &dev->tx_idle))
659 || dev->state != STATE_BUSY));
660 if (!req) {
661 r = ret;
662 break;
663 }
664
665 if (count > MTP_BULK_BUFFER_SIZE)
666 xfer = MTP_BULK_BUFFER_SIZE;
667 else
668 xfer = count;
669 if (xfer && copy_from_user(req->buf, buf, xfer)) {
670 r = -EFAULT;
671 break;
672 }
673
674 req->length = xfer;
675 ret = usb_ep_queue(dev->ep_in, req, GFP_KERNEL);
676 if (ret < 0) {
677 DBG(cdev, "mtp_write: xfer error %d\n", ret);
678 r = -EIO;
679 break;
680 }
681
682 buf += xfer;
683 count -= xfer;
684
685 /* zero this so we don't try to free it on error exit */
686 req = 0;
687 }
688
689 if (req)
690 mtp_req_put(dev, &dev->tx_idle, req);
691
692 spin_lock_irq(&dev->lock);
693 if (dev->state == STATE_CANCELED)
694 r = -ECANCELED;
695 else if (dev->state != STATE_OFFLINE)
696 dev->state = STATE_READY;
697 spin_unlock_irq(&dev->lock);
698
Greg Hackmann1b07ec72014-02-24 10:19:13 -0800699 DBG(cdev, "mtp_write returning %zd\n", r);
Benoit Goby7a6d39e2011-12-19 14:37:50 -0800700 return r;
701}
702
703/* read from a local file and write to USB */
704static void send_file_work(struct work_struct *data)
705{
706 struct mtp_dev *dev = container_of(data, struct mtp_dev,
707 send_file_work);
708 struct usb_composite_dev *cdev = dev->cdev;
709 struct usb_request *req = 0;
710 struct mtp_data_header *header;
711 struct file *filp;
712 loff_t offset;
713 int64_t count;
714 int xfer, ret, hdr_size;
715 int r = 0;
716 int sendZLP = 0;
717
718 /* read our parameters */
719 smp_rmb();
720 filp = dev->xfer_file;
721 offset = dev->xfer_file_offset;
722 count = dev->xfer_file_length;
723
724 DBG(cdev, "send_file_work(%lld %lld)\n", offset, count);
725
726 if (dev->xfer_send_header) {
727 hdr_size = sizeof(struct mtp_data_header);
728 count += hdr_size;
729 } else {
730 hdr_size = 0;
731 }
732
733 /* we need to send a zero length packet to signal the end of transfer
734 * if the transfer size is aligned to a packet boundary.
735 */
736 if ((count & (dev->ep_in->maxpacket - 1)) == 0)
737 sendZLP = 1;
738
739 while (count > 0 || sendZLP) {
740 /* so we exit after sending ZLP */
741 if (count == 0)
742 sendZLP = 0;
743
744 /* get an idle tx request to use */
745 req = 0;
746 ret = wait_event_interruptible(dev->write_wq,
747 (req = mtp_req_get(dev, &dev->tx_idle))
748 || dev->state != STATE_BUSY);
749 if (dev->state == STATE_CANCELED) {
750 r = -ECANCELED;
751 break;
752 }
753 if (!req) {
754 r = ret;
755 break;
756 }
757
758 if (count > MTP_BULK_BUFFER_SIZE)
759 xfer = MTP_BULK_BUFFER_SIZE;
760 else
761 xfer = count;
762
763 if (hdr_size) {
764 /* prepend MTP data header */
765 header = (struct mtp_data_header *)req->buf;
766 header->length = __cpu_to_le32(count);
767 header->type = __cpu_to_le16(2); /* data packet */
768 header->command = __cpu_to_le16(dev->xfer_command);
769 header->transaction_id =
770 __cpu_to_le32(dev->xfer_transaction_id);
771 }
772
773 ret = vfs_read(filp, req->buf + hdr_size, xfer - hdr_size,
774 &offset);
775 if (ret < 0) {
776 r = ret;
777 break;
778 }
779 xfer = ret + hdr_size;
780 hdr_size = 0;
781
782 req->length = xfer;
783 ret = usb_ep_queue(dev->ep_in, req, GFP_KERNEL);
784 if (ret < 0) {
785 DBG(cdev, "send_file_work: xfer error %d\n", ret);
786 dev->state = STATE_ERROR;
787 r = -EIO;
788 break;
789 }
790
791 count -= xfer;
792
793 /* zero this so we don't try to free it on error exit */
794 req = 0;
795 }
796
797 if (req)
798 mtp_req_put(dev, &dev->tx_idle, req);
799
800 DBG(cdev, "send_file_work returning %d\n", r);
801 /* write the result */
802 dev->xfer_result = r;
803 smp_wmb();
804}
805
806/* read from USB and write to a local file */
807static void receive_file_work(struct work_struct *data)
808{
809 struct mtp_dev *dev = container_of(data, struct mtp_dev,
810 receive_file_work);
811 struct usb_composite_dev *cdev = dev->cdev;
812 struct usb_request *read_req = NULL, *write_req = NULL;
813 struct file *filp;
814 loff_t offset;
815 int64_t count;
816 int ret, cur_buf = 0;
817 int r = 0;
818
819 /* read our parameters */
820 smp_rmb();
821 filp = dev->xfer_file;
822 offset = dev->xfer_file_offset;
823 count = dev->xfer_file_length;
824
825 DBG(cdev, "receive_file_work(%lld)\n", count);
826
827 while (count > 0 || write_req) {
828 if (count > 0) {
829 /* queue a request */
830 read_req = dev->rx_req[cur_buf];
831 cur_buf = (cur_buf + 1) % RX_REQ_MAX;
832
833 read_req->length = (count > MTP_BULK_BUFFER_SIZE
834 ? MTP_BULK_BUFFER_SIZE : count);
835 dev->rx_done = 0;
836 ret = usb_ep_queue(dev->ep_out, read_req, GFP_KERNEL);
837 if (ret < 0) {
838 r = -EIO;
839 dev->state = STATE_ERROR;
840 break;
841 }
842 }
843
844 if (write_req) {
845 DBG(cdev, "rx %p %d\n", write_req, write_req->actual);
846 ret = vfs_write(filp, write_req->buf, write_req->actual,
847 &offset);
848 DBG(cdev, "vfs_write %d\n", ret);
849 if (ret != write_req->actual) {
850 r = -EIO;
851 dev->state = STATE_ERROR;
852 break;
853 }
854 write_req = NULL;
855 }
856
857 if (read_req) {
858 /* wait for our last read to complete */
859 ret = wait_event_interruptible(dev->read_wq,
860 dev->rx_done || dev->state != STATE_BUSY);
861 if (dev->state == STATE_CANCELED) {
862 r = -ECANCELED;
863 if (!dev->rx_done)
864 usb_ep_dequeue(dev->ep_out, read_req);
865 break;
866 }
867 /* if xfer_file_length is 0xFFFFFFFF, then we read until
868 * we get a zero length packet
869 */
870 if (count != 0xFFFFFFFF)
871 count -= read_req->actual;
872 if (read_req->actual < read_req->length) {
873 /*
874 * short packet is used to signal EOF for
875 * sizes > 4 gig
876 */
877 DBG(cdev, "got short packet\n");
878 count = 0;
879 }
880
881 write_req = read_req;
882 read_req = NULL;
883 }
884 }
885
886 DBG(cdev, "receive_file_work returning %d\n", r);
887 /* write the result */
888 dev->xfer_result = r;
889 smp_wmb();
890}
891
892static int mtp_send_event(struct mtp_dev *dev, struct mtp_event *event)
893{
894 struct usb_request *req = NULL;
895 int ret;
896 int length = event->length;
897
Greg Hackmann1b07ec72014-02-24 10:19:13 -0800898 DBG(dev->cdev, "mtp_send_event(%zu)\n", event->length);
Benoit Goby7a6d39e2011-12-19 14:37:50 -0800899
900 if (length < 0 || length > INTR_BUFFER_SIZE)
901 return -EINVAL;
902 if (dev->state == STATE_OFFLINE)
903 return -ENODEV;
904
905 ret = wait_event_interruptible_timeout(dev->intr_wq,
906 (req = mtp_req_get(dev, &dev->intr_idle)),
907 msecs_to_jiffies(1000));
908 if (!req)
909 return -ETIME;
910
911 if (copy_from_user(req->buf, (void __user *)event->data, length)) {
912 mtp_req_put(dev, &dev->intr_idle, req);
913 return -EFAULT;
914 }
915 req->length = length;
916 ret = usb_ep_queue(dev->ep_intr, req, GFP_KERNEL);
917 if (ret)
918 mtp_req_put(dev, &dev->intr_idle, req);
919
920 return ret;
921}
922
923static long mtp_ioctl(struct file *fp, unsigned code, unsigned long value)
924{
925 struct mtp_dev *dev = fp->private_data;
926 struct file *filp = NULL;
927 int ret = -EINVAL;
928
929 if (mtp_lock(&dev->ioctl_excl))
930 return -EBUSY;
931
932 switch (code) {
933 case MTP_SEND_FILE:
934 case MTP_RECEIVE_FILE:
935 case MTP_SEND_FILE_WITH_HEADER:
936 {
937 struct mtp_file_range mfr;
938 struct work_struct *work;
939
940 spin_lock_irq(&dev->lock);
941 if (dev->state == STATE_CANCELED) {
942 /* report cancelation to userspace */
943 dev->state = STATE_READY;
944 spin_unlock_irq(&dev->lock);
945 ret = -ECANCELED;
946 goto out;
947 }
948 if (dev->state == STATE_OFFLINE) {
949 spin_unlock_irq(&dev->lock);
950 ret = -ENODEV;
951 goto out;
952 }
953 dev->state = STATE_BUSY;
954 spin_unlock_irq(&dev->lock);
955
956 if (copy_from_user(&mfr, (void __user *)value, sizeof(mfr))) {
957 ret = -EFAULT;
958 goto fail;
959 }
960 /* hold a reference to the file while we are working with it */
961 filp = fget(mfr.fd);
962 if (!filp) {
963 ret = -EBADF;
964 goto fail;
965 }
966
967 /* write the parameters */
968 dev->xfer_file = filp;
969 dev->xfer_file_offset = mfr.offset;
970 dev->xfer_file_length = mfr.length;
971 smp_wmb();
972
973 if (code == MTP_SEND_FILE_WITH_HEADER) {
974 work = &dev->send_file_work;
975 dev->xfer_send_header = 1;
976 dev->xfer_command = mfr.command;
977 dev->xfer_transaction_id = mfr.transaction_id;
978 } else if (code == MTP_SEND_FILE) {
979 work = &dev->send_file_work;
980 dev->xfer_send_header = 0;
981 } else {
982 work = &dev->receive_file_work;
983 }
984
985 /* We do the file transfer on a work queue so it will run
986 * in kernel context, which is necessary for vfs_read and
987 * vfs_write to use our buffers in the kernel address space.
988 */
989 queue_work(dev->wq, work);
990 /* wait for operation to complete */
991 flush_workqueue(dev->wq);
992 fput(filp);
993
994 /* read the result */
995 smp_rmb();
996 ret = dev->xfer_result;
997 break;
998 }
999 case MTP_SEND_EVENT:
1000 {
1001 struct mtp_event event;
1002 /* return here so we don't change dev->state below,
1003 * which would interfere with bulk transfer state.
1004 */
1005 if (copy_from_user(&event, (void __user *)value, sizeof(event)))
1006 ret = -EFAULT;
1007 else
1008 ret = mtp_send_event(dev, &event);
1009 goto out;
1010 }
1011 }
1012
1013fail:
1014 spin_lock_irq(&dev->lock);
1015 if (dev->state == STATE_CANCELED)
1016 ret = -ECANCELED;
1017 else if (dev->state != STATE_OFFLINE)
1018 dev->state = STATE_READY;
1019 spin_unlock_irq(&dev->lock);
1020out:
1021 mtp_unlock(&dev->ioctl_excl);
1022 DBG(dev->cdev, "ioctl returning %d\n", ret);
1023 return ret;
1024}
1025
1026static int mtp_open(struct inode *ip, struct file *fp)
1027{
1028 printk(KERN_INFO "mtp_open\n");
1029 if (mtp_lock(&_mtp_dev->open_excl))
1030 return -EBUSY;
1031
1032 /* clear any error condition */
1033 if (_mtp_dev->state != STATE_OFFLINE)
1034 _mtp_dev->state = STATE_READY;
1035
1036 fp->private_data = _mtp_dev;
1037 return 0;
1038}
1039
1040static int mtp_release(struct inode *ip, struct file *fp)
1041{
1042 printk(KERN_INFO "mtp_release\n");
1043
1044 mtp_unlock(&_mtp_dev->open_excl);
1045 return 0;
1046}
1047
1048/* file operations for /dev/mtp_usb */
1049static const struct file_operations mtp_fops = {
1050 .owner = THIS_MODULE,
1051 .read = mtp_read,
1052 .write = mtp_write,
1053 .unlocked_ioctl = mtp_ioctl,
1054 .open = mtp_open,
1055 .release = mtp_release,
1056};
1057
1058static struct miscdevice mtp_device = {
1059 .minor = MISC_DYNAMIC_MINOR,
1060 .name = mtp_shortname,
1061 .fops = &mtp_fops,
1062};
1063
1064static int mtp_ctrlrequest(struct usb_composite_dev *cdev,
1065 const struct usb_ctrlrequest *ctrl)
1066{
1067 struct mtp_dev *dev = _mtp_dev;
1068 int value = -EOPNOTSUPP;
1069 u16 w_index = le16_to_cpu(ctrl->wIndex);
1070 u16 w_value = le16_to_cpu(ctrl->wValue);
1071 u16 w_length = le16_to_cpu(ctrl->wLength);
1072 unsigned long flags;
1073
1074 VDBG(cdev, "mtp_ctrlrequest "
1075 "%02x.%02x v%04x i%04x l%u\n",
1076 ctrl->bRequestType, ctrl->bRequest,
1077 w_value, w_index, w_length);
1078
1079 /* Handle MTP OS string */
1080 if (ctrl->bRequestType ==
1081 (USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE)
1082 && ctrl->bRequest == USB_REQ_GET_DESCRIPTOR
1083 && (w_value >> 8) == USB_DT_STRING
1084 && (w_value & 0xFF) == MTP_OS_STRING_ID) {
1085 value = (w_length < sizeof(mtp_os_string)
1086 ? w_length : sizeof(mtp_os_string));
1087 memcpy(cdev->req->buf, mtp_os_string, value);
1088 } else if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_VENDOR) {
1089 /* Handle MTP OS descriptor */
1090 DBG(cdev, "vendor request: %d index: %d value: %d length: %d\n",
1091 ctrl->bRequest, w_index, w_value, w_length);
1092
1093 if (ctrl->bRequest == 1
1094 && (ctrl->bRequestType & USB_DIR_IN)
1095 && (w_index == 4 || w_index == 5)) {
1096 value = (w_length < sizeof(mtp_ext_config_desc) ?
1097 w_length : sizeof(mtp_ext_config_desc));
1098 memcpy(cdev->req->buf, &mtp_ext_config_desc, value);
1099 }
1100 } else if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_CLASS) {
1101 DBG(cdev, "class request: %d index: %d value: %d length: %d\n",
1102 ctrl->bRequest, w_index, w_value, w_length);
1103
1104 if (ctrl->bRequest == MTP_REQ_CANCEL && w_index == 0
1105 && w_value == 0) {
1106 DBG(cdev, "MTP_REQ_CANCEL\n");
1107
1108 spin_lock_irqsave(&dev->lock, flags);
1109 if (dev->state == STATE_BUSY) {
1110 dev->state = STATE_CANCELED;
1111 wake_up(&dev->read_wq);
1112 wake_up(&dev->write_wq);
1113 }
1114 spin_unlock_irqrestore(&dev->lock, flags);
1115
1116 /* We need to queue a request to read the remaining
1117 * bytes, but we don't actually need to look at
1118 * the contents.
1119 */
1120 value = w_length;
1121 } else if (ctrl->bRequest == MTP_REQ_GET_DEVICE_STATUS
1122 && w_index == 0 && w_value == 0) {
1123 struct mtp_device_status *status = cdev->req->buf;
1124 status->wLength =
1125 __constant_cpu_to_le16(sizeof(*status));
1126
1127 DBG(cdev, "MTP_REQ_GET_DEVICE_STATUS\n");
1128 spin_lock_irqsave(&dev->lock, flags);
1129 /* device status is "busy" until we report
1130 * the cancelation to userspace
1131 */
1132 if (dev->state == STATE_CANCELED)
1133 status->wCode =
1134 __cpu_to_le16(MTP_RESPONSE_DEVICE_BUSY);
1135 else
1136 status->wCode =
1137 __cpu_to_le16(MTP_RESPONSE_OK);
1138 spin_unlock_irqrestore(&dev->lock, flags);
1139 value = sizeof(*status);
1140 }
1141 }
1142
1143 /* respond with data transfer or status phase? */
1144 if (value >= 0) {
1145 int rc;
1146 cdev->req->zero = value < w_length;
1147 cdev->req->length = value;
1148 rc = usb_ep_queue(cdev->gadget->ep0, cdev->req, GFP_ATOMIC);
1149 if (rc < 0)
1150 ERROR(cdev, "%s: response queue error\n", __func__);
1151 }
1152 return value;
1153}
1154
1155static int
1156mtp_function_bind(struct usb_configuration *c, struct usb_function *f)
1157{
1158 struct usb_composite_dev *cdev = c->cdev;
1159 struct mtp_dev *dev = func_to_mtp(f);
1160 int id;
1161 int ret;
Badhri Jagan Sridharan38333b02015-10-06 20:32:01 -07001162 struct mtp_instance *fi_mtp;
Benoit Goby7a6d39e2011-12-19 14:37:50 -08001163
1164 dev->cdev = cdev;
1165 DBG(cdev, "mtp_function_bind dev: %p\n", dev);
1166
1167 /* allocate interface ID(s) */
1168 id = usb_interface_id(c, f);
1169 if (id < 0)
1170 return id;
1171 mtp_interface_desc.bInterfaceNumber = id;
1172
Badhri Jagan Sridharaneab46c42014-11-17 21:11:23 -08001173 if (mtp_string_defs[INTERFACE_STRING_INDEX].id == 0) {
1174 ret = usb_string_id(c->cdev);
1175 if (ret < 0)
1176 return ret;
1177 mtp_string_defs[INTERFACE_STRING_INDEX].id = ret;
1178 mtp_interface_desc.iInterface = ret;
1179 }
Badhri Jagan Sridharan38333b02015-10-06 20:32:01 -07001180
1181 fi_mtp = container_of(f->fi, struct mtp_instance, func_inst);
1182
1183 if (cdev->use_os_string) {
1184 f->os_desc_table = kzalloc(sizeof(*f->os_desc_table),
1185 GFP_KERNEL);
1186 if (!f->os_desc_table)
1187 return -ENOMEM;
1188 f->os_desc_n = 1;
1189 f->os_desc_table[0].os_desc = &fi_mtp->mtp_os_desc;
1190 }
1191
Benoit Goby7a6d39e2011-12-19 14:37:50 -08001192 /* allocate endpoints */
1193 ret = mtp_create_bulk_endpoints(dev, &mtp_fullspeed_in_desc,
1194 &mtp_fullspeed_out_desc, &mtp_intr_desc);
1195 if (ret)
1196 return ret;
1197
1198 /* support high speed hardware */
1199 if (gadget_is_dualspeed(c->cdev->gadget)) {
1200 mtp_highspeed_in_desc.bEndpointAddress =
1201 mtp_fullspeed_in_desc.bEndpointAddress;
1202 mtp_highspeed_out_desc.bEndpointAddress =
1203 mtp_fullspeed_out_desc.bEndpointAddress;
1204 }
Mark Kuob115a6f2015-08-20 13:01:46 +08001205 /* support super speed hardware */
1206 if (gadget_is_superspeed(c->cdev->gadget)) {
1207 unsigned max_burst;
1208
1209 /* Calculate bMaxBurst, we know packet size is 1024 */
1210 max_burst = min_t(unsigned, MTP_BULK_BUFFER_SIZE / 1024, 15);
1211 mtp_ss_in_desc.bEndpointAddress =
1212 mtp_fullspeed_in_desc.bEndpointAddress;
1213 mtp_ss_in_comp_desc.bMaxBurst = max_burst;
1214 mtp_ss_out_desc.bEndpointAddress =
1215 mtp_fullspeed_out_desc.bEndpointAddress;
1216 mtp_ss_out_comp_desc.bMaxBurst = max_burst;
1217 }
Benoit Goby7a6d39e2011-12-19 14:37:50 -08001218
1219 DBG(cdev, "%s speed %s: IN/%s, OUT/%s\n",
Mark Kuob115a6f2015-08-20 13:01:46 +08001220 gadget_is_superspeed(c->cdev->gadget) ? "super" :
1221 (gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full"),
1222 f->name, dev->ep_in->name, dev->ep_out->name);
Benoit Goby7a6d39e2011-12-19 14:37:50 -08001223 return 0;
1224}
1225
1226static void
1227mtp_function_unbind(struct usb_configuration *c, struct usb_function *f)
1228{
1229 struct mtp_dev *dev = func_to_mtp(f);
1230 struct usb_request *req;
1231 int i;
1232
Badhri Jagan Sridharaneab46c42014-11-17 21:11:23 -08001233 mtp_string_defs[INTERFACE_STRING_INDEX].id = 0;
Benoit Goby7a6d39e2011-12-19 14:37:50 -08001234 while ((req = mtp_req_get(dev, &dev->tx_idle)))
1235 mtp_request_free(req, dev->ep_in);
1236 for (i = 0; i < RX_REQ_MAX; i++)
1237 mtp_request_free(dev->rx_req[i], dev->ep_out);
1238 while ((req = mtp_req_get(dev, &dev->intr_idle)))
1239 mtp_request_free(req, dev->ep_intr);
1240 dev->state = STATE_OFFLINE;
Badhri Jagan Sridharan38333b02015-10-06 20:32:01 -07001241 kfree(f->os_desc_table);
1242 f->os_desc_n = 0;
Benoit Goby7a6d39e2011-12-19 14:37:50 -08001243}
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);
1255 if (ret)
1256 return ret;
1257
1258 ret = usb_ep_enable(dev->ep_in);
1259 if (ret)
1260 return ret;
1261
1262 ret = config_ep_by_speed(cdev->gadget, f, dev->ep_out);
1263 if (ret)
1264 return ret;
1265
1266 ret = usb_ep_enable(dev->ep_out);
1267 if (ret) {
1268 usb_ep_disable(dev->ep_in);
1269 return ret;
1270 }
1271
1272 ret = config_ep_by_speed(cdev->gadget, f, dev->ep_intr);
1273 if (ret)
1274 return ret;
1275
1276 ret = usb_ep_enable(dev->ep_intr);
1277 if (ret) {
1278 usb_ep_disable(dev->ep_out);
1279 usb_ep_disable(dev->ep_in);
1280 return ret;
1281 }
1282 dev->state = STATE_READY;
1283
1284 /* readers may be blocked waiting for us to go online */
1285 wake_up(&dev->read_wq);
1286 return 0;
1287}
1288
1289static void mtp_function_disable(struct usb_function *f)
1290{
1291 struct mtp_dev *dev = func_to_mtp(f);
1292 struct usb_composite_dev *cdev = dev->cdev;
1293
1294 DBG(cdev, "mtp_function_disable\n");
1295 dev->state = STATE_OFFLINE;
1296 usb_ep_disable(dev->ep_in);
1297 usb_ep_disable(dev->ep_out);
1298 usb_ep_disable(dev->ep_intr);
1299
1300 /* readers may be blocked waiting for us to go online */
1301 wake_up(&dev->read_wq);
1302
1303 VDBG(cdev, "%s disabled\n", dev->function.name);
1304}
1305
Badhri Jagan Sridharaneab46c42014-11-17 21:11:23 -08001306static int __mtp_setup(struct mtp_instance *fi_mtp)
Benoit Goby7a6d39e2011-12-19 14:37:50 -08001307{
1308 struct mtp_dev *dev;
1309 int ret;
1310
1311 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Badhri Jagan Sridharaneab46c42014-11-17 21:11:23 -08001312
1313 if (fi_mtp != NULL)
1314 fi_mtp->dev = dev;
1315
Benoit Goby7a6d39e2011-12-19 14:37:50 -08001316 if (!dev)
1317 return -ENOMEM;
1318
1319 spin_lock_init(&dev->lock);
1320 init_waitqueue_head(&dev->read_wq);
1321 init_waitqueue_head(&dev->write_wq);
1322 init_waitqueue_head(&dev->intr_wq);
1323 atomic_set(&dev->open_excl, 0);
1324 atomic_set(&dev->ioctl_excl, 0);
1325 INIT_LIST_HEAD(&dev->tx_idle);
1326 INIT_LIST_HEAD(&dev->intr_idle);
1327
1328 dev->wq = create_singlethread_workqueue("f_mtp");
1329 if (!dev->wq) {
1330 ret = -ENOMEM;
1331 goto err1;
1332 }
1333 INIT_WORK(&dev->send_file_work, send_file_work);
1334 INIT_WORK(&dev->receive_file_work, receive_file_work);
1335
1336 _mtp_dev = dev;
1337
1338 ret = misc_register(&mtp_device);
1339 if (ret)
1340 goto err2;
1341
1342 return 0;
1343
1344err2:
1345 destroy_workqueue(dev->wq);
1346err1:
1347 _mtp_dev = NULL;
1348 kfree(dev);
1349 printk(KERN_ERR "mtp gadget driver failed to initialize\n");
1350 return ret;
1351}
1352
Badhri Jagan Sridharaneab46c42014-11-17 21:11:23 -08001353static int mtp_setup_configfs(struct mtp_instance *fi_mtp)
1354{
1355 return __mtp_setup(fi_mtp);
1356}
1357
1358
Benoit Goby7a6d39e2011-12-19 14:37:50 -08001359static void mtp_cleanup(void)
1360{
1361 struct mtp_dev *dev = _mtp_dev;
1362
1363 if (!dev)
1364 return;
1365
1366 misc_deregister(&mtp_device);
1367 destroy_workqueue(dev->wq);
1368 _mtp_dev = NULL;
1369 kfree(dev);
1370}
Badhri Jagan Sridharaneab46c42014-11-17 21:11:23 -08001371
1372static struct mtp_instance *to_mtp_instance(struct config_item *item)
1373{
1374 return container_of(to_config_group(item), struct mtp_instance,
1375 func_inst.group);
1376}
1377
1378static void mtp_attr_release(struct config_item *item)
1379{
1380 struct mtp_instance *fi_mtp = to_mtp_instance(item);
1381 usb_put_function_instance(&fi_mtp->func_inst);
1382}
1383
1384static struct configfs_item_operations mtp_item_ops = {
1385 .release = mtp_attr_release,
1386};
1387
1388static struct config_item_type mtp_func_type = {
1389 .ct_item_ops = &mtp_item_ops,
1390 .ct_owner = THIS_MODULE,
1391};
1392
1393
1394static struct mtp_instance *to_fi_mtp(struct usb_function_instance *fi)
1395{
1396 return container_of(fi, struct mtp_instance, func_inst);
1397}
1398
1399static int mtp_set_inst_name(struct usb_function_instance *fi, const char *name)
1400{
1401 struct mtp_instance *fi_mtp;
1402 char *ptr;
1403 int name_len;
1404
1405 name_len = strlen(name) + 1;
1406 if (name_len > MAX_INST_NAME_LEN)
1407 return -ENAMETOOLONG;
1408
1409 ptr = kstrndup(name, name_len, GFP_KERNEL);
1410 if (!ptr)
1411 return -ENOMEM;
1412
1413 fi_mtp = to_fi_mtp(fi);
1414 fi_mtp->name = ptr;
1415
1416 return 0;
1417}
1418
1419static void mtp_free_inst(struct usb_function_instance *fi)
1420{
1421 struct mtp_instance *fi_mtp;
1422
1423 fi_mtp = to_fi_mtp(fi);
1424 kfree(fi_mtp->name);
1425 mtp_cleanup();
1426 kfree(fi_mtp);
1427}
1428
1429struct usb_function_instance *alloc_inst_mtp_ptp(bool mtp_config)
1430{
1431 struct mtp_instance *fi_mtp;
1432 int ret = 0;
Badhri Jagan Sridharan38333b02015-10-06 20:32:01 -07001433 struct usb_os_desc *descs[1];
1434 char *names[1];
Badhri Jagan Sridharaneab46c42014-11-17 21:11:23 -08001435
1436 fi_mtp = kzalloc(sizeof(*fi_mtp), GFP_KERNEL);
1437 if (!fi_mtp)
1438 return ERR_PTR(-ENOMEM);
1439 fi_mtp->func_inst.set_inst_name = mtp_set_inst_name;
1440 fi_mtp->func_inst.free_func_inst = mtp_free_inst;
1441
Badhri Jagan Sridharan38333b02015-10-06 20:32:01 -07001442 fi_mtp->mtp_os_desc.ext_compat_id = fi_mtp->mtp_ext_compat_id;
1443 INIT_LIST_HEAD(&fi_mtp->mtp_os_desc.ext_prop);
1444 descs[0] = &fi_mtp->mtp_os_desc;
1445 names[0] = "MTP";
1446
Badhri Jagan Sridharaneab46c42014-11-17 21:11:23 -08001447 if (mtp_config) {
1448 ret = mtp_setup_configfs(fi_mtp);
1449 if (ret) {
1450 kfree(fi_mtp);
1451 pr_err("Error setting MTP\n");
1452 return ERR_PTR(ret);
1453 }
1454 } else
1455 fi_mtp->dev = _mtp_dev;
1456
1457 config_group_init_type_name(&fi_mtp->func_inst.group,
1458 "", &mtp_func_type);
Badhri Jagan Sridharan38333b02015-10-06 20:32:01 -07001459 usb_os_desc_prepare_interf_dir(&fi_mtp->func_inst.group, 1,
1460 descs, names, THIS_MODULE);
Badhri Jagan Sridharaneab46c42014-11-17 21:11:23 -08001461
1462 return &fi_mtp->func_inst;
1463}
1464EXPORT_SYMBOL_GPL(alloc_inst_mtp_ptp);
1465
1466static struct usb_function_instance *mtp_alloc_inst(void)
1467{
1468 return alloc_inst_mtp_ptp(true);
1469}
1470
1471static int mtp_ctrlreq_configfs(struct usb_function *f,
1472 const struct usb_ctrlrequest *ctrl)
1473{
1474 return mtp_ctrlrequest(f->config->cdev, ctrl);
1475}
1476
1477static void mtp_free(struct usb_function *f)
1478{
1479 /*NO-OP: no function specific resource allocation in mtp_alloc*/
1480}
1481
1482struct usb_function *function_alloc_mtp_ptp(struct usb_function_instance *fi,
1483 bool mtp_config)
1484{
1485 struct mtp_instance *fi_mtp = to_fi_mtp(fi);
Amit Pundir171b8122015-08-01 03:26:51 +05301486 struct mtp_dev *dev;
Badhri Jagan Sridharaneab46c42014-11-17 21:11:23 -08001487
Amit Pundir171b8122015-08-01 03:26:51 +05301488 /*
1489 * PTP piggybacks on MTP function so make sure we have
1490 * created MTP function before we associate this PTP
1491 * function with a gadget configuration.
1492 */
1493 if (fi_mtp->dev == NULL) {
1494 pr_err("Error: Create MTP function before linking"
1495 " PTP function with a gadget configuration\n");
1496 pr_err("\t1: Delete existing PTP function if any\n");
1497 pr_err("\t2: Create MTP function\n");
1498 pr_err("\t3: Create and symlink PTP function"
1499 " with a gadget configuration\n");
1500 return NULL;
1501 }
1502
1503 dev = fi_mtp->dev;
Badhri Jagan Sridharaneab46c42014-11-17 21:11:23 -08001504 dev->function.name = DRIVER_NAME;
1505 dev->function.strings = mtp_strings;
1506 if (mtp_config) {
1507 dev->function.fs_descriptors = fs_mtp_descs;
1508 dev->function.hs_descriptors = hs_mtp_descs;
Mark Kuob115a6f2015-08-20 13:01:46 +08001509 dev->function.ss_descriptors = ss_mtp_descs;
Badhri Jagan Sridharaneab46c42014-11-17 21:11:23 -08001510 } else {
1511 dev->function.fs_descriptors = fs_ptp_descs;
1512 dev->function.hs_descriptors = hs_ptp_descs;
Mark Kuob115a6f2015-08-20 13:01:46 +08001513 dev->function.ss_descriptors = ss_ptp_descs;
Badhri Jagan Sridharaneab46c42014-11-17 21:11:23 -08001514 }
1515 dev->function.bind = mtp_function_bind;
1516 dev->function.unbind = mtp_function_unbind;
1517 dev->function.set_alt = mtp_function_set_alt;
1518 dev->function.disable = mtp_function_disable;
1519 dev->function.setup = mtp_ctrlreq_configfs;
1520 dev->function.free_func = mtp_free;
1521
1522 return &dev->function;
1523}
1524EXPORT_SYMBOL_GPL(function_alloc_mtp_ptp);
1525
1526static struct usb_function *mtp_alloc(struct usb_function_instance *fi)
1527{
1528 return function_alloc_mtp_ptp(fi, true);
1529}
1530
1531DECLARE_USB_FUNCTION_INIT(mtp, mtp_alloc_inst, mtp_alloc);
1532MODULE_LICENSE("GPL");