blob: 7aabf65c48ef0d4aa31b7e8f74a205d447477bbd [file] [log] [blame]
Bruno Prémont236db472010-03-30 22:33:50 +02001/***************************************************************************
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 ***************************************************************************/
19
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>
26
Bruno Prémontb8c21cf2010-03-30 22:34:30 +020027#include <linux/fb.h>
28#include <linux/vmalloc.h>
Bruno Prémontf1c21762010-03-30 22:35:27 +020029#include <linux/backlight.h>
Bruno Prémonte8d931b2010-03-30 22:36:07 +020030#include <linux/lcd.h>
Bruno Prémontb8c21cf2010-03-30 22:34:30 +020031
Bruno Prémont467d6522010-03-30 22:36:49 +020032#include <linux/leds.h>
33
Bruno Prémont236db472010-03-30 22:33:50 +020034#include <linux/seq_file.h>
35#include <linux/debugfs.h>
36
37#include <linux/completion.h>
Jiri Kosinaeb741102010-04-01 08:24:42 +020038#include <linux/uaccess.h>
Bruno Prémont236db472010-03-30 22:33:50 +020039
40#define PICOLCD_NAME "PicoLCD (graphic)"
41
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] */
79
Bruno Prémont5435f282010-04-11 12:17:45 +020080#ifdef CONFIG_HID_PICOLCD_FB
Bruno Prémontb8c21cf2010-03-30 22:34:30 +020081/* Framebuffer
82 *
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.
87 *
88 * The display can be updated at a tile granularity.
89 *
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 * +----------------+----------------+----------------+----------------+
100 */
101#define PICOLCDFB_NAME "picolcdfb"
102#define PICOLCDFB_WIDTH (256)
103#define PICOLCDFB_HEIGHT (64)
104#define PICOLCDFB_SIZE (PICOLCDFB_WIDTH * PICOLCDFB_HEIGHT / 8)
105
106#define PICOLCDFB_UPDATE_RATE_LIMIT 10
107#define PICOLCDFB_UPDATE_RATE_DEFAULT 2
108
109/* Framebuffer visual structures */
110static 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,
119};
120
121static 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};
Bruno Prémont5435f282010-04-11 12:17:45 +0200131#endif /* CONFIG_HID_PICOLCD_FB */
Bruno Prémontb8c21cf2010-03-30 22:34:30 +0200132
Bruno Prémont236db472010-03-30 22:33:50 +0200133/* Input device
134 *
135 * The PicoLCD has an IR receiver header, a built-in keypad with 5 keys
136 * and header for 4x4 key matrix. The built-in keys are part of the matrix.
137 */
138static const unsigned short def_keymap[] = {
139 KEY_RESERVED, /* none */
140 KEY_BACK, /* col 4 + row 1 */
141 KEY_HOMEPAGE, /* col 3 + row 1 */
142 KEY_RESERVED, /* col 2 + row 1 */
143 KEY_RESERVED, /* col 1 + row 1 */
144 KEY_SCROLLUP, /* col 4 + row 2 */
145 KEY_OK, /* col 3 + row 2 */
146 KEY_SCROLLDOWN, /* col 2 + row 2 */
147 KEY_RESERVED, /* col 1 + row 2 */
148 KEY_RESERVED, /* col 4 + row 3 */
149 KEY_RESERVED, /* col 3 + row 3 */
150 KEY_RESERVED, /* col 2 + row 3 */
151 KEY_RESERVED, /* col 1 + row 3 */
152 KEY_RESERVED, /* col 4 + row 4 */
153 KEY_RESERVED, /* col 3 + row 4 */
154 KEY_RESERVED, /* col 2 + row 4 */
155 KEY_RESERVED, /* col 1 + row 4 */
156};
157#define PICOLCD_KEYS ARRAY_SIZE(def_keymap)
158
159/* Description of in-progress IO operation, used for operations
160 * that trigger response from device */
161struct picolcd_pending {
162 struct hid_report *out_report;
163 struct hid_report *in_report;
164 struct completion ready;
165 int raw_size;
166 u8 raw_data[64];
167};
168
169/* Per device data structure */
170struct picolcd_data {
171 struct hid_device *hdev;
172#ifdef CONFIG_DEBUG_FS
Bruno Prémont9bbf2b92010-03-30 22:38:09 +0200173 struct dentry *debug_reset;
174 struct dentry *debug_eeprom;
175 struct dentry *debug_flash;
176 struct mutex mutex_flash;
Bruno Prémont236db472010-03-30 22:33:50 +0200177 int addr_sz;
178#endif
179 u8 version[2];
Bruno Prémont0b5adf92010-04-25 21:29:16 +0200180 unsigned short opmode_delay;
Bruno Prémont236db472010-03-30 22:33:50 +0200181 /* input stuff */
182 u8 pressed_keys[2];
183 struct input_dev *input_keys;
184 struct input_dev *input_cir;
185 unsigned short keycode[PICOLCD_KEYS];
186
Bruno Prémont5435f282010-04-11 12:17:45 +0200187#ifdef CONFIG_HID_PICOLCD_FB
Bruno Prémontb8c21cf2010-03-30 22:34:30 +0200188 /* Framebuffer stuff */
189 u8 fb_update_rate;
190 u8 fb_bpp;
191 u8 *fb_vbitmap; /* local copy of what was sent to PicoLCD */
192 u8 *fb_bitmap; /* framebuffer */
193 struct fb_info *fb_info;
194 struct fb_deferred_io fb_defio;
Bruno Prémont5435f282010-04-11 12:17:45 +0200195#endif /* CONFIG_HID_PICOLCD_FB */
196#ifdef CONFIG_HID_PICOLCD_LCD
Bruno Prémonte8d931b2010-03-30 22:36:07 +0200197 struct lcd_device *lcd;
198 u8 lcd_contrast;
Bruno Prémont5435f282010-04-11 12:17:45 +0200199#endif /* CONFIG_HID_PICOLCD_LCD */
200#ifdef CONFIG_HID_PICOLCD_BACKLIGHT
Bruno Prémontf1c21762010-03-30 22:35:27 +0200201 struct backlight_device *backlight;
202 u8 lcd_brightness;
203 u8 lcd_power;
Bruno Prémont5435f282010-04-11 12:17:45 +0200204#endif /* CONFIG_HID_PICOLCD_BACKLIGHT */
205#ifdef CONFIG_HID_PICOLCD_LEDS
Bruno Prémont467d6522010-03-30 22:36:49 +0200206 /* LED stuff */
207 u8 led_state;
208 struct led_classdev *led[8];
Bruno Prémont5435f282010-04-11 12:17:45 +0200209#endif /* CONFIG_HID_PICOLCD_LEDS */
Bruno Prémontb8c21cf2010-03-30 22:34:30 +0200210
Bruno Prémont236db472010-03-30 22:33:50 +0200211 /* Housekeeping stuff */
212 spinlock_t lock;
213 struct mutex mutex;
214 struct picolcd_pending *pending;
215 int status;
216#define PICOLCD_BOOTLOADER 1
217#define PICOLCD_FAILED 2
Bruno Prémontb8c21cf2010-03-30 22:34:30 +0200218#define PICOLCD_READY_FB 4
Bruno Prémont236db472010-03-30 22:33:50 +0200219};
220
221
222/* Find a given report */
223#define picolcd_in_report(id, dev) picolcd_report(id, dev, HID_INPUT_REPORT)
224#define picolcd_out_report(id, dev) picolcd_report(id, dev, HID_OUTPUT_REPORT)
225
226static struct hid_report *picolcd_report(int id, struct hid_device *hdev, int dir)
227{
228 struct list_head *feature_report_list = &hdev->report_enum[dir].report_list;
229 struct hid_report *report = NULL;
230
231 list_for_each_entry(report, feature_report_list, list) {
232 if (report->id == id)
233 return report;
234 }
235 dev_warn(&hdev->dev, "No report with id 0x%x found\n", id);
236 return NULL;
237}
238
239#ifdef CONFIG_DEBUG_FS
240static void picolcd_debug_out_report(struct picolcd_data *data,
241 struct hid_device *hdev, struct hid_report *report);
242#define usbhid_submit_report(a, b, c) \
243 do { \
244 picolcd_debug_out_report(hid_get_drvdata(a), a, b); \
245 usbhid_submit_report(a, b, c); \
246 } while (0)
247#endif
248
249/* Submit a report and wait for a reply from device - if device fades away
250 * or does not respond in time, return NULL */
251static struct picolcd_pending *picolcd_send_and_wait(struct hid_device *hdev,
252 int report_id, const u8 *raw_data, int size)
253{
254 struct picolcd_data *data = hid_get_drvdata(hdev);
255 struct picolcd_pending *work;
256 struct hid_report *report = picolcd_out_report(report_id, hdev);
257 unsigned long flags;
258 int i, j, k;
259
260 if (!report || !data)
261 return NULL;
262 if (data->status & PICOLCD_FAILED)
263 return NULL;
264 work = kzalloc(sizeof(*work), GFP_KERNEL);
265 if (!work)
266 return NULL;
267
268 init_completion(&work->ready);
269 work->out_report = report;
270 work->in_report = NULL;
271 work->raw_size = 0;
272
273 mutex_lock(&data->mutex);
274 spin_lock_irqsave(&data->lock, flags);
275 for (i = k = 0; i < report->maxfield; i++)
276 for (j = 0; j < report->field[i]->report_count; j++) {
277 hid_set_field(report->field[i], j, k < size ? raw_data[k] : 0);
278 k++;
279 }
280 data->pending = work;
281 usbhid_submit_report(data->hdev, report, USB_DIR_OUT);
282 spin_unlock_irqrestore(&data->lock, flags);
283 wait_for_completion_interruptible_timeout(&work->ready, HZ*2);
284 spin_lock_irqsave(&data->lock, flags);
285 data->pending = NULL;
286 spin_unlock_irqrestore(&data->lock, flags);
287 mutex_unlock(&data->mutex);
288 return work;
289}
290
Bruno Prémont5435f282010-04-11 12:17:45 +0200291#ifdef CONFIG_HID_PICOLCD_FB
Bruno Prémontb8c21cf2010-03-30 22:34:30 +0200292/* Send a given tile to PicoLCD */
293static int picolcd_fb_send_tile(struct hid_device *hdev, int chip, int tile)
294{
295 struct picolcd_data *data = hid_get_drvdata(hdev);
296 struct hid_report *report1 = picolcd_out_report(REPORT_LCD_CMD_DATA, hdev);
297 struct hid_report *report2 = picolcd_out_report(REPORT_LCD_DATA, hdev);
298 unsigned long flags;
299 u8 *tdata;
300 int i;
301
302 if (!report1 || report1->maxfield != 1 || !report2 || report2->maxfield != 1)
303 return -ENODEV;
304
305 spin_lock_irqsave(&data->lock, flags);
306 hid_set_field(report1->field[0], 0, chip << 2);
307 hid_set_field(report1->field[0], 1, 0x02);
308 hid_set_field(report1->field[0], 2, 0x00);
309 hid_set_field(report1->field[0], 3, 0x00);
310 hid_set_field(report1->field[0], 4, 0xb8 | tile);
311 hid_set_field(report1->field[0], 5, 0x00);
312 hid_set_field(report1->field[0], 6, 0x00);
313 hid_set_field(report1->field[0], 7, 0x40);
314 hid_set_field(report1->field[0], 8, 0x00);
315 hid_set_field(report1->field[0], 9, 0x00);
316 hid_set_field(report1->field[0], 10, 32);
317
318 hid_set_field(report2->field[0], 0, (chip << 2) | 0x01);
319 hid_set_field(report2->field[0], 1, 0x00);
320 hid_set_field(report2->field[0], 2, 0x00);
321 hid_set_field(report2->field[0], 3, 32);
322
323 tdata = data->fb_vbitmap + (tile * 4 + chip) * 64;
324 for (i = 0; i < 64; i++)
325 if (i < 32)
326 hid_set_field(report1->field[0], 11 + i, tdata[i]);
327 else
328 hid_set_field(report2->field[0], 4 + i - 32, tdata[i]);
329
330 usbhid_submit_report(data->hdev, report1, USB_DIR_OUT);
331 usbhid_submit_report(data->hdev, report2, USB_DIR_OUT);
332 spin_unlock_irqrestore(&data->lock, flags);
333 return 0;
334}
335
336/* Translate a single tile*/
337static int picolcd_fb_update_tile(u8 *vbitmap, const u8 *bitmap, int bpp,
338 int chip, int tile)
339{
340 int i, b, changed = 0;
341 u8 tdata[64];
342 u8 *vdata = vbitmap + (tile * 4 + chip) * 64;
343
344 if (bpp == 1) {
345 for (b = 7; b >= 0; b--) {
346 const u8 *bdata = bitmap + tile * 256 + chip * 8 + b * 32;
347 for (i = 0; i < 64; i++) {
348 tdata[i] <<= 1;
349 tdata[i] |= (bdata[i/8] >> (7 - i % 8)) & 0x01;
350 }
351 }
352 } else if (bpp == 8) {
353 for (b = 7; b >= 0; b--) {
354 const u8 *bdata = bitmap + (tile * 256 + chip * 8 + b * 32) * 8;
355 for (i = 0; i < 64; i++) {
356 tdata[i] <<= 1;
357 tdata[i] |= (bdata[i] & 0x80) ? 0x01 : 0x00;
358 }
359 }
360 } else {
361 /* Oops, we should never get here! */
362 WARN_ON(1);
363 return 0;
364 }
365
366 for (i = 0; i < 64; i++)
367 if (tdata[i] != vdata[i]) {
368 changed = 1;
369 vdata[i] = tdata[i];
370 }
371 return changed;
372}
373
374/* Reconfigure LCD display */
375static int picolcd_fb_reset(struct picolcd_data *data, int clear)
376{
377 struct hid_report *report = picolcd_out_report(REPORT_LCD_CMD, data->hdev);
378 int i, j;
379 unsigned long flags;
380 static const u8 mapcmd[8] = { 0x00, 0x02, 0x00, 0x64, 0x3f, 0x00, 0x64, 0xc0 };
381
382 if (!report || report->maxfield != 1)
383 return -ENODEV;
384
385 spin_lock_irqsave(&data->lock, flags);
386 for (i = 0; i < 4; i++) {
387 for (j = 0; j < report->field[0]->maxusage; j++)
388 if (j == 0)
389 hid_set_field(report->field[0], j, i << 2);
390 else if (j < sizeof(mapcmd))
391 hid_set_field(report->field[0], j, mapcmd[j]);
392 else
393 hid_set_field(report->field[0], j, 0);
394 usbhid_submit_report(data->hdev, report, USB_DIR_OUT);
395 }
396
397 data->status |= PICOLCD_READY_FB;
398 spin_unlock_irqrestore(&data->lock, flags);
399
400 if (data->fb_bitmap) {
401 if (clear) {
402 memset(data->fb_vbitmap, 0xff, PICOLCDFB_SIZE);
403 memset(data->fb_bitmap, 0, PICOLCDFB_SIZE*data->fb_bpp);
404 } else {
405 /* invert 1 byte in each tile to force resend */
406 for (i = 0; i < PICOLCDFB_SIZE; i += 64)
407 data->fb_vbitmap[i] = ~data->fb_vbitmap[i];
408 }
409 }
410
411 /* schedule first output of framebuffer */
412 if (data->fb_info)
413 schedule_delayed_work(&data->fb_info->deferred_work, 0);
414
415 return 0;
416}
417
418/* Update fb_vbitmap from the screen_base and send changed tiles to device */
419static void picolcd_fb_update(struct picolcd_data *data)
420{
421 int chip, tile, n;
422 unsigned long flags;
423
424 spin_lock_irqsave(&data->lock, flags);
425 if (!(data->status & PICOLCD_READY_FB)) {
426 spin_unlock_irqrestore(&data->lock, flags);
427 picolcd_fb_reset(data, 0);
428 } else {
429 spin_unlock_irqrestore(&data->lock, flags);
430 }
431
432 /*
433 * Translate the framebuffer into the format needed by the PicoLCD.
434 * See display layout above.
435 * Do this one tile after the other and push those tiles that changed.
436 *
437 * Wait for our IO to complete as otherwise we might flood the queue!
438 */
439 n = 0;
440 for (chip = 0; chip < 4; chip++)
441 for (tile = 0; tile < 8; tile++)
442 if (picolcd_fb_update_tile(data->fb_vbitmap,
443 data->fb_bitmap, data->fb_bpp, chip, tile)) {
444 n += 2;
445 if (n >= HID_OUTPUT_FIFO_SIZE / 2) {
446 usbhid_wait_io(data->hdev);
447 n = 0;
448 }
449 picolcd_fb_send_tile(data->hdev, chip, tile);
450 }
451 if (n)
452 usbhid_wait_io(data->hdev);
453}
454
455/* Stub to call the system default and update the image on the picoLCD */
456static void picolcd_fb_fillrect(struct fb_info *info,
457 const struct fb_fillrect *rect)
458{
459 if (!info->par)
460 return;
461 sys_fillrect(info, rect);
462
463 schedule_delayed_work(&info->deferred_work, 0);
464}
465
466/* Stub to call the system default and update the image on the picoLCD */
467static void picolcd_fb_copyarea(struct fb_info *info,
468 const struct fb_copyarea *area)
469{
470 if (!info->par)
471 return;
472 sys_copyarea(info, area);
473
474 schedule_delayed_work(&info->deferred_work, 0);
475}
476
477/* Stub to call the system default and update the image on the picoLCD */
478static void picolcd_fb_imageblit(struct fb_info *info, const struct fb_image *image)
479{
480 if (!info->par)
481 return;
482 sys_imageblit(info, image);
483
484 schedule_delayed_work(&info->deferred_work, 0);
485}
486
487/*
488 * this is the slow path from userspace. they can seek and write to
489 * the fb. it's inefficient to do anything less than a full screen draw
490 */
491static ssize_t picolcd_fb_write(struct fb_info *info, const char __user *buf,
492 size_t count, loff_t *ppos)
493{
494 ssize_t ret;
495 if (!info->par)
496 return -ENODEV;
497 ret = fb_sys_write(info, buf, count, ppos);
498 if (ret >= 0)
499 schedule_delayed_work(&info->deferred_work, 0);
500 return ret;
501}
502
503static int picolcd_fb_blank(int blank, struct fb_info *info)
504{
505 if (!info->par)
506 return -ENODEV;
507 /* We let fb notification do this for us via lcd/backlight device */
508 return 0;
509}
510
511static void picolcd_fb_destroy(struct fb_info *info)
512{
513 struct picolcd_data *data = info->par;
514 info->par = NULL;
515 if (data)
516 data->fb_info = NULL;
517 fb_deferred_io_cleanup(info);
518 framebuffer_release(info);
519}
520
521static int picolcd_fb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
522{
523 __u32 bpp = var->bits_per_pixel;
524 __u32 activate = var->activate;
525
526 /* only allow 1/8 bit depth (8-bit is grayscale) */
527 *var = picolcdfb_var;
528 var->activate = activate;
529 if (bpp >= 8)
530 var->bits_per_pixel = 8;
531 else
532 var->bits_per_pixel = 1;
533 return 0;
534}
535
536static int picolcd_set_par(struct fb_info *info)
537{
538 struct picolcd_data *data = info->par;
539 u8 *o_fb, *n_fb;
540 if (info->var.bits_per_pixel == data->fb_bpp)
541 return 0;
542 /* switch between 1/8 bit depths */
543 if (info->var.bits_per_pixel != 1 && info->var.bits_per_pixel != 8)
544 return -EINVAL;
545
546 o_fb = data->fb_bitmap;
547 n_fb = vmalloc(PICOLCDFB_SIZE*info->var.bits_per_pixel);
548 if (!n_fb)
549 return -ENOMEM;
550
551 fb_deferred_io_cleanup(info);
552 /* translate FB content to new bits-per-pixel */
553 if (info->var.bits_per_pixel == 1) {
554 int i, b;
555 for (i = 0; i < PICOLCDFB_SIZE; i++) {
556 u8 p = 0;
557 for (b = 0; b < 8; b++) {
558 p <<= 1;
559 p |= o_fb[i*8+b] ? 0x01 : 0x00;
560 }
561 }
562 info->fix.visual = FB_VISUAL_MONO01;
563 info->fix.line_length = PICOLCDFB_WIDTH / 8;
564 } else {
565 int i;
566 for (i = 0; i < PICOLCDFB_SIZE * 8; i++)
567 n_fb[i] = o_fb[i/8] & (0x01 << (7 - i % 8)) ? 0xff : 0x00;
568 info->fix.visual = FB_VISUAL_TRUECOLOR;
569 info->fix.line_length = PICOLCDFB_WIDTH;
570 }
571
572 data->fb_bitmap = n_fb;
573 data->fb_bpp = info->var.bits_per_pixel;
574 info->screen_base = (char __force __iomem *)n_fb;
575 info->fix.smem_start = (unsigned long)n_fb;
576 info->fix.smem_len = PICOLCDFB_SIZE*data->fb_bpp;
577 fb_deferred_io_init(info);
578 vfree(o_fb);
579 return 0;
580}
581
582/* Note this can't be const because of struct fb_info definition */
583static struct fb_ops picolcdfb_ops = {
584 .owner = THIS_MODULE,
585 .fb_destroy = picolcd_fb_destroy,
586 .fb_read = fb_sys_read,
587 .fb_write = picolcd_fb_write,
588 .fb_blank = picolcd_fb_blank,
589 .fb_fillrect = picolcd_fb_fillrect,
590 .fb_copyarea = picolcd_fb_copyarea,
591 .fb_imageblit = picolcd_fb_imageblit,
592 .fb_check_var = picolcd_fb_check_var,
593 .fb_set_par = picolcd_set_par,
594};
595
596
597/* Callback from deferred IO workqueue */
598static void picolcd_fb_deferred_io(struct fb_info *info, struct list_head *pagelist)
599{
600 picolcd_fb_update(info->par);
601}
602
603static const struct fb_deferred_io picolcd_fb_defio = {
604 .delay = HZ / PICOLCDFB_UPDATE_RATE_DEFAULT,
605 .deferred_io = picolcd_fb_deferred_io,
606};
607
608
609/*
610 * The "fb_update_rate" sysfs attribute
611 */
612static ssize_t picolcd_fb_update_rate_show(struct device *dev,
613 struct device_attribute *attr, char *buf)
614{
615 struct picolcd_data *data = dev_get_drvdata(dev);
616 unsigned i, fb_update_rate = data->fb_update_rate;
617 size_t ret = 0;
618
619 for (i = 1; i <= PICOLCDFB_UPDATE_RATE_LIMIT; i++)
620 if (ret >= PAGE_SIZE)
621 break;
622 else if (i == fb_update_rate)
623 ret += snprintf(buf+ret, PAGE_SIZE-ret, "[%u] ", i);
624 else
625 ret += snprintf(buf+ret, PAGE_SIZE-ret, "%u ", i);
626 if (ret > 0)
627 buf[min(ret, (size_t)PAGE_SIZE)-1] = '\n';
628 return ret;
629}
630
631static ssize_t picolcd_fb_update_rate_store(struct device *dev,
632 struct device_attribute *attr, const char *buf, size_t count)
633{
634 struct picolcd_data *data = dev_get_drvdata(dev);
635 int i;
636 unsigned u;
637
638 if (count < 1 || count > 10)
639 return -EINVAL;
640
641 i = sscanf(buf, "%u", &u);
642 if (i != 1)
643 return -EINVAL;
644
645 if (u > PICOLCDFB_UPDATE_RATE_LIMIT)
646 return -ERANGE;
647 else if (u == 0)
648 u = PICOLCDFB_UPDATE_RATE_DEFAULT;
649
650 data->fb_update_rate = u;
651 data->fb_defio.delay = HZ / data->fb_update_rate;
652 return count;
653}
654
655static DEVICE_ATTR(fb_update_rate, 0666, picolcd_fb_update_rate_show,
656 picolcd_fb_update_rate_store);
657
658/* initialize Framebuffer device */
659static int picolcd_init_framebuffer(struct picolcd_data *data)
660{
661 struct device *dev = &data->hdev->dev;
662 struct fb_info *info = NULL;
663 int error = -ENOMEM;
664 u8 *fb_vbitmap = NULL;
665 u8 *fb_bitmap = NULL;
666
667 fb_bitmap = vmalloc(PICOLCDFB_SIZE*picolcdfb_var.bits_per_pixel);
668 if (fb_bitmap == NULL) {
669 dev_err(dev, "can't get a free page for framebuffer\n");
670 goto err_nomem;
671 }
672
673 fb_vbitmap = kmalloc(PICOLCDFB_SIZE, GFP_KERNEL);
674 if (fb_vbitmap == NULL) {
675 dev_err(dev, "can't alloc vbitmap image buffer\n");
676 goto err_nomem;
677 }
678
679 data->fb_update_rate = PICOLCDFB_UPDATE_RATE_DEFAULT;
680 data->fb_defio = picolcd_fb_defio;
681 info = framebuffer_alloc(0, dev);
682 if (info == NULL) {
683 dev_err(dev, "failed to allocate a framebuffer\n");
684 goto err_nomem;
685 }
686
687 info->fbdefio = &data->fb_defio;
688 info->screen_base = (char __force __iomem *)fb_bitmap;
689 info->fbops = &picolcdfb_ops;
690 info->var = picolcdfb_var;
691 info->fix = picolcdfb_fix;
692 info->fix.smem_len = PICOLCDFB_SIZE;
693 info->fix.smem_start = (unsigned long)fb_bitmap;
694 info->par = data;
695 info->flags = FBINFO_FLAG_DEFAULT;
696
697 data->fb_vbitmap = fb_vbitmap;
698 data->fb_bitmap = fb_bitmap;
699 data->fb_bpp = picolcdfb_var.bits_per_pixel;
700 error = picolcd_fb_reset(data, 1);
701 if (error) {
702 dev_err(dev, "failed to configure display\n");
703 goto err_cleanup;
704 }
705 error = device_create_file(dev, &dev_attr_fb_update_rate);
706 if (error) {
707 dev_err(dev, "failed to create sysfs attributes\n");
708 goto err_cleanup;
709 }
710 data->fb_info = info;
711 error = register_framebuffer(info);
712 if (error) {
713 dev_err(dev, "failed to register framebuffer\n");
714 goto err_sysfs;
715 }
716 fb_deferred_io_init(info);
717 /* schedule first output of framebuffer */
718 schedule_delayed_work(&info->deferred_work, 0);
719 return 0;
720
721err_sysfs:
722 device_remove_file(dev, &dev_attr_fb_update_rate);
723err_cleanup:
724 data->fb_vbitmap = NULL;
725 data->fb_bitmap = NULL;
726 data->fb_bpp = 0;
727 data->fb_info = NULL;
728
729err_nomem:
730 framebuffer_release(info);
731 vfree(fb_bitmap);
732 kfree(fb_vbitmap);
733 return error;
734}
735
736static void picolcd_exit_framebuffer(struct picolcd_data *data)
737{
738 struct fb_info *info = data->fb_info;
739 u8 *fb_vbitmap = data->fb_vbitmap;
740 u8 *fb_bitmap = data->fb_bitmap;
741
742 if (!info)
743 return;
744
745 data->fb_vbitmap = NULL;
746 data->fb_bitmap = NULL;
747 data->fb_bpp = 0;
748 data->fb_info = NULL;
749 device_remove_file(&data->hdev->dev, &dev_attr_fb_update_rate);
750 fb_deferred_io_cleanup(info);
751 unregister_framebuffer(info);
752 vfree(fb_bitmap);
753 kfree(fb_vbitmap);
754}
755
Bruno Prémontf1c21762010-03-30 22:35:27 +0200756#define picolcd_fbinfo(d) ((d)->fb_info)
Bruno Prémontb8c21cf2010-03-30 22:34:30 +0200757#else
758static inline int picolcd_fb_reset(struct picolcd_data *data, int clear)
759{
760 return 0;
761}
762static inline int picolcd_init_framebuffer(struct picolcd_data *data)
763{
764 return 0;
765}
Bruno Prémont9d71ea02010-05-02 16:05:05 +0200766static inline void picolcd_exit_framebuffer(struct picolcd_data *data)
Bruno Prémontb8c21cf2010-03-30 22:34:30 +0200767{
768}
Bruno Prémontf1c21762010-03-30 22:35:27 +0200769#define picolcd_fbinfo(d) NULL
Bruno Prémont5435f282010-04-11 12:17:45 +0200770#endif /* CONFIG_HID_PICOLCD_FB */
Bruno Prémontb8c21cf2010-03-30 22:34:30 +0200771
Bruno Prémont5435f282010-04-11 12:17:45 +0200772#ifdef CONFIG_HID_PICOLCD_BACKLIGHT
Bruno Prémontf1c21762010-03-30 22:35:27 +0200773/*
774 * backlight class device
775 */
776static int picolcd_get_brightness(struct backlight_device *bdev)
777{
778 struct picolcd_data *data = bl_get_data(bdev);
779 return data->lcd_brightness;
780}
781
782static int picolcd_set_brightness(struct backlight_device *bdev)
783{
784 struct picolcd_data *data = bl_get_data(bdev);
785 struct hid_report *report = picolcd_out_report(REPORT_BRIGHTNESS, data->hdev);
786 unsigned long flags;
787
788 if (!report || report->maxfield != 1 || report->field[0]->report_count != 1)
789 return -ENODEV;
790
791 data->lcd_brightness = bdev->props.brightness & 0x0ff;
792 data->lcd_power = bdev->props.power;
793 spin_lock_irqsave(&data->lock, flags);
794 hid_set_field(report->field[0], 0, data->lcd_power == FB_BLANK_UNBLANK ? data->lcd_brightness : 0);
795 usbhid_submit_report(data->hdev, report, USB_DIR_OUT);
796 spin_unlock_irqrestore(&data->lock, flags);
797 return 0;
798}
799
800static int picolcd_check_bl_fb(struct backlight_device *bdev, struct fb_info *fb)
801{
802 return fb && fb == picolcd_fbinfo((struct picolcd_data *)bl_get_data(bdev));
803}
804
805static const struct backlight_ops picolcd_blops = {
806 .update_status = picolcd_set_brightness,
807 .get_brightness = picolcd_get_brightness,
808 .check_fb = picolcd_check_bl_fb,
809};
810
811static int picolcd_init_backlight(struct picolcd_data *data, struct hid_report *report)
812{
813 struct device *dev = &data->hdev->dev;
814 struct backlight_device *bdev;
815 struct backlight_properties props;
816 if (!report)
817 return -ENODEV;
818 if (report->maxfield != 1 || report->field[0]->report_count != 1 ||
819 report->field[0]->report_size != 8) {
820 dev_err(dev, "unsupported BRIGHTNESS report");
821 return -EINVAL;
822 }
823
824 memset(&props, 0, sizeof(props));
825 props.max_brightness = 0xff;
826 bdev = backlight_device_register(dev_name(dev), dev, data,
827 &picolcd_blops, &props);
828 if (IS_ERR(bdev)) {
829 dev_err(dev, "failed to register backlight\n");
830 return PTR_ERR(bdev);
831 }
832 bdev->props.brightness = 0xff;
833 data->lcd_brightness = 0xff;
834 data->backlight = bdev;
835 picolcd_set_brightness(bdev);
836 return 0;
837}
838
839static void picolcd_exit_backlight(struct picolcd_data *data)
840{
841 struct backlight_device *bdev = data->backlight;
842
843 data->backlight = NULL;
844 if (bdev)
845 backlight_device_unregister(bdev);
846}
847
848static inline int picolcd_resume_backlight(struct picolcd_data *data)
849{
850 if (!data->backlight)
851 return 0;
852 return picolcd_set_brightness(data->backlight);
853}
854
Bruno Prémont9d71ea02010-05-02 16:05:05 +0200855#ifdef CONFIG_PM
856static void picolcd_suspend_backlight(struct picolcd_data *data)
857{
858 int bl_power = data->lcd_power;
859 if (!data->backlight)
860 return;
861
862 data->backlight->props.power = FB_BLANK_POWERDOWN;
863 picolcd_set_brightness(data->backlight);
864 data->lcd_power = data->backlight->props.power = bl_power;
865}
866#endif /* CONFIG_PM */
Bruno Prémontf1c21762010-03-30 22:35:27 +0200867#else
868static inline int picolcd_init_backlight(struct picolcd_data *data,
869 struct hid_report *report)
870{
871 return 0;
872}
873static inline void picolcd_exit_backlight(struct picolcd_data *data)
874{
875}
876static inline int picolcd_resume_backlight(struct picolcd_data *data)
877{
878 return 0;
879}
Bruno Prémont9d71ea02010-05-02 16:05:05 +0200880static inline void picolcd_suspend_backlight(struct picolcd_data *data)
881{
882}
Bruno Prémont5435f282010-04-11 12:17:45 +0200883#endif /* CONFIG_HID_PICOLCD_BACKLIGHT */
Bruno Prémontf1c21762010-03-30 22:35:27 +0200884
Bruno Prémont5435f282010-04-11 12:17:45 +0200885#ifdef CONFIG_HID_PICOLCD_LCD
Bruno Prémonte8d931b2010-03-30 22:36:07 +0200886/*
887 * lcd class device
888 */
889static int picolcd_get_contrast(struct lcd_device *ldev)
890{
891 struct picolcd_data *data = lcd_get_data(ldev);
892 return data->lcd_contrast;
893}
894
895static int picolcd_set_contrast(struct lcd_device *ldev, int contrast)
896{
897 struct picolcd_data *data = lcd_get_data(ldev);
898 struct hid_report *report = picolcd_out_report(REPORT_CONTRAST, data->hdev);
899 unsigned long flags;
900
901 if (!report || report->maxfield != 1 || report->field[0]->report_count != 1)
902 return -ENODEV;
903
904 data->lcd_contrast = contrast & 0x0ff;
905 spin_lock_irqsave(&data->lock, flags);
906 hid_set_field(report->field[0], 0, data->lcd_contrast);
907 usbhid_submit_report(data->hdev, report, USB_DIR_OUT);
908 spin_unlock_irqrestore(&data->lock, flags);
909 return 0;
910}
911
912static int picolcd_check_lcd_fb(struct lcd_device *ldev, struct fb_info *fb)
913{
914 return fb && fb == picolcd_fbinfo((struct picolcd_data *)lcd_get_data(ldev));
915}
916
917static struct lcd_ops picolcd_lcdops = {
918 .get_contrast = picolcd_get_contrast,
919 .set_contrast = picolcd_set_contrast,
920 .check_fb = picolcd_check_lcd_fb,
921};
922
923static int picolcd_init_lcd(struct picolcd_data *data, struct hid_report *report)
924{
925 struct device *dev = &data->hdev->dev;
926 struct lcd_device *ldev;
927
928 if (!report)
929 return -ENODEV;
930 if (report->maxfield != 1 || report->field[0]->report_count != 1 ||
931 report->field[0]->report_size != 8) {
932 dev_err(dev, "unsupported CONTRAST report");
933 return -EINVAL;
934 }
935
936 ldev = lcd_device_register(dev_name(dev), dev, data, &picolcd_lcdops);
937 if (IS_ERR(ldev)) {
938 dev_err(dev, "failed to register LCD\n");
939 return PTR_ERR(ldev);
940 }
941 ldev->props.max_contrast = 0x0ff;
942 data->lcd_contrast = 0xe5;
943 data->lcd = ldev;
944 picolcd_set_contrast(ldev, 0xe5);
945 return 0;
946}
947
948static void picolcd_exit_lcd(struct picolcd_data *data)
949{
950 struct lcd_device *ldev = data->lcd;
951
952 data->lcd = NULL;
953 if (ldev)
954 lcd_device_unregister(ldev);
955}
956
957static inline int picolcd_resume_lcd(struct picolcd_data *data)
958{
959 if (!data->lcd)
960 return 0;
961 return picolcd_set_contrast(data->lcd, data->lcd_contrast);
962}
963#else
964static inline int picolcd_init_lcd(struct picolcd_data *data,
965 struct hid_report *report)
966{
967 return 0;
968}
969static inline void picolcd_exit_lcd(struct picolcd_data *data)
970{
971}
972static inline int picolcd_resume_lcd(struct picolcd_data *data)
973{
974 return 0;
975}
Bruno Prémont5435f282010-04-11 12:17:45 +0200976#endif /* CONFIG_HID_PICOLCD_LCD */
Bruno Prémonte8d931b2010-03-30 22:36:07 +0200977
Bruno Prémont5435f282010-04-11 12:17:45 +0200978#ifdef CONFIG_HID_PICOLCD_LEDS
Bruno Prémont467d6522010-03-30 22:36:49 +0200979/**
980 * LED class device
981 */
982static void picolcd_leds_set(struct picolcd_data *data)
983{
984 struct hid_report *report;
985 unsigned long flags;
986
987 if (!data->led[0])
988 return;
989 report = picolcd_out_report(REPORT_LED_STATE, data->hdev);
990 if (!report || report->maxfield != 1 || report->field[0]->report_count != 1)
991 return;
992
993 spin_lock_irqsave(&data->lock, flags);
994 hid_set_field(report->field[0], 0, data->led_state);
995 usbhid_submit_report(data->hdev, report, USB_DIR_OUT);
996 spin_unlock_irqrestore(&data->lock, flags);
997}
998
999static void picolcd_led_set_brightness(struct led_classdev *led_cdev,
1000 enum led_brightness value)
1001{
1002 struct device *dev;
1003 struct hid_device *hdev;
1004 struct picolcd_data *data;
1005 int i, state = 0;
1006
1007 dev = led_cdev->dev->parent;
1008 hdev = container_of(dev, struct hid_device, dev);
1009 data = hid_get_drvdata(hdev);
1010 for (i = 0; i < 8; i++) {
1011 if (led_cdev != data->led[i])
1012 continue;
1013 state = (data->led_state >> i) & 1;
1014 if (value == LED_OFF && state) {
1015 data->led_state &= ~(1 << i);
1016 picolcd_leds_set(data);
1017 } else if (value != LED_OFF && !state) {
1018 data->led_state |= 1 << i;
1019 picolcd_leds_set(data);
1020 }
1021 break;
1022 }
1023}
1024
1025static enum led_brightness picolcd_led_get_brightness(struct led_classdev *led_cdev)
1026{
1027 struct device *dev;
1028 struct hid_device *hdev;
1029 struct picolcd_data *data;
1030 int i, value = 0;
1031
1032 dev = led_cdev->dev->parent;
1033 hdev = container_of(dev, struct hid_device, dev);
1034 data = hid_get_drvdata(hdev);
1035 for (i = 0; i < 8; i++)
1036 if (led_cdev == data->led[i]) {
1037 value = (data->led_state >> i) & 1;
1038 break;
1039 }
1040 return value ? LED_FULL : LED_OFF;
1041}
1042
1043static int picolcd_init_leds(struct picolcd_data *data, struct hid_report *report)
1044{
1045 struct device *dev = &data->hdev->dev;
1046 struct led_classdev *led;
1047 size_t name_sz = strlen(dev_name(dev)) + 8;
1048 char *name;
1049 int i, ret = 0;
1050
1051 if (!report)
1052 return -ENODEV;
1053 if (report->maxfield != 1 || report->field[0]->report_count != 1 ||
1054 report->field[0]->report_size != 8) {
1055 dev_err(dev, "unsupported LED_STATE report");
1056 return -EINVAL;
1057 }
1058
1059 for (i = 0; i < 8; i++) {
1060 led = kzalloc(sizeof(struct led_classdev)+name_sz, GFP_KERNEL);
1061 if (!led) {
1062 dev_err(dev, "can't allocate memory for LED %d\n", i);
1063 ret = -ENOMEM;
1064 goto err;
1065 }
1066 name = (void *)(&led[1]);
1067 snprintf(name, name_sz, "%s::GPO%d", dev_name(dev), i);
1068 led->name = name;
1069 led->brightness = 0;
1070 led->max_brightness = 1;
1071 led->brightness_get = picolcd_led_get_brightness;
1072 led->brightness_set = picolcd_led_set_brightness;
1073
1074 data->led[i] = led;
1075 ret = led_classdev_register(dev, data->led[i]);
1076 if (ret) {
1077 data->led[i] = NULL;
1078 kfree(led);
1079 dev_err(dev, "can't register LED %d\n", i);
1080 goto err;
1081 }
1082 }
1083 return 0;
1084err:
1085 for (i = 0; i < 8; i++)
1086 if (data->led[i]) {
1087 led = data->led[i];
1088 data->led[i] = NULL;
1089 led_classdev_unregister(led);
1090 kfree(led);
1091 }
1092 return ret;
1093}
1094
1095static void picolcd_exit_leds(struct picolcd_data *data)
1096{
1097 struct led_classdev *led;
1098 int i;
1099
1100 for (i = 0; i < 8; i++) {
1101 led = data->led[i];
1102 data->led[i] = NULL;
1103 if (!led)
1104 continue;
1105 led_classdev_unregister(led);
1106 kfree(led);
1107 }
1108}
1109
1110#else
1111static inline int picolcd_init_leds(struct picolcd_data *data,
1112 struct hid_report *report)
1113{
1114 return 0;
1115}
Bruno Prémont9d71ea02010-05-02 16:05:05 +02001116static inline void picolcd_exit_leds(struct picolcd_data *data)
Bruno Prémont467d6522010-03-30 22:36:49 +02001117{
1118}
1119static inline int picolcd_leds_set(struct picolcd_data *data)
1120{
1121 return 0;
1122}
Bruno Prémont5435f282010-04-11 12:17:45 +02001123#endif /* CONFIG_HID_PICOLCD_LEDS */
Bruno Prémont467d6522010-03-30 22:36:49 +02001124
Bruno Prémont236db472010-03-30 22:33:50 +02001125/*
1126 * input class device
1127 */
1128static int picolcd_raw_keypad(struct picolcd_data *data,
1129 struct hid_report *report, u8 *raw_data, int size)
1130{
1131 /*
1132 * Keypad event
1133 * First and second data bytes list currently pressed keys,
1134 * 0x00 means no key and at most 2 keys may be pressed at same time
1135 */
1136 int i, j;
1137
1138 /* determine newly pressed keys */
1139 for (i = 0; i < size; i++) {
1140 unsigned int key_code;
1141 if (raw_data[i] == 0)
1142 continue;
1143 for (j = 0; j < sizeof(data->pressed_keys); j++)
1144 if (data->pressed_keys[j] == raw_data[i])
1145 goto key_already_down;
1146 for (j = 0; j < sizeof(data->pressed_keys); j++)
1147 if (data->pressed_keys[j] == 0) {
1148 data->pressed_keys[j] = raw_data[i];
1149 break;
1150 }
1151 input_event(data->input_keys, EV_MSC, MSC_SCAN, raw_data[i]);
1152 if (raw_data[i] < PICOLCD_KEYS)
1153 key_code = data->keycode[raw_data[i]];
1154 else
1155 key_code = KEY_UNKNOWN;
1156 if (key_code != KEY_UNKNOWN) {
1157 dbg_hid(PICOLCD_NAME " got key press for %u:%d",
1158 raw_data[i], key_code);
1159 input_report_key(data->input_keys, key_code, 1);
1160 }
1161 input_sync(data->input_keys);
1162key_already_down:
1163 continue;
1164 }
1165
1166 /* determine newly released keys */
1167 for (j = 0; j < sizeof(data->pressed_keys); j++) {
1168 unsigned int key_code;
1169 if (data->pressed_keys[j] == 0)
1170 continue;
1171 for (i = 0; i < size; i++)
1172 if (data->pressed_keys[j] == raw_data[i])
1173 goto key_still_down;
1174 input_event(data->input_keys, EV_MSC, MSC_SCAN, data->pressed_keys[j]);
1175 if (data->pressed_keys[j] < PICOLCD_KEYS)
1176 key_code = data->keycode[data->pressed_keys[j]];
1177 else
1178 key_code = KEY_UNKNOWN;
1179 if (key_code != KEY_UNKNOWN) {
1180 dbg_hid(PICOLCD_NAME " got key release for %u:%d",
1181 data->pressed_keys[j], key_code);
1182 input_report_key(data->input_keys, key_code, 0);
1183 }
1184 input_sync(data->input_keys);
1185 data->pressed_keys[j] = 0;
1186key_still_down:
1187 continue;
1188 }
1189 return 1;
1190}
1191
1192static int picolcd_raw_cir(struct picolcd_data *data,
1193 struct hid_report *report, u8 *raw_data, int size)
1194{
1195 /* Need understanding of CIR data format to implement ... */
1196 return 1;
1197}
1198
1199static int picolcd_check_version(struct hid_device *hdev)
1200{
1201 struct picolcd_data *data = hid_get_drvdata(hdev);
1202 struct picolcd_pending *verinfo;
1203 int ret = 0;
1204
1205 if (!data)
1206 return -ENODEV;
1207
1208 verinfo = picolcd_send_and_wait(hdev, REPORT_VERSION, NULL, 0);
1209 if (!verinfo) {
1210 dev_err(&hdev->dev, "no version response from PicoLCD");
1211 return -ENODEV;
1212 }
1213
1214 if (verinfo->raw_size == 2) {
Bruno Prémont76d17e62010-04-25 21:31:40 +02001215 data->version[0] = verinfo->raw_data[1];
1216 data->version[1] = verinfo->raw_data[0];
Bruno Prémont236db472010-03-30 22:33:50 +02001217 if (data->status & PICOLCD_BOOTLOADER) {
1218 dev_info(&hdev->dev, "PicoLCD, bootloader version %d.%d\n",
Bruno Prémont76d17e62010-04-25 21:31:40 +02001219 verinfo->raw_data[1], verinfo->raw_data[0]);
Bruno Prémont236db472010-03-30 22:33:50 +02001220 } else {
1221 dev_info(&hdev->dev, "PicoLCD, firmware version %d.%d\n",
1222 verinfo->raw_data[1], verinfo->raw_data[0]);
Bruno Prémont236db472010-03-30 22:33:50 +02001223 }
1224 } else {
1225 dev_err(&hdev->dev, "confused, got unexpected version response from PicoLCD\n");
1226 ret = -EINVAL;
1227 }
1228 kfree(verinfo);
1229 return ret;
1230}
1231
1232/*
1233 * Reset our device and wait for answer to VERSION request
1234 */
1235static int picolcd_reset(struct hid_device *hdev)
1236{
1237 struct picolcd_data *data = hid_get_drvdata(hdev);
1238 struct hid_report *report = picolcd_out_report(REPORT_RESET, hdev);
1239 unsigned long flags;
Bruno Prémontb8c21cf2010-03-30 22:34:30 +02001240 int error;
Bruno Prémont236db472010-03-30 22:33:50 +02001241
1242 if (!data || !report || report->maxfield != 1)
1243 return -ENODEV;
1244
1245 spin_lock_irqsave(&data->lock, flags);
1246 if (hdev->product == USB_DEVICE_ID_PICOLCD_BOOTLOADER)
1247 data->status |= PICOLCD_BOOTLOADER;
1248
1249 /* perform the reset */
1250 hid_set_field(report->field[0], 0, 1);
1251 usbhid_submit_report(hdev, report, USB_DIR_OUT);
1252 spin_unlock_irqrestore(&data->lock, flags);
1253
Bruno Prémontb8c21cf2010-03-30 22:34:30 +02001254 error = picolcd_check_version(hdev);
1255 if (error)
1256 return error;
1257
Bruno Prémonte8d931b2010-03-30 22:36:07 +02001258 picolcd_resume_lcd(data);
Bruno Prémontf1c21762010-03-30 22:35:27 +02001259 picolcd_resume_backlight(data);
Bruno Prémont5435f282010-04-11 12:17:45 +02001260#ifdef CONFIG_HID_PICOLCD_FB
Bruno Prémontb8c21cf2010-03-30 22:34:30 +02001261 if (data->fb_info)
1262 schedule_delayed_work(&data->fb_info->deferred_work, 0);
Bruno Prémont5435f282010-04-11 12:17:45 +02001263#endif /* CONFIG_HID_PICOLCD_FB */
Bruno Prémontb8c21cf2010-03-30 22:34:30 +02001264
Bruno Prémont467d6522010-03-30 22:36:49 +02001265 picolcd_leds_set(data);
Bruno Prémontb8c21cf2010-03-30 22:34:30 +02001266 return 0;
Bruno Prémont236db472010-03-30 22:33:50 +02001267}
1268
1269/*
1270 * The "operation_mode" sysfs attribute
1271 */
1272static ssize_t picolcd_operation_mode_show(struct device *dev,
1273 struct device_attribute *attr, char *buf)
1274{
1275 struct picolcd_data *data = dev_get_drvdata(dev);
1276
1277 if (data->status & PICOLCD_BOOTLOADER)
1278 return snprintf(buf, PAGE_SIZE, "[bootloader] lcd\n");
1279 else
1280 return snprintf(buf, PAGE_SIZE, "bootloader [lcd]\n");
1281}
1282
1283static ssize_t picolcd_operation_mode_store(struct device *dev,
1284 struct device_attribute *attr, const char *buf, size_t count)
1285{
1286 struct picolcd_data *data = dev_get_drvdata(dev);
1287 struct hid_report *report = NULL;
1288 size_t cnt = count;
Bruno Prémont0b5adf92010-04-25 21:29:16 +02001289 int timeout = data->opmode_delay;
Bruno Prémont236db472010-03-30 22:33:50 +02001290 unsigned long flags;
1291
1292 if (cnt >= 3 && strncmp("lcd", buf, 3) == 0) {
1293 if (data->status & PICOLCD_BOOTLOADER)
1294 report = picolcd_out_report(REPORT_EXIT_FLASHER, data->hdev);
1295 buf += 3;
1296 cnt -= 3;
1297 } else if (cnt >= 10 && strncmp("bootloader", buf, 10) == 0) {
1298 if (!(data->status & PICOLCD_BOOTLOADER))
1299 report = picolcd_out_report(REPORT_EXIT_KEYBOARD, data->hdev);
1300 buf += 10;
1301 cnt -= 10;
1302 }
1303 if (!report)
1304 return -EINVAL;
1305
Bruno Prémont236db472010-03-30 22:33:50 +02001306 while (cnt > 0 && (buf[cnt-1] == '\n' || buf[cnt-1] == '\r'))
1307 cnt--;
Bruno Prémont0b5adf92010-04-25 21:29:16 +02001308 if (cnt != 0)
1309 return -EINVAL;
Bruno Prémont236db472010-03-30 22:33:50 +02001310
1311 spin_lock_irqsave(&data->lock, flags);
1312 hid_set_field(report->field[0], 0, timeout & 0xff);
1313 hid_set_field(report->field[0], 1, (timeout >> 8) & 0xff);
1314 usbhid_submit_report(data->hdev, report, USB_DIR_OUT);
1315 spin_unlock_irqrestore(&data->lock, flags);
1316 return count;
1317}
1318
1319static DEVICE_ATTR(operation_mode, 0644, picolcd_operation_mode_show,
1320 picolcd_operation_mode_store);
1321
Bruno Prémont0b5adf92010-04-25 21:29:16 +02001322/*
1323 * The "operation_mode_delay" sysfs attribute
1324 */
1325static ssize_t picolcd_operation_mode_delay_show(struct device *dev,
1326 struct device_attribute *attr, char *buf)
1327{
1328 struct picolcd_data *data = dev_get_drvdata(dev);
1329
1330 return snprintf(buf, PAGE_SIZE, "%hu\n", data->opmode_delay);
1331}
1332
1333static ssize_t picolcd_operation_mode_delay_store(struct device *dev,
1334 struct device_attribute *attr, const char *buf, size_t count)
1335{
1336 struct picolcd_data *data = dev_get_drvdata(dev);
1337 unsigned u;
1338 if (sscanf(buf, "%u", &u) != 1)
1339 return -EINVAL;
1340 if (u > 30000)
1341 return -EINVAL;
1342 else
1343 data->opmode_delay = u;
1344 return count;
1345}
1346
1347static DEVICE_ATTR(operation_mode_delay, 0644, picolcd_operation_mode_delay_show,
1348 picolcd_operation_mode_delay_store);
1349
Bruno Prémont236db472010-03-30 22:33:50 +02001350
1351#ifdef CONFIG_DEBUG_FS
1352/*
Bruno Prémont9bbf2b92010-03-30 22:38:09 +02001353 * The "reset" file
1354 */
1355static int picolcd_debug_reset_show(struct seq_file *f, void *p)
1356{
1357 if (picolcd_fbinfo((struct picolcd_data *)f->private))
1358 seq_printf(f, "all fb\n");
1359 else
1360 seq_printf(f, "all\n");
1361 return 0;
1362}
1363
1364static int picolcd_debug_reset_open(struct inode *inode, struct file *f)
1365{
1366 return single_open(f, picolcd_debug_reset_show, inode->i_private);
1367}
1368
1369static ssize_t picolcd_debug_reset_write(struct file *f, const char __user *user_buf,
1370 size_t count, loff_t *ppos)
1371{
1372 struct picolcd_data *data = ((struct seq_file *)f->private_data)->private;
1373 char buf[32];
1374 size_t cnt = min(count, sizeof(buf)-1);
1375 if (copy_from_user(buf, user_buf, cnt))
1376 return -EFAULT;
1377
1378 while (cnt > 0 && (buf[cnt-1] == ' ' || buf[cnt-1] == '\n'))
1379 cnt--;
1380 buf[cnt] = '\0';
1381 if (strcmp(buf, "all") == 0) {
1382 picolcd_reset(data->hdev);
1383 picolcd_fb_reset(data, 1);
1384 } else if (strcmp(buf, "fb") == 0) {
1385 picolcd_fb_reset(data, 1);
1386 } else {
1387 return -EINVAL;
1388 }
1389 return count;
1390}
1391
1392static const struct file_operations picolcd_debug_reset_fops = {
1393 .owner = THIS_MODULE,
1394 .open = picolcd_debug_reset_open,
1395 .read = seq_read,
1396 .llseek = seq_lseek,
1397 .write = picolcd_debug_reset_write,
1398 .release = single_release,
1399};
1400
1401/*
1402 * The "eeprom" file
1403 */
1404static int picolcd_debug_eeprom_open(struct inode *i, struct file *f)
1405{
1406 f->private_data = i->i_private;
1407 return 0;
1408}
1409
1410static ssize_t picolcd_debug_eeprom_read(struct file *f, char __user *u,
1411 size_t s, loff_t *off)
1412{
1413 struct picolcd_data *data = f->private_data;
1414 struct picolcd_pending *resp;
1415 u8 raw_data[3];
1416 ssize_t ret = -EIO;
1417
1418 if (s == 0)
1419 return -EINVAL;
1420 if (*off > 0x0ff)
1421 return 0;
1422
1423 /* prepare buffer with info about what we want to read (addr & len) */
1424 raw_data[0] = *off & 0xff;
1425 raw_data[1] = (*off >> 8) && 0xff;
1426 raw_data[2] = s < 20 ? s : 20;
1427 if (*off + raw_data[2] > 0xff)
1428 raw_data[2] = 0x100 - *off;
1429 resp = picolcd_send_and_wait(data->hdev, REPORT_EE_READ, raw_data,
1430 sizeof(raw_data));
1431 if (!resp)
1432 return -EIO;
1433
1434 if (resp->in_report && resp->in_report->id == REPORT_EE_DATA) {
1435 /* successful read :) */
1436 ret = resp->raw_data[2];
1437 if (ret > s)
1438 ret = s;
1439 if (copy_to_user(u, resp->raw_data+3, ret))
1440 ret = -EFAULT;
1441 else
1442 *off += ret;
1443 } /* anything else is some kind of IO error */
1444
1445 kfree(resp);
1446 return ret;
1447}
1448
1449static ssize_t picolcd_debug_eeprom_write(struct file *f, const char __user *u,
1450 size_t s, loff_t *off)
1451{
1452 struct picolcd_data *data = f->private_data;
1453 struct picolcd_pending *resp;
1454 ssize_t ret = -EIO;
1455 u8 raw_data[23];
1456
1457 if (s == 0)
1458 return -EINVAL;
1459 if (*off > 0x0ff)
1460 return -ENOSPC;
1461
1462 memset(raw_data, 0, sizeof(raw_data));
1463 raw_data[0] = *off & 0xff;
1464 raw_data[1] = (*off >> 8) && 0xff;
1465 raw_data[2] = s < 20 ? s : 20;
1466 if (*off + raw_data[2] > 0xff)
1467 raw_data[2] = 0x100 - *off;
1468
1469 if (copy_from_user(raw_data+3, u, raw_data[2]))
1470 return -EFAULT;
1471 resp = picolcd_send_and_wait(data->hdev, REPORT_EE_WRITE, raw_data,
1472 sizeof(raw_data));
1473
1474 if (!resp)
1475 return -EIO;
1476
1477 if (resp->in_report && resp->in_report->id == REPORT_EE_DATA) {
1478 /* check if written data matches */
1479 if (memcmp(raw_data, resp->raw_data, 3+raw_data[2]) == 0) {
1480 *off += raw_data[2];
1481 ret = raw_data[2];
1482 }
1483 }
1484 kfree(resp);
1485 return ret;
1486}
1487
1488/*
1489 * Notes:
1490 * - read/write happens in chunks of at most 20 bytes, it's up to userspace
1491 * to loop in order to get more data.
1492 * - on write errors on otherwise correct write request the bytes
1493 * that should have been written are in undefined state.
1494 */
1495static const struct file_operations picolcd_debug_eeprom_fops = {
1496 .owner = THIS_MODULE,
1497 .open = picolcd_debug_eeprom_open,
1498 .read = picolcd_debug_eeprom_read,
1499 .write = picolcd_debug_eeprom_write,
1500 .llseek = generic_file_llseek,
1501};
1502
1503/*
1504 * The "flash" file
1505 */
1506static int picolcd_debug_flash_open(struct inode *i, struct file *f)
1507{
1508 f->private_data = i->i_private;
1509 return 0;
1510}
1511
1512/* record a flash address to buf (bounds check to be done by caller) */
1513static int _picolcd_flash_setaddr(struct picolcd_data *data, u8 *buf, long off)
1514{
1515 buf[0] = off & 0xff;
1516 buf[1] = (off >> 8) & 0xff;
1517 if (data->addr_sz == 3)
1518 buf[2] = (off >> 16) & 0xff;
1519 return data->addr_sz == 2 ? 2 : 3;
1520}
1521
1522/* read a given size of data (bounds check to be done by caller) */
1523static ssize_t _picolcd_flash_read(struct picolcd_data *data, int report_id,
1524 char __user *u, size_t s, loff_t *off)
1525{
1526 struct picolcd_pending *resp;
1527 u8 raw_data[4];
1528 ssize_t ret = 0;
1529 int len_off, err = -EIO;
1530
1531 while (s > 0) {
1532 err = -EIO;
1533 len_off = _picolcd_flash_setaddr(data, raw_data, *off);
1534 raw_data[len_off] = s > 32 ? 32 : s;
1535 resp = picolcd_send_and_wait(data->hdev, report_id, raw_data, len_off+1);
1536 if (!resp || !resp->in_report)
1537 goto skip;
1538 if (resp->in_report->id == REPORT_MEMORY ||
1539 resp->in_report->id == REPORT_BL_READ_MEMORY) {
1540 if (memcmp(raw_data, resp->raw_data, len_off+1) != 0)
1541 goto skip;
1542 if (copy_to_user(u+ret, resp->raw_data+len_off+1, raw_data[len_off])) {
1543 err = -EFAULT;
1544 goto skip;
1545 }
1546 *off += raw_data[len_off];
1547 s -= raw_data[len_off];
1548 ret += raw_data[len_off];
1549 err = 0;
1550 }
1551skip:
1552 kfree(resp);
1553 if (err)
1554 return ret > 0 ? ret : err;
1555 }
1556 return ret;
1557}
1558
1559static ssize_t picolcd_debug_flash_read(struct file *f, char __user *u,
1560 size_t s, loff_t *off)
1561{
1562 struct picolcd_data *data = f->private_data;
1563
1564 if (s == 0)
1565 return -EINVAL;
1566 if (*off > 0x05fff)
1567 return 0;
1568 if (*off + s > 0x05fff)
1569 s = 0x06000 - *off;
1570
1571 if (data->status & PICOLCD_BOOTLOADER)
1572 return _picolcd_flash_read(data, REPORT_BL_READ_MEMORY, u, s, off);
1573 else
1574 return _picolcd_flash_read(data, REPORT_READ_MEMORY, u, s, off);
1575}
1576
1577/* erase block aligned to 64bytes boundary */
1578static ssize_t _picolcd_flash_erase64(struct picolcd_data *data, int report_id,
1579 loff_t *off)
1580{
1581 struct picolcd_pending *resp;
1582 u8 raw_data[3];
1583 int len_off;
1584 ssize_t ret = -EIO;
1585
1586 if (*off & 0x3f)
1587 return -EINVAL;
1588
1589 len_off = _picolcd_flash_setaddr(data, raw_data, *off);
1590 resp = picolcd_send_and_wait(data->hdev, report_id, raw_data, len_off);
1591 if (!resp || !resp->in_report)
1592 goto skip;
1593 if (resp->in_report->id == REPORT_MEMORY ||
1594 resp->in_report->id == REPORT_BL_ERASE_MEMORY) {
1595 if (memcmp(raw_data, resp->raw_data, len_off) != 0)
1596 goto skip;
1597 ret = 0;
1598 }
1599skip:
1600 kfree(resp);
1601 return ret;
1602}
1603
1604/* write a given size of data (bounds check to be done by caller) */
1605static ssize_t _picolcd_flash_write(struct picolcd_data *data, int report_id,
1606 const char __user *u, size_t s, loff_t *off)
1607{
1608 struct picolcd_pending *resp;
1609 u8 raw_data[36];
1610 ssize_t ret = 0;
1611 int len_off, err = -EIO;
1612
1613 while (s > 0) {
1614 err = -EIO;
1615 len_off = _picolcd_flash_setaddr(data, raw_data, *off);
1616 raw_data[len_off] = s > 32 ? 32 : s;
1617 if (copy_from_user(raw_data+len_off+1, u, raw_data[len_off])) {
1618 err = -EFAULT;
Julia Lawallaeacb6f2010-05-15 11:46:36 +02001619 break;
Bruno Prémont9bbf2b92010-03-30 22:38:09 +02001620 }
1621 resp = picolcd_send_and_wait(data->hdev, report_id, raw_data,
1622 len_off+1+raw_data[len_off]);
1623 if (!resp || !resp->in_report)
1624 goto skip;
1625 if (resp->in_report->id == REPORT_MEMORY ||
1626 resp->in_report->id == REPORT_BL_WRITE_MEMORY) {
1627 if (memcmp(raw_data, resp->raw_data, len_off+1+raw_data[len_off]) != 0)
1628 goto skip;
1629 *off += raw_data[len_off];
1630 s -= raw_data[len_off];
1631 ret += raw_data[len_off];
1632 err = 0;
1633 }
1634skip:
1635 kfree(resp);
1636 if (err)
1637 break;
1638 }
1639 return ret > 0 ? ret : err;
1640}
1641
1642static ssize_t picolcd_debug_flash_write(struct file *f, const char __user *u,
1643 size_t s, loff_t *off)
1644{
1645 struct picolcd_data *data = f->private_data;
1646 ssize_t err, ret = 0;
1647 int report_erase, report_write;
1648
1649 if (s == 0)
1650 return -EINVAL;
1651 if (*off > 0x5fff)
1652 return -ENOSPC;
1653 if (s & 0x3f)
1654 return -EINVAL;
1655 if (*off & 0x3f)
1656 return -EINVAL;
1657
1658 if (data->status & PICOLCD_BOOTLOADER) {
1659 report_erase = REPORT_BL_ERASE_MEMORY;
1660 report_write = REPORT_BL_WRITE_MEMORY;
1661 } else {
1662 report_erase = REPORT_ERASE_MEMORY;
1663 report_write = REPORT_WRITE_MEMORY;
1664 }
1665 mutex_lock(&data->mutex_flash);
1666 while (s > 0) {
1667 err = _picolcd_flash_erase64(data, report_erase, off);
1668 if (err)
1669 break;
1670 err = _picolcd_flash_write(data, report_write, u, 64, off);
1671 if (err < 0)
1672 break;
1673 ret += err;
1674 *off += err;
1675 s -= err;
1676 if (err != 64)
1677 break;
1678 }
1679 mutex_unlock(&data->mutex_flash);
1680 return ret > 0 ? ret : err;
1681}
1682
1683/*
1684 * Notes:
1685 * - concurrent writing is prevented by mutex and all writes must be
1686 * n*64 bytes and 64-byte aligned, each write being preceeded by an
1687 * ERASE which erases a 64byte block.
1688 * If less than requested was written or an error is returned for an
1689 * otherwise correct write request the next 64-byte block which should
1690 * have been written is in undefined state (mostly: original, erased,
1691 * (half-)written with write error)
1692 * - reading can happend without special restriction
1693 */
1694static const struct file_operations picolcd_debug_flash_fops = {
1695 .owner = THIS_MODULE,
1696 .open = picolcd_debug_flash_open,
1697 .read = picolcd_debug_flash_read,
1698 .write = picolcd_debug_flash_write,
1699 .llseek = generic_file_llseek,
1700};
1701
1702
1703/*
Bruno Prémont236db472010-03-30 22:33:50 +02001704 * Helper code for HID report level dumping/debugging
1705 */
1706static const char *error_codes[] = {
1707 "success", "parameter missing", "data_missing", "block readonly",
1708 "block not erasable", "block too big", "section overflow",
1709 "invalid command length", "invalid data length",
1710};
1711
1712static void dump_buff_as_hex(char *dst, size_t dst_sz, const u8 *data,
1713 const size_t data_len)
1714{
1715 int i, j;
1716 for (i = j = 0; i < data_len && j + 3 < dst_sz; i++) {
1717 dst[j++] = hex_asc[(data[i] >> 4) & 0x0f];
1718 dst[j++] = hex_asc[data[i] & 0x0f];
1719 dst[j++] = ' ';
1720 }
1721 if (j < dst_sz) {
1722 dst[j--] = '\0';
1723 dst[j] = '\n';
1724 } else
1725 dst[j] = '\0';
1726}
1727
1728static void picolcd_debug_out_report(struct picolcd_data *data,
1729 struct hid_device *hdev, struct hid_report *report)
1730{
1731 u8 raw_data[70];
1732 int raw_size = (report->size >> 3) + 1;
1733 char *buff;
1734#define BUFF_SZ 256
1735
1736 /* Avoid unnecessary overhead if debugfs is disabled */
1737 if (!hdev->debug_events)
1738 return;
1739
1740 buff = kmalloc(BUFF_SZ, GFP_ATOMIC);
1741 if (!buff)
1742 return;
1743
1744 snprintf(buff, BUFF_SZ, "\nout report %d (size %d) = ",
1745 report->id, raw_size);
1746 hid_debug_event(hdev, buff);
1747 if (raw_size + 5 > sizeof(raw_data)) {
1748 hid_debug_event(hdev, " TOO BIG\n");
1749 return;
1750 } else {
1751 raw_data[0] = report->id;
1752 hid_output_report(report, raw_data);
1753 dump_buff_as_hex(buff, BUFF_SZ, raw_data, raw_size);
1754 hid_debug_event(hdev, buff);
1755 }
1756
1757 switch (report->id) {
1758 case REPORT_LED_STATE:
1759 /* 1 data byte with GPO state */
1760 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1761 "REPORT_LED_STATE", report->id, raw_size-1);
1762 hid_debug_event(hdev, buff);
1763 snprintf(buff, BUFF_SZ, "\tGPO state: 0x%02x\n", raw_data[1]);
1764 hid_debug_event(hdev, buff);
1765 break;
1766 case REPORT_BRIGHTNESS:
1767 /* 1 data byte with brightness */
1768 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1769 "REPORT_BRIGHTNESS", report->id, raw_size-1);
1770 hid_debug_event(hdev, buff);
1771 snprintf(buff, BUFF_SZ, "\tBrightness: 0x%02x\n", raw_data[1]);
1772 hid_debug_event(hdev, buff);
1773 break;
1774 case REPORT_CONTRAST:
1775 /* 1 data byte with contrast */
1776 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1777 "REPORT_CONTRAST", report->id, raw_size-1);
1778 hid_debug_event(hdev, buff);
1779 snprintf(buff, BUFF_SZ, "\tContrast: 0x%02x\n", raw_data[1]);
1780 hid_debug_event(hdev, buff);
1781 break;
1782 case REPORT_RESET:
1783 /* 2 data bytes with reset duration in ms */
1784 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1785 "REPORT_RESET", report->id, raw_size-1);
1786 hid_debug_event(hdev, buff);
1787 snprintf(buff, BUFF_SZ, "\tDuration: 0x%02x%02x (%dms)\n",
1788 raw_data[2], raw_data[1], raw_data[2] << 8 | raw_data[1]);
1789 hid_debug_event(hdev, buff);
1790 break;
1791 case REPORT_LCD_CMD:
1792 /* 63 data bytes with LCD commands */
1793 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1794 "REPORT_LCD_CMD", report->id, raw_size-1);
1795 hid_debug_event(hdev, buff);
1796 /* TODO: format decoding */
1797 break;
1798 case REPORT_LCD_DATA:
1799 /* 63 data bytes with LCD data */
1800 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1801 "REPORT_LCD_CMD", report->id, raw_size-1);
1802 /* TODO: format decoding */
1803 hid_debug_event(hdev, buff);
1804 break;
1805 case REPORT_LCD_CMD_DATA:
1806 /* 63 data bytes with LCD commands and data */
1807 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1808 "REPORT_LCD_CMD", report->id, raw_size-1);
1809 /* TODO: format decoding */
1810 hid_debug_event(hdev, buff);
1811 break;
1812 case REPORT_EE_READ:
1813 /* 3 data bytes with read area description */
1814 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1815 "REPORT_EE_READ", report->id, raw_size-1);
1816 hid_debug_event(hdev, buff);
1817 snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x\n",
1818 raw_data[2], raw_data[1]);
1819 hid_debug_event(hdev, buff);
1820 snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[3]);
1821 hid_debug_event(hdev, buff);
1822 break;
1823 case REPORT_EE_WRITE:
1824 /* 3+1..20 data bytes with write area description */
1825 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1826 "REPORT_EE_WRITE", report->id, raw_size-1);
1827 hid_debug_event(hdev, buff);
1828 snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x\n",
1829 raw_data[2], raw_data[1]);
1830 hid_debug_event(hdev, buff);
1831 snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[3]);
1832 hid_debug_event(hdev, buff);
1833 if (raw_data[3] == 0) {
1834 snprintf(buff, BUFF_SZ, "\tNo data\n");
1835 } else if (raw_data[3] + 4 <= raw_size) {
1836 snprintf(buff, BUFF_SZ, "\tData: ");
1837 hid_debug_event(hdev, buff);
1838 dump_buff_as_hex(buff, BUFF_SZ, raw_data+4, raw_data[3]);
1839 } else {
1840 snprintf(buff, BUFF_SZ, "\tData overflowed\n");
1841 }
1842 hid_debug_event(hdev, buff);
1843 break;
1844 case REPORT_ERASE_MEMORY:
1845 case REPORT_BL_ERASE_MEMORY:
1846 /* 3 data bytes with pointer inside erase block */
1847 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1848 "REPORT_ERASE_MEMORY", report->id, raw_size-1);
1849 hid_debug_event(hdev, buff);
1850 switch (data->addr_sz) {
1851 case 2:
1852 snprintf(buff, BUFF_SZ, "\tAddress inside 64 byte block: 0x%02x%02x\n",
1853 raw_data[2], raw_data[1]);
1854 break;
1855 case 3:
1856 snprintf(buff, BUFF_SZ, "\tAddress inside 64 byte block: 0x%02x%02x%02x\n",
1857 raw_data[3], raw_data[2], raw_data[1]);
1858 break;
1859 default:
1860 snprintf(buff, BUFF_SZ, "\tNot supported\n");
1861 }
1862 hid_debug_event(hdev, buff);
1863 break;
1864 case REPORT_READ_MEMORY:
1865 case REPORT_BL_READ_MEMORY:
1866 /* 4 data bytes with read area description */
1867 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1868 "REPORT_READ_MEMORY", report->id, raw_size-1);
1869 hid_debug_event(hdev, buff);
1870 switch (data->addr_sz) {
1871 case 2:
1872 snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x\n",
1873 raw_data[2], raw_data[1]);
1874 hid_debug_event(hdev, buff);
1875 snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[3]);
1876 break;
1877 case 3:
1878 snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x%02x\n",
1879 raw_data[3], raw_data[2], raw_data[1]);
1880 hid_debug_event(hdev, buff);
1881 snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[4]);
1882 break;
1883 default:
1884 snprintf(buff, BUFF_SZ, "\tNot supported\n");
1885 }
1886 hid_debug_event(hdev, buff);
1887 break;
1888 case REPORT_WRITE_MEMORY:
1889 case REPORT_BL_WRITE_MEMORY:
1890 /* 4+1..32 data bytes with write adrea description */
1891 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1892 "REPORT_WRITE_MEMORY", report->id, raw_size-1);
1893 hid_debug_event(hdev, buff);
1894 switch (data->addr_sz) {
1895 case 2:
1896 snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x\n",
1897 raw_data[2], raw_data[1]);
1898 hid_debug_event(hdev, buff);
1899 snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[3]);
1900 hid_debug_event(hdev, buff);
1901 if (raw_data[3] == 0) {
1902 snprintf(buff, BUFF_SZ, "\tNo data\n");
1903 } else if (raw_data[3] + 4 <= raw_size) {
1904 snprintf(buff, BUFF_SZ, "\tData: ");
1905 hid_debug_event(hdev, buff);
1906 dump_buff_as_hex(buff, BUFF_SZ, raw_data+4, raw_data[3]);
1907 } else {
1908 snprintf(buff, BUFF_SZ, "\tData overflowed\n");
1909 }
1910 break;
1911 case 3:
1912 snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x%02x\n",
1913 raw_data[3], raw_data[2], raw_data[1]);
1914 hid_debug_event(hdev, buff);
1915 snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[4]);
1916 hid_debug_event(hdev, buff);
1917 if (raw_data[4] == 0) {
1918 snprintf(buff, BUFF_SZ, "\tNo data\n");
1919 } else if (raw_data[4] + 5 <= raw_size) {
1920 snprintf(buff, BUFF_SZ, "\tData: ");
1921 hid_debug_event(hdev, buff);
1922 dump_buff_as_hex(buff, BUFF_SZ, raw_data+5, raw_data[4]);
1923 } else {
1924 snprintf(buff, BUFF_SZ, "\tData overflowed\n");
1925 }
1926 break;
1927 default:
1928 snprintf(buff, BUFF_SZ, "\tNot supported\n");
1929 }
1930 hid_debug_event(hdev, buff);
1931 break;
1932 case REPORT_SPLASH_RESTART:
1933 /* TODO */
1934 break;
1935 case REPORT_EXIT_KEYBOARD:
1936 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1937 "REPORT_EXIT_KEYBOARD", report->id, raw_size-1);
1938 hid_debug_event(hdev, buff);
1939 snprintf(buff, BUFF_SZ, "\tRestart delay: %dms (0x%02x%02x)\n",
1940 raw_data[1] | (raw_data[2] << 8),
1941 raw_data[2], raw_data[1]);
1942 hid_debug_event(hdev, buff);
1943 break;
1944 case REPORT_VERSION:
1945 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1946 "REPORT_VERSION", report->id, raw_size-1);
1947 hid_debug_event(hdev, buff);
1948 break;
1949 case REPORT_DEVID:
1950 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1951 "REPORT_DEVID", report->id, raw_size-1);
1952 hid_debug_event(hdev, buff);
1953 break;
1954 case REPORT_SPLASH_SIZE:
1955 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1956 "REPORT_SPLASH_SIZE", report->id, raw_size-1);
1957 hid_debug_event(hdev, buff);
1958 break;
1959 case REPORT_HOOK_VERSION:
1960 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1961 "REPORT_HOOK_VERSION", report->id, raw_size-1);
1962 hid_debug_event(hdev, buff);
1963 break;
1964 case REPORT_EXIT_FLASHER:
1965 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1966 "REPORT_VERSION", report->id, raw_size-1);
1967 hid_debug_event(hdev, buff);
1968 snprintf(buff, BUFF_SZ, "\tRestart delay: %dms (0x%02x%02x)\n",
1969 raw_data[1] | (raw_data[2] << 8),
1970 raw_data[2], raw_data[1]);
1971 hid_debug_event(hdev, buff);
1972 break;
1973 default:
1974 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1975 "<unknown>", report->id, raw_size-1);
1976 hid_debug_event(hdev, buff);
1977 break;
1978 }
1979 wake_up_interruptible(&hdev->debug_wait);
1980 kfree(buff);
1981}
1982
1983static void picolcd_debug_raw_event(struct picolcd_data *data,
1984 struct hid_device *hdev, struct hid_report *report,
1985 u8 *raw_data, int size)
1986{
1987 char *buff;
1988
1989#define BUFF_SZ 256
1990 /* Avoid unnecessary overhead if debugfs is disabled */
1991 if (!hdev->debug_events)
1992 return;
1993
1994 buff = kmalloc(BUFF_SZ, GFP_ATOMIC);
1995 if (!buff)
1996 return;
1997
1998 switch (report->id) {
1999 case REPORT_ERROR_CODE:
2000 /* 2 data bytes with affected report and error code */
2001 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2002 "REPORT_ERROR_CODE", report->id, size-1);
2003 hid_debug_event(hdev, buff);
2004 if (raw_data[2] < ARRAY_SIZE(error_codes))
2005 snprintf(buff, BUFF_SZ, "\tError code 0x%02x (%s) in reply to report 0x%02x\n",
2006 raw_data[2], error_codes[raw_data[2]], raw_data[1]);
2007 else
2008 snprintf(buff, BUFF_SZ, "\tError code 0x%02x in reply to report 0x%02x\n",
2009 raw_data[2], raw_data[1]);
2010 hid_debug_event(hdev, buff);
2011 break;
2012 case REPORT_KEY_STATE:
2013 /* 2 data bytes with key state */
2014 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2015 "REPORT_KEY_STATE", report->id, size-1);
2016 hid_debug_event(hdev, buff);
2017 if (raw_data[1] == 0)
2018 snprintf(buff, BUFF_SZ, "\tNo key pressed\n");
2019 else if (raw_data[2] == 0)
2020 snprintf(buff, BUFF_SZ, "\tOne key pressed: 0x%02x (%d)\n",
2021 raw_data[1], raw_data[1]);
2022 else
2023 snprintf(buff, BUFF_SZ, "\tTwo keys pressed: 0x%02x (%d), 0x%02x (%d)\n",
2024 raw_data[1], raw_data[1], raw_data[2], raw_data[2]);
2025 hid_debug_event(hdev, buff);
2026 break;
2027 case REPORT_IR_DATA:
2028 /* Up to 20 byes of IR scancode data */
2029 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2030 "REPORT_IR_DATA", report->id, size-1);
2031 hid_debug_event(hdev, buff);
2032 if (raw_data[1] == 0) {
2033 snprintf(buff, BUFF_SZ, "\tUnexpectedly 0 data length\n");
2034 hid_debug_event(hdev, buff);
2035 } else if (raw_data[1] + 1 <= size) {
2036 snprintf(buff, BUFF_SZ, "\tData length: %d\n\tIR Data: ",
2037 raw_data[1]-1);
2038 hid_debug_event(hdev, buff);
2039 dump_buff_as_hex(buff, BUFF_SZ, raw_data+2, raw_data[1]-1);
2040 hid_debug_event(hdev, buff);
2041 } else {
2042 snprintf(buff, BUFF_SZ, "\tOverflowing data length: %d\n",
2043 raw_data[1]-1);
2044 hid_debug_event(hdev, buff);
2045 }
2046 break;
2047 case REPORT_EE_DATA:
2048 /* Data buffer in response to REPORT_EE_READ or REPORT_EE_WRITE */
2049 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2050 "REPORT_EE_DATA", report->id, size-1);
2051 hid_debug_event(hdev, buff);
2052 snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x\n",
2053 raw_data[2], raw_data[1]);
2054 hid_debug_event(hdev, buff);
2055 snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[3]);
2056 hid_debug_event(hdev, buff);
2057 if (raw_data[3] == 0) {
2058 snprintf(buff, BUFF_SZ, "\tNo data\n");
2059 hid_debug_event(hdev, buff);
2060 } else if (raw_data[3] + 4 <= size) {
2061 snprintf(buff, BUFF_SZ, "\tData: ");
2062 hid_debug_event(hdev, buff);
2063 dump_buff_as_hex(buff, BUFF_SZ, raw_data+4, raw_data[3]);
2064 hid_debug_event(hdev, buff);
2065 } else {
2066 snprintf(buff, BUFF_SZ, "\tData overflowed\n");
2067 hid_debug_event(hdev, buff);
2068 }
2069 break;
2070 case REPORT_MEMORY:
2071 /* Data buffer in response to REPORT_READ_MEMORY or REPORT_WRTIE_MEMORY */
2072 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2073 "REPORT_MEMORY", report->id, size-1);
2074 hid_debug_event(hdev, buff);
2075 switch (data->addr_sz) {
2076 case 2:
2077 snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x\n",
2078 raw_data[2], raw_data[1]);
2079 hid_debug_event(hdev, buff);
2080 snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[3]);
2081 hid_debug_event(hdev, buff);
2082 if (raw_data[3] == 0) {
2083 snprintf(buff, BUFF_SZ, "\tNo data\n");
2084 } else if (raw_data[3] + 4 <= size) {
2085 snprintf(buff, BUFF_SZ, "\tData: ");
2086 hid_debug_event(hdev, buff);
2087 dump_buff_as_hex(buff, BUFF_SZ, raw_data+4, raw_data[3]);
2088 } else {
2089 snprintf(buff, BUFF_SZ, "\tData overflowed\n");
2090 }
2091 break;
2092 case 3:
2093 snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x%02x\n",
2094 raw_data[3], raw_data[2], raw_data[1]);
2095 hid_debug_event(hdev, buff);
2096 snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[4]);
2097 hid_debug_event(hdev, buff);
2098 if (raw_data[4] == 0) {
2099 snprintf(buff, BUFF_SZ, "\tNo data\n");
2100 } else if (raw_data[4] + 5 <= size) {
2101 snprintf(buff, BUFF_SZ, "\tData: ");
2102 hid_debug_event(hdev, buff);
2103 dump_buff_as_hex(buff, BUFF_SZ, raw_data+5, raw_data[4]);
2104 } else {
2105 snprintf(buff, BUFF_SZ, "\tData overflowed\n");
2106 }
2107 break;
2108 default:
2109 snprintf(buff, BUFF_SZ, "\tNot supported\n");
2110 }
2111 hid_debug_event(hdev, buff);
2112 break;
2113 case REPORT_VERSION:
2114 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2115 "REPORT_VERSION", report->id, size-1);
2116 hid_debug_event(hdev, buff);
2117 snprintf(buff, BUFF_SZ, "\tFirmware version: %d.%d\n",
2118 raw_data[2], raw_data[1]);
2119 hid_debug_event(hdev, buff);
2120 break;
2121 case REPORT_BL_ERASE_MEMORY:
2122 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2123 "REPORT_BL_ERASE_MEMORY", report->id, size-1);
2124 hid_debug_event(hdev, buff);
2125 /* TODO */
2126 break;
2127 case REPORT_BL_READ_MEMORY:
2128 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2129 "REPORT_BL_READ_MEMORY", report->id, size-1);
2130 hid_debug_event(hdev, buff);
2131 /* TODO */
2132 break;
2133 case REPORT_BL_WRITE_MEMORY:
2134 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2135 "REPORT_BL_WRITE_MEMORY", report->id, size-1);
2136 hid_debug_event(hdev, buff);
2137 /* TODO */
2138 break;
2139 case REPORT_DEVID:
2140 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2141 "REPORT_DEVID", report->id, size-1);
2142 hid_debug_event(hdev, buff);
2143 snprintf(buff, BUFF_SZ, "\tSerial: 0x%02x%02x%02x%02x\n",
2144 raw_data[1], raw_data[2], raw_data[3], raw_data[4]);
2145 hid_debug_event(hdev, buff);
2146 snprintf(buff, BUFF_SZ, "\tType: 0x%02x\n",
2147 raw_data[5]);
2148 hid_debug_event(hdev, buff);
2149 break;
2150 case REPORT_SPLASH_SIZE:
2151 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2152 "REPORT_SPLASH_SIZE", report->id, size-1);
2153 hid_debug_event(hdev, buff);
2154 snprintf(buff, BUFF_SZ, "\tTotal splash space: %d\n",
2155 (raw_data[2] << 8) | raw_data[1]);
2156 hid_debug_event(hdev, buff);
2157 snprintf(buff, BUFF_SZ, "\tUsed splash space: %d\n",
2158 (raw_data[4] << 8) | raw_data[3]);
2159 hid_debug_event(hdev, buff);
2160 break;
2161 case REPORT_HOOK_VERSION:
2162 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2163 "REPORT_HOOK_VERSION", report->id, size-1);
2164 hid_debug_event(hdev, buff);
2165 snprintf(buff, BUFF_SZ, "\tFirmware version: %d.%d\n",
2166 raw_data[1], raw_data[2]);
2167 hid_debug_event(hdev, buff);
2168 break;
2169 default:
2170 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2171 "<unknown>", report->id, size-1);
2172 hid_debug_event(hdev, buff);
2173 break;
2174 }
2175 wake_up_interruptible(&hdev->debug_wait);
2176 kfree(buff);
2177}
Bruno Prémont9bbf2b92010-03-30 22:38:09 +02002178
2179static void picolcd_init_devfs(struct picolcd_data *data,
2180 struct hid_report *eeprom_r, struct hid_report *eeprom_w,
2181 struct hid_report *flash_r, struct hid_report *flash_w,
2182 struct hid_report *reset)
2183{
2184 struct hid_device *hdev = data->hdev;
2185
2186 mutex_init(&data->mutex_flash);
2187
2188 /* reset */
2189 if (reset)
2190 data->debug_reset = debugfs_create_file("reset", 0600,
2191 hdev->debug_dir, data, &picolcd_debug_reset_fops);
2192
2193 /* eeprom */
2194 if (eeprom_r || eeprom_w)
2195 data->debug_eeprom = debugfs_create_file("eeprom",
2196 (eeprom_w ? S_IWUSR : 0) | (eeprom_r ? S_IRUSR : 0),
2197 hdev->debug_dir, data, &picolcd_debug_eeprom_fops);
2198
2199 /* flash */
2200 if (flash_r && flash_r->maxfield == 1 && flash_r->field[0]->report_size == 8)
2201 data->addr_sz = flash_r->field[0]->report_count - 1;
2202 else
2203 data->addr_sz = -1;
2204 if (data->addr_sz == 2 || data->addr_sz == 3) {
2205 data->debug_flash = debugfs_create_file("flash",
2206 (flash_w ? S_IWUSR : 0) | (flash_r ? S_IRUSR : 0),
2207 hdev->debug_dir, data, &picolcd_debug_flash_fops);
2208 } else if (flash_r || flash_w)
2209 dev_warn(&hdev->dev, "Unexpected FLASH access reports, "
2210 "please submit rdesc for review\n");
2211}
2212
2213static void picolcd_exit_devfs(struct picolcd_data *data)
2214{
2215 struct dentry *dent;
2216
2217 dent = data->debug_reset;
2218 data->debug_reset = NULL;
2219 if (dent)
2220 debugfs_remove(dent);
2221 dent = data->debug_eeprom;
2222 data->debug_eeprom = NULL;
2223 if (dent)
2224 debugfs_remove(dent);
2225 dent = data->debug_flash;
2226 data->debug_flash = NULL;
2227 if (dent)
2228 debugfs_remove(dent);
2229 mutex_destroy(&data->mutex_flash);
2230}
Bruno Prémont236db472010-03-30 22:33:50 +02002231#else
Bruno Prémont9d71ea02010-05-02 16:05:05 +02002232static inline void picolcd_debug_raw_event(struct picolcd_data *data,
2233 struct hid_device *hdev, struct hid_report *report,
2234 u8 *raw_data, int size)
2235{
2236}
2237static inline void picolcd_init_devfs(struct picolcd_data *data,
2238 struct hid_report *eeprom_r, struct hid_report *eeprom_w,
2239 struct hid_report *flash_r, struct hid_report *flash_w,
2240 struct hid_report *reset)
2241{
2242}
2243static inline void picolcd_exit_devfs(struct picolcd_data *data)
Bruno Prémont9bbf2b92010-03-30 22:38:09 +02002244{
2245}
2246#endif /* CONFIG_DEBUG_FS */
Bruno Prémont236db472010-03-30 22:33:50 +02002247
2248/*
2249 * Handle raw report as sent by device
2250 */
2251static int picolcd_raw_event(struct hid_device *hdev,
2252 struct hid_report *report, u8 *raw_data, int size)
2253{
2254 struct picolcd_data *data = hid_get_drvdata(hdev);
2255 unsigned long flags;
2256 int ret = 0;
2257
2258 if (!data)
2259 return 1;
2260
2261 if (report->id == REPORT_KEY_STATE) {
2262 if (data->input_keys)
2263 ret = picolcd_raw_keypad(data, report, raw_data+1, size-1);
2264 } else if (report->id == REPORT_IR_DATA) {
2265 if (data->input_cir)
2266 ret = picolcd_raw_cir(data, report, raw_data+1, size-1);
2267 } else {
2268 spin_lock_irqsave(&data->lock, flags);
2269 /*
2270 * We let the caller of picolcd_send_and_wait() check if the
2271 * report we got is one of the expected ones or not.
2272 */
2273 if (data->pending) {
2274 memcpy(data->pending->raw_data, raw_data+1, size-1);
2275 data->pending->raw_size = size-1;
2276 data->pending->in_report = report;
2277 complete(&data->pending->ready);
2278 }
2279 spin_unlock_irqrestore(&data->lock, flags);
2280 }
2281
2282 picolcd_debug_raw_event(data, hdev, report, raw_data, size);
2283 return 1;
2284}
2285
Bruno Prémont9d71ea02010-05-02 16:05:05 +02002286#ifdef CONFIG_PM
2287static int picolcd_suspend(struct hid_device *hdev, pm_message_t message)
2288{
2289 if (message.event & PM_EVENT_AUTO)
2290 return 0;
2291
2292 picolcd_suspend_backlight(hid_get_drvdata(hdev));
2293 dbg_hid(PICOLCD_NAME " device ready for suspend\n");
2294 return 0;
2295}
2296
2297static int picolcd_resume(struct hid_device *hdev)
2298{
2299 int ret;
2300 ret = picolcd_resume_backlight(hid_get_drvdata(hdev));
2301 if (ret)
2302 dbg_hid(PICOLCD_NAME " restoring backlight failed: %d\n", ret);
2303 return 0;
2304}
2305
2306static int picolcd_reset_resume(struct hid_device *hdev)
2307{
2308 int ret;
2309 ret = picolcd_reset(hdev);
2310 if (ret)
2311 dbg_hid(PICOLCD_NAME " resetting our device failed: %d\n", ret);
2312 ret = picolcd_fb_reset(hid_get_drvdata(hdev), 0);
2313 if (ret)
2314 dbg_hid(PICOLCD_NAME " restoring framebuffer content failed: %d\n", ret);
2315 ret = picolcd_resume_lcd(hid_get_drvdata(hdev));
2316 if (ret)
2317 dbg_hid(PICOLCD_NAME " restoring lcd failed: %d\n", ret);
2318 ret = picolcd_resume_backlight(hid_get_drvdata(hdev));
2319 if (ret)
2320 dbg_hid(PICOLCD_NAME " restoring backlight failed: %d\n", ret);
2321 picolcd_leds_set(hid_get_drvdata(hdev));
2322 return 0;
2323}
2324#endif
2325
Bruno Prémont236db472010-03-30 22:33:50 +02002326/* initialize keypad input device */
2327static int picolcd_init_keys(struct picolcd_data *data,
2328 struct hid_report *report)
2329{
2330 struct hid_device *hdev = data->hdev;
2331 struct input_dev *idev;
2332 int error, i;
2333
2334 if (!report)
2335 return -ENODEV;
2336 if (report->maxfield != 1 || report->field[0]->report_count != 2 ||
2337 report->field[0]->report_size != 8) {
2338 dev_err(&hdev->dev, "unsupported KEY_STATE report");
2339 return -EINVAL;
2340 }
2341
2342 idev = input_allocate_device();
2343 if (idev == NULL) {
2344 dev_err(&hdev->dev, "failed to allocate input device");
2345 return -ENOMEM;
2346 }
2347 input_set_drvdata(idev, hdev);
2348 memcpy(data->keycode, def_keymap, sizeof(def_keymap));
2349 idev->name = hdev->name;
2350 idev->phys = hdev->phys;
2351 idev->uniq = hdev->uniq;
2352 idev->id.bustype = hdev->bus;
2353 idev->id.vendor = hdev->vendor;
2354 idev->id.product = hdev->product;
2355 idev->id.version = hdev->version;
2356 idev->dev.parent = hdev->dev.parent;
2357 idev->keycode = &data->keycode;
2358 idev->keycodemax = PICOLCD_KEYS;
2359 idev->keycodesize = sizeof(data->keycode[0]);
2360 input_set_capability(idev, EV_MSC, MSC_SCAN);
2361 set_bit(EV_REP, idev->evbit);
2362 for (i = 0; i < PICOLCD_KEYS; i++)
2363 input_set_capability(idev, EV_KEY, data->keycode[i]);
2364 error = input_register_device(idev);
2365 if (error) {
2366 dev_err(&hdev->dev, "error registering the input device");
2367 input_free_device(idev);
2368 return error;
2369 }
2370 data->input_keys = idev;
2371 return 0;
2372}
2373
2374static void picolcd_exit_keys(struct picolcd_data *data)
2375{
2376 struct input_dev *idev = data->input_keys;
2377
2378 data->input_keys = NULL;
2379 if (idev)
2380 input_unregister_device(idev);
2381}
2382
2383/* initialize CIR input device */
2384static inline int picolcd_init_cir(struct picolcd_data *data, struct hid_report *report)
2385{
2386 /* support not implemented yet */
2387 return 0;
2388}
2389
2390static inline void picolcd_exit_cir(struct picolcd_data *data)
2391{
2392}
2393
2394static int picolcd_probe_lcd(struct hid_device *hdev, struct picolcd_data *data)
2395{
Bruno Prémont236db472010-03-30 22:33:50 +02002396 int error;
2397
2398 error = picolcd_check_version(hdev);
2399 if (error)
2400 return error;
2401
2402 if (data->version[0] != 0 && data->version[1] != 3)
2403 dev_info(&hdev->dev, "Device with untested firmware revision, "
2404 "please submit /sys/kernel/debug/hid/%s/rdesc for this device.\n",
2405 dev_name(&hdev->dev));
2406
2407 /* Setup keypad input device */
2408 error = picolcd_init_keys(data, picolcd_in_report(REPORT_KEY_STATE, hdev));
2409 if (error)
2410 goto err;
2411
2412 /* Setup CIR input device */
2413 error = picolcd_init_cir(data, picolcd_in_report(REPORT_IR_DATA, hdev));
2414 if (error)
2415 goto err;
2416
Bruno Prémontb8c21cf2010-03-30 22:34:30 +02002417 /* Set up the framebuffer device */
2418 error = picolcd_init_framebuffer(data);
2419 if (error)
2420 goto err;
2421
Bruno Prémonte8d931b2010-03-30 22:36:07 +02002422 /* Setup lcd class device */
2423 error = picolcd_init_lcd(data, picolcd_out_report(REPORT_CONTRAST, hdev));
2424 if (error)
2425 goto err;
2426
Bruno Prémontf1c21762010-03-30 22:35:27 +02002427 /* Setup backlight class device */
2428 error = picolcd_init_backlight(data, picolcd_out_report(REPORT_BRIGHTNESS, hdev));
2429 if (error)
2430 goto err;
2431
Bruno Prémont467d6522010-03-30 22:36:49 +02002432 /* Setup the LED class devices */
2433 error = picolcd_init_leds(data, picolcd_out_report(REPORT_LED_STATE, hdev));
2434 if (error)
2435 goto err;
2436
Bruno Prémont9bbf2b92010-03-30 22:38:09 +02002437 picolcd_init_devfs(data, picolcd_out_report(REPORT_EE_READ, hdev),
2438 picolcd_out_report(REPORT_EE_WRITE, hdev),
2439 picolcd_out_report(REPORT_READ_MEMORY, hdev),
2440 picolcd_out_report(REPORT_WRITE_MEMORY, hdev),
2441 picolcd_out_report(REPORT_RESET, hdev));
Bruno Prémont236db472010-03-30 22:33:50 +02002442 return 0;
2443err:
Bruno Prémont467d6522010-03-30 22:36:49 +02002444 picolcd_exit_leds(data);
Bruno Prémontf1c21762010-03-30 22:35:27 +02002445 picolcd_exit_backlight(data);
Bruno Prémonte8d931b2010-03-30 22:36:07 +02002446 picolcd_exit_lcd(data);
Bruno Prémontb8c21cf2010-03-30 22:34:30 +02002447 picolcd_exit_framebuffer(data);
Bruno Prémont236db472010-03-30 22:33:50 +02002448 picolcd_exit_cir(data);
2449 picolcd_exit_keys(data);
2450 return error;
2451}
2452
2453static int picolcd_probe_bootloader(struct hid_device *hdev, struct picolcd_data *data)
2454{
Bruno Prémont236db472010-03-30 22:33:50 +02002455 int error;
2456
2457 error = picolcd_check_version(hdev);
2458 if (error)
2459 return error;
2460
2461 if (data->version[0] != 1 && data->version[1] != 0)
2462 dev_info(&hdev->dev, "Device with untested bootloader revision, "
2463 "please submit /sys/kernel/debug/hid/%s/rdesc for this device.\n",
2464 dev_name(&hdev->dev));
2465
Bruno Prémont9bbf2b92010-03-30 22:38:09 +02002466 picolcd_init_devfs(data, NULL, NULL,
2467 picolcd_out_report(REPORT_BL_READ_MEMORY, hdev),
2468 picolcd_out_report(REPORT_BL_WRITE_MEMORY, hdev), NULL);
Bruno Prémont236db472010-03-30 22:33:50 +02002469 return 0;
2470}
2471
2472static int picolcd_probe(struct hid_device *hdev,
2473 const struct hid_device_id *id)
2474{
2475 struct picolcd_data *data;
2476 int error = -ENOMEM;
2477
2478 dbg_hid(PICOLCD_NAME " hardware probe...\n");
2479
2480 /*
2481 * Let's allocate the picolcd data structure, set some reasonable
2482 * defaults, and associate it with the device
2483 */
2484 data = kzalloc(sizeof(struct picolcd_data), GFP_KERNEL);
2485 if (data == NULL) {
2486 dev_err(&hdev->dev, "can't allocate space for Minibox PicoLCD device data\n");
2487 error = -ENOMEM;
2488 goto err_no_cleanup;
2489 }
2490
2491 spin_lock_init(&data->lock);
2492 mutex_init(&data->mutex);
2493 data->hdev = hdev;
Bruno Prémont0b5adf92010-04-25 21:29:16 +02002494 data->opmode_delay = 5000;
Bruno Prémont236db472010-03-30 22:33:50 +02002495 if (hdev->product == USB_DEVICE_ID_PICOLCD_BOOTLOADER)
2496 data->status |= PICOLCD_BOOTLOADER;
2497 hid_set_drvdata(hdev, data);
2498
2499 /* Parse the device reports and start it up */
2500 error = hid_parse(hdev);
2501 if (error) {
2502 dev_err(&hdev->dev, "device report parse failed\n");
2503 goto err_cleanup_data;
2504 }
2505
2506 /* We don't use hidinput but hid_hw_start() fails if nothing is
2507 * claimed. So spoof claimed input. */
2508 hdev->claimed = HID_CLAIMED_INPUT;
2509 error = hid_hw_start(hdev, 0);
2510 hdev->claimed = 0;
2511 if (error) {
2512 dev_err(&hdev->dev, "hardware start failed\n");
2513 goto err_cleanup_data;
2514 }
2515
2516 error = hdev->ll_driver->open(hdev);
2517 if (error) {
2518 dev_err(&hdev->dev, "failed to open input interrupt pipe for key and IR events\n");
2519 goto err_cleanup_hid_hw;
2520 }
2521
Bruno Prémont0b5adf92010-04-25 21:29:16 +02002522 error = device_create_file(&hdev->dev, &dev_attr_operation_mode_delay);
Bruno Prémont236db472010-03-30 22:33:50 +02002523 if (error) {
2524 dev_err(&hdev->dev, "failed to create sysfs attributes\n");
2525 goto err_cleanup_hid_ll;
2526 }
2527
Bruno Prémont0b5adf92010-04-25 21:29:16 +02002528 error = device_create_file(&hdev->dev, &dev_attr_operation_mode);
2529 if (error) {
2530 dev_err(&hdev->dev, "failed to create sysfs attributes\n");
2531 goto err_cleanup_sysfs1;
2532 }
2533
Bruno Prémont236db472010-03-30 22:33:50 +02002534 if (data->status & PICOLCD_BOOTLOADER)
2535 error = picolcd_probe_bootloader(hdev, data);
2536 else
2537 error = picolcd_probe_lcd(hdev, data);
2538 if (error)
Bruno Prémont0b5adf92010-04-25 21:29:16 +02002539 goto err_cleanup_sysfs2;
Bruno Prémont236db472010-03-30 22:33:50 +02002540
2541 dbg_hid(PICOLCD_NAME " activated and initialized\n");
2542 return 0;
2543
Bruno Prémont0b5adf92010-04-25 21:29:16 +02002544err_cleanup_sysfs2:
Bruno Prémont236db472010-03-30 22:33:50 +02002545 device_remove_file(&hdev->dev, &dev_attr_operation_mode);
Bruno Prémont0b5adf92010-04-25 21:29:16 +02002546err_cleanup_sysfs1:
2547 device_remove_file(&hdev->dev, &dev_attr_operation_mode_delay);
Bruno Prémont236db472010-03-30 22:33:50 +02002548err_cleanup_hid_ll:
2549 hdev->ll_driver->close(hdev);
2550err_cleanup_hid_hw:
2551 hid_hw_stop(hdev);
2552err_cleanup_data:
2553 kfree(data);
2554err_no_cleanup:
2555 hid_set_drvdata(hdev, NULL);
2556
2557 return error;
2558}
2559
2560static void picolcd_remove(struct hid_device *hdev)
2561{
2562 struct picolcd_data *data = hid_get_drvdata(hdev);
2563 unsigned long flags;
2564
2565 dbg_hid(PICOLCD_NAME " hardware remove...\n");
2566 spin_lock_irqsave(&data->lock, flags);
2567 data->status |= PICOLCD_FAILED;
2568 spin_unlock_irqrestore(&data->lock, flags);
2569
Bruno Prémont9bbf2b92010-03-30 22:38:09 +02002570 picolcd_exit_devfs(data);
Bruno Prémont236db472010-03-30 22:33:50 +02002571 device_remove_file(&hdev->dev, &dev_attr_operation_mode);
Bruno Prémont0b5adf92010-04-25 21:29:16 +02002572 device_remove_file(&hdev->dev, &dev_attr_operation_mode_delay);
Bruno Prémont236db472010-03-30 22:33:50 +02002573 hdev->ll_driver->close(hdev);
2574 hid_hw_stop(hdev);
2575 hid_set_drvdata(hdev, NULL);
2576
2577 /* Shortcut potential pending reply that will never arrive */
2578 spin_lock_irqsave(&data->lock, flags);
2579 if (data->pending)
2580 complete(&data->pending->ready);
2581 spin_unlock_irqrestore(&data->lock, flags);
2582
Bruno Prémont467d6522010-03-30 22:36:49 +02002583 /* Cleanup LED */
2584 picolcd_exit_leds(data);
Bruno Prémontb8c21cf2010-03-30 22:34:30 +02002585 /* Clean up the framebuffer */
Bruno Prémontf1c21762010-03-30 22:35:27 +02002586 picolcd_exit_backlight(data);
Bruno Prémonte8d931b2010-03-30 22:36:07 +02002587 picolcd_exit_lcd(data);
Bruno Prémontb8c21cf2010-03-30 22:34:30 +02002588 picolcd_exit_framebuffer(data);
Bruno Prémont236db472010-03-30 22:33:50 +02002589 /* Cleanup input */
2590 picolcd_exit_cir(data);
2591 picolcd_exit_keys(data);
2592
2593 mutex_destroy(&data->mutex);
2594 /* Finally, clean up the picolcd data itself */
2595 kfree(data);
2596}
2597
2598static const struct hid_device_id picolcd_devices[] = {
2599 { HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP, USB_DEVICE_ID_PICOLCD) },
2600 { HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP, USB_DEVICE_ID_PICOLCD_BOOTLOADER) },
2601 { }
2602};
2603MODULE_DEVICE_TABLE(hid, picolcd_devices);
2604
2605static struct hid_driver picolcd_driver = {
2606 .name = "hid-picolcd",
2607 .id_table = picolcd_devices,
2608 .probe = picolcd_probe,
2609 .remove = picolcd_remove,
2610 .raw_event = picolcd_raw_event,
Bruno Prémont9d71ea02010-05-02 16:05:05 +02002611#ifdef CONFIG_PM
2612 .suspend = picolcd_suspend,
2613 .resume = picolcd_resume,
2614 .reset_resume = picolcd_reset_resume,
2615#endif
Bruno Prémont236db472010-03-30 22:33:50 +02002616};
2617
2618static int __init picolcd_init(void)
2619{
2620 return hid_register_driver(&picolcd_driver);
2621}
2622
2623static void __exit picolcd_exit(void)
2624{
2625 hid_unregister_driver(&picolcd_driver);
2626}
2627
2628module_init(picolcd_init);
2629module_exit(picolcd_exit);
2630MODULE_DESCRIPTION("Minibox graphics PicoLCD Driver");
2631MODULE_LICENSE("GPL v2");