Merge branch 'sched-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[pandora-kernel.git] / drivers / input / serio / hp_sdc.c
1 /*
2  * HP i8042-based System Device Controller driver.
3  *
4  * Copyright (c) 2001 Brian S. Julin
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions, and the following disclaimer,
12  *    without modification.
13  * 2. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
15  *
16  * Alternatively, this software may be distributed under the terms of the
17  * GNU General Public License ("GPL").
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
23  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  *
29  * References:
30  * System Device Controller Microprocessor Firmware Theory of Operation
31  *      for Part Number 1820-4784 Revision B.  Dwg No. A-1820-4784-2
32  * Helge Deller's original hilkbd.c port for PA-RISC.
33  *
34  *
35  * Driver theory of operation:
36  *
37  * hp_sdc_put does all writing to the SDC.  ISR can run on a different
38  * CPU than hp_sdc_put, but only one CPU runs hp_sdc_put at a time
39  * (it cannot really benefit from SMP anyway.)  A tasket fit this perfectly.
40  *
41  * All data coming back from the SDC is sent via interrupt and can be read
42  * fully in the ISR, so there are no latency/throughput problems there.
43  * The problem is with output, due to the slow clock speed of the SDC
44  * compared to the CPU.  This should not be too horrible most of the time,
45  * but if used with HIL devices that support the multibyte transfer command,
46  * keeping outbound throughput flowing at the 6500KBps that the HIL is
47  * capable of is more than can be done at HZ=100.
48  *
49  * Busy polling for IBF clear wastes CPU cycles and bus cycles.  hp_sdc.ibf
50  * is set to 0 when the IBF flag in the status register has cleared.  ISR
51  * may do this, and may also access the parts of queued transactions related
52  * to reading data back from the SDC, but otherwise will not touch the
53  * hp_sdc state. Whenever a register is written hp_sdc.ibf is set to 1.
54  *
55  * The i8042 write index and the values in the 4-byte input buffer
56  * starting at 0x70 are kept track of in hp_sdc.wi, and .r7[], respectively,
57  * to minimize the amount of IO needed to the SDC.  However these values
58  * do not need to be locked since they are only ever accessed by hp_sdc_put.
59  *
60  * A timer task schedules the tasklet once per second just to make
61  * sure it doesn't freeze up and to allow for bad reads to time out.
62  */
63
64 #include <linux/hp_sdc.h>
65 #include <linux/errno.h>
66 #include <linux/init.h>
67 #include <linux/module.h>
68 #include <linux/ioport.h>
69 #include <linux/time.h>
70 #include <linux/semaphore.h>
71 #include <linux/slab.h>
72 #include <linux/hil.h>
73 #include <linux/semaphore.h>
74 #include <asm/io.h>
75 #include <asm/system.h>
76
77 /* Machine-specific abstraction */
78
79 #if defined(__hppa__)
80 # include <asm/parisc-device.h>
81 # define sdc_readb(p)           gsc_readb(p)
82 # define sdc_writeb(v,p)        gsc_writeb((v),(p))
83 #elif defined(__mc68000__)
84 # include <asm/uaccess.h>
85 # define sdc_readb(p)           in_8(p)
86 # define sdc_writeb(v,p)        out_8((p),(v))
87 #else
88 # error "HIL is not supported on this platform"
89 #endif
90
91 #define PREFIX "HP SDC: "
92
93 MODULE_AUTHOR("Brian S. Julin <bri@calyx.com>");
94 MODULE_DESCRIPTION("HP i8042-based SDC Driver");
95 MODULE_LICENSE("Dual BSD/GPL");
96
97 EXPORT_SYMBOL(hp_sdc_request_timer_irq);
98 EXPORT_SYMBOL(hp_sdc_request_hil_irq);
99 EXPORT_SYMBOL(hp_sdc_request_cooked_irq);
100
101 EXPORT_SYMBOL(hp_sdc_release_timer_irq);
102 EXPORT_SYMBOL(hp_sdc_release_hil_irq);
103 EXPORT_SYMBOL(hp_sdc_release_cooked_irq);
104
105 EXPORT_SYMBOL(__hp_sdc_enqueue_transaction);
106 EXPORT_SYMBOL(hp_sdc_enqueue_transaction);
107 EXPORT_SYMBOL(hp_sdc_dequeue_transaction);
108
109 static unsigned int hp_sdc_disabled;
110 module_param_named(no_hpsdc, hp_sdc_disabled, bool, 0);
111 MODULE_PARM_DESC(no_hpsdc, "Do not enable HP SDC driver.");
112
113 static hp_i8042_sdc     hp_sdc; /* All driver state is kept in here. */
114
115 /*************** primitives for use in any context *********************/
116 static inline uint8_t hp_sdc_status_in8(void)
117 {
118         uint8_t status;
119         unsigned long flags;
120
121         write_lock_irqsave(&hp_sdc.ibf_lock, flags);
122         status = sdc_readb(hp_sdc.status_io);
123         if (!(status & HP_SDC_STATUS_IBF))
124                 hp_sdc.ibf = 0;
125         write_unlock_irqrestore(&hp_sdc.ibf_lock, flags);
126
127         return status;
128 }
129
130 static inline uint8_t hp_sdc_data_in8(void)
131 {
132         return sdc_readb(hp_sdc.data_io);
133 }
134
135 static inline void hp_sdc_status_out8(uint8_t val)
136 {
137         unsigned long flags;
138
139         write_lock_irqsave(&hp_sdc.ibf_lock, flags);
140         hp_sdc.ibf = 1;
141         if ((val & 0xf0) == 0xe0)
142                 hp_sdc.wi = 0xff;
143         sdc_writeb(val, hp_sdc.status_io);
144         write_unlock_irqrestore(&hp_sdc.ibf_lock, flags);
145 }
146
147 static inline void hp_sdc_data_out8(uint8_t val)
148 {
149         unsigned long flags;
150
151         write_lock_irqsave(&hp_sdc.ibf_lock, flags);
152         hp_sdc.ibf = 1;
153         sdc_writeb(val, hp_sdc.data_io);
154         write_unlock_irqrestore(&hp_sdc.ibf_lock, flags);
155 }
156
157 /*      Care must be taken to only invoke hp_sdc_spin_ibf when
158  *      absolutely needed, or in rarely invoked subroutines.
159  *      Not only does it waste CPU cycles, it also wastes bus cycles.
160  */
161 static inline void hp_sdc_spin_ibf(void)
162 {
163         unsigned long flags;
164         rwlock_t *lock;
165
166         lock = &hp_sdc.ibf_lock;
167
168         read_lock_irqsave(lock, flags);
169         if (!hp_sdc.ibf) {
170                 read_unlock_irqrestore(lock, flags);
171                 return;
172         }
173         read_unlock(lock);
174         write_lock(lock);
175         while (sdc_readb(hp_sdc.status_io) & HP_SDC_STATUS_IBF)
176                 { }
177         hp_sdc.ibf = 0;
178         write_unlock_irqrestore(lock, flags);
179 }
180
181
182 /************************ Interrupt context functions ************************/
183 static void hp_sdc_take(int irq, void *dev_id, uint8_t status, uint8_t data)
184 {
185         hp_sdc_transaction *curr;
186
187         read_lock(&hp_sdc.rtq_lock);
188         if (hp_sdc.rcurr < 0) {
189                 read_unlock(&hp_sdc.rtq_lock);
190                 return;
191         }
192         curr = hp_sdc.tq[hp_sdc.rcurr];
193         read_unlock(&hp_sdc.rtq_lock);
194
195         curr->seq[curr->idx++] = status;
196         curr->seq[curr->idx++] = data;
197         hp_sdc.rqty -= 2;
198         do_gettimeofday(&hp_sdc.rtv);
199
200         if (hp_sdc.rqty <= 0) {
201                 /* All data has been gathered. */
202                 if (curr->seq[curr->actidx] & HP_SDC_ACT_SEMAPHORE)
203                         if (curr->act.semaphore)
204                                 up(curr->act.semaphore);
205
206                 if (curr->seq[curr->actidx] & HP_SDC_ACT_CALLBACK)
207                         if (curr->act.irqhook)
208                                 curr->act.irqhook(irq, dev_id, status, data);
209
210                 curr->actidx = curr->idx;
211                 curr->idx++;
212                 /* Return control of this transaction */
213                 write_lock(&hp_sdc.rtq_lock);
214                 hp_sdc.rcurr = -1;
215                 hp_sdc.rqty = 0;
216                 write_unlock(&hp_sdc.rtq_lock);
217                 tasklet_schedule(&hp_sdc.task);
218         }
219 }
220
221 static irqreturn_t hp_sdc_isr(int irq, void *dev_id)
222 {
223         uint8_t status, data;
224
225         status = hp_sdc_status_in8();
226         /* Read data unconditionally to advance i8042. */
227         data =   hp_sdc_data_in8();
228
229         /* For now we are ignoring these until we get the SDC to behave. */
230         if (((status & 0xf1) == 0x51) && data == 0x82)
231                 return IRQ_HANDLED;
232
233         switch (status & HP_SDC_STATUS_IRQMASK) {
234         case 0: /* This case is not documented. */
235                 break;
236
237         case HP_SDC_STATUS_USERTIMER:
238         case HP_SDC_STATUS_PERIODIC:
239         case HP_SDC_STATUS_TIMER:
240                 read_lock(&hp_sdc.hook_lock);
241                 if (hp_sdc.timer != NULL)
242                         hp_sdc.timer(irq, dev_id, status, data);
243                 read_unlock(&hp_sdc.hook_lock);
244                 break;
245
246         case HP_SDC_STATUS_REG:
247                 hp_sdc_take(irq, dev_id, status, data);
248                 break;
249
250         case HP_SDC_STATUS_HILCMD:
251         case HP_SDC_STATUS_HILDATA:
252                 read_lock(&hp_sdc.hook_lock);
253                 if (hp_sdc.hil != NULL)
254                         hp_sdc.hil(irq, dev_id, status, data);
255                 read_unlock(&hp_sdc.hook_lock);
256                 break;
257
258         case HP_SDC_STATUS_PUP:
259                 read_lock(&hp_sdc.hook_lock);
260                 if (hp_sdc.pup != NULL)
261                         hp_sdc.pup(irq, dev_id, status, data);
262                 else
263                         printk(KERN_INFO PREFIX "HP SDC reports successful PUP.\n");
264                 read_unlock(&hp_sdc.hook_lock);
265                 break;
266
267         default:
268                 read_lock(&hp_sdc.hook_lock);
269                 if (hp_sdc.cooked != NULL)
270                         hp_sdc.cooked(irq, dev_id, status, data);
271                 read_unlock(&hp_sdc.hook_lock);
272                 break;
273         }
274
275         return IRQ_HANDLED;
276 }
277
278
279 static irqreturn_t hp_sdc_nmisr(int irq, void *dev_id)
280 {
281         int status;
282
283         status = hp_sdc_status_in8();
284         printk(KERN_WARNING PREFIX "NMI !\n");
285
286 #if 0
287         if (status & HP_SDC_NMISTATUS_FHS) {
288                 read_lock(&hp_sdc.hook_lock);
289                 if (hp_sdc.timer != NULL)
290                         hp_sdc.timer(irq, dev_id, status, 0);
291                 read_unlock(&hp_sdc.hook_lock);
292         } else {
293                 /* TODO: pass this on to the HIL handler, or do SAK here? */
294                 printk(KERN_WARNING PREFIX "HIL NMI\n");
295         }
296 #endif
297
298         return IRQ_HANDLED;
299 }
300
301
302 /***************** Kernel (tasklet) context functions ****************/
303
304 unsigned long hp_sdc_put(void);
305
306 static void hp_sdc_tasklet(unsigned long foo)
307 {
308         write_lock_irq(&hp_sdc.rtq_lock);
309
310         if (hp_sdc.rcurr >= 0) {
311                 struct timeval tv;
312
313                 do_gettimeofday(&tv);
314                 if (tv.tv_sec > hp_sdc.rtv.tv_sec)
315                         tv.tv_usec += USEC_PER_SEC;
316
317                 if (tv.tv_usec - hp_sdc.rtv.tv_usec > HP_SDC_MAX_REG_DELAY) {
318                         hp_sdc_transaction *curr;
319                         uint8_t tmp;
320
321                         curr = hp_sdc.tq[hp_sdc.rcurr];
322                         /* If this turns out to be a normal failure mode
323                          * we'll need to figure out a way to communicate
324                          * it back to the application. and be less verbose.
325                          */
326                         printk(KERN_WARNING PREFIX "read timeout (%ius)!\n",
327                                tv.tv_usec - hp_sdc.rtv.tv_usec);
328                         curr->idx += hp_sdc.rqty;
329                         hp_sdc.rqty = 0;
330                         tmp = curr->seq[curr->actidx];
331                         curr->seq[curr->actidx] |= HP_SDC_ACT_DEAD;
332                         if (tmp & HP_SDC_ACT_SEMAPHORE)
333                                 if (curr->act.semaphore)
334                                         up(curr->act.semaphore);
335
336                         if (tmp & HP_SDC_ACT_CALLBACK) {
337                                 /* Note this means that irqhooks may be called
338                                  * in tasklet/bh context.
339                                  */
340                                 if (curr->act.irqhook)
341                                         curr->act.irqhook(0, NULL, 0, 0);
342                         }
343
344                         curr->actidx = curr->idx;
345                         curr->idx++;
346                         hp_sdc.rcurr = -1;
347                 }
348         }
349         write_unlock_irq(&hp_sdc.rtq_lock);
350         hp_sdc_put();
351 }
352
353 unsigned long hp_sdc_put(void)
354 {
355         hp_sdc_transaction *curr;
356         uint8_t act;
357         int idx, curridx;
358
359         int limit = 0;
360
361         write_lock(&hp_sdc.lock);
362
363         /* If i8042 buffers are full, we cannot do anything that
364            requires output, so we skip to the administrativa. */
365         if (hp_sdc.ibf) {
366                 hp_sdc_status_in8();
367                 if (hp_sdc.ibf)
368                         goto finish;
369         }
370
371  anew:
372         /* See if we are in the middle of a sequence. */
373         if (hp_sdc.wcurr < 0)
374                 hp_sdc.wcurr = 0;
375         read_lock_irq(&hp_sdc.rtq_lock);
376         if (hp_sdc.rcurr == hp_sdc.wcurr)
377                 hp_sdc.wcurr++;
378         read_unlock_irq(&hp_sdc.rtq_lock);
379         if (hp_sdc.wcurr >= HP_SDC_QUEUE_LEN)
380                 hp_sdc.wcurr = 0;
381         curridx = hp_sdc.wcurr;
382
383         if (hp_sdc.tq[curridx] != NULL)
384                 goto start;
385
386         while (++curridx != hp_sdc.wcurr) {
387                 if (curridx >= HP_SDC_QUEUE_LEN) {
388                         curridx = -1; /* Wrap to top */
389                         continue;
390                 }
391                 read_lock_irq(&hp_sdc.rtq_lock);
392                 if (hp_sdc.rcurr == curridx) {
393                         read_unlock_irq(&hp_sdc.rtq_lock);
394                         continue;
395                 }
396                 read_unlock_irq(&hp_sdc.rtq_lock);
397                 if (hp_sdc.tq[curridx] != NULL)
398                         break; /* Found one. */
399         }
400         if (curridx == hp_sdc.wcurr) { /* There's nothing queued to do. */
401                 curridx = -1;
402         }
403         hp_sdc.wcurr = curridx;
404
405  start:
406
407         /* Check to see if the interrupt mask needs to be set. */
408         if (hp_sdc.set_im) {
409                 hp_sdc_status_out8(hp_sdc.im | HP_SDC_CMD_SET_IM);
410                 hp_sdc.set_im = 0;
411                 goto finish;
412         }
413
414         if (hp_sdc.wcurr == -1)
415                 goto done;
416
417         curr = hp_sdc.tq[curridx];
418         idx = curr->actidx;
419
420         if (curr->actidx >= curr->endidx) {
421                 hp_sdc.tq[curridx] = NULL;
422                 /* Interleave outbound data between the transactions. */
423                 hp_sdc.wcurr++;
424                 if (hp_sdc.wcurr >= HP_SDC_QUEUE_LEN)
425                         hp_sdc.wcurr = 0;
426                 goto finish;
427         }
428
429         act = curr->seq[idx];
430         idx++;
431
432         if (curr->idx >= curr->endidx) {
433                 if (act & HP_SDC_ACT_DEALLOC)
434                         kfree(curr);
435                 hp_sdc.tq[curridx] = NULL;
436                 /* Interleave outbound data between the transactions. */
437                 hp_sdc.wcurr++;
438                 if (hp_sdc.wcurr >= HP_SDC_QUEUE_LEN)
439                         hp_sdc.wcurr = 0;
440                 goto finish;
441         }
442
443         while (act & HP_SDC_ACT_PRECMD) {
444                 if (curr->idx != idx) {
445                         idx++;
446                         act &= ~HP_SDC_ACT_PRECMD;
447                         break;
448                 }
449                 hp_sdc_status_out8(curr->seq[idx]);
450                 curr->idx++;
451                 /* act finished? */
452                 if ((act & HP_SDC_ACT_DURING) == HP_SDC_ACT_PRECMD)
453                         goto actdone;
454                 /* skip quantity field if data-out sequence follows. */
455                 if (act & HP_SDC_ACT_DATAOUT)
456                         curr->idx++;
457                 goto finish;
458         }
459         if (act & HP_SDC_ACT_DATAOUT) {
460                 int qty;
461
462                 qty = curr->seq[idx];
463                 idx++;
464                 if (curr->idx - idx < qty) {
465                         hp_sdc_data_out8(curr->seq[curr->idx]);
466                         curr->idx++;
467                         /* act finished? */
468                         if (curr->idx - idx >= qty &&
469                             (act & HP_SDC_ACT_DURING) == HP_SDC_ACT_DATAOUT)
470                                 goto actdone;
471                         goto finish;
472                 }
473                 idx += qty;
474                 act &= ~HP_SDC_ACT_DATAOUT;
475         } else
476             while (act & HP_SDC_ACT_DATAREG) {
477                 int mask;
478                 uint8_t w7[4];
479
480                 mask = curr->seq[idx];
481                 if (idx != curr->idx) {
482                         idx++;
483                         idx += !!(mask & 1);
484                         idx += !!(mask & 2);
485                         idx += !!(mask & 4);
486                         idx += !!(mask & 8);
487                         act &= ~HP_SDC_ACT_DATAREG;
488                         break;
489                 }
490
491                 w7[0] = (mask & 1) ? curr->seq[++idx] : hp_sdc.r7[0];
492                 w7[1] = (mask & 2) ? curr->seq[++idx] : hp_sdc.r7[1];
493                 w7[2] = (mask & 4) ? curr->seq[++idx] : hp_sdc.r7[2];
494                 w7[3] = (mask & 8) ? curr->seq[++idx] : hp_sdc.r7[3];
495
496                 if (hp_sdc.wi > 0x73 || hp_sdc.wi < 0x70 ||
497                     w7[hp_sdc.wi - 0x70] == hp_sdc.r7[hp_sdc.wi - 0x70]) {
498                         int i = 0;
499
500                         /* Need to point the write index register */
501                         while (i < 4 && w7[i] == hp_sdc.r7[i])
502                                 i++;
503
504                         if (i < 4) {
505                                 hp_sdc_status_out8(HP_SDC_CMD_SET_D0 + i);
506                                 hp_sdc.wi = 0x70 + i;
507                                 goto finish;
508                         }
509
510                         idx++;
511                         if ((act & HP_SDC_ACT_DURING) == HP_SDC_ACT_DATAREG)
512                                 goto actdone;
513
514                         curr->idx = idx;
515                         act &= ~HP_SDC_ACT_DATAREG;
516                         break;
517                 }
518
519                 hp_sdc_data_out8(w7[hp_sdc.wi - 0x70]);
520                 hp_sdc.r7[hp_sdc.wi - 0x70] = w7[hp_sdc.wi - 0x70];
521                 hp_sdc.wi++; /* write index register autoincrements */
522                 {
523                         int i = 0;
524
525                         while ((i < 4) && w7[i] == hp_sdc.r7[i])
526                                 i++;
527                         if (i >= 4) {
528                                 curr->idx = idx + 1;
529                                 if ((act & HP_SDC_ACT_DURING) ==
530                                     HP_SDC_ACT_DATAREG)
531                                         goto actdone;
532                         }
533                 }
534                 goto finish;
535         }
536         /* We don't go any further in the command if there is a pending read,
537            because we don't want interleaved results. */
538         read_lock_irq(&hp_sdc.rtq_lock);
539         if (hp_sdc.rcurr >= 0) {
540                 read_unlock_irq(&hp_sdc.rtq_lock);
541                 goto finish;
542         }
543         read_unlock_irq(&hp_sdc.rtq_lock);
544
545
546         if (act & HP_SDC_ACT_POSTCMD) {
547                 uint8_t postcmd;
548
549                 /* curr->idx should == idx at this point. */
550                 postcmd = curr->seq[idx];
551                 curr->idx++;
552                 if (act & HP_SDC_ACT_DATAIN) {
553
554                         /* Start a new read */
555                         hp_sdc.rqty = curr->seq[curr->idx];
556                         do_gettimeofday(&hp_sdc.rtv);
557                         curr->idx++;
558                         /* Still need to lock here in case of spurious irq. */
559                         write_lock_irq(&hp_sdc.rtq_lock);
560                         hp_sdc.rcurr = curridx;
561                         write_unlock_irq(&hp_sdc.rtq_lock);
562                         hp_sdc_status_out8(postcmd);
563                         goto finish;
564                 }
565                 hp_sdc_status_out8(postcmd);
566                 goto actdone;
567         }
568
569  actdone:
570         if (act & HP_SDC_ACT_SEMAPHORE)
571                 up(curr->act.semaphore);
572         else if (act & HP_SDC_ACT_CALLBACK)
573                 curr->act.irqhook(0,NULL,0,0);
574
575         if (curr->idx >= curr->endidx) { /* This transaction is over. */
576                 if (act & HP_SDC_ACT_DEALLOC)
577                         kfree(curr);
578                 hp_sdc.tq[curridx] = NULL;
579         } else {
580                 curr->actidx = idx + 1;
581                 curr->idx = idx + 2;
582         }
583         /* Interleave outbound data between the transactions. */
584         hp_sdc.wcurr++;
585         if (hp_sdc.wcurr >= HP_SDC_QUEUE_LEN)
586                 hp_sdc.wcurr = 0;
587
588  finish:
589         /* If by some quirk IBF has cleared and our ISR has run to
590            see that that has happened, do it all again. */
591         if (!hp_sdc.ibf && limit++ < 20)
592                 goto anew;
593
594  done:
595         if (hp_sdc.wcurr >= 0)
596                 tasklet_schedule(&hp_sdc.task);
597         write_unlock(&hp_sdc.lock);
598
599         return 0;
600 }
601
602 /******* Functions called in either user or kernel context ****/
603 int __hp_sdc_enqueue_transaction(hp_sdc_transaction *this)
604 {
605         int i;
606
607         if (this == NULL) {
608                 BUG();
609                 return -EINVAL;
610         }
611
612         /* Can't have same transaction on queue twice */
613         for (i = 0; i < HP_SDC_QUEUE_LEN; i++)
614                 if (hp_sdc.tq[i] == this)
615                         goto fail;
616
617         this->actidx = 0;
618         this->idx = 1;
619
620         /* Search for empty slot */
621         for (i = 0; i < HP_SDC_QUEUE_LEN; i++)
622                 if (hp_sdc.tq[i] == NULL) {
623                         hp_sdc.tq[i] = this;
624                         tasklet_schedule(&hp_sdc.task);
625                         return 0;
626                 }
627
628         printk(KERN_WARNING PREFIX "No free slot to add transaction.\n");
629         return -EBUSY;
630
631  fail:
632         printk(KERN_WARNING PREFIX "Transaction add failed: transaction already queued?\n");
633         return -EINVAL;
634 }
635
636 int hp_sdc_enqueue_transaction(hp_sdc_transaction *this) {
637         unsigned long flags;
638         int ret;
639
640         write_lock_irqsave(&hp_sdc.lock, flags);
641         ret = __hp_sdc_enqueue_transaction(this);
642         write_unlock_irqrestore(&hp_sdc.lock,flags);
643
644         return ret;
645 }
646
647 int hp_sdc_dequeue_transaction(hp_sdc_transaction *this)
648 {
649         unsigned long flags;
650         int i;
651
652         write_lock_irqsave(&hp_sdc.lock, flags);
653
654         /* TODO: don't remove it if it's not done. */
655
656         for (i = 0; i < HP_SDC_QUEUE_LEN; i++)
657                 if (hp_sdc.tq[i] == this)
658                         hp_sdc.tq[i] = NULL;
659
660         write_unlock_irqrestore(&hp_sdc.lock, flags);
661         return 0;
662 }
663
664
665
666 /********************** User context functions **************************/
667 int hp_sdc_request_timer_irq(hp_sdc_irqhook *callback)
668 {
669         if (callback == NULL || hp_sdc.dev == NULL)
670                 return -EINVAL;
671
672         write_lock_irq(&hp_sdc.hook_lock);
673         if (hp_sdc.timer != NULL) {
674                 write_unlock_irq(&hp_sdc.hook_lock);
675                 return -EBUSY;
676         }
677
678         hp_sdc.timer = callback;
679         /* Enable interrupts from the timers */
680         hp_sdc.im &= ~HP_SDC_IM_FH;
681         hp_sdc.im &= ~HP_SDC_IM_PT;
682         hp_sdc.im &= ~HP_SDC_IM_TIMERS;
683         hp_sdc.set_im = 1;
684         write_unlock_irq(&hp_sdc.hook_lock);
685
686         tasklet_schedule(&hp_sdc.task);
687
688         return 0;
689 }
690
691 int hp_sdc_request_hil_irq(hp_sdc_irqhook *callback)
692 {
693         if (callback == NULL || hp_sdc.dev == NULL)
694                 return -EINVAL;
695
696         write_lock_irq(&hp_sdc.hook_lock);
697         if (hp_sdc.hil != NULL) {
698                 write_unlock_irq(&hp_sdc.hook_lock);
699                 return -EBUSY;
700         }
701
702         hp_sdc.hil = callback;
703         hp_sdc.im &= ~(HP_SDC_IM_HIL | HP_SDC_IM_RESET);
704         hp_sdc.set_im = 1;
705         write_unlock_irq(&hp_sdc.hook_lock);
706
707         tasklet_schedule(&hp_sdc.task);
708
709         return 0;
710 }
711
712 int hp_sdc_request_cooked_irq(hp_sdc_irqhook *callback)
713 {
714         if (callback == NULL || hp_sdc.dev == NULL)
715                 return -EINVAL;
716
717         write_lock_irq(&hp_sdc.hook_lock);
718         if (hp_sdc.cooked != NULL) {
719                 write_unlock_irq(&hp_sdc.hook_lock);
720                 return -EBUSY;
721         }
722
723         /* Enable interrupts from the HIL MLC */
724         hp_sdc.cooked = callback;
725         hp_sdc.im &= ~(HP_SDC_IM_HIL | HP_SDC_IM_RESET);
726         hp_sdc.set_im = 1;
727         write_unlock_irq(&hp_sdc.hook_lock);
728
729         tasklet_schedule(&hp_sdc.task);
730
731         return 0;
732 }
733
734 int hp_sdc_release_timer_irq(hp_sdc_irqhook *callback)
735 {
736         write_lock_irq(&hp_sdc.hook_lock);
737         if ((callback != hp_sdc.timer) ||
738             (hp_sdc.timer == NULL)) {
739                 write_unlock_irq(&hp_sdc.hook_lock);
740                 return -EINVAL;
741         }
742
743         /* Disable interrupts from the timers */
744         hp_sdc.timer = NULL;
745         hp_sdc.im |= HP_SDC_IM_TIMERS;
746         hp_sdc.im |= HP_SDC_IM_FH;
747         hp_sdc.im |= HP_SDC_IM_PT;
748         hp_sdc.set_im = 1;
749         write_unlock_irq(&hp_sdc.hook_lock);
750         tasklet_schedule(&hp_sdc.task);
751
752         return 0;
753 }
754
755 int hp_sdc_release_hil_irq(hp_sdc_irqhook *callback)
756 {
757         write_lock_irq(&hp_sdc.hook_lock);
758         if ((callback != hp_sdc.hil) ||
759             (hp_sdc.hil == NULL)) {
760                 write_unlock_irq(&hp_sdc.hook_lock);
761                 return -EINVAL;
762         }
763
764         hp_sdc.hil = NULL;
765         /* Disable interrupts from HIL only if there is no cooked driver. */
766         if(hp_sdc.cooked == NULL) {
767                 hp_sdc.im |= (HP_SDC_IM_HIL | HP_SDC_IM_RESET);
768                 hp_sdc.set_im = 1;
769         }
770         write_unlock_irq(&hp_sdc.hook_lock);
771         tasklet_schedule(&hp_sdc.task);
772
773         return 0;
774 }
775
776 int hp_sdc_release_cooked_irq(hp_sdc_irqhook *callback)
777 {
778         write_lock_irq(&hp_sdc.hook_lock);
779         if ((callback != hp_sdc.cooked) ||
780             (hp_sdc.cooked == NULL)) {
781                 write_unlock_irq(&hp_sdc.hook_lock);
782                 return -EINVAL;
783         }
784
785         hp_sdc.cooked = NULL;
786         /* Disable interrupts from HIL only if there is no raw HIL driver. */
787         if(hp_sdc.hil == NULL) {
788                 hp_sdc.im |= (HP_SDC_IM_HIL | HP_SDC_IM_RESET);
789                 hp_sdc.set_im = 1;
790         }
791         write_unlock_irq(&hp_sdc.hook_lock);
792         tasklet_schedule(&hp_sdc.task);
793
794         return 0;
795 }
796
797 /************************* Keepalive timer task *********************/
798
799 void hp_sdc_kicker (unsigned long data)
800 {
801         tasklet_schedule(&hp_sdc.task);
802         /* Re-insert the periodic task. */
803         mod_timer(&hp_sdc.kicker, jiffies + HZ);
804 }
805
806 /************************** Module Initialization ***************************/
807
808 #if defined(__hppa__)
809
810 static const struct parisc_device_id hp_sdc_tbl[] = {
811         {
812                 .hw_type =      HPHW_FIO,
813                 .hversion_rev = HVERSION_REV_ANY_ID,
814                 .hversion =     HVERSION_ANY_ID,
815                 .sversion =     0x73,
816          },
817         { 0, }
818 };
819
820 MODULE_DEVICE_TABLE(parisc, hp_sdc_tbl);
821
822 static int __init hp_sdc_init_hppa(struct parisc_device *d);
823
824 static struct parisc_driver hp_sdc_driver = {
825         .name =         "hp_sdc",
826         .id_table =     hp_sdc_tbl,
827         .probe =        hp_sdc_init_hppa,
828 };
829
830 #endif /* __hppa__ */
831
832 static int __init hp_sdc_init(void)
833 {
834         char *errstr;
835         hp_sdc_transaction t_sync;
836         uint8_t ts_sync[6];
837         struct semaphore s_sync;
838
839         rwlock_init(&hp_sdc.lock);
840         rwlock_init(&hp_sdc.ibf_lock);
841         rwlock_init(&hp_sdc.rtq_lock);
842         rwlock_init(&hp_sdc.hook_lock);
843
844         hp_sdc.timer            = NULL;
845         hp_sdc.hil              = NULL;
846         hp_sdc.pup              = NULL;
847         hp_sdc.cooked           = NULL;
848         hp_sdc.im               = HP_SDC_IM_MASK;  /* Mask maskable irqs */
849         hp_sdc.set_im           = 1;
850         hp_sdc.wi               = 0xff;
851         hp_sdc.r7[0]            = 0xff;
852         hp_sdc.r7[1]            = 0xff;
853         hp_sdc.r7[2]            = 0xff;
854         hp_sdc.r7[3]            = 0xff;
855         hp_sdc.ibf              = 1;
856
857         memset(&hp_sdc.tq, 0, sizeof(hp_sdc.tq));
858
859         hp_sdc.wcurr            = -1;
860         hp_sdc.rcurr            = -1;
861         hp_sdc.rqty             = 0;
862
863         hp_sdc.dev_err = -ENODEV;
864
865         errstr = "IO not found for";
866         if (!hp_sdc.base_io)
867                 goto err0;
868
869         errstr = "IRQ not found for";
870         if (!hp_sdc.irq)
871                 goto err0;
872
873         hp_sdc.dev_err = -EBUSY;
874
875 #if defined(__hppa__)
876         errstr = "IO not available for";
877         if (request_region(hp_sdc.data_io, 2, hp_sdc_driver.name))
878                 goto err0;
879 #endif
880
881         errstr = "IRQ not available for";
882         if (request_irq(hp_sdc.irq, &hp_sdc_isr, IRQF_SHARED|IRQF_SAMPLE_RANDOM,
883                         "HP SDC", &hp_sdc))
884                 goto err1;
885
886         errstr = "NMI not available for";
887         if (request_irq(hp_sdc.nmi, &hp_sdc_nmisr, IRQF_SHARED,
888                         "HP SDC NMI", &hp_sdc))
889                 goto err2;
890
891         printk(KERN_INFO PREFIX "HP SDC at 0x%p, IRQ %d (NMI IRQ %d)\n",
892                (void *)hp_sdc.base_io, hp_sdc.irq, hp_sdc.nmi);
893
894         hp_sdc_status_in8();
895         hp_sdc_data_in8();
896
897         tasklet_init(&hp_sdc.task, hp_sdc_tasklet, 0);
898
899         /* Sync the output buffer registers, thus scheduling hp_sdc_tasklet. */
900         t_sync.actidx   = 0;
901         t_sync.idx      = 1;
902         t_sync.endidx   = 6;
903         t_sync.seq      = ts_sync;
904         ts_sync[0]      = HP_SDC_ACT_DATAREG | HP_SDC_ACT_SEMAPHORE;
905         ts_sync[1]      = 0x0f;
906         ts_sync[2] = ts_sync[3] = ts_sync[4] = ts_sync[5] = 0;
907         t_sync.act.semaphore = &s_sync;
908         init_MUTEX_LOCKED(&s_sync);
909         hp_sdc_enqueue_transaction(&t_sync);
910         down(&s_sync); /* Wait for t_sync to complete */
911
912         /* Create the keepalive task */
913         init_timer(&hp_sdc.kicker);
914         hp_sdc.kicker.expires = jiffies + HZ;
915         hp_sdc.kicker.function = &hp_sdc_kicker;
916         add_timer(&hp_sdc.kicker);
917
918         hp_sdc.dev_err = 0;
919         return 0;
920  err2:
921         free_irq(hp_sdc.irq, &hp_sdc);
922  err1:
923         release_region(hp_sdc.data_io, 2);
924  err0:
925         printk(KERN_WARNING PREFIX ": %s SDC IO=0x%p IRQ=0x%x NMI=0x%x\n",
926                 errstr, (void *)hp_sdc.base_io, hp_sdc.irq, hp_sdc.nmi);
927         hp_sdc.dev = NULL;
928
929         return hp_sdc.dev_err;
930 }
931
932 #if defined(__hppa__)
933
934 static int __init hp_sdc_init_hppa(struct parisc_device *d)
935 {
936         if (!d)
937                 return 1;
938         if (hp_sdc.dev != NULL)
939                 return 1;       /* We only expect one SDC */
940
941         hp_sdc.dev              = d;
942         hp_sdc.irq              = d->irq;
943         hp_sdc.nmi              = d->aux_irq;
944         hp_sdc.base_io          = d->hpa.start;
945         hp_sdc.data_io          = d->hpa.start + 0x800;
946         hp_sdc.status_io        = d->hpa.start + 0x801;
947
948         return hp_sdc_init();
949 }
950
951 #endif /* __hppa__ */
952
953 static void hp_sdc_exit(void)
954 {
955         write_lock_irq(&hp_sdc.lock);
956
957         /* Turn off all maskable "sub-function" irq's. */
958         hp_sdc_spin_ibf();
959         sdc_writeb(HP_SDC_CMD_SET_IM | HP_SDC_IM_MASK, hp_sdc.status_io);
960
961         /* Wait until we know this has been processed by the i8042 */
962         hp_sdc_spin_ibf();
963
964         free_irq(hp_sdc.nmi, &hp_sdc);
965         free_irq(hp_sdc.irq, &hp_sdc);
966         write_unlock_irq(&hp_sdc.lock);
967
968         del_timer(&hp_sdc.kicker);
969
970         tasklet_kill(&hp_sdc.task);
971
972 #if defined(__hppa__)
973         if (unregister_parisc_driver(&hp_sdc_driver))
974                 printk(KERN_WARNING PREFIX "Error unregistering HP SDC");
975 #endif
976 }
977
978 static int __init hp_sdc_register(void)
979 {
980         hp_sdc_transaction tq_init;
981         uint8_t tq_init_seq[5];
982         struct semaphore tq_init_sem;
983 #if defined(__mc68000__)
984         mm_segment_t fs;
985         unsigned char i;
986 #endif
987
988         if (hp_sdc_disabled) {
989                 printk(KERN_WARNING PREFIX "HP SDC driver disabled by no_hpsdc=1.\n");
990                 return -ENODEV;
991         }
992
993         hp_sdc.dev = NULL;
994         hp_sdc.dev_err = 0;
995 #if defined(__hppa__)
996         if (register_parisc_driver(&hp_sdc_driver)) {
997                 printk(KERN_WARNING PREFIX "Error registering SDC with system bus tree.\n");
998                 return -ENODEV;
999         }
1000 #elif defined(__mc68000__)
1001         if (!MACH_IS_HP300)
1002             return -ENODEV;
1003
1004         hp_sdc.irq       = 1;
1005         hp_sdc.nmi       = 7;
1006         hp_sdc.base_io   = (unsigned long) 0xf0428000;
1007         hp_sdc.data_io   = (unsigned long) hp_sdc.base_io + 1;
1008         hp_sdc.status_io = (unsigned long) hp_sdc.base_io + 3;
1009         fs = get_fs();
1010         set_fs(KERNEL_DS);
1011         if (!get_user(i, (unsigned char *)hp_sdc.data_io))
1012                 hp_sdc.dev = (void *)1;
1013         set_fs(fs);
1014         hp_sdc.dev_err   = hp_sdc_init();
1015 #endif
1016         if (hp_sdc.dev == NULL) {
1017                 printk(KERN_WARNING PREFIX "No SDC found.\n");
1018                 return hp_sdc.dev_err;
1019         }
1020
1021         init_MUTEX_LOCKED(&tq_init_sem);
1022
1023         tq_init.actidx          = 0;
1024         tq_init.idx             = 1;
1025         tq_init.endidx          = 5;
1026         tq_init.seq             = tq_init_seq;
1027         tq_init.act.semaphore   = &tq_init_sem;
1028
1029         tq_init_seq[0] =
1030                 HP_SDC_ACT_POSTCMD | HP_SDC_ACT_DATAIN | HP_SDC_ACT_SEMAPHORE;
1031         tq_init_seq[1] = HP_SDC_CMD_READ_KCC;
1032         tq_init_seq[2] = 1;
1033         tq_init_seq[3] = 0;
1034         tq_init_seq[4] = 0;
1035
1036         hp_sdc_enqueue_transaction(&tq_init);
1037
1038         down(&tq_init_sem);
1039         up(&tq_init_sem);
1040
1041         if ((tq_init_seq[0] & HP_SDC_ACT_DEAD) == HP_SDC_ACT_DEAD) {
1042                 printk(KERN_WARNING PREFIX "Error reading config byte.\n");
1043                 hp_sdc_exit();
1044                 return -ENODEV;
1045         }
1046         hp_sdc.r11 = tq_init_seq[4];
1047         if (hp_sdc.r11 & HP_SDC_CFG_NEW) {
1048                 const char *str;
1049                 printk(KERN_INFO PREFIX "New style SDC\n");
1050                 tq_init_seq[1] = HP_SDC_CMD_READ_XTD;
1051                 tq_init.actidx          = 0;
1052                 tq_init.idx             = 1;
1053                 down(&tq_init_sem);
1054                 hp_sdc_enqueue_transaction(&tq_init);
1055                 down(&tq_init_sem);
1056                 up(&tq_init_sem);
1057                 if ((tq_init_seq[0] & HP_SDC_ACT_DEAD) == HP_SDC_ACT_DEAD) {
1058                         printk(KERN_WARNING PREFIX "Error reading extended config byte.\n");
1059                         return -ENODEV;
1060                 }
1061                 hp_sdc.r7e = tq_init_seq[4];
1062                 HP_SDC_XTD_REV_STRINGS(hp_sdc.r7e & HP_SDC_XTD_REV, str)
1063                 printk(KERN_INFO PREFIX "Revision: %s\n", str);
1064                 if (hp_sdc.r7e & HP_SDC_XTD_BEEPER)
1065                         printk(KERN_INFO PREFIX "TI SN76494 beeper present\n");
1066                 if (hp_sdc.r7e & HP_SDC_XTD_BBRTC)
1067                         printk(KERN_INFO PREFIX "OKI MSM-58321 BBRTC present\n");
1068                 printk(KERN_INFO PREFIX "Spunking the self test register to force PUP "
1069                        "on next firmware reset.\n");
1070                 tq_init_seq[0] = HP_SDC_ACT_PRECMD |
1071                         HP_SDC_ACT_DATAOUT | HP_SDC_ACT_SEMAPHORE;
1072                 tq_init_seq[1] = HP_SDC_CMD_SET_STR;
1073                 tq_init_seq[2] = 1;
1074                 tq_init_seq[3] = 0;
1075                 tq_init.actidx          = 0;
1076                 tq_init.idx             = 1;
1077                 tq_init.endidx          = 4;
1078                 down(&tq_init_sem);
1079                 hp_sdc_enqueue_transaction(&tq_init);
1080                 down(&tq_init_sem);
1081                 up(&tq_init_sem);
1082         } else
1083                 printk(KERN_INFO PREFIX "Old style SDC (1820-%s).\n",
1084                        (hp_sdc.r11 & HP_SDC_CFG_REV) ? "3300" : "2564/3087");
1085
1086         return 0;
1087 }
1088
1089 module_init(hp_sdc_register);
1090 module_exit(hp_sdc_exit);
1091
1092 /* Timing notes:  These measurements taken on my 64MHz 7100-LC (715/64)
1093  *                                              cycles cycles-adj    time
1094  * between two consecutive mfctl(16)'s:              4        n/a    63ns
1095  * hp_sdc_spin_ibf when idle:                      119        115   1.7us
1096  * gsc_writeb status register:                      83         79   1.2us
1097  * IBF to clear after sending SET_IM:             6204       6006    93us
1098  * IBF to clear after sending LOAD_RT:            4467       4352    68us
1099  * IBF to clear after sending two LOAD_RTs:      18974      18859   295us
1100  * READ_T1, read status/data, IRQ, call handler: 35564        n/a   556us
1101  * cmd to ~IBF READ_T1 2nd time right after:   5158403        n/a    81ms
1102  * between IRQ received and ~IBF for above:    2578877        n/a    40ms
1103  *
1104  * Performance stats after a run of this module configuring HIL and
1105  * receiving a few mouse events:
1106  *
1107  * status in8  282508 cycles 7128 calls
1108  * status out8   8404 cycles  341 calls
1109  * data out8     1734 cycles   78 calls
1110  * isr         174324 cycles  617 calls (includes take)
1111  * take          1241 cycles    2 calls
1112  * put        1411504 cycles 6937 calls
1113  * task       1655209 cycles 6937 calls (includes put)
1114  *
1115  */