radio-aimslab.c 11 KB

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