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