radio-gemtek.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
  17. */
  18. #include <linux/module.h> /* Modules */
  19. #include <linux/init.h> /* Initdata */
  20. #include <linux/ioport.h> /* request_region */
  21. #include <linux/delay.h> /* udelay */
  22. #include <asm/io.h> /* outb, outb_p */
  23. #include <asm/uaccess.h> /* copy to/from user */
  24. #include <linux/videodev2.h> /* kernel radio structs */
  25. #include <media/v4l2-common.h>
  26. #include <linux/spinlock.h>
  27. #include <linux/version.h> /* for KERNEL_VERSION MACRO */
  28. #define RADIO_VERSION KERNEL_VERSION(0,0,2)
  29. static struct v4l2_queryctrl radio_qctrl[] = {
  30. {
  31. .id = V4L2_CID_AUDIO_MUTE,
  32. .name = "Mute",
  33. .minimum = 0,
  34. .maximum = 1,
  35. .default_value = 1,
  36. .type = V4L2_CTRL_TYPE_BOOLEAN,
  37. },{
  38. .id = V4L2_CID_AUDIO_VOLUME,
  39. .name = "Volume",
  40. .minimum = 0,
  41. .maximum = 65535,
  42. .step = 65535,
  43. .default_value = 0xff,
  44. .type = V4L2_CTRL_TYPE_INTEGER,
  45. }
  46. };
  47. #ifndef CONFIG_RADIO_GEMTEK_PORT
  48. #define CONFIG_RADIO_GEMTEK_PORT -1
  49. #endif
  50. static int io = CONFIG_RADIO_GEMTEK_PORT;
  51. static int radio_nr = -1;
  52. static spinlock_t lock;
  53. struct gemtek_device
  54. {
  55. int port;
  56. unsigned long curfreq;
  57. int muted;
  58. };
  59. /* local things */
  60. /* the correct way to mute the gemtek may be to write the last written
  61. * frequency || 0x10, but just writing 0x10 once seems to do it as well
  62. */
  63. static void gemtek_mute(struct gemtek_device *dev)
  64. {
  65. if(dev->muted)
  66. return;
  67. spin_lock(&lock);
  68. outb(0x10, io);
  69. spin_unlock(&lock);
  70. dev->muted = 1;
  71. }
  72. static void gemtek_unmute(struct gemtek_device *dev)
  73. {
  74. if(dev->muted == 0)
  75. return;
  76. spin_lock(&lock);
  77. outb(0x20, io);
  78. spin_unlock(&lock);
  79. dev->muted = 0;
  80. }
  81. static void zero(void)
  82. {
  83. outb_p(0x04, io);
  84. udelay(5);
  85. outb_p(0x05, io);
  86. udelay(5);
  87. }
  88. static void one(void)
  89. {
  90. outb_p(0x06, io);
  91. udelay(5);
  92. outb_p(0x07, io);
  93. udelay(5);
  94. }
  95. static int gemtek_setfreq(struct gemtek_device *dev, unsigned long freq)
  96. {
  97. int i;
  98. /* freq = 78.25*((float)freq/16000.0 + 10.52); */
  99. freq /= 16;
  100. freq += 10520;
  101. freq *= 7825;
  102. freq /= 100000;
  103. spin_lock(&lock);
  104. /* 2 start bits */
  105. outb_p(0x03, io);
  106. udelay(5);
  107. outb_p(0x07, io);
  108. udelay(5);
  109. /* 28 frequency bits (lsb first) */
  110. for (i = 0; i < 14; i++)
  111. if (freq & (1 << i))
  112. one();
  113. else
  114. zero();
  115. /* 36 unknown bits */
  116. for (i = 0; i < 11; i++)
  117. zero();
  118. one();
  119. for (i = 0; i < 4; i++)
  120. zero();
  121. one();
  122. zero();
  123. /* 2 end bits */
  124. outb_p(0x03, io);
  125. udelay(5);
  126. outb_p(0x07, io);
  127. udelay(5);
  128. spin_unlock(&lock);
  129. return 0;
  130. }
  131. static int gemtek_getsigstr(struct gemtek_device *dev)
  132. {
  133. spin_lock(&lock);
  134. inb(io);
  135. udelay(5);
  136. spin_unlock(&lock);
  137. if (inb(io) & 8) /* bit set = no signal present */
  138. return 0;
  139. return 1; /* signal present */
  140. }
  141. static int gemtek_do_ioctl(struct inode *inode, struct file *file,
  142. unsigned int cmd, void *arg)
  143. {
  144. struct video_device *dev = video_devdata(file);
  145. struct gemtek_device *rt=dev->priv;
  146. switch(cmd)
  147. {
  148. case VIDIOC_QUERYCAP:
  149. {
  150. struct v4l2_capability *v = arg;
  151. memset(v,0,sizeof(*v));
  152. strlcpy(v->driver, "radio-gemtek", sizeof (v->driver));
  153. strlcpy(v->card, "GemTek", sizeof (v->card));
  154. sprintf(v->bus_info,"ISA");
  155. v->version = RADIO_VERSION;
  156. v->capabilities = V4L2_CAP_TUNER;
  157. return 0;
  158. }
  159. case VIDIOC_G_TUNER:
  160. {
  161. struct v4l2_tuner *v = arg;
  162. if (v->index > 0)
  163. return -EINVAL;
  164. memset(v,0,sizeof(*v));
  165. strcpy(v->name, "FM");
  166. v->type = V4L2_TUNER_RADIO;
  167. v->rangelow=(87*16000);
  168. v->rangehigh=(108*16000);
  169. v->rxsubchans =V4L2_TUNER_SUB_MONO;
  170. v->capability=V4L2_TUNER_CAP_LOW;
  171. v->audmode = V4L2_TUNER_MODE_MONO;
  172. v->signal=0xFFFF*gemtek_getsigstr(rt);
  173. return 0;
  174. }
  175. case VIDIOC_S_TUNER:
  176. {
  177. struct v4l2_tuner *v = arg;
  178. if (v->index > 0)
  179. return -EINVAL;
  180. return 0;
  181. }
  182. case VIDIOC_S_FREQUENCY:
  183. {
  184. struct v4l2_frequency *f = arg;
  185. rt->curfreq = f->frequency;
  186. /* needs to be called twice in order for getsigstr to work */
  187. gemtek_setfreq(rt, rt->curfreq);
  188. gemtek_setfreq(rt, rt->curfreq);
  189. return 0;
  190. }
  191. case VIDIOC_G_FREQUENCY:
  192. {
  193. struct v4l2_frequency *f = arg;
  194. f->type = V4L2_TUNER_RADIO;
  195. f->frequency = rt->curfreq;
  196. return 0;
  197. }
  198. case VIDIOC_QUERYCTRL:
  199. {
  200. struct v4l2_queryctrl *qc = arg;
  201. int i;
  202. for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
  203. if (qc->id && qc->id == radio_qctrl[i].id) {
  204. memcpy(qc, &(radio_qctrl[i]),
  205. sizeof(*qc));
  206. return (0);
  207. }
  208. }
  209. return -EINVAL;
  210. }
  211. case VIDIOC_G_CTRL:
  212. {
  213. struct v4l2_control *ctrl= arg;
  214. switch (ctrl->id) {
  215. case V4L2_CID_AUDIO_MUTE:
  216. ctrl->value=rt->muted;
  217. return (0);
  218. case V4L2_CID_AUDIO_VOLUME:
  219. if (rt->muted)
  220. ctrl->value=0;
  221. else
  222. ctrl->value=65535;
  223. return (0);
  224. }
  225. return -EINVAL;
  226. }
  227. case VIDIOC_S_CTRL:
  228. {
  229. struct v4l2_control *ctrl= arg;
  230. switch (ctrl->id) {
  231. case V4L2_CID_AUDIO_MUTE:
  232. if (ctrl->value) {
  233. gemtek_mute(rt);
  234. } else {
  235. gemtek_unmute(rt);
  236. }
  237. return (0);
  238. case V4L2_CID_AUDIO_VOLUME:
  239. if (ctrl->value) {
  240. gemtek_unmute(rt);
  241. } else {
  242. gemtek_mute(rt);
  243. }
  244. return (0);
  245. }
  246. return -EINVAL;
  247. }
  248. default:
  249. return v4l_compat_translate_ioctl(inode,file,cmd,arg,
  250. gemtek_do_ioctl);
  251. }
  252. }
  253. static int gemtek_ioctl(struct inode *inode, struct file *file,
  254. unsigned int cmd, unsigned long arg)
  255. {
  256. return video_usercopy(inode, file, cmd, arg, gemtek_do_ioctl);
  257. }
  258. static struct gemtek_device gemtek_unit;
  259. static struct file_operations gemtek_fops = {
  260. .owner = THIS_MODULE,
  261. .open = video_exclusive_open,
  262. .release = video_exclusive_release,
  263. .ioctl = gemtek_ioctl,
  264. .compat_ioctl = v4l_compat_ioctl32,
  265. .llseek = no_llseek,
  266. };
  267. static struct video_device gemtek_radio=
  268. {
  269. .owner = THIS_MODULE,
  270. .name = "GemTek radio",
  271. .type = VID_TYPE_TUNER,
  272. .hardware = 0,
  273. .fops = &gemtek_fops,
  274. };
  275. static int __init gemtek_init(void)
  276. {
  277. if(io==-1)
  278. {
  279. 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");
  280. return -EINVAL;
  281. }
  282. if (!request_region(io, 4, "gemtek"))
  283. {
  284. printk(KERN_ERR "gemtek: port 0x%x already in use\n", io);
  285. return -EBUSY;
  286. }
  287. gemtek_radio.priv=&gemtek_unit;
  288. if(video_register_device(&gemtek_radio, VFL_TYPE_RADIO, radio_nr)==-1)
  289. {
  290. release_region(io, 4);
  291. return -EINVAL;
  292. }
  293. printk(KERN_INFO "GemTek Radio Card driver.\n");
  294. spin_lock_init(&lock);
  295. /* this is _maybe_ unnecessary */
  296. outb(0x01, io);
  297. /* mute card - prevents noisy bootups */
  298. gemtek_unit.muted = 0;
  299. gemtek_mute(&gemtek_unit);
  300. return 0;
  301. }
  302. MODULE_AUTHOR("Jonas Munsin");
  303. MODULE_DESCRIPTION("A driver for the GemTek Radio Card");
  304. MODULE_LICENSE("GPL");
  305. module_param(io, int, 0);
  306. 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)).");
  307. module_param(radio_nr, int, 0);
  308. static void __exit gemtek_cleanup(void)
  309. {
  310. video_unregister_device(&gemtek_radio);
  311. release_region(io,4);
  312. }
  313. module_init(gemtek_init);
  314. module_exit(gemtek_cleanup);
  315. /*
  316. Local variables:
  317. 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"
  318. End:
  319. */