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