← Documents Documentation/usb/gadget_configfs.rst GitHub 원문 ↗

Linux 6.18.37 · USB

Configfs로 구성하는 Linux USB gadget

Configfs에서 gadget·configuration·function·string을 만들고 UDC에 bind하는 절차와 내부 config_item/attribute 설계입니다.

Source pathDocumentation/usb/gadget_configfs.rst
Source versionLinux v6.18.37
TranslationDUJINLABS 전문 번역 + 해설

요약·해설과 원문, 전문 번역을 서로 분리했습니다. API 이름, symbol, source path는 원문 표기를 사용합니다.

1. 요약·해설

원문의 핵심 논리와 kernel programming 관점의 보충 설명입니다. 아래의 전문 번역과는 별도로 작성했습니다.

요약·해설

gadget_configfs.rst:1-399

Configfs gadget 구성은 gadget root에서 ID/string을 설정하고 configuration과 function instance를 만든 뒤 symlink로 연결하고 마지막에 `UDC`를 기록하는 순서입니다. 삭제는 반드시 bind 해제 후 역순으로 진행합니다. 내부적으로 generic `config_item`/`configfs_attribute`를 용도별 structure에 embedded하고 `container_of`로 복원해 show/store callback과 USB function bind를 연결합니다.

2. 영어 원문 전체

번역 기준이 된 Linux v6.18.37 원문입니다. 줄 번호는 이 버전의 파일 좌표입니다.

원문 전체 펼치기
1 ============================================
2 Linux USB gadget configured through configfs
3 ============================================
4
5
6 25th April 2013
7
8
9
10
11 Overview
12 ========
13
14 A USB Linux Gadget is a device which has a UDC (USB Device Controller) and can
15 be connected to a USB Host to extend it with additional functions like a serial
16 port or a mass storage capability.
17
18 A gadget is seen by its host as a set of configurations, each of which contains
19 a number of interfaces which, from the gadget's perspective, are known as
20 functions, each function representing e.g. a serial connection or a SCSI disk.
21
22 Linux provides a number of functions for gadgets to use.
23
24 Creating a gadget means deciding what configurations there will be
25 and which functions each configuration will provide.
26
27 Configfs (please see `Documentation/filesystems/configfs.rst`) lends itself nicely
28 for the purpose of telling the kernel about the above mentioned decision.
29 This document is about how to do it.
30
31 It also describes how configfs integration into gadget is designed.
32
33
34
35
36 Requirements
37 ============
38
39 In order for this to work configfs must be available, so CONFIGFS_FS must be
40 'y' or 'm' in .config. As of this writing USB_LIBCOMPOSITE selects CONFIGFS_FS.
41
42
43
44
45 Usage
46 =====
47
48 (The original post describing the first function
49 made available through configfs can be seen here:
50 http://www.spinics.net/lists/linux-usb/msg76388.html)
51
52 ::
53
54 $ modprobe libcomposite
55 $ mount none $CONFIGFS_HOME -t configfs
56
57 where CONFIGFS_HOME is the mount point for configfs
58
59 1. Creating the gadgets
60 -----------------------
61
62 For each gadget to be created its corresponding directory must be created::
63
64 $ mkdir $CONFIGFS_HOME/usb_gadget/<gadget name>
65
66 e.g.::
67
68 $ mkdir $CONFIGFS_HOME/usb_gadget/g1
69
70 ...
71 ...
72 ...
73
74 $ cd $CONFIGFS_HOME/usb_gadget/g1
75
76 Each gadget needs to have its vendor id <VID> and product id <PID> specified::
77
78 $ echo <VID> > idVendor
79 $ echo <PID> > idProduct
80
81 A gadget also needs its serial number, manufacturer and product strings.
82 In order to have a place to store them, a strings subdirectory must be created
83 for each language, e.g.::
84
85 $ mkdir strings/0x409
86
87 Then the strings can be specified::
88
89 $ echo <serial number> > strings/0x409/serialnumber
90 $ echo <manufacturer> > strings/0x409/manufacturer
91 $ echo <product> > strings/0x409/product
92
93 Further custom string descriptors can be created as directories within the
94 language's directory, with the string text being written to the "s" attribute
95 within the string's directory::
96
97 $ mkdir strings/0x409/xu.0
98 $ echo <string text> > strings/0x409/xu.0/s
99
100 Where function drivers support it, functions may allow symlinks to these custom
101 string descriptors to associate those strings with class descriptors.
102
103 2. Creating the configurations
104 ------------------------------
105
106 Each gadget will consist of a number of configurations, their corresponding
107 directories must be created::
108
109 $ mkdir configs/<name>.<number>
110
111 where <name> can be any string which is legal in a filesystem and the
112 <number> is the configuration's number, e.g.::
113
114 $ mkdir configs/c.1
115
116 ...
117 ...
118 ...
119
120 Each configuration also needs its strings, so a subdirectory must be created
121 for each language, e.g.::
122
123 $ mkdir configs/c.1/strings/0x409
124
125 Then the configuration string can be specified::
126
127 $ echo <configuration> > configs/c.1/strings/0x409/configuration
128
129 Some attributes can also be set for a configuration, e.g.::
130
131 $ echo 120 > configs/c.1/MaxPower
132
133 3. Creating the functions
134 -------------------------
135
136 The gadget will provide some functions, for each function its corresponding
137 directory must be created::
138
139 $ mkdir functions/<name>.<instance name>
140
141 where <name> corresponds to one of allowed function names and instance name
142 is an arbitrary string allowed in a filesystem, e.g.::
143
144 $ mkdir functions/ncm.usb0 # usb_f_ncm.ko gets loaded with request_module()
145
146 ...
147 ...
148 ...
149
150 Each function provides its specific set of attributes, with either read-only
151 or read-write access. Where applicable they need to be written to as
152 appropriate.
153 Please refer to Documentation/ABI/testing/configfs-usb-gadget for more information.
154
155 4. Associating the functions with their configurations
156 ------------------------------------------------------
157
158 At this moment a number of gadgets is created, each of which has a number of
159 configurations specified and a number of functions available. What remains
160 is specifying which function is available in which configuration (the same
161 function can be used in multiple configurations). This is achieved with
162 creating symbolic links::
163
164 $ ln -s functions/<name>.<instance name> configs/<name>.<number>
165
166 e.g.::
167
168 $ ln -s functions/ncm.usb0 configs/c.1
169
170 ...
171 ...
172 ...
173
174 5. Enabling the gadget
175 ----------------------
176
177 All the above steps serve the purpose of composing the gadget of
178 configurations and functions.
179
180 An example directory structure might look like this::
181
182 .
183 ./strings
184 ./strings/0x409
185 ./strings/0x409/serialnumber
186 ./strings/0x409/product
187 ./strings/0x409/manufacturer
188 ./configs
189 ./configs/c.1
190 ./configs/c.1/ncm.usb0 -> ../../../../usb_gadget/g1/functions/ncm.usb0
191 ./configs/c.1/strings
192 ./configs/c.1/strings/0x409
193 ./configs/c.1/strings/0x409/configuration
194 ./configs/c.1/bmAttributes
195 ./configs/c.1/MaxPower
196 ./functions
197 ./functions/ncm.usb0
198 ./functions/ncm.usb0/ifname
199 ./functions/ncm.usb0/qmult
200 ./functions/ncm.usb0/host_addr
201 ./functions/ncm.usb0/dev_addr
202 ./UDC
203 ./bcdUSB
204 ./bcdDevice
205 ./idProduct
206 ./idVendor
207 ./bMaxPacketSize0
208 ./bDeviceProtocol
209 ./bDeviceSubClass
210 ./bDeviceClass
211
212
213 Such a gadget must be finally enabled so that the USB host can enumerate it.
214
215 In order to enable the gadget it must be bound to a UDC (USB Device
216 Controller)::
217
218 $ echo <udc name> > UDC
219
220 where <udc name> is one of those found in /sys/class/udc/*
221 e.g.::
222
223 $ echo s3c-hsotg > UDC
224
225
226 6. Disabling the gadget
227 -----------------------
228
229 ::
230
231 $ echo "" > UDC
232
233 7. Cleaning up
234 --------------
235
236 Remove functions from configurations::
237
238 $ rm configs/<config name>.<number>/<function>
239
240 where <config name>.<number> specify the configuration and <function> is
241 a symlink to a function being removed from the configuration, e.g.::
242
243 $ rm configs/c.1/ncm.usb0
244
245 ...
246 ...
247 ...
248
249 Remove strings directories in configurations::
250
251 $ rmdir configs/<config name>.<number>/strings/<lang>
252
253 e.g.::
254
255 $ rmdir configs/c.1/strings/0x409
256
257 ...
258 ...
259 ...
260
261 and remove the configurations::
262
263 $ rmdir configs/<config name>.<number>
264
265 e.g.::
266
267 rmdir configs/c.1
268
269 ...
270 ...
271 ...
272
273 Remove functions (function modules are not unloaded, though)::
274
275 $ rmdir functions/<name>.<instance name>
276
277 e.g.::
278
279 $ rmdir functions/ncm.usb0
280
281 ...
282 ...
283 ...
284
285 Remove strings directories in the gadget::
286
287 $ rmdir strings/<lang>
288
289 e.g.::
290
291 $ rmdir strings/0x409
292
293 and finally remove the gadget::
294
295 $ cd ..
296 $ rmdir <gadget name>
297
298 e.g.::
299
300 $ rmdir g1
301
302
303
304
305 Implementation design
306 =====================
307
308 Below the idea of how configfs works is presented.
309 In configfs there are items and groups, both represented as directories.
310 The difference between an item and a group is that a group can contain
311 other groups. In the picture below only an item is shown.
312 Both items and groups can have attributes, which are represented as files.
313 The user can create and remove directories, but cannot remove files,
314 which can be read-only or read-write, depending on what they represent.
315
316 The filesystem part of configfs operates on config_items/groups and
317 configfs_attributes which are generic and of the same type for all
318 configured elements. However, they are embedded in usage-specific
319 larger structures. In the picture below there is a "cs" which contains
320 a config_item and an "sa" which contains a configfs_attribute.
321
322 The filesystem view would be like this::
323
324 ./
325 ./cs (directory)
326 |
327 +--sa (file)
328 |
329 .
330 .
331 .
332
333 Whenever a user reads/writes the "sa" file, a function is called
334 which accepts a struct config_item and a struct configfs_attribute.
335 In the said function the "cs" and "sa" are retrieved using the well
336 known container_of technique and an appropriate sa's function (show or
337 store) is called and passed the "cs" and a character buffer. The "show"
338 is for displaying the file's contents (copy data from the cs to the
339 buffer), while the "store" is for modifying the file's contents (copy data
340 from the buffer to the cs), but it is up to the implementer of the
341 two functions to decide what they actually do.
342
343 ::
344
345 typedef struct configured_structure cs;
346 typedef struct specific_attribute sa;
347
348 sa
349 +----------------------------------+
350 cs | (*show)(cs *, buffer); |
351 +-----------------+ | (*store)(cs *, buffer, length); |
352 | | | |
353 | +-------------+ | | +------------------+ |
354 | | struct |-|----|------>|struct | |
355 | | config_item | | | |configfs_attribute| |
356 | +-------------+ | | +------------------+ |
357 | | +----------------------------------+
358 | data to be set | .
359 | | .
360 +-----------------+ .
361
362 The file names are decided by the config item/group designer, while
363 the directories in general can be named at will. A group can have
364 a number of its default sub-groups created automatically.
365
366 For more information on configfs please see
367 `Documentation/filesystems/configfs.rst`.
368
369 The concepts described above translate to USB gadgets like this:
370
371 1. A gadget has its config group, which has some attributes (idVendor,
372 idProduct etc) and default sub-groups (configs, functions, strings).
373 Writing to the attributes causes the information to be stored in appropriate
374 locations. In the configs, functions and strings sub-groups a user can
375 create their sub-groups to represent configurations, functions, and groups
376 of strings in a given language.
377
378 2. The user creates configurations and functions, in the configurations
379 creates symbolic links to functions. This information is used when the
380 gadget's UDC attribute is written to, which means binding the gadget to the
381 UDC. The code in drivers/usb/gadget/configfs.c iterates over all
382 configurations, and in each configuration it iterates over all functions and
383 binds them. This way the whole gadget is bound.
384
385 3. The file drivers/usb/gadget/configfs.c contains code for
386
387 - gadget's config_group
388 - gadget's default groups (configs, functions, strings)
389 - associating functions with configurations (symlinks)
390
391 4. Each USB function naturally has its own view of what it wants configured, so
392 config_groups for particular functions are defined in the functions
393 implementation files drivers/usb/gadget/f_*.c.
394
395 5. Function's code is written in such a way that it uses
396 usb_get_function_instance(), which, in turn, calls request_module. So,
397 provided that modprobe works, modules for particular functions are loaded
398 automatically. Please note that the converse is not true: after a gadget is
399 disabled and torn down, the modules remain loaded.
400

3. 한국어 전문 번역

영어 원문의 문단 순서와 의미를 유지한 전체 번역입니다. 코드, 함수명, symbol과 URL은 원문 표기를 유지합니다.

제목과 작성일

1-10

Configfs를 통해 Linux USB gadget을 구성하는 문서이며 기록된 날짜는 2013년 4월 25일입니다.

============================================
Linux USB gadget configured through configfs
============================================


25th April 2013



Gadget, configuration, function

11-33

USB Linux Gadget은 UDC(USB Device Controller)를 갖고 USB host에 연결되어 serial port나 mass storage 같은 기능을 추가하는 device입니다.

Host는 gadget을 configuration 집합으로 봅니다. 각 configuration은 여러 interface를 포함하며 gadget 관점에서는 이를 function이라 부릅니다. Function 하나는 serial 연결이나 SCSI disk 같은 기능을 나타냅니다.

Linux는 gadget이 사용할 여러 function을 제공합니다. Gadget을 만든다는 것은 configuration의 종류와 각 configuration이 제공할 function을 결정하는 일입니다.

Configfs는 이 결정을 kernel에 전달하는 데 적합합니다. 문서는 사용 방법과 gadget의 configfs integration 설계를 함께 설명합니다.

USB gadget 구성 계층
USB gadget과 UDC 준비하나 이상의 configuration 생성Serial, mass storage 등 function instance 생성Function을 configuration에 연결UDC에 bind해 host가 enumerate

Host가 보는 configuration/interface와 gadget이 구성하는 function 관계입니다.

Overview
========

A USB Linux Gadget is a device which has a UDC (USB Device Controller) and can
be connected to a USB Host to extend it with additional functions like a serial
port or a mass storage capability.

A gadget is seen by its host as a set of configurations, each of which contains
a number of interfaces which, from the gadget's perspective, are known as
functions, each function representing e.g. a serial connection or a SCSI disk.

Linux provides a number of functions for gadgets to use.

Creating a gadget means deciding what configurations there will be
and which functions each configuration will provide.

Configfs (please see `Documentation/filesystems/configfs.rst`) lends itself nicely
for the purpose of telling the kernel about the above mentioned decision.
This document is about how to do it.

It also describes how configfs integration into gadget is designed.

Configfs 요구 사항

34-44

Configfs가 사용 가능해야 하므로 `.config`의 `CONFIGFS_FS`가 `y` 또는 `m`이어야 합니다. 문서 작성 당시 `USB_LIBCOMPOSITE`가 `CONFIGFS_FS`를 select합니다.



Requirements
============

In order for this to work configfs must be available, so CONFIGFS_FS must be
'y' or 'm' in .config. As of this writing USB_LIBCOMPOSITE selects CONFIGFS_FS.



libcomposite와 configfs mount

45-58

먼저 `modprobe libcomposite`로 module을 load하고 `mount none $CONFIGFS_HOME -t configfs`로 configfs를 mount합니다. `$CONFIGFS_HOME`은 configfs mount point입니다.

Configfs에 처음 공개된 function의 원래 소개 글 URL도 원문에 보존되어 있습니다.

Usage
=====

(The original post describing the first function
made available through configfs can be seen here:
http://www.spinics.net/lists/linux-usb/msg76388.html)

::

	$ modprobe libcomposite
	$ mount none $CONFIGFS_HOME -t configfs

where CONFIGFS_HOME is the mount point for configfs

Gadget directory, ID, string

59-102

Gadget마다 `$CONFIGFS_HOME/usb_gadget/<gadget name>` directory를 만듭니다. 예제 이름은 `g1`이며 이후 그 directory로 이동합니다.

각 gadget의 `idVendor`와 `idProduct`에 VID와 PID를 기록합니다.

Serial number, manufacturer, product string을 저장하려면 language마다 `strings/<lang>` subdirectory를 만듭니다. 예제 language ID `0x409` 아래의 `serialnumber`, `manufacturer`, `product` attribute에 값을 씁니다.

추가 custom string descriptor는 language directory 안에 별도 directory로 만들고 그 안의 `s` attribute에 text를 기록합니다. Function driver가 지원하면 이 custom string으로 symlink를 만들어 class descriptor와 연결할 수 있습니다.

Gadget 기본 attribute와 string
경로내용
idVendorUSB vendor ID
idProductUSB product ID
strings/0x409/serialnumberSerial number
strings/0x409/manufacturerManufacturer string
strings/0x409/productProduct string
strings/0x409/xu.0/sCustom string text

g1 root와 strings/0x409에서 설정하는 항목입니다.

1. Creating the gadgets
-----------------------

For each gadget to be created its corresponding directory must be created::

	$ mkdir $CONFIGFS_HOME/usb_gadget/<gadget name>

e.g.::

	$ mkdir $CONFIGFS_HOME/usb_gadget/g1

	...
	...
	...

	$ cd $CONFIGFS_HOME/usb_gadget/g1

Each gadget needs to have its vendor id <VID> and product id <PID> specified::

	$ echo <VID> > idVendor
	$ echo <PID> > idProduct

A gadget also needs its serial number, manufacturer and product strings.
In order to have a place to store them, a strings subdirectory must be created
for each language, e.g.::

	$ mkdir strings/0x409

Then the strings can be specified::

	$ echo <serial number> > strings/0x409/serialnumber
	$ echo <manufacturer> > strings/0x409/manufacturer
	$ echo <product> > strings/0x409/product

Further custom string descriptors can be created as directories within the
language's directory, with the string text being written to the "s" attribute
within the string's directory::

	$ mkdir strings/0x409/xu.0
	$ echo <string text> > strings/0x409/xu.0/s

Where function drivers support it, functions may allow symlinks to these custom
string descriptors to associate those strings with class descriptors.

Configuration 생성과 attribute

103-132

각 configuration에 `configs/<name>.<number>` directory를 만듭니다. `name`은 filesystem에서 유효한 임의 string이고 `number`는 configuration number이며 예제는 `configs/c.1`입니다.

Configuration도 language별 string directory가 필요합니다. `configs/c.1/strings/0x409/configuration`에 configuration string을 기록합니다.

`configs/c.1/MaxPower` 같은 configuration attribute도 설정할 수 있으며 예제 값은 120입니다.

2. Creating the configurations
------------------------------

Each gadget will consist of a number of configurations, their corresponding
directories must be created::

        $ mkdir configs/<name>.<number>

where <name> can be any string which is legal in a filesystem and the
<number> is the configuration's number, e.g.::

	$ mkdir configs/c.1

	...
	...
	...

Each configuration also needs its strings, so a subdirectory must be created
for each language, e.g.::

	$ mkdir configs/c.1/strings/0x409

Then the configuration string can be specified::

	$ echo <configuration> > configs/c.1/strings/0x409/configuration

Some attributes can also be set for a configuration, e.g.::

	$ echo 120 > configs/c.1/MaxPower

Function instance 생성

133-154

Gadget이 제공할 각 function마다 `functions/<name>.<instance name>` directory를 만듭니다. `name`은 허용된 function 이름이고 instance name은 filesystem에서 유효한 임의 string입니다.

예제 `functions/ncm.usb0`를 만들면 `request_module()`을 통해 `usb_f_ncm.ko`가 load됩니다.

각 function은 read-only 또는 read-write인 고유 attribute 집합을 제공합니다. 자세한 내용은 `Documentation/ABI/testing/configfs-usb-gadget`을 참고합니다.

3. Creating the functions
-------------------------

The gadget will provide some functions, for each function its corresponding
directory must be created::

	$ mkdir functions/<name>.<instance name>

where <name> corresponds to one of allowed function names and instance name
is an arbitrary string allowed in a filesystem, e.g.::

  $ mkdir functions/ncm.usb0 # usb_f_ncm.ko gets loaded with request_module()

  ...
  ...
  ...

Each function provides its specific set of attributes, with either read-only
or read-write access. Where applicable they need to be written to as
appropriate.
Please refer to Documentation/ABI/testing/configfs-usb-gadget for more information.

Function을 configuration에 연결

155-173

생성한 function을 어느 configuration에서 사용할지 symbolic link로 지정합니다. 같은 function을 여러 configuration에서 사용할 수도 있습니다.

`ln -s functions/<name>.<instance name> configs/<name>.<number>` 형식을 사용하며 예제는 `functions/ncm.usb0`를 `configs/c.1`에 연결합니다.

4. Associating the functions with their configurations
------------------------------------------------------

At this moment a number of gadgets is created, each of which has a number of
configurations specified and a number of functions available. What remains
is specifying which function is available in which configuration (the same
function can be used in multiple configurations). This is achieved with
creating symbolic links::

	$ ln -s functions/<name>.<instance name> configs/<name>.<number>

e.g.::

	$ ln -s functions/ncm.usb0 configs/c.1

	...
	...
	...

Directory 구조와 UDC bind

174-225

앞 단계로 gadget의 configuration과 function 구성이 완성됩니다. 원문 directory tree는 gadget string, configuration, NCM function, device descriptor attribute, `UDC` file의 관계를 보여 줍니다.

Host가 enumerate하려면 gadget을 UDC에 bind해야 합니다. `/sys/class/udc/*`에서 controller 이름을 찾아 `UDC` attribute에 씁니다. 예제는 `echo s3c-hsotg > UDC`입니다.

Configfs gadget directory 구조
경로역할
strings/0x409/*Gadget serial/manufacturer/product string
configs/c.1/Configuration attribute와 language string
configs/c.1/ncm.usb0Function instance로 향하는 symlink
functions/ncm.usb0/*NCM function 고유 attribute
idVendor, idProduct, bcdUSB, ...Device descriptor attribute
UDCBind할 USB Device Controller 이름

원문의 directory tree를 역할별로 정리한 구조화 도식입니다.

Gadget 활성화 절차
Gadget ID와 string 설정Configuration과 function instance 생성Symlink로 function을 configuration에 연결/sys/class/udc에서 UDC 이름 확인 후 UDC attribute에 기록Controller bind와 host enumeration

Configfs object 생성부터 host enumeration까지의 순서입니다.

5. Enabling the gadget
----------------------

All the above steps serve the purpose of composing the gadget of
configurations and functions.

An example directory structure might look like this::

  .
  ./strings
  ./strings/0x409
  ./strings/0x409/serialnumber
  ./strings/0x409/product
  ./strings/0x409/manufacturer
  ./configs
  ./configs/c.1
  ./configs/c.1/ncm.usb0 -> ../../../../usb_gadget/g1/functions/ncm.usb0
  ./configs/c.1/strings
  ./configs/c.1/strings/0x409
  ./configs/c.1/strings/0x409/configuration
  ./configs/c.1/bmAttributes
  ./configs/c.1/MaxPower
  ./functions
  ./functions/ncm.usb0
  ./functions/ncm.usb0/ifname
  ./functions/ncm.usb0/qmult
  ./functions/ncm.usb0/host_addr
  ./functions/ncm.usb0/dev_addr
  ./UDC
  ./bcdUSB
  ./bcdDevice
  ./idProduct
  ./idVendor
  ./bMaxPacketSize0
  ./bDeviceProtocol
  ./bDeviceSubClass
  ./bDeviceClass


Such a gadget must be finally enabled so that the USB host can enumerate it.

In order to enable the gadget it must be bound to a UDC (USB Device
Controller)::

	$ echo <udc name> > UDC

where <udc name> is one of those found in /sys/class/udc/*
e.g.::

	$ echo s3c-hsotg > UDC

Gadget 비활성화

226-232

Gadget을 비활성화하려면 빈 string을 `UDC`에 써서 controller binding을 해제합니다: `echo "" > UDC`.

6. Disabling the gadget
-----------------------

::

	$ echo "" > UDC

Configuration cleanup

233-272

Cleanup은 의존 관계의 역순으로 수행합니다. 먼저 configuration 안의 function symlink를 제거합니다.

그다음 configuration의 language string directory를 제거하고 마지막으로 configuration directory 자체를 제거합니다. 예제에서는 `configs/c.1/ncm.usb0`, `configs/c.1/strings/0x409`, `configs/c.1` 순서입니다.

Configfs gadget cleanup 순서
UDC에 빈 string을 써서 gadget disableConfiguration의 function symlink 제거Configuration language string directory 제거Configuration과 function instance directory 제거Gadget string directory와 gadget root 제거

Bind 해제 뒤 하위 object부터 역순으로 제거합니다.

7. Cleaning up
--------------

Remove functions from configurations::

	$ rm configs/<config name>.<number>/<function>

where <config name>.<number> specify the configuration and <function> is
a symlink to a function being removed from the configuration, e.g.::

	$ rm configs/c.1/ncm.usb0

	...
	...
	...

Remove strings directories in configurations::

	$ rmdir configs/<config name>.<number>/strings/<lang>

e.g.::

	$ rmdir configs/c.1/strings/0x409

	...
	...
	...

and remove the configurations::

	$ rmdir configs/<config name>.<number>

e.g.::

	rmdir configs/c.1

	...
	...
	...

Function, string, gadget 제거

273-304

Function instance directory를 제거해도 function module은 unload되지 않습니다. 예제는 `rmdir functions/ncm.usb0`입니다.

Gadget의 language string directory를 제거한 뒤 상위 directory로 이동해 gadget root `g1`을 제거합니다.

Remove functions (function modules are not unloaded, though)::

	$ rmdir functions/<name>.<instance name>

e.g.::

	$ rmdir functions/ncm.usb0

	...
	...
	...

Remove strings directories in the gadget::

	$ rmdir strings/<lang>

e.g.::

	$ rmdir strings/0x409

and finally remove the gadget::

	$ cd ..
	$ rmdir <gadget name>

e.g.::

	$ rmdir g1



Configfs item, group, attribute

305-321

Configfs에는 directory로 표현되는 item과 group이 있습니다. Group은 다른 group을 포함할 수 있다는 점에서 item과 다릅니다.

Item과 group 모두 file로 표현되는 attribute를 가질 수 있습니다. 사용자는 directory를 생성·제거할 수 있지만 file은 제거할 수 없으며 file은 의미에 따라 read-only 또는 read-write입니다.

Filesystem layer는 모든 구성 요소에 공통인 `config_item`/group과 `configfs_attribute`를 다루지만, 실제로는 용도별 더 큰 structure에 embedded됩니다. 예제의 `cs`는 `config_item`, `sa`는 `configfs_attribute`를 포함합니다.

Configfs 객체 모델
객체Filesystem 표현특징
config itemDirectoryAttribute file 보유
config groupDirectory다른 group과 default subgroup 포함 가능
configfs attributeFileRead-only 또는 read-write
usage-specific structureGeneric object의 container실제 설정 data와 callback 보유

Generic configfs object와 사용별 container의 관계입니다.

Implementation design
=====================

Below the idea of how configfs works is presented.
In configfs there are items and groups, both represented as directories.
The difference between an item and a group is that a group can contain
other groups. In the picture below only an item is shown.
Both items and groups can have attributes, which are represented as files.
The user can create and remove directories, but cannot remove files,
which can be read-only or read-write, depending on what they represent.

The filesystem part of configfs operates on config_items/groups and
configfs_attributes which are generic and of the same type for all
configured elements. However, they are embedded in usage-specific
larger structures. In the picture below there is a "cs" which contains
a config_item and an "sa" which contains a configfs_attribute.

Filesystem view와 show/store

322-342

Filesystem view에서는 `cs`가 directory이고 `sa`가 그 안의 file입니다.

사용자가 `sa` file을 읽거나 쓰면 `struct config_item`과 `struct configfs_attribute`를 받는 function이 호출됩니다. Function은 `container_of`로 바깥의 `cs`와 `sa`를 복원한 뒤 적절한 `show` 또는 `store` callback을 호출합니다.

`show`는 `cs`의 data를 character buffer로 복사해 file 내용을 표시하고, `store`는 buffer의 data를 `cs`로 복사해 내용을 변경합니다. 실제 의미는 callback 구현자가 정합니다.

The filesystem view would be like this::

  ./
  ./cs        (directory)
     |
     +--sa    (file)
     |
     .
     .
     .

Whenever a user reads/writes the "sa" file, a function is called
which accepts a struct config_item and a struct configfs_attribute.
In the said function the "cs" and "sa" are retrieved using the well
known container_of technique and an appropriate sa's function (show or
store) is called and passed the "cs" and a character buffer. The "show"
is for displaying the file's contents (copy data from the cs to the
buffer), while the "store" is for modifying the file's contents (copy data
from the buffer to the cs), but it is up to the implementer of the
two functions to decide what they actually do.

cs와 sa 관계 도식

343-361

원문의 ASCII 그림은 configured structure `cs` 내부의 `config_item`과 specific attribute `sa` 내부의 `configfs_attribute` 및 callback 관계를 나타냅니다.

config_item에서 specific callback까지
User가 cs/sa attribute file read 또는 writeConfigfs가 struct config_item과 struct configfs_attribute 전달container_of로 configured_structure cs와 specific_attribute sa 복원sa의 show(cs, buffer) 또는 store(cs, buffer, length) 호출cs의 설정 data와 buffer 사이를 읽거나 갱신

ASCII 구조도를 동일한 의미의 단계형 관계 도식으로 재구성했습니다.

::

  typedef struct configured_structure cs;
  typedef struct specific_attribute sa;

                                         sa
                         +----------------------------------+
          cs             |  (*show)(cs *, buffer);          |
  +-----------------+    |  (*store)(cs *, buffer, length); |
  |                 |    |                                  |
  | +-------------+ |    |       +------------------+       |
  | | struct      |-|----|------>|struct            |       |
  | | config_item | |    |       |configfs_attribute|       |
  | +-------------+ |    |       +------------------+       |
  |                 |    +----------------------------------+
  | data to be set  |                .
  |                 |                .
  +-----------------+                .

이름과 default subgroup

362-368

File 이름은 config item/group 설계자가 정하고 directory 이름은 일반적으로 사용자가 자유롭게 정할 수 있습니다. Group에는 여러 default subgroup이 자동 생성될 수 있습니다.

Configfs 자체의 자세한 내용은 `Documentation/filesystems/configfs.rst`를 참고합니다.

The file names are decided by the config item/group designer, while
the directories in general can be named at will. A group can have
a number of its default sub-groups created automatically.

For more information on configfs please see
`Documentation/filesystems/configfs.rst`.

Configfs 개념의 USB gadget 대응

369-377

Gadget은 `idVendor`, `idProduct` 같은 attribute와 `configs`, `functions`, `strings` default subgroup을 가진 config group입니다.

Attribute write는 적절한 내부 위치에 정보를 저장합니다. 사용자는 default subgroup 아래에 configuration, function, language별 string group을 나타내는 subgroup을 만듭니다.

The concepts described above translate to USB gadgets like this:

1. A gadget has its config group, which has some attributes (idVendor,
   idProduct etc) and default sub-groups (configs, functions, strings).
   Writing to the attributes causes the information to be stored in appropriate
   locations. In the configs, functions and strings sub-groups a user can
   create their sub-groups to represent configurations, functions, and groups
   of strings in a given language.

UDC write와 전체 function bind

378-384

사용자는 configuration과 function을 만들고 configuration 안에 function symlink를 만듭니다.

`UDC` attribute를 쓰면 gadget을 controller에 bind합니다. `drivers/usb/gadget/configfs.c`는 모든 configuration을 순회하고 각 configuration의 모든 function을 순회해 bind하므로 전체 gadget이 결합됩니다.

UDC bind 내부 순회
User가 gadget UDC attribute에 controller 이름 기록drivers/usb/gadget/configfs.c가 configuration 목록 순회각 configuration의 function symlink 목록 순회각 function instance bind모든 configuration이 결합되어 gadget 활성화

UDC attribute write가 전체 gadget binding으로 이어지는 구조입니다.

2. The user creates configurations and functions, in the configurations
   creates symbolic links to functions. This information is used when the
   gadget's UDC attribute is written to, which means binding the gadget to the
   UDC. The code in drivers/usb/gadget/configfs.c iterates over all
   configurations, and in each configuration it iterates over all functions and
   binds them. This way the whole gadget is bound.

Configfs core와 function 구현 파일

385-394

`drivers/usb/gadget/configfs.c`에는 gadget config_group, `configs`/`functions`/`strings` default group, symlink를 통한 function-configuration 연결 code가 있습니다.

각 USB function의 고유 설정 view는 해당 function 구현 file `drivers/usb/gadget/f_*.c`에 config_group으로 정의됩니다.

3. The file drivers/usb/gadget/configfs.c contains code for

	- gadget's config_group
	- gadget's default groups (configs, functions, strings)
	- associating functions with configurations (symlinks)

4. Each USB function naturally has its own view of what it wants configured, so
   config_groups for particular functions are defined in the functions
   implementation files drivers/usb/gadget/f_*.c.

Function module 자동 load와 잔류

395-399

Function code는 `usb_get_function_instance()`를 사용하고 이 함수가 `request_module()`을 호출합니다. `modprobe`가 동작하면 특정 function module이 자동으로 load됩니다.

반대 방향은 자동이 아닙니다. Gadget을 disable하고 해체해도 function module은 load된 상태로 남습니다.

5. Function's code is written in such a way that it uses
   usb_get_function_instance(), which, in turn, calls request_module.  So,
   provided that modprobe works, modules for particular functions are loaded
   automatically. Please note that the converse is not true: after a gadget is
   disabled and torn down, the modules remain loaded.