radio-typhoon.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /* Typhoon Radio Card driver for radio support
  2. * (c) 1999 Dr. Henrik Seidel <Henrik.Seidel@gmx.de>
  3. *
  4. * Notes on the hardware
  5. *
  6. * This card has two output sockets, one for speakers and one for line.
  7. * The speaker output has volume control, but only in four discrete
  8. * steps. The line output has neither volume control nor mute.
  9. *
  10. * The card has auto-stereo according to its manual, although it all
  11. * sounds mono to me (even with the Win/DOS drivers). Maybe it's my
  12. * antenna - I really don't know for sure.
  13. *
  14. * Frequency control is done digitally.
  15. *
  16. * Volume control is done digitally, but there are only four different
  17. * possible values. So you should better always turn the volume up and
  18. * use line control. I got the best results by connecting line output
  19. * to the sound card microphone input. For such a configuration the
  20. * volume control has no effect, since volume control only influences
  21. * the speaker output.
  22. *
  23. * There is no explicit mute/unmute. So I set the radio frequency to a
  24. * value where I do expect just noise and turn the speaker volume down.
  25. * The frequency change is necessary since the card never seems to be
  26. * completely silent.
  27. *
  28. * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
  29. */
  30. #include <linux/module.h> /* Modules */
  31. #include <linux/init.h> /* Initdata */
  32. #include <linux/ioport.h> /* request_region */
  33. #include <linux/videodev2.h> /* kernel radio structs */
  34. #include <linux/io.h> /* outb, outb_p */
  35. #include <media/v4l2-device.h>
  36. #include <media/v4l2-ioctl.h>
  37. #include "radio-isa.h"
  38. #define DRIVER_VERSION "0.1.2"
  39. MODULE_AUTHOR("Dr. Henrik Seidel");
  40. MODULE_DESCRIPTION("A driver for the Typhoon radio card (a.k.a. EcoRadio).");
  41. MODULE_LICENSE("GPL");
  42. MODULE_VERSION("0.1.99");
  43. #ifndef CONFIG_RADIO_TYPHOON_PORT
  44. #define CONFIG_RADIO_TYPHOON_PORT -1
  45. #endif
  46. #ifndef CONFIG_RADIO_TYPHOON_MUTEFREQ
  47. #define CONFIG_RADIO_TYPHOON_MUTEFREQ 87000
  48. #endif
  49. #define TYPHOON_MAX 2
  50. static int io[TYPHOON_MAX] = { [0] = CONFIG_RADIO_TYPHOON_PORT,
  51. [1 ... (TYPHOON_MAX - 1)] = -1 };
  52. static int radio_nr[TYPHOON_MAX] = { [0 ... (TYPHOON_MAX - 1)] = -1 };
  53. static unsigned long mutefreq = CONFIG_RADIO_TYPHOON_MUTEFREQ;
  54. module_param_array(io, int, NULL, 0444);
  55. MODULE_PARM_DESC(io, "I/O addresses of the Typhoon card (0x316 or 0x336)");
  56. module_param_array(radio_nr, int, NULL, 0444);
  57. MODULE_PARM_DESC(radio_nr, "Radio device numbers");
  58. module_param(mutefreq, ulong, 0);
  59. MODULE_PARM_DESC(mutefreq, "Frequency used when muting the card (in kHz)");
  60. struct typhoon {
  61. struct radio_isa_card isa;
  62. int muted;
  63. };
  64. static struct radio_isa_card *typhoon_alloc(void)
  65. {
  66. struct typhoon *ty = kzalloc(sizeof(*ty), GFP_KERNEL);
  67. return ty ? &ty->isa : NULL;
  68. }
  69. static int typhoon_s_frequency(struct radio_isa_card *isa, u32 freq)
  70. {
  71. unsigned long outval;
  72. unsigned long x;
  73. /*
  74. * The frequency transfer curve is not linear. The best fit I could
  75. * get is
  76. *
  77. * outval = -155 + exp((f + 15.55) * 0.057))
  78. *
  79. * where frequency f is in MHz. Since we don't have exp in the kernel,
  80. * I approximate this function by a third order polynomial.
  81. *
  82. */
  83. x = freq / 160;
  84. outval = (x * x + 2500) / 5000;
  85. outval = (outval * x + 5000) / 10000;
  86. outval -= (10 * x * x + 10433) / 20866;
  87. outval += 4 * x - 11505;
  88. outb_p((outval >> 8) & 0x01, isa->io + 4);
  89. outb_p(outval >> 9, isa->io + 6);
  90. outb_p(outval & 0xff, isa->io + 8);
  91. return 0;
  92. }
  93. static int typhoon_s_mute_volume(struct radio_isa_card *isa, bool mute, int vol)
  94. {
  95. struct typhoon *ty = container_of(isa, struct typhoon, isa);
  96. if (mute)
  97. vol = 0;
  98. vol >>= 14; /* Map 16 bit to 2 bit */
  99. vol &= 3;
  100. outb_p(vol / 2, isa->io); /* Set the volume, high bit. */
  101. outb_p(vol % 2, isa->io + 2); /* Set the volume, low bit. */
  102. if (vol == 0 && !ty->muted) {
  103. ty->muted = true;
  104. return typhoon_s_frequency(isa, mutefreq << 4);
  105. }
  106. if (vol && ty->muted) {
  107. ty->muted = false;
  108. return typhoon_s_frequency(isa, isa->freq);
  109. }
  110. return 0;
  111. }
  112. static const struct radio_isa_ops typhoon_ops = {
  113. .alloc = typhoon_alloc,
  114. .s_mute_volume = typhoon_s_mute_volume,
  115. .s_frequency = typhoon_s_frequency,
  116. };
  117. static const int typhoon_ioports[] = { 0x316, 0x336 };
  118. static struct radio_isa_driver typhoon_driver = {
  119. .driver = {
  120. .match = radio_isa_match,
  121. .probe = radio_isa_probe,
  122. .remove = radio_isa_remove,
  123. .driver = {
  124. .name = "radio-typhoon",
  125. },
  126. },
  127. .io_params = io,
  128. .radio_nr_params = radio_nr,
  129. .io_ports = typhoon_ioports,
  130. .num_of_io_ports = ARRAY_SIZE(typhoon_ioports),
  131. .region_size = 8,
  132. .card = "Typhoon Radio",
  133. .ops = &typhoon_ops,
  134. .has_stereo = true,
  135. .max_volume = 3,
  136. };
  137. static int __init typhoon_init(void)
  138. {
  139. if (mutefreq < 87000 || mutefreq > 108000) {
  140. printk(KERN_ERR "%s: You must set a frequency (in kHz) used when muting the card,\n",
  141. typhoon_driver.driver.driver.name);
  142. printk(KERN_ERR "%s: e.g. with \"mutefreq=87500\" (87000 <= mutefreq <= 108000)\n",
  143. typhoon_driver.driver.driver.name);
  144. return -ENODEV;
  145. }
  146. return isa_register_driver(&typhoon_driver.driver, TYPHOON_MAX);
  147. }
  148. static void __exit typhoon_exit(void)
  149. {
  150. isa_unregister_driver(&typhoon_driver.driver);
  151. }
  152. module_init(typhoon_init);
  153. module_exit(typhoon_exit);