radio-aztech.c 9.7 KB

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