firmware_class.c 14 KB

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