radio-sf16fmr2.c 11 KB

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