Uwe Kleine-König | 4d80d59 | 2008-05-31 11:37:27 +0200 | [diff] [blame] | 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> |
Paul Gortmaker | 2949ead | 2011-07-17 16:38:40 -0400 | [diff] [blame] | 14 | #include <linux/module.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 15 | #include <linux/slab.h> |
Uwe Kleine-König | 4d80d59 | 2008-05-31 11:37:27 +0200 | [diff] [blame] | 16 | |
Hans J. Koch | a6fcc3a | 2008-09-18 23:03:07 +0200 | [diff] [blame] | 17 | #define DRIVER_NAME "uio_pdrv" |
Uwe Kleine-König | 4d80d59 | 2008-05-31 11:37:27 +0200 | [diff] [blame] | 18 | |
| 19 | struct uio_platdata { |
| 20 | struct uio_info *uioinfo; |
| 21 | }; |
| 22 | |
| 23 | static int uio_pdrv_probe(struct platform_device *pdev) |
| 24 | { |
| 25 | struct uio_info *uioinfo = pdev->dev.platform_data; |
| 26 | struct uio_platdata *pdata; |
| 27 | struct uio_mem *uiomem; |
| 28 | int ret = -ENODEV; |
| 29 | int i; |
| 30 | |
| 31 | if (!uioinfo || !uioinfo->name || !uioinfo->version) { |
| 32 | dev_dbg(&pdev->dev, "%s: err_uioinfo\n", __func__); |
| 33 | goto err_uioinfo; |
| 34 | } |
| 35 | |
| 36 | pdata = kzalloc(sizeof(*pdata), GFP_KERNEL); |
| 37 | if (!pdata) { |
| 38 | ret = -ENOMEM; |
| 39 | dev_dbg(&pdev->dev, "%s: err_alloc_pdata\n", __func__); |
| 40 | goto err_alloc_pdata; |
| 41 | } |
| 42 | |
| 43 | pdata->uioinfo = uioinfo; |
| 44 | |
| 45 | uiomem = &uioinfo->mem[0]; |
| 46 | |
| 47 | for (i = 0; i < pdev->num_resources; ++i) { |
| 48 | struct resource *r = &pdev->resource[i]; |
| 49 | |
| 50 | if (r->flags != IORESOURCE_MEM) |
| 51 | continue; |
| 52 | |
| 53 | if (uiomem >= &uioinfo->mem[MAX_UIO_MAPS]) { |
| 54 | dev_warn(&pdev->dev, "device has more than " |
| 55 | __stringify(MAX_UIO_MAPS) |
| 56 | " I/O memory resources.\n"); |
| 57 | break; |
| 58 | } |
| 59 | |
| 60 | uiomem->memtype = UIO_MEM_PHYS; |
| 61 | uiomem->addr = r->start; |
Joe Perches | 28f65c11 | 2011-06-09 09:13:32 -0700 | [diff] [blame] | 62 | uiomem->size = resource_size(r); |
Manuel Traut | ecd43c0 | 2012-11-09 07:06:40 +0100 | [diff] [blame] | 63 | uiomem->name = r->name; |
Uwe Kleine-König | 4d80d59 | 2008-05-31 11:37:27 +0200 | [diff] [blame] | 64 | ++uiomem; |
| 65 | } |
| 66 | |
| 67 | while (uiomem < &uioinfo->mem[MAX_UIO_MAPS]) { |
| 68 | uiomem->size = 0; |
| 69 | ++uiomem; |
| 70 | } |
| 71 | |
| 72 | pdata->uioinfo->priv = pdata; |
| 73 | |
| 74 | ret = uio_register_device(&pdev->dev, pdata->uioinfo); |
| 75 | |
| 76 | if (ret) { |
| 77 | kfree(pdata); |
| 78 | err_alloc_pdata: |
| 79 | err_uioinfo: |
| 80 | return ret; |
| 81 | } |
| 82 | |
| 83 | platform_set_drvdata(pdev, pdata); |
| 84 | |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | static int uio_pdrv_remove(struct platform_device *pdev) |
| 89 | { |
| 90 | struct uio_platdata *pdata = platform_get_drvdata(pdev); |
| 91 | |
| 92 | uio_unregister_device(pdata->uioinfo); |
| 93 | |
Uwe Kleine-König | 331995e | 2008-07-11 11:10:36 +0200 | [diff] [blame] | 94 | kfree(pdata); |
| 95 | |
Uwe Kleine-König | 4d80d59 | 2008-05-31 11:37:27 +0200 | [diff] [blame] | 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | static struct platform_driver uio_pdrv = { |
| 100 | .probe = uio_pdrv_probe, |
| 101 | .remove = uio_pdrv_remove, |
| 102 | .driver = { |
| 103 | .name = DRIVER_NAME, |
| 104 | .owner = THIS_MODULE, |
| 105 | }, |
| 106 | }; |
| 107 | |
Axel Lin | 11e3123 | 2011-11-26 15:18:55 +0800 | [diff] [blame] | 108 | module_platform_driver(uio_pdrv); |
Uwe Kleine-König | 4d80d59 | 2008-05-31 11:37:27 +0200 | [diff] [blame] | 109 | |
| 110 | MODULE_AUTHOR("Uwe Kleine-Koenig"); |
| 111 | MODULE_DESCRIPTION("Userspace I/O platform driver"); |
Uwe Kleine-König | 0f90927 | 2008-07-11 11:10:37 +0200 | [diff] [blame] | 112 | MODULE_LICENSE("GPL v2"); |
Uwe Kleine-König | 4d80d59 | 2008-05-31 11:37:27 +0200 | [diff] [blame] | 113 | MODULE_ALIAS("platform:" DRIVER_NAME); |