€•ì0Œsphinx.addnodes”Œdocument”“”)”}”(Œ rawsource”Œ”Œchildren”]”(Œ translations”Œ LanguagesNode”“”)”}”(hhh]”(hŒ pending_xref”“”)”}”(hhh]”Œdocutils.nodes”ŒText”“”ŒChinese (Simplified)”…””}”Œparent”hsbaŒ attributes”}”(Œids”]”Œclasses”]”Œnames”]”Œdupnames”]”Œbackrefs”]”Œ refdomain”Œstd”Œreftype”Œdoc”Œ reftarget”Œ;/translations/zh_CN/driver-api/driver-model/design-patterns”Œmodname”NŒ classname”NŒ refexplicit”ˆuŒtagname”hhh ubh)”}”(hhh]”hŒChinese (Traditional)”…””}”hh2sbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ;/translations/zh_TW/driver-api/driver-model/design-patterns”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubh)”}”(hhh]”hŒItalian”…””}”hhFsbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ;/translations/it_IT/driver-api/driver-model/design-patterns”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubh)”}”(hhh]”hŒJapanese”…””}”hhZsbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ;/translations/ja_JP/driver-api/driver-model/design-patterns”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubh)”}”(hhh]”hŒKorean”…””}”hhnsbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ;/translations/ko_KR/driver-api/driver-model/design-patterns”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubh)”}”(hhh]”hŒPortuguese (Brazilian)”…””}”hh‚sbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ;/translations/pt_BR/driver-api/driver-model/design-patterns”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubh)”}”(hhh]”hŒSpanish”…””}”hh–sbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ;/translations/sp_SP/driver-api/driver-model/design-patterns”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubeh}”(h]”h ]”h"]”h$]”h&]”Œcurrent_language”ŒEnglish”uh1h hhŒ _document”hŒsource”NŒline”NubhŒsection”“”)”}”(hhh]”(hŒtitle”“”)”}”(hŒDevice Driver Design Patterns”h]”hŒDevice Driver Design Patterns”…””}”(hh¼h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hºhh·h²hh³ŒU/var/lib/git/docbuild/linux/Documentation/driver-api/driver-model/design-patterns.rst”h´KubhŒ paragraph”“”)”}”(hŒµ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.”h]”hŒµ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.”…””}”(hhÍh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´Khh·h²hubhŒenumerated_list”“”)”}”(hhh]”(hŒ list_item”“”)”}”(hŒState Container”h]”hÌ)”}”(hhäh]”hŒState Container”…””}”(hhæh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´K hhâubah}”(h]”h ]”h"]”h$]”h&]”uh1hàhhÝh²hh³hÊh´Nubhá)”}”(hŒcontainer_of() ”h]”hÌ)”}”(hŒcontainer_of()”h]”hŒcontainer_of()”…””}”(hhýh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´K hhùubah}”(h]”h ]”h"]”h$]”h&]”uh1hàhhÝh²hh³hÊh´Nubeh}”(h]”h ]”h"]”h$]”h&]”Œenumtype”Œarabic”Œprefix”hŒsuffix”Œ.”uh1hÛhh·h²hh³hÊh´K ubh¶)”}”(hhh]”(h»)”}”(hŒ1. State Container”h]”hŒ1. State Container”…””}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hºhjh²hh³hÊh´KubhÌ)”}”(hX+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.”h]”hX+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.”…””}”(hj-h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´Khjh²hubhÌ)”}”(hŒlThe most common way to achieve this is to use the state container design pattern. It usually has this form::”h]”hŒkThe most common way to achieve this is to use the state container design pattern. It usually has this form:”…””}”(hj;h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´Khjh²hubhŒ literal_block”“”)”}”(hŒÿ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); (...) }”h]”hŒÿ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); (...) }”…””}”hjKsbah}”(h]”h ]”h"]”h$]”h&]”Œ xml:space”Œpreserve”uh1jIh³hÊh´Khjh²hubhÌ)”}”(hX&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.”h]”hX&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.”…””}”(hj[h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´K(hjh²hubhÌ)”}”(hŒyFor example, if the driver is registering an interrupt handler, you would pass around a pointer to struct foo like this::”h]”hŒxFor example, if the driver is registering an interrupt handler, you would pass around a pointer to struct foo like this:”…””}”(hjih²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´K-hjh²hubjJ)”}”(hŒÒ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); }”h]”hŒÒ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); }”…””}”hjwsbah}”(h]”h ]”h"]”h$]”h&]”jYjZuh1jIh³hÊh´K0hjh²hubhÌ)”}”(hŒ`This way you always get a pointer back to the correct instance of foo in your interrupt handler.”h]”hŒ`This way you always get a pointer back to the correct instance of foo in your interrupt handler.”…””}”(hj…h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´K>hjh²hubeh}”(h]”Œstate-container”ah ]”h"]”Œ1. state container”ah$]”h&]”uh1hµhh·h²hh³hÊh´Kubh¶)”}”(hhh]”(h»)”}”(hŒ2. container_of()”h]”hŒ2. container_of()”…””}”(hjžh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hºhj›h²hh³hÊh´KCubhÌ)”}”(hŒ:Continuing on the above example we add an offloaded work::”h]”hŒ9Continuing on the above example we add an offloaded work:”…””}”(hj¬h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´KEhj›h²hubjJ)”}”(hXstruct 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); (...) }”h]”hXstruct 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); (...) }”…””}”hjºsbah}”(h]”h ]”h"]”h$]”h&]”jYjZuh1jIh³hÊh´KGhj›h²hubhÌ)”}”(hŒœThe design pattern is the same for an hrtimer or something similar that will return a single argument which is a pointer to a struct member in the callback.”h]”hŒœThe design pattern is the same for an hrtimer or something similar that will return a single argument which is a pointer to a struct member in the callback.”…””}”(hjÈh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´Kfhj›h²hubhÌ)”}”(hŒ;container_of() is a macro defined in ”h]”hŒ;container_of() is a macro defined in ”…””}”(hjÖh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´Kjhj›h²hubhÌ)”}”(hXDWhat 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.”h]”hXDWhat 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.”…””}”(hjäh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´Klhj›h²hubhÌ)”}”(hŒ¹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.”h]”hŒ¹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.”…””}”(hjòh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´Krhj›h²hubeh}”(h]”Œ container-of”ah ]”h"]”Œ2. container_of()”ah$]”h&]”uh1hµhh·h²hh³hÊh´KCubeh}”(h]”Œdevice-driver-design-patterns”ah ]”h"]”Œdevice driver design patterns”ah$]”h&]”uh1hµhhh²hh³hÊh´Kubeh}”(h]”h ]”h"]”h$]”h&]”Œsource”hÊuh1hŒcurrent_source”NŒ current_line”NŒsettings”Œdocutils.frontend”ŒValues”“”)”}”(hºNŒ generator”NŒ datestamp”NŒ source_link”NŒ source_url”NŒ toc_backlinks”Œentry”Œfootnote_backlinks”KŒ sectnum_xform”KŒstrip_comments”NŒstrip_elements_with_classes”NŒ strip_classes”NŒ report_level”KŒ halt_level”KŒexit_status_level”KŒdebug”NŒwarning_stream”NŒ traceback”ˆŒinput_encoding”Œ utf-8-sig”Œinput_encoding_error_handler”Œstrict”Œoutput_encoding”Œutf-8”Œoutput_encoding_error_handler”j3Œerror_encoding”Œutf-8”Œerror_encoding_error_handler”Œbackslashreplace”Œ language_code”Œen”Œrecord_dependencies”NŒconfig”NŒ id_prefix”hŒauto_id_prefix”Œid”Œ dump_settings”NŒdump_internals”NŒdump_transforms”NŒdump_pseudo_xml”NŒexpose_internals”NŒstrict_visitor”NŒ_disable_config”NŒ_source”hÊŒ _destination”NŒ _config_files”]”Œ7/var/lib/git/docbuild/linux/Documentation/docutils.conf”aŒfile_insertion_enabled”ˆŒ raw_enabled”KŒline_length_limit”M'Œpep_references”NŒ pep_base_url”Œhttps://peps.python.org/”Œpep_file_url_template”Œpep-%04d”Œrfc_references”NŒ rfc_base_url”Œ&https://datatracker.ietf.org/doc/html/”Œ tab_width”KŒtrim_footnote_reference_space”‰Œsyntax_highlight”Œlong”Œ smart_quotes”ˆŒsmartquotes_locales”]”Œcharacter_level_inline_markup”‰Œdoctitle_xform”‰Œ docinfo_xform”KŒsectsubtitle_xform”‰Œ image_loading”Œlink”Œembed_stylesheet”‰Œcloak_email_addresses”ˆŒsection_self_link”‰Œenv”NubŒreporter”NŒindirect_targets”]”Œsubstitution_defs”}”Œsubstitution_names”}”Œrefnames”}”Œrefids”}”Œnameids”}”(j j j˜j•jjuŒ nametypes”}”(j ‰j˜‰j‰uh}”(j h·j•jjj›uŒ footnote_refs”}”Œ citation_refs”}”Œ autofootnotes”]”Œautofootnote_refs”]”Œsymbol_footnotes”]”Œsymbol_footnote_refs”]”Œ footnotes”]”Œ citations”]”Œautofootnote_start”KŒsymbol_footnote_start”KŒ id_counter”Œ collections”ŒCounter”“”}”…”R”Œparse_messages”]”Œtransform_messages”]”Œ transformer”NŒ include_log”]”Œ decoration”Nh²hub.