maple: more robust device detection.

Replacement second-in-series patch:

This patch fixes up memory leaks and, by delaying initialisation, makes
device detection more robust.

It also makes clearer the difference between struct maple_device and
struct device, as well as cleaning up the interrupt request code
(without changing its function in any way).

Also now removes redundant registration checking.

Signed-off-by: Adrian McMenamin <adrian@mcmen.demon.co.uk>
Signed-off-by: Paul Mundt <lethal@linux-sh.org>
This commit is contained in:
Adrian McMenamin 2008-02-06 23:51:21 +00:00 коммит произвёл Paul Mundt
Родитель b948237891
Коммит b3c69e2481
1 изменённых файлов: 107 добавлений и 99 удалений

Просмотреть файл

@ -31,6 +31,7 @@
#include <asm/mach/dma.h> #include <asm/mach/dma.h>
#include <asm/mach/sysasic.h> #include <asm/mach/sysasic.h>
#include <asm/mach/maple.h> #include <asm/mach/maple.h>
#include <linux/delay.h>
MODULE_AUTHOR("Yaegshi Takeshi, Paul Mundt, M.R. Brown, Adrian McMenamin"); MODULE_AUTHOR("Yaegshi Takeshi, Paul Mundt, M.R. Brown, Adrian McMenamin");
MODULE_DESCRIPTION("Maple bus driver for Dreamcast"); MODULE_DESCRIPTION("Maple bus driver for Dreamcast");
@ -53,7 +54,7 @@ static struct device maple_bus;
static int subdevice_map[MAPLE_PORTS]; static int subdevice_map[MAPLE_PORTS];
static unsigned long *maple_sendbuf, *maple_sendptr, *maple_lastptr; static unsigned long *maple_sendbuf, *maple_sendptr, *maple_lastptr;
static unsigned long maple_pnp_time; static unsigned long maple_pnp_time;
static int started, scanning, liststatus; static int started, scanning, liststatus, realscan;
static struct kmem_cache *maple_queue_cache; static struct kmem_cache *maple_queue_cache;
struct maple_device_specify { struct maple_device_specify {
@ -73,7 +74,6 @@ int maple_driver_register(struct device_driver *drv)
drv->bus = &maple_bus_type; drv->bus = &maple_bus_type;
return driver_register(drv); return driver_register(drv);
} }
EXPORT_SYMBOL_GPL(maple_driver_register); EXPORT_SYMBOL_GPL(maple_driver_register);
/* set hardware registers to enable next round of dma */ /* set hardware registers to enable next round of dma */
@ -95,15 +95,14 @@ static void maplebus_dma_reset(void)
* @function: the function code for the device * @function: the function code for the device
*/ */
void maple_getcond_callback(struct maple_device *dev, void maple_getcond_callback(struct maple_device *dev,
void (*callback) (struct mapleq * mq), void (*callback) (struct mapleq *mq),
unsigned long interval, unsigned long function) unsigned long interval, unsigned long function)
{ {
dev->callback = callback; dev->callback = callback;
dev->interval = interval; dev->interval = interval;
dev->function = cpu_to_be32(function); dev->function = cpu_to_be32(function);
dev->when = jiffies; dev->when = jiffies;
} }
EXPORT_SYMBOL_GPL(maple_getcond_callback); EXPORT_SYMBOL_GPL(maple_getcond_callback);
static int maple_dma_done(void) static int maple_dma_done(void)
@ -113,10 +112,19 @@ static int maple_dma_done(void)
static void maple_release_device(struct device *dev) static void maple_release_device(struct device *dev)
{ {
if (dev->type) { struct maple_device *mdev;
kfree(dev->type->name); struct mapleq *mq;
kfree(dev->type); if (!dev)
return;
mdev = to_maple_dev(dev);
mq = mdev->mq;
if (mq) {
if (mq->recvbufdcsp)
kmem_cache_free(maple_queue_cache, mq->recvbufdcsp);
kfree(mq);
mq = NULL;
} }
kfree(mdev);
} }
/** /**
@ -129,10 +137,9 @@ void maple_add_packet(struct mapleq *mq)
list_add(&mq->list, &maple_waitq); list_add(&mq->list, &maple_waitq);
mutex_unlock(&maple_list_lock); mutex_unlock(&maple_list_lock);
} }
EXPORT_SYMBOL_GPL(maple_add_packet); EXPORT_SYMBOL_GPL(maple_add_packet);
static struct mapleq *maple_allocq(struct maple_device *dev) static struct mapleq *maple_allocq(struct maple_device *mdev)
{ {
struct mapleq *mq; struct mapleq *mq;
@ -140,7 +147,7 @@ static struct mapleq *maple_allocq(struct maple_device *dev)
if (!mq) if (!mq)
return NULL; return NULL;
mq->dev = dev; mq->dev = mdev;
mq->recvbufdcsp = kmem_cache_zalloc(maple_queue_cache, GFP_KERNEL); mq->recvbufdcsp = kmem_cache_zalloc(maple_queue_cache, GFP_KERNEL);
mq->recvbuf = (void *) P2SEGADDR(mq->recvbufdcsp); mq->recvbuf = (void *) P2SEGADDR(mq->recvbufdcsp);
if (!mq->recvbuf) { if (!mq->recvbuf) {
@ -153,22 +160,24 @@ static struct mapleq *maple_allocq(struct maple_device *dev)
static struct maple_device *maple_alloc_dev(int port, int unit) static struct maple_device *maple_alloc_dev(int port, int unit)
{ {
struct maple_device *dev; struct maple_device *mdev;
dev = kzalloc(sizeof(*dev), GFP_KERNEL); mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
if (!dev) if (!mdev)
return NULL; return NULL;
dev->port = port; mdev->port = port;
dev->unit = unit; mdev->unit = unit;
dev->mq = maple_allocq(dev); mdev->mq = maple_allocq(mdev);
if (!dev->mq) { if (!mdev->mq) {
kfree(dev); kfree(mdev);
return NULL; return NULL;
} }
mdev->dev.bus = &maple_bus_type;
return dev; mdev->dev.parent = &maple_bus;
mdev->function = 0;
return mdev;
} }
static void maple_free_dev(struct maple_device *mdev) static void maple_free_dev(struct maple_device *mdev)
@ -176,7 +185,9 @@ static void maple_free_dev(struct maple_device *mdev)
if (!mdev) if (!mdev)
return; return;
if (mdev->mq) { if (mdev->mq) {
kmem_cache_free(maple_queue_cache, mdev->mq->recvbufdcsp); if (mdev->mq->recvbufdcsp)
kmem_cache_free(maple_queue_cache,
mdev->mq->recvbufdcsp);
kfree(mdev->mq); kfree(mdev->mq);
} }
kfree(mdev); kfree(mdev);
@ -260,80 +271,89 @@ static void maple_detach_driver(struct maple_device *mdev)
mdev->driver->disconnect(mdev); mdev->driver->disconnect(mdev);
} }
mdev->driver = NULL; mdev->driver = NULL;
if (mdev->registered) { device_unregister(&mdev->dev);
maple_release_device(&mdev->dev); mdev = NULL;
device_unregister(&mdev->dev);
}
mdev->registered = 0;
maple_free_dev(mdev);
} }
/* process initial MAPLE_COMMAND_DEVINFO for each device or port */ /* process initial MAPLE_COMMAND_DEVINFO for each device or port */
static void maple_attach_driver(struct maple_device *dev) static void maple_attach_driver(struct maple_device *mdev)
{ {
char *p; char *p, *recvbuf;
char *recvbuf;
unsigned long function; unsigned long function;
int matched, retval; int matched, retval;
recvbuf = dev->mq->recvbuf; recvbuf = mdev->mq->recvbuf;
memcpy(&dev->devinfo, recvbuf + 4, sizeof(dev->devinfo)); /* copy the data as individual elements in
memcpy(dev->product_name, dev->devinfo.product_name, 30); * case of memory optimisation */
memcpy(dev->product_licence, dev->devinfo.product_licence, 60); memcpy(&mdev->devinfo.function, recvbuf + 4, 4);
dev->product_name[30] = '\0'; memcpy(&mdev->devinfo.function_data[0], recvbuf + 8, 12);
dev->product_licence[60] = '\0'; memcpy(&mdev->devinfo.area_code, recvbuf + 20, 1);
memcpy(&mdev->devinfo.connector_direction, recvbuf + 21, 1);
memcpy(&mdev->devinfo.product_name[0], recvbuf + 22, 30);
memcpy(&mdev->devinfo.product_licence[0], recvbuf + 52, 60);
memcpy(&mdev->devinfo.standby_power, recvbuf + 112, 2);
memcpy(&mdev->devinfo.max_power, recvbuf + 114, 2);
memcpy(mdev->product_name, mdev->devinfo.product_name, 30);
mdev->product_name[30] = '\0';
memcpy(mdev->product_licence, mdev->devinfo.product_licence, 60);
mdev->product_licence[60] = '\0';
for (p = dev->product_name + 29; dev->product_name <= p; p--) for (p = mdev->product_name + 29; mdev->product_name <= p; p--)
if (*p == ' ')
*p = '\0';
else
break;
for (p = mdev->product_licence + 59; mdev->product_licence <= p; p--)
if (*p == ' ') if (*p == ' ')
*p = '\0'; *p = '\0';
else else
break; break;
for (p = dev->product_licence + 59; dev->product_licence <= p; p--) if (realscan) {
if (*p == ' ') printk(KERN_INFO "Maple device detected: %s\n",
*p = '\0'; mdev->product_name);
else printk(KERN_INFO "Maple device: %s\n", mdev->product_licence);
break; }
function = be32_to_cpu(dev->devinfo.function); function = be32_to_cpu(mdev->devinfo.function);
if (function > 0x200) { if (function > 0x200) {
/* Do this silently - as not a real device */ /* Do this silently - as not a real device */
function = 0; function = 0;
dev->driver = &maple_dummy_driver; mdev->driver = &maple_dummy_driver;
sprintf(dev->dev.bus_id, "%d:0.port", dev->port); sprintf(mdev->dev.bus_id, "%d:0.port", mdev->port);
} else { } else {
printk(KERN_INFO if (realscan)
"Maple bus at (%d, %d): Connected function 0x%lX\n", printk(KERN_INFO
dev->port, dev->unit, function); "Maple bus at (%d, %d): Function 0x%lX\n",
mdev->port, mdev->unit, function);
matched = matched =
bus_for_each_drv(&maple_bus_type, NULL, dev, bus_for_each_drv(&maple_bus_type, NULL, mdev,
attach_matching_maple_driver); attach_matching_maple_driver);
if (matched == 0) { if (matched == 0) {
/* Driver does not exist yet */ /* Driver does not exist yet */
printk(KERN_INFO if (realscan)
"No maple driver found for this device\n"); printk(KERN_INFO
dev->driver = &maple_dummy_driver; "No maple driver found.\n");
mdev->driver = &maple_dummy_driver;
} }
sprintf(mdev->dev.bus_id, "%d:0%d.%lX", mdev->port,
sprintf(dev->dev.bus_id, "%d:0%d.%lX", dev->port, mdev->unit, function);
dev->unit, function);
} }
dev->function = function; mdev->function = function;
dev->dev.bus = &maple_bus_type; mdev->dev.release = &maple_release_device;
dev->dev.parent = &maple_bus; retval = device_register(&mdev->dev);
dev->dev.release = &maple_release_device;
retval = device_register(&dev->dev);
if (retval) { if (retval) {
printk(KERN_INFO printk(KERN_INFO
"Maple bus: Attempt to register device (%x, %x) failed.\n", "Maple bus: Attempt to register device"
dev->port, dev->unit); " (%x, %x) failed.\n",
maple_free_dev(dev); mdev->port, mdev->unit);
maple_free_dev(mdev);
mdev = NULL;
return;
} }
dev->registered = 1;
} }
/* /*
@ -519,7 +539,8 @@ static void maple_dma_handler(struct work_struct *work)
case MAPLE_RESPONSE_ALLINFO: case MAPLE_RESPONSE_ALLINFO:
printk(KERN_DEBUG printk(KERN_DEBUG
"Maple - extended device information not supported\n"); "Maple - extended device information"
" not supported\n");
break; break;
case MAPLE_RESPONSE_OK: case MAPLE_RESPONSE_OK:
@ -555,26 +576,16 @@ static irqreturn_t maplebus_vblank_interrupt(int irq, void *dev_id)
return IRQ_HANDLED; return IRQ_HANDLED;
} }
static struct irqaction maple_dma_irq = {
.name = "maple bus DMA handler",
.handler = maplebus_dma_interrupt,
.flags = IRQF_SHARED,
};
static struct irqaction maple_vblank_irq = {
.name = "maple bus VBLANK handler",
.handler = maplebus_vblank_interrupt,
.flags = IRQF_SHARED,
};
static int maple_set_dma_interrupt_handler(void) static int maple_set_dma_interrupt_handler(void)
{ {
return setup_irq(HW_EVENT_MAPLE_DMA, &maple_dma_irq); return request_irq(HW_EVENT_MAPLE_DMA, maplebus_dma_interrupt,
IRQF_SHARED, "maple bus DMA", &maple_dummy_driver);
} }
static int maple_set_vblank_interrupt_handler(void) static int maple_set_vblank_interrupt_handler(void)
{ {
return setup_irq(HW_EVENT_VSYNC, &maple_vblank_irq); return request_irq(HW_EVENT_VSYNC, maplebus_vblank_interrupt,
IRQF_SHARED, "maple bus VBLANK", &maple_dummy_driver);
} }
static int maple_get_dma_buffer(void) static int maple_get_dma_buffer(void)
@ -618,7 +629,7 @@ static struct maple_driver maple_dummy_driver = {
.drv = { .drv = {
.name = "maple_dummy_driver", .name = "maple_dummy_driver",
.bus = &maple_bus_type, .bus = &maple_bus_type,
}, },
}; };
struct bus_type maple_bus_type = { struct bus_type maple_bus_type = {
@ -626,7 +637,6 @@ struct bus_type maple_bus_type = {
.match = match_maple_bus_driver, .match = match_maple_bus_driver,
.uevent = maple_bus_uevent, .uevent = maple_bus_uevent,
}; };
EXPORT_SYMBOL_GPL(maple_bus_type); EXPORT_SYMBOL_GPL(maple_bus_type);
static struct device maple_bus = { static struct device maple_bus = {
@ -678,7 +688,7 @@ static int __init maple_bus_init(void)
maple_queue_cache = maple_queue_cache =
kmem_cache_create("maple_queue_cache", 0x400, 0, kmem_cache_create("maple_queue_cache", 0x400, 0,
SLAB_HWCACHE_ALIGN, NULL); SLAB_POISON|SLAB_HWCACHE_ALIGN, NULL);
if (!maple_queue_cache) if (!maple_queue_cache)
goto cleanup_bothirqs; goto cleanup_bothirqs;
@ -691,50 +701,48 @@ static int __init maple_bus_init(void)
maple_free_dev(mdev[i]); maple_free_dev(mdev[i]);
goto cleanup_cache; goto cleanup_cache;
} }
mdev[i]->registered = 0;
mdev[i]->mq->command = MAPLE_COMMAND_DEVINFO; mdev[i]->mq->command = MAPLE_COMMAND_DEVINFO;
mdev[i]->mq->length = 0; mdev[i]->mq->length = 0;
maple_attach_driver(mdev[i]);
maple_add_packet(mdev[i]->mq); maple_add_packet(mdev[i]->mq);
/* delay aids hardware detection */
udelay(20);
subdevice_map[i] = 0; subdevice_map[i] = 0;
} }
realscan = 1;
/* setup maplebus hardware */ /* setup maplebus hardware */
maplebus_dma_reset(); maplebus_dma_reset();
/* initial detection */ /* initial detection */
maple_send(); maple_send();
maple_pnp_time = jiffies; maple_pnp_time = jiffies;
printk(KERN_INFO "Maple bus core now registered.\n"); printk(KERN_INFO "Maple bus core now registered.\n");
return 0; return 0;
cleanup_cache: cleanup_cache:
kmem_cache_destroy(maple_queue_cache); kmem_cache_destroy(maple_queue_cache);
cleanup_bothirqs: cleanup_bothirqs:
free_irq(HW_EVENT_VSYNC, 0); free_irq(HW_EVENT_VSYNC, 0);
cleanup_irq: cleanup_irq:
free_irq(HW_EVENT_MAPLE_DMA, 0); free_irq(HW_EVENT_MAPLE_DMA, 0);
cleanup_dma: cleanup_dma:
free_pages((unsigned long) maple_sendbuf, MAPLE_DMA_PAGES); free_pages((unsigned long) maple_sendbuf, MAPLE_DMA_PAGES);
cleanup_basic: cleanup_basic:
driver_unregister(&maple_dummy_driver.drv); driver_unregister(&maple_dummy_driver.drv);
cleanup_bus: cleanup_bus:
bus_unregister(&maple_bus_type); bus_unregister(&maple_bus_type);
cleanup_device: cleanup_device:
device_unregister(&maple_bus); device_unregister(&maple_bus);
cleanup: cleanup:
printk(KERN_INFO "Maple bus registration failed\n"); printk(KERN_INFO "Maple bus registration failed\n");
return retval; return retval;
} }
/* Push init to later to ensure hardware gets detected */
subsys_initcall(maple_bus_init); fs_initcall(maple_bus_init);