| 1 | /*
|
|---|
| 2 | * Copyright (c) 2010 Lenka Trochtova
|
|---|
| 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 |
|
|---|
| 37 |
|
|---|
| 38 | #include <adt/list.h>
|
|---|
| 39 | #include <ipc/devman.h>
|
|---|
| 40 | #include <ipc/dev_iface.h>
|
|---|
| 41 | #include <device/hw_res.h>
|
|---|
| 42 | #include <assert.h>
|
|---|
| 43 |
|
|---|
| 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 {
|
|---|
| 54 | size_t method_count;
|
|---|
| 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 |
|
|---|
| 63 | static inline bool is_valid_iface_idx(int idx)
|
|---|
| 64 | {
|
|---|
| 65 | return 0 <= idx && idx < DEV_IFACE_MAX;
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | remote_iface_t* get_remote_iface(int idx);
|
|---|
| 69 | remote_iface_func_ptr_t get_remote_method(remote_iface_t *rem_iface, ipcarg_t iface_method_idx);
|
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 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 |
|
|---|
| 83 | // device
|
|---|
| 84 |
|
|---|
| 85 | /** The device. */
|
|---|
| 86 | struct device {
|
|---|
| 87 | /** Globally unique device identifier (assigned to the device by the device manager). */
|
|---|
| 88 | device_handle_t handle;
|
|---|
| 89 | /** The phone to the parent device driver (if it is different from this driver).*/
|
|---|
| 90 | int parent_phone;
|
|---|
| 91 | /** Parent device if handled by this driver, NULL otherwise. */
|
|---|
| 92 | device_t *parent;
|
|---|
| 93 | /** The device's name.*/
|
|---|
| 94 | const char *name;
|
|---|
| 95 | /** The list of device ids for device-to-driver matching.*/
|
|---|
| 96 | match_id_list_t match_ids;
|
|---|
| 97 | /** The device driver's data associated with this device.*/
|
|---|
| 98 | void *driver_data;
|
|---|
| 99 | /** Device class consist of class id and table of interfaces supported by the device.*/
|
|---|
| 100 | device_class_t *class;
|
|---|
| 101 | /** Pointer to the previous and next device in the list of devices handled by the driver */
|
|---|
| 102 | link_t link;
|
|---|
| 103 | };
|
|---|
| 104 |
|
|---|
| 105 |
|
|---|
| 106 | // driver
|
|---|
| 107 |
|
|---|
| 108 | /** Generic device driver operations. */
|
|---|
| 109 | typedef struct driver_ops {
|
|---|
| 110 | /** Callback method for passing a new device to the device driver.*/
|
|---|
| 111 | bool (*add_device)(device_t *dev);
|
|---|
| 112 | // TODO add other generic driver operations
|
|---|
| 113 | } driver_ops_t;
|
|---|
| 114 |
|
|---|
| 115 | /** The driver structure.*/
|
|---|
| 116 | typedef struct driver {
|
|---|
| 117 | /** The name of the device driver. */
|
|---|
| 118 | const char *name;
|
|---|
| 119 | /** Generic device driver operations. */
|
|---|
| 120 | driver_ops_t *driver_ops;
|
|---|
| 121 | } driver_t;
|
|---|
| 122 |
|
|---|
| 123 |
|
|---|
| 124 |
|
|---|
| 125 |
|
|---|
| 126 |
|
|---|
| 127 | int driver_main(driver_t *drv);
|
|---|
| 128 |
|
|---|
| 129 | /** Create new device structure.
|
|---|
| 130 | *
|
|---|
| 131 | * @return the device structure.
|
|---|
| 132 | */
|
|---|
| 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));
|
|---|
| 138 | }
|
|---|
| 139 | list_initialize(&dev->match_ids.ids);
|
|---|
| 140 | return dev;
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | /** Delete device structure.
|
|---|
| 144 | *
|
|---|
| 145 | * @param dev the device structure.
|
|---|
| 146 | */
|
|---|
| 147 | static inline void delete_device(device_t *dev)
|
|---|
| 148 | {
|
|---|
| 149 | clean_match_ids(&dev->match_ids);
|
|---|
| 150 | if (NULL != dev->name) {
|
|---|
| 151 | free(dev->name);
|
|---|
| 152 | }
|
|---|
| 153 | free(dev);
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 | static inline void * device_get_iface(device_t *dev, dev_inferface_idx_t idx)
|
|---|
| 157 | {
|
|---|
| 158 | assert(is_valid_iface_idx(idx));
|
|---|
| 159 |
|
|---|
| 160 | return dev->class->interfaces[idx];
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 | bool child_device_register(device_t *child, device_t *parent);
|
|---|
| 164 |
|
|---|
| 165 |
|
|---|
| 166 | #endif
|
|---|
| 167 |
|
|---|
| 168 | /**
|
|---|
| 169 | * @}
|
|---|
| 170 | */
|
|---|