Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2003-2008 Takahiro Hirofuchi |
| 3 | * |
| 4 | * This is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
| 17 | * USA. |
| 18 | */ |
| 19 | |
| 20 | #include "usbip_common.h" |
| 21 | #include "vhci.h" |
| 22 | |
| 23 | #include <linux/in.h> |
Max Vozeler | d1b2e95 | 2011-04-18 21:44:10 +0200 | [diff] [blame^] | 24 | #include <linux/kthread.h> |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 25 | |
| 26 | /* TODO: refine locking ?*/ |
| 27 | |
| 28 | /* Sysfs entry to show port status */ |
| 29 | static ssize_t show_status(struct device *dev, struct device_attribute *attr, |
| 30 | char *out) |
| 31 | { |
| 32 | char *s = out; |
| 33 | int i = 0; |
| 34 | |
Stoyan Gaydarov | 2961f24 | 2009-03-10 00:10:27 -0500 | [diff] [blame] | 35 | BUG_ON(!the_controller || !out); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 36 | |
| 37 | spin_lock(&the_controller->lock); |
| 38 | |
| 39 | /* |
| 40 | * output example: |
| 41 | * prt sta spd dev socket local_busid |
| 42 | * 000 004 000 000 c5a7bb80 1-2.3 |
| 43 | * 001 004 000 000 d8cee980 2-3.4 |
| 44 | * |
| 45 | * IP address can be retrieved from a socket pointer address by looking |
| 46 | * up /proc/net/{tcp,tcp6}. Also, a userland program may remember a |
| 47 | * port number and its peer IP address. |
| 48 | */ |
| 49 | out += sprintf(out, "prt sta spd bus dev socket " |
| 50 | "local_busid\n"); |
| 51 | |
| 52 | for (i = 0; i < VHCI_NPORTS; i++) { |
| 53 | struct vhci_device *vdev = port_to_vdev(i); |
| 54 | |
| 55 | spin_lock(&vdev->ud.lock); |
| 56 | |
| 57 | out += sprintf(out, "%03u %03u ", i, vdev->ud.status); |
| 58 | |
| 59 | if (vdev->ud.status == VDEV_ST_USED) { |
| 60 | out += sprintf(out, "%03u %08x ", |
| 61 | vdev->speed, vdev->devid); |
| 62 | out += sprintf(out, "%16p ", vdev->ud.tcp_socket); |
Kay Sievers | e913397 | 2008-10-30 01:59:32 +0100 | [diff] [blame] | 63 | out += sprintf(out, "%s", dev_name(&vdev->udev->dev)); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 64 | |
| 65 | } else |
| 66 | out += sprintf(out, "000 000 000 0000000000000000 0-0"); |
| 67 | |
| 68 | out += sprintf(out, "\n"); |
| 69 | |
| 70 | spin_unlock(&vdev->ud.lock); |
| 71 | } |
| 72 | |
| 73 | spin_unlock(&the_controller->lock); |
| 74 | |
| 75 | return out - s; |
| 76 | } |
| 77 | static DEVICE_ATTR(status, S_IRUGO, show_status, NULL); |
| 78 | |
| 79 | /* Sysfs entry to shutdown a virtual connection */ |
| 80 | static int vhci_port_disconnect(__u32 rhport) |
| 81 | { |
| 82 | struct vhci_device *vdev; |
| 83 | |
Brian G. Merrell | b8868e4 | 2009-07-21 00:46:13 -0600 | [diff] [blame] | 84 | usbip_dbg_vhci_sysfs("enter\n"); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 85 | |
| 86 | /* lock */ |
| 87 | spin_lock(&the_controller->lock); |
| 88 | |
| 89 | vdev = port_to_vdev(rhport); |
| 90 | |
| 91 | spin_lock(&vdev->ud.lock); |
| 92 | if (vdev->ud.status == VDEV_ST_NULL) { |
Brian G. Merrell | b8868e4 | 2009-07-21 00:46:13 -0600 | [diff] [blame] | 93 | usbip_uerr("not connected %d\n", vdev->ud.status); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 94 | |
| 95 | /* unlock */ |
| 96 | spin_unlock(&vdev->ud.lock); |
| 97 | spin_unlock(&the_controller->lock); |
| 98 | |
| 99 | return -EINVAL; |
| 100 | } |
| 101 | |
| 102 | /* unlock */ |
| 103 | spin_unlock(&vdev->ud.lock); |
| 104 | spin_unlock(&the_controller->lock); |
| 105 | |
| 106 | usbip_event_add(&vdev->ud, VDEV_EVENT_DOWN); |
| 107 | |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | static ssize_t store_detach(struct device *dev, struct device_attribute *attr, |
| 112 | const char *buf, size_t count) |
| 113 | { |
| 114 | int err; |
| 115 | __u32 rhport = 0; |
| 116 | |
| 117 | sscanf(buf, "%u", &rhport); |
| 118 | |
| 119 | /* check rhport */ |
| 120 | if (rhport >= VHCI_NPORTS) { |
Brian G. Merrell | b8868e4 | 2009-07-21 00:46:13 -0600 | [diff] [blame] | 121 | usbip_uerr("invalid port %u\n", rhport); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 122 | return -EINVAL; |
| 123 | } |
| 124 | |
| 125 | err = vhci_port_disconnect(rhport); |
| 126 | if (err < 0) |
| 127 | return -EINVAL; |
| 128 | |
Brian G. Merrell | b8868e4 | 2009-07-21 00:46:13 -0600 | [diff] [blame] | 129 | usbip_dbg_vhci_sysfs("Leave\n"); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 130 | return count; |
| 131 | } |
| 132 | static DEVICE_ATTR(detach, S_IWUSR, NULL, store_detach); |
| 133 | |
| 134 | /* Sysfs entry to establish a virtual connection */ |
| 135 | static int valid_args(__u32 rhport, enum usb_device_speed speed) |
| 136 | { |
| 137 | /* check rhport */ |
| 138 | if ((rhport < 0) || (rhport >= VHCI_NPORTS)) { |
Brian G. Merrell | b8868e4 | 2009-07-21 00:46:13 -0600 | [diff] [blame] | 139 | usbip_uerr("port %u\n", rhport); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 140 | return -EINVAL; |
| 141 | } |
| 142 | |
| 143 | /* check speed */ |
| 144 | switch (speed) { |
| 145 | case USB_SPEED_LOW: |
| 146 | case USB_SPEED_FULL: |
| 147 | case USB_SPEED_HIGH: |
Greg Kroah-Hartman | 551cdbb | 2010-01-14 11:08:04 -0800 | [diff] [blame] | 148 | case USB_SPEED_WIRELESS: |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 149 | break; |
| 150 | default: |
Brian G. Merrell | b8868e4 | 2009-07-21 00:46:13 -0600 | [diff] [blame] | 151 | usbip_uerr("speed %d\n", speed); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 152 | return -EINVAL; |
| 153 | } |
| 154 | |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | /* |
| 159 | * To start a new USB/IP attachment, a userland program needs to setup a TCP |
| 160 | * connection and then write its socket descriptor with remote device |
| 161 | * information into this sysfs file. |
| 162 | * |
| 163 | * A remote device is virtually attached to the root-hub port of @rhport with |
| 164 | * @speed. @devid is embedded into a request to specify the remote device in a |
| 165 | * server host. |
| 166 | * |
| 167 | * write() returns 0 on success, else negative errno. |
| 168 | */ |
| 169 | static ssize_t store_attach(struct device *dev, struct device_attribute *attr, |
| 170 | const char *buf, size_t count) |
| 171 | { |
| 172 | struct vhci_device *vdev; |
| 173 | struct socket *socket; |
| 174 | int sockfd = 0; |
| 175 | __u32 rhport = 0, devid = 0, speed = 0; |
| 176 | |
| 177 | /* |
| 178 | * @rhport: port number of vhci_hcd |
| 179 | * @sockfd: socket descriptor of an established TCP connection |
| 180 | * @devid: unique device identifier in a remote host |
| 181 | * @speed: usb device speed in a remote host |
| 182 | */ |
| 183 | sscanf(buf, "%u %u %u %u", &rhport, &sockfd, &devid, &speed); |
| 184 | |
Brian G. Merrell | b8868e4 | 2009-07-21 00:46:13 -0600 | [diff] [blame] | 185 | usbip_dbg_vhci_sysfs("rhport(%u) sockfd(%u) devid(%u) speed(%u)\n", |
| 186 | rhport, sockfd, devid, speed); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 187 | |
| 188 | |
| 189 | /* check received parameters */ |
| 190 | if (valid_args(rhport, speed) < 0) |
| 191 | return -EINVAL; |
| 192 | |
| 193 | /* check sockfd */ |
| 194 | socket = sockfd_to_socket(sockfd); |
| 195 | if (!socket) |
| 196 | return -EINVAL; |
| 197 | |
| 198 | /* now need lock until setting vdev status as used */ |
| 199 | |
| 200 | /* begin a lock */ |
| 201 | spin_lock(&the_controller->lock); |
| 202 | |
| 203 | vdev = port_to_vdev(rhport); |
| 204 | |
| 205 | spin_lock(&vdev->ud.lock); |
| 206 | |
| 207 | if (vdev->ud.status != VDEV_ST_NULL) { |
| 208 | /* end of the lock */ |
| 209 | spin_unlock(&vdev->ud.lock); |
| 210 | spin_unlock(&the_controller->lock); |
| 211 | |
Brian G. Merrell | b8868e4 | 2009-07-21 00:46:13 -0600 | [diff] [blame] | 212 | usbip_uerr("port %d already used\n", rhport); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 213 | return -EINVAL; |
| 214 | } |
| 215 | |
Brian G. Merrell | b8868e4 | 2009-07-21 00:46:13 -0600 | [diff] [blame] | 216 | usbip_uinfo("rhport(%u) sockfd(%d) devid(%u) speed(%u)\n", |
| 217 | rhport, sockfd, devid, speed); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 218 | |
| 219 | vdev->devid = devid; |
| 220 | vdev->speed = speed; |
| 221 | vdev->ud.tcp_socket = socket; |
| 222 | vdev->ud.status = VDEV_ST_NOTASSIGNED; |
| 223 | |
| 224 | spin_unlock(&vdev->ud.lock); |
| 225 | spin_unlock(&the_controller->lock); |
| 226 | /* end the lock */ |
| 227 | |
Max Vozeler | d1b2e95 | 2011-04-18 21:44:10 +0200 | [diff] [blame^] | 228 | vdev->ud.tcp_rx = kthread_run(vhci_rx_loop, &vdev->ud, "vhci_rx"); |
| 229 | vdev->ud.tcp_tx = kthread_run(vhci_tx_loop, &vdev->ud, "vhci_tx"); |
| 230 | |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 231 | rh_port_connect(rhport, speed); |
| 232 | |
| 233 | return count; |
| 234 | } |
| 235 | static DEVICE_ATTR(attach, S_IWUSR, NULL, store_attach); |
| 236 | |
| 237 | static struct attribute *dev_attrs[] = { |
| 238 | &dev_attr_status.attr, |
| 239 | &dev_attr_detach.attr, |
| 240 | &dev_attr_attach.attr, |
| 241 | &dev_attr_usbip_debug.attr, |
| 242 | NULL, |
| 243 | }; |
| 244 | |
| 245 | struct attribute_group dev_attr_group = { |
| 246 | .attrs = dev_attrs, |
| 247 | }; |