firmware_class.c 14 KB

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