Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753513AbbGUMBy (ORCPT ); Tue, 21 Jul 2015 08:01:54 -0400 Received: from mail-by2on0119.outbound.protection.outlook.com ([207.46.100.119]:46262 "EHLO na01-by2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1751219AbbGUMBv (ORCPT ); Tue, 21 Jul 2015 08:01:51 -0400 Authentication-Results: spf=fail (sender IP is 192.88.158.2) smtp.mailfrom=freescale.com; vger.kernel.org; dkim=none (message not signed) header.d=none; Date: Tue, 21 Jul 2015 18:52:17 +0800 From: Li Jun To: Roger Quadros CC: , , , , , , , , , , , Subject: Re: [PATCH v3 07/11] usb: otg: add OTG core Message-ID: <20150721105159.GA8903@shlinux2> References: <1436350777-28056-1-git-send-email-rogerq@ti.com> <1436350777-28056-8-git-send-email-rogerq@ti.com> <20150717074832.GA4515@shlinux2> <55A8EFBA.1000504@ti.com> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Disposition: inline In-Reply-To: <55A8EFBA.1000504@ti.com> User-Agent: Mutt/1.5.21 (2010-09-15) X-EOPAttributedMessage: 0 X-Microsoft-Exchange-Diagnostics: 1;BY2FFO11FD013;1:MQmLyKJXfaWkDcU42RdJLo08LqEbFLaEHvKj+0tgNSuL+PsAD9uW89gkvwsF1/Xn7Q9C1HY7zv2dM2y2m0O8kdJjoWcHUwaNMzXxHqzMnSUvzstXiERanYqxk7aJThmvYc7wFl0CV0ycPKwtZGc6Vaj2WxMve34I3929lE22uTkcVdMweaSycxoim8G0lv0sJ1XI6ZN6S740GcbzFluFiYRtZ6J3Wdw+MLCv9oDvWTUjptLnU9xJn1q8AeyMSHFFyjaaqlV066cRwGELuKe/HbD8F57T1q7x2L8KG9ZunZyrMWBEzQo22EKQ6+MO2rXsuSVW8tj64qzEUTrx/dOuegFa3pPPxQPgjQJKRCTvEZ86Iq8ltqR+UAmvyTdVKf23jChZ3OdEEsN8yyChbkQbZK0AI0nz0sE+20RXDe8dsD5DFlEA/YP7LhqKUTVpr7tO X-Forefront-Antispam-Report: CIP:192.88.158.2;CTRY:US;IPV:NLI;EFV:NLI;SFV:NSPM;SFS:(10019020)(6009001)(2980300002)(339900001)(199003)(189002)(97756001)(105606002)(33656002)(47776003)(85426001)(77096005)(87936001)(92566002)(33716001)(93886004)(104016003)(110136002)(62966003)(77156002)(6806004)(50986999)(23726002)(189998001)(15975445007)(19580395003)(46102003)(46406003)(5001920100001)(76176999)(5001960100002)(106466001)(2950100001)(54356999)(50466002);DIR:OUT;SFP:1102;SCL:1;SRVR:DM2PR0301MB0861;H:az84smr01.freescale.net;FPR:;SPF:Fail;MLV:sfv;A:1;MX:1;LANG:en; X-Microsoft-Exchange-Diagnostics: 1;DM2PR0301MB0861;2:6nSVYFYSQjaKx0A4yqHTP/AKmbtb/0ru68Hhl5S8HqXpa+tz5D9NqTnACixaxFacVnTQEjw0qcNkZdIE9FIhSHEDr4m1Sm3raBfi73q4lmT00Bh44tgz5mS2gIz18QvTrTzDierhSU6eiDob7dkL5w8aV2UdACo+KzpPegbGEG0=;3:u6x44J/Cqw/QhNPSJ+YI5p6YOwXdrcy7oze+yG1+/iYaJMqV9pgZLiCKr2z9Xnc11N0gZ2ErDYzWqiJ09ijmzJsfOXHi8yiK+sL4otE2tigtBzt9n1JaegeaYtVslkt2C4UeUfb4dcIDZuQubwijuayCqEp3sqQdxD17xRiAvsqugK2WpX2zN8IsvEFQpbSTJiynIgbFGf+jsACbhJPh/pqwGGHVmY3mhUwMdywZOnI=;25:vGmEhp/y9az/V2ha5DnoNAl764s6tP5abxx3zjRRYpufftxIP95w9+4sneKm9bF5vvGaRYUp2/zq5mAnD+fW7I2MOiZf0wqF4DGqfDLkCSVBEVx1t8HryoMFAZ+P4PQ9OkODmWwB/vFgLqT2ZQPO1JTvGuIOljlrZplQPjV2uYj5MUrl8aFs2tq018XBpg6cARHpDjhta/k1DwbxfhGXDGRHZzkWBmLVkeY0X45ygKi00Yds+L485EomVkmwby5Pv3g7aP8XzU7dmcSMgJA1uw== X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:;SRVR:DM2PR0301MB0861; X-Microsoft-Exchange-Diagnostics: 1;DM2PR0301MB0861;20:2l6bKtnP/kkN5UEO8l8uAVx4HUCK2y1ud+6373Q/zdNYbD4n4VsmKLYVqgRpXIi9VKhKsGHJCa4uYNmk6tudrCX1wp9vzCisZq3alxEUzBiZtyrgEBcNVrHV9BEbLxQaZ6yub120SgB0MRVj2SLpKJLYf2o56/dY66AZ2Y4YMNm65Osg97CYgC/is9C32oz/dW5GwYbMqp0CXWDIGGFLBnyqGIej+e2/XcDWAzxmpdsn8XbUEOR4kAaE4xdjZR1zaE1O8+17knDMXf3S+rvFAJ5JPsEfi1KuxWEWPTII+pTAe42NvU9aG3Vse358915E1qZ+Fo9oJpTymyJXaJjk6CSDasFH2cMm7yqHNa8tbWs=;4:26FUZLthTAT1tApIVuBpWr2Sw/Q09BvAV5eQYqZtCCid6EcmwMYmCRMyz4orQu+Fc0X4jo2svN0b9l3Uo6t6xRXV0L4HSQP36DbpMf1jp35gPG84oNtgGuXvsch1pF48lJ6WhE56rXuwYDrJstQy4RZRalqt2Nv1yQOGPSjdtkwKeLYHXzUVR8wxtdQg8n6CN/Z9GAWZL+wSgMiPmiyzDwFXerOuwZblDeodTr6jXHnMfSzXyJn6qpEkLXJS2h0X+Jnju+rdzGrwbV1H5lsJGAJqfTNBkgWFEqpaZ++fXIY= DM2PR0301MB0861: X-MS-Exchange-Organization-RulesExecuted X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:; X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(601004)(5005006)(3002001);SRVR:DM2PR0301MB0861;BCL:0;PCL:0;RULEID:;SRVR:DM2PR0301MB0861; X-Forefront-PRVS: 0644578634 X-Microsoft-Exchange-Diagnostics: 1;DM2PR0301MB0861;23:J6gUjxO57JY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Sch5qVDM0yasQ==;24:mVczGItFWFA404nelaVPGHJ5f2rCSaYHlbTDK22w+Ohpczx3udoeCyUFWy2gBboynHXBjiL/wAh+W5zQMkfv1/H1rcs5TRDwZ/LbxbBmOQ4=;20:Ex7mHJLtu7Ud3+rNO7qQKj9/z5lMIeZ/Z2DaOoc0Q10SYma56Ke5+AF0SGLgXB9PLS7R4t05xzAp+xkQAlEwaQ== X-OriginatorOrg: freescale.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 21 Jul 2015 12:01:48.2598 (UTC) X-MS-Exchange-CrossTenant-Id: 710a03f5-10f6-4d38-9ff4-a80b81da590d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=710a03f5-10f6-4d38-9ff4-a80b81da590d;Ip=[192.88.158.2];Helo=[az84smr01.freescale.net] X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM2PR0301MB0861 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 12377 Lines: 409 Hi, [...] > >> + otg_timer_init(A_WAIT_ENUM, otgd, set_tmout, TB_SRP_FAIL, NULL); > > > > 2 timers are missing: B_DATA_PLS, B_SSEND_SRP. > > Those 2 are not used by usb-otg-fsm.c. We can add it when usb-otg-fsm.c is updated. > ok. > > > >> +} [...] > >> + > >> +/** > >> + * OTG FSM ops function to start/stop host > >> + */ > >> +static int usb_otg_start_host(struct otg_fsm *fsm, int on) > >> +{ > >> + struct otg_data *otgd = container_of(fsm, struct otg_data, fsm); > >> + struct otg_hcd_ops *hcd_ops; > >> + > >> + dev_dbg(otgd->dev, "otg: %s %d\n", __func__, on); > >> + if (!fsm->otg->host) { > >> + WARN_ONCE(1, "otg: fsm running without host\n"); > >> + return 0; > >> + } > >> + > >> + if (on) { > >> + /* OTG device operations */ > >> + if (otgd->start_host) > >> + otgd->start_host(fsm, on); > >> + > >> + /* start host */ > >> + hcd_ops = otgd->primary_hcd.ops; > >> + hcd_ops->add(otgd->primary_hcd.hcd, otgd->primary_hcd.irqnum, > >> + otgd->primary_hcd.irqflags); > >> + if (otgd->shared_hcd.hcd) { > >> + hcd_ops = otgd->shared_hcd.ops; > >> + hcd_ops->add(otgd->shared_hcd.hcd, > >> + otgd->shared_hcd.irqnum, > >> + otgd->shared_hcd.irqflags); > >> + } > >> + } else { > >> + /* stop host */ > >> + if (otgd->shared_hcd.hcd) { > >> + hcd_ops = otgd->shared_hcd.ops; > >> + hcd_ops->remove(otgd->shared_hcd.hcd); > >> + } > >> + hcd_ops = otgd->primary_hcd.ops; > >> + hcd_ops->remove(otgd->primary_hcd.hcd); > >> + > >> + /* OTG device operations */ > >> + if (otgd->start_host) > >> + otgd->start_host(fsm, on); > >> + } > >> + > >> + return 0; > >> +} > > > > I do not see much benefit by this override function, usb_add/remove_hcd > > can be simply included by controller's start_host function, also there > > maybe some additional operations after usb_add_hcd, but this override > > function limit usb_add_hcd() is the last step. > > I had tried host start/stop way before but Alan's suggestion was to use > bind/unbind the host controller completely as that is much simpler > > [1] http://article.gmane.org/gmane.linux.usb.general/123842 > I did not mean host start/stop in your first version, I agree using usb_add/remove_hcd() for simple. > > > > Maybe your intention is to make usb_add_hcd is the only operation required > > to start host, so ideally controller driver need not define its start_host > > routine for this otg ops, I am not sure if this can work for different otg > > Yes that was the intention. > > > platforms. If the shared code is only usb_add/remove_hcd(), maybe leave this > > ops defined by controller driver can make core code simple and give flexibility > > to controller drivers. > > We don't completely override start/stop_host(). The flexibility is still there. > We call controllers start_host(1) before starting the controller and controllers > start_host(0) after stopping the controller. > So the the controller can still do what they want in otg_fsm_ops.start_host/gadget(). > But if controller driver wants to do something after usb_otg_add_hcd(), it's impossible with your current usb_otg_start_host(). > The OTG core only takes care of actually starting/stopping the host controller. > > If we don't do that then the code in usb_otg_start_host() has to be pasted > in every OTG controller driver. This is code duplication. > Actually the only duplication code may be a function call to original usb_add/remove_hcd(). > > > >> + > >> +/** > >> + * OTG FSM ops function to start/stop gadget > >> + */ > >> +static int usb_otg_start_gadget(struct otg_fsm *fsm, int on) > >> +{ > >> + struct otg_data *otgd = container_of(fsm, struct otg_data, fsm); > >> + struct usb_gadget *gadget = fsm->otg->gadget; > >> + > >> + dev_dbg(otgd->dev, "otg: %s %d\n", __func__, on); > >> + if (!gadget) { > >> + WARN_ONCE(1, "otg: fsm running without gadget\n"); > >> + return 0; > >> + } > >> + > >> + if (on) { > >> + /* OTG device operations */ > >> + if (otgd->start_gadget) > >> + otgd->start_gadget(fsm, on); > >> + > >> + otgd->gadget_ops->start(fsm->otg->gadget); > >> + } else { > >> + otgd->gadget_ops->stop(fsm->otg->gadget); > >> + > >> + /* OTG device operations */ > >> + if (otgd->start_gadget) > >> + otgd->start_gadget(fsm, on); > >> + } > >> + > >> + return 0; > >> +} > >> + > >> +/** > >> + * OTG FSM work function > >> + */ > >> +static void usb_otg_work(struct work_struct *work) > >> +{ > >> + struct otg_data *otgd = container_of(work, struct otg_data, work); > >> + > >> + otg_statemachine(&otgd->fsm); > > > > Need consider runtime pm, or you want to rely on controller driver take > > care of it? > > For simplicity let's say that controller driver takes care of it. > Then controller driver need add runtime pm for every otg fsm ops. Code like below can make it simple: runtime_pm_get_sync(otgd->dev); otg_statemachine(&otgd->fsm); runtime_pm_get_put(otgd->dev); There is another problem, otg work will only do one state transition, but in some cases we may need successive state transitions. > > > >> +} > >> + > >> +/** > >> + * usb_otg_register() - Register the OTG device to OTG core > >> + * @parent_device: parent device of Host & Gadget controllers. > >> + * @otg_fsm_ops: otg state machine ops. > >> + * [...] > >> +/** > >> + * start/kick the OTG FSM if we can > >> + * fsm->lock must be held > >> + */ > >> +static void usb_otg_start_fsm(struct otg_fsm *fsm) > >> +{ > >> + struct otg_data *otgd = container_of(fsm, struct otg_data, fsm); > >> + > >> + if (otgd->fsm_running) > >> + goto kick_fsm; > >> + > >> + if (!fsm->otg->host) { > >> + dev_info(otgd->dev, "otg: can't start till host registers\n"); > >> + return; > >> + } > >> + > > > > This cannot work, fsm->otg->host is set in usb_otg_register_hcd(), which is > > called by usb_add_hcd(), but usb_add_hcd() should be called only if otg fsm > > already started to some A-device state, deadlock. > > I've changed usb_add_hcd() behaviour. Now usb_otg_add_hcd() does the real work of adding > the hcd. usb_add_hcd() prevents the add if it is an otg hcd and just registers > with OTG core. > So you expect the controller driver still call usb_add_hcd() before otg fsm start, in which it only registers the created hcd with OTG core. > > > >> + if (!fsm->otg->gadget) { > >> + dev_info(otgd->dev, "otg: can't start till gadget registers\n"); > >> + return; > >> + } > >> + > >> + otgd->fsm_running = true; > >> +kick_fsm: > >> + queue_work(otgd->wq, &otgd->work); > >> +} > >> + [...] > >> + > >> +/** > >> + * usb_otg_register_hcd - Register Host controller to OTG core > >> + * @hcd: Host controller device > >> + * @irqnum: interrupt number > >> + * @irqflags: interrupt flags > >> + * @ops: HCD ops to add/remove the HCD > >> + * > >> + * This is used by the USB Host stack to register the Host controller > >> + * to the OTG core. Host controller must not be started by the > >> + * caller as it is left upto the OTG state machine to do so. > >> + * > >> + * Returns: 0 on success, error value otherwise. > >> + */ > >> +int usb_otg_register_hcd(struct usb_hcd *hcd, unsigned int irqnum, > >> + unsigned long irqflags, struct otg_hcd_ops *ops) > >> +{ > >> + struct otg_data *otgd; > >> + struct device *otg_dev = hcd->self.controller->parent; > >> + I see normally we directly use controller dev for hcd->self.controller, usb_create_hcd(... struct device *dev, ...) { ... ... hcd->self.controller = dev; ... ... } For register gadget, it's okay since: int usb_add_gadget_udc_release(struct device *parent, ...) { ... ... gadget->dev.parent = parent; ... ... } So we need parent dev for usb_otg_register(struct device *dev,...), and child dev for usb_create_hcd(struct device *dev,...)? dwc3 is designed like this? > >> + mutex_lock(&otg_list_mutex); > >> + otgd = usb_otg_device_get_otgd(otg_dev); > >> + if (!otgd) { > >> + dev_dbg(otg_dev, "otg: %s: device not registered to otg core\n", > >> + __func__); > >> + mutex_unlock(&otg_list_mutex); > >> + return -EINVAL; > >> + } > >> + > >> + mutex_unlock(&otg_list_mutex); > >> + /* HCD will be started by OTG fsm when needed */ > >> + mutex_lock(&otgd->fsm.lock); > >> + if (otgd->primary_hcd.hcd) { > >> + /* probably a shared HCD ? */ > >> + if (usb_otg_hcd_is_primary_hcd(hcd)) { > >> + dev_err(otg_dev, "otg: primary host already registered\n"); > >> + goto err; > >> + } > >> + > >> + if (hcd->shared_hcd == otgd->primary_hcd.hcd) { > >> + if (otgd->shared_hcd.hcd) { > >> + dev_err(otg_dev, "otg: shared host already registered\n"); > >> + goto err; > >> + } > >> + > >> + otgd->shared_hcd.hcd = hcd; > >> + otgd->shared_hcd.irqnum = irqnum; > >> + otgd->shared_hcd.irqflags = irqflags; > >> + otgd->shared_hcd.ops = ops; > >> + dev_info(otg_dev, "otg: shared host %s registered\n", > >> + dev_name(hcd->self.controller)); > >> + } else { > >> + dev_err(otg_dev, "otg: invalid shared host %s\n", > >> + dev_name(hcd->self.controller)); > >> + goto err; > >> + } > >> + } else { > >> + if (!usb_otg_hcd_is_primary_hcd(hcd)) { > >> + dev_err(otg_dev, "otg: primary host must be registered first\n"); > >> + goto err; > >> + } > >> + > >> + otgd->primary_hcd.hcd = hcd; > >> + otgd->primary_hcd.irqnum = irqnum; > >> + otgd->primary_hcd.irqflags = irqflags; > >> + otgd->primary_hcd.ops = ops; > >> + dev_info(otg_dev, "otg: primary host %s registered\n", > >> + dev_name(hcd->self.controller)); > >> + } > >> + > >> + /* > >> + * we're ready only if we have shared HCD > >> + * or we don't need shared HCD. > >> + */ > >> + if (otgd->shared_hcd.hcd || !otgd->primary_hcd.hcd->shared_hcd) { > >> + otgd->fsm.otg->host = hcd_to_bus(hcd); > >> + /* FIXME: set bus->otg_port if this is true OTG port with HNP */ > >> + > >> + /* start FSM */ > >> + usb_otg_start_fsm(&otgd->fsm); > > > > usb_otg_register_hcd() is called before usb_otg_add_hcd(), start fsm on > > this point can make sense since hcd has not been added? > > for OTG/DRD HCD case: > - usb_add_hcd() does not really ADD (or START) the HCD. It just registers with OTG core. > - FSM takes care of ADDing (or STARTing) the HCD when it wants using the > usb_otg_add_hcd() call. Understood. > - FSM does not need HCD to be already added. It just needs it to be registered. My point is only registering hcd to OTG core cannot be a valid *input* to make otg fsm state can be changed, so it's making no sense to call usb_otg_start_fsm(), but it's no harm. > It takes care of strting it when it wants to. > Any otg fsm state change(or start it to make its state change) need some otg fsm input or variables change happen. > > > >> + } else { > >> + dev_dbg(otg_dev, "otg: can't start till shared host registers\n"); > >> + } > >> + > >> + mutex_unlock(&otgd->fsm.lock); > >> + > >> + return 0; > >> + > >> +err: > >> + mutex_unlock(&otgd->fsm.lock); > >> + return -EINVAL; > >> +} > >> +EXPORT_SYMBOL_GPL(usb_otg_register_hcd); [...] > >> +#define TB_ASE0_BRST (155) /* minimum 155 ms, section:5.3.1 */ > >> +/* SE0 Time Before SRP */ > >> +#define TB_SE0_SRP (1000) /* b_idle,minimum 1s, section:5.1.2 */ > >> +/* SSEND time before SRP */ > >> +#define TB_SSEND_SRP (1500) /* minimum 1.5 sec, section:5.1.2 */ > >> + > >> +#define TB_SESS_VLD (1000) > >> + > > > > All otg timer timeout value should be in some *range* defined by otg spec, > > not some specific value, I don't think one specific value can meet all otg > > platforms, so we need find a way to make those value can be configured by > > controller drivers. > > OK. How about introducing 'struct usb_otg_config' which must be passed > to usb_otg_register(). > I think it's okay. > /* otg controller configuration */ > struct usb_otg_config { > /* OTG caps */ > struct usb_otg_caps otg_caps; You can use a pointer to avoid data copy. > > /* OTG Timer timeouts in ms. If 0, sane default will be used */ > int ta_wait_vrise; > ... > }; > > > > >> 2.1.4 > >> > > > > I assume I will have more comments after try more. > > Sure, no worries :) > > cheers, > -roger -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/