summaryrefslogtreecommitdiffstats
path: root/drivers/media/video/vino.c
blob: 76e8681d65c662f863636fb67deaf35f7158391b (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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
 * (incomplete) Driver for the VINO (Video In No Out) system found in SGI Indys.
 *
 * This file is subject to the terms and conditions of the GNU General Public
 * License version 2 as published by the Free Software Foundation.
 *
 * Copyright (C) 2003 Ladislav Michl <ladis@linux-mips.org>
 */

#include <linux/module.h>
#include <linux/init.h>
#include <linux/types.h>
#include <linux/mm.h>
#include <linux/slab.h>
#include <linux/wrapper.h>
#include <linux/errno.h>
#include <linux/irq.h>
#include <linux/delay.h>
#include <linux/videodev.h>
#include <linux/i2c.h>
#include <linux/i2c-algo-sgi.h>

#include <asm/addrspace.h>
#include <asm/system.h>
#include <asm/bootinfo.h>
#include <asm/pgtable.h>
#include <asm/paccess.h>
#include <asm/io.h>
#include <asm/sgi/ip22.h>
#include <asm/sgi/hpc3.h>
#include <asm/sgi/mc.h>

#include "vino.h"

/* debugging? */
#if 1
#define DEBUG(x...)     printk(x);
#else
#define DEBUG(x...)
#endif


/* VINO ASIC registers */
struct sgi_vino *vino;

static const char *vinostr = "VINO IndyCam/TV";
static int threshold_a = 512;
static int threshold_b = 512;

struct vino_device {
	struct video_device vdev;
#define VINO_CHAN_A		1
#define VINO_CHAN_B		2
	int chan;
};

struct vino_client {
	struct i2c_client *driver;
	int owner;
};

struct vino_video {
	struct vino_device chA;
	struct vino_device chB;

	struct vino_client decoder;
	struct vino_client camera;

	struct semaphore input_lock;

	/* Loaded into VINO descriptors to clear End Of Descriptors table
	 * interupt condition */
	unsigned long dummy_page;
	unsigned int dummy_buf[4] __attribute__((aligned(8)));
};

static struct vino_video *Vino;

unsigned i2c_vino_getctrl(void *data)
{
	return vino->i2c_control;
}

void i2c_vino_setctrl(void *data, unsigned val)
{
	vino->i2c_control = val;
}

unsigned i2c_vino_rdata(void *data)
{
	return vino->i2c_data;
}

void i2c_vino_wdata(void *data, unsigned val)
{
	vino->i2c_data = val;
}

static struct i2c_algo_sgi_data i2c_sgi_vino_data =
{
	.getctrl = &i2c_vino_getctrl,
	.setctrl = &i2c_vino_setctrl,
	.rdata   = &i2c_vino_rdata,
	.wdata   = &i2c_vino_wdata,
	.xfer_timeout = 200,
	.ack_timeout  = 1000,
};

/*
 * There are two possible clients on VINO I2C bus, so we limit usage only
 * to them.
 */
static int i2c_vino_client_reg(struct i2c_client *client)
{
	int res = 0;

	down(&Vino->input_lock);
	switch (client->driver->id) {
	case I2C_DRIVERID_SAA7191:
		if (Vino->decoder.driver)
			res = -EBUSY;
		else
			Vino->decoder.driver = client;
		break;
	case I2C_DRIVERID_INDYCAM:
		if (Vino->camera.driver)
			res = -EBUSY;
		else
			Vino->camera.driver = client;
		break;
	default:
		res = -ENODEV;
	}
	up(&Vino->input_lock);

	return res;
}

static int i2c_vino_client_unreg(struct i2c_client *client)
{
	int res = 0;

	down(&Vino->input_lock);
	if (client == Vino->decoder.driver) {
		if (Vino->decoder.owner)
			res = -EBUSY;
		else
			Vino->decoder.driver = NULL;
	} else if (client == Vino->camera.driver) {
		if (Vino->camera.owner)
			res = -EBUSY;
		else
			Vino->camera.driver = NULL;
	}
	up(&Vino->input_lock);

	return res;
}

static struct i2c_adapter vino_i2c_adapter =
{
	.name			= "VINO I2C bus",
	.id			= I2C_HW_SGI_VINO,
	.algo_data		= &i2c_sgi_vino_data,
	.client_register	= &i2c_vino_client_reg,
	.client_unregister	= &i2c_vino_client_unreg,
};

static int vino_i2c_add_bus(void)
{
	return i2c_sgi_add_bus(&vino_i2c_adapter);
}

static int vino_i2c_del_bus(void)
{
	return i2c_sgi_del_bus(&vino_i2c_adapter);
}


static void vino_interrupt(int irq, void *dev_id, struct pt_regs *regs)
{
}

static int vino_open(struct video_device *dev, int flags)
{
	struct vino_device *videv = (struct vino_device *)dev;

	return 0;
}

static void vino_close(struct video_device *dev)
{
	struct vino_device *videv = (struct vino_device *)dev;
}

static int vino_mmap(struct video_device *dev, const char *adr,
		     unsigned long size)
{
	struct vino_device *videv = (struct vino_device *)dev;

	return -EINVAL;
}

static int vino_ioctl(struct video_device *dev, unsigned int cmd, void *arg)
{
	struct vino_device *videv = (struct vino_device *)dev;

	return -EINVAL;
}

static const struct video_device vino_device = {
	.owner		= THIS_MODULE,
	.type		= VID_TYPE_CAPTURE | VID_TYPE_SUBCAPTURE,
	.hardware	= VID_HARDWARE_VINO,
	.name		= "VINO",
	.open		= vino_open,
	.close		= vino_close,
	.ioctl		= vino_ioctl,
	.mmap		= vino_mmap,
};

static int __init vino_init(void)
{
	unsigned long rev;
	int i, ret = 0;

	/* VINO is Indy specific beast */
	if (ip22_is_fullhouse())
		return -ENODEV;

	/*
	 * VINO is in the EISA address space, so the sysid register will tell
	 * us if the EISA_PRESENT pin on MC has been pulled low.
	 *
	 * If EISA_PRESENT is not set we definitely don't have a VINO equiped
	 * system.
	 */
	if (!(sgimc->systemid & SGIMC_SYSID_EPRESENT)) {
		printk(KERN_ERR "VINO not found\n");
		return -ENODEV;
	}

	vino = (struct sgi_vino *)ioremap(VINO_BASE, sizeof(struct sgi_vino));
	if (!vino)
		return -EIO;

	/* Okay, once we know that VINO is present we'll read its revision
	 * safe way. One never knows... */
	if (get_dbe(rev, &(vino->rev_id))) {
		printk(KERN_ERR "VINO: failed to read revision register\n");
		ret = -ENODEV;
		goto out_unmap;
	}
	if (VINO_ID_VALUE(rev) != VINO_CHIP_ID) {
		printk(KERN_ERR "VINO is not VINO (Rev/ID: 0x%04lx)\n", rev);
		ret = -ENODEV;
		goto out_unmap;
	}
	printk(KERN_INFO "VINO Rev: 0x%02lx\n", VINO_REV_NUM(rev));

	Vino = (struct vino_video *)
		kmalloc(sizeof(struct vino_video), GFP_KERNEL);
	if (!Vino) {
		ret = -ENOMEM;
		goto out_unmap;
	}

	Vino->dummy_page = get_zeroed_page(GFP_KERNEL | GFP_DMA);
	if (!Vino->dummy_page) {
		ret = -ENOMEM;
		goto out_free_vino;
	}
	for (i = 0; i < 4; i++)
		Vino->dummy_buf[i] = PHYSADDR(Vino->dummy_page);

	vino->control = 0;
	/* prevent VINO from throwing spurious interrupts */
	vino->a.next_4_desc = PHYSADDR(Vino->dummy_buf);
	vino->b.next_4_desc = PHYSADDR(Vino->dummy_buf);
	udelay(5);
	vino->intr_status = 0;
        /* set threshold level */
        vino->a.fifo_thres = threshold_a;
	vino->b.fifo_thres = threshold_b;

	init_MUTEX(&Vino->input_lock);

	if (request_irq(SGI_VINO_IRQ, vino_interrupt, 0, vinostr, NULL)) {
		printk(KERN_ERR "VINO: irq%02d registration failed\n",
		       SGI_VINO_IRQ);
		ret = -EAGAIN;
		goto out_free_page;
	}

	ret = vino_i2c_add_bus();
	if (ret) {
		printk(KERN_ERR "VINO: I2C bus registration failed\n");
		goto out_free_irq;
	}

	if (video_register_device(&Vino->chA.vdev, VFL_TYPE_GRABBER, -1) < 0) {
		printk("%s, chnl %d: device registration failed.\n",
			Vino->chA.vdev.name, Vino->chA.chan);
		ret = -EINVAL;
		goto out_i2c_del_bus;
	}
	if (video_register_device(&Vino->chB.vdev, VFL_TYPE_GRABBER, -1) < 0) {
		printk("%s, chnl %d: device registration failed.\n",
			Vino->chB.vdev.name, Vino->chB.chan);
		ret = -EINVAL;
		goto out_unregister_vdev;
	}

	return 0;

out_unregister_vdev:
	video_unregister_device(&Vino->chA.vdev);
out_i2c_del_bus:
	vino_i2c_del_bus();
out_free_irq:
	free_irq(SGI_VINO_IRQ, NULL);
out_free_page:
	free_page(Vino->dummy_page);
out_free_vino:
	kfree(Vino);
out_unmap:
	iounmap(vino);

	return ret;
}

static void __exit vino_exit(void)
{
	video_unregister_device(&Vino->chA.vdev);
	video_unregister_device(&Vino->chB.vdev);
	vino_i2c_del_bus();
	free_irq(SGI_VINO_IRQ, NULL);
	free_page(Vino->dummy_page);
	kfree(Vino);
	iounmap(vino);
}

module_init(vino_init);
module_exit(vino_exit);

MODULE_DESCRIPTION("Video4Linux driver for SGI Indy VINO (IndyCam)");
MODULE_LICENSE("GPL");