radio-zoltrix.c 11 KB

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