Import 2.1.116pre2
[davej-history.git] / arch / m68k / mac / adb-bus.c
blob0f41bc6aaba905ae502de1bdeaabd9583a215ed0
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 <stdarg.h>
17 #include <linux/types.h>
18 #include <linux/errno.h>
19 #include <linux/miscdevice.h>
20 #include <linux/kernel.h>
21 #include <linux/delay.h>
22 #include <linux/sched.h>
23 #include <linux/malloc.h>
24 #include <linux/mm.h>
25 #include "via6522.h"
27 #include <asm/uaccess.h>
28 #include <asm/io.h>
29 #include <asm/adb.h>
30 #include <asm/system.h>
31 #include <asm/segment.h>
32 #include <asm/setup.h>
33 #include <asm/macintosh.h>
34 #include <asm/macints.h>
37 #define MACII /* For now - will be a switch */
39 /* Bits in B data register: all active low */
40 #define TREQ 0x08 /* Transfer request (input) */
41 #define TACK 0x10 /* Transfer acknowledge (output) */
42 #define TIP 0x20 /* Transfer in progress (output) */
44 /* Bits in B data register: ADB transaction states MacII */
45 #define ST_MASK 0x30 /* mask for selecting ADB state bits */
46 /* ADB transaction states according to GMHW */
47 #define ST_CMD 0x00 /* ADB state: command byte */
48 #define ST_EVEN 0x10 /* ADB state: even data byte */
49 #define ST_ODD 0x20 /* ADB state: odd data byte */
50 #define ST_IDLE 0x30 /* ADB state: idle, nothing to send */
52 /* Bits in ACR */
53 #define SR_CTRL 0x1c /* Shift register control bits */
54 #ifdef USE_ORIG
55 #define SR_EXT 0x1c /* Shift on external clock */
56 #else
57 #define SR_EXT 0x0c /* Shift on external clock */
58 #endif
59 #define SR_OUT 0x10 /* Shift out if 1 */
61 /* Bits in IFR and IER */
62 #define IER_SET 0x80 /* set bits in IER */
63 #define IER_CLR 0 /* clear bits in IER */
64 #define SR_INT 0x04 /* Shift register full/empty */
65 #define SR_DATA 0x08 /* Shift register data */
66 #define SR_CLOCK 0x10 /* Shift register clock */
68 /* JRT */
69 #define ADB_DELAY 150
71 static struct adb_handler {
72 void (*handler)(unsigned char *, int, struct pt_regs *);
73 } adb_handler[16];
75 static enum adb_state {
76 idle,
77 sent_first_byte,
78 sending,
79 reading,
80 read_done,
81 awaiting_reply
82 } adb_state;
84 static struct adb_request *current_req;
85 static struct adb_request *last_req;
86 static unsigned char cuda_rbuf[16];
87 static unsigned char *reply_ptr;
88 static int reply_len;
89 static int reading_reply;
90 static int data_index;
91 static int first_byte;
92 static int prefix_len;
94 static int status = ST_IDLE|TREQ;
95 static int last_status;
97 static int driver_running = 0;
99 /*static int adb_delay;*/
100 int in_keybinit = 1;
102 static void adb_start(void);
103 extern void adb_interrupt(int irq, void *arg, struct pt_regs *regs);
104 extern void adb_cuda_interrupt(int irq, void *arg, struct pt_regs *regs);
105 extern void adb_clock_interrupt(int irq, void *arg, struct pt_regs *regs);
106 extern void adb_data_interrupt(int irq, void *arg, struct pt_regs *regs);
107 static void adb_input(unsigned char *buf, int nb, struct pt_regs *regs);
109 static void adb_hw_setup_IIsi(void);
110 static void adb_hw_setup_cuda(void);
113 * debug level 10 required for ADB logging (should be && debug_adb, ideally)
116 extern int console_loglevel;
119 * Misc. defines for testing - should go to header :-(
122 #define ADBDEBUG_STATUS (1)
123 #define ADBDEBUG_STATE (2)
124 #define ADBDEBUG_READ (4)
125 #define ADBDEBUG_WRITE (8)
126 #define ADBDEBUG_START (16)
127 #define ADBDEBUG_RETRY (32)
128 #define ADBDEBUG_POLL (64)
129 #define ADBDEBUG_INT (128)
130 #define ADBDEBUG_PROT (256)
131 #define ADBDEBUG_SRQ (512)
132 #define ADBDEBUG_REQUEST (1024)
133 #define ADBDEBUG_INPUT (2048)
134 #define ADBDEBUG_DEVICE (4096)
136 #define ADBDEBUG_IISI (8192)
139 /*#define DEBUG_ADB*/
141 #ifdef DEBUG_ADB
142 #define ADBDEBUG (ADBDEBUG_INPUT | ADBDEBUG_READ | ADBDEBUG_START | ADBDEBUG_WRITE | ADBDEBUG_SRQ | ADBDEBUG_REQUEST)
143 #else
144 #define ADBDEBUG (0)
145 #endif
147 #define TRY_CUDA
149 void adb_bus_init(void)
151 unsigned long flags;
152 unsigned char c, i;
154 save_flags(flags);
155 cli();
158 * Setup ADB
161 switch(macintosh_config->adb_type)
164 case MAC_ADB_II:
165 printk("adb: MacII style keyboard/mouse driver.\n");
166 /* Set the lines up. We want TREQ as input TACK|TIP as output */
167 via_write(via1, vDirB, ((via_read(via1,vDirB)|TACK|TIP)&~TREQ));
169 * Docs suggest TREQ should be output - that seems nuts
170 * BSD agrees here :-)
171 * Setup vPCR ??
174 #ifdef USE_ORIG
175 /* Lower the bus signals (MacII is active low it seems ???) */
176 via_write(via1, vBufB, via_read(via1, vBufB)&~TACK);
177 #else
178 /* Corresponding state: idle (clear state bits) */
179 via_write(via1, vBufB, (via_read(via1, vBufB)&~ST_MASK)|ST_IDLE);
180 last_status = (via_read(via1, vBufB)&~ST_MASK);
181 #endif
182 /* Shift register on input */
183 c=via_read(via1, vACR);
184 c&=~SR_CTRL; /* Clear shift register bits */
185 c|=SR_EXT; /* Shift on external clock; out or in? */
186 via_write(via1, vACR, c);
187 /* Wipe any pending data and int */
188 via_read(via1, vSR);
190 /* This is interrupts on enable SR for keyboard */
191 via_write(via1, vIER, IER_SET|SR_INT);
192 /* This clears the interrupt bit */
193 via_write(via1, vIFR, SR_INT);
196 * Ok we probably ;) have a ready to use adb bus. Its also
197 * hopefully idle (Im assuming the mac didnt leave a half
198 * complete transaction on booting us).
201 request_irq(IRQ_MAC_ADB, adb_interrupt, IRQ_FLG_LOCK,
202 "adb interrupt", adb_interrupt);
203 adb_state = idle;
204 break;
206 * Unsupported; but later code doesn't notice !!
208 case MAC_ADB_CUDA:
209 printk("adb: CUDA interface.\n");
210 #if 0
211 /* don't know what to set up here ... */
212 adb_state = idle;
213 /* Set the lines up. We want TREQ as input TACK|TIP as output */
214 via_write(via1, vDirB, ((via_read(via1,vDirB)|TACK|TIP)&~TREQ));
215 #endif
216 adb_hw_setup_cuda();
217 adb_state = idle;
218 request_irq(IRQ_MAC_ADB, adb_cuda_interrupt, IRQ_FLG_LOCK,
219 "adb CUDA interrupt", adb_cuda_interrupt);
220 break;
221 case MAC_ADB_IISI:
222 printk("adb: Using IIsi hardware.\n");
223 printk("\tDEBUG_JRT\n");
224 /* Set the lines up. We want TREQ as input TACK|TIP as output */
225 via_write(via1, vDirB, ((via_read(via1,vDirB)|TACK|TIP)&~TREQ));
228 * MSch: I'm pretty sure the setup is mildly wrong
229 * for the IIsi.
231 /* Initial state: idle (clear state bits) */
232 via_write(via1, vBufB, (via_read(via1, vBufB) & ~(TIP|TACK)) );
233 last_status = (via_read(via1, vBufB)&~ST_MASK);
234 /* Shift register on input */
235 c=via_read(via1, vACR);
236 c&=~SR_CTRL; /* Clear shift register bits */
237 c|=SR_EXT; /* Shift on external clock; out or in? */
238 via_write(via1, vACR, c);
239 /* Wipe any pending data and int */
240 via_read(via1, vSR);
242 /* This is interrupts on enable SR for keyboard */
243 via_write(via1, vIER, IER_SET|SR_INT);
244 /* This clears the interrupt bit */
245 via_write(via1, vIFR, SR_INT);
247 /* get those pesky clock ticks we missed while booting */
248 for ( i = 0; i < 30; i++) {
249 udelay(ADB_DELAY);
250 adb_hw_setup_IIsi();
251 udelay(ADB_DELAY);
252 if (via_read(via1, vBufB) & TREQ)
253 break;
256 * Ok we probably ;) have a ready to use adb bus. Its also
258 request_irq(IRQ_MAC_ADB, adb_cuda_interrupt, IRQ_FLG_LOCK,
259 "adb interrupt", adb_cuda_interrupt);
260 adb_state = idle;
261 break;
262 default:
263 printk("adb: Unknown hardware interface.\n");
264 nosupp:
265 printk("adb: Interface unsupported.\n");
266 restore_flags(flags);
267 return;
271 * XXX: interrupt only registered if supported HW !!
272 * -> unsupported HW will just time out on keyb_init!
274 #if 0
275 request_irq(IRQ_MAC_ADB, adb_interrupt, IRQ_FLG_LOCK,
276 "adb interrupt", adb_interrupt);
277 #endif
278 #ifdef DEBUG_ADB_INTS
279 request_irq(IRQ_MAC_ADB_CL, adb_clock_interrupt, IRQ_FLG_LOCK,
280 "adb clock interrupt", adb_clock_interrupt);
281 request_irq(IRQ_MAC_ADB_SD, adb_data_interrupt, IRQ_FLG_LOCK,
282 "adb data interrupt", adb_data_interrupt);
283 #endif
285 printk("adb: init done.\n");
286 restore_flags(flags);
289 void adb_hw_setup_cuda(void)
291 int x;
292 unsigned long flags;
294 printk("CUDA: HW Setup:");
296 save_flags(flags);
297 cli();
299 if (console_loglevel == 10)
300 printk(" 1,");
302 /* Set the direction of the cuda signals, TIP+TACK are output TREQ is an input */
303 via_write( via1, vDirB, via_read( via1, vDirB ) | TIP | TACK );
304 via_write( via1, vDirB, via_read( via1, vDirB ) & ~TREQ );
306 if (console_loglevel == 10)
307 printk("2,");
309 /* Set the clock control. Set to shift data in by external clock CB1 */
310 via_write( via1, vACR, ( via_read(via1, vACR ) | SR_EXT ) & ~SR_OUT );
312 if (console_loglevel == 10)
313 printk("3,");
315 /* Clear any possible Cuda interrupt */
316 x = via_read( via1, vSR );
318 if (console_loglevel == 10)
319 printk("4,");
321 /* Terminate transaction and set idle state */
322 via_write( via1, vBufB, via_read( via1, vBufB ) | TIP | TACK );
324 if (console_loglevel == 10)
325 printk("5,");
327 /* Delay 4 mS for ADB reset to complete */
328 udelay(4000);
330 if (console_loglevel == 10)
331 printk("6,");
333 /* Clear pending interrupts... */
334 x = via_read( via1, vSR );
336 if (console_loglevel == 10)
337 printk("7,");
338 /* Issue a sync transaction, TACK asserted while TIP negated */
339 via_write( via1, vBufB, via_read( via1, vBufB ) & ~TACK );
341 if (console_loglevel == 10)
342 printk("8,");
344 /* Wait for the sync acknowledgement, Cuda to assert TREQ */
345 while( ( via_read( via1, vBufB ) & TREQ ) != 0 )
346 barrier();
348 if (console_loglevel == 10)
349 printk("9,");
351 /* Wait for the sync acknowledment interrupt */
352 while( ( via_read( via1, vIFR ) & SR_INT ) == 0 )
353 barrier();
355 if (console_loglevel == 10)
356 printk("10,");
358 /* Clear pending interrupts... */
359 x = via_read( via1, vSR );
361 if (console_loglevel == 10)
362 printk("11,");
364 /* Terminate the sync cycle by negating TACK */
365 via_write( via1, vBufB, via_read( via1, vBufB ) | TACK );
367 if (console_loglevel == 10)
368 printk("12,");
370 /* Wait for the sync termination acknowledgement, Cuda to negate TREQ */
371 while( ( via_read( via1, vBufB ) & TREQ ) == 0 )
372 barrier();
374 if (console_loglevel == 10)
375 printk("13,");
377 /* Wait for the sync termination acknowledment interrupt */
378 while( ( via_read( via1, vIFR ) & SR_INT ) == 0 )
379 barrier();
381 if (console_loglevel == 10)
382 printk("14,");
384 /* Terminate transaction and set idle state, TIP+TACK negate */
385 via_write( via1, vBufB, via_read( via1, vBufB ) | TIP );
387 if (console_loglevel == 10)
388 printk("15 !");
390 /* Clear pending interrupts... */
391 x = via_read( via1, vSR );
393 restore_flags(flags);
395 printk("\nCUDA: HW Setup done!\n");
398 void adb_hw_setup_IIsi(void)
400 int dummy;
401 long poll_timeout;
403 printk("adb_IIsi: cleanup!\n");
405 /* ??? */
406 udelay(ADB_DELAY);
408 /* disable SR int. */
409 via_write(via1, vIER, IER_CLR|SR_INT);
410 /* set SR to shift in */
411 via_write(via1, vACR, via_read(via1, vACR ) & ~SR_OUT);
413 /* this is required, especially on faster machines */
414 udelay(ADB_DELAY);
416 if (!(via_read(via1, vBufB) & TREQ)) { /* IRQ on */
417 /* start frame */
418 via_write(via1, vBufB,via_read(via1,vBufB) | TIP);
420 while (1) {
421 /* poll for ADB interrupt and watch for timeout */
422 /* if time out, keep going in hopes of not hanging the
423 * ADB chip - I think */
424 poll_timeout = ADB_DELAY * 5;
425 while ( !(via_read(via1, vIFR) & SR_INT)
426 && (poll_timeout-- > 0) )
427 dummy = via_read(via1, vBufB);
429 dummy = via_read(via1, vSR); /* reset interrupt flag */
431 /* perhaps put in a check here that ignores all data
432 * after the first ADB_MAX_MSG_LENGTH bytes ??? */
434 /* end of frame reached ?? */
435 if (via_read(via1, vBufB) & TREQ)
436 break;
438 /* set ACK */
439 via_write(via1,vBufB,via_read(via1, vBufB) | TACK);
440 /* delay */
441 udelay(ADB_DELAY);
442 /* clear ACK */
443 via_write(via1,vBufB,via_read(via1, vBufB) & ~TACK);
445 /* end frame */
446 via_write(via1, vBufB,via_read(via1,vBufB) & ~TIP);
447 /* probably don't need to delay this long */
448 udelay(ADB_DELAY);
450 /* re-enable SR int. */
451 via_write(via1, vIER, IER_SET|SR_INT);
454 #define WAIT_FOR(cond, what) \
455 do { \
456 for (x = 1000; !(cond); --x) { \
457 if (x == 0) { \
458 printk("Timeout waiting for " what); \
459 return 0; \
461 __delay(100*160); \
463 } while (0)
466 * Construct and send an adb request
467 * This function is the main entry point into the ADB driver from
468 * kernel code; it takes the request data supplied and populates the
469 * adb_request structure.
470 * In order to keep this interface independent from any assumption about
471 * the underlying ADB hardware, we take requests in CUDA format here,
472 * the ADB packet 'prefixed' with a packet type code.
473 * Non-CUDA hardware is confused by this, so we strip the packet type
474 * here depending on hardware type ...
476 int adb_request(struct adb_request *req, void (*done)(struct adb_request *),
477 int nbytes, ...)
479 va_list list;
480 int i, start;
482 va_start(list, nbytes);
485 * skip first byte if not CUDA
487 if (macintosh_config->adb_type == MAC_ADB_II) {
488 start = va_arg(list, int);
489 nbytes--;
491 req->nbytes = nbytes;
492 req->done = done;
493 #if (ADBDEBUG & ADBDEBUG_REQUEST)
494 if (console_loglevel == 10)
495 printk("adb_request, data bytes: ");
496 #endif
497 for (i = 0; i < nbytes; ++i) {
498 req->data[i] = va_arg(list, int);
499 #if (ADBDEBUG & ADBDEBUG_REQUEST)
500 if (console_loglevel == 10)
501 printk("%x ", req->data[i]);
502 #endif
504 #if (ADBDEBUG & ADBDEBUG_REQUEST)
505 if (console_loglevel == 10)
506 printk(" !\n");
507 #endif
508 va_end(list);
510 * XXX: This might be fatal if no reply is generated (i.e. Listen) !
511 * Currently, the interrupt handler 'fakes' a reply on non-TALK
512 * commands for this reason.
513 * Also, we need a CUDA_AUTOPOLL emulation here for non-CUDA
514 * Macs, and some mechanism to remember the last issued TALK
515 * request for resending it repeatedly on timeout!
517 req->reply_expected = 1;
518 return adb_send_request(req);
522 * Construct an adb request for later sending
523 * This function only populates the adb_request structure, without
524 * actually queueing it.
525 * Reason: Poll requests and Talk requests need to be handled in a way
526 * different from 'user' requests; no reply_expected is set and
527 * Poll requests need to be placed at the head of the request queue.
528 * Using adb_request results in implicit queueing at the tail of the
529 * request queue (duplicating the Poll) with reply_expected set.
530 * No adjustment of packet data is necessary, as this mechanisnm is not
531 * used by CUDA hardware (Autopoll used instead).
533 int adb_build_request(struct adb_request *req, void (*done)(struct adb_request *),
534 int nbytes, ...)
536 va_list list;
537 int i;
539 req->nbytes = nbytes;
540 req->done = done;
541 va_start(list, nbytes);
542 #if (ADBDEBUG & ADBDEBUG_REQUEST)
543 if (console_loglevel == 10)
544 printk("adb__build_request, data bytes: ");
545 #endif
547 * skip first byte if not CUDA ?
549 for (i = 0; i < nbytes; ++i) {
550 req->data[i] = va_arg(list, int);
551 #if (ADBDEBUG & ADBDEBUG_REQUEST)
552 if (console_loglevel == 10)
553 printk("%x ", req->data[i]);
554 #endif
556 #if (ADBDEBUG & ADBDEBUG_REQUEST)
557 if (console_loglevel == 10)
558 printk(" !\n");
559 #endif
560 va_end(list);
562 req->reply_expected = 0;
563 return 0;
567 * Send an ADB poll (Talk, tagged on the front of the request queue)
569 void adb_queue_poll(void)
571 static int pod=0;
572 static int in_poll=0;
573 static struct adb_request r;
574 unsigned long flags;
576 if(in_poll)
577 printk("Double poll!\n");
579 in_poll++;
580 pod++;
581 if(pod>7) /* 15 */
582 pod=0;
584 #if (ADBDEBUG & ADBDEBUG_POLL)
585 if (console_loglevel == 10)
586 printk("adb: Polling %d\n",pod);
587 #endif
589 if (macintosh_config->adb_type == MAC_ADB_II)
590 /* XXX: that's a TALK, register 0, MacII version */
591 adb_build_request(&r,NULL, 1, (pod<<4|0xC));
592 else
593 /* CUDA etc. version */
594 adb_build_request(&r,NULL, 2, 0, (pod<<4|0xC));
596 r.reply_expected=0;
597 r.done=NULL;
598 r.sent=0;
599 r.got_reply=0;
600 r.reply_len=0;
601 save_flags(flags);
602 cli();
603 /* Poll inserted at head of queue ... */
604 r.next=current_req;
605 current_req=&r;
606 restore_flags(flags);
607 adb_start();
608 in_poll--;
612 * Send an ADB retransmit (Talk, appended to the request queue)
614 void adb_retransmit(int device)
616 static int in_retransmit=0;
617 static struct adb_request rt;
618 unsigned long flags;
620 if(in_retransmit)
621 printk("Double retransmit!\n");
623 in_retransmit++;
625 #if (ADBDEBUG & ADBDEBUG_POLL)
626 if (console_loglevel == 10)
627 printk("adb: Sending retransmit: %d\n", device);
628 #endif
630 /* MacII version */
631 adb_build_request(&rt,NULL, 1, (device<<4|0xC));
633 rt.reply_expected = 0;
634 rt.done = NULL;
635 rt.sent = 0;
636 rt.got_reply = 0;
637 rt.reply_len = 0;
638 rt.next = NULL;
640 save_flags(flags);
641 cli();
643 /* Retransmit inserted at tail of queue ... */
645 if (current_req != NULL)
647 last_req->next = &rt;
648 last_req = &rt;
650 else
652 current_req = &rt;
653 last_req = &rt;
656 /* always restart driver (send_retransmit used in place of adb_start!)*/
658 if (adb_state == idle)
659 adb_start();
661 restore_flags(flags);
662 in_retransmit--;
666 * Queue an ADB request; start ADB transfer if necessary
668 int adb_send_request(struct adb_request *req)
670 unsigned long flags;
672 req->next = 0;
673 req->sent = 0;
674 req->got_reply = 0;
675 req->reply_len = 0;
676 save_flags(flags);
677 cli();
679 if (current_req != NULL)
681 last_req->next = req;
682 last_req = req;
684 else
686 current_req = req;
687 last_req = req;
688 if (adb_state == idle)
689 adb_start();
692 restore_flags(flags);
693 return 0;
696 static int nclock, ndata;
698 static int need_poll = 0;
699 static int command_byte = 0;
700 static int last_reply = 0;
701 static int last_active = 0;
703 static struct adb_request *retry_req;
706 * Start sending ADB packet
708 static void adb_start(void)
710 unsigned long flags;
711 struct adb_request *req;
714 * We get here on three 'sane' conditions:
715 * 1) called from send_adb_request, if adb_state == idle
716 * 2) called from within adb_interrupt, if adb_state == idle
717 * (after receiving, or after sending a LISTEN)
718 * 3) called from within adb_interrupt, if adb_state == sending
719 * and no reply is expected (immediate next command).
720 * Maybe we get here on SRQ as well ??
723 /* get the packet to send */
724 req = current_req;
725 /* assert adb_state == idle */
726 if (adb_state != idle) {
727 printk("ADB: adb_start called while driver busy (%p %x %x)!\n",
728 req, adb_state, via_read(via1, vBufB)&(ST_MASK|TREQ));
729 return;
731 if (req == 0)
732 return;
733 save_flags(flags);
734 cli();
736 #if (ADBDEBUG & ADBDEBUG_START)
737 if (console_loglevel == 10)
738 printk("adb_start: request %p ", req);
739 #endif
741 nclock = 0;
742 ndata = 0;
745 * IRQ signaled ?? (means ADB controller wants to send, or might
746 * be end of packet if we were reading)
748 if ((via_read(via1, vBufB) & TREQ) == 0)
750 switch(macintosh_config->adb_type)
753 * FIXME - we need to restart this on a timer
754 * or a collision at boot hangs us.
755 * Never set adb_state to idle here, or adb_start
756 * won't be called again from send_request!
757 * (need to re-check other cases ...)
759 case MAC_ADB_CUDA:
760 /* printk("device busy - fail\n"); */
761 restore_flags(flags);
762 /* a byte is coming in from the CUDA */
763 return;
764 case MAC_ADB_IISI:
765 printk("adb_start: device busy - fail\n");
766 retry_req = req;
767 restore_flags(flags);
768 return;
769 case MAC_ADB_II:
771 * if the interrupt handler set the need_poll
772 * flag, it's hopefully a SRQ poll or re-Talk
773 * so we try to send here anyway
775 if (!need_poll) {
776 printk("device busy - retry %p state %d status %x!\n",
777 req, adb_state, via_read(via1, vBufB)&(ST_MASK|TREQ));
778 retry_req = req;
779 /* set ADB status here ? */
780 restore_flags(flags);
781 return;
782 } else {
783 #if (ADBDEBUG & ADBDEBUG_START)
784 if (console_loglevel == 10)
785 printk("device busy - polling; state %d status %x!\n",
786 adb_state, via_read(via1, vBufB)&(ST_MASK|TREQ));
787 #endif
788 need_poll = 0;
789 break;
794 #if 0
796 * Bus idle ?? Not sure about this one; SRQ might need ST_CMD here!
797 * OTOH: setting ST_CMD in the interrupt routine would make the
798 * ADB contoller shift in before this routine starts shifting out ...
800 if ((via_read(via1, vBufB)&ST_MASK) != ST_IDLE)
802 #if (ADBDEBUG & ADBDEBUG_STATE)
803 if (console_loglevel == 10)
804 printk("ADB bus not idle (%x), retry later!\n",
805 via_read(via1, vBufB)&(ST_MASK|TREQ));
806 #endif
807 retry_req = req;
808 restore_flags(flags);
809 return;
811 #endif
814 * Another retry pending? (sanity check)
816 if (retry_req) {
817 #if (ADBDEBUG & ADBDEBUG_RETRY)
818 if (console_loglevel == 10)
819 if (retry_req == req)
820 /* new requests are appended at tail of request queue */
821 printk("adb_start: retry %p pending ! \n", req);
822 else
823 /* poll requests are added to the head of queue */
824 printk("adb_start: retry %p pending, req %p (poll?) current! \n",
825 retry_req, req);
826 #endif
827 retry_req = NULL;
831 * Seems OK, go for it!
833 switch(macintosh_config->adb_type)
835 case MAC_ADB_CUDA:
836 /* store command byte (first byte is 'type' byte) */
837 command_byte = req->data[1];
838 /* set the shift register to shift out and send a byte */
839 via_write(via1, vACR, via_read(via1, vACR)|SR_OUT);
840 via_write(via1, vSR, req->data[0]);
841 via_write(via1, vBufB, via_read(via1, vBufB)&~TIP);
842 break;
843 case MAC_ADB_IISI:
844 /* store command byte (first byte is 'type' byte) */
845 command_byte = req->data[1];
846 /* set ADB state to 'active' */
847 via_write(via1, vBufB, via_read(via1, vBufB) | TIP);
848 /* switch ACK off (in case it was left on) */
849 via_write(via1, vBufB, via_read(via1, vBufB) & ~TACK);
850 /* set the shift register to shift out and send a byte */
851 via_write(via1, vACR, via_read(via1, vACR) | SR_OUT);
852 via_write(via1, vSR, req->data[0]);
853 /* signal 'byte ready' */
854 via_write(via1, vBufB, via_read(via1, vBufB) | TACK);
855 break;
856 case MAC_ADB_II:
857 /* store command byte */
858 command_byte = req->data[0];
859 /* Output mode */
860 via_write(via1, vACR, via_read(via1, vACR)|SR_OUT);
861 /* Load data */
862 via_write(via1, vSR, req->data[0]);
863 #ifdef USE_ORIG
864 /* Turn off TIP/TACK - this should tell the external logic to
865 start the external shift clock */
866 /* via_write(via1, vBufB, via_read(via1, vBufB)&~(TIP|TACK));*/
867 via_write(via1, vBufB, via_read(via1, vBufB)|(TIP|TACK));
868 #else
869 /* set ADB state to 'command' */
870 via_write(via1, vBufB, (via_read(via1, vBufB)&~ST_MASK)|ST_CMD);
871 #endif
872 break;
874 adb_state = sent_first_byte;
875 data_index = 1;
876 #if (ADBDEBUG & ADBDEBUG_START)
877 if (console_loglevel == 10)
878 printk("sent first byte of %d: %x, (%x %x) ... ",
879 req->nbytes, req->data[0], adb_state,
880 (via_read(via1, vBufB) & (ST_MASK|TREQ)) );
881 #endif
882 restore_flags(flags);
886 * Poll the ADB state (maybe obsolete now that interrupt-driven ADB runs)
888 void adb_poll(void)
890 unsigned char c;
891 unsigned long flags;
892 save_flags(flags);
893 cli();
894 c=via_read(via1, vIFR);
895 #if (ADBDEBUG & ADBDEBUG_POLL)
896 #ifdef DEBUG_ADB_INTS
897 if (console_loglevel == 10) {
898 printk("adb_poll: IFR %x state %x cl %d dat %d ",
899 c, adb_state, nclock, ndata);
900 if (c & (SR_CLOCK|SR_DATA)) {
901 if (c & SR_CLOCK)
902 printk("adb clock event ");
903 if (c & SR_DATA)
904 printk("adb data event ");
907 #else
908 if (console_loglevel == 10)
909 printk("adb_poll: IFR %x state %x ",
910 c, adb_state);
911 #endif
912 if (console_loglevel == 10)
913 printk("\r");
914 #endif
915 if (c & SR_INT)
917 #if (ADBDEBUG & ADBDEBUG_POLL)
918 if (console_loglevel == 10)
919 printk("adb_poll: adb interrupt event\n");
920 #endif
921 adb_interrupt(0, 0, 0);
923 restore_flags(flags);
927 * Debugging gimmicks
929 void adb_clock_interrupt(int irq, void *arg, struct pt_regs *regs)
931 nclock++;
934 void adb_data_interrupt(int irq, void *arg, struct pt_regs *regs)
936 ndata++;
940 * The notorious ADB interrupt handler - does all of the protocol handling,
941 * except for starting new send operations. Relies heavily on the ADB
942 * controller sending and receiving data, thereby generating SR interrupts
943 * for us. This means there has to be always activity on the ADB bus, otherwise
944 * the whole process dies and has to be re-kicked by sending TALK requests ...
945 * CUDA-based Macs seem to solve this with the autopoll option, for MacII-type
946 * ADB the problem isn't solved yet (retransmit of the latest active TALK seems
947 * a good choice; either on timeout or on a timer interrupt).
949 * The basic ADB state machine was left unchanged from the original MacII code
950 * by Alan Cox, which was based on the CUDA driver for PowerMac.
951 * The syntax of the ADB status lines seems to be totally different on MacII,
952 * though. MacII uses the states Command -> Even -> Odd -> Even ->...-> Idle for
953 * sending, and Idle -> Even -> Odd -> Even ->...-> Idle for receiving. Start
954 * and end of a receive packet are signaled by asserting /IRQ on the interrupt
955 * line. Timeouts are signaled by a sequence of 4 0xFF, with /IRQ asserted on
956 * every other byte. SRQ is probably signaled by 3 or more 0xFF tacked on the
957 * end of a packet. (Thanks to Guido Koerber for eavesdropping on the ADB
958 * protocol with a logic analyzer!!)
959 * CUDA seems to use /TIP -> /TIP | TACK -> /TIP -> /TIP | TACK ... -> TIP|TACK
960 * for sending, and /TIP -> /TIP | TACK -> /TIP -> /TIP | TACK ... -> TIP for
961 * receiving. No clue how timeouts are handled; SRQ seems to be sent as a
962 * separate packet. Quite a few changes have been made outside the handshake
963 * code, so I don't know if the CUDA code still behaves as before.
965 * Note: As of 21/10/97, the MacII ADB part works including timeout detection
966 * and retransmit (Talk to the last active device). Cleanup of code and
967 * testing of the CUDA functionality is required, though.
968 * Note2: As of 13/12/97, CUDA support is definitely broken ...
969 * Note3: As of 21/12/97, CUDA works on a P475. What was broken? The assumption
970 * that Q700 and Q800 use CUDA :-(
972 * 27/01/98: IIsi driver implemented (thanks to Robert Thompson for the
973 * initial bits). See adb_cuda_interrupts ...
975 * Next TODO: implementation of IIsi ADB protocol (maybe the USE_ORIG
976 * conditionals can be a start?)
978 void adb_interrupt(int irq, void *arg, struct pt_regs *regs)
980 int x, adbdir;
981 unsigned long flags;
982 struct adb_request *req;
984 last_status = status;
986 /* prevent races due to SCSI enabling ints */
987 save_flags(flags);
988 cli();
990 if (driver_running) {
991 restore_flags(flags);
992 return;
995 driver_running = 1;
997 #ifdef USE_ORIG
998 status = (~via_read(via1, vBufB) & (TIP|TREQ)) | (via_read(via1, vACR) & SR_OUT);
999 #else
1000 if (macintosh_config->adb_type==MAC_ADB_CUDA)
1001 status = (~via_read(via1, vBufB) & (TIP|TREQ)) | (via_read(via1, vACR) & SR_OUT);
1002 else
1003 /* status bits (0x8->0x20) and direction (0x10 ??) CLASH !! */
1004 status = (via_read(via1, vBufB) & (ST_MASK|TREQ));
1005 #endif
1006 adbdir = (via_read(via1, vACR) & SR_OUT);
1007 #if (ADBDEBUG & ADBDEBUG_INT)
1008 if (console_loglevel == 10)
1009 printk("adb_interrupt: state=%d status=%x last=%x direction=%x\n",
1010 adb_state, status, last_status, adbdir);
1011 #endif
1013 switch (adb_state)
1015 case idle:
1016 if(macintosh_config->adb_type==MAC_ADB_CUDA)
1018 /* CUDA has sent us the first byte of data - unsolicited */
1019 if (status != TREQ)
1020 printk("cuda: state=idle, status=%x\n", status);
1021 x = via_read(via1, vSR);
1022 via_write(via1, vBufB, via_read(via1,vBufB)&~TIP);
1024 else if(macintosh_config->adb_type==MAC_ADB_IISI)
1026 udelay(150);
1027 /* set SR to IN (??? no byte received else) */
1028 via_write(via1, vACR,via_read(via1, vACR)&~SR_OUT);
1029 /* signal start of frame */
1030 via_write(via1, vBufB, via_read(via1, vBufB) | TIP);
1031 /* read first byte */
1032 x = via_read(via1, vSR);
1033 first_byte = x;
1034 #if (ADBDEBUG & ADBDEBUG_READ)
1035 if (console_loglevel == 10)
1036 printk("adb_macIIsi : receiving unsol. packet: %x (%x %x) ",
1037 x, adb_state, status);
1038 #endif
1039 /* ACK adb chip */
1040 via_write(via1, vBufB, via_read(via1, vBufB) | TACK);
1041 udelay(150);
1042 via_write(via1, vBufB, via_read(via1, vBufB) & ~TACK);
1044 else if(macintosh_config->adb_type==MAC_ADB_II)
1046 #if (ADBDEBUG & ADBDEBUG_STATUS)
1047 if (status == TREQ && !adbdir)
1048 /* that's: not IRQ, idle, input -> weird */
1049 printk("adb_macII: idle, status=%x dir=%x\n",
1050 status, adbdir);
1051 #endif
1052 x = via_read(via1, vSR);
1053 first_byte = x;
1054 #if (ADBDEBUG & ADBDEBUG_READ)
1055 if (console_loglevel == 10)
1056 printk("adb_macII: receiving unsol. packet: %x (%x %x) ",
1057 x, adb_state, status);
1058 #endif
1059 /* set ADB state = even for first data byte */
1060 via_write(via1, vBufB, (via_read(via1, vBufB)&~ST_MASK)|ST_EVEN);
1062 adb_state = reading;
1063 reply_ptr = cuda_rbuf;
1064 reply_len = 0;
1065 reading_reply = 0;
1066 prefix_len = 0;
1067 if (macintosh_config->adb_type==MAC_ADB_II) {
1068 *reply_ptr++ = ADB_PACKET;
1069 *reply_ptr++ = first_byte;
1070 *reply_ptr++ = command_byte; /*first_byte;*/
1071 reply_len = 3;
1072 prefix_len = 3;
1074 break;
1076 case awaiting_reply:
1077 if(macintosh_config->adb_type==MAC_ADB_CUDA)
1079 /* CUDA has sent us the first byte of data of a reply */
1080 if (status != TREQ)
1081 printk("cuda: state=awaiting_reply, status=%x\n", status);
1082 x = via_read(via1, vSR);
1083 via_write(via1,vBufB,
1084 via_read(via1, vBufB)&~TIP);
1086 else if(macintosh_config->adb_type==MAC_ADB_IISI)
1088 /* set SR to IN */
1089 via_write(via1, vACR,via_read(via1, vACR)&~SR_OUT);
1090 /* signal start of frame */
1091 via_write(via1, vBufB, via_read(via1, vBufB) | TIP);
1092 /* read first byte */
1093 x = via_read(via1, vSR);
1094 first_byte = x;
1095 #if (ADBDEBUG & ADBDEBUG_READ)
1096 if (console_loglevel == 10)
1097 printk("adb_macIIsi: reading reply: %x (%x %x) ",
1098 x, adb_state, status);
1099 #endif
1100 #if 0
1101 if( via_read(via1,vBufB) & TREQ)
1102 ending = 1;
1103 else
1104 ending = 0;
1105 #endif
1106 /* ACK adb chip */
1107 via_write(via1, vBufB, via_read(via1, vBufB) | TACK);
1108 udelay(150);
1109 via_write(via1, vBufB, via_read(via1, vBufB) & ~TACK);
1111 else if(macintosh_config->adb_type==MAC_ADB_II)
1113 /* handshake etc. for II ?? */
1114 x = via_read(via1, vSR);
1115 first_byte = x;
1116 #if (ADBDEBUG & ADBDEBUG_READ)
1117 if (console_loglevel == 10)
1118 printk("adb_macII: reading reply: %x (%x %x) ",
1119 x, adb_state, status);
1120 #endif
1121 /* set ADB state = even for first data byte */
1122 via_write(via1, vBufB, (via_read(via1, vBufB)&~ST_MASK)|ST_EVEN);
1124 adb_state = reading;
1125 reply_ptr = current_req->reply;
1126 reading_reply = 1;
1127 reply_len = 0;
1128 prefix_len = 0;
1129 if (macintosh_config->adb_type==MAC_ADB_II) {
1130 *reply_ptr++ = ADB_PACKET;
1131 *reply_ptr++ = first_byte;
1132 *reply_ptr++ = first_byte; /* should be command byte */
1133 reply_len = 3;
1134 prefix_len = 3;
1136 break;
1138 case sent_first_byte:
1139 #if (ADBDEBUG & ADBDEBUG_WRITE)
1140 if (console_loglevel == 10)
1141 printk(" sending: %x (%x %x) ",
1142 current_req->data[1], adb_state, status);
1143 #endif
1144 if(macintosh_config->adb_type==MAC_ADB_CUDA)
1146 if (status == TREQ + TIP + SR_OUT)
1148 /* collision */
1149 via_write(via1, vACR,
1150 via_read(via1, vACR)&~SR_OUT);
1151 x = via_read(via1, vSR);
1152 via_write(via1, vBufB,
1153 via_read(via1,vBufB)|TIP|TACK);
1154 adb_state = idle;
1156 else
1158 /* assert status == TIP + SR_OUT */
1159 if (status != TIP + SR_OUT)
1160 printk("cuda: state=sent_first_byte status=%x\n", status);
1161 via_write(via1,vSR,current_req->data[1]);
1162 via_write(via1, vBufB,
1163 via_read(via1, vBufB)^TACK);
1164 data_index = 2;
1165 adb_state = sending;
1168 else if(macintosh_config->adb_type==MAC_ADB_IISI)
1170 /* switch ACK off */
1171 via_write(via1, vBufB, via_read(via1, vBufB) & ~TACK);
1172 if ( !(via_read(via1, vBufB) & TREQ) )
1174 /* collision */
1175 #if (ADBDEBUG & ADBDEBUG_WRITE)
1176 if (console_loglevel == 10)
1177 printk("adb_macIIsi: send collison, aborting!\n");
1178 #endif
1179 /* set shift in */
1180 via_write(via1, vACR,
1181 via_read(via1, vACR)&~SR_OUT);
1182 /* clear SR int. */
1183 x = via_read(via1, vSR);
1184 /* set ADB state to 'idle' */
1185 via_write(via1, vBufB,
1186 via_read(via1,vBufB) & ~(TIP|TACK));
1187 adb_state = idle;
1189 else
1191 /* delay */
1192 udelay(ADB_DELAY);
1193 /* set the shift register to shift out and send a byte */
1194 #if 0
1195 via_write(via1, vACR, via_read(via1, vACR) | SR_OUT);
1196 #endif
1197 via_write(via1, vSR, current_req->data[1]);
1198 /* signal 'byte ready' */
1199 via_write(via1, vBufB, via_read(via1, vBufB) | TACK);
1200 data_index=2;
1201 adb_state = sending;
1204 else if(macintosh_config->adb_type==MAC_ADB_II)
1206 /* how to detect a collision here ?? */
1207 /* maybe we're already done (Talk, or Poll)? */
1208 if (data_index >= current_req->nbytes)
1210 /* assert it's a Talk ?? */
1211 if ( (command_byte&0xc) != 0xc
1212 && console_loglevel == 10 )
1213 printk("ADB: single byte command, no Talk: %x!\n",
1214 command_byte);
1215 #if (ADBDEBUG & ADBDEBUG_WRITE)
1216 if (console_loglevel == 10)
1217 printk(" -> end (%d of %d) (%x %x)!\n",
1218 data_index, current_req->nbytes, adb_state, status);
1219 #endif
1220 current_req->sent = 1;
1221 if (current_req->reply_expected)
1223 #if (ADBDEBUG & ADBDEBUG_WRITE)
1224 if (console_loglevel == 10)
1225 printk("ADB: reply expected on poll!\n");
1226 #endif
1227 adb_state = awaiting_reply;
1228 reading_reply = 0;
1229 } else {
1230 #if (ADBDEBUG & ADBDEBUG_WRITE)
1231 if (console_loglevel == 10)
1232 printk("ADB: no reply for poll, not calling done()!\n");
1233 #endif
1234 req = current_req;
1235 current_req = req->next;
1236 #if 0 /* XXX Not sure about that one ... probably better enabled */
1237 if (req->done)
1238 (*req->done)(req);
1239 #endif
1240 adb_state = idle;
1241 reading_reply = 0;
1243 /* set to shift in */
1244 via_write(via1, vACR,
1245 via_read(via1, vACR) & ~SR_OUT);
1246 x=via_read(via1, vSR);
1247 /* set ADB state idle - might get SRQ */
1248 via_write(via1, vBufB,
1249 (via_read(via1, vBufB)&~ST_MASK)|ST_IDLE);
1250 break;
1252 #if (ADBDEBUG & ADBDEBUG_STATUS)
1253 if(!(status==(ST_CMD|TREQ) && adbdir == SR_OUT))
1254 printk("adb_macII: sent_first_byte, weird status=%x dir=%x\n",
1255 status, adbdir);
1256 #endif
1257 /* SR already set to shift out; send byte */
1258 via_write(via1, vSR, current_req->data[1]);
1259 /* set state to ST_EVEN (first byte was: ST_CMD) */
1260 via_write(via1, vBufB,
1261 (via_read(via1, vBufB)&~ST_MASK)|ST_EVEN);
1262 data_index=2;
1263 adb_state = sending;
1265 break;
1267 case sending:
1268 req = current_req;
1269 if (data_index >= req->nbytes)
1271 #if (ADBDEBUG & ADBDEBUG_WRITE)
1272 if (console_loglevel == 10)
1273 printk(" -> end (%d of %d) (%x %x)!\n",
1274 data_index-1, req->nbytes, adb_state, status);
1275 #endif
1276 /* end of packet */
1277 if(macintosh_config->adb_type==MAC_ADB_CUDA)
1279 via_write(via1, vACR,
1280 via_read(via1, vACR)&~SR_OUT);
1281 x = via_read(via1, vSR);
1282 via_write(via1, vBufB,
1283 via_read(via1,vBufB)|TACK|TIP);
1285 else if(macintosh_config->adb_type==MAC_ADB_IISI)
1287 /* XXX maybe clear ACK here ??? */
1288 /* switch ACK off */
1289 via_write(via1, vBufB, via_read(via1, vBufB) & ~TACK);
1290 /* delay */
1291 udelay(ADB_DELAY);
1292 /* set the shift register to shift in */
1293 via_write(via1, vACR, via_read(via1, vACR)|SR_OUT);
1294 /* clear SR int. */
1295 x = via_read(via1, vSR);
1296 /* set ADB state 'idle' (end of frame) */
1297 via_write(via1, vBufB,
1298 via_read(via1,vBufB) & ~(TACK|TIP));
1300 else if(macintosh_config->adb_type==MAC_ADB_II)
1303 * XXX Not sure: maybe only switch to
1304 * input mode on Talk ??
1306 /* set to shift in */
1307 via_write(via1, vACR,
1308 via_read(via1, vACR) & ~SR_OUT);
1309 x=via_read(via1, vSR);
1310 /* set ADB state idle - might get SRQ */
1311 via_write(via1, vBufB,
1312 (via_read(via1, vBufB)&~ST_MASK)|ST_IDLE);
1314 req->sent = 1;
1315 if (req->reply_expected)
1318 * maybe fake a reply here on Listen ??
1319 * Otherwise, a Listen hangs on success
1321 if ( macintosh_config->adb_type==MAC_ADB_II
1322 && ((req->data[0]&0xc) == 0xc) )
1323 adb_state = awaiting_reply;
1324 else if ( macintosh_config->adb_type != MAC_ADB_II
1325 && ( req->data[0] == 0x0)
1326 && ((req->data[1]&0xc) == 0xc) )
1327 adb_state = awaiting_reply;
1328 else {
1330 * Reply expected, but none
1331 * possible -> fake reply.
1332 * Problem: sending next command
1333 * should probably be done
1334 * without setting bus to 'idle'!
1335 * (except if no more commands)
1337 #if (ADBDEBUG & ADBDEBUG_PROT)
1338 printk("ADB: reply expected on Listen, faking reply\n");
1339 #endif
1340 /* make it look weird */
1341 /* XXX: return reply_len -1? */
1342 /* XXX: fake ADB header? */
1343 req->reply[0] = req->reply[1] = req->reply[2] = 0xFF;
1344 req->reply_len = 3;
1345 req->got_reply = 1;
1346 current_req = req->next;
1347 if (req->done)
1348 (*req->done)(req);
1350 * ready with this one, run
1351 * next command or repeat last
1352 * Talk (=idle on II)
1354 /* set state to idle !! */
1355 adb_state = idle;
1356 if (current_req || retry_req)
1357 adb_start();
1360 else
1362 current_req = req->next;
1363 if (req->done)
1364 (*req->done)(req);
1365 /* not sure about this */
1367 * MS: Must set idle, no new request
1368 * started else !
1370 adb_state = idle;
1372 * requires setting ADB state to idle,
1373 * maybe read a byte ! (done above)
1375 if (current_req || retry_req)
1376 adb_start();
1379 else
1381 #if (ADBDEBUG & ADBDEBUG_WRITE)
1382 if (console_loglevel == 10)
1383 printk(" %x (%x %x) ",
1384 req->data[data_index], adb_state, status);
1385 #endif
1386 if(macintosh_config->adb_type==MAC_ADB_CUDA)
1388 via_write(via1, vSR, req->data[data_index++]);
1389 via_write(via1, vBufB,
1390 via_read(via1, vBufB)^TACK);
1392 else if(macintosh_config->adb_type==MAC_ADB_IISI)
1394 /* switch ACK off */
1395 via_write(via1, vBufB, via_read(via1, vBufB) & ~TACK);
1396 /* delay */
1397 udelay(ADB_DELAY);
1398 /* XXX: need to check for collision?? */
1399 /* set the shift register to shift out and send a byte */
1400 #if 0
1401 via_write(via1, vACR, via_read(via1, vACR)|SR_OUT);
1402 #endif
1403 via_write(via1, vSR, req->data[data_index++]);
1404 /* signal 'byte ready' */
1405 via_write(via1, vBufB, via_read(via1, vBufB) | TACK);
1407 else if(macintosh_config->adb_type==MAC_ADB_II)
1409 via_write(via1, vSR, req->data[data_index++]);
1410 /* invert state bits, toggle ODD/EVEN */
1411 x = via_read(via1, vBufB);
1412 via_write(via1, vBufB,
1413 (x&~ST_MASK)|~(x&ST_MASK));
1416 break;
1418 case reading:
1420 /* timeout / SRQ handling for II hw */
1421 #ifdef POLL_ON_TIMEOUT
1422 if((reply_len-prefix_len)==3 && memcmp(reply_ptr-3,"\xFF\xFF\xFF",3)==0)
1423 #else
1424 if( (first_byte == 0xFF && (reply_len-prefix_len)==2
1425 && memcmp(reply_ptr-2,"\xFF\xFF",2)==0) ||
1426 ((reply_len-prefix_len)==3
1427 && memcmp(reply_ptr-3,"\xFF\xFF\xFF",3)==0))
1428 #endif
1431 * possible timeout (in fact, most probably a
1432 * timeout, since SRQ can't be signaled without
1433 * transfer on the bus).
1434 * The last three bytes seen were FF, together
1435 * with the starting byte (in case we started
1436 * on 'idle' or 'awaiting_reply') this probably
1437 * makes four. So this is mostl likely #5!
1438 * The timeout signal is a pattern 1 0 1 0 0..
1439 * on /INT, meaning we missed it :-(
1441 x = via_read(via1, vSR);
1442 if (x != 0xFF)
1443 printk("ADB: mistaken timeout/SRQ!\n");
1446 * ADB status bits: either even or odd.
1447 * adb_state: need to set 'idle' here.
1448 * Maybe saner: set 'need_poll' or
1449 * 'need_resend' here, fall through to
1450 * read_done ??
1452 #if (ADBDEBUG & ADBDEBUG_READ)
1453 if (console_loglevel == 10)
1454 printk(" -> read aborted: %x (%x %x)!\n",
1455 x, adb_state, status);
1456 #endif
1458 #if 0 /* XXX leave status unchanged!! - need to check this again! */
1459 /* XXX Only touch status on II !!! */
1460 /* set ADB state to idle (required by adb_start()) */
1461 via_write(via1, vBufB,
1462 (via_read(via1, vBufB)&~ST_MASK)|ST_IDLE);
1463 #endif
1466 * What if the timeout happens on reading a
1467 * reply ?? Assemble error reply and call
1468 * current_request->done()? Keep request
1469 * on queue?
1472 /* prevent 'busy' in adb_start() */
1473 need_poll = 1;
1476 * Timeout: /IRQ alternates high/low during
1477 * 4 'FF' bytes (1 0 1 0 0...)
1478 * We're on byte 5, so we need one
1479 * more backlog here (TBI) ....
1481 if ((status&TREQ) != (last_status&TREQ)) {
1482 #if (ADBDEBUG & ADBDEBUG_SRQ)
1483 if (console_loglevel == 10)
1484 printk("ADB: reply timeout, resending!\n");
1485 #endif
1487 * first byte received should be the
1488 * command byte timing out !!
1490 if (first_byte != 0xff)
1491 command_byte = first_byte;
1494 * compute target for retransmit: if
1495 * last_active is set, use that one,
1496 * else use command_byte
1498 if (last_active == -1)
1499 last_active = (command_byte&0xf0)>>4;
1500 adb_state = idle;
1501 /* resend if TALK, don't poll! */
1502 if (current_req)
1503 adb_start();
1504 else
1506 * XXX: need to count the timeouts ??
1507 * restart last active TALK ??
1508 * If no current_req, reuse old one!
1510 adb_retransmit(last_active);
1512 } else {
1514 * SRQ: NetBSD suggests /IRQ is asserted!?
1516 if (status&TREQ)
1517 printk("ADB: SRQ signature w/o /INT!\n");
1518 #if (ADBDEBUG & ADBDEBUG_SRQ)
1519 if (console_loglevel == 10)
1520 printk("ADB: empty SRQ packet!\n");
1521 #endif
1522 /* Terminate the SRQ packet and poll */
1523 adb_state = idle;
1524 adb_queue_poll();
1527 * Leave ADB status lines unchanged (means /IRQ
1528 * will still be low when entering adb_start!)
1530 break;
1532 /* end timeout / SRQ handling for II hw. */
1533 if((reply_len-prefix_len)>3 && memcmp(reply_ptr-3,"\xFF\xFF\xFF",3)==0)
1535 /* SRQ tacked on data packet */
1536 /* Check /IRQ here ?? */
1537 #if (ADBDEBUG & ADBDEBUG_SRQ)
1538 if (console_loglevel == 10)
1539 printk("\nADB: Packet with SRQ!\n");
1540 #endif
1541 /* Terminate the packet (SRQ never ends) */
1542 x = via_read(via1, vSR);
1543 adb_state = read_done;
1544 reply_len -= 3;
1545 reply_ptr -= 3;
1546 need_poll = 1;
1547 /* need to continue; next byte not seen else */
1549 * XXX: not at all sure here; maybe need to
1550 * send away the reply and poll immediately?
1552 } else {
1553 /* Sanity check */
1554 if(reply_len>15)
1555 reply_len=0;
1556 /* read byte */
1557 *reply_ptr = via_read(via1, vSR);
1558 x = *reply_ptr;
1559 #if (ADBDEBUG & ADBDEBUG_READ)
1560 if (console_loglevel == 10)
1561 printk(" %x (%x %x) ",
1562 *reply_ptr, adb_state, status);
1563 #endif
1564 reply_ptr++;
1565 reply_len++;
1567 /* The usual handshake ... */
1568 if (macintosh_config->adb_type==MAC_ADB_CUDA)
1570 if (status == TIP)
1572 /* that's all folks */
1573 via_write(via1, vBufB,
1574 via_read(via1, vBufB)|TACK|TIP);
1575 adb_state = read_done;
1577 else
1579 /* assert status == TIP | TREQ */
1580 if (status != TIP + TREQ)
1581 printk("cuda: state=reading status=%x\n", status);
1582 via_write(via1, vBufB,
1583 via_read(via1, vBufB)^TACK);
1586 else if (macintosh_config->adb_type==MAC_ADB_IISI)
1588 /* ACK adb chip (maybe check for end first?) */
1589 via_write(via1, vBufB, via_read(via1, vBufB) | TACK);
1590 udelay(150);
1591 via_write(via1, vBufB, via_read(via1, vBufB) & ~TACK);
1592 /* end of frame?? */
1593 if (status & TREQ)
1595 #if (ADBDEBUG & ADBDEBUG_READ)
1596 if (console_loglevel == 10)
1597 printk("adb_IIsi: end of frame!\n");
1598 #endif
1599 /* that's all folks */
1600 via_write(via1, vBufB,
1601 via_read(via1, vBufB) & ~(TACK|TIP));
1602 adb_state = read_done;
1603 /* maybe process read_done here?? Handshake anyway?? */
1606 else if (macintosh_config->adb_type==MAC_ADB_II)
1609 * NetBSD hints that the next to last byte
1610 * is sent with IRQ !!
1611 * Guido found out it's the last one (0x0),
1612 * but IRQ should be asserted already.
1613 * Problem with timeout detection: First
1614 * transition to /IRQ might be second
1615 * byte of timeout packet!
1616 * Timeouts are signaled by 4x FF.
1618 if(!(status&TREQ) && x == 0x00) /* != 0xFF */
1620 #if (ADBDEBUG & ADBDEBUG_READ)
1621 if (console_loglevel == 10)
1622 printk(" -> read done!\n");
1623 #endif
1624 #if 0 /* XXX: we take one more byte (why?), so handshake! */
1625 /* set ADB state to idle */
1626 via_write(via1, vBufB,
1627 (via_read(via1, vBufB)&~ST_MASK)|ST_IDLE);
1628 #else
1629 /* invert state bits, toggle ODD/EVEN */
1630 x = via_read(via1, vBufB);
1631 via_write(via1, vBufB,
1632 (x&~ST_MASK)|~(x&ST_MASK));
1633 #endif
1634 /* adjust packet length */
1635 reply_len--;
1636 reply_ptr--;
1637 adb_state = read_done;
1639 else
1641 #if (ADBDEBUG & ADBDEBUG_STATUS)
1642 if(status!=TIP+TREQ)
1643 printk("macII_adb: state=reading status=%x\n", status);
1644 #endif
1645 /* not caught: ST_CMD */
1646 /* required for re-entry 'reading'! */
1647 if ((status&ST_MASK) == ST_IDLE) {
1648 /* (in)sanity check - set even */
1649 via_write(via1, vBufB,
1650 (via_read(via1, vBufB)&~ST_MASK)|ST_EVEN);
1651 } else {
1652 /* invert state bits, toggle ODD/EVEN */
1653 x = via_read(via1, vBufB);
1654 via_write(via1, vBufB,
1655 (x&~ST_MASK)|~(x&ST_MASK));
1659 break;
1661 case read_done:
1662 x = via_read(via1, vSR);
1663 #if (ADBDEBUG & ADBDEBUG_READ)
1664 if (console_loglevel == 10)
1665 printk("ADB: read done: %x (%x %x)!\n",
1666 x, adb_state, status);
1667 #endif
1668 if (reading_reply)
1670 req = current_req;
1671 req->reply_len = reply_ptr - req->reply;
1672 req->got_reply = 1;
1673 current_req = req->next;
1674 if (req->done)
1675 (*req->done)(req);
1677 else
1679 adb_input(cuda_rbuf, reply_ptr - cuda_rbuf, regs);
1683 * remember this device ID; it's the latest we got a
1684 * reply from!
1686 last_reply = command_byte;
1687 last_active = (command_byte&0xf0)>>4;
1690 * Assert status = ST_IDLE ??
1693 * SRQ seen before, initiate poll now
1695 if (need_poll) {
1696 #if (ADBDEBUG & ADBDEBUG_POLL)
1697 if (console_loglevel == 10)
1698 printk("ADB: initiate poll!\n");
1699 #endif
1700 adb_state = idle;
1702 * set ADB status bits?? (unchanged above!)
1704 adb_queue_poll();
1705 need_poll = 0;
1706 /* hope this is ok; queue_poll runs adb_start */
1707 break;
1711 * /IRQ seen, so the ADB controller has data for us
1713 if (!(status&TREQ))
1715 /* set ADB state to idle */
1716 via_write(via1, vBufB,
1717 (via_read(via1, vBufB)&~ST_MASK)|ST_IDLE);
1719 adb_state = reading;
1720 reply_ptr = cuda_rbuf;
1721 reply_len = 0;
1722 prefix_len = 0;
1723 if (macintosh_config->adb_type==MAC_ADB_II) {
1724 *reply_ptr++ = ADB_PACKET;
1725 *reply_ptr++ = command_byte;
1726 reply_len = 2;
1727 prefix_len = 2;
1729 reading_reply = 0;
1731 else
1734 * no IRQ, send next packet or wait
1736 adb_state = idle;
1737 if (current_req)
1738 adb_start();
1739 else
1740 adb_retransmit(last_active);
1742 break;
1744 default:
1745 #if (ADBDEBUG & ADBDEBUG_STATE)
1746 printk("adb_interrupt: unknown adb_state %d?\n", adb_state);
1747 #endif
1749 /* reset mutex and interrupts */
1750 driver_running = 0;
1751 restore_flags(flags);
1755 * Restart of CUDA support: please modify this interrupt handler while
1756 * working at the Quadra etc. ADB driver. We can try to merge them later, or
1757 * remove the CUDA stuff from the MacII handler
1759 * MSch 27/01/98: Implemented IIsi driver based on initial code by Robert
1760 * Thompson and hints from the NetBSD driver. CUDA and IIsi seem more closely
1761 * related than to the MacII code, so merging all three might be a bad
1762 * idea.
1765 void adb_cuda_interrupt(int irq, void *arg, struct pt_regs *regs)
1767 int x, status;
1768 struct adb_request *req;
1769 unsigned long flags;
1771 save_flags(flags);
1772 cli();
1774 if(macintosh_config->adb_type==MAC_ADB_CUDA)
1775 status = (~via_read(via1, vBufB) & (TIP|TREQ)) | (via_read(via1, vACR) & SR_OUT);
1776 else
1777 status = via_read(via1, vBufB) & (TIP|TREQ);
1779 #if (ADBDEBUG & ADBDEBUG_INT)
1780 if (console_loglevel == 10)
1781 printk("adb_interrupt: state=%d status=%x\n", adb_state, status);
1782 #endif
1784 switch (adb_state)
1786 case idle:
1787 first_byte = 0;
1788 if(macintosh_config->adb_type==MAC_ADB_CUDA)
1790 #if (ADBDEBUG & ADBDEBUG_STATUS)
1791 /* CUDA has sent us the first byte of data - unsolicited */
1792 if (status != TREQ)
1793 printk("cuda: state=idle, status=%x want=%x\n",
1794 status, TREQ);
1795 #endif
1796 x = via_read(via1, vSR);
1797 #if (ADBDEBUG & ADBDEBUG_READ)
1798 if (console_loglevel == 10)
1799 printk("adb_cuda: receiving unsol. packet: %x (%x %x) ",
1800 x, adb_state, status);
1801 #endif
1802 via_write(via1, vBufB, via_read(via1,vBufB)&~TIP);
1804 else if(macintosh_config->adb_type==MAC_ADB_IISI)
1806 udelay(150);
1807 /* set SR to IN */
1808 via_write(via1, vACR,via_read(via1, vACR)&~SR_OUT);
1809 /* signal start of frame */
1810 via_write(via1, vBufB, via_read(via1, vBufB) | TIP);
1811 /* read first byte */
1812 x = via_read(via1, vSR);
1813 first_byte = x;
1814 #if (ADBDEBUG & ADBDEBUG_READ)
1815 if (console_loglevel == 10)
1816 printk("adb_IIsi : receiving unsol. packet: %x (%x %x) ",
1817 x, adb_state, status);
1818 #endif
1819 /* ACK adb chip */
1820 via_write(via1, vBufB, via_read(via1, vBufB) | TACK);
1821 udelay(150);
1822 via_write(via1, vBufB, via_read(via1, vBufB) & ~TACK);
1824 else if(macintosh_config->adb_type==MAC_ADB_II)
1826 if (status != TREQ)
1827 printk("adb_macII: state=idle status=%x want=%x\n",
1828 status, TREQ);
1829 x = via_read(via1, vSR);
1830 via_write(via1, vBufB, via_read(via1, vBufB)&~(TIP|TACK));
1832 adb_state = reading;
1833 reply_ptr = cuda_rbuf;
1834 reply_len = 0;
1835 reading_reply = 0;
1836 break;
1838 case awaiting_reply:
1839 if(macintosh_config->adb_type==MAC_ADB_CUDA)
1841 /* CUDA has sent us the first byte of data of a reply */
1842 #if (ADBDEBUG & ADBDEBUG_STATUS)
1843 if (status != TREQ)
1844 printk("cuda: state=awaiting_reply, status=%x want=%x\n",
1845 status, TREQ);
1846 #endif
1847 x = via_read(via1, vSR);
1848 #if (ADBDEBUG & ADBDEBUG_READ)
1849 if (console_loglevel == 10)
1850 printk("adb_cuda: reading reply: %x (%x %x) ",
1851 x, adb_state, status);
1852 #endif
1853 via_write(via1,vBufB,
1854 via_read(via1, vBufB)&~TIP);
1856 else if(macintosh_config->adb_type==MAC_ADB_IISI)
1858 /* udelay(150);*/
1859 /* set SR to IN */
1860 via_write(via1, vACR,via_read(via1, vACR)&~SR_OUT);
1861 /* signal start of frame */
1862 via_write(via1, vBufB, via_read(via1, vBufB) | TIP);
1863 /* read first byte */
1864 x = via_read(via1, vSR);
1865 first_byte = x;
1866 #if (ADBDEBUG & ADBDEBUG_READ)
1867 if (console_loglevel == 10)
1868 printk("adb_IIsi: reading reply: %x (%x %x) ",
1869 x, adb_state, status);
1870 #endif
1871 #if 0
1872 if( via_read(via1,vBufB) & TREQ)
1873 ending = 1;
1874 else
1875 ending = 0;
1876 #endif
1877 /* ACK adb chip */
1878 via_write(via1, vBufB, via_read(via1, vBufB) | TACK);
1879 udelay(150);
1880 via_write(via1, vBufB, via_read(via1, vBufB) & ~TACK);
1882 adb_state = reading;
1883 reply_ptr = current_req->reply;
1884 reading_reply = 1;
1885 reply_len = 0;
1886 break;
1888 case sent_first_byte:
1889 if(macintosh_config->adb_type==MAC_ADB_CUDA)
1891 #if (ADBDEBUG & ADBDEBUG_WRITE)
1892 if (console_loglevel == 10)
1893 printk(" sending: %x (%x %x) ",
1894 current_req->data[1], adb_state, status);
1895 #endif
1896 if (status == TREQ + TIP + SR_OUT)
1898 /* collision */
1899 if (console_loglevel == 10)
1900 printk("adb_cuda: send collision!\n");
1901 via_write(via1, vACR,
1902 via_read(via1, vACR)&~SR_OUT);
1903 x = via_read(via1, vSR);
1904 via_write(via1, vBufB,
1905 via_read(via1,vBufB)|TIP|TACK);
1906 adb_state = idle;
1908 else
1910 /* assert status == TIP + SR_OUT */
1911 #if (ADBDEBUG & ADBDEBUG_STATUS)
1912 if (status != TIP + SR_OUT)
1913 printk("adb_cuda: state=sent_first_byte status=%x want=%x\n",
1914 status, TIP + SR_OUT);
1915 #endif
1916 via_write(via1,vSR,current_req->data[1]);
1917 via_write(via1, vBufB,
1918 via_read(via1, vBufB)^TACK);
1919 data_index = 2;
1920 adb_state = sending;
1923 else if(macintosh_config->adb_type==MAC_ADB_IISI)
1925 /* switch ACK off */
1926 via_write(via1, vBufB, via_read(via1, vBufB) & ~TACK);
1927 if ( !(via_read(via1, vBufB) & TREQ) )
1929 /* collision */
1930 #if (ADBDEBUG & ADBDEBUG_WRITE)
1931 if (console_loglevel == 10)
1932 printk("adb_macIIsi: send collison, aborting!\n");
1933 #endif
1934 /* set shift in */
1935 via_write(via1, vACR,
1936 via_read(via1, vACR)&~SR_OUT);
1937 /* clear SR int. */
1938 x = via_read(via1, vSR);
1939 /* set ADB state to 'idle' */
1940 via_write(via1, vBufB,
1941 via_read(via1,vBufB) & ~(TIP|TACK));
1942 adb_state = idle;
1944 else
1946 /* delay */
1947 udelay(ADB_DELAY);
1948 /* set the shift register to shift out and send a byte */
1949 #if 0
1950 via_write(via1, vACR, via_read(via1, vACR) | SR_OUT);
1951 #endif
1952 via_write(via1, vSR, current_req->data[1]);
1953 /* signal 'byte ready' */
1954 via_write(via1, vBufB, via_read(via1, vBufB) | TACK);
1955 data_index=2;
1956 adb_state = sending;
1959 else if(macintosh_config->adb_type==MAC_ADB_II)
1961 if(status!=TIP+SR_OUT)
1962 printk("adb_macII: state=send_first_byte status=%x want=%x\n",
1963 status, TIP+SR_OUT);
1964 via_write(via1, vSR, current_req->data[1]);
1965 via_write(via1, vBufB,
1966 via_read(via1, vBufB)^TACK);
1967 data_index=2;
1968 adb_state = sending;
1970 break;
1972 case sending:
1973 req = current_req;
1974 if (data_index >= req->nbytes)
1976 #if (ADBDEBUG & ADBDEBUG_WRITE)
1977 if (console_loglevel == 10)
1978 printk(" -> end (%d of %d) (%x %x)!\n",
1979 data_index-1, req->nbytes, adb_state, status);
1980 #endif
1981 if(macintosh_config->adb_type==MAC_ADB_CUDA)
1983 via_write(via1, vACR,
1984 via_read(via1, vACR)&~SR_OUT);
1985 x = via_read(via1, vSR);
1986 via_write(via1, vBufB,
1987 via_read(via1,vBufB)|TACK|TIP);
1989 else if(macintosh_config->adb_type==MAC_ADB_IISI)
1991 /* XXX maybe clear ACK here ??? */
1992 /* switch ACK off */
1993 via_write(via1, vBufB, via_read(via1, vBufB) & ~TACK);
1994 /* delay */
1995 udelay(ADB_DELAY);
1996 /* set the shift register to shift in */
1997 via_write(via1, vACR, via_read(via1, vACR)|SR_OUT);
1998 /* clear SR int. */
1999 x = via_read(via1, vSR);
2000 /* set ADB state 'idle' (end of frame) */
2001 via_write(via1, vBufB,
2002 via_read(via1,vBufB) & ~(TACK|TIP));
2004 else if(macintosh_config->adb_type==MAC_ADB_II)
2006 via_write(via1, vACR,
2007 via_read(via1, vACR) & ~SR_OUT);
2008 x=via_read(via1, vSR);
2009 via_write(via1, vBufB,
2010 via_read(via1, vBufB)|TACK|TIP);
2012 req->sent = 1;
2013 if (req->reply_expected)
2016 * maybe fake a reply here on Listen ??
2017 * Otherwise, a Listen hangs on success
2018 * CUDA+IIsi: only ADB Talk considered
2019 * RTC/PRAM read (0x1 0x3) to follow.
2021 if ( (req->data[0] == 0x0) && ((req->data[1]&0xc) == 0xc) )
2022 adb_state = awaiting_reply;
2023 else {
2025 * Reply expected, but none
2026 * possible -> fake reply.
2028 #if (ADBDEBUG & ADBDEBUG_PROT)
2029 printk("ADB: reply expected on Listen, faking reply\n");
2030 #endif
2031 /* make it look weird */
2032 /* XXX: return reply_len -1? */
2033 /* XXX: fake ADB header? */
2034 req->reply[0] = req->reply[1] = req->reply[2] = 0xFF;
2035 req->reply_len = 3;
2036 req->got_reply = 1;
2037 current_req = req->next;
2038 if (req->done)
2039 (*req->done)(req);
2041 * ready with this one, run
2042 * next command !
2044 /* set state to idle !! */
2045 adb_state = idle;
2046 if (current_req || retry_req)
2047 adb_start();
2050 else
2052 current_req = req->next;
2053 if (req->done)
2054 (*req->done)(req);
2055 /* not sure about this */
2056 adb_state = idle;
2057 adb_start();
2060 else
2062 #if (ADBDEBUG & ADBDEBUG_WRITE)
2063 if (console_loglevel == 10)
2064 printk(" %x (%x %x) ",
2065 req->data[data_index], adb_state, status);
2066 #endif
2067 if(macintosh_config->adb_type==MAC_ADB_CUDA)
2069 via_write(via1, vSR, req->data[data_index++]);
2070 via_write(via1, vBufB,
2071 via_read(via1, vBufB)^TACK);
2073 else if(macintosh_config->adb_type==MAC_ADB_IISI)
2075 /* switch ACK off */
2076 via_write(via1, vBufB, via_read(via1, vBufB) & ~TACK);
2077 /* delay */
2078 udelay(ADB_DELAY);
2079 /* XXX: need to check for collision?? */
2080 /* set the shift register to shift out and send a byte */
2081 #if 0
2082 via_write(via1, vACR, via_read(via1, vACR)|SR_OUT);
2083 #endif
2084 via_write(via1, vSR, req->data[data_index++]);
2085 /* signal 'byte ready' */
2086 via_write(via1, vBufB, via_read(via1, vBufB) | TACK);
2088 else if(macintosh_config->adb_type==MAC_ADB_II)
2090 via_write(via1, vSR, req->data[data_index++]);
2091 via_write(via1, vBufB,
2092 via_read(via1, vBufB)^TACK);
2095 break;
2097 case reading:
2098 if(reply_len==3 && memcmp(reply_ptr-3,"\xFF\xFF\xFF",3)==0)
2100 /* Terminate the SRQ packet */
2101 #if (ADBDEBUG & ADBDEBUG_SRQ)
2102 if (console_loglevel == 10)
2103 printk("adb: Got an SRQ\n");
2104 #endif
2105 adb_state = idle;
2106 adb_queue_poll();
2107 break;
2109 /* Sanity check - botched in orig. code! */
2110 if(reply_len>15) {
2111 printk("adb_cuda: reply buffer overrun!\n");
2112 /* wrap buffer */
2113 reply_len=0;
2114 if (reading_reply)
2115 reply_ptr = current_req->reply;
2116 else
2117 reply_ptr = cuda_rbuf;
2119 *reply_ptr = via_read(via1, vSR);
2120 #if (ADBDEBUG & ADBDEBUG_READ)
2121 if (console_loglevel == 10)
2122 printk(" %x (%x %x) ",
2123 *reply_ptr, adb_state, status);
2124 #endif
2125 reply_ptr++;
2126 reply_len++;
2127 if(macintosh_config->adb_type==MAC_ADB_CUDA)
2129 if (status == TIP)
2131 /* that's all folks */
2132 via_write(via1, vBufB,
2133 via_read(via1, vBufB)|TACK|TIP);
2134 adb_state = read_done;
2136 else
2138 /* assert status == TIP | TREQ */
2139 #if (ADBDEBUG & ADBDEBUG_STATUS)
2140 if (status != TIP + TREQ)
2141 printk("cuda: state=reading status=%x want=%x\n",
2142 status, TIP + TREQ);
2143 #endif
2144 via_write(via1, vBufB,
2145 via_read(via1, vBufB)^TACK);
2148 else if (macintosh_config->adb_type==MAC_ADB_IISI)
2150 /* ACK adb chip (maybe check for end first?) */
2151 via_write(via1, vBufB, via_read(via1, vBufB) | TACK);
2152 udelay(150);
2153 via_write(via1, vBufB, via_read(via1, vBufB) & ~TACK);
2154 /* end of frame?? */
2155 if (status & TREQ)
2157 #if (ADBDEBUG & ADBDEBUG_READ)
2158 if (console_loglevel == 10)
2159 printk("adb_IIsi: end of frame!\n");
2160 #endif
2161 /* that's all folks */
2162 via_write(via1, vBufB,
2163 via_read(via1, vBufB) & ~(TACK|TIP));
2164 adb_state = read_done;
2165 /* XXX maybe process read_done here??
2166 Handshake anyway?? */
2169 if(macintosh_config->adb_type==MAC_ADB_II)
2171 if( status == TIP)
2173 via_write(via1, vBufB,
2174 via_read(via1, vBufB)|TACK|TIP);
2175 adb_state = read_done;
2177 else
2179 #if (ADBDEBUG & ADBDEBUG_STATUS)
2180 if(status!=TIP+TREQ)
2181 printk("macII_adb: state=reading status=%x\n", status);
2182 #endif
2183 via_write(via1, vBufB,
2184 via_read(via1, vBufB)^TACK);
2187 /* fall through for IIsi on end of frame */
2188 if (macintosh_config->adb_type != MAC_ADB_IISI
2189 || adb_state != read_done)
2190 break;
2192 case read_done:
2193 x = via_read(via1, vSR);
2194 #if (ADBDEBUG & ADBDEBUG_READ)
2195 if (console_loglevel == 10)
2196 printk("adb: read done: %x (%x %x)!\n",
2197 x, adb_state, status);
2198 #endif
2199 if (reading_reply)
2201 req = current_req;
2202 req->reply_len = reply_ptr - req->reply;
2203 req->got_reply = 1;
2204 current_req = req->next;
2205 if (req->done)
2206 (*req->done)(req);
2208 else
2210 adb_input(cuda_rbuf, reply_ptr - cuda_rbuf, regs);
2213 if (macintosh_config->adb_type==MAC_ADB_CUDA
2214 && status & TREQ)
2216 via_write(via1, vBufB,
2217 via_read(via1, vBufB)&~TIP);
2218 adb_state = reading;
2219 reply_ptr = cuda_rbuf;
2220 reading_reply = 0;
2222 else if (macintosh_config->adb_type==MAC_ADB_IISI
2223 && !(status & TREQ))
2225 udelay(150);
2226 via_write(via1, vBufB,
2227 via_read(via1, vBufB) | TIP);
2228 adb_state = reading;
2229 reply_ptr = cuda_rbuf;
2230 reading_reply = 0;
2232 else
2234 adb_state = idle;
2235 adb_start();
2237 break;
2239 default:
2240 printk("adb_cuda_interrupt: unknown adb_state %d?\n", adb_state);
2243 restore_flags(flags);
2248 * The 'reply delivery' routine; determines which device sent the
2249 * request and calls the appropriate handler.
2250 * Reply data are expected in CUDA format (again, argh...) so we need
2251 * to fake this in the interrupt handler for MacII.
2252 * Only one handler per device ID is currently possible.
2253 * XXX: is the ID field here representing the default or real ID?
2255 static void adb_input(unsigned char *buf, int nb, struct pt_regs *regs)
2257 int i, id;
2259 switch (buf[0])
2261 case ADB_PACKET:
2262 /* what's in buf[1] ?? */
2263 id = buf[2] >> 4;
2264 #if 0
2265 xmon_printf("adb packet: ");
2266 for (i = 0; i < nb; ++i)
2267 xmon_printf(" %x", buf[i]);
2268 xmon_printf(", id = %d\n", id);
2269 #endif
2270 #if (ADBDEBUG & ADBDEBUG_INPUT)
2271 if (console_loglevel == 10) {
2272 printk("adb_input: adb packet ");
2273 for (i = 0; i < nb; ++i)
2274 printk(" %x", buf[i]);
2275 printk(", id = %d\n", id);
2277 #endif
2278 if (adb_handler[id].handler != 0)
2280 (*adb_handler[id].handler)(buf, nb, regs);
2282 break;
2284 default:
2285 #if (ADBDEBUG & ADBDEBUG_INPUT)
2286 if (console_loglevel == 10) {
2287 printk("adb_input: data from via (%d bytes):", nb);
2288 for (i = 0; i < nb; ++i)
2289 printk(" %.2x", buf[i]);
2290 printk("\n");
2292 #endif
2296 /* Ultimately this should return the number of devices with
2297 the given default id. */
2299 int adb_register(int default_id,
2300 void (*handler)(unsigned char *, int, struct pt_regs *))
2302 if (adb_handler[default_id].handler != 0)
2303 panic("Two handlers for ADB device %d\n", default_id);
2304 adb_handler[default_id].handler = handler;
2305 return 1;
2309 * /dev/adb device driver.
2312 #define ADB_MAJOR 56 /* major number for /dev/adb */
2314 #define ADB_MAX_MINOR 64 /* range of ADB minors */
2315 #define ADB_TYPE_SHIFT 4 /* # bits for device ID/type in subdevices */
2317 #define ADB_TYPE_RAW 0 /* raw device; unbuffered */
2318 #define ADB_TYPE_BUFF 1 /* raw device; buffered */
2319 #define ADB_TYPE_COOKED 2 /* 'cooked' device */
2322 extern void adbdev_init(void);
2324 struct adbdev_state {
2325 struct adb_request req;
2328 static struct wait_queue *adb_wait;
2330 static int adb_wait_reply(struct adbdev_state *state, struct file *file)
2332 int ret = 0;
2333 struct wait_queue wait = { current, NULL };
2335 add_wait_queue(&adb_wait, &wait);
2336 current->state = TASK_INTERRUPTIBLE;
2338 while (!state->req.got_reply) {
2339 if (file->f_flags & O_NONBLOCK) {
2340 ret = -EAGAIN;
2341 break;
2343 if (signal_pending(current)) {
2344 ret = -ERESTARTSYS;
2345 break;
2347 schedule();
2350 current->state = TASK_RUNNING;
2351 remove_wait_queue(&adb_wait, &wait);
2353 return ret;
2356 static void adb_write_done(struct adb_request *req)
2358 if (!req->got_reply) {
2359 req->reply_len = 0;
2360 req->got_reply = 1;
2362 wake_up_interruptible(&adb_wait);
2365 struct file_operations *adb_raw[16];
2366 struct file_operations *adb_buffered[16];
2367 struct file_operations *adb_cooked[16];
2369 static int adb_open(struct inode *inode, struct file *file)
2371 int adb_type, adb_subtype;
2372 struct adbdev_state *state;
2374 if (MINOR(inode->i_rdev) > ADB_MAX_MINOR)
2375 return -ENXIO;
2377 switch (MINOR(inode->i_rdev) >> ADB_TYPE_SHIFT) {
2378 case ADB_TYPE_RAW:
2379 /* see code below */
2380 break;
2381 case ADB_TYPE_BUFF:
2382 /* TBI */
2383 return -ENXIO;
2384 case ADB_TYPE_COOKED:
2385 /* subtypes such as kbd, mouse, ... */
2386 adb_subtype = MINOR(inode->i_rdev) & ~ADB_TYPE_SHIFT;
2387 if ((file->f_op = adb_cooked[adb_subtype]))
2388 return file->f_op->open(inode,file);
2389 else
2390 return -ENODEV;
2393 state = kmalloc(sizeof(struct adbdev_state), GFP_KERNEL);
2394 if (state == 0)
2395 return -ENOMEM;
2396 file->private_data = state;
2397 state->req.reply_expected = 0;
2398 return 0;
2401 static void adb_release(struct inode *inode, struct file *file)
2403 struct adbdev_state *state = file->private_data;
2405 if (state) {
2406 file->private_data = NULL;
2407 if (state->req.reply_expected && !state->req.got_reply)
2408 if (adb_wait_reply(state, file))
2409 return;
2410 kfree(state);
2412 return;
2415 static int adb_lseek(struct inode *inode, struct file *file,
2416 off_t offset, int origin)
2418 return -ESPIPE;
2421 static int adb_read(struct inode *inode, struct file *file,
2422 char *buf, int count)
2424 int ret;
2425 struct adbdev_state *state = file->private_data;
2427 if (count < 2)
2428 return -EINVAL;
2429 if (count > sizeof(state->req.reply))
2430 count = sizeof(state->req.reply);
2431 ret = verify_area(VERIFY_WRITE, buf, count);
2432 if (ret)
2433 return ret;
2435 if (!state->req.reply_expected)
2436 return 0;
2438 ret = adb_wait_reply(state, file);
2439 if (ret)
2440 return ret;
2442 state->req.reply_expected = 0;
2443 ret = state->req.reply_len;
2444 copy_to_user(buf, state->req.reply, ret);
2446 return ret;
2449 static int adb_write(struct inode *inode, struct file *file,
2450 const char *buf, int count)
2452 int ret, i;
2453 struct adbdev_state *state = file->private_data;
2455 if (count < 2 || count > sizeof(state->req.data))
2456 return -EINVAL;
2457 ret = verify_area(VERIFY_READ, buf, count);
2458 if (ret)
2459 return ret;
2461 if (state->req.reply_expected && !state->req.got_reply) {
2462 /* A previous request is still being processed.
2463 Wait for it to finish. */
2464 ret = adb_wait_reply(state, file);
2465 if (ret)
2466 return ret;
2469 state->req.nbytes = count;
2470 state->req.done = adb_write_done;
2471 state->req.got_reply = 0;
2472 copy_from_user(state->req.data, buf, count);
2473 #if 0
2474 switch (adb_hardware) {
2475 case ADB_NONE:
2476 return -ENXIO;
2477 case ADB_VIACUDA:
2478 state->req.reply_expected = 1;
2479 cuda_send_request(&state->req);
2480 break;
2481 default:
2482 #endif
2483 if (state->req.data[0] != ADB_PACKET)
2484 return -EINVAL;
2485 for (i = 1; i < state->req.nbytes; ++i)
2486 state->req.data[i] = state->req.data[i+1];
2487 state->req.reply_expected =
2488 ((state->req.data[0] & 0xc) == 0xc);
2489 adb_send_request(&state->req);
2490 #if 0
2491 break;
2493 #endif
2495 return count;
2498 static struct file_operations adb_fops = {
2499 adb_lseek,
2500 adb_read,
2501 adb_write,
2502 NULL, /* no readdir */
2503 NULL, /* no poll yet */
2504 NULL, /* no ioctl yet */
2505 NULL, /* no mmap */
2506 adb_open,
2507 adb_release
2510 int adbdev_register(int subtype, struct file_operations *fops)
2512 if (subtype < 0 || subtype > 15)
2513 return -EINVAL;
2514 if (adb_cooked[subtype])
2515 return -EBUSY;
2516 adb_cooked[subtype] = fops;
2517 return 0;
2520 int adbdev_unregister(int subtype)
2522 if (subtype < 0 || subtype > 15)
2523 return -EINVAL;
2524 if (!adb_cooked[subtype])
2525 return -ENODEV;
2526 adb_cooked[subtype] = NULL;
2527 return 0;
2530 void adbdev_init()
2532 if (register_chrdev(ADB_MAJOR, "adb", &adb_fops))
2533 printk(KERN_ERR "adb: unable to get major %d\n", ADB_MAJOR);
2537 #if 0 /* old ADB device */
2540 * Here are the file operations we export for /dev/adb.
2543 #define ADB_MINOR 140 /* /dev/adb is c 10 140 */
2545 extern void adbdev_inits(void);
2547 struct adbdev_state {
2548 struct adb_request req;
2551 static struct wait_queue *adb_wait;
2553 static int adb_wait_reply(struct adbdev_state *state, struct file *file)
2555 int ret = 0;
2556 struct wait_queue wait = { current, NULL };
2558 #if (ADBDEBUG & ADBDEBUG_DEVICE)
2559 printk("ADB request: wait_reply (blocking ... \n");
2560 #endif
2562 add_wait_queue(&adb_wait, &wait);
2563 current->state = TASK_INTERRUPTIBLE;
2565 while (!state->req.got_reply) {
2566 if (file->f_flags & O_NONBLOCK) {
2567 ret = -EAGAIN;
2568 break;
2570 if (signal_pending(current)) {
2571 ret = -ERESTARTSYS;
2572 break;
2574 schedule();
2577 current->state = TASK_RUNNING;
2578 remove_wait_queue(&adb_wait, &wait);
2580 return ret;
2583 static void adb_write_done(struct adb_request *req)
2585 if (!req->got_reply) {
2586 req->reply_len = 0;
2587 req->got_reply = 1;
2589 wake_up_interruptible(&adb_wait);
2592 static int adb_open(struct inode *inode, struct file *file)
2594 struct adbdev_state *state;
2596 state = kmalloc(sizeof(struct adbdev_state), GFP_KERNEL);
2597 if (state == 0)
2598 return -ENOMEM;
2599 file->private_data = state;
2600 state->req.reply_expected = 0;
2601 return 0;
2604 static void adb_release(struct inode *inode, struct file *file)
2606 struct adbdev_state *state = file->private_data;
2608 if (state) {
2609 file->private_data = NULL;
2610 if (state->req.reply_expected && !state->req.got_reply)
2611 if (adb_wait_reply(state, file))
2612 return;
2613 kfree(state);
2615 return;
2618 static int adb_lseek(struct inode *inode, struct file *file,
2619 off_t offset, int origin)
2621 return -ESPIPE;
2624 static int adb_read(struct inode *inode, struct file *file,
2625 char *buf, int count)
2627 int ret;
2628 struct adbdev_state *state = file->private_data;
2630 if (count < 2)
2631 return -EINVAL;
2632 if (count > sizeof(state->req.reply))
2633 count = sizeof(state->req.reply);
2634 ret = verify_area(VERIFY_WRITE, buf, count);
2635 if (ret)
2636 return ret;
2638 if (!state->req.reply_expected)
2639 return 0;
2641 ret = adb_wait_reply(state, file);
2642 if (ret)
2643 return ret;
2645 ret = state->req.reply_len;
2646 memcpy_tofs(buf, state->req.reply, ret);
2647 state->req.reply_expected = 0;
2649 return ret;
2652 static int adb_write(struct inode *inode, struct file *file,
2653 const char *buf, int count)
2655 int ret;
2656 struct adbdev_state *state = file->private_data;
2658 if (count < 2 || count > sizeof(state->req.data))
2659 return -EINVAL;
2660 ret = verify_area(VERIFY_READ, buf, count);
2661 if (ret)
2662 return ret;
2664 if (state->req.reply_expected && !state->req.got_reply) {
2665 /* A previous request is still being processed.
2666 Wait for it to finish. */
2667 ret = adb_wait_reply(state, file);
2668 if (ret)
2669 return ret;
2672 state->req.nbytes = count;
2673 state->req.done = adb_write_done;
2674 memcpy_fromfs(state->req.data, buf, count);
2675 state->req.reply_expected = 1;
2676 state->req.got_reply = 0;
2677 adb_send_request(&state->req);
2679 return count;
2682 static struct file_operations adb_fops = {
2683 adb_lseek,
2684 adb_read,
2685 adb_write,
2686 NULL, /* no readdir */
2687 NULL, /* no select */
2688 NULL, /* no ioctl */
2689 NULL, /* no mmap */
2690 adb_open,
2691 adb_release
2694 static struct miscdevice adb_dev = {
2695 ADB_MINOR,
2696 "adb",
2697 &adb_fops
2700 void adbdev_init(void)
2702 misc_register(&adb_dev);
2705 #endif /* old ADB device */