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