Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2003-2008 Takahiro Hirofuchi |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 3 | * Copyright (C) 2015-2016 Nobuo Iwata |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 4 | * |
| 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. |
| 9 | * |
| 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. |
| 14 | * |
| 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, |
| 18 | * USA. |
| 19 | */ |
| 20 | |
matt mooney | 7aaacb4 | 2011-05-11 22:33:43 -0700 | [diff] [blame] | 21 | #include <linux/kthread.h> |
Bernard Blackham | 3d0a2a2 | 2012-10-22 06:45:00 +1100 | [diff] [blame] | 22 | #include <linux/file.h> |
matt mooney | 7aaacb4 | 2011-05-11 22:33:43 -0700 | [diff] [blame] | 23 | #include <linux/net.h> |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 24 | #include <linux/platform_device.h> |
| 25 | #include <linux/slab.h> |
matt mooney | 7aaacb4 | 2011-05-11 22:33:43 -0700 | [diff] [blame] | 26 | |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 27 | #include "usbip_common.h" |
| 28 | #include "vhci.h" |
| 29 | |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 30 | /* TODO: refine locking ?*/ |
| 31 | |
Yuyang Du | 1c9de5b | 2017-06-08 13:04:10 +0800 | [diff] [blame] | 32 | /* |
| 33 | * output example: |
| 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 | * ................................................ |
| 39 | * |
| 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. |
| 43 | */ |
| 44 | static void port_show_vhci(char **out, int hub, int port, struct vhci_device *vdev) |
| 45 | { |
| 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); |
| 52 | |
| 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", |
| 57 | vdev->ud.tcp_socket, |
| 58 | dev_name(&vdev->udev->dev)); |
| 59 | |
| 60 | } else { |
| 61 | *out += sprintf(*out, "000 00000000 "); |
| 62 | *out += sprintf(*out, "0000000000000000 0-0"); |
| 63 | } |
| 64 | |
| 65 | *out += sprintf(*out, "\n"); |
| 66 | } |
| 67 | |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 68 | /* Sysfs entry to show port status */ |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 69 | static ssize_t status_show_vhci(int pdev_nr, char *out) |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 70 | { |
Yuyang Du | 89a73d2 | 2017-06-08 13:04:07 +0800 | [diff] [blame] | 71 | struct platform_device *pdev = vhcis[pdev_nr].pdev; |
Yuyang Du | 03cd00d | 2017-06-08 13:04:09 +0800 | [diff] [blame] | 72 | struct vhci *vhci; |
| 73 | struct usb_hcd *hcd; |
| 74 | struct vhci_hcd *vhci_hcd; |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 75 | char *s = out; |
Yuyang Du | 03cd00d | 2017-06-08 13:04:09 +0800 | [diff] [blame] | 76 | int i; |
Andrew Goodbody | 2161979 | 2016-02-02 17:36:39 +0000 | [diff] [blame] | 77 | unsigned long flags; |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 78 | |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 79 | if (!pdev || !out) { |
| 80 | usbip_dbg_vhci_sysfs("show status error\n"); |
| 81 | return 0; |
| 82 | } |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 83 | |
Yuyang Du | 03cd00d | 2017-06-08 13:04:09 +0800 | [diff] [blame] | 84 | hcd = platform_get_drvdata(pdev); |
| 85 | vhci_hcd = hcd_to_vhci_hcd(hcd); |
| 86 | vhci = vhci_hcd->vhci; |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 87 | |
| 88 | spin_lock_irqsave(&vhci->lock, flags); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 89 | |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 90 | for (i = 0; i < VHCI_HC_PORTS; i++) { |
Yuyang Du | 1c9de5b | 2017-06-08 13:04:10 +0800 | [diff] [blame] | 91 | struct vhci_device *vdev = &vhci->vhci_hcd_hs->vdev[i]; |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 92 | |
| 93 | spin_lock(&vdev->ud.lock); |
Yuyang Du | 1c9de5b | 2017-06-08 13:04:10 +0800 | [diff] [blame] | 94 | port_show_vhci(&out, HUB_SPEED_HIGH, |
Yuyang Du | b891245 | 2017-06-08 13:04:13 +0800 | [diff] [blame] | 95 | pdev_nr * VHCI_PORTS + i, vdev); |
Yuyang Du | 1c9de5b | 2017-06-08 13:04:10 +0800 | [diff] [blame] | 96 | spin_unlock(&vdev->ud.lock); |
| 97 | } |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 98 | |
Yuyang Du | 1c9de5b | 2017-06-08 13:04:10 +0800 | [diff] [blame] | 99 | for (i = 0; i < VHCI_HC_PORTS; i++) { |
| 100 | struct vhci_device *vdev = &vhci->vhci_hcd_ss->vdev[i]; |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 101 | |
Yuyang Du | 1c9de5b | 2017-06-08 13:04:10 +0800 | [diff] [blame] | 102 | spin_lock(&vdev->ud.lock); |
| 103 | port_show_vhci(&out, HUB_SPEED_SUPER, |
Yuyang Du | b891245 | 2017-06-08 13:04:13 +0800 | [diff] [blame] | 104 | pdev_nr * VHCI_PORTS + VHCI_HC_PORTS + i, vdev); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 105 | spin_unlock(&vdev->ud.lock); |
| 106 | } |
| 107 | |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 108 | spin_unlock_irqrestore(&vhci->lock, flags); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 109 | |
| 110 | return out - s; |
| 111 | } |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 112 | |
| 113 | static ssize_t status_show_not_ready(int pdev_nr, char *out) |
| 114 | { |
| 115 | char *s = out; |
| 116 | int i = 0; |
| 117 | |
| 118 | for (i = 0; i < VHCI_HC_PORTS; i++) { |
Yuyang Du | 1c9de5b | 2017-06-08 13:04:10 +0800 | [diff] [blame] | 119 | out += sprintf(out, "hs %04u %03u ", |
Yuyang Du | b891245 | 2017-06-08 13:04:13 +0800 | [diff] [blame] | 120 | (pdev_nr * VHCI_PORTS) + i, |
Yuyang Du | 1c9de5b | 2017-06-08 13:04:10 +0800 | [diff] [blame] | 121 | VDEV_ST_NOTASSIGNED); |
| 122 | out += sprintf(out, "000 00000000 0000000000000000 0-0"); |
| 123 | out += sprintf(out, "\n"); |
| 124 | } |
| 125 | |
| 126 | for (i = 0; i < VHCI_HC_PORTS; i++) { |
| 127 | out += sprintf(out, "ss %04u %03u ", |
Yuyang Du | b891245 | 2017-06-08 13:04:13 +0800 | [diff] [blame] | 128 | (pdev_nr * VHCI_PORTS) + VHCI_HC_PORTS + i, |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 129 | VDEV_ST_NOTASSIGNED); |
| 130 | out += sprintf(out, "000 00000000 0000000000000000 0-0"); |
| 131 | out += sprintf(out, "\n"); |
| 132 | } |
| 133 | return out - s; |
| 134 | } |
| 135 | |
| 136 | static int status_name_to_id(const char *name) |
| 137 | { |
| 138 | char *c; |
| 139 | long val; |
| 140 | int ret; |
| 141 | |
| 142 | c = strchr(name, '.'); |
| 143 | if (c == NULL) |
| 144 | return 0; |
| 145 | |
| 146 | ret = kstrtol(c+1, 10, &val); |
| 147 | if (ret < 0) |
| 148 | return ret; |
| 149 | |
| 150 | return val; |
| 151 | } |
| 152 | |
| 153 | static ssize_t status_show(struct device *dev, |
| 154 | struct device_attribute *attr, char *out) |
| 155 | { |
| 156 | char *s = out; |
| 157 | int pdev_nr; |
| 158 | |
| 159 | out += sprintf(out, |
Yuyang Du | 1c9de5b | 2017-06-08 13:04:10 +0800 | [diff] [blame] | 160 | "hub port sta spd dev socket local_busid\n"); |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 161 | |
| 162 | pdev_nr = status_name_to_id(attr->attr.name); |
| 163 | if (pdev_nr < 0) |
| 164 | out += status_show_not_ready(pdev_nr, out); |
| 165 | else |
| 166 | out += status_show_vhci(pdev_nr, out); |
| 167 | |
| 168 | return out - s; |
| 169 | } |
| 170 | |
| 171 | static ssize_t nports_show(struct device *dev, struct device_attribute *attr, |
| 172 | char *out) |
| 173 | { |
| 174 | char *s = out; |
| 175 | |
Yuyang Du | 1c9de5b | 2017-06-08 13:04:10 +0800 | [diff] [blame] | 176 | /* |
| 177 | * Half the ports are for SPEED_HIGH and half for SPEED_SUPER, thus the * 2. |
| 178 | */ |
Yuyang Du | b891245 | 2017-06-08 13:04:13 +0800 | [diff] [blame] | 179 | out += sprintf(out, "%d\n", VHCI_PORTS * vhci_num_controllers); |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 180 | return out - s; |
| 181 | } |
| 182 | static DEVICE_ATTR_RO(nports); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 183 | |
| 184 | /* Sysfs entry to shutdown a virtual connection */ |
Yuyang Du | 03cd00d | 2017-06-08 13:04:09 +0800 | [diff] [blame] | 185 | static int vhci_port_disconnect(struct vhci_hcd *vhci_hcd, __u32 rhport) |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 186 | { |
Yuyang Du | 03cd00d | 2017-06-08 13:04:09 +0800 | [diff] [blame] | 187 | struct vhci_device *vdev = &vhci_hcd->vdev[rhport]; |
| 188 | struct vhci *vhci = vhci_hcd->vhci; |
Andrew Goodbody | 2161979 | 2016-02-02 17:36:39 +0000 | [diff] [blame] | 189 | unsigned long flags; |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 190 | |
Brian G. Merrell | b8868e4 | 2009-07-21 00:46:13 -0600 | [diff] [blame] | 191 | usbip_dbg_vhci_sysfs("enter\n"); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 192 | |
| 193 | /* lock */ |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 194 | spin_lock_irqsave(&vhci->lock, flags); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 195 | spin_lock(&vdev->ud.lock); |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 196 | |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 197 | if (vdev->ud.status == VDEV_ST_NULL) { |
matt mooney | 1a4b6f6 | 2011-05-19 16:47:32 -0700 | [diff] [blame] | 198 | pr_err("not connected %d\n", vdev->ud.status); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 199 | |
| 200 | /* unlock */ |
| 201 | spin_unlock(&vdev->ud.lock); |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 202 | spin_unlock_irqrestore(&vhci->lock, flags); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 203 | |
| 204 | return -EINVAL; |
| 205 | } |
| 206 | |
| 207 | /* unlock */ |
| 208 | spin_unlock(&vdev->ud.lock); |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 209 | spin_unlock_irqrestore(&vhci->lock, flags); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 210 | |
| 211 | usbip_event_add(&vdev->ud, VDEV_EVENT_DOWN); |
| 212 | |
| 213 | return 0; |
| 214 | } |
| 215 | |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 216 | static int valid_port(__u32 pdev_nr, __u32 rhport) |
| 217 | { |
| 218 | if (pdev_nr >= vhci_num_controllers) { |
| 219 | pr_err("pdev %u\n", pdev_nr); |
| 220 | return 0; |
| 221 | } |
| 222 | if (rhport >= VHCI_HC_PORTS) { |
| 223 | pr_err("rhport %u\n", rhport); |
| 224 | return 0; |
| 225 | } |
| 226 | return 1; |
| 227 | } |
| 228 | |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 229 | static ssize_t store_detach(struct device *dev, struct device_attribute *attr, |
| 230 | const char *buf, size_t count) |
| 231 | { |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 232 | __u32 port = 0, pdev_nr = 0, rhport = 0; |
| 233 | struct usb_hcd *hcd; |
Yuyang Du | 1c9de5b | 2017-06-08 13:04:10 +0800 | [diff] [blame] | 234 | struct vhci_hcd *vhci_hcd; |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 235 | int ret; |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 236 | |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 237 | if (kstrtoint(buf, 10, &port) < 0) |
John de la Garza | 88fa1eb | 2014-03-06 10:36:34 -0800 | [diff] [blame] | 238 | return -EINVAL; |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 239 | |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 240 | pdev_nr = port_to_pdev_nr(port); |
| 241 | rhport = port_to_rhport(port); |
| 242 | |
| 243 | if (!valid_port(pdev_nr, rhport)) |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 244 | return -EINVAL; |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 245 | |
Yuyang Du | 89a73d2 | 2017-06-08 13:04:07 +0800 | [diff] [blame] | 246 | hcd = platform_get_drvdata(vhcis[pdev_nr].pdev); |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 247 | if (hcd == NULL) { |
| 248 | dev_err(dev, "port is not ready %u\n", port); |
| 249 | return -EAGAIN; |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 250 | } |
| 251 | |
Yuyang Du | 1c9de5b | 2017-06-08 13:04:10 +0800 | [diff] [blame] | 252 | usbip_dbg_vhci_sysfs("rhport %d\n", rhport); |
| 253 | |
| 254 | if ((port / VHCI_HC_PORTS) % 2) |
| 255 | vhci_hcd = hcd_to_vhci_hcd(hcd)->vhci->vhci_hcd_ss; |
| 256 | else |
| 257 | vhci_hcd = hcd_to_vhci_hcd(hcd)->vhci->vhci_hcd_hs; |
| 258 | |
| 259 | ret = vhci_port_disconnect(vhci_hcd, rhport); |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 260 | if (ret < 0) |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 261 | return -EINVAL; |
| 262 | |
Brian G. Merrell | b8868e4 | 2009-07-21 00:46:13 -0600 | [diff] [blame] | 263 | usbip_dbg_vhci_sysfs("Leave\n"); |
matt mooney | bd608f6 | 2011-05-06 03:47:52 -0700 | [diff] [blame] | 264 | |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 265 | return count; |
| 266 | } |
| 267 | static DEVICE_ATTR(detach, S_IWUSR, NULL, store_detach); |
| 268 | |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 269 | static int valid_args(__u32 pdev_nr, __u32 rhport, enum usb_device_speed speed) |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 270 | { |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 271 | if (!valid_port(pdev_nr, rhport)) { |
| 272 | return 0; |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 273 | } |
| 274 | |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 275 | switch (speed) { |
| 276 | case USB_SPEED_LOW: |
| 277 | case USB_SPEED_FULL: |
| 278 | case USB_SPEED_HIGH: |
Greg Kroah-Hartman | 551cdbb | 2010-01-14 11:08:04 -0800 | [diff] [blame] | 279 | case USB_SPEED_WIRELESS: |
Yuyang Du | df9032c | 2017-06-08 13:04:11 +0800 | [diff] [blame] | 280 | case USB_SPEED_SUPER: |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 281 | break; |
| 282 | default: |
Shuah Khan | 8360fb0 | 2014-01-22 09:38:14 -0700 | [diff] [blame] | 283 | pr_err("Failed attach request for unsupported USB speed: %s\n", |
| 284 | usb_speed_string(speed)); |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 285 | return 0; |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 286 | } |
| 287 | |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 288 | return 1; |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 289 | } |
| 290 | |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 291 | /* Sysfs entry to establish a virtual connection */ |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 292 | /* |
| 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. |
| 296 | * |
| 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 |
| 299 | * server host. |
| 300 | * |
| 301 | * write() returns 0 on success, else negative errno. |
| 302 | */ |
| 303 | static ssize_t store_attach(struct device *dev, struct device_attribute *attr, |
| 304 | const char *buf, size_t count) |
| 305 | { |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 306 | struct socket *socket; |
| 307 | int sockfd = 0; |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 308 | __u32 port = 0, pdev_nr = 0, rhport = 0, devid = 0, speed = 0; |
| 309 | struct usb_hcd *hcd; |
Yuyang Du | 03cd00d | 2017-06-08 13:04:09 +0800 | [diff] [blame] | 310 | struct vhci_hcd *vhci_hcd; |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 311 | struct vhci_device *vdev; |
Yuyang Du | 03cd00d | 2017-06-08 13:04:09 +0800 | [diff] [blame] | 312 | struct vhci *vhci; |
Al Viro | 964ea96 | 2014-03-05 20:33:08 -0500 | [diff] [blame] | 313 | int err; |
Andrew Goodbody | 2161979 | 2016-02-02 17:36:39 +0000 | [diff] [blame] | 314 | unsigned long flags; |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 315 | |
| 316 | /* |
| 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 |
| 321 | */ |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 322 | if (sscanf(buf, "%u %u %u %u", &port, &sockfd, &devid, &speed) != 4) |
John de la Garza | 88fa1eb | 2014-03-06 10:36:34 -0800 | [diff] [blame] | 323 | return -EINVAL; |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 324 | pdev_nr = port_to_pdev_nr(port); |
| 325 | rhport = port_to_rhport(port); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 326 | |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 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); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 331 | |
| 332 | /* check received parameters */ |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 333 | if (!valid_args(pdev_nr, rhport, speed)) |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 334 | return -EINVAL; |
| 335 | |
Yuyang Du | 89a73d2 | 2017-06-08 13:04:07 +0800 | [diff] [blame] | 336 | hcd = platform_get_drvdata(vhcis[pdev_nr].pdev); |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 337 | if (hcd == NULL) { |
| 338 | dev_err(dev, "port %d is not ready\n", port); |
| 339 | return -EAGAIN; |
| 340 | } |
Yuyang Du | 03cd00d | 2017-06-08 13:04:09 +0800 | [diff] [blame] | 341 | |
| 342 | vhci_hcd = hcd_to_vhci_hcd(hcd); |
| 343 | vhci = vhci_hcd->vhci; |
Yuyang Du | 1c9de5b | 2017-06-08 13:04:10 +0800 | [diff] [blame] | 344 | |
| 345 | if (speed == USB_SPEED_SUPER) |
| 346 | vdev = &vhci->vhci_hcd_ss->vdev[rhport]; |
| 347 | else |
| 348 | vdev = &vhci->vhci_hcd_hs->vdev[rhport]; |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 349 | |
Bernard Blackham | 3d0a2a2 | 2012-10-22 06:45:00 +1100 | [diff] [blame] | 350 | /* Extract socket from fd. */ |
Al Viro | 964ea96 | 2014-03-05 20:33:08 -0500 | [diff] [blame] | 351 | socket = sockfd_lookup(sockfd, &err); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 352 | if (!socket) |
Márton Németh | a6d81814a | 2011-05-27 06:18:48 +0200 | [diff] [blame] | 353 | return -EINVAL; |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 354 | |
| 355 | /* now need lock until setting vdev status as used */ |
| 356 | |
| 357 | /* begin a lock */ |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 358 | spin_lock_irqsave(&vhci->lock, flags); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 359 | spin_lock(&vdev->ud.lock); |
| 360 | |
| 361 | if (vdev->ud.status != VDEV_ST_NULL) { |
| 362 | /* end of the lock */ |
| 363 | spin_unlock(&vdev->ud.lock); |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 364 | spin_unlock_irqrestore(&vhci->lock, flags); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 365 | |
Al Viro | 964ea96 | 2014-03-05 20:33:08 -0500 | [diff] [blame] | 366 | sockfd_put(socket); |
Bernard Blackham | 3d0a2a2 | 2012-10-22 06:45:00 +1100 | [diff] [blame] | 367 | |
matt mooney | 1a4b6f6 | 2011-05-19 16:47:32 -0700 | [diff] [blame] | 368 | dev_err(dev, "port %d already used\n", rhport); |
Nobuo Iwata | a38711a | 2017-08-29 11:13:34 +0900 | [diff] [blame] | 369 | /* |
| 370 | * Will be retried from userspace |
| 371 | * if there's another free port. |
| 372 | */ |
| 373 | return -EBUSY; |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 374 | } |
| 375 | |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 376 | dev_info(dev, "pdev(%u) rhport(%u) sockfd(%d)\n", |
| 377 | pdev_nr, rhport, sockfd); |
| 378 | dev_info(dev, "devid(%u) speed(%u) speed_str(%s)\n", |
| 379 | devid, speed, usb_speed_string(speed)); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 380 | |
| 381 | vdev->devid = devid; |
| 382 | vdev->speed = speed; |
| 383 | vdev->ud.tcp_socket = socket; |
| 384 | vdev->ud.status = VDEV_ST_NOTASSIGNED; |
| 385 | |
| 386 | spin_unlock(&vdev->ud.lock); |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 387 | spin_unlock_irqrestore(&vhci->lock, flags); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 388 | /* end the lock */ |
| 389 | |
Oleg Nesterov | ba46ce3 | 2012-03-13 19:07:18 +0100 | [diff] [blame] | 390 | vdev->ud.tcp_rx = kthread_get_run(vhci_rx_loop, &vdev->ud, "vhci_rx"); |
| 391 | vdev->ud.tcp_tx = kthread_get_run(vhci_tx_loop, &vdev->ud, "vhci_tx"); |
Max Vozeler | d1b2e95 | 2011-04-18 21:44:10 +0200 | [diff] [blame] | 392 | |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 393 | rh_port_connect(vdev, speed); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 394 | |
| 395 | return count; |
| 396 | } |
| 397 | static DEVICE_ATTR(attach, S_IWUSR, NULL, store_attach); |
| 398 | |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 399 | #define MAX_STATUS_NAME 16 |
| 400 | |
| 401 | struct status_attr { |
| 402 | struct device_attribute attr; |
| 403 | char name[MAX_STATUS_NAME+1]; |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 404 | }; |
| 405 | |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 406 | static struct status_attr *status_attrs; |
| 407 | |
| 408 | static void set_status_attr(int id) |
| 409 | { |
| 410 | struct status_attr *status; |
| 411 | |
| 412 | status = status_attrs + id; |
| 413 | if (id == 0) |
| 414 | strcpy(status->name, "status"); |
| 415 | else |
| 416 | snprintf(status->name, MAX_STATUS_NAME+1, "status.%d", id); |
| 417 | status->attr.attr.name = status->name; |
| 418 | status->attr.attr.mode = S_IRUGO; |
| 419 | status->attr.show = status_show; |
Shuah Khan | 918b8ac | 2016-12-05 12:56:38 -0700 | [diff] [blame] | 420 | sysfs_attr_init(&status->attr.attr); |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | static int init_status_attrs(void) |
| 424 | { |
| 425 | int id; |
| 426 | |
| 427 | status_attrs = kcalloc(vhci_num_controllers, sizeof(struct status_attr), |
| 428 | GFP_KERNEL); |
| 429 | if (status_attrs == NULL) |
| 430 | return -ENOMEM; |
| 431 | |
| 432 | for (id = 0; id < vhci_num_controllers; id++) |
| 433 | set_status_attr(id); |
| 434 | |
| 435 | return 0; |
| 436 | } |
| 437 | |
| 438 | static void finish_status_attrs(void) |
| 439 | { |
| 440 | kfree(status_attrs); |
| 441 | } |
| 442 | |
| 443 | struct attribute_group vhci_attr_group = { |
| 444 | .attrs = NULL, |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 445 | }; |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 446 | |
| 447 | int vhci_init_attr_group(void) |
| 448 | { |
| 449 | struct attribute **attrs; |
| 450 | int ret, i; |
| 451 | |
| 452 | attrs = kcalloc((vhci_num_controllers + 5), sizeof(struct attribute *), |
| 453 | GFP_KERNEL); |
| 454 | if (attrs == NULL) |
| 455 | return -ENOMEM; |
| 456 | |
| 457 | ret = init_status_attrs(); |
| 458 | if (ret) { |
| 459 | kfree(attrs); |
| 460 | return ret; |
| 461 | } |
| 462 | *attrs = &dev_attr_nports.attr; |
| 463 | *(attrs + 1) = &dev_attr_detach.attr; |
| 464 | *(attrs + 2) = &dev_attr_attach.attr; |
| 465 | *(attrs + 3) = &dev_attr_usbip_debug.attr; |
| 466 | for (i = 0; i < vhci_num_controllers; i++) |
| 467 | *(attrs + i + 4) = &((status_attrs + i)->attr.attr); |
| 468 | vhci_attr_group.attrs = attrs; |
| 469 | return 0; |
| 470 | } |
| 471 | |
| 472 | void vhci_finish_attr_group(void) |
| 473 | { |
| 474 | finish_status_attrs(); |
| 475 | kfree(vhci_attr_group.attrs); |
| 476 | } |