1 /***************************************************************************
2 * Copyright (C) 2010-2012 by Bruno Prémont <bonbons@linux-vserver.org> *
4 * Based on Logitech G13 driver (v0.4) *
5 * Copyright (C) 2009 by Rick L. Vinyard, Jr. <rvinyard@cs.nmsu.edu> *
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. *
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. *
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 ***************************************************************************/
20 #include <linux/hid.h>
21 #include <linux/hid-debug.h>
22 #include <linux/input.h>
26 #include <linux/vmalloc.h>
27 #include <linux/backlight.h>
28 #include <linux/lcd.h>
30 #include <linux/leds.h>
32 #include <linux/seq_file.h>
33 #include <linux/debugfs.h>
35 #include <linux/completion.h>
36 #include <linux/uaccess.h>
37 #include <linux/module.h>
39 #include "hid-picolcd.h"
42 void picolcd_leds_set(struct picolcd_data
*data
)
44 struct hid_report
*report
;
49 report
= picolcd_out_report(REPORT_LED_STATE
, data
->hdev
);
50 if (!report
|| report
->maxfield
!= 1 || report
->field
[0]->report_count
!= 1)
53 spin_lock_irqsave(&data
->lock
, flags
);
54 hid_set_field(report
->field
[0], 0, data
->led_state
);
55 if (!(data
->status
& PICOLCD_FAILED
))
56 hid_hw_request(data
->hdev
, report
, HID_REQ_SET_REPORT
);
57 spin_unlock_irqrestore(&data
->lock
, flags
);
60 static void picolcd_led_set_brightness(struct led_classdev
*led_cdev
,
61 enum led_brightness value
)
64 struct hid_device
*hdev
;
65 struct picolcd_data
*data
;
68 dev
= led_cdev
->dev
->parent
;
69 hdev
= container_of(dev
, struct hid_device
, dev
);
70 data
= hid_get_drvdata(hdev
);
73 for (i
= 0; i
< 8; i
++) {
74 if (led_cdev
!= data
->led
[i
])
76 state
= (data
->led_state
>> i
) & 1;
77 if (value
== LED_OFF
&& state
) {
78 data
->led_state
&= ~(1 << i
);
79 picolcd_leds_set(data
);
80 } else if (value
!= LED_OFF
&& !state
) {
81 data
->led_state
|= 1 << i
;
82 picolcd_leds_set(data
);
88 static enum led_brightness
picolcd_led_get_brightness(struct led_classdev
*led_cdev
)
91 struct hid_device
*hdev
;
92 struct picolcd_data
*data
;
95 dev
= led_cdev
->dev
->parent
;
96 hdev
= container_of(dev
, struct hid_device
, dev
);
97 data
= hid_get_drvdata(hdev
);
98 for (i
= 0; i
< 8; i
++)
99 if (led_cdev
== data
->led
[i
]) {
100 value
= (data
->led_state
>> i
) & 1;
103 return value
? LED_FULL
: LED_OFF
;
106 int picolcd_init_leds(struct picolcd_data
*data
, struct hid_report
*report
)
108 struct device
*dev
= &data
->hdev
->dev
;
109 struct led_classdev
*led
;
110 size_t name_sz
= strlen(dev_name(dev
)) + 8;
116 if (report
->maxfield
!= 1 || report
->field
[0]->report_count
!= 1 ||
117 report
->field
[0]->report_size
!= 8) {
118 dev_err(dev
, "unsupported LED_STATE report");
122 for (i
= 0; i
< 8; i
++) {
123 led
= kzalloc(sizeof(struct led_classdev
)+name_sz
, GFP_KERNEL
);
125 dev_err(dev
, "can't allocate memory for LED %d\n", i
);
129 name
= (void *)(&led
[1]);
130 snprintf(name
, name_sz
, "%s::GPO%d", dev_name(dev
), i
);
133 led
->max_brightness
= 1;
134 led
->brightness_get
= picolcd_led_get_brightness
;
135 led
->brightness_set
= picolcd_led_set_brightness
;
138 ret
= led_classdev_register(dev
, data
->led
[i
]);
142 dev_err(dev
, "can't register LED %d\n", i
);
148 for (i
= 0; i
< 8; i
++)
152 led_classdev_unregister(led
);
158 void picolcd_exit_leds(struct picolcd_data
*data
)
160 struct led_classdev
*led
;
163 for (i
= 0; i
< 8; i
++) {
168 led_classdev_unregister(led
);