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 BLOCKING_NOTIFIER_HEAD(adb_client_list
);
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
;
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
;
324 pmu_register_sleep_notifier(&adb_sleep_notifier
);
325 #endif /* CONFIG_PM */
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
);
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
= blocking_notifier_call_chain(&adb_client_list
,
358 ADB_MSG_POWERDOWN
, NULL
);
359 if (ret
& NOTIFY_STOP_MASK
) {
360 up(&adb_probe_mutex
);
361 return PBOOK_SLEEP_REFUSE
;
364 case PBOOK_SLEEP_REJECT
:
367 up(&adb_probe_mutex
);
372 case PBOOK_SLEEP_NOW
:
376 up(&adb_probe_mutex
);
380 return PBOOK_SLEEP_OK
;
382 #endif /* CONFIG_PM */
385 do_adb_reset_bus(void)
389 if (adb_controller
== NULL
)
392 if (adb_controller
->autopoll
)
393 adb_controller
->autopoll(0);
395 nret
= blocking_notifier_call_chain(&adb_client_list
,
396 ADB_MSG_PRE_RESET
, NULL
);
397 if (nret
& NOTIFY_STOP_MASK
) {
398 if (adb_controller
->autopoll
)
399 adb_controller
->autopoll(autopoll_devs
);
403 if (sleepy_trackpad
) {
404 /* Let the trackpad settle down */
408 down(&adb_handler_sem
);
409 write_lock_irq(&adb_handler_lock
);
410 memset(adb_handler
, 0, sizeof(adb_handler
));
411 write_unlock_irq(&adb_handler_lock
);
413 /* That one is still a bit synchronous, oh well... */
414 if (adb_controller
->reset_bus
)
415 ret
= adb_controller
->reset_bus();
419 if (sleepy_trackpad
) {
420 /* Let the trackpad settle down */
425 autopoll_devs
= adb_scan_bus();
426 if (adb_controller
->autopoll
)
427 adb_controller
->autopoll(autopoll_devs
);
429 up(&adb_handler_sem
);
431 nret
= blocking_notifier_call_chain(&adb_client_list
,
432 ADB_MSG_POST_RESET
, NULL
);
433 if (nret
& NOTIFY_STOP_MASK
)
442 if ((adb_controller
== NULL
)||(adb_controller
->poll
== NULL
))
444 adb_controller
->poll();
448 adb_probe_wakeup(struct adb_request
*req
)
450 complete(&adb_probe_task_comp
);
453 /* Static request used during probe */
454 static struct adb_request adb_sreq
;
455 static unsigned long adb_sreq_lock
; // Use semaphore ! */
458 adb_request(struct adb_request
*req
, void (*done
)(struct adb_request
*),
459 int flags
, int nbytes
, ...)
465 if ((adb_controller
== NULL
) || (adb_controller
->send_request
== NULL
))
469 if (req
== NULL
&& (flags
& ADBREQ_NOSEND
))
473 if (test_and_set_bit(0,&adb_sreq_lock
)) {
474 printk("adb.c: Warning: contention on static request !\n");
478 flags
|= ADBREQ_SYNC
;
482 req
->nbytes
= nbytes
+1;
484 req
->reply_expected
= flags
& ADBREQ_REPLY
;
485 req
->data
[0] = ADB_PACKET
;
486 va_start(list
, nbytes
);
487 for (i
= 0; i
< nbytes
; ++i
)
488 req
->data
[i
+1] = va_arg(list
, int);
491 if (flags
& ADBREQ_NOSEND
)
494 /* Synchronous requests send from the probe thread cause it to
495 * block. Beware that the "done" callback will be overriden !
497 if ((flags
& ADBREQ_SYNC
) &&
498 (current
->pid
&& adb_probe_task_pid
&&
499 adb_probe_task_pid
== current
->pid
)) {
500 req
->done
= adb_probe_wakeup
;
501 rc
= adb_controller
->send_request(req
, 0);
502 if (rc
|| req
->complete
)
504 wait_for_completion(&adb_probe_task_comp
);
509 rc
= adb_controller
->send_request(req
, flags
& ADBREQ_SYNC
);
512 clear_bit(0, &adb_sreq_lock
);
517 /* Ultimately this should return the number of devices with
518 the given default id.
519 And it does it now ! Note: changed behaviour: This function
520 will now register if default_id _and_ handler_id both match
521 but handler_id can be left to 0 to match with default_id only.
522 When handler_id is set, this function will try to adjust
523 the handler_id id it doesn't match. */
525 adb_register(int default_id
, int handler_id
, struct adb_ids
*ids
,
526 void (*handler
)(unsigned char *, int, struct pt_regs
*, int))
530 down(&adb_handler_sem
);
532 for (i
= 1; i
< 16; i
++) {
533 if ((adb_handler
[i
].original_address
== default_id
) &&
534 (!handler_id
|| (handler_id
== adb_handler
[i
].handler_id
) ||
535 try_handler_change(i
, handler_id
))) {
536 if (adb_handler
[i
].handler
!= 0) {
538 "Two handlers for ADB device %d\n",
542 write_lock_irq(&adb_handler_lock
);
543 adb_handler
[i
].handler
= handler
;
544 write_unlock_irq(&adb_handler_lock
);
545 ids
->id
[ids
->nids
++] = i
;
548 up(&adb_handler_sem
);
553 adb_unregister(int index
)
557 down(&adb_handler_sem
);
558 write_lock_irq(&adb_handler_lock
);
559 if (adb_handler
[index
].handler
) {
560 while(adb_handler
[index
].busy
) {
561 write_unlock_irq(&adb_handler_lock
);
563 write_lock_irq(&adb_handler_lock
);
566 adb_handler
[index
].handler
= NULL
;
568 write_unlock_irq(&adb_handler_lock
);
569 up(&adb_handler_sem
);
574 adb_input(unsigned char *buf
, int nb
, struct pt_regs
*regs
, int autopoll
)
577 static int dump_adb_input
= 0;
580 void (*handler
)(unsigned char *, int, struct pt_regs
*, int);
582 /* We skip keystrokes and mouse moves when the sleep process
583 * has been started. We stop autopoll, but this is another security
589 if (dump_adb_input
) {
590 printk(KERN_INFO
"adb packet: ");
591 for (i
= 0; i
< nb
; ++i
)
592 printk(" %x", buf
[i
]);
593 printk(", id = %d\n", id
);
595 write_lock_irqsave(&adb_handler_lock
, flags
);
596 handler
= adb_handler
[id
].handler
;
598 adb_handler
[id
].busy
= 1;
599 write_unlock_irqrestore(&adb_handler_lock
, flags
);
600 if (handler
!= NULL
) {
601 (*handler
)(buf
, nb
, regs
, autopoll
);
603 adb_handler
[id
].busy
= 0;
608 /* Try to change handler to new_id. Will return 1 if successful. */
609 static int try_handler_change(int address
, int new_id
)
611 struct adb_request req
;
613 if (adb_handler
[address
].handler_id
== new_id
)
615 adb_request(&req
, NULL
, ADBREQ_SYNC
, 3,
616 ADB_WRITEREG(address
, 3), address
| 0x20, new_id
);
617 adb_request(&req
, NULL
, ADBREQ_SYNC
| ADBREQ_REPLY
, 1,
618 ADB_READREG(address
, 3));
619 if (req
.reply_len
< 2)
621 if (req
.reply
[2] != new_id
)
623 adb_handler
[address
].handler_id
= req
.reply
[2];
629 adb_try_handler_change(int address
, int new_id
)
633 down(&adb_handler_sem
);
634 ret
= try_handler_change(address
, new_id
);
635 up(&adb_handler_sem
);
640 adb_get_infos(int address
, int *original_address
, int *handler_id
)
642 down(&adb_handler_sem
);
643 *original_address
= adb_handler
[address
].original_address
;
644 *handler_id
= adb_handler
[address
].handler_id
;
645 up(&adb_handler_sem
);
647 return (*original_address
!= 0);
652 * /dev/adb device driver.
655 #define ADB_MAJOR 56 /* major number for /dev/adb */
657 struct adbdev_state
{
660 struct adb_request
*completed
;
661 wait_queue_head_t wait_queue
;
665 static void adb_write_done(struct adb_request
*req
)
667 struct adbdev_state
*state
= (struct adbdev_state
*) req
->arg
;
670 if (!req
->complete
) {
674 spin_lock_irqsave(&state
->lock
, flags
);
675 atomic_dec(&state
->n_pending
);
678 if (atomic_read(&state
->n_pending
) == 0) {
679 spin_unlock_irqrestore(&state
->lock
, flags
);
684 struct adb_request
**ap
= &state
->completed
;
689 wake_up_interruptible(&state
->wait_queue
);
691 spin_unlock_irqrestore(&state
->lock
, flags
);
695 do_adb_query(struct adb_request
*req
)
701 case ADB_QUERY_GETDEVINFO
:
704 down(&adb_handler_sem
);
705 req
->reply
[0] = adb_handler
[req
->data
[2]].original_address
;
706 req
->reply
[1] = adb_handler
[req
->data
[2]].handler_id
;
707 up(&adb_handler_sem
);
717 static int adb_open(struct inode
*inode
, struct file
*file
)
719 struct adbdev_state
*state
;
721 if (iminor(inode
) > 0 || adb_controller
== NULL
)
723 state
= kmalloc(sizeof(struct adbdev_state
), GFP_KERNEL
);
726 file
->private_data
= state
;
727 spin_lock_init(&state
->lock
);
728 atomic_set(&state
->n_pending
, 0);
729 state
->completed
= NULL
;
730 init_waitqueue_head(&state
->wait_queue
);
736 static int adb_release(struct inode
*inode
, struct file
*file
)
738 struct adbdev_state
*state
= file
->private_data
;
743 file
->private_data
= NULL
;
744 spin_lock_irqsave(&state
->lock
, flags
);
745 if (atomic_read(&state
->n_pending
) == 0
746 && state
->completed
== NULL
) {
747 spin_unlock_irqrestore(&state
->lock
, flags
);
751 spin_unlock_irqrestore(&state
->lock
, flags
);
758 static ssize_t
adb_read(struct file
*file
, char __user
*buf
,
759 size_t count
, loff_t
*ppos
)
762 struct adbdev_state
*state
= file
->private_data
;
763 struct adb_request
*req
;
764 wait_queue_t wait
= __WAITQUEUE_INITIALIZER(wait
,current
);
769 if (count
> sizeof(req
->reply
))
770 count
= sizeof(req
->reply
);
771 if (!access_ok(VERIFY_WRITE
, buf
, count
))
775 spin_lock_irqsave(&state
->lock
, flags
);
776 add_wait_queue(&state
->wait_queue
, &wait
);
777 current
->state
= TASK_INTERRUPTIBLE
;
780 req
= state
->completed
;
782 state
->completed
= req
->next
;
783 else if (atomic_read(&state
->n_pending
) == 0)
785 if (req
!= NULL
|| ret
!= 0)
788 if (file
->f_flags
& O_NONBLOCK
) {
792 if (signal_pending(current
)) {
796 spin_unlock_irqrestore(&state
->lock
, flags
);
798 spin_lock_irqsave(&state
->lock
, flags
);
801 current
->state
= TASK_RUNNING
;
802 remove_wait_queue(&state
->wait_queue
, &wait
);
803 spin_unlock_irqrestore(&state
->lock
, flags
);
808 ret
= req
->reply_len
;
811 if (ret
> 0 && copy_to_user(buf
, req
->reply
, ret
))
818 static ssize_t
adb_write(struct file
*file
, const char __user
*buf
,
819 size_t count
, loff_t
*ppos
)
822 struct adbdev_state
*state
= file
->private_data
;
823 struct adb_request
*req
;
825 if (count
< 2 || count
> sizeof(req
->data
))
827 if (adb_controller
== NULL
)
829 if (!access_ok(VERIFY_READ
, buf
, count
))
832 req
= (struct adb_request
*) kmalloc(sizeof(struct adb_request
),
838 req
->done
= adb_write_done
;
839 req
->arg
= (void *) state
;
843 if (copy_from_user(req
->data
, buf
, count
))
846 atomic_inc(&state
->n_pending
);
848 /* If a probe is in progress or we are sleeping, wait for it to complete */
849 down(&adb_probe_mutex
);
851 /* Queries are special requests sent to the ADB driver itself */
852 if (req
->data
[0] == ADB_QUERY
) {
854 ret
= do_adb_query(req
);
857 up(&adb_probe_mutex
);
859 /* Special case for ADB_BUSRESET request, all others are sent to
861 else if ((req
->data
[0] == ADB_PACKET
)&&(count
> 1)
862 &&(req
->data
[1] == ADB_BUSRESET
)) {
863 ret
= do_adb_reset_bus();
864 up(&adb_probe_mutex
);
865 atomic_dec(&state
->n_pending
);
870 req
->reply_expected
= ((req
->data
[1] & 0xc) == 0xc);
871 if (adb_controller
&& adb_controller
->send_request
)
872 ret
= adb_controller
->send_request(req
, 0);
875 up(&adb_probe_mutex
);
879 atomic_dec(&state
->n_pending
);
889 static struct file_operations adb_fops
= {
890 .owner
= THIS_MODULE
,
895 .release
= adb_release
,
901 if (register_chrdev(ADB_MAJOR
, "adb", &adb_fops
)) {
902 printk(KERN_ERR
"adb: unable to get major %d\n", ADB_MAJOR
);
906 devfs_mk_cdev(MKDEV(ADB_MAJOR
, 0), S_IFCHR
| S_IRUSR
| S_IWUSR
, "adb");
908 adb_dev_class
= class_create(THIS_MODULE
, "adb");
909 if (IS_ERR(adb_dev_class
))
911 class_device_create(adb_dev_class
, NULL
, MKDEV(ADB_MAJOR
, 0), NULL
, "adb");