vhci_sysfs.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. /*
  2. * Copyright (C) 2003-2008 Takahiro Hirofuchi
  3. * Copyright (C) 2015-2016 Nobuo Iwata
  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. #include <linux/kthread.h>
  21. #include <linux/file.h>
  22. #include <linux/net.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/slab.h>
  25. #include "usbip_common.h"
  26. #include "vhci.h"
  27. /* TODO: refine locking ?*/
  28. /*
  29. * output example:
  30. * hub port sta spd dev socket local_busid
  31. * hs 0000 004 000 00000000 c5a7bb80 1-2.3
  32. * ................................................
  33. * ss 0008 004 000 00000000 d8cee980 2-3.4
  34. * ................................................
  35. *
  36. * IP address can be retrieved from a socket pointer address by looking
  37. * up /proc/net/{tcp,tcp6}. Also, a userland program may remember a
  38. * port number and its peer IP address.
  39. */
  40. static void port_show_vhci(char **out, int hub, int port, struct vhci_device *vdev)
  41. {
  42. if (hub == HUB_SPEED_HIGH)
  43. *out += sprintf(*out, "hs %04u %03u ",
  44. port, vdev->ud.status);
  45. else /* hub == HUB_SPEED_SUPER */
  46. *out += sprintf(*out, "ss %04u %03u ",
  47. port, vdev->ud.status);
  48. if (vdev->ud.status == VDEV_ST_USED) {
  49. *out += sprintf(*out, "%03u %08x ",
  50. vdev->speed, vdev->devid);
  51. *out += sprintf(*out, "%16p %s",
  52. vdev->ud.tcp_socket,
  53. dev_name(&vdev->udev->dev));
  54. } else {
  55. *out += sprintf(*out, "000 00000000 ");
  56. *out += sprintf(*out, "0000000000000000 0-0");
  57. }
  58. *out += sprintf(*out, "\n");
  59. }
  60. /* Sysfs entry to show port status */
  61. static ssize_t status_show_vhci(int pdev_nr, char *out)
  62. {
  63. struct platform_device *pdev = vhcis[pdev_nr].pdev;
  64. struct vhci *vhci;
  65. struct usb_hcd *hcd;
  66. struct vhci_hcd *vhci_hcd;
  67. char *s = out;
  68. int i;
  69. unsigned long flags;
  70. if (!pdev || !out) {
  71. usbip_dbg_vhci_sysfs("show status error\n");
  72. return 0;
  73. }
  74. hcd = platform_get_drvdata(pdev);
  75. vhci_hcd = hcd_to_vhci_hcd(hcd);
  76. vhci = vhci_hcd->vhci;
  77. spin_lock_irqsave(&vhci->lock, flags);
  78. for (i = 0; i < VHCI_HC_PORTS; i++) {
  79. struct vhci_device *vdev = &vhci->vhci_hcd_hs->vdev[i];
  80. spin_lock(&vdev->ud.lock);
  81. port_show_vhci(&out, HUB_SPEED_HIGH,
  82. pdev_nr * VHCI_PORTS + i, vdev);
  83. spin_unlock(&vdev->ud.lock);
  84. }
  85. for (i = 0; i < VHCI_HC_PORTS; i++) {
  86. struct vhci_device *vdev = &vhci->vhci_hcd_ss->vdev[i];
  87. spin_lock(&vdev->ud.lock);
  88. port_show_vhci(&out, HUB_SPEED_SUPER,
  89. pdev_nr * VHCI_PORTS + VHCI_HC_PORTS + i, vdev);
  90. spin_unlock(&vdev->ud.lock);
  91. }
  92. spin_unlock_irqrestore(&vhci->lock, flags);
  93. return out - s;
  94. }
  95. static ssize_t status_show_not_ready(int pdev_nr, char *out)
  96. {
  97. char *s = out;
  98. int i = 0;
  99. for (i = 0; i < VHCI_HC_PORTS; i++) {
  100. out += sprintf(out, "hs %04u %03u ",
  101. (pdev_nr * VHCI_PORTS) + i,
  102. VDEV_ST_NOTASSIGNED);
  103. out += sprintf(out, "000 00000000 0000000000000000 0-0");
  104. out += sprintf(out, "\n");
  105. }
  106. for (i = 0; i < VHCI_HC_PORTS; i++) {
  107. out += sprintf(out, "ss %04u %03u ",
  108. (pdev_nr * VHCI_PORTS) + VHCI_HC_PORTS + i,
  109. VDEV_ST_NOTASSIGNED);
  110. out += sprintf(out, "000 00000000 0000000000000000 0-0");
  111. out += sprintf(out, "\n");
  112. }
  113. return out - s;
  114. }
  115. static int status_name_to_id(const char *name)
  116. {
  117. char *c;
  118. long val;
  119. int ret;
  120. c = strchr(name, '.');
  121. if (c == NULL)
  122. return 0;
  123. ret = kstrtol(c+1, 10, &val);
  124. if (ret < 0)
  125. return ret;
  126. return val;
  127. }
  128. static ssize_t status_show(struct device *dev,
  129. struct device_attribute *attr, char *out)
  130. {
  131. char *s = out;
  132. int pdev_nr;
  133. out += sprintf(out,
  134. "hub port sta spd dev socket local_busid\n");
  135. pdev_nr = status_name_to_id(attr->attr.name);
  136. if (pdev_nr < 0)
  137. out += status_show_not_ready(pdev_nr, out);
  138. else
  139. out += status_show_vhci(pdev_nr, out);
  140. return out - s;
  141. }
  142. static ssize_t nports_show(struct device *dev, struct device_attribute *attr,
  143. char *out)
  144. {
  145. char *s = out;
  146. /*
  147. * Half the ports are for SPEED_HIGH and half for SPEED_SUPER, thus the * 2.
  148. */
  149. out += sprintf(out, "%d\n", VHCI_PORTS * vhci_num_controllers);
  150. return out - s;
  151. }
  152. static DEVICE_ATTR_RO(nports);
  153. /* Sysfs entry to shutdown a virtual connection */
  154. static int vhci_port_disconnect(struct vhci_hcd *vhci_hcd, __u32 rhport)
  155. {
  156. struct vhci_device *vdev = &vhci_hcd->vdev[rhport];
  157. struct vhci *vhci = vhci_hcd->vhci;
  158. unsigned long flags;
  159. usbip_dbg_vhci_sysfs("enter\n");
  160. /* lock */
  161. spin_lock_irqsave(&vhci->lock, flags);
  162. spin_lock(&vdev->ud.lock);
  163. if (vdev->ud.status == VDEV_ST_NULL) {
  164. pr_err("not connected %d\n", vdev->ud.status);
  165. /* unlock */
  166. spin_unlock(&vdev->ud.lock);
  167. spin_unlock_irqrestore(&vhci->lock, flags);
  168. return -EINVAL;
  169. }
  170. /* unlock */
  171. spin_unlock(&vdev->ud.lock);
  172. spin_unlock_irqrestore(&vhci->lock, flags);
  173. usbip_event_add(&vdev->ud, VDEV_EVENT_DOWN);
  174. return 0;
  175. }
  176. static int valid_port(__u32 pdev_nr, __u32 rhport)
  177. {
  178. if (pdev_nr >= vhci_num_controllers) {
  179. pr_err("pdev %u\n", pdev_nr);
  180. return 0;
  181. }
  182. if (rhport >= VHCI_HC_PORTS) {
  183. pr_err("rhport %u\n", rhport);
  184. return 0;
  185. }
  186. return 1;
  187. }
  188. static ssize_t store_detach(struct device *dev, struct device_attribute *attr,
  189. const char *buf, size_t count)
  190. {
  191. __u32 port = 0, pdev_nr = 0, rhport = 0;
  192. struct usb_hcd *hcd;
  193. struct vhci_hcd *vhci_hcd;
  194. int ret;
  195. if (kstrtoint(buf, 10, &port) < 0)
  196. return -EINVAL;
  197. pdev_nr = port_to_pdev_nr(port);
  198. rhport = port_to_rhport(port);
  199. if (!valid_port(pdev_nr, rhport))
  200. return -EINVAL;
  201. hcd = platform_get_drvdata(vhcis[pdev_nr].pdev);
  202. if (hcd == NULL) {
  203. dev_err(dev, "port is not ready %u\n", port);
  204. return -EAGAIN;
  205. }
  206. usbip_dbg_vhci_sysfs("rhport %d\n", rhport);
  207. if ((port / VHCI_HC_PORTS) % 2)
  208. vhci_hcd = hcd_to_vhci_hcd(hcd)->vhci->vhci_hcd_ss;
  209. else
  210. vhci_hcd = hcd_to_vhci_hcd(hcd)->vhci->vhci_hcd_hs;
  211. ret = vhci_port_disconnect(vhci_hcd, rhport);
  212. if (ret < 0)
  213. return -EINVAL;
  214. usbip_dbg_vhci_sysfs("Leave\n");
  215. return count;
  216. }
  217. static DEVICE_ATTR(detach, S_IWUSR, NULL, store_detach);
  218. static int valid_args(__u32 pdev_nr, __u32 rhport, enum usb_device_speed speed)
  219. {
  220. if (!valid_port(pdev_nr, rhport)) {
  221. return 0;
  222. }
  223. switch (speed) {
  224. case USB_SPEED_LOW:
  225. case USB_SPEED_FULL:
  226. case USB_SPEED_HIGH:
  227. case USB_SPEED_WIRELESS:
  228. case USB_SPEED_SUPER:
  229. break;
  230. default:
  231. pr_err("Failed attach request for unsupported USB speed: %s\n",
  232. usb_speed_string(speed));
  233. return 0;
  234. }
  235. return 1;
  236. }
  237. /* Sysfs entry to establish a virtual connection */
  238. /*
  239. * To start a new USB/IP attachment, a userland program needs to setup a TCP
  240. * connection and then write its socket descriptor with remote device
  241. * information into this sysfs file.
  242. *
  243. * A remote device is virtually attached to the root-hub port of @rhport with
  244. * @speed. @devid is embedded into a request to specify the remote device in a
  245. * server host.
  246. *
  247. * write() returns 0 on success, else negative errno.
  248. */
  249. static ssize_t store_attach(struct device *dev, struct device_attribute *attr,
  250. const char *buf, size_t count)
  251. {
  252. struct socket *socket;
  253. int sockfd = 0;
  254. __u32 port = 0, pdev_nr = 0, rhport = 0, devid = 0, speed = 0;
  255. struct usb_hcd *hcd;
  256. struct vhci_hcd *vhci_hcd;
  257. struct vhci_device *vdev;
  258. struct vhci *vhci;
  259. int err;
  260. unsigned long flags;
  261. /*
  262. * @rhport: port number of vhci_hcd
  263. * @sockfd: socket descriptor of an established TCP connection
  264. * @devid: unique device identifier in a remote host
  265. * @speed: usb device speed in a remote host
  266. */
  267. if (sscanf(buf, "%u %u %u %u", &port, &sockfd, &devid, &speed) != 4)
  268. return -EINVAL;
  269. pdev_nr = port_to_pdev_nr(port);
  270. rhport = port_to_rhport(port);
  271. usbip_dbg_vhci_sysfs("port(%u) pdev(%d) rhport(%u)\n",
  272. port, pdev_nr, rhport);
  273. usbip_dbg_vhci_sysfs("sockfd(%u) devid(%u) speed(%u)\n",
  274. sockfd, devid, speed);
  275. /* check received parameters */
  276. if (!valid_args(pdev_nr, rhport, speed))
  277. return -EINVAL;
  278. hcd = platform_get_drvdata(vhcis[pdev_nr].pdev);
  279. if (hcd == NULL) {
  280. dev_err(dev, "port %d is not ready\n", port);
  281. return -EAGAIN;
  282. }
  283. vhci_hcd = hcd_to_vhci_hcd(hcd);
  284. vhci = vhci_hcd->vhci;
  285. if (speed == USB_SPEED_SUPER)
  286. vdev = &vhci->vhci_hcd_ss->vdev[rhport];
  287. else
  288. vdev = &vhci->vhci_hcd_hs->vdev[rhport];
  289. /* Extract socket from fd. */
  290. socket = sockfd_lookup(sockfd, &err);
  291. if (!socket)
  292. return -EINVAL;
  293. /* now need lock until setting vdev status as used */
  294. /* begin a lock */
  295. spin_lock_irqsave(&vhci->lock, flags);
  296. spin_lock(&vdev->ud.lock);
  297. if (vdev->ud.status != VDEV_ST_NULL) {
  298. /* end of the lock */
  299. spin_unlock(&vdev->ud.lock);
  300. spin_unlock_irqrestore(&vhci->lock, flags);
  301. sockfd_put(socket);
  302. dev_err(dev, "port %d already used\n", rhport);
  303. /*
  304. * Will be retried from userspace
  305. * if there's another free port.
  306. */
  307. return -EBUSY;
  308. }
  309. dev_info(dev, "pdev(%u) rhport(%u) sockfd(%d)\n",
  310. pdev_nr, rhport, sockfd);
  311. dev_info(dev, "devid(%u) speed(%u) speed_str(%s)\n",
  312. devid, speed, usb_speed_string(speed));
  313. vdev->devid = devid;
  314. vdev->speed = speed;
  315. vdev->ud.tcp_socket = socket;
  316. vdev->ud.status = VDEV_ST_NOTASSIGNED;
  317. spin_unlock(&vdev->ud.lock);
  318. spin_unlock_irqrestore(&vhci->lock, flags);
  319. /* end the lock */
  320. vdev->ud.tcp_rx = kthread_get_run(vhci_rx_loop, &vdev->ud, "vhci_rx");
  321. vdev->ud.tcp_tx = kthread_get_run(vhci_tx_loop, &vdev->ud, "vhci_tx");
  322. rh_port_connect(vdev, speed);
  323. return count;
  324. }
  325. static DEVICE_ATTR(attach, S_IWUSR, NULL, store_attach);
  326. #define MAX_STATUS_NAME 16
  327. struct status_attr {
  328. struct device_attribute attr;
  329. char name[MAX_STATUS_NAME+1];
  330. };
  331. static struct status_attr *status_attrs;
  332. static void set_status_attr(int id)
  333. {
  334. struct status_attr *status;
  335. status = status_attrs + id;
  336. if (id == 0)
  337. strcpy(status->name, "status");
  338. else
  339. snprintf(status->name, MAX_STATUS_NAME+1, "status.%d", id);
  340. status->attr.attr.name = status->name;
  341. status->attr.attr.mode = S_IRUGO;
  342. status->attr.show = status_show;
  343. sysfs_attr_init(&status->attr.attr);
  344. }
  345. static int init_status_attrs(void)
  346. {
  347. int id;
  348. status_attrs = kcalloc(vhci_num_controllers, sizeof(struct status_attr),
  349. GFP_KERNEL);
  350. if (status_attrs == NULL)
  351. return -ENOMEM;
  352. for (id = 0; id < vhci_num_controllers; id++)
  353. set_status_attr(id);
  354. return 0;
  355. }
  356. static void finish_status_attrs(void)
  357. {
  358. kfree(status_attrs);
  359. }
  360. struct attribute_group vhci_attr_group = {
  361. .attrs = NULL,
  362. };
  363. int vhci_init_attr_group(void)
  364. {
  365. struct attribute **attrs;
  366. int ret, i;
  367. attrs = kcalloc((vhci_num_controllers + 5), sizeof(struct attribute *),
  368. GFP_KERNEL);
  369. if (attrs == NULL)
  370. return -ENOMEM;
  371. ret = init_status_attrs();
  372. if (ret) {
  373. kfree(attrs);
  374. return ret;
  375. }
  376. *attrs = &dev_attr_nports.attr;
  377. *(attrs + 1) = &dev_attr_detach.attr;
  378. *(attrs + 2) = &dev_attr_attach.attr;
  379. *(attrs + 3) = &dev_attr_usbip_debug.attr;
  380. for (i = 0; i < vhci_num_controllers; i++)
  381. *(attrs + i + 4) = &((status_attrs + i)->attr.attr);
  382. vhci_attr_group.attrs = attrs;
  383. return 0;
  384. }
  385. void vhci_finish_attr_group(void)
  386. {
  387. finish_status_attrs();
  388. kfree(vhci_attr_group.attrs);
  389. }