Couple of sysfs patches
Quoted 10 lines · Russell King
Quoted 9 lines · Dmitry Torokhov
Quoted 7 lines · Russell King
As this currently stands, you have no chance to add resources to the platform device before it's made available to the driver. It's likely that any attached resources will have the same lifetime as the device itself, so it makes sense to allocate them together with the platform device.
Quoted 7 lines
Quoted 3 lines
Are you suggesting adding pointer to resources as a 3rd argument and automotically release it for the user? It probably could be done but users will be tempted to use static module data and bad things would happen.
Please read my second sentence again. It implies a copy of the resources is kept with the platform device, so both have the same lifetime.
Ok, so function pointer to allocate resources and associate with the device? You can't just allocate memory for resources structure, you need to populate it with data if you want it to be used by a driver immediately after registration... And have actually release all resources, not only memory? It is getting beyond the "*_simple" approach though.
Or do I still misunderstand you?
--
Dmitry
Quoted 19 lines · Dmitry Torokhov
Quoted 10 lines · Russell King
Quoted 9 lines · Dmitry Torokhov
Quoted 7 lines · Russell King
As this currently stands, you have no chance to add resources to the platform device before it's made available to the driver. It's likely that any attached resources will have the same lifetime as the device itself, so it makes sense to allocate them together with the platform device.
Quoted 7 lines
Quoted 3 lines
Are you suggesting adding pointer to resources as a 3rd argument and automotically release it for the user? It probably could be done but users will be tempted to use static module data and bad things would happen.
Please read my second sentence again. It implies a copy of the resources is kept with the platform device, so both have the same lifetime.
Quoted 8 lines
Ok, so function pointer to allocate resources and associate with the device? You can't just allocate memory for resources structure, you need to populate it with data if you want it to be used by a driver immediately after registration... And have actually release all resources, not only memory? It is getting beyond the "
*_simple" approach though.Or do I still misunderstand you?
Something like:
struct platform_object {
struct platform_device pdev;
struct resource resources[0];
};struct platform_device *platform_device_register_simple(char *name, unsigned int id, struct resource *res, int num)
{
struct platform_object *pobj;
int retval;
pobj = kmalloc(sizeof(struct platform_object) + sizeof(struct resource) * num, GFP_KERNEL);
if (!pobj) {
retval = -ENOMEM;
goto error;
}
memset(pobj, 0, sizeof(*pdev));
pobj->pdev.name = name;
pobj->pdev.id = id;
pobj->pdev.dev.release = platform_device_release_simple;
if (num) {
memcpy(pobj->resources, res, sizeof(struct resource) * num);
pobj->pdev.resource = pobj->resources;
pobj->pdev.num_resources = num;
}ARM is going very much down this route, and we're also going to need these things dynamically allocated as you do, but with the resource stuff. So rather than putting in something which only satisfies your problem, and then adding yet more interfaces to cover the extra resources, we should be thinking about something sane from the beginning.
Now that I can see the platform device interfaces multipling like rabbits, (to GregKH) I think that the patch I submitted for platform_add_device suffers from this problem as well, and I should've thrown that code into platform_register_device itself.
Greg - comments? Would you like a new patch which does that, or do you think that's too risky?
Signature · 4 lines
Russell King
Linux kernel 2.6 ARM Linux - http://www.arm.linux.org.uk/
maintainer of: 2.6 PCMCIA - http://pcmcia.arm.linux.org.uk/
2.6 Serial coreQuoted 8 lines · Russell King
Now that I can see the platform device interfaces multipling like rabbits, (to GregKH) I think that the patch I submitted for
platform_add_devicesuffers from this problem as well, and I should've thrown that code intoplatform_register_deviceitself.Greg - comments? Would you like a new patch which does that, or do you think that's too risky?
Hm, I don't think it's too risky. Make up a patch and let's see how it looks.
I'm just worried that this "simple" interface really isn't so simple, as it's almost just as much work to manage it as a normal platform device.
thanks,
greg k-h
Quoted 7 lines · Greg KH
Quoted 6 lines · Russell King
Now that I can see the platform device interfaces multipling like rabbits, (to GregKH) I think that the patch I submitted for
platform_add_devicesuffers from this problem as well, and I should've thrown that code intoplatform_register_deviceitself.
Quoted 8 lines
Quoted 2 lines
Greg - comments? Would you like a new patch which does that, or do you think that's too risky?
Hm, I don't think it's too risky. Make up a patch and let's see how it looks.
I'm just worried that this "simple" interface really isn't so simple, as it's almost just as much work to manage it as a normal platform device.
Ok, here's a patch so you can see what I'm suggesting above. This is on top of the previous patch I sent. Merely discards one over-eager rabbit [1] and moves the code into platform_device_register().
[1]: No animals were harmed in the creation of this patch.
drivers/base/platform.c +43 -40
@@ -55,51 +55,24 @@ }/** -* platform_add_device - add one platform device -* @dev: platform device -* -* Adds one platform device, claiming the memory resources -*/ -int platform_add_device (struct platform_device *dev) -{ -int i; - -for (i =0 ; i < dev->num_resources; i++) { -struct resource *p, *r = &dev->resource[i]; - - r->name = dev->dev.bus_id; - - p =NULL ; -if (r->flags &IORESOURCE_MEM ) - p = &iomem_resource; -else if (r->flags &IORESOURCE_IO ) - p = &ioport_resource; - -if (p &&request_resource (p, r)) { -printk (KERN_ERR -"%s%d: failed to claim resource %d\n" , - dev->name, dev->id, i); -break ; - } - } -if (i == dev->num_resources) -platform_device_register (dev); -return 0 ; -} - -/** * platform_add_devices - add a numbers of platform devices * @devs: array of platform devices to add * @num: number of platform devices in array */ int platform_add_devices (struct platform_device **devs,int num) { -int i; +int i, ret =0 ; -for (i =0 ; i < num; i++) -platform_add_device (devs[i]); +for (i =0 ; i < num; i++) { + ret =platform_device_register (devs[i]); +if (ret) { +while (--i >0 ) +platform_device_unregister (devs[i]); +break ; + } + } -return 0 ; +return ret; }/**
@@ -109,6 +82,8 @@*/ int platform_device_register (struct platform_device * pdev) { +int i, ret =0 ; +if (!pdev)return -EINVAL ;
@@ -119,9 +94,37 @@snprintf (pdev->dev.bus_id,BUS_ID_SIZE ,"%s%u" ,pdev->name,pdev->id); -pr_debug ("Registering platform device '%s'. Parent at %s\n" , - pdev->dev.bus_id,pdev->dev.parent->bus_id); -return device_register (&pdev->dev); +for (i =0 ; i < pdev->num_resources; i++) { +struct resource *p, *r = &pdev->resource[i]; + + r->name = pdev->dev.bus_id; + + p =NULL ; +if (r->flags &IORESOURCE_MEM ) + p = &iomem_resource; +else if (r->flags &IORESOURCE_IO ) + p = &ioport_resource; + +if (p &&request_resource (p, r)) { +printk (KERN_ERR +"%s%d: failed to claim resource %d\n" , + pdev->name, pdev->id, i); + +while (--i >0 ) +release_resource (&pdev->resource[i]); + ret = -EBUSY ; +break ; + } + } + +if (ret ==0 ) { +pr_debug ("Registering platform device '%s'. Parent at %s\n" , + pdev->dev.bus_id,pdev->dev.parent->bus_id); + + ret =device_register (&pdev->dev); + } + +return ret; }void platform_device_unregister (struct platform_device * pdev)
include/linux/device.h +0 -1
@@ -392,7 +392,6 @@extern struct resource *platform_get_resource (struct platform_device *,unsigned int ,unsigned int );extern int platform_get_irq (struct platform_device *,unsigned int ); -extern int platform_add_device (struct platform_device *);extern int platform_add_devices (struct platform_device **,int );/* drivers/base/power.c */
Signature · 4 lines
Russell King
Linux kernel 2.6 ARM Linux - http://www.arm.linux.org.uk/
maintainer of: 2.6 PCMCIA - http://pcmcia.arm.linux.org.uk/
2.6 Serial coreQuoted 14 lines · Russell King
Quoted 13 lines · Greg KH
Quoted 8 lines · Russell King
Now that I can see the platform device interfaces multipling like rabbits, (to GregKH) I think that the patch I submitted for
platform_add_devicesuffers from this problem as well, and I should've thrown that code intoplatform_register_deviceitself.Greg - comments? Would you like a new patch which does that, or do you think that's too risky?
Hm, I don't think it's too risky. Make up a patch and let's see how it looks.
Quoted 8 lines
Quoted 2 lines
I'm just worried that this "simple" interface really isn't so simple, as it's almost just as much work to manage it as a normal platform device.
Ok, here's a patch so you can see what I'm suggesting above. This is on top of the previous patch I sent. Merely discards one over-eager rabbit [1] and moves the code into
platform_device_register().[1]: No animals were harmed in the creation of this patch.
And for added good behaviour, particularly when things go wrong.
drivers/base/platform.c +42 -38
@@ -55,51 +55,24 @@ }/** -* platform_add_device - add one platform device -* @dev: platform device -* -* Adds one platform device, claiming the memory resources -*/ -int platform_add_device (struct platform_device *dev) -{ -int i; - -for (i =0 ; i < dev->num_resources; i++) { -struct resource *p, *r = &dev->resource[i]; - - r->name = dev->dev.bus_id; - - p =NULL ; -if (r->flags &IORESOURCE_MEM ) - p = &iomem_resource; -else if (r->flags &IORESOURCE_IO ) - p = &ioport_resource; - -if (p &&request_resource (p, r)) { -printk (KERN_ERR -"%s%d: failed to claim resource %d\n" , - dev->name, dev->id, i); -break ; - } - } -if (i == dev->num_resources) -platform_device_register (dev); -return 0 ; -} - -/** * platform_add_devices - add a numbers of platform devices * @devs: array of platform devices to add * @num: number of platform devices in array */ int platform_add_devices (struct platform_device **devs,int num) { -int i; +int i, ret =0 ; -for (i =0 ; i < num; i++) -platform_add_device (devs[i]); +for (i =0 ; i < num; i++) { + ret =platform_device_register (devs[i]); +if (ret) { +while (--i >=0 ) +platform_device_unregister (devs[i]); +break ; + } + } -return 0 ; +return ret; }/**
@@ -109,6 +82,8 @@*/ int platform_device_register (struct platform_device * pdev) { +int i, ret =0 ; +if (!pdev)return -EINVAL ;
@@ -119,9 +94,38 @@snprintf (pdev->dev.bus_id,BUS_ID_SIZE ,"%s%u" ,pdev->name,pdev->id); +for (i =0 ; i < pdev->num_resources; i++) { +struct resource *p, *r = &pdev->resource[i]; + + r->name = pdev->dev.bus_id; + + p =NULL ; +if (r->flags &IORESOURCE_MEM ) + p = &iomem_resource; +else if (r->flags &IORESOURCE_IO ) + p = &ioport_resource; + +if (p &&request_resource (p, r)) { +printk (KERN_ERR +"%s: failed to claim resource %d\n" , + pdev->dev.bus_id, i); + ret = -EBUSY ; + goto failed; + } + } +pr_debug ("Registering platform device '%s'. Parent at %s\n" , pdev->dev.bus_id,pdev->dev.parent->bus_id); -return device_register (&pdev->dev); + + ret =device_register (&pdev->dev); +if (ret ==0 ) +return ret; + + failed: +while (--i >=0 ) +if (pdev->resource[i].flags & (IORESOURCE_MEM |IORESOURCE_IO )) +release_resource (&pdev->resource[i]); +return ret; }void platform_device_unregister (struct platform_device * pdev)
include/linux/device.h +0 -1
@@ -392,7 +392,6 @@extern struct resource *platform_get_resource (struct platform_device *,unsigned int ,unsigned int );extern int platform_get_irq (struct platform_device *,unsigned int ); -extern int platform_add_device (struct platform_device *);extern int platform_add_devices (struct platform_device **,int );/* drivers/base/power.c */
Signature · 4 lines
Russell King
Linux kernel 2.6 ARM Linux - http://www.arm.linux.org.uk/
maintainer of: 2.6 PCMCIA - http://pcmcia.arm.linux.org.uk/
2.6 Serial coreQuoted 37 lines · Russell King
Quoted 14 lines · Russell King
Quoted 13 lines · Greg KH
Quoted 8 lines · Russell King
Now that I can see the platform device interfaces multipling like rabbits, (to GregKH) I think that the patch I submitted for
platform_add_devicesuffers from this problem as well, and I should've thrown that code intoplatform_register_deviceitself.Greg - comments? Would you like a new patch which does that, or do you think that's too risky?
Hm, I don't think it's too risky. Make up a patch and let's see how it looks.
Quoted 8 lines
Quoted 2 lines
I'm just worried that this "simple" interface really isn't so simple, as it's almost just as much work to manage it as a normal platform device.
Ok, here's a patch so you can see what I'm suggesting above. This is on top of the previous patch I sent. Merely discards one over-eager rabbit [1] and moves the code into
platform_device_register().[1]: No animals were harmed in the creation of this patch.
And for added good behaviour, particularly when things go wrong.
drivers/base/platform.c +10
-0+for (i =0 ; i < pdev->num_resources; i++) { +struct resource *p, *r = &pdev->resource[i]; + + r->name = pdev->dev.bus_id; + + p =NULL ; +if (r->flags &IORESOURCE_MEM ) + p = &iomem_resource; +else if (r->flags &IORESOURCE_IO ) + p = &ioport_resource;
drivers/base/platform.c line 116
Quoted 10 lines
drivers/base/platform.c +10
-0+ +if (p &&request_resource (p, r)) { +printk (KERN_ERR +"%s: failed to claim resource %d\n" , + pdev->dev.bus_id, i); + ret = -EBUSY ; + goto failed; + } + } +
What about freeing the resources? Can it be put in platform_device_unregister or is it release handler task? I'd put it in unregister because when I call unregister I expect device be half-dead and release as much resources as it can.
Signature · 1 line
Dmitry
Quoted 4 lines · Dmitry Torokhov
What about freeing the resources? Can it be put in
platform_device_unregisteror is it release handler task? I'd put it in unregister because when I call unregister I expect device be half-dead and release as much resources as it can.
Greg,
Here's the updated patch - to be applied on top of the platform_get_resource() patch sent previously.
drivers/base/platform.c +52 -39
@@ -55,51 +55,24 @@ int platform_get_irq(struct platform_dev }/** -* platform_add_device - add one platform device -* @dev: platform device -* -* Adds one platform device, claiming the memory resources -*/ -int platform_add_device (struct platform_device *dev) -{ -int i; - -for (i =0 ; i < dev->num_resources; i++) { -struct resource *p, *r = &dev->resource[i]; - - r->name = dev->dev.bus_id; - - p =NULL ; -if (r->flags &IORESOURCE_MEM ) - p = &iomem_resource; -else if (r->flags &IORESOURCE_IO ) - p = &ioport_resource; - -if (p &&request_resource (p, r)) { -printk (KERN_ERR -"%s%d: failed to claim resource %d\n" , - dev->name, dev->id, i); -break ; - } - } -if (i == dev->num_resources) -platform_device_register (dev); -return 0 ; -} - -/** * platform_add_devices - add a numbers of platform devices * @devs: array of platform devices to add * @num: number of platform devices in array */ int platform_add_devices (struct platform_device **devs,int num) { -int i; +int i, ret =0 ; -for (i =0 ; i < num; i++) -platform_add_device (devs[i]); +for (i =0 ; i < num; i++) { + ret =platform_device_register (devs[i]); +if (ret) { +while (--i >=0 ) +platform_device_unregister (devs[i]); +break ; + } + } -return 0 ; +return ret; }/**
@@ -109,6 +82,8 @@ int platform_add_devices(struct platform*/ int platform_device_register (struct platform_device * pdev) { +int i, ret =0 ; +if (!pdev)return -EINVAL ;
@@ -119,15 +94,53 @@ int platform_device_register(struct platsnprintf (pdev->dev.bus_id,BUS_ID_SIZE ,"%s%u" ,pdev->name,pdev->id); +for (i =0 ; i < pdev->num_resources; i++) { +struct resource *p, *r = &pdev->resource[i]; + + r->name = pdev->dev.bus_id; + + p =NULL ; +if (r->flags &IORESOURCE_MEM ) + p = &iomem_resource; +else if (r->flags &IORESOURCE_IO ) + p = &ioport_resource; + +if (p &&request_resource (p, r)) { +printk (KERN_ERR +"%s: failed to claim resource %d\n" , + pdev->dev.bus_id, i); + ret = -EBUSY ; + goto failed; + } + } +pr_debug ("Registering platform device '%s'. Parent at %s\n" , pdev->dev.bus_id,pdev->dev.parent->bus_id); -return device_register (&pdev->dev); + + ret =device_register (&pdev->dev); +if (ret ==0 ) +return ret; + + failed: +while (--i >=0 ) +if (pdev->resource[i].flags & (IORESOURCE_MEM |IORESOURCE_IO )) +release_resource (&pdev->resource[i]); +return ret; }void platform_device_unregister (struct platform_device * pdev) { -if (pdev) +int i; + +if (pdev) {device_unregister (&pdev->dev); + +for (i =0 ; i < pdev->num_resources; i++) { +struct resource *r = &pdev->resource[i]; +if (r->flags & (IORESOURCE_MEM |IORESOURCE_IO )) +release_resource (r); + } + } }
include/linux/device.h +0 -1
@@ -392,7 +392,6 @@ extern struct device platform_bus;extern struct resource *platform_get_resource (struct platform_device *,unsigned int ,unsigned int );extern int platform_get_irq (struct platform_device *,unsigned int ); -extern int platform_add_device (struct platform_device *);extern int platform_add_devices (struct platform_device **,int );/* drivers/base/power.c */
Signature · 4 lines
Russell King
Linux kernel 2.6 ARM Linux - http://www.arm.linux.org.uk/
maintainer of: 2.6 PCMCIA - http://pcmcia.arm.linux.org.uk/
2.6 Serial coreQuoted 10 lines · Russell King
Quoted 4 lines · Dmitry Torokhov
What about freeing the resources? Can it be put in
platform_device_unregisteror is it release handler task? I'd put it in unregister because when I call unregister I expect device be half-dead and release as much resources as it can.Greg,
Here's the updated patch - to be applied on top of the
platform_get_resource()patch sent previously.
Looks good, thanks I've applied this and will send it on.
greg k-h