|
35 | 35 |
|
36 | 36 | #include <linux/cacheflush.h> |
37 | 37 | #include <linux/container_of.h> |
| 38 | +#include <linux/crc32.h> |
38 | 39 | #include <linux/dev_printk.h> |
39 | 40 | #include <linux/dma-mapping.h> |
| 41 | +#include <linux/dmi.h> |
40 | 42 | #include <linux/errno.h> |
41 | 43 | #include <linux/firmware.h> |
42 | 44 | #include <linux/gfp_types.h> |
@@ -193,21 +195,117 @@ static int prepare_dma_bufs(struct ishtp_device *dev, |
193 | 195 | return 0; |
194 | 196 | } |
195 | 197 |
|
| 198 | +#define ISH_FW_FILE_VENDOR_NAME_SKU_FMT "intel/ish/ish_%s_%08x_%08x_%08x.bin" |
| 199 | +#define ISH_FW_FILE_VENDOR_SKU_FMT "intel/ish/ish_%s_%08x_%08x.bin" |
| 200 | +#define ISH_FW_FILE_VENDOR_NAME_FMT "intel/ish/ish_%s_%08x_%08x.bin" |
| 201 | +#define ISH_FW_FILE_VENDOR_FMT "intel/ish/ish_%s_%08x.bin" |
196 | 202 | #define ISH_FW_FILE_DEFAULT_FMT "intel/ish/ish_%s.bin" |
197 | 203 |
|
198 | 204 | #define ISH_FW_FILENAME_LEN_MAX 56 |
199 | 205 |
|
| 206 | +#define ISH_CRC_INIT (~0u) |
| 207 | +#define ISH_CRC_XOROUT (~0u) |
| 208 | + |
| 209 | +static int _request_ish_firmware(const struct firmware **firmware_p, |
| 210 | + const char *name, struct device *dev) |
| 211 | +{ |
| 212 | + int ret; |
| 213 | + |
| 214 | + dev_dbg(dev, "Try to load firmware: %s\n", name); |
| 215 | + ret = firmware_request_nowarn(firmware_p, name, dev); |
| 216 | + if (!ret) |
| 217 | + dev_info(dev, "load firmware: %s\n", name); |
| 218 | + |
| 219 | + return ret; |
| 220 | +} |
| 221 | + |
| 222 | +/** |
| 223 | + * request_ish_firmware() - Request and load the ISH firmware. |
| 224 | + * @firmware_p: Pointer to the firmware image. |
| 225 | + * @dev: Device for which firmware is being requested. |
| 226 | + * |
| 227 | + * This function attempts to load the Integrated Sensor Hub (ISH) firmware |
| 228 | + * for the given device in the following order, prioritizing custom firmware |
| 229 | + * with more precise matching patterns: |
| 230 | + * |
| 231 | + * ish_${fw_generation}_${SYS_VENDOR_CRC32}_$(PRODUCT_NAME_CRC32)_${PRODUCT_SKU_CRC32}.bin |
| 232 | + * ish_${fw_generation}_${SYS_VENDOR_CRC32}_${PRODUCT_SKU_CRC32}.bin |
| 233 | + * ish_${fw_generation}_${SYS_VENDOR_CRC32}_$(PRODUCT_NAME_CRC32).bin |
| 234 | + * ish_${fw_generation}_${SYS_VENDOR_CRC32}.bin |
| 235 | + * ish_${fw_generation}.bin |
| 236 | + * |
| 237 | + * The driver will load the first matching firmware and skip the rest. If no |
| 238 | + * matching firmware is found, it will proceed to the next pattern in the |
| 239 | + * specified order. If all searches fail, the default Intel firmware, listed |
| 240 | + * last in the order above, will be loaded. |
| 241 | + * |
| 242 | + * The firmware file name is constructed using CRC32 checksums of strings. |
| 243 | + * This is done to create a valid file name that does not contain spaces |
| 244 | + * or special characters which may be present in the original strings. |
| 245 | + * |
| 246 | + * The CRC-32 algorithm uses the following parameters: |
| 247 | + * Poly: 0x04C11DB7 |
| 248 | + * Init: 0xFFFFFFFF |
| 249 | + * RefIn: true |
| 250 | + * RefOut: true |
| 251 | + * XorOut: 0xFFFFFFFF |
| 252 | + * |
| 253 | + * Return: 0 on success, negative error code on failure. |
| 254 | + */ |
200 | 255 | static int request_ish_firmware(const struct firmware **firmware_p, |
201 | 256 | struct device *dev) |
202 | 257 | { |
| 258 | + const char *gen, *sys_vendor, *product_name, *product_sku; |
203 | 259 | struct ishtp_device *ishtp = dev_get_drvdata(dev); |
204 | | - const char *gen; |
| 260 | + u32 vendor_crc, name_crc, sku_crc; |
205 | 261 | char filename[ISH_FW_FILENAME_LEN_MAX]; |
| 262 | + int ret; |
206 | 263 |
|
207 | 264 | gen = ishtp->driver_data->fw_generation; |
208 | | - snprintf(filename, sizeof(filename), ISH_FW_FILE_DEFAULT_FMT, gen); |
| 265 | + sys_vendor = dmi_get_system_info(DMI_SYS_VENDOR); |
| 266 | + product_name = dmi_get_system_info(DMI_PRODUCT_NAME); |
| 267 | + product_sku = dmi_get_system_info(DMI_PRODUCT_SKU); |
| 268 | + |
| 269 | + if (sys_vendor) |
| 270 | + vendor_crc = crc32(ISH_CRC_INIT, sys_vendor, strlen(sys_vendor)) ^ ISH_CRC_XOROUT; |
| 271 | + if (product_name) |
| 272 | + name_crc = crc32(ISH_CRC_INIT, product_name, strlen(product_name)) ^ ISH_CRC_XOROUT; |
| 273 | + if (product_sku) |
| 274 | + sku_crc = crc32(ISH_CRC_INIT, product_sku, strlen(product_sku)) ^ ISH_CRC_XOROUT; |
| 275 | + |
| 276 | + if (sys_vendor && product_name && product_sku) { |
| 277 | + snprintf(filename, sizeof(filename), ISH_FW_FILE_VENDOR_NAME_SKU_FMT, gen, |
| 278 | + vendor_crc, name_crc, sku_crc); |
| 279 | + ret = _request_ish_firmware(firmware_p, filename, dev); |
| 280 | + if (!ret) |
| 281 | + return 0; |
| 282 | + } |
| 283 | + |
| 284 | + if (sys_vendor && product_sku) { |
| 285 | + snprintf(filename, sizeof(filename), ISH_FW_FILE_VENDOR_SKU_FMT, gen, vendor_crc, |
| 286 | + sku_crc); |
| 287 | + ret = _request_ish_firmware(firmware_p, filename, dev); |
| 288 | + if (!ret) |
| 289 | + return 0; |
| 290 | + } |
209 | 291 |
|
210 | | - return request_firmware(firmware_p, filename, dev); |
| 292 | + if (sys_vendor && product_name) { |
| 293 | + snprintf(filename, sizeof(filename), ISH_FW_FILE_VENDOR_NAME_FMT, gen, vendor_crc, |
| 294 | + name_crc); |
| 295 | + ret = _request_ish_firmware(firmware_p, filename, dev); |
| 296 | + if (!ret) |
| 297 | + return 0; |
| 298 | + } |
| 299 | + |
| 300 | + if (sys_vendor) { |
| 301 | + snprintf(filename, sizeof(filename), ISH_FW_FILE_VENDOR_FMT, gen, vendor_crc); |
| 302 | + ret = _request_ish_firmware(firmware_p, filename, dev); |
| 303 | + if (!ret) |
| 304 | + return 0; |
| 305 | + } |
| 306 | + |
| 307 | + snprintf(filename, sizeof(filename), ISH_FW_FILE_DEFAULT_FMT, gen); |
| 308 | + return _request_ish_firmware(firmware_p, filename, dev); |
211 | 309 | } |
212 | 310 |
|
213 | 311 | /** |
|
0 commit comments