Skip to content

Commit afe2dab

Browse files
Nathaniel McCallumgregkh
authored andcommitted
USB: add hex/bcd detection to usb modalias generation
The current code to generate usb modaliases from usb_device_id assumes that the device's bcdDevice descriptor will actually be in BCD format. While this should be a sane assumption, some devices don't follow spec and just use plain old hex. This causes drivers for these devices to generate invalid modalias lines which will never actually match for the hardware. The following patch adds hex support for bcdDevice in file2alias.c by detecting when a driver uses a hex formatted bcdDevice_(lo|hi) and adjusts the output to hex format accordingly. Drivers for devices which have bcdDevice conforming to BCD will have no change in modalias output. Drivers for devices which don't conform (i.e. ibmcam) should now generate valid modaliases. EXAMPLE OUTPUT (ibmcam; space added to highlight change) Old: usb:v0545p800D d030[10-9] dc*dsc*dp*ic*isc*ip* New: usb:v0545p800D d030a dc*dsc*dp*ic*isc*ip* Signed-off-by: Nathaniel McCallum <nathaniel@natemccallum.com> Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
1 parent c1479a9 commit afe2dab

1 file changed

Lines changed: 36 additions & 12 deletions

File tree

‎scripts/mod/file2alias.c‎

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ static void device_id_check(const char *modname, const char *device_id,
104104
static void do_usb_entry(struct usb_device_id *id,
105105
unsigned int bcdDevice_initial, int bcdDevice_initial_digits,
106106
unsigned char range_lo, unsigned char range_hi,
107-
struct module *mod)
107+
unsigned char max, struct module *mod)
108108
{
109109
char alias[500];
110110
strcpy(alias, "usb:");
@@ -118,9 +118,22 @@ static void do_usb_entry(struct usb_device_id *id,
118118
sprintf(alias + strlen(alias), "%0*X",
119119
bcdDevice_initial_digits, bcdDevice_initial);
120120
if (range_lo == range_hi)
121-
sprintf(alias + strlen(alias), "%u", range_lo);
122-
else if (range_lo > 0 || range_hi < 9)
123-
sprintf(alias + strlen(alias), "[%u-%u]", range_lo, range_hi);
121+
sprintf(alias + strlen(alias), "%X", range_lo);
122+
else if (range_lo > 0 || range_hi < max) {
123+
if (range_lo > 0x9 || range_hi < 0xA)
124+
sprintf(alias + strlen(alias),
125+
"[%X-%X]",
126+
range_lo,
127+
range_hi);
128+
else {
129+
sprintf(alias + strlen(alias),
130+
range_lo < 0x9 ? "[%X-9" : "[%X",
131+
range_lo);
132+
sprintf(alias + strlen(alias),
133+
range_hi > 0xA ? "a-%X]" : "%X]",
134+
range_lo);
135+
}
136+
}
124137
if (bcdDevice_initial_digits < (sizeof(id->bcdDevice_lo) * 2 - 1))
125138
strcat(alias, "*");
126139

@@ -150,7 +163,7 @@ static void do_usb_entry(struct usb_device_id *id,
150163
static void do_usb_entry_multi(struct usb_device_id *id, struct module *mod)
151164
{
152165
unsigned int devlo, devhi;
153-
unsigned char chi, clo;
166+
unsigned char chi, clo, max;
154167
int ndigits;
155168

156169
id->match_flags = TO_NATIVE(id->match_flags);
@@ -162,6 +175,17 @@ static void do_usb_entry_multi(struct usb_device_id *id, struct module *mod)
162175
devhi = id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI ?
163176
TO_NATIVE(id->bcdDevice_hi) : ~0x0U;
164177

178+
/* Figure out if this entry is in bcd or hex format */
179+
max = 0x9; /* Default to decimal format */
180+
for (ndigits = 0 ; ndigits < sizeof(id->bcdDevice_lo) * 2 ; ndigits++) {
181+
clo = (devlo >> (ndigits << 2)) & 0xf;
182+
chi = ((devhi > 0x9999 ? 0x9999 : devhi) >> (ndigits << 2)) & 0xf;
183+
if (clo > max || chi > max) {
184+
max = 0xf;
185+
break;
186+
}
187+
}
188+
165189
/*
166190
* Some modules (visor) have empty slots as placeholder for
167191
* run-time specification that results in catch-all alias
@@ -173,21 +197,21 @@ static void do_usb_entry_multi(struct usb_device_id *id, struct module *mod)
173197
for (ndigits = sizeof(id->bcdDevice_lo) * 2 - 1; devlo <= devhi; ndigits--) {
174198
clo = devlo & 0xf;
175199
chi = devhi & 0xf;
176-
if (chi > 9) /* it's bcd not hex */
177-
chi = 9;
200+
if (chi > max) /* If we are in bcd mode, truncate if necessary */
201+
chi = max;
178202
devlo >>= 4;
179203
devhi >>= 4;
180204

181205
if (devlo == devhi || !ndigits) {
182-
do_usb_entry(id, devlo, ndigits, clo, chi, mod);
206+
do_usb_entry(id, devlo, ndigits, clo, chi, max, mod);
183207
break;
184208
}
185209

186-
if (clo > 0)
187-
do_usb_entry(id, devlo++, ndigits, clo, 9, mod);
210+
if (clo > 0x0)
211+
do_usb_entry(id, devlo++, ndigits, clo, max, max, mod);
188212

189-
if (chi < 9)
190-
do_usb_entry(id, devhi--, ndigits, 0, chi, mod);
213+
if (chi < max)
214+
do_usb_entry(id, devhi--, ndigits, 0x0, chi, max, mod);
191215
}
192216
}
193217

0 commit comments

Comments
 (0)