/**1* \file drm_ioctl.c2* IOCTL processing for DRM3*4* \author Rickard E. (Rik) Faith <[email protected]>5* \author Gareth Hughes <[email protected]>6*/78/*9* Created: Fri Jan 8 09:01:26 1999 by [email protected]10*11* Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.12* Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.13* All Rights Reserved.14*15* Permission is hereby granted, free of charge, to any person obtaining a16* copy of this software and associated documentation files (the "Software"),17* to deal in the Software without restriction, including without limitation18* the rights to use, copy, modify, merge, publish, distribute, sublicense,19* and/or sell copies of the Software, and to permit persons to whom the20* Software is furnished to do so, subject to the following conditions:21*22* The above copyright notice and this permission notice (including the next23* paragraph) shall be included in all copies or substantial portions of the24* Software.25*26* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR27* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,28* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL29* VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR30* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,31* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR32* OTHER DEALINGS IN THE SOFTWARE.33*/3435#include "drmP.h"36#include "drm_core.h"3738#include "linux/pci.h"3940/**41* Get the bus id.42*43* \param inode device inode.44* \param file_priv DRM file private.45* \param cmd command.46* \param arg user argument, pointing to a drm_unique structure.47* \return zero on success or a negative number on failure.48*49* Copies the bus id from drm_device::unique into user space.50*/51int drm_getunique(struct drm_device *dev, void *data,52struct drm_file *file_priv)53{54struct drm_unique *u = data;55struct drm_master *master = file_priv->master;5657if (u->unique_len >= master->unique_len) {58if (copy_to_user(u->unique, master->unique, master->unique_len))59return -EFAULT;60}61u->unique_len = master->unique_len;6263return 0;64}6566static void67drm_unset_busid(struct drm_device *dev,68struct drm_master *master)69{70kfree(dev->devname);71dev->devname = NULL;7273kfree(master->unique);74master->unique = NULL;75master->unique_len = 0;76master->unique_size = 0;77}7879/**80* Set the bus id.81*82* \param inode device inode.83* \param file_priv DRM file private.84* \param cmd command.85* \param arg user argument, pointing to a drm_unique structure.86* \return zero on success or a negative number on failure.87*88* Copies the bus id from userspace into drm_device::unique, and verifies that89* it matches the device this DRM is attached to (EINVAL otherwise). Deprecated90* in interface version 1.1 and will return EBUSY when setversion has requested91* version 1.1 or greater.92*/93int drm_setunique(struct drm_device *dev, void *data,94struct drm_file *file_priv)95{96struct drm_unique *u = data;97struct drm_master *master = file_priv->master;98int ret;99100if (master->unique_len || master->unique)101return -EBUSY;102103if (!u->unique_len || u->unique_len > 1024)104return -EINVAL;105106if (!dev->driver->bus->set_unique)107return -EINVAL;108109ret = dev->driver->bus->set_unique(dev, master, u);110if (ret)111goto err;112113return 0;114115err:116drm_unset_busid(dev, master);117return ret;118}119120static int drm_set_busid(struct drm_device *dev, struct drm_file *file_priv)121{122struct drm_master *master = file_priv->master;123int ret;124125if (master->unique != NULL)126drm_unset_busid(dev, master);127128ret = dev->driver->bus->set_busid(dev, master);129if (ret)130goto err;131return 0;132err:133drm_unset_busid(dev, master);134return ret;135}136137/**138* Get a mapping information.139*140* \param inode device inode.141* \param file_priv DRM file private.142* \param cmd command.143* \param arg user argument, pointing to a drm_map structure.144*145* \return zero on success or a negative number on failure.146*147* Searches for the mapping with the specified offset and copies its information148* into userspace149*/150int drm_getmap(struct drm_device *dev, void *data,151struct drm_file *file_priv)152{153struct drm_map *map = data;154struct drm_map_list *r_list = NULL;155struct list_head *list;156int idx;157int i;158159idx = map->offset;160161mutex_lock(&dev->struct_mutex);162if (idx < 0) {163mutex_unlock(&dev->struct_mutex);164return -EINVAL;165}166167i = 0;168list_for_each(list, &dev->maplist) {169if (i == idx) {170r_list = list_entry(list, struct drm_map_list, head);171break;172}173i++;174}175if (!r_list || !r_list->map) {176mutex_unlock(&dev->struct_mutex);177return -EINVAL;178}179180map->offset = r_list->map->offset;181map->size = r_list->map->size;182map->type = r_list->map->type;183map->flags = r_list->map->flags;184map->handle = (void *)(unsigned long) r_list->user_token;185map->mtrr = r_list->map->mtrr;186mutex_unlock(&dev->struct_mutex);187188return 0;189}190191/**192* Get client information.193*194* \param inode device inode.195* \param file_priv DRM file private.196* \param cmd command.197* \param arg user argument, pointing to a drm_client structure.198*199* \return zero on success or a negative number on failure.200*201* Searches for the client with the specified index and copies its information202* into userspace203*/204int drm_getclient(struct drm_device *dev, void *data,205struct drm_file *file_priv)206{207struct drm_client *client = data;208struct drm_file *pt;209int idx;210int i;211212idx = client->idx;213mutex_lock(&dev->struct_mutex);214215i = 0;216list_for_each_entry(pt, &dev->filelist, lhead) {217if (i++ >= idx) {218client->auth = pt->authenticated;219client->pid = pt->pid;220client->uid = pt->uid;221client->magic = pt->magic;222client->iocs = pt->ioctl_count;223mutex_unlock(&dev->struct_mutex);224225return 0;226}227}228mutex_unlock(&dev->struct_mutex);229230return -EINVAL;231}232233/**234* Get statistics information.235*236* \param inode device inode.237* \param file_priv DRM file private.238* \param cmd command.239* \param arg user argument, pointing to a drm_stats structure.240*241* \return zero on success or a negative number on failure.242*/243int drm_getstats(struct drm_device *dev, void *data,244struct drm_file *file_priv)245{246struct drm_stats *stats = data;247int i;248249memset(stats, 0, sizeof(*stats));250251mutex_lock(&dev->struct_mutex);252253for (i = 0; i < dev->counters; i++) {254if (dev->types[i] == _DRM_STAT_LOCK)255stats->data[i].value =256(file_priv->master->lock.hw_lock ? file_priv->master->lock.hw_lock->lock : 0);257else258stats->data[i].value = atomic_read(&dev->counts[i]);259stats->data[i].type = dev->types[i];260}261262stats->count = dev->counters;263264mutex_unlock(&dev->struct_mutex);265266return 0;267}268269/**270* Get device/driver capabilities271*/272int drm_getcap(struct drm_device *dev, void *data, struct drm_file *file_priv)273{274struct drm_get_cap *req = data;275276req->value = 0;277switch (req->capability) {278case DRM_CAP_DUMB_BUFFER:279if (dev->driver->dumb_create)280req->value = 1;281break;282case DRM_CAP_VBLANK_HIGH_CRTC:283req->value = 1;284break;285default:286return -EINVAL;287}288return 0;289}290291/**292* Setversion ioctl.293*294* \param inode device inode.295* \param file_priv DRM file private.296* \param cmd command.297* \param arg user argument, pointing to a drm_lock structure.298* \return zero on success or negative number on failure.299*300* Sets the requested interface version301*/302int drm_setversion(struct drm_device *dev, void *data, struct drm_file *file_priv)303{304struct drm_set_version *sv = data;305int if_version, retcode = 0;306307if (sv->drm_di_major != -1) {308if (sv->drm_di_major != DRM_IF_MAJOR ||309sv->drm_di_minor < 0 || sv->drm_di_minor > DRM_IF_MINOR) {310retcode = -EINVAL;311goto done;312}313if_version = DRM_IF_VERSION(sv->drm_di_major,314sv->drm_di_minor);315dev->if_version = max(if_version, dev->if_version);316if (sv->drm_di_minor >= 1) {317/*318* Version 1.1 includes tying of DRM to specific device319* Version 1.4 has proper PCI domain support320*/321retcode = drm_set_busid(dev, file_priv);322if (retcode)323goto done;324}325}326327if (sv->drm_dd_major != -1) {328if (sv->drm_dd_major != dev->driver->major ||329sv->drm_dd_minor < 0 || sv->drm_dd_minor >330dev->driver->minor) {331retcode = -EINVAL;332goto done;333}334335if (dev->driver->set_version)336dev->driver->set_version(dev, sv);337}338339done:340sv->drm_di_major = DRM_IF_MAJOR;341sv->drm_di_minor = DRM_IF_MINOR;342sv->drm_dd_major = dev->driver->major;343sv->drm_dd_minor = dev->driver->minor;344345return retcode;346}347348/** No-op ioctl. */349int drm_noop(struct drm_device *dev, void *data,350struct drm_file *file_priv)351{352DRM_DEBUG("\n");353return 0;354}355356357