blob: 11114f389c4958213f9c8e9a8b08209204885f4e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * firmware_sample_driver.c -
3 *
Markus Rechberger87d37a42007-06-04 18:45:44 +02004 * Copyright (c) 2003 Manuel Estrada Sainz
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * Sample code on how to use request_firmware() from drivers.
7 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9
10#include <linux/module.h>
11#include <linux/kernel.h>
12#include <linux/init.h>
13#include <linux/device.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080014#include <linux/string.h>
Greg Kroah-Hartman4b65fc82008-02-20 16:07:07 -080015#include <linux/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
Linus Torvalds1da177e2005-04-16 15:20:36 -070017static struct device ghost_device = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 .bus_id = "ghost0",
19};
20
21
22static void sample_firmware_load(char *firmware, int size)
23{
24 u8 buf[size+1];
25 memcpy(buf, firmware, size);
26 buf[size] = '\0';
Christophe Lucas20dd0262005-09-08 08:55:53 +020027 printk(KERN_INFO "firmware_sample_driver: firmware: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070028}
29
30static void sample_probe_default(void)
31{
32 /* uses the default method to get the firmware */
Greg Kroah-Hartman4b65fc82008-02-20 16:07:07 -080033 const struct firmware *fw_entry;
34 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Greg Kroah-Hartman4b65fc82008-02-20 16:07:07 -080036 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 Torvalds1da177e2005-04-16 15:20:36 -070041 printk(KERN_ERR
42 "firmware_sample_driver: Firmware not available\n");
43 return;
44 }
Randy Dunlapd289bf72008-02-20 13:20:50 -080045
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 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-Hartman4b65fc82008-02-20 16:07:07 -080052
Linus Torvalds1da177e2005-04-16 15:20:36 -070053static void sample_probe_specific(void)
54{
Greg Kroah-Hartman4b65fc82008-02-20 16:07:07 -080055 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 /* 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-Hartman4b65fc82008-02-20 16:07:07 -080061 printk(KERN_INFO "firmware_sample_driver: "
62 "a ghost device got inserted :)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Greg Kroah-Hartman4b65fc82008-02-20 16:07:07 -080064 retval = request_firmware(NULL, "sample_driver_fw", &ghost_device);
65 if (retval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 printk(KERN_ERR
67 "firmware_sample_driver: Firmware load failed\n");
68 return;
69 }
Randy Dunlapd289bf72008-02-20 13:20:50 -080070
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 /* 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 Torvaldse9b62692008-04-21 16:36:46 -070076
Linus Torvalds1da177e2005-04-16 15:20:36 -070077static void sample_probe_async_cont(const struct firmware *fw, void *context)
78{
Greg Kroah-Hartman4b65fc82008-02-20 16:07:07 -080079 if (!fw) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 printk(KERN_ERR
81 "firmware_sample_driver: firmware load failed\n");
82 return;
83 }
84
Christophe Lucas20dd0262005-09-08 08:55:53 +020085 printk(KERN_INFO "firmware_sample_driver: device pointer \"%s\"\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 (char *)context);
87 sample_firmware_load(fw->data, fw->size);
88}
Greg Kroah-Hartman4b65fc82008-02-20 16:07:07 -080089
Linus Torvalds1da177e2005-04-16 15:20:36 -070090static void sample_probe_async(void)
91{
92 /* Let's say that I can't sleep */
93 int error;
Greg Kroah-Hartman4b65fc82008-02-20 16:07:07 -080094 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 Torvalds1da177e2005-04-16 15:20:36 -0700100 " request_firmware_nowait failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101}
102
103static int sample_init(void)
104{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 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-Hartman4b65fc82008-02-20 16:07:07 -0800113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114static void __exit sample_exit(void)
115{
116}
117
Greg Kroah-Hartman4b65fc82008-02-20 16:07:07 -0800118module_init(sample_init);
119module_exit(sample_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121MODULE_LICENSE("GPL");