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