radio-aztech.c 9.9 KB

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