firmware_class.c 14 KB

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