summaryrefslogtreecommitdiffstats
path: root/sys/i386/isa/qcam.c
blob: 006803591c350dc5fa3947d134c6ff2d46b5568b (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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/*
 * FreeBSD Connectix QuickCam parallel-port camera video capture driver.
 * Copyright (c) 1996, Paul Traina.
 *
 * This driver is based in part on the Linux QuickCam driver which is
 * Copyright (c) 1996, Thomas Davis.
 *
 * Additional ideas from code written by Michael Chinn.
 *
 * QuickCam(TM) is a registered trademark of Connectix Inc.
 * Use this driver at your own risk, it is not warranted by
 * Connectix or the authors.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer
 *    in this position and unchanged.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software withough specific prior written permission
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include	"qcam.h"
#if NQCAM > 0

#include	<sys/param.h>
#include	<sys/systm.h>
#include	<sys/kernel.h>
#include	<sys/conf.h>
#include	<sys/ioctl.h>
#include	<sys/uio.h>
#include	<sys/malloc.h>
#include	<sys/devconf.h>
#include	<sys/errno.h>
#ifdef DEVFS
#include	<sys/devfsext.h>
#endif /* DEVFS */

#include	<machine/clock.h>
#include	<machine/qcam.h>

#include	<i386/isa/qcamreg.h>
#include	<i386/isa/qcamdefs.h>
#include	<i386/isa/isa.h>
#include	<i386/isa/isa_device.h>

int	qcam_debug = 1;

static struct qcam_softc qcam_softc[NQCAM];

#define	QC_CONF_NODETECT 0x01		/* always assume camera is present */
#define	QC_CONF_FORCEUNI 0x02		/* force unidirectional transfers */

static struct kern_devconf kdc_qcam_template = {
	0, 0, 0,			/* filled in by dev_attach() */
	"qcam",				/* kdc_name */
	0, 				/* kdc_unit */
	{ 				/* kdc_md */
  	   MDDT_ISA,			/* mddc_devtype */
	   0,				/* mddc_flags */
	   "tty"			/* mddc_imask[4] */
	},
	isa_generic_externalize,	/* kdc_externalize */
	0,				/* kdc_internalize */
	0,				/* kdc_goaway */
	ISA_EXTERNALLEN,		/* kdc_datalen */
	&kdc_isa0,			/* kdc_parent */
	0,				/* kdc_parentdata */
	DC_UNCONFIGURED,		/* kdc_state */
	"QuickCam video input", 	/* kdc_description */
	DC_CLS_MISC			/* class */
};

#define	UNIT(dev)		minor(dev)

static int qcam_probe(struct isa_device *id);
static int qcam_attach(struct isa_device *id);

struct isa_driver	qcamdriver =
			{qcam_probe, qcam_attach, "qcam"};

static	d_open_t	qcam_open;
static	d_close_t	qcam_close;
static	d_read_t	qcam_read;
static	d_ioctl_t	qcam_ioctl;

#define CDEV_MAJOR 73

static struct cdevsw qcam_cdevsw = 
	{ qcam_open,	qcam_close,	qcam_read,	nowrite,
	  qcam_ioctl,	nostop,		nullreset,	nodevtotty,
	  noselect,	nommap,		nostrategy,	"qcam",
	  NULL,		-1  };

static void
qcam_registerdev (struct isa_device *id)
{
	struct kern_devconf *kdc = &qcam_softc[id->id_unit].kdc;

	*kdc = kdc_qcam_template;		/* byte-copy template */

	kdc->kdc_unit = id->id_unit;
	kdc->kdc_parentdata = id;

#ifndef	ACTUALLY_LKM_NOT_KERNEL		/* there's a bug in dev_attach
					   when running from an LKM */
	dev_attach(kdc);
#endif
}

static int
qcam_probe (struct isa_device *devp)
{
	u_char reg, last;
	int i, transitions = 0;

	switch (devp->id_iobase) {	/* don't probe weird ports */
	case IO_LPT1:
	case IO_LPT2:
	case IO_LPT3:
		break;
	default:
		printf("qcam%d: ignoring non-standard port 0x%x\n",
		       devp->id_unit, devp->id_iobase);
		return 0;
	}

	/*
	 * XXX The probe code is reported to be flakey.
	 *     We need to work on this some more, so temporarily,
	 *     allow bit one of the "flags" parameter to bypass this
	 *     check.
	 */

	if (!(devp->id_flags & QC_CONF_NODETECT))
	    if (!qcam_detect(devp->id_iobase))
		return 0;	/* failure */

	qcam_registerdev(devp);
	return 1;		/* found */
}

static int
qcam_attach (struct isa_device *devp)
{
	struct	qcam_softc *qs = &qcam_softc[devp->id_unit];

	qs->iobase	 = devp->id_iobase;
	qs->unit	 = devp->id_unit;
	qs->kdc.kdc_state = DC_IDLE;
	qs->flags |= QC_ALIVE;

	/* force unidirectional parallel port mode? */
	if (devp->id_flags & QC_CONF_FORCEUNI)
		qs->flags |= QC_FORCEUNI;

	qcam_reset(qs);

	printf("qcam%d: %sdirectional parallel port\n",
	       qs->unit, qs->flags & QC_BIDIR_HW ? "bi" : "uni");

#ifdef DEVFS
	qs->devfs_token = 
		devfs_add_devswf(&qcam_cdevsw, qs->unit, DV_CHR, 0, 0, 0600, 
				 "qcam%d", qs->unit);
#endif
	return 1;
}

static int
qcam_open (dev_t dev, int flags, int fmt, struct proc *p)
{
	struct qcam_softc *qs = &qcam_softc[UNIT(dev)];

	if (!(qs->flags & QC_ALIVE))
		return ENXIO;

	if (qs->flags & QC_OPEN)
		return EBUSY;

	qs->buffer_end = qs->buffer = malloc(QC_MAXFRAMEBUFSIZE, M_DEVBUF,
					     M_WAITOK);
	if (!qs->buffer)
		return ENOMEM;

	qcam_reset(qs);
	qcam_default(qs);
	qs->init_req = 1;	/* request initialization before scan */

	qs->flags |= QC_OPEN;
	qs->kdc.kdc_state = DC_BUSY;

	return 0;
}

static int
qcam_close (dev_t dev, int flags, int fmt, struct proc *p)
{
	struct qcam_softc *qs = &qcam_softc[UNIT(dev)];

	if (qs->buffer) {
	    free(qs->buffer, M_DEVBUF);
	    qs->buffer = NULL;
	    qs->buffer_end = NULL;
	}

	qs->flags &= ~QC_OPEN;
	qs->kdc.kdc_state = DC_IDLE;
	return 0;
}

static int
qcam_read (dev_t dev, struct uio *uio, int ioflag)
{
	struct qcam_softc *qs = &qcam_softc[UNIT(dev)];
	int bytes, bufsize;
	int error;

	/* if we've seeked back to 0, that's our signal to scan */
	if (uio->uio_offset == 0)
		if (qcam_scan(qs))
			return EIO;

	bufsize = qs->buffer_end - qs->buffer;
	if (uio->uio_offset > bufsize)
		return EIO;

	bytes = min(uio->uio_resid, (bufsize - uio->uio_offset));
	error = uiomove(qs->buffer + uio->uio_offset, bytes, uio);
	if (error)
		return error;

	return 0;		/* success */
}

static int
qcam_ioctl (dev_t dev, int cmd, caddr_t data, int flag, struct proc *p)
{
	struct qcam_softc *qs = &qcam_softc[UNIT(dev)];
	struct qcam      *info = (struct qcam *)data;

	if (!data)
		return(EINVAL);

	switch (cmd) {

	case QC_GET:
		info->qc_version	= QC_IOCTL_VERSION;
		info->qc_xsize		= qs->x_size;
		info->qc_ysize		= qs->y_size;
		info->qc_xorigin	= qs->x_origin;
		info->qc_yorigin	= qs->y_origin;
		info->qc_bpp		= qs->bpp;
		info->qc_zoom		= qs->zoom;
		info->qc_exposure	= qs->exposure;
		info->qc_brightness	= qs->brightness;
		info->qc_whitebalance	= qs->whitebalance;
		info->qc_contrast	= qs->contrast;
		break;

	case QC_SET:
		/*
		 * sanity check parameters passed in by user
		 * we're extra paranoid right now because the API
		 * is in flux
		 */
		if (info->qc_xsize        > QC_MAX_XSIZE	||
		    info->qc_ysize        > QC_MAX_YSIZE	||
		    info->qc_xorigin      > QC_MAX_XSIZE	||
		    info->qc_yorigin      > QC_MAX_YSIZE	||
		    (info->qc_bpp != 4 && info->qc_bpp != 6)	||
		    info->qc_zoom         > QC_ZOOM_200		||
		    info->qc_brightness   > UCHAR_MAX		||
		    info->qc_whitebalance > UCHAR_MAX		||
		    info->qc_contrast     > UCHAR_MAX)
			return EINVAL;

		/* version check */
		if (info->qc_version != QC_IOCTL_VERSION)
			return EINVAL;

		qs->x_size	  = info->qc_xsize;
		qs->y_size	  = info->qc_ysize;
		qs->x_origin	  = info->qc_xorigin;
		qs->y_origin	  = info->qc_yorigin;
		qs->bpp		  = info->qc_bpp;
		qs->zoom	  = info->qc_zoom;
		qs->exposure	  = info->qc_exposure;
		qs->brightness	  = info->qc_brightness;
		qs->whitebalance  = info->qc_whitebalance;
		qs->contrast	  = info->qc_contrast;

		/* request initialization before next scan pass */
		qs->init_req = 1;
		break;

	default:
		return ENOTTY;
	}

	return 0;
}

/*
 * Initialize the dynamic cdevsw hooks.
 */
static void
qcam_drvinit (void *unused)
{
	static int qcam_devsw_installed = 0;
	dev_t dev;

	if (!qcam_devsw_installed) {
		dev = makedev(CDEV_MAJOR, 0);
		cdevsw_add(&dev,&qcam_cdevsw, NULL);
		qcam_devsw_installed++;
    	}
}

SYSINIT(qcamdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,qcam_drvinit,NULL)

#ifdef QCAM_MODULE

#include <sys/exec.h>
#include <sys/sysent.h>
#include <sys/sysproto.h>
#include <sys/lkm.h>

static struct isa_device qcam_mod_dev =
	{0, &qcamdriver, IO_LPT1, 0, -1, (caddr_t) 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 1, 0, 0};

MOD_DEV(qcam, LM_DT_CHAR, CDEV_MAJOR, &qcam_cdevsw);

static int 
qcam_load (struct lkm_table *lkmtp, int cmd)
{
	if (qcam_probe(&qcam_mod_dev)) {
	 	qcam_attach(&qcam_mod_dev);

		qcam_drvinit(NULL); /* XXX this shouldn't NEED to be here
				     * the LKM code should be doing this
				     * for us! */

	 	uprintf("qcam: driver loaded\n");
		return 0;
	} else {
		uprintf("qcam: probe failed\n");
		return 1;
	}
}

static int
qcam_unload (struct lkm_table *lkmtp, int cmd)
{
	struct qcam_softc *qs;
	int i;

	for (i = 0; i < NQCAM; i++) {
		qs = &qcam_softc[i];
		if (qs->flags & QC_OPEN) {
			uprintf("qcam%d: cannot unload, device busy", qs->unit);
			return 1;
		}
	}

	uprintf("qcam: driver unloaded\n");
	return 0;
}

static int
qcam_stat (struct lkm_table *lkmtp, int cmd)
{
	return 0;
}

int
qcam_mod (struct lkm_table *lkmtp, int cmd, int ver)
{
#define _module qcam_module
	DISPATCH(lkmtp, cmd, ver,
		 qcam_load, qcam_unload, qcam_stat);
}

#endif /* QCAM_MODULE */
#endif /* NQCAM */
OpenPOWER on IntegriCloud