qtronix.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. /*
  2. *
  3. * BRIEF MODULE DESCRIPTION
  4. * Qtronix 990P infrared keyboard driver.
  5. *
  6. *
  7. * Copyright 2001 MontaVista Software Inc.
  8. * Author: MontaVista Software, Inc.
  9. * ppopov@mvista.com or source@mvista.com
  10. *
  11. *
  12. * The bottom portion of this driver was take from
  13. * pc_keyb.c Please see that file for copyrights.
  14. *
  15. * This program is free software; you can redistribute it and/or modify it
  16. * under the terms of the GNU General Public License as published by the
  17. * Free Software Foundation; either version 2 of the License, or (at your
  18. * option) any later version.
  19. *
  20. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  21. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  22. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
  23. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  25. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  26. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  27. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  29. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. *
  31. * You should have received a copy of the GNU General Public License along
  32. * with this program; if not, write to the Free Software Foundation, Inc.,
  33. * 675 Mass Ave, Cambridge, MA 02139, USA.
  34. */
  35. #include <linux/config.h>
  36. /*
  37. * NOTE:
  38. *
  39. * This driver has only been tested with the Consumer IR
  40. * port of the ITE 8172 system controller.
  41. *
  42. * You do not need this driver if you are using the ps/2 or
  43. * USB adapter that the keyboard ships with. You only need
  44. * this driver if your board has a IR port and the keyboard
  45. * data is being sent directly to the IR. In that case,
  46. * you also need some low-level IR support. See it8172_cir.c.
  47. *
  48. */
  49. #ifdef CONFIG_QTRONIX_KEYBOARD
  50. #include <linux/module.h>
  51. #include <linux/types.h>
  52. #include <linux/pci.h>
  53. #include <linux/kernel.h>
  54. #include <asm/it8172/it8172.h>
  55. #include <asm/it8172/it8172_int.h>
  56. #include <asm/it8172/it8172_cir.h>
  57. #include <linux/spinlock.h>
  58. #include <linux/sched.h>
  59. #include <linux/interrupt.h>
  60. #include <linux/tty.h>
  61. #include <linux/mm.h>
  62. #include <linux/signal.h>
  63. #include <linux/init.h>
  64. #include <linux/kbd_ll.h>
  65. #include <linux/delay.h>
  66. #include <linux/poll.h>
  67. #include <linux/miscdevice.h>
  68. #include <linux/slab.h>
  69. #include <linux/kbd_kern.h>
  70. #include <linux/smp_lock.h>
  71. #include <asm/io.h>
  72. #include <linux/pc_keyb.h>
  73. #include <asm/keyboard.h>
  74. #include <linux/bitops.h>
  75. #include <asm/uaccess.h>
  76. #include <asm/irq.h>
  77. #include <asm/system.h>
  78. #define leading1 0
  79. #define leading2 0xF
  80. #define KBD_CIR_PORT 0
  81. #define AUX_RECONNECT 170 /* scancode when ps2 device is plugged (back) in */
  82. static int data_index;
  83. struct cir_port *cir;
  84. static unsigned char kbdbytes[5];
  85. static unsigned char cir_data[32]; /* we only need 16 chars */
  86. static void kbd_int_handler(int irq, void *dev_id, struct pt_regs *regs);
  87. static int handle_data(unsigned char *p_data);
  88. static inline void handle_mouse_event(unsigned char scancode);
  89. static inline void handle_keyboard_event(unsigned char scancode, int down);
  90. static int __init psaux_init(void);
  91. static struct aux_queue *queue; /* Mouse data buffer. */
  92. static int aux_count = 0;
  93. /*
  94. * Keys accessed through the 'Fn' key
  95. * The Fn key does not produce a key-up sequence. So, the first
  96. * time the user presses it, it will be key-down event. The key
  97. * stays down until the user presses it again.
  98. */
  99. #define NUM_FN_KEYS 56
  100. static unsigned char fn_keys[NUM_FN_KEYS] = {
  101. 0,0,0,0,0,0,0,0, /* 0 7 */
  102. 8,9,10,93,0,0,0,0, /* 8 15 */
  103. 0,0,0,0,0,0,0,5, /* 16 23 */
  104. 6,7,91,0,0,0,0,0, /* 24 31 */
  105. 0,0,0,0,0,2,3,4, /* 32 39 */
  106. 92,0,0,0,0,0,0,0, /* 40 47 */
  107. 0,0,0,0,11,0,94,95 /* 48 55 */
  108. };
  109. void __init init_qtronix_990P_kbd(void)
  110. {
  111. int retval;
  112. cir = (struct cir_port *)kmalloc(sizeof(struct cir_port), GFP_KERNEL);
  113. if (!cir) {
  114. printk("Unable to initialize Qtronix keyboard\n");
  115. return;
  116. }
  117. /*
  118. * revisit
  119. * this should be programmable, somehow by the, by the user.
  120. */
  121. cir->port = KBD_CIR_PORT;
  122. cir->baud_rate = 0x1d;
  123. cir->rdwos = 0;
  124. cir->rxdcr = 0x3;
  125. cir->hcfs = 0;
  126. cir->fifo_tl = 0;
  127. cir->cfq = 0x1d;
  128. cir_port_init(cir);
  129. retval = request_irq(IT8172_CIR0_IRQ, kbd_int_handler,
  130. (unsigned long )(SA_INTERRUPT|SA_SHIRQ),
  131. (const char *)"Qtronix IR Keyboard", (void *)cir);
  132. if (retval) {
  133. printk("unable to allocate cir %d irq %d\n",
  134. cir->port, IT8172_CIR0_IRQ);
  135. }
  136. #ifdef CONFIG_PSMOUSE
  137. psaux_init();
  138. #endif
  139. }
  140. static inline unsigned char BitReverse(unsigned short key)
  141. {
  142. unsigned char rkey = 0;
  143. rkey |= (key & 0x1) << 7;
  144. rkey |= (key & 0x2) << 5;
  145. rkey |= (key & 0x4) << 3;
  146. rkey |= (key & 0x8) << 1;
  147. rkey |= (key & 0x10) >> 1;
  148. rkey |= (key & 0x20) >> 3;
  149. rkey |= (key & 0x40) >> 5;
  150. rkey |= (key & 0x80) >> 7;
  151. return rkey;
  152. }
  153. static inline u_int8_t UpperByte(u_int8_t data)
  154. {
  155. return (data >> 4);
  156. }
  157. static inline u_int8_t LowerByte(u_int8_t data)
  158. {
  159. return (data & 0xF);
  160. }
  161. int CheckSumOk(u_int8_t byte1, u_int8_t byte2,
  162. u_int8_t byte3, u_int8_t byte4, u_int8_t byte5)
  163. {
  164. u_int8_t CheckSum;
  165. CheckSum = (byte1 & 0x0F) + byte2 + byte3 + byte4 + byte5;
  166. if ( LowerByte(UpperByte(CheckSum) + LowerByte(CheckSum)) != UpperByte(byte1) )
  167. return 0;
  168. else
  169. return 1;
  170. }
  171. static void kbd_int_handler(int irq, void *dev_id, struct pt_regs *regs)
  172. {
  173. struct cir_port *cir;
  174. int j;
  175. unsigned char int_status;
  176. cir = (struct cir_port *)dev_id;
  177. int_status = get_int_status(cir);
  178. if (int_status & 0x4) {
  179. clear_fifo(cir);
  180. return;
  181. }
  182. while (cir_get_rx_count(cir)) {
  183. cir_data[data_index] = cir_read_data(cir);
  184. if (data_index == 0) {/* expecting first byte */
  185. if (cir_data[data_index] != leading1) {
  186. //printk("!leading byte %x\n", cir_data[data_index]);
  187. set_rx_active(cir);
  188. clear_fifo(cir);
  189. continue;
  190. }
  191. }
  192. if (data_index == 1) {
  193. if ((cir_data[data_index] & 0xf) != leading2) {
  194. set_rx_active(cir);
  195. data_index = 0; /* start over */
  196. clear_fifo(cir);
  197. continue;
  198. }
  199. }
  200. if ( (cir_data[data_index] == 0xff)) { /* last byte */
  201. //printk("data_index %d\n", data_index);
  202. set_rx_active(cir);
  203. #if 0
  204. for (j=0; j<=data_index; j++) {
  205. printk("rx_data %d: %x\n", j, cir_data[j]);
  206. }
  207. #endif
  208. data_index = 0;
  209. handle_data(cir_data);
  210. return;
  211. }
  212. else if (data_index>16) {
  213. set_rx_active(cir);
  214. #if 0
  215. printk("warning: data_index %d\n", data_index);
  216. for (j=0; j<=data_index; j++) {
  217. printk("rx_data %d: %x\n", j, cir_data[j]);
  218. }
  219. #endif
  220. data_index = 0;
  221. clear_fifo(cir);
  222. return;
  223. }
  224. data_index++;
  225. }
  226. }
  227. #define NUM_KBD_BYTES 5
  228. static int handle_data(unsigned char *p_data)
  229. {
  230. u_int32_t bit_bucket;
  231. u_int32_t i, j;
  232. u_int32_t got_bits, next_byte;
  233. int down = 0;
  234. /* Reorganize the bit stream */
  235. for (i=0; i<16; i++)
  236. p_data[i] = BitReverse(~p_data[i]);
  237. /*
  238. * We've already previously checked that p_data[0]
  239. * is equal to leading1 and that (p_data[1] & 0xf)
  240. * is equal to leading2. These twelve bits are the
  241. * leader code. We can now throw them away (the 12
  242. * bits) and continue parsing the stream.
  243. */
  244. bit_bucket = p_data[1] << 12;
  245. got_bits = 4;
  246. next_byte = 2;
  247. /*
  248. * Process four bits at a time
  249. */
  250. for (i=0; i<NUM_KBD_BYTES; i++) {
  251. kbdbytes[i]=0;
  252. for (j=0; j<8; j++) /* 8 bits per byte */
  253. {
  254. if (got_bits < 4) {
  255. bit_bucket |= (p_data[next_byte++] << (8 - got_bits));
  256. got_bits += 8;
  257. }
  258. if ((bit_bucket & 0xF000) == 0x8000) {
  259. /* Convert 1000b to 1 */
  260. kbdbytes[i] = 0x80 | (kbdbytes[i] >> 1);
  261. got_bits -= 4;
  262. bit_bucket = bit_bucket << 4;
  263. }
  264. else if ((bit_bucket & 0xC000) == 0x8000) {
  265. /* Convert 10b to 0 */
  266. kbdbytes[i] = kbdbytes[i] >> 1;
  267. got_bits -= 2;
  268. bit_bucket = bit_bucket << 2;
  269. }
  270. else {
  271. /* bad serial stream */
  272. return 1;
  273. }
  274. if (next_byte > 16) {
  275. //printk("error: too many bytes\n");
  276. return 1;
  277. }
  278. }
  279. }
  280. if (!CheckSumOk(kbdbytes[0], kbdbytes[1],
  281. kbdbytes[2], kbdbytes[3], kbdbytes[4])) {
  282. //printk("checksum failed\n");
  283. return 1;
  284. }
  285. if (kbdbytes[1] & 0x08) {
  286. //printk("m: %x %x %x\n", kbdbytes[1], kbdbytes[2], kbdbytes[3]);
  287. handle_mouse_event(kbdbytes[1]);
  288. handle_mouse_event(kbdbytes[2]);
  289. handle_mouse_event(kbdbytes[3]);
  290. }
  291. else {
  292. if (kbdbytes[2] == 0) down = 1;
  293. #if 0
  294. if (down)
  295. printk("down %d\n", kbdbytes[3]);
  296. else
  297. printk("up %d\n", kbdbytes[3]);
  298. #endif
  299. handle_keyboard_event(kbdbytes[3], down);
  300. }
  301. return 0;
  302. }
  303. DEFINE_SPINLOCK(kbd_controller_lock);
  304. static unsigned char handle_kbd_event(void);
  305. int kbd_setkeycode(unsigned int scancode, unsigned int keycode)
  306. {
  307. printk("kbd_setkeycode scancode %x keycode %x\n", scancode, keycode);
  308. return 0;
  309. }
  310. int kbd_getkeycode(unsigned int scancode)
  311. {
  312. return scancode;
  313. }
  314. int kbd_translate(unsigned char scancode, unsigned char *keycode,
  315. char raw_mode)
  316. {
  317. static int prev_scancode = 0;
  318. if (scancode == 0x00 || scancode == 0xff) {
  319. prev_scancode = 0;
  320. return 0;
  321. }
  322. /* todo */
  323. if (!prev_scancode && scancode == 160) { /* Fn key down */
  324. //printk("Fn key down\n");
  325. prev_scancode = 160;
  326. return 0;
  327. }
  328. else if (prev_scancode && scancode == 160) { /* Fn key up */
  329. //printk("Fn key up\n");
  330. prev_scancode = 0;
  331. return 0;
  332. }
  333. /* todo */
  334. if (prev_scancode == 160) {
  335. if (scancode <= NUM_FN_KEYS) {
  336. *keycode = fn_keys[scancode];
  337. //printk("fn keycode %d\n", *keycode);
  338. }
  339. else
  340. return 0;
  341. }
  342. else if (scancode <= 127) {
  343. *keycode = scancode;
  344. }
  345. else
  346. return 0;
  347. return 1;
  348. }
  349. char kbd_unexpected_up(unsigned char keycode)
  350. {
  351. //printk("kbd_unexpected_up\n");
  352. return 0;
  353. }
  354. static unsigned char kbd_exists = 1;
  355. static inline void handle_keyboard_event(unsigned char scancode, int down)
  356. {
  357. kbd_exists = 1;
  358. handle_scancode(scancode, down);
  359. tasklet_schedule(&keyboard_tasklet);
  360. }
  361. void kbd_leds(unsigned char leds)
  362. {
  363. }
  364. /* dummy */
  365. void kbd_init_hw(void)
  366. {
  367. }
  368. static inline void handle_mouse_event(unsigned char scancode)
  369. {
  370. if(scancode == AUX_RECONNECT){
  371. queue->head = queue->tail = 0; /* Flush input queue */
  372. // __aux_write_ack(AUX_ENABLE_DEV); /* ping the mouse :) */
  373. return;
  374. }
  375. if (aux_count) {
  376. int head = queue->head;
  377. queue->buf[head] = scancode;
  378. head = (head + 1) & (AUX_BUF_SIZE-1);
  379. if (head != queue->tail) {
  380. queue->head = head;
  381. kill_fasync(&queue->fasync, SIGIO, POLL_IN);
  382. wake_up_interruptible(&queue->proc_list);
  383. }
  384. }
  385. }
  386. static unsigned char get_from_queue(void)
  387. {
  388. unsigned char result;
  389. unsigned long flags;
  390. spin_lock_irqsave(&kbd_controller_lock, flags);
  391. result = queue->buf[queue->tail];
  392. queue->tail = (queue->tail + 1) & (AUX_BUF_SIZE-1);
  393. spin_unlock_irqrestore(&kbd_controller_lock, flags);
  394. return result;
  395. }
  396. static inline int queue_empty(void)
  397. {
  398. return queue->head == queue->tail;
  399. }
  400. static int fasync_aux(int fd, struct file *filp, int on)
  401. {
  402. int retval;
  403. //printk("fasync_aux\n");
  404. retval = fasync_helper(fd, filp, on, &queue->fasync);
  405. if (retval < 0)
  406. return retval;
  407. return 0;
  408. }
  409. /*
  410. * Random magic cookie for the aux device
  411. */
  412. #define AUX_DEV ((void *)queue)
  413. static int release_aux(struct inode * inode, struct file * file)
  414. {
  415. fasync_aux(-1, file, 0);
  416. aux_count--;
  417. return 0;
  418. }
  419. static int open_aux(struct inode * inode, struct file * file)
  420. {
  421. if (aux_count++) {
  422. return 0;
  423. }
  424. queue->head = queue->tail = 0; /* Flush input queue */
  425. return 0;
  426. }
  427. /*
  428. * Put bytes from input queue to buffer.
  429. */
  430. static ssize_t read_aux(struct file * file, char * buffer,
  431. size_t count, loff_t *ppos)
  432. {
  433. DECLARE_WAITQUEUE(wait, current);
  434. ssize_t i = count;
  435. unsigned char c;
  436. if (queue_empty()) {
  437. if (file->f_flags & O_NONBLOCK)
  438. return -EAGAIN;
  439. add_wait_queue(&queue->proc_list, &wait);
  440. repeat:
  441. set_current_state(TASK_INTERRUPTIBLE);
  442. if (queue_empty() && !signal_pending(current)) {
  443. schedule();
  444. goto repeat;
  445. }
  446. current->state = TASK_RUNNING;
  447. remove_wait_queue(&queue->proc_list, &wait);
  448. }
  449. while (i > 0 && !queue_empty()) {
  450. c = get_from_queue();
  451. put_user(c, buffer++);
  452. i--;
  453. }
  454. if (count-i) {
  455. struct inode *inode = file->f_dentry->d_inode;
  456. inode->i_atime = current_fs_time(inode->i_sb);
  457. return count-i;
  458. }
  459. if (signal_pending(current))
  460. return -ERESTARTSYS;
  461. return 0;
  462. }
  463. /*
  464. * Write to the aux device.
  465. */
  466. static ssize_t write_aux(struct file * file, const char * buffer,
  467. size_t count, loff_t *ppos)
  468. {
  469. /*
  470. * The ITE boards this was tested on did not have the
  471. * transmit wires connected.
  472. */
  473. return count;
  474. }
  475. static unsigned int aux_poll(struct file *file, poll_table * wait)
  476. {
  477. poll_wait(file, &queue->proc_list, wait);
  478. if (!queue_empty())
  479. return POLLIN | POLLRDNORM;
  480. return 0;
  481. }
  482. struct file_operations psaux_fops = {
  483. .read = read_aux,
  484. .write = write_aux,
  485. .poll = aux_poll,
  486. .open = open_aux,
  487. .release = release_aux,
  488. .fasync = fasync_aux,
  489. };
  490. /*
  491. * Initialize driver.
  492. */
  493. static struct miscdevice psaux_mouse = {
  494. PSMOUSE_MINOR, "psaux", &psaux_fops
  495. };
  496. static int __init psaux_init(void)
  497. {
  498. int retval;
  499. retval = misc_register(&psaux_mouse);
  500. if(retval < 0)
  501. return retval;
  502. queue = (struct aux_queue *) kmalloc(sizeof(*queue), GFP_KERNEL);
  503. if (!queue) {
  504. misc_deregister(&psaux_mouse);
  505. return -ENOMEM;
  506. }
  507. memset(queue, 0, sizeof(*queue));
  508. queue->head = queue->tail = 0;
  509. init_waitqueue_head(&queue->proc_list);
  510. return 0;
  511. }
  512. module_init(init_qtronix_990P_kbd);
  513. #endif