radio-typhoon.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /* Typhoon Radio Card driver for radio support
  2. * (c) 1999 Dr. Henrik Seidel <Henrik.Seidel@gmx.de>
  3. *
  4. * Card manufacturer:
  5. * http://194.18.155.92/idc/prod2.idc?nr=50753&lang=e
  6. *
  7. * Notes on the hardware
  8. *
  9. * This card has two output sockets, one for speakers and one for line.
  10. * The speaker output has volume control, but only in four discrete
  11. * steps. The line output has neither volume control nor mute.
  12. *
  13. * The card has auto-stereo according to its manual, although it all
  14. * sounds mono to me (even with the Win/DOS drivers). Maybe it's my
  15. * antenna - I really don't know for sure.
  16. *
  17. * Frequency control is done digitally.
  18. *
  19. * Volume control is done digitally, but there are only four different
  20. * possible values. So you should better always turn the volume up and
  21. * use line control. I got the best results by connecting line output
  22. * to the sound card microphone input. For such a configuration the
  23. * volume control has no effect, since volume control only influences
  24. * the speaker output.
  25. *
  26. * There is no explicit mute/unmute. So I set the radio frequency to a
  27. * value where I do expect just noise and turn the speaker volume down.
  28. * The frequency change is necessary since the card never seems to be
  29. * completely silent.
  30. *
  31. * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
  32. */
  33. #include <linux/module.h> /* Modules */
  34. #include <linux/init.h> /* Initdata */
  35. #include <linux/ioport.h> /* request_region */
  36. #include <linux/version.h> /* for KERNEL_VERSION MACRO */
  37. #include <linux/videodev2.h> /* kernel radio structs */
  38. #include <linux/io.h> /* outb, outb_p */
  39. #include <media/v4l2-device.h>
  40. #include <media/v4l2-ioctl.h>
  41. MODULE_AUTHOR("Dr. Henrik Seidel");
  42. MODULE_DESCRIPTION("A driver for the Typhoon radio card (a.k.a. EcoRadio).");
  43. MODULE_LICENSE("GPL");
  44. #ifndef CONFIG_RADIO_TYPHOON_PORT
  45. #define CONFIG_RADIO_TYPHOON_PORT -1
  46. #endif
  47. #ifndef CONFIG_RADIO_TYPHOON_MUTEFREQ
  48. #define CONFIG_RADIO_TYPHOON_MUTEFREQ 0
  49. #endif
  50. static int io = CONFIG_RADIO_TYPHOON_PORT;
  51. static int radio_nr = -1;
  52. module_param(io, int, 0);
  53. MODULE_PARM_DESC(io, "I/O address of the Typhoon card (0x316 or 0x336)");
  54. module_param(radio_nr, int, 0);
  55. static unsigned long mutefreq = CONFIG_RADIO_TYPHOON_MUTEFREQ;
  56. module_param(mutefreq, ulong, 0);
  57. MODULE_PARM_DESC(mutefreq, "Frequency used when muting the card (in kHz)");
  58. #define RADIO_VERSION KERNEL_VERSION(0, 1, 1)
  59. #define BANNER "Typhoon Radio Card driver v0.1.1\n"
  60. struct typhoon {
  61. struct v4l2_device v4l2_dev;
  62. struct video_device vdev;
  63. int io;
  64. int curvol;
  65. int muted;
  66. unsigned long curfreq;
  67. unsigned long mutefreq;
  68. struct mutex lock;
  69. };
  70. static struct typhoon typhoon_card;
  71. static void typhoon_setvol_generic(struct typhoon *dev, int vol)
  72. {
  73. mutex_lock(&dev->lock);
  74. vol >>= 14; /* Map 16 bit to 2 bit */
  75. vol &= 3;
  76. outb_p(vol / 2, dev->io); /* Set the volume, high bit. */
  77. outb_p(vol % 2, dev->io + 2); /* Set the volume, low bit. */
  78. mutex_unlock(&dev->lock);
  79. }
  80. static int typhoon_setfreq_generic(struct typhoon *dev,
  81. unsigned long frequency)
  82. {
  83. unsigned long outval;
  84. unsigned long x;
  85. /*
  86. * The frequency transfer curve is not linear. The best fit I could
  87. * get is
  88. *
  89. * outval = -155 + exp((f + 15.55) * 0.057))
  90. *
  91. * where frequency f is in MHz. Since we don't have exp in the kernel,
  92. * I approximate this function by a third order polynomial.
  93. *
  94. */
  95. mutex_lock(&dev->lock);
  96. x = frequency / 160;
  97. outval = (x * x + 2500) / 5000;
  98. outval = (outval * x + 5000) / 10000;
  99. outval -= (10 * x * x + 10433) / 20866;
  100. outval += 4 * x - 11505;
  101. outb_p((outval >> 8) & 0x01, dev->io + 4);
  102. outb_p(outval >> 9, dev->io + 6);
  103. outb_p(outval & 0xff, dev->io + 8);
  104. mutex_unlock(&dev->lock);
  105. return 0;
  106. }
  107. static int typhoon_setfreq(struct typhoon *dev, unsigned long frequency)
  108. {
  109. typhoon_setfreq_generic(dev, frequency);
  110. dev->curfreq = frequency;
  111. return 0;
  112. }
  113. static void typhoon_mute(struct typhoon *dev)
  114. {
  115. if (dev->muted == 1)
  116. return;
  117. typhoon_setvol_generic(dev, 0);
  118. typhoon_setfreq_generic(dev, dev->mutefreq);
  119. dev->muted = 1;
  120. }
  121. static void typhoon_unmute(struct typhoon *dev)
  122. {
  123. if (dev->muted == 0)
  124. return;
  125. typhoon_setfreq_generic(dev, dev->curfreq);
  126. typhoon_setvol_generic(dev, dev->curvol);
  127. dev->muted = 0;
  128. }
  129. static int typhoon_setvol(struct typhoon *dev, int vol)
  130. {
  131. if (dev->muted && vol != 0) { /* user is unmuting the card */
  132. dev->curvol = vol;
  133. typhoon_unmute(dev);
  134. return 0;
  135. }
  136. if (vol == dev->curvol) /* requested volume == current */
  137. return 0;
  138. if (vol == 0) { /* volume == 0 means mute the card */
  139. typhoon_mute(dev);
  140. dev->curvol = vol;
  141. return 0;
  142. }
  143. typhoon_setvol_generic(dev, vol);
  144. dev->curvol = vol;
  145. return 0;
  146. }
  147. static int vidioc_querycap(struct file *file, void *priv,
  148. struct v4l2_capability *v)
  149. {
  150. strlcpy(v->driver, "radio-typhoon", sizeof(v->driver));
  151. strlcpy(v->card, "Typhoon Radio", sizeof(v->card));
  152. strlcpy(v->bus_info, "ISA", sizeof(v->bus_info));
  153. v->version = RADIO_VERSION;
  154. v->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
  155. return 0;
  156. }
  157. static int vidioc_g_tuner(struct file *file, void *priv,
  158. struct v4l2_tuner *v)
  159. {
  160. if (v->index > 0)
  161. return -EINVAL;
  162. strlcpy(v->name, "FM", sizeof(v->name));
  163. v->type = V4L2_TUNER_RADIO;
  164. v->rangelow = 87.5 * 16000;
  165. v->rangehigh = 108 * 16000;
  166. v->rxsubchans = V4L2_TUNER_SUB_MONO;
  167. v->capability = V4L2_TUNER_CAP_LOW;
  168. v->audmode = V4L2_TUNER_MODE_MONO;
  169. v->signal = 0xFFFF; /* We can't get the signal strength */
  170. return 0;
  171. }
  172. static int vidioc_s_tuner(struct file *file, void *priv,
  173. struct v4l2_tuner *v)
  174. {
  175. return v->index ? -EINVAL : 0;
  176. }
  177. static int vidioc_g_frequency(struct file *file, void *priv,
  178. struct v4l2_frequency *f)
  179. {
  180. struct typhoon *dev = video_drvdata(file);
  181. if (f->tuner != 0)
  182. return -EINVAL;
  183. f->type = V4L2_TUNER_RADIO;
  184. f->frequency = dev->curfreq;
  185. return 0;
  186. }
  187. static int vidioc_s_frequency(struct file *file, void *priv,
  188. struct v4l2_frequency *f)
  189. {
  190. struct typhoon *dev = video_drvdata(file);
  191. if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
  192. return -EINVAL;
  193. dev->curfreq = f->frequency;
  194. typhoon_setfreq(dev, dev->curfreq);
  195. return 0;
  196. }
  197. static int vidioc_queryctrl(struct file *file, void *priv,
  198. struct v4l2_queryctrl *qc)
  199. {
  200. switch (qc->id) {
  201. case V4L2_CID_AUDIO_MUTE:
  202. return v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
  203. case V4L2_CID_AUDIO_VOLUME:
  204. return v4l2_ctrl_query_fill(qc, 0, 65535, 16384, 65535);
  205. }
  206. return -EINVAL;
  207. }
  208. static int vidioc_g_ctrl(struct file *file, void *priv,
  209. struct v4l2_control *ctrl)
  210. {
  211. struct typhoon *dev = video_drvdata(file);
  212. switch (ctrl->id) {
  213. case V4L2_CID_AUDIO_MUTE:
  214. ctrl->value = dev->muted;
  215. return 0;
  216. case V4L2_CID_AUDIO_VOLUME:
  217. ctrl->value = dev->curvol;
  218. return 0;
  219. }
  220. return -EINVAL;
  221. }
  222. static int vidioc_s_ctrl (struct file *file, void *priv,
  223. struct v4l2_control *ctrl)
  224. {
  225. struct typhoon *dev = video_drvdata(file);
  226. switch (ctrl->id) {
  227. case V4L2_CID_AUDIO_MUTE:
  228. if (ctrl->value)
  229. typhoon_mute(dev);
  230. else
  231. typhoon_unmute(dev);
  232. return 0;
  233. case V4L2_CID_AUDIO_VOLUME:
  234. typhoon_setvol(dev, ctrl->value);
  235. return 0;
  236. }
  237. return -EINVAL;
  238. }
  239. static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
  240. {
  241. *i = 0;
  242. return 0;
  243. }
  244. static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
  245. {
  246. return i ? -EINVAL : 0;
  247. }
  248. static int vidioc_g_audio(struct file *file, void *priv,
  249. struct v4l2_audio *a)
  250. {
  251. a->index = 0;
  252. strlcpy(a->name, "Radio", sizeof(a->name));
  253. a->capability = V4L2_AUDCAP_STEREO;
  254. return 0;
  255. }
  256. static int vidioc_s_audio(struct file *file, void *priv,
  257. struct v4l2_audio *a)
  258. {
  259. return a->index ? -EINVAL : 0;
  260. }
  261. static int vidioc_log_status(struct file *file, void *priv)
  262. {
  263. struct typhoon *dev = video_drvdata(file);
  264. struct v4l2_device *v4l2_dev = &dev->v4l2_dev;
  265. v4l2_info(v4l2_dev, BANNER);
  266. #ifdef MODULE
  267. v4l2_info(v4l2_dev, "Load type: Driver loaded as a module\n\n");
  268. #else
  269. v4l2_info(v4l2_dev, "Load type: Driver compiled into kernel\n\n");
  270. #endif
  271. v4l2_info(v4l2_dev, "frequency = %lu kHz\n", dev->curfreq >> 4);
  272. v4l2_info(v4l2_dev, "volume = %d\n", dev->curvol);
  273. v4l2_info(v4l2_dev, "mute = %s\n", dev->muted ? "on" : "off");
  274. v4l2_info(v4l2_dev, "io = 0x%x\n", dev->io);
  275. v4l2_info(v4l2_dev, "mute frequency = %lu kHz\n", dev->mutefreq >> 4);
  276. return 0;
  277. }
  278. static const struct v4l2_file_operations typhoon_fops = {
  279. .owner = THIS_MODULE,
  280. .ioctl = video_ioctl2,
  281. };
  282. static const struct v4l2_ioctl_ops typhoon_ioctl_ops = {
  283. .vidioc_log_status = vidioc_log_status,
  284. .vidioc_querycap = vidioc_querycap,
  285. .vidioc_g_tuner = vidioc_g_tuner,
  286. .vidioc_s_tuner = vidioc_s_tuner,
  287. .vidioc_g_audio = vidioc_g_audio,
  288. .vidioc_s_audio = vidioc_s_audio,
  289. .vidioc_g_input = vidioc_g_input,
  290. .vidioc_s_input = vidioc_s_input,
  291. .vidioc_g_frequency = vidioc_g_frequency,
  292. .vidioc_s_frequency = vidioc_s_frequency,
  293. .vidioc_queryctrl = vidioc_queryctrl,
  294. .vidioc_g_ctrl = vidioc_g_ctrl,
  295. .vidioc_s_ctrl = vidioc_s_ctrl,
  296. };
  297. static int __init typhoon_init(void)
  298. {
  299. struct typhoon *dev = &typhoon_card;
  300. struct v4l2_device *v4l2_dev = &dev->v4l2_dev;
  301. int res;
  302. strlcpy(v4l2_dev->name, "typhoon", sizeof(v4l2_dev->name));
  303. dev->io = io;
  304. dev->curfreq = dev->mutefreq = mutefreq;
  305. if (dev->io == -1) {
  306. v4l2_err(v4l2_dev, "You must set an I/O address with io=0x316 or io=0x336\n");
  307. return -EINVAL;
  308. }
  309. if (dev->mutefreq < 87000 || dev->mutefreq > 108500) {
  310. v4l2_err(v4l2_dev, "You must set a frequency (in kHz) used when muting the card,\n");
  311. v4l2_err(v4l2_dev, "e.g. with \"mutefreq=87500\" (87000 <= mutefreq <= 108500)\n");
  312. return -EINVAL;
  313. }
  314. mutex_init(&dev->lock);
  315. if (!request_region(dev->io, 8, "typhoon")) {
  316. v4l2_err(v4l2_dev, "port 0x%x already in use\n",
  317. dev->io);
  318. return -EBUSY;
  319. }
  320. res = v4l2_device_register(NULL, v4l2_dev);
  321. if (res < 0) {
  322. release_region(dev->io, 8);
  323. v4l2_err(v4l2_dev, "Could not register v4l2_device\n");
  324. return res;
  325. }
  326. v4l2_info(v4l2_dev, BANNER);
  327. strlcpy(dev->vdev.name, v4l2_dev->name, sizeof(dev->vdev.name));
  328. dev->vdev.v4l2_dev = v4l2_dev;
  329. dev->vdev.fops = &typhoon_fops;
  330. dev->vdev.ioctl_ops = &typhoon_ioctl_ops;
  331. dev->vdev.release = video_device_release_empty;
  332. video_set_drvdata(&dev->vdev, dev);
  333. if (video_register_device(&dev->vdev, VFL_TYPE_RADIO, radio_nr) < 0) {
  334. v4l2_device_unregister(&dev->v4l2_dev);
  335. release_region(dev->io, 8);
  336. return -EINVAL;
  337. }
  338. v4l2_info(v4l2_dev, "port 0x%x.\n", dev->io);
  339. v4l2_info(v4l2_dev, "mute frequency is %lu kHz.\n", dev->mutefreq);
  340. dev->mutefreq <<= 4;
  341. /* mute card - prevents noisy bootups */
  342. typhoon_mute(dev);
  343. return 0;
  344. }
  345. static void __exit typhoon_exit(void)
  346. {
  347. struct typhoon *dev = &typhoon_card;
  348. video_unregister_device(&dev->vdev);
  349. v4l2_device_unregister(&dev->v4l2_dev);
  350. release_region(dev->io, 8);
  351. }
  352. module_init(typhoon_init);
  353. module_exit(typhoon_exit);