/* Find the frame interval closest to the requested frame interval for the given frame format and size. This should be done by the device as part of the Video Probe and Commit negotiation, but some hardware don't implement that feature.  */
static __u32 uvc_try_frame_interval(struct uvc_frame *frame, __u32 interval)
/* Find the frame interval closest to the requested frame interval for the given frame format and size. This should be done by the device as part of the Video Probe and Commit negotiation, but some hardware don't implement that feature.  */
static __u32 uvc_try_frame_interval(struct uvc_frame *frame, __u32 interval)
{
	unsigned int i;
	if (frame->bFrameIntervalType) {
		__u32 best = -1, dist;
		for (i = 0; i < frame->bFrameIntervalType; ++i) {
			dist = interval > frame->dwFrameInterval[i]
			     ? interval - frame->dwFrameInterval[i]
			     : frame->dwFrameInterval[i] - interval;
			if (dist > best)
				break;
			best = dist;
		}
		interval = frame->dwFrameInterval[i-1];
	} else {
		const __u32 min = frame->dwFrameInterval[0];
		const __u32 max = frame->dwFrameInterval[1];
		const __u32 step = frame->dwFrameInterval[2];
		interval = min + (interval - min + step/2) / step * step;
		if (interval > max)
			interval = max;
	}
	return interval;
}