patch to import Jon Smirl's work from Bitkeeper
[mesa.git] / src / glx / mini / xf86drm.c
1 /**
2 * \file xf86drm.c
3 * \brief User-level interface to DRM device
4 *
5 * This file is an user-friendly interface to the DRM ioctls defined in drm.h.
6 *
7 * This covers only the device-independent ioctls -- it is up to the driver to
8 * wrap the device-dependent ioctls.
9 *
10 * \author Rickard E. (Rik) Faith <faith@valinux.com>
11 * \author Kevin E. Martin <martin@valinux.com>
12 */
13
14 /*
15 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
16 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
17 * All Rights Reserved.
18 *
19 * Permission is hereby granted, free of charge, to any person obtaining a
20 * copy of this software and associated documentation files (the "Software"),
21 * to deal in the Software without restriction, including without limitation
22 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
23 * and/or sell copies of the Software, and to permit persons to whom the
24 * Software is furnished to do so, subject to the following conditions:
25 *
26 * The above copyright notice and this permission notice (including the next
27 * paragraph) shall be included in all copies or substantial portions of the
28 * Software.
29 *
30 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
31 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
32 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
33 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
34 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
35 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
36 * DEALINGS IN THE SOFTWARE.
37 */
38
39 # include <stdio.h>
40 # include <stdlib.h>
41 # include <unistd.h>
42 # include <string.h>
43 # include <ctype.h>
44 # include <fcntl.h>
45 # include <errno.h>
46 # include <signal.h>
47 # include <sys/types.h>
48 # include <sys/stat.h>
49 # include <sys/ioctl.h>
50 # include <sys/mman.h>
51 # include <sys/time.h>
52 # include <stdarg.h>
53 # include "drm.h"
54
55 /* Not all systems have MAP_FAILED defined */
56 #ifndef MAP_FAILED
57 #define MAP_FAILED ((void *)-1)
58 #endif
59
60 #include "xf86drm.h"
61
62 #ifndef DRM_MAJOR
63 #define DRM_MAJOR 226 /* Linux */
64 #endif
65
66 #ifndef __linux__
67 #undef DRM_MAJOR
68 #define DRM_MAJOR 145 /* Should set in drm.h for *BSD */
69 #endif
70
71 #ifndef DRM_MAX_MINOR
72 #define DRM_MAX_MINOR 16
73 #endif
74
75 #ifdef __linux__
76 #include <sys/sysmacros.h> /* for makedev() */
77 #endif
78
79 #ifndef makedev
80 /* This definition needs to be changed on
81 some systems if dev_t is a structure.
82 If there is a header file we can get it
83 from, there would be best. */
84 #define makedev(x,y) ((dev_t)(((x) << 8) | (y)))
85 #endif
86
87
88 /**
89 * \brief Output a message to stderr.
90 *
91 * \param format printf() like format string.
92 *
93 * \internal
94 * This function is a wrapper around vfprintf().
95 */
96 static void
97 drmMsg(const char *format, ...)
98 {
99 va_list ap;
100
101 const char *env;
102 if ((env = getenv("LIBGL_DEBUG")) && strstr(env, "verbose"))
103 {
104 va_start(ap, format);
105 vfprintf(stderr, format, ap);
106 va_end(ap);
107 }
108 }
109
110
111
112 /**
113 * \brief Open the DRM device, creating it if necessary.
114 *
115 * \param dev major and minor numbers of the device.
116 * \param minor minor number of the device.
117 *
118 * \return a file descriptor on success, or a negative value on error.
119 *
120 * \internal
121 * Assembles the device name from \p minor and opens it, creating the device
122 * special file node with the major and minor numbers specified by \p dev and
123 * parent directory if necessary and was called by root.
124 */
125 static int drmOpenDevice(long dev, int minor)
126 {
127 struct stat st;
128 char buf[64];
129 int fd;
130 mode_t devmode = DRM_DEV_MODE;
131 int isroot = !geteuid();
132 #if defined(XFree86Server)
133 uid_t user = DRM_DEV_UID;
134 gid_t group = DRM_DEV_GID;
135 #endif
136
137 drmMsg("drmOpenDevice: minor is %d\n", minor);
138
139 #if defined(XFree86Server)
140 devmode = xf86ConfigDRI.mode ? xf86ConfigDRI.mode : DRM_DEV_MODE;
141 devmode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
142 group = (xf86ConfigDRI.group >= 0) ? xf86ConfigDRI.group : DRM_DEV_GID;
143 #endif
144
145 if (stat(DRM_DIR_NAME, &st)) {
146 if (!isroot) return DRM_ERR_NOT_ROOT;
147 mkdir(DRM_DIR_NAME, DRM_DEV_DIRMODE);
148 chown(DRM_DIR_NAME, 0, 0); /* root:root */
149 chmod(DRM_DIR_NAME, DRM_DEV_DIRMODE);
150 }
151
152 sprintf(buf, DRM_DEV_NAME, DRM_DIR_NAME, minor);
153 drmMsg("drmOpenDevice: node name is %s\n", buf);
154 if (stat(buf, &st)) {
155 if (!isroot) return DRM_ERR_NOT_ROOT;
156 remove(buf);
157 mknod(buf, S_IFCHR | devmode, dev);
158 }
159 #if defined(XFree86Server)
160 chown(buf, user, group);
161 chmod(buf, devmode);
162 #endif
163
164 fd = open(buf, O_RDWR, 0);
165 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
166 fd, fd < 0 ? strerror(errno) : "OK");
167 if (fd >= 0) return fd;
168
169 if (st.st_rdev != dev) {
170 if (!isroot) return DRM_ERR_NOT_ROOT;
171 remove(buf);
172 mknod(buf, S_IFCHR | devmode, dev);
173 }
174 fd = open(buf, O_RDWR, 0);
175 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
176 fd, fd < 0 ? strerror(errno) : "OK");
177 if (fd >= 0) return fd;
178
179 drmMsg("drmOpenDevice: Open failed\n");
180 remove(buf);
181 return -errno;
182 }
183
184
185 /**
186 * \brief Open the DRM device
187 *
188 * \param minor device minor number.
189 * \param create allow to create the device if set.
190 *
191 * \return a file descriptor on success, or a negative value on error.
192 *
193 * \internal
194 * Calls drmOpenDevice() if \p create is set, otherwise assembles the device
195 * name from \p minor and opens it.
196 */
197 static int drmOpenMinor(int minor, int create)
198 {
199 int fd;
200 char buf[64];
201
202 if (create) return drmOpenDevice(makedev(DRM_MAJOR, minor), minor);
203
204 sprintf(buf, DRM_DEV_NAME, DRM_DIR_NAME, minor);
205 if ((fd = open(buf, O_RDWR, 0)) >= 0) return fd;
206 drmMsg("drmOpenMinor: open result is %d, (%s)\n",
207 fd, fd < 0 ? strerror(errno) : "OK");
208 return -errno;
209 }
210
211
212 /**
213 * \brief Determine whether the DRM kernel driver has been loaded.
214 *
215 * \return 1 if the DRM driver is loaded, 0 otherwise.
216 *
217 * \internal
218 * Determine the presence of the kernel driver by attempting to open the 0
219 * minor and get version information. For backward compatibility with older
220 * Linux implementations, /proc/dri is also checked.
221 */
222 int drmAvailable(void)
223 {
224 drmVersionPtr version;
225 int retval = 0;
226 int fd;
227
228 if ((fd = drmOpenMinor(0, 1)) < 0) {
229 /* Try proc for backward Linux compatibility */
230 if (!access("/proc/dri/0", R_OK)) return 1;
231 return 0;
232 }
233
234 if ((version = drmGetVersion(fd))) {
235 retval = 1;
236 drmFreeVersion(version);
237 }
238 close(fd);
239 drmMsg("close %d\n", fd);
240
241 return retval;
242 }
243
244
245 /**
246 * \brief Open the device by bus ID.
247 *
248 * \param busid bus ID.
249 *
250 * \return a file descriptor on success, or a negative value on error.
251 *
252 * \internal
253 * This function attempts to open every possible minor (up to DRM_MAX_MINOR),
254 * comparing the device bus ID with the one supplied.
255 *
256 * \sa drmOpenMinor() and drmGetBusid().
257 */
258 static int drmOpenByBusid(const char *busid)
259 {
260 int i;
261 int fd;
262 const char *buf;
263
264 drmMsg("drmOpenByBusid: busid is %s\n", busid);
265 for (i = 0; i < DRM_MAX_MINOR; i++) {
266 fd = drmOpenMinor(i, 1);
267 drmMsg("drmOpenByBusid: drmOpenMinor returns %d\n", fd);
268 if (fd >= 0) {
269 buf = drmGetBusid(fd);
270 drmMsg("drmOpenByBusid: drmGetBusid reports %s\n", buf);
271 if (buf && !strcmp(buf, busid)) {
272 drmFreeBusid(buf);
273 return fd;
274 }
275 if (buf) drmFreeBusid(buf);
276 close(fd);
277 drmMsg("close %d\n", fd);
278 }
279 }
280 return -1;
281 }
282
283
284 /**
285 * \brief Open the device by name.
286 *
287 * \param name driver name.
288 *
289 * \return a file descriptor on success, or a negative value on error.
290 *
291 * \internal
292 * This function opens the first minor number that matches the driver name and
293 * isn't already in use. If it's in use it then it will already have a bus ID
294 * assigned.
295 *
296 * \sa drmOpenMinor(), drmGetVersion() and drmGetBusid().
297 */
298 static int drmOpenByName(const char *name)
299 {
300 int i;
301 int fd;
302 drmVersionPtr version;
303 char * id;
304
305 if (!drmAvailable()) {
306 #if !defined(XFree86Server)
307 return -1;
308 #else
309 /* try to load the kernel module now */
310 if (!xf86LoadKernelModule(name)) {
311 ErrorF("[drm] failed to load kernel module \"%s\"\n",
312 name);
313 return -1;
314 }
315 #endif
316 }
317
318 for (i = 0; i < DRM_MAX_MINOR; i++) {
319 if ((fd = drmOpenMinor(i, 1)) >= 0) {
320 if ((version = drmGetVersion(fd))) {
321 if (!strcmp(version->name, name)) {
322 drmFreeVersion(version);
323
324 /* return fd; */
325
326 id = drmGetBusid(fd);
327 drmMsg("drmGetBusid returned '%s'\n", id ? id : "NULL");
328 if (!id || !*id) {
329 if (id) {
330 drmFreeBusid(id);
331 }
332 return fd;
333 } else {
334 drmFreeBusid(id);
335 }
336 } else {
337 drmFreeVersion(version);
338 }
339 }
340 close(fd);
341 drmMsg("close %d\n", fd);
342 }
343 }
344
345 return -1;
346 }
347
348
349 /**
350 * \brief Open the DRM device.
351 *
352 * Looks up the specified name and bus ID, and opens the device found. The
353 * entry in /dev/dri is created if necessary and if called by root.
354 *
355 * \param name driver name. Not referenced if bus ID is supplied.
356 * \param busid bus ID. Zero if not known.
357 *
358 * \return a file descriptor on success, or a negative value on error.
359 *
360 * \internal
361 * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
362 * otherwise.
363 */
364 int drmOpen(const char *name, const char *busid)
365 {
366
367 if (busid) return drmOpenByBusid(busid);
368 return drmOpenByName(name);
369 }
370
371
372 /**
373 * \brief Free the version information returned by drmGetVersion().
374 *
375 * \param v pointer to the version information.
376 *
377 * \internal
378 * It frees the memory pointed by \p %v as well as all the non-null strings
379 * pointers in it.
380 */
381 void drmFreeVersion(drmVersionPtr v)
382 {
383 if (!v) return;
384 if (v->name) free(v->name);
385 if (v->date) free(v->date);
386 if (v->desc) free(v->desc);
387 free(v);
388 }
389
390
391 /**
392 * \brief Free the non-public version information returned by the kernel.
393 *
394 * \param v pointer to the version information.
395 *
396 * \internal
397 * Used by drmGetVersion() to free the memory pointed by \p %v as well as all
398 * the non-null strings pointers in it.
399 */
400 static void drmFreeKernelVersion(drm_version_t *v)
401 {
402 if (!v) return;
403 if (v->name) free(v->name);
404 if (v->date) free(v->date);
405 if (v->desc) free(v->desc);
406 free(v);
407 }
408
409
410 /**
411 * \brief Copy version information.
412 *
413 * \param d destination pointer.
414 * \param s source pointer.
415 *
416 * \internal
417 * Used by drmGetVersion() to translate the information returned by the ioctl
418 * interface in a private structure into the public structure counterpart.
419 */
420 static void drmCopyVersion(drmVersionPtr d, const drm_version_t *s)
421 {
422 d->version_major = s->version_major;
423 d->version_minor = s->version_minor;
424 d->version_patchlevel = s->version_patchlevel;
425 d->name_len = s->name_len;
426 d->name = strdup(s->name);
427 d->date_len = s->date_len;
428 d->date = strdup(s->date);
429 d->desc_len = s->desc_len;
430 d->desc = strdup(s->desc);
431 }
432
433
434 /**
435 * \brief Query the driver version information.
436 *
437 * \param fd file descriptor.
438 *
439 * \return pointer to a drmVersion structure which should be freed with
440 * drmFreeVersion().
441 *
442 * \note Similar information is available via /proc/dri.
443 *
444 * \internal
445 * It gets the version information via successive DRM_IOCTL_VERSION ioctls,
446 * first with zeros to get the string lengths, and then the actually strings.
447 * It also null-terminates them since they might not be already.
448 */
449 drmVersionPtr drmGetVersion(int fd)
450 {
451 drmVersionPtr retval;
452 drm_version_t *version = malloc(sizeof(*version));
453
454 /* First, get the lengths */
455 version->name_len = 0;
456 version->name = NULL;
457 version->date_len = 0;
458 version->date = NULL;
459 version->desc_len = 0;
460 version->desc = NULL;
461
462 if (ioctl(fd, DRM_IOCTL_VERSION, version)) {
463 drmFreeKernelVersion(version);
464 return NULL;
465 }
466
467 /* Now, allocate space and get the data */
468 if (version->name_len)
469 version->name = malloc(version->name_len + 1);
470 if (version->date_len)
471 version->date = malloc(version->date_len + 1);
472 if (version->desc_len)
473 version->desc = malloc(version->desc_len + 1);
474
475 if (ioctl(fd, DRM_IOCTL_VERSION, version)) {
476 drmFreeKernelVersion(version);
477 return NULL;
478 }
479
480 /* The results might not be null-terminated
481 strings, so terminate them. */
482
483 if (version->name_len) version->name[version->name_len] = '\0';
484 if (version->date_len) version->date[version->date_len] = '\0';
485 if (version->desc_len) version->desc[version->desc_len] = '\0';
486
487 /* Now, copy it all back into the
488 client-visible data structure... */
489 retval = malloc(sizeof(*retval));
490 drmCopyVersion(retval, version);
491 drmFreeKernelVersion(version);
492 return retval;
493 }
494
495
496 /**
497 * \brief Get version information for the DRM user space library.
498 *
499 * This version number is driver independent.
500 *
501 * \param fd file descriptor.
502 *
503 * \return version information.
504 *
505 * \internal
506 * This function allocates and fills a drm_version structure with a hard coded
507 * version number.
508 */
509 drmVersionPtr drmGetLibVersion(int fd)
510 {
511 drm_version_t *version = malloc(sizeof(*version));
512
513 /* Version history:
514 * revision 1.0.x = original DRM interface with no drmGetLibVersion
515 * entry point and many drm<Device> extensions
516 * revision 1.1.x = added drmCommand entry points for device extensions
517 * added drmGetLibVersion to identify libdrm.a version
518 */
519 version->version_major = 1;
520 version->version_minor = 1;
521 version->version_patchlevel = 0;
522
523 return (drmVersionPtr)version;
524 }
525
526
527 /**
528 * \brief Free the bus ID information.
529 *
530 * \param busid bus ID information string as given by drmGetBusid().
531 *
532 * \internal
533 * This function is just frees the memory pointed by \p busid.
534 */
535 void drmFreeBusid(const char *busid)
536 {
537 free((void *)busid);
538 }
539
540
541 /**
542 * \brief Get the bus ID of the device.
543 *
544 * \param fd file descriptor.
545 *
546 * \return bus ID string.
547 *
548 * \internal
549 * This function gets the bus ID via successive DRM_IOCTL_GET_UNIQUE ioctls to
550 * get the string length and data, passing the arguments in a drm_unique
551 * structure.
552 */
553 char *drmGetBusid(int fd)
554 {
555 drm_unique_t u;
556
557 u.unique_len = 0;
558 u.unique = NULL;
559
560 if (ioctl(fd, DRM_IOCTL_GET_UNIQUE, &u)) return NULL;
561 u.unique = malloc(u.unique_len + 1);
562 if (ioctl(fd, DRM_IOCTL_GET_UNIQUE, &u)) return NULL;
563 u.unique[u.unique_len] = '\0';
564 return u.unique;
565 }
566
567
568 /**
569 * \brief Set the bus ID of the device.
570 *
571 * \param fd file descriptor.
572 * \param busid bus ID string.
573 *
574 * \return zero on success, negative on failure.
575 *
576 * \internal
577 * This function is a wrapper around the DRM_IOCTL_SET_UNIQUE ioctl, passing
578 * the arguments in a drm_unique structure.
579 */
580 int drmSetBusid(int fd, const char *busid)
581 {
582 drm_unique_t u;
583
584 u.unique = (char *)busid;
585 u.unique_len = strlen(busid);
586
587 if (ioctl(fd, DRM_IOCTL_SET_UNIQUE, &u)) {
588 return -errno;
589 }
590 return 0;
591 }
592
593
594 /**
595 * \brief Specifies a range of memory that is available for mapping by a
596 * non-root process.
597 *
598 * \param fd file descriptor.
599 * \param offset usually the physical address. The actual meaning depends of
600 * the \p type parameter. See below.
601 * \param size of the memory in bytes.
602 * \param type type of the memory to be mapped.
603 * \param flags combination of several flags to modify the function actions.
604 * \param handle will be set to a value that may be used as the offset
605 * parameter for mmap().
606 *
607 * \return zero on success or a negative value on error.
608 *
609 * \par Mapping the frame buffer
610 * For the frame buffer
611 * - \p offset will be the physical address of the start of the frame buffer,
612 * - \p size will be the size of the frame buffer in bytes, and
613 * - \p type will be DRM_FRAME_BUFFER.
614 *
615 * \par
616 * The area mapped will be uncached. If MTRR support is available in the
617 * kernel, the frame buffer area will be set to write combining.
618 *
619 * \par Mapping the MMIO register area
620 * For the MMIO register area,
621 * - \p offset will be the physical address of the start of the register area,
622 * - \p size will be the size of the register area bytes, and
623 * - \p type will be DRM_REGISTERS.
624 * \par
625 * The area mapped will be uncached.
626 *
627 * \par Mapping the SAREA
628 * For the SAREA,
629 * - \p offset will be ignored and should be set to zero,
630 * - \p size will be the desired size of the SAREA in bytes,
631 * - \p type will be DRM_SHM.
632 *
633 * \par
634 * A shared memory area of the requested size will be created and locked in
635 * kernel memory. This area may be mapped into client-space by using the handle
636 * returned.
637 *
638 * \note May only be called by root.
639 *
640 * \internal
641 * This function is a wrapper around the DRM_IOCTL_ADD_MAP ioctl, passing
642 * the arguments in a drm_map structure.
643 */
644 int drmAddMap(int fd,
645 drmHandle offset,
646 drmSize size,
647 drmMapType type,
648 drmMapFlags flags,
649 drmHandlePtr handle)
650 {
651 drm_map_t map;
652
653 map.offset = offset;
654 map.size = size;
655 map.handle = 0;
656 map.type = type;
657 map.flags = flags;
658 if (ioctl(fd, DRM_IOCTL_ADD_MAP, &map)) return -errno;
659 if (handle) *handle = (drmHandle)map.handle;
660 return 0;
661 }
662
663
664 /**
665 * \brief Make buffers available for DMA transfers.
666 *
667 * \param fd file descriptor.
668 * \param count number of buffers.
669 * \param size size of each buffer.
670 * \param flags buffer allocation flags.
671 * \param agp_offset offset in the AGP aperture
672 *
673 * \return number of buffers allocated, negative on error.
674 *
675 * \internal
676 * This function is a wrapper around DRM_IOCTL_ADD_BUFS ioctl.
677 *
678 * \sa drm_buf_desc.
679 */
680 int drmAddBufs(int fd, int count, int size, drmBufDescFlags flags,
681 int agp_offset)
682 {
683 drm_buf_desc_t request;
684
685 request.count = count;
686 request.size = size;
687 request.low_mark = 0;
688 request.high_mark = 0;
689 request.flags = flags;
690 request.agp_start = agp_offset;
691
692 if (ioctl(fd, DRM_IOCTL_ADD_BUFS, &request)) return -errno;
693 return request.count;
694 }
695
696
697 /**
698 * \brief Free buffers.
699 *
700 * \param fd file descriptor.
701 * \param count number of buffers to free.
702 * \param list list of buffers to be freed.
703 *
704 * \return zero on success, or a negative value on failure.
705 *
706 * \note This function is primarily used for debugging.
707 *
708 * \internal
709 * This function is a wrapper around the DRM_IOCTL_FREE_BUFS ioctl, passing
710 * the arguments in a drm_buf_free structure.
711 */
712 int drmFreeBufs(int fd, int count, int *list)
713 {
714 drm_buf_free_t request;
715
716 request.count = count;
717 request.list = list;
718 if (ioctl(fd, DRM_IOCTL_FREE_BUFS, &request)) return -errno;
719 return 0;
720 }
721
722
723 /**
724 * \brief Close the device.
725 *
726 * \param fd file descriptor.
727 *
728 * \internal
729 * This function closes the file descriptor.
730 */
731 int drmClose(int fd)
732 {
733 drmMsg("close %d\n", fd);
734 return close(fd);
735 }
736
737
738 /**
739 * \brief Map a region of memory.
740 *
741 * \param fd file descriptor.
742 * \param handle handle returned by drmAddMap().
743 * \param size size in bytes. Must match the size used by drmAddMap().
744 * \param address will contain the user-space virtual address where the mapping
745 * begins.
746 *
747 * \return zero on success, or a negative value on failure.
748 *
749 * \internal
750 * This function is a wrapper for mmap().
751 */
752 int drmMap(int fd,
753 drmHandle handle,
754 drmSize size,
755 drmAddressPtr address)
756 {
757 static unsigned long pagesize_mask = 0;
758
759 if (fd < 0) return -EINVAL;
760
761 if (!pagesize_mask)
762 pagesize_mask = getpagesize() - 1;
763
764 size = (size + pagesize_mask) & ~pagesize_mask;
765
766 *address = mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, handle);
767 if (*address == MAP_FAILED) return -errno;
768 return 0;
769 }
770
771
772 /**
773 * \brief Unmap mappings obtained with drmMap().
774 *
775 * \param address address as given by drmMap().
776 * \param size size in bytes. Must match the size used by drmMap().
777 *
778 * \return zero on success, or a negative value on failure.
779 *
780 * \internal
781 * This function is a wrapper for unmap().
782 */
783 int drmUnmap(drmAddress address, drmSize size)
784 {
785 return munmap(address, size);
786 }
787
788
789 /**
790 * \brief Map all DMA buffers into client-virtual space.
791 *
792 * \param fd file descriptor.
793 *
794 * \return a pointer to a ::drmBufMap structure.
795 *
796 * \note The client may not use these buffers until obtaining buffer indices
797 * with drmDMA().
798 *
799 * \internal
800 * This function calls the DRM_IOCTL_MAP_BUFS ioctl and copies the returned
801 * information about the buffers in a drm_buf_map structure into the
802 * client-visible data structures.
803 */
804 drmBufMapPtr drmMapBufs(int fd)
805 {
806 drm_buf_map_t bufs;
807 drmBufMapPtr retval;
808 int i;
809
810 bufs.count = 0;
811 bufs.list = NULL;
812 if (ioctl(fd, DRM_IOCTL_MAP_BUFS, &bufs)) return NULL;
813
814 if (bufs.count) {
815 if (!(bufs.list = malloc(bufs.count * sizeof(*bufs.list))))
816 return NULL;
817
818 if (ioctl(fd, DRM_IOCTL_MAP_BUFS, &bufs)) {
819 free(bufs.list);
820 return NULL;
821 }
822 /* Now, copy it all back into the
823 client-visible data structures... */
824 retval = malloc(sizeof(*retval));
825 retval->count = bufs.count;
826 retval->list = malloc(bufs.count * sizeof(*retval->list));
827 for (i = 0; i < bufs.count; i++) {
828 retval->list[i].idx = bufs.list[i].idx;
829 retval->list[i].total = bufs.list[i].total;
830 retval->list[i].used = 0;
831 retval->list[i].address = bufs.list[i].address;
832 }
833 return retval;
834 }
835 return NULL;
836 }
837
838
839 /**
840 * \brief Unmap buffers allocated with drmMapBufs().
841 *
842 * \return zero on success, or negative value on failure.
843 *
844 * \internal
845 * Calls munmap() for every buffer stored in \p bufs.
846 */
847 int drmUnmapBufs(drmBufMapPtr bufs)
848 {
849 int i;
850
851 for (i = 0; i < bufs->count; i++) {
852 munmap(bufs->list[i].address, bufs->list[i].total);
853 }
854 return 0;
855 }
856
857
858 #define DRM_DMA_RETRY 16
859
860 /**
861 * \brief Reserve DMA buffers.
862 *
863 * \param fd file descriptor.
864 * \param request
865 *
866 * \return zero on success, or a negative value on failure.
867 *
868 * \internal
869 * Assemble the arguments into a drm_dma structure and keeps issuing the
870 * DRM_IOCTL_DMA ioctl until success or until maximum number of retries.
871 */
872 int drmDMA(int fd, drmDMAReqPtr request)
873 {
874 drm_dma_t dma;
875 int ret, i = 0;
876
877 /* Copy to hidden structure */
878 dma.context = request->context;
879 dma.send_count = request->send_count;
880 dma.send_indices = request->send_list;
881 dma.send_sizes = request->send_sizes;
882 dma.flags = request->flags;
883 dma.request_count = request->request_count;
884 dma.request_size = request->request_size;
885 dma.request_indices = request->request_list;
886 dma.request_sizes = request->request_sizes;
887
888 do {
889 ret = ioctl( fd, DRM_IOCTL_DMA, &dma );
890 } while ( ret && errno == EAGAIN && i++ < DRM_DMA_RETRY );
891
892 if ( ret == 0 ) {
893 request->granted_count = dma.granted_count;
894 return 0;
895 } else {
896 return -errno;
897 }
898 }
899
900
901 /**
902 * \brief Obtain heavyweight hardware lock.
903 *
904 * \param fd file descriptor.
905 * \param context context.
906 * \param flags flags that determine the sate of the hardware when the function
907 * returns.
908 *
909 * \return always zero.
910 *
911 * \internal
912 * This function translates the arguments into a drm_lock structure and issue
913 * the DRM_IOCTL_LOCK ioctl until the lock is successfully acquired.
914 */
915 int drmGetLock(int fd, drmContext context, drmLockFlags flags)
916 {
917 drm_lock_t lock;
918
919 lock.context = context;
920 lock.flags = 0;
921 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
922 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
923 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
924 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
925 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
926 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
927
928 while (ioctl(fd, DRM_IOCTL_LOCK, &lock))
929 ;
930 return 0;
931 }
932
933 static void (*drm_unlock_callback)( void ) = 0;
934
935 /**
936 * \brief Release the hardware lock.
937 *
938 * \param fd file descriptor.
939 * \param context context.
940 *
941 * \return zero on success, or a negative value on failure.
942 *
943 * \internal
944 * This function is a wrapper around the DRM_IOCTL_UNLOCK ioctl, passing the
945 * argument in a drm_lock structure.
946 */
947 int drmUnlock(int fd, drmContext context)
948 {
949 drm_lock_t lock;
950 int ret;
951
952 lock.context = context;
953 lock.flags = 0;
954 ret = ioctl(fd, DRM_IOCTL_UNLOCK, &lock);
955
956 /* Need this to synchronize vt releasing. Could also teach fbdev
957 * about the drm lock...
958 */
959 if (drm_unlock_callback) {
960 drm_unlock_callback();
961 }
962
963 return ret;
964 }
965
966
967 /**
968 * \brief Create context.
969 *
970 * Used by the X server during GLXContext initialization. This causes
971 * per-context kernel-level resources to be allocated.
972 *
973 * \param fd file descriptor.
974 * \param handle is set on success. To be used by the client when requesting DMA
975 * dispatch with drmDMA().
976 *
977 * \return zero on success, or a negative value on failure.
978 *
979 * \note May only be called by root.
980 *
981 * \internal
982 * This function is a wrapper around the DRM_IOCTL_ADD_CTX ioctl, passing the
983 * argument in a drm_ctx structure.
984 */
985 int drmCreateContext(int fd, drmContextPtr handle)
986 {
987 drm_ctx_t ctx;
988
989 ctx.flags = 0; /* Modified with functions below */
990 if (ioctl(fd, DRM_IOCTL_ADD_CTX, &ctx)) return -errno;
991 *handle = ctx.handle;
992 return 0;
993 }
994
995
996 /**
997 * \brief Destroy context.
998 *
999 * Free any kernel-level resources allocated with drmCreateContext() associated
1000 * with the context.
1001 *
1002 * \param fd file descriptor.
1003 * \param handle handle given by drmCreateContext().
1004 *
1005 * \return zero on success, or a negative value on failure.
1006 *
1007 * \note May only be called by root.
1008 *
1009 * \internal
1010 * This function is a wrapper around the DRM_IOCTL_RM_CTX ioctl, passing the
1011 * argument in a drm_ctx structure.
1012 */
1013 int drmDestroyContext(int fd, drmContext handle)
1014 {
1015 drm_ctx_t ctx;
1016 ctx.handle = handle;
1017 if (ioctl(fd, DRM_IOCTL_RM_CTX, &ctx)) return -errno;
1018 return 0;
1019 }
1020
1021
1022 /**
1023 * \brief Acquire the AGP device.
1024 *
1025 * Must be called before any of the other AGP related calls.
1026 *
1027 * \param fd file descriptor.
1028 *
1029 * \return zero on success, or a negative value on failure.
1030 *
1031 * \internal
1032 * This function is a wrapper around the DRM_IOCTL_AGP_ACQUIRE ioctl.
1033 */
1034 int drmAgpAcquire(int fd)
1035 {
1036 if (ioctl(fd, DRM_IOCTL_AGP_ACQUIRE, NULL)) return -errno;
1037 return 0;
1038 }
1039
1040
1041 /**
1042 * \brief Release the AGP device.
1043 *
1044 * \param fd file descriptor.
1045 *
1046 * \return zero on success, or a negative value on failure.
1047 *
1048 * \internal
1049 * This function is a wrapper around the DRM_IOCTL_AGP_RELEASE ioctl.
1050 */
1051 int drmAgpRelease(int fd)
1052 {
1053 if (ioctl(fd, DRM_IOCTL_AGP_RELEASE, NULL)) return -errno;
1054 return 0;
1055 }
1056
1057
1058 /**
1059 * \brief Set the AGP mode.
1060 *
1061 * \param fd file descriptor.
1062 * \param mode AGP mode.
1063 *
1064 * \return zero on success, or a negative value on failure.
1065 *
1066 * \internal
1067 * This function is a wrapper around the DRM_IOCTL_AGP_ENABLE ioctl, passing the
1068 * argument in a drm_agp_mode structure.
1069 */
1070 int drmAgpEnable(int fd, unsigned long mode)
1071 {
1072 drm_agp_mode_t m;
1073
1074 m.mode = mode;
1075 if (ioctl(fd, DRM_IOCTL_AGP_ENABLE, &m)) return -errno;
1076 return 0;
1077 }
1078
1079
1080 /**
1081 * \brief Allocate a chunk of AGP memory.
1082 *
1083 * \param fd file descriptor.
1084 * \param size requested memory size in bytes. Will be rounded to page boundary.
1085 * \param type type of memory to allocate.
1086 * \param address if not zero, will be set to the physical address of the
1087 * allocated memory.
1088 * \param handle on success will be set to a handle of the allocated memory.
1089 *
1090 * \return zero on success, or a negative value on failure.
1091 *
1092 * \internal
1093 * This function is a wrapper around the DRM_IOCTL_AGP_ALLOC ioctl, passing the
1094 * arguments in a drm_agp_buffer structure.
1095 */
1096 int drmAgpAlloc(int fd, unsigned long size, unsigned long type,
1097 unsigned long *address, unsigned long *handle)
1098 {
1099 drm_agp_buffer_t b;
1100 *handle = 0;
1101 b.size = size;
1102 b.handle = 0;
1103 b.type = type;
1104 if (ioctl(fd, DRM_IOCTL_AGP_ALLOC, &b)) return -errno;
1105 if (address != 0UL) *address = b.physical;
1106 *handle = b.handle;
1107 return 0;
1108 }
1109
1110
1111 /**
1112 * \brief Free a chunk of AGP memory.
1113 *
1114 * \param fd file descriptor.
1115 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1116 *
1117 * \return zero on success, or a negative value on failure.
1118 *
1119 * \internal
1120 * This function is a wrapper around the DRM_IOCTL_AGP_FREE ioctl, passing the
1121 * argument in a drm_agp_buffer structure.
1122 */
1123 int drmAgpFree(int fd, unsigned long handle)
1124 {
1125 drm_agp_buffer_t b;
1126
1127 b.size = 0;
1128 b.handle = handle;
1129 if (ioctl(fd, DRM_IOCTL_AGP_FREE, &b)) return -errno;
1130 return 0;
1131 }
1132
1133
1134 /**
1135 * \brief Bind a chunk of AGP memory.
1136 *
1137 * \param fd file descriptor.
1138 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1139 * \param offset offset in bytes. It will round to page boundary.
1140 *
1141 * \return zero on success, or a negative value on failure.
1142 *
1143 * \internal
1144 * This function is a wrapper around the DRM_IOCTL_AGP_BIND ioctl, passing the
1145 * argument in a drm_agp_binding structure.
1146 */
1147 int drmAgpBind(int fd, unsigned long handle, unsigned long offset)
1148 {
1149 drm_agp_binding_t b;
1150
1151 b.handle = handle;
1152 b.offset = offset;
1153 if (ioctl(fd, DRM_IOCTL_AGP_BIND, &b)) return -errno;
1154 return 0;
1155 }
1156
1157
1158 /**
1159 * \brief Unbind a chunk of AGP memory.
1160 *
1161 * \param fd file descriptor.
1162 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1163 *
1164 * \return zero on success, or a negative value on failure.
1165 *
1166 * \internal
1167 * This function is a wrapper around the DRM_IOCTL_AGP_UNBIND ioctl, passing
1168 * the argument in a drm_agp_binding structure.
1169 */
1170 int drmAgpUnbind(int fd, unsigned long handle)
1171 {
1172 drm_agp_binding_t b;
1173
1174 b.handle = handle;
1175 b.offset = 0;
1176 if (ioctl(fd, DRM_IOCTL_AGP_UNBIND, &b)) return -errno;
1177 return 0;
1178 }
1179
1180
1181 /**
1182 * \brief Get AGP driver major version number.
1183 *
1184 * \param fd file descriptor.
1185 *
1186 * \return major version number on success, or a negative value on failure..
1187 *
1188 * \internal
1189 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1190 * necessary information in a drm_agp_info structure.
1191 */
1192 int drmAgpVersionMajor(int fd)
1193 {
1194 drm_agp_info_t i;
1195
1196 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i)) return -errno;
1197 return i.agp_version_major;
1198 }
1199
1200
1201 /**
1202 * \brief Get AGP driver minor version number.
1203 *
1204 * \param fd file descriptor.
1205 *
1206 * \return minor version number on success, or a negative value on failure.
1207 *
1208 * \internal
1209 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1210 * necessary information in a drm_agp_info structure.
1211 */
1212 int drmAgpVersionMinor(int fd)
1213 {
1214 drm_agp_info_t i;
1215
1216 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i)) return -errno;
1217 return i.agp_version_minor;
1218 }
1219
1220
1221 /**
1222 * \brief Get AGP mode.
1223 *
1224 * \param fd file descriptor.
1225 *
1226 * \return mode on success, or zero on failure.
1227 *
1228 * \internal
1229 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1230 * necessary information in a drm_agp_info structure.
1231 */
1232 unsigned long drmAgpGetMode(int fd)
1233 {
1234 drm_agp_info_t i;
1235
1236 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i)) return 0;
1237 return i.mode;
1238 }
1239
1240
1241 /**
1242 * \brief Get AGP aperture base.
1243 *
1244 * \param fd file descriptor.
1245 *
1246 * \return aperture base on success, zero on failure.
1247 *
1248 * \internal
1249 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1250 * necessary information in a drm_agp_info structure.
1251 */
1252 unsigned long drmAgpBase(int fd)
1253 {
1254 drm_agp_info_t i;
1255
1256 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i)) return 0;
1257 return i.aperture_base;
1258 }
1259
1260
1261 /**
1262 * \brief Get AGP aperture size.
1263 *
1264 * \param fd file descriptor.
1265 *
1266 * \return aperture size on success, zero on failure.
1267 *
1268 * \internal
1269 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1270 * necessary information in a drm_agp_info structure.
1271 */
1272 unsigned long drmAgpSize(int fd)
1273 {
1274 drm_agp_info_t i;
1275
1276 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i)) return 0;
1277 return i.aperture_size;
1278 }
1279
1280
1281 /**
1282 * \brief Get used AGP memory.
1283 *
1284 * \param fd file descriptor.
1285 *
1286 * \return memory used on success, or zero on failure.
1287 *
1288 * \internal
1289 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1290 * necessary information in a drm_agp_info structure.
1291 */
1292 unsigned long drmAgpMemoryUsed(int fd)
1293 {
1294 drm_agp_info_t i;
1295
1296 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i)) return 0;
1297 return i.memory_used;
1298 }
1299
1300
1301 /**
1302 * \brief Get available AGP memory.
1303 *
1304 * \param fd file descriptor.
1305 *
1306 * \return memory available on success, or zero on failure.
1307 *
1308 * \internal
1309 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1310 * necessary information in a drm_agp_info structure.
1311 */
1312 unsigned long drmAgpMemoryAvail(int fd)
1313 {
1314 drm_agp_info_t i;
1315
1316 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i)) return 0;
1317 return i.memory_allowed;
1318 }
1319
1320
1321 /**
1322 * \brief Get hardware vendor ID.
1323 *
1324 * \param fd file descriptor.
1325 *
1326 * \return vendor ID on success, or zero on failure.
1327 *
1328 * \internal
1329 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1330 * necessary information in a drm_agp_info structure.
1331 */
1332 unsigned int drmAgpVendorId(int fd)
1333 {
1334 drm_agp_info_t i;
1335
1336 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i)) return 0;
1337 return i.id_vendor;
1338 }
1339
1340
1341 /**
1342 * \brief Get hardware device ID.
1343 *
1344 * \param fd file descriptor.
1345 *
1346 * \return zero on success, or zero on failure.
1347 *
1348 * \internal
1349 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1350 * necessary information in a drm_agp_info structure.
1351 */
1352 unsigned int drmAgpDeviceId(int fd)
1353 {
1354 drm_agp_info_t i;
1355
1356 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i)) return 0;
1357 return i.id_device;
1358 }
1359
1360 int drmScatterGatherAlloc(int fd, unsigned long size, unsigned long *handle)
1361 {
1362 drm_scatter_gather_t sg;
1363
1364 *handle = 0;
1365 sg.size = size;
1366 sg.handle = 0;
1367 if (ioctl(fd, DRM_IOCTL_SG_ALLOC, &sg)) return -errno;
1368 *handle = sg.handle;
1369 return 0;
1370 }
1371
1372 int drmScatterGatherFree(int fd, unsigned long handle)
1373 {
1374 drm_scatter_gather_t sg;
1375
1376 sg.size = 0;
1377 sg.handle = handle;
1378 if (ioctl(fd, DRM_IOCTL_SG_FREE, &sg)) return -errno;
1379 return 0;
1380 }
1381
1382 /**
1383 * \brief Wait for VBLANK.
1384 *
1385 * \param fd file descriptor.
1386 * \param vbl pointer to a drmVBlank structure.
1387 *
1388 * \return zero on success, or a negative value on failure.
1389 *
1390 * \internal
1391 * This function is a wrapper around the DRM_IOCTL_WAIT_VBLANK ioctl.
1392 */
1393 int drmWaitVBlank(int fd, drmVBlankPtr vbl)
1394 {
1395 int ret;
1396
1397 do {
1398 ret = ioctl(fd, DRM_IOCTL_WAIT_VBLANK, vbl);
1399 } while (ret && errno == EINTR);
1400
1401 return ret;
1402 }
1403
1404
1405 /**
1406 * \brief Install IRQ handler.
1407 *
1408 * \param fd file descriptor.
1409 * \param irq IRQ number.
1410 *
1411 * \return zero on success, or a negative value on failure.
1412 *
1413 * \internal
1414 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
1415 * argument in a drm_control structure.
1416 */
1417 int drmCtlInstHandler(int fd, int irq)
1418 {
1419 drm_control_t ctl;
1420
1421 ctl.func = DRM_INST_HANDLER;
1422 ctl.irq = irq;
1423 if (ioctl(fd, DRM_IOCTL_CONTROL, &ctl)) return -errno;
1424 return 0;
1425 }
1426
1427
1428 /**
1429 * \brief Uninstall IRQ handler.
1430 *
1431 * \param fd file descriptor.
1432 *
1433 * \return zero on success, or a negative value on failure.
1434 *
1435 * \internal
1436 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
1437 * argument in a drm_control structure.
1438 */
1439 int drmCtlUninstHandler(int fd)
1440 {
1441 drm_control_t ctl;
1442
1443 ctl.func = DRM_UNINST_HANDLER;
1444 ctl.irq = 0;
1445 if (ioctl(fd, DRM_IOCTL_CONTROL, &ctl)) return -errno;
1446 return 0;
1447 }
1448
1449
1450 /**
1451 * \brief Get IRQ from bus ID.
1452 *
1453 * \param fd file descriptor.
1454 * \param busnum bus number.
1455 * \param devnum device number.
1456 * \param funcnum function number.
1457 *
1458 * \return IRQ number on success, or a negative value on failure.
1459 *
1460 * \internal
1461 * This function is a wrapper around the DRM_IOCTL_IRQ_BUSID ioctl, passing the
1462 * arguments in a drm_irq_busid structure.
1463 */
1464 int drmGetInterruptFromBusID(int fd, int busnum, int devnum, int funcnum)
1465 {
1466 drm_irq_busid_t p;
1467
1468 p.busnum = busnum;
1469 p.devnum = devnum;
1470 p.funcnum = funcnum;
1471 if (ioctl(fd, DRM_IOCTL_IRQ_BUSID, &p)) return -errno;
1472 return p.irq;
1473 }
1474
1475
1476 /**
1477 * \brief Send a device-specific command.
1478 *
1479 * \param fd file descriptor.
1480 * \param drmCommandIndex command index
1481 *
1482 * \return zero on success, or a negative value on failure.
1483 *
1484 * \internal
1485 * It issues a ioctl given by
1486 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
1487 */
1488 int drmCommandNone(int fd, unsigned long drmCommandIndex)
1489 {
1490 void *data = NULL; /* dummy */
1491 unsigned long request;
1492
1493 request = DRM_IO( DRM_COMMAND_BASE + drmCommandIndex);
1494
1495 if (ioctl(fd, request, data)) {
1496 return -errno;
1497 }
1498 return 0;
1499 }
1500
1501
1502 /**
1503 * \brief Send a device-specific read command.
1504 *
1505 * \param fd file descriptor.
1506 * \param drmCommandIndex command index
1507 * \param data destination pointer of the data to be read.
1508 * \param size size of the data to be read.
1509 *
1510 * \return zero on success, or a negative value on failure.
1511 *
1512 * \internal
1513 * It issues a read ioctl given by
1514 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
1515 */
1516 int drmCommandRead(int fd, unsigned long drmCommandIndex,
1517 void *data, unsigned long size )
1518 {
1519 unsigned long request;
1520
1521 request = DRM_IOC( DRM_IOC_READ, DRM_IOCTL_BASE,
1522 DRM_COMMAND_BASE + drmCommandIndex, size);
1523
1524 if (ioctl(fd, request, data)) {
1525 return -errno;
1526 }
1527 return 0;
1528 }
1529
1530
1531 /**
1532 * \brief Send a device-specific write command.
1533 *
1534 * \param fd file descriptor.
1535 * \param drmCommandIndex command index
1536 * \param data source pointer of the data to be written.
1537 * \param size size of the data to be written.
1538 *
1539 * \return zero on success, or a negative value on failure.
1540 *
1541 * \internal
1542 * It issues a write ioctl given by
1543 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
1544 */
1545 int drmCommandWrite(int fd, unsigned long drmCommandIndex,
1546 void *data, unsigned long size )
1547 {
1548 unsigned long request;
1549
1550 request = DRM_IOC( DRM_IOC_WRITE, DRM_IOCTL_BASE,
1551 DRM_COMMAND_BASE + drmCommandIndex, size);
1552
1553 if (ioctl(fd, request, data)) {
1554 return -errno;
1555 }
1556 return 0;
1557 }
1558
1559
1560 /**
1561 * \brief Send a device-specific read-write command.
1562 *
1563 * \param fd file descriptor.
1564 * \param drmCommandIndex command index
1565 * \param data source pointer of the data to be read and written.
1566 * \param size size of the data to be read and written.
1567 *
1568 * \return zero on success, or a negative value on failure.
1569 *
1570 * \internal
1571 * It issues a read-write ioctl given by
1572 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
1573 */
1574 int drmCommandWriteRead(int fd, unsigned long drmCommandIndex,
1575 void *data, unsigned long size )
1576 {
1577 unsigned long request;
1578
1579 request = DRM_IOC( DRM_IOC_READ|DRM_IOC_WRITE, DRM_IOCTL_BASE,
1580 DRM_COMMAND_BASE + drmCommandIndex, size);
1581
1582 if (ioctl(fd, request, data)) {
1583 return -errno;
1584 }
1585 return 0;
1586 }
1587