radio-rtrack2.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /* RadioTrack II driver for Linux radio support (C) 1998 Ben Pfaff
  2. *
  3. * Based on RadioTrack I/RadioReveal (C) 1997 M. Kirkwood
  4. * Converted to new API by Alan Cox <Alan.Cox@linux.org>
  5. * Various bugfixes and enhancements by Russell Kroll <rkroll@exploits.org>
  6. *
  7. * TODO: Allow for more than one of these foolish entities :-)
  8. *
  9. */
  10. #include <linux/module.h> /* Modules */
  11. #include <linux/init.h> /* Initdata */
  12. #include <linux/ioport.h> /* request_region */
  13. #include <linux/delay.h> /* udelay */
  14. #include <asm/io.h> /* outb, outb_p */
  15. #include <asm/uaccess.h> /* copy to/from user */
  16. #include <linux/videodev.h> /* kernel radio structs */
  17. #include <linux/config.h> /* CONFIG_RADIO_RTRACK2_PORT */
  18. #include <linux/spinlock.h>
  19. #ifndef CONFIG_RADIO_RTRACK2_PORT
  20. #define CONFIG_RADIO_RTRACK2_PORT -1
  21. #endif
  22. static int io = CONFIG_RADIO_RTRACK2_PORT;
  23. static int radio_nr = -1;
  24. static spinlock_t lock;
  25. struct rt_device
  26. {
  27. int port;
  28. unsigned long curfreq;
  29. int muted;
  30. };
  31. /* local things */
  32. static void rt_mute(struct rt_device *dev)
  33. {
  34. if(dev->muted)
  35. return;
  36. spin_lock(&lock);
  37. outb(1, io);
  38. spin_unlock(&lock);
  39. dev->muted = 1;
  40. }
  41. static void rt_unmute(struct rt_device *dev)
  42. {
  43. if(dev->muted == 0)
  44. return;
  45. spin_lock(&lock);
  46. outb(0, io);
  47. spin_unlock(&lock);
  48. dev->muted = 0;
  49. }
  50. static void zero(void)
  51. {
  52. outb_p(1, io);
  53. outb_p(3, io);
  54. outb_p(1, io);
  55. }
  56. static void one(void)
  57. {
  58. outb_p(5, io);
  59. outb_p(7, io);
  60. outb_p(5, io);
  61. }
  62. static int rt_setfreq(struct rt_device *dev, unsigned long freq)
  63. {
  64. int i;
  65. freq = freq / 200 + 856;
  66. spin_lock(&lock);
  67. outb_p(0xc8, io);
  68. outb_p(0xc9, io);
  69. outb_p(0xc9, io);
  70. for (i = 0; i < 10; i++)
  71. zero ();
  72. for (i = 14; i >= 0; i--)
  73. if (freq & (1 << i))
  74. one ();
  75. else
  76. zero ();
  77. outb_p(0xc8, io);
  78. if (!dev->muted)
  79. outb_p(0, io);
  80. spin_unlock(&lock);
  81. return 0;
  82. }
  83. static int rt_getsigstr(struct rt_device *dev)
  84. {
  85. if (inb(io) & 2) /* bit set = no signal present */
  86. return 0;
  87. return 1; /* signal present */
  88. }
  89. static int rt_do_ioctl(struct inode *inode, struct file *file,
  90. unsigned int cmd, void *arg)
  91. {
  92. struct video_device *dev = video_devdata(file);
  93. struct rt_device *rt=dev->priv;
  94. switch(cmd)
  95. {
  96. case VIDIOCGCAP:
  97. {
  98. struct video_capability *v = arg;
  99. memset(v,0,sizeof(*v));
  100. v->type=VID_TYPE_TUNER;
  101. v->channels=1;
  102. v->audios=1;
  103. strcpy(v->name, "RadioTrack II");
  104. return 0;
  105. }
  106. case VIDIOCGTUNER:
  107. {
  108. struct video_tuner *v = arg;
  109. if(v->tuner) /* Only 1 tuner */
  110. return -EINVAL;
  111. v->rangelow=88*16000;
  112. v->rangehigh=108*16000;
  113. v->flags=VIDEO_TUNER_LOW;
  114. v->mode=VIDEO_MODE_AUTO;
  115. v->signal=0xFFFF*rt_getsigstr(rt);
  116. strcpy(v->name, "FM");
  117. return 0;
  118. }
  119. case VIDIOCSTUNER:
  120. {
  121. struct video_tuner *v = arg;
  122. if(v->tuner!=0)
  123. return -EINVAL;
  124. /* Only 1 tuner so no setting needed ! */
  125. return 0;
  126. }
  127. case VIDIOCGFREQ:
  128. {
  129. unsigned long *freq = arg;
  130. *freq = rt->curfreq;
  131. return 0;
  132. }
  133. case VIDIOCSFREQ:
  134. {
  135. unsigned long *freq = arg;
  136. rt->curfreq = *freq;
  137. rt_setfreq(rt, rt->curfreq);
  138. return 0;
  139. }
  140. case VIDIOCGAUDIO:
  141. {
  142. struct video_audio *v = arg;
  143. memset(v,0, sizeof(*v));
  144. v->flags|=VIDEO_AUDIO_MUTABLE;
  145. v->volume=1;
  146. v->step=65535;
  147. strcpy(v->name, "Radio");
  148. return 0;
  149. }
  150. case VIDIOCSAUDIO:
  151. {
  152. struct video_audio *v = arg;
  153. if(v->audio)
  154. return -EINVAL;
  155. if(v->flags&VIDEO_AUDIO_MUTE)
  156. rt_mute(rt);
  157. else
  158. rt_unmute(rt);
  159. return 0;
  160. }
  161. default:
  162. return -ENOIOCTLCMD;
  163. }
  164. }
  165. static int rt_ioctl(struct inode *inode, struct file *file,
  166. unsigned int cmd, unsigned long arg)
  167. {
  168. return video_usercopy(inode, file, cmd, arg, rt_do_ioctl);
  169. }
  170. static struct rt_device rtrack2_unit;
  171. static struct file_operations rtrack2_fops = {
  172. .owner = THIS_MODULE,
  173. .open = video_exclusive_open,
  174. .release = video_exclusive_release,
  175. .ioctl = rt_ioctl,
  176. .compat_ioctl = v4l_compat_ioctl32,
  177. .llseek = no_llseek,
  178. };
  179. static struct video_device rtrack2_radio=
  180. {
  181. .owner = THIS_MODULE,
  182. .name = "RadioTrack II radio",
  183. .type = VID_TYPE_TUNER,
  184. .hardware = VID_HARDWARE_RTRACK2,
  185. .fops = &rtrack2_fops,
  186. };
  187. static int __init rtrack2_init(void)
  188. {
  189. if(io==-1)
  190. {
  191. printk(KERN_ERR "You must set an I/O address with io=0x20c or io=0x30c\n");
  192. return -EINVAL;
  193. }
  194. if (!request_region(io, 4, "rtrack2"))
  195. {
  196. printk(KERN_ERR "rtrack2: port 0x%x already in use\n", io);
  197. return -EBUSY;
  198. }
  199. rtrack2_radio.priv=&rtrack2_unit;
  200. spin_lock_init(&lock);
  201. if(video_register_device(&rtrack2_radio, VFL_TYPE_RADIO, radio_nr)==-1)
  202. {
  203. release_region(io, 4);
  204. return -EINVAL;
  205. }
  206. printk(KERN_INFO "AIMSlab Radiotrack II card driver.\n");
  207. /* mute card - prevents noisy bootups */
  208. outb(1, io);
  209. rtrack2_unit.muted = 1;
  210. return 0;
  211. }
  212. MODULE_AUTHOR("Ben Pfaff");
  213. MODULE_DESCRIPTION("A driver for the RadioTrack II radio card.");
  214. MODULE_LICENSE("GPL");
  215. module_param(io, int, 0);
  216. MODULE_PARM_DESC(io, "I/O address of the RadioTrack card (0x20c or 0x30c)");
  217. module_param(radio_nr, int, 0);
  218. static void __exit rtrack2_cleanup_module(void)
  219. {
  220. video_unregister_device(&rtrack2_radio);
  221. release_region(io,4);
  222. }
  223. module_init(rtrack2_init);
  224. module_exit(rtrack2_cleanup_module);
  225. /*
  226. Local variables:
  227. compile-command: "mmake"
  228. End:
  229. */