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 | |
| 32 | /* Sysfs entry to show port status */ |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 33 | static ssize_t status_show_vhci(int pdev_nr, char *out) |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 34 | { |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 35 | struct platform_device *pdev = *(vhci_pdevs + pdev_nr); |
| 36 | struct vhci_hcd *vhci; |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 37 | char *s = out; |
| 38 | int i = 0; |
Andrew Goodbody | 2161979 | 2016-02-02 17:36:39 +0000 | [diff] [blame] | 39 | unsigned long flags; |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 40 | |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 41 | if (!pdev || !out) { |
| 42 | usbip_dbg_vhci_sysfs("show status error\n"); |
| 43 | return 0; |
| 44 | } |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 45 | |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 46 | vhci = hcd_to_vhci(platform_get_drvdata(pdev)); |
| 47 | |
| 48 | spin_lock_irqsave(&vhci->lock, flags); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 49 | |
| 50 | /* |
| 51 | * output example: |
Shuah Khan | ce601a0 | 2017-12-07 14:16:49 -0700 | [diff] [blame] | 52 | * port sta spd dev sockfd local_busid |
| 53 | * 0000 004 000 00000000 000003 1-2.3 |
| 54 | * 0001 004 000 00000000 000004 2-3.4 |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 55 | * |
Shuah Khan | ce601a0 | 2017-12-07 14:16:49 -0700 | [diff] [blame] | 56 | * Output includes socket fd instead of socket pointer address to |
| 57 | * avoid leaking kernel memory address in: |
| 58 | * /sys/devices/platform/vhci_hcd.0/status and in debug output. |
| 59 | * The socket pointer address is not used at the moment and it was |
| 60 | * made visible as a convenient way to find IP address from socket |
| 61 | * pointer address by looking up /proc/net/{tcp,tcp6}. As this opens |
| 62 | * a security hole, the change is made to use sockfd instead. |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 63 | */ |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 64 | for (i = 0; i < VHCI_HC_PORTS; i++) { |
| 65 | struct vhci_device *vdev = &vhci->vdev[i]; |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 66 | |
| 67 | spin_lock(&vdev->ud.lock); |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 68 | out += sprintf(out, "%04u %03u ", |
| 69 | (pdev_nr * VHCI_HC_PORTS) + i, |
| 70 | vdev->ud.status); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 71 | |
| 72 | if (vdev->ud.status == VDEV_ST_USED) { |
| 73 | out += sprintf(out, "%03u %08x ", |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 74 | vdev->speed, vdev->devid); |
Shuah Khan | ce601a0 | 2017-12-07 14:16:49 -0700 | [diff] [blame] | 75 | out += sprintf(out, "%06u %s", |
| 76 | vdev->ud.sockfd, |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 77 | dev_name(&vdev->udev->dev)); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 78 | |
matt mooney | bd608f6 | 2011-05-06 03:47:52 -0700 | [diff] [blame] | 79 | } else { |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 80 | out += sprintf(out, "000 00000000 "); |
Shuah Khan | ce601a0 | 2017-12-07 14:16:49 -0700 | [diff] [blame] | 81 | out += sprintf(out, "000000 0-0"); |
matt mooney | bd608f6 | 2011-05-06 03:47:52 -0700 | [diff] [blame] | 82 | } |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 83 | |
| 84 | out += sprintf(out, "\n"); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 85 | spin_unlock(&vdev->ud.lock); |
| 86 | } |
| 87 | |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 88 | spin_unlock_irqrestore(&vhci->lock, flags); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 89 | |
| 90 | return out - s; |
| 91 | } |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 92 | |
| 93 | static ssize_t status_show_not_ready(int pdev_nr, char *out) |
| 94 | { |
| 95 | char *s = out; |
| 96 | int i = 0; |
| 97 | |
| 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"); |
| 104 | } |
| 105 | return out - s; |
| 106 | } |
| 107 | |
| 108 | static int status_name_to_id(const char *name) |
| 109 | { |
| 110 | char *c; |
| 111 | long val; |
| 112 | int ret; |
| 113 | |
| 114 | c = strchr(name, '.'); |
| 115 | if (c == NULL) |
| 116 | return 0; |
| 117 | |
| 118 | ret = kstrtol(c+1, 10, &val); |
| 119 | if (ret < 0) |
| 120 | return ret; |
| 121 | |
| 122 | return val; |
| 123 | } |
| 124 | |
| 125 | static ssize_t status_show(struct device *dev, |
| 126 | struct device_attribute *attr, char *out) |
| 127 | { |
| 128 | char *s = out; |
| 129 | int pdev_nr; |
| 130 | |
| 131 | out += sprintf(out, |
Shuah Khan | ce601a0 | 2017-12-07 14:16:49 -0700 | [diff] [blame] | 132 | "port sta spd dev sockfd local_busid\n"); |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 133 | |
| 134 | pdev_nr = status_name_to_id(attr->attr.name); |
| 135 | if (pdev_nr < 0) |
| 136 | out += status_show_not_ready(pdev_nr, out); |
| 137 | else |
| 138 | out += status_show_vhci(pdev_nr, out); |
| 139 | |
| 140 | return out - s; |
| 141 | } |
| 142 | |
| 143 | static ssize_t nports_show(struct device *dev, struct device_attribute *attr, |
| 144 | char *out) |
| 145 | { |
| 146 | char *s = out; |
| 147 | |
| 148 | out += sprintf(out, "%d\n", VHCI_HC_PORTS * vhci_num_controllers); |
| 149 | return out - s; |
| 150 | } |
| 151 | static DEVICE_ATTR_RO(nports); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 152 | |
| 153 | /* Sysfs entry to shutdown a virtual connection */ |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 154 | static int vhci_port_disconnect(struct vhci_hcd *vhci, __u32 rhport) |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 155 | { |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 156 | struct vhci_device *vdev = &vhci->vdev[rhport]; |
Andrew Goodbody | 2161979 | 2016-02-02 17:36:39 +0000 | [diff] [blame] | 157 | unsigned long flags; |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 158 | |
Brian G. Merrell | b8868e4 | 2009-07-21 00:46:13 -0600 | [diff] [blame] | 159 | usbip_dbg_vhci_sysfs("enter\n"); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 160 | |
| 161 | /* lock */ |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 162 | spin_lock_irqsave(&vhci->lock, flags); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 163 | spin_lock(&vdev->ud.lock); |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 164 | |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 165 | if (vdev->ud.status == VDEV_ST_NULL) { |
matt mooney | 1a4b6f6 | 2011-05-19 16:47:32 -0700 | [diff] [blame] | 166 | pr_err("not connected %d\n", vdev->ud.status); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 167 | |
| 168 | /* unlock */ |
| 169 | spin_unlock(&vdev->ud.lock); |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 170 | spin_unlock_irqrestore(&vhci->lock, flags); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 171 | |
| 172 | return -EINVAL; |
| 173 | } |
| 174 | |
| 175 | /* unlock */ |
| 176 | spin_unlock(&vdev->ud.lock); |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 177 | spin_unlock_irqrestore(&vhci->lock, flags); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 178 | |
| 179 | usbip_event_add(&vdev->ud, VDEV_EVENT_DOWN); |
| 180 | |
| 181 | return 0; |
| 182 | } |
| 183 | |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 184 | static int valid_port(__u32 pdev_nr, __u32 rhport) |
| 185 | { |
| 186 | if (pdev_nr >= vhci_num_controllers) { |
| 187 | pr_err("pdev %u\n", pdev_nr); |
| 188 | return 0; |
| 189 | } |
| 190 | if (rhport >= VHCI_HC_PORTS) { |
| 191 | pr_err("rhport %u\n", rhport); |
| 192 | return 0; |
| 193 | } |
| 194 | return 1; |
| 195 | } |
| 196 | |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 197 | static ssize_t store_detach(struct device *dev, struct device_attribute *attr, |
| 198 | const char *buf, size_t count) |
| 199 | { |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 200 | __u32 port = 0, pdev_nr = 0, rhport = 0; |
| 201 | struct usb_hcd *hcd; |
| 202 | int ret; |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 203 | |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 204 | if (kstrtoint(buf, 10, &port) < 0) |
John de la Garza | 88fa1eb | 2014-03-06 10:36:34 -0800 | [diff] [blame] | 205 | return -EINVAL; |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 206 | |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 207 | pdev_nr = port_to_pdev_nr(port); |
| 208 | rhport = port_to_rhport(port); |
| 209 | |
| 210 | if (!valid_port(pdev_nr, rhport)) |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 211 | return -EINVAL; |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 212 | |
| 213 | hcd = platform_get_drvdata(*(vhci_pdevs + pdev_nr)); |
| 214 | if (hcd == NULL) { |
| 215 | dev_err(dev, "port is not ready %u\n", port); |
| 216 | return -EAGAIN; |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 217 | } |
| 218 | |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 219 | ret = vhci_port_disconnect(hcd_to_vhci(hcd), rhport); |
| 220 | if (ret < 0) |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 221 | return -EINVAL; |
| 222 | |
Brian G. Merrell | b8868e4 | 2009-07-21 00:46:13 -0600 | [diff] [blame] | 223 | usbip_dbg_vhci_sysfs("Leave\n"); |
matt mooney | bd608f6 | 2011-05-06 03:47:52 -0700 | [diff] [blame] | 224 | |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 225 | return count; |
| 226 | } |
| 227 | static DEVICE_ATTR(detach, S_IWUSR, NULL, store_detach); |
| 228 | |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 229 | 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] | 230 | { |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 231 | if (!valid_port(pdev_nr, rhport)) { |
| 232 | return 0; |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 233 | } |
| 234 | |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 235 | switch (speed) { |
| 236 | case USB_SPEED_LOW: |
| 237 | case USB_SPEED_FULL: |
| 238 | case USB_SPEED_HIGH: |
Greg Kroah-Hartman | 551cdbb | 2010-01-14 11:08:04 -0800 | [diff] [blame] | 239 | case USB_SPEED_WIRELESS: |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 240 | break; |
| 241 | default: |
Shuah Khan | 8360fb0 | 2014-01-22 09:38:14 -0700 | [diff] [blame] | 242 | pr_err("Failed attach request for unsupported USB speed: %s\n", |
| 243 | usb_speed_string(speed)); |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 244 | return 0; |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 245 | } |
| 246 | |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 247 | return 1; |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 248 | } |
| 249 | |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 250 | /* Sysfs entry to establish a virtual connection */ |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 251 | /* |
| 252 | * To start a new USB/IP attachment, a userland program needs to setup a TCP |
| 253 | * connection and then write its socket descriptor with remote device |
| 254 | * information into this sysfs file. |
| 255 | * |
| 256 | * A remote device is virtually attached to the root-hub port of @rhport with |
| 257 | * @speed. @devid is embedded into a request to specify the remote device in a |
| 258 | * server host. |
| 259 | * |
| 260 | * write() returns 0 on success, else negative errno. |
| 261 | */ |
| 262 | static ssize_t store_attach(struct device *dev, struct device_attribute *attr, |
| 263 | const char *buf, size_t count) |
| 264 | { |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 265 | struct socket *socket; |
| 266 | int sockfd = 0; |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 267 | __u32 port = 0, pdev_nr = 0, rhport = 0, devid = 0, speed = 0; |
| 268 | struct usb_hcd *hcd; |
| 269 | struct vhci_hcd *vhci; |
| 270 | struct vhci_device *vdev; |
Al Viro | 964ea96 | 2014-03-05 20:33:08 -0500 | [diff] [blame] | 271 | int err; |
Andrew Goodbody | 2161979 | 2016-02-02 17:36:39 +0000 | [diff] [blame] | 272 | unsigned long flags; |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 273 | |
| 274 | /* |
| 275 | * @rhport: port number of vhci_hcd |
| 276 | * @sockfd: socket descriptor of an established TCP connection |
| 277 | * @devid: unique device identifier in a remote host |
| 278 | * @speed: usb device speed in a remote host |
| 279 | */ |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 280 | 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] | 281 | return -EINVAL; |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 282 | pdev_nr = port_to_pdev_nr(port); |
| 283 | rhport = port_to_rhport(port); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 284 | |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 285 | usbip_dbg_vhci_sysfs("port(%u) pdev(%d) rhport(%u)\n", |
| 286 | port, pdev_nr, rhport); |
| 287 | usbip_dbg_vhci_sysfs("sockfd(%u) devid(%u) speed(%u)\n", |
| 288 | sockfd, devid, speed); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 289 | |
| 290 | /* check received parameters */ |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 291 | if (!valid_args(pdev_nr, rhport, speed)) |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 292 | return -EINVAL; |
| 293 | |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 294 | hcd = platform_get_drvdata(*(vhci_pdevs + pdev_nr)); |
| 295 | if (hcd == NULL) { |
| 296 | dev_err(dev, "port %d is not ready\n", port); |
| 297 | return -EAGAIN; |
| 298 | } |
| 299 | vhci = hcd_to_vhci(hcd); |
| 300 | vdev = &vhci->vdev[rhport]; |
| 301 | |
Bernard Blackham | 3d0a2a2 | 2012-10-22 06:45:00 +1100 | [diff] [blame] | 302 | /* Extract socket from fd. */ |
Al Viro | 964ea96 | 2014-03-05 20:33:08 -0500 | [diff] [blame] | 303 | socket = sockfd_lookup(sockfd, &err); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 304 | if (!socket) |
Márton Németh | a6d81814a | 2011-05-27 06:18:48 +0200 | [diff] [blame] | 305 | return -EINVAL; |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 306 | |
| 307 | /* now need lock until setting vdev status as used */ |
| 308 | |
| 309 | /* begin a lock */ |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 310 | spin_lock_irqsave(&vhci->lock, flags); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 311 | spin_lock(&vdev->ud.lock); |
| 312 | |
| 313 | if (vdev->ud.status != VDEV_ST_NULL) { |
| 314 | /* end of the lock */ |
| 315 | spin_unlock(&vdev->ud.lock); |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 316 | spin_unlock_irqrestore(&vhci->lock, flags); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 317 | |
Al Viro | 964ea96 | 2014-03-05 20:33:08 -0500 | [diff] [blame] | 318 | sockfd_put(socket); |
Bernard Blackham | 3d0a2a2 | 2012-10-22 06:45:00 +1100 | [diff] [blame] | 319 | |
matt mooney | 1a4b6f6 | 2011-05-19 16:47:32 -0700 | [diff] [blame] | 320 | dev_err(dev, "port %d already used\n", rhport); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 321 | return -EINVAL; |
| 322 | } |
| 323 | |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 324 | dev_info(dev, "pdev(%u) rhport(%u) sockfd(%d)\n", |
| 325 | pdev_nr, rhport, sockfd); |
| 326 | dev_info(dev, "devid(%u) speed(%u) speed_str(%s)\n", |
| 327 | devid, speed, usb_speed_string(speed)); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 328 | |
| 329 | vdev->devid = devid; |
| 330 | vdev->speed = speed; |
Shuah Khan | ce601a0 | 2017-12-07 14:16:49 -0700 | [diff] [blame] | 331 | vdev->ud.sockfd = sockfd; |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 332 | vdev->ud.tcp_socket = socket; |
| 333 | vdev->ud.status = VDEV_ST_NOTASSIGNED; |
| 334 | |
| 335 | spin_unlock(&vdev->ud.lock); |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 336 | spin_unlock_irqrestore(&vhci->lock, flags); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 337 | /* end the lock */ |
| 338 | |
Oleg Nesterov | ba46ce3 | 2012-03-13 19:07:18 +0100 | [diff] [blame] | 339 | vdev->ud.tcp_rx = kthread_get_run(vhci_rx_loop, &vdev->ud, "vhci_rx"); |
| 340 | 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] | 341 | |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 342 | rh_port_connect(vdev, speed); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 343 | |
| 344 | return count; |
| 345 | } |
| 346 | static DEVICE_ATTR(attach, S_IWUSR, NULL, store_attach); |
| 347 | |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 348 | #define MAX_STATUS_NAME 16 |
| 349 | |
| 350 | struct status_attr { |
| 351 | struct device_attribute attr; |
| 352 | char name[MAX_STATUS_NAME+1]; |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 353 | }; |
| 354 | |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 355 | static struct status_attr *status_attrs; |
| 356 | |
| 357 | static void set_status_attr(int id) |
| 358 | { |
| 359 | struct status_attr *status; |
| 360 | |
| 361 | status = status_attrs + id; |
| 362 | if (id == 0) |
| 363 | strcpy(status->name, "status"); |
| 364 | else |
| 365 | snprintf(status->name, MAX_STATUS_NAME+1, "status.%d", id); |
| 366 | status->attr.attr.name = status->name; |
| 367 | status->attr.attr.mode = S_IRUGO; |
| 368 | status->attr.show = status_show; |
Shuah Khan | 87ac297 | 2016-12-05 12:56:38 -0700 | [diff] [blame] | 369 | sysfs_attr_init(&status->attr.attr); |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | static int init_status_attrs(void) |
| 373 | { |
| 374 | int id; |
| 375 | |
| 376 | status_attrs = kcalloc(vhci_num_controllers, sizeof(struct status_attr), |
| 377 | GFP_KERNEL); |
| 378 | if (status_attrs == NULL) |
| 379 | return -ENOMEM; |
| 380 | |
| 381 | for (id = 0; id < vhci_num_controllers; id++) |
| 382 | set_status_attr(id); |
| 383 | |
| 384 | return 0; |
| 385 | } |
| 386 | |
| 387 | static void finish_status_attrs(void) |
| 388 | { |
| 389 | kfree(status_attrs); |
| 390 | } |
| 391 | |
| 392 | struct attribute_group vhci_attr_group = { |
| 393 | .attrs = NULL, |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 394 | }; |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 395 | |
| 396 | int vhci_init_attr_group(void) |
| 397 | { |
| 398 | struct attribute **attrs; |
| 399 | int ret, i; |
| 400 | |
| 401 | attrs = kcalloc((vhci_num_controllers + 5), sizeof(struct attribute *), |
| 402 | GFP_KERNEL); |
| 403 | if (attrs == NULL) |
| 404 | return -ENOMEM; |
| 405 | |
| 406 | ret = init_status_attrs(); |
| 407 | if (ret) { |
| 408 | kfree(attrs); |
| 409 | return ret; |
| 410 | } |
| 411 | *attrs = &dev_attr_nports.attr; |
| 412 | *(attrs + 1) = &dev_attr_detach.attr; |
| 413 | *(attrs + 2) = &dev_attr_attach.attr; |
| 414 | *(attrs + 3) = &dev_attr_usbip_debug.attr; |
| 415 | for (i = 0; i < vhci_num_controllers; i++) |
| 416 | *(attrs + i + 4) = &((status_attrs + i)->attr.attr); |
| 417 | vhci_attr_group.attrs = attrs; |
| 418 | return 0; |
| 419 | } |
| 420 | |
| 421 | void vhci_finish_attr_group(void) |
| 422 | { |
| 423 | finish_status_attrs(); |
| 424 | kfree(vhci_attr_group.attrs); |
| 425 | } |