firmware_sample_driver.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. {
  32. printk(KERN_ERR
  33. "firmware_sample_driver: Firmware not available\n");
  34. return;
  35. }
  36. sample_firmware_load(fw_entry->data, fw_entry->size);
  37. release_firmware(fw_entry);
  38. /* finish setting up the device */
  39. }
  40. static void sample_probe_specific(void)
  41. {
  42. /* Uses some specific hotplug support to get the firmware from
  43. * userspace directly into the hardware, or via some sysfs file */
  44. /* NOTE: This currently doesn't work */
  45. printk(KERN_INFO "firmware_sample_driver: a ghost device got inserted :)\n");
  46. if(request_firmware(NULL, "sample_driver_fw", &ghost_device)!=0)
  47. {
  48. printk(KERN_ERR
  49. "firmware_sample_driver: Firmware load failed\n");
  50. return;
  51. }
  52. /* request_firmware blocks until userspace finished, so at
  53. * this point the firmware should be already in the device */
  54. /* finish setting up the device */
  55. }
  56. static void sample_probe_async_cont(const struct firmware *fw, void *context)
  57. {
  58. if(!fw){
  59. printk(KERN_ERR
  60. "firmware_sample_driver: firmware load failed\n");
  61. return;
  62. }
  63. printk(KERN_INFO "firmware_sample_driver: device pointer \"%s\"\n",
  64. (char *)context);
  65. sample_firmware_load(fw->data, fw->size);
  66. }
  67. static void sample_probe_async(void)
  68. {
  69. /* Let's say that I can't sleep */
  70. int error;
  71. error = request_firmware_nowait (THIS_MODULE, FW_ACTION_NOHOTPLUG,
  72. "sample_driver_fw", &ghost_device,
  73. "my device pointer",
  74. sample_probe_async_cont);
  75. if(error){
  76. printk(KERN_ERR
  77. "firmware_sample_driver:"
  78. " request_firmware_nowait failed\n");
  79. }
  80. }
  81. static int sample_init(void)
  82. {
  83. device_initialize(&ghost_device);
  84. /* since there is no real hardware insertion I just call the
  85. * sample probe functions here */
  86. sample_probe_specific();
  87. sample_probe_default();
  88. sample_probe_async();
  89. return 0;
  90. }
  91. static void __exit sample_exit(void)
  92. {
  93. }
  94. module_init (sample_init);
  95. module_exit (sample_exit);
  96. MODULE_LICENSE("GPL");