cash_drawers.c 11 KB

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