Changeset c2245a3 in mainline for uspace/lib/usbdev/src/hub.c
- Timestamp:
- 2011-11-10T20:03:48Z (13 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- fa76f81
- Parents:
- 27ca3a3 (diff), 747ef72 (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/usbdev/src/hub.c
r27ca3a3 rc2245a3 66 66 * 67 67 * @param connection Opened connection to host controller. 68 * @param preferred Preferred SUB address. 69 * @param strict Fail if the preferred address is not avialable. 68 70 * @param speed Speed of the new device (device that will be assigned 69 71 * the returned address). … … 71 73 */ 72 74 usb_address_t usb_hc_request_address(usb_hc_connection_t *connection, 73 usb_ speed_t speed)75 usb_address_t preferred, bool strict, usb_speed_t speed) 74 76 { 75 77 CHECK_CONNECTION(connection); … … 78 80 79 81 sysarg_t address; 80 int rc = async_req_2_1(exch, DEV_IFACE_ID(USBHC_DEV_IFACE), 81 IPC_M_USBHC_REQUEST_ADDRESS, speed, 82 &address); 82 int rc = async_req_4_1(exch, DEV_IFACE_ID(USBHC_DEV_IFACE), 83 IPC_M_USBHC_REQUEST_ADDRESS, preferred, strict, speed, &address); 83 84 84 85 async_exchange_end(exch); … … 132 133 } 133 134 134 135 static void unregister_control_endpoint_on_default_address( 136 usb_hc_connection_t *connection) 135 /** Change address of connected device. 136 * This function automatically updates the backing connection to point to 137 * the new address. It also unregisterrs the old endpoint and registers 138 * a new one. 139 * This creates whole bunch of problems: 140 * 1. All pipes using this wire are broken because they are not 141 * registered for new address 142 * 2. All other pipes for this device are using wrong address, 143 * possibly targeting completely different device 144 * 145 * @param pipe Control endpoint pipe (session must be already started). 146 * @param new_address New USB address to be set (in native endianness). 147 * @return Error code. 148 */ 149 static int usb_request_set_address(usb_pipe_t *pipe, usb_address_t new_address, 150 usb_hc_connection_t *hc_conn) 137 151 { 138 usb_device_connection_t dev_conn; 139 int rc = usb_device_connection_initialize_on_default_address(&dev_conn, 140 connection); 141 if (rc != EOK) { 142 return; 143 } 144 145 usb_pipe_t ctrl_pipe; 146 rc = usb_pipe_initialize_default_control(&ctrl_pipe, &dev_conn); 147 if (rc != EOK) { 148 return; 149 } 150 151 usb_pipe_unregister(&ctrl_pipe, connection); 152 if ((new_address < 0) || (new_address >= USB11_ADDRESS_MAX)) { 153 return EINVAL; 154 } 155 assert(pipe); 156 assert(hc_conn); 157 assert(pipe->wire != NULL); 158 159 const uint16_t addr = uint16_host2usb((uint16_t)new_address); 160 161 int rc = usb_control_request_set(pipe, 162 USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE, 163 USB_DEVREQ_SET_ADDRESS, addr, 0, NULL, 0); 164 165 if (rc != EOK) { 166 return rc; 167 } 168 169 /* TODO: prevent others from accessing the wire now. */ 170 if (usb_pipe_unregister(pipe, hc_conn) != EOK) { 171 usb_log_warning( 172 "Failed to unregister the old pipe on address change.\n"); 173 } 174 /* The address is already changed so set it in the wire */ 175 pipe->wire->address = new_address; 176 rc = usb_pipe_register(pipe, 0, hc_conn); 177 if (rc != EOK) 178 return EADDRNOTAVAIL; 179 180 return EOK; 152 181 } 153 182 … … 171 200 * 172 201 * @param[in] parent Parent device (i.e. the hub device). 173 * @param[in] connection Connection to host controller. 202 * @param[in] connection Connection to host controller. Must be non-null. 174 203 * @param[in] dev_speed New device speed. 175 204 * @param[in] enable_port Function for enabling signaling through the port the … … 177 206 * @param[in] arg Any data argument to @p enable_port. 178 207 * @param[out] assigned_address USB address of the device. 179 * @param[in] dev_ops Child device ops. 208 * @param[in] dev_ops Child device ops. Will use default if not provided. 180 209 * @param[in] new_dev_data Arbitrary pointer to be stored in the child 181 * as @c driver_data. 210 * as @c driver_data. Will allocate and assign usb_hub_attached_device_t 211 * structure if NULL. 182 212 * @param[out] new_fun Storage where pointer to allocated child function 183 * will be written. 213 * will be written. Must be non-null. 184 214 * @return Error code. 215 * @retval EINVAL Either connection or new_fun is a NULL pointer. 185 216 * @retval ENOENT Connection to HC not opened. 186 217 * @retval EADDRNOTAVAIL Failed retrieving free address from host controller. … … 195 226 ddf_dev_ops_t *dev_ops, void *new_dev_data, ddf_fun_t **new_fun) 196 227 { 197 assert(connection != NULL); 228 if (new_fun == NULL || connection == NULL) 229 return EINVAL; 230 198 231 // FIXME: this is awful, we are accessing directly the structure. 232 // TODO: Why not use provided connection? 199 233 usb_hc_connection_t hc_conn = { 200 234 .hc_handle = connection->hc_handle, … … 215 249 } 216 250 217 218 251 /* 219 252 * Request new address. 220 253 */ 221 usb_address_t dev_addr = usb_hc_request_address(&hc_conn, dev_speed); 254 usb_address_t dev_addr = 255 usb_hc_request_address(&hc_conn, 0, false, dev_speed); 222 256 if (dev_addr < 0) { 223 257 rc = EADDRNOTAVAIL; … … 240 274 241 275 usb_pipe_t ctrl_pipe; 242 rc = usb_pipe_initialize_default_control(&ctrl_pipe, 243 &dev_conn); 276 rc = usb_pipe_initialize_default_control(&ctrl_pipe, &dev_conn); 244 277 if (rc != EOK) { 245 278 rc = ENOTCONN; … … 248 281 249 282 do { 250 rc = usb_ pipe_register_with_speed(&ctrl_pipe, dev_speed, 0,251 &hc_conn);252 if (rc != EOK) {283 rc = usb_hc_request_address(&hc_conn, USB_ADDRESS_DEFAULT, 284 true, dev_speed); 285 if (rc == ENOENT) { 253 286 /* Do not overheat the CPU ;-). */ 254 287 async_usleep(ENDPOINT_0_0_REGISTER_ATTEMPT_DELAY_USEC); 255 288 } 256 } while (rc != EOK); 289 } while (rc == ENOENT); 290 if (rc < 0) { 291 goto leave_release_free_address; 292 } 293 294 /* Register control pipe on default address. */ 295 rc = usb_pipe_register(&ctrl_pipe, 0, &hc_conn); 296 if (rc != EOK) { 297 rc = ENOTCONN; 298 goto leave_release_default_address; 299 } 300 257 301 struct timeval end_time; 258 302 … … 267 311 * above might use much of this time so we should only wait to fill 268 312 * up the 100ms quota*/ 269 suseconds_t elapsed = tv_sub(&end_time, &start_time);313 const suseconds_t elapsed = tv_sub(&end_time, &start_time); 270 314 if (elapsed < 100000) { 271 315 async_usleep(100000 - elapsed); 272 316 } 273 317 274 /* 275 * Endpoint is registered. We can enable the port and change 276 * device address. 277 */ 318 /* Endpoint is registered. We can enable the port and change address. */ 278 319 rc = enable_port(arg); 279 320 if (rc != EOK) { … … 287 328 async_usleep(10000); 288 329 330 /* Get max_packet_size value. */ 289 331 rc = usb_pipe_probe_default_control(&ctrl_pipe); 290 332 if (rc != EOK) { … … 293 335 } 294 336 295 rc = usb_request_set_address(&ctrl_pipe, dev_addr );337 rc = usb_request_set_address(&ctrl_pipe, dev_addr, &hc_conn); 296 338 if (rc != EOK) { 297 339 rc = ESTALL; … … 299 341 } 300 342 301 /* 302 * Address changed. We can release the original endpoint, thus 303 * allowing other to access the default address. 304 */ 305 unregister_control_endpoint_on_default_address(&hc_conn); 306 307 /* 308 * Time to register the new endpoint. 309 */ 310 rc = usb_pipe_register(&ctrl_pipe, 0, &hc_conn); 311 if (rc != EOK) { 312 goto leave_release_free_address; 313 } 314 315 /* 316 * It is time to register the device with devman. 317 */ 343 /* Address changed. We can release the default, thus 344 * allowing other to access the default address. */ 345 usb_hc_unregister_device(&hc_conn, USB_ADDRESS_DEFAULT); 346 347 /* Register the device with devman. */ 318 348 /* FIXME: create device_register that will get opened ctrl pipe. */ 319 349 ddf_fun_t *child_fun; 320 rc = usb_device_register_child_in_devman( dev_addr, dev_conn.hc_handle,350 rc = usb_device_register_child_in_devman(&ctrl_pipe, 321 351 parent, dev_ops, new_dev_data, &child_fun); 322 352 if (rc != EOK) { 323 rc = ESTALL;324 353 goto leave_release_free_address; 325 354 } 326 355 327 /* 328 * And now inform the host controller about the handle. 329 */ 330 usb_hub_attached_device_t new_device = { 356 const usb_hub_attached_device_t new_device = { 331 357 .address = dev_addr, 332 358 .fun = child_fun, 333 359 }; 360 361 362 /* Inform the host controller about the handle. */ 334 363 rc = usb_hc_register_device(&hc_conn, &new_device); 335 364 if (rc != EOK) { 365 /* We know nothing about that data. */ 366 if (new_dev_data) 367 child_fun->driver_data = NULL; 368 /* The child function is already created. */ 369 ddf_fun_destroy(child_fun); 336 370 rc = EDESTADDRREQ; 337 371 goto leave_release_free_address; 338 372 } 339 373 340 341 /*342 * And we are done.343 */344 374 if (assigned_address != NULL) { 345 375 *assigned_address = dev_addr; 346 376 } 347 if (new_fun != NULL) { 348 *new_fun = child_fun; 349 } 377 378 *new_fun = child_fun; 350 379 351 380 rc = EOK; … … 357 386 */ 358 387 leave_release_default_address: 359 usb_ pipe_unregister(&ctrl_pipe, &hc_conn);388 usb_hc_unregister_device(&hc_conn, USB_ADDRESS_DEFAULT); 360 389 361 390 leave_release_free_address: 362 usb_hc_unregister_device(&hc_conn, dev_addr); 391 /* This might be either 0:0 or dev_addr:0 */ 392 if (usb_pipe_unregister(&ctrl_pipe, &hc_conn) != EOK) 393 usb_log_warning("%s: Failed to unregister default pipe.\n", 394 __FUNCTION__); 395 396 if (usb_hc_unregister_device(&hc_conn, dev_addr) != EOK) 397 usb_log_warning("%s: Failed to unregister device.\n", 398 __FUNCTION__); 363 399 364 400 close_connection: 365 401 if (usb_hc_connection_close(&hc_conn) != EOK) 366 usb_log_warning(" usb_hc_new_device_wrapper(): Failed to close "367 "connection.\n");402 usb_log_warning("%s: Failed to close hc connection.\n", 403 __FUNCTION__); 368 404 369 405 return rc;
Note:
See TracChangeset
for help on using the changeset viewer.