dev-sysfs.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * WUSB devices
  3. * sysfs bindings
  4. *
  5. * Copyright (C) 2007 Intel Corporation
  6. * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License version
  10. * 2 as published by the Free Software Foundation.
  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, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  20. * 02110-1301, USA.
  21. *
  22. *
  23. * Get them out of the way...
  24. */
  25. #include <linux/jiffies.h>
  26. #include <linux/ctype.h>
  27. #include <linux/workqueue.h>
  28. #include "wusbhc.h"
  29. static ssize_t wusb_disconnect_store(struct device *dev,
  30. struct device_attribute *attr,
  31. const char *buf, size_t size)
  32. {
  33. struct usb_device *usb_dev;
  34. struct wusbhc *wusbhc;
  35. unsigned command;
  36. u8 port_idx;
  37. if (sscanf(buf, "%u", &command) != 1)
  38. return -EINVAL;
  39. if (command == 0)
  40. return size;
  41. usb_dev = to_usb_device(dev);
  42. wusbhc = wusbhc_get_by_usb_dev(usb_dev);
  43. if (wusbhc == NULL)
  44. return -ENODEV;
  45. mutex_lock(&wusbhc->mutex);
  46. port_idx = wusb_port_no_to_idx(usb_dev->portnum);
  47. __wusbhc_dev_disable(wusbhc, port_idx);
  48. mutex_unlock(&wusbhc->mutex);
  49. wusbhc_put(wusbhc);
  50. return size;
  51. }
  52. static DEVICE_ATTR(wusb_disconnect, 0200, NULL, wusb_disconnect_store);
  53. static ssize_t wusb_cdid_show(struct device *dev,
  54. struct device_attribute *attr, char *buf)
  55. {
  56. ssize_t result;
  57. struct wusb_dev *wusb_dev;
  58. wusb_dev = wusb_dev_get_by_usb_dev(to_usb_device(dev));
  59. if (wusb_dev == NULL)
  60. return -ENODEV;
  61. result = ckhdid_printf(buf, PAGE_SIZE, &wusb_dev->cdid);
  62. strcat(buf, "\n");
  63. wusb_dev_put(wusb_dev);
  64. return result + 1;
  65. }
  66. static DEVICE_ATTR(wusb_cdid, 0444, wusb_cdid_show, NULL);
  67. static ssize_t wusb_ck_store(struct device *dev,
  68. struct device_attribute *attr,
  69. const char *buf, size_t size)
  70. {
  71. int result;
  72. struct usb_device *usb_dev;
  73. struct wusbhc *wusbhc;
  74. struct wusb_ckhdid ck;
  75. result = sscanf(buf,
  76. "%02hhx %02hhx %02hhx %02hhx "
  77. "%02hhx %02hhx %02hhx %02hhx "
  78. "%02hhx %02hhx %02hhx %02hhx "
  79. "%02hhx %02hhx %02hhx %02hhx\n",
  80. &ck.data[0] , &ck.data[1],
  81. &ck.data[2] , &ck.data[3],
  82. &ck.data[4] , &ck.data[5],
  83. &ck.data[6] , &ck.data[7],
  84. &ck.data[8] , &ck.data[9],
  85. &ck.data[10], &ck.data[11],
  86. &ck.data[12], &ck.data[13],
  87. &ck.data[14], &ck.data[15]);
  88. if (result != 16)
  89. return -EINVAL;
  90. usb_dev = to_usb_device(dev);
  91. wusbhc = wusbhc_get_by_usb_dev(usb_dev);
  92. if (wusbhc == NULL)
  93. return -ENODEV;
  94. result = wusb_dev_4way_handshake(wusbhc, usb_dev->wusb_dev, &ck);
  95. memzero_explicit(&ck, sizeof(ck));
  96. wusbhc_put(wusbhc);
  97. return result < 0 ? result : size;
  98. }
  99. static DEVICE_ATTR(wusb_ck, 0200, NULL, wusb_ck_store);
  100. static struct attribute *wusb_dev_attrs[] = {
  101. &dev_attr_wusb_disconnect.attr,
  102. &dev_attr_wusb_cdid.attr,
  103. &dev_attr_wusb_ck.attr,
  104. NULL,
  105. };
  106. static struct attribute_group wusb_dev_attr_group = {
  107. .name = NULL, /* we want them in the same directory */
  108. .attrs = wusb_dev_attrs,
  109. };
  110. int wusb_dev_sysfs_add(struct wusbhc *wusbhc, struct usb_device *usb_dev,
  111. struct wusb_dev *wusb_dev)
  112. {
  113. int result = sysfs_create_group(&usb_dev->dev.kobj,
  114. &wusb_dev_attr_group);
  115. struct device *dev = &usb_dev->dev;
  116. if (result < 0)
  117. dev_err(dev, "Cannot register WUSB-dev attributes: %d\n",
  118. result);
  119. return result;
  120. }
  121. void wusb_dev_sysfs_rm(struct wusb_dev *wusb_dev)
  122. {
  123. struct usb_device *usb_dev = wusb_dev->usb_dev;
  124. if (usb_dev)
  125. sysfs_remove_group(&usb_dev->dev.kobj, &wusb_dev_attr_group);
  126. }