radio-sf16fmr2.c 11 KB

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