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>
36 if (uformat->type == UVCG_UNCOMPRESSED) { in to_uvc_format()
37 unc = to_uvcg_uncompressed(&uformat->group.cg_item); in to_uvc_format()
39 return ERR_PTR(-EINVAL); in to_uvc_format()
41 memcpy(guid, unc->desc.guidFormat, sizeof(guid)); in to_uvc_format()
46 return ERR_PTR(-EINVAL); in to_uvc_format()
56 if (uformat->type == UVCG_UNCOMPRESSED) { in uvc_v4l2_get_bytesperline()
57 u = to_uvcg_uncompressed(&uformat->group.cg_item); in uvc_v4l2_get_bytesperline()
61 return u->desc.bBitsPerPixel * uframe->frame.w_width / 8; in uvc_v4l2_get_bytesperline()
72 return bpl ? bpl * uframe->frame.w_height : in uvc_get_frame_size()
73 uframe->frame.dw_max_video_frame_buffer_size; in uvc_get_frame_size()
82 list_for_each_entry(format, &uvc->header->formats, entry) { in find_format_by_index()
84 uformat = format->fmt; in find_format_by_index()
98 struct uvcg_frame_ptr *frame; in find_frame_by_index() local
101 list_for_each_entry(format, &uvc->header->formats, entry) { in find_frame_by_index()
102 if (format->fmt->type != uformat->type) in find_frame_by_index()
104 list_for_each_entry(frame, &format->fmt->frames, entry) { in find_frame_by_index()
105 if (index == frame->frm->frame.b_frame_index) { in find_frame_by_index()
106 uframe = frame->frm; in find_frame_by_index()
121 list_for_each_entry(format, &uvc->header->formats, entry) { in find_format_by_pix()
122 const struct uvc_format_desc *fmtdesc = to_uvc_format(format->fmt); in find_format_by_pix()
127 if (fmtdesc->fcc == pixelformat) { in find_format_by_pix()
128 uformat = format->fmt; in find_format_by_pix()
140 struct uvc_video *video = &uvc->video; in find_closest_frame_by_size()
142 struct uvcg_frame_ptr *frame; in find_closest_frame_by_size() local
147 * the size in pixels of the non-overlapping regions between the in find_closest_frame_by_size()
148 * requested size and the frame-specified size. in find_closest_frame_by_size()
150 maxd = (unsigned int)-1; in find_closest_frame_by_size()
152 list_for_each_entry(format, &uvc->header->formats, entry) { in find_closest_frame_by_size()
153 if (format->fmt->type != uformat->type) in find_closest_frame_by_size()
156 list_for_each_entry(frame, &format->fmt->frames, entry) { in find_closest_frame_by_size()
159 w = frame->frm->frame.w_width; in find_closest_frame_by_size()
160 h = frame->frm->frame.w_height; in find_closest_frame_by_size()
163 d = w*h + rw*rh - 2*d; in find_closest_frame_by_size()
166 uframe = frame->frm; in find_closest_frame_by_size()
175 uvcg_dbg(&video->uvc->func, "Unsupported size %ux%u\n", rw, rh); in find_closest_frame_by_size()
180 /* --------------------------------------------------------------------------
187 struct usb_composite_dev *cdev = uvc->func.config->cdev; in uvc_send_response()
188 struct usb_request *req = uvc->control_req; in uvc_send_response()
190 if (data->length < 0) in uvc_send_response()
191 return usb_ep_set_halt(cdev->gadget->ep0); in uvc_send_response()
193 req->length = min_t(unsigned int, uvc->event_length, data->length); in uvc_send_response()
194 req->zero = data->length < uvc->event_length; in uvc_send_response()
196 memcpy(req->buf, data->data, req->length); in uvc_send_response()
198 return usb_ep_queue(cdev->gadget->ep0, req, GFP_KERNEL); in uvc_send_response()
201 /* --------------------------------------------------------------------------
210 struct usb_composite_dev *cdev = uvc->func.config->cdev; in uvc_v4l2_querycap()
212 strscpy(cap->driver, "g_uvc", sizeof(cap->driver)); in uvc_v4l2_querycap()
213 strscpy(cap->card, cdev->gadget->name, sizeof(cap->card)); in uvc_v4l2_querycap()
214 strscpy(cap->bus_info, dev_name(&cdev->gadget->dev), in uvc_v4l2_querycap()
215 sizeof(cap->bus_info)); in uvc_v4l2_querycap()
224 struct uvc_video *video = &uvc->video; in uvc_v4l2_get_format()
226 fmt->fmt.pix.pixelformat = video->fcc; in uvc_v4l2_get_format()
227 fmt->fmt.pix.width = video->width; in uvc_v4l2_get_format()
228 fmt->fmt.pix.height = video->height; in uvc_v4l2_get_format()
229 fmt->fmt.pix.field = V4L2_FIELD_NONE; in uvc_v4l2_get_format()
230 fmt->fmt.pix.bytesperline = video->bpp * video->width / 8; in uvc_v4l2_get_format()
231 fmt->fmt.pix.sizeimage = video->imagesize; in uvc_v4l2_get_format()
232 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB; in uvc_v4l2_get_format()
233 fmt->fmt.pix.priv = 0; in uvc_v4l2_get_format()
243 struct uvc_video *video = &uvc->video; in uvc_v4l2_try_format()
249 if (fmt->type != video->queue.queue.type) in uvc_v4l2_try_format()
250 return -EINVAL; in uvc_v4l2_try_format()
252 fcc = (u8 *)&fmt->fmt.pix.pixelformat; in uvc_v4l2_try_format()
253 uvcg_dbg(&uvc->func, "Trying format 0x%08x (%c%c%c%c): %ux%u\n", in uvc_v4l2_try_format()
254 fmt->fmt.pix.pixelformat, in uvc_v4l2_try_format()
256 fmt->fmt.pix.width, fmt->fmt.pix.height); in uvc_v4l2_try_format()
258 uformat = find_format_by_pix(uvc, fmt->fmt.pix.pixelformat); in uvc_v4l2_try_format()
260 return -EINVAL; in uvc_v4l2_try_format()
263 fmt->fmt.pix.width, fmt->fmt.pix.height); in uvc_v4l2_try_format()
265 return -EINVAL; in uvc_v4l2_try_format()
267 if (uformat->type == UVCG_UNCOMPRESSED) { in uvc_v4l2_try_format()
269 to_uvcg_uncompressed(&uformat->group.cg_item); in uvc_v4l2_try_format()
273 v4l2_fill_pixfmt(&fmt->fmt.pix, fmt->fmt.pix.pixelformat, in uvc_v4l2_try_format()
274 uframe->frame.w_width, uframe->frame.w_height); in uvc_v4l2_try_format()
276 if (fmt->fmt.pix.sizeimage != (uvc_v4l2_get_bytesperline(uformat, uframe) * in uvc_v4l2_try_format()
277 uframe->frame.w_height)) in uvc_v4l2_try_format()
278 return -EINVAL; in uvc_v4l2_try_format()
280 fmt->fmt.pix.width = uframe->frame.w_width; in uvc_v4l2_try_format()
281 fmt->fmt.pix.height = uframe->frame.w_height; in uvc_v4l2_try_format()
282 fmt->fmt.pix.bytesperline = uvc_v4l2_get_bytesperline(uformat, uframe); in uvc_v4l2_try_format()
283 fmt->fmt.pix.sizeimage = uvc_get_frame_size(uformat, uframe); in uvc_v4l2_try_format()
287 fmt->fmt.pix.pixelformat = fmtdesc->fcc; in uvc_v4l2_try_format()
289 fmt->fmt.pix.field = V4L2_FIELD_NONE; in uvc_v4l2_try_format()
290 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB; in uvc_v4l2_try_format()
291 fmt->fmt.pix.priv = 0; in uvc_v4l2_try_format()
301 struct uvc_video *video = &uvc->video; in uvc_v4l2_set_format()
308 video->fcc = fmt->fmt.pix.pixelformat; in uvc_v4l2_set_format()
309 video->bpp = fmt->fmt.pix.bytesperline * 8 / video->width; in uvc_v4l2_set_format()
310 video->width = fmt->fmt.pix.width; in uvc_v4l2_set_format()
311 video->height = fmt->fmt.pix.height; in uvc_v4l2_set_format()
312 video->imagesize = fmt->fmt.pix.sizeimage; in uvc_v4l2_set_format()
325 struct uvcg_frame_ptr *frame; in uvc_v4l2_enum_frameintervals() local
327 uformat = find_format_by_pix(uvc, fival->pixel_format); in uvc_v4l2_enum_frameintervals()
329 return -EINVAL; in uvc_v4l2_enum_frameintervals()
331 list_for_each_entry(frame, &uformat->frames, entry) { in uvc_v4l2_enum_frameintervals()
332 if (frame->frm->frame.w_width == fival->width && in uvc_v4l2_enum_frameintervals()
333 frame->frm->frame.w_height == fival->height) { in uvc_v4l2_enum_frameintervals()
334 uframe = frame->frm; in uvc_v4l2_enum_frameintervals()
339 return -EINVAL; in uvc_v4l2_enum_frameintervals()
341 if (fival->index >= uframe->frame.b_frame_interval_type) in uvc_v4l2_enum_frameintervals()
342 return -EINVAL; in uvc_v4l2_enum_frameintervals()
344 fival->discrete.numerator = in uvc_v4l2_enum_frameintervals()
345 uframe->dw_frame_interval[fival->index]; in uvc_v4l2_enum_frameintervals()
348 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE; in uvc_v4l2_enum_frameintervals()
349 fival->discrete.denominator = 10000000; in uvc_v4l2_enum_frameintervals()
350 v4l2_simplify_fraction(&fival->discrete.numerator, in uvc_v4l2_enum_frameintervals()
351 &fival->discrete.denominator, 8, 333); in uvc_v4l2_enum_frameintervals()
365 uformat = find_format_by_pix(uvc, fsize->pixel_format); in uvc_v4l2_enum_framesizes()
367 return -EINVAL; in uvc_v4l2_enum_framesizes()
369 if (fsize->index >= uformat->num_frames) in uvc_v4l2_enum_framesizes()
370 return -EINVAL; in uvc_v4l2_enum_framesizes()
372 uframe = find_frame_by_index(uvc, uformat, fsize->index + 1); in uvc_v4l2_enum_framesizes()
374 return -EINVAL; in uvc_v4l2_enum_framesizes()
376 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE; in uvc_v4l2_enum_framesizes()
377 fsize->discrete.width = uframe->frame.w_width; in uvc_v4l2_enum_framesizes()
378 fsize->discrete.height = uframe->frame.w_height; in uvc_v4l2_enum_framesizes()
391 if (f->index >= uvc->header->num_fmt) in uvc_v4l2_enum_format()
392 return -EINVAL; in uvc_v4l2_enum_format()
394 uformat = find_format_by_index(uvc, f->index + 1); in uvc_v4l2_enum_format()
396 return -EINVAL; in uvc_v4l2_enum_format()
402 f->pixelformat = fmtdesc->fcc; in uvc_v4l2_enum_format()
412 struct uvc_video *video = &uvc->video; in uvc_v4l2_reqbufs()
414 if (b->type != video->queue.queue.type) in uvc_v4l2_reqbufs()
415 return -EINVAL; in uvc_v4l2_reqbufs()
417 return uvcg_alloc_buffers(&video->queue, b); in uvc_v4l2_reqbufs()
425 struct uvc_video *video = &uvc->video; in uvc_v4l2_querybuf()
427 return uvcg_query_buffer(&video->queue, b); in uvc_v4l2_querybuf()
435 struct uvc_video *video = &uvc->video; in uvc_v4l2_qbuf()
438 ret = uvcg_queue_buffer(&video->queue, b); in uvc_v4l2_qbuf()
442 if (uvc->state == UVC_STATE_STREAMING) in uvc_v4l2_qbuf()
443 queue_work(video->async_wq, &video->pump); in uvc_v4l2_qbuf()
453 struct uvc_video *video = &uvc->video; in uvc_v4l2_dqbuf()
455 return uvcg_dequeue_buffer(&video->queue, b, file->f_flags & O_NONBLOCK); in uvc_v4l2_dqbuf()
463 struct uvc_video *video = &uvc->video; in uvc_v4l2_streamon()
466 if (type != video->queue.queue.type) in uvc_v4l2_streamon()
467 return -EINVAL; in uvc_v4l2_streamon()
479 uvc->state = UVC_STATE_STREAMING; in uvc_v4l2_streamon()
489 struct uvc_video *video = &uvc->video; in uvc_v4l2_streamoff()
492 if (type != video->queue.queue.type) in uvc_v4l2_streamoff()
493 return -EINVAL; in uvc_v4l2_streamoff()
499 uvc->state = UVC_STATE_CONNECTED; in uvc_v4l2_streamoff()
506 const struct v4l2_event_subscription *sub) in uvc_v4l2_subscribe_event() argument
508 struct uvc_device *uvc = video_get_drvdata(fh->vdev); in uvc_v4l2_subscribe_event()
512 if (sub->type < UVC_EVENT_FIRST || sub->type > UVC_EVENT_LAST) in uvc_v4l2_subscribe_event()
513 return -EINVAL; in uvc_v4l2_subscribe_event()
515 if (sub->type == UVC_EVENT_SETUP && uvc->func_connected) in uvc_v4l2_subscribe_event()
516 return -EBUSY; in uvc_v4l2_subscribe_event()
518 ret = v4l2_event_subscribe(fh, sub, 2, NULL); in uvc_v4l2_subscribe_event()
522 if (sub->type == UVC_EVENT_SETUP) { in uvc_v4l2_subscribe_event()
523 uvc->func_connected = true; in uvc_v4l2_subscribe_event()
524 handle->is_uvc_app_handle = true; in uvc_v4l2_subscribe_event()
534 uvcg_video_disable(&uvc->video); in uvc_v4l2_disable()
535 uvcg_free_buffers(&uvc->video.queue); in uvc_v4l2_disable()
536 uvc->func_connected = false; in uvc_v4l2_disable()
537 wake_up_interruptible(&uvc->func_connected_queue); in uvc_v4l2_disable()
542 const struct v4l2_event_subscription *sub) in uvc_v4l2_unsubscribe_event() argument
544 struct uvc_device *uvc = video_get_drvdata(fh->vdev); in uvc_v4l2_unsubscribe_event()
548 ret = v4l2_event_unsubscribe(fh, sub); in uvc_v4l2_unsubscribe_event()
552 if (sub->type == UVC_EVENT_SETUP && handle->is_uvc_app_handle) { in uvc_v4l2_unsubscribe_event()
554 handle->is_uvc_app_handle = false; in uvc_v4l2_unsubscribe_event()
572 return -ENOIOCTLCMD; in uvc_v4l2_ioctl_default()
595 /* --------------------------------------------------------------------------
608 return -ENOMEM; in uvc_v4l2_open()
610 v4l2_fh_init(&handle->vfh, vdev); in uvc_v4l2_open()
611 v4l2_fh_add(&handle->vfh); in uvc_v4l2_open()
613 handle->device = &uvc->video; in uvc_v4l2_open()
614 file->private_data = &handle->vfh; in uvc_v4l2_open()
624 struct uvc_file_handle *handle = to_uvc_file_handle(file->private_data); in uvc_v4l2_release()
625 struct uvc_video *video = handle->device; in uvc_v4l2_release()
627 mutex_lock(&video->mutex); in uvc_v4l2_release()
628 if (handle->is_uvc_app_handle) in uvc_v4l2_release()
630 mutex_unlock(&video->mutex); in uvc_v4l2_release()
632 file->private_data = NULL; in uvc_v4l2_release()
633 v4l2_fh_del(&handle->vfh); in uvc_v4l2_release()
634 v4l2_fh_exit(&handle->vfh); in uvc_v4l2_release()
646 return uvcg_queue_mmap(&uvc->video.queue, vma); in uvc_v4l2_mmap()
655 return uvcg_queue_poll(&uvc->video.queue, file, wait); in uvc_v4l2_poll()
666 return uvcg_queue_get_unmapped_area(&uvc->video.queue, pgoff); in uvcg_v4l2_get_unmapped_area()