cash_drawers.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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 read_flag;
  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->read_flag = 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. if (!dev)
  113. return -EINVAL;
  114. if (!buf || count == 0)
  115. return -EINVAL;
  116. if(dev->read_flag >= 1)
  117. {
  118. return 0;
  119. }
  120. dev->read_flag++;
  121. dev->in_status = readl(dev->gpio_status_reg_base);
  122. if(dev->in_status & 0x2)
  123. {
  124. ret = copy_to_user(buf, "o", 1);
  125. if(ret != 0)
  126. {
  127. return -EFAULT;
  128. }
  129. }
  130. else
  131. {
  132. ret = copy_to_user(buf, "c", 1);
  133. if(ret != 0)
  134. {
  135. return -EFAULT;
  136. }
  137. }
  138. dev->status_changed = 0;
  139. bytes_read = 1;
  140. return bytes_read;
  141. }
  142. static ssize_t cashd_write(struct file *filp, const char __user *buf,
  143. size_t count, loff_t *f_pos)
  144. {
  145. struct cashd_device *dev = filp->private_data;
  146. int ret;
  147. unsigned int value = 0;
  148. if (!dev)
  149. return -EINVAL;
  150. if(count > BUFFER_SIZE)
  151. {
  152. count = BUFFER_SIZE;
  153. }
  154. ret = copy_from_user(dev->buffer, buf, count);
  155. if(ret != 0)
  156. {
  157. return -EFAULT;
  158. }
  159. if(dev->buffer[0] == 'o' || dev->buffer[0] == 'O')
  160. {
  161. // printk("%s %s %d minor=%d\n", __FILE__, __func__, __LINE__, dev->dev_minor);
  162. value = readl(dev->gpio_ctl_reg_base);
  163. value |= 0x1;
  164. writel(value, dev->gpio_ctl_reg_base);
  165. schedule_delayed_work(&dev->delay_work1, msecs_to_jiffies(300));
  166. }
  167. else
  168. {
  169. printk(KERN_INFO "cashd: Invalid input\n");
  170. }
  171. return count;
  172. }
  173. static unsigned int cashd_poll(struct file *filp, poll_table *wait)
  174. {
  175. struct cashd_device *dev = filp->private_data;
  176. unsigned int mask = 0;
  177. if (!dev)
  178. return POLLERR;
  179. poll_wait(filp, &dev->read_wait, wait);
  180. mutex_lock(&dev->lock);
  181. if(dev->status_changed)
  182. mask |= POLLIN | POLLRDNORM;
  183. mutex_unlock(&dev->lock);
  184. return mask;
  185. }
  186. static const struct file_operations cashd_fops = {
  187. .owner = THIS_MODULE,
  188. .open = cashd_open,
  189. .release = cashd_release,
  190. .read = cashd_read,
  191. .write = cashd_write,
  192. .poll = cashd_poll,
  193. };
  194. static int __init cashd_init_device(struct cashd_device *dev, int minor)
  195. {
  196. int ret;
  197. dev->buffer = kmalloc(BUFFER_SIZE, GFP_KERNEL);
  198. if (!dev->buffer) {
  199. pr_err("cashd: Failed to allocate buffer for minor %d\n", minor);
  200. return -ENOMEM;
  201. }
  202. memset(dev->buffer, 0, BUFFER_SIZE);
  203. dev->buffer_size = BUFFER_SIZE;
  204. dev->dev_major = cashd_major;
  205. dev->dev_minor = minor;
  206. init_waitqueue_head(&dev->read_wait);
  207. mutex_init(&dev->lock);
  208. INIT_DELAYED_WORK(&dev->delay_work1, gpio_ctl_delay_work_func);
  209. INIT_DELAYED_WORK(&dev->delay_work2, gpio_status_delay_work_func);
  210. cdev_init(&dev->cdev, &cashd_fops);
  211. dev->cdev.owner = THIS_MODULE;
  212. ret = cdev_add(&dev->cdev, MKDEV(cashd_major, minor), 1);
  213. if (ret) {
  214. pr_err("cashd: Failed to add cdev for minor %d\n", minor);
  215. kfree(dev->buffer);
  216. return ret;
  217. }
  218. return 0;
  219. }
  220. static char *my_devnode(struct device *dev, umode_t *mode) {
  221. if (mode) {
  222. *mode = 0666;
  223. }
  224. return NULL;
  225. }
  226. int cashd_init(void)
  227. {
  228. int ret;
  229. int i;
  230. struct device *device;
  231. cashd_major = 56;
  232. cashd_class = class_create(THIS_MODULE, CLASS_NAME);
  233. if (IS_ERR(cashd_class)) {
  234. ret = PTR_ERR(cashd_class);
  235. pr_err("cashd: Failed to create class\n");
  236. goto fail_class;
  237. }
  238. cashd_class->devnode = my_devnode;
  239. for (i = 0; i < MINOR_COUNT; i++) {
  240. cashd_devices[i] = kzalloc(sizeof(struct cashd_device), GFP_KERNEL);
  241. if (!cashd_devices[i]) {
  242. pr_err("cashd: Failed to allocate device for minor %d\n", i);
  243. ret = -ENOMEM;
  244. goto fail_devices;
  245. }
  246. ret = cashd_init_device(cashd_devices[i], minors[i]);
  247. if (ret) {
  248. pr_err("cashd: Failed to init device for minor %d\n", i);
  249. kfree(cashd_devices[i]);
  250. cashd_devices[i] = NULL;
  251. goto fail_devices;
  252. }
  253. device = device_create(cashd_class, NULL,
  254. MKDEV(cashd_major, minors[i]), NULL,
  255. "cashd%d", i);
  256. if (IS_ERR(device)) {
  257. ret = PTR_ERR(device);
  258. pr_err("cashd: Failed to create device for minor %d\n", i);
  259. cdev_del(&cashd_devices[i]->cdev);
  260. kfree(cashd_devices[i]);
  261. cashd_devices[i] = NULL;
  262. goto fail_devices;
  263. }
  264. if(i == 0)
  265. {
  266. cashd_devices[i]->gpio_ctl_reg_base = ioremap(GPIO0_CTL, 4);
  267. cashd_devices[i]->gpio_status_reg_base = ioremap(GPIO0_STATUS, 4);
  268. }
  269. else if(i == 1)
  270. {
  271. cashd_devices[i]->gpio_ctl_reg_base = ioremap(GPIO1_CTL, 4);
  272. cashd_devices[i]->gpio_status_reg_base = ioremap(GPIO1_STATUS, 4);
  273. }
  274. schedule_delayed_work(&cashd_devices[i]->delay_work2, msecs_to_jiffies(100));
  275. pr_info("cashd: Created /dev/cashd%d\n", i);
  276. }
  277. pr_info("cashd: Driver initialized successfully\n");
  278. return 0;
  279. fail_devices:
  280. for (i = 0; i < MINOR_COUNT; i++) {
  281. if (cashd_devices[i]) {
  282. device_destroy(cashd_class, MKDEV(cashd_major, minors[i]));
  283. cdev_del(&cashd_devices[i]->cdev);
  284. if (cashd_devices[i]->buffer)
  285. kfree(cashd_devices[i]->buffer);
  286. kfree(cashd_devices[i]);
  287. }
  288. }
  289. class_destroy(cashd_class);
  290. fail_class:
  291. unregister_chrdev_region(MKDEV(cashd_major, MINOR_BASE), MINOR_COUNT);
  292. return ret;
  293. }
  294. void cashd_exit(void)
  295. {
  296. int i;
  297. pr_info("cashd: Cleaning up driver\n");
  298. for (i = 0; i < MINOR_COUNT; i++) {
  299. if (cashd_devices[i]) {
  300. cancel_delayed_work_sync(&cashd_devices[i]->delay_work1);
  301. cancel_delayed_work_sync(&cashd_devices[i]->delay_work2);
  302. device_destroy(cashd_class, MKDEV(cashd_major, minors[i]));
  303. cdev_del(&cashd_devices[i]->cdev);
  304. if (cashd_devices[i]->buffer)
  305. kfree(cashd_devices[i]->buffer);
  306. iounmap(cashd_devices[i]->gpio_ctl_reg_base);
  307. iounmap(cashd_devices[i]->gpio_status_reg_base);
  308. kfree(cashd_devices[i]);
  309. pr_info("cashd: Removed /dev/cashd%d\n", i);
  310. }
  311. }
  312. if (cashd_class)
  313. class_destroy(cashd_class);
  314. unregister_chrdev_region(MKDEV(cashd_major, MINOR_BASE), MINOR_COUNT);
  315. pr_info("cashd: Driver cleaned up\n");
  316. }