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