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 #if defined(CONFIG_FB) || defined(CONFIG_FB_MODULE)
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_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;
182 struct input_dev *input_keys;
183 struct input_dev *input_cir;
184 unsigned short keycode[PICOLCD_KEYS];
186 #if defined(CONFIG_FB) || defined(CONFIG_FB_MODULE)
187 /* Framebuffer stuff */
190 u8 *fb_vbitmap; /* local copy of what was sent to PicoLCD */
191 u8 *fb_bitmap; /* framebuffer */
192 struct fb_info *fb_info;
193 struct fb_deferred_io fb_defio;
194 #endif /* CONFIG_FB */
195 #if defined(CONFIG_LCD_CLASS_DEVICE) || defined(CONFIG_LCD_CLASS_DEVICE_MODULE)
196 struct lcd_device *lcd;
199 #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) || defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
200 struct backlight_device *backlight;
203 #endif /* CONFIG_BACKLIGHT_CLASS_DEVICE */
204 #if defined(CONFIG_LEDS_CLASS) || defined(CONFIG_LEDS_CLASS_MODULE)
207 struct led_classdev *led[8];
208 #endif /* CONFIG_LEDS_CLASS */
210 /* Housekeeping stuff */
213 struct picolcd_pending *pending;
215 #define PICOLCD_BOOTLOADER 1
216 #define PICOLCD_FAILED 2
217 #define PICOLCD_READY_FB 4
221 /* Find a given report */
222 #define picolcd_in_report(id, dev) picolcd_report(id, dev, HID_INPUT_REPORT)
223 #define picolcd_out_report(id, dev) picolcd_report(id, dev, HID_OUTPUT_REPORT)
225 static struct hid_report *picolcd_report(int id, struct hid_device *hdev, int dir)
227 struct list_head *feature_report_list = &hdev->report_enum[dir].report_list;
228 struct hid_report *report = NULL;
230 list_for_each_entry(report, feature_report_list, list) {
231 if (report->id == id)
234 dev_warn(&hdev->dev, "No report with id 0x%x found\n", id);
238 #ifdef CONFIG_DEBUG_FS
239 static void picolcd_debug_out_report(struct picolcd_data *data,
240 struct hid_device *hdev, struct hid_report *report);
241 #define usbhid_submit_report(a, b, c) \
243 picolcd_debug_out_report(hid_get_drvdata(a), a, b); \
244 usbhid_submit_report(a, b, c); \
248 /* Submit a report and wait for a reply from device - if device fades away
249 * or does not respond in time, return NULL */
250 static struct picolcd_pending *picolcd_send_and_wait(struct hid_device *hdev,
251 int report_id, const u8 *raw_data, int size)
253 struct picolcd_data *data = hid_get_drvdata(hdev);
254 struct picolcd_pending *work;
255 struct hid_report *report = picolcd_out_report(report_id, hdev);
259 if (!report || !data)
261 if (data->status & PICOLCD_FAILED)
263 work = kzalloc(sizeof(*work), GFP_KERNEL);
267 init_completion(&work->ready);
268 work->out_report = report;
269 work->in_report = NULL;
272 mutex_lock(&data->mutex);
273 spin_lock_irqsave(&data->lock, flags);
274 for (i = k = 0; i < report->maxfield; i++)
275 for (j = 0; j < report->field[i]->report_count; j++) {
276 hid_set_field(report->field[i], j, k < size ? raw_data[k] : 0);
279 data->pending = work;
280 usbhid_submit_report(data->hdev, report, USB_DIR_OUT);
281 spin_unlock_irqrestore(&data->lock, flags);
282 wait_for_completion_interruptible_timeout(&work->ready, HZ*2);
283 spin_lock_irqsave(&data->lock, flags);
284 data->pending = NULL;
285 spin_unlock_irqrestore(&data->lock, flags);
286 mutex_unlock(&data->mutex);
290 #if defined(CONFIG_FB) || defined(CONFIG_FB_MODULE)
291 /* Send a given tile to PicoLCD */
292 static int picolcd_fb_send_tile(struct hid_device *hdev, int chip, int tile)
294 struct picolcd_data *data = hid_get_drvdata(hdev);
295 struct hid_report *report1 = picolcd_out_report(REPORT_LCD_CMD_DATA, hdev);
296 struct hid_report *report2 = picolcd_out_report(REPORT_LCD_DATA, hdev);
301 if (!report1 || report1->maxfield != 1 || !report2 || report2->maxfield != 1)
304 spin_lock_irqsave(&data->lock, flags);
305 hid_set_field(report1->field[0], 0, chip << 2);
306 hid_set_field(report1->field[0], 1, 0x02);
307 hid_set_field(report1->field[0], 2, 0x00);
308 hid_set_field(report1->field[0], 3, 0x00);
309 hid_set_field(report1->field[0], 4, 0xb8 | tile);
310 hid_set_field(report1->field[0], 5, 0x00);
311 hid_set_field(report1->field[0], 6, 0x00);
312 hid_set_field(report1->field[0], 7, 0x40);
313 hid_set_field(report1->field[0], 8, 0x00);
314 hid_set_field(report1->field[0], 9, 0x00);
315 hid_set_field(report1->field[0], 10, 32);
317 hid_set_field(report2->field[0], 0, (chip << 2) | 0x01);
318 hid_set_field(report2->field[0], 1, 0x00);
319 hid_set_field(report2->field[0], 2, 0x00);
320 hid_set_field(report2->field[0], 3, 32);
322 tdata = data->fb_vbitmap + (tile * 4 + chip) * 64;
323 for (i = 0; i < 64; i++)
325 hid_set_field(report1->field[0], 11 + i, tdata[i]);
327 hid_set_field(report2->field[0], 4 + i - 32, tdata[i]);
329 usbhid_submit_report(data->hdev, report1, USB_DIR_OUT);
330 usbhid_submit_report(data->hdev, report2, USB_DIR_OUT);
331 spin_unlock_irqrestore(&data->lock, flags);
335 /* Translate a single tile*/
336 static int picolcd_fb_update_tile(u8 *vbitmap, const u8 *bitmap, int bpp,
339 int i, b, changed = 0;
341 u8 *vdata = vbitmap + (tile * 4 + chip) * 64;
344 for (b = 7; b >= 0; b--) {
345 const u8 *bdata = bitmap + tile * 256 + chip * 8 + b * 32;
346 for (i = 0; i < 64; i++) {
348 tdata[i] |= (bdata[i/8] >> (7 - i % 8)) & 0x01;
351 } else if (bpp == 8) {
352 for (b = 7; b >= 0; b--) {
353 const u8 *bdata = bitmap + (tile * 256 + chip * 8 + b * 32) * 8;
354 for (i = 0; i < 64; i++) {
356 tdata[i] |= (bdata[i] & 0x80) ? 0x01 : 0x00;
360 /* Oops, we should never get here! */
365 for (i = 0; i < 64; i++)
366 if (tdata[i] != vdata[i]) {
373 /* Reconfigure LCD display */
374 static int picolcd_fb_reset(struct picolcd_data *data, int clear)
376 struct hid_report *report = picolcd_out_report(REPORT_LCD_CMD, data->hdev);
379 static const u8 mapcmd[8] = { 0x00, 0x02, 0x00, 0x64, 0x3f, 0x00, 0x64, 0xc0 };
381 if (!report || report->maxfield != 1)
384 spin_lock_irqsave(&data->lock, flags);
385 for (i = 0; i < 4; i++) {
386 for (j = 0; j < report->field[0]->maxusage; j++)
388 hid_set_field(report->field[0], j, i << 2);
389 else if (j < sizeof(mapcmd))
390 hid_set_field(report->field[0], j, mapcmd[j]);
392 hid_set_field(report->field[0], j, 0);
393 usbhid_submit_report(data->hdev, report, USB_DIR_OUT);
396 data->status |= PICOLCD_READY_FB;
397 spin_unlock_irqrestore(&data->lock, flags);
399 if (data->fb_bitmap) {
401 memset(data->fb_vbitmap, 0xff, PICOLCDFB_SIZE);
402 memset(data->fb_bitmap, 0, PICOLCDFB_SIZE*data->fb_bpp);
404 /* invert 1 byte in each tile to force resend */
405 for (i = 0; i < PICOLCDFB_SIZE; i += 64)
406 data->fb_vbitmap[i] = ~data->fb_vbitmap[i];
410 /* schedule first output of framebuffer */
412 schedule_delayed_work(&data->fb_info->deferred_work, 0);
417 /* Update fb_vbitmap from the screen_base and send changed tiles to device */
418 static void picolcd_fb_update(struct picolcd_data *data)
423 spin_lock_irqsave(&data->lock, flags);
424 if (!(data->status & PICOLCD_READY_FB)) {
425 spin_unlock_irqrestore(&data->lock, flags);
426 picolcd_fb_reset(data, 0);
428 spin_unlock_irqrestore(&data->lock, flags);
432 * Translate the framebuffer into the format needed by the PicoLCD.
433 * See display layout above.
434 * Do this one tile after the other and push those tiles that changed.
436 * Wait for our IO to complete as otherwise we might flood the queue!
439 for (chip = 0; chip < 4; chip++)
440 for (tile = 0; tile < 8; tile++)
441 if (picolcd_fb_update_tile(data->fb_vbitmap,
442 data->fb_bitmap, data->fb_bpp, chip, tile)) {
444 if (n >= HID_OUTPUT_FIFO_SIZE / 2) {
445 usbhid_wait_io(data->hdev);
448 picolcd_fb_send_tile(data->hdev, chip, tile);
451 usbhid_wait_io(data->hdev);
454 /* Stub to call the system default and update the image on the picoLCD */
455 static void picolcd_fb_fillrect(struct fb_info *info,
456 const struct fb_fillrect *rect)
460 sys_fillrect(info, rect);
462 schedule_delayed_work(&info->deferred_work, 0);
465 /* Stub to call the system default and update the image on the picoLCD */
466 static void picolcd_fb_copyarea(struct fb_info *info,
467 const struct fb_copyarea *area)
471 sys_copyarea(info, area);
473 schedule_delayed_work(&info->deferred_work, 0);
476 /* Stub to call the system default and update the image on the picoLCD */
477 static void picolcd_fb_imageblit(struct fb_info *info, const struct fb_image *image)
481 sys_imageblit(info, image);
483 schedule_delayed_work(&info->deferred_work, 0);
487 * this is the slow path from userspace. they can seek and write to
488 * the fb. it's inefficient to do anything less than a full screen draw
490 static ssize_t picolcd_fb_write(struct fb_info *info, const char __user *buf,
491 size_t count, loff_t *ppos)
496 ret = fb_sys_write(info, buf, count, ppos);
498 schedule_delayed_work(&info->deferred_work, 0);
502 static int picolcd_fb_blank(int blank, struct fb_info *info)
506 /* We let fb notification do this for us via lcd/backlight device */
510 static void picolcd_fb_destroy(struct fb_info *info)
512 struct picolcd_data *data = info->par;
515 data->fb_info = NULL;
516 fb_deferred_io_cleanup(info);
517 framebuffer_release(info);
520 static int picolcd_fb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
522 __u32 bpp = var->bits_per_pixel;
523 __u32 activate = var->activate;
525 /* only allow 1/8 bit depth (8-bit is grayscale) */
526 *var = picolcdfb_var;
527 var->activate = activate;
529 var->bits_per_pixel = 8;
531 var->bits_per_pixel = 1;
535 static int picolcd_set_par(struct fb_info *info)
537 struct picolcd_data *data = info->par;
539 if (info->var.bits_per_pixel == data->fb_bpp)
541 /* switch between 1/8 bit depths */
542 if (info->var.bits_per_pixel != 1 && info->var.bits_per_pixel != 8)
545 o_fb = data->fb_bitmap;
546 n_fb = vmalloc(PICOLCDFB_SIZE*info->var.bits_per_pixel);
550 fb_deferred_io_cleanup(info);
551 /* translate FB content to new bits-per-pixel */
552 if (info->var.bits_per_pixel == 1) {
554 for (i = 0; i < PICOLCDFB_SIZE; i++) {
556 for (b = 0; b < 8; b++) {
558 p |= o_fb[i*8+b] ? 0x01 : 0x00;
561 info->fix.visual = FB_VISUAL_MONO01;
562 info->fix.line_length = PICOLCDFB_WIDTH / 8;
565 for (i = 0; i < PICOLCDFB_SIZE * 8; i++)
566 n_fb[i] = o_fb[i/8] & (0x01 << (7 - i % 8)) ? 0xff : 0x00;
567 info->fix.visual = FB_VISUAL_TRUECOLOR;
568 info->fix.line_length = PICOLCDFB_WIDTH;
571 data->fb_bitmap = n_fb;
572 data->fb_bpp = info->var.bits_per_pixel;
573 info->screen_base = (char __force __iomem *)n_fb;
574 info->fix.smem_start = (unsigned long)n_fb;
575 info->fix.smem_len = PICOLCDFB_SIZE*data->fb_bpp;
576 fb_deferred_io_init(info);
581 /* Note this can't be const because of struct fb_info definition */
582 static struct fb_ops picolcdfb_ops = {
583 .owner = THIS_MODULE,
584 .fb_destroy = picolcd_fb_destroy,
585 .fb_read = fb_sys_read,
586 .fb_write = picolcd_fb_write,
587 .fb_blank = picolcd_fb_blank,
588 .fb_fillrect = picolcd_fb_fillrect,
589 .fb_copyarea = picolcd_fb_copyarea,
590 .fb_imageblit = picolcd_fb_imageblit,
591 .fb_check_var = picolcd_fb_check_var,
592 .fb_set_par = picolcd_set_par,
596 /* Callback from deferred IO workqueue */
597 static void picolcd_fb_deferred_io(struct fb_info *info, struct list_head *pagelist)
599 picolcd_fb_update(info->par);
602 static const struct fb_deferred_io picolcd_fb_defio = {
603 .delay = HZ / PICOLCDFB_UPDATE_RATE_DEFAULT,
604 .deferred_io = picolcd_fb_deferred_io,
609 * The "fb_update_rate" sysfs attribute
611 static ssize_t picolcd_fb_update_rate_show(struct device *dev,
612 struct device_attribute *attr, char *buf)
614 struct picolcd_data *data = dev_get_drvdata(dev);
615 unsigned i, fb_update_rate = data->fb_update_rate;
618 for (i = 1; i <= PICOLCDFB_UPDATE_RATE_LIMIT; i++)
619 if (ret >= PAGE_SIZE)
621 else if (i == fb_update_rate)
622 ret += snprintf(buf+ret, PAGE_SIZE-ret, "[%u] ", i);
624 ret += snprintf(buf+ret, PAGE_SIZE-ret, "%u ", i);
626 buf[min(ret, (size_t)PAGE_SIZE)-1] = '\n';
630 static ssize_t picolcd_fb_update_rate_store(struct device *dev,
631 struct device_attribute *attr, const char *buf, size_t count)
633 struct picolcd_data *data = dev_get_drvdata(dev);
637 if (count < 1 || count > 10)
640 i = sscanf(buf, "%u", &u);
644 if (u > PICOLCDFB_UPDATE_RATE_LIMIT)
647 u = PICOLCDFB_UPDATE_RATE_DEFAULT;
649 data->fb_update_rate = u;
650 data->fb_defio.delay = HZ / data->fb_update_rate;
654 static DEVICE_ATTR(fb_update_rate, 0666, picolcd_fb_update_rate_show,
655 picolcd_fb_update_rate_store);
657 /* initialize Framebuffer device */
658 static int picolcd_init_framebuffer(struct picolcd_data *data)
660 struct device *dev = &data->hdev->dev;
661 struct fb_info *info = NULL;
663 u8 *fb_vbitmap = NULL;
664 u8 *fb_bitmap = NULL;
666 fb_bitmap = vmalloc(PICOLCDFB_SIZE*picolcdfb_var.bits_per_pixel);
667 if (fb_bitmap == NULL) {
668 dev_err(dev, "can't get a free page for framebuffer\n");
672 fb_vbitmap = kmalloc(PICOLCDFB_SIZE, GFP_KERNEL);
673 if (fb_vbitmap == NULL) {
674 dev_err(dev, "can't alloc vbitmap image buffer\n");
678 data->fb_update_rate = PICOLCDFB_UPDATE_RATE_DEFAULT;
679 data->fb_defio = picolcd_fb_defio;
680 info = framebuffer_alloc(0, dev);
682 dev_err(dev, "failed to allocate a framebuffer\n");
686 info->fbdefio = &data->fb_defio;
687 info->screen_base = (char __force __iomem *)fb_bitmap;
688 info->fbops = &picolcdfb_ops;
689 info->var = picolcdfb_var;
690 info->fix = picolcdfb_fix;
691 info->fix.smem_len = PICOLCDFB_SIZE;
692 info->fix.smem_start = (unsigned long)fb_bitmap;
694 info->flags = FBINFO_FLAG_DEFAULT;
696 data->fb_vbitmap = fb_vbitmap;
697 data->fb_bitmap = fb_bitmap;
698 data->fb_bpp = picolcdfb_var.bits_per_pixel;
699 error = picolcd_fb_reset(data, 1);
701 dev_err(dev, "failed to configure display\n");
704 error = device_create_file(dev, &dev_attr_fb_update_rate);
706 dev_err(dev, "failed to create sysfs attributes\n");
709 data->fb_info = info;
710 error = register_framebuffer(info);
712 dev_err(dev, "failed to register framebuffer\n");
715 fb_deferred_io_init(info);
716 /* schedule first output of framebuffer */
717 schedule_delayed_work(&info->deferred_work, 0);
721 device_remove_file(dev, &dev_attr_fb_update_rate);
723 data->fb_vbitmap = NULL;
724 data->fb_bitmap = NULL;
726 data->fb_info = NULL;
729 framebuffer_release(info);
735 static void picolcd_exit_framebuffer(struct picolcd_data *data)
737 struct fb_info *info = data->fb_info;
738 u8 *fb_vbitmap = data->fb_vbitmap;
739 u8 *fb_bitmap = data->fb_bitmap;
744 data->fb_vbitmap = NULL;
745 data->fb_bitmap = NULL;
747 data->fb_info = NULL;
748 device_remove_file(&data->hdev->dev, &dev_attr_fb_update_rate);
749 fb_deferred_io_cleanup(info);
750 unregister_framebuffer(info);
755 #define picolcd_fbinfo(d) ((d)->fb_info)
757 static inline int picolcd_fb_reset(struct picolcd_data *data, int clear)
761 static inline int picolcd_init_framebuffer(struct picolcd_data *data)
765 static void picolcd_exit_framebuffer(struct picolcd_data *data)
768 #define picolcd_fbinfo(d) NULL
769 #endif /* CONFIG_FB */
771 #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) || defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
773 * backlight class device
775 static int picolcd_get_brightness(struct backlight_device *bdev)
777 struct picolcd_data *data = bl_get_data(bdev);
778 return data->lcd_brightness;
781 static int picolcd_set_brightness(struct backlight_device *bdev)
783 struct picolcd_data *data = bl_get_data(bdev);
784 struct hid_report *report = picolcd_out_report(REPORT_BRIGHTNESS, data->hdev);
787 if (!report || report->maxfield != 1 || report->field[0]->report_count != 1)
790 data->lcd_brightness = bdev->props.brightness & 0x0ff;
791 data->lcd_power = bdev->props.power;
792 spin_lock_irqsave(&data->lock, flags);
793 hid_set_field(report->field[0], 0, data->lcd_power == FB_BLANK_UNBLANK ? data->lcd_brightness : 0);
794 usbhid_submit_report(data->hdev, report, USB_DIR_OUT);
795 spin_unlock_irqrestore(&data->lock, flags);
799 static int picolcd_check_bl_fb(struct backlight_device *bdev, struct fb_info *fb)
801 return fb && fb == picolcd_fbinfo((struct picolcd_data *)bl_get_data(bdev));
804 static const struct backlight_ops picolcd_blops = {
805 .update_status = picolcd_set_brightness,
806 .get_brightness = picolcd_get_brightness,
807 .check_fb = picolcd_check_bl_fb,
810 static int picolcd_init_backlight(struct picolcd_data *data, struct hid_report *report)
812 struct device *dev = &data->hdev->dev;
813 struct backlight_device *bdev;
814 struct backlight_properties props;
817 if (report->maxfield != 1 || report->field[0]->report_count != 1 ||
818 report->field[0]->report_size != 8) {
819 dev_err(dev, "unsupported BRIGHTNESS report");
823 memset(&props, 0, sizeof(props));
824 props.max_brightness = 0xff;
825 bdev = backlight_device_register(dev_name(dev), dev, data,
826 &picolcd_blops, &props);
828 dev_err(dev, "failed to register backlight\n");
829 return PTR_ERR(bdev);
831 bdev->props.brightness = 0xff;
832 data->lcd_brightness = 0xff;
833 data->backlight = bdev;
834 picolcd_set_brightness(bdev);
838 static void picolcd_exit_backlight(struct picolcd_data *data)
840 struct backlight_device *bdev = data->backlight;
842 data->backlight = NULL;
844 backlight_device_unregister(bdev);
847 static inline int picolcd_resume_backlight(struct picolcd_data *data)
849 if (!data->backlight)
851 return picolcd_set_brightness(data->backlight);
855 static inline int picolcd_init_backlight(struct picolcd_data *data,
856 struct hid_report *report)
860 static inline void picolcd_exit_backlight(struct picolcd_data *data)
863 static inline int picolcd_resume_backlight(struct picolcd_data *data)
867 #endif /* CONFIG_BACKLIGHT_CLASS_DEVICE */
869 #if defined(CONFIG_LCD_CLASS_DEVICE) || defined(CONFIG_LCD_CLASS_DEVICE_MODULE)
873 static int picolcd_get_contrast(struct lcd_device *ldev)
875 struct picolcd_data *data = lcd_get_data(ldev);
876 return data->lcd_contrast;
879 static int picolcd_set_contrast(struct lcd_device *ldev, int contrast)
881 struct picolcd_data *data = lcd_get_data(ldev);
882 struct hid_report *report = picolcd_out_report(REPORT_CONTRAST, data->hdev);
885 if (!report || report->maxfield != 1 || report->field[0]->report_count != 1)
888 data->lcd_contrast = contrast & 0x0ff;
889 spin_lock_irqsave(&data->lock, flags);
890 hid_set_field(report->field[0], 0, data->lcd_contrast);
891 usbhid_submit_report(data->hdev, report, USB_DIR_OUT);
892 spin_unlock_irqrestore(&data->lock, flags);
896 static int picolcd_check_lcd_fb(struct lcd_device *ldev, struct fb_info *fb)
898 return fb && fb == picolcd_fbinfo((struct picolcd_data *)lcd_get_data(ldev));
901 static struct lcd_ops picolcd_lcdops = {
902 .get_contrast = picolcd_get_contrast,
903 .set_contrast = picolcd_set_contrast,
904 .check_fb = picolcd_check_lcd_fb,
907 static int picolcd_init_lcd(struct picolcd_data *data, struct hid_report *report)
909 struct device *dev = &data->hdev->dev;
910 struct lcd_device *ldev;
914 if (report->maxfield != 1 || report->field[0]->report_count != 1 ||
915 report->field[0]->report_size != 8) {
916 dev_err(dev, "unsupported CONTRAST report");
920 ldev = lcd_device_register(dev_name(dev), dev, data, &picolcd_lcdops);
922 dev_err(dev, "failed to register LCD\n");
923 return PTR_ERR(ldev);
925 ldev->props.max_contrast = 0x0ff;
926 data->lcd_contrast = 0xe5;
928 picolcd_set_contrast(ldev, 0xe5);
932 static void picolcd_exit_lcd(struct picolcd_data *data)
934 struct lcd_device *ldev = data->lcd;
938 lcd_device_unregister(ldev);
941 static inline int picolcd_resume_lcd(struct picolcd_data *data)
945 return picolcd_set_contrast(data->lcd, data->lcd_contrast);
948 static inline int picolcd_init_lcd(struct picolcd_data *data,
949 struct hid_report *report)
953 static inline void picolcd_exit_lcd(struct picolcd_data *data)
956 static inline int picolcd_resume_lcd(struct picolcd_data *data)
960 #endif /* CONFIG_LCD_CLASS_DEVICE */
962 #if defined(CONFIG_LEDS_CLASS) || defined(CONFIG_LEDS_CLASS_MODULE)
966 static void picolcd_leds_set(struct picolcd_data *data)
968 struct hid_report *report;
973 report = picolcd_out_report(REPORT_LED_STATE, data->hdev);
974 if (!report || report->maxfield != 1 || report->field[0]->report_count != 1)
977 spin_lock_irqsave(&data->lock, flags);
978 hid_set_field(report->field[0], 0, data->led_state);
979 usbhid_submit_report(data->hdev, report, USB_DIR_OUT);
980 spin_unlock_irqrestore(&data->lock, flags);
983 static void picolcd_led_set_brightness(struct led_classdev *led_cdev,
984 enum led_brightness value)
987 struct hid_device *hdev;
988 struct picolcd_data *data;
991 dev = led_cdev->dev->parent;
992 hdev = container_of(dev, struct hid_device, dev);
993 data = hid_get_drvdata(hdev);
994 for (i = 0; i < 8; i++) {
995 if (led_cdev != data->led[i])
997 state = (data->led_state >> i) & 1;
998 if (value == LED_OFF && state) {
999 data->led_state &= ~(1 << i);
1000 picolcd_leds_set(data);
1001 } else if (value != LED_OFF && !state) {
1002 data->led_state |= 1 << i;
1003 picolcd_leds_set(data);
1009 static enum led_brightness picolcd_led_get_brightness(struct led_classdev *led_cdev)
1012 struct hid_device *hdev;
1013 struct picolcd_data *data;
1016 dev = led_cdev->dev->parent;
1017 hdev = container_of(dev, struct hid_device, dev);
1018 data = hid_get_drvdata(hdev);
1019 for (i = 0; i < 8; i++)
1020 if (led_cdev == data->led[i]) {
1021 value = (data->led_state >> i) & 1;
1024 return value ? LED_FULL : LED_OFF;
1027 static int picolcd_init_leds(struct picolcd_data *data, struct hid_report *report)
1029 struct device *dev = &data->hdev->dev;
1030 struct led_classdev *led;
1031 size_t name_sz = strlen(dev_name(dev)) + 8;
1037 if (report->maxfield != 1 || report->field[0]->report_count != 1 ||
1038 report->field[0]->report_size != 8) {
1039 dev_err(dev, "unsupported LED_STATE report");
1043 for (i = 0; i < 8; i++) {
1044 led = kzalloc(sizeof(struct led_classdev)+name_sz, GFP_KERNEL);
1046 dev_err(dev, "can't allocate memory for LED %d\n", i);
1050 name = (void *)(&led[1]);
1051 snprintf(name, name_sz, "%s::GPO%d", dev_name(dev), i);
1053 led->brightness = 0;
1054 led->max_brightness = 1;
1055 led->brightness_get = picolcd_led_get_brightness;
1056 led->brightness_set = picolcd_led_set_brightness;
1059 ret = led_classdev_register(dev, data->led[i]);
1061 data->led[i] = NULL;
1063 dev_err(dev, "can't register LED %d\n", i);
1069 for (i = 0; i < 8; i++)
1072 data->led[i] = NULL;
1073 led_classdev_unregister(led);
1079 static void picolcd_exit_leds(struct picolcd_data *data)
1081 struct led_classdev *led;
1084 for (i = 0; i < 8; i++) {
1086 data->led[i] = NULL;
1089 led_classdev_unregister(led);
1095 static inline int picolcd_init_leds(struct picolcd_data *data,
1096 struct hid_report *report)
1100 static void picolcd_exit_leds(struct picolcd_data *data)
1103 static inline int picolcd_leds_set(struct picolcd_data *data)
1107 #endif /* CONFIG_LEDS_CLASS */
1110 * input class device
1112 static int picolcd_raw_keypad(struct picolcd_data *data,
1113 struct hid_report *report, u8 *raw_data, int size)
1117 * First and second data bytes list currently pressed keys,
1118 * 0x00 means no key and at most 2 keys may be pressed at same time
1122 /* determine newly pressed keys */
1123 for (i = 0; i < size; i++) {
1124 unsigned int key_code;
1125 if (raw_data[i] == 0)
1127 for (j = 0; j < sizeof(data->pressed_keys); j++)
1128 if (data->pressed_keys[j] == raw_data[i])
1129 goto key_already_down;
1130 for (j = 0; j < sizeof(data->pressed_keys); j++)
1131 if (data->pressed_keys[j] == 0) {
1132 data->pressed_keys[j] = raw_data[i];
1135 input_event(data->input_keys, EV_MSC, MSC_SCAN, raw_data[i]);
1136 if (raw_data[i] < PICOLCD_KEYS)
1137 key_code = data->keycode[raw_data[i]];
1139 key_code = KEY_UNKNOWN;
1140 if (key_code != KEY_UNKNOWN) {
1141 dbg_hid(PICOLCD_NAME " got key press for %u:%d",
1142 raw_data[i], key_code);
1143 input_report_key(data->input_keys, key_code, 1);
1145 input_sync(data->input_keys);
1150 /* determine newly released keys */
1151 for (j = 0; j < sizeof(data->pressed_keys); j++) {
1152 unsigned int key_code;
1153 if (data->pressed_keys[j] == 0)
1155 for (i = 0; i < size; i++)
1156 if (data->pressed_keys[j] == raw_data[i])
1157 goto key_still_down;
1158 input_event(data->input_keys, EV_MSC, MSC_SCAN, data->pressed_keys[j]);
1159 if (data->pressed_keys[j] < PICOLCD_KEYS)
1160 key_code = data->keycode[data->pressed_keys[j]];
1162 key_code = KEY_UNKNOWN;
1163 if (key_code != KEY_UNKNOWN) {
1164 dbg_hid(PICOLCD_NAME " got key release for %u:%d",
1165 data->pressed_keys[j], key_code);
1166 input_report_key(data->input_keys, key_code, 0);
1168 input_sync(data->input_keys);
1169 data->pressed_keys[j] = 0;
1176 static int picolcd_raw_cir(struct picolcd_data *data,
1177 struct hid_report *report, u8 *raw_data, int size)
1179 /* Need understanding of CIR data format to implement ... */
1183 static int picolcd_check_version(struct hid_device *hdev)
1185 struct picolcd_data *data = hid_get_drvdata(hdev);
1186 struct picolcd_pending *verinfo;
1192 verinfo = picolcd_send_and_wait(hdev, REPORT_VERSION, NULL, 0);
1194 dev_err(&hdev->dev, "no version response from PicoLCD");
1198 if (verinfo->raw_size == 2) {
1199 if (data->status & PICOLCD_BOOTLOADER) {
1200 dev_info(&hdev->dev, "PicoLCD, bootloader version %d.%d\n",
1201 verinfo->raw_data[0], verinfo->raw_data[1]);
1202 data->version[0] = verinfo->raw_data[0];
1203 data->version[1] = verinfo->raw_data[1];
1205 dev_info(&hdev->dev, "PicoLCD, firmware version %d.%d\n",
1206 verinfo->raw_data[1], verinfo->raw_data[0]);
1207 data->version[0] = verinfo->raw_data[1];
1208 data->version[1] = verinfo->raw_data[0];
1211 dev_err(&hdev->dev, "confused, got unexpected version response from PicoLCD\n");
1219 * Reset our device and wait for answer to VERSION request
1221 static int picolcd_reset(struct hid_device *hdev)
1223 struct picolcd_data *data = hid_get_drvdata(hdev);
1224 struct hid_report *report = picolcd_out_report(REPORT_RESET, hdev);
1225 unsigned long flags;
1228 if (!data || !report || report->maxfield != 1)
1231 spin_lock_irqsave(&data->lock, flags);
1232 if (hdev->product == USB_DEVICE_ID_PICOLCD_BOOTLOADER)
1233 data->status |= PICOLCD_BOOTLOADER;
1235 /* perform the reset */
1236 hid_set_field(report->field[0], 0, 1);
1237 usbhid_submit_report(hdev, report, USB_DIR_OUT);
1238 spin_unlock_irqrestore(&data->lock, flags);
1240 error = picolcd_check_version(hdev);
1244 picolcd_resume_lcd(data);
1245 picolcd_resume_backlight(data);
1246 #if defined(CONFIG_FB) || defined(CONFIG_FB_MODULE)
1248 schedule_delayed_work(&data->fb_info->deferred_work, 0);
1249 #endif /* CONFIG_FB */
1251 picolcd_leds_set(data);
1256 * The "operation_mode" sysfs attribute
1258 static ssize_t picolcd_operation_mode_show(struct device *dev,
1259 struct device_attribute *attr, char *buf)
1261 struct picolcd_data *data = dev_get_drvdata(dev);
1263 if (data->status & PICOLCD_BOOTLOADER)
1264 return snprintf(buf, PAGE_SIZE, "[bootloader] lcd\n");
1266 return snprintf(buf, PAGE_SIZE, "bootloader [lcd]\n");
1269 static ssize_t picolcd_operation_mode_store(struct device *dev,
1270 struct device_attribute *attr, const char *buf, size_t count)
1272 struct picolcd_data *data = dev_get_drvdata(dev);
1273 struct hid_report *report = NULL;
1277 unsigned long flags;
1279 if (cnt >= 3 && strncmp("lcd", buf, 3) == 0) {
1280 if (data->status & PICOLCD_BOOTLOADER)
1281 report = picolcd_out_report(REPORT_EXIT_FLASHER, data->hdev);
1284 } else if (cnt >= 10 && strncmp("bootloader", buf, 10) == 0) {
1285 if (!(data->status & PICOLCD_BOOTLOADER))
1286 report = picolcd_out_report(REPORT_EXIT_KEYBOARD, data->hdev);
1293 while (cnt > 0 && (*buf == ' ' || *buf == '\t')) {
1297 while (cnt > 0 && (buf[cnt-1] == '\n' || buf[cnt-1] == '\r'))
1300 if (sscanf(buf, "%u", &u) != 1)
1308 spin_lock_irqsave(&data->lock, flags);
1309 hid_set_field(report->field[0], 0, timeout & 0xff);
1310 hid_set_field(report->field[0], 1, (timeout >> 8) & 0xff);
1311 usbhid_submit_report(data->hdev, report, USB_DIR_OUT);
1312 spin_unlock_irqrestore(&data->lock, flags);
1316 static DEVICE_ATTR(operation_mode, 0644, picolcd_operation_mode_show,
1317 picolcd_operation_mode_store);
1320 #ifdef CONFIG_DEBUG_FS
1324 static int picolcd_debug_reset_show(struct seq_file *f, void *p)
1326 if (picolcd_fbinfo((struct picolcd_data *)f->private))
1327 seq_printf(f, "all fb\n");
1329 seq_printf(f, "all\n");
1333 static int picolcd_debug_reset_open(struct inode *inode, struct file *f)
1335 return single_open(f, picolcd_debug_reset_show, inode->i_private);
1338 static ssize_t picolcd_debug_reset_write(struct file *f, const char __user *user_buf,
1339 size_t count, loff_t *ppos)
1341 struct picolcd_data *data = ((struct seq_file *)f->private_data)->private;
1343 size_t cnt = min(count, sizeof(buf)-1);
1344 if (copy_from_user(buf, user_buf, cnt))
1347 while (cnt > 0 && (buf[cnt-1] == ' ' || buf[cnt-1] == '\n'))
1350 if (strcmp(buf, "all") == 0) {
1351 picolcd_reset(data->hdev);
1352 picolcd_fb_reset(data, 1);
1353 } else if (strcmp(buf, "fb") == 0) {
1354 picolcd_fb_reset(data, 1);
1361 static const struct file_operations picolcd_debug_reset_fops = {
1362 .owner = THIS_MODULE,
1363 .open = picolcd_debug_reset_open,
1365 .llseek = seq_lseek,
1366 .write = picolcd_debug_reset_write,
1367 .release = single_release,
1373 static int picolcd_debug_eeprom_open(struct inode *i, struct file *f)
1375 f->private_data = i->i_private;
1379 static ssize_t picolcd_debug_eeprom_read(struct file *f, char __user *u,
1380 size_t s, loff_t *off)
1382 struct picolcd_data *data = f->private_data;
1383 struct picolcd_pending *resp;
1392 /* prepare buffer with info about what we want to read (addr & len) */
1393 raw_data[0] = *off & 0xff;
1394 raw_data[1] = (*off >> 8) && 0xff;
1395 raw_data[2] = s < 20 ? s : 20;
1396 if (*off + raw_data[2] > 0xff)
1397 raw_data[2] = 0x100 - *off;
1398 resp = picolcd_send_and_wait(data->hdev, REPORT_EE_READ, raw_data,
1403 if (resp->in_report && resp->in_report->id == REPORT_EE_DATA) {
1404 /* successful read :) */
1405 ret = resp->raw_data[2];
1408 if (copy_to_user(u, resp->raw_data+3, ret))
1412 } /* anything else is some kind of IO error */
1418 static ssize_t picolcd_debug_eeprom_write(struct file *f, const char __user *u,
1419 size_t s, loff_t *off)
1421 struct picolcd_data *data = f->private_data;
1422 struct picolcd_pending *resp;
1431 memset(raw_data, 0, sizeof(raw_data));
1432 raw_data[0] = *off & 0xff;
1433 raw_data[1] = (*off >> 8) && 0xff;
1434 raw_data[2] = s < 20 ? s : 20;
1435 if (*off + raw_data[2] > 0xff)
1436 raw_data[2] = 0x100 - *off;
1438 if (copy_from_user(raw_data+3, u, raw_data[2]))
1440 resp = picolcd_send_and_wait(data->hdev, REPORT_EE_WRITE, raw_data,
1446 if (resp->in_report && resp->in_report->id == REPORT_EE_DATA) {
1447 /* check if written data matches */
1448 if (memcmp(raw_data, resp->raw_data, 3+raw_data[2]) == 0) {
1449 *off += raw_data[2];
1459 * - read/write happens in chunks of at most 20 bytes, it's up to userspace
1460 * to loop in order to get more data.
1461 * - on write errors on otherwise correct write request the bytes
1462 * that should have been written are in undefined state.
1464 static const struct file_operations picolcd_debug_eeprom_fops = {
1465 .owner = THIS_MODULE,
1466 .open = picolcd_debug_eeprom_open,
1467 .read = picolcd_debug_eeprom_read,
1468 .write = picolcd_debug_eeprom_write,
1469 .llseek = generic_file_llseek,
1475 static int picolcd_debug_flash_open(struct inode *i, struct file *f)
1477 f->private_data = i->i_private;
1481 /* record a flash address to buf (bounds check to be done by caller) */
1482 static int _picolcd_flash_setaddr(struct picolcd_data *data, u8 *buf, long off)
1484 buf[0] = off & 0xff;
1485 buf[1] = (off >> 8) & 0xff;
1486 if (data->addr_sz == 3)
1487 buf[2] = (off >> 16) & 0xff;
1488 return data->addr_sz == 2 ? 2 : 3;
1491 /* read a given size of data (bounds check to be done by caller) */
1492 static ssize_t _picolcd_flash_read(struct picolcd_data *data, int report_id,
1493 char __user *u, size_t s, loff_t *off)
1495 struct picolcd_pending *resp;
1498 int len_off, err = -EIO;
1502 len_off = _picolcd_flash_setaddr(data, raw_data, *off);
1503 raw_data[len_off] = s > 32 ? 32 : s;
1504 resp = picolcd_send_and_wait(data->hdev, report_id, raw_data, len_off+1);
1505 if (!resp || !resp->in_report)
1507 if (resp->in_report->id == REPORT_MEMORY ||
1508 resp->in_report->id == REPORT_BL_READ_MEMORY) {
1509 if (memcmp(raw_data, resp->raw_data, len_off+1) != 0)
1511 if (copy_to_user(u+ret, resp->raw_data+len_off+1, raw_data[len_off])) {
1515 *off += raw_data[len_off];
1516 s -= raw_data[len_off];
1517 ret += raw_data[len_off];
1523 return ret > 0 ? ret : err;
1528 static ssize_t picolcd_debug_flash_read(struct file *f, char __user *u,
1529 size_t s, loff_t *off)
1531 struct picolcd_data *data = f->private_data;
1537 if (*off + s > 0x05fff)
1540 if (data->status & PICOLCD_BOOTLOADER)
1541 return _picolcd_flash_read(data, REPORT_BL_READ_MEMORY, u, s, off);
1543 return _picolcd_flash_read(data, REPORT_READ_MEMORY, u, s, off);
1546 /* erase block aligned to 64bytes boundary */
1547 static ssize_t _picolcd_flash_erase64(struct picolcd_data *data, int report_id,
1550 struct picolcd_pending *resp;
1558 len_off = _picolcd_flash_setaddr(data, raw_data, *off);
1559 resp = picolcd_send_and_wait(data->hdev, report_id, raw_data, len_off);
1560 if (!resp || !resp->in_report)
1562 if (resp->in_report->id == REPORT_MEMORY ||
1563 resp->in_report->id == REPORT_BL_ERASE_MEMORY) {
1564 if (memcmp(raw_data, resp->raw_data, len_off) != 0)
1573 /* write a given size of data (bounds check to be done by caller) */
1574 static ssize_t _picolcd_flash_write(struct picolcd_data *data, int report_id,
1575 const char __user *u, size_t s, loff_t *off)
1577 struct picolcd_pending *resp;
1580 int len_off, err = -EIO;
1584 len_off = _picolcd_flash_setaddr(data, raw_data, *off);
1585 raw_data[len_off] = s > 32 ? 32 : s;
1586 if (copy_from_user(raw_data+len_off+1, u, raw_data[len_off])) {
1590 resp = picolcd_send_and_wait(data->hdev, report_id, raw_data,
1591 len_off+1+raw_data[len_off]);
1592 if (!resp || !resp->in_report)
1594 if (resp->in_report->id == REPORT_MEMORY ||
1595 resp->in_report->id == REPORT_BL_WRITE_MEMORY) {
1596 if (memcmp(raw_data, resp->raw_data, len_off+1+raw_data[len_off]) != 0)
1598 *off += raw_data[len_off];
1599 s -= raw_data[len_off];
1600 ret += raw_data[len_off];
1608 return ret > 0 ? ret : err;
1611 static ssize_t picolcd_debug_flash_write(struct file *f, const char __user *u,
1612 size_t s, loff_t *off)
1614 struct picolcd_data *data = f->private_data;
1615 ssize_t err, ret = 0;
1616 int report_erase, report_write;
1627 if (data->status & PICOLCD_BOOTLOADER) {
1628 report_erase = REPORT_BL_ERASE_MEMORY;
1629 report_write = REPORT_BL_WRITE_MEMORY;
1631 report_erase = REPORT_ERASE_MEMORY;
1632 report_write = REPORT_WRITE_MEMORY;
1634 mutex_lock(&data->mutex_flash);
1636 err = _picolcd_flash_erase64(data, report_erase, off);
1639 err = _picolcd_flash_write(data, report_write, u, 64, off);
1648 mutex_unlock(&data->mutex_flash);
1649 return ret > 0 ? ret : err;
1654 * - concurrent writing is prevented by mutex and all writes must be
1655 * n*64 bytes and 64-byte aligned, each write being preceeded by an
1656 * ERASE which erases a 64byte block.
1657 * If less than requested was written or an error is returned for an
1658 * otherwise correct write request the next 64-byte block which should
1659 * have been written is in undefined state (mostly: original, erased,
1660 * (half-)written with write error)
1661 * - reading can happend without special restriction
1663 static const struct file_operations picolcd_debug_flash_fops = {
1664 .owner = THIS_MODULE,
1665 .open = picolcd_debug_flash_open,
1666 .read = picolcd_debug_flash_read,
1667 .write = picolcd_debug_flash_write,
1668 .llseek = generic_file_llseek,
1673 * Helper code for HID report level dumping/debugging
1675 static const char *error_codes[] = {
1676 "success", "parameter missing", "data_missing", "block readonly",
1677 "block not erasable", "block too big", "section overflow",
1678 "invalid command length", "invalid data length",
1681 static void dump_buff_as_hex(char *dst, size_t dst_sz, const u8 *data,
1682 const size_t data_len)
1685 for (i = j = 0; i < data_len && j + 3 < dst_sz; i++) {
1686 dst[j++] = hex_asc[(data[i] >> 4) & 0x0f];
1687 dst[j++] = hex_asc[data[i] & 0x0f];
1697 static void picolcd_debug_out_report(struct picolcd_data *data,
1698 struct hid_device *hdev, struct hid_report *report)
1701 int raw_size = (report->size >> 3) + 1;
1705 /* Avoid unnecessary overhead if debugfs is disabled */
1706 if (!hdev->debug_events)
1709 buff = kmalloc(BUFF_SZ, GFP_ATOMIC);
1713 snprintf(buff, BUFF_SZ, "\nout report %d (size %d) = ",
1714 report->id, raw_size);
1715 hid_debug_event(hdev, buff);
1716 if (raw_size + 5 > sizeof(raw_data)) {
1717 hid_debug_event(hdev, " TOO BIG\n");
1720 raw_data[0] = report->id;
1721 hid_output_report(report, raw_data);
1722 dump_buff_as_hex(buff, BUFF_SZ, raw_data, raw_size);
1723 hid_debug_event(hdev, buff);
1726 switch (report->id) {
1727 case REPORT_LED_STATE:
1728 /* 1 data byte with GPO state */
1729 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1730 "REPORT_LED_STATE", report->id, raw_size-1);
1731 hid_debug_event(hdev, buff);
1732 snprintf(buff, BUFF_SZ, "\tGPO state: 0x%02x\n", raw_data[1]);
1733 hid_debug_event(hdev, buff);
1735 case REPORT_BRIGHTNESS:
1736 /* 1 data byte with brightness */
1737 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1738 "REPORT_BRIGHTNESS", report->id, raw_size-1);
1739 hid_debug_event(hdev, buff);
1740 snprintf(buff, BUFF_SZ, "\tBrightness: 0x%02x\n", raw_data[1]);
1741 hid_debug_event(hdev, buff);
1743 case REPORT_CONTRAST:
1744 /* 1 data byte with contrast */
1745 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1746 "REPORT_CONTRAST", report->id, raw_size-1);
1747 hid_debug_event(hdev, buff);
1748 snprintf(buff, BUFF_SZ, "\tContrast: 0x%02x\n", raw_data[1]);
1749 hid_debug_event(hdev, buff);
1752 /* 2 data bytes with reset duration in ms */
1753 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1754 "REPORT_RESET", report->id, raw_size-1);
1755 hid_debug_event(hdev, buff);
1756 snprintf(buff, BUFF_SZ, "\tDuration: 0x%02x%02x (%dms)\n",
1757 raw_data[2], raw_data[1], raw_data[2] << 8 | raw_data[1]);
1758 hid_debug_event(hdev, buff);
1760 case REPORT_LCD_CMD:
1761 /* 63 data bytes with LCD commands */
1762 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1763 "REPORT_LCD_CMD", report->id, raw_size-1);
1764 hid_debug_event(hdev, buff);
1765 /* TODO: format decoding */
1767 case REPORT_LCD_DATA:
1768 /* 63 data bytes with LCD data */
1769 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1770 "REPORT_LCD_CMD", report->id, raw_size-1);
1771 /* TODO: format decoding */
1772 hid_debug_event(hdev, buff);
1774 case REPORT_LCD_CMD_DATA:
1775 /* 63 data bytes with LCD commands and data */
1776 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1777 "REPORT_LCD_CMD", report->id, raw_size-1);
1778 /* TODO: format decoding */
1779 hid_debug_event(hdev, buff);
1781 case REPORT_EE_READ:
1782 /* 3 data bytes with read area description */
1783 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1784 "REPORT_EE_READ", report->id, raw_size-1);
1785 hid_debug_event(hdev, buff);
1786 snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x\n",
1787 raw_data[2], raw_data[1]);
1788 hid_debug_event(hdev, buff);
1789 snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[3]);
1790 hid_debug_event(hdev, buff);
1792 case REPORT_EE_WRITE:
1793 /* 3+1..20 data bytes with write area description */
1794 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1795 "REPORT_EE_WRITE", report->id, raw_size-1);
1796 hid_debug_event(hdev, buff);
1797 snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x\n",
1798 raw_data[2], raw_data[1]);
1799 hid_debug_event(hdev, buff);
1800 snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[3]);
1801 hid_debug_event(hdev, buff);
1802 if (raw_data[3] == 0) {
1803 snprintf(buff, BUFF_SZ, "\tNo data\n");
1804 } else if (raw_data[3] + 4 <= raw_size) {
1805 snprintf(buff, BUFF_SZ, "\tData: ");
1806 hid_debug_event(hdev, buff);
1807 dump_buff_as_hex(buff, BUFF_SZ, raw_data+4, raw_data[3]);
1809 snprintf(buff, BUFF_SZ, "\tData overflowed\n");
1811 hid_debug_event(hdev, buff);
1813 case REPORT_ERASE_MEMORY:
1814 case REPORT_BL_ERASE_MEMORY:
1815 /* 3 data bytes with pointer inside erase block */
1816 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1817 "REPORT_ERASE_MEMORY", report->id, raw_size-1);
1818 hid_debug_event(hdev, buff);
1819 switch (data->addr_sz) {
1821 snprintf(buff, BUFF_SZ, "\tAddress inside 64 byte block: 0x%02x%02x\n",
1822 raw_data[2], raw_data[1]);
1825 snprintf(buff, BUFF_SZ, "\tAddress inside 64 byte block: 0x%02x%02x%02x\n",
1826 raw_data[3], raw_data[2], raw_data[1]);
1829 snprintf(buff, BUFF_SZ, "\tNot supported\n");
1831 hid_debug_event(hdev, buff);
1833 case REPORT_READ_MEMORY:
1834 case REPORT_BL_READ_MEMORY:
1835 /* 4 data bytes with read area description */
1836 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1837 "REPORT_READ_MEMORY", report->id, raw_size-1);
1838 hid_debug_event(hdev, buff);
1839 switch (data->addr_sz) {
1841 snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x\n",
1842 raw_data[2], raw_data[1]);
1843 hid_debug_event(hdev, buff);
1844 snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[3]);
1847 snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x%02x\n",
1848 raw_data[3], raw_data[2], raw_data[1]);
1849 hid_debug_event(hdev, buff);
1850 snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[4]);
1853 snprintf(buff, BUFF_SZ, "\tNot supported\n");
1855 hid_debug_event(hdev, buff);
1857 case REPORT_WRITE_MEMORY:
1858 case REPORT_BL_WRITE_MEMORY:
1859 /* 4+1..32 data bytes with write adrea description */
1860 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1861 "REPORT_WRITE_MEMORY", report->id, raw_size-1);
1862 hid_debug_event(hdev, buff);
1863 switch (data->addr_sz) {
1865 snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x\n",
1866 raw_data[2], raw_data[1]);
1867 hid_debug_event(hdev, buff);
1868 snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[3]);
1869 hid_debug_event(hdev, buff);
1870 if (raw_data[3] == 0) {
1871 snprintf(buff, BUFF_SZ, "\tNo data\n");
1872 } else if (raw_data[3] + 4 <= raw_size) {
1873 snprintf(buff, BUFF_SZ, "\tData: ");
1874 hid_debug_event(hdev, buff);
1875 dump_buff_as_hex(buff, BUFF_SZ, raw_data+4, raw_data[3]);
1877 snprintf(buff, BUFF_SZ, "\tData overflowed\n");
1881 snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x%02x\n",
1882 raw_data[3], raw_data[2], raw_data[1]);
1883 hid_debug_event(hdev, buff);
1884 snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[4]);
1885 hid_debug_event(hdev, buff);
1886 if (raw_data[4] == 0) {
1887 snprintf(buff, BUFF_SZ, "\tNo data\n");
1888 } else if (raw_data[4] + 5 <= raw_size) {
1889 snprintf(buff, BUFF_SZ, "\tData: ");
1890 hid_debug_event(hdev, buff);
1891 dump_buff_as_hex(buff, BUFF_SZ, raw_data+5, raw_data[4]);
1893 snprintf(buff, BUFF_SZ, "\tData overflowed\n");
1897 snprintf(buff, BUFF_SZ, "\tNot supported\n");
1899 hid_debug_event(hdev, buff);
1901 case REPORT_SPLASH_RESTART:
1904 case REPORT_EXIT_KEYBOARD:
1905 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1906 "REPORT_EXIT_KEYBOARD", report->id, raw_size-1);
1907 hid_debug_event(hdev, buff);
1908 snprintf(buff, BUFF_SZ, "\tRestart delay: %dms (0x%02x%02x)\n",
1909 raw_data[1] | (raw_data[2] << 8),
1910 raw_data[2], raw_data[1]);
1911 hid_debug_event(hdev, buff);
1913 case REPORT_VERSION:
1914 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1915 "REPORT_VERSION", report->id, raw_size-1);
1916 hid_debug_event(hdev, buff);
1919 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1920 "REPORT_DEVID", report->id, raw_size-1);
1921 hid_debug_event(hdev, buff);
1923 case REPORT_SPLASH_SIZE:
1924 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1925 "REPORT_SPLASH_SIZE", report->id, raw_size-1);
1926 hid_debug_event(hdev, buff);
1928 case REPORT_HOOK_VERSION:
1929 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1930 "REPORT_HOOK_VERSION", report->id, raw_size-1);
1931 hid_debug_event(hdev, buff);
1933 case REPORT_EXIT_FLASHER:
1934 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1935 "REPORT_VERSION", report->id, raw_size-1);
1936 hid_debug_event(hdev, buff);
1937 snprintf(buff, BUFF_SZ, "\tRestart delay: %dms (0x%02x%02x)\n",
1938 raw_data[1] | (raw_data[2] << 8),
1939 raw_data[2], raw_data[1]);
1940 hid_debug_event(hdev, buff);
1943 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1944 "<unknown>", report->id, raw_size-1);
1945 hid_debug_event(hdev, buff);
1948 wake_up_interruptible(&hdev->debug_wait);
1952 static void picolcd_debug_raw_event(struct picolcd_data *data,
1953 struct hid_device *hdev, struct hid_report *report,
1954 u8 *raw_data, int size)
1959 /* Avoid unnecessary overhead if debugfs is disabled */
1960 if (!hdev->debug_events)
1963 buff = kmalloc(BUFF_SZ, GFP_ATOMIC);
1967 switch (report->id) {
1968 case REPORT_ERROR_CODE:
1969 /* 2 data bytes with affected report and error code */
1970 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
1971 "REPORT_ERROR_CODE", report->id, size-1);
1972 hid_debug_event(hdev, buff);
1973 if (raw_data[2] < ARRAY_SIZE(error_codes))
1974 snprintf(buff, BUFF_SZ, "\tError code 0x%02x (%s) in reply to report 0x%02x\n",
1975 raw_data[2], error_codes[raw_data[2]], raw_data[1]);
1977 snprintf(buff, BUFF_SZ, "\tError code 0x%02x in reply to report 0x%02x\n",
1978 raw_data[2], raw_data[1]);
1979 hid_debug_event(hdev, buff);
1981 case REPORT_KEY_STATE:
1982 /* 2 data bytes with key state */
1983 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
1984 "REPORT_KEY_STATE", report->id, size-1);
1985 hid_debug_event(hdev, buff);
1986 if (raw_data[1] == 0)
1987 snprintf(buff, BUFF_SZ, "\tNo key pressed\n");
1988 else if (raw_data[2] == 0)
1989 snprintf(buff, BUFF_SZ, "\tOne key pressed: 0x%02x (%d)\n",
1990 raw_data[1], raw_data[1]);
1992 snprintf(buff, BUFF_SZ, "\tTwo keys pressed: 0x%02x (%d), 0x%02x (%d)\n",
1993 raw_data[1], raw_data[1], raw_data[2], raw_data[2]);
1994 hid_debug_event(hdev, buff);
1996 case REPORT_IR_DATA:
1997 /* Up to 20 byes of IR scancode data */
1998 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
1999 "REPORT_IR_DATA", report->id, size-1);
2000 hid_debug_event(hdev, buff);
2001 if (raw_data[1] == 0) {
2002 snprintf(buff, BUFF_SZ, "\tUnexpectedly 0 data length\n");
2003 hid_debug_event(hdev, buff);
2004 } else if (raw_data[1] + 1 <= size) {
2005 snprintf(buff, BUFF_SZ, "\tData length: %d\n\tIR Data: ",
2007 hid_debug_event(hdev, buff);
2008 dump_buff_as_hex(buff, BUFF_SZ, raw_data+2, raw_data[1]-1);
2009 hid_debug_event(hdev, buff);
2011 snprintf(buff, BUFF_SZ, "\tOverflowing data length: %d\n",
2013 hid_debug_event(hdev, buff);
2016 case REPORT_EE_DATA:
2017 /* Data buffer in response to REPORT_EE_READ or REPORT_EE_WRITE */
2018 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2019 "REPORT_EE_DATA", report->id, size-1);
2020 hid_debug_event(hdev, buff);
2021 snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x\n",
2022 raw_data[2], raw_data[1]);
2023 hid_debug_event(hdev, buff);
2024 snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[3]);
2025 hid_debug_event(hdev, buff);
2026 if (raw_data[3] == 0) {
2027 snprintf(buff, BUFF_SZ, "\tNo data\n");
2028 hid_debug_event(hdev, buff);
2029 } else if (raw_data[3] + 4 <= size) {
2030 snprintf(buff, BUFF_SZ, "\tData: ");
2031 hid_debug_event(hdev, buff);
2032 dump_buff_as_hex(buff, BUFF_SZ, raw_data+4, raw_data[3]);
2033 hid_debug_event(hdev, buff);
2035 snprintf(buff, BUFF_SZ, "\tData overflowed\n");
2036 hid_debug_event(hdev, buff);
2040 /* Data buffer in response to REPORT_READ_MEMORY or REPORT_WRTIE_MEMORY */
2041 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2042 "REPORT_MEMORY", report->id, size-1);
2043 hid_debug_event(hdev, buff);
2044 switch (data->addr_sz) {
2046 snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x\n",
2047 raw_data[2], raw_data[1]);
2048 hid_debug_event(hdev, buff);
2049 snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[3]);
2050 hid_debug_event(hdev, buff);
2051 if (raw_data[3] == 0) {
2052 snprintf(buff, BUFF_SZ, "\tNo data\n");
2053 } else if (raw_data[3] + 4 <= size) {
2054 snprintf(buff, BUFF_SZ, "\tData: ");
2055 hid_debug_event(hdev, buff);
2056 dump_buff_as_hex(buff, BUFF_SZ, raw_data+4, raw_data[3]);
2058 snprintf(buff, BUFF_SZ, "\tData overflowed\n");
2062 snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x%02x\n",
2063 raw_data[3], raw_data[2], raw_data[1]);
2064 hid_debug_event(hdev, buff);
2065 snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[4]);
2066 hid_debug_event(hdev, buff);
2067 if (raw_data[4] == 0) {
2068 snprintf(buff, BUFF_SZ, "\tNo data\n");
2069 } else if (raw_data[4] + 5 <= size) {
2070 snprintf(buff, BUFF_SZ, "\tData: ");
2071 hid_debug_event(hdev, buff);
2072 dump_buff_as_hex(buff, BUFF_SZ, raw_data+5, raw_data[4]);
2074 snprintf(buff, BUFF_SZ, "\tData overflowed\n");
2078 snprintf(buff, BUFF_SZ, "\tNot supported\n");
2080 hid_debug_event(hdev, buff);
2082 case REPORT_VERSION:
2083 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2084 "REPORT_VERSION", report->id, size-1);
2085 hid_debug_event(hdev, buff);
2086 snprintf(buff, BUFF_SZ, "\tFirmware version: %d.%d\n",
2087 raw_data[2], raw_data[1]);
2088 hid_debug_event(hdev, buff);
2090 case REPORT_BL_ERASE_MEMORY:
2091 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2092 "REPORT_BL_ERASE_MEMORY", report->id, size-1);
2093 hid_debug_event(hdev, buff);
2096 case REPORT_BL_READ_MEMORY:
2097 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2098 "REPORT_BL_READ_MEMORY", report->id, size-1);
2099 hid_debug_event(hdev, buff);
2102 case REPORT_BL_WRITE_MEMORY:
2103 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2104 "REPORT_BL_WRITE_MEMORY", report->id, size-1);
2105 hid_debug_event(hdev, buff);
2109 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2110 "REPORT_DEVID", report->id, size-1);
2111 hid_debug_event(hdev, buff);
2112 snprintf(buff, BUFF_SZ, "\tSerial: 0x%02x%02x%02x%02x\n",
2113 raw_data[1], raw_data[2], raw_data[3], raw_data[4]);
2114 hid_debug_event(hdev, buff);
2115 snprintf(buff, BUFF_SZ, "\tType: 0x%02x\n",
2117 hid_debug_event(hdev, buff);
2119 case REPORT_SPLASH_SIZE:
2120 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2121 "REPORT_SPLASH_SIZE", report->id, size-1);
2122 hid_debug_event(hdev, buff);
2123 snprintf(buff, BUFF_SZ, "\tTotal splash space: %d\n",
2124 (raw_data[2] << 8) | raw_data[1]);
2125 hid_debug_event(hdev, buff);
2126 snprintf(buff, BUFF_SZ, "\tUsed splash space: %d\n",
2127 (raw_data[4] << 8) | raw_data[3]);
2128 hid_debug_event(hdev, buff);
2130 case REPORT_HOOK_VERSION:
2131 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2132 "REPORT_HOOK_VERSION", report->id, size-1);
2133 hid_debug_event(hdev, buff);
2134 snprintf(buff, BUFF_SZ, "\tFirmware version: %d.%d\n",
2135 raw_data[1], raw_data[2]);
2136 hid_debug_event(hdev, buff);
2139 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2140 "<unknown>", report->id, size-1);
2141 hid_debug_event(hdev, buff);
2144 wake_up_interruptible(&hdev->debug_wait);
2148 static void picolcd_init_devfs(struct picolcd_data *data,
2149 struct hid_report *eeprom_r, struct hid_report *eeprom_w,
2150 struct hid_report *flash_r, struct hid_report *flash_w,
2151 struct hid_report *reset)
2153 struct hid_device *hdev = data->hdev;
2155 mutex_init(&data->mutex_flash);
2159 data->debug_reset = debugfs_create_file("reset", 0600,
2160 hdev->debug_dir, data, &picolcd_debug_reset_fops);
2163 if (eeprom_r || eeprom_w)
2164 data->debug_eeprom = debugfs_create_file("eeprom",
2165 (eeprom_w ? S_IWUSR : 0) | (eeprom_r ? S_IRUSR : 0),
2166 hdev->debug_dir, data, &picolcd_debug_eeprom_fops);
2169 if (flash_r && flash_r->maxfield == 1 && flash_r->field[0]->report_size == 8)
2170 data->addr_sz = flash_r->field[0]->report_count - 1;
2173 if (data->addr_sz == 2 || data->addr_sz == 3) {
2174 data->debug_flash = debugfs_create_file("flash",
2175 (flash_w ? S_IWUSR : 0) | (flash_r ? S_IRUSR : 0),
2176 hdev->debug_dir, data, &picolcd_debug_flash_fops);
2177 } else if (flash_r || flash_w)
2178 dev_warn(&hdev->dev, "Unexpected FLASH access reports, "
2179 "please submit rdesc for review\n");
2182 static void picolcd_exit_devfs(struct picolcd_data *data)
2184 struct dentry *dent;
2186 dent = data->debug_reset;
2187 data->debug_reset = NULL;
2189 debugfs_remove(dent);
2190 dent = data->debug_eeprom;
2191 data->debug_eeprom = NULL;
2193 debugfs_remove(dent);
2194 dent = data->debug_flash;
2195 data->debug_flash = NULL;
2197 debugfs_remove(dent);
2198 mutex_destroy(&data->mutex_flash);
2201 #define picolcd_debug_raw_event(data, hdev, report, raw_data, size)
2202 #define picolcd_init_devfs(data, eeprom_r, eeprom_w, flash_r, flash_w, reset)
2203 static void picolcd_exit_devfs(struct picolcd_data *data)
2206 #endif /* CONFIG_DEBUG_FS */
2209 * Handle raw report as sent by device
2211 static int picolcd_raw_event(struct hid_device *hdev,
2212 struct hid_report *report, u8 *raw_data, int size)
2214 struct picolcd_data *data = hid_get_drvdata(hdev);
2215 unsigned long flags;
2221 if (report->id == REPORT_KEY_STATE) {
2222 if (data->input_keys)
2223 ret = picolcd_raw_keypad(data, report, raw_data+1, size-1);
2224 } else if (report->id == REPORT_IR_DATA) {
2225 if (data->input_cir)
2226 ret = picolcd_raw_cir(data, report, raw_data+1, size-1);
2228 spin_lock_irqsave(&data->lock, flags);
2230 * We let the caller of picolcd_send_and_wait() check if the
2231 * report we got is one of the expected ones or not.
2233 if (data->pending) {
2234 memcpy(data->pending->raw_data, raw_data+1, size-1);
2235 data->pending->raw_size = size-1;
2236 data->pending->in_report = report;
2237 complete(&data->pending->ready);
2239 spin_unlock_irqrestore(&data->lock, flags);
2242 picolcd_debug_raw_event(data, hdev, report, raw_data, size);
2246 /* initialize keypad input device */
2247 static int picolcd_init_keys(struct picolcd_data *data,
2248 struct hid_report *report)
2250 struct hid_device *hdev = data->hdev;
2251 struct input_dev *idev;
2256 if (report->maxfield != 1 || report->field[0]->report_count != 2 ||
2257 report->field[0]->report_size != 8) {
2258 dev_err(&hdev->dev, "unsupported KEY_STATE report");
2262 idev = input_allocate_device();
2264 dev_err(&hdev->dev, "failed to allocate input device");
2267 input_set_drvdata(idev, hdev);
2268 memcpy(data->keycode, def_keymap, sizeof(def_keymap));
2269 idev->name = hdev->name;
2270 idev->phys = hdev->phys;
2271 idev->uniq = hdev->uniq;
2272 idev->id.bustype = hdev->bus;
2273 idev->id.vendor = hdev->vendor;
2274 idev->id.product = hdev->product;
2275 idev->id.version = hdev->version;
2276 idev->dev.parent = hdev->dev.parent;
2277 idev->keycode = &data->keycode;
2278 idev->keycodemax = PICOLCD_KEYS;
2279 idev->keycodesize = sizeof(data->keycode[0]);
2280 input_set_capability(idev, EV_MSC, MSC_SCAN);
2281 set_bit(EV_REP, idev->evbit);
2282 for (i = 0; i < PICOLCD_KEYS; i++)
2283 input_set_capability(idev, EV_KEY, data->keycode[i]);
2284 error = input_register_device(idev);
2286 dev_err(&hdev->dev, "error registering the input device");
2287 input_free_device(idev);
2290 data->input_keys = idev;
2294 static void picolcd_exit_keys(struct picolcd_data *data)
2296 struct input_dev *idev = data->input_keys;
2298 data->input_keys = NULL;
2300 input_unregister_device(idev);
2303 /* initialize CIR input device */
2304 static inline int picolcd_init_cir(struct picolcd_data *data, struct hid_report *report)
2306 /* support not implemented yet */
2310 static inline void picolcd_exit_cir(struct picolcd_data *data)
2314 static int picolcd_probe_lcd(struct hid_device *hdev, struct picolcd_data *data)
2318 error = picolcd_check_version(hdev);
2322 if (data->version[0] != 0 && data->version[1] != 3)
2323 dev_info(&hdev->dev, "Device with untested firmware revision, "
2324 "please submit /sys/kernel/debug/hid/%s/rdesc for this device.\n",
2325 dev_name(&hdev->dev));
2327 /* Setup keypad input device */
2328 error = picolcd_init_keys(data, picolcd_in_report(REPORT_KEY_STATE, hdev));
2332 /* Setup CIR input device */
2333 error = picolcd_init_cir(data, picolcd_in_report(REPORT_IR_DATA, hdev));
2337 /* Set up the framebuffer device */
2338 error = picolcd_init_framebuffer(data);
2342 /* Setup lcd class device */
2343 error = picolcd_init_lcd(data, picolcd_out_report(REPORT_CONTRAST, hdev));
2347 /* Setup backlight class device */
2348 error = picolcd_init_backlight(data, picolcd_out_report(REPORT_BRIGHTNESS, hdev));
2352 /* Setup the LED class devices */
2353 error = picolcd_init_leds(data, picolcd_out_report(REPORT_LED_STATE, hdev));
2357 picolcd_init_devfs(data, picolcd_out_report(REPORT_EE_READ, hdev),
2358 picolcd_out_report(REPORT_EE_WRITE, hdev),
2359 picolcd_out_report(REPORT_READ_MEMORY, hdev),
2360 picolcd_out_report(REPORT_WRITE_MEMORY, hdev),
2361 picolcd_out_report(REPORT_RESET, hdev));
2364 picolcd_exit_leds(data);
2365 picolcd_exit_backlight(data);
2366 picolcd_exit_lcd(data);
2367 picolcd_exit_framebuffer(data);
2368 picolcd_exit_cir(data);
2369 picolcd_exit_keys(data);
2373 static int picolcd_probe_bootloader(struct hid_device *hdev, struct picolcd_data *data)
2377 error = picolcd_check_version(hdev);
2381 if (data->version[0] != 1 && data->version[1] != 0)
2382 dev_info(&hdev->dev, "Device with untested bootloader revision, "
2383 "please submit /sys/kernel/debug/hid/%s/rdesc for this device.\n",
2384 dev_name(&hdev->dev));
2386 picolcd_init_devfs(data, NULL, NULL,
2387 picolcd_out_report(REPORT_BL_READ_MEMORY, hdev),
2388 picolcd_out_report(REPORT_BL_WRITE_MEMORY, hdev), NULL);
2392 static int picolcd_probe(struct hid_device *hdev,
2393 const struct hid_device_id *id)
2395 struct picolcd_data *data;
2396 int error = -ENOMEM;
2398 dbg_hid(PICOLCD_NAME " hardware probe...\n");
2401 * Let's allocate the picolcd data structure, set some reasonable
2402 * defaults, and associate it with the device
2404 data = kzalloc(sizeof(struct picolcd_data), GFP_KERNEL);
2406 dev_err(&hdev->dev, "can't allocate space for Minibox PicoLCD device data\n");
2408 goto err_no_cleanup;
2411 spin_lock_init(&data->lock);
2412 mutex_init(&data->mutex);
2414 if (hdev->product == USB_DEVICE_ID_PICOLCD_BOOTLOADER)
2415 data->status |= PICOLCD_BOOTLOADER;
2416 hid_set_drvdata(hdev, data);
2418 /* Parse the device reports and start it up */
2419 error = hid_parse(hdev);
2421 dev_err(&hdev->dev, "device report parse failed\n");
2422 goto err_cleanup_data;
2425 /* We don't use hidinput but hid_hw_start() fails if nothing is
2426 * claimed. So spoof claimed input. */
2427 hdev->claimed = HID_CLAIMED_INPUT;
2428 error = hid_hw_start(hdev, 0);
2431 dev_err(&hdev->dev, "hardware start failed\n");
2432 goto err_cleanup_data;
2435 error = hdev->ll_driver->open(hdev);
2437 dev_err(&hdev->dev, "failed to open input interrupt pipe for key and IR events\n");
2438 goto err_cleanup_hid_hw;
2441 error = device_create_file(&hdev->dev, &dev_attr_operation_mode);
2443 dev_err(&hdev->dev, "failed to create sysfs attributes\n");
2444 goto err_cleanup_hid_ll;
2447 if (data->status & PICOLCD_BOOTLOADER)
2448 error = picolcd_probe_bootloader(hdev, data);
2450 error = picolcd_probe_lcd(hdev, data);
2452 goto err_cleanup_sysfs;
2454 dbg_hid(PICOLCD_NAME " activated and initialized\n");
2458 device_remove_file(&hdev->dev, &dev_attr_operation_mode);
2460 hdev->ll_driver->close(hdev);
2466 hid_set_drvdata(hdev, NULL);
2471 static void picolcd_remove(struct hid_device *hdev)
2473 struct picolcd_data *data = hid_get_drvdata(hdev);
2474 unsigned long flags;
2476 dbg_hid(PICOLCD_NAME " hardware remove...\n");
2477 spin_lock_irqsave(&data->lock, flags);
2478 data->status |= PICOLCD_FAILED;
2479 spin_unlock_irqrestore(&data->lock, flags);
2481 picolcd_exit_devfs(data);
2482 device_remove_file(&hdev->dev, &dev_attr_operation_mode);
2483 hdev->ll_driver->close(hdev);
2485 hid_set_drvdata(hdev, NULL);
2487 /* Shortcut potential pending reply that will never arrive */
2488 spin_lock_irqsave(&data->lock, flags);
2490 complete(&data->pending->ready);
2491 spin_unlock_irqrestore(&data->lock, flags);
2494 picolcd_exit_leds(data);
2495 /* Clean up the framebuffer */
2496 picolcd_exit_backlight(data);
2497 picolcd_exit_lcd(data);
2498 picolcd_exit_framebuffer(data);
2500 picolcd_exit_cir(data);
2501 picolcd_exit_keys(data);
2503 mutex_destroy(&data->mutex);
2504 /* Finally, clean up the picolcd data itself */
2508 static const struct hid_device_id picolcd_devices[] = {
2509 { HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP, USB_DEVICE_ID_PICOLCD) },
2510 { HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP, USB_DEVICE_ID_PICOLCD_BOOTLOADER) },
2513 MODULE_DEVICE_TABLE(hid, picolcd_devices);
2515 static struct hid_driver picolcd_driver = {
2516 .name = "hid-picolcd",
2517 .id_table = picolcd_devices,
2518 .probe = picolcd_probe,
2519 .remove = picolcd_remove,
2520 .raw_event = picolcd_raw_event,
2523 static int __init picolcd_init(void)
2525 return hid_register_driver(&picolcd_driver);
2528 static void __exit picolcd_exit(void)
2530 hid_unregister_driver(&picolcd_driver);
2533 module_init(picolcd_init);
2534 module_exit(picolcd_exit);
2535 MODULE_DESCRIPTION("Minibox graphics PicoLCD Driver");
2536 MODULE_LICENSE("GPL v2");