Merge current mainline tree into linux-omap tree
[pandora-kernel.git] / drivers / input / keyboard / innovator_ps2.c
1 /*
2  * drivers/char/innovator_ps2.c
3  *
4  * Basic PS/2 keyboard/mouse driver for the Juno® USAR HID controller
5  * present on the TI Innovator/OMAP1510 Break-out-board.
6  *
7  *
8  * Author: MontaVista Software, Inc.
9  *         <gdavis@mvista.com> or <source@mvista.com>
10  *
11  *
12  * 2003 (c) MontaVista Software, Inc. This file is licensed under
13  * the terms of the GNU General Public License version 2. This program
14  * is licensed "as is" without any warranty of any kind, whether express
15  * or implied.
16  *
17  *
18  * REFERENCES:
19  *
20  * 1.   Technical Reference Manual
21  *      Juno® 01
22  *      Multi-function ICs family
23  *      UR8HC007-001 HID & Power management controller
24  *      Document Number: DOC8-007-001-TR-075
25  *      Date: February 2002
26  *      Copyright ©1998-2002 Semtech Corporation
27  *      http://www.semtech.com/pdf/doc8-007-001-tr.pdf
28  *
29  * 2.   Juno® 01 UR8HC007-001 Data Sheet
30  *      Extremely Low-power Input Device and Power Management IC
31  *      Copyright ©1998-2002 Semtech Corporation
32  *      DOC8-007-001-DS-112
33  *      http://www.semtech.com/pdf/doc8-007-001-ds.pdf
34  *
35  *
36  * HISTORY:
37  *
38  * 20030626: George G. Davis <gdavis@mvista.com>
39  *      Initially based on the following RidgeRun DSPlinux Version 1.6 files:
40  *              linux-2.4.15-rmk1-dsplinux/arch/arm/dsplinux/hid/omap1510_hid.c
41  *              linux-2.4.15-rmk1-dsplinux/arch/arm/dsplinux/hid/omap1510_hid.h
42  *              linux-2.4.15-rmk1-dsplinux/arch/arm/dsplinux/hid/omap1510_ps2.c
43  *              linux-2.4.15-rmk1-dsplinux/arch/arm/dsplinux/hid/omap1510_spi.c
44  *      All original files above are
45  *              Copyright (C) 2001 RidgeRun, Inc.
46  *              Author: Alex McMains <aam@ridgerun.com>
47  *
48  * 20040812: Thiago Radicchi <trr@dcc.ufmg.br>
49  *      Cleanup of old code from 2.4 driver and some debug code.
50  *      Minor changes in interrupt handling code.
51  *
52  * NOTES:
53  *
54  * 1. This driver does not provide support for setting keyboard/mouse
55  *    configuration parameters. Both devices are managed directly by
56  *    the Juno UR8HC007-001 on behalf of the host. This minimises the
57  *    amount of host processing required to manage HID events and state
58  *    changes, e.g. both keyboard and mouse devices are hot pluggable
59  *    with no host intervention required. However, we cannot customise
60  *    keyboard/mouse settings in this case. So we live with the defaults
61  *    as setup by the Juno UR8HC007-001 whatever they may be.
62  * 2. Keyboard auto repeat does not work. See 1 above. : )
63  *
64  *
65  * TODO:
66  *
67  * 1. Complete DPM/LDM stubs and test.
68  * 2. Add SPI error handling support, i.e. resend, etc.,.
69  * 3. Determine why innovator_hid_interrupt() is called for every
70  *    invocation of Innovator FPGA IRQ demux. It appears that the
71  *    missed Innovator ethernet workaround may be to blame. However,
72  *    it does not adversely affect operation of this driver since we
73  *    check for assertion of ATN prior to servicing the interrupt. If
74  *    ATN is negated, we bug out right away.
75  *
76  */
77
78 #include <linux/version.h>
79 #include <linux/stddef.h>
80 #include <linux/module.h>
81 #include <linux/init.h>
82 #include <linux/kernel.h>
83 #include <linux/types.h>
84 #include <linux/delay.h>
85 #include <linux/ptrace.h>
86 #include <linux/sched.h>
87 #include <linux/interrupt.h>
88 #include <linux/input.h>
89 #include <linux/slab.h>
90 #include <linux/fs.h>
91 #include <linux/miscdevice.h>
92 #include <linux/poll.h>
93 #include <linux/string.h>
94 #include <linux/ioport.h>
95 #include <linux/platform_device.h>
96
97 #include <asm/io.h>
98 #include <asm/hardware.h>
99 #include <asm/irq.h>
100 #include <asm/uaccess.h>
101 #include <asm/arch/fpga.h>
102
103 #undef  INNOVATOR_KEYB_DEBUG
104 #ifdef  INNOVATOR_KEYB_DEBUG
105 #define dbg(format, arg...) printk(KERN_DEBUG "%s:%d: " format , \
106                                    __FUNCTION__ , __LINE__ , ## arg)
107 #define entry() printk(KERN_DEBUG "%s:%d: Entry\n" , __FUNCTION__ , __LINE__)
108 #define exit()  printk(KERN_DEBUG "%s:%d: Exit\n" , __FUNCTION__ , __LINE__)
109 #define dump_packet(p, n)                                       \
110         {                                                       \
111                 int i;                                          \
112                 printk(KERN_DEBUG "%s:%d: %08x:" ,              \
113                        __FUNCTION__ , __LINE__ , (int) p);      \
114                 for (i = 0; i < n; i += 1) {                    \
115                         printk(" %02x", (int) p[i]);            \
116                 }                                               \
117                 printk("\n");                                   \
118         }
119 #else
120 #define dbg(format, arg...) do {} while (0)
121 #define entry() do {} while (0)
122 #define exit()  do {} while (0)
123 #define dump_packet(p, n) do {} while (0)
124 #endif
125
126
127 #define PFX     "innovator_ps2"
128 #define err(format, arg...)     printk(KERN_ERR PFX ": " format , ## arg)
129 #define info(format, arg...)    printk(KERN_INFO PFX ": " format , ## arg)
130 #define warn(format, arg...)    printk(KERN_WARNING PFX ": " format , ## arg)
131
132
133 /****************************************************************************/
134
135 /*
136  * Synchronous communications timing parameters (Reference [1] pg 7-7)
137  */
138
139 #define tMSA    5000    /* -/5ms        _SS to _ATN (master transfer) */
140 #define tMAC    100     /* 100us/5ms    _ATN to first clock pulse (master
141                                         transfer) */
142 #define tMIB    150     /* 150us/5ms    Beginning of byte transfer to beginning
143                                         of next byte transfer */
144 #define tSIB    150     /* 150us/5ms    Beginning of byte transfer to beginning
145                                         of next byte transfer */
146 #define tMSP    100     /* -/100us      Last clock pulse of packet to _SS
147                                         de-assertion */
148 #define tMNSA   100     /* -/100us      _SS de-assertion to _ATN de-assertion */
149 #define tMNEXT  120     /* 120uS/-      _ATN release to _SS re-assertion
150                                         (master transfer) */
151 #define tSAS    5000    /* -/5ms        _ATN to _SS (slave transfer) */
152 #define tSSC    100     /* 100us/5ms    _SS to first clock pulse (slave
153                                         transfer) */
154 #define tSNA    100     /* -/100us      Last clock pulse of packet to _ATN
155                                         de-assertion */
156 #define tSNAS   100     /* -/100us      _ATN release to _SS de-assertion */
157 #define tSNEXT  120     /* 120us/-      _SS release to _ATN re-assertion
158                                         (slave transfer) */
159 #define tSCK    4       /* 4us/-        Clock period */
160 #define tSLOW   2       /* 2us/-        Clock LOW period */
161 #define tHOLD   200     /* 200ns/-      Master data hold time */
162 #define tSETUP  100     /* 100ns/-      Master data setup Time */
163 #define tSSETUP 500     /* -/500ns      Slave data setup time from clock
164                                         falling edge */
165
166
167 /*
168  * Protocol Headers (Reference [1], pg. 5-1):
169  */
170
171
172 /* Protocols used in commands issued by the host: */
173 #define SIMPLE                  0x80    /* Simple commands
174                                          * Common for both host and controller
175                                          * protocol headers.
176                                          */
177 #define WRITE_REGISTER_BIT      0x81    /* Write register bit */
178 #define READ_REGISTER_BIT       0x82    /* Read register bit */
179 #define WRITE_REGISTER          0x83    /* Write register */
180 #define READ_REGISTER           0x84    /* Read register */
181 #define WRITE_BLOCK             0x85    /* Write block */
182 #define READ_BLOCK              0x86    /* Read block */
183
184
185 /* Protocols used in responses, reports and alerts issued by the controller: */
186 #define REPORT_REGISTER_BIT     0x81    /* Report register bit & event alerts */
187 #define REPORT_REGISTER         0x83    /* Report register */
188 #define REPORT_BLOCK            0x85    /* Report block */
189 #define POINTING_REPORT         0x87    /* Pointing device data report */
190 #define KEYBOARD_REPORT         0x88    /* Keyboard device data report */
191
192
193 /* Simple Commands (Reference [1], pg 5-3): */
194 #define INITIALIZE              0x00    /* Forces the recipient to enter the
195                                          * known default power-on state.
196                                          */
197 #define INITIALIZATION_COMPLETE 0x01    /* Issued as a hand-shake response only
198                                          * to the "Initialize" command.
199                                          */
200 #define RESEND_REQUEST          0x05    /* Issued upon error in the reception
201                                          * of a package. The recipient resends
202                                          * the last transmitted packet.
203                                          */
204
205 /* Register offsets (Reference [1], pg 6-1 thru 6-9): */
206
207 #define REG_PM_COMM             0
208 #define REG_PM_STATUS           1
209 #define REG_PAGENO              255
210
211 /* Power management bits ((Reference [1], pg 6-10): */
212
213 #define SUS_STATE               0x2     /* in REG_PM_COMM */
214
215 /* Miscellaneous constants: */
216
217 #define X_MSB_SHIFT     (8-4)
218 #define X_MSB_MASK      (3<<4)
219 #define Y_MSB_SHIFT     (8-6)
220 #define Y_MSB_MASK      (3<<6)
221
222
223 #define JUNO_BLOCK_SIZE     32
224 #define JUNO_BUFFER_SIZE    256
225
226
227 /*
228  * Errors:
229  */
230
231 #define E_BAD_HEADER    1
232 #define E_BAD_LRC       2
233 #define E_ZERO_BYTES    3
234 #define E_BAD_VALUE     4
235 #define E_BAD_MODE      5
236 #define E_REPORT_MODE   6
237 #define E_BAD_ACK       7
238 #define E_BAD_DEVICE_ID 8
239 #define E_PKT_SZ        9
240
241
242 /*
243  * Host/Controller Command/Response Formats:
244  */
245
246 typedef struct _simple_t {
247         u8 header;
248         u8 cmd_code;
249         u8 LRC;
250 } __attribute__ ((packed)) simple_t;
251
252 typedef struct _write_bit_t {
253         u8 header;
254         u8 offset;
255         u8 value_bit;
256         u8 LRC;
257 } __attribute__ ((packed)) write_bit_t;
258
259 typedef struct _read_bit_t {
260         u8 header;
261         u8 offset;
262         u8 bit;
263         u8 LRC;
264 } __attribute__ ((packed)) read_bit_t;
265
266 typedef struct _write_reg_t {
267         u8 header;
268         u8 offset;
269         u8 value;
270         u8 LRC;
271 } __attribute__ ((packed)) write_reg_t;
272
273 typedef struct _read_reg_t {
274         u8 header;
275         u8 offset;
276         u8 LRC;
277 } __attribute__ ((packed)) read_reg_t;
278
279 typedef struct _write_block_t {
280         u8 header;
281         u8 offset;
282         u8 length;
283         u8 block[JUNO_BLOCK_SIZE + 1]; /* Hack: LRC is last element of block[] */
284 } __attribute__ ((packed)) write_block_t;
285
286 typedef struct _read_block_t {
287         u8 header;
288         u8 offset;
289         u8 length;
290         u8 LRC;
291 } __attribute__ ((packed)) read_block_t;
292
293 typedef struct _report_bit_t {
294         u8 header;
295         u8 offset;
296         u8 value_bit;
297         u8 LRC;
298 } __attribute__ ((packed)) report_bit_t;
299
300 typedef struct _report_reg_t {
301         u8 header;
302         u8 offset;
303         u8 value;
304         u8 LRC;
305 } __attribute__ ((packed)) report_reg_t;
306
307 typedef struct _report_block_t {
308         u8 header;
309         u8 offset;
310         u8 length;
311         u8 block[32];
312         u8 LRC;
313 } __attribute__ ((packed)) report_block_t;
314
315 typedef struct _mse_report_t {
316         u8 header;
317         u8 buttons;
318         u8 Xdisplacement;
319         u8 Ydisplacement;
320         u8 Zdisplacement;
321         u8 LRC;
322 } __attribute__ ((packed)) mse_report_t;
323
324 typedef struct _kdb_report_t {
325         u8 header;
326         u8 keynum;              /* up > 0x80, down < 0x7E, all keys up 0x00 */
327         u8 LRC;
328 } __attribute__ ((packed)) kdb_report_t;
329
330
331 static u8 buffer[JUNO_BUFFER_SIZE];
332
333 static void do_hid_tasklet(unsigned long);
334 DECLARE_TASKLET(hid_tasklet, do_hid_tasklet, 0);
335 static struct innovator_hid_dev *hid;
336
337 struct innovator_hid_dev {
338         struct input_dev *mouse, *keyboard;
339         int open;
340         int irq_enabled;
341 };
342
343 /****************************************************************************/
344
345 /*
346  * Low-level TI Innovator/OMAP1510 FPGA HID SPI interface helper functions:
347  */
348
349 static u8
350 innovator_fpga_hid_rd(void)
351 {
352         u8 val = inb(INNOVATOR_FPGA_HID_SPI);
353         return val;
354 }
355
356 static void
357 innovator_fpga_hid_wr(u8 val)
358 {
359         outb(val, INNOVATOR_FPGA_HID_SPI);
360 }
361
362 static void
363 innovator_fpga_hid_frob(u8 mask, u8 val)
364 {
365         unsigned long flags;
366         local_irq_save(flags);
367         innovator_fpga_hid_wr((innovator_fpga_hid_rd() & ~mask) | val);
368         local_irq_restore(flags);
369 }
370
371 static void
372 innovator_fpga_hid_set_bits(u8 x)
373 {
374         innovator_fpga_hid_frob(x, x);
375 }
376
377 static void
378 SS(int value)
379 {
380         innovator_fpga_hid_frob(OMAP1510_FPGA_HID_nSS, value ? OMAP1510_FPGA_HID_nSS : 0);
381 }
382
383 static void
384 SCLK(int value)
385 {
386         innovator_fpga_hid_frob(OMAP1510_FPGA_HID_SCLK, value ? OMAP1510_FPGA_HID_SCLK : 0);
387 }
388
389 static void
390 MOSI(int value)
391 {
392         innovator_fpga_hid_frob(OMAP1510_FPGA_HID_MOSI, value ? OMAP1510_FPGA_HID_MOSI : 0);
393 }
394
395 static u8
396 MISO(void)
397 {
398         return ((innovator_fpga_hid_rd() & OMAP1510_FPGA_HID_MISO) ? 1 : 0);
399 }
400
401 static u8 
402 ATN(void)
403 {
404         return ((innovator_fpga_hid_rd() & OMAP1510_FPGA_HID_ATN) ? 1 : 0);
405 }
406
407 static int
408 wait_for_ATN(int assert, int timeout)
409 {
410         do {
411                 if (ATN() == assert)
412                         return 0;
413                 udelay(1);
414         } while (timeout -= 1);
415         return -1;
416 }
417
418 static u8
419 innovator_fpga_hid_xfer_byte(u8 xbyte)
420 {
421         int i;
422         u8 rbyte;
423
424         for (rbyte = 0, i = 7; i >= 0; i -= 1) {
425                 SCLK(0);
426                 MOSI((xbyte >> i) & 1);
427                 udelay(tSLOW);
428                 SCLK(1);
429                 rbyte = (rbyte << 1) | MISO();
430                 udelay(tSLOW);
431         }
432
433         return rbyte;
434 }
435
436 static void
437 innovator_fpga_hid_reset(void)
438 {
439         innovator_fpga_hid_wr(OMAP1510_FPGA_HID_SCLK | OMAP1510_FPGA_HID_MOSI);
440         mdelay(1);
441         innovator_fpga_hid_set_bits(OMAP1510_FPGA_HID_RESETn);
442 }
443
444
445 /*****************************************************************************
446
447   Refer to Reference [1], Chapter 7 / Low-level communications, Serial
448   Peripheral Interface (SPI) implementation Host (master) packet
449   transmission timing, pg. 7-3, for timing and implementation details
450   for spi_xmt().
451
452  *****************************************************************************/
453
454 int
455 spi_xmt(u8 * p, u8 n)
456 {
457         unsigned long flags;
458
459         dump_packet(p, n);
460         local_irq_save(flags);
461         disable_irq(OMAP1510_INT_FPGA_ATN);
462
463         if (ATN()) {
464                 /* Oops, we have a collision. */
465                 enable_irq(OMAP1510_INT_FPGA_ATN);
466                 local_irq_restore(flags);
467                 dbg("Protocol error: ATN is asserted\n");
468                 return -EAGAIN;
469         }
470
471         SS(1);
472
473         if (wait_for_ATN(1, tMSA) < 0) {
474                 SS(0);
475                 enable_irq(OMAP1510_INT_FPGA_ATN);
476                 local_irq_restore(flags);
477                 dbg("timeout waiting for ATN assertion\n");
478                 return -EREMOTEIO;
479         }
480
481         udelay(tMAC);
482
483         while (n--) {
484                 innovator_fpga_hid_xfer_byte(*p++);
485                 if (n) {
486                         udelay(tMIB - 8 * tSCK);
487                 }
488         }
489
490         MOSI(1);        /* Set MOSI to idle high. */
491
492         /* NOTE: The data sheet does not specify a minimum delay
493          * here. But innovator_fpga_hid_xfer_byte() gives us a half-clock
494          * delay (tSLOW) after the last bit is sent. So I'm happy with
495          * that.
496          */
497
498         SS(0);
499
500         if (wait_for_ATN(0, tMNSA) < 0) {
501                 enable_irq(OMAP1510_INT_FPGA_ATN);
502                 local_irq_restore(flags);
503                 dbg("timeout waiting for ATN negation\n");
504                 return -EREMOTEIO;
505         }
506
507         udelay(tMNEXT);
508         enable_irq(OMAP1510_INT_FPGA_ATN);
509         local_irq_restore(flags);
510         return 0;
511 }
512
513
514 /*****************************************************************************
515
516   Refer to Reference [1],  Chapter 7 / Low-level communications, Serial
517   Peripheral Interface (SPI) implementation, Slave packet transmission
518   timing, pg. 7-5, for timing and implementation details for spi_rcv().
519
520  *****************************************************************************/
521
522 int
523 spi_rcv(u8 * p, int len)
524 {
525         unsigned long flags;
526         int ret = 0;
527
528         if (len > 256) {
529                 /* Limit packet size to something reasonable */
530                 return -1;
531         }
532
533         local_irq_save(flags);
534
535         if (wait_for_ATN(1, tMSA) < 0) {
536                 local_irq_restore(flags);
537                 dbg("Protocol error: ATN is not asserted\n");
538                 return -EREMOTEIO;
539         }
540
541         SS(1);
542
543         udelay(tSSC);
544
545         while (ATN()) {
546                 if (ret >= len) {
547                         err("over run error\n");
548                         ret = -1;
549                         break;
550                 }
551                 p[ret++] = innovator_fpga_hid_xfer_byte(0xff);
552                 udelay(tSNA);   /* Wait long enough to detect negation of ATN
553                                  * after last clock pulse of packet.
554                                  *
555                                  * NOTE: Normally, we need a minimum delay of
556                                  *       tSIB between the start of one byte
557                                  *       and the start of the next. However,
558                                  *       we also need to wait long enough
559                                  *       for the USAR to negate ATN before
560                                  *       starting the next byte. So we use
561                                  *       max(tSIB - 8 * tSCK, tSNA) here to
562                                  *       satisfy both constraints.
563                                  */
564         }
565
566         SS(0);  /* NOTE: The data sheet does not specify a minimum delay
567                  * here. But innovator_fpga_hid_xfer_byte() gives us a
568                  * half-clock delay (tSLOW) after the last bit is sent. So
569                  * I'm happy with that (rather than no delay at all : ).
570                  */
571
572
573         udelay(tSNEXT); /* This isn't quite right. Assertion of ATN after
574                          * negation of SS is an USAR timing constraint.
575                          * What we need here is a spec for the minimum
576                          * delay from SS negation to SS assertion. But
577                          * for now, just use this brain dead delay.
578                          */
579
580         local_irq_restore(flags);
581
582         if (ret > 0) {
583                 dump_packet(p, ret);
584         }
585
586         return ret;
587 }
588
589
590 /*****************************************************************************
591   Calculate Host/Controller Command/Response Longitudinal Redundancy Check (LRC)
592
593   The algorithm implemented in calculate_LRC() below is taken directly from
594   the reference [1], Chapter 7 / Low-level communications, LRC (Longitudinal
595   Redundancy Check), pg 5-10.
596
597  *****************************************************************************/
598
599 static u8
600 calculate_LRC(u8 * p, int n)
601 {
602         u8 LRC;
603         int i;
604
605         /*
606          * Init the LRC using the first two message bytes.
607          */
608         LRC = p[0] ^ p[1];
609
610         /*
611          * Update the LRC using the remainder of the p.
612          */
613         for (i = 2; i < n; i++)
614                 LRC ^= p[i];
615
616         /*
617          * If the MSB is set then clear the MSB and change the next
618          * most significant bit
619          */
620         if (LRC & 0x80)
621                 LRC ^= 0xC0;
622
623         return LRC;
624 }
625
626
627 /*
628  * Controller response helper functions:
629  */
630
631 static inline int
632 report_mouse(mse_report_t * p, int n)
633 {
634         if (p->header != POINTING_REPORT)
635                 return -E_BAD_HEADER;
636
637         if (n != sizeof(mse_report_t))
638                 return -E_PKT_SZ;
639
640         return (p->LRC != calculate_LRC((u8 *) p, sizeof(mse_report_t) - 1)) ?
641                 -E_BAD_LRC : POINTING_REPORT;
642 }
643
644 static inline int
645 report_keyboard(kdb_report_t * p, int n)
646 {
647         if (p->header != KEYBOARD_REPORT)
648                 return -E_BAD_HEADER;
649
650         if (n != sizeof(kdb_report_t))
651                 return -E_PKT_SZ;
652
653         return (p->LRC != calculate_LRC((u8 *) p, sizeof(kdb_report_t) - 1)) ?
654                 -E_BAD_LRC : KEYBOARD_REPORT;
655 }
656
657
658 /*
659  * Miscellaneous helper functions:
660  */
661
662 static inline int
663 report_type(u8 * type)
664 {
665         /* check the header to find out what kind of report it is */
666         if ((*type) == KEYBOARD_REPORT)
667                 return KEYBOARD_REPORT;
668         else if ((*type) == POINTING_REPORT)
669                 return POINTING_REPORT;
670         else
671                 return -E_BAD_HEADER;
672 }
673
674 static inline int
675 report_async(void * p, int n)
676 {
677         int ret;
678
679         if ((ret = spi_rcv((u8 *) p, n)) < 0)
680                 return ret;
681
682         if (report_type((u8 *) p) == POINTING_REPORT)
683                 ret = report_mouse((mse_report_t *) p, ret);
684         else if (report_type((u8 *) p) == KEYBOARD_REPORT)
685                 ret = report_keyboard((kdb_report_t *) p, ret);
686
687         return ret;
688 }
689
690 /*
691  * Host command helper functions:
692  */
693
694 #if     0
695 /* REVISIT/TODO: Wrapper for command/response with resend handing. */
696 static int
697 spi_xfer(u8 * optr, u8 osz, u8 * iptr, u8 isz)
698 {
699         static u8 buf[256];
700         int ret;
701         int xretries = 3;
702
703         do {
704                 if (optr != NULL && osz) {
705                         do {
706                                 ret = spi_xmt((u8 *) optr, osz);
707                         } while (ret < 0);
708                 }
709
710                 ret = spi_rcv((u8 *) buf, 256);
711
712                 if (ret == -EREMOTEIO) {
713                         if (iptr == NULL) {
714                                 break;
715                         }
716                 }
717         } while (xretries--);
718
719         return ret;
720 }
721 #endif
722
723 /* REVISIT: Enable these when/if additional Juno features are required. */
724 static inline int
725 simple(u8 cmd)
726 {
727         static simple_t p;
728         int ret;
729
730         p.header = SIMPLE;
731         p.cmd_code = cmd;
732         p.LRC = calculate_LRC((u8 *) & p, sizeof(p) - 1);
733
734         if ((ret = spi_xmt((u8 *) & p, sizeof(p))) < 0)
735                 return ret;
736
737         if ((ret = spi_rcv((u8 *) & p, sizeof(p))) < 0)
738                 return ret;
739
740         if (ret == 0)
741                 return -E_ZERO_BYTES;
742
743         if (ret != sizeof(p))
744                 return -E_PKT_SZ;
745
746         if (p.header != SIMPLE)
747                 return -E_BAD_HEADER;
748
749         if (p.LRC != calculate_LRC((u8 *) & p, sizeof(p) - 1))
750                 return -E_BAD_LRC;
751
752         /* REVISIT: Need to check or return response code here? */
753 }
754
755 static inline int
756 write_bit(u8 offset, u8 bit, u8 value)
757 {
758         static write_bit_t p;
759
760         p.header = WRITE_REGISTER_BIT;
761         p.offset = offset;
762         p.value_bit = (bit << 1) | (value & 1);
763         p.LRC = calculate_LRC((u8 *) & p, sizeof(p) - 1);
764
765         return spi_xmt((u8 *) & p, sizeof(p));
766 }
767
768 static inline int
769 read_bit(u8 offset, u8 bit, u8 * data)
770 {
771         static read_bit_t p;
772         static report_bit_t q;
773         int ret;
774
775         p.header = READ_REGISTER_BIT;
776         p.offset = offset;
777         p.bit = bit;
778         p.LRC = calculate_LRC((u8 *) & p, sizeof(p) - 1);
779
780         if ((ret = spi_xmt((u8 *) & p, sizeof(p))) < 0)
781                 return ret;
782
783         if ((ret = spi_rcv((u8 *) & q, sizeof(q))) < 0)
784                 return ret;
785
786         if (ret == 0)
787                 return -E_ZERO_BYTES;
788
789         if (ret != sizeof(q))
790                 return -E_PKT_SZ;
791
792         if (q.header != REPORT_REGISTER_BIT)
793                 return -E_BAD_HEADER;
794
795         if (q.LRC != calculate_LRC((u8 *) & q, sizeof(q) - 1))
796                 return -E_BAD_LRC;
797
798         *data = q.value_bit;
799
800         return 0;
801 }
802
803 static inline int
804 write_reg(u8 offset, u8 value)
805 {
806         static write_reg_t p;
807
808         p.header = WRITE_REGISTER;
809         p.offset = offset;
810         p.value = value;
811         p.LRC = calculate_LRC((u8 *) & p, sizeof(p) - 1);
812
813         return spi_xmt((u8 *) & p, sizeof(p));
814 }
815
816 static inline int
817 read_reg(u8 offset, u8 * data)
818 {
819         static read_reg_t p;
820         static report_reg_t q;
821         int ret;
822
823         p.header = READ_REGISTER;
824         p.offset = offset;
825         p.LRC = calculate_LRC((u8 *) & p, sizeof(p) - 1);
826
827         if ((ret = spi_xmt((u8 *) & p, sizeof(p))) < 0)
828                 return ret;
829
830         if ((ret = spi_rcv((u8 *) & q, sizeof(q))) < 0)
831                 return ret;
832
833         if (ret == 0)
834                 return -E_ZERO_BYTES;
835
836         if (ret != sizeof(q))
837                 return -E_PKT_SZ;
838
839         if (q.header != REPORT_REGISTER)
840                 return -E_BAD_HEADER;
841
842         if (q.LRC != calculate_LRC((u8 *) & q, sizeof(q) - 1))
843                 return -E_BAD_LRC;
844
845         *data = q.value;
846
847         return 0;
848 }
849
850 static inline int
851 write_block(u8 offset, u8 length, u8 * block)
852 {
853         static write_block_t p;
854
855         p.header = WRITE_BLOCK;
856         p.offset = offset;
857         p.length = length;
858         memcpy(&p.block, block, length);
859         p.block[length] = calculate_LRC((u8 *) & p, 3 + length);
860
861         return spi_xmt((u8 *) & p, 4 + length);
862 }
863
864 static inline int
865 read_block(u8 offset, u8 length, u8 * buf)
866 {
867         static read_block_t p;
868         static report_block_t q;
869         int ret;
870
871         p.header = READ_BLOCK;
872         p.offset = offset;
873         p.length = length;
874         p.LRC = calculate_LRC((u8 *) & p, sizeof(p) - 1);
875
876         if ((ret = spi_xmt((u8 *) & p, sizeof(p))) < 0)
877                 return ret;
878
879         if ((ret = spi_rcv((u8 *) & q, sizeof(q))) < 0)
880                 return ret;
881
882         if (ret == 0)
883                 return -E_ZERO_BYTES;
884
885         if (ret != sizeof(4 + q.length))
886                 return -E_PKT_SZ;
887
888         if (q.header != REPORT_BLOCK)
889                 return -E_BAD_HEADER;
890
891         if (q.block[q.length] != calculate_LRC((u8 *) & q, 3 + q.length))
892                 return -E_BAD_LRC;
893
894         if (length != q.length)
895                 return -E_PKT_SZ;
896
897         memcpy(buf, &q.block, length);
898
899         return 0;
900 }
901
902 #ifdef  INNOVATOR_KEYB_DEBUG
903 static void
904 ctrl_dump_regs(void)
905 {
906         int i;
907         int n;
908
909         for (i = 0; i < 256; i += 8) {
910                 read_block(i, 16, buffer);
911                 mdelay(1);
912         }
913 }
914 #endif
915
916 /*****************************************************************************/
917
918 static void
919 process_pointing_report(struct innovator_hid_dev *hid, u8 * buffer)
920 {
921         static int prev_x, prev_y, prev_btn;
922         int x, y, btn;
923         hid->keyboard = input_allocate_device();
924         hid->mouse = input_allocate_device();
925
926         if (buffer[1] & (1 << 3)) {
927                 /* relative pointing device report */
928                 x = buffer[2];
929                 y = buffer[3];
930
931                 /* check the sign and convert from 2's complement if negative */
932                 if (buffer[1] & (1<<4))
933                         x = ~(-x) - 255;
934
935                 /* input driver wants -y */
936                 if (buffer[1] & (1<<5))
937                         y = -(~(-y) - 255);
938                 else
939                         y = -y;
940
941                 input_report_key(hid->mouse,
942                                  BTN_LEFT, buffer[1] & (1<<0));
943                 input_report_key(hid->mouse,
944                                  BTN_RIGHT, buffer[1] & (1<<1));
945                 input_report_key(hid->mouse,
946                                  BTN_MIDDLE, buffer[1] & (1<<2));
947                 input_report_rel(hid->mouse, REL_X, x);
948                 input_report_rel(hid->mouse, REL_Y, y);
949         } else {
950                 /* REVISIT: Does this work? */
951                 /* absolute pointing device report */
952                 x = buffer[2] + ((buffer[1] & X_MSB_MASK) << X_MSB_SHIFT);
953                 y = buffer[3] + ((buffer[1] & Y_MSB_MASK) << Y_MSB_SHIFT);
954                 btn = buffer[1] & (1<<0);
955
956                 if ((prev_x == x) && (prev_y == y)
957                     && (prev_btn == btn))
958                         return;
959
960                 input_report_key(hid->mouse, BTN_LEFT, btn);
961                 input_report_abs(hid->mouse, ABS_X, x);
962                 input_report_abs(hid->mouse, ABS_Y, y);
963                 prev_x = x;
964                 prev_y = y;
965                 prev_btn = btn;
966         }
967         input_sync(hid->mouse);
968         dbg("HID X: %d Y: %d Functions: %x\n", x, y, buffer[1]);
969 }
970
971 /*
972  * Reference [1], Appendix A, Semtech standard PS/2 key number definitions,
973  * pgs. A-1 through A-3. The following table lists standard PS/2 key numbers
974  * used by the Juno® 01 keyboard manager.
975  *
976  * NOTES:
977  * 1. The following table indices are E0 codes which require special handling:
978  *      53..62, 77..78, 94, 96, 100, 102..104, 108..110
979  * 2. The following table indices are E1 codes which require special handling:
980  *      101
981  */
982
983 static unsigned char usar2scancode[128] = {
984         0x00, 0x29, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
985         0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
986         0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
987         0x18, 0x19, 0x1a, 0x1b, 0x2b, 0x1e, 0x1f, 0x20,
988         0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
989         0x1c, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32,
990         0x33, 0x34, 0x35, 0x39, 0x01, 0x52, 0x53, 0x4b,
991         0x47, 0x4f, 0x48, 0x50, 0x49, 0x51, 0x4d, 0x37,
992         0x4e, 0x4f, 0x50, 0x51, 0x4b, 0x4c, 0x4d, 0x47,
993         0x48, 0x49, 0x52, 0x53, 0x4a, 0x1c, 0x35, 0x3b,
994         0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x41, 0x42, 0x43,
995         0x44, 0x57, 0x58, 0x2a, 0x36, 0x38, 0x38, 0x1d,
996         0x1d, 0x3a, 0x45, 0x46, 0x2a, 0x1d, 0x5b, 0x5c,
997         0x5d, 0xff, 0x00, 0x00, 0x5e, 0x5f, 0x63, 0x70,
998         0x7b, 0x79, 0x7d, 0x73, 0x5b, 0x5c, 0x5d, 0x63,
999         0x65, 0x66, 0x68, 0x69, 0x6b, 0x56, 0x54, 0x00
1000 };
1001
1002 /*
1003  * The following are bit masks used to encode E0 scan codes which
1004  * require special handling. However, scan codes 100 and 101 are
1005  * excludable here since they each require unique multi-byte scan
1006  * code translations and are therefore dealt with individually via
1007  * handle_print_scr() and handle_pause() respectively below.
1008  */
1009
1010 static unsigned long int e0_codes1 = 0x030003ff; /* scan codes 53..84 */
1011 static unsigned long int e0_codes2 = 0x038e0a00; /* scan codes 85..116 */
1012
1013 static void
1014 handle_print_scr(int up)
1015 {
1016         if (up) {
1017                 input_report_key(hid->keyboard, 0xe0, 1);
1018                 input_report_key(hid->keyboard, 0xb7, 1);
1019                 input_report_key(hid->keyboard, 0xe0, 1);
1020                 input_report_key(hid->keyboard, 0xaa, 1);
1021         } else {
1022                 input_report_key(hid->keyboard, 0xe0, 0);
1023                 input_report_key(hid->keyboard, 0x2a, 0);
1024                 input_report_key(hid->keyboard, 0xe0, 0);
1025                 input_report_key(hid->keyboard, 0x37, 0);
1026         }
1027 }
1028
1029 static void
1030 handle_pause(void)
1031 {
1032         input_report_key(hid->keyboard, 0xe1, 0);
1033         input_report_key(hid->keyboard, 0x1d, 0);
1034         input_report_key(hid->keyboard, 0x45, 0);
1035         input_report_key(hid->keyboard, 0xe1, 0);
1036         input_report_key(hid->keyboard, 0x9d, 0);
1037         input_report_key(hid->keyboard, 0xc5, 0);
1038 }
1039
1040 static void
1041 process_keyboard_report(struct innovator_hid_dev *hid, u8 * buffer)
1042 {
1043         unsigned char ch = buffer[1] & 0x7f;
1044         int up = buffer[1] & 0x80 ? 1 : 0;
1045         int is_e0 = 0;
1046         hid->keyboard = input_allocate_device();
1047         hid->mouse = input_allocate_device();
1048
1049         if ((ch == 106) || (ch == 107))
1050                 return;         /* no code */
1051
1052         if (ch == 100) {
1053                 handle_print_scr(up);
1054                 return;
1055         }
1056
1057         if (ch == 101) {
1058                 handle_pause();
1059                 return;
1060         }
1061
1062         if ((ch >= 53) && (ch <= 84)) {
1063                 /* first block of e0 codes */
1064                 is_e0 = e0_codes1 & (1 << (ch - 53));
1065         } else if ((ch >= 85) && (ch <= 116)) {
1066                 /* second block of e0 codes */
1067                 is_e0 = e0_codes2 & (1 << (ch - 85));
1068         }
1069
1070         if (is_e0) {
1071                 input_report_key(hid->keyboard, 0xe0, !up);
1072         }
1073         input_report_key(hid->keyboard, usar2scancode[ch], !up);
1074         input_sync(hid->keyboard);
1075 }
1076
1077 static irqreturn_t
1078 innovator_hid_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1079 {
1080         if (ATN()) {
1081                 disable_irq(OMAP1510_INT_FPGA_ATN);
1082                 tasklet_schedule(&hid_tasklet);
1083         }
1084         return IRQ_HANDLED;
1085 }
1086
1087 static void
1088 do_hid_tasklet(unsigned long unused)
1089 {
1090         int ret;
1091         if ((ret = report_async(buffer, 256)) == -1) {
1092                 dbg("Error: Bad Juno return value: %d\n", ret);
1093         } else if (ret == KEYBOARD_REPORT) {
1094                 process_keyboard_report(hid, buffer);
1095         } else if (ret == POINTING_REPORT) {
1096                 process_pointing_report(hid, buffer);
1097         } else {
1098                 dbg("ERROR: bad report\n");
1099         }
1100         enable_irq(OMAP1510_INT_FPGA_ATN);
1101 }
1102
1103 static int
1104 innovator_hid_open(struct input_dev *dev)
1105 {
1106         if (hid->open++)
1107                 return 0;
1108
1109         if (request_irq(OMAP1510_INT_FPGA_ATN, (void *) innovator_hid_interrupt,
1110                         IRQF_DISABLED, PFX, hid) < 0)
1111                 return -EINVAL;
1112
1113         return 0;
1114 }
1115
1116 static void
1117 innovator_hid_close(struct input_dev *dev)
1118 {
1119         if (!--hid->open)
1120                 return;
1121
1122         if (hid == NULL)
1123                 return;
1124
1125         kfree(hid);
1126 }
1127
1128 static int innovator_ps2_remove(struct device *dev)
1129 {
1130         return 0;
1131 }
1132
1133 static void innovator_ps2_device_release(struct device *dev)
1134 {
1135         /* Nothing */
1136 }
1137
1138 static int innovator_ps2_suspend(struct device *dev, pm_message_t state)
1139 {
1140         u8 pmcomm = 0;
1141
1142         /*
1143          * Set SUS_STATE in REG_PM_COMM (Page 0 R0).  This will cause
1144          * PM_MOD bits of REG_PM_STATUS to show suspended state,
1145          * but the SUS_STAT bit of REG_PM_STATUS will continue to
1146          * reflect the state of the _HSUS pin.
1147          */
1148
1149         if (write_reg(REG_PAGENO, 0) < 0)
1150                 printk("ps2 suspend: write_reg REG_PAGENO error\n");
1151
1152         if (read_reg(REG_PM_COMM, &pmcomm) < 0)
1153                 printk("ps2 suspend: read_reg REG_PM_COMM error\n");
1154                 
1155         if (write_reg(REG_PM_COMM, pmcomm | SUS_STATE) < 0)
1156                 printk("ps2 suspend: write_reg REG_PM_COMM error\n");
1157
1158         return 0;
1159 }
1160
1161 static int innovator_ps2_resume(struct device *dev)
1162 {
1163         u8 pmcomm = 0;
1164
1165         /*
1166          * Clear SUS_STATE from REG_PM_COMM (Page 0 R0).
1167          */
1168
1169         if (write_reg(REG_PAGENO, 0) < 0)
1170                 printk("ps2 resume: write_reg REG_PAGENO error\n");
1171
1172         if (read_reg(REG_PM_COMM, &pmcomm) < 0)
1173                 printk("ps2 resume: read_reg REG_PM_COMM error\n");
1174
1175         if (write_reg(REG_PM_COMM, pmcomm & ~SUS_STATE) < 0)
1176                 printk("ps2 resume: write_reg REG_PM_COMM error\n");
1177
1178         return 0;
1179 }
1180
1181 static struct device_driver innovator_ps2_driver = {
1182         .name           = "innovator_ps2",
1183         .bus            = &platform_bus_type,
1184         .remove         = innovator_ps2_remove,
1185         .suspend        = innovator_ps2_suspend,
1186         .resume         = innovator_ps2_resume,
1187 };
1188
1189 static struct platform_device innovator_ps2_device = {
1190         .name           = "ps2",
1191         .id             = -1,
1192         .dev = {
1193                 .driver         = &innovator_ps2_driver,
1194                 .release        = innovator_ps2_device_release,
1195         },
1196 };
1197
1198 static int __init
1199 innovator_kbd_init(void)
1200 {
1201         int i;
1202         info("Innovator PS/2 keyboard/mouse driver v1.0\n");
1203
1204         innovator_fpga_hid_reset();
1205
1206         if ((hid = kmalloc(sizeof(struct innovator_hid_dev),
1207              GFP_KERNEL)) == NULL) {
1208                 warn("unable to allocate space for HID device\n");
1209                 return -ENOMEM;
1210         }
1211
1212         /* setup the mouse */
1213         memset(hid, 0, sizeof(struct innovator_hid_dev));
1214         hid->mouse = input_allocate_device();
1215         hid->mouse->evbit[0] = BIT(EV_KEY) | BIT(EV_REL);
1216         hid->mouse->keybit[BIT_WORD(BTN_MOUSE)] =
1217             BIT(BTN_LEFT) | BIT(BTN_RIGHT) |
1218             BIT(BTN_MIDDLE) | BIT(BTN_TOUCH);
1219         hid->mouse->relbit[0] = BIT(REL_X) | BIT(REL_Y);
1220         hid->mouse->private = hid;
1221         hid->mouse->open = innovator_hid_open;
1222         hid->mouse->close = innovator_hid_close;
1223         hid->mouse->name = "innovator_mouse";
1224         hid->mouse->id.bustype = 0;
1225         hid->mouse->id.vendor = 0;
1226         hid->mouse->id.product = 0;
1227         hid->mouse->id.version = 0;
1228        hid->keyboard = input_allocate_device();
1229         hid->keyboard->evbit[0] = BIT(EV_KEY) | BIT(EV_REP);
1230        hid->keyboard->keycodesize = sizeof(unsigned char);
1231        hid->keyboard->keycodemax = ARRAY_SIZE(usar2scancode);
1232         for(i = 0; i < 128; i++)
1233                 set_bit(usar2scancode[i], hid->keyboard->keybit);
1234         hid->keyboard->private = hid;
1235         hid->keyboard->open = innovator_hid_open;
1236         hid->keyboard->close = innovator_hid_close;
1237         hid->keyboard->name = "innovator_keyboard";
1238         hid->keyboard->id.bustype = 0;
1239         hid->keyboard->id.vendor = 0;
1240         hid->keyboard->id.product = 0;
1241         hid->keyboard->id.version = 0;
1242         input_register_device(hid->mouse);
1243         input_register_device(hid->keyboard);
1244         innovator_hid_open(hid->mouse);
1245         innovator_hid_open(hid->keyboard);
1246
1247         if (driver_register(&innovator_ps2_driver) != 0)
1248                 printk(KERN_ERR "Driver register failed for innovator_ps2\n");
1249
1250         if (platform_device_register(&innovator_ps2_device) != 0) {
1251                 printk(KERN_ERR "Device register failed for ps2\n");
1252                 driver_unregister(&innovator_ps2_driver);
1253         }
1254
1255 #ifdef  INNOVATOR_KEYB_DEBUG
1256         ctrl_dump_regs();
1257 #endif
1258         return 0;
1259 }
1260
1261 static void __exit
1262 innovator_kbd_exit(void)
1263 {
1264         input_unregister_device(hid->mouse);
1265         input_unregister_device(hid->keyboard);
1266         free_irq(OMAP1510_INT_FPGA_ATN, hid);
1267         if (hid != NULL)
1268                 kfree(hid);
1269         driver_unregister(&innovator_ps2_driver);
1270         platform_device_unregister(&innovator_ps2_device);
1271         return;
1272 }
1273
1274 module_init(innovator_kbd_init);
1275 module_exit(innovator_kbd_exit);
1276
1277 MODULE_AUTHOR("George G. Davis <gdavis@mvista.com>");
1278 MODULE_DESCRIPTION("Innovator PS/2 Driver");
1279 MODULE_LICENSE("GPL");