vudc_sysfs.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * Copyright (C) 2015 Karol Kosik <karo9@interia.eu>
  3. * Copyright (C) 2015-2016 Samsung Electronics
  4. * Igor Kotrasinski <i.kotrasinsk@samsung.com>
  5. * Krzysztof Opasiak <k.opasiak@samsung.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <linux/device.h>
  21. #include <linux/list.h>
  22. #include <linux/usb/gadget.h>
  23. #include <linux/usb/ch9.h>
  24. #include <linux/sysfs.h>
  25. #include <linux/kthread.h>
  26. #include <linux/byteorder/generic.h>
  27. #include "usbip_common.h"
  28. #include "vudc.h"
  29. #include <net/sock.h>
  30. /* called with udc->lock held */
  31. int get_gadget_descs(struct vudc *udc)
  32. {
  33. struct vrequest *usb_req;
  34. struct vep *ep0 = to_vep(udc->gadget.ep0);
  35. struct usb_device_descriptor *ddesc = &udc->dev_desc;
  36. struct usb_ctrlrequest req;
  37. int ret;
  38. if (!udc->driver || !udc->pullup)
  39. return -EINVAL;
  40. req.bRequestType = USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
  41. req.bRequest = USB_REQ_GET_DESCRIPTOR;
  42. req.wValue = cpu_to_le16(USB_DT_DEVICE << 8);
  43. req.wIndex = cpu_to_le16(0);
  44. req.wLength = cpu_to_le16(sizeof(*ddesc));
  45. spin_unlock(&udc->lock);
  46. ret = udc->driver->setup(&(udc->gadget), &req);
  47. spin_lock(&udc->lock);
  48. if (ret < 0)
  49. goto out;
  50. /* assuming request queue is empty; request is now on top */
  51. usb_req = list_last_entry(&ep0->req_queue, struct vrequest, req_entry);
  52. list_del(&usb_req->req_entry);
  53. if (usb_req->req.length > sizeof(*ddesc)) {
  54. ret = -EOVERFLOW;
  55. goto giveback_req;
  56. }
  57. memcpy(ddesc, usb_req->req.buf, sizeof(*ddesc));
  58. udc->desc_cached = 1;
  59. ret = 0;
  60. giveback_req:
  61. usb_req->req.status = 0;
  62. usb_req->req.actual = usb_req->req.length;
  63. usb_gadget_giveback_request(&(ep0->ep), &(usb_req->req));
  64. out:
  65. return ret;
  66. }
  67. /*
  68. * Exposes device descriptor from the gadget driver.
  69. */
  70. static ssize_t dev_desc_read(struct file *file, struct kobject *kobj,
  71. struct bin_attribute *attr, char *out,
  72. loff_t off, size_t count)
  73. {
  74. struct device *dev = kobj_to_dev(kobj);
  75. struct vudc *udc = (struct vudc *)dev_get_drvdata(dev);
  76. char *desc_ptr = (char *) &udc->dev_desc;
  77. unsigned long flags;
  78. int ret;
  79. spin_lock_irqsave(&udc->lock, flags);
  80. if (!udc->desc_cached) {
  81. ret = -ENODEV;
  82. goto unlock;
  83. }
  84. memcpy(out, desc_ptr + off, count);
  85. ret = count;
  86. unlock:
  87. spin_unlock_irqrestore(&udc->lock, flags);
  88. return ret;
  89. }
  90. static BIN_ATTR_RO(dev_desc, sizeof(struct usb_device_descriptor));
  91. static ssize_t store_sockfd(struct device *dev, struct device_attribute *attr,
  92. const char *in, size_t count)
  93. {
  94. struct vudc *udc = (struct vudc *) dev_get_drvdata(dev);
  95. int rv;
  96. int sockfd = 0;
  97. int err;
  98. struct socket *socket;
  99. unsigned long flags;
  100. int ret;
  101. rv = kstrtoint(in, 0, &sockfd);
  102. if (rv != 0)
  103. return -EINVAL;
  104. spin_lock_irqsave(&udc->lock, flags);
  105. /* Don't export what we don't have */
  106. if (!udc || !udc->driver || !udc->pullup) {
  107. dev_err(dev, "no device or gadget not bound");
  108. ret = -ENODEV;
  109. goto unlock;
  110. }
  111. if (sockfd != -1) {
  112. if (udc->connected) {
  113. dev_err(dev, "Device already connected");
  114. ret = -EBUSY;
  115. goto unlock;
  116. }
  117. spin_lock_irq(&udc->ud.lock);
  118. if (udc->ud.status != SDEV_ST_AVAILABLE) {
  119. ret = -EINVAL;
  120. goto unlock_ud;
  121. }
  122. socket = sockfd_lookup(sockfd, &err);
  123. if (!socket) {
  124. dev_err(dev, "failed to lookup sock");
  125. ret = -EINVAL;
  126. goto unlock_ud;
  127. }
  128. udc->ud.tcp_socket = socket;
  129. spin_unlock_irq(&udc->ud.lock);
  130. spin_unlock_irqrestore(&udc->lock, flags);
  131. udc->ud.tcp_rx = kthread_get_run(&v_rx_loop,
  132. &udc->ud, "vudc_rx");
  133. udc->ud.tcp_tx = kthread_get_run(&v_tx_loop,
  134. &udc->ud, "vudc_tx");
  135. spin_lock_irqsave(&udc->lock, flags);
  136. spin_lock_irq(&udc->ud.lock);
  137. udc->ud.status = SDEV_ST_USED;
  138. spin_unlock_irq(&udc->ud.lock);
  139. do_gettimeofday(&udc->start_time);
  140. v_start_timer(udc);
  141. udc->connected = 1;
  142. } else {
  143. if (!udc->connected) {
  144. dev_err(dev, "Device not connected");
  145. ret = -EINVAL;
  146. goto unlock;
  147. }
  148. spin_lock_irq(&udc->ud.lock);
  149. if (udc->ud.status != SDEV_ST_USED) {
  150. ret = -EINVAL;
  151. goto unlock_ud;
  152. }
  153. spin_unlock_irq(&udc->ud.lock);
  154. usbip_event_add(&udc->ud, VUDC_EVENT_DOWN);
  155. }
  156. spin_unlock_irqrestore(&udc->lock, flags);
  157. return count;
  158. unlock_ud:
  159. spin_unlock_irq(&udc->ud.lock);
  160. unlock:
  161. spin_unlock_irqrestore(&udc->lock, flags);
  162. return ret;
  163. }
  164. static DEVICE_ATTR(usbip_sockfd, S_IWUSR, NULL, store_sockfd);
  165. static ssize_t usbip_status_show(struct device *dev,
  166. struct device_attribute *attr, char *out)
  167. {
  168. struct vudc *udc = (struct vudc *) dev_get_drvdata(dev);
  169. int status;
  170. if (!udc) {
  171. dev_err(dev, "no device");
  172. return -ENODEV;
  173. }
  174. spin_lock_irq(&udc->ud.lock);
  175. status = udc->ud.status;
  176. spin_unlock_irq(&udc->ud.lock);
  177. return snprintf(out, PAGE_SIZE, "%d\n", status);
  178. }
  179. static DEVICE_ATTR_RO(usbip_status);
  180. static struct attribute *dev_attrs[] = {
  181. &dev_attr_usbip_sockfd.attr,
  182. &dev_attr_usbip_status.attr,
  183. NULL,
  184. };
  185. static struct bin_attribute *dev_bin_attrs[] = {
  186. &bin_attr_dev_desc,
  187. NULL,
  188. };
  189. const struct attribute_group vudc_attr_group = {
  190. .attrs = dev_attrs,
  191. .bin_attrs = dev_bin_attrs,
  192. };