radio-aztech.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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 az_do_ioctl(struct inode *inode, struct file *file,
  157. unsigned int cmd, void *arg)
  158. {
  159. struct video_device *dev = video_devdata(file);
  160. struct az_device *az = dev->priv;
  161. switch(cmd)
  162. {
  163. case VIDIOC_QUERYCAP:
  164. {
  165. struct v4l2_capability *v = arg;
  166. memset(v,0,sizeof(*v));
  167. strlcpy(v->driver, "radio-aztech", sizeof (v->driver));
  168. strlcpy(v->card, "Aztech Radio", sizeof (v->card));
  169. sprintf(v->bus_info,"ISA");
  170. v->version = RADIO_VERSION;
  171. v->capabilities = V4L2_CAP_TUNER;
  172. return 0;
  173. }
  174. case VIDIOC_G_TUNER:
  175. {
  176. struct v4l2_tuner *v = arg;
  177. if (v->index > 0)
  178. return -EINVAL;
  179. memset(v,0,sizeof(*v));
  180. strcpy(v->name, "FM");
  181. v->type = V4L2_TUNER_RADIO;
  182. v->rangelow=(87*16000);
  183. v->rangehigh=(108*16000);
  184. v->rxsubchans =V4L2_TUNER_SUB_MONO|V4L2_TUNER_SUB_STEREO;
  185. v->capability=V4L2_TUNER_CAP_LOW;
  186. if(az_getstereo(az))
  187. v->audmode = V4L2_TUNER_MODE_STEREO;
  188. else
  189. v->audmode = V4L2_TUNER_MODE_MONO;
  190. v->signal=0xFFFF*az_getsigstr(az);
  191. return 0;
  192. }
  193. case VIDIOC_S_TUNER:
  194. {
  195. struct v4l2_tuner *v = arg;
  196. if (v->index > 0)
  197. return -EINVAL;
  198. return 0;
  199. }
  200. case VIDIOC_S_FREQUENCY:
  201. {
  202. struct v4l2_frequency *f = arg;
  203. az->curfreq = f->frequency;
  204. az_setfreq(az, az->curfreq);
  205. return 0;
  206. }
  207. case VIDIOC_G_FREQUENCY:
  208. {
  209. struct v4l2_frequency *f = arg;
  210. f->type = V4L2_TUNER_RADIO;
  211. f->frequency = az->curfreq;
  212. return 0;
  213. }
  214. case VIDIOC_QUERYCTRL:
  215. {
  216. struct v4l2_queryctrl *qc = arg;
  217. int i;
  218. for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
  219. if (qc->id && qc->id == radio_qctrl[i].id) {
  220. memcpy(qc, &(radio_qctrl[i]),
  221. sizeof(*qc));
  222. return (0);
  223. }
  224. }
  225. return -EINVAL;
  226. }
  227. case VIDIOC_G_CTRL:
  228. {
  229. struct v4l2_control *ctrl= arg;
  230. switch (ctrl->id) {
  231. case V4L2_CID_AUDIO_MUTE:
  232. if (az->curvol==0)
  233. ctrl->value=1;
  234. else
  235. ctrl->value=0;
  236. return (0);
  237. case V4L2_CID_AUDIO_VOLUME:
  238. ctrl->value=az->curvol * 6554;
  239. return (0);
  240. }
  241. return -EINVAL;
  242. }
  243. case VIDIOC_S_CTRL:
  244. {
  245. struct v4l2_control *ctrl= arg;
  246. switch (ctrl->id) {
  247. case V4L2_CID_AUDIO_MUTE:
  248. if (ctrl->value) {
  249. az_setvol(az,0);
  250. } else {
  251. az_setvol(az,az->curvol);
  252. }
  253. return (0);
  254. case V4L2_CID_AUDIO_VOLUME:
  255. az_setvol(az,ctrl->value);
  256. return (0);
  257. }
  258. return -EINVAL;
  259. }
  260. default:
  261. return v4l_compat_translate_ioctl(inode,file,cmd,arg,
  262. az_do_ioctl);
  263. }
  264. }
  265. static int az_ioctl(struct inode *inode, struct file *file,
  266. unsigned int cmd, unsigned long arg)
  267. {
  268. return video_usercopy(inode, file, cmd, arg, az_do_ioctl);
  269. }
  270. static struct az_device aztech_unit;
  271. static struct file_operations aztech_fops = {
  272. .owner = THIS_MODULE,
  273. .open = video_exclusive_open,
  274. .release = video_exclusive_release,
  275. .ioctl = az_ioctl,
  276. .compat_ioctl = v4l_compat_ioctl32,
  277. .llseek = no_llseek,
  278. };
  279. static struct video_device aztech_radio=
  280. {
  281. .owner = THIS_MODULE,
  282. .name = "Aztech radio",
  283. .type = VID_TYPE_TUNER,
  284. .hardware = 0,
  285. .fops = &aztech_fops,
  286. };
  287. static int __init aztech_init(void)
  288. {
  289. if(io==-1)
  290. {
  291. printk(KERN_ERR "You must set an I/O address with io=0x???\n");
  292. return -EINVAL;
  293. }
  294. if (!request_region(io, 2, "aztech"))
  295. {
  296. printk(KERN_ERR "aztech: port 0x%x already in use\n", io);
  297. return -EBUSY;
  298. }
  299. mutex_init(&lock);
  300. aztech_radio.priv=&aztech_unit;
  301. if(video_register_device(&aztech_radio, VFL_TYPE_RADIO, radio_nr)==-1)
  302. {
  303. release_region(io,2);
  304. return -EINVAL;
  305. }
  306. printk(KERN_INFO "Aztech radio card driver v1.00/19990224 rkroll@exploits.org\n");
  307. /* mute card - prevents noisy bootups */
  308. outb (0, io);
  309. return 0;
  310. }
  311. MODULE_AUTHOR("Russell Kroll, Quay Lu, Donald Song, Jason Lewis, Scott McGrath, William McGrath");
  312. MODULE_DESCRIPTION("A driver for the Aztech radio card.");
  313. MODULE_LICENSE("GPL");
  314. module_param(io, int, 0);
  315. module_param(radio_nr, int, 0);
  316. MODULE_PARM_DESC(io, "I/O address of the Aztech card (0x350 or 0x358)");
  317. static void __exit aztech_cleanup(void)
  318. {
  319. video_unregister_device(&aztech_radio);
  320. release_region(io,2);
  321. }
  322. module_init(aztech_init);
  323. module_exit(aztech_cleanup);