fw_upload.rst 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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(sec->dev, sec->fw_name, &m10bmc_ops, sec);
  50. if (IS_ERR(fwl)) {
  51. dev_err(sec->dev, "Firmware Upload driver failed to start\n");
  52. kfree(sec->fw_name);
  53. return PTR_ERR(fwl);
  54. }
  55. sec->fwl = fwl;
  56. return 0;
  57. }
  58. static int m10bmc_sec_remove(struct platform_device *pdev)
  59. {
  60. struct m10bmc_sec *sec = dev_get_drvdata(&pdev->dev);
  61. firmware_upload_unregister(sec->fwl);
  62. kfree(sec->fw_name);
  63. return 0;
  64. }
  65. firmware_upload_register
  66. ------------------------
  67. .. kernel-doc:: drivers/base/firmware_loader/sysfs_upload.c
  68. :identifiers: firmware_upload_register
  69. firmware_upload_unregister
  70. --------------------------
  71. .. kernel-doc:: drivers/base/firmware_loader/sysfs_upload.c
  72. :identifiers: firmware_upload_unregister
  73. Firmware Upload Ops
  74. -------------------
  75. .. kernel-doc:: include/linux/firmware.h
  76. :identifiers: fw_upload_ops
  77. Firmware Upload Progress Codes
  78. ------------------------------
  79. The following progress codes are used internally by the firmware loader.
  80. Corresponding strings are reported through the status sysfs node that
  81. is described below and are documented in the ABI documentation.
  82. .. kernel-doc:: drivers/base/firmware_loader/sysfs_upload.h
  83. :identifiers: fw_upload_prog
  84. Firmware Upload Error Codes
  85. ---------------------------
  86. The following error codes may be returned by the driver ops in case of
  87. failure:
  88. .. kernel-doc:: include/linux/firmware.h
  89. :identifiers: fw_upload_err
  90. Sysfs Attributes
  91. ================
  92. In addition to the *loading* and *data* sysfs files, there are additional
  93. sysfs files to monitor the status of the data transfer to the target
  94. device and to determine the final pass/fail status of the transfer.
  95. Depending on the device and the size of the firmware image, a firmware
  96. update could take milliseconds or minutes.
  97. The additional sysfs files are:
  98. * status - provides an indication of the progress of a firmware update
  99. * error - provides error information for a failed firmware update
  100. * remaining_size - tracks the data transfer portion of an update
  101. * cancel - echo 1 to this file to cancel the update