/home/lenb/src/to-linus branch 'acpi-2.6.12'
[linux-2.6] / drivers / char / drm / drm_fops.c
1 /**
2  * \file drm_fops.h 
3  * File operations for DRM
4  * 
5  * \author Rickard E. (Rik) Faith <faith@valinux.com>
6  * \author Daryll Strauss <daryll@valinux.com>
7  * \author Gareth Hughes <gareth@valinux.com>
8  */
9
10 /*
11  * Created: Mon Jan  4 08:58:31 1999 by faith@valinux.com
12  *
13  * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
14  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
15  * All Rights Reserved.
16  *
17  * Permission is hereby granted, free of charge, to any person obtaining a
18  * copy of this software and associated documentation files (the "Software"),
19  * to deal in the Software without restriction, including without limitation
20  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
21  * and/or sell copies of the Software, and to permit persons to whom the
22  * Software is furnished to do so, subject to the following conditions:
23  *
24  * The above copyright notice and this permission notice (including the next
25  * paragraph) shall be included in all copies or substantial portions of the
26  * Software.
27  *
28  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
29  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
30  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
31  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
32  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
33  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
34  * OTHER DEALINGS IN THE SOFTWARE.
35  */
36
37 #include "drmP.h"
38 #include <linux/poll.h>
39
40 static int drm_open_helper(struct inode *inode, struct file *filp, drm_device_t *dev);
41
42 static int drm_setup( drm_device_t *dev )
43 {
44         int i;
45         int ret;
46
47         if (dev->driver->presetup)
48         {
49                 ret=dev->driver->presetup(dev);
50                 if (ret!=0) 
51                         return ret;
52         }
53
54         atomic_set( &dev->ioctl_count, 0 );
55         atomic_set( &dev->vma_count, 0 );
56         dev->buf_use = 0;
57         atomic_set( &dev->buf_alloc, 0 );
58
59         if (drm_core_check_feature(dev, DRIVER_HAVE_DMA))
60         {
61                 i = drm_dma_setup( dev );
62                 if ( i < 0 )
63                         return i;
64         }
65
66         for ( i = 0 ; i < DRM_ARRAY_SIZE(dev->counts) ; i++ )
67                 atomic_set( &dev->counts[i], 0 );
68
69         for ( i = 0 ; i < DRM_HASH_SIZE ; i++ ) {
70                 dev->magiclist[i].head = NULL;
71                 dev->magiclist[i].tail = NULL;
72         }
73
74         dev->maplist = drm_alloc(sizeof(*dev->maplist),
75                                   DRM_MEM_MAPS);
76         if(dev->maplist == NULL) return -ENOMEM;
77         memset(dev->maplist, 0, sizeof(*dev->maplist));
78         INIT_LIST_HEAD(&dev->maplist->head);
79
80         dev->ctxlist = drm_alloc(sizeof(*dev->ctxlist),
81                                   DRM_MEM_CTXLIST);
82         if(dev->ctxlist == NULL) return -ENOMEM;
83         memset(dev->ctxlist, 0, sizeof(*dev->ctxlist));
84         INIT_LIST_HEAD(&dev->ctxlist->head);
85
86         dev->vmalist = NULL;
87         dev->sigdata.lock = dev->lock.hw_lock = NULL;
88         init_waitqueue_head( &dev->lock.lock_queue );
89         dev->queue_count = 0;
90         dev->queue_reserved = 0;
91         dev->queue_slots = 0;
92         dev->queuelist = NULL;
93         dev->irq_enabled = 0;
94         dev->context_flag = 0;
95         dev->interrupt_flag = 0;
96         dev->dma_flag = 0;
97         dev->last_context = 0;
98         dev->last_switch = 0;
99         dev->last_checked = 0;
100         init_waitqueue_head( &dev->context_wait );
101         dev->if_version = 0;
102
103         dev->ctx_start = 0;
104         dev->lck_start = 0;
105
106         dev->buf_rp = dev->buf;
107         dev->buf_wp = dev->buf;
108         dev->buf_end = dev->buf + DRM_BSZ;
109         dev->buf_async = NULL;
110         init_waitqueue_head( &dev->buf_readers );
111         init_waitqueue_head( &dev->buf_writers );
112
113         DRM_DEBUG( "\n" );
114
115         /*
116          * The kernel's context could be created here, but is now created
117          * in drm_dma_enqueue.  This is more resource-efficient for
118          * hardware that does not do DMA, but may mean that
119          * drm_select_queue fails between the time the interrupt is
120          * initialized and the time the queues are initialized.
121          */
122         if (dev->driver->postsetup)
123                 dev->driver->postsetup(dev);
124
125         return 0;
126 }
127
128 /**
129  * Open file.
130  * 
131  * \param inode device inode
132  * \param filp file pointer.
133  * \return zero on success or a negative number on failure.
134  *
135  * Searches the DRM device with the same minor number, calls open_helper(), and
136  * increments the device open count. If the open count was previous at zero,
137  * i.e., it's the first that the device is open, then calls setup().
138  */
139 int drm_open( struct inode *inode, struct file *filp )
140 {
141         drm_device_t *dev = NULL;
142         int minor = iminor(inode);
143         int retcode = 0;
144
145         if (!((minor >= 0) && (minor < drm_cards_limit)))
146                 return -ENODEV;
147                 
148         if (!drm_heads[minor])
149                 return -ENODEV;
150
151         if (!(dev = drm_heads[minor]->dev))
152                 return -ENODEV;
153         
154         retcode = drm_open_helper( inode, filp, dev );
155         if ( !retcode ) {
156                 atomic_inc( &dev->counts[_DRM_STAT_OPENS] );
157                 spin_lock( &dev->count_lock );
158                 if ( !dev->open_count++ ) {
159                         spin_unlock( &dev->count_lock );
160                         return drm_setup( dev );
161                 }
162                 spin_unlock( &dev->count_lock );
163         }
164
165         return retcode;
166 }
167 EXPORT_SYMBOL(drm_open);
168
169 /**
170  * Release file.
171  *
172  * \param inode device inode
173  * \param filp file pointer.
174  * \return zero on success or a negative number on failure.
175  *
176  * If the hardware lock is held then free it, and take it again for the kernel
177  * context since it's necessary to reclaim buffers. Unlink the file private
178  * data from its list and free it. Decreases the open count and if it reaches
179  * zero calls takedown().
180  */
181 int drm_release( struct inode *inode, struct file *filp )
182 {
183         drm_file_t *priv = filp->private_data;
184         drm_device_t *dev;
185         int retcode = 0;
186
187         lock_kernel();
188         dev = priv->head->dev;
189
190         DRM_DEBUG( "open_count = %d\n", dev->open_count );
191
192         if (dev->driver->prerelease)
193                 dev->driver->prerelease(dev, filp);
194
195         /* ========================================================
196          * Begin inline drm_release
197          */
198
199         DRM_DEBUG( "pid = %d, device = 0x%lx, open_count = %d\n",
200                    current->pid, (long)old_encode_dev(priv->head->device), dev->open_count );
201
202         if ( priv->lock_count && dev->lock.hw_lock &&
203              _DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock) &&
204              dev->lock.filp == filp ) {
205                 DRM_DEBUG( "File %p released, freeing lock for context %d\n",
206                         filp,
207                         _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock) );
208                 
209                 if (dev->driver->release)
210                         dev->driver->release(dev, filp);
211
212                 drm_lock_free( dev, &dev->lock.hw_lock->lock,
213                                 _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock) );
214
215                                 /* FIXME: may require heavy-handed reset of
216                                    hardware at this point, possibly
217                                    processed via a callback to the X
218                                    server. */
219         }
220         else if ( dev->driver->release && priv->lock_count && dev->lock.hw_lock ) {
221                 /* The lock is required to reclaim buffers */
222                 DECLARE_WAITQUEUE( entry, current );
223
224                 add_wait_queue( &dev->lock.lock_queue, &entry );
225                 for (;;) {
226                         __set_current_state(TASK_INTERRUPTIBLE);
227                         if ( !dev->lock.hw_lock ) {
228                                 /* Device has been unregistered */
229                                 retcode = -EINTR;
230                                 break;
231                         }
232                         if ( drm_lock_take( &dev->lock.hw_lock->lock,
233                                              DRM_KERNEL_CONTEXT ) ) {
234                                 dev->lock.filp      = filp;
235                                 dev->lock.lock_time = jiffies;
236                                 atomic_inc( &dev->counts[_DRM_STAT_LOCKS] );
237                                 break;  /* Got lock */
238                         }
239                                 /* Contention */
240                         schedule();
241                         if ( signal_pending( current ) ) {
242                                 retcode = -ERESTARTSYS;
243                                 break;
244                         }
245                 }
246                 __set_current_state(TASK_RUNNING);
247                 remove_wait_queue( &dev->lock.lock_queue, &entry );
248                 if( !retcode ) {
249                         if (dev->driver->release)
250                                 dev->driver->release(dev, filp);
251                         drm_lock_free( dev, &dev->lock.hw_lock->lock,
252                                         DRM_KERNEL_CONTEXT );
253                 }
254         }
255         
256         if (drm_core_check_feature(dev, DRIVER_HAVE_DMA) && !dev->driver->release)
257         {
258                 dev->driver->reclaim_buffers(dev, filp);
259         }
260
261         drm_fasync( -1, filp, 0 );
262
263         down( &dev->ctxlist_sem );
264         if ( dev->ctxlist && (!list_empty(&dev->ctxlist->head))) {
265                 drm_ctx_list_t *pos, *n;
266
267                 list_for_each_entry_safe( pos, n, &dev->ctxlist->head, head ) {
268                         if ( pos->tag == priv &&
269                              pos->handle != DRM_KERNEL_CONTEXT ) {
270                                 if (dev->driver->context_dtor)
271                                         dev->driver->context_dtor(dev, pos->handle);
272
273                                 drm_ctxbitmap_free( dev, pos->handle );
274
275                                 list_del( &pos->head );
276                                 drm_free( pos, sizeof(*pos), DRM_MEM_CTXLIST );
277                                 --dev->ctx_count;
278                         }
279                 }
280         }
281         up( &dev->ctxlist_sem );
282
283         down( &dev->struct_sem );
284         if ( priv->remove_auth_on_close == 1 ) {
285                 drm_file_t *temp = dev->file_first;
286                 while ( temp ) {
287                         temp->authenticated = 0;
288                         temp = temp->next;
289                 }
290         }
291         if ( priv->prev ) {
292                 priv->prev->next = priv->next;
293         } else {
294                 dev->file_first  = priv->next;
295         }
296         if ( priv->next ) {
297                 priv->next->prev = priv->prev;
298         } else {
299                 dev->file_last   = priv->prev;
300         }
301         up( &dev->struct_sem );
302         
303         if (dev->driver->free_filp_priv)
304                 dev->driver->free_filp_priv(dev, priv);
305
306         drm_free( priv, sizeof(*priv), DRM_MEM_FILES );
307
308         /* ========================================================
309          * End inline drm_release
310          */
311
312         atomic_inc( &dev->counts[_DRM_STAT_CLOSES] );
313         spin_lock( &dev->count_lock );
314         if ( !--dev->open_count ) {
315                 if ( atomic_read( &dev->ioctl_count ) || dev->blocked ) {
316                         DRM_ERROR( "Device busy: %d %d\n",
317                                    atomic_read( &dev->ioctl_count ),
318                                    dev->blocked );
319                         spin_unlock( &dev->count_lock );
320                         unlock_kernel();
321                         return -EBUSY;
322                 }
323                 spin_unlock( &dev->count_lock );
324                 unlock_kernel();
325                 return drm_takedown( dev );
326         }
327         spin_unlock( &dev->count_lock );
328
329         unlock_kernel();
330
331         return retcode;
332 }
333 EXPORT_SYMBOL(drm_release);
334
335 /**
336  * Called whenever a process opens /dev/drm. 
337  *
338  * \param inode device inode.
339  * \param filp file pointer.
340  * \param dev device.
341  * \return zero on success or a negative number on failure.
342  * 
343  * Creates and initializes a drm_file structure for the file private data in \p
344  * filp and add it into the double linked list in \p dev.
345  */
346 static int drm_open_helper(struct inode *inode, struct file *filp, drm_device_t *dev)
347 {
348         int          minor = iminor(inode);
349         drm_file_t   *priv;
350         int ret;
351
352         if (filp->f_flags & O_EXCL)   return -EBUSY; /* No exclusive opens */
353         if (!drm_cpu_valid())        return -EINVAL;
354
355         DRM_DEBUG("pid = %d, minor = %d\n", current->pid, minor);
356
357         priv                = drm_alloc(sizeof(*priv), DRM_MEM_FILES);
358         if(!priv) return -ENOMEM;
359
360         memset(priv, 0, sizeof(*priv));
361         filp->private_data  = priv;
362         priv->uid           = current->euid;
363         priv->pid           = current->pid;
364         priv->minor         = minor;
365         priv->head          = drm_heads[minor];
366         priv->ioctl_count   = 0;
367         priv->authenticated = capable(CAP_SYS_ADMIN);
368         priv->lock_count    = 0;
369
370         if (dev->driver->open_helper) {
371                 ret=dev->driver->open_helper(dev, priv);
372                 if (ret < 0)
373                         goto out_free;
374         }
375
376         down(&dev->struct_sem);
377         if (!dev->file_last) {
378                 priv->next      = NULL;
379                 priv->prev      = NULL;
380                 dev->file_first = priv;
381                 dev->file_last  = priv;
382         } else {
383                 priv->next           = NULL;
384                 priv->prev           = dev->file_last;
385                 dev->file_last->next = priv;
386                 dev->file_last       = priv;
387         }
388         up(&dev->struct_sem);
389
390 #ifdef __alpha__
391         /*
392          * Default the hose
393          */
394         if (!dev->hose) {
395                 struct pci_dev *pci_dev;
396                 pci_dev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL);
397                 if (pci_dev) {
398                         dev->hose = pci_dev->sysdata;
399                         pci_dev_put(pci_dev);
400                 }
401                 if (!dev->hose) {
402                         struct pci_bus *b = pci_bus_b(pci_root_buses.next);
403                         if (b) dev->hose = b->sysdata;
404                 }
405         }
406 #endif
407
408         return 0;
409 out_free:
410         drm_free(priv, sizeof(*priv), DRM_MEM_FILES);
411         filp->private_data=NULL;
412         return ret;
413 }
414
415 /** No-op. */
416 int drm_flush(struct file *filp)
417 {
418         drm_file_t    *priv   = filp->private_data;
419         drm_device_t  *dev    = priv->head->dev;
420
421         DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
422                   current->pid, (long)old_encode_dev(priv->head->device), dev->open_count);
423         return 0;
424 }
425 EXPORT_SYMBOL(drm_flush);
426
427 /** No-op. */
428 int drm_fasync(int fd, struct file *filp, int on)
429 {
430         drm_file_t    *priv   = filp->private_data;
431         drm_device_t  *dev    = priv->head->dev;
432         int           retcode;
433
434         DRM_DEBUG("fd = %d, device = 0x%lx\n", fd, (long)old_encode_dev(priv->head->device));
435         retcode = fasync_helper(fd, filp, on, &dev->buf_async);
436         if (retcode < 0) return retcode;
437         return 0;
438 }
439 EXPORT_SYMBOL(drm_fasync);
440
441 /** No-op. */
442 unsigned int drm_poll(struct file *filp, struct poll_table_struct *wait)
443 {
444         return 0;
445 }
446 EXPORT_SYMBOL(drm_poll);
447