firmware_sample_driver.c 2.8 KB

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