radio-sf16fmr2.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /* SF16FMR2 radio driver for Linux radio support
  2. * heavily based on fmi driver...
  3. * (c) 2000-2002 Ziglio Frediano, freddy77@angelfire.com
  4. *
  5. * Notes on the hardware
  6. *
  7. * Frequency control is done digitally -- ie out(port,encodefreq(95.8));
  8. * No volume control - only mute/unmute - you have to use line volume
  9. *
  10. * For read stereo/mono you must wait 0.1 sec after set frequency and
  11. * card unmuted so I set frequency on unmute
  12. * Signal handling seem to work only on autoscanning (not implemented)
  13. */
  14. #include <linux/module.h> /* Modules */
  15. #include <linux/init.h> /* Initdata */
  16. #include <linux/ioport.h> /* check_region, request_region */
  17. #include <linux/delay.h> /* udelay */
  18. #include <asm/io.h> /* outb, outb_p */
  19. #include <asm/uaccess.h> /* copy to/from user */
  20. #include <linux/videodev.h> /* kernel radio structs */
  21. #include <asm/semaphore.h>
  22. static struct semaphore lock;
  23. #undef DEBUG
  24. //#define DEBUG 1
  25. #ifdef DEBUG
  26. # define debug_print(s) printk s
  27. #else
  28. # define debug_print(s)
  29. #endif
  30. /* this should be static vars for module size */
  31. struct fmr2_device
  32. {
  33. int port;
  34. int curvol; /* 0-65535, if not volume 0 or 65535 */
  35. int mute;
  36. int stereo; /* card is producing stereo audio */
  37. unsigned long curfreq; /* freq in kHz */
  38. int card_type;
  39. __u32 flags;
  40. };
  41. static int io = 0x384;
  42. static int radio_nr = -1;
  43. /* hw precision is 12.5 kHz
  44. * It is only usefull to give freq in intervall of 200 (=0.0125Mhz),
  45. * other bits will be truncated
  46. */
  47. #define RSF16_ENCODE(x) ((x)/200+856)
  48. #define RSF16_MINFREQ 87*16000
  49. #define RSF16_MAXFREQ 108*16000
  50. static inline void wait(int n,int port)
  51. {
  52. for (;n;--n) inb(port);
  53. }
  54. static void outbits(int bits, unsigned int data, int nWait, int port)
  55. {
  56. int bit;
  57. for(;--bits>=0;) {
  58. bit = (data>>bits) & 1;
  59. outb(bit,port);
  60. wait(nWait,port);
  61. outb(bit|2,port);
  62. wait(nWait,port);
  63. outb(bit,port);
  64. wait(nWait,port);
  65. }
  66. }
  67. static inline void fmr2_mute(int port)
  68. {
  69. outb(0x00, port);
  70. wait(4,port);
  71. }
  72. static inline void fmr2_unmute(int port)
  73. {
  74. outb(0x04, port);
  75. wait(4,port);
  76. }
  77. static inline int fmr2_stereo_mode(int port)
  78. {
  79. int n = inb(port);
  80. outb(6,port);
  81. inb(port);
  82. n = ((n>>3)&1)^1;
  83. debug_print((KERN_DEBUG "stereo: %d\n", n));
  84. return n;
  85. }
  86. static int fmr2_product_info(struct fmr2_device *dev)
  87. {
  88. int n = inb(dev->port);
  89. n &= 0xC1;
  90. if (n == 0)
  91. {
  92. /* this should support volume set */
  93. dev->card_type = 12;
  94. return 0;
  95. }
  96. /* not volume (mine is 11) */
  97. dev->card_type = (n==128)?11:0;
  98. return n;
  99. }
  100. static inline int fmr2_getsigstr(struct fmr2_device *dev)
  101. {
  102. /* !!! work only if scanning freq */
  103. int port = dev->port, res = 0xffff;
  104. outb(5,port);
  105. wait(4,port);
  106. if (!(inb(port)&1)) res = 0;
  107. debug_print((KERN_DEBUG "signal: %d\n", res));
  108. return res;
  109. }
  110. /* set frequency and unmute card */
  111. static int fmr2_setfreq(struct fmr2_device *dev)
  112. {
  113. int port = dev->port;
  114. unsigned long freq = dev->curfreq;
  115. fmr2_mute(port);
  116. /* 0x42 for mono output
  117. * 0x102 forward scanning
  118. * 0x182 scansione avanti
  119. */
  120. outbits(9,0x2,3,port);
  121. outbits(16,RSF16_ENCODE(freq),2,port);
  122. fmr2_unmute(port);
  123. /* wait 0.11 sec */
  124. msleep(110);
  125. /* NOTE if mute this stop radio
  126. you must set freq on unmute */
  127. dev->stereo = fmr2_stereo_mode(port);
  128. return 0;
  129. }
  130. /* !!! not tested, in my card this does't work !!! */
  131. static int fmr2_setvolume(struct fmr2_device *dev)
  132. {
  133. int i,a,n, port = dev->port;
  134. if (dev->card_type != 11) return 1;
  135. switch( (dev->curvol+(1<<11)) >> 12 )
  136. {
  137. case 0: case 1: n = 0x21; break;
  138. case 2: n = 0x84; break;
  139. case 3: n = 0x90; break;
  140. case 4: n = 0x104; break;
  141. case 5: n = 0x110; break;
  142. case 6: n = 0x204; break;
  143. case 7: n = 0x210; break;
  144. case 8: n = 0x402; break;
  145. case 9: n = 0x404; break;
  146. default:
  147. case 10: n = 0x408; break;
  148. case 11: n = 0x410; break;
  149. case 12: n = 0x801; break;
  150. case 13: n = 0x802; break;
  151. case 14: n = 0x804; break;
  152. case 15: n = 0x808; break;
  153. case 16: n = 0x810; break;
  154. }
  155. for(i=12;--i>=0;)
  156. {
  157. a = ((n >> i) & 1) << 6; /* if (a=0) a= 0; else a= 0x40; */
  158. outb(a|4, port);
  159. wait(4,port);
  160. outb(a|0x24, port);
  161. wait(4,port);
  162. outb(a|4, port);
  163. wait(4,port);
  164. }
  165. for(i=6;--i>=0;)
  166. {
  167. a = ((0x18 >> i) & 1) << 6;
  168. outb(a|4, port);
  169. wait(4,port);
  170. outb(a|0x24, port);
  171. wait(4,port);
  172. outb(a|4, port);
  173. wait(4,port);
  174. }
  175. wait(4,port);
  176. outb(0x14, port);
  177. return 0;
  178. }
  179. static int fmr2_do_ioctl(struct inode *inode, struct file *file,
  180. unsigned int cmd, void *arg)
  181. {
  182. struct video_device *dev = video_devdata(file);
  183. struct fmr2_device *fmr2 = dev->priv;
  184. debug_print((KERN_DEBUG "freq %ld flags %d vol %d mute %d "
  185. "stereo %d type %d\n",
  186. fmr2->curfreq, fmr2->flags, fmr2->curvol, fmr2->mute,
  187. fmr2->stereo, fmr2->card_type));
  188. switch(cmd)
  189. {
  190. case VIDIOCGCAP:
  191. {
  192. struct video_capability *v = arg;
  193. memset(v,0,sizeof(*v));
  194. strcpy(v->name, "SF16-FMR2 radio");
  195. v->type=VID_TYPE_TUNER;
  196. v->channels=1;
  197. v->audios=1;
  198. return 0;
  199. }
  200. case VIDIOCGTUNER:
  201. {
  202. struct video_tuner *v = arg;
  203. int mult;
  204. if(v->tuner) /* Only 1 tuner */
  205. return -EINVAL;
  206. strcpy(v->name, "FM");
  207. mult = (fmr2->flags & VIDEO_TUNER_LOW) ? 1 : 1000;
  208. v->rangelow = RSF16_MINFREQ/mult;
  209. v->rangehigh = RSF16_MAXFREQ/mult;
  210. v->flags = fmr2->flags | VIDEO_AUDIO_MUTABLE;
  211. if (fmr2->mute)
  212. v->flags |= VIDEO_AUDIO_MUTE;
  213. v->mode=VIDEO_MODE_AUTO;
  214. down(&lock);
  215. v->signal = fmr2_getsigstr(fmr2);
  216. up(&lock);
  217. return 0;
  218. }
  219. case VIDIOCSTUNER:
  220. {
  221. struct video_tuner *v = arg;
  222. if (v->tuner!=0)
  223. return -EINVAL;
  224. fmr2->flags = v->flags & VIDEO_TUNER_LOW;
  225. return 0;
  226. }
  227. case VIDIOCGFREQ:
  228. {
  229. unsigned long *freq = arg;
  230. *freq = fmr2->curfreq;
  231. if (!(fmr2->flags & VIDEO_TUNER_LOW))
  232. *freq /= 1000;
  233. return 0;
  234. }
  235. case VIDIOCSFREQ:
  236. {
  237. unsigned long *freq = arg;
  238. if (!(fmr2->flags & VIDEO_TUNER_LOW))
  239. *freq *= 1000;
  240. if ( *freq < RSF16_MINFREQ || *freq > RSF16_MAXFREQ )
  241. return -EINVAL;
  242. /* rounding in steps of 200 to match th freq
  243. * that will be used
  244. */
  245. fmr2->curfreq = (*freq/200)*200;
  246. /* set card freq (if not muted) */
  247. if (fmr2->curvol && !fmr2->mute)
  248. {
  249. down(&lock);
  250. fmr2_setfreq(fmr2);
  251. up(&lock);
  252. }
  253. return 0;
  254. }
  255. case VIDIOCGAUDIO:
  256. {
  257. struct video_audio *v = arg;
  258. memset(v,0,sizeof(*v));
  259. /* !!! do not return VIDEO_AUDIO_MUTE */
  260. v->flags = VIDEO_AUDIO_MUTABLE;
  261. strcpy(v->name, "Radio");
  262. /* get current stereo mode */
  263. v->mode = fmr2->stereo ? VIDEO_SOUND_STEREO: VIDEO_SOUND_MONO;
  264. /* volume supported ? */
  265. if (fmr2->card_type == 11)
  266. {
  267. v->flags |= VIDEO_AUDIO_VOLUME;
  268. v->step = 1 << 12;
  269. v->volume = fmr2->curvol;
  270. }
  271. debug_print((KERN_DEBUG "Get flags %d vol %d\n", v->flags, v->volume));
  272. return 0;
  273. }
  274. case VIDIOCSAUDIO:
  275. {
  276. struct video_audio *v = arg;
  277. if(v->audio)
  278. return -EINVAL;
  279. debug_print((KERN_DEBUG "Set flags %d vol %d\n", v->flags, v->volume));
  280. /* set volume */
  281. if (v->flags & VIDEO_AUDIO_VOLUME)
  282. fmr2->curvol = v->volume; /* !!! set with precision */
  283. if (fmr2->card_type != 11) fmr2->curvol = 65535;
  284. fmr2->mute = 0;
  285. if (v->flags & VIDEO_AUDIO_MUTE)
  286. fmr2->mute = 1;
  287. #ifdef DEBUG
  288. if (fmr2->curvol && !fmr2->mute)
  289. printk(KERN_DEBUG "unmute\n");
  290. else
  291. printk(KERN_DEBUG "mute\n");
  292. #endif
  293. down(&lock);
  294. if (fmr2->curvol && !fmr2->mute)
  295. {
  296. fmr2_setvolume(fmr2);
  297. fmr2_setfreq(fmr2);
  298. }
  299. else fmr2_mute(fmr2->port);
  300. up(&lock);
  301. return 0;
  302. }
  303. case VIDIOCGUNIT:
  304. {
  305. struct video_unit *v = arg;
  306. v->video=VIDEO_NO_UNIT;
  307. v->vbi=VIDEO_NO_UNIT;
  308. v->radio=dev->minor;
  309. v->audio=0; /* How do we find out this??? */
  310. v->teletext=VIDEO_NO_UNIT;
  311. return 0;
  312. }
  313. default:
  314. return -ENOIOCTLCMD;
  315. }
  316. }
  317. static int fmr2_ioctl(struct inode *inode, struct file *file,
  318. unsigned int cmd, unsigned long arg)
  319. {
  320. return video_usercopy(inode, file, cmd, arg, fmr2_do_ioctl);
  321. }
  322. static struct fmr2_device fmr2_unit;
  323. static struct file_operations fmr2_fops = {
  324. .owner = THIS_MODULE,
  325. .open = video_exclusive_open,
  326. .release = video_exclusive_release,
  327. .ioctl = fmr2_ioctl,
  328. .llseek = no_llseek,
  329. };
  330. static struct video_device fmr2_radio=
  331. {
  332. .owner = THIS_MODULE,
  333. .name = "SF16FMR2 radio",
  334. . type = VID_TYPE_TUNER,
  335. .hardware = VID_HARDWARE_SF16FMR2,
  336. .fops = &fmr2_fops,
  337. };
  338. static int __init fmr2_init(void)
  339. {
  340. fmr2_unit.port = io;
  341. fmr2_unit.curvol = 0;
  342. fmr2_unit.mute = 0;
  343. fmr2_unit.curfreq = 0;
  344. fmr2_unit.stereo = 1;
  345. fmr2_unit.flags = VIDEO_TUNER_LOW;
  346. fmr2_unit.card_type = 0;
  347. fmr2_radio.priv = &fmr2_unit;
  348. init_MUTEX(&lock);
  349. if (request_region(io, 2, "sf16fmr2"))
  350. {
  351. printk(KERN_ERR "fmr2: port 0x%x already in use\n", io);
  352. return -EBUSY;
  353. }
  354. if(video_register_device(&fmr2_radio, VFL_TYPE_RADIO, radio_nr)==-1)
  355. {
  356. release_region(io, 2);
  357. return -EINVAL;
  358. }
  359. printk(KERN_INFO "SF16FMR2 radio card driver at 0x%x.\n", io);
  360. debug_print((KERN_DEBUG "Mute %d Low %d\n",VIDEO_AUDIO_MUTE,VIDEO_TUNER_LOW));
  361. /* mute card - prevents noisy bootups */
  362. down(&lock);
  363. fmr2_mute(io);
  364. fmr2_product_info(&fmr2_unit);
  365. up(&lock);
  366. debug_print((KERN_DEBUG "card_type %d\n", fmr2_unit.card_type));
  367. return 0;
  368. }
  369. MODULE_AUTHOR("Ziglio Frediano, freddy77@angelfire.com");
  370. MODULE_DESCRIPTION("A driver for the SF16FMR2 radio.");
  371. MODULE_LICENSE("GPL");
  372. module_param(io, int, 0);
  373. MODULE_PARM_DESC(io, "I/O address of the SF16FMR2 card (should be 0x384, if do not work try 0x284)");
  374. module_param(radio_nr, int, 0);
  375. static void __exit fmr2_cleanup_module(void)
  376. {
  377. video_unregister_device(&fmr2_radio);
  378. release_region(io,2);
  379. }
  380. module_init(fmr2_init);
  381. module_exit(fmr2_cleanup_module);
  382. #ifndef MODULE
  383. static int __init fmr2_setup_io(char *str)
  384. {
  385. get_option(&str, &io);
  386. return 1;
  387. }
  388. __setup("sf16fmr2=", fmr2_setup_io);
  389. #endif