Documentation/driver-api/extcon.rst GitHub 원문 ↗

Linux 6.18.37 · Driver API

Extcon Device Subsystem

external connector state, mutually exclusive cable, property/sysfs API와 managed driver 예제를 설명합니다.

Source pathDocumentation/driver-api/extcon.rst
Source versionLinux v6.18.37
TranslationDUJINLABS 전문 번역 + 해설

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

1. 요약·해설

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

요약과 해설

extcon.rst:1-255

Extcon은 USB, charging, audio, display 같은 external connector의 state와 property를 공통 model로 보고합니다. `supported_cable`과 state bitmap, cable별 notifier를 사용하고 `mutually_exclusive` bitmask로 hardware가 허용하지 않는 동시 연결을 차단합니다.

provider driver는 managed allocation/registration API로 `extcon_dev`를 등록하고 `extcon_set_state_sync()`로 state와 notification을 갱신합니다. userspace는 device-wide state와 `cable.N` sysfs attribute로 이를 관찰합니다.

2. 영어 원문 전체

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

원문 전체 펼치기
1 =======================
2 Extcon Device Subsystem
3 =======================
4
5 Overview
6 ========
7
8 The Extcon (External Connector) subsystem provides a unified framework for
9 managing external connectors in Linux systems. It allows drivers to report
10 the state of external connectors and provides a standardized interface for
11 userspace to query and monitor these states.
12
13 Extcon is particularly useful in modern devices with multiple connectivity
14 options, such as smartphones, tablets, and laptops. It helps manage various
15 types of connectors, including:
16
17 1. USB connectors (e.g., USB-C, micro-USB)
18 2. Charging ports (e.g., fast charging, wireless charging)
19 3. Audio jacks (e.g., 3.5mm headphone jack)
20 4. Video outputs (e.g., HDMI, DisplayPort)
21 5. Docking stations
22
23 Real-world examples:
24
25 1. Smartphone USB-C port:
26 A single USB-C port on a smartphone can serve multiple functions. Extcon
27 can manage the different states of this port, such as:
28 - USB data connection
29 - Charging (various types like fast charging, USB Power Delivery)
30 - Audio output (USB-C headphones)
31 - Video output (USB-C to HDMI adapter)
32
33 2. Laptop docking station:
34 When a laptop is connected to a docking station, multiple connections are
35 made simultaneously. Extcon can handle the state changes for:
36 - Power delivery
37 - External displays
38 - USB hub connections
39 - Ethernet connectivity
40
41 3. Wireless charging pad:
42 Extcon can manage the state of a wireless charging connection, allowing
43 the system to respond appropriately when a device is placed on or removed
44 from the charging pad.
45
46 4. Smart TV HDMI ports:
47 In a smart TV, Extcon can manage multiple HDMI ports, detecting when
48 devices are connected or disconnected, and potentially identifying the
49 type of device (e.g., gaming console, set-top box, Blu-ray player).
50
51 The Extcon framework simplifies the development of drivers for these complex
52 scenarios by providing a standardized way to report and query connector
53 states, handle mutually exclusive connections, and manage connector
54 properties. This allows for more robust and flexible handling of external
55 connections in modern devices.
56
57 Key Components
58 ==============
59
60 extcon_dev
61 ----------
62
63 The core structure representing an Extcon device::
64
65 struct extcon_dev {
66 const char *name;
67 const unsigned int *supported_cable;
68 const u32 *mutually_exclusive;
69
70 /* Internal data */
71 struct device dev;
72 unsigned int id;
73 struct raw_notifier_head nh_all;
74 struct raw_notifier_head *nh;
75 struct list_head entry;
76 int max_supported;
77 spinlock_t lock;
78 u32 state;
79
80 /* Sysfs related */
81 struct device_type extcon_dev_type;
82 struct extcon_cable *cables;
83 struct attribute_group attr_g_muex;
84 struct attribute **attrs_muex;
85 struct device_attribute *d_attrs_muex;
86 };
87
88 Key fields:
89
90 - ``name``: Name of the Extcon device
91 - ``supported_cable``: Array of supported cable types
92 - ``mutually_exclusive``: Array defining mutually exclusive cable types
93 This field is crucial for enforcing hardware constraints. It's an array of
94 32-bit unsigned integers, where each element represents a set of mutually
95 exclusive cable types. The array should be terminated with a 0.
96
97 For example:
98
99 ::
100
101 static const u32 mutually_exclusive[] = {
102 BIT(0) | BIT(1), /* Cable 0 and 1 are mutually exclusive */
103 BIT(2) | BIT(3) | BIT(4), /* Cables 2, 3, and 4 are mutually exclusive */
104 0 /* Terminator */
105 };
106
107 In this example, cables 0 and 1 cannot be connected simultaneously, and
108 cables 2, 3, and 4 are also mutually exclusive. This is useful for
109 scenarios like a single port that can either be USB or HDMI, but not both
110 at the same time.
111
112 The Extcon core uses this information to prevent invalid combinations of
113 cable states, ensuring that the reported states are always consistent
114 with the hardware capabilities.
115
116 - ``state``: Current state of the device (bitmap of connected cables)
117
118
119 extcon_cable
120 ------------
121
122 Represents an individual cable managed by an Extcon device::
123
124 struct extcon_cable {
125 struct extcon_dev *edev;
126 int cable_index;
127 struct attribute_group attr_g;
128 struct device_attribute attr_name;
129 struct device_attribute attr_state;
130 struct attribute *attrs[3];
131 union extcon_property_value usb_propval[EXTCON_PROP_USB_CNT];
132 union extcon_property_value chg_propval[EXTCON_PROP_CHG_CNT];
133 union extcon_property_value jack_propval[EXTCON_PROP_JACK_CNT];
134 union extcon_property_value disp_propval[EXTCON_PROP_DISP_CNT];
135 DECLARE_BITMAP(usb_bits, EXTCON_PROP_USB_CNT);
136 DECLARE_BITMAP(chg_bits, EXTCON_PROP_CHG_CNT);
137 DECLARE_BITMAP(jack_bits, EXTCON_PROP_JACK_CNT);
138 DECLARE_BITMAP(disp_bits, EXTCON_PROP_DISP_CNT);
139 };
140
141 Core Functions
142 ==============
143
144 .. kernel-doc:: drivers/extcon/extcon.c
145 :identifiers: extcon_get_state
146
147 .. kernel-doc:: drivers/extcon/extcon.c
148 :identifiers: extcon_set_state
149
150 .. kernel-doc:: drivers/extcon/extcon.c
151 :identifiers: extcon_set_state_sync
152
153 .. kernel-doc:: drivers/extcon/extcon.c
154 :identifiers: extcon_get_property
155
156
157 Sysfs Interface
158 ===============
159
160 Extcon devices expose the following sysfs attributes:
161
162 - ``name``: Name of the Extcon device
163 - ``state``: Current state of all supported cables
164 - ``cable.N/name``: Name of the Nth supported cable
165 - ``cable.N/state``: State of the Nth supported cable
166
167 Usage Example
168 -------------
169
170 .. code-block:: c
171
172 #include <linux/module.h>
173 #include <linux/platform_device.h>
174 #include <linux/extcon.h>
175
176 struct my_extcon_data {
177 struct extcon_dev *edev;
178 struct device *dev;
179 };
180
181 static const unsigned int my_extcon_cable[] = {
182 EXTCON_USB,
183 EXTCON_USB_HOST,
184 EXTCON_NONE,
185 };
186
187 static int my_extcon_probe(struct platform_device *pdev)
188 {
189 struct my_extcon_data *data;
190 int ret;
191
192 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
193 if (!data)
194 return -ENOMEM;
195
196 data->dev = &pdev->dev;
197
198 /* Initialize extcon device */
199 data->edev = devm_extcon_dev_allocate(data->dev, my_extcon_cable);
200 if (IS_ERR(data->edev)) {
201 dev_err(data->dev, "Failed to allocate extcon device\n");
202 return PTR_ERR(data->edev);
203 }
204
205 /* Register extcon device */
206 ret = devm_extcon_dev_register(data->dev, data->edev);
207 if (ret < 0) {
208 dev_err(data->dev, "Failed to register extcon device\n");
209 return ret;
210 }
211
212 platform_set_drvdata(pdev, data);
213
214 /* Example: Set initial state */
215 extcon_set_state_sync(data->edev, EXTCON_USB, true);
216
217 dev_info(data->dev, "My extcon driver probed successfully\n");
218 return 0;
219 }
220
221 static int my_extcon_remove(struct platform_device *pdev)
222 {
223 struct my_extcon_data *data = platform_get_drvdata(pdev);
224
225 /* Example: Clear state before removal */
226 extcon_set_state_sync(data->edev, EXTCON_USB, false);
227
228 dev_info(data->dev, "My extcon driver removed\n");
229 return 0;
230 }
231
232 static const struct of_device_id my_extcon_of_match[] = {
233 { .compatible = "my,extcon-device", },
234 { },
235 };
236 MODULE_DEVICE_TABLE(of, my_extcon_of_match);
237
238 static struct platform_driver my_extcon_driver = {
239 .driver = {
240 .name = "my-extcon-driver",
241 .of_match_table = my_extcon_of_match,
242 },
243 .probe = my_extcon_probe,
244 .remove = my_extcon_remove,
245 };
246
247 module_platform_driver(my_extcon_driver);
248
249 This example demonstrates:
250 ---------------------------
251
252 - Defining supported cable types (USB and USB Host in this case).
253 - Allocating and registering an extcon device.
254 - Setting an initial state for a cable (USB connected in this example).
255 - Clearing the state when the driver is removed.
256

3. 한국어 전문 번역

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

Extcon subsystem 개요와 사용 사례

1-56

문서 제목은 `Extcon Device Subsystem`입니다. Extcon(External Connector) subsystem은 Linux system의 external connector를 관리하는 통합 framework입니다. driver가 connector state를 보고할 수 있게 하고 userspace가 그 state를 query하고 monitor하는 표준 interface를 제공합니다.

Extcon은 smartphone, tablet, laptop처럼 connectivity option이 많은 현대 device에서 특히 유용합니다. USB-C·micro-USB connector, fast/wireless charging port, 3.5mm audio jack, HDMI·DisplayPort video output, docking station 등을 관리할 수 있습니다.

smartphone의 USB-C port 하나는 USB data, fast charging·USB Power Delivery, USB-C headphone audio, USB-C-to-HDMI video output 같은 여러 기능을 가질 수 있으며 Extcon이 각 state를 관리합니다.

laptop을 docking station에 연결하면 power delivery, external display, USB hub, Ethernet connection이 동시에 생길 수 있고 Extcon이 이 state change를 처리합니다.

wireless charging pad에서는 device를 pad에 올리거나 제거할 때 charging connection state를 관리해 system이 적절히 반응하게 합니다.

smart TV에서는 여러 HDMI port의 connect/disconnect를 감지하고 gaming console, set-top box, Blu-ray player 같은 device type까지 식별하는 데 사용할 수 있습니다.

Extcon framework는 connector state report/query, mutually exclusive connection, connector property 관리를 표준화해 복잡한 driver 개발을 단순화하고 external connection을 더 견고하고 유연하게 처리합니다.

Extcon 대표 사용 사례
DeviceConnector관리 state
SmartphoneUSB-Cdata, charging, audio, video
LaptopDockpower, display, USB hub, Ethernet
Mobile deviceWireless padcharging attach/detach
Smart TVHDMI portsconnect/disconnect와 device type

하나 또는 여러 connector에서 관리하는 state를 정리했습니다.

extcon_dev와 mutually exclusive cable

57-118

`struct extcon_dev`는 Extcon device를 나타내는 core structure입니다.

struct extcon_dev {
    const char *name;
    const unsigned int *supported_cable;
    const u32 *mutually_exclusive;

    /* Internal data */
    struct device dev;
    unsigned int id;
    struct raw_notifier_head nh_all;
    struct raw_notifier_head *nh;
    struct list_head entry;
    int max_supported;
    spinlock_t lock;
    u32 state;

    /* Sysfs related */
    struct device_type extcon_dev_type;
    struct extcon_cable *cables;
    struct attribute_group attr_g_muex;
    struct attribute **attrs_muex;
    struct device_attribute *d_attrs_muex;
};

`name`은 Extcon device 이름이고 `supported_cable`은 지원 cable type array입니다. `mutually_exclusive`는 동시에 연결될 수 없는 cable type 집합을 정의하는 array입니다. `state`는 현재 연결된 cable을 나타내는 bitmap입니다.

internal field에는 generic `struct device`, instance `id`, 전체/cable별 raw notifier head, global entry list, 최대 cable 수, state를 보호하는 spinlock이 있습니다. sysfs 관련 field는 device type, cable object array, mutually exclusive attribute group과 attribute pointer를 보관합니다.

`mutually_exclusive`는 hardware constraint를 강제하는 핵심 field입니다. 각 32-bit unsigned integer가 서로 배타적인 cable type 집합을 bitmask로 표현하며 array는 0으로 끝나야 합니다.

static const u32 mutually_exclusive[] = {
    BIT(0) | BIT(1),  /* Cable 0 and 1 are mutually exclusive */
    BIT(2) | BIT(3) | BIT(4),  /* Cables 2, 3, and 4 are mutually exclusive */
    0  /* Terminator */
};

예제에서는 cable 0과 1을 동시에 연결할 수 없고 cable 2, 3, 4도 서로 배타적입니다. 하나의 port가 USB 또는 HDMI 중 하나만 제공할 수 있는 경우에 유용합니다. Extcon core는 이 정보로 잘못된 state 조합을 막아 report state가 hardware capability와 항상 일치하게 합니다.

extcon_dev field 분류
분류Field역할
Identityname, idExtcon instance 식별
Capabilitysupported_cable지원 cable type 목록
Constraintmutually_exclusive동시 state 금지 bitmask
Runtimestate, lock연결 bitmap과 동기화
Notificationnh_all, nh전체·cable별 notifier
Sysfscables, attr_g_muex, attrs_muexcable와 배타성 attribute

public configuration, runtime state와 sysfs 내부 data를 구분했습니다.

Mutually exclusive state 검증
현재 state bitmap 읽기요청 cable bit 적용 후보 생성각 mutually_exclusive mask와 교집합 계산한 mask에서 여러 bit가 켜지면 invalid유효한 조합만 state와 notifier에 반영

새 cable state를 적용하기 전 core가 hardware constraint를 확인하는 개념 흐름입니다.

extcon_cable과 property 저장소

119-140

`struct extcon_cable`은 Extcon device가 관리하는 개별 cable을 나타냅니다.

struct extcon_cable {
    struct extcon_dev *edev;
    int cable_index;
    struct attribute_group attr_g;
    struct device_attribute attr_name;
    struct device_attribute attr_state;
    struct attribute *attrs[3];
    union extcon_property_value usb_propval[EXTCON_PROP_USB_CNT];
    union extcon_property_value chg_propval[EXTCON_PROP_CHG_CNT];
    union extcon_property_value jack_propval[EXTCON_PROP_JACK_CNT];
    union extcon_property_value disp_propval[EXTCON_PROP_DISP_CNT];
    DECLARE_BITMAP(usb_bits, EXTCON_PROP_USB_CNT);
    DECLARE_BITMAP(chg_bits, EXTCON_PROP_CHG_CNT);
    DECLARE_BITMAP(jack_bits, EXTCON_PROP_JACK_CNT);
    DECLARE_BITMAP(disp_bits, EXTCON_PROP_DISP_CNT);
};

`edev`는 owner `extcon_dev`, `cable_index`는 지원 cable array의 index입니다. `attr_g`, `attr_name`, `attr_state`, `attrs`는 cable별 sysfs name/state attribute를 구성합니다.

USB, charger, jack, display category마다 `union extcon_property_value` array를 가지고, 각 category의 지원 property를 표시하는 bitmap도 별도로 선언합니다.

extcon_cable property group
CategoryValue storageSupported-property bitmap
USBusb_propvalusb_bits
Chargerchg_propvalchg_bits
Jackjack_propvaljack_bits
Displaydisp_propvaldisp_bits

connector category별 value array와 capability bitmap의 쌍입니다.

Core function

141-156

Extcon core function은 `drivers/extcon/extcon.c`의 kernel-doc에서 가져옵니다.

`extcon_get_state`는 cable의 현재 connected state를 조회합니다.

.. kernel-doc:: drivers/extcon/extcon.c
   :identifiers: extcon_get_state

`extcon_set_state`는 cable state를 변경합니다.

.. kernel-doc:: drivers/extcon/extcon.c
   :identifiers: extcon_set_state

`extcon_set_state_sync`는 state를 변경하고 관련 notification을 동기적으로 전달하는 API입니다.

.. kernel-doc:: drivers/extcon/extcon.c
   :identifiers: extcon_set_state_sync

`extcon_get_property`는 connector의 지정 property value를 읽습니다.

.. kernel-doc:: drivers/extcon/extcon.c
   :identifiers: extcon_get_property
Extcon core API
API대상동작
extcon_get_stateCable현재 connected state 조회
extcon_set_stateCablestate 변경
extcon_set_state_syncCablestate 변경과 notification 동기화
extcon_get_propertyConnector propertyproperty value 조회

state와 property를 조회·변경하는 핵심 operation입니다.

Sysfs interface

157-166

Extcon device는 sysfs에 다음 attribute를 노출합니다.

  • `name`: Extcon device 이름
  • `state`: 지원하는 모든 cable의 현재 state
  • `cable.N/name`: N번째 지원 cable의 이름
  • `cable.N/state`: N번째 지원 cable의 state
Extcon sysfs hierarchy
경로 형태범위내용
nameDeviceExtcon device 이름
stateDevice전체 cable state bitmap
cable.N/nameCable N지원 cable 이름
cable.N/stateCable N개별 connected state

device-wide attribute와 cable별 attribute를 구분했습니다.

Managed Extcon platform driver 예제

167-248

다음 전체 예제는 USB와 USB Host cable을 지원하는 Extcon platform driver를 정의합니다.

.. code-block:: c

    #include <linux/module.h>
    #include <linux/platform_device.h>
    #include <linux/extcon.h>

    struct my_extcon_data {
        struct extcon_dev *edev;
        struct device *dev;
    };

    static const unsigned int my_extcon_cable[] = {
        EXTCON_USB,
        EXTCON_USB_HOST,
        EXTCON_NONE,
    };

    static int my_extcon_probe(struct platform_device *pdev)
    {
        struct my_extcon_data *data;
        int ret;

        data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
        if (!data)
            return -ENOMEM;

        data->dev = &pdev->dev;

        /* Initialize extcon device */
        data->edev = devm_extcon_dev_allocate(data->dev, my_extcon_cable);
        if (IS_ERR(data->edev)) {
            dev_err(data->dev, "Failed to allocate extcon device\n");
            return PTR_ERR(data->edev);
        }

        /* Register extcon device */
        ret = devm_extcon_dev_register(data->dev, data->edev);
        if (ret < 0) {
            dev_err(data->dev, "Failed to register extcon device\n");
            return ret;
        }

        platform_set_drvdata(pdev, data);

        /* Example: Set initial state */
        extcon_set_state_sync(data->edev, EXTCON_USB, true);

        dev_info(data->dev, "My extcon driver probed successfully\n");
        return 0;
    }

    static int my_extcon_remove(struct platform_device *pdev)
    {
        struct my_extcon_data *data = platform_get_drvdata(pdev);

        /* Example: Clear state before removal */
        extcon_set_state_sync(data->edev, EXTCON_USB, false);

        dev_info(data->dev, "My extcon driver removed\n");
        return 0;
    }

    static const struct of_device_id my_extcon_of_match[] = {
        { .compatible = "my,extcon-device", },
        { },
    };
    MODULE_DEVICE_TABLE(of, my_extcon_of_match);

    static struct platform_driver my_extcon_driver = {
        .driver = {
            .name = "my-extcon-driver",
            .of_match_table = my_extcon_of_match,
        },
        .probe = my_extcon_probe,
        .remove = my_extcon_remove,
    };

    module_platform_driver(my_extcon_driver);

`my_extcon_cable`은 `EXTCON_USB`, `EXTCON_USB_HOST`를 나열하고 `EXTCON_NONE`으로 끝납니다. probe는 `devm_kzalloc()`으로 private data를 할당하고 `devm_extcon_dev_allocate()`로 Extcon device를 만든 뒤 `devm_extcon_dev_register()`로 등록합니다.

등록 성공 후 `platform_set_drvdata()`로 data를 저장하고 `extcon_set_state_sync(..., EXTCON_USB, true)`로 USB connected 초기 state를 설정합니다. remove에서는 `platform_get_drvdata()`로 data를 얻어 같은 API에 false를 넘겨 state를 clear합니다.

OF match table은 compatible string `my,extcon-device`를 사용하고 `module_platform_driver()`로 platform driver를 등록합니다. managed allocation과 registration을 사용하므로 resource cleanup은 device lifecycle에 결합됩니다.

Managed Extcon driver lifecycle
devm_kzalloc private datadevm_extcon_dev_allocate supported cable 목록devm_extcon_dev_registerplatform_set_drvdataextcon_set_state_sync USB=trueremove에서 USB=false

예제의 probe, state report와 remove 순서입니다.

예제가 보여주는 핵심

249-255

예제는 다음 네 가지를 보여줍니다.

  • USB와 USB Host라는 지원 cable type 정의
  • Extcon device allocation과 registration
  • USB connected라는 cable 초기 state 설정
  • driver remove 때 state clear