radio-zoltrix.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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 video_device *dev = video_devdata(file);
  213. struct zol_device *zol = dev->priv;
  214. if (v->index > 0)
  215. return -EINVAL;
  216. strcpy(v->name, "FM");
  217. v->type = V4L2_TUNER_RADIO;
  218. v->rangelow = (88*16000);
  219. v->rangehigh = (108*16000);
  220. v->rxsubchans = V4L2_TUNER_SUB_MONO|V4L2_TUNER_SUB_STEREO;
  221. v->capability = V4L2_TUNER_CAP_LOW;
  222. if (zol_is_stereo(zol))
  223. v->audmode = V4L2_TUNER_MODE_STEREO;
  224. else
  225. v->audmode = V4L2_TUNER_MODE_MONO;
  226. v->signal = 0xFFFF*zol_getsigstr(zol);
  227. return 0;
  228. }
  229. static int vidioc_s_tuner(struct file *file, void *priv,
  230. struct v4l2_tuner *v)
  231. {
  232. if (v->index > 0)
  233. return -EINVAL;
  234. return 0;
  235. }
  236. static int vidioc_s_frequency(struct file *file, void *priv,
  237. struct v4l2_frequency *f)
  238. {
  239. struct video_device *dev = video_devdata(file);
  240. struct zol_device *zol = dev->priv;
  241. zol->curfreq = f->frequency;
  242. zol_setfreq(zol, zol->curfreq);
  243. return 0;
  244. }
  245. static int vidioc_g_frequency(struct file *file, void *priv,
  246. struct v4l2_frequency *f)
  247. {
  248. struct video_device *dev = video_devdata(file);
  249. struct zol_device *zol = dev->priv;
  250. f->type = V4L2_TUNER_RADIO;
  251. f->frequency = zol->curfreq;
  252. return 0;
  253. }
  254. static int vidioc_queryctrl(struct file *file, void *priv,
  255. struct v4l2_queryctrl *qc)
  256. {
  257. int i;
  258. for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
  259. if (qc->id && qc->id == radio_qctrl[i].id) {
  260. memcpy(qc, &(radio_qctrl[i]),
  261. sizeof(*qc));
  262. return 0;
  263. }
  264. }
  265. return -EINVAL;
  266. }
  267. static int vidioc_g_ctrl(struct file *file, void *priv,
  268. struct v4l2_control *ctrl)
  269. {
  270. struct video_device *dev = video_devdata(file);
  271. struct zol_device *zol = dev->priv;
  272. switch (ctrl->id) {
  273. case V4L2_CID_AUDIO_MUTE:
  274. ctrl->value = zol->muted;
  275. return 0;
  276. case V4L2_CID_AUDIO_VOLUME:
  277. ctrl->value = zol->curvol * 4096;
  278. return 0;
  279. }
  280. return -EINVAL;
  281. }
  282. static int vidioc_s_ctrl(struct file *file, void *priv,
  283. struct v4l2_control *ctrl)
  284. {
  285. struct video_device *dev = video_devdata(file);
  286. struct zol_device *zol = dev->priv;
  287. switch (ctrl->id) {
  288. case V4L2_CID_AUDIO_MUTE:
  289. if (ctrl->value)
  290. zol_mute(zol);
  291. else {
  292. zol_unmute(zol);
  293. zol_setvol(zol,zol->curvol);
  294. }
  295. return 0;
  296. case V4L2_CID_AUDIO_VOLUME:
  297. zol_setvol(zol,ctrl->value/4096);
  298. return 0;
  299. }
  300. zol->stereo = 1;
  301. zol_setfreq(zol, zol->curfreq);
  302. #if 0
  303. /* FIXME: Implement stereo/mono switch on V4L2 */
  304. if (v->mode & VIDEO_SOUND_STEREO) {
  305. zol->stereo = 1;
  306. zol_setfreq(zol, zol->curfreq);
  307. }
  308. if (v->mode & VIDEO_SOUND_MONO) {
  309. zol->stereo = 0;
  310. zol_setfreq(zol, zol->curfreq);
  311. }
  312. #endif
  313. return -EINVAL;
  314. }
  315. static int vidioc_g_audio(struct file *file, void *priv,
  316. struct v4l2_audio *a)
  317. {
  318. if (a->index > 1)
  319. return -EINVAL;
  320. strcpy(a->name, "Radio");
  321. a->capability = V4L2_AUDCAP_STEREO;
  322. return 0;
  323. }
  324. static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
  325. {
  326. *i = 0;
  327. return 0;
  328. }
  329. static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
  330. {
  331. if (i != 0)
  332. return -EINVAL;
  333. return 0;
  334. }
  335. static int vidioc_s_audio(struct file *file, void *priv,
  336. struct v4l2_audio *a)
  337. {
  338. if (a->index != 0)
  339. return -EINVAL;
  340. return 0;
  341. }
  342. static struct zol_device zoltrix_unit;
  343. static int zoltrix_exclusive_open(struct inode *inode, struct file *file)
  344. {
  345. return test_and_set_bit(0, &zoltrix_unit.in_use) ? -EBUSY : 0;
  346. }
  347. static int zoltrix_exclusive_release(struct inode *inode, struct file *file)
  348. {
  349. clear_bit(0, &zoltrix_unit.in_use);
  350. return 0;
  351. }
  352. static const struct file_operations zoltrix_fops =
  353. {
  354. .owner = THIS_MODULE,
  355. .open = zoltrix_exclusive_open,
  356. .release = zoltrix_exclusive_release,
  357. .ioctl = video_ioctl2,
  358. #ifdef CONFIG_COMPAT
  359. .compat_ioctl = v4l_compat_ioctl32,
  360. #endif
  361. .llseek = no_llseek,
  362. };
  363. static const struct v4l2_ioctl_ops zoltrix_ioctl_ops = {
  364. .vidioc_querycap = vidioc_querycap,
  365. .vidioc_g_tuner = vidioc_g_tuner,
  366. .vidioc_s_tuner = vidioc_s_tuner,
  367. .vidioc_g_audio = vidioc_g_audio,
  368. .vidioc_s_audio = vidioc_s_audio,
  369. .vidioc_g_input = vidioc_g_input,
  370. .vidioc_s_input = vidioc_s_input,
  371. .vidioc_g_frequency = vidioc_g_frequency,
  372. .vidioc_s_frequency = vidioc_s_frequency,
  373. .vidioc_queryctrl = vidioc_queryctrl,
  374. .vidioc_g_ctrl = vidioc_g_ctrl,
  375. .vidioc_s_ctrl = vidioc_s_ctrl,
  376. };
  377. static struct video_device zoltrix_radio = {
  378. .name = "Zoltrix Radio Plus",
  379. .fops = &zoltrix_fops,
  380. .ioctl_ops = &zoltrix_ioctl_ops,
  381. .release = video_device_release_empty,
  382. };
  383. static int __init zoltrix_init(void)
  384. {
  385. if (io == -1) {
  386. printk(KERN_ERR "You must set an I/O address with io=0x???\n");
  387. return -EINVAL;
  388. }
  389. if ((io != 0x20c) && (io != 0x30c)) {
  390. printk(KERN_ERR "zoltrix: invalid port, try 0x20c or 0x30c\n");
  391. return -ENXIO;
  392. }
  393. zoltrix_radio.priv = &zoltrix_unit;
  394. if (!request_region(io, 2, "zoltrix")) {
  395. printk(KERN_ERR "zoltrix: port 0x%x already in use\n", io);
  396. return -EBUSY;
  397. }
  398. if (video_register_device(&zoltrix_radio, VFL_TYPE_RADIO, radio_nr) < 0) {
  399. release_region(io, 2);
  400. return -EINVAL;
  401. }
  402. printk(KERN_INFO "Zoltrix Radio Plus card driver.\n");
  403. mutex_init(&zoltrix_unit.lock);
  404. /* mute card - prevents noisy bootups */
  405. /* this ensures that the volume is all the way down */
  406. outb(0, io);
  407. outb(0, io);
  408. msleep(20);
  409. inb(io + 3);
  410. zoltrix_unit.curvol = 0;
  411. zoltrix_unit.stereo = 1;
  412. return 0;
  413. }
  414. MODULE_AUTHOR("C.van Schaik");
  415. MODULE_DESCRIPTION("A driver for the Zoltrix Radio Plus.");
  416. MODULE_LICENSE("GPL");
  417. module_param(io, int, 0);
  418. MODULE_PARM_DESC(io, "I/O address of the Zoltrix Radio Plus (0x20c or 0x30c)");
  419. module_param(radio_nr, int, 0);
  420. static void __exit zoltrix_cleanup_module(void)
  421. {
  422. video_unregister_device(&zoltrix_radio);
  423. release_region(io, 2);
  424. }
  425. module_init(zoltrix_init);
  426. module_exit(zoltrix_cleanup_module);