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/types.h>
20 #include <linux/errno.h>
21 #include <linux/kernel.h>
22 #include <linux/slab.h>
23 #include <linux/module.h>
26 #include <linux/sched/signal.h>
27 #include <linux/adb.h>
28 #include <linux/cuda.h>
29 #include <linux/pmu.h>
30 #include <linux/notifier.h>
31 #include <linux/wait.h>
32 #include <linux/init.h>
33 #include <linux/delay.h>
34 #include <linux/spinlock.h>
35 #include <linux/completion.h>
36 #include <linux/device.h>
37 #include <linux/kthread.h>
38 #include <linux/platform_device.h>
39 #include <linux/mutex.h>
41 #include <linux/uaccess.h>
44 #include <asm/machdep.h>
48 EXPORT_SYMBOL(adb_client_list);
50 extern struct adb_driver via_macii_driver;
51 extern struct adb_driver via_cuda_driver;
52 extern struct adb_driver adb_iop_driver;
53 extern struct adb_driver via_pmu_driver;
54 extern struct adb_driver macio_adb_driver;
56 static DEFINE_MUTEX(adb_mutex);
57 static struct adb_driver *adb_driver_list[] = {
58 #ifdef CONFIG_ADB_MACII
61 #ifdef CONFIG_ADB_CUDA
67 #if defined(CONFIG_ADB_PMU) || defined(CONFIG_ADB_PMU68K)
70 #ifdef CONFIG_ADB_MACIO
76 static struct class *adb_dev_class;
78 static struct adb_driver *adb_controller;
79 BLOCKING_NOTIFIER_HEAD(adb_client_list);
80 static int adb_got_sleep;
81 static int adb_inited;
82 static DEFINE_SEMAPHORE(adb_probe_mutex);
83 static int sleepy_trackpad;
84 static int autopoll_devs;
87 static int adb_scan_bus(void);
88 static int do_adb_reset_bus(void);
89 static void adbdev_init(void);
90 static int try_handler_change(int, int);
92 static struct adb_handler {
93 void (*handler)(unsigned char *, int, int);
100 * The adb_handler_mutex mutex protects all accesses to the original_address
101 * and handler_id fields of adb_handler[i] for all i, and changes to the
103 * Accesses to the handler field are protected by the adb_handler_lock
104 * rwlock. It is held across all calls to any handler, so that by the
105 * time adb_unregister returns, we know that the old handler isn't being
108 static DEFINE_MUTEX(adb_handler_mutex);
109 static DEFINE_RWLOCK(adb_handler_lock);
112 static void printADBreply(struct adb_request *req)
116 printk("adb reply (%d)", req->reply_len);
117 for(i = 0; i < req->reply_len; i++)
118 printk(" %x", req->reply[i]);
124 static int adb_scan_bus(void)
126 int i, highFree=0, noMovement;
128 struct adb_request req;
130 /* assumes adb_handler[] is all zeroes at this point */
131 for (i = 1; i < 16; i++) {
132 /* see if there is anything at address i */
133 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
135 if (req.reply_len > 1)
136 /* one or more devices at this address */
137 adb_handler[i].original_address = i;
138 else if (i > highFree)
142 /* Note we reset noMovement to 0 each time we move a device */
143 for (noMovement = 1; noMovement < 2 && highFree > 0; noMovement++) {
144 for (i = 1; i < 16; i++) {
145 if (adb_handler[i].original_address == 0)
148 * Send a "talk register 3" command to address i
149 * to provoke a collision if there is more than
150 * one device at this address.
152 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
155 * Move the device(s) which didn't detect a
156 * collision to address `highFree'. Hopefully
157 * this only moves one device.
159 adb_request(&req, NULL, ADBREQ_SYNC, 3,
160 (i<< 4) | 0xb, (highFree | 0x60), 0xfe);
162 * See if anybody actually moved. This is suggested
165 * http://developer.apple.com/technotes/hw/hw_01.html
167 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
168 (highFree << 4) | 0xf);
169 if (req.reply_len <= 1) continue;
171 * Test whether there are any device(s) left
174 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
176 if (req.reply_len > 1) {
178 * There are still one or more devices
179 * left at address i. Register the one(s)
180 * we moved to `highFree', and find a new
181 * value for highFree.
183 adb_handler[highFree].original_address =
184 adb_handler[i].original_address;
185 while (highFree > 0 &&
186 adb_handler[highFree].original_address)
194 * No devices left at address i; move the
195 * one(s) we moved to `highFree' back to i.
197 adb_request(&req, NULL, ADBREQ_SYNC, 3,
198 (highFree << 4) | 0xb,
204 /* Now fill in the handler_id field of the adb_handler entries. */
205 printk(KERN_DEBUG "adb devices:");
206 for (i = 1; i < 16; i++) {
207 if (adb_handler[i].original_address == 0)
209 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
211 adb_handler[i].handler_id = req.reply[2];
212 printk(" [%d]: %d %x", i, adb_handler[i].original_address,
213 adb_handler[i].handler_id);
221 * This kernel task handles ADB probing. It dies once probing is
225 adb_probe_task(void *x)
227 printk(KERN_INFO "adb: starting probe task...\n");
229 printk(KERN_INFO "adb: finished probe task...\n");
231 up(&adb_probe_mutex);
237 __adb_probe_task(struct work_struct *bullshit)
239 kthread_run(adb_probe_task, NULL, "kadbprobe");
242 static DECLARE_WORK(adb_reset_work, __adb_probe_task);
247 if (__adb_probe_sync) {
252 down(&adb_probe_mutex);
253 schedule_work(&adb_reset_work);
259 * notify clients before sleep
261 static int __adb_suspend(struct platform_device *dev, pm_message_t state)
264 /* We need to get a lock on the probe thread */
265 down(&adb_probe_mutex);
267 if (adb_controller->autopoll)
268 adb_controller->autopoll(0);
269 blocking_notifier_call_chain(&adb_client_list, ADB_MSG_POWERDOWN, NULL);
274 static int adb_suspend(struct device *dev)
276 return __adb_suspend(to_platform_device(dev), PMSG_SUSPEND);
279 static int adb_freeze(struct device *dev)
281 return __adb_suspend(to_platform_device(dev), PMSG_FREEZE);
284 static int adb_poweroff(struct device *dev)
286 return __adb_suspend(to_platform_device(dev), PMSG_HIBERNATE);
290 * reset bus after sleep
292 static int __adb_resume(struct platform_device *dev)
295 up(&adb_probe_mutex);
301 static int adb_resume(struct device *dev)
303 return __adb_resume(to_platform_device(dev));
305 #endif /* CONFIG_PM */
307 static int __init adb_init(void)
309 struct adb_driver *driver;
313 if (!machine_is(chrp) && !machine_is(powermac))
321 /* xmon may do early-init */
326 adb_controller = NULL;
329 while ((driver = adb_driver_list[i++]) != NULL) {
330 if (!driver->probe()) {
331 adb_controller = driver;
335 if (adb_controller != NULL && adb_controller->init &&
336 adb_controller->init())
337 adb_controller = NULL;
338 if (adb_controller == NULL) {
339 printk(KERN_WARNING "Warning: no ADB interface detected\n");
342 if (of_machine_is_compatible("AAPL,PowerBook1998") ||
343 of_machine_is_compatible("PowerBook1,1"))
345 #endif /* CONFIG_PPC */
353 device_initcall(adb_init);
356 do_adb_reset_bus(void)
360 if (adb_controller == NULL)
363 if (adb_controller->autopoll)
364 adb_controller->autopoll(0);
366 blocking_notifier_call_chain(&adb_client_list,
367 ADB_MSG_PRE_RESET, NULL);
369 if (sleepy_trackpad) {
370 /* Let the trackpad settle down */
374 mutex_lock(&adb_handler_mutex);
375 write_lock_irq(&adb_handler_lock);
376 memset(adb_handler, 0, sizeof(adb_handler));
377 write_unlock_irq(&adb_handler_lock);
379 /* That one is still a bit synchronous, oh well... */
380 if (adb_controller->reset_bus)
381 ret = adb_controller->reset_bus();
385 if (sleepy_trackpad) {
386 /* Let the trackpad settle down */
391 autopoll_devs = adb_scan_bus();
392 if (adb_controller->autopoll)
393 adb_controller->autopoll(autopoll_devs);
395 mutex_unlock(&adb_handler_mutex);
397 blocking_notifier_call_chain(&adb_client_list,
398 ADB_MSG_POST_RESET, NULL);
406 if ((adb_controller == NULL)||(adb_controller->poll == NULL))
408 adb_controller->poll();
410 EXPORT_SYMBOL(adb_poll);
412 static void adb_sync_req_done(struct adb_request *req)
414 struct completion *comp = req->arg;
420 adb_request(struct adb_request *req, void (*done)(struct adb_request *),
421 int flags, int nbytes, ...)
426 struct completion comp;
428 if ((adb_controller == NULL) || (adb_controller->send_request == NULL))
433 req->nbytes = nbytes+1;
435 req->reply_expected = flags & ADBREQ_REPLY;
436 req->data[0] = ADB_PACKET;
437 va_start(list, nbytes);
438 for (i = 0; i < nbytes; ++i)
439 req->data[i+1] = va_arg(list, int);
442 if (flags & ADBREQ_NOSEND)
445 /* Synchronous requests block using an on-stack completion */
446 if (flags & ADBREQ_SYNC) {
448 req->done = adb_sync_req_done;
450 init_completion(&comp);
453 rc = adb_controller->send_request(req, 0);
455 if ((flags & ADBREQ_SYNC) && !rc && !req->complete)
456 wait_for_completion(&comp);
460 EXPORT_SYMBOL(adb_request);
462 /* Ultimately this should return the number of devices with
463 the given default id.
464 And it does it now ! Note: changed behaviour: This function
465 will now register if default_id _and_ handler_id both match
466 but handler_id can be left to 0 to match with default_id only.
467 When handler_id is set, this function will try to adjust
468 the handler_id id it doesn't match. */
470 adb_register(int default_id, int handler_id, struct adb_ids *ids,
471 void (*handler)(unsigned char *, int, int))
475 mutex_lock(&adb_handler_mutex);
477 for (i = 1; i < 16; i++) {
478 if ((adb_handler[i].original_address == default_id) &&
479 (!handler_id || (handler_id == adb_handler[i].handler_id) ||
480 try_handler_change(i, handler_id))) {
481 if (adb_handler[i].handler != 0) {
483 "Two handlers for ADB device %d\n",
487 write_lock_irq(&adb_handler_lock);
488 adb_handler[i].handler = handler;
489 write_unlock_irq(&adb_handler_lock);
490 ids->id[ids->nids++] = i;
493 mutex_unlock(&adb_handler_mutex);
496 EXPORT_SYMBOL(adb_register);
499 adb_unregister(int index)
503 mutex_lock(&adb_handler_mutex);
504 write_lock_irq(&adb_handler_lock);
505 if (adb_handler[index].handler) {
506 while(adb_handler[index].busy) {
507 write_unlock_irq(&adb_handler_lock);
509 write_lock_irq(&adb_handler_lock);
512 adb_handler[index].handler = NULL;
514 write_unlock_irq(&adb_handler_lock);
515 mutex_unlock(&adb_handler_mutex);
518 EXPORT_SYMBOL(adb_unregister);
521 adb_input(unsigned char *buf, int nb, int autopoll)
524 static int dump_adb_input;
527 void (*handler)(unsigned char *, int, int);
529 /* We skip keystrokes and mouse moves when the sleep process
530 * has been started. We stop autopoll, but this is another security
536 if (dump_adb_input) {
537 printk(KERN_INFO "adb packet: ");
538 for (i = 0; i < nb; ++i)
539 printk(" %x", buf[i]);
540 printk(", id = %d\n", id);
542 write_lock_irqsave(&adb_handler_lock, flags);
543 handler = adb_handler[id].handler;
545 adb_handler[id].busy = 1;
546 write_unlock_irqrestore(&adb_handler_lock, flags);
547 if (handler != NULL) {
548 (*handler)(buf, nb, autopoll);
550 adb_handler[id].busy = 0;
555 /* Try to change handler to new_id. Will return 1 if successful. */
556 static int try_handler_change(int address, int new_id)
558 struct adb_request req;
560 if (adb_handler[address].handler_id == new_id)
562 adb_request(&req, NULL, ADBREQ_SYNC, 3,
563 ADB_WRITEREG(address, 3), address | 0x20, new_id);
564 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
565 ADB_READREG(address, 3));
566 if (req.reply_len < 2)
568 if (req.reply[2] != new_id)
570 adb_handler[address].handler_id = req.reply[2];
576 adb_try_handler_change(int address, int new_id)
580 mutex_lock(&adb_handler_mutex);
581 ret = try_handler_change(address, new_id);
582 mutex_unlock(&adb_handler_mutex);
585 EXPORT_SYMBOL(adb_try_handler_change);
588 adb_get_infos(int address, int *original_address, int *handler_id)
590 mutex_lock(&adb_handler_mutex);
591 *original_address = adb_handler[address].original_address;
592 *handler_id = adb_handler[address].handler_id;
593 mutex_unlock(&adb_handler_mutex);
595 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)
647 switch(req->data[1]) {
648 case ADB_QUERY_GETDEVINFO:
651 mutex_lock(&adb_handler_mutex);
652 req->reply[0] = adb_handler[req->data[2]].original_address;
653 req->reply[1] = adb_handler[req->data[2]].handler_id;
654 mutex_unlock(&adb_handler_mutex);
664 static int adb_open(struct inode *inode, struct file *file)
666 struct adbdev_state *state;
669 mutex_lock(&adb_mutex);
670 if (iminor(inode) > 0 || adb_controller == NULL) {
674 state = kmalloc(sizeof(struct adbdev_state), GFP_KERNEL);
679 file->private_data = state;
680 spin_lock_init(&state->lock);
681 atomic_set(&state->n_pending, 0);
682 state->completed = NULL;
683 init_waitqueue_head(&state->wait_queue);
687 mutex_unlock(&adb_mutex);
691 static int adb_release(struct inode *inode, struct file *file)
693 struct adbdev_state *state = file->private_data;
696 mutex_lock(&adb_mutex);
698 file->private_data = NULL;
699 spin_lock_irqsave(&state->lock, flags);
700 if (atomic_read(&state->n_pending) == 0
701 && state->completed == NULL) {
702 spin_unlock_irqrestore(&state->lock, flags);
706 spin_unlock_irqrestore(&state->lock, flags);
709 mutex_unlock(&adb_mutex);
713 static ssize_t adb_read(struct file *file, char __user *buf,
714 size_t count, loff_t *ppos)
717 struct adbdev_state *state = file->private_data;
718 struct adb_request *req;
719 DECLARE_WAITQUEUE(wait, current);
724 if (count > sizeof(req->reply))
725 count = sizeof(req->reply);
726 if (!access_ok(VERIFY_WRITE, buf, count))
730 spin_lock_irqsave(&state->lock, flags);
731 add_wait_queue(&state->wait_queue, &wait);
732 set_current_state(TASK_INTERRUPTIBLE);
735 req = state->completed;
737 state->completed = req->next;
738 else if (atomic_read(&state->n_pending) == 0)
740 if (req != NULL || ret != 0)
743 if (file->f_flags & O_NONBLOCK) {
747 if (signal_pending(current)) {
751 spin_unlock_irqrestore(&state->lock, flags);
753 spin_lock_irqsave(&state->lock, flags);
756 set_current_state(TASK_RUNNING);
757 remove_wait_queue(&state->wait_queue, &wait);
758 spin_unlock_irqrestore(&state->lock, flags);
763 ret = req->reply_len;
766 if (ret > 0 && copy_to_user(buf, req->reply, ret))
773 static ssize_t adb_write(struct file *file, const char __user *buf,
774 size_t count, loff_t *ppos)
777 struct adbdev_state *state = file->private_data;
778 struct adb_request *req;
780 if (count < 2 || count > sizeof(req->data))
782 if (adb_controller == NULL)
784 if (!access_ok(VERIFY_READ, buf, count))
787 req = kmalloc(sizeof(struct adb_request),
793 req->done = adb_write_done;
794 req->arg = (void *) state;
798 if (copy_from_user(req->data, buf, count))
801 atomic_inc(&state->n_pending);
803 /* If a probe is in progress or we are sleeping, wait for it to complete */
804 down(&adb_probe_mutex);
806 /* Queries are special requests sent to the ADB driver itself */
807 if (req->data[0] == ADB_QUERY) {
809 ret = do_adb_query(req);
812 up(&adb_probe_mutex);
814 /* Special case for ADB_BUSRESET request, all others are sent to
816 else if ((req->data[0] == ADB_PACKET) && (count > 1)
817 && (req->data[1] == ADB_BUSRESET)) {
818 ret = do_adb_reset_bus();
819 up(&adb_probe_mutex);
820 atomic_dec(&state->n_pending);
825 req->reply_expected = ((req->data[1] & 0xc) == 0xc);
826 if (adb_controller && adb_controller->send_request)
827 ret = adb_controller->send_request(req, 0);
830 up(&adb_probe_mutex);
834 atomic_dec(&state->n_pending);
844 static const struct file_operations adb_fops = {
845 .owner = THIS_MODULE,
850 .release = adb_release,
854 static const struct dev_pm_ops adb_dev_pm_ops = {
855 .suspend = adb_suspend,
856 .resume = adb_resume,
857 /* Hibernate hooks */
858 .freeze = adb_freeze,
860 .poweroff = adb_poweroff,
861 .restore = adb_resume,
865 static struct platform_driver adb_pfdrv = {
869 .pm = &adb_dev_pm_ops,
874 static struct platform_device adb_pfdev = {
879 adb_dummy_probe(struct platform_device *dev)
881 if (dev == &adb_pfdev)
889 if (register_chrdev(ADB_MAJOR, "adb", &adb_fops)) {
890 printk(KERN_ERR "adb: unable to get major %d\n", ADB_MAJOR);
894 adb_dev_class = class_create(THIS_MODULE, "adb");
895 if (IS_ERR(adb_dev_class))
897 device_create(adb_dev_class, NULL, MKDEV(ADB_MAJOR, 0), NULL, "adb");
899 platform_device_register(&adb_pfdev);
900 platform_driver_probe(&adb_pfdrv, adb_dummy_probe);