Linux Audio

Check our new training course

Embedded Linux Audio

Check our new training course
with Creative Commons CC-BY-SA
lecture materials

Bootlin logo

Elixir Cross Referencer

Loading...
  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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
/* -*- linux-c -*-
 * USB ViCAM driver
 *
 * Copyright (c) 2001 Christopher L Cheney (ccheney@cheney.cx)
 * Copyright (c) 2001 Pavel Machek (pavel@suse.cz) sponsored by SuSE
 *
 *      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 driver is for the Vista Imaging ViCAM and 3Com HomeConnect USB
 *
 * Thanks to Greg Kroah-Hartman for the USB Skeleton driver
 *
 * TODO:
 *	- find out the ids for the Vista Imaging ViCAM
 *
 * History:
 *
 * 2001_07_07 - 0.1 - christopher: first version
 * 2001_08_28 - 0.2 - pavel: messed it up, but for some fun, try 
 			while true; do dd if=/dev/video of=/dev/fb0 bs=$[0x1e480] count=1 2> /dev/null; done
		      yep, moving pictures.
 * 2001_08_29 - 0.3 - pavel: played a little bit more. Experimental mmap support. For some fun,
 			get gqcam-0.9, compile it and run. Better than dd ;-).
 * 2001_08_29 - 0.4 - pavel: added shutter speed control (not much functional)
 			kill update_params if it does not seem to work for you.
 * 2001_08_30 - 0.5 - pavel: fixed stupid bug with update_params & vicam_bulk

 *
 * FIXME: It crashes on rmmod with camera plugged.
 */
#define DEBUG 1

#include <linux/config.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/signal.h>
#include <linux/errno.h>
#include <linux/poll.h>
#include <linux/init.h>
#include <linux/mm.h>
#include <linux/pagemap.h>
#include <linux/slab.h>
#include <linux/fcntl.h>
#include <linux/module.h>
#include <linux/spinlock.h>
#include <linux/list.h>
#include <linux/smp_lock.h>
#include <linux/devfs_fs_kernel.h>
#include <linux/usb.h>

#include <asm/io.h>
#include <linux/wrapper.h>
#include <linux/vmalloc.h>

#include <linux/videodev.h>

#include "vicam.h"
#include "vicamurbs.h"

/* Version Information */
#define DRIVER_VERSION "v0"
#define DRIVER_AUTHOR "Christopher L Cheney <ccheney@cheney.cx>, Pavel Machek <pavel@suse.cz>"
#define DRIVER_DESC "USB ViCAM Driver"

/* Define these values to match your device */
#define USB_VICAM_VENDOR_ID	0x04C1
#define USB_VICAM_PRODUCT_ID	0x009D

/* table of devices that work with this driver */
static struct usb_device_id vicam_table [] = {
	{ USB_DEVICE(USB_VICAM_VENDOR_ID, USB_VICAM_PRODUCT_ID) },
	{ }					/* Terminating entry */
};

MODULE_DEVICE_TABLE (usb, vicam_table);

static int video_nr = -1; 		/* next avail video device */
static struct usb_driver vicam_driver;

static char *buf, *buf2;
static volatile int change_pending = 0; 

static int vicam_parameters(struct usb_vicam *vicam);

/******************************************************************************
 *
 *  Memory management functions
 *
 *  Taken from bttv-drivers.c 2.4.7-pre3
 *
 ******************************************************************************/

/* Here we want the physical address of the memory.
 * This is used when initializing the contents of the area.
 */
static inline unsigned long kvirt_to_pa(unsigned long adr)
{
	unsigned long kva, ret;

	kva = (unsigned long) page_address(vmalloc_to_page((void *)adr));
	kva |= adr & (PAGE_SIZE-1); /* restore the offset */
	ret = __pa(kva);
	return ret;
}

static void * rvmalloc(unsigned long size)
{
	void * mem;
	unsigned long adr;

	size=PAGE_ALIGN(size);
	mem=vmalloc_32(size);
	if (mem)
	{
		memset(mem, 0, size); /* Clear the ram out, no junk to the user */
		adr=(unsigned long) mem;
		while (size > 0)
		{
			mem_map_reserve(vmalloc_to_page((void *)adr));
			adr+=PAGE_SIZE;
			size-=PAGE_SIZE;
		}
	}
	return mem;
}

static void rvfree(void * mem, unsigned long size)
{
	unsigned long adr;

	if (mem)
	{
		adr=(unsigned long) mem;
		while ((long) size > 0)
		{
			mem_map_unreserve(vmalloc_to_page((void *)adr));
			adr+=PAGE_SIZE;
			size-=PAGE_SIZE;
		}
		vfree(mem);
	}
}

/******************************************************************************
 *
 *  Foo Bar
 *
 ******************************************************************************/

/**
 *	usb_vicam_debug_data
 */
static inline void usb_vicam_debug_data (const char *function, int size, const unsigned char *data)
{
	int i;

	if (!debug)
		return;

	printk (KERN_DEBUG __FILE__": %s - length = %d, data = ",
		function, size);
	for (i = 0; i < size; ++i) {
		printk ("%.2x ", data[i]);
	}
	printk ("\n");
}

/*****************************************************************************
 *
 *  Send command to vicam
 *
 *****************************************************************************/

static int vicam_sndctrl(int set, struct usb_vicam *vicam, unsigned short req,
	unsigned short value, unsigned char *cp, int size)
{
	int ret;
	unsigned char *transfer_buffer = kmalloc (size, GFP_KERNEL);

	/* Needs to return data I think, works for sending though */
	memcpy(transfer_buffer, cp, size);
	
	ret = usb_control_msg ( vicam->udev, set ? usb_sndctrlpipe(vicam->udev, 0) : usb_rcvctrlpipe(vicam->udev, 0), req, (set ? USB_DIR_OUT : USB_DIR_IN) | USB_TYPE_VENDOR | USB_RECIP_DEVICE, value, 0, transfer_buffer, size, HZ);

	kfree(transfer_buffer);
	if (ret)
		printk("vicam: error: %d\n", ret);
	mdelay(100);
	return ret;
}


/*****************************************************************************
 *
 *  Video4Linux Helpers
 * 
 *****************************************************************************/

static int vicam_get_capability(struct usb_vicam *vicam, struct video_capability *b)
{
	dbg("vicam_get_capability");

	strcpy(b->name, vicam->camera_name);
	b->type = VID_TYPE_CAPTURE | VID_TYPE_MONOCHROME;
	b->channels = 1;
	b->audios = 0;

	b->maxwidth = vicam->width[vicam->sizes-1];
	b->maxheight = vicam->height[vicam->sizes-1];
	b->minwidth = vicam->width[0];
	b->minheight = vicam->height[0];

	return 0;
}
		
static int vicam_get_channel(struct usb_vicam *vicam, struct video_channel *v)
{
	dbg("vicam_get_channel");

	if (v->channel != 0)
		return -EINVAL;
 
	v->flags = 0;
	v->tuners = 0;
	v->type = VIDEO_TYPE_CAMERA;
	strcpy(v->name, "Camera");

	return 0;
} 
		
static int vicam_set_channel(struct usb_vicam *vicam, struct video_channel *v)
{
	dbg("vicam_set_channel");

	if (v->channel != 0)
		return -EINVAL;
	
	return 0;
}
		
static int vicam_get_mmapbuffer(struct usb_vicam *vicam, struct video_mbuf *vm)
{
	int i;

	dbg("vicam_get_mmapbuffer");

	memset(vm, 0, sizeof(vm));
	vm->size = VICAM_NUMFRAMES * vicam->maxframesize;
	vm->frames = VICAM_NUMFRAMES;

	for (i=0; i<VICAM_NUMFRAMES; i++)
		vm->offsets[i] = vicam->maxframesize * i;

	return 0;
}

static int vicam_get_picture(struct usb_vicam *vicam, struct video_picture *p)
{
	dbg("vicam_get_picture");

	/* This is probably where that weird 0x56 call goes */
	p->brightness = vicam->win.brightness;
	p->hue = vicam->win.hue;
	p->colour = vicam->win.colour;
	p->contrast = vicam->win.contrast;
	p->whiteness = vicam->win.whiteness;
	p->depth = vicam->win.depth;
	p->palette = vicam->win.palette;

	return 0;
}

static void synchronize(struct usb_vicam *vicam)
{
	DECLARE_WAITQUEUE(wait, current);
	change_pending = 1;
	set_current_state(TASK_INTERRUPTIBLE);
	add_wait_queue(&vicam->wait, &wait);
	if (change_pending)
		schedule();
	remove_wait_queue(&vicam->wait, &wait);
	set_current_state(TASK_RUNNING);
	vicam_sndctrl(1, vicam, VICAM_REQ_CAMERA_POWER, 0x00, NULL, 0);
	mdelay(10);
	vicam_sndctrl(1, vicam, VICAM_REQ_LED_CONTROL, 0x00, NULL, 0);
	mdelay(10);
}

static void params_changed(struct usb_vicam *vicam)
{
#if 1
	synchronize(vicam);
	mdelay(10);
	vicam_parameters(vicam);
	printk(KERN_DEBUG "Submiting urb: %d\n", usb_submit_urb(vicam->readurb, GFP_KERNEL));
#endif
}

static int vicam_set_picture(struct usb_vicam *vicam, struct video_picture *p)
{
	int changed = 0;
	info("vicam_set_picture (%d)", p->brightness);


#define SET(x) \
	if (vicam->win.x != p->x) \
		vicam->win.x = p->x, changed = 1;
	SET(brightness);
	SET(hue);
	SET(colour);
	SET(contrast);
	SET(whiteness);
	SET(depth);
	SET(palette);
	if (changed)
		params_changed(vicam);

	return 0;
	/* Investigate what should be done maybe 0x56 type call */
	if (p->depth != 8) return 1;
	if (p->palette != VIDEO_PALETTE_GREY) return 1;

	return 0;
}

/* FIXME - vicam_sync_frame - important */
static int vicam_sync_frame(struct usb_vicam *vicam, int frame)
{
	dbg("vicam_sync_frame");

	if(frame <0 || frame >= VICAM_NUMFRAMES)
		return -EINVAL;

	/* Probably need to handle various cases */
/*	ret=vicam_newframe(vicam, frame);
	vicam->frame[frame].grabstate=FRAME_UNUSED;
*/
	return 0;
}
	
static int vicam_get_window(struct usb_vicam *vicam, struct video_window *vw)
{
	dbg("vicam_get_window");

	vw->x = 0;
	vw->y = 0;
	vw->chromakey = 0;
	vw->flags = 0;
	vw->clipcount = 0;
	vw->width = vicam->win.width;
	vw->height = vicam->win.height;

	return 0;
}

static int vicam_set_window(struct usb_vicam *vicam, struct video_window *vw)
{
	info("vicam_set_window");
		
	if (vw->flags)
		return -EINVAL;
	if (vw->clipcount)
		return -EINVAL;

	if (vicam->win.width == vw->width && vicam->win.height == vw->height)
		return 0;

	/* Pick largest mode that is smaller than specified res */
	/* If specified res is too small reject                 */

	/* Add urb send to device... */

	vicam->win.width = vw->width;
	vicam->win.height = vw->height;
	params_changed(vicam);

	return 0;
}

/* FIXME - vicam_mmap_capture - important */
static int vicam_mmap_capture(struct usb_vicam *vicam, struct video_mmap *vm)
{
	dbg("vicam_mmap_capture");

	/* usbvideo.c looks good for using here */

	/* 
	if (vm->frame >= VICAM_NUMFRAMES)
		return -EINVAL;
	if (vicam->frame[vm->frame].grabstate != FRAME_UNUSED)
		return -EBUSY;
	vicam->frame[vm->frame].grabstate=FRAME_READY;
	*/

	/* No need to vicam_set_window here according to Alan */

	/*
	if (!vicam->streaming)
		vicam_start_stream(vicam);
	*/

	/* set frame as ready */

	return 0;
}

/*****************************************************************************
 *
 *  Video4Linux
 * 
 *****************************************************************************/

static int vicam_v4l_open(struct inode *inode, struct file *file)
{
	struct video_device *vdev = video_devdata(file);
	struct usb_vicam *vicam = (struct usb_vicam *)vdev;
	int err = 0;
	
	dbg("vicam_v4l_open");
	down(&vicam->sem);

	vicam->fbuf = rvmalloc(vicam->maxframesize * VICAM_NUMFRAMES);
	if (vicam->open_count) {
		err = -EBUSY;
	} else if (!vicam->fbuf) {
		err =- ENOMEM;
	} else {
#ifdef BLINKING
		vicam_sndctrl(1, vicam, VICAM_REQ_CAMERA_POWER, 0x01, NULL, 0);
		info ("led on");
		vicam_sndctrl(1, vicam, VICAM_REQ_LED_CONTROL, 0x01, NULL, 0);
#endif
		vicam->open_count++;
		file->private_data = vdev;
	}

	up(&vicam->sem);
	return err;
}

static int vicam_v4l_close(struct inode *inode, struct file *file)
{
	struct video_device *vdev = file->private_data;
	struct usb_vicam *vicam = (struct usb_vicam *)vdev;

	dbg("vicam_v4l_close");
	
	down(&vicam->sem);

#ifdef BLINKING
	info ("led off");
	vicam_sndctrl(1, vicam, VICAM_REQ_LED_CONTROL, 0x00, NULL, 0);
//	vicam_sndctrl(1, vicam, VICAM_REQ_CAMERA_POWER, 0x00, NULL, 0); Leave it on
#endif

	rvfree(vicam->fbuf, vicam->maxframesize * VICAM_NUMFRAMES);
	vicam->fbuf = 0;
	vicam->open_count=0;
	file->private_data = NULL;

	up(&vicam->sem);
	/* Why does se401.c have a usbdevice check here? */
	/* If device is unplugged while open, I guess we only may unregister now */
	return 0;
}

static int vicam_v4l_read(struct file *file, char *user_buf,
			  size_t buflen, loff_t *ppos)
{
	struct video_device *vdev = file->private_data;
	//struct usb_vicam *vicam = (struct usb_vicam *)vdev;

	dbg("vicam_v4l_read(%d)", buflen);

	if (!vdev || !buf)
		return -EFAULT;

	if (copy_to_user(user_buf, buf2, buflen))
		return -EFAULT;
	return buflen;
}

static int vicam_v4l_do_ioctl(struct inode *inode, struct file *file,
			      unsigned int cmd, void *arg)
{
	struct video_device *vdev = file->private_data;
	struct usb_vicam *vicam = (struct usb_vicam *)vdev;
	int ret = -EL3RST;

	if (!vicam->udev)
		return -EIO;

	down(&vicam->sem);

	switch (cmd) {
	case VIDIOCGCAP:
	{
		struct video_capability *b = arg;
		ret = vicam_get_capability(vicam,b);
		dbg("name %s",b->name);
		break;
	}
	case VIDIOCGFBUF:
	{
		struct video_buffer *vb = arg;
		info("vicam_v4l_ioctl - VIDIOCGBUF - query frame buffer param");
		/* frame buffer not supported, not used */
		memset(vb, 0, sizeof(*vb));
		ret = 0;
		break;
	}
	case VIDIOCGWIN:
	{
		struct video_window *vw = arg;
		ret = vicam_get_window(vicam, vw);
		break;
	}
	case VIDIOCSWIN:
	{
		struct video_window *vw = arg;
		ret = vicam_set_window(vicam, vw);
		break;
	}
	case VIDIOCGCHAN:
	{
		struct video_channel *v = arg;

		ret = vicam_get_channel(vicam,v);
		break;
	}
	case VIDIOCSCHAN:
	{
		struct video_channel *v = arg;
		ret = vicam_set_channel(vicam,v);
		break;
 	}
	case VIDIOCGPICT:
	{
		struct video_picture *p = arg;
		ret = vicam_get_picture(vicam,p);
		break;
	}
	case VIDIOCSPICT:
	{
		struct video_picture *p = arg;
		ret = vicam_set_picture(vicam,p);
		break;
	}
	case VIDIOCGMBUF:
	{
		struct video_mbuf *vm = arg;
		ret = vicam_get_mmapbuffer(vicam,vm);
		break;
	}
	case VIDIOCMCAPTURE:
	{
		struct video_mmap *vm = arg;
		ret = vicam_mmap_capture(vicam,vm);
		break;
	}
	case VIDIOCSYNC:
	{
		int *frame = arg;
		ret = vicam_sync_frame(vicam,*frame);
		break;
	}

	case VIDIOCKEY:
		ret = 0;
 
	case VIDIOCCAPTURE:
	case VIDIOCSFBUF:
	case VIDIOCGTUNER:
	case VIDIOCSTUNER:
	case VIDIOCGFREQ:
	case VIDIOCSFREQ:
	case VIDIOCGAUDIO:
	case VIDIOCSAUDIO:
	case VIDIOCGUNIT:
		ret = -EINVAL;

	default:
	{
		info("vicam_v4l_ioctl - %ui",cmd);
		ret = -ENOIOCTLCMD;
	}
	} /* end switch */

	up(&vicam->sem);
        return ret;
}

static int vicam_v4l_ioctl(struct inode *inode, struct file *file,
			   unsigned int cmd, unsigned long arg)
{
	return video_usercopy(inode, file, cmd, arg, vicam_v4l_do_ioctl);
}

static int vicam_v4l_mmap(struct file *file, struct vm_area_struct *vma)
{
	struct video_device *vdev = file->private_data;
	struct usb_vicam *vicam = (struct usb_vicam *)vdev;
	unsigned long start = vma->vm_start;
	unsigned long size  = vma->vm_end-vma->vm_start;
	unsigned long page, pos;

	down(&vicam->sem);
	
	if (vicam->udev == NULL) {
		up(&vicam->sem);
		return -EIO;
	}
#if 0
	if (size > (((VICAM_NUMFRAMES * vicam->maxframesize) + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1))) {
		up(&vicam->sem);
		return -EINVAL;
	}
#endif
	pos = (unsigned long)vicam->fbuf;
	while (size > 0) {
		page = kvirt_to_pa(pos);
		if (remap_page_range(vma, start, page, PAGE_SIZE, PAGE_SHARED)) {
			up(&vicam->sem);
			return -EAGAIN;
		}
		start += PAGE_SIZE;
		pos += PAGE_SIZE;
		if (size > PAGE_SIZE)
			size -= PAGE_SIZE;
		else
			size = 0;
	}
	up(&vicam->sem);

        return 0;
}

/* FIXME - vicam_template - important */
static struct file_operations vicam_fops = {
	.owner =	THIS_MODULE,
	.open =		vicam_v4l_open,
	.release =     	vicam_v4l_close,
	.read =		vicam_v4l_read,
	.mmap =		vicam_v4l_mmap,
	.ioctl =	vicam_v4l_ioctl,
	.llseek =       no_llseek,
};
static struct video_device vicam_template = {
	.owner =	THIS_MODULE,
	.name =		"vicam USB camera",
	.type =		VID_TYPE_CAPTURE,
	.hardware =	VID_HARDWARE_SE401, /* need to ask for own id */
	.fops =         &vicam_fops,
};

/******************************************************************************
 *
 *  Some Routines
 *
 ******************************************************************************/

/*
Flash the led
vicam_sndctrl(1, vicam, VICAM_REQ_CAMERA_POWER, 0x01, NULL, 0);
info ("led on");
vicam_sndctrl(1, vicam, VICAM_REQ_LED_CONTROL, 0x01, NULL, 0);
info ("led off");
vicam_sndctrl(1, vicam, VICAM_REQ_LED_CONTROL, 0x00, NULL, 0);
vicam_sndctrl(1, vicam, VICAM_REQ_CAMERA_POWER, 0x00, NULL, 0);
*/

static void vicam_bulk(struct urb *urb)
{
	struct usb_vicam *vicam = urb->context;

	/*	if (!vicam || !vicam->dev || !vicam->used)
		return;
	*/

	if (urb->status)
		printk("vicam%d: nonzero read/write bulk status received: %d",
			0, urb->status);

	urb->actual_length = 0;
	urb->dev = vicam->udev;

	memcpy(buf2, buf+64, 0x1e480);
	if (vicam->fbuf)
		memcpy(vicam->fbuf, buf+64, 0x1e480);

	if (!change_pending) {
		if (usb_submit_urb(urb, GFP_ATOMIC))
			dbg("failed resubmitting read urb");
	} else {
		change_pending = 0;
		wake_up_interruptible(&vicam->wait);
	}
}

static int vicam_parameters(struct usb_vicam *vicam)
{
	unsigned char req[0x10];
	unsigned int shutter;
	shutter = 10;

	switch (vicam->win.width) {
	case 512:
	default:
		memcpy(req, s512x242bw, 0x10);
		break;
	case 256:
		memcpy(req, s256x242bw, 0x10);
		break;
	case 128:
		memcpy(req, s128x122bw, 0x10);
		break;
	}


	mdelay(10);
	vicam_sndctrl(1, vicam, VICAM_REQ_CAMERA_POWER, 0x01, NULL, 0);
	info ("led on");
	vicam_sndctrl(1, vicam, VICAM_REQ_LED_CONTROL, 0x01, NULL, 0);

	mdelay(10);

	shutter = vicam->win.contrast / 256;
	if (shutter == 0)
		shutter = 1;
	printk("vicam_parameters: brightness %d, shutter %d\n", vicam->win.brightness, shutter );
	req[0] = vicam->win.brightness /256;
	shutter = 15600/shutter - 1;
	req[6] = shutter & 0xff;
	req[7] = (shutter >> 8) & 0xff;
	vicam_sndctrl(1, vicam, VICAM_REQ_CAPTURE, 0x80, req, 0x10);
	mdelay(10);
	vicam_sndctrl(0, vicam, VICAM_REQ_GET_SOMETHIN, 0, buf, 0x10);
	mdelay(10);

	return 0;
}

static int vicam_init(struct usb_vicam *vicam)
{
	int width[] = {128, 256, 512};
	int height[] = {122, 242, 242};

	dbg("vicam_init");
	buf = kmalloc(0x1e480, GFP_KERNEL);
	buf2 = kmalloc(0x1e480, GFP_KERNEL);
	if ((!buf) || (!buf2)) {
		printk("Not enough memory for vicam!\n");
		goto error;
	}

	/* do we do aspect correction in kernel or not? */
	vicam->sizes = 3;
	vicam->width = kmalloc(vicam->sizes*sizeof(int), GFP_KERNEL);
	vicam->height = kmalloc(vicam->sizes*sizeof(int), GFP_KERNEL);
	memcpy(vicam->width, &width, sizeof(width));
	memcpy(vicam->height, &height, sizeof(height));
	vicam->maxframesize = vicam->width[vicam->sizes-1] * vicam->height[vicam->sizes-1];

	/* Download firmware to camera */
	vicam_sndctrl(1, vicam, VICAM_REQ_VENDOR, 0, firmware1, sizeof(firmware1));
	vicam_sndctrl(1, vicam, VICAM_REQ_VENDOR, 0, findex1, sizeof(findex1));
	vicam_sndctrl(1, vicam, VICAM_REQ_VENDOR, 0, fsetup, sizeof(fsetup));
	vicam_sndctrl(1, vicam, VICAM_REQ_VENDOR, 0, firmware2, sizeof(firmware2));
	vicam_sndctrl(1, vicam, VICAM_REQ_VENDOR, 0, findex2, sizeof(findex2));
	vicam_sndctrl(1, vicam, VICAM_REQ_VENDOR, 0, fsetup, sizeof(fsetup));

	vicam_parameters(vicam);

	FILL_BULK_URB(vicam->readurb, vicam->udev, usb_rcvbulkpipe(vicam->udev, 0x81),
		      buf, 0x1e480, vicam_bulk, vicam);
	printk(KERN_DEBUG "Submiting urb: %d\n", usb_submit_urb(vicam->readurb, GFP_KERNEL));

	return 0;
error:
	if (buf)
		kfree(buf);
	if (buf2)
		kfree(buf2);
	return 1;
}

static void *vicam_probe(struct usb_device *udev, unsigned int ifnum,
	const struct usb_device_id *id)
{
	struct usb_vicam *vicam;
	char *camera_name=NULL;

	dbg("vicam_probe");

	/* See if the device offered us matches what we can accept */
	if ((udev->descriptor.idVendor != USB_VICAM_VENDOR_ID) ||
	    (udev->descriptor.idProduct != USB_VICAM_PRODUCT_ID)) {
		return NULL;
	}
	
	camera_name="3Com HomeConnect USB";
	info("ViCAM camera found: %s", camera_name);
	
	vicam = kmalloc (sizeof(struct usb_vicam), GFP_KERNEL);
	if (vicam == NULL) {
		err ("couldn't kmalloc vicam struct");
		return NULL;
	}
	memset(vicam, 0, sizeof(*vicam));

	vicam->readurb = usb_alloc_urb(0, GFP_KERNEL);
	if (!vicam->readurb) {
		kfree(vicam);
		return NULL;
	}

	vicam->udev = udev;
	vicam->camera_name = camera_name;
	vicam->win.brightness = 128;
	vicam->win.contrast = 10;

	/* FIXME */
	if (vicam_init(vicam)) {
		usb_free_urb(vicam->readurb);
		kfree(vicam);
		return NULL;
	}
	memcpy(&vicam->vdev, &vicam_template, sizeof(vicam_template));
	memcpy(vicam->vdev.name, vicam->camera_name, strlen(vicam->camera_name));
	
	if (video_register_device(&vicam->vdev, VFL_TYPE_GRABBER, video_nr) == -1) {
		err("video_register_device");
		usb_free_urb(vicam->readurb);
		kfree(vicam);
		return NULL;
	}

	info("registered new video device: video%d", vicam->vdev.minor);
	
	init_MUTEX (&vicam->sem);
	init_waitqueue_head(&vicam->wait);
	
	return vicam;
}


/* FIXME - vicam_disconnect - important */
static void vicam_disconnect(struct usb_device *udev, void *ptr)
{
	struct usb_vicam *vicam;

	vicam = (struct usb_vicam *) ptr;

	video_unregister_device(&vicam->vdev);
	vicam->udev = NULL;
/*
	vicam->frame[0].grabstate = FRAME_ERROR;
	vicam->frame[1].grabstate = FRAME_ERROR;
*/

	/* Free buffers and shit */

	info("%s disconnected", vicam->camera_name);
	synchronize(vicam);

	if (!vicam->open_count) {
		/* Other random junk */
		usb_free_urb(vicam->readurb);
		kfree(vicam);
		vicam = NULL;
	}
}

/* usb specific object needed to register this driver with the usb subsystem */
static struct usb_driver vicam_driver = {
	.owner =	THIS_MODULE,
	.name =		"vicam",
	.probe =	vicam_probe,
	.disconnect =	vicam_disconnect,
	.id_table =	vicam_table,
};

/******************************************************************************
 *
 *  Module Routines
 *
 ******************************************************************************/

MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_LICENSE("GPL");

/* Module paramaters */
MODULE_PARM(debug, "i");
MODULE_PARM_DESC(debug, "Debug enabled or not");

static int __init usb_vicam_init(void)
{
	int result;

	printk("VICAM: initializing\n");
	/* register this driver with the USB subsystem */
	result = usb_register(&vicam_driver);
	if (result < 0) {
		err("usb_register failed for the "__FILE__" driver. Error number %d",
		    result);
		return -1;
	}

	info(DRIVER_VERSION " " DRIVER_AUTHOR);
	info(DRIVER_DESC);
	return 0;
}

static void __exit usb_vicam_exit(void)
{
	/* deregister this driver with the USB subsystem */
	usb_deregister(&vicam_driver);
}

module_init(usb_vicam_init);
module_exit(usb_vicam_exit);