drm_stub.h
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:5k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /* drm_stub.h -- -*- linux-c -*-
  2.  * Created: Fri Jan 19 10:48:35 2001 by faith@acm.org
  3.  *
  4.  * Copyright 2001 VA Linux Systems, Inc., Sunnyvale, California.
  5.  * All Rights Reserved.
  6.  *
  7.  * Permission is hereby granted, free of charge, to any person obtaining a
  8.  * copy of this software and associated documentation files (the "Software"),
  9.  * to deal in the Software without restriction, including without limitation
  10.  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  11.  * and/or sell copies of the Software, and to permit persons to whom the
  12.  * Software is furnished to do so, subject to the following conditions:
  13.  *
  14.  * The above copyright notice and this permission notice (including the next
  15.  * paragraph) shall be included in all copies or substantial portions of the
  16.  * Software.
  17.  *
  18.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  21.  * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  22.  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  23.  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  24.  * DEALINGS IN THE SOFTWARE.
  25.  *
  26.  * Authors:
  27.  *    Rickard E. (Rik) Faith <faith@valinux.com>
  28.  *
  29.  */
  30. #include "drmP.h"
  31. #define DRM_STUB_MAXCARDS 16 /* Enough for one machine */
  32. static struct drm_stub_list {
  33. const char             *name;
  34. struct file_operations *fops;
  35. struct proc_dir_entry  *dev_root;
  36. } *DRM(stub_list);
  37. static struct proc_dir_entry *DRM(stub_root);
  38. static struct drm_stub_info {
  39. int (*info_register)(const char *name, struct file_operations *fops,
  40.      drm_device_t *dev);
  41. int (*info_unregister)(int minor);
  42. } DRM(stub_info);
  43. static int DRM(stub_open)(struct inode *inode, struct file *filp)
  44. {
  45. int                    minor = MINOR(inode->i_rdev);
  46. int                    err   = -ENODEV;
  47. struct file_operations *old_fops;
  48. if (!DRM(stub_list) || !DRM(stub_list)[minor].fops) return -ENODEV;
  49. old_fops   = filp->f_op;
  50. filp->f_op = fops_get(DRM(stub_list)[minor].fops);
  51. if (filp->f_op->open && (err = filp->f_op->open(inode, filp))) {
  52. fops_put(filp->f_op);
  53. filp->f_op = fops_get(old_fops);
  54. }
  55. fops_put(old_fops);
  56. return err;
  57. }
  58. static struct file_operations DRM(stub_fops) = {
  59. owner:   THIS_MODULE,
  60. open:  DRM(stub_open)
  61. };
  62. static int DRM(stub_getminor)(const char *name, struct file_operations *fops,
  63.       drm_device_t *dev)
  64. {
  65. int i;
  66. if (!DRM(stub_list)) {
  67. DRM(stub_list) = DRM(alloc)(sizeof(*DRM(stub_list))
  68.     * DRM_STUB_MAXCARDS, DRM_MEM_STUB);
  69. if(!DRM(stub_list)) return -1;
  70. for (i = 0; i < DRM_STUB_MAXCARDS; i++) {
  71. DRM(stub_list)[i].name = NULL;
  72. DRM(stub_list)[i].fops = NULL;
  73. }
  74. }
  75. for (i = 0; i < DRM_STUB_MAXCARDS; i++) {
  76. if (!DRM(stub_list)[i].fops) {
  77. DRM(stub_list)[i].name = name;
  78. DRM(stub_list)[i].fops = fops;
  79. DRM(stub_root) = DRM(proc_init)(dev, i, DRM(stub_root),
  80. &DRM(stub_list)[i]
  81. .dev_root);
  82. return i;
  83. }
  84. }
  85. return -1;
  86. }
  87. static int DRM(stub_putminor)(int minor)
  88. {
  89. if (minor < 0 || minor >= DRM_STUB_MAXCARDS) return -1;
  90. DRM(stub_list)[minor].name = NULL;
  91. DRM(stub_list)[minor].fops = NULL;
  92. DRM(proc_cleanup)(minor, DRM(stub_root),
  93.   DRM(stub_list)[minor].dev_root);
  94. if (minor) {
  95. inter_module_put("drm");
  96. } else {
  97. inter_module_unregister("drm");
  98. DRM(free)(DRM(stub_list),
  99.   sizeof(*DRM(stub_list)) * DRM_STUB_MAXCARDS,
  100.   DRM_MEM_STUB);
  101. unregister_chrdev(DRM_MAJOR, "drm");
  102. }
  103. return 0;
  104. }
  105. int DRM(stub_register)(const char *name, struct file_operations *fops,
  106.        drm_device_t *dev)
  107. {
  108. struct drm_stub_info *i = NULL;
  109. DRM_DEBUG("n");
  110. if (register_chrdev(DRM_MAJOR, "drm", &DRM(stub_fops)))
  111. i = (struct drm_stub_info *)inter_module_get("drm");
  112. if (i) {
  113. /* Already registered */
  114. DRM(stub_info).info_register   = i->info_register;
  115. DRM(stub_info).info_unregister = i->info_unregister;
  116. DRM_DEBUG("already registeredn");
  117. } else if (DRM(stub_info).info_register != DRM(stub_getminor)) {
  118. DRM(stub_info).info_register   = DRM(stub_getminor);
  119. DRM(stub_info).info_unregister = DRM(stub_putminor);
  120. DRM_DEBUG("calling inter_module_registern");
  121. inter_module_register("drm", THIS_MODULE, &DRM(stub_info));
  122. }
  123. if (DRM(stub_info).info_register)
  124. return DRM(stub_info).info_register(name, fops, dev);
  125. return -1;
  126. }
  127. int DRM(stub_unregister)(int minor)
  128. {
  129. DRM_DEBUG("%dn", minor);
  130. if (DRM(stub_info).info_unregister)
  131. return DRM(stub_info).info_unregister(minor);
  132. return -1;
  133. }