hid-picolcd_leds.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 <linux/fb.h>
  24. #include <linux/vmalloc.h>
  25. #include <linux/backlight.h>
  26. #include <linux/lcd.h>
  27. #include <linux/leds.h>
  28. #include <linux/seq_file.h>
  29. #include <linux/debugfs.h>
  30. #include <linux/completion.h>
  31. #include <linux/uaccess.h>
  32. #include <linux/module.h>
  33. #include "hid-picolcd.h"
  34. void picolcd_leds_set(struct picolcd_data *data)
  35. {
  36. struct hid_report *report;
  37. unsigned long flags;
  38. if (!data->led[0])
  39. return;
  40. report = picolcd_out_report(REPORT_LED_STATE, data->hdev);
  41. if (!report || report->maxfield != 1 || report->field[0]->report_count != 1)
  42. return;
  43. spin_lock_irqsave(&data->lock, flags);
  44. hid_set_field(report->field[0], 0, data->led_state);
  45. if (!(data->status & PICOLCD_FAILED))
  46. hid_hw_request(data->hdev, report, HID_REQ_SET_REPORT);
  47. spin_unlock_irqrestore(&data->lock, flags);
  48. }
  49. static void picolcd_led_set_brightness(struct led_classdev *led_cdev,
  50. enum led_brightness value)
  51. {
  52. struct device *dev;
  53. struct hid_device *hdev;
  54. struct picolcd_data *data;
  55. int i, state = 0;
  56. dev = led_cdev->dev->parent;
  57. hdev = to_hid_device(dev);
  58. data = hid_get_drvdata(hdev);
  59. if (!data)
  60. return;
  61. for (i = 0; i < 8; i++) {
  62. if (led_cdev != data->led[i])
  63. continue;
  64. state = (data->led_state >> i) & 1;
  65. if (value == LED_OFF && state) {
  66. data->led_state &= ~(1 << i);
  67. picolcd_leds_set(data);
  68. } else if (value != LED_OFF && !state) {
  69. data->led_state |= 1 << i;
  70. picolcd_leds_set(data);
  71. }
  72. break;
  73. }
  74. }
  75. static enum led_brightness picolcd_led_get_brightness(struct led_classdev *led_cdev)
  76. {
  77. struct device *dev;
  78. struct hid_device *hdev;
  79. struct picolcd_data *data;
  80. int i, value = 0;
  81. dev = led_cdev->dev->parent;
  82. hdev = to_hid_device(dev);
  83. data = hid_get_drvdata(hdev);
  84. for (i = 0; i < 8; i++)
  85. if (led_cdev == data->led[i]) {
  86. value = (data->led_state >> i) & 1;
  87. break;
  88. }
  89. return value ? LED_FULL : LED_OFF;
  90. }
  91. int picolcd_init_leds(struct picolcd_data *data, struct hid_report *report)
  92. {
  93. struct device *dev = &data->hdev->dev;
  94. struct led_classdev *led;
  95. size_t name_sz = strlen(dev_name(dev)) + 8;
  96. char *name;
  97. int i, ret = 0;
  98. if (!report)
  99. return -ENODEV;
  100. if (report->maxfield != 1 || report->field[0]->report_count != 1 ||
  101. report->field[0]->report_size != 8) {
  102. dev_err(dev, "unsupported LED_STATE report");
  103. return -EINVAL;
  104. }
  105. for (i = 0; i < 8; i++) {
  106. led = kzalloc(sizeof(struct led_classdev)+name_sz, GFP_KERNEL);
  107. if (!led) {
  108. dev_err(dev, "can't allocate memory for LED %d\n", i);
  109. ret = -ENOMEM;
  110. goto err;
  111. }
  112. name = (void *)(&led[1]);
  113. snprintf(name, name_sz, "%s::GPO%d", dev_name(dev), i);
  114. led->name = name;
  115. led->brightness = 0;
  116. led->max_brightness = 1;
  117. led->brightness_get = picolcd_led_get_brightness;
  118. led->brightness_set = picolcd_led_set_brightness;
  119. data->led[i] = led;
  120. ret = led_classdev_register(dev, data->led[i]);
  121. if (ret) {
  122. data->led[i] = NULL;
  123. kfree(led);
  124. dev_err(dev, "can't register LED %d\n", i);
  125. goto err;
  126. }
  127. }
  128. return 0;
  129. err:
  130. for (i = 0; i < 8; i++)
  131. if (data->led[i]) {
  132. led = data->led[i];
  133. data->led[i] = NULL;
  134. led_classdev_unregister(led);
  135. kfree(led);
  136. }
  137. return ret;
  138. }
  139. void picolcd_exit_leds(struct picolcd_data *data)
  140. {
  141. struct led_classdev *led;
  142. int i;
  143. for (i = 0; i < 8; i++) {
  144. led = data->led[i];
  145. data->led[i] = NULL;
  146. if (!led)
  147. continue;
  148. led_classdev_unregister(led);
  149. kfree(led);
  150. }
  151. }