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