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