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