radio-rtrack2.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
  10. */
  11. #include <linux/module.h> /* Modules */
  12. #include <linux/init.h> /* Initdata */
  13. #include <linux/ioport.h> /* request_region */
  14. #include <linux/delay.h> /* udelay */
  15. #include <asm/io.h> /* outb, outb_p */
  16. #include <asm/uaccess.h> /* copy to/from user */
  17. #include <linux/videodev2.h> /* kernel radio structs */
  18. #include <media/v4l2-common.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/version.h> /* for KERNEL_VERSION MACRO */
  21. #define RADIO_VERSION KERNEL_VERSION(0,0,2)
  22. static struct v4l2_queryctrl radio_qctrl[] = {
  23. {
  24. .id = V4L2_CID_AUDIO_MUTE,
  25. .name = "Mute",
  26. .minimum = 0,
  27. .maximum = 1,
  28. .default_value = 1,
  29. .type = V4L2_CTRL_TYPE_BOOLEAN,
  30. },{
  31. .id = V4L2_CID_AUDIO_VOLUME,
  32. .name = "Volume",
  33. .minimum = 0,
  34. .maximum = 65535,
  35. .step = 65535,
  36. .default_value = 0xff,
  37. .type = V4L2_CTRL_TYPE_INTEGER,
  38. }
  39. };
  40. #ifndef CONFIG_RADIO_RTRACK2_PORT
  41. #define CONFIG_RADIO_RTRACK2_PORT -1
  42. #endif
  43. static int io = CONFIG_RADIO_RTRACK2_PORT;
  44. static int radio_nr = -1;
  45. static spinlock_t lock;
  46. struct rt_device
  47. {
  48. int port;
  49. unsigned long curfreq;
  50. int muted;
  51. };
  52. /* local things */
  53. static void rt_mute(struct rt_device *dev)
  54. {
  55. if(dev->muted)
  56. return;
  57. spin_lock(&lock);
  58. outb(1, io);
  59. spin_unlock(&lock);
  60. dev->muted = 1;
  61. }
  62. static void rt_unmute(struct rt_device *dev)
  63. {
  64. if(dev->muted == 0)
  65. return;
  66. spin_lock(&lock);
  67. outb(0, io);
  68. spin_unlock(&lock);
  69. dev->muted = 0;
  70. }
  71. static void zero(void)
  72. {
  73. outb_p(1, io);
  74. outb_p(3, io);
  75. outb_p(1, io);
  76. }
  77. static void one(void)
  78. {
  79. outb_p(5, io);
  80. outb_p(7, io);
  81. outb_p(5, io);
  82. }
  83. static int rt_setfreq(struct rt_device *dev, unsigned long freq)
  84. {
  85. int i;
  86. freq = freq / 200 + 856;
  87. spin_lock(&lock);
  88. outb_p(0xc8, io);
  89. outb_p(0xc9, io);
  90. outb_p(0xc9, io);
  91. for (i = 0; i < 10; i++)
  92. zero ();
  93. for (i = 14; i >= 0; i--)
  94. if (freq & (1 << i))
  95. one ();
  96. else
  97. zero ();
  98. outb_p(0xc8, io);
  99. if (!dev->muted)
  100. outb_p(0, io);
  101. spin_unlock(&lock);
  102. return 0;
  103. }
  104. static int rt_getsigstr(struct rt_device *dev)
  105. {
  106. if (inb(io) & 2) /* bit set = no signal present */
  107. return 0;
  108. return 1; /* signal present */
  109. }
  110. static int rt_do_ioctl(struct inode *inode, struct file *file,
  111. unsigned int cmd, void *arg)
  112. {
  113. struct video_device *dev = video_devdata(file);
  114. struct rt_device *rt=dev->priv;
  115. switch(cmd)
  116. {
  117. case VIDIOC_QUERYCAP:
  118. {
  119. struct v4l2_capability *v = arg;
  120. memset(v,0,sizeof(*v));
  121. strlcpy(v->driver, "radio-rtrack2", sizeof (v->driver));
  122. strlcpy(v->card, "RadioTrack II", sizeof (v->card));
  123. sprintf(v->bus_info,"ISA");
  124. v->version = RADIO_VERSION;
  125. v->capabilities = V4L2_CAP_TUNER;
  126. return 0;
  127. }
  128. case VIDIOC_G_TUNER:
  129. {
  130. struct v4l2_tuner *v = arg;
  131. if (v->index > 0)
  132. return -EINVAL;
  133. memset(v,0,sizeof(*v));
  134. strcpy(v->name, "FM");
  135. v->type = V4L2_TUNER_RADIO;
  136. v->rangelow=(88*16000);
  137. v->rangehigh=(108*16000);
  138. v->rxsubchans =V4L2_TUNER_SUB_MONO;
  139. v->capability=V4L2_TUNER_CAP_LOW;
  140. v->audmode = V4L2_TUNER_MODE_MONO;
  141. v->signal=0xFFFF*rt_getsigstr(rt);
  142. return 0;
  143. }
  144. case VIDIOC_S_TUNER:
  145. {
  146. struct v4l2_tuner *v = arg;
  147. if (v->index > 0)
  148. return -EINVAL;
  149. return 0;
  150. }
  151. case VIDIOC_S_FREQUENCY:
  152. {
  153. struct v4l2_frequency *f = arg;
  154. rt->curfreq = f->frequency;
  155. rt_setfreq(rt, rt->curfreq);
  156. return 0;
  157. }
  158. case VIDIOC_G_FREQUENCY:
  159. {
  160. struct v4l2_frequency *f = arg;
  161. f->type = V4L2_TUNER_RADIO;
  162. f->frequency = rt->curfreq;
  163. return 0;
  164. }
  165. case VIDIOC_QUERYCTRL:
  166. {
  167. struct v4l2_queryctrl *qc = arg;
  168. int i;
  169. for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
  170. if (qc->id && qc->id == radio_qctrl[i].id) {
  171. memcpy(qc, &(radio_qctrl[i]),
  172. sizeof(*qc));
  173. return (0);
  174. }
  175. }
  176. return -EINVAL;
  177. }
  178. case VIDIOC_G_CTRL:
  179. {
  180. struct v4l2_control *ctrl= arg;
  181. switch (ctrl->id) {
  182. case V4L2_CID_AUDIO_MUTE:
  183. ctrl->value=rt->muted;
  184. return (0);
  185. case V4L2_CID_AUDIO_VOLUME:
  186. if (rt->muted)
  187. ctrl->value=0;
  188. else
  189. ctrl->value=65535;
  190. return (0);
  191. }
  192. return -EINVAL;
  193. }
  194. case VIDIOC_S_CTRL:
  195. {
  196. struct v4l2_control *ctrl= arg;
  197. switch (ctrl->id) {
  198. case V4L2_CID_AUDIO_MUTE:
  199. if (ctrl->value) {
  200. rt_mute(rt);
  201. } else {
  202. rt_unmute(rt);
  203. }
  204. return (0);
  205. case V4L2_CID_AUDIO_VOLUME:
  206. if (ctrl->value) {
  207. rt_unmute(rt);
  208. } else {
  209. rt_mute(rt);
  210. }
  211. return (0);
  212. }
  213. return -EINVAL;
  214. }
  215. default:
  216. return v4l_compat_translate_ioctl(inode,file,cmd,arg,
  217. rt_do_ioctl);
  218. }
  219. }
  220. static int rt_ioctl(struct inode *inode, struct file *file,
  221. unsigned int cmd, unsigned long arg)
  222. {
  223. return video_usercopy(inode, file, cmd, arg, rt_do_ioctl);
  224. }
  225. static struct rt_device rtrack2_unit;
  226. static struct file_operations rtrack2_fops = {
  227. .owner = THIS_MODULE,
  228. .open = video_exclusive_open,
  229. .release = video_exclusive_release,
  230. .ioctl = rt_ioctl,
  231. .compat_ioctl = v4l_compat_ioctl32,
  232. .llseek = no_llseek,
  233. };
  234. static struct video_device rtrack2_radio=
  235. {
  236. .owner = THIS_MODULE,
  237. .name = "RadioTrack II radio",
  238. .type = VID_TYPE_TUNER,
  239. .hardware = 0,
  240. .fops = &rtrack2_fops,
  241. };
  242. static int __init rtrack2_init(void)
  243. {
  244. if(io==-1)
  245. {
  246. printk(KERN_ERR "You must set an I/O address with io=0x20c or io=0x30c\n");
  247. return -EINVAL;
  248. }
  249. if (!request_region(io, 4, "rtrack2"))
  250. {
  251. printk(KERN_ERR "rtrack2: port 0x%x already in use\n", io);
  252. return -EBUSY;
  253. }
  254. rtrack2_radio.priv=&rtrack2_unit;
  255. spin_lock_init(&lock);
  256. if(video_register_device(&rtrack2_radio, VFL_TYPE_RADIO, radio_nr)==-1)
  257. {
  258. release_region(io, 4);
  259. return -EINVAL;
  260. }
  261. printk(KERN_INFO "AIMSlab Radiotrack II card driver.\n");
  262. /* mute card - prevents noisy bootups */
  263. outb(1, io);
  264. rtrack2_unit.muted = 1;
  265. return 0;
  266. }
  267. MODULE_AUTHOR("Ben Pfaff");
  268. MODULE_DESCRIPTION("A driver for the RadioTrack II radio card.");
  269. MODULE_LICENSE("GPL");
  270. module_param(io, int, 0);
  271. MODULE_PARM_DESC(io, "I/O address of the RadioTrack card (0x20c or 0x30c)");
  272. module_param(radio_nr, int, 0);
  273. static void __exit rtrack2_cleanup_module(void)
  274. {
  275. video_unregister_device(&rtrack2_radio);
  276. release_region(io,4);
  277. }
  278. module_init(rtrack2_init);
  279. module_exit(rtrack2_cleanup_module);
  280. /*
  281. Local variables:
  282. compile-command: "mmake"
  283. End:
  284. */