0001
0002
0003
0004
0005
0006 #include <linux/device.h>
0007 #include <linux/file.h>
0008 #include <linux/kthread.h>
0009 #include <linux/module.h>
0010
0011 #include "usbip_common.h"
0012 #include "stub.h"
0013
0014
0015
0016
0017
0018 static ssize_t usbip_status_show(struct device *dev,
0019 struct device_attribute *attr, char *buf)
0020 {
0021 struct stub_device *sdev = dev_get_drvdata(dev);
0022 int status;
0023
0024 if (!sdev) {
0025 dev_err(dev, "sdev is null\n");
0026 return -ENODEV;
0027 }
0028
0029 spin_lock_irq(&sdev->ud.lock);
0030 status = sdev->ud.status;
0031 spin_unlock_irq(&sdev->ud.lock);
0032
0033 return snprintf(buf, PAGE_SIZE, "%d\n", status);
0034 }
0035 static DEVICE_ATTR_RO(usbip_status);
0036
0037
0038
0039
0040
0041
0042 static ssize_t usbip_sockfd_store(struct device *dev, struct device_attribute *attr,
0043 const char *buf, size_t count)
0044 {
0045 struct stub_device *sdev = dev_get_drvdata(dev);
0046 int sockfd = 0;
0047 struct socket *socket;
0048 int rv;
0049 struct task_struct *tcp_rx = NULL;
0050 struct task_struct *tcp_tx = NULL;
0051
0052 if (!sdev) {
0053 dev_err(dev, "sdev is null\n");
0054 return -ENODEV;
0055 }
0056
0057 rv = sscanf(buf, "%d", &sockfd);
0058 if (rv != 1)
0059 return -EINVAL;
0060
0061 if (sockfd != -1) {
0062 int err;
0063
0064 dev_info(dev, "stub up\n");
0065
0066 mutex_lock(&sdev->ud.sysfs_lock);
0067 spin_lock_irq(&sdev->ud.lock);
0068
0069 if (sdev->ud.status != SDEV_ST_AVAILABLE) {
0070 dev_err(dev, "not ready\n");
0071 goto err;
0072 }
0073
0074 socket = sockfd_lookup(sockfd, &err);
0075 if (!socket) {
0076 dev_err(dev, "failed to lookup sock");
0077 goto err;
0078 }
0079
0080 if (socket->type != SOCK_STREAM) {
0081 dev_err(dev, "Expecting SOCK_STREAM - found %d",
0082 socket->type);
0083 goto sock_err;
0084 }
0085
0086
0087 spin_unlock_irq(&sdev->ud.lock);
0088 tcp_rx = kthread_create(stub_rx_loop, &sdev->ud, "stub_rx");
0089 if (IS_ERR(tcp_rx)) {
0090 sockfd_put(socket);
0091 goto unlock_mutex;
0092 }
0093 tcp_tx = kthread_create(stub_tx_loop, &sdev->ud, "stub_tx");
0094 if (IS_ERR(tcp_tx)) {
0095 kthread_stop(tcp_rx);
0096 sockfd_put(socket);
0097 goto unlock_mutex;
0098 }
0099
0100
0101 get_task_struct(tcp_rx);
0102 get_task_struct(tcp_tx);
0103
0104
0105 spin_lock_irq(&sdev->ud.lock);
0106 sdev->ud.tcp_socket = socket;
0107 sdev->ud.sockfd = sockfd;
0108 sdev->ud.tcp_rx = tcp_rx;
0109 sdev->ud.tcp_tx = tcp_tx;
0110 sdev->ud.status = SDEV_ST_USED;
0111 spin_unlock_irq(&sdev->ud.lock);
0112
0113 wake_up_process(sdev->ud.tcp_rx);
0114 wake_up_process(sdev->ud.tcp_tx);
0115
0116 mutex_unlock(&sdev->ud.sysfs_lock);
0117
0118 } else {
0119 dev_info(dev, "stub down\n");
0120
0121 spin_lock_irq(&sdev->ud.lock);
0122 if (sdev->ud.status != SDEV_ST_USED)
0123 goto err;
0124
0125 spin_unlock_irq(&sdev->ud.lock);
0126
0127 usbip_event_add(&sdev->ud, SDEV_EVENT_DOWN);
0128 mutex_unlock(&sdev->ud.sysfs_lock);
0129 }
0130
0131 return count;
0132
0133 sock_err:
0134 sockfd_put(socket);
0135 err:
0136 spin_unlock_irq(&sdev->ud.lock);
0137 unlock_mutex:
0138 mutex_unlock(&sdev->ud.sysfs_lock);
0139 return -EINVAL;
0140 }
0141 static DEVICE_ATTR_WO(usbip_sockfd);
0142
0143 static struct attribute *usbip_attrs[] = {
0144 &dev_attr_usbip_status.attr,
0145 &dev_attr_usbip_sockfd.attr,
0146 &dev_attr_usbip_debug.attr,
0147 NULL,
0148 };
0149 ATTRIBUTE_GROUPS(usbip);
0150
0151 static void stub_shutdown_connection(struct usbip_device *ud)
0152 {
0153 struct stub_device *sdev = container_of(ud, struct stub_device, ud);
0154
0155
0156
0157
0158
0159
0160
0161 if (ud->tcp_socket) {
0162 dev_dbg(&sdev->udev->dev, "shutdown sockfd %d\n", ud->sockfd);
0163 kernel_sock_shutdown(ud->tcp_socket, SHUT_RDWR);
0164 }
0165
0166
0167 if (ud->tcp_rx) {
0168 kthread_stop_put(ud->tcp_rx);
0169 ud->tcp_rx = NULL;
0170 }
0171 if (ud->tcp_tx) {
0172 kthread_stop_put(ud->tcp_tx);
0173 ud->tcp_tx = NULL;
0174 }
0175
0176
0177
0178
0179
0180
0181
0182 if (ud->tcp_socket) {
0183 sockfd_put(ud->tcp_socket);
0184 ud->tcp_socket = NULL;
0185 ud->sockfd = -1;
0186 }
0187
0188
0189 stub_device_cleanup_urbs(sdev);
0190
0191
0192 {
0193 unsigned long flags;
0194 struct stub_unlink *unlink, *tmp;
0195
0196 spin_lock_irqsave(&sdev->priv_lock, flags);
0197 list_for_each_entry_safe(unlink, tmp, &sdev->unlink_tx, list) {
0198 list_del(&unlink->list);
0199 kfree(unlink);
0200 }
0201 list_for_each_entry_safe(unlink, tmp, &sdev->unlink_free,
0202 list) {
0203 list_del(&unlink->list);
0204 kfree(unlink);
0205 }
0206 spin_unlock_irqrestore(&sdev->priv_lock, flags);
0207 }
0208 }
0209
0210 static void stub_device_reset(struct usbip_device *ud)
0211 {
0212 struct stub_device *sdev = container_of(ud, struct stub_device, ud);
0213 struct usb_device *udev = sdev->udev;
0214 int ret;
0215
0216 dev_dbg(&udev->dev, "device reset");
0217
0218 ret = usb_lock_device_for_reset(udev, NULL);
0219 if (ret < 0) {
0220 dev_err(&udev->dev, "lock for reset\n");
0221 spin_lock_irq(&ud->lock);
0222 ud->status = SDEV_ST_ERROR;
0223 spin_unlock_irq(&ud->lock);
0224 return;
0225 }
0226
0227
0228 ret = usb_reset_device(udev);
0229 usb_unlock_device(udev);
0230
0231 spin_lock_irq(&ud->lock);
0232 if (ret) {
0233 dev_err(&udev->dev, "device reset\n");
0234 ud->status = SDEV_ST_ERROR;
0235 } else {
0236 dev_info(&udev->dev, "device reset\n");
0237 ud->status = SDEV_ST_AVAILABLE;
0238 }
0239 spin_unlock_irq(&ud->lock);
0240 }
0241
0242 static void stub_device_unusable(struct usbip_device *ud)
0243 {
0244 spin_lock_irq(&ud->lock);
0245 ud->status = SDEV_ST_ERROR;
0246 spin_unlock_irq(&ud->lock);
0247 }
0248
0249
0250
0251
0252
0253
0254
0255 static struct stub_device *stub_device_alloc(struct usb_device *udev)
0256 {
0257 struct stub_device *sdev;
0258 int busnum = udev->bus->busnum;
0259 int devnum = udev->devnum;
0260
0261 dev_dbg(&udev->dev, "allocating stub device");
0262
0263
0264 sdev = kzalloc(sizeof(struct stub_device), GFP_KERNEL);
0265 if (!sdev)
0266 return NULL;
0267
0268 sdev->udev = usb_get_dev(udev);
0269
0270
0271
0272
0273
0274
0275 sdev->devid = (busnum << 16) | devnum;
0276 sdev->ud.side = USBIP_STUB;
0277 sdev->ud.status = SDEV_ST_AVAILABLE;
0278 spin_lock_init(&sdev->ud.lock);
0279 mutex_init(&sdev->ud.sysfs_lock);
0280 sdev->ud.tcp_socket = NULL;
0281 sdev->ud.sockfd = -1;
0282
0283 INIT_LIST_HEAD(&sdev->priv_init);
0284 INIT_LIST_HEAD(&sdev->priv_tx);
0285 INIT_LIST_HEAD(&sdev->priv_free);
0286 INIT_LIST_HEAD(&sdev->unlink_free);
0287 INIT_LIST_HEAD(&sdev->unlink_tx);
0288 spin_lock_init(&sdev->priv_lock);
0289
0290 init_waitqueue_head(&sdev->tx_waitq);
0291
0292 sdev->ud.eh_ops.shutdown = stub_shutdown_connection;
0293 sdev->ud.eh_ops.reset = stub_device_reset;
0294 sdev->ud.eh_ops.unusable = stub_device_unusable;
0295
0296 usbip_start_eh(&sdev->ud);
0297
0298 dev_dbg(&udev->dev, "register new device\n");
0299
0300 return sdev;
0301 }
0302
0303 static void stub_device_free(struct stub_device *sdev)
0304 {
0305 kfree(sdev);
0306 }
0307
0308 static int stub_probe(struct usb_device *udev)
0309 {
0310 struct stub_device *sdev = NULL;
0311 const char *udev_busid = dev_name(&udev->dev);
0312 struct bus_id_priv *busid_priv;
0313 int rc = 0;
0314 char save_status;
0315
0316 dev_dbg(&udev->dev, "Enter probe\n");
0317
0318
0319
0320
0321 sdev = stub_device_alloc(udev);
0322 if (!sdev)
0323 return -ENOMEM;
0324
0325
0326 busid_priv = get_busid_priv(udev_busid);
0327 if (!busid_priv || (busid_priv->status == STUB_BUSID_REMOV) ||
0328 (busid_priv->status == STUB_BUSID_OTHER)) {
0329 dev_info(&udev->dev,
0330 "%s is not in match_busid table... skip!\n",
0331 udev_busid);
0332
0333
0334
0335
0336
0337
0338 rc = -ENODEV;
0339 if (!busid_priv)
0340 goto sdev_free;
0341
0342 goto call_put_busid_priv;
0343 }
0344
0345 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB) {
0346 dev_dbg(&udev->dev, "%s is a usb hub device... skip!\n",
0347 udev_busid);
0348 rc = -ENODEV;
0349 goto call_put_busid_priv;
0350 }
0351
0352 if (!strcmp(udev->bus->bus_name, "vhci_hcd")) {
0353 dev_dbg(&udev->dev,
0354 "%s is attached on vhci_hcd... skip!\n",
0355 udev_busid);
0356
0357 rc = -ENODEV;
0358 goto call_put_busid_priv;
0359 }
0360
0361
0362 dev_info(&udev->dev,
0363 "usbip-host: register new device (bus %u dev %u)\n",
0364 udev->bus->busnum, udev->devnum);
0365
0366 busid_priv->shutdown_busid = 0;
0367
0368
0369 dev_set_drvdata(&udev->dev, sdev);
0370
0371 busid_priv->sdev = sdev;
0372 busid_priv->udev = udev;
0373
0374 save_status = busid_priv->status;
0375 busid_priv->status = STUB_BUSID_ALLOC;
0376
0377
0378 put_busid_priv(busid_priv);
0379
0380
0381
0382
0383
0384
0385 rc = usb_hub_claim_port(udev->parent, udev->portnum,
0386 (struct usb_dev_state *) udev);
0387 if (rc) {
0388 dev_dbg(&udev->dev, "unable to claim port\n");
0389 goto err_port;
0390 }
0391
0392 return 0;
0393
0394 err_port:
0395 dev_set_drvdata(&udev->dev, NULL);
0396
0397
0398 spin_lock(&busid_priv->busid_lock);
0399 busid_priv->sdev = NULL;
0400 busid_priv->status = save_status;
0401 spin_unlock(&busid_priv->busid_lock);
0402
0403 goto sdev_free;
0404
0405 call_put_busid_priv:
0406
0407 put_busid_priv(busid_priv);
0408
0409 sdev_free:
0410 usb_put_dev(udev);
0411 stub_device_free(sdev);
0412
0413 return rc;
0414 }
0415
0416 static void shutdown_busid(struct bus_id_priv *busid_priv)
0417 {
0418 usbip_event_add(&busid_priv->sdev->ud, SDEV_EVENT_REMOVED);
0419
0420
0421 usbip_stop_eh(&busid_priv->sdev->ud);
0422 }
0423
0424
0425
0426
0427
0428 static void stub_disconnect(struct usb_device *udev)
0429 {
0430 struct stub_device *sdev;
0431 const char *udev_busid = dev_name(&udev->dev);
0432 struct bus_id_priv *busid_priv;
0433 int rc;
0434
0435 dev_dbg(&udev->dev, "Enter disconnect\n");
0436
0437 busid_priv = get_busid_priv(udev_busid);
0438 if (!busid_priv) {
0439 BUG();
0440 return;
0441 }
0442
0443 sdev = dev_get_drvdata(&udev->dev);
0444
0445
0446 if (!sdev) {
0447 dev_err(&udev->dev, "could not get device");
0448
0449 put_busid_priv(busid_priv);
0450 return;
0451 }
0452
0453 dev_set_drvdata(&udev->dev, NULL);
0454
0455
0456 put_busid_priv(busid_priv);
0457
0458
0459
0460
0461
0462
0463 rc = usb_hub_release_port(udev->parent, udev->portnum,
0464 (struct usb_dev_state *) udev);
0465 if (rc) {
0466 dev_dbg(&udev->dev, "unable to release port\n");
0467 return;
0468 }
0469
0470
0471 if (usbip_in_eh(current))
0472 return;
0473
0474
0475 spin_lock(&busid_priv->busid_lock);
0476 if (!busid_priv->shutdown_busid)
0477 busid_priv->shutdown_busid = 1;
0478
0479 spin_unlock(&busid_priv->busid_lock);
0480
0481
0482 shutdown_busid(busid_priv);
0483
0484 usb_put_dev(sdev->udev);
0485
0486
0487 spin_lock(&busid_priv->busid_lock);
0488
0489 busid_priv->sdev = NULL;
0490 stub_device_free(sdev);
0491
0492 if (busid_priv->status == STUB_BUSID_ALLOC)
0493 busid_priv->status = STUB_BUSID_ADDED;
0494
0495 spin_unlock(&busid_priv->busid_lock);
0496 return;
0497 }
0498
0499 #ifdef CONFIG_PM
0500
0501
0502
0503
0504 static int stub_suspend(struct usb_device *udev, pm_message_t message)
0505 {
0506 dev_dbg(&udev->dev, "stub_suspend\n");
0507
0508 return 0;
0509 }
0510
0511 static int stub_resume(struct usb_device *udev, pm_message_t message)
0512 {
0513 dev_dbg(&udev->dev, "stub_resume\n");
0514
0515 return 0;
0516 }
0517
0518 #endif
0519
0520 struct usb_device_driver stub_driver = {
0521 .name = "usbip-host",
0522 .probe = stub_probe,
0523 .disconnect = stub_disconnect,
0524 #ifdef CONFIG_PM
0525 .suspend = stub_suspend,
0526 .resume = stub_resume,
0527 #endif
0528 .supports_autosuspend = 0,
0529 .dev_groups = usbip_groups,
0530 };