buzzer.c 7.9 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/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. // You can specify the major device number and minor device number through module parameters
  20. static int buzzer_major = 56; // Default main device number
  21. static int buzzer_minor = 100; // Default sub-device number
  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. struct buzzer_dev
  31. {
  32. char *buffer;
  33. size_t size;
  34. struct mutex lock;
  35. struct cdev cdev;
  36. struct delayed_work delay_work1;
  37. };
  38. static struct buzzer_dev *dev = NULL;
  39. void IoWrite8 (uint16_t addr, uint8_t data)
  40. {
  41. outb(data,addr);
  42. }
  43. uint8_t IoRead8 (uint16_t addr)
  44. {
  45. return inb(addr);
  46. }
  47. void BeepOff(void)
  48. {
  49. IoWrite8(BUZZER_PORT, IoRead8(BUZZER_PORT) & 0xfc);
  50. }
  51. void BeepOn(uint16_t freq)
  52. {
  53. uint16_t Frequency = note(freq);
  54. //set up channel 1 (used for delays)
  55. IoWrite8(BUZZER_CHANNEL, 0x54);
  56. IoWrite8(BUZZER_CHAN_1, 0x12);
  57. //set up channel 2 (used by speaker)
  58. IoWrite8(BUZZER_CHANNEL, 0xb6);
  59. IoWrite8(BUZZER_FREQ, (uint8_t)Frequency);
  60. IoWrite8(BUZZER_FREQ, (uint8_t)(Frequency >> 8));
  61. //turn the speaker on
  62. IoWrite8(BUZZER_PORT, IoRead8(BUZZER_PORT) | 3);
  63. }
  64. static void delay_work_func(struct work_struct *work)
  65. {
  66. int status_flag = 0;
  67. int freq = 0;
  68. int duration = 0;
  69. if(dev->size < 4)
  70. {
  71. printk(KERN_INFO "buzzer: size < 4\n");
  72. return;
  73. }
  74. // Close buzzer
  75. BeepOff();
  76. printk(KERN_INFO "delay_work_func\n");
  77. }
  78. static int buzzer_open(struct inode *inode, struct file *filp)
  79. {
  80. struct buzzer_dev *dev;
  81. // if (!request_region(PORT_80, 1, DRIVER_NAME))
  82. // {
  83. // pr_err("Port 80 I/O region busy\n");
  84. // return -EBUSY;
  85. // }
  86. dev = container_of(inode->i_cdev, struct buzzer_dev, cdev);
  87. filp->private_data = dev;
  88. printk(KERN_INFO "buzzer: Device opened (major=%d, minor=%d)\n",
  89. imajor(inode), iminor(inode));
  90. return 0;
  91. }
  92. static int buzzer_release(struct inode *inode, struct file *filp)
  93. {
  94. // release_region(PORT_80, 1);
  95. printk(KERN_INFO "buzzer: Device closed\n");
  96. return 0;
  97. }
  98. static ssize_t buzzer_read(struct file *filp, char __user *buf,
  99. size_t count, loff_t *f_pos)
  100. {
  101. struct buzzer_dev *dev = filp->private_data;
  102. ssize_t retval = 0;
  103. size_t available;
  104. int read_count = 0;
  105. int ret = 0;
  106. if (mutex_lock_interruptible(&dev->lock))
  107. return -ERESTARTSYS;
  108. // The read count here is not fixed at 4 bytes, in order to facilitate future expansion.
  109. if (count > dev->size)
  110. {
  111. read_count = dev->size;
  112. }
  113. else
  114. {
  115. read_count = count;
  116. }
  117. ret = copy_to_user(buf, dev->buffer, read_count);
  118. if (ret != 0)
  119. {
  120. printk(KERN_INFO "buzzer: copy_to_user failed\n");
  121. goto out;
  122. }
  123. printk(KERN_INFO "buzzer: Read %zu bytes\n", count);
  124. out:
  125. mutex_unlock(&dev->lock);
  126. return ret;
  127. }
  128. static ssize_t buzzer_write(struct file *filp, const char __user *buf,
  129. size_t count, loff_t *f_pos)
  130. {
  131. struct buzzer_dev *dev = filp->private_data;
  132. ssize_t retval = 0;
  133. size_t available;
  134. int ret = 0;
  135. int status_flag = 0;
  136. int freq = 0;
  137. int duration = 0;
  138. if (mutex_lock_interruptible(&dev->lock))
  139. {
  140. return -ERESTARTSYS;
  141. }
  142. if (count > BUFFER_SIZE)
  143. {
  144. count = BUFFER_SIZE;
  145. }
  146. if(count < 4)
  147. {
  148. printk(KERN_INFO "buzzer: count < 4\n");
  149. ret = -1;
  150. goto out;
  151. }
  152. ret = copy_from_user(dev->buffer, buf, count);
  153. if (ret != 0)
  154. {
  155. printk(KERN_INFO "buzzer: copy_from_user failed\n");
  156. goto out;
  157. }
  158. dev->size = count;
  159. status_flag = dev->buffer[0];
  160. freq = dev->buffer[2]<<8 + dev->buffer[1];
  161. duration = dev->buffer[3];
  162. if(status_flag == 1)
  163. {
  164. BeepOn(freq);
  165. }
  166. else
  167. {
  168. BeepOff();
  169. }
  170. printk(KERN_INFO "buzzer: Written %zu bytes\n", count);
  171. out:
  172. mutex_unlock(&dev->lock);
  173. if (ret == 0)
  174. {
  175. schedule_delayed_work(&dev->delay_work1, msecs_to_jiffies(duration*1000));
  176. }
  177. return count;
  178. }
  179. static struct file_operations fops = {
  180. .owner = THIS_MODULE,
  181. .open = buzzer_open,
  182. .release = buzzer_release,
  183. .read = buzzer_read,
  184. .write = buzzer_write,
  185. };
  186. int buzzer_init(void)
  187. {
  188. int result;
  189. printk(KERN_INFO "buzzer: Initializing driver with major=%d, minor=%d\n",
  190. buzzer_major, buzzer_minor);
  191. if (buzzer_major <= 0)
  192. {
  193. printk(KERN_ALERT "buzzer: Invalid major number %d\n", buzzer_major);
  194. return -EINVAL;
  195. }
  196. dev_num = MKDEV(buzzer_major, buzzer_minor);
  197. result = register_chrdev_region(dev_num, 1, DEVICE_NAME);
  198. if (result < 0)
  199. {
  200. printk(KERN_ALERT "buzzer: Failed to register major number %d\n", buzzer_major);
  201. printk(KERN_ALERT "buzzer: Try using a different major number\n");
  202. return result;
  203. }
  204. printk(KERN_INFO "buzzer: Registered with major=%d, minor=%d\n",
  205. MAJOR(dev_num), MINOR(dev_num));
  206. dev = kmalloc(sizeof(struct buzzer_dev), GFP_KERNEL);
  207. if (!dev)
  208. {
  209. result = -ENOMEM;
  210. goto fail_malloc;
  211. }
  212. memset(dev, 0, sizeof(struct buzzer_dev));
  213. dev->buffer = kmalloc(BUFFER_SIZE, GFP_KERNEL);
  214. if (!dev->buffer)
  215. {
  216. result = -ENOMEM;
  217. goto fail_buffer;
  218. }
  219. mutex_init(&dev->lock);
  220. INIT_DELAYED_WORK(&dev->delay_work1, delay_work_func);
  221. cdev_init(&dev->cdev, &fops);
  222. dev->cdev.owner = THIS_MODULE;
  223. result = cdev_add(&dev->cdev, dev_num, 1);
  224. if (result)
  225. {
  226. printk(KERN_ALERT "buzzer: Failed to add cdev\n");
  227. goto fail_cdev;
  228. }
  229. char_class = class_create(THIS_MODULE, CLASS_NAME);
  230. if (IS_ERR(char_class))
  231. {
  232. result = PTR_ERR(char_class);
  233. printk(KERN_ALERT "buzzer: Failed to create class\n");
  234. goto fail_class;
  235. }
  236. char_device = device_create(char_class, NULL, dev_num, NULL, DEVICE_NAME);
  237. if (IS_ERR(char_device))
  238. {
  239. result = PTR_ERR(char_device);
  240. printk(KERN_ALERT "buzzer: Failed to create device\n");
  241. goto fail_device;
  242. }
  243. printk(KERN_INFO "buzzer: Driver initialized successfully\n");
  244. printk(KERN_INFO "buzzer: Device node: /dev/%s (major=%d, minor=%d)\n",
  245. DEVICE_NAME, buzzer_major, buzzer_minor);
  246. return 0;
  247. fail_device:
  248. class_destroy(char_class);
  249. fail_class:
  250. cdev_del(&dev->cdev);
  251. fail_cdev:
  252. kfree(dev->buffer);
  253. fail_buffer:
  254. kfree(dev);
  255. fail_malloc:
  256. unregister_chrdev_region(dev_num, 1);
  257. return result;
  258. }
  259. void buzzer_exit(void)
  260. {
  261. cancel_delayed_work_sync(&dev->delay_work1);
  262. device_destroy(char_class, dev_num);
  263. class_destroy(char_class);
  264. if (dev)
  265. {
  266. cdev_del(&dev->cdev);
  267. if (dev->buffer)
  268. kfree(dev->buffer);
  269. kfree(dev);
  270. }
  271. unregister_chrdev_region(dev_num, 1);
  272. printk(KERN_INFO "buzzer: Driver removed (major=%d, minor=%d)\n",
  273. buzzer_major, buzzer_minor);
  274. }