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