radio-typhoon.c 12 KB

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