radio-gemtek-pci.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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. * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
  38. *
  39. ***************************************************************************
  40. */
  41. #include <linux/types.h>
  42. #include <linux/list.h>
  43. #include <linux/module.h>
  44. #include <linux/init.h>
  45. #include <linux/pci.h>
  46. #include <linux/videodev2.h>
  47. #include <media/v4l2-common.h>
  48. #include <linux/errno.h>
  49. #include <linux/version.h> /* for KERNEL_VERSION MACRO */
  50. #define RADIO_VERSION KERNEL_VERSION(0,0,2)
  51. static struct v4l2_queryctrl radio_qctrl[] = {
  52. {
  53. .id = V4L2_CID_AUDIO_MUTE,
  54. .name = "Mute",
  55. .minimum = 0,
  56. .maximum = 1,
  57. .default_value = 1,
  58. .type = V4L2_CTRL_TYPE_BOOLEAN,
  59. },{
  60. .id = V4L2_CID_AUDIO_VOLUME,
  61. .name = "Volume",
  62. .minimum = 0,
  63. .maximum = 65535,
  64. .step = 65535,
  65. .default_value = 0xff,
  66. .type = V4L2_CTRL_TYPE_INTEGER,
  67. }
  68. };
  69. #include <asm/io.h>
  70. #include <asm/uaccess.h>
  71. #ifndef PCI_VENDOR_ID_GEMTEK
  72. #define PCI_VENDOR_ID_GEMTEK 0x5046
  73. #endif
  74. #ifndef PCI_DEVICE_ID_GEMTEK_PR103
  75. #define PCI_DEVICE_ID_GEMTEK_PR103 0x1001
  76. #endif
  77. #ifndef GEMTEK_PCI_RANGE_LOW
  78. #define GEMTEK_PCI_RANGE_LOW (87*16000)
  79. #endif
  80. #ifndef GEMTEK_PCI_RANGE_HIGH
  81. #define GEMTEK_PCI_RANGE_HIGH (108*16000)
  82. #endif
  83. #ifndef TRUE
  84. #define TRUE (1)
  85. #endif
  86. #ifndef FALSE
  87. #define FALSE (0)
  88. #endif
  89. struct gemtek_pci_card {
  90. struct video_device *videodev;
  91. u32 iobase;
  92. u32 length;
  93. u8 chiprev;
  94. u16 model;
  95. u32 current_frequency;
  96. u8 mute;
  97. };
  98. static const char rcsid[] = "$Id: radio-gemtek-pci.c,v 1.1 2001/07/23 08:08:16 ted Exp ted $";
  99. static int nr_radio = -1;
  100. static inline u8 gemtek_pci_out( u16 value, u32 port )
  101. {
  102. outw( value, port );
  103. return (u8)value;
  104. }
  105. #define _b0( v ) *((u8 *)&v)
  106. static void __gemtek_pci_cmd( u16 value, u32 port, u8 *last_byte, int keep )
  107. {
  108. register u8 byte = *last_byte;
  109. if ( !value ) {
  110. if ( !keep )
  111. value = (u16)port;
  112. byte &= 0xfd;
  113. } else
  114. byte |= 2;
  115. _b0( value ) = byte;
  116. outw( value, port );
  117. byte |= 1;
  118. _b0( value ) = byte;
  119. outw( value, port );
  120. byte &= 0xfe;
  121. _b0( value ) = byte;
  122. outw( value, port );
  123. *last_byte = byte;
  124. }
  125. static inline void gemtek_pci_nil( u32 port, u8 *last_byte )
  126. {
  127. __gemtek_pci_cmd( 0x00, port, last_byte, FALSE );
  128. }
  129. static inline void gemtek_pci_cmd( u16 cmd, u32 port, u8 *last_byte )
  130. {
  131. __gemtek_pci_cmd( cmd, port, last_byte, TRUE );
  132. }
  133. static void gemtek_pci_setfrequency( struct gemtek_pci_card *card, unsigned long frequency )
  134. {
  135. register int i;
  136. register u32 value = frequency / 200 + 856;
  137. register u16 mask = 0x8000;
  138. u8 last_byte;
  139. u32 port = card->iobase;
  140. last_byte = gemtek_pci_out( 0x06, port );
  141. i = 0;
  142. do {
  143. gemtek_pci_nil( port, &last_byte );
  144. i++;
  145. } while ( i < 9 );
  146. i = 0;
  147. do {
  148. gemtek_pci_cmd( value & mask, port, &last_byte );
  149. mask >>= 1;
  150. i++;
  151. } while ( i < 16 );
  152. outw( 0x10, port );
  153. }
  154. static inline void gemtek_pci_mute( struct gemtek_pci_card *card )
  155. {
  156. outb( 0x1f, card->iobase );
  157. card->mute = TRUE;
  158. }
  159. static inline void gemtek_pci_unmute( struct gemtek_pci_card *card )
  160. {
  161. if ( card->mute ) {
  162. gemtek_pci_setfrequency( card, card->current_frequency );
  163. card->mute = FALSE;
  164. }
  165. }
  166. static inline unsigned int gemtek_pci_getsignal( struct gemtek_pci_card *card )
  167. {
  168. return ( inb( card->iobase ) & 0x08 ) ? 0 : 1;
  169. }
  170. static int gemtek_pci_do_ioctl(struct inode *inode, struct file *file,
  171. unsigned int cmd, void *arg)
  172. {
  173. struct video_device *dev = video_devdata(file);
  174. struct gemtek_pci_card *card = dev->priv;
  175. switch ( cmd ) {
  176. case VIDIOC_QUERYCAP:
  177. {
  178. struct v4l2_capability *v = arg;
  179. memset(v,0,sizeof(*v));
  180. strlcpy(v->driver, "radio-gemtek-pci", sizeof (v->driver));
  181. strlcpy(v->card, "GemTek PCI Radio", sizeof (v->card));
  182. sprintf(v->bus_info,"ISA");
  183. v->version = RADIO_VERSION;
  184. v->capabilities = V4L2_CAP_TUNER;
  185. return 0;
  186. }
  187. case VIDIOC_G_TUNER:
  188. {
  189. struct v4l2_tuner *v = arg;
  190. if (v->index > 0)
  191. return -EINVAL;
  192. memset(v,0,sizeof(*v));
  193. strcpy(v->name, "FM");
  194. v->type = V4L2_TUNER_RADIO;
  195. v->rangelow = GEMTEK_PCI_RANGE_LOW;
  196. v->rangehigh = GEMTEK_PCI_RANGE_HIGH;
  197. v->rxsubchans =V4L2_TUNER_SUB_MONO;
  198. v->capability=V4L2_TUNER_CAP_LOW;
  199. v->audmode = V4L2_TUNER_MODE_MONO;
  200. v->signal=0xFFFF*gemtek_pci_getsignal( card );
  201. return 0;
  202. }
  203. case VIDIOC_S_TUNER:
  204. {
  205. struct v4l2_tuner *v = arg;
  206. if (v->index > 0)
  207. return -EINVAL;
  208. return 0;
  209. }
  210. case VIDIOC_S_FREQUENCY:
  211. {
  212. struct v4l2_frequency *f = arg;
  213. if ( (f->frequency < GEMTEK_PCI_RANGE_LOW) ||
  214. (f->frequency > GEMTEK_PCI_RANGE_HIGH) )
  215. return -EINVAL;
  216. gemtek_pci_setfrequency( card, f->frequency );
  217. card->current_frequency = f->frequency;
  218. card->mute = FALSE;
  219. return 0;
  220. }
  221. case VIDIOC_QUERYCTRL:
  222. {
  223. struct v4l2_queryctrl *qc = arg;
  224. int i;
  225. for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
  226. if (qc->id && qc->id == radio_qctrl[i].id) {
  227. memcpy(qc, &(radio_qctrl[i]),
  228. sizeof(*qc));
  229. return (0);
  230. }
  231. }
  232. return -EINVAL;
  233. }
  234. case VIDIOC_G_CTRL:
  235. {
  236. struct v4l2_control *ctrl= arg;
  237. switch (ctrl->id) {
  238. case V4L2_CID_AUDIO_MUTE:
  239. ctrl->value=card->mute;
  240. return (0);
  241. case V4L2_CID_AUDIO_VOLUME:
  242. if (card->mute)
  243. ctrl->value=0;
  244. else
  245. ctrl->value=65535;
  246. return (0);
  247. }
  248. return -EINVAL;
  249. }
  250. case VIDIOC_S_CTRL:
  251. {
  252. struct v4l2_control *ctrl= arg;
  253. switch (ctrl->id) {
  254. case V4L2_CID_AUDIO_MUTE:
  255. if (ctrl->value) {
  256. gemtek_pci_mute(card);
  257. } else {
  258. gemtek_pci_unmute(card);
  259. }
  260. return (0);
  261. case V4L2_CID_AUDIO_VOLUME:
  262. if (ctrl->value) {
  263. gemtek_pci_unmute(card);
  264. } else {
  265. gemtek_pci_mute(card);
  266. }
  267. return (0);
  268. }
  269. return -EINVAL;
  270. }
  271. default:
  272. return v4l_compat_translate_ioctl(inode,file,cmd,arg,
  273. gemtek_pci_do_ioctl);
  274. }
  275. }
  276. static int gemtek_pci_ioctl(struct inode *inode, struct file *file,
  277. unsigned int cmd, unsigned long arg)
  278. {
  279. return video_usercopy(inode, file, cmd, arg, gemtek_pci_do_ioctl);
  280. }
  281. enum {
  282. GEMTEK_PR103
  283. };
  284. static char *card_names[] __devinitdata = {
  285. "GEMTEK_PR103"
  286. };
  287. static struct pci_device_id gemtek_pci_id[] =
  288. {
  289. { PCI_VENDOR_ID_GEMTEK, PCI_DEVICE_ID_GEMTEK_PR103,
  290. PCI_ANY_ID, PCI_ANY_ID, 0, 0, GEMTEK_PR103 },
  291. { 0 }
  292. };
  293. MODULE_DEVICE_TABLE( pci, gemtek_pci_id );
  294. static int mx = 1;
  295. static struct file_operations gemtek_pci_fops = {
  296. .owner = THIS_MODULE,
  297. .open = video_exclusive_open,
  298. .release = video_exclusive_release,
  299. .ioctl = gemtek_pci_ioctl,
  300. .compat_ioctl = v4l_compat_ioctl32,
  301. .llseek = no_llseek,
  302. };
  303. static struct video_device vdev_template = {
  304. .owner = THIS_MODULE,
  305. .name = "Gemtek PCI Radio",
  306. .type = VID_TYPE_TUNER,
  307. .hardware = 0,
  308. .fops = &gemtek_pci_fops,
  309. };
  310. static int __devinit gemtek_pci_probe( struct pci_dev *pci_dev, const struct pci_device_id *pci_id )
  311. {
  312. struct gemtek_pci_card *card;
  313. struct video_device *devradio;
  314. if ( (card = kzalloc( sizeof( struct gemtek_pci_card ), GFP_KERNEL )) == NULL ) {
  315. printk( KERN_ERR "gemtek_pci: out of memory\n" );
  316. return -ENOMEM;
  317. }
  318. if ( pci_enable_device( pci_dev ) )
  319. goto err_pci;
  320. card->iobase = pci_resource_start( pci_dev, 0 );
  321. card->length = pci_resource_len( pci_dev, 0 );
  322. if ( request_region( card->iobase, card->length, card_names[pci_id->driver_data] ) == NULL ) {
  323. printk( KERN_ERR "gemtek_pci: i/o port already in use\n" );
  324. goto err_pci;
  325. }
  326. pci_read_config_byte( pci_dev, PCI_REVISION_ID, &card->chiprev );
  327. pci_read_config_word( pci_dev, PCI_SUBSYSTEM_ID, &card->model );
  328. pci_set_drvdata( pci_dev, card );
  329. if ( (devradio = kmalloc( sizeof( struct video_device ), GFP_KERNEL )) == NULL ) {
  330. printk( KERN_ERR "gemtek_pci: out of memory\n" );
  331. goto err_video;
  332. }
  333. *devradio = vdev_template;
  334. if ( video_register_device( devradio, VFL_TYPE_RADIO , nr_radio) == -1 ) {
  335. kfree( devradio );
  336. goto err_video;
  337. }
  338. card->videodev = devradio;
  339. devradio->priv = card;
  340. gemtek_pci_mute( card );
  341. printk( KERN_INFO "Gemtek PCI Radio (rev. %d) found at 0x%04x-0x%04x.\n",
  342. card->chiprev, card->iobase, card->iobase + card->length - 1 );
  343. return 0;
  344. err_video:
  345. release_region( card->iobase, card->length );
  346. err_pci:
  347. kfree( card );
  348. return -ENODEV;
  349. }
  350. static void __devexit gemtek_pci_remove( struct pci_dev *pci_dev )
  351. {
  352. struct gemtek_pci_card *card = pci_get_drvdata( pci_dev );
  353. video_unregister_device( card->videodev );
  354. kfree( card->videodev );
  355. release_region( card->iobase, card->length );
  356. if ( mx )
  357. gemtek_pci_mute( card );
  358. kfree( card );
  359. pci_set_drvdata( pci_dev, NULL );
  360. }
  361. static struct pci_driver gemtek_pci_driver =
  362. {
  363. .name = "gemtek_pci",
  364. .id_table = gemtek_pci_id,
  365. .probe = gemtek_pci_probe,
  366. .remove = __devexit_p(gemtek_pci_remove),
  367. };
  368. static int __init gemtek_pci_init_module( void )
  369. {
  370. return pci_register_driver( &gemtek_pci_driver );
  371. }
  372. static void __exit gemtek_pci_cleanup_module( void )
  373. {
  374. pci_unregister_driver(&gemtek_pci_driver);
  375. }
  376. MODULE_AUTHOR( "Vladimir Shebordaev <vshebordaev@mail.ru>" );
  377. MODULE_DESCRIPTION( "The video4linux driver for the Gemtek PCI Radio Card" );
  378. MODULE_LICENSE("GPL");
  379. module_param(mx, bool, 0);
  380. MODULE_PARM_DESC( mx, "single digit: 1 - turn off the turner upon module exit (default), 0 - do not" );
  381. module_param(nr_radio, int, 0);
  382. MODULE_PARM_DESC( nr_radio, "video4linux device number to use");
  383. module_init( gemtek_pci_init_module );
  384. module_exit( gemtek_pci_cleanup_module );