vhci_sysfs.c 9.7 KB

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