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