2 ***************************************************************************
4 * radio-gemtek-pci.c - Gemtek PCI Radio driver
5 * (C) 2001 Vladimir Shebordaev <vshebordaev@mail.ru>
7 ***************************************************************************
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.
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.
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,
24 ***************************************************************************
26 * Gemtek Corp still silently refuses to release any specifications
27 * of their multimedia devices, so the protocol still has to be
30 * The v4l code was inspired by Jonas Munsin's Gemtek serial line
31 * radio device driver.
33 * Please, let me know if this piece of code was useful :)
35 * TODO: multiple device support and portability were not tested
37 * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
39 ***************************************************************************
42 #include <linux/types.h>
43 #include <linux/list.h>
44 #include <linux/module.h>
45 #include <linux/init.h>
46 #include <linux/pci.h>
47 #include <linux/videodev2.h>
48 #include <linux/errno.h>
49 #include <linux/version.h> /* for KERNEL_VERSION MACRO */
51 #include <media/v4l2-device.h>
52 #include <media/v4l2-ioctl.h>
54 MODULE_AUTHOR("Vladimir Shebordaev <vshebordaev@mail.ru>");
55 MODULE_DESCRIPTION("The video4linux driver for the Gemtek PCI Radio Card");
56 MODULE_LICENSE("GPL");
58 static int nr_radio
= -1;
61 module_param(mx
, bool, 0);
62 MODULE_PARM_DESC(mx
, "single digit: 1 - turn off the turner upon module exit (default), 0 - do not");
63 module_param(nr_radio
, int, 0);
64 MODULE_PARM_DESC(nr_radio
, "video4linux device number to use");
66 #define RADIO_VERSION KERNEL_VERSION(0, 0, 2)
68 #ifndef PCI_VENDOR_ID_GEMTEK
69 #define PCI_VENDOR_ID_GEMTEK 0x5046
72 #ifndef PCI_DEVICE_ID_GEMTEK_PR103
73 #define PCI_DEVICE_ID_GEMTEK_PR103 0x1001
76 #ifndef GEMTEK_PCI_RANGE_LOW
77 #define GEMTEK_PCI_RANGE_LOW (87*16000)
80 #ifndef GEMTEK_PCI_RANGE_HIGH
81 #define GEMTEK_PCI_RANGE_HIGH (108*16000)
85 struct v4l2_device v4l2_dev
;
86 struct video_device vdev
;
93 u32 current_frequency
;
97 static inline struct gemtek_pci
*to_gemtek_pci(struct v4l2_device
*v4l2_dev
)
99 return container_of(v4l2_dev
, struct gemtek_pci
, v4l2_dev
);
102 static inline u8
gemtek_pci_out(u16 value
, u32 port
)
109 #define _b0(v) (*((u8 *)&v))
111 static void __gemtek_pci_cmd(u16 value
, u32 port
, u8
*last_byte
, int keep
)
113 u8 byte
= *last_byte
;
134 static inline void gemtek_pci_nil(u32 port
, u8
*last_byte
)
136 __gemtek_pci_cmd(0x00, port
, last_byte
, false);
139 static inline void gemtek_pci_cmd(u16 cmd
, u32 port
, u8
*last_byte
)
141 __gemtek_pci_cmd(cmd
, port
, last_byte
, true);
144 static void gemtek_pci_setfrequency(struct gemtek_pci
*card
, unsigned long frequency
)
147 u32 value
= frequency
/ 200 + 856;
150 u32 port
= card
->iobase
;
152 mutex_lock(&card
->lock
);
153 card
->current_frequency
= frequency
;
154 last_byte
= gemtek_pci_out(0x06, port
);
158 gemtek_pci_nil(port
, &last_byte
);
164 gemtek_pci_cmd(value
& mask
, port
, &last_byte
);
170 mutex_unlock(&card
->lock
);
174 static void gemtek_pci_mute(struct gemtek_pci
*card
)
176 mutex_lock(&card
->lock
);
177 outb(0x1f, card
->iobase
);
179 mutex_unlock(&card
->lock
);
182 static void gemtek_pci_unmute(struct gemtek_pci
*card
)
185 gemtek_pci_setfrequency(card
, card
->current_frequency
);
190 static int gemtek_pci_getsignal(struct gemtek_pci
*card
)
194 mutex_lock(&card
->lock
);
195 sig
= (inb(card
->iobase
) & 0x08) ? 0 : 1;
196 mutex_unlock(&card
->lock
);
200 static int vidioc_querycap(struct file
*file
, void *priv
,
201 struct v4l2_capability
*v
)
203 struct gemtek_pci
*card
= video_drvdata(file
);
205 strlcpy(v
->driver
, "radio-gemtek-pci", sizeof(v
->driver
));
206 strlcpy(v
->card
, "GemTek PCI Radio", sizeof(v
->card
));
207 snprintf(v
->bus_info
, sizeof(v
->bus_info
), "PCI:%s", pci_name(card
->pdev
));
208 v
->version
= RADIO_VERSION
;
209 v
->capabilities
= V4L2_CAP_TUNER
| V4L2_CAP_RADIO
;
213 static int vidioc_g_tuner(struct file
*file
, void *priv
,
214 struct v4l2_tuner
*v
)
216 struct gemtek_pci
*card
= video_drvdata(file
);
221 strlcpy(v
->name
, "FM", sizeof(v
->name
));
222 v
->type
= V4L2_TUNER_RADIO
;
223 v
->rangelow
= GEMTEK_PCI_RANGE_LOW
;
224 v
->rangehigh
= GEMTEK_PCI_RANGE_HIGH
;
225 v
->rxsubchans
= V4L2_TUNER_SUB_MONO
;
226 v
->capability
= V4L2_TUNER_CAP_LOW
;
227 v
->audmode
= V4L2_TUNER_MODE_MONO
;
228 v
->signal
= 0xffff * gemtek_pci_getsignal(card
);
232 static int vidioc_s_tuner(struct file
*file
, void *priv
,
233 struct v4l2_tuner
*v
)
235 return v
->index
? -EINVAL
: 0;
238 static int vidioc_s_frequency(struct file
*file
, void *priv
,
239 struct v4l2_frequency
*f
)
241 struct gemtek_pci
*card
= video_drvdata(file
);
243 if (f
->frequency
< GEMTEK_PCI_RANGE_LOW
||
244 f
->frequency
> GEMTEK_PCI_RANGE_HIGH
)
246 gemtek_pci_setfrequency(card
, f
->frequency
);
251 static int vidioc_g_frequency(struct file
*file
, void *priv
,
252 struct v4l2_frequency
*f
)
254 struct gemtek_pci
*card
= video_drvdata(file
);
256 f
->type
= V4L2_TUNER_RADIO
;
257 f
->frequency
= card
->current_frequency
;
261 static int vidioc_queryctrl(struct file
*file
, void *priv
,
262 struct v4l2_queryctrl
*qc
)
265 case V4L2_CID_AUDIO_MUTE
:
266 return v4l2_ctrl_query_fill(qc
, 0, 1, 1, 1);
267 case V4L2_CID_AUDIO_VOLUME
:
268 return v4l2_ctrl_query_fill(qc
, 0, 65535, 65535, 65535);
273 static int vidioc_g_ctrl(struct file
*file
, void *priv
,
274 struct v4l2_control
*ctrl
)
276 struct gemtek_pci
*card
= video_drvdata(file
);
279 case V4L2_CID_AUDIO_MUTE
:
280 ctrl
->value
= card
->mute
;
282 case V4L2_CID_AUDIO_VOLUME
:
292 static int vidioc_s_ctrl(struct file
*file
, void *priv
,
293 struct v4l2_control
*ctrl
)
295 struct gemtek_pci
*card
= video_drvdata(file
);
298 case V4L2_CID_AUDIO_MUTE
:
300 gemtek_pci_mute(card
);
302 gemtek_pci_unmute(card
);
304 case V4L2_CID_AUDIO_VOLUME
:
306 gemtek_pci_unmute(card
);
308 gemtek_pci_mute(card
);
314 static int vidioc_g_input(struct file
*filp
, void *priv
, unsigned int *i
)
320 static int vidioc_s_input(struct file
*filp
, void *priv
, unsigned int i
)
322 return i
? -EINVAL
: 0;
325 static int vidioc_g_audio(struct file
*file
, void *priv
,
326 struct v4l2_audio
*a
)
329 strlcpy(a
->name
, "Radio", sizeof(a
->name
));
330 a
->capability
= V4L2_AUDCAP_STEREO
;
334 static int vidioc_s_audio(struct file
*file
, void *priv
,
335 struct v4l2_audio
*a
)
337 return a
->index
? -EINVAL
: 0;
344 static char *card_names
[] __devinitdata
= {
348 static struct pci_device_id gemtek_pci_id
[] =
350 { PCI_VENDOR_ID_GEMTEK
, PCI_DEVICE_ID_GEMTEK_PR103
,
351 PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, GEMTEK_PR103
},
355 MODULE_DEVICE_TABLE(pci
, gemtek_pci_id
);
357 static const struct v4l2_file_operations gemtek_pci_fops
= {
358 .owner
= THIS_MODULE
,
359 .ioctl
= video_ioctl2
,
362 static const struct v4l2_ioctl_ops gemtek_pci_ioctl_ops
= {
363 .vidioc_querycap
= vidioc_querycap
,
364 .vidioc_g_tuner
= vidioc_g_tuner
,
365 .vidioc_s_tuner
= vidioc_s_tuner
,
366 .vidioc_g_audio
= vidioc_g_audio
,
367 .vidioc_s_audio
= vidioc_s_audio
,
368 .vidioc_g_input
= vidioc_g_input
,
369 .vidioc_s_input
= vidioc_s_input
,
370 .vidioc_g_frequency
= vidioc_g_frequency
,
371 .vidioc_s_frequency
= vidioc_s_frequency
,
372 .vidioc_queryctrl
= vidioc_queryctrl
,
373 .vidioc_g_ctrl
= vidioc_g_ctrl
,
374 .vidioc_s_ctrl
= vidioc_s_ctrl
,
377 static int __devinit
gemtek_pci_probe(struct pci_dev
*pdev
, const struct pci_device_id
*pci_id
)
379 struct gemtek_pci
*card
;
380 struct v4l2_device
*v4l2_dev
;
383 card
= kzalloc(sizeof(struct gemtek_pci
), GFP_KERNEL
);
385 dev_err(&pdev
->dev
, "out of memory\n");
389 v4l2_dev
= &card
->v4l2_dev
;
390 mutex_init(&card
->lock
);
393 strlcpy(v4l2_dev
->name
, "gemtek_pci", sizeof(v4l2_dev
->name
));
395 res
= v4l2_device_register(&pdev
->dev
, v4l2_dev
);
397 v4l2_err(v4l2_dev
, "Could not register v4l2_device\n");
402 if (pci_enable_device(pdev
))
405 card
->iobase
= pci_resource_start(pdev
, 0);
406 card
->length
= pci_resource_len(pdev
, 0);
408 if (request_region(card
->iobase
, card
->length
, card_names
[pci_id
->driver_data
]) == NULL
) {
409 v4l2_err(v4l2_dev
, "i/o port already in use\n");
413 strlcpy(card
->vdev
.name
, v4l2_dev
->name
, sizeof(card
->vdev
.name
));
414 card
->vdev
.v4l2_dev
= v4l2_dev
;
415 card
->vdev
.fops
= &gemtek_pci_fops
;
416 card
->vdev
.ioctl_ops
= &gemtek_pci_ioctl_ops
;
417 card
->vdev
.release
= video_device_release_empty
;
418 video_set_drvdata(&card
->vdev
, card
);
420 if (video_register_device(&card
->vdev
, VFL_TYPE_RADIO
, nr_radio
) < 0)
423 gemtek_pci_mute(card
);
425 v4l2_info(v4l2_dev
, "Gemtek PCI Radio (rev. %d) found at 0x%04x-0x%04x.\n",
426 pdev
->revision
, card
->iobase
, card
->iobase
+ card
->length
- 1);
431 release_region(card
->iobase
, card
->length
);
434 v4l2_device_unregister(v4l2_dev
);
439 static void __devexit
gemtek_pci_remove(struct pci_dev
*pdev
)
441 struct v4l2_device
*v4l2_dev
= dev_get_drvdata(&pdev
->dev
);
442 struct gemtek_pci
*card
= to_gemtek_pci(v4l2_dev
);
444 video_unregister_device(&card
->vdev
);
445 v4l2_device_unregister(v4l2_dev
);
447 release_region(card
->iobase
, card
->length
);
450 gemtek_pci_mute(card
);
455 static struct pci_driver gemtek_pci_driver
= {
456 .name
= "gemtek_pci",
457 .id_table
= gemtek_pci_id
,
458 .probe
= gemtek_pci_probe
,
459 .remove
= __devexit_p(gemtek_pci_remove
),
462 static int __init
gemtek_pci_init(void)
464 return pci_register_driver(&gemtek_pci_driver
);
467 static void __exit
gemtek_pci_exit(void)
469 pci_unregister_driver(&gemtek_pci_driver
);
472 module_init(gemtek_pci_init
);
473 module_exit(gemtek_pci_exit
);