Linux Audio

Check our new training course

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