blob: 0f0568a4bd35cb4c04c358fe240ac45b939e8733 [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;
Lars Poeschela2060f22020-11-03 10:58:21 +0100262 if (priv->flags != oldflags)
263 charlcd_backlight(lcd, CHARLCD_ON);
264
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100265 processed = 1;
266 break;
267 case '-': /* Back light OFF */
268 priv->flags &= ~LCD_FLAG_L;
Lars Poeschela2060f22020-11-03 10:58:21 +0100269 if (priv->flags != oldflags)
270 charlcd_backlight(lcd, CHARLCD_OFF);
271
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100272 processed = 1;
273 break;
274 case '*': /* Flash back light */
275 charlcd_poke(lcd);
276 processed = 1;
277 break;
278 case 'f': /* Small Font */
279 priv->flags &= ~LCD_FLAG_F;
Lars Poescheld2f21872020-11-03 10:58:18 +0100280 if (priv->flags != oldflags)
281 lcd->ops->fontsize(lcd, CHARLCD_FONTSIZE_SMALL);
282
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100283 processed = 1;
284 break;
285 case 'F': /* Large Font */
286 priv->flags |= LCD_FLAG_F;
Lars Poescheld2f21872020-11-03 10:58:18 +0100287 if (priv->flags != oldflags)
288 lcd->ops->fontsize(lcd, CHARLCD_FONTSIZE_LARGE);
289
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100290 processed = 1;
291 break;
292 case 'n': /* One Line */
293 priv->flags &= ~LCD_FLAG_N;
Lars Poescheld2f21872020-11-03 10:58:18 +0100294 if (priv->flags != oldflags)
295 lcd->ops->lines(lcd, CHARLCD_LINES_1);
296
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100297 processed = 1;
298 break;
299 case 'N': /* Two Lines */
300 priv->flags |= LCD_FLAG_N;
Lars Poescheld2f21872020-11-03 10:58:18 +0100301 if (priv->flags != oldflags)
302 lcd->ops->lines(lcd, CHARLCD_LINES_2);
303
Robert Abel99b9b492018-02-26 00:54:29 +0100304 processed = 1;
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100305 break;
306 case 'l': /* Shift Cursor Left */
Lars Poeschel11588b52020-11-03 10:58:10 +0100307 if (lcd->addr.x > 0) {
Lars Poescheld2f21872020-11-03 10:58:18 +0100308 if (!lcd->ops->shift_cursor(lcd, CHARLCD_SHIFT_LEFT))
309 lcd->addr.x--;
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100310 }
Lars Poescheld2f21872020-11-03 10:58:18 +0100311
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100312 processed = 1;
313 break;
314 case 'r': /* shift cursor right */
Lars Poeschel11588b52020-11-03 10:58:10 +0100315 if (lcd->addr.x < lcd->width) {
Lars Poescheld2f21872020-11-03 10:58:18 +0100316 if (!lcd->ops->shift_cursor(lcd, CHARLCD_SHIFT_RIGHT))
317 lcd->addr.x++;
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100318 }
Lars Poescheld2f21872020-11-03 10:58:18 +0100319
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100320 processed = 1;
321 break;
322 case 'L': /* shift display left */
Lars Poescheld2f21872020-11-03 10:58:18 +0100323 lcd->ops->shift_display(lcd, CHARLCD_SHIFT_LEFT);
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100324 processed = 1;
325 break;
326 case 'R': /* shift display right */
Lars Poescheld2f21872020-11-03 10:58:18 +0100327 lcd->ops->shift_display(lcd, CHARLCD_SHIFT_RIGHT);
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100328 processed = 1;
329 break;
330 case 'k': { /* kill end of line */
Lars Poeschelb26deab2020-11-03 10:58:11 +0100331 int x, xs, ys;
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100332
Lars Poeschelb26deab2020-11-03 10:58:11 +0100333 xs = lcd->addr.x;
334 ys = lcd->addr.y;
Lars Poeschel11588b52020-11-03 10:58:10 +0100335 for (x = lcd->addr.x; x < hdc->bwidth; x++)
Lars Poeschelb26deab2020-11-03 10:58:11 +0100336 lcd->ops->print(lcd, ' ');
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100337
338 /* restore cursor position */
Lars Poeschelb26deab2020-11-03 10:58:11 +0100339 lcd->addr.x = xs;
340 lcd->addr.y = ys;
Lars Poescheld3a2fb82020-11-03 10:58:12 +0100341 lcd->ops->gotoxy(lcd);
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100342 processed = 1;
343 break;
344 }
345 case 'I': /* reinitialize display */
Lars Poeschel01ec46d2020-11-03 10:58:17 +0100346 lcd->ops->init_display(lcd);
347 priv->flags = ((lcd->height > 1) ? LCD_FLAG_N : 0) | LCD_FLAG_D |
348 LCD_FLAG_C | LCD_FLAG_B;
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100349 processed = 1;
350 break;
Lars Poeschel339acb02020-11-03 10:58:20 +0100351 case 'G':
352 if (lcd->ops->redefine_char)
353 processed = lcd->ops->redefine_char(lcd, esc);
354 else
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100355 processed = 1;
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100356 break;
Lars Poeschel339acb02020-11-03 10:58:20 +0100357
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100358 case 'x': /* gotoxy : LxXXX[yYYY]; */
359 case 'y': /* gotoxy : LyYYY[xXXX]; */
Mans Rullgard9bc30ab2018-12-05 13:52:47 +0000360 if (priv->esc_seq.buf[priv->esc_seq.len - 1] != ';')
361 break;
362
Miguel Ojedab34050f2018-02-27 23:09:52 +0100363 /* If the command is valid, move to the new address */
Lars Poeschel11588b52020-11-03 10:58:10 +0100364 if (parse_xy(esc, &lcd->addr.x, &lcd->addr.y))
Lars Poescheld3a2fb82020-11-03 10:58:12 +0100365 lcd->ops->gotoxy(lcd);
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100366
Miguel Ojedab34050f2018-02-27 23:09:52 +0100367 /* Regardless of its validity, mark as processed */
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100368 processed = 1;
369 break;
370 }
371
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100372 return processed;
373}
374
375static void charlcd_write_char(struct charlcd *lcd, char c)
376{
Andy Shevchenkob658a212019-03-12 16:44:29 +0200377 struct charlcd_priv *priv = charlcd_to_priv(lcd);
Lars Poeschel2545c1c2020-11-03 10:58:06 +0100378 struct hd44780_common *hdc = lcd->drvdata;
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100379
380 /* first, we'll test if we're in escape mode */
381 if ((c != '\n') && priv->esc_seq.len >= 0) {
382 /* yes, let's add this char to the buffer */
383 priv->esc_seq.buf[priv->esc_seq.len++] = c;
Robert Abel8c483752018-02-10 00:50:11 +0100384 priv->esc_seq.buf[priv->esc_seq.len] = '\0';
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100385 } else {
386 /* aborts any previous escape sequence */
387 priv->esc_seq.len = -1;
388
389 switch (c) {
390 case LCD_ESCAPE_CHAR:
391 /* start of an escape sequence */
392 priv->esc_seq.len = 0;
Robert Abel8c483752018-02-10 00:50:11 +0100393 priv->esc_seq.buf[priv->esc_seq.len] = '\0';
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100394 break;
395 case '\b':
396 /* go back one char and clear it */
Lars Poeschel11588b52020-11-03 10:58:10 +0100397 if (lcd->addr.x > 0) {
Lars Poescheld2f21872020-11-03 10:58:18 +0100398 /* back one char */
399 if (!lcd->ops->shift_cursor(lcd,
400 CHARLCD_SHIFT_LEFT))
401 lcd->addr.x--;
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100402 }
403 /* replace with a space */
Lars Poescheld2f21872020-11-03 10:58:18 +0100404 charlcd_print(lcd, ' ');
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100405 /* back one char again */
Lars Poescheld2f21872020-11-03 10:58:18 +0100406 if (!lcd->ops->shift_cursor(lcd, CHARLCD_SHIFT_LEFT))
407 lcd->addr.x--;
408
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100409 break;
Robert Abel9629ccc2018-02-10 00:50:12 +0100410 case '\f':
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100411 /* quickly clear the display */
412 charlcd_clear_fast(lcd);
413 break;
414 case '\n':
415 /*
416 * flush the remainder of the current line and
417 * go to the beginning of the next line
418 */
Lars Poeschel11588b52020-11-03 10:58:10 +0100419 for (; lcd->addr.x < hdc->bwidth; lcd->addr.x++)
Lars Poeschelb26deab2020-11-03 10:58:11 +0100420 lcd->ops->print(lcd, ' ');
421
Lars Poeschel11588b52020-11-03 10:58:10 +0100422 lcd->addr.x = 0;
423 lcd->addr.y = (lcd->addr.y + 1) % lcd->height;
Lars Poescheld3a2fb82020-11-03 10:58:12 +0100424 lcd->ops->gotoxy(lcd);
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100425 break;
426 case '\r':
427 /* go to the beginning of the same line */
Lars Poeschel11588b52020-11-03 10:58:10 +0100428 lcd->addr.x = 0;
Lars Poescheld3a2fb82020-11-03 10:58:12 +0100429 lcd->ops->gotoxy(lcd);
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100430 break;
431 case '\t':
432 /* print a space instead of the tab */
433 charlcd_print(lcd, ' ');
434 break;
435 default:
436 /* simply print this char */
437 charlcd_print(lcd, c);
438 break;
439 }
440 }
441
442 /*
443 * now we'll see if we're in an escape mode and if the current
444 * escape sequence can be understood.
445 */
446 if (priv->esc_seq.len >= 2) {
447 int processed = 0;
448
449 if (!strcmp(priv->esc_seq.buf, "[2J")) {
450 /* clear the display */
451 charlcd_clear_fast(lcd);
452 processed = 1;
453 } else if (!strcmp(priv->esc_seq.buf, "[H")) {
454 /* cursor to home */
455 charlcd_home(lcd);
456 processed = 1;
457 }
458 /* codes starting with ^[[L */
459 else if ((priv->esc_seq.len >= 3) &&
460 (priv->esc_seq.buf[0] == '[') &&
461 (priv->esc_seq.buf[1] == 'L')) {
462 processed = handle_lcd_special_code(lcd);
463 }
464
465 /* LCD special escape codes */
466 /*
467 * flush the escape sequence if it's been processed
468 * or if it is getting too long.
469 */
470 if (processed || (priv->esc_seq.len >= LCD_ESCAPE_LEN))
471 priv->esc_seq.len = -1;
472 } /* escape codes */
473}
474
475static struct charlcd *the_charlcd;
476
477static ssize_t charlcd_write(struct file *file, const char __user *buf,
478 size_t count, loff_t *ppos)
479{
480 const char __user *tmp = buf;
481 char c;
482
483 for (; count-- > 0; (*ppos)++, tmp++) {
484 if (!in_interrupt() && (((count + 1) & 0x1f) == 0))
485 /*
486 * let's be a little nice with other processes
487 * that need some CPU
488 */
489 schedule();
490
491 if (get_user(c, tmp))
492 return -EFAULT;
493
494 charlcd_write_char(the_charlcd, c);
495 }
496
497 return tmp - buf;
498}
499
500static int charlcd_open(struct inode *inode, struct file *file)
501{
Andy Shevchenkob658a212019-03-12 16:44:29 +0200502 struct charlcd_priv *priv = charlcd_to_priv(the_charlcd);
Willy Tarreau93dc1772017-09-07 15:37:30 +0200503 int ret;
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100504
Willy Tarreau93dc1772017-09-07 15:37:30 +0200505 ret = -EBUSY;
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100506 if (!atomic_dec_and_test(&charlcd_available))
Willy Tarreau93dc1772017-09-07 15:37:30 +0200507 goto fail; /* open only once at a time */
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100508
Willy Tarreau93dc1772017-09-07 15:37:30 +0200509 ret = -EPERM;
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100510 if (file->f_mode & FMODE_READ) /* device is write-only */
Willy Tarreau93dc1772017-09-07 15:37:30 +0200511 goto fail;
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100512
513 if (priv->must_clear) {
Lars Poeschel45421ff2020-11-03 10:58:14 +0100514 priv->lcd.ops->clear_display(&priv->lcd);
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100515 priv->must_clear = false;
Lars Poeschel45421ff2020-11-03 10:58:14 +0100516 priv->lcd.addr.x = 0;
517 priv->lcd.addr.y = 0;
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100518 }
519 return nonseekable_open(inode, file);
Willy Tarreau93dc1772017-09-07 15:37:30 +0200520
521 fail:
522 atomic_inc(&charlcd_available);
523 return ret;
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100524}
525
526static int charlcd_release(struct inode *inode, struct file *file)
527{
528 atomic_inc(&charlcd_available);
529 return 0;
530}
531
532static const struct file_operations charlcd_fops = {
533 .write = charlcd_write,
534 .open = charlcd_open,
535 .release = charlcd_release,
536 .llseek = no_llseek,
537};
538
539static struct miscdevice charlcd_dev = {
540 .minor = LCD_MINOR,
541 .name = "lcd",
542 .fops = &charlcd_fops,
543};
544
545static void charlcd_puts(struct charlcd *lcd, const char *s)
546{
547 const char *tmp = s;
548 int count = strlen(s);
549
550 for (; count-- > 0; tmp++) {
551 if (!in_interrupt() && (((count + 1) & 0x1f) == 0))
552 /*
553 * let's be a little nice with other processes
554 * that need some CPU
555 */
556 schedule();
557
558 charlcd_write_char(lcd, *tmp);
559 }
560}
561
Mans Rullgardc9171722019-03-01 18:48:15 +0000562#ifdef CONFIG_PANEL_BOOT_MESSAGE
563#define LCD_INIT_TEXT CONFIG_PANEL_BOOT_MESSAGE
564#else
565#define LCD_INIT_TEXT "Linux-" UTS_RELEASE "\n"
566#endif
567
Mans Rullgardcc5d04d2019-03-01 18:48:16 +0000568#ifdef CONFIG_CHARLCD_BL_ON
569#define LCD_INIT_BL "\x1b[L+"
570#elif defined(CONFIG_CHARLCD_BL_FLASH)
571#define LCD_INIT_BL "\x1b[L*"
572#else
573#define LCD_INIT_BL "\x1b[L-"
574#endif
575
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100576/* initialize the LCD driver */
577static int charlcd_init(struct charlcd *lcd)
578{
Andy Shevchenkob658a212019-03-12 16:44:29 +0200579 struct charlcd_priv *priv = charlcd_to_priv(lcd);
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100580 int ret;
581
Lars Poeschel01ec46d2020-11-03 10:58:17 +0100582 priv->flags = ((lcd->height > 1) ? LCD_FLAG_N : 0) | LCD_FLAG_D |
583 LCD_FLAG_C | LCD_FLAG_B;
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100584 if (lcd->ops->backlight) {
585 mutex_init(&priv->bl_tempo_lock);
586 INIT_DELAYED_WORK(&priv->bl_work, charlcd_bl_off);
587 }
588
589 /*
590 * before this line, we must NOT send anything to the display.
591 * Since charlcd_init_display() needs to write data, we have to
592 * enable mark the LCD initialized just before.
593 */
Lars Poeschel01ec46d2020-11-03 10:58:17 +0100594 ret = lcd->ops->init_display(lcd);
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100595 if (ret)
596 return ret;
597
598 /* display a short message */
Mans Rullgardcc5d04d2019-03-01 18:48:16 +0000599 charlcd_puts(lcd, "\x1b[Lc\x1b[Lb" LCD_INIT_BL LCD_INIT_TEXT);
Mans Rullgardc9171722019-03-01 18:48:15 +0000600
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100601 /* clear the display on the next device opening */
602 priv->must_clear = true;
603 charlcd_home(lcd);
604 return 0;
605}
606
Lars Poeschel2545c1c2020-11-03 10:58:06 +0100607struct charlcd *charlcd_alloc(void)
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100608{
609 struct charlcd_priv *priv;
610 struct charlcd *lcd;
611
Lars Poeschel2545c1c2020-11-03 10:58:06 +0100612 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100613 if (!priv)
614 return NULL;
615
616 priv->esc_seq.len = -1;
617
618 lcd = &priv->lcd;
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100619
620 return lcd;
621}
622EXPORT_SYMBOL_GPL(charlcd_alloc);
623
Andy Shevchenko8e44fc82019-03-12 16:44:30 +0200624void charlcd_free(struct charlcd *lcd)
625{
626 kfree(charlcd_to_priv(lcd));
627}
628EXPORT_SYMBOL_GPL(charlcd_free);
629
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100630static int panel_notify_sys(struct notifier_block *this, unsigned long code,
631 void *unused)
632{
633 struct charlcd *lcd = the_charlcd;
634
635 switch (code) {
636 case SYS_DOWN:
637 charlcd_puts(lcd,
638 "\x0cReloading\nSystem...\x1b[Lc\x1b[Lb\x1b[L+");
639 break;
640 case SYS_HALT:
641 charlcd_puts(lcd, "\x0cSystem Halted.\x1b[Lc\x1b[Lb\x1b[L+");
642 break;
643 case SYS_POWER_OFF:
644 charlcd_puts(lcd, "\x0cPower off.\x1b[Lc\x1b[Lb\x1b[L+");
645 break;
646 default:
647 break;
648 }
649 return NOTIFY_DONE;
650}
651
652static struct notifier_block panel_notifier = {
653 panel_notify_sys,
654 NULL,
655 0
656};
657
658int charlcd_register(struct charlcd *lcd)
659{
660 int ret;
661
662 ret = charlcd_init(lcd);
663 if (ret)
664 return ret;
665
666 ret = misc_register(&charlcd_dev);
667 if (ret)
668 return ret;
669
670 the_charlcd = lcd;
671 register_reboot_notifier(&panel_notifier);
672 return 0;
673}
674EXPORT_SYMBOL_GPL(charlcd_register);
675
676int charlcd_unregister(struct charlcd *lcd)
677{
Andy Shevchenkob658a212019-03-12 16:44:29 +0200678 struct charlcd_priv *priv = charlcd_to_priv(lcd);
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100679
680 unregister_reboot_notifier(&panel_notifier);
681 charlcd_puts(lcd, "\x0cLCD driver unloaded.\x1b[Lc\x1b[Lb\x1b[L-");
682 misc_deregister(&charlcd_dev);
683 the_charlcd = NULL;
684 if (lcd->ops->backlight) {
685 cancel_delayed_work_sync(&priv->bl_work);
Lars Poeschelbd26b182020-11-03 10:58:16 +0100686 priv->lcd.ops->backlight(&priv->lcd, CHARLCD_OFF);
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100687 }
688
689 return 0;
690}
691EXPORT_SYMBOL_GPL(charlcd_unregister);
692
693MODULE_LICENSE("GPL");