fw_upload.rst 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. .. SPDX-License-Identifier: GPL-2.0
  2. ===================
  3. Firmware Upload API
  4. ===================
  5. A device driver that registers with the firmware loader will expose
  6. persistent sysfs nodes to enable users to initiate firmware updates for
  7. that device. It is the responsibility of the device driver and/or the
  8. device itself to perform any validation on the data received. Firmware
  9. upload uses the same *loading* and *data* sysfs files described in the
  10. documentation for firmware fallback. It also adds additional sysfs files
  11. to provide status on the transfer of the firmware image to the device.
  12. Register for firmware upload
  13. ============================
  14. A device driver registers for firmware upload by calling
  15. firmware_upload_register(). Among the parameter list is a name to
  16. identify the device under /sys/class/firmware. A user may initiate a
  17. firmware upload by echoing a 1 to the *loading* sysfs file for the target
  18. device. Next, the user writes the firmware image to the *data* sysfs
  19. file. After writing the firmware data, the user echos 0 to the *loading*
  20. sysfs file to signal completion. Echoing 0 to *loading* also triggers the
  21. transfer of the firmware to the lower-lever device driver in the context
  22. of a kernel worker thread.
  23. To use the firmware upload API, write a driver that implements a set of
  24. ops. The probe function calls firmware_upload_register() and the remove
  25. function calls firmware_upload_unregister() such as::
  26. static const struct fw_upload_ops m10bmc_ops = {
  27. .prepare = m10bmc_sec_prepare,
  28. .write = m10bmc_sec_write,
  29. .poll_complete = m10bmc_sec_poll_complete,
  30. .cancel = m10bmc_sec_cancel,
  31. .cleanup = m10bmc_sec_cleanup,
  32. };
  33. static int m10bmc_sec_probe(struct platform_device *pdev)
  34. {
  35. const char *fw_name, *truncate;
  36. struct m10bmc_sec *sec;
  37. struct fw_upload *fwl;
  38. unsigned int len;
  39. sec = devm_kzalloc(&pdev->dev, sizeof(*sec), GFP_KERNEL);
  40. if (!sec)
  41. return -ENOMEM;
  42. sec->dev = &pdev->dev;
  43. sec->m10bmc = dev_get_drvdata(pdev->dev.parent);
  44. dev_set_drvdata(&pdev->dev, sec);
  45. fw_name = dev_name(sec->dev);
  46. truncate = strstr(fw_name, ".auto");
  47. len = (truncate) ? truncate - fw_name : strlen(fw_name);
  48. sec->fw_name = kmemdup_nul(fw_name, len, GFP_KERNEL);
  49. fwl = firmware_upload_register(THIS_MODULE, sec->dev, sec->fw_name,
  50. &m10bmc_ops, sec);
  51. if (IS_ERR(fwl)) {
  52. dev_err(sec->dev, "Firmware Upload driver failed to start\n");
  53. kfree(sec->fw_name);
  54. return PTR_ERR(fwl);
  55. }
  56. sec->fwl = fwl;
  57. return 0;
  58. }
  59. static int m10bmc_sec_remove(struct platform_device *pdev)
  60. {
  61. struct m10bmc_sec *sec = dev_get_drvdata(&pdev->dev);
  62. firmware_upload_unregister(sec->fwl);
  63. kfree(sec->fw_name);
  64. return 0;
  65. }
  66. firmware_upload_register
  67. ------------------------
  68. .. kernel-doc:: drivers/base/firmware_loader/sysfs_upload.c
  69. :identifiers: firmware_upload_register
  70. firmware_upload_unregister
  71. --------------------------
  72. .. kernel-doc:: drivers/base/firmware_loader/sysfs_upload.c
  73. :identifiers: firmware_upload_unregister
  74. Firmware Upload Ops
  75. -------------------
  76. .. kernel-doc:: include/linux/firmware.h
  77. :identifiers: fw_upload_ops
  78. Firmware Upload Progress Codes
  79. ------------------------------
  80. The following progress codes are used internally by the firmware loader.
  81. Corresponding strings are reported through the status sysfs node that
  82. is described below and are documented in the ABI documentation.
  83. .. kernel-doc:: drivers/base/firmware_loader/sysfs_upload.h
  84. :identifiers: fw_upload_prog
  85. Firmware Upload Error Codes
  86. ---------------------------
  87. The following error codes may be returned by the driver ops in case of
  88. failure:
  89. .. kernel-doc:: include/linux/firmware.h
  90. :identifiers: fw_upload_err
  91. Sysfs Attributes
  92. ================
  93. In addition to the *loading* and *data* sysfs files, there are additional
  94. sysfs files to monitor the status of the data transfer to the target
  95. device and to determine the final pass/fail status of the transfer.
  96. Depending on the device and the size of the firmware image, a firmware
  97. update could take milliseconds or minutes.
  98. The additional sysfs files are:
  99. * status - provides an indication of the progress of a firmware update
  100. * error - provides error information for a failed firmware update
  101. * remaining_size - tracks the data transfer portion of an update
  102. * cancel - echo 1 to this file to cancel the update