linux camera GS5604调试

来源:互联网 发布:佳能eos软件下载 编辑:程序博客网 时间:2024/05/17 07:38
#include <linux/kobject.h>
#include <linux/string.h>
#include <linux/sysfs.h>

/*
 * This module shows how to create a simple subdirectory in sysfs called
 * /sys/kernel/kobject-example  In that directory, 3 files are created:
 * "foo", "baz", and "bar".  If an integer is written to these files, it can be
 * later read out of it.
 */
static int sensor8;
static int sensor16;


// cd  sys/kernel/sensor_dump   ; echo 0x1345 0x34bc > sensor16  ; echo 0x1345 0x34 > sensor8

/*
 * The "foo" file where a static variable is read from and written to.
 */
static ssize_t read8_show(struct kobject *kobj, struct kobj_attribute *attr,
            char *buf)
{
#if 0
    int rc = -1;
    unsigned int   iregister = 0;
    unsigned char   u8Value = 0xff;
    
    sscanf(buf, "%x", iregister);
    
    rc = sensor_read_value8(glb_sd, iregister, &u8Value);
    printk("%s,addr=0x%x, value8=0x%x,rc =%d\n", __func__,iregister, u8Value,rc);
    
    return sprintf(buf, "%x\n", u8Value);
#endif
    return 0;
}

static ssize_t write8_store(struct kobject *kobj, struct kobj_attribute *attr,
             const char *buf, size_t count)
{
    unsigned int   iregister = 0;
    unsigned int   u8Value = 0xff;
    int rc = -1;
    
    if(sscanf(buf, "%x %x", &iregister, &u8Value)==2) {
        rc = sensor_write_value8(glb_sd, (unsigned short)iregister, (unsigned char)u8Value);
        printk("write16 %s,addr=0x%x, value8=0x%x,rc =%d\n", __func__,iregister, u8Value,rc);
    } else if (sscanf(buf, "%x", &iregister) == 1) {
        rc = sensor_read_value8(glb_sd, iregister, &u8Value);
        printk("read16 %s,addr=0x%x, value8=0x%x,rc =%d\n", __func__,iregister, u8Value,rc);
    } else
        printk("input err %s,addr=0x%x, value8=0x%x,rc =%d\n", __func__,iregister, u8Value,rc);
    
    return count;
}

static struct kobj_attribute sensor8_attribute =
    __ATTR(sensor8, 0666, read8_show, write8_store);

/*
 * More complex function where we determine which variable is being accessed by
 * looking at the attribute for the "baz" and "bar" files.
 */
static ssize_t read16_show(struct kobject *kobj, struct kobj_attribute *attr,
              char *buf)
{
#if 0
    int rc = -1;
    unsigned int   iregister = 0;
    unsigned int   u16Value = 0xffff;
    
    sscanf(buf, "%x", iregister);
    rc = sensor_read_value16(glb_sd, iregister, &u16Value);
    
    printk("%s,addr=0x%x, u16Value=0x%x\n", __func__,iregister, u16Value);

    return sprintf(buf, "%x\n", u16Value);
#else
    return 0;
#endif
}

static ssize_t write16_store(struct kobject *kobj, struct kobj_attribute *attr,
               const char *buf, size_t count)
{
    int rc = -1;
    unsigned int   iregister = 0;
    unsigned int   u16Value = 0xff;
    
    if(sscanf(buf, "%x %x", &iregister, &u16Value) == 2) {

        rc = sensor_write_value16(glb_sd, (unsigned short)iregister, (unsigned char)u16Value);
        printk("write16 %s,addr=0x%x, u16Value=0x%x,rc =%d\n", __func__,iregister, u16Value,rc);
    } else if (sscanf(buf, "%x", &iregister) == 1) {
        rc = sensor_read_value16(glb_sd, iregister, &u16Value);
        printk("read16 %s,addr=0x%x, value16=0x%x,rc =%d\n", __func__,iregister, u16Value,rc);
    } else
        printk("input error %s,addr=0x%x, value16=0x%x,rc =%d\n", __func__,iregister, u16Value,rc);
    
    return count;

}

static struct kobj_attribute sensor16_attribute =
    __ATTR(sensor16, 0666, read16_show, write16_store);
/*
 * Create a group of attributes so that we can create and destroy them all
 * at once.
 */
static struct attribute *attrs[] = {
    &sensor8_attribute.attr,
    &sensor16_attribute.attr,
    NULL,    /* need to NULL terminate the list of attributes */
};
/*
 * An unnamed attribute group will put all of the attributes directly in
 * the kobject directory.  If we specify a name, a subdirectory will be
 * created for the attributes with the directory being the name of the
 * attribute group.
 */
static struct attribute_group attr_group = {
    .attrs = attrs,
};

static struct kobject *example_kobj;

static int  example_init(void)
{
    int retval;

    example_kobj = kobject_create_and_add("sensor_dump", kernel_kobj);
    if (!example_kobj)
        return -ENOMEM;

    /* Create the files associated with this kobject */
    retval = sysfs_create_group(sensor_dump, &attr_group);
    if (retval)
        kobject_put(example_kobj);

    return retval;
}

static void  example_exit(void)
{
    kobject_put(example_kobj);
}


// cd  sys/kernel/sensor_dump   ; echo 0x1345 0x34bc > sensor16  ; echo 0x1345 0x34 > sensor8

static __init int init_sensor(void)
{
    example_init();
    return cci_dev_init_helper(&sensor_driver);
}

static __exit void exit_sensor(void)
{
    example_exit();
    cci_dev_exit_helper(&sensor_driver);
}
module_init(init_sensor);
module_exit(exit_sensor);

0 0
原创粉丝点击