[CPUFREQ] fix BUG on cpufreq policy init failure
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / hid / hid-picolcd.c
blobde9cf21b3494c39d7a105f75a90078ebfe8b145e
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 hid_warn(hdev, "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 hid_err(hdev, "no version response from PicoLCD\n");
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 hid_info(hdev, "PicoLCD, bootloader version %d.%d\n",
1341 verinfo->raw_data[1], verinfo->raw_data[0]);
1342 } else {
1343 hid_info(hdev, "PicoLCD, firmware version %d.%d\n",
1344 verinfo->raw_data[1], verinfo->raw_data[0]);
1346 } else {
1347 hid_err(hdev, "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 kfree(buff);
1871 hid_debug_event(hdev, " TOO BIG\n");
1872 return;
1873 } else {
1874 raw_data[0] = report->id;
1875 hid_output_report(report, raw_data);
1876 dump_buff_as_hex(buff, BUFF_SZ, raw_data, raw_size);
1877 hid_debug_event(hdev, buff);
1880 switch (report->id) {
1881 case REPORT_LED_STATE:
1882 /* 1 data byte with GPO state */
1883 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1884 "REPORT_LED_STATE", report->id, raw_size-1);
1885 hid_debug_event(hdev, buff);
1886 snprintf(buff, BUFF_SZ, "\tGPO state: 0x%02x\n", raw_data[1]);
1887 hid_debug_event(hdev, buff);
1888 break;
1889 case REPORT_BRIGHTNESS:
1890 /* 1 data byte with brightness */
1891 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1892 "REPORT_BRIGHTNESS", report->id, raw_size-1);
1893 hid_debug_event(hdev, buff);
1894 snprintf(buff, BUFF_SZ, "\tBrightness: 0x%02x\n", raw_data[1]);
1895 hid_debug_event(hdev, buff);
1896 break;
1897 case REPORT_CONTRAST:
1898 /* 1 data byte with contrast */
1899 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1900 "REPORT_CONTRAST", report->id, raw_size-1);
1901 hid_debug_event(hdev, buff);
1902 snprintf(buff, BUFF_SZ, "\tContrast: 0x%02x\n", raw_data[1]);
1903 hid_debug_event(hdev, buff);
1904 break;
1905 case REPORT_RESET:
1906 /* 2 data bytes with reset duration in ms */
1907 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1908 "REPORT_RESET", report->id, raw_size-1);
1909 hid_debug_event(hdev, buff);
1910 snprintf(buff, BUFF_SZ, "\tDuration: 0x%02x%02x (%dms)\n",
1911 raw_data[2], raw_data[1], raw_data[2] << 8 | raw_data[1]);
1912 hid_debug_event(hdev, buff);
1913 break;
1914 case REPORT_LCD_CMD:
1915 /* 63 data bytes with LCD commands */
1916 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1917 "REPORT_LCD_CMD", report->id, raw_size-1);
1918 hid_debug_event(hdev, buff);
1919 /* TODO: format decoding */
1920 break;
1921 case REPORT_LCD_DATA:
1922 /* 63 data bytes with LCD data */
1923 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1924 "REPORT_LCD_CMD", report->id, raw_size-1);
1925 /* TODO: format decoding */
1926 hid_debug_event(hdev, buff);
1927 break;
1928 case REPORT_LCD_CMD_DATA:
1929 /* 63 data bytes with LCD commands and data */
1930 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1931 "REPORT_LCD_CMD", report->id, raw_size-1);
1932 /* TODO: format decoding */
1933 hid_debug_event(hdev, buff);
1934 break;
1935 case REPORT_EE_READ:
1936 /* 3 data bytes with read area description */
1937 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1938 "REPORT_EE_READ", report->id, raw_size-1);
1939 hid_debug_event(hdev, buff);
1940 snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x\n",
1941 raw_data[2], raw_data[1]);
1942 hid_debug_event(hdev, buff);
1943 snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[3]);
1944 hid_debug_event(hdev, buff);
1945 break;
1946 case REPORT_EE_WRITE:
1947 /* 3+1..20 data bytes with write area description */
1948 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1949 "REPORT_EE_WRITE", report->id, raw_size-1);
1950 hid_debug_event(hdev, buff);
1951 snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x\n",
1952 raw_data[2], raw_data[1]);
1953 hid_debug_event(hdev, buff);
1954 snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[3]);
1955 hid_debug_event(hdev, buff);
1956 if (raw_data[3] == 0) {
1957 snprintf(buff, BUFF_SZ, "\tNo data\n");
1958 } else if (raw_data[3] + 4 <= raw_size) {
1959 snprintf(buff, BUFF_SZ, "\tData: ");
1960 hid_debug_event(hdev, buff);
1961 dump_buff_as_hex(buff, BUFF_SZ, raw_data+4, raw_data[3]);
1962 } else {
1963 snprintf(buff, BUFF_SZ, "\tData overflowed\n");
1965 hid_debug_event(hdev, buff);
1966 break;
1967 case REPORT_ERASE_MEMORY:
1968 case REPORT_BL_ERASE_MEMORY:
1969 /* 3 data bytes with pointer inside erase block */
1970 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1971 "REPORT_ERASE_MEMORY", report->id, raw_size-1);
1972 hid_debug_event(hdev, buff);
1973 switch (data->addr_sz) {
1974 case 2:
1975 snprintf(buff, BUFF_SZ, "\tAddress inside 64 byte block: 0x%02x%02x\n",
1976 raw_data[2], raw_data[1]);
1977 break;
1978 case 3:
1979 snprintf(buff, BUFF_SZ, "\tAddress inside 64 byte block: 0x%02x%02x%02x\n",
1980 raw_data[3], raw_data[2], raw_data[1]);
1981 break;
1982 default:
1983 snprintf(buff, BUFF_SZ, "\tNot supported\n");
1985 hid_debug_event(hdev, buff);
1986 break;
1987 case REPORT_READ_MEMORY:
1988 case REPORT_BL_READ_MEMORY:
1989 /* 4 data bytes with read area description */
1990 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1991 "REPORT_READ_MEMORY", report->id, raw_size-1);
1992 hid_debug_event(hdev, buff);
1993 switch (data->addr_sz) {
1994 case 2:
1995 snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x\n",
1996 raw_data[2], raw_data[1]);
1997 hid_debug_event(hdev, buff);
1998 snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[3]);
1999 break;
2000 case 3:
2001 snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x%02x\n",
2002 raw_data[3], raw_data[2], raw_data[1]);
2003 hid_debug_event(hdev, buff);
2004 snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[4]);
2005 break;
2006 default:
2007 snprintf(buff, BUFF_SZ, "\tNot supported\n");
2009 hid_debug_event(hdev, buff);
2010 break;
2011 case REPORT_WRITE_MEMORY:
2012 case REPORT_BL_WRITE_MEMORY:
2013 /* 4+1..32 data bytes with write adrea description */
2014 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
2015 "REPORT_WRITE_MEMORY", report->id, raw_size-1);
2016 hid_debug_event(hdev, buff);
2017 switch (data->addr_sz) {
2018 case 2:
2019 snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x\n",
2020 raw_data[2], raw_data[1]);
2021 hid_debug_event(hdev, buff);
2022 snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[3]);
2023 hid_debug_event(hdev, buff);
2024 if (raw_data[3] == 0) {
2025 snprintf(buff, BUFF_SZ, "\tNo data\n");
2026 } else if (raw_data[3] + 4 <= raw_size) {
2027 snprintf(buff, BUFF_SZ, "\tData: ");
2028 hid_debug_event(hdev, buff);
2029 dump_buff_as_hex(buff, BUFF_SZ, raw_data+4, raw_data[3]);
2030 } else {
2031 snprintf(buff, BUFF_SZ, "\tData overflowed\n");
2033 break;
2034 case 3:
2035 snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x%02x\n",
2036 raw_data[3], raw_data[2], raw_data[1]);
2037 hid_debug_event(hdev, buff);
2038 snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[4]);
2039 hid_debug_event(hdev, buff);
2040 if (raw_data[4] == 0) {
2041 snprintf(buff, BUFF_SZ, "\tNo data\n");
2042 } else if (raw_data[4] + 5 <= raw_size) {
2043 snprintf(buff, BUFF_SZ, "\tData: ");
2044 hid_debug_event(hdev, buff);
2045 dump_buff_as_hex(buff, BUFF_SZ, raw_data+5, raw_data[4]);
2046 } else {
2047 snprintf(buff, BUFF_SZ, "\tData overflowed\n");
2049 break;
2050 default:
2051 snprintf(buff, BUFF_SZ, "\tNot supported\n");
2053 hid_debug_event(hdev, buff);
2054 break;
2055 case REPORT_SPLASH_RESTART:
2056 /* TODO */
2057 break;
2058 case REPORT_EXIT_KEYBOARD:
2059 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
2060 "REPORT_EXIT_KEYBOARD", report->id, raw_size-1);
2061 hid_debug_event(hdev, buff);
2062 snprintf(buff, BUFF_SZ, "\tRestart delay: %dms (0x%02x%02x)\n",
2063 raw_data[1] | (raw_data[2] << 8),
2064 raw_data[2], raw_data[1]);
2065 hid_debug_event(hdev, buff);
2066 break;
2067 case REPORT_VERSION:
2068 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
2069 "REPORT_VERSION", report->id, raw_size-1);
2070 hid_debug_event(hdev, buff);
2071 break;
2072 case REPORT_DEVID:
2073 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
2074 "REPORT_DEVID", report->id, raw_size-1);
2075 hid_debug_event(hdev, buff);
2076 break;
2077 case REPORT_SPLASH_SIZE:
2078 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
2079 "REPORT_SPLASH_SIZE", report->id, raw_size-1);
2080 hid_debug_event(hdev, buff);
2081 break;
2082 case REPORT_HOOK_VERSION:
2083 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
2084 "REPORT_HOOK_VERSION", report->id, raw_size-1);
2085 hid_debug_event(hdev, buff);
2086 break;
2087 case REPORT_EXIT_FLASHER:
2088 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
2089 "REPORT_VERSION", report->id, raw_size-1);
2090 hid_debug_event(hdev, buff);
2091 snprintf(buff, BUFF_SZ, "\tRestart delay: %dms (0x%02x%02x)\n",
2092 raw_data[1] | (raw_data[2] << 8),
2093 raw_data[2], raw_data[1]);
2094 hid_debug_event(hdev, buff);
2095 break;
2096 default:
2097 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
2098 "<unknown>", report->id, raw_size-1);
2099 hid_debug_event(hdev, buff);
2100 break;
2102 wake_up_interruptible(&hdev->debug_wait);
2103 kfree(buff);
2106 static void picolcd_debug_raw_event(struct picolcd_data *data,
2107 struct hid_device *hdev, struct hid_report *report,
2108 u8 *raw_data, int size)
2110 char *buff;
2112 #define BUFF_SZ 256
2113 /* Avoid unnecessary overhead if debugfs is disabled */
2114 if (!hdev->debug_events)
2115 return;
2117 buff = kmalloc(BUFF_SZ, GFP_ATOMIC);
2118 if (!buff)
2119 return;
2121 switch (report->id) {
2122 case REPORT_ERROR_CODE:
2123 /* 2 data bytes with affected report and error code */
2124 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2125 "REPORT_ERROR_CODE", report->id, size-1);
2126 hid_debug_event(hdev, buff);
2127 if (raw_data[2] < ARRAY_SIZE(error_codes))
2128 snprintf(buff, BUFF_SZ, "\tError code 0x%02x (%s) in reply to report 0x%02x\n",
2129 raw_data[2], error_codes[raw_data[2]], raw_data[1]);
2130 else
2131 snprintf(buff, BUFF_SZ, "\tError code 0x%02x in reply to report 0x%02x\n",
2132 raw_data[2], raw_data[1]);
2133 hid_debug_event(hdev, buff);
2134 break;
2135 case REPORT_KEY_STATE:
2136 /* 2 data bytes with key state */
2137 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2138 "REPORT_KEY_STATE", report->id, size-1);
2139 hid_debug_event(hdev, buff);
2140 if (raw_data[1] == 0)
2141 snprintf(buff, BUFF_SZ, "\tNo key pressed\n");
2142 else if (raw_data[2] == 0)
2143 snprintf(buff, BUFF_SZ, "\tOne key pressed: 0x%02x (%d)\n",
2144 raw_data[1], raw_data[1]);
2145 else
2146 snprintf(buff, BUFF_SZ, "\tTwo keys pressed: 0x%02x (%d), 0x%02x (%d)\n",
2147 raw_data[1], raw_data[1], raw_data[2], raw_data[2]);
2148 hid_debug_event(hdev, buff);
2149 break;
2150 case REPORT_IR_DATA:
2151 /* Up to 20 byes of IR scancode data */
2152 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2153 "REPORT_IR_DATA", report->id, size-1);
2154 hid_debug_event(hdev, buff);
2155 if (raw_data[1] == 0) {
2156 snprintf(buff, BUFF_SZ, "\tUnexpectedly 0 data length\n");
2157 hid_debug_event(hdev, buff);
2158 } else if (raw_data[1] + 1 <= size) {
2159 snprintf(buff, BUFF_SZ, "\tData length: %d\n\tIR Data: ",
2160 raw_data[1]-1);
2161 hid_debug_event(hdev, buff);
2162 dump_buff_as_hex(buff, BUFF_SZ, raw_data+2, raw_data[1]-1);
2163 hid_debug_event(hdev, buff);
2164 } else {
2165 snprintf(buff, BUFF_SZ, "\tOverflowing data length: %d\n",
2166 raw_data[1]-1);
2167 hid_debug_event(hdev, buff);
2169 break;
2170 case REPORT_EE_DATA:
2171 /* Data buffer in response to REPORT_EE_READ or REPORT_EE_WRITE */
2172 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2173 "REPORT_EE_DATA", report->id, size-1);
2174 hid_debug_event(hdev, buff);
2175 snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x\n",
2176 raw_data[2], raw_data[1]);
2177 hid_debug_event(hdev, buff);
2178 snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[3]);
2179 hid_debug_event(hdev, buff);
2180 if (raw_data[3] == 0) {
2181 snprintf(buff, BUFF_SZ, "\tNo data\n");
2182 hid_debug_event(hdev, buff);
2183 } else if (raw_data[3] + 4 <= size) {
2184 snprintf(buff, BUFF_SZ, "\tData: ");
2185 hid_debug_event(hdev, buff);
2186 dump_buff_as_hex(buff, BUFF_SZ, raw_data+4, raw_data[3]);
2187 hid_debug_event(hdev, buff);
2188 } else {
2189 snprintf(buff, BUFF_SZ, "\tData overflowed\n");
2190 hid_debug_event(hdev, buff);
2192 break;
2193 case REPORT_MEMORY:
2194 /* Data buffer in response to REPORT_READ_MEMORY or REPORT_WRTIE_MEMORY */
2195 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2196 "REPORT_MEMORY", report->id, size-1);
2197 hid_debug_event(hdev, buff);
2198 switch (data->addr_sz) {
2199 case 2:
2200 snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x\n",
2201 raw_data[2], raw_data[1]);
2202 hid_debug_event(hdev, buff);
2203 snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[3]);
2204 hid_debug_event(hdev, buff);
2205 if (raw_data[3] == 0) {
2206 snprintf(buff, BUFF_SZ, "\tNo data\n");
2207 } else if (raw_data[3] + 4 <= size) {
2208 snprintf(buff, BUFF_SZ, "\tData: ");
2209 hid_debug_event(hdev, buff);
2210 dump_buff_as_hex(buff, BUFF_SZ, raw_data+4, raw_data[3]);
2211 } else {
2212 snprintf(buff, BUFF_SZ, "\tData overflowed\n");
2214 break;
2215 case 3:
2216 snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x%02x\n",
2217 raw_data[3], raw_data[2], raw_data[1]);
2218 hid_debug_event(hdev, buff);
2219 snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[4]);
2220 hid_debug_event(hdev, buff);
2221 if (raw_data[4] == 0) {
2222 snprintf(buff, BUFF_SZ, "\tNo data\n");
2223 } else if (raw_data[4] + 5 <= size) {
2224 snprintf(buff, BUFF_SZ, "\tData: ");
2225 hid_debug_event(hdev, buff);
2226 dump_buff_as_hex(buff, BUFF_SZ, raw_data+5, raw_data[4]);
2227 } else {
2228 snprintf(buff, BUFF_SZ, "\tData overflowed\n");
2230 break;
2231 default:
2232 snprintf(buff, BUFF_SZ, "\tNot supported\n");
2234 hid_debug_event(hdev, buff);
2235 break;
2236 case REPORT_VERSION:
2237 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2238 "REPORT_VERSION", report->id, size-1);
2239 hid_debug_event(hdev, buff);
2240 snprintf(buff, BUFF_SZ, "\tFirmware version: %d.%d\n",
2241 raw_data[2], raw_data[1]);
2242 hid_debug_event(hdev, buff);
2243 break;
2244 case REPORT_BL_ERASE_MEMORY:
2245 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2246 "REPORT_BL_ERASE_MEMORY", report->id, size-1);
2247 hid_debug_event(hdev, buff);
2248 /* TODO */
2249 break;
2250 case REPORT_BL_READ_MEMORY:
2251 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2252 "REPORT_BL_READ_MEMORY", report->id, size-1);
2253 hid_debug_event(hdev, buff);
2254 /* TODO */
2255 break;
2256 case REPORT_BL_WRITE_MEMORY:
2257 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2258 "REPORT_BL_WRITE_MEMORY", report->id, size-1);
2259 hid_debug_event(hdev, buff);
2260 /* TODO */
2261 break;
2262 case REPORT_DEVID:
2263 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2264 "REPORT_DEVID", report->id, size-1);
2265 hid_debug_event(hdev, buff);
2266 snprintf(buff, BUFF_SZ, "\tSerial: 0x%02x%02x%02x%02x\n",
2267 raw_data[1], raw_data[2], raw_data[3], raw_data[4]);
2268 hid_debug_event(hdev, buff);
2269 snprintf(buff, BUFF_SZ, "\tType: 0x%02x\n",
2270 raw_data[5]);
2271 hid_debug_event(hdev, buff);
2272 break;
2273 case REPORT_SPLASH_SIZE:
2274 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2275 "REPORT_SPLASH_SIZE", report->id, size-1);
2276 hid_debug_event(hdev, buff);
2277 snprintf(buff, BUFF_SZ, "\tTotal splash space: %d\n",
2278 (raw_data[2] << 8) | raw_data[1]);
2279 hid_debug_event(hdev, buff);
2280 snprintf(buff, BUFF_SZ, "\tUsed splash space: %d\n",
2281 (raw_data[4] << 8) | raw_data[3]);
2282 hid_debug_event(hdev, buff);
2283 break;
2284 case REPORT_HOOK_VERSION:
2285 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2286 "REPORT_HOOK_VERSION", report->id, size-1);
2287 hid_debug_event(hdev, buff);
2288 snprintf(buff, BUFF_SZ, "\tFirmware version: %d.%d\n",
2289 raw_data[1], raw_data[2]);
2290 hid_debug_event(hdev, buff);
2291 break;
2292 default:
2293 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2294 "<unknown>", report->id, size-1);
2295 hid_debug_event(hdev, buff);
2296 break;
2298 wake_up_interruptible(&hdev->debug_wait);
2299 kfree(buff);
2302 static void picolcd_init_devfs(struct picolcd_data *data,
2303 struct hid_report *eeprom_r, struct hid_report *eeprom_w,
2304 struct hid_report *flash_r, struct hid_report *flash_w,
2305 struct hid_report *reset)
2307 struct hid_device *hdev = data->hdev;
2309 mutex_init(&data->mutex_flash);
2311 /* reset */
2312 if (reset)
2313 data->debug_reset = debugfs_create_file("reset", 0600,
2314 hdev->debug_dir, data, &picolcd_debug_reset_fops);
2316 /* eeprom */
2317 if (eeprom_r || eeprom_w)
2318 data->debug_eeprom = debugfs_create_file("eeprom",
2319 (eeprom_w ? S_IWUSR : 0) | (eeprom_r ? S_IRUSR : 0),
2320 hdev->debug_dir, data, &picolcd_debug_eeprom_fops);
2322 /* flash */
2323 if (flash_r && flash_r->maxfield == 1 && flash_r->field[0]->report_size == 8)
2324 data->addr_sz = flash_r->field[0]->report_count - 1;
2325 else
2326 data->addr_sz = -1;
2327 if (data->addr_sz == 2 || data->addr_sz == 3) {
2328 data->debug_flash = debugfs_create_file("flash",
2329 (flash_w ? S_IWUSR : 0) | (flash_r ? S_IRUSR : 0),
2330 hdev->debug_dir, data, &picolcd_debug_flash_fops);
2331 } else if (flash_r || flash_w)
2332 hid_warn(hdev, "Unexpected FLASH access reports, 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 hid_err(hdev, "unsupported KEY_STATE report\n");
2461 return -EINVAL;
2464 idev = input_allocate_device();
2465 if (idev == NULL) {
2466 hid_err(hdev, "failed to allocate input device\n");
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 hid_err(hdev, "error registering the input device\n");
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 hid_info(hdev, "Device with untested firmware revision, please submit /sys/kernel/debug/hid/%s/rdesc for this device.\n",
2526 dev_name(&hdev->dev));
2528 /* Setup keypad input device */
2529 error = picolcd_init_keys(data, picolcd_in_report(REPORT_KEY_STATE, hdev));
2530 if (error)
2531 goto err;
2533 /* Setup CIR input device */
2534 error = picolcd_init_cir(data, picolcd_in_report(REPORT_IR_DATA, hdev));
2535 if (error)
2536 goto err;
2538 /* Set up the framebuffer device */
2539 error = picolcd_init_framebuffer(data);
2540 if (error)
2541 goto err;
2543 /* Setup lcd class device */
2544 error = picolcd_init_lcd(data, picolcd_out_report(REPORT_CONTRAST, hdev));
2545 if (error)
2546 goto err;
2548 /* Setup backlight class device */
2549 error = picolcd_init_backlight(data, picolcd_out_report(REPORT_BRIGHTNESS, hdev));
2550 if (error)
2551 goto err;
2553 /* Setup the LED class devices */
2554 error = picolcd_init_leds(data, picolcd_out_report(REPORT_LED_STATE, hdev));
2555 if (error)
2556 goto err;
2558 picolcd_init_devfs(data, picolcd_out_report(REPORT_EE_READ, hdev),
2559 picolcd_out_report(REPORT_EE_WRITE, hdev),
2560 picolcd_out_report(REPORT_READ_MEMORY, hdev),
2561 picolcd_out_report(REPORT_WRITE_MEMORY, hdev),
2562 picolcd_out_report(REPORT_RESET, hdev));
2563 return 0;
2564 err:
2565 picolcd_exit_leds(data);
2566 picolcd_exit_backlight(data);
2567 picolcd_exit_lcd(data);
2568 picolcd_exit_framebuffer(data);
2569 picolcd_exit_cir(data);
2570 picolcd_exit_keys(data);
2571 return error;
2574 static int picolcd_probe_bootloader(struct hid_device *hdev, struct picolcd_data *data)
2576 int error;
2578 error = picolcd_check_version(hdev);
2579 if (error)
2580 return error;
2582 if (data->version[0] != 1 && data->version[1] != 0)
2583 hid_info(hdev, "Device with untested bootloader revision, please submit /sys/kernel/debug/hid/%s/rdesc for this device.\n",
2584 dev_name(&hdev->dev));
2586 picolcd_init_devfs(data, NULL, NULL,
2587 picolcd_out_report(REPORT_BL_READ_MEMORY, hdev),
2588 picolcd_out_report(REPORT_BL_WRITE_MEMORY, hdev), NULL);
2589 return 0;
2592 static int picolcd_probe(struct hid_device *hdev,
2593 const struct hid_device_id *id)
2595 struct picolcd_data *data;
2596 int error = -ENOMEM;
2598 dbg_hid(PICOLCD_NAME " hardware probe...\n");
2601 * Let's allocate the picolcd data structure, set some reasonable
2602 * defaults, and associate it with the device
2604 data = kzalloc(sizeof(struct picolcd_data), GFP_KERNEL);
2605 if (data == NULL) {
2606 hid_err(hdev, "can't allocate space for Minibox PicoLCD device data\n");
2607 error = -ENOMEM;
2608 goto err_no_cleanup;
2611 spin_lock_init(&data->lock);
2612 mutex_init(&data->mutex);
2613 data->hdev = hdev;
2614 data->opmode_delay = 5000;
2615 if (hdev->product == USB_DEVICE_ID_PICOLCD_BOOTLOADER)
2616 data->status |= PICOLCD_BOOTLOADER;
2617 hid_set_drvdata(hdev, data);
2619 /* Parse the device reports and start it up */
2620 error = hid_parse(hdev);
2621 if (error) {
2622 hid_err(hdev, "device report parse failed\n");
2623 goto err_cleanup_data;
2626 /* We don't use hidinput but hid_hw_start() fails if nothing is
2627 * claimed. So spoof claimed input. */
2628 hdev->claimed = HID_CLAIMED_INPUT;
2629 error = hid_hw_start(hdev, 0);
2630 hdev->claimed = 0;
2631 if (error) {
2632 hid_err(hdev, "hardware start failed\n");
2633 goto err_cleanup_data;
2636 error = hid_hw_open(hdev);
2637 if (error) {
2638 hid_err(hdev, "failed to open input interrupt pipe for key and IR events\n");
2639 goto err_cleanup_hid_hw;
2642 error = device_create_file(&hdev->dev, &dev_attr_operation_mode_delay);
2643 if (error) {
2644 hid_err(hdev, "failed to create sysfs attributes\n");
2645 goto err_cleanup_hid_ll;
2648 error = device_create_file(&hdev->dev, &dev_attr_operation_mode);
2649 if (error) {
2650 hid_err(hdev, "failed to create sysfs attributes\n");
2651 goto err_cleanup_sysfs1;
2654 if (data->status & PICOLCD_BOOTLOADER)
2655 error = picolcd_probe_bootloader(hdev, data);
2656 else
2657 error = picolcd_probe_lcd(hdev, data);
2658 if (error)
2659 goto err_cleanup_sysfs2;
2661 dbg_hid(PICOLCD_NAME " activated and initialized\n");
2662 return 0;
2664 err_cleanup_sysfs2:
2665 device_remove_file(&hdev->dev, &dev_attr_operation_mode);
2666 err_cleanup_sysfs1:
2667 device_remove_file(&hdev->dev, &dev_attr_operation_mode_delay);
2668 err_cleanup_hid_ll:
2669 hid_hw_close(hdev);
2670 err_cleanup_hid_hw:
2671 hid_hw_stop(hdev);
2672 err_cleanup_data:
2673 kfree(data);
2674 err_no_cleanup:
2675 hid_set_drvdata(hdev, NULL);
2677 return error;
2680 static void picolcd_remove(struct hid_device *hdev)
2682 struct picolcd_data *data = hid_get_drvdata(hdev);
2683 unsigned long flags;
2685 dbg_hid(PICOLCD_NAME " hardware remove...\n");
2686 spin_lock_irqsave(&data->lock, flags);
2687 data->status |= PICOLCD_FAILED;
2688 spin_unlock_irqrestore(&data->lock, flags);
2689 #ifdef CONFIG_HID_PICOLCD_FB
2690 /* short-circuit FB as early as possible in order to
2691 * avoid long delays if we host console.
2693 if (data->fb_info)
2694 data->fb_info->par = NULL;
2695 #endif
2697 picolcd_exit_devfs(data);
2698 device_remove_file(&hdev->dev, &dev_attr_operation_mode);
2699 device_remove_file(&hdev->dev, &dev_attr_operation_mode_delay);
2700 hid_hw_close(hdev);
2701 hid_hw_stop(hdev);
2702 hid_set_drvdata(hdev, NULL);
2704 /* Shortcut potential pending reply that will never arrive */
2705 spin_lock_irqsave(&data->lock, flags);
2706 if (data->pending)
2707 complete(&data->pending->ready);
2708 spin_unlock_irqrestore(&data->lock, flags);
2710 /* Cleanup LED */
2711 picolcd_exit_leds(data);
2712 /* Clean up the framebuffer */
2713 picolcd_exit_backlight(data);
2714 picolcd_exit_lcd(data);
2715 picolcd_exit_framebuffer(data);
2716 /* Cleanup input */
2717 picolcd_exit_cir(data);
2718 picolcd_exit_keys(data);
2720 mutex_destroy(&data->mutex);
2721 /* Finally, clean up the picolcd data itself */
2722 kfree(data);
2725 static const struct hid_device_id picolcd_devices[] = {
2726 { HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP, USB_DEVICE_ID_PICOLCD) },
2727 { HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP, USB_DEVICE_ID_PICOLCD_BOOTLOADER) },
2730 MODULE_DEVICE_TABLE(hid, picolcd_devices);
2732 static struct hid_driver picolcd_driver = {
2733 .name = "hid-picolcd",
2734 .id_table = picolcd_devices,
2735 .probe = picolcd_probe,
2736 .remove = picolcd_remove,
2737 .raw_event = picolcd_raw_event,
2738 #ifdef CONFIG_PM
2739 .suspend = picolcd_suspend,
2740 .resume = picolcd_resume,
2741 .reset_resume = picolcd_reset_resume,
2742 #endif
2745 static int __init picolcd_init(void)
2747 return hid_register_driver(&picolcd_driver);
2750 static void __exit picolcd_exit(void)
2752 hid_unregister_driver(&picolcd_driver);
2753 #ifdef CONFIG_HID_PICOLCD_FB
2754 flush_work_sync(&picolcd_fb_cleanup);
2755 WARN_ON(fb_pending);
2756 #endif
2759 module_init(picolcd_init);
2760 module_exit(picolcd_exit);
2761 MODULE_DESCRIPTION("Minibox graphics PicoLCD Driver");
2762 MODULE_LICENSE("GPL v2");