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 ?*/
32 /* Sysfs entry to show port status */
33 static ssize_t
status_show_vhci(int pdev_nr
, char *out
)
35 struct platform_device
*pdev
= vhcis
[pdev_nr
].pdev
;
38 struct vhci_hcd
*vhci_hcd
;
44 usbip_dbg_vhci_sysfs("show status error\n");
48 hcd
= platform_get_drvdata(pdev
);
49 vhci_hcd
= hcd_to_vhci_hcd(hcd
);
50 vhci
= vhci_hcd
->vhci
;
52 spin_lock_irqsave(&vhci
->lock
, flags
);
56 * port sta spd dev socket local_busid
57 * 0000 004 000 00000000 c5a7bb80 1-2.3
58 * 0001 004 000 00000000 d8cee980 2-3.4
60 * IP address can be retrieved from a socket pointer address by looking
61 * up /proc/net/{tcp,tcp6}. Also, a userland program may remember a
62 * port number and its peer IP address.
64 for (i
= 0; i
< VHCI_HC_PORTS
; i
++) {
65 struct vhci_device
*vdev
= &vhci_hcd
->vdev
[i
];
67 spin_lock(&vdev
->ud
.lock
);
68 out
+= sprintf(out
, "%04u %03u ",
69 (pdev_nr
* VHCI_HC_PORTS
) + i
,
72 if (vdev
->ud
.status
== VDEV_ST_USED
) {
73 out
+= sprintf(out
, "%03u %08x ",
74 vdev
->speed
, vdev
->devid
);
75 out
+= sprintf(out
, "%16p %s",
77 dev_name(&vdev
->udev
->dev
));
80 out
+= sprintf(out
, "000 00000000 ");
81 out
+= sprintf(out
, "0000000000000000 0-0");
84 out
+= sprintf(out
, "\n");
85 spin_unlock(&vdev
->ud
.lock
);
88 spin_unlock_irqrestore(&vhci
->lock
, flags
);
93 static ssize_t
status_show_not_ready(int pdev_nr
, char *out
)
98 for (i
= 0; i
< VHCI_HC_PORTS
; i
++) {
99 out
+= sprintf(out
, "%04u %03u ",
100 (pdev_nr
* VHCI_HC_PORTS
) + i
,
101 VDEV_ST_NOTASSIGNED
);
102 out
+= sprintf(out
, "000 00000000 0000000000000000 0-0");
103 out
+= sprintf(out
, "\n");
108 static int status_name_to_id(const char *name
)
114 c
= strchr(name
, '.');
118 ret
= kstrtol(c
+1, 10, &val
);
125 static ssize_t
status_show(struct device
*dev
,
126 struct device_attribute
*attr
, char *out
)
132 "port sta spd dev socket local_busid\n");
134 pdev_nr
= status_name_to_id(attr
->attr
.name
);
136 out
+= status_show_not_ready(pdev_nr
, out
);
138 out
+= status_show_vhci(pdev_nr
, out
);
143 static ssize_t
nports_show(struct device
*dev
, struct device_attribute
*attr
,
148 out
+= sprintf(out
, "%d\n", VHCI_HC_PORTS
* vhci_num_controllers
);
151 static DEVICE_ATTR_RO(nports
);
153 /* Sysfs entry to shutdown a virtual connection */
154 static int vhci_port_disconnect(struct vhci_hcd
*vhci_hcd
, __u32 rhport
)
156 struct vhci_device
*vdev
= &vhci_hcd
->vdev
[rhport
];
157 struct vhci
*vhci
= vhci_hcd
->vhci
;
160 usbip_dbg_vhci_sysfs("enter\n");
163 spin_lock_irqsave(&vhci
->lock
, flags
);
164 spin_lock(&vdev
->ud
.lock
);
166 if (vdev
->ud
.status
== VDEV_ST_NULL
) {
167 pr_err("not connected %d\n", vdev
->ud
.status
);
170 spin_unlock(&vdev
->ud
.lock
);
171 spin_unlock_irqrestore(&vhci
->lock
, flags
);
177 spin_unlock(&vdev
->ud
.lock
);
178 spin_unlock_irqrestore(&vhci
->lock
, flags
);
180 usbip_event_add(&vdev
->ud
, VDEV_EVENT_DOWN
);
185 static int valid_port(__u32 pdev_nr
, __u32 rhport
)
187 if (pdev_nr
>= vhci_num_controllers
) {
188 pr_err("pdev %u\n", pdev_nr
);
191 if (rhport
>= VHCI_HC_PORTS
) {
192 pr_err("rhport %u\n", rhport
);
198 static ssize_t
store_detach(struct device
*dev
, struct device_attribute
*attr
,
199 const char *buf
, size_t count
)
201 __u32 port
= 0, pdev_nr
= 0, rhport
= 0;
205 if (kstrtoint(buf
, 10, &port
) < 0)
208 pdev_nr
= port_to_pdev_nr(port
);
209 rhport
= port_to_rhport(port
);
211 if (!valid_port(pdev_nr
, rhport
))
214 hcd
= platform_get_drvdata(vhcis
[pdev_nr
].pdev
);
216 dev_err(dev
, "port is not ready %u\n", port
);
220 ret
= vhci_port_disconnect(hcd_to_vhci_hcd(hcd
), rhport
);
224 usbip_dbg_vhci_sysfs("Leave\n");
228 static DEVICE_ATTR(detach
, S_IWUSR
, NULL
, store_detach
);
230 static int valid_args(__u32 pdev_nr
, __u32 rhport
, enum usb_device_speed speed
)
232 if (!valid_port(pdev_nr
, rhport
)) {
240 case USB_SPEED_WIRELESS
:
243 pr_err("Failed attach request for unsupported USB speed: %s\n",
244 usb_speed_string(speed
));
251 /* Sysfs entry to establish a virtual connection */
253 * To start a new USB/IP attachment, a userland program needs to setup a TCP
254 * connection and then write its socket descriptor with remote device
255 * information into this sysfs file.
257 * A remote device is virtually attached to the root-hub port of @rhport with
258 * @speed. @devid is embedded into a request to specify the remote device in a
261 * write() returns 0 on success, else negative errno.
263 static ssize_t
store_attach(struct device
*dev
, struct device_attribute
*attr
,
264 const char *buf
, size_t count
)
266 struct socket
*socket
;
268 __u32 port
= 0, pdev_nr
= 0, rhport
= 0, devid
= 0, speed
= 0;
270 struct vhci_hcd
*vhci_hcd
;
271 struct vhci_device
*vdev
;
277 * @rhport: port number of vhci_hcd
278 * @sockfd: socket descriptor of an established TCP connection
279 * @devid: unique device identifier in a remote host
280 * @speed: usb device speed in a remote host
282 if (sscanf(buf
, "%u %u %u %u", &port
, &sockfd
, &devid
, &speed
) != 4)
284 pdev_nr
= port_to_pdev_nr(port
);
285 rhport
= port_to_rhport(port
);
287 usbip_dbg_vhci_sysfs("port(%u) pdev(%d) rhport(%u)\n",
288 port
, pdev_nr
, rhport
);
289 usbip_dbg_vhci_sysfs("sockfd(%u) devid(%u) speed(%u)\n",
290 sockfd
, devid
, speed
);
292 /* check received parameters */
293 if (!valid_args(pdev_nr
, rhport
, speed
))
296 hcd
= platform_get_drvdata(vhcis
[pdev_nr
].pdev
);
298 dev_err(dev
, "port %d is not ready\n", port
);
302 vhci_hcd
= hcd_to_vhci_hcd(hcd
);
303 vhci
= vhci_hcd
->vhci
;
304 vdev
= &vhci_hcd
->vdev
[rhport
];
306 /* Extract socket from fd. */
307 socket
= sockfd_lookup(sockfd
, &err
);
311 /* now need lock until setting vdev status as used */
314 spin_lock_irqsave(&vhci
->lock
, flags
);
315 spin_lock(&vdev
->ud
.lock
);
317 if (vdev
->ud
.status
!= VDEV_ST_NULL
) {
318 /* end of the lock */
319 spin_unlock(&vdev
->ud
.lock
);
320 spin_unlock_irqrestore(&vhci
->lock
, flags
);
324 dev_err(dev
, "port %d already used\n", rhport
);
328 dev_info(dev
, "pdev(%u) rhport(%u) sockfd(%d)\n",
329 pdev_nr
, rhport
, sockfd
);
330 dev_info(dev
, "devid(%u) speed(%u) speed_str(%s)\n",
331 devid
, speed
, usb_speed_string(speed
));
335 vdev
->ud
.tcp_socket
= socket
;
336 vdev
->ud
.status
= VDEV_ST_NOTASSIGNED
;
338 spin_unlock(&vdev
->ud
.lock
);
339 spin_unlock_irqrestore(&vhci
->lock
, flags
);
342 vdev
->ud
.tcp_rx
= kthread_get_run(vhci_rx_loop
, &vdev
->ud
, "vhci_rx");
343 vdev
->ud
.tcp_tx
= kthread_get_run(vhci_tx_loop
, &vdev
->ud
, "vhci_tx");
345 rh_port_connect(vdev
, speed
);
349 static DEVICE_ATTR(attach
, S_IWUSR
, NULL
, store_attach
);
351 #define MAX_STATUS_NAME 16
354 struct device_attribute attr
;
355 char name
[MAX_STATUS_NAME
+1];
358 static struct status_attr
*status_attrs
;
360 static void set_status_attr(int id
)
362 struct status_attr
*status
;
364 status
= status_attrs
+ id
;
366 strcpy(status
->name
, "status");
368 snprintf(status
->name
, MAX_STATUS_NAME
+1, "status.%d", id
);
369 status
->attr
.attr
.name
= status
->name
;
370 status
->attr
.attr
.mode
= S_IRUGO
;
371 status
->attr
.show
= status_show
;
372 sysfs_attr_init(&status
->attr
.attr
);
375 static int init_status_attrs(void)
379 status_attrs
= kcalloc(vhci_num_controllers
, sizeof(struct status_attr
),
381 if (status_attrs
== NULL
)
384 for (id
= 0; id
< vhci_num_controllers
; id
++)
390 static void finish_status_attrs(void)
395 struct attribute_group vhci_attr_group
= {
399 int vhci_init_attr_group(void)
401 struct attribute
**attrs
;
404 attrs
= kcalloc((vhci_num_controllers
+ 5), sizeof(struct attribute
*),
409 ret
= init_status_attrs();
414 *attrs
= &dev_attr_nports
.attr
;
415 *(attrs
+ 1) = &dev_attr_detach
.attr
;
416 *(attrs
+ 2) = &dev_attr_attach
.attr
;
417 *(attrs
+ 3) = &dev_attr_usbip_debug
.attr
;
418 for (i
= 0; i
< vhci_num_controllers
; i
++)
419 *(attrs
+ i
+ 4) = &((status_attrs
+ i
)->attr
.attr
);
420 vhci_attr_group
.attrs
= attrs
;
424 void vhci_finish_attr_group(void)
426 finish_status_attrs();
427 kfree(vhci_attr_group
.attrs
);