radio-typhoon.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. #include <linux/module.h> /* Modules */
  32. #include <linux/init.h> /* Initdata */
  33. #include <linux/ioport.h> /* request_region */
  34. #include <linux/proc_fs.h> /* radio card status report */
  35. #include <asm/io.h> /* outb, outb_p */
  36. #include <asm/uaccess.h> /* copy to/from user */
  37. #include <linux/videodev.h> /* kernel radio structs */
  38. #include <media/v4l2-common.h>
  39. #include <linux/config.h> /* CONFIG_RADIO_TYPHOON_* */
  40. #define BANNER "Typhoon Radio Card driver v0.1\n"
  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. #ifndef CONFIG_PROC_FS
  48. #undef CONFIG_RADIO_TYPHOON_PROC_FS
  49. #endif
  50. struct typhoon_device {
  51. int users;
  52. int iobase;
  53. int curvol;
  54. int muted;
  55. unsigned long curfreq;
  56. unsigned long mutefreq;
  57. struct mutex lock;
  58. };
  59. static void typhoon_setvol_generic(struct typhoon_device *dev, int vol);
  60. static int typhoon_setfreq_generic(struct typhoon_device *dev,
  61. unsigned long frequency);
  62. static int typhoon_setfreq(struct typhoon_device *dev, unsigned long frequency);
  63. static void typhoon_mute(struct typhoon_device *dev);
  64. static void typhoon_unmute(struct typhoon_device *dev);
  65. static int typhoon_setvol(struct typhoon_device *dev, int vol);
  66. static int typhoon_ioctl(struct inode *inode, struct file *file,
  67. unsigned int cmd, unsigned long arg);
  68. #ifdef CONFIG_RADIO_TYPHOON_PROC_FS
  69. static int typhoon_get_info(char *buf, char **start, off_t offset, int len);
  70. #endif
  71. static void typhoon_setvol_generic(struct typhoon_device *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->iobase); /* Set the volume, high bit. */
  77. outb_p(vol % 2, dev->iobase + 2); /* Set the volume, low bit. */
  78. mutex_unlock(&dev->lock);
  79. }
  80. static int typhoon_setfreq_generic(struct typhoon_device *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->iobase + 4);
  102. outb_p(outval >> 9, dev->iobase + 6);
  103. outb_p(outval & 0xff, dev->iobase + 8);
  104. mutex_unlock(&dev->lock);
  105. return 0;
  106. }
  107. static int typhoon_setfreq(struct typhoon_device *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_device *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_device *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_device *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 typhoon_do_ioctl(struct inode *inode, struct file *file,
  148. unsigned int cmd, void *arg)
  149. {
  150. struct video_device *dev = video_devdata(file);
  151. struct typhoon_device *typhoon = dev->priv;
  152. switch (cmd) {
  153. case VIDIOCGCAP:
  154. {
  155. struct video_capability *v = arg;
  156. memset(v,0,sizeof(*v));
  157. v->type = VID_TYPE_TUNER;
  158. v->channels = 1;
  159. v->audios = 1;
  160. strcpy(v->name, "Typhoon Radio");
  161. return 0;
  162. }
  163. case VIDIOCGTUNER:
  164. {
  165. struct video_tuner *v = arg;
  166. if (v->tuner) /* Only 1 tuner */
  167. return -EINVAL;
  168. v->rangelow = 875 * 1600;
  169. v->rangehigh = 1080 * 1600;
  170. v->flags = VIDEO_TUNER_LOW;
  171. v->mode = VIDEO_MODE_AUTO;
  172. v->signal = 0xFFFF; /* We can't get the signal strength */
  173. strcpy(v->name, "FM");
  174. return 0;
  175. }
  176. case VIDIOCSTUNER:
  177. {
  178. struct video_tuner *v = arg;
  179. if (v->tuner != 0)
  180. return -EINVAL;
  181. /* Only 1 tuner so no setting needed ! */
  182. return 0;
  183. }
  184. case VIDIOCGFREQ:
  185. {
  186. unsigned long *freq = arg;
  187. *freq = typhoon->curfreq;
  188. return 0;
  189. }
  190. case VIDIOCSFREQ:
  191. {
  192. unsigned long *freq = arg;
  193. typhoon->curfreq = *freq;
  194. typhoon_setfreq(typhoon, typhoon->curfreq);
  195. return 0;
  196. }
  197. case VIDIOCGAUDIO:
  198. {
  199. struct video_audio *v = arg;
  200. memset(v, 0, sizeof(*v));
  201. v->flags |= VIDEO_AUDIO_MUTABLE | VIDEO_AUDIO_VOLUME;
  202. v->mode |= VIDEO_SOUND_MONO;
  203. v->volume = typhoon->curvol;
  204. v->step = 1 << 14;
  205. strcpy(v->name, "Typhoon Radio");
  206. return 0;
  207. }
  208. case VIDIOCSAUDIO:
  209. {
  210. struct video_audio *v = arg;
  211. if (v->audio)
  212. return -EINVAL;
  213. if (v->flags & VIDEO_AUDIO_MUTE)
  214. typhoon_mute(typhoon);
  215. else
  216. typhoon_unmute(typhoon);
  217. if (v->flags & VIDEO_AUDIO_VOLUME)
  218. typhoon_setvol(typhoon, v->volume);
  219. return 0;
  220. }
  221. default:
  222. return -ENOIOCTLCMD;
  223. }
  224. }
  225. static int typhoon_ioctl(struct inode *inode, struct file *file,
  226. unsigned int cmd, unsigned long arg)
  227. {
  228. return video_usercopy(inode, file, cmd, arg, typhoon_do_ioctl);
  229. }
  230. static struct typhoon_device typhoon_unit =
  231. {
  232. .iobase = CONFIG_RADIO_TYPHOON_PORT,
  233. .curfreq = CONFIG_RADIO_TYPHOON_MUTEFREQ,
  234. .mutefreq = CONFIG_RADIO_TYPHOON_MUTEFREQ,
  235. };
  236. static struct file_operations typhoon_fops = {
  237. .owner = THIS_MODULE,
  238. .open = video_exclusive_open,
  239. .release = video_exclusive_release,
  240. .ioctl = typhoon_ioctl,
  241. .compat_ioctl = v4l_compat_ioctl32,
  242. .llseek = no_llseek,
  243. };
  244. static struct video_device typhoon_radio =
  245. {
  246. .owner = THIS_MODULE,
  247. .name = "Typhoon Radio",
  248. .type = VID_TYPE_TUNER,
  249. .hardware = VID_HARDWARE_TYPHOON,
  250. .fops = &typhoon_fops,
  251. };
  252. #ifdef CONFIG_RADIO_TYPHOON_PROC_FS
  253. static int typhoon_get_info(char *buf, char **start, off_t offset, int len)
  254. {
  255. char *out = buf;
  256. #ifdef MODULE
  257. #define MODULEPROCSTRING "Driver loaded as a module"
  258. #else
  259. #define MODULEPROCSTRING "Driver compiled into kernel"
  260. #endif
  261. /* output must be kept under PAGE_SIZE */
  262. out += sprintf(out, BANNER);
  263. out += sprintf(out, "Load type: " MODULEPROCSTRING "\n\n");
  264. out += sprintf(out, "frequency = %lu kHz\n",
  265. typhoon_unit.curfreq >> 4);
  266. out += sprintf(out, "volume = %d\n", typhoon_unit.curvol);
  267. out += sprintf(out, "mute = %s\n", typhoon_unit.muted ?
  268. "on" : "off");
  269. out += sprintf(out, "iobase = 0x%x\n", typhoon_unit.iobase);
  270. out += sprintf(out, "mute frequency = %lu kHz\n",
  271. typhoon_unit.mutefreq >> 4);
  272. return out - buf;
  273. }
  274. #endif /* CONFIG_RADIO_TYPHOON_PROC_FS */
  275. MODULE_AUTHOR("Dr. Henrik Seidel");
  276. MODULE_DESCRIPTION("A driver for the Typhoon radio card (a.k.a. EcoRadio).");
  277. MODULE_LICENSE("GPL");
  278. static int io = -1;
  279. static int radio_nr = -1;
  280. module_param(io, int, 0);
  281. MODULE_PARM_DESC(io, "I/O address of the Typhoon card (0x316 or 0x336)");
  282. module_param(radio_nr, int, 0);
  283. #ifdef MODULE
  284. static unsigned long mutefreq = 0;
  285. module_param(mutefreq, ulong, 0);
  286. MODULE_PARM_DESC(mutefreq, "Frequency used when muting the card (in kHz)");
  287. #endif
  288. static int __init typhoon_init(void)
  289. {
  290. #ifdef MODULE
  291. if (io == -1) {
  292. printk(KERN_ERR "radio-typhoon: You must set an I/O address with io=0x316 or io=0x336\n");
  293. return -EINVAL;
  294. }
  295. typhoon_unit.iobase = io;
  296. if (mutefreq < 87000 || mutefreq > 108500) {
  297. printk(KERN_ERR "radio-typhoon: You must set a frequency (in kHz) used when muting the card,\n");
  298. printk(KERN_ERR "radio-typhoon: e.g. with \"mutefreq=87500\" (87000 <= mutefreq <= 108500)\n");
  299. return -EINVAL;
  300. }
  301. typhoon_unit.mutefreq = mutefreq;
  302. #endif /* MODULE */
  303. printk(KERN_INFO BANNER);
  304. mutex_init(&typhoon_unit.lock);
  305. io = typhoon_unit.iobase;
  306. if (!request_region(io, 8, "typhoon")) {
  307. printk(KERN_ERR "radio-typhoon: port 0x%x already in use\n",
  308. typhoon_unit.iobase);
  309. return -EBUSY;
  310. }
  311. typhoon_radio.priv = &typhoon_unit;
  312. if (video_register_device(&typhoon_radio, VFL_TYPE_RADIO, radio_nr) == -1)
  313. {
  314. release_region(io, 8);
  315. return -EINVAL;
  316. }
  317. printk(KERN_INFO "radio-typhoon: port 0x%x.\n", typhoon_unit.iobase);
  318. printk(KERN_INFO "radio-typhoon: mute frequency is %lu kHz.\n",
  319. typhoon_unit.mutefreq);
  320. typhoon_unit.mutefreq <<= 4;
  321. /* mute card - prevents noisy bootups */
  322. typhoon_mute(&typhoon_unit);
  323. #ifdef CONFIG_RADIO_TYPHOON_PROC_FS
  324. if (!create_proc_info_entry("driver/radio-typhoon", 0, NULL,
  325. typhoon_get_info))
  326. printk(KERN_ERR "radio-typhoon: registering /proc/driver/radio-typhoon failed\n");
  327. #endif
  328. return 0;
  329. }
  330. static void __exit typhoon_cleanup_module(void)
  331. {
  332. #ifdef CONFIG_RADIO_TYPHOON_PROC_FS
  333. remove_proc_entry("driver/radio-typhoon", NULL);
  334. #endif
  335. video_unregister_device(&typhoon_radio);
  336. release_region(io, 8);
  337. }
  338. module_init(typhoon_init);
  339. module_exit(typhoon_cleanup_module);