radio-rtrack2.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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@lxorguk.ukuu.org.uk>
  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. unsigned long in_use;
  50. int port;
  51. unsigned long curfreq;
  52. int muted;
  53. };
  54. /* local things */
  55. static void rt_mute(struct rt_device *dev)
  56. {
  57. if(dev->muted)
  58. return;
  59. spin_lock(&lock);
  60. outb(1, io);
  61. spin_unlock(&lock);
  62. dev->muted = 1;
  63. }
  64. static void rt_unmute(struct rt_device *dev)
  65. {
  66. if(dev->muted == 0)
  67. return;
  68. spin_lock(&lock);
  69. outb(0, io);
  70. spin_unlock(&lock);
  71. dev->muted = 0;
  72. }
  73. static void zero(void)
  74. {
  75. outb_p(1, io);
  76. outb_p(3, io);
  77. outb_p(1, io);
  78. }
  79. static void one(void)
  80. {
  81. outb_p(5, io);
  82. outb_p(7, io);
  83. outb_p(5, io);
  84. }
  85. static int rt_setfreq(struct rt_device *dev, unsigned long freq)
  86. {
  87. int i;
  88. freq = freq / 200 + 856;
  89. spin_lock(&lock);
  90. outb_p(0xc8, io);
  91. outb_p(0xc9, io);
  92. outb_p(0xc9, io);
  93. for (i = 0; i < 10; i++)
  94. zero ();
  95. for (i = 14; i >= 0; i--)
  96. if (freq & (1 << i))
  97. one ();
  98. else
  99. zero ();
  100. outb_p(0xc8, io);
  101. if (!dev->muted)
  102. outb_p(0, io);
  103. spin_unlock(&lock);
  104. return 0;
  105. }
  106. static int vidioc_querycap(struct file *file, void *priv,
  107. struct v4l2_capability *v)
  108. {
  109. strlcpy(v->driver, "radio-rtrack2", sizeof(v->driver));
  110. strlcpy(v->card, "RadioTrack II", sizeof(v->card));
  111. sprintf(v->bus_info, "ISA");
  112. v->version = RADIO_VERSION;
  113. v->capabilities = V4L2_CAP_TUNER;
  114. return 0;
  115. }
  116. static int vidioc_s_tuner(struct file *file, void *priv,
  117. struct v4l2_tuner *v)
  118. {
  119. if (v->index > 0)
  120. return -EINVAL;
  121. return 0;
  122. }
  123. static int rt_getsigstr(struct rt_device *dev)
  124. {
  125. if (inb(io) & 2) /* bit set = no signal present */
  126. return 0;
  127. return 1; /* signal present */
  128. }
  129. static int vidioc_g_tuner(struct file *file, void *priv,
  130. struct v4l2_tuner *v)
  131. {
  132. struct rt_device *rt = video_drvdata(file);
  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 rt_device *rt = video_drvdata(file);
  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 rt_device *rt = video_drvdata(file);
  157. f->type = V4L2_TUNER_RADIO;
  158. f->frequency = rt->curfreq;
  159. return 0;
  160. }
  161. static int vidioc_queryctrl(struct file *file, void *priv,
  162. struct v4l2_queryctrl *qc)
  163. {
  164. int i;
  165. for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
  166. if (qc->id && qc->id == radio_qctrl[i].id) {
  167. memcpy(qc, &(radio_qctrl[i]),
  168. sizeof(*qc));
  169. return 0;
  170. }
  171. }
  172. return -EINVAL;
  173. }
  174. static int vidioc_g_ctrl(struct file *file, void *priv,
  175. struct v4l2_control *ctrl)
  176. {
  177. struct rt_device *rt = video_drvdata(file);
  178. switch (ctrl->id) {
  179. case V4L2_CID_AUDIO_MUTE:
  180. ctrl->value = rt->muted;
  181. return 0;
  182. case V4L2_CID_AUDIO_VOLUME:
  183. if (rt->muted)
  184. ctrl->value = 0;
  185. else
  186. ctrl->value = 65535;
  187. return 0;
  188. }
  189. return -EINVAL;
  190. }
  191. static int vidioc_s_ctrl(struct file *file, void *priv,
  192. struct v4l2_control *ctrl)
  193. {
  194. struct rt_device *rt = video_drvdata(file);
  195. switch (ctrl->id) {
  196. case V4L2_CID_AUDIO_MUTE:
  197. if (ctrl->value)
  198. rt_mute(rt);
  199. else
  200. rt_unmute(rt);
  201. return 0;
  202. case V4L2_CID_AUDIO_VOLUME:
  203. if (ctrl->value)
  204. rt_unmute(rt);
  205. else
  206. rt_mute(rt);
  207. return 0;
  208. }
  209. return -EINVAL;
  210. }
  211. static int vidioc_g_audio(struct file *file, void *priv,
  212. struct v4l2_audio *a)
  213. {
  214. if (a->index > 1)
  215. return -EINVAL;
  216. strcpy(a->name, "Radio");
  217. a->capability = V4L2_AUDCAP_STEREO;
  218. return 0;
  219. }
  220. static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
  221. {
  222. *i = 0;
  223. return 0;
  224. }
  225. static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
  226. {
  227. if (i != 0)
  228. return -EINVAL;
  229. return 0;
  230. }
  231. static int vidioc_s_audio(struct file *file, void *priv,
  232. struct v4l2_audio *a)
  233. {
  234. if (a->index != 0)
  235. return -EINVAL;
  236. return 0;
  237. }
  238. static struct rt_device rtrack2_unit;
  239. static int rtrack2_exclusive_open(struct file *file)
  240. {
  241. return test_and_set_bit(0, &rtrack2_unit.in_use) ? -EBUSY : 0;
  242. }
  243. static int rtrack2_exclusive_release(struct file *file)
  244. {
  245. clear_bit(0, &rtrack2_unit.in_use);
  246. return 0;
  247. }
  248. static const struct v4l2_file_operations rtrack2_fops = {
  249. .owner = THIS_MODULE,
  250. .open = rtrack2_exclusive_open,
  251. .release = rtrack2_exclusive_release,
  252. .ioctl = video_ioctl2,
  253. };
  254. static const struct v4l2_ioctl_ops rtrack2_ioctl_ops = {
  255. .vidioc_querycap = vidioc_querycap,
  256. .vidioc_g_tuner = vidioc_g_tuner,
  257. .vidioc_s_tuner = vidioc_s_tuner,
  258. .vidioc_g_frequency = vidioc_g_frequency,
  259. .vidioc_s_frequency = vidioc_s_frequency,
  260. .vidioc_queryctrl = vidioc_queryctrl,
  261. .vidioc_g_ctrl = vidioc_g_ctrl,
  262. .vidioc_s_ctrl = vidioc_s_ctrl,
  263. .vidioc_g_audio = vidioc_g_audio,
  264. .vidioc_s_audio = vidioc_s_audio,
  265. .vidioc_g_input = vidioc_g_input,
  266. .vidioc_s_input = vidioc_s_input,
  267. };
  268. static struct video_device rtrack2_radio = {
  269. .name = "RadioTrack II radio",
  270. .fops = &rtrack2_fops,
  271. .ioctl_ops = &rtrack2_ioctl_ops,
  272. .release = video_device_release_empty,
  273. };
  274. static int __init rtrack2_init(void)
  275. {
  276. if(io==-1)
  277. {
  278. printk(KERN_ERR "You must set an I/O address with io=0x20c or io=0x30c\n");
  279. return -EINVAL;
  280. }
  281. if (!request_region(io, 4, "rtrack2"))
  282. {
  283. printk(KERN_ERR "rtrack2: port 0x%x already in use\n", io);
  284. return -EBUSY;
  285. }
  286. video_set_drvdata(&rtrack2_radio, &rtrack2_unit);
  287. spin_lock_init(&lock);
  288. if (video_register_device(&rtrack2_radio, VFL_TYPE_RADIO, radio_nr) < 0) {
  289. release_region(io, 4);
  290. return -EINVAL;
  291. }
  292. printk(KERN_INFO "AIMSlab Radiotrack II card driver.\n");
  293. /* mute card - prevents noisy bootups */
  294. outb(1, io);
  295. rtrack2_unit.muted = 1;
  296. return 0;
  297. }
  298. MODULE_AUTHOR("Ben Pfaff");
  299. MODULE_DESCRIPTION("A driver for the RadioTrack II radio card.");
  300. MODULE_LICENSE("GPL");
  301. module_param(io, int, 0);
  302. MODULE_PARM_DESC(io, "I/O address of the RadioTrack card (0x20c or 0x30c)");
  303. module_param(radio_nr, int, 0);
  304. static void __exit rtrack2_cleanup_module(void)
  305. {
  306. video_unregister_device(&rtrack2_radio);
  307. release_region(io,4);
  308. }
  309. module_init(rtrack2_init);
  310. module_exit(rtrack2_cleanup_module);
  311. /*
  312. Local variables:
  313. compile-command: "mmake"
  314. End:
  315. */