radio-typhoon.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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 <linux/config.h> /* CONFIG_RADIO_TYPHOON_* */
  39. #define BANNER "Typhoon Radio Card driver v0.1\n"
  40. #ifndef CONFIG_RADIO_TYPHOON_PORT
  41. #define CONFIG_RADIO_TYPHOON_PORT -1
  42. #endif
  43. #ifndef CONFIG_RADIO_TYPHOON_MUTEFREQ
  44. #define CONFIG_RADIO_TYPHOON_MUTEFREQ 0
  45. #endif
  46. #ifndef CONFIG_PROC_FS
  47. #undef CONFIG_RADIO_TYPHOON_PROC_FS
  48. #endif
  49. struct typhoon_device {
  50. int users;
  51. int iobase;
  52. int curvol;
  53. int muted;
  54. unsigned long curfreq;
  55. unsigned long mutefreq;
  56. struct mutex lock;
  57. };
  58. static void typhoon_setvol_generic(struct typhoon_device *dev, int vol);
  59. static int typhoon_setfreq_generic(struct typhoon_device *dev,
  60. unsigned long frequency);
  61. static int typhoon_setfreq(struct typhoon_device *dev, unsigned long frequency);
  62. static void typhoon_mute(struct typhoon_device *dev);
  63. static void typhoon_unmute(struct typhoon_device *dev);
  64. static int typhoon_setvol(struct typhoon_device *dev, int vol);
  65. static int typhoon_ioctl(struct inode *inode, struct file *file,
  66. unsigned int cmd, unsigned long arg);
  67. #ifdef CONFIG_RADIO_TYPHOON_PROC_FS
  68. static int typhoon_get_info(char *buf, char **start, off_t offset, int len);
  69. #endif
  70. static void typhoon_setvol_generic(struct typhoon_device *dev, int vol)
  71. {
  72. mutex_lock(&dev->lock);
  73. vol >>= 14; /* Map 16 bit to 2 bit */
  74. vol &= 3;
  75. outb_p(vol / 2, dev->iobase); /* Set the volume, high bit. */
  76. outb_p(vol % 2, dev->iobase + 2); /* Set the volume, low bit. */
  77. mutex_unlock(&dev->lock);
  78. }
  79. static int typhoon_setfreq_generic(struct typhoon_device *dev,
  80. unsigned long frequency)
  81. {
  82. unsigned long outval;
  83. unsigned long x;
  84. /*
  85. * The frequency transfer curve is not linear. The best fit I could
  86. * get is
  87. *
  88. * outval = -155 + exp((f + 15.55) * 0.057))
  89. *
  90. * where frequency f is in MHz. Since we don't have exp in the kernel,
  91. * I approximate this function by a third order polynomial.
  92. *
  93. */
  94. mutex_lock(&dev->lock);
  95. x = frequency / 160;
  96. outval = (x * x + 2500) / 5000;
  97. outval = (outval * x + 5000) / 10000;
  98. outval -= (10 * x * x + 10433) / 20866;
  99. outval += 4 * x - 11505;
  100. outb_p((outval >> 8) & 0x01, dev->iobase + 4);
  101. outb_p(outval >> 9, dev->iobase + 6);
  102. outb_p(outval & 0xff, dev->iobase + 8);
  103. mutex_unlock(&dev->lock);
  104. return 0;
  105. }
  106. static int typhoon_setfreq(struct typhoon_device *dev, unsigned long frequency)
  107. {
  108. typhoon_setfreq_generic(dev, frequency);
  109. dev->curfreq = frequency;
  110. return 0;
  111. }
  112. static void typhoon_mute(struct typhoon_device *dev)
  113. {
  114. if (dev->muted == 1)
  115. return;
  116. typhoon_setvol_generic(dev, 0);
  117. typhoon_setfreq_generic(dev, dev->mutefreq);
  118. dev->muted = 1;
  119. }
  120. static void typhoon_unmute(struct typhoon_device *dev)
  121. {
  122. if (dev->muted == 0)
  123. return;
  124. typhoon_setfreq_generic(dev, dev->curfreq);
  125. typhoon_setvol_generic(dev, dev->curvol);
  126. dev->muted = 0;
  127. }
  128. static int typhoon_setvol(struct typhoon_device *dev, int vol)
  129. {
  130. if (dev->muted && vol != 0) { /* user is unmuting the card */
  131. dev->curvol = vol;
  132. typhoon_unmute(dev);
  133. return 0;
  134. }
  135. if (vol == dev->curvol) /* requested volume == current */
  136. return 0;
  137. if (vol == 0) { /* volume == 0 means mute the card */
  138. typhoon_mute(dev);
  139. dev->curvol = vol;
  140. return 0;
  141. }
  142. typhoon_setvol_generic(dev, vol);
  143. dev->curvol = vol;
  144. return 0;
  145. }
  146. static int typhoon_do_ioctl(struct inode *inode, struct file *file,
  147. unsigned int cmd, void *arg)
  148. {
  149. struct video_device *dev = video_devdata(file);
  150. struct typhoon_device *typhoon = dev->priv;
  151. switch (cmd) {
  152. case VIDIOCGCAP:
  153. {
  154. struct video_capability *v = arg;
  155. memset(v,0,sizeof(*v));
  156. v->type = VID_TYPE_TUNER;
  157. v->channels = 1;
  158. v->audios = 1;
  159. strcpy(v->name, "Typhoon Radio");
  160. return 0;
  161. }
  162. case VIDIOCGTUNER:
  163. {
  164. struct video_tuner *v = arg;
  165. if (v->tuner) /* Only 1 tuner */
  166. return -EINVAL;
  167. v->rangelow = 875 * 1600;
  168. v->rangehigh = 1080 * 1600;
  169. v->flags = VIDEO_TUNER_LOW;
  170. v->mode = VIDEO_MODE_AUTO;
  171. v->signal = 0xFFFF; /* We can't get the signal strength */
  172. strcpy(v->name, "FM");
  173. return 0;
  174. }
  175. case VIDIOCSTUNER:
  176. {
  177. struct video_tuner *v = arg;
  178. if (v->tuner != 0)
  179. return -EINVAL;
  180. /* Only 1 tuner so no setting needed ! */
  181. return 0;
  182. }
  183. case VIDIOCGFREQ:
  184. {
  185. unsigned long *freq = arg;
  186. *freq = typhoon->curfreq;
  187. return 0;
  188. }
  189. case VIDIOCSFREQ:
  190. {
  191. unsigned long *freq = arg;
  192. typhoon->curfreq = *freq;
  193. typhoon_setfreq(typhoon, typhoon->curfreq);
  194. return 0;
  195. }
  196. case VIDIOCGAUDIO:
  197. {
  198. struct video_audio *v = arg;
  199. memset(v, 0, sizeof(*v));
  200. v->flags |= VIDEO_AUDIO_MUTABLE | VIDEO_AUDIO_VOLUME;
  201. v->mode |= VIDEO_SOUND_MONO;
  202. v->volume = typhoon->curvol;
  203. v->step = 1 << 14;
  204. strcpy(v->name, "Typhoon Radio");
  205. return 0;
  206. }
  207. case VIDIOCSAUDIO:
  208. {
  209. struct video_audio *v = arg;
  210. if (v->audio)
  211. return -EINVAL;
  212. if (v->flags & VIDEO_AUDIO_MUTE)
  213. typhoon_mute(typhoon);
  214. else
  215. typhoon_unmute(typhoon);
  216. if (v->flags & VIDEO_AUDIO_VOLUME)
  217. typhoon_setvol(typhoon, v->volume);
  218. return 0;
  219. }
  220. default:
  221. return -ENOIOCTLCMD;
  222. }
  223. }
  224. static int typhoon_ioctl(struct inode *inode, struct file *file,
  225. unsigned int cmd, unsigned long arg)
  226. {
  227. return video_usercopy(inode, file, cmd, arg, typhoon_do_ioctl);
  228. }
  229. static struct typhoon_device typhoon_unit =
  230. {
  231. .iobase = CONFIG_RADIO_TYPHOON_PORT,
  232. .curfreq = CONFIG_RADIO_TYPHOON_MUTEFREQ,
  233. .mutefreq = CONFIG_RADIO_TYPHOON_MUTEFREQ,
  234. };
  235. static struct file_operations typhoon_fops = {
  236. .owner = THIS_MODULE,
  237. .open = video_exclusive_open,
  238. .release = video_exclusive_release,
  239. .ioctl = typhoon_ioctl,
  240. .compat_ioctl = v4l_compat_ioctl32,
  241. .llseek = no_llseek,
  242. };
  243. static struct video_device typhoon_radio =
  244. {
  245. .owner = THIS_MODULE,
  246. .name = "Typhoon Radio",
  247. .type = VID_TYPE_TUNER,
  248. .hardware = VID_HARDWARE_TYPHOON,
  249. .fops = &typhoon_fops,
  250. };
  251. #ifdef CONFIG_RADIO_TYPHOON_PROC_FS
  252. static int typhoon_get_info(char *buf, char **start, off_t offset, int len)
  253. {
  254. char *out = buf;
  255. #ifdef MODULE
  256. #define MODULEPROCSTRING "Driver loaded as a module"
  257. #else
  258. #define MODULEPROCSTRING "Driver compiled into kernel"
  259. #endif
  260. /* output must be kept under PAGE_SIZE */
  261. out += sprintf(out, BANNER);
  262. out += sprintf(out, "Load type: " MODULEPROCSTRING "\n\n");
  263. out += sprintf(out, "frequency = %lu kHz\n",
  264. typhoon_unit.curfreq >> 4);
  265. out += sprintf(out, "volume = %d\n", typhoon_unit.curvol);
  266. out += sprintf(out, "mute = %s\n", typhoon_unit.muted ?
  267. "on" : "off");
  268. out += sprintf(out, "iobase = 0x%x\n", typhoon_unit.iobase);
  269. out += sprintf(out, "mute frequency = %lu kHz\n",
  270. typhoon_unit.mutefreq >> 4);
  271. return out - buf;
  272. }
  273. #endif /* CONFIG_RADIO_TYPHOON_PROC_FS */
  274. MODULE_AUTHOR("Dr. Henrik Seidel");
  275. MODULE_DESCRIPTION("A driver for the Typhoon radio card (a.k.a. EcoRadio).");
  276. MODULE_LICENSE("GPL");
  277. static int io = -1;
  278. static int radio_nr = -1;
  279. module_param(io, int, 0);
  280. MODULE_PARM_DESC(io, "I/O address of the Typhoon card (0x316 or 0x336)");
  281. module_param(radio_nr, int, 0);
  282. #ifdef MODULE
  283. static unsigned long mutefreq = 0;
  284. module_param(mutefreq, ulong, 0);
  285. MODULE_PARM_DESC(mutefreq, "Frequency used when muting the card (in kHz)");
  286. #endif
  287. static int __init typhoon_init(void)
  288. {
  289. #ifdef MODULE
  290. if (io == -1) {
  291. printk(KERN_ERR "radio-typhoon: You must set an I/O address with io=0x316 or io=0x336\n");
  292. return -EINVAL;
  293. }
  294. typhoon_unit.iobase = io;
  295. if (mutefreq < 87000 || mutefreq > 108500) {
  296. printk(KERN_ERR "radio-typhoon: You must set a frequency (in kHz) used when muting the card,\n");
  297. printk(KERN_ERR "radio-typhoon: e.g. with \"mutefreq=87500\" (87000 <= mutefreq <= 108500)\n");
  298. return -EINVAL;
  299. }
  300. typhoon_unit.mutefreq = mutefreq;
  301. #endif /* MODULE */
  302. printk(KERN_INFO BANNER);
  303. mutex_init(&typhoon_unit.lock);
  304. io = typhoon_unit.iobase;
  305. if (!request_region(io, 8, "typhoon")) {
  306. printk(KERN_ERR "radio-typhoon: port 0x%x already in use\n",
  307. typhoon_unit.iobase);
  308. return -EBUSY;
  309. }
  310. typhoon_radio.priv = &typhoon_unit;
  311. if (video_register_device(&typhoon_radio, VFL_TYPE_RADIO, radio_nr) == -1)
  312. {
  313. release_region(io, 8);
  314. return -EINVAL;
  315. }
  316. printk(KERN_INFO "radio-typhoon: port 0x%x.\n", typhoon_unit.iobase);
  317. printk(KERN_INFO "radio-typhoon: mute frequency is %lu kHz.\n",
  318. typhoon_unit.mutefreq);
  319. typhoon_unit.mutefreq <<= 4;
  320. /* mute card - prevents noisy bootups */
  321. typhoon_mute(&typhoon_unit);
  322. #ifdef CONFIG_RADIO_TYPHOON_PROC_FS
  323. if (!create_proc_info_entry("driver/radio-typhoon", 0, NULL,
  324. typhoon_get_info))
  325. printk(KERN_ERR "radio-typhoon: registering /proc/driver/radio-typhoon failed\n");
  326. #endif
  327. return 0;
  328. }
  329. static void __exit typhoon_cleanup_module(void)
  330. {
  331. #ifdef CONFIG_RADIO_TYPHOON_PROC_FS
  332. remove_proc_entry("driver/radio-typhoon", NULL);
  333. #endif
  334. video_unregister_device(&typhoon_radio);
  335. release_region(io, 8);
  336. }
  337. module_init(typhoon_init);
  338. module_exit(typhoon_cleanup_module);