radio-sf16fmr2.c 9.7 KB

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