treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / gpu / drm / drm_file.c
blob92d16724f9495c2cf70ef6d608c83da52fad1203
1 /*
2 * \author Rickard E. (Rik) Faith <faith@valinux.com>
3 * \author Daryll Strauss <daryll@valinux.com>
4 * \author Gareth Hughes <gareth@valinux.com>
5 */
7 /*
8 * Created: Mon Jan 4 08:58:31 1999 by faith@valinux.com
10 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
11 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
12 * All Rights Reserved.
14 * Permission is hereby granted, free of charge, to any person obtaining a
15 * copy of this software and associated documentation files (the "Software"),
16 * to deal in the Software without restriction, including without limitation
17 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
18 * and/or sell copies of the Software, and to permit persons to whom the
19 * Software is furnished to do so, subject to the following conditions:
21 * The above copyright notice and this permission notice (including the next
22 * paragraph) shall be included in all copies or substantial portions of the
23 * Software.
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
28 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
29 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
30 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
31 * OTHER DEALINGS IN THE SOFTWARE.
34 #include <linux/anon_inodes.h>
35 #include <linux/dma-fence.h>
36 #include <linux/file.h>
37 #include <linux/module.h>
38 #include <linux/pci.h>
39 #include <linux/poll.h>
40 #include <linux/slab.h>
42 #include <drm/drm_client.h>
43 #include <drm/drm_drv.h>
44 #include <drm/drm_file.h>
45 #include <drm/drm_print.h>
47 #include "drm_crtc_internal.h"
48 #include "drm_internal.h"
49 #include "drm_legacy.h"
51 /* from BKL pushdown */
52 DEFINE_MUTEX(drm_global_mutex);
54 /**
55 * DOC: file operations
57 * Drivers must define the file operations structure that forms the DRM
58 * userspace API entry point, even though most of those operations are
59 * implemented in the DRM core. The resulting &struct file_operations must be
60 * stored in the &drm_driver.fops field. The mandatory functions are drm_open(),
61 * drm_read(), drm_ioctl() and drm_compat_ioctl() if CONFIG_COMPAT is enabled
62 * Note that drm_compat_ioctl will be NULL if CONFIG_COMPAT=n, so there's no
63 * need to sprinkle #ifdef into the code. Drivers which implement private ioctls
64 * that require 32/64 bit compatibility support must provide their own
65 * &file_operations.compat_ioctl handler that processes private ioctls and calls
66 * drm_compat_ioctl() for core ioctls.
68 * In addition drm_read() and drm_poll() provide support for DRM events. DRM
69 * events are a generic and extensible means to send asynchronous events to
70 * userspace through the file descriptor. They are used to send vblank event and
71 * page flip completions by the KMS API. But drivers can also use it for their
72 * own needs, e.g. to signal completion of rendering.
74 * For the driver-side event interface see drm_event_reserve_init() and
75 * drm_send_event() as the main starting points.
77 * The memory mapping implementation will vary depending on how the driver
78 * manages memory. Legacy drivers will use the deprecated drm_legacy_mmap()
79 * function, modern drivers should use one of the provided memory-manager
80 * specific implementations. For GEM-based drivers this is drm_gem_mmap(), and
81 * for drivers which use the CMA GEM helpers it's drm_gem_cma_mmap().
83 * No other file operations are supported by the DRM userspace API. Overall the
84 * following is an example &file_operations structure::
86 * static const example_drm_fops = {
87 * .owner = THIS_MODULE,
88 * .open = drm_open,
89 * .release = drm_release,
90 * .unlocked_ioctl = drm_ioctl,
91 * .compat_ioctl = drm_compat_ioctl, // NULL if CONFIG_COMPAT=n
92 * .poll = drm_poll,
93 * .read = drm_read,
94 * .llseek = no_llseek,
95 * .mmap = drm_gem_mmap,
96 * };
98 * For plain GEM based drivers there is the DEFINE_DRM_GEM_FOPS() macro, and for
99 * CMA based drivers there is the DEFINE_DRM_GEM_CMA_FOPS() macro to make this
100 * simpler.
102 * The driver's &file_operations must be stored in &drm_driver.fops.
104 * For driver-private IOCTL handling see the more detailed discussion in
105 * :ref:`IOCTL support in the userland interfaces chapter<drm_driver_ioctl>`.
109 * drm_file_alloc - allocate file context
110 * @minor: minor to allocate on
112 * This allocates a new DRM file context. It is not linked into any context and
113 * can be used by the caller freely. Note that the context keeps a pointer to
114 * @minor, so it must be freed before @minor is.
116 * RETURNS:
117 * Pointer to newly allocated context, ERR_PTR on failure.
119 struct drm_file *drm_file_alloc(struct drm_minor *minor)
121 struct drm_device *dev = minor->dev;
122 struct drm_file *file;
123 int ret;
125 file = kzalloc(sizeof(*file), GFP_KERNEL);
126 if (!file)
127 return ERR_PTR(-ENOMEM);
129 file->pid = get_pid(task_pid(current));
130 file->minor = minor;
132 /* for compatibility root is always authenticated */
133 file->authenticated = capable(CAP_SYS_ADMIN);
135 INIT_LIST_HEAD(&file->lhead);
136 INIT_LIST_HEAD(&file->fbs);
137 mutex_init(&file->fbs_lock);
138 INIT_LIST_HEAD(&file->blobs);
139 INIT_LIST_HEAD(&file->pending_event_list);
140 INIT_LIST_HEAD(&file->event_list);
141 init_waitqueue_head(&file->event_wait);
142 file->event_space = 4096; /* set aside 4k for event buffer */
144 mutex_init(&file->event_read_lock);
146 if (drm_core_check_feature(dev, DRIVER_GEM))
147 drm_gem_open(dev, file);
149 if (drm_core_check_feature(dev, DRIVER_SYNCOBJ))
150 drm_syncobj_open(file);
152 drm_prime_init_file_private(&file->prime);
154 if (dev->driver->open) {
155 ret = dev->driver->open(dev, file);
156 if (ret < 0)
157 goto out_prime_destroy;
160 return file;
162 out_prime_destroy:
163 drm_prime_destroy_file_private(&file->prime);
164 if (drm_core_check_feature(dev, DRIVER_SYNCOBJ))
165 drm_syncobj_release(file);
166 if (drm_core_check_feature(dev, DRIVER_GEM))
167 drm_gem_release(dev, file);
168 put_pid(file->pid);
169 kfree(file);
171 return ERR_PTR(ret);
174 static void drm_events_release(struct drm_file *file_priv)
176 struct drm_device *dev = file_priv->minor->dev;
177 struct drm_pending_event *e, *et;
178 unsigned long flags;
180 spin_lock_irqsave(&dev->event_lock, flags);
182 /* Unlink pending events */
183 list_for_each_entry_safe(e, et, &file_priv->pending_event_list,
184 pending_link) {
185 list_del(&e->pending_link);
186 e->file_priv = NULL;
189 /* Remove unconsumed events */
190 list_for_each_entry_safe(e, et, &file_priv->event_list, link) {
191 list_del(&e->link);
192 kfree(e);
195 spin_unlock_irqrestore(&dev->event_lock, flags);
199 * drm_file_free - free file context
200 * @file: context to free, or NULL
202 * This destroys and deallocates a DRM file context previously allocated via
203 * drm_file_alloc(). The caller must make sure to unlink it from any contexts
204 * before calling this.
206 * If NULL is passed, this is a no-op.
208 * RETURNS:
209 * 0 on success, or error code on failure.
211 void drm_file_free(struct drm_file *file)
213 struct drm_device *dev;
215 if (!file)
216 return;
218 dev = file->minor->dev;
220 DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
221 task_pid_nr(current),
222 (long)old_encode_dev(file->minor->kdev->devt),
223 dev->open_count);
225 if (drm_core_check_feature(dev, DRIVER_LEGACY) &&
226 dev->driver->preclose)
227 dev->driver->preclose(dev, file);
229 if (drm_core_check_feature(dev, DRIVER_LEGACY))
230 drm_legacy_lock_release(dev, file->filp);
232 if (drm_core_check_feature(dev, DRIVER_HAVE_DMA))
233 drm_legacy_reclaim_buffers(dev, file);
235 drm_events_release(file);
237 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
238 drm_fb_release(file);
239 drm_property_destroy_user_blobs(dev, file);
242 if (drm_core_check_feature(dev, DRIVER_SYNCOBJ))
243 drm_syncobj_release(file);
245 if (drm_core_check_feature(dev, DRIVER_GEM))
246 drm_gem_release(dev, file);
248 drm_legacy_ctxbitmap_flush(dev, file);
250 if (drm_is_primary_client(file))
251 drm_master_release(file);
253 if (dev->driver->postclose)
254 dev->driver->postclose(dev, file);
256 drm_prime_destroy_file_private(&file->prime);
258 WARN_ON(!list_empty(&file->event_list));
260 put_pid(file->pid);
261 kfree(file);
264 static void drm_close_helper(struct file *filp)
266 struct drm_file *file_priv = filp->private_data;
267 struct drm_device *dev = file_priv->minor->dev;
269 mutex_lock(&dev->filelist_mutex);
270 list_del(&file_priv->lhead);
271 mutex_unlock(&dev->filelist_mutex);
273 drm_file_free(file_priv);
277 * Check whether DRI will run on this CPU.
279 * \return non-zero if the DRI will run on this CPU, or zero otherwise.
281 static int drm_cpu_valid(void)
283 #if defined(__sparc__) && !defined(__sparc_v9__)
284 return 0; /* No cmpxchg before v9 sparc. */
285 #endif
286 return 1;
290 * Called whenever a process opens a drm node
292 * \param filp file pointer.
293 * \param minor acquired minor-object.
294 * \return zero on success or a negative number on failure.
296 * Creates and initializes a drm_file structure for the file private data in \p
297 * filp and add it into the double linked list in \p dev.
299 static int drm_open_helper(struct file *filp, struct drm_minor *minor)
301 struct drm_device *dev = minor->dev;
302 struct drm_file *priv;
303 int ret;
305 if (filp->f_flags & O_EXCL)
306 return -EBUSY; /* No exclusive opens */
307 if (!drm_cpu_valid())
308 return -EINVAL;
309 if (dev->switch_power_state != DRM_SWITCH_POWER_ON && dev->switch_power_state != DRM_SWITCH_POWER_DYNAMIC_OFF)
310 return -EINVAL;
312 DRM_DEBUG("pid = %d, minor = %d\n", task_pid_nr(current), minor->index);
314 priv = drm_file_alloc(minor);
315 if (IS_ERR(priv))
316 return PTR_ERR(priv);
318 if (drm_is_primary_client(priv)) {
319 ret = drm_master_open(priv);
320 if (ret) {
321 drm_file_free(priv);
322 return ret;
326 filp->private_data = priv;
327 filp->f_mode |= FMODE_UNSIGNED_OFFSET;
328 priv->filp = filp;
330 mutex_lock(&dev->filelist_mutex);
331 list_add(&priv->lhead, &dev->filelist);
332 mutex_unlock(&dev->filelist_mutex);
334 #ifdef __alpha__
336 * Default the hose
338 if (!dev->hose) {
339 struct pci_dev *pci_dev;
340 pci_dev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL);
341 if (pci_dev) {
342 dev->hose = pci_dev->sysdata;
343 pci_dev_put(pci_dev);
345 if (!dev->hose) {
346 struct pci_bus *b = list_entry(pci_root_buses.next,
347 struct pci_bus, node);
348 if (b)
349 dev->hose = b->sysdata;
352 #endif
354 return 0;
358 * drm_open - open method for DRM file
359 * @inode: device inode
360 * @filp: file pointer.
362 * This function must be used by drivers as their &file_operations.open method.
363 * It looks up the correct DRM device and instantiates all the per-file
364 * resources for it. It also calls the &drm_driver.open driver callback.
366 * RETURNS:
368 * 0 on success or negative errno value on falure.
370 int drm_open(struct inode *inode, struct file *filp)
372 struct drm_device *dev;
373 struct drm_minor *minor;
374 int retcode;
375 int need_setup = 0;
377 minor = drm_minor_acquire(iminor(inode));
378 if (IS_ERR(minor))
379 return PTR_ERR(minor);
381 dev = minor->dev;
382 if (!dev->open_count++)
383 need_setup = 1;
385 /* share address_space across all char-devs of a single device */
386 filp->f_mapping = dev->anon_inode->i_mapping;
388 retcode = drm_open_helper(filp, minor);
389 if (retcode)
390 goto err_undo;
391 if (need_setup) {
392 retcode = drm_legacy_setup(dev);
393 if (retcode) {
394 drm_close_helper(filp);
395 goto err_undo;
398 return 0;
400 err_undo:
401 dev->open_count--;
402 drm_minor_release(minor);
403 return retcode;
405 EXPORT_SYMBOL(drm_open);
407 void drm_lastclose(struct drm_device * dev)
409 DRM_DEBUG("\n");
411 if (dev->driver->lastclose)
412 dev->driver->lastclose(dev);
413 DRM_DEBUG("driver lastclose completed\n");
415 if (drm_core_check_feature(dev, DRIVER_LEGACY))
416 drm_legacy_dev_reinit(dev);
418 drm_client_dev_restore(dev);
422 * drm_release - release method for DRM file
423 * @inode: device inode
424 * @filp: file pointer.
426 * This function must be used by drivers as their &file_operations.release
427 * method. It frees any resources associated with the open file, and calls the
428 * &drm_driver.postclose driver callback. If this is the last open file for the
429 * DRM device also proceeds to call the &drm_driver.lastclose driver callback.
431 * RETURNS:
433 * Always succeeds and returns 0.
435 int drm_release(struct inode *inode, struct file *filp)
437 struct drm_file *file_priv = filp->private_data;
438 struct drm_minor *minor = file_priv->minor;
439 struct drm_device *dev = minor->dev;
441 mutex_lock(&drm_global_mutex);
443 DRM_DEBUG("open_count = %d\n", dev->open_count);
445 drm_close_helper(filp);
447 if (!--dev->open_count)
448 drm_lastclose(dev);
450 mutex_unlock(&drm_global_mutex);
452 drm_minor_release(minor);
454 return 0;
456 EXPORT_SYMBOL(drm_release);
459 * drm_read - read method for DRM file
460 * @filp: file pointer
461 * @buffer: userspace destination pointer for the read
462 * @count: count in bytes to read
463 * @offset: offset to read
465 * This function must be used by drivers as their &file_operations.read
466 * method iff they use DRM events for asynchronous signalling to userspace.
467 * Since events are used by the KMS API for vblank and page flip completion this
468 * means all modern display drivers must use it.
470 * @offset is ignored, DRM events are read like a pipe. Therefore drivers also
471 * must set the &file_operation.llseek to no_llseek(). Polling support is
472 * provided by drm_poll().
474 * This function will only ever read a full event. Therefore userspace must
475 * supply a big enough buffer to fit any event to ensure forward progress. Since
476 * the maximum event space is currently 4K it's recommended to just use that for
477 * safety.
479 * RETURNS:
481 * Number of bytes read (always aligned to full events, and can be 0) or a
482 * negative error code on failure.
484 ssize_t drm_read(struct file *filp, char __user *buffer,
485 size_t count, loff_t *offset)
487 struct drm_file *file_priv = filp->private_data;
488 struct drm_device *dev = file_priv->minor->dev;
489 ssize_t ret;
491 if (!access_ok(buffer, count))
492 return -EFAULT;
494 ret = mutex_lock_interruptible(&file_priv->event_read_lock);
495 if (ret)
496 return ret;
498 for (;;) {
499 struct drm_pending_event *e = NULL;
501 spin_lock_irq(&dev->event_lock);
502 if (!list_empty(&file_priv->event_list)) {
503 e = list_first_entry(&file_priv->event_list,
504 struct drm_pending_event, link);
505 file_priv->event_space += e->event->length;
506 list_del(&e->link);
508 spin_unlock_irq(&dev->event_lock);
510 if (e == NULL) {
511 if (ret)
512 break;
514 if (filp->f_flags & O_NONBLOCK) {
515 ret = -EAGAIN;
516 break;
519 mutex_unlock(&file_priv->event_read_lock);
520 ret = wait_event_interruptible(file_priv->event_wait,
521 !list_empty(&file_priv->event_list));
522 if (ret >= 0)
523 ret = mutex_lock_interruptible(&file_priv->event_read_lock);
524 if (ret)
525 return ret;
526 } else {
527 unsigned length = e->event->length;
529 if (length > count - ret) {
530 put_back_event:
531 spin_lock_irq(&dev->event_lock);
532 file_priv->event_space -= length;
533 list_add(&e->link, &file_priv->event_list);
534 spin_unlock_irq(&dev->event_lock);
535 wake_up_interruptible(&file_priv->event_wait);
536 break;
539 if (copy_to_user(buffer + ret, e->event, length)) {
540 if (ret == 0)
541 ret = -EFAULT;
542 goto put_back_event;
545 ret += length;
546 kfree(e);
549 mutex_unlock(&file_priv->event_read_lock);
551 return ret;
553 EXPORT_SYMBOL(drm_read);
556 * drm_poll - poll method for DRM file
557 * @filp: file pointer
558 * @wait: poll waiter table
560 * This function must be used by drivers as their &file_operations.read method
561 * iff they use DRM events for asynchronous signalling to userspace. Since
562 * events are used by the KMS API for vblank and page flip completion this means
563 * all modern display drivers must use it.
565 * See also drm_read().
567 * RETURNS:
569 * Mask of POLL flags indicating the current status of the file.
571 __poll_t drm_poll(struct file *filp, struct poll_table_struct *wait)
573 struct drm_file *file_priv = filp->private_data;
574 __poll_t mask = 0;
576 poll_wait(filp, &file_priv->event_wait, wait);
578 if (!list_empty(&file_priv->event_list))
579 mask |= EPOLLIN | EPOLLRDNORM;
581 return mask;
583 EXPORT_SYMBOL(drm_poll);
586 * drm_event_reserve_init_locked - init a DRM event and reserve space for it
587 * @dev: DRM device
588 * @file_priv: DRM file private data
589 * @p: tracking structure for the pending event
590 * @e: actual event data to deliver to userspace
592 * This function prepares the passed in event for eventual delivery. If the event
593 * doesn't get delivered (because the IOCTL fails later on, before queuing up
594 * anything) then the even must be cancelled and freed using
595 * drm_event_cancel_free(). Successfully initialized events should be sent out
596 * using drm_send_event() or drm_send_event_locked() to signal completion of the
597 * asynchronous event to userspace.
599 * If callers embedded @p into a larger structure it must be allocated with
600 * kmalloc and @p must be the first member element.
602 * This is the locked version of drm_event_reserve_init() for callers which
603 * already hold &drm_device.event_lock.
605 * RETURNS:
607 * 0 on success or a negative error code on failure.
609 int drm_event_reserve_init_locked(struct drm_device *dev,
610 struct drm_file *file_priv,
611 struct drm_pending_event *p,
612 struct drm_event *e)
614 if (file_priv->event_space < e->length)
615 return -ENOMEM;
617 file_priv->event_space -= e->length;
619 p->event = e;
620 list_add(&p->pending_link, &file_priv->pending_event_list);
621 p->file_priv = file_priv;
623 return 0;
625 EXPORT_SYMBOL(drm_event_reserve_init_locked);
628 * drm_event_reserve_init - init a DRM event and reserve space for it
629 * @dev: DRM device
630 * @file_priv: DRM file private data
631 * @p: tracking structure for the pending event
632 * @e: actual event data to deliver to userspace
634 * This function prepares the passed in event for eventual delivery. If the event
635 * doesn't get delivered (because the IOCTL fails later on, before queuing up
636 * anything) then the even must be cancelled and freed using
637 * drm_event_cancel_free(). Successfully initialized events should be sent out
638 * using drm_send_event() or drm_send_event_locked() to signal completion of the
639 * asynchronous event to userspace.
641 * If callers embedded @p into a larger structure it must be allocated with
642 * kmalloc and @p must be the first member element.
644 * Callers which already hold &drm_device.event_lock should use
645 * drm_event_reserve_init_locked() instead.
647 * RETURNS:
649 * 0 on success or a negative error code on failure.
651 int drm_event_reserve_init(struct drm_device *dev,
652 struct drm_file *file_priv,
653 struct drm_pending_event *p,
654 struct drm_event *e)
656 unsigned long flags;
657 int ret;
659 spin_lock_irqsave(&dev->event_lock, flags);
660 ret = drm_event_reserve_init_locked(dev, file_priv, p, e);
661 spin_unlock_irqrestore(&dev->event_lock, flags);
663 return ret;
665 EXPORT_SYMBOL(drm_event_reserve_init);
668 * drm_event_cancel_free - free a DRM event and release its space
669 * @dev: DRM device
670 * @p: tracking structure for the pending event
672 * This function frees the event @p initialized with drm_event_reserve_init()
673 * and releases any allocated space. It is used to cancel an event when the
674 * nonblocking operation could not be submitted and needed to be aborted.
676 void drm_event_cancel_free(struct drm_device *dev,
677 struct drm_pending_event *p)
679 unsigned long flags;
680 spin_lock_irqsave(&dev->event_lock, flags);
681 if (p->file_priv) {
682 p->file_priv->event_space += p->event->length;
683 list_del(&p->pending_link);
685 spin_unlock_irqrestore(&dev->event_lock, flags);
687 if (p->fence)
688 dma_fence_put(p->fence);
690 kfree(p);
692 EXPORT_SYMBOL(drm_event_cancel_free);
695 * drm_send_event_locked - send DRM event to file descriptor
696 * @dev: DRM device
697 * @e: DRM event to deliver
699 * This function sends the event @e, initialized with drm_event_reserve_init(),
700 * to its associated userspace DRM file. Callers must already hold
701 * &drm_device.event_lock, see drm_send_event() for the unlocked version.
703 * Note that the core will take care of unlinking and disarming events when the
704 * corresponding DRM file is closed. Drivers need not worry about whether the
705 * DRM file for this event still exists and can call this function upon
706 * completion of the asynchronous work unconditionally.
708 void drm_send_event_locked(struct drm_device *dev, struct drm_pending_event *e)
710 assert_spin_locked(&dev->event_lock);
712 if (e->completion) {
713 complete_all(e->completion);
714 e->completion_release(e->completion);
715 e->completion = NULL;
718 if (e->fence) {
719 dma_fence_signal(e->fence);
720 dma_fence_put(e->fence);
723 if (!e->file_priv) {
724 kfree(e);
725 return;
728 list_del(&e->pending_link);
729 list_add_tail(&e->link,
730 &e->file_priv->event_list);
731 wake_up_interruptible(&e->file_priv->event_wait);
733 EXPORT_SYMBOL(drm_send_event_locked);
736 * drm_send_event - send DRM event to file descriptor
737 * @dev: DRM device
738 * @e: DRM event to deliver
740 * This function sends the event @e, initialized with drm_event_reserve_init(),
741 * to its associated userspace DRM file. This function acquires
742 * &drm_device.event_lock, see drm_send_event_locked() for callers which already
743 * hold this lock.
745 * Note that the core will take care of unlinking and disarming events when the
746 * corresponding DRM file is closed. Drivers need not worry about whether the
747 * DRM file for this event still exists and can call this function upon
748 * completion of the asynchronous work unconditionally.
750 void drm_send_event(struct drm_device *dev, struct drm_pending_event *e)
752 unsigned long irqflags;
754 spin_lock_irqsave(&dev->event_lock, irqflags);
755 drm_send_event_locked(dev, e);
756 spin_unlock_irqrestore(&dev->event_lock, irqflags);
758 EXPORT_SYMBOL(drm_send_event);
761 * mock_drm_getfile - Create a new struct file for the drm device
762 * @minor: drm minor to wrap (e.g. #drm_device.primary)
763 * @flags: file creation mode (O_RDWR etc)
765 * This create a new struct file that wraps a DRM file context around a
766 * DRM minor. This mimicks userspace opening e.g. /dev/dri/card0, but without
767 * invoking userspace. The struct file may be operated on using its f_op
768 * (the drm_device.driver.fops) to mimick userspace operations, or be supplied
769 * to userspace facing functions as an internal/anonymous client.
771 * RETURNS:
772 * Pointer to newly created struct file, ERR_PTR on failure.
774 struct file *mock_drm_getfile(struct drm_minor *minor, unsigned int flags)
776 struct drm_device *dev = minor->dev;
777 struct drm_file *priv;
778 struct file *file;
780 priv = drm_file_alloc(minor);
781 if (IS_ERR(priv))
782 return ERR_CAST(priv);
784 file = anon_inode_getfile("drm", dev->driver->fops, priv, flags);
785 if (IS_ERR(file)) {
786 drm_file_free(priv);
787 return file;
790 /* Everyone shares a single global address space */
791 file->f_mapping = dev->anon_inode->i_mapping;
793 drm_dev_get(dev);
794 priv->filp = file;
796 return file;
798 EXPORT_SYMBOL_FOR_TESTS_ONLY(mock_drm_getfile);