firmware_class.c 14 KB

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