dev-sysfs.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * WUSB devices
  4. * sysfs bindings
  5. *
  6. * Copyright (C) 2007 Intel Corporation
  7. * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
  8. *
  9. * Get them out of the way...
  10. */
  11. #include <linux/jiffies.h>
  12. #include <linux/ctype.h>
  13. #include <linux/workqueue.h>
  14. #include "wusbhc.h"
  15. static ssize_t wusb_disconnect_store(struct device *dev,
  16. struct device_attribute *attr,
  17. const char *buf, size_t size)
  18. {
  19. struct usb_device *usb_dev;
  20. struct wusbhc *wusbhc;
  21. unsigned command;
  22. u8 port_idx;
  23. if (sscanf(buf, "%u", &command) != 1)
  24. return -EINVAL;
  25. if (command == 0)
  26. return size;
  27. usb_dev = to_usb_device(dev);
  28. wusbhc = wusbhc_get_by_usb_dev(usb_dev);
  29. if (wusbhc == NULL)
  30. return -ENODEV;
  31. mutex_lock(&wusbhc->mutex);
  32. port_idx = wusb_port_no_to_idx(usb_dev->portnum);
  33. __wusbhc_dev_disable(wusbhc, port_idx);
  34. mutex_unlock(&wusbhc->mutex);
  35. wusbhc_put(wusbhc);
  36. return size;
  37. }
  38. static DEVICE_ATTR_WO(wusb_disconnect);
  39. static ssize_t wusb_cdid_show(struct device *dev,
  40. struct device_attribute *attr, char *buf)
  41. {
  42. ssize_t result;
  43. struct wusb_dev *wusb_dev;
  44. wusb_dev = wusb_dev_get_by_usb_dev(to_usb_device(dev));
  45. if (wusb_dev == NULL)
  46. return -ENODEV;
  47. result = ckhdid_printf(buf, PAGE_SIZE, &wusb_dev->cdid);
  48. strcat(buf, "\n");
  49. wusb_dev_put(wusb_dev);
  50. return result + 1;
  51. }
  52. static DEVICE_ATTR_RO(wusb_cdid);
  53. static ssize_t wusb_ck_store(struct device *dev,
  54. struct device_attribute *attr,
  55. const char *buf, size_t size)
  56. {
  57. int result;
  58. struct usb_device *usb_dev;
  59. struct wusbhc *wusbhc;
  60. struct wusb_ckhdid ck;
  61. result = sscanf(buf,
  62. "%02hhx %02hhx %02hhx %02hhx "
  63. "%02hhx %02hhx %02hhx %02hhx "
  64. "%02hhx %02hhx %02hhx %02hhx "
  65. "%02hhx %02hhx %02hhx %02hhx\n",
  66. &ck.data[0] , &ck.data[1],
  67. &ck.data[2] , &ck.data[3],
  68. &ck.data[4] , &ck.data[5],
  69. &ck.data[6] , &ck.data[7],
  70. &ck.data[8] , &ck.data[9],
  71. &ck.data[10], &ck.data[11],
  72. &ck.data[12], &ck.data[13],
  73. &ck.data[14], &ck.data[15]);
  74. if (result != 16)
  75. return -EINVAL;
  76. usb_dev = to_usb_device(dev);
  77. wusbhc = wusbhc_get_by_usb_dev(usb_dev);
  78. if (wusbhc == NULL)
  79. return -ENODEV;
  80. result = wusb_dev_4way_handshake(wusbhc, usb_dev->wusb_dev, &ck);
  81. memzero_explicit(&ck, sizeof(ck));
  82. wusbhc_put(wusbhc);
  83. return result < 0 ? result : size;
  84. }
  85. static DEVICE_ATTR_WO(wusb_ck);
  86. static struct attribute *wusb_dev_attrs[] = {
  87. &dev_attr_wusb_disconnect.attr,
  88. &dev_attr_wusb_cdid.attr,
  89. &dev_attr_wusb_ck.attr,
  90. NULL,
  91. };
  92. static const struct attribute_group wusb_dev_attr_group = {
  93. .name = NULL, /* we want them in the same directory */
  94. .attrs = wusb_dev_attrs,
  95. };
  96. int wusb_dev_sysfs_add(struct wusbhc *wusbhc, struct usb_device *usb_dev,
  97. struct wusb_dev *wusb_dev)
  98. {
  99. int result = sysfs_create_group(&usb_dev->dev.kobj,
  100. &wusb_dev_attr_group);
  101. struct device *dev = &usb_dev->dev;
  102. if (result < 0)
  103. dev_err(dev, "Cannot register WUSB-dev attributes: %d\n",
  104. result);
  105. return result;
  106. }
  107. void wusb_dev_sysfs_rm(struct wusb_dev *wusb_dev)
  108. {
  109. struct usb_device *usb_dev = wusb_dev->usb_dev;
  110. if (usb_dev)
  111. sysfs_remove_group(&usb_dev->dev.kobj, &wusb_dev_attr_group);
  112. }