Michael S. Tsirkin | ccb86a6 | 2009-07-20 10:29:34 +0300 | [diff] [blame] | 1 | /* uio_pci_generic - generic UIO driver for PCI 2.3 devices |
| 2 | * |
| 3 | * Copyright (C) 2009 Red Hat, Inc. |
| 4 | * Author: Michael S. Tsirkin <mst@redhat.com> |
| 5 | * |
| 6 | * This work is licensed under the terms of the GNU GPL, version 2. |
| 7 | * |
| 8 | * Since the driver does not declare any device ids, you must allocate |
| 9 | * id and bind the device to the driver yourself. For example: |
| 10 | * |
| 11 | * # echo "8086 10f5" > /sys/bus/pci/drivers/uio_pci_generic/new_id |
| 12 | * # echo -n 0000:00:19.0 > /sys/bus/pci/drivers/e1000e/unbind |
| 13 | * # echo -n 0000:00:19.0 > /sys/bus/pci/drivers/uio_pci_generic/bind |
| 14 | * # ls -l /sys/bus/pci/devices/0000:00:19.0/driver |
| 15 | * .../0000:00:19.0/driver -> ../../../bus/pci/drivers/uio_pci_generic |
| 16 | * |
| 17 | * Driver won't bind to devices which do not support the Interrupt Disable Bit |
| 18 | * in the command register. All devices compliant to PCI 2.3 (circa 2002) and |
| 19 | * all compliant PCI Express devices should support this bit. |
| 20 | */ |
| 21 | |
| 22 | #include <linux/device.h> |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/pci.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 25 | #include <linux/slab.h> |
Michael S. Tsirkin | ccb86a6 | 2009-07-20 10:29:34 +0300 | [diff] [blame] | 26 | #include <linux/uio_driver.h> |
Michael S. Tsirkin | ccb86a6 | 2009-07-20 10:29:34 +0300 | [diff] [blame] | 27 | |
| 28 | #define DRIVER_VERSION "0.01.0" |
| 29 | #define DRIVER_AUTHOR "Michael S. Tsirkin <mst@redhat.com>" |
| 30 | #define DRIVER_DESC "Generic UIO driver for PCI 2.3 devices" |
| 31 | |
| 32 | struct uio_pci_generic_dev { |
| 33 | struct uio_info info; |
| 34 | struct pci_dev *pdev; |
Michael S. Tsirkin | ccb86a6 | 2009-07-20 10:29:34 +0300 | [diff] [blame] | 35 | }; |
| 36 | |
| 37 | static inline struct uio_pci_generic_dev * |
| 38 | to_uio_pci_generic_dev(struct uio_info *info) |
| 39 | { |
| 40 | return container_of(info, struct uio_pci_generic_dev, info); |
| 41 | } |
| 42 | |
| 43 | /* Interrupt handler. Read/modify/write the command register to disable |
| 44 | * the interrupt. */ |
| 45 | static irqreturn_t irqhandler(int irq, struct uio_info *info) |
| 46 | { |
| 47 | struct uio_pci_generic_dev *gdev = to_uio_pci_generic_dev(info); |
Michael S. Tsirkin | ccb86a6 | 2009-07-20 10:29:34 +0300 | [diff] [blame] | 48 | |
Jan Kiszka | 2502dbd | 2011-11-04 09:46:01 +0100 | [diff] [blame] | 49 | if (!pci_check_and_mask_intx(gdev->pdev)) |
| 50 | return IRQ_NONE; |
Michael S. Tsirkin | ccb86a6 | 2009-07-20 10:29:34 +0300 | [diff] [blame] | 51 | |
| 52 | /* UIO core will signal the user process. */ |
Jan Kiszka | 2502dbd | 2011-11-04 09:46:01 +0100 | [diff] [blame] | 53 | return IRQ_HANDLED; |
Michael S. Tsirkin | ccb86a6 | 2009-07-20 10:29:34 +0300 | [diff] [blame] | 54 | } |
| 55 | |
Bill Pemberton | b17b75b | 2012-11-19 13:21:49 -0500 | [diff] [blame] | 56 | static int probe(struct pci_dev *pdev, |
Michael S. Tsirkin | ccb86a6 | 2009-07-20 10:29:34 +0300 | [diff] [blame] | 57 | const struct pci_device_id *id) |
| 58 | { |
| 59 | struct uio_pci_generic_dev *gdev; |
| 60 | int err; |
| 61 | |
Michael S. Tsirkin | ccb86a6 | 2009-07-20 10:29:34 +0300 | [diff] [blame] | 62 | err = pci_enable_device(pdev); |
| 63 | if (err) { |
| 64 | dev_err(&pdev->dev, "%s: pci_enable_device failed: %d\n", |
| 65 | __func__, err); |
| 66 | return err; |
| 67 | } |
| 68 | |
Jim Harris | acec09e | 2017-05-02 07:20:59 -0700 | [diff] [blame] | 69 | if (pdev->irq && !pci_intx_mask_supported(pdev)) { |
Jan Kiszka | 2502dbd | 2011-11-04 09:46:01 +0100 | [diff] [blame] | 70 | err = -ENODEV; |
Michael S. Tsirkin | ccb86a6 | 2009-07-20 10:29:34 +0300 | [diff] [blame] | 71 | goto err_verify; |
Jan Kiszka | 2502dbd | 2011-11-04 09:46:01 +0100 | [diff] [blame] | 72 | } |
Michael S. Tsirkin | ccb86a6 | 2009-07-20 10:29:34 +0300 | [diff] [blame] | 73 | |
| 74 | gdev = kzalloc(sizeof(struct uio_pci_generic_dev), GFP_KERNEL); |
| 75 | if (!gdev) { |
| 76 | err = -ENOMEM; |
| 77 | goto err_alloc; |
| 78 | } |
| 79 | |
| 80 | gdev->info.name = "uio_pci_generic"; |
| 81 | gdev->info.version = DRIVER_VERSION; |
Michael S. Tsirkin | ccb86a6 | 2009-07-20 10:29:34 +0300 | [diff] [blame] | 82 | gdev->pdev = pdev; |
Jim Harris | acec09e | 2017-05-02 07:20:59 -0700 | [diff] [blame] | 83 | if (pdev->irq) { |
| 84 | gdev->info.irq = pdev->irq; |
| 85 | gdev->info.irq_flags = IRQF_SHARED; |
| 86 | gdev->info.handler = irqhandler; |
| 87 | } else { |
| 88 | dev_warn(&pdev->dev, "No IRQ assigned to device: " |
| 89 | "no support for interrupts?\n"); |
| 90 | } |
Michael S. Tsirkin | ccb86a6 | 2009-07-20 10:29:34 +0300 | [diff] [blame] | 91 | |
Alexey Khoroshilov | c4277e9 | 2014-12-06 00:28:01 +0300 | [diff] [blame] | 92 | err = uio_register_device(&pdev->dev, &gdev->info); |
| 93 | if (err) |
Michael S. Tsirkin | ccb86a6 | 2009-07-20 10:29:34 +0300 | [diff] [blame] | 94 | goto err_register; |
| 95 | pci_set_drvdata(pdev, gdev); |
| 96 | |
| 97 | return 0; |
| 98 | err_register: |
| 99 | kfree(gdev); |
| 100 | err_alloc: |
| 101 | err_verify: |
| 102 | pci_disable_device(pdev); |
| 103 | return err; |
| 104 | } |
| 105 | |
| 106 | static void remove(struct pci_dev *pdev) |
| 107 | { |
| 108 | struct uio_pci_generic_dev *gdev = pci_get_drvdata(pdev); |
| 109 | |
| 110 | uio_unregister_device(&gdev->info); |
| 111 | pci_disable_device(pdev); |
| 112 | kfree(gdev); |
| 113 | } |
| 114 | |
Peter Huewe | cd43739 | 2013-05-21 22:47:44 +0200 | [diff] [blame] | 115 | static struct pci_driver uio_pci_driver = { |
Michael S. Tsirkin | ccb86a6 | 2009-07-20 10:29:34 +0300 | [diff] [blame] | 116 | .name = "uio_pci_generic", |
| 117 | .id_table = NULL, /* only dynamic id's */ |
| 118 | .probe = probe, |
| 119 | .remove = remove, |
| 120 | }; |
| 121 | |
Peter Huewe | cd43739 | 2013-05-21 22:47:44 +0200 | [diff] [blame] | 122 | module_pci_driver(uio_pci_driver); |
Michael S. Tsirkin | ccb86a6 | 2009-07-20 10:29:34 +0300 | [diff] [blame] | 123 | MODULE_VERSION(DRIVER_VERSION); |
| 124 | MODULE_LICENSE("GPL v2"); |
| 125 | MODULE_AUTHOR(DRIVER_AUTHOR); |
| 126 | MODULE_DESCRIPTION(DRIVER_DESC); |