160405de4Skz151634 /*
2*d0538f66Scg149915 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
360405de4Skz151634 * Use is subject to license terms.
460405de4Skz151634 */
560405de4Skz151634
660405de4Skz151634 /*
760405de4Skz151634 * drm_auth.h -- IOCTLs for authentication -*- linux-c -*-
860405de4Skz151634 * Created: Tue Feb 2 08:37:54 1999 by faith@valinux.com
960405de4Skz151634 */
1060405de4Skz151634 /*
1160405de4Skz151634 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
1260405de4Skz151634 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
1360405de4Skz151634 * All Rights Reserved.
1460405de4Skz151634 *
1560405de4Skz151634 * Permission is hereby granted, free of charge, to any person obtaining a
1660405de4Skz151634 * copy of this software and associated documentation files (the "Software"),
1760405de4Skz151634 * to deal in the Software without restriction, including without limitation
1860405de4Skz151634 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
1960405de4Skz151634 * and/or sell copies of the Software, and to permit persons to whom the
2060405de4Skz151634 * Software is furnished to do so, subject to the following conditions:
2160405de4Skz151634 *
2260405de4Skz151634 * The above copyright notice and this permission notice (including the next
2360405de4Skz151634 * paragraph) shall be included in all copies or substantial portions of the
2460405de4Skz151634 * Software.
2560405de4Skz151634 *
2660405de4Skz151634 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2760405de4Skz151634 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2860405de4Skz151634 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
2960405de4Skz151634 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
3060405de4Skz151634 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
3160405de4Skz151634 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
3260405de4Skz151634 * OTHER DEALINGS IN THE SOFTWARE.
3360405de4Skz151634 *
3460405de4Skz151634 * Authors:
3560405de4Skz151634 * Rickard E. (Rik) Faith <faith@valinux.com>
3660405de4Skz151634 * Gareth Hughes <gareth@valinux.com>
3760405de4Skz151634 *
3860405de4Skz151634 */
3960405de4Skz151634
4060405de4Skz151634 #pragma ident "%Z%%M% %I% %E% SMI"
4160405de4Skz151634
4260405de4Skz151634 #include "drmP.h"
4360405de4Skz151634
4460405de4Skz151634 static int
drm_hash_magic(drm_magic_t magic)4560405de4Skz151634 drm_hash_magic(drm_magic_t magic)
4660405de4Skz151634 {
4760405de4Skz151634 return (magic & (DRM_HASH_SIZE-1));
4860405de4Skz151634 }
4960405de4Skz151634
5060405de4Skz151634 drm_file_t *
drm_find_file(drm_device_t * dev,drm_magic_t magic)5160405de4Skz151634 drm_find_file(drm_device_t *dev, drm_magic_t magic)
5260405de4Skz151634 {
5360405de4Skz151634 drm_file_t *retval = NULL;
5460405de4Skz151634 drm_magic_entry_t *pt;
5560405de4Skz151634 int hash;
5660405de4Skz151634
5760405de4Skz151634 hash = drm_hash_magic(magic);
5860405de4Skz151634 for (pt = dev->magiclist[hash].head; pt; pt = pt->next) {
5960405de4Skz151634 if (pt->magic == magic) {
6060405de4Skz151634 retval = pt->priv;
6160405de4Skz151634 break;
6260405de4Skz151634 }
6360405de4Skz151634 }
64*d0538f66Scg149915
6560405de4Skz151634 return (retval);
6660405de4Skz151634 }
6760405de4Skz151634
6860405de4Skz151634 static int
drm_add_magic(drm_device_t * dev,drm_file_t * priv,drm_magic_t magic)6960405de4Skz151634 drm_add_magic(drm_device_t *dev, drm_file_t *priv, drm_magic_t magic)
7060405de4Skz151634 {
7160405de4Skz151634 int hash;
7260405de4Skz151634 drm_magic_entry_t *entry;
7360405de4Skz151634
7460405de4Skz151634 hash = drm_hash_magic(magic);
7560405de4Skz151634 entry = drm_alloc(sizeof (*entry), DRM_MEM_MAGIC);
7660405de4Skz151634 if (!entry)
77*d0538f66Scg149915 return (ENOMEM);
7860405de4Skz151634 entry->magic = magic;
7960405de4Skz151634 entry->priv = priv;
8060405de4Skz151634 entry->next = NULL;
8160405de4Skz151634
8260405de4Skz151634 DRM_LOCK();
8360405de4Skz151634 if (dev->magiclist[hash].tail) {
8460405de4Skz151634 dev->magiclist[hash].tail->next = entry;
8560405de4Skz151634 dev->magiclist[hash].tail = entry;
8660405de4Skz151634 } else {
8760405de4Skz151634 dev->magiclist[hash].head = entry;
8860405de4Skz151634 dev->magiclist[hash].tail = entry;
8960405de4Skz151634 }
9060405de4Skz151634 DRM_UNLOCK();
9160405de4Skz151634
9260405de4Skz151634 return (0);
9360405de4Skz151634 }
9460405de4Skz151634
9560405de4Skz151634 int
drm_remove_magic(drm_device_t * dev,drm_magic_t magic)9660405de4Skz151634 drm_remove_magic(drm_device_t *dev, drm_magic_t magic)
9760405de4Skz151634 {
9860405de4Skz151634 drm_magic_entry_t *prev = NULL;
9960405de4Skz151634 drm_magic_entry_t *pt;
10060405de4Skz151634 int hash;
10160405de4Skz151634
10260405de4Skz151634 DRM_DEBUG("drm_remove_magic : %d", magic);
10360405de4Skz151634 hash = drm_hash_magic(magic);
10460405de4Skz151634
10560405de4Skz151634 DRM_LOCK();
10660405de4Skz151634 for (pt = dev->magiclist[hash].head; pt; prev = pt, pt = pt->next) {
10760405de4Skz151634 if (pt->magic == magic) {
10860405de4Skz151634 if (dev->magiclist[hash].head == pt) {
10960405de4Skz151634 dev->magiclist[hash].head = pt->next;
11060405de4Skz151634 }
11160405de4Skz151634 if (dev->magiclist[hash].tail == pt) {
11260405de4Skz151634 dev->magiclist[hash].tail = prev;
11360405de4Skz151634 }
11460405de4Skz151634 if (prev) {
11560405de4Skz151634 prev->next = pt->next;
11660405de4Skz151634 }
11760405de4Skz151634 DRM_UNLOCK();
11860405de4Skz151634 drm_free(pt, sizeof (*pt), DRM_MEM_MAGIC);
11960405de4Skz151634 return (0);
12060405de4Skz151634 }
12160405de4Skz151634 }
12260405de4Skz151634 DRM_UNLOCK();
12360405de4Skz151634
124*d0538f66Scg149915 return (EINVAL);
12560405de4Skz151634 }
12660405de4Skz151634
12760405de4Skz151634 /*ARGSUSED*/
12860405de4Skz151634 int
drm_getmagic(DRM_IOCTL_ARGS)12960405de4Skz151634 drm_getmagic(DRM_IOCTL_ARGS)
13060405de4Skz151634 {
13160405de4Skz151634 DRM_DEVICE;
13260405de4Skz151634 static drm_magic_t sequence = 0;
13360405de4Skz151634 drm_auth_t auth;
13460405de4Skz151634
13560405de4Skz151634 /* Find unique magic */
136*d0538f66Scg149915 if (fpriv->magic) {
137*d0538f66Scg149915 auth.magic = fpriv->magic;
13860405de4Skz151634 } else {
13960405de4Skz151634 do {
14060405de4Skz151634 int old = sequence;
14160405de4Skz151634 auth.magic = old+1;
14260405de4Skz151634 if (!atomic_cmpset_int(&sequence, old, auth.magic))
14360405de4Skz151634 continue;
14460405de4Skz151634 } while (drm_find_file(dev, auth.magic));
145*d0538f66Scg149915 fpriv->magic = auth.magic;
146*d0538f66Scg149915 (void) drm_add_magic(dev, fpriv, auth.magic);
14760405de4Skz151634 }
14860405de4Skz151634
149*d0538f66Scg149915
15060405de4Skz151634 DRM_DEBUG("drm_getmagic: %u", auth.magic);
15160405de4Skz151634
152*d0538f66Scg149915 DRM_COPYTO_WITH_RETURN((void *)data, &auth, sizeof (auth));
15360405de4Skz151634
15460405de4Skz151634 return (0);
15560405de4Skz151634 }
15660405de4Skz151634
15760405de4Skz151634 /*ARGSUSED*/
15860405de4Skz151634 int
drm_authmagic(DRM_IOCTL_ARGS)15960405de4Skz151634 drm_authmagic(DRM_IOCTL_ARGS)
16060405de4Skz151634 {
16160405de4Skz151634 drm_auth_t auth;
16260405de4Skz151634 drm_file_t *file;
16360405de4Skz151634 DRM_DEVICE;
16460405de4Skz151634
165*d0538f66Scg149915 DRM_COPYFROM_WITH_RETURN(&auth, (void *)data, sizeof (auth));
16660405de4Skz151634
16760405de4Skz151634 if ((file = drm_find_file(dev, auth.magic))) {
16860405de4Skz151634 file->authenticated = 1;
16960405de4Skz151634 (void) drm_remove_magic(dev, auth.magic);
17060405de4Skz151634 return (0);
17160405de4Skz151634 }
172*d0538f66Scg149915 return (EINVAL);
17360405de4Skz151634 }
174