• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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>
6  *
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.
11  *
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.
16  *
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/>.
19  */
20 
21 #include <linux/device.h>
22 #include <linux/list.h>
23 #include <linux/usb/gadget.h>
24 #include <linux/usb/ch9.h>
25 #include <linux/sysfs.h>
26 #include <linux/kthread.h>
27 #include <linux/byteorder/generic.h>
28 
29 #include "usbip_common.h"
30 #include "vudc.h"
31 
32 #include <net/sock.h>
33 
34 /* called with udc->lock held */
get_gadget_descs(struct vudc * udc)35 int get_gadget_descs(struct vudc *udc)
36 {
37 	struct vrequest *usb_req;
38 	struct vep *ep0 = to_vep(udc->gadget.ep0);
39 	struct usb_device_descriptor *ddesc = &udc->dev_desc;
40 	struct usb_ctrlrequest req;
41 	int ret;
42 
43 	if (!udc->driver || !udc->pullup)
44 		return -EINVAL;
45 
46 	req.bRequestType = USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
47 	req.bRequest = USB_REQ_GET_DESCRIPTOR;
48 	req.wValue = cpu_to_le16(USB_DT_DEVICE << 8);
49 	req.wIndex = cpu_to_le16(0);
50 	req.wLength = cpu_to_le16(sizeof(*ddesc));
51 
52 	spin_unlock(&udc->lock);
53 	ret = udc->driver->setup(&(udc->gadget), &req);
54 	spin_lock(&udc->lock);
55 	if (ret < 0)
56 		goto out;
57 
58 	/* assuming request queue is empty; request is now on top */
59 	usb_req = list_last_entry(&ep0->req_queue, struct vrequest, req_entry);
60 	list_del(&usb_req->req_entry);
61 
62 	if (usb_req->req.length > sizeof(*ddesc)) {
63 		ret = -EOVERFLOW;
64 		goto giveback_req;
65 	}
66 
67 	memcpy(ddesc, usb_req->req.buf, sizeof(*ddesc));
68 	udc->desc_cached = 1;
69 	ret = 0;
70 giveback_req:
71 	usb_req->req.status = 0;
72 	usb_req->req.actual = usb_req->req.length;
73 	usb_gadget_giveback_request(&(ep0->ep), &(usb_req->req));
74 out:
75 	return ret;
76 }
77 
78 /*
79  * Exposes device descriptor from the gadget driver.
80  */
dev_desc_read(struct file * file,struct kobject * kobj,struct bin_attribute * attr,char * out,loff_t off,size_t count)81 static ssize_t dev_desc_read(struct file *file, struct kobject *kobj,
82 			     struct bin_attribute *attr, char *out,
83 			     loff_t off, size_t count)
84 {
85 	struct device *dev = kobj_to_dev(kobj);
86 	struct vudc *udc = (struct vudc *)dev_get_drvdata(dev);
87 	char *desc_ptr = (char *) &udc->dev_desc;
88 	unsigned long flags;
89 	int ret;
90 
91 	spin_lock_irqsave(&udc->lock, flags);
92 	if (!udc->desc_cached) {
93 		ret = -ENODEV;
94 		goto unlock;
95 	}
96 
97 	memcpy(out, desc_ptr + off, count);
98 	ret = count;
99 unlock:
100 	spin_unlock_irqrestore(&udc->lock, flags);
101 	return ret;
102 }
103 static BIN_ATTR_RO(dev_desc, sizeof(struct usb_device_descriptor));
104 
store_sockfd(struct device * dev,struct device_attribute * attr,const char * in,size_t count)105 static ssize_t store_sockfd(struct device *dev, struct device_attribute *attr,
106 		     const char *in, size_t count)
107 {
108 	struct vudc *udc = (struct vudc *) dev_get_drvdata(dev);
109 	int rv;
110 	int sockfd = 0;
111 	int err;
112 	struct socket *socket;
113 	unsigned long flags;
114 	int ret;
115 
116 	rv = kstrtoint(in, 0, &sockfd);
117 	if (rv != 0)
118 		return -EINVAL;
119 
120 	if (!udc) {
121 		dev_err(dev, "no device");
122 		return -ENODEV;
123 	}
124 	spin_lock_irqsave(&udc->lock, flags);
125 	/* Don't export what we don't have */
126 	if (!udc->driver || !udc->pullup) {
127 		dev_err(dev, "gadget not bound");
128 		ret = -ENODEV;
129 		goto unlock;
130 	}
131 
132 	if (sockfd != -1) {
133 		if (udc->connected) {
134 			dev_err(dev, "Device already connected");
135 			ret = -EBUSY;
136 			goto unlock;
137 		}
138 
139 		spin_lock_irq(&udc->ud.lock);
140 
141 		if (udc->ud.status != SDEV_ST_AVAILABLE) {
142 			ret = -EINVAL;
143 			goto unlock_ud;
144 		}
145 
146 		socket = sockfd_lookup(sockfd, &err);
147 		if (!socket) {
148 			dev_err(dev, "failed to lookup sock");
149 			ret = -EINVAL;
150 			goto unlock_ud;
151 		}
152 
153 		udc->ud.tcp_socket = socket;
154 
155 		spin_unlock_irq(&udc->ud.lock);
156 		spin_unlock_irqrestore(&udc->lock, flags);
157 
158 		udc->ud.tcp_rx = kthread_get_run(&v_rx_loop,
159 						    &udc->ud, "vudc_rx");
160 		udc->ud.tcp_tx = kthread_get_run(&v_tx_loop,
161 						    &udc->ud, "vudc_tx");
162 
163 		spin_lock_irqsave(&udc->lock, flags);
164 		spin_lock_irq(&udc->ud.lock);
165 		udc->ud.status = SDEV_ST_USED;
166 		spin_unlock_irq(&udc->ud.lock);
167 
168 		do_gettimeofday(&udc->start_time);
169 		v_start_timer(udc);
170 		udc->connected = 1;
171 	} else {
172 		if (!udc->connected) {
173 			dev_err(dev, "Device not connected");
174 			ret = -EINVAL;
175 			goto unlock;
176 		}
177 
178 		spin_lock_irq(&udc->ud.lock);
179 		if (udc->ud.status != SDEV_ST_USED) {
180 			ret = -EINVAL;
181 			goto unlock_ud;
182 		}
183 		spin_unlock_irq(&udc->ud.lock);
184 
185 		usbip_event_add(&udc->ud, VUDC_EVENT_DOWN);
186 	}
187 
188 	spin_unlock_irqrestore(&udc->lock, flags);
189 
190 	return count;
191 
192 unlock_ud:
193 	spin_unlock_irq(&udc->ud.lock);
194 unlock:
195 	spin_unlock_irqrestore(&udc->lock, flags);
196 
197 	return ret;
198 }
199 static DEVICE_ATTR(usbip_sockfd, S_IWUSR, NULL, store_sockfd);
200 
usbip_status_show(struct device * dev,struct device_attribute * attr,char * out)201 static ssize_t usbip_status_show(struct device *dev,
202 			       struct device_attribute *attr, char *out)
203 {
204 	struct vudc *udc = (struct vudc *) dev_get_drvdata(dev);
205 	int status;
206 
207 	if (!udc) {
208 		dev_err(dev, "no device");
209 		return -ENODEV;
210 	}
211 	spin_lock_irq(&udc->ud.lock);
212 	status = udc->ud.status;
213 	spin_unlock_irq(&udc->ud.lock);
214 
215 	return snprintf(out, PAGE_SIZE, "%d\n", status);
216 }
217 static DEVICE_ATTR_RO(usbip_status);
218 
219 static struct attribute *dev_attrs[] = {
220 	&dev_attr_usbip_sockfd.attr,
221 	&dev_attr_usbip_status.attr,
222 	NULL,
223 };
224 
225 static struct bin_attribute *dev_bin_attrs[] = {
226 	&bin_attr_dev_desc,
227 	NULL,
228 };
229 
230 const struct attribute_group vudc_attr_group = {
231 	.attrs = dev_attrs,
232 	.bin_attrs = dev_bin_attrs,
233 };
234