radio-rtrack2.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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 vidioc_querycap(struct file *file, void *priv,
  105. struct v4l2_capability *v)
  106. {
  107. strlcpy(v->driver, "radio-rtrack2", sizeof(v->driver));
  108. strlcpy(v->card, "RadioTrack II", sizeof(v->card));
  109. sprintf(v->bus_info, "ISA");
  110. v->version = RADIO_VERSION;
  111. v->capabilities = V4L2_CAP_TUNER;
  112. return 0;
  113. }
  114. static int vidioc_s_tuner(struct file *file, void *priv,
  115. struct v4l2_tuner *v)
  116. {
  117. if (v->index > 0)
  118. return -EINVAL;
  119. return 0;
  120. }
  121. static int rt_getsigstr(struct rt_device *dev)
  122. {
  123. if (inb(io) & 2) /* bit set = no signal present */
  124. return 0;
  125. return 1; /* signal present */
  126. }
  127. static int vidioc_g_tuner(struct file *file, void *priv,
  128. struct v4l2_tuner *v)
  129. {
  130. struct video_device *dev = video_devdata(file);
  131. struct rt_device *rt = dev->priv;
  132. if (v->index > 0)
  133. return -EINVAL;
  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. static int vidioc_s_frequency(struct file *file, void *priv,
  145. struct v4l2_frequency *f)
  146. {
  147. struct video_device *dev = video_devdata(file);
  148. struct rt_device *rt = dev->priv;
  149. rt->curfreq = f->frequency;
  150. rt_setfreq(rt, rt->curfreq);
  151. return 0;
  152. }
  153. static int vidioc_g_frequency(struct file *file, void *priv,
  154. struct v4l2_frequency *f)
  155. {
  156. struct video_device *dev = video_devdata(file);
  157. struct rt_device *rt = dev->priv;
  158. f->type = V4L2_TUNER_RADIO;
  159. f->frequency = rt->curfreq;
  160. return 0;
  161. }
  162. static int vidioc_queryctrl(struct file *file, void *priv,
  163. struct v4l2_queryctrl *qc)
  164. {
  165. int i;
  166. for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
  167. if (qc->id && qc->id == radio_qctrl[i].id) {
  168. memcpy(qc, &(radio_qctrl[i]),
  169. sizeof(*qc));
  170. return 0;
  171. }
  172. }
  173. return -EINVAL;
  174. }
  175. static int vidioc_g_ctrl(struct file *file, void *priv,
  176. struct v4l2_control *ctrl)
  177. {
  178. struct video_device *dev = video_devdata(file);
  179. struct rt_device *rt = dev->priv;
  180. switch (ctrl->id) {
  181. case V4L2_CID_AUDIO_MUTE:
  182. ctrl->value = rt->muted;
  183. return 0;
  184. case V4L2_CID_AUDIO_VOLUME:
  185. if (rt->muted)
  186. ctrl->value = 0;
  187. else
  188. ctrl->value = 65535;
  189. return 0;
  190. }
  191. return -EINVAL;
  192. }
  193. static int vidioc_s_ctrl(struct file *file, void *priv,
  194. struct v4l2_control *ctrl)
  195. {
  196. struct video_device *dev = video_devdata(file);
  197. struct rt_device *rt = dev->priv;
  198. switch (ctrl->id) {
  199. case V4L2_CID_AUDIO_MUTE:
  200. if (ctrl->value)
  201. rt_mute(rt);
  202. else
  203. rt_unmute(rt);
  204. return 0;
  205. case V4L2_CID_AUDIO_VOLUME:
  206. if (ctrl->value)
  207. rt_unmute(rt);
  208. else
  209. rt_mute(rt);
  210. return 0;
  211. }
  212. return -EINVAL;
  213. }
  214. static int vidioc_g_audio(struct file *file, void *priv,
  215. struct v4l2_audio *a)
  216. {
  217. if (a->index > 1)
  218. return -EINVAL;
  219. strcpy(a->name, "Radio");
  220. a->capability = V4L2_AUDCAP_STEREO;
  221. return 0;
  222. }
  223. static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
  224. {
  225. *i = 0;
  226. return 0;
  227. }
  228. static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
  229. {
  230. if (i != 0)
  231. return -EINVAL;
  232. return 0;
  233. }
  234. static int vidioc_s_audio(struct file *file, void *priv,
  235. struct v4l2_audio *a)
  236. {
  237. if (a->index != 0)
  238. return -EINVAL;
  239. return 0;
  240. }
  241. static struct rt_device rtrack2_unit;
  242. static const struct file_operations rtrack2_fops = {
  243. .owner = THIS_MODULE,
  244. .open = video_exclusive_open,
  245. .release = video_exclusive_release,
  246. .ioctl = video_ioctl2,
  247. .compat_ioctl = v4l_compat_ioctl32,
  248. .llseek = no_llseek,
  249. };
  250. static struct video_device rtrack2_radio=
  251. {
  252. .owner = THIS_MODULE,
  253. .name = "RadioTrack II radio",
  254. .type = VID_TYPE_TUNER,
  255. .fops = &rtrack2_fops,
  256. .vidioc_querycap = vidioc_querycap,
  257. .vidioc_g_tuner = vidioc_g_tuner,
  258. .vidioc_s_tuner = vidioc_s_tuner,
  259. .vidioc_g_frequency = vidioc_g_frequency,
  260. .vidioc_s_frequency = vidioc_s_frequency,
  261. .vidioc_queryctrl = vidioc_queryctrl,
  262. .vidioc_g_ctrl = vidioc_g_ctrl,
  263. .vidioc_s_ctrl = vidioc_s_ctrl,
  264. .vidioc_g_audio = vidioc_g_audio,
  265. .vidioc_s_audio = vidioc_s_audio,
  266. .vidioc_g_input = vidioc_g_input,
  267. .vidioc_s_input = vidioc_s_input,
  268. };
  269. static int __init rtrack2_init(void)
  270. {
  271. if(io==-1)
  272. {
  273. printk(KERN_ERR "You must set an I/O address with io=0x20c or io=0x30c\n");
  274. return -EINVAL;
  275. }
  276. if (!request_region(io, 4, "rtrack2"))
  277. {
  278. printk(KERN_ERR "rtrack2: port 0x%x already in use\n", io);
  279. return -EBUSY;
  280. }
  281. rtrack2_radio.priv=&rtrack2_unit;
  282. spin_lock_init(&lock);
  283. if(video_register_device(&rtrack2_radio, VFL_TYPE_RADIO, radio_nr)==-1)
  284. {
  285. release_region(io, 4);
  286. return -EINVAL;
  287. }
  288. printk(KERN_INFO "AIMSlab Radiotrack II card driver.\n");
  289. /* mute card - prevents noisy bootups */
  290. outb(1, io);
  291. rtrack2_unit.muted = 1;
  292. return 0;
  293. }
  294. MODULE_AUTHOR("Ben Pfaff");
  295. MODULE_DESCRIPTION("A driver for the RadioTrack II radio card.");
  296. MODULE_LICENSE("GPL");
  297. module_param(io, int, 0);
  298. MODULE_PARM_DESC(io, "I/O address of the RadioTrack card (0x20c or 0x30c)");
  299. module_param(radio_nr, int, 0);
  300. static void __exit rtrack2_cleanup_module(void)
  301. {
  302. video_unregister_device(&rtrack2_radio);
  303. release_region(io,4);
  304. }
  305. module_init(rtrack2_init);
  306. module_exit(rtrack2_cleanup_module);
  307. /*
  308. Local variables:
  309. compile-command: "mmake"
  310. End:
  311. */