Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
/* $NetBSD: pseye.c,v 1.29 2022/03/03 06:23:25 riastradh Exp $ */

/*-
 * Copyright (c) 2008 Jared D. McNeill <jmcneill@invisible.ca>
 * All rights reserved.
 *
 * 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.
 * 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.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``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 FOUNDATION OR CONTRIBUTORS
 * 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.
 */

/*
 * Sony PlayStation Eye Driver
 *
 * The only documentation we have for this part is based on a series
 * of forum postings by Jim Paris on ps2dev.org. Many thanks for
 * figuring this one out.
 *
 * URL: http://forums.ps2dev.org/viewtopic.php?t=9238
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: pseye.c,v 1.29 2022/03/03 06:23:25 riastradh Exp $");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/fcntl.h>
#include <sys/conf.h>
#include <sys/poll.h>
#include <sys/bus.h>
#include <sys/mutex.h>
#include <sys/kthread.h>
#include <sys/condvar.h>
#include <sys/module.h>

#include <dev/usb/usb.h>
#include <dev/usb/usbdi.h>
#include <dev/usb/usbdivar.h>
#include <dev/usb/usbdi_util.h>
#include <dev/usb/usbdevs.h>
#include <dev/usb/uvideoreg.h>

#include <dev/video_if.h>

#define PRI_PSEYE	PRI_BIO

/* Bulk-in buffer length -- make room for payload + UVC headers */
#define PSEYE_BULKIN_BUFLEN	((640 * 480 * 2) + 4096)
#define PSEYE_BULKIN_BLKLEN	2048

/* SCCB/sensor interface */
#define PSEYE_SCCB_ADDRESS	0xf1
#define PSEYE_SCCB_SUBADDR	0xf2
#define PSEYE_SCCB_WRITE	0xf3
#define PSEYE_SCCB_READ		0xf4
#define PSEYE_SCCB_OPERATION	0xf5
#define PSEYE_SCCB_STATUS	0xf6

#define PSEYE_SCCB_OP_WRITE_3	0x37
#define PSEYE_SCCB_OP_WRITE_2	0x33
#define PSEYE_SCCB_OP_READ_2	0xf9

struct pseye_softc {
	device_t		sc_dev;

	struct usbd_device *	sc_udev;
	struct usbd_interface *	sc_iface;

	device_t		sc_videodev;
	char			sc_running;

	kcondvar_t		sc_cv;
	kmutex_t		sc_mtx;

	struct usbd_pipe *	sc_bulkin_pipe;
	struct usbd_xfer *	sc_bulkin_xfer;
	int			sc_bulkin;
	uint8_t			*sc_bulkin_buffer;
	int			sc_bulkin_bufferlen;

	char			sc_dying;

	char			sc_businfo[32];
};

static int	pseye_match(device_t, cfdata_t, void *);
static void	pseye_attach(device_t, device_t, void *);
static int	pseye_detach(device_t, int);
static void	pseye_childdet(device_t, device_t);
static int	pseye_activate(device_t, enum devact);

static void	pseye_init(struct pseye_softc *);
static void	pseye_sccb_init(struct pseye_softc *);
static void	pseye_stop(struct pseye_softc *);
static void	pseye_start(struct pseye_softc *);
static void	pseye_led(struct pseye_softc *, bool);
static uint8_t	pseye_getreg(struct pseye_softc *, uint16_t);
static void	pseye_setreg(struct pseye_softc *, uint16_t, uint8_t);
static void	pseye_setregv(struct pseye_softc *, uint16_t, uint8_t);
static void	pseye_sccb_setreg(struct pseye_softc *, uint8_t, uint8_t);
static bool	pseye_sccb_status(struct pseye_softc *);

static int	pseye_init_pipes(struct pseye_softc *);
static int	pseye_close_pipes(struct pseye_softc *);

static usbd_status	pseye_get_frame(struct pseye_softc *, uint32_t *);
static void	pseye_submit_payload(struct pseye_softc *, uint32_t);

/* video(9) API */
static int		pseye_open(void *, int);
static void		pseye_close(void *);
static const char *	pseye_get_devname(void *);
static const char *	pseye_get_businfo(void *);
static int		pseye_enum_format(void *, uint32_t,
					  struct video_format *);
static int		pseye_get_format(void *, struct video_format *);
static int		pseye_set_format(void *, struct video_format *);
static int		pseye_try_format(void *, struct video_format *);
static int		pseye_get_framerate(void *, struct video_fract *);
static int		pseye_set_framerate(void *, struct video_fract *);
static int		pseye_start_transfer(void *);
static int		pseye_stop_transfer(void *);

CFATTACH_DECL2_NEW(pseye, sizeof(struct pseye_softc),
    pseye_match, pseye_attach, pseye_detach, pseye_activate,
    NULL, pseye_childdet);

static const struct video_hw_if pseye_hw_if = {
	.open = pseye_open,
	.close = pseye_close,
	.get_devname = pseye_get_devname,
	.get_businfo = pseye_get_businfo,
	.enum_format = pseye_enum_format,
	.get_format = pseye_get_format,
	.set_format = pseye_set_format,
	.try_format = pseye_try_format,
	.get_framerate = pseye_get_framerate,
	.set_framerate = pseye_set_framerate,
	.start_transfer = pseye_start_transfer,
	.stop_transfer = pseye_stop_transfer,
	.control_iter_init = NULL,
	.control_iter_next = NULL,
	.get_control_desc_group = NULL,
	.get_control_group = NULL,
	.set_control_group = NULL,
};

static int
pseye_match(device_t parent, cfdata_t match, void *opaque)
{
	struct usbif_attach_arg *uiaa = opaque;

	if (uiaa->uiaa_class != UICLASS_VENDOR)
		return UMATCH_NONE;

	if (uiaa->uiaa_vendor == USB_VENDOR_OMNIVISION2) {
		switch (uiaa->uiaa_product) {
		case USB_PRODUCT_OMNIVISION2_PSEYE:
			if (uiaa->uiaa_ifaceno != 0)
				return UMATCH_NONE;
			return UMATCH_VENDOR_PRODUCT;
		}
	}

	return UMATCH_NONE;
}

static void
pseye_attach(device_t parent, device_t self, void *opaque)
{
	struct pseye_softc *sc = device_private(self);
	struct usbif_attach_arg *uiaa = opaque;
	struct usbd_device *dev = uiaa->uiaa_device;
	usb_interface_descriptor_t *id = NULL;
	usb_endpoint_descriptor_t *ed = NULL, *ed_bulkin = NULL;
	char *devinfop;
	int i;

	aprint_naive("\n");
	aprint_normal("\n");

	devinfop = usbd_devinfo_alloc(dev, 0);
	aprint_normal_dev(self, "%s\n", devinfop);
	usbd_devinfo_free(devinfop);

	sc->sc_dev = self;
	sc->sc_udev = dev;
	sc->sc_iface = uiaa->uiaa_iface;
	snprintf(sc->sc_businfo, sizeof(sc->sc_businfo), "usb:%08x",
	    sc->sc_udev->ud_cookie.cookie);
	sc->sc_bulkin_bufferlen = PSEYE_BULKIN_BUFLEN;

	sc->sc_dying = sc->sc_running = 0;
	cv_init(&sc->sc_cv, device_xname(self));
	mutex_init(&sc->sc_mtx, MUTEX_DEFAULT, IPL_NONE);

	id = usbd_get_interface_descriptor(sc->sc_iface);
	if (id == NULL) {
		aprint_error_dev(self, "failed to get interface descriptor\n");
		sc->sc_dying = 1;
		return;
	}

	for (i = 0; i < id->bNumEndpoints; i++) {
		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
		if (ed == NULL) {
			aprint_error_dev(self, "couldn't get ep %d\n", i);
			sc->sc_dying = 1;
			return;
		}

		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
			ed_bulkin = ed;
			break;
		}
	}

	if (ed_bulkin == NULL) {
		aprint_error_dev(self, "no bulk-in endpoint found\n");
		sc->sc_dying = 1;
		return;
	}

	sc->sc_bulkin = ed_bulkin->bEndpointAddress;

	int error = pseye_init_pipes(sc);
	if (error) {
		aprint_error_dev(self, "couldn't open pipes\n");
		return;
	}

	error = usbd_create_xfer(sc->sc_bulkin_pipe, sc->sc_bulkin_bufferlen,
	    0, 0, &sc->sc_bulkin_xfer);
	if (error) {
		aprint_error_dev(self, "couldn't create transfer\n");
		pseye_close_pipes(sc);
		return;
	}

	sc->sc_bulkin_buffer = usbd_get_buffer(sc->sc_bulkin_xfer);

	pseye_init(sc);

	if (!pmf_device_register(self, NULL, NULL))
		aprint_error_dev(self, "couldn't establish power handler\n");

	sc->sc_videodev = video_attach_mi(&pseye_hw_if, self, sc);
	if (sc->sc_videodev == NULL) {
		aprint_error_dev(self, "couldn't attach video layer\n");
		sc->sc_dying = 1;
		return;
	}

	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, self);

}

static int
pseye_detach(device_t self, int flags)
{
	struct pseye_softc *sc = device_private(self);

	sc->sc_dying = 1;

	pmf_device_deregister(self);

	if (sc->sc_videodev != NULL) {
		config_detach(sc->sc_videodev, flags);
		sc->sc_videodev = NULL;
	}

	if (sc->sc_bulkin_pipe != NULL) {
		usbd_abort_pipe(sc->sc_bulkin_pipe);
	}

	if (sc->sc_bulkin_xfer != NULL) {
		usbd_destroy_xfer(sc->sc_bulkin_xfer);
		sc->sc_bulkin_xfer = NULL;
	}

	if (sc->sc_bulkin_pipe != NULL) {
		usbd_close_pipe(sc->sc_bulkin_pipe);
		sc->sc_bulkin_pipe = NULL;
	}

	mutex_enter(&sc->sc_mtx);
	if (sc->sc_running) {
		sc->sc_running = 0;
		cv_wait_sig(&sc->sc_cv, &sc->sc_mtx);
	}
	mutex_exit(&sc->sc_mtx);

	cv_destroy(&sc->sc_cv);
	mutex_destroy(&sc->sc_mtx);

	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);

	return 0;
}

int
pseye_activate(device_t self, enum devact act)
{
	struct pseye_softc *sc = device_private(self);

	switch (act) {
	case DVACT_DEACTIVATE:
		sc->sc_dying = 1;
		return 0;
	default:
		return EOPNOTSUPP;
	}
}

static void
pseye_childdet(device_t self, device_t child)
{
	struct pseye_softc *sc = device_private(self);

	if (sc->sc_videodev) {
		KASSERT(sc->sc_videodev == child);
		sc->sc_videodev = NULL;
	}
}

/*
 * Device access
 */

static void
pseye_init(struct pseye_softc *sc)
{
	pseye_sccb_init(sc);

	pseye_setregv(sc, 0xc2, 0x0c);
	pseye_setregv(sc, 0x88, 0xf8);
	pseye_setregv(sc, 0xc3, 0x69);
	pseye_setregv(sc, 0x89, 0xff);
	pseye_setregv(sc, 0x76, 0x03);
	pseye_setregv(sc, 0x92, 0x01);
	pseye_setregv(sc, 0x93, 0x18);
	pseye_setregv(sc, 0x94, 0x10);
	pseye_setregv(sc, 0x95, 0x10);
	pseye_setregv(sc, 0xe2, 0x00);
	pseye_setregv(sc, 0xe7, 0x3e);

	pseye_setregv(sc, 0x96, 0x00);

	pseye_setreg(sc, 0x97, 0x20);
	pseye_setreg(sc, 0x97, 0x20);
	pseye_setreg(sc, 0x97, 0x20);
	pseye_setreg(sc, 0x97, 0x0a);
	pseye_setreg(sc, 0x97, 0x3f);
	pseye_setreg(sc, 0x97, 0x4a);
	pseye_setreg(sc, 0x97, 0x20);
	pseye_setreg(sc, 0x97, 0x15);
	pseye_setreg(sc, 0x97, 0x0b);

	pseye_setregv(sc, 0x8e, 0x40);
	pseye_setregv(sc, 0x1f, 0x81);
	pseye_setregv(sc, 0x34, 0x05);
	pseye_setregv(sc, 0xe3, 0x04);
	pseye_setregv(sc, 0x88, 0x00);
	pseye_setregv(sc, 0x89, 0x00);
	pseye_setregv(sc, 0x76, 0x00);
	pseye_setregv(sc, 0xe7, 0x2e);
	pseye_setregv(sc, 0x31, 0xf9);
	pseye_setregv(sc, 0x25, 0x42);
	pseye_setregv(sc, 0x21, 0xf0);

	pseye_setreg(sc, 0x1c, 0x00);
	pseye_setreg(sc, 0x1d, 0x40);
	pseye_setreg(sc, 0x1d, 0x02);	/* payload size 0x0200 * 4 == 2048 */
	pseye_setreg(sc, 0x1d, 0x00);
	pseye_setreg(sc, 0x1d, 0x02);	/* frame size 0x025800 * 4 == 614400 */
	pseye_setreg(sc, 0x1d, 0x58);
	pseye_setreg(sc, 0x1d, 0x00);

	pseye_setreg(sc, 0x1c, 0x0a);
	pseye_setreg(sc, 0x1d, 0x08);	/* enable UVC header */
	pseye_setreg(sc, 0x1d, 0x0e);

	pseye_setregv(sc, 0x8d, 0x1c);
	pseye_setregv(sc, 0x8e, 0x80);
	pseye_setregv(sc, 0xe5, 0x04);

	pseye_sccb_setreg(sc, 0x12, 0x80);
	pseye_sccb_setreg(sc, 0x11, 0x01);
	pseye_sccb_setreg(sc, 0x11, 0x01);
	pseye_sccb_setreg(sc, 0x11, 0x01);
	pseye_sccb_setreg(sc, 0x11, 0x01);
	pseye_sccb_setreg(sc, 0x11, 0x01);
	pseye_sccb_setreg(sc, 0x11, 0x01);
	pseye_sccb_setreg(sc, 0x11, 0x01);
	pseye_sccb_setreg(sc, 0x11, 0x01);
	pseye_sccb_setreg(sc, 0x11, 0x01);
	pseye_sccb_setreg(sc, 0x11, 0x01);
	pseye_sccb_setreg(sc, 0x11, 0x01);

	pseye_sccb_setreg(sc, 0x3d, 0x03);
	pseye_sccb_setreg(sc, 0x17, 0x26);
	pseye_sccb_setreg(sc, 0x18, 0xa0);
	pseye_sccb_setreg(sc, 0x19, 0x07);
	pseye_sccb_setreg(sc, 0x1a, 0xf0);
	pseye_sccb_setreg(sc, 0x32, 0x00);
	pseye_sccb_setreg(sc, 0x29, 0xa0);
	pseye_sccb_setreg(sc, 0x2c, 0xf0);
	pseye_sccb_setreg(sc, 0x65, 0x20);
	pseye_sccb_setreg(sc, 0x11, 0x01);
	pseye_sccb_setreg(sc, 0x42, 0x7f);
	pseye_sccb_setreg(sc, 0x63, 0xe0);
	pseye_sccb_setreg(sc, 0x64, 0xff);
	pseye_sccb_setreg(sc, 0x66, 0x00);
	pseye_sccb_setreg(sc, 0x13, 0xf0);
	pseye_sccb_setreg(sc, 0x0d, 0x41);
	pseye_sccb_setreg(sc, 0x0f, 0xc5);
	pseye_sccb_setreg(sc, 0x14, 0x11);

	pseye_sccb_setreg(sc, 0x22, 0x7f);
	pseye_sccb_setreg(sc, 0x23, 0x03);
	pseye_sccb_setreg(sc, 0x24, 0x40);
	pseye_sccb_setreg(sc, 0x25, 0x30);
	pseye_sccb_setreg(sc, 0x26, 0xa1);
	pseye_sccb_setreg(sc, 0x2a, 0x00);
	pseye_sccb_setreg(sc, 0x2b, 0x00);
	pseye_sccb_setreg(sc, 0x6b, 0xaa);
	pseye_sccb_setreg(sc, 0x13, 0xff);

	pseye_sccb_setreg(sc, 0x90, 0x05);
	pseye_sccb_setreg(sc, 0x91, 0x01);
	pseye_sccb_setreg(sc, 0x92, 0x03);
	pseye_sccb_setreg(sc, 0x93, 0x00);
	pseye_sccb_setreg(sc, 0x94, 0x60);
	pseye_sccb_setreg(sc, 0x95, 0x3c);
	pseye_sccb_setreg(sc, 0x96, 0x24);
	pseye_sccb_setreg(sc, 0x97, 0x1e);
	pseye_sccb_setreg(sc, 0x98, 0x62);
	pseye_sccb_setreg(sc, 0x99, 0x80);
	pseye_sccb_setreg(sc, 0x9a, 0x1e);
	pseye_sccb_setreg(sc, 0x9b, 0x08);
	pseye_sccb_setreg(sc, 0x9c, 0x20);
	pseye_sccb_setreg(sc, 0x9e, 0x81);

	pseye_sccb_setreg(sc, 0xa6, 0x04);
	pseye_sccb_setreg(sc, 0x7e, 0x0c);
	pseye_sccb_setreg(sc, 0x7f, 0x16);

	pseye_sccb_setreg(sc, 0x80, 0x2a);
	pseye_sccb_setreg(sc, 0x81, 0x4e);
	pseye_sccb_setreg(sc, 0x82, 0x61);
	pseye_sccb_setreg(sc, 0x83, 0x6f);
	pseye_sccb_setreg(sc, 0x84, 0x7b);
	pseye_sccb_setreg(sc, 0x85, 0x86);
	pseye_sccb_setreg(sc, 0x86, 0x8e);
	pseye_sccb_setreg(sc, 0x87, 0x97);
	pseye_sccb_setreg(sc, 0x88, 0xa4);
	pseye_sccb_setreg(sc, 0x89, 0xaf);
	pseye_sccb_setreg(sc, 0x8a, 0xc5);
	pseye_sccb_setreg(sc, 0x8b, 0xd7);
	pseye_sccb_setreg(sc, 0x8c, 0xe8);
	pseye_sccb_setreg(sc, 0x8d, 0x20);

	pseye_sccb_setreg(sc, 0x0c, 0x90);

	pseye_setregv(sc, 0xc0, 0x50);
	pseye_setregv(sc, 0xc1, 0x3c);
	pseye_setregv(sc, 0xc2, 0x0c);

	pseye_sccb_setreg(sc, 0x2b, 0x00);
	pseye_sccb_setreg(sc, 0x22, 0x7f);
	pseye_sccb_setreg(sc, 0x23, 0x03);
	pseye_sccb_setreg(sc, 0x11, 0x01);
	pseye_sccb_setreg(sc, 0x0c, 0xd0);
	pseye_sccb_setreg(sc, 0x64, 0xff);
	pseye_sccb_setreg(sc, 0x0d, 0x41);

	pseye_sccb_setreg(sc, 0x14, 0x41);
	pseye_sccb_setreg(sc, 0x0e, 0xcd);
	pseye_sccb_setreg(sc, 0xac, 0xbf);
	pseye_sccb_setreg(sc, 0x8e, 0x00);
	pseye_sccb_setreg(sc, 0x0c, 0xd0);

	pseye_stop(sc);
}

static void
pseye_sccb_init(struct pseye_softc *sc)
{
	pseye_setregv(sc, 0xe7, 0x3a);
	pseye_setreg(sc, PSEYE_SCCB_ADDRESS, 0x60);
	pseye_setreg(sc, PSEYE_SCCB_ADDRESS, 0x60);
	pseye_setreg(sc, PSEYE_SCCB_ADDRESS, 0x60);
	pseye_setreg(sc, PSEYE_SCCB_ADDRESS, 0x42);
}

static void
pseye_stop(struct pseye_softc *sc)
{
	pseye_led(sc, false);
	pseye_setreg(sc, 0xe0, 0x09);
}

static void
pseye_start(struct pseye_softc *sc)
{
	pseye_led(sc, true);
	pseye_setreg(sc, 0xe0, 0x00);
}

static void
pseye_led(struct pseye_softc *sc, bool enabled)
{
	uint8_t val;

	val = pseye_getreg(sc, 0x21);
	pseye_setreg(sc, 0x21, val | 0x80);

	val = pseye_getreg(sc, 0x23);
	if (enabled == true)
		val |= 0x80;
	else
		val &= ~0x80;
	pseye_setreg(sc, 0x23, val);
}

static uint8_t
pseye_getreg(struct pseye_softc *sc, uint16_t reg)
{
	usb_device_request_t req;
	usbd_status err;
	uint8_t buf;

	req.bmRequestType = UT_READ_VENDOR_DEVICE;
	req.bRequest = 1;
	USETW(req.wValue, 0x0000);
	USETW(req.wIndex, reg);
	USETW(req.wLength, 1);

	err = usbd_do_request(sc->sc_udev, &req, &buf);
	if (err) {
		aprint_error_dev(sc->sc_dev, "couldn't read reg 0x%04x: %s\n",
		    reg, usbd_errstr(err));
		return 0xff;
	}

	return buf;
}

static void
pseye_setreg(struct pseye_softc *sc, uint16_t reg, uint8_t val)
{
	usb_device_request_t req;
	usbd_status err;

	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
	req.bRequest = 1;
	USETW(req.wValue, 0x0000);
	USETW(req.wIndex, reg);
	USETW(req.wLength, 1);

	err = usbd_do_request(sc->sc_udev, &req, &val);
	if (err)
		aprint_error_dev(sc->sc_dev, "couldn't write reg 0x%04x: %s\n",
		    reg, usbd_errstr(err));
}

static void
pseye_setregv(struct pseye_softc *sc, uint16_t reg, uint8_t val)
{
	pseye_setreg(sc, reg, val);
	if (pseye_getreg(sc, reg) != val)
		aprint_error_dev(sc->sc_dev, "couldn't verify reg 0x%04x\n",
		    reg);
}

static void
pseye_sccb_setreg(struct pseye_softc *sc, uint8_t reg, uint8_t val)
{
	pseye_setreg(sc, PSEYE_SCCB_SUBADDR, reg);
	pseye_setreg(sc, PSEYE_SCCB_WRITE, val);
	pseye_setreg(sc, PSEYE_SCCB_OPERATION, PSEYE_SCCB_OP_WRITE_3);

	if (pseye_sccb_status(sc) == false)
		aprint_error_dev(sc->sc_dev, "couldn't write sccb reg 0x%04x\n",
		    reg);
}

static bool
pseye_sccb_status(struct pseye_softc *sc)
{
	int retry = 5;
	uint8_t reg;

	while (retry-- >= 0) {
		reg = pseye_getreg(sc, PSEYE_SCCB_STATUS);
		if (reg == 0x00)
			return true;
		else if (reg == 0x04)
			return false;
	}

	aprint_error_dev(sc->sc_dev, "timeout reading sccb status\n");
	return false;
}

static usbd_status
pseye_get_frame(struct pseye_softc *sc, uint32_t *plen)
{
	if (sc->sc_dying)
		return USBD_IOERROR;

	return usbd_bulk_transfer(sc->sc_bulkin_xfer, sc->sc_bulkin_pipe,
	    USBD_SHORT_XFER_OK, 1000, sc->sc_bulkin_buffer, plen);
}

static int
pseye_init_pipes(struct pseye_softc *sc)
{
	usbd_status err;

	if (sc->sc_dying)
		return EIO;

	err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkin, 0,
	    &sc->sc_bulkin_pipe);
	if (err) {
		aprint_error_dev(sc->sc_dev, "couldn't open bulk-in pipe: %s\n",
		    usbd_errstr(err));
		return ENOMEM;
	}

	return 0;
}

int
pseye_close_pipes(struct pseye_softc *sc)
{
	if (sc->sc_bulkin_pipe != NULL) {
		usbd_abort_pipe(sc->sc_bulkin_pipe);
		usbd_close_pipe(sc->sc_bulkin_pipe);
		sc->sc_bulkin_pipe = NULL;
	}

	return 0;
}

static void
pseye_submit_payload(struct pseye_softc *sc, uint32_t tlen)
{
	struct video_payload payload;
	uvideo_payload_header_t *uvchdr;
	uint8_t *buf = sc->sc_bulkin_buffer;
	uint32_t len;
	uint32_t brem = (640*480*2);

	while (brem > 0 && tlen > 0) {
		len = uimin(tlen, PSEYE_BULKIN_BLKLEN);
		if (len < UVIDEO_PAYLOAD_HEADER_SIZE) {
			printf("pseye_submit_payload: len=%u\n", len);
			return;
		}

		uvchdr = (uvideo_payload_header_t *)buf;
		if (uvchdr->bHeaderLength != UVIDEO_PAYLOAD_HEADER_SIZE)
			goto next;
		if (uvchdr->bHeaderLength == len &&
		    !(uvchdr->bmHeaderInfo & UV_END_OF_FRAME))
			goto next;
		if (uvchdr->bmHeaderInfo & UV_ERROR)
			return;
		if ((uvchdr->bmHeaderInfo & UV_PRES_TIME) == 0)
			goto next;

		payload.data = buf + uvchdr->bHeaderLength;
		payload.size = uimin(brem, len - uvchdr->bHeaderLength);
		payload.frameno = UGETDW(&buf[2]);
		payload.end_of_frame = uvchdr->bmHeaderInfo & UV_END_OF_FRAME;
		video_submit_payload(sc->sc_videodev, &payload);

next:
		tlen -= len;
		buf += len;
		brem -= payload.size;
	}
}

static void
pseye_transfer_thread(void *opaque)
{
	struct pseye_softc *sc = opaque;
	uint32_t len;
	int error;

	while (sc->sc_running) {
		len = sc->sc_bulkin_bufferlen;
		error = pseye_get_frame(sc, &len);
		if (error == USBD_NORMAL_COMPLETION)
			pseye_submit_payload(sc, len);
	}

	mutex_enter(&sc->sc_mtx);
	cv_broadcast(&sc->sc_cv);
	mutex_exit(&sc->sc_mtx);

	kthread_exit(0);
}

/* video(9) API implementations */
static int
pseye_open(void *opaque, int flags)
{
	struct pseye_softc *sc = opaque;

	if (sc->sc_dying)
		return EIO;

	pseye_start(sc);

	return 0;
}

static void
pseye_close(void *opaque)
{
	struct pseye_softc *sc = opaque;

	pseye_stop(sc);
}

static const char *
pseye_get_devname(void *opaque)
{
	return "PlayStation Eye";
}

static const char *
pseye_get_businfo(void *opaque)
{
	struct pseye_softc *sc = opaque;

	return sc->sc_businfo;
}

static int
pseye_enum_format(void *opaque, uint32_t index, struct video_format *format)
{
	if (index != 0)
		return EINVAL;
	return pseye_get_format(opaque, format);
}

static int
pseye_get_format(void *opaque, struct video_format *format)
{
	format->pixel_format = VIDEO_FORMAT_YUY2; /* XXX actually YUYV */
	format->width = 640;
	format->height = 480;
	format->aspect_x = 4;
	format->aspect_y = 3;
	format->sample_size = format->width * format->height * 2;
	format->stride = format->width * 2;
	format->color.primaries = VIDEO_COLOR_PRIMARIES_UNSPECIFIED;
	format->color.gamma_function = VIDEO_GAMMA_FUNCTION_UNSPECIFIED;
	format->color.matrix_coeff = VIDEO_MATRIX_COEFF_UNSPECIFIED;
	format->interlace_flags = VIDEO_INTERLACE_ON;
	format->priv = 0;

	return 0;
}

static int
pseye_set_format(void *opaque, struct video_format *format)
{
#if notyet
	if (format->pixel_format != VIDEO_FORMAT_YUYV)
		return EINVAL;
	if (format->width != 640 || format->height != 480)
		return EINVAL;
#endif
	/* XXX */
	return pseye_get_format(opaque, format);
}

static int
pseye_try_format(void *opaque, struct video_format *format)
{
	return pseye_get_format(opaque, format);
}

static int
pseye_get_framerate(void *opaque, struct video_fract *fract)
{
	/* Driver only supports 60fps */
	fract->numerator = 1;
	fract->denominator = 60;

	return 0;
}

static int
pseye_set_framerate(void *opaque, struct video_fract *fract)
{
	/* Driver only supports one framerate. Return actual rate. */
	return pseye_get_framerate(opaque, fract);
}

static int
pseye_start_transfer(void *opaque)
{
	struct pseye_softc *sc = opaque;
	int err = 0;

	mutex_enter(&sc->sc_mtx);
	if (sc->sc_running == 0) {
		sc->sc_running = 1;
		err = kthread_create(PRI_PSEYE, 0, NULL, pseye_transfer_thread,
		    opaque, NULL, "%s", device_xname(sc->sc_dev));
	} else
		aprint_error_dev(sc->sc_dev, "transfer already in progress\n");
	mutex_exit(&sc->sc_mtx);

	return err;
}

static int
pseye_stop_transfer(void *opaque)
{
	struct pseye_softc *sc = opaque;

	mutex_enter(&sc->sc_mtx);
	if (sc->sc_running) {
		sc->sc_running = 0;
		cv_wait_sig(&sc->sc_cv, &sc->sc_mtx);
	}
	mutex_exit(&sc->sc_mtx);

	return 0;
}

MODULE(MODULE_CLASS_DRIVER, pseye, NULL);

#ifdef _MODULE
#include "ioconf.c"
#endif

static int
pseye_modcmd(modcmd_t cmd, void *opaque)
{
	switch (cmd) {
	case MODULE_CMD_INIT:
#ifdef _MODULE
		return config_init_component(cfdriver_ioconf_pseye,
		    cfattach_ioconf_pseye, cfdata_ioconf_pseye);
#else
		return 0;
#endif
	case MODULE_CMD_FINI:
#ifdef _MODULE
		return config_fini_component(cfdriver_ioconf_pseye,
		    cfattach_ioconf_pseye, cfdata_ioconf_pseye);
#else
		return 0;
#endif
	default:
		return ENOTTY;
	}
}