radio-gemtek-pci.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /*
  2. ***************************************************************************
  3. *
  4. * radio-gemtek-pci.c - Gemtek PCI Radio driver
  5. * (C) 2001 Vladimir Shebordaev <vshebordaev@mail.ru>
  6. *
  7. ***************************************************************************
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of
  12. * the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public
  20. * License along with this program; if not, write to the Free
  21. * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
  22. * USA.
  23. *
  24. ***************************************************************************
  25. *
  26. * Gemtek Corp still silently refuses to release any specifications
  27. * of their multimedia devices, so the protocol still has to be
  28. * reverse engineered.
  29. *
  30. * The v4l code was inspired by Jonas Munsin's Gemtek serial line
  31. * radio device driver.
  32. *
  33. * Please, let me know if this piece of code was useful :)
  34. *
  35. * TODO: multiple device support and portability were not tested
  36. *
  37. ***************************************************************************
  38. */
  39. #include <linux/config.h>
  40. #include <linux/types.h>
  41. #include <linux/list.h>
  42. #include <linux/module.h>
  43. #include <linux/init.h>
  44. #include <linux/pci.h>
  45. #include <linux/videodev.h>
  46. #include <linux/errno.h>
  47. #include <asm/io.h>
  48. #include <asm/uaccess.h>
  49. #ifndef PCI_VENDOR_ID_GEMTEK
  50. #define PCI_VENDOR_ID_GEMTEK 0x5046
  51. #endif
  52. #ifndef PCI_DEVICE_ID_GEMTEK_PR103
  53. #define PCI_DEVICE_ID_GEMTEK_PR103 0x1001
  54. #endif
  55. #ifndef GEMTEK_PCI_RANGE_LOW
  56. #define GEMTEK_PCI_RANGE_LOW (87*16000)
  57. #endif
  58. #ifndef GEMTEK_PCI_RANGE_HIGH
  59. #define GEMTEK_PCI_RANGE_HIGH (108*16000)
  60. #endif
  61. #ifndef TRUE
  62. #define TRUE (1)
  63. #endif
  64. #ifndef FALSE
  65. #define FALSE (0)
  66. #endif
  67. struct gemtek_pci_card {
  68. struct video_device *videodev;
  69. u32 iobase;
  70. u32 length;
  71. u8 chiprev;
  72. u16 model;
  73. u32 current_frequency;
  74. u8 mute;
  75. };
  76. static const char rcsid[] = "$Id: radio-gemtek-pci.c,v 1.1 2001/07/23 08:08:16 ted Exp ted $";
  77. static int nr_radio = -1;
  78. static inline u8 gemtek_pci_out( u16 value, u32 port )
  79. {
  80. outw( value, port );
  81. return (u8)value;
  82. }
  83. #define _b0( v ) *((u8 *)&v)
  84. static void __gemtek_pci_cmd( u16 value, u32 port, u8 *last_byte, int keep )
  85. {
  86. register u8 byte = *last_byte;
  87. if ( !value ) {
  88. if ( !keep )
  89. value = (u16)port;
  90. byte &= 0xfd;
  91. } else
  92. byte |= 2;
  93. _b0( value ) = byte;
  94. outw( value, port );
  95. byte |= 1;
  96. _b0( value ) = byte;
  97. outw( value, port );
  98. byte &= 0xfe;
  99. _b0( value ) = byte;
  100. outw( value, port );
  101. *last_byte = byte;
  102. }
  103. static inline void gemtek_pci_nil( u32 port, u8 *last_byte )
  104. {
  105. __gemtek_pci_cmd( 0x00, port, last_byte, FALSE );
  106. }
  107. static inline void gemtek_pci_cmd( u16 cmd, u32 port, u8 *last_byte )
  108. {
  109. __gemtek_pci_cmd( cmd, port, last_byte, TRUE );
  110. }
  111. static void gemtek_pci_setfrequency( struct gemtek_pci_card *card, unsigned long frequency )
  112. {
  113. register int i;
  114. register u32 value = frequency / 200 + 856;
  115. register u16 mask = 0x8000;
  116. u8 last_byte;
  117. u32 port = card->iobase;
  118. last_byte = gemtek_pci_out( 0x06, port );
  119. i = 0;
  120. do {
  121. gemtek_pci_nil( port, &last_byte );
  122. i++;
  123. } while ( i < 9 );
  124. i = 0;
  125. do {
  126. gemtek_pci_cmd( value & mask, port, &last_byte );
  127. mask >>= 1;
  128. i++;
  129. } while ( i < 16 );
  130. outw( 0x10, port );
  131. }
  132. static inline void gemtek_pci_mute( struct gemtek_pci_card *card )
  133. {
  134. outb( 0x1f, card->iobase );
  135. card->mute = TRUE;
  136. }
  137. static inline void gemtek_pci_unmute( struct gemtek_pci_card *card )
  138. {
  139. if ( card->mute ) {
  140. gemtek_pci_setfrequency( card, card->current_frequency );
  141. card->mute = FALSE;
  142. }
  143. }
  144. static inline unsigned int gemtek_pci_getsignal( struct gemtek_pci_card *card )
  145. {
  146. return ( inb( card->iobase ) & 0x08 ) ? 0 : 1;
  147. }
  148. static int gemtek_pci_do_ioctl(struct inode *inode, struct file *file,
  149. unsigned int cmd, void *arg)
  150. {
  151. struct video_device *dev = video_devdata(file);
  152. struct gemtek_pci_card *card = dev->priv;
  153. switch ( cmd ) {
  154. case VIDIOCGCAP:
  155. {
  156. struct video_capability *c = arg;
  157. memset(c,0,sizeof(*c));
  158. c->type = VID_TYPE_TUNER;
  159. c->channels = 1;
  160. c->audios = 1;
  161. strcpy( c->name, "Gemtek PCI Radio" );
  162. return 0;
  163. }
  164. case VIDIOCGTUNER:
  165. {
  166. struct video_tuner *t = arg;
  167. if ( t->tuner )
  168. return -EINVAL;
  169. t->rangelow = GEMTEK_PCI_RANGE_LOW;
  170. t->rangehigh = GEMTEK_PCI_RANGE_HIGH;
  171. t->flags = VIDEO_TUNER_LOW;
  172. t->mode = VIDEO_MODE_AUTO;
  173. t->signal = 0xFFFF * gemtek_pci_getsignal( card );
  174. strcpy( t->name, "FM" );
  175. return 0;
  176. }
  177. case VIDIOCSTUNER:
  178. {
  179. struct video_tuner *t = arg;
  180. if ( t->tuner )
  181. return -EINVAL;
  182. return 0;
  183. }
  184. case VIDIOCGFREQ:
  185. {
  186. unsigned long *freq = arg;
  187. *freq = card->current_frequency;
  188. return 0;
  189. }
  190. case VIDIOCSFREQ:
  191. {
  192. unsigned long *freq = arg;
  193. if ( (*freq < GEMTEK_PCI_RANGE_LOW) ||
  194. (*freq > GEMTEK_PCI_RANGE_HIGH) )
  195. return -EINVAL;
  196. gemtek_pci_setfrequency( card, *freq );
  197. card->current_frequency = *freq;
  198. card->mute = FALSE;
  199. return 0;
  200. }
  201. case VIDIOCGAUDIO:
  202. {
  203. struct video_audio *a = arg;
  204. memset( a, 0, sizeof( *a ) );
  205. a->flags |= VIDEO_AUDIO_MUTABLE;
  206. a->volume = 1;
  207. a->step = 65535;
  208. strcpy( a->name, "Radio" );
  209. return 0;
  210. }
  211. case VIDIOCSAUDIO:
  212. {
  213. struct video_audio *a = arg;
  214. if ( a->audio )
  215. return -EINVAL;
  216. if ( a->flags & VIDEO_AUDIO_MUTE )
  217. gemtek_pci_mute( card );
  218. else
  219. gemtek_pci_unmute( card );
  220. return 0;
  221. }
  222. default:
  223. return -ENOIOCTLCMD;
  224. }
  225. }
  226. static int gemtek_pci_ioctl(struct inode *inode, struct file *file,
  227. unsigned int cmd, unsigned long arg)
  228. {
  229. return video_usercopy(inode, file, cmd, arg, gemtek_pci_do_ioctl);
  230. }
  231. enum {
  232. GEMTEK_PR103
  233. };
  234. static char *card_names[] __devinitdata = {
  235. "GEMTEK_PR103"
  236. };
  237. static struct pci_device_id gemtek_pci_id[] =
  238. {
  239. { PCI_VENDOR_ID_GEMTEK, PCI_DEVICE_ID_GEMTEK_PR103,
  240. PCI_ANY_ID, PCI_ANY_ID, 0, 0, GEMTEK_PR103 },
  241. { 0 }
  242. };
  243. MODULE_DEVICE_TABLE( pci, gemtek_pci_id );
  244. static int mx = 1;
  245. static struct file_operations gemtek_pci_fops = {
  246. .owner = THIS_MODULE,
  247. .open = video_exclusive_open,
  248. .release = video_exclusive_release,
  249. .ioctl = gemtek_pci_ioctl,
  250. .compat_ioctl = v4l_compat_ioctl32,
  251. .llseek = no_llseek,
  252. };
  253. static struct video_device vdev_template = {
  254. .owner = THIS_MODULE,
  255. .name = "Gemtek PCI Radio",
  256. .type = VID_TYPE_TUNER,
  257. .hardware = VID_HARDWARE_GEMTEK,
  258. .fops = &gemtek_pci_fops,
  259. };
  260. static int __devinit gemtek_pci_probe( struct pci_dev *pci_dev, const struct pci_device_id *pci_id )
  261. {
  262. struct gemtek_pci_card *card;
  263. struct video_device *devradio;
  264. if ( (card = kzalloc( sizeof( struct gemtek_pci_card ), GFP_KERNEL )) == NULL ) {
  265. printk( KERN_ERR "gemtek_pci: out of memory\n" );
  266. return -ENOMEM;
  267. }
  268. if ( pci_enable_device( pci_dev ) )
  269. goto err_pci;
  270. card->iobase = pci_resource_start( pci_dev, 0 );
  271. card->length = pci_resource_len( pci_dev, 0 );
  272. if ( request_region( card->iobase, card->length, card_names[pci_id->driver_data] ) == NULL ) {
  273. printk( KERN_ERR "gemtek_pci: i/o port already in use\n" );
  274. goto err_pci;
  275. }
  276. pci_read_config_byte( pci_dev, PCI_REVISION_ID, &card->chiprev );
  277. pci_read_config_word( pci_dev, PCI_SUBSYSTEM_ID, &card->model );
  278. pci_set_drvdata( pci_dev, card );
  279. if ( (devradio = kmalloc( sizeof( struct video_device ), GFP_KERNEL )) == NULL ) {
  280. printk( KERN_ERR "gemtek_pci: out of memory\n" );
  281. goto err_video;
  282. }
  283. *devradio = vdev_template;
  284. if ( video_register_device( devradio, VFL_TYPE_RADIO , nr_radio) == -1 ) {
  285. kfree( devradio );
  286. goto err_video;
  287. }
  288. card->videodev = devradio;
  289. devradio->priv = card;
  290. gemtek_pci_mute( card );
  291. printk( KERN_INFO "Gemtek PCI Radio (rev. %d) found at 0x%04x-0x%04x.\n",
  292. card->chiprev, card->iobase, card->iobase + card->length - 1 );
  293. return 0;
  294. err_video:
  295. release_region( card->iobase, card->length );
  296. err_pci:
  297. kfree( card );
  298. return -ENODEV;
  299. }
  300. static void __devexit gemtek_pci_remove( struct pci_dev *pci_dev )
  301. {
  302. struct gemtek_pci_card *card = pci_get_drvdata( pci_dev );
  303. video_unregister_device( card->videodev );
  304. kfree( card->videodev );
  305. release_region( card->iobase, card->length );
  306. if ( mx )
  307. gemtek_pci_mute( card );
  308. kfree( card );
  309. pci_set_drvdata( pci_dev, NULL );
  310. }
  311. static struct pci_driver gemtek_pci_driver =
  312. {
  313. .name = "gemtek_pci",
  314. .id_table = gemtek_pci_id,
  315. .probe = gemtek_pci_probe,
  316. .remove = __devexit_p(gemtek_pci_remove),
  317. };
  318. static int __init gemtek_pci_init_module( void )
  319. {
  320. return pci_register_driver( &gemtek_pci_driver );
  321. }
  322. static void __exit gemtek_pci_cleanup_module( void )
  323. {
  324. return pci_unregister_driver( &gemtek_pci_driver );
  325. }
  326. MODULE_AUTHOR( "Vladimir Shebordaev <vshebordaev@mail.ru>" );
  327. MODULE_DESCRIPTION( "The video4linux driver for the Gemtek PCI Radio Card" );
  328. MODULE_LICENSE("GPL");
  329. module_param(mx, bool, 0);
  330. MODULE_PARM_DESC( mx, "single digit: 1 - turn off the turner upon module exit (default), 0 - do not" );
  331. module_param(nr_radio, int, 0);
  332. MODULE_PARM_DESC( nr_radio, "video4linux device number to use");
  333. module_init( gemtek_pci_init_module );
  334. module_exit( gemtek_pci_cleanup_module );