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 * - /proc/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>
26 #include <linux/devfs_fs_kernel.h>
28 #include <linux/sched.h>
29 #include <linux/smp_lock.h>
30 #include <linux/adb.h>
31 #include <linux/cuda.h>
32 #include <linux/pmu.h>
33 #include <linux/notifier.h>
34 #include <linux/wait.h>
35 #include <linux/init.h>
36 #include <linux/delay.h>
37 #include <linux/completion.h>
38 #include <asm/uaccess.h>
41 #include <asm/hydra.h>
44 EXPORT_SYMBOL(adb_controller);
45 EXPORT_SYMBOL(adb_client_list);
47 extern struct adb_driver via_macii_driver;
48 extern struct adb_driver via_maciisi_driver;
49 extern struct adb_driver via_cuda_driver;
50 extern struct adb_driver adb_iop_driver;
51 extern struct adb_driver via_pmu_driver;
52 extern struct adb_driver macio_adb_driver;
54 static struct adb_driver *adb_driver_list[] = {
55 #ifdef CONFIG_ADB_MACII
58 #ifdef CONFIG_ADB_MACIISI
61 #ifdef CONFIG_ADB_CUDA
67 #if defined(CONFIG_ADB_PMU) || defined(CONFIG_ADB_PMU68K)
70 #ifdef CONFIG_ADB_MACIO
76 struct adb_driver *adb_controller;
77 struct notifier_block *adb_client_list = NULL;
78 static int adb_got_sleep = 0;
79 static int adb_inited = 0;
80 static pid_t adb_probe_task_pid;
81 static DECLARE_MUTEX(adb_probe_mutex);
82 static struct completion adb_probe_task_comp;
83 static int sleepy_trackpad;
86 #ifdef CONFIG_PMAC_PBOOK
87 static int adb_notify_sleep(struct pmu_sleep_notifier *self, int when);
88 static struct pmu_sleep_notifier adb_sleep_notifier = {
94 static int adb_scan_bus(void);
95 static int do_adb_reset_bus(void);
96 static void adbdev_init(void);
99 static struct adb_handler {
100 void (*handler)(unsigned char *, int, struct pt_regs *, int);
101 int original_address;
106 static void printADBreply(struct adb_request *req)
110 printk("adb reply (%d)", req->reply_len);
111 for(i = 0; i < req->reply_len; i++)
112 printk(" %x", req->reply[i]);
119 static __inline__ void adb_wait_ms(unsigned int ms)
121 if (current->pid && adb_probe_task_pid &&
122 adb_probe_task_pid == current->pid) {
123 current->state = TASK_UNINTERRUPTIBLE;
124 schedule_timeout(1 + ms * HZ / 1000);
129 static int adb_scan_bus(void)
131 int i, highFree=0, noMovement;
133 struct adb_request req;
135 /* assumes adb_handler[] is all zeroes at this point */
136 for (i = 1; i < 16; i++) {
137 /* see if there is anything at address i */
138 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
140 if (req.reply_len > 1)
141 /* one or more devices at this address */
142 adb_handler[i].original_address = i;
143 else if (i > highFree)
147 /* Note we reset noMovement to 0 each time we move a device */
148 for (noMovement = 1; noMovement < 2 && highFree > 0; noMovement++) {
149 for (i = 1; i < 16; i++) {
150 if (adb_handler[i].original_address == 0)
153 * Send a "talk register 3" command to address i
154 * to provoke a collision if there is more than
155 * one device at this address.
157 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
160 * Move the device(s) which didn't detect a
161 * collision to address `highFree'. Hopefully
162 * this only moves one device.
164 adb_request(&req, NULL, ADBREQ_SYNC, 3,
165 (i<< 4) | 0xb, (highFree | 0x60), 0xfe);
167 * See if anybody actually moved. This is suggested
170 * http://developer.apple.com/technotes/hw/hw_01.html
172 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
173 (highFree << 4) | 0xf);
174 if (req.reply_len <= 1) continue;
176 * Test whether there are any device(s) left
179 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
181 if (req.reply_len > 1) {
183 * There are still one or more devices
184 * left at address i. Register the one(s)
185 * we moved to `highFree', and find a new
186 * value for highFree.
188 adb_handler[highFree].original_address =
189 adb_handler[i].original_address;
190 while (highFree > 0 &&
191 adb_handler[highFree].original_address)
200 * No devices left at address i; move the
201 * one(s) we moved to `highFree' back to i.
203 adb_request(&req, NULL, ADBREQ_SYNC, 3,
204 (highFree << 4) | 0xb,
210 /* Now fill in the handler_id field of the adb_handler entries. */
211 printk(KERN_DEBUG "adb devices:");
212 for (i = 1; i < 16; i++) {
213 if (adb_handler[i].original_address == 0)
215 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
217 adb_handler[i].handler_id = req.reply[2];
218 printk(" [%d]: %d %x", i, adb_handler[i].original_address,
219 adb_handler[i].handler_id);
227 * This kernel task handles ADB probing. It dies once probing is
231 adb_probe_task(void *x)
233 strcpy(current->comm, "kadbprobe");
235 spin_lock_irq(¤t->sigmask_lock);
236 sigfillset(¤t->blocked);
237 flush_signals(current);
238 spin_unlock_irq(¤t->sigmask_lock);
240 printk(KERN_INFO "adb: starting probe task...\n");
242 printk(KERN_INFO "adb: finished probe task...\n");
244 adb_probe_task_pid = 0;
245 up(&adb_probe_mutex);
251 __adb_probe_task(void *data)
253 adb_probe_task_pid = kernel_thread(adb_probe_task, NULL,
254 SIGCHLD | CLONE_FS | CLONE_FILES | CLONE_SIGHAND);
255 if (adb_probe_task_pid < 0) {
256 adb_probe_task_pid = 0;
257 printk(KERN_ERR "adb: failed to create probe task !\n");
264 static struct tq_struct tqs = {
265 routine: __adb_probe_task,
268 if (__adb_probe_sync) {
273 down(&adb_probe_mutex);
275 /* Create probe thread as a child of keventd */
276 if (current_is_keventd())
277 __adb_probe_task(NULL);
283 int __init adb_init(void)
285 struct adb_driver *driver;
289 if ( (_machine != _MACH_chrp) && (_machine != _MACH_Pmac) )
297 /* xmon may do early-init */
302 adb_controller = NULL;
305 while ((driver = adb_driver_list[i++]) != NULL) {
306 if (!driver->probe()) {
307 adb_controller = driver;
311 if ((adb_controller == NULL) || adb_controller->init()) {
312 printk(KERN_WARNING "Warning: no ADB interface detected\n");
313 adb_controller = NULL;
315 #ifdef CONFIG_PMAC_PBOOK
316 pmu_register_sleep_notifier(&adb_sleep_notifier);
317 #endif /* CONFIG_PMAC_PBOOK */
319 if (machine_is_compatible("AAPL,PowerBook1998") ||
320 machine_is_compatible("PowerBook1,1"))
322 #endif /* CONFIG_PPC */
323 init_completion(&adb_probe_task_comp);
330 __initcall(adb_init);
332 #ifdef CONFIG_PMAC_PBOOK
334 * notify clients before sleep and reset bus afterwards
337 adb_notify_sleep(struct pmu_sleep_notifier *self, int when)
342 case PBOOK_SLEEP_REQUEST:
344 /* We need to get a lock on the probe thread */
345 down(&adb_probe_mutex);
347 if (adb_controller->autopoll)
348 adb_controller->autopoll(0);
349 ret = notifier_call_chain(&adb_client_list, ADB_MSG_POWERDOWN, NULL);
350 if (ret & NOTIFY_STOP_MASK) {
351 up(&adb_probe_mutex);
352 return PBOOK_SLEEP_REFUSE;
355 case PBOOK_SLEEP_REJECT:
358 up(&adb_probe_mutex);
363 case PBOOK_SLEEP_NOW:
367 up(&adb_probe_mutex);
371 return PBOOK_SLEEP_OK;
373 #endif /* CONFIG_PMAC_PBOOK */
376 do_adb_reset_bus(void)
381 if (adb_controller == NULL)
384 if (adb_controller->autopoll)
385 adb_controller->autopoll(0);
387 nret = notifier_call_chain(&adb_client_list, ADB_MSG_PRE_RESET, NULL);
388 if (nret & NOTIFY_STOP_MASK) {
389 if (adb_controller->autopoll)
390 adb_controller->autopoll(devs);
394 if (sleepy_trackpad) {
395 /* Let the trackpad settle down */
401 memset(adb_handler, 0, sizeof(adb_handler));
402 restore_flags(flags);
404 /* That one is still a bit synchronous, oh well... */
405 if (adb_controller->reset_bus)
406 ret = adb_controller->reset_bus();
410 if (sleepy_trackpad) {
411 /* Let the trackpad settle down */
416 devs = adb_scan_bus();
417 if (adb_controller->autopoll)
418 adb_controller->autopoll(devs);
421 nret = notifier_call_chain(&adb_client_list, ADB_MSG_POST_RESET, NULL);
422 if (nret & NOTIFY_STOP_MASK)
431 if ((adb_controller == NULL)||(adb_controller->poll == NULL))
433 adb_controller->poll();
437 adb_probe_wakeup(struct adb_request *req)
439 complete(&adb_probe_task_comp);
442 static struct adb_request adb_sreq;
443 static int adb_sreq_lock; // Use semaphore ! */
446 adb_request(struct adb_request *req, void (*done)(struct adb_request *),
447 int flags, int nbytes, ...)
453 if ((adb_controller == NULL) || (adb_controller->send_request == NULL))
457 if (req == NULL && (flags & ADBREQ_NOSEND))
461 if (test_and_set_bit(0,&adb_sreq_lock)) {
462 printk("adb.c: Warning: contention on static request !\n");
466 flags |= ADBREQ_SYNC;
470 req->nbytes = nbytes+1;
472 req->reply_expected = flags & ADBREQ_REPLY;
473 req->data[0] = ADB_PACKET;
474 va_start(list, nbytes);
475 for (i = 0; i < nbytes; ++i)
476 req->data[i+1] = va_arg(list, int);
479 if (flags & ADBREQ_NOSEND)
482 /* Synchronous requests send from the probe thread cause it to
483 * block. Beware that the "done" callback will be overriden !
485 if ((flags & ADBREQ_SYNC) &&
486 (current->pid && adb_probe_task_pid &&
487 adb_probe_task_pid == current->pid)) {
488 req->done = adb_probe_wakeup;
489 rc = adb_controller->send_request(req, 0);
490 if (rc || req->complete)
492 wait_for_completion(&adb_probe_task_comp);
497 rc = adb_controller->send_request(req, flags & ADBREQ_SYNC);
500 clear_bit(0, &adb_sreq_lock);
505 /* Ultimately this should return the number of devices with
506 the given default id.
507 And it does it now ! Note: changed behaviour: This function
508 will now register if default_id _and_ handler_id both match
509 but handler_id can be left to 0 to match with default_id only.
510 When handler_id is set, this function will try to adjust
511 the handler_id id it doesn't match. */
513 adb_register(int default_id, int handler_id, struct adb_ids *ids,
514 void (*handler)(unsigned char *, int, struct pt_regs *, int))
519 for (i = 1; i < 16; i++) {
520 if ((adb_handler[i].original_address == default_id) &&
521 (!handler_id || (handler_id == adb_handler[i].handler_id) ||
522 adb_try_handler_change(i, handler_id))) {
523 if (adb_handler[i].handler != 0) {
525 "Two handlers for ADB device %d\n",
529 adb_handler[i].handler = handler;
530 ids->id[ids->nids++] = i;
537 adb_unregister(int index)
539 if (!adb_handler[index].handler)
541 adb_handler[index].handler = 0;
546 adb_input(unsigned char *buf, int nb, struct pt_regs *regs, int autopoll)
549 static int dump_adb_input = 0;
551 /* We skip keystrokes and mouse moves when the sleep process
552 * has been started. We stop autopoll, but this is another security
558 if (dump_adb_input) {
559 printk(KERN_INFO "adb packet: ");
560 for (i = 0; i < nb; ++i)
561 printk(" %x", buf[i]);
562 printk(", id = %d\n", id);
564 if (adb_handler[id].handler != 0) {
565 (*adb_handler[id].handler)(buf, nb, regs, autopoll);
569 /* Try to change handler to new_id. Will return 1 if successful */
571 adb_try_handler_change(int address, int new_id)
573 struct adb_request req;
575 if (adb_handler[address].handler_id == new_id)
577 adb_request(&req, NULL, ADBREQ_SYNC, 3,
578 ADB_WRITEREG(address, 3), address | 0x20, new_id);
579 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
580 ADB_READREG(address, 3));
581 if (req.reply_len < 2)
583 if (req.reply[2] != new_id)
585 adb_handler[address].handler_id = req.reply[2];
591 adb_get_infos(int address, int *original_address, int *handler_id)
593 *original_address = adb_handler[address].original_address;
594 *handler_id = adb_handler[address].handler_id;
596 return (*original_address != 0);
600 * /dev/adb device driver.
603 #define ADB_MAJOR 56 /* major number for /dev/adb */
605 struct adbdev_state {
608 struct adb_request *completed;
609 wait_queue_head_t wait_queue;
613 static void adb_write_done(struct adb_request *req)
615 struct adbdev_state *state = (struct adbdev_state *) req->arg;
618 if (!req->complete) {
622 spin_lock_irqsave(&state->lock, flags);
623 atomic_dec(&state->n_pending);
626 if (atomic_read(&state->n_pending) == 0) {
627 spin_unlock_irqrestore(&state->lock, flags);
632 struct adb_request **ap = &state->completed;
637 wake_up_interruptible(&state->wait_queue);
639 spin_unlock_irqrestore(&state->lock, flags);
643 do_adb_query(struct adb_request *req)
649 case ADB_QUERY_GETDEVINFO:
652 req->reply[0] = adb_handler[req->data[2]].original_address;
653 req->reply[1] = adb_handler[req->data[2]].handler_id;
663 static int adb_open(struct inode *inode, struct file *file)
665 struct adbdev_state *state;
667 if (MINOR(inode->i_rdev) > 0 || adb_controller == NULL)
669 state = kmalloc(sizeof(struct adbdev_state), GFP_KERNEL);
672 file->private_data = state;
673 spin_lock_init(&state->lock);
674 atomic_set(&state->n_pending, 0);
675 state->completed = NULL;
676 init_waitqueue_head(&state->wait_queue);
682 /* FIXME: Should wait completion, dequeue & delete pending requests */
683 static int adb_release(struct inode *inode, struct file *file)
685 struct adbdev_state *state = file->private_data;
690 file->private_data = NULL;
691 spin_lock_irqsave(&state->lock, flags);
692 if (atomic_read(&state->n_pending) == 0
693 && state->completed == NULL) {
694 spin_unlock_irqrestore(&state->lock, flags);
698 spin_unlock_irqrestore(&state->lock, flags);
705 static ssize_t adb_read(struct file *file, char *buf,
706 size_t count, loff_t *ppos)
709 struct adbdev_state *state = file->private_data;
710 struct adb_request *req;
711 wait_queue_t wait = __WAITQUEUE_INITIALIZER(wait,current);
716 if (count > sizeof(req->reply))
717 count = sizeof(req->reply);
718 ret = verify_area(VERIFY_WRITE, buf, count);
723 spin_lock_irqsave(&state->lock, flags);
724 add_wait_queue(&state->wait_queue, &wait);
725 current->state = TASK_INTERRUPTIBLE;
728 req = state->completed;
730 state->completed = req->next;
731 else if (atomic_read(&state->n_pending) == 0)
733 if (req != NULL || ret != 0)
736 if (file->f_flags & O_NONBLOCK) {
740 if (signal_pending(current)) {
744 spin_unlock_irqrestore(&state->lock, flags);
746 spin_lock_irqsave(&state->lock, flags);
749 current->state = TASK_RUNNING;
750 remove_wait_queue(&state->wait_queue, &wait);
751 spin_unlock_irqrestore(&state->lock, flags);
756 ret = req->reply_len;
759 if (ret > 0 && copy_to_user(buf, req->reply, ret))
766 static ssize_t adb_write(struct file *file, const char *buf,
767 size_t count, loff_t *ppos)
770 struct adbdev_state *state = file->private_data;
771 struct adb_request *req;
773 if (count < 2 || count > sizeof(req->data))
775 if (adb_controller == NULL)
777 ret = verify_area(VERIFY_READ, buf, count);
781 req = (struct adb_request *) kmalloc(sizeof(struct adb_request),
787 req->done = adb_write_done;
788 req->arg = (void *) state;
792 if (copy_from_user(req->data, buf, count))
795 atomic_inc(&state->n_pending);
797 /* If a probe is in progress or we are sleeping, wait for it to complete */
798 down(&adb_probe_mutex);
800 /* Queries are special requests sent to the ADB driver itself */
801 if (req->data[0] == ADB_QUERY) {
803 ret = do_adb_query(req);
806 up(&adb_probe_mutex);
808 /* Special case for ADB_BUSRESET request, all others are sent to
810 else if ((req->data[0] == ADB_PACKET)&&(count > 1)
811 &&(req->data[1] == ADB_BUSRESET)) {
812 ret = do_adb_reset_bus();
813 up(&adb_probe_mutex);
814 atomic_dec(&state->n_pending);
819 req->reply_expected = ((req->data[1] & 0xc) == 0xc);
820 if (adb_controller && adb_controller->send_request)
821 ret = adb_controller->send_request(req, 0);
824 up(&adb_probe_mutex);
828 atomic_dec(&state->n_pending);
838 static struct file_operations adb_fops = {
843 release: adb_release,
849 if (devfs_register_chrdev(ADB_MAJOR, "adb", &adb_fops))
850 printk(KERN_ERR "adb: unable to get major %d\n", ADB_MAJOR);
852 devfs_register (NULL, "adb", DEVFS_FL_DEFAULT,
854 S_IFCHR | S_IRUSR | S_IWUSR,