Return-Path: Received: from mail-by2nam01on0104.outbound.protection.outlook.com ([104.47.34.104]:11884 "EHLO NAM01-BY2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1754876AbeARVBW (ORCPT ); Thu, 18 Jan 2018 16:01:22 -0500 From: Sasha Levin To: "stable@vger.kernel.org" , "stable-commits@vger.kernel.org" CC: Sumit Semwal , Henrik Ingo , Laurent Pinchart , Mauro Carvalho Chehab , Sasha Levin Subject: [added to the 4.1 stable tree] [media] uvcvideo: uvc_scan_fallback() for webcams with broken chain Date: Thu, 18 Jan 2018 21:00:06 +0000 Message-ID: <20180118205908.3220-81-alexander.levin@microsoft.com> References: <20180118205908.3220-1-alexander.levin@microsoft.com> In-Reply-To: <20180118205908.3220-1-alexander.levin@microsoft.com> Content-Language: en-US Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Sender: stable-owner@vger.kernel.org List-ID: From: Sumit Semwal This patch has been added to the stable tree. If you have any objections, please let us know. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D [ Upstream commit e950267ab802c8558f1100eafd4087fd039ad634 ] Some devices have invalid baSourceID references, causing uvc_scan_chain() to fail, but if we just take the entities we can find and put them together in the most sensible chain we can think of, turns out they do work anyway. Note: This heuristic assumes there is a single chain. At the time of writing, devices known to have such a broken chain are - Acer Integrated Camera (5986:055a) - Realtek rtl157a7 (0bda:57a7) Signed-off-by: Henrik Ingo Signed-off-by: Laurent Pinchart Signed-off-by: Mauro Carvalho Chehab Signed-off-by: Sasha Levin --- drivers/media/usb/uvc/uvc_driver.c | 118 +++++++++++++++++++++++++++++++++= ++-- 1 file changed, 112 insertions(+), 6 deletions(-) diff --git a/drivers/media/usb/uvc/uvc_driver.c b/drivers/media/usb/uvc/uvc= _driver.c index 5970dd6a1c1c..a41a4f6a51b2 100644 --- a/drivers/media/usb/uvc/uvc_driver.c +++ b/drivers/media/usb/uvc/uvc_driver.c @@ -1574,6 +1574,114 @@ static const char *uvc_print_chain(struct uvc_video= _chain *chain) return buffer; } =20 +static struct uvc_video_chain *uvc_alloc_chain(struct uvc_device *dev) +{ + struct uvc_video_chain *chain; + + chain =3D kzalloc(sizeof(*chain), GFP_KERNEL); + if (chain =3D=3D NULL) + return NULL; + + INIT_LIST_HEAD(&chain->entities); + mutex_init(&chain->ctrl_mutex); + chain->dev =3D dev; + v4l2_prio_init(&chain->prio); + + return chain; +} + +/* + * Fallback heuristic for devices that don't connect units and terminals i= n a + * valid chain. + * + * Some devices have invalid baSourceID references, causing uvc_scan_chain= () + * to fail, but if we just take the entities we can find and put them toge= ther + * in the most sensible chain we can think of, turns out they do work anyw= ay. + * Note: This heuristic assumes there is a single chain. + * + * At the time of writing, devices known to have such a broken chain are + * - Acer Integrated Camera (5986:055a) + * - Realtek rtl157a7 (0bda:57a7) + */ +static int uvc_scan_fallback(struct uvc_device *dev) +{ + struct uvc_video_chain *chain; + struct uvc_entity *iterm =3D NULL; + struct uvc_entity *oterm =3D NULL; + struct uvc_entity *entity; + struct uvc_entity *prev; + + /* + * Start by locating the input and output terminals. We only support + * devices with exactly one of each for now. + */ + list_for_each_entry(entity, &dev->entities, list) { + if (UVC_ENTITY_IS_ITERM(entity)) { + if (iterm) + return -EINVAL; + iterm =3D entity; + } + + if (UVC_ENTITY_IS_OTERM(entity)) { + if (oterm) + return -EINVAL; + oterm =3D entity; + } + } + + if (iterm =3D=3D NULL || oterm =3D=3D NULL) + return -EINVAL; + + /* Allocate the chain and fill it. */ + chain =3D uvc_alloc_chain(dev); + if (chain =3D=3D NULL) + return -ENOMEM; + + if (uvc_scan_chain_entity(chain, oterm) < 0) + goto error; + + prev =3D oterm; + + /* + * Add all Processing and Extension Units with two pads. The order + * doesn't matter much, use reverse list traversal to connect units in + * UVC descriptor order as we build the chain from output to input. This + * leads to units appearing in the order meant by the manufacturer for + * the cameras known to require this heuristic. + */ + list_for_each_entry_reverse(entity, &dev->entities, list) { + if (entity->type !=3D UVC_VC_PROCESSING_UNIT && + entity->type !=3D UVC_VC_EXTENSION_UNIT) + continue; + + if (entity->num_pads !=3D 2) + continue; + + if (uvc_scan_chain_entity(chain, entity) < 0) + goto error; + + prev->baSourceID[0] =3D entity->id; + prev =3D entity; + } + + if (uvc_scan_chain_entity(chain, iterm) < 0) + goto error; + + prev->baSourceID[0] =3D iterm->id; + + list_add_tail(&chain->list, &dev->chains); + + uvc_trace(UVC_TRACE_PROBE, + "Found a video chain by fallback heuristic (%s).\n", + uvc_print_chain(chain)); + + return 0; + +error: + kfree(chain); + return -EINVAL; +} + /* * Scan the device for video chains and register video devices. * @@ -1596,15 +1704,10 @@ static int uvc_scan_device(struct uvc_device *dev) if (term->chain.next || term->chain.prev) continue; =20 - chain =3D kzalloc(sizeof(*chain), GFP_KERNEL); + chain =3D uvc_alloc_chain(dev); if (chain =3D=3D NULL) return -ENOMEM; =20 - INIT_LIST_HEAD(&chain->entities); - mutex_init(&chain->ctrl_mutex); - chain->dev =3D dev; - v4l2_prio_init(&chain->prio); - term->flags |=3D UVC_ENTITY_FLAG_DEFAULT; =20 if (uvc_scan_chain(chain, term) < 0) { @@ -1618,6 +1721,9 @@ static int uvc_scan_device(struct uvc_device *dev) list_add_tail(&chain->list, &dev->chains); } =20 + if (list_empty(&dev->chains)) + uvc_scan_fallback(dev); + if (list_empty(&dev->chains)) { uvc_printk(KERN_INFO, "No valid video chain found.\n"); return -1; --=20 2.11.0