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 n_fb = vmalloc(PICOLCDFB_SIZE*info->var.bits_per_pixel);
578 fb_deferred_io_cleanup(info);
579 /* translate FB content to new bits-per-pixel */
580 if (info->var.bits_per_pixel == 1) {
582 for (i = 0; i < PICOLCDFB_SIZE; i++) {
584 for (b = 0; b < 8; b++) {
586 p |= o_fb[i*8+b] ? 0x01 : 0x00;
589 info->fix.visual = FB_VISUAL_MONO01;
590 info->fix.line_length = PICOLCDFB_WIDTH / 8;
593 for (i = 0; i < PICOLCDFB_SIZE * 8; i++)
594 n_fb[i] = o_fb[i/8] & (0x01 << (7 - i % 8)) ? 0xff : 0x00;
595 info->fix.visual = FB_VISUAL_TRUECOLOR;
596 info->fix.line_length = PICOLCDFB_WIDTH;
599 data->fb_bitmap = n_fb;
600 data->fb_bpp = info->var.bits_per_pixel;
601 info->screen_base = (char __force __iomem *)n_fb;
602 info->fix.smem_start = (unsigned long)n_fb;
603 info->fix.smem_len = PICOLCDFB_SIZE*data->fb_bpp;
604 fb_deferred_io_init(info);
609 /* Note this can't be const because of struct fb_info definition */
610 static struct fb_ops picolcdfb_ops = {
611 .owner = THIS_MODULE,
612 .fb_destroy = picolcd_fb_destroy,
613 .fb_read = fb_sys_read,
614 .fb_write = picolcd_fb_write,
615 .fb_blank = picolcd_fb_blank,
616 .fb_fillrect = picolcd_fb_fillrect,
617 .fb_copyarea = picolcd_fb_copyarea,
618 .fb_imageblit = picolcd_fb_imageblit,
619 .fb_check_var = picolcd_fb_check_var,
620 .fb_set_par = picolcd_set_par,
624 /* Callback from deferred IO workqueue */
625 static void picolcd_fb_deferred_io(struct fb_info *info, struct list_head *pagelist)
627 picolcd_fb_update(info->par);
630 static const struct fb_deferred_io picolcd_fb_defio = {
631 .delay = HZ / PICOLCDFB_UPDATE_RATE_DEFAULT,
632 .deferred_io = picolcd_fb_deferred_io,
637 * The "fb_update_rate" sysfs attribute
639 static ssize_t picolcd_fb_update_rate_show(struct device *dev,
640 struct device_attribute *attr, char *buf)
642 struct picolcd_data *data = dev_get_drvdata(dev);
643 unsigned i, fb_update_rate = data->fb_update_rate;
646 for (i = 1; i <= PICOLCDFB_UPDATE_RATE_LIMIT; i++)
647 if (ret >= PAGE_SIZE)
649 else if (i == fb_update_rate)
650 ret += snprintf(buf+ret, PAGE_SIZE-ret, "[%u] ", i);
652 ret += snprintf(buf+ret, PAGE_SIZE-ret, "%u ", i);
654 buf[min(ret, (size_t)PAGE_SIZE)-1] = '\n';
658 static ssize_t picolcd_fb_update_rate_store(struct device *dev,
659 struct device_attribute *attr, const char *buf, size_t count)
661 struct picolcd_data *data = dev_get_drvdata(dev);
665 if (count < 1 || count > 10)
668 i = sscanf(buf, "%u", &u);
672 if (u > PICOLCDFB_UPDATE_RATE_LIMIT)
675 u = PICOLCDFB_UPDATE_RATE_DEFAULT;
677 data->fb_update_rate = u;
678 data->fb_defio.delay = HZ / data->fb_update_rate;
682 static DEVICE_ATTR(fb_update_rate, 0666, picolcd_fb_update_rate_show,
683 picolcd_fb_update_rate_store);
685 /* initialize Framebuffer device */
686 static int picolcd_init_framebuffer(struct picolcd_data *data)
688 struct device *dev = &data->hdev->dev;
689 struct fb_info *info = NULL;
690 int i, error = -ENOMEM;
691 u8 *fb_vbitmap = NULL;
692 u8 *fb_bitmap = NULL;
695 fb_bitmap = vmalloc(PICOLCDFB_SIZE*picolcdfb_var.bits_per_pixel);
696 if (fb_bitmap == NULL) {
697 dev_err(dev, "can't get a free page for framebuffer\n");
701 fb_vbitmap = kmalloc(PICOLCDFB_SIZE, GFP_KERNEL);
702 if (fb_vbitmap == NULL) {
703 dev_err(dev, "can't alloc vbitmap image buffer\n");
707 data->fb_update_rate = PICOLCDFB_UPDATE_RATE_DEFAULT;
708 data->fb_defio = picolcd_fb_defio;
709 /* The extra memory is:
710 * - struct picolcd_fb_cleanup_item
711 * - u32 for ref_count
712 * - 256*u32 for pseudo_palette
714 info = framebuffer_alloc(257 * sizeof(u32), dev);
716 dev_err(dev, "failed to allocate a framebuffer\n");
723 for (i = 0; i < 256; i++)
724 palette[i] = i > 0 && i < 16 ? 0xff : 0;
725 info->pseudo_palette = palette;
726 info->fbdefio = &data->fb_defio;
727 info->screen_base = (char __force __iomem *)fb_bitmap;
728 info->fbops = &picolcdfb_ops;
729 info->var = picolcdfb_var;
730 info->fix = picolcdfb_fix;
731 info->fix.smem_len = PICOLCDFB_SIZE;
732 info->fix.smem_start = (unsigned long)fb_bitmap;
734 info->flags = FBINFO_FLAG_DEFAULT;
736 data->fb_vbitmap = fb_vbitmap;
737 data->fb_bitmap = fb_bitmap;
738 data->fb_bpp = picolcdfb_var.bits_per_pixel;
739 error = picolcd_fb_reset(data, 1);
741 dev_err(dev, "failed to configure display\n");
744 error = device_create_file(dev, &dev_attr_fb_update_rate);
746 dev_err(dev, "failed to create sysfs attributes\n");
749 fb_deferred_io_init(info);
750 data->fb_info = info;
751 error = register_framebuffer(info);
753 dev_err(dev, "failed to register framebuffer\n");
756 /* schedule first output of framebuffer */
758 schedule_delayed_work(&info->deferred_work, 0);
762 fb_deferred_io_cleanup(info);
763 device_remove_file(dev, &dev_attr_fb_update_rate);
765 data->fb_vbitmap = NULL;
766 data->fb_bitmap = NULL;
768 data->fb_info = NULL;
771 framebuffer_release(info);
777 static void picolcd_exit_framebuffer(struct picolcd_data *data)
779 struct fb_info *info = data->fb_info;
780 u8 *fb_vbitmap = data->fb_vbitmap;
781 u8 *fb_bitmap = data->fb_bitmap;
786 data->fb_vbitmap = NULL;
787 data->fb_bitmap = NULL;
789 data->fb_info = NULL;
790 device_remove_file(&data->hdev->dev, &dev_attr_fb_update_rate);
791 unregister_framebuffer(info);
796 #define picolcd_fbinfo(d) ((d)->fb_info)
798 static inline int picolcd_fb_reset(struct picolcd_data *data, int clear)
802 static inline int picolcd_init_framebuffer(struct picolcd_data *data)
806 static inline void picolcd_exit_framebuffer(struct picolcd_data *data)
809 #define picolcd_fbinfo(d) NULL
810 #endif /* CONFIG_HID_PICOLCD_FB */
812 #ifdef CONFIG_HID_PICOLCD_BACKLIGHT
814 * backlight class device
816 static int picolcd_get_brightness(struct backlight_device *bdev)
818 struct picolcd_data *data = bl_get_data(bdev);
819 return data->lcd_brightness;
822 static int picolcd_set_brightness(struct backlight_device *bdev)
824 struct picolcd_data *data = bl_get_data(bdev);
825 struct hid_report *report = picolcd_out_report(REPORT_BRIGHTNESS, data->hdev);
828 if (!report || report->maxfield != 1 || report->field[0]->report_count != 1)
831 data->lcd_brightness = bdev->props.brightness & 0x0ff;
832 data->lcd_power = bdev->props.power;
833 spin_lock_irqsave(&data->lock, flags);
834 hid_set_field(report->field[0], 0, data->lcd_power == FB_BLANK_UNBLANK ? data->lcd_brightness : 0);
835 usbhid_submit_report(data->hdev, report, USB_DIR_OUT);
836 spin_unlock_irqrestore(&data->lock, flags);
840 static int picolcd_check_bl_fb(struct backlight_device *bdev, struct fb_info *fb)
842 return fb && fb == picolcd_fbinfo((struct picolcd_data *)bl_get_data(bdev));
845 static const struct backlight_ops picolcd_blops = {
846 .update_status = picolcd_set_brightness,
847 .get_brightness = picolcd_get_brightness,
848 .check_fb = picolcd_check_bl_fb,
851 static int picolcd_init_backlight(struct picolcd_data *data, struct hid_report *report)
853 struct device *dev = &data->hdev->dev;
854 struct backlight_device *bdev;
855 struct backlight_properties props;
858 if (report->maxfield != 1 || report->field[0]->report_count != 1 ||
859 report->field[0]->report_size != 8) {
860 dev_err(dev, "unsupported BRIGHTNESS report");
864 memset(&props, 0, sizeof(props));
865 props.max_brightness = 0xff;
866 bdev = backlight_device_register(dev_name(dev), dev, data,
867 &picolcd_blops, &props);
869 dev_err(dev, "failed to register backlight\n");
870 return PTR_ERR(bdev);
872 bdev->props.brightness = 0xff;
873 data->lcd_brightness = 0xff;
874 data->backlight = bdev;
875 picolcd_set_brightness(bdev);
879 static void picolcd_exit_backlight(struct picolcd_data *data)
881 struct backlight_device *bdev = data->backlight;
883 data->backlight = NULL;
885 backlight_device_unregister(bdev);
888 static inline int picolcd_resume_backlight(struct picolcd_data *data)
890 if (!data->backlight)
892 return picolcd_set_brightness(data->backlight);
896 static void picolcd_suspend_backlight(struct picolcd_data *data)
898 int bl_power = data->lcd_power;
899 if (!data->backlight)
902 data->backlight->props.power = FB_BLANK_POWERDOWN;
903 picolcd_set_brightness(data->backlight);
904 data->lcd_power = data->backlight->props.power = bl_power;
906 #endif /* CONFIG_PM */
908 static inline int picolcd_init_backlight(struct picolcd_data *data,
909 struct hid_report *report)
913 static inline void picolcd_exit_backlight(struct picolcd_data *data)
916 static inline int picolcd_resume_backlight(struct picolcd_data *data)
920 static inline void picolcd_suspend_backlight(struct picolcd_data *data)
923 #endif /* CONFIG_HID_PICOLCD_BACKLIGHT */
925 #ifdef CONFIG_HID_PICOLCD_LCD
929 static int picolcd_get_contrast(struct lcd_device *ldev)
931 struct picolcd_data *data = lcd_get_data(ldev);
932 return data->lcd_contrast;
935 static int picolcd_set_contrast(struct lcd_device *ldev, int contrast)
937 struct picolcd_data *data = lcd_get_data(ldev);
938 struct hid_report *report = picolcd_out_report(REPORT_CONTRAST, data->hdev);
941 if (!report || report->maxfield != 1 || report->field[0]->report_count != 1)
944 data->lcd_contrast = contrast & 0x0ff;
945 spin_lock_irqsave(&data->lock, flags);
946 hid_set_field(report->field[0], 0, data->lcd_contrast);
947 usbhid_submit_report(data->hdev, report, USB_DIR_OUT);
948 spin_unlock_irqrestore(&data->lock, flags);
952 static int picolcd_check_lcd_fb(struct lcd_device *ldev, struct fb_info *fb)
954 return fb && fb == picolcd_fbinfo((struct picolcd_data *)lcd_get_data(ldev));
957 static struct lcd_ops picolcd_lcdops = {
958 .get_contrast = picolcd_get_contrast,
959 .set_contrast = picolcd_set_contrast,
960 .check_fb = picolcd_check_lcd_fb,
963 static int picolcd_init_lcd(struct picolcd_data *data, struct hid_report *report)
965 struct device *dev = &data->hdev->dev;
966 struct lcd_device *ldev;
970 if (report->maxfield != 1 || report->field[0]->report_count != 1 ||
971 report->field[0]->report_size != 8) {
972 dev_err(dev, "unsupported CONTRAST report");
976 ldev = lcd_device_register(dev_name(dev), dev, data, &picolcd_lcdops);
978 dev_err(dev, "failed to register LCD\n");
979 return PTR_ERR(ldev);
981 ldev->props.max_contrast = 0x0ff;
982 data->lcd_contrast = 0xe5;
984 picolcd_set_contrast(ldev, 0xe5);
988 static void picolcd_exit_lcd(struct picolcd_data *data)
990 struct lcd_device *ldev = data->lcd;
994 lcd_device_unregister(ldev);
997 static inline int picolcd_resume_lcd(struct picolcd_data *data)
1001 return picolcd_set_contrast(data->lcd, data->lcd_contrast);
1004 static inline int picolcd_init_lcd(struct picolcd_data *data,
1005 struct hid_report *report)
1009 static inline void picolcd_exit_lcd(struct picolcd_data *data)
1012 static inline int picolcd_resume_lcd(struct picolcd_data *data)
1016 #endif /* CONFIG_HID_PICOLCD_LCD */
1018 #ifdef CONFIG_HID_PICOLCD_LEDS
1022 static void picolcd_leds_set(struct picolcd_data *data)
1024 struct hid_report *report;
1025 unsigned long flags;
1029 report = picolcd_out_report(REPORT_LED_STATE, data->hdev);
1030 if (!report || report->maxfield != 1 || report->field[0]->report_count != 1)
1033 spin_lock_irqsave(&data->lock, flags);
1034 hid_set_field(report->field[0], 0, data->led_state);
1035 usbhid_submit_report(data->hdev, report, USB_DIR_OUT);
1036 spin_unlock_irqrestore(&data->lock, flags);
1039 static void picolcd_led_set_brightness(struct led_classdev *led_cdev,
1040 enum led_brightness value)
1043 struct hid_device *hdev;
1044 struct picolcd_data *data;
1047 dev = led_cdev->dev->parent;
1048 hdev = container_of(dev, struct hid_device, dev);
1049 data = hid_get_drvdata(hdev);
1050 for (i = 0; i < 8; i++) {
1051 if (led_cdev != data->led[i])
1053 state = (data->led_state >> i) & 1;
1054 if (value == LED_OFF && state) {
1055 data->led_state &= ~(1 << i);
1056 picolcd_leds_set(data);
1057 } else if (value != LED_OFF && !state) {
1058 data->led_state |= 1 << i;
1059 picolcd_leds_set(data);
1065 static enum led_brightness picolcd_led_get_brightness(struct led_classdev *led_cdev)
1068 struct hid_device *hdev;
1069 struct picolcd_data *data;
1072 dev = led_cdev->dev->parent;
1073 hdev = container_of(dev, struct hid_device, dev);
1074 data = hid_get_drvdata(hdev);
1075 for (i = 0; i < 8; i++)
1076 if (led_cdev == data->led[i]) {
1077 value = (data->led_state >> i) & 1;
1080 return value ? LED_FULL : LED_OFF;
1083 static int picolcd_init_leds(struct picolcd_data *data, struct hid_report *report)
1085 struct device *dev = &data->hdev->dev;
1086 struct led_classdev *led;
1087 size_t name_sz = strlen(dev_name(dev)) + 8;
1093 if (report->maxfield != 1 || report->field[0]->report_count != 1 ||
1094 report->field[0]->report_size != 8) {
1095 dev_err(dev, "unsupported LED_STATE report");
1099 for (i = 0; i < 8; i++) {
1100 led = kzalloc(sizeof(struct led_classdev)+name_sz, GFP_KERNEL);
1102 dev_err(dev, "can't allocate memory for LED %d\n", i);
1106 name = (void *)(&led[1]);
1107 snprintf(name, name_sz, "%s::GPO%d", dev_name(dev), i);
1109 led->brightness = 0;
1110 led->max_brightness = 1;
1111 led->brightness_get = picolcd_led_get_brightness;
1112 led->brightness_set = picolcd_led_set_brightness;
1115 ret = led_classdev_register(dev, data->led[i]);
1117 data->led[i] = NULL;
1119 dev_err(dev, "can't register LED %d\n", i);
1125 for (i = 0; i < 8; i++)
1128 data->led[i] = NULL;
1129 led_classdev_unregister(led);
1135 static void picolcd_exit_leds(struct picolcd_data *data)
1137 struct led_classdev *led;
1140 for (i = 0; i < 8; i++) {
1142 data->led[i] = NULL;
1145 led_classdev_unregister(led);
1151 static inline int picolcd_init_leds(struct picolcd_data *data,
1152 struct hid_report *report)
1156 static inline void picolcd_exit_leds(struct picolcd_data *data)
1159 static inline int picolcd_leds_set(struct picolcd_data *data)
1163 #endif /* CONFIG_HID_PICOLCD_LEDS */
1166 * input class device
1168 static int picolcd_raw_keypad(struct picolcd_data *data,
1169 struct hid_report *report, u8 *raw_data, int size)
1173 * First and second data bytes list currently pressed keys,
1174 * 0x00 means no key and at most 2 keys may be pressed at same time
1178 /* determine newly pressed keys */
1179 for (i = 0; i < size; i++) {
1180 unsigned int key_code;
1181 if (raw_data[i] == 0)
1183 for (j = 0; j < sizeof(data->pressed_keys); j++)
1184 if (data->pressed_keys[j] == raw_data[i])
1185 goto key_already_down;
1186 for (j = 0; j < sizeof(data->pressed_keys); j++)
1187 if (data->pressed_keys[j] == 0) {
1188 data->pressed_keys[j] = raw_data[i];
1191 input_event(data->input_keys, EV_MSC, MSC_SCAN, raw_data[i]);
1192 if (raw_data[i] < PICOLCD_KEYS)
1193 key_code = data->keycode[raw_data[i]];
1195 key_code = KEY_UNKNOWN;
1196 if (key_code != KEY_UNKNOWN) {
1197 dbg_hid(PICOLCD_NAME " got key press for %u:%d",
1198 raw_data[i], key_code);
1199 input_report_key(data->input_keys, key_code, 1);
1201 input_sync(data->input_keys);
1206 /* determine newly released keys */
1207 for (j = 0; j < sizeof(data->pressed_keys); j++) {
1208 unsigned int key_code;
1209 if (data->pressed_keys[j] == 0)
1211 for (i = 0; i < size; i++)
1212 if (data->pressed_keys[j] == raw_data[i])
1213 goto key_still_down;
1214 input_event(data->input_keys, EV_MSC, MSC_SCAN, data->pressed_keys[j]);
1215 if (data->pressed_keys[j] < PICOLCD_KEYS)
1216 key_code = data->keycode[data->pressed_keys[j]];
1218 key_code = KEY_UNKNOWN;
1219 if (key_code != KEY_UNKNOWN) {
1220 dbg_hid(PICOLCD_NAME " got key release for %u:%d",
1221 data->pressed_keys[j], key_code);
1222 input_report_key(data->input_keys, key_code, 0);
1224 input_sync(data->input_keys);
1225 data->pressed_keys[j] = 0;
1232 static int picolcd_raw_cir(struct picolcd_data *data,
1233 struct hid_report *report, u8 *raw_data, int size)
1235 /* Need understanding of CIR data format to implement ... */
1239 static int picolcd_check_version(struct hid_device *hdev)
1241 struct picolcd_data *data = hid_get_drvdata(hdev);
1242 struct picolcd_pending *verinfo;
1248 verinfo = picolcd_send_and_wait(hdev, REPORT_VERSION, NULL, 0);
1250 dev_err(&hdev->dev, "no version response from PicoLCD");
1254 if (verinfo->raw_size == 2) {
1255 data->version[0] = verinfo->raw_data[1];
1256 data->version[1] = verinfo->raw_data[0];
1257 if (data->status & PICOLCD_BOOTLOADER) {
1258 dev_info(&hdev->dev, "PicoLCD, bootloader version %d.%d\n",
1259 verinfo->raw_data[1], verinfo->raw_data[0]);
1261 dev_info(&hdev->dev, "PicoLCD, firmware version %d.%d\n",
1262 verinfo->raw_data[1], verinfo->raw_data[0]);
1265 dev_err(&hdev->dev, "confused, got unexpected version response from PicoLCD\n");
1273 * Reset our device and wait for answer to VERSION request
1275 static int picolcd_reset(struct hid_device *hdev)
1277 struct picolcd_data *data = hid_get_drvdata(hdev);
1278 struct hid_report *report = picolcd_out_report(REPORT_RESET, hdev);
1279 unsigned long flags;
1282 if (!data || !report || report->maxfield != 1)
1285 spin_lock_irqsave(&data->lock, flags);
1286 if (hdev->product == USB_DEVICE_ID_PICOLCD_BOOTLOADER)
1287 data->status |= PICOLCD_BOOTLOADER;
1289 /* perform the reset */
1290 hid_set_field(report->field[0], 0, 1);
1291 usbhid_submit_report(hdev, report, USB_DIR_OUT);
1292 spin_unlock_irqrestore(&data->lock, flags);
1294 error = picolcd_check_version(hdev);
1298 picolcd_resume_lcd(data);
1299 picolcd_resume_backlight(data);
1300 #ifdef CONFIG_HID_PICOLCD_FB
1302 schedule_delayed_work(&data->fb_info->deferred_work, 0);
1303 #endif /* CONFIG_HID_PICOLCD_FB */
1305 picolcd_leds_set(data);
1310 * The "operation_mode" sysfs attribute
1312 static ssize_t picolcd_operation_mode_show(struct device *dev,
1313 struct device_attribute *attr, char *buf)
1315 struct picolcd_data *data = dev_get_drvdata(dev);
1317 if (data->status & PICOLCD_BOOTLOADER)
1318 return snprintf(buf, PAGE_SIZE, "[bootloader] lcd\n");
1320 return snprintf(buf, PAGE_SIZE, "bootloader [lcd]\n");
1323 static ssize_t picolcd_operation_mode_store(struct device *dev,
1324 struct device_attribute *attr, const char *buf, size_t count)
1326 struct picolcd_data *data = dev_get_drvdata(dev);
1327 struct hid_report *report = NULL;
1329 int timeout = data->opmode_delay;
1330 unsigned long flags;
1332 if (cnt >= 3 && strncmp("lcd", buf, 3) == 0) {
1333 if (data->status & PICOLCD_BOOTLOADER)
1334 report = picolcd_out_report(REPORT_EXIT_FLASHER, data->hdev);
1337 } else if (cnt >= 10 && strncmp("bootloader", buf, 10) == 0) {
1338 if (!(data->status & PICOLCD_BOOTLOADER))
1339 report = picolcd_out_report(REPORT_EXIT_KEYBOARD, data->hdev);
1346 while (cnt > 0 && (buf[cnt-1] == '\n' || buf[cnt-1] == '\r'))
1351 spin_lock_irqsave(&data->lock, flags);
1352 hid_set_field(report->field[0], 0, timeout & 0xff);
1353 hid_set_field(report->field[0], 1, (timeout >> 8) & 0xff);
1354 usbhid_submit_report(data->hdev, report, USB_DIR_OUT);
1355 spin_unlock_irqrestore(&data->lock, flags);
1359 static DEVICE_ATTR(operation_mode, 0644, picolcd_operation_mode_show,
1360 picolcd_operation_mode_store);
1363 * The "operation_mode_delay" sysfs attribute
1365 static ssize_t picolcd_operation_mode_delay_show(struct device *dev,
1366 struct device_attribute *attr, char *buf)
1368 struct picolcd_data *data = dev_get_drvdata(dev);
1370 return snprintf(buf, PAGE_SIZE, "%hu\n", data->opmode_delay);
1373 static ssize_t picolcd_operation_mode_delay_store(struct device *dev,
1374 struct device_attribute *attr, const char *buf, size_t count)
1376 struct picolcd_data *data = dev_get_drvdata(dev);
1378 if (sscanf(buf, "%u", &u) != 1)
1383 data->opmode_delay = u;
1387 static DEVICE_ATTR(operation_mode_delay, 0644, picolcd_operation_mode_delay_show,
1388 picolcd_operation_mode_delay_store);
1391 #ifdef CONFIG_DEBUG_FS
1395 static int picolcd_debug_reset_show(struct seq_file *f, void *p)
1397 if (picolcd_fbinfo((struct picolcd_data *)f->private))
1398 seq_printf(f, "all fb\n");
1400 seq_printf(f, "all\n");
1404 static int picolcd_debug_reset_open(struct inode *inode, struct file *f)
1406 return single_open(f, picolcd_debug_reset_show, inode->i_private);
1409 static ssize_t picolcd_debug_reset_write(struct file *f, const char __user *user_buf,
1410 size_t count, loff_t *ppos)
1412 struct picolcd_data *data = ((struct seq_file *)f->private_data)->private;
1414 size_t cnt = min(count, sizeof(buf)-1);
1415 if (copy_from_user(buf, user_buf, cnt))
1418 while (cnt > 0 && (buf[cnt-1] == ' ' || buf[cnt-1] == '\n'))
1421 if (strcmp(buf, "all") == 0) {
1422 picolcd_reset(data->hdev);
1423 picolcd_fb_reset(data, 1);
1424 } else if (strcmp(buf, "fb") == 0) {
1425 picolcd_fb_reset(data, 1);
1432 static const struct file_operations picolcd_debug_reset_fops = {
1433 .owner = THIS_MODULE,
1434 .open = picolcd_debug_reset_open,
1436 .llseek = seq_lseek,
1437 .write = picolcd_debug_reset_write,
1438 .release = single_release,
1444 static int picolcd_debug_eeprom_open(struct inode *i, struct file *f)
1446 f->private_data = i->i_private;
1450 static ssize_t picolcd_debug_eeprom_read(struct file *f, char __user *u,
1451 size_t s, loff_t *off)
1453 struct picolcd_data *data = f->private_data;
1454 struct picolcd_pending *resp;
1463 /* prepare buffer with info about what we want to read (addr & len) */
1464 raw_data[0] = *off & 0xff;
1465 raw_data[1] = (*off >> 8) && 0xff;
1466 raw_data[2] = s < 20 ? s : 20;
1467 if (*off + raw_data[2] > 0xff)
1468 raw_data[2] = 0x100 - *off;
1469 resp = picolcd_send_and_wait(data->hdev, REPORT_EE_READ, raw_data,
1474 if (resp->in_report && resp->in_report->id == REPORT_EE_DATA) {
1475 /* successful read :) */
1476 ret = resp->raw_data[2];
1479 if (copy_to_user(u, resp->raw_data+3, ret))
1483 } /* anything else is some kind of IO error */
1489 static ssize_t picolcd_debug_eeprom_write(struct file *f, const char __user *u,
1490 size_t s, loff_t *off)
1492 struct picolcd_data *data = f->private_data;
1493 struct picolcd_pending *resp;
1502 memset(raw_data, 0, sizeof(raw_data));
1503 raw_data[0] = *off & 0xff;
1504 raw_data[1] = (*off >> 8) && 0xff;
1505 raw_data[2] = s < 20 ? s : 20;
1506 if (*off + raw_data[2] > 0xff)
1507 raw_data[2] = 0x100 - *off;
1509 if (copy_from_user(raw_data+3, u, raw_data[2]))
1511 resp = picolcd_send_and_wait(data->hdev, REPORT_EE_WRITE, raw_data,
1517 if (resp->in_report && resp->in_report->id == REPORT_EE_DATA) {
1518 /* check if written data matches */
1519 if (memcmp(raw_data, resp->raw_data, 3+raw_data[2]) == 0) {
1520 *off += raw_data[2];
1530 * - read/write happens in chunks of at most 20 bytes, it's up to userspace
1531 * to loop in order to get more data.
1532 * - on write errors on otherwise correct write request the bytes
1533 * that should have been written are in undefined state.
1535 static const struct file_operations picolcd_debug_eeprom_fops = {
1536 .owner = THIS_MODULE,
1537 .open = picolcd_debug_eeprom_open,
1538 .read = picolcd_debug_eeprom_read,
1539 .write = picolcd_debug_eeprom_write,
1540 .llseek = generic_file_llseek,
1546 static int picolcd_debug_flash_open(struct inode *i, struct file *f)
1548 f->private_data = i->i_private;
1552 /* record a flash address to buf (bounds check to be done by caller) */
1553 static int _picolcd_flash_setaddr(struct picolcd_data *data, u8 *buf, long off)
1555 buf[0] = off & 0xff;
1556 buf[1] = (off >> 8) & 0xff;
1557 if (data->addr_sz == 3)
1558 buf[2] = (off >> 16) & 0xff;
1559 return data->addr_sz == 2 ? 2 : 3;
1562 /* read a given size of data (bounds check to be done by caller) */
1563 static ssize_t _picolcd_flash_read(struct picolcd_data *data, int report_id,
1564 char __user *u, size_t s, loff_t *off)
1566 struct picolcd_pending *resp;
1569 int len_off, err = -EIO;
1573 len_off = _picolcd_flash_setaddr(data, raw_data, *off);
1574 raw_data[len_off] = s > 32 ? 32 : s;
1575 resp = picolcd_send_and_wait(data->hdev, report_id, raw_data, len_off+1);
1576 if (!resp || !resp->in_report)
1578 if (resp->in_report->id == REPORT_MEMORY ||
1579 resp->in_report->id == REPORT_BL_READ_MEMORY) {
1580 if (memcmp(raw_data, resp->raw_data, len_off+1) != 0)
1582 if (copy_to_user(u+ret, resp->raw_data+len_off+1, raw_data[len_off])) {
1586 *off += raw_data[len_off];
1587 s -= raw_data[len_off];
1588 ret += raw_data[len_off];
1594 return ret > 0 ? ret : err;
1599 static ssize_t picolcd_debug_flash_read(struct file *f, char __user *u,
1600 size_t s, loff_t *off)
1602 struct picolcd_data *data = f->private_data;
1608 if (*off + s > 0x05fff)
1611 if (data->status & PICOLCD_BOOTLOADER)
1612 return _picolcd_flash_read(data, REPORT_BL_READ_MEMORY, u, s, off);
1614 return _picolcd_flash_read(data, REPORT_READ_MEMORY, u, s, off);
1617 /* erase block aligned to 64bytes boundary */
1618 static ssize_t _picolcd_flash_erase64(struct picolcd_data *data, int report_id,
1621 struct picolcd_pending *resp;
1629 len_off = _picolcd_flash_setaddr(data, raw_data, *off);
1630 resp = picolcd_send_and_wait(data->hdev, report_id, raw_data, len_off);
1631 if (!resp || !resp->in_report)
1633 if (resp->in_report->id == REPORT_MEMORY ||
1634 resp->in_report->id == REPORT_BL_ERASE_MEMORY) {
1635 if (memcmp(raw_data, resp->raw_data, len_off) != 0)
1644 /* write a given size of data (bounds check to be done by caller) */
1645 static ssize_t _picolcd_flash_write(struct picolcd_data *data, int report_id,
1646 const char __user *u, size_t s, loff_t *off)
1648 struct picolcd_pending *resp;
1651 int len_off, err = -EIO;
1655 len_off = _picolcd_flash_setaddr(data, raw_data, *off);
1656 raw_data[len_off] = s > 32 ? 32 : s;
1657 if (copy_from_user(raw_data+len_off+1, u, raw_data[len_off])) {
1661 resp = picolcd_send_and_wait(data->hdev, report_id, raw_data,
1662 len_off+1+raw_data[len_off]);
1663 if (!resp || !resp->in_report)
1665 if (resp->in_report->id == REPORT_MEMORY ||
1666 resp->in_report->id == REPORT_BL_WRITE_MEMORY) {
1667 if (memcmp(raw_data, resp->raw_data, len_off+1+raw_data[len_off]) != 0)
1669 *off += raw_data[len_off];
1670 s -= raw_data[len_off];
1671 ret += raw_data[len_off];
1679 return ret > 0 ? ret : err;
1682 static ssize_t picolcd_debug_flash_write(struct file *f, const char __user *u,
1683 size_t s, loff_t *off)
1685 struct picolcd_data *data = f->private_data;
1686 ssize_t err, ret = 0;
1687 int report_erase, report_write;
1698 if (data->status & PICOLCD_BOOTLOADER) {
1699 report_erase = REPORT_BL_ERASE_MEMORY;
1700 report_write = REPORT_BL_WRITE_MEMORY;
1702 report_erase = REPORT_ERASE_MEMORY;
1703 report_write = REPORT_WRITE_MEMORY;
1705 mutex_lock(&data->mutex_flash);
1707 err = _picolcd_flash_erase64(data, report_erase, off);
1710 err = _picolcd_flash_write(data, report_write, u, 64, off);
1719 mutex_unlock(&data->mutex_flash);
1720 return ret > 0 ? ret : err;
1725 * - concurrent writing is prevented by mutex and all writes must be
1726 * n*64 bytes and 64-byte aligned, each write being preceeded by an
1727 * ERASE which erases a 64byte block.
1728 * If less than requested was written or an error is returned for an
1729 * otherwise correct write request the next 64-byte block which should
1730 * have been written is in undefined state (mostly: original, erased,
1731 * (half-)written with write error)
1732 * - reading can happend without special restriction
1734 static const struct file_operations picolcd_debug_flash_fops = {
1735 .owner = THIS_MODULE,
1736 .open = picolcd_debug_flash_open,
1737 .read = picolcd_debug_flash_read,
1738 .write = picolcd_debug_flash_write,
1739 .llseek = generic_file_llseek,
1744 * Helper code for HID report level dumping/debugging
1746 static const char *error_codes[] = {
1747 "success", "parameter missing", "data_missing", "block readonly",
1748 "block not erasable", "block too big", "section overflow",
1749 "invalid command length", "invalid data length",
1752 static void dump_buff_as_hex(char *dst, size_t dst_sz, const u8 *data,
1753 const size_t data_len)
1756 for (i = j = 0; i < data_len && j + 3 < dst_sz; i++) {
1757 dst[j++] = hex_asc[(data[i] >> 4) & 0x0f];
1758 dst[j++] = hex_asc[data[i] & 0x0f];
1768 static void picolcd_debug_out_report(struct picolcd_data *data,
1769 struct hid_device *hdev, struct hid_report *report)
1772 int raw_size = (report->size >> 3) + 1;
1776 /* Avoid unnecessary overhead if debugfs is disabled */
1777 if (!hdev->debug_events)
1780 buff = kmalloc(BUFF_SZ, GFP_ATOMIC);
1784 snprintf(buff, BUFF_SZ, "\nout report %d (size %d) = ",
1785 report->id, raw_size);
1786 hid_debug_event(hdev, buff);
1787 if (raw_size + 5 > sizeof(raw_data)) {
1788 hid_debug_event(hdev, " TOO BIG\n");
1791 raw_data[0] = report->id;
1792 hid_output_report(report, raw_data);
1793 dump_buff_as_hex(buff, BUFF_SZ, raw_data, raw_size);
1794 hid_debug_event(hdev, buff);
1797 switch (report->id) {
1798 case REPORT_LED_STATE:
1799 /* 1 data byte with GPO state */
1800 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1801 "REPORT_LED_STATE", report->id, raw_size-1);
1802 hid_debug_event(hdev, buff);
1803 snprintf(buff, BUFF_SZ, "\tGPO state: 0x%02x\n", raw_data[1]);
1804 hid_debug_event(hdev, buff);
1806 case REPORT_BRIGHTNESS:
1807 /* 1 data byte with brightness */
1808 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1809 "REPORT_BRIGHTNESS", report->id, raw_size-1);
1810 hid_debug_event(hdev, buff);
1811 snprintf(buff, BUFF_SZ, "\tBrightness: 0x%02x\n", raw_data[1]);
1812 hid_debug_event(hdev, buff);
1814 case REPORT_CONTRAST:
1815 /* 1 data byte with contrast */
1816 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1817 "REPORT_CONTRAST", report->id, raw_size-1);
1818 hid_debug_event(hdev, buff);
1819 snprintf(buff, BUFF_SZ, "\tContrast: 0x%02x\n", raw_data[1]);
1820 hid_debug_event(hdev, buff);
1823 /* 2 data bytes with reset duration in ms */
1824 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1825 "REPORT_RESET", report->id, raw_size-1);
1826 hid_debug_event(hdev, buff);
1827 snprintf(buff, BUFF_SZ, "\tDuration: 0x%02x%02x (%dms)\n",
1828 raw_data[2], raw_data[1], raw_data[2] << 8 | raw_data[1]);
1829 hid_debug_event(hdev, buff);
1831 case REPORT_LCD_CMD:
1832 /* 63 data bytes with LCD commands */
1833 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1834 "REPORT_LCD_CMD", report->id, raw_size-1);
1835 hid_debug_event(hdev, buff);
1836 /* TODO: format decoding */
1838 case REPORT_LCD_DATA:
1839 /* 63 data bytes with LCD data */
1840 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1841 "REPORT_LCD_CMD", report->id, raw_size-1);
1842 /* TODO: format decoding */
1843 hid_debug_event(hdev, buff);
1845 case REPORT_LCD_CMD_DATA:
1846 /* 63 data bytes with LCD commands and data */
1847 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1848 "REPORT_LCD_CMD", report->id, raw_size-1);
1849 /* TODO: format decoding */
1850 hid_debug_event(hdev, buff);
1852 case REPORT_EE_READ:
1853 /* 3 data bytes with read area description */
1854 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1855 "REPORT_EE_READ", report->id, raw_size-1);
1856 hid_debug_event(hdev, buff);
1857 snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x\n",
1858 raw_data[2], raw_data[1]);
1859 hid_debug_event(hdev, buff);
1860 snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[3]);
1861 hid_debug_event(hdev, buff);
1863 case REPORT_EE_WRITE:
1864 /* 3+1..20 data bytes with write area description */
1865 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1866 "REPORT_EE_WRITE", report->id, raw_size-1);
1867 hid_debug_event(hdev, buff);
1868 snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x\n",
1869 raw_data[2], raw_data[1]);
1870 hid_debug_event(hdev, buff);
1871 snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[3]);
1872 hid_debug_event(hdev, buff);
1873 if (raw_data[3] == 0) {
1874 snprintf(buff, BUFF_SZ, "\tNo data\n");
1875 } else if (raw_data[3] + 4 <= raw_size) {
1876 snprintf(buff, BUFF_SZ, "\tData: ");
1877 hid_debug_event(hdev, buff);
1878 dump_buff_as_hex(buff, BUFF_SZ, raw_data+4, raw_data[3]);
1880 snprintf(buff, BUFF_SZ, "\tData overflowed\n");
1882 hid_debug_event(hdev, buff);
1884 case REPORT_ERASE_MEMORY:
1885 case REPORT_BL_ERASE_MEMORY:
1886 /* 3 data bytes with pointer inside erase block */
1887 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1888 "REPORT_ERASE_MEMORY", report->id, raw_size-1);
1889 hid_debug_event(hdev, buff);
1890 switch (data->addr_sz) {
1892 snprintf(buff, BUFF_SZ, "\tAddress inside 64 byte block: 0x%02x%02x\n",
1893 raw_data[2], raw_data[1]);
1896 snprintf(buff, BUFF_SZ, "\tAddress inside 64 byte block: 0x%02x%02x%02x\n",
1897 raw_data[3], raw_data[2], raw_data[1]);
1900 snprintf(buff, BUFF_SZ, "\tNot supported\n");
1902 hid_debug_event(hdev, buff);
1904 case REPORT_READ_MEMORY:
1905 case REPORT_BL_READ_MEMORY:
1906 /* 4 data bytes with read area description */
1907 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1908 "REPORT_READ_MEMORY", report->id, raw_size-1);
1909 hid_debug_event(hdev, buff);
1910 switch (data->addr_sz) {
1912 snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x\n",
1913 raw_data[2], raw_data[1]);
1914 hid_debug_event(hdev, buff);
1915 snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[3]);
1918 snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x%02x\n",
1919 raw_data[3], raw_data[2], raw_data[1]);
1920 hid_debug_event(hdev, buff);
1921 snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[4]);
1924 snprintf(buff, BUFF_SZ, "\tNot supported\n");
1926 hid_debug_event(hdev, buff);
1928 case REPORT_WRITE_MEMORY:
1929 case REPORT_BL_WRITE_MEMORY:
1930 /* 4+1..32 data bytes with write adrea description */
1931 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1932 "REPORT_WRITE_MEMORY", report->id, raw_size-1);
1933 hid_debug_event(hdev, buff);
1934 switch (data->addr_sz) {
1936 snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x\n",
1937 raw_data[2], raw_data[1]);
1938 hid_debug_event(hdev, buff);
1939 snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[3]);
1940 hid_debug_event(hdev, buff);
1941 if (raw_data[3] == 0) {
1942 snprintf(buff, BUFF_SZ, "\tNo data\n");
1943 } else if (raw_data[3] + 4 <= raw_size) {
1944 snprintf(buff, BUFF_SZ, "\tData: ");
1945 hid_debug_event(hdev, buff);
1946 dump_buff_as_hex(buff, BUFF_SZ, raw_data+4, raw_data[3]);
1948 snprintf(buff, BUFF_SZ, "\tData overflowed\n");
1952 snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x%02x\n",
1953 raw_data[3], raw_data[2], raw_data[1]);
1954 hid_debug_event(hdev, buff);
1955 snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[4]);
1956 hid_debug_event(hdev, buff);
1957 if (raw_data[4] == 0) {
1958 snprintf(buff, BUFF_SZ, "\tNo data\n");
1959 } else if (raw_data[4] + 5 <= raw_size) {
1960 snprintf(buff, BUFF_SZ, "\tData: ");
1961 hid_debug_event(hdev, buff);
1962 dump_buff_as_hex(buff, BUFF_SZ, raw_data+5, raw_data[4]);
1964 snprintf(buff, BUFF_SZ, "\tData overflowed\n");
1968 snprintf(buff, BUFF_SZ, "\tNot supported\n");
1970 hid_debug_event(hdev, buff);
1972 case REPORT_SPLASH_RESTART:
1975 case REPORT_EXIT_KEYBOARD:
1976 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1977 "REPORT_EXIT_KEYBOARD", report->id, raw_size-1);
1978 hid_debug_event(hdev, buff);
1979 snprintf(buff, BUFF_SZ, "\tRestart delay: %dms (0x%02x%02x)\n",
1980 raw_data[1] | (raw_data[2] << 8),
1981 raw_data[2], raw_data[1]);
1982 hid_debug_event(hdev, buff);
1984 case REPORT_VERSION:
1985 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1986 "REPORT_VERSION", report->id, raw_size-1);
1987 hid_debug_event(hdev, buff);
1990 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1991 "REPORT_DEVID", report->id, raw_size-1);
1992 hid_debug_event(hdev, buff);
1994 case REPORT_SPLASH_SIZE:
1995 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1996 "REPORT_SPLASH_SIZE", report->id, raw_size-1);
1997 hid_debug_event(hdev, buff);
1999 case REPORT_HOOK_VERSION:
2000 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
2001 "REPORT_HOOK_VERSION", report->id, raw_size-1);
2002 hid_debug_event(hdev, buff);
2004 case REPORT_EXIT_FLASHER:
2005 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
2006 "REPORT_VERSION", report->id, raw_size-1);
2007 hid_debug_event(hdev, buff);
2008 snprintf(buff, BUFF_SZ, "\tRestart delay: %dms (0x%02x%02x)\n",
2009 raw_data[1] | (raw_data[2] << 8),
2010 raw_data[2], raw_data[1]);
2011 hid_debug_event(hdev, buff);
2014 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
2015 "<unknown>", report->id, raw_size-1);
2016 hid_debug_event(hdev, buff);
2019 wake_up_interruptible(&hdev->debug_wait);
2023 static void picolcd_debug_raw_event(struct picolcd_data *data,
2024 struct hid_device *hdev, struct hid_report *report,
2025 u8 *raw_data, int size)
2030 /* Avoid unnecessary overhead if debugfs is disabled */
2031 if (!hdev->debug_events)
2034 buff = kmalloc(BUFF_SZ, GFP_ATOMIC);
2038 switch (report->id) {
2039 case REPORT_ERROR_CODE:
2040 /* 2 data bytes with affected report and error code */
2041 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2042 "REPORT_ERROR_CODE", report->id, size-1);
2043 hid_debug_event(hdev, buff);
2044 if (raw_data[2] < ARRAY_SIZE(error_codes))
2045 snprintf(buff, BUFF_SZ, "\tError code 0x%02x (%s) in reply to report 0x%02x\n",
2046 raw_data[2], error_codes[raw_data[2]], raw_data[1]);
2048 snprintf(buff, BUFF_SZ, "\tError code 0x%02x in reply to report 0x%02x\n",
2049 raw_data[2], raw_data[1]);
2050 hid_debug_event(hdev, buff);
2052 case REPORT_KEY_STATE:
2053 /* 2 data bytes with key state */
2054 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2055 "REPORT_KEY_STATE", report->id, size-1);
2056 hid_debug_event(hdev, buff);
2057 if (raw_data[1] == 0)
2058 snprintf(buff, BUFF_SZ, "\tNo key pressed\n");
2059 else if (raw_data[2] == 0)
2060 snprintf(buff, BUFF_SZ, "\tOne key pressed: 0x%02x (%d)\n",
2061 raw_data[1], raw_data[1]);
2063 snprintf(buff, BUFF_SZ, "\tTwo keys pressed: 0x%02x (%d), 0x%02x (%d)\n",
2064 raw_data[1], raw_data[1], raw_data[2], raw_data[2]);
2065 hid_debug_event(hdev, buff);
2067 case REPORT_IR_DATA:
2068 /* Up to 20 byes of IR scancode data */
2069 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2070 "REPORT_IR_DATA", report->id, size-1);
2071 hid_debug_event(hdev, buff);
2072 if (raw_data[1] == 0) {
2073 snprintf(buff, BUFF_SZ, "\tUnexpectedly 0 data length\n");
2074 hid_debug_event(hdev, buff);
2075 } else if (raw_data[1] + 1 <= size) {
2076 snprintf(buff, BUFF_SZ, "\tData length: %d\n\tIR Data: ",
2078 hid_debug_event(hdev, buff);
2079 dump_buff_as_hex(buff, BUFF_SZ, raw_data+2, raw_data[1]-1);
2080 hid_debug_event(hdev, buff);
2082 snprintf(buff, BUFF_SZ, "\tOverflowing data length: %d\n",
2084 hid_debug_event(hdev, buff);
2087 case REPORT_EE_DATA:
2088 /* Data buffer in response to REPORT_EE_READ or REPORT_EE_WRITE */
2089 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2090 "REPORT_EE_DATA", report->id, size-1);
2091 hid_debug_event(hdev, buff);
2092 snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x\n",
2093 raw_data[2], raw_data[1]);
2094 hid_debug_event(hdev, buff);
2095 snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[3]);
2096 hid_debug_event(hdev, buff);
2097 if (raw_data[3] == 0) {
2098 snprintf(buff, BUFF_SZ, "\tNo data\n");
2099 hid_debug_event(hdev, buff);
2100 } else if (raw_data[3] + 4 <= size) {
2101 snprintf(buff, BUFF_SZ, "\tData: ");
2102 hid_debug_event(hdev, buff);
2103 dump_buff_as_hex(buff, BUFF_SZ, raw_data+4, raw_data[3]);
2104 hid_debug_event(hdev, buff);
2106 snprintf(buff, BUFF_SZ, "\tData overflowed\n");
2107 hid_debug_event(hdev, buff);
2111 /* Data buffer in response to REPORT_READ_MEMORY or REPORT_WRTIE_MEMORY */
2112 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2113 "REPORT_MEMORY", report->id, size-1);
2114 hid_debug_event(hdev, buff);
2115 switch (data->addr_sz) {
2117 snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x\n",
2118 raw_data[2], raw_data[1]);
2119 hid_debug_event(hdev, buff);
2120 snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[3]);
2121 hid_debug_event(hdev, buff);
2122 if (raw_data[3] == 0) {
2123 snprintf(buff, BUFF_SZ, "\tNo data\n");
2124 } else if (raw_data[3] + 4 <= size) {
2125 snprintf(buff, BUFF_SZ, "\tData: ");
2126 hid_debug_event(hdev, buff);
2127 dump_buff_as_hex(buff, BUFF_SZ, raw_data+4, raw_data[3]);
2129 snprintf(buff, BUFF_SZ, "\tData overflowed\n");
2133 snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x%02x\n",
2134 raw_data[3], raw_data[2], raw_data[1]);
2135 hid_debug_event(hdev, buff);
2136 snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[4]);
2137 hid_debug_event(hdev, buff);
2138 if (raw_data[4] == 0) {
2139 snprintf(buff, BUFF_SZ, "\tNo data\n");
2140 } else if (raw_data[4] + 5 <= size) {
2141 snprintf(buff, BUFF_SZ, "\tData: ");
2142 hid_debug_event(hdev, buff);
2143 dump_buff_as_hex(buff, BUFF_SZ, raw_data+5, raw_data[4]);
2145 snprintf(buff, BUFF_SZ, "\tData overflowed\n");
2149 snprintf(buff, BUFF_SZ, "\tNot supported\n");
2151 hid_debug_event(hdev, buff);
2153 case REPORT_VERSION:
2154 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2155 "REPORT_VERSION", report->id, size-1);
2156 hid_debug_event(hdev, buff);
2157 snprintf(buff, BUFF_SZ, "\tFirmware version: %d.%d\n",
2158 raw_data[2], raw_data[1]);
2159 hid_debug_event(hdev, buff);
2161 case REPORT_BL_ERASE_MEMORY:
2162 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2163 "REPORT_BL_ERASE_MEMORY", report->id, size-1);
2164 hid_debug_event(hdev, buff);
2167 case REPORT_BL_READ_MEMORY:
2168 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2169 "REPORT_BL_READ_MEMORY", report->id, size-1);
2170 hid_debug_event(hdev, buff);
2173 case REPORT_BL_WRITE_MEMORY:
2174 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2175 "REPORT_BL_WRITE_MEMORY", report->id, size-1);
2176 hid_debug_event(hdev, buff);
2180 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2181 "REPORT_DEVID", report->id, size-1);
2182 hid_debug_event(hdev, buff);
2183 snprintf(buff, BUFF_SZ, "\tSerial: 0x%02x%02x%02x%02x\n",
2184 raw_data[1], raw_data[2], raw_data[3], raw_data[4]);
2185 hid_debug_event(hdev, buff);
2186 snprintf(buff, BUFF_SZ, "\tType: 0x%02x\n",
2188 hid_debug_event(hdev, buff);
2190 case REPORT_SPLASH_SIZE:
2191 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2192 "REPORT_SPLASH_SIZE", report->id, size-1);
2193 hid_debug_event(hdev, buff);
2194 snprintf(buff, BUFF_SZ, "\tTotal splash space: %d\n",
2195 (raw_data[2] << 8) | raw_data[1]);
2196 hid_debug_event(hdev, buff);
2197 snprintf(buff, BUFF_SZ, "\tUsed splash space: %d\n",
2198 (raw_data[4] << 8) | raw_data[3]);
2199 hid_debug_event(hdev, buff);
2201 case REPORT_HOOK_VERSION:
2202 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2203 "REPORT_HOOK_VERSION", report->id, size-1);
2204 hid_debug_event(hdev, buff);
2205 snprintf(buff, BUFF_SZ, "\tFirmware version: %d.%d\n",
2206 raw_data[1], raw_data[2]);
2207 hid_debug_event(hdev, buff);
2210 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2211 "<unknown>", report->id, size-1);
2212 hid_debug_event(hdev, buff);
2215 wake_up_interruptible(&hdev->debug_wait);
2219 static void picolcd_init_devfs(struct picolcd_data *data,
2220 struct hid_report *eeprom_r, struct hid_report *eeprom_w,
2221 struct hid_report *flash_r, struct hid_report *flash_w,
2222 struct hid_report *reset)
2224 struct hid_device *hdev = data->hdev;
2226 mutex_init(&data->mutex_flash);
2230 data->debug_reset = debugfs_create_file("reset", 0600,
2231 hdev->debug_dir, data, &picolcd_debug_reset_fops);
2234 if (eeprom_r || eeprom_w)
2235 data->debug_eeprom = debugfs_create_file("eeprom",
2236 (eeprom_w ? S_IWUSR : 0) | (eeprom_r ? S_IRUSR : 0),
2237 hdev->debug_dir, data, &picolcd_debug_eeprom_fops);
2240 if (flash_r && flash_r->maxfield == 1 && flash_r->field[0]->report_size == 8)
2241 data->addr_sz = flash_r->field[0]->report_count - 1;
2244 if (data->addr_sz == 2 || data->addr_sz == 3) {
2245 data->debug_flash = debugfs_create_file("flash",
2246 (flash_w ? S_IWUSR : 0) | (flash_r ? S_IRUSR : 0),
2247 hdev->debug_dir, data, &picolcd_debug_flash_fops);
2248 } else if (flash_r || flash_w)
2249 dev_warn(&hdev->dev, "Unexpected FLASH access reports, "
2250 "please submit rdesc for review\n");
2253 static void picolcd_exit_devfs(struct picolcd_data *data)
2255 struct dentry *dent;
2257 dent = data->debug_reset;
2258 data->debug_reset = NULL;
2260 debugfs_remove(dent);
2261 dent = data->debug_eeprom;
2262 data->debug_eeprom = NULL;
2264 debugfs_remove(dent);
2265 dent = data->debug_flash;
2266 data->debug_flash = NULL;
2268 debugfs_remove(dent);
2269 mutex_destroy(&data->mutex_flash);
2272 static inline void picolcd_debug_raw_event(struct picolcd_data *data,
2273 struct hid_device *hdev, struct hid_report *report,
2274 u8 *raw_data, int size)
2277 static inline void picolcd_init_devfs(struct picolcd_data *data,
2278 struct hid_report *eeprom_r, struct hid_report *eeprom_w,
2279 struct hid_report *flash_r, struct hid_report *flash_w,
2280 struct hid_report *reset)
2283 static inline void picolcd_exit_devfs(struct picolcd_data *data)
2286 #endif /* CONFIG_DEBUG_FS */
2289 * Handle raw report as sent by device
2291 static int picolcd_raw_event(struct hid_device *hdev,
2292 struct hid_report *report, u8 *raw_data, int size)
2294 struct picolcd_data *data = hid_get_drvdata(hdev);
2295 unsigned long flags;
2301 if (report->id == REPORT_KEY_STATE) {
2302 if (data->input_keys)
2303 ret = picolcd_raw_keypad(data, report, raw_data+1, size-1);
2304 } else if (report->id == REPORT_IR_DATA) {
2305 if (data->input_cir)
2306 ret = picolcd_raw_cir(data, report, raw_data+1, size-1);
2308 spin_lock_irqsave(&data->lock, flags);
2310 * We let the caller of picolcd_send_and_wait() check if the
2311 * report we got is one of the expected ones or not.
2313 if (data->pending) {
2314 memcpy(data->pending->raw_data, raw_data+1, size-1);
2315 data->pending->raw_size = size-1;
2316 data->pending->in_report = report;
2317 complete(&data->pending->ready);
2319 spin_unlock_irqrestore(&data->lock, flags);
2322 picolcd_debug_raw_event(data, hdev, report, raw_data, size);
2327 static int picolcd_suspend(struct hid_device *hdev, pm_message_t message)
2329 if (message.event & PM_EVENT_AUTO)
2332 picolcd_suspend_backlight(hid_get_drvdata(hdev));
2333 dbg_hid(PICOLCD_NAME " device ready for suspend\n");
2337 static int picolcd_resume(struct hid_device *hdev)
2340 ret = picolcd_resume_backlight(hid_get_drvdata(hdev));
2342 dbg_hid(PICOLCD_NAME " restoring backlight failed: %d\n", ret);
2346 static int picolcd_reset_resume(struct hid_device *hdev)
2349 ret = picolcd_reset(hdev);
2351 dbg_hid(PICOLCD_NAME " resetting our device failed: %d\n", ret);
2352 ret = picolcd_fb_reset(hid_get_drvdata(hdev), 0);
2354 dbg_hid(PICOLCD_NAME " restoring framebuffer content failed: %d\n", ret);
2355 ret = picolcd_resume_lcd(hid_get_drvdata(hdev));
2357 dbg_hid(PICOLCD_NAME " restoring lcd failed: %d\n", ret);
2358 ret = picolcd_resume_backlight(hid_get_drvdata(hdev));
2360 dbg_hid(PICOLCD_NAME " restoring backlight failed: %d\n", ret);
2361 picolcd_leds_set(hid_get_drvdata(hdev));
2366 /* initialize keypad input device */
2367 static int picolcd_init_keys(struct picolcd_data *data,
2368 struct hid_report *report)
2370 struct hid_device *hdev = data->hdev;
2371 struct input_dev *idev;
2376 if (report->maxfield != 1 || report->field[0]->report_count != 2 ||
2377 report->field[0]->report_size != 8) {
2378 dev_err(&hdev->dev, "unsupported KEY_STATE report");
2382 idev = input_allocate_device();
2384 dev_err(&hdev->dev, "failed to allocate input device");
2387 input_set_drvdata(idev, hdev);
2388 memcpy(data->keycode, def_keymap, sizeof(def_keymap));
2389 idev->name = hdev->name;
2390 idev->phys = hdev->phys;
2391 idev->uniq = hdev->uniq;
2392 idev->id.bustype = hdev->bus;
2393 idev->id.vendor = hdev->vendor;
2394 idev->id.product = hdev->product;
2395 idev->id.version = hdev->version;
2396 idev->dev.parent = hdev->dev.parent;
2397 idev->keycode = &data->keycode;
2398 idev->keycodemax = PICOLCD_KEYS;
2399 idev->keycodesize = sizeof(data->keycode[0]);
2400 input_set_capability(idev, EV_MSC, MSC_SCAN);
2401 set_bit(EV_REP, idev->evbit);
2402 for (i = 0; i < PICOLCD_KEYS; i++)
2403 input_set_capability(idev, EV_KEY, data->keycode[i]);
2404 error = input_register_device(idev);
2406 dev_err(&hdev->dev, "error registering the input device");
2407 input_free_device(idev);
2410 data->input_keys = idev;
2414 static void picolcd_exit_keys(struct picolcd_data *data)
2416 struct input_dev *idev = data->input_keys;
2418 data->input_keys = NULL;
2420 input_unregister_device(idev);
2423 /* initialize CIR input device */
2424 static inline int picolcd_init_cir(struct picolcd_data *data, struct hid_report *report)
2426 /* support not implemented yet */
2430 static inline void picolcd_exit_cir(struct picolcd_data *data)
2434 static int picolcd_probe_lcd(struct hid_device *hdev, struct picolcd_data *data)
2438 error = picolcd_check_version(hdev);
2442 if (data->version[0] != 0 && data->version[1] != 3)
2443 dev_info(&hdev->dev, "Device with untested firmware revision, "
2444 "please submit /sys/kernel/debug/hid/%s/rdesc for this device.\n",
2445 dev_name(&hdev->dev));
2447 /* Setup keypad input device */
2448 error = picolcd_init_keys(data, picolcd_in_report(REPORT_KEY_STATE, hdev));
2452 /* Setup CIR input device */
2453 error = picolcd_init_cir(data, picolcd_in_report(REPORT_IR_DATA, hdev));
2457 /* Set up the framebuffer device */
2458 error = picolcd_init_framebuffer(data);
2462 /* Setup lcd class device */
2463 error = picolcd_init_lcd(data, picolcd_out_report(REPORT_CONTRAST, hdev));
2467 /* Setup backlight class device */
2468 error = picolcd_init_backlight(data, picolcd_out_report(REPORT_BRIGHTNESS, hdev));
2472 /* Setup the LED class devices */
2473 error = picolcd_init_leds(data, picolcd_out_report(REPORT_LED_STATE, hdev));
2477 picolcd_init_devfs(data, picolcd_out_report(REPORT_EE_READ, hdev),
2478 picolcd_out_report(REPORT_EE_WRITE, hdev),
2479 picolcd_out_report(REPORT_READ_MEMORY, hdev),
2480 picolcd_out_report(REPORT_WRITE_MEMORY, hdev),
2481 picolcd_out_report(REPORT_RESET, hdev));
2484 picolcd_exit_leds(data);
2485 picolcd_exit_backlight(data);
2486 picolcd_exit_lcd(data);
2487 picolcd_exit_framebuffer(data);
2488 picolcd_exit_cir(data);
2489 picolcd_exit_keys(data);
2493 static int picolcd_probe_bootloader(struct hid_device *hdev, struct picolcd_data *data)
2497 error = picolcd_check_version(hdev);
2501 if (data->version[0] != 1 && data->version[1] != 0)
2502 dev_info(&hdev->dev, "Device with untested bootloader revision, "
2503 "please submit /sys/kernel/debug/hid/%s/rdesc for this device.\n",
2504 dev_name(&hdev->dev));
2506 picolcd_init_devfs(data, NULL, NULL,
2507 picolcd_out_report(REPORT_BL_READ_MEMORY, hdev),
2508 picolcd_out_report(REPORT_BL_WRITE_MEMORY, hdev), NULL);
2512 static int picolcd_probe(struct hid_device *hdev,
2513 const struct hid_device_id *id)
2515 struct picolcd_data *data;
2516 int error = -ENOMEM;
2518 dbg_hid(PICOLCD_NAME " hardware probe...\n");
2521 * Let's allocate the picolcd data structure, set some reasonable
2522 * defaults, and associate it with the device
2524 data = kzalloc(sizeof(struct picolcd_data), GFP_KERNEL);
2526 dev_err(&hdev->dev, "can't allocate space for Minibox PicoLCD device data\n");
2528 goto err_no_cleanup;
2531 spin_lock_init(&data->lock);
2532 mutex_init(&data->mutex);
2534 data->opmode_delay = 5000;
2535 if (hdev->product == USB_DEVICE_ID_PICOLCD_BOOTLOADER)
2536 data->status |= PICOLCD_BOOTLOADER;
2537 hid_set_drvdata(hdev, data);
2539 /* Parse the device reports and start it up */
2540 error = hid_parse(hdev);
2542 dev_err(&hdev->dev, "device report parse failed\n");
2543 goto err_cleanup_data;
2546 /* We don't use hidinput but hid_hw_start() fails if nothing is
2547 * claimed. So spoof claimed input. */
2548 hdev->claimed = HID_CLAIMED_INPUT;
2549 error = hid_hw_start(hdev, 0);
2552 dev_err(&hdev->dev, "hardware start failed\n");
2553 goto err_cleanup_data;
2556 error = hdev->ll_driver->open(hdev);
2558 dev_err(&hdev->dev, "failed to open input interrupt pipe for key and IR events\n");
2559 goto err_cleanup_hid_hw;
2562 error = device_create_file(&hdev->dev, &dev_attr_operation_mode_delay);
2564 dev_err(&hdev->dev, "failed to create sysfs attributes\n");
2565 goto err_cleanup_hid_ll;
2568 error = device_create_file(&hdev->dev, &dev_attr_operation_mode);
2570 dev_err(&hdev->dev, "failed to create sysfs attributes\n");
2571 goto err_cleanup_sysfs1;
2574 if (data->status & PICOLCD_BOOTLOADER)
2575 error = picolcd_probe_bootloader(hdev, data);
2577 error = picolcd_probe_lcd(hdev, data);
2579 goto err_cleanup_sysfs2;
2581 dbg_hid(PICOLCD_NAME " activated and initialized\n");
2585 device_remove_file(&hdev->dev, &dev_attr_operation_mode);
2587 device_remove_file(&hdev->dev, &dev_attr_operation_mode_delay);
2589 hdev->ll_driver->close(hdev);
2595 hid_set_drvdata(hdev, NULL);
2600 static void picolcd_remove(struct hid_device *hdev)
2602 struct picolcd_data *data = hid_get_drvdata(hdev);
2603 unsigned long flags;
2605 dbg_hid(PICOLCD_NAME " hardware remove...\n");
2606 spin_lock_irqsave(&data->lock, flags);
2607 data->status |= PICOLCD_FAILED;
2608 spin_unlock_irqrestore(&data->lock, flags);
2610 picolcd_exit_devfs(data);
2611 device_remove_file(&hdev->dev, &dev_attr_operation_mode);
2612 device_remove_file(&hdev->dev, &dev_attr_operation_mode_delay);
2613 hdev->ll_driver->close(hdev);
2615 hid_set_drvdata(hdev, NULL);
2617 /* Shortcut potential pending reply that will never arrive */
2618 spin_lock_irqsave(&data->lock, flags);
2620 complete(&data->pending->ready);
2621 spin_unlock_irqrestore(&data->lock, flags);
2624 picolcd_exit_leds(data);
2625 /* Clean up the framebuffer */
2626 picolcd_exit_backlight(data);
2627 picolcd_exit_lcd(data);
2628 picolcd_exit_framebuffer(data);
2630 picolcd_exit_cir(data);
2631 picolcd_exit_keys(data);
2633 mutex_destroy(&data->mutex);
2634 /* Finally, clean up the picolcd data itself */
2638 static const struct hid_device_id picolcd_devices[] = {
2639 { HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP, USB_DEVICE_ID_PICOLCD) },
2640 { HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP, USB_DEVICE_ID_PICOLCD_BOOTLOADER) },
2643 MODULE_DEVICE_TABLE(hid, picolcd_devices);
2645 static struct hid_driver picolcd_driver = {
2646 .name = "hid-picolcd",
2647 .id_table = picolcd_devices,
2648 .probe = picolcd_probe,
2649 .remove = picolcd_remove,
2650 .raw_event = picolcd_raw_event,
2652 .suspend = picolcd_suspend,
2653 .resume = picolcd_resume,
2654 .reset_resume = picolcd_reset_resume,
2658 static int __init picolcd_init(void)
2660 return hid_register_driver(&picolcd_driver);
2663 static void __exit picolcd_exit(void)
2665 hid_unregister_driver(&picolcd_driver);
2668 module_init(picolcd_init);
2669 module_exit(picolcd_exit);
2670 MODULE_DESCRIPTION("Minibox graphics PicoLCD Driver");
2671 MODULE_LICENSE("GPL v2");