xref: /linux/drivers/base/firmware_loader/sysfs.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
1e0c11a8bSRuss Weight // SPDX-License-Identifier: GPL-2.0
2e0c11a8bSRuss Weight 
3e0c11a8bSRuss Weight #include <linux/highmem.h>
4e0c11a8bSRuss Weight #include <linux/module.h>
5e0c11a8bSRuss Weight #include <linux/security.h>
6e0c11a8bSRuss Weight #include <linux/slab.h>
7e0c11a8bSRuss Weight #include <linux/types.h>
8e0c11a8bSRuss Weight 
9e0c11a8bSRuss Weight #include "sysfs.h"
10e0c11a8bSRuss Weight 
11e0c11a8bSRuss Weight /*
12e0c11a8bSRuss Weight  * sysfs support for firmware loader
13e0c11a8bSRuss Weight  */
14e0c11a8bSRuss Weight 
__fw_load_abort(struct fw_priv * fw_priv)15e0c11a8bSRuss Weight void __fw_load_abort(struct fw_priv *fw_priv)
16e0c11a8bSRuss Weight {
17e0c11a8bSRuss Weight 	/*
18e0c11a8bSRuss Weight 	 * There is a small window in which user can write to 'loading'
19e0c11a8bSRuss Weight 	 * between loading done/aborted and disappearance of 'loading'
20e0c11a8bSRuss Weight 	 */
21e0c11a8bSRuss Weight 	if (fw_state_is_aborted(fw_priv) || fw_state_is_done(fw_priv))
22e0c11a8bSRuss Weight 		return;
23e0c11a8bSRuss Weight 
24e0c11a8bSRuss Weight 	fw_state_aborted(fw_priv);
25e0c11a8bSRuss Weight }
26e0c11a8bSRuss Weight 
27e0c11a8bSRuss Weight #ifdef CONFIG_FW_LOADER_USER_HELPER
timeout_show(const struct class * class,const struct class_attribute * attr,char * buf)28*75a2d422SGreg Kroah-Hartman static ssize_t timeout_show(const struct class *class, const struct class_attribute *attr,
29e0c11a8bSRuss Weight 			    char *buf)
30e0c11a8bSRuss Weight {
31e0c11a8bSRuss Weight 	return sysfs_emit(buf, "%d\n", __firmware_loading_timeout());
32e0c11a8bSRuss Weight }
33e0c11a8bSRuss Weight 
34e0c11a8bSRuss Weight /**
35e0c11a8bSRuss Weight  * timeout_store() - set number of seconds to wait for firmware
36e0c11a8bSRuss Weight  * @class: device class pointer
37e0c11a8bSRuss Weight  * @attr: device attribute pointer
38e0c11a8bSRuss Weight  * @buf: buffer to scan for timeout value
39e0c11a8bSRuss Weight  * @count: number of bytes in @buf
40e0c11a8bSRuss Weight  *
41e0c11a8bSRuss Weight  *	Sets the number of seconds to wait for the firmware.  Once
42e0c11a8bSRuss Weight  *	this expires an error will be returned to the driver and no
43e0c11a8bSRuss Weight  *	firmware will be provided.
44e0c11a8bSRuss Weight  *
45e0c11a8bSRuss Weight  *	Note: zero means 'wait forever'.
46e0c11a8bSRuss Weight  **/
timeout_store(const struct class * class,const struct class_attribute * attr,const char * buf,size_t count)47*75a2d422SGreg Kroah-Hartman static ssize_t timeout_store(const struct class *class, const struct class_attribute *attr,
48e0c11a8bSRuss Weight 			     const char *buf, size_t count)
49e0c11a8bSRuss Weight {
50e0c11a8bSRuss Weight 	int tmp_loading_timeout = simple_strtol(buf, NULL, 10);
51e0c11a8bSRuss Weight 
52e0c11a8bSRuss Weight 	if (tmp_loading_timeout < 0)
53e0c11a8bSRuss Weight 		tmp_loading_timeout = 0;
54e0c11a8bSRuss Weight 
55e0c11a8bSRuss Weight 	__fw_fallback_set_timeout(tmp_loading_timeout);
56e0c11a8bSRuss Weight 
57e0c11a8bSRuss Weight 	return count;
58e0c11a8bSRuss Weight }
59e0c11a8bSRuss Weight static CLASS_ATTR_RW(timeout);
60e0c11a8bSRuss Weight 
61e0c11a8bSRuss Weight static struct attribute *firmware_class_attrs[] = {
62e0c11a8bSRuss Weight 	&class_attr_timeout.attr,
63e0c11a8bSRuss Weight 	NULL,
64e0c11a8bSRuss Weight };
65e0c11a8bSRuss Weight ATTRIBUTE_GROUPS(firmware_class);
66e0c11a8bSRuss Weight 
do_firmware_uevent(const struct fw_sysfs * fw_sysfs,struct kobj_uevent_env * env)6723680f0bSGreg Kroah-Hartman static int do_firmware_uevent(const struct fw_sysfs *fw_sysfs, struct kobj_uevent_env *env)
68e0c11a8bSRuss Weight {
69e0c11a8bSRuss Weight 	if (add_uevent_var(env, "FIRMWARE=%s", fw_sysfs->fw_priv->fw_name))
70e0c11a8bSRuss Weight 		return -ENOMEM;
71e0c11a8bSRuss Weight 	if (add_uevent_var(env, "TIMEOUT=%i", __firmware_loading_timeout()))
72e0c11a8bSRuss Weight 		return -ENOMEM;
73e0c11a8bSRuss Weight 	if (add_uevent_var(env, "ASYNC=%d", fw_sysfs->nowait))
74e0c11a8bSRuss Weight 		return -ENOMEM;
75e0c11a8bSRuss Weight 
76e0c11a8bSRuss Weight 	return 0;
77e0c11a8bSRuss Weight }
78e0c11a8bSRuss Weight 
firmware_uevent(const struct device * dev,struct kobj_uevent_env * env)7923680f0bSGreg Kroah-Hartman static int firmware_uevent(const struct device *dev, struct kobj_uevent_env *env)
80e0c11a8bSRuss Weight {
8123680f0bSGreg Kroah-Hartman 	const struct fw_sysfs *fw_sysfs = to_fw_sysfs(dev);
82e0c11a8bSRuss Weight 	int err = 0;
83e0c11a8bSRuss Weight 
84e0c11a8bSRuss Weight 	mutex_lock(&fw_lock);
85e0c11a8bSRuss Weight 	if (fw_sysfs->fw_priv)
86e0c11a8bSRuss Weight 		err = do_firmware_uevent(fw_sysfs, env);
87e0c11a8bSRuss Weight 	mutex_unlock(&fw_lock);
88e0c11a8bSRuss Weight 	return err;
89e0c11a8bSRuss Weight }
90e0c11a8bSRuss Weight #endif /* CONFIG_FW_LOADER_USER_HELPER */
91e0c11a8bSRuss Weight 
fw_dev_release(struct device * dev)92e0c11a8bSRuss Weight static void fw_dev_release(struct device *dev)
93e0c11a8bSRuss Weight {
94e0c11a8bSRuss Weight 	struct fw_sysfs *fw_sysfs = to_fw_sysfs(dev);
95e0c11a8bSRuss Weight 
96789bba82SRuss Weight 	if (fw_sysfs->fw_upload_priv)
97789bba82SRuss Weight 		fw_upload_free(fw_sysfs);
98789bba82SRuss Weight 
99e0c11a8bSRuss Weight 	kfree(fw_sysfs);
100e0c11a8bSRuss Weight }
101e0c11a8bSRuss Weight 
102e0c11a8bSRuss Weight static struct class firmware_class = {
103e0c11a8bSRuss Weight 	.name		= "firmware",
104e0c11a8bSRuss Weight #ifdef CONFIG_FW_LOADER_USER_HELPER
105e0c11a8bSRuss Weight 	.class_groups	= firmware_class_groups,
106e0c11a8bSRuss Weight 	.dev_uevent	= firmware_uevent,
107e0c11a8bSRuss Weight #endif
108e0c11a8bSRuss Weight 	.dev_release	= fw_dev_release,
109e0c11a8bSRuss Weight };
110e0c11a8bSRuss Weight 
register_sysfs_loader(void)111e0c11a8bSRuss Weight int register_sysfs_loader(void)
112e0c11a8bSRuss Weight {
113e0c11a8bSRuss Weight 	int ret = class_register(&firmware_class);
114e0c11a8bSRuss Weight 
115e0c11a8bSRuss Weight 	if (ret != 0)
116e0c11a8bSRuss Weight 		return ret;
117e0c11a8bSRuss Weight 	return register_firmware_config_sysctl();
118e0c11a8bSRuss Weight }
119e0c11a8bSRuss Weight 
unregister_sysfs_loader(void)120e0c11a8bSRuss Weight void unregister_sysfs_loader(void)
121e0c11a8bSRuss Weight {
122e0c11a8bSRuss Weight 	unregister_firmware_config_sysctl();
123e0c11a8bSRuss Weight 	class_unregister(&firmware_class);
124e0c11a8bSRuss Weight }
125e0c11a8bSRuss Weight 
firmware_loading_show(struct device * dev,struct device_attribute * attr,char * buf)126e0c11a8bSRuss Weight static ssize_t firmware_loading_show(struct device *dev,
127e0c11a8bSRuss Weight 				     struct device_attribute *attr, char *buf)
128e0c11a8bSRuss Weight {
129e0c11a8bSRuss Weight 	struct fw_sysfs *fw_sysfs = to_fw_sysfs(dev);
130e0c11a8bSRuss Weight 	int loading = 0;
131e0c11a8bSRuss Weight 
132e0c11a8bSRuss Weight 	mutex_lock(&fw_lock);
133e0c11a8bSRuss Weight 	if (fw_sysfs->fw_priv)
134e0c11a8bSRuss Weight 		loading = fw_state_is_loading(fw_sysfs->fw_priv);
135e0c11a8bSRuss Weight 	mutex_unlock(&fw_lock);
136e0c11a8bSRuss Weight 
137e0c11a8bSRuss Weight 	return sysfs_emit(buf, "%d\n", loading);
138e0c11a8bSRuss Weight }
139e0c11a8bSRuss Weight 
140e0c11a8bSRuss Weight /**
141e0c11a8bSRuss Weight  * firmware_loading_store() - set value in the 'loading' control file
142e0c11a8bSRuss Weight  * @dev: device pointer
143e0c11a8bSRuss Weight  * @attr: device attribute pointer
144e0c11a8bSRuss Weight  * @buf: buffer to scan for loading control value
145e0c11a8bSRuss Weight  * @count: number of bytes in @buf
146e0c11a8bSRuss Weight  *
147e0c11a8bSRuss Weight  *	The relevant values are:
148e0c11a8bSRuss Weight  *
149e0c11a8bSRuss Weight  *	 1: Start a load, discarding any previous partial load.
150e0c11a8bSRuss Weight  *	 0: Conclude the load and hand the data to the driver code.
151e0c11a8bSRuss Weight  *	-1: Conclude the load with an error and discard any written data.
152e0c11a8bSRuss Weight  **/
firmware_loading_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)153e0c11a8bSRuss Weight static ssize_t firmware_loading_store(struct device *dev,
154e0c11a8bSRuss Weight 				      struct device_attribute *attr,
155e0c11a8bSRuss Weight 				      const char *buf, size_t count)
156e0c11a8bSRuss Weight {
157e0c11a8bSRuss Weight 	struct fw_sysfs *fw_sysfs = to_fw_sysfs(dev);
158e0c11a8bSRuss Weight 	struct fw_priv *fw_priv;
159e0c11a8bSRuss Weight 	ssize_t written = count;
160e0c11a8bSRuss Weight 	int loading = simple_strtol(buf, NULL, 10);
161e0c11a8bSRuss Weight 
162e0c11a8bSRuss Weight 	mutex_lock(&fw_lock);
163e0c11a8bSRuss Weight 	fw_priv = fw_sysfs->fw_priv;
164e0c11a8bSRuss Weight 	if (fw_state_is_aborted(fw_priv) || fw_state_is_done(fw_priv))
165e0c11a8bSRuss Weight 		goto out;
166e0c11a8bSRuss Weight 
167e0c11a8bSRuss Weight 	switch (loading) {
168e0c11a8bSRuss Weight 	case 1:
169e0c11a8bSRuss Weight 		/* discarding any previous partial load */
170e0c11a8bSRuss Weight 		fw_free_paged_buf(fw_priv);
171e0c11a8bSRuss Weight 		fw_state_start(fw_priv);
172e0c11a8bSRuss Weight 		break;
173e0c11a8bSRuss Weight 	case 0:
174e0c11a8bSRuss Weight 		if (fw_state_is_loading(fw_priv)) {
175e0c11a8bSRuss Weight 			int rc;
176e0c11a8bSRuss Weight 
177e0c11a8bSRuss Weight 			/*
178e0c11a8bSRuss Weight 			 * Several loading requests may be pending on
179e0c11a8bSRuss Weight 			 * one same firmware buf, so let all requests
180e0c11a8bSRuss Weight 			 * see the mapped 'buf->data' once the loading
181e0c11a8bSRuss Weight 			 * is completed.
182e0c11a8bSRuss Weight 			 */
183e0c11a8bSRuss Weight 			rc = fw_map_paged_buf(fw_priv);
184e0c11a8bSRuss Weight 			if (rc)
185e0c11a8bSRuss Weight 				dev_err(dev, "%s: map pages failed\n",
186e0c11a8bSRuss Weight 					__func__);
187e0c11a8bSRuss Weight 			else
188e0c11a8bSRuss Weight 				rc = security_kernel_post_load_data(fw_priv->data,
189e0c11a8bSRuss Weight 								    fw_priv->size,
190e0c11a8bSRuss Weight 								    LOADING_FIRMWARE,
191e0c11a8bSRuss Weight 								    "blob");
192e0c11a8bSRuss Weight 
193e0c11a8bSRuss Weight 			/*
194e0c11a8bSRuss Weight 			 * Same logic as fw_load_abort, only the DONE bit
195e0c11a8bSRuss Weight 			 * is ignored and we set ABORT only on failure.
196e0c11a8bSRuss Weight 			 */
197e0c11a8bSRuss Weight 			if (rc) {
198e0c11a8bSRuss Weight 				fw_state_aborted(fw_priv);
199e0c11a8bSRuss Weight 				written = rc;
200e0c11a8bSRuss Weight 			} else {
201e0c11a8bSRuss Weight 				fw_state_done(fw_priv);
20297730bbbSRuss Weight 
20397730bbbSRuss Weight 				/*
20497730bbbSRuss Weight 				 * If this is a user-initiated firmware upload
20597730bbbSRuss Weight 				 * then start the upload in a worker thread now.
20697730bbbSRuss Weight 				 */
20797730bbbSRuss Weight 				rc = fw_upload_start(fw_sysfs);
20897730bbbSRuss Weight 				if (rc)
20997730bbbSRuss Weight 					written = rc;
210e0c11a8bSRuss Weight 			}
211e0c11a8bSRuss Weight 			break;
212e0c11a8bSRuss Weight 		}
213e0c11a8bSRuss Weight 		fallthrough;
214e0c11a8bSRuss Weight 	default:
215e0c11a8bSRuss Weight 		dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
216e0c11a8bSRuss Weight 		fallthrough;
217e0c11a8bSRuss Weight 	case -1:
218e0c11a8bSRuss Weight 		fw_load_abort(fw_sysfs);
21997730bbbSRuss Weight 		if (fw_sysfs->fw_upload_priv)
22097730bbbSRuss Weight 			fw_state_init(fw_sysfs->fw_priv);
22197730bbbSRuss Weight 
222e0c11a8bSRuss Weight 		break;
223e0c11a8bSRuss Weight 	}
224e0c11a8bSRuss Weight out:
225e0c11a8bSRuss Weight 	mutex_unlock(&fw_lock);
226e0c11a8bSRuss Weight 	return written;
227e0c11a8bSRuss Weight }
228e0c11a8bSRuss Weight 
22997730bbbSRuss Weight DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
230e0c11a8bSRuss Weight 
firmware_rw_data(struct fw_priv * fw_priv,char * buffer,loff_t offset,size_t count,bool read)231e0c11a8bSRuss Weight static void firmware_rw_data(struct fw_priv *fw_priv, char *buffer,
232e0c11a8bSRuss Weight 			     loff_t offset, size_t count, bool read)
233e0c11a8bSRuss Weight {
234e0c11a8bSRuss Weight 	if (read)
235e0c11a8bSRuss Weight 		memcpy(buffer, fw_priv->data + offset, count);
236e0c11a8bSRuss Weight 	else
237e0c11a8bSRuss Weight 		memcpy(fw_priv->data + offset, buffer, count);
238e0c11a8bSRuss Weight }
239e0c11a8bSRuss Weight 
firmware_rw(struct fw_priv * fw_priv,char * buffer,loff_t offset,size_t count,bool read)240e0c11a8bSRuss Weight static void firmware_rw(struct fw_priv *fw_priv, char *buffer,
241e0c11a8bSRuss Weight 			loff_t offset, size_t count, bool read)
242e0c11a8bSRuss Weight {
243e0c11a8bSRuss Weight 	while (count) {
244e0c11a8bSRuss Weight 		int page_nr = offset >> PAGE_SHIFT;
245e0c11a8bSRuss Weight 		int page_ofs = offset & (PAGE_SIZE - 1);
246e0c11a8bSRuss Weight 		int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
247e0c11a8bSRuss Weight 
248e0c11a8bSRuss Weight 		if (read)
249f2d57765SFabio M. De Francesco 			memcpy_from_page(buffer, fw_priv->pages[page_nr],
250f2d57765SFabio M. De Francesco 					 page_ofs, page_cnt);
251e0c11a8bSRuss Weight 		else
252f2d57765SFabio M. De Francesco 			memcpy_to_page(fw_priv->pages[page_nr], page_ofs,
253f2d57765SFabio M. De Francesco 				       buffer, page_cnt);
254e0c11a8bSRuss Weight 
255e0c11a8bSRuss Weight 		buffer += page_cnt;
256e0c11a8bSRuss Weight 		offset += page_cnt;
257e0c11a8bSRuss Weight 		count -= page_cnt;
258e0c11a8bSRuss Weight 	}
259e0c11a8bSRuss Weight }
260e0c11a8bSRuss Weight 
firmware_data_read(struct file * filp,struct kobject * kobj,struct bin_attribute * bin_attr,char * buffer,loff_t offset,size_t count)261e0c11a8bSRuss Weight static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
262e0c11a8bSRuss Weight 				  struct bin_attribute *bin_attr,
263e0c11a8bSRuss Weight 				  char *buffer, loff_t offset, size_t count)
264e0c11a8bSRuss Weight {
265e0c11a8bSRuss Weight 	struct device *dev = kobj_to_dev(kobj);
266e0c11a8bSRuss Weight 	struct fw_sysfs *fw_sysfs = to_fw_sysfs(dev);
267e0c11a8bSRuss Weight 	struct fw_priv *fw_priv;
268e0c11a8bSRuss Weight 	ssize_t ret_count;
269e0c11a8bSRuss Weight 
270e0c11a8bSRuss Weight 	mutex_lock(&fw_lock);
271e0c11a8bSRuss Weight 	fw_priv = fw_sysfs->fw_priv;
272e0c11a8bSRuss Weight 	if (!fw_priv || fw_state_is_done(fw_priv)) {
273e0c11a8bSRuss Weight 		ret_count = -ENODEV;
274e0c11a8bSRuss Weight 		goto out;
275e0c11a8bSRuss Weight 	}
276e0c11a8bSRuss Weight 	if (offset > fw_priv->size) {
277e0c11a8bSRuss Weight 		ret_count = 0;
278e0c11a8bSRuss Weight 		goto out;
279e0c11a8bSRuss Weight 	}
280e0c11a8bSRuss Weight 	if (count > fw_priv->size - offset)
281e0c11a8bSRuss Weight 		count = fw_priv->size - offset;
282e0c11a8bSRuss Weight 
283e0c11a8bSRuss Weight 	ret_count = count;
284e0c11a8bSRuss Weight 
285e0c11a8bSRuss Weight 	if (fw_priv->data)
286e0c11a8bSRuss Weight 		firmware_rw_data(fw_priv, buffer, offset, count, true);
287e0c11a8bSRuss Weight 	else
288e0c11a8bSRuss Weight 		firmware_rw(fw_priv, buffer, offset, count, true);
289e0c11a8bSRuss Weight 
290e0c11a8bSRuss Weight out:
291e0c11a8bSRuss Weight 	mutex_unlock(&fw_lock);
292e0c11a8bSRuss Weight 	return ret_count;
293e0c11a8bSRuss Weight }
294e0c11a8bSRuss Weight 
fw_realloc_pages(struct fw_sysfs * fw_sysfs,int min_size)295e0c11a8bSRuss Weight static int fw_realloc_pages(struct fw_sysfs *fw_sysfs, int min_size)
296e0c11a8bSRuss Weight {
297e0c11a8bSRuss Weight 	int err;
298e0c11a8bSRuss Weight 
299e0c11a8bSRuss Weight 	err = fw_grow_paged_buf(fw_sysfs->fw_priv,
300e0c11a8bSRuss Weight 				PAGE_ALIGN(min_size) >> PAGE_SHIFT);
301e0c11a8bSRuss Weight 	if (err)
302e0c11a8bSRuss Weight 		fw_load_abort(fw_sysfs);
303e0c11a8bSRuss Weight 	return err;
304e0c11a8bSRuss Weight }
305e0c11a8bSRuss Weight 
306e0c11a8bSRuss Weight /**
307e0c11a8bSRuss Weight  * firmware_data_write() - write method for firmware
308e0c11a8bSRuss Weight  * @filp: open sysfs file
309e0c11a8bSRuss Weight  * @kobj: kobject for the device
310e0c11a8bSRuss Weight  * @bin_attr: bin_attr structure
311e0c11a8bSRuss Weight  * @buffer: buffer being written
312e0c11a8bSRuss Weight  * @offset: buffer offset for write in total data store area
313e0c11a8bSRuss Weight  * @count: buffer size
314e0c11a8bSRuss Weight  *
315e0c11a8bSRuss Weight  *	Data written to the 'data' attribute will be later handed to
316e0c11a8bSRuss Weight  *	the driver as a firmware image.
317e0c11a8bSRuss Weight  **/
firmware_data_write(struct file * filp,struct kobject * kobj,struct bin_attribute * bin_attr,char * buffer,loff_t offset,size_t count)318e0c11a8bSRuss Weight static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
319e0c11a8bSRuss Weight 				   struct bin_attribute *bin_attr,
320e0c11a8bSRuss Weight 				   char *buffer, loff_t offset, size_t count)
321e0c11a8bSRuss Weight {
322e0c11a8bSRuss Weight 	struct device *dev = kobj_to_dev(kobj);
323e0c11a8bSRuss Weight 	struct fw_sysfs *fw_sysfs = to_fw_sysfs(dev);
324e0c11a8bSRuss Weight 	struct fw_priv *fw_priv;
325e0c11a8bSRuss Weight 	ssize_t retval;
326e0c11a8bSRuss Weight 
327e0c11a8bSRuss Weight 	if (!capable(CAP_SYS_RAWIO))
328e0c11a8bSRuss Weight 		return -EPERM;
329e0c11a8bSRuss Weight 
330e0c11a8bSRuss Weight 	mutex_lock(&fw_lock);
331e0c11a8bSRuss Weight 	fw_priv = fw_sysfs->fw_priv;
332e0c11a8bSRuss Weight 	if (!fw_priv || fw_state_is_done(fw_priv)) {
333e0c11a8bSRuss Weight 		retval = -ENODEV;
334e0c11a8bSRuss Weight 		goto out;
335e0c11a8bSRuss Weight 	}
336e0c11a8bSRuss Weight 
337e0c11a8bSRuss Weight 	if (fw_priv->data) {
338e0c11a8bSRuss Weight 		if (offset + count > fw_priv->allocated_size) {
339e0c11a8bSRuss Weight 			retval = -ENOMEM;
340e0c11a8bSRuss Weight 			goto out;
341e0c11a8bSRuss Weight 		}
342e0c11a8bSRuss Weight 		firmware_rw_data(fw_priv, buffer, offset, count, false);
343e0c11a8bSRuss Weight 		retval = count;
344e0c11a8bSRuss Weight 	} else {
345e0c11a8bSRuss Weight 		retval = fw_realloc_pages(fw_sysfs, offset + count);
346e0c11a8bSRuss Weight 		if (retval)
347e0c11a8bSRuss Weight 			goto out;
348e0c11a8bSRuss Weight 
349e0c11a8bSRuss Weight 		retval = count;
350e0c11a8bSRuss Weight 		firmware_rw(fw_priv, buffer, offset, count, false);
351e0c11a8bSRuss Weight 	}
352e0c11a8bSRuss Weight 
353e0c11a8bSRuss Weight 	fw_priv->size = max_t(size_t, offset + count, fw_priv->size);
354e0c11a8bSRuss Weight out:
355e0c11a8bSRuss Weight 	mutex_unlock(&fw_lock);
356e0c11a8bSRuss Weight 	return retval;
357e0c11a8bSRuss Weight }
358e0c11a8bSRuss Weight 
359e0c11a8bSRuss Weight static struct bin_attribute firmware_attr_data = {
360e0c11a8bSRuss Weight 	.attr = { .name = "data", .mode = 0644 },
361e0c11a8bSRuss Weight 	.size = 0,
362e0c11a8bSRuss Weight 	.read = firmware_data_read,
363e0c11a8bSRuss Weight 	.write = firmware_data_write,
364e0c11a8bSRuss Weight };
365e0c11a8bSRuss Weight 
366e0c11a8bSRuss Weight static struct attribute *fw_dev_attrs[] = {
367e0c11a8bSRuss Weight 	&dev_attr_loading.attr,
368536fd818SRuss Weight #ifdef CONFIG_FW_UPLOAD
369536fd818SRuss Weight 	&dev_attr_cancel.attr,
370536fd818SRuss Weight 	&dev_attr_status.attr,
371536fd818SRuss Weight 	&dev_attr_error.attr,
372536fd818SRuss Weight 	&dev_attr_remaining_size.attr,
373536fd818SRuss Weight #endif
374e0c11a8bSRuss Weight 	NULL
375e0c11a8bSRuss Weight };
376e0c11a8bSRuss Weight 
377e0c11a8bSRuss Weight static struct bin_attribute *fw_dev_bin_attrs[] = {
378e0c11a8bSRuss Weight 	&firmware_attr_data,
379e0c11a8bSRuss Weight 	NULL
380e0c11a8bSRuss Weight };
381e0c11a8bSRuss Weight 
382e0c11a8bSRuss Weight static const struct attribute_group fw_dev_attr_group = {
383e0c11a8bSRuss Weight 	.attrs = fw_dev_attrs,
384e0c11a8bSRuss Weight 	.bin_attrs = fw_dev_bin_attrs,
385536fd818SRuss Weight #ifdef CONFIG_FW_UPLOAD
386536fd818SRuss Weight 	.is_visible = fw_upload_is_visible,
387536fd818SRuss Weight #endif
388e0c11a8bSRuss Weight };
389e0c11a8bSRuss Weight 
390e0c11a8bSRuss Weight static const struct attribute_group *fw_dev_attr_groups[] = {
391e0c11a8bSRuss Weight 	&fw_dev_attr_group,
392e0c11a8bSRuss Weight 	NULL
393e0c11a8bSRuss Weight };
394e0c11a8bSRuss Weight 
395e0c11a8bSRuss Weight struct fw_sysfs *
fw_create_instance(struct firmware * firmware,const char * fw_name,struct device * device,u32 opt_flags)396e0c11a8bSRuss Weight fw_create_instance(struct firmware *firmware, const char *fw_name,
397e0c11a8bSRuss Weight 		   struct device *device, u32 opt_flags)
398e0c11a8bSRuss Weight {
399e0c11a8bSRuss Weight 	struct fw_sysfs *fw_sysfs;
400e0c11a8bSRuss Weight 	struct device *f_dev;
401e0c11a8bSRuss Weight 
402e0c11a8bSRuss Weight 	fw_sysfs = kzalloc(sizeof(*fw_sysfs), GFP_KERNEL);
403e0c11a8bSRuss Weight 	if (!fw_sysfs) {
404e0c11a8bSRuss Weight 		fw_sysfs = ERR_PTR(-ENOMEM);
405e0c11a8bSRuss Weight 		goto exit;
406e0c11a8bSRuss Weight 	}
407e0c11a8bSRuss Weight 
408e0c11a8bSRuss Weight 	fw_sysfs->nowait = !!(opt_flags & FW_OPT_NOWAIT);
409e0c11a8bSRuss Weight 	fw_sysfs->fw = firmware;
410e0c11a8bSRuss Weight 	f_dev = &fw_sysfs->dev;
411e0c11a8bSRuss Weight 
412e0c11a8bSRuss Weight 	device_initialize(f_dev);
413e0c11a8bSRuss Weight 	dev_set_name(f_dev, "%s", fw_name);
414e0c11a8bSRuss Weight 	f_dev->parent = device;
415e0c11a8bSRuss Weight 	f_dev->class = &firmware_class;
416e0c11a8bSRuss Weight 	f_dev->groups = fw_dev_attr_groups;
417e0c11a8bSRuss Weight exit:
418e0c11a8bSRuss Weight 	return fw_sysfs;
419e0c11a8bSRuss Weight }
420