hid-picolcd_leds.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. if (!(data->status & PICOLCD_FAILED))
  48. usbhid_submit_report(data->hdev, report, USB_DIR_OUT);
  49. spin_unlock_irqrestore(&data->lock, flags);
  50. }
  51. static void picolcd_led_set_brightness(struct led_classdev *led_cdev,
  52. enum led_brightness value)
  53. {
  54. struct device *dev;
  55. struct hid_device *hdev;
  56. struct picolcd_data *data;
  57. int i, state = 0;
  58. dev = led_cdev->dev->parent;
  59. hdev = container_of(dev, struct hid_device, dev);
  60. data = hid_get_drvdata(hdev);
  61. if (!data)
  62. return;
  63. for (i = 0; i < 8; i++) {
  64. if (led_cdev != data->led[i])
  65. continue;
  66. state = (data->led_state >> i) & 1;
  67. if (value == LED_OFF && state) {
  68. data->led_state &= ~(1 << i);
  69. picolcd_leds_set(data);
  70. } else if (value != LED_OFF && !state) {
  71. data->led_state |= 1 << i;
  72. picolcd_leds_set(data);
  73. }
  74. break;
  75. }
  76. }
  77. static enum led_brightness picolcd_led_get_brightness(struct led_classdev *led_cdev)
  78. {
  79. struct device *dev;
  80. struct hid_device *hdev;
  81. struct picolcd_data *data;
  82. int i, value = 0;
  83. dev = led_cdev->dev->parent;
  84. hdev = container_of(dev, struct hid_device, dev);
  85. data = hid_get_drvdata(hdev);
  86. for (i = 0; i < 8; i++)
  87. if (led_cdev == data->led[i]) {
  88. value = (data->led_state >> i) & 1;
  89. break;
  90. }
  91. return value ? LED_FULL : LED_OFF;
  92. }
  93. int picolcd_init_leds(struct picolcd_data *data, struct hid_report *report)
  94. {
  95. struct device *dev = &data->hdev->dev;
  96. struct led_classdev *led;
  97. size_t name_sz = strlen(dev_name(dev)) + 8;
  98. char *name;
  99. int i, ret = 0;
  100. if (!report)
  101. return -ENODEV;
  102. if (report->maxfield != 1 || report->field[0]->report_count != 1 ||
  103. report->field[0]->report_size != 8) {
  104. dev_err(dev, "unsupported LED_STATE report");
  105. return -EINVAL;
  106. }
  107. for (i = 0; i < 8; i++) {
  108. led = kzalloc(sizeof(struct led_classdev)+name_sz, GFP_KERNEL);
  109. if (!led) {
  110. dev_err(dev, "can't allocate memory for LED %d\n", i);
  111. ret = -ENOMEM;
  112. goto err;
  113. }
  114. name = (void *)(&led[1]);
  115. snprintf(name, name_sz, "%s::GPO%d", dev_name(dev), i);
  116. led->name = name;
  117. led->brightness = 0;
  118. led->max_brightness = 1;
  119. led->brightness_get = picolcd_led_get_brightness;
  120. led->brightness_set = picolcd_led_set_brightness;
  121. data->led[i] = led;
  122. ret = led_classdev_register(dev, data->led[i]);
  123. if (ret) {
  124. data->led[i] = NULL;
  125. kfree(led);
  126. dev_err(dev, "can't register LED %d\n", i);
  127. goto err;
  128. }
  129. }
  130. return 0;
  131. err:
  132. for (i = 0; i < 8; i++)
  133. if (data->led[i]) {
  134. led = data->led[i];
  135. data->led[i] = NULL;
  136. led_classdev_unregister(led);
  137. kfree(led);
  138. }
  139. return ret;
  140. }
  141. void picolcd_exit_leds(struct picolcd_data *data)
  142. {
  143. struct led_classdev *led;
  144. int i;
  145. for (i = 0; i < 8; i++) {
  146. led = data->led[i];
  147. data->led[i] = NULL;
  148. if (!led)
  149. continue;
  150. led_classdev_unregister(led);
  151. kfree(led);
  152. }
  153. }