]> mj.ucw.cz Git - pciutils.git/blob - pread.h
99a91b2d6c55040c237e232a3df3cab5af991cb8
[pciutils.git] / pread.h
1 /*
2  *      The PCI Library -- Portable interface to pread() and pwrite()
3  *
4  *      Copyright (c) 1997--2003 Martin Mares <mj@ucw.cz>
5  *
6  *      Can be freely distributed and used under the terms of the GNU GPL v2+
7  *
8  *      SPDX-License-Identifier: GPL-2.0-or-later
9  */
10
11 /*
12  *  We'd like to use pread/pwrite for configuration space accesses, but
13  *  unfortunately it isn't simple at all since all libc's until glibc 2.1
14  *  don't define it.
15  */
16
17 #if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ > 0
18 /* glibc 2.1 or newer -> pread/pwrite supported automatically */
19
20 #elif defined(i386) && defined(__GLIBC__)
21 /* glibc 2.0 on i386 -> call syscalls directly */
22 #include <asm/unistd.h>
23 #include <syscall-list.h>
24 #ifndef SYS_pread
25 #define SYS_pread 180
26 #endif
27 static int pread(unsigned int fd, void *buf, size_t size, loff_t where)
28 { return syscall(SYS_pread, fd, buf, size, where); }
29 #ifndef SYS_pwrite
30 #define SYS_pwrite 181
31 #endif
32 static int pwrite(unsigned int fd, void *buf, size_t size, loff_t where)
33 { return syscall(SYS_pwrite, fd, buf, size, where); }
34
35 #else
36 /* In all other cases we use lseek/read/write instead to be safe */
37 #define make_rw_glue(op) \
38         static int do_##op(struct pci_dev *d, int fd, void *buf, size_t size, int where)        \
39         {                                                                                       \
40           struct pci_access *a = d->access;                                                     \
41           int r;                                                                                \
42           if (a->fd_pos != where && lseek(fd, where, SEEK_SET) < 0)                             \
43             return -1;                                                                          \
44           r = op(fd, buf, size);                                                                \
45           if (r < 0)                                                                            \
46             a->fd_pos = -1;                                                                     \
47           else                                                                                  \
48             a->fd_pos = where + r;                                                              \
49           return r;                                                                             \
50         }
51 make_rw_glue(read)
52 make_rw_glue(write)
53 #define PCI_HAVE_DO_READ
54 #endif
55
56 #ifndef PCI_HAVE_DO_READ
57 #define do_read(d,f,b,l,p) pread(f,b,l,p)
58 #define do_write(d,f,b,l,p) pwrite(f,b,l,p)
59 #endif