firmware_class.c 14 KB

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