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