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 | { |
Yuyang Du | 89a73d2 | 2017-06-08 13:04:07 +0800 | [diff] [blame] | 35 | struct platform_device *pdev = vhcis[pdev_nr].pdev; |
Yuyang Du | 03cd00d | 2017-06-08 13:04:09 +0800 | [diff] [blame^] | 36 | struct vhci *vhci; |
| 37 | struct usb_hcd *hcd; |
| 38 | struct vhci_hcd *vhci_hcd; |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 39 | char *s = out; |
Yuyang Du | 03cd00d | 2017-06-08 13:04:09 +0800 | [diff] [blame^] | 40 | int i; |
Andrew Goodbody | 2161979 | 2016-02-02 17:36:39 +0000 | [diff] [blame] | 41 | unsigned long flags; |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 42 | |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 43 | if (!pdev || !out) { |
| 44 | usbip_dbg_vhci_sysfs("show status error\n"); |
| 45 | return 0; |
| 46 | } |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 47 | |
Yuyang Du | 03cd00d | 2017-06-08 13:04:09 +0800 | [diff] [blame^] | 48 | hcd = platform_get_drvdata(pdev); |
| 49 | vhci_hcd = hcd_to_vhci_hcd(hcd); |
| 50 | vhci = vhci_hcd->vhci; |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 51 | |
| 52 | spin_lock_irqsave(&vhci->lock, flags); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 53 | |
| 54 | /* |
| 55 | * output example: |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 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 |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 59 | * |
| 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. |
| 63 | */ |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 64 | for (i = 0; i < VHCI_HC_PORTS; i++) { |
Yuyang Du | 03cd00d | 2017-06-08 13:04:09 +0800 | [diff] [blame^] | 65 | struct vhci_device *vdev = &vhci_hcd->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); |
| 75 | out += sprintf(out, "%16p %s", |
| 76 | vdev->ud.tcp_socket, |
| 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 "); |
| 81 | out += sprintf(out, "0000000000000000 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, |
| 132 | "port sta spd dev socket local_busid\n"); |
| 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 */ |
Yuyang Du | 03cd00d | 2017-06-08 13:04:09 +0800 | [diff] [blame^] | 154 | static int vhci_port_disconnect(struct vhci_hcd *vhci_hcd, __u32 rhport) |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 155 | { |
Yuyang Du | 03cd00d | 2017-06-08 13:04:09 +0800 | [diff] [blame^] | 156 | struct vhci_device *vdev = &vhci_hcd->vdev[rhport]; |
| 157 | struct vhci *vhci = vhci_hcd->vhci; |
Andrew Goodbody | 2161979 | 2016-02-02 17:36:39 +0000 | [diff] [blame] | 158 | unsigned long flags; |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 159 | |
Brian G. Merrell | b8868e4 | 2009-07-21 00:46:13 -0600 | [diff] [blame] | 160 | usbip_dbg_vhci_sysfs("enter\n"); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 161 | |
| 162 | /* lock */ |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 163 | spin_lock_irqsave(&vhci->lock, flags); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 164 | spin_lock(&vdev->ud.lock); |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 165 | |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 166 | if (vdev->ud.status == VDEV_ST_NULL) { |
matt mooney | 1a4b6f6 | 2011-05-19 16:47:32 -0700 | [diff] [blame] | 167 | pr_err("not connected %d\n", vdev->ud.status); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 168 | |
| 169 | /* unlock */ |
| 170 | spin_unlock(&vdev->ud.lock); |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 171 | spin_unlock_irqrestore(&vhci->lock, flags); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 172 | |
| 173 | return -EINVAL; |
| 174 | } |
| 175 | |
| 176 | /* unlock */ |
| 177 | spin_unlock(&vdev->ud.lock); |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 178 | spin_unlock_irqrestore(&vhci->lock, flags); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 179 | |
| 180 | usbip_event_add(&vdev->ud, VDEV_EVENT_DOWN); |
| 181 | |
| 182 | return 0; |
| 183 | } |
| 184 | |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 185 | static int valid_port(__u32 pdev_nr, __u32 rhport) |
| 186 | { |
| 187 | if (pdev_nr >= vhci_num_controllers) { |
| 188 | pr_err("pdev %u\n", pdev_nr); |
| 189 | return 0; |
| 190 | } |
| 191 | if (rhport >= VHCI_HC_PORTS) { |
| 192 | pr_err("rhport %u\n", rhport); |
| 193 | return 0; |
| 194 | } |
| 195 | return 1; |
| 196 | } |
| 197 | |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 198 | static ssize_t store_detach(struct device *dev, struct device_attribute *attr, |
| 199 | const char *buf, size_t count) |
| 200 | { |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 201 | __u32 port = 0, pdev_nr = 0, rhport = 0; |
| 202 | struct usb_hcd *hcd; |
| 203 | int ret; |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 204 | |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 205 | if (kstrtoint(buf, 10, &port) < 0) |
John de la Garza | 88fa1eb | 2014-03-06 10:36:34 -0800 | [diff] [blame] | 206 | return -EINVAL; |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 207 | |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 208 | pdev_nr = port_to_pdev_nr(port); |
| 209 | rhport = port_to_rhport(port); |
| 210 | |
| 211 | if (!valid_port(pdev_nr, rhport)) |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 212 | return -EINVAL; |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 213 | |
Yuyang Du | 89a73d2 | 2017-06-08 13:04:07 +0800 | [diff] [blame] | 214 | hcd = platform_get_drvdata(vhcis[pdev_nr].pdev); |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 215 | if (hcd == NULL) { |
| 216 | dev_err(dev, "port is not ready %u\n", port); |
| 217 | return -EAGAIN; |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 218 | } |
| 219 | |
Yuyang Du | 5ec0edc | 2017-06-08 13:04:05 +0800 | [diff] [blame] | 220 | ret = vhci_port_disconnect(hcd_to_vhci_hcd(hcd), rhport); |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 221 | if (ret < 0) |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 222 | return -EINVAL; |
| 223 | |
Brian G. Merrell | b8868e4 | 2009-07-21 00:46:13 -0600 | [diff] [blame] | 224 | usbip_dbg_vhci_sysfs("Leave\n"); |
matt mooney | bd608f6 | 2011-05-06 03:47:52 -0700 | [diff] [blame] | 225 | |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 226 | return count; |
| 227 | } |
| 228 | static DEVICE_ATTR(detach, S_IWUSR, NULL, store_detach); |
| 229 | |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 230 | 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] | 231 | { |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 232 | if (!valid_port(pdev_nr, rhport)) { |
| 233 | return 0; |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 234 | } |
| 235 | |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 236 | switch (speed) { |
| 237 | case USB_SPEED_LOW: |
| 238 | case USB_SPEED_FULL: |
| 239 | case USB_SPEED_HIGH: |
Greg Kroah-Hartman | 551cdbb | 2010-01-14 11:08:04 -0800 | [diff] [blame] | 240 | case USB_SPEED_WIRELESS: |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 241 | break; |
| 242 | default: |
Shuah Khan | 8360fb0 | 2014-01-22 09:38:14 -0700 | [diff] [blame] | 243 | pr_err("Failed attach request for unsupported USB speed: %s\n", |
| 244 | usb_speed_string(speed)); |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 245 | return 0; |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 246 | } |
| 247 | |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 248 | return 1; |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 249 | } |
| 250 | |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 251 | /* Sysfs entry to establish a virtual connection */ |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 252 | /* |
| 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. |
| 256 | * |
| 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 |
| 259 | * server host. |
| 260 | * |
| 261 | * write() returns 0 on success, else negative errno. |
| 262 | */ |
| 263 | static ssize_t store_attach(struct device *dev, struct device_attribute *attr, |
| 264 | const char *buf, size_t count) |
| 265 | { |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 266 | struct socket *socket; |
| 267 | int sockfd = 0; |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 268 | __u32 port = 0, pdev_nr = 0, rhport = 0, devid = 0, speed = 0; |
| 269 | struct usb_hcd *hcd; |
Yuyang Du | 03cd00d | 2017-06-08 13:04:09 +0800 | [diff] [blame^] | 270 | struct vhci_hcd *vhci_hcd; |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 271 | struct vhci_device *vdev; |
Yuyang Du | 03cd00d | 2017-06-08 13:04:09 +0800 | [diff] [blame^] | 272 | struct vhci *vhci; |
Al Viro | 964ea96 | 2014-03-05 20:33:08 -0500 | [diff] [blame] | 273 | int err; |
Andrew Goodbody | 2161979 | 2016-02-02 17:36:39 +0000 | [diff] [blame] | 274 | unsigned long flags; |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 275 | |
| 276 | /* |
| 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 |
| 281 | */ |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 282 | 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] | 283 | return -EINVAL; |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 284 | pdev_nr = port_to_pdev_nr(port); |
| 285 | rhport = port_to_rhport(port); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 286 | |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 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); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 291 | |
| 292 | /* check received parameters */ |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 293 | if (!valid_args(pdev_nr, rhport, speed)) |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 294 | return -EINVAL; |
| 295 | |
Yuyang Du | 89a73d2 | 2017-06-08 13:04:07 +0800 | [diff] [blame] | 296 | hcd = platform_get_drvdata(vhcis[pdev_nr].pdev); |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 297 | if (hcd == NULL) { |
| 298 | dev_err(dev, "port %d is not ready\n", port); |
| 299 | return -EAGAIN; |
| 300 | } |
Yuyang Du | 03cd00d | 2017-06-08 13:04:09 +0800 | [diff] [blame^] | 301 | |
| 302 | vhci_hcd = hcd_to_vhci_hcd(hcd); |
| 303 | vhci = vhci_hcd->vhci; |
| 304 | vdev = &vhci_hcd->vdev[rhport]; |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 305 | |
Bernard Blackham | 3d0a2a2 | 2012-10-22 06:45:00 +1100 | [diff] [blame] | 306 | /* Extract socket from fd. */ |
Al Viro | 964ea96 | 2014-03-05 20:33:08 -0500 | [diff] [blame] | 307 | socket = sockfd_lookup(sockfd, &err); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 308 | if (!socket) |
Márton Németh | a6d81814a | 2011-05-27 06:18:48 +0200 | [diff] [blame] | 309 | return -EINVAL; |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 310 | |
| 311 | /* now need lock until setting vdev status as used */ |
| 312 | |
| 313 | /* begin a lock */ |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 314 | spin_lock_irqsave(&vhci->lock, flags); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 315 | spin_lock(&vdev->ud.lock); |
| 316 | |
| 317 | if (vdev->ud.status != VDEV_ST_NULL) { |
| 318 | /* end of the lock */ |
| 319 | spin_unlock(&vdev->ud.lock); |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 320 | spin_unlock_irqrestore(&vhci->lock, flags); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 321 | |
Al Viro | 964ea96 | 2014-03-05 20:33:08 -0500 | [diff] [blame] | 322 | sockfd_put(socket); |
Bernard Blackham | 3d0a2a2 | 2012-10-22 06:45:00 +1100 | [diff] [blame] | 323 | |
matt mooney | 1a4b6f6 | 2011-05-19 16:47:32 -0700 | [diff] [blame] | 324 | dev_err(dev, "port %d already used\n", rhport); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 325 | return -EINVAL; |
| 326 | } |
| 327 | |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 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)); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 332 | |
| 333 | vdev->devid = devid; |
| 334 | vdev->speed = speed; |
| 335 | vdev->ud.tcp_socket = socket; |
| 336 | vdev->ud.status = VDEV_ST_NOTASSIGNED; |
| 337 | |
| 338 | spin_unlock(&vdev->ud.lock); |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 339 | spin_unlock_irqrestore(&vhci->lock, flags); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 340 | /* end the lock */ |
| 341 | |
Oleg Nesterov | ba46ce3 | 2012-03-13 19:07:18 +0100 | [diff] [blame] | 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"); |
Max Vozeler | d1b2e95 | 2011-04-18 21:44:10 +0200 | [diff] [blame] | 344 | |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 345 | rh_port_connect(vdev, speed); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 346 | |
| 347 | return count; |
| 348 | } |
| 349 | static DEVICE_ATTR(attach, S_IWUSR, NULL, store_attach); |
| 350 | |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 351 | #define MAX_STATUS_NAME 16 |
| 352 | |
| 353 | struct status_attr { |
| 354 | struct device_attribute attr; |
| 355 | char name[MAX_STATUS_NAME+1]; |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 356 | }; |
| 357 | |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 358 | static struct status_attr *status_attrs; |
| 359 | |
| 360 | static void set_status_attr(int id) |
| 361 | { |
| 362 | struct status_attr *status; |
| 363 | |
| 364 | status = status_attrs + id; |
| 365 | if (id == 0) |
| 366 | strcpy(status->name, "status"); |
| 367 | else |
| 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; |
Shuah Khan | 918b8ac | 2016-12-05 12:56:38 -0700 | [diff] [blame] | 372 | sysfs_attr_init(&status->attr.attr); |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | static int init_status_attrs(void) |
| 376 | { |
| 377 | int id; |
| 378 | |
| 379 | status_attrs = kcalloc(vhci_num_controllers, sizeof(struct status_attr), |
| 380 | GFP_KERNEL); |
| 381 | if (status_attrs == NULL) |
| 382 | return -ENOMEM; |
| 383 | |
| 384 | for (id = 0; id < vhci_num_controllers; id++) |
| 385 | set_status_attr(id); |
| 386 | |
| 387 | return 0; |
| 388 | } |
| 389 | |
| 390 | static void finish_status_attrs(void) |
| 391 | { |
| 392 | kfree(status_attrs); |
| 393 | } |
| 394 | |
| 395 | struct attribute_group vhci_attr_group = { |
| 396 | .attrs = NULL, |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 397 | }; |
Nobuo Iwata | 0775a9c | 2016-06-13 11:33:40 +0900 | [diff] [blame] | 398 | |
| 399 | int vhci_init_attr_group(void) |
| 400 | { |
| 401 | struct attribute **attrs; |
| 402 | int ret, i; |
| 403 | |
| 404 | attrs = kcalloc((vhci_num_controllers + 5), sizeof(struct attribute *), |
| 405 | GFP_KERNEL); |
| 406 | if (attrs == NULL) |
| 407 | return -ENOMEM; |
| 408 | |
| 409 | ret = init_status_attrs(); |
| 410 | if (ret) { |
| 411 | kfree(attrs); |
| 412 | return ret; |
| 413 | } |
| 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; |
| 421 | return 0; |
| 422 | } |
| 423 | |
| 424 | void vhci_finish_attr_group(void) |
| 425 | { |
| 426 | finish_status_attrs(); |
| 427 | kfree(vhci_attr_group.attrs); |
| 428 | } |