radio-typhoon.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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. f->type = V4L2_TUNER_RADIO;
  182. f->frequency = dev->curfreq;
  183. return 0;
  184. }
  185. static int vidioc_s_frequency(struct file *file, void *priv,
  186. struct v4l2_frequency *f)
  187. {
  188. struct typhoon *dev = video_drvdata(file);
  189. dev->curfreq = f->frequency;
  190. typhoon_setfreq(dev, dev->curfreq);
  191. return 0;
  192. }
  193. static int vidioc_queryctrl(struct file *file, void *priv,
  194. struct v4l2_queryctrl *qc)
  195. {
  196. switch (qc->id) {
  197. case V4L2_CID_AUDIO_MUTE:
  198. return v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
  199. case V4L2_CID_AUDIO_VOLUME:
  200. return v4l2_ctrl_query_fill(qc, 0, 65535, 16384, 65535);
  201. }
  202. return -EINVAL;
  203. }
  204. static int vidioc_g_ctrl(struct file *file, void *priv,
  205. struct v4l2_control *ctrl)
  206. {
  207. struct typhoon *dev = video_drvdata(file);
  208. switch (ctrl->id) {
  209. case V4L2_CID_AUDIO_MUTE:
  210. ctrl->value = dev->muted;
  211. return 0;
  212. case V4L2_CID_AUDIO_VOLUME:
  213. ctrl->value = dev->curvol;
  214. return 0;
  215. }
  216. return -EINVAL;
  217. }
  218. static int vidioc_s_ctrl (struct file *file, void *priv,
  219. struct v4l2_control *ctrl)
  220. {
  221. struct typhoon *dev = video_drvdata(file);
  222. switch (ctrl->id) {
  223. case V4L2_CID_AUDIO_MUTE:
  224. if (ctrl->value)
  225. typhoon_mute(dev);
  226. else
  227. typhoon_unmute(dev);
  228. return 0;
  229. case V4L2_CID_AUDIO_VOLUME:
  230. typhoon_setvol(dev, ctrl->value);
  231. return 0;
  232. }
  233. return -EINVAL;
  234. }
  235. static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
  236. {
  237. *i = 0;
  238. return 0;
  239. }
  240. static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
  241. {
  242. return i ? -EINVAL : 0;
  243. }
  244. static int vidioc_g_audio(struct file *file, void *priv,
  245. struct v4l2_audio *a)
  246. {
  247. a->index = 0;
  248. strlcpy(a->name, "Radio", sizeof(a->name));
  249. a->capability = V4L2_AUDCAP_STEREO;
  250. return 0;
  251. }
  252. static int vidioc_s_audio(struct file *file, void *priv,
  253. struct v4l2_audio *a)
  254. {
  255. return a->index ? -EINVAL : 0;
  256. }
  257. static int vidioc_log_status(struct file *file, void *priv)
  258. {
  259. struct typhoon *dev = video_drvdata(file);
  260. struct v4l2_device *v4l2_dev = &dev->v4l2_dev;
  261. v4l2_info(v4l2_dev, BANNER);
  262. #ifdef MODULE
  263. v4l2_info(v4l2_dev, "Load type: Driver loaded as a module\n\n");
  264. #else
  265. v4l2_info(v4l2_dev, "Load type: Driver compiled into kernel\n\n");
  266. #endif
  267. v4l2_info(v4l2_dev, "frequency = %lu kHz\n", dev->curfreq >> 4);
  268. v4l2_info(v4l2_dev, "volume = %d\n", dev->curvol);
  269. v4l2_info(v4l2_dev, "mute = %s\n", dev->muted ? "on" : "off");
  270. v4l2_info(v4l2_dev, "io = 0x%x\n", dev->io);
  271. v4l2_info(v4l2_dev, "mute frequency = %lu kHz\n", dev->mutefreq >> 4);
  272. return 0;
  273. }
  274. static const struct v4l2_file_operations typhoon_fops = {
  275. .owner = THIS_MODULE,
  276. .ioctl = video_ioctl2,
  277. };
  278. static const struct v4l2_ioctl_ops typhoon_ioctl_ops = {
  279. .vidioc_log_status = vidioc_log_status,
  280. .vidioc_querycap = vidioc_querycap,
  281. .vidioc_g_tuner = vidioc_g_tuner,
  282. .vidioc_s_tuner = vidioc_s_tuner,
  283. .vidioc_g_audio = vidioc_g_audio,
  284. .vidioc_s_audio = vidioc_s_audio,
  285. .vidioc_g_input = vidioc_g_input,
  286. .vidioc_s_input = vidioc_s_input,
  287. .vidioc_g_frequency = vidioc_g_frequency,
  288. .vidioc_s_frequency = vidioc_s_frequency,
  289. .vidioc_queryctrl = vidioc_queryctrl,
  290. .vidioc_g_ctrl = vidioc_g_ctrl,
  291. .vidioc_s_ctrl = vidioc_s_ctrl,
  292. };
  293. static int __init typhoon_init(void)
  294. {
  295. struct typhoon *dev = &typhoon_card;
  296. struct v4l2_device *v4l2_dev = &dev->v4l2_dev;
  297. int res;
  298. strlcpy(v4l2_dev->name, "typhoon", sizeof(v4l2_dev->name));
  299. dev->io = io;
  300. dev->curfreq = dev->mutefreq = mutefreq;
  301. if (dev->io == -1) {
  302. v4l2_err(v4l2_dev, "You must set an I/O address with io=0x316 or io=0x336\n");
  303. return -EINVAL;
  304. }
  305. if (dev->mutefreq < 87000 || dev->mutefreq > 108500) {
  306. v4l2_err(v4l2_dev, "You must set a frequency (in kHz) used when muting the card,\n");
  307. v4l2_err(v4l2_dev, "e.g. with \"mutefreq=87500\" (87000 <= mutefreq <= 108500)\n");
  308. return -EINVAL;
  309. }
  310. mutex_init(&dev->lock);
  311. if (!request_region(dev->io, 8, "typhoon")) {
  312. v4l2_err(v4l2_dev, "port 0x%x already in use\n",
  313. dev->io);
  314. return -EBUSY;
  315. }
  316. res = v4l2_device_register(NULL, v4l2_dev);
  317. if (res < 0) {
  318. release_region(dev->io, 8);
  319. v4l2_err(v4l2_dev, "Could not register v4l2_device\n");
  320. return res;
  321. }
  322. v4l2_info(v4l2_dev, BANNER);
  323. strlcpy(dev->vdev.name, v4l2_dev->name, sizeof(dev->vdev.name));
  324. dev->vdev.v4l2_dev = v4l2_dev;
  325. dev->vdev.fops = &typhoon_fops;
  326. dev->vdev.ioctl_ops = &typhoon_ioctl_ops;
  327. dev->vdev.release = video_device_release_empty;
  328. video_set_drvdata(&dev->vdev, dev);
  329. if (video_register_device(&dev->vdev, VFL_TYPE_RADIO, radio_nr) < 0) {
  330. v4l2_device_unregister(&dev->v4l2_dev);
  331. release_region(dev->io, 8);
  332. return -EINVAL;
  333. }
  334. v4l2_info(v4l2_dev, "port 0x%x.\n", dev->io);
  335. v4l2_info(v4l2_dev, "mute frequency is %lu kHz.\n", dev->mutefreq);
  336. dev->mutefreq <<= 4;
  337. /* mute card - prevents noisy bootups */
  338. typhoon_mute(dev);
  339. return 0;
  340. }
  341. static void __exit typhoon_exit(void)
  342. {
  343. struct typhoon *dev = &typhoon_card;
  344. video_unregister_device(&dev->vdev);
  345. v4l2_device_unregister(&dev->v4l2_dev);
  346. release_region(dev->io, 8);
  347. }
  348. module_init(typhoon_init);
  349. module_exit(typhoon_exit);