pseye.c revision 1.6 1 /* $NetBSD: pseye.c,v 1.6 2008/09/13 18:51:10 mjf Exp $ */
2
3 /*-
4 * Copyright (c) 2008 Jared D. McNeill <jmcneill (at) invisible.ca>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: pseye.c,v 1.6 2008/09/13 18:51:10 mjf Exp $");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/device.h>
35 #include <sys/malloc.h>
36 #include <sys/fcntl.h>
37 #include <sys/conf.h>
38 #include <sys/poll.h>
39 #include <sys/bus.h>
40 #include <sys/mutex.h>
41 #include <sys/kthread.h>
42 #include <sys/condvar.h>
43
44 #include <uvm/uvm_extern.h>
45
46 #include <dev/usb/usb.h>
47 #include <dev/usb/usbdi.h>
48 #include <dev/usb/usbdi_util.h>
49 #include <dev/usb/usbdevs.h>
50
51 #include <dev/video_if.h>
52
53 #define PRI_PSEYE PRI_BIO
54
55 /* Bulk-in buffer length */
56 #define PSEYE_BULKIN_BUFLEN (640 * 480 * 2)
57
58 /* SCCB/sensor interface */
59 #define PSEYE_SCCB_ADDRESS 0xf1
60 #define PSEYE_SCCB_SUBADDR 0xf2
61 #define PSEYE_SCCB_WRITE 0xf3
62 #define PSEYE_SCCB_READ 0xf4
63 #define PSEYE_SCCB_OPERATION 0xf5
64 #define PSEYE_SCCB_STATUS 0xf6
65
66 #define PSEYE_SCCB_OP_WRITE_3 0x37
67 #define PSEYE_SCCB_OP_WRITE_2 0x33
68 #define PSEYE_SCCB_OP_READ_2 0xf9
69
70 struct pseye_softc {
71 USBBASEDEVICE sc_dev;
72
73 usbd_device_handle sc_udev;
74 usbd_interface_handle sc_iface;
75
76 device_t sc_videodev;
77 char sc_running;
78
79 kcondvar_t sc_cv;
80 kmutex_t sc_mtx;
81
82 usbd_pipe_handle sc_bulkin_pipe;
83 usbd_xfer_handle sc_bulkin_xfer;
84 int sc_bulkin;
85 uint8_t *sc_bulkin_buffer;
86 int sc_bulkin_bufferlen;
87
88 char sc_dying;
89 };
90
91 static int pseye_match(device_t, cfdata_t, void *);
92 static void pseye_attach(device_t, device_t, void *);
93 static int pseye_detach(device_t, int);
94 static void pseye_childdet(device_t, device_t);
95 static int pseye_activate(device_t, enum devact);
96
97 static void pseye_init(struct pseye_softc *);
98 static void pseye_sccb_init(struct pseye_softc *);
99 static void pseye_stop(struct pseye_softc *);
100 static void pseye_start(struct pseye_softc *);
101 static void pseye_led(struct pseye_softc *, bool);
102 static uint8_t pseye_getreg(struct pseye_softc *, uint16_t);
103 static void pseye_setreg(struct pseye_softc *, uint16_t, uint8_t);
104 static void pseye_setregv(struct pseye_softc *, uint16_t, uint8_t);
105 static void pseye_sccb_setreg(struct pseye_softc *, uint8_t, uint8_t);
106 static bool pseye_sccb_status(struct pseye_softc *);
107
108 static int pseye_init_pipes(struct pseye_softc *);
109 static int pseye_close_pipes(struct pseye_softc *);
110
111 static usbd_status pseye_get_frame(struct pseye_softc *);
112
113 /* video(9) API */
114 static int pseye_open(void *, int);
115 static void pseye_close(void *);
116 static const char * pseye_get_devname(void *);
117 static int pseye_enum_format(void *, uint32_t,
118 struct video_format *);
119 static int pseye_get_format(void *, struct video_format *);
120 static int pseye_set_format(void *, struct video_format *);
121 static int pseye_try_format(void *, struct video_format *);
122 static int pseye_start_transfer(void *);
123 static int pseye_stop_transfer(void *);
124
125 CFATTACH_DECL2_NEW(pseye, sizeof(struct pseye_softc),
126 pseye_match, pseye_attach, pseye_detach, pseye_activate,
127 NULL, pseye_childdet);
128
129 static const struct video_hw_if pseye_hw_if = {
130 .open = pseye_open,
131 .close = pseye_close,
132 .get_devname = pseye_get_devname,
133 .enum_format = pseye_enum_format,
134 .get_format = pseye_get_format,
135 .set_format = pseye_set_format,
136 .try_format = pseye_try_format,
137 .start_transfer = pseye_start_transfer,
138 .stop_transfer = pseye_stop_transfer,
139 .control_iter_init = NULL,
140 .control_iter_next = NULL,
141 .get_control_desc_group = NULL,
142 .get_control_group = NULL,
143 .set_control_group = NULL,
144 };
145
146 USB_MATCH(pseye)
147 {
148 USB_IFMATCH_START(pseye, uaa);
149
150 if (uaa->class != UICLASS_VENDOR)
151 return UMATCH_NONE;
152
153 if (uaa->vendor == USB_VENDOR_OMNIVISION2) {
154 switch (uaa->product) {
155 case USB_PRODUCT_OMNIVISION2_PSEYE:
156 if (uaa->ifaceno != 0)
157 return UMATCH_NONE;
158 return UMATCH_VENDOR_PRODUCT;
159 }
160 }
161
162 return UMATCH_NONE;
163 }
164
165 USB_ATTACH(pseye)
166 {
167 USB_IFATTACH_START(pseye, sc, uaa);
168 usbd_device_handle dev = uaa->device;
169 usb_interface_descriptor_t *id = NULL;
170 usb_endpoint_descriptor_t *ed = NULL, *ed_bulkin = NULL;
171 char *devinfo;
172 int i;
173
174 USB_ATTACH_SETUP;
175 devinfo = usbd_devinfo_alloc(dev, 0);
176 aprint_normal_dev(self, "%s\n", devinfo);
177 usbd_devinfo_free(devinfo);
178
179 sc->sc_dev = self;
180 sc->sc_udev = dev;
181 sc->sc_iface = uaa->iface;
182 sc->sc_bulkin_bufferlen = PSEYE_BULKIN_BUFLEN;
183
184 sc->sc_dying = sc->sc_running = 0;
185 cv_init(&sc->sc_cv, device_xname(sc->sc_dev));
186 mutex_init(&sc->sc_mtx, MUTEX_DEFAULT, IPL_NONE);
187
188 id = usbd_get_interface_descriptor(sc->sc_iface);
189 if (id == NULL) {
190 aprint_error_dev(self, "failed to get interface descriptor\n");
191 sc->sc_dying = 1;
192 USB_ATTACH_ERROR_RETURN;
193 }
194
195 for (i = 0; i < id->bNumEndpoints; i++) {
196 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
197 if (ed == NULL) {
198 aprint_error_dev(sc->sc_dev, "couldn't get ep %d\n", i);
199 sc->sc_dying = 1;
200 USB_ATTACH_ERROR_RETURN;
201 }
202
203 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
204 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
205 ed_bulkin = ed;
206 break;
207 }
208 }
209
210 if (ed_bulkin == NULL) {
211 aprint_error_dev(sc->sc_dev, "no bulk-in endpoint found\n");
212 sc->sc_dying = 1;
213 USB_ATTACH_ERROR_RETURN;
214 }
215
216 sc->sc_bulkin = ed_bulkin->bEndpointAddress;
217
218 sc->sc_bulkin_xfer = usbd_alloc_xfer(sc->sc_udev);
219 if (sc->sc_bulkin_xfer == NULL) {
220 sc->sc_dying = 1;
221 USB_ATTACH_ERROR_RETURN;
222 }
223 sc->sc_bulkin_buffer = usbd_alloc_buffer(sc->sc_bulkin_xfer,
224 sc->sc_bulkin_bufferlen);
225 if (sc->sc_bulkin_buffer == NULL) {
226 usbd_free_xfer(sc->sc_bulkin_xfer);
227 sc->sc_bulkin_xfer = NULL;
228 sc->sc_dying = 1;
229 USB_ATTACH_ERROR_RETURN;
230 }
231
232 pseye_init(sc);
233
234 sc->sc_videodev = video_attach_mi(&pseye_hw_if, sc->sc_dev);
235 if (sc->sc_videodev == NULL) {
236 aprint_error_dev(sc->sc_dev, "couldn't attach video layer\n");
237 sc->sc_dying = 1;
238 USB_ATTACH_ERROR_RETURN;
239 }
240
241 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
242 USBDEV(sc->sc_dev));
243
244 USB_ATTACH_SUCCESS_RETURN;
245 }
246
247 USB_DETACH(pseye)
248 {
249 USB_DETACH_START(pseye, sc);
250
251 sc->sc_dying = 1;
252
253 if (sc->sc_videodev != NULL) {
254 config_detach(sc->sc_videodev, flags);
255 sc->sc_videodev = NULL;
256 }
257
258 if (sc->sc_bulkin_xfer != NULL) {
259 usbd_free_xfer(sc->sc_bulkin_xfer);
260 sc->sc_bulkin_xfer = NULL;
261 }
262
263 if (sc->sc_bulkin_pipe != NULL) {
264 usbd_abort_pipe(sc->sc_bulkin_pipe);
265 sc->sc_bulkin_pipe = NULL;
266 }
267
268 mutex_enter(&sc->sc_mtx);
269 if (sc->sc_running) {
270 sc->sc_running = 0;
271 cv_wait_sig(&sc->sc_cv, &sc->sc_mtx);
272 }
273 mutex_exit(&sc->sc_mtx);
274
275 cv_destroy(&sc->sc_cv);
276 mutex_destroy(&sc->sc_mtx);
277
278 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
279 USBDEV(sc->sc_dev));
280
281 return 0;
282 }
283
284 int
285 pseye_activate(device_ptr_t self, enum devact act)
286 {
287 struct pseye_softc *sc = device_private(self);
288 int rv;
289
290 rv = 0;
291
292 switch (act) {
293 case DVACT_ACTIVATE:
294 break;
295 case DVACT_DEACTIVATE:
296 sc->sc_dying = 1;
297 break;
298 }
299
300 return rv;
301 }
302
303 static void
304 pseye_childdet(device_t self, device_t child)
305 {
306 struct pseye_softc *sc = device_private(self);
307
308 if (sc->sc_videodev) {
309 KASSERT(sc->sc_videodev == child);
310 sc->sc_videodev = NULL;
311 }
312 }
313
314 /*
315 * Device access
316 */
317
318 static void
319 pseye_init(struct pseye_softc *sc)
320 {
321 pseye_sccb_init(sc);
322
323 pseye_setregv(sc, 0xc2, 0x0c);
324 pseye_setregv(sc, 0x88, 0xf8);
325 pseye_setregv(sc, 0xc3, 0x69);
326 pseye_setregv(sc, 0x89, 0xff);
327 pseye_setregv(sc, 0x76, 0x03);
328 pseye_setregv(sc, 0x92, 0x01);
329 pseye_setregv(sc, 0x93, 0x18);
330 pseye_setregv(sc, 0x94, 0x10);
331 pseye_setregv(sc, 0x95, 0x10);
332 pseye_setregv(sc, 0xe2, 0x00);
333 pseye_setregv(sc, 0xe7, 0x3e);
334
335 pseye_setreg(sc, 0x1c, 0x0a);
336 pseye_setreg(sc, 0x1d, 0x22);
337 pseye_setreg(sc, 0x1d, 0x06);
338 pseye_setregv(sc, 0x96, 0x00);
339
340 pseye_setreg(sc, 0x97, 0x20);
341 pseye_setreg(sc, 0x97, 0x20);
342 pseye_setreg(sc, 0x97, 0x20);
343 pseye_setreg(sc, 0x97, 0x0a);
344 pseye_setreg(sc, 0x97, 0x3f);
345 pseye_setreg(sc, 0x97, 0x4a);
346 pseye_setreg(sc, 0x97, 0x20);
347 pseye_setreg(sc, 0x97, 0x15);
348 pseye_setreg(sc, 0x97, 0x0b);
349
350 pseye_setregv(sc, 0x8e, 0x40);
351 pseye_setregv(sc, 0x1f, 0x81);
352 pseye_setregv(sc, 0x34, 0x05);
353 pseye_setregv(sc, 0xe3, 0x04);
354 pseye_setregv(sc, 0x88, 0x00);
355 pseye_setregv(sc, 0x89, 0x00);
356 pseye_setregv(sc, 0x76, 0x00);
357 pseye_setregv(sc, 0xe7, 0x2e);
358 pseye_setregv(sc, 0x31, 0xf9);
359 pseye_setregv(sc, 0x25, 0x42);
360 pseye_setregv(sc, 0x21, 0xf0);
361
362 pseye_setreg(sc, 0x1c, 0x00);
363 pseye_setreg(sc, 0x1d, 0x40);
364 pseye_setreg(sc, 0x1d, 0x02);
365 pseye_setreg(sc, 0x1d, 0x00);
366 pseye_setreg(sc, 0x1d, 0x02);
367 pseye_setreg(sc, 0x1d, 0x57);
368 pseye_setreg(sc, 0x1d, 0xff);
369
370 pseye_setregv(sc, 0x8d, 0x1c);
371 pseye_setregv(sc, 0x8e, 0x80);
372 pseye_setregv(sc, 0xe5, 0x04);
373
374 pseye_sccb_setreg(sc, 0x12, 0x80);
375 pseye_sccb_setreg(sc, 0x11, 0x01);
376 pseye_sccb_setreg(sc, 0x11, 0x01);
377 pseye_sccb_setreg(sc, 0x11, 0x01);
378 pseye_sccb_setreg(sc, 0x11, 0x01);
379 pseye_sccb_setreg(sc, 0x11, 0x01);
380 pseye_sccb_setreg(sc, 0x11, 0x01);
381 pseye_sccb_setreg(sc, 0x11, 0x01);
382 pseye_sccb_setreg(sc, 0x11, 0x01);
383 pseye_sccb_setreg(sc, 0x11, 0x01);
384 pseye_sccb_setreg(sc, 0x11, 0x01);
385 pseye_sccb_setreg(sc, 0x11, 0x01);
386
387 pseye_sccb_setreg(sc, 0x3d, 0x03);
388 pseye_sccb_setreg(sc, 0x17, 0x26);
389 pseye_sccb_setreg(sc, 0x18, 0xa0);
390 pseye_sccb_setreg(sc, 0x19, 0x07);
391 pseye_sccb_setreg(sc, 0x1a, 0xf0);
392 pseye_sccb_setreg(sc, 0x32, 0x00);
393 pseye_sccb_setreg(sc, 0x29, 0xa0);
394 pseye_sccb_setreg(sc, 0x2c, 0xf0);
395 pseye_sccb_setreg(sc, 0x65, 0x20);
396 pseye_sccb_setreg(sc, 0x11, 0x01);
397 pseye_sccb_setreg(sc, 0x42, 0x7f);
398 pseye_sccb_setreg(sc, 0x63, 0xe0);
399 pseye_sccb_setreg(sc, 0x64, 0xff);
400 pseye_sccb_setreg(sc, 0x66, 0x00);
401 pseye_sccb_setreg(sc, 0x13, 0xf0);
402 pseye_sccb_setreg(sc, 0x0d, 0x41);
403 pseye_sccb_setreg(sc, 0x0f, 0xc5);
404 pseye_sccb_setreg(sc, 0x14, 0x11);
405
406 pseye_sccb_setreg(sc, 0x22, 0x7f);
407 pseye_sccb_setreg(sc, 0x23, 0x03);
408 pseye_sccb_setreg(sc, 0x24, 0x40);
409 pseye_sccb_setreg(sc, 0x25, 0x30);
410 pseye_sccb_setreg(sc, 0x26, 0xa1);
411 pseye_sccb_setreg(sc, 0x2a, 0x00);
412 pseye_sccb_setreg(sc, 0x2b, 0x00);
413 pseye_sccb_setreg(sc, 0x6b, 0xaa);
414 pseye_sccb_setreg(sc, 0x13, 0xff);
415
416 pseye_sccb_setreg(sc, 0x90, 0x05);
417 pseye_sccb_setreg(sc, 0x91, 0x01);
418 pseye_sccb_setreg(sc, 0x92, 0x03);
419 pseye_sccb_setreg(sc, 0x93, 0x00);
420 pseye_sccb_setreg(sc, 0x94, 0x60);
421 pseye_sccb_setreg(sc, 0x95, 0x3c);
422 pseye_sccb_setreg(sc, 0x96, 0x24);
423 pseye_sccb_setreg(sc, 0x97, 0x1e);
424 pseye_sccb_setreg(sc, 0x98, 0x62);
425 pseye_sccb_setreg(sc, 0x99, 0x80);
426 pseye_sccb_setreg(sc, 0x9a, 0x1e);
427 pseye_sccb_setreg(sc, 0x9b, 0x08);
428 pseye_sccb_setreg(sc, 0x9c, 0x20);
429 pseye_sccb_setreg(sc, 0x9e, 0x81);
430
431 pseye_sccb_setreg(sc, 0xa6, 0x04);
432 pseye_sccb_setreg(sc, 0x7e, 0x0c);
433 pseye_sccb_setreg(sc, 0x7f, 0x16);
434
435 pseye_sccb_setreg(sc, 0x80, 0x2a);
436 pseye_sccb_setreg(sc, 0x81, 0x4e);
437 pseye_sccb_setreg(sc, 0x82, 0x61);
438 pseye_sccb_setreg(sc, 0x83, 0x6f);
439 pseye_sccb_setreg(sc, 0x84, 0x7b);
440 pseye_sccb_setreg(sc, 0x85, 0x86);
441 pseye_sccb_setreg(sc, 0x86, 0x8e);
442 pseye_sccb_setreg(sc, 0x87, 0x97);
443 pseye_sccb_setreg(sc, 0x88, 0xa4);
444 pseye_sccb_setreg(sc, 0x89, 0xaf);
445 pseye_sccb_setreg(sc, 0x8a, 0xc5);
446 pseye_sccb_setreg(sc, 0x8b, 0xd7);
447 pseye_sccb_setreg(sc, 0x8c, 0xe8);
448 pseye_sccb_setreg(sc, 0x8d, 0x20);
449
450 pseye_sccb_setreg(sc, 0x0c, 0x90);
451
452 pseye_setregv(sc, 0xc0, 0x50);
453 pseye_setregv(sc, 0xc1, 0x3c);
454 pseye_setregv(sc, 0xc2, 0x0c);
455
456 pseye_sccb_setreg(sc, 0x2b, 0x00);
457 pseye_sccb_setreg(sc, 0x22, 0x7f);
458 pseye_sccb_setreg(sc, 0x23, 0x03);
459 pseye_sccb_setreg(sc, 0x11, 0x01);
460 pseye_sccb_setreg(sc, 0x0c, 0xd0);
461 pseye_sccb_setreg(sc, 0x64, 0xff);
462 pseye_sccb_setreg(sc, 0x0d, 0x41);
463
464 pseye_sccb_setreg(sc, 0x14, 0x41);
465 pseye_sccb_setreg(sc, 0x0e, 0xcd);
466 pseye_sccb_setreg(sc, 0xac, 0xbf);
467 pseye_sccb_setreg(sc, 0x8e, 0x00);
468 pseye_sccb_setreg(sc, 0x0c, 0xd0);
469
470 pseye_stop(sc);
471 }
472
473 static void
474 pseye_sccb_init(struct pseye_softc *sc)
475 {
476 pseye_setregv(sc, 0xe7, 0x3a);
477 pseye_setreg(sc, PSEYE_SCCB_ADDRESS, 0x60);
478 pseye_setreg(sc, PSEYE_SCCB_ADDRESS, 0x60);
479 pseye_setreg(sc, PSEYE_SCCB_ADDRESS, 0x60);
480 pseye_setreg(sc, PSEYE_SCCB_ADDRESS, 0x42);
481 }
482
483 static void
484 pseye_stop(struct pseye_softc *sc)
485 {
486 pseye_led(sc, false);
487 pseye_setreg(sc, 0xe0, 0x09);
488 }
489
490 static void
491 pseye_start(struct pseye_softc *sc)
492 {
493 pseye_led(sc, true);
494 pseye_setreg(sc, 0xe0, 0x00);
495 }
496
497 static void
498 pseye_led(struct pseye_softc *sc, bool enabled)
499 {
500 uint8_t val;
501
502 val = pseye_getreg(sc, 0x21);
503 pseye_setreg(sc, 0x21, val | 0x80);
504
505 val = pseye_getreg(sc, 0x23);
506 if (enabled == true)
507 val |= 0x80;
508 else
509 val &= ~0x80;
510 pseye_setreg(sc, 0x23, val);
511 }
512
513 static uint8_t
514 pseye_getreg(struct pseye_softc *sc, uint16_t reg)
515 {
516 usb_device_request_t req;
517 usbd_status err;
518 uint8_t buf;
519
520 req.bmRequestType = UT_READ_VENDOR_DEVICE;
521 req.bRequest = 1;
522 USETW(req.wValue, 0x0000);
523 USETW(req.wIndex, reg);
524 USETW(req.wLength, 1);
525
526 err = usbd_do_request(sc->sc_udev, &req, &buf);
527 if (err) {
528 aprint_error_dev(sc->sc_dev, "couldn't read reg 0x%04x: %s\n",
529 reg, usbd_errstr(err));
530 return 0xff;
531 }
532
533 return buf;
534 }
535
536 static void
537 pseye_setreg(struct pseye_softc *sc, uint16_t reg, uint8_t val)
538 {
539 usb_device_request_t req;
540 usbd_status err;
541
542 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
543 req.bRequest = 1;
544 USETW(req.wValue, 0x0000);
545 USETW(req.wIndex, reg);
546 USETW(req.wLength, 1);
547
548 err = usbd_do_request(sc->sc_udev, &req, &val);
549 if (err)
550 aprint_error_dev(sc->sc_dev, "couldn't write reg 0x%04x: %s\n",
551 reg, usbd_errstr(err));
552 }
553
554 static void
555 pseye_setregv(struct pseye_softc *sc, uint16_t reg, uint8_t val)
556 {
557 pseye_setreg(sc, reg, val);
558 if (pseye_getreg(sc, reg) != val)
559 aprint_error_dev(sc->sc_dev, "couldn't verify reg 0x%04x\n",
560 reg);
561 }
562
563 static void
564 pseye_sccb_setreg(struct pseye_softc *sc, uint8_t reg, uint8_t val)
565 {
566 pseye_setreg(sc, PSEYE_SCCB_SUBADDR, reg);
567 pseye_setreg(sc, PSEYE_SCCB_WRITE, val);
568 pseye_setreg(sc, PSEYE_SCCB_OPERATION, PSEYE_SCCB_OP_WRITE_3);
569
570 if (pseye_sccb_status(sc) == false)
571 aprint_error_dev(sc->sc_dev, "couldn't write sccb reg 0x%04x\n",
572 reg);
573 }
574
575 static bool
576 pseye_sccb_status(struct pseye_softc *sc)
577 {
578 int retry = 5;
579 uint8_t reg;
580
581 while (retry-- >= 0) {
582 reg = pseye_getreg(sc, PSEYE_SCCB_STATUS);
583 if (reg == 0x00)
584 return true;
585 else if (reg == 0x04)
586 return false;
587 }
588
589 aprint_error_dev(sc->sc_dev, "timeout reading sccb status\n");
590 return false;
591 }
592
593 static usbd_status
594 pseye_get_frame(struct pseye_softc *sc)
595 {
596 uint32_t len = sc->sc_bulkin_bufferlen;
597
598 if (sc->sc_dying)
599 return USBD_IOERROR;
600
601 return usbd_bulk_transfer(sc->sc_bulkin_xfer, sc->sc_bulkin_pipe,
602 USBD_SHORT_XFER_OK|USBD_NO_COPY, 50,
603 sc->sc_bulkin_buffer, &len, "pseyerb");
604 }
605
606 static int
607 pseye_init_pipes(struct pseye_softc *sc)
608 {
609 usbd_status err;
610
611 if (sc->sc_dying)
612 return EIO;
613
614 err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkin, 0,
615 &sc->sc_bulkin_pipe);
616 if (err) {
617 aprint_error_dev(sc->sc_dev, "couldn't open bulk-in pipe: %s\n",
618 usbd_errstr(err));
619 return ENOMEM;
620 }
621
622 pseye_start(sc);
623
624 return 0;
625 }
626
627 int
628 pseye_close_pipes(struct pseye_softc *sc)
629 {
630 if (sc->sc_bulkin_pipe != NULL) {
631 usbd_abort_pipe(sc->sc_bulkin_pipe);
632 usbd_close_pipe(sc->sc_bulkin_pipe);
633 sc->sc_bulkin_pipe = NULL;
634 }
635
636 pseye_stop(sc);
637
638 return 0;
639 }
640
641 static void
642 pseye_transfer_thread(void *opaque)
643 {
644 struct pseye_softc *sc = opaque;
645 int error;
646 struct video_payload payload;
647
648 payload.frameno = 0;
649
650 while (sc->sc_running) {
651 error = pseye_get_frame(sc);
652 if (error == USBD_NORMAL_COMPLETION) {
653 payload.data = sc->sc_bulkin_buffer;
654 payload.size = sc->sc_bulkin_bufferlen;
655 payload.frameno = (payload.frameno + 1) & 1;
656 payload.end_of_frame = 1;
657 video_submit_payload(sc->sc_videodev, &payload);
658 }
659 }
660
661 mutex_enter(&sc->sc_mtx);
662 cv_broadcast(&sc->sc_cv);
663 mutex_exit(&sc->sc_mtx);
664
665 kthread_exit(0);
666 }
667
668 /* video(9) API implementations */
669 static int
670 pseye_open(void *opaque, int flags)
671 {
672 struct pseye_softc *sc = opaque;
673
674 if (sc->sc_dying)
675 return EIO;
676
677 return pseye_init_pipes(sc);
678 }
679
680 static void
681 pseye_close(void *opaque)
682 {
683 struct pseye_softc *sc = opaque;
684
685 pseye_close_pipes(sc);
686 }
687
688 static const char *
689 pseye_get_devname(void *opaque)
690 {
691 return "PLAYSTATION(R) Eye";
692 }
693
694 static int
695 pseye_enum_format(void *opaque, uint32_t index, struct video_format *format)
696 {
697 if (index != 0)
698 return EINVAL;
699 return pseye_get_format(opaque, format);
700 }
701
702 static int
703 pseye_get_format(void *opaque, struct video_format *format)
704 {
705 format->pixel_format = VIDEO_FORMAT_YUY2; /* XXX actually YUYV */
706 format->width = 640;
707 format->height = 480;
708 format->aspect_x = 4;
709 format->aspect_y = 3;
710 format->sample_size = format->width * format->height * 2;
711 format->stride = format->width * 2;
712 format->color.primaries = VIDEO_COLOR_PRIMARIES_UNSPECIFIED;
713 format->color.gamma_function = VIDEO_GAMMA_FUNCTION_UNSPECIFIED;
714 format->color.matrix_coeff = VIDEO_MATRIX_COEFF_UNSPECIFIED;
715 format->interlace_flags = VIDEO_INTERLACE_ON;
716 format->priv = 0;
717
718 return 0;
719 }
720
721 static int
722 pseye_set_format(void *opaque, struct video_format *format)
723 {
724 #if notyet
725 if (format->pixel_format != VIDEO_FORMAT_YUYV)
726 return EINVAL;
727 if (format->width != 640 || format->height != 480)
728 return EINVAL;
729 #endif
730 /* XXX */
731 return pseye_get_format(opaque, format);
732 }
733
734 static int
735 pseye_try_format(void *opaque, struct video_format *format)
736 {
737 return pseye_get_format(opaque, format);
738 }
739
740 static int
741 pseye_start_transfer(void *opaque)
742 {
743 struct pseye_softc *sc = opaque;
744 int err = 0;
745
746 mutex_enter(&sc->sc_mtx);
747 if (sc->sc_running == 0) {
748 sc->sc_running = 1;
749 err = kthread_create(PRI_PSEYE, 0, NULL, pseye_transfer_thread,
750 opaque, NULL, device_xname(sc->sc_dev));
751 } else
752 aprint_error_dev(sc->sc_dev, "transfer already in progress\n");
753 mutex_exit(&sc->sc_mtx);
754
755 return err;
756 }
757
758 static int
759 pseye_stop_transfer(void *opaque)
760 {
761 struct pseye_softc *sc = opaque;
762
763 mutex_enter(&sc->sc_mtx);
764 if (sc->sc_running) {
765 sc->sc_running = 0;
766 cv_wait_sig(&sc->sc_cv, &sc->sc_mtx);
767 }
768 mutex_exit(&sc->sc_mtx);
769
770 return 0;
771 }
772