firmware_class.c 14 KB

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