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
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>
27 #include <asm/uaccess.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 */
53 #define SR_CTRL 0x1c /* Shift register control bits */
55 #define SR_EXT 0x1c /* Shift on external clock */
57 #define SR_EXT 0x0c /* Shift on external clock */
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 */
71 static struct adb_handler
{
72 void (*handler
)(unsigned char *, int, struct pt_regs
*);
75 static enum 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
;
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;*/
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*/
142 #define ADBDEBUG (ADBDEBUG_INPUT | ADBDEBUG_READ | ADBDEBUG_START | ADBDEBUG_WRITE | ADBDEBUG_SRQ | ADBDEBUG_REQUEST)
149 void adb_bus_init(void)
161 switch(macintosh_config
->adb_type
)
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 :-)
175 /* Lower the bus signals (MacII is active low it seems ???) */
176 via_write(via1
, vBufB
, via_read(via1
, vBufB
)&~TACK
);
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
);
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 */
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
);
206 * Unsupported; but later code doesn't notice !!
209 printk("adb: CUDA interface.\n");
211 /* don't know what to set up here ... */
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
));
218 request_irq(IRQ_MAC_ADB
, adb_cuda_interrupt
, IRQ_FLG_LOCK
,
219 "adb CUDA interrupt", adb_cuda_interrupt
);
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
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 */
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
++) {
252 if (via_read(via1
, vBufB
) & TREQ
)
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
);
263 printk("adb: Unknown hardware interface.\n");
265 printk("adb: Interface unsupported.\n");
266 restore_flags(flags
);
271 * XXX: interrupt only registered if supported HW !!
272 * -> unsupported HW will just time out on keyb_init!
275 request_irq(IRQ_MAC_ADB
, adb_interrupt
, IRQ_FLG_LOCK
,
276 "adb interrupt", adb_interrupt
);
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
);
285 printk("adb: init done.\n");
286 restore_flags(flags
);
289 void adb_hw_setup_cuda(void)
294 printk("CUDA: HW Setup:");
299 if (console_loglevel
== 10)
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)
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)
315 /* Clear any possible Cuda interrupt */
316 x
= via_read( via1
, vSR
);
318 if (console_loglevel
== 10)
321 /* Terminate transaction and set idle state */
322 via_write( via1
, vBufB
, via_read( via1
, vBufB
) | TIP
| TACK
);
324 if (console_loglevel
== 10)
327 /* Delay 4 mS for ADB reset to complete */
330 if (console_loglevel
== 10)
333 /* Clear pending interrupts... */
334 x
= via_read( via1
, vSR
);
336 if (console_loglevel
== 10)
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)
344 /* Wait for the sync acknowledgement, Cuda to assert TREQ */
345 while( ( via_read( via1
, vBufB
) & TREQ
) != 0 )
348 if (console_loglevel
== 10)
351 /* Wait for the sync acknowledment interrupt */
352 while( ( via_read( via1
, vIFR
) & SR_INT
) == 0 )
355 if (console_loglevel
== 10)
358 /* Clear pending interrupts... */
359 x
= via_read( via1
, vSR
);
361 if (console_loglevel
== 10)
364 /* Terminate the sync cycle by negating TACK */
365 via_write( via1
, vBufB
, via_read( via1
, vBufB
) | TACK
);
367 if (console_loglevel
== 10)
370 /* Wait for the sync termination acknowledgement, Cuda to negate TREQ */
371 while( ( via_read( via1
, vBufB
) & TREQ
) == 0 )
374 if (console_loglevel
== 10)
377 /* Wait for the sync termination acknowledment interrupt */
378 while( ( via_read( via1
, vIFR
) & SR_INT
) == 0 )
381 if (console_loglevel
== 10)
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)
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)
403 printk("adb_IIsi: cleanup!\n");
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 */
416 if (!(via_read(via1
, vBufB
) & TREQ
)) { /* IRQ on */
418 via_write(via1
, vBufB
,via_read(via1
,vBufB
) | TIP
);
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
)
439 via_write(via1
,vBufB
,via_read(via1
, vBufB
) | TACK
);
443 via_write(via1
,vBufB
,via_read(via1
, vBufB
) & ~TACK
);
446 via_write(via1
, vBufB
,via_read(via1
,vBufB
) & ~TIP
);
447 /* probably don't need to delay this long */
450 /* re-enable SR int. */
451 via_write(via1
, vIER
, IER_SET
|SR_INT
);
454 #define WAIT_FOR(cond, what) \
456 for (x = 1000; !(cond); --x) { \
458 printk("Timeout waiting for " what); \
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
*),
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);
491 req
->nbytes
= nbytes
;
493 #if (ADBDEBUG & ADBDEBUG_REQUEST)
494 if (console_loglevel
== 10)
495 printk("adb_request, data bytes: ");
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
]);
504 #if (ADBDEBUG & ADBDEBUG_REQUEST)
505 if (console_loglevel
== 10)
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
*),
539 req
->nbytes
= nbytes
;
541 va_start(list
, nbytes
);
542 #if (ADBDEBUG & ADBDEBUG_REQUEST)
543 if (console_loglevel
== 10)
544 printk("adb__build_request, data bytes: ");
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
]);
556 #if (ADBDEBUG & ADBDEBUG_REQUEST)
557 if (console_loglevel
== 10)
562 req
->reply_expected
= 0;
567 * Send an ADB poll (Talk, tagged on the front of the request queue)
569 void adb_queue_poll(void)
572 static int in_poll
=0;
573 static struct adb_request r
;
577 printk("Double poll!\n");
584 #if (ADBDEBUG & ADBDEBUG_POLL)
585 if (console_loglevel
== 10)
586 printk("adb: Polling %d\n",pod
);
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));
593 /* CUDA etc. version */
594 adb_build_request(&r
,NULL
, 2, 0, (pod
<<4|0xC));
603 /* Poll inserted at head of queue ... */
606 restore_flags(flags
);
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
;
621 printk("Double retransmit!\n");
625 #if (ADBDEBUG & ADBDEBUG_POLL)
626 if (console_loglevel
== 10)
627 printk("adb: Sending retransmit: %d\n", device
);
631 adb_build_request(&rt
,NULL
, 1, (device
<<4|0xC));
633 rt
.reply_expected
= 0;
643 /* Retransmit inserted at tail of queue ... */
645 if (current_req
!= NULL
)
647 last_req
->next
= &rt
;
656 /* always restart driver (send_retransmit used in place of adb_start!)*/
658 if (adb_state
== idle
)
661 restore_flags(flags
);
666 * Queue an ADB request; start ADB transfer if necessary
668 int adb_send_request(struct adb_request
*req
)
679 if (current_req
!= NULL
)
681 last_req
->next
= req
;
688 if (adb_state
== idle
)
692 restore_flags(flags
);
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)
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 */
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
));
736 #if (ADBDEBUG & ADBDEBUG_START)
737 if (console_loglevel
== 10)
738 printk("adb_start: request %p ", req
);
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 ...)
760 /* printk("device busy - fail\n"); */
761 restore_flags(flags
);
762 /* a byte is coming in from the CUDA */
765 printk("adb_start: device busy - fail\n");
767 restore_flags(flags
);
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
776 printk("device busy - retry %p state %d status %x!\n",
777 req
, adb_state
, via_read(via1
, vBufB
)&(ST_MASK
|TREQ
));
779 /* set ADB status here ? */
780 restore_flags(flags
);
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
));
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
));
808 restore_flags(flags
);
814 * Another retry pending? (sanity check)
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
);
823 /* poll requests are added to the head of queue */
824 printk("adb_start: retry %p pending, req %p (poll?) current! \n",
831 * Seems OK, go for it!
833 switch(macintosh_config
->adb_type
)
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
);
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
);
857 /* store command byte */
858 command_byte
= req
->data
[0];
860 via_write(via1
, vACR
, via_read(via1
, vACR
)|SR_OUT
);
862 via_write(via1
, vSR
, req
->data
[0]);
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
));
869 /* set ADB state to 'command' */
870 via_write(via1
, vBufB
, (via_read(via1
, vBufB
)&~ST_MASK
)|ST_CMD
);
874 adb_state
= sent_first_byte
;
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
)) );
882 restore_flags(flags
);
886 * Poll the ADB state (maybe obsolete now that interrupt-driven ADB runs)
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
)) {
902 printk("adb clock event ");
904 printk("adb data event ");
908 if (console_loglevel
== 10)
909 printk("adb_poll: IFR %x state %x ",
912 if (console_loglevel
== 10)
917 #if (ADBDEBUG & ADBDEBUG_POLL)
918 if (console_loglevel
== 10)
919 printk("adb_poll: adb interrupt event\n");
921 adb_interrupt(0, 0, 0);
923 restore_flags(flags
);
929 void adb_clock_interrupt(int irq
, void *arg
, struct pt_regs
*regs
)
934 void adb_data_interrupt(int irq
, void *arg
, struct pt_regs
*regs
)
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
)
982 struct adb_request
*req
;
984 last_status
= status
;
986 /* prevent races due to SCSI enabling ints */
990 if (driver_running
) {
991 restore_flags(flags
);
998 status
= (~via_read(via1
, vBufB
) & (TIP
|TREQ
)) | (via_read(via1
, vACR
) & SR_OUT
);
1000 if (macintosh_config
->adb_type
==MAC_ADB_CUDA
)
1001 status
= (~via_read(via1
, vBufB
) & (TIP
|TREQ
)) | (via_read(via1
, vACR
) & SR_OUT
);
1003 /* status bits (0x8->0x20) and direction (0x10 ??) CLASH !! */
1004 status
= (via_read(via1
, vBufB
) & (ST_MASK
|TREQ
));
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
);
1016 if(macintosh_config
->adb_type
==MAC_ADB_CUDA
)
1018 /* CUDA has sent us the first byte of data - unsolicited */
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
)
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
);
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
);
1040 via_write(via1
, vBufB
, via_read(via1
, vBufB
) | TACK
);
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",
1052 x
= via_read(via1
, vSR
);
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
);
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
;
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;*/
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 */
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
)
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
);
1095 #if (ADBDEBUG & ADBDEBUG_READ)
1096 if (console_loglevel
== 10)
1097 printk("adb_macIIsi: reading reply: %x (%x %x) ",
1098 x
, adb_state
, status
);
1101 if( via_read(via1
,vBufB
) & TREQ
)
1107 via_write(via1
, vBufB
, via_read(via1
, vBufB
) | TACK
);
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
);
1116 #if (ADBDEBUG & ADBDEBUG_READ)
1117 if (console_loglevel
== 10)
1118 printk("adb_macII: reading reply: %x (%x %x) ",
1119 x
, adb_state
, status
);
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
;
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 */
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
);
1144 if(macintosh_config
->adb_type
==MAC_ADB_CUDA
)
1146 if (status
== TREQ
+ TIP
+ SR_OUT
)
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
);
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
);
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
) )
1175 #if (ADBDEBUG & ADBDEBUG_WRITE)
1176 if (console_loglevel
== 10)
1177 printk("adb_macIIsi: send collison, aborting!\n");
1180 via_write(via1
, vACR
,
1181 via_read(via1
, vACR
)&~SR_OUT
);
1183 x
= via_read(via1
, vSR
);
1184 /* set ADB state to 'idle' */
1185 via_write(via1
, vBufB
,
1186 via_read(via1
,vBufB
) & ~(TIP
|TACK
));
1193 /* set the shift register to shift out and send a byte */
1195 via_write(via1
, vACR
, via_read(via1
, vACR
) | SR_OUT
);
1197 via_write(via1
, vSR
, current_req
->data
[1]);
1198 /* signal 'byte ready' */
1199 via_write(via1
, vBufB
, via_read(via1
, vBufB
) | TACK
);
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",
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
);
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");
1227 adb_state
= awaiting_reply
;
1230 #if (ADBDEBUG & ADBDEBUG_WRITE)
1231 if (console_loglevel
== 10)
1232 printk("ADB: no reply for poll, not calling done()!\n");
1235 current_req
= req
->next
;
1236 #if 0 /* XXX Not sure about that one ... probably better enabled */
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
);
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",
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
);
1263 adb_state
= sending
;
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
);
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
);
1292 /* set the shift register to shift in */
1293 via_write(via1
, vACR
, via_read(via1
, vACR
)|SR_OUT
);
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
);
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
;
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");
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;
1346 current_req
= req
->next
;
1350 * ready with this one, run
1351 * next command or repeat last
1352 * Talk (=idle on II)
1354 /* set state to idle !! */
1356 if (current_req
|| retry_req
)
1362 current_req
= req
->next
;
1365 /* not sure about this */
1367 * MS: Must set idle, no new request
1372 * requires setting ADB state to idle,
1373 * maybe read a byte ! (done above)
1375 if (current_req
|| retry_req
)
1381 #if (ADBDEBUG & ADBDEBUG_WRITE)
1382 if (console_loglevel
== 10)
1383 printk(" %x (%x %x) ",
1384 req
->data
[data_index
], adb_state
, status
);
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
);
1398 /* XXX: need to check for collision?? */
1399 /* set the shift register to shift out and send a byte */
1401 via_write(via1
, vACR
, via_read(via1
, vACR
)|SR_OUT
);
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
));
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)
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))
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
);
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
1452 #if (ADBDEBUG & ADBDEBUG_READ)
1453 if (console_loglevel
== 10)
1454 printk(" -> read aborted: %x (%x %x)!\n",
1455 x
, adb_state
, status
);
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
);
1466 * What if the timeout happens on reading a
1467 * reply ?? Assemble error reply and call
1468 * current_request->done()? Keep request
1472 /* prevent 'busy' in adb_start() */
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");
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;
1501 /* resend if TALK, don't poll! */
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
);
1514 * SRQ: NetBSD suggests /IRQ is asserted!?
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");
1522 /* Terminate the SRQ packet and poll */
1527 * Leave ADB status lines unchanged (means /IRQ
1528 * will still be low when entering adb_start!)
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");
1541 /* Terminate the packet (SRQ never ends) */
1542 x
= via_read(via1
, vSR
);
1543 adb_state
= read_done
;
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?
1557 *reply_ptr
= via_read(via1
, vSR
);
1559 #if (ADBDEBUG & ADBDEBUG_READ)
1560 if (console_loglevel
== 10)
1561 printk(" %x (%x %x) ",
1562 *reply_ptr
, adb_state
, status
);
1567 /* The usual handshake ... */
1568 if (macintosh_config
->adb_type
==MAC_ADB_CUDA
)
1572 /* that's all folks */
1573 via_write(via1
, vBufB
,
1574 via_read(via1
, vBufB
)|TACK
|TIP
);
1575 adb_state
= read_done
;
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
);
1591 via_write(via1
, vBufB
, via_read(via1
, vBufB
) & ~TACK
);
1592 /* end of frame?? */
1595 #if (ADBDEBUG & ADBDEBUG_READ)
1596 if (console_loglevel
== 10)
1597 printk("adb_IIsi: end of frame!\n");
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");
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
);
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
));
1634 /* adjust packet length */
1637 adb_state
= read_done
;
1641 #if (ADBDEBUG & ADBDEBUG_STATUS)
1642 if(status
!=TIP
+TREQ
)
1643 printk("macII_adb: state=reading status=%x\n", status
);
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
);
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
));
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
);
1671 req
->reply_len
= reply_ptr
- req
->reply
;
1673 current_req
= req
->next
;
1679 adb_input(cuda_rbuf
, reply_ptr
- cuda_rbuf
, regs
);
1683 * remember this device ID; it's the latest we got a
1686 last_reply
= command_byte
;
1687 last_active
= (command_byte
&0xf0)>>4;
1690 * Assert status = ST_IDLE ??
1693 * SRQ seen before, initiate poll now
1696 #if (ADBDEBUG & ADBDEBUG_POLL)
1697 if (console_loglevel
== 10)
1698 printk("ADB: initiate poll!\n");
1702 * set ADB status bits?? (unchanged above!)
1706 /* hope this is ok; queue_poll runs adb_start */
1711 * /IRQ seen, so the ADB controller has data for us
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
;
1723 if (macintosh_config
->adb_type
==MAC_ADB_II
) {
1724 *reply_ptr
++ = ADB_PACKET
;
1725 *reply_ptr
++ = command_byte
;
1734 * no IRQ, send next packet or wait
1740 adb_retransmit(last_active
);
1745 #if (ADBDEBUG & ADBDEBUG_STATE)
1746 printk("adb_interrupt: unknown adb_state %d?\n", adb_state
);
1749 /* reset mutex and interrupts */
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
1765 void adb_cuda_interrupt(int irq
, void *arg
, struct pt_regs
*regs
)
1768 struct adb_request
*req
;
1769 unsigned long flags
;
1774 if(macintosh_config
->adb_type
==MAC_ADB_CUDA
)
1775 status
= (~via_read(via1
, vBufB
) & (TIP
|TREQ
)) | (via_read(via1
, vACR
) & SR_OUT
);
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
);
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 */
1793 printk("cuda: state=idle, status=%x want=%x\n",
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
);
1802 via_write(via1
, vBufB
, via_read(via1
,vBufB
)&~TIP
);
1804 else if(macintosh_config
->adb_type
==MAC_ADB_IISI
)
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
);
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
);
1820 via_write(via1
, vBufB
, via_read(via1
, vBufB
) | TACK
);
1822 via_write(via1
, vBufB
, via_read(via1
, vBufB
) & ~TACK
);
1824 else if(macintosh_config
->adb_type
==MAC_ADB_II
)
1827 printk("adb_macII: state=idle status=%x want=%x\n",
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
;
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)
1844 printk("cuda: state=awaiting_reply, status=%x want=%x\n",
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
);
1853 via_write(via1
,vBufB
,
1854 via_read(via1
, vBufB
)&~TIP
);
1856 else if(macintosh_config
->adb_type
==MAC_ADB_IISI
)
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
);
1866 #if (ADBDEBUG & ADBDEBUG_READ)
1867 if (console_loglevel
== 10)
1868 printk("adb_IIsi: reading reply: %x (%x %x) ",
1869 x
, adb_state
, status
);
1872 if( via_read(via1
,vBufB
) & TREQ
)
1878 via_write(via1
, vBufB
, via_read(via1
, vBufB
) | TACK
);
1880 via_write(via1
, vBufB
, via_read(via1
, vBufB
) & ~TACK
);
1882 adb_state
= reading
;
1883 reply_ptr
= current_req
->reply
;
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
);
1896 if (status
== TREQ
+ TIP
+ SR_OUT
)
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
);
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
);
1916 via_write(via1
,vSR
,current_req
->data
[1]);
1917 via_write(via1
, vBufB
,
1918 via_read(via1
, vBufB
)^TACK
);
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
) )
1930 #if (ADBDEBUG & ADBDEBUG_WRITE)
1931 if (console_loglevel
== 10)
1932 printk("adb_macIIsi: send collison, aborting!\n");
1935 via_write(via1
, vACR
,
1936 via_read(via1
, vACR
)&~SR_OUT
);
1938 x
= via_read(via1
, vSR
);
1939 /* set ADB state to 'idle' */
1940 via_write(via1
, vBufB
,
1941 via_read(via1
,vBufB
) & ~(TIP
|TACK
));
1948 /* set the shift register to shift out and send a byte */
1950 via_write(via1
, vACR
, via_read(via1
, vACR
) | SR_OUT
);
1952 via_write(via1
, vSR
, current_req
->data
[1]);
1953 /* signal 'byte ready' */
1954 via_write(via1
, vBufB
, via_read(via1
, vBufB
) | TACK
);
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
);
1968 adb_state
= sending
;
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
);
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
);
1996 /* set the shift register to shift in */
1997 via_write(via1
, vACR
, via_read(via1
, vACR
)|SR_OUT
);
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
);
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
;
2025 * Reply expected, but none
2026 * possible -> fake reply.
2028 #if (ADBDEBUG & ADBDEBUG_PROT)
2029 printk("ADB: reply expected on Listen, faking reply\n");
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;
2037 current_req
= req
->next
;
2041 * ready with this one, run
2044 /* set state to idle !! */
2046 if (current_req
|| retry_req
)
2052 current_req
= req
->next
;
2055 /* not sure about this */
2062 #if (ADBDEBUG & ADBDEBUG_WRITE)
2063 if (console_loglevel
== 10)
2064 printk(" %x (%x %x) ",
2065 req
->data
[data_index
], adb_state
, status
);
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
);
2079 /* XXX: need to check for collision?? */
2080 /* set the shift register to shift out and send a byte */
2082 via_write(via1
, vACR
, via_read(via1
, vACR
)|SR_OUT
);
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
);
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");
2109 /* Sanity check - botched in orig. code! */
2111 printk("adb_cuda: reply buffer overrun!\n");
2115 reply_ptr
= current_req
->reply
;
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
);
2127 if(macintosh_config
->adb_type
==MAC_ADB_CUDA
)
2131 /* that's all folks */
2132 via_write(via1
, vBufB
,
2133 via_read(via1
, vBufB
)|TACK
|TIP
);
2134 adb_state
= read_done
;
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
);
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
);
2153 via_write(via1
, vBufB
, via_read(via1
, vBufB
) & ~TACK
);
2154 /* end of frame?? */
2157 #if (ADBDEBUG & ADBDEBUG_READ)
2158 if (console_loglevel
== 10)
2159 printk("adb_IIsi: end of frame!\n");
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
)
2173 via_write(via1
, vBufB
,
2174 via_read(via1
, vBufB
)|TACK
|TIP
);
2175 adb_state
= read_done
;
2179 #if (ADBDEBUG & ADBDEBUG_STATUS)
2180 if(status
!=TIP
+TREQ
)
2181 printk("macII_adb: state=reading status=%x\n", status
);
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
)
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
);
2202 req
->reply_len
= reply_ptr
- req
->reply
;
2204 current_req
= req
->next
;
2210 adb_input(cuda_rbuf
, reply_ptr
- cuda_rbuf
, regs
);
2213 if (macintosh_config
->adb_type
==MAC_ADB_CUDA
2216 via_write(via1
, vBufB
,
2217 via_read(via1
, vBufB
)&~TIP
);
2218 adb_state
= reading
;
2219 reply_ptr
= cuda_rbuf
;
2222 else if (macintosh_config
->adb_type
==MAC_ADB_IISI
2223 && !(status
& TREQ
))
2226 via_write(via1
, vBufB
,
2227 via_read(via1
, vBufB
) | TIP
);
2228 adb_state
= reading
;
2229 reply_ptr
= cuda_rbuf
;
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
)
2262 /* what's in buf[1] ?? */
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
);
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
);
2278 if (adb_handler
[id
].handler
!= 0)
2280 (*adb_handler
[id
].handler
)(buf
, nb
, regs
);
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
]);
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
;
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
)
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
) {
2343 if (signal_pending(current
)) {
2350 current
->state
= TASK_RUNNING
;
2351 remove_wait_queue(&adb_wait
, &wait
);
2356 static void adb_write_done(struct adb_request
*req
)
2358 if (!req
->got_reply
) {
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
)
2377 switch (MINOR(inode
->i_rdev
) >> ADB_TYPE_SHIFT
) {
2379 /* see code below */
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
);
2393 state
= kmalloc(sizeof(struct adbdev_state
), GFP_KERNEL
);
2396 file
->private_data
= state
;
2397 state
->req
.reply_expected
= 0;
2401 static void adb_release(struct inode
*inode
, struct file
*file
)
2403 struct adbdev_state
*state
= file
->private_data
;
2406 file
->private_data
= NULL
;
2407 if (state
->req
.reply_expected
&& !state
->req
.got_reply
)
2408 if (adb_wait_reply(state
, file
))
2415 static int adb_lseek(struct inode
*inode
, struct file
*file
,
2416 off_t offset
, int origin
)
2421 static int adb_read(struct inode
*inode
, struct file
*file
,
2422 char *buf
, int count
)
2425 struct adbdev_state
*state
= file
->private_data
;
2429 if (count
> sizeof(state
->req
.reply
))
2430 count
= sizeof(state
->req
.reply
);
2431 ret
= verify_area(VERIFY_WRITE
, buf
, count
);
2435 if (!state
->req
.reply_expected
)
2438 ret
= adb_wait_reply(state
, file
);
2442 state
->req
.reply_expected
= 0;
2443 ret
= state
->req
.reply_len
;
2444 copy_to_user(buf
, state
->req
.reply
, ret
);
2449 static int adb_write(struct inode
*inode
, struct file
*file
,
2450 const char *buf
, int count
)
2453 struct adbdev_state
*state
= file
->private_data
;
2455 if (count
< 2 || count
> sizeof(state
->req
.data
))
2457 ret
= verify_area(VERIFY_READ
, buf
, count
);
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
);
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
);
2474 switch (adb_hardware
) {
2478 state
->req
.reply_expected
= 1;
2479 cuda_send_request(&state
->req
);
2483 if (state
->req
.data
[0] != ADB_PACKET
)
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
);
2498 static struct file_operations adb_fops
= {
2502 NULL
, /* no readdir */
2503 NULL
, /* no poll yet */
2504 NULL
, /* no ioctl yet */
2510 int adbdev_register(int subtype
, struct file_operations
*fops
)
2512 if (subtype
< 0 || subtype
> 15)
2514 if (adb_cooked
[subtype
])
2516 adb_cooked
[subtype
] = fops
;
2520 int adbdev_unregister(int subtype
)
2522 if (subtype
< 0 || subtype
> 15)
2524 if (!adb_cooked
[subtype
])
2526 adb_cooked
[subtype
] = NULL
;
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
)
2556 struct wait_queue wait
= { current
, NULL
};
2558 #if (ADBDEBUG & ADBDEBUG_DEVICE)
2559 printk("ADB request: wait_reply (blocking ... \n");
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
) {
2570 if (signal_pending(current
)) {
2577 current
->state
= TASK_RUNNING
;
2578 remove_wait_queue(&adb_wait
, &wait
);
2583 static void adb_write_done(struct adb_request
*req
)
2585 if (!req
->got_reply
) {
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
);
2599 file
->private_data
= state
;
2600 state
->req
.reply_expected
= 0;
2604 static void adb_release(struct inode
*inode
, struct file
*file
)
2606 struct adbdev_state
*state
= file
->private_data
;
2609 file
->private_data
= NULL
;
2610 if (state
->req
.reply_expected
&& !state
->req
.got_reply
)
2611 if (adb_wait_reply(state
, file
))
2618 static int adb_lseek(struct inode
*inode
, struct file
*file
,
2619 off_t offset
, int origin
)
2624 static int adb_read(struct inode
*inode
, struct file
*file
,
2625 char *buf
, int count
)
2628 struct adbdev_state
*state
= file
->private_data
;
2632 if (count
> sizeof(state
->req
.reply
))
2633 count
= sizeof(state
->req
.reply
);
2634 ret
= verify_area(VERIFY_WRITE
, buf
, count
);
2638 if (!state
->req
.reply_expected
)
2641 ret
= adb_wait_reply(state
, file
);
2645 ret
= state
->req
.reply_len
;
2646 memcpy_tofs(buf
, state
->req
.reply
, ret
);
2647 state
->req
.reply_expected
= 0;
2652 static int adb_write(struct inode
*inode
, struct file
*file
,
2653 const char *buf
, int count
)
2656 struct adbdev_state
*state
= file
->private_data
;
2658 if (count
< 2 || count
> sizeof(state
->req
.data
))
2660 ret
= verify_area(VERIFY_READ
, buf
, count
);
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
);
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
);
2682 static struct file_operations adb_fops
= {
2686 NULL
, /* no readdir */
2687 NULL
, /* no select */
2688 NULL
, /* no ioctl */
2694 static struct miscdevice adb_dev
= {
2700 void adbdev_init(void)
2702 misc_register(&adb_dev
);
2705 #endif /* old ADB device */