dtoc: Scan drivers for available information

At present we simply record the name of a driver parsed from its
implementation file. We also need to get the uclass and a few other
things so we can instantiate devices at build time. Add support for
collecting this information. This requires parsing each driver file.

Signed-off-by: Simon Glass <sjg@chromium.org>
This commit is contained in:
Simon Glass 2021-02-03 06:00:50 -07:00
parent 2a38e71265
commit c58662fc65
2 changed files with 311 additions and 14 deletions

View File

@ -54,15 +54,30 @@ class Driver:
Attributes:
name: Name of driver. For U_BOOT_DRIVER(x) this is 'x'
fname: Filename where the driver was found
uclass_id: Name of uclass, e.g. 'UCLASS_I2C'
compat: Driver data for each compatible string:
key: Compatible string, e.g. 'rockchip,rk3288-grf'
value: Driver data, e,g, 'ROCKCHIP_SYSCON_GRF', or None
fname: Filename where the driver was found
priv (str): struct name of the priv_auto member, e.g. 'serial_priv'
"""
def __init__(self, name):
def __init__(self, name, fname):
self.name = name
self.fname = fname
self.uclass_id = None
self.compat = None
self.priv = ''
def __eq__(self, other):
return self.name == other.name
return (self.name == other.name and
self.uclass_id == other.uclass_id and
self.compat == other.compat and
self.priv == other.priv)
def __repr__(self):
return "Driver(name='%s')" % self.name
return ("Driver(name='%s', uclass_id='%s', compat=%s, priv=%s)" %
(self.name, self.uclass_id, self.compat, self.priv))
class Scanner:
@ -81,6 +96,12 @@ class Scanner:
_warning_disabled: true to disable warnings about driver names not found
_drivers_additional (list or str): List of additional drivers to use
during scanning
_of_match: Dict holding information about compatible strings
key: Name of struct udevice_id variable
value: Dict of compatible info in that variable:
key: Compatible string, e.g. 'rockchip,rk3288-grf'
value: Driver data, e,g, 'ROCKCHIP_SYSCON_GRF', or None
_compat_to_driver: Maps compatible strings to Driver
"""
def __init__(self, basedir, warning_disabled, drivers_additional):
"""Set up a new Scanner
@ -94,6 +115,8 @@ class Scanner:
self._driver_aliases = {}
self._drivers_additional = drivers_additional or []
self._warning_disabled = warning_disabled
self._of_match = {}
self._compat_to_driver = {}
def get_normalized_compat_name(self, node):
"""Get a node's normalized compat name
@ -131,10 +154,163 @@ class Scanner:
return compat_list_c[0], compat_list_c[1:]
@classmethod
def _get_re_for_member(cls, member):
"""_get_re_for_member: Get a compiled regular expression
Args:
member (str): Struct member name, e.g. 'priv_auto'
Returns:
re.Pattern: Compiled regular expression that parses:
.member = sizeof(struct fred),
and returns "fred" as group 1
"""
return re.compile(r'^\s*.%s\s*=\s*sizeof\(struct\s+(.*)\),$' % member)
def _parse_driver(self, fname, buff):
"""Parse a C file to extract driver information contained within
This parses U_BOOT_DRIVER() structs to obtain various pieces of useful
information.
It updates the following members:
_drivers - updated with new Driver records for each driver found
in the file
_of_match - updated with each compatible string found in the file
_compat_to_driver - Maps compatible string to Driver
Args:
fname (str): Filename being parsed (used for warnings)
buff (str): Contents of file
Raises:
ValueError: Compatible variable is mentioned in .of_match in
U_BOOT_DRIVER() but not found in the file
"""
# Dict holding information about compatible strings collected in this
# function so far
# key: Name of struct udevice_id variable
# value: Dict of compatible info in that variable:
# key: Compatible string, e.g. 'rockchip,rk3288-grf'
# value: Driver data, e,g, 'ROCKCHIP_SYSCON_GRF', or None
of_match = {}
# Dict holding driver information collected in this function so far
# key: Driver name (C name as in U_BOOT_DRIVER(xxx))
# value: Driver
drivers = {}
# Collect the driver info
driver = None
re_driver = re.compile(r'U_BOOT_DRIVER\((.*)\)')
# Collect the uclass ID, e.g. 'UCLASS_SPI'
re_id = re.compile(r'\s*\.id\s*=\s*(UCLASS_[A-Z0-9_]+)')
# Collect the compatible string, e.g. 'rockchip,rk3288-grf'
compat = None
re_compat = re.compile(r'{\s*.compatible\s*=\s*"(.*)"\s*'
r'(,\s*.data\s*=\s*(\S*))?\s*},')
# This is a dict of compatible strings that were found:
# key: Compatible string, e.g. 'rockchip,rk3288-grf'
# value: Driver data, e,g, 'ROCKCHIP_SYSCON_GRF', or None
compat_dict = {}
# Holds the var nane of the udevice_id list, e.g.
# 'rk3288_syscon_ids_noc' in
# static const struct udevice_id rk3288_syscon_ids_noc[] = {
ids_name = None
re_ids = re.compile(r'struct udevice_id (.*)\[\]\s*=')
# Matches the references to the udevice_id list
re_of_match = re.compile(
r'\.of_match\s*=\s*(of_match_ptr\()?([a-z0-9_]+)(\))?,')
# Matches the struct name for priv
re_priv = self._get_re_for_member('priv_auto')
prefix = ''
for line in buff.splitlines():
# Handle line continuation
if prefix:
line = prefix + line
prefix = ''
if line.endswith('\\'):
prefix = line[:-1]
continue
driver_match = re_driver.search(line)
# If this line contains U_BOOT_DRIVER()...
if driver:
m_id = re_id.search(line)
m_of_match = re_of_match.search(line)
m_priv = re_priv.match(line)
if m_priv:
driver.priv = m_priv.group(1)
elif m_id:
driver.uclass_id = m_id.group(1)
elif m_of_match:
compat = m_of_match.group(2)
elif '};' in line:
if driver.uclass_id and compat:
if compat not in of_match:
raise ValueError(
"%s: Unknown compatible var '%s' (found: %s)" %
(fname, compat, ','.join(of_match.keys())))
driver.compat = of_match[compat]
# This needs to be deterministic, since a driver may
# have multiple compatible strings pointing to it.
# We record the one earliest in the alphabet so it
# will produce the same result on all machines.
for compat_id in of_match[compat]:
old = self._compat_to_driver.get(compat_id)
if not old or driver.name < old.name:
self._compat_to_driver[compat_id] = driver
drivers[driver.name] = driver
else:
# The driver does not have a uclass or compat string.
# The first is required but the second is not, so just
# ignore this.
pass
driver = None
ids_name = None
compat = None
compat_dict = {}
elif ids_name:
compat_m = re_compat.search(line)
if compat_m:
compat_dict[compat_m.group(1)] = compat_m.group(3)
elif '};' in line:
of_match[ids_name] = compat_dict
ids_name = None
elif driver_match:
driver_name = driver_match.group(1)
driver = Driver(driver_name, fname)
else:
ids_m = re_ids.search(line)
if ids_m:
ids_name = ids_m.group(1)
# Make the updates based on what we found
self._drivers.update(drivers)
self._of_match.update(of_match)
def scan_driver(self, fname):
"""Scan a driver file to build a list of driver names and aliases
This procedure will populate self._drivers and self._driver_aliases
It updates the following members:
_drivers - updated with new Driver records for each driver found
in the file
_of_match - updated with each compatible string found in the file
_compat_to_driver - Maps compatible string to Driver
_driver_aliases - Maps alias names to driver name
Args
fname: Driver filename to scan
@ -147,12 +323,10 @@ class Scanner:
print("Skipping file '%s' due to unicode error" % fname)
return
# The following re will search for driver names declared as
# U_BOOT_DRIVER(driver_name)
drivers = re.findall(r'U_BOOT_DRIVER\((.*)\)', buff)
for driver in drivers:
self._drivers[driver] = Driver(driver)
# If this file has any U_BOOT_DRIVER() declarations, process it to
# obtain driver information
if 'U_BOOT_DRIVER' in buff:
self._parse_driver(fname, buff)
# The following re will search for driver aliases declared as
# DM_DRIVER_ALIAS(alias, driver_name)

View File

@ -17,6 +17,20 @@ from dtoc import src_scan
from patman import test_util
from patman import tools
OUR_PATH = os.path.dirname(os.path.realpath(__file__))
class FakeNode:
"""Fake Node object for testing"""
def __init__(self):
self.name = None
self.props = {}
class FakeProp:
"""Fake Prop object for testing"""
def __init__(self):
self.name = None
self.value = None
# This is a test so is allowed to access private things in the module it is
# testing
# pylint: disable=W0212
@ -69,10 +83,22 @@ class TestSrcScan(unittest.TestCase):
def test_driver(self):
"""Test the Driver class"""
drv1 = src_scan.Driver('fred')
drv2 = src_scan.Driver('mary')
drv3 = src_scan.Driver('fred')
self.assertEqual("Driver(name='fred')", str(drv1))
i2c = 'I2C_UCLASS'
compat = {'rockchip,rk3288-grf': 'ROCKCHIP_SYSCON_GRF',
'rockchip,rk3288-srf': None}
drv1 = src_scan.Driver('fred', 'fred.c')
drv2 = src_scan.Driver('mary', 'mary.c')
drv3 = src_scan.Driver('fred', 'fred.c')
drv1.uclass_id = i2c
drv1.compat = compat
drv2.uclass_id = i2c
drv2.compat = compat
drv3.uclass_id = i2c
drv3.compat = compat
self.assertEqual(
"Driver(name='fred', uclass_id='I2C_UCLASS', "
"compat={'rockchip,rk3288-grf': 'ROCKCHIP_SYSCON_GRF', "
"'rockchip,rk3288-srf': None}, priv=)", str(drv1))
self.assertEqual(drv1, drv3)
self.assertNotEqual(drv1, drv2)
self.assertNotEqual(drv2, drv3)
@ -105,3 +131,100 @@ class TestSrcScan(unittest.TestCase):
mocked.mock_calls[1])
finally:
shutil.rmtree(indir)
def test_scan(self):
"""Test scanning of a driver"""
fname = os.path.join(OUR_PATH, '..', '..', 'drivers/i2c/tegra_i2c.c')
buff = tools.ReadFile(fname, False)
scan = src_scan.Scanner(None, False, None)
scan._parse_driver(fname, buff)
self.assertIn('i2c_tegra', scan._drivers)
drv = scan._drivers['i2c_tegra']
self.assertEqual('i2c_tegra', drv.name)
self.assertEqual('UCLASS_I2C', drv.uclass_id)
self.assertEqual(
{'nvidia,tegra114-i2c': 'TYPE_114',
'nvidia,tegra20-i2c': 'TYPE_STD',
'nvidia,tegra20-i2c-dvc': 'TYPE_DVC'}, drv.compat)
self.assertEqual('i2c_bus', drv.priv)
self.assertEqual(1, len(scan._drivers))
def test_normalized_name(self):
"""Test operation of get_normalized_compat_name()"""
prop = FakeProp()
prop.name = 'compatible'
prop.value = 'rockchip,rk3288-grf'
node = FakeNode()
node.props = {'compatible': prop}
scan = src_scan.Scanner(None, False, None)
with test_util.capture_sys_output() as (stdout, _):
name, aliases = scan.get_normalized_compat_name(node)
self.assertEqual('rockchip_rk3288_grf', name)
self.assertEqual([], aliases)
self.assertEqual(
'WARNING: the driver rockchip_rk3288_grf was not found in the driver list',
stdout.getvalue().strip())
i2c = 'I2C_UCLASS'
compat = {'rockchip,rk3288-grf': 'ROCKCHIP_SYSCON_GRF',
'rockchip,rk3288-srf': None}
drv = src_scan.Driver('fred', 'fred.c')
drv.uclass_id = i2c
drv.compat = compat
scan._drivers['rockchip_rk3288_grf'] = drv
scan._driver_aliases['rockchip_rk3288_srf'] = 'rockchip_rk3288_grf'
with test_util.capture_sys_output() as (stdout, _):
name, aliases = scan.get_normalized_compat_name(node)
self.assertEqual('', stdout.getvalue().strip())
self.assertEqual('rockchip_rk3288_grf', name)
self.assertEqual([], aliases)
prop.value = 'rockchip,rk3288-srf'
with test_util.capture_sys_output() as (stdout, _):
name, aliases = scan.get_normalized_compat_name(node)
self.assertEqual('', stdout.getvalue().strip())
self.assertEqual('rockchip_rk3288_grf', name)
self.assertEqual(['rockchip_rk3288_srf'], aliases)
def test_scan_errors(self):
"""Test detection of scanning errors"""
buff = '''
static const struct udevice_id tegra_i2c_ids2[] = {
{ .compatible = "nvidia,tegra114-i2c", .data = TYPE_114 },
{ }
};
U_BOOT_DRIVER(i2c_tegra) = {
.name = "i2c_tegra",
.id = UCLASS_I2C,
.of_match = tegra_i2c_ids,
};
'''
scan = src_scan.Scanner(None, False, None)
with self.assertRaises(ValueError) as exc:
scan._parse_driver('file.c', buff)
self.assertIn(
"file.c: Unknown compatible var 'tegra_i2c_ids' (found: tegra_i2c_ids2)",
str(exc.exception))
def test_of_match(self):
"""Test detection of of_match_ptr() member"""
buff = '''
static const struct udevice_id tegra_i2c_ids[] = {
{ .compatible = "nvidia,tegra114-i2c", .data = TYPE_114 },
{ }
};
U_BOOT_DRIVER(i2c_tegra) = {
.name = "i2c_tegra",
.id = UCLASS_I2C,
.of_match = of_match_ptr(tegra_i2c_ids),
};
'''
scan = src_scan.Scanner(None, False, None)
scan._parse_driver('file.c', buff)
self.assertIn('i2c_tegra', scan._drivers)
drv = scan._drivers['i2c_tegra']
self.assertEqual('i2c_tegra', drv.name)