2 * Device driver for the Apple Desktop Bus
3 * and the /dev/adb device on macintoshes.
5 * Copyright (C) 1996 Paul Mackerras.
7 * Modified to declare controllers as structures, added
8 * client notification of bus reset and handles PowerBook
9 * sleep, by Benjamin Herrenschmidt.
13 * - /sys/bus/adb to list the devices and infos
14 * - more /dev/adb to allow userland to receive the
15 * flow of auto-polling datas from a given device.
16 * - move bus probe to a kernel thread
19 #include <linux/config.h>
20 #include <linux/types.h>
21 #include <linux/errno.h>
22 #include <linux/kernel.h>
23 #include <linux/slab.h>
24 #include <linux/module.h>
27 #include <linux/sched.h>
28 #include <linux/smp_lock.h>
29 #include <linux/adb.h>
30 #include <linux/cuda.h>
31 #include <linux/pmu.h>
32 #include <linux/notifier.h>
33 #include <linux/wait.h>
34 #include <linux/init.h>
35 #include <linux/delay.h>
36 #include <linux/spinlock.h>
37 #include <linux/completion.h>
38 #include <linux/device.h>
39 #include <linux/devfs_fs_kernel.h>
41 #include <asm/uaccess.h>
42 #include <asm/semaphore.h>
48 EXPORT_SYMBOL(adb_controller
);
49 EXPORT_SYMBOL(adb_client_list
);
51 extern struct adb_driver via_macii_driver
;
52 extern struct adb_driver via_maciisi_driver
;
53 extern struct adb_driver via_cuda_driver
;
54 extern struct adb_driver adb_iop_driver
;
55 extern struct adb_driver via_pmu_driver
;
56 extern struct adb_driver macio_adb_driver
;
58 static struct adb_driver
*adb_driver_list
[] = {
59 #ifdef CONFIG_ADB_MACII
62 #ifdef CONFIG_ADB_MACIISI
65 #ifdef CONFIG_ADB_CUDA
71 #if defined(CONFIG_ADB_PMU) || defined(CONFIG_ADB_PMU68K)
74 #ifdef CONFIG_ADB_MACIO
80 static struct class *adb_dev_class
;
82 struct adb_driver
*adb_controller
;
83 struct notifier_block
*adb_client_list
= NULL
;
84 static int adb_got_sleep
;
85 static int adb_inited
;
86 static pid_t adb_probe_task_pid
;
87 static DECLARE_MUTEX(adb_probe_mutex
);
88 static struct completion adb_probe_task_comp
;
89 static int sleepy_trackpad
;
90 static int autopoll_devs
;
93 #ifdef CONFIG_PMAC_PBOOK
94 static int adb_notify_sleep(struct pmu_sleep_notifier
*self
, int when
);
95 static struct pmu_sleep_notifier adb_sleep_notifier
= {
101 static int adb_scan_bus(void);
102 static int do_adb_reset_bus(void);
103 static void adbdev_init(void);
104 static int try_handler_change(int, int);
106 static struct adb_handler
{
107 void (*handler
)(unsigned char *, int, struct pt_regs
*, int);
108 int original_address
;
114 * The adb_handler_sem mutex protects all accesses to the original_address
115 * and handler_id fields of adb_handler[i] for all i, and changes to the
117 * Accesses to the handler field are protected by the adb_handler_lock
118 * rwlock. It is held across all calls to any handler, so that by the
119 * time adb_unregister returns, we know that the old handler isn't being
122 static DECLARE_MUTEX(adb_handler_sem
);
123 static DEFINE_RWLOCK(adb_handler_lock
);
126 static void printADBreply(struct adb_request
*req
)
130 printk("adb reply (%d)", req
->reply_len
);
131 for(i
= 0; i
< req
->reply_len
; i
++)
132 printk(" %x", req
->reply
[i
]);
139 static __inline__
void adb_wait_ms(unsigned int ms
)
141 if (current
->pid
&& adb_probe_task_pid
&&
142 adb_probe_task_pid
== current
->pid
)
148 static int adb_scan_bus(void)
150 int i
, highFree
=0, noMovement
;
152 struct adb_request req
;
154 /* assumes adb_handler[] is all zeroes at this point */
155 for (i
= 1; i
< 16; i
++) {
156 /* see if there is anything at address i */
157 adb_request(&req
, NULL
, ADBREQ_SYNC
| ADBREQ_REPLY
, 1,
159 if (req
.reply_len
> 1)
160 /* one or more devices at this address */
161 adb_handler
[i
].original_address
= i
;
162 else if (i
> highFree
)
166 /* Note we reset noMovement to 0 each time we move a device */
167 for (noMovement
= 1; noMovement
< 2 && highFree
> 0; noMovement
++) {
168 for (i
= 1; i
< 16; i
++) {
169 if (adb_handler
[i
].original_address
== 0)
172 * Send a "talk register 3" command to address i
173 * to provoke a collision if there is more than
174 * one device at this address.
176 adb_request(&req
, NULL
, ADBREQ_SYNC
| ADBREQ_REPLY
, 1,
179 * Move the device(s) which didn't detect a
180 * collision to address `highFree'. Hopefully
181 * this only moves one device.
183 adb_request(&req
, NULL
, ADBREQ_SYNC
, 3,
184 (i
<< 4) | 0xb, (highFree
| 0x60), 0xfe);
186 * See if anybody actually moved. This is suggested
189 * http://developer.apple.com/technotes/hw/hw_01.html
191 adb_request(&req
, NULL
, ADBREQ_SYNC
| ADBREQ_REPLY
, 1,
192 (highFree
<< 4) | 0xf);
193 if (req
.reply_len
<= 1) continue;
195 * Test whether there are any device(s) left
198 adb_request(&req
, NULL
, ADBREQ_SYNC
| ADBREQ_REPLY
, 1,
200 if (req
.reply_len
> 1) {
202 * There are still one or more devices
203 * left at address i. Register the one(s)
204 * we moved to `highFree', and find a new
205 * value for highFree.
207 adb_handler
[highFree
].original_address
=
208 adb_handler
[i
].original_address
;
209 while (highFree
> 0 &&
210 adb_handler
[highFree
].original_address
)
219 * No devices left at address i; move the
220 * one(s) we moved to `highFree' back to i.
222 adb_request(&req
, NULL
, ADBREQ_SYNC
, 3,
223 (highFree
<< 4) | 0xb,
229 /* Now fill in the handler_id field of the adb_handler entries. */
230 printk(KERN_DEBUG
"adb devices:");
231 for (i
= 1; i
< 16; i
++) {
232 if (adb_handler
[i
].original_address
== 0)
234 adb_request(&req
, NULL
, ADBREQ_SYNC
| ADBREQ_REPLY
, 1,
236 adb_handler
[i
].handler_id
= req
.reply
[2];
237 printk(" [%d]: %d %x", i
, adb_handler
[i
].original_address
,
238 adb_handler
[i
].handler_id
);
246 * This kernel task handles ADB probing. It dies once probing is
250 adb_probe_task(void *x
)
254 strcpy(current
->comm
, "kadbprobe");
256 sigfillset(&blocked
);
257 sigprocmask(SIG_BLOCK
, &blocked
, NULL
);
258 flush_signals(current
);
260 printk(KERN_INFO
"adb: starting probe task...\n");
262 printk(KERN_INFO
"adb: finished probe task...\n");
264 adb_probe_task_pid
= 0;
265 up(&adb_probe_mutex
);
271 __adb_probe_task(void *data
)
273 adb_probe_task_pid
= kernel_thread(adb_probe_task
, NULL
, SIGCHLD
| CLONE_KERNEL
);
276 static DECLARE_WORK(adb_reset_work
, __adb_probe_task
, NULL
);
281 if (__adb_probe_sync
) {
286 down(&adb_probe_mutex
);
287 schedule_work(&adb_reset_work
);
291 int __init
adb_init(void)
293 struct adb_driver
*driver
;
297 if ( (_machine
!= _MACH_chrp
) && (_machine
!= _MACH_Pmac
) )
305 /* xmon may do early-init */
310 adb_controller
= NULL
;
313 while ((driver
= adb_driver_list
[i
++]) != NULL
) {
314 if (!driver
->probe()) {
315 adb_controller
= driver
;
319 if ((adb_controller
== NULL
) || adb_controller
->init()) {
320 printk(KERN_WARNING
"Warning: no ADB interface detected\n");
321 adb_controller
= NULL
;
323 #ifdef CONFIG_PMAC_PBOOK
324 pmu_register_sleep_notifier(&adb_sleep_notifier
);
325 #endif /* CONFIG_PMAC_PBOOK */
327 if (machine_is_compatible("AAPL,PowerBook1998") ||
328 machine_is_compatible("PowerBook1,1"))
330 #endif /* CONFIG_PPC */
331 init_completion(&adb_probe_task_comp
);
338 __initcall(adb_init
);
340 #ifdef CONFIG_PMAC_PBOOK
342 * notify clients before sleep and reset bus afterwards
345 adb_notify_sleep(struct pmu_sleep_notifier
*self
, int when
)
350 case PBOOK_SLEEP_REQUEST
:
352 /* We need to get a lock on the probe thread */
353 down(&adb_probe_mutex
);
355 if (adb_controller
->autopoll
)
356 adb_controller
->autopoll(0);
357 ret
= notifier_call_chain(&adb_client_list
, ADB_MSG_POWERDOWN
, NULL
);
358 if (ret
& NOTIFY_STOP_MASK
) {
359 up(&adb_probe_mutex
);
360 return PBOOK_SLEEP_REFUSE
;
363 case PBOOK_SLEEP_REJECT
:
366 up(&adb_probe_mutex
);
371 case PBOOK_SLEEP_NOW
:
375 up(&adb_probe_mutex
);
379 return PBOOK_SLEEP_OK
;
381 #endif /* CONFIG_PMAC_PBOOK */
384 do_adb_reset_bus(void)
388 if (adb_controller
== NULL
)
391 if (adb_controller
->autopoll
)
392 adb_controller
->autopoll(0);
394 nret
= notifier_call_chain(&adb_client_list
, ADB_MSG_PRE_RESET
, NULL
);
395 if (nret
& NOTIFY_STOP_MASK
) {
396 if (adb_controller
->autopoll
)
397 adb_controller
->autopoll(autopoll_devs
);
401 if (sleepy_trackpad
) {
402 /* Let the trackpad settle down */
406 down(&adb_handler_sem
);
407 write_lock_irq(&adb_handler_lock
);
408 memset(adb_handler
, 0, sizeof(adb_handler
));
409 write_unlock_irq(&adb_handler_lock
);
411 /* That one is still a bit synchronous, oh well... */
412 if (adb_controller
->reset_bus
)
413 ret
= adb_controller
->reset_bus();
417 if (sleepy_trackpad
) {
418 /* Let the trackpad settle down */
423 autopoll_devs
= adb_scan_bus();
424 if (adb_controller
->autopoll
)
425 adb_controller
->autopoll(autopoll_devs
);
427 up(&adb_handler_sem
);
429 nret
= notifier_call_chain(&adb_client_list
, ADB_MSG_POST_RESET
, NULL
);
430 if (nret
& NOTIFY_STOP_MASK
)
439 if ((adb_controller
== NULL
)||(adb_controller
->poll
== NULL
))
441 adb_controller
->poll();
445 adb_probe_wakeup(struct adb_request
*req
)
447 complete(&adb_probe_task_comp
);
450 /* Static request used during probe */
451 static struct adb_request adb_sreq
;
452 static unsigned long adb_sreq_lock
; // Use semaphore ! */
455 adb_request(struct adb_request
*req
, void (*done
)(struct adb_request
*),
456 int flags
, int nbytes
, ...)
462 if ((adb_controller
== NULL
) || (adb_controller
->send_request
== NULL
))
466 if (req
== NULL
&& (flags
& ADBREQ_NOSEND
))
470 if (test_and_set_bit(0,&adb_sreq_lock
)) {
471 printk("adb.c: Warning: contention on static request !\n");
475 flags
|= ADBREQ_SYNC
;
479 req
->nbytes
= nbytes
+1;
481 req
->reply_expected
= flags
& ADBREQ_REPLY
;
482 req
->data
[0] = ADB_PACKET
;
483 va_start(list
, nbytes
);
484 for (i
= 0; i
< nbytes
; ++i
)
485 req
->data
[i
+1] = va_arg(list
, int);
488 if (flags
& ADBREQ_NOSEND
)
491 /* Synchronous requests send from the probe thread cause it to
492 * block. Beware that the "done" callback will be overriden !
494 if ((flags
& ADBREQ_SYNC
) &&
495 (current
->pid
&& adb_probe_task_pid
&&
496 adb_probe_task_pid
== current
->pid
)) {
497 req
->done
= adb_probe_wakeup
;
498 rc
= adb_controller
->send_request(req
, 0);
499 if (rc
|| req
->complete
)
501 wait_for_completion(&adb_probe_task_comp
);
506 rc
= adb_controller
->send_request(req
, flags
& ADBREQ_SYNC
);
509 clear_bit(0, &adb_sreq_lock
);
514 /* Ultimately this should return the number of devices with
515 the given default id.
516 And it does it now ! Note: changed behaviour: This function
517 will now register if default_id _and_ handler_id both match
518 but handler_id can be left to 0 to match with default_id only.
519 When handler_id is set, this function will try to adjust
520 the handler_id id it doesn't match. */
522 adb_register(int default_id
, int handler_id
, struct adb_ids
*ids
,
523 void (*handler
)(unsigned char *, int, struct pt_regs
*, int))
527 down(&adb_handler_sem
);
529 for (i
= 1; i
< 16; i
++) {
530 if ((adb_handler
[i
].original_address
== default_id
) &&
531 (!handler_id
|| (handler_id
== adb_handler
[i
].handler_id
) ||
532 try_handler_change(i
, handler_id
))) {
533 if (adb_handler
[i
].handler
!= 0) {
535 "Two handlers for ADB device %d\n",
539 write_lock_irq(&adb_handler_lock
);
540 adb_handler
[i
].handler
= handler
;
541 write_unlock_irq(&adb_handler_lock
);
542 ids
->id
[ids
->nids
++] = i
;
545 up(&adb_handler_sem
);
550 adb_unregister(int index
)
554 down(&adb_handler_sem
);
555 write_lock_irq(&adb_handler_lock
);
556 if (adb_handler
[index
].handler
) {
557 while(adb_handler
[index
].busy
) {
558 write_unlock_irq(&adb_handler_lock
);
560 write_lock_irq(&adb_handler_lock
);
563 adb_handler
[index
].handler
= NULL
;
565 write_unlock_irq(&adb_handler_lock
);
566 up(&adb_handler_sem
);
571 adb_input(unsigned char *buf
, int nb
, struct pt_regs
*regs
, int autopoll
)
574 static int dump_adb_input
= 0;
577 void (*handler
)(unsigned char *, int, struct pt_regs
*, int);
579 /* We skip keystrokes and mouse moves when the sleep process
580 * has been started. We stop autopoll, but this is another security
586 if (dump_adb_input
) {
587 printk(KERN_INFO
"adb packet: ");
588 for (i
= 0; i
< nb
; ++i
)
589 printk(" %x", buf
[i
]);
590 printk(", id = %d\n", id
);
592 write_lock_irqsave(&adb_handler_lock
, flags
);
593 handler
= adb_handler
[id
].handler
;
595 adb_handler
[id
].busy
= 1;
596 write_unlock_irqrestore(&adb_handler_lock
, flags
);
597 if (handler
!= NULL
) {
598 (*handler
)(buf
, nb
, regs
, autopoll
);
600 adb_handler
[id
].busy
= 0;
605 /* Try to change handler to new_id. Will return 1 if successful. */
606 static int try_handler_change(int address
, int new_id
)
608 struct adb_request req
;
610 if (adb_handler
[address
].handler_id
== new_id
)
612 adb_request(&req
, NULL
, ADBREQ_SYNC
, 3,
613 ADB_WRITEREG(address
, 3), address
| 0x20, new_id
);
614 adb_request(&req
, NULL
, ADBREQ_SYNC
| ADBREQ_REPLY
, 1,
615 ADB_READREG(address
, 3));
616 if (req
.reply_len
< 2)
618 if (req
.reply
[2] != new_id
)
620 adb_handler
[address
].handler_id
= req
.reply
[2];
626 adb_try_handler_change(int address
, int new_id
)
630 down(&adb_handler_sem
);
631 ret
= try_handler_change(address
, new_id
);
632 up(&adb_handler_sem
);
637 adb_get_infos(int address
, int *original_address
, int *handler_id
)
639 down(&adb_handler_sem
);
640 *original_address
= adb_handler
[address
].original_address
;
641 *handler_id
= adb_handler
[address
].handler_id
;
642 up(&adb_handler_sem
);
644 return (*original_address
!= 0);
649 * /dev/adb device driver.
652 #define ADB_MAJOR 56 /* major number for /dev/adb */
654 struct adbdev_state
{
657 struct adb_request
*completed
;
658 wait_queue_head_t wait_queue
;
662 static void adb_write_done(struct adb_request
*req
)
664 struct adbdev_state
*state
= (struct adbdev_state
*) req
->arg
;
667 if (!req
->complete
) {
671 spin_lock_irqsave(&state
->lock
, flags
);
672 atomic_dec(&state
->n_pending
);
675 if (atomic_read(&state
->n_pending
) == 0) {
676 spin_unlock_irqrestore(&state
->lock
, flags
);
681 struct adb_request
**ap
= &state
->completed
;
686 wake_up_interruptible(&state
->wait_queue
);
688 spin_unlock_irqrestore(&state
->lock
, flags
);
692 do_adb_query(struct adb_request
*req
)
698 case ADB_QUERY_GETDEVINFO
:
701 down(&adb_handler_sem
);
702 req
->reply
[0] = adb_handler
[req
->data
[2]].original_address
;
703 req
->reply
[1] = adb_handler
[req
->data
[2]].handler_id
;
704 up(&adb_handler_sem
);
714 static int adb_open(struct inode
*inode
, struct file
*file
)
716 struct adbdev_state
*state
;
718 if (iminor(inode
) > 0 || adb_controller
== NULL
)
720 state
= kmalloc(sizeof(struct adbdev_state
), GFP_KERNEL
);
723 file
->private_data
= state
;
724 spin_lock_init(&state
->lock
);
725 atomic_set(&state
->n_pending
, 0);
726 state
->completed
= NULL
;
727 init_waitqueue_head(&state
->wait_queue
);
733 static int adb_release(struct inode
*inode
, struct file
*file
)
735 struct adbdev_state
*state
= file
->private_data
;
740 file
->private_data
= NULL
;
741 spin_lock_irqsave(&state
->lock
, flags
);
742 if (atomic_read(&state
->n_pending
) == 0
743 && state
->completed
== NULL
) {
744 spin_unlock_irqrestore(&state
->lock
, flags
);
748 spin_unlock_irqrestore(&state
->lock
, flags
);
755 static ssize_t
adb_read(struct file
*file
, char __user
*buf
,
756 size_t count
, loff_t
*ppos
)
759 struct adbdev_state
*state
= file
->private_data
;
760 struct adb_request
*req
;
761 wait_queue_t wait
= __WAITQUEUE_INITIALIZER(wait
,current
);
766 if (count
> sizeof(req
->reply
))
767 count
= sizeof(req
->reply
);
768 if (!access_ok(VERIFY_WRITE
, buf
, count
))
772 spin_lock_irqsave(&state
->lock
, flags
);
773 add_wait_queue(&state
->wait_queue
, &wait
);
774 current
->state
= TASK_INTERRUPTIBLE
;
777 req
= state
->completed
;
779 state
->completed
= req
->next
;
780 else if (atomic_read(&state
->n_pending
) == 0)
782 if (req
!= NULL
|| ret
!= 0)
785 if (file
->f_flags
& O_NONBLOCK
) {
789 if (signal_pending(current
)) {
793 spin_unlock_irqrestore(&state
->lock
, flags
);
795 spin_lock_irqsave(&state
->lock
, flags
);
798 current
->state
= TASK_RUNNING
;
799 remove_wait_queue(&state
->wait_queue
, &wait
);
800 spin_unlock_irqrestore(&state
->lock
, flags
);
805 ret
= req
->reply_len
;
808 if (ret
> 0 && copy_to_user(buf
, req
->reply
, ret
))
815 static ssize_t
adb_write(struct file
*file
, const char __user
*buf
,
816 size_t count
, loff_t
*ppos
)
819 struct adbdev_state
*state
= file
->private_data
;
820 struct adb_request
*req
;
822 if (count
< 2 || count
> sizeof(req
->data
))
824 if (adb_controller
== NULL
)
826 if (!access_ok(VERIFY_READ
, buf
, count
))
829 req
= (struct adb_request
*) kmalloc(sizeof(struct adb_request
),
835 req
->done
= adb_write_done
;
836 req
->arg
= (void *) state
;
840 if (copy_from_user(req
->data
, buf
, count
))
843 atomic_inc(&state
->n_pending
);
845 /* If a probe is in progress or we are sleeping, wait for it to complete */
846 down(&adb_probe_mutex
);
848 /* Queries are special requests sent to the ADB driver itself */
849 if (req
->data
[0] == ADB_QUERY
) {
851 ret
= do_adb_query(req
);
854 up(&adb_probe_mutex
);
856 /* Special case for ADB_BUSRESET request, all others are sent to
858 else if ((req
->data
[0] == ADB_PACKET
)&&(count
> 1)
859 &&(req
->data
[1] == ADB_BUSRESET
)) {
860 ret
= do_adb_reset_bus();
861 up(&adb_probe_mutex
);
862 atomic_dec(&state
->n_pending
);
867 req
->reply_expected
= ((req
->data
[1] & 0xc) == 0xc);
868 if (adb_controller
&& adb_controller
->send_request
)
869 ret
= adb_controller
->send_request(req
, 0);
872 up(&adb_probe_mutex
);
876 atomic_dec(&state
->n_pending
);
886 static struct file_operations adb_fops
= {
887 .owner
= THIS_MODULE
,
892 .release
= adb_release
,
898 if (register_chrdev(ADB_MAJOR
, "adb", &adb_fops
)) {
899 printk(KERN_ERR
"adb: unable to get major %d\n", ADB_MAJOR
);
903 devfs_mk_cdev(MKDEV(ADB_MAJOR
, 0), S_IFCHR
| S_IRUSR
| S_IWUSR
, "adb");
905 adb_dev_class
= class_create(THIS_MODULE
, "adb");
906 if (IS_ERR(adb_dev_class
))
908 class_device_create(adb_dev_class
, MKDEV(ADB_MAJOR
, 0), NULL
, "adb");