GNU Linux-libre 4.19.286-gnu1
[releases.git] / drivers / usb / usbip / vudc_sysfs.c
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  */
8
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>
17
18 #include "usbip_common.h"
19 #include "vudc.h"
20
21 #include <net/sock.h>
22
23 /* called with udc->lock held */
24 int get_gadget_descs(struct vudc *udc)
25 {
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;
30         int ret;
31
32         if (!udc->driver || !udc->pullup)
33                 return -EINVAL;
34
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));
40
41         spin_unlock(&udc->lock);
42         ret = udc->driver->setup(&(udc->gadget), &req);
43         spin_lock(&udc->lock);
44         if (ret < 0)
45                 goto out;
46
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);
50
51         if (usb_req->req.length > sizeof(*ddesc)) {
52                 ret = -EOVERFLOW;
53                 goto giveback_req;
54         }
55
56         memcpy(ddesc, usb_req->req.buf, sizeof(*ddesc));
57         udc->desc_cached = 1;
58         ret = 0;
59 giveback_req:
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));
63 out:
64         return ret;
65 }
66
67 /*
68  * Exposes device descriptor from the gadget driver.
69  */
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)
73 {
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;
77         unsigned long flags;
78         int ret;
79
80         spin_lock_irqsave(&udc->lock, flags);
81         if (!udc->desc_cached) {
82                 ret = -ENODEV;
83                 goto unlock;
84         }
85
86         memcpy(out, desc_ptr + off, count);
87         ret = count;
88 unlock:
89         spin_unlock_irqrestore(&udc->lock, flags);
90         return ret;
91 }
92 static BIN_ATTR_RO(dev_desc, sizeof(struct usb_device_descriptor));
93
94 static ssize_t usbip_sockfd_store(struct device *dev,
95                                   struct device_attribute *attr,
96                                   const char *in, size_t count)
97 {
98         struct vudc *udc = (struct vudc *) dev_get_drvdata(dev);
99         int rv;
100         int sockfd = 0;
101         int err;
102         struct socket *socket;
103         unsigned long flags;
104         int ret;
105         struct task_struct *tcp_rx = NULL;
106         struct task_struct *tcp_tx = NULL;
107
108         rv = kstrtoint(in, 0, &sockfd);
109         if (rv != 0)
110                 return -EINVAL;
111
112         if (!udc) {
113                 dev_err(dev, "no device");
114                 return -ENODEV;
115         }
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");
121                 ret = -ENODEV;
122                 goto unlock;
123         }
124
125         if (sockfd != -1) {
126                 if (udc->connected) {
127                         dev_err(dev, "Device already connected");
128                         ret = -EBUSY;
129                         goto unlock;
130                 }
131
132                 spin_lock_irq(&udc->ud.lock);
133
134                 if (udc->ud.status != SDEV_ST_AVAILABLE) {
135                         ret = -EINVAL;
136                         goto unlock_ud;
137                 }
138
139                 socket = sockfd_lookup(sockfd, &err);
140                 if (!socket) {
141                         dev_err(dev, "failed to lookup sock");
142                         ret = -EINVAL;
143                         goto unlock_ud;
144                 }
145
146                 if (socket->type != SOCK_STREAM) {
147                         dev_err(dev, "Expecting SOCK_STREAM - found %d",
148                                 socket->type);
149                         ret = -EINVAL;
150                         goto sock_err;
151                 }
152
153                 /* unlock and create threads and get tasks */
154                 spin_unlock_irq(&udc->ud.lock);
155                 spin_unlock_irqrestore(&udc->lock, flags);
156
157                 tcp_rx = kthread_create(&v_rx_loop, &udc->ud, "vudc_rx");
158                 if (IS_ERR(tcp_rx)) {
159                         sockfd_put(socket);
160                         mutex_unlock(&udc->ud.sysfs_lock);
161                         return -EINVAL;
162                 }
163                 tcp_tx = kthread_create(&v_tx_loop, &udc->ud, "vudc_tx");
164                 if (IS_ERR(tcp_tx)) {
165                         kthread_stop(tcp_rx);
166                         sockfd_put(socket);
167                         mutex_unlock(&udc->ud.sysfs_lock);
168                         return -EINVAL;
169                 }
170
171                 /* get task structs now */
172                 get_task_struct(tcp_rx);
173                 get_task_struct(tcp_tx);
174
175                 /* lock and update udc->ud state */
176                 spin_lock_irqsave(&udc->lock, flags);
177                 spin_lock_irq(&udc->ud.lock);
178
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;
183
184                 spin_unlock_irq(&udc->ud.lock);
185
186                 ktime_get_ts64(&udc->start_time);
187                 v_start_timer(udc);
188                 udc->connected = 1;
189
190                 spin_unlock_irqrestore(&udc->lock, flags);
191
192                 wake_up_process(udc->ud.tcp_rx);
193                 wake_up_process(udc->ud.tcp_tx);
194
195                 mutex_unlock(&udc->ud.sysfs_lock);
196                 return count;
197
198         } else {
199                 if (!udc->connected) {
200                         dev_err(dev, "Device not connected");
201                         ret = -EINVAL;
202                         goto unlock;
203                 }
204
205                 spin_lock_irq(&udc->ud.lock);
206                 if (udc->ud.status != SDEV_ST_USED) {
207                         ret = -EINVAL;
208                         goto unlock_ud;
209                 }
210                 spin_unlock_irq(&udc->ud.lock);
211
212                 usbip_event_add(&udc->ud, VUDC_EVENT_DOWN);
213         }
214
215         spin_unlock_irqrestore(&udc->lock, flags);
216         mutex_unlock(&udc->ud.sysfs_lock);
217
218         return count;
219
220 sock_err:
221         sockfd_put(socket);
222 unlock_ud:
223         spin_unlock_irq(&udc->ud.lock);
224 unlock:
225         spin_unlock_irqrestore(&udc->lock, flags);
226         mutex_unlock(&udc->ud.sysfs_lock);
227
228         return ret;
229 }
230 static DEVICE_ATTR_WO(usbip_sockfd);
231
232 static ssize_t usbip_status_show(struct device *dev,
233                                struct device_attribute *attr, char *out)
234 {
235         struct vudc *udc = (struct vudc *) dev_get_drvdata(dev);
236         int status;
237
238         if (!udc) {
239                 dev_err(dev, "no device");
240                 return -ENODEV;
241         }
242         spin_lock_irq(&udc->ud.lock);
243         status = udc->ud.status;
244         spin_unlock_irq(&udc->ud.lock);
245
246         return snprintf(out, PAGE_SIZE, "%d\n", status);
247 }
248 static DEVICE_ATTR_RO(usbip_status);
249
250 static struct attribute *dev_attrs[] = {
251         &dev_attr_usbip_sockfd.attr,
252         &dev_attr_usbip_status.attr,
253         NULL,
254 };
255
256 static struct bin_attribute *dev_bin_attrs[] = {
257         &bin_attr_dev_desc,
258         NULL,
259 };
260
261 const struct attribute_group vudc_attr_group = {
262         .attrs = dev_attrs,
263         .bin_attrs = dev_bin_attrs,
264 };