blob: dca1b138a239139001434702d91e06ad640d8b21 [file] [log] [blame]
Miguel Ojeda351f683b2018-02-17 20:33:13 +01001// SPDX-License-Identifier: GPL-2.0+
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +01002/*
3 * Character LCD driver for Linux
4 *
5 * Copyright (C) 2000-2008, Willy Tarreau <w@1wt.eu>
6 * Copyright (C) 2016-2017 Glider bvba
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +01007 */
8
9#include <linux/atomic.h>
Miguel Ojedab34050f2018-02-27 23:09:52 +010010#include <linux/ctype.h>
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +010011#include <linux/fs.h>
12#include <linux/miscdevice.h>
13#include <linux/module.h>
14#include <linux/notifier.h>
15#include <linux/reboot.h>
16#include <linux/slab.h>
17#include <linux/uaccess.h>
18#include <linux/workqueue.h>
19
20#include <generated/utsrelease.h>
21
Masahiro Yamada75354282019-08-06 16:14:44 +090022#include "charlcd.h"
Lars Poeschel2545c1c2020-11-03 10:58:06 +010023#include "hd44780_common.h"
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +010024
25/* Keep the backlight on this many seconds for each flash */
26#define LCD_BL_TEMPO_PERIOD 4
27
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +010028#define LCD_ESCAPE_LEN 24 /* Max chars for LCD escape command */
29#define LCD_ESCAPE_CHAR 27 /* Use char 27 for escape command */
30
31struct charlcd_priv {
32 struct charlcd lcd;
33
34 struct delayed_work bl_work;
35 struct mutex bl_tempo_lock; /* Protects access to bl_tempo */
36 bool bl_tempo;
37
38 bool must_clear;
39
40 /* contains the LCD config state */
41 unsigned long int flags;
42
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +010043 /* Current escape sequence and it's length or -1 if outside */
44 struct {
45 char buf[LCD_ESCAPE_LEN + 1];
46 int len;
47 } esc_seq;
48
Gustavo A. R. Silva2f920c02020-02-12 13:52:31 -060049 unsigned long long drvdata[];
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +010050};
51
Andy Shevchenkob658a212019-03-12 16:44:29 +020052#define charlcd_to_priv(p) container_of(p, struct charlcd_priv, lcd)
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +010053
54/* Device single-open policy control */
55static atomic_t charlcd_available = ATOMIC_INIT(1);
56
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +010057/* turn the backlight on or off */
Lars Poeschel2bf82b52020-11-03 10:58:15 +010058void charlcd_backlight(struct charlcd *lcd, enum charlcd_onoff on)
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +010059{
Andy Shevchenkob658a212019-03-12 16:44:29 +020060 struct charlcd_priv *priv = charlcd_to_priv(lcd);
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +010061
62 if (!lcd->ops->backlight)
63 return;
64
65 mutex_lock(&priv->bl_tempo_lock);
66 if (!priv->bl_tempo)
67 lcd->ops->backlight(lcd, on);
68 mutex_unlock(&priv->bl_tempo_lock);
69}
Lars Poeschel2bf82b52020-11-03 10:58:15 +010070EXPORT_SYMBOL_GPL(charlcd_backlight);
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +010071
72static void charlcd_bl_off(struct work_struct *work)
73{
74 struct delayed_work *dwork = to_delayed_work(work);
75 struct charlcd_priv *priv =
76 container_of(dwork, struct charlcd_priv, bl_work);
77
78 mutex_lock(&priv->bl_tempo_lock);
79 if (priv->bl_tempo) {
80 priv->bl_tempo = false;
81 if (!(priv->flags & LCD_FLAG_L))
Lars Poeschelbd26b182020-11-03 10:58:16 +010082 priv->lcd.ops->backlight(&priv->lcd, CHARLCD_OFF);
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +010083 }
84 mutex_unlock(&priv->bl_tempo_lock);
85}
86
87/* turn the backlight on for a little while */
88void charlcd_poke(struct charlcd *lcd)
89{
Andy Shevchenkob658a212019-03-12 16:44:29 +020090 struct charlcd_priv *priv = charlcd_to_priv(lcd);
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +010091
92 if (!lcd->ops->backlight)
93 return;
94
95 cancel_delayed_work_sync(&priv->bl_work);
96
97 mutex_lock(&priv->bl_tempo_lock);
98 if (!priv->bl_tempo && !(priv->flags & LCD_FLAG_L))
Lars Poeschelbd26b182020-11-03 10:58:16 +010099 lcd->ops->backlight(lcd, CHARLCD_ON);
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100100 priv->bl_tempo = true;
101 schedule_delayed_work(&priv->bl_work, LCD_BL_TEMPO_PERIOD * HZ);
102 mutex_unlock(&priv->bl_tempo_lock);
103}
104EXPORT_SYMBOL_GPL(charlcd_poke);
105
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100106static void charlcd_home(struct charlcd *lcd)
107{
Lars Poeschel11588b52020-11-03 10:58:10 +0100108 lcd->addr.x = 0;
109 lcd->addr.y = 0;
Lars Poeschel88645a82020-11-03 10:58:13 +0100110 lcd->ops->home(lcd);
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100111}
112
113static void charlcd_print(struct charlcd *lcd, char c)
114{
Lars Poescheld3a2fb82020-11-03 10:58:12 +0100115 struct hd44780_common *hdc = lcd->drvdata;
116
Lars Poeschelb26deab2020-11-03 10:58:11 +0100117 if (lcd->char_conv)
118 c = lcd->char_conv[(unsigned char)c];
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100119
Lars Poeschelb26deab2020-11-03 10:58:11 +0100120 if (!lcd->ops->print(lcd, c))
Lars Poeschel11588b52020-11-03 10:58:10 +0100121 lcd->addr.x++;
Sean Young54bc937f2018-01-15 09:43:48 +0000122
Lars Poeschelb26deab2020-11-03 10:58:11 +0100123 /* prevents the cursor from wrapping onto the next line */
Lars Poescheld3a2fb82020-11-03 10:58:12 +0100124 if (lcd->addr.x == hdc->bwidth)
125 lcd->ops->gotoxy(lcd);
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100126}
127
128static void charlcd_clear_fast(struct charlcd *lcd)
129{
Lars Poeschel2545c1c2020-11-03 10:58:06 +0100130 struct hd44780_common *hdc = lcd->drvdata;
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100131 int pos;
132
133 charlcd_home(lcd);
134
135 if (lcd->ops->clear_fast)
136 lcd->ops->clear_fast(lcd);
137 else
Lars Poeschel2545c1c2020-11-03 10:58:06 +0100138 for (pos = 0; pos < min(2, lcd->height) * hdc->hwidth; pos++)
Lars Poeschelb26deab2020-11-03 10:58:11 +0100139 lcd->ops->print(lcd, ' ');
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100140
141 charlcd_home(lcd);
142}
143
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100144/*
Miguel Ojedab34050f2018-02-27 23:09:52 +0100145 * Parses a movement command of the form "(.*);", where the group can be
146 * any number of subcommands of the form "(x|y)[0-9]+".
147 *
148 * Returns whether the command is valid. The position arguments are
149 * only written if the parsing was successful.
150 *
151 * For instance:
152 * - ";" returns (<original x>, <original y>).
153 * - "x1;" returns (1, <original y>).
154 * - "y2x1;" returns (1, 2).
155 * - "x12y34x56;" returns (56, 34).
156 * - "" fails.
157 * - "x" fails.
158 * - "x;" fails.
159 * - "x1" fails.
160 * - "xy12;" fails.
161 * - "x12yy12;" fails.
162 * - "xx" fails.
163 */
164static bool parse_xy(const char *s, unsigned long *x, unsigned long *y)
165{
166 unsigned long new_x = *x;
167 unsigned long new_y = *y;
Andy Shevchenkod717e7d2019-12-04 16:50:36 -0800168 char *p;
Miguel Ojedab34050f2018-02-27 23:09:52 +0100169
170 for (;;) {
171 if (!*s)
172 return false;
173
174 if (*s == ';')
175 break;
176
177 if (*s == 'x') {
Andy Shevchenkod717e7d2019-12-04 16:50:36 -0800178 new_x = simple_strtoul(s + 1, &p, 10);
179 if (p == s + 1)
Miguel Ojedab34050f2018-02-27 23:09:52 +0100180 return false;
Andy Shevchenkod717e7d2019-12-04 16:50:36 -0800181 s = p;
Miguel Ojedab34050f2018-02-27 23:09:52 +0100182 } else if (*s == 'y') {
Andy Shevchenkod717e7d2019-12-04 16:50:36 -0800183 new_y = simple_strtoul(s + 1, &p, 10);
184 if (p == s + 1)
Miguel Ojedab34050f2018-02-27 23:09:52 +0100185 return false;
Andy Shevchenkod717e7d2019-12-04 16:50:36 -0800186 s = p;
Miguel Ojedab34050f2018-02-27 23:09:52 +0100187 } else {
188 return false;
189 }
190 }
191
192 *x = new_x;
193 *y = new_y;
194 return true;
195}
196
197/*
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100198 * These are the file operation function for user access to /dev/lcd
199 * This function can also be called from inside the kernel, by
200 * setting file and ppos to NULL.
201 *
202 */
203
204static inline int handle_lcd_special_code(struct charlcd *lcd)
205{
Andy Shevchenkob658a212019-03-12 16:44:29 +0200206 struct charlcd_priv *priv = charlcd_to_priv(lcd);
Lars Poeschel2545c1c2020-11-03 10:58:06 +0100207 struct hd44780_common *hdc = lcd->drvdata;
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100208
209 /* LCD special codes */
210
211 int processed = 0;
212
213 char *esc = priv->esc_seq.buf + 2;
214 int oldflags = priv->flags;
215
216 /* check for display mode flags */
217 switch (*esc) {
218 case 'D': /* Display ON */
219 priv->flags |= LCD_FLAG_D;
Lars Poescheld2f21872020-11-03 10:58:18 +0100220 if (priv->flags != oldflags)
221 lcd->ops->display(lcd, CHARLCD_ON);
222
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100223 processed = 1;
224 break;
225 case 'd': /* Display OFF */
226 priv->flags &= ~LCD_FLAG_D;
Lars Poescheld2f21872020-11-03 10:58:18 +0100227 if (priv->flags != oldflags)
228 lcd->ops->display(lcd, CHARLCD_OFF);
229
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100230 processed = 1;
231 break;
232 case 'C': /* Cursor ON */
233 priv->flags |= LCD_FLAG_C;
Lars Poescheld2f21872020-11-03 10:58:18 +0100234 if (priv->flags != oldflags)
235 lcd->ops->cursor(lcd, CHARLCD_ON);
236
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100237 processed = 1;
238 break;
239 case 'c': /* Cursor OFF */
240 priv->flags &= ~LCD_FLAG_C;
Lars Poescheld2f21872020-11-03 10:58:18 +0100241 if (priv->flags != oldflags)
242 lcd->ops->cursor(lcd, CHARLCD_OFF);
243
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100244 processed = 1;
245 break;
246 case 'B': /* Blink ON */
247 priv->flags |= LCD_FLAG_B;
Lars Poescheld2f21872020-11-03 10:58:18 +0100248 if (priv->flags != oldflags)
249 lcd->ops->blink(lcd, CHARLCD_ON);
250
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100251 processed = 1;
252 break;
253 case 'b': /* Blink OFF */
254 priv->flags &= ~LCD_FLAG_B;
Lars Poescheld2f21872020-11-03 10:58:18 +0100255 if (priv->flags != oldflags)
256 lcd->ops->blink(lcd, CHARLCD_OFF);
257
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100258 processed = 1;
259 break;
260 case '+': /* Back light ON */
261 priv->flags |= LCD_FLAG_L;
262 processed = 1;
263 break;
264 case '-': /* Back light OFF */
265 priv->flags &= ~LCD_FLAG_L;
266 processed = 1;
267 break;
268 case '*': /* Flash back light */
269 charlcd_poke(lcd);
270 processed = 1;
271 break;
272 case 'f': /* Small Font */
273 priv->flags &= ~LCD_FLAG_F;
Lars Poescheld2f21872020-11-03 10:58:18 +0100274 if (priv->flags != oldflags)
275 lcd->ops->fontsize(lcd, CHARLCD_FONTSIZE_SMALL);
276
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100277 processed = 1;
278 break;
279 case 'F': /* Large Font */
280 priv->flags |= LCD_FLAG_F;
Lars Poescheld2f21872020-11-03 10:58:18 +0100281 if (priv->flags != oldflags)
282 lcd->ops->fontsize(lcd, CHARLCD_FONTSIZE_LARGE);
283
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100284 processed = 1;
285 break;
286 case 'n': /* One Line */
287 priv->flags &= ~LCD_FLAG_N;
Lars Poescheld2f21872020-11-03 10:58:18 +0100288 if (priv->flags != oldflags)
289 lcd->ops->lines(lcd, CHARLCD_LINES_1);
290
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100291 processed = 1;
292 break;
293 case 'N': /* Two Lines */
294 priv->flags |= LCD_FLAG_N;
Lars Poescheld2f21872020-11-03 10:58:18 +0100295 if (priv->flags != oldflags)
296 lcd->ops->lines(lcd, CHARLCD_LINES_2);
297
Robert Abel99b9b492018-02-26 00:54:29 +0100298 processed = 1;
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100299 break;
300 case 'l': /* Shift Cursor Left */
Lars Poeschel11588b52020-11-03 10:58:10 +0100301 if (lcd->addr.x > 0) {
Lars Poescheld2f21872020-11-03 10:58:18 +0100302 if (!lcd->ops->shift_cursor(lcd, CHARLCD_SHIFT_LEFT))
303 lcd->addr.x--;
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100304 }
Lars Poescheld2f21872020-11-03 10:58:18 +0100305
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100306 processed = 1;
307 break;
308 case 'r': /* shift cursor right */
Lars Poeschel11588b52020-11-03 10:58:10 +0100309 if (lcd->addr.x < lcd->width) {
Lars Poescheld2f21872020-11-03 10:58:18 +0100310 if (!lcd->ops->shift_cursor(lcd, CHARLCD_SHIFT_RIGHT))
311 lcd->addr.x++;
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100312 }
Lars Poescheld2f21872020-11-03 10:58:18 +0100313
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100314 processed = 1;
315 break;
316 case 'L': /* shift display left */
Lars Poescheld2f21872020-11-03 10:58:18 +0100317 lcd->ops->shift_display(lcd, CHARLCD_SHIFT_LEFT);
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100318 processed = 1;
319 break;
320 case 'R': /* shift display right */
Lars Poescheld2f21872020-11-03 10:58:18 +0100321 lcd->ops->shift_display(lcd, CHARLCD_SHIFT_RIGHT);
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100322 processed = 1;
323 break;
324 case 'k': { /* kill end of line */
Lars Poeschelb26deab2020-11-03 10:58:11 +0100325 int x, xs, ys;
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100326
Lars Poeschelb26deab2020-11-03 10:58:11 +0100327 xs = lcd->addr.x;
328 ys = lcd->addr.y;
Lars Poeschel11588b52020-11-03 10:58:10 +0100329 for (x = lcd->addr.x; x < hdc->bwidth; x++)
Lars Poeschelb26deab2020-11-03 10:58:11 +0100330 lcd->ops->print(lcd, ' ');
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100331
332 /* restore cursor position */
Lars Poeschelb26deab2020-11-03 10:58:11 +0100333 lcd->addr.x = xs;
334 lcd->addr.y = ys;
Lars Poescheld3a2fb82020-11-03 10:58:12 +0100335 lcd->ops->gotoxy(lcd);
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100336 processed = 1;
337 break;
338 }
339 case 'I': /* reinitialize display */
Lars Poeschel01ec46d2020-11-03 10:58:17 +0100340 lcd->ops->init_display(lcd);
341 priv->flags = ((lcd->height > 1) ? LCD_FLAG_N : 0) | LCD_FLAG_D |
342 LCD_FLAG_C | LCD_FLAG_B;
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100343 processed = 1;
344 break;
Lars Poeschel339acb02020-11-03 10:58:20 +0100345 case 'G':
346 if (lcd->ops->redefine_char)
347 processed = lcd->ops->redefine_char(lcd, esc);
348 else
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100349 processed = 1;
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100350 break;
Lars Poeschel339acb02020-11-03 10:58:20 +0100351
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100352 case 'x': /* gotoxy : LxXXX[yYYY]; */
353 case 'y': /* gotoxy : LyYYY[xXXX]; */
Mans Rullgard9bc30ab2018-12-05 13:52:47 +0000354 if (priv->esc_seq.buf[priv->esc_seq.len - 1] != ';')
355 break;
356
Miguel Ojedab34050f2018-02-27 23:09:52 +0100357 /* If the command is valid, move to the new address */
Lars Poeschel11588b52020-11-03 10:58:10 +0100358 if (parse_xy(esc, &lcd->addr.x, &lcd->addr.y))
Lars Poescheld3a2fb82020-11-03 10:58:12 +0100359 lcd->ops->gotoxy(lcd);
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100360
Miguel Ojedab34050f2018-02-27 23:09:52 +0100361 /* Regardless of its validity, mark as processed */
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100362 processed = 1;
363 break;
364 }
365
366 /* TODO: This indent party here got ugly, clean it! */
367 /* Check whether one flag was changed */
368 if (oldflags == priv->flags)
369 return processed;
370
Lars Poeschel8a862702020-11-03 10:58:19 +0100371 if ((oldflags ^ priv->flags) & LCD_FLAG_L)
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100372 charlcd_backlight(lcd, !!(priv->flags & LCD_FLAG_L));
373
374 return processed;
375}
376
377static void charlcd_write_char(struct charlcd *lcd, char c)
378{
Andy Shevchenkob658a212019-03-12 16:44:29 +0200379 struct charlcd_priv *priv = charlcd_to_priv(lcd);
Lars Poeschel2545c1c2020-11-03 10:58:06 +0100380 struct hd44780_common *hdc = lcd->drvdata;
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100381
382 /* first, we'll test if we're in escape mode */
383 if ((c != '\n') && priv->esc_seq.len >= 0) {
384 /* yes, let's add this char to the buffer */
385 priv->esc_seq.buf[priv->esc_seq.len++] = c;
Robert Abel8c483752018-02-10 00:50:11 +0100386 priv->esc_seq.buf[priv->esc_seq.len] = '\0';
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100387 } else {
388 /* aborts any previous escape sequence */
389 priv->esc_seq.len = -1;
390
391 switch (c) {
392 case LCD_ESCAPE_CHAR:
393 /* start of an escape sequence */
394 priv->esc_seq.len = 0;
Robert Abel8c483752018-02-10 00:50:11 +0100395 priv->esc_seq.buf[priv->esc_seq.len] = '\0';
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100396 break;
397 case '\b':
398 /* go back one char and clear it */
Lars Poeschel11588b52020-11-03 10:58:10 +0100399 if (lcd->addr.x > 0) {
Lars Poescheld2f21872020-11-03 10:58:18 +0100400 /* back one char */
401 if (!lcd->ops->shift_cursor(lcd,
402 CHARLCD_SHIFT_LEFT))
403 lcd->addr.x--;
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100404 }
405 /* replace with a space */
Lars Poescheld2f21872020-11-03 10:58:18 +0100406 charlcd_print(lcd, ' ');
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100407 /* back one char again */
Lars Poescheld2f21872020-11-03 10:58:18 +0100408 if (!lcd->ops->shift_cursor(lcd, CHARLCD_SHIFT_LEFT))
409 lcd->addr.x--;
410
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100411 break;
Robert Abel9629ccc2018-02-10 00:50:12 +0100412 case '\f':
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100413 /* quickly clear the display */
414 charlcd_clear_fast(lcd);
415 break;
416 case '\n':
417 /*
418 * flush the remainder of the current line and
419 * go to the beginning of the next line
420 */
Lars Poeschel11588b52020-11-03 10:58:10 +0100421 for (; lcd->addr.x < hdc->bwidth; lcd->addr.x++)
Lars Poeschelb26deab2020-11-03 10:58:11 +0100422 lcd->ops->print(lcd, ' ');
423
Lars Poeschel11588b52020-11-03 10:58:10 +0100424 lcd->addr.x = 0;
425 lcd->addr.y = (lcd->addr.y + 1) % lcd->height;
Lars Poescheld3a2fb82020-11-03 10:58:12 +0100426 lcd->ops->gotoxy(lcd);
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100427 break;
428 case '\r':
429 /* go to the beginning of the same line */
Lars Poeschel11588b52020-11-03 10:58:10 +0100430 lcd->addr.x = 0;
Lars Poescheld3a2fb82020-11-03 10:58:12 +0100431 lcd->ops->gotoxy(lcd);
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100432 break;
433 case '\t':
434 /* print a space instead of the tab */
435 charlcd_print(lcd, ' ');
436 break;
437 default:
438 /* simply print this char */
439 charlcd_print(lcd, c);
440 break;
441 }
442 }
443
444 /*
445 * now we'll see if we're in an escape mode and if the current
446 * escape sequence can be understood.
447 */
448 if (priv->esc_seq.len >= 2) {
449 int processed = 0;
450
451 if (!strcmp(priv->esc_seq.buf, "[2J")) {
452 /* clear the display */
453 charlcd_clear_fast(lcd);
454 processed = 1;
455 } else if (!strcmp(priv->esc_seq.buf, "[H")) {
456 /* cursor to home */
457 charlcd_home(lcd);
458 processed = 1;
459 }
460 /* codes starting with ^[[L */
461 else if ((priv->esc_seq.len >= 3) &&
462 (priv->esc_seq.buf[0] == '[') &&
463 (priv->esc_seq.buf[1] == 'L')) {
464 processed = handle_lcd_special_code(lcd);
465 }
466
467 /* LCD special escape codes */
468 /*
469 * flush the escape sequence if it's been processed
470 * or if it is getting too long.
471 */
472 if (processed || (priv->esc_seq.len >= LCD_ESCAPE_LEN))
473 priv->esc_seq.len = -1;
474 } /* escape codes */
475}
476
477static struct charlcd *the_charlcd;
478
479static ssize_t charlcd_write(struct file *file, const char __user *buf,
480 size_t count, loff_t *ppos)
481{
482 const char __user *tmp = buf;
483 char c;
484
485 for (; count-- > 0; (*ppos)++, tmp++) {
486 if (!in_interrupt() && (((count + 1) & 0x1f) == 0))
487 /*
488 * let's be a little nice with other processes
489 * that need some CPU
490 */
491 schedule();
492
493 if (get_user(c, tmp))
494 return -EFAULT;
495
496 charlcd_write_char(the_charlcd, c);
497 }
498
499 return tmp - buf;
500}
501
502static int charlcd_open(struct inode *inode, struct file *file)
503{
Andy Shevchenkob658a212019-03-12 16:44:29 +0200504 struct charlcd_priv *priv = charlcd_to_priv(the_charlcd);
Willy Tarreau93dc1772017-09-07 15:37:30 +0200505 int ret;
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100506
Willy Tarreau93dc1772017-09-07 15:37:30 +0200507 ret = -EBUSY;
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100508 if (!atomic_dec_and_test(&charlcd_available))
Willy Tarreau93dc1772017-09-07 15:37:30 +0200509 goto fail; /* open only once at a time */
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100510
Willy Tarreau93dc1772017-09-07 15:37:30 +0200511 ret = -EPERM;
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100512 if (file->f_mode & FMODE_READ) /* device is write-only */
Willy Tarreau93dc1772017-09-07 15:37:30 +0200513 goto fail;
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100514
515 if (priv->must_clear) {
Lars Poeschel45421ff2020-11-03 10:58:14 +0100516 priv->lcd.ops->clear_display(&priv->lcd);
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100517 priv->must_clear = false;
Lars Poeschel45421ff2020-11-03 10:58:14 +0100518 priv->lcd.addr.x = 0;
519 priv->lcd.addr.y = 0;
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100520 }
521 return nonseekable_open(inode, file);
Willy Tarreau93dc1772017-09-07 15:37:30 +0200522
523 fail:
524 atomic_inc(&charlcd_available);
525 return ret;
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100526}
527
528static int charlcd_release(struct inode *inode, struct file *file)
529{
530 atomic_inc(&charlcd_available);
531 return 0;
532}
533
534static const struct file_operations charlcd_fops = {
535 .write = charlcd_write,
536 .open = charlcd_open,
537 .release = charlcd_release,
538 .llseek = no_llseek,
539};
540
541static struct miscdevice charlcd_dev = {
542 .minor = LCD_MINOR,
543 .name = "lcd",
544 .fops = &charlcd_fops,
545};
546
547static void charlcd_puts(struct charlcd *lcd, const char *s)
548{
549 const char *tmp = s;
550 int count = strlen(s);
551
552 for (; count-- > 0; tmp++) {
553 if (!in_interrupt() && (((count + 1) & 0x1f) == 0))
554 /*
555 * let's be a little nice with other processes
556 * that need some CPU
557 */
558 schedule();
559
560 charlcd_write_char(lcd, *tmp);
561 }
562}
563
Mans Rullgardc9171722019-03-01 18:48:15 +0000564#ifdef CONFIG_PANEL_BOOT_MESSAGE
565#define LCD_INIT_TEXT CONFIG_PANEL_BOOT_MESSAGE
566#else
567#define LCD_INIT_TEXT "Linux-" UTS_RELEASE "\n"
568#endif
569
Mans Rullgardcc5d04d2019-03-01 18:48:16 +0000570#ifdef CONFIG_CHARLCD_BL_ON
571#define LCD_INIT_BL "\x1b[L+"
572#elif defined(CONFIG_CHARLCD_BL_FLASH)
573#define LCD_INIT_BL "\x1b[L*"
574#else
575#define LCD_INIT_BL "\x1b[L-"
576#endif
577
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100578/* initialize the LCD driver */
579static int charlcd_init(struct charlcd *lcd)
580{
Andy Shevchenkob658a212019-03-12 16:44:29 +0200581 struct charlcd_priv *priv = charlcd_to_priv(lcd);
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100582 int ret;
583
Lars Poeschel01ec46d2020-11-03 10:58:17 +0100584 priv->flags = ((lcd->height > 1) ? LCD_FLAG_N : 0) | LCD_FLAG_D |
585 LCD_FLAG_C | LCD_FLAG_B;
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100586 if (lcd->ops->backlight) {
587 mutex_init(&priv->bl_tempo_lock);
588 INIT_DELAYED_WORK(&priv->bl_work, charlcd_bl_off);
589 }
590
591 /*
592 * before this line, we must NOT send anything to the display.
593 * Since charlcd_init_display() needs to write data, we have to
594 * enable mark the LCD initialized just before.
595 */
Lars Poeschel01ec46d2020-11-03 10:58:17 +0100596 ret = lcd->ops->init_display(lcd);
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100597 if (ret)
598 return ret;
599
600 /* display a short message */
Mans Rullgardcc5d04d2019-03-01 18:48:16 +0000601 charlcd_puts(lcd, "\x1b[Lc\x1b[Lb" LCD_INIT_BL LCD_INIT_TEXT);
Mans Rullgardc9171722019-03-01 18:48:15 +0000602
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100603 /* clear the display on the next device opening */
604 priv->must_clear = true;
605 charlcd_home(lcd);
606 return 0;
607}
608
Lars Poeschel2545c1c2020-11-03 10:58:06 +0100609struct charlcd *charlcd_alloc(void)
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100610{
611 struct charlcd_priv *priv;
612 struct charlcd *lcd;
613
Lars Poeschel2545c1c2020-11-03 10:58:06 +0100614 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100615 if (!priv)
616 return NULL;
617
618 priv->esc_seq.len = -1;
619
620 lcd = &priv->lcd;
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100621
622 return lcd;
623}
624EXPORT_SYMBOL_GPL(charlcd_alloc);
625
Andy Shevchenko8e44fc82019-03-12 16:44:30 +0200626void charlcd_free(struct charlcd *lcd)
627{
628 kfree(charlcd_to_priv(lcd));
629}
630EXPORT_SYMBOL_GPL(charlcd_free);
631
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100632static int panel_notify_sys(struct notifier_block *this, unsigned long code,
633 void *unused)
634{
635 struct charlcd *lcd = the_charlcd;
636
637 switch (code) {
638 case SYS_DOWN:
639 charlcd_puts(lcd,
640 "\x0cReloading\nSystem...\x1b[Lc\x1b[Lb\x1b[L+");
641 break;
642 case SYS_HALT:
643 charlcd_puts(lcd, "\x0cSystem Halted.\x1b[Lc\x1b[Lb\x1b[L+");
644 break;
645 case SYS_POWER_OFF:
646 charlcd_puts(lcd, "\x0cPower off.\x1b[Lc\x1b[Lb\x1b[L+");
647 break;
648 default:
649 break;
650 }
651 return NOTIFY_DONE;
652}
653
654static struct notifier_block panel_notifier = {
655 panel_notify_sys,
656 NULL,
657 0
658};
659
660int charlcd_register(struct charlcd *lcd)
661{
662 int ret;
663
664 ret = charlcd_init(lcd);
665 if (ret)
666 return ret;
667
668 ret = misc_register(&charlcd_dev);
669 if (ret)
670 return ret;
671
672 the_charlcd = lcd;
673 register_reboot_notifier(&panel_notifier);
674 return 0;
675}
676EXPORT_SYMBOL_GPL(charlcd_register);
677
678int charlcd_unregister(struct charlcd *lcd)
679{
Andy Shevchenkob658a212019-03-12 16:44:29 +0200680 struct charlcd_priv *priv = charlcd_to_priv(lcd);
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100681
682 unregister_reboot_notifier(&panel_notifier);
683 charlcd_puts(lcd, "\x0cLCD driver unloaded.\x1b[Lc\x1b[Lb\x1b[L-");
684 misc_deregister(&charlcd_dev);
685 the_charlcd = NULL;
686 if (lcd->ops->backlight) {
687 cancel_delayed_work_sync(&priv->bl_work);
Lars Poeschelbd26b182020-11-03 10:58:16 +0100688 priv->lcd.ops->backlight(&priv->lcd, CHARLCD_OFF);
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100689 }
690
691 return 0;
692}
693EXPORT_SYMBOL_GPL(charlcd_unregister);
694
695MODULE_LICENSE("GPL");