summaryrefslogtreecommitdiff
path: root/linux/drivers/media/video/hexium_orion.c
blob: f6d4ca5a35b76cb794f63fa2c456fbfc03b47dec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
    hexium_orion.c - v4l2 driver for the Hexium Orion frame grabber cards
               
    Visit http://www.mihu.de/linux/saa7146/hexium/
    for further details about this card.
    
    Copyright (C) 2003 Michael Hunold <michael@mihu.de>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#define DEBUG_VARIABLE debug

#include <media/saa7146_vv.h>

static int debug = 255;
MODULE_PARM(debug,"i");
MODULE_PARM_DESC(debug, "debug verbosity");

/* global variables */
int hexium_num = 0;

#include "hexium_orion.h"

/* this is only called for old HV-PCI6/Orion cards
   without eeprom */
static int hexium_probe(struct saa7146_dev* dev)
{
	struct hexium* hexium = 0;	
	union i2c_smbus_data data;
	int err = 0;
	
	DEB_EE((".\n"));

	/* there are no hexium orion cards with revision 0 saa7146s */
	if( 0 == dev->revision ) {
		return -EFAULT;
	}

	hexium = (struct hexium*)kmalloc(sizeof(struct hexium), GFP_KERNEL);
	if( NULL == hexium ) {
		printk("hexium_orion.o: hexium_probe: not enough kernel memory.\n");
		return -ENOMEM;
	}
	memset(hexium, 0x0, sizeof(struct hexium));	

	/* FIXME: enable i2c-port pins, video-port-pins
	   video port pins should be enabled here ?! */
	saa7146_write(dev, MC1, (MASK_08 | MASK_24 | MASK_10 | MASK_26));

	saa7146_write(dev, DD1_INIT, 0x02000200);
      	saa7146_write(dev, DD1_STREAM_B, 0x00000000);
	saa7146_write(dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));

	saa7146_i2c_adapter_prepare(dev, &hexium->i2c_adapter, SAA7146_I2C_BUS_BIT_RATE_480);
	if(i2c_add_adapter(&hexium->i2c_adapter) < 0) {
		DEB_S(("cannot register i2c-device. skipping.\n"));
		kfree(hexium);
		return -EFAULT;
	}

	/* set SAA7110 control GPIO 0 */
	saa7146_setgpio(dev, 0, SAA7146_GPIO_OUTHI);
	/*  set HWControl GPIO number 2 */
	saa7146_setgpio(dev, 2, SAA7146_GPIO_OUTHI);
	
	mdelay(10);

	/* detect newer Hexium Orion cards by subsystem ids */
	if( 0x17c8 == dev->pci->subsystem_vendor
	 && 0x0101 == dev->pci->subsystem_device ) {
		printk("hexium_orion.o: device is a Hexium Orion w/ 1 SVHS + 3 BNC inputs.\n");
		/* we store the pointer in our private data field */
		(struct hexium*)dev->ext_priv = hexium;
	 	hexium->type = HEXIUM_ORION_1SVHS_3BNC;
		return 0;
	}

	if( 0x17c8 == dev->pci->subsystem_vendor
	 && 0x2101 == dev->pci->subsystem_device ) {
		printk("hexium_orion.o: device is a Hexium Orion w/ 4 BNC inputs.\n");
		/* we store the pointer in our private data field */
		(struct hexium*)dev->ext_priv = hexium;
	 	hexium->type = HEXIUM_ORION_4BNC;
		return 0;
	}

	/* check if this is an old hexium Orion card by looking at 
	   a saa7110 at address 0x4e */
	if(0 == ( err = i2c_smbus_xfer(&hexium->i2c_adapter, 0x4e, 0, I2C_SMBUS_READ, 0x00, I2C_SMBUS_BYTE_DATA, &data))) {
		printk("hexium_orion.o: device is a Hexium HV-PCI6/Orion (old).\n");
		/* we store the pointer in our private data field */
		(struct hexium*)dev->ext_priv = hexium;
		hexium->type = HEXIUM_HV_PCI6_ORION;
		return 0;
	}

	i2c_del_adapter(&hexium->i2c_adapter);
	kfree(hexium);
	return -EFAULT;
}

/* bring hardware to a sane state. this has to be done, just in case someone
   wants to capture from this device before it has been properly initialized.
   the capture engine would badly fail, because no valid signal arrives on the
   saa7146, thus leading to timeouts and stuff. */
static int hexium_init_done(struct saa7146_dev* dev)
{
	struct hexium* hexium = (struct hexium*)dev->ext_priv;
	union i2c_smbus_data data;
	int i = 0;

	DEB_D(("hexium_init_done called.\n"));

	/* initialize the helper ics to useful values */
	for(i = 0; i < sizeof(hexium_saa7110); i++) {
			data.byte = hexium_saa7110[i];
			if(0 != i2c_smbus_xfer(&hexium->i2c_adapter, 0x4e, 0, I2C_SMBUS_WRITE, i ,I2C_SMBUS_BYTE_DATA, &data)) {
				printk("hexium_orion: failed for address 0x%02x\n",i);			
			}
	}

	return 0;
}

/* this function only gets called when the probing was successful */
static int hexium_attach(struct saa7146_dev* dev, struct saa7146_pci_extension_data *info)
{
	struct hexium* hexium = (struct hexium*)dev->ext_priv;

	DEB_EE((".\n"));

	saa7146_vv_init(dev);
	if( 0 != saa7146_register_device(&hexium->video_dev, dev, "hexium", VFL_TYPE_GRABBER)) {
		ERR(("cannot register capture v4l2 device. skipping.\n"));
		return -1;
	}
	
	printk("hexium_orion.o: found 'hexium orion' frame grabber-%d.\n",hexium_num);
	hexium_num++;
	
	/* the rest */
	hexium->cur_input = 0;
	hexium_init_done(dev);
	
	return 0;
}

static int hexium_detach(struct saa7146_dev* dev)
{
	struct hexium* hexium = (struct hexium*)dev->ext_priv;
	
	DEB_EE(("dev:%p\n",dev));

	saa7146_unregister_device(&hexium->video_dev,dev);
	saa7146_vv_release(dev);

	hexium_num--;

	i2c_del_adapter(&hexium->i2c_adapter);
	kfree(hexium);
	return 0;
}

static int hexium_ioctl(struct saa7146_dev *dev, unsigned int cmd, void *arg) 
{
	struct hexium* hexium = (struct hexium*)dev->ext_priv;
/*
	struct saa7146_vv *vv = dev->vv_data; 
*/
	switch(cmd)
	{
	case VIDIOC_ENUMINPUT:
	{
		struct v4l2_input *i = arg;
		DEB_EE(("VIDIOC_ENUMINPUT %d.\n",i->index));
		
		if( i->index < 0 || i->index >= HEXIUM_INPUTS) {
			return -EINVAL;
		}
		
		memcpy(i, &hexium_inputs[i->index], sizeof(struct v4l2_input));

		DEB_D(("v4l2_ioctl: VIDIOC_ENUMINPUT %d.\n",i->index));
		return 0;
	}
	case VIDIOC_G_INPUT:
	{
		int *input = (int *)arg;
		*input = hexium->cur_input;

		DEB_D(("VIDIOC_G_INPUT: %d\n",*input));
		return 0;
	}
	case VIDIOC_S_INPUT:
	{
		int	input = *(int *)arg;

		if (input < 0 || input >= HEXIUM_INPUTS) {
			return -EINVAL;
		}
	
		hexium->cur_input = input;

		/* fixme: switch input here, switch audio, too! */
//		saa7146_set_hps_source_and_sync(dev, input_port_selection[input].hps_source, input_port_selection[input].hps_sync);
		printk("hexium_orion.o: VIDIOC_S_INPUT: fixme switch input.\n");
		
		return 0;
	}
	default:
/*
		DEB_D(("v4l2_ioctl does not handle this ioctl.\n"));
*/
		return -ENOIOCTLCMD;
	}
	return 0;
}

static int std_callback(struct saa7146_dev* dev, struct saa7146_standard *std)
{
	return 0;
}

static
struct saa7146_extension extension;

static
struct saa7146_pci_extension_data hexium_hv_pci6 = {
        .ext_priv = "Hexium HV-PCI6 / Orion",
        .ext = &extension,
};

static
struct saa7146_pci_extension_data hexium_orion_1svhs_3bnc = {
        .ext_priv = "Hexium HV-PCI6 / Orion (1 SVHS/3 BNC)",
        .ext = &extension,
};

static
struct saa7146_pci_extension_data hexium_orion_4bnc = {
        .ext_priv = "Hexium HV-PCI6 / Orion (4 BNC)",
        .ext = &extension,
};

static
struct pci_device_id pci_tbl[] = {
	{
		.vendor    = PCI_VENDOR_ID_PHILIPS,
		.device	   = PCI_DEVICE_ID_PHILIPS_SAA7146,
		.subvendor = 0x0000,
		.subdevice = 0x0000,
		.driver_data = (unsigned long)&hexium_hv_pci6,
	}, {
		.vendor    = PCI_VENDOR_ID_PHILIPS,
		.device	   = PCI_DEVICE_ID_PHILIPS_SAA7146,
		.subvendor = 0x17c8,
		.subdevice = 0x0101,
		.driver_data = (unsigned long)&hexium_orion_1svhs_3bnc,
	}, {
		.vendor    = PCI_VENDOR_ID_PHILIPS,
		.device	   = PCI_DEVICE_ID_PHILIPS_SAA7146,
		.subvendor = 0x17c8,
		.subdevice = 0x2101,
		.driver_data = (unsigned long)&hexium_orion_4bnc,
	}, {
		.vendor = 0,
	}
};

MODULE_DEVICE_TABLE(pci, pci_tbl);

static
struct saa7146_ext_vv vv_data = {
	.inputs		= HEXIUM_INPUTS,
	.capabilities	= 0,
	.stds		= &hexium_standards[0],
	.num_stds	= sizeof(hexium_standards)/sizeof(struct saa7146_standard),
	.std_callback	= &std_callback, 
	.ioctls		= &ioctls[0],
	.ioctl		= hexium_ioctl,
};

static
struct saa7146_extension extension = {
	.name		= "hexium HV-PCI6/Orion",
	.flags		= 0, // SAA7146_USE_I2C_IRQ,
	
	.pci_tbl	= &pci_tbl[0],
	.module		= THIS_MODULE,
	.ext_vv_data	= &vv_data,

	.probe		= hexium_probe,
	.attach		= hexium_attach,
	.detach		= hexium_detach,

	.irq_mask	= 0,
	.irq_func	= NULL,
};	

int __init hexium_init_module(void) 
{
	if( 0 != saa7146_register_extension(&extension)) {
		DEB_S(("failed to register extension.\n"));
		return -ENODEV;
	}

	return 0;
}

void __exit hexium_cleanup_module(void) 
{
	saa7146_unregister_extension(&extension);
}

module_init(hexium_init_module);
module_exit(hexium_cleanup_module);

MODULE_DESCRIPTION("video4linux-2 driver for Hexium Orion frame grabber cards");
MODULE_AUTHOR("Michael Hunold <michael@mihu.de>");
MODULE_LICENSE("GPL");