switches.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. void __iomem *reg_base = NULL;
  62. reg_base = ioremap(addr, 0x1000);
  63. return readl(reg_base);
  64. }
  65. int get_switch_gpio_status(void)
  66. {
  67. dev->switch_status[0] = 0;
  68. dev->switch_status[1] = 0;
  69. dev->switch_status[0] |= (switch_readl(SWITCH_F12)&0x02) >> 1;
  70. dev->switch_status[0] |= switch_readl(SWITCH_F16)&0x02;
  71. dev->switch_status[0] |= (switch_readl(SWITCH_F17)&0x02) << 1;
  72. dev->switch_status[0] |= (switch_readl(SWITCH_F18)%0x02) << 2;
  73. dev->switch_status[1] |= (switch_readl(SWITCH_ID0)&0x02) >> 1;
  74. dev->switch_status[1] |= switch_readl(SWITCH_ID1)&0x02;
  75. dev->switch_status[1] |= (switch_readl(SWITCH_ID2)&0x02) << 1;
  76. dev->switch_status[1] |= (switch_readl(SWITCH_ID3)%0x02) << 2;
  77. dev->switch_status[1] |= (switch_readl(SWITCH_ID4)%0x02) << 3;
  78. return 0;
  79. }
  80. static void delay_work_func(struct work_struct *work)
  81. {
  82. printk(KERN_INFO "delay_work_func\n");
  83. }
  84. static int switches_open(struct inode *inode, struct file *filp)
  85. {
  86. struct switches_dev *dev;
  87. dev = container_of(inode->i_cdev, struct switches_dev, cdev);
  88. filp->private_data = dev;
  89. printk(KERN_INFO "switches: Device opened (major=%d, minor=%d)\n",
  90. imajor(inode), iminor(inode));
  91. return 0;
  92. }
  93. static int switches_release(struct inode *inode, struct file *filp)
  94. {
  95. // release_region(PORT_80, 1);
  96. printk(KERN_INFO "switches: Device closed\n");
  97. return 0;
  98. }
  99. static ssize_t switches_read(struct file *filp, char __user *buf,
  100. size_t count, loff_t *f_pos)
  101. {
  102. struct switches_dev *dev = filp->private_data;
  103. ssize_t retval = 0;
  104. size_t available;
  105. int read_count = 0;
  106. int ret = 0;
  107. int i = 0;
  108. // ret = copy_to_user(buf, dev->switch_status, read_count);
  109. // if (ret != 0)
  110. // {
  111. // printk(KERN_INFO "switches: copy_to_user failed\n");
  112. // goto out;
  113. // }
  114. printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
  115. ret = get_switch_gpio_status();
  116. for(i = 0; i < 5; i++)
  117. {
  118. printk("switch_status[%d]=%08x\n", i, dev->switch_status[i]);
  119. }
  120. // printk(KERN_INFO "switches: Read %zu bytes\n", count);
  121. return ret;
  122. }
  123. static ssize_t switches_write(struct file *filp, const char __user *buf,
  124. size_t count, loff_t *f_pos)
  125. {
  126. return -1;
  127. }
  128. static unsigned int switches_poll(struct file *filp, poll_table *wait)
  129. {
  130. // poll_wait(filp, &dev->read_wait, wait);
  131. return 0;
  132. }
  133. static struct file_operations fops = {
  134. .owner = THIS_MODULE,
  135. .open = switches_open,
  136. .release = switches_release,
  137. .read = switches_read,
  138. .write = switches_write,
  139. .poll = switches_poll,
  140. };
  141. int switches_init(void)
  142. {
  143. int result;
  144. printk(KERN_INFO "switches: Initializing driver with major=%d, minor=%d\n",
  145. switches_major, switches_minor);
  146. if (switches_major <= 0)
  147. {
  148. printk(KERN_ALERT "switches: Invalid major number %d\n", switches_major);
  149. return -EINVAL;
  150. }
  151. dev_num = MKDEV(switches_major, switches_minor);
  152. result = register_chrdev_region(dev_num, 1, DEVICE_NAME);
  153. if (result < 0)
  154. {
  155. printk(KERN_ALERT "switches: Failed to register major number %d\n", switches_major);
  156. printk(KERN_ALERT "switches: Try using a different major number\n");
  157. return result;
  158. }
  159. printk(KERN_INFO "switches: Registered with major=%d, minor=%d\n",
  160. MAJOR(dev_num), MINOR(dev_num));
  161. dev = kmalloc(sizeof(struct switches_dev), GFP_KERNEL);
  162. if (!dev)
  163. {
  164. result = -ENOMEM;
  165. goto fail_malloc;
  166. }
  167. memset(dev, 0, sizeof(struct switches_dev));
  168. init_waitqueue_head(&dev->read_wait);
  169. dev->buffer = kmalloc(BUFFER_SIZE, GFP_KERNEL);
  170. if (!dev->buffer)
  171. {
  172. result = -ENOMEM;
  173. goto fail_buffer;
  174. }
  175. mutex_init(&dev->lock);
  176. INIT_DELAYED_WORK(&dev->delay_work1, delay_work_func);
  177. cdev_init(&dev->cdev, &fops);
  178. dev->cdev.owner = THIS_MODULE;
  179. result = cdev_add(&dev->cdev, dev_num, 1);
  180. if (result)
  181. {
  182. printk(KERN_ALERT "switches: Failed to add cdev\n");
  183. goto fail_cdev;
  184. }
  185. char_class = class_create(THIS_MODULE, CLASS_NAME);
  186. if (IS_ERR(char_class))
  187. {
  188. result = PTR_ERR(char_class);
  189. printk(KERN_ALERT "switches: Failed to create class\n");
  190. goto fail_class;
  191. }
  192. char_device = device_create(char_class, NULL, dev_num, NULL, DEVICE_NAME);
  193. if (IS_ERR(char_device))
  194. {
  195. result = PTR_ERR(char_device);
  196. printk(KERN_ALERT "switches: Failed to create device\n");
  197. goto fail_device;
  198. }
  199. printk(KERN_INFO "switches: Driver initialized successfully\n");
  200. printk(KERN_INFO "switches: Device node: /dev/%s (major=%d, minor=%d)\n",
  201. DEVICE_NAME, switches_major, switches_minor);
  202. return 0;
  203. fail_device:
  204. class_destroy(char_class);
  205. fail_class:
  206. cdev_del(&dev->cdev);
  207. fail_cdev:
  208. kfree(dev->buffer);
  209. fail_buffer:
  210. kfree(dev);
  211. fail_malloc:
  212. unregister_chrdev_region(dev_num, 1);
  213. return result;
  214. }
  215. void switches_exit(void)
  216. {
  217. cancel_delayed_work_sync(&dev->delay_work1);
  218. device_destroy(char_class, dev_num);
  219. class_destroy(char_class);
  220. if (dev)
  221. {
  222. cdev_del(&dev->cdev);
  223. if (dev->buffer)
  224. kfree(dev->buffer);
  225. kfree(dev);
  226. }
  227. unregister_chrdev_region(dev_num, 1);
  228. printk(KERN_INFO "switches: Driver removed (major=%d, minor=%d)\n",
  229. switches_major, switches_minor);
  230. }