diff options
author | Jon Smirl <jonsmirl@yahoo.com> | 2004-09-15 00:20:21 +0000 |
---|---|---|
committer | Jon Smirl <jonsmirl@yahoo.com> | 2004-09-15 00:20:21 +0000 |
commit | 64ef12c55cfca7885108950eb975c4d6625a0570 (patch) | |
tree | f0c0464df11507b8e53fce2010ebfeda534d09e1 /linux/drm_stub.h | |
parent | 03c2e674e43db5fcf4cb4b641005c329c7b343a9 (diff) |
Dynamic device minor support. Minor device numbers will be reused if the
device is hotplugged in/out of the system
Diffstat (limited to 'linux/drm_stub.h')
-rw-r--r-- | linux/drm_stub.h | 391 |
1 files changed, 236 insertions, 155 deletions
diff --git a/linux/drm_stub.h b/linux/drm_stub.h index 3d9ade35..8fd690d9 100644 --- a/linux/drm_stub.h +++ b/linux/drm_stub.h @@ -33,14 +33,18 @@ #include "drmP.h" -#define DRM_STUB_MAXCARDS 16 /* Enough for one machine */ +static unsigned int cards_limit = 16; /* Enough for one machine */ +static unsigned int debug = 0; /* 1 to enable debug output */ -/** Stub list. One for each minor. */ -static struct drm_stub_list { - const char *name; - struct file_operations *fops; /**< file operations */ - struct proc_dir_entry *dev_root; /**< proc directory entry */ -} *DRM(stub_list); +MODULE_AUTHOR( DRIVER_AUTHOR ); +MODULE_DESCRIPTION( DRIVER_DESC ); +MODULE_LICENSE("GPL and additional rights"); +module_param(cards_limit, int, 0444); +MODULE_PARM_DESC(cards_limit, "Maximum number of graphics cards"); +module_param(debug, int, 0644); +MODULE_PARM_DESC(debug, "Enable debug output"); + +drm_global_t *DRM(global); /** * File \c open operation. @@ -51,15 +55,24 @@ static struct drm_stub_list { * Puts the drm_stub_list::fops corresponding to the device minor number into * \p filp, call the \c open method, and restore the file operations. */ -static int DRM(stub_open)(struct inode *inode, struct file *filp) +static int stub_open(struct inode *inode, struct file *filp) { - int minor = iminor(inode); - int err = -ENODEV; + drm_device_t *dev = NULL; + int minor = iminor(inode); + int err = -ENODEV; struct file_operations *old_fops; + + DRM_DEBUG("\n"); + + if (!((minor >= 0) && (minor < DRM(global)->cards_limit))) + return -ENODEV; - if (!DRM(stub_list) || !DRM(stub_list)[minor].fops) return -ENODEV; - old_fops = filp->f_op; - filp->f_op = fops_get(DRM(stub_list)[minor].fops); + dev = DRM(global)->minors[minor].dev; + if (!dev) + return -ENODEV; + + old_fops = filp->f_op; + filp->f_op = fops_get(dev->fops); if (filp->f_op->open && (err = filp->f_op->open(inode, filp))) { fops_put(filp->f_op); filp->f_op = fops_get(old_fops); @@ -72,23 +85,24 @@ static int DRM(stub_open)(struct inode *inode, struct file *filp) /** File operations structure */ static struct file_operations DRM(stub_fops) = { .owner = THIS_MODULE, - .open = DRM(stub_open) + .open = stub_open }; #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0) -static int drm_hotplug (struct class_device *dev, char **envp, int num_envp, +static int drm_hotplug (struct class_device *cdev, char **envp, int num_envp, char *buffer, int buffer_size) { + drm_device_t *dev; struct pci_dev *pdev; char *scratch; int i = 0; int length = 0; - + DRM_DEBUG("\n"); - if (!dev) + if (!cdev) return -ENODEV; - pdev = to_pci_dev(dev->dev); + pdev = to_pci_dev(cdev->dev); if (!pdev) return -ENODEV; @@ -127,19 +141,17 @@ static int drm_hotplug (struct class_device *dev, char **envp, int num_envp, return -ENOMEM; ++length; scratch += length; -#if 0 - drm_device_t *ddev; - ddev = pci_get_drvdata(pdev); - if (ddev) { + + dev = pci_get_drvdata(pdev); + if (dev) { envp[i++] = scratch; length += snprintf (scratch, buffer_size - length, - "RESET=%s", (ddev->need_reset ? "true" : "false")); + "RESET=%s", (dev->need_reset ? "true" : "false")); if ((buffer_size - length <= 0) || (i >= num_envp)) return -ENOMEM; } -#endif envp[i] = 0; - + return 0; } #endif @@ -156,33 +168,105 @@ static int drm_hotplug (struct class_device *dev, char **envp, int num_envp, * empty entry and initialize it to the given parameters, and create the proc * init entry via proc_init(). */ -static int DRM(stub_getminor)(const char *name, struct file_operations *fops, - drm_device_t *dev) +static int get_minor(struct pci_dev *pdev, const struct pci_device_id *ent) { - int i; + struct class_device *dev_class; + drm_device_t *dev; + int ret; + int minor; + drm_minor_t *minors = &DRM(global)->minors[0]; - if (!DRM(stub_list)) { - DRM(stub_list) = DRM(alloc)(sizeof(*DRM(stub_list)) - * DRM_STUB_MAXCARDS, DRM_MEM_STUB); - if(!DRM(stub_list)) return -1; - for (i = 0; i < DRM_STUB_MAXCARDS; i++) { - DRM(stub_list)[i].name = NULL; - DRM(stub_list)[i].fops = NULL; + DRM_DEBUG("\n"); + + for (minor = 0; minor < DRM(global)->cards_limit; minor++, minors++) { + if (minors->class == DRM_MINOR_FREE) { + + DRM_DEBUG("assigning minor %d\n", minor); + dev = DRM(calloc)(1, sizeof(*dev), DRM_MEM_STUB); + if(!dev) + return -ENOMEM; + + *minors = (drm_minor_t){.dev = NULL, .class = DRM_MINOR_PRIMARY}; + dev->minor = minor; + if ((ret = DRM(fill_in_dev)(dev, pdev, ent))) { + printk (KERN_ERR "DRM: Fill_in_dev failed.\n"); + goto err_g1; + } + if ((ret = DRM(proc_init)(dev, minor, DRM(global)->proc_root, &minors->dev_root))) { + printk (KERN_ERR "DRM: Failed to initialize /proc/dri.\n"); + goto err_g1; + } + if (!DRM(fb_loaded)) { /* set this before device_add hotplug uses it */ + pci_set_drvdata(pdev, dev); + pci_request_regions(pdev, DRIVER_NAME); + pci_enable_device(pdev); + } + dev_class = class_simple_device_add(DRM(global)->drm_class, + MKDEV(DRM_MAJOR, minor), &pdev->dev, "card%d", minor); + if (IS_ERR(dev_class)) { + printk (KERN_ERR "DRM: Error class_simple_device_add.\n"); + ret = PTR_ERR(dev_class); + goto err_g2; + } + + DRM_DEBUG("new primary minor assigned %d\n", minor); + return 0; } } - for (i = 0; i < DRM_STUB_MAXCARDS; i++) { - if (!DRM(stub_list)[i].fops) { - DRM(stub_list)[i].name = name; - DRM(stub_list)[i].fops = fops; - DRM(proc_init)(dev, i, DRM(stub_info).proc_root, - &DRM(stub_list)[i].dev_root); - (*DRM(stub_info).info_count)++; - DRM_DEBUG("info count increased %d\n", *DRM(stub_info).info_count); - - return i; + DRM_ERROR("out of minors\n"); + return -ENOMEM; +err_g2: + if (!DRM(fb_loaded)) { + pci_set_drvdata(pdev, NULL); + pci_release_regions(pdev); + pci_disable_device(pdev); + } + DRM(proc_cleanup)(minor, DRM(global)->proc_root, minors->dev_root); +err_g1: + *minors = (drm_minor_t){.dev = NULL, .class = DRM_MINOR_FREE}; + DRM(free)(dev, sizeof(*dev), DRM_MEM_STUB); + return ret; +} + +int DRM(get_secondary_minor)(drm_device_t *dev, drm_minor_t **sec_minor) +{ + drm_minor_t *minors = &DRM(global)->minors[0]; + struct class_device *dev_class; + int ret; + int minor; + + DRM_DEBUG("\n"); + + for (minor = 0; minor < DRM(global)->cards_limit; minor++, minors++) { + if (minors->class == DRM_MINOR_FREE) { + + *minors = (drm_minor_t){.dev = dev, .class = DRM_MINOR_SECONDARY}; + if ((ret = DRM(proc_init)(dev, minor, DRM(global)->proc_root, &minors->dev_root))) { + printk (KERN_ERR "DRM: Failed to initialize /proc/dri.\n"); + goto err_g1; + } + + dev_class = class_simple_device_add(DRM(global)->drm_class, + MKDEV(DRM_MAJOR, minor), &dev->pdev->dev, "card%d", minor); + if (IS_ERR(dev_class)) { + printk (KERN_ERR "DRM: Error class_simple_device_add.\n"); + ret = PTR_ERR(dev_class); + goto err_g2; + } + *sec_minor = minors; + + DRM_DEBUG("new secondary minor assigned %d\n", minor); + return 0; } } - return -1; + DRM_ERROR("out of minors\n"); + return -ENOMEM; +err_g2: + DRM(proc_cleanup)(minor, DRM(global)->proc_root, minors->dev_root); +err_g1: + *minors = (drm_minor_t){.dev = NULL, .class = DRM_MINOR_FREE}; + DRM(free)(dev, sizeof(*dev), DRM_MEM_STUB); + return ret; } /** @@ -195,31 +279,53 @@ static int DRM(stub_getminor)(const char *name, struct file_operations *fops, * "drm" data, otherwise unregisters the "drm" data, frees the stub list and * unregisters the character device. */ -static int DRM(stub_putminor)(int minor) +int DRM(put_minor)(drm_device_t *dev) { - if (minor < 0 || minor >= DRM_STUB_MAXCARDS) return -1; - DRM(stub_list)[minor].name = NULL; - DRM(stub_list)[minor].fops = NULL; - DRM(proc_cleanup)(minor, DRM(stub_info).proc_root, - DRM(stub_list)[minor].dev_root); + drm_minor_t *minors = &DRM(global)->minors[dev->minor]; + int i; + + DRM_DEBUG("release primary minor %d\n", dev->minor); - (*DRM(stub_info).info_count)--; + DRM(proc_cleanup)(dev->minor, DRM(global)->proc_root, minors->dev_root); + class_simple_device_remove(MKDEV(DRM_MAJOR, dev->minor)); - if ((*DRM(stub_info).info_count) != 0) { - if (DRM(numdevs) == 0) { - DRM_DEBUG("inter_module_put called %d\n", *DRM(stub_info).info_count); + *minors = (drm_minor_t){.dev = NULL, .class = DRM_MINOR_FREE}; + DRM(free)(dev, sizeof(*dev), DRM_MEM_STUB); + + /* if any device pointers are non-NULL we are not the last module */ + for (i = 0; i < DRM(global)->cards_limit; i++) { + if (DRM(global)->minors[i].class != DRM_MINOR_FREE) { + DRM_DEBUG("inter_module_put called\n"); inter_module_put("drm"); + return 0; } - } else { - DRM_DEBUG("unregistering inter_module \n"); - inter_module_unregister("drm"); - DRM(free)(DRM(stub_list), - sizeof(*DRM(stub_list)) * DRM_STUB_MAXCARDS, - DRM_MEM_STUB); - remove_proc_entry("dri", NULL); - class_simple_destroy(DRM(stub_info).drm_class); - unregister_chrdev(DRM_MAJOR, "drm"); } + DRM_DEBUG("unregistering inter_module \n"); + inter_module_unregister("drm"); + remove_proc_entry("dri", NULL); + class_simple_destroy(DRM(global)->drm_class); + cdev_del(&DRM(global)->drm_cdev); + unregister_chrdev_region(MKDEV(DRM_MAJOR, 0), DRM_MAX_MINOR); + + DRM(free)(DRM(global)->minors, sizeof(*DRM(global)->minors) * + DRM(global)->cards_limit, DRM_MEM_STUB); + DRM(free)(DRM(global), sizeof(*DRM(global)), DRM_MEM_STUB); + DRM(global) = NULL; + + return 0; +} + +int DRM(put_secondary_minor)(drm_minor_t *sec_minor) +{ + int minor = sec_minor - &DRM(global)->minors[0]; + + DRM_DEBUG("release secondary minor %d\n", minor); + + DRM(proc_cleanup)(minor, DRM(global)->proc_root, sec_minor->dev_root); + class_simple_device_remove(MKDEV(DRM_MAJOR, minor)); + + *sec_minor = (drm_minor_t){.dev = NULL, .class = DRM_MINOR_FREE}; + return 0; } @@ -237,104 +343,79 @@ static int DRM(stub_putminor)(int minor) * * Finally calls stub_info::info_register. */ -int DRM(stub_register)(const char *name, struct file_operations *fops, - drm_device_t *dev) +int DRM(probe)(struct pci_dev *pdev, const struct pci_device_id *ent) { - struct drm_stub_info *i = NULL; - int ret1; - int ret2; + dev_t dev = MKDEV(DRM_MAJOR, 0); + drm_global_t *global; + int ret = -ENOMEM; DRM_DEBUG("\n"); - /* if we are registering a second device we don't need to worry - about inter module get/put and other things as they've been - done already */ - if (DRM(numdevs) == 0) { - /* use the inter_module_get to check - as if the same module - registers chrdev twice it succeeds */ - i = (struct drm_stub_info *)inter_module_get("drm"); - if (i) { - /* Already registered */ - DRM(stub_info).info_register = i->info_register; - DRM(stub_info).info_unregister = i->info_unregister; - DRM(stub_info).drm_class = i->drm_class; - DRM(stub_info).info_count = i->info_count; - DRM_DEBUG("already registered %d\n", *i->info_count); - } else if (*DRM(stub_info).info_count == 0) { - - ret1 = register_chrdev(DRM_MAJOR, "drm", &DRM(stub_fops)); - if (ret1 < 0) { - printk (KERN_ERR "Error registering drm major number.\n"); - return ret1; - } + /* use the inter_module_get to check - as if the same module + registers chrdev twice it succeeds */ + global = (drm_global_t *)inter_module_get("drm"); + if (global) { + DRM(global) = global; + global = NULL; + } else { + DRM_DEBUG("first probe\n"); - DRM(stub_info).drm_class = class_simple_create(THIS_MODULE, "drm"); - if (IS_ERR(DRM(stub_info).drm_class)) { - printk (KERN_ERR "Error creating drm class.\n"); - unregister_chrdev(DRM_MAJOR, "drm"); - return PTR_ERR(DRM(stub_info).drm_class); - } - class_simple_set_hotplug(DRM(stub_info).drm_class, drm_hotplug); + global = DRM(calloc)(1, sizeof(*global), DRM_MEM_STUB); + if(!global) + return -ENOMEM; - DRM_DEBUG("calling inter_module_register\n"); - inter_module_register("drm", THIS_MODULE, &DRM(stub_info)); + global->cards_limit = (cards_limit < DRM_MAX_MINOR + 1 ? cards_limit : DRM_MAX_MINOR + 1); + global->minors = DRM(calloc)(global->cards_limit, + sizeof(*global->minors), DRM_MEM_STUB); + if(!global->minors) + goto err_p1; + + if (register_chrdev_region(dev, DRM_MAX_MINOR, "drm")) + goto err_p1; + + strncpy(global->drm_cdev.kobj.name, "drm", KOBJ_NAME_LEN); + global->drm_cdev.owner = THIS_MODULE; + cdev_init(&global->drm_cdev, &DRM(stub_fops)); + if (cdev_add(&global->drm_cdev, dev, DRM_MAX_MINOR)) { + kobject_put(&global->drm_cdev.kobj); + printk (KERN_ERR "DRM: Error registering drm major number.\n"); + goto err_p2; + } - DRM(stub_info).proc_root = create_proc_entry("dri", S_IFDIR, NULL); - if (!DRM(stub_info).proc_root) { - DRM_ERROR("Cannot create /proc/dri\n"); - inter_module_unregister("drm"); - unregister_chrdev(DRM_MAJOR, "drm"); - class_simple_destroy(DRM(stub_info).drm_class); - return -1; - } - + global->drm_class = class_simple_create(THIS_MODULE, "drm"); + if (IS_ERR(global->drm_class)) { + printk (KERN_ERR "DRM: Error creating drm class.\n"); + ret = PTR_ERR(global->drm_class); + goto err_p3; } - } else - DRM_DEBUG("already retrieved inter_module information\n"); - - if (DRM(stub_info).info_register) { - ret2 = DRM(stub_info).info_register(name, fops, dev); - if (ret2 < 0) { - if (DRM(numdevs) == 0 && !i) { - inter_module_unregister("drm"); - unregister_chrdev(DRM_MAJOR, "drm"); - class_simple_destroy(DRM(stub_info).drm_class); - remove_proc_entry("dri", NULL); - DRM_DEBUG("info_register failed, deregistered everything\n"); - } - DRM_DEBUG("info_register failed\n"); - return ret2; + class_simple_set_hotplug(global->drm_class, drm_hotplug); + + global->proc_root = create_proc_entry("dri", S_IFDIR, NULL); + if (!global->proc_root) { + DRM_ERROR("Cannot create /proc/dri\n"); + ret = -1; + goto err_p4; } - class_simple_device_add(DRM(stub_info).drm_class, - MKDEV(DRM_MAJOR, ret2), &dev->pdev->dev, "card%d", ret2); - return ret2; + DRM_DEBUG("calling inter_module_register\n"); + inter_module_register("drm", THIS_MODULE, global); + + DRM(global) = global; } - return -1; -} - -/** - * Unregister. - * - * \param minor - * - * Calls drm_stub_info::unregister. - */ -int DRM(stub_unregister)(int minor) -{ - DRM_DEBUG("%d\n", minor); - class_simple_device_remove(MKDEV(DRM_MAJOR, minor)); - if (DRM(stub_info).info_unregister) - return DRM(stub_info).info_unregister(minor); - return -1; + if ((ret = get_minor(pdev, ent))) { + if (global) + goto err_p4; + return ret; + } + return 0; +err_p4: + class_simple_destroy(global->drm_class); +err_p3: + cdev_del(&global->drm_cdev); + unregister_chrdev_region(dev, DRM_MAX_MINOR); +err_p2: + DRM(free)(global->minors, sizeof(*global->minors) * global->cards_limit, DRM_MEM_STUB); +err_p1: + DRM(free)(global, sizeof(*global), DRM_MEM_STUB); + DRM(global) = NULL; + return ret; } - -int DRM(stub_count); - -/** Stub information */ -struct drm_stub_info DRM(stub_info) = { - .info_register = DRM(stub_getminor), - .info_unregister = DRM(stub_putminor), - .drm_class = NULL, - .info_count = &DRM(stub_count), - .proc_root = NULL, -}; |