2 * Device driver for the Apple Desktop Bus
3 * and the /dev/adb device on macintoshes.
5 * Copyright (C) 1996 Paul Mackerras.
7 #include <linux/types.h>
8 #include <linux/errno.h>
9 #include <linux/kernel.h>
10 #include <linux/malloc.h>
13 #include <linux/sched.h>
18 #include <asm/uaccess.h>
19 #include <asm/hydra.h>
22 enum adb_hw adb_hardware
= ADB_NONE
;
23 int (*adb_send_request
)(struct adb_request
*req
, int sync
);
24 int (*adb_autopoll
)(int on
);
25 static void adb_scan_bus(void);
27 static struct adb_handler
{
28 void (*handler
)(unsigned char *, int, struct pt_regs
*, int);
35 static int adb_nodev(void)
41 static void printADBreply(struct adb_request
*req
)
45 printk("adb reply (%d)", req
->reply_len
);
46 for(i
= 0; i
< req
->reply_len
; i
++)
47 printk(" %x", req
->reply
[i
]);
53 static void adb_scan_bus(void)
55 int i
, highFree
=0, noMovement
;
56 struct adb_request req
;
59 /*adb_request(&req, NULL, ADBREQ_SYNC, 1, 0);*/
61 /* assumes adb_handler[] is all zeroes at this point */
62 for (i
= 1; i
< 16; i
++) {
63 /* see if there is anything at address i */
64 adb_request(&req
, NULL
, ADBREQ_SYNC
| ADBREQ_REPLY
, 1,
66 if (req
.reply_len
> 1)
67 /* one or more devices at this address */
68 adb_handler
[i
].original_address
= i
;
69 else if (i
> highFree
)
73 /* Note we reset noMovement to 0 each time we move a device */
74 for (noMovement
= 1; noMovement
< 2 && highFree
> 0; noMovement
++) {
75 for (i
= 1; i
< 16; i
++) {
76 if (adb_handler
[i
].original_address
== 0)
79 * Send a "talk register 3" command to address i
80 * to provoke a collision if there is more than
81 * one device at this address.
83 adb_request(&req
, NULL
, ADBREQ_SYNC
| ADBREQ_REPLY
, 1,
86 * Move the device(s) which didn't detect a
87 * collision to address `highFree'. Hopefully
88 * this only moves one device.
90 adb_request(&req
, NULL
, ADBREQ_SYNC
, 3,
91 (i
<< 4) | 0xb, (highFree
| 0x60), 0xfe);
93 * Test whether there are any device(s) left
96 adb_request(&req
, NULL
, ADBREQ_SYNC
| ADBREQ_REPLY
, 1,
98 if (req
.reply_len
> 1) {
100 * There are still one or more devices
101 * left at address i. Register the one(s)
102 * we moved to `highFree', and find a new
103 * value for highFree.
105 adb_handler
[highFree
].original_address
=
106 adb_handler
[i
].original_address
;
107 while (highFree
> 0 &&
108 adb_handler
[highFree
].original_address
)
117 * No devices left at address i; move the
118 * one(s) we moved to `highFree' back to i.
120 adb_request(&req
, NULL
, ADBREQ_SYNC
, 3,
121 (highFree
<< 4) | 0xb,
127 /* Now fill in the handler_id field of the adb_handler entries. */
128 printk(KERN_DEBUG
"adb devices:");
129 for (i
= 1; i
< 16; i
++) {
130 if (adb_handler
[i
].original_address
== 0)
132 adb_request(&req
, NULL
, ADBREQ_SYNC
| ADBREQ_REPLY
, 1,
134 adb_handler
[i
].handler_id
= req
.reply
[2];
135 printk(" [%d]: %d %x", i
, adb_handler
[i
].original_address
,
136 adb_handler
[i
].handler_id
);
143 adb_send_request
= (void *) adb_nodev
;
144 adb_autopoll
= (void *) adb_nodev
;
145 if ( (_machine
!= _MACH_chrp
) && (_machine
!= _MACH_Pmac
) )
150 if (adb_hardware
== ADB_NONE
)
151 printk(KERN_WARNING
"Warning: no ADB interface detected\n");
159 adb_request(struct adb_request
*req
, void (*done
)(struct adb_request
*),
160 int flags
, int nbytes
, ...)
164 struct adb_request sreq
;
168 flags
|= ADBREQ_SYNC
;
170 req
->nbytes
= nbytes
;
172 req
->reply_expected
= flags
& ADBREQ_REPLY
;
173 va_start(list
, nbytes
);
174 for (i
= 0; i
< nbytes
; ++i
)
175 req
->data
[i
] = va_arg(list
, int);
177 return adb_send_request(req
, flags
& ADBREQ_SYNC
);
180 /* Ultimately this should return the number of devices with
181 the given default id. */
183 adb_register(int default_id
, int handler_id
, struct adb_ids
*ids
,
184 void (*handler
)(unsigned char *, int, struct pt_regs
*, int))
189 for (i
= 1; i
< 16; i
++) {
190 if ((adb_handler
[i
].original_address
== default_id
) ||
191 (adb_handler
[i
].handler_id
== handler_id
)) {
192 if (adb_handler
[i
].handler
!= 0) {
194 "Two handlers for ADB device %d\n",
198 adb_handler
[i
].handler
= handler
;
199 ids
->id
[ids
->nids
++] = i
;
206 adb_input(unsigned char *buf
, int nb
, struct pt_regs
*regs
, int autopoll
)
209 static int dump_adb_input
= 0;
212 if (dump_adb_input
) {
213 printk(KERN_INFO
"adb packet: ");
214 for (i
= 0; i
< nb
; ++i
)
215 printk(" %x", buf
[i
]);
216 printk(", id = %d\n", id
);
218 if (adb_handler
[id
].handler
!= 0) {
219 (*adb_handler
[id
].handler
)(buf
, nb
, regs
, autopoll
);
224 * /dev/adb device driver.
227 #define ADB_MAJOR 56 /* major number for /dev/adb */
229 extern void adbdev_init(void);
231 struct adbdev_state
{
234 struct adb_request
*completed
;
235 struct wait_queue
*wait_queue
;
239 static void adb_write_done(struct adb_request
*req
)
241 struct adbdev_state
*state
= (struct adbdev_state
*) req
->arg
;
244 if (!req
->complete
) {
248 spin_lock_irqsave(&state
->lock
, flags
);
249 atomic_dec(&state
->n_pending
);
252 if (atomic_read(&state
->n_pending
) == 0) {
253 spin_unlock_irqrestore(&state
->lock
, flags
);
258 struct adb_request
**ap
= &state
->completed
;
263 wake_up_interruptible(&state
->wait_queue
);
265 spin_unlock_irqrestore(&state
->lock
, flags
);
268 static int adb_open(struct inode
*inode
, struct file
*file
)
270 struct adbdev_state
*state
;
272 if (MINOR(inode
->i_rdev
) > 0 || adb_hardware
== ADB_NONE
)
274 state
= kmalloc(sizeof(struct adbdev_state
), GFP_KERNEL
);
277 file
->private_data
= state
;
278 spin_lock_init(&state
->lock
);
279 atomic_set(&state
->n_pending
, 0);
280 state
->completed
= NULL
;
281 state
->wait_queue
= NULL
;
287 static int adb_release(struct inode
*inode
, struct file
*file
)
289 struct adbdev_state
*state
= file
->private_data
;
293 file
->private_data
= NULL
;
294 spin_lock_irqsave(&state
->lock
, flags
);
295 if (atomic_read(&state
->n_pending
) == 0
296 && state
->completed
== NULL
) {
297 spin_unlock_irqrestore(&state
->lock
, flags
);
301 spin_unlock_irqrestore(&state
->lock
, flags
);
307 static long long adb_lseek(struct file
*file
, loff_t offset
, int origin
)
312 static ssize_t
adb_read(struct file
*file
, char *buf
,
313 size_t count
, loff_t
*ppos
)
316 struct adbdev_state
*state
= file
->private_data
;
317 struct adb_request
*req
;
318 struct wait_queue wait
= { current
, NULL
};
323 if (count
> sizeof(req
->reply
))
324 count
= sizeof(req
->reply
);
325 ret
= verify_area(VERIFY_WRITE
, buf
, count
);
330 add_wait_queue(&state
->wait_queue
, &wait
);
331 current
->state
= TASK_INTERRUPTIBLE
;
334 spin_lock_irqsave(&state
->lock
, flags
);
335 req
= state
->completed
;
337 state
->completed
= req
->next
;
338 else if (atomic_read(&state
->n_pending
) == 0)
340 spin_unlock_irqrestore(&state
->lock
, flags
);
341 if (req
!= NULL
|| ret
!= 0)
344 if (file
->f_flags
& O_NONBLOCK
) {
348 if (signal_pending(current
)) {
355 current
->state
= TASK_RUNNING
;
356 remove_wait_queue(&state
->wait_queue
, &wait
);
361 ret
= req
->reply_len
;
364 if (ret
> 0 && copy_to_user(buf
, req
->reply
, ret
))
371 static ssize_t
adb_write(struct file
*file
, const char *buf
,
372 size_t count
, loff_t
*ppos
)
375 struct adbdev_state
*state
= file
->private_data
;
376 struct adb_request
*req
;
378 if (count
< 2 || count
> sizeof(req
->data
))
380 ret
= verify_area(VERIFY_READ
, buf
, count
);
384 req
= (struct adb_request
*) kmalloc(sizeof(struct adb_request
),
390 req
->done
= adb_write_done
;
391 req
->arg
= (void *) state
;
395 if (copy_from_user(req
->data
, buf
, count
))
398 atomic_inc(&state
->n_pending
);
399 switch (adb_hardware
) {
404 req
->reply_expected
= 1;
405 ret
= cuda_send_request(req
);
408 if (req
->data
[0] != ADB_PACKET
) {
409 ret
= pmu_send_request(req
);
412 /* else fall through */
415 if (req
->data
[0] != ADB_PACKET
)
417 for (i
= 0; i
< req
->nbytes
-1; ++i
)
418 req
->data
[i
] = req
->data
[i
+1];
420 req
->reply_expected
= ((req
->data
[0] & 0xc) == 0xc);
421 ret
= adb_send_request(req
, 0);
426 atomic_dec(&state
->n_pending
);
436 static struct file_operations adb_fops
= {
440 NULL
, /* no readdir */
441 NULL
, /* no poll yet */
442 NULL
, /* no ioctl yet */
451 if ( (_machine
!= _MACH_chrp
) && (_machine
!= _MACH_Pmac
) )
453 if (register_chrdev(ADB_MAJOR
, "adb", &adb_fops
))
454 printk(KERN_ERR
"adb: unable to get major %d\n", ADB_MAJOR
);