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
,
131 #endif /* CONFIG_HID_PICOLCD_FB */
135 * The PicoLCD has an IR receiver header, a built-in keypad with 5 keys
136 * and header for 4x4 key matrix. The built-in keys are part of the matrix.
138 static const unsigned short def_keymap
[] = {
139 KEY_RESERVED
, /* none */
140 KEY_BACK
, /* col 4 + row 1 */
141 KEY_HOMEPAGE
, /* col 3 + row 1 */
142 KEY_RESERVED
, /* col 2 + row 1 */
143 KEY_RESERVED
, /* col 1 + row 1 */
144 KEY_SCROLLUP
, /* col 4 + row 2 */
145 KEY_OK
, /* col 3 + row 2 */
146 KEY_SCROLLDOWN
, /* col 2 + row 2 */
147 KEY_RESERVED
, /* col 1 + row 2 */
148 KEY_RESERVED
, /* col 4 + row 3 */
149 KEY_RESERVED
, /* col 3 + row 3 */
150 KEY_RESERVED
, /* col 2 + row 3 */
151 KEY_RESERVED
, /* col 1 + row 3 */
152 KEY_RESERVED
, /* col 4 + row 4 */
153 KEY_RESERVED
, /* col 3 + row 4 */
154 KEY_RESERVED
, /* col 2 + row 4 */
155 KEY_RESERVED
, /* col 1 + row 4 */
157 #define PICOLCD_KEYS ARRAY_SIZE(def_keymap)
159 /* Description of in-progress IO operation, used for operations
160 * that trigger response from device */
161 struct picolcd_pending
{
162 struct hid_report
*out_report
;
163 struct hid_report
*in_report
;
164 struct completion ready
;
169 /* Per device data structure */
170 struct picolcd_data
{
171 struct hid_device
*hdev
;
172 #ifdef CONFIG_DEBUG_FS
173 struct dentry
*debug_reset
;
174 struct dentry
*debug_eeprom
;
175 struct dentry
*debug_flash
;
176 struct mutex mutex_flash
;
180 unsigned short opmode_delay
;
183 struct input_dev
*input_keys
;
184 struct input_dev
*input_cir
;
185 unsigned short keycode
[PICOLCD_KEYS
];
187 #ifdef CONFIG_HID_PICOLCD_FB
188 /* Framebuffer stuff */
191 u8
*fb_vbitmap
; /* local copy of what was sent to PicoLCD */
192 u8
*fb_bitmap
; /* framebuffer */
193 struct fb_info
*fb_info
;
194 struct fb_deferred_io fb_defio
;
195 #endif /* CONFIG_HID_PICOLCD_FB */
196 #ifdef CONFIG_HID_PICOLCD_LCD
197 struct lcd_device
*lcd
;
199 #endif /* CONFIG_HID_PICOLCD_LCD */
200 #ifdef CONFIG_HID_PICOLCD_BACKLIGHT
201 struct backlight_device
*backlight
;
204 #endif /* CONFIG_HID_PICOLCD_BACKLIGHT */
205 #ifdef CONFIG_HID_PICOLCD_LEDS
208 struct led_classdev
*led
[8];
209 #endif /* CONFIG_HID_PICOLCD_LEDS */
211 /* Housekeeping stuff */
214 struct picolcd_pending
*pending
;
216 #define PICOLCD_BOOTLOADER 1
217 #define PICOLCD_FAILED 2
218 #define PICOLCD_READY_FB 4
222 /* Find a given report */
223 #define picolcd_in_report(id, dev) picolcd_report(id, dev, HID_INPUT_REPORT)
224 #define picolcd_out_report(id, dev) picolcd_report(id, dev, HID_OUTPUT_REPORT)
226 static struct hid_report
*picolcd_report(int id
, struct hid_device
*hdev
, int dir
)
228 struct list_head
*feature_report_list
= &hdev
->report_enum
[dir
].report_list
;
229 struct hid_report
*report
= NULL
;
231 list_for_each_entry(report
, feature_report_list
, list
) {
232 if (report
->id
== id
)
235 dev_warn(&hdev
->dev
, "No report with id 0x%x found\n", id
);
239 #ifdef CONFIG_DEBUG_FS
240 static void picolcd_debug_out_report(struct picolcd_data
*data
,
241 struct hid_device
*hdev
, struct hid_report
*report
);
242 #define usbhid_submit_report(a, b, c) \
244 picolcd_debug_out_report(hid_get_drvdata(a), a, b); \
245 usbhid_submit_report(a, b, c); \
249 /* Submit a report and wait for a reply from device - if device fades away
250 * or does not respond in time, return NULL */
251 static struct picolcd_pending
*picolcd_send_and_wait(struct hid_device
*hdev
,
252 int report_id
, const u8
*raw_data
, int size
)
254 struct picolcd_data
*data
= hid_get_drvdata(hdev
);
255 struct picolcd_pending
*work
;
256 struct hid_report
*report
= picolcd_out_report(report_id
, hdev
);
260 if (!report
|| !data
)
262 if (data
->status
& PICOLCD_FAILED
)
264 work
= kzalloc(sizeof(*work
), GFP_KERNEL
);
268 init_completion(&work
->ready
);
269 work
->out_report
= report
;
270 work
->in_report
= NULL
;
273 mutex_lock(&data
->mutex
);
274 spin_lock_irqsave(&data
->lock
, flags
);
275 for (i
= k
= 0; i
< report
->maxfield
; i
++)
276 for (j
= 0; j
< report
->field
[i
]->report_count
; j
++) {
277 hid_set_field(report
->field
[i
], j
, k
< size
? raw_data
[k
] : 0);
280 data
->pending
= work
;
281 usbhid_submit_report(data
->hdev
, report
, USB_DIR_OUT
);
282 spin_unlock_irqrestore(&data
->lock
, flags
);
283 wait_for_completion_interruptible_timeout(&work
->ready
, HZ
*2);
284 spin_lock_irqsave(&data
->lock
, flags
);
285 data
->pending
= NULL
;
286 spin_unlock_irqrestore(&data
->lock
, flags
);
287 mutex_unlock(&data
->mutex
);
291 #ifdef CONFIG_HID_PICOLCD_FB
292 /* Send a given tile to PicoLCD */
293 static int picolcd_fb_send_tile(struct hid_device
*hdev
, int chip
, int tile
)
295 struct picolcd_data
*data
= hid_get_drvdata(hdev
);
296 struct hid_report
*report1
= picolcd_out_report(REPORT_LCD_CMD_DATA
, hdev
);
297 struct hid_report
*report2
= picolcd_out_report(REPORT_LCD_DATA
, hdev
);
302 if (!report1
|| report1
->maxfield
!= 1 || !report2
|| report2
->maxfield
!= 1)
305 spin_lock_irqsave(&data
->lock
, flags
);
306 hid_set_field(report1
->field
[0], 0, chip
<< 2);
307 hid_set_field(report1
->field
[0], 1, 0x02);
308 hid_set_field(report1
->field
[0], 2, 0x00);
309 hid_set_field(report1
->field
[0], 3, 0x00);
310 hid_set_field(report1
->field
[0], 4, 0xb8 | tile
);
311 hid_set_field(report1
->field
[0], 5, 0x00);
312 hid_set_field(report1
->field
[0], 6, 0x00);
313 hid_set_field(report1
->field
[0], 7, 0x40);
314 hid_set_field(report1
->field
[0], 8, 0x00);
315 hid_set_field(report1
->field
[0], 9, 0x00);
316 hid_set_field(report1
->field
[0], 10, 32);
318 hid_set_field(report2
->field
[0], 0, (chip
<< 2) | 0x01);
319 hid_set_field(report2
->field
[0], 1, 0x00);
320 hid_set_field(report2
->field
[0], 2, 0x00);
321 hid_set_field(report2
->field
[0], 3, 32);
323 tdata
= data
->fb_vbitmap
+ (tile
* 4 + chip
) * 64;
324 for (i
= 0; i
< 64; i
++)
326 hid_set_field(report1
->field
[0], 11 + i
, tdata
[i
]);
328 hid_set_field(report2
->field
[0], 4 + i
- 32, tdata
[i
]);
330 usbhid_submit_report(data
->hdev
, report1
, USB_DIR_OUT
);
331 usbhid_submit_report(data
->hdev
, report2
, USB_DIR_OUT
);
332 spin_unlock_irqrestore(&data
->lock
, flags
);
336 /* Translate a single tile*/
337 static int picolcd_fb_update_tile(u8
*vbitmap
, const u8
*bitmap
, int bpp
,
340 int i
, b
, changed
= 0;
342 u8
*vdata
= vbitmap
+ (tile
* 4 + chip
) * 64;
345 for (b
= 7; b
>= 0; b
--) {
346 const u8
*bdata
= bitmap
+ tile
* 256 + chip
* 8 + b
* 32;
347 for (i
= 0; i
< 64; i
++) {
349 tdata
[i
] |= (bdata
[i
/8] >> (7 - i
% 8)) & 0x01;
352 } else if (bpp
== 8) {
353 for (b
= 7; b
>= 0; b
--) {
354 const u8
*bdata
= bitmap
+ (tile
* 256 + chip
* 8 + b
* 32) * 8;
355 for (i
= 0; i
< 64; i
++) {
357 tdata
[i
] |= (bdata
[i
] & 0x80) ? 0x01 : 0x00;
361 /* Oops, we should never get here! */
366 for (i
= 0; i
< 64; i
++)
367 if (tdata
[i
] != vdata
[i
]) {
374 /* Reconfigure LCD display */
375 static int picolcd_fb_reset(struct picolcd_data
*data
, int clear
)
377 struct hid_report
*report
= picolcd_out_report(REPORT_LCD_CMD
, data
->hdev
);
380 static const u8 mapcmd
[8] = { 0x00, 0x02, 0x00, 0x64, 0x3f, 0x00, 0x64, 0xc0 };
382 if (!report
|| report
->maxfield
!= 1)
385 spin_lock_irqsave(&data
->lock
, flags
);
386 for (i
= 0; i
< 4; i
++) {
387 for (j
= 0; j
< report
->field
[0]->maxusage
; j
++)
389 hid_set_field(report
->field
[0], j
, i
<< 2);
390 else if (j
< sizeof(mapcmd
))
391 hid_set_field(report
->field
[0], j
, mapcmd
[j
]);
393 hid_set_field(report
->field
[0], j
, 0);
394 usbhid_submit_report(data
->hdev
, report
, USB_DIR_OUT
);
397 data
->status
|= PICOLCD_READY_FB
;
398 spin_unlock_irqrestore(&data
->lock
, flags
);
400 if (data
->fb_bitmap
) {
402 memset(data
->fb_vbitmap
, 0xff, PICOLCDFB_SIZE
);
403 memset(data
->fb_bitmap
, 0, PICOLCDFB_SIZE
*data
->fb_bpp
);
405 /* invert 1 byte in each tile to force resend */
406 for (i
= 0; i
< PICOLCDFB_SIZE
; i
+= 64)
407 data
->fb_vbitmap
[i
] = ~data
->fb_vbitmap
[i
];
411 /* schedule first output of framebuffer */
413 schedule_delayed_work(&data
->fb_info
->deferred_work
, 0);
418 /* Update fb_vbitmap from the screen_base and send changed tiles to device */
419 static void picolcd_fb_update(struct picolcd_data
*data
)
424 spin_lock_irqsave(&data
->lock
, flags
);
425 if (!(data
->status
& PICOLCD_READY_FB
)) {
426 spin_unlock_irqrestore(&data
->lock
, flags
);
427 picolcd_fb_reset(data
, 0);
429 spin_unlock_irqrestore(&data
->lock
, flags
);
433 * Translate the framebuffer into the format needed by the PicoLCD.
434 * See display layout above.
435 * Do this one tile after the other and push those tiles that changed.
437 * Wait for our IO to complete as otherwise we might flood the queue!
440 for (chip
= 0; chip
< 4; chip
++)
441 for (tile
= 0; tile
< 8; tile
++)
442 if (picolcd_fb_update_tile(data
->fb_vbitmap
,
443 data
->fb_bitmap
, data
->fb_bpp
, chip
, tile
)) {
445 if (n
>= HID_OUTPUT_FIFO_SIZE
/ 2) {
446 usbhid_wait_io(data
->hdev
);
449 picolcd_fb_send_tile(data
->hdev
, chip
, tile
);
452 usbhid_wait_io(data
->hdev
);
455 /* Stub to call the system default and update the image on the picoLCD */
456 static void picolcd_fb_fillrect(struct fb_info
*info
,
457 const struct fb_fillrect
*rect
)
461 sys_fillrect(info
, rect
);
463 schedule_delayed_work(&info
->deferred_work
, 0);
466 /* Stub to call the system default and update the image on the picoLCD */
467 static void picolcd_fb_copyarea(struct fb_info
*info
,
468 const struct fb_copyarea
*area
)
472 sys_copyarea(info
, area
);
474 schedule_delayed_work(&info
->deferred_work
, 0);
477 /* Stub to call the system default and update the image on the picoLCD */
478 static void picolcd_fb_imageblit(struct fb_info
*info
, const struct fb_image
*image
)
482 sys_imageblit(info
, image
);
484 schedule_delayed_work(&info
->deferred_work
, 0);
488 * this is the slow path from userspace. they can seek and write to
489 * the fb. it's inefficient to do anything less than a full screen draw
491 static ssize_t
picolcd_fb_write(struct fb_info
*info
, const char __user
*buf
,
492 size_t count
, loff_t
*ppos
)
497 ret
= fb_sys_write(info
, buf
, count
, ppos
);
499 schedule_delayed_work(&info
->deferred_work
, 0);
503 static int picolcd_fb_blank(int blank
, struct fb_info
*info
)
507 /* We let fb notification do this for us via lcd/backlight device */
511 static void picolcd_fb_destroy(struct fb_info
*info
)
513 struct picolcd_data
*data
= info
->par
;
516 data
->fb_info
= NULL
;
517 fb_deferred_io_cleanup(info
);
518 framebuffer_release(info
);
521 static int picolcd_fb_check_var(struct fb_var_screeninfo
*var
, struct fb_info
*info
)
523 __u32 bpp
= var
->bits_per_pixel
;
524 __u32 activate
= var
->activate
;
526 /* only allow 1/8 bit depth (8-bit is grayscale) */
527 *var
= picolcdfb_var
;
528 var
->activate
= activate
;
530 var
->bits_per_pixel
= 8;
532 var
->bits_per_pixel
= 1;
536 static int picolcd_set_par(struct fb_info
*info
)
538 struct picolcd_data
*data
= info
->par
;
540 if (info
->var
.bits_per_pixel
== data
->fb_bpp
)
542 /* switch between 1/8 bit depths */
543 if (info
->var
.bits_per_pixel
!= 1 && info
->var
.bits_per_pixel
!= 8)
546 o_fb
= data
->fb_bitmap
;
547 n_fb
= vmalloc(PICOLCDFB_SIZE
*info
->var
.bits_per_pixel
);
551 fb_deferred_io_cleanup(info
);
552 /* translate FB content to new bits-per-pixel */
553 if (info
->var
.bits_per_pixel
== 1) {
555 for (i
= 0; i
< PICOLCDFB_SIZE
; i
++) {
557 for (b
= 0; b
< 8; b
++) {
559 p
|= o_fb
[i
*8+b
] ? 0x01 : 0x00;
562 info
->fix
.visual
= FB_VISUAL_MONO01
;
563 info
->fix
.line_length
= PICOLCDFB_WIDTH
/ 8;
566 for (i
= 0; i
< PICOLCDFB_SIZE
* 8; i
++)
567 n_fb
[i
] = o_fb
[i
/8] & (0x01 << (7 - i
% 8)) ? 0xff : 0x00;
568 info
->fix
.visual
= FB_VISUAL_TRUECOLOR
;
569 info
->fix
.line_length
= PICOLCDFB_WIDTH
;
572 data
->fb_bitmap
= n_fb
;
573 data
->fb_bpp
= info
->var
.bits_per_pixel
;
574 info
->screen_base
= (char __force __iomem
*)n_fb
;
575 info
->fix
.smem_start
= (unsigned long)n_fb
;
576 info
->fix
.smem_len
= PICOLCDFB_SIZE
*data
->fb_bpp
;
577 fb_deferred_io_init(info
);
582 /* Note this can't be const because of struct fb_info definition */
583 static struct fb_ops picolcdfb_ops
= {
584 .owner
= THIS_MODULE
,
585 .fb_destroy
= picolcd_fb_destroy
,
586 .fb_read
= fb_sys_read
,
587 .fb_write
= picolcd_fb_write
,
588 .fb_blank
= picolcd_fb_blank
,
589 .fb_fillrect
= picolcd_fb_fillrect
,
590 .fb_copyarea
= picolcd_fb_copyarea
,
591 .fb_imageblit
= picolcd_fb_imageblit
,
592 .fb_check_var
= picolcd_fb_check_var
,
593 .fb_set_par
= picolcd_set_par
,
597 /* Callback from deferred IO workqueue */
598 static void picolcd_fb_deferred_io(struct fb_info
*info
, struct list_head
*pagelist
)
600 picolcd_fb_update(info
->par
);
603 static const struct fb_deferred_io picolcd_fb_defio
= {
604 .delay
= HZ
/ PICOLCDFB_UPDATE_RATE_DEFAULT
,
605 .deferred_io
= picolcd_fb_deferred_io
,
610 * The "fb_update_rate" sysfs attribute
612 static ssize_t
picolcd_fb_update_rate_show(struct device
*dev
,
613 struct device_attribute
*attr
, char *buf
)
615 struct picolcd_data
*data
= dev_get_drvdata(dev
);
616 unsigned i
, fb_update_rate
= data
->fb_update_rate
;
619 for (i
= 1; i
<= PICOLCDFB_UPDATE_RATE_LIMIT
; i
++)
620 if (ret
>= PAGE_SIZE
)
622 else if (i
== fb_update_rate
)
623 ret
+= snprintf(buf
+ret
, PAGE_SIZE
-ret
, "[%u] ", i
);
625 ret
+= snprintf(buf
+ret
, PAGE_SIZE
-ret
, "%u ", i
);
627 buf
[min(ret
, (size_t)PAGE_SIZE
)-1] = '\n';
631 static ssize_t
picolcd_fb_update_rate_store(struct device
*dev
,
632 struct device_attribute
*attr
, const char *buf
, size_t count
)
634 struct picolcd_data
*data
= dev_get_drvdata(dev
);
638 if (count
< 1 || count
> 10)
641 i
= sscanf(buf
, "%u", &u
);
645 if (u
> PICOLCDFB_UPDATE_RATE_LIMIT
)
648 u
= PICOLCDFB_UPDATE_RATE_DEFAULT
;
650 data
->fb_update_rate
= u
;
651 data
->fb_defio
.delay
= HZ
/ data
->fb_update_rate
;
655 static DEVICE_ATTR(fb_update_rate
, 0666, picolcd_fb_update_rate_show
,
656 picolcd_fb_update_rate_store
);
658 /* initialize Framebuffer device */
659 static int picolcd_init_framebuffer(struct picolcd_data
*data
)
661 struct device
*dev
= &data
->hdev
->dev
;
662 struct fb_info
*info
= NULL
;
664 u8
*fb_vbitmap
= NULL
;
665 u8
*fb_bitmap
= NULL
;
667 fb_bitmap
= vmalloc(PICOLCDFB_SIZE
*picolcdfb_var
.bits_per_pixel
);
668 if (fb_bitmap
== NULL
) {
669 dev_err(dev
, "can't get a free page for framebuffer\n");
673 fb_vbitmap
= kmalloc(PICOLCDFB_SIZE
, GFP_KERNEL
);
674 if (fb_vbitmap
== NULL
) {
675 dev_err(dev
, "can't alloc vbitmap image buffer\n");
679 data
->fb_update_rate
= PICOLCDFB_UPDATE_RATE_DEFAULT
;
680 data
->fb_defio
= picolcd_fb_defio
;
681 info
= framebuffer_alloc(0, dev
);
683 dev_err(dev
, "failed to allocate a framebuffer\n");
687 info
->fbdefio
= &data
->fb_defio
;
688 info
->screen_base
= (char __force __iomem
*)fb_bitmap
;
689 info
->fbops
= &picolcdfb_ops
;
690 info
->var
= picolcdfb_var
;
691 info
->fix
= picolcdfb_fix
;
692 info
->fix
.smem_len
= PICOLCDFB_SIZE
;
693 info
->fix
.smem_start
= (unsigned long)fb_bitmap
;
695 info
->flags
= FBINFO_FLAG_DEFAULT
;
697 data
->fb_vbitmap
= fb_vbitmap
;
698 data
->fb_bitmap
= fb_bitmap
;
699 data
->fb_bpp
= picolcdfb_var
.bits_per_pixel
;
700 error
= picolcd_fb_reset(data
, 1);
702 dev_err(dev
, "failed to configure display\n");
705 error
= device_create_file(dev
, &dev_attr_fb_update_rate
);
707 dev_err(dev
, "failed to create sysfs attributes\n");
710 data
->fb_info
= info
;
711 error
= register_framebuffer(info
);
713 dev_err(dev
, "failed to register framebuffer\n");
716 fb_deferred_io_init(info
);
717 /* schedule first output of framebuffer */
718 schedule_delayed_work(&info
->deferred_work
, 0);
722 device_remove_file(dev
, &dev_attr_fb_update_rate
);
724 data
->fb_vbitmap
= NULL
;
725 data
->fb_bitmap
= NULL
;
727 data
->fb_info
= NULL
;
730 framebuffer_release(info
);
736 static void picolcd_exit_framebuffer(struct picolcd_data
*data
)
738 struct fb_info
*info
= data
->fb_info
;
739 u8
*fb_vbitmap
= data
->fb_vbitmap
;
740 u8
*fb_bitmap
= data
->fb_bitmap
;
745 data
->fb_vbitmap
= NULL
;
746 data
->fb_bitmap
= NULL
;
748 data
->fb_info
= NULL
;
749 device_remove_file(&data
->hdev
->dev
, &dev_attr_fb_update_rate
);
750 fb_deferred_io_cleanup(info
);
751 unregister_framebuffer(info
);
756 #define picolcd_fbinfo(d) ((d)->fb_info)
758 static inline int picolcd_fb_reset(struct picolcd_data
*data
, int clear
)
762 static inline int picolcd_init_framebuffer(struct picolcd_data
*data
)
766 static inline void picolcd_exit_framebuffer(struct picolcd_data
*data
)
769 #define picolcd_fbinfo(d) NULL
770 #endif /* CONFIG_HID_PICOLCD_FB */
772 #ifdef CONFIG_HID_PICOLCD_BACKLIGHT
774 * backlight class device
776 static int picolcd_get_brightness(struct backlight_device
*bdev
)
778 struct picolcd_data
*data
= bl_get_data(bdev
);
779 return data
->lcd_brightness
;
782 static int picolcd_set_brightness(struct backlight_device
*bdev
)
784 struct picolcd_data
*data
= bl_get_data(bdev
);
785 struct hid_report
*report
= picolcd_out_report(REPORT_BRIGHTNESS
, data
->hdev
);
788 if (!report
|| report
->maxfield
!= 1 || report
->field
[0]->report_count
!= 1)
791 data
->lcd_brightness
= bdev
->props
.brightness
& 0x0ff;
792 data
->lcd_power
= bdev
->props
.power
;
793 spin_lock_irqsave(&data
->lock
, flags
);
794 hid_set_field(report
->field
[0], 0, data
->lcd_power
== FB_BLANK_UNBLANK
? data
->lcd_brightness
: 0);
795 usbhid_submit_report(data
->hdev
, report
, USB_DIR_OUT
);
796 spin_unlock_irqrestore(&data
->lock
, flags
);
800 static int picolcd_check_bl_fb(struct backlight_device
*bdev
, struct fb_info
*fb
)
802 return fb
&& fb
== picolcd_fbinfo((struct picolcd_data
*)bl_get_data(bdev
));
805 static const struct backlight_ops picolcd_blops
= {
806 .update_status
= picolcd_set_brightness
,
807 .get_brightness
= picolcd_get_brightness
,
808 .check_fb
= picolcd_check_bl_fb
,
811 static int picolcd_init_backlight(struct picolcd_data
*data
, struct hid_report
*report
)
813 struct device
*dev
= &data
->hdev
->dev
;
814 struct backlight_device
*bdev
;
815 struct backlight_properties props
;
818 if (report
->maxfield
!= 1 || report
->field
[0]->report_count
!= 1 ||
819 report
->field
[0]->report_size
!= 8) {
820 dev_err(dev
, "unsupported BRIGHTNESS report");
824 memset(&props
, 0, sizeof(props
));
825 props
.max_brightness
= 0xff;
826 bdev
= backlight_device_register(dev_name(dev
), dev
, data
,
827 &picolcd_blops
, &props
);
829 dev_err(dev
, "failed to register backlight\n");
830 return PTR_ERR(bdev
);
832 bdev
->props
.brightness
= 0xff;
833 data
->lcd_brightness
= 0xff;
834 data
->backlight
= bdev
;
835 picolcd_set_brightness(bdev
);
839 static void picolcd_exit_backlight(struct picolcd_data
*data
)
841 struct backlight_device
*bdev
= data
->backlight
;
843 data
->backlight
= NULL
;
845 backlight_device_unregister(bdev
);
848 static inline int picolcd_resume_backlight(struct picolcd_data
*data
)
850 if (!data
->backlight
)
852 return picolcd_set_brightness(data
->backlight
);
856 static void picolcd_suspend_backlight(struct picolcd_data
*data
)
858 int bl_power
= data
->lcd_power
;
859 if (!data
->backlight
)
862 data
->backlight
->props
.power
= FB_BLANK_POWERDOWN
;
863 picolcd_set_brightness(data
->backlight
);
864 data
->lcd_power
= data
->backlight
->props
.power
= bl_power
;
866 #endif /* CONFIG_PM */
868 static inline int picolcd_init_backlight(struct picolcd_data
*data
,
869 struct hid_report
*report
)
873 static inline void picolcd_exit_backlight(struct picolcd_data
*data
)
876 static inline int picolcd_resume_backlight(struct picolcd_data
*data
)
880 static inline void picolcd_suspend_backlight(struct picolcd_data
*data
)
883 #endif /* CONFIG_HID_PICOLCD_BACKLIGHT */
885 #ifdef CONFIG_HID_PICOLCD_LCD
889 static int picolcd_get_contrast(struct lcd_device
*ldev
)
891 struct picolcd_data
*data
= lcd_get_data(ldev
);
892 return data
->lcd_contrast
;
895 static int picolcd_set_contrast(struct lcd_device
*ldev
, int contrast
)
897 struct picolcd_data
*data
= lcd_get_data(ldev
);
898 struct hid_report
*report
= picolcd_out_report(REPORT_CONTRAST
, data
->hdev
);
901 if (!report
|| report
->maxfield
!= 1 || report
->field
[0]->report_count
!= 1)
904 data
->lcd_contrast
= contrast
& 0x0ff;
905 spin_lock_irqsave(&data
->lock
, flags
);
906 hid_set_field(report
->field
[0], 0, data
->lcd_contrast
);
907 usbhid_submit_report(data
->hdev
, report
, USB_DIR_OUT
);
908 spin_unlock_irqrestore(&data
->lock
, flags
);
912 static int picolcd_check_lcd_fb(struct lcd_device
*ldev
, struct fb_info
*fb
)
914 return fb
&& fb
== picolcd_fbinfo((struct picolcd_data
*)lcd_get_data(ldev
));
917 static struct lcd_ops picolcd_lcdops
= {
918 .get_contrast
= picolcd_get_contrast
,
919 .set_contrast
= picolcd_set_contrast
,
920 .check_fb
= picolcd_check_lcd_fb
,
923 static int picolcd_init_lcd(struct picolcd_data
*data
, struct hid_report
*report
)
925 struct device
*dev
= &data
->hdev
->dev
;
926 struct lcd_device
*ldev
;
930 if (report
->maxfield
!= 1 || report
->field
[0]->report_count
!= 1 ||
931 report
->field
[0]->report_size
!= 8) {
932 dev_err(dev
, "unsupported CONTRAST report");
936 ldev
= lcd_device_register(dev_name(dev
), dev
, data
, &picolcd_lcdops
);
938 dev_err(dev
, "failed to register LCD\n");
939 return PTR_ERR(ldev
);
941 ldev
->props
.max_contrast
= 0x0ff;
942 data
->lcd_contrast
= 0xe5;
944 picolcd_set_contrast(ldev
, 0xe5);
948 static void picolcd_exit_lcd(struct picolcd_data
*data
)
950 struct lcd_device
*ldev
= data
->lcd
;
954 lcd_device_unregister(ldev
);
957 static inline int picolcd_resume_lcd(struct picolcd_data
*data
)
961 return picolcd_set_contrast(data
->lcd
, data
->lcd_contrast
);
964 static inline int picolcd_init_lcd(struct picolcd_data
*data
,
965 struct hid_report
*report
)
969 static inline void picolcd_exit_lcd(struct picolcd_data
*data
)
972 static inline int picolcd_resume_lcd(struct picolcd_data
*data
)
976 #endif /* CONFIG_HID_PICOLCD_LCD */
978 #ifdef CONFIG_HID_PICOLCD_LEDS
982 static void picolcd_leds_set(struct picolcd_data
*data
)
984 struct hid_report
*report
;
989 report
= picolcd_out_report(REPORT_LED_STATE
, data
->hdev
);
990 if (!report
|| report
->maxfield
!= 1 || report
->field
[0]->report_count
!= 1)
993 spin_lock_irqsave(&data
->lock
, flags
);
994 hid_set_field(report
->field
[0], 0, data
->led_state
);
995 usbhid_submit_report(data
->hdev
, report
, USB_DIR_OUT
);
996 spin_unlock_irqrestore(&data
->lock
, flags
);
999 static void picolcd_led_set_brightness(struct led_classdev
*led_cdev
,
1000 enum led_brightness value
)
1003 struct hid_device
*hdev
;
1004 struct picolcd_data
*data
;
1007 dev
= led_cdev
->dev
->parent
;
1008 hdev
= container_of(dev
, struct hid_device
, dev
);
1009 data
= hid_get_drvdata(hdev
);
1010 for (i
= 0; i
< 8; i
++) {
1011 if (led_cdev
!= data
->led
[i
])
1013 state
= (data
->led_state
>> i
) & 1;
1014 if (value
== LED_OFF
&& state
) {
1015 data
->led_state
&= ~(1 << i
);
1016 picolcd_leds_set(data
);
1017 } else if (value
!= LED_OFF
&& !state
) {
1018 data
->led_state
|= 1 << i
;
1019 picolcd_leds_set(data
);
1025 static enum led_brightness
picolcd_led_get_brightness(struct led_classdev
*led_cdev
)
1028 struct hid_device
*hdev
;
1029 struct picolcd_data
*data
;
1032 dev
= led_cdev
->dev
->parent
;
1033 hdev
= container_of(dev
, struct hid_device
, dev
);
1034 data
= hid_get_drvdata(hdev
);
1035 for (i
= 0; i
< 8; i
++)
1036 if (led_cdev
== data
->led
[i
]) {
1037 value
= (data
->led_state
>> i
) & 1;
1040 return value
? LED_FULL
: LED_OFF
;
1043 static int picolcd_init_leds(struct picolcd_data
*data
, struct hid_report
*report
)
1045 struct device
*dev
= &data
->hdev
->dev
;
1046 struct led_classdev
*led
;
1047 size_t name_sz
= strlen(dev_name(dev
)) + 8;
1053 if (report
->maxfield
!= 1 || report
->field
[0]->report_count
!= 1 ||
1054 report
->field
[0]->report_size
!= 8) {
1055 dev_err(dev
, "unsupported LED_STATE report");
1059 for (i
= 0; i
< 8; i
++) {
1060 led
= kzalloc(sizeof(struct led_classdev
)+name_sz
, GFP_KERNEL
);
1062 dev_err(dev
, "can't allocate memory for LED %d\n", i
);
1066 name
= (void *)(&led
[1]);
1067 snprintf(name
, name_sz
, "%s::GPO%d", dev_name(dev
), i
);
1069 led
->brightness
= 0;
1070 led
->max_brightness
= 1;
1071 led
->brightness_get
= picolcd_led_get_brightness
;
1072 led
->brightness_set
= picolcd_led_set_brightness
;
1075 ret
= led_classdev_register(dev
, data
->led
[i
]);
1077 data
->led
[i
] = NULL
;
1079 dev_err(dev
, "can't register LED %d\n", i
);
1085 for (i
= 0; i
< 8; i
++)
1088 data
->led
[i
] = NULL
;
1089 led_classdev_unregister(led
);
1095 static void picolcd_exit_leds(struct picolcd_data
*data
)
1097 struct led_classdev
*led
;
1100 for (i
= 0; i
< 8; i
++) {
1102 data
->led
[i
] = NULL
;
1105 led_classdev_unregister(led
);
1111 static inline int picolcd_init_leds(struct picolcd_data
*data
,
1112 struct hid_report
*report
)
1116 static inline void picolcd_exit_leds(struct picolcd_data
*data
)
1119 static inline int picolcd_leds_set(struct picolcd_data
*data
)
1123 #endif /* CONFIG_HID_PICOLCD_LEDS */
1126 * input class device
1128 static int picolcd_raw_keypad(struct picolcd_data
*data
,
1129 struct hid_report
*report
, u8
*raw_data
, int size
)
1133 * First and second data bytes list currently pressed keys,
1134 * 0x00 means no key and at most 2 keys may be pressed at same time
1138 /* determine newly pressed keys */
1139 for (i
= 0; i
< size
; i
++) {
1140 unsigned int key_code
;
1141 if (raw_data
[i
] == 0)
1143 for (j
= 0; j
< sizeof(data
->pressed_keys
); j
++)
1144 if (data
->pressed_keys
[j
] == raw_data
[i
])
1145 goto key_already_down
;
1146 for (j
= 0; j
< sizeof(data
->pressed_keys
); j
++)
1147 if (data
->pressed_keys
[j
] == 0) {
1148 data
->pressed_keys
[j
] = raw_data
[i
];
1151 input_event(data
->input_keys
, EV_MSC
, MSC_SCAN
, raw_data
[i
]);
1152 if (raw_data
[i
] < PICOLCD_KEYS
)
1153 key_code
= data
->keycode
[raw_data
[i
]];
1155 key_code
= KEY_UNKNOWN
;
1156 if (key_code
!= KEY_UNKNOWN
) {
1157 dbg_hid(PICOLCD_NAME
" got key press for %u:%d",
1158 raw_data
[i
], key_code
);
1159 input_report_key(data
->input_keys
, key_code
, 1);
1161 input_sync(data
->input_keys
);
1166 /* determine newly released keys */
1167 for (j
= 0; j
< sizeof(data
->pressed_keys
); j
++) {
1168 unsigned int key_code
;
1169 if (data
->pressed_keys
[j
] == 0)
1171 for (i
= 0; i
< size
; i
++)
1172 if (data
->pressed_keys
[j
] == raw_data
[i
])
1173 goto key_still_down
;
1174 input_event(data
->input_keys
, EV_MSC
, MSC_SCAN
, data
->pressed_keys
[j
]);
1175 if (data
->pressed_keys
[j
] < PICOLCD_KEYS
)
1176 key_code
= data
->keycode
[data
->pressed_keys
[j
]];
1178 key_code
= KEY_UNKNOWN
;
1179 if (key_code
!= KEY_UNKNOWN
) {
1180 dbg_hid(PICOLCD_NAME
" got key release for %u:%d",
1181 data
->pressed_keys
[j
], key_code
);
1182 input_report_key(data
->input_keys
, key_code
, 0);
1184 input_sync(data
->input_keys
);
1185 data
->pressed_keys
[j
] = 0;
1192 static int picolcd_raw_cir(struct picolcd_data
*data
,
1193 struct hid_report
*report
, u8
*raw_data
, int size
)
1195 /* Need understanding of CIR data format to implement ... */
1199 static int picolcd_check_version(struct hid_device
*hdev
)
1201 struct picolcd_data
*data
= hid_get_drvdata(hdev
);
1202 struct picolcd_pending
*verinfo
;
1208 verinfo
= picolcd_send_and_wait(hdev
, REPORT_VERSION
, NULL
, 0);
1210 dev_err(&hdev
->dev
, "no version response from PicoLCD");
1214 if (verinfo
->raw_size
== 2) {
1215 data
->version
[0] = verinfo
->raw_data
[1];
1216 data
->version
[1] = verinfo
->raw_data
[0];
1217 if (data
->status
& PICOLCD_BOOTLOADER
) {
1218 dev_info(&hdev
->dev
, "PicoLCD, bootloader version %d.%d\n",
1219 verinfo
->raw_data
[1], verinfo
->raw_data
[0]);
1221 dev_info(&hdev
->dev
, "PicoLCD, firmware version %d.%d\n",
1222 verinfo
->raw_data
[1], verinfo
->raw_data
[0]);
1225 dev_err(&hdev
->dev
, "confused, got unexpected version response from PicoLCD\n");
1233 * Reset our device and wait for answer to VERSION request
1235 static int picolcd_reset(struct hid_device
*hdev
)
1237 struct picolcd_data
*data
= hid_get_drvdata(hdev
);
1238 struct hid_report
*report
= picolcd_out_report(REPORT_RESET
, hdev
);
1239 unsigned long flags
;
1242 if (!data
|| !report
|| report
->maxfield
!= 1)
1245 spin_lock_irqsave(&data
->lock
, flags
);
1246 if (hdev
->product
== USB_DEVICE_ID_PICOLCD_BOOTLOADER
)
1247 data
->status
|= PICOLCD_BOOTLOADER
;
1249 /* perform the reset */
1250 hid_set_field(report
->field
[0], 0, 1);
1251 usbhid_submit_report(hdev
, report
, USB_DIR_OUT
);
1252 spin_unlock_irqrestore(&data
->lock
, flags
);
1254 error
= picolcd_check_version(hdev
);
1258 picolcd_resume_lcd(data
);
1259 picolcd_resume_backlight(data
);
1260 #ifdef CONFIG_HID_PICOLCD_FB
1262 schedule_delayed_work(&data
->fb_info
->deferred_work
, 0);
1263 #endif /* CONFIG_HID_PICOLCD_FB */
1265 picolcd_leds_set(data
);
1270 * The "operation_mode" sysfs attribute
1272 static ssize_t
picolcd_operation_mode_show(struct device
*dev
,
1273 struct device_attribute
*attr
, char *buf
)
1275 struct picolcd_data
*data
= dev_get_drvdata(dev
);
1277 if (data
->status
& PICOLCD_BOOTLOADER
)
1278 return snprintf(buf
, PAGE_SIZE
, "[bootloader] lcd\n");
1280 return snprintf(buf
, PAGE_SIZE
, "bootloader [lcd]\n");
1283 static ssize_t
picolcd_operation_mode_store(struct device
*dev
,
1284 struct device_attribute
*attr
, const char *buf
, size_t count
)
1286 struct picolcd_data
*data
= dev_get_drvdata(dev
);
1287 struct hid_report
*report
= NULL
;
1289 int timeout
= data
->opmode_delay
;
1290 unsigned long flags
;
1292 if (cnt
>= 3 && strncmp("lcd", buf
, 3) == 0) {
1293 if (data
->status
& PICOLCD_BOOTLOADER
)
1294 report
= picolcd_out_report(REPORT_EXIT_FLASHER
, data
->hdev
);
1297 } else if (cnt
>= 10 && strncmp("bootloader", buf
, 10) == 0) {
1298 if (!(data
->status
& PICOLCD_BOOTLOADER
))
1299 report
= picolcd_out_report(REPORT_EXIT_KEYBOARD
, data
->hdev
);
1306 while (cnt
> 0 && (buf
[cnt
-1] == '\n' || buf
[cnt
-1] == '\r'))
1311 spin_lock_irqsave(&data
->lock
, flags
);
1312 hid_set_field(report
->field
[0], 0, timeout
& 0xff);
1313 hid_set_field(report
->field
[0], 1, (timeout
>> 8) & 0xff);
1314 usbhid_submit_report(data
->hdev
, report
, USB_DIR_OUT
);
1315 spin_unlock_irqrestore(&data
->lock
, flags
);
1319 static DEVICE_ATTR(operation_mode
, 0644, picolcd_operation_mode_show
,
1320 picolcd_operation_mode_store
);
1323 * The "operation_mode_delay" sysfs attribute
1325 static ssize_t
picolcd_operation_mode_delay_show(struct device
*dev
,
1326 struct device_attribute
*attr
, char *buf
)
1328 struct picolcd_data
*data
= dev_get_drvdata(dev
);
1330 return snprintf(buf
, PAGE_SIZE
, "%hu\n", data
->opmode_delay
);
1333 static ssize_t
picolcd_operation_mode_delay_store(struct device
*dev
,
1334 struct device_attribute
*attr
, const char *buf
, size_t count
)
1336 struct picolcd_data
*data
= dev_get_drvdata(dev
);
1338 if (sscanf(buf
, "%u", &u
) != 1)
1343 data
->opmode_delay
= u
;
1347 static DEVICE_ATTR(operation_mode_delay
, 0644, picolcd_operation_mode_delay_show
,
1348 picolcd_operation_mode_delay_store
);
1351 #ifdef CONFIG_DEBUG_FS
1355 static int picolcd_debug_reset_show(struct seq_file
*f
, void *p
)
1357 if (picolcd_fbinfo((struct picolcd_data
*)f
->private))
1358 seq_printf(f
, "all fb\n");
1360 seq_printf(f
, "all\n");
1364 static int picolcd_debug_reset_open(struct inode
*inode
, struct file
*f
)
1366 return single_open(f
, picolcd_debug_reset_show
, inode
->i_private
);
1369 static ssize_t
picolcd_debug_reset_write(struct file
*f
, const char __user
*user_buf
,
1370 size_t count
, loff_t
*ppos
)
1372 struct picolcd_data
*data
= ((struct seq_file
*)f
->private_data
)->private;
1374 size_t cnt
= min(count
, sizeof(buf
)-1);
1375 if (copy_from_user(buf
, user_buf
, cnt
))
1378 while (cnt
> 0 && (buf
[cnt
-1] == ' ' || buf
[cnt
-1] == '\n'))
1381 if (strcmp(buf
, "all") == 0) {
1382 picolcd_reset(data
->hdev
);
1383 picolcd_fb_reset(data
, 1);
1384 } else if (strcmp(buf
, "fb") == 0) {
1385 picolcd_fb_reset(data
, 1);
1392 static const struct file_operations picolcd_debug_reset_fops
= {
1393 .owner
= THIS_MODULE
,
1394 .open
= picolcd_debug_reset_open
,
1396 .llseek
= seq_lseek
,
1397 .write
= picolcd_debug_reset_write
,
1398 .release
= single_release
,
1404 static int picolcd_debug_eeprom_open(struct inode
*i
, struct file
*f
)
1406 f
->private_data
= i
->i_private
;
1410 static ssize_t
picolcd_debug_eeprom_read(struct file
*f
, char __user
*u
,
1411 size_t s
, loff_t
*off
)
1413 struct picolcd_data
*data
= f
->private_data
;
1414 struct picolcd_pending
*resp
;
1423 /* prepare buffer with info about what we want to read (addr & len) */
1424 raw_data
[0] = *off
& 0xff;
1425 raw_data
[1] = (*off
>> 8) && 0xff;
1426 raw_data
[2] = s
< 20 ? s
: 20;
1427 if (*off
+ raw_data
[2] > 0xff)
1428 raw_data
[2] = 0x100 - *off
;
1429 resp
= picolcd_send_and_wait(data
->hdev
, REPORT_EE_READ
, raw_data
,
1434 if (resp
->in_report
&& resp
->in_report
->id
== REPORT_EE_DATA
) {
1435 /* successful read :) */
1436 ret
= resp
->raw_data
[2];
1439 if (copy_to_user(u
, resp
->raw_data
+3, ret
))
1443 } /* anything else is some kind of IO error */
1449 static ssize_t
picolcd_debug_eeprom_write(struct file
*f
, const char __user
*u
,
1450 size_t s
, loff_t
*off
)
1452 struct picolcd_data
*data
= f
->private_data
;
1453 struct picolcd_pending
*resp
;
1462 memset(raw_data
, 0, sizeof(raw_data
));
1463 raw_data
[0] = *off
& 0xff;
1464 raw_data
[1] = (*off
>> 8) && 0xff;
1465 raw_data
[2] = s
< 20 ? s
: 20;
1466 if (*off
+ raw_data
[2] > 0xff)
1467 raw_data
[2] = 0x100 - *off
;
1469 if (copy_from_user(raw_data
+3, u
, raw_data
[2]))
1471 resp
= picolcd_send_and_wait(data
->hdev
, REPORT_EE_WRITE
, raw_data
,
1477 if (resp
->in_report
&& resp
->in_report
->id
== REPORT_EE_DATA
) {
1478 /* check if written data matches */
1479 if (memcmp(raw_data
, resp
->raw_data
, 3+raw_data
[2]) == 0) {
1480 *off
+= raw_data
[2];
1490 * - read/write happens in chunks of at most 20 bytes, it's up to userspace
1491 * to loop in order to get more data.
1492 * - on write errors on otherwise correct write request the bytes
1493 * that should have been written are in undefined state.
1495 static const struct file_operations picolcd_debug_eeprom_fops
= {
1496 .owner
= THIS_MODULE
,
1497 .open
= picolcd_debug_eeprom_open
,
1498 .read
= picolcd_debug_eeprom_read
,
1499 .write
= picolcd_debug_eeprom_write
,
1500 .llseek
= generic_file_llseek
,
1506 static int picolcd_debug_flash_open(struct inode
*i
, struct file
*f
)
1508 f
->private_data
= i
->i_private
;
1512 /* record a flash address to buf (bounds check to be done by caller) */
1513 static int _picolcd_flash_setaddr(struct picolcd_data
*data
, u8
*buf
, long off
)
1515 buf
[0] = off
& 0xff;
1516 buf
[1] = (off
>> 8) & 0xff;
1517 if (data
->addr_sz
== 3)
1518 buf
[2] = (off
>> 16) & 0xff;
1519 return data
->addr_sz
== 2 ? 2 : 3;
1522 /* read a given size of data (bounds check to be done by caller) */
1523 static ssize_t
_picolcd_flash_read(struct picolcd_data
*data
, int report_id
,
1524 char __user
*u
, size_t s
, loff_t
*off
)
1526 struct picolcd_pending
*resp
;
1529 int len_off
, err
= -EIO
;
1533 len_off
= _picolcd_flash_setaddr(data
, raw_data
, *off
);
1534 raw_data
[len_off
] = s
> 32 ? 32 : s
;
1535 resp
= picolcd_send_and_wait(data
->hdev
, report_id
, raw_data
, len_off
+1);
1536 if (!resp
|| !resp
->in_report
)
1538 if (resp
->in_report
->id
== REPORT_MEMORY
||
1539 resp
->in_report
->id
== REPORT_BL_READ_MEMORY
) {
1540 if (memcmp(raw_data
, resp
->raw_data
, len_off
+1) != 0)
1542 if (copy_to_user(u
+ret
, resp
->raw_data
+len_off
+1, raw_data
[len_off
])) {
1546 *off
+= raw_data
[len_off
];
1547 s
-= raw_data
[len_off
];
1548 ret
+= raw_data
[len_off
];
1554 return ret
> 0 ? ret
: err
;
1559 static ssize_t
picolcd_debug_flash_read(struct file
*f
, char __user
*u
,
1560 size_t s
, loff_t
*off
)
1562 struct picolcd_data
*data
= f
->private_data
;
1568 if (*off
+ s
> 0x05fff)
1571 if (data
->status
& PICOLCD_BOOTLOADER
)
1572 return _picolcd_flash_read(data
, REPORT_BL_READ_MEMORY
, u
, s
, off
);
1574 return _picolcd_flash_read(data
, REPORT_READ_MEMORY
, u
, s
, off
);
1577 /* erase block aligned to 64bytes boundary */
1578 static ssize_t
_picolcd_flash_erase64(struct picolcd_data
*data
, int report_id
,
1581 struct picolcd_pending
*resp
;
1589 len_off
= _picolcd_flash_setaddr(data
, raw_data
, *off
);
1590 resp
= picolcd_send_and_wait(data
->hdev
, report_id
, raw_data
, len_off
);
1591 if (!resp
|| !resp
->in_report
)
1593 if (resp
->in_report
->id
== REPORT_MEMORY
||
1594 resp
->in_report
->id
== REPORT_BL_ERASE_MEMORY
) {
1595 if (memcmp(raw_data
, resp
->raw_data
, len_off
) != 0)
1604 /* write a given size of data (bounds check to be done by caller) */
1605 static ssize_t
_picolcd_flash_write(struct picolcd_data
*data
, int report_id
,
1606 const char __user
*u
, size_t s
, loff_t
*off
)
1608 struct picolcd_pending
*resp
;
1611 int len_off
, err
= -EIO
;
1615 len_off
= _picolcd_flash_setaddr(data
, raw_data
, *off
);
1616 raw_data
[len_off
] = s
> 32 ? 32 : s
;
1617 if (copy_from_user(raw_data
+len_off
+1, u
, raw_data
[len_off
])) {
1621 resp
= picolcd_send_and_wait(data
->hdev
, report_id
, raw_data
,
1622 len_off
+1+raw_data
[len_off
]);
1623 if (!resp
|| !resp
->in_report
)
1625 if (resp
->in_report
->id
== REPORT_MEMORY
||
1626 resp
->in_report
->id
== REPORT_BL_WRITE_MEMORY
) {
1627 if (memcmp(raw_data
, resp
->raw_data
, len_off
+1+raw_data
[len_off
]) != 0)
1629 *off
+= raw_data
[len_off
];
1630 s
-= raw_data
[len_off
];
1631 ret
+= raw_data
[len_off
];
1639 return ret
> 0 ? ret
: err
;
1642 static ssize_t
picolcd_debug_flash_write(struct file
*f
, const char __user
*u
,
1643 size_t s
, loff_t
*off
)
1645 struct picolcd_data
*data
= f
->private_data
;
1646 ssize_t err
, ret
= 0;
1647 int report_erase
, report_write
;
1658 if (data
->status
& PICOLCD_BOOTLOADER
) {
1659 report_erase
= REPORT_BL_ERASE_MEMORY
;
1660 report_write
= REPORT_BL_WRITE_MEMORY
;
1662 report_erase
= REPORT_ERASE_MEMORY
;
1663 report_write
= REPORT_WRITE_MEMORY
;
1665 mutex_lock(&data
->mutex_flash
);
1667 err
= _picolcd_flash_erase64(data
, report_erase
, off
);
1670 err
= _picolcd_flash_write(data
, report_write
, u
, 64, off
);
1679 mutex_unlock(&data
->mutex_flash
);
1680 return ret
> 0 ? ret
: err
;
1685 * - concurrent writing is prevented by mutex and all writes must be
1686 * n*64 bytes and 64-byte aligned, each write being preceeded by an
1687 * ERASE which erases a 64byte block.
1688 * If less than requested was written or an error is returned for an
1689 * otherwise correct write request the next 64-byte block which should
1690 * have been written is in undefined state (mostly: original, erased,
1691 * (half-)written with write error)
1692 * - reading can happend without special restriction
1694 static const struct file_operations picolcd_debug_flash_fops
= {
1695 .owner
= THIS_MODULE
,
1696 .open
= picolcd_debug_flash_open
,
1697 .read
= picolcd_debug_flash_read
,
1698 .write
= picolcd_debug_flash_write
,
1699 .llseek
= generic_file_llseek
,
1704 * Helper code for HID report level dumping/debugging
1706 static const char *error_codes
[] = {
1707 "success", "parameter missing", "data_missing", "block readonly",
1708 "block not erasable", "block too big", "section overflow",
1709 "invalid command length", "invalid data length",
1712 static void dump_buff_as_hex(char *dst
, size_t dst_sz
, const u8
*data
,
1713 const size_t data_len
)
1716 for (i
= j
= 0; i
< data_len
&& j
+ 3 < dst_sz
; i
++) {
1717 dst
[j
++] = hex_asc
[(data
[i
] >> 4) & 0x0f];
1718 dst
[j
++] = hex_asc
[data
[i
] & 0x0f];
1728 static void picolcd_debug_out_report(struct picolcd_data
*data
,
1729 struct hid_device
*hdev
, struct hid_report
*report
)
1732 int raw_size
= (report
->size
>> 3) + 1;
1736 /* Avoid unnecessary overhead if debugfs is disabled */
1737 if (!hdev
->debug_events
)
1740 buff
= kmalloc(BUFF_SZ
, GFP_ATOMIC
);
1744 snprintf(buff
, BUFF_SZ
, "\nout report %d (size %d) = ",
1745 report
->id
, raw_size
);
1746 hid_debug_event(hdev
, buff
);
1747 if (raw_size
+ 5 > sizeof(raw_data
)) {
1748 hid_debug_event(hdev
, " TOO BIG\n");
1751 raw_data
[0] = report
->id
;
1752 hid_output_report(report
, raw_data
);
1753 dump_buff_as_hex(buff
, BUFF_SZ
, raw_data
, raw_size
);
1754 hid_debug_event(hdev
, buff
);
1757 switch (report
->id
) {
1758 case REPORT_LED_STATE
:
1759 /* 1 data byte with GPO state */
1760 snprintf(buff
, BUFF_SZ
, "out report %s (%d, size=%d)\n",
1761 "REPORT_LED_STATE", report
->id
, raw_size
-1);
1762 hid_debug_event(hdev
, buff
);
1763 snprintf(buff
, BUFF_SZ
, "\tGPO state: 0x%02x\n", raw_data
[1]);
1764 hid_debug_event(hdev
, buff
);
1766 case REPORT_BRIGHTNESS
:
1767 /* 1 data byte with brightness */
1768 snprintf(buff
, BUFF_SZ
, "out report %s (%d, size=%d)\n",
1769 "REPORT_BRIGHTNESS", report
->id
, raw_size
-1);
1770 hid_debug_event(hdev
, buff
);
1771 snprintf(buff
, BUFF_SZ
, "\tBrightness: 0x%02x\n", raw_data
[1]);
1772 hid_debug_event(hdev
, buff
);
1774 case REPORT_CONTRAST
:
1775 /* 1 data byte with contrast */
1776 snprintf(buff
, BUFF_SZ
, "out report %s (%d, size=%d)\n",
1777 "REPORT_CONTRAST", report
->id
, raw_size
-1);
1778 hid_debug_event(hdev
, buff
);
1779 snprintf(buff
, BUFF_SZ
, "\tContrast: 0x%02x\n", raw_data
[1]);
1780 hid_debug_event(hdev
, buff
);
1783 /* 2 data bytes with reset duration in ms */
1784 snprintf(buff
, BUFF_SZ
, "out report %s (%d, size=%d)\n",
1785 "REPORT_RESET", report
->id
, raw_size
-1);
1786 hid_debug_event(hdev
, buff
);
1787 snprintf(buff
, BUFF_SZ
, "\tDuration: 0x%02x%02x (%dms)\n",
1788 raw_data
[2], raw_data
[1], raw_data
[2] << 8 | raw_data
[1]);
1789 hid_debug_event(hdev
, buff
);
1791 case REPORT_LCD_CMD
:
1792 /* 63 data bytes with LCD commands */
1793 snprintf(buff
, BUFF_SZ
, "out report %s (%d, size=%d)\n",
1794 "REPORT_LCD_CMD", report
->id
, raw_size
-1);
1795 hid_debug_event(hdev
, buff
);
1796 /* TODO: format decoding */
1798 case REPORT_LCD_DATA
:
1799 /* 63 data bytes with LCD data */
1800 snprintf(buff
, BUFF_SZ
, "out report %s (%d, size=%d)\n",
1801 "REPORT_LCD_CMD", report
->id
, raw_size
-1);
1802 /* TODO: format decoding */
1803 hid_debug_event(hdev
, buff
);
1805 case REPORT_LCD_CMD_DATA
:
1806 /* 63 data bytes with LCD commands and data */
1807 snprintf(buff
, BUFF_SZ
, "out report %s (%d, size=%d)\n",
1808 "REPORT_LCD_CMD", report
->id
, raw_size
-1);
1809 /* TODO: format decoding */
1810 hid_debug_event(hdev
, buff
);
1812 case REPORT_EE_READ
:
1813 /* 3 data bytes with read area description */
1814 snprintf(buff
, BUFF_SZ
, "out report %s (%d, size=%d)\n",
1815 "REPORT_EE_READ", report
->id
, raw_size
-1);
1816 hid_debug_event(hdev
, buff
);
1817 snprintf(buff
, BUFF_SZ
, "\tData address: 0x%02x%02x\n",
1818 raw_data
[2], raw_data
[1]);
1819 hid_debug_event(hdev
, buff
);
1820 snprintf(buff
, BUFF_SZ
, "\tData length: %d\n", raw_data
[3]);
1821 hid_debug_event(hdev
, buff
);
1823 case REPORT_EE_WRITE
:
1824 /* 3+1..20 data bytes with write area description */
1825 snprintf(buff
, BUFF_SZ
, "out report %s (%d, size=%d)\n",
1826 "REPORT_EE_WRITE", report
->id
, raw_size
-1);
1827 hid_debug_event(hdev
, buff
);
1828 snprintf(buff
, BUFF_SZ
, "\tData address: 0x%02x%02x\n",
1829 raw_data
[2], raw_data
[1]);
1830 hid_debug_event(hdev
, buff
);
1831 snprintf(buff
, BUFF_SZ
, "\tData length: %d\n", raw_data
[3]);
1832 hid_debug_event(hdev
, buff
);
1833 if (raw_data
[3] == 0) {
1834 snprintf(buff
, BUFF_SZ
, "\tNo data\n");
1835 } else if (raw_data
[3] + 4 <= raw_size
) {
1836 snprintf(buff
, BUFF_SZ
, "\tData: ");
1837 hid_debug_event(hdev
, buff
);
1838 dump_buff_as_hex(buff
, BUFF_SZ
, raw_data
+4, raw_data
[3]);
1840 snprintf(buff
, BUFF_SZ
, "\tData overflowed\n");
1842 hid_debug_event(hdev
, buff
);
1844 case REPORT_ERASE_MEMORY
:
1845 case REPORT_BL_ERASE_MEMORY
:
1846 /* 3 data bytes with pointer inside erase block */
1847 snprintf(buff
, BUFF_SZ
, "out report %s (%d, size=%d)\n",
1848 "REPORT_ERASE_MEMORY", report
->id
, raw_size
-1);
1849 hid_debug_event(hdev
, buff
);
1850 switch (data
->addr_sz
) {
1852 snprintf(buff
, BUFF_SZ
, "\tAddress inside 64 byte block: 0x%02x%02x\n",
1853 raw_data
[2], raw_data
[1]);
1856 snprintf(buff
, BUFF_SZ
, "\tAddress inside 64 byte block: 0x%02x%02x%02x\n",
1857 raw_data
[3], raw_data
[2], raw_data
[1]);
1860 snprintf(buff
, BUFF_SZ
, "\tNot supported\n");
1862 hid_debug_event(hdev
, buff
);
1864 case REPORT_READ_MEMORY
:
1865 case REPORT_BL_READ_MEMORY
:
1866 /* 4 data bytes with read area description */
1867 snprintf(buff
, BUFF_SZ
, "out report %s (%d, size=%d)\n",
1868 "REPORT_READ_MEMORY", report
->id
, raw_size
-1);
1869 hid_debug_event(hdev
, buff
);
1870 switch (data
->addr_sz
) {
1872 snprintf(buff
, BUFF_SZ
, "\tData address: 0x%02x%02x\n",
1873 raw_data
[2], raw_data
[1]);
1874 hid_debug_event(hdev
, buff
);
1875 snprintf(buff
, BUFF_SZ
, "\tData length: %d\n", raw_data
[3]);
1878 snprintf(buff
, BUFF_SZ
, "\tData address: 0x%02x%02x%02x\n",
1879 raw_data
[3], raw_data
[2], raw_data
[1]);
1880 hid_debug_event(hdev
, buff
);
1881 snprintf(buff
, BUFF_SZ
, "\tData length: %d\n", raw_data
[4]);
1884 snprintf(buff
, BUFF_SZ
, "\tNot supported\n");
1886 hid_debug_event(hdev
, buff
);
1888 case REPORT_WRITE_MEMORY
:
1889 case REPORT_BL_WRITE_MEMORY
:
1890 /* 4+1..32 data bytes with write adrea description */
1891 snprintf(buff
, BUFF_SZ
, "out report %s (%d, size=%d)\n",
1892 "REPORT_WRITE_MEMORY", report
->id
, raw_size
-1);
1893 hid_debug_event(hdev
, buff
);
1894 switch (data
->addr_sz
) {
1896 snprintf(buff
, BUFF_SZ
, "\tData address: 0x%02x%02x\n",
1897 raw_data
[2], raw_data
[1]);
1898 hid_debug_event(hdev
, buff
);
1899 snprintf(buff
, BUFF_SZ
, "\tData length: %d\n", raw_data
[3]);
1900 hid_debug_event(hdev
, buff
);
1901 if (raw_data
[3] == 0) {
1902 snprintf(buff
, BUFF_SZ
, "\tNo data\n");
1903 } else if (raw_data
[3] + 4 <= raw_size
) {
1904 snprintf(buff
, BUFF_SZ
, "\tData: ");
1905 hid_debug_event(hdev
, buff
);
1906 dump_buff_as_hex(buff
, BUFF_SZ
, raw_data
+4, raw_data
[3]);
1908 snprintf(buff
, BUFF_SZ
, "\tData overflowed\n");
1912 snprintf(buff
, BUFF_SZ
, "\tData address: 0x%02x%02x%02x\n",
1913 raw_data
[3], raw_data
[2], raw_data
[1]);
1914 hid_debug_event(hdev
, buff
);
1915 snprintf(buff
, BUFF_SZ
, "\tData length: %d\n", raw_data
[4]);
1916 hid_debug_event(hdev
, buff
);
1917 if (raw_data
[4] == 0) {
1918 snprintf(buff
, BUFF_SZ
, "\tNo data\n");
1919 } else if (raw_data
[4] + 5 <= raw_size
) {
1920 snprintf(buff
, BUFF_SZ
, "\tData: ");
1921 hid_debug_event(hdev
, buff
);
1922 dump_buff_as_hex(buff
, BUFF_SZ
, raw_data
+5, raw_data
[4]);
1924 snprintf(buff
, BUFF_SZ
, "\tData overflowed\n");
1928 snprintf(buff
, BUFF_SZ
, "\tNot supported\n");
1930 hid_debug_event(hdev
, buff
);
1932 case REPORT_SPLASH_RESTART
:
1935 case REPORT_EXIT_KEYBOARD
:
1936 snprintf(buff
, BUFF_SZ
, "out report %s (%d, size=%d)\n",
1937 "REPORT_EXIT_KEYBOARD", report
->id
, raw_size
-1);
1938 hid_debug_event(hdev
, buff
);
1939 snprintf(buff
, BUFF_SZ
, "\tRestart delay: %dms (0x%02x%02x)\n",
1940 raw_data
[1] | (raw_data
[2] << 8),
1941 raw_data
[2], raw_data
[1]);
1942 hid_debug_event(hdev
, buff
);
1944 case REPORT_VERSION
:
1945 snprintf(buff
, BUFF_SZ
, "out report %s (%d, size=%d)\n",
1946 "REPORT_VERSION", report
->id
, raw_size
-1);
1947 hid_debug_event(hdev
, buff
);
1950 snprintf(buff
, BUFF_SZ
, "out report %s (%d, size=%d)\n",
1951 "REPORT_DEVID", report
->id
, raw_size
-1);
1952 hid_debug_event(hdev
, buff
);
1954 case REPORT_SPLASH_SIZE
:
1955 snprintf(buff
, BUFF_SZ
, "out report %s (%d, size=%d)\n",
1956 "REPORT_SPLASH_SIZE", report
->id
, raw_size
-1);
1957 hid_debug_event(hdev
, buff
);
1959 case REPORT_HOOK_VERSION
:
1960 snprintf(buff
, BUFF_SZ
, "out report %s (%d, size=%d)\n",
1961 "REPORT_HOOK_VERSION", report
->id
, raw_size
-1);
1962 hid_debug_event(hdev
, buff
);
1964 case REPORT_EXIT_FLASHER
:
1965 snprintf(buff
, BUFF_SZ
, "out report %s (%d, size=%d)\n",
1966 "REPORT_VERSION", report
->id
, raw_size
-1);
1967 hid_debug_event(hdev
, buff
);
1968 snprintf(buff
, BUFF_SZ
, "\tRestart delay: %dms (0x%02x%02x)\n",
1969 raw_data
[1] | (raw_data
[2] << 8),
1970 raw_data
[2], raw_data
[1]);
1971 hid_debug_event(hdev
, buff
);
1974 snprintf(buff
, BUFF_SZ
, "out report %s (%d, size=%d)\n",
1975 "<unknown>", report
->id
, raw_size
-1);
1976 hid_debug_event(hdev
, buff
);
1979 wake_up_interruptible(&hdev
->debug_wait
);
1983 static void picolcd_debug_raw_event(struct picolcd_data
*data
,
1984 struct hid_device
*hdev
, struct hid_report
*report
,
1985 u8
*raw_data
, int size
)
1990 /* Avoid unnecessary overhead if debugfs is disabled */
1991 if (!hdev
->debug_events
)
1994 buff
= kmalloc(BUFF_SZ
, GFP_ATOMIC
);
1998 switch (report
->id
) {
1999 case REPORT_ERROR_CODE
:
2000 /* 2 data bytes with affected report and error code */
2001 snprintf(buff
, BUFF_SZ
, "report %s (%d, size=%d)\n",
2002 "REPORT_ERROR_CODE", report
->id
, size
-1);
2003 hid_debug_event(hdev
, buff
);
2004 if (raw_data
[2] < ARRAY_SIZE(error_codes
))
2005 snprintf(buff
, BUFF_SZ
, "\tError code 0x%02x (%s) in reply to report 0x%02x\n",
2006 raw_data
[2], error_codes
[raw_data
[2]], raw_data
[1]);
2008 snprintf(buff
, BUFF_SZ
, "\tError code 0x%02x in reply to report 0x%02x\n",
2009 raw_data
[2], raw_data
[1]);
2010 hid_debug_event(hdev
, buff
);
2012 case REPORT_KEY_STATE
:
2013 /* 2 data bytes with key state */
2014 snprintf(buff
, BUFF_SZ
, "report %s (%d, size=%d)\n",
2015 "REPORT_KEY_STATE", report
->id
, size
-1);
2016 hid_debug_event(hdev
, buff
);
2017 if (raw_data
[1] == 0)
2018 snprintf(buff
, BUFF_SZ
, "\tNo key pressed\n");
2019 else if (raw_data
[2] == 0)
2020 snprintf(buff
, BUFF_SZ
, "\tOne key pressed: 0x%02x (%d)\n",
2021 raw_data
[1], raw_data
[1]);
2023 snprintf(buff
, BUFF_SZ
, "\tTwo keys pressed: 0x%02x (%d), 0x%02x (%d)\n",
2024 raw_data
[1], raw_data
[1], raw_data
[2], raw_data
[2]);
2025 hid_debug_event(hdev
, buff
);
2027 case REPORT_IR_DATA
:
2028 /* Up to 20 byes of IR scancode data */
2029 snprintf(buff
, BUFF_SZ
, "report %s (%d, size=%d)\n",
2030 "REPORT_IR_DATA", report
->id
, size
-1);
2031 hid_debug_event(hdev
, buff
);
2032 if (raw_data
[1] == 0) {
2033 snprintf(buff
, BUFF_SZ
, "\tUnexpectedly 0 data length\n");
2034 hid_debug_event(hdev
, buff
);
2035 } else if (raw_data
[1] + 1 <= size
) {
2036 snprintf(buff
, BUFF_SZ
, "\tData length: %d\n\tIR Data: ",
2038 hid_debug_event(hdev
, buff
);
2039 dump_buff_as_hex(buff
, BUFF_SZ
, raw_data
+2, raw_data
[1]-1);
2040 hid_debug_event(hdev
, buff
);
2042 snprintf(buff
, BUFF_SZ
, "\tOverflowing data length: %d\n",
2044 hid_debug_event(hdev
, buff
);
2047 case REPORT_EE_DATA
:
2048 /* Data buffer in response to REPORT_EE_READ or REPORT_EE_WRITE */
2049 snprintf(buff
, BUFF_SZ
, "report %s (%d, size=%d)\n",
2050 "REPORT_EE_DATA", report
->id
, size
-1);
2051 hid_debug_event(hdev
, buff
);
2052 snprintf(buff
, BUFF_SZ
, "\tData address: 0x%02x%02x\n",
2053 raw_data
[2], raw_data
[1]);
2054 hid_debug_event(hdev
, buff
);
2055 snprintf(buff
, BUFF_SZ
, "\tData length: %d\n", raw_data
[3]);
2056 hid_debug_event(hdev
, buff
);
2057 if (raw_data
[3] == 0) {
2058 snprintf(buff
, BUFF_SZ
, "\tNo data\n");
2059 hid_debug_event(hdev
, buff
);
2060 } else if (raw_data
[3] + 4 <= size
) {
2061 snprintf(buff
, BUFF_SZ
, "\tData: ");
2062 hid_debug_event(hdev
, buff
);
2063 dump_buff_as_hex(buff
, BUFF_SZ
, raw_data
+4, raw_data
[3]);
2064 hid_debug_event(hdev
, buff
);
2066 snprintf(buff
, BUFF_SZ
, "\tData overflowed\n");
2067 hid_debug_event(hdev
, buff
);
2071 /* Data buffer in response to REPORT_READ_MEMORY or REPORT_WRTIE_MEMORY */
2072 snprintf(buff
, BUFF_SZ
, "report %s (%d, size=%d)\n",
2073 "REPORT_MEMORY", report
->id
, size
-1);
2074 hid_debug_event(hdev
, buff
);
2075 switch (data
->addr_sz
) {
2077 snprintf(buff
, BUFF_SZ
, "\tData address: 0x%02x%02x\n",
2078 raw_data
[2], raw_data
[1]);
2079 hid_debug_event(hdev
, buff
);
2080 snprintf(buff
, BUFF_SZ
, "\tData length: %d\n", raw_data
[3]);
2081 hid_debug_event(hdev
, buff
);
2082 if (raw_data
[3] == 0) {
2083 snprintf(buff
, BUFF_SZ
, "\tNo data\n");
2084 } else if (raw_data
[3] + 4 <= size
) {
2085 snprintf(buff
, BUFF_SZ
, "\tData: ");
2086 hid_debug_event(hdev
, buff
);
2087 dump_buff_as_hex(buff
, BUFF_SZ
, raw_data
+4, raw_data
[3]);
2089 snprintf(buff
, BUFF_SZ
, "\tData overflowed\n");
2093 snprintf(buff
, BUFF_SZ
, "\tData address: 0x%02x%02x%02x\n",
2094 raw_data
[3], raw_data
[2], raw_data
[1]);
2095 hid_debug_event(hdev
, buff
);
2096 snprintf(buff
, BUFF_SZ
, "\tData length: %d\n", raw_data
[4]);
2097 hid_debug_event(hdev
, buff
);
2098 if (raw_data
[4] == 0) {
2099 snprintf(buff
, BUFF_SZ
, "\tNo data\n");
2100 } else if (raw_data
[4] + 5 <= size
) {
2101 snprintf(buff
, BUFF_SZ
, "\tData: ");
2102 hid_debug_event(hdev
, buff
);
2103 dump_buff_as_hex(buff
, BUFF_SZ
, raw_data
+5, raw_data
[4]);
2105 snprintf(buff
, BUFF_SZ
, "\tData overflowed\n");
2109 snprintf(buff
, BUFF_SZ
, "\tNot supported\n");
2111 hid_debug_event(hdev
, buff
);
2113 case REPORT_VERSION
:
2114 snprintf(buff
, BUFF_SZ
, "report %s (%d, size=%d)\n",
2115 "REPORT_VERSION", report
->id
, size
-1);
2116 hid_debug_event(hdev
, buff
);
2117 snprintf(buff
, BUFF_SZ
, "\tFirmware version: %d.%d\n",
2118 raw_data
[2], raw_data
[1]);
2119 hid_debug_event(hdev
, buff
);
2121 case REPORT_BL_ERASE_MEMORY
:
2122 snprintf(buff
, BUFF_SZ
, "report %s (%d, size=%d)\n",
2123 "REPORT_BL_ERASE_MEMORY", report
->id
, size
-1);
2124 hid_debug_event(hdev
, buff
);
2127 case REPORT_BL_READ_MEMORY
:
2128 snprintf(buff
, BUFF_SZ
, "report %s (%d, size=%d)\n",
2129 "REPORT_BL_READ_MEMORY", report
->id
, size
-1);
2130 hid_debug_event(hdev
, buff
);
2133 case REPORT_BL_WRITE_MEMORY
:
2134 snprintf(buff
, BUFF_SZ
, "report %s (%d, size=%d)\n",
2135 "REPORT_BL_WRITE_MEMORY", report
->id
, size
-1);
2136 hid_debug_event(hdev
, buff
);
2140 snprintf(buff
, BUFF_SZ
, "report %s (%d, size=%d)\n",
2141 "REPORT_DEVID", report
->id
, size
-1);
2142 hid_debug_event(hdev
, buff
);
2143 snprintf(buff
, BUFF_SZ
, "\tSerial: 0x%02x%02x%02x%02x\n",
2144 raw_data
[1], raw_data
[2], raw_data
[3], raw_data
[4]);
2145 hid_debug_event(hdev
, buff
);
2146 snprintf(buff
, BUFF_SZ
, "\tType: 0x%02x\n",
2148 hid_debug_event(hdev
, buff
);
2150 case REPORT_SPLASH_SIZE
:
2151 snprintf(buff
, BUFF_SZ
, "report %s (%d, size=%d)\n",
2152 "REPORT_SPLASH_SIZE", report
->id
, size
-1);
2153 hid_debug_event(hdev
, buff
);
2154 snprintf(buff
, BUFF_SZ
, "\tTotal splash space: %d\n",
2155 (raw_data
[2] << 8) | raw_data
[1]);
2156 hid_debug_event(hdev
, buff
);
2157 snprintf(buff
, BUFF_SZ
, "\tUsed splash space: %d\n",
2158 (raw_data
[4] << 8) | raw_data
[3]);
2159 hid_debug_event(hdev
, buff
);
2161 case REPORT_HOOK_VERSION
:
2162 snprintf(buff
, BUFF_SZ
, "report %s (%d, size=%d)\n",
2163 "REPORT_HOOK_VERSION", report
->id
, size
-1);
2164 hid_debug_event(hdev
, buff
);
2165 snprintf(buff
, BUFF_SZ
, "\tFirmware version: %d.%d\n",
2166 raw_data
[1], raw_data
[2]);
2167 hid_debug_event(hdev
, buff
);
2170 snprintf(buff
, BUFF_SZ
, "report %s (%d, size=%d)\n",
2171 "<unknown>", report
->id
, size
-1);
2172 hid_debug_event(hdev
, buff
);
2175 wake_up_interruptible(&hdev
->debug_wait
);
2179 static void picolcd_init_devfs(struct picolcd_data
*data
,
2180 struct hid_report
*eeprom_r
, struct hid_report
*eeprom_w
,
2181 struct hid_report
*flash_r
, struct hid_report
*flash_w
,
2182 struct hid_report
*reset
)
2184 struct hid_device
*hdev
= data
->hdev
;
2186 mutex_init(&data
->mutex_flash
);
2190 data
->debug_reset
= debugfs_create_file("reset", 0600,
2191 hdev
->debug_dir
, data
, &picolcd_debug_reset_fops
);
2194 if (eeprom_r
|| eeprom_w
)
2195 data
->debug_eeprom
= debugfs_create_file("eeprom",
2196 (eeprom_w
? S_IWUSR
: 0) | (eeprom_r
? S_IRUSR
: 0),
2197 hdev
->debug_dir
, data
, &picolcd_debug_eeprom_fops
);
2200 if (flash_r
&& flash_r
->maxfield
== 1 && flash_r
->field
[0]->report_size
== 8)
2201 data
->addr_sz
= flash_r
->field
[0]->report_count
- 1;
2204 if (data
->addr_sz
== 2 || data
->addr_sz
== 3) {
2205 data
->debug_flash
= debugfs_create_file("flash",
2206 (flash_w
? S_IWUSR
: 0) | (flash_r
? S_IRUSR
: 0),
2207 hdev
->debug_dir
, data
, &picolcd_debug_flash_fops
);
2208 } else if (flash_r
|| flash_w
)
2209 dev_warn(&hdev
->dev
, "Unexpected FLASH access reports, "
2210 "please submit rdesc for review\n");
2213 static void picolcd_exit_devfs(struct picolcd_data
*data
)
2215 struct dentry
*dent
;
2217 dent
= data
->debug_reset
;
2218 data
->debug_reset
= NULL
;
2220 debugfs_remove(dent
);
2221 dent
= data
->debug_eeprom
;
2222 data
->debug_eeprom
= NULL
;
2224 debugfs_remove(dent
);
2225 dent
= data
->debug_flash
;
2226 data
->debug_flash
= NULL
;
2228 debugfs_remove(dent
);
2229 mutex_destroy(&data
->mutex_flash
);
2232 static inline void picolcd_debug_raw_event(struct picolcd_data
*data
,
2233 struct hid_device
*hdev
, struct hid_report
*report
,
2234 u8
*raw_data
, int size
)
2237 static inline void picolcd_init_devfs(struct picolcd_data
*data
,
2238 struct hid_report
*eeprom_r
, struct hid_report
*eeprom_w
,
2239 struct hid_report
*flash_r
, struct hid_report
*flash_w
,
2240 struct hid_report
*reset
)
2243 static inline void picolcd_exit_devfs(struct picolcd_data
*data
)
2246 #endif /* CONFIG_DEBUG_FS */
2249 * Handle raw report as sent by device
2251 static int picolcd_raw_event(struct hid_device
*hdev
,
2252 struct hid_report
*report
, u8
*raw_data
, int size
)
2254 struct picolcd_data
*data
= hid_get_drvdata(hdev
);
2255 unsigned long flags
;
2261 if (report
->id
== REPORT_KEY_STATE
) {
2262 if (data
->input_keys
)
2263 ret
= picolcd_raw_keypad(data
, report
, raw_data
+1, size
-1);
2264 } else if (report
->id
== REPORT_IR_DATA
) {
2265 if (data
->input_cir
)
2266 ret
= picolcd_raw_cir(data
, report
, raw_data
+1, size
-1);
2268 spin_lock_irqsave(&data
->lock
, flags
);
2270 * We let the caller of picolcd_send_and_wait() check if the
2271 * report we got is one of the expected ones or not.
2273 if (data
->pending
) {
2274 memcpy(data
->pending
->raw_data
, raw_data
+1, size
-1);
2275 data
->pending
->raw_size
= size
-1;
2276 data
->pending
->in_report
= report
;
2277 complete(&data
->pending
->ready
);
2279 spin_unlock_irqrestore(&data
->lock
, flags
);
2282 picolcd_debug_raw_event(data
, hdev
, report
, raw_data
, size
);
2287 static int picolcd_suspend(struct hid_device
*hdev
, pm_message_t message
)
2289 if (message
.event
& PM_EVENT_AUTO
)
2292 picolcd_suspend_backlight(hid_get_drvdata(hdev
));
2293 dbg_hid(PICOLCD_NAME
" device ready for suspend\n");
2297 static int picolcd_resume(struct hid_device
*hdev
)
2300 ret
= picolcd_resume_backlight(hid_get_drvdata(hdev
));
2302 dbg_hid(PICOLCD_NAME
" restoring backlight failed: %d\n", ret
);
2306 static int picolcd_reset_resume(struct hid_device
*hdev
)
2309 ret
= picolcd_reset(hdev
);
2311 dbg_hid(PICOLCD_NAME
" resetting our device failed: %d\n", ret
);
2312 ret
= picolcd_fb_reset(hid_get_drvdata(hdev
), 0);
2314 dbg_hid(PICOLCD_NAME
" restoring framebuffer content failed: %d\n", ret
);
2315 ret
= picolcd_resume_lcd(hid_get_drvdata(hdev
));
2317 dbg_hid(PICOLCD_NAME
" restoring lcd failed: %d\n", ret
);
2318 ret
= picolcd_resume_backlight(hid_get_drvdata(hdev
));
2320 dbg_hid(PICOLCD_NAME
" restoring backlight failed: %d\n", ret
);
2321 picolcd_leds_set(hid_get_drvdata(hdev
));
2326 /* initialize keypad input device */
2327 static int picolcd_init_keys(struct picolcd_data
*data
,
2328 struct hid_report
*report
)
2330 struct hid_device
*hdev
= data
->hdev
;
2331 struct input_dev
*idev
;
2336 if (report
->maxfield
!= 1 || report
->field
[0]->report_count
!= 2 ||
2337 report
->field
[0]->report_size
!= 8) {
2338 dev_err(&hdev
->dev
, "unsupported KEY_STATE report");
2342 idev
= input_allocate_device();
2344 dev_err(&hdev
->dev
, "failed to allocate input device");
2347 input_set_drvdata(idev
, hdev
);
2348 memcpy(data
->keycode
, def_keymap
, sizeof(def_keymap
));
2349 idev
->name
= hdev
->name
;
2350 idev
->phys
= hdev
->phys
;
2351 idev
->uniq
= hdev
->uniq
;
2352 idev
->id
.bustype
= hdev
->bus
;
2353 idev
->id
.vendor
= hdev
->vendor
;
2354 idev
->id
.product
= hdev
->product
;
2355 idev
->id
.version
= hdev
->version
;
2356 idev
->dev
.parent
= hdev
->dev
.parent
;
2357 idev
->keycode
= &data
->keycode
;
2358 idev
->keycodemax
= PICOLCD_KEYS
;
2359 idev
->keycodesize
= sizeof(data
->keycode
[0]);
2360 input_set_capability(idev
, EV_MSC
, MSC_SCAN
);
2361 set_bit(EV_REP
, idev
->evbit
);
2362 for (i
= 0; i
< PICOLCD_KEYS
; i
++)
2363 input_set_capability(idev
, EV_KEY
, data
->keycode
[i
]);
2364 error
= input_register_device(idev
);
2366 dev_err(&hdev
->dev
, "error registering the input device");
2367 input_free_device(idev
);
2370 data
->input_keys
= idev
;
2374 static void picolcd_exit_keys(struct picolcd_data
*data
)
2376 struct input_dev
*idev
= data
->input_keys
;
2378 data
->input_keys
= NULL
;
2380 input_unregister_device(idev
);
2383 /* initialize CIR input device */
2384 static inline int picolcd_init_cir(struct picolcd_data
*data
, struct hid_report
*report
)
2386 /* support not implemented yet */
2390 static inline void picolcd_exit_cir(struct picolcd_data
*data
)
2394 static int picolcd_probe_lcd(struct hid_device
*hdev
, struct picolcd_data
*data
)
2398 error
= picolcd_check_version(hdev
);
2402 if (data
->version
[0] != 0 && data
->version
[1] != 3)
2403 dev_info(&hdev
->dev
, "Device with untested firmware revision, "
2404 "please submit /sys/kernel/debug/hid/%s/rdesc for this device.\n",
2405 dev_name(&hdev
->dev
));
2407 /* Setup keypad input device */
2408 error
= picolcd_init_keys(data
, picolcd_in_report(REPORT_KEY_STATE
, hdev
));
2412 /* Setup CIR input device */
2413 error
= picolcd_init_cir(data
, picolcd_in_report(REPORT_IR_DATA
, hdev
));
2417 /* Set up the framebuffer device */
2418 error
= picolcd_init_framebuffer(data
);
2422 /* Setup lcd class device */
2423 error
= picolcd_init_lcd(data
, picolcd_out_report(REPORT_CONTRAST
, hdev
));
2427 /* Setup backlight class device */
2428 error
= picolcd_init_backlight(data
, picolcd_out_report(REPORT_BRIGHTNESS
, hdev
));
2432 /* Setup the LED class devices */
2433 error
= picolcd_init_leds(data
, picolcd_out_report(REPORT_LED_STATE
, hdev
));
2437 picolcd_init_devfs(data
, picolcd_out_report(REPORT_EE_READ
, hdev
),
2438 picolcd_out_report(REPORT_EE_WRITE
, hdev
),
2439 picolcd_out_report(REPORT_READ_MEMORY
, hdev
),
2440 picolcd_out_report(REPORT_WRITE_MEMORY
, hdev
),
2441 picolcd_out_report(REPORT_RESET
, hdev
));
2444 picolcd_exit_leds(data
);
2445 picolcd_exit_backlight(data
);
2446 picolcd_exit_lcd(data
);
2447 picolcd_exit_framebuffer(data
);
2448 picolcd_exit_cir(data
);
2449 picolcd_exit_keys(data
);
2453 static int picolcd_probe_bootloader(struct hid_device
*hdev
, struct picolcd_data
*data
)
2457 error
= picolcd_check_version(hdev
);
2461 if (data
->version
[0] != 1 && data
->version
[1] != 0)
2462 dev_info(&hdev
->dev
, "Device with untested bootloader revision, "
2463 "please submit /sys/kernel/debug/hid/%s/rdesc for this device.\n",
2464 dev_name(&hdev
->dev
));
2466 picolcd_init_devfs(data
, NULL
, NULL
,
2467 picolcd_out_report(REPORT_BL_READ_MEMORY
, hdev
),
2468 picolcd_out_report(REPORT_BL_WRITE_MEMORY
, hdev
), NULL
);
2472 static int picolcd_probe(struct hid_device
*hdev
,
2473 const struct hid_device_id
*id
)
2475 struct picolcd_data
*data
;
2476 int error
= -ENOMEM
;
2478 dbg_hid(PICOLCD_NAME
" hardware probe...\n");
2481 * Let's allocate the picolcd data structure, set some reasonable
2482 * defaults, and associate it with the device
2484 data
= kzalloc(sizeof(struct picolcd_data
), GFP_KERNEL
);
2486 dev_err(&hdev
->dev
, "can't allocate space for Minibox PicoLCD device data\n");
2488 goto err_no_cleanup
;
2491 spin_lock_init(&data
->lock
);
2492 mutex_init(&data
->mutex
);
2494 data
->opmode_delay
= 5000;
2495 if (hdev
->product
== USB_DEVICE_ID_PICOLCD_BOOTLOADER
)
2496 data
->status
|= PICOLCD_BOOTLOADER
;
2497 hid_set_drvdata(hdev
, data
);
2499 /* Parse the device reports and start it up */
2500 error
= hid_parse(hdev
);
2502 dev_err(&hdev
->dev
, "device report parse failed\n");
2503 goto err_cleanup_data
;
2506 /* We don't use hidinput but hid_hw_start() fails if nothing is
2507 * claimed. So spoof claimed input. */
2508 hdev
->claimed
= HID_CLAIMED_INPUT
;
2509 error
= hid_hw_start(hdev
, 0);
2512 dev_err(&hdev
->dev
, "hardware start failed\n");
2513 goto err_cleanup_data
;
2516 error
= hdev
->ll_driver
->open(hdev
);
2518 dev_err(&hdev
->dev
, "failed to open input interrupt pipe for key and IR events\n");
2519 goto err_cleanup_hid_hw
;
2522 error
= device_create_file(&hdev
->dev
, &dev_attr_operation_mode_delay
);
2524 dev_err(&hdev
->dev
, "failed to create sysfs attributes\n");
2525 goto err_cleanup_hid_ll
;
2528 error
= device_create_file(&hdev
->dev
, &dev_attr_operation_mode
);
2530 dev_err(&hdev
->dev
, "failed to create sysfs attributes\n");
2531 goto err_cleanup_sysfs1
;
2534 if (data
->status
& PICOLCD_BOOTLOADER
)
2535 error
= picolcd_probe_bootloader(hdev
, data
);
2537 error
= picolcd_probe_lcd(hdev
, data
);
2539 goto err_cleanup_sysfs2
;
2541 dbg_hid(PICOLCD_NAME
" activated and initialized\n");
2545 device_remove_file(&hdev
->dev
, &dev_attr_operation_mode
);
2547 device_remove_file(&hdev
->dev
, &dev_attr_operation_mode_delay
);
2549 hdev
->ll_driver
->close(hdev
);
2555 hid_set_drvdata(hdev
, NULL
);
2560 static void picolcd_remove(struct hid_device
*hdev
)
2562 struct picolcd_data
*data
= hid_get_drvdata(hdev
);
2563 unsigned long flags
;
2565 dbg_hid(PICOLCD_NAME
" hardware remove...\n");
2566 spin_lock_irqsave(&data
->lock
, flags
);
2567 data
->status
|= PICOLCD_FAILED
;
2568 spin_unlock_irqrestore(&data
->lock
, flags
);
2570 picolcd_exit_devfs(data
);
2571 device_remove_file(&hdev
->dev
, &dev_attr_operation_mode
);
2572 device_remove_file(&hdev
->dev
, &dev_attr_operation_mode_delay
);
2573 hdev
->ll_driver
->close(hdev
);
2575 hid_set_drvdata(hdev
, NULL
);
2577 /* Shortcut potential pending reply that will never arrive */
2578 spin_lock_irqsave(&data
->lock
, flags
);
2580 complete(&data
->pending
->ready
);
2581 spin_unlock_irqrestore(&data
->lock
, flags
);
2584 picolcd_exit_leds(data
);
2585 /* Clean up the framebuffer */
2586 picolcd_exit_backlight(data
);
2587 picolcd_exit_lcd(data
);
2588 picolcd_exit_framebuffer(data
);
2590 picolcd_exit_cir(data
);
2591 picolcd_exit_keys(data
);
2593 mutex_destroy(&data
->mutex
);
2594 /* Finally, clean up the picolcd data itself */
2598 static const struct hid_device_id picolcd_devices
[] = {
2599 { HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP
, USB_DEVICE_ID_PICOLCD
) },
2600 { HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP
, USB_DEVICE_ID_PICOLCD_BOOTLOADER
) },
2603 MODULE_DEVICE_TABLE(hid
, picolcd_devices
);
2605 static struct hid_driver picolcd_driver
= {
2606 .name
= "hid-picolcd",
2607 .id_table
= picolcd_devices
,
2608 .probe
= picolcd_probe
,
2609 .remove
= picolcd_remove
,
2610 .raw_event
= picolcd_raw_event
,
2612 .suspend
= picolcd_suspend
,
2613 .resume
= picolcd_resume
,
2614 .reset_resume
= picolcd_reset_resume
,
2618 static int __init
picolcd_init(void)
2620 return hid_register_driver(&picolcd_driver
);
2623 static void __exit
picolcd_exit(void)
2625 hid_unregister_driver(&picolcd_driver
);
2628 module_init(picolcd_init
);
2629 module_exit(picolcd_exit
);
2630 MODULE_DESCRIPTION("Minibox graphics PicoLCD Driver");
2631 MODULE_LICENSE("GPL v2");