radio-aztech.c 9.6 KB

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