From patchwork Wed Dec 4 12:22:37 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 22015 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-qc0-f197.google.com (mail-qc0-f197.google.com [209.85.216.197]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id D71F5202E0 for ; Wed, 4 Dec 2013 12:23:14 +0000 (UTC) Received: by mail-qc0-f197.google.com with SMTP id r5sf6905400qcx.8 for ; Wed, 04 Dec 2013 04:23:14 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:delivered-to:from:to:cc:subject :date:message-id:x-original-sender:x-original-authentication-results :precedence:mailing-list:list-id:list-post:list-help:list-archive :list-unsubscribe; bh=lGg1sRIy5L3SmxWDvzHulcasDoB/9L5PIkTUSFZS/dE=; b=iVPixYcviZeTJdxmY/xjbkIulEKKt12OLTAfBJwm1EMiPv9fZF2/MEIUAuZbv+relJ sbvEk7DpnYeFzA8b3ZZAruClLqtCI29BNaH27/wATwuYWKqYnREOm2pqkbAYyin23QTC Bm1S2Wxagx5D/QJksVuN5S5PqFZv+akNmx2Ueyd9DbpcBnFkA8hyntHQmx/UPw8KgIVJ DaBELsUCWPLE+fo62rBTOMLePNV7zLYnRKmCUHY1ahC5+eXU9y9BnbeT12xsrIHdPOYZ UJEfCpjPah+/TZtz04miRsbbINFkedc5wYqK3F3xi+CAIE6A190E4pA4nXT5UaZdkVg9 7Atg== X-Gm-Message-State: ALoCoQngJXRm+XmlvvqqYPOc7Krsa9cuz+YWpgeuxRHs1R6cD9pEFWjXS0nrxooGdiVvDc4Kqs8X X-Received: by 10.236.51.170 with SMTP id b30mr12670404yhc.45.1386159794098; Wed, 04 Dec 2013 04:23:14 -0800 (PST) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.49.47.9 with SMTP id z9ls330242qem.79.gmail; Wed, 04 Dec 2013 04:23:13 -0800 (PST) X-Received: by 10.221.60.134 with SMTP id ws6mr495356vcb.44.1386159793951; Wed, 04 Dec 2013 04:23:13 -0800 (PST) Received: from mail-ve0-f171.google.com (mail-ve0-f171.google.com [209.85.128.171]) by mx.google.com with ESMTPS id gs2si18356902vdc.18.2013.12.04.04.23.13 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Wed, 04 Dec 2013 04:23:13 -0800 (PST) Received-SPF: neutral (google.com: 209.85.128.171 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) client-ip=209.85.128.171; Received: by mail-ve0-f171.google.com with SMTP id pa12so11909222veb.16 for ; Wed, 04 Dec 2013 04:23:13 -0800 (PST) X-Received: by 10.52.35.41 with SMTP id e9mr30642vdj.79.1386159793718; Wed, 04 Dec 2013 04:23:13 -0800 (PST) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patches@linaro.org Received: by 10.220.174.196 with SMTP id u4csp287786vcz; Wed, 4 Dec 2013 04:23:13 -0800 (PST) X-Received: by 10.112.131.103 with SMTP id ol7mr50898lbb.72.1386159792675; Wed, 04 Dec 2013 04:23:12 -0800 (PST) Received: from mail-lb0-f180.google.com (mail-lb0-f180.google.com [209.85.217.180]) by mx.google.com with ESMTPS id ax2si15364524lbc.165.2013.12.04.04.23.12 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Wed, 04 Dec 2013 04:23:12 -0800 (PST) Received-SPF: neutral (google.com: 209.85.217.180 is neither permitted nor denied by best guess record for domain of linus.walleij@linaro.org) client-ip=209.85.217.180; Received: by mail-lb0-f180.google.com with SMTP id w6so9079662lbh.25 for ; Wed, 04 Dec 2013 04:23:12 -0800 (PST) X-Received: by 10.152.180.228 with SMTP id dr4mr4921275lac.32.1386159792007; Wed, 04 Dec 2013 04:23:12 -0800 (PST) Received: from localhost.localdomain ([85.235.11.236]) by mx.google.com with ESMTPSA id e1sm31310576lbe.0.2013.12.04.04.23.09 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 04 Dec 2013 04:23:10 -0800 (PST) From: Linus Walleij To: linux-kernel@vger.kernel.org, Greg Kroah-Hartman Cc: linux-doc@vger.kernel.org, Linus Walleij , Rob Landley , Mark Brown , Arnd Bergmann , Grant Likely Subject: [PATCH] Documentation: start documenting driver design patterns Date: Wed, 4 Dec 2013 13:22:37 +0100 Message-Id: <1386159757-29966-1-git-send-email-linus.walleij@linaro.org> X-Mailer: git-send-email 1.8.3.1 X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: linus.walleij@linaro.org X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 209.85.128.171 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Precedence: list Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org List-ID: X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , After realizing that we tend to tell developers the same thing over and over, let's attempt to document some commin design patterns used in the device drivers. The idea is that this can be extended so I just start out with two well-known design patterns. Cc: Rob Landley Cc: Greg Kroah-Hartman Cc: Mark Brown Cc: Arnd Bergmann Cc: Grant Likely Signed-off-by: Linus Walleij --- I guess this goes to Greg for merging through the device core tree if it is liked, let's see. --- Documentation/driver-model/design-patterns.txt | 116 +++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 Documentation/driver-model/design-patterns.txt diff --git a/Documentation/driver-model/design-patterns.txt b/Documentation/driver-model/design-patterns.txt new file mode 100644 index 000000000000..9ef8c1684558 --- /dev/null +++ b/Documentation/driver-model/design-patterns.txt @@ -0,0 +1,116 @@ + +Device Driver Design Patterns +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +This document describes a few common design patterns found in device drivers. +It is likely that subsystem maintainers will ask driver developers to +conform to these design patterns. + +1. State Container +2. container_of() + + +1. State Container +~~~~~~~~~~~~~~~~~~ + +While the kernel contains a few device drivers that assume that they will +only be probed() once on a certain system (singletons), it is custom to assume +that the device the driver binds to will appear in several instances. This +means that the probe() function and all callbacks need to be reentrant. + +The most common way to achieve this is to use the state container design +pattern. It usually has this form: + +struct foo { + spinlock_t lock; /* Example member */ + (...) +}; + +static int foo_probe(...) +{ + struct foo *foo; + + foo = devm_kzalloc(dev, sizeof(*foo), GFP_KERNEL); + if (!foo) + return -ENOMEM; + spin_lock_init(&foo->lock); + (...) +} + +This will create an instance of struct foo in memory every time probe() is +called. This is our state container for this instance of the device driver. +Of course it is then necessary to always pass this instance of the +state around to all functions that need access to the state and its members. + +For example, if the driver is registering an interrupt handler, you would +pass around a pointer to struct foo like this: + +static irqreturn_t foo_handler(int irq, void *arg) +{ + struct foo *foo = arg; + (...) +} + +static int foo_probe(...) +{ + struct foo *foo; + + (...) + ret = request_irq(irq, foo_handler, 0, "foo", foo); +} + +This way you always get a pointer back to the correct instance of foo in +your interrupt handler. + + +2. container_of() +~~~~~~~~~~~~~~~~~ + +Continuing on the above example we add a offloaded work: + +struct foo { + spinlock_t lock; + struct workqueue_struct *wq; + struct work_struct offload; + (...) +}; + +static void foo_work(struct work_struct *work) +{ + struct foo *foo = container_of(work, struct foo, offload); + + (...) +} + +static irqreturn_t foo_handler(int irq, void *arg) +{ + struct foo *foo = arg; + + queue_work(foo->wq, &foo->offload); + (...) +} + +static int foo_probe(...) +{ + struct foo *foo; + + foo->wq = create_singlethread_workqueue("foo-wq"); + INIT_WORK(&foo->offload, foo_work); + (...) +} + +The design pattern is the same for a a hrtimer or something similar that will +return a single argument which is a pointer to a struct member in the +callback. + +container_of() is a macro defined in + +What container_of() does is to obtain a pointer to the containing struct from +a pointer to a member by a simple subtraction using the offsetof() macro from +standard C, which allows something similar to object oriented behaviours. +Notice that the contained member must not be a pointer, but an actual member +for this to work. + +We can see here that we avoid having global pointers to our struct foo * +instance this way, while still keeping the number of parameters passed to the +work function to a single pointer.