firmware_sample_driver.c 3.1 KB

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