| 1 | /*
|
|---|
| 2 | * Copyright (c) 2010 Vojtech Horky
|
|---|
| 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 usb
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | * @brief Address management.
|
|---|
| 34 | */
|
|---|
| 35 |
|
|---|
| 36 | #include <usb/usb.h>
|
|---|
| 37 | #include <stdlib.h>
|
|---|
| 38 | #include <stdio.h>
|
|---|
| 39 | #include <errno.h>
|
|---|
| 40 | #include <str_error.h>
|
|---|
| 41 | #include <fibril_synch.h>
|
|---|
| 42 |
|
|---|
| 43 | #include "vhcd.h"
|
|---|
| 44 | #include "conn.h"
|
|---|
| 45 |
|
|---|
| 46 | #define ADDRESS_COUNT 100
|
|---|
| 47 | #define DEFAULT_ADDRESS 0
|
|---|
| 48 |
|
|---|
| 49 | typedef struct {
|
|---|
| 50 | usb_address_t address;
|
|---|
| 51 | bool available;
|
|---|
| 52 | } address_info_t;
|
|---|
| 53 |
|
|---|
| 54 | static address_info_t dev_address[ADDRESS_COUNT];
|
|---|
| 55 | fibril_mutex_t address_guard;
|
|---|
| 56 |
|
|---|
| 57 | typedef struct {
|
|---|
| 58 | fibril_mutex_t condvar_guard;
|
|---|
| 59 | fibril_condvar_t condvar;
|
|---|
| 60 | bool available;
|
|---|
| 61 | } reservable_address_info_t;
|
|---|
| 62 |
|
|---|
| 63 | static reservable_address_info_t default_address;
|
|---|
| 64 |
|
|---|
| 65 | void address_init(void)
|
|---|
| 66 | {
|
|---|
| 67 | usb_address_t i;
|
|---|
| 68 | for (i = 0; i < ADDRESS_COUNT; i++) {
|
|---|
| 69 | dev_address[i].address = i + 1;
|
|---|
| 70 | dev_address[i].available = true;
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | fibril_mutex_initialize(&address_guard);
|
|---|
| 74 | fibril_mutex_initialize(&default_address.condvar_guard);
|
|---|
| 75 | fibril_condvar_initialize(&default_address.condvar);
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | int reserve_default_address(device_t *dev)
|
|---|
| 79 | {
|
|---|
| 80 | fibril_mutex_lock(&default_address.condvar_guard);
|
|---|
| 81 | while (!default_address.available) {
|
|---|
| 82 | fibril_condvar_wait(&default_address.condvar,
|
|---|
| 83 | &default_address.condvar_guard);
|
|---|
| 84 | }
|
|---|
| 85 | fibril_mutex_unlock(&default_address.condvar_guard);
|
|---|
| 86 |
|
|---|
| 87 | return EOK;
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | int release_default_address(device_t *dev)
|
|---|
| 91 | {
|
|---|
| 92 | fibril_mutex_lock(&default_address.condvar_guard);
|
|---|
| 93 | default_address.available = true;
|
|---|
| 94 | fibril_condvar_signal(&default_address.condvar);
|
|---|
| 95 | fibril_mutex_unlock(&default_address.condvar_guard);
|
|---|
| 96 |
|
|---|
| 97 | return EOK;
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | int request_address(device_t *dev, usb_address_t *address)
|
|---|
| 101 | {
|
|---|
| 102 | fibril_mutex_lock(&address_guard);
|
|---|
| 103 | usb_address_t i;
|
|---|
| 104 | bool found = false;
|
|---|
| 105 | for (i = 0; i < ADDRESS_COUNT; i++) {
|
|---|
| 106 | if (dev_address[i].available) {
|
|---|
| 107 | dev_address[i].available = false;
|
|---|
| 108 | *address = dev_address[i].address;
|
|---|
| 109 | found = true;
|
|---|
| 110 | break;
|
|---|
| 111 | }
|
|---|
| 112 | }
|
|---|
| 113 | fibril_mutex_unlock(&address_guard);
|
|---|
| 114 |
|
|---|
| 115 | if (!found) {
|
|---|
| 116 | return ELIMIT;
|
|---|
| 117 | } else {
|
|---|
| 118 | return EOK;
|
|---|
| 119 | }
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | int release_address(device_t *dev, usb_address_t address)
|
|---|
| 123 | {
|
|---|
| 124 | if (address == DEFAULT_ADDRESS) {
|
|---|
| 125 | return EPERM;
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | int rc = EPERM;
|
|---|
| 129 |
|
|---|
| 130 | fibril_mutex_lock(&address_guard);
|
|---|
| 131 | usb_address_t i;
|
|---|
| 132 | for (i = 0; i < ADDRESS_COUNT; i++) {
|
|---|
| 133 | if (dev_address[i].address == address) {
|
|---|
| 134 | if (dev_address[i].available) {
|
|---|
| 135 | rc = ENOENT;
|
|---|
| 136 | break;
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | dev_address[i].available = true;
|
|---|
| 140 | rc = EOK;
|
|---|
| 141 | break;
|
|---|
| 142 | }
|
|---|
| 143 | }
|
|---|
| 144 | fibril_mutex_unlock(&address_guard);
|
|---|
| 145 |
|
|---|
| 146 | return rc;
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | /**
|
|---|
| 150 | * @}
|
|---|
| 151 | */
|
|---|