radio-zoltrix.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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. * 2006-07-24 - Converted to V4L2 API
  29. * by Mauro Carvalho Chehab <mchehab@infradead.org>
  30. */
  31. #include <linux/module.h> /* Modules */
  32. #include <linux/init.h> /* Initdata */
  33. #include <linux/ioport.h> /* request_region */
  34. #include <linux/delay.h> /* udelay, msleep */
  35. #include <asm/io.h> /* outb, outb_p */
  36. #include <asm/uaccess.h> /* copy to/from user */
  37. #include <linux/videodev2.h> /* kernel radio structs */
  38. #include <media/v4l2-common.h>
  39. #include <linux/version.h> /* for KERNEL_VERSION MACRO */
  40. #define RADIO_VERSION KERNEL_VERSION(0,0,2)
  41. static struct v4l2_queryctrl radio_qctrl[] = {
  42. {
  43. .id = V4L2_CID_AUDIO_MUTE,
  44. .name = "Mute",
  45. .minimum = 0,
  46. .maximum = 1,
  47. .default_value = 1,
  48. .type = V4L2_CTRL_TYPE_BOOLEAN,
  49. },{
  50. .id = V4L2_CID_AUDIO_VOLUME,
  51. .name = "Volume",
  52. .minimum = 0,
  53. .maximum = 65535,
  54. .step = 4096,
  55. .default_value = 0xff,
  56. .type = V4L2_CTRL_TYPE_INTEGER,
  57. }
  58. };
  59. #ifndef CONFIG_RADIO_ZOLTRIX_PORT
  60. #define CONFIG_RADIO_ZOLTRIX_PORT -1
  61. #endif
  62. static int io = CONFIG_RADIO_ZOLTRIX_PORT;
  63. static int radio_nr = -1;
  64. struct zol_device {
  65. int port;
  66. int curvol;
  67. unsigned long curfreq;
  68. int muted;
  69. unsigned int stereo;
  70. struct mutex lock;
  71. };
  72. static int zol_setvol(struct zol_device *dev, int vol)
  73. {
  74. dev->curvol = vol;
  75. if (dev->muted)
  76. return 0;
  77. mutex_lock(&dev->lock);
  78. if (vol == 0) {
  79. outb(0, io);
  80. outb(0, io);
  81. inb(io + 3); /* Zoltrix needs to be read to confirm */
  82. mutex_unlock(&dev->lock);
  83. return 0;
  84. }
  85. outb(dev->curvol-1, io);
  86. msleep(10);
  87. inb(io + 2);
  88. mutex_unlock(&dev->lock);
  89. return 0;
  90. }
  91. static void zol_mute(struct zol_device *dev)
  92. {
  93. dev->muted = 1;
  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. mutex_unlock(&dev->lock);
  99. }
  100. static void zol_unmute(struct zol_device *dev)
  101. {
  102. dev->muted = 0;
  103. zol_setvol(dev, dev->curvol);
  104. }
  105. static int zol_setfreq(struct zol_device *dev, unsigned long freq)
  106. {
  107. /* tunes the radio to the desired frequency */
  108. unsigned long long bitmask, f, m;
  109. unsigned int stereo = dev->stereo;
  110. int i;
  111. if (freq == 0)
  112. return 1;
  113. m = (freq / 160 - 8800) * 2;
  114. f = (unsigned long long) m + 0x4d1c;
  115. bitmask = 0xc480402c10080000ull;
  116. i = 45;
  117. mutex_lock(&dev->lock);
  118. outb(0, io);
  119. outb(0, io);
  120. inb(io + 3); /* Zoltrix needs to be read to confirm */
  121. outb(0x40, io);
  122. outb(0xc0, io);
  123. bitmask = (bitmask ^ ((f & 0xff) << 47) ^ ((f & 0xff00) << 30) ^ ( stereo << 31));
  124. while (i--) {
  125. if ((bitmask & 0x8000000000000000ull) != 0) {
  126. outb(0x80, io);
  127. udelay(50);
  128. outb(0x00, io);
  129. udelay(50);
  130. outb(0x80, io);
  131. udelay(50);
  132. } else {
  133. outb(0xc0, io);
  134. udelay(50);
  135. outb(0x40, io);
  136. udelay(50);
  137. outb(0xc0, io);
  138. udelay(50);
  139. }
  140. bitmask *= 2;
  141. }
  142. /* termination sequence */
  143. outb(0x80, io);
  144. outb(0xc0, io);
  145. outb(0x40, io);
  146. udelay(1000);
  147. inb(io+2);
  148. udelay(1000);
  149. if (dev->muted)
  150. {
  151. outb(0, io);
  152. outb(0, io);
  153. inb(io + 3);
  154. udelay(1000);
  155. }
  156. mutex_unlock(&dev->lock);
  157. if(!dev->muted)
  158. {
  159. zol_setvol(dev, dev->curvol);
  160. }
  161. return 0;
  162. }
  163. /* Get signal strength */
  164. static int zol_getsigstr(struct zol_device *dev)
  165. {
  166. int a, b;
  167. mutex_lock(&dev->lock);
  168. outb(0x00, io); /* This stuff I found to do nothing */
  169. outb(dev->curvol, io);
  170. msleep(20);
  171. a = inb(io);
  172. msleep(10);
  173. b = inb(io);
  174. mutex_unlock(&dev->lock);
  175. if (a != b)
  176. return (0);
  177. if ((a == 0xcf) || (a == 0xdf) /* I found this out by playing */
  178. || (a == 0xef)) /* with a binary scanner on the card io */
  179. return (1);
  180. return (0);
  181. }
  182. static int zol_is_stereo (struct zol_device *dev)
  183. {
  184. int x1, x2;
  185. mutex_lock(&dev->lock);
  186. outb(0x00, io);
  187. outb(dev->curvol, io);
  188. msleep(20);
  189. x1 = inb(io);
  190. msleep(10);
  191. x2 = inb(io);
  192. mutex_unlock(&dev->lock);
  193. if ((x1 == x2) && (x1 == 0xcf))
  194. return 1;
  195. return 0;
  196. }
  197. static int vidioc_querycap(struct file *file, void *priv,
  198. struct v4l2_capability *v)
  199. {
  200. strlcpy(v->driver, "radio-zoltrix", sizeof(v->driver));
  201. strlcpy(v->card, "Zoltrix Radio", sizeof(v->card));
  202. sprintf(v->bus_info, "ISA");
  203. v->version = RADIO_VERSION;
  204. v->capabilities = V4L2_CAP_TUNER;
  205. return 0;
  206. }
  207. static int vidioc_g_tuner(struct file *file, void *priv,
  208. struct v4l2_tuner *v)
  209. {
  210. struct video_device *dev = video_devdata(file);
  211. struct zol_device *zol = dev->priv;
  212. if (v->index > 0)
  213. return -EINVAL;
  214. strcpy(v->name, "FM");
  215. v->type = V4L2_TUNER_RADIO;
  216. v->rangelow = (88*16000);
  217. v->rangehigh = (108*16000);
  218. v->rxsubchans = V4L2_TUNER_SUB_MONO|V4L2_TUNER_SUB_STEREO;
  219. v->capability = V4L2_TUNER_CAP_LOW;
  220. if (zol_is_stereo(zol))
  221. v->audmode = V4L2_TUNER_MODE_STEREO;
  222. else
  223. v->audmode = V4L2_TUNER_MODE_MONO;
  224. v->signal = 0xFFFF*zol_getsigstr(zol);
  225. return 0;
  226. }
  227. static int vidioc_s_tuner(struct file *file, void *priv,
  228. struct v4l2_tuner *v)
  229. {
  230. if (v->index > 0)
  231. return -EINVAL;
  232. return 0;
  233. }
  234. static int vidioc_s_frequency(struct file *file, void *priv,
  235. struct v4l2_frequency *f)
  236. {
  237. struct video_device *dev = video_devdata(file);
  238. struct zol_device *zol = dev->priv;
  239. zol->curfreq = f->frequency;
  240. zol_setfreq(zol, zol->curfreq);
  241. return 0;
  242. }
  243. static int vidioc_g_frequency(struct file *file, void *priv,
  244. struct v4l2_frequency *f)
  245. {
  246. struct video_device *dev = video_devdata(file);
  247. struct zol_device *zol = dev->priv;
  248. f->type = V4L2_TUNER_RADIO;
  249. f->frequency = zol->curfreq;
  250. return 0;
  251. }
  252. static int vidioc_queryctrl(struct file *file, void *priv,
  253. struct v4l2_queryctrl *qc)
  254. {
  255. int i;
  256. for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
  257. if (qc->id && qc->id == radio_qctrl[i].id) {
  258. memcpy(qc, &(radio_qctrl[i]),
  259. sizeof(*qc));
  260. return 0;
  261. }
  262. }
  263. return -EINVAL;
  264. }
  265. static int vidioc_g_ctrl(struct file *file, void *priv,
  266. struct v4l2_control *ctrl)
  267. {
  268. struct video_device *dev = video_devdata(file);
  269. struct zol_device *zol = dev->priv;
  270. switch (ctrl->id) {
  271. case V4L2_CID_AUDIO_MUTE:
  272. ctrl->value = zol->muted;
  273. return 0;
  274. case V4L2_CID_AUDIO_VOLUME:
  275. ctrl->value = zol->curvol * 4096;
  276. return 0;
  277. }
  278. return -EINVAL;
  279. }
  280. static int vidioc_s_ctrl(struct file *file, void *priv,
  281. struct v4l2_control *ctrl)
  282. {
  283. struct video_device *dev = video_devdata(file);
  284. struct zol_device *zol = dev->priv;
  285. switch (ctrl->id) {
  286. case V4L2_CID_AUDIO_MUTE:
  287. if (ctrl->value)
  288. zol_mute(zol);
  289. else {
  290. zol_unmute(zol);
  291. zol_setvol(zol,zol->curvol);
  292. }
  293. return 0;
  294. case V4L2_CID_AUDIO_VOLUME:
  295. zol_setvol(zol,ctrl->value/4096);
  296. return 0;
  297. }
  298. zol->stereo = 1;
  299. zol_setfreq(zol, zol->curfreq);
  300. #if 0
  301. /* FIXME: Implement stereo/mono switch on V4L2 */
  302. if (v->mode & VIDEO_SOUND_STEREO) {
  303. zol->stereo = 1;
  304. zol_setfreq(zol, zol->curfreq);
  305. }
  306. if (v->mode & VIDEO_SOUND_MONO) {
  307. zol->stereo = 0;
  308. zol_setfreq(zol, zol->curfreq);
  309. }
  310. #endif
  311. return -EINVAL;
  312. }
  313. static int vidioc_g_audio(struct file *file, void *priv,
  314. struct v4l2_audio *a)
  315. {
  316. if (a->index > 1)
  317. return -EINVAL;
  318. strcpy(a->name, "Radio");
  319. a->capability = V4L2_AUDCAP_STEREO;
  320. return 0;
  321. }
  322. static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
  323. {
  324. *i = 0;
  325. return 0;
  326. }
  327. static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
  328. {
  329. if (i != 0)
  330. return -EINVAL;
  331. return 0;
  332. }
  333. static int vidioc_s_audio(struct file *file, void *priv,
  334. struct v4l2_audio *a)
  335. {
  336. if (a->index != 0)
  337. return -EINVAL;
  338. return 0;
  339. }
  340. static struct zol_device zoltrix_unit;
  341. static const struct file_operations zoltrix_fops =
  342. {
  343. .owner = THIS_MODULE,
  344. .open = video_exclusive_open,
  345. .release = video_exclusive_release,
  346. .ioctl = video_ioctl2,
  347. .compat_ioctl = v4l_compat_ioctl32,
  348. .llseek = no_llseek,
  349. };
  350. static struct video_device zoltrix_radio =
  351. {
  352. .owner = THIS_MODULE,
  353. .name = "Zoltrix Radio Plus",
  354. .type = VID_TYPE_TUNER,
  355. .fops = &zoltrix_fops,
  356. .vidioc_querycap = vidioc_querycap,
  357. .vidioc_g_tuner = vidioc_g_tuner,
  358. .vidioc_s_tuner = vidioc_s_tuner,
  359. .vidioc_g_audio = vidioc_g_audio,
  360. .vidioc_s_audio = vidioc_s_audio,
  361. .vidioc_g_input = vidioc_g_input,
  362. .vidioc_s_input = vidioc_s_input,
  363. .vidioc_g_frequency = vidioc_g_frequency,
  364. .vidioc_s_frequency = vidioc_s_frequency,
  365. .vidioc_queryctrl = vidioc_queryctrl,
  366. .vidioc_g_ctrl = vidioc_g_ctrl,
  367. .vidioc_s_ctrl = vidioc_s_ctrl,
  368. };
  369. static int __init zoltrix_init(void)
  370. {
  371. if (io == -1) {
  372. printk(KERN_ERR "You must set an I/O address with io=0x???\n");
  373. return -EINVAL;
  374. }
  375. if ((io != 0x20c) && (io != 0x30c)) {
  376. printk(KERN_ERR "zoltrix: invalid port, try 0x20c or 0x30c\n");
  377. return -ENXIO;
  378. }
  379. zoltrix_radio.priv = &zoltrix_unit;
  380. if (!request_region(io, 2, "zoltrix")) {
  381. printk(KERN_ERR "zoltrix: port 0x%x already in use\n", io);
  382. return -EBUSY;
  383. }
  384. if (video_register_device(&zoltrix_radio, VFL_TYPE_RADIO, radio_nr) == -1)
  385. {
  386. release_region(io, 2);
  387. return -EINVAL;
  388. }
  389. printk(KERN_INFO "Zoltrix Radio Plus card driver.\n");
  390. mutex_init(&zoltrix_unit.lock);
  391. /* mute card - prevents noisy bootups */
  392. /* this ensures that the volume is all the way down */
  393. outb(0, io);
  394. outb(0, io);
  395. msleep(20);
  396. inb(io + 3);
  397. zoltrix_unit.curvol = 0;
  398. zoltrix_unit.stereo = 1;
  399. return 0;
  400. }
  401. MODULE_AUTHOR("C.van Schaik");
  402. MODULE_DESCRIPTION("A driver for the Zoltrix Radio Plus.");
  403. MODULE_LICENSE("GPL");
  404. module_param(io, int, 0);
  405. MODULE_PARM_DESC(io, "I/O address of the Zoltrix Radio Plus (0x20c or 0x30c)");
  406. module_param(radio_nr, int, 0);
  407. static void __exit zoltrix_cleanup_module(void)
  408. {
  409. video_unregister_device(&zoltrix_radio);
  410. release_region(io, 2);
  411. }
  412. module_init(zoltrix_init);
  413. module_exit(zoltrix_cleanup_module);