]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/hid/hid-picolcd_fb.c
HID: picoLCD: satify some checkpatch warnings
[mirror_ubuntu-artful-kernel.git] / drivers / hid / hid-picolcd_fb.c
CommitLineData
fabdbf2f
BP
1/***************************************************************************
2 * Copyright (C) 2010-2012 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 "usbhid/usbhid.h"
22#include <linux/usb.h>
23
24#include <linux/fb.h>
25#include <linux/module.h>
26
27#include "hid-picolcd.h"
28
29/* Framebuffer
30 *
31 * The PicoLCD use a Topway LCD module of 256x64 pixel
32 * This display area is tiled over 4 controllers with 8 tiles
33 * each. Each tile has 8x64 pixel, each data byte representing
34 * a 1-bit wide vertical line of the tile.
35 *
36 * The display can be updated at a tile granularity.
37 *
38 * Chip 1 Chip 2 Chip 3 Chip 4
39 * +----------------+----------------+----------------+----------------+
40 * | Tile 1 | Tile 1 | Tile 1 | Tile 1 |
41 * +----------------+----------------+----------------+----------------+
42 * | Tile 2 | Tile 2 | Tile 2 | Tile 2 |
43 * +----------------+----------------+----------------+----------------+
44 * ...
45 * +----------------+----------------+----------------+----------------+
46 * | Tile 8 | Tile 8 | Tile 8 | Tile 8 |
47 * +----------------+----------------+----------------+----------------+
48 */
49#define PICOLCDFB_NAME "picolcdfb"
50#define PICOLCDFB_WIDTH (256)
51#define PICOLCDFB_HEIGHT (64)
52#define PICOLCDFB_SIZE (PICOLCDFB_WIDTH * PICOLCDFB_HEIGHT / 8)
53
54#define PICOLCDFB_UPDATE_RATE_LIMIT 10
55#define PICOLCDFB_UPDATE_RATE_DEFAULT 2
56
57/* Framebuffer visual structures */
58static const struct fb_fix_screeninfo picolcdfb_fix = {
59 .id = PICOLCDFB_NAME,
60 .type = FB_TYPE_PACKED_PIXELS,
61 .visual = FB_VISUAL_MONO01,
62 .xpanstep = 0,
63 .ypanstep = 0,
64 .ywrapstep = 0,
65 .line_length = PICOLCDFB_WIDTH / 8,
66 .accel = FB_ACCEL_NONE,
67};
68
69static const struct fb_var_screeninfo picolcdfb_var = {
70 .xres = PICOLCDFB_WIDTH,
71 .yres = PICOLCDFB_HEIGHT,
72 .xres_virtual = PICOLCDFB_WIDTH,
73 .yres_virtual = PICOLCDFB_HEIGHT,
74 .width = 103,
75 .height = 26,
76 .bits_per_pixel = 1,
77 .grayscale = 1,
78 .red = {
79 .offset = 0,
80 .length = 1,
81 .msb_right = 0,
82 },
83 .green = {
84 .offset = 0,
85 .length = 1,
86 .msb_right = 0,
87 },
88 .blue = {
89 .offset = 0,
90 .length = 1,
91 .msb_right = 0,
92 },
93 .transp = {
94 .offset = 0,
95 .length = 0,
96 .msb_right = 0,
97 },
98};
99
100/* Send a given tile to PicoLCD */
101static int picolcd_fb_send_tile(struct hid_device *hdev, int chip, int tile)
102{
103 struct picolcd_data *data = hid_get_drvdata(hdev);
104 struct hid_report *report1 = picolcd_out_report(REPORT_LCD_CMD_DATA, hdev);
105 struct hid_report *report2 = picolcd_out_report(REPORT_LCD_DATA, hdev);
106 unsigned long flags;
107 u8 *tdata;
108 int i;
109
110 if (!report1 || report1->maxfield != 1 || !report2 || report2->maxfield != 1)
111 return -ENODEV;
112
113 spin_lock_irqsave(&data->lock, flags);
114 hid_set_field(report1->field[0], 0, chip << 2);
115 hid_set_field(report1->field[0], 1, 0x02);
116 hid_set_field(report1->field[0], 2, 0x00);
117 hid_set_field(report1->field[0], 3, 0x00);
118 hid_set_field(report1->field[0], 4, 0xb8 | tile);
119 hid_set_field(report1->field[0], 5, 0x00);
120 hid_set_field(report1->field[0], 6, 0x00);
121 hid_set_field(report1->field[0], 7, 0x40);
122 hid_set_field(report1->field[0], 8, 0x00);
123 hid_set_field(report1->field[0], 9, 0x00);
124 hid_set_field(report1->field[0], 10, 32);
125
126 hid_set_field(report2->field[0], 0, (chip << 2) | 0x01);
127 hid_set_field(report2->field[0], 1, 0x00);
128 hid_set_field(report2->field[0], 2, 0x00);
129 hid_set_field(report2->field[0], 3, 32);
130
131 tdata = data->fb_vbitmap + (tile * 4 + chip) * 64;
132 for (i = 0; i < 64; i++)
133 if (i < 32)
134 hid_set_field(report1->field[0], 11 + i, tdata[i]);
135 else
136 hid_set_field(report2->field[0], 4 + i - 32, tdata[i]);
137
138 usbhid_submit_report(data->hdev, report1, USB_DIR_OUT);
139 usbhid_submit_report(data->hdev, report2, USB_DIR_OUT);
140 spin_unlock_irqrestore(&data->lock, flags);
141 return 0;
142}
143
144/* Translate a single tile*/
145static int picolcd_fb_update_tile(u8 *vbitmap, const u8 *bitmap, int bpp,
146 int chip, int tile)
147{
148 int i, b, changed = 0;
149 u8 tdata[64];
150 u8 *vdata = vbitmap + (tile * 4 + chip) * 64;
151
152 if (bpp == 1) {
153 for (b = 7; b >= 0; b--) {
154 const u8 *bdata = bitmap + tile * 256 + chip * 8 + b * 32;
155 for (i = 0; i < 64; i++) {
156 tdata[i] <<= 1;
157 tdata[i] |= (bdata[i/8] >> (i % 8)) & 0x01;
158 }
159 }
160 } else if (bpp == 8) {
161 for (b = 7; b >= 0; b--) {
162 const u8 *bdata = bitmap + (tile * 256 + chip * 8 + b * 32) * 8;
163 for (i = 0; i < 64; i++) {
164 tdata[i] <<= 1;
165 tdata[i] |= (bdata[i] & 0x80) ? 0x01 : 0x00;
166 }
167 }
168 } else {
169 /* Oops, we should never get here! */
170 WARN_ON(1);
171 return 0;
172 }
173
174 for (i = 0; i < 64; i++)
175 if (tdata[i] != vdata[i]) {
176 changed = 1;
177 vdata[i] = tdata[i];
178 }
179 return changed;
180}
181
182void picolcd_fb_refresh(struct picolcd_data *data)
183{
184 if (data->fb_info)
185 schedule_delayed_work(&data->fb_info->deferred_work, 0);
186}
187
188/* Reconfigure LCD display */
189int picolcd_fb_reset(struct picolcd_data *data, int clear)
190{
191 struct hid_report *report = picolcd_out_report(REPORT_LCD_CMD, data->hdev);
192 int i, j;
193 unsigned long flags;
194 static const u8 mapcmd[8] = { 0x00, 0x02, 0x00, 0x64, 0x3f, 0x00, 0x64, 0xc0 };
195
196 if (!report || report->maxfield != 1)
197 return -ENODEV;
198
199 spin_lock_irqsave(&data->lock, flags);
200 for (i = 0; i < 4; i++) {
201 for (j = 0; j < report->field[0]->maxusage; j++)
202 if (j == 0)
203 hid_set_field(report->field[0], j, i << 2);
204 else if (j < sizeof(mapcmd))
205 hid_set_field(report->field[0], j, mapcmd[j]);
206 else
207 hid_set_field(report->field[0], j, 0);
208 usbhid_submit_report(data->hdev, report, USB_DIR_OUT);
209 }
210
211 data->status |= PICOLCD_READY_FB;
212 spin_unlock_irqrestore(&data->lock, flags);
213
214 if (data->fb_bitmap) {
215 if (clear) {
216 memset(data->fb_vbitmap, 0, PICOLCDFB_SIZE);
217 memset(data->fb_bitmap, 0, PICOLCDFB_SIZE*data->fb_bpp);
218 }
219 data->fb_force = 1;
220 }
221
222 /* schedule first output of framebuffer */
223 picolcd_fb_refresh(data);
224
225 return 0;
226}
227
228/* Update fb_vbitmap from the screen_base and send changed tiles to device */
229static void picolcd_fb_update(struct picolcd_data *data)
230{
231 int chip, tile, n;
232 unsigned long flags;
233
234 if (!data)
235 return;
236
237 spin_lock_irqsave(&data->lock, flags);
238 if (!(data->status & PICOLCD_READY_FB)) {
239 spin_unlock_irqrestore(&data->lock, flags);
240 picolcd_fb_reset(data, 0);
241 } else {
242 spin_unlock_irqrestore(&data->lock, flags);
243 }
244
245 /*
246 * Translate the framebuffer into the format needed by the PicoLCD.
247 * See display layout above.
248 * Do this one tile after the other and push those tiles that changed.
249 *
250 * Wait for our IO to complete as otherwise we might flood the queue!
251 */
252 n = 0;
253 for (chip = 0; chip < 4; chip++)
254 for (tile = 0; tile < 8; tile++)
255 if (picolcd_fb_update_tile(data->fb_vbitmap,
256 data->fb_bitmap, data->fb_bpp, chip, tile) ||
257 data->fb_force) {
258 n += 2;
9966c37c 259 if (data->status & PICOLCD_FAILED)
fabdbf2f
BP
260 return; /* device lost! */
261 if (n >= HID_OUTPUT_FIFO_SIZE / 2) {
262 usbhid_wait_io(data->hdev);
263 n = 0;
264 }
265 picolcd_fb_send_tile(data->hdev, chip, tile);
266 }
267 data->fb_force = false;
268 if (n)
269 usbhid_wait_io(data->hdev);
270}
271
272/* Stub to call the system default and update the image on the picoLCD */
273static void picolcd_fb_fillrect(struct fb_info *info,
274 const struct fb_fillrect *rect)
275{
276 if (!info->par)
277 return;
278 sys_fillrect(info, rect);
279
280 schedule_delayed_work(&info->deferred_work, 0);
281}
282
283/* Stub to call the system default and update the image on the picoLCD */
284static void picolcd_fb_copyarea(struct fb_info *info,
285 const struct fb_copyarea *area)
286{
287 if (!info->par)
288 return;
289 sys_copyarea(info, area);
290
291 schedule_delayed_work(&info->deferred_work, 0);
292}
293
294/* Stub to call the system default and update the image on the picoLCD */
295static void picolcd_fb_imageblit(struct fb_info *info, const struct fb_image *image)
296{
297 if (!info->par)
298 return;
299 sys_imageblit(info, image);
300
301 schedule_delayed_work(&info->deferred_work, 0);
302}
303
304/*
305 * this is the slow path from userspace. they can seek and write to
306 * the fb. it's inefficient to do anything less than a full screen draw
307 */
308static ssize_t picolcd_fb_write(struct fb_info *info, const char __user *buf,
309 size_t count, loff_t *ppos)
310{
311 ssize_t ret;
312 if (!info->par)
313 return -ENODEV;
314 ret = fb_sys_write(info, buf, count, ppos);
315 if (ret >= 0)
316 schedule_delayed_work(&info->deferred_work, 0);
317 return ret;
318}
319
320static int picolcd_fb_blank(int blank, struct fb_info *info)
321{
322 if (!info->par)
323 return -ENODEV;
324 /* We let fb notification do this for us via lcd/backlight device */
325 return 0;
326}
327
328static void picolcd_fb_destroy(struct fb_info *info)
329{
9966c37c 330 struct picolcd_data *data;
fabdbf2f 331
9966c37c
BP
332 /* make sure no work is deferred */
333 cancel_delayed_work_sync(&info->deferred_work);
334 data = info->par;
fabdbf2f
BP
335 info->par = NULL;
336 if (data)
337 data->fb_info = NULL;
fabdbf2f 338
9966c37c
BP
339 vfree((u8 *)info->fix.smem_start);
340 framebuffer_release(info);
fabdbf2f
BP
341}
342
343static int picolcd_fb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
344{
345 __u32 bpp = var->bits_per_pixel;
346 __u32 activate = var->activate;
347
348 /* only allow 1/8 bit depth (8-bit is grayscale) */
349 *var = picolcdfb_var;
350 var->activate = activate;
351 if (bpp >= 8) {
352 var->bits_per_pixel = 8;
353 var->red.length = 8;
354 var->green.length = 8;
355 var->blue.length = 8;
356 } else {
357 var->bits_per_pixel = 1;
358 var->red.length = 1;
359 var->green.length = 1;
360 var->blue.length = 1;
361 }
362 return 0;
363}
364
365static int picolcd_set_par(struct fb_info *info)
366{
367 struct picolcd_data *data = info->par;
368 u8 *tmp_fb, *o_fb;
369 if (!data)
370 return -ENODEV;
371 if (info->var.bits_per_pixel == data->fb_bpp)
372 return 0;
373 /* switch between 1/8 bit depths */
374 if (info->var.bits_per_pixel != 1 && info->var.bits_per_pixel != 8)
375 return -EINVAL;
376
377 o_fb = data->fb_bitmap;
378 tmp_fb = kmalloc(PICOLCDFB_SIZE*info->var.bits_per_pixel, GFP_KERNEL);
379 if (!tmp_fb)
380 return -ENOMEM;
381
382 /* translate FB content to new bits-per-pixel */
383 if (info->var.bits_per_pixel == 1) {
384 int i, b;
385 for (i = 0; i < PICOLCDFB_SIZE; i++) {
386 u8 p = 0;
387 for (b = 0; b < 8; b++) {
388 p <<= 1;
389 p |= o_fb[i*8+b] ? 0x01 : 0x00;
390 }
391 tmp_fb[i] = p;
392 }
393 memcpy(o_fb, tmp_fb, PICOLCDFB_SIZE);
394 info->fix.visual = FB_VISUAL_MONO01;
395 info->fix.line_length = PICOLCDFB_WIDTH / 8;
396 } else {
397 int i;
398 memcpy(tmp_fb, o_fb, PICOLCDFB_SIZE);
399 for (i = 0; i < PICOLCDFB_SIZE * 8; i++)
400 o_fb[i] = tmp_fb[i/8] & (0x01 << (7 - i % 8)) ? 0xff : 0x00;
401 info->fix.visual = FB_VISUAL_DIRECTCOLOR;
402 info->fix.line_length = PICOLCDFB_WIDTH;
403 }
404
405 kfree(tmp_fb);
406 data->fb_bpp = info->var.bits_per_pixel;
407 return 0;
408}
409
fabdbf2f
BP
410/* Note this can't be const because of struct fb_info definition */
411static struct fb_ops picolcdfb_ops = {
412 .owner = THIS_MODULE,
413 .fb_destroy = picolcd_fb_destroy,
fabdbf2f
BP
414 .fb_read = fb_sys_read,
415 .fb_write = picolcd_fb_write,
416 .fb_blank = picolcd_fb_blank,
417 .fb_fillrect = picolcd_fb_fillrect,
418 .fb_copyarea = picolcd_fb_copyarea,
419 .fb_imageblit = picolcd_fb_imageblit,
420 .fb_check_var = picolcd_fb_check_var,
421 .fb_set_par = picolcd_set_par,
422};
423
424
425/* Callback from deferred IO workqueue */
426static void picolcd_fb_deferred_io(struct fb_info *info, struct list_head *pagelist)
427{
428 picolcd_fb_update(info->par);
429}
430
431static const struct fb_deferred_io picolcd_fb_defio = {
432 .delay = HZ / PICOLCDFB_UPDATE_RATE_DEFAULT,
433 .deferred_io = picolcd_fb_deferred_io,
434};
435
436
437/*
438 * The "fb_update_rate" sysfs attribute
439 */
440static ssize_t picolcd_fb_update_rate_show(struct device *dev,
441 struct device_attribute *attr, char *buf)
442{
443 struct picolcd_data *data = dev_get_drvdata(dev);
444 unsigned i, fb_update_rate = data->fb_update_rate;
445 size_t ret = 0;
446
447 for (i = 1; i <= PICOLCDFB_UPDATE_RATE_LIMIT; i++)
448 if (ret >= PAGE_SIZE)
449 break;
450 else if (i == fb_update_rate)
451 ret += snprintf(buf+ret, PAGE_SIZE-ret, "[%u] ", i);
452 else
453 ret += snprintf(buf+ret, PAGE_SIZE-ret, "%u ", i);
454 if (ret > 0)
455 buf[min(ret, (size_t)PAGE_SIZE)-1] = '\n';
456 return ret;
457}
458
459static ssize_t picolcd_fb_update_rate_store(struct device *dev,
460 struct device_attribute *attr, const char *buf, size_t count)
461{
462 struct picolcd_data *data = dev_get_drvdata(dev);
463 int i;
464 unsigned u;
465
466 if (count < 1 || count > 10)
467 return -EINVAL;
468
469 i = sscanf(buf, "%u", &u);
470 if (i != 1)
471 return -EINVAL;
472
473 if (u > PICOLCDFB_UPDATE_RATE_LIMIT)
474 return -ERANGE;
475 else if (u == 0)
476 u = PICOLCDFB_UPDATE_RATE_DEFAULT;
477
478 data->fb_update_rate = u;
9966c37c 479 data->fb_info->fbdefio->delay = HZ / data->fb_update_rate;
fabdbf2f
BP
480 return count;
481}
482
483static DEVICE_ATTR(fb_update_rate, 0666, picolcd_fb_update_rate_show,
484 picolcd_fb_update_rate_store);
485
486/* initialize Framebuffer device */
487int picolcd_init_framebuffer(struct picolcd_data *data)
488{
489 struct device *dev = &data->hdev->dev;
490 struct fb_info *info = NULL;
491 int i, error = -ENOMEM;
492 u8 *fb_vbitmap = NULL;
493 u8 *fb_bitmap = NULL;
494 u32 *palette;
495
496 fb_bitmap = vmalloc(PICOLCDFB_SIZE*8);
497 if (fb_bitmap == NULL) {
498 dev_err(dev, "can't get a free page for framebuffer\n");
499 goto err_nomem;
500 }
501
502 fb_vbitmap = kmalloc(PICOLCDFB_SIZE, GFP_KERNEL);
503 if (fb_vbitmap == NULL) {
504 dev_err(dev, "can't alloc vbitmap image buffer\n");
505 goto err_nomem;
506 }
507
508 data->fb_update_rate = PICOLCDFB_UPDATE_RATE_DEFAULT;
fabdbf2f 509 /* The extra memory is:
fabdbf2f 510 * - 256*u32 for pseudo_palette
9966c37c 511 * - struct fb_deferred_io
fabdbf2f 512 */
9966c37c
BP
513 info = framebuffer_alloc(256 * sizeof(u32) +
514 sizeof(struct fb_deferred_io), dev);
fabdbf2f
BP
515 if (info == NULL) {
516 dev_err(dev, "failed to allocate a framebuffer\n");
517 goto err_nomem;
518 }
519
9966c37c
BP
520 info->fbdefio = info->par;
521 *info->fbdefio = picolcd_fb_defio;
522 palette = info->par + sizeof(struct fb_deferred_io);
fabdbf2f
BP
523 for (i = 0; i < 256; i++)
524 palette[i] = i > 0 && i < 16 ? 0xff : 0;
525 info->pseudo_palette = palette;
fabdbf2f
BP
526 info->screen_base = (char __force __iomem *)fb_bitmap;
527 info->fbops = &picolcdfb_ops;
528 info->var = picolcdfb_var;
529 info->fix = picolcdfb_fix;
530 info->fix.smem_len = PICOLCDFB_SIZE*8;
531 info->fix.smem_start = (unsigned long)fb_bitmap;
532 info->par = data;
533 info->flags = FBINFO_FLAG_DEFAULT;
534
535 data->fb_vbitmap = fb_vbitmap;
536 data->fb_bitmap = fb_bitmap;
537 data->fb_bpp = picolcdfb_var.bits_per_pixel;
538 error = picolcd_fb_reset(data, 1);
539 if (error) {
540 dev_err(dev, "failed to configure display\n");
541 goto err_cleanup;
542 }
543 error = device_create_file(dev, &dev_attr_fb_update_rate);
544 if (error) {
545 dev_err(dev, "failed to create sysfs attributes\n");
546 goto err_cleanup;
547 }
548 fb_deferred_io_init(info);
549 data->fb_info = info;
550 error = register_framebuffer(info);
551 if (error) {
552 dev_err(dev, "failed to register framebuffer\n");
553 goto err_sysfs;
554 }
555 /* schedule first output of framebuffer */
556 data->fb_force = 1;
557 schedule_delayed_work(&info->deferred_work, 0);
558 return 0;
559
560err_sysfs:
561 fb_deferred_io_cleanup(info);
562 device_remove_file(dev, &dev_attr_fb_update_rate);
563err_cleanup:
564 data->fb_vbitmap = NULL;
565 data->fb_bitmap = NULL;
566 data->fb_bpp = 0;
567 data->fb_info = NULL;
568
569err_nomem:
570 framebuffer_release(info);
571 vfree(fb_bitmap);
572 kfree(fb_vbitmap);
573 return error;
574}
575
576void picolcd_exit_framebuffer(struct picolcd_data *data)
577{
578 struct fb_info *info = data->fb_info;
579 u8 *fb_vbitmap = data->fb_vbitmap;
580
581 if (!info)
582 return;
583
584 device_remove_file(&data->hdev->dev, &dev_attr_fb_update_rate);
9966c37c
BP
585 mutex_lock(&info->lock);
586 fb_deferred_io_cleanup(info);
587 info->par = NULL;
588 mutex_unlock(&info->lock);
fabdbf2f
BP
589 unregister_framebuffer(info);
590 data->fb_vbitmap = NULL;
591 data->fb_bitmap = NULL;
592 data->fb_bpp = 0;
593 data->fb_info = NULL;
594 kfree(fb_vbitmap);
595}