PageRenderTime 58ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/usb/usbip/vhci_sysfs.c

https://gitlab.com/webhaikal/SenseiOneplus3
C | 252 lines | 140 code | 51 blank | 61 comment | 19 complexity | 0e7e73739e9ec7c9999a0a2a0066bf58 MD5 | raw file
  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. #include <linux/kthread.h>
  20. #include <linux/file.h>
  21. #include <linux/net.h>
  22. #include "usbip_common.h"
  23. #include "vhci.h"
  24. /* TODO: refine locking ?*/
  25. /* Sysfs entry to show port status */
  26. static ssize_t status_show(struct device *dev, struct device_attribute *attr,
  27. char *out)
  28. {
  29. char *s = out;
  30. int i = 0;
  31. BUG_ON(!the_controller || !out);
  32. spin_lock(&the_controller->lock);
  33. /*
  34. * output example:
  35. * prt sta spd dev socket local_busid
  36. * 000 004 000 000 c5a7bb80 1-2.3
  37. * 001 004 000 000 d8cee980 2-3.4
  38. *
  39. * IP address can be retrieved from a socket pointer address by looking
  40. * up /proc/net/{tcp,tcp6}. Also, a userland program may remember a
  41. * port number and its peer IP address.
  42. */
  43. out += sprintf(out,
  44. "prt sta spd bus dev socket local_busid\n");
  45. for (i = 0; i < VHCI_NPORTS; i++) {
  46. struct vhci_device *vdev = port_to_vdev(i);
  47. spin_lock(&vdev->ud.lock);
  48. out += sprintf(out, "%03u %03u ", i, vdev->ud.status);
  49. if (vdev->ud.status == VDEV_ST_USED) {
  50. out += sprintf(out, "%03u %08x ",
  51. vdev->speed, vdev->devid);
  52. out += sprintf(out, "%16p ", vdev->ud.tcp_socket);
  53. out += sprintf(out, "%s", dev_name(&vdev->udev->dev));
  54. } else {
  55. out += sprintf(out, "000 000 000 0000000000000000 0-0");
  56. }
  57. out += sprintf(out, "\n");
  58. spin_unlock(&vdev->ud.lock);
  59. }
  60. spin_unlock(&the_controller->lock);
  61. return out - s;
  62. }
  63. static DEVICE_ATTR_RO(status);
  64. /* Sysfs entry to shutdown a virtual connection */
  65. static int vhci_port_disconnect(__u32 rhport)
  66. {
  67. struct vhci_device *vdev;
  68. usbip_dbg_vhci_sysfs("enter\n");
  69. /* lock */
  70. spin_lock(&the_controller->lock);
  71. vdev = port_to_vdev(rhport);
  72. spin_lock(&vdev->ud.lock);
  73. if (vdev->ud.status == VDEV_ST_NULL) {
  74. pr_err("not connected %d\n", vdev->ud.status);
  75. /* unlock */
  76. spin_unlock(&vdev->ud.lock);
  77. spin_unlock(&the_controller->lock);
  78. return -EINVAL;
  79. }
  80. /* unlock */
  81. spin_unlock(&vdev->ud.lock);
  82. spin_unlock(&the_controller->lock);
  83. usbip_event_add(&vdev->ud, VDEV_EVENT_DOWN);
  84. return 0;
  85. }
  86. static ssize_t store_detach(struct device *dev, struct device_attribute *attr,
  87. const char *buf, size_t count)
  88. {
  89. int err;
  90. __u32 rhport = 0;
  91. if (sscanf(buf, "%u", &rhport) != 1)
  92. return -EINVAL;
  93. /* check rhport */
  94. if (rhport >= VHCI_NPORTS) {
  95. dev_err(dev, "invalid port %u\n", rhport);
  96. return -EINVAL;
  97. }
  98. err = vhci_port_disconnect(rhport);
  99. if (err < 0)
  100. return -EINVAL;
  101. usbip_dbg_vhci_sysfs("Leave\n");
  102. return count;
  103. }
  104. static DEVICE_ATTR(detach, S_IWUSR, NULL, store_detach);
  105. /* Sysfs entry to establish a virtual connection */
  106. static int valid_args(__u32 rhport, enum usb_device_speed speed)
  107. {
  108. /* check rhport */
  109. if (rhport >= VHCI_NPORTS) {
  110. pr_err("port %u\n", rhport);
  111. return -EINVAL;
  112. }
  113. /* check speed */
  114. switch (speed) {
  115. case USB_SPEED_LOW:
  116. case USB_SPEED_FULL:
  117. case USB_SPEED_HIGH:
  118. case USB_SPEED_WIRELESS:
  119. break;
  120. default:
  121. pr_err("Failed attach request for unsupported USB speed: %s\n",
  122. usb_speed_string(speed));
  123. return -EINVAL;
  124. }
  125. return 0;
  126. }
  127. /*
  128. * To start a new USB/IP attachment, a userland program needs to setup a TCP
  129. * connection and then write its socket descriptor with remote device
  130. * information into this sysfs file.
  131. *
  132. * A remote device is virtually attached to the root-hub port of @rhport with
  133. * @speed. @devid is embedded into a request to specify the remote device in a
  134. * server host.
  135. *
  136. * write() returns 0 on success, else negative errno.
  137. */
  138. static ssize_t store_attach(struct device *dev, struct device_attribute *attr,
  139. const char *buf, size_t count)
  140. {
  141. struct vhci_device *vdev;
  142. struct socket *socket;
  143. int sockfd = 0;
  144. __u32 rhport = 0, devid = 0, speed = 0;
  145. int err;
  146. /*
  147. * @rhport: port number of vhci_hcd
  148. * @sockfd: socket descriptor of an established TCP connection
  149. * @devid: unique device identifier in a remote host
  150. * @speed: usb device speed in a remote host
  151. */
  152. if (sscanf(buf, "%u %u %u %u", &rhport, &sockfd, &devid, &speed) != 4)
  153. return -EINVAL;
  154. usbip_dbg_vhci_sysfs("rhport(%u) sockfd(%u) devid(%u) speed(%u)\n",
  155. rhport, sockfd, devid, speed);
  156. /* check received parameters */
  157. if (valid_args(rhport, speed) < 0)
  158. return -EINVAL;
  159. /* Extract socket from fd. */
  160. socket = sockfd_lookup(sockfd, &err);
  161. if (!socket)
  162. return -EINVAL;
  163. /* now need lock until setting vdev status as used */
  164. /* begin a lock */
  165. spin_lock(&the_controller->lock);
  166. vdev = port_to_vdev(rhport);
  167. spin_lock(&vdev->ud.lock);
  168. if (vdev->ud.status != VDEV_ST_NULL) {
  169. /* end of the lock */
  170. spin_unlock(&vdev->ud.lock);
  171. spin_unlock(&the_controller->lock);
  172. sockfd_put(socket);
  173. dev_err(dev, "port %d already used\n", rhport);
  174. return -EINVAL;
  175. }
  176. dev_info(dev,
  177. "rhport(%u) sockfd(%d) devid(%u) speed(%u) speed_str(%s)\n",
  178. rhport, sockfd, devid, speed, usb_speed_string(speed));
  179. vdev->devid = devid;
  180. vdev->speed = speed;
  181. vdev->ud.tcp_socket = socket;
  182. vdev->ud.status = VDEV_ST_NOTASSIGNED;
  183. spin_unlock(&vdev->ud.lock);
  184. spin_unlock(&the_controller->lock);
  185. /* end the lock */
  186. vdev->ud.tcp_rx = kthread_get_run(vhci_rx_loop, &vdev->ud, "vhci_rx");
  187. vdev->ud.tcp_tx = kthread_get_run(vhci_tx_loop, &vdev->ud, "vhci_tx");
  188. rh_port_connect(rhport, speed);
  189. return count;
  190. }
  191. static DEVICE_ATTR(attach, S_IWUSR, NULL, store_attach);
  192. static struct attribute *dev_attrs[] = {
  193. &dev_attr_status.attr,
  194. &dev_attr_detach.attr,
  195. &dev_attr_attach.attr,
  196. &dev_attr_usbip_debug.attr,
  197. NULL,
  198. };
  199. const struct attribute_group dev_attr_group = {
  200. .attrs = dev_attrs,
  201. };