panel.c 62 KB

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