radio-zoltrix.c 11 KB

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