radio-rtrack2.c 5.2 KB

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