blob: e490ffa1f58b655398e5d8a8e110a043d531a90f [file] [log] [blame]
Andrey Konovalovf2c2e712020-02-24 17:13:03 +01001// SPDX-License-Identifier: GPL-2.0
2/*
3 * USB Raw Gadget driver.
4 * See Documentation/usb/raw-gadget.rst for more details.
5 *
6 * Andrey Konovalov <andreyknvl@gmail.com>
7 */
8
9#include <linux/compiler.h>
10#include <linux/debugfs.h>
11#include <linux/delay.h>
12#include <linux/kref.h>
13#include <linux/miscdevice.h>
14#include <linux/module.h>
15#include <linux/semaphore.h>
16#include <linux/sched.h>
17#include <linux/slab.h>
18#include <linux/uaccess.h>
19#include <linux/wait.h>
20
21#include <linux/usb.h>
22#include <linux/usb/ch9.h>
23#include <linux/usb/ch11.h>
24#include <linux/usb/gadget.h>
25
26#include <uapi/linux/usb/raw_gadget.h>
27
28#define DRIVER_DESC "USB Raw Gadget"
29#define DRIVER_NAME "raw-gadget"
30
31MODULE_DESCRIPTION(DRIVER_DESC);
32MODULE_AUTHOR("Andrey Konovalov");
33MODULE_LICENSE("GPL");
34
35/*----------------------------------------------------------------------*/
36
37#define RAW_EVENT_QUEUE_SIZE 16
38
39struct raw_event_queue {
40 /* See the comment in raw_event_queue_fetch() for locking details. */
41 spinlock_t lock;
42 struct semaphore sema;
43 struct usb_raw_event *events[RAW_EVENT_QUEUE_SIZE];
44 int size;
45};
46
47static void raw_event_queue_init(struct raw_event_queue *queue)
48{
49 spin_lock_init(&queue->lock);
50 sema_init(&queue->sema, 0);
51 queue->size = 0;
52}
53
54static int raw_event_queue_add(struct raw_event_queue *queue,
55 enum usb_raw_event_type type, size_t length, const void *data)
56{
57 unsigned long flags;
58 struct usb_raw_event *event;
59
60 spin_lock_irqsave(&queue->lock, flags);
61 if (WARN_ON(queue->size >= RAW_EVENT_QUEUE_SIZE)) {
62 spin_unlock_irqrestore(&queue->lock, flags);
63 return -ENOMEM;
64 }
65 event = kmalloc(sizeof(*event) + length, GFP_ATOMIC);
66 if (!event) {
67 spin_unlock_irqrestore(&queue->lock, flags);
68 return -ENOMEM;
69 }
70 event->type = type;
71 event->length = length;
72 if (event->length)
73 memcpy(&event->data[0], data, length);
74 queue->events[queue->size] = event;
75 queue->size++;
76 up(&queue->sema);
77 spin_unlock_irqrestore(&queue->lock, flags);
78 return 0;
79}
80
81static struct usb_raw_event *raw_event_queue_fetch(
82 struct raw_event_queue *queue)
83{
84 unsigned long flags;
85 struct usb_raw_event *event;
86
87 /*
88 * This function can be called concurrently. We first check that
89 * there's at least one event queued by decrementing the semaphore,
90 * and then take the lock to protect queue struct fields.
91 */
92 if (down_interruptible(&queue->sema))
93 return NULL;
94 spin_lock_irqsave(&queue->lock, flags);
95 if (WARN_ON(!queue->size))
96 return NULL;
97 event = queue->events[0];
98 queue->size--;
99 memmove(&queue->events[0], &queue->events[1],
100 queue->size * sizeof(queue->events[0]));
101 spin_unlock_irqrestore(&queue->lock, flags);
102 return event;
103}
104
105static void raw_event_queue_destroy(struct raw_event_queue *queue)
106{
107 int i;
108
109 for (i = 0; i < queue->size; i++)
110 kfree(queue->events[i]);
111 queue->size = 0;
112}
113
114/*----------------------------------------------------------------------*/
115
116struct raw_dev;
117
118#define USB_RAW_MAX_ENDPOINTS 32
119
120enum ep_state {
121 STATE_EP_DISABLED,
122 STATE_EP_ENABLED,
123};
124
125struct raw_ep {
126 struct raw_dev *dev;
127 enum ep_state state;
128 struct usb_ep *ep;
129 struct usb_request *req;
130 bool urb_queued;
131 bool disabling;
132 ssize_t status;
133};
134
135enum dev_state {
136 STATE_DEV_INVALID = 0,
137 STATE_DEV_OPENED,
138 STATE_DEV_INITIALIZED,
139 STATE_DEV_RUNNING,
140 STATE_DEV_CLOSED,
141 STATE_DEV_FAILED
142};
143
144struct raw_dev {
145 struct kref count;
146 spinlock_t lock;
147
148 const char *udc_name;
149 struct usb_gadget_driver driver;
150
151 /* Reference to misc device: */
152 struct device *dev;
153
154 /* Protected by lock: */
155 enum dev_state state;
156 bool gadget_registered;
157 struct usb_gadget *gadget;
158 struct usb_request *req;
159 bool ep0_in_pending;
160 bool ep0_out_pending;
161 bool ep0_urb_queued;
162 ssize_t ep0_status;
163 struct raw_ep eps[USB_RAW_MAX_ENDPOINTS];
164
165 struct completion ep0_done;
166 struct raw_event_queue queue;
167};
168
169static struct raw_dev *dev_new(void)
170{
171 struct raw_dev *dev;
172
173 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
174 if (!dev)
175 return NULL;
176 /* Matches kref_put() in raw_release(). */
177 kref_init(&dev->count);
178 spin_lock_init(&dev->lock);
179 init_completion(&dev->ep0_done);
180 raw_event_queue_init(&dev->queue);
181 return dev;
182}
183
184static void dev_free(struct kref *kref)
185{
186 struct raw_dev *dev = container_of(kref, struct raw_dev, count);
187 int i;
188
189 kfree(dev->udc_name);
190 kfree(dev->driver.udc_name);
191 if (dev->req) {
192 if (dev->ep0_urb_queued)
193 usb_ep_dequeue(dev->gadget->ep0, dev->req);
194 usb_ep_free_request(dev->gadget->ep0, dev->req);
195 }
196 raw_event_queue_destroy(&dev->queue);
197 for (i = 0; i < USB_RAW_MAX_ENDPOINTS; i++) {
198 if (dev->eps[i].state != STATE_EP_ENABLED)
199 continue;
200 usb_ep_disable(dev->eps[i].ep);
201 usb_ep_free_request(dev->eps[i].ep, dev->eps[i].req);
202 kfree(dev->eps[i].ep->desc);
203 dev->eps[i].state = STATE_EP_DISABLED;
204 }
205 kfree(dev);
206}
207
208/*----------------------------------------------------------------------*/
209
210static int raw_queue_event(struct raw_dev *dev,
211 enum usb_raw_event_type type, size_t length, const void *data)
212{
213 int ret = 0;
214 unsigned long flags;
215
216 ret = raw_event_queue_add(&dev->queue, type, length, data);
217 if (ret < 0) {
218 spin_lock_irqsave(&dev->lock, flags);
219 dev->state = STATE_DEV_FAILED;
220 spin_unlock_irqrestore(&dev->lock, flags);
221 }
222 return ret;
223}
224
225static void gadget_ep0_complete(struct usb_ep *ep, struct usb_request *req)
226{
227 struct raw_dev *dev = req->context;
228 unsigned long flags;
229
230 spin_lock_irqsave(&dev->lock, flags);
231 if (req->status)
232 dev->ep0_status = req->status;
233 else
234 dev->ep0_status = req->actual;
235 if (dev->ep0_in_pending)
236 dev->ep0_in_pending = false;
237 else
238 dev->ep0_out_pending = false;
239 spin_unlock_irqrestore(&dev->lock, flags);
240
241 complete(&dev->ep0_done);
242}
243
244static int gadget_bind(struct usb_gadget *gadget,
245 struct usb_gadget_driver *driver)
246{
247 int ret = 0;
248 struct raw_dev *dev = container_of(driver, struct raw_dev, driver);
249 struct usb_request *req;
250 unsigned long flags;
251
252 if (strcmp(gadget->name, dev->udc_name) != 0)
253 return -ENODEV;
254
255 set_gadget_data(gadget, dev);
256 req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
257 if (!req) {
258 dev_err(&gadget->dev, "usb_ep_alloc_request failed\n");
259 set_gadget_data(gadget, NULL);
260 return -ENOMEM;
261 }
262
263 spin_lock_irqsave(&dev->lock, flags);
264 dev->req = req;
265 dev->req->context = dev;
266 dev->req->complete = gadget_ep0_complete;
267 dev->gadget = gadget;
268 spin_unlock_irqrestore(&dev->lock, flags);
269
270 /* Matches kref_put() in gadget_unbind(). */
271 kref_get(&dev->count);
272
273 ret = raw_queue_event(dev, USB_RAW_EVENT_CONNECT, 0, NULL);
274 if (ret < 0)
275 dev_err(&gadget->dev, "failed to queue event\n");
276
277 return ret;
278}
279
280static void gadget_unbind(struct usb_gadget *gadget)
281{
282 struct raw_dev *dev = get_gadget_data(gadget);
283
284 set_gadget_data(gadget, NULL);
285 /* Matches kref_get() in gadget_bind(). */
286 kref_put(&dev->count, dev_free);
287}
288
289static int gadget_setup(struct usb_gadget *gadget,
290 const struct usb_ctrlrequest *ctrl)
291{
292 int ret = 0;
293 struct raw_dev *dev = get_gadget_data(gadget);
294 unsigned long flags;
295
296 spin_lock_irqsave(&dev->lock, flags);
297 if (dev->state != STATE_DEV_RUNNING) {
298 dev_err(&gadget->dev, "ignoring, device is not running\n");
299 ret = -ENODEV;
300 goto out_unlock;
301 }
302 if (dev->ep0_in_pending || dev->ep0_out_pending) {
303 dev_dbg(&gadget->dev, "stalling, request already pending\n");
304 ret = -EBUSY;
305 goto out_unlock;
306 }
307 if ((ctrl->bRequestType & USB_DIR_IN) && ctrl->wLength)
308 dev->ep0_in_pending = true;
309 else
310 dev->ep0_out_pending = true;
311 spin_unlock_irqrestore(&dev->lock, flags);
312
313 ret = raw_queue_event(dev, USB_RAW_EVENT_CONTROL, sizeof(*ctrl), ctrl);
314 if (ret < 0)
315 dev_err(&gadget->dev, "failed to queue event\n");
316 goto out;
317
318out_unlock:
319 spin_unlock_irqrestore(&dev->lock, flags);
320out:
321 return ret;
322}
323
324/* These are currently unused but present in case UDC driver requires them. */
325static void gadget_disconnect(struct usb_gadget *gadget) { }
326static void gadget_suspend(struct usb_gadget *gadget) { }
327static void gadget_resume(struct usb_gadget *gadget) { }
328static void gadget_reset(struct usb_gadget *gadget) { }
329
330/*----------------------------------------------------------------------*/
331
332static struct miscdevice raw_misc_device;
333
334static int raw_open(struct inode *inode, struct file *fd)
335{
336 struct raw_dev *dev;
337
338 /* Nonblocking I/O is not supported yet. */
339 if (fd->f_flags & O_NONBLOCK)
340 return -EINVAL;
341
342 dev = dev_new();
343 if (!dev)
344 return -ENOMEM;
345 fd->private_data = dev;
346 dev->state = STATE_DEV_OPENED;
347 dev->dev = raw_misc_device.this_device;
348 return 0;
349}
350
351static int raw_release(struct inode *inode, struct file *fd)
352{
353 int ret = 0;
354 struct raw_dev *dev = fd->private_data;
355 unsigned long flags;
356 bool unregister = false;
357
358 spin_lock_irqsave(&dev->lock, flags);
359 dev->state = STATE_DEV_CLOSED;
360 if (!dev->gadget) {
361 spin_unlock_irqrestore(&dev->lock, flags);
362 goto out_put;
363 }
364 if (dev->gadget_registered)
365 unregister = true;
366 dev->gadget_registered = false;
367 spin_unlock_irqrestore(&dev->lock, flags);
368
369 if (unregister) {
370 ret = usb_gadget_unregister_driver(&dev->driver);
371 if (ret != 0)
372 dev_err(dev->dev,
373 "usb_gadget_unregister_driver() failed with %d\n",
374 ret);
375 /* Matches kref_get() in raw_ioctl_run(). */
376 kref_put(&dev->count, dev_free);
377 }
378
379out_put:
380 /* Matches dev_new() in raw_open(). */
381 kref_put(&dev->count, dev_free);
382 return ret;
383}
384
385/*----------------------------------------------------------------------*/
386
387static int raw_ioctl_init(struct raw_dev *dev, unsigned long value)
388{
389 int ret = 0;
390 struct usb_raw_init arg;
391 char *udc_driver_name;
392 char *udc_device_name;
393 unsigned long flags;
394
Dan Carpenter068fbff2020-04-06 17:51:19 +0300395 if (copy_from_user(&arg, (void __user *)value, sizeof(arg)))
396 return -EFAULT;
Andrey Konovalovf2c2e712020-02-24 17:13:03 +0100397
398 switch (arg.speed) {
399 case USB_SPEED_UNKNOWN:
400 arg.speed = USB_SPEED_HIGH;
401 break;
402 case USB_SPEED_LOW:
403 case USB_SPEED_FULL:
404 case USB_SPEED_HIGH:
405 case USB_SPEED_SUPER:
406 break;
407 default:
408 return -EINVAL;
409 }
410
411 udc_driver_name = kmalloc(UDC_NAME_LENGTH_MAX, GFP_KERNEL);
412 if (!udc_driver_name)
413 return -ENOMEM;
414 ret = strscpy(udc_driver_name, &arg.driver_name[0],
415 UDC_NAME_LENGTH_MAX);
416 if (ret < 0) {
417 kfree(udc_driver_name);
418 return ret;
419 }
420 ret = 0;
421
422 udc_device_name = kmalloc(UDC_NAME_LENGTH_MAX, GFP_KERNEL);
423 if (!udc_device_name) {
424 kfree(udc_driver_name);
425 return -ENOMEM;
426 }
427 ret = strscpy(udc_device_name, &arg.device_name[0],
428 UDC_NAME_LENGTH_MAX);
429 if (ret < 0) {
430 kfree(udc_driver_name);
431 kfree(udc_device_name);
432 return ret;
433 }
434 ret = 0;
435
436 spin_lock_irqsave(&dev->lock, flags);
437 if (dev->state != STATE_DEV_OPENED) {
438 dev_dbg(dev->dev, "fail, device is not opened\n");
439 kfree(udc_driver_name);
440 kfree(udc_device_name);
441 ret = -EINVAL;
442 goto out_unlock;
443 }
444 dev->udc_name = udc_driver_name;
445
446 dev->driver.function = DRIVER_DESC;
447 dev->driver.max_speed = arg.speed;
448 dev->driver.setup = gadget_setup;
449 dev->driver.disconnect = gadget_disconnect;
450 dev->driver.bind = gadget_bind;
451 dev->driver.unbind = gadget_unbind;
452 dev->driver.suspend = gadget_suspend;
453 dev->driver.resume = gadget_resume;
454 dev->driver.reset = gadget_reset;
455 dev->driver.driver.name = DRIVER_NAME;
456 dev->driver.udc_name = udc_device_name;
457 dev->driver.match_existing_only = 1;
458
459 dev->state = STATE_DEV_INITIALIZED;
460
461out_unlock:
462 spin_unlock_irqrestore(&dev->lock, flags);
463 return ret;
464}
465
466static int raw_ioctl_run(struct raw_dev *dev, unsigned long value)
467{
468 int ret = 0;
469 unsigned long flags;
470
471 if (value)
472 return -EINVAL;
473
474 spin_lock_irqsave(&dev->lock, flags);
475 if (dev->state != STATE_DEV_INITIALIZED) {
476 dev_dbg(dev->dev, "fail, device is not initialized\n");
477 ret = -EINVAL;
478 goto out_unlock;
479 }
480 spin_unlock_irqrestore(&dev->lock, flags);
481
482 ret = usb_gadget_probe_driver(&dev->driver);
483
484 spin_lock_irqsave(&dev->lock, flags);
485 if (ret) {
486 dev_err(dev->dev,
487 "fail, usb_gadget_probe_driver returned %d\n", ret);
488 dev->state = STATE_DEV_FAILED;
489 goto out_unlock;
490 }
491 dev->gadget_registered = true;
492 dev->state = STATE_DEV_RUNNING;
493 /* Matches kref_put() in raw_release(). */
494 kref_get(&dev->count);
495
496out_unlock:
497 spin_unlock_irqrestore(&dev->lock, flags);
498 return ret;
499}
500
501static int raw_ioctl_event_fetch(struct raw_dev *dev, unsigned long value)
502{
Andrey Konovalovf2c2e712020-02-24 17:13:03 +0100503 struct usb_raw_event arg;
504 unsigned long flags;
505 struct usb_raw_event *event;
506 uint32_t length;
507
Dan Carpenter068fbff2020-04-06 17:51:19 +0300508 if (copy_from_user(&arg, (void __user *)value, sizeof(arg)))
509 return -EFAULT;
Andrey Konovalovf2c2e712020-02-24 17:13:03 +0100510
511 spin_lock_irqsave(&dev->lock, flags);
512 if (dev->state != STATE_DEV_RUNNING) {
513 dev_dbg(dev->dev, "fail, device is not running\n");
514 spin_unlock_irqrestore(&dev->lock, flags);
515 return -EINVAL;
516 }
517 if (!dev->gadget) {
518 dev_dbg(dev->dev, "fail, gadget is not bound\n");
519 spin_unlock_irqrestore(&dev->lock, flags);
520 return -EBUSY;
521 }
522 spin_unlock_irqrestore(&dev->lock, flags);
523
524 event = raw_event_queue_fetch(&dev->queue);
525 if (!event) {
526 dev_dbg(&dev->gadget->dev, "event fetching interrupted\n");
527 return -EINTR;
528 }
529 length = min(arg.length, event->length);
Dan Carpenter068fbff2020-04-06 17:51:19 +0300530 if (copy_to_user((void __user *)value, event, sizeof(*event) + length))
531 return -EFAULT;
532
533 return 0;
Andrey Konovalovf2c2e712020-02-24 17:13:03 +0100534}
535
536static void *raw_alloc_io_data(struct usb_raw_ep_io *io, void __user *ptr,
537 bool get_from_user)
538{
Andrey Konovalovf2c2e712020-02-24 17:13:03 +0100539 void *data;
540
Dan Carpenter068fbff2020-04-06 17:51:19 +0300541 if (copy_from_user(io, ptr, sizeof(*io)))
542 return ERR_PTR(-EFAULT);
Andrey Konovalovf2c2e712020-02-24 17:13:03 +0100543 if (io->ep >= USB_RAW_MAX_ENDPOINTS)
544 return ERR_PTR(-EINVAL);
545 if (!usb_raw_io_flags_valid(io->flags))
546 return ERR_PTR(-EINVAL);
547 if (io->length > PAGE_SIZE)
548 return ERR_PTR(-EINVAL);
549 if (get_from_user)
550 data = memdup_user(ptr + sizeof(*io), io->length);
551 else {
552 data = kmalloc(io->length, GFP_KERNEL);
553 if (!data)
554 data = ERR_PTR(-ENOMEM);
555 }
556 return data;
557}
558
559static int raw_process_ep0_io(struct raw_dev *dev, struct usb_raw_ep_io *io,
560 void *data, bool in)
561{
562 int ret = 0;
563 unsigned long flags;
564
565 spin_lock_irqsave(&dev->lock, flags);
566 if (dev->state != STATE_DEV_RUNNING) {
567 dev_dbg(dev->dev, "fail, device is not running\n");
568 ret = -EINVAL;
569 goto out_unlock;
570 }
571 if (!dev->gadget) {
572 dev_dbg(dev->dev, "fail, gadget is not bound\n");
573 ret = -EBUSY;
574 goto out_unlock;
575 }
576 if (dev->ep0_urb_queued) {
577 dev_dbg(&dev->gadget->dev, "fail, urb already queued\n");
578 ret = -EBUSY;
579 goto out_unlock;
580 }
581 if ((in && !dev->ep0_in_pending) ||
582 (!in && !dev->ep0_out_pending)) {
583 dev_dbg(&dev->gadget->dev, "fail, wrong direction\n");
584 ret = -EBUSY;
585 goto out_unlock;
586 }
587 if (WARN_ON(in && dev->ep0_out_pending)) {
588 ret = -ENODEV;
589 dev->state = STATE_DEV_FAILED;
590 goto out_done;
591 }
592 if (WARN_ON(!in && dev->ep0_in_pending)) {
593 ret = -ENODEV;
594 dev->state = STATE_DEV_FAILED;
595 goto out_done;
596 }
597
598 dev->req->buf = data;
599 dev->req->length = io->length;
600 dev->req->zero = usb_raw_io_flags_zero(io->flags);
601 dev->ep0_urb_queued = true;
602 spin_unlock_irqrestore(&dev->lock, flags);
603
604 ret = usb_ep_queue(dev->gadget->ep0, dev->req, GFP_KERNEL);
605 if (ret) {
606 dev_err(&dev->gadget->dev,
607 "fail, usb_ep_queue returned %d\n", ret);
608 spin_lock_irqsave(&dev->lock, flags);
609 dev->state = STATE_DEV_FAILED;
610 goto out_done;
611 }
612
613 ret = wait_for_completion_interruptible(&dev->ep0_done);
614 if (ret) {
615 dev_dbg(&dev->gadget->dev, "wait interrupted\n");
616 usb_ep_dequeue(dev->gadget->ep0, dev->req);
617 wait_for_completion(&dev->ep0_done);
618 spin_lock_irqsave(&dev->lock, flags);
619 goto out_done;
620 }
621
622 spin_lock_irqsave(&dev->lock, flags);
623 ret = dev->ep0_status;
624
625out_done:
626 dev->ep0_urb_queued = false;
627out_unlock:
628 spin_unlock_irqrestore(&dev->lock, flags);
629 return ret;
630}
631
632static int raw_ioctl_ep0_write(struct raw_dev *dev, unsigned long value)
633{
634 int ret = 0;
635 void *data;
636 struct usb_raw_ep_io io;
637
638 data = raw_alloc_io_data(&io, (void __user *)value, true);
639 if (IS_ERR(data))
640 return PTR_ERR(data);
641 ret = raw_process_ep0_io(dev, &io, data, true);
642 kfree(data);
643 return ret;
644}
645
646static int raw_ioctl_ep0_read(struct raw_dev *dev, unsigned long value)
647{
648 int ret = 0;
649 void *data;
650 struct usb_raw_ep_io io;
651 unsigned int length;
652
653 data = raw_alloc_io_data(&io, (void __user *)value, false);
654 if (IS_ERR(data))
655 return PTR_ERR(data);
656 ret = raw_process_ep0_io(dev, &io, data, false);
Dan Carpenter068fbff2020-04-06 17:51:19 +0300657 if (ret)
658 goto free;
659
Andrey Konovalovf2c2e712020-02-24 17:13:03 +0100660 length = min(io.length, (unsigned int)ret);
Dan Carpenter068fbff2020-04-06 17:51:19 +0300661 if (copy_to_user((void __user *)(value + sizeof(io)), data, length))
662 ret = -EFAULT;
663free:
Andrey Konovalovf2c2e712020-02-24 17:13:03 +0100664 kfree(data);
665 return ret;
666}
667
668static bool check_ep_caps(struct usb_ep *ep,
669 struct usb_endpoint_descriptor *desc)
670{
671 switch (usb_endpoint_type(desc)) {
672 case USB_ENDPOINT_XFER_ISOC:
673 if (!ep->caps.type_iso)
674 return false;
675 break;
676 case USB_ENDPOINT_XFER_BULK:
677 if (!ep->caps.type_bulk)
678 return false;
679 break;
680 case USB_ENDPOINT_XFER_INT:
681 if (!ep->caps.type_int)
682 return false;
683 break;
684 default:
685 return false;
686 }
687
688 if (usb_endpoint_dir_in(desc) && !ep->caps.dir_in)
689 return false;
690 if (usb_endpoint_dir_out(desc) && !ep->caps.dir_out)
691 return false;
692
693 return true;
694}
695
696static int raw_ioctl_ep_enable(struct raw_dev *dev, unsigned long value)
697{
698 int ret = 0, i;
699 unsigned long flags;
700 struct usb_endpoint_descriptor *desc;
701 struct usb_ep *ep = NULL;
702
703 desc = memdup_user((void __user *)value, sizeof(*desc));
704 if (IS_ERR(desc))
705 return PTR_ERR(desc);
706
707 /*
708 * Endpoints with a maxpacket length of 0 can cause crashes in UDC
709 * drivers.
710 */
711 if (usb_endpoint_maxp(desc) == 0) {
712 dev_dbg(dev->dev, "fail, bad endpoint maxpacket\n");
713 kfree(desc);
714 return -EINVAL;
715 }
716
717 spin_lock_irqsave(&dev->lock, flags);
718 if (dev->state != STATE_DEV_RUNNING) {
719 dev_dbg(dev->dev, "fail, device is not running\n");
720 ret = -EINVAL;
721 goto out_free;
722 }
723 if (!dev->gadget) {
724 dev_dbg(dev->dev, "fail, gadget is not bound\n");
725 ret = -EBUSY;
726 goto out_free;
727 }
728
729 for (i = 0; i < USB_RAW_MAX_ENDPOINTS; i++) {
730 if (dev->eps[i].state == STATE_EP_ENABLED)
731 continue;
732 break;
733 }
734 if (i == USB_RAW_MAX_ENDPOINTS) {
735 dev_dbg(&dev->gadget->dev,
736 "fail, no device endpoints available\n");
737 ret = -EBUSY;
738 goto out_free;
739 }
740
741 gadget_for_each_ep(ep, dev->gadget) {
742 if (ep->enabled)
743 continue;
744 if (!check_ep_caps(ep, desc))
745 continue;
746 ep->desc = desc;
747 ret = usb_ep_enable(ep);
748 if (ret < 0) {
749 dev_err(&dev->gadget->dev,
750 "fail, usb_ep_enable returned %d\n", ret);
751 goto out_free;
752 }
753 dev->eps[i].req = usb_ep_alloc_request(ep, GFP_ATOMIC);
754 if (!dev->eps[i].req) {
755 dev_err(&dev->gadget->dev,
756 "fail, usb_ep_alloc_request failed\n");
757 usb_ep_disable(ep);
758 ret = -ENOMEM;
759 goto out_free;
760 }
761 dev->eps[i].ep = ep;
762 dev->eps[i].state = STATE_EP_ENABLED;
763 ep->driver_data = &dev->eps[i];
764 ret = i;
765 goto out_unlock;
766 }
767
768 dev_dbg(&dev->gadget->dev, "fail, no gadget endpoints available\n");
769 ret = -EBUSY;
770
771out_free:
772 kfree(desc);
773out_unlock:
774 spin_unlock_irqrestore(&dev->lock, flags);
775 return ret;
776}
777
778static int raw_ioctl_ep_disable(struct raw_dev *dev, unsigned long value)
779{
780 int ret = 0, i = value;
781 unsigned long flags;
782 const void *desc;
783
784 if (i < 0 || i >= USB_RAW_MAX_ENDPOINTS)
785 return -EINVAL;
786
787 spin_lock_irqsave(&dev->lock, flags);
788 if (dev->state != STATE_DEV_RUNNING) {
789 dev_dbg(dev->dev, "fail, device is not running\n");
790 ret = -EINVAL;
791 goto out_unlock;
792 }
793 if (!dev->gadget) {
794 dev_dbg(dev->dev, "fail, gadget is not bound\n");
795 ret = -EBUSY;
796 goto out_unlock;
797 }
798 if (dev->eps[i].state != STATE_EP_ENABLED) {
799 dev_dbg(&dev->gadget->dev, "fail, endpoint is not enabled\n");
800 ret = -EINVAL;
801 goto out_unlock;
802 }
803 if (dev->eps[i].disabling) {
804 dev_dbg(&dev->gadget->dev,
805 "fail, disable already in progress\n");
806 ret = -EINVAL;
807 goto out_unlock;
808 }
809 if (dev->eps[i].urb_queued) {
810 dev_dbg(&dev->gadget->dev,
811 "fail, waiting for urb completion\n");
812 ret = -EINVAL;
813 goto out_unlock;
814 }
815 dev->eps[i].disabling = true;
816 spin_unlock_irqrestore(&dev->lock, flags);
817
818 usb_ep_disable(dev->eps[i].ep);
819
820 spin_lock_irqsave(&dev->lock, flags);
821 usb_ep_free_request(dev->eps[i].ep, dev->eps[i].req);
822 desc = dev->eps[i].ep->desc;
823 dev->eps[i].ep = NULL;
824 dev->eps[i].state = STATE_EP_DISABLED;
825 kfree(desc);
826 dev->eps[i].disabling = false;
827
828out_unlock:
829 spin_unlock_irqrestore(&dev->lock, flags);
830 return ret;
831}
832
833static void gadget_ep_complete(struct usb_ep *ep, struct usb_request *req)
834{
835 struct raw_ep *r_ep = (struct raw_ep *)ep->driver_data;
836 struct raw_dev *dev = r_ep->dev;
837 unsigned long flags;
838
839 spin_lock_irqsave(&dev->lock, flags);
840 if (req->status)
841 r_ep->status = req->status;
842 else
843 r_ep->status = req->actual;
844 spin_unlock_irqrestore(&dev->lock, flags);
845
846 complete((struct completion *)req->context);
847}
848
849static int raw_process_ep_io(struct raw_dev *dev, struct usb_raw_ep_io *io,
850 void *data, bool in)
851{
852 int ret = 0;
853 unsigned long flags;
854 struct raw_ep *ep = &dev->eps[io->ep];
855 DECLARE_COMPLETION_ONSTACK(done);
856
857 spin_lock_irqsave(&dev->lock, flags);
858 if (dev->state != STATE_DEV_RUNNING) {
859 dev_dbg(dev->dev, "fail, device is not running\n");
860 ret = -EINVAL;
861 goto out_unlock;
862 }
863 if (!dev->gadget) {
864 dev_dbg(dev->dev, "fail, gadget is not bound\n");
865 ret = -EBUSY;
866 goto out_unlock;
867 }
868 if (ep->state != STATE_EP_ENABLED) {
869 dev_dbg(&dev->gadget->dev, "fail, endpoint is not enabled\n");
870 ret = -EBUSY;
871 goto out_unlock;
872 }
873 if (ep->disabling) {
874 dev_dbg(&dev->gadget->dev,
875 "fail, endpoint is already being disabled\n");
876 ret = -EBUSY;
877 goto out_unlock;
878 }
879 if (ep->urb_queued) {
880 dev_dbg(&dev->gadget->dev, "fail, urb already queued\n");
881 ret = -EBUSY;
882 goto out_unlock;
883 }
884 if ((in && !ep->ep->caps.dir_in) || (!in && ep->ep->caps.dir_in)) {
885 dev_dbg(&dev->gadget->dev, "fail, wrong direction\n");
886 ret = -EINVAL;
887 goto out_unlock;
888 }
889
890 ep->dev = dev;
891 ep->req->context = &done;
892 ep->req->complete = gadget_ep_complete;
893 ep->req->buf = data;
894 ep->req->length = io->length;
895 ep->req->zero = usb_raw_io_flags_zero(io->flags);
896 ep->urb_queued = true;
897 spin_unlock_irqrestore(&dev->lock, flags);
898
899 ret = usb_ep_queue(ep->ep, ep->req, GFP_KERNEL);
900 if (ret) {
901 dev_err(&dev->gadget->dev,
902 "fail, usb_ep_queue returned %d\n", ret);
903 spin_lock_irqsave(&dev->lock, flags);
904 dev->state = STATE_DEV_FAILED;
905 goto out_done;
906 }
907
908 ret = wait_for_completion_interruptible(&done);
909 if (ret) {
910 dev_dbg(&dev->gadget->dev, "wait interrupted\n");
911 usb_ep_dequeue(ep->ep, ep->req);
912 wait_for_completion(&done);
913 spin_lock_irqsave(&dev->lock, flags);
914 goto out_done;
915 }
916
917 spin_lock_irqsave(&dev->lock, flags);
918 ret = ep->status;
919
920out_done:
921 ep->urb_queued = false;
922out_unlock:
923 spin_unlock_irqrestore(&dev->lock, flags);
924 return ret;
925}
926
927static int raw_ioctl_ep_write(struct raw_dev *dev, unsigned long value)
928{
929 int ret = 0;
930 char *data;
931 struct usb_raw_ep_io io;
932
933 data = raw_alloc_io_data(&io, (void __user *)value, true);
934 if (IS_ERR(data))
935 return PTR_ERR(data);
936 ret = raw_process_ep_io(dev, &io, data, true);
937 kfree(data);
938 return ret;
939}
940
941static int raw_ioctl_ep_read(struct raw_dev *dev, unsigned long value)
942{
943 int ret = 0;
944 char *data;
945 struct usb_raw_ep_io io;
946 unsigned int length;
947
948 data = raw_alloc_io_data(&io, (void __user *)value, false);
949 if (IS_ERR(data))
950 return PTR_ERR(data);
951 ret = raw_process_ep_io(dev, &io, data, false);
Dan Carpenter068fbff2020-04-06 17:51:19 +0300952 if (ret)
953 goto free;
954
Andrey Konovalovf2c2e712020-02-24 17:13:03 +0100955 length = min(io.length, (unsigned int)ret);
Dan Carpenter068fbff2020-04-06 17:51:19 +0300956 if (copy_to_user((void __user *)(value + sizeof(io)), data, length))
957 ret = -EFAULT;
958free:
Andrey Konovalovf2c2e712020-02-24 17:13:03 +0100959 kfree(data);
960 return ret;
961}
962
963static int raw_ioctl_configure(struct raw_dev *dev, unsigned long value)
964{
965 int ret = 0;
966 unsigned long flags;
967
968 if (value)
969 return -EINVAL;
970 spin_lock_irqsave(&dev->lock, flags);
971 if (dev->state != STATE_DEV_RUNNING) {
972 dev_dbg(dev->dev, "fail, device is not running\n");
973 ret = -EINVAL;
974 goto out_unlock;
975 }
976 if (!dev->gadget) {
977 dev_dbg(dev->dev, "fail, gadget is not bound\n");
978 ret = -EBUSY;
979 goto out_unlock;
980 }
981 usb_gadget_set_state(dev->gadget, USB_STATE_CONFIGURED);
982
983out_unlock:
984 spin_unlock_irqrestore(&dev->lock, flags);
985 return ret;
986}
987
988static int raw_ioctl_vbus_draw(struct raw_dev *dev, unsigned long value)
989{
990 int ret = 0;
991 unsigned long flags;
992
993 spin_lock_irqsave(&dev->lock, flags);
994 if (dev->state != STATE_DEV_RUNNING) {
995 dev_dbg(dev->dev, "fail, device is not running\n");
996 ret = -EINVAL;
997 goto out_unlock;
998 }
999 if (!dev->gadget) {
1000 dev_dbg(dev->dev, "fail, gadget is not bound\n");
1001 ret = -EBUSY;
1002 goto out_unlock;
1003 }
1004 usb_gadget_vbus_draw(dev->gadget, 2 * value);
1005
1006out_unlock:
1007 spin_unlock_irqrestore(&dev->lock, flags);
1008 return ret;
1009}
1010
1011static long raw_ioctl(struct file *fd, unsigned int cmd, unsigned long value)
1012{
1013 struct raw_dev *dev = fd->private_data;
1014 int ret = 0;
1015
1016 if (!dev)
1017 return -EBUSY;
1018
1019 switch (cmd) {
1020 case USB_RAW_IOCTL_INIT:
1021 ret = raw_ioctl_init(dev, value);
1022 break;
1023 case USB_RAW_IOCTL_RUN:
1024 ret = raw_ioctl_run(dev, value);
1025 break;
1026 case USB_RAW_IOCTL_EVENT_FETCH:
1027 ret = raw_ioctl_event_fetch(dev, value);
1028 break;
1029 case USB_RAW_IOCTL_EP0_WRITE:
1030 ret = raw_ioctl_ep0_write(dev, value);
1031 break;
1032 case USB_RAW_IOCTL_EP0_READ:
1033 ret = raw_ioctl_ep0_read(dev, value);
1034 break;
1035 case USB_RAW_IOCTL_EP_ENABLE:
1036 ret = raw_ioctl_ep_enable(dev, value);
1037 break;
1038 case USB_RAW_IOCTL_EP_DISABLE:
1039 ret = raw_ioctl_ep_disable(dev, value);
1040 break;
1041 case USB_RAW_IOCTL_EP_WRITE:
1042 ret = raw_ioctl_ep_write(dev, value);
1043 break;
1044 case USB_RAW_IOCTL_EP_READ:
1045 ret = raw_ioctl_ep_read(dev, value);
1046 break;
1047 case USB_RAW_IOCTL_CONFIGURE:
1048 ret = raw_ioctl_configure(dev, value);
1049 break;
1050 case USB_RAW_IOCTL_VBUS_DRAW:
1051 ret = raw_ioctl_vbus_draw(dev, value);
1052 break;
1053 default:
1054 ret = -EINVAL;
1055 }
1056
1057 return ret;
1058}
1059
1060/*----------------------------------------------------------------------*/
1061
1062static const struct file_operations raw_fops = {
1063 .open = raw_open,
1064 .unlocked_ioctl = raw_ioctl,
1065 .compat_ioctl = raw_ioctl,
1066 .release = raw_release,
1067 .llseek = no_llseek,
1068};
1069
1070static struct miscdevice raw_misc_device = {
1071 .minor = MISC_DYNAMIC_MINOR,
1072 .name = DRIVER_NAME,
1073 .fops = &raw_fops,
1074};
1075
1076module_misc_device(raw_misc_device);