perf python: Do not force closing original perf descriptor in evlist.get_pollfd()
[linux/fpc-iii.git] / drivers / usb / usbip / vudc_dev.c
blob1634d8698e151133ebcec50991cbc46657f4d3e4
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (C) 2015 Karol Kosik <karo9@interia.eu>
4 * Copyright (C) 2015-2016 Samsung Electronics
5 * Igor Kotrasinski <i.kotrasinsk@samsung.com>
6 * Krzysztof Opasiak <k.opasiak@samsung.com>
7 */
9 #include <linux/device.h>
10 #include <linux/kernel.h>
11 #include <linux/list.h>
12 #include <linux/platform_device.h>
13 #include <linux/usb.h>
14 #include <linux/usb/gadget.h>
15 #include <linux/usb/hcd.h>
16 #include <linux/kthread.h>
17 #include <linux/file.h>
18 #include <linux/byteorder/generic.h>
20 #include "usbip_common.h"
21 #include "vudc.h"
23 #define VIRTUAL_ENDPOINTS (1 /* ep0 */ + 15 /* in eps */ + 15 /* out eps */)
25 /* urb-related structures alloc / free */
28 static void free_urb(struct urb *urb)
30 if (!urb)
31 return;
33 kfree(urb->setup_packet);
34 urb->setup_packet = NULL;
36 kfree(urb->transfer_buffer);
37 urb->transfer_buffer = NULL;
39 usb_free_urb(urb);
42 struct urbp *alloc_urbp(void)
44 struct urbp *urb_p;
46 urb_p = kzalloc(sizeof(*urb_p), GFP_KERNEL);
47 if (!urb_p)
48 return urb_p;
50 urb_p->urb = NULL;
51 urb_p->ep = NULL;
52 INIT_LIST_HEAD(&urb_p->urb_entry);
53 return urb_p;
56 static void free_urbp(struct urbp *urb_p)
58 kfree(urb_p);
61 void free_urbp_and_urb(struct urbp *urb_p)
63 if (!urb_p)
64 return;
65 free_urb(urb_p->urb);
66 free_urbp(urb_p);
70 /* utilities ; almost verbatim from dummy_hcd.c */
72 /* called with spinlock held */
73 static void nuke(struct vudc *udc, struct vep *ep)
75 struct vrequest *req;
77 while (!list_empty(&ep->req_queue)) {
78 req = list_first_entry(&ep->req_queue, struct vrequest,
79 req_entry);
80 list_del_init(&req->req_entry);
81 req->req.status = -ESHUTDOWN;
83 spin_unlock(&udc->lock);
84 usb_gadget_giveback_request(&ep->ep, &req->req);
85 spin_lock(&udc->lock);
89 /* caller must hold lock */
90 static void stop_activity(struct vudc *udc)
92 int i;
93 struct urbp *urb_p, *tmp;
95 udc->address = 0;
97 for (i = 0; i < VIRTUAL_ENDPOINTS; i++)
98 nuke(udc, &udc->ep[i]);
100 list_for_each_entry_safe(urb_p, tmp, &udc->urb_queue, urb_entry) {
101 list_del(&urb_p->urb_entry);
102 free_urbp_and_urb(urb_p);
106 struct vep *vudc_find_endpoint(struct vudc *udc, u8 address)
108 int i;
110 if ((address & ~USB_DIR_IN) == 0)
111 return &udc->ep[0];
113 for (i = 1; i < VIRTUAL_ENDPOINTS; i++) {
114 struct vep *ep = &udc->ep[i];
116 if (!ep->desc)
117 continue;
118 if (ep->desc->bEndpointAddress == address)
119 return ep;
121 return NULL;
124 /* gadget ops */
126 static int vgadget_get_frame(struct usb_gadget *_gadget)
128 struct timespec64 now;
129 struct vudc *udc = usb_gadget_to_vudc(_gadget);
131 ktime_get_ts64(&now);
132 return ((now.tv_sec - udc->start_time.tv_sec) * 1000 +
133 (now.tv_nsec - udc->start_time.tv_nsec) / NSEC_PER_MSEC)
134 & 0x7FF;
137 static int vgadget_set_selfpowered(struct usb_gadget *_gadget, int value)
139 struct vudc *udc = usb_gadget_to_vudc(_gadget);
141 if (value)
142 udc->devstatus |= (1 << USB_DEVICE_SELF_POWERED);
143 else
144 udc->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED);
145 return 0;
148 static int vgadget_pullup(struct usb_gadget *_gadget, int value)
150 struct vudc *udc = usb_gadget_to_vudc(_gadget);
151 unsigned long flags;
152 int ret;
155 spin_lock_irqsave(&udc->lock, flags);
156 value = !!value;
157 if (value == udc->pullup)
158 goto unlock;
160 udc->pullup = value;
161 if (value) {
162 udc->gadget.speed = min_t(u8, USB_SPEED_HIGH,
163 udc->driver->max_speed);
164 udc->ep[0].ep.maxpacket = 64;
166 * This is the first place where we can ask our
167 * gadget driver for descriptors.
169 ret = get_gadget_descs(udc);
170 if (ret) {
171 dev_err(&udc->gadget.dev, "Unable go get desc: %d", ret);
172 goto unlock;
175 spin_unlock_irqrestore(&udc->lock, flags);
176 usbip_start_eh(&udc->ud);
177 } else {
178 /* Invalidate descriptors */
179 udc->desc_cached = 0;
181 spin_unlock_irqrestore(&udc->lock, flags);
182 usbip_event_add(&udc->ud, VUDC_EVENT_REMOVED);
183 usbip_stop_eh(&udc->ud); /* Wait for eh completion */
186 return 0;
188 unlock:
189 spin_unlock_irqrestore(&udc->lock, flags);
190 return 0;
193 static int vgadget_udc_start(struct usb_gadget *g,
194 struct usb_gadget_driver *driver)
196 struct vudc *udc = usb_gadget_to_vudc(g);
197 unsigned long flags;
199 spin_lock_irqsave(&udc->lock, flags);
200 udc->driver = driver;
201 udc->pullup = udc->connected = udc->desc_cached = 0;
202 spin_unlock_irqrestore(&udc->lock, flags);
204 return 0;
207 static int vgadget_udc_stop(struct usb_gadget *g)
209 struct vudc *udc = usb_gadget_to_vudc(g);
210 unsigned long flags;
212 spin_lock_irqsave(&udc->lock, flags);
213 udc->driver = NULL;
214 spin_unlock_irqrestore(&udc->lock, flags);
215 return 0;
218 static const struct usb_gadget_ops vgadget_ops = {
219 .get_frame = vgadget_get_frame,
220 .set_selfpowered = vgadget_set_selfpowered,
221 .pullup = vgadget_pullup,
222 .udc_start = vgadget_udc_start,
223 .udc_stop = vgadget_udc_stop,
227 /* endpoint ops */
229 static int vep_enable(struct usb_ep *_ep,
230 const struct usb_endpoint_descriptor *desc)
232 struct vep *ep;
233 struct vudc *udc;
234 unsigned int maxp;
235 unsigned long flags;
237 ep = to_vep(_ep);
238 udc = ep_to_vudc(ep);
240 if (!_ep || !desc || ep->desc || _ep->caps.type_control
241 || desc->bDescriptorType != USB_DT_ENDPOINT)
242 return -EINVAL;
244 if (!udc->driver)
245 return -ESHUTDOWN;
247 spin_lock_irqsave(&udc->lock, flags);
249 maxp = usb_endpoint_maxp(desc);
250 _ep->maxpacket = maxp;
251 ep->desc = desc;
252 ep->type = usb_endpoint_type(desc);
253 ep->halted = ep->wedged = 0;
255 spin_unlock_irqrestore(&udc->lock, flags);
257 return 0;
260 static int vep_disable(struct usb_ep *_ep)
262 struct vep *ep;
263 struct vudc *udc;
264 unsigned long flags;
266 ep = to_vep(_ep);
267 udc = ep_to_vudc(ep);
268 if (!_ep || !ep->desc || _ep->caps.type_control)
269 return -EINVAL;
271 spin_lock_irqsave(&udc->lock, flags);
272 ep->desc = NULL;
273 nuke(udc, ep);
274 spin_unlock_irqrestore(&udc->lock, flags);
276 return 0;
279 static struct usb_request *vep_alloc_request(struct usb_ep *_ep,
280 gfp_t mem_flags)
282 struct vrequest *req;
284 if (!_ep)
285 return NULL;
287 req = kzalloc(sizeof(*req), mem_flags);
288 if (!req)
289 return NULL;
291 INIT_LIST_HEAD(&req->req_entry);
293 return &req->req;
296 static void vep_free_request(struct usb_ep *_ep, struct usb_request *_req)
298 struct vrequest *req;
300 if (WARN_ON(!_ep || !_req))
301 return;
303 req = to_vrequest(_req);
304 kfree(req);
307 static int vep_queue(struct usb_ep *_ep, struct usb_request *_req,
308 gfp_t mem_flags)
310 struct vep *ep;
311 struct vrequest *req;
312 struct vudc *udc;
313 unsigned long flags;
315 if (!_ep || !_req)
316 return -EINVAL;
318 ep = to_vep(_ep);
319 req = to_vrequest(_req);
320 udc = ep_to_vudc(ep);
322 spin_lock_irqsave(&udc->lock, flags);
323 _req->actual = 0;
324 _req->status = -EINPROGRESS;
326 list_add_tail(&req->req_entry, &ep->req_queue);
327 spin_unlock_irqrestore(&udc->lock, flags);
329 return 0;
332 static int vep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
334 struct vep *ep;
335 struct vrequest *req;
336 struct vudc *udc;
337 struct vrequest *lst;
338 unsigned long flags;
339 int ret = -EINVAL;
341 if (!_ep || !_req)
342 return ret;
344 ep = to_vep(_ep);
345 req = to_vrequest(_req);
346 udc = req->udc;
348 if (!udc->driver)
349 return -ESHUTDOWN;
351 spin_lock_irqsave(&udc->lock, flags);
352 list_for_each_entry(lst, &ep->req_queue, req_entry) {
353 if (&lst->req == _req) {
354 list_del_init(&lst->req_entry);
355 _req->status = -ECONNRESET;
356 ret = 0;
357 break;
360 spin_unlock_irqrestore(&udc->lock, flags);
362 if (ret == 0)
363 usb_gadget_giveback_request(_ep, _req);
365 return ret;
368 static int
369 vep_set_halt_and_wedge(struct usb_ep *_ep, int value, int wedged)
371 struct vep *ep;
372 struct vudc *udc;
373 unsigned long flags;
374 int ret = 0;
376 ep = to_vep(_ep);
377 if (!_ep)
378 return -EINVAL;
380 udc = ep_to_vudc(ep);
381 if (!udc->driver)
382 return -ESHUTDOWN;
384 spin_lock_irqsave(&udc->lock, flags);
385 if (!value)
386 ep->halted = ep->wedged = 0;
387 else if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
388 !list_empty(&ep->req_queue))
389 ret = -EAGAIN;
390 else {
391 ep->halted = 1;
392 if (wedged)
393 ep->wedged = 1;
396 spin_unlock_irqrestore(&udc->lock, flags);
397 return ret;
400 static int
401 vep_set_halt(struct usb_ep *_ep, int value)
403 return vep_set_halt_and_wedge(_ep, value, 0);
406 static int vep_set_wedge(struct usb_ep *_ep)
408 return vep_set_halt_and_wedge(_ep, 1, 1);
411 static const struct usb_ep_ops vep_ops = {
412 .enable = vep_enable,
413 .disable = vep_disable,
415 .alloc_request = vep_alloc_request,
416 .free_request = vep_free_request,
418 .queue = vep_queue,
419 .dequeue = vep_dequeue,
421 .set_halt = vep_set_halt,
422 .set_wedge = vep_set_wedge,
426 /* shutdown / reset / error handlers */
428 static void vudc_shutdown(struct usbip_device *ud)
430 struct vudc *udc = container_of(ud, struct vudc, ud);
431 int call_disconnect = 0;
432 unsigned long flags;
434 dev_dbg(&udc->pdev->dev, "device shutdown");
435 if (ud->tcp_socket)
436 kernel_sock_shutdown(ud->tcp_socket, SHUT_RDWR);
438 if (ud->tcp_rx) {
439 kthread_stop_put(ud->tcp_rx);
440 ud->tcp_rx = NULL;
442 if (ud->tcp_tx) {
443 kthread_stop_put(ud->tcp_tx);
444 ud->tcp_tx = NULL;
447 if (ud->tcp_socket) {
448 sockfd_put(ud->tcp_socket);
449 ud->tcp_socket = NULL;
452 spin_lock_irqsave(&udc->lock, flags);
453 stop_activity(udc);
454 if (udc->connected && udc->driver->disconnect)
455 call_disconnect = 1;
456 udc->connected = 0;
457 spin_unlock_irqrestore(&udc->lock, flags);
458 if (call_disconnect)
459 udc->driver->disconnect(&udc->gadget);
462 static void vudc_device_reset(struct usbip_device *ud)
464 struct vudc *udc = container_of(ud, struct vudc, ud);
465 unsigned long flags;
467 dev_dbg(&udc->pdev->dev, "device reset");
468 spin_lock_irqsave(&udc->lock, flags);
469 stop_activity(udc);
470 spin_unlock_irqrestore(&udc->lock, flags);
471 if (udc->driver)
472 usb_gadget_udc_reset(&udc->gadget, udc->driver);
473 spin_lock_irqsave(&ud->lock, flags);
474 ud->status = SDEV_ST_AVAILABLE;
475 spin_unlock_irqrestore(&ud->lock, flags);
478 static void vudc_device_unusable(struct usbip_device *ud)
480 unsigned long flags;
482 spin_lock_irqsave(&ud->lock, flags);
483 ud->status = SDEV_ST_ERROR;
484 spin_unlock_irqrestore(&ud->lock, flags);
487 /* device setup / cleanup */
489 struct vudc_device *alloc_vudc_device(int devid)
491 struct vudc_device *udc_dev = NULL;
493 udc_dev = kzalloc(sizeof(*udc_dev), GFP_KERNEL);
494 if (!udc_dev)
495 goto out;
497 INIT_LIST_HEAD(&udc_dev->dev_entry);
499 udc_dev->pdev = platform_device_alloc(GADGET_NAME, devid);
500 if (!udc_dev->pdev) {
501 kfree(udc_dev);
502 udc_dev = NULL;
505 out:
506 return udc_dev;
509 void put_vudc_device(struct vudc_device *udc_dev)
511 platform_device_put(udc_dev->pdev);
512 kfree(udc_dev);
515 static int init_vudc_hw(struct vudc *udc)
517 int i;
518 struct usbip_device *ud = &udc->ud;
519 struct vep *ep;
521 udc->ep = kcalloc(VIRTUAL_ENDPOINTS, sizeof(*udc->ep), GFP_KERNEL);
522 if (!udc->ep)
523 goto nomem_ep;
525 INIT_LIST_HEAD(&udc->gadget.ep_list);
527 /* create ep0 and 15 in, 15 out general purpose eps */
528 for (i = 0; i < VIRTUAL_ENDPOINTS; ++i) {
529 int is_out = i % 2;
530 int num = (i + 1) / 2;
532 ep = &udc->ep[i];
534 sprintf(ep->name, "ep%d%s", num,
535 i ? (is_out ? "out" : "in") : "");
536 ep->ep.name = ep->name;
538 ep->ep.ops = &vep_ops;
540 usb_ep_set_maxpacket_limit(&ep->ep, ~0);
541 ep->ep.max_streams = 16;
542 ep->gadget = &udc->gadget;
543 INIT_LIST_HEAD(&ep->req_queue);
545 if (i == 0) {
546 /* ep0 */
547 ep->ep.caps.type_control = true;
548 ep->ep.caps.dir_out = true;
549 ep->ep.caps.dir_in = true;
551 udc->gadget.ep0 = &ep->ep;
552 } else {
553 /* All other eps */
554 ep->ep.caps.type_iso = true;
555 ep->ep.caps.type_int = true;
556 ep->ep.caps.type_bulk = true;
558 if (is_out)
559 ep->ep.caps.dir_out = true;
560 else
561 ep->ep.caps.dir_in = true;
563 list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list);
567 spin_lock_init(&udc->lock);
568 spin_lock_init(&udc->lock_tx);
569 INIT_LIST_HEAD(&udc->urb_queue);
570 INIT_LIST_HEAD(&udc->tx_queue);
571 init_waitqueue_head(&udc->tx_waitq);
573 spin_lock_init(&ud->lock);
574 ud->status = SDEV_ST_AVAILABLE;
575 ud->side = USBIP_VUDC;
577 ud->eh_ops.shutdown = vudc_shutdown;
578 ud->eh_ops.reset = vudc_device_reset;
579 ud->eh_ops.unusable = vudc_device_unusable;
581 v_init_timer(udc);
582 return 0;
584 nomem_ep:
585 return -ENOMEM;
588 static void cleanup_vudc_hw(struct vudc *udc)
590 kfree(udc->ep);
593 /* platform driver ops */
595 int vudc_probe(struct platform_device *pdev)
597 struct vudc *udc;
598 int ret = -ENOMEM;
600 udc = kzalloc(sizeof(*udc), GFP_KERNEL);
601 if (!udc)
602 goto out;
604 udc->gadget.name = GADGET_NAME;
605 udc->gadget.ops = &vgadget_ops;
606 udc->gadget.max_speed = USB_SPEED_HIGH;
607 udc->gadget.dev.parent = &pdev->dev;
608 udc->pdev = pdev;
610 ret = init_vudc_hw(udc);
611 if (ret)
612 goto err_init_vudc_hw;
614 ret = usb_add_gadget_udc(&pdev->dev, &udc->gadget);
615 if (ret < 0)
616 goto err_add_udc;
618 ret = sysfs_create_group(&pdev->dev.kobj, &vudc_attr_group);
619 if (ret) {
620 dev_err(&udc->pdev->dev, "create sysfs files\n");
621 goto err_sysfs;
624 platform_set_drvdata(pdev, udc);
626 return ret;
628 err_sysfs:
629 usb_del_gadget_udc(&udc->gadget);
630 err_add_udc:
631 cleanup_vudc_hw(udc);
632 err_init_vudc_hw:
633 kfree(udc);
634 out:
635 return ret;
638 int vudc_remove(struct platform_device *pdev)
640 struct vudc *udc = platform_get_drvdata(pdev);
642 sysfs_remove_group(&pdev->dev.kobj, &vudc_attr_group);
643 usb_del_gadget_udc(&udc->gadget);
644 cleanup_vudc_hw(udc);
645 kfree(udc);
646 return 0;