Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752186AbdHHMqr (ORCPT ); Tue, 8 Aug 2017 08:46:47 -0400 Received: from mail-ve1eur01on0105.outbound.protection.outlook.com ([104.47.1.105]:59200 "EHLO EUR01-VE1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1751993AbdHHMqn (ORCPT ); Tue, 8 Aug 2017 08:46:43 -0400 Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=peda@axentia.se; Subject: Re: [PATCH v2 2/3] usb: chipidea: Hook into mux framework to toggle usb switch To: Stephen Boyd , Peter Chen Cc: linux-usb@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.infradead.org, Rob Herring , Rob Clark , Andy Gross , Greg Kroah-Hartman , devicetree@vger.kernel.org References: <20170714214005.14967-1-stephen.boyd@linaro.org> <20170714214005.14967-3-stephen.boyd@linaro.org> <7080030c-c9c2-657d-aad4-aef636438cb1@axentia.se> <150215709134.18242.599725161375995991@sboyd-linaro> From: Peter Rosin Organization: Axentia Technologies AB Message-ID: Date: Tue, 8 Aug 2017 14:46:30 +0200 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.2.1 MIME-Version: 1.0 In-Reply-To: <150215709134.18242.599725161375995991@sboyd-linaro> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit X-Originating-IP: [87.96.186.165] X-ClientProxiedBy: HE1P195CA0016.EURP195.PROD.OUTLOOK.COM (2603:10a6:3:fd::26) To AM5PR0202MB2545.eurprd02.prod.outlook.com (2603:10a6:203:6c::23) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 5a34e7a1-c157-4ea9-18d5-08d4de5b8396 X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:(300000500095)(300135000095)(300000501095)(300135300095)(22001)(300000502095)(300135100095)(2017030254152)(2017082002075)(300000503095)(300135400095)(201703131423075)(201702281549075)(300000504095)(300135200095)(300000505095)(300135600095)(300000506095)(300135500095);SRVR:AM5PR0202MB2545; X-Microsoft-Exchange-Diagnostics: 1;AM5PR0202MB2545;3:VJTRqvlneBT2UUnyCNbaMFf+Bqdk4a8DFCY5UHL5KtZ6KottgjBX0YAlHHM3ZqGmUOkNwAfBBEN3yRBD1OP55VHXgOPbyEuil8GU3EX0uKPJ9LqxiYgfymP0HHqV0YxMy3HFjj6dygUZtWWgWvU3dgOYAHTlDELFPf2xZ2V3OwpQ3oVWiIZTszyFGs/8qVrqsoj9w9jkGMEm4XrjjZnInEO4cKt03QoRd80u8t4jjMJfJ87b6OCavtjNFK/OuGDD;25:AlhNXIwpJkBAuE7PhpVwYsbt91drVYqDqwTZ4esnSEszbz8GyNSZ+cJkJrrCNmjngEEautJQKU6B+KbW3Pof8p4Ua0eGBL0Cgk8pGpf1ZRPBqS0s2pvGiaOpwwu0YBBmWB1Eh06snuicc6JsGA01RC+zokqvSNwAggvdvU8qpvIvsfQGHcDZdns1O4FYtx/KOKJL2YKF1k5hYiWIZPQ9Vu9esMmzNR/wPKt3iUsb7mkplJ/wcztKiWCSrq7cxbDNt5QSyyrkrnacDFPv9e20sWZXtwZVrl9Gp1CFkxU/fTfOpuJkxiGCuGJP6Xq+P7eA+sBxqB3tn7+RScbdwirlGQ==;31:xB7LEptfHCDt/HKlyUY7QoeYuVR3SVU2ApROkwsArCecZLQr36LccZnKEPEcyVLY38i/TdRSvDqYhqvyMc1qhNBWh5OBS97AZ0533uUY/o0kjjOY6KpvvtGocjQanu4pCaQSDG42oyFrlyzEcFm6PvDa1P4NscuStYnXVezGw9TrFwEsbHAZzmXRK6G63X+8o9ytfLdY5uqAqoqu15/x3Uhxzf53su5UnERXJ7tFqn0= X-MS-TrafficTypeDiagnostic: AM5PR0202MB2545: X-Exchange-Antispam-Report-Test: UriScan:; X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(100000700101)(100105000095)(100000701101)(100105300095)(100000702101)(100105100095)(6040450)(601004)(2401047)(5005006)(8121501046)(10201501046)(3002001)(93006095)(93001095)(100000703101)(100105400095)(6041248)(20161123564025)(20161123560025)(20161123555025)(2016111802025)(20161123558100)(20161123562025)(201703131423075)(201702281528075)(201703061421075)(201703061406153)(6072148)(6043046)(201708071742011)(100000704101)(100105200095)(100000705101)(100105500095);SRVR:AM5PR0202MB2545;BCL:0;PCL:0;RULEID:(100000800101)(100110000095)(100000801101)(100110300095)(100000802101)(100110100095)(100000803101)(100110400095)(100000804101)(100110200095)(100000805101)(100110500095);SRVR:AM5PR0202MB2545; X-Microsoft-Exchange-Diagnostics: 1;AM5PR0202MB2545;4:BS8jQQ/nGRcsG3ndK+g5aOc6YTRFNiGS1qXQ5LrdbJMs4k0OTohbVrS587AXzONj9Y1BqmpJoCOgr/8HlUfUp0DLVPhL8wZKCsOAgf00j3oQTcDiH1BPtvFi+P6Q/+USOrLG9WlyzHW9Z5diJg4Jo4hteneHae2DrMa+3P0/2Xl7DGIxVqhNgkvim373mus/L0ArsBXASeAZKxa9dNToxBcVl53nFD8R5+/xSZlyPdf+MhwE0dQW8fUt5QFuLRNk X-Forefront-PRVS: 03932714EB X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10019020)(7370300001)(4630300001)(6049001)(6009001)(39830400002)(39400400002)(39410400002)(39450400003)(189002)(377424004)(199003)(24454002)(25786009)(74482002)(7736002)(31686004)(50986999)(65956001)(66066001)(305945005)(47776003)(7416002)(106356001)(97736004)(65806001)(3260700006)(117156002)(33646002)(36756003)(76176999)(6486002)(189998001)(50466002)(230700001)(54356999)(101416001)(478600001)(2906002)(4001350100001)(42186005)(2950100002)(6666003)(229853002)(3846002)(105586002)(64126003)(6116002)(77096006)(68736007)(93886004)(53936002)(8656003)(6246003)(54906002)(31696002)(86362001)(38730400002)(5660300001)(4326008)(81166006)(53546010)(65826007)(23676002)(81156014)(83506001)(8676002)(7350300001)(42262002);DIR:OUT;SFP:1102;SCL:1;SRVR:AM5PR0202MB2545;H:[192.168.4.73];FPR:;SPF:None;PTR:InfoNoRecords;MX:1;A:1;LANG:en; X-Microsoft-Exchange-Diagnostics: =?utf-8?B?MTtBTTVQUjAyMDJNQjI1NDU7MjM6K0JiQWp6R3RSSldoakEvTFluZU1TV2ho?= =?utf-8?B?QWhWVENXVkp0VDV2YmtvS0o3aHFhYklqQTRSOEFPRHBkdHd5dXdKdFY1VXh1?= =?utf-8?B?bmVXWDNRTVVtYWh6bTU2TWJNdzdaUEJjNjBmeEgwM3U5blo4OTNjeHBQZk4z?= =?utf-8?B?ZnJvaXpHZTVMMG16Zmc1bkY3NkZFNUQxMlNES1VQaXRmcmt6VERrcU9PcWlX?= =?utf-8?B?T1dmN2tzSUdITzlPNStNWnMxbHlWLzc1RmFSYTRYeEcrVjl1NnJVQnRHMUNL?= =?utf-8?B?eHI5TUJPUFRpU0FFL09uVUZKOGNLUGRpdW1jZkJBT1I3bUEvOFNwRStKRy9l?= =?utf-8?B?UnRZZG5RZ1JCclFEVE42MDY4MVMxbERlYmVNNURVeGV3TE1GZXltdHNsZFlv?= =?utf-8?B?ZjczUjE0THBHYWg3aGc5RE80U3FibzdYL2FWTlJyVEVERVljaXlQOWkyZ2Jq?= =?utf-8?B?OGEwL0ZUeG15dHdzMGN1dTl6czVidXBsMUdHa3grdDVOMEQ2Smk4aWtGeUhj?= =?utf-8?B?ZHlmUkptVUt2cXE2NW1SZTZERUROQUduTGhrTkhsQWl5TGZpQ1NleEJLRUgx?= =?utf-8?B?ekJ4MWNFQzZSUUpQVzRLam9US2ZvNlh1K2w1MjJIcnpsSUlQWks2Q0xTQmQ1?= =?utf-8?B?MzlqSGZzWmtMRFhHSjdEeG1rVjRsR1hRVEJrenVoeWhoRGZhd05CMzZlUXN3?= =?utf-8?B?SDdINFk3ejRHeUtldVI2cXFZTG1acDV3K0JmY29oUVVhb1hGL0RCSit4L2Z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FDK6fU/IHJHaUKccYtBcl1JyueeJCo5zXgiQ7vpjsYdeEf3Edpas53pCY6ckXckAwLFtsw40RSP5Hh6LIWf61EXpqaQkphlt4GpYc78Ky5kJOFwmy0uK8nA==;24:lSFiNh4eaGtepUX4aY3WjoF3SGb/ZSj2JiC/DkmqcuclJUmWRTdcL3oevqgsgOF019vvkXhwV9NzAI078wGmuX0UQD/wZEZzV7tETlcQFXk=;7:9yBhoKUkQ7REWdoUfa0DjNmkSpjnSzzBFduxUMkApy8Da4UTOUOcZVKuhJejx1++hefSjQe2bynmlJqDPUxciwUQdfAfvN+nr7/67TvlB6ZXKBcrDbzrbN07+tkwrSA73j0uPCAHDzFuxmw7NFDHCz4SmkIXqjNjjmC95y0eYTmr3ldRs6QkVb5LixKRKhCdvAk26SHq47T2fVZigiogGgnoow4NPD8bYV47X4aCvZA= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-OriginatorOrg: axentia.se X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Aug 2017 12:46:36.8687 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM5PR0202MB2545 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 4442 Lines: 104 On 2017-08-08 03:51, Stephen Boyd wrote: > Quoting Peter Rosin (2017-07-31 03:33:22) >> On 2017-07-14 23:40, Stephen Boyd wrote: >>> @@ -1964,16 +1965,26 @@ void ci_hdrc_gadget_destroy(struct ci_hdrc *ci) >>> >>> static int udc_id_switch_for_device(struct ci_hdrc *ci) >>> { >>> + int ret = 0; >>> + >>> if (ci->is_otg) >>> /* Clear and enable BSV irq */ >>> hw_write_otgsc(ci, OTGSC_BSVIS | OTGSC_BSVIE, >>> OTGSC_BSVIS | OTGSC_BSVIE); >>> >>> - return 0; >>> + if (!ci_otg_is_fsm_mode(ci)) >>> + ret = mux_control_select(ci->platdata->usb_switch, 0); >>> + >>> + if (ci->is_otg && ret) >>> + hw_write_otgsc(ci, OTGSC_BSVIE | OTGSC_BSVIS, OTGSC_BSVIS); >>> + >>> + return ret; >>> } >>> >>> static void udc_id_switch_for_host(struct ci_hdrc *ci) >>> { >>> + mux_control_deselect(ci->platdata->usb_switch); >>> + >> >> This looks broken. You conditionally lock the mux and you unconditionally >> unlock it. Quoting the mux_control_deselect doc: >> >> * It is required that a single call is made to mux_control_deselect() for >> * each and every successful call made to either of mux_control_select() or >> * mux_control_try_select(). >> >> Think of the mux as a semaphore with a max count of one. If you lock it, >> you have to unlock it when you're done. If you didn't lock it, you have >> zero business unlocking it. If you try to lock it but fail, you also have >> no business unlocking it. Just like a semaphore. > > Good catch. I've added a if (!ci_otg_is_fsm_mode()) check here. > >> >> Another point: I do not know if udc_id_switch_for_host is *only* called >> when there has been a prior call to udc_id_switch_for_device that >> succeeded or how this works exactly. But this all looks fragile. Using >> mux_control_select and mux_control_deselect *must* be done in pairs. >> If you want a mux to be locked for "a while", such as in this case, you >> have to make sure you stay within the rules. There is no room for half >> measures, or you will likely cause a deadlock when something unexpected >> happens. >> > > Can you elaborate? Is it bad that we keep it "locked" while we're in > host or device mode? No no, that's good. You do not want some other consumer of the same mux controller to clobber your state (in case it's shared), so you absolutely want to have the mux locked when you want it to remain in a specific position. > It looked like we paired the start/stop ops with > each other so that the mux is properly managed across these ops. Yes, it *looks* ok... > My > testing hasn't shown a problem, but maybe there's some corner case > you're thinking of? I'll double check the code. ...but since I do not know the usb code, I can't tell. What I worry about is the usb core calling udc_id_switch_for_host or udc_id_switch_for_device more than once without any call to the other in between. Maybe that is a guarantee that the usb core makes? Or maybe it isn't? If e.g. there is a third mode (or if one is added in the future), then the calls to mux_control_select and mux_control_deselect would not be paired correctly. Ok, sure, a third mode probably doesn't exist and will probably not be added, but but but... Also, what happens if udc_id_switch_for_device fails? Is it certain that it will be called again before udc_id_switch_for_host is called, or is the failure simply logged? If the latter, you might have a call to mux_control_deselect without a preceding (and successful) call to mux_control_select. That's fatal. I have similar worries for host_start/host_stop, but for that case host_stop is not allowed to fail, and it seems like a safe bet that host_stop will only be called if host_start succeeds. So, I'm not as worried there. In other words, the question is if the usb core is designed to allow this kind of "raw" resource administration in udc_id_switch_for_host and udc_id_switch_for_device, or if you need to keep a local record of the state so that you do not do double resource acquisition or attempt to free resources you don't have? I think I would feel better if the muxing for the device mode could be done in a start/stop pair of function just like the host mode is doing. Again, I don't know the usb code and don't know if such hooks exist or not? Cheers, Peter