From 28357d61e0ec39851ca88a09b04ce876b8758448 Mon Sep 17 00:00:00 2001 From: John Ogness Date: Fri, 15 Feb 2019 12:11:27 +0100 Subject: [PATCH 2/7] hello: change from platform to pci bus Signed-off-by: John Ogness --- hello.c | 89 +++++++++++++++++++++++++++++++---------------------------------- 1 file changed, 43 insertions(+), 46 deletions(-) diff --git a/hello.c b/hello.c index 7f78d06..db50302 100644 --- a/hello.c +++ b/hello.c @@ -5,9 +5,8 @@ #include #include #include -#include +#include #include -#include struct hello_dev { struct device *dev; @@ -77,7 +76,7 @@ static const struct file_operations hello_fops = { .write = hello_write, }; -static int hello_probe(struct platform_device *pdev) +static int hello_probe(struct pci_dev *pdev, const struct pci_device_id *ent) { struct hello_dev *hello; struct device *dev; @@ -90,15 +89,7 @@ static int hello_probe(struct platform_device *pdev) return -ENOMEM; } -#ifdef CONFIG_OF - ret = of_property_read_u32(pdev->dev.of_node, "index", &hello->minor); - if (ret < 0) { - dev_err(&pdev->dev, "no index specified\n"); - goto err_out; - } -#else - hello->minor = (unsigned)pdev->id; -#endif + hello->minor = 1; if (hello->minor >= HELLO_MAX_DEVICES) { dev_err(&pdev->dev, "invalid index: %u\n", hello->minor); @@ -107,7 +98,27 @@ static int hello_probe(struct platform_device *pdev) } hello->dev = &pdev->dev; - platform_set_drvdata(pdev, hello); + pci_set_drvdata(pdev, hello); + + ret = pci_enable_device(pdev); + if (ret) { + dev_err(&pdev->dev, "failed to enable device\n"); + goto err_drvdata; + } + + if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) { + dev_err(&pdev->dev, "failed to find base address\n"); + ret = -ENODEV; + goto err_enable; + } + + ret = pci_request_regions(pdev, "hello"); + if (ret) { + dev_err(&pdev->dev, "failed to request resources\n"); + goto err_enable; + } + + pci_set_master(pdev); devt = MKDEV(MAJOR(hello_devt), hello->minor); @@ -116,7 +127,7 @@ static int hello_probe(struct platform_device *pdev) ret = cdev_add(&hello->cdev, devt, 1); if (ret != 0) { dev_err(&pdev->dev, "cdev_add failed\n"); - goto err_drvdata; + goto err_regions; } dev = device_create(hello_class, &pdev->dev, devt, hello, @@ -133,43 +144,41 @@ static int hello_probe(struct platform_device *pdev) err_cdev: cdev_del(&hello->cdev); +err_regions: + pci_release_regions(pdev); +err_enable: + pci_disable_device(pdev); err_drvdata: - platform_set_drvdata(pdev, NULL); -err_out: + pci_set_drvdata(pdev, NULL); return ret; } -static int hello_remove(struct platform_device *pdev) +static void hello_remove(struct pci_dev *pdev) { - struct hello_dev *hello = platform_get_drvdata(pdev); + struct hello_dev *hello = pci_get_drvdata(pdev); device_destroy(hello_class, MKDEV(MAJOR(hello_devt), hello->minor)); cdev_del(&hello->cdev); - platform_set_drvdata(pdev, NULL); + pci_release_regions(pdev); + pci_disable_device(pdev); + pci_set_drvdata(pdev, NULL); dev_info(&pdev->dev, "GOODBYE! I was hello device %d!\n", hello->minor); - - return 0; } -static const struct of_device_id hello_match[] = { - { .compatible = "virtual,hello", }, +static const struct pci_device_id hello_id_table[] = { + { 0x1af4, 0x1110, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID, 0 }, { /* end of table */ } }; +MODULE_DEVICE_TABLE(pci, hello_id_table); -static struct platform_driver hello_driver = { - .driver = { - .name = "hello", - .of_match_table = hello_match, - }, +static struct pci_driver hello_driver = { + .name = "hello", + .id_table = hello_id_table, .probe = hello_probe, .remove = hello_remove, }; -#ifndef CONFIG_OF -static struct platform_device *pdevs[3]; -#endif - static int __init hello_init(void) { int ret; @@ -187,16 +196,10 @@ static int __init hello_init(void) goto err_region; } - ret = platform_driver_register(&hello_driver); + ret = pci_register_driver(&hello_driver); if (ret != 0) goto err_class; -#ifndef CONFIG_OF - pdevs[0] = platform_device_register_simple("hello", 1, NULL, 0); - pdevs[1] = platform_device_register_simple("hello", 3, NULL, 0); - pdevs[2] = platform_device_register_simple("hello", 5, NULL, 0); -#endif - return 0; err_class: @@ -210,13 +213,7 @@ static void __exit hello_exit(void) { printk(KERN_INFO "%s\n", __func__); -#ifndef CONFIG_OF - platform_device_unregister(pdevs[0]); - platform_device_unregister(pdevs[1]); - platform_device_unregister(pdevs[2]); -#endif - - platform_driver_unregister(&hello_driver); + pci_unregister_driver(&hello_driver); class_destroy(hello_class); unregister_chrdev_region(hello_devt, HELLO_MAX_DEVICES); } -- 2.11.0