Lines Matching +full:sub +full:- +full:frame
1 // SPDX-License-Identifier: GPL-2.0+
3 * uvc_v4l2.c -- USB Video Class Gadget driver
5 * Copyright (C) 2009-2010
19 #include <media/v4l2-dev.h>
20 #include <media/v4l2-event.h>
21 #include <media/v4l2-ioctl.h>
35 if (uformat->type == UVCG_UNCOMPRESSED) { in to_uvc_format()
38 unc = to_uvcg_uncompressed(&uformat->group.cg_item); in to_uvc_format()
40 return ERR_PTR(-EINVAL); in to_uvc_format()
42 memcpy(guid, unc->desc.guidFormat, sizeof(guid)); in to_uvc_format()
43 } else if (uformat->type == UVCG_FRAMEBASED) { in to_uvc_format()
46 unc = to_uvcg_framebased(&uformat->group.cg_item); in to_uvc_format()
48 return ERR_PTR(-EINVAL); in to_uvc_format()
50 memcpy(guid, unc->desc.guidFormat, sizeof(guid)); in to_uvc_format()
55 return ERR_PTR(-EINVAL); in to_uvc_format()
65 if (uformat->type == UVCG_UNCOMPRESSED) { in uvc_v4l2_get_bytesperline()
66 u = to_uvcg_uncompressed(&uformat->group.cg_item); in uvc_v4l2_get_bytesperline()
70 return u->desc.bBitsPerPixel * uframe->frame.w_width / 8; in uvc_v4l2_get_bytesperline()
81 return bpl ? bpl * uframe->frame.w_height : in uvc_get_frame_size()
82 uframe->frame.dw_max_video_frame_buffer_size; in uvc_get_frame_size()
91 list_for_each_entry(format, &uvc->header->formats, entry) { in find_format_by_index()
93 uformat = format->fmt; in find_format_by_index()
107 struct uvcg_frame_ptr *frame; in find_frame_by_index() local
110 list_for_each_entry(format, &uvc->header->formats, entry) { in find_frame_by_index()
111 if (format->fmt->type != uformat->type) in find_frame_by_index()
113 list_for_each_entry(frame, &format->fmt->frames, entry) { in find_frame_by_index()
114 if (index == frame->frm->frame.b_frame_index) { in find_frame_by_index()
115 uframe = frame->frm; in find_frame_by_index()
130 list_for_each_entry(format, &uvc->header->formats, entry) { in find_format_by_pix()
131 const struct uvc_format_desc *fmtdesc = to_uvc_format(format->fmt); in find_format_by_pix()
136 if (fmtdesc->fcc == pixelformat) { in find_format_by_pix()
137 uformat = format->fmt; in find_format_by_pix()
149 struct uvc_video *video = &uvc->video; in find_closest_frame_by_size()
151 struct uvcg_frame_ptr *frame; in find_closest_frame_by_size() local
156 * the size in pixels of the non-overlapping regions between the in find_closest_frame_by_size()
157 * requested size and the frame-specified size. in find_closest_frame_by_size()
159 maxd = (unsigned int)-1; in find_closest_frame_by_size()
161 list_for_each_entry(format, &uvc->header->formats, entry) { in find_closest_frame_by_size()
162 if (format->fmt->type != uformat->type) in find_closest_frame_by_size()
165 list_for_each_entry(frame, &format->fmt->frames, entry) { in find_closest_frame_by_size()
168 w = frame->frm->frame.w_width; in find_closest_frame_by_size()
169 h = frame->frm->frame.w_height; in find_closest_frame_by_size()
172 d = w*h + rw*rh - 2*d; in find_closest_frame_by_size()
175 uframe = frame->frm; in find_closest_frame_by_size()
184 uvcg_dbg(&video->uvc->func, "Unsupported size %ux%u\n", rw, rh); in find_closest_frame_by_size()
189 /* --------------------------------------------------------------------------
196 struct usb_composite_dev *cdev = uvc->func.config->cdev; in uvc_send_response()
197 struct usb_request *req = uvc->control_req; in uvc_send_response()
199 if (data->length < 0) in uvc_send_response()
200 return usb_ep_set_halt(cdev->gadget->ep0); in uvc_send_response()
202 req->length = min_t(unsigned int, uvc->event_length, data->length); in uvc_send_response()
203 req->zero = data->length < uvc->event_length; in uvc_send_response()
205 memcpy(req->buf, data->data, req->length); in uvc_send_response()
207 return usb_ep_queue(cdev->gadget->ep0, req, GFP_KERNEL); in uvc_send_response()
210 /* --------------------------------------------------------------------------
219 struct usb_composite_dev *cdev = uvc->func.config->cdev; in uvc_v4l2_querycap()
221 strscpy(cap->driver, "g_uvc", sizeof(cap->driver)); in uvc_v4l2_querycap()
222 strscpy(cap->card, cdev->gadget->name, sizeof(cap->card)); in uvc_v4l2_querycap()
223 strscpy(cap->bus_info, dev_name(&cdev->gadget->dev), in uvc_v4l2_querycap()
224 sizeof(cap->bus_info)); in uvc_v4l2_querycap()
233 struct uvc_video *video = &uvc->video; in uvc_v4l2_get_format()
235 fmt->fmt.pix.pixelformat = video->fcc; in uvc_v4l2_get_format()
236 fmt->fmt.pix.width = video->width; in uvc_v4l2_get_format()
237 fmt->fmt.pix.height = video->height; in uvc_v4l2_get_format()
238 fmt->fmt.pix.field = V4L2_FIELD_NONE; in uvc_v4l2_get_format()
239 fmt->fmt.pix.bytesperline = video->bpp * video->width / 8; in uvc_v4l2_get_format()
240 fmt->fmt.pix.sizeimage = video->imagesize; in uvc_v4l2_get_format()
241 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB; in uvc_v4l2_get_format()
242 fmt->fmt.pix.priv = 0; in uvc_v4l2_get_format()
252 struct uvc_video *video = &uvc->video; in uvc_v4l2_try_format()
258 if (fmt->type != video->queue.queue.type) in uvc_v4l2_try_format()
259 return -EINVAL; in uvc_v4l2_try_format()
261 fcc = (u8 *)&fmt->fmt.pix.pixelformat; in uvc_v4l2_try_format()
262 uvcg_dbg(&uvc->func, "Trying format 0x%08x (%c%c%c%c): %ux%u\n", in uvc_v4l2_try_format()
263 fmt->fmt.pix.pixelformat, in uvc_v4l2_try_format()
265 fmt->fmt.pix.width, fmt->fmt.pix.height); in uvc_v4l2_try_format()
267 uformat = find_format_by_pix(uvc, fmt->fmt.pix.pixelformat); in uvc_v4l2_try_format()
269 return -EINVAL; in uvc_v4l2_try_format()
272 fmt->fmt.pix.width, fmt->fmt.pix.height); in uvc_v4l2_try_format()
274 return -EINVAL; in uvc_v4l2_try_format()
276 if (uformat->type == UVCG_UNCOMPRESSED) { in uvc_v4l2_try_format()
278 to_uvcg_uncompressed(&uformat->group.cg_item); in uvc_v4l2_try_format()
282 v4l2_fill_pixfmt(&fmt->fmt.pix, fmt->fmt.pix.pixelformat, in uvc_v4l2_try_format()
283 uframe->frame.w_width, uframe->frame.w_height); in uvc_v4l2_try_format()
285 if (fmt->fmt.pix.sizeimage != (uvc_v4l2_get_bytesperline(uformat, uframe) * in uvc_v4l2_try_format()
286 uframe->frame.w_height)) in uvc_v4l2_try_format()
287 return -EINVAL; in uvc_v4l2_try_format()
289 fmt->fmt.pix.width = uframe->frame.w_width; in uvc_v4l2_try_format()
290 fmt->fmt.pix.height = uframe->frame.w_height; in uvc_v4l2_try_format()
291 fmt->fmt.pix.bytesperline = uvc_v4l2_get_bytesperline(uformat, uframe); in uvc_v4l2_try_format()
292 fmt->fmt.pix.sizeimage = uvc_get_frame_size(uformat, uframe); in uvc_v4l2_try_format()
296 fmt->fmt.pix.pixelformat = fmtdesc->fcc; in uvc_v4l2_try_format()
298 fmt->fmt.pix.field = V4L2_FIELD_NONE; in uvc_v4l2_try_format()
299 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB; in uvc_v4l2_try_format()
300 fmt->fmt.pix.priv = 0; in uvc_v4l2_try_format()
310 struct uvc_video *video = &uvc->video; in uvc_v4l2_set_format()
317 video->fcc = fmt->fmt.pix.pixelformat; in uvc_v4l2_set_format()
318 video->bpp = fmt->fmt.pix.bytesperline * 8 / video->width; in uvc_v4l2_set_format()
319 video->width = fmt->fmt.pix.width; in uvc_v4l2_set_format()
320 video->height = fmt->fmt.pix.height; in uvc_v4l2_set_format()
321 video->imagesize = fmt->fmt.pix.sizeimage; in uvc_v4l2_set_format()
331 struct uvc_video *video = &uvc->video; in uvc_v4l2_g_parm()
334 if (!V4L2_TYPE_IS_OUTPUT(parm->type)) in uvc_v4l2_g_parm()
335 return -EINVAL; in uvc_v4l2_g_parm()
337 /* Return the actual frame period. */ in uvc_v4l2_g_parm()
338 timeperframe.numerator = video->interval; in uvc_v4l2_g_parm()
343 uvcg_dbg(&uvc->func, "Getting frame interval of %u/%u (%u)\n", in uvc_v4l2_g_parm()
345 video->interval); in uvc_v4l2_g_parm()
347 parm->parm.output.timeperframe = timeperframe; in uvc_v4l2_g_parm()
348 parm->parm.output.capability = V4L2_CAP_TIMEPERFRAME; in uvc_v4l2_g_parm()
358 struct uvc_video *video = &uvc->video; in uvc_v4l2_s_parm()
361 if (!V4L2_TYPE_IS_OUTPUT(parm->type)) in uvc_v4l2_s_parm()
362 return -EINVAL; in uvc_v4l2_s_parm()
364 timeperframe = parm->parm.output.timeperframe; in uvc_v4l2_s_parm()
366 video->interval = v4l2_fraction_to_interval(timeperframe.numerator, in uvc_v4l2_s_parm()
369 uvcg_dbg(&uvc->func, "Setting frame interval to %u/%u (%u)\n", in uvc_v4l2_s_parm()
371 video->interval); in uvc_v4l2_s_parm()
384 struct uvcg_frame_ptr *frame; in uvc_v4l2_enum_frameintervals() local
386 uformat = find_format_by_pix(uvc, fival->pixel_format); in uvc_v4l2_enum_frameintervals()
388 return -EINVAL; in uvc_v4l2_enum_frameintervals()
390 list_for_each_entry(frame, &uformat->frames, entry) { in uvc_v4l2_enum_frameintervals()
391 if (frame->frm->frame.w_width == fival->width && in uvc_v4l2_enum_frameintervals()
392 frame->frm->frame.w_height == fival->height) { in uvc_v4l2_enum_frameintervals()
393 uframe = frame->frm; in uvc_v4l2_enum_frameintervals()
398 return -EINVAL; in uvc_v4l2_enum_frameintervals()
400 if (fival->index >= uframe->frame.b_frame_interval_type) in uvc_v4l2_enum_frameintervals()
401 return -EINVAL; in uvc_v4l2_enum_frameintervals()
403 fival->discrete.numerator = in uvc_v4l2_enum_frameintervals()
404 uframe->dw_frame_interval[fival->index]; in uvc_v4l2_enum_frameintervals()
407 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE; in uvc_v4l2_enum_frameintervals()
408 fival->discrete.denominator = 10000000; in uvc_v4l2_enum_frameintervals()
409 v4l2_simplify_fraction(&fival->discrete.numerator, in uvc_v4l2_enum_frameintervals()
410 &fival->discrete.denominator, 8, 333); in uvc_v4l2_enum_frameintervals()
424 uformat = find_format_by_pix(uvc, fsize->pixel_format); in uvc_v4l2_enum_framesizes()
426 return -EINVAL; in uvc_v4l2_enum_framesizes()
428 if (fsize->index >= uformat->num_frames) in uvc_v4l2_enum_framesizes()
429 return -EINVAL; in uvc_v4l2_enum_framesizes()
431 uframe = find_frame_by_index(uvc, uformat, fsize->index + 1); in uvc_v4l2_enum_framesizes()
433 return -EINVAL; in uvc_v4l2_enum_framesizes()
435 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE; in uvc_v4l2_enum_framesizes()
436 fsize->discrete.width = uframe->frame.w_width; in uvc_v4l2_enum_framesizes()
437 fsize->discrete.height = uframe->frame.w_height; in uvc_v4l2_enum_framesizes()
450 if (f->index >= uvc->header->num_fmt) in uvc_v4l2_enum_format()
451 return -EINVAL; in uvc_v4l2_enum_format()
453 uformat = find_format_by_index(uvc, f->index + 1); in uvc_v4l2_enum_format()
455 return -EINVAL; in uvc_v4l2_enum_format()
461 f->pixelformat = fmtdesc->fcc; in uvc_v4l2_enum_format()
471 struct uvc_video *video = &uvc->video; in uvc_v4l2_reqbufs()
473 if (b->type != video->queue.queue.type) in uvc_v4l2_reqbufs()
474 return -EINVAL; in uvc_v4l2_reqbufs()
476 return uvcg_alloc_buffers(&video->queue, b); in uvc_v4l2_reqbufs()
484 struct uvc_video *video = &uvc->video; in uvc_v4l2_querybuf()
486 return uvcg_query_buffer(&video->queue, b); in uvc_v4l2_querybuf()
494 struct uvc_video *video = &uvc->video; in uvc_v4l2_qbuf()
497 ret = uvcg_queue_buffer(&video->queue, b); in uvc_v4l2_qbuf()
501 if (uvc->state == UVC_STATE_STREAMING) in uvc_v4l2_qbuf()
502 queue_work(video->async_wq, &video->pump); in uvc_v4l2_qbuf()
512 struct uvc_video *video = &uvc->video; in uvc_v4l2_dqbuf()
514 return uvcg_dequeue_buffer(&video->queue, b, file->f_flags & O_NONBLOCK); in uvc_v4l2_dqbuf()
522 struct uvc_video *video = &uvc->video; in uvc_v4l2_streamon()
525 if (type != video->queue.queue.type) in uvc_v4l2_streamon()
526 return -EINVAL; in uvc_v4l2_streamon()
538 uvc->state = UVC_STATE_STREAMING; in uvc_v4l2_streamon()
548 struct uvc_video *video = &uvc->video; in uvc_v4l2_streamoff()
551 if (type != video->queue.queue.type) in uvc_v4l2_streamoff()
552 return -EINVAL; in uvc_v4l2_streamoff()
558 if (uvc->state != UVC_STATE_STREAMING) in uvc_v4l2_streamoff()
561 uvc->state = UVC_STATE_CONNECTED; in uvc_v4l2_streamoff()
568 const struct v4l2_event_subscription *sub) in uvc_v4l2_subscribe_event() argument
570 struct uvc_device *uvc = video_get_drvdata(fh->vdev); in uvc_v4l2_subscribe_event()
574 if (sub->type < UVC_EVENT_FIRST || sub->type > UVC_EVENT_LAST) in uvc_v4l2_subscribe_event()
575 return -EINVAL; in uvc_v4l2_subscribe_event()
577 if (sub->type == UVC_EVENT_SETUP && uvc->func_connected) in uvc_v4l2_subscribe_event()
578 return -EBUSY; in uvc_v4l2_subscribe_event()
580 ret = v4l2_event_subscribe(fh, sub, 2, NULL); in uvc_v4l2_subscribe_event()
584 if (sub->type == UVC_EVENT_SETUP) { in uvc_v4l2_subscribe_event()
585 uvc->func_connected = true; in uvc_v4l2_subscribe_event()
586 handle->is_uvc_app_handle = true; in uvc_v4l2_subscribe_event()
596 uvcg_video_disable(&uvc->video); in uvc_v4l2_disable()
597 uvcg_free_buffers(&uvc->video.queue); in uvc_v4l2_disable()
598 uvc->func_connected = false; in uvc_v4l2_disable()
599 wake_up_interruptible(&uvc->func_connected_queue); in uvc_v4l2_disable()
604 const struct v4l2_event_subscription *sub) in uvc_v4l2_unsubscribe_event() argument
606 struct uvc_device *uvc = video_get_drvdata(fh->vdev); in uvc_v4l2_unsubscribe_event()
610 ret = v4l2_event_unsubscribe(fh, sub); in uvc_v4l2_unsubscribe_event()
614 if (sub->type == UVC_EVENT_SETUP && handle->is_uvc_app_handle) { in uvc_v4l2_unsubscribe_event()
616 handle->is_uvc_app_handle = false; in uvc_v4l2_unsubscribe_event()
634 return -ENOIOCTLCMD; in uvc_v4l2_ioctl_default()
659 /* --------------------------------------------------------------------------
672 return -ENOMEM; in uvc_v4l2_open()
674 v4l2_fh_init(&handle->vfh, vdev); in uvc_v4l2_open()
675 v4l2_fh_add(&handle->vfh); in uvc_v4l2_open()
677 handle->device = &uvc->video; in uvc_v4l2_open()
678 file->private_data = &handle->vfh; in uvc_v4l2_open()
688 struct uvc_file_handle *handle = to_uvc_file_handle(file->private_data); in uvc_v4l2_release()
689 struct uvc_video *video = handle->device; in uvc_v4l2_release()
691 mutex_lock(&video->mutex); in uvc_v4l2_release()
692 if (handle->is_uvc_app_handle) in uvc_v4l2_release()
694 mutex_unlock(&video->mutex); in uvc_v4l2_release()
696 file->private_data = NULL; in uvc_v4l2_release()
697 v4l2_fh_del(&handle->vfh); in uvc_v4l2_release()
698 v4l2_fh_exit(&handle->vfh); in uvc_v4l2_release()
710 return uvcg_queue_mmap(&uvc->video.queue, vma); in uvc_v4l2_mmap()
719 return uvcg_queue_poll(&uvc->video.queue, file, wait); in uvc_v4l2_poll()
730 return uvcg_queue_get_unmapped_area(&uvc->video.queue, pgoff); in uvcg_v4l2_get_unmapped_area()