radio-gemtek.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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 <media/v4l2-common.h>
  25. #include <linux/config.h> /* CONFIG_RADIO_GEMTEK_PORT */
  26. #include <linux/spinlock.h>
  27. #ifndef CONFIG_RADIO_GEMTEK_PORT
  28. #define CONFIG_RADIO_GEMTEK_PORT -1
  29. #endif
  30. static int io = CONFIG_RADIO_GEMTEK_PORT;
  31. static int radio_nr = -1;
  32. static spinlock_t lock;
  33. struct gemtek_device
  34. {
  35. int port;
  36. unsigned long curfreq;
  37. int muted;
  38. };
  39. /* local things */
  40. /* the correct way to mute the gemtek may be to write the last written
  41. * frequency || 0x10, but just writing 0x10 once seems to do it as well
  42. */
  43. static void gemtek_mute(struct gemtek_device *dev)
  44. {
  45. if(dev->muted)
  46. return;
  47. spin_lock(&lock);
  48. outb(0x10, io);
  49. spin_unlock(&lock);
  50. dev->muted = 1;
  51. }
  52. static void gemtek_unmute(struct gemtek_device *dev)
  53. {
  54. if(dev->muted == 0)
  55. return;
  56. spin_lock(&lock);
  57. outb(0x20, io);
  58. spin_unlock(&lock);
  59. dev->muted = 0;
  60. }
  61. static void zero(void)
  62. {
  63. outb_p(0x04, io);
  64. udelay(5);
  65. outb_p(0x05, io);
  66. udelay(5);
  67. }
  68. static void one(void)
  69. {
  70. outb_p(0x06, io);
  71. udelay(5);
  72. outb_p(0x07, io);
  73. udelay(5);
  74. }
  75. static int gemtek_setfreq(struct gemtek_device *dev, unsigned long freq)
  76. {
  77. int i;
  78. /* freq = 78.25*((float)freq/16000.0 + 10.52); */
  79. freq /= 16;
  80. freq += 10520;
  81. freq *= 7825;
  82. freq /= 100000;
  83. spin_lock(&lock);
  84. /* 2 start bits */
  85. outb_p(0x03, io);
  86. udelay(5);
  87. outb_p(0x07, io);
  88. udelay(5);
  89. /* 28 frequency bits (lsb first) */
  90. for (i = 0; i < 14; i++)
  91. if (freq & (1 << i))
  92. one();
  93. else
  94. zero();
  95. /* 36 unknown bits */
  96. for (i = 0; i < 11; i++)
  97. zero();
  98. one();
  99. for (i = 0; i < 4; i++)
  100. zero();
  101. one();
  102. zero();
  103. /* 2 end bits */
  104. outb_p(0x03, io);
  105. udelay(5);
  106. outb_p(0x07, io);
  107. udelay(5);
  108. spin_unlock(&lock);
  109. return 0;
  110. }
  111. static int gemtek_getsigstr(struct gemtek_device *dev)
  112. {
  113. spin_lock(&lock);
  114. inb(io);
  115. udelay(5);
  116. spin_unlock(&lock);
  117. if (inb(io) & 8) /* bit set = no signal present */
  118. return 0;
  119. return 1; /* signal present */
  120. }
  121. static int gemtek_do_ioctl(struct inode *inode, struct file *file,
  122. unsigned int cmd, void *arg)
  123. {
  124. struct video_device *dev = video_devdata(file);
  125. struct gemtek_device *rt=dev->priv;
  126. switch(cmd)
  127. {
  128. case VIDIOCGCAP:
  129. {
  130. struct video_capability *v = arg;
  131. memset(v,0,sizeof(*v));
  132. v->type=VID_TYPE_TUNER;
  133. v->channels=1;
  134. v->audios=1;
  135. strcpy(v->name, "GemTek");
  136. return 0;
  137. }
  138. case VIDIOCGTUNER:
  139. {
  140. struct video_tuner *v = arg;
  141. if(v->tuner) /* Only 1 tuner */
  142. return -EINVAL;
  143. v->rangelow=87*16000;
  144. v->rangehigh=108*16000;
  145. v->flags=VIDEO_TUNER_LOW;
  146. v->mode=VIDEO_MODE_AUTO;
  147. v->signal=0xFFFF*gemtek_getsigstr(rt);
  148. strcpy(v->name, "FM");
  149. return 0;
  150. }
  151. case VIDIOCSTUNER:
  152. {
  153. struct video_tuner *v = arg;
  154. if(v->tuner!=0)
  155. return -EINVAL;
  156. /* Only 1 tuner so no setting needed ! */
  157. return 0;
  158. }
  159. case VIDIOCGFREQ:
  160. {
  161. unsigned long *freq = arg;
  162. *freq = rt->curfreq;
  163. return 0;
  164. }
  165. case VIDIOCSFREQ:
  166. {
  167. unsigned long *freq = arg;
  168. rt->curfreq = *freq;
  169. /* needs to be called twice in order for getsigstr to work */
  170. gemtek_setfreq(rt, rt->curfreq);
  171. gemtek_setfreq(rt, rt->curfreq);
  172. return 0;
  173. }
  174. case VIDIOCGAUDIO:
  175. {
  176. struct video_audio *v = arg;
  177. memset(v,0, sizeof(*v));
  178. v->flags|=VIDEO_AUDIO_MUTABLE;
  179. v->volume=1;
  180. v->step=65535;
  181. strcpy(v->name, "Radio");
  182. return 0;
  183. }
  184. case VIDIOCSAUDIO:
  185. {
  186. struct video_audio *v = arg;
  187. if(v->audio)
  188. return -EINVAL;
  189. if(v->flags&VIDEO_AUDIO_MUTE)
  190. gemtek_mute(rt);
  191. else
  192. gemtek_unmute(rt);
  193. return 0;
  194. }
  195. default:
  196. return -ENOIOCTLCMD;
  197. }
  198. }
  199. static int gemtek_ioctl(struct inode *inode, struct file *file,
  200. unsigned int cmd, unsigned long arg)
  201. {
  202. return video_usercopy(inode, file, cmd, arg, gemtek_do_ioctl);
  203. }
  204. static struct gemtek_device gemtek_unit;
  205. static struct file_operations gemtek_fops = {
  206. .owner = THIS_MODULE,
  207. .open = video_exclusive_open,
  208. .release = video_exclusive_release,
  209. .ioctl = gemtek_ioctl,
  210. .compat_ioctl = v4l_compat_ioctl32,
  211. .llseek = no_llseek,
  212. };
  213. static struct video_device gemtek_radio=
  214. {
  215. .owner = THIS_MODULE,
  216. .name = "GemTek radio",
  217. .type = VID_TYPE_TUNER,
  218. .hardware = VID_HARDWARE_GEMTEK,
  219. .fops = &gemtek_fops,
  220. };
  221. static int __init gemtek_init(void)
  222. {
  223. if(io==-1)
  224. {
  225. 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");
  226. return -EINVAL;
  227. }
  228. if (!request_region(io, 4, "gemtek"))
  229. {
  230. printk(KERN_ERR "gemtek: port 0x%x already in use\n", io);
  231. return -EBUSY;
  232. }
  233. gemtek_radio.priv=&gemtek_unit;
  234. if(video_register_device(&gemtek_radio, VFL_TYPE_RADIO, radio_nr)==-1)
  235. {
  236. release_region(io, 4);
  237. return -EINVAL;
  238. }
  239. printk(KERN_INFO "GemTek Radio Card driver.\n");
  240. spin_lock_init(&lock);
  241. /* this is _maybe_ unnecessary */
  242. outb(0x01, io);
  243. /* mute card - prevents noisy bootups */
  244. gemtek_unit.muted = 0;
  245. gemtek_mute(&gemtek_unit);
  246. return 0;
  247. }
  248. MODULE_AUTHOR("Jonas Munsin");
  249. MODULE_DESCRIPTION("A driver for the GemTek Radio Card");
  250. MODULE_LICENSE("GPL");
  251. module_param(io, int, 0);
  252. 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)).");
  253. module_param(radio_nr, int, 0);
  254. static void __exit gemtek_cleanup(void)
  255. {
  256. video_unregister_device(&gemtek_radio);
  257. release_region(io,4);
  258. }
  259. module_init(gemtek_init);
  260. module_exit(gemtek_cleanup);
  261. /*
  262. Local variables:
  263. 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"
  264. End:
  265. */