[PATCH] 2.5.8-pre3 set_bit cleanup IV
[opensuse: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  * - /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 <asm/uaccess.h>
38 #ifdef CONFIG_PPC
39 #include <asm/prom.h>
40 #include <asm/hydra.h>
41 #endif
42
43 EXPORT_SYMBOL(adb_controller);
44 EXPORT_SYMBOL(adb_client_list);
45
46 extern struct adb_driver via_macii_driver;
47 extern struct adb_driver via_maciisi_driver;
48 extern struct adb_driver via_cuda_driver;
49 extern struct adb_driver adb_iop_driver;
50 extern struct adb_driver via_pmu_driver;
51 extern struct adb_driver macio_adb_driver;
52
53 static struct adb_driver *adb_driver_list[] = {
54 #ifdef CONFIG_ADB_MACII
55         &via_macii_driver,
56 #endif
57 #ifdef CONFIG_ADB_MACIISI
58         &via_maciisi_driver,
59 #endif
60 #ifdef CONFIG_ADB_CUDA
61         &via_cuda_driver,
62 #endif
63 #ifdef CONFIG_ADB_IOP
64         &adb_iop_driver,
65 #endif
66 #ifdef CONFIG_ADB_PMU
67         &via_pmu_driver,
68 #endif
69 #ifdef CONFIG_ADB_MACIO
70         &macio_adb_driver,
71 #endif
72         NULL
73 };
74
75 struct adb_driver *adb_controller;
76 struct notifier_block *adb_client_list = NULL;
77 static int adb_got_sleep = 0;
78 static int adb_inited = 0;
79 static pid_t adb_probe_task_pid;
80 static unsigned long adb_probe_task_flag;
81 static wait_queue_head_t adb_probe_task_wq;
82 static int sleepy_trackpad;
83 int __adb_probe_sync;
84
85 #ifdef CONFIG_PMAC_PBOOK
86 static int adb_notify_sleep(struct pmu_sleep_notifier *self, int when);
87 static struct pmu_sleep_notifier adb_sleep_notifier = {
88         adb_notify_sleep,
89         SLEEP_LEVEL_ADB,
90 };
91 #endif
92
93 static int adb_scan_bus(void);
94 static int do_adb_reset_bus(void);
95 static void adbdev_init(void);
96
97
98 static struct adb_handler {
99         void (*handler)(unsigned char *, int, struct pt_regs *, int);
100         int original_address;
101         int handler_id;
102 } adb_handler[16];
103
104 #if 0
105 static void printADBreply(struct adb_request *req)
106 {
107         int i;
108
109         printk("adb reply (%d)", req->reply_len);
110         for(i = 0; i < req->reply_len; i++)
111                 printk(" %x", req->reply[i]);
112         printk("\n");
113
114 }
115 #endif
116
117
118 static __inline__ void adb_wait_ms(unsigned int ms)
119 {
120         if (current->pid && adb_probe_task_pid &&
121           adb_probe_task_pid == current->pid) {
122                 current->state = TASK_UNINTERRUPTIBLE;
123                 schedule_timeout(1 + ms * HZ / 1000);
124         } else
125                 mdelay(ms);
126 }
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         strcpy(current->comm, "kadbprobe");
233         
234         spin_lock_irq(&current->sigmask_lock);
235         sigfillset(&current->blocked);
236         flush_signals(current);
237         spin_unlock_irq(&current->sigmask_lock);
238
239         printk(KERN_INFO "adb: starting probe task...\n");
240         do_adb_reset_bus();
241         printk(KERN_INFO "adb: finished probe task...\n");
242         
243         adb_probe_task_pid = 0;
244         clear_bit(0, &adb_probe_task_flag);
245         return 0;
246 }
247
248 static void
249 __adb_probe_task(void *data)
250 {
251         adb_probe_task_pid = kernel_thread(adb_probe_task, NULL,
252                 SIGCHLD | CLONE_FS | CLONE_FILES | CLONE_SIGHAND);
253 }
254
255 int
256 adb_reset_bus(void)
257 {
258         static struct tq_struct tqs = {
259                 routine:        __adb_probe_task,
260         };
261
262         if (__adb_probe_sync) {
263                 do_adb_reset_bus();
264                 return 0;
265         }
266                 
267         /* We need to get a lock on the probe thread */
268         while (test_and_set_bit(0, &adb_probe_task_flag))
269                 schedule();
270
271         /* Just wait for PID to be 0 just in case (possible race) */
272         while (adb_probe_task_pid != 0)
273                 schedule();
274
275         /* Create probe thread as a child of keventd */
276         if (current_is_keventd())
277                 __adb_probe_task(NULL);
278         else
279                 schedule_task(&tqs);
280         return 0;
281 }
282
283 int __init adb_init(void)
284 {
285         struct adb_driver *driver;
286         int i;
287
288 #ifdef CONFIG_PPC
289         if ( (_machine != _MACH_chrp) && (_machine != _MACH_Pmac) )
290                 return 0;
291 #endif
292 #ifdef CONFIG_MAC
293         if (!MACH_IS_MAC)
294                 return 0;
295 #endif
296
297         /* xmon may do early-init */
298         if (adb_inited)
299                 return 0;
300         adb_inited = 1;
301                 
302         adb_controller = NULL;
303
304         i = 0;
305         while ((driver = adb_driver_list[i++]) != NULL) {
306                 if (!driver->probe()) {
307                         adb_controller = driver;
308                         break;
309                 }
310         }
311         if ((adb_controller == NULL) || adb_controller->init()) {
312                 printk(KERN_WARNING "Warning: no ADB interface detected\n");
313                 adb_controller = NULL;
314         } else {
315 #ifdef CONFIG_PMAC_PBOOK
316                 pmu_register_sleep_notifier(&adb_sleep_notifier);
317 #endif /* CONFIG_PMAC_PBOOK */
318                 if (machine_is_compatible("AAPL,PowerBook1998") ||
319                         machine_is_compatible("PowerBook1,1"))
320                         sleepy_trackpad = 1;
321                 init_waitqueue_head(&adb_probe_task_wq);
322                 adbdev_init();
323                 adb_reset_bus();
324         }
325         return 0;
326 }
327
328 __initcall(adb_init);
329
330 #ifdef CONFIG_PMAC_PBOOK
331 /*
332  * notify clients before sleep and reset bus afterwards
333  */
334 int
335 adb_notify_sleep(struct pmu_sleep_notifier *self, int when)
336 {
337         int ret;
338         
339         switch (when) {
340         case PBOOK_SLEEP_REQUEST:
341                 adb_got_sleep = 1;
342                 /* We need to get a lock on the probe thread */
343                 while (test_and_set_bit(0, &adb_probe_task_flag))
344                         schedule();
345                 /* Just wait for PID to be 0 just in case (possible race) */
346                 while (adb_probe_task_pid != 0)
347                         schedule();
348                 if (adb_controller->autopoll)
349                         adb_controller->autopoll(0);
350                 ret = notifier_call_chain(&adb_client_list, ADB_MSG_POWERDOWN, NULL);
351                 if (ret & NOTIFY_STOP_MASK)
352                         return PBOOK_SLEEP_REFUSE;
353                 break;
354         case PBOOK_SLEEP_REJECT:
355                 if (adb_got_sleep) {
356                         adb_got_sleep = 0;
357                         clear_bit(0, &adb_probe_task_flag);
358                         adb_reset_bus();
359                 }
360                 break;
361                 
362         case PBOOK_SLEEP_NOW:
363                 break;
364         case PBOOK_WAKE:
365                 adb_got_sleep = 0;
366                 clear_bit(0, &adb_probe_task_flag);
367                 adb_reset_bus();
368                 break;
369         }
370         return PBOOK_SLEEP_OK;
371 }
372 #endif /* CONFIG_PMAC_PBOOK */
373
374 static int
375 do_adb_reset_bus(void)
376 {
377         int ret, nret, devs;
378         unsigned long flags;
379         
380         if (adb_controller == NULL)
381                 return -ENXIO;
382                 
383         if (adb_controller->autopoll)
384                 adb_controller->autopoll(0);
385
386         nret = notifier_call_chain(&adb_client_list, ADB_MSG_PRE_RESET, NULL);
387         if (nret & NOTIFY_STOP_MASK) {
388                 if (adb_controller->autopoll)
389                         adb_controller->autopoll(devs);
390                 return -EBUSY;
391         }
392
393         if (sleepy_trackpad) {
394                 /* Let the trackpad settle down */
395                 adb_wait_ms(500);
396         }
397         
398         save_flags(flags);
399         cli();
400         memset(adb_handler, 0, sizeof(adb_handler));
401         restore_flags(flags);
402
403         /* That one is still a bit synchronous, oh well... */
404         if (adb_controller->reset_bus)
405                 ret = adb_controller->reset_bus();
406         else
407                 ret = 0;
408
409         if (sleepy_trackpad) {
410                 /* Let the trackpad settle down */
411                 adb_wait_ms(1500);
412         }
413
414         if (!ret) {
415                 devs = adb_scan_bus();
416                 if (adb_controller->autopoll)
417                         adb_controller->autopoll(devs);
418         }
419
420         nret = notifier_call_chain(&adb_client_list, ADB_MSG_POST_RESET, NULL);
421         if (nret & NOTIFY_STOP_MASK)
422                 return -EBUSY;
423         
424         return ret;
425 }
426
427 void
428 adb_poll(void)
429 {
430         if ((adb_controller == NULL)||(adb_controller->poll == NULL))
431                 return;
432         adb_controller->poll();
433 }
434
435 static void
436 adb_probe_wakeup(struct adb_request *req)
437 {
438         wake_up(&adb_probe_task_wq);
439 }
440
441 static struct adb_request adb_sreq;
442 static unsigned long adb_sreq_lock; // Use semaphore ! */ 
443
444 int
445 adb_request(struct adb_request *req, void (*done)(struct adb_request *),
446             int flags, int nbytes, ...)
447 {
448         va_list list;
449         int i, use_sreq;
450         int rc;
451
452         if ((adb_controller == NULL) || (adb_controller->send_request == NULL))
453                 return -ENXIO;
454         if (nbytes < 1)
455                 return -EINVAL;
456         if (req == NULL && (flags & ADBREQ_NOSEND))
457                 return -EINVAL;
458         
459         if (req == NULL) {
460                 if (test_and_set_bit(0,&adb_sreq_lock)) {
461                         printk("adb.c: Warning: contention on static request !\n");
462                         return -EPERM;
463                 }
464                 req = &adb_sreq;
465                 flags |= ADBREQ_SYNC;
466                 use_sreq = 1;
467         } else
468                 use_sreq = 0;
469         req->nbytes = nbytes+1;
470         req->done = done;
471         req->reply_expected = flags & ADBREQ_REPLY;
472         req->data[0] = ADB_PACKET;
473         va_start(list, nbytes);
474         for (i = 0; i < nbytes; ++i)
475                 req->data[i+1] = va_arg(list, int);
476         va_end(list);
477
478         if (flags & ADBREQ_NOSEND)
479                 return 0;
480
481         /* Synchronous requests send from the probe thread cause it to
482          * block. Beware that the "done" callback will be overriden !
483          */
484         if ((flags & ADBREQ_SYNC) &&
485             (current->pid && adb_probe_task_pid &&
486             adb_probe_task_pid == current->pid)) {
487                 DECLARE_WAITQUEUE(wait, current);
488                 req->done = adb_probe_wakeup;
489                 add_wait_queue(&adb_probe_task_wq, &wait);
490                 rc = adb_controller->send_request(req, 0);
491                 if (rc || req->complete)
492                         goto bail;
493                 for (;;) {
494                         set_current_state(TASK_UNINTERRUPTIBLE);
495                         if (req->complete)
496                                 break;
497                         schedule();
498                 }
499                 current->state = TASK_RUNNING;
500                 remove_wait_queue(&adb_probe_task_wq, &wait);
501                 rc = 0;
502                 goto bail;
503         }
504
505         rc = adb_controller->send_request(req, flags & ADBREQ_SYNC);
506 bail:
507         if (use_sreq)
508                 clear_bit(0, &adb_sreq_lock);
509
510         return rc;
511 }
512
513  /* Ultimately this should return the number of devices with
514     the given default id.
515     And it does it now ! Note: changed behaviour: This function
516     will now register if default_id _and_ handler_id both match
517     but handler_id can be left to 0 to match with default_id only.
518     When handler_id is set, this function will try to adjust
519     the handler_id id it doesn't match. */
520 int
521 adb_register(int default_id, int handler_id, struct adb_ids *ids,
522              void (*handler)(unsigned char *, int, struct pt_regs *, int))
523 {
524         int i;
525
526         ids->nids = 0;
527         for (i = 1; i < 16; i++) {
528                 if ((adb_handler[i].original_address == default_id) &&
529                     (!handler_id || (handler_id == adb_handler[i].handler_id) || 
530                     adb_try_handler_change(i, handler_id))) {
531                         if (adb_handler[i].handler != 0) {
532                                 printk(KERN_ERR
533                                        "Two handlers for ADB device %d\n",
534                                        default_id);
535                                 continue;
536                         }
537                         adb_handler[i].handler = handler;
538                         ids->id[ids->nids++] = i;
539                 }
540         }
541         return ids->nids;
542 }
543
544 int
545 adb_unregister(int index)
546 {
547         if (!adb_handler[index].handler)
548                 return -ENODEV;
549         adb_handler[index].handler = 0;
550         return 0;
551 }
552
553 void
554 adb_input(unsigned char *buf, int nb, struct pt_regs *regs, int autopoll)
555 {
556         int i, id;
557         static int dump_adb_input = 0;
558
559         /* We skip keystrokes and mouse moves when the sleep process
560          * has been started. We stop autopoll, but this is another security
561          */
562         if (adb_got_sleep)
563                 return;
564                 
565         id = buf[0] >> 4;
566         if (dump_adb_input) {
567                 printk(KERN_INFO "adb packet: ");
568                 for (i = 0; i < nb; ++i)
569                         printk(" %x", buf[i]);
570                 printk(", id = %d\n", id);
571         }
572         if (adb_handler[id].handler != 0) {
573                 (*adb_handler[id].handler)(buf, nb, regs, autopoll);
574         }
575 }
576
577 /* Try to change handler to new_id. Will return 1 if successful */
578 int
579 adb_try_handler_change(int address, int new_id)
580 {
581         struct adb_request req;
582
583         if (adb_handler[address].handler_id == new_id)
584             return 1;
585         adb_request(&req, NULL, ADBREQ_SYNC, 3,
586             ADB_WRITEREG(address, 3), address | 0x20, new_id);
587         adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
588             ADB_READREG(address, 3));
589         if (req.reply_len < 2)
590             return 0;
591         if (req.reply[2] != new_id)
592             return 0;
593         adb_handler[address].handler_id = req.reply[2];
594
595         return 1;
596 }
597
598 int
599 adb_get_infos(int address, int *original_address, int *handler_id)
600 {
601         *original_address = adb_handler[address].original_address;
602         *handler_id = adb_handler[address].handler_id;
603         
604         return (*original_address != 0);
605 }
606
607
608 /*
609  * /dev/adb device driver.
610  */
611
612 #define ADB_MAJOR       56      /* major number for /dev/adb */
613
614 struct adbdev_state {
615         spinlock_t      lock;
616         atomic_t        n_pending;
617         struct adb_request *completed;
618         wait_queue_head_t wait_queue;
619         int             inuse;
620 };
621
622 static void adb_write_done(struct adb_request *req)
623 {
624         struct adbdev_state *state = (struct adbdev_state *) req->arg;
625         unsigned long flags;
626
627         if (!req->complete) {
628                 req->reply_len = 0;
629                 req->complete = 1;
630         }
631         spin_lock_irqsave(&state->lock, flags);
632         atomic_dec(&state->n_pending);
633         if (!state->inuse) {
634                 kfree(req);
635                 if (atomic_read(&state->n_pending) == 0) {
636                         spin_unlock_irqrestore(&state->lock, flags);
637                         kfree(state);
638                         return;
639                 }
640         } else {
641                 struct adb_request **ap = &state->completed;
642                 while (*ap != NULL)
643                         ap = &(*ap)->next;
644                 req->next = NULL;
645                 *ap = req;
646                 wake_up_interruptible(&state->wait_queue);
647         }
648         spin_unlock_irqrestore(&state->lock, flags);
649 }
650
651 static int adb_open(struct inode *inode, struct file *file)
652 {
653         struct adbdev_state *state;
654
655         if (MINOR(inode->i_rdev) > 0 || adb_controller == NULL)
656                 return -ENXIO;
657         state = kmalloc(sizeof(struct adbdev_state), GFP_KERNEL);
658         if (state == 0)
659                 return -ENOMEM;
660         file->private_data = state;
661         spin_lock_init(&state->lock);
662         atomic_set(&state->n_pending, 0);
663         state->completed = NULL;
664         init_waitqueue_head(&state->wait_queue);
665         state->inuse = 1;
666
667         return 0;
668 }
669
670 static int adb_release(struct inode *inode, struct file *file)
671 {
672         struct adbdev_state *state = file->private_data;
673         unsigned long flags;
674
675         if (state) {
676                 file->private_data = NULL;
677                 spin_lock_irqsave(&state->lock, flags);
678                 if (atomic_read(&state->n_pending) == 0
679                     && state->completed == NULL) {
680                         spin_unlock_irqrestore(&state->lock, flags);
681                         kfree(state);
682                 } else {
683                         state->inuse = 0;
684                         spin_unlock_irqrestore(&state->lock, flags);
685                 }
686         }
687         return 0;
688 }
689
690 static ssize_t adb_read(struct file *file, char *buf,
691                         size_t count, loff_t *ppos)
692 {
693         int ret;
694         struct adbdev_state *state = file->private_data;
695         struct adb_request *req;
696         wait_queue_t wait = __WAITQUEUE_INITIALIZER(wait,current);
697         unsigned long flags;
698
699         if (count < 2)
700                 return -EINVAL;
701         if (count > sizeof(req->reply))
702                 count = sizeof(req->reply);
703         ret = verify_area(VERIFY_WRITE, buf, count);
704         if (ret)
705                 return ret;
706
707         req = NULL;
708         add_wait_queue(&state->wait_queue, &wait);
709         current->state = TASK_INTERRUPTIBLE;
710
711         for (;;) {
712                 spin_lock_irqsave(&state->lock, flags);
713                 req = state->completed;
714                 if (req != NULL)
715                         state->completed = req->next;
716                 else if (atomic_read(&state->n_pending) == 0)
717                         ret = -EIO;
718                 spin_unlock_irqrestore(&state->lock, flags);
719                 if (req != NULL || ret != 0)
720                         break;
721                 
722                 if (file->f_flags & O_NONBLOCK) {
723                         ret = -EAGAIN;
724                         break;
725                 }
726                 if (signal_pending(current)) {
727                         ret = -ERESTARTSYS;
728                         break;
729                 }
730                 schedule();
731         }
732
733         current->state = TASK_RUNNING;
734         remove_wait_queue(&state->wait_queue, &wait);
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 *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         ret = verify_area(VERIFY_READ, buf, count);
759         if (ret)
760                 return ret;
761
762         req = (struct adb_request *) kmalloc(sizeof(struct adb_request),
763                                              GFP_KERNEL);
764         if (req == NULL)
765                 return -ENOMEM;
766
767         req->nbytes = count;
768         req->done = adb_write_done;
769         req->arg = (void *) state;
770         req->complete = 0;
771         
772         ret = -EFAULT;
773         if (copy_from_user(req->data, buf, count))
774                 goto out;
775
776         atomic_inc(&state->n_pending);
777         if (adb_controller == NULL) return -ENXIO;
778
779         /* Special case for ADB_BUSRESET request, all others are sent to
780            the controller */
781         if ((req->data[0] == ADB_PACKET)&&(count > 1)
782                 &&(req->data[1] == ADB_BUSRESET)) {
783                 ret = do_adb_reset_bus();
784                 atomic_dec(&state->n_pending);
785                 goto out;
786         } else {        
787                 req->reply_expected = ((req->data[1] & 0xc) == 0xc);
788
789                 if (adb_controller && adb_controller->send_request)
790                         ret = adb_controller->send_request(req, 0);
791                 else
792                         ret = -ENXIO;
793         }
794
795         if (ret != 0) {
796                 atomic_dec(&state->n_pending);
797                 goto out;
798         }
799         return count;
800
801 out:
802         kfree(req);
803         return ret;
804 }
805
806 static struct file_operations adb_fops = {
807         llseek:         no_llseek,
808         read:           adb_read,
809         write:          adb_write,
810         open:           adb_open,
811         release:        adb_release,
812 };
813
814 static void
815 adbdev_init(void)
816 {
817         if (devfs_register_chrdev(ADB_MAJOR, "adb", &adb_fops))
818                 printk(KERN_ERR "adb: unable to get major %d\n", ADB_MAJOR);
819         else
820                 devfs_register (NULL, "adb", DEVFS_FL_DEFAULT,
821                                 ADB_MAJOR, 0,
822                                 S_IFCHR | S_IRUSR | S_IWUSR,
823                                 &adb_fops, NULL);
824 }