2 * Copyright (C) 2003-2008 Takahiro Hirofuchi
3 * Copyright (C) 2015-2016 Nobuo Iwata
5 * This is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 #include <linux/kthread.h>
22 #include <linux/file.h>
23 #include <linux/net.h>
24 #include <linux/platform_device.h>
25 #include <linux/slab.h>
27 #include "usbip_common.h"
30 /* TODO: refine locking ?*/
34 * hub port sta spd dev socket local_busid
35 * hs 0000 004 000 00000000 c5a7bb80 1-2.3
36 * ................................................
37 * ss 0008 004 000 00000000 d8cee980 2-3.4
38 * ................................................
40 * IP address can be retrieved from a socket pointer address by looking
41 * up /proc/net/{tcp,tcp6}. Also, a userland program may remember a
42 * port number and its peer IP address.
44 static void port_show_vhci(char **out, int hub, int port, struct vhci_device *vdev)
46 if (hub == HUB_SPEED_HIGH)
47 *out += sprintf(*out, "hs %04u %03u ",
48 port, vdev->ud.status);
49 else /* hub == HUB_SPEED_SUPER */
50 *out += sprintf(*out, "ss %04u %03u ",
51 port, vdev->ud.status);
53 if (vdev->ud.status == VDEV_ST_USED) {
54 *out += sprintf(*out, "%03u %08x ",
55 vdev->speed, vdev->devid);
56 *out += sprintf(*out, "%16p %s",
58 dev_name(&vdev->udev->dev));
61 *out += sprintf(*out, "000 00000000 ");
62 *out += sprintf(*out, "0000000000000000 0-0");
65 *out += sprintf(*out, "\n");
68 /* Sysfs entry to show port status */
69 static ssize_t status_show_vhci(int pdev_nr, char *out)
71 struct platform_device *pdev = vhcis[pdev_nr].pdev;
74 struct vhci_hcd *vhci_hcd;
80 usbip_dbg_vhci_sysfs("show status error\n");
84 hcd = platform_get_drvdata(pdev);
85 vhci_hcd = hcd_to_vhci_hcd(hcd);
86 vhci = vhci_hcd->vhci;
88 spin_lock_irqsave(&vhci->lock, flags);
90 for (i = 0; i < VHCI_HC_PORTS; i++) {
91 struct vhci_device *vdev = &vhci->vhci_hcd_hs->vdev[i];
93 spin_lock(&vdev->ud.lock);
94 port_show_vhci(&out, HUB_SPEED_HIGH,
95 pdev_nr * VHCI_PORTS + i, vdev);
96 spin_unlock(&vdev->ud.lock);
99 for (i = 0; i < VHCI_HC_PORTS; i++) {
100 struct vhci_device *vdev = &vhci->vhci_hcd_ss->vdev[i];
102 spin_lock(&vdev->ud.lock);
103 port_show_vhci(&out, HUB_SPEED_SUPER,
104 pdev_nr * VHCI_PORTS + VHCI_HC_PORTS + i, vdev);
105 spin_unlock(&vdev->ud.lock);
108 spin_unlock_irqrestore(&vhci->lock, flags);
113 static ssize_t status_show_not_ready(int pdev_nr, char *out)
118 for (i = 0; i < VHCI_HC_PORTS; i++) {
119 out += sprintf(out, "hs %04u %03u ",
120 (pdev_nr * VHCI_PORTS) + i,
121 VDEV_ST_NOTASSIGNED);
122 out += sprintf(out, "000 00000000 0000000000000000 0-0");
123 out += sprintf(out, "\n");
126 for (i = 0; i < VHCI_HC_PORTS; i++) {
127 out += sprintf(out, "ss %04u %03u ",
128 (pdev_nr * VHCI_PORTS) + VHCI_HC_PORTS + i,
129 VDEV_ST_NOTASSIGNED);
130 out += sprintf(out, "000 00000000 0000000000000000 0-0");
131 out += sprintf(out, "\n");
136 static int status_name_to_id(const char *name)
142 c = strchr(name, '.');
146 ret = kstrtol(c+1, 10, &val);
153 static ssize_t status_show(struct device *dev,
154 struct device_attribute *attr, char *out)
160 "hub port sta spd dev socket local_busid\n");
162 pdev_nr = status_name_to_id(attr->attr.name);
164 out += status_show_not_ready(pdev_nr, out);
166 out += status_show_vhci(pdev_nr, out);
171 static ssize_t nports_show(struct device *dev, struct device_attribute *attr,
177 * Half the ports are for SPEED_HIGH and half for SPEED_SUPER, thus the * 2.
179 out += sprintf(out, "%d\n", VHCI_PORTS * vhci_num_controllers);
182 static DEVICE_ATTR_RO(nports);
184 /* Sysfs entry to shutdown a virtual connection */
185 static int vhci_port_disconnect(struct vhci_hcd *vhci_hcd, __u32 rhport)
187 struct vhci_device *vdev = &vhci_hcd->vdev[rhport];
188 struct vhci *vhci = vhci_hcd->vhci;
191 usbip_dbg_vhci_sysfs("enter\n");
194 spin_lock_irqsave(&vhci->lock, flags);
195 spin_lock(&vdev->ud.lock);
197 if (vdev->ud.status == VDEV_ST_NULL) {
198 pr_err("not connected %d\n", vdev->ud.status);
201 spin_unlock(&vdev->ud.lock);
202 spin_unlock_irqrestore(&vhci->lock, flags);
208 spin_unlock(&vdev->ud.lock);
209 spin_unlock_irqrestore(&vhci->lock, flags);
211 usbip_event_add(&vdev->ud, VDEV_EVENT_DOWN);
216 static int valid_port(__u32 pdev_nr, __u32 rhport)
218 if (pdev_nr >= vhci_num_controllers) {
219 pr_err("pdev %u\n", pdev_nr);
222 if (rhport >= VHCI_HC_PORTS) {
223 pr_err("rhport %u\n", rhport);
229 static ssize_t store_detach(struct device *dev, struct device_attribute *attr,
230 const char *buf, size_t count)
232 __u32 port = 0, pdev_nr = 0, rhport = 0;
234 struct vhci_hcd *vhci_hcd;
237 if (kstrtoint(buf, 10, &port) < 0)
240 pdev_nr = port_to_pdev_nr(port);
241 rhport = port_to_rhport(port);
243 if (!valid_port(pdev_nr, rhport))
246 hcd = platform_get_drvdata(vhcis[pdev_nr].pdev);
248 dev_err(dev, "port is not ready %u\n", port);
252 usbip_dbg_vhci_sysfs("rhport %d\n", rhport);
254 if ((port / VHCI_HC_PORTS) % 2)
255 vhci_hcd = hcd_to_vhci_hcd(hcd)->vhci->vhci_hcd_ss;
257 vhci_hcd = hcd_to_vhci_hcd(hcd)->vhci->vhci_hcd_hs;
259 ret = vhci_port_disconnect(vhci_hcd, rhport);
263 usbip_dbg_vhci_sysfs("Leave\n");
267 static DEVICE_ATTR(detach, S_IWUSR, NULL, store_detach);
269 static int valid_args(__u32 pdev_nr, __u32 rhport, enum usb_device_speed speed)
271 if (!valid_port(pdev_nr, rhport)) {
279 case USB_SPEED_WIRELESS:
280 case USB_SPEED_SUPER:
283 pr_err("Failed attach request for unsupported USB speed: %s\n",
284 usb_speed_string(speed));
291 /* Sysfs entry to establish a virtual connection */
293 * To start a new USB/IP attachment, a userland program needs to setup a TCP
294 * connection and then write its socket descriptor with remote device
295 * information into this sysfs file.
297 * A remote device is virtually attached to the root-hub port of @rhport with
298 * @speed. @devid is embedded into a request to specify the remote device in a
301 * write() returns 0 on success, else negative errno.
303 static ssize_t store_attach(struct device *dev, struct device_attribute *attr,
304 const char *buf, size_t count)
306 struct socket *socket;
308 __u32 port = 0, pdev_nr = 0, rhport = 0, devid = 0, speed = 0;
310 struct vhci_hcd *vhci_hcd;
311 struct vhci_device *vdev;
317 * @rhport: port number of vhci_hcd
318 * @sockfd: socket descriptor of an established TCP connection
319 * @devid: unique device identifier in a remote host
320 * @speed: usb device speed in a remote host
322 if (sscanf(buf, "%u %u %u %u", &port, &sockfd, &devid, &speed) != 4)
324 pdev_nr = port_to_pdev_nr(port);
325 rhport = port_to_rhport(port);
327 usbip_dbg_vhci_sysfs("port(%u) pdev(%d) rhport(%u)\n",
328 port, pdev_nr, rhport);
329 usbip_dbg_vhci_sysfs("sockfd(%u) devid(%u) speed(%u)\n",
330 sockfd, devid, speed);
332 /* check received parameters */
333 if (!valid_args(pdev_nr, rhport, speed))
336 hcd = platform_get_drvdata(vhcis[pdev_nr].pdev);
338 dev_err(dev, "port %d is not ready\n", port);
342 vhci_hcd = hcd_to_vhci_hcd(hcd);
343 vhci = vhci_hcd->vhci;
345 if (speed == USB_SPEED_SUPER)
346 vdev = &vhci->vhci_hcd_ss->vdev[rhport];
348 vdev = &vhci->vhci_hcd_hs->vdev[rhport];
350 /* Extract socket from fd. */
351 socket = sockfd_lookup(sockfd, &err);
355 /* now need lock until setting vdev status as used */
358 spin_lock_irqsave(&vhci->lock, flags);
359 spin_lock(&vdev->ud.lock);
361 if (vdev->ud.status != VDEV_ST_NULL) {
362 /* end of the lock */
363 spin_unlock(&vdev->ud.lock);
364 spin_unlock_irqrestore(&vhci->lock, flags);
368 dev_err(dev, "port %d already used\n", rhport);
372 dev_info(dev, "pdev(%u) rhport(%u) sockfd(%d)\n",
373 pdev_nr, rhport, sockfd);
374 dev_info(dev, "devid(%u) speed(%u) speed_str(%s)\n",
375 devid, speed, usb_speed_string(speed));
379 vdev->ud.tcp_socket = socket;
380 vdev->ud.status = VDEV_ST_NOTASSIGNED;
382 spin_unlock(&vdev->ud.lock);
383 spin_unlock_irqrestore(&vhci->lock, flags);
386 vdev->ud.tcp_rx = kthread_get_run(vhci_rx_loop, &vdev->ud, "vhci_rx");
387 vdev->ud.tcp_tx = kthread_get_run(vhci_tx_loop, &vdev->ud, "vhci_tx");
389 rh_port_connect(vdev, speed);
393 static DEVICE_ATTR(attach, S_IWUSR, NULL, store_attach);
395 #define MAX_STATUS_NAME 16
398 struct device_attribute attr;
399 char name[MAX_STATUS_NAME+1];
402 static struct status_attr *status_attrs;
404 static void set_status_attr(int id)
406 struct status_attr *status;
408 status = status_attrs + id;
410 strcpy(status->name, "status");
412 snprintf(status->name, MAX_STATUS_NAME+1, "status.%d", id);
413 status->attr.attr.name = status->name;
414 status->attr.attr.mode = S_IRUGO;
415 status->attr.show = status_show;
416 sysfs_attr_init(&status->attr.attr);
419 static int init_status_attrs(void)
423 status_attrs = kcalloc(vhci_num_controllers, sizeof(struct status_attr),
425 if (status_attrs == NULL)
428 for (id = 0; id < vhci_num_controllers; id++)
434 static void finish_status_attrs(void)
439 struct attribute_group vhci_attr_group = {
443 int vhci_init_attr_group(void)
445 struct attribute **attrs;
448 attrs = kcalloc((vhci_num_controllers + 5), sizeof(struct attribute *),
453 ret = init_status_attrs();
458 *attrs = &dev_attr_nports.attr;
459 *(attrs + 1) = &dev_attr_detach.attr;
460 *(attrs + 2) = &dev_attr_attach.attr;
461 *(attrs + 3) = &dev_attr_usbip_debug.attr;
462 for (i = 0; i < vhci_num_controllers; i++)
463 *(attrs + i + 4) = &((status_attrs + i)->attr.attr);
464 vhci_attr_group.attrs = attrs;
468 void vhci_finish_attr_group(void)
470 finish_status_attrs();
471 kfree(vhci_attr_group.attrs);