vudc_sysfs.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. if (!udc) {
  105. dev_err(dev, "no device");
  106. return -ENODEV;
  107. }
  108. spin_lock_irqsave(&udc->lock, flags);
  109. /* Don't export what we don't have */
  110. if (!udc->driver || !udc->pullup) {
  111. dev_err(dev, "gadget not bound");
  112. ret = -ENODEV;
  113. goto unlock;
  114. }
  115. if (sockfd != -1) {
  116. if (udc->connected) {
  117. dev_err(dev, "Device already connected");
  118. ret = -EBUSY;
  119. goto unlock;
  120. }
  121. spin_lock_irq(&udc->ud.lock);
  122. if (udc->ud.status != SDEV_ST_AVAILABLE) {
  123. ret = -EINVAL;
  124. goto unlock_ud;
  125. }
  126. socket = sockfd_lookup(sockfd, &err);
  127. if (!socket) {
  128. dev_err(dev, "failed to lookup sock");
  129. ret = -EINVAL;
  130. goto unlock_ud;
  131. }
  132. udc->ud.tcp_socket = socket;
  133. spin_unlock_irq(&udc->ud.lock);
  134. spin_unlock_irqrestore(&udc->lock, flags);
  135. udc->ud.tcp_rx = kthread_get_run(&v_rx_loop,
  136. &udc->ud, "vudc_rx");
  137. udc->ud.tcp_tx = kthread_get_run(&v_tx_loop,
  138. &udc->ud, "vudc_tx");
  139. spin_lock_irqsave(&udc->lock, flags);
  140. spin_lock_irq(&udc->ud.lock);
  141. udc->ud.status = SDEV_ST_USED;
  142. spin_unlock_irq(&udc->ud.lock);
  143. do_gettimeofday(&udc->start_time);
  144. v_start_timer(udc);
  145. udc->connected = 1;
  146. } else {
  147. if (!udc->connected) {
  148. dev_err(dev, "Device not connected");
  149. ret = -EINVAL;
  150. goto unlock;
  151. }
  152. spin_lock_irq(&udc->ud.lock);
  153. if (udc->ud.status != SDEV_ST_USED) {
  154. ret = -EINVAL;
  155. goto unlock_ud;
  156. }
  157. spin_unlock_irq(&udc->ud.lock);
  158. usbip_event_add(&udc->ud, VUDC_EVENT_DOWN);
  159. }
  160. spin_unlock_irqrestore(&udc->lock, flags);
  161. return count;
  162. unlock_ud:
  163. spin_unlock_irq(&udc->ud.lock);
  164. unlock:
  165. spin_unlock_irqrestore(&udc->lock, flags);
  166. return ret;
  167. }
  168. static DEVICE_ATTR(usbip_sockfd, S_IWUSR, NULL, store_sockfd);
  169. static ssize_t usbip_status_show(struct device *dev,
  170. struct device_attribute *attr, char *out)
  171. {
  172. struct vudc *udc = (struct vudc *) dev_get_drvdata(dev);
  173. int status;
  174. if (!udc) {
  175. dev_err(dev, "no device");
  176. return -ENODEV;
  177. }
  178. spin_lock_irq(&udc->ud.lock);
  179. status = udc->ud.status;
  180. spin_unlock_irq(&udc->ud.lock);
  181. return snprintf(out, PAGE_SIZE, "%d\n", status);
  182. }
  183. static DEVICE_ATTR_RO(usbip_status);
  184. static struct attribute *dev_attrs[] = {
  185. &dev_attr_usbip_sockfd.attr,
  186. &dev_attr_usbip_status.attr,
  187. NULL,
  188. };
  189. static struct bin_attribute *dev_bin_attrs[] = {
  190. &bin_attr_dev_desc,
  191. NULL,
  192. };
  193. const struct attribute_group vudc_attr_group = {
  194. .attrs = dev_attrs,
  195. .bin_attrs = dev_bin_attrs,
  196. };