radio-aztech.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /* radio-aztech.c - Aztech radio card driver for Linux 2.2
  2. *
  3. * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
  4. * Adapted to support the Video for Linux API by
  5. * Russell Kroll <rkroll@exploits.org>. Based on original tuner code by:
  6. *
  7. * Quay Ly
  8. * Donald Song
  9. * Jason Lewis (jlewis@twilight.vtc.vsc.edu)
  10. * Scott McGrath (smcgrath@twilight.vtc.vsc.edu)
  11. * William McGrath (wmcgrath@twilight.vtc.vsc.edu)
  12. *
  13. * The basis for this code may be found at http://bigbang.vtc.vsc.edu/fmradio/
  14. * along with more information on the card itself.
  15. *
  16. * History:
  17. * 1999-02-24 Russell Kroll <rkroll@exploits.org>
  18. * Fine tuning/VIDEO_TUNER_LOW
  19. * Range expanded to 87-108 MHz (from 87.9-107.8)
  20. *
  21. * Notable changes from the original source:
  22. * - includes stripped down to the essentials
  23. * - for loops used as delays replaced with udelay()
  24. * - #defines removed, changed to static values
  25. * - tuning structure changed - no more character arrays, other changes
  26. */
  27. #include <linux/module.h> /* Modules */
  28. #include <linux/init.h> /* Initdata */
  29. #include <linux/ioport.h> /* request_region */
  30. #include <linux/delay.h> /* udelay */
  31. #include <linux/videodev2.h> /* kernel radio structs */
  32. #include <linux/io.h> /* outb, outb_p */
  33. #include <media/v4l2-device.h>
  34. #include <media/v4l2-ioctl.h>
  35. MODULE_AUTHOR("Russell Kroll, Quay Lu, Donald Song, Jason Lewis, Scott McGrath, William McGrath");
  36. MODULE_DESCRIPTION("A driver for the Aztech radio card.");
  37. MODULE_LICENSE("GPL");
  38. MODULE_VERSION("0.0.3");
  39. /* acceptable ports: 0x350 (JP3 shorted), 0x358 (JP3 open) */
  40. #ifndef CONFIG_RADIO_AZTECH_PORT
  41. #define CONFIG_RADIO_AZTECH_PORT -1
  42. #endif
  43. static int io = CONFIG_RADIO_AZTECH_PORT;
  44. static int radio_nr = -1;
  45. static int radio_wait_time = 1000;
  46. module_param(io, int, 0);
  47. module_param(radio_nr, int, 0);
  48. MODULE_PARM_DESC(io, "I/O address of the Aztech card (0x350 or 0x358)");
  49. struct aztech
  50. {
  51. struct v4l2_device v4l2_dev;
  52. struct video_device vdev;
  53. int io;
  54. int curvol;
  55. unsigned long curfreq;
  56. int stereo;
  57. struct mutex lock;
  58. };
  59. static struct aztech aztech_card;
  60. static int volconvert(int level)
  61. {
  62. level >>= 14; /* Map 16bits down to 2 bit */
  63. level &= 3;
  64. /* convert to card-friendly values */
  65. switch (level) {
  66. case 0:
  67. return 0;
  68. case 1:
  69. return 1;
  70. case 2:
  71. return 4;
  72. case 3:
  73. return 5;
  74. }
  75. return 0; /* Quieten gcc */
  76. }
  77. static void send_0_byte(struct aztech *az)
  78. {
  79. udelay(radio_wait_time);
  80. outb_p(2 + volconvert(az->curvol), az->io);
  81. outb_p(64 + 2 + volconvert(az->curvol), az->io);
  82. }
  83. static void send_1_byte(struct aztech *az)
  84. {
  85. udelay (radio_wait_time);
  86. outb_p(128 + 2 + volconvert(az->curvol), az->io);
  87. outb_p(128 + 64 + 2 + volconvert(az->curvol), az->io);
  88. }
  89. static int az_setvol(struct aztech *az, int vol)
  90. {
  91. mutex_lock(&az->lock);
  92. outb(volconvert(vol), az->io);
  93. mutex_unlock(&az->lock);
  94. return 0;
  95. }
  96. /* thanks to Michael Dwyer for giving me a dose of clues in
  97. * the signal strength department..
  98. *
  99. * This card has a stereo bit - bit 0 set = mono, not set = stereo
  100. * It also has a "signal" bit - bit 1 set = bad signal, not set = good
  101. *
  102. */
  103. static int az_getsigstr(struct aztech *az)
  104. {
  105. int sig = 1;
  106. mutex_lock(&az->lock);
  107. if (inb(az->io) & 2) /* bit set = no signal present */
  108. sig = 0;
  109. mutex_unlock(&az->lock);
  110. return sig;
  111. }
  112. static int az_getstereo(struct aztech *az)
  113. {
  114. int stereo = 1;
  115. mutex_lock(&az->lock);
  116. if (inb(az->io) & 1) /* bit set = mono */
  117. stereo = 0;
  118. mutex_unlock(&az->lock);
  119. return stereo;
  120. }
  121. static int az_setfreq(struct aztech *az, unsigned long frequency)
  122. {
  123. int i;
  124. mutex_lock(&az->lock);
  125. az->curfreq = frequency;
  126. frequency += 171200; /* Add 10.7 MHz IF */
  127. frequency /= 800; /* Convert to 50 kHz units */
  128. send_0_byte(az); /* 0: LSB of frequency */
  129. for (i = 0; i < 13; i++) /* : frequency bits (1-13) */
  130. if (frequency & (1 << i))
  131. send_1_byte(az);
  132. else
  133. send_0_byte(az);
  134. send_0_byte(az); /* 14: test bit - always 0 */
  135. send_0_byte(az); /* 15: test bit - always 0 */
  136. send_0_byte(az); /* 16: band data 0 - always 0 */
  137. if (az->stereo) /* 17: stereo (1 to enable) */
  138. send_1_byte(az);
  139. else
  140. send_0_byte(az);
  141. send_1_byte(az); /* 18: band data 1 - unknown */
  142. send_0_byte(az); /* 19: time base - always 0 */
  143. send_0_byte(az); /* 20: spacing (0 = 25 kHz) */
  144. send_1_byte(az); /* 21: spacing (1 = 25 kHz) */
  145. send_0_byte(az); /* 22: spacing (0 = 25 kHz) */
  146. send_1_byte(az); /* 23: AM/FM (FM = 1, always) */
  147. /* latch frequency */
  148. udelay(radio_wait_time);
  149. outb_p(128 + 64 + volconvert(az->curvol), az->io);
  150. mutex_unlock(&az->lock);
  151. return 0;
  152. }
  153. static int vidioc_querycap(struct file *file, void *priv,
  154. struct v4l2_capability *v)
  155. {
  156. strlcpy(v->driver, "radio-aztech", sizeof(v->driver));
  157. strlcpy(v->card, "Aztech Radio", sizeof(v->card));
  158. strlcpy(v->bus_info, "ISA", sizeof(v->bus_info));
  159. v->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
  160. return 0;
  161. }
  162. static int vidioc_g_tuner(struct file *file, void *priv,
  163. struct v4l2_tuner *v)
  164. {
  165. struct aztech *az = video_drvdata(file);
  166. if (v->index > 0)
  167. return -EINVAL;
  168. strlcpy(v->name, "FM", sizeof(v->name));
  169. v->type = V4L2_TUNER_RADIO;
  170. v->rangelow = 87 * 16000;
  171. v->rangehigh = 108 * 16000;
  172. v->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
  173. v->capability = V4L2_TUNER_CAP_LOW;
  174. if (az_getstereo(az))
  175. v->audmode = V4L2_TUNER_MODE_STEREO;
  176. else
  177. v->audmode = V4L2_TUNER_MODE_MONO;
  178. v->signal = 0xFFFF * az_getsigstr(az);
  179. return 0;
  180. }
  181. static int vidioc_s_tuner(struct file *file, void *priv,
  182. struct v4l2_tuner *v)
  183. {
  184. return v->index ? -EINVAL : 0;
  185. }
  186. static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
  187. {
  188. *i = 0;
  189. return 0;
  190. }
  191. static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
  192. {
  193. return i ? -EINVAL : 0;
  194. }
  195. static int vidioc_g_audio(struct file *file, void *priv,
  196. struct v4l2_audio *a)
  197. {
  198. a->index = 0;
  199. strlcpy(a->name, "Radio", sizeof(a->name));
  200. a->capability = V4L2_AUDCAP_STEREO;
  201. return 0;
  202. }
  203. static int vidioc_s_audio(struct file *file, void *priv,
  204. struct v4l2_audio *a)
  205. {
  206. return a->index ? -EINVAL : 0;
  207. }
  208. static int vidioc_s_frequency(struct file *file, void *priv,
  209. struct v4l2_frequency *f)
  210. {
  211. struct aztech *az = video_drvdata(file);
  212. if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
  213. return -EINVAL;
  214. az_setfreq(az, f->frequency);
  215. return 0;
  216. }
  217. static int vidioc_g_frequency(struct file *file, void *priv,
  218. struct v4l2_frequency *f)
  219. {
  220. struct aztech *az = video_drvdata(file);
  221. if (f->tuner != 0)
  222. return -EINVAL;
  223. f->type = V4L2_TUNER_RADIO;
  224. f->frequency = az->curfreq;
  225. return 0;
  226. }
  227. static int vidioc_queryctrl(struct file *file, void *priv,
  228. struct v4l2_queryctrl *qc)
  229. {
  230. switch (qc->id) {
  231. case V4L2_CID_AUDIO_MUTE:
  232. return v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
  233. case V4L2_CID_AUDIO_VOLUME:
  234. return v4l2_ctrl_query_fill(qc, 0, 0xff, 1, 0xff);
  235. }
  236. return -EINVAL;
  237. }
  238. static int vidioc_g_ctrl(struct file *file, void *priv,
  239. struct v4l2_control *ctrl)
  240. {
  241. struct aztech *az = video_drvdata(file);
  242. switch (ctrl->id) {
  243. case V4L2_CID_AUDIO_MUTE:
  244. if (az->curvol == 0)
  245. ctrl->value = 1;
  246. else
  247. ctrl->value = 0;
  248. return 0;
  249. case V4L2_CID_AUDIO_VOLUME:
  250. ctrl->value = az->curvol * 6554;
  251. return 0;
  252. }
  253. return -EINVAL;
  254. }
  255. static int vidioc_s_ctrl(struct file *file, void *priv,
  256. struct v4l2_control *ctrl)
  257. {
  258. struct aztech *az = video_drvdata(file);
  259. switch (ctrl->id) {
  260. case V4L2_CID_AUDIO_MUTE:
  261. if (ctrl->value)
  262. az_setvol(az, 0);
  263. else
  264. az_setvol(az, az->curvol);
  265. return 0;
  266. case V4L2_CID_AUDIO_VOLUME:
  267. az_setvol(az, ctrl->value);
  268. return 0;
  269. }
  270. return -EINVAL;
  271. }
  272. static const struct v4l2_file_operations aztech_fops = {
  273. .owner = THIS_MODULE,
  274. .unlocked_ioctl = video_ioctl2,
  275. };
  276. static const struct v4l2_ioctl_ops aztech_ioctl_ops = {
  277. .vidioc_querycap = vidioc_querycap,
  278. .vidioc_g_tuner = vidioc_g_tuner,
  279. .vidioc_s_tuner = vidioc_s_tuner,
  280. .vidioc_g_audio = vidioc_g_audio,
  281. .vidioc_s_audio = vidioc_s_audio,
  282. .vidioc_g_input = vidioc_g_input,
  283. .vidioc_s_input = vidioc_s_input,
  284. .vidioc_g_frequency = vidioc_g_frequency,
  285. .vidioc_s_frequency = vidioc_s_frequency,
  286. .vidioc_queryctrl = vidioc_queryctrl,
  287. .vidioc_g_ctrl = vidioc_g_ctrl,
  288. .vidioc_s_ctrl = vidioc_s_ctrl,
  289. };
  290. static int __init aztech_init(void)
  291. {
  292. struct aztech *az = &aztech_card;
  293. struct v4l2_device *v4l2_dev = &az->v4l2_dev;
  294. int res;
  295. strlcpy(v4l2_dev->name, "aztech", sizeof(v4l2_dev->name));
  296. az->io = io;
  297. if (az->io == -1) {
  298. v4l2_err(v4l2_dev, "you must set an I/O address with io=0x350 or 0x358\n");
  299. return -EINVAL;
  300. }
  301. if (!request_region(az->io, 2, "aztech")) {
  302. v4l2_err(v4l2_dev, "port 0x%x already in use\n", az->io);
  303. return -EBUSY;
  304. }
  305. res = v4l2_device_register(NULL, v4l2_dev);
  306. if (res < 0) {
  307. release_region(az->io, 2);
  308. v4l2_err(v4l2_dev, "Could not register v4l2_device\n");
  309. return res;
  310. }
  311. mutex_init(&az->lock);
  312. strlcpy(az->vdev.name, v4l2_dev->name, sizeof(az->vdev.name));
  313. az->vdev.v4l2_dev = v4l2_dev;
  314. az->vdev.fops = &aztech_fops;
  315. az->vdev.ioctl_ops = &aztech_ioctl_ops;
  316. az->vdev.release = video_device_release_empty;
  317. video_set_drvdata(&az->vdev, az);
  318. /* mute card - prevents noisy bootups */
  319. outb(0, az->io);
  320. if (video_register_device(&az->vdev, VFL_TYPE_RADIO, radio_nr) < 0) {
  321. v4l2_device_unregister(v4l2_dev);
  322. release_region(az->io, 2);
  323. return -EINVAL;
  324. }
  325. v4l2_info(v4l2_dev, "Aztech radio card driver v1.00/19990224 rkroll@exploits.org\n");
  326. return 0;
  327. }
  328. static void __exit aztech_exit(void)
  329. {
  330. struct aztech *az = &aztech_card;
  331. video_unregister_device(&az->vdev);
  332. v4l2_device_unregister(&az->v4l2_dev);
  333. release_region(az->io, 2);
  334. }
  335. module_init(aztech_init);
  336. module_exit(aztech_exit);