uio_pdrv.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * drivers/uio/uio_pdrv.c
  3. *
  4. * Copyright (C) 2008 by Digi International Inc.
  5. * All rights reserved.
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License version 2 as published by
  9. * the Free Software Foundation.
  10. */
  11. #include <linux/platform_device.h>
  12. #include <linux/uio_driver.h>
  13. #include <linux/stringify.h>
  14. #define DRIVER_NAME "uio"
  15. struct uio_platdata {
  16. struct uio_info *uioinfo;
  17. };
  18. static int uio_pdrv_probe(struct platform_device *pdev)
  19. {
  20. struct uio_info *uioinfo = pdev->dev.platform_data;
  21. struct uio_platdata *pdata;
  22. struct uio_mem *uiomem;
  23. int ret = -ENODEV;
  24. int i;
  25. if (!uioinfo || !uioinfo->name || !uioinfo->version) {
  26. dev_dbg(&pdev->dev, "%s: err_uioinfo\n", __func__);
  27. goto err_uioinfo;
  28. }
  29. pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
  30. if (!pdata) {
  31. ret = -ENOMEM;
  32. dev_dbg(&pdev->dev, "%s: err_alloc_pdata\n", __func__);
  33. goto err_alloc_pdata;
  34. }
  35. pdata->uioinfo = uioinfo;
  36. uiomem = &uioinfo->mem[0];
  37. for (i = 0; i < pdev->num_resources; ++i) {
  38. struct resource *r = &pdev->resource[i];
  39. if (r->flags != IORESOURCE_MEM)
  40. continue;
  41. if (uiomem >= &uioinfo->mem[MAX_UIO_MAPS]) {
  42. dev_warn(&pdev->dev, "device has more than "
  43. __stringify(MAX_UIO_MAPS)
  44. " I/O memory resources.\n");
  45. break;
  46. }
  47. uiomem->memtype = UIO_MEM_PHYS;
  48. uiomem->addr = r->start;
  49. uiomem->size = r->end - r->start + 1;
  50. ++uiomem;
  51. }
  52. while (uiomem < &uioinfo->mem[MAX_UIO_MAPS]) {
  53. uiomem->size = 0;
  54. ++uiomem;
  55. }
  56. pdata->uioinfo->priv = pdata;
  57. ret = uio_register_device(&pdev->dev, pdata->uioinfo);
  58. if (ret) {
  59. kfree(pdata);
  60. err_alloc_pdata:
  61. err_uioinfo:
  62. return ret;
  63. }
  64. platform_set_drvdata(pdev, pdata);
  65. return 0;
  66. }
  67. static int uio_pdrv_remove(struct platform_device *pdev)
  68. {
  69. struct uio_platdata *pdata = platform_get_drvdata(pdev);
  70. uio_unregister_device(pdata->uioinfo);
  71. kfree(pdata);
  72. return 0;
  73. }
  74. static struct platform_driver uio_pdrv = {
  75. .probe = uio_pdrv_probe,
  76. .remove = uio_pdrv_remove,
  77. .driver = {
  78. .name = DRIVER_NAME,
  79. .owner = THIS_MODULE,
  80. },
  81. };
  82. static int __init uio_pdrv_init(void)
  83. {
  84. return platform_driver_register(&uio_pdrv);
  85. }
  86. static void __exit uio_pdrv_exit(void)
  87. {
  88. platform_driver_unregister(&uio_pdrv);
  89. }
  90. module_init(uio_pdrv_init);
  91. module_exit(uio_pdrv_exit);
  92. MODULE_AUTHOR("Uwe Kleine-Koenig");
  93. MODULE_DESCRIPTION("Userspace I/O platform driver");
  94. MODULE_LICENSE("GPL v2");
  95. MODULE_ALIAS("platform:" DRIVER_NAME);