radio-zoltrix.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /* zoltrix radio plus driver for Linux radio support
  2. * (c) 1998 C. van Schaik <carl@leg.uct.ac.za>
  3. *
  4. * BUGS
  5. * Due to the inconsistency in reading from the signal flags
  6. * it is difficult to get an accurate tuned signal.
  7. *
  8. * It seems that the card is not linear to 0 volume. It cuts off
  9. * at a low volume, and it is not possible (at least I have not found)
  10. * to get fine volume control over the low volume range.
  11. *
  12. * Some code derived from code by Romolo Manfredini
  13. * romolo@bicnet.it
  14. *
  15. * 1999-05-06 - (C. van Schaik)
  16. * - Make signal strength and stereo scans
  17. * kinder to cpu while in delay
  18. * 1999-01-05 - (C. van Schaik)
  19. * - Changed tuning to 1/160Mhz accuracy
  20. * - Added stereo support
  21. * (card defaults to stereo)
  22. * (can explicitly force mono on the card)
  23. * (can detect if station is in stereo)
  24. * - Added unmute function
  25. * - Reworked ioctl functions
  26. * 2002-07-15 - Fix Stereo typo
  27. */
  28. #include <linux/module.h> /* Modules */
  29. #include <linux/init.h> /* Initdata */
  30. #include <linux/ioport.h> /* request_region */
  31. #include <linux/delay.h> /* udelay, msleep */
  32. #include <asm/io.h> /* outb, outb_p */
  33. #include <asm/uaccess.h> /* copy to/from user */
  34. #include <linux/videodev.h> /* kernel radio structs */
  35. #include <linux/config.h> /* CONFIG_RADIO_ZOLTRIX_PORT */
  36. #ifndef CONFIG_RADIO_ZOLTRIX_PORT
  37. #define CONFIG_RADIO_ZOLTRIX_PORT -1
  38. #endif
  39. static int io = CONFIG_RADIO_ZOLTRIX_PORT;
  40. static int radio_nr = -1;
  41. struct zol_device {
  42. int port;
  43. int curvol;
  44. unsigned long curfreq;
  45. int muted;
  46. unsigned int stereo;
  47. struct mutex lock;
  48. };
  49. static int zol_setvol(struct zol_device *dev, int vol)
  50. {
  51. dev->curvol = vol;
  52. if (dev->muted)
  53. return 0;
  54. mutex_lock(&dev->lock);
  55. if (vol == 0) {
  56. outb(0, io);
  57. outb(0, io);
  58. inb(io + 3); /* Zoltrix needs to be read to confirm */
  59. mutex_unlock(&dev->lock);
  60. return 0;
  61. }
  62. outb(dev->curvol-1, io);
  63. msleep(10);
  64. inb(io + 2);
  65. mutex_unlock(&dev->lock);
  66. return 0;
  67. }
  68. static void zol_mute(struct zol_device *dev)
  69. {
  70. dev->muted = 1;
  71. mutex_lock(&dev->lock);
  72. outb(0, io);
  73. outb(0, io);
  74. inb(io + 3); /* Zoltrix needs to be read to confirm */
  75. mutex_unlock(&dev->lock);
  76. }
  77. static void zol_unmute(struct zol_device *dev)
  78. {
  79. dev->muted = 0;
  80. zol_setvol(dev, dev->curvol);
  81. }
  82. static int zol_setfreq(struct zol_device *dev, unsigned long freq)
  83. {
  84. /* tunes the radio to the desired frequency */
  85. unsigned long long bitmask, f, m;
  86. unsigned int stereo = dev->stereo;
  87. int i;
  88. if (freq == 0)
  89. return 1;
  90. m = (freq / 160 - 8800) * 2;
  91. f = (unsigned long long) m + 0x4d1c;
  92. bitmask = 0xc480402c10080000ull;
  93. i = 45;
  94. mutex_lock(&dev->lock);
  95. outb(0, io);
  96. outb(0, io);
  97. inb(io + 3); /* Zoltrix needs to be read to confirm */
  98. outb(0x40, io);
  99. outb(0xc0, io);
  100. bitmask = (bitmask ^ ((f & 0xff) << 47) ^ ((f & 0xff00) << 30) ^ ( stereo << 31));
  101. while (i--) {
  102. if ((bitmask & 0x8000000000000000ull) != 0) {
  103. outb(0x80, io);
  104. udelay(50);
  105. outb(0x00, io);
  106. udelay(50);
  107. outb(0x80, io);
  108. udelay(50);
  109. } else {
  110. outb(0xc0, io);
  111. udelay(50);
  112. outb(0x40, io);
  113. udelay(50);
  114. outb(0xc0, io);
  115. udelay(50);
  116. }
  117. bitmask *= 2;
  118. }
  119. /* termination sequence */
  120. outb(0x80, io);
  121. outb(0xc0, io);
  122. outb(0x40, io);
  123. udelay(1000);
  124. inb(io+2);
  125. udelay(1000);
  126. if (dev->muted)
  127. {
  128. outb(0, io);
  129. outb(0, io);
  130. inb(io + 3);
  131. udelay(1000);
  132. }
  133. mutex_unlock(&dev->lock);
  134. if(!dev->muted)
  135. {
  136. zol_setvol(dev, dev->curvol);
  137. }
  138. return 0;
  139. }
  140. /* Get signal strength */
  141. static int zol_getsigstr(struct zol_device *dev)
  142. {
  143. int a, b;
  144. mutex_lock(&dev->lock);
  145. outb(0x00, io); /* This stuff I found to do nothing */
  146. outb(dev->curvol, io);
  147. msleep(20);
  148. a = inb(io);
  149. msleep(10);
  150. b = inb(io);
  151. mutex_unlock(&dev->lock);
  152. if (a != b)
  153. return (0);
  154. if ((a == 0xcf) || (a == 0xdf) /* I found this out by playing */
  155. || (a == 0xef)) /* with a binary scanner on the card io */
  156. return (1);
  157. return (0);
  158. }
  159. static int zol_is_stereo (struct zol_device *dev)
  160. {
  161. int x1, x2;
  162. mutex_lock(&dev->lock);
  163. outb(0x00, io);
  164. outb(dev->curvol, io);
  165. msleep(20);
  166. x1 = inb(io);
  167. msleep(10);
  168. x2 = inb(io);
  169. mutex_unlock(&dev->lock);
  170. if ((x1 == x2) && (x1 == 0xcf))
  171. return 1;
  172. return 0;
  173. }
  174. static int zol_do_ioctl(struct inode *inode, struct file *file,
  175. unsigned int cmd, void *arg)
  176. {
  177. struct video_device *dev = video_devdata(file);
  178. struct zol_device *zol = dev->priv;
  179. switch (cmd) {
  180. case VIDIOCGCAP:
  181. {
  182. struct video_capability *v = arg;
  183. memset(v,0,sizeof(*v));
  184. v->type = VID_TYPE_TUNER;
  185. v->channels = 1 + zol->stereo;
  186. v->audios = 1;
  187. strcpy(v->name, "Zoltrix Radio");
  188. return 0;
  189. }
  190. case VIDIOCGTUNER:
  191. {
  192. struct video_tuner *v = arg;
  193. if (v->tuner)
  194. return -EINVAL;
  195. strcpy(v->name, "FM");
  196. v->rangelow = (int) (88.0 * 16000);
  197. v->rangehigh = (int) (108.0 * 16000);
  198. v->flags = zol_is_stereo(zol)
  199. ? VIDEO_TUNER_STEREO_ON : 0;
  200. v->flags |= VIDEO_TUNER_LOW;
  201. v->mode = VIDEO_MODE_AUTO;
  202. v->signal = 0xFFFF * zol_getsigstr(zol);
  203. return 0;
  204. }
  205. case VIDIOCSTUNER:
  206. {
  207. struct video_tuner *v = arg;
  208. if (v->tuner != 0)
  209. return -EINVAL;
  210. /* Only 1 tuner so no setting needed ! */
  211. return 0;
  212. }
  213. case VIDIOCGFREQ:
  214. {
  215. unsigned long *freq = arg;
  216. *freq = zol->curfreq;
  217. return 0;
  218. }
  219. case VIDIOCSFREQ:
  220. {
  221. unsigned long *freq = arg;
  222. zol->curfreq = *freq;
  223. zol_setfreq(zol, zol->curfreq);
  224. return 0;
  225. }
  226. case VIDIOCGAUDIO:
  227. {
  228. struct video_audio *v = arg;
  229. memset(v, 0, sizeof(*v));
  230. v->flags |= VIDEO_AUDIO_MUTABLE | VIDEO_AUDIO_VOLUME;
  231. v->mode |= zol_is_stereo(zol)
  232. ? VIDEO_SOUND_STEREO : VIDEO_SOUND_MONO;
  233. v->volume = zol->curvol * 4096;
  234. v->step = 4096;
  235. strcpy(v->name, "Zoltrix Radio");
  236. return 0;
  237. }
  238. case VIDIOCSAUDIO:
  239. {
  240. struct video_audio *v = arg;
  241. if (v->audio)
  242. return -EINVAL;
  243. if (v->flags & VIDEO_AUDIO_MUTE)
  244. zol_mute(zol);
  245. else {
  246. zol_unmute(zol);
  247. zol_setvol(zol, v->volume / 4096);
  248. }
  249. if (v->mode & VIDEO_SOUND_STEREO) {
  250. zol->stereo = 1;
  251. zol_setfreq(zol, zol->curfreq);
  252. }
  253. if (v->mode & VIDEO_SOUND_MONO) {
  254. zol->stereo = 0;
  255. zol_setfreq(zol, zol->curfreq);
  256. }
  257. return 0;
  258. }
  259. default:
  260. return -ENOIOCTLCMD;
  261. }
  262. }
  263. static int zol_ioctl(struct inode *inode, struct file *file,
  264. unsigned int cmd, unsigned long arg)
  265. {
  266. return video_usercopy(inode, file, cmd, arg, zol_do_ioctl);
  267. }
  268. static struct zol_device zoltrix_unit;
  269. static struct file_operations zoltrix_fops =
  270. {
  271. .owner = THIS_MODULE,
  272. .open = video_exclusive_open,
  273. .release = video_exclusive_release,
  274. .ioctl = zol_ioctl,
  275. .compat_ioctl = v4l_compat_ioctl32,
  276. .llseek = no_llseek,
  277. };
  278. static struct video_device zoltrix_radio =
  279. {
  280. .owner = THIS_MODULE,
  281. .name = "Zoltrix Radio Plus",
  282. .type = VID_TYPE_TUNER,
  283. .hardware = VID_HARDWARE_ZOLTRIX,
  284. .fops = &zoltrix_fops,
  285. };
  286. static int __init zoltrix_init(void)
  287. {
  288. if (io == -1) {
  289. printk(KERN_ERR "You must set an I/O address with io=0x???\n");
  290. return -EINVAL;
  291. }
  292. if ((io != 0x20c) && (io != 0x30c)) {
  293. printk(KERN_ERR "zoltrix: invalid port, try 0x20c or 0x30c\n");
  294. return -ENXIO;
  295. }
  296. zoltrix_radio.priv = &zoltrix_unit;
  297. if (!request_region(io, 2, "zoltrix")) {
  298. printk(KERN_ERR "zoltrix: port 0x%x already in use\n", io);
  299. return -EBUSY;
  300. }
  301. if (video_register_device(&zoltrix_radio, VFL_TYPE_RADIO, radio_nr) == -1)
  302. {
  303. release_region(io, 2);
  304. return -EINVAL;
  305. }
  306. printk(KERN_INFO "Zoltrix Radio Plus card driver.\n");
  307. mutex_init(&zoltrix_unit.lock);
  308. /* mute card - prevents noisy bootups */
  309. /* this ensures that the volume is all the way down */
  310. outb(0, io);
  311. outb(0, io);
  312. msleep(20);
  313. inb(io + 3);
  314. zoltrix_unit.curvol = 0;
  315. zoltrix_unit.stereo = 1;
  316. return 0;
  317. }
  318. MODULE_AUTHOR("C.van Schaik");
  319. MODULE_DESCRIPTION("A driver for the Zoltrix Radio Plus.");
  320. MODULE_LICENSE("GPL");
  321. module_param(io, int, 0);
  322. MODULE_PARM_DESC(io, "I/O address of the Zoltrix Radio Plus (0x20c or 0x30c)");
  323. module_param(radio_nr, int, 0);
  324. static void __exit zoltrix_cleanup_module(void)
  325. {
  326. video_unregister_device(&zoltrix_radio);
  327. release_region(io, 2);
  328. }
  329. module_init(zoltrix_init);
  330. module_exit(zoltrix_cleanup_module);