Changeset 80bffdb0 in mainline for uspace/lib/drv/include/driver.h
- Timestamp:
- 2011-01-09T21:02:35Z (14 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 8b5c8ae
- Parents:
- d6b1359 (diff), 22027b6e (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/drv/include/driver.h
rd6b1359 r80bffdb0 47 47 #include <malloc.h> 48 48 49 #include "dev_iface.h" 50 49 51 struct device; 50 52 typedef struct device device_t; 51 53 52 /* device interface */ 54 /* 55 * Device class 56 */ 53 57 54 /* 55 * First two parameters: device and interface structure registered by the 56 * devices driver. 57 */ 58 typedef void remote_iface_func_t(device_t *, void *, ipc_callid_t, 59 ipc_call_t *); 60 typedef remote_iface_func_t *remote_iface_func_ptr_t; 61 typedef void remote_handler_t(device_t *, ipc_callid_t, ipc_call_t *); 62 63 typedef struct { 64 size_t method_count; 65 remote_iface_func_ptr_t *methods; 66 } remote_iface_t; 67 68 typedef struct { 69 remote_iface_t *ifaces[DEV_IFACE_COUNT]; 70 } iface_dipatch_table_t; 71 72 73 static inline bool is_valid_iface_idx(int idx) 74 { 75 return 0 <= idx && idx < DEV_IFACE_MAX; 76 } 77 78 remote_iface_t *get_remote_iface(int); 79 remote_iface_func_ptr_t get_remote_method(remote_iface_t *, sysarg_t); 80 81 82 /* device class */ 83 84 /** Devices operations. */ 58 /** Devices operations */ 85 59 typedef struct device_ops { 86 60 /** … … 108 82 109 83 110 /* device */ 84 /* 85 * Device 86 */ 111 87 112 /** The device.*/88 /** Device structure */ 113 89 struct device { 114 90 /** … … 119 95 120 96 /** 121 * The phone to the parent device driver (if it is different from this122 * driver) .97 * Phone to the parent device driver (if it is different from this 98 * driver) 123 99 */ 124 100 int parent_phone; 125 101 126 /** Parent device if handled by this driver, NULL otherwise .*/102 /** Parent device if handled by this driver, NULL otherwise */ 127 103 device_t *parent; 128 /** The device's name.*/104 /** Device name */ 129 105 const char *name; 130 /** The list of device ids for device-to-driver matching.*/106 /** List of device ids for device-to-driver matching */ 131 107 match_id_list_t match_ids; 132 /** The device driver's data associated with this device.*/108 /** Driver-specific data associated with this device */ 133 109 void *driver_data; 134 /** The implementation of operations provided by this device .*/110 /** The implementation of operations provided by this device */ 135 111 device_ops_t *ops; 136 112 137 /** 138 * Pointer to the previous and next device in the list of devices 139 * handled by the driver. 140 */ 113 /** Link in the list of devices handled by the driver */ 141 114 link_t link; 142 115 }; 143 116 117 /* 118 * Driver 119 */ 144 120 145 /* driver */ 146 147 /** Generic device driver operations. */ 121 /** Generic device driver operations */ 148 122 typedef struct driver_ops { 149 /** Callback method for passing a new device to the device driver .*/123 /** Callback method for passing a new device to the device driver */ 150 124 int (*add_device)(device_t *dev); 151 /* TODO add other generic driver operations */125 /* TODO: add other generic driver operations */ 152 126 } driver_ops_t; 153 127 154 /** The driver structure.*/128 /** Driver structure */ 155 129 typedef struct driver { 156 /** The name of the device driver.*/130 /** Name of the device driver */ 157 131 const char *name; 158 /** Generic device driver operations .*/132 /** Generic device driver operations */ 159 133 driver_ops_t *driver_ops; 160 134 } driver_t; … … 166 140 * @return The device structure. 167 141 */ 168 static inline device_t *create_device(void) 169 { 170 device_t *dev = malloc(sizeof(device_t)); 171 if (NULL != dev) { 172 memset(dev, 0, sizeof(device_t)); 173 init_match_ids(&dev->match_ids); 174 } 175 return dev; 176 } 142 extern device_t *create_device(void); 143 extern void delete_device(device_t *); 144 extern void *device_get_ops(device_t *, dev_inferface_idx_t); 177 145 178 /** Delete device structure. 179 * 180 * @param dev The device structure. 146 extern int child_device_register(device_t *, device_t *); 147 extern int child_device_register_wrapper(device_t *, const char *, const char *, 148 int); 149 150 /* 151 * Interrupts 181 152 */ 182 static inline void delete_device(device_t *dev)183 {184 clean_match_ids(&dev->match_ids);185 if (NULL != dev->name)186 free(dev->name);187 free(dev);188 }189 190 static inline void *device_get_iface(device_t *dev, dev_inferface_idx_t idx)191 {192 assert(is_valid_iface_idx(idx));193 if (NULL == dev->ops)194 return NULL;195 return dev->ops->interfaces[idx];196 }197 198 int child_device_register(device_t *, device_t *);199 int child_device_register_wrapper(device_t *, const char *, const char *, int);200 201 202 /* interrupts */203 153 204 154 typedef void interrupt_handler_t(device_t *, ipc_callid_t, ipc_call_t *); … … 218 168 } interrupt_context_list_t; 219 169 220 static inline interrupt_context_t *create_interrupt_context(void) 221 { 222 interrupt_context_t *ctx; 223 224 ctx = (interrupt_context_t *) malloc(sizeof(interrupt_context_t)); 225 if (NULL != ctx) 226 memset(ctx, 0, sizeof(interrupt_context_t)); 227 228 return ctx; 229 } 170 extern interrupt_context_t *create_interrupt_context(void); 171 extern void delete_interrupt_context(interrupt_context_t *); 172 extern void init_interrupt_context_list(interrupt_context_list_t *); 173 extern void add_interrupt_context(interrupt_context_list_t *, 174 interrupt_context_t *); 175 extern void remove_interrupt_context(interrupt_context_list_t *, 176 interrupt_context_t *); 177 extern interrupt_context_t *find_interrupt_context_by_id( 178 interrupt_context_list_t *, int); 179 extern interrupt_context_t *find_interrupt_context( 180 interrupt_context_list_t *, device_t *, int); 230 181 231 static inline void delete_interrupt_context(interrupt_context_t *ctx) 232 { 233 if (NULL != ctx) 234 free(ctx); 235 } 182 extern int register_interrupt_handler(device_t *, int, interrupt_handler_t *, 183 irq_code_t *); 184 extern int unregister_interrupt_handler(device_t *, int); 236 185 237 static inline void init_interrupt_context_list(interrupt_context_list_t *list) 238 { 239 memset(list, 0, sizeof(interrupt_context_list_t)); 240 fibril_mutex_initialize(&list->mutex); 241 list_initialize(&list->contexts); 242 } 243 244 static inline void 245 add_interrupt_context(interrupt_context_list_t *list, interrupt_context_t *ctx) 246 { 247 fibril_mutex_lock(&list->mutex); 248 ctx->id = list->curr_id++; 249 list_append(&ctx->link, &list->contexts); 250 fibril_mutex_unlock(&list->mutex); 251 } 252 253 static inline void 254 remove_interrupt_context(interrupt_context_list_t *list, 255 interrupt_context_t *ctx) 256 { 257 fibril_mutex_lock(&list->mutex); 258 list_remove(&ctx->link); 259 fibril_mutex_unlock(&list->mutex); 260 } 261 262 static inline interrupt_context_t * 263 find_interrupt_context_by_id(interrupt_context_list_t *list, int id) 264 { 265 fibril_mutex_lock(&list->mutex); 266 267 link_t *link = list->contexts.next; 268 interrupt_context_t *ctx; 269 270 while (link != &list->contexts) { 271 ctx = list_get_instance(link, interrupt_context_t, link); 272 if (id == ctx->id) { 273 fibril_mutex_unlock(&list->mutex); 274 return ctx; 275 } 276 link = link->next; 277 } 278 279 fibril_mutex_unlock(&list->mutex); 280 return NULL; 281 } 282 283 static inline interrupt_context_t * 284 find_interrupt_context(interrupt_context_list_t *list, device_t *dev, int irq) 285 { 286 fibril_mutex_lock(&list->mutex); 287 288 link_t *link = list->contexts.next; 289 interrupt_context_t *ctx; 290 291 while (link != &list->contexts) { 292 ctx = list_get_instance(link, interrupt_context_t, link); 293 if (irq == ctx->irq && dev == ctx->dev) { 294 fibril_mutex_unlock(&list->mutex); 295 return ctx; 296 } 297 link = link->next; 298 } 299 300 fibril_mutex_unlock(&list->mutex); 301 return NULL; 302 } 303 304 int register_interrupt_handler(device_t *, int, interrupt_handler_t *, 305 irq_code_t *); 306 int unregister_interrupt_handler(device_t *, int); 307 308 309 /* default handler for client requests */ 310 311 static inline remote_handler_t *device_get_default_handler(device_t *dev) 312 { 313 if (NULL == dev->ops) 314 return NULL; 315 return dev->ops->default_handler; 316 } 317 318 static inline int add_device_to_class(device_t *dev, const char *class_name) 319 { 320 return devman_add_device_to_class(dev->handle, class_name); 321 } 186 extern remote_handler_t *device_get_default_handler(device_t *); 187 extern int add_device_to_class(device_t *, const char *); 322 188 323 189 #endif
Note:
See TracChangeset
for help on using the changeset viewer.