buzzer.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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/sched.h>
  10. #include <linux/timer.h>
  11. #include <linux/workqueue.h>
  12. #include <linux/io.h>
  13. #include "gpioregs.h"
  14. #define DEVICE_NAME "buzzer"
  15. #define CLASS_NAME "buzzer_class"
  16. #define DRIVER_NAME "buzzer_driver"
  17. #define BUFFER_SIZE 1024
  18. #define note(x) ((119318200+(x)/2)/(x))
  19. // 可以通过模块参数指定主设备号和次设备号
  20. static int buzzer_major = 56; // 默认主设备号
  21. static int buzzer_minor = 100; // 默认次设备号
  22. module_param(buzzer_major, int, S_IRUGO);
  23. module_param(buzzer_minor, int, S_IRUGO);
  24. MODULE_PARM_DESC(buzzer_major, "Major device number");
  25. MODULE_PARM_DESC(buzzer_minor, "Minor device number");
  26. static struct class *char_class = NULL;
  27. static struct device *char_device = NULL;
  28. static struct cdev my_cdev;
  29. static dev_t dev_num;
  30. // 设备结构体
  31. struct buzzer_dev
  32. {
  33. char *buffer;
  34. size_t size;
  35. struct mutex lock;
  36. struct cdev cdev;
  37. struct delayed_work delay_work1;
  38. };
  39. static struct buzzer_dev *dev = NULL;
  40. void IoWrite8 (uint16_t addr, uint8_t data)
  41. {
  42. outb(data,addr);
  43. }
  44. uint8_t IoRead8 (uint16_t addr)
  45. {
  46. return inb(addr);
  47. }
  48. void BeepOff(void)
  49. {
  50. IoWrite8(BUZZER_PORT, IoRead8(BUZZER_PORT) & 0xfc);
  51. }
  52. void BeepOn(uint16_t freq)
  53. {
  54. uint16_t Frequency = note(freq);
  55. //set up channel 1 (used for delays)
  56. IoWrite8(BUZZER_CHANNEL, 0x54);
  57. IoWrite8(BUZZER_CHAN_1, 0x12);
  58. //set up channel 2 (used by speaker)
  59. IoWrite8(BUZZER_CHANNEL, 0xb6);
  60. IoWrite8(BUZZER_FREQ, (uint8_t)Frequency);
  61. IoWrite8(BUZZER_FREQ, (uint8_t)(Frequency >> 8));
  62. //turn the speaker on
  63. IoWrite8(BUZZER_PORT, IoRead8(BUZZER_PORT) | 3);
  64. }
  65. static void delay_work_func(struct work_struct *work)
  66. {
  67. int status_flag = 0;
  68. int freq = 0;
  69. int duration = 0;
  70. if(dev->size < 4)
  71. {
  72. printk(KERN_INFO "buzzer: size < 4\n");
  73. return;
  74. }
  75. // 关闭buzzer
  76. BeepOff();
  77. printk(KERN_INFO "delay_work_func\n");
  78. }
  79. // 文件打开操作
  80. static int buzzer_open(struct inode *inode, struct file *filp)
  81. {
  82. struct buzzer_dev *dev;
  83. // if (!request_region(PORT_80, 1, DRIVER_NAME))
  84. // {
  85. // pr_err("Port 80 I/O region busy\n");
  86. // return -EBUSY;
  87. // }
  88. dev = container_of(inode->i_cdev, struct buzzer_dev, cdev);
  89. filp->private_data = dev;
  90. printk(KERN_INFO "buzzer: Device opened (major=%d, minor=%d)\n",
  91. imajor(inode), iminor(inode));
  92. return 0;
  93. }
  94. // 文件释放操作
  95. static int buzzer_release(struct inode *inode, struct file *filp)
  96. {
  97. // release_region(PORT_80, 1);
  98. printk(KERN_INFO "buzzer: Device closed\n");
  99. return 0;
  100. }
  101. // 读操作 - 支持cat命令
  102. static ssize_t buzzer_read(struct file *filp, char __user *buf,
  103. size_t count, loff_t *f_pos)
  104. {
  105. struct buzzer_dev *dev = filp->private_data;
  106. ssize_t retval = 0;
  107. size_t available;
  108. int read_count = 0;
  109. int ret = 0;
  110. if (mutex_lock_interruptible(&dev->lock))
  111. return -ERESTARTSYS;
  112. // 这里read_count没有固定为4个字节,是为了便于后续扩展
  113. if (count > dev->size)
  114. {
  115. read_count = dev->size;
  116. }
  117. else
  118. {
  119. read_count = count;
  120. }
  121. ret = copy_to_user(buf, dev->buffer, read_count);
  122. if (ret != 0)
  123. {
  124. printk(KERN_INFO "buzzer: copy_to_user failed\n");
  125. goto out;
  126. }
  127. printk(KERN_INFO "buzzer: Read %zu bytes\n", count);
  128. out:
  129. mutex_unlock(&dev->lock);
  130. return ret;
  131. }
  132. // 写操作
  133. static ssize_t buzzer_write(struct file *filp, const char __user *buf,
  134. size_t count, loff_t *f_pos)
  135. {
  136. struct buzzer_dev *dev = filp->private_data;
  137. ssize_t retval = 0;
  138. size_t available;
  139. int ret = 0;
  140. int status_flag = 0;
  141. int freq = 0;
  142. int duration = 0;
  143. // 加锁
  144. if (mutex_lock_interruptible(&dev->lock))
  145. {
  146. return -ERESTARTSYS;
  147. }
  148. // 计算可写数据量
  149. if (count > BUFFER_SIZE)
  150. {
  151. count = BUFFER_SIZE;
  152. }
  153. if(count < 4)
  154. {
  155. printk(KERN_INFO "buzzer: count < 4\n");
  156. ret = -1;
  157. goto out;
  158. }
  159. // 拷贝数据
  160. ret = copy_from_user(dev->buffer, buf, count);
  161. if (ret != 0)
  162. {
  163. printk(KERN_INFO "buzzer: copy_from_user failed\n");
  164. goto out;
  165. }
  166. dev->size = count;
  167. // 解析数据
  168. status_flag = dev->buffer[0];
  169. freq = dev->buffer[2]<<8 + dev->buffer[1];
  170. duration = dev->buffer[3];
  171. // 打开buzzer
  172. if(status_flag == 1)
  173. {
  174. BeepOn(freq);
  175. }
  176. else
  177. {
  178. BeepOff();
  179. }
  180. printk(KERN_INFO "buzzer: Written %zu bytes\n", count);
  181. out:
  182. mutex_unlock(&dev->lock);
  183. // 让buzzer工作duration秒
  184. if (ret == 0)
  185. {
  186. schedule_delayed_work(&dev->delay_work1, msecs_to_jiffies(duration*1000));
  187. }
  188. return count;
  189. }
  190. // 文件操作结构体
  191. static struct file_operations fops = {
  192. .owner = THIS_MODULE,
  193. .open = buzzer_open,
  194. .release = buzzer_release,
  195. .read = buzzer_read,
  196. .write = buzzer_write,
  197. };
  198. // 模块初始化
  199. int buzzer_init(void)
  200. {
  201. int result;
  202. printk(KERN_INFO "buzzer: Initializing driver with major=%d, minor=%d\n",
  203. buzzer_major, buzzer_minor);
  204. // 检查主设备号是否有效
  205. if (buzzer_major <= 0)
  206. {
  207. printk(KERN_ALERT "buzzer: Invalid major number %d\n", buzzer_major);
  208. return -EINVAL;
  209. }
  210. // 构建设备号
  211. dev_num = MKDEV(buzzer_major, buzzer_minor);
  212. // 注册设备号 - 使用指定的主设备号
  213. result = register_chrdev_region(dev_num, 1, DEVICE_NAME);
  214. if (result < 0)
  215. {
  216. printk(KERN_ALERT "buzzer: Failed to register major number %d\n", buzzer_major);
  217. printk(KERN_ALERT "buzzer: Try using a different major number\n");
  218. return result;
  219. }
  220. printk(KERN_INFO "buzzer: Registered with major=%d, minor=%d\n",
  221. MAJOR(dev_num), MINOR(dev_num));
  222. // 分配设备结构体
  223. dev = kmalloc(sizeof(struct buzzer_dev), GFP_KERNEL);
  224. if (!dev)
  225. {
  226. result = -ENOMEM;
  227. goto fail_malloc;
  228. }
  229. memset(dev, 0, sizeof(struct buzzer_dev));
  230. // 分配缓冲区
  231. dev->buffer = kmalloc(BUFFER_SIZE, GFP_KERNEL);
  232. if (!dev->buffer)
  233. {
  234. result = -ENOMEM;
  235. goto fail_buffer;
  236. }
  237. // 初始化互斥锁
  238. mutex_init(&dev->lock);
  239. INIT_DELAYED_WORK(&dev->delay_work1, delay_work_func);
  240. // 初始化字符设备
  241. cdev_init(&dev->cdev, &fops);
  242. dev->cdev.owner = THIS_MODULE;
  243. // 添加字符设备到系统
  244. result = cdev_add(&dev->cdev, dev_num, 1);
  245. if (result)
  246. {
  247. printk(KERN_ALERT "buzzer: Failed to add cdev\n");
  248. goto fail_cdev;
  249. }
  250. // 创建设备类
  251. char_class = class_create(THIS_MODULE, CLASS_NAME);
  252. if (IS_ERR(char_class))
  253. {
  254. result = PTR_ERR(char_class);
  255. printk(KERN_ALERT "buzzer: Failed to create class\n");
  256. goto fail_class;
  257. }
  258. // 创建设备
  259. char_device = device_create(char_class, NULL, dev_num, NULL, DEVICE_NAME);
  260. if (IS_ERR(char_device))
  261. {
  262. result = PTR_ERR(char_device);
  263. printk(KERN_ALERT "buzzer: Failed to create device\n");
  264. goto fail_device;
  265. }
  266. printk(KERN_INFO "buzzer: Driver initialized successfully\n");
  267. printk(KERN_INFO "buzzer: Device node: /dev/%s (major=%d, minor=%d)\n",
  268. DEVICE_NAME, buzzer_major, buzzer_minor);
  269. return 0;
  270. fail_device:
  271. class_destroy(char_class);
  272. fail_class:
  273. cdev_del(&dev->cdev);
  274. fail_cdev:
  275. kfree(dev->buffer);
  276. fail_buffer:
  277. kfree(dev);
  278. fail_malloc:
  279. unregister_chrdev_region(dev_num, 1);
  280. return result;
  281. }
  282. // 模块退出
  283. void buzzer_exit(void)
  284. {
  285. cancel_delayed_work_sync(&dev->delay_work1);
  286. device_destroy(char_class, dev_num);
  287. class_destroy(char_class);
  288. if (dev)
  289. {
  290. cdev_del(&dev->cdev);
  291. if (dev->buffer)
  292. kfree(dev->buffer);
  293. kfree(dev);
  294. }
  295. unregister_chrdev_region(dev_num, 1);
  296. printk(KERN_INFO "buzzer: Driver removed (major=%d, minor=%d)\n",
  297. buzzer_major, buzzer_minor);
  298. }