buzzer.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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(status_flag == 1)
  163. {
  164. BeepOn(freq);
  165. }
  166. else
  167. {
  168. BeepOff();
  169. }
  170. out:
  171. mutex_unlock(&dev->lock);
  172. if (ret == 0)
  173. {
  174. schedule_delayed_work(&dev->delay_work1, msecs_to_jiffies(duration*1000));
  175. }
  176. return count;
  177. }
  178. static struct file_operations fops = {
  179. .owner = THIS_MODULE,
  180. .open = buzzer_open,
  181. .release = buzzer_release,
  182. .read = buzzer_read,
  183. .write = buzzer_write,
  184. };
  185. static char *my_devnode(struct device *dev, umode_t *mode) {
  186. if (mode) {
  187. *mode = 0666;
  188. }
  189. return NULL;
  190. }
  191. int buzzer_init(void)
  192. {
  193. int result;
  194. printk(KERN_INFO "buzzer: Initializing driver with major=%d, minor=%d\n",
  195. buzzer_major, buzzer_minor);
  196. if (buzzer_major <= 0)
  197. {
  198. printk(KERN_ALERT "buzzer: Invalid major number %d\n", buzzer_major);
  199. return -EINVAL;
  200. }
  201. dev_num = MKDEV(buzzer_major, buzzer_minor);
  202. result = register_chrdev_region(dev_num, 1, DEVICE_NAME);
  203. if (result < 0)
  204. {
  205. printk(KERN_ALERT "buzzer: Failed to register major number %d\n", buzzer_major);
  206. printk(KERN_ALERT "buzzer: Try using a different major number\n");
  207. return result;
  208. }
  209. dev = kmalloc(sizeof(struct buzzer_dev), GFP_KERNEL);
  210. if (!dev)
  211. {
  212. result = -ENOMEM;
  213. goto fail_malloc;
  214. }
  215. memset(dev, 0, sizeof(struct buzzer_dev));
  216. dev->buffer = kmalloc(BUFFER_SIZE, GFP_KERNEL);
  217. if (!dev->buffer)
  218. {
  219. result = -ENOMEM;
  220. goto fail_buffer;
  221. }
  222. mutex_init(&dev->lock);
  223. INIT_DELAYED_WORK(&dev->delay_work1, delay_work_func);
  224. cdev_init(&dev->cdev, &fops);
  225. dev->cdev.owner = THIS_MODULE;
  226. result = cdev_add(&dev->cdev, dev_num, 1);
  227. if (result)
  228. {
  229. printk(KERN_ALERT "buzzer: Failed to add cdev\n");
  230. goto fail_cdev;
  231. }
  232. char_class = class_create(THIS_MODULE, CLASS_NAME);
  233. if (IS_ERR(char_class))
  234. {
  235. result = PTR_ERR(char_class);
  236. printk(KERN_ALERT "buzzer: Failed to create class\n");
  237. goto fail_class;
  238. }
  239. char_class->devnode = my_devnode;
  240. char_device = device_create(char_class, NULL, dev_num, NULL, DEVICE_NAME);
  241. if (IS_ERR(char_device))
  242. {
  243. result = PTR_ERR(char_device);
  244. printk(KERN_ALERT "buzzer: Failed to create device\n");
  245. goto fail_device;
  246. }
  247. return 0;
  248. fail_device:
  249. class_destroy(char_class);
  250. fail_class:
  251. cdev_del(&dev->cdev);
  252. fail_cdev:
  253. kfree(dev->buffer);
  254. fail_buffer:
  255. kfree(dev);
  256. fail_malloc:
  257. unregister_chrdev_region(dev_num, 1);
  258. return result;
  259. }
  260. void buzzer_exit(void)
  261. {
  262. cancel_delayed_work_sync(&dev->delay_work1);
  263. device_destroy(char_class, dev_num);
  264. class_destroy(char_class);
  265. if (dev)
  266. {
  267. cdev_del(&dev->cdev);
  268. if (dev->buffer)
  269. kfree(dev->buffer);
  270. kfree(dev);
  271. }
  272. unregister_chrdev_region(dev_num, 1);
  273. printk(KERN_INFO "buzzer: Driver removed (major=%d, minor=%d)\n",
  274. buzzer_major, buzzer_minor);
  275. }