| [c16cf62] | 1 | /*
|
|---|
| [a1769ee] | 2 | * Copyright (c) 2010 Lenka Trochtova
|
|---|
| [c16cf62] | 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | /** @addtogroup libdrv
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | */
|
|---|
| 34 | #ifndef LIBDRV_DRIVER_H_
|
|---|
| 35 | #define LIBDRV_DRIVER_H_
|
|---|
| 36 |
|
|---|
| [084ff99] | 37 |
|
|---|
| 38 | #include <adt/list.h>
|
|---|
| [bda60d9] | 39 | #include <ipc/devman.h>
|
|---|
| [a1769ee] | 40 | #include <ipc/dev_iface.h>
|
|---|
| [3843ecb] | 41 | #include <device/hw_res.h>
|
|---|
| [52b7b1bb] | 42 | #include <assert.h>
|
|---|
| [084ff99] | 43 |
|
|---|
| [a1769ee] | 44 | struct device;
|
|---|
| 45 | typedef struct device device_t;
|
|---|
| 46 |
|
|---|
| 47 | // device interface
|
|---|
| 48 |
|
|---|
| 49 | // first two parameters: device and interface structure registered by the devices driver
|
|---|
| 50 | typedef void remote_iface_func_t(device_t*, void *, ipc_callid_t, ipc_call_t *);
|
|---|
| 51 | typedef remote_iface_func_t *remote_iface_func_ptr_t;
|
|---|
| 52 |
|
|---|
| 53 | typedef struct {
|
|---|
| [52b7b1bb] | 54 | size_t method_count;
|
|---|
| [a1769ee] | 55 | remote_iface_func_ptr_t *methods;
|
|---|
| 56 | } remote_iface_t;
|
|---|
| 57 |
|
|---|
| 58 | typedef struct {
|
|---|
| 59 | remote_iface_t * ifaces[DEV_IFACE_COUNT];
|
|---|
| 60 | } iface_dipatch_table_t;
|
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| [3843ecb] | 63 | static inline bool is_valid_iface_idx(int idx)
|
|---|
| [a1769ee] | 64 | {
|
|---|
| [3843ecb] | 65 | return 0 <= idx && idx < DEV_IFACE_MAX;
|
|---|
| [a1769ee] | 66 | }
|
|---|
| 67 |
|
|---|
| [3843ecb] | 68 | remote_iface_t* get_remote_iface(int idx);
|
|---|
| [52b7b1bb] | 69 | remote_iface_func_ptr_t get_remote_method(remote_iface_t *rem_iface, ipcarg_t iface_method_idx);
|
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| [3843ecb] | 72 | // device class
|
|---|
| 73 |
|
|---|
| 74 | /** Devices belonging to the same class should implement the same set of interfaces.*/
|
|---|
| 75 | typedef struct device_class {
|
|---|
| 76 | /** Unique identification of the class. */
|
|---|
| 77 | int id;
|
|---|
| 78 | /** The table of interfaces implemented by the device. */
|
|---|
| 79 | void *interfaces[DEV_IFACE_COUNT];
|
|---|
| 80 | } device_class_t;
|
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| [a1769ee] | 83 | // device
|
|---|
| 84 |
|
|---|
| [52b7b1bb] | 85 | /** The device. */
|
|---|
| [a1769ee] | 86 | struct device {
|
|---|
| [52b7b1bb] | 87 | /** Globally unique device identifier (assigned to the device by the device manager). */
|
|---|
| [bda60d9] | 88 | device_handle_t handle;
|
|---|
| [5e598e0] | 89 | /** The phone to the parent device driver (if it is different from this driver).*/
|
|---|
| [9a66bc2e] | 90 | int parent_phone;
|
|---|
| [5e598e0] | 91 | /** Parent device if handled by this driver, NULL otherwise. */
|
|---|
| 92 | device_t *parent;
|
|---|
| [52b7b1bb] | 93 | /** The device's name.*/
|
|---|
| [bda60d9] | 94 | const char *name;
|
|---|
| [52b7b1bb] | 95 | /** The list of device ids for device-to-driver matching.*/
|
|---|
| [bda60d9] | 96 | match_id_list_t match_ids;
|
|---|
| [52b7b1bb] | 97 | /** The device driver's data associated with this device.*/
|
|---|
| [eff1a590] | 98 | void *driver_data;
|
|---|
| [3843ecb] | 99 | /** Device class consist of class id and table of interfaces supported by the device.*/
|
|---|
| 100 | device_class_t *class;
|
|---|
| [52b7b1bb] | 101 | /** Pointer to the previous and next device in the list of devices handled by the driver */
|
|---|
| [084ff99] | 102 | link_t link;
|
|---|
| [a1769ee] | 103 | };
|
|---|
| [c16cf62] | 104 |
|
|---|
| [a1769ee] | 105 |
|
|---|
| 106 | // driver
|
|---|
| 107 |
|
|---|
| [52b7b1bb] | 108 | /** Generic device driver operations. */
|
|---|
| [a1769ee] | 109 | typedef struct driver_ops {
|
|---|
| [52b7b1bb] | 110 | /** Callback method for passing a new device to the device driver.*/
|
|---|
| [c16cf62] | 111 | bool (*add_device)(device_t *dev);
|
|---|
| 112 | // TODO add other generic driver operations
|
|---|
| 113 | } driver_ops_t;
|
|---|
| 114 |
|
|---|
| [52b7b1bb] | 115 | /** The driver structure.*/
|
|---|
| [c16cf62] | 116 | typedef struct driver {
|
|---|
| [52b7b1bb] | 117 | /** The name of the device driver. */
|
|---|
| [c16cf62] | 118 | const char *name;
|
|---|
| [52b7b1bb] | 119 | /** Generic device driver operations. */
|
|---|
| [c16cf62] | 120 | driver_ops_t *driver_ops;
|
|---|
| 121 | } driver_t;
|
|---|
| 122 |
|
|---|
| [a1769ee] | 123 |
|
|---|
| 124 |
|
|---|
| 125 |
|
|---|
| 126 |
|
|---|
| [c16cf62] | 127 | int driver_main(driver_t *drv);
|
|---|
| 128 |
|
|---|
| [52b7b1bb] | 129 | /** Create new device structure.
|
|---|
| 130 | *
|
|---|
| 131 | * @return the device structure.
|
|---|
| 132 | */
|
|---|
| [7707954] | 133 | static inline device_t * create_device()
|
|---|
| 134 | {
|
|---|
| 135 | device_t *dev = malloc(sizeof(device_t));
|
|---|
| 136 | if (NULL != dev) {
|
|---|
| 137 | memset(dev, 0, sizeof(device_t));
|
|---|
| [a1769ee] | 138 | }
|
|---|
| [bda60d9] | 139 | list_initialize(&dev->match_ids.ids);
|
|---|
| [7707954] | 140 | return dev;
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| [52b7b1bb] | 143 | /** Delete device structure.
|
|---|
| 144 | *
|
|---|
| 145 | * @param dev the device structure.
|
|---|
| 146 | */
|
|---|
| [a1769ee] | 147 | static inline void delete_device(device_t *dev)
|
|---|
| 148 | {
|
|---|
| [bda60d9] | 149 | clean_match_ids(&dev->match_ids);
|
|---|
| 150 | if (NULL != dev->name) {
|
|---|
| 151 | free(dev->name);
|
|---|
| 152 | }
|
|---|
| 153 | free(dev);
|
|---|
| 154 | }
|
|---|
| [7707954] | 155 |
|
|---|
| [3843ecb] | 156 | static inline void * device_get_iface(device_t *dev, dev_inferface_idx_t idx)
|
|---|
| [a1769ee] | 157 | {
|
|---|
| [3843ecb] | 158 | assert(is_valid_iface_idx(idx));
|
|---|
| [a1769ee] | 159 |
|
|---|
| [3843ecb] | 160 | return dev->class->interfaces[idx];
|
|---|
| [52b7b1bb] | 161 | }
|
|---|
| 162 |
|
|---|
| [bda60d9] | 163 | bool child_device_register(device_t *child, device_t *parent);
|
|---|
| [7707954] | 164 |
|
|---|
| 165 |
|
|---|
| [c16cf62] | 166 | #endif
|
|---|
| 167 |
|
|---|
| 168 | /**
|
|---|
| 169 | * @}
|
|---|
| [a1769ee] | 170 | */
|
|---|