1 /***************************************************************************
2 * Copyright (C) 2010 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>
29 #include <linux/backlight.h>
30 #include <linux/lcd.h>
32 #include <linux/leds.h>
34 #include <linux/seq_file.h>
35 #include <linux/debugfs.h>
37 #include <linux/completion.h>
38 #include <linux/uaccess.h>
40 #define PICOLCD_NAME "PicoLCD (graphic)"
43 #define REPORT_ERROR_CODE 0x10 /* LCD: IN[16] */
44 #define ERR_SUCCESS 0x00
45 #define ERR_PARAMETER_MISSING 0x01
46 #define ERR_DATA_MISSING 0x02
47 #define ERR_BLOCK_READ_ONLY 0x03
48 #define ERR_BLOCK_NOT_ERASABLE 0x04
49 #define ERR_BLOCK_TOO_BIG 0x05
50 #define ERR_SECTION_OVERFLOW 0x06
51 #define ERR_INVALID_CMD_LEN 0x07
52 #define ERR_INVALID_DATA_LEN 0x08
53 #define REPORT_KEY_STATE 0x11 /* LCD: IN[2] */
54 #define REPORT_IR_DATA 0x21 /* LCD: IN[63] */
55 #define REPORT_EE_DATA 0x32 /* LCD: IN[63] */
56 #define REPORT_MEMORY 0x41 /* LCD: IN[63] */
57 #define REPORT_LED_STATE 0x81 /* LCD: OUT[1] */
58 #define REPORT_BRIGHTNESS 0x91 /* LCD: OUT[1] */
59 #define REPORT_CONTRAST 0x92 /* LCD: OUT[1] */
60 #define REPORT_RESET 0x93 /* LCD: OUT[2] */
61 #define REPORT_LCD_CMD 0x94 /* LCD: OUT[63] */
62 #define REPORT_LCD_DATA 0x95 /* LCD: OUT[63] */
63 #define REPORT_LCD_CMD_DATA 0x96 /* LCD: OUT[63] */
64 #define REPORT_EE_READ 0xa3 /* LCD: OUT[63] */
65 #define REPORT_EE_WRITE 0xa4 /* LCD: OUT[63] */
66 #define REPORT_ERASE_MEMORY 0xb2 /* LCD: OUT[2] */
67 #define REPORT_READ_MEMORY 0xb3 /* LCD: OUT[3] */
68 #define REPORT_WRITE_MEMORY 0xb4 /* LCD: OUT[63] */
69 #define REPORT_SPLASH_RESTART 0xc1 /* LCD: OUT[1] */
70 #define REPORT_EXIT_KEYBOARD 0xef /* LCD: OUT[2] */
71 #define REPORT_VERSION 0xf1 /* LCD: IN[2],OUT[1] Bootloader: IN[2],OUT[1] */
72 #define REPORT_BL_ERASE_MEMORY 0xf2 /* Bootloader: IN[36],OUT[4] */
73 #define REPORT_BL_READ_MEMORY 0xf3 /* Bootloader: IN[36],OUT[4] */
74 #define REPORT_BL_WRITE_MEMORY 0xf4 /* Bootloader: IN[36],OUT[36] */
75 #define REPORT_DEVID 0xf5 /* LCD: IN[5], OUT[1] Bootloader: IN[5],OUT[1] */
76 #define REPORT_SPLASH_SIZE 0xf6 /* LCD: IN[4], OUT[1] */
77 #define REPORT_HOOK_VERSION 0xf7 /* LCD: IN[2], OUT[1] */
78 #define REPORT_EXIT_FLASHER 0xff /* Bootloader: OUT[2] */
80 #ifdef CONFIG_HID_PICOLCD_FB
83 * The PicoLCD use a Topway LCD module of 256x64 pixel
84 * This display area is tiled over 4 controllers with 8 tiles
85 * each. Each tile has 8x64 pixel, each data byte representing
86 * a 1-bit wide vertical line of the tile.
88 * The display can be updated at a tile granularity.
90 * Chip 1 Chip 2 Chip 3 Chip 4
91 * +----------------+----------------+----------------+----------------+
92 * | Tile 1 | Tile 1 | Tile 1 | Tile 1 |
93 * +----------------+----------------+----------------+----------------+
94 * | Tile 2 | Tile 2 | Tile 2 | Tile 2 |
95 * +----------------+----------------+----------------+----------------+
97 * +----------------+----------------+----------------+----------------+
98 * | Tile 8 | Tile 8 | Tile 8 | Tile 8 |
99 * +----------------+----------------+----------------+----------------+
101 #define PICOLCDFB_NAME "picolcdfb"
102 #define PICOLCDFB_WIDTH (256)
103 #define PICOLCDFB_HEIGHT (64)
104 #define PICOLCDFB_SIZE (PICOLCDFB_WIDTH * PICOLCDFB_HEIGHT / 8)
106 #define PICOLCDFB_UPDATE_RATE_LIMIT 10
107 #define PICOLCDFB_UPDATE_RATE_DEFAULT 2
109 /* Framebuffer visual structures */
110 static const struct fb_fix_screeninfo picolcdfb_fix
= {
111 .id
= PICOLCDFB_NAME
,
112 .type
= FB_TYPE_PACKED_PIXELS
,
113 .visual
= FB_VISUAL_MONO01
,
117 .line_length
= PICOLCDFB_WIDTH
/ 8,
118 .accel
= FB_ACCEL_NONE
,
121 static const struct fb_var_screeninfo picolcdfb_var
= {
122 .xres
= PICOLCDFB_WIDTH
,
123 .yres
= PICOLCDFB_HEIGHT
,
124 .xres_virtual
= PICOLCDFB_WIDTH
,
125 .yres_virtual
= PICOLCDFB_HEIGHT
,
151 #endif /* CONFIG_HID_PICOLCD_FB */
155 * The PicoLCD has an IR receiver header, a built-in keypad with 5 keys
156 * and header for 4x4 key matrix. The built-in keys are part of the matrix.
158 static const unsigned short def_keymap
[] = {
159 KEY_RESERVED
, /* none */
160 KEY_BACK
, /* col 4 + row 1 */
161 KEY_HOMEPAGE
, /* col 3 + row 1 */
162 KEY_RESERVED
, /* col 2 + row 1 */
163 KEY_RESERVED
, /* col 1 + row 1 */
164 KEY_SCROLLUP
, /* col 4 + row 2 */
165 KEY_OK
, /* col 3 + row 2 */
166 KEY_SCROLLDOWN
, /* col 2 + row 2 */
167 KEY_RESERVED
, /* col 1 + row 2 */
168 KEY_RESERVED
, /* col 4 + row 3 */
169 KEY_RESERVED
, /* col 3 + row 3 */
170 KEY_RESERVED
, /* col 2 + row 3 */
171 KEY_RESERVED
, /* col 1 + row 3 */
172 KEY_RESERVED
, /* col 4 + row 4 */
173 KEY_RESERVED
, /* col 3 + row 4 */
174 KEY_RESERVED
, /* col 2 + row 4 */
175 KEY_RESERVED
, /* col 1 + row 4 */
177 #define PICOLCD_KEYS ARRAY_SIZE(def_keymap)
179 /* Description of in-progress IO operation, used for operations
180 * that trigger response from device */
181 struct picolcd_pending
{
182 struct hid_report
*out_report
;
183 struct hid_report
*in_report
;
184 struct completion ready
;
189 /* Per device data structure */
190 struct picolcd_data
{
191 struct hid_device
*hdev
;
192 #ifdef CONFIG_DEBUG_FS
193 struct dentry
*debug_reset
;
194 struct dentry
*debug_eeprom
;
195 struct dentry
*debug_flash
;
196 struct mutex mutex_flash
;
200 unsigned short opmode_delay
;
203 struct input_dev
*input_keys
;
204 struct input_dev
*input_cir
;
205 unsigned short keycode
[PICOLCD_KEYS
];
207 #ifdef CONFIG_HID_PICOLCD_FB
208 /* Framebuffer stuff */
212 u8
*fb_vbitmap
; /* local copy of what was sent to PicoLCD */
213 u8
*fb_bitmap
; /* framebuffer */
214 struct fb_info
*fb_info
;
215 struct fb_deferred_io fb_defio
;
216 #endif /* CONFIG_HID_PICOLCD_FB */
217 #ifdef CONFIG_HID_PICOLCD_LCD
218 struct lcd_device
*lcd
;
220 #endif /* CONFIG_HID_PICOLCD_LCD */
221 #ifdef CONFIG_HID_PICOLCD_BACKLIGHT
222 struct backlight_device
*backlight
;
225 #endif /* CONFIG_HID_PICOLCD_BACKLIGHT */
226 #ifdef CONFIG_HID_PICOLCD_LEDS
229 struct led_classdev
*led
[8];
230 #endif /* CONFIG_HID_PICOLCD_LEDS */
232 /* Housekeeping stuff */
235 struct picolcd_pending
*pending
;
237 #define PICOLCD_BOOTLOADER 1
238 #define PICOLCD_FAILED 2
239 #define PICOLCD_READY_FB 4
243 /* Find a given report */
244 #define picolcd_in_report(id, dev) picolcd_report(id, dev, HID_INPUT_REPORT)
245 #define picolcd_out_report(id, dev) picolcd_report(id, dev, HID_OUTPUT_REPORT)
247 static struct hid_report
*picolcd_report(int id
, struct hid_device
*hdev
, int dir
)
249 struct list_head
*feature_report_list
= &hdev
->report_enum
[dir
].report_list
;
250 struct hid_report
*report
= NULL
;
252 list_for_each_entry(report
, feature_report_list
, list
) {
253 if (report
->id
== id
)
256 dev_warn(&hdev
->dev
, "No report with id 0x%x found\n", id
);
260 #ifdef CONFIG_DEBUG_FS
261 static void picolcd_debug_out_report(struct picolcd_data
*data
,
262 struct hid_device
*hdev
, struct hid_report
*report
);
263 #define usbhid_submit_report(a, b, c) \
265 picolcd_debug_out_report(hid_get_drvdata(a), a, b); \
266 usbhid_submit_report(a, b, c); \
270 /* Submit a report and wait for a reply from device - if device fades away
271 * or does not respond in time, return NULL */
272 static struct picolcd_pending
*picolcd_send_and_wait(struct hid_device
*hdev
,
273 int report_id
, const u8
*raw_data
, int size
)
275 struct picolcd_data
*data
= hid_get_drvdata(hdev
);
276 struct picolcd_pending
*work
;
277 struct hid_report
*report
= picolcd_out_report(report_id
, hdev
);
281 if (!report
|| !data
)
283 if (data
->status
& PICOLCD_FAILED
)
285 work
= kzalloc(sizeof(*work
), GFP_KERNEL
);
289 init_completion(&work
->ready
);
290 work
->out_report
= report
;
291 work
->in_report
= NULL
;
294 mutex_lock(&data
->mutex
);
295 spin_lock_irqsave(&data
->lock
, flags
);
296 for (i
= k
= 0; i
< report
->maxfield
; i
++)
297 for (j
= 0; j
< report
->field
[i
]->report_count
; j
++) {
298 hid_set_field(report
->field
[i
], j
, k
< size
? raw_data
[k
] : 0);
301 data
->pending
= work
;
302 usbhid_submit_report(data
->hdev
, report
, USB_DIR_OUT
);
303 spin_unlock_irqrestore(&data
->lock
, flags
);
304 wait_for_completion_interruptible_timeout(&work
->ready
, HZ
*2);
305 spin_lock_irqsave(&data
->lock
, flags
);
306 data
->pending
= NULL
;
307 spin_unlock_irqrestore(&data
->lock
, flags
);
308 mutex_unlock(&data
->mutex
);
312 #ifdef CONFIG_HID_PICOLCD_FB
313 /* Send a given tile to PicoLCD */
314 static int picolcd_fb_send_tile(struct hid_device
*hdev
, int chip
, int tile
)
316 struct picolcd_data
*data
= hid_get_drvdata(hdev
);
317 struct hid_report
*report1
= picolcd_out_report(REPORT_LCD_CMD_DATA
, hdev
);
318 struct hid_report
*report2
= picolcd_out_report(REPORT_LCD_DATA
, hdev
);
323 if (!report1
|| report1
->maxfield
!= 1 || !report2
|| report2
->maxfield
!= 1)
326 spin_lock_irqsave(&data
->lock
, flags
);
327 hid_set_field(report1
->field
[0], 0, chip
<< 2);
328 hid_set_field(report1
->field
[0], 1, 0x02);
329 hid_set_field(report1
->field
[0], 2, 0x00);
330 hid_set_field(report1
->field
[0], 3, 0x00);
331 hid_set_field(report1
->field
[0], 4, 0xb8 | tile
);
332 hid_set_field(report1
->field
[0], 5, 0x00);
333 hid_set_field(report1
->field
[0], 6, 0x00);
334 hid_set_field(report1
->field
[0], 7, 0x40);
335 hid_set_field(report1
->field
[0], 8, 0x00);
336 hid_set_field(report1
->field
[0], 9, 0x00);
337 hid_set_field(report1
->field
[0], 10, 32);
339 hid_set_field(report2
->field
[0], 0, (chip
<< 2) | 0x01);
340 hid_set_field(report2
->field
[0], 1, 0x00);
341 hid_set_field(report2
->field
[0], 2, 0x00);
342 hid_set_field(report2
->field
[0], 3, 32);
344 tdata
= data
->fb_vbitmap
+ (tile
* 4 + chip
) * 64;
345 for (i
= 0; i
< 64; i
++)
347 hid_set_field(report1
->field
[0], 11 + i
, tdata
[i
]);
349 hid_set_field(report2
->field
[0], 4 + i
- 32, tdata
[i
]);
351 usbhid_submit_report(data
->hdev
, report1
, USB_DIR_OUT
);
352 usbhid_submit_report(data
->hdev
, report2
, USB_DIR_OUT
);
353 spin_unlock_irqrestore(&data
->lock
, flags
);
357 /* Translate a single tile*/
358 static int picolcd_fb_update_tile(u8
*vbitmap
, const u8
*bitmap
, int bpp
,
361 int i
, b
, changed
= 0;
363 u8
*vdata
= vbitmap
+ (tile
* 4 + chip
) * 64;
366 for (b
= 7; b
>= 0; b
--) {
367 const u8
*bdata
= bitmap
+ tile
* 256 + chip
* 8 + b
* 32;
368 for (i
= 0; i
< 64; i
++) {
370 tdata
[i
] |= (bdata
[i
/8] >> (i
% 8)) & 0x01;
373 } else if (bpp
== 8) {
374 for (b
= 7; b
>= 0; b
--) {
375 const u8
*bdata
= bitmap
+ (tile
* 256 + chip
* 8 + b
* 32) * 8;
376 for (i
= 0; i
< 64; i
++) {
378 tdata
[i
] |= (bdata
[i
] & 0x80) ? 0x01 : 0x00;
382 /* Oops, we should never get here! */
387 for (i
= 0; i
< 64; i
++)
388 if (tdata
[i
] != vdata
[i
]) {
395 /* Reconfigure LCD display */
396 static int picolcd_fb_reset(struct picolcd_data
*data
, int clear
)
398 struct hid_report
*report
= picolcd_out_report(REPORT_LCD_CMD
, data
->hdev
);
401 static const u8 mapcmd
[8] = { 0x00, 0x02, 0x00, 0x64, 0x3f, 0x00, 0x64, 0xc0 };
403 if (!report
|| report
->maxfield
!= 1)
406 spin_lock_irqsave(&data
->lock
, flags
);
407 for (i
= 0; i
< 4; i
++) {
408 for (j
= 0; j
< report
->field
[0]->maxusage
; j
++)
410 hid_set_field(report
->field
[0], j
, i
<< 2);
411 else if (j
< sizeof(mapcmd
))
412 hid_set_field(report
->field
[0], j
, mapcmd
[j
]);
414 hid_set_field(report
->field
[0], j
, 0);
415 usbhid_submit_report(data
->hdev
, report
, USB_DIR_OUT
);
418 data
->status
|= PICOLCD_READY_FB
;
419 spin_unlock_irqrestore(&data
->lock
, flags
);
421 if (data
->fb_bitmap
) {
423 memset(data
->fb_vbitmap
, 0, PICOLCDFB_SIZE
);
424 memset(data
->fb_bitmap
, 0, PICOLCDFB_SIZE
*data
->fb_bpp
);
429 /* schedule first output of framebuffer */
431 schedule_delayed_work(&data
->fb_info
->deferred_work
, 0);
436 /* Update fb_vbitmap from the screen_base and send changed tiles to device */
437 static void picolcd_fb_update(struct picolcd_data
*data
)
442 spin_lock_irqsave(&data
->lock
, flags
);
443 if (!(data
->status
& PICOLCD_READY_FB
)) {
444 spin_unlock_irqrestore(&data
->lock
, flags
);
445 picolcd_fb_reset(data
, 0);
447 spin_unlock_irqrestore(&data
->lock
, flags
);
451 * Translate the framebuffer into the format needed by the PicoLCD.
452 * See display layout above.
453 * Do this one tile after the other and push those tiles that changed.
455 * Wait for our IO to complete as otherwise we might flood the queue!
458 for (chip
= 0; chip
< 4; chip
++)
459 for (tile
= 0; tile
< 8; tile
++)
460 if (picolcd_fb_update_tile(data
->fb_vbitmap
,
461 data
->fb_bitmap
, data
->fb_bpp
, chip
, tile
) ||
464 if (n
>= HID_OUTPUT_FIFO_SIZE
/ 2) {
465 usbhid_wait_io(data
->hdev
);
468 picolcd_fb_send_tile(data
->hdev
, chip
, tile
);
470 data
->fb_force
= false;
472 usbhid_wait_io(data
->hdev
);
475 /* Stub to call the system default and update the image on the picoLCD */
476 static void picolcd_fb_fillrect(struct fb_info
*info
,
477 const struct fb_fillrect
*rect
)
481 sys_fillrect(info
, rect
);
483 schedule_delayed_work(&info
->deferred_work
, 0);
486 /* Stub to call the system default and update the image on the picoLCD */
487 static void picolcd_fb_copyarea(struct fb_info
*info
,
488 const struct fb_copyarea
*area
)
492 sys_copyarea(info
, area
);
494 schedule_delayed_work(&info
->deferred_work
, 0);
497 /* Stub to call the system default and update the image on the picoLCD */
498 static void picolcd_fb_imageblit(struct fb_info
*info
, const struct fb_image
*image
)
502 sys_imageblit(info
, image
);
504 schedule_delayed_work(&info
->deferred_work
, 0);
508 * this is the slow path from userspace. they can seek and write to
509 * the fb. it's inefficient to do anything less than a full screen draw
511 static ssize_t
picolcd_fb_write(struct fb_info
*info
, const char __user
*buf
,
512 size_t count
, loff_t
*ppos
)
517 ret
= fb_sys_write(info
, buf
, count
, ppos
);
519 schedule_delayed_work(&info
->deferred_work
, 0);
523 static int picolcd_fb_blank(int blank
, struct fb_info
*info
)
527 /* We let fb notification do this for us via lcd/backlight device */
531 static void picolcd_fb_destroy(struct fb_info
*info
)
533 struct picolcd_data
*data
= info
->par
;
536 data
->fb_info
= NULL
;
537 fb_deferred_io_cleanup(info
);
538 framebuffer_release(info
);
541 static int picolcd_fb_check_var(struct fb_var_screeninfo
*var
, struct fb_info
*info
)
543 __u32 bpp
= var
->bits_per_pixel
;
544 __u32 activate
= var
->activate
;
546 /* only allow 1/8 bit depth (8-bit is grayscale) */
547 *var
= picolcdfb_var
;
548 var
->activate
= activate
;
550 var
->bits_per_pixel
= 8;
552 var
->green
.length
= 8;
553 var
->blue
.length
= 8;
555 var
->bits_per_pixel
= 1;
557 var
->green
.length
= 1;
558 var
->blue
.length
= 1;
563 static int picolcd_set_par(struct fb_info
*info
)
565 struct picolcd_data
*data
= info
->par
;
567 if (info
->var
.bits_per_pixel
== data
->fb_bpp
)
569 /* switch between 1/8 bit depths */
570 if (info
->var
.bits_per_pixel
!= 1 && info
->var
.bits_per_pixel
!= 8)
573 o_fb
= data
->fb_bitmap
;
574 tmp_fb
= kmalloc(PICOLCDFB_SIZE
*info
->var
.bits_per_pixel
, GFP_KERNEL
);
578 /* translate FB content to new bits-per-pixel */
579 if (info
->var
.bits_per_pixel
== 1) {
581 for (i
= 0; i
< PICOLCDFB_SIZE
; i
++) {
583 for (b
= 0; b
< 8; b
++) {
585 p
|= o_fb
[i
*8+b
] ? 0x01 : 0x00;
589 memcpy(o_fb
, tmp_fb
, PICOLCDFB_SIZE
);
590 info
->fix
.visual
= FB_VISUAL_MONO01
;
591 info
->fix
.line_length
= PICOLCDFB_WIDTH
/ 8;
594 memcpy(tmp_fb
, o_fb
, PICOLCDFB_SIZE
);
595 for (i
= 0; i
< PICOLCDFB_SIZE
* 8; i
++)
596 o_fb
[i
] = tmp_fb
[i
/8] & (0x01 << (7 - i
% 8)) ? 0xff : 0x00;
597 info
->fix
.visual
= FB_VISUAL_DIRECTCOLOR
;
598 info
->fix
.line_length
= PICOLCDFB_WIDTH
;
602 data
->fb_bpp
= info
->var
.bits_per_pixel
;
606 /* Note this can't be const because of struct fb_info definition */
607 static struct fb_ops picolcdfb_ops
= {
608 .owner
= THIS_MODULE
,
609 .fb_destroy
= picolcd_fb_destroy
,
610 .fb_read
= fb_sys_read
,
611 .fb_write
= picolcd_fb_write
,
612 .fb_blank
= picolcd_fb_blank
,
613 .fb_fillrect
= picolcd_fb_fillrect
,
614 .fb_copyarea
= picolcd_fb_copyarea
,
615 .fb_imageblit
= picolcd_fb_imageblit
,
616 .fb_check_var
= picolcd_fb_check_var
,
617 .fb_set_par
= picolcd_set_par
,
621 /* Callback from deferred IO workqueue */
622 static void picolcd_fb_deferred_io(struct fb_info
*info
, struct list_head
*pagelist
)
624 picolcd_fb_update(info
->par
);
627 static const struct fb_deferred_io picolcd_fb_defio
= {
628 .delay
= HZ
/ PICOLCDFB_UPDATE_RATE_DEFAULT
,
629 .deferred_io
= picolcd_fb_deferred_io
,
634 * The "fb_update_rate" sysfs attribute
636 static ssize_t
picolcd_fb_update_rate_show(struct device
*dev
,
637 struct device_attribute
*attr
, char *buf
)
639 struct picolcd_data
*data
= dev_get_drvdata(dev
);
640 unsigned i
, fb_update_rate
= data
->fb_update_rate
;
643 for (i
= 1; i
<= PICOLCDFB_UPDATE_RATE_LIMIT
; i
++)
644 if (ret
>= PAGE_SIZE
)
646 else if (i
== fb_update_rate
)
647 ret
+= snprintf(buf
+ret
, PAGE_SIZE
-ret
, "[%u] ", i
);
649 ret
+= snprintf(buf
+ret
, PAGE_SIZE
-ret
, "%u ", i
);
651 buf
[min(ret
, (size_t)PAGE_SIZE
)-1] = '\n';
655 static ssize_t
picolcd_fb_update_rate_store(struct device
*dev
,
656 struct device_attribute
*attr
, const char *buf
, size_t count
)
658 struct picolcd_data
*data
= dev_get_drvdata(dev
);
662 if (count
< 1 || count
> 10)
665 i
= sscanf(buf
, "%u", &u
);
669 if (u
> PICOLCDFB_UPDATE_RATE_LIMIT
)
672 u
= PICOLCDFB_UPDATE_RATE_DEFAULT
;
674 data
->fb_update_rate
= u
;
675 data
->fb_defio
.delay
= HZ
/ data
->fb_update_rate
;
679 static DEVICE_ATTR(fb_update_rate
, 0666, picolcd_fb_update_rate_show
,
680 picolcd_fb_update_rate_store
);
682 /* initialize Framebuffer device */
683 static int picolcd_init_framebuffer(struct picolcd_data
*data
)
685 struct device
*dev
= &data
->hdev
->dev
;
686 struct fb_info
*info
= NULL
;
687 int i
, error
= -ENOMEM
;
688 u8
*fb_vbitmap
= NULL
;
689 u8
*fb_bitmap
= NULL
;
692 fb_bitmap
= vmalloc(PICOLCDFB_SIZE
*8);
693 if (fb_bitmap
== NULL
) {
694 dev_err(dev
, "can't get a free page for framebuffer\n");
698 fb_vbitmap
= kmalloc(PICOLCDFB_SIZE
, GFP_KERNEL
);
699 if (fb_vbitmap
== NULL
) {
700 dev_err(dev
, "can't alloc vbitmap image buffer\n");
704 data
->fb_update_rate
= PICOLCDFB_UPDATE_RATE_DEFAULT
;
705 data
->fb_defio
= picolcd_fb_defio
;
706 /* The extra memory is:
707 * - struct picolcd_fb_cleanup_item
708 * - u32 for ref_count
709 * - 256*u32 for pseudo_palette
711 info
= framebuffer_alloc(257 * sizeof(u32
), dev
);
713 dev_err(dev
, "failed to allocate a framebuffer\n");
720 for (i
= 0; i
< 256; i
++)
721 palette
[i
] = i
> 0 && i
< 16 ? 0xff : 0;
722 info
->pseudo_palette
= palette
;
723 info
->fbdefio
= &data
->fb_defio
;
724 info
->screen_base
= (char __force __iomem
*)fb_bitmap
;
725 info
->fbops
= &picolcdfb_ops
;
726 info
->var
= picolcdfb_var
;
727 info
->fix
= picolcdfb_fix
;
728 info
->fix
.smem_len
= PICOLCDFB_SIZE
*8;
729 info
->fix
.smem_start
= (unsigned long)fb_bitmap
;
731 info
->flags
= FBINFO_FLAG_DEFAULT
;
733 data
->fb_vbitmap
= fb_vbitmap
;
734 data
->fb_bitmap
= fb_bitmap
;
735 data
->fb_bpp
= picolcdfb_var
.bits_per_pixel
;
736 error
= picolcd_fb_reset(data
, 1);
738 dev_err(dev
, "failed to configure display\n");
741 error
= device_create_file(dev
, &dev_attr_fb_update_rate
);
743 dev_err(dev
, "failed to create sysfs attributes\n");
746 fb_deferred_io_init(info
);
747 data
->fb_info
= info
;
748 error
= register_framebuffer(info
);
750 dev_err(dev
, "failed to register framebuffer\n");
753 /* schedule first output of framebuffer */
755 schedule_delayed_work(&info
->deferred_work
, 0);
759 fb_deferred_io_cleanup(info
);
760 device_remove_file(dev
, &dev_attr_fb_update_rate
);
762 data
->fb_vbitmap
= NULL
;
763 data
->fb_bitmap
= NULL
;
765 data
->fb_info
= NULL
;
768 framebuffer_release(info
);
774 static void picolcd_exit_framebuffer(struct picolcd_data
*data
)
776 struct fb_info
*info
= data
->fb_info
;
777 u8
*fb_vbitmap
= data
->fb_vbitmap
;
778 u8
*fb_bitmap
= data
->fb_bitmap
;
783 data
->fb_vbitmap
= NULL
;
784 data
->fb_bitmap
= NULL
;
786 data
->fb_info
= NULL
;
787 device_remove_file(&data
->hdev
->dev
, &dev_attr_fb_update_rate
);
788 unregister_framebuffer(info
);
793 #define picolcd_fbinfo(d) ((d)->fb_info)
795 static inline int picolcd_fb_reset(struct picolcd_data
*data
, int clear
)
799 static inline int picolcd_init_framebuffer(struct picolcd_data
*data
)
803 static inline void picolcd_exit_framebuffer(struct picolcd_data
*data
)
806 #define picolcd_fbinfo(d) NULL
807 #endif /* CONFIG_HID_PICOLCD_FB */
809 #ifdef CONFIG_HID_PICOLCD_BACKLIGHT
811 * backlight class device
813 static int picolcd_get_brightness(struct backlight_device
*bdev
)
815 struct picolcd_data
*data
= bl_get_data(bdev
);
816 return data
->lcd_brightness
;
819 static int picolcd_set_brightness(struct backlight_device
*bdev
)
821 struct picolcd_data
*data
= bl_get_data(bdev
);
822 struct hid_report
*report
= picolcd_out_report(REPORT_BRIGHTNESS
, data
->hdev
);
825 if (!report
|| report
->maxfield
!= 1 || report
->field
[0]->report_count
!= 1)
828 data
->lcd_brightness
= bdev
->props
.brightness
& 0x0ff;
829 data
->lcd_power
= bdev
->props
.power
;
830 spin_lock_irqsave(&data
->lock
, flags
);
831 hid_set_field(report
->field
[0], 0, data
->lcd_power
== FB_BLANK_UNBLANK
? data
->lcd_brightness
: 0);
832 usbhid_submit_report(data
->hdev
, report
, USB_DIR_OUT
);
833 spin_unlock_irqrestore(&data
->lock
, flags
);
837 static int picolcd_check_bl_fb(struct backlight_device
*bdev
, struct fb_info
*fb
)
839 return fb
&& fb
== picolcd_fbinfo((struct picolcd_data
*)bl_get_data(bdev
));
842 static const struct backlight_ops picolcd_blops
= {
843 .update_status
= picolcd_set_brightness
,
844 .get_brightness
= picolcd_get_brightness
,
845 .check_fb
= picolcd_check_bl_fb
,
848 static int picolcd_init_backlight(struct picolcd_data
*data
, struct hid_report
*report
)
850 struct device
*dev
= &data
->hdev
->dev
;
851 struct backlight_device
*bdev
;
852 struct backlight_properties props
;
855 if (report
->maxfield
!= 1 || report
->field
[0]->report_count
!= 1 ||
856 report
->field
[0]->report_size
!= 8) {
857 dev_err(dev
, "unsupported BRIGHTNESS report");
861 memset(&props
, 0, sizeof(props
));
862 props
.max_brightness
= 0xff;
863 bdev
= backlight_device_register(dev_name(dev
), dev
, data
,
864 &picolcd_blops
, &props
);
866 dev_err(dev
, "failed to register backlight\n");
867 return PTR_ERR(bdev
);
869 bdev
->props
.brightness
= 0xff;
870 data
->lcd_brightness
= 0xff;
871 data
->backlight
= bdev
;
872 picolcd_set_brightness(bdev
);
876 static void picolcd_exit_backlight(struct picolcd_data
*data
)
878 struct backlight_device
*bdev
= data
->backlight
;
880 data
->backlight
= NULL
;
882 backlight_device_unregister(bdev
);
885 static inline int picolcd_resume_backlight(struct picolcd_data
*data
)
887 if (!data
->backlight
)
889 return picolcd_set_brightness(data
->backlight
);
893 static void picolcd_suspend_backlight(struct picolcd_data
*data
)
895 int bl_power
= data
->lcd_power
;
896 if (!data
->backlight
)
899 data
->backlight
->props
.power
= FB_BLANK_POWERDOWN
;
900 picolcd_set_brightness(data
->backlight
);
901 data
->lcd_power
= data
->backlight
->props
.power
= bl_power
;
903 #endif /* CONFIG_PM */
905 static inline int picolcd_init_backlight(struct picolcd_data
*data
,
906 struct hid_report
*report
)
910 static inline void picolcd_exit_backlight(struct picolcd_data
*data
)
913 static inline int picolcd_resume_backlight(struct picolcd_data
*data
)
917 static inline void picolcd_suspend_backlight(struct picolcd_data
*data
)
920 #endif /* CONFIG_HID_PICOLCD_BACKLIGHT */
922 #ifdef CONFIG_HID_PICOLCD_LCD
926 static int picolcd_get_contrast(struct lcd_device
*ldev
)
928 struct picolcd_data
*data
= lcd_get_data(ldev
);
929 return data
->lcd_contrast
;
932 static int picolcd_set_contrast(struct lcd_device
*ldev
, int contrast
)
934 struct picolcd_data
*data
= lcd_get_data(ldev
);
935 struct hid_report
*report
= picolcd_out_report(REPORT_CONTRAST
, data
->hdev
);
938 if (!report
|| report
->maxfield
!= 1 || report
->field
[0]->report_count
!= 1)
941 data
->lcd_contrast
= contrast
& 0x0ff;
942 spin_lock_irqsave(&data
->lock
, flags
);
943 hid_set_field(report
->field
[0], 0, data
->lcd_contrast
);
944 usbhid_submit_report(data
->hdev
, report
, USB_DIR_OUT
);
945 spin_unlock_irqrestore(&data
->lock
, flags
);
949 static int picolcd_check_lcd_fb(struct lcd_device
*ldev
, struct fb_info
*fb
)
951 return fb
&& fb
== picolcd_fbinfo((struct picolcd_data
*)lcd_get_data(ldev
));
954 static struct lcd_ops picolcd_lcdops
= {
955 .get_contrast
= picolcd_get_contrast
,
956 .set_contrast
= picolcd_set_contrast
,
957 .check_fb
= picolcd_check_lcd_fb
,
960 static int picolcd_init_lcd(struct picolcd_data
*data
, struct hid_report
*report
)
962 struct device
*dev
= &data
->hdev
->dev
;
963 struct lcd_device
*ldev
;
967 if (report
->maxfield
!= 1 || report
->field
[0]->report_count
!= 1 ||
968 report
->field
[0]->report_size
!= 8) {
969 dev_err(dev
, "unsupported CONTRAST report");
973 ldev
= lcd_device_register(dev_name(dev
), dev
, data
, &picolcd_lcdops
);
975 dev_err(dev
, "failed to register LCD\n");
976 return PTR_ERR(ldev
);
978 ldev
->props
.max_contrast
= 0x0ff;
979 data
->lcd_contrast
= 0xe5;
981 picolcd_set_contrast(ldev
, 0xe5);
985 static void picolcd_exit_lcd(struct picolcd_data
*data
)
987 struct lcd_device
*ldev
= data
->lcd
;
991 lcd_device_unregister(ldev
);
994 static inline int picolcd_resume_lcd(struct picolcd_data
*data
)
998 return picolcd_set_contrast(data
->lcd
, data
->lcd_contrast
);
1001 static inline int picolcd_init_lcd(struct picolcd_data
*data
,
1002 struct hid_report
*report
)
1006 static inline void picolcd_exit_lcd(struct picolcd_data
*data
)
1009 static inline int picolcd_resume_lcd(struct picolcd_data
*data
)
1013 #endif /* CONFIG_HID_PICOLCD_LCD */
1015 #ifdef CONFIG_HID_PICOLCD_LEDS
1019 static void picolcd_leds_set(struct picolcd_data
*data
)
1021 struct hid_report
*report
;
1022 unsigned long flags
;
1026 report
= picolcd_out_report(REPORT_LED_STATE
, data
->hdev
);
1027 if (!report
|| report
->maxfield
!= 1 || report
->field
[0]->report_count
!= 1)
1030 spin_lock_irqsave(&data
->lock
, flags
);
1031 hid_set_field(report
->field
[0], 0, data
->led_state
);
1032 usbhid_submit_report(data
->hdev
, report
, USB_DIR_OUT
);
1033 spin_unlock_irqrestore(&data
->lock
, flags
);
1036 static void picolcd_led_set_brightness(struct led_classdev
*led_cdev
,
1037 enum led_brightness value
)
1040 struct hid_device
*hdev
;
1041 struct picolcd_data
*data
;
1044 dev
= led_cdev
->dev
->parent
;
1045 hdev
= container_of(dev
, struct hid_device
, dev
);
1046 data
= hid_get_drvdata(hdev
);
1047 for (i
= 0; i
< 8; i
++) {
1048 if (led_cdev
!= data
->led
[i
])
1050 state
= (data
->led_state
>> i
) & 1;
1051 if (value
== LED_OFF
&& state
) {
1052 data
->led_state
&= ~(1 << i
);
1053 picolcd_leds_set(data
);
1054 } else if (value
!= LED_OFF
&& !state
) {
1055 data
->led_state
|= 1 << i
;
1056 picolcd_leds_set(data
);
1062 static enum led_brightness
picolcd_led_get_brightness(struct led_classdev
*led_cdev
)
1065 struct hid_device
*hdev
;
1066 struct picolcd_data
*data
;
1069 dev
= led_cdev
->dev
->parent
;
1070 hdev
= container_of(dev
, struct hid_device
, dev
);
1071 data
= hid_get_drvdata(hdev
);
1072 for (i
= 0; i
< 8; i
++)
1073 if (led_cdev
== data
->led
[i
]) {
1074 value
= (data
->led_state
>> i
) & 1;
1077 return value
? LED_FULL
: LED_OFF
;
1080 static int picolcd_init_leds(struct picolcd_data
*data
, struct hid_report
*report
)
1082 struct device
*dev
= &data
->hdev
->dev
;
1083 struct led_classdev
*led
;
1084 size_t name_sz
= strlen(dev_name(dev
)) + 8;
1090 if (report
->maxfield
!= 1 || report
->field
[0]->report_count
!= 1 ||
1091 report
->field
[0]->report_size
!= 8) {
1092 dev_err(dev
, "unsupported LED_STATE report");
1096 for (i
= 0; i
< 8; i
++) {
1097 led
= kzalloc(sizeof(struct led_classdev
)+name_sz
, GFP_KERNEL
);
1099 dev_err(dev
, "can't allocate memory for LED %d\n", i
);
1103 name
= (void *)(&led
[1]);
1104 snprintf(name
, name_sz
, "%s::GPO%d", dev_name(dev
), i
);
1106 led
->brightness
= 0;
1107 led
->max_brightness
= 1;
1108 led
->brightness_get
= picolcd_led_get_brightness
;
1109 led
->brightness_set
= picolcd_led_set_brightness
;
1112 ret
= led_classdev_register(dev
, data
->led
[i
]);
1114 data
->led
[i
] = NULL
;
1116 dev_err(dev
, "can't register LED %d\n", i
);
1122 for (i
= 0; i
< 8; i
++)
1125 data
->led
[i
] = NULL
;
1126 led_classdev_unregister(led
);
1132 static void picolcd_exit_leds(struct picolcd_data
*data
)
1134 struct led_classdev
*led
;
1137 for (i
= 0; i
< 8; i
++) {
1139 data
->led
[i
] = NULL
;
1142 led_classdev_unregister(led
);
1148 static inline int picolcd_init_leds(struct picolcd_data
*data
,
1149 struct hid_report
*report
)
1153 static inline void picolcd_exit_leds(struct picolcd_data
*data
)
1156 static inline int picolcd_leds_set(struct picolcd_data
*data
)
1160 #endif /* CONFIG_HID_PICOLCD_LEDS */
1163 * input class device
1165 static int picolcd_raw_keypad(struct picolcd_data
*data
,
1166 struct hid_report
*report
, u8
*raw_data
, int size
)
1170 * First and second data bytes list currently pressed keys,
1171 * 0x00 means no key and at most 2 keys may be pressed at same time
1175 /* determine newly pressed keys */
1176 for (i
= 0; i
< size
; i
++) {
1177 unsigned int key_code
;
1178 if (raw_data
[i
] == 0)
1180 for (j
= 0; j
< sizeof(data
->pressed_keys
); j
++)
1181 if (data
->pressed_keys
[j
] == raw_data
[i
])
1182 goto key_already_down
;
1183 for (j
= 0; j
< sizeof(data
->pressed_keys
); j
++)
1184 if (data
->pressed_keys
[j
] == 0) {
1185 data
->pressed_keys
[j
] = raw_data
[i
];
1188 input_event(data
->input_keys
, EV_MSC
, MSC_SCAN
, raw_data
[i
]);
1189 if (raw_data
[i
] < PICOLCD_KEYS
)
1190 key_code
= data
->keycode
[raw_data
[i
]];
1192 key_code
= KEY_UNKNOWN
;
1193 if (key_code
!= KEY_UNKNOWN
) {
1194 dbg_hid(PICOLCD_NAME
" got key press for %u:%d",
1195 raw_data
[i
], key_code
);
1196 input_report_key(data
->input_keys
, key_code
, 1);
1198 input_sync(data
->input_keys
);
1203 /* determine newly released keys */
1204 for (j
= 0; j
< sizeof(data
->pressed_keys
); j
++) {
1205 unsigned int key_code
;
1206 if (data
->pressed_keys
[j
] == 0)
1208 for (i
= 0; i
< size
; i
++)
1209 if (data
->pressed_keys
[j
] == raw_data
[i
])
1210 goto key_still_down
;
1211 input_event(data
->input_keys
, EV_MSC
, MSC_SCAN
, data
->pressed_keys
[j
]);
1212 if (data
->pressed_keys
[j
] < PICOLCD_KEYS
)
1213 key_code
= data
->keycode
[data
->pressed_keys
[j
]];
1215 key_code
= KEY_UNKNOWN
;
1216 if (key_code
!= KEY_UNKNOWN
) {
1217 dbg_hid(PICOLCD_NAME
" got key release for %u:%d",
1218 data
->pressed_keys
[j
], key_code
);
1219 input_report_key(data
->input_keys
, key_code
, 0);
1221 input_sync(data
->input_keys
);
1222 data
->pressed_keys
[j
] = 0;
1229 static int picolcd_raw_cir(struct picolcd_data
*data
,
1230 struct hid_report
*report
, u8
*raw_data
, int size
)
1232 /* Need understanding of CIR data format to implement ... */
1236 static int picolcd_check_version(struct hid_device
*hdev
)
1238 struct picolcd_data
*data
= hid_get_drvdata(hdev
);
1239 struct picolcd_pending
*verinfo
;
1245 verinfo
= picolcd_send_and_wait(hdev
, REPORT_VERSION
, NULL
, 0);
1247 dev_err(&hdev
->dev
, "no version response from PicoLCD");
1251 if (verinfo
->raw_size
== 2) {
1252 data
->version
[0] = verinfo
->raw_data
[1];
1253 data
->version
[1] = verinfo
->raw_data
[0];
1254 if (data
->status
& PICOLCD_BOOTLOADER
) {
1255 dev_info(&hdev
->dev
, "PicoLCD, bootloader version %d.%d\n",
1256 verinfo
->raw_data
[1], verinfo
->raw_data
[0]);
1258 dev_info(&hdev
->dev
, "PicoLCD, firmware version %d.%d\n",
1259 verinfo
->raw_data
[1], verinfo
->raw_data
[0]);
1262 dev_err(&hdev
->dev
, "confused, got unexpected version response from PicoLCD\n");
1270 * Reset our device and wait for answer to VERSION request
1272 static int picolcd_reset(struct hid_device
*hdev
)
1274 struct picolcd_data
*data
= hid_get_drvdata(hdev
);
1275 struct hid_report
*report
= picolcd_out_report(REPORT_RESET
, hdev
);
1276 unsigned long flags
;
1279 if (!data
|| !report
|| report
->maxfield
!= 1)
1282 spin_lock_irqsave(&data
->lock
, flags
);
1283 if (hdev
->product
== USB_DEVICE_ID_PICOLCD_BOOTLOADER
)
1284 data
->status
|= PICOLCD_BOOTLOADER
;
1286 /* perform the reset */
1287 hid_set_field(report
->field
[0], 0, 1);
1288 usbhid_submit_report(hdev
, report
, USB_DIR_OUT
);
1289 spin_unlock_irqrestore(&data
->lock
, flags
);
1291 error
= picolcd_check_version(hdev
);
1295 picolcd_resume_lcd(data
);
1296 picolcd_resume_backlight(data
);
1297 #ifdef CONFIG_HID_PICOLCD_FB
1299 schedule_delayed_work(&data
->fb_info
->deferred_work
, 0);
1300 #endif /* CONFIG_HID_PICOLCD_FB */
1302 picolcd_leds_set(data
);
1307 * The "operation_mode" sysfs attribute
1309 static ssize_t
picolcd_operation_mode_show(struct device
*dev
,
1310 struct device_attribute
*attr
, char *buf
)
1312 struct picolcd_data
*data
= dev_get_drvdata(dev
);
1314 if (data
->status
& PICOLCD_BOOTLOADER
)
1315 return snprintf(buf
, PAGE_SIZE
, "[bootloader] lcd\n");
1317 return snprintf(buf
, PAGE_SIZE
, "bootloader [lcd]\n");
1320 static ssize_t
picolcd_operation_mode_store(struct device
*dev
,
1321 struct device_attribute
*attr
, const char *buf
, size_t count
)
1323 struct picolcd_data
*data
= dev_get_drvdata(dev
);
1324 struct hid_report
*report
= NULL
;
1326 int timeout
= data
->opmode_delay
;
1327 unsigned long flags
;
1329 if (cnt
>= 3 && strncmp("lcd", buf
, 3) == 0) {
1330 if (data
->status
& PICOLCD_BOOTLOADER
)
1331 report
= picolcd_out_report(REPORT_EXIT_FLASHER
, data
->hdev
);
1334 } else if (cnt
>= 10 && strncmp("bootloader", buf
, 10) == 0) {
1335 if (!(data
->status
& PICOLCD_BOOTLOADER
))
1336 report
= picolcd_out_report(REPORT_EXIT_KEYBOARD
, data
->hdev
);
1343 while (cnt
> 0 && (buf
[cnt
-1] == '\n' || buf
[cnt
-1] == '\r'))
1348 spin_lock_irqsave(&data
->lock
, flags
);
1349 hid_set_field(report
->field
[0], 0, timeout
& 0xff);
1350 hid_set_field(report
->field
[0], 1, (timeout
>> 8) & 0xff);
1351 usbhid_submit_report(data
->hdev
, report
, USB_DIR_OUT
);
1352 spin_unlock_irqrestore(&data
->lock
, flags
);
1356 static DEVICE_ATTR(operation_mode
, 0644, picolcd_operation_mode_show
,
1357 picolcd_operation_mode_store
);
1360 * The "operation_mode_delay" sysfs attribute
1362 static ssize_t
picolcd_operation_mode_delay_show(struct device
*dev
,
1363 struct device_attribute
*attr
, char *buf
)
1365 struct picolcd_data
*data
= dev_get_drvdata(dev
);
1367 return snprintf(buf
, PAGE_SIZE
, "%hu\n", data
->opmode_delay
);
1370 static ssize_t
picolcd_operation_mode_delay_store(struct device
*dev
,
1371 struct device_attribute
*attr
, const char *buf
, size_t count
)
1373 struct picolcd_data
*data
= dev_get_drvdata(dev
);
1375 if (sscanf(buf
, "%u", &u
) != 1)
1380 data
->opmode_delay
= u
;
1384 static DEVICE_ATTR(operation_mode_delay
, 0644, picolcd_operation_mode_delay_show
,
1385 picolcd_operation_mode_delay_store
);
1388 #ifdef CONFIG_DEBUG_FS
1392 static int picolcd_debug_reset_show(struct seq_file
*f
, void *p
)
1394 if (picolcd_fbinfo((struct picolcd_data
*)f
->private))
1395 seq_printf(f
, "all fb\n");
1397 seq_printf(f
, "all\n");
1401 static int picolcd_debug_reset_open(struct inode
*inode
, struct file
*f
)
1403 return single_open(f
, picolcd_debug_reset_show
, inode
->i_private
);
1406 static ssize_t
picolcd_debug_reset_write(struct file
*f
, const char __user
*user_buf
,
1407 size_t count
, loff_t
*ppos
)
1409 struct picolcd_data
*data
= ((struct seq_file
*)f
->private_data
)->private;
1411 size_t cnt
= min(count
, sizeof(buf
)-1);
1412 if (copy_from_user(buf
, user_buf
, cnt
))
1415 while (cnt
> 0 && (buf
[cnt
-1] == ' ' || buf
[cnt
-1] == '\n'))
1418 if (strcmp(buf
, "all") == 0) {
1419 picolcd_reset(data
->hdev
);
1420 picolcd_fb_reset(data
, 1);
1421 } else if (strcmp(buf
, "fb") == 0) {
1422 picolcd_fb_reset(data
, 1);
1429 static const struct file_operations picolcd_debug_reset_fops
= {
1430 .owner
= THIS_MODULE
,
1431 .open
= picolcd_debug_reset_open
,
1433 .llseek
= seq_lseek
,
1434 .write
= picolcd_debug_reset_write
,
1435 .release
= single_release
,
1441 static int picolcd_debug_eeprom_open(struct inode
*i
, struct file
*f
)
1443 f
->private_data
= i
->i_private
;
1447 static ssize_t
picolcd_debug_eeprom_read(struct file
*f
, char __user
*u
,
1448 size_t s
, loff_t
*off
)
1450 struct picolcd_data
*data
= f
->private_data
;
1451 struct picolcd_pending
*resp
;
1460 /* prepare buffer with info about what we want to read (addr & len) */
1461 raw_data
[0] = *off
& 0xff;
1462 raw_data
[1] = (*off
>> 8) && 0xff;
1463 raw_data
[2] = s
< 20 ? s
: 20;
1464 if (*off
+ raw_data
[2] > 0xff)
1465 raw_data
[2] = 0x100 - *off
;
1466 resp
= picolcd_send_and_wait(data
->hdev
, REPORT_EE_READ
, raw_data
,
1471 if (resp
->in_report
&& resp
->in_report
->id
== REPORT_EE_DATA
) {
1472 /* successful read :) */
1473 ret
= resp
->raw_data
[2];
1476 if (copy_to_user(u
, resp
->raw_data
+3, ret
))
1480 } /* anything else is some kind of IO error */
1486 static ssize_t
picolcd_debug_eeprom_write(struct file
*f
, const char __user
*u
,
1487 size_t s
, loff_t
*off
)
1489 struct picolcd_data
*data
= f
->private_data
;
1490 struct picolcd_pending
*resp
;
1499 memset(raw_data
, 0, sizeof(raw_data
));
1500 raw_data
[0] = *off
& 0xff;
1501 raw_data
[1] = (*off
>> 8) && 0xff;
1502 raw_data
[2] = s
< 20 ? s
: 20;
1503 if (*off
+ raw_data
[2] > 0xff)
1504 raw_data
[2] = 0x100 - *off
;
1506 if (copy_from_user(raw_data
+3, u
, raw_data
[2]))
1508 resp
= picolcd_send_and_wait(data
->hdev
, REPORT_EE_WRITE
, raw_data
,
1514 if (resp
->in_report
&& resp
->in_report
->id
== REPORT_EE_DATA
) {
1515 /* check if written data matches */
1516 if (memcmp(raw_data
, resp
->raw_data
, 3+raw_data
[2]) == 0) {
1517 *off
+= raw_data
[2];
1527 * - read/write happens in chunks of at most 20 bytes, it's up to userspace
1528 * to loop in order to get more data.
1529 * - on write errors on otherwise correct write request the bytes
1530 * that should have been written are in undefined state.
1532 static const struct file_operations picolcd_debug_eeprom_fops
= {
1533 .owner
= THIS_MODULE
,
1534 .open
= picolcd_debug_eeprom_open
,
1535 .read
= picolcd_debug_eeprom_read
,
1536 .write
= picolcd_debug_eeprom_write
,
1537 .llseek
= generic_file_llseek
,
1543 static int picolcd_debug_flash_open(struct inode
*i
, struct file
*f
)
1545 f
->private_data
= i
->i_private
;
1549 /* record a flash address to buf (bounds check to be done by caller) */
1550 static int _picolcd_flash_setaddr(struct picolcd_data
*data
, u8
*buf
, long off
)
1552 buf
[0] = off
& 0xff;
1553 buf
[1] = (off
>> 8) & 0xff;
1554 if (data
->addr_sz
== 3)
1555 buf
[2] = (off
>> 16) & 0xff;
1556 return data
->addr_sz
== 2 ? 2 : 3;
1559 /* read a given size of data (bounds check to be done by caller) */
1560 static ssize_t
_picolcd_flash_read(struct picolcd_data
*data
, int report_id
,
1561 char __user
*u
, size_t s
, loff_t
*off
)
1563 struct picolcd_pending
*resp
;
1566 int len_off
, err
= -EIO
;
1570 len_off
= _picolcd_flash_setaddr(data
, raw_data
, *off
);
1571 raw_data
[len_off
] = s
> 32 ? 32 : s
;
1572 resp
= picolcd_send_and_wait(data
->hdev
, report_id
, raw_data
, len_off
+1);
1573 if (!resp
|| !resp
->in_report
)
1575 if (resp
->in_report
->id
== REPORT_MEMORY
||
1576 resp
->in_report
->id
== REPORT_BL_READ_MEMORY
) {
1577 if (memcmp(raw_data
, resp
->raw_data
, len_off
+1) != 0)
1579 if (copy_to_user(u
+ret
, resp
->raw_data
+len_off
+1, raw_data
[len_off
])) {
1583 *off
+= raw_data
[len_off
];
1584 s
-= raw_data
[len_off
];
1585 ret
+= raw_data
[len_off
];
1591 return ret
> 0 ? ret
: err
;
1596 static ssize_t
picolcd_debug_flash_read(struct file
*f
, char __user
*u
,
1597 size_t s
, loff_t
*off
)
1599 struct picolcd_data
*data
= f
->private_data
;
1605 if (*off
+ s
> 0x05fff)
1608 if (data
->status
& PICOLCD_BOOTLOADER
)
1609 return _picolcd_flash_read(data
, REPORT_BL_READ_MEMORY
, u
, s
, off
);
1611 return _picolcd_flash_read(data
, REPORT_READ_MEMORY
, u
, s
, off
);
1614 /* erase block aligned to 64bytes boundary */
1615 static ssize_t
_picolcd_flash_erase64(struct picolcd_data
*data
, int report_id
,
1618 struct picolcd_pending
*resp
;
1626 len_off
= _picolcd_flash_setaddr(data
, raw_data
, *off
);
1627 resp
= picolcd_send_and_wait(data
->hdev
, report_id
, raw_data
, len_off
);
1628 if (!resp
|| !resp
->in_report
)
1630 if (resp
->in_report
->id
== REPORT_MEMORY
||
1631 resp
->in_report
->id
== REPORT_BL_ERASE_MEMORY
) {
1632 if (memcmp(raw_data
, resp
->raw_data
, len_off
) != 0)
1641 /* write a given size of data (bounds check to be done by caller) */
1642 static ssize_t
_picolcd_flash_write(struct picolcd_data
*data
, int report_id
,
1643 const char __user
*u
, size_t s
, loff_t
*off
)
1645 struct picolcd_pending
*resp
;
1648 int len_off
, err
= -EIO
;
1652 len_off
= _picolcd_flash_setaddr(data
, raw_data
, *off
);
1653 raw_data
[len_off
] = s
> 32 ? 32 : s
;
1654 if (copy_from_user(raw_data
+len_off
+1, u
, raw_data
[len_off
])) {
1658 resp
= picolcd_send_and_wait(data
->hdev
, report_id
, raw_data
,
1659 len_off
+1+raw_data
[len_off
]);
1660 if (!resp
|| !resp
->in_report
)
1662 if (resp
->in_report
->id
== REPORT_MEMORY
||
1663 resp
->in_report
->id
== REPORT_BL_WRITE_MEMORY
) {
1664 if (memcmp(raw_data
, resp
->raw_data
, len_off
+1+raw_data
[len_off
]) != 0)
1666 *off
+= raw_data
[len_off
];
1667 s
-= raw_data
[len_off
];
1668 ret
+= raw_data
[len_off
];
1676 return ret
> 0 ? ret
: err
;
1679 static ssize_t
picolcd_debug_flash_write(struct file
*f
, const char __user
*u
,
1680 size_t s
, loff_t
*off
)
1682 struct picolcd_data
*data
= f
->private_data
;
1683 ssize_t err
, ret
= 0;
1684 int report_erase
, report_write
;
1695 if (data
->status
& PICOLCD_BOOTLOADER
) {
1696 report_erase
= REPORT_BL_ERASE_MEMORY
;
1697 report_write
= REPORT_BL_WRITE_MEMORY
;
1699 report_erase
= REPORT_ERASE_MEMORY
;
1700 report_write
= REPORT_WRITE_MEMORY
;
1702 mutex_lock(&data
->mutex_flash
);
1704 err
= _picolcd_flash_erase64(data
, report_erase
, off
);
1707 err
= _picolcd_flash_write(data
, report_write
, u
, 64, off
);
1716 mutex_unlock(&data
->mutex_flash
);
1717 return ret
> 0 ? ret
: err
;
1722 * - concurrent writing is prevented by mutex and all writes must be
1723 * n*64 bytes and 64-byte aligned, each write being preceeded by an
1724 * ERASE which erases a 64byte block.
1725 * If less than requested was written or an error is returned for an
1726 * otherwise correct write request the next 64-byte block which should
1727 * have been written is in undefined state (mostly: original, erased,
1728 * (half-)written with write error)
1729 * - reading can happend without special restriction
1731 static const struct file_operations picolcd_debug_flash_fops
= {
1732 .owner
= THIS_MODULE
,
1733 .open
= picolcd_debug_flash_open
,
1734 .read
= picolcd_debug_flash_read
,
1735 .write
= picolcd_debug_flash_write
,
1736 .llseek
= generic_file_llseek
,
1741 * Helper code for HID report level dumping/debugging
1743 static const char *error_codes
[] = {
1744 "success", "parameter missing", "data_missing", "block readonly",
1745 "block not erasable", "block too big", "section overflow",
1746 "invalid command length", "invalid data length",
1749 static void dump_buff_as_hex(char *dst
, size_t dst_sz
, const u8
*data
,
1750 const size_t data_len
)
1753 for (i
= j
= 0; i
< data_len
&& j
+ 3 < dst_sz
; i
++) {
1754 dst
[j
++] = hex_asc
[(data
[i
] >> 4) & 0x0f];
1755 dst
[j
++] = hex_asc
[data
[i
] & 0x0f];
1765 static void picolcd_debug_out_report(struct picolcd_data
*data
,
1766 struct hid_device
*hdev
, struct hid_report
*report
)
1769 int raw_size
= (report
->size
>> 3) + 1;
1773 /* Avoid unnecessary overhead if debugfs is disabled */
1774 if (!hdev
->debug_events
)
1777 buff
= kmalloc(BUFF_SZ
, GFP_ATOMIC
);
1781 snprintf(buff
, BUFF_SZ
, "\nout report %d (size %d) = ",
1782 report
->id
, raw_size
);
1783 hid_debug_event(hdev
, buff
);
1784 if (raw_size
+ 5 > sizeof(raw_data
)) {
1785 hid_debug_event(hdev
, " TOO BIG\n");
1788 raw_data
[0] = report
->id
;
1789 hid_output_report(report
, raw_data
);
1790 dump_buff_as_hex(buff
, BUFF_SZ
, raw_data
, raw_size
);
1791 hid_debug_event(hdev
, buff
);
1794 switch (report
->id
) {
1795 case REPORT_LED_STATE
:
1796 /* 1 data byte with GPO state */
1797 snprintf(buff
, BUFF_SZ
, "out report %s (%d, size=%d)\n",
1798 "REPORT_LED_STATE", report
->id
, raw_size
-1);
1799 hid_debug_event(hdev
, buff
);
1800 snprintf(buff
, BUFF_SZ
, "\tGPO state: 0x%02x\n", raw_data
[1]);
1801 hid_debug_event(hdev
, buff
);
1803 case REPORT_BRIGHTNESS
:
1804 /* 1 data byte with brightness */
1805 snprintf(buff
, BUFF_SZ
, "out report %s (%d, size=%d)\n",
1806 "REPORT_BRIGHTNESS", report
->id
, raw_size
-1);
1807 hid_debug_event(hdev
, buff
);
1808 snprintf(buff
, BUFF_SZ
, "\tBrightness: 0x%02x\n", raw_data
[1]);
1809 hid_debug_event(hdev
, buff
);
1811 case REPORT_CONTRAST
:
1812 /* 1 data byte with contrast */
1813 snprintf(buff
, BUFF_SZ
, "out report %s (%d, size=%d)\n",
1814 "REPORT_CONTRAST", report
->id
, raw_size
-1);
1815 hid_debug_event(hdev
, buff
);
1816 snprintf(buff
, BUFF_SZ
, "\tContrast: 0x%02x\n", raw_data
[1]);
1817 hid_debug_event(hdev
, buff
);
1820 /* 2 data bytes with reset duration in ms */
1821 snprintf(buff
, BUFF_SZ
, "out report %s (%d, size=%d)\n",
1822 "REPORT_RESET", report
->id
, raw_size
-1);
1823 hid_debug_event(hdev
, buff
);
1824 snprintf(buff
, BUFF_SZ
, "\tDuration: 0x%02x%02x (%dms)\n",
1825 raw_data
[2], raw_data
[1], raw_data
[2] << 8 | raw_data
[1]);
1826 hid_debug_event(hdev
, buff
);
1828 case REPORT_LCD_CMD
:
1829 /* 63 data bytes with LCD commands */
1830 snprintf(buff
, BUFF_SZ
, "out report %s (%d, size=%d)\n",
1831 "REPORT_LCD_CMD", report
->id
, raw_size
-1);
1832 hid_debug_event(hdev
, buff
);
1833 /* TODO: format decoding */
1835 case REPORT_LCD_DATA
:
1836 /* 63 data bytes with LCD data */
1837 snprintf(buff
, BUFF_SZ
, "out report %s (%d, size=%d)\n",
1838 "REPORT_LCD_CMD", report
->id
, raw_size
-1);
1839 /* TODO: format decoding */
1840 hid_debug_event(hdev
, buff
);
1842 case REPORT_LCD_CMD_DATA
:
1843 /* 63 data bytes with LCD commands and data */
1844 snprintf(buff
, BUFF_SZ
, "out report %s (%d, size=%d)\n",
1845 "REPORT_LCD_CMD", report
->id
, raw_size
-1);
1846 /* TODO: format decoding */
1847 hid_debug_event(hdev
, buff
);
1849 case REPORT_EE_READ
:
1850 /* 3 data bytes with read area description */
1851 snprintf(buff
, BUFF_SZ
, "out report %s (%d, size=%d)\n",
1852 "REPORT_EE_READ", report
->id
, raw_size
-1);
1853 hid_debug_event(hdev
, buff
);
1854 snprintf(buff
, BUFF_SZ
, "\tData address: 0x%02x%02x\n",
1855 raw_data
[2], raw_data
[1]);
1856 hid_debug_event(hdev
, buff
);
1857 snprintf(buff
, BUFF_SZ
, "\tData length: %d\n", raw_data
[3]);
1858 hid_debug_event(hdev
, buff
);
1860 case REPORT_EE_WRITE
:
1861 /* 3+1..20 data bytes with write area description */
1862 snprintf(buff
, BUFF_SZ
, "out report %s (%d, size=%d)\n",
1863 "REPORT_EE_WRITE", report
->id
, raw_size
-1);
1864 hid_debug_event(hdev
, buff
);
1865 snprintf(buff
, BUFF_SZ
, "\tData address: 0x%02x%02x\n",
1866 raw_data
[2], raw_data
[1]);
1867 hid_debug_event(hdev
, buff
);
1868 snprintf(buff
, BUFF_SZ
, "\tData length: %d\n", raw_data
[3]);
1869 hid_debug_event(hdev
, buff
);
1870 if (raw_data
[3] == 0) {
1871 snprintf(buff
, BUFF_SZ
, "\tNo data\n");
1872 } else if (raw_data
[3] + 4 <= raw_size
) {
1873 snprintf(buff
, BUFF_SZ
, "\tData: ");
1874 hid_debug_event(hdev
, buff
);
1875 dump_buff_as_hex(buff
, BUFF_SZ
, raw_data
+4, raw_data
[3]);
1877 snprintf(buff
, BUFF_SZ
, "\tData overflowed\n");
1879 hid_debug_event(hdev
, buff
);
1881 case REPORT_ERASE_MEMORY
:
1882 case REPORT_BL_ERASE_MEMORY
:
1883 /* 3 data bytes with pointer inside erase block */
1884 snprintf(buff
, BUFF_SZ
, "out report %s (%d, size=%d)\n",
1885 "REPORT_ERASE_MEMORY", report
->id
, raw_size
-1);
1886 hid_debug_event(hdev
, buff
);
1887 switch (data
->addr_sz
) {
1889 snprintf(buff
, BUFF_SZ
, "\tAddress inside 64 byte block: 0x%02x%02x\n",
1890 raw_data
[2], raw_data
[1]);
1893 snprintf(buff
, BUFF_SZ
, "\tAddress inside 64 byte block: 0x%02x%02x%02x\n",
1894 raw_data
[3], raw_data
[2], raw_data
[1]);
1897 snprintf(buff
, BUFF_SZ
, "\tNot supported\n");
1899 hid_debug_event(hdev
, buff
);
1901 case REPORT_READ_MEMORY
:
1902 case REPORT_BL_READ_MEMORY
:
1903 /* 4 data bytes with read area description */
1904 snprintf(buff
, BUFF_SZ
, "out report %s (%d, size=%d)\n",
1905 "REPORT_READ_MEMORY", report
->id
, raw_size
-1);
1906 hid_debug_event(hdev
, buff
);
1907 switch (data
->addr_sz
) {
1909 snprintf(buff
, BUFF_SZ
, "\tData address: 0x%02x%02x\n",
1910 raw_data
[2], raw_data
[1]);
1911 hid_debug_event(hdev
, buff
);
1912 snprintf(buff
, BUFF_SZ
, "\tData length: %d\n", raw_data
[3]);
1915 snprintf(buff
, BUFF_SZ
, "\tData address: 0x%02x%02x%02x\n",
1916 raw_data
[3], raw_data
[2], raw_data
[1]);
1917 hid_debug_event(hdev
, buff
);
1918 snprintf(buff
, BUFF_SZ
, "\tData length: %d\n", raw_data
[4]);
1921 snprintf(buff
, BUFF_SZ
, "\tNot supported\n");
1923 hid_debug_event(hdev
, buff
);
1925 case REPORT_WRITE_MEMORY
:
1926 case REPORT_BL_WRITE_MEMORY
:
1927 /* 4+1..32 data bytes with write adrea description */
1928 snprintf(buff
, BUFF_SZ
, "out report %s (%d, size=%d)\n",
1929 "REPORT_WRITE_MEMORY", report
->id
, raw_size
-1);
1930 hid_debug_event(hdev
, buff
);
1931 switch (data
->addr_sz
) {
1933 snprintf(buff
, BUFF_SZ
, "\tData address: 0x%02x%02x\n",
1934 raw_data
[2], raw_data
[1]);
1935 hid_debug_event(hdev
, buff
);
1936 snprintf(buff
, BUFF_SZ
, "\tData length: %d\n", raw_data
[3]);
1937 hid_debug_event(hdev
, buff
);
1938 if (raw_data
[3] == 0) {
1939 snprintf(buff
, BUFF_SZ
, "\tNo data\n");
1940 } else if (raw_data
[3] + 4 <= raw_size
) {
1941 snprintf(buff
, BUFF_SZ
, "\tData: ");
1942 hid_debug_event(hdev
, buff
);
1943 dump_buff_as_hex(buff
, BUFF_SZ
, raw_data
+4, raw_data
[3]);
1945 snprintf(buff
, BUFF_SZ
, "\tData overflowed\n");
1949 snprintf(buff
, BUFF_SZ
, "\tData address: 0x%02x%02x%02x\n",
1950 raw_data
[3], raw_data
[2], raw_data
[1]);
1951 hid_debug_event(hdev
, buff
);
1952 snprintf(buff
, BUFF_SZ
, "\tData length: %d\n", raw_data
[4]);
1953 hid_debug_event(hdev
, buff
);
1954 if (raw_data
[4] == 0) {
1955 snprintf(buff
, BUFF_SZ
, "\tNo data\n");
1956 } else if (raw_data
[4] + 5 <= raw_size
) {
1957 snprintf(buff
, BUFF_SZ
, "\tData: ");
1958 hid_debug_event(hdev
, buff
);
1959 dump_buff_as_hex(buff
, BUFF_SZ
, raw_data
+5, raw_data
[4]);
1961 snprintf(buff
, BUFF_SZ
, "\tData overflowed\n");
1965 snprintf(buff
, BUFF_SZ
, "\tNot supported\n");
1967 hid_debug_event(hdev
, buff
);
1969 case REPORT_SPLASH_RESTART
:
1972 case REPORT_EXIT_KEYBOARD
:
1973 snprintf(buff
, BUFF_SZ
, "out report %s (%d, size=%d)\n",
1974 "REPORT_EXIT_KEYBOARD", report
->id
, raw_size
-1);
1975 hid_debug_event(hdev
, buff
);
1976 snprintf(buff
, BUFF_SZ
, "\tRestart delay: %dms (0x%02x%02x)\n",
1977 raw_data
[1] | (raw_data
[2] << 8),
1978 raw_data
[2], raw_data
[1]);
1979 hid_debug_event(hdev
, buff
);
1981 case REPORT_VERSION
:
1982 snprintf(buff
, BUFF_SZ
, "out report %s (%d, size=%d)\n",
1983 "REPORT_VERSION", report
->id
, raw_size
-1);
1984 hid_debug_event(hdev
, buff
);
1987 snprintf(buff
, BUFF_SZ
, "out report %s (%d, size=%d)\n",
1988 "REPORT_DEVID", report
->id
, raw_size
-1);
1989 hid_debug_event(hdev
, buff
);
1991 case REPORT_SPLASH_SIZE
:
1992 snprintf(buff
, BUFF_SZ
, "out report %s (%d, size=%d)\n",
1993 "REPORT_SPLASH_SIZE", report
->id
, raw_size
-1);
1994 hid_debug_event(hdev
, buff
);
1996 case REPORT_HOOK_VERSION
:
1997 snprintf(buff
, BUFF_SZ
, "out report %s (%d, size=%d)\n",
1998 "REPORT_HOOK_VERSION", report
->id
, raw_size
-1);
1999 hid_debug_event(hdev
, buff
);
2001 case REPORT_EXIT_FLASHER
:
2002 snprintf(buff
, BUFF_SZ
, "out report %s (%d, size=%d)\n",
2003 "REPORT_VERSION", report
->id
, raw_size
-1);
2004 hid_debug_event(hdev
, buff
);
2005 snprintf(buff
, BUFF_SZ
, "\tRestart delay: %dms (0x%02x%02x)\n",
2006 raw_data
[1] | (raw_data
[2] << 8),
2007 raw_data
[2], raw_data
[1]);
2008 hid_debug_event(hdev
, buff
);
2011 snprintf(buff
, BUFF_SZ
, "out report %s (%d, size=%d)\n",
2012 "<unknown>", report
->id
, raw_size
-1);
2013 hid_debug_event(hdev
, buff
);
2016 wake_up_interruptible(&hdev
->debug_wait
);
2020 static void picolcd_debug_raw_event(struct picolcd_data
*data
,
2021 struct hid_device
*hdev
, struct hid_report
*report
,
2022 u8
*raw_data
, int size
)
2027 /* Avoid unnecessary overhead if debugfs is disabled */
2028 if (!hdev
->debug_events
)
2031 buff
= kmalloc(BUFF_SZ
, GFP_ATOMIC
);
2035 switch (report
->id
) {
2036 case REPORT_ERROR_CODE
:
2037 /* 2 data bytes with affected report and error code */
2038 snprintf(buff
, BUFF_SZ
, "report %s (%d, size=%d)\n",
2039 "REPORT_ERROR_CODE", report
->id
, size
-1);
2040 hid_debug_event(hdev
, buff
);
2041 if (raw_data
[2] < ARRAY_SIZE(error_codes
))
2042 snprintf(buff
, BUFF_SZ
, "\tError code 0x%02x (%s) in reply to report 0x%02x\n",
2043 raw_data
[2], error_codes
[raw_data
[2]], raw_data
[1]);
2045 snprintf(buff
, BUFF_SZ
, "\tError code 0x%02x in reply to report 0x%02x\n",
2046 raw_data
[2], raw_data
[1]);
2047 hid_debug_event(hdev
, buff
);
2049 case REPORT_KEY_STATE
:
2050 /* 2 data bytes with key state */
2051 snprintf(buff
, BUFF_SZ
, "report %s (%d, size=%d)\n",
2052 "REPORT_KEY_STATE", report
->id
, size
-1);
2053 hid_debug_event(hdev
, buff
);
2054 if (raw_data
[1] == 0)
2055 snprintf(buff
, BUFF_SZ
, "\tNo key pressed\n");
2056 else if (raw_data
[2] == 0)
2057 snprintf(buff
, BUFF_SZ
, "\tOne key pressed: 0x%02x (%d)\n",
2058 raw_data
[1], raw_data
[1]);
2060 snprintf(buff
, BUFF_SZ
, "\tTwo keys pressed: 0x%02x (%d), 0x%02x (%d)\n",
2061 raw_data
[1], raw_data
[1], raw_data
[2], raw_data
[2]);
2062 hid_debug_event(hdev
, buff
);
2064 case REPORT_IR_DATA
:
2065 /* Up to 20 byes of IR scancode data */
2066 snprintf(buff
, BUFF_SZ
, "report %s (%d, size=%d)\n",
2067 "REPORT_IR_DATA", report
->id
, size
-1);
2068 hid_debug_event(hdev
, buff
);
2069 if (raw_data
[1] == 0) {
2070 snprintf(buff
, BUFF_SZ
, "\tUnexpectedly 0 data length\n");
2071 hid_debug_event(hdev
, buff
);
2072 } else if (raw_data
[1] + 1 <= size
) {
2073 snprintf(buff
, BUFF_SZ
, "\tData length: %d\n\tIR Data: ",
2075 hid_debug_event(hdev
, buff
);
2076 dump_buff_as_hex(buff
, BUFF_SZ
, raw_data
+2, raw_data
[1]-1);
2077 hid_debug_event(hdev
, buff
);
2079 snprintf(buff
, BUFF_SZ
, "\tOverflowing data length: %d\n",
2081 hid_debug_event(hdev
, buff
);
2084 case REPORT_EE_DATA
:
2085 /* Data buffer in response to REPORT_EE_READ or REPORT_EE_WRITE */
2086 snprintf(buff
, BUFF_SZ
, "report %s (%d, size=%d)\n",
2087 "REPORT_EE_DATA", report
->id
, size
-1);
2088 hid_debug_event(hdev
, buff
);
2089 snprintf(buff
, BUFF_SZ
, "\tData address: 0x%02x%02x\n",
2090 raw_data
[2], raw_data
[1]);
2091 hid_debug_event(hdev
, buff
);
2092 snprintf(buff
, BUFF_SZ
, "\tData length: %d\n", raw_data
[3]);
2093 hid_debug_event(hdev
, buff
);
2094 if (raw_data
[3] == 0) {
2095 snprintf(buff
, BUFF_SZ
, "\tNo data\n");
2096 hid_debug_event(hdev
, buff
);
2097 } else if (raw_data
[3] + 4 <= size
) {
2098 snprintf(buff
, BUFF_SZ
, "\tData: ");
2099 hid_debug_event(hdev
, buff
);
2100 dump_buff_as_hex(buff
, BUFF_SZ
, raw_data
+4, raw_data
[3]);
2101 hid_debug_event(hdev
, buff
);
2103 snprintf(buff
, BUFF_SZ
, "\tData overflowed\n");
2104 hid_debug_event(hdev
, buff
);
2108 /* Data buffer in response to REPORT_READ_MEMORY or REPORT_WRTIE_MEMORY */
2109 snprintf(buff
, BUFF_SZ
, "report %s (%d, size=%d)\n",
2110 "REPORT_MEMORY", report
->id
, size
-1);
2111 hid_debug_event(hdev
, buff
);
2112 switch (data
->addr_sz
) {
2114 snprintf(buff
, BUFF_SZ
, "\tData address: 0x%02x%02x\n",
2115 raw_data
[2], raw_data
[1]);
2116 hid_debug_event(hdev
, buff
);
2117 snprintf(buff
, BUFF_SZ
, "\tData length: %d\n", raw_data
[3]);
2118 hid_debug_event(hdev
, buff
);
2119 if (raw_data
[3] == 0) {
2120 snprintf(buff
, BUFF_SZ
, "\tNo data\n");
2121 } else if (raw_data
[3] + 4 <= size
) {
2122 snprintf(buff
, BUFF_SZ
, "\tData: ");
2123 hid_debug_event(hdev
, buff
);
2124 dump_buff_as_hex(buff
, BUFF_SZ
, raw_data
+4, raw_data
[3]);
2126 snprintf(buff
, BUFF_SZ
, "\tData overflowed\n");
2130 snprintf(buff
, BUFF_SZ
, "\tData address: 0x%02x%02x%02x\n",
2131 raw_data
[3], raw_data
[2], raw_data
[1]);
2132 hid_debug_event(hdev
, buff
);
2133 snprintf(buff
, BUFF_SZ
, "\tData length: %d\n", raw_data
[4]);
2134 hid_debug_event(hdev
, buff
);
2135 if (raw_data
[4] == 0) {
2136 snprintf(buff
, BUFF_SZ
, "\tNo data\n");
2137 } else if (raw_data
[4] + 5 <= size
) {
2138 snprintf(buff
, BUFF_SZ
, "\tData: ");
2139 hid_debug_event(hdev
, buff
);
2140 dump_buff_as_hex(buff
, BUFF_SZ
, raw_data
+5, raw_data
[4]);
2142 snprintf(buff
, BUFF_SZ
, "\tData overflowed\n");
2146 snprintf(buff
, BUFF_SZ
, "\tNot supported\n");
2148 hid_debug_event(hdev
, buff
);
2150 case REPORT_VERSION
:
2151 snprintf(buff
, BUFF_SZ
, "report %s (%d, size=%d)\n",
2152 "REPORT_VERSION", report
->id
, size
-1);
2153 hid_debug_event(hdev
, buff
);
2154 snprintf(buff
, BUFF_SZ
, "\tFirmware version: %d.%d\n",
2155 raw_data
[2], raw_data
[1]);
2156 hid_debug_event(hdev
, buff
);
2158 case REPORT_BL_ERASE_MEMORY
:
2159 snprintf(buff
, BUFF_SZ
, "report %s (%d, size=%d)\n",
2160 "REPORT_BL_ERASE_MEMORY", report
->id
, size
-1);
2161 hid_debug_event(hdev
, buff
);
2164 case REPORT_BL_READ_MEMORY
:
2165 snprintf(buff
, BUFF_SZ
, "report %s (%d, size=%d)\n",
2166 "REPORT_BL_READ_MEMORY", report
->id
, size
-1);
2167 hid_debug_event(hdev
, buff
);
2170 case REPORT_BL_WRITE_MEMORY
:
2171 snprintf(buff
, BUFF_SZ
, "report %s (%d, size=%d)\n",
2172 "REPORT_BL_WRITE_MEMORY", report
->id
, size
-1);
2173 hid_debug_event(hdev
, buff
);
2177 snprintf(buff
, BUFF_SZ
, "report %s (%d, size=%d)\n",
2178 "REPORT_DEVID", report
->id
, size
-1);
2179 hid_debug_event(hdev
, buff
);
2180 snprintf(buff
, BUFF_SZ
, "\tSerial: 0x%02x%02x%02x%02x\n",
2181 raw_data
[1], raw_data
[2], raw_data
[3], raw_data
[4]);
2182 hid_debug_event(hdev
, buff
);
2183 snprintf(buff
, BUFF_SZ
, "\tType: 0x%02x\n",
2185 hid_debug_event(hdev
, buff
);
2187 case REPORT_SPLASH_SIZE
:
2188 snprintf(buff
, BUFF_SZ
, "report %s (%d, size=%d)\n",
2189 "REPORT_SPLASH_SIZE", report
->id
, size
-1);
2190 hid_debug_event(hdev
, buff
);
2191 snprintf(buff
, BUFF_SZ
, "\tTotal splash space: %d\n",
2192 (raw_data
[2] << 8) | raw_data
[1]);
2193 hid_debug_event(hdev
, buff
);
2194 snprintf(buff
, BUFF_SZ
, "\tUsed splash space: %d\n",
2195 (raw_data
[4] << 8) | raw_data
[3]);
2196 hid_debug_event(hdev
, buff
);
2198 case REPORT_HOOK_VERSION
:
2199 snprintf(buff
, BUFF_SZ
, "report %s (%d, size=%d)\n",
2200 "REPORT_HOOK_VERSION", report
->id
, size
-1);
2201 hid_debug_event(hdev
, buff
);
2202 snprintf(buff
, BUFF_SZ
, "\tFirmware version: %d.%d\n",
2203 raw_data
[1], raw_data
[2]);
2204 hid_debug_event(hdev
, buff
);
2207 snprintf(buff
, BUFF_SZ
, "report %s (%d, size=%d)\n",
2208 "<unknown>", report
->id
, size
-1);
2209 hid_debug_event(hdev
, buff
);
2212 wake_up_interruptible(&hdev
->debug_wait
);
2216 static void picolcd_init_devfs(struct picolcd_data
*data
,
2217 struct hid_report
*eeprom_r
, struct hid_report
*eeprom_w
,
2218 struct hid_report
*flash_r
, struct hid_report
*flash_w
,
2219 struct hid_report
*reset
)
2221 struct hid_device
*hdev
= data
->hdev
;
2223 mutex_init(&data
->mutex_flash
);
2227 data
->debug_reset
= debugfs_create_file("reset", 0600,
2228 hdev
->debug_dir
, data
, &picolcd_debug_reset_fops
);
2231 if (eeprom_r
|| eeprom_w
)
2232 data
->debug_eeprom
= debugfs_create_file("eeprom",
2233 (eeprom_w
? S_IWUSR
: 0) | (eeprom_r
? S_IRUSR
: 0),
2234 hdev
->debug_dir
, data
, &picolcd_debug_eeprom_fops
);
2237 if (flash_r
&& flash_r
->maxfield
== 1 && flash_r
->field
[0]->report_size
== 8)
2238 data
->addr_sz
= flash_r
->field
[0]->report_count
- 1;
2241 if (data
->addr_sz
== 2 || data
->addr_sz
== 3) {
2242 data
->debug_flash
= debugfs_create_file("flash",
2243 (flash_w
? S_IWUSR
: 0) | (flash_r
? S_IRUSR
: 0),
2244 hdev
->debug_dir
, data
, &picolcd_debug_flash_fops
);
2245 } else if (flash_r
|| flash_w
)
2246 dev_warn(&hdev
->dev
, "Unexpected FLASH access reports, "
2247 "please submit rdesc for review\n");
2250 static void picolcd_exit_devfs(struct picolcd_data
*data
)
2252 struct dentry
*dent
;
2254 dent
= data
->debug_reset
;
2255 data
->debug_reset
= NULL
;
2257 debugfs_remove(dent
);
2258 dent
= data
->debug_eeprom
;
2259 data
->debug_eeprom
= NULL
;
2261 debugfs_remove(dent
);
2262 dent
= data
->debug_flash
;
2263 data
->debug_flash
= NULL
;
2265 debugfs_remove(dent
);
2266 mutex_destroy(&data
->mutex_flash
);
2269 static inline void picolcd_debug_raw_event(struct picolcd_data
*data
,
2270 struct hid_device
*hdev
, struct hid_report
*report
,
2271 u8
*raw_data
, int size
)
2274 static inline void picolcd_init_devfs(struct picolcd_data
*data
,
2275 struct hid_report
*eeprom_r
, struct hid_report
*eeprom_w
,
2276 struct hid_report
*flash_r
, struct hid_report
*flash_w
,
2277 struct hid_report
*reset
)
2280 static inline void picolcd_exit_devfs(struct picolcd_data
*data
)
2283 #endif /* CONFIG_DEBUG_FS */
2286 * Handle raw report as sent by device
2288 static int picolcd_raw_event(struct hid_device
*hdev
,
2289 struct hid_report
*report
, u8
*raw_data
, int size
)
2291 struct picolcd_data
*data
= hid_get_drvdata(hdev
);
2292 unsigned long flags
;
2298 if (report
->id
== REPORT_KEY_STATE
) {
2299 if (data
->input_keys
)
2300 ret
= picolcd_raw_keypad(data
, report
, raw_data
+1, size
-1);
2301 } else if (report
->id
== REPORT_IR_DATA
) {
2302 if (data
->input_cir
)
2303 ret
= picolcd_raw_cir(data
, report
, raw_data
+1, size
-1);
2305 spin_lock_irqsave(&data
->lock
, flags
);
2307 * We let the caller of picolcd_send_and_wait() check if the
2308 * report we got is one of the expected ones or not.
2310 if (data
->pending
) {
2311 memcpy(data
->pending
->raw_data
, raw_data
+1, size
-1);
2312 data
->pending
->raw_size
= size
-1;
2313 data
->pending
->in_report
= report
;
2314 complete(&data
->pending
->ready
);
2316 spin_unlock_irqrestore(&data
->lock
, flags
);
2319 picolcd_debug_raw_event(data
, hdev
, report
, raw_data
, size
);
2324 static int picolcd_suspend(struct hid_device
*hdev
, pm_message_t message
)
2326 if (message
.event
& PM_EVENT_AUTO
)
2329 picolcd_suspend_backlight(hid_get_drvdata(hdev
));
2330 dbg_hid(PICOLCD_NAME
" device ready for suspend\n");
2334 static int picolcd_resume(struct hid_device
*hdev
)
2337 ret
= picolcd_resume_backlight(hid_get_drvdata(hdev
));
2339 dbg_hid(PICOLCD_NAME
" restoring backlight failed: %d\n", ret
);
2343 static int picolcd_reset_resume(struct hid_device
*hdev
)
2346 ret
= picolcd_reset(hdev
);
2348 dbg_hid(PICOLCD_NAME
" resetting our device failed: %d\n", ret
);
2349 ret
= picolcd_fb_reset(hid_get_drvdata(hdev
), 0);
2351 dbg_hid(PICOLCD_NAME
" restoring framebuffer content failed: %d\n", ret
);
2352 ret
= picolcd_resume_lcd(hid_get_drvdata(hdev
));
2354 dbg_hid(PICOLCD_NAME
" restoring lcd failed: %d\n", ret
);
2355 ret
= picolcd_resume_backlight(hid_get_drvdata(hdev
));
2357 dbg_hid(PICOLCD_NAME
" restoring backlight failed: %d\n", ret
);
2358 picolcd_leds_set(hid_get_drvdata(hdev
));
2363 /* initialize keypad input device */
2364 static int picolcd_init_keys(struct picolcd_data
*data
,
2365 struct hid_report
*report
)
2367 struct hid_device
*hdev
= data
->hdev
;
2368 struct input_dev
*idev
;
2373 if (report
->maxfield
!= 1 || report
->field
[0]->report_count
!= 2 ||
2374 report
->field
[0]->report_size
!= 8) {
2375 dev_err(&hdev
->dev
, "unsupported KEY_STATE report");
2379 idev
= input_allocate_device();
2381 dev_err(&hdev
->dev
, "failed to allocate input device");
2384 input_set_drvdata(idev
, hdev
);
2385 memcpy(data
->keycode
, def_keymap
, sizeof(def_keymap
));
2386 idev
->name
= hdev
->name
;
2387 idev
->phys
= hdev
->phys
;
2388 idev
->uniq
= hdev
->uniq
;
2389 idev
->id
.bustype
= hdev
->bus
;
2390 idev
->id
.vendor
= hdev
->vendor
;
2391 idev
->id
.product
= hdev
->product
;
2392 idev
->id
.version
= hdev
->version
;
2393 idev
->dev
.parent
= hdev
->dev
.parent
;
2394 idev
->keycode
= &data
->keycode
;
2395 idev
->keycodemax
= PICOLCD_KEYS
;
2396 idev
->keycodesize
= sizeof(data
->keycode
[0]);
2397 input_set_capability(idev
, EV_MSC
, MSC_SCAN
);
2398 set_bit(EV_REP
, idev
->evbit
);
2399 for (i
= 0; i
< PICOLCD_KEYS
; i
++)
2400 input_set_capability(idev
, EV_KEY
, data
->keycode
[i
]);
2401 error
= input_register_device(idev
);
2403 dev_err(&hdev
->dev
, "error registering the input device");
2404 input_free_device(idev
);
2407 data
->input_keys
= idev
;
2411 static void picolcd_exit_keys(struct picolcd_data
*data
)
2413 struct input_dev
*idev
= data
->input_keys
;
2415 data
->input_keys
= NULL
;
2417 input_unregister_device(idev
);
2420 /* initialize CIR input device */
2421 static inline int picolcd_init_cir(struct picolcd_data
*data
, struct hid_report
*report
)
2423 /* support not implemented yet */
2427 static inline void picolcd_exit_cir(struct picolcd_data
*data
)
2431 static int picolcd_probe_lcd(struct hid_device
*hdev
, struct picolcd_data
*data
)
2435 error
= picolcd_check_version(hdev
);
2439 if (data
->version
[0] != 0 && data
->version
[1] != 3)
2440 dev_info(&hdev
->dev
, "Device with untested firmware revision, "
2441 "please submit /sys/kernel/debug/hid/%s/rdesc for this device.\n",
2442 dev_name(&hdev
->dev
));
2444 /* Setup keypad input device */
2445 error
= picolcd_init_keys(data
, picolcd_in_report(REPORT_KEY_STATE
, hdev
));
2449 /* Setup CIR input device */
2450 error
= picolcd_init_cir(data
, picolcd_in_report(REPORT_IR_DATA
, hdev
));
2454 /* Set up the framebuffer device */
2455 error
= picolcd_init_framebuffer(data
);
2459 /* Setup lcd class device */
2460 error
= picolcd_init_lcd(data
, picolcd_out_report(REPORT_CONTRAST
, hdev
));
2464 /* Setup backlight class device */
2465 error
= picolcd_init_backlight(data
, picolcd_out_report(REPORT_BRIGHTNESS
, hdev
));
2469 /* Setup the LED class devices */
2470 error
= picolcd_init_leds(data
, picolcd_out_report(REPORT_LED_STATE
, hdev
));
2474 picolcd_init_devfs(data
, picolcd_out_report(REPORT_EE_READ
, hdev
),
2475 picolcd_out_report(REPORT_EE_WRITE
, hdev
),
2476 picolcd_out_report(REPORT_READ_MEMORY
, hdev
),
2477 picolcd_out_report(REPORT_WRITE_MEMORY
, hdev
),
2478 picolcd_out_report(REPORT_RESET
, hdev
));
2481 picolcd_exit_leds(data
);
2482 picolcd_exit_backlight(data
);
2483 picolcd_exit_lcd(data
);
2484 picolcd_exit_framebuffer(data
);
2485 picolcd_exit_cir(data
);
2486 picolcd_exit_keys(data
);
2490 static int picolcd_probe_bootloader(struct hid_device
*hdev
, struct picolcd_data
*data
)
2494 error
= picolcd_check_version(hdev
);
2498 if (data
->version
[0] != 1 && data
->version
[1] != 0)
2499 dev_info(&hdev
->dev
, "Device with untested bootloader revision, "
2500 "please submit /sys/kernel/debug/hid/%s/rdesc for this device.\n",
2501 dev_name(&hdev
->dev
));
2503 picolcd_init_devfs(data
, NULL
, NULL
,
2504 picolcd_out_report(REPORT_BL_READ_MEMORY
, hdev
),
2505 picolcd_out_report(REPORT_BL_WRITE_MEMORY
, hdev
), NULL
);
2509 static int picolcd_probe(struct hid_device
*hdev
,
2510 const struct hid_device_id
*id
)
2512 struct picolcd_data
*data
;
2513 int error
= -ENOMEM
;
2515 dbg_hid(PICOLCD_NAME
" hardware probe...\n");
2518 * Let's allocate the picolcd data structure, set some reasonable
2519 * defaults, and associate it with the device
2521 data
= kzalloc(sizeof(struct picolcd_data
), GFP_KERNEL
);
2523 dev_err(&hdev
->dev
, "can't allocate space for Minibox PicoLCD device data\n");
2525 goto err_no_cleanup
;
2528 spin_lock_init(&data
->lock
);
2529 mutex_init(&data
->mutex
);
2531 data
->opmode_delay
= 5000;
2532 if (hdev
->product
== USB_DEVICE_ID_PICOLCD_BOOTLOADER
)
2533 data
->status
|= PICOLCD_BOOTLOADER
;
2534 hid_set_drvdata(hdev
, data
);
2536 /* Parse the device reports and start it up */
2537 error
= hid_parse(hdev
);
2539 dev_err(&hdev
->dev
, "device report parse failed\n");
2540 goto err_cleanup_data
;
2543 /* We don't use hidinput but hid_hw_start() fails if nothing is
2544 * claimed. So spoof claimed input. */
2545 hdev
->claimed
= HID_CLAIMED_INPUT
;
2546 error
= hid_hw_start(hdev
, 0);
2549 dev_err(&hdev
->dev
, "hardware start failed\n");
2550 goto err_cleanup_data
;
2553 error
= hdev
->ll_driver
->open(hdev
);
2555 dev_err(&hdev
->dev
, "failed to open input interrupt pipe for key and IR events\n");
2556 goto err_cleanup_hid_hw
;
2559 error
= device_create_file(&hdev
->dev
, &dev_attr_operation_mode_delay
);
2561 dev_err(&hdev
->dev
, "failed to create sysfs attributes\n");
2562 goto err_cleanup_hid_ll
;
2565 error
= device_create_file(&hdev
->dev
, &dev_attr_operation_mode
);
2567 dev_err(&hdev
->dev
, "failed to create sysfs attributes\n");
2568 goto err_cleanup_sysfs1
;
2571 if (data
->status
& PICOLCD_BOOTLOADER
)
2572 error
= picolcd_probe_bootloader(hdev
, data
);
2574 error
= picolcd_probe_lcd(hdev
, data
);
2576 goto err_cleanup_sysfs2
;
2578 dbg_hid(PICOLCD_NAME
" activated and initialized\n");
2582 device_remove_file(&hdev
->dev
, &dev_attr_operation_mode
);
2584 device_remove_file(&hdev
->dev
, &dev_attr_operation_mode_delay
);
2586 hdev
->ll_driver
->close(hdev
);
2592 hid_set_drvdata(hdev
, NULL
);
2597 static void picolcd_remove(struct hid_device
*hdev
)
2599 struct picolcd_data
*data
= hid_get_drvdata(hdev
);
2600 unsigned long flags
;
2602 dbg_hid(PICOLCD_NAME
" hardware remove...\n");
2603 spin_lock_irqsave(&data
->lock
, flags
);
2604 data
->status
|= PICOLCD_FAILED
;
2605 spin_unlock_irqrestore(&data
->lock
, flags
);
2607 picolcd_exit_devfs(data
);
2608 device_remove_file(&hdev
->dev
, &dev_attr_operation_mode
);
2609 device_remove_file(&hdev
->dev
, &dev_attr_operation_mode_delay
);
2610 hdev
->ll_driver
->close(hdev
);
2612 hid_set_drvdata(hdev
, NULL
);
2614 /* Shortcut potential pending reply that will never arrive */
2615 spin_lock_irqsave(&data
->lock
, flags
);
2617 complete(&data
->pending
->ready
);
2618 spin_unlock_irqrestore(&data
->lock
, flags
);
2621 picolcd_exit_leds(data
);
2622 /* Clean up the framebuffer */
2623 picolcd_exit_backlight(data
);
2624 picolcd_exit_lcd(data
);
2625 picolcd_exit_framebuffer(data
);
2627 picolcd_exit_cir(data
);
2628 picolcd_exit_keys(data
);
2630 mutex_destroy(&data
->mutex
);
2631 /* Finally, clean up the picolcd data itself */
2635 static const struct hid_device_id picolcd_devices
[] = {
2636 { HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP
, USB_DEVICE_ID_PICOLCD
) },
2637 { HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP
, USB_DEVICE_ID_PICOLCD_BOOTLOADER
) },
2640 MODULE_DEVICE_TABLE(hid
, picolcd_devices
);
2642 static struct hid_driver picolcd_driver
= {
2643 .name
= "hid-picolcd",
2644 .id_table
= picolcd_devices
,
2645 .probe
= picolcd_probe
,
2646 .remove
= picolcd_remove
,
2647 .raw_event
= picolcd_raw_event
,
2649 .suspend
= picolcd_suspend
,
2650 .resume
= picolcd_resume
,
2651 .reset_resume
= picolcd_reset_resume
,
2655 static int __init
picolcd_init(void)
2657 return hid_register_driver(&picolcd_driver
);
2660 static void __exit
picolcd_exit(void)
2662 hid_unregister_driver(&picolcd_driver
);
2665 module_init(picolcd_init
);
2666 module_exit(picolcd_exit
);
2667 MODULE_DESCRIPTION("Minibox graphics PicoLCD Driver");
2668 MODULE_LICENSE("GPL v2");