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.h>
27 #include <linux/adb.h>
28 #include <linux/cuda.h>
29 #include <linux/pmu.h>
31 #include <linux/wait.h>
36 #include <linux/device.h>
41 #include <asm/uaccess.h>
44 #include <asm/machdep.h>
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;
84 static int adb_got_sleep;
85 static int adb_inited;
87 static int sleepy_trackpad;
88 static int autopoll_devs;
91 static int adb_scan_bus(
void);
92 static int do_adb_reset_bus(
void);
93 static void adbdev_init(
void);
94 static int try_handler_change(
int,
int);
96 static struct adb_handler {
128 static int adb_scan_bus(
void)
130 int i, highFree=0, noMovement;
135 for (i = 1; i < 16; i++) {
141 adb_handler[
i].original_address =
i;
142 else if (i > highFree)
147 for (noMovement = 1; noMovement < 2 && highFree > 0; noMovement++) {
148 for (i = 1; i < 16; i++) {
149 if (adb_handler[i].original_address == 0)
164 (i<< 4) | 0
xb, (highFree | 0x60), 0xfe);
172 (highFree << 4) | 0xf);
187 adb_handler[highFree].original_address =
188 adb_handler[
i].original_address;
189 while (highFree > 0 &&
190 adb_handler[highFree].original_address)
203 (highFree << 4) | 0
xb,
211 for (i = 1; i < 16; i++) {
212 if (adb_handler[i].original_address == 0)
216 adb_handler[
i].handler_id = req.
reply[2];
217 printk(
" [%d]: %d %x", i, adb_handler[i].original_address,
218 adb_handler[i].handler_id);
230 adb_probe_task(
void *
x)
236 up(&adb_probe_mutex);
257 down(&adb_probe_mutex);
270 down(&adb_probe_mutex);
285 up(&adb_probe_mutex);
292 static int __init adb_init(
void)
298 if (!machine_is(chrp) && !machine_is(powermac))
311 adb_controller =
NULL;
314 while ((driver = adb_driver_list[i++]) !=
NULL) {
315 if (!driver->
probe()) {
320 if (adb_controller !=
NULL && adb_controller->
init &&
321 adb_controller->
init())
322 adb_controller =
NULL;
323 if (adb_controller ==
NULL) {
341 do_adb_reset_bus(
void)
345 if (adb_controller ==
NULL)
354 if (sleepy_trackpad) {
361 memset(adb_handler, 0,
sizeof(adb_handler));
370 if (sleepy_trackpad) {
376 autopoll_devs = adb_scan_bus();
378 adb_controller->
autopoll(autopoll_devs);
391 if ((adb_controller ==
NULL)||(adb_controller->
poll ==
NULL))
393 adb_controller->
poll();
396 static void adb_sync_req_done(
struct adb_request *req)
432 req->
done = adb_sync_req_done;
434 init_completion(&comp);
439 if ((flags & ADBREQ_SYNC) && !rc && !req->
complete)
454 void (*handler)(
unsigned char *,
int,
int))
460 for (i = 1; i < 16; i++) {
461 if ((adb_handler[i].original_address == default_id) &&
462 (!handler_id || (handler_id == adb_handler[i].handler_id) ||
463 try_handler_change(i, handler_id))) {
464 if (adb_handler[i].
handler != 0) {
466 "Two handlers for ADB device %d\n",
487 if (adb_handler[index].
handler) {
488 while(adb_handler[index].
busy) {
505 static int dump_adb_input = 0;
517 if (dump_adb_input) {
519 for (i = 0; i < nb; ++
i)
521 printk(
", id = %d\n",
id);
526 adb_handler[
id].busy = 1;
529 (*handler)(
buf, nb, autopoll);
531 adb_handler[
id].busy = 0;
537 static int try_handler_change(
int address,
int new_id)
541 if (adb_handler[address].handler_id == new_id)
549 if (req.
reply[2] != new_id)
562 ret = try_handler_change(address, new_id);
571 *original_address = adb_handler[
address].original_address;
572 *handler_id = adb_handler[
address].handler_id;
575 return (*original_address != 0);
593 static void adb_write_done(
struct adb_request *req)
607 spin_unlock_irqrestore(&state->
lock,
flags);
619 spin_unlock_irqrestore(&state->
lock,
flags);
633 req->
reply[0] = adb_handler[req->
data[2]].original_address;
634 req->
reply[1] = adb_handler[req->
data[2]].handler_id;
651 if (iminor(inode) > 0 || adb_controller ==
NULL) {
672 static int adb_release(
struct inode *inode,
struct file *file)
683 spin_unlock_irqrestore(&state->
lock, flags);
687 spin_unlock_irqrestore(&state->
lock, flags);
694 static ssize_t adb_read(
struct file *file,
char __user *
buf,
695 size_t count, loff_t *ppos)
705 if (count >
sizeof(req->
reply))
706 count =
sizeof(req->
reply);
721 if (req !=
NULL || ret != 0)
732 spin_unlock_irqrestore(&state->
lock, flags);
739 spin_unlock_irqrestore(&state->
lock, flags);
754 static ssize_t adb_write(
struct file *file,
const char __user *buf,
755 size_t count, loff_t *ppos)
761 if (count < 2 || count >
sizeof(req->
data))
763 if (adb_controller ==
NULL)
774 req->
done = adb_write_done;
775 req->
arg = (
void *) state;
785 down(&adb_probe_mutex);
790 ret = do_adb_query(req);
793 up(&adb_probe_mutex);
799 ret = do_adb_reset_bus();
800 up(&adb_probe_mutex);
811 up(&adb_probe_mutex);
831 .release = adb_release,
839 .suspend = adb_suspend,
840 .resume = adb_resume,
851 if (dev == &adb_pfdev)
859 if (register_chrdev(
ADB_MAJOR,
"adb", &adb_fops)) {
865 if (IS_ERR(adb_dev_class))