radio-aimslab.c 8.8 KB

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