Changes in uspace/lib/drv/generic/driver.c [5fdd7c3:228e490] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/drv/generic/driver.c
r5fdd7c3 r228e490 52 52 #include <ipc/driver.h> 53 53 54 #include "dev_iface.h"55 54 #include "driver.h" 56 55 57 /** Driver structure */ 56 /* driver structure */ 57 58 58 static driver_t *driver; 59 59 60 /** Devices */ 60 /* devices */ 61 61 62 LIST_INITIALIZE(devices); 62 63 FIBRIL_MUTEX_INITIALIZE(devices_mutex); 63 64 64 /** Interrupts */ 65 /* interrupts */ 66 65 67 static interrupt_context_list_t interrupt_contexts; 66 68 … … 83 85 84 86 ctx = find_interrupt_context_by_id(&interrupt_contexts, id); 85 if ( ctx != NULL && ctx->handler != NULL)87 if (NULL != ctx && NULL != ctx->handler) 86 88 (*ctx->handler)(ctx->dev, iid, icall); 87 89 } 88 89 interrupt_context_t *create_interrupt_context(void)90 {91 interrupt_context_t *ctx;92 93 ctx = (interrupt_context_t *) malloc(sizeof(interrupt_context_t));94 if (ctx != NULL)95 memset(ctx, 0, sizeof(interrupt_context_t));96 97 return ctx;98 }99 100 void delete_interrupt_context(interrupt_context_t *ctx)101 {102 if (ctx != NULL)103 free(ctx);104 }105 106 void init_interrupt_context_list(interrupt_context_list_t *list)107 {108 memset(list, 0, sizeof(interrupt_context_list_t));109 fibril_mutex_initialize(&list->mutex);110 list_initialize(&list->contexts);111 }112 113 void114 add_interrupt_context(interrupt_context_list_t *list, interrupt_context_t *ctx)115 {116 fibril_mutex_lock(&list->mutex);117 ctx->id = list->curr_id++;118 list_append(&ctx->link, &list->contexts);119 fibril_mutex_unlock(&list->mutex);120 }121 122 void remove_interrupt_context(interrupt_context_list_t *list,123 interrupt_context_t *ctx)124 {125 fibril_mutex_lock(&list->mutex);126 list_remove(&ctx->link);127 fibril_mutex_unlock(&list->mutex);128 }129 130 interrupt_context_t *131 find_interrupt_context_by_id(interrupt_context_list_t *list, int id)132 {133 fibril_mutex_lock(&list->mutex);134 135 link_t *link = list->contexts.next;136 interrupt_context_t *ctx;137 138 while (link != &list->contexts) {139 ctx = list_get_instance(link, interrupt_context_t, link);140 if (ctx->id == id) {141 fibril_mutex_unlock(&list->mutex);142 return ctx;143 }144 link = link->next;145 }146 147 fibril_mutex_unlock(&list->mutex);148 return NULL;149 }150 151 interrupt_context_t *152 find_interrupt_context(interrupt_context_list_t *list, device_t *dev, int irq)153 {154 fibril_mutex_lock(&list->mutex);155 156 link_t *link = list->contexts.next;157 interrupt_context_t *ctx;158 159 while (link != &list->contexts) {160 ctx = list_get_instance(link, interrupt_context_t, link);161 if (ctx->irq == irq && ctx->dev == dev) {162 fibril_mutex_unlock(&list->mutex);163 return ctx;164 }165 link = link->next;166 }167 168 fibril_mutex_unlock(&list->mutex);169 return NULL;170 }171 172 90 173 91 int … … 183 101 add_interrupt_context(&interrupt_contexts, ctx); 184 102 185 if ( pseudocode == NULL)103 if (NULL == pseudocode) 186 104 pseudocode = &default_pseudocode; 187 105 188 106 int res = ipc_register_irq(irq, dev->handle, ctx->id, pseudocode); 189 if ( res != EOK) {107 if (0 != res) { 190 108 remove_interrupt_context(&interrupt_contexts, ctx); 191 109 delete_interrupt_context(ctx); … … 200 118 dev, irq); 201 119 int res = ipc_unregister_irq(irq, dev->handle); 202 203 if ( ctx != NULL) {120 121 if (NULL != ctx) { 204 122 remove_interrupt_context(&interrupt_contexts, ctx); 205 123 delete_interrupt_context(ctx); 206 124 } 207 208 125 return res; 209 126 } … … 223 140 } 224 141 225 static device_t * driver_get_device(link_t *devices, devman_handle_t handle)142 static device_t * driver_get_device(link_t *devices, devman_handle_t handle) 226 143 { 227 144 device_t *dev = NULL; … … 229 146 fibril_mutex_lock(&devices_mutex); 230 147 link_t *link = devices->next; 231 232 148 while (link != devices) { 233 149 dev = list_get_instance(link, device_t, link); 234 if ( dev->handle ==handle) {150 if (handle == dev->handle) { 235 151 fibril_mutex_unlock(&devices_mutex); 236 152 return dev; … … 238 154 link = link->next; 239 155 } 240 241 156 fibril_mutex_unlock(&devices_mutex); 242 157 243 158 return NULL; 244 159 } … … 247 162 { 248 163 char *dev_name = NULL; 249 int res ;250 251 devman_handle_t dev_handle = IPC_GET_ARG1(*icall);164 int res = EOK; 165 166 devman_handle_t dev_handle = IPC_GET_ARG1(*icall); 252 167 devman_handle_t parent_dev_handle = IPC_GET_ARG2(*icall); 253 168 254 169 device_t *dev = create_device(); 255 170 dev->handle = dev_handle; … … 262 177 263 178 res = driver->driver_ops->add_device(dev); 264 if ( res == EOK) {179 if (0 == res) { 265 180 printf("%s: new device with handle=%" PRIun " was added.\n", 266 181 driver->name, dev_handle); … … 279 194 /* Accept connection */ 280 195 ipc_answer_0(iid, EOK); 281 196 282 197 bool cont = true; 283 198 while (cont) { 284 199 ipc_call_t call; 285 200 ipc_callid_t callid = async_get_call(&call); 286 201 287 202 switch (IPC_GET_IMETHOD(call)) { 288 203 case IPC_M_PHONE_HUNGUP: … … 325 240 * use the device. 326 241 */ 327 242 328 243 int ret = EOK; 329 244 /* open the device */ 330 if ( dev->ops != NULL && dev->ops->open != NULL)245 if (NULL != dev->ops && NULL != dev->ops->open) 331 246 ret = (*dev->ops->open)(dev); 332 247 333 ipc_answer_0(iid, ret); 334 if ( ret != EOK)248 ipc_answer_0(iid, ret); 249 if (EOK != ret) 335 250 return; 336 251 337 252 while (1) { 338 253 ipc_callid_t callid; … … 343 258 344 259 switch (method) { 345 case IPC_M_PHONE_HUNGUP: 260 case IPC_M_PHONE_HUNGUP: 346 261 /* close the device */ 347 if ( dev->ops != NULL && dev->ops->close != NULL)262 if (NULL != dev->ops && NULL != dev->ops->close) 348 263 (*dev->ops->close)(dev); 349 264 ipc_answer_0(callid, EOK); 350 265 return; 351 default: 266 default: 352 267 /* convert ipc interface id to interface index */ 353 268 … … 357 272 remote_handler_t *default_handler = 358 273 device_get_default_handler(dev); 359 if ( default_handler != NULL) {274 if (NULL != default_handler) { 360 275 (*default_handler)(dev, callid, &call); 361 276 break; … … 371 286 break; 372 287 } 373 288 374 289 /* calling one of the device's interfaces */ 375 290 376 /* Get the interface ops structure.*/377 void * ops = device_get_ops(dev, iface_idx);378 if ( ops == NULL) {291 /* get the device interface structure */ 292 void *iface = device_get_iface(dev, iface_idx); 293 if (NULL == iface) { 379 294 printf("%s: driver_connection_gen error - ", 380 295 driver->name); … … 384 299 break; 385 300 } 386 301 387 302 /* 388 303 * Get the corresponding interface for remote request 389 304 * handling ("remote interface"). 390 305 */ 391 remote_iface_t *rem_iface = get_remote_iface(iface_idx);392 assert( rem_iface != NULL);393 306 remote_iface_t* rem_iface = get_remote_iface(iface_idx); 307 assert(NULL != rem_iface); 308 394 309 /* get the method of the remote interface */ 395 310 sysarg_t iface_method_idx = IPC_GET_ARG1(call); 396 311 remote_iface_func_ptr_t iface_method_ptr = 397 312 get_remote_method(rem_iface, iface_method_idx); 398 if ( iface_method_ptr == NULL) {313 if (NULL == iface_method_ptr) { 399 314 // the interface has not such method 400 315 printf("%s: driver_connection_gen error - " … … 410 325 * associated with the device by its driver. 411 326 */ 412 (*iface_method_ptr)(dev, ops, callid, &call);327 (*iface_method_ptr)(dev, iface, callid, &call); 413 328 break; 414 329 } … … 433 348 switch ((sysarg_t) (IPC_GET_ARG1(*icall))) { 434 349 case DRIVER_DEVMAN: 435 /* Handle requestfrom device manager */350 /* handle PnP events from device manager */ 436 351 driver_connection_devman(iid, icall); 437 352 break; 438 353 case DRIVER_DRIVER: 439 /* Handle request from drivers of child devices */354 /* handle request from drivers of child devices */ 440 355 driver_connection_driver(iid, icall); 441 356 break; 442 357 case DRIVER_CLIENT: 443 /* Handle requestfrom client applications */358 /* handle requests from client applications */ 444 359 driver_connection_client(iid, icall); 445 360 break; 361 446 362 default: 447 363 /* No such interface */ … … 450 366 } 451 367 452 /** Create new device structure.453 *454 * @return The device structure.455 */456 device_t *create_device(void)457 {458 device_t *dev = malloc(sizeof(device_t));459 460 if (dev != NULL) {461 memset(dev, 0, sizeof(device_t));462 init_match_ids(&dev->match_ids);463 }464 465 return dev;466 }467 468 /** Delete device structure.469 *470 * @param dev The device structure.471 */472 void delete_device(device_t *dev)473 {474 clean_match_ids(&dev->match_ids);475 if (dev->name != NULL)476 free(dev->name);477 free(dev);478 }479 480 void *device_get_ops(device_t *dev, dev_inferface_idx_t idx)481 {482 assert(is_valid_iface_idx(idx));483 if (dev->ops == NULL)484 return NULL;485 return dev->ops->interfaces[idx];486 }487 488 368 int child_device_register(device_t *child, device_t *parent) 489 369 { 490 assert( child->name != NULL);491 370 assert(NULL != child->name); 371 492 372 int res; 493 373 … … 495 375 res = devman_child_device_register(child->name, &child->match_ids, 496 376 parent->handle, &child->handle); 497 if (res != EOK) { 498 remove_from_devices_list(child); 377 if (EOK == res) 499 378 return res; 500 } 501 379 remove_from_devices_list(child); 502 380 return res; 503 381 } … … 517 395 match_id_t *match_id = NULL; 518 396 int rc; 519 397 520 398 child = create_device(); 521 399 if (child == NULL) { … … 523 401 goto failure; 524 402 } 525 403 526 404 child->name = child_name; 527 405 528 406 match_id = create_match_id(); 529 407 if (match_id == NULL) { … … 531 409 goto failure; 532 410 } 533 411 534 412 match_id->id = child_match_id; 535 413 match_id->score = child_match_score; 536 414 add_match_id(&child->match_ids, match_id); 537 415 538 416 rc = child_device_register(child, parent); 539 if ( rc != EOK)417 if (EOK != rc) 540 418 goto failure; 541 419 542 420 return EOK; 543 421 544 422 failure: 545 423 if (match_id != NULL) { … … 547 425 delete_match_id(match_id); 548 426 } 549 427 550 428 if (child != NULL) { 551 429 child->name = NULL; 552 430 delete_device(child); 553 431 } 554 432 555 433 return rc; 556 }557 558 /** Get default handler for client requests */559 remote_handler_t *device_get_default_handler(device_t *dev)560 {561 if (dev->ops == NULL)562 return NULL;563 return dev->ops->default_handler;564 }565 566 int add_device_to_class(device_t *dev, const char *class_name)567 {568 return devman_add_device_to_class(dev->handle, class_name);569 434 } 570 435 … … 576 441 */ 577 442 driver = drv; 578 443 579 444 /* Initialize the list of interrupt contexts. */ 580 445 init_interrupt_context_list(&interrupt_contexts); … … 588 453 */ 589 454 devman_driver_register(driver->name, driver_connection); 590 455 591 456 async_manager(); 592 457 593 458 /* Never reached. */ 594 459 return 0;
Note:
See TracChangeset
for help on using the changeset viewer.