radio-zoltrix.c 8.1 KB

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