radio-typhoon.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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. static int typhoon_ioctl(struct inode *inode, struct file *file,
  88. unsigned int cmd, unsigned long arg);
  89. #ifdef CONFIG_RADIO_TYPHOON_PROC_FS
  90. static int typhoon_get_info(char *buf, char **start, off_t offset, int len);
  91. #endif
  92. static void typhoon_setvol_generic(struct typhoon_device *dev, int vol)
  93. {
  94. mutex_lock(&dev->lock);
  95. vol >>= 14; /* Map 16 bit to 2 bit */
  96. vol &= 3;
  97. outb_p(vol / 2, dev->iobase); /* Set the volume, high bit. */
  98. outb_p(vol % 2, dev->iobase + 2); /* Set the volume, low bit. */
  99. mutex_unlock(&dev->lock);
  100. }
  101. static int typhoon_setfreq_generic(struct typhoon_device *dev,
  102. unsigned long frequency)
  103. {
  104. unsigned long outval;
  105. unsigned long x;
  106. /*
  107. * The frequency transfer curve is not linear. The best fit I could
  108. * get is
  109. *
  110. * outval = -155 + exp((f + 15.55) * 0.057))
  111. *
  112. * where frequency f is in MHz. Since we don't have exp in the kernel,
  113. * I approximate this function by a third order polynomial.
  114. *
  115. */
  116. mutex_lock(&dev->lock);
  117. x = frequency / 160;
  118. outval = (x * x + 2500) / 5000;
  119. outval = (outval * x + 5000) / 10000;
  120. outval -= (10 * x * x + 10433) / 20866;
  121. outval += 4 * x - 11505;
  122. outb_p((outval >> 8) & 0x01, dev->iobase + 4);
  123. outb_p(outval >> 9, dev->iobase + 6);
  124. outb_p(outval & 0xff, dev->iobase + 8);
  125. mutex_unlock(&dev->lock);
  126. return 0;
  127. }
  128. static int typhoon_setfreq(struct typhoon_device *dev, unsigned long frequency)
  129. {
  130. typhoon_setfreq_generic(dev, frequency);
  131. dev->curfreq = frequency;
  132. return 0;
  133. }
  134. static void typhoon_mute(struct typhoon_device *dev)
  135. {
  136. if (dev->muted == 1)
  137. return;
  138. typhoon_setvol_generic(dev, 0);
  139. typhoon_setfreq_generic(dev, dev->mutefreq);
  140. dev->muted = 1;
  141. }
  142. static void typhoon_unmute(struct typhoon_device *dev)
  143. {
  144. if (dev->muted == 0)
  145. return;
  146. typhoon_setfreq_generic(dev, dev->curfreq);
  147. typhoon_setvol_generic(dev, dev->curvol);
  148. dev->muted = 0;
  149. }
  150. static int typhoon_setvol(struct typhoon_device *dev, int vol)
  151. {
  152. if (dev->muted && vol != 0) { /* user is unmuting the card */
  153. dev->curvol = vol;
  154. typhoon_unmute(dev);
  155. return 0;
  156. }
  157. if (vol == dev->curvol) /* requested volume == current */
  158. return 0;
  159. if (vol == 0) { /* volume == 0 means mute the card */
  160. typhoon_mute(dev);
  161. dev->curvol = vol;
  162. return 0;
  163. }
  164. typhoon_setvol_generic(dev, vol);
  165. dev->curvol = vol;
  166. return 0;
  167. }
  168. static int typhoon_do_ioctl(struct inode *inode, struct file *file,
  169. unsigned int cmd, void *arg)
  170. {
  171. struct video_device *dev = video_devdata(file);
  172. struct typhoon_device *typhoon = dev->priv;
  173. switch (cmd) {
  174. case VIDIOC_QUERYCAP:
  175. {
  176. struct v4l2_capability *v = arg;
  177. memset(v,0,sizeof(*v));
  178. strlcpy(v->driver, "radio-typhoon", sizeof (v->driver));
  179. strlcpy(v->card, "Typhoon Radio", sizeof (v->card));
  180. sprintf(v->bus_info,"ISA");
  181. v->version = RADIO_VERSION;
  182. v->capabilities = V4L2_CAP_TUNER;
  183. return 0;
  184. }
  185. case VIDIOC_G_TUNER:
  186. {
  187. struct v4l2_tuner *v = arg;
  188. if (v->index > 0)
  189. return -EINVAL;
  190. memset(v,0,sizeof(*v));
  191. strcpy(v->name, "FM");
  192. v->type = V4L2_TUNER_RADIO;
  193. v->rangelow=(87.5*16000);
  194. v->rangehigh=(108*16000);
  195. v->rxsubchans =V4L2_TUNER_SUB_MONO;
  196. v->capability=V4L2_TUNER_CAP_LOW;
  197. v->audmode = V4L2_TUNER_MODE_MONO;
  198. v->signal = 0xFFFF; /* We can't get the signal strength */
  199. return 0;
  200. }
  201. case VIDIOC_S_TUNER:
  202. {
  203. struct v4l2_tuner *v = arg;
  204. if (v->index > 0)
  205. return -EINVAL;
  206. return 0;
  207. }
  208. case VIDIOC_S_FREQUENCY:
  209. {
  210. struct v4l2_frequency *f = arg;
  211. typhoon->curfreq = f->frequency;
  212. typhoon_setfreq(typhoon, typhoon->curfreq);
  213. return 0;
  214. }
  215. case VIDIOC_G_FREQUENCY:
  216. {
  217. struct v4l2_frequency *f = arg;
  218. f->type = V4L2_TUNER_RADIO;
  219. f->frequency = typhoon->curfreq;
  220. return 0;
  221. }
  222. case VIDIOC_QUERYCTRL:
  223. {
  224. struct v4l2_queryctrl *qc = arg;
  225. int i;
  226. for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
  227. if (qc->id && qc->id == radio_qctrl[i].id) {
  228. memcpy(qc, &(radio_qctrl[i]),
  229. sizeof(*qc));
  230. return (0);
  231. }
  232. }
  233. return -EINVAL;
  234. }
  235. case VIDIOC_G_CTRL:
  236. {
  237. struct v4l2_control *ctrl= arg;
  238. switch (ctrl->id) {
  239. case V4L2_CID_AUDIO_MUTE:
  240. ctrl->value=typhoon->muted;
  241. return (0);
  242. case V4L2_CID_AUDIO_VOLUME:
  243. ctrl->value=typhoon->curvol;
  244. return (0);
  245. }
  246. return -EINVAL;
  247. }
  248. case VIDIOC_S_CTRL:
  249. {
  250. struct v4l2_control *ctrl= arg;
  251. switch (ctrl->id) {
  252. case V4L2_CID_AUDIO_MUTE:
  253. if (ctrl->value) {
  254. typhoon_mute(typhoon);
  255. } else {
  256. typhoon_unmute(typhoon);
  257. }
  258. return (0);
  259. case V4L2_CID_AUDIO_VOLUME:
  260. typhoon_setvol(typhoon, ctrl->value);
  261. return (0);
  262. }
  263. return -EINVAL;
  264. }
  265. default:
  266. return v4l_compat_translate_ioctl(inode,file,cmd,arg,
  267. typhoon_do_ioctl);
  268. }
  269. }
  270. static int typhoon_ioctl(struct inode *inode, struct file *file,
  271. unsigned int cmd, unsigned long arg)
  272. {
  273. return video_usercopy(inode, file, cmd, arg, typhoon_do_ioctl);
  274. }
  275. static struct typhoon_device typhoon_unit =
  276. {
  277. .iobase = CONFIG_RADIO_TYPHOON_PORT,
  278. .curfreq = CONFIG_RADIO_TYPHOON_MUTEFREQ,
  279. .mutefreq = CONFIG_RADIO_TYPHOON_MUTEFREQ,
  280. };
  281. static struct file_operations typhoon_fops = {
  282. .owner = THIS_MODULE,
  283. .open = video_exclusive_open,
  284. .release = video_exclusive_release,
  285. .ioctl = typhoon_ioctl,
  286. .compat_ioctl = v4l_compat_ioctl32,
  287. .llseek = no_llseek,
  288. };
  289. static struct video_device typhoon_radio =
  290. {
  291. .owner = THIS_MODULE,
  292. .name = "Typhoon Radio",
  293. .type = VID_TYPE_TUNER,
  294. .hardware = 0,
  295. .fops = &typhoon_fops,
  296. };
  297. #ifdef CONFIG_RADIO_TYPHOON_PROC_FS
  298. static int typhoon_get_info(char *buf, char **start, off_t offset, int len)
  299. {
  300. char *out = buf;
  301. #ifdef MODULE
  302. #define MODULEPROCSTRING "Driver loaded as a module"
  303. #else
  304. #define MODULEPROCSTRING "Driver compiled into kernel"
  305. #endif
  306. /* output must be kept under PAGE_SIZE */
  307. out += sprintf(out, BANNER);
  308. out += sprintf(out, "Load type: " MODULEPROCSTRING "\n\n");
  309. out += sprintf(out, "frequency = %lu kHz\n",
  310. typhoon_unit.curfreq >> 4);
  311. out += sprintf(out, "volume = %d\n", typhoon_unit.curvol);
  312. out += sprintf(out, "mute = %s\n", typhoon_unit.muted ?
  313. "on" : "off");
  314. out += sprintf(out, "iobase = 0x%x\n", typhoon_unit.iobase);
  315. out += sprintf(out, "mute frequency = %lu kHz\n",
  316. typhoon_unit.mutefreq >> 4);
  317. return out - buf;
  318. }
  319. #endif /* CONFIG_RADIO_TYPHOON_PROC_FS */
  320. MODULE_AUTHOR("Dr. Henrik Seidel");
  321. MODULE_DESCRIPTION("A driver for the Typhoon radio card (a.k.a. EcoRadio).");
  322. MODULE_LICENSE("GPL");
  323. static int io = -1;
  324. static int radio_nr = -1;
  325. module_param(io, int, 0);
  326. MODULE_PARM_DESC(io, "I/O address of the Typhoon card (0x316 or 0x336)");
  327. module_param(radio_nr, int, 0);
  328. #ifdef MODULE
  329. static unsigned long mutefreq = 0;
  330. module_param(mutefreq, ulong, 0);
  331. MODULE_PARM_DESC(mutefreq, "Frequency used when muting the card (in kHz)");
  332. #endif
  333. static int __init typhoon_init(void)
  334. {
  335. #ifdef MODULE
  336. if (io == -1) {
  337. printk(KERN_ERR "radio-typhoon: You must set an I/O address with io=0x316 or io=0x336\n");
  338. return -EINVAL;
  339. }
  340. typhoon_unit.iobase = io;
  341. if (mutefreq < 87000 || mutefreq > 108500) {
  342. printk(KERN_ERR "radio-typhoon: You must set a frequency (in kHz) used when muting the card,\n");
  343. printk(KERN_ERR "radio-typhoon: e.g. with \"mutefreq=87500\" (87000 <= mutefreq <= 108500)\n");
  344. return -EINVAL;
  345. }
  346. typhoon_unit.mutefreq = mutefreq;
  347. #endif /* MODULE */
  348. printk(KERN_INFO BANNER);
  349. mutex_init(&typhoon_unit.lock);
  350. io = typhoon_unit.iobase;
  351. if (!request_region(io, 8, "typhoon")) {
  352. printk(KERN_ERR "radio-typhoon: port 0x%x already in use\n",
  353. typhoon_unit.iobase);
  354. return -EBUSY;
  355. }
  356. typhoon_radio.priv = &typhoon_unit;
  357. if (video_register_device(&typhoon_radio, VFL_TYPE_RADIO, radio_nr) == -1)
  358. {
  359. release_region(io, 8);
  360. return -EINVAL;
  361. }
  362. printk(KERN_INFO "radio-typhoon: port 0x%x.\n", typhoon_unit.iobase);
  363. printk(KERN_INFO "radio-typhoon: mute frequency is %lu kHz.\n",
  364. typhoon_unit.mutefreq);
  365. typhoon_unit.mutefreq <<= 4;
  366. /* mute card - prevents noisy bootups */
  367. typhoon_mute(&typhoon_unit);
  368. #ifdef CONFIG_RADIO_TYPHOON_PROC_FS
  369. if (!create_proc_info_entry("driver/radio-typhoon", 0, NULL,
  370. typhoon_get_info))
  371. printk(KERN_ERR "radio-typhoon: registering /proc/driver/radio-typhoon failed\n");
  372. #endif
  373. return 0;
  374. }
  375. static void __exit typhoon_cleanup_module(void)
  376. {
  377. #ifdef CONFIG_RADIO_TYPHOON_PROC_FS
  378. remove_proc_entry("driver/radio-typhoon", NULL);
  379. #endif
  380. video_unregister_device(&typhoon_radio);
  381. release_region(io, 8);
  382. }
  383. module_init(typhoon_init);
  384. module_exit(typhoon_cleanup_module);