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