0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #include <linux/init.h>
0012 #include <linux/kernel.h>
0013 #include <linux/module.h>
0014 #include <linux/platform_device.h>
0015 #include <linux/slab.h>
0016 #include <linux/usb.h>
0017 #include <linux/usb/ch11.h>
0018 #include <linux/usb/hcd.h>
0019 #include <linux/usb/phy.h>
0020
0021 struct lvs_rh {
0022
0023 struct usb_interface *intf;
0024
0025 bool present;
0026
0027 int portnum;
0028
0029 u8 buffer[8];
0030
0031 struct usb_hub_descriptor descriptor;
0032
0033 struct urb *urb;
0034
0035 struct work_struct rh_work;
0036
0037 struct usb_port_status port_status;
0038 };
0039
0040 static struct usb_device *create_lvs_device(struct usb_interface *intf)
0041 {
0042 struct usb_device *udev, *hdev;
0043 struct usb_hcd *hcd;
0044 struct lvs_rh *lvs = usb_get_intfdata(intf);
0045
0046 if (!lvs->present) {
0047 dev_err(&intf->dev, "No LVS device is present\n");
0048 return NULL;
0049 }
0050
0051 hdev = interface_to_usbdev(intf);
0052 hcd = bus_to_hcd(hdev->bus);
0053
0054 udev = usb_alloc_dev(hdev, hdev->bus, lvs->portnum);
0055 if (!udev) {
0056 dev_err(&intf->dev, "Could not allocate lvs udev\n");
0057 return NULL;
0058 }
0059 udev->speed = USB_SPEED_SUPER;
0060 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(512);
0061 usb_set_device_state(udev, USB_STATE_DEFAULT);
0062
0063 if (hcd->driver->enable_device) {
0064 if (hcd->driver->enable_device(hcd, udev) < 0) {
0065 dev_err(&intf->dev, "Failed to enable\n");
0066 usb_put_dev(udev);
0067 return NULL;
0068 }
0069 }
0070
0071 return udev;
0072 }
0073
0074 static void destroy_lvs_device(struct usb_device *udev)
0075 {
0076 struct usb_device *hdev = udev->parent;
0077 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
0078
0079 if (hcd->driver->free_dev)
0080 hcd->driver->free_dev(hcd, udev);
0081
0082 usb_put_dev(udev);
0083 }
0084
0085 static int lvs_rh_clear_port_feature(struct usb_device *hdev,
0086 int port1, int feature)
0087 {
0088 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
0089 USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature, port1,
0090 NULL, 0, 1000);
0091 }
0092
0093 static int lvs_rh_set_port_feature(struct usb_device *hdev,
0094 int port1, int feature)
0095 {
0096 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
0097 USB_REQ_SET_FEATURE, USB_RT_PORT, feature, port1,
0098 NULL, 0, 1000);
0099 }
0100
0101 static ssize_t u3_entry_store(struct device *dev,
0102 struct device_attribute *attr, const char *buf, size_t count)
0103 {
0104 struct usb_interface *intf = to_usb_interface(dev);
0105 struct usb_device *hdev = interface_to_usbdev(intf);
0106 struct lvs_rh *lvs = usb_get_intfdata(intf);
0107 struct usb_device *udev;
0108 int ret;
0109
0110 udev = create_lvs_device(intf);
0111 if (!udev) {
0112 dev_err(dev, "failed to create lvs device\n");
0113 return -ENOMEM;
0114 }
0115
0116 ret = lvs_rh_set_port_feature(hdev, lvs->portnum,
0117 USB_PORT_FEAT_SUSPEND);
0118 if (ret < 0)
0119 dev_err(dev, "can't issue U3 entry %d\n", ret);
0120
0121 destroy_lvs_device(udev);
0122
0123 if (ret < 0)
0124 return ret;
0125
0126 return count;
0127 }
0128 static DEVICE_ATTR_WO(u3_entry);
0129
0130 static ssize_t u3_exit_store(struct device *dev,
0131 struct device_attribute *attr, const char *buf, size_t count)
0132 {
0133 struct usb_interface *intf = to_usb_interface(dev);
0134 struct usb_device *hdev = interface_to_usbdev(intf);
0135 struct lvs_rh *lvs = usb_get_intfdata(intf);
0136 struct usb_device *udev;
0137 int ret;
0138
0139 udev = create_lvs_device(intf);
0140 if (!udev) {
0141 dev_err(dev, "failed to create lvs device\n");
0142 return -ENOMEM;
0143 }
0144
0145 ret = lvs_rh_clear_port_feature(hdev, lvs->portnum,
0146 USB_PORT_FEAT_SUSPEND);
0147 if (ret < 0)
0148 dev_err(dev, "can't issue U3 exit %d\n", ret);
0149
0150 destroy_lvs_device(udev);
0151
0152 if (ret < 0)
0153 return ret;
0154
0155 return count;
0156 }
0157 static DEVICE_ATTR_WO(u3_exit);
0158
0159 static ssize_t hot_reset_store(struct device *dev,
0160 struct device_attribute *attr, const char *buf, size_t count)
0161 {
0162 struct usb_interface *intf = to_usb_interface(dev);
0163 struct usb_device *hdev = interface_to_usbdev(intf);
0164 struct lvs_rh *lvs = usb_get_intfdata(intf);
0165 int ret;
0166
0167 ret = lvs_rh_set_port_feature(hdev, lvs->portnum,
0168 USB_PORT_FEAT_RESET);
0169 if (ret < 0) {
0170 dev_err(dev, "can't issue hot reset %d\n", ret);
0171 return ret;
0172 }
0173
0174 return count;
0175 }
0176 static DEVICE_ATTR_WO(hot_reset);
0177
0178 static ssize_t warm_reset_store(struct device *dev,
0179 struct device_attribute *attr, const char *buf, size_t count)
0180 {
0181 struct usb_interface *intf = to_usb_interface(dev);
0182 struct usb_device *hdev = interface_to_usbdev(intf);
0183 struct lvs_rh *lvs = usb_get_intfdata(intf);
0184 int ret;
0185
0186 ret = lvs_rh_set_port_feature(hdev, lvs->portnum,
0187 USB_PORT_FEAT_BH_PORT_RESET);
0188 if (ret < 0) {
0189 dev_err(dev, "can't issue warm reset %d\n", ret);
0190 return ret;
0191 }
0192
0193 return count;
0194 }
0195 static DEVICE_ATTR_WO(warm_reset);
0196
0197 static ssize_t u2_timeout_store(struct device *dev,
0198 struct device_attribute *attr, const char *buf, size_t count)
0199 {
0200 struct usb_interface *intf = to_usb_interface(dev);
0201 struct usb_device *hdev = interface_to_usbdev(intf);
0202 struct lvs_rh *lvs = usb_get_intfdata(intf);
0203 unsigned long val;
0204 int ret;
0205
0206 ret = kstrtoul(buf, 10, &val);
0207 if (ret < 0) {
0208 dev_err(dev, "couldn't parse string %d\n", ret);
0209 return ret;
0210 }
0211
0212 if (val > 127)
0213 return -EINVAL;
0214
0215 ret = lvs_rh_set_port_feature(hdev, lvs->portnum | (val << 8),
0216 USB_PORT_FEAT_U2_TIMEOUT);
0217 if (ret < 0) {
0218 dev_err(dev, "Error %d while setting U2 timeout %ld\n", ret, val);
0219 return ret;
0220 }
0221
0222 return count;
0223 }
0224 static DEVICE_ATTR_WO(u2_timeout);
0225
0226 static ssize_t u1_timeout_store(struct device *dev,
0227 struct device_attribute *attr, const char *buf, size_t count)
0228 {
0229 struct usb_interface *intf = to_usb_interface(dev);
0230 struct usb_device *hdev = interface_to_usbdev(intf);
0231 struct lvs_rh *lvs = usb_get_intfdata(intf);
0232 unsigned long val;
0233 int ret;
0234
0235 ret = kstrtoul(buf, 10, &val);
0236 if (ret < 0) {
0237 dev_err(dev, "couldn't parse string %d\n", ret);
0238 return ret;
0239 }
0240
0241 if (val > 127)
0242 return -EINVAL;
0243
0244 ret = lvs_rh_set_port_feature(hdev, lvs->portnum | (val << 8),
0245 USB_PORT_FEAT_U1_TIMEOUT);
0246 if (ret < 0) {
0247 dev_err(dev, "Error %d while setting U1 timeout %ld\n", ret, val);
0248 return ret;
0249 }
0250
0251 return count;
0252 }
0253 static DEVICE_ATTR_WO(u1_timeout);
0254
0255 static ssize_t get_dev_desc_store(struct device *dev,
0256 struct device_attribute *attr, const char *buf, size_t count)
0257 {
0258 struct usb_interface *intf = to_usb_interface(dev);
0259 struct usb_device *udev;
0260 struct usb_device_descriptor *descriptor;
0261 int ret;
0262
0263 descriptor = kmalloc(sizeof(*descriptor), GFP_KERNEL);
0264 if (!descriptor)
0265 return -ENOMEM;
0266
0267 udev = create_lvs_device(intf);
0268 if (!udev) {
0269 dev_err(dev, "failed to create lvs device\n");
0270 ret = -ENOMEM;
0271 goto free_desc;
0272 }
0273
0274 ret = usb_control_msg(udev, (PIPE_CONTROL << 30) | USB_DIR_IN,
0275 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN, USB_DT_DEVICE << 8,
0276 0, descriptor, sizeof(*descriptor),
0277 USB_CTRL_GET_TIMEOUT);
0278 if (ret < 0)
0279 dev_err(dev, "can't read device descriptor %d\n", ret);
0280
0281 destroy_lvs_device(udev);
0282
0283 free_desc:
0284 kfree(descriptor);
0285
0286 if (ret < 0)
0287 return ret;
0288
0289 return count;
0290 }
0291 static DEVICE_ATTR_WO(get_dev_desc);
0292
0293 static ssize_t enable_compliance_store(struct device *dev,
0294 struct device_attribute *attr, const char *buf, size_t count)
0295 {
0296 struct usb_interface *intf = to_usb_interface(dev);
0297 struct usb_device *hdev = interface_to_usbdev(intf);
0298 struct lvs_rh *lvs = usb_get_intfdata(intf);
0299 int ret;
0300
0301 ret = lvs_rh_set_port_feature(hdev,
0302 lvs->portnum | USB_SS_PORT_LS_COMP_MOD << 3,
0303 USB_PORT_FEAT_LINK_STATE);
0304 if (ret < 0) {
0305 dev_err(dev, "can't enable compliance mode %d\n", ret);
0306 return ret;
0307 }
0308
0309 return count;
0310 }
0311 static DEVICE_ATTR_WO(enable_compliance);
0312
0313 static struct attribute *lvs_attrs[] = {
0314 &dev_attr_get_dev_desc.attr,
0315 &dev_attr_u1_timeout.attr,
0316 &dev_attr_u2_timeout.attr,
0317 &dev_attr_hot_reset.attr,
0318 &dev_attr_warm_reset.attr,
0319 &dev_attr_u3_entry.attr,
0320 &dev_attr_u3_exit.attr,
0321 &dev_attr_enable_compliance.attr,
0322 NULL
0323 };
0324 ATTRIBUTE_GROUPS(lvs);
0325
0326 static void lvs_rh_work(struct work_struct *work)
0327 {
0328 struct lvs_rh *lvs = container_of(work, struct lvs_rh, rh_work);
0329 struct usb_interface *intf = lvs->intf;
0330 struct usb_device *hdev = interface_to_usbdev(intf);
0331 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
0332 struct usb_hub_descriptor *descriptor = &lvs->descriptor;
0333 struct usb_port_status *port_status = &lvs->port_status;
0334 int i, ret = 0;
0335 u16 portchange;
0336
0337
0338 for (i = 1; i <= descriptor->bNbrPorts; i++) {
0339 ret = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
0340 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, i,
0341 port_status, sizeof(*port_status), 1000);
0342 if (ret < 4)
0343 continue;
0344
0345 portchange = le16_to_cpu(port_status->wPortChange);
0346
0347 if (portchange & USB_PORT_STAT_C_LINK_STATE)
0348 lvs_rh_clear_port_feature(hdev, i,
0349 USB_PORT_FEAT_C_PORT_LINK_STATE);
0350 if (portchange & USB_PORT_STAT_C_ENABLE)
0351 lvs_rh_clear_port_feature(hdev, i,
0352 USB_PORT_FEAT_C_ENABLE);
0353 if (portchange & USB_PORT_STAT_C_RESET)
0354 lvs_rh_clear_port_feature(hdev, i,
0355 USB_PORT_FEAT_C_RESET);
0356 if (portchange & USB_PORT_STAT_C_BH_RESET)
0357 lvs_rh_clear_port_feature(hdev, i,
0358 USB_PORT_FEAT_C_BH_PORT_RESET);
0359 if (portchange & USB_PORT_STAT_C_CONNECTION) {
0360 lvs_rh_clear_port_feature(hdev, i,
0361 USB_PORT_FEAT_C_CONNECTION);
0362
0363 if (le16_to_cpu(port_status->wPortStatus) &
0364 USB_PORT_STAT_CONNECTION) {
0365 lvs->present = true;
0366 lvs->portnum = i;
0367 if (hcd->usb_phy)
0368 usb_phy_notify_connect(hcd->usb_phy,
0369 USB_SPEED_SUPER);
0370 } else {
0371 lvs->present = false;
0372 if (hcd->usb_phy)
0373 usb_phy_notify_disconnect(hcd->usb_phy,
0374 USB_SPEED_SUPER);
0375 }
0376 break;
0377 }
0378 }
0379
0380 ret = usb_submit_urb(lvs->urb, GFP_KERNEL);
0381 if (ret != 0 && ret != -ENODEV && ret != -EPERM)
0382 dev_err(&intf->dev, "urb resubmit error %d\n", ret);
0383 }
0384
0385 static void lvs_rh_irq(struct urb *urb)
0386 {
0387 struct lvs_rh *lvs = urb->context;
0388
0389 schedule_work(&lvs->rh_work);
0390 }
0391
0392 static int lvs_rh_probe(struct usb_interface *intf,
0393 const struct usb_device_id *id)
0394 {
0395 struct usb_device *hdev;
0396 struct usb_host_interface *desc;
0397 struct usb_endpoint_descriptor *endpoint;
0398 struct lvs_rh *lvs;
0399 unsigned int pipe;
0400 int ret, maxp;
0401
0402 hdev = interface_to_usbdev(intf);
0403 desc = intf->cur_altsetting;
0404
0405 ret = usb_find_int_in_endpoint(desc, &endpoint);
0406 if (ret)
0407 return ret;
0408
0409
0410 if (hdev->descriptor.bDeviceProtocol != USB_HUB_PR_SS || hdev->parent) {
0411 dev_err(&intf->dev, "Bind LVS driver with SS root Hub only\n");
0412 return -EINVAL;
0413 }
0414
0415 lvs = devm_kzalloc(&intf->dev, sizeof(*lvs), GFP_KERNEL);
0416 if (!lvs)
0417 return -ENOMEM;
0418
0419 lvs->intf = intf;
0420 usb_set_intfdata(intf, lvs);
0421
0422
0423 ret = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
0424 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB,
0425 USB_DT_SS_HUB << 8, 0, &lvs->descriptor,
0426 USB_DT_SS_HUB_SIZE, USB_CTRL_GET_TIMEOUT);
0427 if (ret < (USB_DT_HUB_NONVAR_SIZE + 2)) {
0428 dev_err(&hdev->dev, "wrong root hub descriptor read %d\n", ret);
0429 return ret < 0 ? ret : -EINVAL;
0430 }
0431
0432
0433 lvs->urb = usb_alloc_urb(0, GFP_KERNEL);
0434 if (!lvs->urb)
0435 return -ENOMEM;
0436
0437 INIT_WORK(&lvs->rh_work, lvs_rh_work);
0438
0439 pipe = usb_rcvintpipe(hdev, endpoint->bEndpointAddress);
0440 maxp = usb_maxpacket(hdev, pipe);
0441 usb_fill_int_urb(lvs->urb, hdev, pipe, &lvs->buffer[0], maxp,
0442 lvs_rh_irq, lvs, endpoint->bInterval);
0443
0444 ret = usb_submit_urb(lvs->urb, GFP_KERNEL);
0445 if (ret < 0) {
0446 dev_err(&intf->dev, "couldn't submit lvs urb %d\n", ret);
0447 goto free_urb;
0448 }
0449
0450 return ret;
0451
0452 free_urb:
0453 usb_free_urb(lvs->urb);
0454 return ret;
0455 }
0456
0457 static void lvs_rh_disconnect(struct usb_interface *intf)
0458 {
0459 struct lvs_rh *lvs = usb_get_intfdata(intf);
0460
0461 usb_poison_urb(lvs->urb);
0462 flush_work(&lvs->rh_work);
0463 usb_free_urb(lvs->urb);
0464 }
0465
0466 static struct usb_driver lvs_driver = {
0467 .name = "lvs",
0468 .probe = lvs_rh_probe,
0469 .disconnect = lvs_rh_disconnect,
0470 .dev_groups = lvs_groups,
0471 };
0472
0473 module_usb_driver(lvs_driver);
0474
0475 MODULE_DESCRIPTION("Link Layer Validation System Driver");
0476 MODULE_LICENSE("GPL");