radio-aimslab.c 11 KB

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