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