radio-zoltrix.c 11 KB

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