radio-sf16fmr2.c 11 KB

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