radio-typhoon.c 5.2 KB

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