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