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