radio-zoltrix.c 11 KB

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