radio-maxiradio.c
上传用户:lgb322
上传日期:2013-02-24
资源大小:30529k
文件大小:9k
源码类别:

嵌入式Linux

开发平台:

Unix_Linux

  1. /* 
  2.  * Guillemot Maxi Radio FM 2000 PCI radio card driver for Linux 
  3.  * (C) 2001 Dimitromanolakis Apostolos <apdim@grecian.net>
  4.  *
  5.  * Based in the radio Maestro PCI driver. Actually it uses the same chip
  6.  * for radio but different pci controller.
  7.  *
  8.  * I didn't have any specs I reversed engineered the protocol from
  9.  * the windows driver (radio.dll). 
  10.  *
  11.  * The card uses the TEA5757 chip that includes a search function but it
  12.  * is useless as I haven't found any way to read back the frequency. If 
  13.  * anybody does please mail me.
  14.  *
  15.  * For the pdf file see:
  16.  * http://www.semiconductors.philips.com/pip/TEA5757H/V1
  17.  *
  18.  *
  19.  * CHANGES:
  20.  *   0.75b
  21.  *     - better pci interface thanks to Francois Romieu <romieu@cogenit.fr>
  22.  *
  23.  *   0.75
  24.  *     - tiding up
  25.  *     - removed support for multiple devices as it didn't work anyway
  26.  *
  27.  * BUGS: 
  28.  *   - card unmutes if you change frequency
  29.  *
  30.  */
  31. #include <linux/module.h>
  32. #include <linux/init.h>
  33. #include <linux/ioport.h>
  34. #include <linux/delay.h>
  35. #include <linux/sched.h>
  36. #include <asm/io.h>
  37. #include <asm/uaccess.h>
  38. #include <asm/semaphore.h>
  39. #include <linux/pci.h>
  40. #include <linux/videodev.h>
  41. /* version 0.75      Sun Feb  4 22:51:27 EET 2001 */
  42. #define DRIVER_VERSION "0.75"
  43. #ifndef PCI_VENDOR_ID_GUILLEMOT
  44. #define PCI_VENDOR_ID_GUILLEMOT 0x5046
  45. #endif
  46. #ifndef PCI_DEVICE_ID_GUILLEMOT
  47. #define PCI_DEVICE_ID_GUILLEMOT_MAXIRADIO 0x1001
  48. #endif
  49. /* TEA5757 pin mappings */
  50. const int clk = 1, data = 2, wren = 4, mo_st = 8, power = 16 ;
  51. static int radio_nr = -1;
  52. MODULE_PARM(radio_nr, "i");
  53. #define FREQ_LO  50*16000
  54. #define FREQ_HI 150*16000
  55. #define FREQ_IF         171200 /* 10.7*16000   */
  56. #define FREQ_STEP       200    /* 12.5*16      */
  57. #define FREQ2BITS(x) ((( (unsigned int)(x)+FREQ_IF+(FREQ_STEP<<1))
  58. /(FREQ_STEP<<2))<<2) /* (x==fmhz*16*1000) -> bits */
  59. #define BITS2FREQ(x) ((x) * FREQ_STEP - FREQ_IF)
  60. static int radio_open(struct video_device *, int);
  61. static int radio_ioctl(struct video_device *, unsigned int, void *);
  62. static void radio_close(struct video_device *);
  63. static struct video_device maxiradio_radio=
  64. {
  65. owner: THIS_MODULE,
  66. name: "Maxi Radio FM2000 radio",
  67. type: VID_TYPE_TUNER,
  68. hardware: VID_HARDWARE_SF16MI,
  69. open: radio_open,
  70. close: radio_close,
  71. ioctl: radio_ioctl,
  72. };
  73. static struct radio_device
  74. {
  75. __u16 io, /* base of radio io */
  76. muted, /* VIDEO_AUDIO_MUTE */
  77. stereo, /* VIDEO_TUNER_STEREO_ON */
  78. tuned; /* signal strength (0 or 0xffff) */
  79. unsigned long freq;
  80. struct  semaphore lock;
  81. } radio_unit = {0, 0, 0, 0, };
  82. static void sleep_125ms(void)
  83. {
  84. current->state = TASK_INTERRUPTIBLE;
  85. schedule_timeout(HZ >> 3);
  86. }
  87. static void outbit(unsigned long bit, __u16 io)
  88. {
  89. if(bit != 0)
  90. {
  91. outb(  power|wren|data     ,io); udelay(4);
  92. outb(  power|wren|data|clk ,io); udelay(4);
  93. outb(  power|wren|data     ,io); udelay(4);
  94. }
  95. else
  96. {
  97. outb(  power|wren          ,io); udelay(4);
  98. outb(  power|wren|clk      ,io); udelay(4);
  99. outb(  power|wren          ,io); udelay(4);
  100. }
  101. }
  102. static void turn_power(__u16 io, int p)
  103. {
  104. if(p != 0) outb(power, io); else outb(0,io);
  105. }
  106. static void set_freq(__u16 io, __u32 data)
  107. {
  108. unsigned long int si;
  109. int bl;
  110. /* TEA5757 shift register bits (see pdf) */
  111. outbit(0,io); // 24  search 
  112. outbit(1,io); // 23  search up/down
  113. outbit(0,io); // 22  stereo/mono
  114. outbit(0,io); // 21  band
  115. outbit(0,io); // 20  band (only 00=FM works I think)
  116. outbit(0,io); // 19  port ?
  117. outbit(0,io); // 18  port ?
  118. outbit(0,io); // 17  search level
  119. outbit(0,io); // 16  search level
  120.  
  121. si = 0x8000;
  122. for(bl = 1; bl <= 16 ; bl++) { outbit(data & si,io); si >>=1; }
  123. outb(power,io);
  124. }
  125. static int get_stereo(__u16 io)
  126. {
  127. outb(power,io); udelay(4);
  128. return !(inb(io) & mo_st);
  129. }
  130. static int get_tune(__u16 io)
  131. {
  132. outb(power+clk,io); udelay(4);
  133. return !(inb(io) & mo_st);
  134. }
  135. inline static int radio_function(struct video_device *dev, 
  136.  unsigned int cmd, void *arg)
  137. {
  138. struct radio_device *card=dev->priv;
  139. switch(cmd) {
  140. case VIDIOCGCAP: {
  141. struct video_capability v;
  142. strcpy(v.name, "Maxi Radio FM2000 radio");
  143. v.type=VID_TYPE_TUNER;
  144. v.channels=v.audios=1;
  145. v.maxwidth=v.maxheight=v.minwidth=v.minheight=0;
  146. if(copy_to_user(arg,&v,sizeof(v)))
  147. return -EFAULT;
  148. return 0;
  149. }
  150. case VIDIOCGTUNER: {
  151. struct video_tuner v;
  152. if(copy_from_user(&v, arg,sizeof(v))!=0)
  153. return -EFAULT;
  154. if(v.tuner)
  155. return -EINVAL;
  156. card->stereo = 0xffff * get_stereo(card->io);
  157. card->tuned = 0xffff * get_tune(card->io);
  158. v.flags = VIDEO_TUNER_LOW | card->stereo;
  159. v.signal = card->tuned;
  160. strcpy(v.name, "FM");
  161. v.rangelow = FREQ_LO;
  162. v.rangehigh = FREQ_HI;
  163. v.mode = VIDEO_MODE_AUTO;
  164. if(copy_to_user(arg,&v, sizeof(v)))
  165. return -EFAULT;
  166.   return 0;
  167. }
  168. case VIDIOCSTUNER: {
  169. struct video_tuner v;
  170. if(copy_from_user(&v, arg, sizeof(v)))
  171. return -EFAULT;
  172. if(v.tuner!=0)
  173. return -EINVAL;
  174. return 0;
  175. }
  176. case VIDIOCGFREQ: {
  177. unsigned long tmp=card->freq;
  178. if(copy_to_user(arg, &tmp, sizeof(tmp)))
  179. return -EFAULT;
  180. return 0;
  181. }
  182. case VIDIOCSFREQ: {
  183. unsigned long tmp;
  184. if(copy_from_user(&tmp, arg, sizeof(tmp)))
  185. return -EFAULT;
  186. if ( tmp<FREQ_LO || tmp>FREQ_HI )
  187. return -EINVAL;
  188. card->freq = tmp;
  189. set_freq(card->io, FREQ2BITS(card->freq));
  190. sleep_125ms();
  191. return 0;
  192. }
  193. case VIDIOCGAUDIO: {
  194. struct video_audio v;
  195. strcpy(v.name, "Radio");
  196. v.audio=v.volume=v.bass=v.treble=v.balance=v.step=0;
  197. v.flags=VIDEO_AUDIO_MUTABLE | card->muted;
  198. v.mode=VIDEO_SOUND_STEREO;
  199. if(copy_to_user(arg,&v, sizeof(v)))
  200. return -EFAULT;
  201. return 0;
  202. }
  203. case VIDIOCSAUDIO: {
  204. struct video_audio v;
  205. if(copy_from_user(&v, arg, sizeof(v)))
  206. return -EFAULT;
  207. if(v.audio)
  208. return -EINVAL;
  209. card->muted = v.flags & VIDEO_AUDIO_MUTE;
  210. if(card->muted)
  211. turn_power(card->io, 0);
  212. else
  213. set_freq(card->io, FREQ2BITS(card->freq));
  214. return 0;
  215. }
  216. case VIDIOCGUNIT: {
  217. struct video_unit v;
  218. v.video=VIDEO_NO_UNIT;
  219. v.vbi=VIDEO_NO_UNIT;
  220. v.radio=dev->minor;
  221. v.audio=0;
  222. v.teletext=VIDEO_NO_UNIT;
  223. if(copy_to_user(arg, &v, sizeof(v)))
  224. return -EFAULT;
  225. return 0;
  226. }
  227. default: return -ENOIOCTLCMD;
  228. }
  229. }
  230. static int radio_ioctl(struct video_device *dev, unsigned int cmd, void *arg)
  231. {
  232. struct radio_device *card=dev->priv;
  233. int ret;
  234. down(&card->lock);
  235. ret = radio_function(dev, cmd, arg);
  236. up(&card->lock);
  237. return ret;
  238. }
  239. static int radio_open(struct video_device *dev, int flags)
  240. {
  241. return 0;
  242. }
  243. static void radio_close(struct video_device *dev)
  244. {
  245. }
  246. MODULE_AUTHOR("Dimitromanolakis Apostolos, apdim@grecian.net");
  247. MODULE_DESCRIPTION("Radio driver for the Guillemot Maxi Radio FM2000 radio.");
  248. MODULE_LICENSE("GPL");
  249. EXPORT_NO_SYMBOLS;
  250. static int __devinit maxiradio_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  251. {
  252. if(!request_region(pci_resource_start(pdev, 0),
  253.                    pci_resource_len(pdev, 0), "Maxi Radio FM 2000")) {
  254.         printk(KERN_ERR "radio-maxiradio: can't reserve I/O portsn");
  255.         goto err_out;
  256. }
  257. if (pci_enable_device(pdev))
  258.         goto err_out_free_region;
  259. radio_unit.io = pci_resource_start(pdev, 0);
  260. init_MUTEX(&radio_unit.lock);
  261. maxiradio_radio.priv = &radio_unit;
  262. if(video_register_device(&maxiradio_radio, VFL_TYPE_RADIO, radio_nr)==-1) {
  263.         printk("radio-maxiradio: can't register device!");
  264.         goto err_out_free_region;
  265. }
  266. printk(KERN_INFO "radio-maxiradio: version "
  267.        DRIVER_VERSION
  268.        " time "
  269.        __TIME__ "  "
  270.        __DATE__
  271.        "n");
  272. printk(KERN_INFO "radio-maxiradio: found Guillemot MAXI Radio device (io = 0x%x)n",
  273.        radio_unit.io);
  274. return 0;
  275. err_out_free_region:
  276. release_region(pci_resource_start(pdev, 0), pci_resource_len(pdev, 0));
  277. err_out:
  278. return -ENODEV;
  279. }
  280. static void __devexit maxiradio_remove_one(struct pci_dev *pdev)
  281. {
  282. video_unregister_device(&maxiradio_radio);
  283. release_region(pci_resource_start(pdev, 0), pci_resource_len(pdev, 0));
  284. }
  285. static struct pci_device_id maxiradio_pci_tbl[] __devinitdata = {
  286. { PCI_VENDOR_ID_GUILLEMOT, PCI_DEVICE_ID_GUILLEMOT_MAXIRADIO,
  287. PCI_ANY_ID, PCI_ANY_ID, },
  288. { 0,}
  289. };
  290. MODULE_DEVICE_TABLE(pci, maxiradio_pci_tbl);
  291. static struct pci_driver maxiradio_driver = {
  292. name: "radio-maxiradio",
  293. id_table: maxiradio_pci_tbl,
  294. probe: maxiradio_init_one,
  295. remove: __devexit_p(maxiradio_remove_one),
  296. };
  297. int __init maxiradio_radio_init(void)
  298. {
  299. return pci_module_init(&maxiradio_driver);
  300. }
  301. void __exit maxiradio_radio_exit(void)
  302. {
  303. pci_unregister_driver(&maxiradio_driver);
  304. }
  305. module_init(maxiradio_radio_init);
  306. module_exit(maxiradio_radio_exit);