x86, numa: Reduce minimum fake node size to 32M
[linux/fpc-iii.git] / drivers / hid / hid-picolcd.c
blobbc2e0774062864667ca92c52e3b4e7a23d923f53
1 /***************************************************************************
2 * Copyright (C) 2010 by Bruno Prémont <bonbons@linux-vserver.org> *
3 * *
4 * Based on Logitech G13 driver (v0.4) *
5 * Copyright (C) 2009 by Rick L. Vinyard, Jr. <rvinyard@cs.nmsu.edu> *
6 * *
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. *
10 * *
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. *
15 * *
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>
23 #include "hid-ids.h"
24 #include "usbhid/usbhid.h"
25 #include <linux/usb.h>
27 #include <linux/fb.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)"
42 /* Report numbers */
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
81 /* Framebuffer
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 * +----------------+----------------+----------------+----------------+
96 * ...
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,
114 .xpanstep = 0,
115 .ypanstep = 0,
116 .ywrapstep = 0,
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,
126 .width = 103,
127 .height = 26,
128 .bits_per_pixel = 1,
129 .grayscale = 1,
130 .red = {
131 .offset = 0,
132 .length = 1,
133 .msb_right = 0,
135 .green = {
136 .offset = 0,
137 .length = 1,
138 .msb_right = 0,
140 .blue = {
141 .offset = 0,
142 .length = 1,
143 .msb_right = 0,
145 .transp = {
146 .offset = 0,
147 .length = 0,
148 .msb_right = 0,
151 #endif /* CONFIG_HID_PICOLCD_FB */
153 /* Input device
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;
185 int raw_size;
186 u8 raw_data[64];
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;
197 int addr_sz;
198 #endif
199 u8 version[2];
200 unsigned short opmode_delay;
201 /* input stuff */
202 u8 pressed_keys[2];
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 */
209 u8 fb_update_rate;
210 u8 fb_bpp;
211 u8 fb_force;
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;
219 u8 lcd_contrast;
220 #endif /* CONFIG_HID_PICOLCD_LCD */
221 #ifdef CONFIG_HID_PICOLCD_BACKLIGHT
222 struct backlight_device *backlight;
223 u8 lcd_brightness;
224 u8 lcd_power;
225 #endif /* CONFIG_HID_PICOLCD_BACKLIGHT */
226 #ifdef CONFIG_HID_PICOLCD_LEDS
227 /* LED stuff */
228 u8 led_state;
229 struct led_classdev *led[8];
230 #endif /* CONFIG_HID_PICOLCD_LEDS */
232 /* Housekeeping stuff */
233 spinlock_t lock;
234 struct mutex mutex;
235 struct picolcd_pending *pending;
236 int status;
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)
254 return report;
256 dev_warn(&hdev->dev, "No report with id 0x%x found\n", id);
257 return NULL;
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) \
264 do { \
265 picolcd_debug_out_report(hid_get_drvdata(a), a, b); \
266 usbhid_submit_report(a, b, c); \
267 } while (0)
268 #endif
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);
278 unsigned long flags;
279 int i, j, k;
281 if (!report || !data)
282 return NULL;
283 if (data->status & PICOLCD_FAILED)
284 return NULL;
285 work = kzalloc(sizeof(*work), GFP_KERNEL);
286 if (!work)
287 return NULL;
289 init_completion(&work->ready);
290 work->out_report = report;
291 work->in_report = NULL;
292 work->raw_size = 0;
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);
299 k++;
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);
309 return work;
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);
319 unsigned long flags;
320 u8 *tdata;
321 int i;
323 if (!report1 || report1->maxfield != 1 || !report2 || report2->maxfield != 1)
324 return -ENODEV;
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++)
346 if (i < 32)
347 hid_set_field(report1->field[0], 11 + i, tdata[i]);
348 else
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);
354 return 0;
357 /* Translate a single tile*/
358 static int picolcd_fb_update_tile(u8 *vbitmap, const u8 *bitmap, int bpp,
359 int chip, int tile)
361 int i, b, changed = 0;
362 u8 tdata[64];
363 u8 *vdata = vbitmap + (tile * 4 + chip) * 64;
365 if (bpp == 1) {
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++) {
369 tdata[i] <<= 1;
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++) {
377 tdata[i] <<= 1;
378 tdata[i] |= (bdata[i] & 0x80) ? 0x01 : 0x00;
381 } else {
382 /* Oops, we should never get here! */
383 WARN_ON(1);
384 return 0;
387 for (i = 0; i < 64; i++)
388 if (tdata[i] != vdata[i]) {
389 changed = 1;
390 vdata[i] = tdata[i];
392 return changed;
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);
399 int i, j;
400 unsigned long flags;
401 static const u8 mapcmd[8] = { 0x00, 0x02, 0x00, 0x64, 0x3f, 0x00, 0x64, 0xc0 };
403 if (!report || report->maxfield != 1)
404 return -ENODEV;
406 spin_lock_irqsave(&data->lock, flags);
407 for (i = 0; i < 4; i++) {
408 for (j = 0; j < report->field[0]->maxusage; j++)
409 if (j == 0)
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]);
413 else
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) {
422 if (clear) {
423 memset(data->fb_vbitmap, 0, PICOLCDFB_SIZE);
424 memset(data->fb_bitmap, 0, PICOLCDFB_SIZE*data->fb_bpp);
426 data->fb_force = 1;
429 /* schedule first output of framebuffer */
430 if (data->fb_info)
431 schedule_delayed_work(&data->fb_info->deferred_work, 0);
433 return 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)
439 int chip, tile, n;
440 unsigned long flags;
442 if (!data)
443 return;
445 spin_lock_irqsave(&data->lock, flags);
446 if (!(data->status & PICOLCD_READY_FB)) {
447 spin_unlock_irqrestore(&data->lock, flags);
448 picolcd_fb_reset(data, 0);
449 } else {
450 spin_unlock_irqrestore(&data->lock, flags);
454 * Translate the framebuffer into the format needed by the PicoLCD.
455 * See display layout above.
456 * Do this one tile after the other and push those tiles that changed.
458 * Wait for our IO to complete as otherwise we might flood the queue!
460 n = 0;
461 for (chip = 0; chip < 4; chip++)
462 for (tile = 0; tile < 8; tile++)
463 if (picolcd_fb_update_tile(data->fb_vbitmap,
464 data->fb_bitmap, data->fb_bpp, chip, tile) ||
465 data->fb_force) {
466 n += 2;
467 if (!data->fb_info->par)
468 return; /* device lost! */
469 if (n >= HID_OUTPUT_FIFO_SIZE / 2) {
470 usbhid_wait_io(data->hdev);
471 n = 0;
473 picolcd_fb_send_tile(data->hdev, chip, tile);
475 data->fb_force = false;
476 if (n)
477 usbhid_wait_io(data->hdev);
480 /* Stub to call the system default and update the image on the picoLCD */
481 static void picolcd_fb_fillrect(struct fb_info *info,
482 const struct fb_fillrect *rect)
484 if (!info->par)
485 return;
486 sys_fillrect(info, rect);
488 schedule_delayed_work(&info->deferred_work, 0);
491 /* Stub to call the system default and update the image on the picoLCD */
492 static void picolcd_fb_copyarea(struct fb_info *info,
493 const struct fb_copyarea *area)
495 if (!info->par)
496 return;
497 sys_copyarea(info, area);
499 schedule_delayed_work(&info->deferred_work, 0);
502 /* Stub to call the system default and update the image on the picoLCD */
503 static void picolcd_fb_imageblit(struct fb_info *info, const struct fb_image *image)
505 if (!info->par)
506 return;
507 sys_imageblit(info, image);
509 schedule_delayed_work(&info->deferred_work, 0);
513 * this is the slow path from userspace. they can seek and write to
514 * the fb. it's inefficient to do anything less than a full screen draw
516 static ssize_t picolcd_fb_write(struct fb_info *info, const char __user *buf,
517 size_t count, loff_t *ppos)
519 ssize_t ret;
520 if (!info->par)
521 return -ENODEV;
522 ret = fb_sys_write(info, buf, count, ppos);
523 if (ret >= 0)
524 schedule_delayed_work(&info->deferred_work, 0);
525 return ret;
528 static int picolcd_fb_blank(int blank, struct fb_info *info)
530 if (!info->par)
531 return -ENODEV;
532 /* We let fb notification do this for us via lcd/backlight device */
533 return 0;
536 static void picolcd_fb_destroy(struct fb_info *info)
538 struct picolcd_data *data = info->par;
539 u32 *ref_cnt = info->pseudo_palette;
540 int may_release;
542 info->par = NULL;
543 if (data)
544 data->fb_info = NULL;
545 fb_deferred_io_cleanup(info);
547 ref_cnt--;
548 mutex_lock(&info->lock);
549 (*ref_cnt)--;
550 may_release = !*ref_cnt;
551 mutex_unlock(&info->lock);
552 if (may_release) {
553 vfree((u8 *)info->fix.smem_start);
554 framebuffer_release(info);
558 static int picolcd_fb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
560 __u32 bpp = var->bits_per_pixel;
561 __u32 activate = var->activate;
563 /* only allow 1/8 bit depth (8-bit is grayscale) */
564 *var = picolcdfb_var;
565 var->activate = activate;
566 if (bpp >= 8) {
567 var->bits_per_pixel = 8;
568 var->red.length = 8;
569 var->green.length = 8;
570 var->blue.length = 8;
571 } else {
572 var->bits_per_pixel = 1;
573 var->red.length = 1;
574 var->green.length = 1;
575 var->blue.length = 1;
577 return 0;
580 static int picolcd_set_par(struct fb_info *info)
582 struct picolcd_data *data = info->par;
583 u8 *tmp_fb, *o_fb;
584 if (!data)
585 return -ENODEV;
586 if (info->var.bits_per_pixel == data->fb_bpp)
587 return 0;
588 /* switch between 1/8 bit depths */
589 if (info->var.bits_per_pixel != 1 && info->var.bits_per_pixel != 8)
590 return -EINVAL;
592 o_fb = data->fb_bitmap;
593 tmp_fb = kmalloc(PICOLCDFB_SIZE*info->var.bits_per_pixel, GFP_KERNEL);
594 if (!tmp_fb)
595 return -ENOMEM;
597 /* translate FB content to new bits-per-pixel */
598 if (info->var.bits_per_pixel == 1) {
599 int i, b;
600 for (i = 0; i < PICOLCDFB_SIZE; i++) {
601 u8 p = 0;
602 for (b = 0; b < 8; b++) {
603 p <<= 1;
604 p |= o_fb[i*8+b] ? 0x01 : 0x00;
606 tmp_fb[i] = p;
608 memcpy(o_fb, tmp_fb, PICOLCDFB_SIZE);
609 info->fix.visual = FB_VISUAL_MONO01;
610 info->fix.line_length = PICOLCDFB_WIDTH / 8;
611 } else {
612 int i;
613 memcpy(tmp_fb, o_fb, PICOLCDFB_SIZE);
614 for (i = 0; i < PICOLCDFB_SIZE * 8; i++)
615 o_fb[i] = tmp_fb[i/8] & (0x01 << (7 - i % 8)) ? 0xff : 0x00;
616 info->fix.visual = FB_VISUAL_DIRECTCOLOR;
617 info->fix.line_length = PICOLCDFB_WIDTH;
620 kfree(tmp_fb);
621 data->fb_bpp = info->var.bits_per_pixel;
622 return 0;
625 /* Do refcounting on our FB and cleanup per worker if FB is
626 * closed after unplug of our device
627 * (fb_release holds info->lock and still touches info after
628 * we return so we can't release it immediately.
630 struct picolcd_fb_cleanup_item {
631 struct fb_info *info;
632 struct picolcd_fb_cleanup_item *next;
634 static struct picolcd_fb_cleanup_item *fb_pending;
635 DEFINE_SPINLOCK(fb_pending_lock);
637 static void picolcd_fb_do_cleanup(struct work_struct *data)
639 struct picolcd_fb_cleanup_item *item;
640 unsigned long flags;
642 do {
643 spin_lock_irqsave(&fb_pending_lock, flags);
644 item = fb_pending;
645 fb_pending = item ? item->next : NULL;
646 spin_unlock_irqrestore(&fb_pending_lock, flags);
648 if (item) {
649 u8 *fb = (u8 *)item->info->fix.smem_start;
650 /* make sure we do not race against fb core when
651 * releasing */
652 mutex_lock(&item->info->lock);
653 mutex_unlock(&item->info->lock);
654 framebuffer_release(item->info);
655 vfree(fb);
657 } while (item);
660 DECLARE_WORK(picolcd_fb_cleanup, picolcd_fb_do_cleanup);
662 static int picolcd_fb_open(struct fb_info *info, int u)
664 u32 *ref_cnt = info->pseudo_palette;
665 ref_cnt--;
667 (*ref_cnt)++;
668 return 0;
671 static int picolcd_fb_release(struct fb_info *info, int u)
673 u32 *ref_cnt = info->pseudo_palette;
674 ref_cnt--;
676 (*ref_cnt)++;
677 if (!*ref_cnt) {
678 unsigned long flags;
679 struct picolcd_fb_cleanup_item *item = (struct picolcd_fb_cleanup_item *)ref_cnt;
680 item--;
681 spin_lock_irqsave(&fb_pending_lock, flags);
682 item->next = fb_pending;
683 fb_pending = item;
684 spin_unlock_irqrestore(&fb_pending_lock, flags);
685 schedule_work(&picolcd_fb_cleanup);
687 return 0;
690 /* Note this can't be const because of struct fb_info definition */
691 static struct fb_ops picolcdfb_ops = {
692 .owner = THIS_MODULE,
693 .fb_destroy = picolcd_fb_destroy,
694 .fb_open = picolcd_fb_open,
695 .fb_release = picolcd_fb_release,
696 .fb_read = fb_sys_read,
697 .fb_write = picolcd_fb_write,
698 .fb_blank = picolcd_fb_blank,
699 .fb_fillrect = picolcd_fb_fillrect,
700 .fb_copyarea = picolcd_fb_copyarea,
701 .fb_imageblit = picolcd_fb_imageblit,
702 .fb_check_var = picolcd_fb_check_var,
703 .fb_set_par = picolcd_set_par,
707 /* Callback from deferred IO workqueue */
708 static void picolcd_fb_deferred_io(struct fb_info *info, struct list_head *pagelist)
710 picolcd_fb_update(info->par);
713 static const struct fb_deferred_io picolcd_fb_defio = {
714 .delay = HZ / PICOLCDFB_UPDATE_RATE_DEFAULT,
715 .deferred_io = picolcd_fb_deferred_io,
720 * The "fb_update_rate" sysfs attribute
722 static ssize_t picolcd_fb_update_rate_show(struct device *dev,
723 struct device_attribute *attr, char *buf)
725 struct picolcd_data *data = dev_get_drvdata(dev);
726 unsigned i, fb_update_rate = data->fb_update_rate;
727 size_t ret = 0;
729 for (i = 1; i <= PICOLCDFB_UPDATE_RATE_LIMIT; i++)
730 if (ret >= PAGE_SIZE)
731 break;
732 else if (i == fb_update_rate)
733 ret += snprintf(buf+ret, PAGE_SIZE-ret, "[%u] ", i);
734 else
735 ret += snprintf(buf+ret, PAGE_SIZE-ret, "%u ", i);
736 if (ret > 0)
737 buf[min(ret, (size_t)PAGE_SIZE)-1] = '\n';
738 return ret;
741 static ssize_t picolcd_fb_update_rate_store(struct device *dev,
742 struct device_attribute *attr, const char *buf, size_t count)
744 struct picolcd_data *data = dev_get_drvdata(dev);
745 int i;
746 unsigned u;
748 if (count < 1 || count > 10)
749 return -EINVAL;
751 i = sscanf(buf, "%u", &u);
752 if (i != 1)
753 return -EINVAL;
755 if (u > PICOLCDFB_UPDATE_RATE_LIMIT)
756 return -ERANGE;
757 else if (u == 0)
758 u = PICOLCDFB_UPDATE_RATE_DEFAULT;
760 data->fb_update_rate = u;
761 data->fb_defio.delay = HZ / data->fb_update_rate;
762 return count;
765 static DEVICE_ATTR(fb_update_rate, 0666, picolcd_fb_update_rate_show,
766 picolcd_fb_update_rate_store);
768 /* initialize Framebuffer device */
769 static int picolcd_init_framebuffer(struct picolcd_data *data)
771 struct device *dev = &data->hdev->dev;
772 struct fb_info *info = NULL;
773 int i, error = -ENOMEM;
774 u8 *fb_vbitmap = NULL;
775 u8 *fb_bitmap = NULL;
776 u32 *palette;
778 fb_bitmap = vmalloc(PICOLCDFB_SIZE*8);
779 if (fb_bitmap == NULL) {
780 dev_err(dev, "can't get a free page for framebuffer\n");
781 goto err_nomem;
784 fb_vbitmap = kmalloc(PICOLCDFB_SIZE, GFP_KERNEL);
785 if (fb_vbitmap == NULL) {
786 dev_err(dev, "can't alloc vbitmap image buffer\n");
787 goto err_nomem;
790 data->fb_update_rate = PICOLCDFB_UPDATE_RATE_DEFAULT;
791 data->fb_defio = picolcd_fb_defio;
792 /* The extra memory is:
793 * - struct picolcd_fb_cleanup_item
794 * - u32 for ref_count
795 * - 256*u32 for pseudo_palette
797 info = framebuffer_alloc(257 * sizeof(u32) + sizeof(struct picolcd_fb_cleanup_item), dev);
798 if (info == NULL) {
799 dev_err(dev, "failed to allocate a framebuffer\n");
800 goto err_nomem;
803 palette = info->par + sizeof(struct picolcd_fb_cleanup_item);
804 *palette = 1;
805 palette++;
806 for (i = 0; i < 256; i++)
807 palette[i] = i > 0 && i < 16 ? 0xff : 0;
808 info->pseudo_palette = palette;
809 info->fbdefio = &data->fb_defio;
810 info->screen_base = (char __force __iomem *)fb_bitmap;
811 info->fbops = &picolcdfb_ops;
812 info->var = picolcdfb_var;
813 info->fix = picolcdfb_fix;
814 info->fix.smem_len = PICOLCDFB_SIZE*8;
815 info->fix.smem_start = (unsigned long)fb_bitmap;
816 info->par = data;
817 info->flags = FBINFO_FLAG_DEFAULT;
819 data->fb_vbitmap = fb_vbitmap;
820 data->fb_bitmap = fb_bitmap;
821 data->fb_bpp = picolcdfb_var.bits_per_pixel;
822 error = picolcd_fb_reset(data, 1);
823 if (error) {
824 dev_err(dev, "failed to configure display\n");
825 goto err_cleanup;
827 error = device_create_file(dev, &dev_attr_fb_update_rate);
828 if (error) {
829 dev_err(dev, "failed to create sysfs attributes\n");
830 goto err_cleanup;
832 fb_deferred_io_init(info);
833 data->fb_info = info;
834 error = register_framebuffer(info);
835 if (error) {
836 dev_err(dev, "failed to register framebuffer\n");
837 goto err_sysfs;
839 /* schedule first output of framebuffer */
840 data->fb_force = 1;
841 schedule_delayed_work(&info->deferred_work, 0);
842 return 0;
844 err_sysfs:
845 fb_deferred_io_cleanup(info);
846 device_remove_file(dev, &dev_attr_fb_update_rate);
847 err_cleanup:
848 data->fb_vbitmap = NULL;
849 data->fb_bitmap = NULL;
850 data->fb_bpp = 0;
851 data->fb_info = NULL;
853 err_nomem:
854 framebuffer_release(info);
855 vfree(fb_bitmap);
856 kfree(fb_vbitmap);
857 return error;
860 static void picolcd_exit_framebuffer(struct picolcd_data *data)
862 struct fb_info *info = data->fb_info;
863 u8 *fb_vbitmap = data->fb_vbitmap;
865 if (!info)
866 return;
868 info->par = NULL;
869 device_remove_file(&data->hdev->dev, &dev_attr_fb_update_rate);
870 unregister_framebuffer(info);
871 data->fb_vbitmap = NULL;
872 data->fb_bitmap = NULL;
873 data->fb_bpp = 0;
874 data->fb_info = NULL;
875 kfree(fb_vbitmap);
878 #define picolcd_fbinfo(d) ((d)->fb_info)
879 #else
880 static inline int picolcd_fb_reset(struct picolcd_data *data, int clear)
882 return 0;
884 static inline int picolcd_init_framebuffer(struct picolcd_data *data)
886 return 0;
888 static inline void picolcd_exit_framebuffer(struct picolcd_data *data)
891 #define picolcd_fbinfo(d) NULL
892 #endif /* CONFIG_HID_PICOLCD_FB */
894 #ifdef CONFIG_HID_PICOLCD_BACKLIGHT
896 * backlight class device
898 static int picolcd_get_brightness(struct backlight_device *bdev)
900 struct picolcd_data *data = bl_get_data(bdev);
901 return data->lcd_brightness;
904 static int picolcd_set_brightness(struct backlight_device *bdev)
906 struct picolcd_data *data = bl_get_data(bdev);
907 struct hid_report *report = picolcd_out_report(REPORT_BRIGHTNESS, data->hdev);
908 unsigned long flags;
910 if (!report || report->maxfield != 1 || report->field[0]->report_count != 1)
911 return -ENODEV;
913 data->lcd_brightness = bdev->props.brightness & 0x0ff;
914 data->lcd_power = bdev->props.power;
915 spin_lock_irqsave(&data->lock, flags);
916 hid_set_field(report->field[0], 0, data->lcd_power == FB_BLANK_UNBLANK ? data->lcd_brightness : 0);
917 usbhid_submit_report(data->hdev, report, USB_DIR_OUT);
918 spin_unlock_irqrestore(&data->lock, flags);
919 return 0;
922 static int picolcd_check_bl_fb(struct backlight_device *bdev, struct fb_info *fb)
924 return fb && fb == picolcd_fbinfo((struct picolcd_data *)bl_get_data(bdev));
927 static const struct backlight_ops picolcd_blops = {
928 .update_status = picolcd_set_brightness,
929 .get_brightness = picolcd_get_brightness,
930 .check_fb = picolcd_check_bl_fb,
933 static int picolcd_init_backlight(struct picolcd_data *data, struct hid_report *report)
935 struct device *dev = &data->hdev->dev;
936 struct backlight_device *bdev;
937 struct backlight_properties props;
938 if (!report)
939 return -ENODEV;
940 if (report->maxfield != 1 || report->field[0]->report_count != 1 ||
941 report->field[0]->report_size != 8) {
942 dev_err(dev, "unsupported BRIGHTNESS report");
943 return -EINVAL;
946 memset(&props, 0, sizeof(props));
947 props.max_brightness = 0xff;
948 bdev = backlight_device_register(dev_name(dev), dev, data,
949 &picolcd_blops, &props);
950 if (IS_ERR(bdev)) {
951 dev_err(dev, "failed to register backlight\n");
952 return PTR_ERR(bdev);
954 bdev->props.brightness = 0xff;
955 data->lcd_brightness = 0xff;
956 data->backlight = bdev;
957 picolcd_set_brightness(bdev);
958 return 0;
961 static void picolcd_exit_backlight(struct picolcd_data *data)
963 struct backlight_device *bdev = data->backlight;
965 data->backlight = NULL;
966 if (bdev)
967 backlight_device_unregister(bdev);
970 static inline int picolcd_resume_backlight(struct picolcd_data *data)
972 if (!data->backlight)
973 return 0;
974 return picolcd_set_brightness(data->backlight);
977 #ifdef CONFIG_PM
978 static void picolcd_suspend_backlight(struct picolcd_data *data)
980 int bl_power = data->lcd_power;
981 if (!data->backlight)
982 return;
984 data->backlight->props.power = FB_BLANK_POWERDOWN;
985 picolcd_set_brightness(data->backlight);
986 data->lcd_power = data->backlight->props.power = bl_power;
988 #endif /* CONFIG_PM */
989 #else
990 static inline int picolcd_init_backlight(struct picolcd_data *data,
991 struct hid_report *report)
993 return 0;
995 static inline void picolcd_exit_backlight(struct picolcd_data *data)
998 static inline int picolcd_resume_backlight(struct picolcd_data *data)
1000 return 0;
1002 static inline void picolcd_suspend_backlight(struct picolcd_data *data)
1005 #endif /* CONFIG_HID_PICOLCD_BACKLIGHT */
1007 #ifdef CONFIG_HID_PICOLCD_LCD
1009 * lcd class device
1011 static int picolcd_get_contrast(struct lcd_device *ldev)
1013 struct picolcd_data *data = lcd_get_data(ldev);
1014 return data->lcd_contrast;
1017 static int picolcd_set_contrast(struct lcd_device *ldev, int contrast)
1019 struct picolcd_data *data = lcd_get_data(ldev);
1020 struct hid_report *report = picolcd_out_report(REPORT_CONTRAST, data->hdev);
1021 unsigned long flags;
1023 if (!report || report->maxfield != 1 || report->field[0]->report_count != 1)
1024 return -ENODEV;
1026 data->lcd_contrast = contrast & 0x0ff;
1027 spin_lock_irqsave(&data->lock, flags);
1028 hid_set_field(report->field[0], 0, data->lcd_contrast);
1029 usbhid_submit_report(data->hdev, report, USB_DIR_OUT);
1030 spin_unlock_irqrestore(&data->lock, flags);
1031 return 0;
1034 static int picolcd_check_lcd_fb(struct lcd_device *ldev, struct fb_info *fb)
1036 return fb && fb == picolcd_fbinfo((struct picolcd_data *)lcd_get_data(ldev));
1039 static struct lcd_ops picolcd_lcdops = {
1040 .get_contrast = picolcd_get_contrast,
1041 .set_contrast = picolcd_set_contrast,
1042 .check_fb = picolcd_check_lcd_fb,
1045 static int picolcd_init_lcd(struct picolcd_data *data, struct hid_report *report)
1047 struct device *dev = &data->hdev->dev;
1048 struct lcd_device *ldev;
1050 if (!report)
1051 return -ENODEV;
1052 if (report->maxfield != 1 || report->field[0]->report_count != 1 ||
1053 report->field[0]->report_size != 8) {
1054 dev_err(dev, "unsupported CONTRAST report");
1055 return -EINVAL;
1058 ldev = lcd_device_register(dev_name(dev), dev, data, &picolcd_lcdops);
1059 if (IS_ERR(ldev)) {
1060 dev_err(dev, "failed to register LCD\n");
1061 return PTR_ERR(ldev);
1063 ldev->props.max_contrast = 0x0ff;
1064 data->lcd_contrast = 0xe5;
1065 data->lcd = ldev;
1066 picolcd_set_contrast(ldev, 0xe5);
1067 return 0;
1070 static void picolcd_exit_lcd(struct picolcd_data *data)
1072 struct lcd_device *ldev = data->lcd;
1074 data->lcd = NULL;
1075 if (ldev)
1076 lcd_device_unregister(ldev);
1079 static inline int picolcd_resume_lcd(struct picolcd_data *data)
1081 if (!data->lcd)
1082 return 0;
1083 return picolcd_set_contrast(data->lcd, data->lcd_contrast);
1085 #else
1086 static inline int picolcd_init_lcd(struct picolcd_data *data,
1087 struct hid_report *report)
1089 return 0;
1091 static inline void picolcd_exit_lcd(struct picolcd_data *data)
1094 static inline int picolcd_resume_lcd(struct picolcd_data *data)
1096 return 0;
1098 #endif /* CONFIG_HID_PICOLCD_LCD */
1100 #ifdef CONFIG_HID_PICOLCD_LEDS
1102 * LED class device
1104 static void picolcd_leds_set(struct picolcd_data *data)
1106 struct hid_report *report;
1107 unsigned long flags;
1109 if (!data->led[0])
1110 return;
1111 report = picolcd_out_report(REPORT_LED_STATE, data->hdev);
1112 if (!report || report->maxfield != 1 || report->field[0]->report_count != 1)
1113 return;
1115 spin_lock_irqsave(&data->lock, flags);
1116 hid_set_field(report->field[0], 0, data->led_state);
1117 usbhid_submit_report(data->hdev, report, USB_DIR_OUT);
1118 spin_unlock_irqrestore(&data->lock, flags);
1121 static void picolcd_led_set_brightness(struct led_classdev *led_cdev,
1122 enum led_brightness value)
1124 struct device *dev;
1125 struct hid_device *hdev;
1126 struct picolcd_data *data;
1127 int i, state = 0;
1129 dev = led_cdev->dev->parent;
1130 hdev = container_of(dev, struct hid_device, dev);
1131 data = hid_get_drvdata(hdev);
1132 for (i = 0; i < 8; i++) {
1133 if (led_cdev != data->led[i])
1134 continue;
1135 state = (data->led_state >> i) & 1;
1136 if (value == LED_OFF && state) {
1137 data->led_state &= ~(1 << i);
1138 picolcd_leds_set(data);
1139 } else if (value != LED_OFF && !state) {
1140 data->led_state |= 1 << i;
1141 picolcd_leds_set(data);
1143 break;
1147 static enum led_brightness picolcd_led_get_brightness(struct led_classdev *led_cdev)
1149 struct device *dev;
1150 struct hid_device *hdev;
1151 struct picolcd_data *data;
1152 int i, value = 0;
1154 dev = led_cdev->dev->parent;
1155 hdev = container_of(dev, struct hid_device, dev);
1156 data = hid_get_drvdata(hdev);
1157 for (i = 0; i < 8; i++)
1158 if (led_cdev == data->led[i]) {
1159 value = (data->led_state >> i) & 1;
1160 break;
1162 return value ? LED_FULL : LED_OFF;
1165 static int picolcd_init_leds(struct picolcd_data *data, struct hid_report *report)
1167 struct device *dev = &data->hdev->dev;
1168 struct led_classdev *led;
1169 size_t name_sz = strlen(dev_name(dev)) + 8;
1170 char *name;
1171 int i, ret = 0;
1173 if (!report)
1174 return -ENODEV;
1175 if (report->maxfield != 1 || report->field[0]->report_count != 1 ||
1176 report->field[0]->report_size != 8) {
1177 dev_err(dev, "unsupported LED_STATE report");
1178 return -EINVAL;
1181 for (i = 0; i < 8; i++) {
1182 led = kzalloc(sizeof(struct led_classdev)+name_sz, GFP_KERNEL);
1183 if (!led) {
1184 dev_err(dev, "can't allocate memory for LED %d\n", i);
1185 ret = -ENOMEM;
1186 goto err;
1188 name = (void *)(&led[1]);
1189 snprintf(name, name_sz, "%s::GPO%d", dev_name(dev), i);
1190 led->name = name;
1191 led->brightness = 0;
1192 led->max_brightness = 1;
1193 led->brightness_get = picolcd_led_get_brightness;
1194 led->brightness_set = picolcd_led_set_brightness;
1196 data->led[i] = led;
1197 ret = led_classdev_register(dev, data->led[i]);
1198 if (ret) {
1199 data->led[i] = NULL;
1200 kfree(led);
1201 dev_err(dev, "can't register LED %d\n", i);
1202 goto err;
1205 return 0;
1206 err:
1207 for (i = 0; i < 8; i++)
1208 if (data->led[i]) {
1209 led = data->led[i];
1210 data->led[i] = NULL;
1211 led_classdev_unregister(led);
1212 kfree(led);
1214 return ret;
1217 static void picolcd_exit_leds(struct picolcd_data *data)
1219 struct led_classdev *led;
1220 int i;
1222 for (i = 0; i < 8; i++) {
1223 led = data->led[i];
1224 data->led[i] = NULL;
1225 if (!led)
1226 continue;
1227 led_classdev_unregister(led);
1228 kfree(led);
1232 #else
1233 static inline int picolcd_init_leds(struct picolcd_data *data,
1234 struct hid_report *report)
1236 return 0;
1238 static inline void picolcd_exit_leds(struct picolcd_data *data)
1241 static inline int picolcd_leds_set(struct picolcd_data *data)
1243 return 0;
1245 #endif /* CONFIG_HID_PICOLCD_LEDS */
1248 * input class device
1250 static int picolcd_raw_keypad(struct picolcd_data *data,
1251 struct hid_report *report, u8 *raw_data, int size)
1254 * Keypad event
1255 * First and second data bytes list currently pressed keys,
1256 * 0x00 means no key and at most 2 keys may be pressed at same time
1258 int i, j;
1260 /* determine newly pressed keys */
1261 for (i = 0; i < size; i++) {
1262 unsigned int key_code;
1263 if (raw_data[i] == 0)
1264 continue;
1265 for (j = 0; j < sizeof(data->pressed_keys); j++)
1266 if (data->pressed_keys[j] == raw_data[i])
1267 goto key_already_down;
1268 for (j = 0; j < sizeof(data->pressed_keys); j++)
1269 if (data->pressed_keys[j] == 0) {
1270 data->pressed_keys[j] = raw_data[i];
1271 break;
1273 input_event(data->input_keys, EV_MSC, MSC_SCAN, raw_data[i]);
1274 if (raw_data[i] < PICOLCD_KEYS)
1275 key_code = data->keycode[raw_data[i]];
1276 else
1277 key_code = KEY_UNKNOWN;
1278 if (key_code != KEY_UNKNOWN) {
1279 dbg_hid(PICOLCD_NAME " got key press for %u:%d",
1280 raw_data[i], key_code);
1281 input_report_key(data->input_keys, key_code, 1);
1283 input_sync(data->input_keys);
1284 key_already_down:
1285 continue;
1288 /* determine newly released keys */
1289 for (j = 0; j < sizeof(data->pressed_keys); j++) {
1290 unsigned int key_code;
1291 if (data->pressed_keys[j] == 0)
1292 continue;
1293 for (i = 0; i < size; i++)
1294 if (data->pressed_keys[j] == raw_data[i])
1295 goto key_still_down;
1296 input_event(data->input_keys, EV_MSC, MSC_SCAN, data->pressed_keys[j]);
1297 if (data->pressed_keys[j] < PICOLCD_KEYS)
1298 key_code = data->keycode[data->pressed_keys[j]];
1299 else
1300 key_code = KEY_UNKNOWN;
1301 if (key_code != KEY_UNKNOWN) {
1302 dbg_hid(PICOLCD_NAME " got key release for %u:%d",
1303 data->pressed_keys[j], key_code);
1304 input_report_key(data->input_keys, key_code, 0);
1306 input_sync(data->input_keys);
1307 data->pressed_keys[j] = 0;
1308 key_still_down:
1309 continue;
1311 return 1;
1314 static int picolcd_raw_cir(struct picolcd_data *data,
1315 struct hid_report *report, u8 *raw_data, int size)
1317 /* Need understanding of CIR data format to implement ... */
1318 return 1;
1321 static int picolcd_check_version(struct hid_device *hdev)
1323 struct picolcd_data *data = hid_get_drvdata(hdev);
1324 struct picolcd_pending *verinfo;
1325 int ret = 0;
1327 if (!data)
1328 return -ENODEV;
1330 verinfo = picolcd_send_and_wait(hdev, REPORT_VERSION, NULL, 0);
1331 if (!verinfo) {
1332 dev_err(&hdev->dev, "no version response from PicoLCD");
1333 return -ENODEV;
1336 if (verinfo->raw_size == 2) {
1337 data->version[0] = verinfo->raw_data[1];
1338 data->version[1] = verinfo->raw_data[0];
1339 if (data->status & PICOLCD_BOOTLOADER) {
1340 dev_info(&hdev->dev, "PicoLCD, bootloader version %d.%d\n",
1341 verinfo->raw_data[1], verinfo->raw_data[0]);
1342 } else {
1343 dev_info(&hdev->dev, "PicoLCD, firmware version %d.%d\n",
1344 verinfo->raw_data[1], verinfo->raw_data[0]);
1346 } else {
1347 dev_err(&hdev->dev, "confused, got unexpected version response from PicoLCD\n");
1348 ret = -EINVAL;
1350 kfree(verinfo);
1351 return ret;
1355 * Reset our device and wait for answer to VERSION request
1357 static int picolcd_reset(struct hid_device *hdev)
1359 struct picolcd_data *data = hid_get_drvdata(hdev);
1360 struct hid_report *report = picolcd_out_report(REPORT_RESET, hdev);
1361 unsigned long flags;
1362 int error;
1364 if (!data || !report || report->maxfield != 1)
1365 return -ENODEV;
1367 spin_lock_irqsave(&data->lock, flags);
1368 if (hdev->product == USB_DEVICE_ID_PICOLCD_BOOTLOADER)
1369 data->status |= PICOLCD_BOOTLOADER;
1371 /* perform the reset */
1372 hid_set_field(report->field[0], 0, 1);
1373 usbhid_submit_report(hdev, report, USB_DIR_OUT);
1374 spin_unlock_irqrestore(&data->lock, flags);
1376 error = picolcd_check_version(hdev);
1377 if (error)
1378 return error;
1380 picolcd_resume_lcd(data);
1381 picolcd_resume_backlight(data);
1382 #ifdef CONFIG_HID_PICOLCD_FB
1383 if (data->fb_info)
1384 schedule_delayed_work(&data->fb_info->deferred_work, 0);
1385 #endif /* CONFIG_HID_PICOLCD_FB */
1387 picolcd_leds_set(data);
1388 return 0;
1392 * The "operation_mode" sysfs attribute
1394 static ssize_t picolcd_operation_mode_show(struct device *dev,
1395 struct device_attribute *attr, char *buf)
1397 struct picolcd_data *data = dev_get_drvdata(dev);
1399 if (data->status & PICOLCD_BOOTLOADER)
1400 return snprintf(buf, PAGE_SIZE, "[bootloader] lcd\n");
1401 else
1402 return snprintf(buf, PAGE_SIZE, "bootloader [lcd]\n");
1405 static ssize_t picolcd_operation_mode_store(struct device *dev,
1406 struct device_attribute *attr, const char *buf, size_t count)
1408 struct picolcd_data *data = dev_get_drvdata(dev);
1409 struct hid_report *report = NULL;
1410 size_t cnt = count;
1411 int timeout = data->opmode_delay;
1412 unsigned long flags;
1414 if (cnt >= 3 && strncmp("lcd", buf, 3) == 0) {
1415 if (data->status & PICOLCD_BOOTLOADER)
1416 report = picolcd_out_report(REPORT_EXIT_FLASHER, data->hdev);
1417 buf += 3;
1418 cnt -= 3;
1419 } else if (cnt >= 10 && strncmp("bootloader", buf, 10) == 0) {
1420 if (!(data->status & PICOLCD_BOOTLOADER))
1421 report = picolcd_out_report(REPORT_EXIT_KEYBOARD, data->hdev);
1422 buf += 10;
1423 cnt -= 10;
1425 if (!report)
1426 return -EINVAL;
1428 while (cnt > 0 && (buf[cnt-1] == '\n' || buf[cnt-1] == '\r'))
1429 cnt--;
1430 if (cnt != 0)
1431 return -EINVAL;
1433 spin_lock_irqsave(&data->lock, flags);
1434 hid_set_field(report->field[0], 0, timeout & 0xff);
1435 hid_set_field(report->field[0], 1, (timeout >> 8) & 0xff);
1436 usbhid_submit_report(data->hdev, report, USB_DIR_OUT);
1437 spin_unlock_irqrestore(&data->lock, flags);
1438 return count;
1441 static DEVICE_ATTR(operation_mode, 0644, picolcd_operation_mode_show,
1442 picolcd_operation_mode_store);
1445 * The "operation_mode_delay" sysfs attribute
1447 static ssize_t picolcd_operation_mode_delay_show(struct device *dev,
1448 struct device_attribute *attr, char *buf)
1450 struct picolcd_data *data = dev_get_drvdata(dev);
1452 return snprintf(buf, PAGE_SIZE, "%hu\n", data->opmode_delay);
1455 static ssize_t picolcd_operation_mode_delay_store(struct device *dev,
1456 struct device_attribute *attr, const char *buf, size_t count)
1458 struct picolcd_data *data = dev_get_drvdata(dev);
1459 unsigned u;
1460 if (sscanf(buf, "%u", &u) != 1)
1461 return -EINVAL;
1462 if (u > 30000)
1463 return -EINVAL;
1464 else
1465 data->opmode_delay = u;
1466 return count;
1469 static DEVICE_ATTR(operation_mode_delay, 0644, picolcd_operation_mode_delay_show,
1470 picolcd_operation_mode_delay_store);
1473 #ifdef CONFIG_DEBUG_FS
1475 * The "reset" file
1477 static int picolcd_debug_reset_show(struct seq_file *f, void *p)
1479 if (picolcd_fbinfo((struct picolcd_data *)f->private))
1480 seq_printf(f, "all fb\n");
1481 else
1482 seq_printf(f, "all\n");
1483 return 0;
1486 static int picolcd_debug_reset_open(struct inode *inode, struct file *f)
1488 return single_open(f, picolcd_debug_reset_show, inode->i_private);
1491 static ssize_t picolcd_debug_reset_write(struct file *f, const char __user *user_buf,
1492 size_t count, loff_t *ppos)
1494 struct picolcd_data *data = ((struct seq_file *)f->private_data)->private;
1495 char buf[32];
1496 size_t cnt = min(count, sizeof(buf)-1);
1497 if (copy_from_user(buf, user_buf, cnt))
1498 return -EFAULT;
1500 while (cnt > 0 && (buf[cnt-1] == ' ' || buf[cnt-1] == '\n'))
1501 cnt--;
1502 buf[cnt] = '\0';
1503 if (strcmp(buf, "all") == 0) {
1504 picolcd_reset(data->hdev);
1505 picolcd_fb_reset(data, 1);
1506 } else if (strcmp(buf, "fb") == 0) {
1507 picolcd_fb_reset(data, 1);
1508 } else {
1509 return -EINVAL;
1511 return count;
1514 static const struct file_operations picolcd_debug_reset_fops = {
1515 .owner = THIS_MODULE,
1516 .open = picolcd_debug_reset_open,
1517 .read = seq_read,
1518 .llseek = seq_lseek,
1519 .write = picolcd_debug_reset_write,
1520 .release = single_release,
1524 * The "eeprom" file
1526 static int picolcd_debug_eeprom_open(struct inode *i, struct file *f)
1528 f->private_data = i->i_private;
1529 return 0;
1532 static ssize_t picolcd_debug_eeprom_read(struct file *f, char __user *u,
1533 size_t s, loff_t *off)
1535 struct picolcd_data *data = f->private_data;
1536 struct picolcd_pending *resp;
1537 u8 raw_data[3];
1538 ssize_t ret = -EIO;
1540 if (s == 0)
1541 return -EINVAL;
1542 if (*off > 0x0ff)
1543 return 0;
1545 /* prepare buffer with info about what we want to read (addr & len) */
1546 raw_data[0] = *off & 0xff;
1547 raw_data[1] = (*off >> 8) && 0xff;
1548 raw_data[2] = s < 20 ? s : 20;
1549 if (*off + raw_data[2] > 0xff)
1550 raw_data[2] = 0x100 - *off;
1551 resp = picolcd_send_and_wait(data->hdev, REPORT_EE_READ, raw_data,
1552 sizeof(raw_data));
1553 if (!resp)
1554 return -EIO;
1556 if (resp->in_report && resp->in_report->id == REPORT_EE_DATA) {
1557 /* successful read :) */
1558 ret = resp->raw_data[2];
1559 if (ret > s)
1560 ret = s;
1561 if (copy_to_user(u, resp->raw_data+3, ret))
1562 ret = -EFAULT;
1563 else
1564 *off += ret;
1565 } /* anything else is some kind of IO error */
1567 kfree(resp);
1568 return ret;
1571 static ssize_t picolcd_debug_eeprom_write(struct file *f, const char __user *u,
1572 size_t s, loff_t *off)
1574 struct picolcd_data *data = f->private_data;
1575 struct picolcd_pending *resp;
1576 ssize_t ret = -EIO;
1577 u8 raw_data[23];
1579 if (s == 0)
1580 return -EINVAL;
1581 if (*off > 0x0ff)
1582 return -ENOSPC;
1584 memset(raw_data, 0, sizeof(raw_data));
1585 raw_data[0] = *off & 0xff;
1586 raw_data[1] = (*off >> 8) && 0xff;
1587 raw_data[2] = s < 20 ? s : 20;
1588 if (*off + raw_data[2] > 0xff)
1589 raw_data[2] = 0x100 - *off;
1591 if (copy_from_user(raw_data+3, u, raw_data[2]))
1592 return -EFAULT;
1593 resp = picolcd_send_and_wait(data->hdev, REPORT_EE_WRITE, raw_data,
1594 sizeof(raw_data));
1596 if (!resp)
1597 return -EIO;
1599 if (resp->in_report && resp->in_report->id == REPORT_EE_DATA) {
1600 /* check if written data matches */
1601 if (memcmp(raw_data, resp->raw_data, 3+raw_data[2]) == 0) {
1602 *off += raw_data[2];
1603 ret = raw_data[2];
1606 kfree(resp);
1607 return ret;
1611 * Notes:
1612 * - read/write happens in chunks of at most 20 bytes, it's up to userspace
1613 * to loop in order to get more data.
1614 * - on write errors on otherwise correct write request the bytes
1615 * that should have been written are in undefined state.
1617 static const struct file_operations picolcd_debug_eeprom_fops = {
1618 .owner = THIS_MODULE,
1619 .open = picolcd_debug_eeprom_open,
1620 .read = picolcd_debug_eeprom_read,
1621 .write = picolcd_debug_eeprom_write,
1622 .llseek = generic_file_llseek,
1626 * The "flash" file
1628 static int picolcd_debug_flash_open(struct inode *i, struct file *f)
1630 f->private_data = i->i_private;
1631 return 0;
1634 /* record a flash address to buf (bounds check to be done by caller) */
1635 static int _picolcd_flash_setaddr(struct picolcd_data *data, u8 *buf, long off)
1637 buf[0] = off & 0xff;
1638 buf[1] = (off >> 8) & 0xff;
1639 if (data->addr_sz == 3)
1640 buf[2] = (off >> 16) & 0xff;
1641 return data->addr_sz == 2 ? 2 : 3;
1644 /* read a given size of data (bounds check to be done by caller) */
1645 static ssize_t _picolcd_flash_read(struct picolcd_data *data, int report_id,
1646 char __user *u, size_t s, loff_t *off)
1648 struct picolcd_pending *resp;
1649 u8 raw_data[4];
1650 ssize_t ret = 0;
1651 int len_off, err = -EIO;
1653 while (s > 0) {
1654 err = -EIO;
1655 len_off = _picolcd_flash_setaddr(data, raw_data, *off);
1656 raw_data[len_off] = s > 32 ? 32 : s;
1657 resp = picolcd_send_and_wait(data->hdev, report_id, raw_data, len_off+1);
1658 if (!resp || !resp->in_report)
1659 goto skip;
1660 if (resp->in_report->id == REPORT_MEMORY ||
1661 resp->in_report->id == REPORT_BL_READ_MEMORY) {
1662 if (memcmp(raw_data, resp->raw_data, len_off+1) != 0)
1663 goto skip;
1664 if (copy_to_user(u+ret, resp->raw_data+len_off+1, raw_data[len_off])) {
1665 err = -EFAULT;
1666 goto skip;
1668 *off += raw_data[len_off];
1669 s -= raw_data[len_off];
1670 ret += raw_data[len_off];
1671 err = 0;
1673 skip:
1674 kfree(resp);
1675 if (err)
1676 return ret > 0 ? ret : err;
1678 return ret;
1681 static ssize_t picolcd_debug_flash_read(struct file *f, char __user *u,
1682 size_t s, loff_t *off)
1684 struct picolcd_data *data = f->private_data;
1686 if (s == 0)
1687 return -EINVAL;
1688 if (*off > 0x05fff)
1689 return 0;
1690 if (*off + s > 0x05fff)
1691 s = 0x06000 - *off;
1693 if (data->status & PICOLCD_BOOTLOADER)
1694 return _picolcd_flash_read(data, REPORT_BL_READ_MEMORY, u, s, off);
1695 else
1696 return _picolcd_flash_read(data, REPORT_READ_MEMORY, u, s, off);
1699 /* erase block aligned to 64bytes boundary */
1700 static ssize_t _picolcd_flash_erase64(struct picolcd_data *data, int report_id,
1701 loff_t *off)
1703 struct picolcd_pending *resp;
1704 u8 raw_data[3];
1705 int len_off;
1706 ssize_t ret = -EIO;
1708 if (*off & 0x3f)
1709 return -EINVAL;
1711 len_off = _picolcd_flash_setaddr(data, raw_data, *off);
1712 resp = picolcd_send_and_wait(data->hdev, report_id, raw_data, len_off);
1713 if (!resp || !resp->in_report)
1714 goto skip;
1715 if (resp->in_report->id == REPORT_MEMORY ||
1716 resp->in_report->id == REPORT_BL_ERASE_MEMORY) {
1717 if (memcmp(raw_data, resp->raw_data, len_off) != 0)
1718 goto skip;
1719 ret = 0;
1721 skip:
1722 kfree(resp);
1723 return ret;
1726 /* write a given size of data (bounds check to be done by caller) */
1727 static ssize_t _picolcd_flash_write(struct picolcd_data *data, int report_id,
1728 const char __user *u, size_t s, loff_t *off)
1730 struct picolcd_pending *resp;
1731 u8 raw_data[36];
1732 ssize_t ret = 0;
1733 int len_off, err = -EIO;
1735 while (s > 0) {
1736 err = -EIO;
1737 len_off = _picolcd_flash_setaddr(data, raw_data, *off);
1738 raw_data[len_off] = s > 32 ? 32 : s;
1739 if (copy_from_user(raw_data+len_off+1, u, raw_data[len_off])) {
1740 err = -EFAULT;
1741 break;
1743 resp = picolcd_send_and_wait(data->hdev, report_id, raw_data,
1744 len_off+1+raw_data[len_off]);
1745 if (!resp || !resp->in_report)
1746 goto skip;
1747 if (resp->in_report->id == REPORT_MEMORY ||
1748 resp->in_report->id == REPORT_BL_WRITE_MEMORY) {
1749 if (memcmp(raw_data, resp->raw_data, len_off+1+raw_data[len_off]) != 0)
1750 goto skip;
1751 *off += raw_data[len_off];
1752 s -= raw_data[len_off];
1753 ret += raw_data[len_off];
1754 err = 0;
1756 skip:
1757 kfree(resp);
1758 if (err)
1759 break;
1761 return ret > 0 ? ret : err;
1764 static ssize_t picolcd_debug_flash_write(struct file *f, const char __user *u,
1765 size_t s, loff_t *off)
1767 struct picolcd_data *data = f->private_data;
1768 ssize_t err, ret = 0;
1769 int report_erase, report_write;
1771 if (s == 0)
1772 return -EINVAL;
1773 if (*off > 0x5fff)
1774 return -ENOSPC;
1775 if (s & 0x3f)
1776 return -EINVAL;
1777 if (*off & 0x3f)
1778 return -EINVAL;
1780 if (data->status & PICOLCD_BOOTLOADER) {
1781 report_erase = REPORT_BL_ERASE_MEMORY;
1782 report_write = REPORT_BL_WRITE_MEMORY;
1783 } else {
1784 report_erase = REPORT_ERASE_MEMORY;
1785 report_write = REPORT_WRITE_MEMORY;
1787 mutex_lock(&data->mutex_flash);
1788 while (s > 0) {
1789 err = _picolcd_flash_erase64(data, report_erase, off);
1790 if (err)
1791 break;
1792 err = _picolcd_flash_write(data, report_write, u, 64, off);
1793 if (err < 0)
1794 break;
1795 ret += err;
1796 *off += err;
1797 s -= err;
1798 if (err != 64)
1799 break;
1801 mutex_unlock(&data->mutex_flash);
1802 return ret > 0 ? ret : err;
1806 * Notes:
1807 * - concurrent writing is prevented by mutex and all writes must be
1808 * n*64 bytes and 64-byte aligned, each write being preceeded by an
1809 * ERASE which erases a 64byte block.
1810 * If less than requested was written or an error is returned for an
1811 * otherwise correct write request the next 64-byte block which should
1812 * have been written is in undefined state (mostly: original, erased,
1813 * (half-)written with write error)
1814 * - reading can happend without special restriction
1816 static const struct file_operations picolcd_debug_flash_fops = {
1817 .owner = THIS_MODULE,
1818 .open = picolcd_debug_flash_open,
1819 .read = picolcd_debug_flash_read,
1820 .write = picolcd_debug_flash_write,
1821 .llseek = generic_file_llseek,
1826 * Helper code for HID report level dumping/debugging
1828 static const char *error_codes[] = {
1829 "success", "parameter missing", "data_missing", "block readonly",
1830 "block not erasable", "block too big", "section overflow",
1831 "invalid command length", "invalid data length",
1834 static void dump_buff_as_hex(char *dst, size_t dst_sz, const u8 *data,
1835 const size_t data_len)
1837 int i, j;
1838 for (i = j = 0; i < data_len && j + 3 < dst_sz; i++) {
1839 dst[j++] = hex_asc[(data[i] >> 4) & 0x0f];
1840 dst[j++] = hex_asc[data[i] & 0x0f];
1841 dst[j++] = ' ';
1843 if (j < dst_sz) {
1844 dst[j--] = '\0';
1845 dst[j] = '\n';
1846 } else
1847 dst[j] = '\0';
1850 static void picolcd_debug_out_report(struct picolcd_data *data,
1851 struct hid_device *hdev, struct hid_report *report)
1853 u8 raw_data[70];
1854 int raw_size = (report->size >> 3) + 1;
1855 char *buff;
1856 #define BUFF_SZ 256
1858 /* Avoid unnecessary overhead if debugfs is disabled */
1859 if (!hdev->debug_events)
1860 return;
1862 buff = kmalloc(BUFF_SZ, GFP_ATOMIC);
1863 if (!buff)
1864 return;
1866 snprintf(buff, BUFF_SZ, "\nout report %d (size %d) = ",
1867 report->id, raw_size);
1868 hid_debug_event(hdev, buff);
1869 if (raw_size + 5 > sizeof(raw_data)) {
1870 hid_debug_event(hdev, " TOO BIG\n");
1871 return;
1872 } else {
1873 raw_data[0] = report->id;
1874 hid_output_report(report, raw_data);
1875 dump_buff_as_hex(buff, BUFF_SZ, raw_data, raw_size);
1876 hid_debug_event(hdev, buff);
1879 switch (report->id) {
1880 case REPORT_LED_STATE:
1881 /* 1 data byte with GPO state */
1882 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1883 "REPORT_LED_STATE", report->id, raw_size-1);
1884 hid_debug_event(hdev, buff);
1885 snprintf(buff, BUFF_SZ, "\tGPO state: 0x%02x\n", raw_data[1]);
1886 hid_debug_event(hdev, buff);
1887 break;
1888 case REPORT_BRIGHTNESS:
1889 /* 1 data byte with brightness */
1890 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1891 "REPORT_BRIGHTNESS", report->id, raw_size-1);
1892 hid_debug_event(hdev, buff);
1893 snprintf(buff, BUFF_SZ, "\tBrightness: 0x%02x\n", raw_data[1]);
1894 hid_debug_event(hdev, buff);
1895 break;
1896 case REPORT_CONTRAST:
1897 /* 1 data byte with contrast */
1898 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1899 "REPORT_CONTRAST", report->id, raw_size-1);
1900 hid_debug_event(hdev, buff);
1901 snprintf(buff, BUFF_SZ, "\tContrast: 0x%02x\n", raw_data[1]);
1902 hid_debug_event(hdev, buff);
1903 break;
1904 case REPORT_RESET:
1905 /* 2 data bytes with reset duration in ms */
1906 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1907 "REPORT_RESET", report->id, raw_size-1);
1908 hid_debug_event(hdev, buff);
1909 snprintf(buff, BUFF_SZ, "\tDuration: 0x%02x%02x (%dms)\n",
1910 raw_data[2], raw_data[1], raw_data[2] << 8 | raw_data[1]);
1911 hid_debug_event(hdev, buff);
1912 break;
1913 case REPORT_LCD_CMD:
1914 /* 63 data bytes with LCD commands */
1915 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1916 "REPORT_LCD_CMD", report->id, raw_size-1);
1917 hid_debug_event(hdev, buff);
1918 /* TODO: format decoding */
1919 break;
1920 case REPORT_LCD_DATA:
1921 /* 63 data bytes with LCD data */
1922 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1923 "REPORT_LCD_CMD", report->id, raw_size-1);
1924 /* TODO: format decoding */
1925 hid_debug_event(hdev, buff);
1926 break;
1927 case REPORT_LCD_CMD_DATA:
1928 /* 63 data bytes with LCD commands and data */
1929 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1930 "REPORT_LCD_CMD", report->id, raw_size-1);
1931 /* TODO: format decoding */
1932 hid_debug_event(hdev, buff);
1933 break;
1934 case REPORT_EE_READ:
1935 /* 3 data bytes with read area description */
1936 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1937 "REPORT_EE_READ", report->id, raw_size-1);
1938 hid_debug_event(hdev, buff);
1939 snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x\n",
1940 raw_data[2], raw_data[1]);
1941 hid_debug_event(hdev, buff);
1942 snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[3]);
1943 hid_debug_event(hdev, buff);
1944 break;
1945 case REPORT_EE_WRITE:
1946 /* 3+1..20 data bytes with write area description */
1947 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1948 "REPORT_EE_WRITE", report->id, raw_size-1);
1949 hid_debug_event(hdev, buff);
1950 snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x\n",
1951 raw_data[2], raw_data[1]);
1952 hid_debug_event(hdev, buff);
1953 snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[3]);
1954 hid_debug_event(hdev, buff);
1955 if (raw_data[3] == 0) {
1956 snprintf(buff, BUFF_SZ, "\tNo data\n");
1957 } else if (raw_data[3] + 4 <= raw_size) {
1958 snprintf(buff, BUFF_SZ, "\tData: ");
1959 hid_debug_event(hdev, buff);
1960 dump_buff_as_hex(buff, BUFF_SZ, raw_data+4, raw_data[3]);
1961 } else {
1962 snprintf(buff, BUFF_SZ, "\tData overflowed\n");
1964 hid_debug_event(hdev, buff);
1965 break;
1966 case REPORT_ERASE_MEMORY:
1967 case REPORT_BL_ERASE_MEMORY:
1968 /* 3 data bytes with pointer inside erase block */
1969 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1970 "REPORT_ERASE_MEMORY", report->id, raw_size-1);
1971 hid_debug_event(hdev, buff);
1972 switch (data->addr_sz) {
1973 case 2:
1974 snprintf(buff, BUFF_SZ, "\tAddress inside 64 byte block: 0x%02x%02x\n",
1975 raw_data[2], raw_data[1]);
1976 break;
1977 case 3:
1978 snprintf(buff, BUFF_SZ, "\tAddress inside 64 byte block: 0x%02x%02x%02x\n",
1979 raw_data[3], raw_data[2], raw_data[1]);
1980 break;
1981 default:
1982 snprintf(buff, BUFF_SZ, "\tNot supported\n");
1984 hid_debug_event(hdev, buff);
1985 break;
1986 case REPORT_READ_MEMORY:
1987 case REPORT_BL_READ_MEMORY:
1988 /* 4 data bytes with read area description */
1989 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1990 "REPORT_READ_MEMORY", report->id, raw_size-1);
1991 hid_debug_event(hdev, buff);
1992 switch (data->addr_sz) {
1993 case 2:
1994 snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x\n",
1995 raw_data[2], raw_data[1]);
1996 hid_debug_event(hdev, buff);
1997 snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[3]);
1998 break;
1999 case 3:
2000 snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x%02x\n",
2001 raw_data[3], raw_data[2], raw_data[1]);
2002 hid_debug_event(hdev, buff);
2003 snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[4]);
2004 break;
2005 default:
2006 snprintf(buff, BUFF_SZ, "\tNot supported\n");
2008 hid_debug_event(hdev, buff);
2009 break;
2010 case REPORT_WRITE_MEMORY:
2011 case REPORT_BL_WRITE_MEMORY:
2012 /* 4+1..32 data bytes with write adrea description */
2013 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
2014 "REPORT_WRITE_MEMORY", report->id, raw_size-1);
2015 hid_debug_event(hdev, buff);
2016 switch (data->addr_sz) {
2017 case 2:
2018 snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x\n",
2019 raw_data[2], raw_data[1]);
2020 hid_debug_event(hdev, buff);
2021 snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[3]);
2022 hid_debug_event(hdev, buff);
2023 if (raw_data[3] == 0) {
2024 snprintf(buff, BUFF_SZ, "\tNo data\n");
2025 } else if (raw_data[3] + 4 <= raw_size) {
2026 snprintf(buff, BUFF_SZ, "\tData: ");
2027 hid_debug_event(hdev, buff);
2028 dump_buff_as_hex(buff, BUFF_SZ, raw_data+4, raw_data[3]);
2029 } else {
2030 snprintf(buff, BUFF_SZ, "\tData overflowed\n");
2032 break;
2033 case 3:
2034 snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x%02x\n",
2035 raw_data[3], raw_data[2], raw_data[1]);
2036 hid_debug_event(hdev, buff);
2037 snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[4]);
2038 hid_debug_event(hdev, buff);
2039 if (raw_data[4] == 0) {
2040 snprintf(buff, BUFF_SZ, "\tNo data\n");
2041 } else if (raw_data[4] + 5 <= raw_size) {
2042 snprintf(buff, BUFF_SZ, "\tData: ");
2043 hid_debug_event(hdev, buff);
2044 dump_buff_as_hex(buff, BUFF_SZ, raw_data+5, raw_data[4]);
2045 } else {
2046 snprintf(buff, BUFF_SZ, "\tData overflowed\n");
2048 break;
2049 default:
2050 snprintf(buff, BUFF_SZ, "\tNot supported\n");
2052 hid_debug_event(hdev, buff);
2053 break;
2054 case REPORT_SPLASH_RESTART:
2055 /* TODO */
2056 break;
2057 case REPORT_EXIT_KEYBOARD:
2058 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
2059 "REPORT_EXIT_KEYBOARD", report->id, raw_size-1);
2060 hid_debug_event(hdev, buff);
2061 snprintf(buff, BUFF_SZ, "\tRestart delay: %dms (0x%02x%02x)\n",
2062 raw_data[1] | (raw_data[2] << 8),
2063 raw_data[2], raw_data[1]);
2064 hid_debug_event(hdev, buff);
2065 break;
2066 case REPORT_VERSION:
2067 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
2068 "REPORT_VERSION", report->id, raw_size-1);
2069 hid_debug_event(hdev, buff);
2070 break;
2071 case REPORT_DEVID:
2072 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
2073 "REPORT_DEVID", report->id, raw_size-1);
2074 hid_debug_event(hdev, buff);
2075 break;
2076 case REPORT_SPLASH_SIZE:
2077 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
2078 "REPORT_SPLASH_SIZE", report->id, raw_size-1);
2079 hid_debug_event(hdev, buff);
2080 break;
2081 case REPORT_HOOK_VERSION:
2082 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
2083 "REPORT_HOOK_VERSION", report->id, raw_size-1);
2084 hid_debug_event(hdev, buff);
2085 break;
2086 case REPORT_EXIT_FLASHER:
2087 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
2088 "REPORT_VERSION", report->id, raw_size-1);
2089 hid_debug_event(hdev, buff);
2090 snprintf(buff, BUFF_SZ, "\tRestart delay: %dms (0x%02x%02x)\n",
2091 raw_data[1] | (raw_data[2] << 8),
2092 raw_data[2], raw_data[1]);
2093 hid_debug_event(hdev, buff);
2094 break;
2095 default:
2096 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
2097 "<unknown>", report->id, raw_size-1);
2098 hid_debug_event(hdev, buff);
2099 break;
2101 wake_up_interruptible(&hdev->debug_wait);
2102 kfree(buff);
2105 static void picolcd_debug_raw_event(struct picolcd_data *data,
2106 struct hid_device *hdev, struct hid_report *report,
2107 u8 *raw_data, int size)
2109 char *buff;
2111 #define BUFF_SZ 256
2112 /* Avoid unnecessary overhead if debugfs is disabled */
2113 if (!hdev->debug_events)
2114 return;
2116 buff = kmalloc(BUFF_SZ, GFP_ATOMIC);
2117 if (!buff)
2118 return;
2120 switch (report->id) {
2121 case REPORT_ERROR_CODE:
2122 /* 2 data bytes with affected report and error code */
2123 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2124 "REPORT_ERROR_CODE", report->id, size-1);
2125 hid_debug_event(hdev, buff);
2126 if (raw_data[2] < ARRAY_SIZE(error_codes))
2127 snprintf(buff, BUFF_SZ, "\tError code 0x%02x (%s) in reply to report 0x%02x\n",
2128 raw_data[2], error_codes[raw_data[2]], raw_data[1]);
2129 else
2130 snprintf(buff, BUFF_SZ, "\tError code 0x%02x in reply to report 0x%02x\n",
2131 raw_data[2], raw_data[1]);
2132 hid_debug_event(hdev, buff);
2133 break;
2134 case REPORT_KEY_STATE:
2135 /* 2 data bytes with key state */
2136 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2137 "REPORT_KEY_STATE", report->id, size-1);
2138 hid_debug_event(hdev, buff);
2139 if (raw_data[1] == 0)
2140 snprintf(buff, BUFF_SZ, "\tNo key pressed\n");
2141 else if (raw_data[2] == 0)
2142 snprintf(buff, BUFF_SZ, "\tOne key pressed: 0x%02x (%d)\n",
2143 raw_data[1], raw_data[1]);
2144 else
2145 snprintf(buff, BUFF_SZ, "\tTwo keys pressed: 0x%02x (%d), 0x%02x (%d)\n",
2146 raw_data[1], raw_data[1], raw_data[2], raw_data[2]);
2147 hid_debug_event(hdev, buff);
2148 break;
2149 case REPORT_IR_DATA:
2150 /* Up to 20 byes of IR scancode data */
2151 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2152 "REPORT_IR_DATA", report->id, size-1);
2153 hid_debug_event(hdev, buff);
2154 if (raw_data[1] == 0) {
2155 snprintf(buff, BUFF_SZ, "\tUnexpectedly 0 data length\n");
2156 hid_debug_event(hdev, buff);
2157 } else if (raw_data[1] + 1 <= size) {
2158 snprintf(buff, BUFF_SZ, "\tData length: %d\n\tIR Data: ",
2159 raw_data[1]-1);
2160 hid_debug_event(hdev, buff);
2161 dump_buff_as_hex(buff, BUFF_SZ, raw_data+2, raw_data[1]-1);
2162 hid_debug_event(hdev, buff);
2163 } else {
2164 snprintf(buff, BUFF_SZ, "\tOverflowing data length: %d\n",
2165 raw_data[1]-1);
2166 hid_debug_event(hdev, buff);
2168 break;
2169 case REPORT_EE_DATA:
2170 /* Data buffer in response to REPORT_EE_READ or REPORT_EE_WRITE */
2171 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2172 "REPORT_EE_DATA", report->id, size-1);
2173 hid_debug_event(hdev, buff);
2174 snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x\n",
2175 raw_data[2], raw_data[1]);
2176 hid_debug_event(hdev, buff);
2177 snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[3]);
2178 hid_debug_event(hdev, buff);
2179 if (raw_data[3] == 0) {
2180 snprintf(buff, BUFF_SZ, "\tNo data\n");
2181 hid_debug_event(hdev, buff);
2182 } else if (raw_data[3] + 4 <= size) {
2183 snprintf(buff, BUFF_SZ, "\tData: ");
2184 hid_debug_event(hdev, buff);
2185 dump_buff_as_hex(buff, BUFF_SZ, raw_data+4, raw_data[3]);
2186 hid_debug_event(hdev, buff);
2187 } else {
2188 snprintf(buff, BUFF_SZ, "\tData overflowed\n");
2189 hid_debug_event(hdev, buff);
2191 break;
2192 case REPORT_MEMORY:
2193 /* Data buffer in response to REPORT_READ_MEMORY or REPORT_WRTIE_MEMORY */
2194 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2195 "REPORT_MEMORY", report->id, size-1);
2196 hid_debug_event(hdev, buff);
2197 switch (data->addr_sz) {
2198 case 2:
2199 snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x\n",
2200 raw_data[2], raw_data[1]);
2201 hid_debug_event(hdev, buff);
2202 snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[3]);
2203 hid_debug_event(hdev, buff);
2204 if (raw_data[3] == 0) {
2205 snprintf(buff, BUFF_SZ, "\tNo data\n");
2206 } else if (raw_data[3] + 4 <= size) {
2207 snprintf(buff, BUFF_SZ, "\tData: ");
2208 hid_debug_event(hdev, buff);
2209 dump_buff_as_hex(buff, BUFF_SZ, raw_data+4, raw_data[3]);
2210 } else {
2211 snprintf(buff, BUFF_SZ, "\tData overflowed\n");
2213 break;
2214 case 3:
2215 snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x%02x\n",
2216 raw_data[3], raw_data[2], raw_data[1]);
2217 hid_debug_event(hdev, buff);
2218 snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[4]);
2219 hid_debug_event(hdev, buff);
2220 if (raw_data[4] == 0) {
2221 snprintf(buff, BUFF_SZ, "\tNo data\n");
2222 } else if (raw_data[4] + 5 <= size) {
2223 snprintf(buff, BUFF_SZ, "\tData: ");
2224 hid_debug_event(hdev, buff);
2225 dump_buff_as_hex(buff, BUFF_SZ, raw_data+5, raw_data[4]);
2226 } else {
2227 snprintf(buff, BUFF_SZ, "\tData overflowed\n");
2229 break;
2230 default:
2231 snprintf(buff, BUFF_SZ, "\tNot supported\n");
2233 hid_debug_event(hdev, buff);
2234 break;
2235 case REPORT_VERSION:
2236 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2237 "REPORT_VERSION", report->id, size-1);
2238 hid_debug_event(hdev, buff);
2239 snprintf(buff, BUFF_SZ, "\tFirmware version: %d.%d\n",
2240 raw_data[2], raw_data[1]);
2241 hid_debug_event(hdev, buff);
2242 break;
2243 case REPORT_BL_ERASE_MEMORY:
2244 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2245 "REPORT_BL_ERASE_MEMORY", report->id, size-1);
2246 hid_debug_event(hdev, buff);
2247 /* TODO */
2248 break;
2249 case REPORT_BL_READ_MEMORY:
2250 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2251 "REPORT_BL_READ_MEMORY", report->id, size-1);
2252 hid_debug_event(hdev, buff);
2253 /* TODO */
2254 break;
2255 case REPORT_BL_WRITE_MEMORY:
2256 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2257 "REPORT_BL_WRITE_MEMORY", report->id, size-1);
2258 hid_debug_event(hdev, buff);
2259 /* TODO */
2260 break;
2261 case REPORT_DEVID:
2262 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2263 "REPORT_DEVID", report->id, size-1);
2264 hid_debug_event(hdev, buff);
2265 snprintf(buff, BUFF_SZ, "\tSerial: 0x%02x%02x%02x%02x\n",
2266 raw_data[1], raw_data[2], raw_data[3], raw_data[4]);
2267 hid_debug_event(hdev, buff);
2268 snprintf(buff, BUFF_SZ, "\tType: 0x%02x\n",
2269 raw_data[5]);
2270 hid_debug_event(hdev, buff);
2271 break;
2272 case REPORT_SPLASH_SIZE:
2273 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2274 "REPORT_SPLASH_SIZE", report->id, size-1);
2275 hid_debug_event(hdev, buff);
2276 snprintf(buff, BUFF_SZ, "\tTotal splash space: %d\n",
2277 (raw_data[2] << 8) | raw_data[1]);
2278 hid_debug_event(hdev, buff);
2279 snprintf(buff, BUFF_SZ, "\tUsed splash space: %d\n",
2280 (raw_data[4] << 8) | raw_data[3]);
2281 hid_debug_event(hdev, buff);
2282 break;
2283 case REPORT_HOOK_VERSION:
2284 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2285 "REPORT_HOOK_VERSION", report->id, size-1);
2286 hid_debug_event(hdev, buff);
2287 snprintf(buff, BUFF_SZ, "\tFirmware version: %d.%d\n",
2288 raw_data[1], raw_data[2]);
2289 hid_debug_event(hdev, buff);
2290 break;
2291 default:
2292 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2293 "<unknown>", report->id, size-1);
2294 hid_debug_event(hdev, buff);
2295 break;
2297 wake_up_interruptible(&hdev->debug_wait);
2298 kfree(buff);
2301 static void picolcd_init_devfs(struct picolcd_data *data,
2302 struct hid_report *eeprom_r, struct hid_report *eeprom_w,
2303 struct hid_report *flash_r, struct hid_report *flash_w,
2304 struct hid_report *reset)
2306 struct hid_device *hdev = data->hdev;
2308 mutex_init(&data->mutex_flash);
2310 /* reset */
2311 if (reset)
2312 data->debug_reset = debugfs_create_file("reset", 0600,
2313 hdev->debug_dir, data, &picolcd_debug_reset_fops);
2315 /* eeprom */
2316 if (eeprom_r || eeprom_w)
2317 data->debug_eeprom = debugfs_create_file("eeprom",
2318 (eeprom_w ? S_IWUSR : 0) | (eeprom_r ? S_IRUSR : 0),
2319 hdev->debug_dir, data, &picolcd_debug_eeprom_fops);
2321 /* flash */
2322 if (flash_r && flash_r->maxfield == 1 && flash_r->field[0]->report_size == 8)
2323 data->addr_sz = flash_r->field[0]->report_count - 1;
2324 else
2325 data->addr_sz = -1;
2326 if (data->addr_sz == 2 || data->addr_sz == 3) {
2327 data->debug_flash = debugfs_create_file("flash",
2328 (flash_w ? S_IWUSR : 0) | (flash_r ? S_IRUSR : 0),
2329 hdev->debug_dir, data, &picolcd_debug_flash_fops);
2330 } else if (flash_r || flash_w)
2331 dev_warn(&hdev->dev, "Unexpected FLASH access reports, "
2332 "please submit rdesc for review\n");
2335 static void picolcd_exit_devfs(struct picolcd_data *data)
2337 struct dentry *dent;
2339 dent = data->debug_reset;
2340 data->debug_reset = NULL;
2341 if (dent)
2342 debugfs_remove(dent);
2343 dent = data->debug_eeprom;
2344 data->debug_eeprom = NULL;
2345 if (dent)
2346 debugfs_remove(dent);
2347 dent = data->debug_flash;
2348 data->debug_flash = NULL;
2349 if (dent)
2350 debugfs_remove(dent);
2351 mutex_destroy(&data->mutex_flash);
2353 #else
2354 static inline void picolcd_debug_raw_event(struct picolcd_data *data,
2355 struct hid_device *hdev, struct hid_report *report,
2356 u8 *raw_data, int size)
2359 static inline void picolcd_init_devfs(struct picolcd_data *data,
2360 struct hid_report *eeprom_r, struct hid_report *eeprom_w,
2361 struct hid_report *flash_r, struct hid_report *flash_w,
2362 struct hid_report *reset)
2365 static inline void picolcd_exit_devfs(struct picolcd_data *data)
2368 #endif /* CONFIG_DEBUG_FS */
2371 * Handle raw report as sent by device
2373 static int picolcd_raw_event(struct hid_device *hdev,
2374 struct hid_report *report, u8 *raw_data, int size)
2376 struct picolcd_data *data = hid_get_drvdata(hdev);
2377 unsigned long flags;
2378 int ret = 0;
2380 if (!data)
2381 return 1;
2383 if (report->id == REPORT_KEY_STATE) {
2384 if (data->input_keys)
2385 ret = picolcd_raw_keypad(data, report, raw_data+1, size-1);
2386 } else if (report->id == REPORT_IR_DATA) {
2387 if (data->input_cir)
2388 ret = picolcd_raw_cir(data, report, raw_data+1, size-1);
2389 } else {
2390 spin_lock_irqsave(&data->lock, flags);
2392 * We let the caller of picolcd_send_and_wait() check if the
2393 * report we got is one of the expected ones or not.
2395 if (data->pending) {
2396 memcpy(data->pending->raw_data, raw_data+1, size-1);
2397 data->pending->raw_size = size-1;
2398 data->pending->in_report = report;
2399 complete(&data->pending->ready);
2401 spin_unlock_irqrestore(&data->lock, flags);
2404 picolcd_debug_raw_event(data, hdev, report, raw_data, size);
2405 return 1;
2408 #ifdef CONFIG_PM
2409 static int picolcd_suspend(struct hid_device *hdev, pm_message_t message)
2411 if (message.event & PM_EVENT_AUTO)
2412 return 0;
2414 picolcd_suspend_backlight(hid_get_drvdata(hdev));
2415 dbg_hid(PICOLCD_NAME " device ready for suspend\n");
2416 return 0;
2419 static int picolcd_resume(struct hid_device *hdev)
2421 int ret;
2422 ret = picolcd_resume_backlight(hid_get_drvdata(hdev));
2423 if (ret)
2424 dbg_hid(PICOLCD_NAME " restoring backlight failed: %d\n", ret);
2425 return 0;
2428 static int picolcd_reset_resume(struct hid_device *hdev)
2430 int ret;
2431 ret = picolcd_reset(hdev);
2432 if (ret)
2433 dbg_hid(PICOLCD_NAME " resetting our device failed: %d\n", ret);
2434 ret = picolcd_fb_reset(hid_get_drvdata(hdev), 0);
2435 if (ret)
2436 dbg_hid(PICOLCD_NAME " restoring framebuffer content failed: %d\n", ret);
2437 ret = picolcd_resume_lcd(hid_get_drvdata(hdev));
2438 if (ret)
2439 dbg_hid(PICOLCD_NAME " restoring lcd failed: %d\n", ret);
2440 ret = picolcd_resume_backlight(hid_get_drvdata(hdev));
2441 if (ret)
2442 dbg_hid(PICOLCD_NAME " restoring backlight failed: %d\n", ret);
2443 picolcd_leds_set(hid_get_drvdata(hdev));
2444 return 0;
2446 #endif
2448 /* initialize keypad input device */
2449 static int picolcd_init_keys(struct picolcd_data *data,
2450 struct hid_report *report)
2452 struct hid_device *hdev = data->hdev;
2453 struct input_dev *idev;
2454 int error, i;
2456 if (!report)
2457 return -ENODEV;
2458 if (report->maxfield != 1 || report->field[0]->report_count != 2 ||
2459 report->field[0]->report_size != 8) {
2460 dev_err(&hdev->dev, "unsupported KEY_STATE report");
2461 return -EINVAL;
2464 idev = input_allocate_device();
2465 if (idev == NULL) {
2466 dev_err(&hdev->dev, "failed to allocate input device");
2467 return -ENOMEM;
2469 input_set_drvdata(idev, hdev);
2470 memcpy(data->keycode, def_keymap, sizeof(def_keymap));
2471 idev->name = hdev->name;
2472 idev->phys = hdev->phys;
2473 idev->uniq = hdev->uniq;
2474 idev->id.bustype = hdev->bus;
2475 idev->id.vendor = hdev->vendor;
2476 idev->id.product = hdev->product;
2477 idev->id.version = hdev->version;
2478 idev->dev.parent = hdev->dev.parent;
2479 idev->keycode = &data->keycode;
2480 idev->keycodemax = PICOLCD_KEYS;
2481 idev->keycodesize = sizeof(data->keycode[0]);
2482 input_set_capability(idev, EV_MSC, MSC_SCAN);
2483 set_bit(EV_REP, idev->evbit);
2484 for (i = 0; i < PICOLCD_KEYS; i++)
2485 input_set_capability(idev, EV_KEY, data->keycode[i]);
2486 error = input_register_device(idev);
2487 if (error) {
2488 dev_err(&hdev->dev, "error registering the input device");
2489 input_free_device(idev);
2490 return error;
2492 data->input_keys = idev;
2493 return 0;
2496 static void picolcd_exit_keys(struct picolcd_data *data)
2498 struct input_dev *idev = data->input_keys;
2500 data->input_keys = NULL;
2501 if (idev)
2502 input_unregister_device(idev);
2505 /* initialize CIR input device */
2506 static inline int picolcd_init_cir(struct picolcd_data *data, struct hid_report *report)
2508 /* support not implemented yet */
2509 return 0;
2512 static inline void picolcd_exit_cir(struct picolcd_data *data)
2516 static int picolcd_probe_lcd(struct hid_device *hdev, struct picolcd_data *data)
2518 int error;
2520 error = picolcd_check_version(hdev);
2521 if (error)
2522 return error;
2524 if (data->version[0] != 0 && data->version[1] != 3)
2525 dev_info(&hdev->dev, "Device with untested firmware revision, "
2526 "please submit /sys/kernel/debug/hid/%s/rdesc for this device.\n",
2527 dev_name(&hdev->dev));
2529 /* Setup keypad input device */
2530 error = picolcd_init_keys(data, picolcd_in_report(REPORT_KEY_STATE, hdev));
2531 if (error)
2532 goto err;
2534 /* Setup CIR input device */
2535 error = picolcd_init_cir(data, picolcd_in_report(REPORT_IR_DATA, hdev));
2536 if (error)
2537 goto err;
2539 /* Set up the framebuffer device */
2540 error = picolcd_init_framebuffer(data);
2541 if (error)
2542 goto err;
2544 /* Setup lcd class device */
2545 error = picolcd_init_lcd(data, picolcd_out_report(REPORT_CONTRAST, hdev));
2546 if (error)
2547 goto err;
2549 /* Setup backlight class device */
2550 error = picolcd_init_backlight(data, picolcd_out_report(REPORT_BRIGHTNESS, hdev));
2551 if (error)
2552 goto err;
2554 /* Setup the LED class devices */
2555 error = picolcd_init_leds(data, picolcd_out_report(REPORT_LED_STATE, hdev));
2556 if (error)
2557 goto err;
2559 picolcd_init_devfs(data, picolcd_out_report(REPORT_EE_READ, hdev),
2560 picolcd_out_report(REPORT_EE_WRITE, hdev),
2561 picolcd_out_report(REPORT_READ_MEMORY, hdev),
2562 picolcd_out_report(REPORT_WRITE_MEMORY, hdev),
2563 picolcd_out_report(REPORT_RESET, hdev));
2564 return 0;
2565 err:
2566 picolcd_exit_leds(data);
2567 picolcd_exit_backlight(data);
2568 picolcd_exit_lcd(data);
2569 picolcd_exit_framebuffer(data);
2570 picolcd_exit_cir(data);
2571 picolcd_exit_keys(data);
2572 return error;
2575 static int picolcd_probe_bootloader(struct hid_device *hdev, struct picolcd_data *data)
2577 int error;
2579 error = picolcd_check_version(hdev);
2580 if (error)
2581 return error;
2583 if (data->version[0] != 1 && data->version[1] != 0)
2584 dev_info(&hdev->dev, "Device with untested bootloader revision, "
2585 "please submit /sys/kernel/debug/hid/%s/rdesc for this device.\n",
2586 dev_name(&hdev->dev));
2588 picolcd_init_devfs(data, NULL, NULL,
2589 picolcd_out_report(REPORT_BL_READ_MEMORY, hdev),
2590 picolcd_out_report(REPORT_BL_WRITE_MEMORY, hdev), NULL);
2591 return 0;
2594 static int picolcd_probe(struct hid_device *hdev,
2595 const struct hid_device_id *id)
2597 struct picolcd_data *data;
2598 int error = -ENOMEM;
2600 dbg_hid(PICOLCD_NAME " hardware probe...\n");
2603 * Let's allocate the picolcd data structure, set some reasonable
2604 * defaults, and associate it with the device
2606 data = kzalloc(sizeof(struct picolcd_data), GFP_KERNEL);
2607 if (data == NULL) {
2608 dev_err(&hdev->dev, "can't allocate space for Minibox PicoLCD device data\n");
2609 error = -ENOMEM;
2610 goto err_no_cleanup;
2613 spin_lock_init(&data->lock);
2614 mutex_init(&data->mutex);
2615 data->hdev = hdev;
2616 data->opmode_delay = 5000;
2617 if (hdev->product == USB_DEVICE_ID_PICOLCD_BOOTLOADER)
2618 data->status |= PICOLCD_BOOTLOADER;
2619 hid_set_drvdata(hdev, data);
2621 /* Parse the device reports and start it up */
2622 error = hid_parse(hdev);
2623 if (error) {
2624 dev_err(&hdev->dev, "device report parse failed\n");
2625 goto err_cleanup_data;
2628 /* We don't use hidinput but hid_hw_start() fails if nothing is
2629 * claimed. So spoof claimed input. */
2630 hdev->claimed = HID_CLAIMED_INPUT;
2631 error = hid_hw_start(hdev, 0);
2632 hdev->claimed = 0;
2633 if (error) {
2634 dev_err(&hdev->dev, "hardware start failed\n");
2635 goto err_cleanup_data;
2638 error = hdev->ll_driver->open(hdev);
2639 if (error) {
2640 dev_err(&hdev->dev, "failed to open input interrupt pipe for key and IR events\n");
2641 goto err_cleanup_hid_hw;
2644 error = device_create_file(&hdev->dev, &dev_attr_operation_mode_delay);
2645 if (error) {
2646 dev_err(&hdev->dev, "failed to create sysfs attributes\n");
2647 goto err_cleanup_hid_ll;
2650 error = device_create_file(&hdev->dev, &dev_attr_operation_mode);
2651 if (error) {
2652 dev_err(&hdev->dev, "failed to create sysfs attributes\n");
2653 goto err_cleanup_sysfs1;
2656 if (data->status & PICOLCD_BOOTLOADER)
2657 error = picolcd_probe_bootloader(hdev, data);
2658 else
2659 error = picolcd_probe_lcd(hdev, data);
2660 if (error)
2661 goto err_cleanup_sysfs2;
2663 dbg_hid(PICOLCD_NAME " activated and initialized\n");
2664 return 0;
2666 err_cleanup_sysfs2:
2667 device_remove_file(&hdev->dev, &dev_attr_operation_mode);
2668 err_cleanup_sysfs1:
2669 device_remove_file(&hdev->dev, &dev_attr_operation_mode_delay);
2670 err_cleanup_hid_ll:
2671 hdev->ll_driver->close(hdev);
2672 err_cleanup_hid_hw:
2673 hid_hw_stop(hdev);
2674 err_cleanup_data:
2675 kfree(data);
2676 err_no_cleanup:
2677 hid_set_drvdata(hdev, NULL);
2679 return error;
2682 static void picolcd_remove(struct hid_device *hdev)
2684 struct picolcd_data *data = hid_get_drvdata(hdev);
2685 unsigned long flags;
2687 dbg_hid(PICOLCD_NAME " hardware remove...\n");
2688 spin_lock_irqsave(&data->lock, flags);
2689 data->status |= PICOLCD_FAILED;
2690 spin_unlock_irqrestore(&data->lock, flags);
2691 #ifdef CONFIG_HID_PICOLCD_FB
2692 /* short-circuit FB as early as possible in order to
2693 * avoid long delays if we host console.
2695 if (data->fb_info)
2696 data->fb_info->par = NULL;
2697 #endif
2699 picolcd_exit_devfs(data);
2700 device_remove_file(&hdev->dev, &dev_attr_operation_mode);
2701 device_remove_file(&hdev->dev, &dev_attr_operation_mode_delay);
2702 hdev->ll_driver->close(hdev);
2703 hid_hw_stop(hdev);
2704 hid_set_drvdata(hdev, NULL);
2706 /* Shortcut potential pending reply that will never arrive */
2707 spin_lock_irqsave(&data->lock, flags);
2708 if (data->pending)
2709 complete(&data->pending->ready);
2710 spin_unlock_irqrestore(&data->lock, flags);
2712 /* Cleanup LED */
2713 picolcd_exit_leds(data);
2714 /* Clean up the framebuffer */
2715 picolcd_exit_backlight(data);
2716 picolcd_exit_lcd(data);
2717 picolcd_exit_framebuffer(data);
2718 /* Cleanup input */
2719 picolcd_exit_cir(data);
2720 picolcd_exit_keys(data);
2722 mutex_destroy(&data->mutex);
2723 /* Finally, clean up the picolcd data itself */
2724 kfree(data);
2727 static const struct hid_device_id picolcd_devices[] = {
2728 { HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP, USB_DEVICE_ID_PICOLCD) },
2729 { HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP, USB_DEVICE_ID_PICOLCD_BOOTLOADER) },
2732 MODULE_DEVICE_TABLE(hid, picolcd_devices);
2734 static struct hid_driver picolcd_driver = {
2735 .name = "hid-picolcd",
2736 .id_table = picolcd_devices,
2737 .probe = picolcd_probe,
2738 .remove = picolcd_remove,
2739 .raw_event = picolcd_raw_event,
2740 #ifdef CONFIG_PM
2741 .suspend = picolcd_suspend,
2742 .resume = picolcd_resume,
2743 .reset_resume = picolcd_reset_resume,
2744 #endif
2747 static int __init picolcd_init(void)
2749 return hid_register_driver(&picolcd_driver);
2752 static void __exit picolcd_exit(void)
2754 hid_unregister_driver(&picolcd_driver);
2755 #ifdef CONFIG_HID_PICOLCD_FB
2756 flush_scheduled_work();
2757 WARN_ON(fb_pending);
2758 #endif
2761 module_init(picolcd_init);
2762 module_exit(picolcd_exit);
2763 MODULE_DESCRIPTION("Minibox graphics PicoLCD Driver");
2764 MODULE_LICENSE("GPL v2");