0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <linux/device.h>
0010 #include <linux/list.h>
0011 #include <linux/usb/gadget.h>
0012 #include <linux/usb/ch9.h>
0013 #include <linux/sysfs.h>
0014 #include <linux/kthread.h>
0015 #include <linux/byteorder/generic.h>
0016
0017 #include "usbip_common.h"
0018 #include "vudc.h"
0019
0020 #include <net/sock.h>
0021
0022
0023 int get_gadget_descs(struct vudc *udc)
0024 {
0025 struct vrequest *usb_req;
0026 struct vep *ep0 = to_vep(udc->gadget.ep0);
0027 struct usb_device_descriptor *ddesc = &udc->dev_desc;
0028 struct usb_ctrlrequest req;
0029 int ret;
0030
0031 if (!udc->driver || !udc->pullup)
0032 return -EINVAL;
0033
0034 req.bRequestType = USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
0035 req.bRequest = USB_REQ_GET_DESCRIPTOR;
0036 req.wValue = cpu_to_le16(USB_DT_DEVICE << 8);
0037 req.wIndex = cpu_to_le16(0);
0038 req.wLength = cpu_to_le16(sizeof(*ddesc));
0039
0040 spin_unlock(&udc->lock);
0041 ret = udc->driver->setup(&(udc->gadget), &req);
0042 spin_lock(&udc->lock);
0043 if (ret < 0)
0044 goto out;
0045
0046
0047 usb_req = list_last_entry(&ep0->req_queue, struct vrequest, req_entry);
0048 list_del(&usb_req->req_entry);
0049
0050 if (usb_req->req.length > sizeof(*ddesc)) {
0051 ret = -EOVERFLOW;
0052 goto giveback_req;
0053 }
0054
0055 memcpy(ddesc, usb_req->req.buf, sizeof(*ddesc));
0056 udc->desc_cached = 1;
0057 ret = 0;
0058 giveback_req:
0059 usb_req->req.status = 0;
0060 usb_req->req.actual = usb_req->req.length;
0061 usb_gadget_giveback_request(&(ep0->ep), &(usb_req->req));
0062 out:
0063 return ret;
0064 }
0065
0066
0067
0068
0069 static ssize_t dev_desc_read(struct file *file, struct kobject *kobj,
0070 struct bin_attribute *attr, char *out,
0071 loff_t off, size_t count)
0072 {
0073 struct device *dev = kobj_to_dev(kobj);
0074 struct vudc *udc = (struct vudc *)dev_get_drvdata(dev);
0075 char *desc_ptr = (char *) &udc->dev_desc;
0076 unsigned long flags;
0077 int ret;
0078
0079 spin_lock_irqsave(&udc->lock, flags);
0080 if (!udc->desc_cached) {
0081 ret = -ENODEV;
0082 goto unlock;
0083 }
0084
0085 memcpy(out, desc_ptr + off, count);
0086 ret = count;
0087 unlock:
0088 spin_unlock_irqrestore(&udc->lock, flags);
0089 return ret;
0090 }
0091 static BIN_ATTR_RO(dev_desc, sizeof(struct usb_device_descriptor));
0092
0093 static ssize_t usbip_sockfd_store(struct device *dev,
0094 struct device_attribute *attr,
0095 const char *in, size_t count)
0096 {
0097 struct vudc *udc = (struct vudc *) dev_get_drvdata(dev);
0098 int rv;
0099 int sockfd = 0;
0100 int err;
0101 struct socket *socket;
0102 unsigned long flags;
0103 int ret;
0104 struct task_struct *tcp_rx = NULL;
0105 struct task_struct *tcp_tx = NULL;
0106
0107 rv = kstrtoint(in, 0, &sockfd);
0108 if (rv != 0)
0109 return -EINVAL;
0110
0111 if (!udc) {
0112 dev_err(dev, "no device");
0113 return -ENODEV;
0114 }
0115 mutex_lock(&udc->ud.sysfs_lock);
0116 spin_lock_irqsave(&udc->lock, flags);
0117
0118 if (!udc->driver || !udc->pullup) {
0119 dev_err(dev, "gadget not bound");
0120 ret = -ENODEV;
0121 goto unlock;
0122 }
0123
0124 if (sockfd != -1) {
0125 if (udc->connected) {
0126 dev_err(dev, "Device already connected");
0127 ret = -EBUSY;
0128 goto unlock;
0129 }
0130
0131 spin_lock(&udc->ud.lock);
0132
0133 if (udc->ud.status != SDEV_ST_AVAILABLE) {
0134 ret = -EINVAL;
0135 goto unlock_ud;
0136 }
0137
0138 socket = sockfd_lookup(sockfd, &err);
0139 if (!socket) {
0140 dev_err(dev, "failed to lookup sock");
0141 ret = -EINVAL;
0142 goto unlock_ud;
0143 }
0144
0145 if (socket->type != SOCK_STREAM) {
0146 dev_err(dev, "Expecting SOCK_STREAM - found %d",
0147 socket->type);
0148 ret = -EINVAL;
0149 goto sock_err;
0150 }
0151
0152
0153 spin_unlock(&udc->ud.lock);
0154 spin_unlock_irqrestore(&udc->lock, flags);
0155
0156 tcp_rx = kthread_create(&v_rx_loop, &udc->ud, "vudc_rx");
0157 if (IS_ERR(tcp_rx)) {
0158 sockfd_put(socket);
0159 mutex_unlock(&udc->ud.sysfs_lock);
0160 return -EINVAL;
0161 }
0162 tcp_tx = kthread_create(&v_tx_loop, &udc->ud, "vudc_tx");
0163 if (IS_ERR(tcp_tx)) {
0164 kthread_stop(tcp_rx);
0165 sockfd_put(socket);
0166 mutex_unlock(&udc->ud.sysfs_lock);
0167 return -EINVAL;
0168 }
0169
0170
0171 get_task_struct(tcp_rx);
0172 get_task_struct(tcp_tx);
0173
0174
0175 spin_lock_irqsave(&udc->lock, flags);
0176 spin_lock(&udc->ud.lock);
0177
0178 udc->ud.tcp_socket = socket;
0179 udc->ud.tcp_rx = tcp_rx;
0180 udc->ud.tcp_tx = tcp_tx;
0181 udc->ud.status = SDEV_ST_USED;
0182
0183 spin_unlock(&udc->ud.lock);
0184
0185 ktime_get_ts64(&udc->start_time);
0186 v_start_timer(udc);
0187 udc->connected = 1;
0188
0189 spin_unlock_irqrestore(&udc->lock, flags);
0190
0191 wake_up_process(udc->ud.tcp_rx);
0192 wake_up_process(udc->ud.tcp_tx);
0193
0194 mutex_unlock(&udc->ud.sysfs_lock);
0195 return count;
0196
0197 } else {
0198 if (!udc->connected) {
0199 dev_err(dev, "Device not connected");
0200 ret = -EINVAL;
0201 goto unlock;
0202 }
0203
0204 spin_lock(&udc->ud.lock);
0205 if (udc->ud.status != SDEV_ST_USED) {
0206 ret = -EINVAL;
0207 goto unlock_ud;
0208 }
0209 spin_unlock(&udc->ud.lock);
0210
0211 usbip_event_add(&udc->ud, VUDC_EVENT_DOWN);
0212 }
0213
0214 spin_unlock_irqrestore(&udc->lock, flags);
0215 mutex_unlock(&udc->ud.sysfs_lock);
0216
0217 return count;
0218
0219 sock_err:
0220 sockfd_put(socket);
0221 unlock_ud:
0222 spin_unlock(&udc->ud.lock);
0223 unlock:
0224 spin_unlock_irqrestore(&udc->lock, flags);
0225 mutex_unlock(&udc->ud.sysfs_lock);
0226
0227 return ret;
0228 }
0229 static DEVICE_ATTR_WO(usbip_sockfd);
0230
0231 static ssize_t usbip_status_show(struct device *dev,
0232 struct device_attribute *attr, char *out)
0233 {
0234 struct vudc *udc = (struct vudc *) dev_get_drvdata(dev);
0235 int status;
0236
0237 if (!udc) {
0238 dev_err(dev, "no device");
0239 return -ENODEV;
0240 }
0241 spin_lock_irq(&udc->ud.lock);
0242 status = udc->ud.status;
0243 spin_unlock_irq(&udc->ud.lock);
0244
0245 return snprintf(out, PAGE_SIZE, "%d\n", status);
0246 }
0247 static DEVICE_ATTR_RO(usbip_status);
0248
0249 static struct attribute *dev_attrs[] = {
0250 &dev_attr_usbip_sockfd.attr,
0251 &dev_attr_usbip_status.attr,
0252 NULL,
0253 };
0254
0255 static struct bin_attribute *dev_bin_attrs[] = {
0256 &bin_attr_dev_desc,
0257 NULL,
0258 };
0259
0260 static const struct attribute_group vudc_attr_group = {
0261 .attrs = dev_attrs,
0262 .bin_attrs = dev_bin_attrs,
0263 };
0264
0265 const struct attribute_group *vudc_groups[] = {
0266 &vudc_attr_group,
0267 NULL,
0268 };