firmware_sample_driver.c 3.1 KB

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