radio-aimslab.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /* radiotrack (radioreveal) driver for Linux radio support
  2. * (c) 1997 M. Kirkwood
  3. * Converted to new API by Alan Cox <Alan.Cox@linux.org>
  4. * Various bugfixes and enhancements by Russell Kroll <rkroll@exploits.org>
  5. *
  6. * History:
  7. * 1999-02-24 Russell Kroll <rkroll@exploits.org>
  8. * Fine tuning/VIDEO_TUNER_LOW
  9. * Frequency range expanded to start at 87 MHz
  10. *
  11. * TODO: Allow for more than one of these foolish entities :-)
  12. *
  13. * Notes on the hardware (reverse engineered from other peoples'
  14. * reverse engineering of AIMS' code :-)
  15. *
  16. * Frequency control is done digitally -- ie out(port,encodefreq(95.8));
  17. *
  18. * The signal strength query is unsurprisingly inaccurate. And it seems
  19. * to indicate that (on my card, at least) the frequency setting isn't
  20. * too great. (I have to tune up .025MHz from what the freq should be
  21. * to get a report that the thing is tuned.)
  22. *
  23. * Volume control is (ugh) analogue:
  24. * out(port, start_increasing_volume);
  25. * wait(a_wee_while);
  26. * out(port, stop_changing_the_volume);
  27. *
  28. */
  29. #include <linux/module.h> /* Modules */
  30. #include <linux/init.h> /* Initdata */
  31. #include <linux/ioport.h> /* request_region */
  32. #include <linux/delay.h> /* udelay */
  33. #include <asm/io.h> /* outb, outb_p */
  34. #include <asm/uaccess.h> /* copy to/from user */
  35. #include <linux/videodev.h> /* kernel radio structs */
  36. #include <linux/config.h> /* CONFIG_RADIO_RTRACK_PORT */
  37. #include <asm/semaphore.h> /* Lock for the I/O */
  38. #ifndef CONFIG_RADIO_RTRACK_PORT
  39. #define CONFIG_RADIO_RTRACK_PORT -1
  40. #endif
  41. static int io = CONFIG_RADIO_RTRACK_PORT;
  42. static int radio_nr = -1;
  43. static struct mutex lock;
  44. struct rt_device
  45. {
  46. int port;
  47. int curvol;
  48. unsigned long curfreq;
  49. int muted;
  50. };
  51. /* local things */
  52. static void sleep_delay(long n)
  53. {
  54. /* Sleep nicely for 'n' uS */
  55. int d=n/(1000000/HZ);
  56. if(!d)
  57. udelay(n);
  58. else
  59. msleep(jiffies_to_msecs(d));
  60. }
  61. static void rt_decvol(void)
  62. {
  63. outb(0x58, io); /* volume down + sigstr + on */
  64. sleep_delay(100000);
  65. outb(0xd8, io); /* volume steady + sigstr + on */
  66. }
  67. static void rt_incvol(void)
  68. {
  69. outb(0x98, io); /* volume up + sigstr + on */
  70. sleep_delay(100000);
  71. outb(0xd8, io); /* volume steady + sigstr + on */
  72. }
  73. static void rt_mute(struct rt_device *dev)
  74. {
  75. dev->muted = 1;
  76. mutex_lock(&lock);
  77. outb(0xd0, io); /* volume steady, off */
  78. mutex_unlock(&lock);
  79. }
  80. static int rt_setvol(struct rt_device *dev, int vol)
  81. {
  82. int i;
  83. mutex_lock(&lock);
  84. if(vol == dev->curvol) { /* requested volume = current */
  85. if (dev->muted) { /* user is unmuting the card */
  86. dev->muted = 0;
  87. outb (0xd8, io); /* enable card */
  88. }
  89. mutex_unlock(&lock);
  90. return 0;
  91. }
  92. if(vol == 0) { /* volume = 0 means mute the card */
  93. outb(0x48, io); /* volume down but still "on" */
  94. sleep_delay(2000000); /* make sure it's totally down */
  95. outb(0xd0, io); /* volume steady, off */
  96. dev->curvol = 0; /* track the volume state! */
  97. mutex_unlock(&lock);
  98. return 0;
  99. }
  100. dev->muted = 0;
  101. if(vol > dev->curvol)
  102. for(i = dev->curvol; i < vol; i++)
  103. rt_incvol();
  104. else
  105. for(i = dev->curvol; i > vol; i--)
  106. rt_decvol();
  107. dev->curvol = vol;
  108. mutex_unlock(&lock);
  109. return 0;
  110. }
  111. /* the 128+64 on these outb's is to keep the volume stable while tuning
  112. * without them, the volume _will_ creep up with each frequency change
  113. * and bit 4 (+16) is to keep the signal strength meter enabled
  114. */
  115. static void send_0_byte(int port, struct rt_device *dev)
  116. {
  117. if ((dev->curvol == 0) || (dev->muted)) {
  118. outb_p(128+64+16+ 1, port); /* wr-enable + data low */
  119. outb_p(128+64+16+2+1, port); /* clock */
  120. }
  121. else {
  122. outb_p(128+64+16+8+ 1, port); /* on + wr-enable + data low */
  123. outb_p(128+64+16+8+2+1, port); /* clock */
  124. }
  125. sleep_delay(1000);
  126. }
  127. static void send_1_byte(int port, struct rt_device *dev)
  128. {
  129. if ((dev->curvol == 0) || (dev->muted)) {
  130. outb_p(128+64+16+4 +1, port); /* wr-enable+data high */
  131. outb_p(128+64+16+4+2+1, port); /* clock */
  132. }
  133. else {
  134. outb_p(128+64+16+8+4 +1, port); /* on+wr-enable+data high */
  135. outb_p(128+64+16+8+4+2+1, port); /* clock */
  136. }
  137. sleep_delay(1000);
  138. }
  139. static int rt_setfreq(struct rt_device *dev, unsigned long freq)
  140. {
  141. int i;
  142. /* adapted from radio-aztech.c */
  143. /* now uses VIDEO_TUNER_LOW for fine tuning */
  144. freq += 171200; /* Add 10.7 MHz IF */
  145. freq /= 800; /* Convert to 50 kHz units */
  146. mutex_lock(&lock); /* Stop other ops interfering */
  147. send_0_byte (io, dev); /* 0: LSB of frequency */
  148. for (i = 0; i < 13; i++) /* : frequency bits (1-13) */
  149. if (freq & (1 << i))
  150. send_1_byte (io, dev);
  151. else
  152. send_0_byte (io, dev);
  153. send_0_byte (io, dev); /* 14: test bit - always 0 */
  154. send_0_byte (io, dev); /* 15: test bit - always 0 */
  155. send_0_byte (io, dev); /* 16: band data 0 - always 0 */
  156. send_0_byte (io, dev); /* 17: band data 1 - always 0 */
  157. send_0_byte (io, dev); /* 18: band data 2 - always 0 */
  158. send_0_byte (io, dev); /* 19: time base - always 0 */
  159. send_0_byte (io, dev); /* 20: spacing (0 = 25 kHz) */
  160. send_1_byte (io, dev); /* 21: spacing (1 = 25 kHz) */
  161. send_0_byte (io, dev); /* 22: spacing (0 = 25 kHz) */
  162. send_1_byte (io, dev); /* 23: AM/FM (FM = 1, always) */
  163. if ((dev->curvol == 0) || (dev->muted))
  164. outb (0xd0, io); /* volume steady + sigstr */
  165. else
  166. outb (0xd8, io); /* volume steady + sigstr + on */
  167. mutex_unlock(&lock);
  168. return 0;
  169. }
  170. static int rt_getsigstr(struct rt_device *dev)
  171. {
  172. if (inb(io) & 2) /* bit set = no signal present */
  173. return 0;
  174. return 1; /* signal present */
  175. }
  176. static int rt_do_ioctl(struct inode *inode, struct file *file,
  177. unsigned int cmd, void *arg)
  178. {
  179. struct video_device *dev = video_devdata(file);
  180. struct rt_device *rt=dev->priv;
  181. switch(cmd)
  182. {
  183. case VIDIOCGCAP:
  184. {
  185. struct video_capability *v = arg;
  186. memset(v,0,sizeof(*v));
  187. v->type=VID_TYPE_TUNER;
  188. v->channels=1;
  189. v->audios=1;
  190. strcpy(v->name, "RadioTrack");
  191. return 0;
  192. }
  193. case VIDIOCGTUNER:
  194. {
  195. struct video_tuner *v = arg;
  196. if(v->tuner) /* Only 1 tuner */
  197. return -EINVAL;
  198. v->rangelow=(87*16000);
  199. v->rangehigh=(108*16000);
  200. v->flags=VIDEO_TUNER_LOW;
  201. v->mode=VIDEO_MODE_AUTO;
  202. strcpy(v->name, "FM");
  203. v->signal=0xFFFF*rt_getsigstr(rt);
  204. return 0;
  205. }
  206. case VIDIOCSTUNER:
  207. {
  208. struct video_tuner *v = arg;
  209. if(v->tuner!=0)
  210. return -EINVAL;
  211. /* Only 1 tuner so no setting needed ! */
  212. return 0;
  213. }
  214. case VIDIOCGFREQ:
  215. {
  216. unsigned long *freq = arg;
  217. *freq = rt->curfreq;
  218. return 0;
  219. }
  220. case VIDIOCSFREQ:
  221. {
  222. unsigned long *freq = arg;
  223. rt->curfreq = *freq;
  224. rt_setfreq(rt, rt->curfreq);
  225. return 0;
  226. }
  227. case VIDIOCGAUDIO:
  228. {
  229. struct video_audio *v = arg;
  230. memset(v,0, sizeof(*v));
  231. v->flags|=VIDEO_AUDIO_MUTABLE|VIDEO_AUDIO_VOLUME;
  232. v->volume=rt->curvol * 6554;
  233. v->step=6554;
  234. strcpy(v->name, "Radio");
  235. return 0;
  236. }
  237. case VIDIOCSAUDIO:
  238. {
  239. struct video_audio *v = arg;
  240. if(v->audio)
  241. return -EINVAL;
  242. if(v->flags&VIDEO_AUDIO_MUTE)
  243. rt_mute(rt);
  244. else
  245. rt_setvol(rt,v->volume/6554);
  246. return 0;
  247. }
  248. default:
  249. return -ENOIOCTLCMD;
  250. }
  251. }
  252. static int rt_ioctl(struct inode *inode, struct file *file,
  253. unsigned int cmd, unsigned long arg)
  254. {
  255. return video_usercopy(inode, file, cmd, arg, rt_do_ioctl);
  256. }
  257. static struct rt_device rtrack_unit;
  258. static struct file_operations rtrack_fops = {
  259. .owner = THIS_MODULE,
  260. .open = video_exclusive_open,
  261. .release = video_exclusive_release,
  262. .ioctl = rt_ioctl,
  263. .compat_ioctl = v4l_compat_ioctl32,
  264. .llseek = no_llseek,
  265. };
  266. static struct video_device rtrack_radio=
  267. {
  268. .owner = THIS_MODULE,
  269. .name = "RadioTrack radio",
  270. .type = VID_TYPE_TUNER,
  271. .hardware = VID_HARDWARE_RTRACK,
  272. .fops = &rtrack_fops,
  273. };
  274. static int __init rtrack_init(void)
  275. {
  276. if(io==-1)
  277. {
  278. printk(KERN_ERR "You must set an I/O address with io=0x???\n");
  279. return -EINVAL;
  280. }
  281. if (!request_region(io, 2, "rtrack"))
  282. {
  283. printk(KERN_ERR "rtrack: port 0x%x already in use\n", io);
  284. return -EBUSY;
  285. }
  286. rtrack_radio.priv=&rtrack_unit;
  287. if(video_register_device(&rtrack_radio, VFL_TYPE_RADIO, radio_nr)==-1)
  288. {
  289. release_region(io, 2);
  290. return -EINVAL;
  291. }
  292. printk(KERN_INFO "AIMSlab RadioTrack/RadioReveal card driver.\n");
  293. /* Set up the I/O locking */
  294. mutex_init(&lock);
  295. /* mute card - prevents noisy bootups */
  296. /* this ensures that the volume is all the way down */
  297. outb(0x48, io); /* volume down but still "on" */
  298. sleep_delay(2000000); /* make sure it's totally down */
  299. outb(0xc0, io); /* steady volume, mute card */
  300. rtrack_unit.curvol = 0;
  301. return 0;
  302. }
  303. MODULE_AUTHOR("M.Kirkwood");
  304. MODULE_DESCRIPTION("A driver for the RadioTrack/RadioReveal radio card.");
  305. MODULE_LICENSE("GPL");
  306. module_param(io, int, 0);
  307. MODULE_PARM_DESC(io, "I/O address of the RadioTrack card (0x20f or 0x30f)");
  308. module_param(radio_nr, int, 0);
  309. static void __exit cleanup_rtrack_module(void)
  310. {
  311. video_unregister_device(&rtrack_radio);
  312. release_region(io,2);
  313. }
  314. module_init(rtrack_init);
  315. module_exit(cleanup_rtrack_module);