radio-zoltrix.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 <linux/slab.h>
  48. #include <media/v4l2-device.h>
  49. #include <media/v4l2-ioctl.h>
  50. #include "radio-isa.h"
  51. MODULE_AUTHOR("C. van Schaik");
  52. MODULE_DESCRIPTION("A driver for the Zoltrix Radio Plus.");
  53. MODULE_LICENSE("GPL");
  54. MODULE_VERSION("0.1.99");
  55. #ifndef CONFIG_RADIO_ZOLTRIX_PORT
  56. #define CONFIG_RADIO_ZOLTRIX_PORT -1
  57. #endif
  58. #define ZOLTRIX_MAX 2
  59. static int io[ZOLTRIX_MAX] = { [0] = CONFIG_RADIO_ZOLTRIX_PORT,
  60. [1 ... (ZOLTRIX_MAX - 1)] = -1 };
  61. static int radio_nr[ZOLTRIX_MAX] = { [0 ... (ZOLTRIX_MAX - 1)] = -1 };
  62. module_param_array(io, int, NULL, 0444);
  63. MODULE_PARM_DESC(io, "I/O addresses of the Zoltrix Radio Plus card (0x20c or 0x30c)");
  64. module_param_array(radio_nr, int, NULL, 0444);
  65. MODULE_PARM_DESC(radio_nr, "Radio device numbers");
  66. struct zoltrix {
  67. struct radio_isa_card isa;
  68. int curvol;
  69. bool muted;
  70. };
  71. static struct radio_isa_card *zoltrix_alloc(void)
  72. {
  73. struct zoltrix *zol = kzalloc(sizeof(*zol), GFP_KERNEL);
  74. return zol ? &zol->isa : NULL;
  75. }
  76. static int zoltrix_s_mute_volume(struct radio_isa_card *isa, bool mute, int vol)
  77. {
  78. struct zoltrix *zol = container_of(isa, struct zoltrix, isa);
  79. zol->curvol = vol;
  80. zol->muted = mute;
  81. if (mute || vol == 0) {
  82. outb(0, isa->io);
  83. outb(0, isa->io);
  84. inb(isa->io + 3); /* Zoltrix needs to be read to confirm */
  85. return 0;
  86. }
  87. outb(vol - 1, isa->io);
  88. msleep(10);
  89. inb(isa->io + 2);
  90. return 0;
  91. }
  92. /* tunes the radio to the desired frequency */
  93. static int zoltrix_s_frequency(struct radio_isa_card *isa, u32 freq)
  94. {
  95. struct zoltrix *zol = container_of(isa, struct zoltrix, isa);
  96. struct v4l2_device *v4l2_dev = &isa->v4l2_dev;
  97. unsigned long long bitmask, f, m;
  98. bool stereo = isa->stereo;
  99. int i;
  100. if (freq == 0) {
  101. v4l2_warn(v4l2_dev, "cannot set a frequency of 0.\n");
  102. return -EINVAL;
  103. }
  104. m = (freq / 160 - 8800) * 2;
  105. f = (unsigned long long)m + 0x4d1c;
  106. bitmask = 0xc480402c10080000ull;
  107. i = 45;
  108. outb(0, isa->io);
  109. outb(0, isa->io);
  110. inb(isa->io + 3); /* Zoltrix needs to be read to confirm */
  111. outb(0x40, isa->io);
  112. outb(0xc0, isa->io);
  113. bitmask = (bitmask ^ ((f & 0xff) << 47) ^ ((f & 0xff00) << 30) ^ (stereo << 31));
  114. while (i--) {
  115. if ((bitmask & 0x8000000000000000ull) != 0) {
  116. outb(0x80, isa->io);
  117. udelay(50);
  118. outb(0x00, isa->io);
  119. udelay(50);
  120. outb(0x80, isa->io);
  121. udelay(50);
  122. } else {
  123. outb(0xc0, isa->io);
  124. udelay(50);
  125. outb(0x40, isa->io);
  126. udelay(50);
  127. outb(0xc0, isa->io);
  128. udelay(50);
  129. }
  130. bitmask *= 2;
  131. }
  132. /* termination sequence */
  133. outb(0x80, isa->io);
  134. outb(0xc0, isa->io);
  135. outb(0x40, isa->io);
  136. udelay(1000);
  137. inb(isa->io + 2);
  138. udelay(1000);
  139. return zoltrix_s_mute_volume(isa, zol->muted, zol->curvol);
  140. }
  141. /* Get signal strength */
  142. static u32 zoltrix_g_rxsubchans(struct radio_isa_card *isa)
  143. {
  144. struct zoltrix *zol = container_of(isa, struct zoltrix, isa);
  145. int a, b;
  146. outb(0x00, isa->io); /* This stuff I found to do nothing */
  147. outb(zol->curvol, isa->io);
  148. msleep(20);
  149. a = inb(isa->io);
  150. msleep(10);
  151. b = inb(isa->io);
  152. return (a == b && a == 0xcf) ?
  153. V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO;
  154. }
  155. static u32 zoltrix_g_signal(struct radio_isa_card *isa)
  156. {
  157. struct zoltrix *zol = container_of(isa, struct zoltrix, isa);
  158. int a, b;
  159. outb(0x00, isa->io); /* This stuff I found to do nothing */
  160. outb(zol->curvol, isa->io);
  161. msleep(20);
  162. a = inb(isa->io);
  163. msleep(10);
  164. b = inb(isa->io);
  165. if (a != b)
  166. return 0;
  167. /* I found this out by playing with a binary scanner on the card io */
  168. return (a == 0xcf || a == 0xdf || a == 0xef) ? 0xffff : 0;
  169. }
  170. static int zoltrix_s_stereo(struct radio_isa_card *isa, bool stereo)
  171. {
  172. return zoltrix_s_frequency(isa, isa->freq);
  173. }
  174. static const struct radio_isa_ops zoltrix_ops = {
  175. .alloc = zoltrix_alloc,
  176. .s_mute_volume = zoltrix_s_mute_volume,
  177. .s_frequency = zoltrix_s_frequency,
  178. .s_stereo = zoltrix_s_stereo,
  179. .g_rxsubchans = zoltrix_g_rxsubchans,
  180. .g_signal = zoltrix_g_signal,
  181. };
  182. static const int zoltrix_ioports[] = { 0x20c, 0x30c };
  183. static struct radio_isa_driver zoltrix_driver = {
  184. .driver = {
  185. .match = radio_isa_match,
  186. .probe = radio_isa_probe,
  187. .remove = radio_isa_remove,
  188. .driver = {
  189. .name = "radio-zoltrix",
  190. },
  191. },
  192. .io_params = io,
  193. .radio_nr_params = radio_nr,
  194. .io_ports = zoltrix_ioports,
  195. .num_of_io_ports = ARRAY_SIZE(zoltrix_ioports),
  196. .region_size = 2,
  197. .card = "Zoltrix Radio Plus",
  198. .ops = &zoltrix_ops,
  199. .has_stereo = true,
  200. .max_volume = 15,
  201. };
  202. static int __init zoltrix_init(void)
  203. {
  204. return isa_register_driver(&zoltrix_driver.driver, ZOLTRIX_MAX);
  205. }
  206. static void __exit zoltrix_exit(void)
  207. {
  208. isa_unregister_driver(&zoltrix_driver.driver);
  209. }
  210. module_init(zoltrix_init);
  211. module_exit(zoltrix_exit);