uvideo.c revision 1.4 1 /* $NetBSD: uvideo.c,v 1.4 2008/09/18 02:57:07 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.4 2008/09/18 02:57:07 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 #define UVIDEO_NXFERS 3
79
80 /* #define UVIDEO_DEBUG */
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 const usb_guid_t *guid;
1190
1191 switch (format_desc->bDescriptorSubtype) {
1192 case UDESC_VS_FORMAT_UNCOMPRESSED:
1193 subtype = UDESC_VS_FRAME_UNCOMPRESSED;
1194 default_index = GET(uvideo_vs_format_uncompressed_descriptor_t,
1195 format_desc,
1196 bDefaultFrameIndex);
1197 break;
1198 case UDESC_VS_FORMAT_FRAME_BASED:
1199 subtype = UDESC_VS_FRAME_FRAME_BASED;
1200 default_index = GET(uvideo_format_frame_based_descriptor_t,
1201 format_desc,
1202 bDefaultFrameIndex);
1203 break;
1204
1205 break;
1206 case UDESC_VS_FORMAT_MJPEG:
1207 subtype = UDESC_VS_FRAME_MJPEG;
1208 default_index = GET(uvideo_vs_format_mjpeg_descriptor_t,
1209 format_desc,
1210 bDefaultFrameIndex);
1211 break;
1212 default:
1213 DPRINTF(("uvideo: unknown frame based format %d\n",
1214 format_desc->bDescriptorSubtype));
1215 return USBD_INVAL;
1216 }
1217
1218 /* Iterate through frame descriptors directly following the
1219 * format descriptor, and add a format to the format list for
1220 * each frame descriptor. */
1221 while ((uvdesc = (const uvideo_descriptor_t *) usb_desc_iter_peek_next(iter)) &&
1222 (uvdesc != NULL) && (uvdesc->bDescriptorSubtype == subtype))
1223 {
1224 uvdesc = (const uvideo_descriptor_t *) usb_desc_iter_next(iter);
1225
1226 format = kmem_alloc(sizeof(struct uvideo_format), KM_NOSLEEP);
1227 if (format == NULL) {
1228 DPRINTF(("uvideo: failed to alloc video format\n"));
1229 return USBD_NOMEM;
1230 }
1231
1232 switch (format_desc->bDescriptorSubtype) {
1233 case UDESC_VS_FORMAT_UNCOMPRESSED:
1234 guid = GETP(uvideo_vs_format_uncompressed_descriptor_t,
1235 format_desc,
1236 guidFormat);
1237
1238 if (usb_guid_cmp(guid,
1239 &uvideo_guid_format_yuy2) == 0) {
1240 format->format.pixel_format =
1241 VIDEO_FORMAT_YUY2;
1242 } else if (usb_guid_cmp(guid,
1243 &uvideo_guid_format_nv12) == 0) {
1244 format->format.pixel_format =
1245 VIDEO_FORMAT_NV12;
1246 } else {
1247 format->format.pixel_format =
1248 VIDEO_FORMAT_UNDEFINED;
1249 #ifdef UVIDEO_DEBUG
1250 if (uvideodebug) {
1251 DPRINTF(("uvideo: format undefined "));
1252 usb_guid_print(guid);
1253 DPRINTF(("\n"));
1254 }
1255 #endif
1256 }
1257
1258 UVIDEO_FORMAT_INIT_FRAME_BASED(
1259 uvideo_vs_format_uncompressed_descriptor_t,
1260 format_desc,
1261 uvideo_vs_frame_uncompressed_descriptor_t,
1262 uvdesc,
1263 format);
1264 index = GET(uvideo_vs_frame_uncompressed_descriptor_t,
1265 uvdesc,
1266 bFrameIndex);
1267 break;
1268 case UDESC_VS_FORMAT_MJPEG:
1269 format->format.pixel_format = VIDEO_FORMAT_MJPEG;
1270 UVIDEO_FORMAT_INIT_FRAME_BASED(
1271 uvideo_vs_format_mjpeg_descriptor_t,
1272 format_desc,
1273 uvideo_vs_frame_mjpeg_descriptor_t,
1274 uvdesc,
1275 format);
1276 index = GET(uvideo_vs_frame_mjpeg_descriptor_t,
1277 uvdesc,
1278 bFrameIndex);
1279 break;
1280 case UDESC_VS_FORMAT_FRAME_BASED:
1281 format->format.pixel_format = VIDEO_FORMAT_UNDEFINED;
1282 UVIDEO_FORMAT_INIT_FRAME_BASED(
1283 uvideo_format_frame_based_descriptor_t,
1284 format_desc,
1285 uvideo_frame_frame_based_descriptor_t,
1286 uvdesc,
1287 format);
1288 index = GET(uvideo_frame_frame_based_descriptor_t,
1289 uvdesc,
1290 bFrameIndex);
1291 break;
1292 default:
1293 /* shouldn't ever get here */
1294 DPRINTF(("uvideo: unknown frame based format %d\n",
1295 format_desc->bDescriptorSubtype));
1296 kmem_free(format, sizeof(struct uvideo_format));
1297 return USBD_INVAL;
1298 }
1299
1300 SIMPLEQ_INSERT_TAIL(&vs->vs_formats, format, entries);
1301
1302 if (vs->vs_default_format == NULL && index == default_index)
1303 vs->vs_default_format = &format->format;
1304 }
1305
1306 return USBD_NORMAL_COMPLETION;
1307 }
1308
1309 static int
1310 uvideo_stream_start_xfer(struct uvideo_stream *vs)
1311 {
1312 struct uvideo_softc *sc = vs->vs_parent;
1313 struct uvideo_bulk_xfer *bx;
1314 struct uvideo_isoc_xfer *ix;
1315 uint32_t vframe_len; /* rough bytes per video frame */
1316 uint32_t uframe_len; /* bytes per usb frame (TODO: or microframe?) */
1317 uint32_t nframes; /* number of usb frames (TODO: or microframs?) */
1318 int i;
1319
1320 struct uvideo_alternate *alt, *alt_maybe;
1321 usbd_status err;
1322
1323 switch (vs->vs_xfer_type) {
1324 case UE_BULK:
1325 bx = &vs->vs_xfer.bulk;
1326 return 0;
1327 case UE_ISOCHRONOUS:
1328 ix = &vs->vs_xfer.isoc;
1329
1330 vframe_len = vs->vs_max_frame_size; /* + nframes * HEADER */
1331 uframe_len = UVIDEO_FRAME_INTERVAL_UNITS_PER_USB_FRAME *
1332 vframe_len / vs->vs_frame_interval;
1333 nframes = (vframe_len / uframe_len);
1334 if (nframes == 0)
1335 nframes++;
1336
1337 nframes = 64; /* ehci requires power of 2. doesn't
1338 * work well with logitech/uhci */
1339
1340 /* Choose an alternate interface most suitable for
1341 * this format. Choose the smallest size that can
1342 * contain max_payload_size.
1343 *
1344 * It is assumed that the list is sorted in descending
1345 * order from largest to smallest packet size.
1346 *
1347 * TODO: what should the strategy be for choosing an
1348 * alt interface?
1349 */
1350 alt = NULL;
1351 SLIST_FOREACH(alt_maybe, &ix->ix_altlist, entries) {
1352 /* TODO: define "packet" and "payload". I think
1353 * several packets can make up one payload which would
1354 * call into question this method of selecting an
1355 * alternate interface... */
1356 if (alt_maybe->max_packet_size >=
1357 vs->vs_max_payload_size)
1358 alt = alt_maybe;
1359 else
1360 break;
1361 }
1362
1363 if (alt == NULL) {
1364 DPRINTF(("uvideo_stream_start_xfer: "
1365 "no suitable alternate interface found\n"));
1366 return EINVAL;
1367 }
1368
1369 DPRINTFN(15,("uvideo_stream_start_xfer: "
1370 "choosing alternate interface "
1371 "%d wMaxPacketSize=%d bInterval=%d\n",
1372 alt->altno, alt->max_packet_size, alt->interval));
1373
1374 err = usbd_set_interface(vs->vs_iface, alt->altno);
1375 if (err != USBD_NORMAL_COMPLETION) {
1376 DPRINTF(("uvideo_open: error setting alt interface: %s (%d)\n",
1377 usbd_errstr(err), err));
1378 return EIO;
1379 }
1380
1381 /* TODO: "packet" not same as frame */
1382 uframe_len = alt->max_packet_size;
1383
1384 ix->ix_nframes = nframes;
1385 ix->ix_uframe_len = uframe_len;
1386 for (i = 0; i < UVIDEO_NXFERS; i++) {
1387 struct uvideo_isoc *isoc = &ix->ix_i[i];
1388 isoc->i_frlengths =
1389 kmem_alloc(sizeof(isoc->i_frlengths[0]) * nframes,
1390 KM_SLEEP);
1391 if (isoc->i_frlengths == NULL) {
1392 DPRINTF(("uvideo: failed to alloc frlengths:"
1393 "%s (%d)\n",
1394 usbd_errstr(err), err));
1395 return ENOMEM;
1396 }
1397 }
1398
1399 err = usbd_open_pipe(vs->vs_iface, ix->ix_endpt,
1400 USBD_EXCLUSIVE_USE, &ix->ix_pipe);
1401 if (err != USBD_NORMAL_COMPLETION) {
1402 DPRINTF(("uvideo: error opening pipe: %s (%d)\n",
1403 usbd_errstr(err), err));
1404 return EIO;
1405 }
1406
1407 for (i = 0; i < UVIDEO_NXFERS; i++) {
1408 struct uvideo_isoc *isoc = &ix->ix_i[i];
1409 isoc->i_xfer = usbd_alloc_xfer(sc->sc_udev);
1410 if (isoc->i_xfer == NULL) {
1411 DPRINTF(("uvideo: failed to alloc xfer: %s"
1412 " (%d)\n",
1413 usbd_errstr(err), err));
1414 return ENOMEM;
1415 }
1416
1417 isoc->i_buf = usbd_alloc_buffer(isoc->i_xfer,
1418 nframes * uframe_len);
1419 if (isoc->i_xfer == NULL) {
1420 DPRINTF(("uvideo: failed to alloc buf: %s"
1421 " (%d)\n",
1422 usbd_errstr(err), err));
1423 return ENOMEM;
1424 }
1425 }
1426
1427 uvideo_stream_recv_isoc_start(vs);
1428
1429 return 0;
1430 default:
1431 /* should never get here */
1432 DPRINTF(("uvideo_stream_start_xfer: unknown xfer type 0x%x\n",
1433 vs->vs_xfer_type));
1434 return EINVAL;
1435 }
1436 }
1437
1438 static int
1439 uvideo_stream_stop_xfer(struct uvideo_stream *vs)
1440 {
1441 struct uvideo_bulk_xfer *bx;
1442 struct uvideo_isoc_xfer *ix;
1443 usbd_status err;
1444 int i;
1445
1446 switch (vs->vs_xfer_type) {
1447 case UE_BULK:
1448 bx = &vs->vs_xfer.bulk;
1449 return 0;
1450 case UE_ISOCHRONOUS:
1451 ix = &vs->vs_xfer.isoc;
1452 if (ix->ix_pipe != NULL) {
1453 usbd_abort_pipe(ix->ix_pipe);
1454 usbd_close_pipe(ix->ix_pipe);
1455 ix->ix_pipe = NULL;
1456 }
1457
1458 for (i = 0; i < UVIDEO_NXFERS; i++) {
1459 struct uvideo_isoc *isoc = &ix->ix_i[i];
1460 if (isoc->i_xfer != NULL) {
1461 usbd_free_buffer(isoc->i_xfer);
1462 usbd_free_xfer(isoc->i_xfer);
1463 isoc->i_xfer = NULL;
1464 }
1465
1466 if (isoc->i_frlengths != NULL) {
1467 kmem_free(isoc->i_frlengths,
1468 sizeof(isoc->i_frlengths[0]) *
1469 ix->ix_nframes);
1470 isoc->i_frlengths = NULL;
1471 }
1472 }
1473
1474 /* Give it some time to settle */
1475 usbd_delay_ms(vs->vs_parent->sc_udev, 1000);
1476
1477 /* Set to zero bandwidth alternate interface zero */
1478 err = usbd_set_interface(vs->vs_iface, 0);
1479 if (err != USBD_NORMAL_COMPLETION) {
1480 DPRINTF(("uvideo_stream_stop_transfer: "
1481 "error setting zero bandwidth interface: "
1482 "%s (%d)\n",
1483 usbd_errstr(err), err));
1484 return EIO;
1485 }
1486
1487 return 0;
1488 default:
1489 /* should never get here */
1490 DPRINTF(("uvideo_stream_stop_xfer: unknown xfer type 0x%x\n",
1491 vs->vs_xfer_type));
1492 return EINVAL;
1493 }
1494 }
1495
1496 static usbd_status
1497 uvideo_stream_recv_isoc_start(struct uvideo_stream *vs)
1498 {
1499 int i;
1500
1501 for (i = 0; i < UVIDEO_NXFERS; i++)
1502 uvideo_stream_recv_isoc_start1(&vs->vs_xfer.isoc.ix_i[i]);
1503
1504 return USBD_NORMAL_COMPLETION;
1505 }
1506
1507 /* Initiate a usb transfer. */
1508 static usbd_status
1509 uvideo_stream_recv_isoc_start1(struct uvideo_isoc *isoc)
1510 {
1511 struct uvideo_isoc_xfer *ix;
1512 usbd_status err;
1513 int i;
1514
1515 ix = isoc->i_ix;
1516
1517 for (i = 0; i < ix->ix_nframes; ++i)
1518 isoc->i_frlengths[i] = ix->ix_uframe_len;
1519
1520 usbd_setup_isoc_xfer(isoc->i_xfer,
1521 ix->ix_pipe,
1522 isoc,
1523 isoc->i_frlengths,
1524 ix->ix_nframes,
1525 USBD_NO_COPY | USBD_SHORT_XFER_OK,
1526 uvideo_stream_recv_isoc_complete);
1527
1528 err = usbd_transfer(isoc->i_xfer);
1529 if (err != USBD_IN_PROGRESS) {
1530 DPRINTF(("uvideo_stream_recv_start: "
1531 "usbd_transfer status=%s (%d)\n",
1532 usbd_errstr(err), err));
1533 }
1534 return err;
1535 }
1536
1537 /* Callback on completion of usb isoc transfer */
1538 static void
1539 uvideo_stream_recv_isoc_complete(usbd_xfer_handle xfer,
1540 usbd_private_handle priv,
1541 usbd_status status)
1542 {
1543 struct uvideo_stream *vs;
1544 struct uvideo_isoc_xfer *ix;
1545 struct uvideo_isoc *isoc;
1546 int i;
1547 uint32_t count;
1548 const uvideo_payload_header_t *hdr;
1549 uint8_t *buf;
1550 struct video_payload payload;
1551
1552 isoc = priv;
1553 vs = isoc->i_vs;
1554 ix = isoc->i_ix;
1555
1556 if (status != USBD_NORMAL_COMPLETION) {
1557 DPRINTF(("uvideo_stream_recv_isoc_complete: status=%s (%d)\n",
1558 usbd_errstr(status), status));
1559
1560 if (status == USBD_STALLED)
1561 usbd_clear_endpoint_stall_async(ix->ix_pipe);
1562 else
1563 return;
1564 } else {
1565 usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
1566
1567 if (count == 0) {
1568 /* DPRINTF(("uvideo: zero length transfer\n")); */
1569 /* return; */ /* TODO: what to do here? restarting
1570 * can lead to infinite loop */
1571 }
1572
1573 hdr = (const uvideo_payload_header_t *)isoc->i_buf;
1574
1575 for (i = 0, buf = isoc->i_buf;
1576 i < ix->ix_nframes;
1577 ++i, buf += ix->ix_uframe_len)
1578 {
1579 if (isoc->i_frlengths[i] == 0)
1580 continue;
1581
1582 hdr = (uvideo_payload_header_t *)buf;
1583
1584 payload.data = buf + hdr->bHeaderLength;
1585 payload.size = isoc->i_frlengths[i] -
1586 hdr->bHeaderLength;
1587 payload.frameno = hdr->bmHeaderInfo & UV_FRAME_ID;
1588 payload.end_of_frame =
1589 hdr->bmHeaderInfo & UV_END_OF_FRAME;
1590
1591 video_submit_payload(vs->vs_parent->sc_videodev,
1592 &payload);
1593 }
1594 }
1595
1596 uvideo_stream_recv_isoc_start1(isoc);
1597 }
1598
1599
1600 /*
1601 * uvideo_open - probe and commit video format and start receiving
1602 * video data
1603 */
1604 static int
1605 uvideo_open(void *addr, int flags)
1606 {
1607 struct uvideo_softc *sc;
1608
1609 sc = addr;
1610
1611 DPRINTF(("uvideo_open: sc=%p\n", sc));
1612 if (sc->sc_dying)
1613 return EIO;
1614
1615 return 0;
1616 }
1617
1618
1619 static void
1620 uvideo_close(void *addr)
1621 {
1622 struct uvideo_softc *sc;
1623
1624 sc = addr;
1625
1626 if (sc->sc_state != UVIDEO_STATE_CLOSED) {
1627 sc->sc_state = UVIDEO_STATE_CLOSED;
1628 }
1629 }
1630
1631 static const char *
1632 uvideo_get_devname(void *addr)
1633 {
1634 struct uvideo_softc *sc = addr;
1635 return sc->sc_devname;
1636 }
1637
1638 static int
1639 uvideo_enum_format(void *addr, uint32_t index, struct video_format *format)
1640 {
1641 struct uvideo_softc *sc = addr;
1642 struct uvideo_stream *vs = sc->sc_stream_in;
1643
1644 if (sc->sc_dying)
1645 return EIO;
1646
1647 if (index != 0)
1648 return EINVAL;
1649
1650 /* TODO: actually enumerate formats */
1651 *format = *vs->vs_default_format;
1652
1653 return 0;
1654 }
1655
1656 /*
1657 * uvideo_get_format
1658 */
1659 static int
1660 uvideo_get_format(void *addr, struct video_format *format)
1661 {
1662 struct uvideo_softc *sc = addr;
1663 struct uvideo_stream *vs = sc->sc_stream_in;
1664
1665 if (sc->sc_dying)
1666 return EIO;
1667
1668 if (vs->vs_current_format.priv != -1)
1669 *format = vs->vs_current_format;
1670 else
1671 *format = *vs->vs_default_format;
1672
1673 return 0;
1674 }
1675
1676
1677 /*
1678 * uvideo_set_format - TODO: this is boken and does nothing
1679 */
1680 static int
1681 uvideo_set_format(void *addr, struct video_format *format)
1682 {
1683 struct uvideo_softc *sc;
1684 struct uvideo_stream *vs;
1685 struct uvideo_format *uvfmt;
1686 uvideo_probe_and_commit_data_t probe;
1687 uint8_t ifaceno;
1688 usbd_status err;
1689
1690 sc = addr;
1691
1692 DPRINTF(("uvideo_set_format: sc=%p\n", sc));
1693 if (sc->sc_dying)
1694 return EIO;
1695
1696 vs = sc->sc_stream_in;
1697 ifaceno = vs->vs_ifaceno;
1698
1699 uvideo_init_probe_data(&probe);
1700
1701 /* If format comes from a previously enumerated format from
1702 this driver then it should have format and frame
1703 indicies. */
1704 /*
1705 probe.bFormatIndex = UVIDEO_FORMAT_GET_FORMAT_INDEX(
1706 (struct uvideo_format *)format);
1707 probe.bFrameIndex = UVIDEO_FORMAT_GET_FRAME_INDEX(
1708 (struct uvideo_format *)format);
1709 */
1710
1711 /* First step is to probe/SET_CUR. Prior to this, state is
1712 * undefined. TODO: this should be moved or changed so that
1713 * it isn't done on every set_format, only the first one.*/
1714
1715 /* Xbox camera does not like zeros during SET_CUR, at least
1716 * for FormatIndex, FrameIndex, and FrameInterval. For now,
1717 * initialize with the default values. */
1718 err = uvideo_stream_probe(vs, UR_GET_DEF, &probe);
1719 if (err != USBD_NORMAL_COMPLETION) {
1720 DPRINTF(("uvideo: error probe/GET_DEF: %s (%d)\n",
1721 usbd_errstr(err), err));
1722 }
1723 DPRINTFN(15, ("uvideo_set_format: default format is: "
1724 "bmHint=0x%04x bFormatIndex=%d bFrameIndex=%d "
1725 "dwFrameInterval=%d wKeyFrameRate=%d wPFrameRate=%d "
1726 "wCompQuality=%d wCompWindowSize=%d wDelay=%d "
1727 "dwMaxVideoFrameSize=%d dwMaxPayloadTransferSize=%d\n",
1728 UGETW(probe.bmHint),
1729 probe.bFormatIndex,
1730 probe.bFrameIndex,
1731 UGETDW(probe.dwFrameInterval),
1732 UGETW(probe.wKeyFrameRate),
1733 UGETW(probe.wPFrameRate),
1734 UGETW(probe.wCompQuality),
1735 UGETW(probe.wCompWindowSize),
1736 UGETW(probe.wDelay),
1737 UGETDW(probe.dwMaxVideoFrameSize),
1738 UGETDW(probe.dwMaxPayloadTransferSize)));
1739
1740 err = uvideo_stream_probe(vs, UR_SET_CUR, &probe);
1741 if (err != USBD_NORMAL_COMPLETION) {
1742 DPRINTF(("uvideo_open: error initializing format: %s (%d)\n",
1743 usbd_errstr(err), err));
1744 return EIO;
1745 }
1746
1747 /* Second step is probe/GET_CUR to see what the hardware is
1748 * offering. */
1749 uvideo_init_probe_data(&probe);
1750 err = uvideo_stream_probe(vs, UR_GET_CUR, &probe);
1751 if (err != USBD_NORMAL_COMPLETION) {
1752 DPRINTF(("uvideo: error probe/GET_CUR: %s (%d)\n",
1753 usbd_errstr(err), err));
1754 }
1755
1756 /* Third step is commit/SET_CUR. Fourth step is to set the
1757 * alternate interface. Currently the fourth step is in
1758 * uvideo_start_transfer. Maybe move it here? */
1759 err = uvideo_stream_commit(vs, UR_SET_CUR, &probe);
1760 if (err != USBD_NORMAL_COMPLETION) {
1761 DPRINTF(("uvideo: error commit/SET_CUR: %s (%d)\n",
1762 usbd_errstr(err), err));
1763 return EIO;
1764 }
1765
1766 DPRINTFN(15, ("uvideo_set_format: committing to format: "
1767 "bmHint=0x%04x bFormatIndex=%d bFrameIndex=%d "
1768 "dwFrameInterval=%d wKeyFrameRate=%d wPFrameRate=%d "
1769 "wCompQuality=%d wCompWindowSize=%d wDelay=%d "
1770 "dwMaxVideoFrameSize=%d dwMaxPayloadTransferSize=%d",
1771 UGETW(probe.bmHint),
1772 probe.bFormatIndex,
1773 probe.bFrameIndex,
1774 UGETDW(probe.dwFrameInterval),
1775 UGETW(probe.wKeyFrameRate),
1776 UGETW(probe.wPFrameRate),
1777 UGETW(probe.wCompQuality),
1778 UGETW(probe.wCompWindowSize),
1779 UGETW(probe.wDelay),
1780 UGETDW(probe.dwMaxVideoFrameSize),
1781 UGETDW(probe.dwMaxPayloadTransferSize)));
1782 if (vs->vs_probelen == 34) {
1783 DPRINTFN(15, (" dwClockFrequency=%d bmFramingInfo=0x%02x "
1784 "bPreferedVersion=%d bMinVersion=%d "
1785 "bMaxVersion=%d",
1786 UGETDW(probe.dwClockFrequency),
1787 probe.bmFramingInfo,
1788 probe.bPreferedVersion,
1789 probe.bMinVersion,
1790 probe.bMaxVersion));
1791 }
1792 DPRINTFN(15, ("\n"));
1793
1794 vs->vs_frame_interval = UGETDW(probe.dwFrameInterval);
1795 vs->vs_max_payload_size = UGETDW(probe.dwMaxPayloadTransferSize);
1796
1797 uvfmt = uvideo_stream_find_format(vs, probe.bFormatIndex,
1798 probe.bFrameIndex);
1799 if (uvfmt != NULL) {
1800 *format = uvfmt->format;
1801 } else {
1802 DPRINTF(("uvideo_set_format: matching format not found\n"));
1803 *format = *vs->vs_default_format;
1804 }
1805
1806 DPRINTF(("uvideo_set_format: pixeltype is %d\n", format->pixel_format));
1807
1808 format->sample_size = UGETDW(probe.dwMaxVideoFrameSize);
1809 /* format->stride = width * bits_per_px / 8 */
1810 /* format->color.primaries = */
1811 /* format->color.gamma_function = */
1812 /* format->color.matrix_coeff = */
1813 /* format->interlace_flags = */
1814
1815 vs->vs_current_format = *format;
1816
1817 return 0;
1818 }
1819
1820
1821 static int
1822 uvideo_start_transfer(void *addr)
1823 {
1824 struct uvideo_softc *sc = addr;
1825 struct uvideo_stream *vs;
1826 int s, err;
1827
1828 /* FIXME: this functions should be stream specific */
1829 vs = SLIST_FIRST(&sc->sc_stream_list);
1830 s = splusb();
1831 err = uvideo_stream_start_xfer(vs);
1832 splx(s);
1833
1834 return err;
1835 }
1836
1837 static int
1838 uvideo_stop_transfer(void *addr)
1839 {
1840 struct uvideo_softc *sc;
1841 int err;
1842
1843 sc = addr;
1844
1845 err = uvideo_stream_stop_xfer(sc->sc_stream_in);
1846 return err;
1847 }
1848
1849
1850 static int
1851 uvideo_get_control_group(void *addr, struct video_control_group *group)
1852 {
1853 struct uvideo_softc *sc;
1854 usb_device_request_t req;
1855 usbd_status err;
1856 uint8_t control_id, ent_id, data[16];
1857 uint16_t len;
1858 int s;
1859
1860 sc = addr;
1861
1862 /* request setup */
1863 switch (group->group_id) {
1864 case VIDEO_CONTROL_PANTILT_RELATIVE:
1865 if (group->length != 4)
1866 return EINVAL;
1867
1868 return EINVAL;
1869 case VIDEO_CONTROL_SHARPNESS:
1870 if (group->length != 1)
1871 return EINVAL;
1872
1873 control_id = UVIDEO_PU_SHARPNESS_CONTROL;
1874 ent_id = 2; /* TODO: hardcoded logitech processing unit */
1875 len = 2;
1876 break;
1877 default:
1878 return EINVAL;
1879 }
1880
1881 /* do request */
1882 req.bmRequestType = UVIDEO_REQUEST_TYPE_INTERFACE |
1883 UVIDEO_REQUEST_TYPE_CLASS_SPECIFIC |
1884 UVIDEO_REQUEST_TYPE_GET;
1885 req.bRequest = UR_GET_CUR;
1886 USETW(req.wValue, control_id << 8);
1887 USETW(req.wIndex, (ent_id << 8) | sc->sc_ifaceno);
1888 USETW(req.wLength, len);
1889
1890 s = splusb();
1891 err = usbd_do_request(sc->sc_udev, &req, data);
1892 splx(s);
1893 if (err != USBD_NORMAL_COMPLETION) {
1894 DPRINTF(("uvideo_set_control: error %s (%d)\n",
1895 usbd_errstr(err), err));
1896 return EIO; /* TODO: more detail here? */
1897 }
1898
1899 /* extract request data */
1900 switch (group->group_id) {
1901 case VIDEO_CONTROL_SHARPNESS:
1902 group->control[0].value = UGETW(data);
1903 break;
1904 default:
1905 return EINVAL;
1906 }
1907
1908 return 0;
1909 }
1910
1911
1912 static int
1913 uvideo_set_control_group(void *addr, const struct video_control_group *group)
1914 {
1915 struct uvideo_softc *sc;
1916 usb_device_request_t req;
1917 usbd_status err;
1918 uint8_t control_id, ent_id, data[16]; /* long enough for all controls */
1919 uint16_t len;
1920 int s;
1921
1922 sc = addr;
1923
1924 switch (group->group_id) {
1925 case VIDEO_CONTROL_PANTILT_RELATIVE:
1926 if (group->length != 4)
1927 return EINVAL;
1928
1929 if (group->control[0].value != 0 ||
1930 group->control[0].value != 1 ||
1931 group->control[0].value != 0xff)
1932 return ERANGE;
1933
1934 if (group->control[2].value != 0 ||
1935 group->control[2].value != 1 ||
1936 group->control[2].value != 0xff)
1937 return ERANGE;
1938
1939 control_id = UVIDEO_CT_PANTILT_RELATIVE_CONTROL;
1940 ent_id = 1; /* TODO: hardcoded logitech camera terminal */
1941 len = 4;
1942 data[0] = group->control[0].value;
1943 data[1] = group->control[1].value;
1944 data[2] = group->control[2].value;
1945 data[3] = group->control[3].value;
1946 break;
1947 case VIDEO_CONTROL_BRIGHTNESS:
1948 if (group->length != 1)
1949 return EINVAL;
1950 control_id = UVIDEO_PU_BRIGHTNESS_CONTROL;
1951 ent_id = 2;
1952 len = 2;
1953 USETW(data, group->control[0].value);
1954 break;
1955 case VIDEO_CONTROL_GAIN:
1956 if (group->length != 1)
1957 return EINVAL;
1958 control_id = UVIDEO_PU_GAIN_CONTROL;
1959 ent_id = 2;
1960 len = 2;
1961 USETW(data, group->control[0].value);
1962 break;
1963 case VIDEO_CONTROL_SHARPNESS:
1964 if (group->length != 1)
1965 return EINVAL;
1966 control_id = UVIDEO_PU_SHARPNESS_CONTROL;
1967 ent_id = 2; /* TODO: hardcoded logitech processing unit */
1968 len = 2;
1969 USETW(data, group->control[0].value);
1970 break;
1971 default:
1972 return EINVAL;
1973 }
1974
1975 req.bmRequestType = UVIDEO_REQUEST_TYPE_INTERFACE |
1976 UVIDEO_REQUEST_TYPE_CLASS_SPECIFIC |
1977 UVIDEO_REQUEST_TYPE_SET;
1978 req.bRequest = UR_SET_CUR;
1979 USETW(req.wValue, control_id << 8);
1980 USETW(req.wIndex, (ent_id << 8) | sc->sc_ifaceno);
1981 USETW(req.wLength, len);
1982
1983 s = splusb();
1984 err = usbd_do_request(sc->sc_udev, &req, data);
1985 splx(s);
1986 if (err != USBD_NORMAL_COMPLETION) {
1987 DPRINTF(("uvideo_set_control: error %s (%d)\n",
1988 usbd_errstr(err), err));
1989 return EIO; /* TODO: more detail here? */
1990 }
1991
1992 return 0;
1993 }
1994
1995 static usbd_status
1996 uvideo_stream_probe_and_commit(struct uvideo_stream *vs,
1997 uint8_t action, uint8_t control,
1998 void *data)
1999 {
2000 usb_device_request_t req;
2001
2002 switch (action) {
2003 case UR_SET_CUR:
2004 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
2005 USETW(req.wLength, vs->vs_probelen);
2006 break;
2007 case UR_GET_CUR:
2008 case UR_GET_MIN:
2009 case UR_GET_MAX:
2010 case UR_GET_DEF:
2011 req.bmRequestType = UT_READ_CLASS_INTERFACE;
2012 USETW(req.wLength, vs->vs_probelen);
2013 break;
2014 case UR_GET_INFO:
2015 req.bmRequestType = UT_READ_CLASS_INTERFACE;
2016 USETW(req.wLength, sizeof(uByte));
2017 break;
2018 case UR_GET_LEN:
2019 req.bmRequestType = UT_READ_CLASS_INTERFACE;
2020 USETW(req.wLength, sizeof(uWord)); /* is this right? */
2021 break;
2022 default:
2023 DPRINTF(("uvideo_probe_and_commit: "
2024 "unknown request action %d\n", action));
2025 return USBD_NOT_STARTED;
2026 }
2027
2028 req.bRequest = action;
2029 USETW2(req.wValue, control, 0);
2030 USETW2(req.wIndex, 0, vs->vs_ifaceno);
2031
2032 return (usbd_do_request_flags(vs->vs_parent->sc_udev, &req, data,
2033 0, 0,
2034 USBD_DEFAULT_TIMEOUT));
2035 }
2036
2037 static void
2038 uvideo_init_probe_data(uvideo_probe_and_commit_data_t *probe)
2039 {
2040 /* all zeroes tells camera to choose what it wants */
2041 memset(probe, 0, sizeof(*probe));
2042 }
2043
2044
2045 #ifdef _MODULE
2046
2047 MODULE(MODULE_CLASS_DRIVER, uvideo, "usb");
2048
2049 CFDRIVER_DECL(uvideo, DV_DULL, NULL);
2050 extern struct cfattach uvideo_ca;
2051 static struct cfdata uvideo_cfdata[] = {
2052 {
2053 .cf_name = "uvideo",
2054 .cf_atname = "uvideo",
2055 .cf_unit = 0,
2056 .cf_fstate = FSTATE_STAR,
2057 .cf_loc = NULL,
2058 .cf_flags = 0,
2059 .cf_pspec = NULL,
2060 },
2061 { NULL }
2062 };
2063
2064 static int
2065 uvideo_modcmd(modcmd_t cmd, void *arg)
2066 {
2067 int err;
2068
2069
2070 switch (cmd) {
2071 case MODULE_CMD_INIT:
2072 DPRINTF(("uvideo: attempting to load\n"));
2073
2074 err = config_cfdriver_attach(&uvideo_cd);
2075 if (err)
2076 return err;
2077 err = config_cfattach_attach("uvideo", &uvideo_ca);
2078 if (err) {
2079 config_cfdriver_detach(&uvideo_cd);
2080 return err;
2081 }
2082 err = config_cfdata_attach(uvideo_cfdata, 1);
2083 if (err) {
2084 config_cfattach_detach("uvideo", &uvideo_ca);
2085 config_cfdriver_detach(&uvideo_cd);
2086 return err;
2087 }
2088 DPRINTF(("uvideo: loaded module\n"));
2089 return 0;
2090 case MODULE_CMD_FINI:
2091 DPRINTF(("uvideo: attempting to unload module\n"));
2092 err = config_cfdriver_detach(&uvideo_cd);
2093 if (err)
2094 return err;
2095 config_cfattach_detach("uvideo", &uvideo_ca);
2096 config_cfdriver_detach(&uvideo_cd);
2097 DPRINTF(("uvideo: module unload\n"));
2098 return 0;
2099 default:
2100 return ENOTTY;
2101 }
2102 }
2103
2104 #endif /* _MODULE */
2105
2106
2107 #ifdef UVIDEO_DEBUG
2108 /* Some functions to print out descriptors. Mostly useless other than
2109 * debugging/exploration purposes. */
2110
2111
2112 static void
2113 print_bitmap(const uByte *start, uByte nbytes)
2114 {
2115 int byte, bit;
2116
2117 /* most significant first */
2118 for (byte = nbytes-1; byte >= 0; --byte) {
2119 if (byte < nbytes-1) printf("-");
2120 for (bit = 7; bit >= 0; --bit)
2121 printf("%01d", (start[byte] >> bit) &1);
2122 }
2123 }
2124
2125 static void
2126 print_descriptor(const usb_descriptor_t *desc)
2127 {
2128 static int current_class = -1;
2129 static int current_subclass = -1;
2130
2131 if (desc->bDescriptorType == UDESC_INTERFACE) {
2132 const usb_interface_descriptor_t *id;
2133 id = (const usb_interface_descriptor_t *)desc;
2134 current_class = id->bInterfaceClass;
2135 current_subclass = id->bInterfaceSubClass;
2136 print_interface_descriptor(id);
2137 printf("\n");
2138 return;
2139 }
2140
2141 printf(" "); /* indent */
2142
2143 if (current_class == UICLASS_VIDEO) {
2144 switch (current_subclass) {
2145 case UISUBCLASS_VIDEOCONTROL:
2146 print_vc_descriptor(desc);
2147 break;
2148 case UISUBCLASS_VIDEOSTREAMING:
2149 print_vs_descriptor(desc);
2150 break;
2151 case UISUBCLASS_VIDEOCOLLECTION:
2152 printf("uvc collection: len=%d type=0x%02x",
2153 desc->bLength, desc->bDescriptorType);
2154 break;
2155 }
2156 } else {
2157 printf("non uvc descriptor len=%d type=0x%02x",
2158 desc->bLength, desc->bDescriptorType);
2159 }
2160
2161 printf("\n");
2162 }
2163
2164 static void
2165 print_vc_descriptor(const usb_descriptor_t *desc)
2166 {
2167 const uvideo_descriptor_t *vcdesc;
2168
2169 printf("VC ");
2170
2171 switch (desc->bDescriptorType) {
2172 case UDESC_ENDPOINT:
2173 print_endpoint_descriptor(
2174 (const usb_endpoint_descriptor_t *)desc);
2175 break;
2176 case UDESC_CS_INTERFACE:
2177 vcdesc = (const uvideo_descriptor_t *)desc;
2178 switch (vcdesc->bDescriptorSubtype) {
2179 case UDESC_VC_HEADER:
2180 print_vc_header_descriptor(
2181 (const uvideo_vc_header_descriptor_t *)
2182 vcdesc);
2183 break;
2184 case UDESC_INPUT_TERMINAL:
2185 switch (UGETW(
2186 ((const uvideo_input_terminal_descriptor_t *)
2187 vcdesc)->wTerminalType)) {
2188 case UVIDEO_ITT_CAMERA:
2189 print_camera_terminal_descriptor(
2190 (const uvideo_camera_terminal_descriptor_t *)vcdesc);
2191 break;
2192 default:
2193 print_input_terminal_descriptor(
2194 (const uvideo_input_terminal_descriptor_t *)vcdesc);
2195 break;
2196 }
2197 break;
2198 case UDESC_OUTPUT_TERMINAL:
2199 print_output_terminal_descriptor(
2200 (const uvideo_output_terminal_descriptor_t *)
2201 vcdesc);
2202 break;
2203 case UDESC_SELECTOR_UNIT:
2204 print_selector_unit_descriptor(
2205 (const uvideo_selector_unit_descriptor_t *)
2206 vcdesc);
2207 break;
2208 case UDESC_PROCESSING_UNIT:
2209 print_processing_unit_descriptor(
2210 (const uvideo_processing_unit_descriptor_t *)
2211 vcdesc);
2212 break;
2213 case UDESC_EXTENSION_UNIT:
2214 print_extension_unit_descriptor(
2215 (const uvideo_extension_unit_descriptor_t *)
2216 vcdesc);
2217 break;
2218 default:
2219 printf("class specific interface "
2220 "len=%d type=0x%02x subtype=0x%02x",
2221 vcdesc->bLength,
2222 vcdesc->bDescriptorType,
2223 vcdesc->bDescriptorSubtype);
2224 break;
2225 }
2226 break;
2227 case UDESC_CS_ENDPOINT:
2228 vcdesc = (const uvideo_descriptor_t *)desc;
2229 switch (vcdesc->bDescriptorSubtype) {
2230 case UDESC_VC_INTERRUPT_ENDPOINT:
2231 print_interrupt_endpoint_descriptor(
2232 (const uvideo_vc_interrupt_endpoint_descriptor_t *)
2233 vcdesc);
2234 break;
2235 default:
2236 printf("class specific endpoint "
2237 "len=%d type=0x%02x subtype=0x%02x",
2238 vcdesc->bLength,
2239 vcdesc->bDescriptorType,
2240 vcdesc->bDescriptorSubtype);
2241 break;
2242 }
2243 break;
2244 default:
2245 printf("unknown: len=%d type=0x%02x",
2246 desc->bLength, desc->bDescriptorType);
2247 break;
2248 }
2249 }
2250
2251 static void
2252 print_vs_descriptor(const usb_descriptor_t *desc)
2253 {
2254 const uvideo_descriptor_t * vsdesc;
2255 printf("VS ");
2256
2257 switch (desc->bDescriptorType) {
2258 case UDESC_ENDPOINT:
2259 print_endpoint_descriptor(
2260 (const usb_endpoint_descriptor_t *)desc);
2261 break;
2262 case UDESC_CS_INTERFACE:
2263 vsdesc = (const uvideo_descriptor_t *)desc;
2264 switch (vsdesc->bDescriptorSubtype) {
2265 case UDESC_VS_INPUT_HEADER:
2266 print_vs_input_header_descriptor(
2267 (const uvideo_vs_input_header_descriptor_t *)
2268 vsdesc);
2269 break;
2270 case UDESC_VS_OUTPUT_HEADER:
2271 print_vs_output_header_descriptor(
2272 (const uvideo_vs_output_header_descriptor_t *)
2273 vsdesc);
2274 break;
2275 case UDESC_VS_FORMAT_UNCOMPRESSED:
2276 print_vs_format_uncompressed_descriptor(
2277 (const uvideo_vs_format_uncompressed_descriptor_t *)
2278 vsdesc);
2279 break;
2280 case UDESC_VS_FRAME_UNCOMPRESSED:
2281 print_vs_frame_uncompressed_descriptor(
2282 (const uvideo_vs_frame_uncompressed_descriptor_t *)
2283 vsdesc);
2284 break;
2285 case UDESC_VS_FORMAT_MJPEG:
2286 print_vs_format_mjpeg_descriptor(
2287 (const uvideo_vs_format_mjpeg_descriptor_t *)
2288 vsdesc);
2289 break;
2290 case UDESC_VS_FRAME_MJPEG:
2291 print_vs_frame_mjpeg_descriptor(
2292 (const uvideo_vs_frame_mjpeg_descriptor_t *)
2293 vsdesc);
2294 break;
2295 case UDESC_VS_FORMAT_DV:
2296 print_vs_format_dv_descriptor(
2297 (const uvideo_vs_format_dv_descriptor_t *)
2298 vsdesc);
2299 break;
2300 default:
2301 printf("unknown cs interface: len=%d type=0x%02x "
2302 "subtype=0x%02x",
2303 vsdesc->bLength, vsdesc->bDescriptorType,
2304 vsdesc->bDescriptorSubtype);
2305 }
2306 break;
2307 default:
2308 printf("unknown: len=%d type=0x%02x",
2309 desc->bLength, desc->bDescriptorType);
2310 break;
2311 }
2312 }
2313
2314 static void
2315 print_interface_descriptor(const usb_interface_descriptor_t *id)
2316 {
2317 printf("Interface: Len=%d Type=0x%02x "
2318 "bInterfaceNumber=0x%02x "
2319 "bAlternateSetting=0x%02x bNumEndpoints=0x%02x "
2320 "bInterfaceClass=0x%02x bInterfaceSubClass=0x%02x "
2321 "bInterfaceProtocol=0x%02x iInterface=0x%02x",
2322 id->bLength,
2323 id->bDescriptorType,
2324 id->bInterfaceNumber,
2325 id->bAlternateSetting,
2326 id->bNumEndpoints,
2327 id->bInterfaceClass,
2328 id->bInterfaceSubClass,
2329 id->bInterfaceProtocol,
2330 id->iInterface);
2331 }
2332
2333 static void
2334 print_endpoint_descriptor(const usb_endpoint_descriptor_t *desc)
2335 {
2336 printf("Endpoint: Len=%d Type=0x%02x "
2337 "bEndpointAddress=0x%02x ",
2338 desc->bLength,
2339 desc->bDescriptorType,
2340 desc->bEndpointAddress);
2341 printf("bmAttributes=");
2342 print_bitmap(&desc->bmAttributes, 1);
2343 printf(" wMaxPacketSize=%d bInterval=%d",
2344 UGETW(desc->wMaxPacketSize),
2345 desc->bInterval);
2346 }
2347
2348 static void
2349 print_vc_header_descriptor(
2350 const uvideo_vc_header_descriptor_t *desc)
2351 {
2352 printf("Interface Header: "
2353 "Len=%d Type=0x%02x Subtype=0x%02x "
2354 "bcdUVC=%d wTotalLength=%d "
2355 "dwClockFrequency=%d bInCollection=%d",
2356 desc->bLength,
2357 desc->bDescriptorType,
2358 desc->bDescriptorSubtype,
2359 UGETW(desc->bcdUVC),
2360 UGETW(desc->wTotalLength),
2361 UGETDW(desc->dwClockFrequency),
2362 desc->bInCollection);
2363 }
2364
2365 static void
2366 print_input_terminal_descriptor(
2367 const uvideo_input_terminal_descriptor_t *desc)
2368 {
2369 printf("Input Terminal: "
2370 "Len=%d Type=0x%02x Subtype=0x%02x "
2371 "bTerminalID=%d wTerminalType=%x bAssocTerminal=%d "
2372 "iTerminal=%d",
2373 desc->bLength,
2374 desc->bDescriptorType,
2375 desc->bDescriptorSubtype,
2376 desc->bTerminalID,
2377 UGETW(desc->wTerminalType),
2378 desc->bAssocTerminal,
2379 desc->iTerminal);
2380 }
2381
2382 static void
2383 print_output_terminal_descriptor(
2384 const uvideo_output_terminal_descriptor_t *desc)
2385 {
2386 printf("Output Terminal: "
2387 "Len=%d Type=0x%02x Subtype=0x%02x "
2388 "bTerminalID=%d wTerminalType=%x bAssocTerminal=%d "
2389 "bSourceID=%d iTerminal=%d",
2390 desc->bLength,
2391 desc->bDescriptorType,
2392 desc->bDescriptorSubtype,
2393 desc->bTerminalID,
2394 UGETW(desc->wTerminalType),
2395 desc->bAssocTerminal,
2396 desc->bSourceID,
2397 desc->iTerminal);
2398 }
2399
2400 static void
2401 print_camera_terminal_descriptor(
2402 const uvideo_camera_terminal_descriptor_t *desc)
2403 {
2404 printf("Camera Terminal: "
2405 "Len=%d Type=0x%02x Subtype=0x%02x "
2406 "bTerminalID=%d wTerminalType=%x bAssocTerminal=%d "
2407 "iTerminal=%d "
2408 "wObjectiveFocalLengthMin/Max=%d/%d "
2409 "wOcularFocalLength=%d "
2410 "bControlSize=%d ",
2411 desc->bLength,
2412 desc->bDescriptorType,
2413 desc->bDescriptorSubtype,
2414 desc->bTerminalID,
2415 UGETW(desc->wTerminalType),
2416 desc->bAssocTerminal,
2417 desc->iTerminal,
2418 UGETW(desc->wObjectiveFocalLengthMin),
2419 UGETW(desc->wObjectiveFocalLengthMax),
2420 UGETW(desc->wOcularFocalLength),
2421 desc->bControlSize);
2422 printf("bmControls=");
2423 print_bitmap(desc->bmControls, desc->bControlSize);
2424 }
2425
2426 static void
2427 print_selector_unit_descriptor(
2428 const uvideo_selector_unit_descriptor_t *desc)
2429 {
2430 int i;
2431 const uByte *b;
2432 printf("Selector Unit: "
2433 "Len=%d Type=0x%02x Subtype=0x%02x "
2434 "bUnitID=%d bNrInPins=%d ",
2435 desc->bLength,
2436 desc->bDescriptorType,
2437 desc->bDescriptorSubtype,
2438 desc->bUnitID,
2439 desc->bNrInPins);
2440 printf("baSourceIDs=");
2441 b = &desc->baSourceID[0];
2442 for (i = 0; i < desc->bNrInPins; ++i)
2443 printf("%d ", *b++);
2444 printf("iSelector=%d", *b);
2445 }
2446
2447 static void
2448 print_processing_unit_descriptor(
2449 const uvideo_processing_unit_descriptor_t *desc)
2450 {
2451 const uByte *b;
2452
2453 printf("Processing Unit: "
2454 "Len=%d Type=0x%02x Subtype=0x%02x "
2455 "bUnitID=%d bSourceID=%d wMaxMultiplier=%d bControlSize=%d ",
2456 desc->bLength,
2457 desc->bDescriptorType,
2458 desc->bDescriptorSubtype,
2459 desc->bUnitID,
2460 desc->bSourceID,
2461 UGETW(desc->wMaxMultiplier),
2462 desc->bControlSize);
2463 printf("bmControls=");
2464 print_bitmap(desc->bmControls, desc->bControlSize);
2465 b = &desc->bControlSize + desc->bControlSize + 1;
2466 printf(" iProcessing=%d bmVideoStandards=", *b);
2467 b += 1;
2468 print_bitmap(b, 1);
2469 }
2470
2471 static void
2472 print_extension_unit_descriptor(
2473 const uvideo_extension_unit_descriptor_t *desc)
2474 {
2475 const uByte * byte;
2476 uByte controlbytes;
2477 int i;
2478
2479 printf("Extension Unit: "
2480 "Len=%d Type=0x%02x Subtype=0x%02x "
2481 "bUnitID=%d ",
2482 desc->bLength,
2483 desc->bDescriptorType,
2484 desc->bDescriptorSubtype,
2485 desc->bUnitID);
2486
2487 printf("guidExtensionCode=");
2488 usb_guid_print(&desc->guidExtensionCode);
2489 printf(" ");
2490
2491 printf("bNumControls=%d bNrInPins=%d ",
2492 desc->bNumControls,
2493 desc->bNrInPins);
2494
2495 printf("baSourceIDs=");
2496 byte = &desc->baSourceID[0];
2497 for (i = 0; i < desc->bNrInPins; ++i)
2498 printf("%d ", *byte++);
2499
2500 controlbytes = *byte++;
2501 printf("bControlSize=%d ", controlbytes);
2502 printf("bmControls=");
2503 print_bitmap(byte, controlbytes);
2504
2505 byte += controlbytes;
2506 printf(" iExtension=%d", *byte);
2507 }
2508
2509 static void
2510 print_interrupt_endpoint_descriptor(
2511 const uvideo_vc_interrupt_endpoint_descriptor_t *desc)
2512 {
2513 printf("Interrupt Endpoint: "
2514 "Len=%d Type=0x%02x Subtype=0x%02x "
2515 "wMaxTransferSize=%d ",
2516 desc->bLength,
2517 desc->bDescriptorType,
2518 desc->bDescriptorSubtype,
2519 UGETW(desc->wMaxTransferSize));
2520 }
2521
2522
2523 static void
2524 print_vs_output_header_descriptor(
2525 const uvideo_vs_output_header_descriptor_t *desc)
2526 {
2527 printf("Interface Output Header: "
2528 "Len=%d Type=0x%02x Subtype=0x%02x "
2529 "bNumFormats=%d wTotalLength=%d bEndpointAddress=%d "
2530 "bTerminalLink=%d bControlSize=%d",
2531 desc->bLength,
2532 desc->bDescriptorType,
2533 desc->bDescriptorSubtype,
2534 desc->bNumFormats,
2535 UGETW(desc->wTotalLength),
2536 desc->bEndpointAddress,
2537 desc->bTerminalLink,
2538 desc->bControlSize);
2539 }
2540
2541 static void
2542 print_vs_input_header_descriptor(
2543 const uvideo_vs_input_header_descriptor_t *desc)
2544 {
2545 printf("Interface Input Header: "
2546 "Len=%d Type=0x%02x Subtype=0x%02x "
2547 "bNumFormats=%d wTotalLength=%d bEndpointAddress=%d "
2548 "bmInfo=%x bTerminalLink=%d bStillCaptureMethod=%d "
2549 "bTriggerSupport=%d bTriggerUsage=%d bControlSize=%d ",
2550 desc->bLength,
2551 desc->bDescriptorType,
2552 desc->bDescriptorSubtype,
2553 desc->bNumFormats,
2554 UGETW(desc->wTotalLength),
2555 desc->bEndpointAddress,
2556 desc->bmInfo,
2557 desc->bTerminalLink,
2558 desc->bStillCaptureMethod,
2559 desc->bTriggerSupport,
2560 desc->bTriggerUsage,
2561 desc->bControlSize);
2562 print_bitmap(desc->bmaControls, desc->bControlSize);
2563 }
2564
2565 static void
2566 print_vs_format_uncompressed_descriptor(
2567 const uvideo_vs_format_uncompressed_descriptor_t *desc)
2568 {
2569 printf("Format Uncompressed: "
2570 "Len=%d Type=0x%02x Subtype=0x%02x "
2571 "bFormatIndex=%d bNumFrameDescriptors=%d ",
2572 desc->bLength,
2573 desc->bDescriptorType,
2574 desc->bDescriptorSubtype,
2575 desc->bFormatIndex,
2576 desc->bNumFrameDescriptors);
2577 usb_guid_print(&desc->guidFormat);
2578 printf(" bBitsPerPixel=%d bDefaultFrameIndex=%d "
2579 "bAspectRatioX=%d bAspectRatioY=%d "
2580 "bmInterlaceFlags=0x%02x bCopyProtect=%d",
2581 desc->bBitsPerPixel,
2582 desc->bDefaultFrameIndex,
2583 desc->bAspectRatioX,
2584 desc->bAspectRatioY,
2585 desc->bmInterlaceFlags,
2586 desc->bCopyProtect);
2587 }
2588
2589 static void
2590 print_vs_frame_uncompressed_descriptor(
2591 const uvideo_vs_frame_uncompressed_descriptor_t *desc)
2592 {
2593 printf("Frame Uncompressed: "
2594 "Len=%d Type=0x%02x Subtype=0x%02x "
2595 "bFrameIndex=%d bmCapabilities=0x%02x "
2596 "wWidth=%d wHeight=%d dwMinBitRate=%d dwMaxBitRate=%d "
2597 "dwMaxVideoFrameBufferSize=%d dwDefaultFrameInterval=%d "
2598 "bFrameIntervalType=%d",
2599 desc->bLength,
2600 desc->bDescriptorType,
2601 desc->bDescriptorSubtype,
2602 desc->bFrameIndex,
2603 desc->bmCapabilities,
2604 UGETW(desc->wWidth),
2605 UGETW(desc->wHeight),
2606 UGETDW(desc->dwMinBitRate),
2607 UGETDW(desc->dwMaxBitRate),
2608 UGETDW(desc->dwMaxVideoFrameBufferSize),
2609 UGETDW(desc->dwDefaultFrameInterval),
2610 desc->bFrameIntervalType);
2611 }
2612
2613 static void
2614 print_vs_format_mjpeg_descriptor(
2615 const uvideo_vs_format_mjpeg_descriptor_t *desc)
2616 {
2617 printf("MJPEG format: "
2618 "Len=%d Type=0x%02x Subtype=0x%02x "
2619 "bFormatIndex=%d bNumFrameDescriptors=%d bmFlags=0x%02x "
2620 "bDefaultFrameIndex=%d bAspectRatioX=%d bAspectRatioY=%d "
2621 "bmInterlaceFlags=0x%02x bCopyProtect=%d",
2622 desc->bLength,
2623 desc->bDescriptorType,
2624 desc->bDescriptorSubtype,
2625 desc->bFormatIndex,
2626 desc->bNumFrameDescriptors,
2627 desc->bmFlags,
2628 desc->bDefaultFrameIndex,
2629 desc->bAspectRatioX,
2630 desc->bAspectRatioY,
2631 desc->bmInterlaceFlags,
2632 desc->bCopyProtect);
2633 }
2634
2635 static void
2636 print_vs_frame_mjpeg_descriptor(
2637 const uvideo_vs_frame_mjpeg_descriptor_t *desc)
2638 {
2639 printf("MJPEG frame: "
2640 "Len=%d Type=0x%02x Subtype=0x%02x "
2641 "bFrameIndex=%d bmCapabilities=0x%02x "
2642 "wWidth=%d wHeight=%d dwMinBitRate=%d dwMaxBitRate=%d "
2643 "dwMaxVideoFrameBufferSize=%d dwDefaultFrameInterval=%d "
2644 "bFrameIntervalType=%d",
2645 desc->bLength,
2646 desc->bDescriptorType,
2647 desc->bDescriptorSubtype,
2648 desc->bFrameIndex,
2649 desc->bmCapabilities,
2650 UGETW(desc->wWidth),
2651 UGETW(desc->wHeight),
2652 UGETDW(desc->dwMinBitRate),
2653 UGETDW(desc->dwMaxBitRate),
2654 UGETDW(desc->dwMaxVideoFrameBufferSize),
2655 UGETDW(desc->dwDefaultFrameInterval),
2656 desc->bFrameIntervalType);
2657 }
2658
2659 static void
2660 print_vs_format_dv_descriptor(
2661 const uvideo_vs_format_dv_descriptor_t *desc)
2662 {
2663 printf("MJPEG format: "
2664 "Len=%d Type=0x%02x Subtype=0x%02x "
2665 "bFormatIndex=%d dwMaxVideoFrameBufferSize=%d "
2666 "bFormatType/Rate=%d bFormatType/Format=%d",
2667 desc->bLength,
2668 desc->bDescriptorType,
2669 desc->bDescriptorSubtype,
2670 desc->bFormatIndex,
2671 UGETDW(desc->dwMaxVideoFrameBufferSize),
2672 UVIDEO_GET_DV_FREQ(desc->bFormatType),
2673 UVIDEO_GET_DV_FORMAT(desc->bFormatType));
2674 }
2675
2676 #endif /* !UVIDEO_DEBUG */
2677
2678 static const usb_descriptor_t *
2679 usb_desc_iter_peek_next(usbd_desc_iter_t *iter)
2680 {
2681 const usb_descriptor_t *desc;
2682
2683 if (iter->cur + sizeof(usb_descriptor_t) >= iter->end) {
2684 if (iter->cur != iter->end)
2685 printf("usb_desc_iter_peek_next: bad descriptor\n");
2686 return NULL;
2687 }
2688 desc = (const usb_descriptor_t *)iter->cur;
2689 if (desc->bLength == 0) {
2690 printf("usb_desc_iter_peek_next: descriptor length = 0\n");
2691 return NULL;
2692 }
2693 if (iter->cur + desc->bLength > iter->end) {
2694 printf("usb_desc_iter_peek_next: descriptor length too large\n");
2695 return NULL;
2696 }
2697 return desc;
2698 }
2699
2700 /* Return the next interface descriptor, skipping over any other
2701 * descriptors. Returns NULL at the end or on error. */
2702 static const usb_interface_descriptor_t *
2703 usb_desc_iter_next_interface(usbd_desc_iter_t *iter)
2704 {
2705 const usb_descriptor_t *desc;
2706
2707 while ((desc = usb_desc_iter_peek_next(iter)) != NULL &&
2708 desc->bDescriptorType != UDESC_INTERFACE)
2709 {
2710 usb_desc_iter_next(iter);
2711 }
2712
2713 return (const usb_interface_descriptor_t *)usb_desc_iter_next(iter);
2714 }
2715
2716 /* Returns the next non-interface descriptor, returning NULL when the
2717 * next descriptor would be an interface descriptor. */
2718 static const usb_descriptor_t *
2719 usb_desc_iter_next_non_interface(usbd_desc_iter_t *iter)
2720 {
2721 const usb_descriptor_t *desc;
2722
2723 if ((desc = usb_desc_iter_peek_next(iter)) != NULL &&
2724 desc->bDescriptorType != UDESC_INTERFACE)
2725 {
2726 return (usb_desc_iter_next(iter));
2727 } else {
2728 return NULL;
2729 }
2730 }
2731
2732 #ifdef UVIDEO_DEBUG
2733 static void
2734 usb_guid_print(const usb_guid_t *guid)
2735 {
2736 printf("%04X-%02X-%02X-",
2737 UGETDW(guid->data1),
2738 UGETW(guid->data2),
2739 UGETW(guid->data3));
2740 printf("%02X%02X-",
2741 guid->data4[0],
2742 guid->data4[1]);
2743 printf("%02X%02X%02X%02X%02X%02X",
2744 guid->data4[2],
2745 guid->data4[3],
2746 guid->data4[4],
2747 guid->data4[5],
2748 guid->data4[6],
2749 guid->data4[7]);
2750 }
2751 #endif /* !UVIDEO_DEBUG */
2752
2753 /* Returns less than zero, zero, or greater than zero if uguid is less
2754 * than, equal to, or greater than guid. */
2755 static int
2756 usb_guid_cmp(const usb_guid_t *uguid, const guid_t *guid)
2757 {
2758 if (guid->data1 > UGETDW(uguid->data1))
2759 return 1;
2760 else if (guid->data1 < UGETDW(uguid->data1))
2761 return -1;
2762
2763 if (guid->data2 > UGETW(uguid->data2))
2764 return 1;
2765 else if (guid->data2 < UGETW(uguid->data2))
2766 return -1;
2767
2768 if (guid->data3 > UGETW(uguid->data3))
2769 return 1;
2770 else if (guid->data3 < UGETW(uguid->data3))
2771 return -1;
2772
2773 return (memcmp(guid->data4, uguid->data4, 8));
2774 }
2775