Add linux-next specific files for 20110831
[linux-2.6/next.git] / drivers / hid / hid-picolcd.c
blob01e7d2cd7c26a55170d2f7eda4e0a83cb37cb55d
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>
39 #include <linux/module.h>
41 #define PICOLCD_NAME "PicoLCD (graphic)"
43 /* Report numbers */
44 #define REPORT_ERROR_CODE 0x10 /* LCD: IN[16] */
45 #define ERR_SUCCESS 0x00
46 #define ERR_PARAMETER_MISSING 0x01
47 #define ERR_DATA_MISSING 0x02
48 #define ERR_BLOCK_READ_ONLY 0x03
49 #define ERR_BLOCK_NOT_ERASABLE 0x04
50 #define ERR_BLOCK_TOO_BIG 0x05
51 #define ERR_SECTION_OVERFLOW 0x06
52 #define ERR_INVALID_CMD_LEN 0x07
53 #define ERR_INVALID_DATA_LEN 0x08
54 #define REPORT_KEY_STATE 0x11 /* LCD: IN[2] */
55 #define REPORT_IR_DATA 0x21 /* LCD: IN[63] */
56 #define REPORT_EE_DATA 0x32 /* LCD: IN[63] */
57 #define REPORT_MEMORY 0x41 /* LCD: IN[63] */
58 #define REPORT_LED_STATE 0x81 /* LCD: OUT[1] */
59 #define REPORT_BRIGHTNESS 0x91 /* LCD: OUT[1] */
60 #define REPORT_CONTRAST 0x92 /* LCD: OUT[1] */
61 #define REPORT_RESET 0x93 /* LCD: OUT[2] */
62 #define REPORT_LCD_CMD 0x94 /* LCD: OUT[63] */
63 #define REPORT_LCD_DATA 0x95 /* LCD: OUT[63] */
64 #define REPORT_LCD_CMD_DATA 0x96 /* LCD: OUT[63] */
65 #define REPORT_EE_READ 0xa3 /* LCD: OUT[63] */
66 #define REPORT_EE_WRITE 0xa4 /* LCD: OUT[63] */
67 #define REPORT_ERASE_MEMORY 0xb2 /* LCD: OUT[2] */
68 #define REPORT_READ_MEMORY 0xb3 /* LCD: OUT[3] */
69 #define REPORT_WRITE_MEMORY 0xb4 /* LCD: OUT[63] */
70 #define REPORT_SPLASH_RESTART 0xc1 /* LCD: OUT[1] */
71 #define REPORT_EXIT_KEYBOARD 0xef /* LCD: OUT[2] */
72 #define REPORT_VERSION 0xf1 /* LCD: IN[2],OUT[1] Bootloader: IN[2],OUT[1] */
73 #define REPORT_BL_ERASE_MEMORY 0xf2 /* Bootloader: IN[36],OUT[4] */
74 #define REPORT_BL_READ_MEMORY 0xf3 /* Bootloader: IN[36],OUT[4] */
75 #define REPORT_BL_WRITE_MEMORY 0xf4 /* Bootloader: IN[36],OUT[36] */
76 #define REPORT_DEVID 0xf5 /* LCD: IN[5], OUT[1] Bootloader: IN[5],OUT[1] */
77 #define REPORT_SPLASH_SIZE 0xf6 /* LCD: IN[4], OUT[1] */
78 #define REPORT_HOOK_VERSION 0xf7 /* LCD: IN[2], OUT[1] */
79 #define REPORT_EXIT_FLASHER 0xff /* Bootloader: OUT[2] */
81 #ifdef CONFIG_HID_PICOLCD_FB
82 /* Framebuffer
84 * The PicoLCD use a Topway LCD module of 256x64 pixel
85 * This display area is tiled over 4 controllers with 8 tiles
86 * each. Each tile has 8x64 pixel, each data byte representing
87 * a 1-bit wide vertical line of the tile.
89 * The display can be updated at a tile granularity.
91 * Chip 1 Chip 2 Chip 3 Chip 4
92 * +----------------+----------------+----------------+----------------+
93 * | Tile 1 | Tile 1 | Tile 1 | Tile 1 |
94 * +----------------+----------------+----------------+----------------+
95 * | Tile 2 | Tile 2 | Tile 2 | Tile 2 |
96 * +----------------+----------------+----------------+----------------+
97 * ...
98 * +----------------+----------------+----------------+----------------+
99 * | Tile 8 | Tile 8 | Tile 8 | Tile 8 |
100 * +----------------+----------------+----------------+----------------+
102 #define PICOLCDFB_NAME "picolcdfb"
103 #define PICOLCDFB_WIDTH (256)
104 #define PICOLCDFB_HEIGHT (64)
105 #define PICOLCDFB_SIZE (PICOLCDFB_WIDTH * PICOLCDFB_HEIGHT / 8)
107 #define PICOLCDFB_UPDATE_RATE_LIMIT 10
108 #define PICOLCDFB_UPDATE_RATE_DEFAULT 2
110 /* Framebuffer visual structures */
111 static const struct fb_fix_screeninfo picolcdfb_fix = {
112 .id = PICOLCDFB_NAME,
113 .type = FB_TYPE_PACKED_PIXELS,
114 .visual = FB_VISUAL_MONO01,
115 .xpanstep = 0,
116 .ypanstep = 0,
117 .ywrapstep = 0,
118 .line_length = PICOLCDFB_WIDTH / 8,
119 .accel = FB_ACCEL_NONE,
122 static const struct fb_var_screeninfo picolcdfb_var = {
123 .xres = PICOLCDFB_WIDTH,
124 .yres = PICOLCDFB_HEIGHT,
125 .xres_virtual = PICOLCDFB_WIDTH,
126 .yres_virtual = PICOLCDFB_HEIGHT,
127 .width = 103,
128 .height = 26,
129 .bits_per_pixel = 1,
130 .grayscale = 1,
131 .red = {
132 .offset = 0,
133 .length = 1,
134 .msb_right = 0,
136 .green = {
137 .offset = 0,
138 .length = 1,
139 .msb_right = 0,
141 .blue = {
142 .offset = 0,
143 .length = 1,
144 .msb_right = 0,
146 .transp = {
147 .offset = 0,
148 .length = 0,
149 .msb_right = 0,
152 #endif /* CONFIG_HID_PICOLCD_FB */
154 /* Input device
156 * The PicoLCD has an IR receiver header, a built-in keypad with 5 keys
157 * and header for 4x4 key matrix. The built-in keys are part of the matrix.
159 static const unsigned short def_keymap[] = {
160 KEY_RESERVED, /* none */
161 KEY_BACK, /* col 4 + row 1 */
162 KEY_HOMEPAGE, /* col 3 + row 1 */
163 KEY_RESERVED, /* col 2 + row 1 */
164 KEY_RESERVED, /* col 1 + row 1 */
165 KEY_SCROLLUP, /* col 4 + row 2 */
166 KEY_OK, /* col 3 + row 2 */
167 KEY_SCROLLDOWN, /* col 2 + row 2 */
168 KEY_RESERVED, /* col 1 + row 2 */
169 KEY_RESERVED, /* col 4 + row 3 */
170 KEY_RESERVED, /* col 3 + row 3 */
171 KEY_RESERVED, /* col 2 + row 3 */
172 KEY_RESERVED, /* col 1 + row 3 */
173 KEY_RESERVED, /* col 4 + row 4 */
174 KEY_RESERVED, /* col 3 + row 4 */
175 KEY_RESERVED, /* col 2 + row 4 */
176 KEY_RESERVED, /* col 1 + row 4 */
178 #define PICOLCD_KEYS ARRAY_SIZE(def_keymap)
180 /* Description of in-progress IO operation, used for operations
181 * that trigger response from device */
182 struct picolcd_pending {
183 struct hid_report *out_report;
184 struct hid_report *in_report;
185 struct completion ready;
186 int raw_size;
187 u8 raw_data[64];
190 /* Per device data structure */
191 struct picolcd_data {
192 struct hid_device *hdev;
193 #ifdef CONFIG_DEBUG_FS
194 struct dentry *debug_reset;
195 struct dentry *debug_eeprom;
196 struct dentry *debug_flash;
197 struct mutex mutex_flash;
198 int addr_sz;
199 #endif
200 u8 version[2];
201 unsigned short opmode_delay;
202 /* input stuff */
203 u8 pressed_keys[2];
204 struct input_dev *input_keys;
205 struct input_dev *input_cir;
206 unsigned short keycode[PICOLCD_KEYS];
208 #ifdef CONFIG_HID_PICOLCD_FB
209 /* Framebuffer stuff */
210 u8 fb_update_rate;
211 u8 fb_bpp;
212 u8 fb_force;
213 u8 *fb_vbitmap; /* local copy of what was sent to PicoLCD */
214 u8 *fb_bitmap; /* framebuffer */
215 struct fb_info *fb_info;
216 struct fb_deferred_io fb_defio;
217 #endif /* CONFIG_HID_PICOLCD_FB */
218 #ifdef CONFIG_HID_PICOLCD_LCD
219 struct lcd_device *lcd;
220 u8 lcd_contrast;
221 #endif /* CONFIG_HID_PICOLCD_LCD */
222 #ifdef CONFIG_HID_PICOLCD_BACKLIGHT
223 struct backlight_device *backlight;
224 u8 lcd_brightness;
225 u8 lcd_power;
226 #endif /* CONFIG_HID_PICOLCD_BACKLIGHT */
227 #ifdef CONFIG_HID_PICOLCD_LEDS
228 /* LED stuff */
229 u8 led_state;
230 struct led_classdev *led[8];
231 #endif /* CONFIG_HID_PICOLCD_LEDS */
233 /* Housekeeping stuff */
234 spinlock_t lock;
235 struct mutex mutex;
236 struct picolcd_pending *pending;
237 int status;
238 #define PICOLCD_BOOTLOADER 1
239 #define PICOLCD_FAILED 2
240 #define PICOLCD_READY_FB 4
244 /* Find a given report */
245 #define picolcd_in_report(id, dev) picolcd_report(id, dev, HID_INPUT_REPORT)
246 #define picolcd_out_report(id, dev) picolcd_report(id, dev, HID_OUTPUT_REPORT)
248 static struct hid_report *picolcd_report(int id, struct hid_device *hdev, int dir)
250 struct list_head *feature_report_list = &hdev->report_enum[dir].report_list;
251 struct hid_report *report = NULL;
253 list_for_each_entry(report, feature_report_list, list) {
254 if (report->id == id)
255 return report;
257 hid_warn(hdev, "No report with id 0x%x found\n", id);
258 return NULL;
261 #ifdef CONFIG_DEBUG_FS
262 static void picolcd_debug_out_report(struct picolcd_data *data,
263 struct hid_device *hdev, struct hid_report *report);
264 #define usbhid_submit_report(a, b, c) \
265 do { \
266 picolcd_debug_out_report(hid_get_drvdata(a), a, b); \
267 usbhid_submit_report(a, b, c); \
268 } while (0)
269 #endif
271 /* Submit a report and wait for a reply from device - if device fades away
272 * or does not respond in time, return NULL */
273 static struct picolcd_pending *picolcd_send_and_wait(struct hid_device *hdev,
274 int report_id, const u8 *raw_data, int size)
276 struct picolcd_data *data = hid_get_drvdata(hdev);
277 struct picolcd_pending *work;
278 struct hid_report *report = picolcd_out_report(report_id, hdev);
279 unsigned long flags;
280 int i, j, k;
282 if (!report || !data)
283 return NULL;
284 if (data->status & PICOLCD_FAILED)
285 return NULL;
286 work = kzalloc(sizeof(*work), GFP_KERNEL);
287 if (!work)
288 return NULL;
290 init_completion(&work->ready);
291 work->out_report = report;
292 work->in_report = NULL;
293 work->raw_size = 0;
295 mutex_lock(&data->mutex);
296 spin_lock_irqsave(&data->lock, flags);
297 for (i = k = 0; i < report->maxfield; i++)
298 for (j = 0; j < report->field[i]->report_count; j++) {
299 hid_set_field(report->field[i], j, k < size ? raw_data[k] : 0);
300 k++;
302 data->pending = work;
303 usbhid_submit_report(data->hdev, report, USB_DIR_OUT);
304 spin_unlock_irqrestore(&data->lock, flags);
305 wait_for_completion_interruptible_timeout(&work->ready, HZ*2);
306 spin_lock_irqsave(&data->lock, flags);
307 data->pending = NULL;
308 spin_unlock_irqrestore(&data->lock, flags);
309 mutex_unlock(&data->mutex);
310 return work;
313 #ifdef CONFIG_HID_PICOLCD_FB
314 /* Send a given tile to PicoLCD */
315 static int picolcd_fb_send_tile(struct hid_device *hdev, int chip, int tile)
317 struct picolcd_data *data = hid_get_drvdata(hdev);
318 struct hid_report *report1 = picolcd_out_report(REPORT_LCD_CMD_DATA, hdev);
319 struct hid_report *report2 = picolcd_out_report(REPORT_LCD_DATA, hdev);
320 unsigned long flags;
321 u8 *tdata;
322 int i;
324 if (!report1 || report1->maxfield != 1 || !report2 || report2->maxfield != 1)
325 return -ENODEV;
327 spin_lock_irqsave(&data->lock, flags);
328 hid_set_field(report1->field[0], 0, chip << 2);
329 hid_set_field(report1->field[0], 1, 0x02);
330 hid_set_field(report1->field[0], 2, 0x00);
331 hid_set_field(report1->field[0], 3, 0x00);
332 hid_set_field(report1->field[0], 4, 0xb8 | tile);
333 hid_set_field(report1->field[0], 5, 0x00);
334 hid_set_field(report1->field[0], 6, 0x00);
335 hid_set_field(report1->field[0], 7, 0x40);
336 hid_set_field(report1->field[0], 8, 0x00);
337 hid_set_field(report1->field[0], 9, 0x00);
338 hid_set_field(report1->field[0], 10, 32);
340 hid_set_field(report2->field[0], 0, (chip << 2) | 0x01);
341 hid_set_field(report2->field[0], 1, 0x00);
342 hid_set_field(report2->field[0], 2, 0x00);
343 hid_set_field(report2->field[0], 3, 32);
345 tdata = data->fb_vbitmap + (tile * 4 + chip) * 64;
346 for (i = 0; i < 64; i++)
347 if (i < 32)
348 hid_set_field(report1->field[0], 11 + i, tdata[i]);
349 else
350 hid_set_field(report2->field[0], 4 + i - 32, tdata[i]);
352 usbhid_submit_report(data->hdev, report1, USB_DIR_OUT);
353 usbhid_submit_report(data->hdev, report2, USB_DIR_OUT);
354 spin_unlock_irqrestore(&data->lock, flags);
355 return 0;
358 /* Translate a single tile*/
359 static int picolcd_fb_update_tile(u8 *vbitmap, const u8 *bitmap, int bpp,
360 int chip, int tile)
362 int i, b, changed = 0;
363 u8 tdata[64];
364 u8 *vdata = vbitmap + (tile * 4 + chip) * 64;
366 if (bpp == 1) {
367 for (b = 7; b >= 0; b--) {
368 const u8 *bdata = bitmap + tile * 256 + chip * 8 + b * 32;
369 for (i = 0; i < 64; i++) {
370 tdata[i] <<= 1;
371 tdata[i] |= (bdata[i/8] >> (i % 8)) & 0x01;
374 } else if (bpp == 8) {
375 for (b = 7; b >= 0; b--) {
376 const u8 *bdata = bitmap + (tile * 256 + chip * 8 + b * 32) * 8;
377 for (i = 0; i < 64; i++) {
378 tdata[i] <<= 1;
379 tdata[i] |= (bdata[i] & 0x80) ? 0x01 : 0x00;
382 } else {
383 /* Oops, we should never get here! */
384 WARN_ON(1);
385 return 0;
388 for (i = 0; i < 64; i++)
389 if (tdata[i] != vdata[i]) {
390 changed = 1;
391 vdata[i] = tdata[i];
393 return changed;
396 /* Reconfigure LCD display */
397 static int picolcd_fb_reset(struct picolcd_data *data, int clear)
399 struct hid_report *report = picolcd_out_report(REPORT_LCD_CMD, data->hdev);
400 int i, j;
401 unsigned long flags;
402 static const u8 mapcmd[8] = { 0x00, 0x02, 0x00, 0x64, 0x3f, 0x00, 0x64, 0xc0 };
404 if (!report || report->maxfield != 1)
405 return -ENODEV;
407 spin_lock_irqsave(&data->lock, flags);
408 for (i = 0; i < 4; i++) {
409 for (j = 0; j < report->field[0]->maxusage; j++)
410 if (j == 0)
411 hid_set_field(report->field[0], j, i << 2);
412 else if (j < sizeof(mapcmd))
413 hid_set_field(report->field[0], j, mapcmd[j]);
414 else
415 hid_set_field(report->field[0], j, 0);
416 usbhid_submit_report(data->hdev, report, USB_DIR_OUT);
419 data->status |= PICOLCD_READY_FB;
420 spin_unlock_irqrestore(&data->lock, flags);
422 if (data->fb_bitmap) {
423 if (clear) {
424 memset(data->fb_vbitmap, 0, PICOLCDFB_SIZE);
425 memset(data->fb_bitmap, 0, PICOLCDFB_SIZE*data->fb_bpp);
427 data->fb_force = 1;
430 /* schedule first output of framebuffer */
431 if (data->fb_info)
432 schedule_delayed_work(&data->fb_info->deferred_work, 0);
434 return 0;
437 /* Update fb_vbitmap from the screen_base and send changed tiles to device */
438 static void picolcd_fb_update(struct picolcd_data *data)
440 int chip, tile, n;
441 unsigned long flags;
443 if (!data)
444 return;
446 spin_lock_irqsave(&data->lock, flags);
447 if (!(data->status & PICOLCD_READY_FB)) {
448 spin_unlock_irqrestore(&data->lock, flags);
449 picolcd_fb_reset(data, 0);
450 } else {
451 spin_unlock_irqrestore(&data->lock, flags);
455 * Translate the framebuffer into the format needed by the PicoLCD.
456 * See display layout above.
457 * Do this one tile after the other and push those tiles that changed.
459 * Wait for our IO to complete as otherwise we might flood the queue!
461 n = 0;
462 for (chip = 0; chip < 4; chip++)
463 for (tile = 0; tile < 8; tile++)
464 if (picolcd_fb_update_tile(data->fb_vbitmap,
465 data->fb_bitmap, data->fb_bpp, chip, tile) ||
466 data->fb_force) {
467 n += 2;
468 if (!data->fb_info->par)
469 return; /* device lost! */
470 if (n >= HID_OUTPUT_FIFO_SIZE / 2) {
471 usbhid_wait_io(data->hdev);
472 n = 0;
474 picolcd_fb_send_tile(data->hdev, chip, tile);
476 data->fb_force = false;
477 if (n)
478 usbhid_wait_io(data->hdev);
481 /* Stub to call the system default and update the image on the picoLCD */
482 static void picolcd_fb_fillrect(struct fb_info *info,
483 const struct fb_fillrect *rect)
485 if (!info->par)
486 return;
487 sys_fillrect(info, rect);
489 schedule_delayed_work(&info->deferred_work, 0);
492 /* Stub to call the system default and update the image on the picoLCD */
493 static void picolcd_fb_copyarea(struct fb_info *info,
494 const struct fb_copyarea *area)
496 if (!info->par)
497 return;
498 sys_copyarea(info, area);
500 schedule_delayed_work(&info->deferred_work, 0);
503 /* Stub to call the system default and update the image on the picoLCD */
504 static void picolcd_fb_imageblit(struct fb_info *info, const struct fb_image *image)
506 if (!info->par)
507 return;
508 sys_imageblit(info, image);
510 schedule_delayed_work(&info->deferred_work, 0);
514 * this is the slow path from userspace. they can seek and write to
515 * the fb. it's inefficient to do anything less than a full screen draw
517 static ssize_t picolcd_fb_write(struct fb_info *info, const char __user *buf,
518 size_t count, loff_t *ppos)
520 ssize_t ret;
521 if (!info->par)
522 return -ENODEV;
523 ret = fb_sys_write(info, buf, count, ppos);
524 if (ret >= 0)
525 schedule_delayed_work(&info->deferred_work, 0);
526 return ret;
529 static int picolcd_fb_blank(int blank, struct fb_info *info)
531 if (!info->par)
532 return -ENODEV;
533 /* We let fb notification do this for us via lcd/backlight device */
534 return 0;
537 static void picolcd_fb_destroy(struct fb_info *info)
539 struct picolcd_data *data = info->par;
540 u32 *ref_cnt = info->pseudo_palette;
541 int may_release;
543 info->par = NULL;
544 if (data)
545 data->fb_info = NULL;
546 fb_deferred_io_cleanup(info);
548 ref_cnt--;
549 mutex_lock(&info->lock);
550 (*ref_cnt)--;
551 may_release = !*ref_cnt;
552 mutex_unlock(&info->lock);
553 if (may_release) {
554 vfree((u8 *)info->fix.smem_start);
555 framebuffer_release(info);
559 static int picolcd_fb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
561 __u32 bpp = var->bits_per_pixel;
562 __u32 activate = var->activate;
564 /* only allow 1/8 bit depth (8-bit is grayscale) */
565 *var = picolcdfb_var;
566 var->activate = activate;
567 if (bpp >= 8) {
568 var->bits_per_pixel = 8;
569 var->red.length = 8;
570 var->green.length = 8;
571 var->blue.length = 8;
572 } else {
573 var->bits_per_pixel = 1;
574 var->red.length = 1;
575 var->green.length = 1;
576 var->blue.length = 1;
578 return 0;
581 static int picolcd_set_par(struct fb_info *info)
583 struct picolcd_data *data = info->par;
584 u8 *tmp_fb, *o_fb;
585 if (!data)
586 return -ENODEV;
587 if (info->var.bits_per_pixel == data->fb_bpp)
588 return 0;
589 /* switch between 1/8 bit depths */
590 if (info->var.bits_per_pixel != 1 && info->var.bits_per_pixel != 8)
591 return -EINVAL;
593 o_fb = data->fb_bitmap;
594 tmp_fb = kmalloc(PICOLCDFB_SIZE*info->var.bits_per_pixel, GFP_KERNEL);
595 if (!tmp_fb)
596 return -ENOMEM;
598 /* translate FB content to new bits-per-pixel */
599 if (info->var.bits_per_pixel == 1) {
600 int i, b;
601 for (i = 0; i < PICOLCDFB_SIZE; i++) {
602 u8 p = 0;
603 for (b = 0; b < 8; b++) {
604 p <<= 1;
605 p |= o_fb[i*8+b] ? 0x01 : 0x00;
607 tmp_fb[i] = p;
609 memcpy(o_fb, tmp_fb, PICOLCDFB_SIZE);
610 info->fix.visual = FB_VISUAL_MONO01;
611 info->fix.line_length = PICOLCDFB_WIDTH / 8;
612 } else {
613 int i;
614 memcpy(tmp_fb, o_fb, PICOLCDFB_SIZE);
615 for (i = 0; i < PICOLCDFB_SIZE * 8; i++)
616 o_fb[i] = tmp_fb[i/8] & (0x01 << (7 - i % 8)) ? 0xff : 0x00;
617 info->fix.visual = FB_VISUAL_DIRECTCOLOR;
618 info->fix.line_length = PICOLCDFB_WIDTH;
621 kfree(tmp_fb);
622 data->fb_bpp = info->var.bits_per_pixel;
623 return 0;
626 /* Do refcounting on our FB and cleanup per worker if FB is
627 * closed after unplug of our device
628 * (fb_release holds info->lock and still touches info after
629 * we return so we can't release it immediately.
631 struct picolcd_fb_cleanup_item {
632 struct fb_info *info;
633 struct picolcd_fb_cleanup_item *next;
635 static struct picolcd_fb_cleanup_item *fb_pending;
636 DEFINE_SPINLOCK(fb_pending_lock);
638 static void picolcd_fb_do_cleanup(struct work_struct *data)
640 struct picolcd_fb_cleanup_item *item;
641 unsigned long flags;
643 do {
644 spin_lock_irqsave(&fb_pending_lock, flags);
645 item = fb_pending;
646 fb_pending = item ? item->next : NULL;
647 spin_unlock_irqrestore(&fb_pending_lock, flags);
649 if (item) {
650 u8 *fb = (u8 *)item->info->fix.smem_start;
651 /* make sure we do not race against fb core when
652 * releasing */
653 mutex_lock(&item->info->lock);
654 mutex_unlock(&item->info->lock);
655 framebuffer_release(item->info);
656 vfree(fb);
658 } while (item);
661 DECLARE_WORK(picolcd_fb_cleanup, picolcd_fb_do_cleanup);
663 static int picolcd_fb_open(struct fb_info *info, int u)
665 u32 *ref_cnt = info->pseudo_palette;
666 ref_cnt--;
668 (*ref_cnt)++;
669 return 0;
672 static int picolcd_fb_release(struct fb_info *info, int u)
674 u32 *ref_cnt = info->pseudo_palette;
675 ref_cnt--;
677 (*ref_cnt)++;
678 if (!*ref_cnt) {
679 unsigned long flags;
680 struct picolcd_fb_cleanup_item *item = (struct picolcd_fb_cleanup_item *)ref_cnt;
681 item--;
682 spin_lock_irqsave(&fb_pending_lock, flags);
683 item->next = fb_pending;
684 fb_pending = item;
685 spin_unlock_irqrestore(&fb_pending_lock, flags);
686 schedule_work(&picolcd_fb_cleanup);
688 return 0;
691 /* Note this can't be const because of struct fb_info definition */
692 static struct fb_ops picolcdfb_ops = {
693 .owner = THIS_MODULE,
694 .fb_destroy = picolcd_fb_destroy,
695 .fb_open = picolcd_fb_open,
696 .fb_release = picolcd_fb_release,
697 .fb_read = fb_sys_read,
698 .fb_write = picolcd_fb_write,
699 .fb_blank = picolcd_fb_blank,
700 .fb_fillrect = picolcd_fb_fillrect,
701 .fb_copyarea = picolcd_fb_copyarea,
702 .fb_imageblit = picolcd_fb_imageblit,
703 .fb_check_var = picolcd_fb_check_var,
704 .fb_set_par = picolcd_set_par,
708 /* Callback from deferred IO workqueue */
709 static void picolcd_fb_deferred_io(struct fb_info *info, struct list_head *pagelist)
711 picolcd_fb_update(info->par);
714 static const struct fb_deferred_io picolcd_fb_defio = {
715 .delay = HZ / PICOLCDFB_UPDATE_RATE_DEFAULT,
716 .deferred_io = picolcd_fb_deferred_io,
721 * The "fb_update_rate" sysfs attribute
723 static ssize_t picolcd_fb_update_rate_show(struct device *dev,
724 struct device_attribute *attr, char *buf)
726 struct picolcd_data *data = dev_get_drvdata(dev);
727 unsigned i, fb_update_rate = data->fb_update_rate;
728 size_t ret = 0;
730 for (i = 1; i <= PICOLCDFB_UPDATE_RATE_LIMIT; i++)
731 if (ret >= PAGE_SIZE)
732 break;
733 else if (i == fb_update_rate)
734 ret += snprintf(buf+ret, PAGE_SIZE-ret, "[%u] ", i);
735 else
736 ret += snprintf(buf+ret, PAGE_SIZE-ret, "%u ", i);
737 if (ret > 0)
738 buf[min(ret, (size_t)PAGE_SIZE)-1] = '\n';
739 return ret;
742 static ssize_t picolcd_fb_update_rate_store(struct device *dev,
743 struct device_attribute *attr, const char *buf, size_t count)
745 struct picolcd_data *data = dev_get_drvdata(dev);
746 int i;
747 unsigned u;
749 if (count < 1 || count > 10)
750 return -EINVAL;
752 i = sscanf(buf, "%u", &u);
753 if (i != 1)
754 return -EINVAL;
756 if (u > PICOLCDFB_UPDATE_RATE_LIMIT)
757 return -ERANGE;
758 else if (u == 0)
759 u = PICOLCDFB_UPDATE_RATE_DEFAULT;
761 data->fb_update_rate = u;
762 data->fb_defio.delay = HZ / data->fb_update_rate;
763 return count;
766 static DEVICE_ATTR(fb_update_rate, 0666, picolcd_fb_update_rate_show,
767 picolcd_fb_update_rate_store);
769 /* initialize Framebuffer device */
770 static int picolcd_init_framebuffer(struct picolcd_data *data)
772 struct device *dev = &data->hdev->dev;
773 struct fb_info *info = NULL;
774 int i, error = -ENOMEM;
775 u8 *fb_vbitmap = NULL;
776 u8 *fb_bitmap = NULL;
777 u32 *palette;
779 fb_bitmap = vmalloc(PICOLCDFB_SIZE*8);
780 if (fb_bitmap == NULL) {
781 dev_err(dev, "can't get a free page for framebuffer\n");
782 goto err_nomem;
785 fb_vbitmap = kmalloc(PICOLCDFB_SIZE, GFP_KERNEL);
786 if (fb_vbitmap == NULL) {
787 dev_err(dev, "can't alloc vbitmap image buffer\n");
788 goto err_nomem;
791 data->fb_update_rate = PICOLCDFB_UPDATE_RATE_DEFAULT;
792 data->fb_defio = picolcd_fb_defio;
793 /* The extra memory is:
794 * - struct picolcd_fb_cleanup_item
795 * - u32 for ref_count
796 * - 256*u32 for pseudo_palette
798 info = framebuffer_alloc(257 * sizeof(u32) + sizeof(struct picolcd_fb_cleanup_item), dev);
799 if (info == NULL) {
800 dev_err(dev, "failed to allocate a framebuffer\n");
801 goto err_nomem;
804 palette = info->par + sizeof(struct picolcd_fb_cleanup_item);
805 *palette = 1;
806 palette++;
807 for (i = 0; i < 256; i++)
808 palette[i] = i > 0 && i < 16 ? 0xff : 0;
809 info->pseudo_palette = palette;
810 info->fbdefio = &data->fb_defio;
811 info->screen_base = (char __force __iomem *)fb_bitmap;
812 info->fbops = &picolcdfb_ops;
813 info->var = picolcdfb_var;
814 info->fix = picolcdfb_fix;
815 info->fix.smem_len = PICOLCDFB_SIZE*8;
816 info->fix.smem_start = (unsigned long)fb_bitmap;
817 info->par = data;
818 info->flags = FBINFO_FLAG_DEFAULT;
820 data->fb_vbitmap = fb_vbitmap;
821 data->fb_bitmap = fb_bitmap;
822 data->fb_bpp = picolcdfb_var.bits_per_pixel;
823 error = picolcd_fb_reset(data, 1);
824 if (error) {
825 dev_err(dev, "failed to configure display\n");
826 goto err_cleanup;
828 error = device_create_file(dev, &dev_attr_fb_update_rate);
829 if (error) {
830 dev_err(dev, "failed to create sysfs attributes\n");
831 goto err_cleanup;
833 fb_deferred_io_init(info);
834 data->fb_info = info;
835 error = register_framebuffer(info);
836 if (error) {
837 dev_err(dev, "failed to register framebuffer\n");
838 goto err_sysfs;
840 /* schedule first output of framebuffer */
841 data->fb_force = 1;
842 schedule_delayed_work(&info->deferred_work, 0);
843 return 0;
845 err_sysfs:
846 fb_deferred_io_cleanup(info);
847 device_remove_file(dev, &dev_attr_fb_update_rate);
848 err_cleanup:
849 data->fb_vbitmap = NULL;
850 data->fb_bitmap = NULL;
851 data->fb_bpp = 0;
852 data->fb_info = NULL;
854 err_nomem:
855 framebuffer_release(info);
856 vfree(fb_bitmap);
857 kfree(fb_vbitmap);
858 return error;
861 static void picolcd_exit_framebuffer(struct picolcd_data *data)
863 struct fb_info *info = data->fb_info;
864 u8 *fb_vbitmap = data->fb_vbitmap;
866 if (!info)
867 return;
869 info->par = NULL;
870 device_remove_file(&data->hdev->dev, &dev_attr_fb_update_rate);
871 unregister_framebuffer(info);
872 data->fb_vbitmap = NULL;
873 data->fb_bitmap = NULL;
874 data->fb_bpp = 0;
875 data->fb_info = NULL;
876 kfree(fb_vbitmap);
879 #define picolcd_fbinfo(d) ((d)->fb_info)
880 #else
881 static inline int picolcd_fb_reset(struct picolcd_data *data, int clear)
883 return 0;
885 static inline int picolcd_init_framebuffer(struct picolcd_data *data)
887 return 0;
889 static inline void picolcd_exit_framebuffer(struct picolcd_data *data)
892 #define picolcd_fbinfo(d) NULL
893 #endif /* CONFIG_HID_PICOLCD_FB */
895 #ifdef CONFIG_HID_PICOLCD_BACKLIGHT
897 * backlight class device
899 static int picolcd_get_brightness(struct backlight_device *bdev)
901 struct picolcd_data *data = bl_get_data(bdev);
902 return data->lcd_brightness;
905 static int picolcd_set_brightness(struct backlight_device *bdev)
907 struct picolcd_data *data = bl_get_data(bdev);
908 struct hid_report *report = picolcd_out_report(REPORT_BRIGHTNESS, data->hdev);
909 unsigned long flags;
911 if (!report || report->maxfield != 1 || report->field[0]->report_count != 1)
912 return -ENODEV;
914 data->lcd_brightness = bdev->props.brightness & 0x0ff;
915 data->lcd_power = bdev->props.power;
916 spin_lock_irqsave(&data->lock, flags);
917 hid_set_field(report->field[0], 0, data->lcd_power == FB_BLANK_UNBLANK ? data->lcd_brightness : 0);
918 usbhid_submit_report(data->hdev, report, USB_DIR_OUT);
919 spin_unlock_irqrestore(&data->lock, flags);
920 return 0;
923 static int picolcd_check_bl_fb(struct backlight_device *bdev, struct fb_info *fb)
925 return fb && fb == picolcd_fbinfo((struct picolcd_data *)bl_get_data(bdev));
928 static const struct backlight_ops picolcd_blops = {
929 .update_status = picolcd_set_brightness,
930 .get_brightness = picolcd_get_brightness,
931 .check_fb = picolcd_check_bl_fb,
934 static int picolcd_init_backlight(struct picolcd_data *data, struct hid_report *report)
936 struct device *dev = &data->hdev->dev;
937 struct backlight_device *bdev;
938 struct backlight_properties props;
939 if (!report)
940 return -ENODEV;
941 if (report->maxfield != 1 || report->field[0]->report_count != 1 ||
942 report->field[0]->report_size != 8) {
943 dev_err(dev, "unsupported BRIGHTNESS report");
944 return -EINVAL;
947 memset(&props, 0, sizeof(props));
948 props.type = BACKLIGHT_RAW;
949 props.max_brightness = 0xff;
950 bdev = backlight_device_register(dev_name(dev), dev, data,
951 &picolcd_blops, &props);
952 if (IS_ERR(bdev)) {
953 dev_err(dev, "failed to register backlight\n");
954 return PTR_ERR(bdev);
956 bdev->props.brightness = 0xff;
957 data->lcd_brightness = 0xff;
958 data->backlight = bdev;
959 picolcd_set_brightness(bdev);
960 return 0;
963 static void picolcd_exit_backlight(struct picolcd_data *data)
965 struct backlight_device *bdev = data->backlight;
967 data->backlight = NULL;
968 if (bdev)
969 backlight_device_unregister(bdev);
972 static inline int picolcd_resume_backlight(struct picolcd_data *data)
974 if (!data->backlight)
975 return 0;
976 return picolcd_set_brightness(data->backlight);
979 #ifdef CONFIG_PM
980 static void picolcd_suspend_backlight(struct picolcd_data *data)
982 int bl_power = data->lcd_power;
983 if (!data->backlight)
984 return;
986 data->backlight->props.power = FB_BLANK_POWERDOWN;
987 picolcd_set_brightness(data->backlight);
988 data->lcd_power = data->backlight->props.power = bl_power;
990 #endif /* CONFIG_PM */
991 #else
992 static inline int picolcd_init_backlight(struct picolcd_data *data,
993 struct hid_report *report)
995 return 0;
997 static inline void picolcd_exit_backlight(struct picolcd_data *data)
1000 static inline int picolcd_resume_backlight(struct picolcd_data *data)
1002 return 0;
1004 static inline void picolcd_suspend_backlight(struct picolcd_data *data)
1007 #endif /* CONFIG_HID_PICOLCD_BACKLIGHT */
1009 #ifdef CONFIG_HID_PICOLCD_LCD
1011 * lcd class device
1013 static int picolcd_get_contrast(struct lcd_device *ldev)
1015 struct picolcd_data *data = lcd_get_data(ldev);
1016 return data->lcd_contrast;
1019 static int picolcd_set_contrast(struct lcd_device *ldev, int contrast)
1021 struct picolcd_data *data = lcd_get_data(ldev);
1022 struct hid_report *report = picolcd_out_report(REPORT_CONTRAST, data->hdev);
1023 unsigned long flags;
1025 if (!report || report->maxfield != 1 || report->field[0]->report_count != 1)
1026 return -ENODEV;
1028 data->lcd_contrast = contrast & 0x0ff;
1029 spin_lock_irqsave(&data->lock, flags);
1030 hid_set_field(report->field[0], 0, data->lcd_contrast);
1031 usbhid_submit_report(data->hdev, report, USB_DIR_OUT);
1032 spin_unlock_irqrestore(&data->lock, flags);
1033 return 0;
1036 static int picolcd_check_lcd_fb(struct lcd_device *ldev, struct fb_info *fb)
1038 return fb && fb == picolcd_fbinfo((struct picolcd_data *)lcd_get_data(ldev));
1041 static struct lcd_ops picolcd_lcdops = {
1042 .get_contrast = picolcd_get_contrast,
1043 .set_contrast = picolcd_set_contrast,
1044 .check_fb = picolcd_check_lcd_fb,
1047 static int picolcd_init_lcd(struct picolcd_data *data, struct hid_report *report)
1049 struct device *dev = &data->hdev->dev;
1050 struct lcd_device *ldev;
1052 if (!report)
1053 return -ENODEV;
1054 if (report->maxfield != 1 || report->field[0]->report_count != 1 ||
1055 report->field[0]->report_size != 8) {
1056 dev_err(dev, "unsupported CONTRAST report");
1057 return -EINVAL;
1060 ldev = lcd_device_register(dev_name(dev), dev, data, &picolcd_lcdops);
1061 if (IS_ERR(ldev)) {
1062 dev_err(dev, "failed to register LCD\n");
1063 return PTR_ERR(ldev);
1065 ldev->props.max_contrast = 0x0ff;
1066 data->lcd_contrast = 0xe5;
1067 data->lcd = ldev;
1068 picolcd_set_contrast(ldev, 0xe5);
1069 return 0;
1072 static void picolcd_exit_lcd(struct picolcd_data *data)
1074 struct lcd_device *ldev = data->lcd;
1076 data->lcd = NULL;
1077 if (ldev)
1078 lcd_device_unregister(ldev);
1081 static inline int picolcd_resume_lcd(struct picolcd_data *data)
1083 if (!data->lcd)
1084 return 0;
1085 return picolcd_set_contrast(data->lcd, data->lcd_contrast);
1087 #else
1088 static inline int picolcd_init_lcd(struct picolcd_data *data,
1089 struct hid_report *report)
1091 return 0;
1093 static inline void picolcd_exit_lcd(struct picolcd_data *data)
1096 static inline int picolcd_resume_lcd(struct picolcd_data *data)
1098 return 0;
1100 #endif /* CONFIG_HID_PICOLCD_LCD */
1102 #ifdef CONFIG_HID_PICOLCD_LEDS
1104 * LED class device
1106 static void picolcd_leds_set(struct picolcd_data *data)
1108 struct hid_report *report;
1109 unsigned long flags;
1111 if (!data->led[0])
1112 return;
1113 report = picolcd_out_report(REPORT_LED_STATE, data->hdev);
1114 if (!report || report->maxfield != 1 || report->field[0]->report_count != 1)
1115 return;
1117 spin_lock_irqsave(&data->lock, flags);
1118 hid_set_field(report->field[0], 0, data->led_state);
1119 usbhid_submit_report(data->hdev, report, USB_DIR_OUT);
1120 spin_unlock_irqrestore(&data->lock, flags);
1123 static void picolcd_led_set_brightness(struct led_classdev *led_cdev,
1124 enum led_brightness value)
1126 struct device *dev;
1127 struct hid_device *hdev;
1128 struct picolcd_data *data;
1129 int i, state = 0;
1131 dev = led_cdev->dev->parent;
1132 hdev = container_of(dev, struct hid_device, dev);
1133 data = hid_get_drvdata(hdev);
1134 for (i = 0; i < 8; i++) {
1135 if (led_cdev != data->led[i])
1136 continue;
1137 state = (data->led_state >> i) & 1;
1138 if (value == LED_OFF && state) {
1139 data->led_state &= ~(1 << i);
1140 picolcd_leds_set(data);
1141 } else if (value != LED_OFF && !state) {
1142 data->led_state |= 1 << i;
1143 picolcd_leds_set(data);
1145 break;
1149 static enum led_brightness picolcd_led_get_brightness(struct led_classdev *led_cdev)
1151 struct device *dev;
1152 struct hid_device *hdev;
1153 struct picolcd_data *data;
1154 int i, value = 0;
1156 dev = led_cdev->dev->parent;
1157 hdev = container_of(dev, struct hid_device, dev);
1158 data = hid_get_drvdata(hdev);
1159 for (i = 0; i < 8; i++)
1160 if (led_cdev == data->led[i]) {
1161 value = (data->led_state >> i) & 1;
1162 break;
1164 return value ? LED_FULL : LED_OFF;
1167 static int picolcd_init_leds(struct picolcd_data *data, struct hid_report *report)
1169 struct device *dev = &data->hdev->dev;
1170 struct led_classdev *led;
1171 size_t name_sz = strlen(dev_name(dev)) + 8;
1172 char *name;
1173 int i, ret = 0;
1175 if (!report)
1176 return -ENODEV;
1177 if (report->maxfield != 1 || report->field[0]->report_count != 1 ||
1178 report->field[0]->report_size != 8) {
1179 dev_err(dev, "unsupported LED_STATE report");
1180 return -EINVAL;
1183 for (i = 0; i < 8; i++) {
1184 led = kzalloc(sizeof(struct led_classdev)+name_sz, GFP_KERNEL);
1185 if (!led) {
1186 dev_err(dev, "can't allocate memory for LED %d\n", i);
1187 ret = -ENOMEM;
1188 goto err;
1190 name = (void *)(&led[1]);
1191 snprintf(name, name_sz, "%s::GPO%d", dev_name(dev), i);
1192 led->name = name;
1193 led->brightness = 0;
1194 led->max_brightness = 1;
1195 led->brightness_get = picolcd_led_get_brightness;
1196 led->brightness_set = picolcd_led_set_brightness;
1198 data->led[i] = led;
1199 ret = led_classdev_register(dev, data->led[i]);
1200 if (ret) {
1201 data->led[i] = NULL;
1202 kfree(led);
1203 dev_err(dev, "can't register LED %d\n", i);
1204 goto err;
1207 return 0;
1208 err:
1209 for (i = 0; i < 8; i++)
1210 if (data->led[i]) {
1211 led = data->led[i];
1212 data->led[i] = NULL;
1213 led_classdev_unregister(led);
1214 kfree(led);
1216 return ret;
1219 static void picolcd_exit_leds(struct picolcd_data *data)
1221 struct led_classdev *led;
1222 int i;
1224 for (i = 0; i < 8; i++) {
1225 led = data->led[i];
1226 data->led[i] = NULL;
1227 if (!led)
1228 continue;
1229 led_classdev_unregister(led);
1230 kfree(led);
1234 #else
1235 static inline int picolcd_init_leds(struct picolcd_data *data,
1236 struct hid_report *report)
1238 return 0;
1240 static inline void picolcd_exit_leds(struct picolcd_data *data)
1243 static inline int picolcd_leds_set(struct picolcd_data *data)
1245 return 0;
1247 #endif /* CONFIG_HID_PICOLCD_LEDS */
1250 * input class device
1252 static int picolcd_raw_keypad(struct picolcd_data *data,
1253 struct hid_report *report, u8 *raw_data, int size)
1256 * Keypad event
1257 * First and second data bytes list currently pressed keys,
1258 * 0x00 means no key and at most 2 keys may be pressed at same time
1260 int i, j;
1262 /* determine newly pressed keys */
1263 for (i = 0; i < size; i++) {
1264 unsigned int key_code;
1265 if (raw_data[i] == 0)
1266 continue;
1267 for (j = 0; j < sizeof(data->pressed_keys); j++)
1268 if (data->pressed_keys[j] == raw_data[i])
1269 goto key_already_down;
1270 for (j = 0; j < sizeof(data->pressed_keys); j++)
1271 if (data->pressed_keys[j] == 0) {
1272 data->pressed_keys[j] = raw_data[i];
1273 break;
1275 input_event(data->input_keys, EV_MSC, MSC_SCAN, raw_data[i]);
1276 if (raw_data[i] < PICOLCD_KEYS)
1277 key_code = data->keycode[raw_data[i]];
1278 else
1279 key_code = KEY_UNKNOWN;
1280 if (key_code != KEY_UNKNOWN) {
1281 dbg_hid(PICOLCD_NAME " got key press for %u:%d",
1282 raw_data[i], key_code);
1283 input_report_key(data->input_keys, key_code, 1);
1285 input_sync(data->input_keys);
1286 key_already_down:
1287 continue;
1290 /* determine newly released keys */
1291 for (j = 0; j < sizeof(data->pressed_keys); j++) {
1292 unsigned int key_code;
1293 if (data->pressed_keys[j] == 0)
1294 continue;
1295 for (i = 0; i < size; i++)
1296 if (data->pressed_keys[j] == raw_data[i])
1297 goto key_still_down;
1298 input_event(data->input_keys, EV_MSC, MSC_SCAN, data->pressed_keys[j]);
1299 if (data->pressed_keys[j] < PICOLCD_KEYS)
1300 key_code = data->keycode[data->pressed_keys[j]];
1301 else
1302 key_code = KEY_UNKNOWN;
1303 if (key_code != KEY_UNKNOWN) {
1304 dbg_hid(PICOLCD_NAME " got key release for %u:%d",
1305 data->pressed_keys[j], key_code);
1306 input_report_key(data->input_keys, key_code, 0);
1308 input_sync(data->input_keys);
1309 data->pressed_keys[j] = 0;
1310 key_still_down:
1311 continue;
1313 return 1;
1316 static int picolcd_raw_cir(struct picolcd_data *data,
1317 struct hid_report *report, u8 *raw_data, int size)
1319 /* Need understanding of CIR data format to implement ... */
1320 return 1;
1323 static int picolcd_check_version(struct hid_device *hdev)
1325 struct picolcd_data *data = hid_get_drvdata(hdev);
1326 struct picolcd_pending *verinfo;
1327 int ret = 0;
1329 if (!data)
1330 return -ENODEV;
1332 verinfo = picolcd_send_and_wait(hdev, REPORT_VERSION, NULL, 0);
1333 if (!verinfo) {
1334 hid_err(hdev, "no version response from PicoLCD\n");
1335 return -ENODEV;
1338 if (verinfo->raw_size == 2) {
1339 data->version[0] = verinfo->raw_data[1];
1340 data->version[1] = verinfo->raw_data[0];
1341 if (data->status & PICOLCD_BOOTLOADER) {
1342 hid_info(hdev, "PicoLCD, bootloader version %d.%d\n",
1343 verinfo->raw_data[1], verinfo->raw_data[0]);
1344 } else {
1345 hid_info(hdev, "PicoLCD, firmware version %d.%d\n",
1346 verinfo->raw_data[1], verinfo->raw_data[0]);
1348 } else {
1349 hid_err(hdev, "confused, got unexpected version response from PicoLCD\n");
1350 ret = -EINVAL;
1352 kfree(verinfo);
1353 return ret;
1357 * Reset our device and wait for answer to VERSION request
1359 static int picolcd_reset(struct hid_device *hdev)
1361 struct picolcd_data *data = hid_get_drvdata(hdev);
1362 struct hid_report *report = picolcd_out_report(REPORT_RESET, hdev);
1363 unsigned long flags;
1364 int error;
1366 if (!data || !report || report->maxfield != 1)
1367 return -ENODEV;
1369 spin_lock_irqsave(&data->lock, flags);
1370 if (hdev->product == USB_DEVICE_ID_PICOLCD_BOOTLOADER)
1371 data->status |= PICOLCD_BOOTLOADER;
1373 /* perform the reset */
1374 hid_set_field(report->field[0], 0, 1);
1375 usbhid_submit_report(hdev, report, USB_DIR_OUT);
1376 spin_unlock_irqrestore(&data->lock, flags);
1378 error = picolcd_check_version(hdev);
1379 if (error)
1380 return error;
1382 picolcd_resume_lcd(data);
1383 picolcd_resume_backlight(data);
1384 #ifdef CONFIG_HID_PICOLCD_FB
1385 if (data->fb_info)
1386 schedule_delayed_work(&data->fb_info->deferred_work, 0);
1387 #endif /* CONFIG_HID_PICOLCD_FB */
1389 picolcd_leds_set(data);
1390 return 0;
1394 * The "operation_mode" sysfs attribute
1396 static ssize_t picolcd_operation_mode_show(struct device *dev,
1397 struct device_attribute *attr, char *buf)
1399 struct picolcd_data *data = dev_get_drvdata(dev);
1401 if (data->status & PICOLCD_BOOTLOADER)
1402 return snprintf(buf, PAGE_SIZE, "[bootloader] lcd\n");
1403 else
1404 return snprintf(buf, PAGE_SIZE, "bootloader [lcd]\n");
1407 static ssize_t picolcd_operation_mode_store(struct device *dev,
1408 struct device_attribute *attr, const char *buf, size_t count)
1410 struct picolcd_data *data = dev_get_drvdata(dev);
1411 struct hid_report *report = NULL;
1412 size_t cnt = count;
1413 int timeout = data->opmode_delay;
1414 unsigned long flags;
1416 if (cnt >= 3 && strncmp("lcd", buf, 3) == 0) {
1417 if (data->status & PICOLCD_BOOTLOADER)
1418 report = picolcd_out_report(REPORT_EXIT_FLASHER, data->hdev);
1419 buf += 3;
1420 cnt -= 3;
1421 } else if (cnt >= 10 && strncmp("bootloader", buf, 10) == 0) {
1422 if (!(data->status & PICOLCD_BOOTLOADER))
1423 report = picolcd_out_report(REPORT_EXIT_KEYBOARD, data->hdev);
1424 buf += 10;
1425 cnt -= 10;
1427 if (!report)
1428 return -EINVAL;
1430 while (cnt > 0 && (buf[cnt-1] == '\n' || buf[cnt-1] == '\r'))
1431 cnt--;
1432 if (cnt != 0)
1433 return -EINVAL;
1435 spin_lock_irqsave(&data->lock, flags);
1436 hid_set_field(report->field[0], 0, timeout & 0xff);
1437 hid_set_field(report->field[0], 1, (timeout >> 8) & 0xff);
1438 usbhid_submit_report(data->hdev, report, USB_DIR_OUT);
1439 spin_unlock_irqrestore(&data->lock, flags);
1440 return count;
1443 static DEVICE_ATTR(operation_mode, 0644, picolcd_operation_mode_show,
1444 picolcd_operation_mode_store);
1447 * The "operation_mode_delay" sysfs attribute
1449 static ssize_t picolcd_operation_mode_delay_show(struct device *dev,
1450 struct device_attribute *attr, char *buf)
1452 struct picolcd_data *data = dev_get_drvdata(dev);
1454 return snprintf(buf, PAGE_SIZE, "%hu\n", data->opmode_delay);
1457 static ssize_t picolcd_operation_mode_delay_store(struct device *dev,
1458 struct device_attribute *attr, const char *buf, size_t count)
1460 struct picolcd_data *data = dev_get_drvdata(dev);
1461 unsigned u;
1462 if (sscanf(buf, "%u", &u) != 1)
1463 return -EINVAL;
1464 if (u > 30000)
1465 return -EINVAL;
1466 else
1467 data->opmode_delay = u;
1468 return count;
1471 static DEVICE_ATTR(operation_mode_delay, 0644, picolcd_operation_mode_delay_show,
1472 picolcd_operation_mode_delay_store);
1475 #ifdef CONFIG_DEBUG_FS
1477 * The "reset" file
1479 static int picolcd_debug_reset_show(struct seq_file *f, void *p)
1481 if (picolcd_fbinfo((struct picolcd_data *)f->private))
1482 seq_printf(f, "all fb\n");
1483 else
1484 seq_printf(f, "all\n");
1485 return 0;
1488 static int picolcd_debug_reset_open(struct inode *inode, struct file *f)
1490 return single_open(f, picolcd_debug_reset_show, inode->i_private);
1493 static ssize_t picolcd_debug_reset_write(struct file *f, const char __user *user_buf,
1494 size_t count, loff_t *ppos)
1496 struct picolcd_data *data = ((struct seq_file *)f->private_data)->private;
1497 char buf[32];
1498 size_t cnt = min(count, sizeof(buf)-1);
1499 if (copy_from_user(buf, user_buf, cnt))
1500 return -EFAULT;
1502 while (cnt > 0 && (buf[cnt-1] == ' ' || buf[cnt-1] == '\n'))
1503 cnt--;
1504 buf[cnt] = '\0';
1505 if (strcmp(buf, "all") == 0) {
1506 picolcd_reset(data->hdev);
1507 picolcd_fb_reset(data, 1);
1508 } else if (strcmp(buf, "fb") == 0) {
1509 picolcd_fb_reset(data, 1);
1510 } else {
1511 return -EINVAL;
1513 return count;
1516 static const struct file_operations picolcd_debug_reset_fops = {
1517 .owner = THIS_MODULE,
1518 .open = picolcd_debug_reset_open,
1519 .read = seq_read,
1520 .llseek = seq_lseek,
1521 .write = picolcd_debug_reset_write,
1522 .release = single_release,
1526 * The "eeprom" file
1528 static int picolcd_debug_eeprom_open(struct inode *i, struct file *f)
1530 f->private_data = i->i_private;
1531 return 0;
1534 static ssize_t picolcd_debug_eeprom_read(struct file *f, char __user *u,
1535 size_t s, loff_t *off)
1537 struct picolcd_data *data = f->private_data;
1538 struct picolcd_pending *resp;
1539 u8 raw_data[3];
1540 ssize_t ret = -EIO;
1542 if (s == 0)
1543 return -EINVAL;
1544 if (*off > 0x0ff)
1545 return 0;
1547 /* prepare buffer with info about what we want to read (addr & len) */
1548 raw_data[0] = *off & 0xff;
1549 raw_data[1] = (*off >> 8) & 0xff;
1550 raw_data[2] = s < 20 ? s : 20;
1551 if (*off + raw_data[2] > 0xff)
1552 raw_data[2] = 0x100 - *off;
1553 resp = picolcd_send_and_wait(data->hdev, REPORT_EE_READ, raw_data,
1554 sizeof(raw_data));
1555 if (!resp)
1556 return -EIO;
1558 if (resp->in_report && resp->in_report->id == REPORT_EE_DATA) {
1559 /* successful read :) */
1560 ret = resp->raw_data[2];
1561 if (ret > s)
1562 ret = s;
1563 if (copy_to_user(u, resp->raw_data+3, ret))
1564 ret = -EFAULT;
1565 else
1566 *off += ret;
1567 } /* anything else is some kind of IO error */
1569 kfree(resp);
1570 return ret;
1573 static ssize_t picolcd_debug_eeprom_write(struct file *f, const char __user *u,
1574 size_t s, loff_t *off)
1576 struct picolcd_data *data = f->private_data;
1577 struct picolcd_pending *resp;
1578 ssize_t ret = -EIO;
1579 u8 raw_data[23];
1581 if (s == 0)
1582 return -EINVAL;
1583 if (*off > 0x0ff)
1584 return -ENOSPC;
1586 memset(raw_data, 0, sizeof(raw_data));
1587 raw_data[0] = *off & 0xff;
1588 raw_data[1] = (*off >> 8) & 0xff;
1589 raw_data[2] = min((size_t)20, s);
1590 if (*off + raw_data[2] > 0xff)
1591 raw_data[2] = 0x100 - *off;
1593 if (copy_from_user(raw_data+3, u, min((u8)20, raw_data[2])))
1594 return -EFAULT;
1595 resp = picolcd_send_and_wait(data->hdev, REPORT_EE_WRITE, raw_data,
1596 sizeof(raw_data));
1598 if (!resp)
1599 return -EIO;
1601 if (resp->in_report && resp->in_report->id == REPORT_EE_DATA) {
1602 /* check if written data matches */
1603 if (memcmp(raw_data, resp->raw_data, 3+raw_data[2]) == 0) {
1604 *off += raw_data[2];
1605 ret = raw_data[2];
1608 kfree(resp);
1609 return ret;
1613 * Notes:
1614 * - read/write happens in chunks of at most 20 bytes, it's up to userspace
1615 * to loop in order to get more data.
1616 * - on write errors on otherwise correct write request the bytes
1617 * that should have been written are in undefined state.
1619 static const struct file_operations picolcd_debug_eeprom_fops = {
1620 .owner = THIS_MODULE,
1621 .open = picolcd_debug_eeprom_open,
1622 .read = picolcd_debug_eeprom_read,
1623 .write = picolcd_debug_eeprom_write,
1624 .llseek = generic_file_llseek,
1628 * The "flash" file
1630 static int picolcd_debug_flash_open(struct inode *i, struct file *f)
1632 f->private_data = i->i_private;
1633 return 0;
1636 /* record a flash address to buf (bounds check to be done by caller) */
1637 static int _picolcd_flash_setaddr(struct picolcd_data *data, u8 *buf, long off)
1639 buf[0] = off & 0xff;
1640 buf[1] = (off >> 8) & 0xff;
1641 if (data->addr_sz == 3)
1642 buf[2] = (off >> 16) & 0xff;
1643 return data->addr_sz == 2 ? 2 : 3;
1646 /* read a given size of data (bounds check to be done by caller) */
1647 static ssize_t _picolcd_flash_read(struct picolcd_data *data, int report_id,
1648 char __user *u, size_t s, loff_t *off)
1650 struct picolcd_pending *resp;
1651 u8 raw_data[4];
1652 ssize_t ret = 0;
1653 int len_off, err = -EIO;
1655 while (s > 0) {
1656 err = -EIO;
1657 len_off = _picolcd_flash_setaddr(data, raw_data, *off);
1658 raw_data[len_off] = s > 32 ? 32 : s;
1659 resp = picolcd_send_and_wait(data->hdev, report_id, raw_data, len_off+1);
1660 if (!resp || !resp->in_report)
1661 goto skip;
1662 if (resp->in_report->id == REPORT_MEMORY ||
1663 resp->in_report->id == REPORT_BL_READ_MEMORY) {
1664 if (memcmp(raw_data, resp->raw_data, len_off+1) != 0)
1665 goto skip;
1666 if (copy_to_user(u+ret, resp->raw_data+len_off+1, raw_data[len_off])) {
1667 err = -EFAULT;
1668 goto skip;
1670 *off += raw_data[len_off];
1671 s -= raw_data[len_off];
1672 ret += raw_data[len_off];
1673 err = 0;
1675 skip:
1676 kfree(resp);
1677 if (err)
1678 return ret > 0 ? ret : err;
1680 return ret;
1683 static ssize_t picolcd_debug_flash_read(struct file *f, char __user *u,
1684 size_t s, loff_t *off)
1686 struct picolcd_data *data = f->private_data;
1688 if (s == 0)
1689 return -EINVAL;
1690 if (*off > 0x05fff)
1691 return 0;
1692 if (*off + s > 0x05fff)
1693 s = 0x06000 - *off;
1695 if (data->status & PICOLCD_BOOTLOADER)
1696 return _picolcd_flash_read(data, REPORT_BL_READ_MEMORY, u, s, off);
1697 else
1698 return _picolcd_flash_read(data, REPORT_READ_MEMORY, u, s, off);
1701 /* erase block aligned to 64bytes boundary */
1702 static ssize_t _picolcd_flash_erase64(struct picolcd_data *data, int report_id,
1703 loff_t *off)
1705 struct picolcd_pending *resp;
1706 u8 raw_data[3];
1707 int len_off;
1708 ssize_t ret = -EIO;
1710 if (*off & 0x3f)
1711 return -EINVAL;
1713 len_off = _picolcd_flash_setaddr(data, raw_data, *off);
1714 resp = picolcd_send_and_wait(data->hdev, report_id, raw_data, len_off);
1715 if (!resp || !resp->in_report)
1716 goto skip;
1717 if (resp->in_report->id == REPORT_MEMORY ||
1718 resp->in_report->id == REPORT_BL_ERASE_MEMORY) {
1719 if (memcmp(raw_data, resp->raw_data, len_off) != 0)
1720 goto skip;
1721 ret = 0;
1723 skip:
1724 kfree(resp);
1725 return ret;
1728 /* write a given size of data (bounds check to be done by caller) */
1729 static ssize_t _picolcd_flash_write(struct picolcd_data *data, int report_id,
1730 const char __user *u, size_t s, loff_t *off)
1732 struct picolcd_pending *resp;
1733 u8 raw_data[36];
1734 ssize_t ret = 0;
1735 int len_off, err = -EIO;
1737 while (s > 0) {
1738 err = -EIO;
1739 len_off = _picolcd_flash_setaddr(data, raw_data, *off);
1740 raw_data[len_off] = s > 32 ? 32 : s;
1741 if (copy_from_user(raw_data+len_off+1, u, raw_data[len_off])) {
1742 err = -EFAULT;
1743 break;
1745 resp = picolcd_send_and_wait(data->hdev, report_id, raw_data,
1746 len_off+1+raw_data[len_off]);
1747 if (!resp || !resp->in_report)
1748 goto skip;
1749 if (resp->in_report->id == REPORT_MEMORY ||
1750 resp->in_report->id == REPORT_BL_WRITE_MEMORY) {
1751 if (memcmp(raw_data, resp->raw_data, len_off+1+raw_data[len_off]) != 0)
1752 goto skip;
1753 *off += raw_data[len_off];
1754 s -= raw_data[len_off];
1755 ret += raw_data[len_off];
1756 err = 0;
1758 skip:
1759 kfree(resp);
1760 if (err)
1761 break;
1763 return ret > 0 ? ret : err;
1766 static ssize_t picolcd_debug_flash_write(struct file *f, const char __user *u,
1767 size_t s, loff_t *off)
1769 struct picolcd_data *data = f->private_data;
1770 ssize_t err, ret = 0;
1771 int report_erase, report_write;
1773 if (s == 0)
1774 return -EINVAL;
1775 if (*off > 0x5fff)
1776 return -ENOSPC;
1777 if (s & 0x3f)
1778 return -EINVAL;
1779 if (*off & 0x3f)
1780 return -EINVAL;
1782 if (data->status & PICOLCD_BOOTLOADER) {
1783 report_erase = REPORT_BL_ERASE_MEMORY;
1784 report_write = REPORT_BL_WRITE_MEMORY;
1785 } else {
1786 report_erase = REPORT_ERASE_MEMORY;
1787 report_write = REPORT_WRITE_MEMORY;
1789 mutex_lock(&data->mutex_flash);
1790 while (s > 0) {
1791 err = _picolcd_flash_erase64(data, report_erase, off);
1792 if (err)
1793 break;
1794 err = _picolcd_flash_write(data, report_write, u, 64, off);
1795 if (err < 0)
1796 break;
1797 ret += err;
1798 *off += err;
1799 s -= err;
1800 if (err != 64)
1801 break;
1803 mutex_unlock(&data->mutex_flash);
1804 return ret > 0 ? ret : err;
1808 * Notes:
1809 * - concurrent writing is prevented by mutex and all writes must be
1810 * n*64 bytes and 64-byte aligned, each write being preceded by an
1811 * ERASE which erases a 64byte block.
1812 * If less than requested was written or an error is returned for an
1813 * otherwise correct write request the next 64-byte block which should
1814 * have been written is in undefined state (mostly: original, erased,
1815 * (half-)written with write error)
1816 * - reading can happen without special restriction
1818 static const struct file_operations picolcd_debug_flash_fops = {
1819 .owner = THIS_MODULE,
1820 .open = picolcd_debug_flash_open,
1821 .read = picolcd_debug_flash_read,
1822 .write = picolcd_debug_flash_write,
1823 .llseek = generic_file_llseek,
1828 * Helper code for HID report level dumping/debugging
1830 static const char *error_codes[] = {
1831 "success", "parameter missing", "data_missing", "block readonly",
1832 "block not erasable", "block too big", "section overflow",
1833 "invalid command length", "invalid data length",
1836 static void dump_buff_as_hex(char *dst, size_t dst_sz, const u8 *data,
1837 const size_t data_len)
1839 int i, j;
1840 for (i = j = 0; i < data_len && j + 3 < dst_sz; i++) {
1841 dst[j++] = hex_asc[(data[i] >> 4) & 0x0f];
1842 dst[j++] = hex_asc[data[i] & 0x0f];
1843 dst[j++] = ' ';
1845 if (j < dst_sz) {
1846 dst[j--] = '\0';
1847 dst[j] = '\n';
1848 } else
1849 dst[j] = '\0';
1852 static void picolcd_debug_out_report(struct picolcd_data *data,
1853 struct hid_device *hdev, struct hid_report *report)
1855 u8 raw_data[70];
1856 int raw_size = (report->size >> 3) + 1;
1857 char *buff;
1858 #define BUFF_SZ 256
1860 /* Avoid unnecessary overhead if debugfs is disabled */
1861 if (!hdev->debug_events)
1862 return;
1864 buff = kmalloc(BUFF_SZ, GFP_ATOMIC);
1865 if (!buff)
1866 return;
1868 snprintf(buff, BUFF_SZ, "\nout report %d (size %d) = ",
1869 report->id, raw_size);
1870 hid_debug_event(hdev, buff);
1871 if (raw_size + 5 > sizeof(raw_data)) {
1872 kfree(buff);
1873 hid_debug_event(hdev, " TOO BIG\n");
1874 return;
1875 } else {
1876 raw_data[0] = report->id;
1877 hid_output_report(report, raw_data);
1878 dump_buff_as_hex(buff, BUFF_SZ, raw_data, raw_size);
1879 hid_debug_event(hdev, buff);
1882 switch (report->id) {
1883 case REPORT_LED_STATE:
1884 /* 1 data byte with GPO state */
1885 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1886 "REPORT_LED_STATE", report->id, raw_size-1);
1887 hid_debug_event(hdev, buff);
1888 snprintf(buff, BUFF_SZ, "\tGPO state: 0x%02x\n", raw_data[1]);
1889 hid_debug_event(hdev, buff);
1890 break;
1891 case REPORT_BRIGHTNESS:
1892 /* 1 data byte with brightness */
1893 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1894 "REPORT_BRIGHTNESS", report->id, raw_size-1);
1895 hid_debug_event(hdev, buff);
1896 snprintf(buff, BUFF_SZ, "\tBrightness: 0x%02x\n", raw_data[1]);
1897 hid_debug_event(hdev, buff);
1898 break;
1899 case REPORT_CONTRAST:
1900 /* 1 data byte with contrast */
1901 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1902 "REPORT_CONTRAST", report->id, raw_size-1);
1903 hid_debug_event(hdev, buff);
1904 snprintf(buff, BUFF_SZ, "\tContrast: 0x%02x\n", raw_data[1]);
1905 hid_debug_event(hdev, buff);
1906 break;
1907 case REPORT_RESET:
1908 /* 2 data bytes with reset duration in ms */
1909 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1910 "REPORT_RESET", report->id, raw_size-1);
1911 hid_debug_event(hdev, buff);
1912 snprintf(buff, BUFF_SZ, "\tDuration: 0x%02x%02x (%dms)\n",
1913 raw_data[2], raw_data[1], raw_data[2] << 8 | raw_data[1]);
1914 hid_debug_event(hdev, buff);
1915 break;
1916 case REPORT_LCD_CMD:
1917 /* 63 data bytes with LCD commands */
1918 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1919 "REPORT_LCD_CMD", report->id, raw_size-1);
1920 hid_debug_event(hdev, buff);
1921 /* TODO: format decoding */
1922 break;
1923 case REPORT_LCD_DATA:
1924 /* 63 data bytes with LCD data */
1925 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1926 "REPORT_LCD_CMD", report->id, raw_size-1);
1927 /* TODO: format decoding */
1928 hid_debug_event(hdev, buff);
1929 break;
1930 case REPORT_LCD_CMD_DATA:
1931 /* 63 data bytes with LCD commands and data */
1932 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1933 "REPORT_LCD_CMD", report->id, raw_size-1);
1934 /* TODO: format decoding */
1935 hid_debug_event(hdev, buff);
1936 break;
1937 case REPORT_EE_READ:
1938 /* 3 data bytes with read area description */
1939 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1940 "REPORT_EE_READ", report->id, raw_size-1);
1941 hid_debug_event(hdev, buff);
1942 snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x\n",
1943 raw_data[2], raw_data[1]);
1944 hid_debug_event(hdev, buff);
1945 snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[3]);
1946 hid_debug_event(hdev, buff);
1947 break;
1948 case REPORT_EE_WRITE:
1949 /* 3+1..20 data bytes with write area description */
1950 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1951 "REPORT_EE_WRITE", report->id, raw_size-1);
1952 hid_debug_event(hdev, buff);
1953 snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x\n",
1954 raw_data[2], raw_data[1]);
1955 hid_debug_event(hdev, buff);
1956 snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[3]);
1957 hid_debug_event(hdev, buff);
1958 if (raw_data[3] == 0) {
1959 snprintf(buff, BUFF_SZ, "\tNo data\n");
1960 } else if (raw_data[3] + 4 <= raw_size) {
1961 snprintf(buff, BUFF_SZ, "\tData: ");
1962 hid_debug_event(hdev, buff);
1963 dump_buff_as_hex(buff, BUFF_SZ, raw_data+4, raw_data[3]);
1964 } else {
1965 snprintf(buff, BUFF_SZ, "\tData overflowed\n");
1967 hid_debug_event(hdev, buff);
1968 break;
1969 case REPORT_ERASE_MEMORY:
1970 case REPORT_BL_ERASE_MEMORY:
1971 /* 3 data bytes with pointer inside erase block */
1972 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1973 "REPORT_ERASE_MEMORY", report->id, raw_size-1);
1974 hid_debug_event(hdev, buff);
1975 switch (data->addr_sz) {
1976 case 2:
1977 snprintf(buff, BUFF_SZ, "\tAddress inside 64 byte block: 0x%02x%02x\n",
1978 raw_data[2], raw_data[1]);
1979 break;
1980 case 3:
1981 snprintf(buff, BUFF_SZ, "\tAddress inside 64 byte block: 0x%02x%02x%02x\n",
1982 raw_data[3], raw_data[2], raw_data[1]);
1983 break;
1984 default:
1985 snprintf(buff, BUFF_SZ, "\tNot supported\n");
1987 hid_debug_event(hdev, buff);
1988 break;
1989 case REPORT_READ_MEMORY:
1990 case REPORT_BL_READ_MEMORY:
1991 /* 4 data bytes with read area description */
1992 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1993 "REPORT_READ_MEMORY", report->id, raw_size-1);
1994 hid_debug_event(hdev, buff);
1995 switch (data->addr_sz) {
1996 case 2:
1997 snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x\n",
1998 raw_data[2], raw_data[1]);
1999 hid_debug_event(hdev, buff);
2000 snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[3]);
2001 break;
2002 case 3:
2003 snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x%02x\n",
2004 raw_data[3], raw_data[2], raw_data[1]);
2005 hid_debug_event(hdev, buff);
2006 snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[4]);
2007 break;
2008 default:
2009 snprintf(buff, BUFF_SZ, "\tNot supported\n");
2011 hid_debug_event(hdev, buff);
2012 break;
2013 case REPORT_WRITE_MEMORY:
2014 case REPORT_BL_WRITE_MEMORY:
2015 /* 4+1..32 data bytes with write adrea description */
2016 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
2017 "REPORT_WRITE_MEMORY", report->id, raw_size-1);
2018 hid_debug_event(hdev, buff);
2019 switch (data->addr_sz) {
2020 case 2:
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 } else if (raw_data[3] + 4 <= raw_size) {
2029 snprintf(buff, BUFF_SZ, "\tData: ");
2030 hid_debug_event(hdev, buff);
2031 dump_buff_as_hex(buff, BUFF_SZ, raw_data+4, raw_data[3]);
2032 } else {
2033 snprintf(buff, BUFF_SZ, "\tData overflowed\n");
2035 break;
2036 case 3:
2037 snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x%02x\n",
2038 raw_data[3], raw_data[2], raw_data[1]);
2039 hid_debug_event(hdev, buff);
2040 snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[4]);
2041 hid_debug_event(hdev, buff);
2042 if (raw_data[4] == 0) {
2043 snprintf(buff, BUFF_SZ, "\tNo data\n");
2044 } else if (raw_data[4] + 5 <= raw_size) {
2045 snprintf(buff, BUFF_SZ, "\tData: ");
2046 hid_debug_event(hdev, buff);
2047 dump_buff_as_hex(buff, BUFF_SZ, raw_data+5, raw_data[4]);
2048 } else {
2049 snprintf(buff, BUFF_SZ, "\tData overflowed\n");
2051 break;
2052 default:
2053 snprintf(buff, BUFF_SZ, "\tNot supported\n");
2055 hid_debug_event(hdev, buff);
2056 break;
2057 case REPORT_SPLASH_RESTART:
2058 /* TODO */
2059 break;
2060 case REPORT_EXIT_KEYBOARD:
2061 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
2062 "REPORT_EXIT_KEYBOARD", report->id, raw_size-1);
2063 hid_debug_event(hdev, buff);
2064 snprintf(buff, BUFF_SZ, "\tRestart delay: %dms (0x%02x%02x)\n",
2065 raw_data[1] | (raw_data[2] << 8),
2066 raw_data[2], raw_data[1]);
2067 hid_debug_event(hdev, buff);
2068 break;
2069 case REPORT_VERSION:
2070 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
2071 "REPORT_VERSION", report->id, raw_size-1);
2072 hid_debug_event(hdev, buff);
2073 break;
2074 case REPORT_DEVID:
2075 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
2076 "REPORT_DEVID", report->id, raw_size-1);
2077 hid_debug_event(hdev, buff);
2078 break;
2079 case REPORT_SPLASH_SIZE:
2080 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
2081 "REPORT_SPLASH_SIZE", report->id, raw_size-1);
2082 hid_debug_event(hdev, buff);
2083 break;
2084 case REPORT_HOOK_VERSION:
2085 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
2086 "REPORT_HOOK_VERSION", report->id, raw_size-1);
2087 hid_debug_event(hdev, buff);
2088 break;
2089 case REPORT_EXIT_FLASHER:
2090 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
2091 "REPORT_VERSION", report->id, raw_size-1);
2092 hid_debug_event(hdev, buff);
2093 snprintf(buff, BUFF_SZ, "\tRestart delay: %dms (0x%02x%02x)\n",
2094 raw_data[1] | (raw_data[2] << 8),
2095 raw_data[2], raw_data[1]);
2096 hid_debug_event(hdev, buff);
2097 break;
2098 default:
2099 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
2100 "<unknown>", report->id, raw_size-1);
2101 hid_debug_event(hdev, buff);
2102 break;
2104 wake_up_interruptible(&hdev->debug_wait);
2105 kfree(buff);
2108 static void picolcd_debug_raw_event(struct picolcd_data *data,
2109 struct hid_device *hdev, struct hid_report *report,
2110 u8 *raw_data, int size)
2112 char *buff;
2114 #define BUFF_SZ 256
2115 /* Avoid unnecessary overhead if debugfs is disabled */
2116 if (!hdev->debug_events)
2117 return;
2119 buff = kmalloc(BUFF_SZ, GFP_ATOMIC);
2120 if (!buff)
2121 return;
2123 switch (report->id) {
2124 case REPORT_ERROR_CODE:
2125 /* 2 data bytes with affected report and error code */
2126 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2127 "REPORT_ERROR_CODE", report->id, size-1);
2128 hid_debug_event(hdev, buff);
2129 if (raw_data[2] < ARRAY_SIZE(error_codes))
2130 snprintf(buff, BUFF_SZ, "\tError code 0x%02x (%s) in reply to report 0x%02x\n",
2131 raw_data[2], error_codes[raw_data[2]], raw_data[1]);
2132 else
2133 snprintf(buff, BUFF_SZ, "\tError code 0x%02x in reply to report 0x%02x\n",
2134 raw_data[2], raw_data[1]);
2135 hid_debug_event(hdev, buff);
2136 break;
2137 case REPORT_KEY_STATE:
2138 /* 2 data bytes with key state */
2139 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2140 "REPORT_KEY_STATE", report->id, size-1);
2141 hid_debug_event(hdev, buff);
2142 if (raw_data[1] == 0)
2143 snprintf(buff, BUFF_SZ, "\tNo key pressed\n");
2144 else if (raw_data[2] == 0)
2145 snprintf(buff, BUFF_SZ, "\tOne key pressed: 0x%02x (%d)\n",
2146 raw_data[1], raw_data[1]);
2147 else
2148 snprintf(buff, BUFF_SZ, "\tTwo keys pressed: 0x%02x (%d), 0x%02x (%d)\n",
2149 raw_data[1], raw_data[1], raw_data[2], raw_data[2]);
2150 hid_debug_event(hdev, buff);
2151 break;
2152 case REPORT_IR_DATA:
2153 /* Up to 20 byes of IR scancode data */
2154 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2155 "REPORT_IR_DATA", report->id, size-1);
2156 hid_debug_event(hdev, buff);
2157 if (raw_data[1] == 0) {
2158 snprintf(buff, BUFF_SZ, "\tUnexpectedly 0 data length\n");
2159 hid_debug_event(hdev, buff);
2160 } else if (raw_data[1] + 1 <= size) {
2161 snprintf(buff, BUFF_SZ, "\tData length: %d\n\tIR Data: ",
2162 raw_data[1]-1);
2163 hid_debug_event(hdev, buff);
2164 dump_buff_as_hex(buff, BUFF_SZ, raw_data+2, raw_data[1]-1);
2165 hid_debug_event(hdev, buff);
2166 } else {
2167 snprintf(buff, BUFF_SZ, "\tOverflowing data length: %d\n",
2168 raw_data[1]-1);
2169 hid_debug_event(hdev, buff);
2171 break;
2172 case REPORT_EE_DATA:
2173 /* Data buffer in response to REPORT_EE_READ or REPORT_EE_WRITE */
2174 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2175 "REPORT_EE_DATA", report->id, size-1);
2176 hid_debug_event(hdev, buff);
2177 snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x\n",
2178 raw_data[2], raw_data[1]);
2179 hid_debug_event(hdev, buff);
2180 snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[3]);
2181 hid_debug_event(hdev, buff);
2182 if (raw_data[3] == 0) {
2183 snprintf(buff, BUFF_SZ, "\tNo data\n");
2184 hid_debug_event(hdev, buff);
2185 } else if (raw_data[3] + 4 <= size) {
2186 snprintf(buff, BUFF_SZ, "\tData: ");
2187 hid_debug_event(hdev, buff);
2188 dump_buff_as_hex(buff, BUFF_SZ, raw_data+4, raw_data[3]);
2189 hid_debug_event(hdev, buff);
2190 } else {
2191 snprintf(buff, BUFF_SZ, "\tData overflowed\n");
2192 hid_debug_event(hdev, buff);
2194 break;
2195 case REPORT_MEMORY:
2196 /* Data buffer in response to REPORT_READ_MEMORY or REPORT_WRTIE_MEMORY */
2197 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2198 "REPORT_MEMORY", report->id, size-1);
2199 hid_debug_event(hdev, buff);
2200 switch (data->addr_sz) {
2201 case 2:
2202 snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x\n",
2203 raw_data[2], raw_data[1]);
2204 hid_debug_event(hdev, buff);
2205 snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[3]);
2206 hid_debug_event(hdev, buff);
2207 if (raw_data[3] == 0) {
2208 snprintf(buff, BUFF_SZ, "\tNo data\n");
2209 } else if (raw_data[3] + 4 <= size) {
2210 snprintf(buff, BUFF_SZ, "\tData: ");
2211 hid_debug_event(hdev, buff);
2212 dump_buff_as_hex(buff, BUFF_SZ, raw_data+4, raw_data[3]);
2213 } else {
2214 snprintf(buff, BUFF_SZ, "\tData overflowed\n");
2216 break;
2217 case 3:
2218 snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x%02x\n",
2219 raw_data[3], raw_data[2], raw_data[1]);
2220 hid_debug_event(hdev, buff);
2221 snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[4]);
2222 hid_debug_event(hdev, buff);
2223 if (raw_data[4] == 0) {
2224 snprintf(buff, BUFF_SZ, "\tNo data\n");
2225 } else if (raw_data[4] + 5 <= size) {
2226 snprintf(buff, BUFF_SZ, "\tData: ");
2227 hid_debug_event(hdev, buff);
2228 dump_buff_as_hex(buff, BUFF_SZ, raw_data+5, raw_data[4]);
2229 } else {
2230 snprintf(buff, BUFF_SZ, "\tData overflowed\n");
2232 break;
2233 default:
2234 snprintf(buff, BUFF_SZ, "\tNot supported\n");
2236 hid_debug_event(hdev, buff);
2237 break;
2238 case REPORT_VERSION:
2239 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2240 "REPORT_VERSION", report->id, size-1);
2241 hid_debug_event(hdev, buff);
2242 snprintf(buff, BUFF_SZ, "\tFirmware version: %d.%d\n",
2243 raw_data[2], raw_data[1]);
2244 hid_debug_event(hdev, buff);
2245 break;
2246 case REPORT_BL_ERASE_MEMORY:
2247 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2248 "REPORT_BL_ERASE_MEMORY", report->id, size-1);
2249 hid_debug_event(hdev, buff);
2250 /* TODO */
2251 break;
2252 case REPORT_BL_READ_MEMORY:
2253 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2254 "REPORT_BL_READ_MEMORY", report->id, size-1);
2255 hid_debug_event(hdev, buff);
2256 /* TODO */
2257 break;
2258 case REPORT_BL_WRITE_MEMORY:
2259 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2260 "REPORT_BL_WRITE_MEMORY", report->id, size-1);
2261 hid_debug_event(hdev, buff);
2262 /* TODO */
2263 break;
2264 case REPORT_DEVID:
2265 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2266 "REPORT_DEVID", report->id, size-1);
2267 hid_debug_event(hdev, buff);
2268 snprintf(buff, BUFF_SZ, "\tSerial: 0x%02x%02x%02x%02x\n",
2269 raw_data[1], raw_data[2], raw_data[3], raw_data[4]);
2270 hid_debug_event(hdev, buff);
2271 snprintf(buff, BUFF_SZ, "\tType: 0x%02x\n",
2272 raw_data[5]);
2273 hid_debug_event(hdev, buff);
2274 break;
2275 case REPORT_SPLASH_SIZE:
2276 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2277 "REPORT_SPLASH_SIZE", report->id, size-1);
2278 hid_debug_event(hdev, buff);
2279 snprintf(buff, BUFF_SZ, "\tTotal splash space: %d\n",
2280 (raw_data[2] << 8) | raw_data[1]);
2281 hid_debug_event(hdev, buff);
2282 snprintf(buff, BUFF_SZ, "\tUsed splash space: %d\n",
2283 (raw_data[4] << 8) | raw_data[3]);
2284 hid_debug_event(hdev, buff);
2285 break;
2286 case REPORT_HOOK_VERSION:
2287 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2288 "REPORT_HOOK_VERSION", report->id, size-1);
2289 hid_debug_event(hdev, buff);
2290 snprintf(buff, BUFF_SZ, "\tFirmware version: %d.%d\n",
2291 raw_data[1], raw_data[2]);
2292 hid_debug_event(hdev, buff);
2293 break;
2294 default:
2295 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2296 "<unknown>", report->id, size-1);
2297 hid_debug_event(hdev, buff);
2298 break;
2300 wake_up_interruptible(&hdev->debug_wait);
2301 kfree(buff);
2304 static void picolcd_init_devfs(struct picolcd_data *data,
2305 struct hid_report *eeprom_r, struct hid_report *eeprom_w,
2306 struct hid_report *flash_r, struct hid_report *flash_w,
2307 struct hid_report *reset)
2309 struct hid_device *hdev = data->hdev;
2311 mutex_init(&data->mutex_flash);
2313 /* reset */
2314 if (reset)
2315 data->debug_reset = debugfs_create_file("reset", 0600,
2316 hdev->debug_dir, data, &picolcd_debug_reset_fops);
2318 /* eeprom */
2319 if (eeprom_r || eeprom_w)
2320 data->debug_eeprom = debugfs_create_file("eeprom",
2321 (eeprom_w ? S_IWUSR : 0) | (eeprom_r ? S_IRUSR : 0),
2322 hdev->debug_dir, data, &picolcd_debug_eeprom_fops);
2324 /* flash */
2325 if (flash_r && flash_r->maxfield == 1 && flash_r->field[0]->report_size == 8)
2326 data->addr_sz = flash_r->field[0]->report_count - 1;
2327 else
2328 data->addr_sz = -1;
2329 if (data->addr_sz == 2 || data->addr_sz == 3) {
2330 data->debug_flash = debugfs_create_file("flash",
2331 (flash_w ? S_IWUSR : 0) | (flash_r ? S_IRUSR : 0),
2332 hdev->debug_dir, data, &picolcd_debug_flash_fops);
2333 } else if (flash_r || flash_w)
2334 hid_warn(hdev, "Unexpected FLASH access reports, please submit rdesc for review\n");
2337 static void picolcd_exit_devfs(struct picolcd_data *data)
2339 struct dentry *dent;
2341 dent = data->debug_reset;
2342 data->debug_reset = NULL;
2343 if (dent)
2344 debugfs_remove(dent);
2345 dent = data->debug_eeprom;
2346 data->debug_eeprom = NULL;
2347 if (dent)
2348 debugfs_remove(dent);
2349 dent = data->debug_flash;
2350 data->debug_flash = NULL;
2351 if (dent)
2352 debugfs_remove(dent);
2353 mutex_destroy(&data->mutex_flash);
2355 #else
2356 static inline void picolcd_debug_raw_event(struct picolcd_data *data,
2357 struct hid_device *hdev, struct hid_report *report,
2358 u8 *raw_data, int size)
2361 static inline void picolcd_init_devfs(struct picolcd_data *data,
2362 struct hid_report *eeprom_r, struct hid_report *eeprom_w,
2363 struct hid_report *flash_r, struct hid_report *flash_w,
2364 struct hid_report *reset)
2367 static inline void picolcd_exit_devfs(struct picolcd_data *data)
2370 #endif /* CONFIG_DEBUG_FS */
2373 * Handle raw report as sent by device
2375 static int picolcd_raw_event(struct hid_device *hdev,
2376 struct hid_report *report, u8 *raw_data, int size)
2378 struct picolcd_data *data = hid_get_drvdata(hdev);
2379 unsigned long flags;
2380 int ret = 0;
2382 if (!data)
2383 return 1;
2385 if (report->id == REPORT_KEY_STATE) {
2386 if (data->input_keys)
2387 ret = picolcd_raw_keypad(data, report, raw_data+1, size-1);
2388 } else if (report->id == REPORT_IR_DATA) {
2389 if (data->input_cir)
2390 ret = picolcd_raw_cir(data, report, raw_data+1, size-1);
2391 } else {
2392 spin_lock_irqsave(&data->lock, flags);
2394 * We let the caller of picolcd_send_and_wait() check if the
2395 * report we got is one of the expected ones or not.
2397 if (data->pending) {
2398 memcpy(data->pending->raw_data, raw_data+1, size-1);
2399 data->pending->raw_size = size-1;
2400 data->pending->in_report = report;
2401 complete(&data->pending->ready);
2403 spin_unlock_irqrestore(&data->lock, flags);
2406 picolcd_debug_raw_event(data, hdev, report, raw_data, size);
2407 return 1;
2410 #ifdef CONFIG_PM
2411 static int picolcd_suspend(struct hid_device *hdev, pm_message_t message)
2413 if (PMSG_IS_AUTO(message))
2414 return 0;
2416 picolcd_suspend_backlight(hid_get_drvdata(hdev));
2417 dbg_hid(PICOLCD_NAME " device ready for suspend\n");
2418 return 0;
2421 static int picolcd_resume(struct hid_device *hdev)
2423 int ret;
2424 ret = picolcd_resume_backlight(hid_get_drvdata(hdev));
2425 if (ret)
2426 dbg_hid(PICOLCD_NAME " restoring backlight failed: %d\n", ret);
2427 return 0;
2430 static int picolcd_reset_resume(struct hid_device *hdev)
2432 int ret;
2433 ret = picolcd_reset(hdev);
2434 if (ret)
2435 dbg_hid(PICOLCD_NAME " resetting our device failed: %d\n", ret);
2436 ret = picolcd_fb_reset(hid_get_drvdata(hdev), 0);
2437 if (ret)
2438 dbg_hid(PICOLCD_NAME " restoring framebuffer content failed: %d\n", ret);
2439 ret = picolcd_resume_lcd(hid_get_drvdata(hdev));
2440 if (ret)
2441 dbg_hid(PICOLCD_NAME " restoring lcd failed: %d\n", ret);
2442 ret = picolcd_resume_backlight(hid_get_drvdata(hdev));
2443 if (ret)
2444 dbg_hid(PICOLCD_NAME " restoring backlight failed: %d\n", ret);
2445 picolcd_leds_set(hid_get_drvdata(hdev));
2446 return 0;
2448 #endif
2450 /* initialize keypad input device */
2451 static int picolcd_init_keys(struct picolcd_data *data,
2452 struct hid_report *report)
2454 struct hid_device *hdev = data->hdev;
2455 struct input_dev *idev;
2456 int error, i;
2458 if (!report)
2459 return -ENODEV;
2460 if (report->maxfield != 1 || report->field[0]->report_count != 2 ||
2461 report->field[0]->report_size != 8) {
2462 hid_err(hdev, "unsupported KEY_STATE report\n");
2463 return -EINVAL;
2466 idev = input_allocate_device();
2467 if (idev == NULL) {
2468 hid_err(hdev, "failed to allocate input device\n");
2469 return -ENOMEM;
2471 input_set_drvdata(idev, hdev);
2472 memcpy(data->keycode, def_keymap, sizeof(def_keymap));
2473 idev->name = hdev->name;
2474 idev->phys = hdev->phys;
2475 idev->uniq = hdev->uniq;
2476 idev->id.bustype = hdev->bus;
2477 idev->id.vendor = hdev->vendor;
2478 idev->id.product = hdev->product;
2479 idev->id.version = hdev->version;
2480 idev->dev.parent = hdev->dev.parent;
2481 idev->keycode = &data->keycode;
2482 idev->keycodemax = PICOLCD_KEYS;
2483 idev->keycodesize = sizeof(data->keycode[0]);
2484 input_set_capability(idev, EV_MSC, MSC_SCAN);
2485 set_bit(EV_REP, idev->evbit);
2486 for (i = 0; i < PICOLCD_KEYS; i++)
2487 input_set_capability(idev, EV_KEY, data->keycode[i]);
2488 error = input_register_device(idev);
2489 if (error) {
2490 hid_err(hdev, "error registering the input device\n");
2491 input_free_device(idev);
2492 return error;
2494 data->input_keys = idev;
2495 return 0;
2498 static void picolcd_exit_keys(struct picolcd_data *data)
2500 struct input_dev *idev = data->input_keys;
2502 data->input_keys = NULL;
2503 if (idev)
2504 input_unregister_device(idev);
2507 /* initialize CIR input device */
2508 static inline int picolcd_init_cir(struct picolcd_data *data, struct hid_report *report)
2510 /* support not implemented yet */
2511 return 0;
2514 static inline void picolcd_exit_cir(struct picolcd_data *data)
2518 static int picolcd_probe_lcd(struct hid_device *hdev, struct picolcd_data *data)
2520 int error;
2522 error = picolcd_check_version(hdev);
2523 if (error)
2524 return error;
2526 if (data->version[0] != 0 && data->version[1] != 3)
2527 hid_info(hdev, "Device with untested firmware revision, please submit /sys/kernel/debug/hid/%s/rdesc for this device.\n",
2528 dev_name(&hdev->dev));
2530 /* Setup keypad input device */
2531 error = picolcd_init_keys(data, picolcd_in_report(REPORT_KEY_STATE, hdev));
2532 if (error)
2533 goto err;
2535 /* Setup CIR input device */
2536 error = picolcd_init_cir(data, picolcd_in_report(REPORT_IR_DATA, hdev));
2537 if (error)
2538 goto err;
2540 /* Set up the framebuffer device */
2541 error = picolcd_init_framebuffer(data);
2542 if (error)
2543 goto err;
2545 /* Setup lcd class device */
2546 error = picolcd_init_lcd(data, picolcd_out_report(REPORT_CONTRAST, hdev));
2547 if (error)
2548 goto err;
2550 /* Setup backlight class device */
2551 error = picolcd_init_backlight(data, picolcd_out_report(REPORT_BRIGHTNESS, hdev));
2552 if (error)
2553 goto err;
2555 /* Setup the LED class devices */
2556 error = picolcd_init_leds(data, picolcd_out_report(REPORT_LED_STATE, hdev));
2557 if (error)
2558 goto err;
2560 picolcd_init_devfs(data, picolcd_out_report(REPORT_EE_READ, hdev),
2561 picolcd_out_report(REPORT_EE_WRITE, hdev),
2562 picolcd_out_report(REPORT_READ_MEMORY, hdev),
2563 picolcd_out_report(REPORT_WRITE_MEMORY, hdev),
2564 picolcd_out_report(REPORT_RESET, hdev));
2565 return 0;
2566 err:
2567 picolcd_exit_leds(data);
2568 picolcd_exit_backlight(data);
2569 picolcd_exit_lcd(data);
2570 picolcd_exit_framebuffer(data);
2571 picolcd_exit_cir(data);
2572 picolcd_exit_keys(data);
2573 return error;
2576 static int picolcd_probe_bootloader(struct hid_device *hdev, struct picolcd_data *data)
2578 int error;
2580 error = picolcd_check_version(hdev);
2581 if (error)
2582 return error;
2584 if (data->version[0] != 1 && data->version[1] != 0)
2585 hid_info(hdev, "Device with untested bootloader revision, 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 hid_err(hdev, "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 hid_err(hdev, "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 hid_err(hdev, "hardware start failed\n");
2635 goto err_cleanup_data;
2638 error = hid_hw_open(hdev);
2639 if (error) {
2640 hid_err(hdev, "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 hid_err(hdev, "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 hid_err(hdev, "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 hid_hw_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 hid_hw_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_work_sync(&picolcd_fb_cleanup);
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");