radio-rtrack2.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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 <media/v4l2-ioctl.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/version.h> /* for KERNEL_VERSION MACRO */
  22. #define RADIO_VERSION KERNEL_VERSION(0,0,2)
  23. static struct v4l2_queryctrl radio_qctrl[] = {
  24. {
  25. .id = V4L2_CID_AUDIO_MUTE,
  26. .name = "Mute",
  27. .minimum = 0,
  28. .maximum = 1,
  29. .default_value = 1,
  30. .type = V4L2_CTRL_TYPE_BOOLEAN,
  31. },{
  32. .id = V4L2_CID_AUDIO_VOLUME,
  33. .name = "Volume",
  34. .minimum = 0,
  35. .maximum = 65535,
  36. .step = 65535,
  37. .default_value = 0xff,
  38. .type = V4L2_CTRL_TYPE_INTEGER,
  39. }
  40. };
  41. #ifndef CONFIG_RADIO_RTRACK2_PORT
  42. #define CONFIG_RADIO_RTRACK2_PORT -1
  43. #endif
  44. static int io = CONFIG_RADIO_RTRACK2_PORT;
  45. static int radio_nr = -1;
  46. static spinlock_t lock;
  47. struct rt_device
  48. {
  49. int port;
  50. unsigned long curfreq;
  51. int muted;
  52. };
  53. /* local things */
  54. static void rt_mute(struct rt_device *dev)
  55. {
  56. if(dev->muted)
  57. return;
  58. spin_lock(&lock);
  59. outb(1, io);
  60. spin_unlock(&lock);
  61. dev->muted = 1;
  62. }
  63. static void rt_unmute(struct rt_device *dev)
  64. {
  65. if(dev->muted == 0)
  66. return;
  67. spin_lock(&lock);
  68. outb(0, io);
  69. spin_unlock(&lock);
  70. dev->muted = 0;
  71. }
  72. static void zero(void)
  73. {
  74. outb_p(1, io);
  75. outb_p(3, io);
  76. outb_p(1, io);
  77. }
  78. static void one(void)
  79. {
  80. outb_p(5, io);
  81. outb_p(7, io);
  82. outb_p(5, io);
  83. }
  84. static int rt_setfreq(struct rt_device *dev, unsigned long freq)
  85. {
  86. int i;
  87. freq = freq / 200 + 856;
  88. spin_lock(&lock);
  89. outb_p(0xc8, io);
  90. outb_p(0xc9, io);
  91. outb_p(0xc9, io);
  92. for (i = 0; i < 10; i++)
  93. zero ();
  94. for (i = 14; i >= 0; i--)
  95. if (freq & (1 << i))
  96. one ();
  97. else
  98. zero ();
  99. outb_p(0xc8, io);
  100. if (!dev->muted)
  101. outb_p(0, io);
  102. spin_unlock(&lock);
  103. return 0;
  104. }
  105. static int vidioc_querycap(struct file *file, void *priv,
  106. struct v4l2_capability *v)
  107. {
  108. strlcpy(v->driver, "radio-rtrack2", sizeof(v->driver));
  109. strlcpy(v->card, "RadioTrack II", sizeof(v->card));
  110. sprintf(v->bus_info, "ISA");
  111. v->version = RADIO_VERSION;
  112. v->capabilities = V4L2_CAP_TUNER;
  113. return 0;
  114. }
  115. static int vidioc_s_tuner(struct file *file, void *priv,
  116. struct v4l2_tuner *v)
  117. {
  118. if (v->index > 0)
  119. return -EINVAL;
  120. return 0;
  121. }
  122. static int rt_getsigstr(struct rt_device *dev)
  123. {
  124. if (inb(io) & 2) /* bit set = no signal present */
  125. return 0;
  126. return 1; /* signal present */
  127. }
  128. static int vidioc_g_tuner(struct file *file, void *priv,
  129. struct v4l2_tuner *v)
  130. {
  131. struct video_device *dev = video_devdata(file);
  132. struct rt_device *rt = dev->priv;
  133. if (v->index > 0)
  134. return -EINVAL;
  135. strcpy(v->name, "FM");
  136. v->type = V4L2_TUNER_RADIO;
  137. v->rangelow = (88*16000);
  138. v->rangehigh = (108*16000);
  139. v->rxsubchans = V4L2_TUNER_SUB_MONO;
  140. v->capability = V4L2_TUNER_CAP_LOW;
  141. v->audmode = V4L2_TUNER_MODE_MONO;
  142. v->signal = 0xFFFF*rt_getsigstr(rt);
  143. return 0;
  144. }
  145. static int vidioc_s_frequency(struct file *file, void *priv,
  146. struct v4l2_frequency *f)
  147. {
  148. struct video_device *dev = video_devdata(file);
  149. struct rt_device *rt = dev->priv;
  150. rt->curfreq = f->frequency;
  151. rt_setfreq(rt, rt->curfreq);
  152. return 0;
  153. }
  154. static int vidioc_g_frequency(struct file *file, void *priv,
  155. struct v4l2_frequency *f)
  156. {
  157. struct video_device *dev = video_devdata(file);
  158. struct rt_device *rt = dev->priv;
  159. f->type = V4L2_TUNER_RADIO;
  160. f->frequency = rt->curfreq;
  161. return 0;
  162. }
  163. static int vidioc_queryctrl(struct file *file, void *priv,
  164. struct v4l2_queryctrl *qc)
  165. {
  166. int i;
  167. for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
  168. if (qc->id && qc->id == radio_qctrl[i].id) {
  169. memcpy(qc, &(radio_qctrl[i]),
  170. sizeof(*qc));
  171. return 0;
  172. }
  173. }
  174. return -EINVAL;
  175. }
  176. static int vidioc_g_ctrl(struct file *file, void *priv,
  177. struct v4l2_control *ctrl)
  178. {
  179. struct video_device *dev = video_devdata(file);
  180. struct rt_device *rt = dev->priv;
  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. static int vidioc_s_ctrl(struct file *file, void *priv,
  195. struct v4l2_control *ctrl)
  196. {
  197. struct video_device *dev = video_devdata(file);
  198. struct rt_device *rt = dev->priv;
  199. switch (ctrl->id) {
  200. case V4L2_CID_AUDIO_MUTE:
  201. if (ctrl->value)
  202. rt_mute(rt);
  203. else
  204. rt_unmute(rt);
  205. return 0;
  206. case V4L2_CID_AUDIO_VOLUME:
  207. if (ctrl->value)
  208. rt_unmute(rt);
  209. else
  210. rt_mute(rt);
  211. return 0;
  212. }
  213. return -EINVAL;
  214. }
  215. static int vidioc_g_audio(struct file *file, void *priv,
  216. struct v4l2_audio *a)
  217. {
  218. if (a->index > 1)
  219. return -EINVAL;
  220. strcpy(a->name, "Radio");
  221. a->capability = V4L2_AUDCAP_STEREO;
  222. return 0;
  223. }
  224. static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
  225. {
  226. *i = 0;
  227. return 0;
  228. }
  229. static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
  230. {
  231. if (i != 0)
  232. return -EINVAL;
  233. return 0;
  234. }
  235. static int vidioc_s_audio(struct file *file, void *priv,
  236. struct v4l2_audio *a)
  237. {
  238. if (a->index != 0)
  239. return -EINVAL;
  240. return 0;
  241. }
  242. static struct rt_device rtrack2_unit;
  243. static const struct file_operations rtrack2_fops = {
  244. .owner = THIS_MODULE,
  245. .open = video_exclusive_open,
  246. .release = video_exclusive_release,
  247. .ioctl = video_ioctl2,
  248. #ifdef CONFIG_COMPAT
  249. .compat_ioctl = v4l_compat_ioctl32,
  250. #endif
  251. .llseek = no_llseek,
  252. };
  253. static const struct v4l2_ioctl_ops rtrack2_ioctl_ops = {
  254. .vidioc_querycap = vidioc_querycap,
  255. .vidioc_g_tuner = vidioc_g_tuner,
  256. .vidioc_s_tuner = vidioc_s_tuner,
  257. .vidioc_g_frequency = vidioc_g_frequency,
  258. .vidioc_s_frequency = vidioc_s_frequency,
  259. .vidioc_queryctrl = vidioc_queryctrl,
  260. .vidioc_g_ctrl = vidioc_g_ctrl,
  261. .vidioc_s_ctrl = vidioc_s_ctrl,
  262. .vidioc_g_audio = vidioc_g_audio,
  263. .vidioc_s_audio = vidioc_s_audio,
  264. .vidioc_g_input = vidioc_g_input,
  265. .vidioc_s_input = vidioc_s_input,
  266. };
  267. static struct video_device rtrack2_radio = {
  268. .name = "RadioTrack II radio",
  269. .fops = &rtrack2_fops,
  270. .ioctl_ops = &rtrack2_ioctl_ops,
  271. };
  272. static int __init rtrack2_init(void)
  273. {
  274. if(io==-1)
  275. {
  276. printk(KERN_ERR "You must set an I/O address with io=0x20c or io=0x30c\n");
  277. return -EINVAL;
  278. }
  279. if (!request_region(io, 4, "rtrack2"))
  280. {
  281. printk(KERN_ERR "rtrack2: port 0x%x already in use\n", io);
  282. return -EBUSY;
  283. }
  284. rtrack2_radio.priv=&rtrack2_unit;
  285. spin_lock_init(&lock);
  286. if(video_register_device(&rtrack2_radio, VFL_TYPE_RADIO, radio_nr)==-1)
  287. {
  288. release_region(io, 4);
  289. return -EINVAL;
  290. }
  291. printk(KERN_INFO "AIMSlab Radiotrack II card driver.\n");
  292. /* mute card - prevents noisy bootups */
  293. outb(1, io);
  294. rtrack2_unit.muted = 1;
  295. return 0;
  296. }
  297. MODULE_AUTHOR("Ben Pfaff");
  298. MODULE_DESCRIPTION("A driver for the RadioTrack II radio card.");
  299. MODULE_LICENSE("GPL");
  300. module_param(io, int, 0);
  301. MODULE_PARM_DESC(io, "I/O address of the RadioTrack card (0x20c or 0x30c)");
  302. module_param(radio_nr, int, 0);
  303. static void __exit rtrack2_cleanup_module(void)
  304. {
  305. video_unregister_device(&rtrack2_radio);
  306. release_region(io,4);
  307. }
  308. module_init(rtrack2_init);
  309. module_exit(rtrack2_cleanup_module);
  310. /*
  311. Local variables:
  312. compile-command: "mmake"
  313. End:
  314. */