radio-zoltrix.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * Zoltrix Radio Plus driver
  3. * Copyright 1998 C. van Schaik <carl@leg.uct.ac.za>
  4. *
  5. * BUGS
  6. * Due to the inconsistency in reading from the signal flags
  7. * it is difficult to get an accurate tuned signal.
  8. *
  9. * It seems that the card is not linear to 0 volume. It cuts off
  10. * at a low volume, and it is not possible (at least I have not found)
  11. * to get fine volume control over the low volume range.
  12. *
  13. * Some code derived from code by Romolo Manfredini
  14. * romolo@bicnet.it
  15. *
  16. * 1999-05-06 - (C. van Schaik)
  17. * - Make signal strength and stereo scans
  18. * kinder to cpu while in delay
  19. * 1999-01-05 - (C. van Schaik)
  20. * - Changed tuning to 1/160Mhz accuracy
  21. * - Added stereo support
  22. * (card defaults to stereo)
  23. * (can explicitly force mono on the card)
  24. * (can detect if station is in stereo)
  25. * - Added unmute function
  26. * - Reworked ioctl functions
  27. * 2002-07-15 - Fix Stereo typo
  28. *
  29. * 2006-07-24 - Converted to V4L2 API
  30. * by Mauro Carvalho Chehab <mchehab@infradead.org>
  31. *
  32. * Converted to the radio-isa framework by Hans Verkuil <hans.verkuil@cisco.com>
  33. *
  34. * Note that this is the driver for the Zoltrix Radio Plus.
  35. * This driver does not work for the Zoltrix Radio Plus 108 or the
  36. * Zoltrix Radio Plus for Windows.
  37. *
  38. * Fully tested with the Keene USB FM Transmitter and the v4l2-compliance tool.
  39. */
  40. #include <linux/module.h> /* Modules */
  41. #include <linux/init.h> /* Initdata */
  42. #include <linux/ioport.h> /* request_region */
  43. #include <linux/delay.h> /* udelay, msleep */
  44. #include <linux/videodev2.h> /* kernel radio structs */
  45. #include <linux/mutex.h>
  46. #include <linux/io.h> /* outb, outb_p */
  47. #include <media/v4l2-device.h>
  48. #include <media/v4l2-ioctl.h>
  49. #include "radio-isa.h"
  50. MODULE_AUTHOR("C. van Schaik");
  51. MODULE_DESCRIPTION("A driver for the Zoltrix Radio Plus.");
  52. MODULE_LICENSE("GPL");
  53. MODULE_VERSION("0.1.99");
  54. #ifndef CONFIG_RADIO_ZOLTRIX_PORT
  55. #define CONFIG_RADIO_ZOLTRIX_PORT -1
  56. #endif
  57. #define ZOLTRIX_MAX 2
  58. static int io[ZOLTRIX_MAX] = { [0] = CONFIG_RADIO_ZOLTRIX_PORT,
  59. [1 ... (ZOLTRIX_MAX - 1)] = -1 };
  60. static int radio_nr[ZOLTRIX_MAX] = { [0 ... (ZOLTRIX_MAX - 1)] = -1 };
  61. module_param_array(io, int, NULL, 0444);
  62. MODULE_PARM_DESC(io, "I/O addresses of the Zoltrix Radio Plus card (0x20c or 0x30c)");
  63. module_param_array(radio_nr, int, NULL, 0444);
  64. MODULE_PARM_DESC(radio_nr, "Radio device numbers");
  65. struct zoltrix {
  66. struct radio_isa_card isa;
  67. int curvol;
  68. bool muted;
  69. };
  70. static struct radio_isa_card *zoltrix_alloc(void)
  71. {
  72. struct zoltrix *zol = kzalloc(sizeof(*zol), GFP_KERNEL);
  73. return zol ? &zol->isa : NULL;
  74. }
  75. static int zoltrix_s_mute_volume(struct radio_isa_card *isa, bool mute, int vol)
  76. {
  77. struct zoltrix *zol = container_of(isa, struct zoltrix, isa);
  78. zol->curvol = vol;
  79. zol->muted = mute;
  80. if (mute || vol == 0) {
  81. outb(0, isa->io);
  82. outb(0, isa->io);
  83. inb(isa->io + 3); /* Zoltrix needs to be read to confirm */
  84. return 0;
  85. }
  86. outb(vol - 1, isa->io);
  87. msleep(10);
  88. inb(isa->io + 2);
  89. return 0;
  90. }
  91. /* tunes the radio to the desired frequency */
  92. static int zoltrix_s_frequency(struct radio_isa_card *isa, u32 freq)
  93. {
  94. struct zoltrix *zol = container_of(isa, struct zoltrix, isa);
  95. struct v4l2_device *v4l2_dev = &isa->v4l2_dev;
  96. unsigned long long bitmask, f, m;
  97. bool stereo = isa->stereo;
  98. int i;
  99. if (freq == 0) {
  100. v4l2_warn(v4l2_dev, "cannot set a frequency of 0.\n");
  101. return -EINVAL;
  102. }
  103. m = (freq / 160 - 8800) * 2;
  104. f = (unsigned long long)m + 0x4d1c;
  105. bitmask = 0xc480402c10080000ull;
  106. i = 45;
  107. outb(0, isa->io);
  108. outb(0, isa->io);
  109. inb(isa->io + 3); /* Zoltrix needs to be read to confirm */
  110. outb(0x40, isa->io);
  111. outb(0xc0, isa->io);
  112. bitmask = (bitmask ^ ((f & 0xff) << 47) ^ ((f & 0xff00) << 30) ^ (stereo << 31));
  113. while (i--) {
  114. if ((bitmask & 0x8000000000000000ull) != 0) {
  115. outb(0x80, isa->io);
  116. udelay(50);
  117. outb(0x00, isa->io);
  118. udelay(50);
  119. outb(0x80, isa->io);
  120. udelay(50);
  121. } else {
  122. outb(0xc0, isa->io);
  123. udelay(50);
  124. outb(0x40, isa->io);
  125. udelay(50);
  126. outb(0xc0, isa->io);
  127. udelay(50);
  128. }
  129. bitmask *= 2;
  130. }
  131. /* termination sequence */
  132. outb(0x80, isa->io);
  133. outb(0xc0, isa->io);
  134. outb(0x40, isa->io);
  135. udelay(1000);
  136. inb(isa->io + 2);
  137. udelay(1000);
  138. return zoltrix_s_mute_volume(isa, zol->muted, zol->curvol);
  139. }
  140. /* Get signal strength */
  141. static u32 zoltrix_g_rxsubchans(struct radio_isa_card *isa)
  142. {
  143. struct zoltrix *zol = container_of(isa, struct zoltrix, isa);
  144. int a, b;
  145. outb(0x00, isa->io); /* This stuff I found to do nothing */
  146. outb(zol->curvol, isa->io);
  147. msleep(20);
  148. a = inb(isa->io);
  149. msleep(10);
  150. b = inb(isa->io);
  151. return (a == b && a == 0xcf) ?
  152. V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO;
  153. }
  154. static u32 zoltrix_g_signal(struct radio_isa_card *isa)
  155. {
  156. struct zoltrix *zol = container_of(isa, struct zoltrix, isa);
  157. int a, b;
  158. outb(0x00, isa->io); /* This stuff I found to do nothing */
  159. outb(zol->curvol, isa->io);
  160. msleep(20);
  161. a = inb(isa->io);
  162. msleep(10);
  163. b = inb(isa->io);
  164. if (a != b)
  165. return 0;
  166. /* I found this out by playing with a binary scanner on the card io */
  167. return (a == 0xcf || a == 0xdf || a == 0xef) ? 0xffff : 0;
  168. }
  169. static int zoltrix_s_stereo(struct radio_isa_card *isa, bool stereo)
  170. {
  171. return zoltrix_s_frequency(isa, isa->freq);
  172. }
  173. static const struct radio_isa_ops zoltrix_ops = {
  174. .alloc = zoltrix_alloc,
  175. .s_mute_volume = zoltrix_s_mute_volume,
  176. .s_frequency = zoltrix_s_frequency,
  177. .s_stereo = zoltrix_s_stereo,
  178. .g_rxsubchans = zoltrix_g_rxsubchans,
  179. .g_signal = zoltrix_g_signal,
  180. };
  181. static const int zoltrix_ioports[] = { 0x20c, 0x30c };
  182. static struct radio_isa_driver zoltrix_driver = {
  183. .driver = {
  184. .match = radio_isa_match,
  185. .probe = radio_isa_probe,
  186. .remove = radio_isa_remove,
  187. .driver = {
  188. .name = "radio-zoltrix",
  189. },
  190. },
  191. .io_params = io,
  192. .radio_nr_params = radio_nr,
  193. .io_ports = zoltrix_ioports,
  194. .num_of_io_ports = ARRAY_SIZE(zoltrix_ioports),
  195. .region_size = 2,
  196. .card = "Zoltrix Radio Plus",
  197. .ops = &zoltrix_ops,
  198. .has_stereo = true,
  199. .max_volume = 15,
  200. };
  201. static int __init zoltrix_init(void)
  202. {
  203. return isa_register_driver(&zoltrix_driver.driver, ZOLTRIX_MAX);
  204. }
  205. static void __exit zoltrix_exit(void)
  206. {
  207. isa_unregister_driver(&zoltrix_driver.driver);
  208. }
  209. module_init(zoltrix_init);
  210. module_exit(zoltrix_exit);