pvrusb2-sysfs.c
来自「trident tm5600的linux驱动」· C语言 代码 · 共 869 行 · 第 1/2 页
C
869 行
"device_create_file error: %d", ret); } else { dip->debugcmd_created_ok = !0; } ret = device_create_file(sfp->class_dev,&dip->attr_debuginfo); if (ret < 0) { pvr2_trace(PVR2_TRACE_ERROR_LEGS, "device_create_file error: %d", ret); } else { dip->debuginfo_created_ok = !0; }}static void pvr2_sysfs_tear_down_debugifc(struct pvr2_sysfs *sfp){ if (!sfp->debugifc) return; if (sfp->debugifc->debuginfo_created_ok) { device_remove_file(sfp->class_dev, &sfp->debugifc->attr_debuginfo); } if (sfp->debugifc->debugcmd_created_ok) { device_remove_file(sfp->class_dev, &sfp->debugifc->attr_debugcmd); } kfree(sfp->debugifc); sfp->debugifc = NULL;}#endif /* CONFIG_VIDEO_PVRUSB2_DEBUGIFC */static void pvr2_sysfs_add_controls(struct pvr2_sysfs *sfp){ unsigned int idx,cnt; cnt = pvr2_hdw_get_ctrl_count(sfp->channel.hdw); for (idx = 0; idx < cnt; idx++) { pvr2_sysfs_add_control(sfp,idx); }}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; 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); }}static void pvr2_sysfs_class_release(struct class *class){ struct pvr2_sysfs_class *clp; clp = container_of(class,struct pvr2_sysfs_class,class); pvr2_sysfs_trace("Destroying pvr2_sysfs_class id=%p",clp); kfree(clp);}#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)static void pvr2_sysfs_release(struct class_device *class_dev){ pvr2_sysfs_trace("Releasing class_dev id=%p",class_dev); kfree(class_dev);}#elsestatic void pvr2_sysfs_release(struct device *class_dev){ pvr2_sysfs_trace("Releasing class_dev id=%p",class_dev); kfree(class_dev);}#endifstatic void class_dev_destroy(struct pvr2_sysfs *sfp){ if (!sfp->class_dev) return;#ifdef CONFIG_VIDEO_PVRUSB2_DEBUGIFC pvr2_sysfs_tear_down_debugifc(sfp);#endif /* CONFIG_VIDEO_PVRUSB2_DEBUGIFC */ pvr2_sysfs_tear_down_controls(sfp); if (sfp->hdw_desc_created_ok) { device_remove_file(sfp->class_dev, &sfp->attr_hdw_desc); } if (sfp->hdw_name_created_ok) { device_remove_file(sfp->class_dev, &sfp->attr_hdw_name); } if (sfp->bus_info_created_ok) { device_remove_file(sfp->class_dev, &sfp->attr_bus_info); } if (sfp->v4l_minor_number_created_ok) { device_remove_file(sfp->class_dev, &sfp->attr_v4l_minor_number); } if (sfp->v4l_radio_minor_number_created_ok) { device_remove_file(sfp->class_dev, &sfp->attr_v4l_radio_minor_number); } if (sfp->unit_number_created_ok) { device_remove_file(sfp->class_dev, &sfp->attr_unit_number); } pvr2_sysfs_trace("Destroying class_dev id=%p",sfp->class_dev); sfp->class_dev->driver_data = NULL; device_unregister(sfp->class_dev); sfp->class_dev = NULL;}static ssize_t v4l_minor_number_show(struct device *class_dev, struct device_attribute *attr, char *buf){ struct pvr2_sysfs *sfp; sfp = (struct pvr2_sysfs *)class_dev->driver_data; if (!sfp) return -EINVAL; return scnprintf(buf,PAGE_SIZE,"%d\n", pvr2_hdw_v4l_get_minor_number(sfp->channel.hdw, pvr2_v4l_type_video));}static ssize_t bus_info_show(struct device *class_dev, struct device_attribute *attr, char *buf){ struct pvr2_sysfs *sfp; sfp = (struct pvr2_sysfs *)class_dev->driver_data; if (!sfp) return -EINVAL; return scnprintf(buf,PAGE_SIZE,"%s\n", pvr2_hdw_get_bus_info(sfp->channel.hdw));}static ssize_t hdw_name_show(struct device *class_dev, struct device_attribute *attr, char *buf){ struct pvr2_sysfs *sfp; sfp = (struct pvr2_sysfs *)class_dev->driver_data; if (!sfp) return -EINVAL; return scnprintf(buf,PAGE_SIZE,"%s\n", pvr2_hdw_get_type(sfp->channel.hdw));}static ssize_t hdw_desc_show(struct device *class_dev, struct device_attribute *attr, char *buf){ struct pvr2_sysfs *sfp; sfp = (struct pvr2_sysfs *)class_dev->driver_data; if (!sfp) return -EINVAL; return scnprintf(buf,PAGE_SIZE,"%s\n", pvr2_hdw_get_desc(sfp->channel.hdw));}static ssize_t v4l_radio_minor_number_show(struct device *class_dev, struct device_attribute *attr, char *buf){ struct pvr2_sysfs *sfp; sfp = (struct pvr2_sysfs *)class_dev->driver_data; if (!sfp) return -EINVAL; return scnprintf(buf,PAGE_SIZE,"%d\n", pvr2_hdw_v4l_get_minor_number(sfp->channel.hdw, pvr2_v4l_type_radio));}static ssize_t unit_number_show(struct device *class_dev, struct device_attribute *attr, char *buf){ struct pvr2_sysfs *sfp; sfp = (struct pvr2_sysfs *)class_dev->driver_data; if (!sfp) return -EINVAL; return scnprintf(buf,PAGE_SIZE,"%d\n", pvr2_hdw_get_unit_number(sfp->channel.hdw));}static void class_dev_create(struct pvr2_sysfs *sfp, struct pvr2_sysfs_class *class_ptr){ struct usb_device *usb_dev; struct device *class_dev; int ret; usb_dev = pvr2_hdw_get_dev(sfp->channel.hdw); if (!usb_dev) return; class_dev = kzalloc(sizeof(*class_dev),GFP_KERNEL); if (!class_dev) return; pvr2_sysfs_trace("Creating class_dev id=%p",class_dev); class_dev->class = &class_ptr->class; if (pvr2_hdw_get_sn(sfp->channel.hdw)) { snprintf(class_dev->bus_id, BUS_ID_SIZE, "sn-%lu", pvr2_hdw_get_sn(sfp->channel.hdw)); } else if (pvr2_hdw_get_unit_number(sfp->channel.hdw) >= 0) { snprintf(class_dev->bus_id, BUS_ID_SIZE, "unit-%c", pvr2_hdw_get_unit_number(sfp->channel.hdw) + 'a'); } else { kfree(class_dev); return; } class_dev->parent = &usb_dev->dev; sfp->class_dev = class_dev; class_dev->driver_data = sfp; ret = device_register(class_dev); if (ret) { pvr2_trace(PVR2_TRACE_ERROR_LEGS, "device_register failed"); kfree(class_dev); return; } 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 = NULL; ret = device_create_file(sfp->class_dev, &sfp->attr_v4l_minor_number); if (ret < 0) { pvr2_trace(PVR2_TRACE_ERROR_LEGS, "device_create_file error: %d", ret); } else { sfp->v4l_minor_number_created_ok = !0; } sfp->attr_v4l_radio_minor_number.attr.name = "v4l_radio_minor_number"; sfp->attr_v4l_radio_minor_number.attr.mode = S_IRUGO; sfp->attr_v4l_radio_minor_number.show = v4l_radio_minor_number_show; sfp->attr_v4l_radio_minor_number.store = NULL; ret = device_create_file(sfp->class_dev, &sfp->attr_v4l_radio_minor_number); if (ret < 0) { pvr2_trace(PVR2_TRACE_ERROR_LEGS, "device_create_file error: %d", ret); } else { sfp->v4l_radio_minor_number_created_ok = !0; } 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 = NULL; ret = device_create_file(sfp->class_dev,&sfp->attr_unit_number); if (ret < 0) { pvr2_trace(PVR2_TRACE_ERROR_LEGS, "device_create_file error: %d", ret); } else { sfp->unit_number_created_ok = !0; } sfp->attr_bus_info.attr.name = "bus_info_str"; sfp->attr_bus_info.attr.mode = S_IRUGO; sfp->attr_bus_info.show = bus_info_show; sfp->attr_bus_info.store = NULL; ret = device_create_file(sfp->class_dev, &sfp->attr_bus_info); if (ret < 0) { pvr2_trace(PVR2_TRACE_ERROR_LEGS, "device_create_file error: %d", ret); } else { sfp->bus_info_created_ok = !0; } sfp->attr_hdw_name.attr.name = "device_hardware_type"; sfp->attr_hdw_name.attr.mode = S_IRUGO; sfp->attr_hdw_name.show = hdw_name_show; sfp->attr_hdw_name.store = NULL; ret = device_create_file(sfp->class_dev, &sfp->attr_hdw_name); if (ret < 0) { pvr2_trace(PVR2_TRACE_ERROR_LEGS, "device_create_file error: %d", ret); } else { sfp->hdw_name_created_ok = !0; } sfp->attr_hdw_desc.attr.name = "device_hardware_description"; sfp->attr_hdw_desc.attr.mode = S_IRUGO; sfp->attr_hdw_desc.show = hdw_desc_show; sfp->attr_hdw_desc.store = NULL; ret = device_create_file(sfp->class_dev, &sfp->attr_hdw_desc); if (ret < 0) { pvr2_trace(PVR2_TRACE_ERROR_LEGS, "device_create_file error: %d", ret); } else { sfp->hdw_desc_created_ok = !0; } pvr2_sysfs_add_controls(sfp);#ifdef CONFIG_VIDEO_PVRUSB2_DEBUGIFC pvr2_sysfs_add_debugifc(sfp);#endif /* CONFIG_VIDEO_PVRUSB2_DEBUGIFC */}static void pvr2_sysfs_internal_check(struct pvr2_channel *chp){ struct pvr2_sysfs *sfp; sfp = container_of(chp,struct pvr2_sysfs,channel); if (!sfp->channel.mc_head->disconnect_flag) return; pvr2_trace(PVR2_TRACE_STRUCT,"Destroying pvr2_sysfs id=%p",sfp); class_dev_destroy(sfp); pvr2_channel_done(&sfp->channel); kfree(sfp);}struct pvr2_sysfs *pvr2_sysfs_create(struct pvr2_context *mp, struct pvr2_sysfs_class *class_ptr){ struct pvr2_sysfs *sfp; sfp = kzalloc(sizeof(*sfp),GFP_KERNEL); if (!sfp) return sfp; pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr2_sysfs id=%p",sfp); pvr2_channel_init(&sfp->channel,mp); sfp->channel.check_func = pvr2_sysfs_internal_check; class_dev_create(sfp,class_ptr); return sfp;}#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)static int pvr2_sysfs_hotplug(struct class_device *cd,char **envp, int numenvp,char *buf,int size){ /* Even though we don't do anything here, we still need this function because sysfs will still try to call it. */ return 0;}#endifstruct pvr2_sysfs_class *pvr2_sysfs_class_create(void){ struct pvr2_sysfs_class *clp; clp = kzalloc(sizeof(*clp),GFP_KERNEL); if (!clp) return clp; pvr2_sysfs_trace("Creating pvr2_sysfs_class id=%p",clp); clp->class.name = "pvrusb2"; clp->class.class_release = pvr2_sysfs_class_release;#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19) clp->class.release = pvr2_sysfs_release;#else clp->class.dev_release = pvr2_sysfs_release;#endif#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19) clp->class.uevent = pvr2_sysfs_hotplug;#endif if (class_register(&clp->class)) { pvr2_sysfs_trace( "Registration failed for pvr2_sysfs_class id=%p",clp); kfree(clp); clp = NULL; } return clp;}void pvr2_sysfs_class_destroy(struct pvr2_sysfs_class *clp){ class_unregister(&clp->class);}#ifdef CONFIG_VIDEO_PVRUSB2_DEBUGIFCstatic ssize_t debuginfo_show(struct device *class_dev, struct device_attribute *attr, char *buf){ struct pvr2_sysfs *sfp; sfp = (struct pvr2_sysfs *)class_dev->driver_data; if (!sfp) return -EINVAL; pvr2_hdw_trigger_module_log(sfp->channel.hdw); return pvr2_debugifc_print_info(sfp->channel.hdw,buf,PAGE_SIZE);}static ssize_t debugcmd_show(struct device *class_dev, struct device_attribute *attr, char *buf){ struct pvr2_sysfs *sfp; sfp = (struct pvr2_sysfs *)class_dev->driver_data; if (!sfp) return -EINVAL; return pvr2_debugifc_print_status(sfp->channel.hdw,buf,PAGE_SIZE);}static ssize_t debugcmd_store(struct device *class_dev, struct device_attribute *attr, const char *buf, size_t count){ struct pvr2_sysfs *sfp; int ret; sfp = (struct pvr2_sysfs *)class_dev->driver_data; if (!sfp) return -EINVAL; ret = pvr2_debugifc_docmd(sfp->channel.hdw,buf,count); if (ret < 0) return ret; return count;}#endif /* CONFIG_VIDEO_PVRUSB2_DEBUGIFC *//* Stuff for Emacs to see, in order to encourage consistent editing style: *** Local Variables: *** *** mode: c *** *** fill-column: 75 *** *** tab-width: 8 *** *** c-basic-offset: 8 *** *** End: *** */
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?