|
| 1 | +/* |
| 2 | + * dht11.c - Using GPIO to read temperature and humidity from DHT11 sensor. |
| 3 | + */ |
| 4 | + |
| 5 | +#include <linux/cdev.h> |
| 6 | +#include <linux/delay.h> |
| 7 | +#include <linux/device.h> |
| 8 | +#include <linux/fs.h> |
| 9 | +#include <linux/gpio.h> |
| 10 | +#include <linux/init.h> |
| 11 | +#include <linux/module.h> |
| 12 | +#include <linux/printk.h> |
| 13 | +#include <linux/types.h> |
| 14 | +#include <linux/uaccess.h> |
| 15 | +#include <linux/version.h> |
| 16 | + |
| 17 | +#include <asm/errno.h> |
| 18 | + |
| 19 | +#define GPIO_PIN_4 575 |
| 20 | +#define DEVICE_NAME "dht11" |
| 21 | +#define DEVICE_CNT 1 |
| 22 | + |
| 23 | +static char msg[64]; |
| 24 | + |
| 25 | +struct dht11_dev { |
| 26 | + dev_t dev_num; |
| 27 | + int major_num, minor_num; |
| 28 | + struct cdev cdev; |
| 29 | + struct class *cls; |
| 30 | + struct device *dev; |
| 31 | +}; |
| 32 | + |
| 33 | +static struct dht11_dev dht11_device; |
| 34 | + |
| 35 | +/* Define GPIOs for LEDs. |
| 36 | + * TODO: According to the requirements, search /sys/kernel/debug/gpio to |
| 37 | + * find the corresponding GPIO location. |
| 38 | + */ |
| 39 | +static struct gpio dht11[] = { { GPIO_PIN_4, GPIOF_OUT_INIT_HIGH, "Signal" } }; |
| 40 | + |
| 41 | +static int dht11_read_data(void) |
| 42 | +{ |
| 43 | + int timeout; |
| 44 | + uint8_t sensor_data[5] = { 0 }; |
| 45 | + uint8_t i, j; |
| 46 | + |
| 47 | + gpio_set_value(dht11[0].gpio, 0); |
| 48 | + mdelay(20); |
| 49 | + gpio_set_value(dht11[0].gpio, 1); |
| 50 | + udelay(30); |
| 51 | + gpio_direction_input(dht11[0].gpio); |
| 52 | + udelay(2); |
| 53 | + |
| 54 | + timeout = 300; |
| 55 | + while (gpio_get_value(dht11[0].gpio) && timeout--) |
| 56 | + udelay(1); |
| 57 | + |
| 58 | + if (timeout == -1) |
| 59 | + return -ETIMEDOUT; |
| 60 | + |
| 61 | + timeout = 300; |
| 62 | + while (!gpio_get_value(dht11[0].gpio) && timeout--) |
| 63 | + udelay(1); |
| 64 | + |
| 65 | + if (timeout == -1) |
| 66 | + return -ETIMEDOUT; |
| 67 | + |
| 68 | + timeout = 300; |
| 69 | + while (gpio_get_value(dht11[0].gpio) && timeout--) |
| 70 | + udelay(1); |
| 71 | + |
| 72 | + if (timeout == -1) |
| 73 | + return -ETIMEDOUT; |
| 74 | + |
| 75 | + for (j = 0; j < 5; j++) { |
| 76 | + uint8_t byte = 0; |
| 77 | + for (i = 0; i < 8; i++) { |
| 78 | + timeout = 300; |
| 79 | + while (gpio_get_value(dht11[0].gpio) && timeout--) |
| 80 | + udelay(1); |
| 81 | + |
| 82 | + if (timeout == -1) |
| 83 | + return -ETIMEDOUT; |
| 84 | + |
| 85 | + timeout = 300; |
| 86 | + while (!gpio_get_value(dht11[0].gpio) && timeout--) |
| 87 | + udelay(1); |
| 88 | + |
| 89 | + if (timeout == -1) |
| 90 | + return -ETIMEDOUT; |
| 91 | + |
| 92 | + udelay(50); |
| 93 | + byte <<= 1; |
| 94 | + if (gpio_get_value(dht11[0].gpio)) |
| 95 | + byte |= 0x01; |
| 96 | + } |
| 97 | + sensor_data[j] = byte; |
| 98 | + } |
| 99 | + |
| 100 | + if (sensor_data[4] != (uint8_t)(sensor_data[0] + sensor_data[1] + |
| 101 | + sensor_data[2] + sensor_data[3])) |
| 102 | + return -EIO; |
| 103 | + |
| 104 | + gpio_direction_output(dht11[0].gpio, 1); |
| 105 | + sprintf(msg, "Humidity: %d%%\nTemperature: %d deg C\n", sensor_data[0], |
| 106 | + sensor_data[2]); |
| 107 | + |
| 108 | + return 0; |
| 109 | +} |
| 110 | + |
| 111 | +static int device_open(struct inode *inode, struct file *file) |
| 112 | +{ |
| 113 | + int ret, retry; |
| 114 | + |
| 115 | + for (retry = 0; retry < 5; ++retry) { |
| 116 | + ret = dht11_read_data(); |
| 117 | + if (ret == 0) |
| 118 | + return 0; |
| 119 | + msleep(10); |
| 120 | + } |
| 121 | + |
| 122 | + if (ret) { |
| 123 | + gpio_direction_output(dht11[0].gpio, 1); |
| 124 | + return ret; |
| 125 | + } |
| 126 | + |
| 127 | + return 0; |
| 128 | +} |
| 129 | + |
| 130 | +static int device_release(struct inode *inode, struct file *file) |
| 131 | +{ |
| 132 | + return 0; |
| 133 | +} |
| 134 | + |
| 135 | +static ssize_t device_read(struct file *filp, char __user *buffer, |
| 136 | + size_t length, loff_t *offset) |
| 137 | +{ |
| 138 | + int msg_len = strlen(msg); |
| 139 | + |
| 140 | + if (*offset >= msg_len) |
| 141 | + return 0; |
| 142 | + |
| 143 | + size_t remain = msg_len - *offset; |
| 144 | + size_t bytes_read = min(length, remain); |
| 145 | + |
| 146 | + if (copy_to_user(buffer, msg + *offset, bytes_read)) |
| 147 | + return -EFAULT; |
| 148 | + |
| 149 | + *offset += bytes_read; |
| 150 | + |
| 151 | + return bytes_read; |
| 152 | +} |
| 153 | + |
| 154 | +static struct file_operations fops = { |
| 155 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(6, 4, 0) |
| 156 | + .owner = THIS_MODULE, |
| 157 | +#endif |
| 158 | + .open = device_open, |
| 159 | + .release = device_release, |
| 160 | + .read = device_read |
| 161 | +}; |
| 162 | + |
| 163 | +/* Initialize the module - Register the character device */ |
| 164 | +static int __init dht11_init(void) |
| 165 | +{ |
| 166 | + int ret = 0; |
| 167 | + |
| 168 | + /* Determine whether dynamic allocation of the device number is needed. */ |
| 169 | + if (dht11_device.major_num) { |
| 170 | + dht11_device.dev_num = |
| 171 | + MKDEV(dht11_device.major_num, dht11_device.minor_num); |
| 172 | + ret = register_chrdev_region(dht11_device.dev_num, DEVICE_CNT, |
| 173 | + DEVICE_NAME); |
| 174 | + } else { |
| 175 | + ret = alloc_chrdev_region(&dht11_device.dev_num, 0, DEVICE_CNT, |
| 176 | + DEVICE_NAME); |
| 177 | + } |
| 178 | + |
| 179 | + /* Negative values signify an error */ |
| 180 | + if (ret < 0) { |
| 181 | + pr_alert("Failed to register character device, error: %d\n", ret); |
| 182 | + return ret; |
| 183 | + } |
| 184 | + |
| 185 | + pr_info("Major = %d, Minor = %d\n", MAJOR(dht11_device.dev_num), |
| 186 | + MINOR(dht11_device.dev_num)); |
| 187 | + |
| 188 | + /* Prevents module unloading while operations are in use */ |
| 189 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(6, 4, 0) |
| 190 | + dht11_device.cdev.owner = THIS_MODULE; |
| 191 | +#endif |
| 192 | + |
| 193 | + cdev_init(&dht11_device.cdev, &fops); |
| 194 | + ret = cdev_add(&dht11_device.cdev, dht11_device.dev_num, 1); |
| 195 | + if (ret) { |
| 196 | + pr_err("Failed to add the device to the system\n"); |
| 197 | + goto fail1; |
| 198 | + } |
| 199 | + |
| 200 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 4, 0) |
| 201 | + dht11_device.cls = class_create(DEVICE_NAME); |
| 202 | +#else |
| 203 | + dht11_device.cls = class_create(THIS_MODULE, DEVICE_NAME); |
| 204 | +#endif |
| 205 | + if (IS_ERR(dht11_device.cls)) { |
| 206 | + pr_err("Failed to create class for device\n"); |
| 207 | + ret = PTR_ERR(dht11_device.cls); |
| 208 | + goto fail2; |
| 209 | + } |
| 210 | + |
| 211 | + dht11_device.dev = device_create(dht11_device.cls, NULL, |
| 212 | + dht11_device.dev_num, NULL, DEVICE_NAME); |
| 213 | + if (IS_ERR(dht11_device.dev)) { |
| 214 | + pr_err("Failed to create the device file\n"); |
| 215 | + ret = PTR_ERR(dht11_device.dev); |
| 216 | + goto fail3; |
| 217 | + } |
| 218 | + |
| 219 | + pr_info("Device created on /dev/%s\n", DEVICE_NAME); |
| 220 | + |
| 221 | + ret = gpio_request(dht11[0].gpio, dht11[0].label); |
| 222 | + |
| 223 | + if (ret) { |
| 224 | + pr_err("Unable to request GPIOs for dht11: %d\n", ret); |
| 225 | + goto fail4; |
| 226 | + } |
| 227 | + |
| 228 | + ret = gpio_direction_output(dht11[0].gpio, 1); |
| 229 | + |
| 230 | + if (ret) { |
| 231 | + pr_err("Failed to set GPIO %d direction\n", dht11[0].gpio); |
| 232 | + goto fail5; |
| 233 | + } |
| 234 | + |
| 235 | + return 0; |
| 236 | + |
| 237 | +fail5: |
| 238 | + gpio_free(dht11[0].gpio); |
| 239 | + |
| 240 | +fail4: |
| 241 | + device_destroy(dht11_device.cls, dht11_device.dev_num); |
| 242 | + |
| 243 | +fail3: |
| 244 | + class_destroy(dht11_device.cls); |
| 245 | + |
| 246 | +fail2: |
| 247 | + cdev_del(&dht11_device.cdev); |
| 248 | + |
| 249 | +fail1: |
| 250 | + unregister_chrdev_region(dht11_device.dev_num, DEVICE_CNT); |
| 251 | + |
| 252 | + return ret; |
| 253 | +} |
| 254 | + |
| 255 | +static void __exit dht11_exit(void) |
| 256 | +{ |
| 257 | + gpio_set_value(dht11[0].gpio, 0); |
| 258 | + gpio_free(dht11[0].gpio); |
| 259 | + |
| 260 | + device_destroy(dht11_device.cls, dht11_device.dev_num); |
| 261 | + class_destroy(dht11_device.cls); |
| 262 | + cdev_del(&dht11_device.cdev); |
| 263 | + unregister_chrdev_region(dht11_device.dev_num, DEVICE_CNT); |
| 264 | +} |
| 265 | + |
| 266 | +module_init(dht11_init); |
| 267 | +module_exit(dht11_exit); |
| 268 | + |
| 269 | +MODULE_LICENSE("GPL"); |
0 commit comments