Merge master.kernel.org:/home/rmk/linux-2.6-serial
[pandora-kernel.git] / drivers / macintosh / adb.c
1 /*
2  * Device driver for the Apple Desktop Bus
3  * and the /dev/adb device on macintoshes.
4  *
5  * Copyright (C) 1996 Paul Mackerras.
6  *
7  * Modified to declare controllers as structures, added
8  * client notification of bus reset and handles PowerBook
9  * sleep, by Benjamin Herrenschmidt.
10  *
11  * To do:
12  *
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
17  */
18
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>
25 #include <linux/fs.h>
26 #include <linux/mm.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>
40
41 #include <asm/uaccess.h>
42 #include <asm/semaphore.h>
43 #ifdef CONFIG_PPC
44 #include <asm/prom.h>
45 #endif
46
47
48 EXPORT_SYMBOL(adb_controller);
49 EXPORT_SYMBOL(adb_client_list);
50
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;
57
58 static struct adb_driver *adb_driver_list[] = {
59 #ifdef CONFIG_ADB_MACII
60         &via_macii_driver,
61 #endif
62 #ifdef CONFIG_ADB_MACIISI
63         &via_maciisi_driver,
64 #endif
65 #ifdef CONFIG_ADB_CUDA
66         &via_cuda_driver,
67 #endif
68 #ifdef CONFIG_ADB_IOP
69         &adb_iop_driver,
70 #endif
71 #if defined(CONFIG_ADB_PMU) || defined(CONFIG_ADB_PMU68K)
72         &via_pmu_driver,
73 #endif
74 #ifdef CONFIG_ADB_MACIO
75         &macio_adb_driver,
76 #endif
77         NULL
78 };
79
80 static struct class *adb_dev_class;
81
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;
91 int __adb_probe_sync;
92
93 #ifdef CONFIG_PM
94 static int adb_notify_sleep(struct pmu_sleep_notifier *self, int when);
95 static struct pmu_sleep_notifier adb_sleep_notifier = {
96         adb_notify_sleep,
97         SLEEP_LEVEL_ADB,
98 };
99 #endif
100
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);
105
106 static struct adb_handler {
107         void (*handler)(unsigned char *, int, struct pt_regs *, int);
108         int original_address;
109         int handler_id;
110         int busy;
111 } adb_handler[16];
112
113 /*
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
116  * handler field.
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
120  * called.
121  */
122 static DECLARE_MUTEX(adb_handler_sem);
123 static DEFINE_RWLOCK(adb_handler_lock);
124
125 #if 0
126 static void printADBreply(struct adb_request *req)
127 {
128         int i;
129
130         printk("adb reply (%d)", req->reply_len);
131         for(i = 0; i < req->reply_len; i++)
132                 printk(" %x", req->reply[i]);
133         printk("\n");
134
135 }
136 #endif
137
138
139 static __inline__ void adb_wait_ms(unsigned int ms)
140 {
141         if (current->pid && adb_probe_task_pid &&
142           adb_probe_task_pid == current->pid)
143                 msleep(ms);
144         else
145                 mdelay(ms);
146 }
147
148 static int adb_scan_bus(void)
149 {
150         int i, highFree=0, noMovement;
151         int devmask = 0;
152         struct adb_request req;
153         
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,
158                             (i << 4) | 0xf);
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)
163                         highFree = i;
164         }
165
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)
170                                 continue;
171                         /*
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.
175                          */
176                         adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
177                                     (i << 4) | 0xf);
178                         /*
179                          * Move the device(s) which didn't detect a
180                          * collision to address `highFree'.  Hopefully
181                          * this only moves one device.
182                          */
183                         adb_request(&req, NULL, ADBREQ_SYNC, 3,
184                                     (i<< 4) | 0xb, (highFree | 0x60), 0xfe);
185                         /*
186                          * See if anybody actually moved. This is suggested
187                          * by HW TechNote 01:
188                          *
189                          * http://developer.apple.com/technotes/hw/hw_01.html
190                          */
191                         adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
192                                     (highFree << 4) | 0xf);
193                         if (req.reply_len <= 1) continue;
194                         /*
195                          * Test whether there are any device(s) left
196                          * at address i.
197                          */
198                         adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
199                                     (i << 4) | 0xf);
200                         if (req.reply_len > 1) {
201                                 /*
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.
206                                  */
207                                 adb_handler[highFree].original_address =
208                                         adb_handler[i].original_address;
209                                 while (highFree > 0 &&
210                                        adb_handler[highFree].original_address)
211                                         highFree--;
212                                 if (highFree <= 0)
213                                         break;
214
215                                 noMovement = 0;
216                         }
217                         else {
218                                 /*
219                                  * No devices left at address i; move the
220                                  * one(s) we moved to `highFree' back to i.
221                                  */
222                                 adb_request(&req, NULL, ADBREQ_SYNC, 3,
223                                             (highFree << 4) | 0xb,
224                                             (i | 0x60), 0xfe);
225                         }
226                 }       
227         }
228
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)
233                         continue;
234                 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
235                             (i << 4) | 0xf);
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);
239                 devmask |= 1 << i;
240         }
241         printk("\n");
242         return devmask;
243 }
244
245 /*
246  * This kernel task handles ADB probing. It dies once probing is
247  * completed.
248  */
249 static int
250 adb_probe_task(void *x)
251 {
252         sigset_t blocked;
253
254         strcpy(current->comm, "kadbprobe");
255
256         sigfillset(&blocked);
257         sigprocmask(SIG_BLOCK, &blocked, NULL);
258         flush_signals(current);
259
260         printk(KERN_INFO "adb: starting probe task...\n");
261         do_adb_reset_bus();
262         printk(KERN_INFO "adb: finished probe task...\n");
263         
264         adb_probe_task_pid = 0;
265         up(&adb_probe_mutex);
266         
267         return 0;
268 }
269
270 static void
271 __adb_probe_task(void *data)
272 {
273         adb_probe_task_pid = kernel_thread(adb_probe_task, NULL, SIGCHLD | CLONE_KERNEL);
274 }
275
276 static DECLARE_WORK(adb_reset_work, __adb_probe_task, NULL);
277
278 int
279 adb_reset_bus(void)
280 {
281         if (__adb_probe_sync) {
282                 do_adb_reset_bus();
283                 return 0;
284         }
285
286         down(&adb_probe_mutex);
287         schedule_work(&adb_reset_work);
288         return 0;
289 }
290
291 int __init adb_init(void)
292 {
293         struct adb_driver *driver;
294         int i;
295
296 #ifdef CONFIG_PPC32
297         if ( (_machine != _MACH_chrp) && (_machine != _MACH_Pmac) )
298                 return 0;
299 #endif
300 #ifdef CONFIG_MAC
301         if (!MACH_IS_MAC)
302                 return 0;
303 #endif
304
305         /* xmon may do early-init */
306         if (adb_inited)
307                 return 0;
308         adb_inited = 1;
309                 
310         adb_controller = NULL;
311
312         i = 0;
313         while ((driver = adb_driver_list[i++]) != NULL) {
314                 if (!driver->probe()) {
315                         adb_controller = driver;
316                         break;
317                 }
318         }
319         if ((adb_controller == NULL) || adb_controller->init()) {
320                 printk(KERN_WARNING "Warning: no ADB interface detected\n");
321                 adb_controller = NULL;
322         } else {
323 #ifdef CONFIG_PM
324                 pmu_register_sleep_notifier(&adb_sleep_notifier);
325 #endif /* CONFIG_PM */
326 #ifdef CONFIG_PPC
327                 if (machine_is_compatible("AAPL,PowerBook1998") ||
328                         machine_is_compatible("PowerBook1,1"))
329                         sleepy_trackpad = 1;
330 #endif /* CONFIG_PPC */
331                 init_completion(&adb_probe_task_comp);
332                 adbdev_init();
333                 adb_reset_bus();
334         }
335         return 0;
336 }
337
338 __initcall(adb_init);
339
340 #ifdef CONFIG_PM
341 /*
342  * notify clients before sleep and reset bus afterwards
343  */
344 int
345 adb_notify_sleep(struct pmu_sleep_notifier *self, int when)
346 {
347         int ret;
348         
349         switch (when) {
350         case PBOOK_SLEEP_REQUEST:
351                 adb_got_sleep = 1;
352                 /* We need to get a lock on the probe thread */
353                 down(&adb_probe_mutex);
354                 /* Stop autopoll */
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;
362                 }
363                 break;
364         case PBOOK_SLEEP_REJECT:
365                 if (adb_got_sleep) {
366                         adb_got_sleep = 0;
367                         up(&adb_probe_mutex);
368                         adb_reset_bus();
369                 }
370                 break;
371                 
372         case PBOOK_SLEEP_NOW:
373                 break;
374         case PBOOK_WAKE:
375                 adb_got_sleep = 0;
376                 up(&adb_probe_mutex);
377                 adb_reset_bus();
378                 break;
379         }
380         return PBOOK_SLEEP_OK;
381 }
382 #endif /* CONFIG_PM */
383
384 static int
385 do_adb_reset_bus(void)
386 {
387         int ret, nret;
388         
389         if (adb_controller == NULL)
390                 return -ENXIO;
391                 
392         if (adb_controller->autopoll)
393                 adb_controller->autopoll(0);
394
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);
400                 return -EBUSY;
401         }
402
403         if (sleepy_trackpad) {
404                 /* Let the trackpad settle down */
405                 adb_wait_ms(500);
406         }
407
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);
412
413         /* That one is still a bit synchronous, oh well... */
414         if (adb_controller->reset_bus)
415                 ret = adb_controller->reset_bus();
416         else
417                 ret = 0;
418
419         if (sleepy_trackpad) {
420                 /* Let the trackpad settle down */
421                 adb_wait_ms(1500);
422         }
423
424         if (!ret) {
425                 autopoll_devs = adb_scan_bus();
426                 if (adb_controller->autopoll)
427                         adb_controller->autopoll(autopoll_devs);
428         }
429         up(&adb_handler_sem);
430
431         nret = blocking_notifier_call_chain(&adb_client_list,
432                         ADB_MSG_POST_RESET, NULL);
433         if (nret & NOTIFY_STOP_MASK)
434                 return -EBUSY;
435         
436         return ret;
437 }
438
439 void
440 adb_poll(void)
441 {
442         if ((adb_controller == NULL)||(adb_controller->poll == NULL))
443                 return;
444         adb_controller->poll();
445 }
446
447 static void
448 adb_probe_wakeup(struct adb_request *req)
449 {
450         complete(&adb_probe_task_comp);
451 }
452
453 /* Static request used during probe */
454 static struct adb_request adb_sreq;
455 static unsigned long adb_sreq_lock; // Use semaphore ! */ 
456
457 int
458 adb_request(struct adb_request *req, void (*done)(struct adb_request *),
459             int flags, int nbytes, ...)
460 {
461         va_list list;
462         int i, use_sreq;
463         int rc;
464
465         if ((adb_controller == NULL) || (adb_controller->send_request == NULL))
466                 return -ENXIO;
467         if (nbytes < 1)
468                 return -EINVAL;
469         if (req == NULL && (flags & ADBREQ_NOSEND))
470                 return -EINVAL;
471         
472         if (req == NULL) {
473                 if (test_and_set_bit(0,&adb_sreq_lock)) {
474                         printk("adb.c: Warning: contention on static request !\n");
475                         return -EPERM;
476                 }
477                 req = &adb_sreq;
478                 flags |= ADBREQ_SYNC;
479                 use_sreq = 1;
480         } else
481                 use_sreq = 0;
482         req->nbytes = nbytes+1;
483         req->done = done;
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);
489         va_end(list);
490
491         if (flags & ADBREQ_NOSEND)
492                 return 0;
493
494         /* Synchronous requests send from the probe thread cause it to
495          * block. Beware that the "done" callback will be overriden !
496          */
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)
503                         goto bail;
504                 wait_for_completion(&adb_probe_task_comp);
505                 rc = 0;
506                 goto bail;
507         }
508
509         rc = adb_controller->send_request(req, flags & ADBREQ_SYNC);
510 bail:
511         if (use_sreq)
512                 clear_bit(0, &adb_sreq_lock);
513
514         return rc;
515 }
516
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. */
524 int
525 adb_register(int default_id, int handler_id, struct adb_ids *ids,
526              void (*handler)(unsigned char *, int, struct pt_regs *, int))
527 {
528         int i;
529
530         down(&adb_handler_sem);
531         ids->nids = 0;
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) {
537                                 printk(KERN_ERR
538                                        "Two handlers for ADB device %d\n",
539                                        default_id);
540                                 continue;
541                         }
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;
546                 }
547         }
548         up(&adb_handler_sem);
549         return ids->nids;
550 }
551
552 int
553 adb_unregister(int index)
554 {
555         int ret = -ENODEV;
556
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);
562                         yield();
563                         write_lock_irq(&adb_handler_lock);
564                 }
565                 ret = 0;
566                 adb_handler[index].handler = NULL;
567         }
568         write_unlock_irq(&adb_handler_lock);
569         up(&adb_handler_sem);
570         return ret;
571 }
572
573 void
574 adb_input(unsigned char *buf, int nb, struct pt_regs *regs, int autopoll)
575 {
576         int i, id;
577         static int dump_adb_input = 0;
578         unsigned long flags;
579         
580         void (*handler)(unsigned char *, int, struct pt_regs *, int);
581
582         /* We skip keystrokes and mouse moves when the sleep process
583          * has been started. We stop autopoll, but this is another security
584          */
585         if (adb_got_sleep)
586                 return;
587                 
588         id = buf[0] >> 4;
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);
594         }
595         write_lock_irqsave(&adb_handler_lock, flags);
596         handler = adb_handler[id].handler;
597         if (handler != NULL)
598                 adb_handler[id].busy = 1;
599         write_unlock_irqrestore(&adb_handler_lock, flags);
600         if (handler != NULL) {
601                 (*handler)(buf, nb, regs, autopoll);
602                 wmb();
603                 adb_handler[id].busy = 0;
604         }
605                 
606 }
607
608 /* Try to change handler to new_id. Will return 1 if successful. */
609 static int try_handler_change(int address, int new_id)
610 {
611         struct adb_request req;
612
613         if (adb_handler[address].handler_id == new_id)
614             return 1;
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)
620             return 0;
621         if (req.reply[2] != new_id)
622             return 0;
623         adb_handler[address].handler_id = req.reply[2];
624
625         return 1;
626 }
627
628 int
629 adb_try_handler_change(int address, int new_id)
630 {
631         int ret;
632
633         down(&adb_handler_sem);
634         ret = try_handler_change(address, new_id);
635         up(&adb_handler_sem);
636         return ret;
637 }
638
639 int
640 adb_get_infos(int address, int *original_address, int *handler_id)
641 {
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);
646
647         return (*original_address != 0);
648 }
649
650
651 /*
652  * /dev/adb device driver.
653  */
654
655 #define ADB_MAJOR       56      /* major number for /dev/adb */
656
657 struct adbdev_state {
658         spinlock_t      lock;
659         atomic_t        n_pending;
660         struct adb_request *completed;
661         wait_queue_head_t wait_queue;
662         int             inuse;
663 };
664
665 static void adb_write_done(struct adb_request *req)
666 {
667         struct adbdev_state *state = (struct adbdev_state *) req->arg;
668         unsigned long flags;
669
670         if (!req->complete) {
671                 req->reply_len = 0;
672                 req->complete = 1;
673         }
674         spin_lock_irqsave(&state->lock, flags);
675         atomic_dec(&state->n_pending);
676         if (!state->inuse) {
677                 kfree(req);
678                 if (atomic_read(&state->n_pending) == 0) {
679                         spin_unlock_irqrestore(&state->lock, flags);
680                         kfree(state);
681                         return;
682                 }
683         } else {
684                 struct adb_request **ap = &state->completed;
685                 while (*ap != NULL)
686                         ap = &(*ap)->next;
687                 req->next = NULL;
688                 *ap = req;
689                 wake_up_interruptible(&state->wait_queue);
690         }
691         spin_unlock_irqrestore(&state->lock, flags);
692 }
693
694 static int
695 do_adb_query(struct adb_request *req)
696 {
697         int     ret = -EINVAL;
698
699         switch(req->data[1])
700         {
701         case ADB_QUERY_GETDEVINFO:
702                 if (req->nbytes < 3)
703                         break;
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);
708                 req->complete = 1;
709                 req->reply_len = 2;
710                 adb_write_done(req);
711                 ret = 0;
712                 break;
713         }
714         return ret;
715 }
716
717 static int adb_open(struct inode *inode, struct file *file)
718 {
719         struct adbdev_state *state;
720
721         if (iminor(inode) > 0 || adb_controller == NULL)
722                 return -ENXIO;
723         state = kmalloc(sizeof(struct adbdev_state), GFP_KERNEL);
724         if (state == 0)
725                 return -ENOMEM;
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);
731         state->inuse = 1;
732
733         return 0;
734 }
735
736 static int adb_release(struct inode *inode, struct file *file)
737 {
738         struct adbdev_state *state = file->private_data;
739         unsigned long flags;
740
741         lock_kernel();
742         if (state) {
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);
748                         kfree(state);
749                 } else {
750                         state->inuse = 0;
751                         spin_unlock_irqrestore(&state->lock, flags);
752                 }
753         }
754         unlock_kernel();
755         return 0;
756 }
757
758 static ssize_t adb_read(struct file *file, char __user *buf,
759                         size_t count, loff_t *ppos)
760 {
761         int ret = 0;
762         struct adbdev_state *state = file->private_data;
763         struct adb_request *req;
764         wait_queue_t wait = __WAITQUEUE_INITIALIZER(wait,current);
765         unsigned long flags;
766
767         if (count < 2)
768                 return -EINVAL;
769         if (count > sizeof(req->reply))
770                 count = sizeof(req->reply);
771         if (!access_ok(VERIFY_WRITE, buf, count))
772                 return -EFAULT;
773
774         req = NULL;
775         spin_lock_irqsave(&state->lock, flags);
776         add_wait_queue(&state->wait_queue, &wait);
777         current->state = TASK_INTERRUPTIBLE;
778
779         for (;;) {
780                 req = state->completed;
781                 if (req != NULL)
782                         state->completed = req->next;
783                 else if (atomic_read(&state->n_pending) == 0)
784                         ret = -EIO;
785                 if (req != NULL || ret != 0)
786                         break;
787                 
788                 if (file->f_flags & O_NONBLOCK) {
789                         ret = -EAGAIN;
790                         break;
791                 }
792                 if (signal_pending(current)) {
793                         ret = -ERESTARTSYS;
794                         break;
795                 }
796                 spin_unlock_irqrestore(&state->lock, flags);
797                 schedule();
798                 spin_lock_irqsave(&state->lock, flags);
799         }
800
801         current->state = TASK_RUNNING;
802         remove_wait_queue(&state->wait_queue, &wait);
803         spin_unlock_irqrestore(&state->lock, flags);
804         
805         if (ret)
806                 return ret;
807
808         ret = req->reply_len;
809         if (ret > count)
810                 ret = count;
811         if (ret > 0 && copy_to_user(buf, req->reply, ret))
812                 ret = -EFAULT;
813
814         kfree(req);
815         return ret;
816 }
817
818 static ssize_t adb_write(struct file *file, const char __user *buf,
819                          size_t count, loff_t *ppos)
820 {
821         int ret/*, i*/;
822         struct adbdev_state *state = file->private_data;
823         struct adb_request *req;
824
825         if (count < 2 || count > sizeof(req->data))
826                 return -EINVAL;
827         if (adb_controller == NULL)
828                 return -ENXIO;
829         if (!access_ok(VERIFY_READ, buf, count))
830                 return -EFAULT;
831
832         req = (struct adb_request *) kmalloc(sizeof(struct adb_request),
833                                              GFP_KERNEL);
834         if (req == NULL)
835                 return -ENOMEM;
836
837         req->nbytes = count;
838         req->done = adb_write_done;
839         req->arg = (void *) state;
840         req->complete = 0;
841         
842         ret = -EFAULT;
843         if (copy_from_user(req->data, buf, count))
844                 goto out;
845
846         atomic_inc(&state->n_pending);
847
848         /* If a probe is in progress or we are sleeping, wait for it to complete */
849         down(&adb_probe_mutex);
850
851         /* Queries are special requests sent to the ADB driver itself */
852         if (req->data[0] == ADB_QUERY) {
853                 if (count > 1)
854                         ret = do_adb_query(req);
855                 else
856                         ret = -EINVAL;
857                 up(&adb_probe_mutex);
858         }
859         /* Special case for ADB_BUSRESET request, all others are sent to
860            the controller */
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);
866                 if (ret == 0)
867                         ret = count;
868                 goto out;
869         } else {        
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);
873                 else
874                         ret = -ENXIO;
875                 up(&adb_probe_mutex);
876         }
877
878         if (ret != 0) {
879                 atomic_dec(&state->n_pending);
880                 goto out;
881         }
882         return count;
883
884 out:
885         kfree(req);
886         return ret;
887 }
888
889 static struct file_operations adb_fops = {
890         .owner          = THIS_MODULE,
891         .llseek         = no_llseek,
892         .read           = adb_read,
893         .write          = adb_write,
894         .open           = adb_open,
895         .release        = adb_release,
896 };
897
898 static void
899 adbdev_init(void)
900 {
901         if (register_chrdev(ADB_MAJOR, "adb", &adb_fops)) {
902                 printk(KERN_ERR "adb: unable to get major %d\n", ADB_MAJOR);
903                 return;
904         }
905
906         devfs_mk_cdev(MKDEV(ADB_MAJOR, 0), S_IFCHR | S_IRUSR | S_IWUSR, "adb");
907
908         adb_dev_class = class_create(THIS_MODULE, "adb");
909         if (IS_ERR(adb_dev_class))
910                 return;
911         class_device_create(adb_dev_class, NULL, MKDEV(ADB_MAJOR, 0), NULL, "adb");
912 }