radio-aztech.c 7.3 KB

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