Guest User

Untitled

a guest
Aug 4th, 2020
72
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3.  * Firmware security data kernel module
  4.  *
  5.  * Copyright (C) 2020 Daniel Gutson <daniel.gutson@eclypsium.com>
  6.  * Copyright (C) 2020 Eclypsium Inc.
  7.  */
  8. #include <linux/kobject.h>
  9. #include <linux/sysfs.h>
  10. #include <linux/module.h>
  11. #include <linux/init.h>
  12. #include <linux/list.h>
  13. #include <linux/slab.h>
  14. #include <linux/device.h>
  15. #include <linux/pci.h>
  16.  
  17.  
  18. static ssize_t howareyou_show(struct class *class, struct class_attribute *attr,
  19.                 char *buf)
  20. {
  21.     return sprintf(buf, "%s\n", "How are you?");
  22. }
  23. static CLASS_ATTR_RO(howareyou);
  24.  
  25. static struct class my_class = {
  26.         .name =         "my-class",
  27.         .owner =        THIS_MODULE,
  28. };
  29.  
  30. struct device* child_device;
  31.  
  32.  
  33. static int mypci_probe(struct pci_dev *pdev,
  34.                    const struct pci_device_id *id)
  35. {
  36.     int ret;
  37.  
  38.     ret = pcim_enable_device(pdev);
  39.     if (ret)
  40.         return ret;
  41.  
  42.     ret = class_register(&my_class);
  43.     if (ret < 0)
  44.         return ret;
  45.  
  46.  
  47.     pr_info("DFG: Recognized. DID: %lx\n", (unsigned long int)id->driver_data);
  48.     pr_info("DFG: device DID: %lx\n", (unsigned long int)pdev->device);
  49.  
  50.     ret = class_create_file(&my_class, &class_attr_howareyou);
  51.     if (ret != 0)
  52.     {
  53.         pr_err("DFG class create file error: %d\n", ret);
  54.         class_unregister(&my_class);
  55.         return ret;
  56.     }
  57.  
  58.     child_device = device_create(&my_class, &pdev->dev, MKDEV(0, 0), NULL, "child");
  59.     if (child_device == NULL)
  60.     {
  61.         pr_err("DFG error child device NULL");
  62.     }
  63.  
  64.     return ret;
  65. }
  66.  
  67.  
  68. static void mypci_remove(struct pci_dev *pdev)
  69. {
  70.     /* I tried enabling and disabling this
  71.     if (child_device != NULL)
  72.     {
  73.         put_device(child_device);
  74.         device_unregister(child_device);
  75.     }
  76.     */
  77.  
  78.     class_remove_file(&my_class, &class_attr_howareyou);
  79.     class_unregister(&my_class);
  80. }
  81.  
  82. static const struct pci_device_id my_dids[] = {
  83.     { PCI_VDEVICE(INTEL, 0xa30e), (unsigned long)0xa30e },
  84.     { PCI_VDEVICE(INTEL, 0xa324), (unsigned long)0xa324 },
  85.     { },
  86. };
  87. MODULE_DEVICE_TABLE(pci, my_dids);
  88.  
  89. static struct pci_driver my_pci_driver = {
  90.     .name = "dfg-pci",
  91.     .id_table = my_dids,
  92.     .probe = mypci_probe,
  93.     .remove = mypci_remove,
  94. };
  95.  
  96. module_pci_driver(my_pci_driver);
  97.  
  98. MODULE_LICENSE("GPL v2");
  99. MODULE_AUTHOR("Daniel Gutson <daniel.gutson@eclypsium.com>");
  100.  
RAW Paste Data