radio-typhoon.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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/proc_fs.h> /* radio card status report */
  37. #include <linux/seq_file.h>
  38. #include <asm/io.h> /* outb, outb_p */
  39. #include <asm/uaccess.h> /* copy to/from user */
  40. #include <linux/videodev2.h> /* kernel radio structs */
  41. #include <media/v4l2-common.h>
  42. #include <linux/version.h> /* for KERNEL_VERSION MACRO */
  43. #define RADIO_VERSION KERNEL_VERSION(0,1,1)
  44. #define BANNER "Typhoon Radio Card driver v0.1.1\n"
  45. static struct v4l2_queryctrl radio_qctrl[] = {
  46. {
  47. .id = V4L2_CID_AUDIO_MUTE,
  48. .name = "Mute",
  49. .minimum = 0,
  50. .maximum = 1,
  51. .default_value = 1,
  52. .type = V4L2_CTRL_TYPE_BOOLEAN,
  53. },{
  54. .id = V4L2_CID_AUDIO_VOLUME,
  55. .name = "Volume",
  56. .minimum = 0,
  57. .maximum = 65535,
  58. .step = 1<<14,
  59. .default_value = 0xff,
  60. .type = V4L2_CTRL_TYPE_INTEGER,
  61. }
  62. };
  63. #ifndef CONFIG_RADIO_TYPHOON_PORT
  64. #define CONFIG_RADIO_TYPHOON_PORT -1
  65. #endif
  66. #ifndef CONFIG_RADIO_TYPHOON_MUTEFREQ
  67. #define CONFIG_RADIO_TYPHOON_MUTEFREQ 0
  68. #endif
  69. #ifndef CONFIG_PROC_FS
  70. #undef CONFIG_RADIO_TYPHOON_PROC_FS
  71. #endif
  72. struct typhoon_device {
  73. int users;
  74. int iobase;
  75. int curvol;
  76. int muted;
  77. unsigned long curfreq;
  78. unsigned long mutefreq;
  79. struct mutex lock;
  80. };
  81. static void typhoon_setvol_generic(struct typhoon_device *dev, int vol);
  82. static int typhoon_setfreq_generic(struct typhoon_device *dev,
  83. unsigned long frequency);
  84. static int typhoon_setfreq(struct typhoon_device *dev, unsigned long frequency);
  85. static void typhoon_mute(struct typhoon_device *dev);
  86. static void typhoon_unmute(struct typhoon_device *dev);
  87. static int typhoon_setvol(struct typhoon_device *dev, int vol);
  88. static void typhoon_setvol_generic(struct typhoon_device *dev, int vol)
  89. {
  90. mutex_lock(&dev->lock);
  91. vol >>= 14; /* Map 16 bit to 2 bit */
  92. vol &= 3;
  93. outb_p(vol / 2, dev->iobase); /* Set the volume, high bit. */
  94. outb_p(vol % 2, dev->iobase + 2); /* Set the volume, low bit. */
  95. mutex_unlock(&dev->lock);
  96. }
  97. static int typhoon_setfreq_generic(struct typhoon_device *dev,
  98. unsigned long frequency)
  99. {
  100. unsigned long outval;
  101. unsigned long x;
  102. /*
  103. * The frequency transfer curve is not linear. The best fit I could
  104. * get is
  105. *
  106. * outval = -155 + exp((f + 15.55) * 0.057))
  107. *
  108. * where frequency f is in MHz. Since we don't have exp in the kernel,
  109. * I approximate this function by a third order polynomial.
  110. *
  111. */
  112. mutex_lock(&dev->lock);
  113. x = frequency / 160;
  114. outval = (x * x + 2500) / 5000;
  115. outval = (outval * x + 5000) / 10000;
  116. outval -= (10 * x * x + 10433) / 20866;
  117. outval += 4 * x - 11505;
  118. outb_p((outval >> 8) & 0x01, dev->iobase + 4);
  119. outb_p(outval >> 9, dev->iobase + 6);
  120. outb_p(outval & 0xff, dev->iobase + 8);
  121. mutex_unlock(&dev->lock);
  122. return 0;
  123. }
  124. static int typhoon_setfreq(struct typhoon_device *dev, unsigned long frequency)
  125. {
  126. typhoon_setfreq_generic(dev, frequency);
  127. dev->curfreq = frequency;
  128. return 0;
  129. }
  130. static void typhoon_mute(struct typhoon_device *dev)
  131. {
  132. if (dev->muted == 1)
  133. return;
  134. typhoon_setvol_generic(dev, 0);
  135. typhoon_setfreq_generic(dev, dev->mutefreq);
  136. dev->muted = 1;
  137. }
  138. static void typhoon_unmute(struct typhoon_device *dev)
  139. {
  140. if (dev->muted == 0)
  141. return;
  142. typhoon_setfreq_generic(dev, dev->curfreq);
  143. typhoon_setvol_generic(dev, dev->curvol);
  144. dev->muted = 0;
  145. }
  146. static int typhoon_setvol(struct typhoon_device *dev, int vol)
  147. {
  148. if (dev->muted && vol != 0) { /* user is unmuting the card */
  149. dev->curvol = vol;
  150. typhoon_unmute(dev);
  151. return 0;
  152. }
  153. if (vol == dev->curvol) /* requested volume == current */
  154. return 0;
  155. if (vol == 0) { /* volume == 0 means mute the card */
  156. typhoon_mute(dev);
  157. dev->curvol = vol;
  158. return 0;
  159. }
  160. typhoon_setvol_generic(dev, vol);
  161. dev->curvol = vol;
  162. return 0;
  163. }
  164. static int vidioc_querycap(struct file *file, void *priv,
  165. struct v4l2_capability *v)
  166. {
  167. strlcpy(v->driver, "radio-typhoon", sizeof(v->driver));
  168. strlcpy(v->card, "Typhoon Radio", sizeof(v->card));
  169. sprintf(v->bus_info, "ISA");
  170. v->version = RADIO_VERSION;
  171. v->capabilities = V4L2_CAP_TUNER;
  172. return 0;
  173. }
  174. static int vidioc_g_tuner(struct file *file, void *priv,
  175. struct v4l2_tuner *v)
  176. {
  177. if (v->index > 0)
  178. return -EINVAL;
  179. strcpy(v->name, "FM");
  180. v->type = V4L2_TUNER_RADIO;
  181. v->rangelow = (87.5*16000);
  182. v->rangehigh = (108*16000);
  183. v->rxsubchans = V4L2_TUNER_SUB_MONO;
  184. v->capability = V4L2_TUNER_CAP_LOW;
  185. v->audmode = V4L2_TUNER_MODE_MONO;
  186. v->signal = 0xFFFF; /* We can't get the signal strength */
  187. return 0;
  188. }
  189. static int vidioc_s_tuner(struct file *file, void *priv,
  190. struct v4l2_tuner *v)
  191. {
  192. if (v->index > 0)
  193. return -EINVAL;
  194. return 0;
  195. }
  196. static int vidioc_s_frequency(struct file *file, void *priv,
  197. struct v4l2_frequency *f)
  198. {
  199. struct video_device *dev = video_devdata(file);
  200. struct typhoon_device *typhoon = dev->priv;
  201. typhoon->curfreq = f->frequency;
  202. typhoon_setfreq(typhoon, typhoon->curfreq);
  203. return 0;
  204. }
  205. static int vidioc_g_frequency(struct file *file, void *priv,
  206. struct v4l2_frequency *f)
  207. {
  208. struct video_device *dev = video_devdata(file);
  209. struct typhoon_device *typhoon = dev->priv;
  210. f->type = V4L2_TUNER_RADIO;
  211. f->frequency = typhoon->curfreq;
  212. return 0;
  213. }
  214. static int vidioc_queryctrl(struct file *file, void *priv,
  215. struct v4l2_queryctrl *qc)
  216. {
  217. int i;
  218. for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
  219. if (qc->id && qc->id == radio_qctrl[i].id) {
  220. memcpy(qc, &(radio_qctrl[i]),
  221. sizeof(*qc));
  222. return 0;
  223. }
  224. }
  225. return -EINVAL;
  226. }
  227. static int vidioc_g_ctrl(struct file *file, void *priv,
  228. struct v4l2_control *ctrl)
  229. {
  230. struct video_device *dev = video_devdata(file);
  231. struct typhoon_device *typhoon = dev->priv;
  232. switch (ctrl->id) {
  233. case V4L2_CID_AUDIO_MUTE:
  234. ctrl->value = typhoon->muted;
  235. return 0;
  236. case V4L2_CID_AUDIO_VOLUME:
  237. ctrl->value = typhoon->curvol;
  238. return 0;
  239. }
  240. return -EINVAL;
  241. }
  242. static int vidioc_s_ctrl (struct file *file, void *priv,
  243. struct v4l2_control *ctrl)
  244. {
  245. struct video_device *dev = video_devdata(file);
  246. struct typhoon_device *typhoon = dev->priv;
  247. switch (ctrl->id) {
  248. case V4L2_CID_AUDIO_MUTE:
  249. if (ctrl->value)
  250. typhoon_mute(typhoon);
  251. else
  252. typhoon_unmute(typhoon);
  253. return 0;
  254. case V4L2_CID_AUDIO_VOLUME:
  255. typhoon_setvol(typhoon, ctrl->value);
  256. return 0;
  257. }
  258. return -EINVAL;
  259. }
  260. static int vidioc_g_audio(struct file *file, void *priv,
  261. struct v4l2_audio *a)
  262. {
  263. if (a->index > 1)
  264. return -EINVAL;
  265. strcpy(a->name, "Radio");
  266. a->capability = V4L2_AUDCAP_STEREO;
  267. return 0;
  268. }
  269. static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
  270. {
  271. *i = 0;
  272. return 0;
  273. }
  274. static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
  275. {
  276. if (i != 0)
  277. return -EINVAL;
  278. return 0;
  279. }
  280. static int vidioc_s_audio(struct file *file, void *priv,
  281. struct v4l2_audio *a)
  282. {
  283. if (a->index != 0)
  284. return -EINVAL;
  285. return 0;
  286. }
  287. static struct typhoon_device typhoon_unit =
  288. {
  289. .iobase = CONFIG_RADIO_TYPHOON_PORT,
  290. .curfreq = CONFIG_RADIO_TYPHOON_MUTEFREQ,
  291. .mutefreq = CONFIG_RADIO_TYPHOON_MUTEFREQ,
  292. };
  293. static const struct file_operations typhoon_fops = {
  294. .owner = THIS_MODULE,
  295. .open = video_exclusive_open,
  296. .release = video_exclusive_release,
  297. .ioctl = video_ioctl2,
  298. #ifdef CONFIG_COMPAT
  299. .compat_ioctl = v4l_compat_ioctl32,
  300. #endif
  301. .llseek = no_llseek,
  302. };
  303. static struct video_device typhoon_radio =
  304. {
  305. .owner = THIS_MODULE,
  306. .name = "Typhoon Radio",
  307. .type = VID_TYPE_TUNER,
  308. .fops = &typhoon_fops,
  309. .vidioc_querycap = vidioc_querycap,
  310. .vidioc_g_tuner = vidioc_g_tuner,
  311. .vidioc_s_tuner = vidioc_s_tuner,
  312. .vidioc_g_audio = vidioc_g_audio,
  313. .vidioc_s_audio = vidioc_s_audio,
  314. .vidioc_g_input = vidioc_g_input,
  315. .vidioc_s_input = vidioc_s_input,
  316. .vidioc_g_frequency = vidioc_g_frequency,
  317. .vidioc_s_frequency = vidioc_s_frequency,
  318. .vidioc_queryctrl = vidioc_queryctrl,
  319. .vidioc_g_ctrl = vidioc_g_ctrl,
  320. .vidioc_s_ctrl = vidioc_s_ctrl,
  321. };
  322. #ifdef CONFIG_RADIO_TYPHOON_PROC_FS
  323. static int typhoon_proc_show(struct seq_file *m, void *v)
  324. {
  325. #ifdef MODULE
  326. #define MODULEPROCSTRING "Driver loaded as a module"
  327. #else
  328. #define MODULEPROCSTRING "Driver compiled into kernel"
  329. #endif
  330. seq_puts(m, BANNER);
  331. seq_puts(m, "Load type: " MODULEPROCSTRING "\n\n");
  332. seq_printf(m, "frequency = %lu kHz\n",
  333. typhoon_unit.curfreq >> 4);
  334. seq_printf(m, "volume = %d\n", typhoon_unit.curvol);
  335. seq_printf(m, "mute = %s\n", typhoon_unit.muted ?
  336. "on" : "off");
  337. seq_printf(m, "iobase = 0x%x\n", typhoon_unit.iobase);
  338. seq_printf(m, "mute frequency = %lu kHz\n",
  339. typhoon_unit.mutefreq >> 4);
  340. return 0;
  341. }
  342. static int typhoon_proc_open(struct inode *inode, struct file *file)
  343. {
  344. return single_open(file, typhoon_proc_show, NULL);
  345. }
  346. static const struct file_operations typhoon_proc_fops = {
  347. .owner = THIS_MODULE,
  348. .open = typhoon_proc_open,
  349. .read = seq_read,
  350. .llseek = seq_lseek,
  351. .release = single_release,
  352. };
  353. #endif /* CONFIG_RADIO_TYPHOON_PROC_FS */
  354. MODULE_AUTHOR("Dr. Henrik Seidel");
  355. MODULE_DESCRIPTION("A driver for the Typhoon radio card (a.k.a. EcoRadio).");
  356. MODULE_LICENSE("GPL");
  357. static int io = -1;
  358. static int radio_nr = -1;
  359. module_param(io, int, 0);
  360. MODULE_PARM_DESC(io, "I/O address of the Typhoon card (0x316 or 0x336)");
  361. module_param(radio_nr, int, 0);
  362. #ifdef MODULE
  363. static unsigned long mutefreq;
  364. module_param(mutefreq, ulong, 0);
  365. MODULE_PARM_DESC(mutefreq, "Frequency used when muting the card (in kHz)");
  366. #endif
  367. static int __init typhoon_init(void)
  368. {
  369. #ifdef MODULE
  370. if (io == -1) {
  371. printk(KERN_ERR "radio-typhoon: You must set an I/O address with io=0x316 or io=0x336\n");
  372. return -EINVAL;
  373. }
  374. typhoon_unit.iobase = io;
  375. if (mutefreq < 87000 || mutefreq > 108500) {
  376. printk(KERN_ERR "radio-typhoon: You must set a frequency (in kHz) used when muting the card,\n");
  377. printk(KERN_ERR "radio-typhoon: e.g. with \"mutefreq=87500\" (87000 <= mutefreq <= 108500)\n");
  378. return -EINVAL;
  379. }
  380. typhoon_unit.mutefreq = mutefreq;
  381. #endif /* MODULE */
  382. printk(KERN_INFO BANNER);
  383. mutex_init(&typhoon_unit.lock);
  384. io = typhoon_unit.iobase;
  385. if (!request_region(io, 8, "typhoon")) {
  386. printk(KERN_ERR "radio-typhoon: port 0x%x already in use\n",
  387. typhoon_unit.iobase);
  388. return -EBUSY;
  389. }
  390. typhoon_radio.priv = &typhoon_unit;
  391. if (video_register_device(&typhoon_radio, VFL_TYPE_RADIO, radio_nr) == -1)
  392. {
  393. release_region(io, 8);
  394. return -EINVAL;
  395. }
  396. printk(KERN_INFO "radio-typhoon: port 0x%x.\n", typhoon_unit.iobase);
  397. printk(KERN_INFO "radio-typhoon: mute frequency is %lu kHz.\n",
  398. typhoon_unit.mutefreq);
  399. typhoon_unit.mutefreq <<= 4;
  400. /* mute card - prevents noisy bootups */
  401. typhoon_mute(&typhoon_unit);
  402. #ifdef CONFIG_RADIO_TYPHOON_PROC_FS
  403. if (!proc_create("driver/radio-typhoon", 0, NULL, &typhoon_proc_fops))
  404. printk(KERN_ERR "radio-typhoon: registering /proc/driver/radio-typhoon failed\n");
  405. #endif
  406. return 0;
  407. }
  408. static void __exit typhoon_cleanup_module(void)
  409. {
  410. #ifdef CONFIG_RADIO_TYPHOON_PROC_FS
  411. remove_proc_entry("driver/radio-typhoon", NULL);
  412. #endif
  413. video_unregister_device(&typhoon_radio);
  414. release_region(io, 8);
  415. }
  416. module_init(typhoon_init);
  417. module_exit(typhoon_cleanup_module);