radio-aimslab.c 11 KB

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