2 * MACII ADB keyboard handler.
3 * Copyright (c) 1997 Alan Cox
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>
26 #include <asm/uaccess.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 */
52 #define SR_CTRL 0x1c /* Shift register control bits */
54 #define SR_EXT 0x1c /* Shift on external clock */
56 #define SR_EXT 0x0c /* Shift on external clock */
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 */
70 static struct adb_handler
{
71 void (*handler
)(unsigned char *, int, struct pt_regs
*);
74 static enum 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
;
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;*/
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*/
141 #define ADBDEBUG (ADBDEBUG_INPUT | ADBDEBUG_READ | ADBDEBUG_START | ADBDEBUG_WRITE | ADBDEBUG_SRQ | ADBDEBUG_REQUEST)
148 void adb_bus_init(void)
160 switch(macintosh_config
->adb_type
)
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 :-)
174 /* Lower the bus signals (MacII is active low it seems ???) */
175 via_write(via1
, vBufB
, via_read(via1
, vBufB
)&~TACK
);
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
);
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 */
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
);
205 * Unsupported; but later code doesn't notice !!
208 printk("adb: CUDA interface.\n");
210 /* don't know what to set up here ... */
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
));
217 request_irq(IRQ_MAC_ADB
, adb_cuda_interrupt
, IRQ_FLG_LOCK
,
218 "adb CUDA interrupt", adb_cuda_interrupt
);
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
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 */
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
++) {
251 if (via_read(via1
, vBufB
) & TREQ
)
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
);
265 printk("adb: Unknown hardware interface.\n");
267 printk("adb: Interface unsupported.\n");
268 restore_flags(flags
);
273 * XXX: interrupt only registered if supported HW !!
274 * -> unsupported HW will just time out on keyb_init!
277 request_irq(IRQ_MAC_ADB
, adb_interrupt
, IRQ_FLG_LOCK
,
278 "adb interrupt", adb_interrupt
);
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
);
287 printk("adb: init done.\n");
288 restore_flags(flags
);
291 void adb_hw_setup_cuda(void)
296 printk("CUDA: HW Setup:");
301 if (console_loglevel
== 10)
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)
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)
317 /* Clear any possible Cuda interrupt */
318 x
= via_read( via1
, vSR
);
320 if (console_loglevel
== 10)
323 /* Terminate transaction and set idle state */
324 via_write( via1
, vBufB
, via_read( via1
, vBufB
) | TIP
| TACK
);
326 if (console_loglevel
== 10)
329 /* Delay 4 mS for ADB reset to complete */
332 if (console_loglevel
== 10)
335 /* Clear pending interrupts... */
336 x
= via_read( via1
, vSR
);
338 if (console_loglevel
== 10)
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)
346 /* Wait for the sync acknowledgement, Cuda to assert TREQ */
347 while( ( via_read( via1
, vBufB
) & TREQ
) != 0 )
350 if (console_loglevel
== 10)
353 /* Wait for the sync acknowledment interrupt */
354 while( ( via_read( via1
, vIFR
) & SR_INT
) == 0 )
357 if (console_loglevel
== 10)
360 /* Clear pending interrupts... */
361 x
= via_read( via1
, vSR
);
363 if (console_loglevel
== 10)
366 /* Terminate the sync cycle by negating TACK */
367 via_write( via1
, vBufB
, via_read( via1
, vBufB
) | TACK
);
369 if (console_loglevel
== 10)
372 /* Wait for the sync termination acknowledgement, Cuda to negate TREQ */
373 while( ( via_read( via1
, vBufB
) & TREQ
) == 0 )
376 if (console_loglevel
== 10)
379 /* Wait for the sync termination acknowledment interrupt */
380 while( ( via_read( via1
, vIFR
) & SR_INT
) == 0 )
383 if (console_loglevel
== 10)
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)
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)
405 printk("adb_IIsi: cleanup!\n");
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 */
418 if (!(via_read(via1
, vBufB
) & TREQ
)) { /* IRQ on */
420 via_write(via1
, vBufB
,via_read(via1
,vBufB
) | TIP
);
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
)
441 via_write(via1
,vBufB
,via_read(via1
, vBufB
) | TACK
);
445 via_write(via1
,vBufB
,via_read(via1
, vBufB
) & ~TACK
);
448 via_write(via1
, vBufB
,via_read(via1
,vBufB
) & ~TIP
);
449 /* probably don't need to delay this long */
452 /* re-enable SR int. */
453 via_write(via1
, vIER
, IER_SET
|SR_INT
);
456 #define WAIT_FOR(cond, what) \
458 for (x = 1000; !(cond); --x) { \
460 printk("Timeout waiting for " what); \
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
*),
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);
493 req
->nbytes
= nbytes
;
495 #if (ADBDEBUG & ADBDEBUG_REQUEST)
496 if (console_loglevel
== 10)
497 printk("adb_request, data bytes: ");
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
]);
506 #if (ADBDEBUG & ADBDEBUG_REQUEST)
507 if (console_loglevel
== 10)
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
*),
541 req
->nbytes
= nbytes
;
543 va_start(list
, nbytes
);
544 #if (ADBDEBUG & ADBDEBUG_REQUEST)
545 if (console_loglevel
== 10)
546 printk("adb__build_request, data bytes: ");
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
]);
558 #if (ADBDEBUG & ADBDEBUG_REQUEST)
559 if (console_loglevel
== 10)
564 req
->reply_expected
= 0;
569 * Send an ADB poll (Talk, tagged on the front of the request queue)
571 void adb_queue_poll(void)
574 static int in_poll
=0;
575 static struct adb_request r
;
579 printk("Double poll!\n");
586 #if (ADBDEBUG & ADBDEBUG_POLL)
587 if (console_loglevel
== 10)
588 printk("adb: Polling %d\n",pod
);
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));
595 /* CUDA etc. version */
596 adb_build_request(&r
,NULL
, 2, 0, (pod
<<4|0xC));
605 /* Poll inserted at head of queue ... */
608 restore_flags(flags
);
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
;
623 printk("Double retransmit!\n");
627 #if (ADBDEBUG & ADBDEBUG_POLL)
628 if (console_loglevel
== 10)
629 printk("adb: Sending retransmit: %d\n", device
);
633 adb_build_request(&rt
,NULL
, 1, (device
<<4|0xC));
635 rt
.reply_expected
= 0;
645 /* Retransmit inserted at tail of queue ... */
647 if (current_req
!= NULL
)
649 last_req
->next
= &rt
;
658 /* always restart driver (send_retransmit used in place of adb_start!)*/
660 if (adb_state
== idle
)
663 restore_flags(flags
);
668 * Queue an ADB request; start ADB transfer if necessary
670 int adb_send_request(struct adb_request
*req
)
681 if (current_req
!= NULL
)
683 last_req
->next
= req
;
690 if (adb_state
== idle
)
694 restore_flags(flags
);
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)
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 */
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
));
738 #if (ADBDEBUG & ADBDEBUG_START)
739 if (console_loglevel
== 10)
740 printk("adb_start: request %p ", req
);
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 ...)
762 /* printk("device busy - fail\n"); */
763 restore_flags(flags
);
764 /* a byte is coming in from the CUDA */
767 printk("adb_start: device busy - fail\n");
769 restore_flags(flags
);
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
778 printk("device busy - retry %p state %d status %x!\n",
779 req
, adb_state
, via_read(via1
, vBufB
)&(ST_MASK
|TREQ
));
781 /* set ADB status here ? */
782 restore_flags(flags
);
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
));
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
));
810 restore_flags(flags
);
816 * Another retry pending? (sanity check)
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
);
825 /* poll requests are added to the head of queue */
826 printk("adb_start: retry %p pending, req %p (poll?) current! \n",
833 * Seems OK, go for it!
835 switch(macintosh_config
->adb_type
)
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
);
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
);
859 /* store command byte */
860 command_byte
= req
->data
[0];
862 via_write(via1
, vACR
, via_read(via1
, vACR
)|SR_OUT
);
864 via_write(via1
, vSR
, req
->data
[0]);
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
));
871 /* set ADB state to 'command' */
872 via_write(via1
, vBufB
, (via_read(via1
, vBufB
)&~ST_MASK
)|ST_CMD
);
876 adb_state
= sent_first_byte
;
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
)) );
884 restore_flags(flags
);
888 * Poll the ADB state (maybe obsolete now that interrupt-driven ADB runs)
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
)) {
904 printk("adb clock event ");
906 printk("adb data event ");
910 if (console_loglevel
== 10)
911 printk("adb_poll: IFR %x state %x ",
914 if (console_loglevel
== 10)
919 #if (ADBDEBUG & ADBDEBUG_POLL)
920 if (console_loglevel
== 10)
921 printk("adb_poll: adb interrupt event\n");
923 adb_interrupt(0, 0, 0);
925 restore_flags(flags
);
931 void adb_clock_interrupt(int irq
, void *arg
, struct pt_regs
*regs
)
936 void adb_data_interrupt(int irq
, void *arg
, struct pt_regs
*regs
)
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
)
984 struct adb_request
*req
;
986 last_status
= status
;
988 /* prevent races due to SCSI enabling ints */
992 if (driver_running
) {
993 restore_flags(flags
);
1000 status
= (~via_read(via1
, vBufB
) & (TIP
|TREQ
)) | (via_read(via1
, vACR
) & SR_OUT
);
1002 if (macintosh_config
->adb_type
==MAC_ADB_CUDA
)
1003 status
= (~via_read(via1
, vBufB
) & (TIP
|TREQ
)) | (via_read(via1
, vACR
) & SR_OUT
);
1005 /* status bits (0x8->0x20) and direction (0x10 ??) CLASH !! */
1006 status
= (via_read(via1
, vBufB
) & (ST_MASK
|TREQ
));
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
);
1018 if(macintosh_config
->adb_type
==MAC_ADB_CUDA
)
1020 /* CUDA has sent us the first byte of data - unsolicited */
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
)
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
);
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
);
1042 via_write(via1
, vBufB
, via_read(via1
, vBufB
) | TACK
);
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",
1054 x
= via_read(via1
, vSR
);
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
);
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
;
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;*/
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 */
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
)
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
);
1097 #if (ADBDEBUG & ADBDEBUG_READ)
1098 if (console_loglevel
== 10)
1099 printk("adb_macIIsi: reading reply: %x (%x %x) ",
1100 x
, adb_state
, status
);
1103 if( via_read(via1
,vBufB
) & TREQ
)
1109 via_write(via1
, vBufB
, via_read(via1
, vBufB
) | TACK
);
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
);
1118 #if (ADBDEBUG & ADBDEBUG_READ)
1119 if (console_loglevel
== 10)
1120 printk("adb_macII: reading reply: %x (%x %x) ",
1121 x
, adb_state
, status
);
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
;
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 */
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
);
1146 if(macintosh_config
->adb_type
==MAC_ADB_CUDA
)
1148 if (status
== TREQ
+ TIP
+ SR_OUT
)
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
);
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
);
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
) )
1177 #if (ADBDEBUG & ADBDEBUG_WRITE)
1178 if (console_loglevel
== 10)
1179 printk("adb_macIIsi: send collison, aborting!\n");
1182 via_write(via1
, vACR
,
1183 via_read(via1
, vACR
)&~SR_OUT
);
1185 x
= via_read(via1
, vSR
);
1186 /* set ADB state to 'idle' */
1187 via_write(via1
, vBufB
,
1188 via_read(via1
,vBufB
) & ~(TIP
|TACK
));
1195 /* set the shift register to shift out and send a byte */
1197 via_write(via1
, vACR
, via_read(via1
, vACR
) | SR_OUT
);
1199 via_write(via1
, vSR
, current_req
->data
[1]);
1200 /* signal 'byte ready' */
1201 via_write(via1
, vBufB
, via_read(via1
, vBufB
) | TACK
);
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",
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
);
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");
1229 adb_state
= awaiting_reply
;
1232 #if (ADBDEBUG & ADBDEBUG_WRITE)
1233 if (console_loglevel
== 10)
1234 printk("ADB: no reply for poll, not calling done()!\n");
1237 current_req
= req
->next
;
1238 #if 0 /* XXX Not sure about that one ... probably better enabled */
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
);
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",
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
);
1265 adb_state
= sending
;
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
);
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
);
1294 /* set the shift register to shift in */
1295 via_write(via1
, vACR
, via_read(via1
, vACR
)|SR_OUT
);
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
);
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
;
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");
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;
1348 current_req
= req
->next
;
1352 * ready with this one, run
1353 * next command or repeat last
1354 * Talk (=idle on II)
1356 /* set state to idle !! */
1358 if (current_req
|| retry_req
)
1364 current_req
= req
->next
;
1367 /* not sure about this */
1369 * MS: Must set idle, no new request
1374 * requires setting ADB state to idle,
1375 * maybe read a byte ! (done above)
1377 if (current_req
|| retry_req
)
1383 #if (ADBDEBUG & ADBDEBUG_WRITE)
1384 if (console_loglevel
== 10)
1385 printk(" %x (%x %x) ",
1386 req
->data
[data_index
], adb_state
, status
);
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
);
1400 /* XXX: need to check for collision?? */
1401 /* set the shift register to shift out and send a byte */
1403 via_write(via1
, vACR
, via_read(via1
, vACR
)|SR_OUT
);
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
));
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)
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))
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
);
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
1454 #if (ADBDEBUG & ADBDEBUG_READ)
1455 if (console_loglevel
== 10)
1456 printk(" -> read aborted: %x (%x %x)!\n",
1457 x
, adb_state
, status
);
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
);
1468 * What if the timeout happens on reading a
1469 * reply ?? Assemble error reply and call
1470 * current_request->done()? Keep request
1474 /* prevent 'busy' in adb_start() */
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");
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;
1503 /* resend if TALK, don't poll! */
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
);
1516 * SRQ: NetBSD suggests /IRQ is asserted!?
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");
1524 /* Terminate the SRQ packet and poll */
1529 * Leave ADB status lines unchanged (means /IRQ
1530 * will still be low when entering adb_start!)
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");
1543 /* Terminate the packet (SRQ never ends) */
1544 x
= via_read(via1
, vSR
);
1545 adb_state
= read_done
;
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?
1559 *reply_ptr
= via_read(via1
, vSR
);
1561 #if (ADBDEBUG & ADBDEBUG_READ)
1562 if (console_loglevel
== 10)
1563 printk(" %x (%x %x) ",
1564 *reply_ptr
, adb_state
, status
);
1569 /* The usual handshake ... */
1570 if (macintosh_config
->adb_type
==MAC_ADB_CUDA
)
1574 /* that's all folks */
1575 via_write(via1
, vBufB
,
1576 via_read(via1
, vBufB
)|TACK
|TIP
);
1577 adb_state
= read_done
;
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
);
1593 via_write(via1
, vBufB
, via_read(via1
, vBufB
) & ~TACK
);
1594 /* end of frame?? */
1597 #if (ADBDEBUG & ADBDEBUG_READ)
1598 if (console_loglevel
== 10)
1599 printk("adb_IIsi: end of frame!\n");
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");
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
);
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
));
1636 /* adjust packet length */
1639 adb_state
= read_done
;
1643 #if (ADBDEBUG & ADBDEBUG_STATUS)
1644 if(status
!=TIP
+TREQ
)
1645 printk("macII_adb: state=reading status=%x\n", status
);
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
);
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
));
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
);
1673 req
->reply_len
= reply_ptr
- req
->reply
;
1675 current_req
= req
->next
;
1681 adb_input(cuda_rbuf
, reply_ptr
- cuda_rbuf
, regs
);
1685 * remember this device ID; it's the latest we got a
1688 last_reply
= command_byte
;
1689 last_active
= (command_byte
&0xf0)>>4;
1692 * Assert status = ST_IDLE ??
1695 * SRQ seen before, initiate poll now
1698 #if (ADBDEBUG & ADBDEBUG_POLL)
1699 if (console_loglevel
== 10)
1700 printk("ADB: initiate poll!\n");
1704 * set ADB status bits?? (unchanged above!)
1708 /* hope this is ok; queue_poll runs adb_start */
1713 * /IRQ seen, so the ADB controller has data for us
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
;
1725 if (macintosh_config
->adb_type
==MAC_ADB_II
) {
1726 *reply_ptr
++ = ADB_PACKET
;
1727 *reply_ptr
++ = command_byte
;
1736 * no IRQ, send next packet or wait
1742 adb_retransmit(last_active
);
1747 #if (ADBDEBUG & ADBDEBUG_STATE)
1748 printk("adb_interrupt: unknown adb_state %d?\n", adb_state
);
1751 /* reset mutex and interrupts */
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
1767 void adb_cuda_interrupt(int irq
, void *arg
, struct pt_regs
*regs
)
1770 struct adb_request
*req
;
1771 unsigned long flags
;
1776 if(macintosh_config
->adb_type
==MAC_ADB_CUDA
)
1777 status
= (~via_read(via1
, vBufB
) & (TIP
|TREQ
)) | (via_read(via1
, vACR
) & SR_OUT
);
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
);
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 */
1795 printk("cuda: state=idle, status=%x want=%x\n",
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
);
1804 via_write(via1
, vBufB
, via_read(via1
,vBufB
)&~TIP
);
1806 else if(macintosh_config
->adb_type
==MAC_ADB_IISI
)
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
);
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
);
1822 via_write(via1
, vBufB
, via_read(via1
, vBufB
) | TACK
);
1824 via_write(via1
, vBufB
, via_read(via1
, vBufB
) & ~TACK
);
1826 else if(macintosh_config
->adb_type
==MAC_ADB_II
)
1829 printk("adb_macII: state=idle status=%x want=%x\n",
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
;
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)
1846 printk("cuda: state=awaiting_reply, status=%x want=%x\n",
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
);
1855 via_write(via1
,vBufB
,
1856 via_read(via1
, vBufB
)&~TIP
);
1858 else if(macintosh_config
->adb_type
==MAC_ADB_IISI
)
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
);
1868 #if (ADBDEBUG & ADBDEBUG_READ)
1869 if (console_loglevel
== 10)
1870 printk("adb_IIsi: reading reply: %x (%x %x) ",
1871 x
, adb_state
, status
);
1874 if( via_read(via1
,vBufB
) & TREQ
)
1880 via_write(via1
, vBufB
, via_read(via1
, vBufB
) | TACK
);
1882 via_write(via1
, vBufB
, via_read(via1
, vBufB
) & ~TACK
);
1884 adb_state
= reading
;
1885 reply_ptr
= current_req
->reply
;
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
);
1898 if (status
== TREQ
+ TIP
+ SR_OUT
)
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
);
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
);
1918 via_write(via1
,vSR
,current_req
->data
[1]);
1919 via_write(via1
, vBufB
,
1920 via_read(via1
, vBufB
)^TACK
);
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
) )
1932 #if (ADBDEBUG & ADBDEBUG_WRITE)
1933 if (console_loglevel
== 10)
1934 printk("adb_macIIsi: send collison, aborting!\n");
1937 via_write(via1
, vACR
,
1938 via_read(via1
, vACR
)&~SR_OUT
);
1940 x
= via_read(via1
, vSR
);
1941 /* set ADB state to 'idle' */
1942 via_write(via1
, vBufB
,
1943 via_read(via1
,vBufB
) & ~(TIP
|TACK
));
1950 /* set the shift register to shift out and send a byte */
1952 via_write(via1
, vACR
, via_read(via1
, vACR
) | SR_OUT
);
1954 via_write(via1
, vSR
, current_req
->data
[1]);
1955 /* signal 'byte ready' */
1956 via_write(via1
, vBufB
, via_read(via1
, vBufB
) | TACK
);
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
);
1970 adb_state
= sending
;
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
);
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
);
1998 /* set the shift register to shift in */
1999 via_write(via1
, vACR
, via_read(via1
, vACR
)|SR_OUT
);
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
);
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
;
2027 * Reply expected, but none
2028 * possible -> fake reply.
2030 #if (ADBDEBUG & ADBDEBUG_PROT)
2031 printk("ADB: reply expected on Listen, faking reply\n");
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;
2039 current_req
= req
->next
;
2043 * ready with this one, run
2046 /* set state to idle !! */
2048 if (current_req
|| retry_req
)
2054 current_req
= req
->next
;
2057 /* not sure about this */
2064 #if (ADBDEBUG & ADBDEBUG_WRITE)
2065 if (console_loglevel
== 10)
2066 printk(" %x (%x %x) ",
2067 req
->data
[data_index
], adb_state
, status
);
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
);
2081 /* XXX: need to check for collision?? */
2082 /* set the shift register to shift out and send a byte */
2084 via_write(via1
, vACR
, via_read(via1
, vACR
)|SR_OUT
);
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
);
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");
2111 /* Sanity check - botched in orig. code! */
2113 printk("adb_cuda: reply buffer overrun!\n");
2117 reply_ptr
= current_req
->reply
;
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
);
2129 if(macintosh_config
->adb_type
==MAC_ADB_CUDA
)
2133 /* that's all folks */
2134 via_write(via1
, vBufB
,
2135 via_read(via1
, vBufB
)|TACK
|TIP
);
2136 adb_state
= read_done
;
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
);
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
);
2155 via_write(via1
, vBufB
, via_read(via1
, vBufB
) & ~TACK
);
2156 /* end of frame?? */
2159 #if (ADBDEBUG & ADBDEBUG_READ)
2160 if (console_loglevel
== 10)
2161 printk("adb_IIsi: end of frame!\n");
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
)
2175 via_write(via1
, vBufB
,
2176 via_read(via1
, vBufB
)|TACK
|TIP
);
2177 adb_state
= read_done
;
2181 #if (ADBDEBUG & ADBDEBUG_STATUS)
2182 if(status
!=TIP
+TREQ
)
2183 printk("macII_adb: state=reading status=%x\n", status
);
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
)
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
);
2204 req
->reply_len
= reply_ptr
- req
->reply
;
2206 current_req
= req
->next
;
2212 adb_input(cuda_rbuf
, reply_ptr
- cuda_rbuf
, regs
);
2215 if (macintosh_config
->adb_type
==MAC_ADB_CUDA
2218 via_write(via1
, vBufB
,
2219 via_read(via1
, vBufB
)&~TIP
);
2220 adb_state
= reading
;
2221 reply_ptr
= cuda_rbuf
;
2224 else if (macintosh_config
->adb_type
==MAC_ADB_IISI
2225 && !(status
& TREQ
))
2228 via_write(via1
, vBufB
,
2229 via_read(via1
, vBufB
) | TIP
);
2230 adb_state
= reading
;
2231 reply_ptr
= cuda_rbuf
;
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
)
2264 /* what's in buf[1] ?? */
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
);
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
);
2280 if (adb_handler
[id
].handler
!= 0)
2282 (*adb_handler
[id
].handler
)(buf
, nb
, regs
);
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
]);
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
;
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
)
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
) {
2345 if (signal_pending(current
)) {
2352 __set_current_state(TASK_RUNNING
);
2353 remove_wait_queue(&adb_wait
, &wait
);
2358 static void adb_write_done(struct adb_request
*req
)
2360 if (!req
->got_reply
) {
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
)
2379 switch (MINOR(inode
->i_rdev
) >> ADB_TYPE_SHIFT
) {
2381 /* see code below */
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
);
2395 state
= kmalloc(sizeof(struct adbdev_state
), GFP_KERNEL
);
2398 file
->private_data
= state
;
2399 state
->req
.reply_expected
= 0;
2403 static void adb_release(struct inode
*inode
, struct file
*file
)
2405 struct adbdev_state
*state
= file
->private_data
;
2408 file
->private_data
= NULL
;
2409 if (state
->req
.reply_expected
&& !state
->req
.got_reply
)
2410 if (adb_wait_reply(state
, file
))
2417 static int adb_lseek(struct inode
*inode
, struct file
*file
,
2418 off_t offset
, int origin
)
2423 static int adb_read(struct inode
*inode
, struct file
*file
,
2424 char *buf
, int count
)
2427 struct adbdev_state
*state
= file
->private_data
;
2431 if (count
> sizeof(state
->req
.reply
))
2432 count
= sizeof(state
->req
.reply
);
2433 ret
= verify_area(VERIFY_WRITE
, buf
, count
);
2437 if (!state
->req
.reply_expected
)
2440 ret
= adb_wait_reply(state
, file
);
2444 state
->req
.reply_expected
= 0;
2445 ret
= state
->req
.reply_len
;
2446 copy_to_user(buf
, state
->req
.reply
, ret
);
2451 static int adb_write(struct inode
*inode
, struct file
*file
,
2452 const char *buf
, int count
)
2455 struct adbdev_state
*state
= file
->private_data
;
2457 if (count
< 2 || count
> sizeof(state
->req
.data
))
2459 ret
= verify_area(VERIFY_READ
, buf
, count
);
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
);
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
);
2476 switch (adb_hardware
) {
2480 state
->req
.reply_expected
= 1;
2481 cuda_send_request(&state
->req
);
2485 if (state
->req
.data
[0] != ADB_PACKET
)
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
);
2500 static struct file_operations adb_fops
= {
2504 NULL
, /* no readdir */
2505 NULL
, /* no poll yet */
2506 NULL
, /* no ioctl yet */
2513 int adbdev_register(int subtype
, struct file_operations
*fops
)
2515 if (subtype
< 0 || subtype
> 15)
2517 if (adb_cooked
[subtype
])
2519 adb_cooked
[subtype
] = fops
;
2523 int adbdev_unregister(int subtype
)
2525 if (subtype
< 0 || subtype
> 15)
2527 if (!adb_cooked
[subtype
])
2529 adb_cooked
[subtype
] = NULL
;
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
)
2559 DECLARE_WAITQUEUE(wait
, current
);
2561 #if (ADBDEBUG & ADBDEBUG_DEVICE)
2562 printk("ADB request: wait_reply (blocking ... \n");
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
) {
2573 if (signal_pending(current
)) {
2580 __set_current_state(TASK_RUNNING
);
2581 remove_wait_queue(&adb_wait
, &wait
);
2586 static void adb_write_done(struct adb_request
*req
)
2588 if (!req
->got_reply
) {
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
);
2602 file
->private_data
= state
;
2603 state
->req
.reply_expected
= 0;
2607 static void adb_release(struct inode
*inode
, struct file
*file
)
2609 struct adbdev_state
*state
= file
->private_data
;
2612 file
->private_data
= NULL
;
2613 if (state
->req
.reply_expected
&& !state
->req
.got_reply
)
2614 if (adb_wait_reply(state
, file
))
2621 static int adb_lseek(struct inode
*inode
, struct file
*file
,
2622 off_t offset
, int origin
)
2627 static int adb_read(struct inode
*inode
, struct file
*file
,
2628 char *buf
, int count
)
2631 struct adbdev_state
*state
= file
->private_data
;
2635 if (count
> sizeof(state
->req
.reply
))
2636 count
= sizeof(state
->req
.reply
);
2637 ret
= verify_area(VERIFY_WRITE
, buf
, count
);
2641 if (!state
->req
.reply_expected
)
2644 ret
= adb_wait_reply(state
, file
);
2648 ret
= state
->req
.reply_len
;
2649 memcpy_tofs(buf
, state
->req
.reply
, ret
);
2650 state
->req
.reply_expected
= 0;
2655 static int adb_write(struct inode
*inode
, struct file
*file
,
2656 const char *buf
, int count
)
2659 struct adbdev_state
*state
= file
->private_data
;
2661 if (count
< 2 || count
> sizeof(state
->req
.data
))
2663 ret
= verify_area(VERIFY_READ
, buf
, count
);
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
);
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
);
2685 static struct file_operations adb_fops
= {
2689 NULL
, /* no readdir */
2690 NULL
, /* no select */
2691 NULL
, /* no ioctl */
2698 static struct miscdevice adb_dev
= {
2704 void adbdev_init(void)
2706 misc_register(&adb_dev
);
2709 #endif /* old ADB device */