blob: 8994a13819ab96563e08714fd447a39a8d29ab64 [file] [log] [blame]
Igor Kotrasinskib6a0ca12016-03-08 21:49:02 +01001/*
2 * Copyright (C) 2015 Karol Kosik <karo9@interia.eu>
3 * Copyright (C) 2015-2016 Samsung Electronics
4 * Igor Kotrasinski <i.kotrasinsk@samsung.com>
5 * Krzysztof Opasiak <k.opasiak@samsung.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#include <linux/device.h>
22#include <linux/kernel.h>
23#include <linux/list.h>
24#include <linux/platform_device.h>
25#include <linux/usb.h>
26#include <linux/usb/gadget.h>
27#include <linux/usb/hcd.h>
28#include <linux/kthread.h>
29#include <linux/file.h>
30#include <linux/byteorder/generic.h>
31
32#include "usbip_common.h"
33#include "vudc.h"
34
35#define VIRTUAL_ENDPOINTS (1 /* ep0 */ + 15 /* in eps */ + 15 /* out eps */)
36
37/* urb-related structures alloc / free */
38
39
40static void free_urb(struct urb *urb)
41{
42 if (!urb)
43 return;
44
45 kfree(urb->setup_packet);
46 urb->setup_packet = NULL;
47
48 kfree(urb->transfer_buffer);
49 urb->transfer_buffer = NULL;
50
51 usb_free_urb(urb);
52}
53
54struct urbp *alloc_urbp(void)
55{
56 struct urbp *urb_p;
57
58 urb_p = kzalloc(sizeof(*urb_p), GFP_KERNEL);
59 if (!urb_p)
60 return urb_p;
61
62 urb_p->urb = NULL;
63 urb_p->ep = NULL;
64 INIT_LIST_HEAD(&urb_p->urb_entry);
65 return urb_p;
66}
67
68static void free_urbp(struct urbp *urb_p)
69{
70 kfree(urb_p);
71}
72
73void free_urbp_and_urb(struct urbp *urb_p)
74{
75 if (!urb_p)
76 return;
77 free_urb(urb_p->urb);
78 free_urbp(urb_p);
79}
80
81
82/* utilities ; almost verbatim from dummy_hcd.c */
83
84/* called with spinlock held */
85static void nuke(struct vudc *udc, struct vep *ep)
86{
87 struct vrequest *req;
88
89 while (!list_empty(&ep->req_queue)) {
90 req = list_first_entry(&ep->req_queue, struct vrequest,
91 req_entry);
92 list_del_init(&req->req_entry);
93 req->req.status = -ESHUTDOWN;
94
95 spin_unlock(&udc->lock);
96 usb_gadget_giveback_request(&ep->ep, &req->req);
97 spin_lock(&udc->lock);
98 }
99}
100
101/* caller must hold lock */
102static void stop_activity(struct vudc *udc)
103{
104 int i;
105 struct urbp *urb_p, *tmp;
106
107 udc->address = 0;
108
109 for (i = 0; i < VIRTUAL_ENDPOINTS; i++)
110 nuke(udc, &udc->ep[i]);
111
112 list_for_each_entry_safe(urb_p, tmp, &udc->urb_queue, urb_entry) {
113 list_del(&urb_p->urb_entry);
114 free_urbp_and_urb(urb_p);
115 }
116}
117
Krzysztof Opasiak0255cf92016-04-27 20:02:07 +0200118struct vep *vudc_find_endpoint(struct vudc *udc, u8 address)
Igor Kotrasinskib6a0ca12016-03-08 21:49:02 +0100119{
120 int i;
121
122 if ((address & ~USB_DIR_IN) == 0)
123 return &udc->ep[0];
124
125 for (i = 1; i < VIRTUAL_ENDPOINTS; i++) {
126 struct vep *ep = &udc->ep[i];
127
128 if (!ep->desc)
129 continue;
130 if (ep->desc->bEndpointAddress == address)
131 return ep;
132 }
133 return NULL;
134}
135
136/* gadget ops */
137
138/* FIXME - this will probably misbehave when suspend/resume is added */
139static int vgadget_get_frame(struct usb_gadget *_gadget)
140{
141 struct timeval now;
142 struct vudc *udc = usb_gadget_to_vudc(_gadget);
143
144 do_gettimeofday(&now);
145 return ((now.tv_sec - udc->start_time.tv_sec) * 1000 +
146 (now.tv_usec - udc->start_time.tv_usec) / 1000)
147 % 0x7FF;
148}
149
150static int vgadget_set_selfpowered(struct usb_gadget *_gadget, int value)
151{
152 struct vudc *udc = usb_gadget_to_vudc(_gadget);
153
154 if (value)
155 udc->devstatus |= (1 << USB_DEVICE_SELF_POWERED);
156 else
157 udc->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED);
158 return 0;
159}
160
161static int vgadget_pullup(struct usb_gadget *_gadget, int value)
162{
163 struct vudc *udc = usb_gadget_to_vudc(_gadget);
164 unsigned long flags;
165 int ret;
166
167
168 spin_lock_irqsave(&udc->lock, flags);
169 value = !!value;
170 if (value == udc->pullup)
171 goto unlock;
172
173 udc->pullup = value;
174 if (value) {
175 udc->gadget.speed = min_t(u8, USB_SPEED_HIGH,
176 udc->driver->max_speed);
177 udc->ep[0].ep.maxpacket = 64;
178 /*
179 * This is the first place where we can ask our
180 * gadget driver for descriptors.
181 */
182 ret = get_gadget_descs(udc);
183 if (ret) {
184 dev_err(&udc->gadget.dev, "Unable go get desc: %d", ret);
185 goto unlock;
186 }
187
188 spin_unlock_irqrestore(&udc->lock, flags);
189 usbip_start_eh(&udc->ud);
190 } else {
191 /* Invalidate descriptors */
192 udc->desc_cached = 0;
193
194 spin_unlock_irqrestore(&udc->lock, flags);
195 usbip_event_add(&udc->ud, VUDC_EVENT_REMOVED);
196 usbip_stop_eh(&udc->ud); /* Wait for eh completion */
197 }
198
199 return 0;
200
201unlock:
202 spin_unlock_irqrestore(&udc->lock, flags);
203 return 0;
204}
205
206static int vgadget_udc_start(struct usb_gadget *g,
207 struct usb_gadget_driver *driver)
208{
209 struct vudc *udc = usb_gadget_to_vudc(g);
210 unsigned long flags;
211
212 spin_lock_irqsave(&udc->lock, flags);
213 udc->driver = driver;
214 udc->pullup = udc->connected = udc->desc_cached = 0;
215 spin_unlock_irqrestore(&udc->lock, flags);
216
217 return 0;
218}
219
220static int vgadget_udc_stop(struct usb_gadget *g)
221{
222 struct vudc *udc = usb_gadget_to_vudc(g);
223 unsigned long flags;
224
225 spin_lock_irqsave(&udc->lock, flags);
226 udc->driver = NULL;
227 spin_unlock_irqrestore(&udc->lock, flags);
228 return 0;
229}
230
231static const struct usb_gadget_ops vgadget_ops = {
232 .get_frame = vgadget_get_frame,
233 .set_selfpowered = vgadget_set_selfpowered,
234 .pullup = vgadget_pullup,
235 .udc_start = vgadget_udc_start,
236 .udc_stop = vgadget_udc_stop,
237};
238
239
240/* endpoint ops */
241
242static int vep_enable(struct usb_ep *_ep,
243 const struct usb_endpoint_descriptor *desc)
244{
245 struct vep *ep;
246 struct vudc *udc;
247 unsigned maxp;
248 unsigned long flags;
249
250 ep = to_vep(_ep);
251 udc = ep_to_vudc(ep);
252
253 if (!_ep || !desc || ep->desc || _ep->caps.type_control
254 || desc->bDescriptorType != USB_DT_ENDPOINT)
255 return -EINVAL;
256
257 if (!udc->driver)
258 return -ESHUTDOWN;
259
260 spin_lock_irqsave(&udc->lock, flags);
261
262 maxp = usb_endpoint_maxp(desc) & 0x7ff;
263 _ep->maxpacket = maxp;
264 ep->desc = desc;
265 ep->type = usb_endpoint_type(desc);
266 ep->halted = ep->wedged = 0;
267
268 spin_unlock_irqrestore(&udc->lock, flags);
269
270 return 0;
271}
272
273static int vep_disable(struct usb_ep *_ep)
274{
275 struct vep *ep;
276 struct vudc *udc;
277 unsigned long flags;
278
279 ep = to_vep(_ep);
280 udc = ep_to_vudc(ep);
281 if (!_ep || !ep->desc || _ep->caps.type_control)
282 return -EINVAL;
283
284 spin_lock_irqsave(&udc->lock, flags);
285 ep->desc = NULL;
286 nuke(udc, ep);
287 spin_unlock_irqrestore(&udc->lock, flags);
288
289 return 0;
290}
291
292static struct usb_request *vep_alloc_request(struct usb_ep *_ep,
293 gfp_t mem_flags)
294{
295 struct vep *ep;
296 struct vrequest *req;
297
298 if (!_ep)
299 return NULL;
300 ep = to_vep(_ep);
301
302 req = kzalloc(sizeof(*req), mem_flags);
303 if (!req)
304 return NULL;
305
306 INIT_LIST_HEAD(&req->req_entry);
307
308 return &req->req;
309}
310
311static void vep_free_request(struct usb_ep *_ep, struct usb_request *_req)
312{
313 struct vrequest *req;
314
Krzysztof Opasiak2bdf6ea2016-04-27 20:00:26 +0200315 if (WARN_ON(!_ep || !_req))
Igor Kotrasinskib6a0ca12016-03-08 21:49:02 +0100316 return;
Krzysztof Opasiak2bdf6ea2016-04-27 20:00:26 +0200317
Igor Kotrasinskib6a0ca12016-03-08 21:49:02 +0100318 req = to_vrequest(_req);
319 kfree(req);
320}
321
322static int vep_queue(struct usb_ep *_ep, struct usb_request *_req,
323 gfp_t mem_flags)
324{
325 struct vep *ep;
326 struct vrequest *req;
327 struct vudc *udc;
328 unsigned long flags;
329
330 if (!_ep || !_req)
331 return -EINVAL;
332
333 ep = to_vep(_ep);
334 req = to_vrequest(_req);
335 udc = ep_to_vudc(ep);
336
337 spin_lock_irqsave(&udc->lock, flags);
338 _req->actual = 0;
339 _req->status = -EINPROGRESS;
340
341 list_add_tail(&req->req_entry, &ep->req_queue);
342 spin_unlock_irqrestore(&udc->lock, flags);
343
344 return 0;
345}
346
347static int vep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
348{
349 struct vep *ep;
350 struct vrequest *req;
351 struct vudc *udc;
352 struct vrequest *lst;
353 unsigned long flags;
354 int ret = -EINVAL;
355
356 if (!_ep || !_req)
357 return ret;
358
359 ep = to_vep(_ep);
360 req = to_vrequest(_req);
361 udc = req->udc;
362
363 if (!udc->driver)
364 return -ESHUTDOWN;
365
366 spin_lock_irqsave(&udc->lock, flags);
367 list_for_each_entry(lst, &ep->req_queue, req_entry) {
368 if (&lst->req == _req) {
369 list_del_init(&lst->req_entry);
370 _req->status = -ECONNRESET;
371 ret = 0;
372 break;
373 }
374 }
375 spin_unlock_irqrestore(&udc->lock, flags);
376
377 if (ret == 0)
378 usb_gadget_giveback_request(_ep, _req);
379
380 return ret;
381}
382
383static int
384vep_set_halt_and_wedge(struct usb_ep *_ep, int value, int wedged)
385{
386 struct vep *ep;
387 struct vudc *udc;
388 unsigned long flags;
389 int ret = 0;
390
391 ep = to_vep(_ep);
392 if (!_ep)
393 return -EINVAL;
394
395 udc = ep_to_vudc(ep);
396 if (!udc->driver)
397 return -ESHUTDOWN;
398
399 spin_lock_irqsave(&udc->lock, flags);
400 if (!value)
401 ep->halted = ep->wedged = 0;
402 else if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
403 !list_empty(&ep->req_queue))
404 ret = -EAGAIN;
405 else {
406 ep->halted = 1;
407 if (wedged)
408 ep->wedged = 1;
409 }
410
411 spin_unlock_irqrestore(&udc->lock, flags);
412 return ret;
413}
414
415static int
416vep_set_halt(struct usb_ep *_ep, int value)
417{
418 return vep_set_halt_and_wedge(_ep, value, 0);
419}
420
421static int vep_set_wedge(struct usb_ep *_ep)
422{
423 return vep_set_halt_and_wedge(_ep, 1, 1);
424}
425
426static const struct usb_ep_ops vep_ops = {
427 .enable = vep_enable,
428 .disable = vep_disable,
429
430 .alloc_request = vep_alloc_request,
431 .free_request = vep_free_request,
432
433 .queue = vep_queue,
434 .dequeue = vep_dequeue,
435
436 .set_halt = vep_set_halt,
437 .set_wedge = vep_set_wedge,
438};
439
440
441/* shutdown / reset / error handlers */
442
443static void vudc_shutdown(struct usbip_device *ud)
444{
445 struct vudc *udc = container_of(ud, struct vudc, ud);
446 int call_disconnect = 0;
447 unsigned long flags;
448
449 dev_dbg(&udc->pdev->dev, "device shutdown");
450 if (ud->tcp_socket)
451 kernel_sock_shutdown(ud->tcp_socket, SHUT_RDWR);
452
453 if (ud->tcp_tx) {
454 kthread_stop_put(ud->tcp_rx);
455 ud->tcp_rx = NULL;
456 }
457 if (ud->tcp_tx) {
458 kthread_stop_put(ud->tcp_tx);
459 ud->tcp_tx = NULL;
460 }
461
462 if (ud->tcp_socket) {
463 sockfd_put(ud->tcp_socket);
464 ud->tcp_socket = NULL;
465 }
466
467 spin_lock_irqsave(&udc->lock, flags);
468 stop_activity(udc);
469 if (udc->connected && udc->driver->disconnect)
470 call_disconnect = 1;
471 udc->connected = 0;
472 spin_unlock_irqrestore(&udc->lock, flags);
473 if (call_disconnect)
474 udc->driver->disconnect(&udc->gadget);
475}
476
477static void vudc_device_reset(struct usbip_device *ud)
478{
479 struct vudc *udc = container_of(ud, struct vudc, ud);
480 unsigned long flags;
481
482 dev_dbg(&udc->pdev->dev, "device reset");
483 spin_lock_irqsave(&udc->lock, flags);
484 stop_activity(udc);
485 spin_unlock_irqrestore(&udc->lock, flags);
486 if (udc->driver)
487 usb_gadget_udc_reset(&udc->gadget, udc->driver);
488 spin_lock_irqsave(&ud->lock, flags);
489 ud->status = SDEV_ST_AVAILABLE;
490 spin_unlock_irqrestore(&ud->lock, flags);
491}
492
493static void vudc_device_unusable(struct usbip_device *ud)
494{
495 unsigned long flags;
496
497 spin_lock_irqsave(&ud->lock, flags);
498 ud->status = SDEV_ST_ERROR;
499 spin_unlock_irqrestore(&ud->lock, flags);
500}
501
502/* device setup / cleanup */
503
504struct vudc_device *alloc_vudc_device(int devid)
505{
506 struct vudc_device *udc_dev = NULL;
507
508 udc_dev = kzalloc(sizeof(*udc_dev), GFP_KERNEL);
509 if (!udc_dev)
510 goto out;
511
512 INIT_LIST_HEAD(&udc_dev->dev_entry);
513
514 udc_dev->pdev = platform_device_alloc(GADGET_NAME, devid);
515 if (!udc_dev->pdev) {
516 kfree(udc_dev);
517 udc_dev = NULL;
518 }
519
520out:
521 return udc_dev;
522}
523
524void put_vudc_device(struct vudc_device *udc_dev)
525{
526 platform_device_put(udc_dev->pdev);
527 kfree(udc_dev);
528}
529
530static int init_vudc_hw(struct vudc *udc)
531{
532 int i;
533 struct usbip_device *ud = &udc->ud;
534 struct vep *ep;
535
536 udc->ep = kcalloc(VIRTUAL_ENDPOINTS, sizeof(*udc->ep), GFP_KERNEL);
537 if (!udc->ep)
538 goto nomem_ep;
539
540 INIT_LIST_HEAD(&udc->gadget.ep_list);
541
542 /* create ep0 and 15 in, 15 out general purpose eps */
543 for (i = 0; i < VIRTUAL_ENDPOINTS; ++i) {
544 int is_out = i % 2;
545 int num = (i + 1) / 2;
546
547 ep = &udc->ep[i];
548
549 sprintf(ep->name, "ep%d%s", num,
550 i ? (is_out ? "out" : "in") : "");
551 ep->ep.name = ep->name;
552 if (i == 0) {
553 ep->ep.caps.type_control = true;
554 ep->ep.caps.dir_out = true;
555 ep->ep.caps.dir_in = true;
556 } else {
557 ep->ep.caps.type_iso = true;
558 ep->ep.caps.type_int = true;
559 ep->ep.caps.type_bulk = true;
560 }
561
562 if (is_out)
563 ep->ep.caps.dir_out = true;
564 else
565 ep->ep.caps.dir_in = true;
566
567 ep->ep.ops = &vep_ops;
568 list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list);
569 ep->halted = ep->wedged = ep->already_seen =
570 ep->setup_stage = 0;
571 usb_ep_set_maxpacket_limit(&ep->ep, ~0);
572 ep->ep.max_streams = 16;
573 ep->gadget = &udc->gadget;
574 ep->desc = NULL;
575 INIT_LIST_HEAD(&ep->req_queue);
576 }
577
578 spin_lock_init(&udc->lock);
579 spin_lock_init(&udc->lock_tx);
580 INIT_LIST_HEAD(&udc->urb_queue);
581 INIT_LIST_HEAD(&udc->tx_queue);
582 init_waitqueue_head(&udc->tx_waitq);
583
584 spin_lock_init(&ud->lock);
585 ud->status = SDEV_ST_AVAILABLE;
586 ud->side = USBIP_VUDC;
587
588 ud->eh_ops.shutdown = vudc_shutdown;
589 ud->eh_ops.reset = vudc_device_reset;
590 ud->eh_ops.unusable = vudc_device_unusable;
591
592 udc->gadget.ep0 = &udc->ep[0].ep;
593 list_del_init(&udc->ep[0].ep.ep_list);
594
595 v_init_timer(udc);
596 return 0;
597
598nomem_ep:
599 return -ENOMEM;
600}
601
602static void cleanup_vudc_hw(struct vudc *udc)
603{
604 kfree(udc->ep);
605}
606
607/* platform driver ops */
608
609int vudc_probe(struct platform_device *pdev)
610{
611 struct vudc *udc;
612 int ret = -ENOMEM;
613
614 udc = kzalloc(sizeof(*udc), GFP_KERNEL);
615 if (!udc)
616 goto out;
617
618 udc->gadget.name = GADGET_NAME;
619 udc->gadget.ops = &vgadget_ops;
620 udc->gadget.max_speed = USB_SPEED_HIGH;
621 udc->gadget.dev.parent = &pdev->dev;
622 udc->pdev = pdev;
623
624 ret = init_vudc_hw(udc);
625 if (ret)
626 goto err_init_vudc_hw;
627
628 ret = usb_add_gadget_udc(&pdev->dev, &udc->gadget);
629 if (ret < 0)
630 goto err_add_udc;
631
632 ret = sysfs_create_group(&pdev->dev.kobj, &vudc_attr_group);
633 if (ret) {
634 dev_err(&udc->pdev->dev, "create sysfs files\n");
635 goto err_sysfs;
636 }
637
638 platform_set_drvdata(pdev, udc);
639
640 return ret;
641
642err_sysfs:
643 usb_del_gadget_udc(&udc->gadget);
644err_add_udc:
645 cleanup_vudc_hw(udc);
646err_init_vudc_hw:
647 kfree(udc);
648out:
649 return ret;
650}
651
652int vudc_remove(struct platform_device *pdev)
653{
654 struct vudc *udc = platform_get_drvdata(pdev);
655
656 sysfs_remove_group(&pdev->dev.kobj, &vudc_attr_group);
657 usb_del_gadget_udc(&udc->gadget);
658 cleanup_vudc_hw(udc);
659 kfree(udc);
660 return 0;
661}