blob: de9cf21b3494c39d7a105f75a90078ebfe8b145e [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 }
Joe Perches4291ee32010-12-09 19:29:03 -0800256 hid_warn(hdev, "No report with id 0x%x found\n", id);
Bruno Prémont236db472010-03-30 22:33:50 +0200257 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
Bruno Prémont225b4592010-06-28 22:33:27 +0200442 if (!data)
443 return;
444
Bruno Prémontb8c21cf2010-03-30 22:34:30 +0200445 spin_lock_irqsave(&data->lock, flags);
446 if (!(data->status & PICOLCD_READY_FB)) {
447 spin_unlock_irqrestore(&data->lock, flags);
448 picolcd_fb_reset(data, 0);
449 } else {
450 spin_unlock_irqrestore(&data->lock, flags);
451 }
452
453 /*
454 * Translate the framebuffer into the format needed by the PicoLCD.
455 * See display layout above.
456 * Do this one tile after the other and push those tiles that changed.
457 *
458 * Wait for our IO to complete as otherwise we might flood the queue!
459 */
460 n = 0;
461 for (chip = 0; chip < 4; chip++)
462 for (tile = 0; tile < 8; tile++)
463 if (picolcd_fb_update_tile(data->fb_vbitmap,
Bruno Prémontb70884f2010-06-28 22:30:29 +0200464 data->fb_bitmap, data->fb_bpp, chip, tile) ||
465 data->fb_force) {
Bruno Prémontb8c21cf2010-03-30 22:34:30 +0200466 n += 2;
Bruno Prémont225b4592010-06-28 22:33:27 +0200467 if (!data->fb_info->par)
468 return; /* device lost! */
Bruno Prémontb8c21cf2010-03-30 22:34:30 +0200469 if (n >= HID_OUTPUT_FIFO_SIZE / 2) {
470 usbhid_wait_io(data->hdev);
471 n = 0;
472 }
473 picolcd_fb_send_tile(data->hdev, chip, tile);
474 }
Bruno Prémontb70884f2010-06-28 22:30:29 +0200475 data->fb_force = false;
Bruno Prémontb8c21cf2010-03-30 22:34:30 +0200476 if (n)
477 usbhid_wait_io(data->hdev);
478}
479
480/* Stub to call the system default and update the image on the picoLCD */
481static void picolcd_fb_fillrect(struct fb_info *info,
482 const struct fb_fillrect *rect)
483{
484 if (!info->par)
485 return;
486 sys_fillrect(info, rect);
487
488 schedule_delayed_work(&info->deferred_work, 0);
489}
490
491/* Stub to call the system default and update the image on the picoLCD */
492static void picolcd_fb_copyarea(struct fb_info *info,
493 const struct fb_copyarea *area)
494{
495 if (!info->par)
496 return;
497 sys_copyarea(info, area);
498
499 schedule_delayed_work(&info->deferred_work, 0);
500}
501
502/* Stub to call the system default and update the image on the picoLCD */
503static void picolcd_fb_imageblit(struct fb_info *info, const struct fb_image *image)
504{
505 if (!info->par)
506 return;
507 sys_imageblit(info, image);
508
509 schedule_delayed_work(&info->deferred_work, 0);
510}
511
512/*
513 * this is the slow path from userspace. they can seek and write to
514 * the fb. it's inefficient to do anything less than a full screen draw
515 */
516static ssize_t picolcd_fb_write(struct fb_info *info, const char __user *buf,
517 size_t count, loff_t *ppos)
518{
519 ssize_t ret;
520 if (!info->par)
521 return -ENODEV;
522 ret = fb_sys_write(info, buf, count, ppos);
523 if (ret >= 0)
524 schedule_delayed_work(&info->deferred_work, 0);
525 return ret;
526}
527
528static int picolcd_fb_blank(int blank, struct fb_info *info)
529{
530 if (!info->par)
531 return -ENODEV;
532 /* We let fb notification do this for us via lcd/backlight device */
533 return 0;
534}
535
536static void picolcd_fb_destroy(struct fb_info *info)
537{
538 struct picolcd_data *data = info->par;
Bruno Prémont225b4592010-06-28 22:33:27 +0200539 u32 *ref_cnt = info->pseudo_palette;
540 int may_release;
541
Bruno Prémontb8c21cf2010-03-30 22:34:30 +0200542 info->par = NULL;
543 if (data)
544 data->fb_info = NULL;
545 fb_deferred_io_cleanup(info);
Bruno Prémont225b4592010-06-28 22:33:27 +0200546
547 ref_cnt--;
548 mutex_lock(&info->lock);
549 (*ref_cnt)--;
Dan Carpentera1060252010-08-06 14:51:10 +0200550 may_release = !*ref_cnt;
Bruno Prémont225b4592010-06-28 22:33:27 +0200551 mutex_unlock(&info->lock);
552 if (may_release) {
Bruno Prémont225b4592010-06-28 22:33:27 +0200553 vfree((u8 *)info->fix.smem_start);
Bruno Prémont1778ca22010-08-06 10:08:04 +0200554 framebuffer_release(info);
Bruno Prémont225b4592010-06-28 22:33:27 +0200555 }
Bruno Prémontb8c21cf2010-03-30 22:34:30 +0200556}
557
558static int picolcd_fb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
559{
560 __u32 bpp = var->bits_per_pixel;
561 __u32 activate = var->activate;
562
563 /* only allow 1/8 bit depth (8-bit is grayscale) */
564 *var = picolcdfb_var;
565 var->activate = activate;
Bruno Prémontb70884f2010-06-28 22:30:29 +0200566 if (bpp >= 8) {
Bruno Prémontb8c21cf2010-03-30 22:34:30 +0200567 var->bits_per_pixel = 8;
Bruno Prémontb70884f2010-06-28 22:30:29 +0200568 var->red.length = 8;
569 var->green.length = 8;
570 var->blue.length = 8;
571 } else {
Bruno Prémontb8c21cf2010-03-30 22:34:30 +0200572 var->bits_per_pixel = 1;
Bruno Prémontb70884f2010-06-28 22:30:29 +0200573 var->red.length = 1;
574 var->green.length = 1;
575 var->blue.length = 1;
576 }
Bruno Prémontb8c21cf2010-03-30 22:34:30 +0200577 return 0;
578}
579
580static int picolcd_set_par(struct fb_info *info)
581{
582 struct picolcd_data *data = info->par;
Bruno Prémont365f1fc2010-06-28 22:31:20 +0200583 u8 *tmp_fb, *o_fb;
Bruno Prémont225b4592010-06-28 22:33:27 +0200584 if (!data)
585 return -ENODEV;
Bruno Prémontb8c21cf2010-03-30 22:34:30 +0200586 if (info->var.bits_per_pixel == data->fb_bpp)
587 return 0;
588 /* switch between 1/8 bit depths */
589 if (info->var.bits_per_pixel != 1 && info->var.bits_per_pixel != 8)
590 return -EINVAL;
591
Bruno Prémont365f1fc2010-06-28 22:31:20 +0200592 o_fb = data->fb_bitmap;
593 tmp_fb = kmalloc(PICOLCDFB_SIZE*info->var.bits_per_pixel, GFP_KERNEL);
594 if (!tmp_fb)
Bruno Prémontb8c21cf2010-03-30 22:34:30 +0200595 return -ENOMEM;
596
Bruno Prémontb8c21cf2010-03-30 22:34:30 +0200597 /* translate FB content to new bits-per-pixel */
598 if (info->var.bits_per_pixel == 1) {
599 int i, b;
600 for (i = 0; i < PICOLCDFB_SIZE; i++) {
601 u8 p = 0;
602 for (b = 0; b < 8; b++) {
603 p <<= 1;
604 p |= o_fb[i*8+b] ? 0x01 : 0x00;
605 }
Bruno Prémont365f1fc2010-06-28 22:31:20 +0200606 tmp_fb[i] = p;
Bruno Prémontb8c21cf2010-03-30 22:34:30 +0200607 }
Bruno Prémont365f1fc2010-06-28 22:31:20 +0200608 memcpy(o_fb, tmp_fb, PICOLCDFB_SIZE);
Bruno Prémontb8c21cf2010-03-30 22:34:30 +0200609 info->fix.visual = FB_VISUAL_MONO01;
610 info->fix.line_length = PICOLCDFB_WIDTH / 8;
611 } else {
612 int i;
Bruno Prémont365f1fc2010-06-28 22:31:20 +0200613 memcpy(tmp_fb, o_fb, PICOLCDFB_SIZE);
Bruno Prémontb8c21cf2010-03-30 22:34:30 +0200614 for (i = 0; i < PICOLCDFB_SIZE * 8; i++)
Bruno Prémont365f1fc2010-06-28 22:31:20 +0200615 o_fb[i] = tmp_fb[i/8] & (0x01 << (7 - i % 8)) ? 0xff : 0x00;
616 info->fix.visual = FB_VISUAL_DIRECTCOLOR;
Bruno Prémontb8c21cf2010-03-30 22:34:30 +0200617 info->fix.line_length = PICOLCDFB_WIDTH;
618 }
619
Bruno Prémont365f1fc2010-06-28 22:31:20 +0200620 kfree(tmp_fb);
Bruno Prémontb8c21cf2010-03-30 22:34:30 +0200621 data->fb_bpp = info->var.bits_per_pixel;
Bruno Prémontb8c21cf2010-03-30 22:34:30 +0200622 return 0;
623}
624
Bruno Prémont225b4592010-06-28 22:33:27 +0200625/* Do refcounting on our FB and cleanup per worker if FB is
626 * closed after unplug of our device
627 * (fb_release holds info->lock and still touches info after
628 * we return so we can't release it immediately.
629 */
630struct picolcd_fb_cleanup_item {
631 struct fb_info *info;
632 struct picolcd_fb_cleanup_item *next;
633};
634static struct picolcd_fb_cleanup_item *fb_pending;
635DEFINE_SPINLOCK(fb_pending_lock);
636
637static void picolcd_fb_do_cleanup(struct work_struct *data)
638{
639 struct picolcd_fb_cleanup_item *item;
640 unsigned long flags;
641
642 do {
643 spin_lock_irqsave(&fb_pending_lock, flags);
644 item = fb_pending;
645 fb_pending = item ? item->next : NULL;
646 spin_unlock_irqrestore(&fb_pending_lock, flags);
647
648 if (item) {
649 u8 *fb = (u8 *)item->info->fix.smem_start;
650 /* make sure we do not race against fb core when
651 * releasing */
652 mutex_lock(&item->info->lock);
653 mutex_unlock(&item->info->lock);
654 framebuffer_release(item->info);
655 vfree(fb);
656 }
657 } while (item);
658}
659
660DECLARE_WORK(picolcd_fb_cleanup, picolcd_fb_do_cleanup);
661
662static int picolcd_fb_open(struct fb_info *info, int u)
663{
664 u32 *ref_cnt = info->pseudo_palette;
665 ref_cnt--;
666
667 (*ref_cnt)++;
668 return 0;
669}
670
671static int picolcd_fb_release(struct fb_info *info, int u)
672{
673 u32 *ref_cnt = info->pseudo_palette;
674 ref_cnt--;
675
676 (*ref_cnt)++;
677 if (!*ref_cnt) {
678 unsigned long flags;
679 struct picolcd_fb_cleanup_item *item = (struct picolcd_fb_cleanup_item *)ref_cnt;
680 item--;
681 spin_lock_irqsave(&fb_pending_lock, flags);
682 item->next = fb_pending;
683 fb_pending = item;
684 spin_unlock_irqrestore(&fb_pending_lock, flags);
685 schedule_work(&picolcd_fb_cleanup);
686 }
687 return 0;
688}
689
Bruno Prémontb8c21cf2010-03-30 22:34:30 +0200690/* Note this can't be const because of struct fb_info definition */
691static struct fb_ops picolcdfb_ops = {
692 .owner = THIS_MODULE,
693 .fb_destroy = picolcd_fb_destroy,
Bruno Prémont225b4592010-06-28 22:33:27 +0200694 .fb_open = picolcd_fb_open,
695 .fb_release = picolcd_fb_release,
Bruno Prémontb8c21cf2010-03-30 22:34:30 +0200696 .fb_read = fb_sys_read,
697 .fb_write = picolcd_fb_write,
698 .fb_blank = picolcd_fb_blank,
699 .fb_fillrect = picolcd_fb_fillrect,
700 .fb_copyarea = picolcd_fb_copyarea,
701 .fb_imageblit = picolcd_fb_imageblit,
702 .fb_check_var = picolcd_fb_check_var,
703 .fb_set_par = picolcd_set_par,
704};
705
706
707/* Callback from deferred IO workqueue */
708static void picolcd_fb_deferred_io(struct fb_info *info, struct list_head *pagelist)
709{
710 picolcd_fb_update(info->par);
711}
712
713static const struct fb_deferred_io picolcd_fb_defio = {
714 .delay = HZ / PICOLCDFB_UPDATE_RATE_DEFAULT,
715 .deferred_io = picolcd_fb_deferred_io,
716};
717
718
719/*
720 * The "fb_update_rate" sysfs attribute
721 */
722static ssize_t picolcd_fb_update_rate_show(struct device *dev,
723 struct device_attribute *attr, char *buf)
724{
725 struct picolcd_data *data = dev_get_drvdata(dev);
726 unsigned i, fb_update_rate = data->fb_update_rate;
727 size_t ret = 0;
728
729 for (i = 1; i <= PICOLCDFB_UPDATE_RATE_LIMIT; i++)
730 if (ret >= PAGE_SIZE)
731 break;
732 else if (i == fb_update_rate)
733 ret += snprintf(buf+ret, PAGE_SIZE-ret, "[%u] ", i);
734 else
735 ret += snprintf(buf+ret, PAGE_SIZE-ret, "%u ", i);
736 if (ret > 0)
737 buf[min(ret, (size_t)PAGE_SIZE)-1] = '\n';
738 return ret;
739}
740
741static ssize_t picolcd_fb_update_rate_store(struct device *dev,
742 struct device_attribute *attr, const char *buf, size_t count)
743{
744 struct picolcd_data *data = dev_get_drvdata(dev);
745 int i;
746 unsigned u;
747
748 if (count < 1 || count > 10)
749 return -EINVAL;
750
751 i = sscanf(buf, "%u", &u);
752 if (i != 1)
753 return -EINVAL;
754
755 if (u > PICOLCDFB_UPDATE_RATE_LIMIT)
756 return -ERANGE;
757 else if (u == 0)
758 u = PICOLCDFB_UPDATE_RATE_DEFAULT;
759
760 data->fb_update_rate = u;
761 data->fb_defio.delay = HZ / data->fb_update_rate;
762 return count;
763}
764
765static DEVICE_ATTR(fb_update_rate, 0666, picolcd_fb_update_rate_show,
766 picolcd_fb_update_rate_store);
767
768/* initialize Framebuffer device */
769static int picolcd_init_framebuffer(struct picolcd_data *data)
770{
771 struct device *dev = &data->hdev->dev;
772 struct fb_info *info = NULL;
Bruno Prémontb70884f2010-06-28 22:30:29 +0200773 int i, error = -ENOMEM;
Bruno Prémontb8c21cf2010-03-30 22:34:30 +0200774 u8 *fb_vbitmap = NULL;
775 u8 *fb_bitmap = NULL;
Bruno Prémontb70884f2010-06-28 22:30:29 +0200776 u32 *palette;
Bruno Prémontb8c21cf2010-03-30 22:34:30 +0200777
Bruno Prémont365f1fc2010-06-28 22:31:20 +0200778 fb_bitmap = vmalloc(PICOLCDFB_SIZE*8);
Bruno Prémontb8c21cf2010-03-30 22:34:30 +0200779 if (fb_bitmap == NULL) {
780 dev_err(dev, "can't get a free page for framebuffer\n");
781 goto err_nomem;
782 }
783
784 fb_vbitmap = kmalloc(PICOLCDFB_SIZE, GFP_KERNEL);
785 if (fb_vbitmap == NULL) {
786 dev_err(dev, "can't alloc vbitmap image buffer\n");
787 goto err_nomem;
788 }
789
790 data->fb_update_rate = PICOLCDFB_UPDATE_RATE_DEFAULT;
791 data->fb_defio = picolcd_fb_defio;
Bruno Prémontb70884f2010-06-28 22:30:29 +0200792 /* The extra memory is:
793 * - struct picolcd_fb_cleanup_item
794 * - u32 for ref_count
795 * - 256*u32 for pseudo_palette
796 */
Bruno Prémont225b4592010-06-28 22:33:27 +0200797 info = framebuffer_alloc(257 * sizeof(u32) + sizeof(struct picolcd_fb_cleanup_item), dev);
Bruno Prémontb8c21cf2010-03-30 22:34:30 +0200798 if (info == NULL) {
799 dev_err(dev, "failed to allocate a framebuffer\n");
800 goto err_nomem;
801 }
802
Bruno Prémont225b4592010-06-28 22:33:27 +0200803 palette = info->par + sizeof(struct picolcd_fb_cleanup_item);
Bruno Prémontb70884f2010-06-28 22:30:29 +0200804 *palette = 1;
805 palette++;
806 for (i = 0; i < 256; i++)
807 palette[i] = i > 0 && i < 16 ? 0xff : 0;
808 info->pseudo_palette = palette;
Bruno Prémontb8c21cf2010-03-30 22:34:30 +0200809 info->fbdefio = &data->fb_defio;
810 info->screen_base = (char __force __iomem *)fb_bitmap;
811 info->fbops = &picolcdfb_ops;
812 info->var = picolcdfb_var;
813 info->fix = picolcdfb_fix;
Bruno Prémont365f1fc2010-06-28 22:31:20 +0200814 info->fix.smem_len = PICOLCDFB_SIZE*8;
Bruno Prémontb8c21cf2010-03-30 22:34:30 +0200815 info->fix.smem_start = (unsigned long)fb_bitmap;
816 info->par = data;
817 info->flags = FBINFO_FLAG_DEFAULT;
818
819 data->fb_vbitmap = fb_vbitmap;
820 data->fb_bitmap = fb_bitmap;
821 data->fb_bpp = picolcdfb_var.bits_per_pixel;
822 error = picolcd_fb_reset(data, 1);
823 if (error) {
824 dev_err(dev, "failed to configure display\n");
825 goto err_cleanup;
826 }
827 error = device_create_file(dev, &dev_attr_fb_update_rate);
828 if (error) {
829 dev_err(dev, "failed to create sysfs attributes\n");
830 goto err_cleanup;
831 }
Bruno Prémonta7c9a0a2010-06-30 22:36:31 +0200832 fb_deferred_io_init(info);
Bruno Prémontb8c21cf2010-03-30 22:34:30 +0200833 data->fb_info = info;
834 error = register_framebuffer(info);
835 if (error) {
836 dev_err(dev, "failed to register framebuffer\n");
837 goto err_sysfs;
838 }
Bruno Prémontb8c21cf2010-03-30 22:34:30 +0200839 /* schedule first output of framebuffer */
Bruno Prémontb70884f2010-06-28 22:30:29 +0200840 data->fb_force = 1;
Bruno Prémontb8c21cf2010-03-30 22:34:30 +0200841 schedule_delayed_work(&info->deferred_work, 0);
842 return 0;
843
844err_sysfs:
Bruno Prémonta7c9a0a2010-06-30 22:36:31 +0200845 fb_deferred_io_cleanup(info);
Bruno Prémontb8c21cf2010-03-30 22:34:30 +0200846 device_remove_file(dev, &dev_attr_fb_update_rate);
847err_cleanup:
848 data->fb_vbitmap = NULL;
849 data->fb_bitmap = NULL;
850 data->fb_bpp = 0;
851 data->fb_info = NULL;
852
853err_nomem:
854 framebuffer_release(info);
855 vfree(fb_bitmap);
856 kfree(fb_vbitmap);
857 return error;
858}
859
860static void picolcd_exit_framebuffer(struct picolcd_data *data)
861{
862 struct fb_info *info = data->fb_info;
863 u8 *fb_vbitmap = data->fb_vbitmap;
Bruno Prémontb8c21cf2010-03-30 22:34:30 +0200864
865 if (!info)
866 return;
867
Bruno Prémont225b4592010-06-28 22:33:27 +0200868 info->par = NULL;
869 device_remove_file(&data->hdev->dev, &dev_attr_fb_update_rate);
870 unregister_framebuffer(info);
Bruno Prémontb8c21cf2010-03-30 22:34:30 +0200871 data->fb_vbitmap = NULL;
872 data->fb_bitmap = NULL;
873 data->fb_bpp = 0;
874 data->fb_info = NULL;
Bruno Prémontb8c21cf2010-03-30 22:34:30 +0200875 kfree(fb_vbitmap);
876}
877
Bruno Prémontf1c21762010-03-30 22:35:27 +0200878#define picolcd_fbinfo(d) ((d)->fb_info)
Bruno Prémontb8c21cf2010-03-30 22:34:30 +0200879#else
880static inline int picolcd_fb_reset(struct picolcd_data *data, int clear)
881{
882 return 0;
883}
884static inline int picolcd_init_framebuffer(struct picolcd_data *data)
885{
886 return 0;
887}
Bruno Prémont9d71ea02010-05-02 16:05:05 +0200888static inline void picolcd_exit_framebuffer(struct picolcd_data *data)
Bruno Prémontb8c21cf2010-03-30 22:34:30 +0200889{
890}
Bruno Prémontf1c21762010-03-30 22:35:27 +0200891#define picolcd_fbinfo(d) NULL
Bruno Prémont5435f282010-04-11 12:17:45 +0200892#endif /* CONFIG_HID_PICOLCD_FB */
Bruno Prémontb8c21cf2010-03-30 22:34:30 +0200893
Bruno Prémont5435f282010-04-11 12:17:45 +0200894#ifdef CONFIG_HID_PICOLCD_BACKLIGHT
Bruno Prémontf1c21762010-03-30 22:35:27 +0200895/*
896 * backlight class device
897 */
898static int picolcd_get_brightness(struct backlight_device *bdev)
899{
900 struct picolcd_data *data = bl_get_data(bdev);
901 return data->lcd_brightness;
902}
903
904static int picolcd_set_brightness(struct backlight_device *bdev)
905{
906 struct picolcd_data *data = bl_get_data(bdev);
907 struct hid_report *report = picolcd_out_report(REPORT_BRIGHTNESS, data->hdev);
908 unsigned long flags;
909
910 if (!report || report->maxfield != 1 || report->field[0]->report_count != 1)
911 return -ENODEV;
912
913 data->lcd_brightness = bdev->props.brightness & 0x0ff;
914 data->lcd_power = bdev->props.power;
915 spin_lock_irqsave(&data->lock, flags);
916 hid_set_field(report->field[0], 0, data->lcd_power == FB_BLANK_UNBLANK ? data->lcd_brightness : 0);
917 usbhid_submit_report(data->hdev, report, USB_DIR_OUT);
918 spin_unlock_irqrestore(&data->lock, flags);
919 return 0;
920}
921
922static int picolcd_check_bl_fb(struct backlight_device *bdev, struct fb_info *fb)
923{
924 return fb && fb == picolcd_fbinfo((struct picolcd_data *)bl_get_data(bdev));
925}
926
927static const struct backlight_ops picolcd_blops = {
928 .update_status = picolcd_set_brightness,
929 .get_brightness = picolcd_get_brightness,
930 .check_fb = picolcd_check_bl_fb,
931};
932
933static int picolcd_init_backlight(struct picolcd_data *data, struct hid_report *report)
934{
935 struct device *dev = &data->hdev->dev;
936 struct backlight_device *bdev;
937 struct backlight_properties props;
938 if (!report)
939 return -ENODEV;
940 if (report->maxfield != 1 || report->field[0]->report_count != 1 ||
941 report->field[0]->report_size != 8) {
942 dev_err(dev, "unsupported BRIGHTNESS report");
943 return -EINVAL;
944 }
945
946 memset(&props, 0, sizeof(props));
947 props.max_brightness = 0xff;
948 bdev = backlight_device_register(dev_name(dev), dev, data,
949 &picolcd_blops, &props);
950 if (IS_ERR(bdev)) {
951 dev_err(dev, "failed to register backlight\n");
952 return PTR_ERR(bdev);
953 }
954 bdev->props.brightness = 0xff;
955 data->lcd_brightness = 0xff;
956 data->backlight = bdev;
957 picolcd_set_brightness(bdev);
958 return 0;
959}
960
961static void picolcd_exit_backlight(struct picolcd_data *data)
962{
963 struct backlight_device *bdev = data->backlight;
964
965 data->backlight = NULL;
966 if (bdev)
967 backlight_device_unregister(bdev);
968}
969
970static inline int picolcd_resume_backlight(struct picolcd_data *data)
971{
972 if (!data->backlight)
973 return 0;
974 return picolcd_set_brightness(data->backlight);
975}
976
Bruno Prémont9d71ea02010-05-02 16:05:05 +0200977#ifdef CONFIG_PM
978static void picolcd_suspend_backlight(struct picolcd_data *data)
979{
980 int bl_power = data->lcd_power;
981 if (!data->backlight)
982 return;
983
984 data->backlight->props.power = FB_BLANK_POWERDOWN;
985 picolcd_set_brightness(data->backlight);
986 data->lcd_power = data->backlight->props.power = bl_power;
987}
988#endif /* CONFIG_PM */
Bruno Prémontf1c21762010-03-30 22:35:27 +0200989#else
990static inline int picolcd_init_backlight(struct picolcd_data *data,
991 struct hid_report *report)
992{
993 return 0;
994}
995static inline void picolcd_exit_backlight(struct picolcd_data *data)
996{
997}
998static inline int picolcd_resume_backlight(struct picolcd_data *data)
999{
1000 return 0;
1001}
Bruno Prémont9d71ea02010-05-02 16:05:05 +02001002static inline void picolcd_suspend_backlight(struct picolcd_data *data)
1003{
1004}
Bruno Prémont5435f282010-04-11 12:17:45 +02001005#endif /* CONFIG_HID_PICOLCD_BACKLIGHT */
Bruno Prémontf1c21762010-03-30 22:35:27 +02001006
Bruno Prémont5435f282010-04-11 12:17:45 +02001007#ifdef CONFIG_HID_PICOLCD_LCD
Bruno Prémonte8d931b2010-03-30 22:36:07 +02001008/*
1009 * lcd class device
1010 */
1011static int picolcd_get_contrast(struct lcd_device *ldev)
1012{
1013 struct picolcd_data *data = lcd_get_data(ldev);
1014 return data->lcd_contrast;
1015}
1016
1017static int picolcd_set_contrast(struct lcd_device *ldev, int contrast)
1018{
1019 struct picolcd_data *data = lcd_get_data(ldev);
1020 struct hid_report *report = picolcd_out_report(REPORT_CONTRAST, data->hdev);
1021 unsigned long flags;
1022
1023 if (!report || report->maxfield != 1 || report->field[0]->report_count != 1)
1024 return -ENODEV;
1025
1026 data->lcd_contrast = contrast & 0x0ff;
1027 spin_lock_irqsave(&data->lock, flags);
1028 hid_set_field(report->field[0], 0, data->lcd_contrast);
1029 usbhid_submit_report(data->hdev, report, USB_DIR_OUT);
1030 spin_unlock_irqrestore(&data->lock, flags);
1031 return 0;
1032}
1033
1034static int picolcd_check_lcd_fb(struct lcd_device *ldev, struct fb_info *fb)
1035{
1036 return fb && fb == picolcd_fbinfo((struct picolcd_data *)lcd_get_data(ldev));
1037}
1038
1039static struct lcd_ops picolcd_lcdops = {
1040 .get_contrast = picolcd_get_contrast,
1041 .set_contrast = picolcd_set_contrast,
1042 .check_fb = picolcd_check_lcd_fb,
1043};
1044
1045static int picolcd_init_lcd(struct picolcd_data *data, struct hid_report *report)
1046{
1047 struct device *dev = &data->hdev->dev;
1048 struct lcd_device *ldev;
1049
1050 if (!report)
1051 return -ENODEV;
1052 if (report->maxfield != 1 || report->field[0]->report_count != 1 ||
1053 report->field[0]->report_size != 8) {
1054 dev_err(dev, "unsupported CONTRAST report");
1055 return -EINVAL;
1056 }
1057
1058 ldev = lcd_device_register(dev_name(dev), dev, data, &picolcd_lcdops);
1059 if (IS_ERR(ldev)) {
1060 dev_err(dev, "failed to register LCD\n");
1061 return PTR_ERR(ldev);
1062 }
1063 ldev->props.max_contrast = 0x0ff;
1064 data->lcd_contrast = 0xe5;
1065 data->lcd = ldev;
1066 picolcd_set_contrast(ldev, 0xe5);
1067 return 0;
1068}
1069
1070static void picolcd_exit_lcd(struct picolcd_data *data)
1071{
1072 struct lcd_device *ldev = data->lcd;
1073
1074 data->lcd = NULL;
1075 if (ldev)
1076 lcd_device_unregister(ldev);
1077}
1078
1079static inline int picolcd_resume_lcd(struct picolcd_data *data)
1080{
1081 if (!data->lcd)
1082 return 0;
1083 return picolcd_set_contrast(data->lcd, data->lcd_contrast);
1084}
1085#else
1086static inline int picolcd_init_lcd(struct picolcd_data *data,
1087 struct hid_report *report)
1088{
1089 return 0;
1090}
1091static inline void picolcd_exit_lcd(struct picolcd_data *data)
1092{
1093}
1094static inline int picolcd_resume_lcd(struct picolcd_data *data)
1095{
1096 return 0;
1097}
Bruno Prémont5435f282010-04-11 12:17:45 +02001098#endif /* CONFIG_HID_PICOLCD_LCD */
Bruno Prémonte8d931b2010-03-30 22:36:07 +02001099
Bruno Prémont5435f282010-04-11 12:17:45 +02001100#ifdef CONFIG_HID_PICOLCD_LEDS
Bruno Prémont467d6522010-03-30 22:36:49 +02001101/**
1102 * LED class device
1103 */
1104static void picolcd_leds_set(struct picolcd_data *data)
1105{
1106 struct hid_report *report;
1107 unsigned long flags;
1108
1109 if (!data->led[0])
1110 return;
1111 report = picolcd_out_report(REPORT_LED_STATE, data->hdev);
1112 if (!report || report->maxfield != 1 || report->field[0]->report_count != 1)
1113 return;
1114
1115 spin_lock_irqsave(&data->lock, flags);
1116 hid_set_field(report->field[0], 0, data->led_state);
1117 usbhid_submit_report(data->hdev, report, USB_DIR_OUT);
1118 spin_unlock_irqrestore(&data->lock, flags);
1119}
1120
1121static void picolcd_led_set_brightness(struct led_classdev *led_cdev,
1122 enum led_brightness value)
1123{
1124 struct device *dev;
1125 struct hid_device *hdev;
1126 struct picolcd_data *data;
1127 int i, state = 0;
1128
1129 dev = led_cdev->dev->parent;
1130 hdev = container_of(dev, struct hid_device, dev);
1131 data = hid_get_drvdata(hdev);
1132 for (i = 0; i < 8; i++) {
1133 if (led_cdev != data->led[i])
1134 continue;
1135 state = (data->led_state >> i) & 1;
1136 if (value == LED_OFF && state) {
1137 data->led_state &= ~(1 << i);
1138 picolcd_leds_set(data);
1139 } else if (value != LED_OFF && !state) {
1140 data->led_state |= 1 << i;
1141 picolcd_leds_set(data);
1142 }
1143 break;
1144 }
1145}
1146
1147static enum led_brightness picolcd_led_get_brightness(struct led_classdev *led_cdev)
1148{
1149 struct device *dev;
1150 struct hid_device *hdev;
1151 struct picolcd_data *data;
1152 int i, value = 0;
1153
1154 dev = led_cdev->dev->parent;
1155 hdev = container_of(dev, struct hid_device, dev);
1156 data = hid_get_drvdata(hdev);
1157 for (i = 0; i < 8; i++)
1158 if (led_cdev == data->led[i]) {
1159 value = (data->led_state >> i) & 1;
1160 break;
1161 }
1162 return value ? LED_FULL : LED_OFF;
1163}
1164
1165static int picolcd_init_leds(struct picolcd_data *data, struct hid_report *report)
1166{
1167 struct device *dev = &data->hdev->dev;
1168 struct led_classdev *led;
1169 size_t name_sz = strlen(dev_name(dev)) + 8;
1170 char *name;
1171 int i, ret = 0;
1172
1173 if (!report)
1174 return -ENODEV;
1175 if (report->maxfield != 1 || report->field[0]->report_count != 1 ||
1176 report->field[0]->report_size != 8) {
1177 dev_err(dev, "unsupported LED_STATE report");
1178 return -EINVAL;
1179 }
1180
1181 for (i = 0; i < 8; i++) {
1182 led = kzalloc(sizeof(struct led_classdev)+name_sz, GFP_KERNEL);
1183 if (!led) {
1184 dev_err(dev, "can't allocate memory for LED %d\n", i);
1185 ret = -ENOMEM;
1186 goto err;
1187 }
1188 name = (void *)(&led[1]);
1189 snprintf(name, name_sz, "%s::GPO%d", dev_name(dev), i);
1190 led->name = name;
1191 led->brightness = 0;
1192 led->max_brightness = 1;
1193 led->brightness_get = picolcd_led_get_brightness;
1194 led->brightness_set = picolcd_led_set_brightness;
1195
1196 data->led[i] = led;
1197 ret = led_classdev_register(dev, data->led[i]);
1198 if (ret) {
1199 data->led[i] = NULL;
1200 kfree(led);
1201 dev_err(dev, "can't register LED %d\n", i);
1202 goto err;
1203 }
1204 }
1205 return 0;
1206err:
1207 for (i = 0; i < 8; i++)
1208 if (data->led[i]) {
1209 led = data->led[i];
1210 data->led[i] = NULL;
1211 led_classdev_unregister(led);
1212 kfree(led);
1213 }
1214 return ret;
1215}
1216
1217static void picolcd_exit_leds(struct picolcd_data *data)
1218{
1219 struct led_classdev *led;
1220 int i;
1221
1222 for (i = 0; i < 8; i++) {
1223 led = data->led[i];
1224 data->led[i] = NULL;
1225 if (!led)
1226 continue;
1227 led_classdev_unregister(led);
1228 kfree(led);
1229 }
1230}
1231
1232#else
1233static inline int picolcd_init_leds(struct picolcd_data *data,
1234 struct hid_report *report)
1235{
1236 return 0;
1237}
Bruno Prémont9d71ea02010-05-02 16:05:05 +02001238static inline void picolcd_exit_leds(struct picolcd_data *data)
Bruno Prémont467d6522010-03-30 22:36:49 +02001239{
1240}
1241static inline int picolcd_leds_set(struct picolcd_data *data)
1242{
1243 return 0;
1244}
Bruno Prémont5435f282010-04-11 12:17:45 +02001245#endif /* CONFIG_HID_PICOLCD_LEDS */
Bruno Prémont467d6522010-03-30 22:36:49 +02001246
Bruno Prémont236db472010-03-30 22:33:50 +02001247/*
1248 * input class device
1249 */
1250static int picolcd_raw_keypad(struct picolcd_data *data,
1251 struct hid_report *report, u8 *raw_data, int size)
1252{
1253 /*
1254 * Keypad event
1255 * First and second data bytes list currently pressed keys,
1256 * 0x00 means no key and at most 2 keys may be pressed at same time
1257 */
1258 int i, j;
1259
1260 /* determine newly pressed keys */
1261 for (i = 0; i < size; i++) {
1262 unsigned int key_code;
1263 if (raw_data[i] == 0)
1264 continue;
1265 for (j = 0; j < sizeof(data->pressed_keys); j++)
1266 if (data->pressed_keys[j] == raw_data[i])
1267 goto key_already_down;
1268 for (j = 0; j < sizeof(data->pressed_keys); j++)
1269 if (data->pressed_keys[j] == 0) {
1270 data->pressed_keys[j] = raw_data[i];
1271 break;
1272 }
1273 input_event(data->input_keys, EV_MSC, MSC_SCAN, raw_data[i]);
1274 if (raw_data[i] < PICOLCD_KEYS)
1275 key_code = data->keycode[raw_data[i]];
1276 else
1277 key_code = KEY_UNKNOWN;
1278 if (key_code != KEY_UNKNOWN) {
1279 dbg_hid(PICOLCD_NAME " got key press for %u:%d",
1280 raw_data[i], key_code);
1281 input_report_key(data->input_keys, key_code, 1);
1282 }
1283 input_sync(data->input_keys);
1284key_already_down:
1285 continue;
1286 }
1287
1288 /* determine newly released keys */
1289 for (j = 0; j < sizeof(data->pressed_keys); j++) {
1290 unsigned int key_code;
1291 if (data->pressed_keys[j] == 0)
1292 continue;
1293 for (i = 0; i < size; i++)
1294 if (data->pressed_keys[j] == raw_data[i])
1295 goto key_still_down;
1296 input_event(data->input_keys, EV_MSC, MSC_SCAN, data->pressed_keys[j]);
1297 if (data->pressed_keys[j] < PICOLCD_KEYS)
1298 key_code = data->keycode[data->pressed_keys[j]];
1299 else
1300 key_code = KEY_UNKNOWN;
1301 if (key_code != KEY_UNKNOWN) {
1302 dbg_hid(PICOLCD_NAME " got key release for %u:%d",
1303 data->pressed_keys[j], key_code);
1304 input_report_key(data->input_keys, key_code, 0);
1305 }
1306 input_sync(data->input_keys);
1307 data->pressed_keys[j] = 0;
1308key_still_down:
1309 continue;
1310 }
1311 return 1;
1312}
1313
1314static int picolcd_raw_cir(struct picolcd_data *data,
1315 struct hid_report *report, u8 *raw_data, int size)
1316{
1317 /* Need understanding of CIR data format to implement ... */
1318 return 1;
1319}
1320
1321static int picolcd_check_version(struct hid_device *hdev)
1322{
1323 struct picolcd_data *data = hid_get_drvdata(hdev);
1324 struct picolcd_pending *verinfo;
1325 int ret = 0;
1326
1327 if (!data)
1328 return -ENODEV;
1329
1330 verinfo = picolcd_send_and_wait(hdev, REPORT_VERSION, NULL, 0);
1331 if (!verinfo) {
Joe Perches4291ee32010-12-09 19:29:03 -08001332 hid_err(hdev, "no version response from PicoLCD\n");
Bruno Prémont236db472010-03-30 22:33:50 +02001333 return -ENODEV;
1334 }
1335
1336 if (verinfo->raw_size == 2) {
Bruno Prémont76d17e62010-04-25 21:31:40 +02001337 data->version[0] = verinfo->raw_data[1];
1338 data->version[1] = verinfo->raw_data[0];
Bruno Prémont236db472010-03-30 22:33:50 +02001339 if (data->status & PICOLCD_BOOTLOADER) {
Joe Perches4291ee32010-12-09 19:29:03 -08001340 hid_info(hdev, "PicoLCD, bootloader version %d.%d\n",
1341 verinfo->raw_data[1], verinfo->raw_data[0]);
Bruno Prémont236db472010-03-30 22:33:50 +02001342 } else {
Joe Perches4291ee32010-12-09 19:29:03 -08001343 hid_info(hdev, "PicoLCD, firmware version %d.%d\n",
1344 verinfo->raw_data[1], verinfo->raw_data[0]);
Bruno Prémont236db472010-03-30 22:33:50 +02001345 }
1346 } else {
Joe Perches4291ee32010-12-09 19:29:03 -08001347 hid_err(hdev, "confused, got unexpected version response from PicoLCD\n");
Bruno Prémont236db472010-03-30 22:33:50 +02001348 ret = -EINVAL;
1349 }
1350 kfree(verinfo);
1351 return ret;
1352}
1353
1354/*
1355 * Reset our device and wait for answer to VERSION request
1356 */
1357static int picolcd_reset(struct hid_device *hdev)
1358{
1359 struct picolcd_data *data = hid_get_drvdata(hdev);
1360 struct hid_report *report = picolcd_out_report(REPORT_RESET, hdev);
1361 unsigned long flags;
Bruno Prémontb8c21cf2010-03-30 22:34:30 +02001362 int error;
Bruno Prémont236db472010-03-30 22:33:50 +02001363
1364 if (!data || !report || report->maxfield != 1)
1365 return -ENODEV;
1366
1367 spin_lock_irqsave(&data->lock, flags);
1368 if (hdev->product == USB_DEVICE_ID_PICOLCD_BOOTLOADER)
1369 data->status |= PICOLCD_BOOTLOADER;
1370
1371 /* perform the reset */
1372 hid_set_field(report->field[0], 0, 1);
1373 usbhid_submit_report(hdev, report, USB_DIR_OUT);
1374 spin_unlock_irqrestore(&data->lock, flags);
1375
Bruno Prémontb8c21cf2010-03-30 22:34:30 +02001376 error = picolcd_check_version(hdev);
1377 if (error)
1378 return error;
1379
Bruno Prémonte8d931b2010-03-30 22:36:07 +02001380 picolcd_resume_lcd(data);
Bruno Prémontf1c21762010-03-30 22:35:27 +02001381 picolcd_resume_backlight(data);
Bruno Prémont5435f282010-04-11 12:17:45 +02001382#ifdef CONFIG_HID_PICOLCD_FB
Bruno Prémontb8c21cf2010-03-30 22:34:30 +02001383 if (data->fb_info)
1384 schedule_delayed_work(&data->fb_info->deferred_work, 0);
Bruno Prémont5435f282010-04-11 12:17:45 +02001385#endif /* CONFIG_HID_PICOLCD_FB */
Bruno Prémontb8c21cf2010-03-30 22:34:30 +02001386
Bruno Prémont467d6522010-03-30 22:36:49 +02001387 picolcd_leds_set(data);
Bruno Prémontb8c21cf2010-03-30 22:34:30 +02001388 return 0;
Bruno Prémont236db472010-03-30 22:33:50 +02001389}
1390
1391/*
1392 * The "operation_mode" sysfs attribute
1393 */
1394static ssize_t picolcd_operation_mode_show(struct device *dev,
1395 struct device_attribute *attr, char *buf)
1396{
1397 struct picolcd_data *data = dev_get_drvdata(dev);
1398
1399 if (data->status & PICOLCD_BOOTLOADER)
1400 return snprintf(buf, PAGE_SIZE, "[bootloader] lcd\n");
1401 else
1402 return snprintf(buf, PAGE_SIZE, "bootloader [lcd]\n");
1403}
1404
1405static ssize_t picolcd_operation_mode_store(struct device *dev,
1406 struct device_attribute *attr, const char *buf, size_t count)
1407{
1408 struct picolcd_data *data = dev_get_drvdata(dev);
1409 struct hid_report *report = NULL;
1410 size_t cnt = count;
Bruno Prémont0b5adf92010-04-25 21:29:16 +02001411 int timeout = data->opmode_delay;
Bruno Prémont236db472010-03-30 22:33:50 +02001412 unsigned long flags;
1413
1414 if (cnt >= 3 && strncmp("lcd", buf, 3) == 0) {
1415 if (data->status & PICOLCD_BOOTLOADER)
1416 report = picolcd_out_report(REPORT_EXIT_FLASHER, data->hdev);
1417 buf += 3;
1418 cnt -= 3;
1419 } else if (cnt >= 10 && strncmp("bootloader", buf, 10) == 0) {
1420 if (!(data->status & PICOLCD_BOOTLOADER))
1421 report = picolcd_out_report(REPORT_EXIT_KEYBOARD, data->hdev);
1422 buf += 10;
1423 cnt -= 10;
1424 }
1425 if (!report)
1426 return -EINVAL;
1427
Bruno Prémont236db472010-03-30 22:33:50 +02001428 while (cnt > 0 && (buf[cnt-1] == '\n' || buf[cnt-1] == '\r'))
1429 cnt--;
Bruno Prémont0b5adf92010-04-25 21:29:16 +02001430 if (cnt != 0)
1431 return -EINVAL;
Bruno Prémont236db472010-03-30 22:33:50 +02001432
1433 spin_lock_irqsave(&data->lock, flags);
1434 hid_set_field(report->field[0], 0, timeout & 0xff);
1435 hid_set_field(report->field[0], 1, (timeout >> 8) & 0xff);
1436 usbhid_submit_report(data->hdev, report, USB_DIR_OUT);
1437 spin_unlock_irqrestore(&data->lock, flags);
1438 return count;
1439}
1440
1441static DEVICE_ATTR(operation_mode, 0644, picolcd_operation_mode_show,
1442 picolcd_operation_mode_store);
1443
Bruno Prémont0b5adf92010-04-25 21:29:16 +02001444/*
1445 * The "operation_mode_delay" sysfs attribute
1446 */
1447static ssize_t picolcd_operation_mode_delay_show(struct device *dev,
1448 struct device_attribute *attr, char *buf)
1449{
1450 struct picolcd_data *data = dev_get_drvdata(dev);
1451
1452 return snprintf(buf, PAGE_SIZE, "%hu\n", data->opmode_delay);
1453}
1454
1455static ssize_t picolcd_operation_mode_delay_store(struct device *dev,
1456 struct device_attribute *attr, const char *buf, size_t count)
1457{
1458 struct picolcd_data *data = dev_get_drvdata(dev);
1459 unsigned u;
1460 if (sscanf(buf, "%u", &u) != 1)
1461 return -EINVAL;
1462 if (u > 30000)
1463 return -EINVAL;
1464 else
1465 data->opmode_delay = u;
1466 return count;
1467}
1468
1469static DEVICE_ATTR(operation_mode_delay, 0644, picolcd_operation_mode_delay_show,
1470 picolcd_operation_mode_delay_store);
1471
Bruno Prémont236db472010-03-30 22:33:50 +02001472
1473#ifdef CONFIG_DEBUG_FS
1474/*
Bruno Prémont9bbf2b92010-03-30 22:38:09 +02001475 * The "reset" file
1476 */
1477static int picolcd_debug_reset_show(struct seq_file *f, void *p)
1478{
1479 if (picolcd_fbinfo((struct picolcd_data *)f->private))
1480 seq_printf(f, "all fb\n");
1481 else
1482 seq_printf(f, "all\n");
1483 return 0;
1484}
1485
1486static int picolcd_debug_reset_open(struct inode *inode, struct file *f)
1487{
1488 return single_open(f, picolcd_debug_reset_show, inode->i_private);
1489}
1490
1491static ssize_t picolcd_debug_reset_write(struct file *f, const char __user *user_buf,
1492 size_t count, loff_t *ppos)
1493{
1494 struct picolcd_data *data = ((struct seq_file *)f->private_data)->private;
1495 char buf[32];
1496 size_t cnt = min(count, sizeof(buf)-1);
1497 if (copy_from_user(buf, user_buf, cnt))
1498 return -EFAULT;
1499
1500 while (cnt > 0 && (buf[cnt-1] == ' ' || buf[cnt-1] == '\n'))
1501 cnt--;
1502 buf[cnt] = '\0';
1503 if (strcmp(buf, "all") == 0) {
1504 picolcd_reset(data->hdev);
1505 picolcd_fb_reset(data, 1);
1506 } else if (strcmp(buf, "fb") == 0) {
1507 picolcd_fb_reset(data, 1);
1508 } else {
1509 return -EINVAL;
1510 }
1511 return count;
1512}
1513
1514static const struct file_operations picolcd_debug_reset_fops = {
1515 .owner = THIS_MODULE,
1516 .open = picolcd_debug_reset_open,
1517 .read = seq_read,
1518 .llseek = seq_lseek,
1519 .write = picolcd_debug_reset_write,
1520 .release = single_release,
1521};
1522
1523/*
1524 * The "eeprom" file
1525 */
1526static int picolcd_debug_eeprom_open(struct inode *i, struct file *f)
1527{
1528 f->private_data = i->i_private;
1529 return 0;
1530}
1531
1532static ssize_t picolcd_debug_eeprom_read(struct file *f, char __user *u,
1533 size_t s, loff_t *off)
1534{
1535 struct picolcd_data *data = f->private_data;
1536 struct picolcd_pending *resp;
1537 u8 raw_data[3];
1538 ssize_t ret = -EIO;
1539
1540 if (s == 0)
1541 return -EINVAL;
1542 if (*off > 0x0ff)
1543 return 0;
1544
1545 /* prepare buffer with info about what we want to read (addr & len) */
1546 raw_data[0] = *off & 0xff;
David Sterba86280a22010-12-27 16:27:43 +01001547 raw_data[1] = (*off >> 8) & 0xff;
Bruno Prémont9bbf2b92010-03-30 22:38:09 +02001548 raw_data[2] = s < 20 ? s : 20;
1549 if (*off + raw_data[2] > 0xff)
1550 raw_data[2] = 0x100 - *off;
1551 resp = picolcd_send_and_wait(data->hdev, REPORT_EE_READ, raw_data,
1552 sizeof(raw_data));
1553 if (!resp)
1554 return -EIO;
1555
1556 if (resp->in_report && resp->in_report->id == REPORT_EE_DATA) {
1557 /* successful read :) */
1558 ret = resp->raw_data[2];
1559 if (ret > s)
1560 ret = s;
1561 if (copy_to_user(u, resp->raw_data+3, ret))
1562 ret = -EFAULT;
1563 else
1564 *off += ret;
1565 } /* anything else is some kind of IO error */
1566
1567 kfree(resp);
1568 return ret;
1569}
1570
1571static ssize_t picolcd_debug_eeprom_write(struct file *f, const char __user *u,
1572 size_t s, loff_t *off)
1573{
1574 struct picolcd_data *data = f->private_data;
1575 struct picolcd_pending *resp;
1576 ssize_t ret = -EIO;
1577 u8 raw_data[23];
1578
1579 if (s == 0)
1580 return -EINVAL;
1581 if (*off > 0x0ff)
1582 return -ENOSPC;
1583
1584 memset(raw_data, 0, sizeof(raw_data));
1585 raw_data[0] = *off & 0xff;
David Sterba86280a22010-12-27 16:27:43 +01001586 raw_data[1] = (*off >> 8) & 0xff;
Bruno Prémont9bbf2b92010-03-30 22:38:09 +02001587 raw_data[2] = s < 20 ? s : 20;
1588 if (*off + raw_data[2] > 0xff)
1589 raw_data[2] = 0x100 - *off;
1590
1591 if (copy_from_user(raw_data+3, u, raw_data[2]))
1592 return -EFAULT;
1593 resp = picolcd_send_and_wait(data->hdev, REPORT_EE_WRITE, raw_data,
1594 sizeof(raw_data));
1595
1596 if (!resp)
1597 return -EIO;
1598
1599 if (resp->in_report && resp->in_report->id == REPORT_EE_DATA) {
1600 /* check if written data matches */
1601 if (memcmp(raw_data, resp->raw_data, 3+raw_data[2]) == 0) {
1602 *off += raw_data[2];
1603 ret = raw_data[2];
1604 }
1605 }
1606 kfree(resp);
1607 return ret;
1608}
1609
1610/*
1611 * Notes:
1612 * - read/write happens in chunks of at most 20 bytes, it's up to userspace
1613 * to loop in order to get more data.
1614 * - on write errors on otherwise correct write request the bytes
1615 * that should have been written are in undefined state.
1616 */
1617static const struct file_operations picolcd_debug_eeprom_fops = {
1618 .owner = THIS_MODULE,
1619 .open = picolcd_debug_eeprom_open,
1620 .read = picolcd_debug_eeprom_read,
1621 .write = picolcd_debug_eeprom_write,
1622 .llseek = generic_file_llseek,
1623};
1624
1625/*
1626 * The "flash" file
1627 */
1628static int picolcd_debug_flash_open(struct inode *i, struct file *f)
1629{
1630 f->private_data = i->i_private;
1631 return 0;
1632}
1633
1634/* record a flash address to buf (bounds check to be done by caller) */
1635static int _picolcd_flash_setaddr(struct picolcd_data *data, u8 *buf, long off)
1636{
1637 buf[0] = off & 0xff;
1638 buf[1] = (off >> 8) & 0xff;
1639 if (data->addr_sz == 3)
1640 buf[2] = (off >> 16) & 0xff;
1641 return data->addr_sz == 2 ? 2 : 3;
1642}
1643
1644/* read a given size of data (bounds check to be done by caller) */
1645static ssize_t _picolcd_flash_read(struct picolcd_data *data, int report_id,
1646 char __user *u, size_t s, loff_t *off)
1647{
1648 struct picolcd_pending *resp;
1649 u8 raw_data[4];
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 resp = picolcd_send_and_wait(data->hdev, report_id, raw_data, len_off+1);
1658 if (!resp || !resp->in_report)
1659 goto skip;
1660 if (resp->in_report->id == REPORT_MEMORY ||
1661 resp->in_report->id == REPORT_BL_READ_MEMORY) {
1662 if (memcmp(raw_data, resp->raw_data, len_off+1) != 0)
1663 goto skip;
1664 if (copy_to_user(u+ret, resp->raw_data+len_off+1, raw_data[len_off])) {
1665 err = -EFAULT;
1666 goto skip;
1667 }
1668 *off += raw_data[len_off];
1669 s -= raw_data[len_off];
1670 ret += raw_data[len_off];
1671 err = 0;
1672 }
1673skip:
1674 kfree(resp);
1675 if (err)
1676 return ret > 0 ? ret : err;
1677 }
1678 return ret;
1679}
1680
1681static ssize_t picolcd_debug_flash_read(struct file *f, char __user *u,
1682 size_t s, loff_t *off)
1683{
1684 struct picolcd_data *data = f->private_data;
1685
1686 if (s == 0)
1687 return -EINVAL;
1688 if (*off > 0x05fff)
1689 return 0;
1690 if (*off + s > 0x05fff)
1691 s = 0x06000 - *off;
1692
1693 if (data->status & PICOLCD_BOOTLOADER)
1694 return _picolcd_flash_read(data, REPORT_BL_READ_MEMORY, u, s, off);
1695 else
1696 return _picolcd_flash_read(data, REPORT_READ_MEMORY, u, s, off);
1697}
1698
1699/* erase block aligned to 64bytes boundary */
1700static ssize_t _picolcd_flash_erase64(struct picolcd_data *data, int report_id,
1701 loff_t *off)
1702{
1703 struct picolcd_pending *resp;
1704 u8 raw_data[3];
1705 int len_off;
1706 ssize_t ret = -EIO;
1707
1708 if (*off & 0x3f)
1709 return -EINVAL;
1710
1711 len_off = _picolcd_flash_setaddr(data, raw_data, *off);
1712 resp = picolcd_send_and_wait(data->hdev, report_id, raw_data, len_off);
1713 if (!resp || !resp->in_report)
1714 goto skip;
1715 if (resp->in_report->id == REPORT_MEMORY ||
1716 resp->in_report->id == REPORT_BL_ERASE_MEMORY) {
1717 if (memcmp(raw_data, resp->raw_data, len_off) != 0)
1718 goto skip;
1719 ret = 0;
1720 }
1721skip:
1722 kfree(resp);
1723 return ret;
1724}
1725
1726/* write a given size of data (bounds check to be done by caller) */
1727static ssize_t _picolcd_flash_write(struct picolcd_data *data, int report_id,
1728 const char __user *u, size_t s, loff_t *off)
1729{
1730 struct picolcd_pending *resp;
1731 u8 raw_data[36];
1732 ssize_t ret = 0;
1733 int len_off, err = -EIO;
1734
1735 while (s > 0) {
1736 err = -EIO;
1737 len_off = _picolcd_flash_setaddr(data, raw_data, *off);
1738 raw_data[len_off] = s > 32 ? 32 : s;
1739 if (copy_from_user(raw_data+len_off+1, u, raw_data[len_off])) {
1740 err = -EFAULT;
Julia Lawallaeacb6f2010-05-15 11:46:36 +02001741 break;
Bruno Prémont9bbf2b92010-03-30 22:38:09 +02001742 }
1743 resp = picolcd_send_and_wait(data->hdev, report_id, raw_data,
1744 len_off+1+raw_data[len_off]);
1745 if (!resp || !resp->in_report)
1746 goto skip;
1747 if (resp->in_report->id == REPORT_MEMORY ||
1748 resp->in_report->id == REPORT_BL_WRITE_MEMORY) {
1749 if (memcmp(raw_data, resp->raw_data, len_off+1+raw_data[len_off]) != 0)
1750 goto skip;
1751 *off += raw_data[len_off];
1752 s -= raw_data[len_off];
1753 ret += raw_data[len_off];
1754 err = 0;
1755 }
1756skip:
1757 kfree(resp);
1758 if (err)
1759 break;
1760 }
1761 return ret > 0 ? ret : err;
1762}
1763
1764static ssize_t picolcd_debug_flash_write(struct file *f, const char __user *u,
1765 size_t s, loff_t *off)
1766{
1767 struct picolcd_data *data = f->private_data;
1768 ssize_t err, ret = 0;
1769 int report_erase, report_write;
1770
1771 if (s == 0)
1772 return -EINVAL;
1773 if (*off > 0x5fff)
1774 return -ENOSPC;
1775 if (s & 0x3f)
1776 return -EINVAL;
1777 if (*off & 0x3f)
1778 return -EINVAL;
1779
1780 if (data->status & PICOLCD_BOOTLOADER) {
1781 report_erase = REPORT_BL_ERASE_MEMORY;
1782 report_write = REPORT_BL_WRITE_MEMORY;
1783 } else {
1784 report_erase = REPORT_ERASE_MEMORY;
1785 report_write = REPORT_WRITE_MEMORY;
1786 }
1787 mutex_lock(&data->mutex_flash);
1788 while (s > 0) {
1789 err = _picolcd_flash_erase64(data, report_erase, off);
1790 if (err)
1791 break;
1792 err = _picolcd_flash_write(data, report_write, u, 64, off);
1793 if (err < 0)
1794 break;
1795 ret += err;
1796 *off += err;
1797 s -= err;
1798 if (err != 64)
1799 break;
1800 }
1801 mutex_unlock(&data->mutex_flash);
1802 return ret > 0 ? ret : err;
1803}
1804
1805/*
1806 * Notes:
1807 * - concurrent writing is prevented by mutex and all writes must be
1808 * n*64 bytes and 64-byte aligned, each write being preceeded by an
1809 * ERASE which erases a 64byte block.
1810 * If less than requested was written or an error is returned for an
1811 * otherwise correct write request the next 64-byte block which should
1812 * have been written is in undefined state (mostly: original, erased,
1813 * (half-)written with write error)
1814 * - reading can happend without special restriction
1815 */
1816static const struct file_operations picolcd_debug_flash_fops = {
1817 .owner = THIS_MODULE,
1818 .open = picolcd_debug_flash_open,
1819 .read = picolcd_debug_flash_read,
1820 .write = picolcd_debug_flash_write,
1821 .llseek = generic_file_llseek,
1822};
1823
1824
1825/*
Bruno Prémont236db472010-03-30 22:33:50 +02001826 * Helper code for HID report level dumping/debugging
1827 */
1828static const char *error_codes[] = {
1829 "success", "parameter missing", "data_missing", "block readonly",
1830 "block not erasable", "block too big", "section overflow",
1831 "invalid command length", "invalid data length",
1832};
1833
1834static void dump_buff_as_hex(char *dst, size_t dst_sz, const u8 *data,
1835 const size_t data_len)
1836{
1837 int i, j;
1838 for (i = j = 0; i < data_len && j + 3 < dst_sz; i++) {
1839 dst[j++] = hex_asc[(data[i] >> 4) & 0x0f];
1840 dst[j++] = hex_asc[data[i] & 0x0f];
1841 dst[j++] = ' ';
1842 }
1843 if (j < dst_sz) {
1844 dst[j--] = '\0';
1845 dst[j] = '\n';
1846 } else
1847 dst[j] = '\0';
1848}
1849
1850static void picolcd_debug_out_report(struct picolcd_data *data,
1851 struct hid_device *hdev, struct hid_report *report)
1852{
1853 u8 raw_data[70];
1854 int raw_size = (report->size >> 3) + 1;
1855 char *buff;
1856#define BUFF_SZ 256
1857
1858 /* Avoid unnecessary overhead if debugfs is disabled */
1859 if (!hdev->debug_events)
1860 return;
1861
1862 buff = kmalloc(BUFF_SZ, GFP_ATOMIC);
1863 if (!buff)
1864 return;
1865
1866 snprintf(buff, BUFF_SZ, "\nout report %d (size %d) = ",
1867 report->id, raw_size);
1868 hid_debug_event(hdev, buff);
1869 if (raw_size + 5 > sizeof(raw_data)) {
Jesper Juhl0fbf8ed2011-01-02 22:17:00 +01001870 kfree(buff);
Bruno Prémont236db472010-03-30 22:33:50 +02001871 hid_debug_event(hdev, " TOO BIG\n");
1872 return;
1873 } else {
1874 raw_data[0] = report->id;
1875 hid_output_report(report, raw_data);
1876 dump_buff_as_hex(buff, BUFF_SZ, raw_data, raw_size);
1877 hid_debug_event(hdev, buff);
1878 }
1879
1880 switch (report->id) {
1881 case REPORT_LED_STATE:
1882 /* 1 data byte with GPO state */
1883 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1884 "REPORT_LED_STATE", report->id, raw_size-1);
1885 hid_debug_event(hdev, buff);
1886 snprintf(buff, BUFF_SZ, "\tGPO state: 0x%02x\n", raw_data[1]);
1887 hid_debug_event(hdev, buff);
1888 break;
1889 case REPORT_BRIGHTNESS:
1890 /* 1 data byte with brightness */
1891 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1892 "REPORT_BRIGHTNESS", report->id, raw_size-1);
1893 hid_debug_event(hdev, buff);
1894 snprintf(buff, BUFF_SZ, "\tBrightness: 0x%02x\n", raw_data[1]);
1895 hid_debug_event(hdev, buff);
1896 break;
1897 case REPORT_CONTRAST:
1898 /* 1 data byte with contrast */
1899 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1900 "REPORT_CONTRAST", report->id, raw_size-1);
1901 hid_debug_event(hdev, buff);
1902 snprintf(buff, BUFF_SZ, "\tContrast: 0x%02x\n", raw_data[1]);
1903 hid_debug_event(hdev, buff);
1904 break;
1905 case REPORT_RESET:
1906 /* 2 data bytes with reset duration in ms */
1907 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1908 "REPORT_RESET", report->id, raw_size-1);
1909 hid_debug_event(hdev, buff);
1910 snprintf(buff, BUFF_SZ, "\tDuration: 0x%02x%02x (%dms)\n",
1911 raw_data[2], raw_data[1], raw_data[2] << 8 | raw_data[1]);
1912 hid_debug_event(hdev, buff);
1913 break;
1914 case REPORT_LCD_CMD:
1915 /* 63 data bytes with LCD commands */
1916 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1917 "REPORT_LCD_CMD", report->id, raw_size-1);
1918 hid_debug_event(hdev, buff);
1919 /* TODO: format decoding */
1920 break;
1921 case REPORT_LCD_DATA:
1922 /* 63 data bytes with LCD data */
1923 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1924 "REPORT_LCD_CMD", report->id, raw_size-1);
1925 /* TODO: format decoding */
1926 hid_debug_event(hdev, buff);
1927 break;
1928 case REPORT_LCD_CMD_DATA:
1929 /* 63 data bytes with LCD commands and data */
1930 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1931 "REPORT_LCD_CMD", report->id, raw_size-1);
1932 /* TODO: format decoding */
1933 hid_debug_event(hdev, buff);
1934 break;
1935 case REPORT_EE_READ:
1936 /* 3 data bytes with read area description */
1937 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1938 "REPORT_EE_READ", report->id, raw_size-1);
1939 hid_debug_event(hdev, buff);
1940 snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x\n",
1941 raw_data[2], raw_data[1]);
1942 hid_debug_event(hdev, buff);
1943 snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[3]);
1944 hid_debug_event(hdev, buff);
1945 break;
1946 case REPORT_EE_WRITE:
1947 /* 3+1..20 data bytes with write area description */
1948 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1949 "REPORT_EE_WRITE", report->id, raw_size-1);
1950 hid_debug_event(hdev, buff);
1951 snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x\n",
1952 raw_data[2], raw_data[1]);
1953 hid_debug_event(hdev, buff);
1954 snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[3]);
1955 hid_debug_event(hdev, buff);
1956 if (raw_data[3] == 0) {
1957 snprintf(buff, BUFF_SZ, "\tNo data\n");
1958 } else if (raw_data[3] + 4 <= raw_size) {
1959 snprintf(buff, BUFF_SZ, "\tData: ");
1960 hid_debug_event(hdev, buff);
1961 dump_buff_as_hex(buff, BUFF_SZ, raw_data+4, raw_data[3]);
1962 } else {
1963 snprintf(buff, BUFF_SZ, "\tData overflowed\n");
1964 }
1965 hid_debug_event(hdev, buff);
1966 break;
1967 case REPORT_ERASE_MEMORY:
1968 case REPORT_BL_ERASE_MEMORY:
1969 /* 3 data bytes with pointer inside erase block */
1970 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1971 "REPORT_ERASE_MEMORY", report->id, raw_size-1);
1972 hid_debug_event(hdev, buff);
1973 switch (data->addr_sz) {
1974 case 2:
1975 snprintf(buff, BUFF_SZ, "\tAddress inside 64 byte block: 0x%02x%02x\n",
1976 raw_data[2], raw_data[1]);
1977 break;
1978 case 3:
1979 snprintf(buff, BUFF_SZ, "\tAddress inside 64 byte block: 0x%02x%02x%02x\n",
1980 raw_data[3], raw_data[2], raw_data[1]);
1981 break;
1982 default:
1983 snprintf(buff, BUFF_SZ, "\tNot supported\n");
1984 }
1985 hid_debug_event(hdev, buff);
1986 break;
1987 case REPORT_READ_MEMORY:
1988 case REPORT_BL_READ_MEMORY:
1989 /* 4 data bytes with read area description */
1990 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
1991 "REPORT_READ_MEMORY", report->id, raw_size-1);
1992 hid_debug_event(hdev, buff);
1993 switch (data->addr_sz) {
1994 case 2:
1995 snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x\n",
1996 raw_data[2], raw_data[1]);
1997 hid_debug_event(hdev, buff);
1998 snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[3]);
1999 break;
2000 case 3:
2001 snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x%02x\n",
2002 raw_data[3], raw_data[2], raw_data[1]);
2003 hid_debug_event(hdev, buff);
2004 snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[4]);
2005 break;
2006 default:
2007 snprintf(buff, BUFF_SZ, "\tNot supported\n");
2008 }
2009 hid_debug_event(hdev, buff);
2010 break;
2011 case REPORT_WRITE_MEMORY:
2012 case REPORT_BL_WRITE_MEMORY:
2013 /* 4+1..32 data bytes with write adrea description */
2014 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
2015 "REPORT_WRITE_MEMORY", report->id, raw_size-1);
2016 hid_debug_event(hdev, buff);
2017 switch (data->addr_sz) {
2018 case 2:
2019 snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x\n",
2020 raw_data[2], raw_data[1]);
2021 hid_debug_event(hdev, buff);
2022 snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[3]);
2023 hid_debug_event(hdev, buff);
2024 if (raw_data[3] == 0) {
2025 snprintf(buff, BUFF_SZ, "\tNo data\n");
2026 } else if (raw_data[3] + 4 <= raw_size) {
2027 snprintf(buff, BUFF_SZ, "\tData: ");
2028 hid_debug_event(hdev, buff);
2029 dump_buff_as_hex(buff, BUFF_SZ, raw_data+4, raw_data[3]);
2030 } else {
2031 snprintf(buff, BUFF_SZ, "\tData overflowed\n");
2032 }
2033 break;
2034 case 3:
2035 snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x%02x\n",
2036 raw_data[3], raw_data[2], raw_data[1]);
2037 hid_debug_event(hdev, buff);
2038 snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[4]);
2039 hid_debug_event(hdev, buff);
2040 if (raw_data[4] == 0) {
2041 snprintf(buff, BUFF_SZ, "\tNo data\n");
2042 } else if (raw_data[4] + 5 <= raw_size) {
2043 snprintf(buff, BUFF_SZ, "\tData: ");
2044 hid_debug_event(hdev, buff);
2045 dump_buff_as_hex(buff, BUFF_SZ, raw_data+5, raw_data[4]);
2046 } else {
2047 snprintf(buff, BUFF_SZ, "\tData overflowed\n");
2048 }
2049 break;
2050 default:
2051 snprintf(buff, BUFF_SZ, "\tNot supported\n");
2052 }
2053 hid_debug_event(hdev, buff);
2054 break;
2055 case REPORT_SPLASH_RESTART:
2056 /* TODO */
2057 break;
2058 case REPORT_EXIT_KEYBOARD:
2059 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
2060 "REPORT_EXIT_KEYBOARD", report->id, raw_size-1);
2061 hid_debug_event(hdev, buff);
2062 snprintf(buff, BUFF_SZ, "\tRestart delay: %dms (0x%02x%02x)\n",
2063 raw_data[1] | (raw_data[2] << 8),
2064 raw_data[2], raw_data[1]);
2065 hid_debug_event(hdev, buff);
2066 break;
2067 case REPORT_VERSION:
2068 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
2069 "REPORT_VERSION", report->id, raw_size-1);
2070 hid_debug_event(hdev, buff);
2071 break;
2072 case REPORT_DEVID:
2073 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
2074 "REPORT_DEVID", report->id, raw_size-1);
2075 hid_debug_event(hdev, buff);
2076 break;
2077 case REPORT_SPLASH_SIZE:
2078 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
2079 "REPORT_SPLASH_SIZE", report->id, raw_size-1);
2080 hid_debug_event(hdev, buff);
2081 break;
2082 case REPORT_HOOK_VERSION:
2083 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
2084 "REPORT_HOOK_VERSION", report->id, raw_size-1);
2085 hid_debug_event(hdev, buff);
2086 break;
2087 case REPORT_EXIT_FLASHER:
2088 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
2089 "REPORT_VERSION", report->id, raw_size-1);
2090 hid_debug_event(hdev, buff);
2091 snprintf(buff, BUFF_SZ, "\tRestart delay: %dms (0x%02x%02x)\n",
2092 raw_data[1] | (raw_data[2] << 8),
2093 raw_data[2], raw_data[1]);
2094 hid_debug_event(hdev, buff);
2095 break;
2096 default:
2097 snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
2098 "<unknown>", report->id, raw_size-1);
2099 hid_debug_event(hdev, buff);
2100 break;
2101 }
2102 wake_up_interruptible(&hdev->debug_wait);
2103 kfree(buff);
2104}
2105
2106static void picolcd_debug_raw_event(struct picolcd_data *data,
2107 struct hid_device *hdev, struct hid_report *report,
2108 u8 *raw_data, int size)
2109{
2110 char *buff;
2111
2112#define BUFF_SZ 256
2113 /* Avoid unnecessary overhead if debugfs is disabled */
2114 if (!hdev->debug_events)
2115 return;
2116
2117 buff = kmalloc(BUFF_SZ, GFP_ATOMIC);
2118 if (!buff)
2119 return;
2120
2121 switch (report->id) {
2122 case REPORT_ERROR_CODE:
2123 /* 2 data bytes with affected report and error code */
2124 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2125 "REPORT_ERROR_CODE", report->id, size-1);
2126 hid_debug_event(hdev, buff);
2127 if (raw_data[2] < ARRAY_SIZE(error_codes))
2128 snprintf(buff, BUFF_SZ, "\tError code 0x%02x (%s) in reply to report 0x%02x\n",
2129 raw_data[2], error_codes[raw_data[2]], raw_data[1]);
2130 else
2131 snprintf(buff, BUFF_SZ, "\tError code 0x%02x in reply to report 0x%02x\n",
2132 raw_data[2], raw_data[1]);
2133 hid_debug_event(hdev, buff);
2134 break;
2135 case REPORT_KEY_STATE:
2136 /* 2 data bytes with key state */
2137 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2138 "REPORT_KEY_STATE", report->id, size-1);
2139 hid_debug_event(hdev, buff);
2140 if (raw_data[1] == 0)
2141 snprintf(buff, BUFF_SZ, "\tNo key pressed\n");
2142 else if (raw_data[2] == 0)
2143 snprintf(buff, BUFF_SZ, "\tOne key pressed: 0x%02x (%d)\n",
2144 raw_data[1], raw_data[1]);
2145 else
2146 snprintf(buff, BUFF_SZ, "\tTwo keys pressed: 0x%02x (%d), 0x%02x (%d)\n",
2147 raw_data[1], raw_data[1], raw_data[2], raw_data[2]);
2148 hid_debug_event(hdev, buff);
2149 break;
2150 case REPORT_IR_DATA:
2151 /* Up to 20 byes of IR scancode data */
2152 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2153 "REPORT_IR_DATA", report->id, size-1);
2154 hid_debug_event(hdev, buff);
2155 if (raw_data[1] == 0) {
2156 snprintf(buff, BUFF_SZ, "\tUnexpectedly 0 data length\n");
2157 hid_debug_event(hdev, buff);
2158 } else if (raw_data[1] + 1 <= size) {
2159 snprintf(buff, BUFF_SZ, "\tData length: %d\n\tIR Data: ",
2160 raw_data[1]-1);
2161 hid_debug_event(hdev, buff);
2162 dump_buff_as_hex(buff, BUFF_SZ, raw_data+2, raw_data[1]-1);
2163 hid_debug_event(hdev, buff);
2164 } else {
2165 snprintf(buff, BUFF_SZ, "\tOverflowing data length: %d\n",
2166 raw_data[1]-1);
2167 hid_debug_event(hdev, buff);
2168 }
2169 break;
2170 case REPORT_EE_DATA:
2171 /* Data buffer in response to REPORT_EE_READ or REPORT_EE_WRITE */
2172 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2173 "REPORT_EE_DATA", report->id, size-1);
2174 hid_debug_event(hdev, buff);
2175 snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x\n",
2176 raw_data[2], raw_data[1]);
2177 hid_debug_event(hdev, buff);
2178 snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[3]);
2179 hid_debug_event(hdev, buff);
2180 if (raw_data[3] == 0) {
2181 snprintf(buff, BUFF_SZ, "\tNo data\n");
2182 hid_debug_event(hdev, buff);
2183 } else if (raw_data[3] + 4 <= size) {
2184 snprintf(buff, BUFF_SZ, "\tData: ");
2185 hid_debug_event(hdev, buff);
2186 dump_buff_as_hex(buff, BUFF_SZ, raw_data+4, raw_data[3]);
2187 hid_debug_event(hdev, buff);
2188 } else {
2189 snprintf(buff, BUFF_SZ, "\tData overflowed\n");
2190 hid_debug_event(hdev, buff);
2191 }
2192 break;
2193 case REPORT_MEMORY:
2194 /* Data buffer in response to REPORT_READ_MEMORY or REPORT_WRTIE_MEMORY */
2195 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2196 "REPORT_MEMORY", report->id, size-1);
2197 hid_debug_event(hdev, buff);
2198 switch (data->addr_sz) {
2199 case 2:
2200 snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x\n",
2201 raw_data[2], raw_data[1]);
2202 hid_debug_event(hdev, buff);
2203 snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[3]);
2204 hid_debug_event(hdev, buff);
2205 if (raw_data[3] == 0) {
2206 snprintf(buff, BUFF_SZ, "\tNo data\n");
2207 } else if (raw_data[3] + 4 <= size) {
2208 snprintf(buff, BUFF_SZ, "\tData: ");
2209 hid_debug_event(hdev, buff);
2210 dump_buff_as_hex(buff, BUFF_SZ, raw_data+4, raw_data[3]);
2211 } else {
2212 snprintf(buff, BUFF_SZ, "\tData overflowed\n");
2213 }
2214 break;
2215 case 3:
2216 snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x%02x\n",
2217 raw_data[3], raw_data[2], raw_data[1]);
2218 hid_debug_event(hdev, buff);
2219 snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[4]);
2220 hid_debug_event(hdev, buff);
2221 if (raw_data[4] == 0) {
2222 snprintf(buff, BUFF_SZ, "\tNo data\n");
2223 } else if (raw_data[4] + 5 <= size) {
2224 snprintf(buff, BUFF_SZ, "\tData: ");
2225 hid_debug_event(hdev, buff);
2226 dump_buff_as_hex(buff, BUFF_SZ, raw_data+5, raw_data[4]);
2227 } else {
2228 snprintf(buff, BUFF_SZ, "\tData overflowed\n");
2229 }
2230 break;
2231 default:
2232 snprintf(buff, BUFF_SZ, "\tNot supported\n");
2233 }
2234 hid_debug_event(hdev, buff);
2235 break;
2236 case REPORT_VERSION:
2237 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2238 "REPORT_VERSION", report->id, size-1);
2239 hid_debug_event(hdev, buff);
2240 snprintf(buff, BUFF_SZ, "\tFirmware version: %d.%d\n",
2241 raw_data[2], raw_data[1]);
2242 hid_debug_event(hdev, buff);
2243 break;
2244 case REPORT_BL_ERASE_MEMORY:
2245 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2246 "REPORT_BL_ERASE_MEMORY", report->id, size-1);
2247 hid_debug_event(hdev, buff);
2248 /* TODO */
2249 break;
2250 case REPORT_BL_READ_MEMORY:
2251 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2252 "REPORT_BL_READ_MEMORY", report->id, size-1);
2253 hid_debug_event(hdev, buff);
2254 /* TODO */
2255 break;
2256 case REPORT_BL_WRITE_MEMORY:
2257 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2258 "REPORT_BL_WRITE_MEMORY", report->id, size-1);
2259 hid_debug_event(hdev, buff);
2260 /* TODO */
2261 break;
2262 case REPORT_DEVID:
2263 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2264 "REPORT_DEVID", report->id, size-1);
2265 hid_debug_event(hdev, buff);
2266 snprintf(buff, BUFF_SZ, "\tSerial: 0x%02x%02x%02x%02x\n",
2267 raw_data[1], raw_data[2], raw_data[3], raw_data[4]);
2268 hid_debug_event(hdev, buff);
2269 snprintf(buff, BUFF_SZ, "\tType: 0x%02x\n",
2270 raw_data[5]);
2271 hid_debug_event(hdev, buff);
2272 break;
2273 case REPORT_SPLASH_SIZE:
2274 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2275 "REPORT_SPLASH_SIZE", report->id, size-1);
2276 hid_debug_event(hdev, buff);
2277 snprintf(buff, BUFF_SZ, "\tTotal splash space: %d\n",
2278 (raw_data[2] << 8) | raw_data[1]);
2279 hid_debug_event(hdev, buff);
2280 snprintf(buff, BUFF_SZ, "\tUsed splash space: %d\n",
2281 (raw_data[4] << 8) | raw_data[3]);
2282 hid_debug_event(hdev, buff);
2283 break;
2284 case REPORT_HOOK_VERSION:
2285 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2286 "REPORT_HOOK_VERSION", report->id, size-1);
2287 hid_debug_event(hdev, buff);
2288 snprintf(buff, BUFF_SZ, "\tFirmware version: %d.%d\n",
2289 raw_data[1], raw_data[2]);
2290 hid_debug_event(hdev, buff);
2291 break;
2292 default:
2293 snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
2294 "<unknown>", report->id, size-1);
2295 hid_debug_event(hdev, buff);
2296 break;
2297 }
2298 wake_up_interruptible(&hdev->debug_wait);
2299 kfree(buff);
2300}
Bruno Prémont9bbf2b92010-03-30 22:38:09 +02002301
2302static void picolcd_init_devfs(struct picolcd_data *data,
2303 struct hid_report *eeprom_r, struct hid_report *eeprom_w,
2304 struct hid_report *flash_r, struct hid_report *flash_w,
2305 struct hid_report *reset)
2306{
2307 struct hid_device *hdev = data->hdev;
2308
2309 mutex_init(&data->mutex_flash);
2310
2311 /* reset */
2312 if (reset)
2313 data->debug_reset = debugfs_create_file("reset", 0600,
2314 hdev->debug_dir, data, &picolcd_debug_reset_fops);
2315
2316 /* eeprom */
2317 if (eeprom_r || eeprom_w)
2318 data->debug_eeprom = debugfs_create_file("eeprom",
2319 (eeprom_w ? S_IWUSR : 0) | (eeprom_r ? S_IRUSR : 0),
2320 hdev->debug_dir, data, &picolcd_debug_eeprom_fops);
2321
2322 /* flash */
2323 if (flash_r && flash_r->maxfield == 1 && flash_r->field[0]->report_size == 8)
2324 data->addr_sz = flash_r->field[0]->report_count - 1;
2325 else
2326 data->addr_sz = -1;
2327 if (data->addr_sz == 2 || data->addr_sz == 3) {
2328 data->debug_flash = debugfs_create_file("flash",
2329 (flash_w ? S_IWUSR : 0) | (flash_r ? S_IRUSR : 0),
2330 hdev->debug_dir, data, &picolcd_debug_flash_fops);
2331 } else if (flash_r || flash_w)
Joe Perches4291ee32010-12-09 19:29:03 -08002332 hid_warn(hdev, "Unexpected FLASH access reports, please submit rdesc for review\n");
Bruno Prémont9bbf2b92010-03-30 22:38:09 +02002333}
2334
2335static void picolcd_exit_devfs(struct picolcd_data *data)
2336{
2337 struct dentry *dent;
2338
2339 dent = data->debug_reset;
2340 data->debug_reset = NULL;
2341 if (dent)
2342 debugfs_remove(dent);
2343 dent = data->debug_eeprom;
2344 data->debug_eeprom = NULL;
2345 if (dent)
2346 debugfs_remove(dent);
2347 dent = data->debug_flash;
2348 data->debug_flash = NULL;
2349 if (dent)
2350 debugfs_remove(dent);
2351 mutex_destroy(&data->mutex_flash);
2352}
Bruno Prémont236db472010-03-30 22:33:50 +02002353#else
Bruno Prémont9d71ea02010-05-02 16:05:05 +02002354static inline void picolcd_debug_raw_event(struct picolcd_data *data,
2355 struct hid_device *hdev, struct hid_report *report,
2356 u8 *raw_data, int size)
2357{
2358}
2359static inline void picolcd_init_devfs(struct picolcd_data *data,
2360 struct hid_report *eeprom_r, struct hid_report *eeprom_w,
2361 struct hid_report *flash_r, struct hid_report *flash_w,
2362 struct hid_report *reset)
2363{
2364}
2365static inline void picolcd_exit_devfs(struct picolcd_data *data)
Bruno Prémont9bbf2b92010-03-30 22:38:09 +02002366{
2367}
2368#endif /* CONFIG_DEBUG_FS */
Bruno Prémont236db472010-03-30 22:33:50 +02002369
2370/*
2371 * Handle raw report as sent by device
2372 */
2373static int picolcd_raw_event(struct hid_device *hdev,
2374 struct hid_report *report, u8 *raw_data, int size)
2375{
2376 struct picolcd_data *data = hid_get_drvdata(hdev);
2377 unsigned long flags;
2378 int ret = 0;
2379
2380 if (!data)
2381 return 1;
2382
2383 if (report->id == REPORT_KEY_STATE) {
2384 if (data->input_keys)
2385 ret = picolcd_raw_keypad(data, report, raw_data+1, size-1);
2386 } else if (report->id == REPORT_IR_DATA) {
2387 if (data->input_cir)
2388 ret = picolcd_raw_cir(data, report, raw_data+1, size-1);
2389 } else {
2390 spin_lock_irqsave(&data->lock, flags);
2391 /*
2392 * We let the caller of picolcd_send_and_wait() check if the
2393 * report we got is one of the expected ones or not.
2394 */
2395 if (data->pending) {
2396 memcpy(data->pending->raw_data, raw_data+1, size-1);
2397 data->pending->raw_size = size-1;
2398 data->pending->in_report = report;
2399 complete(&data->pending->ready);
2400 }
2401 spin_unlock_irqrestore(&data->lock, flags);
2402 }
2403
2404 picolcd_debug_raw_event(data, hdev, report, raw_data, size);
2405 return 1;
2406}
2407
Bruno Prémont9d71ea02010-05-02 16:05:05 +02002408#ifdef CONFIG_PM
2409static int picolcd_suspend(struct hid_device *hdev, pm_message_t message)
2410{
2411 if (message.event & PM_EVENT_AUTO)
2412 return 0;
2413
2414 picolcd_suspend_backlight(hid_get_drvdata(hdev));
2415 dbg_hid(PICOLCD_NAME " device ready for suspend\n");
2416 return 0;
2417}
2418
2419static int picolcd_resume(struct hid_device *hdev)
2420{
2421 int ret;
2422 ret = picolcd_resume_backlight(hid_get_drvdata(hdev));
2423 if (ret)
2424 dbg_hid(PICOLCD_NAME " restoring backlight failed: %d\n", ret);
2425 return 0;
2426}
2427
2428static int picolcd_reset_resume(struct hid_device *hdev)
2429{
2430 int ret;
2431 ret = picolcd_reset(hdev);
2432 if (ret)
2433 dbg_hid(PICOLCD_NAME " resetting our device failed: %d\n", ret);
2434 ret = picolcd_fb_reset(hid_get_drvdata(hdev), 0);
2435 if (ret)
2436 dbg_hid(PICOLCD_NAME " restoring framebuffer content failed: %d\n", ret);
2437 ret = picolcd_resume_lcd(hid_get_drvdata(hdev));
2438 if (ret)
2439 dbg_hid(PICOLCD_NAME " restoring lcd failed: %d\n", ret);
2440 ret = picolcd_resume_backlight(hid_get_drvdata(hdev));
2441 if (ret)
2442 dbg_hid(PICOLCD_NAME " restoring backlight failed: %d\n", ret);
2443 picolcd_leds_set(hid_get_drvdata(hdev));
2444 return 0;
2445}
2446#endif
2447
Bruno Prémont236db472010-03-30 22:33:50 +02002448/* initialize keypad input device */
2449static int picolcd_init_keys(struct picolcd_data *data,
2450 struct hid_report *report)
2451{
2452 struct hid_device *hdev = data->hdev;
2453 struct input_dev *idev;
2454 int error, i;
2455
2456 if (!report)
2457 return -ENODEV;
2458 if (report->maxfield != 1 || report->field[0]->report_count != 2 ||
2459 report->field[0]->report_size != 8) {
Joe Perches4291ee32010-12-09 19:29:03 -08002460 hid_err(hdev, "unsupported KEY_STATE report\n");
Bruno Prémont236db472010-03-30 22:33:50 +02002461 return -EINVAL;
2462 }
2463
2464 idev = input_allocate_device();
2465 if (idev == NULL) {
Joe Perches4291ee32010-12-09 19:29:03 -08002466 hid_err(hdev, "failed to allocate input device\n");
Bruno Prémont236db472010-03-30 22:33:50 +02002467 return -ENOMEM;
2468 }
2469 input_set_drvdata(idev, hdev);
2470 memcpy(data->keycode, def_keymap, sizeof(def_keymap));
2471 idev->name = hdev->name;
2472 idev->phys = hdev->phys;
2473 idev->uniq = hdev->uniq;
2474 idev->id.bustype = hdev->bus;
2475 idev->id.vendor = hdev->vendor;
2476 idev->id.product = hdev->product;
2477 idev->id.version = hdev->version;
2478 idev->dev.parent = hdev->dev.parent;
2479 idev->keycode = &data->keycode;
2480 idev->keycodemax = PICOLCD_KEYS;
2481 idev->keycodesize = sizeof(data->keycode[0]);
2482 input_set_capability(idev, EV_MSC, MSC_SCAN);
2483 set_bit(EV_REP, idev->evbit);
2484 for (i = 0; i < PICOLCD_KEYS; i++)
2485 input_set_capability(idev, EV_KEY, data->keycode[i]);
2486 error = input_register_device(idev);
2487 if (error) {
Joe Perches4291ee32010-12-09 19:29:03 -08002488 hid_err(hdev, "error registering the input device\n");
Bruno Prémont236db472010-03-30 22:33:50 +02002489 input_free_device(idev);
2490 return error;
2491 }
2492 data->input_keys = idev;
2493 return 0;
2494}
2495
2496static void picolcd_exit_keys(struct picolcd_data *data)
2497{
2498 struct input_dev *idev = data->input_keys;
2499
2500 data->input_keys = NULL;
2501 if (idev)
2502 input_unregister_device(idev);
2503}
2504
2505/* initialize CIR input device */
2506static inline int picolcd_init_cir(struct picolcd_data *data, struct hid_report *report)
2507{
2508 /* support not implemented yet */
2509 return 0;
2510}
2511
2512static inline void picolcd_exit_cir(struct picolcd_data *data)
2513{
2514}
2515
2516static int picolcd_probe_lcd(struct hid_device *hdev, struct picolcd_data *data)
2517{
Bruno Prémont236db472010-03-30 22:33:50 +02002518 int error;
2519
2520 error = picolcd_check_version(hdev);
2521 if (error)
2522 return error;
2523
2524 if (data->version[0] != 0 && data->version[1] != 3)
Joe Perches4291ee32010-12-09 19:29:03 -08002525 hid_info(hdev, "Device with untested firmware revision, please submit /sys/kernel/debug/hid/%s/rdesc for this device.\n",
2526 dev_name(&hdev->dev));
Bruno Prémont236db472010-03-30 22:33:50 +02002527
2528 /* Setup keypad input device */
2529 error = picolcd_init_keys(data, picolcd_in_report(REPORT_KEY_STATE, hdev));
2530 if (error)
2531 goto err;
2532
2533 /* Setup CIR input device */
2534 error = picolcd_init_cir(data, picolcd_in_report(REPORT_IR_DATA, hdev));
2535 if (error)
2536 goto err;
2537
Bruno Prémontb8c21cf2010-03-30 22:34:30 +02002538 /* Set up the framebuffer device */
2539 error = picolcd_init_framebuffer(data);
2540 if (error)
2541 goto err;
2542
Bruno Prémonte8d931b2010-03-30 22:36:07 +02002543 /* Setup lcd class device */
2544 error = picolcd_init_lcd(data, picolcd_out_report(REPORT_CONTRAST, hdev));
2545 if (error)
2546 goto err;
2547
Bruno Prémontf1c21762010-03-30 22:35:27 +02002548 /* Setup backlight class device */
2549 error = picolcd_init_backlight(data, picolcd_out_report(REPORT_BRIGHTNESS, hdev));
2550 if (error)
2551 goto err;
2552
Bruno Prémont467d6522010-03-30 22:36:49 +02002553 /* Setup the LED class devices */
2554 error = picolcd_init_leds(data, picolcd_out_report(REPORT_LED_STATE, hdev));
2555 if (error)
2556 goto err;
2557
Bruno Prémont9bbf2b92010-03-30 22:38:09 +02002558 picolcd_init_devfs(data, picolcd_out_report(REPORT_EE_READ, hdev),
2559 picolcd_out_report(REPORT_EE_WRITE, hdev),
2560 picolcd_out_report(REPORT_READ_MEMORY, hdev),
2561 picolcd_out_report(REPORT_WRITE_MEMORY, hdev),
2562 picolcd_out_report(REPORT_RESET, hdev));
Bruno Prémont236db472010-03-30 22:33:50 +02002563 return 0;
2564err:
Bruno Prémont467d6522010-03-30 22:36:49 +02002565 picolcd_exit_leds(data);
Bruno Prémontf1c21762010-03-30 22:35:27 +02002566 picolcd_exit_backlight(data);
Bruno Prémonte8d931b2010-03-30 22:36:07 +02002567 picolcd_exit_lcd(data);
Bruno Prémontb8c21cf2010-03-30 22:34:30 +02002568 picolcd_exit_framebuffer(data);
Bruno Prémont236db472010-03-30 22:33:50 +02002569 picolcd_exit_cir(data);
2570 picolcd_exit_keys(data);
2571 return error;
2572}
2573
2574static int picolcd_probe_bootloader(struct hid_device *hdev, struct picolcd_data *data)
2575{
Bruno Prémont236db472010-03-30 22:33:50 +02002576 int error;
2577
2578 error = picolcd_check_version(hdev);
2579 if (error)
2580 return error;
2581
2582 if (data->version[0] != 1 && data->version[1] != 0)
Joe Perches4291ee32010-12-09 19:29:03 -08002583 hid_info(hdev, "Device with untested bootloader revision, please submit /sys/kernel/debug/hid/%s/rdesc for this device.\n",
2584 dev_name(&hdev->dev));
Bruno Prémont236db472010-03-30 22:33:50 +02002585
Bruno Prémont9bbf2b92010-03-30 22:38:09 +02002586 picolcd_init_devfs(data, NULL, NULL,
2587 picolcd_out_report(REPORT_BL_READ_MEMORY, hdev),
2588 picolcd_out_report(REPORT_BL_WRITE_MEMORY, hdev), NULL);
Bruno Prémont236db472010-03-30 22:33:50 +02002589 return 0;
2590}
2591
2592static int picolcd_probe(struct hid_device *hdev,
2593 const struct hid_device_id *id)
2594{
2595 struct picolcd_data *data;
2596 int error = -ENOMEM;
2597
2598 dbg_hid(PICOLCD_NAME " hardware probe...\n");
2599
2600 /*
2601 * Let's allocate the picolcd data structure, set some reasonable
2602 * defaults, and associate it with the device
2603 */
2604 data = kzalloc(sizeof(struct picolcd_data), GFP_KERNEL);
2605 if (data == NULL) {
Joe Perches4291ee32010-12-09 19:29:03 -08002606 hid_err(hdev, "can't allocate space for Minibox PicoLCD device data\n");
Bruno Prémont236db472010-03-30 22:33:50 +02002607 error = -ENOMEM;
2608 goto err_no_cleanup;
2609 }
2610
2611 spin_lock_init(&data->lock);
2612 mutex_init(&data->mutex);
2613 data->hdev = hdev;
Bruno Prémont0b5adf92010-04-25 21:29:16 +02002614 data->opmode_delay = 5000;
Bruno Prémont236db472010-03-30 22:33:50 +02002615 if (hdev->product == USB_DEVICE_ID_PICOLCD_BOOTLOADER)
2616 data->status |= PICOLCD_BOOTLOADER;
2617 hid_set_drvdata(hdev, data);
2618
2619 /* Parse the device reports and start it up */
2620 error = hid_parse(hdev);
2621 if (error) {
Joe Perches4291ee32010-12-09 19:29:03 -08002622 hid_err(hdev, "device report parse failed\n");
Bruno Prémont236db472010-03-30 22:33:50 +02002623 goto err_cleanup_data;
2624 }
2625
2626 /* We don't use hidinput but hid_hw_start() fails if nothing is
2627 * claimed. So spoof claimed input. */
2628 hdev->claimed = HID_CLAIMED_INPUT;
2629 error = hid_hw_start(hdev, 0);
2630 hdev->claimed = 0;
2631 if (error) {
Joe Perches4291ee32010-12-09 19:29:03 -08002632 hid_err(hdev, "hardware start failed\n");
Bruno Prémont236db472010-03-30 22:33:50 +02002633 goto err_cleanup_data;
2634 }
2635
Dmitry Torokhov5bea7662010-12-07 23:02:48 -08002636 error = hid_hw_open(hdev);
Bruno Prémont236db472010-03-30 22:33:50 +02002637 if (error) {
Joe Perches4291ee32010-12-09 19:29:03 -08002638 hid_err(hdev, "failed to open input interrupt pipe for key and IR events\n");
Bruno Prémont236db472010-03-30 22:33:50 +02002639 goto err_cleanup_hid_hw;
2640 }
2641
Bruno Prémont0b5adf92010-04-25 21:29:16 +02002642 error = device_create_file(&hdev->dev, &dev_attr_operation_mode_delay);
Bruno Prémont236db472010-03-30 22:33:50 +02002643 if (error) {
Joe Perches4291ee32010-12-09 19:29:03 -08002644 hid_err(hdev, "failed to create sysfs attributes\n");
Bruno Prémont236db472010-03-30 22:33:50 +02002645 goto err_cleanup_hid_ll;
2646 }
2647
Bruno Prémont0b5adf92010-04-25 21:29:16 +02002648 error = device_create_file(&hdev->dev, &dev_attr_operation_mode);
2649 if (error) {
Joe Perches4291ee32010-12-09 19:29:03 -08002650 hid_err(hdev, "failed to create sysfs attributes\n");
Bruno Prémont0b5adf92010-04-25 21:29:16 +02002651 goto err_cleanup_sysfs1;
2652 }
2653
Bruno Prémont236db472010-03-30 22:33:50 +02002654 if (data->status & PICOLCD_BOOTLOADER)
2655 error = picolcd_probe_bootloader(hdev, data);
2656 else
2657 error = picolcd_probe_lcd(hdev, data);
2658 if (error)
Bruno Prémont0b5adf92010-04-25 21:29:16 +02002659 goto err_cleanup_sysfs2;
Bruno Prémont236db472010-03-30 22:33:50 +02002660
2661 dbg_hid(PICOLCD_NAME " activated and initialized\n");
2662 return 0;
2663
Bruno Prémont0b5adf92010-04-25 21:29:16 +02002664err_cleanup_sysfs2:
Bruno Prémont236db472010-03-30 22:33:50 +02002665 device_remove_file(&hdev->dev, &dev_attr_operation_mode);
Bruno Prémont0b5adf92010-04-25 21:29:16 +02002666err_cleanup_sysfs1:
2667 device_remove_file(&hdev->dev, &dev_attr_operation_mode_delay);
Bruno Prémont236db472010-03-30 22:33:50 +02002668err_cleanup_hid_ll:
Dmitry Torokhov5bea7662010-12-07 23:02:48 -08002669 hid_hw_close(hdev);
Bruno Prémont236db472010-03-30 22:33:50 +02002670err_cleanup_hid_hw:
2671 hid_hw_stop(hdev);
2672err_cleanup_data:
2673 kfree(data);
2674err_no_cleanup:
2675 hid_set_drvdata(hdev, NULL);
2676
2677 return error;
2678}
2679
2680static void picolcd_remove(struct hid_device *hdev)
2681{
2682 struct picolcd_data *data = hid_get_drvdata(hdev);
2683 unsigned long flags;
2684
2685 dbg_hid(PICOLCD_NAME " hardware remove...\n");
2686 spin_lock_irqsave(&data->lock, flags);
2687 data->status |= PICOLCD_FAILED;
2688 spin_unlock_irqrestore(&data->lock, flags);
Bruno Prémont225b4592010-06-28 22:33:27 +02002689#ifdef CONFIG_HID_PICOLCD_FB
2690 /* short-circuit FB as early as possible in order to
2691 * avoid long delays if we host console.
2692 */
2693 if (data->fb_info)
2694 data->fb_info->par = NULL;
2695#endif
Bruno Prémont236db472010-03-30 22:33:50 +02002696
Bruno Prémont9bbf2b92010-03-30 22:38:09 +02002697 picolcd_exit_devfs(data);
Bruno Prémont236db472010-03-30 22:33:50 +02002698 device_remove_file(&hdev->dev, &dev_attr_operation_mode);
Bruno Prémont0b5adf92010-04-25 21:29:16 +02002699 device_remove_file(&hdev->dev, &dev_attr_operation_mode_delay);
Dmitry Torokhov5bea7662010-12-07 23:02:48 -08002700 hid_hw_close(hdev);
Bruno Prémont236db472010-03-30 22:33:50 +02002701 hid_hw_stop(hdev);
2702 hid_set_drvdata(hdev, NULL);
2703
2704 /* Shortcut potential pending reply that will never arrive */
2705 spin_lock_irqsave(&data->lock, flags);
2706 if (data->pending)
2707 complete(&data->pending->ready);
2708 spin_unlock_irqrestore(&data->lock, flags);
2709
Bruno Prémont467d6522010-03-30 22:36:49 +02002710 /* Cleanup LED */
2711 picolcd_exit_leds(data);
Bruno Prémontb8c21cf2010-03-30 22:34:30 +02002712 /* Clean up the framebuffer */
Bruno Prémontf1c21762010-03-30 22:35:27 +02002713 picolcd_exit_backlight(data);
Bruno Prémonte8d931b2010-03-30 22:36:07 +02002714 picolcd_exit_lcd(data);
Bruno Prémontb8c21cf2010-03-30 22:34:30 +02002715 picolcd_exit_framebuffer(data);
Bruno Prémont236db472010-03-30 22:33:50 +02002716 /* Cleanup input */
2717 picolcd_exit_cir(data);
2718 picolcd_exit_keys(data);
2719
2720 mutex_destroy(&data->mutex);
2721 /* Finally, clean up the picolcd data itself */
2722 kfree(data);
2723}
2724
2725static const struct hid_device_id picolcd_devices[] = {
2726 { HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP, USB_DEVICE_ID_PICOLCD) },
2727 { HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP, USB_DEVICE_ID_PICOLCD_BOOTLOADER) },
2728 { }
2729};
2730MODULE_DEVICE_TABLE(hid, picolcd_devices);
2731
2732static struct hid_driver picolcd_driver = {
2733 .name = "hid-picolcd",
2734 .id_table = picolcd_devices,
2735 .probe = picolcd_probe,
2736 .remove = picolcd_remove,
2737 .raw_event = picolcd_raw_event,
Bruno Prémont9d71ea02010-05-02 16:05:05 +02002738#ifdef CONFIG_PM
2739 .suspend = picolcd_suspend,
2740 .resume = picolcd_resume,
2741 .reset_resume = picolcd_reset_resume,
2742#endif
Bruno Prémont236db472010-03-30 22:33:50 +02002743};
2744
2745static int __init picolcd_init(void)
2746{
2747 return hid_register_driver(&picolcd_driver);
2748}
2749
2750static void __exit picolcd_exit(void)
2751{
2752 hid_unregister_driver(&picolcd_driver);
Bruno Prémont225b4592010-06-28 22:33:27 +02002753#ifdef CONFIG_HID_PICOLCD_FB
Tejun Heoc4ffafa2010-12-11 17:51:36 +01002754 flush_work_sync(&picolcd_fb_cleanup);
Bruno Prémont225b4592010-06-28 22:33:27 +02002755 WARN_ON(fb_pending);
2756#endif
Bruno Prémont236db472010-03-30 22:33:50 +02002757}
2758
2759module_init(picolcd_init);
2760module_exit(picolcd_exit);
2761MODULE_DESCRIPTION("Minibox graphics PicoLCD Driver");
2762MODULE_LICENSE("GPL v2");