From patchwork Fri Jun 24 11:29:02 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Guennadi Liakhovetski X-Patchwork-Id: 9197339 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 1FA2C60754 for ; Fri, 24 Jun 2016 11:29:11 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0DD1B28498 for ; Fri, 24 Jun 2016 11:29:11 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 002282849E; Fri, 24 Jun 2016 11:29:10 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,FREEMAIL_FROM, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id F38D828498 for ; Fri, 24 Jun 2016 11:29:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751002AbcFXL3H (ORCPT ); Fri, 24 Jun 2016 07:29:07 -0400 Received: from mout.gmx.net ([212.227.17.21]:58982 "EHLO mout.gmx.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750979AbcFXL3G (ORCPT ); Fri, 24 Jun 2016 07:29:06 -0400 Received: from axis700.grange ([89.0.126.241]) by mail.gmx.com (mrgmx103) with ESMTPSA (Nemesis) id 0M7ojs-1bcawJ3gkS-00vR9W; Fri, 24 Jun 2016 13:29:04 +0200 Received: by axis700.grange (Postfix, from userid 1000) id BDF63FF8B; Fri, 24 Jun 2016 13:29:02 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by axis700.grange (Postfix) with ESMTP id BA7C013EC5; Fri, 24 Jun 2016 13:29:02 +0200 (CEST) Date: Fri, 24 Jun 2016 13:29:02 +0200 (CEST) From: Guennadi Liakhovetski X-X-Sender: lyakh@axis700.grange To: Linux Media Mailing List cc: Laurent Pinchart Subject: [PATCH 2/3] uvcvideo: send a control event when a Control Change interrupt arrives In-Reply-To: Message-ID: References: MIME-Version: 1.0 X-Provags-ID: V03:K0:YhVOJoXwZLsTLBw9GQ6zJw0z/UC9CGjw63veGEzzp2JCohr60C7 Yok8z+xOE/FCOn61I17rfmZ2x4rvfugvmOGaCva0D/YvTemJS1FsOftXkaxt1+myFPSom4S beabOzpDVwJ8IPxca5qeVT7WvXVkGFyLLCwJar3sYtpkR+jsY1cUmz8Q+i+1Kq/WHFvSX1B nf016ql4ctwa/tpDAHbbg== X-UI-Out-Filterresults: notjunk:1; V01:K0:jMquN/raqLE=:oVXjX71D8lrxIxQGLaFPdo KYUeE0mVjH8EHkna/v/rtEk+NonDrIhcQFq9LzCqevdKMZzyRzCXhWG7H9AcFTsl36sbLLxJM P6Stk6DfbeWOeB/9QkO7XYGZ0dRkRWzgfvKFdsJf7+fR07qSAwFwYL2sHfmON+5Nbh4kpL9lC lqXbOMWWTSKOkKcgA7bC4sK14D8+gf1/wtmqlaOercl7zUXf+1lLUxlUibyKWP19VyW9ANNcP RHB4brUFfQXUbvFk6QQpFgGRN2jaFw7TM1j3sPnGuUsHMqA16dLLz6csZ6XZwF8X+z0qxvkdu kXAqNsyQxJdQIORUNuiCTqqn8Cbb9KDIFwvBK+AF8zhH00aVmo5HsiIJrWItmvNAeMNPHNtxT ERnNrRgUHaKgdPNNPEtmaSekGZl2dKfg/RjDkBCl5YaeRWB+x3Dawr6CayO06MsND8MiyWGm4 4v/2P0uctrhetBPOPuVPT8RB5NSS1oaJYygREFQqoDjfj2uBcFBriFFQow/CFIh4hfF++kRrk 8UGqJmzyA4SBQvlLoHsMtOIssBOxAMOQ5pNe5p/5XMks/Pbcu8eeGI11bsbXypDCCijqq889A XqTd7dwRqqO0VdDGTZwvOrU4gLfYER5bAJQM9u1LyAIpbjuzU2cV0D/coo9yPwnBgFeyQna83 D7CcGEHFjXGr9JP9oKTC0v/eZl/ZGPutyhWzFFyIIa0AaAX2NfYkZYXUIgf8ran4vC/kXAsLH 2Cs5vCfqxxvxHcqJNnsGb5RWAHqKXJ0qMSWsintFlwRG4wgmWwB9V9HYBec+bBCAI2mpbCUJt GeeEDeU Sender: linux-media-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP UVC defines a method of handling asynchronous controls, which sends a USB packet over the interrupt pipe. This patch implements support for such packets by sending a control event to the user. Since this can involve USB traffic and, therefore, scheduling, this has to be done in a work queue. Signed-off-by: Guennadi Liakhovetski --- drivers/media/usb/uvc/uvc_ctrl.c | 121 ++++++++++++++++++++++++++++++++----- drivers/media/usb/uvc/uvc_status.c | 112 ++++++++++++++++++++++++++++++---- drivers/media/usb/uvc/uvc_v4l2.c | 4 +- drivers/media/usb/uvc/uvcvideo.h | 14 ++++- include/uapi/linux/uvcvideo.h | 2 + 5 files changed, 225 insertions(+), 28 deletions(-) diff --git a/drivers/media/usb/uvc/uvc_ctrl.c b/drivers/media/usb/uvc/uvc_ctrl.c index c2ee6e3..649a5b4 100644 --- a/drivers/media/usb/uvc/uvc_ctrl.c +++ b/drivers/media/usb/uvc/uvc_ctrl.c @@ -20,6 +20,7 @@ #include #include #include +#include #include #include @@ -1236,16 +1237,93 @@ static void uvc_ctrl_send_event(struct uvc_fh *handle, } } -static void uvc_ctrl_send_slave_event(struct uvc_fh *handle, - struct uvc_control *master, u32 slave_id, - const struct v4l2_ext_control *xctrls, unsigned int xctrls_count) +static void __uvc_ctrl_send_slave_event(struct uvc_fh *handle, + struct uvc_control *master, u32 slave_id) { struct uvc_control_mapping *mapping = NULL; struct uvc_control *ctrl = NULL; u32 changes = V4L2_EVENT_CTRL_CH_FLAGS; - unsigned int i; s32 val = 0; + __uvc_find_control(master->entity, slave_id, &mapping, &ctrl, 0); + if (ctrl == NULL) + return; + + if (__uvc_ctrl_get(handle->chain, ctrl, mapping, &val) == 0) + changes |= V4L2_EVENT_CTRL_CH_VALUE; + + uvc_ctrl_send_event(handle, ctrl, mapping, val, changes); +} + +static void uvc_ctrl_status_event_work(struct work_struct *work) +{ + struct uvc_device *dev = container_of(work, struct uvc_device, + async_ctrl.work); + struct uvc_ctrl_work *w = &dev->async_ctrl; + struct uvc_control_mapping *mapping; + struct uvc_control *ctrl; + struct uvc_fh *handle; + __u8 *data; + unsigned int i; + + spin_lock_irq(&w->lock); + data = w->data; + w->data = NULL; + ctrl = w->ctrl; + handle = ctrl->handle; + ctrl->handle = NULL; + spin_unlock_irq(&w->lock); + + list_for_each_entry(mapping, &ctrl->info.mappings, list) { + s32 value = mapping->get(mapping, UVC_GET_CUR, data); + + for (i = 0; i < ARRAY_SIZE(mapping->slave_ids); ++i) { + if (!mapping->slave_ids[i]) + break; + + __uvc_ctrl_send_slave_event(handle, ctrl, + mapping->slave_ids[i]); + } + + if (mapping->v4l2_type == V4L2_CTRL_TYPE_MENU) { + struct uvc_menu_info *menu = mapping->menu_info; + unsigned int i; + + for (i = 0; i < mapping->menu_count; ++i, ++menu) + if (menu->value == value) { + value = i; + break; + } + } + + uvc_ctrl_send_event(handle, ctrl, mapping, value, + V4L2_EVENT_CTRL_CH_VALUE); + } + + kfree(data); +} + +void uvc_ctrl_status_event(struct uvc_device *dev, struct uvc_control *ctrl, + __u8 *data, size_t len) +{ + struct uvc_ctrl_work *w = &dev->async_ctrl; + + spin_lock(&w->lock); + w->data = kmalloc(len, GFP_ATOMIC); + if (w->data) { + memcpy(w->data, data, len); + w->ctrl = ctrl; + schedule_work(&w->work); + } + spin_unlock(&w->lock); +} + +static void uvc_ctrl_send_slave_event(struct uvc_fh *handle, + struct uvc_control *master, u32 slave_id, + const struct v4l2_ext_control *xctrls, unsigned int xctrls_count) +{ + unsigned int i; + /* * We can skip sending an event for the slave if the slave * is being modified in the same transaction. @@ -1255,14 +1333,7 @@ static void uvc_ctrl_send_slave_event(struct uvc_fh *handle, return; } - __uvc_find_control(master->entity, slave_id, &mapping, &ctrl, 0); - if (ctrl == NULL) - return; - - if (__uvc_ctrl_get(handle->chain, ctrl, mapping, &val) == 0) - changes |= V4L2_EVENT_CTRL_CH_VALUE; - - uvc_ctrl_send_event(handle, ctrl, mapping, val, changes); + __uvc_ctrl_send_slave_event(handle, master, slave_id); } static void uvc_ctrl_send_events(struct uvc_fh *handle, @@ -1277,6 +1348,10 @@ static void uvc_ctrl_send_events(struct uvc_fh *handle, for (i = 0; i < xctrls_count; ++i) { ctrl = uvc_find_control(handle->chain, xctrls[i].id, &mapping); + if (ctrl->info.flags & UVC_CTRL_FLAG_ASYNCHRONOUS) + /* Notification will be sent from an Interrupt event */ + continue; + for (j = 0; j < ARRAY_SIZE(mapping->slave_ids); ++j) { if (!mapping->slave_ids[j]) break; @@ -1472,9 +1547,10 @@ int uvc_ctrl_get(struct uvc_video_chain *chain, return __uvc_ctrl_get(chain, ctrl, mapping, &xctrl->value); } -int uvc_ctrl_set(struct uvc_video_chain *chain, +int uvc_ctrl_set(struct uvc_fh *handle, struct v4l2_ext_control *xctrl) { + struct uvc_video_chain *chain = handle->chain; struct uvc_control *ctrl; struct uvc_control_mapping *mapping; s32 value; @@ -1488,6 +1564,11 @@ int uvc_ctrl_set(struct uvc_video_chain *chain, return -EINVAL; if (!(ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR)) return -EACCES; + if (ctrl->info.flags & UVC_CTRL_FLAG_ASYNCHRONOUS) { + if (ctrl->handle) + return -EBUSY; + ctrl->handle = handle; + } /* Clamp out of range values. */ switch (mapping->v4l2_type) { @@ -1676,7 +1757,9 @@ static int uvc_ctrl_fill_xu_info(struct uvc_device *dev, | (data[0] & UVC_CONTROL_CAP_SET ? UVC_CTRL_FLAG_SET_CUR : 0) | (data[0] & UVC_CONTROL_CAP_AUTOUPDATE ? - UVC_CTRL_FLAG_AUTO_UPDATE : 0); + UVC_CTRL_FLAG_AUTO_UPDATE : 0) + | (data[0] & UVC_CONTROL_CAP_ASYNCHRONOUS ? + UVC_CTRL_FLAG_ASYNCHRONOUS : 0); uvc_ctrl_fixup_xu_info(dev, ctrl, info); @@ -2124,6 +2207,13 @@ static void uvc_ctrl_init_ctrl(struct uvc_device *dev, struct uvc_control *ctrl) if (!ctrl->initialized) return; + /* Temporarily abuse DATA_CURRENT buffer to avoid 1 byte allocation */ + if (!uvc_query_ctrl(dev, UVC_GET_INFO, ctrl->entity->id, + dev->intfnum, info->selector, + uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT), 1) && + uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT)[0] & 0x10) + ctrl->info.flags |= UVC_CTRL_FLAG_ASYNCHRONOUS; + for (; mapping < mend; ++mapping) { if (uvc_entity_match_guid(ctrl->entity, mapping->entity) && ctrl->info.selector == mapping->selector) @@ -2139,6 +2229,9 @@ int uvc_ctrl_init_device(struct uvc_device *dev) struct uvc_entity *entity; unsigned int i; + spin_lock_init(&dev->async_ctrl.lock); + INIT_WORK(&dev->async_ctrl.work, uvc_ctrl_status_event_work); + /* Walk the entities list and instantiate controls */ list_for_each_entry(entity, &dev->entities, list) { struct uvc_control *ctrl; diff --git a/drivers/media/usb/uvc/uvc_status.c b/drivers/media/usb/uvc/uvc_status.c index f552ab9..1d8f776 100644 --- a/drivers/media/usb/uvc/uvc_status.c +++ b/drivers/media/usb/uvc/uvc_status.c @@ -78,7 +78,24 @@ static void uvc_input_report_key(struct uvc_device *dev, unsigned int code, /* -------------------------------------------------------------------------- * Status interrupt endpoint */ -static void uvc_event_streaming(struct uvc_device *dev, __u8 *data, int len) +struct uvc_streaming_status { + __u8 bStatusType; + __u8 bOriginator; + __u8 bEvent; + __u8 bValue[]; +} __packed; + +struct uvc_control_status { + __u8 bStatusType; + __u8 bOriginator; + __u8 bEvent; + __u8 bSelector; + __u8 bAttribute; + __u8 bValue[]; +} __packed; + +static void uvc_event_streaming(struct uvc_device *dev, + struct uvc_streaming_status *status, int len) { if (len < 3) { uvc_trace(UVC_TRACE_STATUS, "Invalid streaming status event " @@ -86,30 +103,101 @@ static void uvc_event_streaming(struct uvc_device *dev, __u8 *data, int len) return; } - if (data[2] == 0) { + if (status->bEvent == 0) { if (len < 4) return; uvc_trace(UVC_TRACE_STATUS, "Button (intf %u) %s len %d\n", - data[1], data[3] ? "pressed" : "released", len); - uvc_input_report_key(dev, KEY_CAMERA, data[3]); + status->bOriginator, + status->bValue[0] ? "pressed" : "released", len); + uvc_input_report_key(dev, KEY_CAMERA, status->bValue[0]); } else { uvc_trace(UVC_TRACE_STATUS, "Stream %u error event %02x %02x " - "len %d.\n", data[1], data[2], data[3], len); + "len %d.\n", status->bOriginator, status->bEvent, + status->bValue[0], len); } } -static void uvc_event_control(struct uvc_device *dev, __u8 *data, int len) +#define UVC_CTRL_VALUE_CHANGE 0 +#define UVC_CTRL_INFO_CHANGE 1 +#define UVC_CTRL_FAILURE_CHANGE 2 +#define UVC_CTRL_MIN_CHANGE 3 +#define UVC_CTRL_MAX_CHANGE 4 + +static struct uvc_control *uvc_event_entity_ctrl(struct uvc_entity *entity, + __u8 selector) +{ + struct uvc_control *ctrl; + unsigned int i; + + for (i = 0, ctrl = entity->controls; i < entity->ncontrols; i++, ctrl++) + if (ctrl->info.selector == selector) + return ctrl; + + return NULL; +} + +static struct uvc_control *uvc_event_find_ctrl(struct uvc_device *dev, + struct uvc_control_status *status) +{ + struct uvc_video_chain *chain; + + list_for_each_entry(chain, &dev->chains, list) { + struct uvc_entity *entity; + struct uvc_control *ctrl; + + list_for_each_entry(entity, &chain->entities, chain) { + if (entity->id == status->bOriginator) { + ctrl = uvc_event_entity_ctrl(entity, + status->bSelector); + /* + * Some buggy cameras send asynchronous Control + * Change events for control, other than the + * ones, that had been changed, even though the + * AutoUpdate flag isn't set for the control. + */ + if (ctrl && (!ctrl->handle || + ctrl->handle->chain == chain)) + return ctrl; + } + } + } + + return NULL; +} + +static void uvc_event_control(struct uvc_device *dev, + struct uvc_control_status *status, int len) { - char *attrs[3] = { "value", "info", "failure" }; + struct uvc_control *ctrl; + char *attrs[] = { "value", "info", "failure", "min", "max" }; - if (len < 6 || data[2] != 0 || data[4] > 2) { + if (len < 6 || status->bEvent != 0 || + status->bAttribute >= ARRAY_SIZE(attrs)) { uvc_trace(UVC_TRACE_STATUS, "Invalid control status event " "received.\n"); return; } uvc_trace(UVC_TRACE_STATUS, "Control %u/%u %s change len %d.\n", - data[1], data[3], attrs[data[4]], len); + status->bOriginator, status->bSelector, + attrs[status->bAttribute], len); + + /* Find the control. */ + ctrl = uvc_event_find_ctrl(dev, status); + if (!ctrl) + return; + + switch (status->bAttribute) { + case UVC_CTRL_VALUE_CHANGE: + case UVC_CTRL_INFO_CHANGE: + uvc_ctrl_status_event(dev, ctrl, status->bValue, len - + offsetof(struct uvc_control_status, bValue)); + break; + case UVC_CTRL_FAILURE_CHANGE: + case UVC_CTRL_MIN_CHANGE: + case UVC_CTRL_MAX_CHANGE: + break; + } } static void uvc_status_complete(struct urb *urb) @@ -138,11 +226,13 @@ static void uvc_status_complete(struct urb *urb) if (len > 0) { switch (dev->status[0] & 0x0f) { case UVC_STATUS_TYPE_CONTROL: - uvc_event_control(dev, dev->status, len); + uvc_event_control(dev, + (struct uvc_control_status *)dev->status, len); break; case UVC_STATUS_TYPE_STREAMING: - uvc_event_streaming(dev, dev->status, len); + uvc_event_streaming(dev, + (struct uvc_streaming_status *)dev->status, len); break; default: diff --git a/drivers/media/usb/uvc/uvc_v4l2.c b/drivers/media/usb/uvc/uvc_v4l2.c index cded5ef..ee295f3 100644 --- a/drivers/media/usb/uvc/uvc_v4l2.c +++ b/drivers/media/usb/uvc/uvc_v4l2.c @@ -975,7 +975,7 @@ static int uvc_ioctl_s_ctrl(struct file *file, void *fh, if (ret < 0) return ret; - ret = uvc_ctrl_set(chain, &xctrl); + ret = uvc_ctrl_set(handle, &xctrl); if (ret < 0) { uvc_ctrl_rollback(handle); return ret; @@ -1050,7 +1050,7 @@ static int uvc_ioctl_s_try_ext_ctrls(struct uvc_fh *handle, return ret; for (i = 0; i < ctrls->count; ++ctrl, ++i) { - ret = uvc_ctrl_set(chain, ctrl); + ret = uvc_ctrl_set(handle, ctrl); if (ret < 0) { uvc_ctrl_rollback(handle); ctrls->error_idx = commit ? ctrls->count : i; diff --git a/drivers/media/usb/uvc/uvcvideo.h b/drivers/media/usb/uvc/uvcvideo.h index 7e4d3ee..a61c55b 100644 --- a/drivers/media/usb/uvc/uvcvideo.h +++ b/drivers/media/usb/uvc/uvcvideo.h @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -229,6 +230,8 @@ struct uvc_control { initialized:1; __u8 *uvc_data; + + struct uvc_fh *handle; /* Used for asynchronous event delivery */ }; struct uvc_format_desc { @@ -562,6 +565,13 @@ struct uvc_device { __u8 *status; struct input_dev *input; char input_phys[64]; + + struct uvc_ctrl_work { + struct work_struct work; + struct uvc_control *ctrl; + spinlock_t lock; + void *data; + } async_ctrl; }; enum uvc_handle_state { @@ -708,6 +718,8 @@ extern int uvc_ctrl_add_mapping(struct uvc_video_chain *chain, extern int uvc_ctrl_init_device(struct uvc_device *dev); extern void uvc_ctrl_cleanup_device(struct uvc_device *dev); extern int uvc_ctrl_restore_values(struct uvc_device *dev); +extern void uvc_ctrl_status_event(struct uvc_device *dev, + struct uvc_control *ctrl, __u8 *data, size_t len); extern int uvc_ctrl_begin(struct uvc_video_chain *chain); extern int __uvc_ctrl_commit(struct uvc_fh *handle, int rollback, @@ -726,7 +738,7 @@ static inline int uvc_ctrl_rollback(struct uvc_fh *handle) extern int uvc_ctrl_get(struct uvc_video_chain *chain, struct v4l2_ext_control *xctrl); -extern int uvc_ctrl_set(struct uvc_video_chain *chain, +extern int uvc_ctrl_set(struct uvc_fh *handle, struct v4l2_ext_control *xctrl); extern int uvc_xu_ctrl_query(struct uvc_video_chain *chain, diff --git a/include/uapi/linux/uvcvideo.h b/include/uapi/linux/uvcvideo.h index 3b08186..420883a 100644 --- a/include/uapi/linux/uvcvideo.h +++ b/include/uapi/linux/uvcvideo.h @@ -27,6 +27,8 @@ #define UVC_CTRL_FLAG_RESTORE (1 << 6) /* Control can be updated by the camera. */ #define UVC_CTRL_FLAG_AUTO_UPDATE (1 << 7) +/* Control supports asynchronous reporting */ +#define UVC_CTRL_FLAG_ASYNCHRONOUS (1 << 8) #define UVC_CTRL_FLAG_GET_RANGE \ (UVC_CTRL_FLAG_GET_CUR | UVC_CTRL_FLAG_GET_MIN | \