pcspkr.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /* vim: tabstop=4 shiftwidth=4 noexpandtab
  2. * This file is part of ToaruOS and is released under the terms
  3. * of the NCSA / University of Illinois License - see LICENSE.md
  4. * Copyright (C) 2014-2018 K. Lange
  5. */
  6. #include <kernel/module.h>
  7. #include <kernel/printf.h>
  8. #include <kernel/mod/shell.h>
  9. static void note(int length, int freq) {
  10. uint8_t t;
  11. if (length == 0) {
  12. t = inportb(0x61) & 0xFC;
  13. outportb(0x61, t);
  14. return;
  15. }
  16. uint32_t div = 11931800 / freq;
  17. outportb(0x43, 0xb6);
  18. outportb(0x42, (uint8_t)(div));
  19. outportb(0x42, (uint8_t)(div >> 8));
  20. t = inportb(0x61);
  21. outportb(0x61, t | 0x3);
  22. if (length > 0) {
  23. unsigned long s, ss;
  24. relative_time(length / 1000, length % 1000, &s, &ss);
  25. sleep_until((process_t *)current_process, s, ss);
  26. switch_task(0);
  27. t = inportb(0x61) & 0xFC;
  28. outportb(0x61, t);
  29. }
  30. }
  31. struct spkr {
  32. int length;
  33. int frequency;
  34. };
  35. static uint32_t write_spkr(fs_node_t *node, uint64_t offset, uint32_t size, uint8_t *buffer) {
  36. if (!size % (sizeof(struct spkr))) {
  37. return 0;
  38. }
  39. struct spkr * s = (struct spkr *)buffer;
  40. while ((uintptr_t)s < (uintptr_t)buffer + size) {
  41. note(s->length, s->frequency);
  42. s++;
  43. }
  44. return (uintptr_t)s - (uintptr_t)buffer;
  45. }
  46. static fs_node_t * spkr_device_create(void) {
  47. fs_node_t * fnode = malloc(sizeof(fs_node_t));
  48. memset(fnode, 0x00, sizeof(fs_node_t));
  49. sprintf(fnode->name, "spkr");
  50. fnode->mask = 0666; /* TODO need a speaker group */
  51. fnode->flags = FS_CHARDEVICE;
  52. fnode->write = write_spkr;
  53. return fnode;
  54. }
  55. static int init(void) {
  56. fs_node_t * node = spkr_device_create();
  57. vfs_mount("/dev/spkr", node);
  58. return 0;
  59. }
  60. static int fini(void) {
  61. return 0;
  62. }
  63. MODULE_DEF(pcspkr, init, fini);