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/list.h>
11 #include <linux/usb/gadget.h>
12 #include <linux/usb/ch9.h>
13 #include <linux/sysfs.h>
14 #include <linux/kthread.h>
15 #include <linux/byteorder/generic.h>
17 #include "usbip_common.h"
22 /* called with udc->lock held */
23 int get_gadget_descs(struct vudc
*udc
)
25 struct vrequest
*usb_req
;
26 struct vep
*ep0
= to_vep(udc
->gadget
.ep0
);
27 struct usb_device_descriptor
*ddesc
= &udc
->dev_desc
;
28 struct usb_ctrlrequest req
;
31 if (!udc
->driver
|| !udc
->pullup
)
34 req
.bRequestType
= USB_DIR_IN
| USB_TYPE_STANDARD
| USB_RECIP_DEVICE
;
35 req
.bRequest
= USB_REQ_GET_DESCRIPTOR
;
36 req
.wValue
= cpu_to_le16(USB_DT_DEVICE
<< 8);
37 req
.wIndex
= cpu_to_le16(0);
38 req
.wLength
= cpu_to_le16(sizeof(*ddesc
));
40 spin_unlock(&udc
->lock
);
41 ret
= udc
->driver
->setup(&(udc
->gadget
), &req
);
42 spin_lock(&udc
->lock
);
46 /* assuming request queue is empty; request is now on top */
47 usb_req
= list_last_entry(&ep0
->req_queue
, struct vrequest
, req_entry
);
48 list_del(&usb_req
->req_entry
);
50 if (usb_req
->req
.length
> sizeof(*ddesc
)) {
55 memcpy(ddesc
, usb_req
->req
.buf
, sizeof(*ddesc
));
59 usb_req
->req
.status
= 0;
60 usb_req
->req
.actual
= usb_req
->req
.length
;
61 usb_gadget_giveback_request(&(ep0
->ep
), &(usb_req
->req
));
67 * Exposes device descriptor from the gadget driver.
69 static ssize_t
dev_desc_read(struct file
*file
, struct kobject
*kobj
,
70 struct bin_attribute
*attr
, char *out
,
71 loff_t off
, size_t count
)
73 struct device
*dev
= kobj_to_dev(kobj
);
74 struct vudc
*udc
= (struct vudc
*)dev_get_drvdata(dev
);
75 char *desc_ptr
= (char *) &udc
->dev_desc
;
79 spin_lock_irqsave(&udc
->lock
, flags
);
80 if (!udc
->desc_cached
) {
85 memcpy(out
, desc_ptr
+ off
, count
);
88 spin_unlock_irqrestore(&udc
->lock
, flags
);
91 static BIN_ATTR_RO(dev_desc
, sizeof(struct usb_device_descriptor
));
93 static ssize_t
usbip_sockfd_store(struct device
*dev
, struct device_attribute
*attr
,
94 const char *in
, size_t count
)
96 struct vudc
*udc
= (struct vudc
*) dev_get_drvdata(dev
);
100 struct socket
*socket
;
104 rv
= kstrtoint(in
, 0, &sockfd
);
108 spin_lock_irqsave(&udc
->lock
, flags
);
109 /* Don't export what we don't have */
110 if (!udc
|| !udc
->driver
|| !udc
->pullup
) {
111 dev_err(dev
, "no device or gadget not bound");
117 if (udc
->connected
) {
118 dev_err(dev
, "Device already connected");
123 spin_lock_irq(&udc
->ud
.lock
);
125 if (udc
->ud
.status
!= SDEV_ST_AVAILABLE
) {
130 socket
= sockfd_lookup(sockfd
, &err
);
132 dev_err(dev
, "failed to lookup sock");
137 udc
->ud
.tcp_socket
= socket
;
139 spin_unlock_irq(&udc
->ud
.lock
);
140 spin_unlock_irqrestore(&udc
->lock
, flags
);
142 udc
->ud
.tcp_rx
= kthread_get_run(&v_rx_loop
,
143 &udc
->ud
, "vudc_rx");
144 udc
->ud
.tcp_tx
= kthread_get_run(&v_tx_loop
,
145 &udc
->ud
, "vudc_tx");
147 spin_lock_irqsave(&udc
->lock
, flags
);
148 spin_lock_irq(&udc
->ud
.lock
);
149 udc
->ud
.status
= SDEV_ST_USED
;
150 spin_unlock_irq(&udc
->ud
.lock
);
152 ktime_get_ts64(&udc
->start_time
);
156 if (!udc
->connected
) {
157 dev_err(dev
, "Device not connected");
162 spin_lock_irq(&udc
->ud
.lock
);
163 if (udc
->ud
.status
!= SDEV_ST_USED
) {
167 spin_unlock_irq(&udc
->ud
.lock
);
169 usbip_event_add(&udc
->ud
, VUDC_EVENT_DOWN
);
172 spin_unlock_irqrestore(&udc
->lock
, flags
);
177 spin_unlock_irq(&udc
->ud
.lock
);
179 spin_unlock_irqrestore(&udc
->lock
, flags
);
183 static DEVICE_ATTR_WO(usbip_sockfd
);
185 static ssize_t
usbip_status_show(struct device
*dev
,
186 struct device_attribute
*attr
, char *out
)
188 struct vudc
*udc
= (struct vudc
*) dev_get_drvdata(dev
);
192 dev_err(dev
, "no device");
195 spin_lock_irq(&udc
->ud
.lock
);
196 status
= udc
->ud
.status
;
197 spin_unlock_irq(&udc
->ud
.lock
);
199 return snprintf(out
, PAGE_SIZE
, "%d\n", status
);
201 static DEVICE_ATTR_RO(usbip_status
);
203 static struct attribute
*dev_attrs
[] = {
204 &dev_attr_usbip_sockfd
.attr
,
205 &dev_attr_usbip_status
.attr
,
209 static struct bin_attribute
*dev_bin_attrs
[] = {
214 const struct attribute_group vudc_attr_group
= {
216 .bin_attrs
= dev_bin_attrs
,