buzzer.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. #if defined(PROJECT) && (PROJECT == POS)
  15. #define DEVICE_NAME "buzz0"
  16. #else
  17. #define DEVICE_NAME "buzzer"
  18. #endif
  19. #define CLASS_NAME "buzzer_class"
  20. #define DRIVER_NAME "buzzer_driver"
  21. #define BUFFER_SIZE 4
  22. #define note(x) ((119318200+(x)/2)/(x))
  23. // You can specify the major device number and minor device number through module parameters
  24. static int buzzer_major = 56; // Default main device number
  25. static int buzzer_minor = 100; // Default sub-device number
  26. module_param(buzzer_major, int, S_IRUGO);
  27. module_param(buzzer_minor, int, S_IRUGO);
  28. MODULE_PARM_DESC(buzzer_major, "Major device number");
  29. MODULE_PARM_DESC(buzzer_minor, "Minor device number");
  30. static struct class *char_class = NULL;
  31. static struct device *char_device = NULL;
  32. static struct cdev my_cdev;
  33. static dev_t dev_num;
  34. struct buzzer_dev
  35. {
  36. char *buffer;
  37. size_t size;
  38. struct mutex lock;
  39. struct cdev cdev;
  40. struct delayed_work delay_work1;
  41. };
  42. static struct buzzer_dev *dev = NULL;
  43. void IoWrite8 (uint16_t addr, uint8_t data)
  44. {
  45. outb(data,addr);
  46. }
  47. uint8_t IoRead8 (uint16_t addr)
  48. {
  49. return inb(addr);
  50. }
  51. void BeepOff(void)
  52. {
  53. IoWrite8(BUZZER_PORT, IoRead8(BUZZER_PORT) & 0xfc);
  54. }
  55. void BeepOn(uint16_t freq)
  56. {
  57. uint16_t Frequency = note(freq);
  58. //set up channel 1 (used for delays)
  59. IoWrite8(BUZZER_CHANNEL, 0x54);
  60. IoWrite8(BUZZER_CHAN_1, 0x12);
  61. //set up channel 2 (used by speaker)
  62. IoWrite8(BUZZER_CHANNEL, 0xb6);
  63. IoWrite8(BUZZER_FREQ, (uint8_t)Frequency);
  64. IoWrite8(BUZZER_FREQ, (uint8_t)(Frequency >> 8));
  65. //turn the speaker on
  66. IoWrite8(BUZZER_PORT, IoRead8(BUZZER_PORT) | 3);
  67. }
  68. static void delay_work_func(struct work_struct *work)
  69. {
  70. int status_flag = 0;
  71. int freq = 0;
  72. int duration = 0;
  73. if(dev->size < 4)
  74. {
  75. return;
  76. }
  77. // Close buzzer
  78. BeepOff();
  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. static int buzzer_release(struct inode *inode, struct file *filp)
  95. {
  96. // release_region(PORT_80, 1);
  97. return 0;
  98. }
  99. static ssize_t buzzer_read(struct file *filp, char __user *buf,
  100. size_t count, loff_t *f_pos)
  101. {
  102. struct buzzer_dev *dev = filp->private_data;
  103. ssize_t retval = 0;
  104. size_t available;
  105. int read_count = 0;
  106. int ret = 0;
  107. if (mutex_lock_interruptible(&dev->lock))
  108. return -ERESTARTSYS;
  109. // The read count here is not fixed at 4 bytes, in order to facilitate future expansion.
  110. if (count > dev->size)
  111. {
  112. read_count = dev->size;
  113. }
  114. else
  115. {
  116. read_count = count;
  117. }
  118. ret = copy_to_user(buf, dev->buffer, read_count);
  119. if (ret != 0)
  120. {
  121. printk(KERN_INFO "buzzer: copy_to_user failed\n");
  122. goto out;
  123. }
  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(freq == 0)
  163. {
  164. mutex_unlock(&dev->lock);
  165. return -EINVAL;
  166. }
  167. if(status_flag == 1)
  168. {
  169. BeepOn(freq);
  170. }
  171. else
  172. {
  173. BeepOff();
  174. }
  175. out:
  176. mutex_unlock(&dev->lock);
  177. if (ret == 0)
  178. {
  179. schedule_delayed_work(&dev->delay_work1, msecs_to_jiffies(duration*1000));
  180. }
  181. return count;
  182. }
  183. static struct file_operations fops = {
  184. .owner = THIS_MODULE,
  185. .open = buzzer_open,
  186. .release = buzzer_release,
  187. .read = buzzer_read,
  188. .write = buzzer_write,
  189. };
  190. static char *my_devnode(struct device *dev, umode_t *mode) {
  191. if (mode) {
  192. *mode = 0666;
  193. }
  194. return NULL;
  195. }
  196. int buzzer_init(void)
  197. {
  198. int result;
  199. printk(KERN_INFO "buzzer: Initializing driver with major=%d, minor=%d\n",
  200. buzzer_major, buzzer_minor);
  201. if (buzzer_major <= 0)
  202. {
  203. printk(KERN_ALERT "buzzer: Invalid major number %d\n", buzzer_major);
  204. return -EINVAL;
  205. }
  206. dev_num = MKDEV(buzzer_major, buzzer_minor);
  207. result = register_chrdev_region(dev_num, 1, DEVICE_NAME);
  208. if (result < 0)
  209. {
  210. printk(KERN_ALERT "buzzer: Failed to register major number %d\n", buzzer_major);
  211. printk(KERN_ALERT "buzzer: Try using a different major number\n");
  212. return result;
  213. }
  214. dev = kmalloc(sizeof(struct buzzer_dev), GFP_KERNEL);
  215. if (!dev)
  216. {
  217. result = -ENOMEM;
  218. goto fail_malloc;
  219. }
  220. memset(dev, 0, sizeof(struct buzzer_dev));
  221. dev->buffer = kmalloc(BUFFER_SIZE, GFP_KERNEL);
  222. if (!dev->buffer)
  223. {
  224. result = -ENOMEM;
  225. goto fail_buffer;
  226. }
  227. mutex_init(&dev->lock);
  228. INIT_DELAYED_WORK(&dev->delay_work1, delay_work_func);
  229. cdev_init(&dev->cdev, &fops);
  230. dev->cdev.owner = THIS_MODULE;
  231. result = cdev_add(&dev->cdev, dev_num, 1);
  232. if (result)
  233. {
  234. printk(KERN_ALERT "buzzer: Failed to add cdev\n");
  235. goto fail_cdev;
  236. }
  237. char_class = class_create(THIS_MODULE, CLASS_NAME);
  238. if (IS_ERR(char_class))
  239. {
  240. result = PTR_ERR(char_class);
  241. printk(KERN_ALERT "buzzer: Failed to create class\n");
  242. goto fail_class;
  243. }
  244. char_class->devnode = my_devnode;
  245. char_device = device_create(char_class, NULL, dev_num, NULL, DEVICE_NAME);
  246. if (IS_ERR(char_device))
  247. {
  248. result = PTR_ERR(char_device);
  249. printk(KERN_ALERT "buzzer: Failed to create device\n");
  250. goto fail_device;
  251. }
  252. return 0;
  253. fail_device:
  254. class_destroy(char_class);
  255. fail_class:
  256. cdev_del(&dev->cdev);
  257. fail_cdev:
  258. kfree(dev->buffer);
  259. fail_buffer:
  260. kfree(dev);
  261. fail_malloc:
  262. unregister_chrdev_region(dev_num, 1);
  263. return result;
  264. }
  265. void buzzer_exit(void)
  266. {
  267. cancel_delayed_work_sync(&dev->delay_work1);
  268. device_destroy(char_class, dev_num);
  269. class_destroy(char_class);
  270. if (dev)
  271. {
  272. cdev_del(&dev->cdev);
  273. if (dev->buffer)
  274. kfree(dev->buffer);
  275. kfree(dev);
  276. }
  277. unregister_chrdev_region(dev_num, 1);
  278. printk(KERN_INFO "buzzer: Driver removed (major=%d, minor=%d)\n",
  279. buzzer_major, buzzer_minor);
  280. }