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