V4L/DVB (10883): radio-gemtek-pci: convert to v4l2_device.
[deliverable/linux.git] / drivers / media / radio / radio-gemtek-pci.c
CommitLineData
1da177e4
LT
1/*
2 ***************************************************************************
4286c6f6 3 *
1da177e4
LT
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 :)
4286c6f6 34 *
1da177e4
LT
35 * TODO: multiple device support and portability were not tested
36 *
52afbc2f
MCC
37 * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
38 *
1da177e4
LT
39 ***************************************************************************
40 */
41
1da177e4
LT
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>
52afbc2f 47#include <linux/videodev2.h>
1da177e4 48#include <linux/errno.h>
52afbc2f 49#include <linux/version.h> /* for KERNEL_VERSION MACRO */
79f94878
HV
50#include <linux/io.h>
51#include <linux/uaccess.h>
52#include <media/v4l2-device.h>
53#include <media/v4l2-ioctl.h>
52afbc2f 54
79f94878
HV
55MODULE_AUTHOR("Vladimir Shebordaev <vshebordaev@mail.ru>");
56MODULE_DESCRIPTION("The video4linux driver for the Gemtek PCI Radio Card");
57MODULE_LICENSE("GPL");
58
59static int nr_radio = -1;
60static int mx = 1;
61
62module_param(mx, bool, 0);
63MODULE_PARM_DESC(mx, "single digit: 1 - turn off the turner upon module exit (default), 0 - do not");
64module_param(nr_radio, int, 0);
65MODULE_PARM_DESC(nr_radio, "video4linux device number to use");
66
67#define RADIO_VERSION KERNEL_VERSION(0, 0, 2)
1da177e4
LT
68
69#ifndef PCI_VENDOR_ID_GEMTEK
70#define PCI_VENDOR_ID_GEMTEK 0x5046
71#endif
72
73#ifndef PCI_DEVICE_ID_GEMTEK_PR103
74#define PCI_DEVICE_ID_GEMTEK_PR103 0x1001
75#endif
76
77#ifndef GEMTEK_PCI_RANGE_LOW
78#define GEMTEK_PCI_RANGE_LOW (87*16000)
79#endif
80
81#ifndef GEMTEK_PCI_RANGE_HIGH
82#define GEMTEK_PCI_RANGE_HIGH (108*16000)
83#endif
84
79f94878
HV
85struct gemtek_pci {
86 struct v4l2_device v4l2_dev;
87 struct video_device vdev;
88 struct mutex lock;
89 struct pci_dev *pdev;
4286c6f6 90
1da177e4
LT
91 u32 iobase;
92 u32 length;
4286c6f6 93
1da177e4
LT
94 u32 current_frequency;
95 u8 mute;
96};
97
79f94878
HV
98static inline struct gemtek_pci *to_gemtek_pci(struct v4l2_device *v4l2_dev)
99{
100 return container_of(v4l2_dev, struct gemtek_pci, v4l2_dev);
101}
1da177e4 102
79f94878 103static inline u8 gemtek_pci_out(u16 value, u32 port)
1da177e4 104{
79f94878 105 outw(value, port);
1da177e4
LT
106
107 return (u8)value;
108}
109
79f94878
HV
110#define _b0(v) (*((u8 *)&v))
111
112static void __gemtek_pci_cmd(u16 value, u32 port, u8 *last_byte, int keep)
1da177e4 113{
79f94878 114 u8 byte = *last_byte;
1da177e4 115
79f94878
HV
116 if (!value) {
117 if (!keep)
1da177e4 118 value = (u16)port;
4286c6f6 119 byte &= 0xfd;
1da177e4
LT
120 } else
121 byte |= 2;
122
79f94878
HV
123 _b0(value) = byte;
124 outw(value, port);
1da177e4 125 byte |= 1;
79f94878
HV
126 _b0(value) = byte;
127 outw(value, port);
1da177e4 128 byte &= 0xfe;
79f94878
HV
129 _b0(value) = byte;
130 outw(value, port);
4286c6f6 131
1da177e4
LT
132 *last_byte = byte;
133}
134
79f94878 135static inline void gemtek_pci_nil(u32 port, u8 *last_byte)
1da177e4 136{
79f94878 137 __gemtek_pci_cmd(0x00, port, last_byte, false);
1da177e4
LT
138}
139
79f94878 140static inline void gemtek_pci_cmd(u16 cmd, u32 port, u8 *last_byte)
1da177e4 141{
79f94878 142 __gemtek_pci_cmd(cmd, port, last_byte, true);
1da177e4
LT
143}
144
79f94878 145static void gemtek_pci_setfrequency(struct gemtek_pci *card, unsigned long frequency)
1da177e4 146{
79f94878
HV
147 int i;
148 u32 value = frequency / 200 + 856;
149 u16 mask = 0x8000;
1da177e4
LT
150 u8 last_byte;
151 u32 port = card->iobase;
152
79f94878
HV
153 mutex_lock(&card->lock);
154 card->current_frequency = frequency;
155 last_byte = gemtek_pci_out(0x06, port);
1da177e4
LT
156
157 i = 0;
158 do {
79f94878 159 gemtek_pci_nil(port, &last_byte);
1da177e4 160 i++;
79f94878 161 } while (i < 9);
1da177e4
LT
162
163 i = 0;
164 do {
79f94878 165 gemtek_pci_cmd(value & mask, port, &last_byte);
1da177e4
LT
166 mask >>= 1;
167 i++;
79f94878 168 } while (i < 16);
1da177e4 169
79f94878
HV
170 outw(0x10, port);
171 mutex_unlock(&card->lock);
1da177e4
LT
172}
173
174
79f94878 175static void gemtek_pci_mute(struct gemtek_pci *card)
1da177e4 176{
79f94878
HV
177 mutex_lock(&card->lock);
178 outb(0x1f, card->iobase);
fd4bc445 179 card->mute = true;
79f94878 180 mutex_unlock(&card->lock);
1da177e4
LT
181}
182
79f94878 183static void gemtek_pci_unmute(struct gemtek_pci *card)
1da177e4 184{
79f94878
HV
185 mutex_lock(&card->lock);
186 if (card->mute) {
187 gemtek_pci_setfrequency(card, card->current_frequency);
fd4bc445 188 card->mute = false;
1da177e4 189 }
79f94878 190 mutex_unlock(&card->lock);
1da177e4
LT
191}
192
79f94878 193static int gemtek_pci_getsignal(struct gemtek_pci *card)
1da177e4 194{
79f94878
HV
195 int sig;
196
197 mutex_lock(&card->lock);
198 sig = (inb(card->iobase) & 0x08) ? 0 : 1;
199 mutex_unlock(&card->lock);
200 return sig;
1da177e4
LT
201}
202
6f66446c
DL
203static int vidioc_querycap(struct file *file, void *priv,
204 struct v4l2_capability *v)
205{
79f94878
HV
206 struct gemtek_pci *card = video_drvdata(file);
207
6f66446c
DL
208 strlcpy(v->driver, "radio-gemtek-pci", sizeof(v->driver));
209 strlcpy(v->card, "GemTek PCI Radio", sizeof(v->card));
79f94878 210 snprintf(v->bus_info, sizeof(v->bus_info), "PCI:%s", pci_name(card->pdev));
6f66446c 211 v->version = RADIO_VERSION;
79f94878 212 v->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
6f66446c
DL
213 return 0;
214}
215
216static int vidioc_g_tuner(struct file *file, void *priv,
217 struct v4l2_tuner *v)
1da177e4 218{
79f94878 219 struct gemtek_pci *card = video_drvdata(file);
1da177e4 220
6f66446c
DL
221 if (v->index > 0)
222 return -EINVAL;
223
79f94878 224 strlcpy(v->name, "FM", sizeof(v->name));
6f66446c
DL
225 v->type = V4L2_TUNER_RADIO;
226 v->rangelow = GEMTEK_PCI_RANGE_LOW;
227 v->rangehigh = GEMTEK_PCI_RANGE_HIGH;
228 v->rxsubchans = V4L2_TUNER_SUB_MONO;
229 v->capability = V4L2_TUNER_CAP_LOW;
230 v->audmode = V4L2_TUNER_MODE_MONO;
231 v->signal = 0xffff * gemtek_pci_getsignal(card);
232 return 0;
233}
1da177e4 234
6f66446c
DL
235static int vidioc_s_tuner(struct file *file, void *priv,
236 struct v4l2_tuner *v)
237{
79f94878 238 return v->index ? -EINVAL : 0;
6f66446c 239}
1da177e4 240
6f66446c
DL
241static int vidioc_s_frequency(struct file *file, void *priv,
242 struct v4l2_frequency *f)
243{
79f94878 244 struct gemtek_pci *card = video_drvdata(file);
52afbc2f 245
79f94878
HV
246 if (f->frequency < GEMTEK_PCI_RANGE_LOW ||
247 f->frequency > GEMTEK_PCI_RANGE_HIGH)
6f66446c
DL
248 return -EINVAL;
249 gemtek_pci_setfrequency(card, f->frequency);
6f66446c
DL
250 card->mute = false;
251 return 0;
252}
52afbc2f 253
6f66446c
DL
254static int vidioc_g_frequency(struct file *file, void *priv,
255 struct v4l2_frequency *f)
256{
79f94878 257 struct gemtek_pci *card = video_drvdata(file);
52afbc2f 258
6f66446c
DL
259 f->type = V4L2_TUNER_RADIO;
260 f->frequency = card->current_frequency;
261 return 0;
262}
1da177e4 263
6f66446c
DL
264static int vidioc_queryctrl(struct file *file, void *priv,
265 struct v4l2_queryctrl *qc)
266{
79f94878
HV
267 switch (qc->id) {
268 case V4L2_CID_AUDIO_MUTE:
269 return v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
270 case V4L2_CID_AUDIO_VOLUME:
271 return v4l2_ctrl_query_fill(qc, 0, 65535, 65535, 65535);
6f66446c
DL
272 }
273 return -EINVAL;
274}
275
276static int vidioc_g_ctrl(struct file *file, void *priv,
277 struct v4l2_control *ctrl)
278{
79f94878 279 struct gemtek_pci *card = video_drvdata(file);
4286c6f6 280
6f66446c
DL
281 switch (ctrl->id) {
282 case V4L2_CID_AUDIO_MUTE:
283 ctrl->value = card->mute;
284 return 0;
285 case V4L2_CID_AUDIO_VOLUME:
286 if (card->mute)
287 ctrl->value = 0;
288 else
289 ctrl->value = 65535;
290 return 0;
291 }
292 return -EINVAL;
293}
1da177e4 294
6f66446c
DL
295static int vidioc_s_ctrl(struct file *file, void *priv,
296 struct v4l2_control *ctrl)
297{
79f94878 298 struct gemtek_pci *card = video_drvdata(file);
1da177e4 299
6f66446c
DL
300 switch (ctrl->id) {
301 case V4L2_CID_AUDIO_MUTE:
302 if (ctrl->value)
303 gemtek_pci_mute(card);
304 else
305 gemtek_pci_unmute(card);
306 return 0;
307 case V4L2_CID_AUDIO_VOLUME:
308 if (ctrl->value)
309 gemtek_pci_unmute(card);
310 else
311 gemtek_pci_mute(card);
312 return 0;
1da177e4 313 }
6f66446c
DL
314 return -EINVAL;
315}
316
6f66446c
DL
317static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
318{
319 *i = 0;
320 return 0;
321}
322
323static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
324{
79f94878
HV
325 return i ? -EINVAL : 0;
326}
327
328static int vidioc_g_audio(struct file *file, void *priv,
329 struct v4l2_audio *a)
330{
331 a->index = 0;
332 strlcpy(a->name, "Radio", sizeof(a->name));
333 a->capability = V4L2_AUDCAP_STEREO;
6f66446c 334 return 0;
1da177e4
LT
335}
336
6f66446c
DL
337static int vidioc_s_audio(struct file *file, void *priv,
338 struct v4l2_audio *a)
1da177e4 339{
79f94878 340 return a->index ? -EINVAL : 0;
1da177e4
LT
341}
342
343enum {
344 GEMTEK_PR103
345};
346
347static char *card_names[] __devinitdata = {
348 "GEMTEK_PR103"
349};
350
351static struct pci_device_id gemtek_pci_id[] =
352{
353 { PCI_VENDOR_ID_GEMTEK, PCI_DEVICE_ID_GEMTEK_PR103,
354 PCI_ANY_ID, PCI_ANY_ID, 0, 0, GEMTEK_PR103 },
355 { 0 }
356};
357
79f94878 358MODULE_DEVICE_TABLE(pci, gemtek_pci_id);
1da177e4 359
79f94878 360static int gemtek_pci_open(struct file *file)
3ca685aa 361{
79f94878 362 return 0;
3ca685aa
HV
363}
364
79f94878 365static int gemtek_pci_release(struct file *file)
3ca685aa 366{
3ca685aa
HV
367 return 0;
368}
369
bec43661 370static const struct v4l2_file_operations gemtek_pci_fops = {
1da177e4 371 .owner = THIS_MODULE,
79f94878
HV
372 .open = gemtek_pci_open,
373 .release = gemtek_pci_release,
6f66446c 374 .ioctl = video_ioctl2,
1da177e4
LT
375};
376
a399810c 377static const struct v4l2_ioctl_ops gemtek_pci_ioctl_ops = {
6f66446c
DL
378 .vidioc_querycap = vidioc_querycap,
379 .vidioc_g_tuner = vidioc_g_tuner,
380 .vidioc_s_tuner = vidioc_s_tuner,
381 .vidioc_g_audio = vidioc_g_audio,
382 .vidioc_s_audio = vidioc_s_audio,
383 .vidioc_g_input = vidioc_g_input,
384 .vidioc_s_input = vidioc_s_input,
385 .vidioc_g_frequency = vidioc_g_frequency,
386 .vidioc_s_frequency = vidioc_s_frequency,
387 .vidioc_queryctrl = vidioc_queryctrl,
388 .vidioc_g_ctrl = vidioc_g_ctrl,
389 .vidioc_s_ctrl = vidioc_s_ctrl,
1da177e4
LT
390};
391
79f94878 392static int __devinit gemtek_pci_probe(struct pci_dev *pdev, const struct pci_device_id *pci_id)
1da177e4 393{
79f94878
HV
394 struct gemtek_pci *card;
395 struct v4l2_device *v4l2_dev;
396 int res;
1da177e4 397
79f94878
HV
398 card = kzalloc(sizeof(struct gemtek_pci), GFP_KERNEL);
399 if (card == NULL) {
400 dev_err(&pdev->dev, "out of memory\n");
1da177e4
LT
401 return -ENOMEM;
402 }
1da177e4 403
79f94878
HV
404 v4l2_dev = &card->v4l2_dev;
405 mutex_init(&card->lock);
406 card->pdev = pdev;
4286c6f6 407
79f94878 408 strlcpy(v4l2_dev->name, "gemtek_pci", sizeof(v4l2_dev->name));
1da177e4 409
79f94878
HV
410 res = v4l2_device_register(&pdev->dev, v4l2_dev);
411 if (res < 0) {
412 v4l2_err(v4l2_dev, "Could not register v4l2_device\n");
413 kfree(card);
414 return res;
1da177e4
LT
415 }
416
79f94878
HV
417 if (pci_enable_device(pdev))
418 goto err_pci;
4286c6f6 419
79f94878
HV
420 card->iobase = pci_resource_start(pdev, 0);
421 card->length = pci_resource_len(pdev, 0);
422
423 if (request_region(card->iobase, card->length, card_names[pci_id->driver_data]) == NULL) {
424 v4l2_err(v4l2_dev, "i/o port already in use\n");
425 goto err_pci;
1da177e4 426 }
1da177e4 427
79f94878
HV
428 strlcpy(card->vdev.name, v4l2_dev->name, sizeof(card->vdev.name));
429 card->vdev.v4l2_dev = v4l2_dev;
430 card->vdev.fops = &gemtek_pci_fops;
431 card->vdev.ioctl_ops = &gemtek_pci_ioctl_ops;
432 card->vdev.release = video_device_release_empty;
433 video_set_drvdata(&card->vdev, card);
434
435 if (video_register_device(&card->vdev, VFL_TYPE_RADIO, nr_radio) < 0)
1da177e4 436 goto err_video;
1da177e4 437
79f94878 438 gemtek_pci_mute(card);
1da177e4 439
79f94878
HV
440 v4l2_info(v4l2_dev, "Gemtek PCI Radio (rev. %d) found at 0x%04x-0x%04x.\n",
441 pdev->revision, card->iobase, card->iobase + card->length - 1);
1da177e4
LT
442
443 return 0;
444
445err_video:
79f94878 446 release_region(card->iobase, card->length);
1da177e4
LT
447
448err_pci:
79f94878
HV
449 v4l2_device_unregister(v4l2_dev);
450 kfree(card);
4286c6f6 451 return -ENODEV;
1da177e4
LT
452}
453
79f94878 454static void __devexit gemtek_pci_remove(struct pci_dev *pdev)
1da177e4 455{
79f94878
HV
456 struct v4l2_device *v4l2_dev = dev_get_drvdata(&pdev->dev);
457 struct gemtek_pci *card = to_gemtek_pci(v4l2_dev);
1da177e4 458
79f94878
HV
459 video_unregister_device(&card->vdev);
460 v4l2_device_unregister(v4l2_dev);
1da177e4 461
79f94878 462 release_region(card->iobase, card->length);
4286c6f6 463
79f94878
HV
464 if (mx)
465 gemtek_pci_mute(card);
1da177e4 466
79f94878 467 kfree(card);
1da177e4
LT
468}
469
79f94878 470static struct pci_driver gemtek_pci_driver = {
1da177e4
LT
471 .name = "gemtek_pci",
472 .id_table = gemtek_pci_id,
473 .probe = gemtek_pci_probe,
474 .remove = __devexit_p(gemtek_pci_remove),
475};
476
79f94878 477static int __init gemtek_pci_init(void)
1da177e4 478{
79f94878 479 return pci_register_driver(&gemtek_pci_driver);
1da177e4
LT
480}
481
79f94878 482static void __exit gemtek_pci_exit(void)
1da177e4 483{
13962753 484 pci_unregister_driver(&gemtek_pci_driver);
1da177e4
LT
485}
486
79f94878
HV
487module_init(gemtek_pci_init);
488module_exit(gemtek_pci_exit);
This page took 0.440721 seconds and 5 git commands to generate.