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