Linux Audio

Check our new training course

Loading...
v3.1
  1/*
  2 * firmware_class.c - Multi purpose firmware loading support
  3 *
  4 * Copyright (c) 2003 Manuel Estrada Sainz
  5 *
  6 * Please see Documentation/firmware_class/ for more information.
  7 *
  8 */
  9
 10#include <linux/capability.h>
 11#include <linux/device.h>
 12#include <linux/module.h>
 13#include <linux/init.h>
 14#include <linux/timer.h>
 15#include <linux/vmalloc.h>
 16#include <linux/interrupt.h>
 17#include <linux/bitops.h>
 18#include <linux/mutex.h>
 19#include <linux/kthread.h>
 20#include <linux/highmem.h>
 21#include <linux/firmware.h>
 22#include <linux/slab.h>
 
 23
 24#define to_dev(obj) container_of(obj, struct device, kobj)
 25
 26MODULE_AUTHOR("Manuel Estrada Sainz");
 27MODULE_DESCRIPTION("Multi purpose firmware loading support");
 28MODULE_LICENSE("GPL");
 29
 30/* Builtin firmware support */
 31
 32#ifdef CONFIG_FW_LOADER
 33
 34extern struct builtin_fw __start_builtin_fw[];
 35extern struct builtin_fw __end_builtin_fw[];
 36
 37static bool fw_get_builtin_firmware(struct firmware *fw, const char *name)
 38{
 39	struct builtin_fw *b_fw;
 40
 41	for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) {
 42		if (strcmp(name, b_fw->name) == 0) {
 43			fw->size = b_fw->size;
 44			fw->data = b_fw->data;
 45			return true;
 46		}
 47	}
 48
 49	return false;
 50}
 51
 52static bool fw_is_builtin_firmware(const struct firmware *fw)
 53{
 54	struct builtin_fw *b_fw;
 55
 56	for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++)
 57		if (fw->data == b_fw->data)
 58			return true;
 59
 60	return false;
 61}
 62
 63#else /* Module case - no builtin firmware support */
 64
 65static inline bool fw_get_builtin_firmware(struct firmware *fw, const char *name)
 66{
 67	return false;
 68}
 69
 70static inline bool fw_is_builtin_firmware(const struct firmware *fw)
 71{
 72	return false;
 73}
 74#endif
 75
 76enum {
 77	FW_STATUS_LOADING,
 78	FW_STATUS_DONE,
 79	FW_STATUS_ABORT,
 80};
 81
 82static int loading_timeout = 60;	/* In seconds */
 83
 
 
 
 
 
 84/* fw_lock could be moved to 'struct firmware_priv' but since it is just
 85 * guarding for corner cases a global lock should be OK */
 86static DEFINE_MUTEX(fw_lock);
 87
 88struct firmware_priv {
 89	struct completion completion;
 90	struct firmware *fw;
 91	unsigned long status;
 92	struct page **pages;
 93	int nr_pages;
 94	int page_array_size;
 95	struct timer_list timeout;
 96	struct device dev;
 97	bool nowait;
 98	char fw_id[];
 99};
100
101static struct firmware_priv *to_firmware_priv(struct device *dev)
102{
103	return container_of(dev, struct firmware_priv, dev);
104}
105
106static void fw_load_abort(struct firmware_priv *fw_priv)
107{
108	set_bit(FW_STATUS_ABORT, &fw_priv->status);
109	wmb();
110	complete(&fw_priv->completion);
111}
112
113static ssize_t firmware_timeout_show(struct class *class,
114				     struct class_attribute *attr,
115				     char *buf)
116{
117	return sprintf(buf, "%d\n", loading_timeout);
118}
119
120/**
121 * firmware_timeout_store - set number of seconds to wait for firmware
122 * @class: device class pointer
123 * @attr: device attribute pointer
124 * @buf: buffer to scan for timeout value
125 * @count: number of bytes in @buf
126 *
127 *	Sets the number of seconds to wait for the firmware.  Once
128 *	this expires an error will be returned to the driver and no
129 *	firmware will be provided.
130 *
131 *	Note: zero means 'wait forever'.
132 **/
133static ssize_t firmware_timeout_store(struct class *class,
134				      struct class_attribute *attr,
135				      const char *buf, size_t count)
136{
137	loading_timeout = simple_strtol(buf, NULL, 10);
138	if (loading_timeout < 0)
139		loading_timeout = 0;
140
141	return count;
142}
143
144static struct class_attribute firmware_class_attrs[] = {
145	__ATTR(timeout, S_IWUSR | S_IRUGO,
146		firmware_timeout_show, firmware_timeout_store),
147	__ATTR_NULL
148};
149
150static void fw_dev_release(struct device *dev)
151{
152	struct firmware_priv *fw_priv = to_firmware_priv(dev);
153	int i;
154
155	for (i = 0; i < fw_priv->nr_pages; i++)
156		__free_page(fw_priv->pages[i]);
157	kfree(fw_priv->pages);
158	kfree(fw_priv);
159
160	module_put(THIS_MODULE);
161}
162
163static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
164{
165	struct firmware_priv *fw_priv = to_firmware_priv(dev);
166
167	if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->fw_id))
168		return -ENOMEM;
169	if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
170		return -ENOMEM;
171	if (add_uevent_var(env, "ASYNC=%d", fw_priv->nowait))
172		return -ENOMEM;
173
174	return 0;
175}
176
177static struct class firmware_class = {
178	.name		= "firmware",
179	.class_attrs	= firmware_class_attrs,
180	.dev_uevent	= firmware_uevent,
181	.dev_release	= fw_dev_release,
182};
183
184static ssize_t firmware_loading_show(struct device *dev,
185				     struct device_attribute *attr, char *buf)
186{
187	struct firmware_priv *fw_priv = to_firmware_priv(dev);
188	int loading = test_bit(FW_STATUS_LOADING, &fw_priv->status);
189
190	return sprintf(buf, "%d\n", loading);
191}
192
193static void firmware_free_data(const struct firmware *fw)
194{
195	int i;
196	vunmap(fw->data);
197	if (fw->pages) {
198		for (i = 0; i < PFN_UP(fw->size); i++)
199			__free_page(fw->pages[i]);
200		kfree(fw->pages);
201	}
202}
203
204/* Some architectures don't have PAGE_KERNEL_RO */
205#ifndef PAGE_KERNEL_RO
206#define PAGE_KERNEL_RO PAGE_KERNEL
207#endif
208/**
209 * firmware_loading_store - set value in the 'loading' control file
210 * @dev: device pointer
211 * @attr: device attribute pointer
212 * @buf: buffer to scan for loading control value
213 * @count: number of bytes in @buf
214 *
215 *	The relevant values are:
216 *
217 *	 1: Start a load, discarding any previous partial load.
218 *	 0: Conclude the load and hand the data to the driver code.
219 *	-1: Conclude the load with an error and discard any written data.
220 **/
221static ssize_t firmware_loading_store(struct device *dev,
222				      struct device_attribute *attr,
223				      const char *buf, size_t count)
224{
225	struct firmware_priv *fw_priv = to_firmware_priv(dev);
226	int loading = simple_strtol(buf, NULL, 10);
227	int i;
228
 
 
 
 
 
229	switch (loading) {
230	case 1:
231		mutex_lock(&fw_lock);
232		if (!fw_priv->fw) {
233			mutex_unlock(&fw_lock);
234			break;
235		}
236		firmware_free_data(fw_priv->fw);
237		memset(fw_priv->fw, 0, sizeof(struct firmware));
238		/* If the pages are not owned by 'struct firmware' */
239		for (i = 0; i < fw_priv->nr_pages; i++)
240			__free_page(fw_priv->pages[i]);
241		kfree(fw_priv->pages);
242		fw_priv->pages = NULL;
243		fw_priv->page_array_size = 0;
244		fw_priv->nr_pages = 0;
245		set_bit(FW_STATUS_LOADING, &fw_priv->status);
246		mutex_unlock(&fw_lock);
247		break;
248	case 0:
249		if (test_bit(FW_STATUS_LOADING, &fw_priv->status)) {
250			vunmap(fw_priv->fw->data);
251			fw_priv->fw->data = vmap(fw_priv->pages,
252						 fw_priv->nr_pages,
253						 0, PAGE_KERNEL_RO);
254			if (!fw_priv->fw->data) {
255				dev_err(dev, "%s: vmap() failed\n", __func__);
256				goto err;
257			}
258			/* Pages are now owned by 'struct firmware' */
259			fw_priv->fw->pages = fw_priv->pages;
260			fw_priv->pages = NULL;
261
262			fw_priv->page_array_size = 0;
263			fw_priv->nr_pages = 0;
264			complete(&fw_priv->completion);
265			clear_bit(FW_STATUS_LOADING, &fw_priv->status);
266			break;
267		}
268		/* fallthrough */
269	default:
270		dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
271		/* fallthrough */
272	case -1:
273	err:
274		fw_load_abort(fw_priv);
275		break;
276	}
277
 
278	return count;
279}
280
281static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
282
283static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
284				  struct bin_attribute *bin_attr,
285				  char *buffer, loff_t offset, size_t count)
286{
287	struct device *dev = to_dev(kobj);
288	struct firmware_priv *fw_priv = to_firmware_priv(dev);
289	struct firmware *fw;
290	ssize_t ret_count;
291
292	mutex_lock(&fw_lock);
293	fw = fw_priv->fw;
294	if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
295		ret_count = -ENODEV;
296		goto out;
297	}
298	if (offset > fw->size) {
299		ret_count = 0;
300		goto out;
301	}
302	if (count > fw->size - offset)
303		count = fw->size - offset;
304
305	ret_count = count;
306
307	while (count) {
308		void *page_data;
309		int page_nr = offset >> PAGE_SHIFT;
310		int page_ofs = offset & (PAGE_SIZE-1);
311		int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
312
313		page_data = kmap(fw_priv->pages[page_nr]);
314
315		memcpy(buffer, page_data + page_ofs, page_cnt);
316
317		kunmap(fw_priv->pages[page_nr]);
318		buffer += page_cnt;
319		offset += page_cnt;
320		count -= page_cnt;
321	}
322out:
323	mutex_unlock(&fw_lock);
324	return ret_count;
325}
326
327static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
328{
329	int pages_needed = ALIGN(min_size, PAGE_SIZE) >> PAGE_SHIFT;
330
331	/* If the array of pages is too small, grow it... */
332	if (fw_priv->page_array_size < pages_needed) {
333		int new_array_size = max(pages_needed,
334					 fw_priv->page_array_size * 2);
335		struct page **new_pages;
336
337		new_pages = kmalloc(new_array_size * sizeof(void *),
338				    GFP_KERNEL);
339		if (!new_pages) {
340			fw_load_abort(fw_priv);
341			return -ENOMEM;
342		}
343		memcpy(new_pages, fw_priv->pages,
344		       fw_priv->page_array_size * sizeof(void *));
345		memset(&new_pages[fw_priv->page_array_size], 0, sizeof(void *) *
346		       (new_array_size - fw_priv->page_array_size));
347		kfree(fw_priv->pages);
348		fw_priv->pages = new_pages;
349		fw_priv->page_array_size = new_array_size;
350	}
351
352	while (fw_priv->nr_pages < pages_needed) {
353		fw_priv->pages[fw_priv->nr_pages] =
354			alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
355
356		if (!fw_priv->pages[fw_priv->nr_pages]) {
357			fw_load_abort(fw_priv);
358			return -ENOMEM;
359		}
360		fw_priv->nr_pages++;
361	}
362	return 0;
363}
364
365/**
366 * firmware_data_write - write method for firmware
367 * @filp: open sysfs file
368 * @kobj: kobject for the device
369 * @bin_attr: bin_attr structure
370 * @buffer: buffer being written
371 * @offset: buffer offset for write in total data store area
372 * @count: buffer size
373 *
374 *	Data written to the 'data' attribute will be later handed to
375 *	the driver as a firmware image.
376 **/
377static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
378				   struct bin_attribute *bin_attr,
379				   char *buffer, loff_t offset, size_t count)
380{
381	struct device *dev = to_dev(kobj);
382	struct firmware_priv *fw_priv = to_firmware_priv(dev);
383	struct firmware *fw;
384	ssize_t retval;
385
386	if (!capable(CAP_SYS_RAWIO))
387		return -EPERM;
388
389	mutex_lock(&fw_lock);
390	fw = fw_priv->fw;
391	if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
392		retval = -ENODEV;
393		goto out;
394	}
395	retval = fw_realloc_buffer(fw_priv, offset + count);
396	if (retval)
397		goto out;
398
399	retval = count;
400
401	while (count) {
402		void *page_data;
403		int page_nr = offset >> PAGE_SHIFT;
404		int page_ofs = offset & (PAGE_SIZE - 1);
405		int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
406
407		page_data = kmap(fw_priv->pages[page_nr]);
408
409		memcpy(page_data + page_ofs, buffer, page_cnt);
410
411		kunmap(fw_priv->pages[page_nr]);
412		buffer += page_cnt;
413		offset += page_cnt;
414		count -= page_cnt;
415	}
416
417	fw->size = max_t(size_t, offset, fw->size);
418out:
419	mutex_unlock(&fw_lock);
420	return retval;
421}
422
423static struct bin_attribute firmware_attr_data = {
424	.attr = { .name = "data", .mode = 0644 },
425	.size = 0,
426	.read = firmware_data_read,
427	.write = firmware_data_write,
428};
429
430static void firmware_class_timeout(u_long data)
431{
432	struct firmware_priv *fw_priv = (struct firmware_priv *) data;
433
434	fw_load_abort(fw_priv);
435}
436
437static struct firmware_priv *
438fw_create_instance(struct firmware *firmware, const char *fw_name,
439		   struct device *device, bool uevent, bool nowait)
440{
441	struct firmware_priv *fw_priv;
442	struct device *f_dev;
443	int error;
444
445	fw_priv = kzalloc(sizeof(*fw_priv) + strlen(fw_name) + 1 , GFP_KERNEL);
446	if (!fw_priv) {
447		dev_err(device, "%s: kmalloc failed\n", __func__);
448		error = -ENOMEM;
449		goto err_out;
450	}
451
452	fw_priv->fw = firmware;
453	fw_priv->nowait = nowait;
454	strcpy(fw_priv->fw_id, fw_name);
455	init_completion(&fw_priv->completion);
456	setup_timer(&fw_priv->timeout,
457		    firmware_class_timeout, (u_long) fw_priv);
458
459	f_dev = &fw_priv->dev;
460
461	device_initialize(f_dev);
462	dev_set_name(f_dev, "%s", dev_name(device));
463	f_dev->parent = device;
464	f_dev->class = &firmware_class;
465
466	dev_set_uevent_suppress(f_dev, true);
 
467
468	/* Need to pin this module until class device is destroyed */
469	__module_get(THIS_MODULE);
 
 
 
 
470
471	error = device_add(f_dev);
472	if (error) {
473		dev_err(device, "%s: device_register failed\n", __func__);
474		goto err_put_dev;
475	}
476
477	error = device_create_bin_file(f_dev, &firmware_attr_data);
478	if (error) {
479		dev_err(device, "%s: sysfs_create_bin_file failed\n", __func__);
480		goto err_del_dev;
 
481	}
482
483	error = device_create_file(f_dev, &dev_attr_loading);
484	if (error) {
485		dev_err(device, "%s: device_create_file failed\n", __func__);
486		goto err_del_bin_attr;
487	}
488
489	if (uevent)
490		dev_set_uevent_suppress(f_dev, false);
491
 
 
492	return fw_priv;
493
494err_del_bin_attr:
495	device_remove_bin_file(f_dev, &firmware_attr_data);
496err_del_dev:
497	device_del(f_dev);
498err_put_dev:
499	put_device(f_dev);
500err_out:
501	return ERR_PTR(error);
502}
503
504static void fw_destroy_instance(struct firmware_priv *fw_priv)
505{
506	struct device *f_dev = &fw_priv->dev;
507
508	device_remove_file(f_dev, &dev_attr_loading);
509	device_remove_bin_file(f_dev, &firmware_attr_data);
510	device_unregister(f_dev);
511}
512
513static int _request_firmware(const struct firmware **firmware_p,
514			     const char *name, struct device *device,
515			     bool uevent, bool nowait)
516{
517	struct firmware_priv *fw_priv;
518	struct firmware *firmware;
519	int retval = 0;
 
520
521	if (!firmware_p)
522		return -EINVAL;
523
524	*firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
525	if (!firmware) {
526		dev_err(device, "%s: kmalloc(struct firmware) failed\n",
527			__func__);
528		retval = -ENOMEM;
529		goto out;
530	}
531
532	if (fw_get_builtin_firmware(firmware, name)) {
533		dev_dbg(device, "firmware: using built-in firmware %s\n", name);
534		return 0;
 
535	}
536
537	if (WARN_ON(usermodehelper_is_disabled())) {
538		dev_err(device, "firmware: %s will not be loaded\n", name);
539		retval = -EBUSY;
540		goto out;
541	}
542
543	if (uevent)
544		dev_dbg(device, "firmware: requesting %s\n", name);
545
546	fw_priv = fw_create_instance(firmware, name, device, uevent, nowait);
547	if (IS_ERR(fw_priv)) {
548		retval = PTR_ERR(fw_priv);
549		goto out;
550	}
551
552	if (uevent) {
553		if (loading_timeout > 0)
 
 
554			mod_timer(&fw_priv->timeout,
555				  round_jiffies_up(jiffies +
556						   loading_timeout * HZ));
557
558		kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD);
559	}
560
561	wait_for_completion(&fw_priv->completion);
562
563	set_bit(FW_STATUS_DONE, &fw_priv->status);
564	del_timer_sync(&fw_priv->timeout);
565
566	mutex_lock(&fw_lock);
567	if (!fw_priv->fw->size || test_bit(FW_STATUS_ABORT, &fw_priv->status))
568		retval = -ENOENT;
569	fw_priv->fw = NULL;
570	mutex_unlock(&fw_lock);
571
572	fw_destroy_instance(fw_priv);
573
574out:
575	if (retval) {
576		release_firmware(firmware);
577		*firmware_p = NULL;
578	}
579
580	return retval;
581}
582
583/**
584 * request_firmware: - send firmware request and wait for it
585 * @firmware_p: pointer to firmware image
586 * @name: name of firmware file
587 * @device: device for which firmware is being loaded
588 *
589 *      @firmware_p will be used to return a firmware image by the name
590 *      of @name for device @device.
591 *
592 *      Should be called from user context where sleeping is allowed.
593 *
594 *      @name will be used as $FIRMWARE in the uevent environment and
595 *      should be distinctive enough not to be confused with any other
596 *      firmware image for this or any other device.
597 **/
598int
599request_firmware(const struct firmware **firmware_p, const char *name,
600                 struct device *device)
601{
602        return _request_firmware(firmware_p, name, device, true, false);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
603}
604
605/**
606 * release_firmware: - release the resource associated with a firmware image
607 * @fw: firmware resource to release
608 **/
609void release_firmware(const struct firmware *fw)
610{
611	if (fw) {
612		if (!fw_is_builtin_firmware(fw))
613			firmware_free_data(fw);
614		kfree(fw);
615	}
616}
617
618/* Async support */
619struct firmware_work {
620	struct work_struct work;
621	struct module *module;
622	const char *name;
623	struct device *device;
624	void *context;
625	void (*cont)(const struct firmware *fw, void *context);
626	bool uevent;
627};
628
629static int request_firmware_work_func(void *arg)
630{
631	struct firmware_work *fw_work = arg;
632	const struct firmware *fw;
 
 
633	int ret;
634
635	if (!arg) {
636		WARN_ON(1);
637		return 0;
 
 
 
638	}
639
640	ret = _request_firmware(&fw, fw_work->name, fw_work->device,
641				fw_work->uevent, true);
 
 
 
 
 
 
 
 
 
 
 
642	fw_work->cont(fw, fw_work->context);
643
644	module_put(fw_work->module);
645	kfree(fw_work);
646
647	return ret;
648}
649
650/**
651 * request_firmware_nowait - asynchronous version of request_firmware
652 * @module: module requesting the firmware
653 * @uevent: sends uevent to copy the firmware image if this flag
654 *	is non-zero else the firmware copy must be done manually.
655 * @name: name of firmware file
656 * @device: device for which firmware is being loaded
657 * @gfp: allocation flags
658 * @context: will be passed over to @cont, and
659 *	@fw may be %NULL if firmware request fails.
660 * @cont: function will be called asynchronously when the firmware
661 *	request is over.
662 *
663 *	Asynchronous variant of request_firmware() for user contexts where
664 *	it is not possible to sleep for long time. It can't be called
665 *	in atomic contexts.
666 **/
667int
668request_firmware_nowait(
669	struct module *module, bool uevent,
670	const char *name, struct device *device, gfp_t gfp, void *context,
671	void (*cont)(const struct firmware *fw, void *context))
672{
673	struct task_struct *task;
674	struct firmware_work *fw_work;
675
676	fw_work = kzalloc(sizeof (struct firmware_work), gfp);
677	if (!fw_work)
678		return -ENOMEM;
679
680	fw_work->module = module;
681	fw_work->name = name;
682	fw_work->device = device;
683	fw_work->context = context;
684	fw_work->cont = cont;
685	fw_work->uevent = uevent;
686
687	if (!try_module_get(module)) {
688		kfree(fw_work);
689		return -EFAULT;
690	}
691
692	task = kthread_run(request_firmware_work_func, fw_work,
693			    "firmware/%s", name);
694	if (IS_ERR(task)) {
695		fw_work->cont(NULL, fw_work->context);
696		module_put(fw_work->module);
697		kfree(fw_work);
698		return PTR_ERR(task);
699	}
700
701	return 0;
702}
703
704static int __init firmware_class_init(void)
705{
706	return class_register(&firmware_class);
707}
708
709static void __exit firmware_class_exit(void)
710{
711	class_unregister(&firmware_class);
712}
713
714fs_initcall(firmware_class_init);
715module_exit(firmware_class_exit);
716
717EXPORT_SYMBOL(release_firmware);
718EXPORT_SYMBOL(request_firmware);
719EXPORT_SYMBOL(request_firmware_nowait);
v3.5.6
  1/*
  2 * firmware_class.c - Multi purpose firmware loading support
  3 *
  4 * Copyright (c) 2003 Manuel Estrada Sainz
  5 *
  6 * Please see Documentation/firmware_class/ for more information.
  7 *
  8 */
  9
 10#include <linux/capability.h>
 11#include <linux/device.h>
 12#include <linux/module.h>
 13#include <linux/init.h>
 14#include <linux/timer.h>
 15#include <linux/vmalloc.h>
 16#include <linux/interrupt.h>
 17#include <linux/bitops.h>
 18#include <linux/mutex.h>
 19#include <linux/workqueue.h>
 20#include <linux/highmem.h>
 21#include <linux/firmware.h>
 22#include <linux/slab.h>
 23#include <linux/sched.h>
 24
 25#define to_dev(obj) container_of(obj, struct device, kobj)
 26
 27MODULE_AUTHOR("Manuel Estrada Sainz");
 28MODULE_DESCRIPTION("Multi purpose firmware loading support");
 29MODULE_LICENSE("GPL");
 30
 31/* Builtin firmware support */
 32
 33#ifdef CONFIG_FW_LOADER
 34
 35extern struct builtin_fw __start_builtin_fw[];
 36extern struct builtin_fw __end_builtin_fw[];
 37
 38static bool fw_get_builtin_firmware(struct firmware *fw, const char *name)
 39{
 40	struct builtin_fw *b_fw;
 41
 42	for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) {
 43		if (strcmp(name, b_fw->name) == 0) {
 44			fw->size = b_fw->size;
 45			fw->data = b_fw->data;
 46			return true;
 47		}
 48	}
 49
 50	return false;
 51}
 52
 53static bool fw_is_builtin_firmware(const struct firmware *fw)
 54{
 55	struct builtin_fw *b_fw;
 56
 57	for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++)
 58		if (fw->data == b_fw->data)
 59			return true;
 60
 61	return false;
 62}
 63
 64#else /* Module case - no builtin firmware support */
 65
 66static inline bool fw_get_builtin_firmware(struct firmware *fw, const char *name)
 67{
 68	return false;
 69}
 70
 71static inline bool fw_is_builtin_firmware(const struct firmware *fw)
 72{
 73	return false;
 74}
 75#endif
 76
 77enum {
 78	FW_STATUS_LOADING,
 79	FW_STATUS_DONE,
 80	FW_STATUS_ABORT,
 81};
 82
 83static int loading_timeout = 60;	/* In seconds */
 84
 85static inline long firmware_loading_timeout(void)
 86{
 87	return loading_timeout > 0 ? loading_timeout * HZ : MAX_SCHEDULE_TIMEOUT;
 88}
 89
 90/* fw_lock could be moved to 'struct firmware_priv' but since it is just
 91 * guarding for corner cases a global lock should be OK */
 92static DEFINE_MUTEX(fw_lock);
 93
 94struct firmware_priv {
 95	struct completion completion;
 96	struct firmware *fw;
 97	unsigned long status;
 98	struct page **pages;
 99	int nr_pages;
100	int page_array_size;
101	struct timer_list timeout;
102	struct device dev;
103	bool nowait;
104	char fw_id[];
105};
106
107static struct firmware_priv *to_firmware_priv(struct device *dev)
108{
109	return container_of(dev, struct firmware_priv, dev);
110}
111
112static void fw_load_abort(struct firmware_priv *fw_priv)
113{
114	set_bit(FW_STATUS_ABORT, &fw_priv->status);
115	wmb();
116	complete(&fw_priv->completion);
117}
118
119static ssize_t firmware_timeout_show(struct class *class,
120				     struct class_attribute *attr,
121				     char *buf)
122{
123	return sprintf(buf, "%d\n", loading_timeout);
124}
125
126/**
127 * firmware_timeout_store - set number of seconds to wait for firmware
128 * @class: device class pointer
129 * @attr: device attribute pointer
130 * @buf: buffer to scan for timeout value
131 * @count: number of bytes in @buf
132 *
133 *	Sets the number of seconds to wait for the firmware.  Once
134 *	this expires an error will be returned to the driver and no
135 *	firmware will be provided.
136 *
137 *	Note: zero means 'wait forever'.
138 **/
139static ssize_t firmware_timeout_store(struct class *class,
140				      struct class_attribute *attr,
141				      const char *buf, size_t count)
142{
143	loading_timeout = simple_strtol(buf, NULL, 10);
144	if (loading_timeout < 0)
145		loading_timeout = 0;
146
147	return count;
148}
149
150static struct class_attribute firmware_class_attrs[] = {
151	__ATTR(timeout, S_IWUSR | S_IRUGO,
152		firmware_timeout_show, firmware_timeout_store),
153	__ATTR_NULL
154};
155
156static void fw_dev_release(struct device *dev)
157{
158	struct firmware_priv *fw_priv = to_firmware_priv(dev);
159	int i;
160
161	for (i = 0; i < fw_priv->nr_pages; i++)
162		__free_page(fw_priv->pages[i]);
163	kfree(fw_priv->pages);
164	kfree(fw_priv);
165
166	module_put(THIS_MODULE);
167}
168
169static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
170{
171	struct firmware_priv *fw_priv = to_firmware_priv(dev);
172
173	if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->fw_id))
174		return -ENOMEM;
175	if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
176		return -ENOMEM;
177	if (add_uevent_var(env, "ASYNC=%d", fw_priv->nowait))
178		return -ENOMEM;
179
180	return 0;
181}
182
183static struct class firmware_class = {
184	.name		= "firmware",
185	.class_attrs	= firmware_class_attrs,
186	.dev_uevent	= firmware_uevent,
187	.dev_release	= fw_dev_release,
188};
189
190static ssize_t firmware_loading_show(struct device *dev,
191				     struct device_attribute *attr, char *buf)
192{
193	struct firmware_priv *fw_priv = to_firmware_priv(dev);
194	int loading = test_bit(FW_STATUS_LOADING, &fw_priv->status);
195
196	return sprintf(buf, "%d\n", loading);
197}
198
199static void firmware_free_data(const struct firmware *fw)
200{
201	int i;
202	vunmap(fw->data);
203	if (fw->pages) {
204		for (i = 0; i < PFN_UP(fw->size); i++)
205			__free_page(fw->pages[i]);
206		kfree(fw->pages);
207	}
208}
209
210/* Some architectures don't have PAGE_KERNEL_RO */
211#ifndef PAGE_KERNEL_RO
212#define PAGE_KERNEL_RO PAGE_KERNEL
213#endif
214/**
215 * firmware_loading_store - set value in the 'loading' control file
216 * @dev: device pointer
217 * @attr: device attribute pointer
218 * @buf: buffer to scan for loading control value
219 * @count: number of bytes in @buf
220 *
221 *	The relevant values are:
222 *
223 *	 1: Start a load, discarding any previous partial load.
224 *	 0: Conclude the load and hand the data to the driver code.
225 *	-1: Conclude the load with an error and discard any written data.
226 **/
227static ssize_t firmware_loading_store(struct device *dev,
228				      struct device_attribute *attr,
229				      const char *buf, size_t count)
230{
231	struct firmware_priv *fw_priv = to_firmware_priv(dev);
232	int loading = simple_strtol(buf, NULL, 10);
233	int i;
234
235	mutex_lock(&fw_lock);
236
237	if (!fw_priv->fw)
238		goto out;
239
240	switch (loading) {
241	case 1:
 
 
 
 
 
242		firmware_free_data(fw_priv->fw);
243		memset(fw_priv->fw, 0, sizeof(struct firmware));
244		/* If the pages are not owned by 'struct firmware' */
245		for (i = 0; i < fw_priv->nr_pages; i++)
246			__free_page(fw_priv->pages[i]);
247		kfree(fw_priv->pages);
248		fw_priv->pages = NULL;
249		fw_priv->page_array_size = 0;
250		fw_priv->nr_pages = 0;
251		set_bit(FW_STATUS_LOADING, &fw_priv->status);
 
252		break;
253	case 0:
254		if (test_bit(FW_STATUS_LOADING, &fw_priv->status)) {
255			vunmap(fw_priv->fw->data);
256			fw_priv->fw->data = vmap(fw_priv->pages,
257						 fw_priv->nr_pages,
258						 0, PAGE_KERNEL_RO);
259			if (!fw_priv->fw->data) {
260				dev_err(dev, "%s: vmap() failed\n", __func__);
261				goto err;
262			}
263			/* Pages are now owned by 'struct firmware' */
264			fw_priv->fw->pages = fw_priv->pages;
265			fw_priv->pages = NULL;
266
267			fw_priv->page_array_size = 0;
268			fw_priv->nr_pages = 0;
269			complete(&fw_priv->completion);
270			clear_bit(FW_STATUS_LOADING, &fw_priv->status);
271			break;
272		}
273		/* fallthrough */
274	default:
275		dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
276		/* fallthrough */
277	case -1:
278	err:
279		fw_load_abort(fw_priv);
280		break;
281	}
282out:
283	mutex_unlock(&fw_lock);
284	return count;
285}
286
287static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
288
289static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
290				  struct bin_attribute *bin_attr,
291				  char *buffer, loff_t offset, size_t count)
292{
293	struct device *dev = to_dev(kobj);
294	struct firmware_priv *fw_priv = to_firmware_priv(dev);
295	struct firmware *fw;
296	ssize_t ret_count;
297
298	mutex_lock(&fw_lock);
299	fw = fw_priv->fw;
300	if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
301		ret_count = -ENODEV;
302		goto out;
303	}
304	if (offset > fw->size) {
305		ret_count = 0;
306		goto out;
307	}
308	if (count > fw->size - offset)
309		count = fw->size - offset;
310
311	ret_count = count;
312
313	while (count) {
314		void *page_data;
315		int page_nr = offset >> PAGE_SHIFT;
316		int page_ofs = offset & (PAGE_SIZE-1);
317		int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
318
319		page_data = kmap(fw_priv->pages[page_nr]);
320
321		memcpy(buffer, page_data + page_ofs, page_cnt);
322
323		kunmap(fw_priv->pages[page_nr]);
324		buffer += page_cnt;
325		offset += page_cnt;
326		count -= page_cnt;
327	}
328out:
329	mutex_unlock(&fw_lock);
330	return ret_count;
331}
332
333static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
334{
335	int pages_needed = ALIGN(min_size, PAGE_SIZE) >> PAGE_SHIFT;
336
337	/* If the array of pages is too small, grow it... */
338	if (fw_priv->page_array_size < pages_needed) {
339		int new_array_size = max(pages_needed,
340					 fw_priv->page_array_size * 2);
341		struct page **new_pages;
342
343		new_pages = kmalloc(new_array_size * sizeof(void *),
344				    GFP_KERNEL);
345		if (!new_pages) {
346			fw_load_abort(fw_priv);
347			return -ENOMEM;
348		}
349		memcpy(new_pages, fw_priv->pages,
350		       fw_priv->page_array_size * sizeof(void *));
351		memset(&new_pages[fw_priv->page_array_size], 0, sizeof(void *) *
352		       (new_array_size - fw_priv->page_array_size));
353		kfree(fw_priv->pages);
354		fw_priv->pages = new_pages;
355		fw_priv->page_array_size = new_array_size;
356	}
357
358	while (fw_priv->nr_pages < pages_needed) {
359		fw_priv->pages[fw_priv->nr_pages] =
360			alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
361
362		if (!fw_priv->pages[fw_priv->nr_pages]) {
363			fw_load_abort(fw_priv);
364			return -ENOMEM;
365		}
366		fw_priv->nr_pages++;
367	}
368	return 0;
369}
370
371/**
372 * firmware_data_write - write method for firmware
373 * @filp: open sysfs file
374 * @kobj: kobject for the device
375 * @bin_attr: bin_attr structure
376 * @buffer: buffer being written
377 * @offset: buffer offset for write in total data store area
378 * @count: buffer size
379 *
380 *	Data written to the 'data' attribute will be later handed to
381 *	the driver as a firmware image.
382 **/
383static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
384				   struct bin_attribute *bin_attr,
385				   char *buffer, loff_t offset, size_t count)
386{
387	struct device *dev = to_dev(kobj);
388	struct firmware_priv *fw_priv = to_firmware_priv(dev);
389	struct firmware *fw;
390	ssize_t retval;
391
392	if (!capable(CAP_SYS_RAWIO))
393		return -EPERM;
394
395	mutex_lock(&fw_lock);
396	fw = fw_priv->fw;
397	if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
398		retval = -ENODEV;
399		goto out;
400	}
401	retval = fw_realloc_buffer(fw_priv, offset + count);
402	if (retval)
403		goto out;
404
405	retval = count;
406
407	while (count) {
408		void *page_data;
409		int page_nr = offset >> PAGE_SHIFT;
410		int page_ofs = offset & (PAGE_SIZE - 1);
411		int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
412
413		page_data = kmap(fw_priv->pages[page_nr]);
414
415		memcpy(page_data + page_ofs, buffer, page_cnt);
416
417		kunmap(fw_priv->pages[page_nr]);
418		buffer += page_cnt;
419		offset += page_cnt;
420		count -= page_cnt;
421	}
422
423	fw->size = max_t(size_t, offset, fw->size);
424out:
425	mutex_unlock(&fw_lock);
426	return retval;
427}
428
429static struct bin_attribute firmware_attr_data = {
430	.attr = { .name = "data", .mode = 0644 },
431	.size = 0,
432	.read = firmware_data_read,
433	.write = firmware_data_write,
434};
435
436static void firmware_class_timeout(u_long data)
437{
438	struct firmware_priv *fw_priv = (struct firmware_priv *) data;
439
440	fw_load_abort(fw_priv);
441}
442
443static struct firmware_priv *
444fw_create_instance(struct firmware *firmware, const char *fw_name,
445		   struct device *device, bool uevent, bool nowait)
446{
447	struct firmware_priv *fw_priv;
448	struct device *f_dev;
 
449
450	fw_priv = kzalloc(sizeof(*fw_priv) + strlen(fw_name) + 1 , GFP_KERNEL);
451	if (!fw_priv) {
452		dev_err(device, "%s: kmalloc failed\n", __func__);
453		return ERR_PTR(-ENOMEM);
 
454	}
455
456	fw_priv->fw = firmware;
457	fw_priv->nowait = nowait;
458	strcpy(fw_priv->fw_id, fw_name);
459	init_completion(&fw_priv->completion);
460	setup_timer(&fw_priv->timeout,
461		    firmware_class_timeout, (u_long) fw_priv);
462
463	f_dev = &fw_priv->dev;
464
465	device_initialize(f_dev);
466	dev_set_name(f_dev, "%s", dev_name(device));
467	f_dev->parent = device;
468	f_dev->class = &firmware_class;
469
470	return fw_priv;
471}
472
473static struct firmware_priv *
474_request_firmware_prepare(const struct firmware **firmware_p, const char *name,
475			  struct device *device, bool uevent, bool nowait)
476{
477	struct firmware *firmware;
478	struct firmware_priv *fw_priv;
479
480	if (!firmware_p)
481		return ERR_PTR(-EINVAL);
 
 
 
482
483	*firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
484	if (!firmware) {
485		dev_err(device, "%s: kmalloc(struct firmware) failed\n",
486			__func__);
487		return ERR_PTR(-ENOMEM);
488	}
489
490	if (fw_get_builtin_firmware(firmware, name)) {
491		dev_dbg(device, "firmware: using built-in firmware %s\n", name);
492		return NULL;
 
493	}
494
495	fw_priv = fw_create_instance(firmware, name, device, uevent, nowait);
496	if (IS_ERR(fw_priv)) {
497		release_firmware(firmware);
498		*firmware_p = NULL;
499	}
500	return fw_priv;
 
 
 
 
 
 
 
 
 
501}
502
503static void _request_firmware_cleanup(const struct firmware **firmware_p)
504{
505	release_firmware(*firmware_p);
506	*firmware_p = NULL;
 
 
 
507}
508
509static int _request_firmware_load(struct firmware_priv *fw_priv, bool uevent,
510				  long timeout)
 
511{
 
 
512	int retval = 0;
513	struct device *f_dev = &fw_priv->dev;
514
515	dev_set_uevent_suppress(f_dev, true);
 
516
517	/* Need to pin this module until class device is destroyed */
518	__module_get(THIS_MODULE);
 
 
 
 
 
519
520	retval = device_add(f_dev);
521	if (retval) {
522		dev_err(f_dev, "%s: device_register failed\n", __func__);
523		goto err_put_dev;
524	}
525
526	retval = device_create_bin_file(f_dev, &firmware_attr_data);
527	if (retval) {
528		dev_err(f_dev, "%s: sysfs_create_bin_file failed\n", __func__);
529		goto err_del_dev;
530	}
531
532	retval = device_create_file(f_dev, &dev_attr_loading);
533	if (retval) {
534		dev_err(f_dev, "%s: device_create_file failed\n", __func__);
535		goto err_del_bin_attr;
 
 
 
536	}
537
538	if (uevent) {
539		dev_set_uevent_suppress(f_dev, false);
540		dev_dbg(f_dev, "firmware: requesting %s\n", fw_priv->fw_id);
541		if (timeout != MAX_SCHEDULE_TIMEOUT)
542			mod_timer(&fw_priv->timeout,
543				  round_jiffies_up(jiffies + timeout));
 
544
545		kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD);
546	}
547
548	wait_for_completion(&fw_priv->completion);
549
550	set_bit(FW_STATUS_DONE, &fw_priv->status);
551	del_timer_sync(&fw_priv->timeout);
552
553	mutex_lock(&fw_lock);
554	if (!fw_priv->fw->size || test_bit(FW_STATUS_ABORT, &fw_priv->status))
555		retval = -ENOENT;
556	fw_priv->fw = NULL;
557	mutex_unlock(&fw_lock);
558
559	device_remove_file(f_dev, &dev_attr_loading);
560err_del_bin_attr:
561	device_remove_bin_file(f_dev, &firmware_attr_data);
562err_del_dev:
563	device_del(f_dev);
564err_put_dev:
565	put_device(f_dev);
 
566	return retval;
567}
568
569/**
570 * request_firmware: - send firmware request and wait for it
571 * @firmware_p: pointer to firmware image
572 * @name: name of firmware file
573 * @device: device for which firmware is being loaded
574 *
575 *      @firmware_p will be used to return a firmware image by the name
576 *      of @name for device @device.
577 *
578 *      Should be called from user context where sleeping is allowed.
579 *
580 *      @name will be used as $FIRMWARE in the uevent environment and
581 *      should be distinctive enough not to be confused with any other
582 *      firmware image for this or any other device.
583 **/
584int
585request_firmware(const struct firmware **firmware_p, const char *name,
586                 struct device *device)
587{
588	struct firmware_priv *fw_priv;
589	int ret;
590
591	fw_priv = _request_firmware_prepare(firmware_p, name, device, true,
592					    false);
593	if (IS_ERR_OR_NULL(fw_priv))
594		return PTR_RET(fw_priv);
595
596	ret = usermodehelper_read_trylock();
597	if (WARN_ON(ret)) {
598		dev_err(device, "firmware: %s will not be loaded\n", name);
599	} else {
600		ret = _request_firmware_load(fw_priv, true,
601					firmware_loading_timeout());
602		usermodehelper_read_unlock();
603	}
604	if (ret)
605		_request_firmware_cleanup(firmware_p);
606
607	return ret;
608}
609
610/**
611 * release_firmware: - release the resource associated with a firmware image
612 * @fw: firmware resource to release
613 **/
614void release_firmware(const struct firmware *fw)
615{
616	if (fw) {
617		if (!fw_is_builtin_firmware(fw))
618			firmware_free_data(fw);
619		kfree(fw);
620	}
621}
622
623/* Async support */
624struct firmware_work {
625	struct work_struct work;
626	struct module *module;
627	const char *name;
628	struct device *device;
629	void *context;
630	void (*cont)(const struct firmware *fw, void *context);
631	bool uevent;
632};
633
634static void request_firmware_work_func(struct work_struct *work)
635{
636	struct firmware_work *fw_work;
637	const struct firmware *fw;
638	struct firmware_priv *fw_priv;
639	long timeout;
640	int ret;
641
642	fw_work = container_of(work, struct firmware_work, work);
643	fw_priv = _request_firmware_prepare(&fw, fw_work->name, fw_work->device,
644			fw_work->uevent, true);
645	if (IS_ERR_OR_NULL(fw_priv)) {
646		ret = PTR_RET(fw_priv);
647		goto out;
648	}
649
650	timeout = usermodehelper_read_lock_wait(firmware_loading_timeout());
651	if (timeout) {
652		ret = _request_firmware_load(fw_priv, fw_work->uevent, timeout);
653		usermodehelper_read_unlock();
654	} else {
655		dev_dbg(fw_work->device, "firmware: %s loading timed out\n",
656			fw_work->name);
657		ret = -EAGAIN;
658	}
659	if (ret)
660		_request_firmware_cleanup(&fw);
661
662 out:
663	fw_work->cont(fw, fw_work->context);
664
665	module_put(fw_work->module);
666	kfree(fw_work);
 
 
667}
668
669/**
670 * request_firmware_nowait - asynchronous version of request_firmware
671 * @module: module requesting the firmware
672 * @uevent: sends uevent to copy the firmware image if this flag
673 *	is non-zero else the firmware copy must be done manually.
674 * @name: name of firmware file
675 * @device: device for which firmware is being loaded
676 * @gfp: allocation flags
677 * @context: will be passed over to @cont, and
678 *	@fw may be %NULL if firmware request fails.
679 * @cont: function will be called asynchronously when the firmware
680 *	request is over.
681 *
682 *	Asynchronous variant of request_firmware() for user contexts where
683 *	it is not possible to sleep for long time. It can't be called
684 *	in atomic contexts.
685 **/
686int
687request_firmware_nowait(
688	struct module *module, bool uevent,
689	const char *name, struct device *device, gfp_t gfp, void *context,
690	void (*cont)(const struct firmware *fw, void *context))
691{
 
692	struct firmware_work *fw_work;
693
694	fw_work = kzalloc(sizeof (struct firmware_work), gfp);
695	if (!fw_work)
696		return -ENOMEM;
697
698	fw_work->module = module;
699	fw_work->name = name;
700	fw_work->device = device;
701	fw_work->context = context;
702	fw_work->cont = cont;
703	fw_work->uevent = uevent;
704
705	if (!try_module_get(module)) {
706		kfree(fw_work);
707		return -EFAULT;
708	}
709
710	INIT_WORK(&fw_work->work, request_firmware_work_func);
711	schedule_work(&fw_work->work);
 
 
 
 
 
 
 
712	return 0;
713}
714
715static int __init firmware_class_init(void)
716{
717	return class_register(&firmware_class);
718}
719
720static void __exit firmware_class_exit(void)
721{
722	class_unregister(&firmware_class);
723}
724
725fs_initcall(firmware_class_init);
726module_exit(firmware_class_exit);
727
728EXPORT_SYMBOL(release_firmware);
729EXPORT_SYMBOL(request_firmware);
730EXPORT_SYMBOL(request_firmware_nowait);