charlcd.c 19 KB

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