1145132Sanholt/*-
2145132Sanholt * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
3145132Sanholt * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
4145132Sanholt * All Rights Reserved.
5145132Sanholt *
6145132Sanholt * Permission is hereby granted, free of charge, to any person obtaining a
7145132Sanholt * copy of this software and associated documentation files (the "Software"),
8145132Sanholt * to deal in the Software without restriction, including without limitation
9145132Sanholt * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10145132Sanholt * and/or sell copies of the Software, and to permit persons to whom the
11145132Sanholt * Software is furnished to do so, subject to the following conditions:
12145132Sanholt *
13145132Sanholt * The above copyright notice and this permission notice (including the next
14145132Sanholt * paragraph) shall be included in all copies or substantial portions of the
15145132Sanholt * Software.
16145132Sanholt *
17145132Sanholt * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18145132Sanholt * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19145132Sanholt * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20145132Sanholt * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21145132Sanholt * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22145132Sanholt * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23145132Sanholt * OTHER DEALINGS IN THE SOFTWARE.
24145132Sanholt *
25145132Sanholt * Authors:
26145132Sanholt *    Rickard E. (Rik) Faith <faith@valinux.com>
27145132Sanholt *    Daryll Strauss <daryll@valinux.com>
28145132Sanholt *    Gareth Hughes <gareth@valinux.com>
29145132Sanholt *
30145132Sanholt */
31145132Sanholt
32152909Sanholt#include <sys/cdefs.h>
33152909Sanholt__FBSDID("$FreeBSD$");
34152909Sanholt
35182080Srnoland/** @file drm_fops.c
36182080Srnoland * Support code for dealing with the file privates associated with each
37182080Srnoland * open of the DRM device.
38182080Srnoland */
39182080Srnoland
40145132Sanholt#include "dev/drm/drmP.h"
41145132Sanholt
42145132Sanholt/* drm_open_helper is called whenever a process opens /dev/drm. */
43145132Sanholtint drm_open_helper(struct cdev *kdev, int flags, int fmt, DRM_STRUCTPROC *p,
44182080Srnoland		    struct drm_device *dev)
45145132Sanholt{
46183573Srnoland	struct drm_file *priv;
47145132Sanholt	int retcode;
48145132Sanholt
49145132Sanholt	if (flags & O_EXCL)
50145132Sanholt		return EBUSY; /* No exclusive opens */
51145132Sanholt	dev->flags = flags;
52145132Sanholt
53196465Srnoland	DRM_DEBUG("pid = %d, device = %s\n", DRM_CURRENTPID, devtoname(kdev));
54145132Sanholt
55183833Srnoland	priv = malloc(sizeof(*priv), DRM_MEM_FILES, M_NOWAIT | M_ZERO);
56183573Srnoland	if (priv == NULL) {
57183573Srnoland		return ENOMEM;
58183573Srnoland	}
59183573Srnoland
60145132Sanholt	DRM_LOCK();
61183573Srnoland	priv->dev		= dev;
62183573Srnoland	priv->uid		= p->td_ucred->cr_svuid;
63183573Srnoland	priv->pid		= p->td_proc->p_pid;
64183573Srnoland	priv->ioctl_count 	= 0;
65145132Sanholt
66183573Srnoland	/* for compatibility root is always authenticated */
67183573Srnoland	priv->authenticated	= DRM_SUSER(p);
68145132Sanholt
69183573Srnoland	if (dev->driver->open) {
70183573Srnoland		/* shared code returns -errno */
71183573Srnoland		retcode = -dev->driver->open(dev, priv);
72183573Srnoland		if (retcode != 0) {
73183833Srnoland			free(priv, DRM_MEM_FILES);
74183573Srnoland			DRM_UNLOCK();
75183573Srnoland			return retcode;
76145132Sanholt		}
77183573Srnoland	}
78145132Sanholt
79183573Srnoland	/* first opener automatically becomes master */
80183573Srnoland	priv->master = TAILQ_EMPTY(&dev->files);
81152909Sanholt
82183573Srnoland	TAILQ_INSERT_TAIL(&dev->files, priv, link);
83145132Sanholt	DRM_UNLOCK();
84145132Sanholt	kdev->si_drv1 = dev;
85241088Shselasky
86241088Shselasky	retcode = devfs_set_cdevpriv(priv, drm_close);
87241088Shselasky	if (retcode != 0)
88241088Shselasky		drm_close(priv);
89241088Shselasky
90241088Shselasky	return (retcode);
91145132Sanholt}
92145132Sanholt
93145132Sanholt
94145132Sanholt/* The drm_read and drm_poll are stubs to prevent spurious errors
95145132Sanholt * on older X Servers (4.3.0 and earlier) */
96145132Sanholt
97145132Sanholtint drm_read(struct cdev *kdev, struct uio *uio, int ioflag)
98145132Sanholt{
99145132Sanholt	return 0;
100145132Sanholt}
101145132Sanholt
102145132Sanholtint drm_poll(struct cdev *kdev, int events, DRM_STRUCTPROC *p)
103145132Sanholt{
104145132Sanholt	return 0;
105145132Sanholt}
106