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>
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.
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.
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/>.
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>
32 #include "usbip_common.h"
35 #define VIRTUAL_ENDPOINTS (1 /* ep0 */ + 15 /* in eps */ + 15 /* out eps */)
37 /* urb-related structures alloc / free */
40 static void free_urb(struct urb
*urb
)
45 kfree(urb
->setup_packet
);
46 urb
->setup_packet
= NULL
;
48 kfree(urb
->transfer_buffer
);
49 urb
->transfer_buffer
= NULL
;
54 struct urbp
*alloc_urbp(void)
58 urb_p
= kzalloc(sizeof(*urb_p
), GFP_KERNEL
);
64 INIT_LIST_HEAD(&urb_p
->urb_entry
);
68 static void free_urbp(struct urbp
*urb_p
)
73 void free_urbp_and_urb(struct urbp
*urb_p
)
82 /* utilities ; almost verbatim from dummy_hcd.c */
84 /* called with spinlock held */
85 static void nuke(struct vudc
*udc
, struct vep
*ep
)
89 while (!list_empty(&ep
->req_queue
)) {
90 req
= list_first_entry(&ep
->req_queue
, struct vrequest
,
92 list_del_init(&req
->req_entry
);
93 req
->req
.status
= -ESHUTDOWN
;
95 spin_unlock(&udc
->lock
);
96 usb_gadget_giveback_request(&ep
->ep
, &req
->req
);
97 spin_lock(&udc
->lock
);
101 /* caller must hold lock */
102 static void stop_activity(struct vudc
*udc
)
105 struct urbp
*urb_p
, *tmp
;
109 for (i
= 0; i
< VIRTUAL_ENDPOINTS
; i
++)
110 nuke(udc
, &udc
->ep
[i
]);
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
);
118 struct vep
*vudc_find_endpoint(struct vudc
*udc
, u8 address
)
122 if ((address
& ~USB_DIR_IN
) == 0)
125 for (i
= 1; i
< VIRTUAL_ENDPOINTS
; i
++) {
126 struct vep
*ep
= &udc
->ep
[i
];
130 if (ep
->desc
->bEndpointAddress
== address
)
138 /* FIXME - this will probably misbehave when suspend/resume is added */
139 static int vgadget_get_frame(struct usb_gadget
*_gadget
)
142 struct vudc
*udc
= usb_gadget_to_vudc(_gadget
);
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)
150 static int vgadget_set_selfpowered(struct usb_gadget
*_gadget
, int value
)
152 struct vudc
*udc
= usb_gadget_to_vudc(_gadget
);
155 udc
->devstatus
|= (1 << USB_DEVICE_SELF_POWERED
);
157 udc
->devstatus
&= ~(1 << USB_DEVICE_SELF_POWERED
);
161 static int vgadget_pullup(struct usb_gadget
*_gadget
, int value
)
163 struct vudc
*udc
= usb_gadget_to_vudc(_gadget
);
168 spin_lock_irqsave(&udc
->lock
, flags
);
170 if (value
== udc
->pullup
)
175 udc
->gadget
.speed
= min_t(u8
, USB_SPEED_HIGH
,
176 udc
->driver
->max_speed
);
177 udc
->ep
[0].ep
.maxpacket
= 64;
179 * This is the first place where we can ask our
180 * gadget driver for descriptors.
182 ret
= get_gadget_descs(udc
);
184 dev_err(&udc
->gadget
.dev
, "Unable go get desc: %d", ret
);
188 spin_unlock_irqrestore(&udc
->lock
, flags
);
189 usbip_start_eh(&udc
->ud
);
191 /* Invalidate descriptors */
192 udc
->desc_cached
= 0;
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 */
202 spin_unlock_irqrestore(&udc
->lock
, flags
);
206 static int vgadget_udc_start(struct usb_gadget
*g
,
207 struct usb_gadget_driver
*driver
)
209 struct vudc
*udc
= usb_gadget_to_vudc(g
);
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
);
220 static int vgadget_udc_stop(struct usb_gadget
*g
)
222 struct vudc
*udc
= usb_gadget_to_vudc(g
);
225 spin_lock_irqsave(&udc
->lock
, flags
);
227 spin_unlock_irqrestore(&udc
->lock
, flags
);
231 static 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
,
242 static int vep_enable(struct usb_ep
*_ep
,
243 const struct usb_endpoint_descriptor
*desc
)
251 udc
= ep_to_vudc(ep
);
253 if (!_ep
|| !desc
|| ep
->desc
|| _ep
->caps
.type_control
254 || desc
->bDescriptorType
!= USB_DT_ENDPOINT
)
260 spin_lock_irqsave(&udc
->lock
, flags
);
262 maxp
= usb_endpoint_maxp(desc
) & 0x7ff;
263 _ep
->maxpacket
= maxp
;
265 ep
->type
= usb_endpoint_type(desc
);
266 ep
->halted
= ep
->wedged
= 0;
268 spin_unlock_irqrestore(&udc
->lock
, flags
);
273 static int vep_disable(struct usb_ep
*_ep
)
280 udc
= ep_to_vudc(ep
);
281 if (!_ep
|| !ep
->desc
|| _ep
->caps
.type_control
)
284 spin_lock_irqsave(&udc
->lock
, flags
);
287 spin_unlock_irqrestore(&udc
->lock
, flags
);
292 static struct usb_request
*vep_alloc_request(struct usb_ep
*_ep
,
296 struct vrequest
*req
;
302 req
= kzalloc(sizeof(*req
), mem_flags
);
306 INIT_LIST_HEAD(&req
->req_entry
);
311 static void vep_free_request(struct usb_ep
*_ep
, struct usb_request
*_req
)
313 struct vrequest
*req
;
315 if (WARN_ON(!_ep
|| !_req
))
318 req
= to_vrequest(_req
);
322 static int vep_queue(struct usb_ep
*_ep
, struct usb_request
*_req
,
326 struct vrequest
*req
;
334 req
= to_vrequest(_req
);
335 udc
= ep_to_vudc(ep
);
337 spin_lock_irqsave(&udc
->lock
, flags
);
339 _req
->status
= -EINPROGRESS
;
341 list_add_tail(&req
->req_entry
, &ep
->req_queue
);
342 spin_unlock_irqrestore(&udc
->lock
, flags
);
347 static int vep_dequeue(struct usb_ep
*_ep
, struct usb_request
*_req
)
350 struct vrequest
*req
;
352 struct vrequest
*lst
;
360 req
= to_vrequest(_req
);
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
;
375 spin_unlock_irqrestore(&udc
->lock
, flags
);
378 usb_gadget_giveback_request(_ep
, _req
);
384 vep_set_halt_and_wedge(struct usb_ep
*_ep
, int value
, int wedged
)
395 udc
= ep_to_vudc(ep
);
399 spin_lock_irqsave(&udc
->lock
, flags
);
401 ep
->halted
= ep
->wedged
= 0;
402 else if (ep
->desc
&& (ep
->desc
->bEndpointAddress
& USB_DIR_IN
) &&
403 !list_empty(&ep
->req_queue
))
411 spin_unlock_irqrestore(&udc
->lock
, flags
);
416 vep_set_halt(struct usb_ep
*_ep
, int value
)
418 return vep_set_halt_and_wedge(_ep
, value
, 0);
421 static int vep_set_wedge(struct usb_ep
*_ep
)
423 return vep_set_halt_and_wedge(_ep
, 1, 1);
426 static const struct usb_ep_ops vep_ops
= {
427 .enable
= vep_enable
,
428 .disable
= vep_disable
,
430 .alloc_request
= vep_alloc_request
,
431 .free_request
= vep_free_request
,
434 .dequeue
= vep_dequeue
,
436 .set_halt
= vep_set_halt
,
437 .set_wedge
= vep_set_wedge
,
441 /* shutdown / reset / error handlers */
443 static void vudc_shutdown(struct usbip_device
*ud
)
445 struct vudc
*udc
= container_of(ud
, struct vudc
, ud
);
446 int call_disconnect
= 0;
449 dev_dbg(&udc
->pdev
->dev
, "device shutdown");
451 kernel_sock_shutdown(ud
->tcp_socket
, SHUT_RDWR
);
454 kthread_stop_put(ud
->tcp_rx
);
458 kthread_stop_put(ud
->tcp_tx
);
462 if (ud
->tcp_socket
) {
463 sockfd_put(ud
->tcp_socket
);
464 ud
->tcp_socket
= NULL
;
467 spin_lock_irqsave(&udc
->lock
, flags
);
469 if (udc
->connected
&& udc
->driver
->disconnect
)
472 spin_unlock_irqrestore(&udc
->lock
, flags
);
474 udc
->driver
->disconnect(&udc
->gadget
);
477 static void vudc_device_reset(struct usbip_device
*ud
)
479 struct vudc
*udc
= container_of(ud
, struct vudc
, ud
);
482 dev_dbg(&udc
->pdev
->dev
, "device reset");
483 spin_lock_irqsave(&udc
->lock
, flags
);
485 spin_unlock_irqrestore(&udc
->lock
, flags
);
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
);
493 static void vudc_device_unusable(struct usbip_device
*ud
)
497 spin_lock_irqsave(&ud
->lock
, flags
);
498 ud
->status
= SDEV_ST_ERROR
;
499 spin_unlock_irqrestore(&ud
->lock
, flags
);
502 /* device setup / cleanup */
504 struct vudc_device
*alloc_vudc_device(int devid
)
506 struct vudc_device
*udc_dev
= NULL
;
508 udc_dev
= kzalloc(sizeof(*udc_dev
), GFP_KERNEL
);
512 INIT_LIST_HEAD(&udc_dev
->dev_entry
);
514 udc_dev
->pdev
= platform_device_alloc(GADGET_NAME
, devid
);
515 if (!udc_dev
->pdev
) {
524 void put_vudc_device(struct vudc_device
*udc_dev
)
526 platform_device_put(udc_dev
->pdev
);
530 static int init_vudc_hw(struct vudc
*udc
)
533 struct usbip_device
*ud
= &udc
->ud
;
536 udc
->ep
= kcalloc(VIRTUAL_ENDPOINTS
, sizeof(*udc
->ep
), GFP_KERNEL
);
540 INIT_LIST_HEAD(&udc
->gadget
.ep_list
);
542 /* create ep0 and 15 in, 15 out general purpose eps */
543 for (i
= 0; i
< VIRTUAL_ENDPOINTS
; ++i
) {
545 int num
= (i
+ 1) / 2;
549 sprintf(ep
->name
, "ep%d%s", num
,
550 i
? (is_out
? "out" : "in") : "");
551 ep
->ep
.name
= ep
->name
;
553 ep
->ep
.caps
.type_control
= true;
554 ep
->ep
.caps
.dir_out
= true;
555 ep
->ep
.caps
.dir_in
= true;
557 ep
->ep
.caps
.type_iso
= true;
558 ep
->ep
.caps
.type_int
= true;
559 ep
->ep
.caps
.type_bulk
= true;
563 ep
->ep
.caps
.dir_out
= true;
565 ep
->ep
.caps
.dir_in
= true;
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
=
571 usb_ep_set_maxpacket_limit(&ep
->ep
, ~0);
572 ep
->ep
.max_streams
= 16;
573 ep
->gadget
= &udc
->gadget
;
575 INIT_LIST_HEAD(&ep
->req_queue
);
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
);
584 spin_lock_init(&ud
->lock
);
585 ud
->status
= SDEV_ST_AVAILABLE
;
586 ud
->side
= USBIP_VUDC
;
588 ud
->eh_ops
.shutdown
= vudc_shutdown
;
589 ud
->eh_ops
.reset
= vudc_device_reset
;
590 ud
->eh_ops
.unusable
= vudc_device_unusable
;
592 udc
->gadget
.ep0
= &udc
->ep
[0].ep
;
593 list_del_init(&udc
->ep
[0].ep
.ep_list
);
602 static void cleanup_vudc_hw(struct vudc
*udc
)
607 /* platform driver ops */
609 int vudc_probe(struct platform_device
*pdev
)
614 udc
= kzalloc(sizeof(*udc
), GFP_KERNEL
);
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
;
624 ret
= init_vudc_hw(udc
);
626 goto err_init_vudc_hw
;
628 ret
= usb_add_gadget_udc(&pdev
->dev
, &udc
->gadget
);
632 ret
= sysfs_create_group(&pdev
->dev
.kobj
, &vudc_attr_group
);
634 dev_err(&udc
->pdev
->dev
, "create sysfs files\n");
638 platform_set_drvdata(pdev
, udc
);
643 usb_del_gadget_udc(&udc
->gadget
);
645 cleanup_vudc_hw(udc
);
652 int vudc_remove(struct platform_device
*pdev
)
654 struct vudc
*udc
= platform_get_drvdata(pdev
);
656 sysfs_remove_group(&pdev
->dev
.kobj
, &vudc_attr_group
);
657 usb_del_gadget_udc(&udc
->gadget
);
658 cleanup_vudc_hw(udc
);