* added 0.99 linux version
[mascara-docs.git] / i386 / linux / linux-2.3.21 / arch / m68k / mac / adb-bus.c
blob850e9cb6f90c4deccc1bd90b81b2bc7bb6dd79e1
1 /*
2 * MACII ADB keyboard handler.
3 * Copyright (c) 1997 Alan Cox
5 * Derived from code
6 * Copyright (C) 1996 Paul Mackerras.
8 * MSch (9/97) Partial rewrite of interrupt handler to MacII style
9 * ADB handshake, based on:
10 * - Guide to Mac Hardware
11 * - Guido Koerber's session with a logic analyzer
13 * MSch (1/98) Integrated start of IIsi driver by Robert Thompson
16 #include <linux/types.h>
17 #include <linux/errno.h>
18 #include <linux/miscdevice.h>
19 #include <linux/kernel.h>
20 #include <linux/delay.h>
21 #include <linux/sched.h>
22 #include <linux/malloc.h>
23 #include <linux/mm.h>
24 #include "via6522.h"
26 #include <asm/uaccess.h>
27 #include <asm/io.h>
28 #include <asm/adb.h>
29 #include <asm/system.h>
30 #include <asm/segment.h>
31 #include <asm/setup.h>
32 #include <asm/macintosh.h>
33 #include <asm/macints.h>
36 #define MACII /* For now - will be a switch */
38 /* Bits in B data register: all active low */
39 #define TREQ 0x08 /* Transfer request (input) */
40 #define TACK 0x10 /* Transfer acknowledge (output) */
41 #define TIP 0x20 /* Transfer in progress (output) */
43 /* Bits in B data register: ADB transaction states MacII */
44 #define ST_MASK 0x30 /* mask for selecting ADB state bits */
45 /* ADB transaction states according to GMHW */
46 #define ST_CMD 0x00 /* ADB state: command byte */
47 #define ST_EVEN 0x10 /* ADB state: even data byte */
48 #define ST_ODD 0x20 /* ADB state: odd data byte */
49 #define ST_IDLE 0x30 /* ADB state: idle, nothing to send */
51 /* Bits in ACR */
52 #define SR_CTRL 0x1c /* Shift register control bits */
53 #ifdef USE_ORIG
54 #define SR_EXT 0x1c /* Shift on external clock */
55 #else
56 #define SR_EXT 0x0c /* Shift on external clock */
57 #endif
58 #define SR_OUT 0x10 /* Shift out if 1 */
60 /* Bits in IFR and IER */
61 #define IER_SET 0x80 /* set bits in IER */
62 #define IER_CLR 0 /* clear bits in IER */
63 #define SR_INT 0x04 /* Shift register full/empty */
64 #define SR_DATA 0x08 /* Shift register data */
65 #define SR_CLOCK 0x10 /* Shift register clock */
67 /* JRT */
68 #define ADB_DELAY 150
70 static struct adb_handler {
71 void (*handler)(unsigned char *, int, struct pt_regs *);
72 } adb_handler[16];
74 static enum adb_state {
75 idle,
76 sent_first_byte,
77 sending,
78 reading,
79 read_done,
80 awaiting_reply
81 } adb_state;
83 static struct adb_request *current_req;
84 static struct adb_request *last_req;
85 static unsigned char cuda_rbuf[16];
86 static unsigned char *reply_ptr;
87 static int reply_len;
88 static int reading_reply;
89 static int data_index;
90 static int first_byte;
91 static int prefix_len;
93 static int status = ST_IDLE|TREQ;
94 static int last_status;
96 static int driver_running = 0;
98 /*static int adb_delay;*/
99 int in_keybinit = 1;
101 static void adb_start(void);
102 extern void adb_interrupt(int irq, void *arg, struct pt_regs *regs);
103 extern void adb_cuda_interrupt(int irq, void *arg, struct pt_regs *regs);
104 extern void adb_clock_interrupt(int irq, void *arg, struct pt_regs *regs);
105 extern void adb_data_interrupt(int irq, void *arg, struct pt_regs *regs);
106 static void adb_input(unsigned char *buf, int nb, struct pt_regs *regs);
108 static void adb_hw_setup_IIsi(void);
109 static void adb_hw_setup_cuda(void);
112 * debug level 10 required for ADB logging (should be && debug_adb, ideally)
115 extern int console_loglevel;
118 * Misc. defines for testing - should go to header :-(
121 #define ADBDEBUG_STATUS (1)
122 #define ADBDEBUG_STATE (2)
123 #define ADBDEBUG_READ (4)
124 #define ADBDEBUG_WRITE (8)
125 #define ADBDEBUG_START (16)
126 #define ADBDEBUG_RETRY (32)
127 #define ADBDEBUG_POLL (64)
128 #define ADBDEBUG_INT (128)
129 #define ADBDEBUG_PROT (256)
130 #define ADBDEBUG_SRQ (512)
131 #define ADBDEBUG_REQUEST (1024)
132 #define ADBDEBUG_INPUT (2048)
133 #define ADBDEBUG_DEVICE (4096)
135 #define ADBDEBUG_IISI (8192)
138 /*#define DEBUG_ADB*/
140 #ifdef DEBUG_ADB
141 #define ADBDEBUG (ADBDEBUG_INPUT | ADBDEBUG_READ | ADBDEBUG_START | ADBDEBUG_WRITE | ADBDEBUG_SRQ | ADBDEBUG_REQUEST)
142 #else
143 #define ADBDEBUG (0)
144 #endif
146 #define TRY_CUDA
148 void adb_bus_init(void)
150 unsigned long flags;
151 unsigned char c, i;
153 save_flags(flags);
154 cli();
157 * Setup ADB
160 switch(macintosh_config->adb_type)
163 case MAC_ADB_II:
164 printk("adb: MacII style keyboard/mouse driver.\n");
165 /* Set the lines up. We want TREQ as input TACK|TIP as output */
166 via_write(via1, vDirB, ((via_read(via1,vDirB)|TACK|TIP)&~TREQ));
168 * Docs suggest TREQ should be output - that seems nuts
169 * BSD agrees here :-)
170 * Setup vPCR ??
173 #ifdef USE_ORIG
174 /* Lower the bus signals (MacII is active low it seems ???) */
175 via_write(via1, vBufB, via_read(via1, vBufB)&~TACK);
176 #else
177 /* Corresponding state: idle (clear state bits) */
178 via_write(via1, vBufB, (via_read(via1, vBufB)&~ST_MASK)|ST_IDLE);
179 last_status = (via_read(via1, vBufB)&~ST_MASK);
180 #endif
181 /* Shift register on input */
182 c=via_read(via1, vACR);
183 c&=~SR_CTRL; /* Clear shift register bits */
184 c|=SR_EXT; /* Shift on external clock; out or in? */
185 via_write(via1, vACR, c);
186 /* Wipe any pending data and int */
187 via_read(via1, vSR);
189 /* This is interrupts on enable SR for keyboard */
190 via_write(via1, vIER, IER_SET|SR_INT);
191 /* This clears the interrupt bit */
192 via_write(via1, vIFR, SR_INT);
195 * Ok we probably ;) have a ready to use adb bus. Its also
196 * hopefully idle (Im assuming the mac didnt leave a half
197 * complete transaction on booting us).
200 request_irq(IRQ_MAC_ADB, adb_interrupt, IRQ_FLG_LOCK,
201 "adb interrupt", adb_interrupt);
202 adb_state = idle;
203 break;
205 * Unsupported; but later code doesn't notice !!
207 case MAC_ADB_CUDA:
208 printk("adb: CUDA interface.\n");
209 #if 0
210 /* don't know what to set up here ... */
211 adb_state = idle;
212 /* Set the lines up. We want TREQ as input TACK|TIP as output */
213 via_write(via1, vDirB, ((via_read(via1,vDirB)|TACK|TIP)&~TREQ));
214 #endif
215 adb_hw_setup_cuda();
216 adb_state = idle;
217 request_irq(IRQ_MAC_ADB, adb_cuda_interrupt, IRQ_FLG_LOCK,
218 "adb CUDA interrupt", adb_cuda_interrupt);
219 break;
220 case MAC_ADB_IISI:
221 printk("adb: Using IIsi hardware.\n");
222 printk("\tDEBUG_JRT\n");
223 /* Set the lines up. We want TREQ as input TACK|TIP as output */
224 via_write(via1, vDirB, ((via_read(via1,vDirB)|TACK|TIP)&~TREQ));
227 * MSch: I'm pretty sure the setup is mildly wrong
228 * for the IIsi.
230 /* Initial state: idle (clear state bits) */
231 via_write(via1, vBufB, (via_read(via1, vBufB) & ~(TIP|TACK)) );
232 last_status = (via_read(via1, vBufB)&~ST_MASK);
233 /* Shift register on input */
234 c=via_read(via1, vACR);
235 c&=~SR_CTRL; /* Clear shift register bits */
236 c|=SR_EXT; /* Shift on external clock; out or in? */
237 via_write(via1, vACR, c);
238 /* Wipe any pending data and int */
239 via_read(via1, vSR);
241 /* This is interrupts on enable SR for keyboard */
242 via_write(via1, vIER, IER_SET|SR_INT);
243 /* This clears the interrupt bit */
244 via_write(via1, vIFR, SR_INT);
246 /* get those pesky clock ticks we missed while booting */
247 for ( i = 0; i < 60; i++) {
248 udelay(ADB_DELAY);
249 adb_hw_setup_IIsi();
250 udelay(ADB_DELAY);
251 if (via_read(via1, vBufB) & TREQ)
252 break;
254 if (i == 60)
255 printk("adb_IIsi: maybe bus jammed ??\n");
258 * Ok we probably ;) have a ready to use adb bus. Its also
260 request_irq(IRQ_MAC_ADB, adb_cuda_interrupt, IRQ_FLG_LOCK,
261 "adb interrupt", adb_cuda_interrupt);
262 adb_state = idle;
263 break;
264 default:
265 printk("adb: Unknown hardware interface.\n");
266 nosupp:
267 printk("adb: Interface unsupported.\n");
268 restore_flags(flags);
269 return;
273 * XXX: interrupt only registered if supported HW !!
274 * -> unsupported HW will just time out on keyb_init!
276 #if 0
277 request_irq(IRQ_MAC_ADB, adb_interrupt, IRQ_FLG_LOCK,
278 "adb interrupt", adb_interrupt);
279 #endif
280 #ifdef DEBUG_ADB_INTS
281 request_irq(IRQ_MAC_ADB_CL, adb_clock_interrupt, IRQ_FLG_LOCK,
282 "adb clock interrupt", adb_clock_interrupt);
283 request_irq(IRQ_MAC_ADB_SD, adb_data_interrupt, IRQ_FLG_LOCK,
284 "adb data interrupt", adb_data_interrupt);
285 #endif
287 printk("adb: init done.\n");
288 restore_flags(flags);
291 void adb_hw_setup_cuda(void)
293 int x;
294 unsigned long flags;
296 printk("CUDA: HW Setup:");
298 save_flags(flags);
299 cli();
301 if (console_loglevel == 10)
302 printk(" 1,");
304 /* Set the direction of the cuda signals, TIP+TACK are output TREQ is an input */
305 via_write( via1, vDirB, via_read( via1, vDirB ) | TIP | TACK );
306 via_write( via1, vDirB, via_read( via1, vDirB ) & ~TREQ );
308 if (console_loglevel == 10)
309 printk("2,");
311 /* Set the clock control. Set to shift data in by external clock CB1 */
312 via_write( via1, vACR, ( via_read(via1, vACR ) | SR_EXT ) & ~SR_OUT );
314 if (console_loglevel == 10)
315 printk("3,");
317 /* Clear any possible Cuda interrupt */
318 x = via_read( via1, vSR );
320 if (console_loglevel == 10)
321 printk("4,");
323 /* Terminate transaction and set idle state */
324 via_write( via1, vBufB, via_read( via1, vBufB ) | TIP | TACK );
326 if (console_loglevel == 10)
327 printk("5,");
329 /* Delay 4 mS for ADB reset to complete */
330 udelay(4000);
332 if (console_loglevel == 10)
333 printk("6,");
335 /* Clear pending interrupts... */
336 x = via_read( via1, vSR );
338 if (console_loglevel == 10)
339 printk("7,");
340 /* Issue a sync transaction, TACK asserted while TIP negated */
341 via_write( via1, vBufB, via_read( via1, vBufB ) & ~TACK );
343 if (console_loglevel == 10)
344 printk("8,");
346 /* Wait for the sync acknowledgement, Cuda to assert TREQ */
347 while( ( via_read( via1, vBufB ) & TREQ ) != 0 )
348 barrier();
350 if (console_loglevel == 10)
351 printk("9,");
353 /* Wait for the sync acknowledment interrupt */
354 while( ( via_read( via1, vIFR ) & SR_INT ) == 0 )
355 barrier();
357 if (console_loglevel == 10)
358 printk("10,");
360 /* Clear pending interrupts... */
361 x = via_read( via1, vSR );
363 if (console_loglevel == 10)
364 printk("11,");
366 /* Terminate the sync cycle by negating TACK */
367 via_write( via1, vBufB, via_read( via1, vBufB ) | TACK );
369 if (console_loglevel == 10)
370 printk("12,");
372 /* Wait for the sync termination acknowledgement, Cuda to negate TREQ */
373 while( ( via_read( via1, vBufB ) & TREQ ) == 0 )
374 barrier();
376 if (console_loglevel == 10)
377 printk("13,");
379 /* Wait for the sync termination acknowledment interrupt */
380 while( ( via_read( via1, vIFR ) & SR_INT ) == 0 )
381 barrier();
383 if (console_loglevel == 10)
384 printk("14,");
386 /* Terminate transaction and set idle state, TIP+TACK negate */
387 via_write( via1, vBufB, via_read( via1, vBufB ) | TIP );
389 if (console_loglevel == 10)
390 printk("15 !");
392 /* Clear pending interrupts... */
393 x = via_read( via1, vSR );
395 restore_flags(flags);
397 printk("\nCUDA: HW Setup done!\n");
400 void adb_hw_setup_IIsi(void)
402 int dummy;
403 long poll_timeout;
405 printk("adb_IIsi: cleanup!\n");
407 /* ??? */
408 udelay(ADB_DELAY);
410 /* disable SR int. */
411 via_write(via1, vIER, IER_CLR|SR_INT);
412 /* set SR to shift in */
413 via_write(via1, vACR, via_read(via1, vACR ) & ~SR_OUT);
415 /* this is required, especially on faster machines */
416 udelay(ADB_DELAY);
418 if (!(via_read(via1, vBufB) & TREQ)) { /* IRQ on */
419 /* start frame */
420 via_write(via1, vBufB,via_read(via1,vBufB) | TIP);
422 while (1) {
423 /* poll for ADB interrupt and watch for timeout */
424 /* if time out, keep going in hopes of not hanging the
425 * ADB chip - I think */
426 poll_timeout = ADB_DELAY * 5;
427 while ( !(via_read(via1, vIFR) & SR_INT)
428 && (poll_timeout-- > 0) )
429 dummy = via_read(via1, vBufB);
431 dummy = via_read(via1, vSR); /* reset interrupt flag */
433 /* perhaps put in a check here that ignores all data
434 * after the first ADB_MAX_MSG_LENGTH bytes ??? */
436 /* end of frame reached ?? */
437 if (via_read(via1, vBufB) & TREQ)
438 break;
440 /* set ACK */
441 via_write(via1,vBufB,via_read(via1, vBufB) | TACK);
442 /* delay */
443 udelay(ADB_DELAY);
444 /* clear ACK */
445 via_write(via1,vBufB,via_read(via1, vBufB) & ~TACK);
447 /* end frame */
448 via_write(via1, vBufB,via_read(via1,vBufB) & ~TIP);
449 /* probably don't need to delay this long */
450 udelay(ADB_DELAY);
452 /* re-enable SR int. */
453 via_write(via1, vIER, IER_SET|SR_INT);
456 #define WAIT_FOR(cond, what) \
457 do { \
458 for (x = 1000; !(cond); --x) { \
459 if (x == 0) { \
460 printk("Timeout waiting for " what); \
461 return 0; \
463 __delay(100*160); \
465 } while (0)
468 * Construct and send an adb request
469 * This function is the main entry point into the ADB driver from
470 * kernel code; it takes the request data supplied and populates the
471 * adb_request structure.
472 * In order to keep this interface independent from any assumption about
473 * the underlying ADB hardware, we take requests in CUDA format here,
474 * the ADB packet 'prefixed' with a packet type code.
475 * Non-CUDA hardware is confused by this, so we strip the packet type
476 * here depending on hardware type ...
478 int adb_request(struct adb_request *req, void (*done)(struct adb_request *),
479 int nbytes, ...)
481 va_list list;
482 int i, start;
484 va_start(list, nbytes);
487 * skip first byte if not CUDA
489 if (macintosh_config->adb_type == MAC_ADB_II) {
490 start = va_arg(list, int);
491 nbytes--;
493 req->nbytes = nbytes;
494 req->done = done;
495 #if (ADBDEBUG & ADBDEBUG_REQUEST)
496 if (console_loglevel == 10)
497 printk("adb_request, data bytes: ");
498 #endif
499 for (i = 0; i < nbytes; ++i) {
500 req->data[i] = va_arg(list, int);
501 #if (ADBDEBUG & ADBDEBUG_REQUEST)
502 if (console_loglevel == 10)
503 printk("%x ", req->data[i]);
504 #endif
506 #if (ADBDEBUG & ADBDEBUG_REQUEST)
507 if (console_loglevel == 10)
508 printk(" !\n");
509 #endif
510 va_end(list);
512 * XXX: This might be fatal if no reply is generated (i.e. Listen) !
513 * Currently, the interrupt handler 'fakes' a reply on non-TALK
514 * commands for this reason.
515 * Also, we need a CUDA_AUTOPOLL emulation here for non-CUDA
516 * Macs, and some mechanism to remember the last issued TALK
517 * request for resending it repeatedly on timeout!
519 req->reply_expected = 1;
520 return adb_send_request(req);
524 * Construct an adb request for later sending
525 * This function only populates the adb_request structure, without
526 * actually queueing it.
527 * Reason: Poll requests and Talk requests need to be handled in a way
528 * different from 'user' requests; no reply_expected is set and
529 * Poll requests need to be placed at the head of the request queue.
530 * Using adb_request results in implicit queueing at the tail of the
531 * request queue (duplicating the Poll) with reply_expected set.
532 * No adjustment of packet data is necessary, as this mechanisnm is not
533 * used by CUDA hardware (Autopoll used instead).
535 int adb_build_request(struct adb_request *req, void (*done)(struct adb_request *),
536 int nbytes, ...)
538 va_list list;
539 int i;
541 req->nbytes = nbytes;
542 req->done = done;
543 va_start(list, nbytes);
544 #if (ADBDEBUG & ADBDEBUG_REQUEST)
545 if (console_loglevel == 10)
546 printk("adb__build_request, data bytes: ");
547 #endif
549 * skip first byte if not CUDA ?
551 for (i = 0; i < nbytes; ++i) {
552 req->data[i] = va_arg(list, int);
553 #if (ADBDEBUG & ADBDEBUG_REQUEST)
554 if (console_loglevel == 10)
555 printk("%x ", req->data[i]);
556 #endif
558 #if (ADBDEBUG & ADBDEBUG_REQUEST)
559 if (console_loglevel == 10)
560 printk(" !\n");
561 #endif
562 va_end(list);
564 req->reply_expected = 0;
565 return 0;
569 * Send an ADB poll (Talk, tagged on the front of the request queue)
571 void adb_queue_poll(void)
573 static int pod=0;
574 static int in_poll=0;
575 static struct adb_request r;
576 unsigned long flags;
578 if(in_poll)
579 printk("Double poll!\n");
581 in_poll++;
582 pod++;
583 if(pod>7) /* 15 */
584 pod=0;
586 #if (ADBDEBUG & ADBDEBUG_POLL)
587 if (console_loglevel == 10)
588 printk("adb: Polling %d\n",pod);
589 #endif
591 if (macintosh_config->adb_type == MAC_ADB_II)
592 /* XXX: that's a TALK, register 0, MacII version */
593 adb_build_request(&r,NULL, 1, (pod<<4|0xC));
594 else
595 /* CUDA etc. version */
596 adb_build_request(&r,NULL, 2, 0, (pod<<4|0xC));
598 r.reply_expected=0;
599 r.done=NULL;
600 r.sent=0;
601 r.got_reply=0;
602 r.reply_len=0;
603 save_flags(flags);
604 cli();
605 /* Poll inserted at head of queue ... */
606 r.next=current_req;
607 current_req=&r;
608 restore_flags(flags);
609 adb_start();
610 in_poll--;
614 * Send an ADB retransmit (Talk, appended to the request queue)
616 void adb_retransmit(int device)
618 static int in_retransmit=0;
619 static struct adb_request rt;
620 unsigned long flags;
622 if(in_retransmit)
623 printk("Double retransmit!\n");
625 in_retransmit++;
627 #if (ADBDEBUG & ADBDEBUG_POLL)
628 if (console_loglevel == 10)
629 printk("adb: Sending retransmit: %d\n", device);
630 #endif
632 /* MacII version */
633 adb_build_request(&rt,NULL, 1, (device<<4|0xC));
635 rt.reply_expected = 0;
636 rt.done = NULL;
637 rt.sent = 0;
638 rt.got_reply = 0;
639 rt.reply_len = 0;
640 rt.next = NULL;
642 save_flags(flags);
643 cli();
645 /* Retransmit inserted at tail of queue ... */
647 if (current_req != NULL)
649 last_req->next = &rt;
650 last_req = &rt;
652 else
654 current_req = &rt;
655 last_req = &rt;
658 /* always restart driver (send_retransmit used in place of adb_start!)*/
660 if (adb_state == idle)
661 adb_start();
663 restore_flags(flags);
664 in_retransmit--;
668 * Queue an ADB request; start ADB transfer if necessary
670 int adb_send_request(struct adb_request *req)
672 unsigned long flags;
674 req->next = 0;
675 req->sent = 0;
676 req->got_reply = 0;
677 req->reply_len = 0;
678 save_flags(flags);
679 cli();
681 if (current_req != NULL)
683 last_req->next = req;
684 last_req = req;
686 else
688 current_req = req;
689 last_req = req;
690 if (adb_state == idle)
691 adb_start();
694 restore_flags(flags);
695 return 0;
698 static int nclock, ndata;
700 static int need_poll = 0;
701 static int command_byte = 0;
702 static int last_reply = 0;
703 static int last_active = 0;
705 static struct adb_request *retry_req;
708 * Start sending ADB packet
710 static void adb_start(void)
712 unsigned long flags;
713 struct adb_request *req;
716 * We get here on three 'sane' conditions:
717 * 1) called from send_adb_request, if adb_state == idle
718 * 2) called from within adb_interrupt, if adb_state == idle
719 * (after receiving, or after sending a LISTEN)
720 * 3) called from within adb_interrupt, if adb_state == sending
721 * and no reply is expected (immediate next command).
722 * Maybe we get here on SRQ as well ??
725 /* get the packet to send */
726 req = current_req;
727 /* assert adb_state == idle */
728 if (adb_state != idle) {
729 printk("ADB: adb_start called while driver busy (%p %x %x)!\n",
730 req, adb_state, via_read(via1, vBufB)&(ST_MASK|TREQ));
731 return;
733 if (req == 0)
734 return;
735 save_flags(flags);
736 cli();
738 #if (ADBDEBUG & ADBDEBUG_START)
739 if (console_loglevel == 10)
740 printk("adb_start: request %p ", req);
741 #endif
743 nclock = 0;
744 ndata = 0;
747 * IRQ signaled ?? (means ADB controller wants to send, or might
748 * be end of packet if we were reading)
750 if ((via_read(via1, vBufB) & TREQ) == 0)
752 switch(macintosh_config->adb_type)
755 * FIXME - we need to restart this on a timer
756 * or a collision at boot hangs us.
757 * Never set adb_state to idle here, or adb_start
758 * won't be called again from send_request!
759 * (need to re-check other cases ...)
761 case MAC_ADB_CUDA:
762 /* printk("device busy - fail\n"); */
763 restore_flags(flags);
764 /* a byte is coming in from the CUDA */
765 return;
766 case MAC_ADB_IISI:
767 printk("adb_start: device busy - fail\n");
768 retry_req = req;
769 restore_flags(flags);
770 return;
771 case MAC_ADB_II:
773 * if the interrupt handler set the need_poll
774 * flag, it's hopefully a SRQ poll or re-Talk
775 * so we try to send here anyway
777 if (!need_poll) {
778 printk("device busy - retry %p state %d status %x!\n",
779 req, adb_state, via_read(via1, vBufB)&(ST_MASK|TREQ));
780 retry_req = req;
781 /* set ADB status here ? */
782 restore_flags(flags);
783 return;
784 } else {
785 #if (ADBDEBUG & ADBDEBUG_START)
786 if (console_loglevel == 10)
787 printk("device busy - polling; state %d status %x!\n",
788 adb_state, via_read(via1, vBufB)&(ST_MASK|TREQ));
789 #endif
790 need_poll = 0;
791 break;
796 #if 0
798 * Bus idle ?? Not sure about this one; SRQ might need ST_CMD here!
799 * OTOH: setting ST_CMD in the interrupt routine would make the
800 * ADB contoller shift in before this routine starts shifting out ...
802 if ((via_read(via1, vBufB)&ST_MASK) != ST_IDLE)
804 #if (ADBDEBUG & ADBDEBUG_STATE)
805 if (console_loglevel == 10)
806 printk("ADB bus not idle (%x), retry later!\n",
807 via_read(via1, vBufB)&(ST_MASK|TREQ));
808 #endif
809 retry_req = req;
810 restore_flags(flags);
811 return;
813 #endif
816 * Another retry pending? (sanity check)
818 if (retry_req) {
819 #if (ADBDEBUG & ADBDEBUG_RETRY)
820 if (console_loglevel == 10)
821 if (retry_req == req)
822 /* new requests are appended at tail of request queue */
823 printk("adb_start: retry %p pending ! \n", req);
824 else
825 /* poll requests are added to the head of queue */
826 printk("adb_start: retry %p pending, req %p (poll?) current! \n",
827 retry_req, req);
828 #endif
829 retry_req = NULL;
833 * Seems OK, go for it!
835 switch(macintosh_config->adb_type)
837 case MAC_ADB_CUDA:
838 /* store command byte (first byte is 'type' byte) */
839 command_byte = req->data[1];
840 /* set the shift register to shift out and send a byte */
841 via_write(via1, vACR, via_read(via1, vACR)|SR_OUT);
842 via_write(via1, vSR, req->data[0]);
843 via_write(via1, vBufB, via_read(via1, vBufB)&~TIP);
844 break;
845 case MAC_ADB_IISI:
846 /* store command byte (first byte is 'type' byte) */
847 command_byte = req->data[1];
848 /* set ADB state to 'active' */
849 via_write(via1, vBufB, via_read(via1, vBufB) | TIP);
850 /* switch ACK off (in case it was left on) */
851 via_write(via1, vBufB, via_read(via1, vBufB) & ~TACK);
852 /* set the shift register to shift out and send a byte */
853 via_write(via1, vACR, via_read(via1, vACR) | SR_OUT);
854 via_write(via1, vSR, req->data[0]);
855 /* signal 'byte ready' */
856 via_write(via1, vBufB, via_read(via1, vBufB) | TACK);
857 break;
858 case MAC_ADB_II:
859 /* store command byte */
860 command_byte = req->data[0];
861 /* Output mode */
862 via_write(via1, vACR, via_read(via1, vACR)|SR_OUT);
863 /* Load data */
864 via_write(via1, vSR, req->data[0]);
865 #ifdef USE_ORIG
866 /* Turn off TIP/TACK - this should tell the external logic to
867 start the external shift clock */
868 /* via_write(via1, vBufB, via_read(via1, vBufB)&~(TIP|TACK));*/
869 via_write(via1, vBufB, via_read(via1, vBufB)|(TIP|TACK));
870 #else
871 /* set ADB state to 'command' */
872 via_write(via1, vBufB, (via_read(via1, vBufB)&~ST_MASK)|ST_CMD);
873 #endif
874 break;
876 adb_state = sent_first_byte;
877 data_index = 1;
878 #if (ADBDEBUG & ADBDEBUG_START)
879 if (console_loglevel == 10)
880 printk("sent first byte of %d: %x, (%x %x) ... ",
881 req->nbytes, req->data[0], adb_state,
882 (via_read(via1, vBufB) & (ST_MASK|TREQ)) );
883 #endif
884 restore_flags(flags);
888 * Poll the ADB state (maybe obsolete now that interrupt-driven ADB runs)
890 void adb_poll(void)
892 unsigned char c;
893 unsigned long flags;
894 save_flags(flags);
895 cli();
896 c=via_read(via1, vIFR);
897 #if (ADBDEBUG & ADBDEBUG_POLL)
898 #ifdef DEBUG_ADB_INTS
899 if (console_loglevel == 10) {
900 printk("adb_poll: IFR %x state %x cl %d dat %d ",
901 c, adb_state, nclock, ndata);
902 if (c & (SR_CLOCK|SR_DATA)) {
903 if (c & SR_CLOCK)
904 printk("adb clock event ");
905 if (c & SR_DATA)
906 printk("adb data event ");
909 #else
910 if (console_loglevel == 10)
911 printk("adb_poll: IFR %x state %x ",
912 c, adb_state);
913 #endif
914 if (console_loglevel == 10)
915 printk("\r");
916 #endif
917 if (c & SR_INT)
919 #if (ADBDEBUG & ADBDEBUG_POLL)
920 if (console_loglevel == 10)
921 printk("adb_poll: adb interrupt event\n");
922 #endif
923 adb_interrupt(0, 0, 0);
925 restore_flags(flags);
929 * Debugging gimmicks
931 void adb_clock_interrupt(int irq, void *arg, struct pt_regs *regs)
933 nclock++;
936 void adb_data_interrupt(int irq, void *arg, struct pt_regs *regs)
938 ndata++;
942 * The notorious ADB interrupt handler - does all of the protocol handling,
943 * except for starting new send operations. Relies heavily on the ADB
944 * controller sending and receiving data, thereby generating SR interrupts
945 * for us. This means there has to be always activity on the ADB bus, otherwise
946 * the whole process dies and has to be re-kicked by sending TALK requests ...
947 * CUDA-based Macs seem to solve this with the autopoll option, for MacII-type
948 * ADB the problem isn't solved yet (retransmit of the latest active TALK seems
949 * a good choice; either on timeout or on a timer interrupt).
951 * The basic ADB state machine was left unchanged from the original MacII code
952 * by Alan Cox, which was based on the CUDA driver for PowerMac.
953 * The syntax of the ADB status lines seems to be totally different on MacII,
954 * though. MacII uses the states Command -> Even -> Odd -> Even ->...-> Idle for
955 * sending, and Idle -> Even -> Odd -> Even ->...-> Idle for receiving. Start
956 * and end of a receive packet are signaled by asserting /IRQ on the interrupt
957 * line. Timeouts are signaled by a sequence of 4 0xFF, with /IRQ asserted on
958 * every other byte. SRQ is probably signaled by 3 or more 0xFF tacked on the
959 * end of a packet. (Thanks to Guido Koerber for eavesdropping on the ADB
960 * protocol with a logic analyzer!!)
961 * CUDA seems to use /TIP -> /TIP | TACK -> /TIP -> /TIP | TACK ... -> TIP|TACK
962 * for sending, and /TIP -> /TIP | TACK -> /TIP -> /TIP | TACK ... -> TIP for
963 * receiving. No clue how timeouts are handled; SRQ seems to be sent as a
964 * separate packet. Quite a few changes have been made outside the handshake
965 * code, so I don't know if the CUDA code still behaves as before.
967 * Note: As of 21/10/97, the MacII ADB part works including timeout detection
968 * and retransmit (Talk to the last active device). Cleanup of code and
969 * testing of the CUDA functionality is required, though.
970 * Note2: As of 13/12/97, CUDA support is definitely broken ...
971 * Note3: As of 21/12/97, CUDA works on a P475. What was broken? The assumption
972 * that Q700 and Q800 use CUDA :-(
974 * 27/01/98: IIsi driver implemented (thanks to Robert Thompson for the
975 * initial bits). See adb_cuda_interrupts ...
977 * Next TODO: implementation of IIsi ADB protocol (maybe the USE_ORIG
978 * conditionals can be a start?)
980 void adb_interrupt(int irq, void *arg, struct pt_regs *regs)
982 int x, adbdir;
983 unsigned long flags;
984 struct adb_request *req;
986 last_status = status;
988 /* prevent races due to SCSI enabling ints */
989 save_flags(flags);
990 cli();
992 if (driver_running) {
993 restore_flags(flags);
994 return;
997 driver_running = 1;
999 #ifdef USE_ORIG
1000 status = (~via_read(via1, vBufB) & (TIP|TREQ)) | (via_read(via1, vACR) & SR_OUT);
1001 #else
1002 if (macintosh_config->adb_type==MAC_ADB_CUDA)
1003 status = (~via_read(via1, vBufB) & (TIP|TREQ)) | (via_read(via1, vACR) & SR_OUT);
1004 else
1005 /* status bits (0x8->0x20) and direction (0x10 ??) CLASH !! */
1006 status = (via_read(via1, vBufB) & (ST_MASK|TREQ));
1007 #endif
1008 adbdir = (via_read(via1, vACR) & SR_OUT);
1009 #if (ADBDEBUG & ADBDEBUG_INT)
1010 if (console_loglevel == 10)
1011 printk("adb_interrupt: state=%d status=%x last=%x direction=%x\n",
1012 adb_state, status, last_status, adbdir);
1013 #endif
1015 switch (adb_state)
1017 case idle:
1018 if(macintosh_config->adb_type==MAC_ADB_CUDA)
1020 /* CUDA has sent us the first byte of data - unsolicited */
1021 if (status != TREQ)
1022 printk("cuda: state=idle, status=%x\n", status);
1023 x = via_read(via1, vSR);
1024 via_write(via1, vBufB, via_read(via1,vBufB)&~TIP);
1026 else if(macintosh_config->adb_type==MAC_ADB_IISI)
1028 udelay(150);
1029 /* set SR to IN (??? no byte received else) */
1030 via_write(via1, vACR,via_read(via1, vACR)&~SR_OUT);
1031 /* signal start of frame */
1032 via_write(via1, vBufB, via_read(via1, vBufB) | TIP);
1033 /* read first byte */
1034 x = via_read(via1, vSR);
1035 first_byte = x;
1036 #if (ADBDEBUG & ADBDEBUG_READ)
1037 if (console_loglevel == 10)
1038 printk("adb_macIIsi : receiving unsol. packet: %x (%x %x) ",
1039 x, adb_state, status);
1040 #endif
1041 /* ACK adb chip */
1042 via_write(via1, vBufB, via_read(via1, vBufB) | TACK);
1043 udelay(150);
1044 via_write(via1, vBufB, via_read(via1, vBufB) & ~TACK);
1046 else if(macintosh_config->adb_type==MAC_ADB_II)
1048 #if (ADBDEBUG & ADBDEBUG_STATUS)
1049 if (status == TREQ && !adbdir)
1050 /* that's: not IRQ, idle, input -> weird */
1051 printk("adb_macII: idle, status=%x dir=%x\n",
1052 status, adbdir);
1053 #endif
1054 x = via_read(via1, vSR);
1055 first_byte = x;
1056 #if (ADBDEBUG & ADBDEBUG_READ)
1057 if (console_loglevel == 10)
1058 printk("adb_macII: receiving unsol. packet: %x (%x %x) ",
1059 x, adb_state, status);
1060 #endif
1061 /* set ADB state = even for first data byte */
1062 via_write(via1, vBufB, (via_read(via1, vBufB)&~ST_MASK)|ST_EVEN);
1064 adb_state = reading;
1065 reply_ptr = cuda_rbuf;
1066 reply_len = 0;
1067 reading_reply = 0;
1068 prefix_len = 0;
1069 if (macintosh_config->adb_type==MAC_ADB_II) {
1070 *reply_ptr++ = ADB_PACKET;
1071 *reply_ptr++ = first_byte;
1072 *reply_ptr++ = command_byte; /*first_byte;*/
1073 reply_len = 3;
1074 prefix_len = 3;
1076 break;
1078 case awaiting_reply:
1079 if(macintosh_config->adb_type==MAC_ADB_CUDA)
1081 /* CUDA has sent us the first byte of data of a reply */
1082 if (status != TREQ)
1083 printk("cuda: state=awaiting_reply, status=%x\n", status);
1084 x = via_read(via1, vSR);
1085 via_write(via1,vBufB,
1086 via_read(via1, vBufB)&~TIP);
1088 else if(macintosh_config->adb_type==MAC_ADB_IISI)
1090 /* set SR to IN */
1091 via_write(via1, vACR,via_read(via1, vACR)&~SR_OUT);
1092 /* signal start of frame */
1093 via_write(via1, vBufB, via_read(via1, vBufB) | TIP);
1094 /* read first byte */
1095 x = via_read(via1, vSR);
1096 first_byte = x;
1097 #if (ADBDEBUG & ADBDEBUG_READ)
1098 if (console_loglevel == 10)
1099 printk("adb_macIIsi: reading reply: %x (%x %x) ",
1100 x, adb_state, status);
1101 #endif
1102 #if 0
1103 if( via_read(via1,vBufB) & TREQ)
1104 ending = 1;
1105 else
1106 ending = 0;
1107 #endif
1108 /* ACK adb chip */
1109 via_write(via1, vBufB, via_read(via1, vBufB) | TACK);
1110 udelay(150);
1111 via_write(via1, vBufB, via_read(via1, vBufB) & ~TACK);
1113 else if(macintosh_config->adb_type==MAC_ADB_II)
1115 /* handshake etc. for II ?? */
1116 x = via_read(via1, vSR);
1117 first_byte = x;
1118 #if (ADBDEBUG & ADBDEBUG_READ)
1119 if (console_loglevel == 10)
1120 printk("adb_macII: reading reply: %x (%x %x) ",
1121 x, adb_state, status);
1122 #endif
1123 /* set ADB state = even for first data byte */
1124 via_write(via1, vBufB, (via_read(via1, vBufB)&~ST_MASK)|ST_EVEN);
1126 adb_state = reading;
1127 reply_ptr = current_req->reply;
1128 reading_reply = 1;
1129 reply_len = 0;
1130 prefix_len = 0;
1131 if (macintosh_config->adb_type==MAC_ADB_II) {
1132 *reply_ptr++ = ADB_PACKET;
1133 *reply_ptr++ = first_byte;
1134 *reply_ptr++ = first_byte; /* should be command byte */
1135 reply_len = 3;
1136 prefix_len = 3;
1138 break;
1140 case sent_first_byte:
1141 #if (ADBDEBUG & ADBDEBUG_WRITE)
1142 if (console_loglevel == 10)
1143 printk(" sending: %x (%x %x) ",
1144 current_req->data[1], adb_state, status);
1145 #endif
1146 if(macintosh_config->adb_type==MAC_ADB_CUDA)
1148 if (status == TREQ + TIP + SR_OUT)
1150 /* collision */
1151 via_write(via1, vACR,
1152 via_read(via1, vACR)&~SR_OUT);
1153 x = via_read(via1, vSR);
1154 via_write(via1, vBufB,
1155 via_read(via1,vBufB)|TIP|TACK);
1156 adb_state = idle;
1158 else
1160 /* assert status == TIP + SR_OUT */
1161 if (status != TIP + SR_OUT)
1162 printk("cuda: state=sent_first_byte status=%x\n", status);
1163 via_write(via1,vSR,current_req->data[1]);
1164 via_write(via1, vBufB,
1165 via_read(via1, vBufB)^TACK);
1166 data_index = 2;
1167 adb_state = sending;
1170 else if(macintosh_config->adb_type==MAC_ADB_IISI)
1172 /* switch ACK off */
1173 via_write(via1, vBufB, via_read(via1, vBufB) & ~TACK);
1174 if ( !(via_read(via1, vBufB) & TREQ) )
1176 /* collision */
1177 #if (ADBDEBUG & ADBDEBUG_WRITE)
1178 if (console_loglevel == 10)
1179 printk("adb_macIIsi: send collison, aborting!\n");
1180 #endif
1181 /* set shift in */
1182 via_write(via1, vACR,
1183 via_read(via1, vACR)&~SR_OUT);
1184 /* clear SR int. */
1185 x = via_read(via1, vSR);
1186 /* set ADB state to 'idle' */
1187 via_write(via1, vBufB,
1188 via_read(via1,vBufB) & ~(TIP|TACK));
1189 adb_state = idle;
1191 else
1193 /* delay */
1194 udelay(ADB_DELAY);
1195 /* set the shift register to shift out and send a byte */
1196 #if 0
1197 via_write(via1, vACR, via_read(via1, vACR) | SR_OUT);
1198 #endif
1199 via_write(via1, vSR, current_req->data[1]);
1200 /* signal 'byte ready' */
1201 via_write(via1, vBufB, via_read(via1, vBufB) | TACK);
1202 data_index=2;
1203 adb_state = sending;
1206 else if(macintosh_config->adb_type==MAC_ADB_II)
1208 /* how to detect a collision here ?? */
1209 /* maybe we're already done (Talk, or Poll)? */
1210 if (data_index >= current_req->nbytes)
1212 /* assert it's a Talk ?? */
1213 if ( (command_byte&0xc) != 0xc
1214 && console_loglevel == 10 )
1215 printk("ADB: single byte command, no Talk: %x!\n",
1216 command_byte);
1217 #if (ADBDEBUG & ADBDEBUG_WRITE)
1218 if (console_loglevel == 10)
1219 printk(" -> end (%d of %d) (%x %x)!\n",
1220 data_index, current_req->nbytes, adb_state, status);
1221 #endif
1222 current_req->sent = 1;
1223 if (current_req->reply_expected)
1225 #if (ADBDEBUG & ADBDEBUG_WRITE)
1226 if (console_loglevel == 10)
1227 printk("ADB: reply expected on poll!\n");
1228 #endif
1229 adb_state = awaiting_reply;
1230 reading_reply = 0;
1231 } else {
1232 #if (ADBDEBUG & ADBDEBUG_WRITE)
1233 if (console_loglevel == 10)
1234 printk("ADB: no reply for poll, not calling done()!\n");
1235 #endif
1236 req = current_req;
1237 current_req = req->next;
1238 #if 0 /* XXX Not sure about that one ... probably better enabled */
1239 if (req->done)
1240 (*req->done)(req);
1241 #endif
1242 adb_state = idle;
1243 reading_reply = 0;
1245 /* set to shift in */
1246 via_write(via1, vACR,
1247 via_read(via1, vACR) & ~SR_OUT);
1248 x=via_read(via1, vSR);
1249 /* set ADB state idle - might get SRQ */
1250 via_write(via1, vBufB,
1251 (via_read(via1, vBufB)&~ST_MASK)|ST_IDLE);
1252 break;
1254 #if (ADBDEBUG & ADBDEBUG_STATUS)
1255 if(!(status==(ST_CMD|TREQ) && adbdir == SR_OUT))
1256 printk("adb_macII: sent_first_byte, weird status=%x dir=%x\n",
1257 status, adbdir);
1258 #endif
1259 /* SR already set to shift out; send byte */
1260 via_write(via1, vSR, current_req->data[1]);
1261 /* set state to ST_EVEN (first byte was: ST_CMD) */
1262 via_write(via1, vBufB,
1263 (via_read(via1, vBufB)&~ST_MASK)|ST_EVEN);
1264 data_index=2;
1265 adb_state = sending;
1267 break;
1269 case sending:
1270 req = current_req;
1271 if (data_index >= req->nbytes)
1273 #if (ADBDEBUG & ADBDEBUG_WRITE)
1274 if (console_loglevel == 10)
1275 printk(" -> end (%d of %d) (%x %x)!\n",
1276 data_index-1, req->nbytes, adb_state, status);
1277 #endif
1278 /* end of packet */
1279 if(macintosh_config->adb_type==MAC_ADB_CUDA)
1281 via_write(via1, vACR,
1282 via_read(via1, vACR)&~SR_OUT);
1283 x = via_read(via1, vSR);
1284 via_write(via1, vBufB,
1285 via_read(via1,vBufB)|TACK|TIP);
1287 else if(macintosh_config->adb_type==MAC_ADB_IISI)
1289 /* XXX maybe clear ACK here ??? */
1290 /* switch ACK off */
1291 via_write(via1, vBufB, via_read(via1, vBufB) & ~TACK);
1292 /* delay */
1293 udelay(ADB_DELAY);
1294 /* set the shift register to shift in */
1295 via_write(via1, vACR, via_read(via1, vACR)|SR_OUT);
1296 /* clear SR int. */
1297 x = via_read(via1, vSR);
1298 /* set ADB state 'idle' (end of frame) */
1299 via_write(via1, vBufB,
1300 via_read(via1,vBufB) & ~(TACK|TIP));
1302 else if(macintosh_config->adb_type==MAC_ADB_II)
1305 * XXX Not sure: maybe only switch to
1306 * input mode on Talk ??
1308 /* set to shift in */
1309 via_write(via1, vACR,
1310 via_read(via1, vACR) & ~SR_OUT);
1311 x=via_read(via1, vSR);
1312 /* set ADB state idle - might get SRQ */
1313 via_write(via1, vBufB,
1314 (via_read(via1, vBufB)&~ST_MASK)|ST_IDLE);
1316 req->sent = 1;
1317 if (req->reply_expected)
1320 * maybe fake a reply here on Listen ??
1321 * Otherwise, a Listen hangs on success
1323 if ( macintosh_config->adb_type==MAC_ADB_II
1324 && ((req->data[0]&0xc) == 0xc) )
1325 adb_state = awaiting_reply;
1326 else if ( macintosh_config->adb_type != MAC_ADB_II
1327 && ( req->data[0] == 0x0)
1328 && ((req->data[1]&0xc) == 0xc) )
1329 adb_state = awaiting_reply;
1330 else {
1332 * Reply expected, but none
1333 * possible -> fake reply.
1334 * Problem: sending next command
1335 * should probably be done
1336 * without setting bus to 'idle'!
1337 * (except if no more commands)
1339 #if (ADBDEBUG & ADBDEBUG_PROT)
1340 printk("ADB: reply expected on Listen, faking reply\n");
1341 #endif
1342 /* make it look weird */
1343 /* XXX: return reply_len -1? */
1344 /* XXX: fake ADB header? */
1345 req->reply[0] = req->reply[1] = req->reply[2] = 0xFF;
1346 req->reply_len = 3;
1347 req->got_reply = 1;
1348 current_req = req->next;
1349 if (req->done)
1350 (*req->done)(req);
1352 * ready with this one, run
1353 * next command or repeat last
1354 * Talk (=idle on II)
1356 /* set state to idle !! */
1357 adb_state = idle;
1358 if (current_req || retry_req)
1359 adb_start();
1362 else
1364 current_req = req->next;
1365 if (req->done)
1366 (*req->done)(req);
1367 /* not sure about this */
1369 * MS: Must set idle, no new request
1370 * started else !
1372 adb_state = idle;
1374 * requires setting ADB state to idle,
1375 * maybe read a byte ! (done above)
1377 if (current_req || retry_req)
1378 adb_start();
1381 else
1383 #if (ADBDEBUG & ADBDEBUG_WRITE)
1384 if (console_loglevel == 10)
1385 printk(" %x (%x %x) ",
1386 req->data[data_index], adb_state, status);
1387 #endif
1388 if(macintosh_config->adb_type==MAC_ADB_CUDA)
1390 via_write(via1, vSR, req->data[data_index++]);
1391 via_write(via1, vBufB,
1392 via_read(via1, vBufB)^TACK);
1394 else if(macintosh_config->adb_type==MAC_ADB_IISI)
1396 /* switch ACK off */
1397 via_write(via1, vBufB, via_read(via1, vBufB) & ~TACK);
1398 /* delay */
1399 udelay(ADB_DELAY);
1400 /* XXX: need to check for collision?? */
1401 /* set the shift register to shift out and send a byte */
1402 #if 0
1403 via_write(via1, vACR, via_read(via1, vACR)|SR_OUT);
1404 #endif
1405 via_write(via1, vSR, req->data[data_index++]);
1406 /* signal 'byte ready' */
1407 via_write(via1, vBufB, via_read(via1, vBufB) | TACK);
1409 else if(macintosh_config->adb_type==MAC_ADB_II)
1411 via_write(via1, vSR, req->data[data_index++]);
1412 /* invert state bits, toggle ODD/EVEN */
1413 x = via_read(via1, vBufB);
1414 via_write(via1, vBufB,
1415 (x&~ST_MASK)|~(x&ST_MASK));
1418 break;
1420 case reading:
1422 /* timeout / SRQ handling for II hw */
1423 #ifdef POLL_ON_TIMEOUT
1424 if((reply_len-prefix_len)==3 && memcmp(reply_ptr-3,"\xFF\xFF\xFF",3)==0)
1425 #else
1426 if( (first_byte == 0xFF && (reply_len-prefix_len)==2
1427 && memcmp(reply_ptr-2,"\xFF\xFF",2)==0) ||
1428 ((reply_len-prefix_len)==3
1429 && memcmp(reply_ptr-3,"\xFF\xFF\xFF",3)==0))
1430 #endif
1433 * possible timeout (in fact, most probably a
1434 * timeout, since SRQ can't be signaled without
1435 * transfer on the bus).
1436 * The last three bytes seen were FF, together
1437 * with the starting byte (in case we started
1438 * on 'idle' or 'awaiting_reply') this probably
1439 * makes four. So this is mostl likely #5!
1440 * The timeout signal is a pattern 1 0 1 0 0..
1441 * on /INT, meaning we missed it :-(
1443 x = via_read(via1, vSR);
1444 if (x != 0xFF)
1445 printk("ADB: mistaken timeout/SRQ!\n");
1448 * ADB status bits: either even or odd.
1449 * adb_state: need to set 'idle' here.
1450 * Maybe saner: set 'need_poll' or
1451 * 'need_resend' here, fall through to
1452 * read_done ??
1454 #if (ADBDEBUG & ADBDEBUG_READ)
1455 if (console_loglevel == 10)
1456 printk(" -> read aborted: %x (%x %x)!\n",
1457 x, adb_state, status);
1458 #endif
1460 #if 0 /* XXX leave status unchanged!! - need to check this again! */
1461 /* XXX Only touch status on II !!! */
1462 /* set ADB state to idle (required by adb_start()) */
1463 via_write(via1, vBufB,
1464 (via_read(via1, vBufB)&~ST_MASK)|ST_IDLE);
1465 #endif
1468 * What if the timeout happens on reading a
1469 * reply ?? Assemble error reply and call
1470 * current_request->done()? Keep request
1471 * on queue?
1474 /* prevent 'busy' in adb_start() */
1475 need_poll = 1;
1478 * Timeout: /IRQ alternates high/low during
1479 * 4 'FF' bytes (1 0 1 0 0...)
1480 * We're on byte 5, so we need one
1481 * more backlog here (TBI) ....
1483 if ((status&TREQ) != (last_status&TREQ)) {
1484 #if (ADBDEBUG & ADBDEBUG_SRQ)
1485 if (console_loglevel == 10)
1486 printk("ADB: reply timeout, resending!\n");
1487 #endif
1489 * first byte received should be the
1490 * command byte timing out !!
1492 if (first_byte != 0xff)
1493 command_byte = first_byte;
1496 * compute target for retransmit: if
1497 * last_active is set, use that one,
1498 * else use command_byte
1500 if (last_active == -1)
1501 last_active = (command_byte&0xf0)>>4;
1502 adb_state = idle;
1503 /* resend if TALK, don't poll! */
1504 if (current_req)
1505 adb_start();
1506 else
1508 * XXX: need to count the timeouts ??
1509 * restart last active TALK ??
1510 * If no current_req, reuse old one!
1512 adb_retransmit(last_active);
1514 } else {
1516 * SRQ: NetBSD suggests /IRQ is asserted!?
1518 if (status&TREQ)
1519 printk("ADB: SRQ signature w/o /INT!\n");
1520 #if (ADBDEBUG & ADBDEBUG_SRQ)
1521 if (console_loglevel == 10)
1522 printk("ADB: empty SRQ packet!\n");
1523 #endif
1524 /* Terminate the SRQ packet and poll */
1525 adb_state = idle;
1526 adb_queue_poll();
1529 * Leave ADB status lines unchanged (means /IRQ
1530 * will still be low when entering adb_start!)
1532 break;
1534 /* end timeout / SRQ handling for II hw. */
1535 if((reply_len-prefix_len)>3 && memcmp(reply_ptr-3,"\xFF\xFF\xFF",3)==0)
1537 /* SRQ tacked on data packet */
1538 /* Check /IRQ here ?? */
1539 #if (ADBDEBUG & ADBDEBUG_SRQ)
1540 if (console_loglevel == 10)
1541 printk("\nADB: Packet with SRQ!\n");
1542 #endif
1543 /* Terminate the packet (SRQ never ends) */
1544 x = via_read(via1, vSR);
1545 adb_state = read_done;
1546 reply_len -= 3;
1547 reply_ptr -= 3;
1548 need_poll = 1;
1549 /* need to continue; next byte not seen else */
1551 * XXX: not at all sure here; maybe need to
1552 * send away the reply and poll immediately?
1554 } else {
1555 /* Sanity check */
1556 if(reply_len>15)
1557 reply_len=0;
1558 /* read byte */
1559 *reply_ptr = via_read(via1, vSR);
1560 x = *reply_ptr;
1561 #if (ADBDEBUG & ADBDEBUG_READ)
1562 if (console_loglevel == 10)
1563 printk(" %x (%x %x) ",
1564 *reply_ptr, adb_state, status);
1565 #endif
1566 reply_ptr++;
1567 reply_len++;
1569 /* The usual handshake ... */
1570 if (macintosh_config->adb_type==MAC_ADB_CUDA)
1572 if (status == TIP)
1574 /* that's all folks */
1575 via_write(via1, vBufB,
1576 via_read(via1, vBufB)|TACK|TIP);
1577 adb_state = read_done;
1579 else
1581 /* assert status == TIP | TREQ */
1582 if (status != TIP + TREQ)
1583 printk("cuda: state=reading status=%x\n", status);
1584 via_write(via1, vBufB,
1585 via_read(via1, vBufB)^TACK);
1588 else if (macintosh_config->adb_type==MAC_ADB_IISI)
1590 /* ACK adb chip (maybe check for end first?) */
1591 via_write(via1, vBufB, via_read(via1, vBufB) | TACK);
1592 udelay(150);
1593 via_write(via1, vBufB, via_read(via1, vBufB) & ~TACK);
1594 /* end of frame?? */
1595 if (status & TREQ)
1597 #if (ADBDEBUG & ADBDEBUG_READ)
1598 if (console_loglevel == 10)
1599 printk("adb_IIsi: end of frame!\n");
1600 #endif
1601 /* that's all folks */
1602 via_write(via1, vBufB,
1603 via_read(via1, vBufB) & ~(TACK|TIP));
1604 adb_state = read_done;
1605 /* maybe process read_done here?? Handshake anyway?? */
1608 else if (macintosh_config->adb_type==MAC_ADB_II)
1611 * NetBSD hints that the next to last byte
1612 * is sent with IRQ !!
1613 * Guido found out it's the last one (0x0),
1614 * but IRQ should be asserted already.
1615 * Problem with timeout detection: First
1616 * transition to /IRQ might be second
1617 * byte of timeout packet!
1618 * Timeouts are signaled by 4x FF.
1620 if(!(status&TREQ) && x == 0x00) /* != 0xFF */
1622 #if (ADBDEBUG & ADBDEBUG_READ)
1623 if (console_loglevel == 10)
1624 printk(" -> read done!\n");
1625 #endif
1626 #if 0 /* XXX: we take one more byte (why?), so handshake! */
1627 /* set ADB state to idle */
1628 via_write(via1, vBufB,
1629 (via_read(via1, vBufB)&~ST_MASK)|ST_IDLE);
1630 #else
1631 /* invert state bits, toggle ODD/EVEN */
1632 x = via_read(via1, vBufB);
1633 via_write(via1, vBufB,
1634 (x&~ST_MASK)|~(x&ST_MASK));
1635 #endif
1636 /* adjust packet length */
1637 reply_len--;
1638 reply_ptr--;
1639 adb_state = read_done;
1641 else
1643 #if (ADBDEBUG & ADBDEBUG_STATUS)
1644 if(status!=TIP+TREQ)
1645 printk("macII_adb: state=reading status=%x\n", status);
1646 #endif
1647 /* not caught: ST_CMD */
1648 /* required for re-entry 'reading'! */
1649 if ((status&ST_MASK) == ST_IDLE) {
1650 /* (in)sanity check - set even */
1651 via_write(via1, vBufB,
1652 (via_read(via1, vBufB)&~ST_MASK)|ST_EVEN);
1653 } else {
1654 /* invert state bits, toggle ODD/EVEN */
1655 x = via_read(via1, vBufB);
1656 via_write(via1, vBufB,
1657 (x&~ST_MASK)|~(x&ST_MASK));
1661 break;
1663 case read_done:
1664 x = via_read(via1, vSR);
1665 #if (ADBDEBUG & ADBDEBUG_READ)
1666 if (console_loglevel == 10)
1667 printk("ADB: read done: %x (%x %x)!\n",
1668 x, adb_state, status);
1669 #endif
1670 if (reading_reply)
1672 req = current_req;
1673 req->reply_len = reply_ptr - req->reply;
1674 req->got_reply = 1;
1675 current_req = req->next;
1676 if (req->done)
1677 (*req->done)(req);
1679 else
1681 adb_input(cuda_rbuf, reply_ptr - cuda_rbuf, regs);
1685 * remember this device ID; it's the latest we got a
1686 * reply from!
1688 last_reply = command_byte;
1689 last_active = (command_byte&0xf0)>>4;
1692 * Assert status = ST_IDLE ??
1695 * SRQ seen before, initiate poll now
1697 if (need_poll) {
1698 #if (ADBDEBUG & ADBDEBUG_POLL)
1699 if (console_loglevel == 10)
1700 printk("ADB: initiate poll!\n");
1701 #endif
1702 adb_state = idle;
1704 * set ADB status bits?? (unchanged above!)
1706 adb_queue_poll();
1707 need_poll = 0;
1708 /* hope this is ok; queue_poll runs adb_start */
1709 break;
1713 * /IRQ seen, so the ADB controller has data for us
1715 if (!(status&TREQ))
1717 /* set ADB state to idle */
1718 via_write(via1, vBufB,
1719 (via_read(via1, vBufB)&~ST_MASK)|ST_IDLE);
1721 adb_state = reading;
1722 reply_ptr = cuda_rbuf;
1723 reply_len = 0;
1724 prefix_len = 0;
1725 if (macintosh_config->adb_type==MAC_ADB_II) {
1726 *reply_ptr++ = ADB_PACKET;
1727 *reply_ptr++ = command_byte;
1728 reply_len = 2;
1729 prefix_len = 2;
1731 reading_reply = 0;
1733 else
1736 * no IRQ, send next packet or wait
1738 adb_state = idle;
1739 if (current_req)
1740 adb_start();
1741 else
1742 adb_retransmit(last_active);
1744 break;
1746 default:
1747 #if (ADBDEBUG & ADBDEBUG_STATE)
1748 printk("adb_interrupt: unknown adb_state %d?\n", adb_state);
1749 #endif
1751 /* reset mutex and interrupts */
1752 driver_running = 0;
1753 restore_flags(flags);
1757 * Restart of CUDA support: please modify this interrupt handler while
1758 * working at the Quadra etc. ADB driver. We can try to merge them later, or
1759 * remove the CUDA stuff from the MacII handler
1761 * MSch 27/01/98: Implemented IIsi driver based on initial code by Robert
1762 * Thompson and hints from the NetBSD driver. CUDA and IIsi seem more closely
1763 * related than to the MacII code, so merging all three might be a bad
1764 * idea.
1767 void adb_cuda_interrupt(int irq, void *arg, struct pt_regs *regs)
1769 int x, status;
1770 struct adb_request *req;
1771 unsigned long flags;
1773 save_flags(flags);
1774 cli();
1776 if(macintosh_config->adb_type==MAC_ADB_CUDA)
1777 status = (~via_read(via1, vBufB) & (TIP|TREQ)) | (via_read(via1, vACR) & SR_OUT);
1778 else
1779 status = via_read(via1, vBufB) & (TIP|TREQ);
1781 #if (ADBDEBUG & ADBDEBUG_INT)
1782 if (console_loglevel == 10)
1783 printk("adb_interrupt: state=%d status=%x\n", adb_state, status);
1784 #endif
1786 switch (adb_state)
1788 case idle:
1789 first_byte = 0;
1790 if(macintosh_config->adb_type==MAC_ADB_CUDA)
1792 #if (ADBDEBUG & ADBDEBUG_STATUS)
1793 /* CUDA has sent us the first byte of data - unsolicited */
1794 if (status != TREQ)
1795 printk("cuda: state=idle, status=%x want=%x\n",
1796 status, TREQ);
1797 #endif
1798 x = via_read(via1, vSR);
1799 #if (ADBDEBUG & ADBDEBUG_READ)
1800 if (console_loglevel == 10)
1801 printk("adb_cuda: receiving unsol. packet: %x (%x %x) ",
1802 x, adb_state, status);
1803 #endif
1804 via_write(via1, vBufB, via_read(via1,vBufB)&~TIP);
1806 else if(macintosh_config->adb_type==MAC_ADB_IISI)
1808 udelay(150);
1809 /* set SR to IN */
1810 via_write(via1, vACR,via_read(via1, vACR)&~SR_OUT);
1811 /* signal start of frame */
1812 via_write(via1, vBufB, via_read(via1, vBufB) | TIP);
1813 /* read first byte */
1814 x = via_read(via1, vSR);
1815 first_byte = x;
1816 #if (ADBDEBUG & ADBDEBUG_READ)
1817 if (console_loglevel == 10)
1818 printk("adb_IIsi : receiving unsol. packet: %x (%x %x) ",
1819 x, adb_state, status);
1820 #endif
1821 /* ACK adb chip */
1822 via_write(via1, vBufB, via_read(via1, vBufB) | TACK);
1823 udelay(150);
1824 via_write(via1, vBufB, via_read(via1, vBufB) & ~TACK);
1826 else if(macintosh_config->adb_type==MAC_ADB_II)
1828 if (status != TREQ)
1829 printk("adb_macII: state=idle status=%x want=%x\n",
1830 status, TREQ);
1831 x = via_read(via1, vSR);
1832 via_write(via1, vBufB, via_read(via1, vBufB)&~(TIP|TACK));
1834 adb_state = reading;
1835 reply_ptr = cuda_rbuf;
1836 reply_len = 0;
1837 reading_reply = 0;
1838 break;
1840 case awaiting_reply:
1841 if(macintosh_config->adb_type==MAC_ADB_CUDA)
1843 /* CUDA has sent us the first byte of data of a reply */
1844 #if (ADBDEBUG & ADBDEBUG_STATUS)
1845 if (status != TREQ)
1846 printk("cuda: state=awaiting_reply, status=%x want=%x\n",
1847 status, TREQ);
1848 #endif
1849 x = via_read(via1, vSR);
1850 #if (ADBDEBUG & ADBDEBUG_READ)
1851 if (console_loglevel == 10)
1852 printk("adb_cuda: reading reply: %x (%x %x) ",
1853 x, adb_state, status);
1854 #endif
1855 via_write(via1,vBufB,
1856 via_read(via1, vBufB)&~TIP);
1858 else if(macintosh_config->adb_type==MAC_ADB_IISI)
1860 /* udelay(150);*/
1861 /* set SR to IN */
1862 via_write(via1, vACR,via_read(via1, vACR)&~SR_OUT);
1863 /* signal start of frame */
1864 via_write(via1, vBufB, via_read(via1, vBufB) | TIP);
1865 /* read first byte */
1866 x = via_read(via1, vSR);
1867 first_byte = x;
1868 #if (ADBDEBUG & ADBDEBUG_READ)
1869 if (console_loglevel == 10)
1870 printk("adb_IIsi: reading reply: %x (%x %x) ",
1871 x, adb_state, status);
1872 #endif
1873 #if 0
1874 if( via_read(via1,vBufB) & TREQ)
1875 ending = 1;
1876 else
1877 ending = 0;
1878 #endif
1879 /* ACK adb chip */
1880 via_write(via1, vBufB, via_read(via1, vBufB) | TACK);
1881 udelay(150);
1882 via_write(via1, vBufB, via_read(via1, vBufB) & ~TACK);
1884 adb_state = reading;
1885 reply_ptr = current_req->reply;
1886 reading_reply = 1;
1887 reply_len = 0;
1888 break;
1890 case sent_first_byte:
1891 if(macintosh_config->adb_type==MAC_ADB_CUDA)
1893 #if (ADBDEBUG & ADBDEBUG_WRITE)
1894 if (console_loglevel == 10)
1895 printk(" sending: %x (%x %x) ",
1896 current_req->data[1], adb_state, status);
1897 #endif
1898 if (status == TREQ + TIP + SR_OUT)
1900 /* collision */
1901 if (console_loglevel == 10)
1902 printk("adb_cuda: send collision!\n");
1903 via_write(via1, vACR,
1904 via_read(via1, vACR)&~SR_OUT);
1905 x = via_read(via1, vSR);
1906 via_write(via1, vBufB,
1907 via_read(via1,vBufB)|TIP|TACK);
1908 adb_state = idle;
1910 else
1912 /* assert status == TIP + SR_OUT */
1913 #if (ADBDEBUG & ADBDEBUG_STATUS)
1914 if (status != TIP + SR_OUT)
1915 printk("adb_cuda: state=sent_first_byte status=%x want=%x\n",
1916 status, TIP + SR_OUT);
1917 #endif
1918 via_write(via1,vSR,current_req->data[1]);
1919 via_write(via1, vBufB,
1920 via_read(via1, vBufB)^TACK);
1921 data_index = 2;
1922 adb_state = sending;
1925 else if(macintosh_config->adb_type==MAC_ADB_IISI)
1927 /* switch ACK off */
1928 via_write(via1, vBufB, via_read(via1, vBufB) & ~TACK);
1929 if ( !(via_read(via1, vBufB) & TREQ) )
1931 /* collision */
1932 #if (ADBDEBUG & ADBDEBUG_WRITE)
1933 if (console_loglevel == 10)
1934 printk("adb_macIIsi: send collison, aborting!\n");
1935 #endif
1936 /* set shift in */
1937 via_write(via1, vACR,
1938 via_read(via1, vACR)&~SR_OUT);
1939 /* clear SR int. */
1940 x = via_read(via1, vSR);
1941 /* set ADB state to 'idle' */
1942 via_write(via1, vBufB,
1943 via_read(via1,vBufB) & ~(TIP|TACK));
1944 adb_state = idle;
1946 else
1948 /* delay */
1949 udelay(ADB_DELAY);
1950 /* set the shift register to shift out and send a byte */
1951 #if 0
1952 via_write(via1, vACR, via_read(via1, vACR) | SR_OUT);
1953 #endif
1954 via_write(via1, vSR, current_req->data[1]);
1955 /* signal 'byte ready' */
1956 via_write(via1, vBufB, via_read(via1, vBufB) | TACK);
1957 data_index=2;
1958 adb_state = sending;
1961 else if(macintosh_config->adb_type==MAC_ADB_II)
1963 if(status!=TIP+SR_OUT)
1964 printk("adb_macII: state=send_first_byte status=%x want=%x\n",
1965 status, TIP+SR_OUT);
1966 via_write(via1, vSR, current_req->data[1]);
1967 via_write(via1, vBufB,
1968 via_read(via1, vBufB)^TACK);
1969 data_index=2;
1970 adb_state = sending;
1972 break;
1974 case sending:
1975 req = current_req;
1976 if (data_index >= req->nbytes)
1978 #if (ADBDEBUG & ADBDEBUG_WRITE)
1979 if (console_loglevel == 10)
1980 printk(" -> end (%d of %d) (%x %x)!\n",
1981 data_index-1, req->nbytes, adb_state, status);
1982 #endif
1983 if(macintosh_config->adb_type==MAC_ADB_CUDA)
1985 via_write(via1, vACR,
1986 via_read(via1, vACR)&~SR_OUT);
1987 x = via_read(via1, vSR);
1988 via_write(via1, vBufB,
1989 via_read(via1,vBufB)|TACK|TIP);
1991 else if(macintosh_config->adb_type==MAC_ADB_IISI)
1993 /* XXX maybe clear ACK here ??? */
1994 /* switch ACK off */
1995 via_write(via1, vBufB, via_read(via1, vBufB) & ~TACK);
1996 /* delay */
1997 udelay(ADB_DELAY);
1998 /* set the shift register to shift in */
1999 via_write(via1, vACR, via_read(via1, vACR)|SR_OUT);
2000 /* clear SR int. */
2001 x = via_read(via1, vSR);
2002 /* set ADB state 'idle' (end of frame) */
2003 via_write(via1, vBufB,
2004 via_read(via1,vBufB) & ~(TACK|TIP));
2006 else if(macintosh_config->adb_type==MAC_ADB_II)
2008 via_write(via1, vACR,
2009 via_read(via1, vACR) & ~SR_OUT);
2010 x=via_read(via1, vSR);
2011 via_write(via1, vBufB,
2012 via_read(via1, vBufB)|TACK|TIP);
2014 req->sent = 1;
2015 if (req->reply_expected)
2018 * maybe fake a reply here on Listen ??
2019 * Otherwise, a Listen hangs on success
2020 * CUDA+IIsi: only ADB Talk considered
2021 * RTC/PRAM read (0x1 0x3) to follow.
2023 if ( (req->data[0] == 0x0) && ((req->data[1]&0xc) == 0xc) )
2024 adb_state = awaiting_reply;
2025 else {
2027 * Reply expected, but none
2028 * possible -> fake reply.
2030 #if (ADBDEBUG & ADBDEBUG_PROT)
2031 printk("ADB: reply expected on Listen, faking reply\n");
2032 #endif
2033 /* make it look weird */
2034 /* XXX: return reply_len -1? */
2035 /* XXX: fake ADB header? */
2036 req->reply[0] = req->reply[1] = req->reply[2] = 0xFF;
2037 req->reply_len = 3;
2038 req->got_reply = 1;
2039 current_req = req->next;
2040 if (req->done)
2041 (*req->done)(req);
2043 * ready with this one, run
2044 * next command !
2046 /* set state to idle !! */
2047 adb_state = idle;
2048 if (current_req || retry_req)
2049 adb_start();
2052 else
2054 current_req = req->next;
2055 if (req->done)
2056 (*req->done)(req);
2057 /* not sure about this */
2058 adb_state = idle;
2059 adb_start();
2062 else
2064 #if (ADBDEBUG & ADBDEBUG_WRITE)
2065 if (console_loglevel == 10)
2066 printk(" %x (%x %x) ",
2067 req->data[data_index], adb_state, status);
2068 #endif
2069 if(macintosh_config->adb_type==MAC_ADB_CUDA)
2071 via_write(via1, vSR, req->data[data_index++]);
2072 via_write(via1, vBufB,
2073 via_read(via1, vBufB)^TACK);
2075 else if(macintosh_config->adb_type==MAC_ADB_IISI)
2077 /* switch ACK off */
2078 via_write(via1, vBufB, via_read(via1, vBufB) & ~TACK);
2079 /* delay */
2080 udelay(ADB_DELAY);
2081 /* XXX: need to check for collision?? */
2082 /* set the shift register to shift out and send a byte */
2083 #if 0
2084 via_write(via1, vACR, via_read(via1, vACR)|SR_OUT);
2085 #endif
2086 via_write(via1, vSR, req->data[data_index++]);
2087 /* signal 'byte ready' */
2088 via_write(via1, vBufB, via_read(via1, vBufB) | TACK);
2090 else if(macintosh_config->adb_type==MAC_ADB_II)
2092 via_write(via1, vSR, req->data[data_index++]);
2093 via_write(via1, vBufB,
2094 via_read(via1, vBufB)^TACK);
2097 break;
2099 case reading:
2100 if(reply_len==3 && memcmp(reply_ptr-3,"\xFF\xFF\xFF",3)==0)
2102 /* Terminate the SRQ packet */
2103 #if (ADBDEBUG & ADBDEBUG_SRQ)
2104 if (console_loglevel == 10)
2105 printk("adb: Got an SRQ\n");
2106 #endif
2107 adb_state = idle;
2108 adb_queue_poll();
2109 break;
2111 /* Sanity check - botched in orig. code! */
2112 if(reply_len>15) {
2113 printk("adb_cuda: reply buffer overrun!\n");
2114 /* wrap buffer */
2115 reply_len=0;
2116 if (reading_reply)
2117 reply_ptr = current_req->reply;
2118 else
2119 reply_ptr = cuda_rbuf;
2121 *reply_ptr = via_read(via1, vSR);
2122 #if (ADBDEBUG & ADBDEBUG_READ)
2123 if (console_loglevel == 10)
2124 printk(" %x (%x %x) ",
2125 *reply_ptr, adb_state, status);
2126 #endif
2127 reply_ptr++;
2128 reply_len++;
2129 if(macintosh_config->adb_type==MAC_ADB_CUDA)
2131 if (status == TIP)
2133 /* that's all folks */
2134 via_write(via1, vBufB,
2135 via_read(via1, vBufB)|TACK|TIP);
2136 adb_state = read_done;
2138 else
2140 /* assert status == TIP | TREQ */
2141 #if (ADBDEBUG & ADBDEBUG_STATUS)
2142 if (status != TIP + TREQ)
2143 printk("cuda: state=reading status=%x want=%x\n",
2144 status, TIP + TREQ);
2145 #endif
2146 via_write(via1, vBufB,
2147 via_read(via1, vBufB)^TACK);
2150 else if (macintosh_config->adb_type==MAC_ADB_IISI)
2152 /* ACK adb chip (maybe check for end first?) */
2153 via_write(via1, vBufB, via_read(via1, vBufB) | TACK);
2154 udelay(150);
2155 via_write(via1, vBufB, via_read(via1, vBufB) & ~TACK);
2156 /* end of frame?? */
2157 if (status & TREQ)
2159 #if (ADBDEBUG & ADBDEBUG_READ)
2160 if (console_loglevel == 10)
2161 printk("adb_IIsi: end of frame!\n");
2162 #endif
2163 /* that's all folks */
2164 via_write(via1, vBufB,
2165 via_read(via1, vBufB) & ~(TACK|TIP));
2166 adb_state = read_done;
2167 /* XXX maybe process read_done here??
2168 Handshake anyway?? */
2171 if(macintosh_config->adb_type==MAC_ADB_II)
2173 if( status == TIP)
2175 via_write(via1, vBufB,
2176 via_read(via1, vBufB)|TACK|TIP);
2177 adb_state = read_done;
2179 else
2181 #if (ADBDEBUG & ADBDEBUG_STATUS)
2182 if(status!=TIP+TREQ)
2183 printk("macII_adb: state=reading status=%x\n", status);
2184 #endif
2185 via_write(via1, vBufB,
2186 via_read(via1, vBufB)^TACK);
2189 /* fall through for IIsi on end of frame */
2190 if (macintosh_config->adb_type != MAC_ADB_IISI
2191 || adb_state != read_done)
2192 break;
2194 case read_done:
2195 x = via_read(via1, vSR);
2196 #if (ADBDEBUG & ADBDEBUG_READ)
2197 if (console_loglevel == 10)
2198 printk("adb: read done: %x (%x %x)!\n",
2199 x, adb_state, status);
2200 #endif
2201 if (reading_reply)
2203 req = current_req;
2204 req->reply_len = reply_ptr - req->reply;
2205 req->got_reply = 1;
2206 current_req = req->next;
2207 if (req->done)
2208 (*req->done)(req);
2210 else
2212 adb_input(cuda_rbuf, reply_ptr - cuda_rbuf, regs);
2215 if (macintosh_config->adb_type==MAC_ADB_CUDA
2216 && status & TREQ)
2218 via_write(via1, vBufB,
2219 via_read(via1, vBufB)&~TIP);
2220 adb_state = reading;
2221 reply_ptr = cuda_rbuf;
2222 reading_reply = 0;
2224 else if (macintosh_config->adb_type==MAC_ADB_IISI
2225 && !(status & TREQ))
2227 udelay(150);
2228 via_write(via1, vBufB,
2229 via_read(via1, vBufB) | TIP);
2230 adb_state = reading;
2231 reply_ptr = cuda_rbuf;
2232 reading_reply = 0;
2234 else
2236 adb_state = idle;
2237 adb_start();
2239 break;
2241 default:
2242 printk("adb_cuda_interrupt: unknown adb_state %d?\n", adb_state);
2245 restore_flags(flags);
2250 * The 'reply delivery' routine; determines which device sent the
2251 * request and calls the appropriate handler.
2252 * Reply data are expected in CUDA format (again, argh...) so we need
2253 * to fake this in the interrupt handler for MacII.
2254 * Only one handler per device ID is currently possible.
2255 * XXX: is the ID field here representing the default or real ID?
2257 static void adb_input(unsigned char *buf, int nb, struct pt_regs *regs)
2259 int i, id;
2261 switch (buf[0])
2263 case ADB_PACKET:
2264 /* what's in buf[1] ?? */
2265 id = buf[2] >> 4;
2266 #if 0
2267 xmon_printf("adb packet: ");
2268 for (i = 0; i < nb; ++i)
2269 xmon_printf(" %x", buf[i]);
2270 xmon_printf(", id = %d\n", id);
2271 #endif
2272 #if (ADBDEBUG & ADBDEBUG_INPUT)
2273 if (console_loglevel == 10) {
2274 printk("adb_input: adb packet ");
2275 for (i = 0; i < nb; ++i)
2276 printk(" %x", buf[i]);
2277 printk(", id = %d\n", id);
2279 #endif
2280 if (adb_handler[id].handler != 0)
2282 (*adb_handler[id].handler)(buf, nb, regs);
2284 break;
2286 default:
2287 #if (ADBDEBUG & ADBDEBUG_INPUT)
2288 if (console_loglevel == 10) {
2289 printk("adb_input: data from via (%d bytes):", nb);
2290 for (i = 0; i < nb; ++i)
2291 printk(" %.2x", buf[i]);
2292 printk("\n");
2294 #endif
2298 /* Ultimately this should return the number of devices with
2299 the given default id. */
2301 int adb_register(int default_id,
2302 void (*handler)(unsigned char *, int, struct pt_regs *))
2304 if (adb_handler[default_id].handler != 0)
2305 panic("Two handlers for ADB device %d\n", default_id);
2306 adb_handler[default_id].handler = handler;
2307 return 1;
2311 * /dev/adb device driver.
2314 #define ADB_MAJOR 56 /* major number for /dev/adb */
2316 #define ADB_MAX_MINOR 64 /* range of ADB minors */
2317 #define ADB_TYPE_SHIFT 4 /* # bits for device ID/type in subdevices */
2319 #define ADB_TYPE_RAW 0 /* raw device; unbuffered */
2320 #define ADB_TYPE_BUFF 1 /* raw device; buffered */
2321 #define ADB_TYPE_COOKED 2 /* 'cooked' device */
2324 extern void adbdev_init(void);
2326 struct adbdev_state {
2327 struct adb_request req;
2330 static DECLARE_WAIT_QUEUE_HEAD(adb_wait);
2332 static int adb_wait_reply(struct adbdev_state *state, struct file *file)
2334 int ret = 0;
2335 DECLARE_WAITQUEUE(wait,current);
2337 __set_current_state(TASK_INTERRUPTIBLE);
2338 add_wait_queue(&adb_wait, &wait);
2340 while (!state->req.got_reply) {
2341 if (file->f_flags & O_NONBLOCK) {
2342 ret = -EAGAIN;
2343 break;
2345 if (signal_pending(current)) {
2346 ret = -ERESTARTSYS;
2347 break;
2349 schedule();
2352 __set_current_state(TASK_RUNNING);
2353 remove_wait_queue(&adb_wait, &wait);
2355 return ret;
2358 static void adb_write_done(struct adb_request *req)
2360 if (!req->got_reply) {
2361 req->reply_len = 0;
2362 req->got_reply = 1;
2364 wake_up_interruptible(&adb_wait);
2367 struct file_operations *adb_raw[16];
2368 struct file_operations *adb_buffered[16];
2369 struct file_operations *adb_cooked[16];
2371 static int adb_open(struct inode *inode, struct file *file)
2373 int adb_type, adb_subtype;
2374 struct adbdev_state *state;
2376 if (MINOR(inode->i_rdev) > ADB_MAX_MINOR)
2377 return -ENXIO;
2379 switch (MINOR(inode->i_rdev) >> ADB_TYPE_SHIFT) {
2380 case ADB_TYPE_RAW:
2381 /* see code below */
2382 break;
2383 case ADB_TYPE_BUFF:
2384 /* TBI */
2385 return -ENXIO;
2386 case ADB_TYPE_COOKED:
2387 /* subtypes such as kbd, mouse, ... */
2388 adb_subtype = MINOR(inode->i_rdev) & ~ADB_TYPE_SHIFT;
2389 if ((file->f_op = adb_cooked[adb_subtype]))
2390 return file->f_op->open(inode,file);
2391 else
2392 return -ENODEV;
2395 state = kmalloc(sizeof(struct adbdev_state), GFP_KERNEL);
2396 if (state == 0)
2397 return -ENOMEM;
2398 file->private_data = state;
2399 state->req.reply_expected = 0;
2400 return 0;
2403 static void adb_release(struct inode *inode, struct file *file)
2405 struct adbdev_state *state = file->private_data;
2407 if (state) {
2408 file->private_data = NULL;
2409 if (state->req.reply_expected && !state->req.got_reply)
2410 if (adb_wait_reply(state, file))
2411 return;
2412 kfree(state);
2414 return;
2417 static int adb_lseek(struct inode *inode, struct file *file,
2418 off_t offset, int origin)
2420 return -ESPIPE;
2423 static int adb_read(struct inode *inode, struct file *file,
2424 char *buf, int count)
2426 int ret;
2427 struct adbdev_state *state = file->private_data;
2429 if (count < 2)
2430 return -EINVAL;
2431 if (count > sizeof(state->req.reply))
2432 count = sizeof(state->req.reply);
2433 ret = verify_area(VERIFY_WRITE, buf, count);
2434 if (ret)
2435 return ret;
2437 if (!state->req.reply_expected)
2438 return 0;
2440 ret = adb_wait_reply(state, file);
2441 if (ret)
2442 return ret;
2444 state->req.reply_expected = 0;
2445 ret = state->req.reply_len;
2446 copy_to_user(buf, state->req.reply, ret);
2448 return ret;
2451 static int adb_write(struct inode *inode, struct file *file,
2452 const char *buf, int count)
2454 int ret, i;
2455 struct adbdev_state *state = file->private_data;
2457 if (count < 2 || count > sizeof(state->req.data))
2458 return -EINVAL;
2459 ret = verify_area(VERIFY_READ, buf, count);
2460 if (ret)
2461 return ret;
2463 if (state->req.reply_expected && !state->req.got_reply) {
2464 /* A previous request is still being processed.
2465 Wait for it to finish. */
2466 ret = adb_wait_reply(state, file);
2467 if (ret)
2468 return ret;
2471 state->req.nbytes = count;
2472 state->req.done = adb_write_done;
2473 state->req.got_reply = 0;
2474 copy_from_user(state->req.data, buf, count);
2475 #if 0
2476 switch (adb_hardware) {
2477 case ADB_NONE:
2478 return -ENXIO;
2479 case ADB_VIACUDA:
2480 state->req.reply_expected = 1;
2481 cuda_send_request(&state->req);
2482 break;
2483 default:
2484 #endif
2485 if (state->req.data[0] != ADB_PACKET)
2486 return -EINVAL;
2487 for (i = 1; i < state->req.nbytes; ++i)
2488 state->req.data[i] = state->req.data[i+1];
2489 state->req.reply_expected =
2490 ((state->req.data[0] & 0xc) == 0xc);
2491 adb_send_request(&state->req);
2492 #if 0
2493 break;
2495 #endif
2497 return count;
2500 static struct file_operations adb_fops = {
2501 adb_lseek,
2502 adb_read,
2503 adb_write,
2504 NULL, /* no readdir */
2505 NULL, /* no poll yet */
2506 NULL, /* no ioctl yet */
2507 NULL, /* no mmap */
2508 adb_open,
2509 NULL, /* flush */
2510 adb_release
2513 int adbdev_register(int subtype, struct file_operations *fops)
2515 if (subtype < 0 || subtype > 15)
2516 return -EINVAL;
2517 if (adb_cooked[subtype])
2518 return -EBUSY;
2519 adb_cooked[subtype] = fops;
2520 return 0;
2523 int adbdev_unregister(int subtype)
2525 if (subtype < 0 || subtype > 15)
2526 return -EINVAL;
2527 if (!adb_cooked[subtype])
2528 return -ENODEV;
2529 adb_cooked[subtype] = NULL;
2530 return 0;
2533 void adbdev_init()
2535 if (register_chrdev(ADB_MAJOR, "adb", &adb_fops))
2536 printk(KERN_ERR "adb: unable to get major %d\n", ADB_MAJOR);
2540 #if 0 /* old ADB device */
2543 * Here are the file operations we export for /dev/adb.
2546 #define ADB_MINOR 140 /* /dev/adb is c 10 140 */
2548 extern void adbdev_inits(void);
2550 struct adbdev_state {
2551 struct adb_request req;
2554 static DECLARE_WAIT_QUEUE_HEAD(adb_wait);
2556 static int adb_wait_reply(struct adbdev_state *state, struct file *file)
2558 int ret = 0;
2559 DECLARE_WAITQUEUE(wait, current);
2561 #if (ADBDEBUG & ADBDEBUG_DEVICE)
2562 printk("ADB request: wait_reply (blocking ... \n");
2563 #endif
2565 __set_current_state(TASK_INTERRUPTIBLE);
2566 add_wait_queue(&adb_wait, &wait);
2568 while (!state->req.got_reply) {
2569 if (file->f_flags & O_NONBLOCK) {
2570 ret = -EAGAIN;
2571 break;
2573 if (signal_pending(current)) {
2574 ret = -ERESTARTSYS;
2575 break;
2577 schedule();
2580 __set_current_state(TASK_RUNNING);
2581 remove_wait_queue(&adb_wait, &wait);
2583 return ret;
2586 static void adb_write_done(struct adb_request *req)
2588 if (!req->got_reply) {
2589 req->reply_len = 0;
2590 req->got_reply = 1;
2592 wake_up_interruptible(&adb_wait);
2595 static int adb_open(struct inode *inode, struct file *file)
2597 struct adbdev_state *state;
2599 state = kmalloc(sizeof(struct adbdev_state), GFP_KERNEL);
2600 if (state == 0)
2601 return -ENOMEM;
2602 file->private_data = state;
2603 state->req.reply_expected = 0;
2604 return 0;
2607 static void adb_release(struct inode *inode, struct file *file)
2609 struct adbdev_state *state = file->private_data;
2611 if (state) {
2612 file->private_data = NULL;
2613 if (state->req.reply_expected && !state->req.got_reply)
2614 if (adb_wait_reply(state, file))
2615 return;
2616 kfree(state);
2618 return;
2621 static int adb_lseek(struct inode *inode, struct file *file,
2622 off_t offset, int origin)
2624 return -ESPIPE;
2627 static int adb_read(struct inode *inode, struct file *file,
2628 char *buf, int count)
2630 int ret;
2631 struct adbdev_state *state = file->private_data;
2633 if (count < 2)
2634 return -EINVAL;
2635 if (count > sizeof(state->req.reply))
2636 count = sizeof(state->req.reply);
2637 ret = verify_area(VERIFY_WRITE, buf, count);
2638 if (ret)
2639 return ret;
2641 if (!state->req.reply_expected)
2642 return 0;
2644 ret = adb_wait_reply(state, file);
2645 if (ret)
2646 return ret;
2648 ret = state->req.reply_len;
2649 memcpy_tofs(buf, state->req.reply, ret);
2650 state->req.reply_expected = 0;
2652 return ret;
2655 static int adb_write(struct inode *inode, struct file *file,
2656 const char *buf, int count)
2658 int ret;
2659 struct adbdev_state *state = file->private_data;
2661 if (count < 2 || count > sizeof(state->req.data))
2662 return -EINVAL;
2663 ret = verify_area(VERIFY_READ, buf, count);
2664 if (ret)
2665 return ret;
2667 if (state->req.reply_expected && !state->req.got_reply) {
2668 /* A previous request is still being processed.
2669 Wait for it to finish. */
2670 ret = adb_wait_reply(state, file);
2671 if (ret)
2672 return ret;
2675 state->req.nbytes = count;
2676 state->req.done = adb_write_done;
2677 memcpy_fromfs(state->req.data, buf, count);
2678 state->req.reply_expected = 1;
2679 state->req.got_reply = 0;
2680 adb_send_request(&state->req);
2682 return count;
2685 static struct file_operations adb_fops = {
2686 adb_lseek,
2687 adb_read,
2688 adb_write,
2689 NULL, /* no readdir */
2690 NULL, /* no select */
2691 NULL, /* no ioctl */
2692 NULL, /* no mmap */
2693 adb_open,
2694 NULL, /* flush */
2695 adb_release
2698 static struct miscdevice adb_dev = {
2699 ADB_MINOR,
2700 "adb",
2701 &adb_fops
2704 void adbdev_init(void)
2706 misc_register(&adb_dev);
2709 #endif /* old ADB device */