/drivers/base/firmware_class.c

https://bitbucket.org/evzijst/gittest · C · 583 lines · 424 code · 75 blank · 84 comment · 32 complexity · 1cb2d1d18a72cb07c8619206e0a23f24 MD5 · raw file

  1. /*
  2. * firmware_class.c - Multi purpose firmware loading support
  3. *
  4. * Copyright (c) 2003 Manuel Estrada Sainz <ranty@debian.org>
  5. *
  6. * Please see Documentation/firmware_class/ for more information.
  7. *
  8. */
  9. #include <linux/device.h>
  10. #include <linux/module.h>
  11. #include <linux/init.h>
  12. #include <linux/timer.h>
  13. #include <linux/vmalloc.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/bitops.h>
  16. #include <asm/semaphore.h>
  17. #include <linux/firmware.h>
  18. #include "base.h"
  19. MODULE_AUTHOR("Manuel Estrada Sainz <ranty@debian.org>");
  20. MODULE_DESCRIPTION("Multi purpose firmware loading support");
  21. MODULE_LICENSE("GPL");
  22. enum {
  23. FW_STATUS_LOADING,
  24. FW_STATUS_DONE,
  25. FW_STATUS_ABORT,
  26. FW_STATUS_READY,
  27. };
  28. static int loading_timeout = 10; /* In seconds */
  29. /* fw_lock could be moved to 'struct firmware_priv' but since it is just
  30. * guarding for corner cases a global lock should be OK */
  31. static DECLARE_MUTEX(fw_lock);
  32. struct firmware_priv {
  33. char fw_id[FIRMWARE_NAME_MAX];
  34. struct completion completion;
  35. struct bin_attribute attr_data;
  36. struct firmware *fw;
  37. unsigned long status;
  38. int alloc_size;
  39. struct timer_list timeout;
  40. };
  41. static inline void
  42. fw_load_abort(struct firmware_priv *fw_priv)
  43. {
  44. set_bit(FW_STATUS_ABORT, &fw_priv->status);
  45. wmb();
  46. complete(&fw_priv->completion);
  47. }
  48. static ssize_t
  49. firmware_timeout_show(struct class *class, char *buf)
  50. {
  51. return sprintf(buf, "%d\n", loading_timeout);
  52. }
  53. /**
  54. * firmware_timeout_store:
  55. * Description:
  56. * Sets the number of seconds to wait for the firmware. Once
  57. * this expires an error will be return to the driver and no
  58. * firmware will be provided.
  59. *
  60. * Note: zero means 'wait for ever'
  61. *
  62. **/
  63. static ssize_t
  64. firmware_timeout_store(struct class *class, const char *buf, size_t count)
  65. {
  66. loading_timeout = simple_strtol(buf, NULL, 10);
  67. return count;
  68. }
  69. static CLASS_ATTR(timeout, 0644, firmware_timeout_show, firmware_timeout_store);
  70. static void fw_class_dev_release(struct class_device *class_dev);
  71. int firmware_class_hotplug(struct class_device *dev, char **envp,
  72. int num_envp, char *buffer, int buffer_size);
  73. static struct class firmware_class = {
  74. .name = "firmware",
  75. .hotplug = firmware_class_hotplug,
  76. .release = fw_class_dev_release,
  77. };
  78. int
  79. firmware_class_hotplug(struct class_device *class_dev, char **envp,
  80. int num_envp, char *buffer, int buffer_size)
  81. {
  82. struct firmware_priv *fw_priv = class_get_devdata(class_dev);
  83. int i = 0, len = 0;
  84. if (!test_bit(FW_STATUS_READY, &fw_priv->status))
  85. return -ENODEV;
  86. if (add_hotplug_env_var(envp, num_envp, &i, buffer, buffer_size, &len,
  87. "FIRMWARE=%s", fw_priv->fw_id))
  88. return -ENOMEM;
  89. envp[i] = NULL;
  90. return 0;
  91. }
  92. static ssize_t
  93. firmware_loading_show(struct class_device *class_dev, char *buf)
  94. {
  95. struct firmware_priv *fw_priv = class_get_devdata(class_dev);
  96. int loading = test_bit(FW_STATUS_LOADING, &fw_priv->status);
  97. return sprintf(buf, "%d\n", loading);
  98. }
  99. /**
  100. * firmware_loading_store: - loading control file
  101. * Description:
  102. * The relevant values are:
  103. *
  104. * 1: Start a load, discarding any previous partial load.
  105. * 0: Conclude the load and handle the data to the driver code.
  106. * -1: Conclude the load with an error and discard any written data.
  107. **/
  108. static ssize_t
  109. firmware_loading_store(struct class_device *class_dev,
  110. const char *buf, size_t count)
  111. {
  112. struct firmware_priv *fw_priv = class_get_devdata(class_dev);
  113. int loading = simple_strtol(buf, NULL, 10);
  114. switch (loading) {
  115. case 1:
  116. down(&fw_lock);
  117. vfree(fw_priv->fw->data);
  118. fw_priv->fw->data = NULL;
  119. fw_priv->fw->size = 0;
  120. fw_priv->alloc_size = 0;
  121. set_bit(FW_STATUS_LOADING, &fw_priv->status);
  122. up(&fw_lock);
  123. break;
  124. case 0:
  125. if (test_bit(FW_STATUS_LOADING, &fw_priv->status)) {
  126. complete(&fw_priv->completion);
  127. clear_bit(FW_STATUS_LOADING, &fw_priv->status);
  128. break;
  129. }
  130. /* fallthrough */
  131. default:
  132. printk(KERN_ERR "%s: unexpected value (%d)\n", __FUNCTION__,
  133. loading);
  134. /* fallthrough */
  135. case -1:
  136. fw_load_abort(fw_priv);
  137. break;
  138. }
  139. return count;
  140. }
  141. static CLASS_DEVICE_ATTR(loading, 0644,
  142. firmware_loading_show, firmware_loading_store);
  143. static ssize_t
  144. firmware_data_read(struct kobject *kobj,
  145. char *buffer, loff_t offset, size_t count)
  146. {
  147. struct class_device *class_dev = to_class_dev(kobj);
  148. struct firmware_priv *fw_priv = class_get_devdata(class_dev);
  149. struct firmware *fw;
  150. ssize_t ret_count = count;
  151. down(&fw_lock);
  152. fw = fw_priv->fw;
  153. if (test_bit(FW_STATUS_DONE, &fw_priv->status)) {
  154. ret_count = -ENODEV;
  155. goto out;
  156. }
  157. if (offset > fw->size) {
  158. ret_count = 0;
  159. goto out;
  160. }
  161. if (offset + ret_count > fw->size)
  162. ret_count = fw->size - offset;
  163. memcpy(buffer, fw->data + offset, ret_count);
  164. out:
  165. up(&fw_lock);
  166. return ret_count;
  167. }
  168. static int
  169. fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
  170. {
  171. u8 *new_data;
  172. if (min_size <= fw_priv->alloc_size)
  173. return 0;
  174. new_data = vmalloc(fw_priv->alloc_size + PAGE_SIZE);
  175. if (!new_data) {
  176. printk(KERN_ERR "%s: unable to alloc buffer\n", __FUNCTION__);
  177. /* Make sure that we don't keep incomplete data */
  178. fw_load_abort(fw_priv);
  179. return -ENOMEM;
  180. }
  181. fw_priv->alloc_size += PAGE_SIZE;
  182. if (fw_priv->fw->data) {
  183. memcpy(new_data, fw_priv->fw->data, fw_priv->fw->size);
  184. vfree(fw_priv->fw->data);
  185. }
  186. fw_priv->fw->data = new_data;
  187. BUG_ON(min_size > fw_priv->alloc_size);
  188. return 0;
  189. }
  190. /**
  191. * firmware_data_write:
  192. *
  193. * Description:
  194. *
  195. * Data written to the 'data' attribute will be later handled to
  196. * the driver as a firmware image.
  197. **/
  198. static ssize_t
  199. firmware_data_write(struct kobject *kobj,
  200. char *buffer, loff_t offset, size_t count)
  201. {
  202. struct class_device *class_dev = to_class_dev(kobj);
  203. struct firmware_priv *fw_priv = class_get_devdata(class_dev);
  204. struct firmware *fw;
  205. ssize_t retval;
  206. if (!capable(CAP_SYS_RAWIO))
  207. return -EPERM;
  208. down(&fw_lock);
  209. fw = fw_priv->fw;
  210. if (test_bit(FW_STATUS_DONE, &fw_priv->status)) {
  211. retval = -ENODEV;
  212. goto out;
  213. }
  214. retval = fw_realloc_buffer(fw_priv, offset + count);
  215. if (retval)
  216. goto out;
  217. memcpy(fw->data + offset, buffer, count);
  218. fw->size = max_t(size_t, offset + count, fw->size);
  219. retval = count;
  220. out:
  221. up(&fw_lock);
  222. return retval;
  223. }
  224. static struct bin_attribute firmware_attr_data_tmpl = {
  225. .attr = {.name = "data", .mode = 0644, .owner = THIS_MODULE},
  226. .size = 0,
  227. .read = firmware_data_read,
  228. .write = firmware_data_write,
  229. };
  230. static void
  231. fw_class_dev_release(struct class_device *class_dev)
  232. {
  233. struct firmware_priv *fw_priv = class_get_devdata(class_dev);
  234. kfree(fw_priv);
  235. kfree(class_dev);
  236. module_put(THIS_MODULE);
  237. }
  238. static void
  239. firmware_class_timeout(u_long data)
  240. {
  241. struct firmware_priv *fw_priv = (struct firmware_priv *) data;
  242. fw_load_abort(fw_priv);
  243. }
  244. static inline void
  245. fw_setup_class_device_id(struct class_device *class_dev, struct device *dev)
  246. {
  247. /* XXX warning we should watch out for name collisions */
  248. strlcpy(class_dev->class_id, dev->bus_id, BUS_ID_SIZE);
  249. }
  250. static int
  251. fw_register_class_device(struct class_device **class_dev_p,
  252. const char *fw_name, struct device *device)
  253. {
  254. int retval;
  255. struct firmware_priv *fw_priv = kmalloc(sizeof (struct firmware_priv),
  256. GFP_KERNEL);
  257. struct class_device *class_dev = kmalloc(sizeof (struct class_device),
  258. GFP_KERNEL);
  259. *class_dev_p = NULL;
  260. if (!fw_priv || !class_dev) {
  261. printk(KERN_ERR "%s: kmalloc failed\n", __FUNCTION__);
  262. retval = -ENOMEM;
  263. goto error_kfree;
  264. }
  265. memset(fw_priv, 0, sizeof (*fw_priv));
  266. memset(class_dev, 0, sizeof (*class_dev));
  267. init_completion(&fw_priv->completion);
  268. fw_priv->attr_data = firmware_attr_data_tmpl;
  269. strlcpy(fw_priv->fw_id, fw_name, FIRMWARE_NAME_MAX);
  270. fw_priv->timeout.function = firmware_class_timeout;
  271. fw_priv->timeout.data = (u_long) fw_priv;
  272. init_timer(&fw_priv->timeout);
  273. fw_setup_class_device_id(class_dev, device);
  274. class_dev->dev = device;
  275. class_dev->class = &firmware_class;
  276. class_set_devdata(class_dev, fw_priv);
  277. retval = class_device_register(class_dev);
  278. if (retval) {
  279. printk(KERN_ERR "%s: class_device_register failed\n",
  280. __FUNCTION__);
  281. goto error_kfree;
  282. }
  283. *class_dev_p = class_dev;
  284. return 0;
  285. error_kfree:
  286. kfree(fw_priv);
  287. kfree(class_dev);
  288. return retval;
  289. }
  290. static int
  291. fw_setup_class_device(struct firmware *fw, struct class_device **class_dev_p,
  292. const char *fw_name, struct device *device)
  293. {
  294. struct class_device *class_dev;
  295. struct firmware_priv *fw_priv;
  296. int retval;
  297. *class_dev_p = NULL;
  298. retval = fw_register_class_device(&class_dev, fw_name, device);
  299. if (retval)
  300. goto out;
  301. /* Need to pin this module until class device is destroyed */
  302. __module_get(THIS_MODULE);
  303. fw_priv = class_get_devdata(class_dev);
  304. fw_priv->fw = fw;
  305. retval = sysfs_create_bin_file(&class_dev->kobj, &fw_priv->attr_data);
  306. if (retval) {
  307. printk(KERN_ERR "%s: sysfs_create_bin_file failed\n",
  308. __FUNCTION__);
  309. goto error_unreg;
  310. }
  311. retval = class_device_create_file(class_dev,
  312. &class_device_attr_loading);
  313. if (retval) {
  314. printk(KERN_ERR "%s: class_device_create_file failed\n",
  315. __FUNCTION__);
  316. goto error_unreg;
  317. }
  318. set_bit(FW_STATUS_READY, &fw_priv->status);
  319. *class_dev_p = class_dev;
  320. goto out;
  321. error_unreg:
  322. class_device_unregister(class_dev);
  323. out:
  324. return retval;
  325. }
  326. /**
  327. * request_firmware: - request firmware to hotplug and wait for it
  328. * Description:
  329. * @firmware will be used to return a firmware image by the name
  330. * of @name for device @device.
  331. *
  332. * Should be called from user context where sleeping is allowed.
  333. *
  334. * @name will be use as $FIRMWARE in the hotplug environment and
  335. * should be distinctive enough not to be confused with any other
  336. * firmware image for this or any other device.
  337. **/
  338. int
  339. request_firmware(const struct firmware **firmware_p, const char *name,
  340. struct device *device)
  341. {
  342. struct class_device *class_dev;
  343. struct firmware_priv *fw_priv;
  344. struct firmware *firmware;
  345. int retval;
  346. if (!firmware_p)
  347. return -EINVAL;
  348. *firmware_p = firmware = kmalloc(sizeof (struct firmware), GFP_KERNEL);
  349. if (!firmware) {
  350. printk(KERN_ERR "%s: kmalloc(struct firmware) failed\n",
  351. __FUNCTION__);
  352. retval = -ENOMEM;
  353. goto out;
  354. }
  355. memset(firmware, 0, sizeof (*firmware));
  356. retval = fw_setup_class_device(firmware, &class_dev, name, device);
  357. if (retval)
  358. goto error_kfree_fw;
  359. fw_priv = class_get_devdata(class_dev);
  360. if (loading_timeout) {
  361. fw_priv->timeout.expires = jiffies + loading_timeout * HZ;
  362. add_timer(&fw_priv->timeout);
  363. }
  364. kobject_hotplug(&class_dev->kobj, KOBJ_ADD);
  365. wait_for_completion(&fw_priv->completion);
  366. set_bit(FW_STATUS_DONE, &fw_priv->status);
  367. del_timer_sync(&fw_priv->timeout);
  368. down(&fw_lock);
  369. if (!fw_priv->fw->size || test_bit(FW_STATUS_ABORT, &fw_priv->status)) {
  370. retval = -ENOENT;
  371. release_firmware(fw_priv->fw);
  372. *firmware_p = NULL;
  373. }
  374. fw_priv->fw = NULL;
  375. up(&fw_lock);
  376. class_device_unregister(class_dev);
  377. goto out;
  378. error_kfree_fw:
  379. kfree(firmware);
  380. *firmware_p = NULL;
  381. out:
  382. return retval;
  383. }
  384. /**
  385. * release_firmware: - release the resource associated with a firmware image
  386. **/
  387. void
  388. release_firmware(const struct firmware *fw)
  389. {
  390. if (fw) {
  391. vfree(fw->data);
  392. kfree(fw);
  393. }
  394. }
  395. /**
  396. * register_firmware: - provide a firmware image for later usage
  397. *
  398. * Description:
  399. * Make sure that @data will be available by requesting firmware @name.
  400. *
  401. * Note: This will not be possible until some kind of persistence
  402. * is available.
  403. **/
  404. void
  405. register_firmware(const char *name, const u8 *data, size_t size)
  406. {
  407. /* This is meaningless without firmware caching, so until we
  408. * decide if firmware caching is reasonable just leave it as a
  409. * noop */
  410. }
  411. /* Async support */
  412. struct firmware_work {
  413. struct work_struct work;
  414. struct module *module;
  415. const char *name;
  416. struct device *device;
  417. void *context;
  418. void (*cont)(const struct firmware *fw, void *context);
  419. };
  420. static int
  421. request_firmware_work_func(void *arg)
  422. {
  423. struct firmware_work *fw_work = arg;
  424. const struct firmware *fw;
  425. if (!arg) {
  426. WARN_ON(1);
  427. return 0;
  428. }
  429. daemonize("%s/%s", "firmware", fw_work->name);
  430. request_firmware(&fw, fw_work->name, fw_work->device);
  431. fw_work->cont(fw, fw_work->context);
  432. release_firmware(fw);
  433. module_put(fw_work->module);
  434. kfree(fw_work);
  435. return 0;
  436. }
  437. /**
  438. * request_firmware_nowait:
  439. *
  440. * Description:
  441. * Asynchronous variant of request_firmware() for contexts where
  442. * it is not possible to sleep.
  443. *
  444. * @cont will be called asynchronously when the firmware request is over.
  445. *
  446. * @context will be passed over to @cont.
  447. *
  448. * @fw may be %NULL if firmware request fails.
  449. *
  450. **/
  451. int
  452. request_firmware_nowait(
  453. struct module *module,
  454. const char *name, struct device *device, void *context,
  455. void (*cont)(const struct firmware *fw, void *context))
  456. {
  457. struct firmware_work *fw_work = kmalloc(sizeof (struct firmware_work),
  458. GFP_ATOMIC);
  459. int ret;
  460. if (!fw_work)
  461. return -ENOMEM;
  462. if (!try_module_get(module)) {
  463. kfree(fw_work);
  464. return -EFAULT;
  465. }
  466. *fw_work = (struct firmware_work) {
  467. .module = module,
  468. .name = name,
  469. .device = device,
  470. .context = context,
  471. .cont = cont,
  472. };
  473. ret = kernel_thread(request_firmware_work_func, fw_work,
  474. CLONE_FS | CLONE_FILES);
  475. if (ret < 0) {
  476. fw_work->cont(NULL, fw_work->context);
  477. return ret;
  478. }
  479. return 0;
  480. }
  481. static int __init
  482. firmware_class_init(void)
  483. {
  484. int error;
  485. error = class_register(&firmware_class);
  486. if (error) {
  487. printk(KERN_ERR "%s: class_register failed\n", __FUNCTION__);
  488. return error;
  489. }
  490. error = class_create_file(&firmware_class, &class_attr_timeout);
  491. if (error) {
  492. printk(KERN_ERR "%s: class_create_file failed\n",
  493. __FUNCTION__);
  494. class_unregister(&firmware_class);
  495. }
  496. return error;
  497. }
  498. static void __exit
  499. firmware_class_exit(void)
  500. {
  501. class_unregister(&firmware_class);
  502. }
  503. module_init(firmware_class_init);
  504. module_exit(firmware_class_exit);
  505. EXPORT_SYMBOL(release_firmware);
  506. EXPORT_SYMBOL(request_firmware);
  507. EXPORT_SYMBOL(request_firmware_nowait);
  508. EXPORT_SYMBOL(register_firmware);