switches.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. // 这里需要待完善,确认熊工提供的地址和API文档的对应关系
  72. dev->switch_status[0] = readl(SWITCH_F12);
  73. return 0;
  74. }
  75. static void delay_work_func(struct work_struct *work)
  76. {
  77. printk(KERN_INFO "delay_work_func\n");
  78. }
  79. // 文件打开操作
  80. static int switches_open(struct inode *inode, struct file *filp)
  81. {
  82. struct switches_dev *dev;
  83. dev = container_of(inode->i_cdev, struct switches_dev, cdev);
  84. filp->private_data = dev;
  85. printk(KERN_INFO "switches: Device opened (major=%d, minor=%d)\n",
  86. imajor(inode), iminor(inode));
  87. return 0;
  88. }
  89. // 文件释放操作
  90. static int switches_release(struct inode *inode, struct file *filp)
  91. {
  92. // release_region(PORT_80, 1);
  93. printk(KERN_INFO "switches: Device closed\n");
  94. return 0;
  95. }
  96. // 读操作 - 支持cat命令
  97. static ssize_t switches_read(struct file *filp, char __user *buf,
  98. size_t count, loff_t *f_pos)
  99. {
  100. struct switches_dev *dev = filp->private_data;
  101. ssize_t retval = 0;
  102. size_t available;
  103. int read_count = 0;
  104. int ret = 0;
  105. if (mutex_lock_interruptible(&dev->lock))
  106. return -ERESTARTSYS;
  107. // 这里read_count没有固定为4个字节,是为了便于后续扩展
  108. if (count > dev->size)
  109. {
  110. read_count = dev->size;
  111. }
  112. else
  113. {
  114. read_count = count;
  115. }
  116. ret = copy_to_user(buf, dev->buffer, read_count);
  117. if (ret != 0)
  118. {
  119. printk(KERN_INFO "switches: copy_to_user failed\n");
  120. goto out;
  121. }
  122. printk(KERN_INFO "switches: Read %zu bytes\n", count);
  123. out:
  124. mutex_unlock(&dev->lock);
  125. return ret;
  126. }
  127. // 写操作
  128. static ssize_t switches_write(struct file *filp, const char __user *buf,
  129. size_t count, loff_t *f_pos)
  130. {
  131. struct switches_dev *dev = filp->private_data;
  132. ssize_t retval = 0;
  133. size_t available;
  134. int ret = 0;
  135. // 加锁
  136. if (mutex_lock_interruptible(&dev->lock))
  137. {
  138. return -ERESTARTSYS;
  139. }
  140. // 计算可写数据量
  141. if (count > BUFFER_SIZE)
  142. {
  143. count = BUFFER_SIZE;
  144. }
  145. // 拷贝数据
  146. ret = copy_from_user(dev->buffer, buf, count);
  147. if (ret != 0)
  148. {
  149. printk(KERN_INFO "switches: copy_from_user failed\n");
  150. goto out;
  151. }
  152. dev->size = count;
  153. printk(KERN_INFO "switches: Written %zu bytes\n", count);
  154. out:
  155. mutex_unlock(&dev->lock);
  156. // 调度延迟工作
  157. if (ret == 0)
  158. {
  159. schedule_delayed_work(&dev->delay_work1, msecs_to_jiffies(10));
  160. }
  161. return count;
  162. }
  163. static unsigned int switches_poll(struct file *filp, poll_table *wait)
  164. {
  165. // poll_wait(filp, &dev->read_wait, wait);
  166. return 0;
  167. }
  168. // 文件操作结构体
  169. static struct file_operations fops = {
  170. .owner = THIS_MODULE,
  171. .open = switches_open,
  172. .release = switches_release,
  173. .read = switches_read,
  174. .write = switches_write,
  175. .poll = switches_poll,
  176. };
  177. // 模块初始化
  178. int switches_init(void)
  179. {
  180. int result;
  181. printk(KERN_INFO "switches: Initializing driver with major=%d, minor=%d\n",
  182. switches_major, switches_minor);
  183. // 检查主设备号是否有效
  184. if (switches_major <= 0)
  185. {
  186. printk(KERN_ALERT "switches: Invalid major number %d\n", switches_major);
  187. return -EINVAL;
  188. }
  189. // 构建设备号
  190. dev_num = MKDEV(switches_major, switches_minor);
  191. // 注册设备号 - 使用指定的主设备号
  192. result = register_chrdev_region(dev_num, 1, DEVICE_NAME);
  193. if (result < 0)
  194. {
  195. printk(KERN_ALERT "switches: Failed to register major number %d\n", switches_major);
  196. printk(KERN_ALERT "switches: Try using a different major number\n");
  197. return result;
  198. }
  199. printk(KERN_INFO "switches: Registered with major=%d, minor=%d\n",
  200. MAJOR(dev_num), MINOR(dev_num));
  201. // 分配设备结构体
  202. dev = kmalloc(sizeof(struct switches_dev), GFP_KERNEL);
  203. if (!dev)
  204. {
  205. result = -ENOMEM;
  206. goto fail_malloc;
  207. }
  208. memset(dev, 0, sizeof(struct switches_dev));
  209. init_waitqueue_head(&dev->read_wait);
  210. // 分配缓冲区
  211. dev->buffer = kmalloc(BUFFER_SIZE, GFP_KERNEL);
  212. if (!dev->buffer)
  213. {
  214. result = -ENOMEM;
  215. goto fail_buffer;
  216. }
  217. // 初始化互斥锁
  218. mutex_init(&dev->lock);
  219. INIT_DELAYED_WORK(&dev->delay_work1, delay_work_func);
  220. // 初始化字符设备
  221. cdev_init(&dev->cdev, &fops);
  222. dev->cdev.owner = THIS_MODULE;
  223. // 添加字符设备到系统
  224. result = cdev_add(&dev->cdev, dev_num, 1);
  225. if (result)
  226. {
  227. printk(KERN_ALERT "switches: Failed to add cdev\n");
  228. goto fail_cdev;
  229. }
  230. // 创建设备类
  231. char_class = class_create(THIS_MODULE, CLASS_NAME);
  232. if (IS_ERR(char_class))
  233. {
  234. result = PTR_ERR(char_class);
  235. printk(KERN_ALERT "switches: Failed to create class\n");
  236. goto fail_class;
  237. }
  238. // 创建设备
  239. char_device = device_create(char_class, NULL, dev_num, NULL, DEVICE_NAME);
  240. if (IS_ERR(char_device))
  241. {
  242. result = PTR_ERR(char_device);
  243. printk(KERN_ALERT "switches: Failed to create device\n");
  244. goto fail_device;
  245. }
  246. printk(KERN_INFO "switches: Driver initialized successfully\n");
  247. printk(KERN_INFO "switches: Device node: /dev/%s (major=%d, minor=%d)\n",
  248. DEVICE_NAME, switches_major, switches_minor);
  249. return 0;
  250. fail_device:
  251. class_destroy(char_class);
  252. fail_class:
  253. cdev_del(&dev->cdev);
  254. fail_cdev:
  255. kfree(dev->buffer);
  256. fail_buffer:
  257. kfree(dev);
  258. fail_malloc:
  259. unregister_chrdev_region(dev_num, 1);
  260. return result;
  261. }
  262. // 模块退出
  263. void switches_exit(void)
  264. {
  265. cancel_delayed_work_sync(&dev->delay_work1);
  266. device_destroy(char_class, dev_num);
  267. class_destroy(char_class);
  268. if (dev)
  269. {
  270. cdev_del(&dev->cdev);
  271. if (dev->buffer)
  272. kfree(dev->buffer);
  273. kfree(dev);
  274. }
  275. unregister_chrdev_region(dev_num, 1);
  276. printk(KERN_INFO "switches: Driver removed (major=%d, minor=%d)\n",
  277. switches_major, switches_minor);
  278. }