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