radio-sf16fmr2.c 11 KB

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