radio-zoltrix.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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 zol_do_ioctl(struct inode *inode, struct file *file,
  198. unsigned int cmd, void *arg)
  199. {
  200. struct video_device *dev = video_devdata(file);
  201. struct zol_device *zol = dev->priv;
  202. switch (cmd) {
  203. case VIDIOC_QUERYCAP:
  204. {
  205. struct v4l2_capability *v = arg;
  206. memset(v,0,sizeof(*v));
  207. strlcpy(v->driver, "radio-zoltrix", sizeof (v->driver));
  208. strlcpy(v->card, "Zoltrix Radio", sizeof (v->card));
  209. sprintf(v->bus_info,"ISA");
  210. v->version = RADIO_VERSION;
  211. v->capabilities = V4L2_CAP_TUNER;
  212. return 0;
  213. }
  214. case VIDIOC_G_TUNER:
  215. {
  216. struct v4l2_tuner *v = arg;
  217. if (v->index > 0)
  218. return -EINVAL;
  219. memset(v,0,sizeof(*v));
  220. strcpy(v->name, "FM");
  221. v->type = V4L2_TUNER_RADIO;
  222. v->rangelow=(88*16000);
  223. v->rangehigh=(108*16000);
  224. v->rxsubchans =V4L2_TUNER_SUB_MONO|V4L2_TUNER_SUB_STEREO;
  225. v->capability=V4L2_TUNER_CAP_LOW;
  226. if(zol_is_stereo(zol))
  227. v->audmode = V4L2_TUNER_MODE_STEREO;
  228. else
  229. v->audmode = V4L2_TUNER_MODE_MONO;
  230. v->signal=0xFFFF*zol_getsigstr(zol);
  231. return 0;
  232. }
  233. case VIDIOC_S_TUNER:
  234. {
  235. struct v4l2_tuner *v = arg;
  236. if (v->index > 0)
  237. return -EINVAL;
  238. return 0;
  239. }
  240. case VIDIOC_S_FREQUENCY:
  241. {
  242. struct v4l2_frequency *f = arg;
  243. zol->curfreq = f->frequency;
  244. zol_setfreq(zol, zol->curfreq);
  245. return 0;
  246. }
  247. case VIDIOC_G_FREQUENCY:
  248. {
  249. struct v4l2_frequency *f = arg;
  250. f->type = V4L2_TUNER_RADIO;
  251. f->frequency = zol->curfreq;
  252. return 0;
  253. }
  254. case VIDIOC_QUERYCTRL:
  255. {
  256. struct v4l2_queryctrl *qc = arg;
  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. case VIDIOC_G_CTRL:
  268. {
  269. struct v4l2_control *ctrl= arg;
  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. case VIDIOC_S_CTRL:
  281. {
  282. struct v4l2_control *ctrl= arg;
  283. switch (ctrl->id) {
  284. case V4L2_CID_AUDIO_MUTE:
  285. if (ctrl->value) {
  286. zol_mute(zol);
  287. } else {
  288. zol_unmute(zol);
  289. zol_setvol(zol,zol->curvol);
  290. }
  291. return (0);
  292. case V4L2_CID_AUDIO_VOLUME:
  293. zol_setvol(zol,ctrl->value/4096);
  294. return (0);
  295. }
  296. zol->stereo = 1;
  297. zol_setfreq(zol, zol->curfreq);
  298. #if 0
  299. /* FIXME: Implement stereo/mono switch on V4L2 */
  300. if (v->mode & VIDEO_SOUND_STEREO) {
  301. zol->stereo = 1;
  302. zol_setfreq(zol, zol->curfreq);
  303. }
  304. if (v->mode & VIDEO_SOUND_MONO) {
  305. zol->stereo = 0;
  306. zol_setfreq(zol, zol->curfreq);
  307. }
  308. #endif
  309. return -EINVAL;
  310. }
  311. default:
  312. return v4l_compat_translate_ioctl(inode,file,cmd,arg,
  313. zol_do_ioctl);
  314. }
  315. }
  316. static int zol_ioctl(struct inode *inode, struct file *file,
  317. unsigned int cmd, unsigned long arg)
  318. {
  319. return video_usercopy(inode, file, cmd, arg, zol_do_ioctl);
  320. }
  321. static struct zol_device zoltrix_unit;
  322. static struct file_operations zoltrix_fops =
  323. {
  324. .owner = THIS_MODULE,
  325. .open = video_exclusive_open,
  326. .release = video_exclusive_release,
  327. .ioctl = zol_ioctl,
  328. .compat_ioctl = v4l_compat_ioctl32,
  329. .llseek = no_llseek,
  330. };
  331. static struct video_device zoltrix_radio =
  332. {
  333. .owner = THIS_MODULE,
  334. .name = "Zoltrix Radio Plus",
  335. .type = VID_TYPE_TUNER,
  336. .hardware = 0,
  337. .fops = &zoltrix_fops,
  338. };
  339. static int __init zoltrix_init(void)
  340. {
  341. if (io == -1) {
  342. printk(KERN_ERR "You must set an I/O address with io=0x???\n");
  343. return -EINVAL;
  344. }
  345. if ((io != 0x20c) && (io != 0x30c)) {
  346. printk(KERN_ERR "zoltrix: invalid port, try 0x20c or 0x30c\n");
  347. return -ENXIO;
  348. }
  349. zoltrix_radio.priv = &zoltrix_unit;
  350. if (!request_region(io, 2, "zoltrix")) {
  351. printk(KERN_ERR "zoltrix: port 0x%x already in use\n", io);
  352. return -EBUSY;
  353. }
  354. if (video_register_device(&zoltrix_radio, VFL_TYPE_RADIO, radio_nr) == -1)
  355. {
  356. release_region(io, 2);
  357. return -EINVAL;
  358. }
  359. printk(KERN_INFO "Zoltrix Radio Plus card driver.\n");
  360. mutex_init(&zoltrix_unit.lock);
  361. /* mute card - prevents noisy bootups */
  362. /* this ensures that the volume is all the way down */
  363. outb(0, io);
  364. outb(0, io);
  365. msleep(20);
  366. inb(io + 3);
  367. zoltrix_unit.curvol = 0;
  368. zoltrix_unit.stereo = 1;
  369. return 0;
  370. }
  371. MODULE_AUTHOR("C.van Schaik");
  372. MODULE_DESCRIPTION("A driver for the Zoltrix Radio Plus.");
  373. MODULE_LICENSE("GPL");
  374. module_param(io, int, 0);
  375. MODULE_PARM_DESC(io, "I/O address of the Zoltrix Radio Plus (0x20c or 0x30c)");
  376. module_param(radio_nr, int, 0);
  377. static void __exit zoltrix_cleanup_module(void)
  378. {
  379. video_unregister_device(&zoltrix_radio);
  380. release_region(io, 2);
  381. }
  382. module_init(zoltrix_init);
  383. module_exit(zoltrix_cleanup_module);