cash_drawers.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. #include <linux/module.h>
  2. #include <linux/kernel.h>
  3. #include <linux/fs.h>
  4. #include <linux/cdev.h>
  5. #include <linux/device.h>
  6. #include <linux/slab.h>
  7. #include <linux/uaccess.h>
  8. #include <linux/poll.h>
  9. #include <linux/wait.h>
  10. #include <linux/sched.h>
  11. #include <linux/mutex.h>
  12. #include <linux/string.h>
  13. #include <linux/io.h>
  14. #define DEVICE_NAME "cashd"
  15. #define CLASS_NAME "cashd_class"
  16. #define MINOR_BASE 0
  17. #define MINOR_COUNT 2
  18. #define BUFFER_SIZE 4
  19. #define GPIO0_CTL 0xFD6D0B50
  20. #define GPIO0_STATUS 0xFD6D0940
  21. #define GPIO1_CTL 0xFD6D0B60
  22. #define GPIO1_STATUS 0xFD6D0950
  23. struct cashd_device {
  24. unsigned char *buffer;
  25. size_t buffer_size;
  26. wait_queue_head_t read_wait;
  27. wait_queue_head_t write_wait;
  28. struct mutex lock;
  29. struct cdev cdev;
  30. int dev_major;
  31. int dev_minor;
  32. bool can_read;
  33. bool can_write;
  34. unsigned int ctl_status;
  35. unsigned int in_status;
  36. unsigned int status_changed;
  37. int last_status;
  38. struct delayed_work delay_work1;
  39. struct delayed_work delay_work2;
  40. void __iomem *gpio_ctl_reg_base;
  41. void __iomem *gpio_status_reg_base;
  42. int count;
  43. };
  44. static int cashd_major = 0;
  45. static struct class *cashd_class = NULL;
  46. static struct cashd_device *cashd_devices[MINOR_COUNT];
  47. static unsigned int minors[MINOR_COUNT] = {10, 11};
  48. static void gpio_ctl_delay_work_func(struct work_struct *work)
  49. {
  50. unsigned int value = 0;
  51. struct delayed_work *dwork = to_delayed_work(work);
  52. struct cashd_device *dev = container_of(dwork, struct cashd_device, delay_work1);
  53. value = readl(dev->gpio_ctl_reg_base);
  54. value &= 0xfffffffe;
  55. writel(value, dev->gpio_ctl_reg_base);
  56. }
  57. static void gpio_status_delay_work_func(struct work_struct *work)
  58. {
  59. unsigned int value = 0;
  60. struct delayed_work *dwork = to_delayed_work(work);
  61. struct cashd_device *dev = container_of(dwork, struct cashd_device, delay_work2);
  62. value = readl(dev->gpio_status_reg_base);
  63. value = value & 0x1;
  64. if(value != dev->last_status)
  65. {
  66. dev->last_status = value;
  67. dev->status_changed = 1;
  68. wake_up_interruptible(&dev->read_wait);
  69. }
  70. dev->count++;
  71. if(dev->count >= 50)
  72. {
  73. dev->count = 0;
  74. dev->status_changed = 1;
  75. printk("%s %s %d\n", __FILE__, __func__, __LINE__);
  76. wake_up_interruptible(&dev->read_wait);
  77. }
  78. printk("%s %s %d\n", __FILE__, __func__, __LINE__);
  79. schedule_delayed_work(&dev->delay_work2, msecs_to_jiffies(100));
  80. }
  81. static int cashd_open(struct inode *inode, struct file *filp)
  82. {
  83. struct cashd_device *dev = NULL;
  84. unsigned int minor = iminor(inode);
  85. int i = 0;
  86. for(i = 0; i < MINOR_COUNT; i++)
  87. {
  88. if(minor == minors[i])
  89. {
  90. dev = cashd_devices[i];
  91. break;
  92. }
  93. }
  94. if (!dev) {
  95. pr_err("cashd: Device not initialized for minor %d\n", minor);
  96. return -ENODEV;
  97. }
  98. filp->private_data = dev;
  99. pr_info("cashd: Device /dev/cashd%d opened\n", minor);
  100. return 0;
  101. }
  102. static int cashd_release(struct inode *inode, struct file *filp)
  103. {
  104. unsigned int minor = iminor(inode);
  105. pr_info("cashd: Device /dev/cashd%d closed\n", minor);
  106. return 0;
  107. }
  108. static ssize_t cashd_read(struct file *filp, char __user *buf,
  109. size_t count, loff_t *f_pos)
  110. {
  111. struct cashd_device *dev = filp->private_data;
  112. ssize_t bytes_read = 0;
  113. int ret = 0;
  114. if (!dev)
  115. return -EINVAL;
  116. if (!buf || count == 0)
  117. return -EINVAL;
  118. dev->in_status = readl(dev->gpio_status_reg_base);
  119. if(dev->in_status & 0x1)
  120. {
  121. ret = copy_to_user(buf, "o", 1);
  122. if(ret != 0)
  123. {
  124. return -EFAULT;
  125. }
  126. }
  127. else
  128. {
  129. ret = copy_to_user(buf, "c", 1);
  130. if(ret != 0)
  131. {
  132. return -EFAULT;
  133. }
  134. }
  135. dev->status_changed = 0;
  136. bytes_read = 1;
  137. return bytes_read;
  138. }
  139. static ssize_t cashd_write(struct file *filp, const char __user *buf,
  140. size_t count, loff_t *f_pos)
  141. {
  142. struct cashd_device *dev = filp->private_data;
  143. int ret;
  144. unsigned int value = 0;
  145. if (!dev)
  146. return -EINVAL;
  147. if(count > BUFFER_SIZE)
  148. {
  149. count = BUFFER_SIZE;
  150. }
  151. ret = copy_from_user(dev->buffer, buf, count);
  152. if(ret != 0)
  153. {
  154. return -EFAULT;
  155. }
  156. if(dev->buffer[0] == 'o' || dev->buffer[0] == 'O')
  157. {
  158. printk("%s %s %d minor=%d\n", __FILE__, __func__, __LINE__, dev->dev_minor);
  159. value = readl(dev->gpio_ctl_reg_base);
  160. value |= 0x1;
  161. writel(value, dev->gpio_ctl_reg_base);
  162. schedule_delayed_work(&dev->delay_work1, msecs_to_jiffies(1000));
  163. }
  164. else
  165. {
  166. printk(KERN_INFO "cashd: Invalid input\n");
  167. }
  168. return count;
  169. }
  170. static unsigned int cashd_poll(struct file *filp, poll_table *wait)
  171. {
  172. struct cashd_device *dev = filp->private_data;
  173. unsigned int mask = 0;
  174. if (!dev)
  175. return POLLERR;
  176. poll_wait(filp, &dev->read_wait, wait);
  177. // poll_wait(filp, &dev->write_wait, wait);
  178. mutex_lock(&dev->lock);
  179. if(dev->status_changed)
  180. mask |= POLLIN | POLLRDNORM;
  181. mutex_unlock(&dev->lock);
  182. return mask;
  183. }
  184. static const struct file_operations cashd_fops = {
  185. .owner = THIS_MODULE,
  186. .open = cashd_open,
  187. .release = cashd_release,
  188. .read = cashd_read,
  189. .write = cashd_write,
  190. .poll = cashd_poll,
  191. };
  192. static int __init cashd_init_device(struct cashd_device *dev, int minor)
  193. {
  194. int ret;
  195. dev->buffer = kmalloc(BUFFER_SIZE, GFP_KERNEL);
  196. if (!dev->buffer) {
  197. pr_err("cashd: Failed to allocate buffer for minor %d\n", minor);
  198. return -ENOMEM;
  199. }
  200. memset(dev->buffer, 0, BUFFER_SIZE);
  201. dev->buffer_size = BUFFER_SIZE;
  202. dev->dev_major = cashd_major;
  203. dev->dev_minor = minor;
  204. dev->can_read = false;
  205. dev->can_write = true;
  206. init_waitqueue_head(&dev->read_wait);
  207. init_waitqueue_head(&dev->write_wait);
  208. mutex_init(&dev->lock);
  209. INIT_DELAYED_WORK(&dev->delay_work1, gpio_ctl_delay_work_func);
  210. INIT_DELAYED_WORK(&dev->delay_work2, gpio_status_delay_work_func);
  211. cdev_init(&dev->cdev, &cashd_fops);
  212. dev->cdev.owner = THIS_MODULE;
  213. ret = cdev_add(&dev->cdev, MKDEV(cashd_major, minor), 1);
  214. if (ret) {
  215. pr_err("cashd: Failed to add cdev for minor %d\n", minor);
  216. kfree(dev->buffer);
  217. return ret;
  218. }
  219. return 0;
  220. }
  221. static char *my_devnode(struct device *dev, umode_t *mode) {
  222. if (mode) {
  223. *mode = 0666;
  224. }
  225. return NULL;
  226. }
  227. int cashd_init(void)
  228. {
  229. int ret;
  230. int i;
  231. struct device *device;
  232. cashd_major = 56;
  233. cashd_class = class_create(THIS_MODULE, CLASS_NAME);
  234. if (IS_ERR(cashd_class)) {
  235. ret = PTR_ERR(cashd_class);
  236. pr_err("cashd: Failed to create class\n");
  237. goto fail_class;
  238. }
  239. cashd_class->devnode = my_devnode;
  240. for (i = 0; i < MINOR_COUNT; i++) {
  241. cashd_devices[i] = kzalloc(sizeof(struct cashd_device), GFP_KERNEL);
  242. if (!cashd_devices[i]) {
  243. pr_err("cashd: Failed to allocate device for minor %d\n", i);
  244. ret = -ENOMEM;
  245. goto fail_devices;
  246. }
  247. ret = cashd_init_device(cashd_devices[i], minors[i]);
  248. if (ret) {
  249. pr_err("cashd: Failed to init device for minor %d\n", i);
  250. kfree(cashd_devices[i]);
  251. cashd_devices[i] = NULL;
  252. goto fail_devices;
  253. }
  254. device = device_create(cashd_class, NULL,
  255. MKDEV(cashd_major, minors[i]), NULL,
  256. "cashd%d", i);
  257. if (IS_ERR(device)) {
  258. ret = PTR_ERR(device);
  259. pr_err("cashd: Failed to create device for minor %d\n", i);
  260. cdev_del(&cashd_devices[i]->cdev);
  261. kfree(cashd_devices[i]);
  262. cashd_devices[i] = NULL;
  263. goto fail_devices;
  264. }
  265. if(i == 0)
  266. {
  267. cashd_devices[i]->gpio_ctl_reg_base = ioremap(GPIO0_CTL, 4);
  268. cashd_devices[i]->gpio_status_reg_base = ioremap(GPIO0_STATUS, 4);
  269. }
  270. else if(i == 1)
  271. {
  272. cashd_devices[i]->gpio_ctl_reg_base = ioremap(GPIO1_CTL, 4);
  273. cashd_devices[i]->gpio_status_reg_base = ioremap(GPIO1_STATUS, 4);
  274. }
  275. schedule_delayed_work(&cashd_devices[i]->delay_work2, msecs_to_jiffies(100));
  276. pr_info("cashd: Created /dev/cashd%d\n", i);
  277. }
  278. pr_info("cashd: Driver initialized successfully\n");
  279. return 0;
  280. fail_devices:
  281. for (i = 0; i < MINOR_COUNT; i++) {
  282. if (cashd_devices[i]) {
  283. device_destroy(cashd_class, MKDEV(cashd_major, minors[i]));
  284. cdev_del(&cashd_devices[i]->cdev);
  285. if (cashd_devices[i]->buffer)
  286. kfree(cashd_devices[i]->buffer);
  287. kfree(cashd_devices[i]);
  288. }
  289. }
  290. class_destroy(cashd_class);
  291. fail_class:
  292. unregister_chrdev_region(MKDEV(cashd_major, MINOR_BASE), MINOR_COUNT);
  293. return ret;
  294. }
  295. void cashd_exit(void)
  296. {
  297. int i;
  298. pr_info("cashd: Cleaning up driver\n");
  299. for (i = 0; i < MINOR_COUNT; i++) {
  300. if (cashd_devices[i]) {
  301. cancel_delayed_work_sync(&cashd_devices[i]->delay_work1);
  302. cancel_delayed_work_sync(&cashd_devices[i]->delay_work2);
  303. device_destroy(cashd_class, MKDEV(cashd_major, minors[i]));
  304. cdev_del(&cashd_devices[i]->cdev);
  305. if (cashd_devices[i]->buffer)
  306. kfree(cashd_devices[i]->buffer);
  307. iounmap(cashd_devices[i]->gpio_ctl_reg_base);
  308. iounmap(cashd_devices[i]->gpio_status_reg_base);
  309. kfree(cashd_devices[i]);
  310. pr_info("cashd: Removed /dev/cashd%d\n", i);
  311. }
  312. }
  313. if (cashd_class)
  314. class_destroy(cashd_class);
  315. unregister_chrdev_region(MKDEV(cashd_major, MINOR_BASE), MINOR_COUNT);
  316. pr_info("cashd: Driver cleaned up\n");
  317. }