radio-gemtek-pci.c 12 KB

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