radio-typhoon.c 11 KB

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