.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;
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;
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);
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);