hid-picolcd_leds.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /***************************************************************************
  2. * Copyright (C) 2010-2012 by Bruno Prémont <bonbons@linux-vserver.org> *
  3. * *
  4. * Based on Logitech G13 driver (v0.4) *
  5. * Copyright (C) 2009 by Rick L. Vinyard, Jr. <rvinyard@cs.nmsu.edu> *
  6. * *
  7. * This program is free software: you can redistribute it and/or modify *
  8. * it under the terms of the GNU General Public License as published by *
  9. * the Free Software Foundation, version 2 of the License. *
  10. * *
  11. * This driver is distributed in the hope that it will be useful, but *
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of *
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
  14. * General Public License for more details. *
  15. * *
  16. * You should have received a copy of the GNU General Public License *
  17. * along with this software. If not see <http://www.gnu.org/licenses/>. *
  18. ***************************************************************************/
  19. #include <linux/hid.h>
  20. #include <linux/hid-debug.h>
  21. #include <linux/input.h>
  22. #include "hid-ids.h"
  23. #include "usbhid/usbhid.h"
  24. #include <linux/usb.h>
  25. #include <linux/fb.h>
  26. #include <linux/vmalloc.h>
  27. #include <linux/backlight.h>
  28. #include <linux/lcd.h>
  29. #include <linux/leds.h>
  30. #include <linux/seq_file.h>
  31. #include <linux/debugfs.h>
  32. #include <linux/completion.h>
  33. #include <linux/uaccess.h>
  34. #include <linux/module.h>
  35. #include "hid-picolcd.h"
  36. void picolcd_leds_set(struct picolcd_data *data)
  37. {
  38. struct hid_report *report;
  39. unsigned long flags;
  40. if (!data->led[0])
  41. return;
  42. report = picolcd_out_report(REPORT_LED_STATE, data->hdev);
  43. if (!report || report->maxfield != 1 || report->field[0]->report_count != 1)
  44. return;
  45. spin_lock_irqsave(&data->lock, flags);
  46. hid_set_field(report->field[0], 0, data->led_state);
  47. usbhid_submit_report(data->hdev, report, USB_DIR_OUT);
  48. spin_unlock_irqrestore(&data->lock, flags);
  49. }
  50. static void picolcd_led_set_brightness(struct led_classdev *led_cdev,
  51. enum led_brightness value)
  52. {
  53. struct device *dev;
  54. struct hid_device *hdev;
  55. struct picolcd_data *data;
  56. int i, state = 0;
  57. dev = led_cdev->dev->parent;
  58. hdev = container_of(dev, struct hid_device, dev);
  59. data = hid_get_drvdata(hdev);
  60. for (i = 0; i < 8; i++) {
  61. if (led_cdev != data->led[i])
  62. continue;
  63. state = (data->led_state >> i) & 1;
  64. if (value == LED_OFF && state) {
  65. data->led_state &= ~(1 << i);
  66. picolcd_leds_set(data);
  67. } else if (value != LED_OFF && !state) {
  68. data->led_state |= 1 << i;
  69. picolcd_leds_set(data);
  70. }
  71. break;
  72. }
  73. }
  74. static enum led_brightness picolcd_led_get_brightness(struct led_classdev *led_cdev)
  75. {
  76. struct device *dev;
  77. struct hid_device *hdev;
  78. struct picolcd_data *data;
  79. int i, value = 0;
  80. dev = led_cdev->dev->parent;
  81. hdev = container_of(dev, struct hid_device, dev);
  82. data = hid_get_drvdata(hdev);
  83. for (i = 0; i < 8; i++)
  84. if (led_cdev == data->led[i]) {
  85. value = (data->led_state >> i) & 1;
  86. break;
  87. }
  88. return value ? LED_FULL : LED_OFF;
  89. }
  90. int picolcd_init_leds(struct picolcd_data *data, struct hid_report *report)
  91. {
  92. struct device *dev = &data->hdev->dev;
  93. struct led_classdev *led;
  94. size_t name_sz = strlen(dev_name(dev)) + 8;
  95. char *name;
  96. int i, ret = 0;
  97. if (!report)
  98. return -ENODEV;
  99. if (report->maxfield != 1 || report->field[0]->report_count != 1 ||
  100. report->field[0]->report_size != 8) {
  101. dev_err(dev, "unsupported LED_STATE report");
  102. return -EINVAL;
  103. }
  104. for (i = 0; i < 8; i++) {
  105. led = kzalloc(sizeof(struct led_classdev)+name_sz, GFP_KERNEL);
  106. if (!led) {
  107. dev_err(dev, "can't allocate memory for LED %d\n", i);
  108. ret = -ENOMEM;
  109. goto err;
  110. }
  111. name = (void *)(&led[1]);
  112. snprintf(name, name_sz, "%s::GPO%d", dev_name(dev), i);
  113. led->name = name;
  114. led->brightness = 0;
  115. led->max_brightness = 1;
  116. led->brightness_get = picolcd_led_get_brightness;
  117. led->brightness_set = picolcd_led_set_brightness;
  118. data->led[i] = led;
  119. ret = led_classdev_register(dev, data->led[i]);
  120. if (ret) {
  121. data->led[i] = NULL;
  122. kfree(led);
  123. dev_err(dev, "can't register LED %d\n", i);
  124. goto err;
  125. }
  126. }
  127. return 0;
  128. err:
  129. for (i = 0; i < 8; i++)
  130. if (data->led[i]) {
  131. led = data->led[i];
  132. data->led[i] = NULL;
  133. led_classdev_unregister(led);
  134. kfree(led);
  135. }
  136. return ret;
  137. }
  138. void picolcd_exit_leds(struct picolcd_data *data)
  139. {
  140. struct led_classdev *led;
  141. int i;
  142. for (i = 0; i < 8; i++) {
  143. led = data->led[i];
  144. data->led[i] = NULL;
  145. if (!led)
  146. continue;
  147. led_classdev_unregister(led);
  148. kfree(led);
  149. }
  150. }