switches.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. #include <linux/init.h>
  2. #include <linux/module.h>
  3. #include <linux/fs.h>
  4. #include <linux/cdev.h>
  5. #include <linux/slab.h>
  6. #include <linux/uaccess.h>
  7. #include <linux/device.h>
  8. #include <linux/stat.h>
  9. #include <linux/poll.h>
  10. #include <linux/sched.h>
  11. #include <linux/timer.h>
  12. #include <linux/workqueue.h>
  13. #include <linux/io.h>
  14. #include "gpioregs.h"
  15. #define DEVICE_NAME "switches"
  16. #define CLASS_NAME "switches_class"
  17. #define BUFFER_SIZE 1024
  18. /*
  19. F12 0xFD6A0940 bit1
  20. F16 0xFD6A0980 bit1
  21. F17 0xFD6A0990 bit1
  22. F18 0xFD6A09A0 bit1
  23. ID0-D0 0xFD6D0A40 bit1
  24. ID1-D1 0xFD6D0A50 bit1
  25. ID2-D14 0xFD6D0B20 bit1
  26. ID3-D15 0xFD6D0B30 bit1
  27. ID4-D16 0xFD6D0B40 bit1
  28. */
  29. #define SWITCH_F12 0xFD6A0940
  30. #define SWITCH_F16 0xFD6A0980
  31. #define SWITCH_F17 0xFD6A0990
  32. #define SWITCH_F18 0xFD6A09A0
  33. #define SWITCH_ID0 0xFD6D0A40
  34. #define SWITCH_ID1 0xFD6D0A50
  35. #define SWITCH_ID2 0xFD6D0B20
  36. #define SWITCH_ID3 0xFD6D0B30
  37. #define SWITCH_ID4 0xFD6D0B40
  38. static int switches_major = 56;
  39. static int switches_minor = 71;
  40. module_param(switches_major, int, S_IRUGO);
  41. module_param(switches_minor, int, S_IRUGO);
  42. MODULE_PARM_DESC(switches_major, "Major device number");
  43. MODULE_PARM_DESC(switches_minor, "Minor device number");
  44. static struct class *char_class = NULL;
  45. static struct device *char_device = NULL;
  46. // static struct cdev my_cdev;
  47. static dev_t dev_num;
  48. struct switches_dev
  49. {
  50. char *buffer;
  51. size_t size;
  52. struct mutex lock;
  53. struct cdev cdev;
  54. wait_queue_head_t read_wait;
  55. struct delayed_work delay_work1;
  56. unsigned int switch_status[5];
  57. };
  58. static struct switches_dev *dev = NULL;
  59. unsigned int switch_readl(unsigned int addr)
  60. {
  61. unsigned int val;
  62. void __iomem *reg_base = NULL;
  63. reg_base = ioremap(addr, 4);
  64. val = readl(reg_base);
  65. iounmap(reg_base);
  66. return val;
  67. }
  68. int get_switch_gpio_status(void)
  69. {
  70. dev->switch_status[0] = 0;
  71. dev->switch_status[1] = 0;
  72. dev->switch_status[0] |= (switch_readl(SWITCH_F12)&0x02) >> 1;
  73. dev->switch_status[0] |= switch_readl(SWITCH_F16)&0x02;
  74. dev->switch_status[0] |= (switch_readl(SWITCH_F17)&0x02) << 1;
  75. dev->switch_status[0] |= (switch_readl(SWITCH_F18)%0x02) << 2;
  76. dev->switch_status[1] |= (switch_readl(SWITCH_ID0)&0x02) >> 1;
  77. dev->switch_status[1] |= switch_readl(SWITCH_ID1)&0x02;
  78. dev->switch_status[1] |= (switch_readl(SWITCH_ID2)&0x02) << 1;
  79. dev->switch_status[1] |= (switch_readl(SWITCH_ID3)%0x02) << 2;
  80. dev->switch_status[1] |= (switch_readl(SWITCH_ID4)%0x02) << 3;
  81. return 0;
  82. }
  83. static void delay_work_func(struct work_struct *work)
  84. {
  85. printk(KERN_INFO "delay_work_func\n");
  86. }
  87. static int switches_open(struct inode *inode, struct file *filp)
  88. {
  89. struct switches_dev *dev;
  90. dev = container_of(inode->i_cdev, struct switches_dev, cdev);
  91. filp->private_data = dev;
  92. printk(KERN_INFO "switches: Device opened (major=%d, minor=%d)\n",
  93. imajor(inode), iminor(inode));
  94. return 0;
  95. }
  96. static int switches_release(struct inode *inode, struct file *filp)
  97. {
  98. // release_region(PORT_80, 1);
  99. printk(KERN_INFO "switches: Device closed\n");
  100. return 0;
  101. }
  102. static ssize_t switches_read(struct file *filp, char __user *buf,
  103. size_t count, loff_t *f_pos)
  104. {
  105. struct switches_dev *dev = filp->private_data;
  106. // ssize_t retval = 0;
  107. // size_t available;
  108. // int read_count = 0;
  109. int ret = 0;
  110. int i = 0;
  111. // ret = copy_to_user(buf, dev->switch_status, read_count);
  112. // if (ret != 0)
  113. // {
  114. // printk(KERN_INFO "switches: copy_to_user failed\n");
  115. // goto out;
  116. // }
  117. printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
  118. ret = get_switch_gpio_status();
  119. for(i = 0; i < 5; i++)
  120. {
  121. printk("switch_status[%d]=%08x\n", i, dev->switch_status[i]);
  122. }
  123. // printk(KERN_INFO "switches: Read %zu bytes\n", count);
  124. return ret;
  125. }
  126. static ssize_t switches_write(struct file *filp, const char __user *buf,
  127. size_t count, loff_t *f_pos)
  128. {
  129. return -1;
  130. }
  131. static unsigned int switches_poll(struct file *filp, poll_table *wait)
  132. {
  133. // poll_wait(filp, &dev->read_wait, wait);
  134. return 0;
  135. }
  136. static struct file_operations fops = {
  137. .owner = THIS_MODULE,
  138. .open = switches_open,
  139. .release = switches_release,
  140. .read = switches_read,
  141. .write = switches_write,
  142. .poll = switches_poll,
  143. };
  144. int switches_init(void)
  145. {
  146. int result;
  147. printk(KERN_INFO "switches: Initializing driver with major=%d, minor=%d\n",
  148. switches_major, switches_minor);
  149. if (switches_major <= 0)
  150. {
  151. printk(KERN_ALERT "switches: Invalid major number %d\n", switches_major);
  152. return -EINVAL;
  153. }
  154. dev_num = MKDEV(switches_major, switches_minor);
  155. result = register_chrdev_region(dev_num, 1, DEVICE_NAME);
  156. if (result < 0)
  157. {
  158. printk(KERN_ALERT "switches: Failed to register major number %d\n", switches_major);
  159. printk(KERN_ALERT "switches: Try using a different major number\n");
  160. return result;
  161. }
  162. printk(KERN_INFO "switches: Registered with major=%d, minor=%d\n",
  163. MAJOR(dev_num), MINOR(dev_num));
  164. dev = kmalloc(sizeof(struct switches_dev), GFP_KERNEL);
  165. if (!dev)
  166. {
  167. result = -ENOMEM;
  168. goto fail_malloc;
  169. }
  170. memset(dev, 0, sizeof(struct switches_dev));
  171. init_waitqueue_head(&dev->read_wait);
  172. dev->buffer = kmalloc(BUFFER_SIZE, GFP_KERNEL);
  173. if (!dev->buffer)
  174. {
  175. result = -ENOMEM;
  176. goto fail_buffer;
  177. }
  178. mutex_init(&dev->lock);
  179. INIT_DELAYED_WORK(&dev->delay_work1, delay_work_func);
  180. cdev_init(&dev->cdev, &fops);
  181. dev->cdev.owner = THIS_MODULE;
  182. result = cdev_add(&dev->cdev, dev_num, 1);
  183. if (result)
  184. {
  185. printk(KERN_ALERT "switches: Failed to add cdev\n");
  186. goto fail_cdev;
  187. }
  188. char_class = class_create(THIS_MODULE, CLASS_NAME);
  189. if (IS_ERR(char_class))
  190. {
  191. result = PTR_ERR(char_class);
  192. printk(KERN_ALERT "switches: Failed to create class\n");
  193. goto fail_class;
  194. }
  195. char_device = device_create(char_class, NULL, dev_num, NULL, DEVICE_NAME);
  196. if (IS_ERR(char_device))
  197. {
  198. result = PTR_ERR(char_device);
  199. printk(KERN_ALERT "switches: Failed to create device\n");
  200. goto fail_device;
  201. }
  202. printk(KERN_INFO "switches: Driver initialized successfully\n");
  203. printk(KERN_INFO "switches: Device node: /dev/%s (major=%d, minor=%d)\n",
  204. DEVICE_NAME, switches_major, switches_minor);
  205. return 0;
  206. fail_device:
  207. class_destroy(char_class);
  208. fail_class:
  209. cdev_del(&dev->cdev);
  210. fail_cdev:
  211. kfree(dev->buffer);
  212. fail_buffer:
  213. kfree(dev);
  214. fail_malloc:
  215. unregister_chrdev_region(dev_num, 1);
  216. return result;
  217. }
  218. void switches_exit(void)
  219. {
  220. cancel_delayed_work_sync(&dev->delay_work1);
  221. device_destroy(char_class, dev_num);
  222. class_destroy(char_class);
  223. if (dev)
  224. {
  225. cdev_del(&dev->cdev);
  226. if (dev->buffer)
  227. kfree(dev->buffer);
  228. kfree(dev);
  229. }
  230. unregister_chrdev_region(dev_num, 1);
  231. printk(KERN_INFO "switches: Driver removed (major=%d, minor=%d)\n",
  232. switches_major, switches_minor);
  233. }