1 // SPDX-License-Identifier: GPL-2.0-only
2 /***************************************************************************
3 * Copyright (C) 2010-2012 by Bruno Prémont <bonbons@linux-vserver.org> *
5 * Based on Logitech G13 driver (v0.4) *
6 * Copyright (C) 2009 by Rick L. Vinyard, Jr. <rvinyard@cs.nmsu.edu> *
8 ***************************************************************************/
10 #include <linux/hid.h>
11 #include <linux/hid-debug.h>
12 #include <linux/input.h>
16 #include <linux/vmalloc.h>
18 #include <linux/completion.h>
19 #include <linux/uaccess.h>
20 #include <linux/module.h>
21 #include <linux/string.h>
23 #include "hid-picolcd.h"
28 * The PicoLCD has an IR receiver header, a built-in keypad with 5 keys
29 * and header for 4x4 key matrix. The built-in keys are part of the matrix.
31 static const unsigned short def_keymap
[PICOLCD_KEYS
] = {
32 KEY_RESERVED
, /* none */
33 KEY_BACK
, /* col 4 + row 1 */
34 KEY_HOMEPAGE
, /* col 3 + row 1 */
35 KEY_RESERVED
, /* col 2 + row 1 */
36 KEY_RESERVED
, /* col 1 + row 1 */
37 KEY_SCROLLUP
, /* col 4 + row 2 */
38 KEY_OK
, /* col 3 + row 2 */
39 KEY_SCROLLDOWN
, /* col 2 + row 2 */
40 KEY_RESERVED
, /* col 1 + row 2 */
41 KEY_RESERVED
, /* col 4 + row 3 */
42 KEY_RESERVED
, /* col 3 + row 3 */
43 KEY_RESERVED
, /* col 2 + row 3 */
44 KEY_RESERVED
, /* col 1 + row 3 */
45 KEY_RESERVED
, /* col 4 + row 4 */
46 KEY_RESERVED
, /* col 3 + row 4 */
47 KEY_RESERVED
, /* col 2 + row 4 */
48 KEY_RESERVED
, /* col 1 + row 4 */
52 /* Find a given report */
53 struct hid_report
*picolcd_report(int id
, struct hid_device
*hdev
, int dir
)
55 struct list_head
*feature_report_list
= &hdev
->report_enum
[dir
].report_list
;
56 struct hid_report
*report
= NULL
;
58 list_for_each_entry(report
, feature_report_list
, list
) {
62 hid_warn(hdev
, "No report with id 0x%x found\n", id
);
66 /* Submit a report and wait for a reply from device - if device fades away
67 * or does not respond in time, return NULL */
68 struct picolcd_pending
*picolcd_send_and_wait(struct hid_device
*hdev
,
69 int report_id
, const u8
*raw_data
, int size
)
71 struct picolcd_data
*data
= hid_get_drvdata(hdev
);
72 struct picolcd_pending
*work
;
73 struct hid_report
*report
= picolcd_out_report(report_id
, hdev
);
79 if (data
->status
& PICOLCD_FAILED
)
81 work
= kzalloc(sizeof(*work
), GFP_KERNEL
);
85 init_completion(&work
->ready
);
86 work
->out_report
= report
;
87 work
->in_report
= NULL
;
90 mutex_lock(&data
->mutex
);
91 spin_lock_irqsave(&data
->lock
, flags
);
92 for (i
= k
= 0; i
< report
->maxfield
; i
++)
93 for (j
= 0; j
< report
->field
[i
]->report_count
; j
++) {
94 hid_set_field(report
->field
[i
], j
, k
< size
? raw_data
[k
] : 0);
97 if (data
->status
& PICOLCD_FAILED
) {
101 data
->pending
= work
;
102 hid_hw_request(data
->hdev
, report
, HID_REQ_SET_REPORT
);
103 spin_unlock_irqrestore(&data
->lock
, flags
);
104 wait_for_completion_interruptible_timeout(&work
->ready
, HZ
*2);
105 spin_lock_irqsave(&data
->lock
, flags
);
106 data
->pending
= NULL
;
108 spin_unlock_irqrestore(&data
->lock
, flags
);
109 mutex_unlock(&data
->mutex
);
116 static int picolcd_raw_keypad(struct picolcd_data
*data
,
117 struct hid_report
*report
, u8
*raw_data
, int size
)
121 * First and second data bytes list currently pressed keys,
122 * 0x00 means no key and at most 2 keys may be pressed at same time
126 /* determine newly pressed keys */
127 for (i
= 0; i
< size
; i
++) {
128 unsigned int key_code
;
129 if (raw_data
[i
] == 0)
131 for (j
= 0; j
< sizeof(data
->pressed_keys
); j
++)
132 if (data
->pressed_keys
[j
] == raw_data
[i
])
133 goto key_already_down
;
134 for (j
= 0; j
< sizeof(data
->pressed_keys
); j
++)
135 if (data
->pressed_keys
[j
] == 0) {
136 data
->pressed_keys
[j
] = raw_data
[i
];
139 input_event(data
->input_keys
, EV_MSC
, MSC_SCAN
, raw_data
[i
]);
140 if (raw_data
[i
] < PICOLCD_KEYS
)
141 key_code
= data
->keycode
[raw_data
[i
]];
143 key_code
= KEY_UNKNOWN
;
144 if (key_code
!= KEY_UNKNOWN
) {
145 dbg_hid(PICOLCD_NAME
" got key press for %u:%d",
146 raw_data
[i
], key_code
);
147 input_report_key(data
->input_keys
, key_code
, 1);
149 input_sync(data
->input_keys
);
154 /* determine newly released keys */
155 for (j
= 0; j
< sizeof(data
->pressed_keys
); j
++) {
156 unsigned int key_code
;
157 if (data
->pressed_keys
[j
] == 0)
159 for (i
= 0; i
< size
; i
++)
160 if (data
->pressed_keys
[j
] == raw_data
[i
])
162 input_event(data
->input_keys
, EV_MSC
, MSC_SCAN
, data
->pressed_keys
[j
]);
163 if (data
->pressed_keys
[j
] < PICOLCD_KEYS
)
164 key_code
= data
->keycode
[data
->pressed_keys
[j
]];
166 key_code
= KEY_UNKNOWN
;
167 if (key_code
!= KEY_UNKNOWN
) {
168 dbg_hid(PICOLCD_NAME
" got key release for %u:%d",
169 data
->pressed_keys
[j
], key_code
);
170 input_report_key(data
->input_keys
, key_code
, 0);
172 input_sync(data
->input_keys
);
173 data
->pressed_keys
[j
] = 0;
180 static int picolcd_check_version(struct hid_device
*hdev
)
182 struct picolcd_data
*data
= hid_get_drvdata(hdev
);
183 struct picolcd_pending
*verinfo
;
189 verinfo
= picolcd_send_and_wait(hdev
, REPORT_VERSION
, NULL
, 0);
191 hid_err(hdev
, "no version response from PicoLCD\n");
195 if (verinfo
->raw_size
== 2) {
196 data
->version
[0] = verinfo
->raw_data
[1];
197 data
->version
[1] = verinfo
->raw_data
[0];
198 if (data
->status
& PICOLCD_BOOTLOADER
) {
199 hid_info(hdev
, "PicoLCD, bootloader version %d.%d\n",
200 verinfo
->raw_data
[1], verinfo
->raw_data
[0]);
202 hid_info(hdev
, "PicoLCD, firmware version %d.%d\n",
203 verinfo
->raw_data
[1], verinfo
->raw_data
[0]);
206 hid_err(hdev
, "confused, got unexpected version response from PicoLCD\n");
214 * Reset our device and wait for answer to VERSION request
216 int picolcd_reset(struct hid_device
*hdev
)
218 struct picolcd_data
*data
= hid_get_drvdata(hdev
);
219 struct hid_report
*report
= picolcd_out_report(REPORT_RESET
, hdev
);
223 if (!data
|| !report
|| report
->maxfield
!= 1)
226 spin_lock_irqsave(&data
->lock
, flags
);
227 if (hdev
->product
== USB_DEVICE_ID_PICOLCD_BOOTLOADER
)
228 data
->status
|= PICOLCD_BOOTLOADER
;
230 /* perform the reset */
231 hid_set_field(report
->field
[0], 0, 1);
232 if (data
->status
& PICOLCD_FAILED
) {
233 spin_unlock_irqrestore(&data
->lock
, flags
);
236 hid_hw_request(hdev
, report
, HID_REQ_SET_REPORT
);
237 spin_unlock_irqrestore(&data
->lock
, flags
);
239 error
= picolcd_check_version(hdev
);
243 picolcd_resume_lcd(data
);
244 picolcd_resume_backlight(data
);
245 picolcd_fb_refresh(data
);
246 picolcd_leds_set(data
);
251 * The "operation_mode" sysfs attribute
253 static ssize_t
picolcd_operation_mode_show(struct device
*dev
,
254 struct device_attribute
*attr
, char *buf
)
256 struct picolcd_data
*data
= dev_get_drvdata(dev
);
258 if (data
->status
& PICOLCD_BOOTLOADER
)
259 return snprintf(buf
, PAGE_SIZE
, "[bootloader] lcd\n");
261 return snprintf(buf
, PAGE_SIZE
, "bootloader [lcd]\n");
264 static ssize_t
picolcd_operation_mode_store(struct device
*dev
,
265 struct device_attribute
*attr
, const char *buf
, size_t count
)
267 struct picolcd_data
*data
= dev_get_drvdata(dev
);
268 struct hid_report
*report
= NULL
;
269 int timeout
= data
->opmode_delay
;
272 if (sysfs_streq(buf
, "lcd")) {
273 if (data
->status
& PICOLCD_BOOTLOADER
)
274 report
= picolcd_out_report(REPORT_EXIT_FLASHER
, data
->hdev
);
275 } else if (sysfs_streq(buf
, "bootloader")) {
276 if (!(data
->status
& PICOLCD_BOOTLOADER
))
277 report
= picolcd_out_report(REPORT_EXIT_KEYBOARD
, data
->hdev
);
282 if (!report
|| report
->maxfield
!= 1)
285 spin_lock_irqsave(&data
->lock
, flags
);
286 hid_set_field(report
->field
[0], 0, timeout
& 0xff);
287 hid_set_field(report
->field
[0], 1, (timeout
>> 8) & 0xff);
288 hid_hw_request(data
->hdev
, report
, HID_REQ_SET_REPORT
);
289 spin_unlock_irqrestore(&data
->lock
, flags
);
293 static DEVICE_ATTR(operation_mode
, 0644, picolcd_operation_mode_show
,
294 picolcd_operation_mode_store
);
297 * The "operation_mode_delay" sysfs attribute
299 static ssize_t
picolcd_operation_mode_delay_show(struct device
*dev
,
300 struct device_attribute
*attr
, char *buf
)
302 struct picolcd_data
*data
= dev_get_drvdata(dev
);
304 return snprintf(buf
, PAGE_SIZE
, "%hu\n", data
->opmode_delay
);
307 static ssize_t
picolcd_operation_mode_delay_store(struct device
*dev
,
308 struct device_attribute
*attr
, const char *buf
, size_t count
)
310 struct picolcd_data
*data
= dev_get_drvdata(dev
);
312 if (sscanf(buf
, "%u", &u
) != 1)
317 data
->opmode_delay
= u
;
321 static DEVICE_ATTR(operation_mode_delay
, 0644, picolcd_operation_mode_delay_show
,
322 picolcd_operation_mode_delay_store
);
325 * Handle raw report as sent by device
327 static int picolcd_raw_event(struct hid_device
*hdev
,
328 struct hid_report
*report
, u8
*raw_data
, int size
)
330 struct picolcd_data
*data
= hid_get_drvdata(hdev
);
338 hid_warn(hdev
, "invalid size value (%d) for picolcd raw event (%d)\n",
343 if (report
->id
== REPORT_KEY_STATE
) {
344 if (data
->input_keys
)
345 ret
= picolcd_raw_keypad(data
, report
, raw_data
+1, size
-1);
346 } else if (report
->id
== REPORT_IR_DATA
) {
347 ret
= picolcd_raw_cir(data
, report
, raw_data
+1, size
-1);
349 spin_lock_irqsave(&data
->lock
, flags
);
351 * We let the caller of picolcd_send_and_wait() check if the
352 * report we got is one of the expected ones or not.
355 memcpy(data
->pending
->raw_data
, raw_data
+1, size
-1);
356 data
->pending
->raw_size
= size
-1;
357 data
->pending
->in_report
= report
;
358 complete(&data
->pending
->ready
);
360 spin_unlock_irqrestore(&data
->lock
, flags
);
363 picolcd_debug_raw_event(data
, hdev
, report
, raw_data
, size
);
368 static int picolcd_suspend(struct hid_device
*hdev
, pm_message_t message
)
370 if (PMSG_IS_AUTO(message
))
373 picolcd_suspend_backlight(hid_get_drvdata(hdev
));
374 dbg_hid(PICOLCD_NAME
" device ready for suspend\n");
378 static int picolcd_resume(struct hid_device
*hdev
)
381 ret
= picolcd_resume_backlight(hid_get_drvdata(hdev
));
383 dbg_hid(PICOLCD_NAME
" restoring backlight failed: %d\n", ret
);
387 static int picolcd_reset_resume(struct hid_device
*hdev
)
390 ret
= picolcd_reset(hdev
);
392 dbg_hid(PICOLCD_NAME
" resetting our device failed: %d\n", ret
);
393 ret
= picolcd_fb_reset(hid_get_drvdata(hdev
), 0);
395 dbg_hid(PICOLCD_NAME
" restoring framebuffer content failed: %d\n", ret
);
396 ret
= picolcd_resume_lcd(hid_get_drvdata(hdev
));
398 dbg_hid(PICOLCD_NAME
" restoring lcd failed: %d\n", ret
);
399 ret
= picolcd_resume_backlight(hid_get_drvdata(hdev
));
401 dbg_hid(PICOLCD_NAME
" restoring backlight failed: %d\n", ret
);
402 picolcd_leds_set(hid_get_drvdata(hdev
));
407 /* initialize keypad input device */
408 static int picolcd_init_keys(struct picolcd_data
*data
,
409 struct hid_report
*report
)
411 struct hid_device
*hdev
= data
->hdev
;
412 struct input_dev
*idev
;
417 if (report
->maxfield
!= 1 || report
->field
[0]->report_count
!= 2 ||
418 report
->field
[0]->report_size
!= 8) {
419 hid_err(hdev
, "unsupported KEY_STATE report\n");
423 idev
= input_allocate_device();
425 hid_err(hdev
, "failed to allocate input device\n");
428 input_set_drvdata(idev
, hdev
);
429 memcpy(data
->keycode
, def_keymap
, sizeof(def_keymap
));
430 idev
->name
= hdev
->name
;
431 idev
->phys
= hdev
->phys
;
432 idev
->uniq
= hdev
->uniq
;
433 idev
->id
.bustype
= hdev
->bus
;
434 idev
->id
.vendor
= hdev
->vendor
;
435 idev
->id
.product
= hdev
->product
;
436 idev
->id
.version
= hdev
->version
;
437 idev
->dev
.parent
= &hdev
->dev
;
438 idev
->keycode
= &data
->keycode
;
439 idev
->keycodemax
= PICOLCD_KEYS
;
440 idev
->keycodesize
= sizeof(data
->keycode
[0]);
441 input_set_capability(idev
, EV_MSC
, MSC_SCAN
);
442 set_bit(EV_REP
, idev
->evbit
);
443 for (i
= 0; i
< PICOLCD_KEYS
; i
++)
444 input_set_capability(idev
, EV_KEY
, data
->keycode
[i
]);
445 error
= input_register_device(idev
);
447 hid_err(hdev
, "error registering the input device\n");
448 input_free_device(idev
);
451 data
->input_keys
= idev
;
455 static void picolcd_exit_keys(struct picolcd_data
*data
)
457 struct input_dev
*idev
= data
->input_keys
;
459 data
->input_keys
= NULL
;
461 input_unregister_device(idev
);
464 static int picolcd_probe_lcd(struct hid_device
*hdev
, struct picolcd_data
*data
)
468 /* Setup keypad input device */
469 error
= picolcd_init_keys(data
, picolcd_in_report(REPORT_KEY_STATE
, hdev
));
473 /* Setup CIR input device */
474 error
= picolcd_init_cir(data
, picolcd_in_report(REPORT_IR_DATA
, hdev
));
478 /* Set up the framebuffer device */
479 error
= picolcd_init_framebuffer(data
);
483 /* Setup lcd class device */
484 error
= picolcd_init_lcd(data
, picolcd_out_report(REPORT_CONTRAST
, hdev
));
488 /* Setup backlight class device */
489 error
= picolcd_init_backlight(data
, picolcd_out_report(REPORT_BRIGHTNESS
, hdev
));
493 /* Setup the LED class devices */
494 error
= picolcd_init_leds(data
, picolcd_out_report(REPORT_LED_STATE
, hdev
));
498 picolcd_init_devfs(data
, picolcd_out_report(REPORT_EE_READ
, hdev
),
499 picolcd_out_report(REPORT_EE_WRITE
, hdev
),
500 picolcd_out_report(REPORT_READ_MEMORY
, hdev
),
501 picolcd_out_report(REPORT_WRITE_MEMORY
, hdev
),
502 picolcd_out_report(REPORT_RESET
, hdev
));
505 picolcd_exit_leds(data
);
506 picolcd_exit_backlight(data
);
507 picolcd_exit_lcd(data
);
508 picolcd_exit_framebuffer(data
);
509 picolcd_exit_cir(data
);
510 picolcd_exit_keys(data
);
514 static int picolcd_probe_bootloader(struct hid_device
*hdev
, struct picolcd_data
*data
)
516 picolcd_init_devfs(data
, NULL
, NULL
,
517 picolcd_out_report(REPORT_BL_READ_MEMORY
, hdev
),
518 picolcd_out_report(REPORT_BL_WRITE_MEMORY
, hdev
), NULL
);
522 static int picolcd_probe(struct hid_device
*hdev
,
523 const struct hid_device_id
*id
)
525 struct picolcd_data
*data
;
528 dbg_hid(PICOLCD_NAME
" hardware probe...\n");
531 * Let's allocate the picolcd data structure, set some reasonable
532 * defaults, and associate it with the device
534 data
= kzalloc(sizeof(struct picolcd_data
), GFP_KERNEL
);
536 hid_err(hdev
, "can't allocate space for Minibox PicoLCD device data\n");
540 spin_lock_init(&data
->lock
);
541 mutex_init(&data
->mutex
);
543 data
->opmode_delay
= 5000;
544 if (hdev
->product
== USB_DEVICE_ID_PICOLCD_BOOTLOADER
)
545 data
->status
|= PICOLCD_BOOTLOADER
;
546 hid_set_drvdata(hdev
, data
);
548 /* Parse the device reports and start it up */
549 error
= hid_parse(hdev
);
551 hid_err(hdev
, "device report parse failed\n");
552 goto err_cleanup_data
;
555 error
= hid_hw_start(hdev
, 0);
557 hid_err(hdev
, "hardware start failed\n");
558 goto err_cleanup_data
;
561 error
= hid_hw_open(hdev
);
563 hid_err(hdev
, "failed to open input interrupt pipe for key and IR events\n");
564 goto err_cleanup_hid_hw
;
567 error
= device_create_file(&hdev
->dev
, &dev_attr_operation_mode_delay
);
569 hid_err(hdev
, "failed to create sysfs attributes\n");
570 goto err_cleanup_hid_ll
;
573 error
= device_create_file(&hdev
->dev
, &dev_attr_operation_mode
);
575 hid_err(hdev
, "failed to create sysfs attributes\n");
576 goto err_cleanup_sysfs1
;
579 if (data
->status
& PICOLCD_BOOTLOADER
)
580 error
= picolcd_probe_bootloader(hdev
, data
);
582 error
= picolcd_probe_lcd(hdev
, data
);
584 goto err_cleanup_sysfs2
;
586 dbg_hid(PICOLCD_NAME
" activated and initialized\n");
590 device_remove_file(&hdev
->dev
, &dev_attr_operation_mode
);
592 device_remove_file(&hdev
->dev
, &dev_attr_operation_mode_delay
);
602 static void picolcd_remove(struct hid_device
*hdev
)
604 struct picolcd_data
*data
= hid_get_drvdata(hdev
);
607 dbg_hid(PICOLCD_NAME
" hardware remove...\n");
608 spin_lock_irqsave(&data
->lock
, flags
);
609 data
->status
|= PICOLCD_FAILED
;
610 spin_unlock_irqrestore(&data
->lock
, flags
);
612 picolcd_exit_devfs(data
);
613 device_remove_file(&hdev
->dev
, &dev_attr_operation_mode
);
614 device_remove_file(&hdev
->dev
, &dev_attr_operation_mode_delay
);
618 /* Shortcut potential pending reply that will never arrive */
619 spin_lock_irqsave(&data
->lock
, flags
);
621 complete(&data
->pending
->ready
);
622 spin_unlock_irqrestore(&data
->lock
, flags
);
625 picolcd_exit_leds(data
);
626 /* Clean up the framebuffer */
627 picolcd_exit_backlight(data
);
628 picolcd_exit_lcd(data
);
629 picolcd_exit_framebuffer(data
);
631 picolcd_exit_cir(data
);
632 picolcd_exit_keys(data
);
634 mutex_destroy(&data
->mutex
);
635 /* Finally, clean up the picolcd data itself */
639 static const struct hid_device_id picolcd_devices
[] = {
640 { HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP
, USB_DEVICE_ID_PICOLCD
) },
641 { HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP
, USB_DEVICE_ID_PICOLCD_BOOTLOADER
) },
644 MODULE_DEVICE_TABLE(hid
, picolcd_devices
);
646 static struct hid_driver picolcd_driver
= {
647 .name
= "hid-picolcd",
648 .id_table
= picolcd_devices
,
649 .probe
= picolcd_probe
,
650 .remove
= picolcd_remove
,
651 .raw_event
= picolcd_raw_event
,
653 .suspend
= picolcd_suspend
,
654 .resume
= picolcd_resume
,
655 .reset_resume
= picolcd_reset_resume
,
658 module_hid_driver(picolcd_driver
);
660 MODULE_DESCRIPTION("Minibox graphics PicoLCD Driver");
661 MODULE_LICENSE("GPL v2");