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