1 // SPDX-License-Identifier: GPL-2.0+
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>
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"
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
)
33 kfree(urb
->setup_packet
);
34 urb
->setup_packet
= NULL
;
36 kfree(urb
->transfer_buffer
);
37 urb
->transfer_buffer
= NULL
;
42 struct urbp
*alloc_urbp(void)
46 urb_p
= kzalloc(sizeof(*urb_p
), GFP_KERNEL
);
52 INIT_LIST_HEAD(&urb_p
->urb_entry
);
56 static void free_urbp(struct urbp
*urb_p
)
61 void free_urbp_and_urb(struct 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
)
77 while (!list_empty(&ep
->req_queue
)) {
78 req
= list_first_entry(&ep
->req_queue
, struct vrequest
,
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
)
93 struct urbp
*urb_p
, *tmp
;
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
)
110 if ((address
& ~USB_DIR_IN
) == 0)
113 for (i
= 1; i
< VIRTUAL_ENDPOINTS
; i
++) {
114 struct vep
*ep
= &udc
->ep
[i
];
118 if (ep
->desc
->bEndpointAddress
== address
)
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
)
137 static int vgadget_set_selfpowered(struct usb_gadget
*_gadget
, int value
)
139 struct vudc
*udc
= usb_gadget_to_vudc(_gadget
);
142 udc
->devstatus
|= (1 << USB_DEVICE_SELF_POWERED
);
144 udc
->devstatus
&= ~(1 << USB_DEVICE_SELF_POWERED
);
148 static int vgadget_pullup(struct usb_gadget
*_gadget
, int value
)
150 struct vudc
*udc
= usb_gadget_to_vudc(_gadget
);
155 spin_lock_irqsave(&udc
->lock
, flags
);
157 if (value
== udc
->pullup
)
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
);
171 dev_err(&udc
->gadget
.dev
, "Unable go get desc: %d", ret
);
175 spin_unlock_irqrestore(&udc
->lock
, flags
);
176 usbip_start_eh(&udc
->ud
);
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 */
189 spin_unlock_irqrestore(&udc
->lock
, flags
);
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
);
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
);
207 static int vgadget_udc_stop(struct usb_gadget
*g
)
209 struct vudc
*udc
= usb_gadget_to_vudc(g
);
212 spin_lock_irqsave(&udc
->lock
, flags
);
214 spin_unlock_irqrestore(&udc
->lock
, flags
);
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
,
229 static int vep_enable(struct usb_ep
*_ep
,
230 const struct usb_endpoint_descriptor
*desc
)
238 udc
= ep_to_vudc(ep
);
240 if (!_ep
|| !desc
|| ep
->desc
|| _ep
->caps
.type_control
241 || desc
->bDescriptorType
!= USB_DT_ENDPOINT
)
247 spin_lock_irqsave(&udc
->lock
, flags
);
249 maxp
= usb_endpoint_maxp(desc
);
250 _ep
->maxpacket
= maxp
;
252 ep
->type
= usb_endpoint_type(desc
);
253 ep
->halted
= ep
->wedged
= 0;
255 spin_unlock_irqrestore(&udc
->lock
, flags
);
260 static int vep_disable(struct usb_ep
*_ep
)
267 udc
= ep_to_vudc(ep
);
268 if (!_ep
|| !ep
->desc
|| _ep
->caps
.type_control
)
271 spin_lock_irqsave(&udc
->lock
, flags
);
274 spin_unlock_irqrestore(&udc
->lock
, flags
);
279 static struct usb_request
*vep_alloc_request(struct usb_ep
*_ep
,
282 struct vrequest
*req
;
287 req
= kzalloc(sizeof(*req
), mem_flags
);
291 INIT_LIST_HEAD(&req
->req_entry
);
296 static void vep_free_request(struct usb_ep
*_ep
, struct usb_request
*_req
)
298 struct vrequest
*req
;
300 /* ep is always valid here - see usb_ep_free_request() */
304 req
= to_vrequest(_req
);
308 static int vep_queue(struct usb_ep
*_ep
, struct usb_request
*_req
,
312 struct vrequest
*req
;
320 req
= to_vrequest(_req
);
321 udc
= ep_to_vudc(ep
);
323 spin_lock_irqsave(&udc
->lock
, flags
);
325 _req
->status
= -EINPROGRESS
;
327 list_add_tail(&req
->req_entry
, &ep
->req_queue
);
328 spin_unlock_irqrestore(&udc
->lock
, flags
);
333 static int vep_dequeue(struct usb_ep
*_ep
, struct usb_request
*_req
)
336 struct vrequest
*req
;
338 struct vrequest
*lst
;
346 req
= to_vrequest(_req
);
352 spin_lock_irqsave(&udc
->lock
, flags
);
353 list_for_each_entry(lst
, &ep
->req_queue
, req_entry
) {
354 if (&lst
->req
== _req
) {
355 list_del_init(&lst
->req_entry
);
356 _req
->status
= -ECONNRESET
;
361 spin_unlock_irqrestore(&udc
->lock
, flags
);
364 usb_gadget_giveback_request(_ep
, _req
);
370 vep_set_halt_and_wedge(struct usb_ep
*_ep
, int value
, int wedged
)
381 udc
= ep_to_vudc(ep
);
385 spin_lock_irqsave(&udc
->lock
, flags
);
387 ep
->halted
= ep
->wedged
= 0;
388 else if (ep
->desc
&& (ep
->desc
->bEndpointAddress
& USB_DIR_IN
) &&
389 !list_empty(&ep
->req_queue
))
397 spin_unlock_irqrestore(&udc
->lock
, flags
);
402 vep_set_halt(struct usb_ep
*_ep
, int value
)
404 return vep_set_halt_and_wedge(_ep
, value
, 0);
407 static int vep_set_wedge(struct usb_ep
*_ep
)
409 return vep_set_halt_and_wedge(_ep
, 1, 1);
412 static const struct usb_ep_ops vep_ops
= {
413 .enable
= vep_enable
,
414 .disable
= vep_disable
,
416 .alloc_request
= vep_alloc_request
,
417 .free_request
= vep_free_request
,
420 .dequeue
= vep_dequeue
,
422 .set_halt
= vep_set_halt
,
423 .set_wedge
= vep_set_wedge
,
427 /* shutdown / reset / error handlers */
429 static void vudc_shutdown(struct usbip_device
*ud
)
431 struct vudc
*udc
= container_of(ud
, struct vudc
, ud
);
432 int call_disconnect
= 0;
435 dev_dbg(&udc
->pdev
->dev
, "device shutdown");
437 kernel_sock_shutdown(ud
->tcp_socket
, SHUT_RDWR
);
440 kthread_stop_put(ud
->tcp_rx
);
444 kthread_stop_put(ud
->tcp_tx
);
448 if (ud
->tcp_socket
) {
449 sockfd_put(ud
->tcp_socket
);
450 ud
->tcp_socket
= NULL
;
453 spin_lock_irqsave(&udc
->lock
, flags
);
455 if (udc
->connected
&& udc
->driver
->disconnect
)
458 spin_unlock_irqrestore(&udc
->lock
, flags
);
460 udc
->driver
->disconnect(&udc
->gadget
);
463 static void vudc_device_reset(struct usbip_device
*ud
)
465 struct vudc
*udc
= container_of(ud
, struct vudc
, ud
);
468 dev_dbg(&udc
->pdev
->dev
, "device reset");
469 spin_lock_irqsave(&udc
->lock
, flags
);
471 spin_unlock_irqrestore(&udc
->lock
, flags
);
473 usb_gadget_udc_reset(&udc
->gadget
, udc
->driver
);
474 spin_lock_irqsave(&ud
->lock
, flags
);
475 ud
->status
= SDEV_ST_AVAILABLE
;
476 spin_unlock_irqrestore(&ud
->lock
, flags
);
479 static void vudc_device_unusable(struct usbip_device
*ud
)
483 spin_lock_irqsave(&ud
->lock
, flags
);
484 ud
->status
= SDEV_ST_ERROR
;
485 spin_unlock_irqrestore(&ud
->lock
, flags
);
488 /* device setup / cleanup */
490 struct vudc_device
*alloc_vudc_device(int devid
)
492 struct vudc_device
*udc_dev
= NULL
;
494 udc_dev
= kzalloc(sizeof(*udc_dev
), GFP_KERNEL
);
498 INIT_LIST_HEAD(&udc_dev
->dev_entry
);
500 udc_dev
->pdev
= platform_device_alloc(GADGET_NAME
, devid
);
501 if (!udc_dev
->pdev
) {
510 void put_vudc_device(struct vudc_device
*udc_dev
)
512 platform_device_put(udc_dev
->pdev
);
516 static int init_vudc_hw(struct vudc
*udc
)
519 struct usbip_device
*ud
= &udc
->ud
;
522 udc
->ep
= kcalloc(VIRTUAL_ENDPOINTS
, sizeof(*udc
->ep
), GFP_KERNEL
);
526 INIT_LIST_HEAD(&udc
->gadget
.ep_list
);
528 /* create ep0 and 15 in, 15 out general purpose eps */
529 for (i
= 0; i
< VIRTUAL_ENDPOINTS
; ++i
) {
531 int num
= (i
+ 1) / 2;
535 sprintf(ep
->name
, "ep%d%s", num
,
536 i
? (is_out
? "out" : "in") : "");
537 ep
->ep
.name
= ep
->name
;
539 ep
->ep
.ops
= &vep_ops
;
541 usb_ep_set_maxpacket_limit(&ep
->ep
, ~0);
542 ep
->ep
.max_streams
= 16;
543 ep
->gadget
= &udc
->gadget
;
544 INIT_LIST_HEAD(&ep
->req_queue
);
548 ep
->ep
.caps
.type_control
= true;
549 ep
->ep
.caps
.dir_out
= true;
550 ep
->ep
.caps
.dir_in
= true;
552 udc
->gadget
.ep0
= &ep
->ep
;
555 ep
->ep
.caps
.type_iso
= true;
556 ep
->ep
.caps
.type_int
= true;
557 ep
->ep
.caps
.type_bulk
= true;
560 ep
->ep
.caps
.dir_out
= true;
562 ep
->ep
.caps
.dir_in
= true;
564 list_add_tail(&ep
->ep
.ep_list
, &udc
->gadget
.ep_list
);
568 spin_lock_init(&udc
->lock
);
569 spin_lock_init(&udc
->lock_tx
);
570 INIT_LIST_HEAD(&udc
->urb_queue
);
571 INIT_LIST_HEAD(&udc
->tx_queue
);
572 init_waitqueue_head(&udc
->tx_waitq
);
574 spin_lock_init(&ud
->lock
);
575 ud
->status
= SDEV_ST_AVAILABLE
;
576 ud
->side
= USBIP_VUDC
;
578 ud
->eh_ops
.shutdown
= vudc_shutdown
;
579 ud
->eh_ops
.reset
= vudc_device_reset
;
580 ud
->eh_ops
.unusable
= vudc_device_unusable
;
589 static void cleanup_vudc_hw(struct vudc
*udc
)
594 /* platform driver ops */
596 int vudc_probe(struct platform_device
*pdev
)
601 udc
= kzalloc(sizeof(*udc
), GFP_KERNEL
);
605 udc
->gadget
.name
= GADGET_NAME
;
606 udc
->gadget
.ops
= &vgadget_ops
;
607 udc
->gadget
.max_speed
= USB_SPEED_HIGH
;
608 udc
->gadget
.dev
.parent
= &pdev
->dev
;
611 ret
= init_vudc_hw(udc
);
613 goto err_init_vudc_hw
;
615 ret
= usb_add_gadget_udc(&pdev
->dev
, &udc
->gadget
);
619 platform_set_drvdata(pdev
, udc
);
624 cleanup_vudc_hw(udc
);
631 int vudc_remove(struct platform_device
*pdev
)
633 struct vudc
*udc
= platform_get_drvdata(pdev
);
635 usb_del_gadget_udc(&udc
->gadget
);
636 cleanup_vudc_hw(udc
);