firmware_class.c 14 KB

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