radio-aztech.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /* radio-aztech.c - Aztech radio card driver for Linux 2.2
  2. *
  3. * Adapted to support the Video for Linux API by
  4. * Russell Kroll <rkroll@exploits.org>. Based on original tuner code by:
  5. *
  6. * Quay Ly
  7. * Donald Song
  8. * Jason Lewis (jlewis@twilight.vtc.vsc.edu)
  9. * Scott McGrath (smcgrath@twilight.vtc.vsc.edu)
  10. * William McGrath (wmcgrath@twilight.vtc.vsc.edu)
  11. *
  12. * The basis for this code may be found at http://bigbang.vtc.vsc.edu/fmradio/
  13. * along with more information on the card itself.
  14. *
  15. * History:
  16. * 1999-02-24 Russell Kroll <rkroll@exploits.org>
  17. * Fine tuning/VIDEO_TUNER_LOW
  18. * Range expanded to 87-108 MHz (from 87.9-107.8)
  19. *
  20. * Notable changes from the original source:
  21. * - includes stripped down to the essentials
  22. * - for loops used as delays replaced with udelay()
  23. * - #defines removed, changed to static values
  24. * - tuning structure changed - no more character arrays, other changes
  25. */
  26. #include <linux/module.h> /* Modules */
  27. #include <linux/init.h> /* Initdata */
  28. #include <linux/ioport.h> /* request_region */
  29. #include <linux/delay.h> /* udelay */
  30. #include <asm/io.h> /* outb, outb_p */
  31. #include <asm/uaccess.h> /* copy to/from user */
  32. #include <linux/videodev.h> /* kernel radio structs */
  33. #include <linux/config.h> /* CONFIG_RADIO_AZTECH_PORT */
  34. /* acceptable ports: 0x350 (JP3 shorted), 0x358 (JP3 open) */
  35. #ifndef CONFIG_RADIO_AZTECH_PORT
  36. #define CONFIG_RADIO_AZTECH_PORT -1
  37. #endif
  38. static int io = CONFIG_RADIO_AZTECH_PORT;
  39. static int radio_nr = -1;
  40. static int radio_wait_time = 1000;
  41. static struct mutex lock;
  42. struct az_device
  43. {
  44. int curvol;
  45. unsigned long curfreq;
  46. int stereo;
  47. };
  48. static int volconvert(int level)
  49. {
  50. level>>=14; /* Map 16bits down to 2 bit */
  51. level&=3;
  52. /* convert to card-friendly values */
  53. switch (level)
  54. {
  55. case 0:
  56. return 0;
  57. case 1:
  58. return 1;
  59. case 2:
  60. return 4;
  61. case 3:
  62. return 5;
  63. }
  64. return 0; /* Quieten gcc */
  65. }
  66. static void send_0_byte (struct az_device *dev)
  67. {
  68. udelay(radio_wait_time);
  69. outb_p(2+volconvert(dev->curvol), io);
  70. outb_p(64+2+volconvert(dev->curvol), io);
  71. }
  72. static void send_1_byte (struct az_device *dev)
  73. {
  74. udelay (radio_wait_time);
  75. outb_p(128+2+volconvert(dev->curvol), io);
  76. outb_p(128+64+2+volconvert(dev->curvol), io);
  77. }
  78. static int az_setvol(struct az_device *dev, int vol)
  79. {
  80. mutex_lock(&lock);
  81. outb (volconvert(vol), io);
  82. mutex_unlock(&lock);
  83. return 0;
  84. }
  85. /* thanks to Michael Dwyer for giving me a dose of clues in
  86. * the signal strength department..
  87. *
  88. * This card has a stereo bit - bit 0 set = mono, not set = stereo
  89. * It also has a "signal" bit - bit 1 set = bad signal, not set = good
  90. *
  91. */
  92. static int az_getsigstr(struct az_device *dev)
  93. {
  94. if (inb(io) & 2) /* bit set = no signal present */
  95. return 0;
  96. return 1; /* signal present */
  97. }
  98. static int az_getstereo(struct az_device *dev)
  99. {
  100. if (inb(io) & 1) /* bit set = mono */
  101. return 0;
  102. return 1; /* stereo */
  103. }
  104. static int az_setfreq(struct az_device *dev, unsigned long frequency)
  105. {
  106. int i;
  107. frequency += 171200; /* Add 10.7 MHz IF */
  108. frequency /= 800; /* Convert to 50 kHz units */
  109. mutex_lock(&lock);
  110. send_0_byte (dev); /* 0: LSB of frequency */
  111. for (i = 0; i < 13; i++) /* : frequency bits (1-13) */
  112. if (frequency & (1 << i))
  113. send_1_byte (dev);
  114. else
  115. send_0_byte (dev);
  116. send_0_byte (dev); /* 14: test bit - always 0 */
  117. send_0_byte (dev); /* 15: test bit - always 0 */
  118. send_0_byte (dev); /* 16: band data 0 - always 0 */
  119. if (dev->stereo) /* 17: stereo (1 to enable) */
  120. send_1_byte (dev);
  121. else
  122. send_0_byte (dev);
  123. send_1_byte (dev); /* 18: band data 1 - unknown */
  124. send_0_byte (dev); /* 19: time base - always 0 */
  125. send_0_byte (dev); /* 20: spacing (0 = 25 kHz) */
  126. send_1_byte (dev); /* 21: spacing (1 = 25 kHz) */
  127. send_0_byte (dev); /* 22: spacing (0 = 25 kHz) */
  128. send_1_byte (dev); /* 23: AM/FM (FM = 1, always) */
  129. /* latch frequency */
  130. udelay (radio_wait_time);
  131. outb_p(128+64+volconvert(dev->curvol), io);
  132. mutex_unlock(&lock);
  133. return 0;
  134. }
  135. static int az_do_ioctl(struct inode *inode, struct file *file,
  136. unsigned int cmd, void *arg)
  137. {
  138. struct video_device *dev = video_devdata(file);
  139. struct az_device *az = dev->priv;
  140. switch(cmd)
  141. {
  142. case VIDIOCGCAP:
  143. {
  144. struct video_capability *v = arg;
  145. memset(v,0,sizeof(*v));
  146. v->type=VID_TYPE_TUNER;
  147. v->channels=1;
  148. v->audios=1;
  149. strcpy(v->name, "Aztech Radio");
  150. return 0;
  151. }
  152. case VIDIOCGTUNER:
  153. {
  154. struct video_tuner *v = arg;
  155. if(v->tuner) /* Only 1 tuner */
  156. return -EINVAL;
  157. v->rangelow=(87*16000);
  158. v->rangehigh=(108*16000);
  159. v->flags=VIDEO_TUNER_LOW;
  160. v->mode=VIDEO_MODE_AUTO;
  161. v->signal=0xFFFF*az_getsigstr(az);
  162. if(az_getstereo(az))
  163. v->flags|=VIDEO_TUNER_STEREO_ON;
  164. strcpy(v->name, "FM");
  165. return 0;
  166. }
  167. case VIDIOCSTUNER:
  168. {
  169. struct video_tuner *v = arg;
  170. if(v->tuner!=0)
  171. return -EINVAL;
  172. return 0;
  173. }
  174. case VIDIOCGFREQ:
  175. {
  176. unsigned long *freq = arg;
  177. *freq = az->curfreq;
  178. return 0;
  179. }
  180. case VIDIOCSFREQ:
  181. {
  182. unsigned long *freq = arg;
  183. az->curfreq = *freq;
  184. az_setfreq(az, az->curfreq);
  185. return 0;
  186. }
  187. case VIDIOCGAUDIO:
  188. {
  189. struct video_audio *v = arg;
  190. memset(v,0, sizeof(*v));
  191. v->flags|=VIDEO_AUDIO_MUTABLE|VIDEO_AUDIO_VOLUME;
  192. if(az->stereo)
  193. v->mode=VIDEO_SOUND_STEREO;
  194. else
  195. v->mode=VIDEO_SOUND_MONO;
  196. v->volume=az->curvol;
  197. v->step=16384;
  198. strcpy(v->name, "Radio");
  199. return 0;
  200. }
  201. case VIDIOCSAUDIO:
  202. {
  203. struct video_audio *v = arg;
  204. if(v->audio)
  205. return -EINVAL;
  206. az->curvol=v->volume;
  207. az->stereo=(v->mode&VIDEO_SOUND_STEREO)?1:0;
  208. if(v->flags&VIDEO_AUDIO_MUTE)
  209. az_setvol(az,0);
  210. else
  211. az_setvol(az,az->curvol);
  212. return 0;
  213. }
  214. default:
  215. return -ENOIOCTLCMD;
  216. }
  217. }
  218. static int az_ioctl(struct inode *inode, struct file *file,
  219. unsigned int cmd, unsigned long arg)
  220. {
  221. return video_usercopy(inode, file, cmd, arg, az_do_ioctl);
  222. }
  223. static struct az_device aztech_unit;
  224. static struct file_operations aztech_fops = {
  225. .owner = THIS_MODULE,
  226. .open = video_exclusive_open,
  227. .release = video_exclusive_release,
  228. .ioctl = az_ioctl,
  229. .compat_ioctl = v4l_compat_ioctl32,
  230. .llseek = no_llseek,
  231. };
  232. static struct video_device aztech_radio=
  233. {
  234. .owner = THIS_MODULE,
  235. .name = "Aztech radio",
  236. .type = VID_TYPE_TUNER,
  237. .hardware = VID_HARDWARE_AZTECH,
  238. .fops = &aztech_fops,
  239. };
  240. static int __init aztech_init(void)
  241. {
  242. if(io==-1)
  243. {
  244. printk(KERN_ERR "You must set an I/O address with io=0x???\n");
  245. return -EINVAL;
  246. }
  247. if (!request_region(io, 2, "aztech"))
  248. {
  249. printk(KERN_ERR "aztech: port 0x%x already in use\n", io);
  250. return -EBUSY;
  251. }
  252. mutex_init(&lock);
  253. aztech_radio.priv=&aztech_unit;
  254. if(video_register_device(&aztech_radio, VFL_TYPE_RADIO, radio_nr)==-1)
  255. {
  256. release_region(io,2);
  257. return -EINVAL;
  258. }
  259. printk(KERN_INFO "Aztech radio card driver v1.00/19990224 rkroll@exploits.org\n");
  260. /* mute card - prevents noisy bootups */
  261. outb (0, io);
  262. return 0;
  263. }
  264. MODULE_AUTHOR("Russell Kroll, Quay Lu, Donald Song, Jason Lewis, Scott McGrath, William McGrath");
  265. MODULE_DESCRIPTION("A driver for the Aztech radio card.");
  266. MODULE_LICENSE("GPL");
  267. module_param(io, int, 0);
  268. module_param(radio_nr, int, 0);
  269. MODULE_PARM_DESC(io, "I/O address of the Aztech card (0x350 or 0x358)");
  270. static void __exit aztech_cleanup(void)
  271. {
  272. video_unregister_device(&aztech_radio);
  273. release_region(io,2);
  274. }
  275. module_init(aztech_init);
  276. module_exit(aztech_cleanup);