Simon Glass | 9065bc9 | 2020-12-28 20:35:06 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # SPDX-License-Identifier: GPL-2.0+ |
| 3 | # |
| 4 | # Copyright (C) 2017 Google, Inc |
| 5 | # Written by Simon Glass <sjg@chromium.org> |
| 6 | # |
| 7 | |
| 8 | """Scanning of U-Boot source for drivers and structs |
| 9 | |
| 10 | This scans the source tree to find out things about all instances of |
| 11 | U_BOOT_DRIVER(), UCLASS_DRIVER and all struct declarations in header files. |
| 12 | |
| 13 | See doc/driver-model/of-plat.rst for more informaiton |
| 14 | """ |
| 15 | |
| 16 | import os |
| 17 | import re |
| 18 | import sys |
| 19 | |
| 20 | |
| 21 | def conv_name_to_c(name): |
| 22 | """Convert a device-tree name to a C identifier |
| 23 | |
| 24 | This uses multiple replace() calls instead of re.sub() since it is faster |
| 25 | (400ms for 1m calls versus 1000ms for the 're' version). |
| 26 | |
| 27 | Args: |
| 28 | name (str): Name to convert |
| 29 | Return: |
| 30 | str: String containing the C version of this name |
| 31 | """ |
| 32 | new = name.replace('@', '_at_') |
| 33 | new = new.replace('-', '_') |
| 34 | new = new.replace(',', '_') |
| 35 | new = new.replace('.', '_') |
| 36 | return new |
| 37 | |
| 38 | def get_compat_name(node): |
| 39 | """Get the node's list of compatible string as a C identifiers |
| 40 | |
| 41 | Args: |
| 42 | node (fdt.Node): Node object to check |
| 43 | Return: |
| 44 | list of str: List of C identifiers for all the compatible strings |
| 45 | """ |
| 46 | compat = node.props['compatible'].value |
| 47 | if not isinstance(compat, list): |
| 48 | compat = [compat] |
| 49 | return [conv_name_to_c(c) for c in compat] |
| 50 | |
| 51 | |
| 52 | class Driver: |
| 53 | """Information about a driver in U-Boot |
| 54 | |
| 55 | Attributes: |
| 56 | name: Name of driver. For U_BOOT_DRIVER(x) this is 'x' |
Simon Glass | 78933b7 | 2021-02-03 06:00:50 -0700 | [diff] [blame] | 57 | fname: Filename where the driver was found |
| 58 | uclass_id: Name of uclass, e.g. 'UCLASS_I2C' |
| 59 | compat: Driver data for each compatible string: |
| 60 | key: Compatible string, e.g. 'rockchip,rk3288-grf' |
| 61 | value: Driver data, e,g, 'ROCKCHIP_SYSCON_GRF', or None |
| 62 | fname: Filename where the driver was found |
| 63 | priv (str): struct name of the priv_auto member, e.g. 'serial_priv' |
Simon Glass | 0f3b141 | 2021-02-03 06:00:53 -0700 | [diff] [blame] | 64 | plat (str): struct name of the plat_auto member, e.g. 'serial_plat' |
| 65 | child_priv (str): struct name of the per_child_auto member, |
| 66 | e.g. 'pci_child_priv' |
| 67 | child_plat (str): struct name of the per_child_plat_auto member, |
| 68 | e.g. 'pci_child_plat' |
Simon Glass | eb3c249 | 2021-02-03 06:01:01 -0700 | [diff] [blame^] | 69 | used (bool): True if the driver is used by the structs being output |
Simon Glass | 9065bc9 | 2020-12-28 20:35:06 -0700 | [diff] [blame] | 70 | """ |
Simon Glass | 78933b7 | 2021-02-03 06:00:50 -0700 | [diff] [blame] | 71 | def __init__(self, name, fname): |
Simon Glass | 9065bc9 | 2020-12-28 20:35:06 -0700 | [diff] [blame] | 72 | self.name = name |
Simon Glass | 78933b7 | 2021-02-03 06:00:50 -0700 | [diff] [blame] | 73 | self.fname = fname |
| 74 | self.uclass_id = None |
| 75 | self.compat = None |
| 76 | self.priv = '' |
Simon Glass | 0f3b141 | 2021-02-03 06:00:53 -0700 | [diff] [blame] | 77 | self.plat = '' |
| 78 | self.child_priv = '' |
| 79 | self.child_plat = '' |
Simon Glass | eb3c249 | 2021-02-03 06:01:01 -0700 | [diff] [blame^] | 80 | self.used = False |
Simon Glass | 9065bc9 | 2020-12-28 20:35:06 -0700 | [diff] [blame] | 81 | |
| 82 | def __eq__(self, other): |
Simon Glass | 78933b7 | 2021-02-03 06:00:50 -0700 | [diff] [blame] | 83 | return (self.name == other.name and |
| 84 | self.uclass_id == other.uclass_id and |
| 85 | self.compat == other.compat and |
Simon Glass | 0f3b141 | 2021-02-03 06:00:53 -0700 | [diff] [blame] | 86 | self.priv == other.priv and |
Simon Glass | eb3c249 | 2021-02-03 06:01:01 -0700 | [diff] [blame^] | 87 | self.plat == other.plat and |
| 88 | self.used == other.used) |
Simon Glass | 9065bc9 | 2020-12-28 20:35:06 -0700 | [diff] [blame] | 89 | |
| 90 | def __repr__(self): |
Simon Glass | eb3c249 | 2021-02-03 06:01:01 -0700 | [diff] [blame^] | 91 | return ("Driver(name='%s', used=%s, uclass_id='%s', compat=%s, priv=%s)" % |
| 92 | (self.name, self.used, self.uclass_id, self.compat, self.priv)) |
Simon Glass | 9065bc9 | 2020-12-28 20:35:06 -0700 | [diff] [blame] | 93 | |
| 94 | |
Simon Glass | 2e199ab | 2021-02-03 06:00:54 -0700 | [diff] [blame] | 95 | class UclassDriver: |
| 96 | """Holds information about a uclass driver |
| 97 | |
| 98 | Attributes: |
| 99 | name: Uclass name, e.g. 'i2c' if the driver is for UCLASS_I2C |
| 100 | uclass_id: Uclass ID, e.g. 'UCLASS_I2C' |
| 101 | priv: struct name of the private data, e.g. 'i2c_priv' |
| 102 | per_dev_priv (str): struct name of the priv_auto member, e.g. 'spi_info' |
| 103 | per_dev_plat (str): struct name of the plat_auto member, e.g. 'i2c_chip' |
| 104 | per_child_priv (str): struct name of the per_child_auto member, |
| 105 | e.g. 'pci_child_priv' |
| 106 | per_child_plat (str): struct name of the per_child_plat_auto member, |
| 107 | e.g. 'pci_child_plat' |
| 108 | """ |
| 109 | def __init__(self, name): |
| 110 | self.name = name |
| 111 | self.uclass_id = None |
| 112 | self.priv = '' |
| 113 | self.per_dev_priv = '' |
| 114 | self.per_dev_plat = '' |
| 115 | self.per_child_priv = '' |
| 116 | self.per_child_plat = '' |
| 117 | |
| 118 | def __eq__(self, other): |
| 119 | return (self.name == other.name and |
| 120 | self.uclass_id == other.uclass_id and |
| 121 | self.priv == other.priv) |
| 122 | |
| 123 | def __repr__(self): |
| 124 | return ("UclassDriver(name='%s', uclass_id='%s')" % |
| 125 | (self.name, self.uclass_id)) |
| 126 | |
| 127 | def __hash__(self): |
| 128 | # We can use the uclass ID since it is unique among uclasses |
| 129 | return hash(self.uclass_id) |
| 130 | |
| 131 | |
Simon Glass | 88bd538 | 2021-02-03 06:00:55 -0700 | [diff] [blame] | 132 | class Struct: |
| 133 | """Holds information about a struct definition |
| 134 | |
| 135 | Attributes: |
| 136 | name: Struct name, e.g. 'fred' if the struct is 'struct fred' |
| 137 | fname: Filename containing the struct, in a format that C files can |
| 138 | include, e.g. 'asm/clk.h' |
| 139 | """ |
| 140 | def __init__(self, name, fname): |
| 141 | self.name = name |
| 142 | self.fname =fname |
| 143 | |
| 144 | def __repr__(self): |
| 145 | return ("Struct(name='%s', fname='%s')" % (self.name, self.fname)) |
| 146 | |
| 147 | |
Simon Glass | 9065bc9 | 2020-12-28 20:35:06 -0700 | [diff] [blame] | 148 | class Scanner: |
| 149 | """Scanning of the U-Boot source tree |
| 150 | |
| 151 | Properties: |
| 152 | _basedir (str): Base directory of U-Boot source code. Defaults to the |
| 153 | grandparent of this file's directory |
| 154 | _drivers: Dict of valid driver names found in drivers/ |
| 155 | key: Driver name |
| 156 | value: Driver for that driver |
| 157 | _driver_aliases: Dict that holds aliases for driver names |
| 158 | key: Driver alias declared with |
| 159 | DM_DRIVER_ALIAS(driver_alias, driver_name) |
| 160 | value: Driver name declared with U_BOOT_DRIVER(driver_name) |
Simon Glass | df692c3 | 2020-12-28 20:35:07 -0700 | [diff] [blame] | 161 | _warning_disabled: true to disable warnings about driver names not found |
Simon Glass | 9065bc9 | 2020-12-28 20:35:06 -0700 | [diff] [blame] | 162 | _drivers_additional (list or str): List of additional drivers to use |
| 163 | during scanning |
Simon Glass | 78933b7 | 2021-02-03 06:00:50 -0700 | [diff] [blame] | 164 | _of_match: Dict holding information about compatible strings |
| 165 | key: Name of struct udevice_id variable |
| 166 | value: Dict of compatible info in that variable: |
| 167 | key: Compatible string, e.g. 'rockchip,rk3288-grf' |
| 168 | value: Driver data, e,g, 'ROCKCHIP_SYSCON_GRF', or None |
| 169 | _compat_to_driver: Maps compatible strings to Driver |
Simon Glass | 2e199ab | 2021-02-03 06:00:54 -0700 | [diff] [blame] | 170 | _uclass: Dict of uclass information |
| 171 | key: uclass name, e.g. 'UCLASS_I2C' |
| 172 | value: UClassDriver |
Simon Glass | 88bd538 | 2021-02-03 06:00:55 -0700 | [diff] [blame] | 173 | _structs: Dict of all structs found in U-Boot: |
| 174 | key: Name of struct |
| 175 | value: Struct object |
Simon Glass | 9065bc9 | 2020-12-28 20:35:06 -0700 | [diff] [blame] | 176 | """ |
Simon Glass | df692c3 | 2020-12-28 20:35:07 -0700 | [diff] [blame] | 177 | def __init__(self, basedir, warning_disabled, drivers_additional): |
Simon Glass | 9065bc9 | 2020-12-28 20:35:06 -0700 | [diff] [blame] | 178 | """Set up a new Scanner |
| 179 | """ |
| 180 | if not basedir: |
| 181 | basedir = sys.argv[0].replace('tools/dtoc/dtoc', '') |
| 182 | if basedir == '': |
| 183 | basedir = './' |
| 184 | self._basedir = basedir |
| 185 | self._drivers = {} |
| 186 | self._driver_aliases = {} |
| 187 | self._drivers_additional = drivers_additional or [] |
| 188 | self._warning_disabled = warning_disabled |
Simon Glass | 78933b7 | 2021-02-03 06:00:50 -0700 | [diff] [blame] | 189 | self._of_match = {} |
| 190 | self._compat_to_driver = {} |
Simon Glass | 2e199ab | 2021-02-03 06:00:54 -0700 | [diff] [blame] | 191 | self._uclass = {} |
Simon Glass | 88bd538 | 2021-02-03 06:00:55 -0700 | [diff] [blame] | 192 | self._structs = {} |
Simon Glass | 9065bc9 | 2020-12-28 20:35:06 -0700 | [diff] [blame] | 193 | |
Simon Glass | 047a480 | 2021-02-03 06:01:00 -0700 | [diff] [blame] | 194 | def get_driver(self, name): |
| 195 | """Get a driver given its name |
| 196 | |
| 197 | Args: |
| 198 | name (str): Driver name |
| 199 | |
| 200 | Returns: |
| 201 | Driver: Driver or None if not found |
| 202 | """ |
| 203 | return self._drivers.get(name) |
| 204 | |
Simon Glass | 9065bc9 | 2020-12-28 20:35:06 -0700 | [diff] [blame] | 205 | def get_normalized_compat_name(self, node): |
| 206 | """Get a node's normalized compat name |
| 207 | |
| 208 | Returns a valid driver name by retrieving node's list of compatible |
| 209 | string as a C identifier and performing a check against _drivers |
| 210 | and a lookup in driver_aliases printing a warning in case of failure. |
| 211 | |
| 212 | Args: |
| 213 | node (Node): Node object to check |
| 214 | Return: |
| 215 | Tuple: |
| 216 | Driver name associated with the first compatible string |
| 217 | List of C identifiers for all the other compatible strings |
| 218 | (possibly empty) |
| 219 | In case of no match found, the return will be the same as |
| 220 | get_compat_name() |
| 221 | """ |
| 222 | compat_list_c = get_compat_name(node) |
| 223 | |
| 224 | for compat_c in compat_list_c: |
| 225 | if not compat_c in self._drivers.keys(): |
| 226 | compat_c = self._driver_aliases.get(compat_c) |
| 227 | if not compat_c: |
| 228 | continue |
| 229 | |
| 230 | aliases_c = compat_list_c |
| 231 | if compat_c in aliases_c: |
| 232 | aliases_c.remove(compat_c) |
| 233 | return compat_c, aliases_c |
| 234 | |
| 235 | if not self._warning_disabled: |
| 236 | print('WARNING: the driver %s was not found in the driver list' |
| 237 | % (compat_list_c[0])) |
| 238 | |
| 239 | return compat_list_c[0], compat_list_c[1:] |
| 240 | |
Simon Glass | 88bd538 | 2021-02-03 06:00:55 -0700 | [diff] [blame] | 241 | def _parse_structs(self, fname, buff): |
| 242 | """Parse a H file to extract struct definitions contained within |
| 243 | |
| 244 | This parses 'struct xx {' definitions to figure out what structs this |
| 245 | header defines. |
| 246 | |
| 247 | Args: |
| 248 | buff (str): Contents of file |
| 249 | fname (str): Filename (to use when printing errors) |
| 250 | """ |
| 251 | structs = {} |
| 252 | |
| 253 | re_struct = re.compile('^struct ([a-z0-9_]+) {$') |
| 254 | re_asm = re.compile('../arch/[a-z0-9]+/include/asm/(.*)') |
| 255 | prefix = '' |
| 256 | for line in buff.splitlines(): |
| 257 | # Handle line continuation |
| 258 | if prefix: |
| 259 | line = prefix + line |
| 260 | prefix = '' |
| 261 | if line.endswith('\\'): |
| 262 | prefix = line[:-1] |
| 263 | continue |
| 264 | |
| 265 | m_struct = re_struct.match(line) |
| 266 | if m_struct: |
| 267 | name = m_struct.group(1) |
| 268 | include_dir = os.path.join(self._basedir, 'include') |
| 269 | rel_fname = os.path.relpath(fname, include_dir) |
| 270 | m_asm = re_asm.match(rel_fname) |
| 271 | if m_asm: |
| 272 | rel_fname = 'asm/' + m_asm.group(1) |
| 273 | structs[name] = Struct(name, rel_fname) |
| 274 | self._structs.update(structs) |
| 275 | |
Simon Glass | 78933b7 | 2021-02-03 06:00:50 -0700 | [diff] [blame] | 276 | @classmethod |
| 277 | def _get_re_for_member(cls, member): |
| 278 | """_get_re_for_member: Get a compiled regular expression |
| 279 | |
| 280 | Args: |
| 281 | member (str): Struct member name, e.g. 'priv_auto' |
| 282 | |
| 283 | Returns: |
| 284 | re.Pattern: Compiled regular expression that parses: |
| 285 | |
| 286 | .member = sizeof(struct fred), |
| 287 | |
| 288 | and returns "fred" as group 1 |
| 289 | """ |
| 290 | return re.compile(r'^\s*.%s\s*=\s*sizeof\(struct\s+(.*)\),$' % member) |
| 291 | |
Simon Glass | 2e199ab | 2021-02-03 06:00:54 -0700 | [diff] [blame] | 292 | def _parse_uclass_driver(self, fname, buff): |
| 293 | """Parse a C file to extract uclass driver information contained within |
| 294 | |
| 295 | This parses UCLASS_DRIVER() structs to obtain various pieces of useful |
| 296 | information. |
| 297 | |
| 298 | It updates the following member: |
| 299 | _uclass: Dict of uclass information |
| 300 | key: uclass name, e.g. 'UCLASS_I2C' |
| 301 | value: UClassDriver |
| 302 | |
| 303 | Args: |
| 304 | fname (str): Filename being parsed (used for warnings) |
| 305 | buff (str): Contents of file |
| 306 | """ |
| 307 | uc_drivers = {} |
| 308 | |
| 309 | # Collect the driver name and associated Driver |
| 310 | driver = None |
| 311 | re_driver = re.compile(r'UCLASS_DRIVER\((.*)\)') |
| 312 | |
| 313 | # Collect the uclass ID, e.g. 'UCLASS_SPI' |
| 314 | re_id = re.compile(r'\s*\.id\s*=\s*(UCLASS_[A-Z0-9_]+)') |
| 315 | |
| 316 | # Matches the header/size information for uclass-private data |
| 317 | re_priv = self._get_re_for_member('priv_auto') |
| 318 | |
| 319 | # Set up parsing for the auto members |
| 320 | re_per_device_priv = self._get_re_for_member('per_device_auto') |
| 321 | re_per_device_plat = self._get_re_for_member('per_device_plat_auto') |
| 322 | re_per_child_priv = self._get_re_for_member('per_child_auto') |
| 323 | re_per_child_plat = self._get_re_for_member('per_child_plat_auto') |
| 324 | |
| 325 | prefix = '' |
| 326 | for line in buff.splitlines(): |
| 327 | # Handle line continuation |
| 328 | if prefix: |
| 329 | line = prefix + line |
| 330 | prefix = '' |
| 331 | if line.endswith('\\'): |
| 332 | prefix = line[:-1] |
| 333 | continue |
| 334 | |
| 335 | driver_match = re_driver.search(line) |
| 336 | |
| 337 | # If we have seen UCLASS_DRIVER()... |
| 338 | if driver: |
| 339 | m_id = re_id.search(line) |
| 340 | m_priv = re_priv.match(line) |
| 341 | m_per_dev_priv = re_per_device_priv.match(line) |
| 342 | m_per_dev_plat = re_per_device_plat.match(line) |
| 343 | m_per_child_priv = re_per_child_priv.match(line) |
| 344 | m_per_child_plat = re_per_child_plat.match(line) |
| 345 | if m_id: |
| 346 | driver.uclass_id = m_id.group(1) |
| 347 | elif m_priv: |
| 348 | driver.priv = m_priv.group(1) |
| 349 | elif m_per_dev_priv: |
| 350 | driver.per_dev_priv = m_per_dev_priv.group(1) |
| 351 | elif m_per_dev_plat: |
| 352 | driver.per_dev_plat = m_per_dev_plat.group(1) |
| 353 | elif m_per_child_priv: |
| 354 | driver.per_child_priv = m_per_child_priv.group(1) |
| 355 | elif m_per_child_plat: |
| 356 | driver.per_child_plat = m_per_child_plat.group(1) |
| 357 | elif '};' in line: |
| 358 | if not driver.uclass_id: |
| 359 | raise ValueError( |
| 360 | "%s: Cannot parse uclass ID in driver '%s'" % |
| 361 | (fname, driver.name)) |
| 362 | uc_drivers[driver.uclass_id] = driver |
| 363 | driver = None |
| 364 | |
| 365 | elif driver_match: |
| 366 | driver_name = driver_match.group(1) |
| 367 | driver = UclassDriver(driver_name) |
| 368 | |
| 369 | self._uclass.update(uc_drivers) |
| 370 | |
Simon Glass | 78933b7 | 2021-02-03 06:00:50 -0700 | [diff] [blame] | 371 | def _parse_driver(self, fname, buff): |
| 372 | """Parse a C file to extract driver information contained within |
| 373 | |
| 374 | This parses U_BOOT_DRIVER() structs to obtain various pieces of useful |
| 375 | information. |
| 376 | |
| 377 | It updates the following members: |
| 378 | _drivers - updated with new Driver records for each driver found |
| 379 | in the file |
| 380 | _of_match - updated with each compatible string found in the file |
| 381 | _compat_to_driver - Maps compatible string to Driver |
| 382 | |
| 383 | Args: |
| 384 | fname (str): Filename being parsed (used for warnings) |
| 385 | buff (str): Contents of file |
| 386 | |
| 387 | Raises: |
| 388 | ValueError: Compatible variable is mentioned in .of_match in |
| 389 | U_BOOT_DRIVER() but not found in the file |
| 390 | """ |
| 391 | # Dict holding information about compatible strings collected in this |
| 392 | # function so far |
| 393 | # key: Name of struct udevice_id variable |
| 394 | # value: Dict of compatible info in that variable: |
| 395 | # key: Compatible string, e.g. 'rockchip,rk3288-grf' |
| 396 | # value: Driver data, e,g, 'ROCKCHIP_SYSCON_GRF', or None |
| 397 | of_match = {} |
| 398 | |
| 399 | # Dict holding driver information collected in this function so far |
| 400 | # key: Driver name (C name as in U_BOOT_DRIVER(xxx)) |
| 401 | # value: Driver |
| 402 | drivers = {} |
| 403 | |
| 404 | # Collect the driver info |
| 405 | driver = None |
| 406 | re_driver = re.compile(r'U_BOOT_DRIVER\((.*)\)') |
| 407 | |
| 408 | # Collect the uclass ID, e.g. 'UCLASS_SPI' |
| 409 | re_id = re.compile(r'\s*\.id\s*=\s*(UCLASS_[A-Z0-9_]+)') |
| 410 | |
| 411 | # Collect the compatible string, e.g. 'rockchip,rk3288-grf' |
| 412 | compat = None |
| 413 | re_compat = re.compile(r'{\s*.compatible\s*=\s*"(.*)"\s*' |
| 414 | r'(,\s*.data\s*=\s*(\S*))?\s*},') |
| 415 | |
| 416 | # This is a dict of compatible strings that were found: |
| 417 | # key: Compatible string, e.g. 'rockchip,rk3288-grf' |
| 418 | # value: Driver data, e,g, 'ROCKCHIP_SYSCON_GRF', or None |
| 419 | compat_dict = {} |
| 420 | |
| 421 | # Holds the var nane of the udevice_id list, e.g. |
| 422 | # 'rk3288_syscon_ids_noc' in |
| 423 | # static const struct udevice_id rk3288_syscon_ids_noc[] = { |
| 424 | ids_name = None |
| 425 | re_ids = re.compile(r'struct udevice_id (.*)\[\]\s*=') |
| 426 | |
| 427 | # Matches the references to the udevice_id list |
| 428 | re_of_match = re.compile( |
| 429 | r'\.of_match\s*=\s*(of_match_ptr\()?([a-z0-9_]+)(\))?,') |
| 430 | |
Simon Glass | 0f3b141 | 2021-02-03 06:00:53 -0700 | [diff] [blame] | 431 | # Matches the struct name for priv, plat |
Simon Glass | 78933b7 | 2021-02-03 06:00:50 -0700 | [diff] [blame] | 432 | re_priv = self._get_re_for_member('priv_auto') |
Simon Glass | 0f3b141 | 2021-02-03 06:00:53 -0700 | [diff] [blame] | 433 | re_plat = self._get_re_for_member('plat_auto') |
| 434 | re_child_priv = self._get_re_for_member('per_child_auto') |
| 435 | re_child_plat = self._get_re_for_member('per_child_plat_auto') |
Simon Glass | 78933b7 | 2021-02-03 06:00:50 -0700 | [diff] [blame] | 436 | |
| 437 | prefix = '' |
| 438 | for line in buff.splitlines(): |
| 439 | # Handle line continuation |
| 440 | if prefix: |
| 441 | line = prefix + line |
| 442 | prefix = '' |
| 443 | if line.endswith('\\'): |
| 444 | prefix = line[:-1] |
| 445 | continue |
| 446 | |
| 447 | driver_match = re_driver.search(line) |
| 448 | |
| 449 | # If this line contains U_BOOT_DRIVER()... |
| 450 | if driver: |
| 451 | m_id = re_id.search(line) |
| 452 | m_of_match = re_of_match.search(line) |
| 453 | m_priv = re_priv.match(line) |
Simon Glass | 0f3b141 | 2021-02-03 06:00:53 -0700 | [diff] [blame] | 454 | m_plat = re_plat.match(line) |
| 455 | m_cplat = re_child_plat.match(line) |
| 456 | m_cpriv = re_child_priv.match(line) |
Simon Glass | 78933b7 | 2021-02-03 06:00:50 -0700 | [diff] [blame] | 457 | if m_priv: |
| 458 | driver.priv = m_priv.group(1) |
Simon Glass | 0f3b141 | 2021-02-03 06:00:53 -0700 | [diff] [blame] | 459 | elif m_plat: |
| 460 | driver.plat = m_plat.group(1) |
| 461 | elif m_cplat: |
| 462 | driver.child_plat = m_cplat.group(1) |
| 463 | elif m_cpriv: |
| 464 | driver.child_priv = m_cpriv.group(1) |
Simon Glass | 78933b7 | 2021-02-03 06:00:50 -0700 | [diff] [blame] | 465 | elif m_id: |
| 466 | driver.uclass_id = m_id.group(1) |
| 467 | elif m_of_match: |
| 468 | compat = m_of_match.group(2) |
| 469 | elif '};' in line: |
| 470 | if driver.uclass_id and compat: |
| 471 | if compat not in of_match: |
| 472 | raise ValueError( |
| 473 | "%s: Unknown compatible var '%s' (found: %s)" % |
| 474 | (fname, compat, ','.join(of_match.keys()))) |
| 475 | driver.compat = of_match[compat] |
| 476 | |
| 477 | # This needs to be deterministic, since a driver may |
| 478 | # have multiple compatible strings pointing to it. |
| 479 | # We record the one earliest in the alphabet so it |
| 480 | # will produce the same result on all machines. |
| 481 | for compat_id in of_match[compat]: |
| 482 | old = self._compat_to_driver.get(compat_id) |
| 483 | if not old or driver.name < old.name: |
| 484 | self._compat_to_driver[compat_id] = driver |
| 485 | drivers[driver.name] = driver |
| 486 | else: |
| 487 | # The driver does not have a uclass or compat string. |
| 488 | # The first is required but the second is not, so just |
| 489 | # ignore this. |
| 490 | pass |
| 491 | driver = None |
| 492 | ids_name = None |
| 493 | compat = None |
| 494 | compat_dict = {} |
| 495 | |
| 496 | elif ids_name: |
| 497 | compat_m = re_compat.search(line) |
| 498 | if compat_m: |
| 499 | compat_dict[compat_m.group(1)] = compat_m.group(3) |
| 500 | elif '};' in line: |
| 501 | of_match[ids_name] = compat_dict |
| 502 | ids_name = None |
| 503 | elif driver_match: |
| 504 | driver_name = driver_match.group(1) |
| 505 | driver = Driver(driver_name, fname) |
| 506 | else: |
| 507 | ids_m = re_ids.search(line) |
| 508 | if ids_m: |
| 509 | ids_name = ids_m.group(1) |
| 510 | |
| 511 | # Make the updates based on what we found |
| 512 | self._drivers.update(drivers) |
| 513 | self._of_match.update(of_match) |
| 514 | |
Simon Glass | 9065bc9 | 2020-12-28 20:35:06 -0700 | [diff] [blame] | 515 | def scan_driver(self, fname): |
| 516 | """Scan a driver file to build a list of driver names and aliases |
| 517 | |
Simon Glass | 78933b7 | 2021-02-03 06:00:50 -0700 | [diff] [blame] | 518 | It updates the following members: |
| 519 | _drivers - updated with new Driver records for each driver found |
| 520 | in the file |
| 521 | _of_match - updated with each compatible string found in the file |
| 522 | _compat_to_driver - Maps compatible string to Driver |
| 523 | _driver_aliases - Maps alias names to driver name |
Simon Glass | 9065bc9 | 2020-12-28 20:35:06 -0700 | [diff] [blame] | 524 | |
| 525 | Args |
| 526 | fname: Driver filename to scan |
| 527 | """ |
| 528 | with open(fname, encoding='utf-8') as inf: |
| 529 | try: |
| 530 | buff = inf.read() |
| 531 | except UnicodeDecodeError: |
| 532 | # This seems to happen on older Python versions |
| 533 | print("Skipping file '%s' due to unicode error" % fname) |
| 534 | return |
| 535 | |
Simon Glass | 78933b7 | 2021-02-03 06:00:50 -0700 | [diff] [blame] | 536 | # If this file has any U_BOOT_DRIVER() declarations, process it to |
| 537 | # obtain driver information |
| 538 | if 'U_BOOT_DRIVER' in buff: |
| 539 | self._parse_driver(fname, buff) |
Simon Glass | 2e199ab | 2021-02-03 06:00:54 -0700 | [diff] [blame] | 540 | if 'UCLASS_DRIVER' in buff: |
| 541 | self._parse_uclass_driver(fname, buff) |
Simon Glass | 9065bc9 | 2020-12-28 20:35:06 -0700 | [diff] [blame] | 542 | |
| 543 | # The following re will search for driver aliases declared as |
| 544 | # DM_DRIVER_ALIAS(alias, driver_name) |
| 545 | driver_aliases = re.findall( |
| 546 | r'DM_DRIVER_ALIAS\(\s*(\w+)\s*,\s*(\w+)\s*\)', |
| 547 | buff) |
| 548 | |
| 549 | for alias in driver_aliases: # pragma: no cover |
| 550 | if len(alias) != 2: |
| 551 | continue |
| 552 | self._driver_aliases[alias[1]] = alias[0] |
| 553 | |
Simon Glass | 88bd538 | 2021-02-03 06:00:55 -0700 | [diff] [blame] | 554 | def scan_header(self, fname): |
| 555 | """Scan a header file to build a list of struct definitions |
| 556 | |
| 557 | It updates the following members: |
| 558 | _structs - updated with new Struct records for each struct found |
| 559 | in the file |
| 560 | |
| 561 | Args |
| 562 | fname: header filename to scan |
| 563 | """ |
| 564 | with open(fname, encoding='utf-8') as inf: |
| 565 | try: |
| 566 | buff = inf.read() |
| 567 | except UnicodeDecodeError: |
| 568 | # This seems to happen on older Python versions |
| 569 | print("Skipping file '%s' due to unicode error" % fname) |
| 570 | return |
| 571 | |
| 572 | # If this file has any U_BOOT_DRIVER() declarations, process it to |
| 573 | # obtain driver information |
| 574 | if 'struct' in buff: |
| 575 | self._parse_structs(fname, buff) |
| 576 | |
Simon Glass | 9065bc9 | 2020-12-28 20:35:06 -0700 | [diff] [blame] | 577 | def scan_drivers(self): |
| 578 | """Scan the driver folders to build a list of driver names and aliases |
| 579 | |
| 580 | This procedure will populate self._drivers and self._driver_aliases |
| 581 | """ |
| 582 | for (dirpath, _, filenames) in os.walk(self._basedir): |
Simon Glass | dc37c81 | 2021-02-03 06:00:52 -0700 | [diff] [blame] | 583 | rel_path = dirpath[len(self._basedir):] |
| 584 | if rel_path.startswith('/'): |
| 585 | rel_path = rel_path[1:] |
| 586 | if rel_path.startswith('build') or rel_path.startswith('.git'): |
| 587 | continue |
Simon Glass | 9065bc9 | 2020-12-28 20:35:06 -0700 | [diff] [blame] | 588 | for fname in filenames: |
Simon Glass | 88bd538 | 2021-02-03 06:00:55 -0700 | [diff] [blame] | 589 | pathname = dirpath + '/' + fname |
| 590 | if fname.endswith('.c'): |
| 591 | self.scan_driver(pathname) |
| 592 | elif fname.endswith('.h'): |
| 593 | self.scan_header(pathname) |
Simon Glass | 9065bc9 | 2020-12-28 20:35:06 -0700 | [diff] [blame] | 594 | |
| 595 | for fname in self._drivers_additional: |
| 596 | if not isinstance(fname, str) or len(fname) == 0: |
| 597 | continue |
| 598 | if fname[0] == '/': |
| 599 | self.scan_driver(fname) |
| 600 | else: |
| 601 | self.scan_driver(self._basedir + '/' + fname) |
Simon Glass | eb3c249 | 2021-02-03 06:01:01 -0700 | [diff] [blame^] | 602 | |
| 603 | def mark_used(self, nodes): |
| 604 | """Mark the drivers associated with a list of nodes as 'used' |
| 605 | |
| 606 | This takes a list of nodes, finds the driver for each one and marks it |
| 607 | as used. |
| 608 | |
| 609 | Args: |
| 610 | nodes (list of None): Nodes that are in use |
| 611 | """ |
| 612 | # Figure out which drivers we actually use |
| 613 | for node in nodes: |
| 614 | struct_name, _ = self.get_normalized_compat_name(node) |
| 615 | driver = self._drivers.get(struct_name) |
| 616 | if driver: |
| 617 | driver.used = True |