uio_pdrv.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. #include <linux/slab.h>
  15. #define DRIVER_NAME "uio_pdrv"
  16. struct uio_platdata {
  17. struct uio_info *uioinfo;
  18. };
  19. static int uio_pdrv_probe(struct platform_device *pdev)
  20. {
  21. struct uio_info *uioinfo = pdev->dev.platform_data;
  22. struct uio_platdata *pdata;
  23. struct uio_mem *uiomem;
  24. int ret = -ENODEV;
  25. int i;
  26. if (!uioinfo || !uioinfo->name || !uioinfo->version) {
  27. dev_dbg(&pdev->dev, "%s: err_uioinfo\n", __func__);
  28. goto err_uioinfo;
  29. }
  30. pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
  31. if (!pdata) {
  32. ret = -ENOMEM;
  33. dev_dbg(&pdev->dev, "%s: err_alloc_pdata\n", __func__);
  34. goto err_alloc_pdata;
  35. }
  36. pdata->uioinfo = uioinfo;
  37. uiomem = &uioinfo->mem[0];
  38. for (i = 0; i < pdev->num_resources; ++i) {
  39. struct resource *r = &pdev->resource[i];
  40. if (r->flags != IORESOURCE_MEM)
  41. continue;
  42. if (uiomem >= &uioinfo->mem[MAX_UIO_MAPS]) {
  43. dev_warn(&pdev->dev, "device has more than "
  44. __stringify(MAX_UIO_MAPS)
  45. " I/O memory resources.\n");
  46. break;
  47. }
  48. uiomem->memtype = UIO_MEM_PHYS;
  49. uiomem->addr = r->start;
  50. uiomem->size = r->end - r->start + 1;
  51. ++uiomem;
  52. }
  53. while (uiomem < &uioinfo->mem[MAX_UIO_MAPS]) {
  54. uiomem->size = 0;
  55. ++uiomem;
  56. }
  57. pdata->uioinfo->priv = pdata;
  58. ret = uio_register_device(&pdev->dev, pdata->uioinfo);
  59. if (ret) {
  60. kfree(pdata);
  61. err_alloc_pdata:
  62. err_uioinfo:
  63. return ret;
  64. }
  65. platform_set_drvdata(pdev, pdata);
  66. return 0;
  67. }
  68. static int uio_pdrv_remove(struct platform_device *pdev)
  69. {
  70. struct uio_platdata *pdata = platform_get_drvdata(pdev);
  71. uio_unregister_device(pdata->uioinfo);
  72. kfree(pdata);
  73. return 0;
  74. }
  75. static struct platform_driver uio_pdrv = {
  76. .probe = uio_pdrv_probe,
  77. .remove = uio_pdrv_remove,
  78. .driver = {
  79. .name = DRIVER_NAME,
  80. .owner = THIS_MODULE,
  81. },
  82. };
  83. static int __init uio_pdrv_init(void)
  84. {
  85. return platform_driver_register(&uio_pdrv);
  86. }
  87. static void __exit uio_pdrv_exit(void)
  88. {
  89. platform_driver_unregister(&uio_pdrv);
  90. }
  91. module_init(uio_pdrv_init);
  92. module_exit(uio_pdrv_exit);
  93. MODULE_AUTHOR("Uwe Kleine-Koenig");
  94. MODULE_DESCRIPTION("Userspace I/O platform driver");
  95. MODULE_LICENSE("GPL v2");
  96. MODULE_ALIAS("platform:" DRIVER_NAME);