firmware_sample_driver.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * firmware_sample_driver.c -
  3. *
  4. * Copyright (c) 2003 Manuel Estrada Sainz
  5. *
  6. * Sample code on how to use request_firmware() from drivers.
  7. *
  8. */
  9. #include <linux/module.h>
  10. #include <linux/kernel.h>
  11. #include <linux/init.h>
  12. #include <linux/device.h>
  13. #include <linux/string.h>
  14. #include "linux/firmware.h"
  15. static struct device ghost_device = {
  16. .bus_id = "ghost0",
  17. };
  18. static void sample_firmware_load(char *firmware, int size)
  19. {
  20. u8 buf[size+1];
  21. memcpy(buf, firmware, size);
  22. buf[size] = '\0';
  23. printk(KERN_INFO "firmware_sample_driver: firmware: %s\n", buf);
  24. }
  25. static void sample_probe_default(void)
  26. {
  27. /* uses the default method to get the firmware */
  28. const struct firmware *fw_entry;
  29. printk(KERN_INFO "firmware_sample_driver: a ghost device got inserted :)\n");
  30. if (request_firmware(&fw_entry, "sample_driver_fw", &ghost_device)!=0) {
  31. printk(KERN_ERR
  32. "firmware_sample_driver: Firmware not available\n");
  33. return;
  34. }
  35. sample_firmware_load(fw_entry->data, fw_entry->size);
  36. release_firmware(fw_entry);
  37. /* finish setting up the device */
  38. }
  39. static void sample_probe_specific(void)
  40. {
  41. /* Uses some specific hotplug support to get the firmware from
  42. * userspace directly into the hardware, or via some sysfs file */
  43. /* NOTE: This currently doesn't work */
  44. printk(KERN_INFO "firmware_sample_driver: a ghost device got inserted :)\n");
  45. if (request_firmware(NULL, "sample_driver_fw", &ghost_device)!=0) {
  46. printk(KERN_ERR
  47. "firmware_sample_driver: Firmware load failed\n");
  48. return;
  49. }
  50. /* request_firmware blocks until userspace finished, so at
  51. * this point the firmware should be already in the device */
  52. /* finish setting up the device */
  53. }
  54. static void sample_probe_async_cont(const struct firmware *fw, void *context)
  55. {
  56. if (!fw) {
  57. printk(KERN_ERR
  58. "firmware_sample_driver: firmware load failed\n");
  59. return;
  60. }
  61. printk(KERN_INFO "firmware_sample_driver: device pointer \"%s\"\n",
  62. (char *)context);
  63. sample_firmware_load(fw->data, fw->size);
  64. }
  65. static void sample_probe_async(void)
  66. {
  67. /* Let's say that I can't sleep */
  68. int error;
  69. error = request_firmware_nowait (THIS_MODULE, FW_ACTION_NOHOTPLUG,
  70. "sample_driver_fw", &ghost_device,
  71. "my device pointer",
  72. sample_probe_async_cont);
  73. if (error) {
  74. printk(KERN_ERR "firmware_sample_driver:"
  75. " request_firmware_nowait failed\n");
  76. }
  77. }
  78. static int sample_init(void)
  79. {
  80. device_initialize(&ghost_device);
  81. /* since there is no real hardware insertion I just call the
  82. * sample probe functions here */
  83. sample_probe_specific();
  84. sample_probe_default();
  85. sample_probe_async();
  86. return 0;
  87. }
  88. static void __exit sample_exit(void)
  89. {
  90. }
  91. module_init (sample_init);
  92. module_exit (sample_exit);
  93. MODULE_LICENSE("GPL");