cash_drawers.c 10 KB

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