radio-gemtek.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /* GemTek radio card driver for Linux (C) 1998 Jonas Munsin <jmunsin@iki.fi>
  2. *
  3. * GemTek hasn't released any specs on the card, so the protocol had to
  4. * be reverse engineered with dosemu.
  5. *
  6. * Besides the protocol changes, this is mostly a copy of:
  7. *
  8. * RadioTrack II driver for Linux radio support (C) 1998 Ben Pfaff
  9. *
  10. * Based on RadioTrack I/RadioReveal (C) 1997 M. Kirkwood
  11. * Converted to new API by Alan Cox <Alan.Cox@linux.org>
  12. * Various bugfixes and enhancements by Russell Kroll <rkroll@exploits.org>
  13. *
  14. * TODO: Allow for more than one of these foolish entities :-)
  15. *
  16. */
  17. #include <linux/module.h> /* Modules */
  18. #include <linux/init.h> /* Initdata */
  19. #include <linux/ioport.h> /* request_region */
  20. #include <linux/delay.h> /* udelay */
  21. #include <asm/io.h> /* outb, outb_p */
  22. #include <asm/uaccess.h> /* copy to/from user */
  23. #include <linux/videodev.h> /* kernel radio structs */
  24. #include <linux/config.h> /* CONFIG_RADIO_GEMTEK_PORT */
  25. #include <linux/spinlock.h>
  26. #ifndef CONFIG_RADIO_GEMTEK_PORT
  27. #define CONFIG_RADIO_GEMTEK_PORT -1
  28. #endif
  29. static int io = CONFIG_RADIO_GEMTEK_PORT;
  30. static int radio_nr = -1;
  31. static spinlock_t lock;
  32. struct gemtek_device
  33. {
  34. int port;
  35. unsigned long curfreq;
  36. int muted;
  37. };
  38. /* local things */
  39. /* the correct way to mute the gemtek may be to write the last written
  40. * frequency || 0x10, but just writing 0x10 once seems to do it as well
  41. */
  42. static void gemtek_mute(struct gemtek_device *dev)
  43. {
  44. if(dev->muted)
  45. return;
  46. spin_lock(&lock);
  47. outb(0x10, io);
  48. spin_unlock(&lock);
  49. dev->muted = 1;
  50. }
  51. static void gemtek_unmute(struct gemtek_device *dev)
  52. {
  53. if(dev->muted == 0)
  54. return;
  55. spin_lock(&lock);
  56. outb(0x20, io);
  57. spin_unlock(&lock);
  58. dev->muted = 0;
  59. }
  60. static void zero(void)
  61. {
  62. outb_p(0x04, io);
  63. udelay(5);
  64. outb_p(0x05, io);
  65. udelay(5);
  66. }
  67. static void one(void)
  68. {
  69. outb_p(0x06, io);
  70. udelay(5);
  71. outb_p(0x07, io);
  72. udelay(5);
  73. }
  74. static int gemtek_setfreq(struct gemtek_device *dev, unsigned long freq)
  75. {
  76. int i;
  77. /* freq = 78.25*((float)freq/16000.0 + 10.52); */
  78. freq /= 16;
  79. freq += 10520;
  80. freq *= 7825;
  81. freq /= 100000;
  82. spin_lock(&lock);
  83. /* 2 start bits */
  84. outb_p(0x03, io);
  85. udelay(5);
  86. outb_p(0x07, io);
  87. udelay(5);
  88. /* 28 frequency bits (lsb first) */
  89. for (i = 0; i < 14; i++)
  90. if (freq & (1 << i))
  91. one();
  92. else
  93. zero();
  94. /* 36 unknown bits */
  95. for (i = 0; i < 11; i++)
  96. zero();
  97. one();
  98. for (i = 0; i < 4; i++)
  99. zero();
  100. one();
  101. zero();
  102. /* 2 end bits */
  103. outb_p(0x03, io);
  104. udelay(5);
  105. outb_p(0x07, io);
  106. udelay(5);
  107. spin_unlock(&lock);
  108. return 0;
  109. }
  110. static int gemtek_getsigstr(struct gemtek_device *dev)
  111. {
  112. spin_lock(&lock);
  113. inb(io);
  114. udelay(5);
  115. spin_unlock(&lock);
  116. if (inb(io) & 8) /* bit set = no signal present */
  117. return 0;
  118. return 1; /* signal present */
  119. }
  120. static int gemtek_do_ioctl(struct inode *inode, struct file *file,
  121. unsigned int cmd, void *arg)
  122. {
  123. struct video_device *dev = video_devdata(file);
  124. struct gemtek_device *rt=dev->priv;
  125. switch(cmd)
  126. {
  127. case VIDIOCGCAP:
  128. {
  129. struct video_capability *v = arg;
  130. memset(v,0,sizeof(*v));
  131. v->type=VID_TYPE_TUNER;
  132. v->channels=1;
  133. v->audios=1;
  134. strcpy(v->name, "GemTek");
  135. return 0;
  136. }
  137. case VIDIOCGTUNER:
  138. {
  139. struct video_tuner *v = arg;
  140. if(v->tuner) /* Only 1 tuner */
  141. return -EINVAL;
  142. v->rangelow=87*16000;
  143. v->rangehigh=108*16000;
  144. v->flags=VIDEO_TUNER_LOW;
  145. v->mode=VIDEO_MODE_AUTO;
  146. v->signal=0xFFFF*gemtek_getsigstr(rt);
  147. strcpy(v->name, "FM");
  148. return 0;
  149. }
  150. case VIDIOCSTUNER:
  151. {
  152. struct video_tuner *v = arg;
  153. if(v->tuner!=0)
  154. return -EINVAL;
  155. /* Only 1 tuner so no setting needed ! */
  156. return 0;
  157. }
  158. case VIDIOCGFREQ:
  159. {
  160. unsigned long *freq = arg;
  161. *freq = rt->curfreq;
  162. return 0;
  163. }
  164. case VIDIOCSFREQ:
  165. {
  166. unsigned long *freq = arg;
  167. rt->curfreq = *freq;
  168. /* needs to be called twice in order for getsigstr to work */
  169. gemtek_setfreq(rt, rt->curfreq);
  170. gemtek_setfreq(rt, rt->curfreq);
  171. return 0;
  172. }
  173. case VIDIOCGAUDIO:
  174. {
  175. struct video_audio *v = arg;
  176. memset(v,0, sizeof(*v));
  177. v->flags|=VIDEO_AUDIO_MUTABLE;
  178. v->volume=1;
  179. v->step=65535;
  180. strcpy(v->name, "Radio");
  181. return 0;
  182. }
  183. case VIDIOCSAUDIO:
  184. {
  185. struct video_audio *v = arg;
  186. if(v->audio)
  187. return -EINVAL;
  188. if(v->flags&VIDEO_AUDIO_MUTE)
  189. gemtek_mute(rt);
  190. else
  191. gemtek_unmute(rt);
  192. return 0;
  193. }
  194. default:
  195. return -ENOIOCTLCMD;
  196. }
  197. }
  198. static int gemtek_ioctl(struct inode *inode, struct file *file,
  199. unsigned int cmd, unsigned long arg)
  200. {
  201. return video_usercopy(inode, file, cmd, arg, gemtek_do_ioctl);
  202. }
  203. static struct gemtek_device gemtek_unit;
  204. static struct file_operations gemtek_fops = {
  205. .owner = THIS_MODULE,
  206. .open = video_exclusive_open,
  207. .release = video_exclusive_release,
  208. .ioctl = gemtek_ioctl,
  209. .compat_ioctl = v4l_compat_ioctl32,
  210. .llseek = no_llseek,
  211. };
  212. static struct video_device gemtek_radio=
  213. {
  214. .owner = THIS_MODULE,
  215. .name = "GemTek radio",
  216. .type = VID_TYPE_TUNER,
  217. .hardware = VID_HARDWARE_GEMTEK,
  218. .fops = &gemtek_fops,
  219. };
  220. static int __init gemtek_init(void)
  221. {
  222. if(io==-1)
  223. {
  224. printk(KERN_ERR "You must set an I/O address with io=0x20c, io=0x30c, io=0x24c or io=0x34c (io=0x020c or io=0x248 for the combined sound/radiocard)\n");
  225. return -EINVAL;
  226. }
  227. if (!request_region(io, 4, "gemtek"))
  228. {
  229. printk(KERN_ERR "gemtek: port 0x%x already in use\n", io);
  230. return -EBUSY;
  231. }
  232. gemtek_radio.priv=&gemtek_unit;
  233. if(video_register_device(&gemtek_radio, VFL_TYPE_RADIO, radio_nr)==-1)
  234. {
  235. release_region(io, 4);
  236. return -EINVAL;
  237. }
  238. printk(KERN_INFO "GemTek Radio Card driver.\n");
  239. spin_lock_init(&lock);
  240. /* this is _maybe_ unnecessary */
  241. outb(0x01, io);
  242. /* mute card - prevents noisy bootups */
  243. gemtek_unit.muted = 0;
  244. gemtek_mute(&gemtek_unit);
  245. return 0;
  246. }
  247. MODULE_AUTHOR("Jonas Munsin");
  248. MODULE_DESCRIPTION("A driver for the GemTek Radio Card");
  249. MODULE_LICENSE("GPL");
  250. module_param(io, int, 0);
  251. MODULE_PARM_DESC(io, "I/O address of the GemTek card (0x20c, 0x30c, 0x24c or 0x34c (0x20c or 0x248 have been reported to work for the combined sound/radiocard)).");
  252. module_param(radio_nr, int, 0);
  253. static void __exit gemtek_cleanup(void)
  254. {
  255. video_unregister_device(&gemtek_radio);
  256. release_region(io,4);
  257. }
  258. module_init(gemtek_init);
  259. module_exit(gemtek_cleanup);
  260. /*
  261. Local variables:
  262. compile-command: "gcc -c -DMODVERSIONS -D__KERNEL__ -DMODULE -O6 -Wall -Wstrict-prototypes -I /home/blp/tmp/linux-2.1.111-rtrack/include radio-rtrack2.c"
  263. End:
  264. */