1450N/A/*
1450N/A * Copyright (c) 2006, 2015, Oracle and/or its affiliates. All rights reserved.
1450N/A */
1450N/A
1450N/A/* drm_fops.h -- File operations for DRM -*- linux-c -*-
1450N/A * Created: Mon Jan 4 08:58:31 1999 by faith@valinux.com
1450N/A */
1450N/A/*-
1450N/A * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
1450N/A * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
1450N/A * All Rights Reserved.
1450N/A *
1450N/A * Permission is hereby granted, free of charge, to any person obtaining a
1450N/A * copy of this software and associated documentation files (the "Software"),
1450N/A * to deal in the Software without restriction, including without limitation
1450N/A * the rights to use, copy, modify, merge, publish, distribute, sublicense,
1450N/A * and/or sell copies of the Software, and to permit persons to whom the
1450N/A * Software is furnished to do so, subject to the following conditions:
1450N/A *
1450N/A * The above copyright notice and this permission notice (including the next
1450N/A * paragraph) shall be included in all copies or substantial portions of the
1450N/A * Software.
1450N/A *
1450N/A * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1450N/A * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1450N/A * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
1450N/A * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
1450N/A * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
1450N/A * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
1450N/A * OTHER DEALINGS IN THE SOFTWARE.
1450N/A *
1450N/A * Authors:
1450N/A * Rickard E. (Rik) Faith <faith@valinux.com>
1450N/A * Daryll Strauss <daryll@valinux.com>
1450N/A * Gareth Hughes <gareth@valinux.com>
1450N/A *
1450N/A */
1450N/A
1450N/A#include "drmP.h"
1450N/A
1450N/A/*ARGSUSED*/
1450N/Adrm_file_t *
1450N/Adrm_find_file_by_proc(drm_device_t *dev, cred_t *credp)
1450N/A{
1450N/A pid_t pid = ddi_get_pid();
1450N/A drm_file_t *priv;
1450N/A
1450N/A TAILQ_FOREACH(priv, &dev->files, link)
1450N/A if (priv->pid == pid)
1450N/A return (priv);
1450N/A return (NULL);
1450N/A}
1450N/A
1450N/A
1450N/Adrm_cminor_t *
1450N/Adrm_find_file_by_minor(drm_device_t *dev, int minor)
1450N/A{
1450N/A drm_cminor_t *mp;
1450N/A
1450N/A TAILQ_FOREACH(mp, &dev->minordevs, link) {
1450N/A if (mp->minor == minor)
1450N/A return (mp);
1450N/A }
1450N/A return (NULL);
1450N/A}
1450N/A
1450N/A/* drm_open_helper is called whenever a process opens /dev/drm. */
1450N/A/*ARGSUSED*/
1450N/Aint
1450N/Adrm_open_helper(drm_device_t *dev, drm_cminor_t *mp, int flags,
1450N/A int otyp, cred_t *credp)
1450N/A{
1450N/A drm_file_t *priv;
1450N/A pid_t pid;
1450N/A int retcode;
1450N/A
1450N/A if (flags & FEXCL)
1450N/A return (EBUSY); /* No exclusive opens */
1450N/A dev->flags = flags;
1450N/A
1450N/A pid = ddi_get_pid();
1450N/A DRM_DEBUG("drm_open_helper :pid = %d", pid);
1450N/A
1450N/A DRM_LOCK();
1450N/A priv = drm_find_file_by_proc(dev, credp);
1450N/A if (priv) {
1450N/A priv->refs++;
1450N/A } else {
1450N/A priv = drm_alloc(sizeof (*priv), DRM_MEM_FILES);
1450N/A if (priv == NULL) {
1450N/A DRM_UNLOCK();
1450N/A return (ENOMEM);
1450N/A }
1450N/A bzero(priv, sizeof (*priv));
1450N/A
1450N/A priv->uid = crgetsuid(credp);
1450N/A priv->pid = pid;
1450N/A
1450N/A priv->refs = 1;
1450N/A priv->minor = 5; /* just for hack */
1450N/A priv->ioctl_count = 0;
1450N/A
1450N/A /* for compatibility root is always authenticated */
1450N/A priv->authenticated = DRM_SUSER(credp);
1450N/A
1450N/A if (dev->driver->open) {
1450N/A retcode = dev->driver->open(dev, priv);
1450N/A if (retcode != 0) {
1450N/A drm_free(priv, sizeof (*priv), DRM_MEM_FILES);
1450N/A DRM_UNLOCK();
1450N/A return (retcode);
1450N/A }
1450N/A }
1450N/A
1450N/A /* first opener automatically becomes master */
1450N/A priv->master = TAILQ_EMPTY(&dev->files);
1450N/A TAILQ_INSERT_TAIL(&dev->files, priv, link);
1450N/A }
1450N/A mp->fpriv = priv;
1450N/A DRM_UNLOCK();
1450N/A return (0);
1450N/A}