]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/sh/maple/maple.c
maple: Kill useless private_data pointer.
[linux-2.6-omap-h63xx.git] / drivers / sh / maple / maple.c
1 /*
2  * Core maple bus functionality
3  *
4  *  Copyright (C) 2007, 2008 Adrian McMenamin
5  *  Copyright (C) 2001 - 2008 Paul Mundt
6  *
7  * Based on 2.4 code by:
8  *
9  *  Copyright (C) 2000-2001 YAEGASHI Takeshi
10  *  Copyright (C) 2001 M. R. Brown
11  *  Copyright (C) 2001 Paul Mundt
12  *
13  * and others.
14  *
15  * This file is subject to the terms and conditions of the GNU General Public
16  * License.  See the file "COPYING" in the main directory of this archive
17  * for more details.
18  */
19 #include <linux/init.h>
20 #include <linux/kernel.h>
21 #include <linux/device.h>
22 #include <linux/interrupt.h>
23 #include <linux/list.h>
24 #include <linux/io.h>
25 #include <linux/slab.h>
26 #include <linux/maple.h>
27 #include <linux/dma-mapping.h>
28 #include <linux/delay.h>
29 #include <asm/cacheflush.h>
30 #include <asm/dma.h>
31 #include <asm/io.h>
32 #include <mach/dma.h>
33 #include <mach/sysasic.h>
34
35 MODULE_AUTHOR("Yaegashi Takeshi, Paul Mundt, M. R. Brown, Adrian McMenamin");
36 MODULE_DESCRIPTION("Maple bus driver for Dreamcast");
37 MODULE_LICENSE("GPL v2");
38 MODULE_SUPPORTED_DEVICE("{{SEGA, Dreamcast/Maple}}");
39
40 static void maple_dma_handler(struct work_struct *work);
41 static void maple_vblank_handler(struct work_struct *work);
42
43 static DECLARE_WORK(maple_dma_process, maple_dma_handler);
44 static DECLARE_WORK(maple_vblank_process, maple_vblank_handler);
45
46 static LIST_HEAD(maple_waitq);
47 static LIST_HEAD(maple_sentq);
48
49 /* mutex to protect queue of waiting packets */
50 static DEFINE_MUTEX(maple_wlist_lock);
51
52 static struct maple_driver maple_dummy_driver;
53 static struct device maple_bus;
54 static int subdevice_map[MAPLE_PORTS];
55 static unsigned long *maple_sendbuf, *maple_sendptr, *maple_lastptr;
56 static unsigned long maple_pnp_time;
57 static int started, scanning, fullscan;
58 static struct kmem_cache *maple_queue_cache;
59
60 struct maple_device_specify {
61         int port;
62         int unit;
63 };
64
65 static bool checked[4];
66 static struct maple_device *baseunits[4];
67
68 /**
69  * maple_driver_register - register a maple driver
70  * @drv: maple driver to be registered.
71  *
72  * Registers the passed in @drv, while updating the bus type.
73  * Devices with matching function IDs will be automatically probed.
74  */
75 int maple_driver_register(struct maple_driver *drv)
76 {
77         if (!drv)
78                 return -EINVAL;
79
80         drv->drv.bus = &maple_bus_type;
81
82         return driver_register(&drv->drv);
83 }
84 EXPORT_SYMBOL_GPL(maple_driver_register);
85
86 /**
87  * maple_driver_unregister - unregister a maple driver.
88  * @drv: maple driver to unregister.
89  *
90  * Cleans up after maple_driver_register(). To be invoked in the exit
91  * path of any module drivers.
92  */
93 void maple_driver_unregister(struct maple_driver *drv)
94 {
95         driver_unregister(&drv->drv);
96 }
97 EXPORT_SYMBOL_GPL(maple_driver_unregister);
98
99 /* set hardware registers to enable next round of dma */
100 static void maplebus_dma_reset(void)
101 {
102         ctrl_outl(MAPLE_MAGIC, MAPLE_RESET);
103         /* set trig type to 0 for software trigger, 1 for hardware (VBLANK) */
104         ctrl_outl(1, MAPLE_TRIGTYPE);
105         ctrl_outl(MAPLE_2MBPS | MAPLE_TIMEOUT(50000), MAPLE_SPEED);
106         ctrl_outl(PHYSADDR(maple_sendbuf), MAPLE_DMAADDR);
107         ctrl_outl(1, MAPLE_ENABLE);
108 }
109
110 /**
111  * maple_getcond_callback - setup handling MAPLE_COMMAND_GETCOND
112  * @dev: device responding
113  * @callback: handler callback
114  * @interval: interval in jiffies between callbacks
115  * @function: the function code for the device
116  */
117 void maple_getcond_callback(struct maple_device *dev,
118                         void (*callback) (struct mapleq *mq),
119                         unsigned long interval, unsigned long function)
120 {
121         dev->callback = callback;
122         dev->interval = interval;
123         dev->function = cpu_to_be32(function);
124         dev->when = jiffies;
125 }
126 EXPORT_SYMBOL_GPL(maple_getcond_callback);
127
128 static int maple_dma_done(void)
129 {
130         return (ctrl_inl(MAPLE_STATE) & 1) == 0;
131 }
132
133 static void maple_release_device(struct device *dev)
134 {
135         struct maple_device *mdev;
136         struct mapleq *mq;
137         if (!dev)
138                 return;
139         mdev = to_maple_dev(dev);
140         mq = mdev->mq;
141         if (mq) {
142                 if (mq->recvbufdcsp)
143                         kmem_cache_free(maple_queue_cache, mq->recvbufdcsp);
144                 kfree(mq);
145                 mq = NULL;
146         }
147         kfree(mdev);
148 }
149
150 /*
151  * maple_add_packet - add a single instruction to the queue
152  * @mdev - maple device
153  * @function - function on device being queried
154  * @command - maple command to add
155  * @length - length of command string (in 32 bit words)
156  * @data - remainder of command string
157  */
158 int maple_add_packet(struct maple_device *mdev, u32 function, u32 command,
159         size_t length, void *data)
160 {
161         int locking, ret = 0;
162         void *sendbuf = NULL;
163
164         mutex_lock(&maple_wlist_lock);
165         /* bounce if device already locked */
166         locking = mutex_is_locked(&mdev->mq->mutex);
167         if (locking) {
168                 ret = -EBUSY;
169                 goto out;
170         }
171
172         mutex_lock(&mdev->mq->mutex);
173
174         if (length) {
175                 sendbuf = kmalloc(length * 4, GFP_KERNEL);
176                 if (!sendbuf) {
177                         mutex_unlock(&mdev->mq->mutex);
178                         ret = -ENOMEM;
179                         goto out;
180                 }
181                 ((__be32 *)sendbuf)[0] = cpu_to_be32(function);
182         }
183
184         mdev->mq->command = command;
185         mdev->mq->length = length;
186         if (length > 1)
187                 memcpy(sendbuf + 4, data, (length - 1) * 4);
188         mdev->mq->sendbuf = sendbuf;
189
190         list_add(&mdev->mq->list, &maple_waitq);
191 out:
192         mutex_unlock(&maple_wlist_lock);
193         return ret;
194 }
195 EXPORT_SYMBOL_GPL(maple_add_packet);
196
197 /*
198  * maple_add_packet_sleeps - add a single instruction to the queue
199  *  - waits for lock to be free
200  * @mdev - maple device
201  * @function - function on device being queried
202  * @command - maple command to add
203  * @length - length of command string (in 32 bit words)
204  * @data - remainder of command string
205  */
206 int maple_add_packet_sleeps(struct maple_device *mdev, u32 function,
207         u32 command, size_t length, void *data)
208 {
209         int locking, ret = 0;
210         void *sendbuf = NULL;
211
212         locking = mutex_lock_interruptible(&mdev->mq->mutex);
213         if (locking) {
214                 ret = -EIO;
215                 goto out;
216         }
217
218         if (length) {
219                 sendbuf = kmalloc(length * 4, GFP_KERNEL);
220                 if (!sendbuf) {
221                         mutex_unlock(&mdev->mq->mutex);
222                         ret = -ENOMEM;
223                         goto out;
224                 }
225                 ((__be32 *)sendbuf)[0] = cpu_to_be32(function);
226         }
227
228         mdev->mq->command = command;
229         mdev->mq->length = length;
230         if (length > 1)
231                 memcpy(sendbuf + 4, data, (length - 1) * 4);
232         mdev->mq->sendbuf = sendbuf;
233
234         mutex_lock(&maple_wlist_lock);
235         list_add(&mdev->mq->list, &maple_waitq);
236         mutex_unlock(&maple_wlist_lock);
237 out:
238         return ret;
239 }
240 EXPORT_SYMBOL_GPL(maple_add_packet_sleeps);
241
242 static struct mapleq *maple_allocq(struct maple_device *mdev)
243 {
244         struct mapleq *mq;
245
246         mq = kmalloc(sizeof(*mq), GFP_KERNEL);
247         if (!mq)
248                 goto failed_nomem;
249
250         mq->dev = mdev;
251         mq->recvbufdcsp = kmem_cache_zalloc(maple_queue_cache, GFP_KERNEL);
252         mq->recvbuf = (void *) P2SEGADDR(mq->recvbufdcsp);
253         if (!mq->recvbuf)
254                 goto failed_p2;
255         /*
256          * most devices do not need the mutex - but
257          * anything that injects block reads or writes
258          * will rely on it
259          */
260         mutex_init(&mq->mutex);
261
262         return mq;
263
264 failed_p2:
265         kfree(mq);
266 failed_nomem:
267         return NULL;
268 }
269
270 static struct maple_device *maple_alloc_dev(int port, int unit)
271 {
272         struct maple_device *mdev;
273
274         mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
275         if (!mdev)
276                 return NULL;
277
278         mdev->port = port;
279         mdev->unit = unit;
280         mdev->mq = maple_allocq(mdev);
281
282         if (!mdev->mq) {
283                 kfree(mdev);
284                 return NULL;
285         }
286         mdev->dev.bus = &maple_bus_type;
287         mdev->dev.parent = &maple_bus;
288         return mdev;
289 }
290
291 static void maple_free_dev(struct maple_device *mdev)
292 {
293         if (!mdev)
294                 return;
295         if (mdev->mq) {
296                 if (mdev->mq->recvbufdcsp)
297                         kmem_cache_free(maple_queue_cache,
298                                 mdev->mq->recvbufdcsp);
299                 kfree(mdev->mq);
300         }
301         kfree(mdev);
302 }
303
304 /* process the command queue into a maple command block
305  * terminating command has bit 32 of first long set to 0
306  */
307 static void maple_build_block(struct mapleq *mq)
308 {
309         int port, unit, from, to, len;
310         unsigned long *lsendbuf = mq->sendbuf;
311
312         port = mq->dev->port & 3;
313         unit = mq->dev->unit;
314         len = mq->length;
315         from = port << 6;
316         to = (port << 6) | (unit > 0 ? (1 << (unit - 1)) & 0x1f : 0x20);
317
318         *maple_lastptr &= 0x7fffffff;
319         maple_lastptr = maple_sendptr;
320
321         *maple_sendptr++ = (port << 16) | len | 0x80000000;
322         *maple_sendptr++ = PHYSADDR(mq->recvbuf);
323         *maple_sendptr++ =
324             mq->command | (to << 8) | (from << 16) | (len << 24);
325         while (len-- > 0)
326                 *maple_sendptr++ = *lsendbuf++;
327 }
328
329 /* build up command queue */
330 static void maple_send(void)
331 {
332         int i, maple_packets = 0;
333         struct mapleq *mq, *nmq;
334
335         if (!list_empty(&maple_sentq))
336                 return;
337         mutex_lock(&maple_wlist_lock);
338         if (list_empty(&maple_waitq) || !maple_dma_done()) {
339                 mutex_unlock(&maple_wlist_lock);
340                 return;
341         }
342         mutex_unlock(&maple_wlist_lock);
343         maple_lastptr = maple_sendbuf;
344         maple_sendptr = maple_sendbuf;
345         mutex_lock(&maple_wlist_lock);
346         list_for_each_entry_safe(mq, nmq, &maple_waitq, list) {
347                 maple_build_block(mq);
348                 list_move(&mq->list, &maple_sentq);
349                 if (maple_packets++ > MAPLE_MAXPACKETS)
350                         break;
351         }
352         mutex_unlock(&maple_wlist_lock);
353         if (maple_packets > 0) {
354                 for (i = 0; i < (1 << MAPLE_DMA_PAGES); i++)
355                         dma_cache_sync(0, maple_sendbuf + i * PAGE_SIZE,
356                                        PAGE_SIZE, DMA_BIDIRECTIONAL);
357         }
358 }
359
360 /* check if there is a driver registered likely to match this device */
361 static int check_matching_maple_driver(struct device_driver *driver,
362                                         void *devptr)
363 {
364         struct maple_driver *maple_drv;
365         struct maple_device *mdev;
366
367         mdev = devptr;
368         maple_drv = to_maple_driver(driver);
369         if (mdev->devinfo.function & cpu_to_be32(maple_drv->function))
370                 return 1;
371         return 0;
372 }
373
374 static void maple_detach_driver(struct maple_device *mdev)
375 {
376         if (!mdev)
377                 return;
378         device_unregister(&mdev->dev);
379         mdev = NULL;
380 }
381
382 /* process initial MAPLE_COMMAND_DEVINFO for each device or port */
383 static void maple_attach_driver(struct maple_device *mdev)
384 {
385         char *p, *recvbuf;
386         unsigned long function;
387         int matched, retval;
388
389         recvbuf = mdev->mq->recvbuf;
390         /* copy the data as individual elements in
391         * case of memory optimisation */
392         memcpy(&mdev->devinfo.function, recvbuf + 4, 4);
393         memcpy(&mdev->devinfo.function_data[0], recvbuf + 8, 12);
394         memcpy(&mdev->devinfo.area_code, recvbuf + 20, 1);
395         memcpy(&mdev->devinfo.connector_direction, recvbuf + 21, 1);
396         memcpy(&mdev->devinfo.product_name[0], recvbuf + 22, 30);
397         memcpy(&mdev->devinfo.product_licence[0], recvbuf + 52, 60);
398         memcpy(&mdev->devinfo.standby_power, recvbuf + 112, 2);
399         memcpy(&mdev->devinfo.max_power, recvbuf + 114, 2);
400         memcpy(mdev->product_name, mdev->devinfo.product_name, 30);
401         mdev->product_name[30] = '\0';
402         memcpy(mdev->product_licence, mdev->devinfo.product_licence, 60);
403         mdev->product_licence[60] = '\0';
404
405         for (p = mdev->product_name + 29; mdev->product_name <= p; p--)
406                 if (*p == ' ')
407                         *p = '\0';
408                 else
409                         break;
410         for (p = mdev->product_licence + 59; mdev->product_licence <= p; p--)
411                 if (*p == ' ')
412                         *p = '\0';
413                 else
414                         break;
415
416         printk(KERN_INFO "Maple device detected: %s\n",
417                 mdev->product_name);
418         printk(KERN_INFO "Maple device: %s\n", mdev->product_licence);
419
420         function = be32_to_cpu(mdev->devinfo.function);
421
422         if (function > 0x200) {
423                 /* Do this silently - as not a real device */
424                 function = 0;
425                 mdev->driver = &maple_dummy_driver;
426                 sprintf(mdev->dev.bus_id, "%d:0.port", mdev->port);
427         } else {
428                 printk(KERN_INFO
429                         "Maple bus at (%d, %d): Function 0x%lX\n",
430                         mdev->port, mdev->unit, function);
431
432                 matched =
433                         bus_for_each_drv(&maple_bus_type, NULL, mdev,
434                                 check_matching_maple_driver);
435
436                 if (matched == 0) {
437                         /* Driver does not exist yet */
438                         printk(KERN_INFO
439                                 "No maple driver found.\n");
440                         mdev->driver = &maple_dummy_driver;
441                 }
442                 sprintf(mdev->dev.bus_id, "%d:0%d.%lX", mdev->port,
443                         mdev->unit, function);
444         }
445         mdev->function = function;
446         mdev->dev.release = &maple_release_device;
447         retval = device_register(&mdev->dev);
448         if (retval) {
449                 printk(KERN_INFO
450                 "Maple bus: Attempt to register device"
451                 " (%x, %x) failed.\n",
452                 mdev->port, mdev->unit);
453                 maple_free_dev(mdev);
454                 mdev = NULL;
455                 return;
456         }
457 }
458
459 /*
460  * if device has been registered for the given
461  * port and unit then return 1 - allows identification
462  * of which devices need to be attached or detached
463  */
464 static int detach_maple_device(struct device *device, void *portptr)
465 {
466         struct maple_device_specify *ds;
467         struct maple_device *mdev;
468
469         ds = portptr;
470         mdev = to_maple_dev(device);
471         if (mdev->port == ds->port && mdev->unit == ds->unit)
472                 return 1;
473         return 0;
474 }
475
476 static int setup_maple_commands(struct device *device, void *ignored)
477 {
478         int add;
479         struct maple_device *maple_dev = to_maple_dev(device);
480
481         if ((maple_dev->interval > 0)
482             && time_after(jiffies, maple_dev->when)) {
483                 /* bounce if we cannot lock */
484                 add = maple_add_packet(maple_dev,
485                         be32_to_cpu(maple_dev->devinfo.function),
486                         MAPLE_COMMAND_GETCOND, 1, NULL);
487                 if (!add)
488                         maple_dev->when = jiffies + maple_dev->interval;
489         } else {
490                 if (time_after(jiffies, maple_pnp_time))
491                         /* This will also bounce */
492                         maple_add_packet(maple_dev, 0,
493                                 MAPLE_COMMAND_DEVINFO, 0, NULL);
494         }
495         return 0;
496 }
497
498 /* VBLANK bottom half - implemented via workqueue */
499 static void maple_vblank_handler(struct work_struct *work)
500 {
501         if (!list_empty(&maple_sentq) || !maple_dma_done())
502                 return;
503
504         ctrl_outl(0, MAPLE_ENABLE);
505
506         bus_for_each_dev(&maple_bus_type, NULL, NULL,
507                          setup_maple_commands);
508
509         if (time_after(jiffies, maple_pnp_time))
510                 maple_pnp_time = jiffies + MAPLE_PNP_INTERVAL;
511
512         mutex_lock(&maple_wlist_lock);
513         if (!list_empty(&maple_waitq) && list_empty(&maple_sentq)) {
514                 mutex_unlock(&maple_wlist_lock);
515                 maple_send();
516         } else {
517                 mutex_unlock(&maple_wlist_lock);
518         }
519
520         maplebus_dma_reset();
521 }
522
523 /* handle devices added via hotplugs - placing them on queue for DEVINFO*/
524 static void maple_map_subunits(struct maple_device *mdev, int submask)
525 {
526         int retval, k, devcheck;
527         struct maple_device *mdev_add;
528         struct maple_device_specify ds;
529
530         ds.port = mdev->port;
531         for (k = 0; k < 5; k++) {
532                 ds.unit = k + 1;
533                 retval =
534                     bus_for_each_dev(&maple_bus_type, NULL, &ds,
535                                      detach_maple_device);
536                 if (retval) {
537                         submask = submask >> 1;
538                         continue;
539                 }
540                 devcheck = submask & 0x01;
541                 if (devcheck) {
542                         mdev_add = maple_alloc_dev(mdev->port, k + 1);
543                         if (!mdev_add)
544                                 return;
545                         maple_add_packet(mdev_add, 0, MAPLE_COMMAND_DEVINFO,
546                                 0, NULL);
547                         /* mark that we are checking sub devices */
548                         scanning = 1;
549                 }
550                 submask = submask >> 1;
551         }
552 }
553
554 /* mark a device as removed */
555 static void maple_clean_submap(struct maple_device *mdev)
556 {
557         int killbit;
558
559         killbit = (mdev->unit > 0 ? (1 << (mdev->unit - 1)) & 0x1f : 0x20);
560         killbit = ~killbit;
561         killbit &= 0xFF;
562         subdevice_map[mdev->port] = subdevice_map[mdev->port] & killbit;
563 }
564
565 /* handle empty port or hotplug removal */
566 static void maple_response_none(struct maple_device *mdev,
567                                 struct mapleq *mq)
568 {
569         if (mdev->unit != 0) {
570                 list_del(&mq->list);
571                 maple_clean_submap(mdev);
572                 printk(KERN_INFO
573                        "Maple bus device detaching at (%d, %d)\n",
574                        mdev->port, mdev->unit);
575                 maple_detach_driver(mdev);
576                 return;
577         }
578         if (!started || !fullscan) {
579                 if (checked[mdev->port] == false) {
580                         checked[mdev->port] = true;
581                         printk(KERN_INFO "No maple devices attached"
582                                 " to port %d\n", mdev->port);
583                 }
584                 return;
585         }
586         maple_clean_submap(mdev);
587 }
588
589 /* preprocess hotplugs or scans */
590 static void maple_response_devinfo(struct maple_device *mdev,
591                                    char *recvbuf)
592 {
593         char submask;
594         if (!started || (scanning == 2) || !fullscan) {
595                 if ((mdev->unit == 0) && (checked[mdev->port] == false)) {
596                         checked[mdev->port] = true;
597                         maple_attach_driver(mdev);
598                 } else {
599                         if (mdev->unit != 0)
600                                 maple_attach_driver(mdev);
601                 }
602                 return;
603         }
604         if (mdev->unit == 0) {
605                 submask = recvbuf[2] & 0x1F;
606                 if (submask ^ subdevice_map[mdev->port]) {
607                         maple_map_subunits(mdev, submask);
608                         subdevice_map[mdev->port] = submask;
609                 }
610         }
611 }
612
613 static void maple_port_rescan(void)
614 {
615         int i;
616         struct maple_device *mdev;
617
618         fullscan = 1;
619         for (i = 0; i < MAPLE_PORTS; i++) {
620                 if (checked[i] == false) {
621                         fullscan = 0;
622                         mdev = baseunits[i];
623                         /*
624                          *  test lock in case scan has failed
625                          *  but device is still locked
626                          */
627                         if (mutex_is_locked(&mdev->mq->mutex))
628                                 mutex_unlock(&mdev->mq->mutex);
629                         maple_add_packet(mdev, 0, MAPLE_COMMAND_DEVINFO,
630                                 0, NULL);
631                 }
632         }
633 }
634
635 /* maple dma end bottom half - implemented via workqueue */
636 static void maple_dma_handler(struct work_struct *work)
637 {
638         struct mapleq *mq, *nmq;
639         struct maple_device *dev;
640         char *recvbuf;
641         enum maple_code code;
642
643         if (!maple_dma_done())
644                 return;
645         ctrl_outl(0, MAPLE_ENABLE);
646         if (!list_empty(&maple_sentq)) {
647                 list_for_each_entry_safe(mq, nmq, &maple_sentq, list) {
648                         recvbuf = mq->recvbuf;
649                         code = recvbuf[0];
650                         dev = mq->dev;
651                         kfree(mq->sendbuf);
652                         mutex_unlock(&mq->mutex);
653                         list_del_init(&mq->list);
654
655                         switch (code) {
656                         case MAPLE_RESPONSE_NONE:
657                                 maple_response_none(dev, mq);
658                                 break;
659
660                         case MAPLE_RESPONSE_DEVINFO:
661                                 maple_response_devinfo(dev, recvbuf);
662                                 break;
663
664                         case MAPLE_RESPONSE_DATATRF:
665                                 if (dev->callback)
666                                         dev->callback(mq);
667                                 break;
668
669                         case MAPLE_RESPONSE_FILEERR:
670                         case MAPLE_RESPONSE_AGAIN:
671                         case MAPLE_RESPONSE_BADCMD:
672                         case MAPLE_RESPONSE_BADFUNC:
673                                 printk(KERN_DEBUG
674                                        "Maple non-fatal error 0x%X\n",
675                                        code);
676                                 break;
677
678                         case MAPLE_RESPONSE_ALLINFO:
679                                 printk(KERN_DEBUG
680                                        "Maple - extended device information"
681                                         " not supported\n");
682                                 break;
683
684                         case MAPLE_RESPONSE_OK:
685                                 break;
686
687                         default:
688                                 break;
689                         }
690                 }
691                 /* if scanning is 1 then we have subdevices to check */
692                 if (scanning == 1) {
693                         maple_send();
694                         scanning = 2;
695                 } else
696                         scanning = 0;
697                 /*check if we have actually tested all ports yet */
698                 if (!fullscan)
699                         maple_port_rescan();
700                 /* mark that we have been through the first scan */
701                 if (started == 0)
702                         started = 1;
703         }
704         maplebus_dma_reset();
705 }
706
707 static irqreturn_t maplebus_dma_interrupt(int irq, void *dev_id)
708 {
709         /* Load everything into the bottom half */
710         schedule_work(&maple_dma_process);
711         return IRQ_HANDLED;
712 }
713
714 static irqreturn_t maplebus_vblank_interrupt(int irq, void *dev_id)
715 {
716         schedule_work(&maple_vblank_process);
717         return IRQ_HANDLED;
718 }
719
720 static int maple_set_dma_interrupt_handler(void)
721 {
722         return request_irq(HW_EVENT_MAPLE_DMA, maplebus_dma_interrupt,
723                 IRQF_SHARED, "maple bus DMA", &maple_dummy_driver);
724 }
725
726 static int maple_set_vblank_interrupt_handler(void)
727 {
728         return request_irq(HW_EVENT_VSYNC, maplebus_vblank_interrupt,
729                 IRQF_SHARED, "maple bus VBLANK", &maple_dummy_driver);
730 }
731
732 static int maple_get_dma_buffer(void)
733 {
734         maple_sendbuf =
735             (void *) __get_free_pages(GFP_KERNEL | __GFP_ZERO,
736                                       MAPLE_DMA_PAGES);
737         if (!maple_sendbuf)
738                 return -ENOMEM;
739         return 0;
740 }
741
742 static int match_maple_bus_driver(struct device *devptr,
743                                   struct device_driver *drvptr)
744 {
745         struct maple_driver *maple_drv = to_maple_driver(drvptr);
746         struct maple_device *maple_dev = to_maple_dev(devptr);
747
748         /* Trap empty port case */
749         if (maple_dev->devinfo.function == 0xFFFFFFFF)
750                 return 0;
751         else if (maple_dev->devinfo.function &
752                  cpu_to_be32(maple_drv->function))
753                 return 1;
754         return 0;
755 }
756
757 static int maple_bus_uevent(struct device *dev,
758                             struct kobj_uevent_env *env)
759 {
760         return 0;
761 }
762
763 static void maple_bus_release(struct device *dev)
764 {
765 }
766
767 static struct maple_driver maple_dummy_driver = {
768         .drv = {
769                 .name = "maple_dummy_driver",
770                 .bus = &maple_bus_type,
771         },
772 };
773
774 struct bus_type maple_bus_type = {
775         .name = "maple",
776         .match = match_maple_bus_driver,
777         .uevent = maple_bus_uevent,
778 };
779 EXPORT_SYMBOL_GPL(maple_bus_type);
780
781 static struct device maple_bus = {
782         .bus_id = "maple",
783         .release = maple_bus_release,
784 };
785
786 static int __init maple_bus_init(void)
787 {
788         int retval, i;
789         struct maple_device *mdev[MAPLE_PORTS];
790         ctrl_outl(0, MAPLE_STATE);
791
792         retval = device_register(&maple_bus);
793         if (retval)
794                 goto cleanup;
795
796         retval = bus_register(&maple_bus_type);
797         if (retval)
798                 goto cleanup_device;
799
800         retval = driver_register(&maple_dummy_driver.drv);
801         if (retval)
802                 goto cleanup_bus;
803
804         /* allocate memory for maple bus dma */
805         retval = maple_get_dma_buffer();
806         if (retval) {
807                 printk(KERN_INFO
808                        "Maple bus: Failed to allocate Maple DMA buffers\n");
809                 goto cleanup_basic;
810         }
811
812         /* set up DMA interrupt handler */
813         retval = maple_set_dma_interrupt_handler();
814         if (retval) {
815                 printk(KERN_INFO
816                        "Maple bus: Failed to grab maple DMA IRQ\n");
817                 goto cleanup_dma;
818         }
819
820         /* set up VBLANK interrupt handler */
821         retval = maple_set_vblank_interrupt_handler();
822         if (retval) {
823                 printk(KERN_INFO "Maple bus: Failed to grab VBLANK IRQ\n");
824                 goto cleanup_irq;
825         }
826
827         maple_queue_cache =
828             kmem_cache_create("maple_queue_cache", 0x400, 0,
829                               SLAB_POISON|SLAB_HWCACHE_ALIGN, NULL);
830
831         if (!maple_queue_cache)
832                 goto cleanup_bothirqs;
833
834         INIT_LIST_HEAD(&maple_waitq);
835         INIT_LIST_HEAD(&maple_sentq);
836
837         /* setup maple ports */
838         for (i = 0; i < MAPLE_PORTS; i++) {
839                 checked[i] = false;
840                 mdev[i] = maple_alloc_dev(i, 0);
841                 baseunits[i] = mdev[i];
842                 if (!mdev[i]) {
843                         while (i-- > 0)
844                                 maple_free_dev(mdev[i]);
845                         goto cleanup_cache;
846                 }
847                 maple_add_packet(mdev[i], 0, MAPLE_COMMAND_DEVINFO, 0, NULL);
848                 subdevice_map[i] = 0;
849         }
850
851         /* setup maplebus hardware */
852         maplebus_dma_reset();
853         /* initial detection */
854         maple_send();
855         maple_pnp_time = jiffies;
856         printk(KERN_INFO "Maple bus core now registered.\n");
857
858         return 0;
859
860 cleanup_cache:
861         kmem_cache_destroy(maple_queue_cache);
862
863 cleanup_bothirqs:
864         free_irq(HW_EVENT_VSYNC, 0);
865
866 cleanup_irq:
867         free_irq(HW_EVENT_MAPLE_DMA, 0);
868
869 cleanup_dma:
870         free_pages((unsigned long) maple_sendbuf, MAPLE_DMA_PAGES);
871
872 cleanup_basic:
873         driver_unregister(&maple_dummy_driver.drv);
874
875 cleanup_bus:
876         bus_unregister(&maple_bus_type);
877
878 cleanup_device:
879         device_unregister(&maple_bus);
880
881 cleanup:
882         printk(KERN_INFO "Maple bus registration failed\n");
883         return retval;
884 }
885 /* Push init to later to ensure hardware gets detected */
886 fs_initcall(maple_bus_init);