uvideo.c revision 1.72 1 /* $NetBSD: uvideo.c,v 1.72 2022/04/06 22:01:45 mlelstv Exp $ */
2
3 /*
4 * Copyright (c) 2008 Patrick Mahoney
5 * All rights reserved.
6 *
7 * This code was written by Patrick Mahoney (pat (at) polycrystal.org) as
8 * part of Google Summer of Code 2008.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * USB video specs:
41 * http://www.usb.org/developers/devclass_docs/USB_Video_Class_1_1.zip
42 */
43
44 #include <sys/cdefs.h>
45 __KERNEL_RCSID(0, "$NetBSD: uvideo.c,v 1.72 2022/04/06 22:01:45 mlelstv Exp $");
46
47 #ifdef _KERNEL_OPT
48 #include "opt_usb.h"
49 #endif
50
51 #ifdef _MODULE
52 #include <sys/module.h>
53 #endif
54
55 #include <sys/param.h>
56 #include <sys/systm.h>
57 #include <sys/kernel.h>
58 #include <sys/kmem.h>
59 #include <sys/device.h>
60 #include <sys/ioctl.h>
61 #include <sys/uio.h>
62 #include <sys/file.h>
63 #include <sys/select.h>
64 #include <sys/proc.h>
65 #include <sys/conf.h>
66 #include <sys/vnode.h>
67 #include <sys/poll.h>
68 #include <sys/queue.h> /* SLIST */
69 #include <sys/kthread.h>
70 #include <sys/bus.h>
71
72 #include <sys/videoio.h>
73 #include <dev/video_if.h>
74
75 #include <dev/usb/usb.h>
76 #include <dev/usb/usbdi.h>
77 #include <dev/usb/usbdivar.h>
78 #include <dev/usb/usbdi_util.h>
79 #include <dev/usb/usb_quirks.h>
80
81 #include <dev/usb/uvideoreg.h>
82
83 #define UVIDEO_NXFERS 3
84 #define UVIDEO_NFRAMES_MAX 80
85 #define PRI_UVIDEO PRI_BIO
86
87 /* #define UVIDEO_DISABLE_MJPEG */
88
89 #ifdef UVIDEO_DEBUG
90 #define DPRINTF(x) do { if (uvideodebug) printf x; } while (0)
91 #define DPRINTFN(n,x) do { if (uvideodebug>(n)) printf x; } while (0)
92 int uvideodebug = 20;
93 #else
94 #define DPRINTF(x) __nothing
95 #define DPRINTFN(n,x) __nothing
96 #endif
97
98 typedef enum {
99 UVIDEO_STATE_CLOSED,
100 UVIDEO_STATE_OPENING,
101 UVIDEO_STATE_IDLE
102 } uvideo_state;
103
104 struct uvideo_camera_terminal {
105 uint16_t ct_objective_focal_min;
106 uint16_t ct_objective_focal_max;
107 uint16_t ct_ocular_focal_length;
108 };
109
110 struct uvideo_processing_unit {
111 uint16_t pu_max_multiplier; /* digital zoom */
112 uint8_t pu_video_standards;
113 };
114
115 struct uvideo_extension_unit {
116 guid_t xu_guid;
117 };
118
119 /* For simplicity, we consider a Terminal a special case of Unit
120 * rather than a separate entity. */
121 struct uvideo_unit {
122 uint8_t vu_id;
123 uint8_t vu_type;
124 uint8_t vu_dst_id;
125 uint8_t vu_nsrcs;
126 union {
127 uint8_t vu_src_id; /* vu_nsrcs = 1 */
128 uint8_t *vu_src_id_ary; /* vu_nsrcs > 1 */
129 } s;
130
131 /* fields for individual unit/terminal types */
132 union {
133 struct uvideo_camera_terminal vu_camera;
134 struct uvideo_processing_unit vu_processing;
135 struct uvideo_extension_unit vu_extension;
136 } u;
137
138 /* Used by camera terminal, processing and extension units. */
139 uint8_t vu_control_size; /* number of bytes in vu_controls */
140 uint8_t *vu_controls; /* array of bytes. bits are
141 * numbered from 0 at least
142 * significant bit to
143 * (8*vu_control_size - 1)*/
144 };
145
146 struct uvideo_alternate {
147 uint8_t altno;
148 uint8_t interval;
149 uint16_t max_packet_size;
150 SLIST_ENTRY(uvideo_alternate) entries;
151 };
152 SLIST_HEAD(altlist, uvideo_alternate);
153
154 #define UVIDEO_FORMAT_GET_FORMAT_INDEX(fmt) \
155 ((fmt)->format.priv & 0xff)
156 #define UVIDEO_FORMAT_GET_FRAME_INDEX(fmt) \
157 (((fmt)->format.priv >> 8) & 0xff)
158 /* TODO: find a better way to set bytes within this 32 bit value? */
159 #define UVIDEO_FORMAT_SET_FORMAT_INDEX(fmt, index) do { \
160 (fmt)->format.priv &= ~0xff; \
161 (fmt)->format.priv |= ((index) & 0xff); \
162 } while (0)
163 #define UVIDEO_FORMAT_SET_FRAME_INDEX(fmt, index) do { \
164 (fmt)->format.priv &= ~(0xff << 8); \
165 ((fmt)->format.priv |= (((index) & 0xff) << 8)); \
166 } while (0)
167
168 struct uvideo_pixel_format {
169 enum video_pixel_format pixel_format;
170 SIMPLEQ_ENTRY(uvideo_pixel_format) entries;
171 };
172 SIMPLEQ_HEAD(uvideo_pixel_format_list, uvideo_pixel_format);
173
174 struct uvideo_format {
175 struct video_format format;
176 SIMPLEQ_ENTRY(uvideo_format) entries;
177 };
178 SIMPLEQ_HEAD(uvideo_format_list, uvideo_format);
179
180 struct uvideo_isoc_xfer;
181 struct uvideo_stream;
182
183 struct uvideo_isoc {
184 struct uvideo_isoc_xfer *i_ix;
185 struct uvideo_stream *i_vs;
186 struct usbd_xfer *i_xfer;
187 uint8_t *i_buf;
188 uint16_t *i_frlengths;
189 };
190
191 struct uvideo_isoc_xfer {
192 uint8_t ix_endpt;
193 struct usbd_pipe *ix_pipe;
194 struct uvideo_isoc ix_i[UVIDEO_NXFERS];
195 uint32_t ix_nframes;
196 uint32_t ix_uframe_len;
197
198 struct altlist ix_altlist;
199 };
200
201 struct uvideo_bulk_xfer {
202 uint8_t bx_endpt;
203 struct usbd_pipe *bx_pipe;
204 struct usbd_xfer *bx_xfer;
205 uint8_t *bx_buffer;
206 int bx_buflen;
207 bool bx_running;
208 kcondvar_t bx_cv;
209 kmutex_t bx_lock;
210 };
211
212 struct uvideo_stream {
213 device_t vs_videodev;
214 struct uvideo_softc *vs_parent;
215 struct usbd_interface *vs_iface;
216 uint8_t vs_ifaceno;
217 uint8_t vs_subtype; /* input or output */
218 uint16_t vs_probelen; /* length of probe and
219 * commit data; varies
220 * depending on version
221 * of spec. */
222 struct uvideo_format_list vs_formats;
223 struct uvideo_pixel_format_list vs_pixel_formats;
224 struct video_format *vs_default_format;
225 struct video_format vs_current_format;
226
227 /* usb transfer details */
228 uint8_t vs_xfer_type;
229 union {
230 struct uvideo_bulk_xfer bulk;
231 struct uvideo_isoc_xfer isoc;
232 } vs_xfer;
233
234 int vs_frameno; /* toggles between 0 and 1 */
235
236 /* current video format */
237 uint32_t vs_max_payload_size;
238 uint32_t vs_frame_interval;
239 SLIST_ENTRY(uvideo_stream) entries;
240
241 uvideo_state vs_state;
242 };
243 SLIST_HEAD(uvideo_stream_list, uvideo_stream);
244
245 struct uvideo_softc {
246 device_t sc_dev; /* base device */
247 struct usbd_device *sc_udev; /* device */
248 struct usbd_interface *sc_iface; /* interface handle */
249 int sc_ifaceno; /* interface number */
250 char *sc_devname;
251
252 int sc_dying;
253
254 uint8_t sc_nunits;
255 struct uvideo_unit **sc_unit;
256
257 struct uvideo_stream_list sc_stream_list;
258
259 char sc_businfo[32];
260 };
261
262 static int uvideo_match(device_t, cfdata_t, void *);
263 static void uvideo_attach(device_t, device_t, void *);
264 static int uvideo_detach(device_t, int);
265 static void uvideo_childdet(device_t, device_t);
266 static int uvideo_activate(device_t, enum devact);
267
268 static int uvideo_open(void *, int);
269 static void uvideo_close(void *);
270 static const char * uvideo_get_devname(void *);
271 static const char * uvideo_get_businfo(void *);
272
273 static int uvideo_enum_format(void *, uint32_t, struct video_format *);
274 static int uvideo_get_format(void *, struct video_format *);
275 static int uvideo_set_format(void *, struct video_format *);
276 static int uvideo_try_format(void *, struct video_format *);
277 static int uvideo_get_framerate(void *, struct video_fract *);
278 static int uvideo_set_framerate(void *, struct video_fract *);
279 static int uvideo_start_transfer(void *);
280 static int uvideo_stop_transfer(void *);
281
282 static int uvideo_get_control_group(void *,
283 struct video_control_group *);
284 static int uvideo_set_control_group(void *,
285 const struct video_control_group *);
286
287 static usbd_status uvideo_init_control(
288 struct uvideo_softc *,
289 const usb_interface_descriptor_t *,
290 usbd_desc_iter_t *);
291 static usbd_status uvideo_init_collection(
292 struct uvideo_softc *,
293 const usb_interface_descriptor_t *,
294 usbd_desc_iter_t *);
295
296 /* Functions for unit & terminal descriptors */
297 static struct uvideo_unit * uvideo_unit_alloc(const uvideo_descriptor_t *);
298 static usbd_status uvideo_unit_init(struct uvideo_unit *,
299 const uvideo_descriptor_t *);
300 static void uvideo_unit_free(struct uvideo_unit *);
301 static usbd_status uvideo_unit_alloc_controls(struct uvideo_unit *,
302 uint8_t,
303 const uint8_t *);
304 static void uvideo_unit_free_controls(struct uvideo_unit *);
305 static usbd_status uvideo_unit_alloc_sources(struct uvideo_unit *,
306 uint8_t,
307 const uint8_t *);
308 static void uvideo_unit_free_sources(struct uvideo_unit *);
309
310
311
312
313 /* Functions for uvideo_stream, primary unit associated with a video
314 * driver or device file. */
315 static struct uvideo_stream * uvideo_find_stream(struct uvideo_softc *,
316 uint8_t);
317 #if 0
318 static struct uvideo_format * uvideo_stream_find_format(
319 struct uvideo_stream *,
320 uint8_t, uint8_t);
321 #endif
322 static struct uvideo_format * uvideo_stream_guess_format(
323 struct uvideo_stream *,
324 enum video_pixel_format, uint32_t, uint32_t);
325 static struct uvideo_stream * uvideo_stream_alloc(void);
326 static usbd_status uvideo_stream_init(
327 struct uvideo_stream *,
328 struct uvideo_softc *,
329 const usb_interface_descriptor_t *);
330 static usbd_status uvideo_stream_init_desc(
331 struct uvideo_stream *,
332 const usb_interface_descriptor_t *,
333 usbd_desc_iter_t *);
334 static usbd_status uvideo_stream_init_frame_based_format(
335 struct uvideo_stream *,
336 const uvideo_descriptor_t *,
337 usbd_desc_iter_t *);
338 static void uvideo_stream_free(struct uvideo_stream *);
339
340 static int uvideo_stream_start_xfer(struct uvideo_stream *);
341 static int uvideo_stream_stop_xfer(struct uvideo_stream *);
342 static usbd_status uvideo_stream_recv_process(struct uvideo_stream *,
343 uint8_t *, uint32_t);
344 static usbd_status uvideo_stream_recv_isoc_start(struct uvideo_stream *);
345 static usbd_status uvideo_stream_recv_isoc_start1(struct uvideo_isoc *);
346 static void uvideo_stream_recv_isoc_complete(struct usbd_xfer *,
347 void *,
348 usbd_status);
349 static void uvideo_stream_recv_bulk_transfer(void *);
350
351 /* format probe and commit */
352 #define uvideo_stream_probe(vs, act, data) \
353 (uvideo_stream_probe_and_commit((vs), (act), \
354 UVIDEO_VS_PROBE_CONTROL, (data)))
355 #define uvideo_stream_commit(vs, act, data) \
356 (uvideo_stream_probe_and_commit((vs), (act), \
357 UVIDEO_VS_COMMIT_CONTROL, (data)))
358 static usbd_status uvideo_stream_probe_and_commit(struct uvideo_stream *,
359 uint8_t, uint8_t,
360 void *);
361 static void uvideo_init_probe_data(uvideo_probe_and_commit_data_t *);
362
363
364 static int usb_guid_cmp(const usb_guid_t *, const guid_t *);
365
366
367 CFATTACH_DECL2_NEW(uvideo, sizeof(struct uvideo_softc),
368 uvideo_match, uvideo_attach, uvideo_detach, uvideo_activate, NULL,
369 uvideo_childdet);
370
371
372
373
374 static const struct video_hw_if uvideo_hw_if = {
375 .open = uvideo_open,
376 .close = uvideo_close,
377 .get_devname = uvideo_get_devname,
378 .get_businfo = uvideo_get_businfo,
379 .enum_format = uvideo_enum_format,
380 .get_format = uvideo_get_format,
381 .set_format = uvideo_set_format,
382 .try_format = uvideo_try_format,
383 .get_framerate = uvideo_get_framerate,
384 .set_framerate = uvideo_set_framerate,
385 .start_transfer = uvideo_start_transfer,
386 .stop_transfer = uvideo_stop_transfer,
387 .control_iter_init = NULL,
388 .control_iter_next = NULL,
389 .get_control_desc_group = NULL,
390 .get_control_group = uvideo_get_control_group,
391 .set_control_group = uvideo_set_control_group,
392 };
393
394 #ifdef UVIDEO_DEBUG
395 /* Some functions to print out descriptors. Mostly useless other than
396 * debugging/exploration purposes. */
397 static void usb_guid_print(const usb_guid_t *);
398 static void print_descriptor(const usb_descriptor_t *);
399 static void print_interface_descriptor(const usb_interface_descriptor_t *);
400 static void print_endpoint_descriptor(const usb_endpoint_descriptor_t *);
401
402 static void print_vc_descriptor(const usb_descriptor_t *);
403 static void print_vs_descriptor(const usb_descriptor_t *);
404
405 static void print_vc_header_descriptor(
406 const uvideo_vc_header_descriptor_t *);
407 static void print_input_terminal_descriptor(
408 const uvideo_input_terminal_descriptor_t *);
409 static void print_output_terminal_descriptor(
410 const uvideo_output_terminal_descriptor_t *);
411 static void print_camera_terminal_descriptor(
412 const uvideo_camera_terminal_descriptor_t *);
413 static void print_selector_unit_descriptor(
414 const uvideo_selector_unit_descriptor_t *);
415 static void print_processing_unit_descriptor(
416 const uvideo_processing_unit_descriptor_t *);
417 static void print_extension_unit_descriptor(
418 const uvideo_extension_unit_descriptor_t *);
419 static void print_interrupt_endpoint_descriptor(
420 const uvideo_vc_interrupt_endpoint_descriptor_t *);
421
422 static void print_vs_input_header_descriptor(
423 const uvideo_vs_input_header_descriptor_t *);
424 static void print_vs_output_header_descriptor(
425 const uvideo_vs_output_header_descriptor_t *);
426
427 static void print_vs_format_uncompressed_descriptor(
428 const uvideo_vs_format_uncompressed_descriptor_t *);
429 static void print_vs_frame_uncompressed_descriptor(
430 const uvideo_vs_frame_uncompressed_descriptor_t *);
431 static void print_vs_format_mjpeg_descriptor(
432 const uvideo_vs_format_mjpeg_descriptor_t *);
433 static void print_vs_frame_mjpeg_descriptor(
434 const uvideo_vs_frame_mjpeg_descriptor_t *);
435 static void print_vs_format_dv_descriptor(
436 const uvideo_vs_format_dv_descriptor_t *);
437 #endif /* !UVIDEO_DEBUG */
438
439 #define GET(type, descp, field) (((const type *)(descp))->field)
440 #define GETP(type, descp, field) (&(((const type *)(descp))->field))
441
442 /* Given a format descriptor and frame descriptor, copy values common
443 * to all formats into a struct uvideo_format. */
444 #define UVIDEO_FORMAT_INIT_FRAME_BASED(format_type, format_desc, \
445 frame_type, frame_desc, \
446 format) \
447 do { \
448 UVIDEO_FORMAT_SET_FORMAT_INDEX( \
449 format, \
450 GET(format_type, format_desc, bFormatIndex)); \
451 UVIDEO_FORMAT_SET_FRAME_INDEX( \
452 format, \
453 GET(frame_type, frame_desc, bFrameIndex)); \
454 format->format.width = \
455 UGETW(GET(frame_type, frame_desc, wWidth)); \
456 format->format.height = \
457 UGETW(GET(frame_type, frame_desc, wHeight)); \
458 format->format.aspect_x = \
459 GET(format_type, format_desc, bAspectRatioX); \
460 format->format.aspect_y = \
461 GET(format_type, format_desc, bAspectRatioY); \
462 } while (0)
463
464
465 static int
466 uvideo_match(device_t parent, cfdata_t match, void *aux)
467 {
468 struct usbif_attach_arg *uiaa = aux;
469
470 /* TODO: May need to change in the future to work with
471 * Interface Association Descriptor. */
472
473 /* Trigger on the Video Control Interface which must be present */
474 if (uiaa->uiaa_class == UICLASS_VIDEO &&
475 uiaa->uiaa_subclass == UISUBCLASS_VIDEOCONTROL)
476 return UMATCH_IFACECLASS_IFACESUBCLASS;
477
478 return UMATCH_NONE;
479 }
480
481 static void
482 uvideo_attach(device_t parent, device_t self, void *aux)
483 {
484 struct uvideo_softc *sc = device_private(self);
485 struct usbif_attach_arg *uiaa = aux;
486 usbd_desc_iter_t iter;
487 const usb_interface_descriptor_t *ifdesc;
488 struct uvideo_stream *vs;
489 usbd_status err;
490
491 sc->sc_dev = self;
492
493 sc->sc_devname = usbd_devinfo_alloc(uiaa->uiaa_device, 0);
494
495 aprint_naive("\n");
496 aprint_normal(": %s\n", sc->sc_devname);
497
498 sc->sc_udev = uiaa->uiaa_device;
499 sc->sc_iface = uiaa->uiaa_iface;
500 sc->sc_ifaceno = uiaa->uiaa_ifaceno;
501 sc->sc_dying = 0;
502 SLIST_INIT(&sc->sc_stream_list);
503 snprintf(sc->sc_businfo, sizeof(sc->sc_businfo), "usb:%08x",
504 sc->sc_udev->ud_cookie.cookie);
505
506 #ifdef UVIDEO_DEBUG
507 /* Debugging dump of descriptors. TODO: move this to userspace
508 * via a custom IOCTL or something. */
509 const usb_descriptor_t *desc;
510 usb_desc_iter_init(sc->sc_udev, &iter);
511 while ((desc = usb_desc_iter_next(&iter)) != NULL) {
512 /* print out all descriptors */
513 printf("uvideo_attach: ");
514 print_descriptor(desc);
515 }
516 #endif /* !UVIDEO_DEBUG */
517
518 /* iterate through interface descriptors and initialize softc */
519 usb_desc_iter_init(sc->sc_udev, &iter);
520 while ((ifdesc = usb_desc_iter_next_interface(&iter)) != NULL) {
521 if (ifdesc->bLength < USB_INTERFACE_DESCRIPTOR_SIZE) {
522 DPRINTFN(50, ("uvideo_attach: "
523 "ignoring incorrect descriptor len=%d\n",
524 ifdesc->bLength));
525 continue;
526 }
527 if (ifdesc->bInterfaceClass != UICLASS_VIDEO) {
528 DPRINTFN(50, ("uvideo_attach: "
529 "ignoring non-uvc interface: "
530 "len=%d type=0x%02x "
531 "class=0x%02x subclass=0x%02x\n",
532 ifdesc->bLength,
533 ifdesc->bDescriptorType,
534 ifdesc->bInterfaceClass,
535 ifdesc->bInterfaceSubClass));
536 continue;
537 }
538
539 switch (ifdesc->bInterfaceSubClass) {
540 case UISUBCLASS_VIDEOCONTROL:
541 err = uvideo_init_control(sc, ifdesc, &iter);
542 if (err != USBD_NORMAL_COMPLETION) {
543 DPRINTF(("uvideo_attach: error with interface "
544 "%d, VideoControl, "
545 "descriptor len=%d type=0x%02x: "
546 "%s (%d)\n",
547 ifdesc->bInterfaceNumber,
548 ifdesc->bLength,
549 ifdesc->bDescriptorType,
550 usbd_errstr(err), err));
551 }
552 break;
553 case UISUBCLASS_VIDEOSTREAMING:
554 vs = uvideo_find_stream(sc, ifdesc->bInterfaceNumber);
555 if (vs == NULL) {
556 vs = uvideo_stream_alloc();
557 err = uvideo_stream_init(vs, sc, ifdesc);
558 if (err != USBD_NORMAL_COMPLETION) {
559 DPRINTF(("uvideo_attach: "
560 "error initializing stream: "
561 "%s (%d)\n",
562 usbd_errstr(err), err));
563 goto bad;
564 }
565 }
566 err = uvideo_stream_init_desc(vs, ifdesc, &iter);
567 if (err != USBD_NORMAL_COMPLETION) {
568 DPRINTF(("uvideo_attach: "
569 "error initializing stream descriptor: "
570 "%s (%d)\n",
571 usbd_errstr(err), err));
572 goto bad;
573 }
574 break;
575 case UISUBCLASS_VIDEOCOLLECTION:
576 err = uvideo_init_collection(sc, ifdesc, &iter);
577 if (err != USBD_NORMAL_COMPLETION) {
578 DPRINTF(("uvideo_attach: error with interface "
579 "%d, VideoCollection, "
580 "descriptor len=%d type=0x%02x: "
581 "%s (%d)\n",
582 ifdesc->bInterfaceNumber,
583 ifdesc->bLength,
584 ifdesc->bDescriptorType,
585 usbd_errstr(err), err));
586 goto bad;
587 }
588 break;
589 default:
590 DPRINTF(("uvideo_attach: unknown UICLASS_VIDEO "
591 "subclass=0x%02x\n",
592 ifdesc->bInterfaceSubClass));
593 break;
594 }
595
596 }
597
598
599 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
600
601 if (!pmf_device_register(self, NULL, NULL))
602 aprint_error_dev(self, "couldn't establish power handler\n");
603
604 SLIST_FOREACH(vs, &sc->sc_stream_list, entries) {
605 /* XXX initialization of vs_videodev is racy */
606 vs->vs_videodev = video_attach_mi(&uvideo_hw_if, sc->sc_dev,
607 vs);
608 }
609
610 return;
611
612 bad:
613 if (err != USBD_NORMAL_COMPLETION) {
614 DPRINTF(("uvideo_attach: error: %s (%d)\n",
615 usbd_errstr(err), err));
616 }
617 return;
618 }
619
620
621 static int
622 uvideo_activate(device_t self, enum devact act)
623 {
624 struct uvideo_softc *sc = device_private(self);
625
626 switch (act) {
627 case DVACT_DEACTIVATE:
628 DPRINTF(("uvideo_activate: deactivating\n"));
629 sc->sc_dying = 1;
630 return 0;
631 default:
632 return EOPNOTSUPP;
633 }
634 }
635
636
637 /* Detach child (video interface) */
638 static void
639 uvideo_childdet(device_t self, device_t child)
640 {
641 struct uvideo_softc *sc = device_private(self);
642 struct uvideo_stream *vs;
643
644 SLIST_FOREACH(vs, &sc->sc_stream_list, entries) {
645 if (child == vs->vs_videodev) {
646 vs->vs_videodev = NULL;
647 break;
648 }
649 }
650 KASSERTMSG(vs != NULL, "unknown child of %s detached: %s @ %p",
651 device_xname(self), device_xname(child), child);
652 }
653
654
655 static int
656 uvideo_detach(device_t self, int flags)
657 {
658 struct uvideo_softc *sc = device_private(self);
659 struct uvideo_stream *vs;
660 int error;
661
662 error = config_detach_children(self, flags);
663 if (error)
664 return error;
665
666 sc->sc_dying = 1;
667
668 pmf_device_deregister(self);
669
670 /* TODO: close the device if it is currently opened? Or will
671 * close be called automatically? */
672
673 while (!SLIST_EMPTY(&sc->sc_stream_list)) {
674 vs = SLIST_FIRST(&sc->sc_stream_list);
675 SLIST_REMOVE_HEAD(&sc->sc_stream_list, entries);
676 uvideo_stream_stop_xfer(vs);
677 uvideo_stream_free(vs);
678 }
679
680 #if 0
681 /* Wait for outstanding request to complete. TODO: what is
682 * appropriate here? */
683 usbd_delay_ms(sc->sc_udev, 1000);
684 #endif
685
686 DPRINTFN(15, ("uvideo: detaching from %s\n",
687 device_xname(sc->sc_dev)));
688
689 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
690
691 usbd_devinfo_free(sc->sc_devname);
692
693 return 0;
694 }
695
696 /* Search the stream list for a stream matching the interface number.
697 * This is an O(n) search, but most devices should have only one or at
698 * most two streams. */
699 static struct uvideo_stream *
700 uvideo_find_stream(struct uvideo_softc *sc, uint8_t ifaceno)
701 {
702 struct uvideo_stream *vs;
703
704 SLIST_FOREACH(vs, &sc->sc_stream_list, entries) {
705 if (vs->vs_ifaceno == ifaceno)
706 return vs;
707 }
708
709 return NULL;
710 }
711
712 /* Search the format list for the given format and frame index. This
713 * might be improved through indexing, but the format and frame count
714 * is unknown ahead of time (only after iterating through the
715 * usb device descriptors). */
716 #if 0
717 static struct uvideo_format *
718 uvideo_stream_find_format(struct uvideo_stream *vs,
719 uint8_t format_index, uint8_t frame_index)
720 {
721 struct uvideo_format *format;
722
723 SIMPLEQ_FOREACH(format, &vs->vs_formats, entries) {
724 if (UVIDEO_FORMAT_GET_FORMAT_INDEX(format) == format_index &&
725 UVIDEO_FORMAT_GET_FRAME_INDEX(format) == frame_index)
726 return format;
727 }
728 return NULL;
729 }
730 #endif
731
732 static struct uvideo_format *
733 uvideo_stream_guess_format(struct uvideo_stream *vs,
734 enum video_pixel_format pixel_format,
735 uint32_t width, uint32_t height)
736 {
737 struct uvideo_format *format, *gformat = NULL;
738
739 SIMPLEQ_FOREACH(format, &vs->vs_formats, entries) {
740 if (format->format.pixel_format != pixel_format)
741 continue;
742 if (format->format.width <= width &&
743 format->format.height <= height) {
744 if (gformat == NULL ||
745 (gformat->format.width < format->format.width &&
746 gformat->format.height < format->format.height))
747 gformat = format;
748 }
749 }
750
751 return gformat;
752 }
753
754 static struct uvideo_stream *
755 uvideo_stream_alloc(void)
756 {
757 return kmem_zalloc(sizeof(struct uvideo_stream), KM_SLEEP);
758 }
759
760
761 static usbd_status
762 uvideo_init_control(struct uvideo_softc *sc,
763 const usb_interface_descriptor_t *ifdesc,
764 usbd_desc_iter_t *iter)
765 {
766 const usb_descriptor_t *desc;
767 const uvideo_descriptor_t *uvdesc;
768 usbd_desc_iter_t orig;
769 uint8_t i, j, nunits;
770
771 /* save original iterator state */
772 memcpy(&orig, iter, sizeof(orig));
773
774 /* count number of units and terminals */
775 nunits = 0;
776 while ((desc = usb_desc_iter_next_non_interface(iter)) != NULL) {
777 uvdesc = (const uvideo_descriptor_t *)desc;
778
779 if (uvdesc->bDescriptorType != UDESC_CS_INTERFACE)
780 continue;
781 if (uvdesc->bDescriptorSubtype < UDESC_INPUT_TERMINAL ||
782 uvdesc->bDescriptorSubtype > UDESC_EXTENSION_UNIT)
783 continue;
784 ++nunits;
785 }
786
787 if (nunits == 0) {
788 DPRINTF(("uvideo_init_control: no units\n"));
789 return USBD_NORMAL_COMPLETION;
790 }
791
792 i = 0;
793
794 /* allocate space for units */
795 sc->sc_nunits = nunits;
796 sc->sc_unit = kmem_alloc(sizeof(*sc->sc_unit) * nunits, KM_SLEEP);
797
798 /* restore original iterator state */
799 memcpy(iter, &orig, sizeof(orig));
800
801 /* iterate again, initializing the units */
802 while ((desc = usb_desc_iter_next_non_interface(iter)) != NULL) {
803 uvdesc = (const uvideo_descriptor_t *)desc;
804
805 if (uvdesc->bDescriptorType != UDESC_CS_INTERFACE)
806 continue;
807 if (uvdesc->bDescriptorSubtype < UDESC_INPUT_TERMINAL ||
808 uvdesc->bDescriptorSubtype > UDESC_EXTENSION_UNIT)
809 continue;
810
811 sc->sc_unit[i] = uvideo_unit_alloc(uvdesc);
812 /* TODO: free other units before returning? */
813 if (sc->sc_unit[i] == NULL)
814 goto enomem;
815 ++i;
816 }
817
818 return USBD_NORMAL_COMPLETION;
819
820 enomem:
821 if (sc->sc_unit != NULL) {
822 for (j = 0; j < i; ++j) {
823 uvideo_unit_free(sc->sc_unit[j]);
824 sc->sc_unit[j] = NULL;
825 }
826 kmem_free(sc->sc_unit, sizeof(*sc->sc_unit) * nunits);
827 sc->sc_unit = NULL;
828 }
829 sc->sc_nunits = 0;
830
831 return USBD_NOMEM;
832 }
833
834 static usbd_status
835 uvideo_init_collection(struct uvideo_softc *sc,
836 const usb_interface_descriptor_t *ifdesc,
837 usbd_desc_iter_t *iter)
838 {
839 DPRINTF(("uvideo: ignoring Video Collection\n"));
840 return USBD_NORMAL_COMPLETION;
841 }
842
843 /* Allocates space for and initializes a uvideo unit based on the
844 * given descriptor. Returns NULL with bad descriptor or ENOMEM. */
845 static struct uvideo_unit *
846 uvideo_unit_alloc(const uvideo_descriptor_t *desc)
847 {
848 struct uvideo_unit *vu;
849 usbd_status err;
850
851 if (desc->bDescriptorType != UDESC_CS_INTERFACE)
852 return NULL;
853
854 vu = kmem_zalloc(sizeof(*vu), KM_SLEEP);
855 err = uvideo_unit_init(vu, desc);
856 if (err != USBD_NORMAL_COMPLETION) {
857 DPRINTF(("uvideo_unit_alloc: error initializing unit: "
858 "%s (%d)\n", usbd_errstr(err), err));
859 kmem_free(vu, sizeof(*vu));
860 return NULL;
861 }
862
863 return vu;
864 }
865
866 static usbd_status
867 uvideo_unit_init(struct uvideo_unit *vu, const uvideo_descriptor_t *desc)
868 {
869 struct uvideo_camera_terminal *ct;
870 struct uvideo_processing_unit *pu;
871
872 const uvideo_input_terminal_descriptor_t *input;
873 const uvideo_camera_terminal_descriptor_t *camera;
874 const uvideo_selector_unit_descriptor_t *selector;
875 const uvideo_processing_unit_descriptor_t *processing;
876 const uvideo_extension_unit_descriptor_t *extension;
877
878 switch (desc->bDescriptorSubtype) {
879 case UDESC_INPUT_TERMINAL:
880 if (desc->bLength < sizeof(*input))
881 return USBD_INVAL;
882 input = (const uvideo_input_terminal_descriptor_t *)desc;
883 switch (UGETW(input->wTerminalType)) {
884 case UVIDEO_ITT_CAMERA:
885 if (desc->bLength < sizeof(*camera))
886 return USBD_INVAL;
887 camera =
888 (const uvideo_camera_terminal_descriptor_t *)desc;
889
890 ct = &vu->u.vu_camera;
891 ct->ct_objective_focal_min =
892 UGETW(camera->wObjectiveFocalLengthMin);
893 ct->ct_objective_focal_max =
894 UGETW(camera->wObjectiveFocalLengthMax);
895 ct->ct_ocular_focal_length =
896 UGETW(camera->wOcularFocalLength);
897
898 uvideo_unit_alloc_controls(vu, camera->bControlSize,
899 camera->bmControls);
900 break;
901 default:
902 DPRINTF(("uvideo_unit_init: "
903 "unknown input terminal type 0x%04x\n",
904 UGETW(input->wTerminalType)));
905 return USBD_INVAL;
906 }
907 break;
908 case UDESC_OUTPUT_TERMINAL:
909 break;
910 case UDESC_SELECTOR_UNIT:
911 if (desc->bLength < sizeof(*selector))
912 return USBD_INVAL;
913 selector = (const uvideo_selector_unit_descriptor_t *)desc;
914
915 uvideo_unit_alloc_sources(vu, selector->bNrInPins,
916 selector->baSourceID);
917 break;
918 case UDESC_PROCESSING_UNIT:
919 if (desc->bLength < sizeof(*processing))
920 return USBD_INVAL;
921 processing = (const uvideo_processing_unit_descriptor_t *)desc;
922 pu = &vu->u.vu_processing;
923
924 pu->pu_video_standards = PU_GET_VIDEO_STANDARDS(processing);
925 pu->pu_max_multiplier = UGETW(processing->wMaxMultiplier);
926
927 uvideo_unit_alloc_sources(vu, 1, &processing->bSourceID);
928 uvideo_unit_alloc_controls(vu, processing->bControlSize,
929 processing->bmControls);
930 break;
931 case UDESC_EXTENSION_UNIT:
932 if (desc->bLength < sizeof(*extension))
933 return USBD_INVAL;
934 extension = (const uvideo_extension_unit_descriptor_t *)desc;
935 /* TODO: copy guid */
936
937 uvideo_unit_alloc_sources(vu, extension->bNrInPins,
938 extension->baSourceID);
939 uvideo_unit_alloc_controls(vu, XU_GET_CONTROL_SIZE(extension),
940 XU_GET_CONTROLS(extension));
941 break;
942 default:
943 DPRINTF(("uvideo_unit_alloc: unknown descriptor "
944 "type=0x%02x subtype=0x%02x\n",
945 desc->bDescriptorType, desc->bDescriptorSubtype));
946 return USBD_INVAL;
947 }
948
949 return USBD_NORMAL_COMPLETION;
950 }
951
952 static void
953 uvideo_unit_free(struct uvideo_unit *vu)
954 {
955 uvideo_unit_free_sources(vu);
956 uvideo_unit_free_controls(vu);
957 kmem_free(vu, sizeof(*vu));
958 }
959
960 static usbd_status
961 uvideo_unit_alloc_sources(struct uvideo_unit *vu,
962 uint8_t nsrcs, const uint8_t *src_ids)
963 {
964 vu->vu_nsrcs = nsrcs;
965
966 if (nsrcs == 0) {
967 /* do nothing */
968 } else if (nsrcs == 1) {
969 vu->s.vu_src_id = src_ids[0];
970 } else {
971 vu->s.vu_src_id_ary =
972 kmem_alloc(sizeof(*vu->s.vu_src_id_ary) * nsrcs, KM_SLEEP);
973 memcpy(vu->s.vu_src_id_ary, src_ids, nsrcs);
974 }
975
976 return USBD_NORMAL_COMPLETION;
977 }
978
979 static void
980 uvideo_unit_free_sources(struct uvideo_unit *vu)
981 {
982 if (vu->vu_nsrcs == 1)
983 return;
984
985 kmem_free(vu->s.vu_src_id_ary,
986 sizeof(*vu->s.vu_src_id_ary) * vu->vu_nsrcs);
987 vu->vu_nsrcs = 0;
988 vu->s.vu_src_id_ary = NULL;
989 }
990
991 static usbd_status
992 uvideo_unit_alloc_controls(struct uvideo_unit *vu, uint8_t size,
993 const uint8_t *controls)
994 {
995 if (size == 0)
996 return USBD_INVAL;
997
998 vu->vu_controls = kmem_alloc(sizeof(*vu->vu_controls) * size, KM_SLEEP);
999 vu->vu_control_size = size;
1000 memcpy(vu->vu_controls, controls, size);
1001
1002 return USBD_NORMAL_COMPLETION;
1003 }
1004
1005 static void
1006 uvideo_unit_free_controls(struct uvideo_unit *vu)
1007 {
1008 kmem_free(vu->vu_controls,
1009 sizeof(*vu->vu_controls) * vu->vu_control_size);
1010 vu->vu_controls = NULL;
1011 vu->vu_control_size = 0;
1012 }
1013
1014
1015 /* Initialize a stream from a Video Streaming interface
1016 * descriptor. Adds the stream to the stream_list in uvideo_softc.
1017 * This should be called once for new streams, and
1018 * uvideo_stream_init_desc() should then be called for this and each
1019 * additional interface with the same interface number. */
1020 static usbd_status
1021 uvideo_stream_init(struct uvideo_stream *vs,
1022 struct uvideo_softc *sc,
1023 const usb_interface_descriptor_t *ifdesc)
1024 {
1025 uWord len;
1026 usbd_status err;
1027
1028 DPRINTF(("%s: %s ifaceno=%d vs=%p\n", __func__,
1029 device_xname(sc->sc_dev),
1030 ifdesc->bInterfaceNumber,
1031 vs));
1032
1033 SLIST_INSERT_HEAD(&sc->sc_stream_list, vs, entries);
1034 vs->vs_parent = sc;
1035 vs->vs_ifaceno = ifdesc->bInterfaceNumber;
1036 vs->vs_subtype = 0;
1037 SIMPLEQ_INIT(&vs->vs_formats);
1038 SIMPLEQ_INIT(&vs->vs_pixel_formats);
1039 vs->vs_default_format = NULL;
1040 vs->vs_current_format.priv = -1;
1041 vs->vs_xfer_type = 0;
1042 vs->vs_state = UVIDEO_STATE_CLOSED;
1043
1044 err = usbd_device2interface_handle(sc->sc_udev, vs->vs_ifaceno,
1045 &vs->vs_iface);
1046 if (err != USBD_NORMAL_COMPLETION) {
1047 DPRINTF(("uvideo_stream_init: "
1048 "error getting vs interface: "
1049 "%s (%d)\n",
1050 usbd_errstr(err), err));
1051 return err;
1052 }
1053
1054 /* For Xbox Live Vision camera, linux-uvc folk say we need to
1055 * set an alternate interface and wait ~3 seconds prior to
1056 * doing the format probe/commit. We set to alternate
1057 * interface 0, which is the default, zero bandwidth
1058 * interface. This should not have adverse affects on other
1059 * cameras. Errors are ignored. */
1060 err = usbd_set_interface(vs->vs_iface, 0);
1061 if (err != USBD_NORMAL_COMPLETION) {
1062 DPRINTF(("uvideo_stream_init: error setting alt interface: "
1063 "%s (%d)\n",
1064 usbd_errstr(err), err));
1065 }
1066
1067 /* Initialize probe and commit data size. This value is
1068 * dependent on the version of the spec the hardware
1069 * implements. */
1070 err = uvideo_stream_probe(vs, UR_GET_LEN, &len);
1071 if (err != USBD_NORMAL_COMPLETION) {
1072 DPRINTF(("uvideo_stream_init: "
1073 "error getting probe data len: "
1074 "%s (%d)\n",
1075 usbd_errstr(err), err));
1076 vs->vs_probelen = 26; /* conservative v1.0 length */
1077 } else if (UGETW(len) <= sizeof(uvideo_probe_and_commit_data_t)) {
1078 DPRINTFN(15,("uvideo_stream_init: probelen=%d\n", UGETW(len)));
1079 vs->vs_probelen = UGETW(len);
1080 } else {
1081 DPRINTFN(15,("uvideo_stream_init: device returned invalid probe"
1082 " len %d, using default\n", UGETW(len)));
1083 vs->vs_probelen = 26;
1084 }
1085
1086 return USBD_NORMAL_COMPLETION;
1087 }
1088
1089 /* Further stream initialization based on a Video Streaming interface
1090 * descriptor and following descriptors belonging to that interface.
1091 * Iterates through all descriptors belonging to this particular
1092 * interface descriptor, modifying the iterator. This may be called
1093 * multiple times because there may be several alternate interfaces
1094 * associated with the same interface number. */
1095 /*
1096 * XXX XXX XXX: This function accesses descriptors in an unsafe manner.
1097 */
1098 static usbd_status
1099 uvideo_stream_init_desc(struct uvideo_stream *vs,
1100 const usb_interface_descriptor_t *ifdesc,
1101 usbd_desc_iter_t *iter)
1102 {
1103 const usb_descriptor_t *desc;
1104 const uvideo_descriptor_t *uvdesc;
1105 struct uvideo_bulk_xfer *bx;
1106 struct uvideo_isoc_xfer *ix;
1107 struct uvideo_alternate *alt;
1108 uint8_t xfer_type, xfer_dir;
1109 uint8_t bmAttributes, bEndpointAddress;
1110 int i;
1111
1112 DPRINTF(("%s: bInterfaceNumber=%d bAlternateSetting=%d\n", __func__,
1113 ifdesc->bInterfaceNumber, ifdesc->bAlternateSetting));
1114
1115 /* Iterate until the next interface descriptor. All
1116 * descriptors until then belong to this streaming
1117 * interface. */
1118 while ((desc = usb_desc_iter_next_non_interface(iter)) != NULL) {
1119 uvdesc = (const uvideo_descriptor_t *)desc;
1120
1121 switch (uvdesc->bDescriptorType) {
1122 case UDESC_ENDPOINT:
1123 bmAttributes = GET(usb_endpoint_descriptor_t,
1124 desc, bmAttributes);
1125 bEndpointAddress = GET(usb_endpoint_descriptor_t,
1126 desc, bEndpointAddress);
1127 xfer_type = UE_GET_XFERTYPE(bmAttributes);
1128 xfer_dir = UE_GET_DIR(bEndpointAddress);
1129 if (xfer_type == UE_BULK && xfer_dir == UE_DIR_IN) {
1130 bx = &vs->vs_xfer.bulk;
1131 if (vs->vs_xfer_type == 0) {
1132 DPRINTFN(15, ("uvideo_attach: "
1133 "BULK stream *\n"));
1134 vs->vs_xfer_type = UE_BULK;
1135 bx->bx_endpt = bEndpointAddress;
1136 DPRINTF(("uvideo_attach: BULK "
1137 "endpoint %x\n",
1138 bx->bx_endpt));
1139 bx->bx_running = false;
1140 cv_init(&bx->bx_cv,
1141 device_xname(vs->vs_parent->sc_dev)
1142 );
1143 mutex_init(&bx->bx_lock,
1144 MUTEX_DEFAULT, IPL_NONE);
1145 }
1146 } else if (xfer_type == UE_ISOCHRONOUS) {
1147 ix = &vs->vs_xfer.isoc;
1148 for (i = 0; i < UVIDEO_NXFERS; i++) {
1149 ix->ix_i[i].i_ix = ix;
1150 ix->ix_i[i].i_vs = vs;
1151 }
1152 if (vs->vs_xfer_type == 0) {
1153 DPRINTFN(15, ("uvideo_attach: "
1154 "ISOC stream *\n"));
1155 SLIST_INIT(&ix->ix_altlist);
1156 vs->vs_xfer_type = UE_ISOCHRONOUS;
1157 ix->ix_endpt =
1158 GET(usb_endpoint_descriptor_t,
1159 desc, bEndpointAddress);
1160 }
1161
1162 alt = kmem_alloc(sizeof(*alt), KM_SLEEP);
1163 alt->altno = ifdesc->bAlternateSetting;
1164 alt->interval =
1165 GET(usb_endpoint_descriptor_t,
1166 desc, bInterval);
1167
1168 alt->max_packet_size =
1169 UE_GET_SIZE(UGETW(GET(usb_endpoint_descriptor_t,
1170 desc, wMaxPacketSize)));
1171 alt->max_packet_size *=
1172 (UE_GET_TRANS(UGETW(GET(
1173 usb_endpoint_descriptor_t, desc,
1174 wMaxPacketSize)))) + 1;
1175
1176 SLIST_INSERT_HEAD(&ix->ix_altlist,
1177 alt, entries);
1178 }
1179 break;
1180 case UDESC_CS_INTERFACE:
1181 if (ifdesc->bAlternateSetting != 0) {
1182 DPRINTF(("uvideo_stream_init_alternate: "
1183 "unexpected class-specific descriptor "
1184 "len=%d type=0x%02x subtype=0x%02x\n",
1185 uvdesc->bLength,
1186 uvdesc->bDescriptorType,
1187 uvdesc->bDescriptorSubtype));
1188 break;
1189 }
1190
1191 switch (uvdesc->bDescriptorSubtype) {
1192 case UDESC_VS_INPUT_HEADER:
1193 vs->vs_subtype = UDESC_VS_INPUT_HEADER;
1194 break;
1195 case UDESC_VS_OUTPUT_HEADER:
1196 /* TODO: handle output stream */
1197 DPRINTF(("uvideo: VS output not implemented\n"));
1198 vs->vs_subtype = UDESC_VS_OUTPUT_HEADER;
1199 return USBD_INVAL;
1200 case UDESC_VS_FORMAT_UNCOMPRESSED:
1201 case UDESC_VS_FORMAT_FRAME_BASED:
1202 case UDESC_VS_FORMAT_MJPEG:
1203 uvideo_stream_init_frame_based_format(vs,
1204 uvdesc,
1205 iter);
1206 break;
1207 case UDESC_VS_FORMAT_MPEG2TS:
1208 case UDESC_VS_FORMAT_DV:
1209 case UDESC_VS_FORMAT_STREAM_BASED:
1210 default:
1211 DPRINTF(("uvideo: unimplemented VS CS "
1212 "descriptor len=%d type=0x%02x "
1213 "subtype=0x%02x\n",
1214 uvdesc->bLength,
1215 uvdesc->bDescriptorType,
1216 uvdesc->bDescriptorSubtype));
1217 break;
1218 }
1219 break;
1220 default:
1221 DPRINTF(("uvideo_stream_init_desc: "
1222 "unknown descriptor "
1223 "len=%d type=0x%02x\n",
1224 uvdesc->bLength,
1225 uvdesc->bDescriptorType));
1226 break;
1227 }
1228 }
1229
1230 DPRINTF(("%s: bInterfaceNumber=%d bAlternateSetting=%d done\n",
1231 __func__,
1232 ifdesc->bInterfaceNumber, ifdesc->bAlternateSetting));
1233
1234 return USBD_NORMAL_COMPLETION;
1235 }
1236
1237 /* Finialize and free memory associated with this stream. */
1238 static void
1239 uvideo_stream_free(struct uvideo_stream *vs)
1240 {
1241 struct uvideo_alternate *alt;
1242 struct uvideo_pixel_format *pixel_format;
1243 struct uvideo_format *format;
1244
1245 /* free linked list of alternate interfaces */
1246 if (vs->vs_xfer_type == UE_ISOCHRONOUS) {
1247 while (!SLIST_EMPTY(&vs->vs_xfer.isoc.ix_altlist)) {
1248 alt = SLIST_FIRST(&vs->vs_xfer.isoc.ix_altlist);
1249 SLIST_REMOVE_HEAD(&vs->vs_xfer.isoc.ix_altlist,
1250 entries);
1251 kmem_free(alt, sizeof(*alt));
1252 }
1253 }
1254
1255 /* free linked-list of formats and pixel formats */
1256 while ((format = SIMPLEQ_FIRST(&vs->vs_formats)) != NULL) {
1257 SIMPLEQ_REMOVE_HEAD(&vs->vs_formats, entries);
1258 kmem_free(format, sizeof(struct uvideo_format));
1259 }
1260 while ((pixel_format = SIMPLEQ_FIRST(&vs->vs_pixel_formats)) != NULL) {
1261 SIMPLEQ_REMOVE_HEAD(&vs->vs_pixel_formats, entries);
1262 kmem_free(pixel_format, sizeof(struct uvideo_pixel_format));
1263 }
1264
1265 kmem_free(vs, sizeof(*vs));
1266 }
1267
1268
1269 static usbd_status
1270 uvideo_stream_init_frame_based_format(struct uvideo_stream *vs,
1271 const uvideo_descriptor_t *format_desc,
1272 usbd_desc_iter_t *iter)
1273 {
1274 struct uvideo_pixel_format *pformat, *pfiter;
1275 enum video_pixel_format pixel_format;
1276 struct uvideo_format *format;
1277 const uvideo_descriptor_t *uvdesc;
1278 uint8_t subtype, default_index, index;
1279 uint32_t frame_interval;
1280 const usb_guid_t *guid;
1281
1282 DPRINTF(("%s: ifaceno=%d subtype=%d probelen=%d\n", __func__,
1283 vs->vs_ifaceno, vs->vs_subtype, vs->vs_probelen));
1284
1285 pixel_format = VIDEO_FORMAT_UNDEFINED;
1286
1287 switch (format_desc->bDescriptorSubtype) {
1288 case UDESC_VS_FORMAT_UNCOMPRESSED:
1289 DPRINTF(("%s: uncompressed\n", __func__));
1290 subtype = UDESC_VS_FRAME_UNCOMPRESSED;
1291 default_index = GET(uvideo_vs_format_uncompressed_descriptor_t,
1292 format_desc,
1293 bDefaultFrameIndex);
1294 guid = GETP(uvideo_vs_format_uncompressed_descriptor_t,
1295 format_desc,
1296 guidFormat);
1297 if (usb_guid_cmp(guid, &uvideo_guid_format_yuy2) == 0)
1298 pixel_format = VIDEO_FORMAT_YUY2;
1299 else if (usb_guid_cmp(guid, &uvideo_guid_format_nv12) == 0)
1300 pixel_format = VIDEO_FORMAT_NV12;
1301 else if (usb_guid_cmp(guid, &uvideo_guid_format_uyvy) == 0)
1302 pixel_format = VIDEO_FORMAT_UYVY;
1303 else {
1304 #ifdef UVIDEO_DEBUG
1305 DPRINTF(("%s: unknown format: ", __func__));
1306 usb_guid_print(guid);
1307 DPRINTF(("\n"));
1308 #endif
1309 }
1310 break;
1311 case UDESC_VS_FORMAT_FRAME_BASED:
1312 DPRINTF(("%s: frame-based\n", __func__));
1313 subtype = UDESC_VS_FRAME_FRAME_BASED;
1314 default_index = GET(uvideo_format_frame_based_descriptor_t,
1315 format_desc,
1316 bDefaultFrameIndex);
1317 break;
1318 case UDESC_VS_FORMAT_MJPEG:
1319 DPRINTF(("%s: mjpeg\n", __func__));
1320 subtype = UDESC_VS_FRAME_MJPEG;
1321 default_index = GET(uvideo_vs_format_mjpeg_descriptor_t,
1322 format_desc,
1323 bDefaultFrameIndex);
1324 pixel_format = VIDEO_FORMAT_MJPEG;
1325 break;
1326 default:
1327 DPRINTF(("uvideo: unknown frame based format %d\n",
1328 format_desc->bDescriptorSubtype));
1329 return USBD_INVAL;
1330 }
1331
1332 pformat = NULL;
1333 SIMPLEQ_FOREACH(pfiter, &vs->vs_pixel_formats, entries) {
1334 if (pfiter->pixel_format == pixel_format) {
1335 pformat = pfiter;
1336 break;
1337 }
1338 }
1339 if (pixel_format != VIDEO_FORMAT_UNDEFINED && pformat == NULL) {
1340 pformat = kmem_zalloc(sizeof(*pformat), KM_SLEEP);
1341 pformat->pixel_format = pixel_format;
1342 DPRINTF(("uvideo: Adding pixel format %d\n",
1343 pixel_format));
1344 SIMPLEQ_INSERT_TAIL(&vs->vs_pixel_formats,
1345 pformat, entries);
1346 }
1347
1348 /* Iterate through frame descriptors directly following the
1349 * format descriptor, and add a format to the format list for
1350 * each frame descriptor. */
1351 while ((uvdesc = (const uvideo_descriptor_t *)usb_desc_iter_peek(iter)) &&
1352 (uvdesc != NULL) && (uvdesc->bDescriptorSubtype == subtype))
1353 {
1354 uvdesc = (const uvideo_descriptor_t *) usb_desc_iter_next(iter);
1355
1356 format = kmem_zalloc(sizeof(struct uvideo_format), KM_SLEEP);
1357 format->format.pixel_format = pixel_format;
1358
1359 switch (format_desc->bDescriptorSubtype) {
1360 case UDESC_VS_FORMAT_UNCOMPRESSED:
1361 #ifdef UVIDEO_DEBUG
1362 if (pixel_format == VIDEO_FORMAT_UNDEFINED &&
1363 uvideodebug) {
1364 guid = GETP(
1365 uvideo_vs_format_uncompressed_descriptor_t,
1366 format_desc,
1367 guidFormat);
1368
1369 DPRINTF(("uvideo: format undefined "));
1370 usb_guid_print(guid);
1371 DPRINTF(("\n"));
1372 }
1373 #endif
1374
1375 UVIDEO_FORMAT_INIT_FRAME_BASED(
1376 uvideo_vs_format_uncompressed_descriptor_t,
1377 format_desc,
1378 uvideo_vs_frame_uncompressed_descriptor_t,
1379 uvdesc,
1380 format);
1381 format->format.sample_size =
1382 UGETDW(
1383 GET(uvideo_vs_frame_uncompressed_descriptor_t,
1384 uvdesc, dwMaxVideoFrameBufferSize));
1385 format->format.stride =
1386 format->format.sample_size / format->format.height;
1387 index = GET(uvideo_vs_frame_uncompressed_descriptor_t,
1388 uvdesc,
1389 bFrameIndex);
1390 frame_interval =
1391 UGETDW(
1392 GET(uvideo_vs_frame_uncompressed_descriptor_t,
1393 uvdesc,
1394 dwDefaultFrameInterval));
1395 break;
1396 case UDESC_VS_FORMAT_MJPEG:
1397 UVIDEO_FORMAT_INIT_FRAME_BASED(
1398 uvideo_vs_format_mjpeg_descriptor_t,
1399 format_desc,
1400 uvideo_vs_frame_mjpeg_descriptor_t,
1401 uvdesc,
1402 format);
1403 format->format.sample_size =
1404 UGETDW(
1405 GET(uvideo_vs_frame_mjpeg_descriptor_t,
1406 uvdesc, dwMaxVideoFrameBufferSize));
1407 format->format.stride =
1408 format->format.sample_size / format->format.height;
1409 index = GET(uvideo_vs_frame_mjpeg_descriptor_t,
1410 uvdesc,
1411 bFrameIndex);
1412 frame_interval =
1413 UGETDW(
1414 GET(uvideo_vs_frame_mjpeg_descriptor_t,
1415 uvdesc,
1416 dwDefaultFrameInterval));
1417 break;
1418 case UDESC_VS_FORMAT_FRAME_BASED:
1419 format->format.pixel_format = VIDEO_FORMAT_UNDEFINED;
1420 UVIDEO_FORMAT_INIT_FRAME_BASED(
1421 uvideo_format_frame_based_descriptor_t,
1422 format_desc,
1423 uvideo_frame_frame_based_descriptor_t,
1424 uvdesc,
1425 format);
1426 index = GET(uvideo_frame_frame_based_descriptor_t,
1427 uvdesc,
1428 bFrameIndex);
1429 format->format.stride =
1430 UGETDW(
1431 GET(uvideo_frame_frame_based_descriptor_t,
1432 uvdesc, dwBytesPerLine));
1433 format->format.sample_size =
1434 format->format.stride * format->format.height;
1435 frame_interval =
1436 UGETDW(
1437 GET(uvideo_frame_frame_based_descriptor_t,
1438 uvdesc, dwDefaultFrameInterval));
1439 break;
1440 default:
1441 /* shouldn't ever get here */
1442 DPRINTF(("uvideo: unknown frame based format %d\n",
1443 format_desc->bDescriptorSubtype));
1444 kmem_free(format, sizeof(struct uvideo_format));
1445 return USBD_INVAL;
1446 }
1447
1448 DPRINTF(("uvideo: found format (index %d) type %d "
1449 "size %ux%u size %u stride %u interval %u\n",
1450 index, format->format.pixel_format, format->format.width,
1451 format->format.height, format->format.sample_size,
1452 format->format.stride, frame_interval));
1453
1454 SIMPLEQ_INSERT_TAIL(&vs->vs_formats, format, entries);
1455
1456 if (vs->vs_default_format == NULL && index == default_index
1457 #ifdef UVIDEO_DISABLE_MJPEG
1458 && subtype != UDESC_VS_FRAME_MJPEG
1459 #endif
1460 ) {
1461 DPRINTF((" ^ picking this one\n"));
1462 vs->vs_default_format = &format->format;
1463 vs->vs_frame_interval = frame_interval;
1464 }
1465
1466 }
1467
1468 return USBD_NORMAL_COMPLETION;
1469 }
1470
1471 static int
1472 uvideo_stream_start_xfer(struct uvideo_stream *vs)
1473 {
1474 struct uvideo_softc *sc = vs->vs_parent;
1475 struct uvideo_bulk_xfer *bx;
1476 struct uvideo_isoc_xfer *ix;
1477 uint32_t vframe_len; /* rough bytes per video frame */
1478 uint32_t uframe_len; /* bytes per usb frame (TODO: or microframe?) */
1479 uint32_t nframes; /* number of usb frames (TODO: or microframs?) */
1480 int i, ret;
1481 int error;
1482
1483 struct uvideo_alternate *alt, *alt_maybe;
1484 usbd_status err;
1485
1486 switch (vs->vs_xfer_type) {
1487 case UE_BULK:
1488 ret = 0;
1489 bx = &vs->vs_xfer.bulk;
1490
1491 err = usbd_open_pipe(vs->vs_iface, bx->bx_endpt, 0,
1492 &bx->bx_pipe);
1493 if (err != USBD_NORMAL_COMPLETION) {
1494 DPRINTF(("uvideo: error opening pipe: %s (%d)\n",
1495 usbd_errstr(err), err));
1496 return EIO;
1497 }
1498 DPRINTF(("uvideo: pipe %p\n", bx->bx_pipe));
1499
1500 error = usbd_create_xfer(bx->bx_pipe, vs->vs_max_payload_size,
1501 0, 0, &bx->bx_xfer);
1502 if (error) {
1503 DPRINTF(("uvideo: couldn't allocate xfer\n"));
1504 return error;
1505 }
1506 DPRINTF(("uvideo: xfer %p\n", bx->bx_xfer));
1507
1508 bx->bx_buflen = vs->vs_max_payload_size;
1509 bx->bx_buffer = usbd_get_buffer(bx->bx_xfer);
1510
1511 mutex_enter(&bx->bx_lock);
1512 if (bx->bx_running == false) {
1513 bx->bx_running = true;
1514 ret = kthread_create(PRI_UVIDEO, 0, NULL,
1515 uvideo_stream_recv_bulk_transfer, vs,
1516 NULL, "%s", device_xname(sc->sc_dev));
1517 if (ret) {
1518 DPRINTF(("uvideo: couldn't create kthread:"
1519 " %d\n", err));
1520 bx->bx_running = false;
1521 mutex_exit(&bx->bx_lock);
1522 return err;
1523 }
1524 } else
1525 aprint_error_dev(sc->sc_dev,
1526 "transfer already in progress\n");
1527 mutex_exit(&bx->bx_lock);
1528
1529 DPRINTF(("uvideo: thread created\n"));
1530
1531 return 0;
1532 case UE_ISOCHRONOUS:
1533 ix = &vs->vs_xfer.isoc;
1534
1535 /* Choose an alternate interface most suitable for
1536 * this format. Choose the smallest size that can
1537 * contain max_payload_size.
1538 *
1539 * It is assumed that the list is sorted in descending
1540 * order from largest to smallest packet size.
1541 *
1542 * TODO: what should the strategy be for choosing an
1543 * alt interface?
1544 */
1545 alt = NULL;
1546 SLIST_FOREACH(alt_maybe, &ix->ix_altlist, entries) {
1547 /* TODO: define "packet" and "payload". I think
1548 * several packets can make up one payload which would
1549 * call into question this method of selecting an
1550 * alternate interface... */
1551
1552 if (alt_maybe->max_packet_size > vs->vs_max_payload_size)
1553 continue;
1554
1555 if (alt == NULL ||
1556 alt_maybe->max_packet_size >= alt->max_packet_size)
1557 alt = alt_maybe;
1558 }
1559
1560 if (alt == NULL) {
1561 DPRINTF(("uvideo_stream_start_xfer: "
1562 "no suitable alternate interface found\n"));
1563 return EINVAL;
1564 }
1565
1566 DPRINTFN(15,("uvideo_stream_start_xfer: "
1567 "choosing alternate interface "
1568 "%d wMaxPacketSize=%d bInterval=%d\n",
1569 alt->altno, alt->max_packet_size, alt->interval));
1570
1571 err = usbd_set_interface(vs->vs_iface, alt->altno);
1572 if (err != USBD_NORMAL_COMPLETION) {
1573 DPRINTF(("uvideo_stream_start_xfer: "
1574 "error setting alt interface: %s (%d)\n",
1575 usbd_errstr(err), err));
1576 return EIO;
1577 }
1578
1579 /* TODO: "packet" not same as frame */
1580 vframe_len = vs->vs_current_format.sample_size;
1581 uframe_len = alt->max_packet_size;
1582 nframes = (vframe_len + uframe_len - 1) / uframe_len;
1583 nframes = (nframes + 7) & ~7; /*round up for ehci inefficiency*/
1584 nframes = uimin(UVIDEO_NFRAMES_MAX, nframes);
1585 DPRINTF(("uvideo_stream_start_xfer: nframes=%d\n", nframes));
1586
1587 ix->ix_nframes = nframes;
1588 ix->ix_uframe_len = uframe_len;
1589 for (i = 0; i < UVIDEO_NXFERS; i++) {
1590 struct uvideo_isoc *isoc = &ix->ix_i[i];
1591 isoc->i_frlengths =
1592 kmem_alloc(sizeof(isoc->i_frlengths[0]) * nframes,
1593 KM_SLEEP);
1594 }
1595
1596 err = usbd_open_pipe(vs->vs_iface, ix->ix_endpt,
1597 USBD_EXCLUSIVE_USE, &ix->ix_pipe);
1598 if (err != USBD_NORMAL_COMPLETION) {
1599 DPRINTF(("uvideo: error opening pipe: %s (%d)\n",
1600 usbd_errstr(err), err));
1601 return EIO;
1602 }
1603
1604 for (i = 0; i < UVIDEO_NXFERS; i++) {
1605 struct uvideo_isoc *isoc = &ix->ix_i[i];
1606 error = usbd_create_xfer(ix->ix_pipe,
1607 nframes * uframe_len, 0, ix->ix_nframes,
1608 &isoc->i_xfer);
1609 if (error) {
1610 DPRINTF(("uvideo: "
1611 "couldn't allocate xfer (%d)\n", error));
1612 return error;
1613 }
1614
1615 isoc->i_buf = usbd_get_buffer(isoc->i_xfer);
1616 }
1617
1618 uvideo_stream_recv_isoc_start(vs);
1619
1620 return 0;
1621 default:
1622 /* should never get here */
1623 DPRINTF(("uvideo_stream_start_xfer: unknown xfer type %#x\n",
1624 vs->vs_xfer_type));
1625 return EINVAL;
1626 }
1627 }
1628
1629 static int
1630 uvideo_stream_stop_xfer(struct uvideo_stream *vs)
1631 {
1632 struct uvideo_bulk_xfer *bx;
1633 struct uvideo_isoc_xfer *ix;
1634 usbd_status err;
1635 int i;
1636
1637 switch (vs->vs_xfer_type) {
1638 case UE_BULK:
1639 bx = &vs->vs_xfer.bulk;
1640
1641 DPRINTF(("uvideo_stream_stop_xfer: UE_BULK: "
1642 "waiting for thread to complete\n"));
1643 mutex_enter(&bx->bx_lock);
1644 if (bx->bx_running == true) {
1645 bx->bx_running = false;
1646 cv_wait_sig(&bx->bx_cv, &bx->bx_lock);
1647 }
1648 mutex_exit(&bx->bx_lock);
1649
1650 DPRINTF(("uvideo_stream_stop_xfer: UE_BULK: cleaning up\n"));
1651
1652 if (bx->bx_pipe) {
1653 usbd_abort_pipe(bx->bx_pipe);
1654 }
1655
1656 if (bx->bx_xfer) {
1657 usbd_destroy_xfer(bx->bx_xfer);
1658 bx->bx_xfer = NULL;
1659 }
1660
1661 if (bx->bx_pipe) {
1662 usbd_close_pipe(bx->bx_pipe);
1663 bx->bx_pipe = NULL;
1664 }
1665
1666 DPRINTF(("uvideo_stream_stop_xfer: UE_BULK: done\n"));
1667
1668 return 0;
1669 case UE_ISOCHRONOUS:
1670 ix = &vs->vs_xfer.isoc;
1671 if (ix->ix_pipe != NULL) {
1672 usbd_abort_pipe(ix->ix_pipe);
1673 }
1674
1675 for (i = 0; i < UVIDEO_NXFERS; i++) {
1676 struct uvideo_isoc *isoc = &ix->ix_i[i];
1677 if (isoc->i_xfer != NULL) {
1678 usbd_destroy_xfer(isoc->i_xfer);
1679 isoc->i_xfer = NULL;
1680 }
1681 }
1682
1683 if (ix->ix_pipe != NULL) {
1684 usbd_close_pipe(ix->ix_pipe);
1685 ix->ix_pipe = NULL;
1686 }
1687
1688 for (i = 0; i < UVIDEO_NXFERS; i++) {
1689 struct uvideo_isoc *isoc = &ix->ix_i[i];
1690 if (isoc->i_frlengths != NULL) {
1691 kmem_free(isoc->i_frlengths,
1692 sizeof(isoc->i_frlengths[0]) *
1693 ix->ix_nframes);
1694 isoc->i_frlengths = NULL;
1695 }
1696 }
1697
1698 /* Give it some time to settle */
1699 usbd_delay_ms(vs->vs_parent->sc_udev, 1000);
1700
1701 /* Set to zero bandwidth alternate interface zero */
1702 err = usbd_set_interface(vs->vs_iface, 0);
1703 if (err != USBD_NORMAL_COMPLETION) {
1704 DPRINTF(("uvideo_stream_stop_transfer: "
1705 "error setting zero bandwidth interface: "
1706 "%s (%d)\n",
1707 usbd_errstr(err), err));
1708 return EIO;
1709 }
1710
1711 return 0;
1712 default:
1713 /* should never get here */
1714 DPRINTF(("uvideo_stream_stop_xfer: unknown xfer type %#x\n",
1715 vs->vs_xfer_type));
1716 return EINVAL;
1717 }
1718 }
1719
1720 static usbd_status
1721 uvideo_stream_recv_isoc_start(struct uvideo_stream *vs)
1722 {
1723 int i;
1724
1725 for (i = 0; i < UVIDEO_NXFERS; i++)
1726 uvideo_stream_recv_isoc_start1(&vs->vs_xfer.isoc.ix_i[i]);
1727
1728 return USBD_NORMAL_COMPLETION;
1729 }
1730
1731 /* Initiate a usb transfer. */
1732 static usbd_status
1733 uvideo_stream_recv_isoc_start1(struct uvideo_isoc *isoc)
1734 {
1735 struct uvideo_isoc_xfer *ix;
1736 usbd_status err;
1737 int i;
1738
1739 ix = isoc->i_ix;
1740
1741 for (i = 0; i < ix->ix_nframes; ++i)
1742 isoc->i_frlengths[i] = ix->ix_uframe_len;
1743
1744 usbd_setup_isoc_xfer(isoc->i_xfer,
1745 isoc,
1746 isoc->i_frlengths,
1747 ix->ix_nframes,
1748 USBD_SHORT_XFER_OK,
1749 uvideo_stream_recv_isoc_complete);
1750
1751 err = usbd_transfer(isoc->i_xfer);
1752 if (err != USBD_IN_PROGRESS) {
1753 DPRINTF(("uvideo_stream_recv_start: "
1754 "usbd_transfer status=%s (%d)\n",
1755 usbd_errstr(err), err));
1756 }
1757 return err;
1758 }
1759
1760 static usbd_status
1761 uvideo_stream_recv_process(struct uvideo_stream *vs, uint8_t *buf, uint32_t len)
1762 {
1763 uvideo_payload_header_t *hdr;
1764 struct video_payload payload;
1765
1766 if (len < sizeof(uvideo_payload_header_t)) {
1767 DPRINTF(("uvideo_stream_recv_process: len %d < payload hdr\n",
1768 len));
1769 return USBD_SHORT_XFER;
1770 }
1771
1772 hdr = (uvideo_payload_header_t *)buf;
1773
1774 if (hdr->bHeaderLength > UVIDEO_PAYLOAD_HEADER_SIZE ||
1775 hdr->bHeaderLength < sizeof(uvideo_payload_header_t))
1776 return USBD_INVAL;
1777 if (hdr->bHeaderLength == len && !(hdr->bmHeaderInfo & UV_END_OF_FRAME))
1778 return USBD_INVAL;
1779 if (hdr->bmHeaderInfo & UV_ERROR)
1780 return USBD_IOERROR;
1781
1782 payload.data = buf + hdr->bHeaderLength;
1783 payload.size = len - hdr->bHeaderLength;
1784 payload.frameno = hdr->bmHeaderInfo & UV_FRAME_ID;
1785 payload.end_of_frame = hdr->bmHeaderInfo & UV_END_OF_FRAME;
1786
1787 video_submit_payload(vs->vs_videodev, &payload);
1788
1789 return USBD_NORMAL_COMPLETION;
1790 }
1791
1792 /* Callback on completion of usb isoc transfer */
1793 static void
1794 uvideo_stream_recv_isoc_complete(struct usbd_xfer *xfer,
1795 void *priv,
1796 usbd_status status)
1797 {
1798 struct uvideo_stream *vs;
1799 struct uvideo_isoc_xfer *ix;
1800 struct uvideo_isoc *isoc;
1801 int i;
1802 uint32_t count;
1803 uint8_t *buf;
1804
1805 isoc = priv;
1806 vs = isoc->i_vs;
1807 ix = isoc->i_ix;
1808
1809 if (status != USBD_NORMAL_COMPLETION) {
1810 DPRINTF(("uvideo_stream_recv_isoc_complete: status=%s (%d)\n",
1811 usbd_errstr(status), status));
1812
1813 if (status == USBD_STALLED)
1814 usbd_clear_endpoint_stall_async(ix->ix_pipe);
1815 else
1816 return;
1817 } else {
1818 usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
1819
1820 if (count == 0) {
1821 /* DPRINTF(("uvideo: zero length transfer\n")); */
1822 goto next;
1823 }
1824
1825
1826 for (i = 0, buf = isoc->i_buf;
1827 i < ix->ix_nframes;
1828 ++i, buf += ix->ix_uframe_len)
1829 {
1830 status = uvideo_stream_recv_process(vs, buf,
1831 isoc->i_frlengths[i]);
1832 if (status == USBD_IOERROR)
1833 break;
1834 }
1835 }
1836
1837 next:
1838 uvideo_stream_recv_isoc_start1(isoc);
1839 }
1840
1841 static void
1842 uvideo_stream_recv_bulk_transfer(void *addr)
1843 {
1844 struct uvideo_stream *vs = addr;
1845 struct uvideo_bulk_xfer *bx = &vs->vs_xfer.bulk;
1846 usbd_status err;
1847 uint32_t len;
1848
1849 DPRINTF(("uvideo_stream_recv_bulk_transfer: "
1850 "vs %p sc %p bx %p buffer %p\n", vs, vs->vs_parent, bx,
1851 bx->bx_buffer));
1852
1853 while (bx->bx_running) {
1854 len = bx->bx_buflen;
1855 err = usbd_bulk_transfer(bx->bx_xfer, bx->bx_pipe,
1856 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT,
1857 bx->bx_buffer, &len);
1858
1859 if (err == USBD_NORMAL_COMPLETION) {
1860 uvideo_stream_recv_process(vs, bx->bx_buffer, len);
1861 } else {
1862 DPRINTF(("uvideo_stream_recv_bulk_transfer: %s\n",
1863 usbd_errstr(err)));
1864 }
1865 }
1866
1867 DPRINTF(("uvideo_stream_recv_bulk_transfer: notify complete\n"));
1868
1869 mutex_enter(&bx->bx_lock);
1870 cv_broadcast(&bx->bx_cv);
1871 mutex_exit(&bx->bx_lock);
1872
1873 DPRINTF(("uvideo_stream_recv_bulk_transfer: return\n"));
1874
1875 kthread_exit(0);
1876 }
1877
1878 /*
1879 * uvideo_open - probe and commit video format and start receiving
1880 * video data
1881 */
1882 static int
1883 uvideo_open(void *addr, int flags)
1884 {
1885 struct uvideo_stream *vs = addr;
1886 struct uvideo_softc *sc = vs->vs_parent;
1887 struct video_format fmt;
1888
1889 DPRINTF(("uvideo_open: sc=%p\n", sc));
1890 if (sc->sc_dying)
1891 return EIO;
1892
1893 /* XXX select default format */
1894 fmt = *vs->vs_default_format;
1895 return uvideo_set_format(addr, &fmt);
1896 }
1897
1898
1899 static void
1900 uvideo_close(void *addr)
1901 {
1902 struct uvideo_stream *vs = addr;
1903
1904 uvideo_stop_transfer(addr);
1905
1906 if (vs->vs_state != UVIDEO_STATE_CLOSED) {
1907 vs->vs_state = UVIDEO_STATE_CLOSED;
1908 }
1909 }
1910
1911 static const char *
1912 uvideo_get_devname(void *addr)
1913 {
1914 struct uvideo_stream *vs = addr;
1915
1916 return vs->vs_parent->sc_devname;
1917 }
1918
1919 static const char *
1920 uvideo_get_businfo(void *addr)
1921 {
1922 struct uvideo_stream *vs = addr;
1923
1924 return vs->vs_parent->sc_businfo;
1925 }
1926
1927 static int
1928 uvideo_enum_format(void *addr, uint32_t index, struct video_format *format)
1929 {
1930 struct uvideo_stream *vs = addr;
1931 struct uvideo_softc *sc = vs->vs_parent;
1932 struct uvideo_format *video_format;
1933 int off;
1934
1935 if (sc->sc_dying)
1936 return EIO;
1937
1938 off = 0;
1939 SIMPLEQ_FOREACH(video_format, &vs->vs_formats, entries) {
1940 if (off++ != index)
1941 continue;
1942 format->pixel_format = video_format->format.pixel_format;
1943 format->width = video_format->format.width;
1944 format->height = video_format->format.height;
1945 return 0;
1946 }
1947
1948 return EINVAL;
1949 }
1950
1951 /*
1952 * uvideo_get_format
1953 */
1954 static int
1955 uvideo_get_format(void *addr, struct video_format *format)
1956 {
1957 struct uvideo_stream *vs = addr;
1958 struct uvideo_softc *sc = vs->vs_parent;
1959
1960 if (sc->sc_dying)
1961 return EIO;
1962
1963 *format = vs->vs_current_format;
1964
1965 return 0;
1966 }
1967
1968 /*
1969 * uvideo_set_format - TODO: this is broken and does nothing
1970 */
1971 static int
1972 uvideo_set_format(void *addr, struct video_format *format)
1973 {
1974 struct uvideo_stream *vs = addr;
1975 struct uvideo_softc *sc = vs->vs_parent;
1976 struct uvideo_format *uvfmt;
1977 uvideo_probe_and_commit_data_t probe, maxprobe;
1978 usbd_status err;
1979
1980 DPRINTF(("uvideo_set_format: sc=%p\n", sc));
1981 if (sc->sc_dying)
1982 return EIO;
1983
1984 uvfmt = uvideo_stream_guess_format(vs, format->pixel_format,
1985 format->width, format->height);
1986 if (uvfmt == NULL) {
1987 DPRINTF(("uvideo: uvideo_stream_guess_format couldn't find "
1988 "%dx%d format %d\n", format->width, format->height,
1989 format->pixel_format));
1990 return EINVAL;
1991 }
1992
1993 uvideo_init_probe_data(&probe);
1994 probe.bFormatIndex = UVIDEO_FORMAT_GET_FORMAT_INDEX(uvfmt);
1995 probe.bFrameIndex = UVIDEO_FORMAT_GET_FRAME_INDEX(uvfmt);
1996 USETDW(probe.dwFrameInterval, vs->vs_frame_interval); /* XXX */
1997
1998 maxprobe = probe;
1999 err = uvideo_stream_probe(vs, UR_GET_MAX, &maxprobe);
2000 if (err) {
2001 DPRINTF(("uvideo: error probe/GET_MAX: %s (%d)\n",
2002 usbd_errstr(err), err));
2003 } else {
2004 USETW(probe.wCompQuality, UGETW(maxprobe.wCompQuality));
2005 }
2006
2007 err = uvideo_stream_probe(vs, UR_SET_CUR, &probe);
2008 if (err) {
2009 DPRINTF(("uvideo: error commit/SET_CUR: %s (%d)\n",
2010 usbd_errstr(err), err));
2011 return EIO;
2012 }
2013
2014 uvideo_init_probe_data(&probe);
2015 err = uvideo_stream_probe(vs, UR_GET_CUR, &probe);
2016 if (err) {
2017 DPRINTF(("uvideo: error commit/SET_CUR: %s (%d)\n",
2018 usbd_errstr(err), err));
2019 return EIO;
2020 }
2021
2022 if (probe.bFormatIndex != UVIDEO_FORMAT_GET_FORMAT_INDEX(uvfmt)) {
2023 DPRINTF(("uvideo: probe/GET_CUR returned format index %d "
2024 "(expected %d)\n", probe.bFormatIndex,
2025 UVIDEO_FORMAT_GET_FORMAT_INDEX(uvfmt)));
2026 probe.bFormatIndex = UVIDEO_FORMAT_GET_FORMAT_INDEX(uvfmt);
2027 }
2028 if (probe.bFrameIndex != UVIDEO_FORMAT_GET_FRAME_INDEX(uvfmt)) {
2029 DPRINTF(("uvideo: probe/GET_CUR returned frame index %d "
2030 "(expected %d)\n", probe.bFrameIndex,
2031 UVIDEO_FORMAT_GET_FRAME_INDEX(uvfmt)));
2032 probe.bFrameIndex = UVIDEO_FORMAT_GET_FRAME_INDEX(uvfmt);
2033 }
2034 USETDW(probe.dwFrameInterval, vs->vs_frame_interval); /* XXX */
2035
2036 /* commit/SET_CUR. Fourth step is to set the alternate
2037 * interface. Currently the fourth step is in
2038 * uvideo_start_transfer. Maybe move it here? */
2039 err = uvideo_stream_commit(vs, UR_SET_CUR, &probe);
2040 if (err) {
2041 DPRINTF(("uvideo: error commit/SET_CUR: %s (%d)\n",
2042 usbd_errstr(err), err));
2043 return EIO;
2044 }
2045
2046 DPRINTFN(15, ("uvideo_set_format: committing to format: "
2047 "bmHint=0x%04x bFormatIndex=%d bFrameIndex=%d "
2048 "dwFrameInterval=%u wKeyFrameRate=%d wPFrameRate=%d "
2049 "wCompQuality=%d wCompWindowSize=%d wDelay=%d "
2050 "dwMaxVideoFrameSize=%u dwMaxPayloadTransferSize=%u",
2051 UGETW(probe.bmHint),
2052 probe.bFormatIndex,
2053 probe.bFrameIndex,
2054 UGETDW(probe.dwFrameInterval),
2055 UGETW(probe.wKeyFrameRate),
2056 UGETW(probe.wPFrameRate),
2057 UGETW(probe.wCompQuality),
2058 UGETW(probe.wCompWindowSize),
2059 UGETW(probe.wDelay),
2060 UGETDW(probe.dwMaxVideoFrameSize),
2061 UGETDW(probe.dwMaxPayloadTransferSize)));
2062 if (vs->vs_probelen == 34) {
2063 DPRINTFN(15, (" dwClockFrequency=%u bmFramingInfo=0x%02x "
2064 "bPreferedVersion=%d bMinVersion=%d "
2065 "bMaxVersion=%d",
2066 UGETDW(probe.dwClockFrequency),
2067 probe.bmFramingInfo,
2068 probe.bPreferedVersion,
2069 probe.bMinVersion,
2070 probe.bMaxVersion));
2071 }
2072 DPRINTFN(15, ("\n"));
2073
2074 vs->vs_frame_interval = UGETDW(probe.dwFrameInterval);
2075 vs->vs_max_payload_size = UGETDW(probe.dwMaxPayloadTransferSize);
2076
2077 *format = uvfmt->format;
2078 vs->vs_current_format = *format;
2079 DPRINTF(("uvideo_set_format: pixeltype is %d\n", format->pixel_format));
2080
2081 return 0;
2082 }
2083
2084 static int
2085 uvideo_try_format(void *addr, struct video_format *format)
2086 {
2087 struct uvideo_stream *vs = addr;
2088 struct uvideo_format *uvfmt;
2089
2090 uvfmt = uvideo_stream_guess_format(vs, format->pixel_format,
2091 format->width, format->height);
2092 if (uvfmt == NULL)
2093 return EINVAL;
2094
2095 *format = uvfmt->format;
2096 return 0;
2097 }
2098
2099 static int
2100 uvideo_get_framerate(void *addr, struct video_fract *fract)
2101 {
2102 struct uvideo_stream *vs = addr;
2103
2104 switch (vs->vs_frame_interval) {
2105 case 41666: /* 240 */
2106 case 83333: /* 120 */
2107 case 166666: /* 60 */
2108 case 200000: /* 50 */
2109 case 333333: /* 30 */
2110 case 400000: /* 25 */
2111 case 500000: /* 20 */
2112 case 666666: /* 15 */
2113 case 1000000: /* 10 */
2114 fract->numerator = 1;
2115 fract->denominator = 10000000 / vs->vs_frame_interval;
2116 break;
2117 case 166833: /* 59.94 */
2118 fract->numerator = 60;
2119 fract->denominator = 1001;
2120 break;
2121 case 333667: /* 29.97 */
2122 fract->numerator = 30;
2123 fract->denominator = 1001;
2124 break;
2125 default:
2126 fract->numerator = vs->vs_frame_interval;
2127 fract->denominator = 10000000;
2128 break;
2129 }
2130
2131 return 0;
2132 }
2133
2134 static int
2135 uvideo_set_framerate(void *addr, struct video_fract *fract)
2136 {
2137 /* XXX setting framerate is not supported yet, return actual rate */
2138 return uvideo_get_framerate(addr, fract);
2139 }
2140
2141 static int
2142 uvideo_start_transfer(void *addr)
2143 {
2144 struct uvideo_stream *vs = addr;
2145 int s, err;
2146
2147 s = splusb();
2148 err = uvideo_stream_start_xfer(vs);
2149 splx(s);
2150
2151 return err;
2152 }
2153
2154 static int
2155 uvideo_stop_transfer(void *addr)
2156 {
2157 struct uvideo_stream *vs = addr;
2158 int err, s;
2159
2160 s = splusb();
2161 err = uvideo_stream_stop_xfer(vs);
2162 splx(s);
2163
2164 return err;
2165 }
2166
2167
2168 static int
2169 uvideo_get_control_group(void *addr, struct video_control_group *group)
2170 {
2171 struct uvideo_stream *vs = addr;
2172 struct uvideo_softc *sc = vs->vs_parent;
2173 usb_device_request_t req;
2174 usbd_status err;
2175 uint8_t control_id, ent_id, data[16];
2176 uint16_t len;
2177 int s;
2178
2179 /* request setup */
2180 switch (group->group_id) {
2181 case VIDEO_CONTROL_PANTILT_RELATIVE:
2182 if (group->length != 4)
2183 return EINVAL;
2184
2185 return EINVAL;
2186 case VIDEO_CONTROL_SHARPNESS:
2187 if (group->length != 1)
2188 return EINVAL;
2189
2190 control_id = UVIDEO_PU_SHARPNESS_CONTROL;
2191 ent_id = 2; /* TODO: hardcoded logitech processing unit */
2192 len = 2;
2193 break;
2194 default:
2195 return EINVAL;
2196 }
2197
2198 /* do request */
2199 req.bmRequestType = UVIDEO_REQUEST_TYPE_INTERFACE |
2200 UVIDEO_REQUEST_TYPE_CLASS_SPECIFIC |
2201 UVIDEO_REQUEST_TYPE_GET;
2202 req.bRequest = UR_GET_CUR;
2203 USETW(req.wValue, control_id << 8);
2204 USETW(req.wIndex, (ent_id << 8) | sc->sc_ifaceno);
2205 USETW(req.wLength, len);
2206
2207 s = splusb();
2208 err = usbd_do_request(sc->sc_udev, &req, data);
2209 splx(s);
2210 if (err != USBD_NORMAL_COMPLETION) {
2211 DPRINTF(("uvideo_set_control: error %s (%d)\n",
2212 usbd_errstr(err), err));
2213 return EIO; /* TODO: more detail here? */
2214 }
2215
2216 /* extract request data */
2217 switch (group->group_id) {
2218 case VIDEO_CONTROL_SHARPNESS:
2219 group->control[0].value = UGETW(data);
2220 break;
2221 default:
2222 return EINVAL;
2223 }
2224
2225 return 0;
2226 }
2227
2228
2229 static int
2230 uvideo_set_control_group(void *addr, const struct video_control_group *group)
2231 {
2232 struct uvideo_stream *vs = addr;
2233 struct uvideo_softc *sc = vs->vs_parent;
2234 usb_device_request_t req;
2235 usbd_status err;
2236 uint8_t control_id, ent_id, data[16]; /* long enough for all controls */
2237 uint16_t len;
2238 int s;
2239
2240 switch (group->group_id) {
2241 case VIDEO_CONTROL_PANTILT_RELATIVE:
2242 if (group->length != 4)
2243 return EINVAL;
2244
2245 if (group->control[0].value != 0 ||
2246 group->control[0].value != 1 ||
2247 group->control[0].value != 0xff)
2248 return ERANGE;
2249
2250 if (group->control[2].value != 0 ||
2251 group->control[2].value != 1 ||
2252 group->control[2].value != 0xff)
2253 return ERANGE;
2254
2255 control_id = UVIDEO_CT_PANTILT_RELATIVE_CONTROL;
2256 ent_id = 1; /* TODO: hardcoded logitech camera terminal */
2257 len = 4;
2258 data[0] = group->control[0].value;
2259 data[1] = group->control[1].value;
2260 data[2] = group->control[2].value;
2261 data[3] = group->control[3].value;
2262 break;
2263 case VIDEO_CONTROL_BRIGHTNESS:
2264 if (group->length != 1)
2265 return EINVAL;
2266 control_id = UVIDEO_PU_BRIGHTNESS_CONTROL;
2267 ent_id = 2;
2268 len = 2;
2269 USETW(data, group->control[0].value);
2270 break;
2271 case VIDEO_CONTROL_GAIN:
2272 if (group->length != 1)
2273 return EINVAL;
2274 control_id = UVIDEO_PU_GAIN_CONTROL;
2275 ent_id = 2;
2276 len = 2;
2277 USETW(data, group->control[0].value);
2278 break;
2279 case VIDEO_CONTROL_SHARPNESS:
2280 if (group->length != 1)
2281 return EINVAL;
2282 control_id = UVIDEO_PU_SHARPNESS_CONTROL;
2283 ent_id = 2; /* TODO: hardcoded logitech processing unit */
2284 len = 2;
2285 USETW(data, group->control[0].value);
2286 break;
2287 default:
2288 return EINVAL;
2289 }
2290
2291 req.bmRequestType = UVIDEO_REQUEST_TYPE_INTERFACE |
2292 UVIDEO_REQUEST_TYPE_CLASS_SPECIFIC |
2293 UVIDEO_REQUEST_TYPE_SET;
2294 req.bRequest = UR_SET_CUR;
2295 USETW(req.wValue, control_id << 8);
2296 USETW(req.wIndex, (ent_id << 8) | sc->sc_ifaceno);
2297 USETW(req.wLength, len);
2298
2299 s = splusb();
2300 err = usbd_do_request(sc->sc_udev, &req, data);
2301 splx(s);
2302 if (err != USBD_NORMAL_COMPLETION) {
2303 DPRINTF(("uvideo_set_control: error %s (%d)\n",
2304 usbd_errstr(err), err));
2305 return EIO; /* TODO: more detail here? */
2306 }
2307
2308 return 0;
2309 }
2310
2311 static usbd_status
2312 uvideo_stream_probe_and_commit(struct uvideo_stream *vs,
2313 uint8_t action, uint8_t control,
2314 void *data)
2315 {
2316 usb_device_request_t req;
2317
2318 switch (action) {
2319 case UR_SET_CUR:
2320 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
2321 USETW(req.wLength, vs->vs_probelen);
2322 break;
2323 case UR_GET_CUR:
2324 case UR_GET_MIN:
2325 case UR_GET_MAX:
2326 case UR_GET_DEF:
2327 req.bmRequestType = UT_READ_CLASS_INTERFACE;
2328 USETW(req.wLength, vs->vs_probelen);
2329 break;
2330 case UR_GET_INFO:
2331 req.bmRequestType = UT_READ_CLASS_INTERFACE;
2332 USETW(req.wLength, sizeof(uByte));
2333 break;
2334 case UR_GET_LEN:
2335 req.bmRequestType = UT_READ_CLASS_INTERFACE;
2336 USETW(req.wLength, sizeof(uWord)); /* is this right? */
2337 break;
2338 default:
2339 DPRINTF(("uvideo_probe_and_commit: "
2340 "unknown request action %d\n", action));
2341 return USBD_NOT_STARTED;
2342 }
2343
2344 req.bRequest = action;
2345 USETW2(req.wValue, control, 0);
2346 USETW2(req.wIndex, 0, vs->vs_ifaceno);
2347
2348 return (usbd_do_request_flags(vs->vs_parent->sc_udev, &req, data,
2349 0, 0,
2350 USBD_DEFAULT_TIMEOUT));
2351 }
2352
2353 static void
2354 uvideo_init_probe_data(uvideo_probe_and_commit_data_t *probe)
2355 {
2356 /* all zeroes tells camera to choose what it wants */
2357 memset(probe, 0, sizeof(*probe));
2358 }
2359
2360
2361 #ifdef _MODULE
2362
2363 MODULE(MODULE_CLASS_DRIVER, uvideo, NULL);
2364 static const struct cfiattrdata videobuscf_iattrdata = {
2365 "videobus", 0, {
2366 { NULL, NULL, 0 },
2367 }
2368 };
2369 static const struct cfiattrdata * const uvideo_attrs[] = {
2370 &videobuscf_iattrdata, NULL
2371 };
2372 CFDRIVER_DECL(uvideo, DV_DULL, uvideo_attrs);
2373 extern struct cfattach uvideo_ca;
2374 extern struct cfattach uvideo_ca;
2375 static int uvideoloc[6] = { -1, -1, -1, -1, -1, -1 };
2376 static struct cfparent uhubparent = {
2377 "usbifif", NULL, DVUNIT_ANY
2378 };
2379 static struct cfdata uvideo_cfdata[] = {
2380 {
2381 .cf_name = "uvideo",
2382 .cf_atname = "uvideo",
2383 .cf_unit = 0,
2384 .cf_fstate = FSTATE_STAR,
2385 .cf_loc = uvideoloc,
2386 .cf_flags = 0,
2387 .cf_pspec = &uhubparent,
2388 },
2389 { NULL, NULL, 0, 0, NULL, 0, NULL },
2390 };
2391
2392 static int
2393 uvideo_modcmd(modcmd_t cmd, void *arg)
2394 {
2395 int err;
2396
2397
2398 switch (cmd) {
2399 case MODULE_CMD_INIT:
2400 DPRINTF(("uvideo: attempting to load\n"));
2401
2402 err = config_cfdriver_attach(&uvideo_cd);
2403 if (err)
2404 return err;
2405 err = config_cfattach_attach("uvideo", &uvideo_ca);
2406 if (err) {
2407 config_cfdriver_detach(&uvideo_cd);
2408 return err;
2409 }
2410 err = config_cfdata_attach(uvideo_cfdata, 1);
2411 if (err) {
2412 config_cfattach_detach("uvideo", &uvideo_ca);
2413 config_cfdriver_detach(&uvideo_cd);
2414 return err;
2415 }
2416 DPRINTF(("uvideo: loaded module\n"));
2417 return 0;
2418 case MODULE_CMD_FINI:
2419 DPRINTF(("uvideo: attempting to unload module\n"));
2420 err = config_cfdata_detach(uvideo_cfdata);
2421 if (err)
2422 return err;
2423 config_cfattach_detach("uvideo", &uvideo_ca);
2424 config_cfdriver_detach(&uvideo_cd);
2425 DPRINTF(("uvideo: module unload\n"));
2426 return 0;
2427 default:
2428 return ENOTTY;
2429 }
2430 }
2431
2432 #endif /* _MODULE */
2433
2434
2435 #ifdef UVIDEO_DEBUG
2436 /* Some functions to print out descriptors. Mostly useless other than
2437 * debugging/exploration purposes. */
2438
2439
2440 static void
2441 print_bitmap(const uByte *start, uByte nbytes)
2442 {
2443 int byte, bit;
2444
2445 /* most significant first */
2446 for (byte = nbytes-1; byte >= 0; --byte) {
2447 if (byte < nbytes-1) printf("-");
2448 for (bit = 7; bit >= 0; --bit)
2449 printf("%01d", (start[byte] >> bit) &1);
2450 }
2451 }
2452
2453 static void
2454 print_descriptor(const usb_descriptor_t *desc)
2455 {
2456 static int current_class = -1;
2457 static int current_subclass = -1;
2458
2459 if (desc->bDescriptorType == UDESC_INTERFACE) {
2460 const usb_interface_descriptor_t *id;
2461 id = (const usb_interface_descriptor_t *)desc;
2462 current_class = id->bInterfaceClass;
2463 current_subclass = id->bInterfaceSubClass;
2464 print_interface_descriptor(id);
2465 printf("\n");
2466 return;
2467 }
2468
2469 printf(" "); /* indent */
2470
2471 if (current_class == UICLASS_VIDEO) {
2472 switch (current_subclass) {
2473 case UISUBCLASS_VIDEOCONTROL:
2474 print_vc_descriptor(desc);
2475 break;
2476 case UISUBCLASS_VIDEOSTREAMING:
2477 print_vs_descriptor(desc);
2478 break;
2479 case UISUBCLASS_VIDEOCOLLECTION:
2480 printf("uvc collection: len=%d type=0x%02x",
2481 desc->bLength, desc->bDescriptorType);
2482 break;
2483 }
2484 } else {
2485 printf("non uvc descriptor len=%d type=0x%02x",
2486 desc->bLength, desc->bDescriptorType);
2487 }
2488
2489 printf("\n");
2490 }
2491
2492 static void
2493 print_vc_descriptor(const usb_descriptor_t *desc)
2494 {
2495 const uvideo_descriptor_t *vcdesc;
2496
2497 printf("VC ");
2498
2499 switch (desc->bDescriptorType) {
2500 case UDESC_ENDPOINT:
2501 print_endpoint_descriptor(
2502 (const usb_endpoint_descriptor_t *)desc);
2503 break;
2504 case UDESC_CS_INTERFACE:
2505 vcdesc = (const uvideo_descriptor_t *)desc;
2506 switch (vcdesc->bDescriptorSubtype) {
2507 case UDESC_VC_HEADER:
2508 print_vc_header_descriptor(
2509 (const uvideo_vc_header_descriptor_t *)
2510 vcdesc);
2511 break;
2512 case UDESC_INPUT_TERMINAL:
2513 switch (UGETW(
2514 ((const uvideo_input_terminal_descriptor_t *)
2515 vcdesc)->wTerminalType)) {
2516 case UVIDEO_ITT_CAMERA:
2517 print_camera_terminal_descriptor(
2518 (const uvideo_camera_terminal_descriptor_t *)vcdesc);
2519 break;
2520 default:
2521 print_input_terminal_descriptor(
2522 (const uvideo_input_terminal_descriptor_t *)vcdesc);
2523 break;
2524 }
2525 break;
2526 case UDESC_OUTPUT_TERMINAL:
2527 print_output_terminal_descriptor(
2528 (const uvideo_output_terminal_descriptor_t *)
2529 vcdesc);
2530 break;
2531 case UDESC_SELECTOR_UNIT:
2532 print_selector_unit_descriptor(
2533 (const uvideo_selector_unit_descriptor_t *)
2534 vcdesc);
2535 break;
2536 case UDESC_PROCESSING_UNIT:
2537 print_processing_unit_descriptor(
2538 (const uvideo_processing_unit_descriptor_t *)
2539 vcdesc);
2540 break;
2541 case UDESC_EXTENSION_UNIT:
2542 print_extension_unit_descriptor(
2543 (const uvideo_extension_unit_descriptor_t *)
2544 vcdesc);
2545 break;
2546 default:
2547 printf("class specific interface "
2548 "len=%d type=0x%02x subtype=0x%02x",
2549 vcdesc->bLength,
2550 vcdesc->bDescriptorType,
2551 vcdesc->bDescriptorSubtype);
2552 break;
2553 }
2554 break;
2555 case UDESC_CS_ENDPOINT:
2556 vcdesc = (const uvideo_descriptor_t *)desc;
2557 switch (vcdesc->bDescriptorSubtype) {
2558 case UDESC_VC_INTERRUPT_ENDPOINT:
2559 print_interrupt_endpoint_descriptor(
2560 (const uvideo_vc_interrupt_endpoint_descriptor_t *)
2561 vcdesc);
2562 break;
2563 default:
2564 printf("class specific endpoint "
2565 "len=%d type=0x%02x subtype=0x%02x",
2566 vcdesc->bLength,
2567 vcdesc->bDescriptorType,
2568 vcdesc->bDescriptorSubtype);
2569 break;
2570 }
2571 break;
2572 default:
2573 printf("unknown: len=%d type=0x%02x",
2574 desc->bLength, desc->bDescriptorType);
2575 break;
2576 }
2577 }
2578
2579 static void
2580 print_vs_descriptor(const usb_descriptor_t *desc)
2581 {
2582 const uvideo_descriptor_t * vsdesc;
2583 printf("VS ");
2584
2585 switch (desc->bDescriptorType) {
2586 case UDESC_ENDPOINT:
2587 print_endpoint_descriptor(
2588 (const usb_endpoint_descriptor_t *)desc);
2589 break;
2590 case UDESC_CS_INTERFACE:
2591 vsdesc = (const uvideo_descriptor_t *)desc;
2592 switch (vsdesc->bDescriptorSubtype) {
2593 case UDESC_VS_INPUT_HEADER:
2594 print_vs_input_header_descriptor(
2595 (const uvideo_vs_input_header_descriptor_t *)
2596 vsdesc);
2597 break;
2598 case UDESC_VS_OUTPUT_HEADER:
2599 print_vs_output_header_descriptor(
2600 (const uvideo_vs_output_header_descriptor_t *)
2601 vsdesc);
2602 break;
2603 case UDESC_VS_FORMAT_UNCOMPRESSED:
2604 print_vs_format_uncompressed_descriptor(
2605 (const uvideo_vs_format_uncompressed_descriptor_t *)
2606 vsdesc);
2607 break;
2608 case UDESC_VS_FRAME_UNCOMPRESSED:
2609 print_vs_frame_uncompressed_descriptor(
2610 (const uvideo_vs_frame_uncompressed_descriptor_t *)
2611 vsdesc);
2612 break;
2613 case UDESC_VS_FORMAT_MJPEG:
2614 print_vs_format_mjpeg_descriptor(
2615 (const uvideo_vs_format_mjpeg_descriptor_t *)
2616 vsdesc);
2617 break;
2618 case UDESC_VS_FRAME_MJPEG:
2619 print_vs_frame_mjpeg_descriptor(
2620 (const uvideo_vs_frame_mjpeg_descriptor_t *)
2621 vsdesc);
2622 break;
2623 case UDESC_VS_FORMAT_DV:
2624 print_vs_format_dv_descriptor(
2625 (const uvideo_vs_format_dv_descriptor_t *)
2626 vsdesc);
2627 break;
2628 default:
2629 printf("unknown cs interface: len=%d type=0x%02x "
2630 "subtype=0x%02x",
2631 vsdesc->bLength, vsdesc->bDescriptorType,
2632 vsdesc->bDescriptorSubtype);
2633 }
2634 break;
2635 default:
2636 printf("unknown: len=%d type=0x%02x",
2637 desc->bLength, desc->bDescriptorType);
2638 break;
2639 }
2640 }
2641
2642 static void
2643 print_interface_descriptor(const usb_interface_descriptor_t *id)
2644 {
2645 printf("Interface: Len=%d Type=0x%02x "
2646 "bInterfaceNumber=0x%02x "
2647 "bAlternateSetting=0x%02x bNumEndpoints=0x%02x "
2648 "bInterfaceClass=0x%02x bInterfaceSubClass=0x%02x "
2649 "bInterfaceProtocol=0x%02x iInterface=0x%02x",
2650 id->bLength,
2651 id->bDescriptorType,
2652 id->bInterfaceNumber,
2653 id->bAlternateSetting,
2654 id->bNumEndpoints,
2655 id->bInterfaceClass,
2656 id->bInterfaceSubClass,
2657 id->bInterfaceProtocol,
2658 id->iInterface);
2659 }
2660
2661 static void
2662 print_endpoint_descriptor(const usb_endpoint_descriptor_t *desc)
2663 {
2664 printf("Endpoint: Len=%d Type=0x%02x "
2665 "bEndpointAddress=0x%02x ",
2666 desc->bLength,
2667 desc->bDescriptorType,
2668 desc->bEndpointAddress);
2669 printf("bmAttributes=");
2670 print_bitmap(&desc->bmAttributes, 1);
2671 printf(" wMaxPacketSize=%d bInterval=%d",
2672 UGETW(desc->wMaxPacketSize),
2673 desc->bInterval);
2674 }
2675
2676 static void
2677 print_vc_header_descriptor(
2678 const uvideo_vc_header_descriptor_t *desc)
2679 {
2680 printf("Interface Header: "
2681 "Len=%d Type=0x%02x Subtype=0x%02x "
2682 "bcdUVC=%d wTotalLength=%d "
2683 "dwClockFrequency=%u bInCollection=%d",
2684 desc->bLength,
2685 desc->bDescriptorType,
2686 desc->bDescriptorSubtype,
2687 UGETW(desc->bcdUVC),
2688 UGETW(desc->wTotalLength),
2689 UGETDW(desc->dwClockFrequency),
2690 desc->bInCollection);
2691 }
2692
2693 static void
2694 print_input_terminal_descriptor(
2695 const uvideo_input_terminal_descriptor_t *desc)
2696 {
2697 printf("Input Terminal: "
2698 "Len=%d Type=0x%02x Subtype=0x%02x "
2699 "bTerminalID=%d wTerminalType=%x bAssocTerminal=%d "
2700 "iTerminal=%d",
2701 desc->bLength,
2702 desc->bDescriptorType,
2703 desc->bDescriptorSubtype,
2704 desc->bTerminalID,
2705 UGETW(desc->wTerminalType),
2706 desc->bAssocTerminal,
2707 desc->iTerminal);
2708 }
2709
2710 static void
2711 print_output_terminal_descriptor(
2712 const uvideo_output_terminal_descriptor_t *desc)
2713 {
2714 printf("Output Terminal: "
2715 "Len=%d Type=0x%02x Subtype=0x%02x "
2716 "bTerminalID=%d wTerminalType=%x bAssocTerminal=%d "
2717 "bSourceID=%d iTerminal=%d",
2718 desc->bLength,
2719 desc->bDescriptorType,
2720 desc->bDescriptorSubtype,
2721 desc->bTerminalID,
2722 UGETW(desc->wTerminalType),
2723 desc->bAssocTerminal,
2724 desc->bSourceID,
2725 desc->iTerminal);
2726 }
2727
2728 static void
2729 print_camera_terminal_descriptor(
2730 const uvideo_camera_terminal_descriptor_t *desc)
2731 {
2732 printf("Camera Terminal: "
2733 "Len=%d Type=0x%02x Subtype=0x%02x "
2734 "bTerminalID=%d wTerminalType=%x bAssocTerminal=%d "
2735 "iTerminal=%d "
2736 "wObjectiveFocalLengthMin/Max=%d/%d "
2737 "wOcularFocalLength=%d "
2738 "bControlSize=%d ",
2739 desc->bLength,
2740 desc->bDescriptorType,
2741 desc->bDescriptorSubtype,
2742 desc->bTerminalID,
2743 UGETW(desc->wTerminalType),
2744 desc->bAssocTerminal,
2745 desc->iTerminal,
2746 UGETW(desc->wObjectiveFocalLengthMin),
2747 UGETW(desc->wObjectiveFocalLengthMax),
2748 UGETW(desc->wOcularFocalLength),
2749 desc->bControlSize);
2750 printf("bmControls=");
2751 print_bitmap(desc->bmControls, desc->bControlSize);
2752 }
2753
2754 static void
2755 print_selector_unit_descriptor(
2756 const uvideo_selector_unit_descriptor_t *desc)
2757 {
2758 int i;
2759 const uByte *b;
2760 printf("Selector Unit: "
2761 "Len=%d Type=0x%02x Subtype=0x%02x "
2762 "bUnitID=%d bNrInPins=%d ",
2763 desc->bLength,
2764 desc->bDescriptorType,
2765 desc->bDescriptorSubtype,
2766 desc->bUnitID,
2767 desc->bNrInPins);
2768 printf("baSourceIDs=");
2769 b = &desc->baSourceID[0];
2770 for (i = 0; i < desc->bNrInPins; ++i)
2771 printf("%d ", *b++);
2772 printf("iSelector=%d", *b);
2773 }
2774
2775 static void
2776 print_processing_unit_descriptor(
2777 const uvideo_processing_unit_descriptor_t *desc)
2778 {
2779 const uByte *b;
2780
2781 printf("Processing Unit: "
2782 "Len=%d Type=0x%02x Subtype=0x%02x "
2783 "bUnitID=%d bSourceID=%d wMaxMultiplier=%d bControlSize=%d ",
2784 desc->bLength,
2785 desc->bDescriptorType,
2786 desc->bDescriptorSubtype,
2787 desc->bUnitID,
2788 desc->bSourceID,
2789 UGETW(desc->wMaxMultiplier),
2790 desc->bControlSize);
2791 printf("bmControls=");
2792 print_bitmap(desc->bmControls, desc->bControlSize);
2793 b = &desc->bControlSize + desc->bControlSize + 1;
2794 printf(" iProcessing=%d bmVideoStandards=", *b);
2795 b += 1;
2796 print_bitmap(b, 1);
2797 }
2798
2799 static void
2800 print_extension_unit_descriptor(
2801 const uvideo_extension_unit_descriptor_t *desc)
2802 {
2803 const uByte * byte;
2804 uByte controlbytes;
2805 int i;
2806
2807 printf("Extension Unit: "
2808 "Len=%d Type=0x%02x Subtype=0x%02x "
2809 "bUnitID=%d ",
2810 desc->bLength,
2811 desc->bDescriptorType,
2812 desc->bDescriptorSubtype,
2813 desc->bUnitID);
2814
2815 printf("guidExtensionCode=");
2816 usb_guid_print(&desc->guidExtensionCode);
2817 printf(" ");
2818
2819 printf("bNumControls=%d bNrInPins=%d ",
2820 desc->bNumControls,
2821 desc->bNrInPins);
2822
2823 printf("baSourceIDs=");
2824 byte = &desc->baSourceID[0];
2825 for (i = 0; i < desc->bNrInPins; ++i)
2826 printf("%d ", *byte++);
2827
2828 controlbytes = *byte++;
2829 printf("bControlSize=%d ", controlbytes);
2830 printf("bmControls=");
2831 print_bitmap(byte, controlbytes);
2832
2833 byte += controlbytes;
2834 printf(" iExtension=%d", *byte);
2835 }
2836
2837 static void
2838 print_interrupt_endpoint_descriptor(
2839 const uvideo_vc_interrupt_endpoint_descriptor_t *desc)
2840 {
2841 printf("Interrupt Endpoint: "
2842 "Len=%d Type=0x%02x Subtype=0x%02x "
2843 "wMaxTransferSize=%d ",
2844 desc->bLength,
2845 desc->bDescriptorType,
2846 desc->bDescriptorSubtype,
2847 UGETW(desc->wMaxTransferSize));
2848 }
2849
2850
2851 static void
2852 print_vs_output_header_descriptor(
2853 const uvideo_vs_output_header_descriptor_t *desc)
2854 {
2855 printf("Interface Output Header: "
2856 "Len=%d Type=0x%02x Subtype=0x%02x "
2857 "bNumFormats=%d wTotalLength=%d bEndpointAddress=%d "
2858 "bTerminalLink=%d bControlSize=%d",
2859 desc->bLength,
2860 desc->bDescriptorType,
2861 desc->bDescriptorSubtype,
2862 desc->bNumFormats,
2863 UGETW(desc->wTotalLength),
2864 desc->bEndpointAddress,
2865 desc->bTerminalLink,
2866 desc->bControlSize);
2867 }
2868
2869 static void
2870 print_vs_input_header_descriptor(
2871 const uvideo_vs_input_header_descriptor_t *desc)
2872 {
2873 printf("Interface Input Header: "
2874 "Len=%d Type=0x%02x Subtype=0x%02x "
2875 "bNumFormats=%d wTotalLength=%d bEndpointAddress=%d "
2876 "bmInfo=%x bTerminalLink=%d bStillCaptureMethod=%d "
2877 "bTriggerSupport=%d bTriggerUsage=%d bControlSize=%d ",
2878 desc->bLength,
2879 desc->bDescriptorType,
2880 desc->bDescriptorSubtype,
2881 desc->bNumFormats,
2882 UGETW(desc->wTotalLength),
2883 desc->bEndpointAddress,
2884 desc->bmInfo,
2885 desc->bTerminalLink,
2886 desc->bStillCaptureMethod,
2887 desc->bTriggerSupport,
2888 desc->bTriggerUsage,
2889 desc->bControlSize);
2890 print_bitmap(desc->bmaControls, desc->bControlSize);
2891 }
2892
2893 static void
2894 print_vs_format_uncompressed_descriptor(
2895 const uvideo_vs_format_uncompressed_descriptor_t *desc)
2896 {
2897 printf("Format Uncompressed: "
2898 "Len=%d Type=0x%02x Subtype=0x%02x "
2899 "bFormatIndex=%d bNumFrameDescriptors=%d ",
2900 desc->bLength,
2901 desc->bDescriptorType,
2902 desc->bDescriptorSubtype,
2903 desc->bFormatIndex,
2904 desc->bNumFrameDescriptors);
2905 usb_guid_print(&desc->guidFormat);
2906 printf(" bBitsPerPixel=%d bDefaultFrameIndex=%d "
2907 "bAspectRatioX=%d bAspectRatioY=%d "
2908 "bmInterlaceFlags=0x%02x bCopyProtect=%d",
2909 desc->bBitsPerPixel,
2910 desc->bDefaultFrameIndex,
2911 desc->bAspectRatioX,
2912 desc->bAspectRatioY,
2913 desc->bmInterlaceFlags,
2914 desc->bCopyProtect);
2915 }
2916
2917 static void
2918 print_vs_frame_uncompressed_descriptor(
2919 const uvideo_vs_frame_uncompressed_descriptor_t *desc)
2920 {
2921 printf("Frame Uncompressed: "
2922 "Len=%d Type=0x%02x Subtype=0x%02x "
2923 "bFrameIndex=%d bmCapabilities=0x%02x "
2924 "wWidth=%d wHeight=%d dwMinBitRate=%u dwMaxBitRate=%u "
2925 "dwMaxVideoFrameBufferSize=%u dwDefaultFrameInterval=%u "
2926 "bFrameIntervalType=%d",
2927 desc->bLength,
2928 desc->bDescriptorType,
2929 desc->bDescriptorSubtype,
2930 desc->bFrameIndex,
2931 desc->bmCapabilities,
2932 UGETW(desc->wWidth),
2933 UGETW(desc->wHeight),
2934 UGETDW(desc->dwMinBitRate),
2935 UGETDW(desc->dwMaxBitRate),
2936 UGETDW(desc->dwMaxVideoFrameBufferSize),
2937 UGETDW(desc->dwDefaultFrameInterval),
2938 desc->bFrameIntervalType);
2939 }
2940
2941 static void
2942 print_vs_format_mjpeg_descriptor(
2943 const uvideo_vs_format_mjpeg_descriptor_t *desc)
2944 {
2945 printf("MJPEG format: "
2946 "Len=%d Type=0x%02x Subtype=0x%02x "
2947 "bFormatIndex=%d bNumFrameDescriptors=%d bmFlags=0x%02x "
2948 "bDefaultFrameIndex=%d bAspectRatioX=%d bAspectRatioY=%d "
2949 "bmInterlaceFlags=0x%02x bCopyProtect=%d",
2950 desc->bLength,
2951 desc->bDescriptorType,
2952 desc->bDescriptorSubtype,
2953 desc->bFormatIndex,
2954 desc->bNumFrameDescriptors,
2955 desc->bmFlags,
2956 desc->bDefaultFrameIndex,
2957 desc->bAspectRatioX,
2958 desc->bAspectRatioY,
2959 desc->bmInterlaceFlags,
2960 desc->bCopyProtect);
2961 }
2962
2963 static void
2964 print_vs_frame_mjpeg_descriptor(
2965 const uvideo_vs_frame_mjpeg_descriptor_t *desc)
2966 {
2967 printf("MJPEG frame: "
2968 "Len=%d Type=0x%02x Subtype=0x%02x "
2969 "bFrameIndex=%d bmCapabilities=0x%02x "
2970 "wWidth=%d wHeight=%d dwMinBitRate=%u dwMaxBitRate=%u "
2971 "dwMaxVideoFrameBufferSize=%u dwDefaultFrameInterval=%u "
2972 "bFrameIntervalType=%d",
2973 desc->bLength,
2974 desc->bDescriptorType,
2975 desc->bDescriptorSubtype,
2976 desc->bFrameIndex,
2977 desc->bmCapabilities,
2978 UGETW(desc->wWidth),
2979 UGETW(desc->wHeight),
2980 UGETDW(desc->dwMinBitRate),
2981 UGETDW(desc->dwMaxBitRate),
2982 UGETDW(desc->dwMaxVideoFrameBufferSize),
2983 UGETDW(desc->dwDefaultFrameInterval),
2984 desc->bFrameIntervalType);
2985 }
2986
2987 static void
2988 print_vs_format_dv_descriptor(
2989 const uvideo_vs_format_dv_descriptor_t *desc)
2990 {
2991 printf("MJPEG format: "
2992 "Len=%d Type=0x%02x Subtype=0x%02x "
2993 "bFormatIndex=%d dwMaxVideoFrameBufferSize=%u "
2994 "bFormatType/Rate=%d bFormatType/Format=%d",
2995 desc->bLength,
2996 desc->bDescriptorType,
2997 desc->bDescriptorSubtype,
2998 desc->bFormatIndex,
2999 UGETDW(desc->dwMaxVideoFrameBufferSize),
3000 UVIDEO_GET_DV_FREQ(desc->bFormatType),
3001 UVIDEO_GET_DV_FORMAT(desc->bFormatType));
3002 }
3003
3004 #endif /* !UVIDEO_DEBUG */
3005
3006 #ifdef UVIDEO_DEBUG
3007 static void
3008 usb_guid_print(const usb_guid_t *guid)
3009 {
3010 printf("%04X-%02X-%02X-",
3011 UGETDW(guid->data1),
3012 UGETW(guid->data2),
3013 UGETW(guid->data3));
3014 printf("%02X%02X-",
3015 guid->data4[0],
3016 guid->data4[1]);
3017 printf("%02X%02X%02X%02X%02X%02X",
3018 guid->data4[2],
3019 guid->data4[3],
3020 guid->data4[4],
3021 guid->data4[5],
3022 guid->data4[6],
3023 guid->data4[7]);
3024 }
3025 #endif /* !UVIDEO_DEBUG */
3026
3027 /* Returns less than zero, zero, or greater than zero if uguid is less
3028 * than, equal to, or greater than guid. */
3029 static int
3030 usb_guid_cmp(const usb_guid_t *uguid, const guid_t *guid)
3031 {
3032 if (guid->data1 > UGETDW(uguid->data1))
3033 return 1;
3034 else if (guid->data1 < UGETDW(uguid->data1))
3035 return -1;
3036
3037 if (guid->data2 > UGETW(uguid->data2))
3038 return 1;
3039 else if (guid->data2 < UGETW(uguid->data2))
3040 return -1;
3041
3042 if (guid->data3 > UGETW(uguid->data3))
3043 return 1;
3044 else if (guid->data3 < UGETW(uguid->data3))
3045 return -1;
3046
3047 return memcmp(guid->data4, uguid->data4, 8);
3048 }
3049