1/**
2 * \file
3 * \brief Common PCI data types
4 */
5
6/*
7 * Copyright (c) 2007, 2008, 2009, 2018 ETH Zurich.
8 * All rights reserved.
9 *
10 * This file is distributed under the terms in the attached LICENSE file.
11 * If you do not find this file, copies can be found by writing to:
12 * ETH Zurich D-INFK, Haldeneggsteig 4, CH-8092 Zurich. Attn: Systems Group.
13 */
14
15#ifndef PCI_TYPES_H
16#define PCI_TYPES_H
17
18#include <stdint.h>
19#include <stdio.h>
20#include <errors/errno.h>
21
22/* Most of the members are smaller, but to allow PCI_DONT_CARE, everything
23 * is expressed as uint32_t */
24
25struct pci_addr {
26    uint32_t bus;
27    uint32_t device;
28    uint32_t function;
29};
30
31struct pci_id {
32    uint32_t device;
33    uint32_t vendor;
34};
35
36struct pci_class {
37    uint32_t class_code;
38    uint32_t subclass;
39    uint32_t prog_if;
40};
41
42#define PCI_OCTET_LEN (8*4+8)
43
44static inline void pci_serialize_octet(
45        struct pci_addr addr,
46        struct pci_id id,
47        struct pci_class cls,
48        char *out)
49{
50    snprintf(out, 8*4+8, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x",
51           addr.bus, addr.device, addr.function,
52           id.device, id.vendor,
53           cls.class_code, cls.subclass, cls.prog_if);
54};
55
56static inline errval_t pci_deserialize_octet(
57        char *in,
58        struct pci_addr* addr,
59        struct pci_id* id,
60        struct pci_class* cls
61        )
62{
63    int scn = sscanf(in, "%x:%x:%x:%x:%x:%x:%x:%x",
64           &addr->bus, &addr->device, &addr->function,
65           &id->device, &id->vendor,
66           &cls->class_code, &cls->subclass, &cls->prog_if);
67
68    return scn == 8 ? SYS_ERR_OK : PCI_ERR_ARG_PARSE;
69};
70
71
72#endif
73