radio-sf16fmr2.c 11 KB

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