|
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> |
43 | 45 | #include <linux/math.h> |
44 | 46 | #include <linux/module.h> |
45 | 47 | #include <linux/pfn.h> |
| 48 | +#include <linux/sprintf.h> |
46 | 49 | #include <linux/string.h> |
47 | 50 | #include <linux/types.h> |
48 | 51 | #include <linux/wait.h> |
@@ -192,6 +195,119 @@ static int prepare_dma_bufs(struct ishtp_device *dev, |
192 | 195 | return 0; |
193 | 196 | } |
194 | 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" |
| 202 | +#define ISH_FW_FILE_DEFAULT_FMT "intel/ish/ish_%s.bin" |
| 203 | + |
| 204 | +#define ISH_FW_FILENAME_LEN_MAX 56 |
| 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 | + */ |
| 255 | +static int request_ish_firmware(const struct firmware **firmware_p, |
| 256 | + struct device *dev) |
| 257 | +{ |
| 258 | + const char *gen, *sys_vendor, *product_name, *product_sku; |
| 259 | + struct ishtp_device *ishtp = dev_get_drvdata(dev); |
| 260 | + u32 vendor_crc, name_crc, sku_crc; |
| 261 | + char filename[ISH_FW_FILENAME_LEN_MAX]; |
| 262 | + int ret; |
| 263 | + |
| 264 | + gen = ishtp->driver_data->fw_generation; |
| 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 | + } |
| 291 | + |
| 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); |
| 309 | +} |
| 310 | + |
195 | 311 | /** |
196 | 312 | * ishtp_loader_work() - Load the ISHTP firmware |
197 | 313 | * @work: The work structure |
@@ -220,17 +336,16 @@ void ishtp_loader_work(struct work_struct *work) |
220 | 336 | struct loader_xfer_query query = { .header = cpu_to_le32(query_hdr.val32), }; |
221 | 337 | struct loader_start start = { .header = cpu_to_le32(start_hdr.val32), }; |
222 | 338 | union loader_recv_message recv_msg; |
223 | | - char *filename = dev->driver_data->fw_filename; |
224 | 339 | const struct firmware *ish_fw; |
225 | 340 | void *dma_bufs[FRAGMENT_MAX_NUM] = {}; |
226 | 341 | u32 fragment_size; |
227 | 342 | u32 fragment_count; |
228 | 343 | int retry = ISHTP_LOADER_RETRY_TIMES; |
229 | 344 | int rv; |
230 | 345 |
|
231 | | - rv = request_firmware(&ish_fw, filename, dev->devc); |
| 346 | + rv = request_ish_firmware(&ish_fw, dev->devc); |
232 | 347 | if (rv < 0) { |
233 | | - dev_err(dev->devc, "request firmware %s failed:%d\n", filename, rv); |
| 348 | + dev_err(dev->devc, "request ISH firmware failed:%d\n", rv); |
234 | 349 | return; |
235 | 350 | } |
236 | 351 |
|
|
0 commit comments