Linux Audio

Check our new training course

Loading...
v5.9
  1// SPDX-License-Identifier: GPL-2.0
  2/* uio_pci_generic - generic UIO driver for PCI 2.3 devices
  3 *
  4 * Copyright (C) 2009 Red Hat, Inc.
  5 * Author: Michael S. Tsirkin <mst@redhat.com>
  6 *
 
 
  7 * Since the driver does not declare any device ids, you must allocate
  8 * id and bind the device to the driver yourself.  For example:
  9 *
 10 * # echo "8086 10f5" > /sys/bus/pci/drivers/uio_pci_generic/new_id
 11 * # echo -n 0000:00:19.0 > /sys/bus/pci/drivers/e1000e/unbind
 12 * # echo -n 0000:00:19.0 > /sys/bus/pci/drivers/uio_pci_generic/bind
 13 * # ls -l /sys/bus/pci/devices/0000:00:19.0/driver
 14 * .../0000:00:19.0/driver -> ../../../bus/pci/drivers/uio_pci_generic
 15 *
 16 * Driver won't bind to devices which do not support the Interrupt Disable Bit
 17 * in the command register. All devices compliant to PCI 2.3 (circa 2002) and
 18 * all compliant PCI Express devices should support this bit.
 19 */
 20
 21#include <linux/device.h>
 22#include <linux/module.h>
 23#include <linux/pci.h>
 24#include <linux/slab.h>
 25#include <linux/uio_driver.h>
 
 26
 27#define DRIVER_VERSION	"0.01.0"
 28#define DRIVER_AUTHOR	"Michael S. Tsirkin <mst@redhat.com>"
 29#define DRIVER_DESC	"Generic UIO driver for PCI 2.3 devices"
 30
 31struct uio_pci_generic_dev {
 32	struct uio_info info;
 33	struct pci_dev *pdev;
 
 34};
 35
 36static inline struct uio_pci_generic_dev *
 37to_uio_pci_generic_dev(struct uio_info *info)
 38{
 39	return container_of(info, struct uio_pci_generic_dev, info);
 40}
 41
 42static int release(struct uio_info *info, struct inode *inode)
 43{
 44	struct uio_pci_generic_dev *gdev = to_uio_pci_generic_dev(info);
 45
 46	/*
 47	 * This driver is insecure when used with devices doing DMA, but some
 48	 * people (mis)use it with such devices.
 49	 * Let's at least make sure DMA isn't left enabled after the userspace
 50	 * driver closes the fd.
 51	 * Note that there's a non-zero chance doing this will wedge the device
 52	 * at least until reset.
 53	 */
 54	pci_clear_master(gdev->pdev);
 55	return 0;
 56}
 57
 58/* Interrupt handler. Read/modify/write the command register to disable
 59 * the interrupt. */
 60static irqreturn_t irqhandler(int irq, struct uio_info *info)
 61{
 62	struct uio_pci_generic_dev *gdev = to_uio_pci_generic_dev(info);
 63
 64	if (!pci_check_and_mask_intx(gdev->pdev))
 65		return IRQ_NONE;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 66
 67	/* UIO core will signal the user process. */
 68	return IRQ_HANDLED;
 
 
 
 
 
 69}
 70
 71static int probe(struct pci_dev *pdev,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 72			   const struct pci_device_id *id)
 73{
 74	struct uio_pci_generic_dev *gdev;
 75	int err;
 76
 77	err = pci_enable_device(pdev);
 78	if (err) {
 79		dev_err(&pdev->dev, "%s: pci_enable_device failed: %d\n",
 80			__func__, err);
 81		return err;
 82	}
 83
 84	if (pdev->irq && !pci_intx_mask_supported(pdev)) {
 85		err = -ENODEV;
 86		goto err_verify;
 
 
 87	}
 88
 
 
 
 
 89	gdev = kzalloc(sizeof(struct uio_pci_generic_dev), GFP_KERNEL);
 90	if (!gdev) {
 91		err = -ENOMEM;
 92		goto err_alloc;
 93	}
 94
 95	gdev->info.name = "uio_pci_generic";
 96	gdev->info.version = DRIVER_VERSION;
 97	gdev->info.release = release;
 
 
 98	gdev->pdev = pdev;
 99	if (pdev->irq) {
100		gdev->info.irq = pdev->irq;
101		gdev->info.irq_flags = IRQF_SHARED;
102		gdev->info.handler = irqhandler;
103	} else {
104		dev_warn(&pdev->dev, "No IRQ assigned to device: "
105			 "no support for interrupts?\n");
106	}
107
108	err = uio_register_device(&pdev->dev, &gdev->info);
109	if (err)
110		goto err_register;
111	pci_set_drvdata(pdev, gdev);
112
113	return 0;
114err_register:
115	kfree(gdev);
116err_alloc:
117err_verify:
118	pci_disable_device(pdev);
119	return err;
120}
121
122static void remove(struct pci_dev *pdev)
123{
124	struct uio_pci_generic_dev *gdev = pci_get_drvdata(pdev);
125
126	uio_unregister_device(&gdev->info);
127	pci_disable_device(pdev);
128	kfree(gdev);
129}
130
131static struct pci_driver uio_pci_driver = {
132	.name = "uio_pci_generic",
133	.id_table = NULL, /* only dynamic id's */
134	.probe = probe,
135	.remove = remove,
136};
137
138module_pci_driver(uio_pci_driver);
 
 
 
 
 
 
 
 
 
 
 
 
 
139MODULE_VERSION(DRIVER_VERSION);
140MODULE_LICENSE("GPL v2");
141MODULE_AUTHOR(DRIVER_AUTHOR);
142MODULE_DESCRIPTION(DRIVER_DESC);
v3.1
 
  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>
 25#include <linux/slab.h>
 26#include <linux/uio_driver.h>
 27#include <linux/spinlock.h>
 28
 29#define DRIVER_VERSION	"0.01.0"
 30#define DRIVER_AUTHOR	"Michael S. Tsirkin <mst@redhat.com>"
 31#define DRIVER_DESC	"Generic UIO driver for PCI 2.3 devices"
 32
 33struct uio_pci_generic_dev {
 34	struct uio_info info;
 35	struct pci_dev *pdev;
 36	spinlock_t lock; /* guards command register accesses */
 37};
 38
 39static inline struct uio_pci_generic_dev *
 40to_uio_pci_generic_dev(struct uio_info *info)
 41{
 42	return container_of(info, struct uio_pci_generic_dev, info);
 43}
 44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 45/* Interrupt handler. Read/modify/write the command register to disable
 46 * the interrupt. */
 47static irqreturn_t irqhandler(int irq, struct uio_info *info)
 48{
 49	struct uio_pci_generic_dev *gdev = to_uio_pci_generic_dev(info);
 50	struct pci_dev *pdev = gdev->pdev;
 51	irqreturn_t ret = IRQ_NONE;
 52	u32 cmd_status_dword;
 53	u16 origcmd, newcmd, status;
 54
 55	/* We do a single dword read to retrieve both command and status.
 56	 * Document assumptions that make this possible. */
 57	BUILD_BUG_ON(PCI_COMMAND % 4);
 58	BUILD_BUG_ON(PCI_COMMAND + 2 != PCI_STATUS);
 59
 60	spin_lock_irq(&gdev->lock);
 61	pci_block_user_cfg_access(pdev);
 62
 63	/* Read both command and status registers in a single 32-bit operation.
 64	 * Note: we could cache the value for command and move the status read
 65	 * out of the lock if there was a way to get notified of user changes
 66	 * to command register through sysfs. Should be good for shared irqs. */
 67	pci_read_config_dword(pdev, PCI_COMMAND, &cmd_status_dword);
 68	origcmd = cmd_status_dword;
 69	status = cmd_status_dword >> 16;
 70
 71	/* Check interrupt status register to see whether our device
 72	 * triggered the interrupt. */
 73	if (!(status & PCI_STATUS_INTERRUPT))
 74		goto done;
 75
 76	/* We triggered the interrupt, disable it. */
 77	newcmd = origcmd | PCI_COMMAND_INTX_DISABLE;
 78	if (newcmd != origcmd)
 79		pci_write_config_word(pdev, PCI_COMMAND, newcmd);
 80
 81	/* UIO core will signal the user process. */
 82	ret = IRQ_HANDLED;
 83done:
 84
 85	pci_unblock_user_cfg_access(pdev);
 86	spin_unlock_irq(&gdev->lock);
 87	return ret;
 88}
 89
 90/* Verify that the device supports Interrupt Disable bit in command register,
 91 * per PCI 2.3, by flipping this bit and reading it back: this bit was readonly
 92 * in PCI 2.2. */
 93static int __devinit verify_pci_2_3(struct pci_dev *pdev)
 94{
 95	u16 orig, new;
 96	int err = 0;
 97
 98	pci_block_user_cfg_access(pdev);
 99	pci_read_config_word(pdev, PCI_COMMAND, &orig);
100	pci_write_config_word(pdev, PCI_COMMAND,
101			      orig ^ PCI_COMMAND_INTX_DISABLE);
102	pci_read_config_word(pdev, PCI_COMMAND, &new);
103	/* There's no way to protect against
104	 * hardware bugs or detect them reliably, but as long as we know
105	 * what the value should be, let's go ahead and check it. */
106	if ((new ^ orig) & ~PCI_COMMAND_INTX_DISABLE) {
107		err = -EBUSY;
108		dev_err(&pdev->dev, "Command changed from 0x%x to 0x%x: "
109			"driver or HW bug?\n", orig, new);
110		goto err;
111	}
112	if (!((new ^ orig) & PCI_COMMAND_INTX_DISABLE)) {
113		dev_warn(&pdev->dev, "Device does not support "
114			 "disabling interrupts: unable to bind.\n");
115		err = -ENODEV;
116		goto err;
117	}
118	/* Now restore the original value. */
119	pci_write_config_word(pdev, PCI_COMMAND, orig);
120err:
121	pci_unblock_user_cfg_access(pdev);
122	return err;
123}
124
125static int __devinit probe(struct pci_dev *pdev,
126			   const struct pci_device_id *id)
127{
128	struct uio_pci_generic_dev *gdev;
129	int err;
130
131	err = pci_enable_device(pdev);
132	if (err) {
133		dev_err(&pdev->dev, "%s: pci_enable_device failed: %d\n",
134			__func__, err);
135		return err;
136	}
137
138	if (!pdev->irq) {
139		dev_warn(&pdev->dev, "No IRQ assigned to device: "
140			 "no support for interrupts?\n");
141		pci_disable_device(pdev);
142		return -ENODEV;
143	}
144
145	err = verify_pci_2_3(pdev);
146	if (err)
147		goto err_verify;
148
149	gdev = kzalloc(sizeof(struct uio_pci_generic_dev), GFP_KERNEL);
150	if (!gdev) {
151		err = -ENOMEM;
152		goto err_alloc;
153	}
154
155	gdev->info.name = "uio_pci_generic";
156	gdev->info.version = DRIVER_VERSION;
157	gdev->info.irq = pdev->irq;
158	gdev->info.irq_flags = IRQF_SHARED;
159	gdev->info.handler = irqhandler;
160	gdev->pdev = pdev;
161	spin_lock_init(&gdev->lock);
 
 
 
 
 
 
 
162
163	if (uio_register_device(&pdev->dev, &gdev->info))
 
164		goto err_register;
165	pci_set_drvdata(pdev, gdev);
166
167	return 0;
168err_register:
169	kfree(gdev);
170err_alloc:
171err_verify:
172	pci_disable_device(pdev);
173	return err;
174}
175
176static void remove(struct pci_dev *pdev)
177{
178	struct uio_pci_generic_dev *gdev = pci_get_drvdata(pdev);
179
180	uio_unregister_device(&gdev->info);
181	pci_disable_device(pdev);
182	kfree(gdev);
183}
184
185static struct pci_driver driver = {
186	.name = "uio_pci_generic",
187	.id_table = NULL, /* only dynamic id's */
188	.probe = probe,
189	.remove = remove,
190};
191
192static int __init init(void)
193{
194	pr_info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
195	return pci_register_driver(&driver);
196}
197
198static void __exit cleanup(void)
199{
200	pci_unregister_driver(&driver);
201}
202
203module_init(init);
204module_exit(cleanup);
205
206MODULE_VERSION(DRIVER_VERSION);
207MODULE_LICENSE("GPL v2");
208MODULE_AUTHOR(DRIVER_AUTHOR);
209MODULE_DESCRIPTION(DRIVER_DESC);