radio-typhoon.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 <linux/slab.h>
  36. #include <media/v4l2-device.h>
  37. #include <media/v4l2-ioctl.h>
  38. #include "radio-isa.h"
  39. #define DRIVER_VERSION "0.1.2"
  40. MODULE_AUTHOR("Dr. Henrik Seidel");
  41. MODULE_DESCRIPTION("A driver for the Typhoon radio card (a.k.a. EcoRadio).");
  42. MODULE_LICENSE("GPL");
  43. MODULE_VERSION("0.1.99");
  44. #ifndef CONFIG_RADIO_TYPHOON_PORT
  45. #define CONFIG_RADIO_TYPHOON_PORT -1
  46. #endif
  47. #ifndef CONFIG_RADIO_TYPHOON_MUTEFREQ
  48. #define CONFIG_RADIO_TYPHOON_MUTEFREQ 87000
  49. #endif
  50. #define TYPHOON_MAX 2
  51. static int io[TYPHOON_MAX] = { [0] = CONFIG_RADIO_TYPHOON_PORT,
  52. [1 ... (TYPHOON_MAX - 1)] = -1 };
  53. static int radio_nr[TYPHOON_MAX] = { [0 ... (TYPHOON_MAX - 1)] = -1 };
  54. static unsigned long mutefreq = CONFIG_RADIO_TYPHOON_MUTEFREQ;
  55. module_param_array(io, int, NULL, 0444);
  56. MODULE_PARM_DESC(io, "I/O addresses of the Typhoon card (0x316 or 0x336)");
  57. module_param_array(radio_nr, int, NULL, 0444);
  58. MODULE_PARM_DESC(radio_nr, "Radio device numbers");
  59. module_param(mutefreq, ulong, 0);
  60. MODULE_PARM_DESC(mutefreq, "Frequency used when muting the card (in kHz)");
  61. struct typhoon {
  62. struct radio_isa_card isa;
  63. int muted;
  64. };
  65. static struct radio_isa_card *typhoon_alloc(void)
  66. {
  67. struct typhoon *ty = kzalloc(sizeof(*ty), GFP_KERNEL);
  68. return ty ? &ty->isa : NULL;
  69. }
  70. static int typhoon_s_frequency(struct radio_isa_card *isa, u32 freq)
  71. {
  72. unsigned long outval;
  73. unsigned long x;
  74. /*
  75. * The frequency transfer curve is not linear. The best fit I could
  76. * get is
  77. *
  78. * outval = -155 + exp((f + 15.55) * 0.057))
  79. *
  80. * where frequency f is in MHz. Since we don't have exp in the kernel,
  81. * I approximate this function by a third order polynomial.
  82. *
  83. */
  84. x = freq / 160;
  85. outval = (x * x + 2500) / 5000;
  86. outval = (outval * x + 5000) / 10000;
  87. outval -= (10 * x * x + 10433) / 20866;
  88. outval += 4 * x - 11505;
  89. outb_p((outval >> 8) & 0x01, isa->io + 4);
  90. outb_p(outval >> 9, isa->io + 6);
  91. outb_p(outval & 0xff, isa->io + 8);
  92. return 0;
  93. }
  94. static int typhoon_s_mute_volume(struct radio_isa_card *isa, bool mute, int vol)
  95. {
  96. struct typhoon *ty = container_of(isa, struct typhoon, isa);
  97. if (mute)
  98. vol = 0;
  99. vol >>= 14; /* Map 16 bit to 2 bit */
  100. vol &= 3;
  101. outb_p(vol / 2, isa->io); /* Set the volume, high bit. */
  102. outb_p(vol % 2, isa->io + 2); /* Set the volume, low bit. */
  103. if (vol == 0 && !ty->muted) {
  104. ty->muted = true;
  105. return typhoon_s_frequency(isa, mutefreq << 4);
  106. }
  107. if (vol && ty->muted) {
  108. ty->muted = false;
  109. return typhoon_s_frequency(isa, isa->freq);
  110. }
  111. return 0;
  112. }
  113. static const struct radio_isa_ops typhoon_ops = {
  114. .alloc = typhoon_alloc,
  115. .s_mute_volume = typhoon_s_mute_volume,
  116. .s_frequency = typhoon_s_frequency,
  117. };
  118. static const int typhoon_ioports[] = { 0x316, 0x336 };
  119. static struct radio_isa_driver typhoon_driver = {
  120. .driver = {
  121. .match = radio_isa_match,
  122. .probe = radio_isa_probe,
  123. .remove = radio_isa_remove,
  124. .driver = {
  125. .name = "radio-typhoon",
  126. },
  127. },
  128. .io_params = io,
  129. .radio_nr_params = radio_nr,
  130. .io_ports = typhoon_ioports,
  131. .num_of_io_ports = ARRAY_SIZE(typhoon_ioports),
  132. .region_size = 8,
  133. .card = "Typhoon Radio",
  134. .ops = &typhoon_ops,
  135. .has_stereo = true,
  136. .max_volume = 3,
  137. };
  138. static int __init typhoon_init(void)
  139. {
  140. if (mutefreq < 87000 || mutefreq > 108000) {
  141. printk(KERN_ERR "%s: You must set a frequency (in kHz) used when muting the card,\n",
  142. typhoon_driver.driver.driver.name);
  143. printk(KERN_ERR "%s: e.g. with \"mutefreq=87500\" (87000 <= mutefreq <= 108000)\n",
  144. typhoon_driver.driver.driver.name);
  145. return -ENODEV;
  146. }
  147. return isa_register_driver(&typhoon_driver.driver, TYPHOON_MAX);
  148. }
  149. static void __exit typhoon_exit(void)
  150. {
  151. isa_unregister_driver(&typhoon_driver.driver);
  152. }
  153. module_init(typhoon_init);
  154. module_exit(typhoon_exit);