€•ù/Œ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Œ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”…””}”(hj hž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.”…””}”(hjhž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); (...) }”…””}”hj7sbah}”(h]”h ]”h"]”h$]”h&]”Œ xml:space”Œpreserve”uh1j5hŸ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.”…””}”(hjGhž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:”…””}”(hjUhžhhŸNh Nubah}”(h]”h ]”h"]”h$]”h&]”uh1h·hŸh¶h K-hjhžhubj6)”}”(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); }”…””}”hjcsbah}”(h]”h ]”h"]”h$]”h&]”jEjFuh1j5hŸ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.”…””}”(hjqhž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žhubj6)”}”(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&]”jEjFuh1j5hŸ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Œ5container_of() is a macro defined in ”h]”hŒ5container_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”jŒ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„jjñjîuŒ nametypes”}”(jù‰j„‰jñ‰uh}”(jöh£jjjîj‡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.