linux-old/drivers/char/drm/drm_fops.h
<<
>>
Prefs
   1/* drm_fops.h -- File operations for DRM -*- linux-c -*-
   2 * Created: Mon Jan  4 08:58:31 1999 by faith@valinux.com
   3 *
   4 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
   5 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
   6 * All Rights Reserved.
   7 *
   8 * Permission is hereby granted, free of charge, to any person obtaining a
   9 * copy of this software and associated documentation files (the "Software"),
  10 * to deal in the Software without restriction, including without limitation
  11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  12 * and/or sell copies of the Software, and to permit persons to whom the
  13 * Software is furnished to do so, subject to the following conditions:
  14 *
  15 * The above copyright notice and this permission notice (including the next
  16 * paragraph) shall be included in all copies or substantial portions of the
  17 * Software.
  18 *
  19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  22 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  23 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  24 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  25 * OTHER DEALINGS IN THE SOFTWARE.
  26 *
  27 * Authors:
  28 *    Rickard E. (Rik) Faith <faith@valinux.com>
  29 *    Daryll Strauss <daryll@valinux.com>
  30 *    Gareth Hughes <gareth@valinux.com>
  31 */
  32
  33#include "drmP.h"
  34#include <linux/poll.h>
  35
  36/* drm_open is called whenever a process opens /dev/drm. */
  37
  38int DRM(open_helper)(struct inode *inode, struct file *filp, drm_device_t *dev)
  39{
  40        int          minor = minor(inode->i_rdev);
  41        drm_file_t   *priv;
  42
  43        if (filp->f_flags & O_EXCL)   return -EBUSY; /* No exclusive opens */
  44        if (!DRM(cpu_valid)())        return -EINVAL;
  45
  46        DRM_DEBUG("pid = %d, minor = %d\n", current->pid, minor);
  47
  48        priv                = DRM(alloc)(sizeof(*priv), DRM_MEM_FILES);
  49        if(!priv) return -ENOMEM;
  50
  51        memset(priv, 0, sizeof(*priv));
  52        filp->private_data  = priv;
  53        priv->uid           = current->euid;
  54        priv->pid           = current->pid;
  55        priv->minor         = minor;
  56        priv->dev           = dev;
  57        priv->ioctl_count   = 0;
  58        priv->authenticated = capable(CAP_SYS_ADMIN);
  59
  60        down(&dev->struct_sem);
  61        if (!dev->file_last) {
  62                priv->next      = NULL;
  63                priv->prev      = NULL;
  64                dev->file_first = priv;
  65                dev->file_last  = priv;
  66        } else {
  67                priv->next           = NULL;
  68                priv->prev           = dev->file_last;
  69                dev->file_last->next = priv;
  70                dev->file_last       = priv;
  71        }
  72        up(&dev->struct_sem);
  73
  74#ifdef __alpha__
  75        /*
  76         * Default the hose
  77         */
  78        if (!dev->hose) {
  79                struct pci_dev *pci_dev;
  80                pci_dev = pci_find_class(PCI_CLASS_DISPLAY_VGA << 8, NULL);
  81                if (pci_dev) dev->hose = pci_dev->sysdata;
  82                if (!dev->hose) {
  83                        struct pci_bus *b = pci_bus_b(pci_root_buses.next);
  84                        if (b) dev->hose = b->sysdata;
  85                }
  86        }
  87#endif
  88
  89        return 0;
  90}
  91
  92int DRM(flush)(struct file *filp)
  93{
  94        drm_file_t    *priv   = filp->private_data;
  95        drm_device_t  *dev    = priv->dev;
  96
  97        DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
  98                  current->pid, (long)dev->device, dev->open_count);
  99        return 0;
 100}
 101
 102int DRM(fasync)(int fd, struct file *filp, int on)
 103{
 104        drm_file_t    *priv   = filp->private_data;
 105        drm_device_t  *dev    = priv->dev;
 106        int           retcode;
 107
 108        DRM_DEBUG("fd = %d, device = 0x%lx\n", fd, (long)dev->device);
 109        retcode = fasync_helper(fd, filp, on, &dev->buf_async);
 110        if (retcode < 0) return retcode;
 111        return 0;
 112}
 113
 114
 115/* The drm_read and drm_write_string code (especially that which manages
 116   the circular buffer), is based on Alessandro Rubini's LINUX DEVICE
 117   DRIVERS (Cambridge: O'Reilly, 1998), pages 111-113. */
 118
 119ssize_t DRM(read)(struct file *filp, char *buf, size_t count, loff_t *off)
 120{
 121        drm_file_t    *priv   = filp->private_data;
 122        drm_device_t  *dev    = priv->dev;
 123        int           left;
 124        int           avail;
 125        int           send;
 126        int           cur;
 127        DECLARE_WAITQUEUE(wait, current);
 128
 129        DRM_DEBUG("%p, %p\n", dev->buf_rp, dev->buf_wp);
 130
 131        add_wait_queue(&dev->buf_readers, &wait);
 132        set_current_state(TASK_INTERRUPTIBLE);
 133        while (dev->buf_rp == dev->buf_wp) {
 134                DRM_DEBUG("  sleeping\n");
 135                if (filp->f_flags & O_NONBLOCK) {
 136                        remove_wait_queue(&dev->buf_readers, &wait);
 137                        set_current_state(TASK_RUNNING);
 138                        return -EAGAIN;
 139                }
 140                schedule(); /* wait for dev->buf_readers */
 141                if (signal_pending(current)) {
 142                        DRM_DEBUG("  interrupted\n");
 143                        remove_wait_queue(&dev->buf_readers, &wait);
 144                        set_current_state(TASK_RUNNING);
 145                        return -ERESTARTSYS;
 146                }
 147                DRM_DEBUG("  awake\n");
 148                set_current_state(TASK_INTERRUPTIBLE);
 149        }
 150        remove_wait_queue(&dev->buf_readers, &wait);
 151        set_current_state(TASK_RUNNING);
 152
 153        left  = (dev->buf_rp + DRM_BSZ - dev->buf_wp) % DRM_BSZ;
 154        avail = DRM_BSZ - left;
 155        send  = DRM_MIN(avail, count);
 156
 157        while (send) {
 158                if (dev->buf_wp > dev->buf_rp) {
 159                        cur = DRM_MIN(send, dev->buf_wp - dev->buf_rp);
 160                } else {
 161                        cur = DRM_MIN(send, dev->buf_end - dev->buf_rp);
 162                }
 163                if (copy_to_user(buf, dev->buf_rp, cur))
 164                        return -EFAULT;
 165                dev->buf_rp += cur;
 166                if (dev->buf_rp == dev->buf_end) dev->buf_rp = dev->buf;
 167                send -= cur;
 168        }
 169
 170        wake_up_interruptible(&dev->buf_writers);
 171        return DRM_MIN(avail, count);;
 172}
 173
 174int DRM(write_string)(drm_device_t *dev, const char *s)
 175{
 176        int left   = (dev->buf_rp + DRM_BSZ - dev->buf_wp) % DRM_BSZ;
 177        int send   = strlen(s);
 178        int count;
 179
 180        DRM_DEBUG("%d left, %d to send (%p, %p)\n",
 181                  left, send, dev->buf_rp, dev->buf_wp);
 182
 183        if (left == 1 || dev->buf_wp != dev->buf_rp) {
 184                DRM_ERROR("Buffer not empty (%d left, wp = %p, rp = %p)\n",
 185                          left,
 186                          dev->buf_wp,
 187                          dev->buf_rp);
 188        }
 189
 190        while (send) {
 191                if (dev->buf_wp >= dev->buf_rp) {
 192                        count = DRM_MIN(send, dev->buf_end - dev->buf_wp);
 193                        if (count == left) --count; /* Leave a hole */
 194                } else {
 195                        count = DRM_MIN(send, dev->buf_rp - dev->buf_wp - 1);
 196                }
 197                strncpy(dev->buf_wp, s, count);
 198                dev->buf_wp += count;
 199                if (dev->buf_wp == dev->buf_end) dev->buf_wp = dev->buf;
 200                send -= count;
 201        }
 202
 203        if (dev->buf_async) kill_fasync(&dev->buf_async, SIGIO, POLL_IN);
 204
 205        DRM_DEBUG("waking\n");
 206        wake_up_interruptible(&dev->buf_readers);
 207        return 0;
 208}
 209
 210unsigned int DRM(poll)(struct file *filp, struct poll_table_struct *wait)
 211{
 212        drm_file_t   *priv = filp->private_data;
 213        drm_device_t *dev  = priv->dev;
 214
 215        poll_wait(filp, &dev->buf_readers, wait);
 216        if (dev->buf_wp != dev->buf_rp) return POLLIN | POLLRDNORM;
 217        return 0;
 218}
 219
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.