X-Git-Url: http://git.cascardo.eti.br/?p=cascardo%2Fkernel%2Fsamples%2Fchar2%2F.git;a=blobdiff_plain;f=hellochar.c;fp=hellochar.c;h=940bfeb837b7ca7ddcbd43aebaa496845a871ae2;hp=e601e38448c010a071de7837ec89ddd167cbd52a;hb=c39b413f2ede7cb37a11a3f6b055a06eedbf6b6c;hpb=ccfa829bd17614400d2d008c15fef13d08f242a2 diff --git a/hellochar.c b/hellochar.c index e601e38..940bfeb 100644 --- a/hellochar.c +++ b/hellochar.c @@ -117,6 +117,18 @@ static const struct file_operations hello_fops = { .write = hello_write, }; +static ssize_t hello_len_show(struct device *dev, struct device_attribute *attr, + char *buffer) +{ + size_t len = CIRC_CNT(hello->head, hello->tail, MAXLEN); + return sprintf(buffer, "%d\n", len); +} + +static const struct device_attribute hello_len_attr = { + .attr = { .name = "len", .mode = 0444, }, + .show = hello_len_show, +}; + static struct class *hello_class; static struct device *hello_dev; @@ -144,6 +156,9 @@ static int __init ch_init(void) r = -ENODEV; goto dev_out; } + r = device_create_file(hello_dev, &hello_len_attr); + if (r) + goto file_out; dev = cdev_alloc(); if (!dev) { r = -ENOMEM; @@ -157,6 +172,8 @@ static int __init ch_init(void) add_out: kfree(dev); cdev_out: + device_remove_file(hello_dev, &hello_len_attr); +file_out: device_destroy(hello_class, devnum); dev_out: unregister_chrdev_region(devnum, 256); @@ -171,6 +188,7 @@ out: static void __exit ch_exit(void) { cdev_del(dev); + device_remove_file(hello_dev, &hello_len_attr); device_destroy(hello_class, devnum); unregister_chrdev_region(devnum, 256); kfree(hello->buf);