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>
24 #include "usbhid/usbhid.h"
25 #include <linux/usb.h>
28 #include <linux/vmalloc.h>
30 #include <linux/completion.h>
31 #include <linux/uaccess.h>
32 #include <linux/module.h>
34 #include "hid-picolcd.h"
39 * The PicoLCD has an IR receiver header, a built-in keypad with 5 keys
40 * and header for 4x4 key matrix. The built-in keys are part of the matrix.
42 static const unsigned short def_keymap
[PICOLCD_KEYS
] = {
43 KEY_RESERVED
, /* none */
44 KEY_BACK
, /* col 4 + row 1 */
45 KEY_HOMEPAGE
, /* col 3 + row 1 */
46 KEY_RESERVED
, /* col 2 + row 1 */
47 KEY_RESERVED
, /* col 1 + row 1 */
48 KEY_SCROLLUP
, /* col 4 + row 2 */
49 KEY_OK
, /* col 3 + row 2 */
50 KEY_SCROLLDOWN
, /* col 2 + row 2 */
51 KEY_RESERVED
, /* col 1 + row 2 */
52 KEY_RESERVED
, /* col 4 + row 3 */
53 KEY_RESERVED
, /* col 3 + row 3 */
54 KEY_RESERVED
, /* col 2 + row 3 */
55 KEY_RESERVED
, /* col 1 + row 3 */
56 KEY_RESERVED
, /* col 4 + row 4 */
57 KEY_RESERVED
, /* col 3 + row 4 */
58 KEY_RESERVED
, /* col 2 + row 4 */
59 KEY_RESERVED
, /* col 1 + row 4 */
63 /* Find a given report */
64 struct hid_report
*picolcd_report(int id
, struct hid_device
*hdev
, int dir
)
66 struct list_head
*feature_report_list
= &hdev
->report_enum
[dir
].report_list
;
67 struct hid_report
*report
= NULL
;
69 list_for_each_entry(report
, feature_report_list
, list
) {
73 hid_warn(hdev
, "No report with id 0x%x found\n", id
);
77 /* Submit a report and wait for a reply from device - if device fades away
78 * or does not respond in time, return NULL */
79 struct picolcd_pending
*picolcd_send_and_wait(struct hid_device
*hdev
,
80 int report_id
, const u8
*raw_data
, int size
)
82 struct picolcd_data
*data
= hid_get_drvdata(hdev
);
83 struct picolcd_pending
*work
;
84 struct hid_report
*report
= picolcd_out_report(report_id
, hdev
);
90 if (data
->status
& PICOLCD_FAILED
)
92 work
= kzalloc(sizeof(*work
), GFP_KERNEL
);
96 init_completion(&work
->ready
);
97 work
->out_report
= report
;
98 work
->in_report
= NULL
;
101 mutex_lock(&data
->mutex
);
102 spin_lock_irqsave(&data
->lock
, flags
);
103 for (i
= k
= 0; i
< report
->maxfield
; i
++)
104 for (j
= 0; j
< report
->field
[i
]->report_count
; j
++) {
105 hid_set_field(report
->field
[i
], j
, k
< size
? raw_data
[k
] : 0);
108 if (data
->status
& PICOLCD_FAILED
) {
112 data
->pending
= work
;
113 usbhid_submit_report(data
->hdev
, report
, USB_DIR_OUT
);
114 spin_unlock_irqrestore(&data
->lock
, flags
);
115 wait_for_completion_interruptible_timeout(&work
->ready
, HZ
*2);
116 spin_lock_irqsave(&data
->lock
, flags
);
117 data
->pending
= NULL
;
119 spin_unlock_irqrestore(&data
->lock
, flags
);
120 mutex_unlock(&data
->mutex
);
127 static int picolcd_raw_keypad(struct picolcd_data
*data
,
128 struct hid_report
*report
, u8
*raw_data
, int size
)
132 * First and second data bytes list currently pressed keys,
133 * 0x00 means no key and at most 2 keys may be pressed at same time
137 /* determine newly pressed keys */
138 for (i
= 0; i
< size
; i
++) {
139 unsigned int key_code
;
140 if (raw_data
[i
] == 0)
142 for (j
= 0; j
< sizeof(data
->pressed_keys
); j
++)
143 if (data
->pressed_keys
[j
] == raw_data
[i
])
144 goto key_already_down
;
145 for (j
= 0; j
< sizeof(data
->pressed_keys
); j
++)
146 if (data
->pressed_keys
[j
] == 0) {
147 data
->pressed_keys
[j
] = raw_data
[i
];
150 input_event(data
->input_keys
, EV_MSC
, MSC_SCAN
, raw_data
[i
]);
151 if (raw_data
[i
] < PICOLCD_KEYS
)
152 key_code
= data
->keycode
[raw_data
[i
]];
154 key_code
= KEY_UNKNOWN
;
155 if (key_code
!= KEY_UNKNOWN
) {
156 dbg_hid(PICOLCD_NAME
" got key press for %u:%d",
157 raw_data
[i
], key_code
);
158 input_report_key(data
->input_keys
, key_code
, 1);
160 input_sync(data
->input_keys
);
165 /* determine newly released keys */
166 for (j
= 0; j
< sizeof(data
->pressed_keys
); j
++) {
167 unsigned int key_code
;
168 if (data
->pressed_keys
[j
] == 0)
170 for (i
= 0; i
< size
; i
++)
171 if (data
->pressed_keys
[j
] == raw_data
[i
])
173 input_event(data
->input_keys
, EV_MSC
, MSC_SCAN
, data
->pressed_keys
[j
]);
174 if (data
->pressed_keys
[j
] < PICOLCD_KEYS
)
175 key_code
= data
->keycode
[data
->pressed_keys
[j
]];
177 key_code
= KEY_UNKNOWN
;
178 if (key_code
!= KEY_UNKNOWN
) {
179 dbg_hid(PICOLCD_NAME
" got key release for %u:%d",
180 data
->pressed_keys
[j
], key_code
);
181 input_report_key(data
->input_keys
, key_code
, 0);
183 input_sync(data
->input_keys
);
184 data
->pressed_keys
[j
] = 0;
191 static int picolcd_check_version(struct hid_device
*hdev
)
193 struct picolcd_data
*data
= hid_get_drvdata(hdev
);
194 struct picolcd_pending
*verinfo
;
200 verinfo
= picolcd_send_and_wait(hdev
, REPORT_VERSION
, NULL
, 0);
202 hid_err(hdev
, "no version response from PicoLCD\n");
206 if (verinfo
->raw_size
== 2) {
207 data
->version
[0] = verinfo
->raw_data
[1];
208 data
->version
[1] = verinfo
->raw_data
[0];
209 if (data
->status
& PICOLCD_BOOTLOADER
) {
210 hid_info(hdev
, "PicoLCD, bootloader version %d.%d\n",
211 verinfo
->raw_data
[1], verinfo
->raw_data
[0]);
213 hid_info(hdev
, "PicoLCD, firmware version %d.%d\n",
214 verinfo
->raw_data
[1], verinfo
->raw_data
[0]);
217 hid_err(hdev
, "confused, got unexpected version response from PicoLCD\n");
225 * Reset our device and wait for answer to VERSION request
227 int picolcd_reset(struct hid_device
*hdev
)
229 struct picolcd_data
*data
= hid_get_drvdata(hdev
);
230 struct hid_report
*report
= picolcd_out_report(REPORT_RESET
, hdev
);
234 if (!data
|| !report
|| report
->maxfield
!= 1)
237 spin_lock_irqsave(&data
->lock
, flags
);
238 if (hdev
->product
== USB_DEVICE_ID_PICOLCD_BOOTLOADER
)
239 data
->status
|= PICOLCD_BOOTLOADER
;
241 /* perform the reset */
242 hid_set_field(report
->field
[0], 0, 1);
243 if (data
->status
& PICOLCD_FAILED
) {
244 spin_unlock_irqrestore(&data
->lock
, flags
);
247 usbhid_submit_report(hdev
, report
, USB_DIR_OUT
);
248 spin_unlock_irqrestore(&data
->lock
, flags
);
250 error
= picolcd_check_version(hdev
);
254 picolcd_resume_lcd(data
);
255 picolcd_resume_backlight(data
);
256 picolcd_fb_refresh(data
);
257 picolcd_leds_set(data
);
262 * The "operation_mode" sysfs attribute
264 static ssize_t
picolcd_operation_mode_show(struct device
*dev
,
265 struct device_attribute
*attr
, char *buf
)
267 struct picolcd_data
*data
= dev_get_drvdata(dev
);
269 if (data
->status
& PICOLCD_BOOTLOADER
)
270 return snprintf(buf
, PAGE_SIZE
, "[bootloader] lcd\n");
272 return snprintf(buf
, PAGE_SIZE
, "bootloader [lcd]\n");
275 static ssize_t
picolcd_operation_mode_store(struct device
*dev
,
276 struct device_attribute
*attr
, const char *buf
, size_t count
)
278 struct picolcd_data
*data
= dev_get_drvdata(dev
);
279 struct hid_report
*report
= NULL
;
281 int timeout
= data
->opmode_delay
;
284 if (cnt
>= 3 && strncmp("lcd", buf
, 3) == 0) {
285 if (data
->status
& PICOLCD_BOOTLOADER
)
286 report
= picolcd_out_report(REPORT_EXIT_FLASHER
, data
->hdev
);
289 } else if (cnt
>= 10 && strncmp("bootloader", buf
, 10) == 0) {
290 if (!(data
->status
& PICOLCD_BOOTLOADER
))
291 report
= picolcd_out_report(REPORT_EXIT_KEYBOARD
, data
->hdev
);
298 while (cnt
> 0 && (buf
[cnt
-1] == '\n' || buf
[cnt
-1] == '\r'))
303 spin_lock_irqsave(&data
->lock
, flags
);
304 hid_set_field(report
->field
[0], 0, timeout
& 0xff);
305 hid_set_field(report
->field
[0], 1, (timeout
>> 8) & 0xff);
306 usbhid_submit_report(data
->hdev
, report
, USB_DIR_OUT
);
307 spin_unlock_irqrestore(&data
->lock
, flags
);
311 static DEVICE_ATTR(operation_mode
, 0644, picolcd_operation_mode_show
,
312 picolcd_operation_mode_store
);
315 * The "operation_mode_delay" sysfs attribute
317 static ssize_t
picolcd_operation_mode_delay_show(struct device
*dev
,
318 struct device_attribute
*attr
, char *buf
)
320 struct picolcd_data
*data
= dev_get_drvdata(dev
);
322 return snprintf(buf
, PAGE_SIZE
, "%hu\n", data
->opmode_delay
);
325 static ssize_t
picolcd_operation_mode_delay_store(struct device
*dev
,
326 struct device_attribute
*attr
, const char *buf
, size_t count
)
328 struct picolcd_data
*data
= dev_get_drvdata(dev
);
330 if (sscanf(buf
, "%u", &u
) != 1)
335 data
->opmode_delay
= u
;
339 static DEVICE_ATTR(operation_mode_delay
, 0644, picolcd_operation_mode_delay_show
,
340 picolcd_operation_mode_delay_store
);
343 * Handle raw report as sent by device
345 static int picolcd_raw_event(struct hid_device
*hdev
,
346 struct hid_report
*report
, u8
*raw_data
, int size
)
348 struct picolcd_data
*data
= hid_get_drvdata(hdev
);
355 if (report
->id
== REPORT_KEY_STATE
) {
356 if (data
->input_keys
)
357 ret
= picolcd_raw_keypad(data
, report
, raw_data
+1, size
-1);
358 } else if (report
->id
== REPORT_IR_DATA
) {
359 ret
= picolcd_raw_cir(data
, report
, raw_data
+1, size
-1);
361 spin_lock_irqsave(&data
->lock
, flags
);
363 * We let the caller of picolcd_send_and_wait() check if the
364 * report we got is one of the expected ones or not.
367 memcpy(data
->pending
->raw_data
, raw_data
+1, size
-1);
368 data
->pending
->raw_size
= size
-1;
369 data
->pending
->in_report
= report
;
370 complete(&data
->pending
->ready
);
372 spin_unlock_irqrestore(&data
->lock
, flags
);
375 picolcd_debug_raw_event(data
, hdev
, report
, raw_data
, size
);
380 static int picolcd_suspend(struct hid_device
*hdev
, pm_message_t message
)
382 if (PMSG_IS_AUTO(message
))
385 picolcd_suspend_backlight(hid_get_drvdata(hdev
));
386 dbg_hid(PICOLCD_NAME
" device ready for suspend\n");
390 static int picolcd_resume(struct hid_device
*hdev
)
393 ret
= picolcd_resume_backlight(hid_get_drvdata(hdev
));
395 dbg_hid(PICOLCD_NAME
" restoring backlight failed: %d\n", ret
);
399 static int picolcd_reset_resume(struct hid_device
*hdev
)
402 ret
= picolcd_reset(hdev
);
404 dbg_hid(PICOLCD_NAME
" resetting our device failed: %d\n", ret
);
405 ret
= picolcd_fb_reset(hid_get_drvdata(hdev
), 0);
407 dbg_hid(PICOLCD_NAME
" restoring framebuffer content failed: %d\n", ret
);
408 ret
= picolcd_resume_lcd(hid_get_drvdata(hdev
));
410 dbg_hid(PICOLCD_NAME
" restoring lcd failed: %d\n", ret
);
411 ret
= picolcd_resume_backlight(hid_get_drvdata(hdev
));
413 dbg_hid(PICOLCD_NAME
" restoring backlight failed: %d\n", ret
);
414 picolcd_leds_set(hid_get_drvdata(hdev
));
419 /* initialize keypad input device */
420 static int picolcd_init_keys(struct picolcd_data
*data
,
421 struct hid_report
*report
)
423 struct hid_device
*hdev
= data
->hdev
;
424 struct input_dev
*idev
;
429 if (report
->maxfield
!= 1 || report
->field
[0]->report_count
!= 2 ||
430 report
->field
[0]->report_size
!= 8) {
431 hid_err(hdev
, "unsupported KEY_STATE report\n");
435 idev
= input_allocate_device();
437 hid_err(hdev
, "failed to allocate input device\n");
440 input_set_drvdata(idev
, hdev
);
441 memcpy(data
->keycode
, def_keymap
, sizeof(def_keymap
));
442 idev
->name
= hdev
->name
;
443 idev
->phys
= hdev
->phys
;
444 idev
->uniq
= hdev
->uniq
;
445 idev
->id
.bustype
= hdev
->bus
;
446 idev
->id
.vendor
= hdev
->vendor
;
447 idev
->id
.product
= hdev
->product
;
448 idev
->id
.version
= hdev
->version
;
449 idev
->dev
.parent
= &hdev
->dev
;
450 idev
->keycode
= &data
->keycode
;
451 idev
->keycodemax
= PICOLCD_KEYS
;
452 idev
->keycodesize
= sizeof(data
->keycode
[0]);
453 input_set_capability(idev
, EV_MSC
, MSC_SCAN
);
454 set_bit(EV_REP
, idev
->evbit
);
455 for (i
= 0; i
< PICOLCD_KEYS
; i
++)
456 input_set_capability(idev
, EV_KEY
, data
->keycode
[i
]);
457 error
= input_register_device(idev
);
459 hid_err(hdev
, "error registering the input device\n");
460 input_free_device(idev
);
463 data
->input_keys
= idev
;
467 static void picolcd_exit_keys(struct picolcd_data
*data
)
469 struct input_dev
*idev
= data
->input_keys
;
471 data
->input_keys
= NULL
;
473 input_unregister_device(idev
);
476 static int picolcd_probe_lcd(struct hid_device
*hdev
, struct picolcd_data
*data
)
480 /* Setup keypad input device */
481 error
= picolcd_init_keys(data
, picolcd_in_report(REPORT_KEY_STATE
, hdev
));
485 /* Setup CIR input device */
486 error
= picolcd_init_cir(data
, picolcd_in_report(REPORT_IR_DATA
, hdev
));
490 /* Set up the framebuffer device */
491 error
= picolcd_init_framebuffer(data
);
495 /* Setup lcd class device */
496 error
= picolcd_init_lcd(data
, picolcd_out_report(REPORT_CONTRAST
, hdev
));
500 /* Setup backlight class device */
501 error
= picolcd_init_backlight(data
, picolcd_out_report(REPORT_BRIGHTNESS
, hdev
));
505 /* Setup the LED class devices */
506 error
= picolcd_init_leds(data
, picolcd_out_report(REPORT_LED_STATE
, hdev
));
510 picolcd_init_devfs(data
, picolcd_out_report(REPORT_EE_READ
, hdev
),
511 picolcd_out_report(REPORT_EE_WRITE
, hdev
),
512 picolcd_out_report(REPORT_READ_MEMORY
, hdev
),
513 picolcd_out_report(REPORT_WRITE_MEMORY
, hdev
),
514 picolcd_out_report(REPORT_RESET
, hdev
));
517 picolcd_exit_leds(data
);
518 picolcd_exit_backlight(data
);
519 picolcd_exit_lcd(data
);
520 picolcd_exit_framebuffer(data
);
521 picolcd_exit_cir(data
);
522 picolcd_exit_keys(data
);
526 static int picolcd_probe_bootloader(struct hid_device
*hdev
, struct picolcd_data
*data
)
528 picolcd_init_devfs(data
, NULL
, NULL
,
529 picolcd_out_report(REPORT_BL_READ_MEMORY
, hdev
),
530 picolcd_out_report(REPORT_BL_WRITE_MEMORY
, hdev
), NULL
);
534 static int picolcd_probe(struct hid_device
*hdev
,
535 const struct hid_device_id
*id
)
537 struct picolcd_data
*data
;
540 dbg_hid(PICOLCD_NAME
" hardware probe...\n");
543 * Let's allocate the picolcd data structure, set some reasonable
544 * defaults, and associate it with the device
546 data
= kzalloc(sizeof(struct picolcd_data
), GFP_KERNEL
);
548 hid_err(hdev
, "can't allocate space for Minibox PicoLCD device data\n");
553 spin_lock_init(&data
->lock
);
554 mutex_init(&data
->mutex
);
556 data
->opmode_delay
= 5000;
557 if (hdev
->product
== USB_DEVICE_ID_PICOLCD_BOOTLOADER
)
558 data
->status
|= PICOLCD_BOOTLOADER
;
559 hid_set_drvdata(hdev
, data
);
561 /* Parse the device reports and start it up */
562 error
= hid_parse(hdev
);
564 hid_err(hdev
, "device report parse failed\n");
565 goto err_cleanup_data
;
568 error
= hid_hw_start(hdev
, 0);
570 hid_err(hdev
, "hardware start failed\n");
571 goto err_cleanup_data
;
574 error
= hid_hw_open(hdev
);
576 hid_err(hdev
, "failed to open input interrupt pipe for key and IR events\n");
577 goto err_cleanup_hid_hw
;
580 error
= device_create_file(&hdev
->dev
, &dev_attr_operation_mode_delay
);
582 hid_err(hdev
, "failed to create sysfs attributes\n");
583 goto err_cleanup_hid_ll
;
586 error
= device_create_file(&hdev
->dev
, &dev_attr_operation_mode
);
588 hid_err(hdev
, "failed to create sysfs attributes\n");
589 goto err_cleanup_sysfs1
;
592 if (data
->status
& PICOLCD_BOOTLOADER
)
593 error
= picolcd_probe_bootloader(hdev
, data
);
595 error
= picolcd_probe_lcd(hdev
, data
);
597 goto err_cleanup_sysfs2
;
599 dbg_hid(PICOLCD_NAME
" activated and initialized\n");
603 device_remove_file(&hdev
->dev
, &dev_attr_operation_mode
);
605 device_remove_file(&hdev
->dev
, &dev_attr_operation_mode_delay
);
613 hid_set_drvdata(hdev
, NULL
);
618 static void picolcd_remove(struct hid_device
*hdev
)
620 struct picolcd_data
*data
= hid_get_drvdata(hdev
);
623 dbg_hid(PICOLCD_NAME
" hardware remove...\n");
624 spin_lock_irqsave(&data
->lock
, flags
);
625 data
->status
|= PICOLCD_FAILED
;
626 spin_unlock_irqrestore(&data
->lock
, flags
);
628 picolcd_exit_devfs(data
);
629 device_remove_file(&hdev
->dev
, &dev_attr_operation_mode
);
630 device_remove_file(&hdev
->dev
, &dev_attr_operation_mode_delay
);
634 /* Shortcut potential pending reply that will never arrive */
635 spin_lock_irqsave(&data
->lock
, flags
);
637 complete(&data
->pending
->ready
);
638 spin_unlock_irqrestore(&data
->lock
, flags
);
641 picolcd_exit_leds(data
);
642 /* Clean up the framebuffer */
643 picolcd_exit_backlight(data
);
644 picolcd_exit_lcd(data
);
645 picolcd_exit_framebuffer(data
);
647 picolcd_exit_cir(data
);
648 picolcd_exit_keys(data
);
650 hid_set_drvdata(hdev
, NULL
);
651 mutex_destroy(&data
->mutex
);
652 /* Finally, clean up the picolcd data itself */
656 static const struct hid_device_id picolcd_devices
[] = {
657 { HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP
, USB_DEVICE_ID_PICOLCD
) },
658 { HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP
, USB_DEVICE_ID_PICOLCD_BOOTLOADER
) },
661 MODULE_DEVICE_TABLE(hid
, picolcd_devices
);
663 static struct hid_driver picolcd_driver
= {
664 .name
= "hid-picolcd",
665 .id_table
= picolcd_devices
,
666 .probe
= picolcd_probe
,
667 .remove
= picolcd_remove
,
668 .raw_event
= picolcd_raw_event
,
670 .suspend
= picolcd_suspend
,
671 .resume
= picolcd_resume
,
672 .reset_resume
= picolcd_reset_resume
,
676 static int __init
picolcd_init(void)
678 return hid_register_driver(&picolcd_driver
);
681 static void __exit
picolcd_exit(void)
683 hid_unregister_driver(&picolcd_driver
);
686 module_init(picolcd_init
);
687 module_exit(picolcd_exit
);
688 MODULE_DESCRIPTION("Minibox graphics PicoLCD Driver");
689 MODULE_LICENSE("GPL v2");