auvitek_video.c revision 1.6.32.3 1 /* $NetBSD: auvitek_video.c,v 1.6.32.3 2015/03/19 17:26:42 skrll Exp $ */
2
3 /*-
4 * Copyright (c) 2010 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 /*
30 * Auvitek AU0828 USB controller
31 */
32
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: auvitek_video.c,v 1.6.32.3 2015/03/19 17:26:42 skrll Exp $");
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/device.h>
39 #include <sys/conf.h>
40 #include <sys/kmem.h>
41 #include <sys/bus.h>
42
43 #include <dev/usb/usb.h>
44 #include <dev/usb/usbdi.h>
45 #include <dev/usb/usbdivar.h>
46 #include <dev/usb/usbdi_util.h>
47 #include <dev/usb/usbdevs.h>
48
49 #include <dev/video_if.h>
50
51 #include <dev/usb/auvitekreg.h>
52 #include <dev/usb/auvitekvar.h>
53
54 #define AUVITEK_FORMAT_DEFAULT 0
55 #define AUVITEK_STANDARD_NTSC_M 0
56 #define AUVITEK_TUNER_DEFAULT 0
57 #define AUVITEK_AUDIO_TELEVISION 0
58 #define AUVITEK_AUDIO_LINEIN 1
59 #define AUVITEK_INPUT_COMPOSITE 0
60 #define AUVITEK_INPUT_SVIDEO 1
61 #define AUVITEK_INPUT_TELEVISION 2
62 #define AUVITEK_INPUT_CABLE 3
63
64 static int auvitek_open(void *, int);
65 static void auvitek_close(void *);
66 static const char * auvitek_get_devname(void *);
67 static const char * auvitek_get_businfo(void *);
68 static int auvitek_enum_format(void *, uint32_t,
69 struct video_format *);
70 static int auvitek_get_format(void *, struct video_format *);
71 static int auvitek_set_format(void *, struct video_format *);
72 static int auvitek_try_format(void *, struct video_format *);
73 static int auvitek_enum_standard(void *, uint32_t,
74 enum video_standard *);
75 static int auvitek_get_standard(void *, enum video_standard *);
76 static int auvitek_set_standard(void *, enum video_standard);
77 static int auvitek_start_transfer(void *);
78 static int auvitek_stop_transfer(void *);
79 static int auvitek_get_tuner(void *, struct video_tuner *);
80 static int auvitek_set_tuner(void *, struct video_tuner *);
81 static int auvitek_enum_audio(void *, uint32_t,
82 struct video_audio *);
83 static int auvitek_get_audio(void *, struct video_audio *);
84 static int auvitek_set_audio(void *, struct video_audio *);
85 static int auvitek_enum_input(void *, uint32_t,
86 struct video_input *);
87 static int auvitek_get_input(void *, struct video_input *);
88 static int auvitek_set_input(void *, struct video_input *);
89 static int auvitek_get_frequency(void *, struct video_frequency *);
90 static int auvitek_set_frequency(void *, struct video_frequency *);
91
92 static int auvitek_start_xfer(struct auvitek_softc *);
93 static int auvitek_stop_xfer(struct auvitek_softc *);
94 static int auvitek_isoc_start(struct auvitek_softc *);
95 static int auvitek_isoc_start1(struct auvitek_isoc *);
96 static void auvitek_isoc_intr(struct usbd_xfer *, void *,
97 usbd_status);
98 static int auvitek_isoc_process(struct auvitek_softc *,
99 uint8_t *, uint32_t);
100 static void auvitek_videobuf_weave(struct auvitek_softc *,
101 uint8_t *, uint32_t);
102
103 static const struct video_hw_if auvitek_video_if = {
104 .open = auvitek_open,
105 .close = auvitek_close,
106 .get_devname = auvitek_get_devname,
107 .get_businfo = auvitek_get_businfo,
108 .enum_format = auvitek_enum_format,
109 .get_format = auvitek_get_format,
110 .set_format = auvitek_set_format,
111 .try_format = auvitek_try_format,
112 .enum_standard = auvitek_enum_standard,
113 .get_standard = auvitek_get_standard,
114 .set_standard = auvitek_set_standard,
115 .start_transfer = auvitek_start_transfer,
116 .stop_transfer = auvitek_stop_transfer,
117 .get_tuner = auvitek_get_tuner,
118 .set_tuner = auvitek_set_tuner,
119 .enum_audio = auvitek_enum_audio,
120 .get_audio = auvitek_get_audio,
121 .set_audio = auvitek_set_audio,
122 .enum_input = auvitek_enum_input,
123 .get_input = auvitek_get_input,
124 .set_input = auvitek_set_input,
125 .get_frequency = auvitek_get_frequency,
126 .set_frequency = auvitek_set_frequency,
127 };
128
129 int
130 auvitek_video_attach(struct auvitek_softc *sc)
131 {
132 snprintf(sc->sc_businfo, sizeof(sc->sc_businfo), "usb:%08x",
133 sc->sc_udev->ud_cookie.cookie);
134
135 auvitek_video_rescan(sc, NULL, NULL);
136
137 return (sc->sc_videodev != NULL);
138 }
139
140 int
141 auvitek_video_detach(struct auvitek_softc *sc, int flags)
142 {
143 if (sc->sc_videodev != NULL) {
144 config_detach(sc->sc_videodev, flags);
145 sc->sc_videodev = NULL;
146 }
147
148 return 0;
149 }
150
151 void
152 auvitek_video_rescan(struct auvitek_softc *sc, const char *ifattr,
153 const int *locs)
154 {
155 if (ifattr_match(ifattr, "videobus") && sc->sc_videodev == NULL)
156 sc->sc_videodev = video_attach_mi(&auvitek_video_if,
157 sc->sc_dev);
158 }
159
160 void
161 auvitek_video_childdet(struct auvitek_softc *sc, device_t child)
162 {
163 if (sc->sc_videodev == child)
164 sc->sc_videodev = NULL;
165 }
166
167 static int
168 auvitek_open(void *opaque, int flags)
169 {
170 struct auvitek_softc *sc = opaque;
171
172 if (sc->sc_dying)
173 return EIO;
174
175 auvitek_attach_tuner(sc->sc_dev);
176
177 if (sc->sc_xc5k == NULL)
178 return ENXIO;
179
180 return 0;
181 }
182
183 static void
184 auvitek_close(void *opaque)
185 {
186 }
187
188 static const char *
189 auvitek_get_devname(void *opaque)
190 {
191 struct auvitek_softc *sc = opaque;
192
193 return sc->sc_descr;
194 }
195
196 static const char *
197 auvitek_get_businfo(void *opaque)
198 {
199 struct auvitek_softc *sc = opaque;
200
201 return sc->sc_businfo;
202 }
203
204 static int
205 auvitek_enum_format(void *opaque, uint32_t index, struct video_format *format)
206 {
207 if (index != AUVITEK_FORMAT_DEFAULT)
208 return EINVAL;
209
210 format->pixel_format = VIDEO_FORMAT_UYVY;
211
212 return 0;
213 }
214
215 static int
216 auvitek_get_format(void *opaque, struct video_format *format)
217 {
218
219 format->pixel_format = VIDEO_FORMAT_UYVY;
220 format->width = 720;
221 format->height = 480;
222 format->stride = format->width * 2;
223 format->sample_size = format->stride * format->height;
224 format->aspect_x = 4;
225 format->aspect_y = 3;
226 format->color.primaries = VIDEO_COLOR_PRIMARIES_SMPTE_170M;
227 format->color.gamma_function = VIDEO_GAMMA_FUNCTION_UNSPECIFIED;
228 format->color.matrix_coeff = VIDEO_MATRIX_COEFF_UNSPECIFIED;
229 format->interlace_flags = VIDEO_INTERLACE_ON;
230 format->priv = 0;
231
232 return 0;
233 }
234
235 static int
236 auvitek_set_format(void *opaque, struct video_format *format)
237 {
238 if (format->pixel_format != VIDEO_FORMAT_UYVY)
239 return EINVAL;
240
241 return auvitek_get_format(opaque, format);
242 }
243
244 static int
245 auvitek_try_format(void *opaque, struct video_format *format)
246 {
247 return auvitek_get_format(opaque, format);
248 }
249
250 static int
251 auvitek_enum_standard(void *opaque, uint32_t index, enum video_standard *vstd)
252 {
253 switch (index) {
254 case AUVITEK_STANDARD_NTSC_M:
255 *vstd = VIDEO_STANDARD_NTSC_M;
256 return 0;
257 default:
258 return EINVAL;
259 }
260 }
261
262 static int
263 auvitek_get_standard(void *opaque, enum video_standard *vstd)
264 {
265 *vstd = VIDEO_STANDARD_NTSC_M;
266 return 0;
267 }
268
269 static int
270 auvitek_set_standard(void *opaque, enum video_standard vstd)
271 {
272 switch (vstd) {
273 case VIDEO_STANDARD_NTSC_M:
274 return 0;
275 default:
276 return EINVAL;
277 }
278 }
279
280 static int
281 auvitek_start_transfer(void *opaque)
282 {
283 struct auvitek_softc *sc = opaque;
284 int error, s;
285 uint16_t vpos = 0, hpos = 0;
286 uint16_t hres = 720 * 2;
287 uint16_t vres = 484 / 2;
288
289 auvitek_write_1(sc, AU0828_REG_SENSORVBI_CTL, 0x00);
290
291 /* program video position and size */
292 auvitek_write_1(sc, AU0828_REG_HPOS_LO, hpos & 0xff);
293 auvitek_write_1(sc, AU0828_REG_HPOS_HI, hpos >> 8);
294 auvitek_write_1(sc, AU0828_REG_VPOS_LO, vpos & 0xff);
295 auvitek_write_1(sc, AU0828_REG_VPOS_HI, vpos >> 8);
296 auvitek_write_1(sc, AU0828_REG_HRES_LO, hres & 0xff);
297 auvitek_write_1(sc, AU0828_REG_HRES_HI, hres >> 8);
298 auvitek_write_1(sc, AU0828_REG_VRES_LO, vres & 0xff);
299 auvitek_write_1(sc, AU0828_REG_VRES_HI, vres >> 8);
300
301 auvitek_write_1(sc, AU0828_REG_SENSOR_CTL, 0xb3);
302
303 auvitek_write_1(sc, AU0828_REG_AUDIOCTL, 0x01);
304
305 s = splusb();
306 error = auvitek_start_xfer(sc);
307 splx(s);
308
309 if (error)
310 auvitek_stop_transfer(sc);
311
312 return error;
313 }
314
315 static int
316 auvitek_stop_transfer(void *opaque)
317 {
318 struct auvitek_softc *sc = opaque;
319 int error, s;
320
321 auvitek_write_1(sc, AU0828_REG_SENSOR_CTL, 0x00);
322
323 s = splusb();
324 error = auvitek_stop_xfer(sc);
325 splx(s);
326
327 return error;
328 }
329
330 static int
331 auvitek_get_tuner(void *opaque, struct video_tuner *vt)
332 {
333 struct auvitek_softc *sc = opaque;
334
335 switch (vt->index) {
336 case AUVITEK_TUNER_DEFAULT:
337 strlcpy(vt->name, "XC5000", sizeof(vt->name));
338 vt->freq_lo = 44000000 / 62500;
339 vt->freq_hi = 958000000 / 62500;
340 vt->caps = VIDEO_TUNER_F_STEREO;
341 vt->mode = VIDEO_TUNER_F_STEREO;
342 if (sc->sc_au8522)
343 vt->signal = au8522_get_signal(sc->sc_au8522);
344 else
345 vt->signal = 0;
346 break;
347 default:
348 return EINVAL;
349 }
350
351 return 0;
352 }
353
354 static int
355 auvitek_set_tuner(void *opaque, struct video_tuner *vt)
356 {
357 if (vt->index != AUVITEK_TUNER_DEFAULT)
358 return EINVAL;
359 return 0;
360 }
361
362 static int
363 auvitek_enum_audio(void *opaque, uint32_t index, struct video_audio *va)
364 {
365 switch (index) {
366 case AUVITEK_AUDIO_TELEVISION:
367 strlcpy(va->name, "Television", sizeof(va->name));
368 va->caps = VIDEO_AUDIO_F_STEREO;
369 break;
370 case AUVITEK_AUDIO_LINEIN:
371 strlcpy(va->name, "Line In", sizeof(va->name));
372 va->caps = VIDEO_AUDIO_F_STEREO;
373 break;
374 default:
375 return EINVAL;
376 }
377
378 return 0;
379 }
380
381 static int
382 auvitek_get_audio(void *opaque, struct video_audio *va)
383 {
384 struct auvitek_softc *sc = opaque;
385
386 return auvitek_enum_audio(opaque, sc->sc_ainput, va);
387 }
388
389 static int
390 auvitek_set_audio(void *opaque, struct video_audio *va)
391 {
392 struct auvitek_softc *sc = opaque;
393
394 if (va->index == sc->sc_ainput)
395 return 0;
396
397 return EINVAL;
398 }
399
400 static int
401 auvitek_enum_input(void *opaque, uint32_t index, struct video_input *vi)
402 {
403 switch (index) {
404 case AUVITEK_INPUT_COMPOSITE:
405 strlcpy(vi->name, "Composite", sizeof(vi->name));
406 vi->type = VIDEO_INPUT_TYPE_BASEBAND;
407 vi->standards = VIDEO_STANDARD_NTSC_M;
408 break;
409 case AUVITEK_INPUT_SVIDEO:
410 strlcpy(vi->name, "S-Video", sizeof(vi->name));
411 vi->type = VIDEO_INPUT_TYPE_BASEBAND;
412 vi->standards = VIDEO_STANDARD_NTSC_M;
413 break;
414 case AUVITEK_INPUT_TELEVISION:
415 strlcpy(vi->name, "Television", sizeof(vi->name));
416 vi->type = VIDEO_INPUT_TYPE_TUNER;
417 vi->standards = VIDEO_STANDARD_NTSC_M;
418 break;
419 case AUVITEK_INPUT_CABLE:
420 strlcpy(vi->name, "Cable TV", sizeof(vi->name));
421 vi->type = VIDEO_INPUT_TYPE_TUNER;
422 vi->standards = VIDEO_STANDARD_NTSC_M;
423 break;
424 default:
425 return EINVAL;
426 }
427
428 vi->index = index;
429 vi->tuner_index = AUVITEK_TUNER_DEFAULT;
430
431 return 0;
432 }
433
434 static int
435 auvitek_get_input(void *opaque, struct video_input *vi)
436 {
437 struct auvitek_softc *sc = opaque;
438
439 return auvitek_enum_input(opaque, sc->sc_vinput, vi);
440 }
441
442 static int
443 auvitek_set_input(void *opaque, struct video_input *vi)
444 {
445 struct auvitek_softc *sc = opaque;
446 struct video_frequency vf;
447 au8522_vinput_t vinput = AU8522_VINPUT_UNCONF;
448 au8522_ainput_t ainput = AU8522_AINPUT_UNCONF;
449 uint8_t r;
450
451 switch (vi->index) {
452 case AUVITEK_INPUT_COMPOSITE:
453 vinput = AU8522_VINPUT_CVBS;
454 ainput = AU8522_AINPUT_NONE;
455 sc->sc_ainput = AUVITEK_AUDIO_LINEIN;
456 break;
457 case AUVITEK_INPUT_SVIDEO:
458 vinput = AU8522_VINPUT_SVIDEO;
459 ainput = AU8522_AINPUT_NONE;
460 sc->sc_ainput = AUVITEK_AUDIO_LINEIN;
461 break;
462 case AUVITEK_INPUT_TELEVISION:
463 case AUVITEK_INPUT_CABLE:
464 vinput = AU8522_VINPUT_CVBS_TUNER;
465 ainput = AU8522_AINPUT_SIF;
466 sc->sc_ainput = AUVITEK_AUDIO_TELEVISION;
467 break;
468 default:
469 return EINVAL;
470 }
471
472 sc->sc_vinput = vi->index;
473
474 au8522_set_input(sc->sc_au8522, vinput, ainput);
475
476 /* XXX HVR-850/950Q specific */
477 r = auvitek_read_1(sc, AU0828_REG_GPIO1_OUTEN);
478 if (ainput == AU8522_AINPUT_NONE)
479 r |= 0x10;
480 else
481 r &= ~0x10;
482 auvitek_write_1(sc, AU0828_REG_GPIO1_OUTEN, r);
483
484 if (vinput == AU8522_VINPUT_CVBS_TUNER && sc->sc_curfreq > 0) {
485 vf.tuner_index = AUVITEK_TUNER_DEFAULT;
486 vf.frequency = sc->sc_curfreq;
487 auvitek_set_frequency(sc, &vf);
488 }
489
490 return 0;
491 }
492
493 static int
494 auvitek_get_frequency(void *opaque, struct video_frequency *vf)
495 {
496 struct auvitek_softc *sc = opaque;
497
498 if (sc->sc_vinput != AUVITEK_INPUT_TELEVISION &&
499 sc->sc_vinput != AUVITEK_INPUT_CABLE)
500 return EINVAL;
501
502 vf->tuner_index = AUVITEK_TUNER_DEFAULT;
503 vf->frequency = sc->sc_curfreq;
504
505 return 0;
506 }
507
508 static int
509 auvitek_set_frequency(void *opaque, struct video_frequency *vf)
510 {
511 struct auvitek_softc *sc = opaque;
512 struct xc5k_params params;
513 int error;
514
515 if (sc->sc_vinput != AUVITEK_INPUT_TELEVISION &&
516 sc->sc_vinput != AUVITEK_INPUT_CABLE)
517 return EINVAL;
518 if (vf->tuner_index != AUVITEK_TUNER_DEFAULT)
519 return EINVAL;
520 if (sc->sc_xc5k == NULL)
521 return ENODEV;
522
523 params.standard = VIDEO_STANDARD_NTSC_M;
524 if (sc->sc_vinput == AUVITEK_INPUT_TELEVISION)
525 params.signal_source = XC5K_SIGNAL_SOURCE_AIR;
526 else
527 params.signal_source = XC5K_SIGNAL_SOURCE_CABLE;
528 params.frequency = vf->frequency;
529 if (sc->sc_au8522)
530 au8522_set_audio(sc->sc_au8522, false);
531 error = xc5k_tune_video(sc->sc_xc5k, ¶ms);
532 if (sc->sc_au8522)
533 au8522_set_audio(sc->sc_au8522, true);
534 if (error)
535 return error;
536
537 sc->sc_curfreq = vf->frequency;
538
539 auvitek_write_1(sc, AU0828_REG_SENSOR_CTL, 0x00);
540 delay(30000);
541 auvitek_write_1(sc, AU0828_REG_SENSOR_CTL, 0xb3);
542
543 return 0;
544 }
545
546 static int
547 auvitek_start_xfer(struct auvitek_softc *sc)
548 {
549 struct auvitek_xfer *ax = &sc->sc_ax;
550 uint32_t vframe_len, uframe_len, nframes;
551 usbd_status err;
552 int i;
553
554 err = usbd_set_interface(sc->sc_isoc_iface, AUVITEK_XFER_ALTNO);
555 if (err != USBD_NORMAL_COMPLETION) {
556 aprint_error_dev(sc->sc_dev, "couldn't set altno %d: %s\n",
557 AUVITEK_XFER_ALTNO, usbd_errstr(err));
558 return EIO;
559 }
560
561 vframe_len = 720 * 480 * 2;
562 uframe_len = ax->ax_maxpktlen;
563 nframes = (vframe_len + uframe_len - 1) / uframe_len;
564 nframes = (nframes + 7) & ~7;
565
566 ax->ax_nframes = nframes;
567 ax->ax_uframe_len = uframe_len;
568 for (i = 0; i < AUVITEK_NISOC_XFERS; i++) {
569 struct auvitek_isoc *isoc = &ax->ax_i[i];
570 isoc->i_ax = ax;
571 isoc->i_frlengths =
572 kmem_alloc(sizeof(isoc->i_frlengths[0]) * nframes,
573 KM_SLEEP);
574 }
575
576 err = usbd_open_pipe(sc->sc_isoc_iface, ax->ax_endpt,
577 USBD_EXCLUSIVE_USE, &ax->ax_pipe);
578 if (err != USBD_NORMAL_COMPLETION) {
579 aprint_error_dev(sc->sc_dev, "couldn't open pipe: %s\n",
580 usbd_errstr(err));
581 return EIO;
582 }
583
584 for (i = 0; i < AUVITEK_NISOC_XFERS; i++) {
585 struct auvitek_isoc *isoc = &ax->ax_i[i];
586
587 isoc->i_xfer = usbd_alloc_xfer(sc->sc_udev);
588 if (isoc->i_xfer == NULL) {
589 aprint_error_dev(sc->sc_dev,
590 "couldn't allocate usb xfer\n");
591 return ENOMEM;
592 }
593
594 isoc->i_buf = usbd_alloc_buffer(isoc->i_xfer,
595 nframes * uframe_len);
596 if (isoc->i_buf == NULL) {
597 aprint_error_dev(sc->sc_dev,
598 "couldn't allocate usb xfer buffer\n");
599 return ENOMEM;
600 }
601 }
602
603 return auvitek_isoc_start(sc);
604 }
605
606 static int
607 auvitek_stop_xfer(struct auvitek_softc *sc)
608 {
609 struct auvitek_xfer *ax = &sc->sc_ax;
610 usbd_status err;
611 int i;
612
613 if (ax->ax_pipe != NULL) {
614 usbd_abort_pipe(ax->ax_pipe);
615 usbd_close_pipe(ax->ax_pipe);
616 ax->ax_pipe = NULL;
617 }
618
619 for (i = 0; i < AUVITEK_NISOC_XFERS; i++) {
620 struct auvitek_isoc *isoc = &ax->ax_i[i];
621 if (isoc->i_xfer != NULL) {
622 usbd_free_buffer(isoc->i_xfer);
623 usbd_free_xfer(isoc->i_xfer);
624 isoc->i_xfer = NULL;
625 }
626 if (isoc->i_frlengths != NULL) {
627 kmem_free(isoc->i_frlengths,
628 sizeof(isoc->i_frlengths[0]) * ax->ax_nframes);
629 isoc->i_frlengths = NULL;
630 }
631 }
632
633 usbd_delay_ms(sc->sc_udev, 1000);
634 err = usbd_set_interface(sc->sc_isoc_iface, 0);
635 if (err != USBD_NORMAL_COMPLETION) {
636 aprint_error_dev(sc->sc_dev,
637 "couldn't set zero bw interface: %s\n",
638 usbd_errstr(err));
639 return EIO;
640 }
641
642 return 0;
643 }
644
645 static int
646 auvitek_isoc_start(struct auvitek_softc *sc)
647 {
648 struct auvitek_xfer *ax = &sc->sc_ax;
649 int i, error;
650
651 ax->ax_av.av_el = ax->ax_av.av_ol = 0;
652 ax->ax_av.av_eb = ax->ax_av.av_ob = 0;
653 ax->ax_av.av_stride = 720 * 2;
654
655 for (i = 0; i < AUVITEK_NISOC_XFERS; i++) {
656 error = auvitek_isoc_start1(&ax->ax_i[i]);
657 if (error)
658 return error;
659 }
660
661 return 0;
662 }
663
664 static int
665 auvitek_isoc_start1(struct auvitek_isoc *isoc)
666 {
667 struct auvitek_xfer *ax = isoc->i_ax;
668 struct auvitek_softc *sc = ax->ax_sc;
669 usbd_status err;
670 unsigned int i;
671
672 ax = isoc->i_ax;
673
674 for (i = 0; i < ax->ax_nframes; i++)
675 isoc->i_frlengths[i] = ax->ax_uframe_len;
676
677 usbd_setup_isoc_xfer(isoc->i_xfer,
678 ax->ax_pipe,
679 isoc,
680 isoc->i_frlengths,
681 ax->ax_nframes,
682 USBD_SHORT_XFER_OK,
683 auvitek_isoc_intr);
684
685 err = usbd_transfer(isoc->i_xfer);
686 if (err != USBD_IN_PROGRESS) {
687 aprint_error_dev(sc->sc_dev, "couldn't start isoc xfer: %s\n",
688 usbd_errstr(err));
689 return ENODEV;
690 }
691
692 return 0;
693 }
694
695 static void
696 auvitek_isoc_intr(struct usbd_xfer *xfer, void * priv,
697 usbd_status status)
698 {
699 struct auvitek_isoc *isoc = priv;
700 struct auvitek_xfer *ax = isoc->i_ax;
701 struct auvitek_softc *sc = ax->ax_sc;
702 uint32_t count;
703 uint8_t *buf;
704 unsigned int i;
705
706 if (sc->sc_dying)
707 return;
708
709 if (status != USBD_NORMAL_COMPLETION) {
710 if (status == USBD_STALLED) {
711 usbd_clear_endpoint_stall_async(ax->ax_pipe);
712 goto next;
713 }
714 return;
715 }
716
717 usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
718
719 if (count == 0)
720 goto next;
721
722 for (i = 0, buf = isoc->i_buf;
723 i < ax->ax_nframes;
724 ++i, buf += ax->ax_uframe_len) {
725 status = auvitek_isoc_process(sc, buf, isoc->i_frlengths[i]);
726 if (status == USBD_IOERROR)
727 break;
728 }
729
730 next:
731 auvitek_isoc_start1(isoc);
732 }
733
734 static int
735 auvitek_isoc_process(struct auvitek_softc *sc, uint8_t *buf, uint32_t len)
736 {
737 struct video_payload payload;
738 bool submit = false;
739
740 if (buf[0] & 0x80) {
741 sc->sc_ax.ax_frinfo = buf[0];
742 if (sc->sc_ax.ax_frinfo & 0x40) {
743 sc->sc_ax.ax_frno = !sc->sc_ax.ax_frno;
744 submit = true;
745 }
746 buf += 4;
747 len -= 4;
748 }
749 buf += 4;
750 len -= 4;
751
752 auvitek_videobuf_weave(sc, buf, len);
753
754 if (submit) {
755 payload.end_of_frame = 1;
756 payload.data = sc->sc_ax.ax_av.av_buf;
757 payload.size = sizeof(sc->sc_ax.ax_av.av_buf);
758 payload.frameno = -1;
759
760 video_submit_payload(sc->sc_videodev, &payload);
761
762 sc->sc_ax.ax_av.av_el = sc->sc_ax.ax_av.av_ol = 0;
763 sc->sc_ax.ax_av.av_eb = sc->sc_ax.ax_av.av_ob = 0;
764 }
765
766 return USBD_NORMAL_COMPLETION;
767 }
768
769 static void
770 auvitek_videobuf_weave(struct auvitek_softc *sc, uint8_t *buf, uint32_t len)
771 {
772 struct auvitek_videobuf *av = &sc->sc_ax.ax_av;
773 uint32_t resid, wlen;
774 uint32_t *l, *b;
775 uint8_t *vp;
776
777 if (sc->sc_ax.ax_frinfo & 0x40) {
778 l = &av->av_ol;
779 b = &av->av_ob;
780 vp = av->av_buf;
781 } else {
782 l = &av->av_el;
783 b = &av->av_eb;
784 vp = av->av_buf + av->av_stride;
785 }
786
787 resid = len;
788 while (resid > 0) {
789 if (*b == av->av_stride) {
790 *l = *l + 1;
791 *b = 0;
792 }
793 if (*l >= 240) {
794 break;
795 }
796 wlen = min(resid, av->av_stride - *b);
797 memcpy(vp + (av->av_stride * 2 * *l) + *b, buf, wlen);
798 *b += wlen;
799 buf += wlen;
800 resid -= wlen;
801 }
802 }
803