Takahiro Hirofuchi | 4d7b5c7 | 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 | |
matt mooney | 7aaacb4 | 2011-05-11 22:33:43 -0700 | [diff] [blame] | 20 | #include <linux/device.h> |
Bernard Blackham | 3d0a2a2 | 2012-10-22 06:45:00 +1100 | [diff] [blame] | 21 | #include <linux/file.h> |
Arnd Bergmann | 9720b4b | 2011-03-02 00:13:05 +0100 | [diff] [blame] | 22 | #include <linux/kthread.h> |
Paul Gortmaker | 99c9785 | 2011-07-03 15:49:50 -0400 | [diff] [blame] | 23 | #include <linux/module.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 24 | |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 25 | #include "usbip_common.h" |
| 26 | #include "stub.h" |
| 27 | |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 28 | /* |
matt mooney | d012c2a5 | 2011-05-19 21:37:03 -0700 | [diff] [blame] | 29 | * usbip_status shows the status of usbip-host as long as this driver is bound |
| 30 | * to the target device. |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 31 | */ |
Greg Kroah-Hartman | b1f56ac | 2013-08-26 12:02:54 -0700 | [diff] [blame] | 32 | static ssize_t usbip_status_show(struct device *dev, |
| 33 | struct device_attribute *attr, char *buf) |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 34 | { |
| 35 | struct stub_device *sdev = dev_get_drvdata(dev); |
| 36 | int status; |
| 37 | |
| 38 | if (!sdev) { |
| 39 | dev_err(dev, "sdev is null\n"); |
| 40 | return -ENODEV; |
| 41 | } |
| 42 | |
Harvey Yang | dcf14779 | 2013-01-22 13:31:30 +0800 | [diff] [blame] | 43 | spin_lock_irq(&sdev->ud.lock); |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 44 | status = sdev->ud.status; |
Harvey Yang | dcf14779 | 2013-01-22 13:31:30 +0800 | [diff] [blame] | 45 | spin_unlock_irq(&sdev->ud.lock); |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 46 | |
| 47 | return snprintf(buf, PAGE_SIZE, "%d\n", status); |
| 48 | } |
Greg Kroah-Hartman | b1f56ac | 2013-08-26 12:02:54 -0700 | [diff] [blame] | 49 | static DEVICE_ATTR_RO(usbip_status); |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 50 | |
| 51 | /* |
| 52 | * usbip_sockfd gets a socket descriptor of an established TCP connection that |
| 53 | * is used to transfer usbip requests by kernel threads. -1 is a magic number |
| 54 | * by which usbip connection is finished. |
| 55 | */ |
| 56 | static ssize_t store_sockfd(struct device *dev, struct device_attribute *attr, |
| 57 | const char *buf, size_t count) |
| 58 | { |
| 59 | struct stub_device *sdev = dev_get_drvdata(dev); |
| 60 | int sockfd = 0; |
| 61 | struct socket *socket; |
Elena Oat | f8cfc02 | 2014-02-27 12:26:52 +0200 | [diff] [blame] | 62 | int rv; |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 63 | |
| 64 | if (!sdev) { |
| 65 | dev_err(dev, "sdev is null\n"); |
| 66 | return -ENODEV; |
| 67 | } |
| 68 | |
Elena Oat | f8cfc02 | 2014-02-27 12:26:52 +0200 | [diff] [blame] | 69 | rv = sscanf(buf, "%d", &sockfd); |
| 70 | if (rv != 1) |
| 71 | return -EINVAL; |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 72 | |
| 73 | if (sockfd != -1) { |
Al Viro | 964ea96 | 2014-03-05 20:33:08 -0500 | [diff] [blame] | 74 | int err; |
Pawel Lebioda | 3eed8c0 | 2014-05-14 19:20:27 +0200 | [diff] [blame] | 75 | |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 76 | dev_info(dev, "stub up\n"); |
| 77 | |
Harvey Yang | dcf14779 | 2013-01-22 13:31:30 +0800 | [diff] [blame] | 78 | spin_lock_irq(&sdev->ud.lock); |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 79 | |
| 80 | if (sdev->ud.status != SDEV_ST_AVAILABLE) { |
| 81 | dev_err(dev, "not ready\n"); |
Kurt Kanzenbach | 31398f6 | 2013-04-04 16:03:08 +0200 | [diff] [blame] | 82 | goto err; |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 83 | } |
| 84 | |
Al Viro | 964ea96 | 2014-03-05 20:33:08 -0500 | [diff] [blame] | 85 | socket = sockfd_lookup(sockfd, &err); |
Kurt Kanzenbach | 31398f6 | 2013-04-04 16:03:08 +0200 | [diff] [blame] | 86 | if (!socket) |
| 87 | goto err; |
| 88 | |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 89 | sdev->ud.tcp_socket = socket; |
| 90 | |
Harvey Yang | dcf14779 | 2013-01-22 13:31:30 +0800 | [diff] [blame] | 91 | spin_unlock_irq(&sdev->ud.lock); |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 92 | |
Kurt Kanzenbach | 8c4e583 | 2013-02-22 12:13:34 +0100 | [diff] [blame] | 93 | sdev->ud.tcp_rx = kthread_get_run(stub_rx_loop, &sdev->ud, |
| 94 | "stub_rx"); |
| 95 | sdev->ud.tcp_tx = kthread_get_run(stub_tx_loop, &sdev->ud, |
| 96 | "stub_tx"); |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 97 | |
Harvey Yang | dcf14779 | 2013-01-22 13:31:30 +0800 | [diff] [blame] | 98 | spin_lock_irq(&sdev->ud.lock); |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 99 | sdev->ud.status = SDEV_ST_USED; |
Harvey Yang | dcf14779 | 2013-01-22 13:31:30 +0800 | [diff] [blame] | 100 | spin_unlock_irq(&sdev->ud.lock); |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 101 | |
| 102 | } else { |
| 103 | dev_info(dev, "stub down\n"); |
| 104 | |
Harvey Yang | dcf14779 | 2013-01-22 13:31:30 +0800 | [diff] [blame] | 105 | spin_lock_irq(&sdev->ud.lock); |
Kurt Kanzenbach | 31398f6 | 2013-04-04 16:03:08 +0200 | [diff] [blame] | 106 | if (sdev->ud.status != SDEV_ST_USED) |
| 107 | goto err; |
| 108 | |
Harvey Yang | dcf14779 | 2013-01-22 13:31:30 +0800 | [diff] [blame] | 109 | spin_unlock_irq(&sdev->ud.lock); |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 110 | |
| 111 | usbip_event_add(&sdev->ud, SDEV_EVENT_DOWN); |
| 112 | } |
| 113 | |
| 114 | return count; |
Kurt Kanzenbach | 31398f6 | 2013-04-04 16:03:08 +0200 | [diff] [blame] | 115 | |
| 116 | err: |
| 117 | spin_unlock_irq(&sdev->ud.lock); |
Al Viro | 964ea96 | 2014-03-05 20:33:08 -0500 | [diff] [blame] | 118 | return -EINVAL; |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 119 | } |
| 120 | static DEVICE_ATTR(usbip_sockfd, S_IWUSR, NULL, store_sockfd); |
| 121 | |
| 122 | static int stub_add_files(struct device *dev) |
| 123 | { |
| 124 | int err = 0; |
| 125 | |
| 126 | err = device_create_file(dev, &dev_attr_usbip_status); |
| 127 | if (err) |
| 128 | goto err_status; |
| 129 | |
| 130 | err = device_create_file(dev, &dev_attr_usbip_sockfd); |
| 131 | if (err) |
| 132 | goto err_sockfd; |
| 133 | |
| 134 | err = device_create_file(dev, &dev_attr_usbip_debug); |
| 135 | if (err) |
| 136 | goto err_debug; |
| 137 | |
| 138 | return 0; |
| 139 | |
| 140 | err_debug: |
| 141 | device_remove_file(dev, &dev_attr_usbip_sockfd); |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 142 | err_sockfd: |
| 143 | device_remove_file(dev, &dev_attr_usbip_status); |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 144 | err_status: |
| 145 | return err; |
| 146 | } |
| 147 | |
| 148 | static void stub_remove_files(struct device *dev) |
| 149 | { |
| 150 | device_remove_file(dev, &dev_attr_usbip_status); |
| 151 | device_remove_file(dev, &dev_attr_usbip_sockfd); |
| 152 | device_remove_file(dev, &dev_attr_usbip_debug); |
| 153 | } |
| 154 | |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 155 | static void stub_shutdown_connection(struct usbip_device *ud) |
| 156 | { |
| 157 | struct stub_device *sdev = container_of(ud, struct stub_device, ud); |
| 158 | |
| 159 | /* |
| 160 | * When removing an exported device, kernel panic sometimes occurred |
| 161 | * and then EIP was sk_wait_data of stub_rx thread. Is this because |
| 162 | * sk_wait_data returned though stub_rx thread was already finished by |
| 163 | * step 1? |
| 164 | */ |
| 165 | if (ud->tcp_socket) { |
matt mooney | 1a4b6f6 | 2011-05-19 16:47:32 -0700 | [diff] [blame] | 166 | dev_dbg(&sdev->udev->dev, "shutdown tcp_socket %p\n", |
| 167 | ud->tcp_socket); |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 168 | kernel_sock_shutdown(ud->tcp_socket, SHUT_RDWR); |
| 169 | } |
| 170 | |
| 171 | /* 1. stop threads */ |
navin | b7caecb | 2012-09-19 17:07:27 +0530 | [diff] [blame] | 172 | if (ud->tcp_rx) { |
Oleg Nesterov | ba46ce3 | 2012-03-13 19:07:18 +0100 | [diff] [blame] | 173 | kthread_stop_put(ud->tcp_rx); |
navin | b7caecb | 2012-09-19 17:07:27 +0530 | [diff] [blame] | 174 | ud->tcp_rx = NULL; |
| 175 | } |
| 176 | if (ud->tcp_tx) { |
Oleg Nesterov | ba46ce3 | 2012-03-13 19:07:18 +0100 | [diff] [blame] | 177 | kthread_stop_put(ud->tcp_tx); |
navin | b7caecb | 2012-09-19 17:07:27 +0530 | [diff] [blame] | 178 | ud->tcp_tx = NULL; |
| 179 | } |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 180 | |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 181 | /* |
matt mooney | 8735276 | 2011-05-19 21:36:56 -0700 | [diff] [blame] | 182 | * 2. close the socket |
| 183 | * |
| 184 | * tcp_socket is freed after threads are killed so that usbip_xmit does |
| 185 | * not touch NULL socket. |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 186 | */ |
| 187 | if (ud->tcp_socket) { |
Al Viro | 964ea96 | 2014-03-05 20:33:08 -0500 | [diff] [blame] | 188 | sockfd_put(ud->tcp_socket); |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 189 | ud->tcp_socket = NULL; |
| 190 | } |
| 191 | |
| 192 | /* 3. free used data */ |
| 193 | stub_device_cleanup_urbs(sdev); |
| 194 | |
| 195 | /* 4. free stub_unlink */ |
| 196 | { |
| 197 | unsigned long flags; |
| 198 | struct stub_unlink *unlink, *tmp; |
| 199 | |
| 200 | spin_lock_irqsave(&sdev->priv_lock, flags); |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 201 | list_for_each_entry_safe(unlink, tmp, &sdev->unlink_tx, list) { |
| 202 | list_del(&unlink->list); |
| 203 | kfree(unlink); |
| 204 | } |
matt mooney | 8735276 | 2011-05-19 21:36:56 -0700 | [diff] [blame] | 205 | list_for_each_entry_safe(unlink, tmp, &sdev->unlink_free, |
| 206 | list) { |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 207 | list_del(&unlink->list); |
| 208 | kfree(unlink); |
| 209 | } |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 210 | spin_unlock_irqrestore(&sdev->priv_lock, flags); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | static void stub_device_reset(struct usbip_device *ud) |
| 215 | { |
| 216 | struct stub_device *sdev = container_of(ud, struct stub_device, ud); |
Max Vozeler | 2d8f459 | 2011-01-12 15:01:59 +0200 | [diff] [blame] | 217 | struct usb_device *udev = sdev->udev; |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 218 | int ret; |
| 219 | |
matt mooney | 1a4b6f6 | 2011-05-19 16:47:32 -0700 | [diff] [blame] | 220 | dev_dbg(&udev->dev, "device reset"); |
Max Vozeler | 2d8f459 | 2011-01-12 15:01:59 +0200 | [diff] [blame] | 221 | |
Alexander Popov | 8c7003a3 | 2016-04-28 13:07:22 +0300 | [diff] [blame] | 222 | ret = usb_lock_device_for_reset(udev, NULL); |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 223 | if (ret < 0) { |
| 224 | dev_err(&udev->dev, "lock for reset\n"); |
Harvey Yang | dcf14779 | 2013-01-22 13:31:30 +0800 | [diff] [blame] | 225 | spin_lock_irq(&ud->lock); |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 226 | ud->status = SDEV_ST_ERROR; |
Harvey Yang | dcf14779 | 2013-01-22 13:31:30 +0800 | [diff] [blame] | 227 | spin_unlock_irq(&ud->lock); |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 228 | return; |
| 229 | } |
| 230 | |
| 231 | /* try to reset the device */ |
| 232 | ret = usb_reset_device(udev); |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 233 | usb_unlock_device(udev); |
| 234 | |
Harvey Yang | dcf14779 | 2013-01-22 13:31:30 +0800 | [diff] [blame] | 235 | spin_lock_irq(&ud->lock); |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 236 | if (ret) { |
| 237 | dev_err(&udev->dev, "device reset\n"); |
| 238 | ud->status = SDEV_ST_ERROR; |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 239 | } else { |
| 240 | dev_info(&udev->dev, "device reset\n"); |
| 241 | ud->status = SDEV_ST_AVAILABLE; |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 242 | } |
Harvey Yang | dcf14779 | 2013-01-22 13:31:30 +0800 | [diff] [blame] | 243 | spin_unlock_irq(&ud->lock); |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | static void stub_device_unusable(struct usbip_device *ud) |
| 247 | { |
Harvey Yang | dcf14779 | 2013-01-22 13:31:30 +0800 | [diff] [blame] | 248 | spin_lock_irq(&ud->lock); |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 249 | ud->status = SDEV_ST_ERROR; |
Harvey Yang | dcf14779 | 2013-01-22 13:31:30 +0800 | [diff] [blame] | 250 | spin_unlock_irq(&ud->lock); |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 251 | } |
| 252 | |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 253 | /** |
| 254 | * stub_device_alloc - allocate a new stub_device struct |
Alexander Popov | 8c7003a3 | 2016-04-28 13:07:22 +0300 | [diff] [blame] | 255 | * @udev: usb_device of a new device |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 256 | * |
| 257 | * Allocates and initializes a new stub_device struct. |
| 258 | */ |
Valentina Manea | b7945b7 | 2014-01-23 23:12:29 +0200 | [diff] [blame] | 259 | static struct stub_device *stub_device_alloc(struct usb_device *udev) |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 260 | { |
| 261 | struct stub_device *sdev; |
Valentina Manea | b7945b7 | 2014-01-23 23:12:29 +0200 | [diff] [blame] | 262 | int busnum = udev->bus->busnum; |
| 263 | int devnum = udev->devnum; |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 264 | |
Valentina Manea | b7945b7 | 2014-01-23 23:12:29 +0200 | [diff] [blame] | 265 | dev_dbg(&udev->dev, "allocating stub device"); |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 266 | |
| 267 | /* yes, it's a new device */ |
| 268 | sdev = kzalloc(sizeof(struct stub_device), GFP_KERNEL); |
Joe Perches | 78110bb | 2013-02-11 09:41:29 -0800 | [diff] [blame] | 269 | if (!sdev) |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 270 | return NULL; |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 271 | |
Max Vozeler | 2d8f459 | 2011-01-12 15:01:59 +0200 | [diff] [blame] | 272 | sdev->udev = usb_get_dev(udev); |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 273 | |
| 274 | /* |
| 275 | * devid is defined with devnum when this driver is first allocated. |
| 276 | * devnum may change later if a device is reset. However, devid never |
| 277 | * changes during a usbip connection. |
| 278 | */ |
matt mooney | b744a45 | 2011-05-06 03:47:41 -0700 | [diff] [blame] | 279 | sdev->devid = (busnum << 16) | devnum; |
| 280 | sdev->ud.side = USBIP_STUB; |
| 281 | sdev->ud.status = SDEV_ST_AVAILABLE; |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 282 | spin_lock_init(&sdev->ud.lock); |
matt mooney | b744a45 | 2011-05-06 03:47:41 -0700 | [diff] [blame] | 283 | sdev->ud.tcp_socket = NULL; |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 284 | |
| 285 | INIT_LIST_HEAD(&sdev->priv_init); |
| 286 | INIT_LIST_HEAD(&sdev->priv_tx); |
| 287 | INIT_LIST_HEAD(&sdev->priv_free); |
| 288 | INIT_LIST_HEAD(&sdev->unlink_free); |
| 289 | INIT_LIST_HEAD(&sdev->unlink_tx); |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 290 | spin_lock_init(&sdev->priv_lock); |
| 291 | |
| 292 | init_waitqueue_head(&sdev->tx_waitq); |
| 293 | |
| 294 | sdev->ud.eh_ops.shutdown = stub_shutdown_connection; |
| 295 | sdev->ud.eh_ops.reset = stub_device_reset; |
| 296 | sdev->ud.eh_ops.unusable = stub_device_unusable; |
| 297 | |
| 298 | usbip_start_eh(&sdev->ud); |
| 299 | |
Valentina Manea | b7945b7 | 2014-01-23 23:12:29 +0200 | [diff] [blame] | 300 | dev_dbg(&udev->dev, "register new device\n"); |
matt mooney | 1a4b6f6 | 2011-05-19 16:47:32 -0700 | [diff] [blame] | 301 | |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 302 | return sdev; |
| 303 | } |
| 304 | |
Kurt Kanzenbach | b94b3a6 | 2013-04-04 16:03:11 +0200 | [diff] [blame] | 305 | static void stub_device_free(struct stub_device *sdev) |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 306 | { |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 307 | kfree(sdev); |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 308 | } |
| 309 | |
Valentina Manea | b7945b7 | 2014-01-23 23:12:29 +0200 | [diff] [blame] | 310 | static int stub_probe(struct usb_device *udev) |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 311 | { |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 312 | struct stub_device *sdev = NULL; |
Valentina Manea | b7945b7 | 2014-01-23 23:12:29 +0200 | [diff] [blame] | 313 | const char *udev_busid = dev_name(&udev->dev); |
Endre Kollar | aa5873e | 2010-07-27 12:39:30 +0200 | [diff] [blame] | 314 | struct bus_id_priv *busid_priv; |
Valentina Manea | 6080cd0 | 2014-03-08 14:53:34 +0200 | [diff] [blame] | 315 | int rc; |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 316 | |
Valentina Manea | b7945b7 | 2014-01-23 23:12:29 +0200 | [diff] [blame] | 317 | dev_dbg(&udev->dev, "Enter\n"); |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 318 | |
| 319 | /* check we should claim or not by busid_table */ |
Endre Kollar | aa5873e | 2010-07-27 12:39:30 +0200 | [diff] [blame] | 320 | busid_priv = get_busid_priv(udev_busid); |
matt mooney | 8735276 | 2011-05-19 21:36:56 -0700 | [diff] [blame] | 321 | if (!busid_priv || (busid_priv->status == STUB_BUSID_REMOV) || |
matt mooney | b744a45 | 2011-05-06 03:47:41 -0700 | [diff] [blame] | 322 | (busid_priv->status == STUB_BUSID_OTHER)) { |
Valentina Manea | b7945b7 | 2014-01-23 23:12:29 +0200 | [diff] [blame] | 323 | dev_info(&udev->dev, |
Elad Wexler | 6165cc5 | 2013-09-05 12:07:10 +0300 | [diff] [blame] | 324 | "%s is not in match_busid table... skip!\n", |
| 325 | udev_busid); |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 326 | |
| 327 | /* |
| 328 | * Return value should be ENODEV or ENOXIO to continue trying |
| 329 | * other matched drivers by the driver core. |
| 330 | * See driver_probe_device() in driver/base/dd.c |
| 331 | */ |
| 332 | return -ENODEV; |
| 333 | } |
| 334 | |
matt mooney | 1a4b6f6 | 2011-05-19 16:47:32 -0700 | [diff] [blame] | 335 | if (udev->descriptor.bDeviceClass == USB_CLASS_HUB) { |
| 336 | dev_dbg(&udev->dev, "%s is a usb hub device... skip!\n", |
| 337 | udev_busid); |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 338 | return -ENODEV; |
| 339 | } |
| 340 | |
| 341 | if (!strcmp(udev->bus->bus_name, "vhci_hcd")) { |
Elad Wexler | 6165cc5 | 2013-09-05 12:07:10 +0300 | [diff] [blame] | 342 | dev_dbg(&udev->dev, |
| 343 | "%s is attached on vhci_hcd... skip!\n", |
| 344 | udev_busid); |
| 345 | |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 346 | return -ENODEV; |
| 347 | } |
| 348 | |
matt mooney | d012c2a5 | 2011-05-19 21:37:03 -0700 | [diff] [blame] | 349 | /* ok, this is my device */ |
Valentina Manea | b7945b7 | 2014-01-23 23:12:29 +0200 | [diff] [blame] | 350 | sdev = stub_device_alloc(udev); |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 351 | if (!sdev) |
| 352 | return -ENOMEM; |
| 353 | |
Valentina Manea | b7945b7 | 2014-01-23 23:12:29 +0200 | [diff] [blame] | 354 | dev_info(&udev->dev, |
| 355 | "usbip-host: register new device (bus %u dev %u)\n", |
| 356 | udev->bus->busnum, udev->devnum); |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 357 | |
Endre Kollar | aa5873e | 2010-07-27 12:39:30 +0200 | [diff] [blame] | 358 | busid_priv->shutdown_busid = 0; |
| 359 | |
Valentina Manea | b7945b7 | 2014-01-23 23:12:29 +0200 | [diff] [blame] | 360 | /* set private data to usb_device */ |
| 361 | dev_set_drvdata(&udev->dev, sdev); |
Endre Kollar | aa5873e | 2010-07-27 12:39:30 +0200 | [diff] [blame] | 362 | busid_priv->sdev = sdev; |
Valentina Manea | a46034c | 2014-03-08 14:53:33 +0200 | [diff] [blame] | 363 | busid_priv->udev = udev; |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 364 | |
Valentina Manea | 6080cd0 | 2014-03-08 14:53:34 +0200 | [diff] [blame] | 365 | /* |
| 366 | * Claim this hub port. |
| 367 | * It doesn't matter what value we pass as owner |
| 368 | * (struct dev_state) as long as it is unique. |
| 369 | */ |
| 370 | rc = usb_hub_claim_port(udev->parent, udev->portnum, |
Valentina Manea | 9b6f0c4 | 2014-03-10 10:36:40 +0200 | [diff] [blame] | 371 | (struct usb_dev_state *) udev); |
Valentina Manea | 6080cd0 | 2014-03-08 14:53:34 +0200 | [diff] [blame] | 372 | if (rc) { |
| 373 | dev_dbg(&udev->dev, "unable to claim port\n"); |
Alexey Khoroshilov | 3ff6744 | 2014-11-29 01:29:10 +0300 | [diff] [blame] | 374 | goto err_port; |
Valentina Manea | 6080cd0 | 2014-03-08 14:53:34 +0200 | [diff] [blame] | 375 | } |
| 376 | |
Alexey Khoroshilov | 3ff6744 | 2014-11-29 01:29:10 +0300 | [diff] [blame] | 377 | rc = stub_add_files(&udev->dev); |
| 378 | if (rc) { |
Valentina Manea | b7945b7 | 2014-01-23 23:12:29 +0200 | [diff] [blame] | 379 | dev_err(&udev->dev, "stub_add_files for %s\n", udev_busid); |
Alexey Khoroshilov | 3ff6744 | 2014-11-29 01:29:10 +0300 | [diff] [blame] | 380 | goto err_files; |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 381 | } |
Endre Kollar | aa5873e | 2010-07-27 12:39:30 +0200 | [diff] [blame] | 382 | busid_priv->status = STUB_BUSID_ALLOC; |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 383 | |
| 384 | return 0; |
Alexey Khoroshilov | 3ff6744 | 2014-11-29 01:29:10 +0300 | [diff] [blame] | 385 | err_files: |
| 386 | usb_hub_release_port(udev->parent, udev->portnum, |
| 387 | (struct usb_dev_state *) udev); |
| 388 | err_port: |
| 389 | dev_set_drvdata(&udev->dev, NULL); |
| 390 | usb_put_dev(udev); |
Alexey Khoroshilov | 3ff6744 | 2014-11-29 01:29:10 +0300 | [diff] [blame] | 391 | |
| 392 | busid_priv->sdev = NULL; |
| 393 | stub_device_free(sdev); |
| 394 | return rc; |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 395 | } |
| 396 | |
Endre Kollar | aa5873e | 2010-07-27 12:39:30 +0200 | [diff] [blame] | 397 | static void shutdown_busid(struct bus_id_priv *busid_priv) |
| 398 | { |
| 399 | if (busid_priv->sdev && !busid_priv->shutdown_busid) { |
| 400 | busid_priv->shutdown_busid = 1; |
| 401 | usbip_event_add(&busid_priv->sdev->ud, SDEV_EVENT_REMOVED); |
| 402 | |
Kurt Kanzenbach | 771788074 | 2013-04-04 16:03:05 +0200 | [diff] [blame] | 403 | /* wait for the stop of the event handler */ |
Endre Kollar | aa5873e | 2010-07-27 12:39:30 +0200 | [diff] [blame] | 404 | usbip_stop_eh(&busid_priv->sdev->ud); |
| 405 | } |
Endre Kollar | aa5873e | 2010-07-27 12:39:30 +0200 | [diff] [blame] | 406 | } |
| 407 | |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 408 | /* |
| 409 | * called in usb_disconnect() or usb_deregister() |
| 410 | * but only if actconfig(active configuration) exists |
| 411 | */ |
Valentina Manea | b7945b7 | 2014-01-23 23:12:29 +0200 | [diff] [blame] | 412 | static void stub_disconnect(struct usb_device *udev) |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 413 | { |
Endre Kollar | aa5873e | 2010-07-27 12:39:30 +0200 | [diff] [blame] | 414 | struct stub_device *sdev; |
Valentina Manea | b7945b7 | 2014-01-23 23:12:29 +0200 | [diff] [blame] | 415 | const char *udev_busid = dev_name(&udev->dev); |
Endre Kollar | aa5873e | 2010-07-27 12:39:30 +0200 | [diff] [blame] | 416 | struct bus_id_priv *busid_priv; |
Valentina Manea | 6080cd0 | 2014-03-08 14:53:34 +0200 | [diff] [blame] | 417 | int rc; |
Endre Kollar | aa5873e | 2010-07-27 12:39:30 +0200 | [diff] [blame] | 418 | |
Valentina Manea | b7945b7 | 2014-01-23 23:12:29 +0200 | [diff] [blame] | 419 | dev_dbg(&udev->dev, "Enter\n"); |
matt mooney | 1a4b6f6 | 2011-05-19 16:47:32 -0700 | [diff] [blame] | 420 | |
Endre Kollar | aa5873e | 2010-07-27 12:39:30 +0200 | [diff] [blame] | 421 | busid_priv = get_busid_priv(udev_busid); |
Endre Kollar | aa5873e | 2010-07-27 12:39:30 +0200 | [diff] [blame] | 422 | if (!busid_priv) { |
| 423 | BUG(); |
| 424 | return; |
| 425 | } |
| 426 | |
Valentina Manea | b7945b7 | 2014-01-23 23:12:29 +0200 | [diff] [blame] | 427 | sdev = dev_get_drvdata(&udev->dev); |
Endre Kollar | aa5873e | 2010-07-27 12:39:30 +0200 | [diff] [blame] | 428 | |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 429 | /* get stub_device */ |
| 430 | if (!sdev) { |
Valentina Manea | b7945b7 | 2014-01-23 23:12:29 +0200 | [diff] [blame] | 431 | dev_err(&udev->dev, "could not get device"); |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 432 | return; |
| 433 | } |
| 434 | |
Valentina Manea | b7945b7 | 2014-01-23 23:12:29 +0200 | [diff] [blame] | 435 | dev_set_drvdata(&udev->dev, NULL); |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 436 | |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 437 | /* |
Bart Westgeest | c7f0089 | 2012-10-10 13:34:27 -0400 | [diff] [blame] | 438 | * NOTE: rx/tx threads are invoked for each usb_device. |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 439 | */ |
Valentina Manea | b7945b7 | 2014-01-23 23:12:29 +0200 | [diff] [blame] | 440 | stub_remove_files(&udev->dev); |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 441 | |
Valentina Manea | 6080cd0 | 2014-03-08 14:53:34 +0200 | [diff] [blame] | 442 | /* release port */ |
| 443 | rc = usb_hub_release_port(udev->parent, udev->portnum, |
Valentina Manea | 9b6f0c4 | 2014-03-10 10:36:40 +0200 | [diff] [blame] | 444 | (struct usb_dev_state *) udev); |
Valentina Manea | 6080cd0 | 2014-03-08 14:53:34 +0200 | [diff] [blame] | 445 | if (rc) { |
| 446 | dev_dbg(&udev->dev, "unable to release port\n"); |
| 447 | return; |
| 448 | } |
| 449 | |
Bart Westgeest | c7f0089 | 2012-10-10 13:34:27 -0400 | [diff] [blame] | 450 | /* If usb reset is called from event handler */ |
Nobuo Iwata | bb7871a | 2016-03-24 10:50:59 +0900 | [diff] [blame] | 451 | if (usbip_in_eh(current)) |
Endre Kollar | aa5873e | 2010-07-27 12:39:30 +0200 | [diff] [blame] | 452 | return; |
Endre Kollar | aa5873e | 2010-07-27 12:39:30 +0200 | [diff] [blame] | 453 | |
Bart Westgeest | c7f0089 | 2012-10-10 13:34:27 -0400 | [diff] [blame] | 454 | /* shutdown the current connection */ |
Endre Kollar | aa5873e | 2010-07-27 12:39:30 +0200 | [diff] [blame] | 455 | shutdown_busid(busid_priv); |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 456 | |
Max Vozeler | 2d8f459 | 2011-01-12 15:01:59 +0200 | [diff] [blame] | 457 | usb_put_dev(sdev->udev); |
Max Vozeler | 2d8f459 | 2011-01-12 15:01:59 +0200 | [diff] [blame] | 458 | |
Bart Westgeest | c7f0089 | 2012-10-10 13:34:27 -0400 | [diff] [blame] | 459 | /* free sdev */ |
Endre Kollar | aa5873e | 2010-07-27 12:39:30 +0200 | [diff] [blame] | 460 | busid_priv->sdev = NULL; |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 461 | stub_device_free(sdev); |
| 462 | |
Endre Kollar | aa5873e | 2010-07-27 12:39:30 +0200 | [diff] [blame] | 463 | if (busid_priv->status == STUB_BUSID_ALLOC) { |
| 464 | busid_priv->status = STUB_BUSID_ADDED; |
| 465 | } else { |
| 466 | busid_priv->status = STUB_BUSID_OTHER; |
| 467 | del_match_busid((char *)udev_busid); |
| 468 | } |
Takahiro Hirofuchi | 4d7b5c7 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 469 | } |
matt mooney | d012c2a5 | 2011-05-19 21:37:03 -0700 | [diff] [blame] | 470 | |
Valentina Manea | b7945b7 | 2014-01-23 23:12:29 +0200 | [diff] [blame] | 471 | #ifdef CONFIG_PM |
Akshay Joshi | 553a1a5 | 2011-08-17 15:58:31 -0400 | [diff] [blame] | 472 | |
Valentina Manea | b7945b7 | 2014-01-23 23:12:29 +0200 | [diff] [blame] | 473 | /* These functions need usb_port_suspend and usb_port_resume, |
| 474 | * which reside in drivers/usb/core/usb.h. Skip for now. */ |
| 475 | |
| 476 | static int stub_suspend(struct usb_device *udev, pm_message_t message) |
Arjan Mels | 1aee199 | 2011-06-30 22:18:18 +0200 | [diff] [blame] | 477 | { |
Valentina Manea | b7945b7 | 2014-01-23 23:12:29 +0200 | [diff] [blame] | 478 | dev_dbg(&udev->dev, "stub_suspend\n"); |
| 479 | |
Arjan Mels | 1aee199 | 2011-06-30 22:18:18 +0200 | [diff] [blame] | 480 | return 0; |
| 481 | } |
| 482 | |
Valentina Manea | b7945b7 | 2014-01-23 23:12:29 +0200 | [diff] [blame] | 483 | static int stub_resume(struct usb_device *udev, pm_message_t message) |
Arjan Mels | 1aee199 | 2011-06-30 22:18:18 +0200 | [diff] [blame] | 484 | { |
Valentina Manea | b7945b7 | 2014-01-23 23:12:29 +0200 | [diff] [blame] | 485 | dev_dbg(&udev->dev, "stub_resume\n"); |
| 486 | |
Arjan Mels | 1aee199 | 2011-06-30 22:18:18 +0200 | [diff] [blame] | 487 | return 0; |
| 488 | } |
| 489 | |
Valentina Manea | b7945b7 | 2014-01-23 23:12:29 +0200 | [diff] [blame] | 490 | #endif /* CONFIG_PM */ |
| 491 | |
| 492 | struct usb_device_driver stub_driver = { |
matt mooney | d012c2a5 | 2011-05-19 21:37:03 -0700 | [diff] [blame] | 493 | .name = "usbip-host", |
| 494 | .probe = stub_probe, |
| 495 | .disconnect = stub_disconnect, |
Valentina Manea | b7945b7 | 2014-01-23 23:12:29 +0200 | [diff] [blame] | 496 | #ifdef CONFIG_PM |
| 497 | .suspend = stub_suspend, |
| 498 | .resume = stub_resume, |
| 499 | #endif |
| 500 | .supports_autosuspend = 0, |
Akshay Joshi | 97c451c | 2011-08-17 15:58:32 -0400 | [diff] [blame] | 501 | }; |