vudc_sysfs.c 5.9 KB

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