radio-sf16fmr2.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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 fmr2_device *fmr2 = video_drvdata(file);
  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 fmr2_device *fmr2 = video_drvdata(file);
  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 fmr2_device *fmr2 = video_drvdata(file);
  252. f->type = V4L2_TUNER_RADIO;
  253. f->frequency = fmr2->curfreq;
  254. if (!(fmr2->flags & V4L2_TUNER_CAP_LOW))
  255. f->frequency /= 1000;
  256. return 0;
  257. }
  258. static int vidioc_queryctrl(struct file *file, void *priv,
  259. struct v4l2_queryctrl *qc)
  260. {
  261. int i;
  262. for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
  263. if (qc->id && qc->id == radio_qctrl[i].id) {
  264. memcpy(qc, &radio_qctrl[i], sizeof(*qc));
  265. return 0;
  266. }
  267. }
  268. return -EINVAL;
  269. }
  270. static int vidioc_g_ctrl(struct file *file, void *priv,
  271. struct v4l2_control *ctrl)
  272. {
  273. struct fmr2_device *fmr2 = video_drvdata(file);
  274. switch (ctrl->id) {
  275. case V4L2_CID_AUDIO_MUTE:
  276. ctrl->value = fmr2->mute;
  277. return 0;
  278. case V4L2_CID_AUDIO_VOLUME:
  279. ctrl->value = fmr2->curvol;
  280. return 0;
  281. }
  282. return -EINVAL;
  283. }
  284. static int vidioc_s_ctrl(struct file *file, void *priv,
  285. struct v4l2_control *ctrl)
  286. {
  287. struct fmr2_device *fmr2 = video_drvdata(file);
  288. switch (ctrl->id) {
  289. case V4L2_CID_AUDIO_MUTE:
  290. fmr2->mute = ctrl->value;
  291. break;
  292. case V4L2_CID_AUDIO_VOLUME:
  293. if (ctrl->value > radio_qctrl[AUD_VOL_INDEX].maximum)
  294. fmr2->curvol = radio_qctrl[AUD_VOL_INDEX].maximum;
  295. else
  296. fmr2->curvol = ctrl->value;
  297. break;
  298. default:
  299. return -EINVAL;
  300. }
  301. #ifdef DEBUG
  302. if (fmr2->curvol && !fmr2->mute)
  303. printk(KERN_DEBUG "unmute\n");
  304. else
  305. printk(KERN_DEBUG "mute\n");
  306. #endif
  307. mutex_lock(&lock);
  308. if (fmr2->curvol && !fmr2->mute) {
  309. fmr2_setvolume(fmr2);
  310. /* Set frequency and unmute card */
  311. fmr2_setfreq(fmr2);
  312. } else
  313. fmr2_mute(fmr2->port);
  314. mutex_unlock(&lock);
  315. return 0;
  316. }
  317. static int vidioc_g_audio(struct file *file, void *priv,
  318. struct v4l2_audio *a)
  319. {
  320. if (a->index > 1)
  321. return -EINVAL;
  322. strcpy(a->name, "Radio");
  323. a->capability = V4L2_AUDCAP_STEREO;
  324. return 0;
  325. }
  326. static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
  327. {
  328. *i = 0;
  329. return 0;
  330. }
  331. static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
  332. {
  333. if (i != 0)
  334. return -EINVAL;
  335. return 0;
  336. }
  337. static int vidioc_s_audio(struct file *file, void *priv,
  338. struct v4l2_audio *a)
  339. {
  340. if (a->index != 0)
  341. return -EINVAL;
  342. return 0;
  343. }
  344. static struct fmr2_device fmr2_unit;
  345. static int fmr2_exclusive_open(struct file *file)
  346. {
  347. return test_and_set_bit(0, &fmr2_unit.in_use) ? -EBUSY : 0;
  348. }
  349. static int fmr2_exclusive_release(struct file *file)
  350. {
  351. clear_bit(0, &fmr2_unit.in_use);
  352. return 0;
  353. }
  354. static const struct v4l2_file_operations fmr2_fops = {
  355. .owner = THIS_MODULE,
  356. .open = fmr2_exclusive_open,
  357. .release = fmr2_exclusive_release,
  358. .ioctl = video_ioctl2,
  359. };
  360. static const struct v4l2_ioctl_ops fmr2_ioctl_ops = {
  361. .vidioc_querycap = vidioc_querycap,
  362. .vidioc_g_tuner = vidioc_g_tuner,
  363. .vidioc_s_tuner = vidioc_s_tuner,
  364. .vidioc_g_audio = vidioc_g_audio,
  365. .vidioc_s_audio = vidioc_s_audio,
  366. .vidioc_g_input = vidioc_g_input,
  367. .vidioc_s_input = vidioc_s_input,
  368. .vidioc_g_frequency = vidioc_g_frequency,
  369. .vidioc_s_frequency = vidioc_s_frequency,
  370. .vidioc_queryctrl = vidioc_queryctrl,
  371. .vidioc_g_ctrl = vidioc_g_ctrl,
  372. .vidioc_s_ctrl = vidioc_s_ctrl,
  373. };
  374. static struct video_device fmr2_radio = {
  375. .name = "SF16FMR2 radio",
  376. .fops = &fmr2_fops,
  377. .ioctl_ops = &fmr2_ioctl_ops,
  378. .release = video_device_release_empty,
  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. video_set_drvdata(&fmr2_radio, &fmr2_unit);
  390. mutex_init(&lock);
  391. if (!request_region(io, 2, "sf16fmr2")) {
  392. printk(KERN_ERR "radio-sf16fmr2: request_region failed!\n");
  393. return -EBUSY;
  394. }
  395. if (video_register_device(&fmr2_radio, VFL_TYPE_RADIO, radio_nr) < 0) {
  396. release_region(io, 2);
  397. return -EINVAL;
  398. }
  399. printk(KERN_INFO "SF16FMR2 radio card driver at 0x%x.\n", io);
  400. /* mute card - prevents noisy bootups */
  401. mutex_lock(&lock);
  402. fmr2_mute(io);
  403. fmr2_product_info(&fmr2_unit);
  404. mutex_unlock(&lock);
  405. debug_print((KERN_DEBUG "card_type %d\n", fmr2_unit.card_type));
  406. /* Only card_type == 11 implements volume */
  407. if (fmr2_unit.card_type != 11)
  408. radio_qctrl[AUD_VOL_INDEX].maximum = 1;
  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