Linux 4.20-rc6
[linux-2.6-block.git] / drivers / auxdisplay / charlcd.c
CommitLineData
351f683b 1// SPDX-License-Identifier: GPL-2.0+
39f8ea46
GU
2/*
3 * Character LCD driver for Linux
4 *
5 * Copyright (C) 2000-2008, Willy Tarreau <w@1wt.eu>
6 * Copyright (C) 2016-2017 Glider bvba
39f8ea46
GU
7 */
8
9#include <linux/atomic.h>
b34050fa 10#include <linux/ctype.h>
39f8ea46
GU
11#include <linux/delay.h>
12#include <linux/fs.h>
13#include <linux/miscdevice.h>
14#include <linux/module.h>
15#include <linux/notifier.h>
16#include <linux/reboot.h>
17#include <linux/slab.h>
18#include <linux/uaccess.h>
19#include <linux/workqueue.h>
20
21#include <generated/utsrelease.h>
22
23#include <misc/charlcd.h>
24
25#define LCD_MINOR 156
26
27#define DEFAULT_LCD_BWIDTH 40
28#define DEFAULT_LCD_HWIDTH 64
29
30/* Keep the backlight on this many seconds for each flash */
31#define LCD_BL_TEMPO_PERIOD 4
32
33#define LCD_FLAG_B 0x0004 /* Blink on */
34#define LCD_FLAG_C 0x0008 /* Cursor on */
35#define LCD_FLAG_D 0x0010 /* Display on */
36#define LCD_FLAG_F 0x0020 /* Large font mode */
37#define LCD_FLAG_N 0x0040 /* 2-rows mode */
38#define LCD_FLAG_L 0x0080 /* Backlight enabled */
39
40/* LCD commands */
41#define LCD_CMD_DISPLAY_CLEAR 0x01 /* Clear entire display */
42
43#define LCD_CMD_ENTRY_MODE 0x04 /* Set entry mode */
44#define LCD_CMD_CURSOR_INC 0x02 /* Increment cursor */
45
46#define LCD_CMD_DISPLAY_CTRL 0x08 /* Display control */
47#define LCD_CMD_DISPLAY_ON 0x04 /* Set display on */
48#define LCD_CMD_CURSOR_ON 0x02 /* Set cursor on */
49#define LCD_CMD_BLINK_ON 0x01 /* Set blink on */
50
51#define LCD_CMD_SHIFT 0x10 /* Shift cursor/display */
52#define LCD_CMD_DISPLAY_SHIFT 0x08 /* Shift display instead of cursor */
53#define LCD_CMD_SHIFT_RIGHT 0x04 /* Shift display/cursor to the right */
54
55#define LCD_CMD_FUNCTION_SET 0x20 /* Set function */
56#define LCD_CMD_DATA_LEN_8BITS 0x10 /* Set data length to 8 bits */
57#define LCD_CMD_TWO_LINES 0x08 /* Set to two display lines */
58#define LCD_CMD_FONT_5X10_DOTS 0x04 /* Set char font to 5x10 dots */
59
60#define LCD_CMD_SET_CGRAM_ADDR 0x40 /* Set char generator RAM address */
61
62#define LCD_CMD_SET_DDRAM_ADDR 0x80 /* Set display data RAM address */
63
64#define LCD_ESCAPE_LEN 24 /* Max chars for LCD escape command */
65#define LCD_ESCAPE_CHAR 27 /* Use char 27 for escape command */
66
67struct charlcd_priv {
68 struct charlcd lcd;
69
70 struct delayed_work bl_work;
71 struct mutex bl_tempo_lock; /* Protects access to bl_tempo */
72 bool bl_tempo;
73
74 bool must_clear;
75
76 /* contains the LCD config state */
77 unsigned long int flags;
78
79 /* Contains the LCD X and Y offset */
80 struct {
81 unsigned long int x;
82 unsigned long int y;
83 } addr;
84
85 /* Current escape sequence and it's length or -1 if outside */
86 struct {
87 char buf[LCD_ESCAPE_LEN + 1];
88 int len;
89 } esc_seq;
90
91 unsigned long long drvdata[0];
92};
93
94#define to_priv(p) container_of(p, struct charlcd_priv, lcd)
95
96/* Device single-open policy control */
97static atomic_t charlcd_available = ATOMIC_INIT(1);
98
99/* sleeps that many milliseconds with a reschedule */
100static void long_sleep(int ms)
101{
17161392 102 schedule_timeout_interruptible(msecs_to_jiffies(ms));
39f8ea46
GU
103}
104
105/* turn the backlight on or off */
106static void charlcd_backlight(struct charlcd *lcd, int on)
107{
108 struct charlcd_priv *priv = to_priv(lcd);
109
110 if (!lcd->ops->backlight)
111 return;
112
113 mutex_lock(&priv->bl_tempo_lock);
114 if (!priv->bl_tempo)
115 lcd->ops->backlight(lcd, on);
116 mutex_unlock(&priv->bl_tempo_lock);
117}
118
119static void charlcd_bl_off(struct work_struct *work)
120{
121 struct delayed_work *dwork = to_delayed_work(work);
122 struct charlcd_priv *priv =
123 container_of(dwork, struct charlcd_priv, bl_work);
124
125 mutex_lock(&priv->bl_tempo_lock);
126 if (priv->bl_tempo) {
127 priv->bl_tempo = false;
128 if (!(priv->flags & LCD_FLAG_L))
129 priv->lcd.ops->backlight(&priv->lcd, 0);
130 }
131 mutex_unlock(&priv->bl_tempo_lock);
132}
133
134/* turn the backlight on for a little while */
135void charlcd_poke(struct charlcd *lcd)
136{
137 struct charlcd_priv *priv = to_priv(lcd);
138
139 if (!lcd->ops->backlight)
140 return;
141
142 cancel_delayed_work_sync(&priv->bl_work);
143
144 mutex_lock(&priv->bl_tempo_lock);
145 if (!priv->bl_tempo && !(priv->flags & LCD_FLAG_L))
146 lcd->ops->backlight(lcd, 1);
147 priv->bl_tempo = true;
148 schedule_delayed_work(&priv->bl_work, LCD_BL_TEMPO_PERIOD * HZ);
149 mutex_unlock(&priv->bl_tempo_lock);
150}
151EXPORT_SYMBOL_GPL(charlcd_poke);
152
153static void charlcd_gotoxy(struct charlcd *lcd)
154{
155 struct charlcd_priv *priv = to_priv(lcd);
1d3b2af2
GU
156 unsigned int addr;
157
158 /*
159 * we force the cursor to stay at the end of the
160 * line if it wants to go farther
161 */
162 addr = priv->addr.x < lcd->bwidth ? priv->addr.x & (lcd->hwidth - 1)
163 : lcd->bwidth - 1;
164 if (priv->addr.y & 1)
165 addr += lcd->hwidth;
166 if (priv->addr.y & 2)
167 addr += lcd->bwidth;
168 lcd->ops->write_cmd(lcd, LCD_CMD_SET_DDRAM_ADDR | addr);
39f8ea46
GU
169}
170
171static void charlcd_home(struct charlcd *lcd)
172{
173 struct charlcd_priv *priv = to_priv(lcd);
174
175 priv->addr.x = 0;
176 priv->addr.y = 0;
177 charlcd_gotoxy(lcd);
178}
179
180static void charlcd_print(struct charlcd *lcd, char c)
181{
182 struct charlcd_priv *priv = to_priv(lcd);
183
184 if (priv->addr.x < lcd->bwidth) {
185 if (lcd->char_conv)
186 c = lcd->char_conv[(unsigned char)c];
187 lcd->ops->write_data(lcd, c);
188 priv->addr.x++;
54bc937f
SY
189
190 /* prevents the cursor from wrapping onto the next line */
191 if (priv->addr.x == lcd->bwidth)
192 charlcd_gotoxy(lcd);
39f8ea46 193 }
39f8ea46
GU
194}
195
196static void charlcd_clear_fast(struct charlcd *lcd)
197{
198 int pos;
199
200 charlcd_home(lcd);
201
202 if (lcd->ops->clear_fast)
203 lcd->ops->clear_fast(lcd);
204 else
1d3b2af2 205 for (pos = 0; pos < min(2, lcd->height) * lcd->hwidth; pos++)
39f8ea46
GU
206 lcd->ops->write_data(lcd, ' ');
207
208 charlcd_home(lcd);
209}
210
211/* clears the display and resets X/Y */
212static void charlcd_clear_display(struct charlcd *lcd)
213{
214 struct charlcd_priv *priv = to_priv(lcd);
215
216 lcd->ops->write_cmd(lcd, LCD_CMD_DISPLAY_CLEAR);
217 priv->addr.x = 0;
218 priv->addr.y = 0;
219 /* we must wait a few milliseconds (15) */
220 long_sleep(15);
221}
222
223static int charlcd_init_display(struct charlcd *lcd)
224{
ac201479 225 void (*write_cmd_raw)(struct charlcd *lcd, int cmd);
39f8ea46 226 struct charlcd_priv *priv = to_priv(lcd);
ac201479
GU
227 u8 init;
228
229 if (lcd->ifwidth != 4 && lcd->ifwidth != 8)
230 return -EINVAL;
39f8ea46
GU
231
232 priv->flags = ((lcd->height > 1) ? LCD_FLAG_N : 0) | LCD_FLAG_D |
233 LCD_FLAG_C | LCD_FLAG_B;
234
235 long_sleep(20); /* wait 20 ms after power-up for the paranoid */
236
ac201479
GU
237 /*
238 * 8-bit mode, 1 line, small fonts; let's do it 3 times, to make sure
239 * the LCD is in 8-bit mode afterwards
240 */
241 init = LCD_CMD_FUNCTION_SET | LCD_CMD_DATA_LEN_8BITS;
242 if (lcd->ifwidth == 4) {
243 init >>= 4;
244 write_cmd_raw = lcd->ops->write_cmd_raw4;
245 } else {
246 write_cmd_raw = lcd->ops->write_cmd;
247 }
248 write_cmd_raw(lcd, init);
39f8ea46 249 long_sleep(10);
ac201479 250 write_cmd_raw(lcd, init);
39f8ea46 251 long_sleep(10);
ac201479 252 write_cmd_raw(lcd, init);
39f8ea46
GU
253 long_sleep(10);
254
ac201479
GU
255 if (lcd->ifwidth == 4) {
256 /* Switch to 4-bit mode, 1 line, small fonts */
257 lcd->ops->write_cmd_raw4(lcd, LCD_CMD_FUNCTION_SET >> 4);
258 long_sleep(10);
259 }
260
39f8ea46
GU
261 /* set font height and lines number */
262 lcd->ops->write_cmd(lcd,
ac201479
GU
263 LCD_CMD_FUNCTION_SET |
264 ((lcd->ifwidth == 8) ? LCD_CMD_DATA_LEN_8BITS : 0) |
39f8ea46
GU
265 ((priv->flags & LCD_FLAG_F) ? LCD_CMD_FONT_5X10_DOTS : 0) |
266 ((priv->flags & LCD_FLAG_N) ? LCD_CMD_TWO_LINES : 0));
267 long_sleep(10);
268
269 /* display off, cursor off, blink off */
270 lcd->ops->write_cmd(lcd, LCD_CMD_DISPLAY_CTRL);
271 long_sleep(10);
272
273 lcd->ops->write_cmd(lcd,
274 LCD_CMD_DISPLAY_CTRL | /* set display mode */
275 ((priv->flags & LCD_FLAG_D) ? LCD_CMD_DISPLAY_ON : 0) |
276 ((priv->flags & LCD_FLAG_C) ? LCD_CMD_CURSOR_ON : 0) |
277 ((priv->flags & LCD_FLAG_B) ? LCD_CMD_BLINK_ON : 0));
278
279 charlcd_backlight(lcd, (priv->flags & LCD_FLAG_L) ? 1 : 0);
280
281 long_sleep(10);
282
283 /* entry mode set : increment, cursor shifting */
284 lcd->ops->write_cmd(lcd, LCD_CMD_ENTRY_MODE | LCD_CMD_CURSOR_INC);
285
286 charlcd_clear_display(lcd);
287 return 0;
288}
289
b34050fa
MO
290/*
291 * Parses an unsigned integer from a string, until a non-digit character
292 * is found. The empty string is not accepted. No overflow checks are done.
293 *
294 * Returns whether the parsing was successful. Only in that case
295 * the output parameters are written to.
296 *
297 * TODO: If the kernel adds an inplace version of kstrtoul(), this function
298 * could be easily replaced by that.
299 */
300static bool parse_n(const char *s, unsigned long *res, const char **next_s)
301{
302 if (!isdigit(*s))
303 return false;
304
305 *res = 0;
306 while (isdigit(*s)) {
307 *res = *res * 10 + (*s - '0');
308 ++s;
309 }
310
311 *next_s = s;
312 return true;
313}
314
315/*
316 * Parses a movement command of the form "(.*);", where the group can be
317 * any number of subcommands of the form "(x|y)[0-9]+".
318 *
319 * Returns whether the command is valid. The position arguments are
320 * only written if the parsing was successful.
321 *
322 * For instance:
323 * - ";" returns (<original x>, <original y>).
324 * - "x1;" returns (1, <original y>).
325 * - "y2x1;" returns (1, 2).
326 * - "x12y34x56;" returns (56, 34).
327 * - "" fails.
328 * - "x" fails.
329 * - "x;" fails.
330 * - "x1" fails.
331 * - "xy12;" fails.
332 * - "x12yy12;" fails.
333 * - "xx" fails.
334 */
335static bool parse_xy(const char *s, unsigned long *x, unsigned long *y)
336{
337 unsigned long new_x = *x;
338 unsigned long new_y = *y;
339
340 for (;;) {
341 if (!*s)
342 return false;
343
344 if (*s == ';')
345 break;
346
347 if (*s == 'x') {
348 if (!parse_n(s + 1, &new_x, &s))
349 return false;
350 } else if (*s == 'y') {
351 if (!parse_n(s + 1, &new_y, &s))
352 return false;
353 } else {
354 return false;
355 }
356 }
357
358 *x = new_x;
359 *y = new_y;
360 return true;
361}
362
39f8ea46
GU
363/*
364 * These are the file operation function for user access to /dev/lcd
365 * This function can also be called from inside the kernel, by
366 * setting file and ppos to NULL.
367 *
368 */
369
370static inline int handle_lcd_special_code(struct charlcd *lcd)
371{
372 struct charlcd_priv *priv = to_priv(lcd);
373
374 /* LCD special codes */
375
376 int processed = 0;
377
378 char *esc = priv->esc_seq.buf + 2;
379 int oldflags = priv->flags;
380
381 /* check for display mode flags */
382 switch (*esc) {
383 case 'D': /* Display ON */
384 priv->flags |= LCD_FLAG_D;
385 processed = 1;
386 break;
387 case 'd': /* Display OFF */
388 priv->flags &= ~LCD_FLAG_D;
389 processed = 1;
390 break;
391 case 'C': /* Cursor ON */
392 priv->flags |= LCD_FLAG_C;
393 processed = 1;
394 break;
395 case 'c': /* Cursor OFF */
396 priv->flags &= ~LCD_FLAG_C;
397 processed = 1;
398 break;
399 case 'B': /* Blink ON */
400 priv->flags |= LCD_FLAG_B;
401 processed = 1;
402 break;
403 case 'b': /* Blink OFF */
404 priv->flags &= ~LCD_FLAG_B;
405 processed = 1;
406 break;
407 case '+': /* Back light ON */
408 priv->flags |= LCD_FLAG_L;
409 processed = 1;
410 break;
411 case '-': /* Back light OFF */
412 priv->flags &= ~LCD_FLAG_L;
413 processed = 1;
414 break;
415 case '*': /* Flash back light */
416 charlcd_poke(lcd);
417 processed = 1;
418 break;
419 case 'f': /* Small Font */
420 priv->flags &= ~LCD_FLAG_F;
421 processed = 1;
422 break;
423 case 'F': /* Large Font */
424 priv->flags |= LCD_FLAG_F;
425 processed = 1;
426 break;
427 case 'n': /* One Line */
428 priv->flags &= ~LCD_FLAG_N;
429 processed = 1;
430 break;
431 case 'N': /* Two Lines */
432 priv->flags |= LCD_FLAG_N;
99b9b490 433 processed = 1;
39f8ea46
GU
434 break;
435 case 'l': /* Shift Cursor Left */
436 if (priv->addr.x > 0) {
437 /* back one char if not at end of line */
438 if (priv->addr.x < lcd->bwidth)
439 lcd->ops->write_cmd(lcd, LCD_CMD_SHIFT);
440 priv->addr.x--;
441 }
442 processed = 1;
443 break;
444 case 'r': /* shift cursor right */
445 if (priv->addr.x < lcd->width) {
446 /* allow the cursor to pass the end of the line */
447 if (priv->addr.x < (lcd->bwidth - 1))
448 lcd->ops->write_cmd(lcd,
449 LCD_CMD_SHIFT | LCD_CMD_SHIFT_RIGHT);
450 priv->addr.x++;
451 }
452 processed = 1;
453 break;
454 case 'L': /* shift display left */
455 lcd->ops->write_cmd(lcd, LCD_CMD_SHIFT | LCD_CMD_DISPLAY_SHIFT);
456 processed = 1;
457 break;
458 case 'R': /* shift display right */
459 lcd->ops->write_cmd(lcd,
460 LCD_CMD_SHIFT | LCD_CMD_DISPLAY_SHIFT |
461 LCD_CMD_SHIFT_RIGHT);
462 processed = 1;
463 break;
464 case 'k': { /* kill end of line */
465 int x;
466
467 for (x = priv->addr.x; x < lcd->bwidth; x++)
468 lcd->ops->write_data(lcd, ' ');
469
470 /* restore cursor position */
471 charlcd_gotoxy(lcd);
472 processed = 1;
473 break;
474 }
475 case 'I': /* reinitialize display */
476 charlcd_init_display(lcd);
477 processed = 1;
478 break;
479 case 'G': {
480 /* Generator : LGcxxxxx...xx; must have <c> between '0'
481 * and '7', representing the numerical ASCII code of the
482 * redefined character, and <xx...xx> a sequence of 16
483 * hex digits representing 8 bytes for each character.
484 * Most LCDs will only use 5 lower bits of the 7 first
485 * bytes.
486 */
487
488 unsigned char cgbytes[8];
489 unsigned char cgaddr;
490 int cgoffset;
491 int shift;
492 char value;
493 int addr;
494
495 if (!strchr(esc, ';'))
496 break;
497
498 esc++;
499
500 cgaddr = *(esc++) - '0';
501 if (cgaddr > 7) {
502 processed = 1;
503 break;
504 }
505
506 cgoffset = 0;
507 shift = 0;
508 value = 0;
509 while (*esc && cgoffset < 8) {
510 shift ^= 4;
511 if (*esc >= '0' && *esc <= '9') {
512 value |= (*esc - '0') << shift;
2e8c04f7 513 } else if (*esc >= 'A' && *esc <= 'F') {
39f8ea46 514 value |= (*esc - 'A' + 10) << shift;
2e8c04f7 515 } else if (*esc >= 'a' && *esc <= 'f') {
39f8ea46
GU
516 value |= (*esc - 'a' + 10) << shift;
517 } else {
518 esc++;
519 continue;
520 }
521
522 if (shift == 0) {
523 cgbytes[cgoffset++] = value;
524 value = 0;
525 }
526
527 esc++;
528 }
529
530 lcd->ops->write_cmd(lcd, LCD_CMD_SET_CGRAM_ADDR | (cgaddr * 8));
531 for (addr = 0; addr < cgoffset; addr++)
532 lcd->ops->write_data(lcd, cgbytes[addr]);
533
534 /* ensures that we stop writing to CGRAM */
535 charlcd_gotoxy(lcd);
536 processed = 1;
537 break;
538 }
539 case 'x': /* gotoxy : LxXXX[yYYY]; */
540 case 'y': /* gotoxy : LyYYY[xXXX]; */
b34050fa
MO
541 /* If the command is valid, move to the new address */
542 if (parse_xy(esc, &priv->addr.x, &priv->addr.y))
543 charlcd_gotoxy(lcd);
39f8ea46 544
b34050fa 545 /* Regardless of its validity, mark as processed */
39f8ea46
GU
546 processed = 1;
547 break;
548 }
549
550 /* TODO: This indent party here got ugly, clean it! */
551 /* Check whether one flag was changed */
552 if (oldflags == priv->flags)
553 return processed;
554
555 /* check whether one of B,C,D flags were changed */
556 if ((oldflags ^ priv->flags) &
557 (LCD_FLAG_B | LCD_FLAG_C | LCD_FLAG_D))
558 /* set display mode */
559 lcd->ops->write_cmd(lcd,
560 LCD_CMD_DISPLAY_CTRL |
561 ((priv->flags & LCD_FLAG_D) ? LCD_CMD_DISPLAY_ON : 0) |
562 ((priv->flags & LCD_FLAG_C) ? LCD_CMD_CURSOR_ON : 0) |
563 ((priv->flags & LCD_FLAG_B) ? LCD_CMD_BLINK_ON : 0));
564 /* check whether one of F,N flags was changed */
565 else if ((oldflags ^ priv->flags) & (LCD_FLAG_F | LCD_FLAG_N))
566 lcd->ops->write_cmd(lcd,
ac201479
GU
567 LCD_CMD_FUNCTION_SET |
568 ((lcd->ifwidth == 8) ? LCD_CMD_DATA_LEN_8BITS : 0) |
39f8ea46
GU
569 ((priv->flags & LCD_FLAG_F) ? LCD_CMD_FONT_5X10_DOTS : 0) |
570 ((priv->flags & LCD_FLAG_N) ? LCD_CMD_TWO_LINES : 0));
571 /* check whether L flag was changed */
572 else if ((oldflags ^ priv->flags) & LCD_FLAG_L)
573 charlcd_backlight(lcd, !!(priv->flags & LCD_FLAG_L));
574
575 return processed;
576}
577
578static void charlcd_write_char(struct charlcd *lcd, char c)
579{
580 struct charlcd_priv *priv = to_priv(lcd);
581
582 /* first, we'll test if we're in escape mode */
583 if ((c != '\n') && priv->esc_seq.len >= 0) {
584 /* yes, let's add this char to the buffer */
585 priv->esc_seq.buf[priv->esc_seq.len++] = c;
8c483758 586 priv->esc_seq.buf[priv->esc_seq.len] = '\0';
39f8ea46
GU
587 } else {
588 /* aborts any previous escape sequence */
589 priv->esc_seq.len = -1;
590
591 switch (c) {
592 case LCD_ESCAPE_CHAR:
593 /* start of an escape sequence */
594 priv->esc_seq.len = 0;
8c483758 595 priv->esc_seq.buf[priv->esc_seq.len] = '\0';
39f8ea46
GU
596 break;
597 case '\b':
598 /* go back one char and clear it */
599 if (priv->addr.x > 0) {
600 /*
601 * check if we're not at the
602 * end of the line
603 */
604 if (priv->addr.x < lcd->bwidth)
605 /* back one char */
606 lcd->ops->write_cmd(lcd, LCD_CMD_SHIFT);
607 priv->addr.x--;
608 }
609 /* replace with a space */
610 lcd->ops->write_data(lcd, ' ');
611 /* back one char again */
612 lcd->ops->write_cmd(lcd, LCD_CMD_SHIFT);
613 break;
9629ccca 614 case '\f':
39f8ea46
GU
615 /* quickly clear the display */
616 charlcd_clear_fast(lcd);
617 break;
618 case '\n':
619 /*
620 * flush the remainder of the current line and
621 * go to the beginning of the next line
622 */
623 for (; priv->addr.x < lcd->bwidth; priv->addr.x++)
624 lcd->ops->write_data(lcd, ' ');
625 priv->addr.x = 0;
626 priv->addr.y = (priv->addr.y + 1) % lcd->height;
627 charlcd_gotoxy(lcd);
628 break;
629 case '\r':
630 /* go to the beginning of the same line */
631 priv->addr.x = 0;
632 charlcd_gotoxy(lcd);
633 break;
634 case '\t':
635 /* print a space instead of the tab */
636 charlcd_print(lcd, ' ');
637 break;
638 default:
639 /* simply print this char */
640 charlcd_print(lcd, c);
641 break;
642 }
643 }
644
645 /*
646 * now we'll see if we're in an escape mode and if the current
647 * escape sequence can be understood.
648 */
649 if (priv->esc_seq.len >= 2) {
650 int processed = 0;
651
652 if (!strcmp(priv->esc_seq.buf, "[2J")) {
653 /* clear the display */
654 charlcd_clear_fast(lcd);
655 processed = 1;
656 } else if (!strcmp(priv->esc_seq.buf, "[H")) {
657 /* cursor to home */
658 charlcd_home(lcd);
659 processed = 1;
660 }
661 /* codes starting with ^[[L */
662 else if ((priv->esc_seq.len >= 3) &&
663 (priv->esc_seq.buf[0] == '[') &&
664 (priv->esc_seq.buf[1] == 'L')) {
665 processed = handle_lcd_special_code(lcd);
666 }
667
668 /* LCD special escape codes */
669 /*
670 * flush the escape sequence if it's been processed
671 * or if it is getting too long.
672 */
673 if (processed || (priv->esc_seq.len >= LCD_ESCAPE_LEN))
674 priv->esc_seq.len = -1;
675 } /* escape codes */
676}
677
678static struct charlcd *the_charlcd;
679
680static ssize_t charlcd_write(struct file *file, const char __user *buf,
681 size_t count, loff_t *ppos)
682{
683 const char __user *tmp = buf;
684 char c;
685
686 for (; count-- > 0; (*ppos)++, tmp++) {
687 if (!in_interrupt() && (((count + 1) & 0x1f) == 0))
688 /*
689 * let's be a little nice with other processes
690 * that need some CPU
691 */
692 schedule();
693
694 if (get_user(c, tmp))
695 return -EFAULT;
696
697 charlcd_write_char(the_charlcd, c);
698 }
699
700 return tmp - buf;
701}
702
703static int charlcd_open(struct inode *inode, struct file *file)
704{
705 struct charlcd_priv *priv = to_priv(the_charlcd);
93dc1774 706 int ret;
39f8ea46 707
93dc1774 708 ret = -EBUSY;
39f8ea46 709 if (!atomic_dec_and_test(&charlcd_available))
93dc1774 710 goto fail; /* open only once at a time */
39f8ea46 711
93dc1774 712 ret = -EPERM;
39f8ea46 713 if (file->f_mode & FMODE_READ) /* device is write-only */
93dc1774 714 goto fail;
39f8ea46
GU
715
716 if (priv->must_clear) {
717 charlcd_clear_display(&priv->lcd);
718 priv->must_clear = false;
719 }
720 return nonseekable_open(inode, file);
93dc1774
WT
721
722 fail:
723 atomic_inc(&charlcd_available);
724 return ret;
39f8ea46
GU
725}
726
727static int charlcd_release(struct inode *inode, struct file *file)
728{
729 atomic_inc(&charlcd_available);
730 return 0;
731}
732
733static const struct file_operations charlcd_fops = {
734 .write = charlcd_write,
735 .open = charlcd_open,
736 .release = charlcd_release,
737 .llseek = no_llseek,
738};
739
740static struct miscdevice charlcd_dev = {
741 .minor = LCD_MINOR,
742 .name = "lcd",
743 .fops = &charlcd_fops,
744};
745
746static void charlcd_puts(struct charlcd *lcd, const char *s)
747{
748 const char *tmp = s;
749 int count = strlen(s);
750
751 for (; count-- > 0; tmp++) {
752 if (!in_interrupt() && (((count + 1) & 0x1f) == 0))
753 /*
754 * let's be a little nice with other processes
755 * that need some CPU
756 */
757 schedule();
758
759 charlcd_write_char(lcd, *tmp);
760 }
761}
762
763/* initialize the LCD driver */
764static int charlcd_init(struct charlcd *lcd)
765{
766 struct charlcd_priv *priv = to_priv(lcd);
767 int ret;
768
769 if (lcd->ops->backlight) {
770 mutex_init(&priv->bl_tempo_lock);
771 INIT_DELAYED_WORK(&priv->bl_work, charlcd_bl_off);
772 }
773
774 /*
775 * before this line, we must NOT send anything to the display.
776 * Since charlcd_init_display() needs to write data, we have to
777 * enable mark the LCD initialized just before.
778 */
779 ret = charlcd_init_display(lcd);
780 if (ret)
781 return ret;
782
783 /* display a short message */
784#ifdef CONFIG_PANEL_CHANGE_MESSAGE
785#ifdef CONFIG_PANEL_BOOT_MESSAGE
786 charlcd_puts(lcd, "\x1b[Lc\x1b[Lb\x1b[L*" CONFIG_PANEL_BOOT_MESSAGE);
787#endif
788#else
789 charlcd_puts(lcd, "\x1b[Lc\x1b[Lb\x1b[L*Linux-" UTS_RELEASE "\n");
790#endif
791 /* clear the display on the next device opening */
792 priv->must_clear = true;
793 charlcd_home(lcd);
794 return 0;
795}
796
797struct charlcd *charlcd_alloc(unsigned int drvdata_size)
798{
799 struct charlcd_priv *priv;
800 struct charlcd *lcd;
801
802 priv = kzalloc(sizeof(*priv) + drvdata_size, GFP_KERNEL);
803 if (!priv)
804 return NULL;
805
806 priv->esc_seq.len = -1;
807
808 lcd = &priv->lcd;
ac201479 809 lcd->ifwidth = 8;
39f8ea46
GU
810 lcd->bwidth = DEFAULT_LCD_BWIDTH;
811 lcd->hwidth = DEFAULT_LCD_HWIDTH;
812 lcd->drvdata = priv->drvdata;
813
814 return lcd;
815}
816EXPORT_SYMBOL_GPL(charlcd_alloc);
817
818static int panel_notify_sys(struct notifier_block *this, unsigned long code,
819 void *unused)
820{
821 struct charlcd *lcd = the_charlcd;
822
823 switch (code) {
824 case SYS_DOWN:
825 charlcd_puts(lcd,
826 "\x0cReloading\nSystem...\x1b[Lc\x1b[Lb\x1b[L+");
827 break;
828 case SYS_HALT:
829 charlcd_puts(lcd, "\x0cSystem Halted.\x1b[Lc\x1b[Lb\x1b[L+");
830 break;
831 case SYS_POWER_OFF:
832 charlcd_puts(lcd, "\x0cPower off.\x1b[Lc\x1b[Lb\x1b[L+");
833 break;
834 default:
835 break;
836 }
837 return NOTIFY_DONE;
838}
839
840static struct notifier_block panel_notifier = {
841 panel_notify_sys,
842 NULL,
843 0
844};
845
846int charlcd_register(struct charlcd *lcd)
847{
848 int ret;
849
850 ret = charlcd_init(lcd);
851 if (ret)
852 return ret;
853
854 ret = misc_register(&charlcd_dev);
855 if (ret)
856 return ret;
857
858 the_charlcd = lcd;
859 register_reboot_notifier(&panel_notifier);
860 return 0;
861}
862EXPORT_SYMBOL_GPL(charlcd_register);
863
864int charlcd_unregister(struct charlcd *lcd)
865{
866 struct charlcd_priv *priv = to_priv(lcd);
867
868 unregister_reboot_notifier(&panel_notifier);
869 charlcd_puts(lcd, "\x0cLCD driver unloaded.\x1b[Lc\x1b[Lb\x1b[L-");
870 misc_deregister(&charlcd_dev);
871 the_charlcd = NULL;
872 if (lcd->ops->backlight) {
873 cancel_delayed_work_sync(&priv->bl_work);
874 priv->lcd.ops->backlight(&priv->lcd, 0);
875 }
876
877 return 0;
878}
879EXPORT_SYMBOL_GPL(charlcd_unregister);
880
881MODULE_LICENSE("GPL");