Changes in uspace/lib/usbdev/src/hub.c [7711296:6e3c005] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/usbdev/src/hub.c
r7711296 r6e3c005 38 38 #include <usb/dev/recognise.h> 39 39 #include <usb/debug.h> 40 #include <usbhc_iface.h>41 40 #include <errno.h> 42 41 #include <assert.h> … … 45 44 #include <async.h> 46 45 47 /** How much time to wait between attempts to register endpoint 0:0.46 /** How much time to wait between attempts to get the default address. 48 47 * The value is based on typical value for port reset + some overhead. 49 48 */ 50 #define ENDPOINT_0_0_REGISTER_ATTEMPT_DELAY_USEC (1000 * (10 + 2)) 51 52 /** Check that HC connection is alright. 53 * 54 * @param conn Connection to be checked. 55 */ 56 #define CHECK_CONNECTION(conn) \ 57 do { \ 58 assert((conn)); \ 59 if (!usb_hc_connection_is_opened((conn))) { \ 60 usb_log_error("Connection not open.\n"); \ 61 return ENOTCONN; \ 62 } \ 63 } while (false) 64 65 /** Ask host controller for free address assignment. 66 * 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. 70 * @param speed Speed of the new device (device that will be assigned 71 * the returned address). 72 * @return Assigned USB address or negative error code. 73 */ 74 usb_address_t usb_hc_request_address(usb_hc_connection_t *connection, 75 usb_address_t preferred, bool strict, usb_speed_t speed) 76 { 77 CHECK_CONNECTION(connection); 78 79 async_exch_t *exch = async_exchange_begin(connection->hc_sess); 80 if (!exch) 81 return (usb_address_t)ENOMEM; 82 83 usb_address_t address = preferred; 84 const int ret = usbhc_request_address(exch, &address, strict, speed); 85 86 async_exchange_end(exch); 87 return ret == EOK ? address : ret; 88 } 49 #define DEFAULT_ADDRESS_ATTEMPT_DELAY_USEC (1000 * (10 + 2)) 89 50 90 51 /** Inform host controller about new device. … … 94 55 * @return Error code. 95 56 */ 96 int usb_h c_register_device(usb_hc_connection_t *connection,57 int usb_hub_register_device(usb_hc_connection_t *connection, 97 58 const usb_hub_attached_device_t *attached_device) 98 59 { 99 CHECK_CONNECTION(connection);60 assert(connection); 100 61 if (attached_device == NULL || attached_device->fun == NULL) 101 return EINVAL; 102 103 async_exch_t *exch = async_exchange_begin(connection->hc_sess); 104 if (!exch) 105 return ENOMEM; 106 const int ret = usbhc_bind_address(exch, 62 return EBADMEM; 63 return usb_hc_bind_address(connection, 107 64 attached_device->address, attached_device->fun->handle); 108 async_exchange_end(exch);109 110 return ret;111 }112 113 /** Inform host controller about device removal.114 *115 * @param connection Opened connection to host controller.116 * @param address Address of the device that is being removed.117 * @return Error code.118 */119 int usb_hc_unregister_device(usb_hc_connection_t *connection,120 usb_address_t address)121 {122 CHECK_CONNECTION(connection);123 124 async_exch_t *exch = async_exchange_begin(connection->hc_sess);125 if (!exch)126 return ENOMEM;127 const int ret = usbhc_release_address(exch, address);128 async_exchange_end(exch);129 130 return ret;131 65 } 132 66 … … 145 79 * @return Error code. 146 80 */ 147 static int usb_request_set_address(usb_pipe_t *pipe, usb_address_t new_address, 148 usb_hc_connection_t *hc_conn) 81 static int usb_request_set_address(usb_pipe_t *pipe, usb_address_t new_address) 149 82 { 150 83 if ((new_address < 0) || (new_address >= USB11_ADDRESS_MAX)) { … … 152 85 } 153 86 assert(pipe); 154 assert(hc_conn);155 87 assert(pipe->wire != NULL); 156 88 … … 166 98 167 99 /* TODO: prevent others from accessing the wire now. */ 168 if (usb_pipe_unregister(pipe , hc_conn) != EOK) {100 if (usb_pipe_unregister(pipe) != EOK) { 169 101 usb_log_warning( 170 102 "Failed to unregister the old pipe on address change.\n"); 171 103 } 104 /* Address changed. We can release the old one, thus 105 * allowing other to us it. */ 106 usb_hc_release_address(pipe->wire->hc_connection, pipe->wire->address); 107 172 108 /* The address is already changed so set it in the wire */ 173 109 pipe->wire->address = new_address; 174 rc = usb_pipe_register(pipe, 0 , hc_conn);110 rc = usb_pipe_register(pipe, 0); 175 111 if (rc != EOK) 176 112 return EADDRNOTAVAIL; … … 220 156 */ 221 157 int usb_hc_new_device_wrapper(ddf_dev_t *parent, 222 usb_hc_connection_t * connection, usb_speed_t dev_speed,158 usb_hc_connection_t *hc_conn, usb_speed_t dev_speed, 223 159 int (*enable_port)(void *arg), void *arg, usb_address_t *assigned_address, 224 160 ddf_dev_ops_t *dev_ops, void *new_dev_data, ddf_fun_t **new_fun) 225 161 { 226 if (new_fun == NULL || connection == NULL)162 if (new_fun == NULL || hc_conn == NULL) 227 163 return EINVAL; 228 229 // TODO: Why not use provided connection?230 usb_hc_connection_t hc_conn;231 usb_hc_connection_initialize(&hc_conn, connection->hc_handle);232 164 233 165 int rc; … … 239 171 } 240 172 241 rc = usb_hc_connection_open(&hc_conn); 173 /* We are gona do a lot of communication better open it in advance. */ 174 rc = usb_hc_connection_open(hc_conn); 242 175 if (rc != EOK) { 243 176 return rc; 244 177 } 245 178 246 /* 247 * Request new address. 248 */ 179 /* Request a new address. */ 249 180 usb_address_t dev_addr = 250 usb_hc_request_address( &hc_conn, 0, false, dev_speed);181 usb_hc_request_address(hc_conn, 0, false, dev_speed); 251 182 if (dev_addr < 0) { 252 183 rc = EADDRNOTAVAIL; … … 254 185 } 255 186 187 /* Initialize connection to device. */ 188 usb_device_connection_t dev_conn; 189 rc = usb_device_connection_initialize( 190 &dev_conn, hc_conn, USB_ADDRESS_DEFAULT); 191 if (rc != EOK) { 192 rc = ENOTCONN; 193 goto leave_release_free_address; 194 } 195 196 /* Initialize control pipe on default address. Don't register yet. */ 197 usb_pipe_t ctrl_pipe; 198 rc = usb_pipe_initialize_default_control(&ctrl_pipe, &dev_conn); 199 if (rc != EOK) { 200 rc = ENOTCONN; 201 goto leave_release_free_address; 202 } 203 256 204 /* 257 * We will not register control pipe on default address.258 * Th e registration might fail. That means that someone else already259 * registered that endpoint.We will simply wait and try again.205 * The default address request might fail. 206 * That means that someone else is already using that address. 207 * We will simply wait and try again. 260 208 * (Someone else already wants to add a new device.) 261 209 */ 262 usb_device_connection_t dev_conn;263 rc = usb_device_connection_initialize_on_default_address(&dev_conn,264 &hc_conn);265 if (rc != EOK) {266 rc = ENOTCONN;267 goto leave_release_free_address;268 }269 270 usb_pipe_t ctrl_pipe;271 rc = usb_pipe_initialize_default_control(&ctrl_pipe, &dev_conn);272 if (rc != EOK) {273 rc = ENOTCONN;274 goto leave_release_free_address;275 }276 277 210 do { 278 rc = usb_hc_request_address( &hc_conn, USB_ADDRESS_DEFAULT,211 rc = usb_hc_request_address(hc_conn, USB_ADDRESS_DEFAULT, 279 212 true, dev_speed); 280 213 if (rc == ENOENT) { 281 214 /* Do not overheat the CPU ;-). */ 282 async_usleep( ENDPOINT_0_0_REGISTER_ATTEMPT_DELAY_USEC);215 async_usleep(DEFAULT_ADDRESS_ATTEMPT_DELAY_USEC); 283 216 } 284 217 } while (rc == ENOENT); … … 287 220 } 288 221 289 /* Register control pipe on default address. */290 rc = usb_pipe_register(&ctrl_pipe, 0 , &hc_conn);222 /* Register control pipe on default address. 0 means no interval. */ 223 rc = usb_pipe_register(&ctrl_pipe, 0); 291 224 if (rc != EOK) { 292 225 rc = ENOTCONN; … … 295 228 296 229 struct timeval end_time; 297 298 230 rc = gettimeofday(&end_time, NULL); 299 231 if (rc != EOK) { … … 330 262 } 331 263 332 rc = usb_request_set_address(&ctrl_pipe, dev_addr , &hc_conn);264 rc = usb_request_set_address(&ctrl_pipe, dev_addr); 333 265 if (rc != EOK) { 334 266 rc = ESTALL; … … 336 268 } 337 269 338 /* Address changed. We can release the default, thus339 * allowing other to access the default address. */340 usb_hc_unregister_device(&hc_conn, USB_ADDRESS_DEFAULT);341 270 342 271 /* Register the device with devman. */ … … 356 285 357 286 /* Inform the host controller about the handle. */ 358 rc = usb_h c_register_device(&hc_conn, &new_device);287 rc = usb_hub_register_device(hc_conn, &new_device); 359 288 if (rc != EOK) { 360 289 /* We know nothing about that data. */ … … 381 310 */ 382 311 leave_release_default_address: 383 usb_hc_unregister_device(&hc_conn, USB_ADDRESS_DEFAULT); 312 if (usb_hc_release_address(hc_conn, USB_ADDRESS_DEFAULT) != EOK) 313 usb_log_warning("%s: Failed to release defaut address.\n", 314 __FUNCTION__); 384 315 385 316 leave_release_free_address: 386 317 /* This might be either 0:0 or dev_addr:0 */ 387 if (usb_pipe_unregister(&ctrl_pipe , &hc_conn) != EOK)318 if (usb_pipe_unregister(&ctrl_pipe) != EOK) 388 319 usb_log_warning("%s: Failed to unregister default pipe.\n", 389 320 __FUNCTION__); 390 321 391 if (usb_hc_ unregister_device(&hc_conn, dev_addr) != EOK)392 usb_log_warning("%s: Failed to unregister device.\n",393 __FUNCTION__ );322 if (usb_hc_release_address(hc_conn, dev_addr) != EOK) 323 usb_log_warning("%s: Failed to release address: %d.\n", 324 __FUNCTION__, dev_addr); 394 325 395 326 close_connection: 396 if (usb_hc_connection_close( &hc_conn) != EOK)327 if (usb_hc_connection_close(hc_conn) != EOK) 397 328 usb_log_warning("%s: Failed to close hc connection.\n", 398 329 __FUNCTION__);
Note:
See TracChangeset
for help on using the changeset viewer.