radio-aztech.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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 video_device *dev = video_devdata(file);
  172. struct az_device *az = dev->priv;
  173. if (v->index > 0)
  174. return -EINVAL;
  175. strcpy(v->name, "FM");
  176. v->type = V4L2_TUNER_RADIO;
  177. v->rangelow=(87*16000);
  178. v->rangehigh=(108*16000);
  179. v->rxsubchans =V4L2_TUNER_SUB_MONO|V4L2_TUNER_SUB_STEREO;
  180. v->capability=V4L2_TUNER_CAP_LOW;
  181. if(az_getstereo(az))
  182. v->audmode = V4L2_TUNER_MODE_STEREO;
  183. else
  184. v->audmode = V4L2_TUNER_MODE_MONO;
  185. v->signal=0xFFFF*az_getsigstr(az);
  186. return 0;
  187. }
  188. static int vidioc_s_tuner (struct file *file, void *priv,
  189. struct v4l2_tuner *v)
  190. {
  191. if (v->index > 0)
  192. return -EINVAL;
  193. return 0;
  194. }
  195. static int vidioc_g_audio (struct file *file, void *priv,
  196. struct v4l2_audio *a)
  197. {
  198. if (a->index > 1)
  199. return -EINVAL;
  200. strcpy(a->name, "Radio");
  201. a->capability = V4L2_AUDCAP_STEREO;
  202. return 0;
  203. }
  204. static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
  205. {
  206. *i = 0;
  207. return 0;
  208. }
  209. static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
  210. {
  211. if (i != 0)
  212. return -EINVAL;
  213. return 0;
  214. }
  215. static int vidioc_s_audio (struct file *file, void *priv,
  216. struct v4l2_audio *a)
  217. {
  218. if (a->index != 0)
  219. return -EINVAL;
  220. return 0;
  221. }
  222. static int vidioc_s_frequency (struct file *file, void *priv,
  223. struct v4l2_frequency *f)
  224. {
  225. struct video_device *dev = video_devdata(file);
  226. struct az_device *az = dev->priv;
  227. az->curfreq = f->frequency;
  228. az_setfreq(az, az->curfreq);
  229. return 0;
  230. }
  231. static int vidioc_g_frequency (struct file *file, void *priv,
  232. struct v4l2_frequency *f)
  233. {
  234. struct video_device *dev = video_devdata(file);
  235. struct az_device *az = dev->priv;
  236. f->type = V4L2_TUNER_RADIO;
  237. f->frequency = az->curfreq;
  238. return 0;
  239. }
  240. static int vidioc_queryctrl (struct file *file, void *priv,
  241. struct v4l2_queryctrl *qc)
  242. {
  243. int i;
  244. for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
  245. if (qc->id && qc->id == radio_qctrl[i].id) {
  246. memcpy(qc, &(radio_qctrl[i]),
  247. sizeof(*qc));
  248. return (0);
  249. }
  250. }
  251. return -EINVAL;
  252. }
  253. static int vidioc_g_ctrl (struct file *file, void *priv,
  254. struct v4l2_control *ctrl)
  255. {
  256. struct video_device *dev = video_devdata(file);
  257. struct az_device *az = dev->priv;
  258. switch (ctrl->id) {
  259. case V4L2_CID_AUDIO_MUTE:
  260. if (az->curvol==0)
  261. ctrl->value=1;
  262. else
  263. ctrl->value=0;
  264. return (0);
  265. case V4L2_CID_AUDIO_VOLUME:
  266. ctrl->value=az->curvol * 6554;
  267. return (0);
  268. }
  269. return -EINVAL;
  270. }
  271. static int vidioc_s_ctrl (struct file *file, void *priv,
  272. struct v4l2_control *ctrl)
  273. {
  274. struct video_device *dev = video_devdata(file);
  275. struct az_device *az = dev->priv;
  276. switch (ctrl->id) {
  277. case V4L2_CID_AUDIO_MUTE:
  278. if (ctrl->value) {
  279. az_setvol(az,0);
  280. } else {
  281. az_setvol(az,az->curvol);
  282. }
  283. return (0);
  284. case V4L2_CID_AUDIO_VOLUME:
  285. az_setvol(az,ctrl->value);
  286. return (0);
  287. }
  288. return -EINVAL;
  289. }
  290. static struct az_device aztech_unit;
  291. static int aztech_exclusive_open(struct inode *inode, struct file *file)
  292. {
  293. return test_and_set_bit(0, &aztech_unit.in_use) ? -EBUSY : 0;
  294. }
  295. static int aztech_exclusive_release(struct inode *inode, struct file *file)
  296. {
  297. clear_bit(0, &aztech_unit.in_use);
  298. return 0;
  299. }
  300. static const struct file_operations aztech_fops = {
  301. .owner = THIS_MODULE,
  302. .open = aztech_exclusive_open,
  303. .release = aztech_exclusive_release,
  304. .ioctl = video_ioctl2,
  305. #ifdef CONFIG_COMPAT
  306. .compat_ioctl = v4l_compat_ioctl32,
  307. #endif
  308. .llseek = no_llseek,
  309. };
  310. static const struct v4l2_ioctl_ops aztech_ioctl_ops = {
  311. .vidioc_querycap = vidioc_querycap,
  312. .vidioc_g_tuner = vidioc_g_tuner,
  313. .vidioc_s_tuner = vidioc_s_tuner,
  314. .vidioc_g_audio = vidioc_g_audio,
  315. .vidioc_s_audio = vidioc_s_audio,
  316. .vidioc_g_input = vidioc_g_input,
  317. .vidioc_s_input = vidioc_s_input,
  318. .vidioc_g_frequency = vidioc_g_frequency,
  319. .vidioc_s_frequency = vidioc_s_frequency,
  320. .vidioc_queryctrl = vidioc_queryctrl,
  321. .vidioc_g_ctrl = vidioc_g_ctrl,
  322. .vidioc_s_ctrl = vidioc_s_ctrl,
  323. };
  324. static struct video_device aztech_radio = {
  325. .name = "Aztech radio",
  326. .fops = &aztech_fops,
  327. .ioctl_ops = &aztech_ioctl_ops,
  328. .release = video_device_release_empty,
  329. };
  330. module_param_named(debug,aztech_radio.debug, int, 0644);
  331. MODULE_PARM_DESC(debug,"activates debug info");
  332. static int __init aztech_init(void)
  333. {
  334. if(io==-1)
  335. {
  336. printk(KERN_ERR "You must set an I/O address with io=0x???\n");
  337. return -EINVAL;
  338. }
  339. if (!request_region(io, 2, "aztech"))
  340. {
  341. printk(KERN_ERR "aztech: port 0x%x already in use\n", io);
  342. return -EBUSY;
  343. }
  344. mutex_init(&lock);
  345. aztech_radio.priv=&aztech_unit;
  346. if (video_register_device(&aztech_radio, VFL_TYPE_RADIO, radio_nr) < 0) {
  347. release_region(io,2);
  348. return -EINVAL;
  349. }
  350. printk(KERN_INFO "Aztech radio card driver v1.00/19990224 rkroll@exploits.org\n");
  351. /* mute card - prevents noisy bootups */
  352. outb (0, io);
  353. return 0;
  354. }
  355. MODULE_AUTHOR("Russell Kroll, Quay Lu, Donald Song, Jason Lewis, Scott McGrath, William McGrath");
  356. MODULE_DESCRIPTION("A driver for the Aztech radio card.");
  357. MODULE_LICENSE("GPL");
  358. module_param(io, int, 0);
  359. module_param(radio_nr, int, 0);
  360. MODULE_PARM_DESC(io, "I/O address of the Aztech card (0x350 or 0x358)");
  361. static void __exit aztech_cleanup(void)
  362. {
  363. video_unregister_device(&aztech_radio);
  364. release_region(io,2);
  365. }
  366. module_init(aztech_init);
  367. module_exit(aztech_cleanup);