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