Import 2.4.0-test2pre7
[davej-history.git] / drivers / char / drm / fops.c
blob76e0dc2c1950c4165624563b00075a5b0cb942ce
1 /* fops.c -- File operations for DRM -*- linux-c -*-
2 * Created: Mon Jan 4 08:58:31 1999 by faith@precisioninsight.com
4 * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
5 * All Rights Reserved.
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:
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.
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.
26 * Authors:
27 * Rickard E. (Rik) Faith <faith@precisioninsight.com>
28 * Daryll Strauss <daryll@precisioninsight.com>
32 #define __NO_VERSION__
33 #include "drmP.h"
35 /* drm_open is called whenever a process opens /dev/drm. */
37 int drm_open_helper(struct inode *inode, struct file *filp, drm_device_t *dev)
39 kdev_t minor = MINOR(inode->i_rdev);
40 drm_file_t *priv;
42 if (filp->f_flags & O_EXCL) return -EBUSY; /* No exclusive opens */
43 if (!drm_cpu_valid()) return -EINVAL;
45 DRM_DEBUG("pid = %d, minor = %d\n", current->pid, minor);
47 priv = drm_alloc(sizeof(*priv), DRM_MEM_FILES);
48 memset(priv, 0, sizeof(*priv));
49 filp->private_data = priv;
50 priv->uid = current->euid;
51 priv->pid = current->pid;
52 priv->minor = minor;
53 priv->dev = dev;
54 priv->ioctl_count = 0;
55 priv->authenticated = capable(CAP_SYS_ADMIN);
57 down(&dev->struct_sem);
58 if (!dev->file_last) {
59 priv->next = NULL;
60 priv->prev = NULL;
61 dev->file_first = priv;
62 dev->file_last = priv;
63 } else {
64 priv->next = NULL;
65 priv->prev = dev->file_last;
66 dev->file_last->next = priv;
67 dev->file_last = priv;
69 up(&dev->struct_sem);
71 return 0;
74 int drm_flush(struct file *filp)
76 drm_file_t *priv = filp->private_data;
77 drm_device_t *dev = priv->dev;
79 DRM_DEBUG("pid = %d, device = 0x%x, open_count = %d\n",
80 current->pid, dev->device, dev->open_count);
81 return 0;
84 /* drm_release is called whenever a process closes /dev/drm*. Linux calls
85 this only if any mappings have been closed. */
87 int drm_release(struct inode *inode, struct file *filp)
89 drm_file_t *priv = filp->private_data;
90 drm_device_t *dev = priv->dev;
92 DRM_DEBUG("pid = %d, device = 0x%x, open_count = %d\n",
93 current->pid, dev->device, dev->open_count);
95 if (dev->lock.hw_lock != NULL
96 && _DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock)
97 && dev->lock.pid == current->pid) {
98 DRM_ERROR("Process %d dead, freeing lock for context %d\n",
99 current->pid,
100 _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock));
101 drm_lock_free(dev,
102 &dev->lock.hw_lock->lock,
103 _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock));
105 /* FIXME: may require heavy-handed reset of
106 hardware at this point, possibly
107 processed via a callback to the X
108 server. */
110 drm_reclaim_buffers(dev, priv->pid);
112 drm_fasync(-1, filp, 0);
114 down(&dev->struct_sem);
115 if (priv->prev) priv->prev->next = priv->next;
116 else dev->file_first = priv->next;
117 if (priv->next) priv->next->prev = priv->prev;
118 else dev->file_last = priv->prev;
119 up(&dev->struct_sem);
121 drm_free(priv, sizeof(*priv), DRM_MEM_FILES);
123 return 0;
126 int drm_fasync(int fd, struct file *filp, int on)
128 drm_file_t *priv = filp->private_data;
129 drm_device_t *dev = priv->dev;
130 int retcode;
132 DRM_DEBUG("fd = %d, device = 0x%x\n", fd, dev->device);
133 retcode = fasync_helper(fd, filp, on, &dev->buf_async);
134 if (retcode < 0) return retcode;
135 return 0;
139 /* The drm_read and drm_write_string code (especially that which manages
140 the circular buffer), is based on Alessandro Rubini's LINUX DEVICE
141 DRIVERS (Cambridge: O'Reilly, 1998), pages 111-113. */
143 ssize_t drm_read(struct file *filp, char *buf, size_t count, loff_t *off)
145 drm_file_t *priv = filp->private_data;
146 drm_device_t *dev = priv->dev;
147 int left;
148 int avail;
149 int send;
150 int cur;
152 DRM_DEBUG("%p, %p\n", dev->buf_rp, dev->buf_wp);
154 while (dev->buf_rp == dev->buf_wp) {
155 DRM_DEBUG(" sleeping\n");
156 if (filp->f_flags & O_NONBLOCK) {
157 return -EAGAIN;
159 interruptible_sleep_on(&dev->buf_readers);
160 if (signal_pending(current)) {
161 DRM_DEBUG(" interrupted\n");
162 return -ERESTARTSYS;
164 DRM_DEBUG(" awake\n");
167 left = (dev->buf_rp + DRM_BSZ - dev->buf_wp) % DRM_BSZ;
168 avail = DRM_BSZ - left;
169 send = DRM_MIN(avail, count);
171 while (send) {
172 if (dev->buf_wp > dev->buf_rp) {
173 cur = DRM_MIN(send, dev->buf_wp - dev->buf_rp);
174 } else {
175 cur = DRM_MIN(send, dev->buf_end - dev->buf_rp);
177 copy_to_user_ret(buf, dev->buf_rp, cur, -EINVAL);
178 dev->buf_rp += cur;
179 if (dev->buf_rp == dev->buf_end) dev->buf_rp = dev->buf;
180 send -= cur;
183 wake_up_interruptible(&dev->buf_writers);
184 return DRM_MIN(avail, count);;
187 int drm_write_string(drm_device_t *dev, const char *s)
189 int left = (dev->buf_rp + DRM_BSZ - dev->buf_wp) % DRM_BSZ;
190 int send = strlen(s);
191 int count;
193 DRM_DEBUG("%d left, %d to send (%p, %p)\n",
194 left, send, dev->buf_rp, dev->buf_wp);
196 if (left == 1 || dev->buf_wp != dev->buf_rp) {
197 DRM_ERROR("Buffer not empty (%d left, wp = %p, rp = %p)\n",
198 left,
199 dev->buf_wp,
200 dev->buf_rp);
203 while (send) {
204 if (dev->buf_wp >= dev->buf_rp) {
205 count = DRM_MIN(send, dev->buf_end - dev->buf_wp);
206 if (count == left) --count; /* Leave a hole */
207 } else {
208 count = DRM_MIN(send, dev->buf_rp - dev->buf_wp - 1);
210 strncpy(dev->buf_wp, s, count);
211 dev->buf_wp += count;
212 if (dev->buf_wp == dev->buf_end) dev->buf_wp = dev->buf;
213 send -= count;
216 #if LINUX_VERSION_CODE < 0x020315
217 if (dev->buf_async) kill_fasync(dev->buf_async, SIGIO);
218 #else
219 /* Parameter added in 2.3.21 */
220 kill_fasync(&dev->buf_async, SIGIO, POLL_IN);
221 #endif
222 DRM_DEBUG("waking\n");
223 wake_up_interruptible(&dev->buf_readers);
224 return 0;
227 unsigned int drm_poll(struct file *filp, struct poll_table_struct *wait)
229 drm_file_t *priv = filp->private_data;
230 drm_device_t *dev = priv->dev;
232 poll_wait(filp, &dev->buf_readers, wait);
233 if (dev->buf_wp != dev->buf_rp) return POLLIN | POLLRDNORM;
234 return 0;