blob: d494ce9288c37e9b59c34962c66b291f7185a313 [file] [log] [blame]
Uwe Kleine-König4d80d592008-05-31 11:37:27 +02001/*
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
Hans J. Kocha6fcc3a2008-09-18 23:03:07 +020015#define DRIVER_NAME "uio_pdrv"
Uwe Kleine-König4d80d592008-05-31 11:37:27 +020016
17struct uio_platdata {
18 struct uio_info *uioinfo;
19};
20
21static int uio_pdrv_probe(struct platform_device *pdev)
22{
23 struct uio_info *uioinfo = pdev->dev.platform_data;
24 struct uio_platdata *pdata;
25 struct uio_mem *uiomem;
26 int ret = -ENODEV;
27 int i;
28
29 if (!uioinfo || !uioinfo->name || !uioinfo->version) {
30 dev_dbg(&pdev->dev, "%s: err_uioinfo\n", __func__);
31 goto err_uioinfo;
32 }
33
34 pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
35 if (!pdata) {
36 ret = -ENOMEM;
37 dev_dbg(&pdev->dev, "%s: err_alloc_pdata\n", __func__);
38 goto err_alloc_pdata;
39 }
40
41 pdata->uioinfo = uioinfo;
42
43 uiomem = &uioinfo->mem[0];
44
45 for (i = 0; i < pdev->num_resources; ++i) {
46 struct resource *r = &pdev->resource[i];
47
48 if (r->flags != IORESOURCE_MEM)
49 continue;
50
51 if (uiomem >= &uioinfo->mem[MAX_UIO_MAPS]) {
52 dev_warn(&pdev->dev, "device has more than "
53 __stringify(MAX_UIO_MAPS)
54 " I/O memory resources.\n");
55 break;
56 }
57
58 uiomem->memtype = UIO_MEM_PHYS;
59 uiomem->addr = r->start;
60 uiomem->size = r->end - r->start + 1;
61 ++uiomem;
62 }
63
64 while (uiomem < &uioinfo->mem[MAX_UIO_MAPS]) {
65 uiomem->size = 0;
66 ++uiomem;
67 }
68
69 pdata->uioinfo->priv = pdata;
70
71 ret = uio_register_device(&pdev->dev, pdata->uioinfo);
72
73 if (ret) {
74 kfree(pdata);
75err_alloc_pdata:
76err_uioinfo:
77 return ret;
78 }
79
80 platform_set_drvdata(pdev, pdata);
81
82 return 0;
83}
84
85static int uio_pdrv_remove(struct platform_device *pdev)
86{
87 struct uio_platdata *pdata = platform_get_drvdata(pdev);
88
89 uio_unregister_device(pdata->uioinfo);
90
Uwe Kleine-König331995e2008-07-11 11:10:36 +020091 kfree(pdata);
92
Uwe Kleine-König4d80d592008-05-31 11:37:27 +020093 return 0;
94}
95
96static struct platform_driver uio_pdrv = {
97 .probe = uio_pdrv_probe,
98 .remove = uio_pdrv_remove,
99 .driver = {
100 .name = DRIVER_NAME,
101 .owner = THIS_MODULE,
102 },
103};
104
105static int __init uio_pdrv_init(void)
106{
107 return platform_driver_register(&uio_pdrv);
108}
109
110static void __exit uio_pdrv_exit(void)
111{
112 platform_driver_unregister(&uio_pdrv);
113}
114module_init(uio_pdrv_init);
115module_exit(uio_pdrv_exit);
116
117MODULE_AUTHOR("Uwe Kleine-Koenig");
118MODULE_DESCRIPTION("Userspace I/O platform driver");
Uwe Kleine-König0f909272008-07-11 11:10:37 +0200119MODULE_LICENSE("GPL v2");
Uwe Kleine-König4d80d592008-05-31 11:37:27 +0200120MODULE_ALIAS("platform:" DRIVER_NAME);