radio-aimslab.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. /* radiotrack (radioreveal) driver for Linux radio support
  2. * (c) 1997 M. Kirkwood
  3. * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
  4. * Converted to new API by Alan Cox <Alan.Cox@linux.org>
  5. * Various bugfixes and enhancements by Russell Kroll <rkroll@exploits.org>
  6. *
  7. * History:
  8. * 1999-02-24 Russell Kroll <rkroll@exploits.org>
  9. * Fine tuning/VIDEO_TUNER_LOW
  10. * Frequency range expanded to start at 87 MHz
  11. *
  12. * TODO: Allow for more than one of these foolish entities :-)
  13. *
  14. * Notes on the hardware (reverse engineered from other peoples'
  15. * reverse engineering of AIMS' code :-)
  16. *
  17. * Frequency control is done digitally -- ie out(port,encodefreq(95.8));
  18. *
  19. * The signal strength query is unsurprisingly inaccurate. And it seems
  20. * to indicate that (on my card, at least) the frequency setting isn't
  21. * too great. (I have to tune up .025MHz from what the freq should be
  22. * to get a report that the thing is tuned.)
  23. *
  24. * Volume control is (ugh) analogue:
  25. * out(port, start_increasing_volume);
  26. * wait(a_wee_while);
  27. * out(port, stop_changing_the_volume);
  28. *
  29. */
  30. #include <linux/module.h> /* Modules */
  31. #include <linux/init.h> /* Initdata */
  32. #include <linux/ioport.h> /* request_region */
  33. #include <linux/delay.h> /* udelay */
  34. #include <asm/io.h> /* outb, outb_p */
  35. #include <asm/uaccess.h> /* copy to/from user */
  36. #include <linux/videodev2.h> /* kernel radio structs */
  37. #include <media/v4l2-common.h>
  38. #include <linux/version.h> /* for KERNEL_VERSION MACRO */
  39. #define RADIO_VERSION KERNEL_VERSION(0,0,2)
  40. #ifndef CONFIG_RADIO_RTRACK_PORT
  41. #define CONFIG_RADIO_RTRACK_PORT -1
  42. #endif
  43. static int io = CONFIG_RADIO_RTRACK_PORT;
  44. static int radio_nr = -1;
  45. static struct mutex lock;
  46. struct rt_device
  47. {
  48. int port;
  49. int curvol;
  50. unsigned long curfreq;
  51. int muted;
  52. };
  53. /* local things */
  54. static void sleep_delay(long n)
  55. {
  56. /* Sleep nicely for 'n' uS */
  57. int d=n/msecs_to_jiffies(1000);
  58. if(!d)
  59. udelay(n);
  60. else
  61. msleep(jiffies_to_msecs(d));
  62. }
  63. static void rt_decvol(void)
  64. {
  65. outb(0x58, io); /* volume down + sigstr + on */
  66. sleep_delay(100000);
  67. outb(0xd8, io); /* volume steady + sigstr + on */
  68. }
  69. static void rt_incvol(void)
  70. {
  71. outb(0x98, io); /* volume up + sigstr + on */
  72. sleep_delay(100000);
  73. outb(0xd8, io); /* volume steady + sigstr + on */
  74. }
  75. static void rt_mute(struct rt_device *dev)
  76. {
  77. dev->muted = 1;
  78. mutex_lock(&lock);
  79. outb(0xd0, io); /* volume steady, off */
  80. mutex_unlock(&lock);
  81. }
  82. static int rt_setvol(struct rt_device *dev, int vol)
  83. {
  84. int i;
  85. mutex_lock(&lock);
  86. if(vol == dev->curvol) { /* requested volume = current */
  87. if (dev->muted) { /* user is unmuting the card */
  88. dev->muted = 0;
  89. outb (0xd8, io); /* enable card */
  90. }
  91. mutex_unlock(&lock);
  92. return 0;
  93. }
  94. if(vol == 0) { /* volume = 0 means mute the card */
  95. outb(0x48, io); /* volume down but still "on" */
  96. sleep_delay(2000000); /* make sure it's totally down */
  97. outb(0xd0, io); /* volume steady, off */
  98. dev->curvol = 0; /* track the volume state! */
  99. mutex_unlock(&lock);
  100. return 0;
  101. }
  102. dev->muted = 0;
  103. if(vol > dev->curvol)
  104. for(i = dev->curvol; i < vol; i++)
  105. rt_incvol();
  106. else
  107. for(i = dev->curvol; i > vol; i--)
  108. rt_decvol();
  109. dev->curvol = vol;
  110. mutex_unlock(&lock);
  111. return 0;
  112. }
  113. /* the 128+64 on these outb's is to keep the volume stable while tuning
  114. * without them, the volume _will_ creep up with each frequency change
  115. * and bit 4 (+16) is to keep the signal strength meter enabled
  116. */
  117. static void send_0_byte(int port, struct rt_device *dev)
  118. {
  119. if ((dev->curvol == 0) || (dev->muted)) {
  120. outb_p(128+64+16+ 1, port); /* wr-enable + data low */
  121. outb_p(128+64+16+2+1, port); /* clock */
  122. }
  123. else {
  124. outb_p(128+64+16+8+ 1, port); /* on + wr-enable + data low */
  125. outb_p(128+64+16+8+2+1, port); /* clock */
  126. }
  127. sleep_delay(1000);
  128. }
  129. static void send_1_byte(int port, struct rt_device *dev)
  130. {
  131. if ((dev->curvol == 0) || (dev->muted)) {
  132. outb_p(128+64+16+4 +1, port); /* wr-enable+data high */
  133. outb_p(128+64+16+4+2+1, port); /* clock */
  134. }
  135. else {
  136. outb_p(128+64+16+8+4 +1, port); /* on+wr-enable+data high */
  137. outb_p(128+64+16+8+4+2+1, port); /* clock */
  138. }
  139. sleep_delay(1000);
  140. }
  141. static int rt_setfreq(struct rt_device *dev, unsigned long freq)
  142. {
  143. int i;
  144. /* adapted from radio-aztech.c */
  145. /* now uses VIDEO_TUNER_LOW for fine tuning */
  146. freq += 171200; /* Add 10.7 MHz IF */
  147. freq /= 800; /* Convert to 50 kHz units */
  148. mutex_lock(&lock); /* Stop other ops interfering */
  149. send_0_byte (io, dev); /* 0: LSB of frequency */
  150. for (i = 0; i < 13; i++) /* : frequency bits (1-13) */
  151. if (freq & (1 << i))
  152. send_1_byte (io, dev);
  153. else
  154. send_0_byte (io, dev);
  155. send_0_byte (io, dev); /* 14: test bit - always 0 */
  156. send_0_byte (io, dev); /* 15: test bit - always 0 */
  157. send_0_byte (io, dev); /* 16: band data 0 - always 0 */
  158. send_0_byte (io, dev); /* 17: band data 1 - always 0 */
  159. send_0_byte (io, dev); /* 18: band data 2 - always 0 */
  160. send_0_byte (io, dev); /* 19: time base - always 0 */
  161. send_0_byte (io, dev); /* 20: spacing (0 = 25 kHz) */
  162. send_1_byte (io, dev); /* 21: spacing (1 = 25 kHz) */
  163. send_0_byte (io, dev); /* 22: spacing (0 = 25 kHz) */
  164. send_1_byte (io, dev); /* 23: AM/FM (FM = 1, always) */
  165. if ((dev->curvol == 0) || (dev->muted))
  166. outb (0xd0, io); /* volume steady + sigstr */
  167. else
  168. outb (0xd8, io); /* volume steady + sigstr + on */
  169. mutex_unlock(&lock);
  170. return 0;
  171. }
  172. static int rt_getsigstr(struct rt_device *dev)
  173. {
  174. if (inb(io) & 2) /* bit set = no signal present */
  175. return 0;
  176. return 1; /* signal present */
  177. }
  178. static struct v4l2_queryctrl radio_qctrl[] = {
  179. {
  180. .id = V4L2_CID_AUDIO_MUTE,
  181. .name = "Mute",
  182. .minimum = 0,
  183. .maximum = 1,
  184. .default_value = 1,
  185. .type = V4L2_CTRL_TYPE_BOOLEAN,
  186. },{
  187. .id = V4L2_CID_AUDIO_VOLUME,
  188. .name = "Volume",
  189. .minimum = 0,
  190. .maximum = 0xff,
  191. .step = 1,
  192. .default_value = 0xff,
  193. .type = V4L2_CTRL_TYPE_INTEGER,
  194. }
  195. };
  196. static int vidioc_querycap(struct file *file, void *priv,
  197. struct v4l2_capability *v)
  198. {
  199. strlcpy(v->driver, "radio-aimslab", sizeof(v->driver));
  200. strlcpy(v->card, "RadioTrack", sizeof(v->card));
  201. sprintf(v->bus_info, "ISA");
  202. v->version = RADIO_VERSION;
  203. v->capabilities = V4L2_CAP_TUNER;
  204. return 0;
  205. }
  206. static int vidioc_g_tuner(struct file *file, void *priv,
  207. struct v4l2_tuner *v)
  208. {
  209. struct video_device *dev = video_devdata(file);
  210. struct rt_device *rt = dev->priv;
  211. if (v->index > 0)
  212. return -EINVAL;
  213. strcpy(v->name, "FM");
  214. v->type = V4L2_TUNER_RADIO;
  215. v->rangelow = (87*16000);
  216. v->rangehigh = (108*16000);
  217. v->rxsubchans = V4L2_TUNER_SUB_MONO;
  218. v->capability = V4L2_TUNER_CAP_LOW;
  219. v->audmode = V4L2_TUNER_MODE_MONO;
  220. v->signal = 0xffff*rt_getsigstr(rt);
  221. return 0;
  222. }
  223. static int vidioc_s_tuner(struct file *file, void *priv,
  224. struct v4l2_tuner *v)
  225. {
  226. if (v->index > 0)
  227. return -EINVAL;
  228. return 0;
  229. }
  230. static int vidioc_s_frequency(struct file *file, void *priv,
  231. struct v4l2_frequency *f)
  232. {
  233. struct video_device *dev = video_devdata(file);
  234. struct rt_device *rt = dev->priv;
  235. rt->curfreq = f->frequency;
  236. rt_setfreq(rt, rt->curfreq);
  237. return 0;
  238. }
  239. static int vidioc_g_frequency(struct file *file, void *priv,
  240. struct v4l2_frequency *f)
  241. {
  242. struct video_device *dev = video_devdata(file);
  243. struct rt_device *rt = dev->priv;
  244. f->type = V4L2_TUNER_RADIO;
  245. f->frequency = rt->curfreq;
  246. return 0;
  247. }
  248. static int vidioc_queryctrl(struct file *file, void *priv,
  249. struct v4l2_queryctrl *qc)
  250. {
  251. int i;
  252. for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
  253. if (qc->id && qc->id == radio_qctrl[i].id) {
  254. memcpy(qc, &(radio_qctrl[i]),
  255. sizeof(*qc));
  256. return 0;
  257. }
  258. }
  259. return -EINVAL;
  260. }
  261. static int vidioc_g_ctrl(struct file *file, void *priv,
  262. struct v4l2_control *ctrl)
  263. {
  264. struct video_device *dev = video_devdata(file);
  265. struct rt_device *rt = dev->priv;
  266. switch (ctrl->id) {
  267. case V4L2_CID_AUDIO_MUTE:
  268. ctrl->value = rt->muted;
  269. return 0;
  270. case V4L2_CID_AUDIO_VOLUME:
  271. ctrl->value = rt->curvol * 6554;
  272. return 0;
  273. }
  274. return -EINVAL;
  275. }
  276. static int vidioc_s_ctrl(struct file *file, void *priv,
  277. struct v4l2_control *ctrl)
  278. {
  279. struct video_device *dev = video_devdata(file);
  280. struct rt_device *rt = dev->priv;
  281. switch (ctrl->id) {
  282. case V4L2_CID_AUDIO_MUTE:
  283. if (ctrl->value)
  284. rt_mute(rt);
  285. else
  286. rt_setvol(rt,rt->curvol);
  287. return 0;
  288. case V4L2_CID_AUDIO_VOLUME:
  289. rt_setvol(rt,ctrl->value);
  290. return 0;
  291. }
  292. return -EINVAL;
  293. }
  294. static int vidioc_g_audio (struct file *file, void *priv,
  295. struct v4l2_audio *a)
  296. {
  297. if (a->index > 1)
  298. return -EINVAL;
  299. strcpy(a->name, "Radio");
  300. a->capability = V4L2_AUDCAP_STEREO;
  301. return 0;
  302. }
  303. static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
  304. {
  305. *i = 0;
  306. return 0;
  307. }
  308. static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
  309. {
  310. if (i != 0)
  311. return -EINVAL;
  312. return 0;
  313. }
  314. static int vidioc_s_audio(struct file *file, void *priv,
  315. struct v4l2_audio *a)
  316. {
  317. if (a->index != 0)
  318. return -EINVAL;
  319. return 0;
  320. }
  321. static struct rt_device rtrack_unit;
  322. static const struct file_operations rtrack_fops = {
  323. .owner = THIS_MODULE,
  324. .open = video_exclusive_open,
  325. .release = video_exclusive_release,
  326. .ioctl = video_ioctl2,
  327. #ifdef CONFIG_COMPAT
  328. .compat_ioctl = v4l_compat_ioctl32,
  329. #endif
  330. .llseek = no_llseek,
  331. };
  332. static struct video_device rtrack_radio=
  333. {
  334. .owner = THIS_MODULE,
  335. .name = "RadioTrack radio",
  336. .type = VID_TYPE_TUNER,
  337. .fops = &rtrack_fops,
  338. .vidioc_querycap = vidioc_querycap,
  339. .vidioc_g_tuner = vidioc_g_tuner,
  340. .vidioc_s_tuner = vidioc_s_tuner,
  341. .vidioc_g_audio = vidioc_g_audio,
  342. .vidioc_s_audio = vidioc_s_audio,
  343. .vidioc_g_input = vidioc_g_input,
  344. .vidioc_s_input = vidioc_s_input,
  345. .vidioc_g_frequency = vidioc_g_frequency,
  346. .vidioc_s_frequency = vidioc_s_frequency,
  347. .vidioc_queryctrl = vidioc_queryctrl,
  348. .vidioc_g_ctrl = vidioc_g_ctrl,
  349. .vidioc_s_ctrl = vidioc_s_ctrl,
  350. };
  351. static int __init rtrack_init(void)
  352. {
  353. if(io==-1)
  354. {
  355. printk(KERN_ERR "You must set an I/O address with io=0x???\n");
  356. return -EINVAL;
  357. }
  358. if (!request_region(io, 2, "rtrack"))
  359. {
  360. printk(KERN_ERR "rtrack: port 0x%x already in use\n", io);
  361. return -EBUSY;
  362. }
  363. rtrack_radio.priv=&rtrack_unit;
  364. if(video_register_device(&rtrack_radio, VFL_TYPE_RADIO, radio_nr)==-1)
  365. {
  366. release_region(io, 2);
  367. return -EINVAL;
  368. }
  369. printk(KERN_INFO "AIMSlab RadioTrack/RadioReveal card driver.\n");
  370. /* Set up the I/O locking */
  371. mutex_init(&lock);
  372. /* mute card - prevents noisy bootups */
  373. /* this ensures that the volume is all the way down */
  374. outb(0x48, io); /* volume down but still "on" */
  375. sleep_delay(2000000); /* make sure it's totally down */
  376. outb(0xc0, io); /* steady volume, mute card */
  377. rtrack_unit.curvol = 0;
  378. return 0;
  379. }
  380. MODULE_AUTHOR("M.Kirkwood");
  381. MODULE_DESCRIPTION("A driver for the RadioTrack/RadioReveal radio card.");
  382. MODULE_LICENSE("GPL");
  383. module_param(io, int, 0);
  384. MODULE_PARM_DESC(io, "I/O address of the RadioTrack card (0x20f or 0x30f)");
  385. module_param(radio_nr, int, 0);
  386. static void __exit cleanup_rtrack_module(void)
  387. {
  388. video_unregister_device(&rtrack_radio);
  389. release_region(io,2);
  390. }
  391. module_init(rtrack_init);
  392. module_exit(cleanup_rtrack_module);