vudc_sysfs.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2015 Karol Kosik <[email protected]>
  4. * Copyright (C) 2015-2016 Samsung Electronics
  5. * Igor Kotrasinski <[email protected]>
  6. * Krzysztof Opasiak <[email protected]>
  7. */
  8. #include <linux/device.h>
  9. #include <linux/list.h>
  10. #include <linux/usb/gadget.h>
  11. #include <linux/usb/ch9.h>
  12. #include <linux/sysfs.h>
  13. #include <linux/kthread.h>
  14. #include <linux/byteorder/generic.h>
  15. #include "usbip_common.h"
  16. #include "vudc.h"
  17. #include <net/sock.h>
  18. /* called with udc->lock held */
  19. int get_gadget_descs(struct vudc *udc)
  20. {
  21. struct vrequest *usb_req;
  22. struct vep *ep0 = to_vep(udc->gadget.ep0);
  23. struct usb_device_descriptor *ddesc = &udc->dev_desc;
  24. struct usb_ctrlrequest req;
  25. int ret;
  26. if (!udc->driver || !udc->pullup)
  27. return -EINVAL;
  28. req.bRequestType = USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
  29. req.bRequest = USB_REQ_GET_DESCRIPTOR;
  30. req.wValue = cpu_to_le16(USB_DT_DEVICE << 8);
  31. req.wIndex = cpu_to_le16(0);
  32. req.wLength = cpu_to_le16(sizeof(*ddesc));
  33. spin_unlock(&udc->lock);
  34. ret = udc->driver->setup(&(udc->gadget), &req);
  35. spin_lock(&udc->lock);
  36. if (ret < 0)
  37. goto out;
  38. /* assuming request queue is empty; request is now on top */
  39. usb_req = list_last_entry(&ep0->req_queue, struct vrequest, req_entry);
  40. list_del(&usb_req->req_entry);
  41. if (usb_req->req.length > sizeof(*ddesc)) {
  42. ret = -EOVERFLOW;
  43. goto giveback_req;
  44. }
  45. memcpy(ddesc, usb_req->req.buf, sizeof(*ddesc));
  46. udc->desc_cached = 1;
  47. ret = 0;
  48. giveback_req:
  49. usb_req->req.status = 0;
  50. usb_req->req.actual = usb_req->req.length;
  51. usb_gadget_giveback_request(&(ep0->ep), &(usb_req->req));
  52. out:
  53. return ret;
  54. }
  55. /*
  56. * Exposes device descriptor from the gadget driver.
  57. */
  58. static ssize_t dev_desc_read(struct file *file, struct kobject *kobj,
  59. struct bin_attribute *attr, char *out,
  60. loff_t off, size_t count)
  61. {
  62. struct device *dev = kobj_to_dev(kobj);
  63. struct vudc *udc = (struct vudc *)dev_get_drvdata(dev);
  64. char *desc_ptr = (char *) &udc->dev_desc;
  65. unsigned long flags;
  66. int ret;
  67. spin_lock_irqsave(&udc->lock, flags);
  68. if (!udc->desc_cached) {
  69. ret = -ENODEV;
  70. goto unlock;
  71. }
  72. memcpy(out, desc_ptr + off, count);
  73. ret = count;
  74. unlock:
  75. spin_unlock_irqrestore(&udc->lock, flags);
  76. return ret;
  77. }
  78. static BIN_ATTR_RO(dev_desc, sizeof(struct usb_device_descriptor));
  79. static ssize_t usbip_sockfd_store(struct device *dev,
  80. struct device_attribute *attr,
  81. const char *in, size_t count)
  82. {
  83. struct vudc *udc = (struct vudc *) dev_get_drvdata(dev);
  84. int rv;
  85. int sockfd = 0;
  86. int err;
  87. struct socket *socket;
  88. unsigned long flags;
  89. int ret;
  90. struct task_struct *tcp_rx = NULL;
  91. struct task_struct *tcp_tx = NULL;
  92. rv = kstrtoint(in, 0, &sockfd);
  93. if (rv != 0)
  94. return -EINVAL;
  95. if (!udc) {
  96. dev_err(dev, "no device");
  97. return -ENODEV;
  98. }
  99. mutex_lock(&udc->ud.sysfs_lock);
  100. spin_lock_irqsave(&udc->lock, flags);
  101. /* Don't export what we don't have */
  102. if (!udc->driver || !udc->pullup) {
  103. dev_err(dev, "gadget not bound");
  104. ret = -ENODEV;
  105. goto unlock;
  106. }
  107. if (sockfd != -1) {
  108. if (udc->connected) {
  109. dev_err(dev, "Device already connected");
  110. ret = -EBUSY;
  111. goto unlock;
  112. }
  113. spin_lock(&udc->ud.lock);
  114. if (udc->ud.status != SDEV_ST_AVAILABLE) {
  115. ret = -EINVAL;
  116. goto unlock_ud;
  117. }
  118. socket = sockfd_lookup(sockfd, &err);
  119. if (!socket) {
  120. dev_err(dev, "failed to lookup sock");
  121. ret = -EINVAL;
  122. goto unlock_ud;
  123. }
  124. if (socket->type != SOCK_STREAM) {
  125. dev_err(dev, "Expecting SOCK_STREAM - found %d",
  126. socket->type);
  127. ret = -EINVAL;
  128. goto sock_err;
  129. }
  130. /* unlock and create threads and get tasks */
  131. spin_unlock(&udc->ud.lock);
  132. spin_unlock_irqrestore(&udc->lock, flags);
  133. tcp_rx = kthread_create(&v_rx_loop, &udc->ud, "vudc_rx");
  134. if (IS_ERR(tcp_rx)) {
  135. sockfd_put(socket);
  136. mutex_unlock(&udc->ud.sysfs_lock);
  137. return -EINVAL;
  138. }
  139. tcp_tx = kthread_create(&v_tx_loop, &udc->ud, "vudc_tx");
  140. if (IS_ERR(tcp_tx)) {
  141. kthread_stop(tcp_rx);
  142. sockfd_put(socket);
  143. mutex_unlock(&udc->ud.sysfs_lock);
  144. return -EINVAL;
  145. }
  146. /* get task structs now */
  147. get_task_struct(tcp_rx);
  148. get_task_struct(tcp_tx);
  149. /* lock and update udc->ud state */
  150. spin_lock_irqsave(&udc->lock, flags);
  151. spin_lock(&udc->ud.lock);
  152. udc->ud.tcp_socket = socket;
  153. udc->ud.tcp_rx = tcp_rx;
  154. udc->ud.tcp_tx = tcp_tx;
  155. udc->ud.status = SDEV_ST_USED;
  156. spin_unlock(&udc->ud.lock);
  157. ktime_get_ts64(&udc->start_time);
  158. v_start_timer(udc);
  159. udc->connected = 1;
  160. spin_unlock_irqrestore(&udc->lock, flags);
  161. wake_up_process(udc->ud.tcp_rx);
  162. wake_up_process(udc->ud.tcp_tx);
  163. mutex_unlock(&udc->ud.sysfs_lock);
  164. return count;
  165. } else {
  166. if (!udc->connected) {
  167. dev_err(dev, "Device not connected");
  168. ret = -EINVAL;
  169. goto unlock;
  170. }
  171. spin_lock(&udc->ud.lock);
  172. if (udc->ud.status != SDEV_ST_USED) {
  173. ret = -EINVAL;
  174. goto unlock_ud;
  175. }
  176. spin_unlock(&udc->ud.lock);
  177. usbip_event_add(&udc->ud, VUDC_EVENT_DOWN);
  178. }
  179. spin_unlock_irqrestore(&udc->lock, flags);
  180. mutex_unlock(&udc->ud.sysfs_lock);
  181. return count;
  182. sock_err:
  183. sockfd_put(socket);
  184. unlock_ud:
  185. spin_unlock(&udc->ud.lock);
  186. unlock:
  187. spin_unlock_irqrestore(&udc->lock, flags);
  188. mutex_unlock(&udc->ud.sysfs_lock);
  189. return ret;
  190. }
  191. static DEVICE_ATTR_WO(usbip_sockfd);
  192. static ssize_t usbip_status_show(struct device *dev,
  193. struct device_attribute *attr, char *out)
  194. {
  195. struct vudc *udc = (struct vudc *) dev_get_drvdata(dev);
  196. int status;
  197. if (!udc) {
  198. dev_err(dev, "no device");
  199. return -ENODEV;
  200. }
  201. spin_lock_irq(&udc->ud.lock);
  202. status = udc->ud.status;
  203. spin_unlock_irq(&udc->ud.lock);
  204. return snprintf(out, PAGE_SIZE, "%d\n", status);
  205. }
  206. static DEVICE_ATTR_RO(usbip_status);
  207. static struct attribute *dev_attrs[] = {
  208. &dev_attr_usbip_sockfd.attr,
  209. &dev_attr_usbip_status.attr,
  210. NULL,
  211. };
  212. static struct bin_attribute *dev_bin_attrs[] = {
  213. &bin_attr_dev_desc,
  214. NULL,
  215. };
  216. static const struct attribute_group vudc_attr_group = {
  217. .attrs = dev_attrs,
  218. .bin_attrs = dev_bin_attrs,
  219. };
  220. const struct attribute_group *vudc_groups[] = {
  221. &vudc_attr_group,
  222. NULL,
  223. };