panel.c 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454
  1. /*
  2. * Front panel driver for Linux
  3. * Copyright (C) 2000-2008, Willy Tarreau <w@1wt.eu>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version
  8. * 2 of the License, or (at your option) any later version.
  9. *
  10. * This code drives an LCD module (/dev/lcd), and a keypad (/dev/keypad)
  11. * connected to a parallel printer port.
  12. *
  13. * The LCD module may either be an HD44780-like 8-bit parallel LCD, or a 1-bit
  14. * serial module compatible with Samsung's KS0074. The pins may be connected in
  15. * any combination, everything is programmable.
  16. *
  17. * The keypad consists in a matrix of push buttons connecting input pins to
  18. * data output pins or to the ground. The combinations have to be hard-coded
  19. * in the driver, though several profiles exist and adding new ones is easy.
  20. *
  21. * Several profiles are provided for commonly found LCD+keypad modules on the
  22. * market, such as those found in Nexcom's appliances.
  23. *
  24. * FIXME:
  25. * - the initialization/deinitialization process is very dirty and should
  26. * be rewritten. It may even be buggy.
  27. *
  28. * TODO:
  29. * - document 24 keys keyboard (3 rows of 8 cols, 32 diodes + 2 inputs)
  30. * - make the LCD a part of a virtual screen of Vx*Vy
  31. * - make the inputs list smp-safe
  32. * - change the keyboard to a double mapping : signals -> key_id -> values
  33. * so that applications can change values without knowing signals
  34. *
  35. */
  36. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  37. #include <linux/module.h>
  38. #include <linux/types.h>
  39. #include <linux/errno.h>
  40. #include <linux/signal.h>
  41. #include <linux/sched.h>
  42. #include <linux/spinlock.h>
  43. #include <linux/interrupt.h>
  44. #include <linux/miscdevice.h>
  45. #include <linux/slab.h>
  46. #include <linux/ioport.h>
  47. #include <linux/fcntl.h>
  48. #include <linux/init.h>
  49. #include <linux/delay.h>
  50. #include <linux/kernel.h>
  51. #include <linux/ctype.h>
  52. #include <linux/parport.h>
  53. #include <linux/list.h>
  54. #include <linux/notifier.h>
  55. #include <linux/reboot.h>
  56. #include <generated/utsrelease.h>
  57. #include <linux/io.h>
  58. #include <linux/uaccess.h>
  59. #define LCD_MINOR 156
  60. #define KEYPAD_MINOR 185
  61. #define PANEL_VERSION "0.9.5"
  62. #define LCD_MAXBYTES 256 /* max burst write */
  63. #define KEYPAD_BUFFER 64
  64. /* poll the keyboard this every second */
  65. #define INPUT_POLL_TIME (HZ / 50)
  66. /* a key starts to repeat after this times INPUT_POLL_TIME */
  67. #define KEYPAD_REP_START (10)
  68. /* a key repeats this times INPUT_POLL_TIME */
  69. #define KEYPAD_REP_DELAY (2)
  70. /* keep the light on this times INPUT_POLL_TIME for each flash */
  71. #define FLASH_LIGHT_TEMPO (200)
  72. /* converts an r_str() input to an active high, bits string : 000BAOSE */
  73. #define PNL_PINPUT(a) ((((unsigned char)(a)) ^ 0x7F) >> 3)
  74. #define PNL_PBUSY 0x80 /* inverted input, active low */
  75. #define PNL_PACK 0x40 /* direct input, active low */
  76. #define PNL_POUTPA 0x20 /* direct input, active high */
  77. #define PNL_PSELECD 0x10 /* direct input, active high */
  78. #define PNL_PERRORP 0x08 /* direct input, active low */
  79. #define PNL_PBIDIR 0x20 /* bi-directional ports */
  80. /* high to read data in or-ed with data out */
  81. #define PNL_PINTEN 0x10
  82. #define PNL_PSELECP 0x08 /* inverted output, active low */
  83. #define PNL_PINITP 0x04 /* direct output, active low */
  84. #define PNL_PAUTOLF 0x02 /* inverted output, active low */
  85. #define PNL_PSTROBE 0x01 /* inverted output */
  86. #define PNL_PD0 0x01
  87. #define PNL_PD1 0x02
  88. #define PNL_PD2 0x04
  89. #define PNL_PD3 0x08
  90. #define PNL_PD4 0x10
  91. #define PNL_PD5 0x20
  92. #define PNL_PD6 0x40
  93. #define PNL_PD7 0x80
  94. #define PIN_NONE 0
  95. #define PIN_STROBE 1
  96. #define PIN_D0 2
  97. #define PIN_D1 3
  98. #define PIN_D2 4
  99. #define PIN_D3 5
  100. #define PIN_D4 6
  101. #define PIN_D5 7
  102. #define PIN_D6 8
  103. #define PIN_D7 9
  104. #define PIN_AUTOLF 14
  105. #define PIN_INITP 16
  106. #define PIN_SELECP 17
  107. #define PIN_NOT_SET 127
  108. #define LCD_FLAG_S 0x0001
  109. #define LCD_FLAG_ID 0x0002
  110. #define LCD_FLAG_B 0x0004 /* blink on */
  111. #define LCD_FLAG_C 0x0008 /* cursor on */
  112. #define LCD_FLAG_D 0x0010 /* display on */
  113. #define LCD_FLAG_F 0x0020 /* large font mode */
  114. #define LCD_FLAG_N 0x0040 /* 2-rows mode */
  115. #define LCD_FLAG_L 0x0080 /* backlight enabled */
  116. /* LCD commands */
  117. #define LCD_CMD_DISPLAY_CLEAR 0x01 /* Clear entire display */
  118. #define LCD_CMD_ENTRY_MODE 0x04 /* Set entry mode */
  119. #define LCD_CMD_CURSOR_INC 0x02 /* Increment cursor */
  120. #define LCD_CMD_DISPLAY_CTRL 0x08 /* Display control */
  121. #define LCD_CMD_DISPLAY_ON 0x04 /* Set display on */
  122. #define LCD_CMD_CURSOR_ON 0x02 /* Set cursor on */
  123. #define LCD_CMD_BLINK_ON 0x01 /* Set blink on */
  124. #define LCD_CMD_SHIFT 0x10 /* Shift cursor/display */
  125. #define LCD_CMD_DISPLAY_SHIFT 0x08 /* Shift display instead of cursor */
  126. #define LCD_CMD_SHIFT_RIGHT 0x04 /* Shift display/cursor to the right */
  127. #define LCD_CMD_FUNCTION_SET 0x20 /* Set function */
  128. #define LCD_CMD_DATA_LEN_8BITS 0x10 /* Set data length to 8 bits */
  129. #define LCD_CMD_TWO_LINES 0x08 /* Set to two display lines */
  130. #define LCD_CMD_FONT_5X10_DOTS 0x04 /* Set char font to 5x10 dots */
  131. #define LCD_CMD_SET_CGRAM_ADDR 0x40 /* Set char generator RAM address */
  132. #define LCD_CMD_SET_DDRAM_ADDR 0x80 /* Set display data RAM address */
  133. #define LCD_ESCAPE_LEN 24 /* max chars for LCD escape command */
  134. #define LCD_ESCAPE_CHAR 27 /* use char 27 for escape command */
  135. #define NOT_SET -1
  136. /* macros to simplify use of the parallel port */
  137. #define r_ctr(x) (parport_read_control((x)->port))
  138. #define r_dtr(x) (parport_read_data((x)->port))
  139. #define r_str(x) (parport_read_status((x)->port))
  140. #define w_ctr(x, y) (parport_write_control((x)->port, (y)))
  141. #define w_dtr(x, y) (parport_write_data((x)->port, (y)))
  142. /* this defines which bits are to be used and which ones to be ignored */
  143. /* logical or of the output bits involved in the scan matrix */
  144. static __u8 scan_mask_o;
  145. /* logical or of the input bits involved in the scan matrix */
  146. static __u8 scan_mask_i;
  147. enum input_type {
  148. INPUT_TYPE_STD,
  149. INPUT_TYPE_KBD,
  150. };
  151. enum input_state {
  152. INPUT_ST_LOW,
  153. INPUT_ST_RISING,
  154. INPUT_ST_HIGH,
  155. INPUT_ST_FALLING,
  156. };
  157. struct logical_input {
  158. struct list_head list;
  159. __u64 mask;
  160. __u64 value;
  161. enum input_type type;
  162. enum input_state state;
  163. __u8 rise_time, fall_time;
  164. __u8 rise_timer, fall_timer, high_timer;
  165. union {
  166. struct { /* valid when type == INPUT_TYPE_STD */
  167. void (*press_fct)(int);
  168. void (*release_fct)(int);
  169. int press_data;
  170. int release_data;
  171. } std;
  172. struct { /* valid when type == INPUT_TYPE_KBD */
  173. /* strings can be non null-terminated */
  174. char press_str[sizeof(void *) + sizeof(int)];
  175. char repeat_str[sizeof(void *) + sizeof(int)];
  176. char release_str[sizeof(void *) + sizeof(int)];
  177. } kbd;
  178. } u;
  179. };
  180. static LIST_HEAD(logical_inputs); /* list of all defined logical inputs */
  181. /* physical contacts history
  182. * Physical contacts are a 45 bits string of 9 groups of 5 bits each.
  183. * The 8 lower groups correspond to output bits 0 to 7, and the 9th group
  184. * corresponds to the ground.
  185. * Within each group, bits are stored in the same order as read on the port :
  186. * BAPSE (busy=4, ack=3, paper empty=2, select=1, error=0).
  187. * So, each __u64 is represented like this :
  188. * 0000000000000000000BAPSEBAPSEBAPSEBAPSEBAPSEBAPSEBAPSEBAPSEBAPSE
  189. * <-----unused------><gnd><d07><d06><d05><d04><d03><d02><d01><d00>
  190. */
  191. /* what has just been read from the I/O ports */
  192. static __u64 phys_read;
  193. /* previous phys_read */
  194. static __u64 phys_read_prev;
  195. /* stabilized phys_read (phys_read|phys_read_prev) */
  196. static __u64 phys_curr;
  197. /* previous phys_curr */
  198. static __u64 phys_prev;
  199. /* 0 means that at least one logical signal needs be computed */
  200. static char inputs_stable;
  201. /* these variables are specific to the keypad */
  202. static struct {
  203. bool enabled;
  204. } keypad;
  205. static char keypad_buffer[KEYPAD_BUFFER];
  206. static int keypad_buflen;
  207. static int keypad_start;
  208. static char keypressed;
  209. static wait_queue_head_t keypad_read_wait;
  210. /* lcd-specific variables */
  211. static struct {
  212. bool enabled;
  213. bool initialized;
  214. bool must_clear;
  215. int height;
  216. int width;
  217. int bwidth;
  218. int hwidth;
  219. int charset;
  220. int proto;
  221. int light_tempo;
  222. /* TODO: use union here? */
  223. struct {
  224. int e;
  225. int rs;
  226. int rw;
  227. int cl;
  228. int da;
  229. int bl;
  230. } pins;
  231. /* contains the LCD config state */
  232. unsigned long int flags;
  233. /* Contains the LCD X and Y offset */
  234. struct {
  235. unsigned long int x;
  236. unsigned long int y;
  237. } addr;
  238. /* Current escape sequence and it's length or -1 if outside */
  239. struct {
  240. char buf[LCD_ESCAPE_LEN + 1];
  241. int len;
  242. } esc_seq;
  243. } lcd;
  244. /* Needed only for init */
  245. static int selected_lcd_type = NOT_SET;
  246. /*
  247. * Bit masks to convert LCD signals to parallel port outputs.
  248. * _d_ are values for data port, _c_ are for control port.
  249. * [0] = signal OFF, [1] = signal ON, [2] = mask
  250. */
  251. #define BIT_CLR 0
  252. #define BIT_SET 1
  253. #define BIT_MSK 2
  254. #define BIT_STATES 3
  255. /*
  256. * one entry for each bit on the LCD
  257. */
  258. #define LCD_BIT_E 0
  259. #define LCD_BIT_RS 1
  260. #define LCD_BIT_RW 2
  261. #define LCD_BIT_BL 3
  262. #define LCD_BIT_CL 4
  263. #define LCD_BIT_DA 5
  264. #define LCD_BITS 6
  265. /*
  266. * each bit can be either connected to a DATA or CTRL port
  267. */
  268. #define LCD_PORT_C 0
  269. #define LCD_PORT_D 1
  270. #define LCD_PORTS 2
  271. static unsigned char lcd_bits[LCD_PORTS][LCD_BITS][BIT_STATES];
  272. /*
  273. * LCD protocols
  274. */
  275. #define LCD_PROTO_PARALLEL 0
  276. #define LCD_PROTO_SERIAL 1
  277. #define LCD_PROTO_TI_DA8XX_LCD 2
  278. /*
  279. * LCD character sets
  280. */
  281. #define LCD_CHARSET_NORMAL 0
  282. #define LCD_CHARSET_KS0074 1
  283. /*
  284. * LCD types
  285. */
  286. #define LCD_TYPE_NONE 0
  287. #define LCD_TYPE_CUSTOM 1
  288. #define LCD_TYPE_OLD 2
  289. #define LCD_TYPE_KS0074 3
  290. #define LCD_TYPE_HANTRONIX 4
  291. #define LCD_TYPE_NEXCOM 5
  292. /*
  293. * keypad types
  294. */
  295. #define KEYPAD_TYPE_NONE 0
  296. #define KEYPAD_TYPE_OLD 1
  297. #define KEYPAD_TYPE_NEW 2
  298. #define KEYPAD_TYPE_NEXCOM 3
  299. /*
  300. * panel profiles
  301. */
  302. #define PANEL_PROFILE_CUSTOM 0
  303. #define PANEL_PROFILE_OLD 1
  304. #define PANEL_PROFILE_NEW 2
  305. #define PANEL_PROFILE_HANTRONIX 3
  306. #define PANEL_PROFILE_NEXCOM 4
  307. #define PANEL_PROFILE_LARGE 5
  308. /*
  309. * Construct custom config from the kernel's configuration
  310. */
  311. #define DEFAULT_PARPORT 0
  312. #define DEFAULT_PROFILE PANEL_PROFILE_LARGE
  313. #define DEFAULT_KEYPAD_TYPE KEYPAD_TYPE_OLD
  314. #define DEFAULT_LCD_TYPE LCD_TYPE_OLD
  315. #define DEFAULT_LCD_HEIGHT 2
  316. #define DEFAULT_LCD_WIDTH 40
  317. #define DEFAULT_LCD_BWIDTH 40
  318. #define DEFAULT_LCD_HWIDTH 64
  319. #define DEFAULT_LCD_CHARSET LCD_CHARSET_NORMAL
  320. #define DEFAULT_LCD_PROTO LCD_PROTO_PARALLEL
  321. #define DEFAULT_LCD_PIN_E PIN_AUTOLF
  322. #define DEFAULT_LCD_PIN_RS PIN_SELECP
  323. #define DEFAULT_LCD_PIN_RW PIN_INITP
  324. #define DEFAULT_LCD_PIN_SCL PIN_STROBE
  325. #define DEFAULT_LCD_PIN_SDA PIN_D0
  326. #define DEFAULT_LCD_PIN_BL PIN_NOT_SET
  327. #ifdef CONFIG_PANEL_PARPORT
  328. #undef DEFAULT_PARPORT
  329. #define DEFAULT_PARPORT CONFIG_PANEL_PARPORT
  330. #endif
  331. #ifdef CONFIG_PANEL_PROFILE
  332. #undef DEFAULT_PROFILE
  333. #define DEFAULT_PROFILE CONFIG_PANEL_PROFILE
  334. #endif
  335. #if DEFAULT_PROFILE == 0 /* custom */
  336. #ifdef CONFIG_PANEL_KEYPAD
  337. #undef DEFAULT_KEYPAD_TYPE
  338. #define DEFAULT_KEYPAD_TYPE CONFIG_PANEL_KEYPAD
  339. #endif
  340. #ifdef CONFIG_PANEL_LCD
  341. #undef DEFAULT_LCD_TYPE
  342. #define DEFAULT_LCD_TYPE CONFIG_PANEL_LCD
  343. #endif
  344. #ifdef CONFIG_PANEL_LCD_HEIGHT
  345. #undef DEFAULT_LCD_HEIGHT
  346. #define DEFAULT_LCD_HEIGHT CONFIG_PANEL_LCD_HEIGHT
  347. #endif
  348. #ifdef CONFIG_PANEL_LCD_WIDTH
  349. #undef DEFAULT_LCD_WIDTH
  350. #define DEFAULT_LCD_WIDTH CONFIG_PANEL_LCD_WIDTH
  351. #endif
  352. #ifdef CONFIG_PANEL_LCD_BWIDTH
  353. #undef DEFAULT_LCD_BWIDTH
  354. #define DEFAULT_LCD_BWIDTH CONFIG_PANEL_LCD_BWIDTH
  355. #endif
  356. #ifdef CONFIG_PANEL_LCD_HWIDTH
  357. #undef DEFAULT_LCD_HWIDTH
  358. #define DEFAULT_LCD_HWIDTH CONFIG_PANEL_LCD_HWIDTH
  359. #endif
  360. #ifdef CONFIG_PANEL_LCD_CHARSET
  361. #undef DEFAULT_LCD_CHARSET
  362. #define DEFAULT_LCD_CHARSET CONFIG_PANEL_LCD_CHARSET
  363. #endif
  364. #ifdef CONFIG_PANEL_LCD_PROTO
  365. #undef DEFAULT_LCD_PROTO
  366. #define DEFAULT_LCD_PROTO CONFIG_PANEL_LCD_PROTO
  367. #endif
  368. #ifdef CONFIG_PANEL_LCD_PIN_E
  369. #undef DEFAULT_LCD_PIN_E
  370. #define DEFAULT_LCD_PIN_E CONFIG_PANEL_LCD_PIN_E
  371. #endif
  372. #ifdef CONFIG_PANEL_LCD_PIN_RS
  373. #undef DEFAULT_LCD_PIN_RS
  374. #define DEFAULT_LCD_PIN_RS CONFIG_PANEL_LCD_PIN_RS
  375. #endif
  376. #ifdef CONFIG_PANEL_LCD_PIN_RW
  377. #undef DEFAULT_LCD_PIN_RW
  378. #define DEFAULT_LCD_PIN_RW CONFIG_PANEL_LCD_PIN_RW
  379. #endif
  380. #ifdef CONFIG_PANEL_LCD_PIN_SCL
  381. #undef DEFAULT_LCD_PIN_SCL
  382. #define DEFAULT_LCD_PIN_SCL CONFIG_PANEL_LCD_PIN_SCL
  383. #endif
  384. #ifdef CONFIG_PANEL_LCD_PIN_SDA
  385. #undef DEFAULT_LCD_PIN_SDA
  386. #define DEFAULT_LCD_PIN_SDA CONFIG_PANEL_LCD_PIN_SDA
  387. #endif
  388. #ifdef CONFIG_PANEL_LCD_PIN_BL
  389. #undef DEFAULT_LCD_PIN_BL
  390. #define DEFAULT_LCD_PIN_BL CONFIG_PANEL_LCD_PIN_BL
  391. #endif
  392. #endif /* DEFAULT_PROFILE == 0 */
  393. /* global variables */
  394. /* Device single-open policy control */
  395. static atomic_t lcd_available = ATOMIC_INIT(1);
  396. static atomic_t keypad_available = ATOMIC_INIT(1);
  397. static struct pardevice *pprt;
  398. static int keypad_initialized;
  399. static void (*lcd_write_cmd)(int);
  400. static void (*lcd_write_data)(int);
  401. static void (*lcd_clear_fast)(void);
  402. static DEFINE_SPINLOCK(pprt_lock);
  403. static struct timer_list scan_timer;
  404. MODULE_DESCRIPTION("Generic parallel port LCD/Keypad driver");
  405. static int parport = DEFAULT_PARPORT;
  406. module_param(parport, int, 0000);
  407. MODULE_PARM_DESC(parport, "Parallel port index (0=lpt1, 1=lpt2, ...)");
  408. static int profile = DEFAULT_PROFILE;
  409. module_param(profile, int, 0000);
  410. MODULE_PARM_DESC(profile,
  411. "1=16x2 old kp; 2=serial 16x2, new kp; 3=16x2 hantronix; "
  412. "4=16x2 nexcom; default=40x2, old kp");
  413. static int keypad_type = NOT_SET;
  414. module_param(keypad_type, int, 0000);
  415. MODULE_PARM_DESC(keypad_type,
  416. "Keypad type: 0=none, 1=old 6 keys, 2=new 6+1 keys, 3=nexcom 4 keys");
  417. static int lcd_type = NOT_SET;
  418. module_param(lcd_type, int, 0000);
  419. MODULE_PARM_DESC(lcd_type,
  420. "LCD type: 0=none, 1=compiled-in, 2=old, 3=serial ks0074, 4=hantronix, 5=nexcom");
  421. static int lcd_height = NOT_SET;
  422. module_param(lcd_height, int, 0000);
  423. MODULE_PARM_DESC(lcd_height, "Number of lines on the LCD");
  424. static int lcd_width = NOT_SET;
  425. module_param(lcd_width, int, 0000);
  426. MODULE_PARM_DESC(lcd_width, "Number of columns on the LCD");
  427. static int lcd_bwidth = NOT_SET; /* internal buffer width (usually 40) */
  428. module_param(lcd_bwidth, int, 0000);
  429. MODULE_PARM_DESC(lcd_bwidth, "Internal LCD line width (40)");
  430. static int lcd_hwidth = NOT_SET; /* hardware buffer width (usually 64) */
  431. module_param(lcd_hwidth, int, 0000);
  432. MODULE_PARM_DESC(lcd_hwidth, "LCD line hardware address (64)");
  433. static int lcd_charset = NOT_SET;
  434. module_param(lcd_charset, int, 0000);
  435. MODULE_PARM_DESC(lcd_charset, "LCD character set: 0=standard, 1=KS0074");
  436. static int lcd_proto = NOT_SET;
  437. module_param(lcd_proto, int, 0000);
  438. MODULE_PARM_DESC(lcd_proto,
  439. "LCD communication: 0=parallel (//), 1=serial, 2=TI LCD Interface");
  440. /*
  441. * These are the parallel port pins the LCD control signals are connected to.
  442. * Set this to 0 if the signal is not used. Set it to its opposite value
  443. * (negative) if the signal is negated. -MAXINT is used to indicate that the
  444. * pin has not been explicitly specified.
  445. *
  446. * WARNING! no check will be performed about collisions with keypad !
  447. */
  448. static int lcd_e_pin = PIN_NOT_SET;
  449. module_param(lcd_e_pin, int, 0000);
  450. MODULE_PARM_DESC(lcd_e_pin,
  451. "# of the // port pin connected to LCD 'E' signal, with polarity (-17..17)");
  452. static int lcd_rs_pin = PIN_NOT_SET;
  453. module_param(lcd_rs_pin, int, 0000);
  454. MODULE_PARM_DESC(lcd_rs_pin,
  455. "# of the // port pin connected to LCD 'RS' signal, with polarity (-17..17)");
  456. static int lcd_rw_pin = PIN_NOT_SET;
  457. module_param(lcd_rw_pin, int, 0000);
  458. MODULE_PARM_DESC(lcd_rw_pin,
  459. "# of the // port pin connected to LCD 'RW' signal, with polarity (-17..17)");
  460. static int lcd_cl_pin = PIN_NOT_SET;
  461. module_param(lcd_cl_pin, int, 0000);
  462. MODULE_PARM_DESC(lcd_cl_pin,
  463. "# of the // port pin connected to serial LCD 'SCL' signal, with polarity (-17..17)");
  464. static int lcd_da_pin = PIN_NOT_SET;
  465. module_param(lcd_da_pin, int, 0000);
  466. MODULE_PARM_DESC(lcd_da_pin,
  467. "# of the // port pin connected to serial LCD 'SDA' signal, with polarity (-17..17)");
  468. static int lcd_bl_pin = PIN_NOT_SET;
  469. module_param(lcd_bl_pin, int, 0000);
  470. MODULE_PARM_DESC(lcd_bl_pin,
  471. "# of the // port pin connected to LCD backlight, with polarity (-17..17)");
  472. /* Deprecated module parameters - consider not using them anymore */
  473. static int lcd_enabled = NOT_SET;
  474. module_param(lcd_enabled, int, 0000);
  475. MODULE_PARM_DESC(lcd_enabled, "Deprecated option, use lcd_type instead");
  476. static int keypad_enabled = NOT_SET;
  477. module_param(keypad_enabled, int, 0000);
  478. MODULE_PARM_DESC(keypad_enabled, "Deprecated option, use keypad_type instead");
  479. static const unsigned char *lcd_char_conv;
  480. /* for some LCD drivers (ks0074) we need a charset conversion table. */
  481. static const unsigned char lcd_char_conv_ks0074[256] = {
  482. /* 0|8 1|9 2|A 3|B 4|C 5|D 6|E 7|F */
  483. /* 0x00 */ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  484. /* 0x08 */ 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  485. /* 0x10 */ 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  486. /* 0x18 */ 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  487. /* 0x20 */ 0x20, 0x21, 0x22, 0x23, 0xa2, 0x25, 0x26, 0x27,
  488. /* 0x28 */ 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  489. /* 0x30 */ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  490. /* 0x38 */ 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
  491. /* 0x40 */ 0xa0, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
  492. /* 0x48 */ 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
  493. /* 0x50 */ 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  494. /* 0x58 */ 0x58, 0x59, 0x5a, 0xfa, 0xfb, 0xfc, 0x1d, 0xc4,
  495. /* 0x60 */ 0x96, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
  496. /* 0x68 */ 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
  497. /* 0x70 */ 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
  498. /* 0x78 */ 0x78, 0x79, 0x7a, 0xfd, 0xfe, 0xff, 0xce, 0x20,
  499. /* 0x80 */ 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  500. /* 0x88 */ 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  501. /* 0x90 */ 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
  502. /* 0x98 */ 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
  503. /* 0xA0 */ 0x20, 0x40, 0xb1, 0xa1, 0x24, 0xa3, 0xfe, 0x5f,
  504. /* 0xA8 */ 0x22, 0xc8, 0x61, 0x14, 0x97, 0x2d, 0xad, 0x96,
  505. /* 0xB0 */ 0x80, 0x8c, 0x82, 0x83, 0x27, 0x8f, 0x86, 0xdd,
  506. /* 0xB8 */ 0x2c, 0x81, 0x6f, 0x15, 0x8b, 0x8a, 0x84, 0x60,
  507. /* 0xC0 */ 0xe2, 0xe2, 0xe2, 0x5b, 0x5b, 0xae, 0xbc, 0xa9,
  508. /* 0xC8 */ 0xc5, 0xbf, 0xc6, 0xf1, 0xe3, 0xe3, 0xe3, 0xe3,
  509. /* 0xD0 */ 0x44, 0x5d, 0xa8, 0xe4, 0xec, 0xec, 0x5c, 0x78,
  510. /* 0xD8 */ 0xab, 0xa6, 0xe5, 0x5e, 0x5e, 0xe6, 0xaa, 0xbe,
  511. /* 0xE0 */ 0x7f, 0xe7, 0xaf, 0x7b, 0x7b, 0xaf, 0xbd, 0xc8,
  512. /* 0xE8 */ 0xa4, 0xa5, 0xc7, 0xf6, 0xa7, 0xe8, 0x69, 0x69,
  513. /* 0xF0 */ 0xed, 0x7d, 0xa8, 0xe4, 0xec, 0x5c, 0x5c, 0x25,
  514. /* 0xF8 */ 0xac, 0xa6, 0xea, 0xef, 0x7e, 0xeb, 0xb2, 0x79,
  515. };
  516. static const char old_keypad_profile[][4][9] = {
  517. {"S0", "Left\n", "Left\n", ""},
  518. {"S1", "Down\n", "Down\n", ""},
  519. {"S2", "Up\n", "Up\n", ""},
  520. {"S3", "Right\n", "Right\n", ""},
  521. {"S4", "Esc\n", "Esc\n", ""},
  522. {"S5", "Ret\n", "Ret\n", ""},
  523. {"", "", "", ""}
  524. };
  525. /* signals, press, repeat, release */
  526. static const char new_keypad_profile[][4][9] = {
  527. {"S0", "Left\n", "Left\n", ""},
  528. {"S1", "Down\n", "Down\n", ""},
  529. {"S2", "Up\n", "Up\n", ""},
  530. {"S3", "Right\n", "Right\n", ""},
  531. {"S4s5", "", "Esc\n", "Esc\n"},
  532. {"s4S5", "", "Ret\n", "Ret\n"},
  533. {"S4S5", "Help\n", "", ""},
  534. /* add new signals above this line */
  535. {"", "", "", ""}
  536. };
  537. /* signals, press, repeat, release */
  538. static const char nexcom_keypad_profile[][4][9] = {
  539. {"a-p-e-", "Down\n", "Down\n", ""},
  540. {"a-p-E-", "Ret\n", "Ret\n", ""},
  541. {"a-P-E-", "Esc\n", "Esc\n", ""},
  542. {"a-P-e-", "Up\n", "Up\n", ""},
  543. /* add new signals above this line */
  544. {"", "", "", ""}
  545. };
  546. static const char (*keypad_profile)[4][9] = old_keypad_profile;
  547. static DECLARE_BITMAP(bits, LCD_BITS);
  548. static void lcd_get_bits(unsigned int port, int *val)
  549. {
  550. unsigned int bit, state;
  551. for (bit = 0; bit < LCD_BITS; bit++) {
  552. state = test_bit(bit, bits) ? BIT_SET : BIT_CLR;
  553. *val &= lcd_bits[port][bit][BIT_MSK];
  554. *val |= lcd_bits[port][bit][state];
  555. }
  556. }
  557. static void init_scan_timer(void);
  558. /* sets data port bits according to current signals values */
  559. static int set_data_bits(void)
  560. {
  561. int val;
  562. val = r_dtr(pprt);
  563. lcd_get_bits(LCD_PORT_D, &val);
  564. w_dtr(pprt, val);
  565. return val;
  566. }
  567. /* sets ctrl port bits according to current signals values */
  568. static int set_ctrl_bits(void)
  569. {
  570. int val;
  571. val = r_ctr(pprt);
  572. lcd_get_bits(LCD_PORT_C, &val);
  573. w_ctr(pprt, val);
  574. return val;
  575. }
  576. /* sets ctrl & data port bits according to current signals values */
  577. static void panel_set_bits(void)
  578. {
  579. set_data_bits();
  580. set_ctrl_bits();
  581. }
  582. /*
  583. * Converts a parallel port pin (from -25 to 25) to data and control ports
  584. * masks, and data and control port bits. The signal will be considered
  585. * unconnected if it's on pin 0 or an invalid pin (<-25 or >25).
  586. *
  587. * Result will be used this way :
  588. * out(dport, in(dport) & d_val[2] | d_val[signal_state])
  589. * out(cport, in(cport) & c_val[2] | c_val[signal_state])
  590. */
  591. static void pin_to_bits(int pin, unsigned char *d_val, unsigned char *c_val)
  592. {
  593. int d_bit, c_bit, inv;
  594. d_val[0] = 0;
  595. c_val[0] = 0;
  596. d_val[1] = 0;
  597. c_val[1] = 0;
  598. d_val[2] = 0xFF;
  599. c_val[2] = 0xFF;
  600. if (pin == 0)
  601. return;
  602. inv = (pin < 0);
  603. if (inv)
  604. pin = -pin;
  605. d_bit = 0;
  606. c_bit = 0;
  607. switch (pin) {
  608. case PIN_STROBE: /* strobe, inverted */
  609. c_bit = PNL_PSTROBE;
  610. inv = !inv;
  611. break;
  612. case PIN_D0...PIN_D7: /* D0 - D7 = 2 - 9 */
  613. d_bit = 1 << (pin - 2);
  614. break;
  615. case PIN_AUTOLF: /* autofeed, inverted */
  616. c_bit = PNL_PAUTOLF;
  617. inv = !inv;
  618. break;
  619. case PIN_INITP: /* init, direct */
  620. c_bit = PNL_PINITP;
  621. break;
  622. case PIN_SELECP: /* select_in, inverted */
  623. c_bit = PNL_PSELECP;
  624. inv = !inv;
  625. break;
  626. default: /* unknown pin, ignore */
  627. break;
  628. }
  629. if (c_bit) {
  630. c_val[2] &= ~c_bit;
  631. c_val[!inv] = c_bit;
  632. } else if (d_bit) {
  633. d_val[2] &= ~d_bit;
  634. d_val[!inv] = d_bit;
  635. }
  636. }
  637. /* sleeps that many milliseconds with a reschedule */
  638. static void long_sleep(int ms)
  639. {
  640. if (in_interrupt())
  641. mdelay(ms);
  642. else
  643. schedule_timeout_interruptible(msecs_to_jiffies(ms));
  644. }
  645. /*
  646. * send a serial byte to the LCD panel. The caller is responsible for locking
  647. * if needed.
  648. */
  649. static void lcd_send_serial(int byte)
  650. {
  651. int bit;
  652. /*
  653. * the data bit is set on D0, and the clock on STROBE.
  654. * LCD reads D0 on STROBE's rising edge.
  655. */
  656. for (bit = 0; bit < 8; bit++) {
  657. clear_bit(LCD_BIT_CL, bits); /* CLK low */
  658. panel_set_bits();
  659. if (byte & 1) {
  660. set_bit(LCD_BIT_DA, bits);
  661. } else {
  662. clear_bit(LCD_BIT_DA, bits);
  663. }
  664. panel_set_bits();
  665. udelay(2); /* maintain the data during 2 us before CLK up */
  666. set_bit(LCD_BIT_CL, bits); /* CLK high */
  667. panel_set_bits();
  668. udelay(1); /* maintain the strobe during 1 us */
  669. byte >>= 1;
  670. }
  671. }
  672. /* turn the backlight on or off */
  673. static void lcd_backlight(int on)
  674. {
  675. if (lcd.pins.bl == PIN_NONE)
  676. return;
  677. /* The backlight is activated by setting the AUTOFEED line to +5V */
  678. spin_lock_irq(&pprt_lock);
  679. if (on)
  680. set_bit(LCD_BIT_BL, bits);
  681. else
  682. clear_bit(LCD_BIT_BL, bits);
  683. panel_set_bits();
  684. spin_unlock_irq(&pprt_lock);
  685. }
  686. /* send a command to the LCD panel in serial mode */
  687. static void lcd_write_cmd_s(int cmd)
  688. {
  689. spin_lock_irq(&pprt_lock);
  690. lcd_send_serial(0x1F); /* R/W=W, RS=0 */
  691. lcd_send_serial(cmd & 0x0F);
  692. lcd_send_serial((cmd >> 4) & 0x0F);
  693. udelay(40); /* the shortest command takes at least 40 us */
  694. spin_unlock_irq(&pprt_lock);
  695. }
  696. /* send data to the LCD panel in serial mode */
  697. static void lcd_write_data_s(int data)
  698. {
  699. spin_lock_irq(&pprt_lock);
  700. lcd_send_serial(0x5F); /* R/W=W, RS=1 */
  701. lcd_send_serial(data & 0x0F);
  702. lcd_send_serial((data >> 4) & 0x0F);
  703. udelay(40); /* the shortest data takes at least 40 us */
  704. spin_unlock_irq(&pprt_lock);
  705. }
  706. /* send a command to the LCD panel in 8 bits parallel mode */
  707. static void lcd_write_cmd_p8(int cmd)
  708. {
  709. spin_lock_irq(&pprt_lock);
  710. /* present the data to the data port */
  711. w_dtr(pprt, cmd);
  712. udelay(20); /* maintain the data during 20 us before the strobe */
  713. set_bit(LCD_BIT_E, bits);
  714. clear_bit(LCD_BIT_RS, bits);
  715. clear_bit(LCD_BIT_RW, bits);
  716. set_ctrl_bits();
  717. udelay(40); /* maintain the strobe during 40 us */
  718. clear_bit(LCD_BIT_E, bits);
  719. set_ctrl_bits();
  720. udelay(120); /* the shortest command takes at least 120 us */
  721. spin_unlock_irq(&pprt_lock);
  722. }
  723. /* send data to the LCD panel in 8 bits parallel mode */
  724. static void lcd_write_data_p8(int data)
  725. {
  726. spin_lock_irq(&pprt_lock);
  727. /* present the data to the data port */
  728. w_dtr(pprt, data);
  729. udelay(20); /* maintain the data during 20 us before the strobe */
  730. set_bit(LCD_BIT_E, bits);
  731. set_bit(LCD_BIT_RS, bits);
  732. clear_bit(LCD_BIT_RW, bits);
  733. set_ctrl_bits();
  734. udelay(40); /* maintain the strobe during 40 us */
  735. clear_bit(LCD_BIT_E, bits);
  736. set_ctrl_bits();
  737. udelay(45); /* the shortest data takes at least 45 us */
  738. spin_unlock_irq(&pprt_lock);
  739. }
  740. /* send a command to the TI LCD panel */
  741. static void lcd_write_cmd_tilcd(int cmd)
  742. {
  743. spin_lock_irq(&pprt_lock);
  744. /* present the data to the control port */
  745. w_ctr(pprt, cmd);
  746. udelay(60);
  747. spin_unlock_irq(&pprt_lock);
  748. }
  749. /* send data to the TI LCD panel */
  750. static void lcd_write_data_tilcd(int data)
  751. {
  752. spin_lock_irq(&pprt_lock);
  753. /* present the data to the data port */
  754. w_dtr(pprt, data);
  755. udelay(60);
  756. spin_unlock_irq(&pprt_lock);
  757. }
  758. static void lcd_gotoxy(void)
  759. {
  760. lcd_write_cmd(LCD_CMD_SET_DDRAM_ADDR
  761. | (lcd.addr.y ? lcd.hwidth : 0)
  762. /*
  763. * we force the cursor to stay at the end of the
  764. * line if it wants to go farther
  765. */
  766. | ((lcd.addr.x < lcd.bwidth) ? lcd.addr.x &
  767. (lcd.hwidth - 1) : lcd.bwidth - 1));
  768. }
  769. static void lcd_print(char c)
  770. {
  771. if (lcd.addr.x < lcd.bwidth) {
  772. if (lcd_char_conv)
  773. c = lcd_char_conv[(unsigned char)c];
  774. lcd_write_data(c);
  775. lcd.addr.x++;
  776. }
  777. /* prevents the cursor from wrapping onto the next line */
  778. if (lcd.addr.x == lcd.bwidth)
  779. lcd_gotoxy();
  780. }
  781. /* fills the display with spaces and resets X/Y */
  782. static void lcd_clear_fast_s(void)
  783. {
  784. int pos;
  785. lcd.addr.x = 0;
  786. lcd.addr.y = 0;
  787. lcd_gotoxy();
  788. spin_lock_irq(&pprt_lock);
  789. for (pos = 0; pos < lcd.height * lcd.hwidth; pos++) {
  790. lcd_send_serial(0x5F); /* R/W=W, RS=1 */
  791. lcd_send_serial(' ' & 0x0F);
  792. lcd_send_serial((' ' >> 4) & 0x0F);
  793. /* the shortest data takes at least 40 us */
  794. udelay(40);
  795. }
  796. spin_unlock_irq(&pprt_lock);
  797. lcd.addr.x = 0;
  798. lcd.addr.y = 0;
  799. lcd_gotoxy();
  800. }
  801. /* fills the display with spaces and resets X/Y */
  802. static void lcd_clear_fast_p8(void)
  803. {
  804. int pos;
  805. lcd.addr.x = 0;
  806. lcd.addr.y = 0;
  807. lcd_gotoxy();
  808. spin_lock_irq(&pprt_lock);
  809. for (pos = 0; pos < lcd.height * lcd.hwidth; pos++) {
  810. /* present the data to the data port */
  811. w_dtr(pprt, ' ');
  812. /* maintain the data during 20 us before the strobe */
  813. udelay(20);
  814. set_bit(LCD_BIT_E, bits);
  815. set_bit(LCD_BIT_RS, bits);
  816. clear_bit(LCD_BIT_RW, bits);
  817. set_ctrl_bits();
  818. /* maintain the strobe during 40 us */
  819. udelay(40);
  820. clear_bit(LCD_BIT_E, bits);
  821. set_ctrl_bits();
  822. /* the shortest data takes at least 45 us */
  823. udelay(45);
  824. }
  825. spin_unlock_irq(&pprt_lock);
  826. lcd.addr.x = 0;
  827. lcd.addr.y = 0;
  828. lcd_gotoxy();
  829. }
  830. /* fills the display with spaces and resets X/Y */
  831. static void lcd_clear_fast_tilcd(void)
  832. {
  833. int pos;
  834. lcd.addr.x = 0;
  835. lcd.addr.y = 0;
  836. lcd_gotoxy();
  837. spin_lock_irq(&pprt_lock);
  838. for (pos = 0; pos < lcd.height * lcd.hwidth; pos++) {
  839. /* present the data to the data port */
  840. w_dtr(pprt, ' ');
  841. udelay(60);
  842. }
  843. spin_unlock_irq(&pprt_lock);
  844. lcd.addr.x = 0;
  845. lcd.addr.y = 0;
  846. lcd_gotoxy();
  847. }
  848. /* clears the display and resets X/Y */
  849. static void lcd_clear_display(void)
  850. {
  851. lcd_write_cmd(LCD_CMD_DISPLAY_CLEAR);
  852. lcd.addr.x = 0;
  853. lcd.addr.y = 0;
  854. /* we must wait a few milliseconds (15) */
  855. long_sleep(15);
  856. }
  857. static void lcd_init_display(void)
  858. {
  859. lcd.flags = ((lcd.height > 1) ? LCD_FLAG_N : 0)
  860. | LCD_FLAG_D | LCD_FLAG_C | LCD_FLAG_B;
  861. long_sleep(20); /* wait 20 ms after power-up for the paranoid */
  862. /* 8bits, 1 line, small fonts; let's do it 3 times */
  863. lcd_write_cmd(LCD_CMD_FUNCTION_SET | LCD_CMD_DATA_LEN_8BITS);
  864. long_sleep(10);
  865. lcd_write_cmd(LCD_CMD_FUNCTION_SET | LCD_CMD_DATA_LEN_8BITS);
  866. long_sleep(10);
  867. lcd_write_cmd(LCD_CMD_FUNCTION_SET | LCD_CMD_DATA_LEN_8BITS);
  868. long_sleep(10);
  869. /* set font height and lines number */
  870. lcd_write_cmd(LCD_CMD_FUNCTION_SET | LCD_CMD_DATA_LEN_8BITS
  871. | ((lcd.flags & LCD_FLAG_F) ? LCD_CMD_FONT_5X10_DOTS : 0)
  872. | ((lcd.flags & LCD_FLAG_N) ? LCD_CMD_TWO_LINES : 0)
  873. );
  874. long_sleep(10);
  875. /* display off, cursor off, blink off */
  876. lcd_write_cmd(LCD_CMD_DISPLAY_CTRL);
  877. long_sleep(10);
  878. lcd_write_cmd(LCD_CMD_DISPLAY_CTRL /* set display mode */
  879. | ((lcd.flags & LCD_FLAG_D) ? LCD_CMD_DISPLAY_ON : 0)
  880. | ((lcd.flags & LCD_FLAG_C) ? LCD_CMD_CURSOR_ON : 0)
  881. | ((lcd.flags & LCD_FLAG_B) ? LCD_CMD_BLINK_ON : 0)
  882. );
  883. lcd_backlight((lcd.flags & LCD_FLAG_L) ? 1 : 0);
  884. long_sleep(10);
  885. /* entry mode set : increment, cursor shifting */
  886. lcd_write_cmd(LCD_CMD_ENTRY_MODE | LCD_CMD_CURSOR_INC);
  887. lcd_clear_display();
  888. }
  889. /*
  890. * These are the file operation function for user access to /dev/lcd
  891. * This function can also be called from inside the kernel, by
  892. * setting file and ppos to NULL.
  893. *
  894. */
  895. static inline int handle_lcd_special_code(void)
  896. {
  897. /* LCD special codes */
  898. int processed = 0;
  899. char *esc = lcd.esc_seq.buf + 2;
  900. int oldflags = lcd.flags;
  901. /* check for display mode flags */
  902. switch (*esc) {
  903. case 'D': /* Display ON */
  904. lcd.flags |= LCD_FLAG_D;
  905. processed = 1;
  906. break;
  907. case 'd': /* Display OFF */
  908. lcd.flags &= ~LCD_FLAG_D;
  909. processed = 1;
  910. break;
  911. case 'C': /* Cursor ON */
  912. lcd.flags |= LCD_FLAG_C;
  913. processed = 1;
  914. break;
  915. case 'c': /* Cursor OFF */
  916. lcd.flags &= ~LCD_FLAG_C;
  917. processed = 1;
  918. break;
  919. case 'B': /* Blink ON */
  920. lcd.flags |= LCD_FLAG_B;
  921. processed = 1;
  922. break;
  923. case 'b': /* Blink OFF */
  924. lcd.flags &= ~LCD_FLAG_B;
  925. processed = 1;
  926. break;
  927. case '+': /* Back light ON */
  928. lcd.flags |= LCD_FLAG_L;
  929. processed = 1;
  930. break;
  931. case '-': /* Back light OFF */
  932. lcd.flags &= ~LCD_FLAG_L;
  933. processed = 1;
  934. break;
  935. case '*':
  936. /* flash back light using the keypad timer */
  937. if (scan_timer.function) {
  938. if (lcd.light_tempo == 0 &&
  939. ((lcd.flags & LCD_FLAG_L) == 0))
  940. lcd_backlight(1);
  941. lcd.light_tempo = FLASH_LIGHT_TEMPO;
  942. }
  943. processed = 1;
  944. break;
  945. case 'f': /* Small Font */
  946. lcd.flags &= ~LCD_FLAG_F;
  947. processed = 1;
  948. break;
  949. case 'F': /* Large Font */
  950. lcd.flags |= LCD_FLAG_F;
  951. processed = 1;
  952. break;
  953. case 'n': /* One Line */
  954. lcd.flags &= ~LCD_FLAG_N;
  955. processed = 1;
  956. break;
  957. case 'N': /* Two Lines */
  958. lcd.flags |= LCD_FLAG_N;
  959. break;
  960. case 'l': /* Shift Cursor Left */
  961. if (lcd.addr.x > 0) {
  962. /* back one char if not at end of line */
  963. if (lcd.addr.x < lcd.bwidth)
  964. lcd_write_cmd(LCD_CMD_SHIFT);
  965. lcd.addr.x--;
  966. }
  967. processed = 1;
  968. break;
  969. case 'r': /* shift cursor right */
  970. if (lcd.addr.x < lcd.width) {
  971. /* allow the cursor to pass the end of the line */
  972. if (lcd.addr.x < (lcd.bwidth - 1))
  973. lcd_write_cmd(LCD_CMD_SHIFT |
  974. LCD_CMD_SHIFT_RIGHT);
  975. lcd.addr.x++;
  976. }
  977. processed = 1;
  978. break;
  979. case 'L': /* shift display left */
  980. lcd_write_cmd(LCD_CMD_SHIFT | LCD_CMD_DISPLAY_SHIFT);
  981. processed = 1;
  982. break;
  983. case 'R': /* shift display right */
  984. lcd_write_cmd(LCD_CMD_SHIFT | LCD_CMD_DISPLAY_SHIFT |
  985. LCD_CMD_SHIFT_RIGHT);
  986. processed = 1;
  987. break;
  988. case 'k': { /* kill end of line */
  989. int x;
  990. for (x = lcd.addr.x; x < lcd.bwidth; x++)
  991. lcd_write_data(' ');
  992. /* restore cursor position */
  993. lcd_gotoxy();
  994. processed = 1;
  995. break;
  996. }
  997. case 'I': /* reinitialize display */
  998. lcd_init_display();
  999. processed = 1;
  1000. break;
  1001. case 'G': {
  1002. /* Generator : LGcxxxxx...xx; must have <c> between '0'
  1003. * and '7', representing the numerical ASCII code of the
  1004. * redefined character, and <xx...xx> a sequence of 16
  1005. * hex digits representing 8 bytes for each character.
  1006. * Most LCDs will only use 5 lower bits of the 7 first
  1007. * bytes.
  1008. */
  1009. unsigned char cgbytes[8];
  1010. unsigned char cgaddr;
  1011. int cgoffset;
  1012. int shift;
  1013. char value;
  1014. int addr;
  1015. if (!strchr(esc, ';'))
  1016. break;
  1017. esc++;
  1018. cgaddr = *(esc++) - '0';
  1019. if (cgaddr > 7) {
  1020. processed = 1;
  1021. break;
  1022. }
  1023. cgoffset = 0;
  1024. shift = 0;
  1025. value = 0;
  1026. while (*esc && cgoffset < 8) {
  1027. shift ^= 4;
  1028. if (*esc >= '0' && *esc <= '9') {
  1029. value |= (*esc - '0') << shift;
  1030. } else if (*esc >= 'A' && *esc <= 'Z') {
  1031. value |= (*esc - 'A' + 10) << shift;
  1032. } else if (*esc >= 'a' && *esc <= 'z') {
  1033. value |= (*esc - 'a' + 10) << shift;
  1034. } else {
  1035. esc++;
  1036. continue;
  1037. }
  1038. if (shift == 0) {
  1039. cgbytes[cgoffset++] = value;
  1040. value = 0;
  1041. }
  1042. esc++;
  1043. }
  1044. lcd_write_cmd(LCD_CMD_SET_CGRAM_ADDR | (cgaddr * 8));
  1045. for (addr = 0; addr < cgoffset; addr++)
  1046. lcd_write_data(cgbytes[addr]);
  1047. /* ensures that we stop writing to CGRAM */
  1048. lcd_gotoxy();
  1049. processed = 1;
  1050. break;
  1051. }
  1052. case 'x': /* gotoxy : LxXXX[yYYY]; */
  1053. case 'y': /* gotoxy : LyYYY[xXXX]; */
  1054. if (!strchr(esc, ';'))
  1055. break;
  1056. while (*esc) {
  1057. if (*esc == 'x') {
  1058. esc++;
  1059. if (kstrtoul(esc, 10, &lcd.addr.x) < 0)
  1060. break;
  1061. } else if (*esc == 'y') {
  1062. esc++;
  1063. if (kstrtoul(esc, 10, &lcd.addr.y) < 0)
  1064. break;
  1065. } else {
  1066. break;
  1067. }
  1068. }
  1069. lcd_gotoxy();
  1070. processed = 1;
  1071. break;
  1072. }
  1073. /* TODO: This indent party here got ugly, clean it! */
  1074. /* Check whether one flag was changed */
  1075. if (oldflags != lcd.flags) {
  1076. /* check whether one of B,C,D flags were changed */
  1077. if ((oldflags ^ lcd.flags) &
  1078. (LCD_FLAG_B | LCD_FLAG_C | LCD_FLAG_D))
  1079. /* set display mode */
  1080. lcd_write_cmd(LCD_CMD_DISPLAY_CTRL
  1081. | ((lcd.flags & LCD_FLAG_D)
  1082. ? LCD_CMD_DISPLAY_ON : 0)
  1083. | ((lcd.flags & LCD_FLAG_C)
  1084. ? LCD_CMD_CURSOR_ON : 0)
  1085. | ((lcd.flags & LCD_FLAG_B)
  1086. ? LCD_CMD_BLINK_ON : 0));
  1087. /* check whether one of F,N flags was changed */
  1088. else if ((oldflags ^ lcd.flags) & (LCD_FLAG_F | LCD_FLAG_N))
  1089. lcd_write_cmd(LCD_CMD_FUNCTION_SET
  1090. | LCD_CMD_DATA_LEN_8BITS
  1091. | ((lcd.flags & LCD_FLAG_F)
  1092. ? LCD_CMD_TWO_LINES : 0)
  1093. | ((lcd.flags & LCD_FLAG_N)
  1094. ? LCD_CMD_FONT_5X10_DOTS
  1095. : 0));
  1096. /* check whether L flag was changed */
  1097. else if ((oldflags ^ lcd.flags) & (LCD_FLAG_L)) {
  1098. if (lcd.flags & (LCD_FLAG_L))
  1099. lcd_backlight(1);
  1100. else if (lcd.light_tempo == 0)
  1101. /*
  1102. * switch off the light only when the tempo
  1103. * lighting is gone
  1104. */
  1105. lcd_backlight(0);
  1106. }
  1107. }
  1108. return processed;
  1109. }
  1110. static void lcd_write_char(char c)
  1111. {
  1112. /* first, we'll test if we're in escape mode */
  1113. if ((c != '\n') && lcd.esc_seq.len >= 0) {
  1114. /* yes, let's add this char to the buffer */
  1115. lcd.esc_seq.buf[lcd.esc_seq.len++] = c;
  1116. lcd.esc_seq.buf[lcd.esc_seq.len] = 0;
  1117. } else {
  1118. /* aborts any previous escape sequence */
  1119. lcd.esc_seq.len = -1;
  1120. switch (c) {
  1121. case LCD_ESCAPE_CHAR:
  1122. /* start of an escape sequence */
  1123. lcd.esc_seq.len = 0;
  1124. lcd.esc_seq.buf[lcd.esc_seq.len] = 0;
  1125. break;
  1126. case '\b':
  1127. /* go back one char and clear it */
  1128. if (lcd.addr.x > 0) {
  1129. /*
  1130. * check if we're not at the
  1131. * end of the line
  1132. */
  1133. if (lcd.addr.x < lcd.bwidth)
  1134. /* back one char */
  1135. lcd_write_cmd(LCD_CMD_SHIFT);
  1136. lcd.addr.x--;
  1137. }
  1138. /* replace with a space */
  1139. lcd_write_data(' ');
  1140. /* back one char again */
  1141. lcd_write_cmd(LCD_CMD_SHIFT);
  1142. break;
  1143. case '\014':
  1144. /* quickly clear the display */
  1145. lcd_clear_fast();
  1146. break;
  1147. case '\n':
  1148. /*
  1149. * flush the remainder of the current line and
  1150. * go to the beginning of the next line
  1151. */
  1152. for (; lcd.addr.x < lcd.bwidth; lcd.addr.x++)
  1153. lcd_write_data(' ');
  1154. lcd.addr.x = 0;
  1155. lcd.addr.y = (lcd.addr.y + 1) % lcd.height;
  1156. lcd_gotoxy();
  1157. break;
  1158. case '\r':
  1159. /* go to the beginning of the same line */
  1160. lcd.addr.x = 0;
  1161. lcd_gotoxy();
  1162. break;
  1163. case '\t':
  1164. /* print a space instead of the tab */
  1165. lcd_print(' ');
  1166. break;
  1167. default:
  1168. /* simply print this char */
  1169. lcd_print(c);
  1170. break;
  1171. }
  1172. }
  1173. /*
  1174. * now we'll see if we're in an escape mode and if the current
  1175. * escape sequence can be understood.
  1176. */
  1177. if (lcd.esc_seq.len >= 2) {
  1178. int processed = 0;
  1179. if (!strcmp(lcd.esc_seq.buf, "[2J")) {
  1180. /* clear the display */
  1181. lcd_clear_fast();
  1182. processed = 1;
  1183. } else if (!strcmp(lcd.esc_seq.buf, "[H")) {
  1184. /* cursor to home */
  1185. lcd.addr.x = 0;
  1186. lcd.addr.y = 0;
  1187. lcd_gotoxy();
  1188. processed = 1;
  1189. }
  1190. /* codes starting with ^[[L */
  1191. else if ((lcd.esc_seq.len >= 3) &&
  1192. (lcd.esc_seq.buf[0] == '[') &&
  1193. (lcd.esc_seq.buf[1] == 'L')) {
  1194. processed = handle_lcd_special_code();
  1195. }
  1196. /* LCD special escape codes */
  1197. /*
  1198. * flush the escape sequence if it's been processed
  1199. * or if it is getting too long.
  1200. */
  1201. if (processed || (lcd.esc_seq.len >= LCD_ESCAPE_LEN))
  1202. lcd.esc_seq.len = -1;
  1203. } /* escape codes */
  1204. }
  1205. static ssize_t lcd_write(struct file *file,
  1206. const char __user *buf, size_t count, loff_t *ppos)
  1207. {
  1208. const char __user *tmp = buf;
  1209. char c;
  1210. for (; count-- > 0; (*ppos)++, tmp++) {
  1211. if (!in_interrupt() && (((count + 1) & 0x1f) == 0))
  1212. /*
  1213. * let's be a little nice with other processes
  1214. * that need some CPU
  1215. */
  1216. schedule();
  1217. if (get_user(c, tmp))
  1218. return -EFAULT;
  1219. lcd_write_char(c);
  1220. }
  1221. return tmp - buf;
  1222. }
  1223. static int lcd_open(struct inode *inode, struct file *file)
  1224. {
  1225. int ret;
  1226. ret = -EBUSY;
  1227. if (!atomic_dec_and_test(&lcd_available))
  1228. goto fail; /* open only once at a time */
  1229. ret = -EPERM;
  1230. if (file->f_mode & FMODE_READ) /* device is write-only */
  1231. goto fail;
  1232. if (lcd.must_clear) {
  1233. lcd_clear_display();
  1234. lcd.must_clear = false;
  1235. }
  1236. return nonseekable_open(inode, file);
  1237. fail:
  1238. atomic_inc(&lcd_available);
  1239. return ret;
  1240. }
  1241. static int lcd_release(struct inode *inode, struct file *file)
  1242. {
  1243. atomic_inc(&lcd_available);
  1244. return 0;
  1245. }
  1246. static const struct file_operations lcd_fops = {
  1247. .write = lcd_write,
  1248. .open = lcd_open,
  1249. .release = lcd_release,
  1250. .llseek = no_llseek,
  1251. };
  1252. static struct miscdevice lcd_dev = {
  1253. .minor = LCD_MINOR,
  1254. .name = "lcd",
  1255. .fops = &lcd_fops,
  1256. };
  1257. /* public function usable from the kernel for any purpose */
  1258. static void panel_lcd_print(const char *s)
  1259. {
  1260. const char *tmp = s;
  1261. int count = strlen(s);
  1262. if (lcd.enabled && lcd.initialized) {
  1263. for (; count-- > 0; tmp++) {
  1264. if (!in_interrupt() && (((count + 1) & 0x1f) == 0))
  1265. /*
  1266. * let's be a little nice with other processes
  1267. * that need some CPU
  1268. */
  1269. schedule();
  1270. lcd_write_char(*tmp);
  1271. }
  1272. }
  1273. }
  1274. /* initialize the LCD driver */
  1275. static void lcd_init(void)
  1276. {
  1277. switch (selected_lcd_type) {
  1278. case LCD_TYPE_OLD:
  1279. /* parallel mode, 8 bits */
  1280. lcd.proto = LCD_PROTO_PARALLEL;
  1281. lcd.charset = LCD_CHARSET_NORMAL;
  1282. lcd.pins.e = PIN_STROBE;
  1283. lcd.pins.rs = PIN_AUTOLF;
  1284. lcd.width = 40;
  1285. lcd.bwidth = 40;
  1286. lcd.hwidth = 64;
  1287. lcd.height = 2;
  1288. break;
  1289. case LCD_TYPE_KS0074:
  1290. /* serial mode, ks0074 */
  1291. lcd.proto = LCD_PROTO_SERIAL;
  1292. lcd.charset = LCD_CHARSET_KS0074;
  1293. lcd.pins.bl = PIN_AUTOLF;
  1294. lcd.pins.cl = PIN_STROBE;
  1295. lcd.pins.da = PIN_D0;
  1296. lcd.width = 16;
  1297. lcd.bwidth = 40;
  1298. lcd.hwidth = 16;
  1299. lcd.height = 2;
  1300. break;
  1301. case LCD_TYPE_NEXCOM:
  1302. /* parallel mode, 8 bits, generic */
  1303. lcd.proto = LCD_PROTO_PARALLEL;
  1304. lcd.charset = LCD_CHARSET_NORMAL;
  1305. lcd.pins.e = PIN_AUTOLF;
  1306. lcd.pins.rs = PIN_SELECP;
  1307. lcd.pins.rw = PIN_INITP;
  1308. lcd.width = 16;
  1309. lcd.bwidth = 40;
  1310. lcd.hwidth = 64;
  1311. lcd.height = 2;
  1312. break;
  1313. case LCD_TYPE_CUSTOM:
  1314. /* customer-defined */
  1315. lcd.proto = DEFAULT_LCD_PROTO;
  1316. lcd.charset = DEFAULT_LCD_CHARSET;
  1317. /* default geometry will be set later */
  1318. break;
  1319. case LCD_TYPE_HANTRONIX:
  1320. /* parallel mode, 8 bits, hantronix-like */
  1321. default:
  1322. lcd.proto = LCD_PROTO_PARALLEL;
  1323. lcd.charset = LCD_CHARSET_NORMAL;
  1324. lcd.pins.e = PIN_STROBE;
  1325. lcd.pins.rs = PIN_SELECP;
  1326. lcd.width = 16;
  1327. lcd.bwidth = 40;
  1328. lcd.hwidth = 64;
  1329. lcd.height = 2;
  1330. break;
  1331. }
  1332. /* Overwrite with module params set on loading */
  1333. if (lcd_height != NOT_SET)
  1334. lcd.height = lcd_height;
  1335. if (lcd_width != NOT_SET)
  1336. lcd.width = lcd_width;
  1337. if (lcd_bwidth != NOT_SET)
  1338. lcd.bwidth = lcd_bwidth;
  1339. if (lcd_hwidth != NOT_SET)
  1340. lcd.hwidth = lcd_hwidth;
  1341. if (lcd_charset != NOT_SET)
  1342. lcd.charset = lcd_charset;
  1343. if (lcd_proto != NOT_SET)
  1344. lcd.proto = lcd_proto;
  1345. if (lcd_e_pin != PIN_NOT_SET)
  1346. lcd.pins.e = lcd_e_pin;
  1347. if (lcd_rs_pin != PIN_NOT_SET)
  1348. lcd.pins.rs = lcd_rs_pin;
  1349. if (lcd_rw_pin != PIN_NOT_SET)
  1350. lcd.pins.rw = lcd_rw_pin;
  1351. if (lcd_cl_pin != PIN_NOT_SET)
  1352. lcd.pins.cl = lcd_cl_pin;
  1353. if (lcd_da_pin != PIN_NOT_SET)
  1354. lcd.pins.da = lcd_da_pin;
  1355. if (lcd_bl_pin != PIN_NOT_SET)
  1356. lcd.pins.bl = lcd_bl_pin;
  1357. /* this is used to catch wrong and default values */
  1358. if (lcd.width <= 0)
  1359. lcd.width = DEFAULT_LCD_WIDTH;
  1360. if (lcd.bwidth <= 0)
  1361. lcd.bwidth = DEFAULT_LCD_BWIDTH;
  1362. if (lcd.hwidth <= 0)
  1363. lcd.hwidth = DEFAULT_LCD_HWIDTH;
  1364. if (lcd.height <= 0)
  1365. lcd.height = DEFAULT_LCD_HEIGHT;
  1366. if (lcd.proto == LCD_PROTO_SERIAL) { /* SERIAL */
  1367. lcd_write_cmd = lcd_write_cmd_s;
  1368. lcd_write_data = lcd_write_data_s;
  1369. lcd_clear_fast = lcd_clear_fast_s;
  1370. if (lcd.pins.cl == PIN_NOT_SET)
  1371. lcd.pins.cl = DEFAULT_LCD_PIN_SCL;
  1372. if (lcd.pins.da == PIN_NOT_SET)
  1373. lcd.pins.da = DEFAULT_LCD_PIN_SDA;
  1374. } else if (lcd.proto == LCD_PROTO_PARALLEL) { /* PARALLEL */
  1375. lcd_write_cmd = lcd_write_cmd_p8;
  1376. lcd_write_data = lcd_write_data_p8;
  1377. lcd_clear_fast = lcd_clear_fast_p8;
  1378. if (lcd.pins.e == PIN_NOT_SET)
  1379. lcd.pins.e = DEFAULT_LCD_PIN_E;
  1380. if (lcd.pins.rs == PIN_NOT_SET)
  1381. lcd.pins.rs = DEFAULT_LCD_PIN_RS;
  1382. if (lcd.pins.rw == PIN_NOT_SET)
  1383. lcd.pins.rw = DEFAULT_LCD_PIN_RW;
  1384. } else {
  1385. lcd_write_cmd = lcd_write_cmd_tilcd;
  1386. lcd_write_data = lcd_write_data_tilcd;
  1387. lcd_clear_fast = lcd_clear_fast_tilcd;
  1388. }
  1389. if (lcd.pins.bl == PIN_NOT_SET)
  1390. lcd.pins.bl = DEFAULT_LCD_PIN_BL;
  1391. if (lcd.pins.e == PIN_NOT_SET)
  1392. lcd.pins.e = PIN_NONE;
  1393. if (lcd.pins.rs == PIN_NOT_SET)
  1394. lcd.pins.rs = PIN_NONE;
  1395. if (lcd.pins.rw == PIN_NOT_SET)
  1396. lcd.pins.rw = PIN_NONE;
  1397. if (lcd.pins.bl == PIN_NOT_SET)
  1398. lcd.pins.bl = PIN_NONE;
  1399. if (lcd.pins.cl == PIN_NOT_SET)
  1400. lcd.pins.cl = PIN_NONE;
  1401. if (lcd.pins.da == PIN_NOT_SET)
  1402. lcd.pins.da = PIN_NONE;
  1403. if (lcd.charset == NOT_SET)
  1404. lcd.charset = DEFAULT_LCD_CHARSET;
  1405. if (lcd.charset == LCD_CHARSET_KS0074)
  1406. lcd_char_conv = lcd_char_conv_ks0074;
  1407. else
  1408. lcd_char_conv = NULL;
  1409. if (lcd.pins.bl != PIN_NONE)
  1410. init_scan_timer();
  1411. pin_to_bits(lcd.pins.e, lcd_bits[LCD_PORT_D][LCD_BIT_E],
  1412. lcd_bits[LCD_PORT_C][LCD_BIT_E]);
  1413. pin_to_bits(lcd.pins.rs, lcd_bits[LCD_PORT_D][LCD_BIT_RS],
  1414. lcd_bits[LCD_PORT_C][LCD_BIT_RS]);
  1415. pin_to_bits(lcd.pins.rw, lcd_bits[LCD_PORT_D][LCD_BIT_RW],
  1416. lcd_bits[LCD_PORT_C][LCD_BIT_RW]);
  1417. pin_to_bits(lcd.pins.bl, lcd_bits[LCD_PORT_D][LCD_BIT_BL],
  1418. lcd_bits[LCD_PORT_C][LCD_BIT_BL]);
  1419. pin_to_bits(lcd.pins.cl, lcd_bits[LCD_PORT_D][LCD_BIT_CL],
  1420. lcd_bits[LCD_PORT_C][LCD_BIT_CL]);
  1421. pin_to_bits(lcd.pins.da, lcd_bits[LCD_PORT_D][LCD_BIT_DA],
  1422. lcd_bits[LCD_PORT_C][LCD_BIT_DA]);
  1423. /*
  1424. * before this line, we must NOT send anything to the display.
  1425. * Since lcd_init_display() needs to write data, we have to
  1426. * enable mark the LCD initialized just before.
  1427. */
  1428. lcd.initialized = true;
  1429. lcd_init_display();
  1430. /* display a short message */
  1431. #ifdef CONFIG_PANEL_CHANGE_MESSAGE
  1432. #ifdef CONFIG_PANEL_BOOT_MESSAGE
  1433. panel_lcd_print("\x1b[Lc\x1b[Lb\x1b[L*" CONFIG_PANEL_BOOT_MESSAGE);
  1434. #endif
  1435. #else
  1436. panel_lcd_print("\x1b[Lc\x1b[Lb\x1b[L*Linux-" UTS_RELEASE "\nPanel-"
  1437. PANEL_VERSION);
  1438. #endif
  1439. lcd.addr.x = 0;
  1440. lcd.addr.y = 0;
  1441. /* clear the display on the next device opening */
  1442. lcd.must_clear = true;
  1443. lcd_gotoxy();
  1444. }
  1445. /*
  1446. * These are the file operation function for user access to /dev/keypad
  1447. */
  1448. static ssize_t keypad_read(struct file *file,
  1449. char __user *buf, size_t count, loff_t *ppos)
  1450. {
  1451. unsigned i = *ppos;
  1452. char __user *tmp = buf;
  1453. if (keypad_buflen == 0) {
  1454. if (file->f_flags & O_NONBLOCK)
  1455. return -EAGAIN;
  1456. if (wait_event_interruptible(keypad_read_wait,
  1457. keypad_buflen != 0))
  1458. return -EINTR;
  1459. }
  1460. for (; count-- > 0 && (keypad_buflen > 0);
  1461. ++i, ++tmp, --keypad_buflen) {
  1462. put_user(keypad_buffer[keypad_start], tmp);
  1463. keypad_start = (keypad_start + 1) % KEYPAD_BUFFER;
  1464. }
  1465. *ppos = i;
  1466. return tmp - buf;
  1467. }
  1468. static int keypad_open(struct inode *inode, struct file *file)
  1469. {
  1470. int ret;
  1471. ret = -EBUSY;
  1472. if (!atomic_dec_and_test(&keypad_available))
  1473. goto fail; /* open only once at a time */
  1474. ret = -EPERM;
  1475. if (file->f_mode & FMODE_WRITE) /* device is read-only */
  1476. goto fail;
  1477. keypad_buflen = 0; /* flush the buffer on opening */
  1478. return 0;
  1479. fail:
  1480. atomic_inc(&keypad_available);
  1481. return ret;
  1482. }
  1483. static int keypad_release(struct inode *inode, struct file *file)
  1484. {
  1485. atomic_inc(&keypad_available);
  1486. return 0;
  1487. }
  1488. static const struct file_operations keypad_fops = {
  1489. .read = keypad_read, /* read */
  1490. .open = keypad_open, /* open */
  1491. .release = keypad_release, /* close */
  1492. .llseek = default_llseek,
  1493. };
  1494. static struct miscdevice keypad_dev = {
  1495. .minor = KEYPAD_MINOR,
  1496. .name = "keypad",
  1497. .fops = &keypad_fops,
  1498. };
  1499. static void keypad_send_key(const char *string, int max_len)
  1500. {
  1501. /* send the key to the device only if a process is attached to it. */
  1502. if (!atomic_read(&keypad_available)) {
  1503. while (max_len-- && keypad_buflen < KEYPAD_BUFFER && *string) {
  1504. keypad_buffer[(keypad_start + keypad_buflen++) %
  1505. KEYPAD_BUFFER] = *string++;
  1506. }
  1507. wake_up_interruptible(&keypad_read_wait);
  1508. }
  1509. }
  1510. /* this function scans all the bits involving at least one logical signal,
  1511. * and puts the results in the bitfield "phys_read" (one bit per established
  1512. * contact), and sets "phys_read_prev" to "phys_read".
  1513. *
  1514. * Note: to debounce input signals, we will only consider as switched a signal
  1515. * which is stable across 2 measures. Signals which are different between two
  1516. * reads will be kept as they previously were in their logical form (phys_prev).
  1517. * A signal which has just switched will have a 1 in
  1518. * (phys_read ^ phys_read_prev).
  1519. */
  1520. static void phys_scan_contacts(void)
  1521. {
  1522. int bit, bitval;
  1523. char oldval;
  1524. char bitmask;
  1525. char gndmask;
  1526. phys_prev = phys_curr;
  1527. phys_read_prev = phys_read;
  1528. phys_read = 0; /* flush all signals */
  1529. /* keep track of old value, with all outputs disabled */
  1530. oldval = r_dtr(pprt) | scan_mask_o;
  1531. /* activate all keyboard outputs (active low) */
  1532. w_dtr(pprt, oldval & ~scan_mask_o);
  1533. /* will have a 1 for each bit set to gnd */
  1534. bitmask = PNL_PINPUT(r_str(pprt)) & scan_mask_i;
  1535. /* disable all matrix signals */
  1536. w_dtr(pprt, oldval);
  1537. /* now that all outputs are cleared, the only active input bits are
  1538. * directly connected to the ground
  1539. */
  1540. /* 1 for each grounded input */
  1541. gndmask = PNL_PINPUT(r_str(pprt)) & scan_mask_i;
  1542. /* grounded inputs are signals 40-44 */
  1543. phys_read |= (__u64)gndmask << 40;
  1544. if (bitmask != gndmask) {
  1545. /*
  1546. * since clearing the outputs changed some inputs, we know
  1547. * that some input signals are currently tied to some outputs.
  1548. * So we'll scan them.
  1549. */
  1550. for (bit = 0; bit < 8; bit++) {
  1551. bitval = BIT(bit);
  1552. if (!(scan_mask_o & bitval)) /* skip unused bits */
  1553. continue;
  1554. w_dtr(pprt, oldval & ~bitval); /* enable this output */
  1555. bitmask = PNL_PINPUT(r_str(pprt)) & ~gndmask;
  1556. phys_read |= (__u64)bitmask << (5 * bit);
  1557. }
  1558. w_dtr(pprt, oldval); /* disable all outputs */
  1559. }
  1560. /*
  1561. * this is easy: use old bits when they are flapping,
  1562. * use new ones when stable
  1563. */
  1564. phys_curr = (phys_prev & (phys_read ^ phys_read_prev)) |
  1565. (phys_read & ~(phys_read ^ phys_read_prev));
  1566. }
  1567. static inline int input_state_high(struct logical_input *input)
  1568. {
  1569. #if 0
  1570. /* FIXME:
  1571. * this is an invalid test. It tries to catch
  1572. * transitions from single-key to multiple-key, but
  1573. * doesn't take into account the contacts polarity.
  1574. * The only solution to the problem is to parse keys
  1575. * from the most complex to the simplest combinations,
  1576. * and mark them as 'caught' once a combination
  1577. * matches, then unmatch it for all other ones.
  1578. */
  1579. /* try to catch dangerous transitions cases :
  1580. * someone adds a bit, so this signal was a false
  1581. * positive resulting from a transition. We should
  1582. * invalidate the signal immediately and not call the
  1583. * release function.
  1584. * eg: 0 -(press A)-> A -(press B)-> AB : don't match A's release.
  1585. */
  1586. if (((phys_prev & input->mask) == input->value) &&
  1587. ((phys_curr & input->mask) > input->value)) {
  1588. input->state = INPUT_ST_LOW; /* invalidate */
  1589. return 1;
  1590. }
  1591. #endif
  1592. if ((phys_curr & input->mask) == input->value) {
  1593. if ((input->type == INPUT_TYPE_STD) &&
  1594. (input->high_timer == 0)) {
  1595. input->high_timer++;
  1596. if (input->u.std.press_fct)
  1597. input->u.std.press_fct(input->u.std.press_data);
  1598. } else if (input->type == INPUT_TYPE_KBD) {
  1599. /* will turn on the light */
  1600. keypressed = 1;
  1601. if (input->high_timer == 0) {
  1602. char *press_str = input->u.kbd.press_str;
  1603. if (press_str[0]) {
  1604. int s = sizeof(input->u.kbd.press_str);
  1605. keypad_send_key(press_str, s);
  1606. }
  1607. }
  1608. if (input->u.kbd.repeat_str[0]) {
  1609. char *repeat_str = input->u.kbd.repeat_str;
  1610. if (input->high_timer >= KEYPAD_REP_START) {
  1611. int s = sizeof(input->u.kbd.repeat_str);
  1612. input->high_timer -= KEYPAD_REP_DELAY;
  1613. keypad_send_key(repeat_str, s);
  1614. }
  1615. /* we will need to come back here soon */
  1616. inputs_stable = 0;
  1617. }
  1618. if (input->high_timer < 255)
  1619. input->high_timer++;
  1620. }
  1621. return 1;
  1622. }
  1623. /* else signal falling down. Let's fall through. */
  1624. input->state = INPUT_ST_FALLING;
  1625. input->fall_timer = 0;
  1626. return 0;
  1627. }
  1628. static inline void input_state_falling(struct logical_input *input)
  1629. {
  1630. #if 0
  1631. /* FIXME !!! same comment as in input_state_high */
  1632. if (((phys_prev & input->mask) == input->value) &&
  1633. ((phys_curr & input->mask) > input->value)) {
  1634. input->state = INPUT_ST_LOW; /* invalidate */
  1635. return;
  1636. }
  1637. #endif
  1638. if ((phys_curr & input->mask) == input->value) {
  1639. if (input->type == INPUT_TYPE_KBD) {
  1640. /* will turn on the light */
  1641. keypressed = 1;
  1642. if (input->u.kbd.repeat_str[0]) {
  1643. char *repeat_str = input->u.kbd.repeat_str;
  1644. if (input->high_timer >= KEYPAD_REP_START) {
  1645. int s = sizeof(input->u.kbd.repeat_str);
  1646. input->high_timer -= KEYPAD_REP_DELAY;
  1647. keypad_send_key(repeat_str, s);
  1648. }
  1649. /* we will need to come back here soon */
  1650. inputs_stable = 0;
  1651. }
  1652. if (input->high_timer < 255)
  1653. input->high_timer++;
  1654. }
  1655. input->state = INPUT_ST_HIGH;
  1656. } else if (input->fall_timer >= input->fall_time) {
  1657. /* call release event */
  1658. if (input->type == INPUT_TYPE_STD) {
  1659. void (*release_fct)(int) = input->u.std.release_fct;
  1660. if (release_fct)
  1661. release_fct(input->u.std.release_data);
  1662. } else if (input->type == INPUT_TYPE_KBD) {
  1663. char *release_str = input->u.kbd.release_str;
  1664. if (release_str[0]) {
  1665. int s = sizeof(input->u.kbd.release_str);
  1666. keypad_send_key(release_str, s);
  1667. }
  1668. }
  1669. input->state = INPUT_ST_LOW;
  1670. } else {
  1671. input->fall_timer++;
  1672. inputs_stable = 0;
  1673. }
  1674. }
  1675. static void panel_process_inputs(void)
  1676. {
  1677. struct list_head *item;
  1678. struct logical_input *input;
  1679. keypressed = 0;
  1680. inputs_stable = 1;
  1681. list_for_each(item, &logical_inputs) {
  1682. input = list_entry(item, struct logical_input, list);
  1683. switch (input->state) {
  1684. case INPUT_ST_LOW:
  1685. if ((phys_curr & input->mask) != input->value)
  1686. break;
  1687. /* if all needed ones were already set previously,
  1688. * this means that this logical signal has been
  1689. * activated by the releasing of another combined
  1690. * signal, so we don't want to match.
  1691. * eg: AB -(release B)-> A -(release A)-> 0 :
  1692. * don't match A.
  1693. */
  1694. if ((phys_prev & input->mask) == input->value)
  1695. break;
  1696. input->rise_timer = 0;
  1697. input->state = INPUT_ST_RISING;
  1698. /* no break here, fall through */
  1699. case INPUT_ST_RISING:
  1700. if ((phys_curr & input->mask) != input->value) {
  1701. input->state = INPUT_ST_LOW;
  1702. break;
  1703. }
  1704. if (input->rise_timer < input->rise_time) {
  1705. inputs_stable = 0;
  1706. input->rise_timer++;
  1707. break;
  1708. }
  1709. input->high_timer = 0;
  1710. input->state = INPUT_ST_HIGH;
  1711. /* no break here, fall through */
  1712. case INPUT_ST_HIGH:
  1713. if (input_state_high(input))
  1714. break;
  1715. /* no break here, fall through */
  1716. case INPUT_ST_FALLING:
  1717. input_state_falling(input);
  1718. }
  1719. }
  1720. }
  1721. static void panel_scan_timer(void)
  1722. {
  1723. if (keypad.enabled && keypad_initialized) {
  1724. if (spin_trylock_irq(&pprt_lock)) {
  1725. phys_scan_contacts();
  1726. /* no need for the parport anymore */
  1727. spin_unlock_irq(&pprt_lock);
  1728. }
  1729. if (!inputs_stable || phys_curr != phys_prev)
  1730. panel_process_inputs();
  1731. }
  1732. if (lcd.enabled && lcd.initialized) {
  1733. if (keypressed) {
  1734. if (lcd.light_tempo == 0 &&
  1735. ((lcd.flags & LCD_FLAG_L) == 0))
  1736. lcd_backlight(1);
  1737. lcd.light_tempo = FLASH_LIGHT_TEMPO;
  1738. } else if (lcd.light_tempo > 0) {
  1739. lcd.light_tempo--;
  1740. if (lcd.light_tempo == 0 &&
  1741. ((lcd.flags & LCD_FLAG_L) == 0))
  1742. lcd_backlight(0);
  1743. }
  1744. }
  1745. mod_timer(&scan_timer, jiffies + INPUT_POLL_TIME);
  1746. }
  1747. static void init_scan_timer(void)
  1748. {
  1749. if (scan_timer.function)
  1750. return; /* already started */
  1751. setup_timer(&scan_timer, (void *)&panel_scan_timer, 0);
  1752. scan_timer.expires = jiffies + INPUT_POLL_TIME;
  1753. add_timer(&scan_timer);
  1754. }
  1755. /* converts a name of the form "({BbAaPpSsEe}{01234567-})*" to a series of bits.
  1756. * if <omask> or <imask> are non-null, they will be or'ed with the bits
  1757. * corresponding to out and in bits respectively.
  1758. * returns 1 if ok, 0 if error (in which case, nothing is written).
  1759. */
  1760. static u8 input_name2mask(const char *name, __u64 *mask, __u64 *value,
  1761. u8 *imask, u8 *omask)
  1762. {
  1763. const char sigtab[] = "EeSsPpAaBb";
  1764. u8 im, om;
  1765. __u64 m, v;
  1766. om = 0;
  1767. im = 0;
  1768. m = 0ULL;
  1769. v = 0ULL;
  1770. while (*name) {
  1771. int in, out, bit, neg;
  1772. const char *idx;
  1773. idx = strchr(sigtab, *name);
  1774. if (!idx)
  1775. return 0; /* input name not found */
  1776. in = idx - sigtab;
  1777. neg = (in & 1); /* odd (lower) names are negated */
  1778. in >>= 1;
  1779. im |= BIT(in);
  1780. name++;
  1781. if (*name >= '0' && *name <= '7') {
  1782. out = *name - '0';
  1783. om |= BIT(out);
  1784. } else if (*name == '-') {
  1785. out = 8;
  1786. } else {
  1787. return 0; /* unknown bit name */
  1788. }
  1789. bit = (out * 5) + in;
  1790. m |= 1ULL << bit;
  1791. if (!neg)
  1792. v |= 1ULL << bit;
  1793. name++;
  1794. }
  1795. *mask = m;
  1796. *value = v;
  1797. if (imask)
  1798. *imask |= im;
  1799. if (omask)
  1800. *omask |= om;
  1801. return 1;
  1802. }
  1803. /* tries to bind a key to the signal name <name>. The key will send the
  1804. * strings <press>, <repeat>, <release> for these respective events.
  1805. * Returns the pointer to the new key if ok, NULL if the key could not be bound.
  1806. */
  1807. static struct logical_input *panel_bind_key(const char *name, const char *press,
  1808. const char *repeat,
  1809. const char *release)
  1810. {
  1811. struct logical_input *key;
  1812. key = kzalloc(sizeof(*key), GFP_KERNEL);
  1813. if (!key)
  1814. return NULL;
  1815. if (!input_name2mask(name, &key->mask, &key->value, &scan_mask_i,
  1816. &scan_mask_o)) {
  1817. kfree(key);
  1818. return NULL;
  1819. }
  1820. key->type = INPUT_TYPE_KBD;
  1821. key->state = INPUT_ST_LOW;
  1822. key->rise_time = 1;
  1823. key->fall_time = 1;
  1824. strncpy(key->u.kbd.press_str, press, sizeof(key->u.kbd.press_str));
  1825. strncpy(key->u.kbd.repeat_str, repeat, sizeof(key->u.kbd.repeat_str));
  1826. strncpy(key->u.kbd.release_str, release,
  1827. sizeof(key->u.kbd.release_str));
  1828. list_add(&key->list, &logical_inputs);
  1829. return key;
  1830. }
  1831. #if 0
  1832. /* tries to bind a callback function to the signal name <name>. The function
  1833. * <press_fct> will be called with the <press_data> arg when the signal is
  1834. * activated, and so on for <release_fct>/<release_data>
  1835. * Returns the pointer to the new signal if ok, NULL if the signal could not
  1836. * be bound.
  1837. */
  1838. static struct logical_input *panel_bind_callback(char *name,
  1839. void (*press_fct)(int),
  1840. int press_data,
  1841. void (*release_fct)(int),
  1842. int release_data)
  1843. {
  1844. struct logical_input *callback;
  1845. callback = kmalloc(sizeof(*callback), GFP_KERNEL);
  1846. if (!callback)
  1847. return NULL;
  1848. memset(callback, 0, sizeof(struct logical_input));
  1849. if (!input_name2mask(name, &callback->mask, &callback->value,
  1850. &scan_mask_i, &scan_mask_o))
  1851. return NULL;
  1852. callback->type = INPUT_TYPE_STD;
  1853. callback->state = INPUT_ST_LOW;
  1854. callback->rise_time = 1;
  1855. callback->fall_time = 1;
  1856. callback->u.std.press_fct = press_fct;
  1857. callback->u.std.press_data = press_data;
  1858. callback->u.std.release_fct = release_fct;
  1859. callback->u.std.release_data = release_data;
  1860. list_add(&callback->list, &logical_inputs);
  1861. return callback;
  1862. }
  1863. #endif
  1864. static void keypad_init(void)
  1865. {
  1866. int keynum;
  1867. init_waitqueue_head(&keypad_read_wait);
  1868. keypad_buflen = 0; /* flushes any eventual noisy keystroke */
  1869. /* Let's create all known keys */
  1870. for (keynum = 0; keypad_profile[keynum][0][0]; keynum++) {
  1871. panel_bind_key(keypad_profile[keynum][0],
  1872. keypad_profile[keynum][1],
  1873. keypad_profile[keynum][2],
  1874. keypad_profile[keynum][3]);
  1875. }
  1876. init_scan_timer();
  1877. keypad_initialized = 1;
  1878. }
  1879. /**************************************************/
  1880. /* device initialization */
  1881. /**************************************************/
  1882. static int panel_notify_sys(struct notifier_block *this, unsigned long code,
  1883. void *unused)
  1884. {
  1885. if (lcd.enabled && lcd.initialized) {
  1886. switch (code) {
  1887. case SYS_DOWN:
  1888. panel_lcd_print
  1889. ("\x0cReloading\nSystem...\x1b[Lc\x1b[Lb\x1b[L+");
  1890. break;
  1891. case SYS_HALT:
  1892. panel_lcd_print
  1893. ("\x0cSystem Halted.\x1b[Lc\x1b[Lb\x1b[L+");
  1894. break;
  1895. case SYS_POWER_OFF:
  1896. panel_lcd_print("\x0cPower off.\x1b[Lc\x1b[Lb\x1b[L+");
  1897. break;
  1898. default:
  1899. break;
  1900. }
  1901. }
  1902. return NOTIFY_DONE;
  1903. }
  1904. static struct notifier_block panel_notifier = {
  1905. panel_notify_sys,
  1906. NULL,
  1907. 0
  1908. };
  1909. static void panel_attach(struct parport *port)
  1910. {
  1911. struct pardev_cb panel_cb;
  1912. if (port->number != parport)
  1913. return;
  1914. if (pprt) {
  1915. pr_err("%s: port->number=%d parport=%d, already registered!\n",
  1916. __func__, port->number, parport);
  1917. return;
  1918. }
  1919. memset(&panel_cb, 0, sizeof(panel_cb));
  1920. panel_cb.private = &pprt;
  1921. /* panel_cb.flags = 0 should be PARPORT_DEV_EXCL? */
  1922. pprt = parport_register_dev_model(port, "panel", &panel_cb, 0);
  1923. if (!pprt) {
  1924. pr_err("%s: port->number=%d parport=%d, parport_register_device() failed\n",
  1925. __func__, port->number, parport);
  1926. return;
  1927. }
  1928. if (parport_claim(pprt)) {
  1929. pr_err("could not claim access to parport%d. Aborting.\n",
  1930. parport);
  1931. goto err_unreg_device;
  1932. }
  1933. /* must init LCD first, just in case an IRQ from the keypad is
  1934. * generated at keypad init
  1935. */
  1936. if (lcd.enabled) {
  1937. lcd_init();
  1938. if (misc_register(&lcd_dev))
  1939. goto err_unreg_device;
  1940. }
  1941. if (keypad.enabled) {
  1942. keypad_init();
  1943. if (misc_register(&keypad_dev))
  1944. goto err_lcd_unreg;
  1945. }
  1946. register_reboot_notifier(&panel_notifier);
  1947. return;
  1948. err_lcd_unreg:
  1949. if (lcd.enabled)
  1950. misc_deregister(&lcd_dev);
  1951. err_unreg_device:
  1952. parport_unregister_device(pprt);
  1953. pprt = NULL;
  1954. }
  1955. static void panel_detach(struct parport *port)
  1956. {
  1957. if (port->number != parport)
  1958. return;
  1959. if (!pprt) {
  1960. pr_err("%s: port->number=%d parport=%d, nothing to unregister.\n",
  1961. __func__, port->number, parport);
  1962. return;
  1963. }
  1964. if (scan_timer.function)
  1965. del_timer_sync(&scan_timer);
  1966. if (pprt) {
  1967. if (keypad.enabled) {
  1968. misc_deregister(&keypad_dev);
  1969. keypad_initialized = 0;
  1970. }
  1971. if (lcd.enabled) {
  1972. panel_lcd_print("\x0cLCD driver " PANEL_VERSION
  1973. "\nunloaded.\x1b[Lc\x1b[Lb\x1b[L-");
  1974. misc_deregister(&lcd_dev);
  1975. lcd.initialized = false;
  1976. }
  1977. /* TODO: free all input signals */
  1978. parport_release(pprt);
  1979. parport_unregister_device(pprt);
  1980. pprt = NULL;
  1981. unregister_reboot_notifier(&panel_notifier);
  1982. }
  1983. }
  1984. static struct parport_driver panel_driver = {
  1985. .name = "panel",
  1986. .match_port = panel_attach,
  1987. .detach = panel_detach,
  1988. .devmodel = true,
  1989. };
  1990. /* init function */
  1991. static int __init panel_init_module(void)
  1992. {
  1993. int selected_keypad_type = NOT_SET, err;
  1994. /* take care of an eventual profile */
  1995. switch (profile) {
  1996. case PANEL_PROFILE_CUSTOM:
  1997. /* custom profile */
  1998. selected_keypad_type = DEFAULT_KEYPAD_TYPE;
  1999. selected_lcd_type = DEFAULT_LCD_TYPE;
  2000. break;
  2001. case PANEL_PROFILE_OLD:
  2002. /* 8 bits, 2*16, old keypad */
  2003. selected_keypad_type = KEYPAD_TYPE_OLD;
  2004. selected_lcd_type = LCD_TYPE_OLD;
  2005. /* TODO: This two are a little hacky, sort it out later */
  2006. if (lcd_width == NOT_SET)
  2007. lcd_width = 16;
  2008. if (lcd_hwidth == NOT_SET)
  2009. lcd_hwidth = 16;
  2010. break;
  2011. case PANEL_PROFILE_NEW:
  2012. /* serial, 2*16, new keypad */
  2013. selected_keypad_type = KEYPAD_TYPE_NEW;
  2014. selected_lcd_type = LCD_TYPE_KS0074;
  2015. break;
  2016. case PANEL_PROFILE_HANTRONIX:
  2017. /* 8 bits, 2*16 hantronix-like, no keypad */
  2018. selected_keypad_type = KEYPAD_TYPE_NONE;
  2019. selected_lcd_type = LCD_TYPE_HANTRONIX;
  2020. break;
  2021. case PANEL_PROFILE_NEXCOM:
  2022. /* generic 8 bits, 2*16, nexcom keypad, eg. Nexcom. */
  2023. selected_keypad_type = KEYPAD_TYPE_NEXCOM;
  2024. selected_lcd_type = LCD_TYPE_NEXCOM;
  2025. break;
  2026. case PANEL_PROFILE_LARGE:
  2027. /* 8 bits, 2*40, old keypad */
  2028. selected_keypad_type = KEYPAD_TYPE_OLD;
  2029. selected_lcd_type = LCD_TYPE_OLD;
  2030. break;
  2031. }
  2032. /*
  2033. * Overwrite selection with module param values (both keypad and lcd),
  2034. * where the deprecated params have lower prio.
  2035. */
  2036. if (keypad_enabled != NOT_SET)
  2037. selected_keypad_type = keypad_enabled;
  2038. if (keypad_type != NOT_SET)
  2039. selected_keypad_type = keypad_type;
  2040. keypad.enabled = (selected_keypad_type > 0);
  2041. if (lcd_enabled != NOT_SET)
  2042. selected_lcd_type = lcd_enabled;
  2043. if (lcd_type != NOT_SET)
  2044. selected_lcd_type = lcd_type;
  2045. lcd.enabled = (selected_lcd_type > 0);
  2046. if (lcd.enabled) {
  2047. /*
  2048. * Init lcd struct with load-time values to preserve exact
  2049. * current functionality (at least for now).
  2050. */
  2051. lcd.height = lcd_height;
  2052. lcd.width = lcd_width;
  2053. lcd.bwidth = lcd_bwidth;
  2054. lcd.hwidth = lcd_hwidth;
  2055. lcd.charset = lcd_charset;
  2056. lcd.proto = lcd_proto;
  2057. lcd.pins.e = lcd_e_pin;
  2058. lcd.pins.rs = lcd_rs_pin;
  2059. lcd.pins.rw = lcd_rw_pin;
  2060. lcd.pins.cl = lcd_cl_pin;
  2061. lcd.pins.da = lcd_da_pin;
  2062. lcd.pins.bl = lcd_bl_pin;
  2063. /* Leave it for now, just in case */
  2064. lcd.esc_seq.len = -1;
  2065. }
  2066. switch (selected_keypad_type) {
  2067. case KEYPAD_TYPE_OLD:
  2068. keypad_profile = old_keypad_profile;
  2069. break;
  2070. case KEYPAD_TYPE_NEW:
  2071. keypad_profile = new_keypad_profile;
  2072. break;
  2073. case KEYPAD_TYPE_NEXCOM:
  2074. keypad_profile = nexcom_keypad_profile;
  2075. break;
  2076. default:
  2077. keypad_profile = NULL;
  2078. break;
  2079. }
  2080. if (!lcd.enabled && !keypad.enabled) {
  2081. /* no device enabled, let's exit */
  2082. pr_err("driver version " PANEL_VERSION " disabled.\n");
  2083. return -ENODEV;
  2084. }
  2085. err = parport_register_driver(&panel_driver);
  2086. if (err) {
  2087. pr_err("could not register with parport. Aborting.\n");
  2088. return err;
  2089. }
  2090. if (pprt)
  2091. pr_info("driver version " PANEL_VERSION
  2092. " registered on parport%d (io=0x%lx).\n", parport,
  2093. pprt->port->base);
  2094. else
  2095. pr_info("driver version " PANEL_VERSION
  2096. " not yet registered\n");
  2097. return 0;
  2098. }
  2099. static void __exit panel_cleanup_module(void)
  2100. {
  2101. parport_unregister_driver(&panel_driver);
  2102. }
  2103. module_init(panel_init_module);
  2104. module_exit(panel_cleanup_module);
  2105. MODULE_AUTHOR("Willy Tarreau");
  2106. MODULE_LICENSE("GPL");
  2107. /*
  2108. * Local variables:
  2109. * c-indent-level: 4
  2110. * tab-width: 8
  2111. * End:
  2112. */