return -EINVAL;
}
-int comedi_device_detach(comedi_device *dev)
+static void cleanup_device_allocations(comedi_device *dev)
{
int i;
comedi_subdevice *s;
+ if(dev->subdevices)
+ {
+ for(i = 0; i < dev->n_subdevices; i++)
+ {
+ s = dev->subdevices + i;
+ if(s->async)
+ {
+ comedi_buf_alloc(dev, s, 0);
+ if(s->buf_change) s->buf_change(dev, s, 0);
+ kfree(s->async);
+ }
+ }
+ kfree(dev->subdevices);
+ dev->subdevices = NULL;
+ }
+ if(dev->private)
+ {
+ kfree(dev->private);
+ dev->private = NULL;
+ }
+}
+
+int comedi_device_detach(comedi_device *dev)
+{
if(!dev->attached)
return 0;
dev->attached=0;
- for(i=0;i<dev->n_subdevices;i++){
- s=dev->subdevices+i;
- if(s->async){
- comedi_buf_alloc(dev, s, 0);
- if(s->buf_change) s->buf_change(dev,s,0);
- kfree(s->async);
- }
- }
-
if(dev->driver){
dev->driver->detach(dev);
}else{
printk("BUG: dev->driver=NULL in comedi_device_detach()\n");
}
- if(dev->subdevices)kfree(dev->subdevices);
- if(dev->private)kfree(dev->private);
-
+ cleanup_device_allocations(dev);
return 0;
}
ret=driv->attach(dev,it);
if(ret<0){
driv->detach(dev);
- if(dev->subdevices)kfree(dev->subdevices);
- if(dev->private)kfree(dev->private);
+ cleanup_device_allocations(dev);
module_put( driv->module );
return ret;
}
if(ret < 0)
{
driv->detach(dev);
- if(dev->subdevices)kfree(dev->subdevices);
- if(dev->private)kfree(dev->private);
-
+ cleanup_device_allocations(dev);
return ret;
}