radio-gemtek.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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 vidioc_querycap(struct file *file, void *priv,
  142. struct v4l2_capability *v)
  143. {
  144. strlcpy(v->driver, "radio-gemtek", sizeof(v->driver));
  145. strlcpy(v->card, "GemTek", sizeof(v->card));
  146. sprintf(v->bus_info, "ISA");
  147. v->version = RADIO_VERSION;
  148. v->capabilities = V4L2_CAP_TUNER;
  149. return 0;
  150. }
  151. static int vidioc_g_tuner(struct file *file, void *priv,
  152. struct v4l2_tuner *v)
  153. {
  154. struct video_device *dev = video_devdata(file);
  155. struct gemtek_device *rt = dev->priv;
  156. if (v->index > 0)
  157. return -EINVAL;
  158. strcpy(v->name, "FM");
  159. v->type = V4L2_TUNER_RADIO;
  160. v->rangelow = (87*16000);
  161. v->rangehigh = (108*16000);
  162. v->rxsubchans = V4L2_TUNER_SUB_MONO;
  163. v->capability = V4L2_TUNER_CAP_LOW;
  164. v->audmode = V4L2_TUNER_MODE_MONO;
  165. v->signal = 0xffff*gemtek_getsigstr(rt);
  166. return 0;
  167. }
  168. static int vidioc_s_tuner(struct file *file, void *priv,
  169. struct v4l2_tuner *v)
  170. {
  171. if (v->index > 0)
  172. return -EINVAL;
  173. return 0;
  174. }
  175. static int vidioc_s_frequency(struct file *file, void *priv,
  176. struct v4l2_frequency *f)
  177. {
  178. struct video_device *dev = video_devdata(file);
  179. struct gemtek_device *rt = dev->priv;
  180. rt->curfreq = f->frequency;
  181. /* needs to be called twice in order for getsigstr to work */
  182. gemtek_setfreq(rt, rt->curfreq);
  183. gemtek_setfreq(rt, rt->curfreq);
  184. return 0;
  185. }
  186. static int vidioc_g_frequency(struct file *file, void *priv,
  187. struct v4l2_frequency *f)
  188. {
  189. struct video_device *dev = video_devdata(file);
  190. struct gemtek_device *rt = dev->priv;
  191. f->type = V4L2_TUNER_RADIO;
  192. f->frequency = rt->curfreq;
  193. return 0;
  194. }
  195. static int vidioc_queryctrl(struct file *file, void *priv,
  196. struct v4l2_queryctrl *qc)
  197. {
  198. int i;
  199. for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
  200. if (qc->id && qc->id == radio_qctrl[i].id) {
  201. memcpy(qc, &(radio_qctrl[i]),
  202. sizeof(*qc));
  203. return 0;
  204. }
  205. }
  206. return -EINVAL;
  207. }
  208. static int vidioc_g_ctrl(struct file *file, void *priv,
  209. struct v4l2_control *ctrl)
  210. {
  211. struct video_device *dev = video_devdata(file);
  212. struct gemtek_device *rt = dev->priv;
  213. switch (ctrl->id) {
  214. case V4L2_CID_AUDIO_MUTE:
  215. ctrl->value = rt->muted;
  216. return 0;
  217. case V4L2_CID_AUDIO_VOLUME:
  218. if (rt->muted)
  219. ctrl->value = 0;
  220. else
  221. ctrl->value = 65535;
  222. return 0;
  223. }
  224. return -EINVAL;
  225. }
  226. static int vidioc_s_ctrl(struct file *file, void *priv,
  227. struct v4l2_control *ctrl)
  228. {
  229. struct video_device *dev = video_devdata(file);
  230. struct gemtek_device *rt = dev->priv;
  231. switch (ctrl->id) {
  232. case V4L2_CID_AUDIO_MUTE:
  233. if (ctrl->value)
  234. gemtek_mute(rt);
  235. else
  236. gemtek_unmute(rt);
  237. return 0;
  238. case V4L2_CID_AUDIO_VOLUME:
  239. if (ctrl->value)
  240. gemtek_unmute(rt);
  241. else
  242. gemtek_mute(rt);
  243. return 0;
  244. }
  245. return -EINVAL;
  246. }
  247. static int vidioc_g_audio (struct file *file, void *priv,
  248. struct v4l2_audio *a)
  249. {
  250. if (a->index > 1)
  251. return -EINVAL;
  252. strcpy(a->name, "Radio");
  253. a->capability = V4L2_AUDCAP_STEREO;
  254. return 0;
  255. }
  256. static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
  257. {
  258. *i = 0;
  259. return 0;
  260. }
  261. static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
  262. {
  263. if (i != 0)
  264. return -EINVAL;
  265. return 0;
  266. }
  267. static int vidioc_s_audio(struct file *file, void *priv,
  268. struct v4l2_audio *a)
  269. {
  270. if (a->index != 0)
  271. return -EINVAL;
  272. return 0;
  273. }
  274. static struct gemtek_device gemtek_unit;
  275. static const struct file_operations gemtek_fops = {
  276. .owner = THIS_MODULE,
  277. .open = video_exclusive_open,
  278. .release = video_exclusive_release,
  279. .ioctl = video_ioctl2,
  280. .compat_ioctl = v4l_compat_ioctl32,
  281. .llseek = no_llseek,
  282. };
  283. static struct video_device gemtek_radio=
  284. {
  285. .owner = THIS_MODULE,
  286. .name = "GemTek radio",
  287. .type = VID_TYPE_TUNER,
  288. .hardware = 0,
  289. .fops = &gemtek_fops,
  290. .vidioc_querycap = vidioc_querycap,
  291. .vidioc_g_tuner = vidioc_g_tuner,
  292. .vidioc_s_tuner = vidioc_s_tuner,
  293. .vidioc_g_audio = vidioc_g_audio,
  294. .vidioc_s_audio = vidioc_s_audio,
  295. .vidioc_g_input = vidioc_g_input,
  296. .vidioc_s_input = vidioc_s_input,
  297. .vidioc_g_frequency = vidioc_g_frequency,
  298. .vidioc_s_frequency = vidioc_s_frequency,
  299. .vidioc_queryctrl = vidioc_queryctrl,
  300. .vidioc_g_ctrl = vidioc_g_ctrl,
  301. .vidioc_s_ctrl = vidioc_s_ctrl,
  302. };
  303. static int __init gemtek_init(void)
  304. {
  305. if(io==-1)
  306. {
  307. 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");
  308. return -EINVAL;
  309. }
  310. if (!request_region(io, 4, "gemtek"))
  311. {
  312. printk(KERN_ERR "gemtek: port 0x%x already in use\n", io);
  313. return -EBUSY;
  314. }
  315. gemtek_radio.priv=&gemtek_unit;
  316. if(video_register_device(&gemtek_radio, VFL_TYPE_RADIO, radio_nr)==-1)
  317. {
  318. release_region(io, 4);
  319. return -EINVAL;
  320. }
  321. printk(KERN_INFO "GemTek Radio Card driver.\n");
  322. spin_lock_init(&lock);
  323. /* this is _maybe_ unnecessary */
  324. outb(0x01, io);
  325. /* mute card - prevents noisy bootups */
  326. gemtek_unit.muted = 0;
  327. gemtek_mute(&gemtek_unit);
  328. return 0;
  329. }
  330. MODULE_AUTHOR("Jonas Munsin");
  331. MODULE_DESCRIPTION("A driver for the GemTek Radio Card");
  332. MODULE_LICENSE("GPL");
  333. module_param(io, int, 0);
  334. 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)).");
  335. module_param(radio_nr, int, 0);
  336. static void __exit gemtek_cleanup(void)
  337. {
  338. video_unregister_device(&gemtek_radio);
  339. release_region(io,4);
  340. }
  341. module_init(gemtek_init);
  342. module_exit(gemtek_cleanup);
  343. /*
  344. Local variables:
  345. 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"
  346. End:
  347. */