switches.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. 拨码
  20. F12 0xFD6A0940 bit1
  21. F16 0xFD6A0980 bit1
  22. F17 0xFD6A0990 bit1
  23. F18 0xFD6A09A0 bit1
  24. 跳帽
  25. ID0-D0 0xFD6D0A40 bit1
  26. ID1-D1 0xFD6D0A50 bit1
  27. ID2-D14 0xFD6D0B20 bit1
  28. ID3-D15 0xFD6D0B30 bit1
  29. ID4-D16 0xFD6D0B40 bit1
  30. */
  31. #define SWITCH_F12 0xFD6A0940
  32. #define SWITCH_F16 0xFD6A0980
  33. #define SWITCH_F17 0xFD6A0990
  34. #define SWITCH_F18 0xFD6A09A0
  35. #define SWITCH_ID0 0xFD6D0A40
  36. #define SWITCH_ID1 0xFD6D0A50
  37. #define SWITCH_ID2 0xFD6D0B20
  38. #define SWITCH_ID3 0xFD6D0B30
  39. #define SWITCH_ID4 0xFD6D0B40
  40. // 可以通过模块参数指定主设备号和次设备号
  41. static int switches_major = 56; // 默认主设备号
  42. static int switches_minor = 71; // 默认次设备号
  43. module_param(switches_major, int, S_IRUGO);
  44. module_param(switches_minor, int, S_IRUGO);
  45. MODULE_PARM_DESC(switches_major, "Major device number");
  46. MODULE_PARM_DESC(switches_minor, "Minor device number");
  47. static struct class *char_class = NULL;
  48. static struct device *char_device = NULL;
  49. static struct cdev my_cdev;
  50. static dev_t dev_num;
  51. // 设备结构体
  52. struct switches_dev
  53. {
  54. char *buffer;
  55. size_t size;
  56. struct mutex lock;
  57. struct cdev cdev;
  58. wait_queue_head_t read_wait;
  59. struct delayed_work delay_work1;
  60. unsigned int switch_status[5];
  61. };
  62. static struct switches_dev *dev = NULL;
  63. unsigned int switch_readl(unsigned int addr)
  64. {
  65. void __iomem *reg_base = NULL;
  66. reg_base = ioremap(addr, 0x1000);
  67. return readl(reg_base);
  68. }
  69. int get_switch_gpio_status(void)
  70. {
  71. dev->switch_status[0] = 0;
  72. dev->switch_status[1] = 0;
  73. // 这里需要待完善,确认熊工提供的地址和API文档的对应关系
  74. dev->switch_status[0] |= (switch_readl(SWITCH_F12)&0x02) >> 1;
  75. dev->switch_status[0] |= switch_readl(SWITCH_F16)&0x02;
  76. dev->switch_status[0] |= (switch_readl(SWITCH_F17)&0x02) << 1;
  77. dev->switch_status[0] |= (switch_readl(SWITCH_F18)%0x02) << 2;
  78. dev->switch_status[1] |= (switch_readl(SWITCH_ID0)&0x02) >> 1;
  79. dev->switch_status[1] |= switch_readl(SWITCH_ID1)&0x02;
  80. dev->switch_status[1] |= (switch_readl(SWITCH_ID2)&0x02) << 1;
  81. dev->switch_status[1] |= (switch_readl(SWITCH_ID3)%0x02) << 2;
  82. dev->switch_status[1] |= (switch_readl(SWITCH_ID4)%0x02) << 3;
  83. return 0;
  84. }
  85. static void delay_work_func(struct work_struct *work)
  86. {
  87. printk(KERN_INFO "delay_work_func\n");
  88. }
  89. // 文件打开操作
  90. static int switches_open(struct inode *inode, struct file *filp)
  91. {
  92. struct switches_dev *dev;
  93. dev = container_of(inode->i_cdev, struct switches_dev, cdev);
  94. filp->private_data = dev;
  95. printk(KERN_INFO "switches: Device opened (major=%d, minor=%d)\n",
  96. imajor(inode), iminor(inode));
  97. return 0;
  98. }
  99. // 文件释放操作
  100. static int switches_release(struct inode *inode, struct file *filp)
  101. {
  102. // release_region(PORT_80, 1);
  103. printk(KERN_INFO "switches: Device closed\n");
  104. return 0;
  105. }
  106. // 读操作 - 支持cat命令
  107. static ssize_t switches_read(struct file *filp, char __user *buf,
  108. size_t count, loff_t *f_pos)
  109. {
  110. struct switches_dev *dev = filp->private_data;
  111. ssize_t retval = 0;
  112. size_t available;
  113. int read_count = 0;
  114. int ret = 0;
  115. int i = 0;
  116. // ret = copy_to_user(buf, dev->switch_status, read_count);
  117. // if (ret != 0)
  118. // {
  119. // printk(KERN_INFO "switches: copy_to_user failed\n");
  120. // goto out;
  121. // }
  122. printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
  123. ret = get_switch_gpio_status();
  124. for(i = 0; i < 5; i++)
  125. {
  126. printk("switch_status[%d]=%08x\n", i, dev->switch_status[i]);
  127. }
  128. // printk(KERN_INFO "switches: Read %zu bytes\n", count);
  129. return ret;
  130. }
  131. // 写操作
  132. static ssize_t switches_write(struct file *filp, const char __user *buf,
  133. size_t count, loff_t *f_pos)
  134. {
  135. return -1;
  136. }
  137. static unsigned int switches_poll(struct file *filp, poll_table *wait)
  138. {
  139. // poll_wait(filp, &dev->read_wait, wait);
  140. return 0;
  141. }
  142. // 文件操作结构体
  143. static struct file_operations fops = {
  144. .owner = THIS_MODULE,
  145. .open = switches_open,
  146. .release = switches_release,
  147. .read = switches_read,
  148. .write = switches_write,
  149. .poll = switches_poll,
  150. };
  151. // 模块初始化
  152. int switches_init(void)
  153. {
  154. int result;
  155. printk(KERN_INFO "switches: Initializing driver with major=%d, minor=%d\n",
  156. switches_major, switches_minor);
  157. // 检查主设备号是否有效
  158. if (switches_major <= 0)
  159. {
  160. printk(KERN_ALERT "switches: Invalid major number %d\n", switches_major);
  161. return -EINVAL;
  162. }
  163. // 构建设备号
  164. dev_num = MKDEV(switches_major, switches_minor);
  165. // 注册设备号 - 使用指定的主设备号
  166. result = register_chrdev_region(dev_num, 1, DEVICE_NAME);
  167. if (result < 0)
  168. {
  169. printk(KERN_ALERT "switches: Failed to register major number %d\n", switches_major);
  170. printk(KERN_ALERT "switches: Try using a different major number\n");
  171. return result;
  172. }
  173. printk(KERN_INFO "switches: Registered with major=%d, minor=%d\n",
  174. MAJOR(dev_num), MINOR(dev_num));
  175. // 分配设备结构体
  176. dev = kmalloc(sizeof(struct switches_dev), GFP_KERNEL);
  177. if (!dev)
  178. {
  179. result = -ENOMEM;
  180. goto fail_malloc;
  181. }
  182. memset(dev, 0, sizeof(struct switches_dev));
  183. init_waitqueue_head(&dev->read_wait);
  184. // 分配缓冲区
  185. dev->buffer = kmalloc(BUFFER_SIZE, GFP_KERNEL);
  186. if (!dev->buffer)
  187. {
  188. result = -ENOMEM;
  189. goto fail_buffer;
  190. }
  191. // 初始化互斥锁
  192. mutex_init(&dev->lock);
  193. INIT_DELAYED_WORK(&dev->delay_work1, delay_work_func);
  194. // 初始化字符设备
  195. cdev_init(&dev->cdev, &fops);
  196. dev->cdev.owner = THIS_MODULE;
  197. // 添加字符设备到系统
  198. result = cdev_add(&dev->cdev, dev_num, 1);
  199. if (result)
  200. {
  201. printk(KERN_ALERT "switches: Failed to add cdev\n");
  202. goto fail_cdev;
  203. }
  204. // 创建设备类
  205. char_class = class_create(THIS_MODULE, CLASS_NAME);
  206. if (IS_ERR(char_class))
  207. {
  208. result = PTR_ERR(char_class);
  209. printk(KERN_ALERT "switches: Failed to create class\n");
  210. goto fail_class;
  211. }
  212. // 创建设备
  213. char_device = device_create(char_class, NULL, dev_num, NULL, DEVICE_NAME);
  214. if (IS_ERR(char_device))
  215. {
  216. result = PTR_ERR(char_device);
  217. printk(KERN_ALERT "switches: Failed to create device\n");
  218. goto fail_device;
  219. }
  220. printk(KERN_INFO "switches: Driver initialized successfully\n");
  221. printk(KERN_INFO "switches: Device node: /dev/%s (major=%d, minor=%d)\n",
  222. DEVICE_NAME, switches_major, switches_minor);
  223. return 0;
  224. fail_device:
  225. class_destroy(char_class);
  226. fail_class:
  227. cdev_del(&dev->cdev);
  228. fail_cdev:
  229. kfree(dev->buffer);
  230. fail_buffer:
  231. kfree(dev);
  232. fail_malloc:
  233. unregister_chrdev_region(dev_num, 1);
  234. return result;
  235. }
  236. // 模块退出
  237. void switches_exit(void)
  238. {
  239. cancel_delayed_work_sync(&dev->delay_work1);
  240. device_destroy(char_class, dev_num);
  241. class_destroy(char_class);
  242. if (dev)
  243. {
  244. cdev_del(&dev->cdev);
  245. if (dev->buffer)
  246. kfree(dev->buffer);
  247. kfree(dev);
  248. }
  249. unregister_chrdev_region(dev_num, 1);
  250. printk(KERN_INFO "switches: Driver removed (major=%d, minor=%d)\n",
  251. switches_major, switches_minor);
  252. }