Changeset db96017 in mainline for uspace/lib/usbhost/src/iface.c
- Timestamp:
- 2012-04-07T17:41:44Z (14 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- b6913b7
- Parents:
- b69e4c0 (diff), 6bb169b5 (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
-
uspace/lib/usbhost/src/iface.c (modified) (15 diffs)
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/usbhost/src/iface.c
rb69e4c0 rdb96017 39 39 #include <usb/host/hcd.h> 40 40 41 /** Prepare generic usb_transfer_batch and schedule it. 42 * @param fun DDF fun 43 * @param target address and endpoint number. 44 * @param setup_data Data to use in setup stage (Control communication type) 45 * @param in Callback for device to host communication. 46 * @param out Callback for host to device communication. 47 * @param arg Callback parameter. 48 * @param name Communication identifier (for nicer output). 49 * @return Error code. 50 */ 41 51 static inline int send_batch( 42 52 ddf_fun_t *fun, usb_target_t target, usb_direction_t direction, … … 49 59 assert(hcd); 50 60 51 int ret; 52 53 size_t res_bw; 54 endpoint_t *ep = usb_endpoint_manager_get_ep(&hcd->ep_manager, 55 target.address, target.endpoint, direction, &res_bw); 61 endpoint_t *ep = usb_endpoint_manager_find_ep(&hcd->ep_manager, 62 target.address, target.endpoint, direction); 56 63 if (ep == NULL) { 57 64 usb_log_error("Endpoint(%d:%d) not registered for %s.\n", … … 65 72 const size_t bw = bandwidth_count_usb11( 66 73 ep->speed, ep->transfer_type, size, ep->max_packet_size); 67 if (res_bw < bw) { 74 /* Check if we have enough bandwidth reserved */ 75 if (ep->bandwidth < bw) { 68 76 usb_log_error("Endpoint(%d:%d) %s needs %zu bw " 69 77 "but only %zu is reserved.\n", 70 target.address, target.endpoint, name, bw, res_bw);78 ep->address, ep->endpoint, name, bw, ep->bandwidth); 71 79 return ENOSPC; 72 80 } … … 78 86 /* No private data and no private data dtor */ 79 87 usb_transfer_batch_t *batch = 80 usb_transfer_batch_ get(ep, data, size, setup_data,88 usb_transfer_batch_create(ep, data, size, setup_data, 81 89 in, out, arg, fun, NULL, NULL); 82 90 if (!batch) { … … 84 92 } 85 93 86 ret = hcd->schedule(hcd, batch);94 const int ret = hcd->schedule(hcd, batch); 87 95 if (ret != EOK) 88 usb_transfer_batch_d ispose(batch);96 usb_transfer_batch_destroy(batch); 89 97 90 98 return ret; 91 99 } 92 100 /*----------------------------------------------------------------------------*/ 93 /** Request address interface function 101 /** Calls ep_add_hook upon endpoint registration. 102 * @param ep Endpoint to be registered. 103 * @param arg hcd_t in disguise. 104 * @return Error code. 105 */ 106 static int register_helper(endpoint_t *ep, void *arg) 107 { 108 hcd_t *hcd = arg; 109 assert(ep); 110 assert(hcd); 111 if (hcd->ep_add_hook) 112 return hcd->ep_add_hook(hcd, ep); 113 return EOK; 114 } 115 /*----------------------------------------------------------------------------*/ 116 /** Calls ep_remove_hook upon endpoint removal. 117 * @param ep Endpoint to be unregistered. 118 * @param arg hcd_t in disguise. 119 */ 120 static void unregister_helper(endpoint_t *ep, void *arg) 121 { 122 hcd_t *hcd = arg; 123 assert(ep); 124 assert(hcd); 125 if (hcd->ep_remove_hook) 126 hcd->ep_remove_hook(hcd, ep); 127 } 128 /*----------------------------------------------------------------------------*/ 129 /** Calls ep_remove_hook upon endpoint removal. Prints warning. 130 * @param ep Endpoint to be unregistered. 131 * @param arg hcd_t in disguise. 132 */ 133 static void unregister_helper_warn(endpoint_t *ep, void *arg) 134 { 135 hcd_t *hcd = arg; 136 assert(ep); 137 assert(hcd); 138 usb_log_warning("Endpoint %d:%d %s was left behind, removing.\n", 139 ep->address, ep->endpoint, usb_str_direction(ep->direction)); 140 if (hcd->ep_remove_hook) 141 hcd->ep_remove_hook(hcd, ep); 142 } 143 /*----------------------------------------------------------------------------*/ 144 /** Request address interface function. 94 145 * 95 146 * @param[in] fun DDF function that was called. 147 * @param[in] address Pointer to preferred USB address. 148 * @param[out] address Place to write a new address. 149 * @param[in] strict Fail if the preferred address is not available. 96 150 * @param[in] speed Speed to associate with the new default address. 97 * @param[out] address Place to write a new address.98 151 * @return Error code. 99 152 */ 100 153 static int request_address( 101 ddf_fun_t *fun, usb_ speed_t speed, usb_address_t *address)154 ddf_fun_t *fun, usb_address_t *address, bool strict, usb_speed_t speed) 102 155 { 103 156 assert(fun); … … 106 159 assert(address); 107 160 108 usb_log_debug("Address request speed: %s.\n", usb_str_speed(speed)); 109 *address = 110 usb_device_manager_get_free_address(&hcd->dev_manager, speed); 111 usb_log_debug("Address request with result: %d.\n", *address); 112 if (*address <= 0) 113 return *address; 114 return EOK; 115 } 116 /*----------------------------------------------------------------------------*/ 117 /** Bind address interface function 161 usb_log_debug("Address request: speed: %s, address: %d, strict: %s.\n", 162 usb_str_speed(speed), *address, strict ? "YES" : "NO"); 163 return usb_device_manager_request_address( 164 &hcd->dev_manager, address, strict, speed); 165 } 166 /*----------------------------------------------------------------------------*/ 167 /** Bind address interface function. 118 168 * 119 169 * @param[in] fun DDF function that was called. … … 123 173 */ 124 174 static int bind_address( 125 ddf_fun_t *fun, usb_address_t address, devman_handle_t handle)175 ddf_fun_t *fun, usb_address_t address, devman_handle_t handle) 126 176 { 127 177 assert(fun); … … 130 180 131 181 usb_log_debug("Address bind %d-%" PRIun ".\n", address, handle); 132 usb_device_manager_bind(&hcd->dev_manager, address, handle);133 return EOK;182 return usb_device_manager_bind_address( 183 &hcd->dev_manager, address, handle); 134 184 } 135 185 /*----------------------------------------------------------------------------*/ … … 147 197 hcd_t *hcd = fun_to_hcd(fun); 148 198 assert(hcd); 149 const bool found = 150 usb_device_manager_find_by_address(&hcd->dev_manager, address, handle); 151 return found ? EOK : ENOENT; 152 } 153 /*----------------------------------------------------------------------------*/ 154 /** Release address interface function 199 return usb_device_manager_get_info_by_address( 200 &hcd->dev_manager, address, handle, NULL); 201 } 202 /*----------------------------------------------------------------------------*/ 203 /** Release address interface function. 155 204 * 156 205 * @param[in] fun DDF function that was called. … … 164 213 assert(hcd); 165 214 usb_log_debug("Address release %d.\n", address); 166 usb_device_manager_release(&hcd->dev_manager, address); 215 usb_device_manager_release_address(&hcd->dev_manager, address); 216 usb_endpoint_manager_remove_address(&hcd->ep_manager, address, 217 unregister_helper_warn, hcd); 167 218 return EOK; 168 219 } 169 220 /*----------------------------------------------------------------------------*/ 221 /** Register endpoint interface function. 222 * @param fun DDF function. 223 * @param address USB address of the device. 224 * @param endpoint USB endpoint number to be registered. 225 * @param transfer_type Endpoint's transfer type. 226 * @param direction USB communication direction the endpoint is capable of. 227 * @param max_packet_size Maximu size of packets the endpoint accepts. 228 * @param interval Preferred timeout between communication. 229 * @return Error code. 230 */ 170 231 static int register_endpoint( 171 ddf_fun_t *fun, usb_address_t address, usb_speed_t ep_speed, 172 usb_endpoint_t endpoint, 232 ddf_fun_t *fun, usb_address_t address, usb_endpoint_t endpoint, 173 233 usb_transfer_type_t transfer_type, usb_direction_t direction, 174 size_t max_packet_size, unsigned int interval)234 size_t max_packet_size, unsigned interval) 175 235 { 176 236 assert(fun); … … 178 238 assert(hcd); 179 239 const size_t size = max_packet_size; 180 /* Default address is not bound or registered, 181 * thus it does not provide speed info. */ 182 const usb_speed_t speed = (address == 0) ? ep_speed : 183 usb_device_manager_get_speed(&hcd->dev_manager, address); 240 usb_speed_t speed = USB_SPEED_MAX; 241 const int ret = usb_device_manager_get_info_by_address( 242 &hcd->dev_manager, address, NULL, &speed); 243 if (ret != EOK) { 244 return ret; 245 } 184 246 185 247 usb_log_debug("Register endpoint %d:%d %s-%s %s %zuB %ums.\n", … … 188 250 max_packet_size, interval); 189 251 190 endpoint_t *ep = endpoint_get( 191 address, endpoint, direction, transfer_type, speed, max_packet_size); 192 if (!ep) 193 return ENOMEM; 194 int ret = EOK; 195 196 if (hcd->ep_add_hook) { 197 ret = hcd->ep_add_hook(hcd, ep); 198 } 199 if (ret != EOK) { 200 endpoint_destroy(ep); 201 return ret; 202 } 203 204 ret = usb_endpoint_manager_register_ep(&hcd->ep_manager, ep, size); 205 if (ret != EOK) { 206 endpoint_destroy(ep); 207 } 208 return ret; 209 } 210 /*----------------------------------------------------------------------------*/ 252 return usb_endpoint_manager_add_ep(&hcd->ep_manager, address, endpoint, 253 direction, transfer_type, speed, max_packet_size, size, 254 register_helper, hcd); 255 } 256 /*----------------------------------------------------------------------------*/ 257 /** Unregister endpoint interface function. 258 * @param fun DDF function. 259 * @param address USB address of the endpoint. 260 * @param endpoint USB endpoint number. 261 * @param direction Communication direction of the enpdoint to unregister. 262 * @return Error code. 263 */ 211 264 static int unregister_endpoint( 212 265 ddf_fun_t *fun, usb_address_t address, … … 218 271 usb_log_debug("Unregister endpoint %d:%d %s.\n", 219 272 address, endpoint, usb_str_direction(direction)); 220 return usb_endpoint_manager_unregister_ep(&hcd->ep_manager, address, 221 endpoint, direction); 222 } 223 /*----------------------------------------------------------------------------*/ 273 return usb_endpoint_manager_remove_ep(&hcd->ep_manager, address, 274 endpoint, direction, unregister_helper, hcd); 275 } 276 /*----------------------------------------------------------------------------*/ 277 /** Inbound communication interface function. 278 * @param fun DDF function. 279 * @param target Communication target. 280 * @param setup_data Data to use in setup stage (control transfers). 281 * @param data Pointer to data buffer. 282 * @param size Size of the data buffer. 283 * @param callback Function to call on communication end. 284 * @param arg Argument passed to the callback function. 285 * @return Error code. 286 */ 224 287 static int usb_read(ddf_fun_t *fun, usb_target_t target, uint64_t setup_data, 225 288 uint8_t *data, size_t size, usbhc_iface_transfer_in_callback_t callback, … … 230 293 } 231 294 /*----------------------------------------------------------------------------*/ 295 /** Outbound communication interface function. 296 * @param fun DDF function. 297 * @param target Communication target. 298 * @param setup_data Data to use in setup stage (control transfers). 299 * @param data Pointer to data buffer. 300 * @param size Size of the data buffer. 301 * @param callback Function to call on communication end. 302 * @param arg Argument passed to the callback function. 303 * @return Error code. 304 */ 232 305 static int usb_write(ddf_fun_t *fun, usb_target_t target, uint64_t setup_data, 233 306 const uint8_t *data, size_t size, … … 238 311 } 239 312 /*----------------------------------------------------------------------------*/ 313 /** usbhc Interface implementation using hcd_t from libusbhost library. */ 240 314 usbhc_iface_t hcd_iface = { 241 315 .request_address = request_address, 242 316 .bind_address = bind_address, 243 . find_by_address= find_by_address,317 .get_handle = find_by_address, 244 318 .release_address = release_address, 245 319
Note:
See TracChangeset
for help on using the changeset viewer.
