blob: 165cff98032e70810ff866afcd51f324cda1efa9 [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}
76static void sample_probe_async_cont(const struct firmware *fw, void *context)
77{
Greg Kroah-Hartman4b65fc82008-02-20 16:07:07 -080078 if (!fw) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 printk(KERN_ERR
80 "firmware_sample_driver: firmware load failed\n");
81 return;
82 }
83
Christophe Lucas20dd0262005-09-08 08:55:53 +020084 printk(KERN_INFO "firmware_sample_driver: device pointer \"%s\"\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 (char *)context);
86 sample_firmware_load(fw->data, fw->size);
87}
Greg Kroah-Hartman4b65fc82008-02-20 16:07:07 -080088
Linus Torvalds1da177e2005-04-16 15:20:36 -070089static void sample_probe_async(void)
90{
91 /* Let's say that I can't sleep */
92 int error;
Greg Kroah-Hartman4b65fc82008-02-20 16:07:07 -080093 error = request_firmware_nowait(THIS_MODULE, FW_ACTION_NOHOTPLUG,
94 "sample_driver_fw", &ghost_device,
95 "my device pointer",
96 sample_probe_async_cont);
97 if (error)
98 printk(KERN_ERR "firmware_sample_driver:"
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 " request_firmware_nowait failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
101
102static int sample_init(void)
103{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 device_initialize(&ghost_device);
105 /* since there is no real hardware insertion I just call the
106 * sample probe functions here */
107 sample_probe_specific();
108 sample_probe_default();
109 sample_probe_async();
110 return 0;
111}
Greg Kroah-Hartman4b65fc82008-02-20 16:07:07 -0800112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113static void __exit sample_exit(void)
114{
115}
116
Greg Kroah-Hartman4b65fc82008-02-20 16:07:07 -0800117module_init(sample_init);
118module_exit(sample_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120MODULE_LICENSE("GPL");