firmware_class.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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. * @buffer: buffer being written
  207. * @offset: buffer offset for write in total data store area
  208. * @count: buffer size
  209. *
  210. * Data written to the 'data' attribute will be later handed to
  211. * the driver as a firmware image.
  212. **/
  213. static ssize_t
  214. firmware_data_write(struct kobject *kobj, struct bin_attribute *bin_attr,
  215. char *buffer, loff_t offset, size_t count)
  216. {
  217. struct device *dev = to_dev(kobj);
  218. struct firmware_priv *fw_priv = dev_get_drvdata(dev);
  219. struct firmware *fw;
  220. ssize_t retval;
  221. if (!capable(CAP_SYS_RAWIO))
  222. return -EPERM;
  223. mutex_lock(&fw_lock);
  224. fw = fw_priv->fw;
  225. if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
  226. retval = -ENODEV;
  227. goto out;
  228. }
  229. retval = fw_realloc_buffer(fw_priv, offset + count);
  230. if (retval)
  231. goto out;
  232. memcpy(fw->data + offset, buffer, count);
  233. fw->size = max_t(size_t, offset + count, fw->size);
  234. retval = count;
  235. out:
  236. mutex_unlock(&fw_lock);
  237. return retval;
  238. }
  239. static struct bin_attribute firmware_attr_data_tmpl = {
  240. .attr = {.name = "data", .mode = 0644},
  241. .size = 0,
  242. .read = firmware_data_read,
  243. .write = firmware_data_write,
  244. };
  245. static void fw_dev_release(struct device *dev)
  246. {
  247. struct firmware_priv *fw_priv = dev_get_drvdata(dev);
  248. kfree(fw_priv);
  249. kfree(dev);
  250. module_put(THIS_MODULE);
  251. }
  252. static void
  253. firmware_class_timeout(u_long data)
  254. {
  255. struct firmware_priv *fw_priv = (struct firmware_priv *) data;
  256. fw_load_abort(fw_priv);
  257. }
  258. static inline void fw_setup_device_id(struct device *f_dev, struct device *dev)
  259. {
  260. /* XXX warning we should watch out for name collisions */
  261. strlcpy(f_dev->bus_id, dev->bus_id, BUS_ID_SIZE);
  262. }
  263. static int fw_register_device(struct device **dev_p, const char *fw_name,
  264. struct device *device)
  265. {
  266. int retval;
  267. struct firmware_priv *fw_priv = kzalloc(sizeof(*fw_priv),
  268. GFP_KERNEL);
  269. struct device *f_dev = kzalloc(sizeof(*f_dev), GFP_KERNEL);
  270. *dev_p = NULL;
  271. if (!fw_priv || !f_dev) {
  272. printk(KERN_ERR "%s: kmalloc failed\n", __FUNCTION__);
  273. retval = -ENOMEM;
  274. goto error_kfree;
  275. }
  276. init_completion(&fw_priv->completion);
  277. fw_priv->attr_data = firmware_attr_data_tmpl;
  278. strlcpy(fw_priv->fw_id, fw_name, FIRMWARE_NAME_MAX);
  279. fw_priv->timeout.function = firmware_class_timeout;
  280. fw_priv->timeout.data = (u_long) fw_priv;
  281. init_timer(&fw_priv->timeout);
  282. fw_setup_device_id(f_dev, device);
  283. f_dev->parent = device;
  284. f_dev->class = &firmware_class;
  285. dev_set_drvdata(f_dev, fw_priv);
  286. f_dev->uevent_suppress = 1;
  287. retval = device_register(f_dev);
  288. if (retval) {
  289. printk(KERN_ERR "%s: device_register failed\n",
  290. __FUNCTION__);
  291. goto error_kfree;
  292. }
  293. *dev_p = f_dev;
  294. return 0;
  295. error_kfree:
  296. kfree(fw_priv);
  297. kfree(f_dev);
  298. return retval;
  299. }
  300. static int fw_setup_device(struct firmware *fw, struct device **dev_p,
  301. const char *fw_name, struct device *device,
  302. int uevent)
  303. {
  304. struct device *f_dev;
  305. struct firmware_priv *fw_priv;
  306. int retval;
  307. *dev_p = NULL;
  308. retval = fw_register_device(&f_dev, fw_name, device);
  309. if (retval)
  310. goto out;
  311. /* Need to pin this module until class device is destroyed */
  312. __module_get(THIS_MODULE);
  313. fw_priv = dev_get_drvdata(f_dev);
  314. fw_priv->fw = fw;
  315. retval = sysfs_create_bin_file(&f_dev->kobj, &fw_priv->attr_data);
  316. if (retval) {
  317. printk(KERN_ERR "%s: sysfs_create_bin_file failed\n",
  318. __FUNCTION__);
  319. goto error_unreg;
  320. }
  321. retval = device_create_file(f_dev, &dev_attr_loading);
  322. if (retval) {
  323. printk(KERN_ERR "%s: device_create_file failed\n",
  324. __FUNCTION__);
  325. goto error_unreg;
  326. }
  327. if (uevent)
  328. f_dev->uevent_suppress = 0;
  329. *dev_p = f_dev;
  330. goto out;
  331. error_unreg:
  332. device_unregister(f_dev);
  333. out:
  334. return retval;
  335. }
  336. static int
  337. _request_firmware(const struct firmware **firmware_p, const char *name,
  338. struct device *device, int uevent)
  339. {
  340. struct device *f_dev;
  341. struct firmware_priv *fw_priv;
  342. struct firmware *firmware;
  343. int retval;
  344. if (!firmware_p)
  345. return -EINVAL;
  346. *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
  347. if (!firmware) {
  348. printk(KERN_ERR "%s: kmalloc(struct firmware) failed\n",
  349. __FUNCTION__);
  350. retval = -ENOMEM;
  351. goto out;
  352. }
  353. retval = fw_setup_device(firmware, &f_dev, name, device, uevent);
  354. if (retval)
  355. goto error_kfree_fw;
  356. fw_priv = dev_get_drvdata(f_dev);
  357. if (uevent) {
  358. if (loading_timeout > 0) {
  359. fw_priv->timeout.expires = jiffies + loading_timeout * HZ;
  360. add_timer(&fw_priv->timeout);
  361. }
  362. kobject_uevent(&f_dev->kobj, KOBJ_ADD);
  363. wait_for_completion(&fw_priv->completion);
  364. set_bit(FW_STATUS_DONE, &fw_priv->status);
  365. del_timer_sync(&fw_priv->timeout);
  366. } else
  367. wait_for_completion(&fw_priv->completion);
  368. mutex_lock(&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. mutex_unlock(&fw_lock);
  376. device_unregister(f_dev);
  377. goto out;
  378. error_kfree_fw:
  379. kfree(firmware);
  380. *firmware_p = NULL;
  381. out:
  382. return retval;
  383. }
  384. /**
  385. * request_firmware: - send firmware request and wait for it
  386. * @firmware_p: pointer to firmware image
  387. * @name: name of firmware file
  388. * @device: device for which firmware is being loaded
  389. *
  390. * @firmware_p will be used to return a firmware image by the name
  391. * of @name for device @device.
  392. *
  393. * Should be called from user context where sleeping is allowed.
  394. *
  395. * @name will be used as $FIRMWARE in the uevent environment and
  396. * should be distinctive enough not to be confused with any other
  397. * firmware image for this or any other device.
  398. **/
  399. int
  400. request_firmware(const struct firmware **firmware_p, const char *name,
  401. struct device *device)
  402. {
  403. int uevent = 1;
  404. return _request_firmware(firmware_p, name, device, uevent);
  405. }
  406. /**
  407. * release_firmware: - release the resource associated with a firmware image
  408. * @fw: firmware resource to release
  409. **/
  410. void
  411. release_firmware(const struct firmware *fw)
  412. {
  413. if (fw) {
  414. vfree(fw->data);
  415. kfree(fw);
  416. }
  417. }
  418. /* Async support */
  419. struct firmware_work {
  420. struct work_struct work;
  421. struct module *module;
  422. const char *name;
  423. struct device *device;
  424. void *context;
  425. void (*cont)(const struct firmware *fw, void *context);
  426. int uevent;
  427. };
  428. static int
  429. request_firmware_work_func(void *arg)
  430. {
  431. struct firmware_work *fw_work = arg;
  432. const struct firmware *fw;
  433. int ret;
  434. if (!arg) {
  435. WARN_ON(1);
  436. return 0;
  437. }
  438. ret = _request_firmware(&fw, fw_work->name, fw_work->device,
  439. fw_work->uevent);
  440. if (ret < 0)
  441. fw_work->cont(NULL, fw_work->context);
  442. else {
  443. fw_work->cont(fw, fw_work->context);
  444. release_firmware(fw);
  445. }
  446. module_put(fw_work->module);
  447. kfree(fw_work);
  448. return ret;
  449. }
  450. /**
  451. * request_firmware_nowait: asynchronous version of request_firmware
  452. * @module: module requesting the firmware
  453. * @uevent: sends uevent to copy the firmware image if this flag
  454. * is non-zero else the firmware copy must be done manually.
  455. * @name: name of firmware file
  456. * @device: device for which firmware is being loaded
  457. * @context: will be passed over to @cont, and
  458. * @fw may be %NULL if firmware request fails.
  459. * @cont: function will be called asynchronously when the firmware
  460. * request is over.
  461. *
  462. * Asynchronous variant of request_firmware() for contexts where
  463. * it is not possible to sleep.
  464. **/
  465. int
  466. request_firmware_nowait(
  467. struct module *module, int uevent,
  468. const char *name, struct device *device, void *context,
  469. void (*cont)(const struct firmware *fw, void *context))
  470. {
  471. struct task_struct *task;
  472. struct firmware_work *fw_work = kmalloc(sizeof (struct firmware_work),
  473. GFP_ATOMIC);
  474. if (!fw_work)
  475. return -ENOMEM;
  476. if (!try_module_get(module)) {
  477. kfree(fw_work);
  478. return -EFAULT;
  479. }
  480. *fw_work = (struct firmware_work) {
  481. .module = module,
  482. .name = name,
  483. .device = device,
  484. .context = context,
  485. .cont = cont,
  486. .uevent = uevent,
  487. };
  488. task = kthread_run(request_firmware_work_func, fw_work,
  489. "firmware/%s", name);
  490. if (IS_ERR(task)) {
  491. fw_work->cont(NULL, fw_work->context);
  492. module_put(fw_work->module);
  493. kfree(fw_work);
  494. return PTR_ERR(task);
  495. }
  496. return 0;
  497. }
  498. static int __init
  499. firmware_class_init(void)
  500. {
  501. int error;
  502. error = class_register(&firmware_class);
  503. if (error) {
  504. printk(KERN_ERR "%s: class_register failed\n", __FUNCTION__);
  505. return error;
  506. }
  507. error = class_create_file(&firmware_class, &class_attr_timeout);
  508. if (error) {
  509. printk(KERN_ERR "%s: class_create_file failed\n",
  510. __FUNCTION__);
  511. class_unregister(&firmware_class);
  512. }
  513. return error;
  514. }
  515. static void __exit
  516. firmware_class_exit(void)
  517. {
  518. class_unregister(&firmware_class);
  519. }
  520. fs_initcall(firmware_class_init);
  521. module_exit(firmware_class_exit);
  522. EXPORT_SYMBOL(release_firmware);
  523. EXPORT_SYMBOL(request_firmware);
  524. EXPORT_SYMBOL(request_firmware_nowait);