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