Merge branch 'linux-2.6'
[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/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/smp_lock.h>
28 #include <linux/adb.h>
29 #include <linux/cuda.h>
30 #include <linux/pmu.h>
31 #include <linux/notifier.h>
32 #include <linux/wait.h>
33 #include <linux/init.h>
34 #include <linux/delay.h>
35 #include <linux/spinlock.h>
36 #include <linux/completion.h>
37 #include <linux/device.h>
38 #include <linux/kthread.h>
39
40 #include <asm/uaccess.h>
41 #include <asm/semaphore.h>
42 #ifdef CONFIG_PPC
43 #include <asm/prom.h>
44 #include <asm/machdep.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 DECLARE_MUTEX(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_sem 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 DECLARE_MUTEX(adb_handler_sem);
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 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_MAC
302         if (!MACH_IS_MAC)
303                 return 0;
304 #endif
305
306         /* xmon may do early-init */
307         if (adb_inited)
308                 return 0;
309         adb_inited = 1;
310                 
311         adb_controller = NULL;
312
313         i = 0;
314         while ((driver = adb_driver_list[i++]) != NULL) {
315                 if (!driver->probe()) {
316                         adb_controller = driver;
317                         break;
318                 }
319         }
320         if ((adb_controller == NULL) || adb_controller->init()) {
321                 printk(KERN_WARNING "Warning: no ADB interface detected\n");
322                 adb_controller = NULL;
323         } else {
324 #ifdef CONFIG_PPC
325                 if (machine_is_compatible("AAPL,PowerBook1998") ||
326                         machine_is_compatible("PowerBook1,1"))
327                         sleepy_trackpad = 1;
328 #endif /* CONFIG_PPC */
329
330                 adbdev_init();
331                 adb_reset_bus();
332         }
333         return 0;
334 }
335
336 __initcall(adb_init);
337
338 static int
339 do_adb_reset_bus(void)
340 {
341         int ret;
342         
343         if (adb_controller == NULL)
344                 return -ENXIO;
345                 
346         if (adb_controller->autopoll)
347                 adb_controller->autopoll(0);
348
349         blocking_notifier_call_chain(&adb_client_list,
350                 ADB_MSG_PRE_RESET, NULL);
351
352         if (sleepy_trackpad) {
353                 /* Let the trackpad settle down */
354                 msleep(500);
355         }
356
357         down(&adb_handler_sem);
358         write_lock_irq(&adb_handler_lock);
359         memset(adb_handler, 0, sizeof(adb_handler));
360         write_unlock_irq(&adb_handler_lock);
361
362         /* That one is still a bit synchronous, oh well... */
363         if (adb_controller->reset_bus)
364                 ret = adb_controller->reset_bus();
365         else
366                 ret = 0;
367
368         if (sleepy_trackpad) {
369                 /* Let the trackpad settle down */
370                 msleep(1500);
371         }
372
373         if (!ret) {
374                 autopoll_devs = adb_scan_bus();
375                 if (adb_controller->autopoll)
376                         adb_controller->autopoll(autopoll_devs);
377         }
378         up(&adb_handler_sem);
379
380         blocking_notifier_call_chain(&adb_client_list,
381                 ADB_MSG_POST_RESET, NULL);
382         
383         return ret;
384 }
385
386 void
387 adb_poll(void)
388 {
389         if ((adb_controller == NULL)||(adb_controller->poll == NULL))
390                 return;
391         adb_controller->poll();
392 }
393
394 static void adb_sync_req_done(struct adb_request *req)
395 {
396         struct completion *comp = req->arg;
397
398         complete(comp);
399 }
400
401 int
402 adb_request(struct adb_request *req, void (*done)(struct adb_request *),
403             int flags, int nbytes, ...)
404 {
405         va_list list;
406         int i;
407         int rc;
408         struct completion comp;
409
410         if ((adb_controller == NULL) || (adb_controller->send_request == NULL))
411                 return -ENXIO;
412         if (nbytes < 1)
413                 return -EINVAL;
414
415         req->nbytes = nbytes+1;
416         req->done = done;
417         req->reply_expected = flags & ADBREQ_REPLY;
418         req->data[0] = ADB_PACKET;
419         va_start(list, nbytes);
420         for (i = 0; i < nbytes; ++i)
421                 req->data[i+1] = va_arg(list, int);
422         va_end(list);
423
424         if (flags & ADBREQ_NOSEND)
425                 return 0;
426
427         /* Synchronous requests block using an on-stack completion */
428         if (flags & ADBREQ_SYNC) {
429                 WARN_ON(done);
430                 req->done = adb_sync_req_done;
431                 req->arg = &comp;
432                 init_completion(&comp);
433         }
434
435         rc = adb_controller->send_request(req, 0);
436
437         if ((flags & ADBREQ_SYNC) && !rc && !req->complete)
438                 wait_for_completion(&comp);
439
440         return rc;
441 }
442
443  /* Ultimately this should return the number of devices with
444     the given default id.
445     And it does it now ! Note: changed behaviour: This function
446     will now register if default_id _and_ handler_id both match
447     but handler_id can be left to 0 to match with default_id only.
448     When handler_id is set, this function will try to adjust
449     the handler_id id it doesn't match. */
450 int
451 adb_register(int default_id, int handler_id, struct adb_ids *ids,
452              void (*handler)(unsigned char *, int, int))
453 {
454         int i;
455
456         down(&adb_handler_sem);
457         ids->nids = 0;
458         for (i = 1; i < 16; i++) {
459                 if ((adb_handler[i].original_address == default_id) &&
460                     (!handler_id || (handler_id == adb_handler[i].handler_id) || 
461                     try_handler_change(i, handler_id))) {
462                         if (adb_handler[i].handler != 0) {
463                                 printk(KERN_ERR
464                                        "Two handlers for ADB device %d\n",
465                                        default_id);
466                                 continue;
467                         }
468                         write_lock_irq(&adb_handler_lock);
469                         adb_handler[i].handler = handler;
470                         write_unlock_irq(&adb_handler_lock);
471                         ids->id[ids->nids++] = i;
472                 }
473         }
474         up(&adb_handler_sem);
475         return ids->nids;
476 }
477
478 int
479 adb_unregister(int index)
480 {
481         int ret = -ENODEV;
482
483         down(&adb_handler_sem);
484         write_lock_irq(&adb_handler_lock);
485         if (adb_handler[index].handler) {
486                 while(adb_handler[index].busy) {
487                         write_unlock_irq(&adb_handler_lock);
488                         yield();
489                         write_lock_irq(&adb_handler_lock);
490                 }
491                 ret = 0;
492                 adb_handler[index].handler = NULL;
493         }
494         write_unlock_irq(&adb_handler_lock);
495         up(&adb_handler_sem);
496         return ret;
497 }
498
499 void
500 adb_input(unsigned char *buf, int nb, int autopoll)
501 {
502         int i, id;
503         static int dump_adb_input = 0;
504         unsigned long flags;
505         
506         void (*handler)(unsigned char *, int, int);
507
508         /* We skip keystrokes and mouse moves when the sleep process
509          * has been started. We stop autopoll, but this is another security
510          */
511         if (adb_got_sleep)
512                 return;
513                 
514         id = buf[0] >> 4;
515         if (dump_adb_input) {
516                 printk(KERN_INFO "adb packet: ");
517                 for (i = 0; i < nb; ++i)
518                         printk(" %x", buf[i]);
519                 printk(", id = %d\n", id);
520         }
521         write_lock_irqsave(&adb_handler_lock, flags);
522         handler = adb_handler[id].handler;
523         if (handler != NULL)
524                 adb_handler[id].busy = 1;
525         write_unlock_irqrestore(&adb_handler_lock, flags);
526         if (handler != NULL) {
527                 (*handler)(buf, nb, autopoll);
528                 wmb();
529                 adb_handler[id].busy = 0;
530         }
531                 
532 }
533
534 /* Try to change handler to new_id. Will return 1 if successful. */
535 static int try_handler_change(int address, int new_id)
536 {
537         struct adb_request req;
538
539         if (adb_handler[address].handler_id == new_id)
540             return 1;
541         adb_request(&req, NULL, ADBREQ_SYNC, 3,
542             ADB_WRITEREG(address, 3), address | 0x20, new_id);
543         adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
544             ADB_READREG(address, 3));
545         if (req.reply_len < 2)
546             return 0;
547         if (req.reply[2] != new_id)
548             return 0;
549         adb_handler[address].handler_id = req.reply[2];
550
551         return 1;
552 }
553
554 int
555 adb_try_handler_change(int address, int new_id)
556 {
557         int ret;
558
559         down(&adb_handler_sem);
560         ret = try_handler_change(address, new_id);
561         up(&adb_handler_sem);
562         return ret;
563 }
564
565 int
566 adb_get_infos(int address, int *original_address, int *handler_id)
567 {
568         down(&adb_handler_sem);
569         *original_address = adb_handler[address].original_address;
570         *handler_id = adb_handler[address].handler_id;
571         up(&adb_handler_sem);
572
573         return (*original_address != 0);
574 }
575
576
577 /*
578  * /dev/adb device driver.
579  */
580
581 #define ADB_MAJOR       56      /* major number for /dev/adb */
582
583 struct adbdev_state {
584         spinlock_t      lock;
585         atomic_t        n_pending;
586         struct adb_request *completed;
587         wait_queue_head_t wait_queue;
588         int             inuse;
589 };
590
591 static void adb_write_done(struct adb_request *req)
592 {
593         struct adbdev_state *state = (struct adbdev_state *) req->arg;
594         unsigned long flags;
595
596         if (!req->complete) {
597                 req->reply_len = 0;
598                 req->complete = 1;
599         }
600         spin_lock_irqsave(&state->lock, flags);
601         atomic_dec(&state->n_pending);
602         if (!state->inuse) {
603                 kfree(req);
604                 if (atomic_read(&state->n_pending) == 0) {
605                         spin_unlock_irqrestore(&state->lock, flags);
606                         kfree(state);
607                         return;
608                 }
609         } else {
610                 struct adb_request **ap = &state->completed;
611                 while (*ap != NULL)
612                         ap = &(*ap)->next;
613                 req->next = NULL;
614                 *ap = req;
615                 wake_up_interruptible(&state->wait_queue);
616         }
617         spin_unlock_irqrestore(&state->lock, flags);
618 }
619
620 static int
621 do_adb_query(struct adb_request *req)
622 {
623         int     ret = -EINVAL;
624
625         switch(req->data[1])
626         {
627         case ADB_QUERY_GETDEVINFO:
628                 if (req->nbytes < 3)
629                         break;
630                 down(&adb_handler_sem);
631                 req->reply[0] = adb_handler[req->data[2]].original_address;
632                 req->reply[1] = adb_handler[req->data[2]].handler_id;
633                 up(&adb_handler_sem);
634                 req->complete = 1;
635                 req->reply_len = 2;
636                 adb_write_done(req);
637                 ret = 0;
638                 break;
639         }
640         return ret;
641 }
642
643 static int adb_open(struct inode *inode, struct file *file)
644 {
645         struct adbdev_state *state;
646
647         if (iminor(inode) > 0 || adb_controller == NULL)
648                 return -ENXIO;
649         state = kmalloc(sizeof(struct adbdev_state), GFP_KERNEL);
650         if (state == 0)
651                 return -ENOMEM;
652         file->private_data = state;
653         spin_lock_init(&state->lock);
654         atomic_set(&state->n_pending, 0);
655         state->completed = NULL;
656         init_waitqueue_head(&state->wait_queue);
657         state->inuse = 1;
658
659         return 0;
660 }
661
662 static int adb_release(struct inode *inode, struct file *file)
663 {
664         struct adbdev_state *state = file->private_data;
665         unsigned long flags;
666
667         lock_kernel();
668         if (state) {
669                 file->private_data = NULL;
670                 spin_lock_irqsave(&state->lock, flags);
671                 if (atomic_read(&state->n_pending) == 0
672                     && state->completed == NULL) {
673                         spin_unlock_irqrestore(&state->lock, flags);
674                         kfree(state);
675                 } else {
676                         state->inuse = 0;
677                         spin_unlock_irqrestore(&state->lock, flags);
678                 }
679         }
680         unlock_kernel();
681         return 0;
682 }
683
684 static ssize_t adb_read(struct file *file, char __user *buf,
685                         size_t count, loff_t *ppos)
686 {
687         int ret = 0;
688         struct adbdev_state *state = file->private_data;
689         struct adb_request *req;
690         wait_queue_t wait = __WAITQUEUE_INITIALIZER(wait,current);
691         unsigned long flags;
692
693         if (count < 2)
694                 return -EINVAL;
695         if (count > sizeof(req->reply))
696                 count = sizeof(req->reply);
697         if (!access_ok(VERIFY_WRITE, buf, count))
698                 return -EFAULT;
699
700         req = NULL;
701         spin_lock_irqsave(&state->lock, flags);
702         add_wait_queue(&state->wait_queue, &wait);
703         current->state = TASK_INTERRUPTIBLE;
704
705         for (;;) {
706                 req = state->completed;
707                 if (req != NULL)
708                         state->completed = req->next;
709                 else if (atomic_read(&state->n_pending) == 0)
710                         ret = -EIO;
711                 if (req != NULL || ret != 0)
712                         break;
713                 
714                 if (file->f_flags & O_NONBLOCK) {
715                         ret = -EAGAIN;
716                         break;
717                 }
718                 if (signal_pending(current)) {
719                         ret = -ERESTARTSYS;
720                         break;
721                 }
722                 spin_unlock_irqrestore(&state->lock, flags);
723                 schedule();
724                 spin_lock_irqsave(&state->lock, flags);
725         }
726
727         current->state = TASK_RUNNING;
728         remove_wait_queue(&state->wait_queue, &wait);
729         spin_unlock_irqrestore(&state->lock, flags);
730         
731         if (ret)
732                 return ret;
733
734         ret = req->reply_len;
735         if (ret > count)
736                 ret = count;
737         if (ret > 0 && copy_to_user(buf, req->reply, ret))
738                 ret = -EFAULT;
739
740         kfree(req);
741         return ret;
742 }
743
744 static ssize_t adb_write(struct file *file, const char __user *buf,
745                          size_t count, loff_t *ppos)
746 {
747         int ret/*, i*/;
748         struct adbdev_state *state = file->private_data;
749         struct adb_request *req;
750
751         if (count < 2 || count > sizeof(req->data))
752                 return -EINVAL;
753         if (adb_controller == NULL)
754                 return -ENXIO;
755         if (!access_ok(VERIFY_READ, buf, count))
756                 return -EFAULT;
757
758         req = kmalloc(sizeof(struct adb_request),
759                                              GFP_KERNEL);
760         if (req == NULL)
761                 return -ENOMEM;
762
763         req->nbytes = count;
764         req->done = adb_write_done;
765         req->arg = (void *) state;
766         req->complete = 0;
767         
768         ret = -EFAULT;
769         if (copy_from_user(req->data, buf, count))
770                 goto out;
771
772         atomic_inc(&state->n_pending);
773
774         /* If a probe is in progress or we are sleeping, wait for it to complete */
775         down(&adb_probe_mutex);
776
777         /* Queries are special requests sent to the ADB driver itself */
778         if (req->data[0] == ADB_QUERY) {
779                 if (count > 1)
780                         ret = do_adb_query(req);
781                 else
782                         ret = -EINVAL;
783                 up(&adb_probe_mutex);
784         }
785         /* Special case for ADB_BUSRESET request, all others are sent to
786            the controller */
787         else if ((req->data[0] == ADB_PACKET)&&(count > 1)
788                 &&(req->data[1] == ADB_BUSRESET)) {
789                 ret = do_adb_reset_bus();
790                 up(&adb_probe_mutex);
791                 atomic_dec(&state->n_pending);
792                 if (ret == 0)
793                         ret = count;
794                 goto out;
795         } else {        
796                 req->reply_expected = ((req->data[1] & 0xc) == 0xc);
797                 if (adb_controller && adb_controller->send_request)
798                         ret = adb_controller->send_request(req, 0);
799                 else
800                         ret = -ENXIO;
801                 up(&adb_probe_mutex);
802         }
803
804         if (ret != 0) {
805                 atomic_dec(&state->n_pending);
806                 goto out;
807         }
808         return count;
809
810 out:
811         kfree(req);
812         return ret;
813 }
814
815 static const struct file_operations adb_fops = {
816         .owner          = THIS_MODULE,
817         .llseek         = no_llseek,
818         .read           = adb_read,
819         .write          = adb_write,
820         .open           = adb_open,
821         .release        = adb_release,
822 };
823
824 static struct platform_driver adb_pfdrv = {
825         .driver = {
826                 .name = "adb",
827         },
828 #ifdef CONFIG_PM
829         .suspend = adb_suspend,
830         .resume = adb_resume,
831 #endif
832 };
833
834 static struct platform_device adb_pfdev = {
835         .name = "adb",
836 };
837
838 static int __init
839 adb_dummy_probe(struct platform_device *dev)
840 {
841         if (dev == &adb_pfdev)
842                 return 0;
843         return -ENODEV;
844 }
845
846 static void __init
847 adbdev_init(void)
848 {
849         if (register_chrdev(ADB_MAJOR, "adb", &adb_fops)) {
850                 printk(KERN_ERR "adb: unable to get major %d\n", ADB_MAJOR);
851                 return;
852         }
853
854         adb_dev_class = class_create(THIS_MODULE, "adb");
855         if (IS_ERR(adb_dev_class))
856                 return;
857         device_create(adb_dev_class, NULL, MKDEV(ADB_MAJOR, 0), "adb");
858
859         platform_device_register(&adb_pfdev);
860         platform_driver_probe(&adb_pfdrv, adb_dummy_probe);
861 }