radio-aimslab.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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/videodev2.h> /* kernel radio structs */
  34. #include <linux/version.h> /* for KERNEL_VERSION MACRO */
  35. #include <linux/io.h> /* outb, outb_p */
  36. #include <media/v4l2-device.h>
  37. #include <media/v4l2-ioctl.h>
  38. MODULE_AUTHOR("M.Kirkwood");
  39. MODULE_DESCRIPTION("A driver for the RadioTrack/RadioReveal radio card.");
  40. MODULE_LICENSE("GPL");
  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. module_param(io, int, 0);
  47. MODULE_PARM_DESC(io, "I/O address of the RadioTrack card (0x20f or 0x30f)");
  48. module_param(radio_nr, int, 0);
  49. #define RADIO_VERSION KERNEL_VERSION(0, 0, 2)
  50. struct rtrack
  51. {
  52. struct v4l2_device v4l2_dev;
  53. struct video_device vdev;
  54. int port;
  55. int curvol;
  56. unsigned long curfreq;
  57. int muted;
  58. int io;
  59. struct mutex lock;
  60. };
  61. static struct rtrack rtrack_card;
  62. /* local things */
  63. static void rt_decvol(struct rtrack *rt)
  64. {
  65. outb(0x58, rt->io); /* volume down + sigstr + on */
  66. msleep(100);
  67. outb(0xd8, rt->io); /* volume steady + sigstr + on */
  68. }
  69. static void rt_incvol(struct rtrack *rt)
  70. {
  71. outb(0x98, rt->io); /* volume up + sigstr + on */
  72. msleep(100);
  73. outb(0xd8, rt->io); /* volume steady + sigstr + on */
  74. }
  75. static void rt_mute(struct rtrack *rt)
  76. {
  77. rt->muted = 1;
  78. mutex_lock(&rt->lock);
  79. outb(0xd0, rt->io); /* volume steady, off */
  80. mutex_unlock(&rt->lock);
  81. }
  82. static int rt_setvol(struct rtrack *rt, int vol)
  83. {
  84. int i;
  85. mutex_lock(&rt->lock);
  86. if (vol == rt->curvol) { /* requested volume = current */
  87. if (rt->muted) { /* user is unmuting the card */
  88. rt->muted = 0;
  89. outb(0xd8, rt->io); /* enable card */
  90. }
  91. mutex_unlock(&rt->lock);
  92. return 0;
  93. }
  94. if (vol == 0) { /* volume = 0 means mute the card */
  95. outb(0x48, rt->io); /* volume down but still "on" */
  96. msleep(2000); /* make sure it's totally down */
  97. outb(0xd0, rt->io); /* volume steady, off */
  98. rt->curvol = 0; /* track the volume state! */
  99. mutex_unlock(&rt->lock);
  100. return 0;
  101. }
  102. rt->muted = 0;
  103. if (vol > rt->curvol)
  104. for (i = rt->curvol; i < vol; i++)
  105. rt_incvol(rt);
  106. else
  107. for (i = rt->curvol; i > vol; i--)
  108. rt_decvol(rt);
  109. rt->curvol = vol;
  110. mutex_unlock(&rt->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(struct rtrack *rt)
  118. {
  119. if (rt->curvol == 0 || rt->muted) {
  120. outb_p(128+64+16+ 1, rt->io); /* wr-enable + data low */
  121. outb_p(128+64+16+2+1, rt->io); /* clock */
  122. }
  123. else {
  124. outb_p(128+64+16+8+ 1, rt->io); /* on + wr-enable + data low */
  125. outb_p(128+64+16+8+2+1, rt->io); /* clock */
  126. }
  127. msleep(1);
  128. }
  129. static void send_1_byte(struct rtrack *rt)
  130. {
  131. if (rt->curvol == 0 || rt->muted) {
  132. outb_p(128+64+16+4 +1, rt->io); /* wr-enable+data high */
  133. outb_p(128+64+16+4+2+1, rt->io); /* clock */
  134. }
  135. else {
  136. outb_p(128+64+16+8+4 +1, rt->io); /* on+wr-enable+data high */
  137. outb_p(128+64+16+8+4+2+1, rt->io); /* clock */
  138. }
  139. msleep(1);
  140. }
  141. static int rt_setfreq(struct rtrack *rt, unsigned long freq)
  142. {
  143. int i;
  144. mutex_lock(&rt->lock); /* Stop other ops interfering */
  145. rt->curfreq = freq;
  146. /* now uses VIDEO_TUNER_LOW for fine tuning */
  147. freq += 171200; /* Add 10.7 MHz IF */
  148. freq /= 800; /* Convert to 50 kHz units */
  149. send_0_byte(rt); /* 0: LSB of frequency */
  150. for (i = 0; i < 13; i++) /* : frequency bits (1-13) */
  151. if (freq & (1 << i))
  152. send_1_byte(rt);
  153. else
  154. send_0_byte(rt);
  155. send_0_byte(rt); /* 14: test bit - always 0 */
  156. send_0_byte(rt); /* 15: test bit - always 0 */
  157. send_0_byte(rt); /* 16: band data 0 - always 0 */
  158. send_0_byte(rt); /* 17: band data 1 - always 0 */
  159. send_0_byte(rt); /* 18: band data 2 - always 0 */
  160. send_0_byte(rt); /* 19: time base - always 0 */
  161. send_0_byte(rt); /* 20: spacing (0 = 25 kHz) */
  162. send_1_byte(rt); /* 21: spacing (1 = 25 kHz) */
  163. send_0_byte(rt); /* 22: spacing (0 = 25 kHz) */
  164. send_1_byte(rt); /* 23: AM/FM (FM = 1, always) */
  165. if (rt->curvol == 0 || rt->muted)
  166. outb(0xd0, rt->io); /* volume steady + sigstr */
  167. else
  168. outb(0xd8, rt->io); /* volume steady + sigstr + on */
  169. mutex_unlock(&rt->lock);
  170. return 0;
  171. }
  172. static int rt_getsigstr(struct rtrack *rt)
  173. {
  174. int sig = 1;
  175. mutex_lock(&rt->lock);
  176. if (inb(rt->io) & 2) /* bit set = no signal present */
  177. sig = 0;
  178. mutex_unlock(&rt->lock);
  179. return sig;
  180. }
  181. static int vidioc_querycap(struct file *file, void *priv,
  182. struct v4l2_capability *v)
  183. {
  184. strlcpy(v->driver, "radio-aimslab", sizeof(v->driver));
  185. strlcpy(v->card, "RadioTrack", sizeof(v->card));
  186. strlcpy(v->bus_info, "ISA", sizeof(v->bus_info));
  187. v->version = RADIO_VERSION;
  188. v->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
  189. return 0;
  190. }
  191. static int vidioc_g_tuner(struct file *file, void *priv,
  192. struct v4l2_tuner *v)
  193. {
  194. struct rtrack *rt = video_drvdata(file);
  195. if (v->index > 0)
  196. return -EINVAL;
  197. strlcpy(v->name, "FM", sizeof(v->name));
  198. v->type = V4L2_TUNER_RADIO;
  199. v->rangelow = 87 * 16000;
  200. v->rangehigh = 108 * 16000;
  201. v->rxsubchans = V4L2_TUNER_SUB_MONO;
  202. v->capability = V4L2_TUNER_CAP_LOW;
  203. v->audmode = V4L2_TUNER_MODE_MONO;
  204. v->signal = 0xffff * rt_getsigstr(rt);
  205. return 0;
  206. }
  207. static int vidioc_s_tuner(struct file *file, void *priv,
  208. struct v4l2_tuner *v)
  209. {
  210. return v->index ? -EINVAL : 0;
  211. }
  212. static int vidioc_s_frequency(struct file *file, void *priv,
  213. struct v4l2_frequency *f)
  214. {
  215. struct rtrack *rt = video_drvdata(file);
  216. if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
  217. return -EINVAL;
  218. rt_setfreq(rt, f->frequency);
  219. return 0;
  220. }
  221. static int vidioc_g_frequency(struct file *file, void *priv,
  222. struct v4l2_frequency *f)
  223. {
  224. struct rtrack *rt = video_drvdata(file);
  225. if (f->tuner != 0)
  226. return -EINVAL;
  227. f->type = V4L2_TUNER_RADIO;
  228. f->frequency = rt->curfreq;
  229. return 0;
  230. }
  231. static int vidioc_queryctrl(struct file *file, void *priv,
  232. struct v4l2_queryctrl *qc)
  233. {
  234. switch (qc->id) {
  235. case V4L2_CID_AUDIO_MUTE:
  236. return v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
  237. case V4L2_CID_AUDIO_VOLUME:
  238. return v4l2_ctrl_query_fill(qc, 0, 0xff, 1, 0xff);
  239. }
  240. return -EINVAL;
  241. }
  242. static int vidioc_g_ctrl(struct file *file, void *priv,
  243. struct v4l2_control *ctrl)
  244. {
  245. struct rtrack *rt = video_drvdata(file);
  246. switch (ctrl->id) {
  247. case V4L2_CID_AUDIO_MUTE:
  248. ctrl->value = rt->muted;
  249. return 0;
  250. case V4L2_CID_AUDIO_VOLUME:
  251. ctrl->value = rt->curvol;
  252. return 0;
  253. }
  254. return -EINVAL;
  255. }
  256. static int vidioc_s_ctrl(struct file *file, void *priv,
  257. struct v4l2_control *ctrl)
  258. {
  259. struct rtrack *rt = video_drvdata(file);
  260. switch (ctrl->id) {
  261. case V4L2_CID_AUDIO_MUTE:
  262. if (ctrl->value)
  263. rt_mute(rt);
  264. else
  265. rt_setvol(rt, rt->curvol);
  266. return 0;
  267. case V4L2_CID_AUDIO_VOLUME:
  268. rt_setvol(rt, ctrl->value);
  269. return 0;
  270. }
  271. return -EINVAL;
  272. }
  273. static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
  274. {
  275. *i = 0;
  276. return 0;
  277. }
  278. static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
  279. {
  280. return i ? -EINVAL : 0;
  281. }
  282. static int vidioc_g_audio(struct file *file, void *priv,
  283. struct v4l2_audio *a)
  284. {
  285. a->index = 0;
  286. strlcpy(a->name, "Radio", sizeof(a->name));
  287. a->capability = V4L2_AUDCAP_STEREO;
  288. return 0;
  289. }
  290. static int vidioc_s_audio(struct file *file, void *priv,
  291. struct v4l2_audio *a)
  292. {
  293. return a->index ? -EINVAL : 0;
  294. }
  295. static const struct v4l2_file_operations rtrack_fops = {
  296. .owner = THIS_MODULE,
  297. .unlocked_ioctl = video_ioctl2,
  298. };
  299. static const struct v4l2_ioctl_ops rtrack_ioctl_ops = {
  300. .vidioc_querycap = vidioc_querycap,
  301. .vidioc_g_tuner = vidioc_g_tuner,
  302. .vidioc_s_tuner = vidioc_s_tuner,
  303. .vidioc_g_audio = vidioc_g_audio,
  304. .vidioc_s_audio = vidioc_s_audio,
  305. .vidioc_g_input = vidioc_g_input,
  306. .vidioc_s_input = vidioc_s_input,
  307. .vidioc_g_frequency = vidioc_g_frequency,
  308. .vidioc_s_frequency = vidioc_s_frequency,
  309. .vidioc_queryctrl = vidioc_queryctrl,
  310. .vidioc_g_ctrl = vidioc_g_ctrl,
  311. .vidioc_s_ctrl = vidioc_s_ctrl,
  312. };
  313. static int __init rtrack_init(void)
  314. {
  315. struct rtrack *rt = &rtrack_card;
  316. struct v4l2_device *v4l2_dev = &rt->v4l2_dev;
  317. int res;
  318. strlcpy(v4l2_dev->name, "rtrack", sizeof(v4l2_dev->name));
  319. rt->io = io;
  320. if (rt->io == -1) {
  321. v4l2_err(v4l2_dev, "you must set an I/O address with io=0x20f or 0x30f\n");
  322. return -EINVAL;
  323. }
  324. if (!request_region(rt->io, 2, "rtrack")) {
  325. v4l2_err(v4l2_dev, "port 0x%x already in use\n", rt->io);
  326. return -EBUSY;
  327. }
  328. res = v4l2_device_register(NULL, v4l2_dev);
  329. if (res < 0) {
  330. release_region(rt->io, 2);
  331. v4l2_err(v4l2_dev, "could not register v4l2_device\n");
  332. return res;
  333. }
  334. strlcpy(rt->vdev.name, v4l2_dev->name, sizeof(rt->vdev.name));
  335. rt->vdev.v4l2_dev = v4l2_dev;
  336. rt->vdev.fops = &rtrack_fops;
  337. rt->vdev.ioctl_ops = &rtrack_ioctl_ops;
  338. rt->vdev.release = video_device_release_empty;
  339. video_set_drvdata(&rt->vdev, rt);
  340. /* Set up the I/O locking */
  341. mutex_init(&rt->lock);
  342. /* mute card - prevents noisy bootups */
  343. /* this ensures that the volume is all the way down */
  344. outb(0x48, rt->io); /* volume down but still "on" */
  345. msleep(2000); /* make sure it's totally down */
  346. outb(0xc0, rt->io); /* steady volume, mute card */
  347. if (video_register_device(&rt->vdev, VFL_TYPE_RADIO, radio_nr) < 0) {
  348. v4l2_device_unregister(&rt->v4l2_dev);
  349. release_region(rt->io, 2);
  350. return -EINVAL;
  351. }
  352. v4l2_info(v4l2_dev, "AIMSlab RadioTrack/RadioReveal card driver.\n");
  353. return 0;
  354. }
  355. static void __exit rtrack_exit(void)
  356. {
  357. struct rtrack *rt = &rtrack_card;
  358. video_unregister_device(&rt->vdev);
  359. v4l2_device_unregister(&rt->v4l2_dev);
  360. release_region(rt->io, 2);
  361. }
  362. module_init(rtrack_init);
  363. module_exit(rtrack_exit);