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