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/file.h>
16 #include <linux/byteorder/generic.h>
18 #include "usbip_common.h"
23 /* called with udc->lock held */
24 int get_gadget_descs(struct vudc *udc)
26 struct vrequest *usb_req;
27 struct vep *ep0 = to_vep(udc->gadget.ep0);
28 struct usb_device_descriptor *ddesc = &udc->dev_desc;
29 struct usb_ctrlrequest req;
32 if (!udc->driver || !udc->pullup)
35 req.bRequestType = USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
36 req.bRequest = USB_REQ_GET_DESCRIPTOR;
37 req.wValue = cpu_to_le16(USB_DT_DEVICE << 8);
38 req.wIndex = cpu_to_le16(0);
39 req.wLength = cpu_to_le16(sizeof(*ddesc));
41 spin_unlock(&udc->lock);
42 ret = udc->driver->setup(&(udc->gadget), &req);
43 spin_lock(&udc->lock);
47 /* assuming request queue is empty; request is now on top */
48 usb_req = list_last_entry(&ep0->req_queue, struct vrequest, req_entry);
49 list_del(&usb_req->req_entry);
51 if (usb_req->req.length > sizeof(*ddesc)) {
56 memcpy(ddesc, usb_req->req.buf, sizeof(*ddesc));
60 usb_req->req.status = 0;
61 usb_req->req.actual = usb_req->req.length;
62 usb_gadget_giveback_request(&(ep0->ep), &(usb_req->req));
68 * Exposes device descriptor from the gadget driver.
70 static ssize_t dev_desc_read(struct file *file, struct kobject *kobj,
71 struct bin_attribute *attr, char *out,
72 loff_t off, size_t count)
74 struct device *dev = kobj_to_dev(kobj);
75 struct vudc *udc = (struct vudc *)dev_get_drvdata(dev);
76 char *desc_ptr = (char *) &udc->dev_desc;
80 spin_lock_irqsave(&udc->lock, flags);
81 if (!udc->desc_cached) {
86 memcpy(out, desc_ptr + off, count);
89 spin_unlock_irqrestore(&udc->lock, flags);
92 static BIN_ATTR_RO(dev_desc, sizeof(struct usb_device_descriptor));
94 static ssize_t usbip_sockfd_store(struct device *dev,
95 struct device_attribute *attr,
96 const char *in, size_t count)
98 struct vudc *udc = (struct vudc *) dev_get_drvdata(dev);
102 struct socket *socket;
105 struct task_struct *tcp_rx = NULL;
106 struct task_struct *tcp_tx = NULL;
108 rv = kstrtoint(in, 0, &sockfd);
113 dev_err(dev, "no device");
116 mutex_lock(&udc->ud.sysfs_lock);
117 spin_lock_irqsave(&udc->lock, flags);
118 /* Don't export what we don't have */
119 if (!udc->driver || !udc->pullup) {
120 dev_err(dev, "gadget not bound");
126 if (udc->connected) {
127 dev_err(dev, "Device already connected");
132 spin_lock_irq(&udc->ud.lock);
134 if (udc->ud.status != SDEV_ST_AVAILABLE) {
139 socket = sockfd_lookup(sockfd, &err);
141 dev_err(dev, "failed to lookup sock");
146 if (socket->type != SOCK_STREAM) {
147 dev_err(dev, "Expecting SOCK_STREAM - found %d",
153 /* unlock and create threads and get tasks */
154 spin_unlock_irq(&udc->ud.lock);
155 spin_unlock_irqrestore(&udc->lock, flags);
157 tcp_rx = kthread_create(&v_rx_loop, &udc->ud, "vudc_rx");
158 if (IS_ERR(tcp_rx)) {
160 mutex_unlock(&udc->ud.sysfs_lock);
163 tcp_tx = kthread_create(&v_tx_loop, &udc->ud, "vudc_tx");
164 if (IS_ERR(tcp_tx)) {
165 kthread_stop(tcp_rx);
167 mutex_unlock(&udc->ud.sysfs_lock);
171 /* get task structs now */
172 get_task_struct(tcp_rx);
173 get_task_struct(tcp_tx);
175 /* lock and update udc->ud state */
176 spin_lock_irqsave(&udc->lock, flags);
177 spin_lock_irq(&udc->ud.lock);
179 udc->ud.tcp_socket = socket;
180 udc->ud.tcp_rx = tcp_rx;
181 udc->ud.tcp_tx = tcp_tx;
182 udc->ud.status = SDEV_ST_USED;
184 spin_unlock_irq(&udc->ud.lock);
186 ktime_get_ts64(&udc->start_time);
190 spin_unlock_irqrestore(&udc->lock, flags);
192 wake_up_process(udc->ud.tcp_rx);
193 wake_up_process(udc->ud.tcp_tx);
195 mutex_unlock(&udc->ud.sysfs_lock);
199 if (!udc->connected) {
200 dev_err(dev, "Device not connected");
205 spin_lock_irq(&udc->ud.lock);
206 if (udc->ud.status != SDEV_ST_USED) {
210 spin_unlock_irq(&udc->ud.lock);
212 usbip_event_add(&udc->ud, VUDC_EVENT_DOWN);
215 spin_unlock_irqrestore(&udc->lock, flags);
216 mutex_unlock(&udc->ud.sysfs_lock);
223 spin_unlock_irq(&udc->ud.lock);
225 spin_unlock_irqrestore(&udc->lock, flags);
226 mutex_unlock(&udc->ud.sysfs_lock);
230 static DEVICE_ATTR_WO(usbip_sockfd);
232 static ssize_t usbip_status_show(struct device *dev,
233 struct device_attribute *attr, char *out)
235 struct vudc *udc = (struct vudc *) dev_get_drvdata(dev);
239 dev_err(dev, "no device");
242 spin_lock_irq(&udc->ud.lock);
243 status = udc->ud.status;
244 spin_unlock_irq(&udc->ud.lock);
246 return snprintf(out, PAGE_SIZE, "%d\n", status);
248 static DEVICE_ATTR_RO(usbip_status);
250 static struct attribute *dev_attrs[] = {
251 &dev_attr_usbip_sockfd.attr,
252 &dev_attr_usbip_status.attr,
256 static struct bin_attribute *dev_bin_attrs[] = {
261 const struct attribute_group vudc_attr_group = {
263 .bin_attrs = dev_bin_attrs,