dev-sysfs.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. #undef D_LOCAL
  30. #define D_LOCAL 4
  31. #include <linux/uwb/debug.h>
  32. static ssize_t wusb_disconnect_store(struct device *dev,
  33. struct device_attribute *attr,
  34. const char *buf, size_t size)
  35. {
  36. struct usb_device *usb_dev;
  37. struct wusbhc *wusbhc;
  38. unsigned command;
  39. u8 port_idx;
  40. if (sscanf(buf, "%u", &command) != 1)
  41. return -EINVAL;
  42. if (command == 0)
  43. return size;
  44. usb_dev = to_usb_device(dev);
  45. wusbhc = wusbhc_get_by_usb_dev(usb_dev);
  46. if (wusbhc == NULL)
  47. return -ENODEV;
  48. mutex_lock(&wusbhc->mutex);
  49. port_idx = wusb_port_no_to_idx(usb_dev->portnum);
  50. __wusbhc_dev_disable(wusbhc, port_idx);
  51. mutex_unlock(&wusbhc->mutex);
  52. wusbhc_put(wusbhc);
  53. return size;
  54. }
  55. static DEVICE_ATTR(wusb_disconnect, 0200, NULL, wusb_disconnect_store);
  56. static ssize_t wusb_cdid_show(struct device *dev,
  57. struct device_attribute *attr, char *buf)
  58. {
  59. ssize_t result;
  60. struct wusb_dev *wusb_dev;
  61. wusb_dev = wusb_dev_get_by_usb_dev(to_usb_device(dev));
  62. if (wusb_dev == NULL)
  63. return -ENODEV;
  64. result = ckhdid_printf(buf, PAGE_SIZE, &wusb_dev->cdid);
  65. strcat(buf, "\n");
  66. wusb_dev_put(wusb_dev);
  67. return result + 1;
  68. }
  69. static DEVICE_ATTR(wusb_cdid, 0444, wusb_cdid_show, NULL);
  70. static ssize_t wusb_ck_store(struct device *dev,
  71. struct device_attribute *attr,
  72. const char *buf, size_t size)
  73. {
  74. int result;
  75. struct usb_device *usb_dev;
  76. struct wusbhc *wusbhc;
  77. struct wusb_ckhdid ck;
  78. result = sscanf(buf,
  79. "%02hhx %02hhx %02hhx %02hhx "
  80. "%02hhx %02hhx %02hhx %02hhx "
  81. "%02hhx %02hhx %02hhx %02hhx "
  82. "%02hhx %02hhx %02hhx %02hhx\n",
  83. &ck.data[0] , &ck.data[1],
  84. &ck.data[2] , &ck.data[3],
  85. &ck.data[4] , &ck.data[5],
  86. &ck.data[6] , &ck.data[7],
  87. &ck.data[8] , &ck.data[9],
  88. &ck.data[10], &ck.data[11],
  89. &ck.data[12], &ck.data[13],
  90. &ck.data[14], &ck.data[15]);
  91. if (result != 16)
  92. return -EINVAL;
  93. usb_dev = to_usb_device(dev);
  94. wusbhc = wusbhc_get_by_usb_dev(usb_dev);
  95. if (wusbhc == NULL)
  96. return -ENODEV;
  97. result = wusb_dev_4way_handshake(wusbhc, usb_dev->wusb_dev, &ck);
  98. memset(&ck, 0, sizeof(ck));
  99. wusbhc_put(wusbhc);
  100. return result < 0 ? result : size;
  101. }
  102. static DEVICE_ATTR(wusb_ck, 0200, NULL, wusb_ck_store);
  103. static struct attribute *wusb_dev_attrs[] = {
  104. &dev_attr_wusb_disconnect.attr,
  105. &dev_attr_wusb_cdid.attr,
  106. &dev_attr_wusb_ck.attr,
  107. NULL,
  108. };
  109. static struct attribute_group wusb_dev_attr_group = {
  110. .name = NULL, /* we want them in the same directory */
  111. .attrs = wusb_dev_attrs,
  112. };
  113. int wusb_dev_sysfs_add(struct wusbhc *wusbhc, struct usb_device *usb_dev,
  114. struct wusb_dev *wusb_dev)
  115. {
  116. int result = sysfs_create_group(&usb_dev->dev.kobj,
  117. &wusb_dev_attr_group);
  118. struct device *dev = &usb_dev->dev;
  119. if (result < 0)
  120. dev_err(dev, "Cannot register WUSB-dev attributes: %d\n",
  121. result);
  122. return result;
  123. }
  124. void wusb_dev_sysfs_rm(struct wusb_dev *wusb_dev)
  125. {
  126. struct usb_device *usb_dev = wusb_dev->usb_dev;
  127. if (usb_dev)
  128. sysfs_remove_group(&usb_dev->dev.kobj, &wusb_dev_attr_group);
  129. }