Changeset c83a55c in mainline for uspace/lib/usbhost/src/usb_device_manager.c
- Timestamp:
- 2011-10-31T06:52:54Z (14 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- c25e39b
- Parents:
- e8ab32f (diff), 3ce78580 (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/usbhost/src/usb_device_manager.c
re8ab32f rc83a55c 38 38 #include <usb/host/usb_device_manager.h> 39 39 40 /*----------------------------------------------------------------------------*/41 40 /** Initialize device manager structure. 42 41 * … … 48 47 { 49 48 assert(instance); 50 unsigned i = 0; 51 for (; i < USB_ADDRESS_COUNT; ++i) { 49 for (unsigned i = 0; i < USB_ADDRESS_COUNT; ++i) { 52 50 instance->devices[i].occupied = false; 53 51 instance->devices[i].handle = 0; … … 77 75 ++new_address; 78 76 if (new_address > USB11_ADDRESS_MAX) 79 new_address = 1; 77 new_address = 1; // NOTE it should be safe to put 0 here 78 // TODO Use mod 80 79 if (new_address == instance->last_address) { 81 80 fibril_mutex_unlock(&instance->guard); … … 86 85 assert(new_address != USB_ADDRESS_DEFAULT); 87 86 assert(instance->devices[new_address].occupied == false); 87 assert(instance->devices[new_address].handle == 0); 88 88 89 89 instance->devices[new_address].occupied = true; … … 100 100 * @param[in] address Device address 101 101 * @param[in] handle Devman handle of the device. 102 */ 103 void usb_device_manager_bind(usb_device_manager_t *instance, 102 * @return Error code. 103 */ 104 int usb_device_manager_bind(usb_device_manager_t *instance, 104 105 usb_address_t address, devman_handle_t handle) 105 106 { 106 assert(instance); 107 fibril_mutex_lock(&instance->guard); 108 109 assert(address > 0); 110 assert(address <= USB11_ADDRESS_MAX); 111 assert(instance->devices[address].occupied); 112 107 if ((address <= 0) || (address >= USB_ADDRESS_COUNT)) { 108 return EINVAL; 109 } 110 assert(instance); 111 112 fibril_mutex_lock(&instance->guard); 113 /* Not reserved */ 114 if (!instance->devices[address].occupied) { 115 fibril_mutex_unlock(&instance->guard); 116 return ENOENT; 117 } 118 /* Already bound */ 119 if (instance->devices[address].handle != 0) { 120 fibril_mutex_unlock(&instance->guard); 121 return EEXISTS; 122 } 113 123 instance->devices[address].handle = handle; 114 124 fibril_mutex_unlock(&instance->guard); 125 return EOK; 115 126 } 116 127 /*----------------------------------------------------------------------------*/ … … 119 130 * @param[in] instance Device manager structure to use. 120 131 * @param[in] address Device address 121 */ 122 void usb_device_manager_release( 132 * @return Error code. 133 */ 134 int usb_device_manager_release( 123 135 usb_device_manager_t *instance, usb_address_t address) 124 136 { 125 assert(instance); 126 assert(address > 0); 127 assert(address <= USB11_ADDRESS_MAX); 128 129 fibril_mutex_lock(&instance->guard); 130 assert(instance->devices[address].occupied); 137 if ((address <= 0) || (address >= USB_ADDRESS_COUNT)) { 138 return EINVAL; 139 } 140 assert(instance); 141 142 fibril_mutex_lock(&instance->guard); 143 if (!instance->devices[address].occupied) { 144 fibril_mutex_unlock(&instance->guard); 145 return ENOENT; 146 } 131 147 132 148 instance->devices[address].occupied = false; 133 fibril_mutex_unlock(&instance->guard); 149 instance->devices[address].handle = 0; 150 fibril_mutex_unlock(&instance->guard); 151 return EOK; 134 152 } 135 153 /*----------------------------------------------------------------------------*/ … … 140 158 * @return USB Address, or error code. 141 159 */ 142 usb_address_t usb_device_manager_find (160 usb_address_t usb_device_manager_find_address( 143 161 usb_device_manager_t *instance, devman_handle_t handle) 144 162 { 145 163 assert(instance); 146 164 fibril_mutex_lock(&instance->guard); 147 usb_address_t address = 1;148 while (address <= USB11_ADDRESS_MAX){165 for (usb_address_t address = 1; address <= USB11_ADDRESS_MAX; ++address) 166 { 149 167 if (instance->devices[address].handle == handle) { 150 168 assert(instance->devices[address].occupied); … … 152 170 return address; 153 171 } 154 ++address;155 172 } 156 173 fibril_mutex_unlock(&instance->guard); 157 174 return ENOENT; 158 175 } 159 160 /** Find devman handle a ssigned to USB address.161 * Intentionally refuse to find handle ofdefault address.176 /*----------------------------------------------------------------------------*/ 177 /** Find devman handle and speed assigned to USB address. 178 * Intentionally refuse to work on default address. 162 179 * 163 180 * @param[in] instance Device manager structure to use. 164 181 * @param[in] address Address the caller wants to find. 165 182 * @param[out] handle Where to store found handle. 166 * @ return Whether such address is currently occupied.167 * /168 bool usb_device_manager_find_by_address(usb_device_manager_t *instance, 169 usb_address_t address, devman_handle_t *handle) 170 { 171 assert(instance); 172 fibril_mutex_lock(&instance->guard);183 * @param[out] speed Assigned speed. 184 * @return Error code. 185 */ 186 int usb_device_manager_get_info_by_address(usb_device_manager_t *instance, 187 usb_address_t address, devman_handle_t *handle, usb_speed_t *speed) 188 { 189 assert(instance); 173 190 if ((address <= 0) || (address >= USB_ADDRESS_COUNT)) { 174 fibril_mutex_unlock(&instance->guard); 175 return false; 176 } 191 return EINVAL; 192 } 193 194 fibril_mutex_lock(&instance->guard); 177 195 if (!instance->devices[address].occupied) { 178 196 fibril_mutex_unlock(&instance->guard); 179 return false;197 return ENOENT; 180 198 } 181 199 … … 183 201 *handle = instance->devices[address].handle; 184 202 } 185 186 fibril_mutex_unlock(&instance->guard); 187 return true; 188 } 189 190 /*----------------------------------------------------------------------------*/ 191 /** Get speed associated with the address 192 * 193 * @param[in] instance Device manager structure to use. 194 * @param[in] address Address of the device. 195 * @return USB speed. 196 */ 197 usb_speed_t usb_device_manager_get_speed( 198 usb_device_manager_t *instance, usb_address_t address) 199 { 200 assert(instance); 201 assert(address >= 0); 202 assert(address <= USB11_ADDRESS_MAX); 203 204 return instance->devices[address].speed; 203 if (speed != NULL) { 204 *speed = instance->devices[address].speed; 205 } 206 207 fibril_mutex_unlock(&instance->guard); 208 return EOK; 205 209 } 206 210 /**
Note:
See TracChangeset
for help on using the changeset viewer.