Merge tag 'r8169-20060920-00' of git://electric-eye.fr.zoreil.com/home/romieu/linux...
[pandora-kernel.git] / drivers / media / video / pvrusb2 / pvrusb2-sysfs.c
index 4254062..d1dda5c 100644 (file)
@@ -44,17 +44,22 @@ struct pvr2_sysfs {
        struct kobj_type ktype;
        struct class_device_attribute attr_v4l_minor_number;
        struct class_device_attribute attr_unit_number;
+       int v4l_minor_number_created_ok;
+       int unit_number_created_ok;
 };
 
 #ifdef CONFIG_VIDEO_PVRUSB2_DEBUGIFC
 struct pvr2_sysfs_debugifc {
        struct class_device_attribute attr_debugcmd;
        struct class_device_attribute attr_debuginfo;
+       int debugcmd_created_ok;
+       int debuginfo_created_ok;
 };
 #endif /* CONFIG_VIDEO_PVRUSB2_DEBUGIFC */
 
 struct pvr2_sysfs_ctl_item {
        struct class_device_attribute attr_name;
+       struct class_device_attribute attr_type;
        struct class_device_attribute attr_min;
        struct class_device_attribute attr_max;
        struct class_device_attribute attr_enum;
@@ -64,8 +69,9 @@ struct pvr2_sysfs_ctl_item {
        struct pvr2_ctrl *cptr;
        struct pvr2_sysfs *chptr;
        struct pvr2_sysfs_ctl_item *item_next;
-       struct attribute *attr_gen[6];
+       struct attribute *attr_gen[7];
        struct attribute_group grp;
+       int created_ok;
        char name[80];
 };
 
@@ -92,6 +98,33 @@ static ssize_t show_name(int id,struct class_device *class_dev,char *buf)
        return scnprintf(buf,PAGE_SIZE,"%s\n",name);
 }
 
+static ssize_t show_type(int id,struct class_device *class_dev,char *buf)
+{
+       struct pvr2_ctrl *cptr;
+       struct pvr2_sysfs *sfp;
+       const char *name;
+       enum pvr2_ctl_type tp;
+
+       sfp = (struct pvr2_sysfs *)class_dev->class_data;
+       if (!sfp) return -EINVAL;
+       cptr = pvr2_hdw_get_ctrl_by_index(sfp->channel.hdw,id);
+       if (!cptr) return -EINVAL;
+
+       tp = pvr2_ctrl_get_type(cptr);
+       switch (tp) {
+       case pvr2_ctl_int: name = "integer"; break;
+       case pvr2_ctl_enum: name = "enum"; break;
+       case pvr2_ctl_bitmask: name = "bitmask"; break;
+       case pvr2_ctl_bool: name = "boolean"; break;
+       default: name = "?"; break;
+       }
+       pvr2_sysfs_trace("pvr2_sysfs(%p) show_type(cid=%d) is %s",sfp,id,name);
+
+       if (!name) return -EINVAL;
+
+       return scnprintf(buf,PAGE_SIZE,"%s\n",name);
+}
+
 static ssize_t show_min(int id,struct class_device *class_dev,char *buf)
 {
        struct pvr2_ctrl *cptr;
@@ -289,6 +322,7 @@ static ssize_t sf_name##_##ctl_id(struct class_device *class_dev,const char *buf
 
 #define CREATE_BATCH(ctl_id) \
 CREATE_SHOW_INSTANCE(show_name,ctl_id) \
+CREATE_SHOW_INSTANCE(show_type,ctl_id) \
 CREATE_SHOW_INSTANCE(show_min,ctl_id) \
 CREATE_SHOW_INSTANCE(show_max,ctl_id) \
 CREATE_SHOW_INSTANCE(show_val_norm,ctl_id) \
@@ -361,6 +395,7 @@ CREATE_BATCH(59)
 
 struct pvr2_sysfs_func_set {
        ssize_t (*show_name)(struct class_device *,char *);
+       ssize_t (*show_type)(struct class_device *,char *);
        ssize_t (*show_min)(struct class_device *,char *);
        ssize_t (*show_max)(struct class_device *,char *);
        ssize_t (*show_enum)(struct class_device *,char *);
@@ -376,6 +411,7 @@ struct pvr2_sysfs_func_set {
 #define INIT_BATCH(ctl_id) \
 [ctl_id] = { \
     .show_name = show_name_##ctl_id, \
+    .show_type = show_type_##ctl_id, \
     .show_min = show_min_##ctl_id, \
     .show_max = show_max_##ctl_id, \
     .show_enum = show_enum_##ctl_id, \
@@ -456,6 +492,7 @@ static void pvr2_sysfs_add_control(struct pvr2_sysfs *sfp,int ctl_id)
        struct pvr2_sysfs_func_set *fp;
        struct pvr2_ctrl *cptr;
        unsigned int cnt,acnt;
+       int ret;
 
        if ((ctl_id < 0) || (ctl_id >= (sizeof(funcs)/sizeof(funcs[0])))) {
                return;
@@ -473,7 +510,7 @@ static void pvr2_sysfs_add_control(struct pvr2_sysfs *sfp,int ctl_id)
        cip->cptr = cptr;
 
        cip->chptr = sfp;
-       cip->item_next = 0;
+       cip->item_next = NULL;
        if (sfp->item_last) {
                sfp->item_last->item_next = cip;
        } else {
@@ -486,6 +523,11 @@ static void pvr2_sysfs_add_control(struct pvr2_sysfs *sfp,int ctl_id)
        cip->attr_name.attr.mode = S_IRUGO;
        cip->attr_name.show = fp->show_name;
 
+       cip->attr_type.attr.owner = THIS_MODULE;
+       cip->attr_type.attr.name = "type";
+       cip->attr_type.attr.mode = S_IRUGO;
+       cip->attr_type.show = fp->show_type;
+
        cip->attr_min.attr.owner = THIS_MODULE;
        cip->attr_min.attr.name = "min_val";
        cip->attr_min.attr.mode = S_IRUGO;
@@ -521,6 +563,7 @@ static void pvr2_sysfs_add_control(struct pvr2_sysfs *sfp,int ctl_id)
 
        acnt = 0;
        cip->attr_gen[acnt++] = &cip->attr_name.attr;
+       cip->attr_gen[acnt++] = &cip->attr_type.attr;
        cip->attr_gen[acnt++] = &cip->attr_val.attr;
        cip->attr_val.show = fp->show_val_norm;
        cip->attr_val.store = fp->store_val_norm;
@@ -552,7 +595,13 @@ static void pvr2_sysfs_add_control(struct pvr2_sysfs *sfp,int ctl_id)
        cip->grp.name = cip->name;
        cip->grp.attrs = cip->attr_gen;
 
-       sysfs_create_group(&sfp->class_dev->kobj,&cip->grp);
+       ret = sysfs_create_group(&sfp->class_dev->kobj,&cip->grp);
+       if (ret) {
+               printk(KERN_WARNING "%s: sysfs_create_group error: %d\n",
+                      __FUNCTION__, ret);
+               return;
+       }
+       cip->created_ok = !0;
 }
 
 #ifdef CONFIG_VIDEO_PVRUSB2_DEBUGIFC
@@ -563,6 +612,8 @@ static ssize_t debugcmd_store(struct class_device *,const char *,size_t count);
 static void pvr2_sysfs_add_debugifc(struct pvr2_sysfs *sfp)
 {
        struct pvr2_sysfs_debugifc *dip;
+       int ret;
+
        dip = kmalloc(sizeof(*dip),GFP_KERNEL);
        if (!dip) return;
        memset(dip,0,sizeof(*dip));
@@ -576,19 +627,36 @@ static void pvr2_sysfs_add_debugifc(struct pvr2_sysfs *sfp)
        dip->attr_debuginfo.attr.mode = S_IRUGO;
        dip->attr_debuginfo.show = debuginfo_show;
        sfp->debugifc = dip;
-       class_device_create_file(sfp->class_dev,&dip->attr_debugcmd);
-       class_device_create_file(sfp->class_dev,&dip->attr_debuginfo);
+       ret = class_device_create_file(sfp->class_dev,&dip->attr_debugcmd);
+       if (ret < 0) {
+               printk(KERN_WARNING "%s: class_device_create_file error: %d\n",
+                      __FUNCTION__, ret);
+       } else {
+               dip->debugcmd_created_ok = !0;
+       }
+       ret = class_device_create_file(sfp->class_dev,&dip->attr_debuginfo);
+       if (ret < 0) {
+               printk(KERN_WARNING "%s: class_device_create_file error: %d\n",
+                      __FUNCTION__, ret);
+       } else {
+               dip->debuginfo_created_ok = !0;
+       }
 }
 
 
 static void pvr2_sysfs_tear_down_debugifc(struct pvr2_sysfs *sfp)
 {
        if (!sfp->debugifc) return;
-       class_device_remove_file(sfp->class_dev,
-                                &sfp->debugifc->attr_debuginfo);
-       class_device_remove_file(sfp->class_dev,&sfp->debugifc->attr_debugcmd);
+       if (sfp->debugifc->debuginfo_created_ok) {
+               class_device_remove_file(sfp->class_dev,
+                                        &sfp->debugifc->attr_debuginfo);
+       }
+       if (sfp->debugifc->debugcmd_created_ok) {
+               class_device_remove_file(sfp->class_dev,
+                                        &sfp->debugifc->attr_debugcmd);
+       }
        kfree(sfp->debugifc);
-       sfp->debugifc = 0;
+       sfp->debugifc = NULL;
 }
 #endif /* CONFIG_VIDEO_PVRUSB2_DEBUGIFC */
 
@@ -608,7 +676,9 @@ static void pvr2_sysfs_tear_down_controls(struct pvr2_sysfs *sfp)
        struct pvr2_sysfs_ctl_item *cip1,*cip2;
        for (cip1 = sfp->item_first; cip1; cip1 = cip2) {
                cip2 = cip1->item_next;
-               sysfs_remove_group(&sfp->class_dev->kobj,&cip1->grp);
+               if (cip1->created_ok) {
+                       sysfs_remove_group(&sfp->class_dev->kobj,&cip1->grp);
+               }
                pvr2_sysfs_trace("Destroying pvr2_sysfs_ctl_item id=%p",cip1);
                kfree(cip1);
        }
@@ -638,12 +708,18 @@ static void class_dev_destroy(struct pvr2_sysfs *sfp)
        pvr2_sysfs_tear_down_debugifc(sfp);
 #endif /* CONFIG_VIDEO_PVRUSB2_DEBUGIFC */
        pvr2_sysfs_tear_down_controls(sfp);
-       class_device_remove_file(sfp->class_dev,&sfp->attr_v4l_minor_number);
-       class_device_remove_file(sfp->class_dev,&sfp->attr_unit_number);
+       if (sfp->v4l_minor_number_created_ok) {
+               class_device_remove_file(sfp->class_dev,
+                                        &sfp->attr_v4l_minor_number);
+       }
+       if (sfp->unit_number_created_ok) {
+               class_device_remove_file(sfp->class_dev,
+                                        &sfp->attr_unit_number);
+       }
        pvr2_sysfs_trace("Destroying class_dev id=%p",sfp->class_dev);
-       sfp->class_dev->class_data = 0;
+       sfp->class_dev->class_data = NULL;
        class_device_unregister(sfp->class_dev);
-       sfp->class_dev = 0;
+       sfp->class_dev = NULL;
 }
 
 
@@ -672,6 +748,8 @@ static void class_dev_create(struct pvr2_sysfs *sfp,
 {
        struct usb_device *usb_dev;
        struct class_device *class_dev;
+       int ret;
+
        usb_dev = pvr2_hdw_get_dev(sfp->channel.hdw);
        if (!usb_dev) return;
        class_dev = kmalloc(sizeof(*class_dev),GFP_KERNEL);
@@ -696,20 +774,40 @@ static void class_dev_create(struct pvr2_sysfs *sfp,
 
        sfp->class_dev = class_dev;
        class_dev->class_data = sfp;
-       class_device_register(class_dev);
+       ret = class_device_register(class_dev);
+       if (ret) {
+               printk(KERN_ERR "%s: class_device_register failed\n",
+                      __FUNCTION__);
+               kfree(class_dev);
+               return;
+       }
 
        sfp->attr_v4l_minor_number.attr.owner = THIS_MODULE;
        sfp->attr_v4l_minor_number.attr.name = "v4l_minor_number";
        sfp->attr_v4l_minor_number.attr.mode = S_IRUGO;
        sfp->attr_v4l_minor_number.show = v4l_minor_number_show;
-       sfp->attr_v4l_minor_number.store = 0;
-       class_device_create_file(sfp->class_dev,&sfp->attr_v4l_minor_number);
+       sfp->attr_v4l_minor_number.store = NULL;
+       ret = class_device_create_file(sfp->class_dev,
+                                      &sfp->attr_v4l_minor_number);
+       if (ret < 0) {
+               printk(KERN_WARNING "%s: class_device_create_file error: %d\n",
+                      __FUNCTION__, ret);
+       } else {
+               sfp->v4l_minor_number_created_ok = !0;
+       }
+
        sfp->attr_unit_number.attr.owner = THIS_MODULE;
        sfp->attr_unit_number.attr.name = "unit_number";
        sfp->attr_unit_number.attr.mode = S_IRUGO;
        sfp->attr_unit_number.show = unit_number_show;
-       sfp->attr_unit_number.store = 0;
-       class_device_create_file(sfp->class_dev,&sfp->attr_unit_number);
+       sfp->attr_unit_number.store = NULL;
+       ret = class_device_create_file(sfp->class_dev,&sfp->attr_unit_number);
+       if (ret < 0) {
+               printk(KERN_WARNING "%s: class_device_create_file error: %d\n",
+                      __FUNCTION__, ret);
+       } else {
+               sfp->unit_number_created_ok = !0;
+       }
 
        pvr2_sysfs_add_controls(sfp);
 #ifdef CONFIG_VIDEO_PVRUSB2_DEBUGIFC
@@ -769,7 +867,7 @@ struct pvr2_sysfs_class *pvr2_sysfs_class_create(void)
                pvr2_sysfs_trace(
                        "Registration failed for pvr2_sysfs_class id=%p",clp);
                kfree(clp);
-               clp = 0;
+               clp = NULL;
        }
        return clp;
 }