blob: 9249c068a4851fdfd8a3b77ff8152d40f52a52b9 [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);
72 for (i = 0; i < 8; i++) {
73 if (led_cdev != data->led[i])
74 continue;
75 state = (data->led_state >> i) & 1;
76 if (value == LED_OFF && state) {
77 data->led_state &= ~(1 << i);
78 picolcd_leds_set(data);
79 } else if (value != LED_OFF && !state) {
80 data->led_state |= 1 << i;
81 picolcd_leds_set(data);
82 }
83 break;
84 }
85}
86
87static enum led_brightness picolcd_led_get_brightness(struct led_classdev *led_cdev)
88{
89 struct device *dev;
90 struct hid_device *hdev;
91 struct picolcd_data *data;
92 int i, value = 0;
93
94 dev = led_cdev->dev->parent;
95 hdev = container_of(dev, struct hid_device, dev);
96 data = hid_get_drvdata(hdev);
97 for (i = 0; i < 8; i++)
98 if (led_cdev == data->led[i]) {
99 value = (data->led_state >> i) & 1;
100 break;
101 }
102 return value ? LED_FULL : LED_OFF;
103}
104
105int picolcd_init_leds(struct picolcd_data *data, struct hid_report *report)
106{
107 struct device *dev = &data->hdev->dev;
108 struct led_classdev *led;
109 size_t name_sz = strlen(dev_name(dev)) + 8;
110 char *name;
111 int i, ret = 0;
112
113 if (!report)
114 return -ENODEV;
115 if (report->maxfield != 1 || report->field[0]->report_count != 1 ||
116 report->field[0]->report_size != 8) {
117 dev_err(dev, "unsupported LED_STATE report");
118 return -EINVAL;
119 }
120
121 for (i = 0; i < 8; i++) {
122 led = kzalloc(sizeof(struct led_classdev)+name_sz, GFP_KERNEL);
123 if (!led) {
124 dev_err(dev, "can't allocate memory for LED %d\n", i);
125 ret = -ENOMEM;
126 goto err;
127 }
128 name = (void *)(&led[1]);
129 snprintf(name, name_sz, "%s::GPO%d", dev_name(dev), i);
130 led->name = name;
131 led->brightness = 0;
132 led->max_brightness = 1;
133 led->brightness_get = picolcd_led_get_brightness;
134 led->brightness_set = picolcd_led_set_brightness;
135
136 data->led[i] = led;
137 ret = led_classdev_register(dev, data->led[i]);
138 if (ret) {
139 data->led[i] = NULL;
140 kfree(led);
141 dev_err(dev, "can't register LED %d\n", i);
142 goto err;
143 }
144 }
145 return 0;
146err:
147 for (i = 0; i < 8; i++)
148 if (data->led[i]) {
149 led = data->led[i];
150 data->led[i] = NULL;
151 led_classdev_unregister(led);
152 kfree(led);
153 }
154 return ret;
155}
156
157void picolcd_exit_leds(struct picolcd_data *data)
158{
159 struct led_classdev *led;
160 int i;
161
162 for (i = 0; i < 8; i++) {
163 led = data->led[i];
164 data->led[i] = NULL;
165 if (!led)
166 continue;
167 led_classdev_unregister(led);
168 kfree(led);
169 }
170}
171
172