2 * Copyright (c) 2007 Paulo R. Zanoni, Tiago Vignatti
5 * Permission is hereby granted, free of charge, to any person
6 * obtaining a copy of this software and associated documentation
7 * files (the "Software"), to deal in the Software without
8 * restriction, including without limitation the rights to use,
9 * copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 * OTHER DEALINGS IN THE SOFTWARE.
34 #include "pciaccess.h"
35 #include "pciaccess_private.h"
40 pci_device_vgaarb_init(void)
42 if ((pci_sys->vgaarb_fd = open ("/dev/vga_arbiter", O_RDWR)) < 0) {
43 fprintf(stderr, "device open failed");
51 pci_device_vgaarb_fini(void)
53 if (close(pci_sys->vgaarb_fd) != 0)
54 fprintf(stderr, "device close failed");
58 * Writes message on vga device. The messages are defined by the kernel
61 * \param fd vga arbiter device.
62 * \param buf message itself.
63 * \param len message length.
66 * Zero on success, 1 if something gets wrong and 2 if fd is busy (only for
70 vgaarb_write(int fd, char *buf, int len)
77 ret = write(fd, buf, len);
79 /* the user may have called "trylock" and didn't get the lock */
83 fprintf(stderr, "write error");
86 else if (ret != len) {
87 /* it's need to receive the exactly amount required. */
88 fprintf(stderr, "write error: wrote different than expected\n");
93 fprintf(stderr, "%s: successfully wrote: '%s'\n", __FUNCTION__, buf);
100 parse_string_to_decodes_rsrc(char *input)
104 tok = strtok(input, ",");
108 tok = strtok(NULL, ",");
111 tok = strtok(tok, "=");
114 tok = strtok(NULL, "=");
118 if (!strncmp(tok, "io+mem", 6))
119 return VGA_ARB_RSRC_LEGACY_IO | VGA_ARB_RSRC_LEGACY_MEM;
120 if (!strncmp(tok, "io", 2))
121 return VGA_ARB_RSRC_LEGACY_IO;
122 if (!strncmp(tok, "mem", 3))
123 return VGA_ARB_RSRC_LEGACY_MEM;
125 return VGA_ARB_RSRC_NONE;
129 rsrc_to_str(int iostate)
132 case VGA_ARB_RSRC_LEGACY_IO | VGA_ARB_RSRC_LEGACY_MEM:
134 case VGA_ARB_RSRC_LEGACY_IO:
136 case VGA_ARB_RSRC_LEGACY_MEM:
144 pci_device_vgaarb_set_target(struct pci_device *dev)
150 len = snprintf(buf, BUFSIZE, "target PCI:%d:%d:%d.%d",
151 dev->domain, dev->bus, dev->dev, dev->func);
153 ret = vgaarb_write(pci_sys->vgaarb_fd, buf, len);
157 ret = read(pci_sys->vgaarb_fd, buf, BUFSIZE);
161 dev->vgaarb_rsrc = parse_string_to_decodes_rsrc(buf);
166 pci_device_vgaarb_decodes(struct pci_device *dev)
171 len = snprintf(buf, BUFSIZE, "decodes %s", rsrc_to_str(dev->vgaarb_rsrc));
173 return vgaarb_write(pci_sys->vgaarb_fd, buf, len);
177 pci_device_vgaarb_lock(struct pci_device *dev)
182 len = snprintf(buf, BUFSIZE, "lock %s", rsrc_to_str(dev->vgaarb_rsrc));
184 return vgaarb_write(pci_sys->vgaarb_fd, buf, len);
188 pci_device_vgaarb_trylock(struct pci_device *dev)
193 len = snprintf(buf, BUFSIZE, "trylock %s", rsrc_to_str(dev->vgaarb_rsrc));
195 return vgaarb_write(pci_sys->vgaarb_fd, buf, len);
199 pci_device_vgaarb_unlock(struct pci_device *dev)
204 len = snprintf(buf, BUFSIZE, "unlock %s", rsrc_to_str(dev->vgaarb_rsrc));
206 return vgaarb_write(pci_sys->vgaarb_fd, buf, len);