Print this page
5832 EOF wireless usb (aka UWB)
Reviewed by: TBD
Reviewed by: TBD
Approved by: TBD
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/common/io/usb/usba/hubdi.c
+++ new/usr/src/uts/common/io/usb/usba/hubdi.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /*
22 22 * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved.
23 23 * Copyright 2012 Garrett D'Amore <garrett@damore.org>. All rights reserved.
24 24 * Copyright 2014 Nexenta Systems, Inc. All rights reserved.
25 25 */
26 26
27 27 /*
28 28 * USBA: Solaris USB Architecture support for the hub
29 29 * including root hub
30 30 * Most of the code for hubd resides in this file and
31 31 * is shared between the HCD root hub support and hubd
32 32 */
33 33 #define USBA_FRAMEWORK
34 34 #include <sys/usb/usba.h>
35 35 #include <sys/usb/usba/usba_devdb.h>
36 36 #include <sys/sunndi.h>
37 37 #include <sys/usb/usba/usba_impl.h>
38 38 #include <sys/usb/usba/usba_types.h>
39 39 #include <sys/usb/usba/hubdi.h>
40 40 #include <sys/usb/usba/hcdi_impl.h>
41 41 #include <sys/usb/hubd/hub.h>
42 42 #include <sys/usb/hubd/hubdvar.h>
43 43 #include <sys/usb/hubd/hubd_impl.h>
44 44 #include <sys/kobj.h>
45 45 #include <sys/kobj_lex.h>
46 46 #include <sys/fs/dv_node.h>
47 47 #include <sys/strsun.h>
48 48
49 49 /*
50 50 * External functions
51 51 */
52 52 extern boolean_t consconfig_console_is_ready(void);
53 53
54 54 /*
55 55 * Prototypes for static functions
56 56 */
57 57 static int usba_hubdi_bus_ctl(
58 58 dev_info_t *dip,
59 59 dev_info_t *rdip,
60 60 ddi_ctl_enum_t op,
61 61 void *arg,
62 62 void *result);
63 63
64 64 static int usba_hubdi_map_fault(
65 65 dev_info_t *dip,
66 66 dev_info_t *rdip,
67 67 struct hat *hat,
68 68 struct seg *seg,
69 69 caddr_t addr,
70 70 struct devpage *dp,
71 71 pfn_t pfn,
72 72 uint_t prot,
73 73 uint_t lock);
74 74
75 75 static int hubd_busop_get_eventcookie(dev_info_t *dip,
76 76 dev_info_t *rdip,
77 77 char *eventname,
78 78 ddi_eventcookie_t *cookie);
79 79 static int hubd_busop_add_eventcall(dev_info_t *dip,
80 80 dev_info_t *rdip,
81 81 ddi_eventcookie_t cookie,
82 82 void (*callback)(dev_info_t *dip,
83 83 ddi_eventcookie_t cookie, void *arg,
84 84 void *bus_impldata),
85 85 void *arg, ddi_callback_id_t *cb_id);
86 86 static int hubd_busop_remove_eventcall(dev_info_t *dip,
87 87 ddi_callback_id_t cb_id);
88 88 static int hubd_bus_config(dev_info_t *dip,
89 89 uint_t flag,
90 90 ddi_bus_config_op_t op,
91 91 void *arg,
92 92 dev_info_t **child);
93 93 static int hubd_bus_unconfig(dev_info_t *dip,
94 94 uint_t flag,
95 95 ddi_bus_config_op_t op,
96 96 void *arg);
97 97 static int hubd_bus_power(dev_info_t *dip, void *impl_arg,
98 98 pm_bus_power_op_t op, void *arg, void *result);
99 99
100 100 static usb_port_t hubd_get_port_num(hubd_t *, struct devctl_iocdata *);
101 101 static dev_info_t *hubd_get_child_dip(hubd_t *, usb_port_t);
102 102 static uint_t hubd_cfgadm_state(hubd_t *, usb_port_t);
103 103 static int hubd_toggle_port(hubd_t *, usb_port_t);
104 104 static void hubd_register_cpr_callback(hubd_t *);
105 105 static void hubd_unregister_cpr_callback(hubd_t *);
106 106
107 107 /*
108 108 * Busops vector for USB HUB's
109 109 */
110 110 struct bus_ops usba_hubdi_busops = {
111 111 BUSO_REV,
112 112 nullbusmap, /* bus_map */
113 113 NULL, /* bus_get_intrspec */
114 114 NULL, /* bus_add_intrspec */
115 115 NULL, /* bus_remove_intrspec */
116 116 usba_hubdi_map_fault, /* bus_map_fault */
117 117 NULL, /* bus_dma_map */
118 118 ddi_dma_allochdl,
119 119 ddi_dma_freehdl,
120 120 ddi_dma_bindhdl,
121 121 ddi_dma_unbindhdl,
122 122 ddi_dma_flush,
123 123 ddi_dma_win,
124 124 ddi_dma_mctl, /* bus_dma_ctl */
125 125 usba_hubdi_bus_ctl, /* bus_ctl */
126 126 ddi_bus_prop_op, /* bus_prop_op */
127 127 hubd_busop_get_eventcookie,
128 128 hubd_busop_add_eventcall,
129 129 hubd_busop_remove_eventcall,
130 130 NULL, /* bus_post_event */
131 131 NULL, /* bus_intr_ctl */
132 132 hubd_bus_config, /* bus_config */
133 133 hubd_bus_unconfig, /* bus_unconfig */
134 134 NULL, /* bus_fm_init */
135 135 NULL, /* bus_fm_fini */
136 136 NULL, /* bus_fm_access_enter */
137 137 NULL, /* bus_fm_access_exit */
138 138 hubd_bus_power /* bus_power */
139 139 };
140 140
141 141 #define USB_HUB_INTEL_VID 0x8087
142 142 #define USB_HUB_INTEL_PID 0x0020
143 143
144 144 /*
145 145 * local variables
146 146 */
147 147 static kmutex_t usba_hubdi_mutex; /* protects USBA HUB data structures */
148 148
149 149 static usba_list_entry_t usba_hubdi_list;
150 150
151 151 usb_log_handle_t hubdi_log_handle;
152 152 uint_t hubdi_errlevel = USB_LOG_L4;
153 153 uint_t hubdi_errmask = (uint_t)-1;
154 154 uint8_t hubdi_min_pm_threshold = 5; /* seconds */
155 155 uint8_t hubdi_reset_delay = 20; /* seconds */
156 156 extern int modrootloaded;
157 157
158 158 /*
159 159 * initialize private data
160 160 */
161 161 void
162 162 usba_hubdi_initialization()
163 163 {
164 164 hubdi_log_handle = usb_alloc_log_hdl(NULL, "hubdi", &hubdi_errlevel,
165 165 &hubdi_errmask, NULL, 0);
166 166
167 167 USB_DPRINTF_L4(DPRINT_MASK_HUBDI, hubdi_log_handle,
168 168 "usba_hubdi_initialization");
169 169
170 170 mutex_init(&usba_hubdi_mutex, NULL, MUTEX_DRIVER, NULL);
171 171
172 172 usba_init_list(&usba_hubdi_list, NULL, NULL);
173 173 }
174 174
175 175
176 176 void
177 177 usba_hubdi_destroy()
178 178 {
179 179 USB_DPRINTF_L4(DPRINT_MASK_HUBDI, hubdi_log_handle,
180 180 "usba_hubdi_destroy");
181 181
182 182 mutex_destroy(&usba_hubdi_mutex);
183 183 usba_destroy_list(&usba_hubdi_list);
184 184
185 185 usb_free_log_hdl(hubdi_log_handle);
186 186 }
187 187
188 188
189 189 /*
190 190 * Called by an HUB to attach an instance of the driver
191 191 * make this instance known to USBA
192 192 * the HUB should initialize usba_hubdi structure prior
193 193 * to calling this interface
194 194 */
195 195 int
196 196 usba_hubdi_register(dev_info_t *dip,
197 197 uint_t flags)
198 198 {
199 199 usba_hubdi_t *hubdi = kmem_zalloc(sizeof (usba_hubdi_t), KM_SLEEP);
200 200 usba_device_t *usba_device = usba_get_usba_device(dip);
201 201
202 202 USB_DPRINTF_L4(DPRINT_MASK_HUBDI, hubdi_log_handle,
203 203 "usba_hubdi_register: %s", ddi_node_name(dip));
204 204
205 205 hubdi->hubdi_dip = dip;
206 206 hubdi->hubdi_flags = flags;
207 207
208 208 usba_device->usb_hubdi = hubdi;
209 209
210 210 /*
211 211 * add this hubdi instance to the list of known hubdi's
212 212 */
213 213 usba_init_list(&hubdi->hubdi_list, (usb_opaque_t)hubdi,
214 214 usba_hcdi_get_hcdi(usba_device->usb_root_hub_dip)->
215 215 hcdi_iblock_cookie);
216 216 mutex_enter(&usba_hubdi_mutex);
217 217 usba_add_to_list(&usba_hubdi_list, &hubdi->hubdi_list);
218 218 mutex_exit(&usba_hubdi_mutex);
219 219
220 220 return (DDI_SUCCESS);
221 221 }
222 222
223 223
224 224 /*
225 225 * Called by an HUB to detach an instance of the driver
226 226 */
227 227 int
228 228 usba_hubdi_unregister(dev_info_t *dip)
229 229 {
230 230 usba_device_t *usba_device = usba_get_usba_device(dip);
231 231 usba_hubdi_t *hubdi = usba_device->usb_hubdi;
232 232
233 233 USB_DPRINTF_L4(DPRINT_MASK_HUBDI, hubdi_log_handle,
234 234 "usba_hubdi_unregister: %s", ddi_node_name(dip));
235 235
236 236 mutex_enter(&usba_hubdi_mutex);
237 237 (void) usba_rm_from_list(&usba_hubdi_list, &hubdi->hubdi_list);
238 238 mutex_exit(&usba_hubdi_mutex);
239 239
240 240 usba_destroy_list(&hubdi->hubdi_list);
241 241
242 242 kmem_free(hubdi, sizeof (usba_hubdi_t));
243 243
244 244 return (DDI_SUCCESS);
245 245 }
246 246
247 247
248 248 /*
249 249 * misc bus routines currently not used
250 250 */
251 251 /*ARGSUSED*/
252 252 static int
253 253 usba_hubdi_map_fault(dev_info_t *dip,
254 254 dev_info_t *rdip,
255 255 struct hat *hat,
256 256 struct seg *seg,
257 257 caddr_t addr,
258 258 struct devpage *dp,
259 259 pfn_t pfn,
260 260 uint_t prot,
261 261 uint_t lock)
262 262 {
263 263 return (DDI_FAILURE);
264 264 }
265 265
266 266
267 267 /*
268 268 * root hub support. the root hub uses the same devi as the HCD
269 269 */
270 270 int
271 271 usba_hubdi_bind_root_hub(dev_info_t *dip,
272 272 uchar_t *root_hub_config_descriptor,
273 273 size_t config_length,
274 274 usb_dev_descr_t *root_hub_device_descriptor)
275 275 {
276 276 usba_device_t *usba_device;
277 277 usba_hcdi_t *hcdi = usba_hcdi_get_hcdi(dip);
278 278 hubd_t *root_hubd;
279 279 usb_pipe_handle_t ph = NULL;
280 280 dev_info_t *child = ddi_get_child(dip);
281 281
282 282 if (ndi_prop_create_boolean(DDI_DEV_T_NONE, dip,
283 283 "root-hub") != NDI_SUCCESS) {
284 284
285 285 return (USB_FAILURE);
286 286 }
287 287
288 288 usba_add_root_hub(dip);
289 289
290 290 root_hubd = kmem_zalloc(sizeof (hubd_t), KM_SLEEP);
291 291
292 292 /*
293 293 * create and initialize a usba_device structure
294 294 */
295 295 usba_device = usba_alloc_usba_device(dip);
296 296
297 297 mutex_enter(&usba_device->usb_mutex);
298 298 usba_device->usb_hcdi_ops = hcdi->hcdi_ops;
299 299 usba_device->usb_cfg = root_hub_config_descriptor;
300 300 usba_device->usb_cfg_length = config_length;
301 301 usba_device->usb_dev_descr = root_hub_device_descriptor;
302 302 usba_device->usb_port = 1;
303 303 usba_device->usb_addr = ROOT_HUB_ADDR;
304 304 usba_device->usb_root_hubd = root_hubd;
305 305 usba_device->usb_cfg_array = kmem_zalloc(sizeof (uchar_t *),
306 306 KM_SLEEP);
307 307 usba_device->usb_cfg_array_length = sizeof (uchar_t *);
308 308
309 309 usba_device->usb_cfg_array_len = kmem_zalloc(sizeof (uint16_t),
310 310 KM_SLEEP);
311 311 usba_device->usb_cfg_array_len_length = sizeof (uint16_t);
312 312
313 313 usba_device->usb_cfg_array[0] = root_hub_config_descriptor;
314 314 usba_device->usb_cfg_array_len[0] =
315 315 sizeof (root_hub_config_descriptor);
316 316
317 317 usba_device->usb_cfg_str_descr = kmem_zalloc(sizeof (uchar_t *),
318 318 KM_SLEEP);
319 319 usba_device->usb_n_cfgs = 1;
320 320 usba_device->usb_n_ifs = 1;
321 321 usba_device->usb_dip = dip;
322 322
323 323 usba_device->usb_client_flags = kmem_zalloc(
324 324 usba_device->usb_n_ifs * USBA_CLIENT_FLAG_SIZE, KM_SLEEP);
325 325
326 326 usba_device->usb_client_attach_list = kmem_zalloc(
327 327 usba_device->usb_n_ifs *
328 328 sizeof (*usba_device->usb_client_attach_list), KM_SLEEP);
329 329
330 330 usba_device->usb_client_ev_cb_list = kmem_zalloc(
331 331 usba_device->usb_n_ifs *
332 332 sizeof (*usba_device->usb_client_ev_cb_list), KM_SLEEP);
333 333
334 334 /*
335 335 * The bDeviceProtocol field of root hub device specifies,
336 336 * whether root hub is a High or Full speed usb device.
337 337 */
338 338 if (root_hub_device_descriptor->bDeviceProtocol) {
339 339 usba_device->usb_port_status = USBA_HIGH_SPEED_DEV;
340 340 } else {
341 341 usba_device->usb_port_status = USBA_FULL_SPEED_DEV;
342 342 }
343 343
344 344 mutex_exit(&usba_device->usb_mutex);
345 345
346 346 usba_set_usba_device(dip, usba_device);
347 347
348 348 /*
349 349 * For the root hub the default pipe is not yet open
350 350 */
351 351 if (usb_pipe_open(dip, NULL, NULL,
352 352 USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, &ph) != USB_SUCCESS) {
353 353 goto fail;
354 354 }
355 355
356 356 /*
357 357 * kill off all OBP children, they may not be fully
358 358 * enumerated
359 359 */
360 360 while (child) {
361 361 dev_info_t *next = ddi_get_next_sibling(child);
362 362 (void) ddi_remove_child(child, 0);
363 363 child = next;
364 364 }
365 365
366 366 /*
367 367 * "attach" the root hub driver
368 368 */
369 369 if (usba_hubdi_attach(dip, DDI_ATTACH) != DDI_SUCCESS) {
370 370 goto fail;
371 371 }
372 372
373 373 return (USB_SUCCESS);
374 374
375 375 fail:
376 376 (void) ndi_prop_remove(DDI_DEV_T_NONE, dip, "root-hub");
377 377
378 378 usba_rem_root_hub(dip);
379 379
380 380 if (ph) {
381 381 usb_pipe_close(dip, ph,
382 382 USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, NULL, NULL);
383 383 }
384 384
385 385 kmem_free(usba_device->usb_cfg_array,
386 386 usba_device->usb_cfg_array_length);
387 387 kmem_free(usba_device->usb_cfg_array_len,
388 388 usba_device->usb_cfg_array_len_length);
389 389
390 390 kmem_free(usba_device->usb_cfg_str_descr, sizeof (uchar_t *));
391 391
392 392 usba_free_usba_device(usba_device);
393 393
394 394 usba_set_usba_device(dip, NULL);
395 395 if (root_hubd) {
396 396 kmem_free(root_hubd, sizeof (hubd_t));
397 397 }
398 398
399 399 return (USB_FAILURE);
400 400 }
401 401
402 402
403 403 int
404 404 usba_hubdi_unbind_root_hub(dev_info_t *dip)
405 405 {
406 406 usba_device_t *usba_device;
407 407
408 408 /* was root hub attached? */
409 409 if (!(usba_is_root_hub(dip))) {
410 410
411 411 /* return success anyway */
412 412 return (USB_SUCCESS);
413 413 }
414 414
415 415 /*
416 416 * usba_hubdi_detach also closes the default pipe
417 417 * and removes properties so there is no need to
418 418 * do it here
419 419 */
420 420 if (usba_hubdi_detach(dip, DDI_DETACH) != DDI_SUCCESS) {
421 421
422 422 if (DEVI_IS_ATTACHING(dip)) {
423 423 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubdi_log_handle,
424 424 "failure to unbind root hub after attach failure");
425 425 }
426 426
427 427 return (USB_FAILURE);
428 428 }
429 429
430 430 usba_device = usba_get_usba_device(dip);
431 431
432 432 kmem_free(usba_device->usb_root_hubd, sizeof (hubd_t));
433 433
434 434 kmem_free(usba_device->usb_cfg_array,
435 435 usba_device->usb_cfg_array_length);
436 436 kmem_free(usba_device->usb_cfg_array_len,
437 437 usba_device->usb_cfg_array_len_length);
438 438
439 439 kmem_free(usba_device->usb_cfg_str_descr, sizeof (uchar_t *));
440 440
441 441 usba_free_usba_device(usba_device);
442 442
443 443 usba_rem_root_hub(dip);
444 444
445 445 (void) ndi_prop_remove(DDI_DEV_T_NONE, dip, "root-hub");
446 446
447 447 return (USB_SUCCESS);
448 448 }
449 449
450 450
451 451 /*
452 452 * Actual Hub Driver support code:
453 453 * shared by root hub and non-root hubs
454 454 */
455 455 #include <sys/usb/usba/usbai_version.h>
456 456
457 457 /* Debugging support */
458 458 uint_t hubd_errlevel = USB_LOG_L4;
459 459 uint_t hubd_errmask = (uint_t)DPRINT_MASK_ALL;
460 460 uint_t hubd_instance_debug = (uint_t)-1;
461 461 static uint_t hubdi_bus_config_debug = 0;
462 462
463 463 _NOTE(DATA_READABLE_WITHOUT_LOCK(hubd_errlevel))
464 464 _NOTE(DATA_READABLE_WITHOUT_LOCK(hubd_errmask))
465 465 _NOTE(DATA_READABLE_WITHOUT_LOCK(hubd_instance_debug))
466 466
467 467 _NOTE(SCHEME_PROTECTS_DATA("unique", msgb))
468 468 _NOTE(SCHEME_PROTECTS_DATA("unique", dev_info))
469 469
470 470
471 471 /*
472 472 * local variables:
473 473 *
474 474 * Amount of time to wait between resetting the port and accessing
475 475 * the device. The value is in microseconds.
476 476 */
477 477 static uint_t hubd_device_delay = 1000000;
478 478
479 479 /*
480 480 * enumeration retry
481 481 */
482 482 #define HUBD_PORT_RETRY 5
483 483 static uint_t hubd_retry_enumerate = HUBD_PORT_RETRY;
484 484
485 485 /*
486 486 * Stale hotremoved device cleanup delay
487 487 */
488 488 #define HUBD_STALE_DIP_CLEANUP_DELAY 5000000
489 489 static uint_t hubd_dip_cleanup_delay = HUBD_STALE_DIP_CLEANUP_DELAY;
490 490
491 491 /*
492 492 * retries for USB suspend and resume
493 493 */
494 494 #define HUBD_SUS_RES_RETRY 2
495 495
496 496 void *hubd_statep;
497 497
498 498 /*
499 499 * prototypes
500 500 */
501 501 static int hubd_cleanup(dev_info_t *dip, hubd_t *hubd);
502 502 static int hubd_check_ports(hubd_t *hubd);
503 503
504 504 static int hubd_open_intr_pipe(hubd_t *hubd);
505 505 static void hubd_start_polling(hubd_t *hubd, int always);
506 506 static void hubd_stop_polling(hubd_t *hubd);
507 507 static void hubd_close_intr_pipe(hubd_t *hubd);
508 508
509 509 static void hubd_read_cb(usb_pipe_handle_t pipe, usb_intr_req_t *req);
510 510 static void hubd_exception_cb(usb_pipe_handle_t pipe,
511 511 usb_intr_req_t *req);
512 512 static void hubd_hotplug_thread(void *arg);
513 513 static void hubd_reset_thread(void *arg);
514 514 static int hubd_create_child(dev_info_t *dip,
515 515 hubd_t *hubd,
516 516 usba_device_t *usba_device,
517 517 usb_port_status_t port_status,
518 518 usb_port_t port,
519 519 int iteration);
520 520
521 521 static int hubd_delete_child(hubd_t *hubd, usb_port_t port, uint_t flag,
522 522 boolean_t retry);
523 523
524 524 static int hubd_get_hub_descriptor(hubd_t *hubd);
525 525
526 526 static int hubd_get_hub_status_words(hubd_t *hubd, uint16_t *status);
527 527
528 528 static int hubd_reset_port(hubd_t *hubd, usb_port_t port);
529 529
530 530 static int hubd_get_hub_status(hubd_t *hubd);
531 531
532 532 static int hubd_handle_port_connect(hubd_t *hubd, usb_port_t port);
533 533
534 534 static int hubd_disable_port(hubd_t *hubd, usb_port_t port);
535 535
536 536 static int hubd_enable_port(hubd_t *hubd, usb_port_t port);
537 537 static int hubd_recover_disabled_port(hubd_t *hubd, usb_port_t port);
538 538
539 539 static int hubd_determine_port_status(hubd_t *hubd, usb_port_t port,
540 540 uint16_t *status, uint16_t *change, uint_t ack_flag);
541 541
542 542 static int hubd_enable_all_port_power(hubd_t *hubd);
543 543 static int hubd_disable_all_port_power(hubd_t *hubd);
544 544 static int hubd_disable_port_power(hubd_t *hubd, usb_port_t port);
545 545 static int hubd_enable_port_power(hubd_t *hubd, usb_port_t port);
546 546
547 547 static void hubd_free_usba_device(hubd_t *hubd, usba_device_t *usba_device);
548 548
549 549 static int hubd_can_suspend(hubd_t *hubd);
550 550 static void hubd_restore_device_state(dev_info_t *dip, hubd_t *hubd);
551 551 static int hubd_setdevaddr(hubd_t *hubd, usb_port_t port);
552 552 static void hubd_setdevconfig(hubd_t *hubd, usb_port_t port);
553 553
554 554 static int hubd_register_events(hubd_t *hubd);
555 555 static void hubd_do_callback(hubd_t *hubd, dev_info_t *dip,
556 556 ddi_eventcookie_t cookie);
557 557 static void hubd_run_callbacks(hubd_t *hubd, usba_event_t type);
558 558 static void hubd_post_event(hubd_t *hubd, usb_port_t port, usba_event_t type);
559 559 static void hubd_create_pm_components(dev_info_t *dip, hubd_t *hubd);
560 560
561 561 static int hubd_disconnect_event_cb(dev_info_t *dip);
562 562 static int hubd_reconnect_event_cb(dev_info_t *dip);
563 563 static int hubd_pre_suspend_event_cb(dev_info_t *dip);
564 564 static int hubd_post_resume_event_cb(dev_info_t *dip);
565 565 static int hubd_cpr_suspend(hubd_t *hubd);
566 566 static void hubd_cpr_resume(dev_info_t *dip);
567 567 static int hubd_restore_state_cb(dev_info_t *dip);
568 568 static int hubd_check_same_device(hubd_t *hubd, usb_port_t port);
569 569
570 570 static int hubd_init_power_budget(hubd_t *hubd);
571 571
572 572 static ndi_event_definition_t hubd_ndi_event_defs[] = {
573 573 {USBA_EVENT_TAG_HOT_REMOVAL, DDI_DEVI_REMOVE_EVENT, EPL_KERNEL,
574 574 NDI_EVENT_POST_TO_ALL},
575 575 {USBA_EVENT_TAG_HOT_INSERTION, DDI_DEVI_INSERT_EVENT, EPL_KERNEL,
576 576 NDI_EVENT_POST_TO_ALL},
577 577 {USBA_EVENT_TAG_POST_RESUME, USBA_POST_RESUME_EVENT, EPL_KERNEL,
578 578 NDI_EVENT_POST_TO_ALL},
579 579 {USBA_EVENT_TAG_PRE_SUSPEND, USBA_PRE_SUSPEND_EVENT, EPL_KERNEL,
580 580 NDI_EVENT_POST_TO_ALL}
581 581 };
582 582
583 583 #define HUBD_N_NDI_EVENTS \
584 584 (sizeof (hubd_ndi_event_defs) / sizeof (ndi_event_definition_t))
585 585
586 586 static ndi_event_set_t hubd_ndi_events = {
587 587 NDI_EVENTS_REV1, HUBD_N_NDI_EVENTS, hubd_ndi_event_defs};
588 588
589 589 /* events received from parent */
↓ open down ↓ |
589 lines elided |
↑ open up ↑ |
590 590 static usb_event_t hubd_events = {
591 591 hubd_disconnect_event_cb,
592 592 hubd_reconnect_event_cb,
593 593 hubd_pre_suspend_event_cb,
594 594 hubd_post_resume_event_cb
595 595 };
596 596
597 597
598 598 /*
599 599 * hubd_get_soft_state() returns the hubd soft state
600 - *
601 - * WUSB support extends this function to support wire adapter class
602 - * devices. The hubd soft state for the wire adapter class device
603 - * would be stored in usb_root_hubd field of the usba_device structure,
604 - * just as the USB host controller drivers do.
605 600 */
606 601 hubd_t *
607 602 hubd_get_soft_state(dev_info_t *dip)
608 603 {
609 604 if (dip == NULL) {
610 605
611 606 return (NULL);
612 607 }
613 608
614 - if (usba_is_root_hub(dip) || usba_is_wa(dip)) {
609 + if (usba_is_root_hub(dip)) {
615 610 usba_device_t *usba_device = usba_get_usba_device(dip);
616 611
617 612 return (usba_device->usb_root_hubd);
618 613 } else {
619 614 int instance = ddi_get_instance(dip);
620 615
621 616 return (ddi_get_soft_state(hubd_statep, instance));
622 617 }
623 618 }
624 619
625 620
626 621 /*
627 622 * PM support functions:
628 623 */
629 624 /*ARGSUSED*/
630 625 static void
631 626 hubd_pm_busy_component(hubd_t *hubd, dev_info_t *dip, int component)
632 627 {
633 628 if (hubd->h_hubpm != NULL) {
634 629 hubd->h_hubpm->hubp_busy_pm++;
635 630 mutex_exit(HUBD_MUTEX(hubd));
636 631 if (pm_busy_component(dip, 0) != DDI_SUCCESS) {
637 632 mutex_enter(HUBD_MUTEX(hubd));
638 633 hubd->h_hubpm->hubp_busy_pm--;
639 634 mutex_exit(HUBD_MUTEX(hubd));
640 635 }
641 636 mutex_enter(HUBD_MUTEX(hubd));
642 637 USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
643 638 "hubd_pm_busy_component: %d", hubd->h_hubpm->hubp_busy_pm);
644 639 }
645 640 }
646 641
647 642
648 643 /*ARGSUSED*/
649 644 static void
650 645 hubd_pm_idle_component(hubd_t *hubd, dev_info_t *dip, int component)
651 646 {
652 647 if (hubd->h_hubpm != NULL) {
653 648 mutex_exit(HUBD_MUTEX(hubd));
654 649 if (pm_idle_component(dip, 0) == DDI_SUCCESS) {
655 650 mutex_enter(HUBD_MUTEX(hubd));
656 651 ASSERT(hubd->h_hubpm->hubp_busy_pm > 0);
657 652 hubd->h_hubpm->hubp_busy_pm--;
658 653 mutex_exit(HUBD_MUTEX(hubd));
659 654 }
660 655 mutex_enter(HUBD_MUTEX(hubd));
661 656 USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
662 657 "hubd_pm_idle_component: %d", hubd->h_hubpm->hubp_busy_pm);
663 658 }
664 659 }
665 660
666 661
667 662 /*
668 663 * track power level changes for children of this instance
669 664 */
670 665 static void
671 666 hubd_set_child_pwrlvl(hubd_t *hubd, usb_port_t port, uint8_t power)
672 667 {
673 668 int old_power, new_power, pwr;
674 669 usb_port_t portno;
675 670 hub_power_t *hubpm;
676 671
677 672 USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
678 673 "hubd_set_child_pwrlvl: port=%d power=%d",
679 674 port, power);
680 675
681 676 mutex_enter(HUBD_MUTEX(hubd));
682 677 hubpm = hubd->h_hubpm;
683 678
684 679 old_power = 0;
685 680 for (portno = 1; portno <= hubd->h_hub_descr.bNbrPorts; portno++) {
686 681 old_power += hubpm->hubp_child_pwrstate[portno];
687 682 }
688 683
689 684 /* assign the port power */
690 685 pwr = hubd->h_hubpm->hubp_child_pwrstate[port];
691 686 hubd->h_hubpm->hubp_child_pwrstate[port] = power;
692 687 new_power = old_power - pwr + power;
693 688
694 689 USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
695 690 "hubd_set_child_pwrlvl: new_power=%d old_power=%d",
696 691 new_power, old_power);
697 692
698 693 if ((new_power > 0) && (old_power == 0)) {
699 694 /* we have the first child coming out of low power */
700 695 (void) hubd_pm_busy_component(hubd, hubd->h_dip, 0);
701 696 } else if ((new_power == 0) && (old_power > 0)) {
702 697 /* we have the last child going to low power */
703 698 (void) hubd_pm_idle_component(hubd, hubd->h_dip, 0);
704 699 }
705 700 mutex_exit(HUBD_MUTEX(hubd));
706 701 }
707 702
708 703
709 704 /*
710 705 * given a child dip, locate its port number
711 706 */
712 707 static usb_port_t
713 708 hubd_child_dip2port(hubd_t *hubd, dev_info_t *dip)
714 709 {
715 710 usb_port_t port;
716 711
717 712 mutex_enter(HUBD_MUTEX(hubd));
718 713 for (port = 1; port <= hubd->h_hub_descr.bNbrPorts; port++) {
719 714 if (hubd->h_children_dips[port] == dip) {
720 715
721 716 break;
722 717 }
723 718 }
724 719 ASSERT(port <= hubd->h_hub_descr.bNbrPorts);
725 720 mutex_exit(HUBD_MUTEX(hubd));
726 721
727 722 return (port);
728 723 }
729 724
730 725
731 726 /*
732 727 * if the hub can be put into low power mode, return success
733 728 * NOTE: suspend here means going to lower power, not CPR suspend.
734 729 */
735 730 static int
736 731 hubd_can_suspend(hubd_t *hubd)
737 732 {
738 733 hub_power_t *hubpm;
739 734 int total_power = 0;
740 735 usb_port_t port;
741 736
742 737 hubpm = hubd->h_hubpm;
743 738
744 739 if (DEVI_IS_DETACHING(hubd->h_dip)) {
745 740
746 741 return (USB_SUCCESS);
747 742 }
748 743
749 744 /*
750 745 * Don't go to lower power if haven't been at full power for enough
751 746 * time to let hotplug thread kickoff.
752 747 */
753 748 if (gethrtime() < (hubpm->hubp_time_at_full_power +
754 749 hubpm->hubp_min_pm_threshold)) {
755 750
756 751 return (USB_FAILURE);
757 752 }
758 753
759 754 for (port = 1; (total_power == 0) &&
760 755 (port <= hubd->h_hub_descr.bNbrPorts); port++) {
761 756 total_power += hubpm->hubp_child_pwrstate[port];
762 757 }
763 758
764 759 USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
765 760 "hubd_can_suspend: %d", total_power);
766 761
767 762 return (total_power ? USB_FAILURE : USB_SUCCESS);
768 763 }
769 764
770 765
771 766 /*
772 767 * resume port depending on current device state
773 768 */
774 769 static int
775 770 hubd_resume_port(hubd_t *hubd, usb_port_t port)
776 771 {
777 772 int rval, retry;
778 773 usb_cr_t completion_reason;
779 774 usb_cb_flags_t cb_flags;
780 775 uint16_t status;
781 776 uint16_t change;
782 777 int retval = USB_FAILURE;
783 778
784 779 mutex_enter(HUBD_MUTEX(hubd));
785 780
786 781 USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
787 782 "hubd_resume_port: port=%d state=0x%x (%s)", port,
788 783 hubd->h_dev_state, usb_str_dev_state(hubd->h_dev_state));
789 784
790 785 switch (hubd->h_dev_state) {
791 786 case USB_DEV_HUB_CHILD_PWRLVL:
792 787 /*
793 788 * This could be a bus ctl for a port other than the one
794 789 * that has a remote wakeup condition. So check.
795 790 */
796 791 if ((hubd->h_port_state[port] & PORT_STATUS_PSS) == 0) {
797 792 /* the port isn't suspended, so don't resume */
798 793 retval = USB_SUCCESS;
799 794
800 795 USB_DPRINTF_L2(DPRINT_MASK_PM, hubd->h_log_handle,
801 796 "hubd_resume_port: port=%d not suspended", port);
802 797
803 798 break;
804 799 }
805 800 /*
806 801 * Device has initiated a wakeup.
807 802 * Issue a ClearFeature(PortSuspend)
808 803 */
809 804 mutex_exit(HUBD_MUTEX(hubd));
810 805 if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
811 806 hubd->h_default_pipe,
812 807 HUB_HANDLE_PORT_FEATURE_TYPE,
813 808 USB_REQ_CLEAR_FEATURE,
814 809 CFS_PORT_SUSPEND,
815 810 port,
816 811 0, NULL, 0,
817 812 &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
818 813 USB_DPRINTF_L2(DPRINT_MASK_PM, hubd->h_log_handle,
819 814 "ClearFeature(PortSuspend) fails "
820 815 "rval=%d cr=%d cb=0x%x", rval,
821 816 completion_reason, cb_flags);
822 817 }
823 818 mutex_enter(HUBD_MUTEX(hubd));
824 819
825 820 /* either way ack changes on the port */
826 821 (void) hubd_determine_port_status(hubd, port,
827 822 &status, &change, PORT_CHANGE_PSSC);
828 823 retval = USB_SUCCESS;
829 824
830 825 break;
831 826 case USB_DEV_HUB_STATE_RECOVER:
832 827 /*
833 828 * When hubd's connect event callback posts a connect
834 829 * event to its child, it results in this busctl call
835 830 * which is valid
836 831 */
837 832 /* FALLTHRU */
838 833 case USB_DEV_ONLINE:
839 834 if (((hubd->h_port_state[port] & PORT_STATUS_CCS) == 0) ||
840 835 ((hubd->h_port_state[port] & PORT_STATUS_PSS) == 0)) {
841 836 /*
842 837 * the port isn't suspended, or connected
843 838 * so don't resume
844 839 */
845 840 retval = USB_SUCCESS;
846 841
847 842 USB_DPRINTF_L2(DPRINT_MASK_PM, hubd->h_log_handle,
848 843 "hubd_resume_port: port=%d not suspended", port);
849 844
850 845 break;
851 846 }
852 847 /*
853 848 * prevent kicking off the hotplug thread
854 849 */
855 850 hubd->h_hotplug_thread++;
856 851 hubd_stop_polling(hubd);
857 852
858 853 /* Now ClearFeature(PortSuspend) */
859 854 for (retry = 0; retry < HUBD_SUS_RES_RETRY; retry++) {
860 855 mutex_exit(HUBD_MUTEX(hubd));
861 856 rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
862 857 hubd->h_default_pipe,
863 858 HUB_HANDLE_PORT_FEATURE_TYPE,
864 859 USB_REQ_CLEAR_FEATURE,
865 860 CFS_PORT_SUSPEND,
866 861 port,
867 862 0, NULL, 0,
868 863 &completion_reason, &cb_flags, 0);
869 864 mutex_enter(HUBD_MUTEX(hubd));
870 865 if (rval != USB_SUCCESS) {
871 866 USB_DPRINTF_L2(DPRINT_MASK_PM,
872 867 hubd->h_log_handle,
873 868 "ClearFeature(PortSuspend) fails"
874 869 "rval=%d cr=%d cb=0x%x", rval,
875 870 completion_reason, cb_flags);
876 871 } else {
877 872 /*
878 873 * As per spec section 11.9 and 7.1.7.7
879 874 * hub need to provide at least 20ms of
880 875 * resume signalling, and s/w provide 10ms of
881 876 * recovery time before accessing the port.
882 877 */
883 878 mutex_exit(HUBD_MUTEX(hubd));
884 879 delay(drv_usectohz(40000));
885 880 mutex_enter(HUBD_MUTEX(hubd));
886 881 (void) hubd_determine_port_status(hubd, port,
887 882 &status, &change, PORT_CHANGE_PSSC);
888 883
889 884 if ((status & PORT_STATUS_PSS) == 0) {
890 885 /* the port did finally resume */
891 886 retval = USB_SUCCESS;
892 887
893 888 break;
894 889 }
895 890 }
896 891 }
897 892
898 893 /* allow hotplug thread again */
899 894 hubd->h_hotplug_thread--;
900 895 hubd_start_polling(hubd, 0);
901 896
902 897 break;
903 898 case USB_DEV_DISCONNECTED:
904 899 /* Ignore - NO Operation */
905 900 retval = USB_SUCCESS;
906 901
907 902 break;
908 903 case USB_DEV_SUSPENDED:
909 904 case USB_DEV_PWRED_DOWN:
910 905 default:
911 906 USB_DPRINTF_L2(DPRINT_MASK_PM, hubd->h_log_handle,
912 907 "Improper state for port Resume");
913 908
914 909 break;
915 910 }
916 911 mutex_exit(HUBD_MUTEX(hubd));
917 912
918 913 return (retval);
919 914 }
920 915
921 916
922 917 /*
923 918 * suspend port depending on device state
924 919 */
925 920 static int
926 921 hubd_suspend_port(hubd_t *hubd, usb_port_t port)
927 922 {
928 923 int rval, retry;
929 924 int retval = USB_FAILURE;
930 925 usb_cr_t completion_reason;
931 926 usb_cb_flags_t cb_flags;
932 927 uint16_t status;
933 928 uint16_t change;
934 929
935 930 USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
936 931 "hubd_suspend_port: port=%d", port);
937 932
938 933 mutex_enter(HUBD_MUTEX(hubd));
939 934
940 935 switch (hubd->h_dev_state) {
941 936 case USB_DEV_HUB_STATE_RECOVER:
942 937 /*
943 938 * When hubd's connect event callback posts a connect
944 939 * event to its child, it results in this busctl call
945 940 * which is valid
946 941 */
947 942 /* FALLTHRU */
948 943 case USB_DEV_HUB_CHILD_PWRLVL:
949 944 /*
950 945 * When one child is resuming, the other could timeout
951 946 * and go to low power mode, which is valid
952 947 */
953 948 /* FALLTHRU */
954 949 case USB_DEV_ONLINE:
955 950 hubd->h_hotplug_thread++;
956 951 hubd_stop_polling(hubd);
957 952
958 953 /*
959 954 * Some devices start an unprovoked resume. According to spec,
960 955 * normal resume time for port is 10ms. Wait for double that
961 956 * time, then check to be sure port is really suspended.
962 957 */
963 958 for (retry = 0; retry < HUBD_SUS_RES_RETRY; retry++) {
964 959 /* Now SetFeature(PortSuspend) */
965 960 mutex_exit(HUBD_MUTEX(hubd));
966 961 if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
967 962 hubd->h_default_pipe,
968 963 HUB_HANDLE_PORT_FEATURE_TYPE,
969 964 USB_REQ_SET_FEATURE,
970 965 CFS_PORT_SUSPEND,
971 966 port,
972 967 0, NULL, 0,
973 968 &completion_reason, &cb_flags, 0)) !=
974 969 USB_SUCCESS) {
975 970 USB_DPRINTF_L2(DPRINT_MASK_PM,
976 971 hubd->h_log_handle,
977 972 "SetFeature(PortSuspend) fails"
978 973 "rval=%d cr=%d cb=0x%x",
979 974 rval, completion_reason, cb_flags);
980 975 }
981 976
982 977 /*
983 978 * some devices start an unprovoked resume
984 979 * wait and check port status after some time
985 980 */
986 981 delay(drv_usectohz(20000));
987 982
988 983 /* either ways ack changes on the port */
989 984 mutex_enter(HUBD_MUTEX(hubd));
990 985 (void) hubd_determine_port_status(hubd, port,
991 986 &status, &change, PORT_CHANGE_PSSC);
992 987 if (status & PORT_STATUS_PSS) {
993 988 /* the port is indeed suspended */
994 989 retval = USB_SUCCESS;
995 990
996 991 break;
997 992 } else {
998 993 USB_DPRINTF_L0(DPRINT_MASK_PM,
999 994 hubd->h_log_handle,
1000 995 "hubdi: port%d failed to be suspended!",
1001 996 port);
1002 997 }
1003 998 }
1004 999
1005 1000 hubd->h_hotplug_thread--;
1006 1001 hubd_start_polling(hubd, 0);
1007 1002
1008 1003 break;
1009 1004
1010 1005 case USB_DEV_DISCONNECTED:
1011 1006 /* Ignore - No Operation */
1012 1007 retval = USB_SUCCESS;
1013 1008
1014 1009 break;
1015 1010
1016 1011 case USB_DEV_SUSPENDED:
1017 1012 case USB_DEV_PWRED_DOWN:
1018 1013 default:
1019 1014 USB_DPRINTF_L2(DPRINT_MASK_PM, hubd->h_log_handle,
1020 1015 "Improper state for port Suspend");
1021 1016
1022 1017 break;
1023 1018 }
1024 1019 mutex_exit(HUBD_MUTEX(hubd));
1025 1020
1026 1021 return (retval);
1027 1022 }
1028 1023
1029 1024
1030 1025 /*
1031 1026 * child post attach/detach notifications
1032 1027 */
1033 1028 static void
1034 1029 hubd_post_attach(hubd_t *hubd, usb_port_t port, struct attachspec *as)
1035 1030 {
1036 1031 dev_info_t *dip;
1037 1032
1038 1033 USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
1039 1034 "hubd_post_attach: port=%d result=%d",
1040 1035 port, as->result);
1041 1036
1042 1037 if (as->result == DDI_SUCCESS) {
1043 1038 /*
1044 1039 * Check if the child created wants to be power managed.
1045 1040 * If yes, the childs power level gets automatically tracked
1046 1041 * by DDI_CTLOPS_POWER busctl.
1047 1042 * If no, we set power of the new child by default
1048 1043 * to USB_DEV_OS_FULL_PWR. Because we should never suspend.
1049 1044 */
1050 1045 mutex_enter(HUBD_MUTEX(hubd));
1051 1046 dip = hubd->h_children_dips[port];
1052 1047 mutex_exit(HUBD_MUTEX(hubd));
1053 1048 if (DEVI(dip)->devi_pm_info == NULL) {
1054 1049 hubd_set_child_pwrlvl(hubd, port, USB_DEV_OS_FULL_PWR);
1055 1050 }
1056 1051 }
1057 1052 }
1058 1053
1059 1054
1060 1055 static void
1061 1056 hubd_post_detach(hubd_t *hubd, usb_port_t port, struct detachspec *ds)
1062 1057 {
1063 1058 USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
1064 1059 "hubd_post_detach: port=%d result=%d", port, ds->result);
1065 1060
1066 1061 /*
1067 1062 * if the device is successfully detached and is the
1068 1063 * last device to detach, mark component as idle
1069 1064 */
1070 1065 mutex_enter(HUBD_MUTEX(hubd));
1071 1066 if (ds->result == DDI_SUCCESS) {
1072 1067 usba_device_t *usba_device = hubd->h_usba_devices[port];
1073 1068 dev_info_t *pdip = hubd->h_dip;
1074 1069 mutex_exit(HUBD_MUTEX(hubd));
1075 1070
1076 1071 usba_hubdi_incr_power_budget(pdip, usba_device);
1077 1072
1078 1073 /*
1079 1074 * We set power of the detached child
1080 1075 * to 0, so that we can suspend if all
1081 1076 * our children are gone
1082 1077 */
1083 1078 hubd_set_child_pwrlvl(hubd, port, USB_DEV_OS_PWR_OFF);
1084 1079
1085 1080 /* check for leaks on detaching */
1086 1081 if ((usba_device) && (ds->cmd == DDI_DETACH)) {
1087 1082 usba_check_for_leaks(usba_device);
1088 1083 }
1089 1084 } else {
1090 1085 mutex_exit(HUBD_MUTEX(hubd));
1091 1086 }
1092 1087 }
1093 1088
1094 1089
1095 1090 /*
1096 1091 * hubd_post_power
1097 1092 * After the child's power entry point has been called
1098 1093 * we record its power level in our local struct.
1099 1094 * If the device has powered off, we suspend port
1100 1095 */
1101 1096 static int
1102 1097 hubd_post_power(hubd_t *hubd, usb_port_t port, pm_bp_child_pwrchg_t *bpc,
1103 1098 int result)
1104 1099 {
1105 1100 int retval = USB_SUCCESS;
1106 1101
1107 1102 USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
1108 1103 "hubd_post_power: port=%d", port);
1109 1104
1110 1105 if (result == DDI_SUCCESS) {
1111 1106
1112 1107 /* record this power in our local struct */
1113 1108 hubd_set_child_pwrlvl(hubd, port, bpc->bpc_nlevel);
1114 1109
1115 1110 if (bpc->bpc_nlevel == USB_DEV_OS_PWR_OFF) {
1116 1111
1117 1112 /* now suspend the port */
1118 1113 retval = hubd_suspend_port(hubd, port);
1119 1114 } else if (bpc->bpc_nlevel == USB_DEV_OS_FULL_PWR) {
1120 1115
1121 1116 /* make sure the port is resumed */
1122 1117 retval = hubd_resume_port(hubd, port);
1123 1118 }
1124 1119 } else {
1125 1120
1126 1121 /* record old power in our local struct */
1127 1122 hubd_set_child_pwrlvl(hubd, port, bpc->bpc_olevel);
1128 1123
1129 1124 if (bpc->bpc_olevel == USB_DEV_OS_PWR_OFF) {
1130 1125
1131 1126 /*
1132 1127 * As this device failed to transition from
1133 1128 * power off state, suspend the port again
1134 1129 */
1135 1130 retval = hubd_suspend_port(hubd, port);
1136 1131 }
1137 1132 }
1138 1133
1139 1134 return (retval);
1140 1135 }
1141 1136
1142 1137
1143 1138 /*
1144 1139 * bus ctl notifications are handled here, the rest goes up to root hub/hcd
1145 1140 */
1146 1141 static int
1147 1142 usba_hubdi_bus_ctl(dev_info_t *dip,
1148 1143 dev_info_t *rdip,
1149 1144 ddi_ctl_enum_t op,
1150 1145 void *arg,
1151 1146 void *result)
1152 1147 {
1153 1148 usba_device_t *hub_usba_device = usba_get_usba_device(rdip);
1154 1149 dev_info_t *root_hub_dip = hub_usba_device->usb_root_hub_dip;
1155 1150 struct attachspec *as;
1156 1151 struct detachspec *ds;
1157 1152 hubd_t *hubd;
1158 1153 usb_port_t port;
1159 1154 int circ, rval;
1160 1155 int retval = DDI_FAILURE;
1161 1156
1162 1157 hubd = hubd_get_soft_state(dip);
1163 1158
1164 1159 mutex_enter(HUBD_MUTEX(hubd));
1165 1160
1166 1161 /* flag that we are currently running bus_ctl */
1167 1162 hubd->h_bus_ctls++;
1168 1163 mutex_exit(HUBD_MUTEX(hubd));
1169 1164
1170 1165 USB_DPRINTF_L3(DPRINT_MASK_HUBDI, hubd->h_log_handle,
1171 1166 "usba_hubdi_bus_ctl:\n\t"
1172 1167 "dip=0x%p, rdip=0x%p, op=0x%x, arg=0x%p",
1173 1168 (void *)dip, (void *)rdip, op, arg);
1174 1169
1175 1170 switch (op) {
1176 1171 case DDI_CTLOPS_ATTACH:
1177 1172 as = (struct attachspec *)arg;
1178 1173 port = hubd_child_dip2port(hubd, rdip);
1179 1174
1180 1175 /* there is nothing to do at resume time */
1181 1176 if (as->cmd == DDI_RESUME) {
1182 1177 break;
1183 1178 }
1184 1179
1185 1180 /* serialize access */
1186 1181 ndi_devi_enter(hubd->h_dip, &circ);
1187 1182
1188 1183 switch (as->when) {
1189 1184 case DDI_PRE:
1190 1185 USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
1191 1186 "DDI_PRE DDI_CTLOPS_ATTACH: dip=%p, port=%d",
1192 1187 (void *)rdip, port);
1193 1188
1194 1189 mutex_enter(HUBD_MUTEX(hubd));
1195 1190 hubd->h_port_state[port] |= HUBD_CHILD_ATTACHING;
1196 1191
1197 1192 /* Go busy here. Matching idle is DDI_POST case. */
1198 1193 (void) hubd_pm_busy_component(hubd, dip, 0);
1199 1194 mutex_exit(HUBD_MUTEX(hubd));
1200 1195
1201 1196 /*
1202 1197 * if we suspended the port previously
1203 1198 * because child went to low power state, and
1204 1199 * someone unloaded the driver, the port would
1205 1200 * still be suspended and needs to be resumed
1206 1201 */
1207 1202 rval = hubd_resume_port(hubd, port);
1208 1203 if (rval == USB_SUCCESS) {
1209 1204 retval = DDI_SUCCESS;
1210 1205 }
1211 1206
1212 1207 break;
1213 1208 case DDI_POST:
1214 1209 USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
1215 1210 "DDI_POST DDI_CTLOPS_ATTACH: dip=%p, port=%d",
1216 1211 (void *)rdip, port);
1217 1212
1218 1213 mutex_enter(HUBD_MUTEX(hubd));
1219 1214 hubd->h_port_state[port] &= ~HUBD_CHILD_ATTACHING;
1220 1215 mutex_exit(HUBD_MUTEX(hubd));
1221 1216
1222 1217 hubd_post_attach(hubd, port, (struct attachspec *)arg);
1223 1218 retval = DDI_SUCCESS;
1224 1219 mutex_enter(HUBD_MUTEX(hubd));
1225 1220
1226 1221 /* Matching idle call for DDI_PRE busy call. */
1227 1222 (void) hubd_pm_idle_component(hubd, dip, 0);
1228 1223 mutex_exit(HUBD_MUTEX(hubd));
1229 1224 }
1230 1225 ndi_devi_exit(hubd->h_dip, circ);
1231 1226
1232 1227 break;
1233 1228 case DDI_CTLOPS_DETACH:
1234 1229 ds = (struct detachspec *)arg;
1235 1230 port = hubd_child_dip2port(hubd, rdip);
1236 1231
1237 1232 /* there is nothing to do at suspend time */
1238 1233 if (ds->cmd == DDI_SUSPEND) {
1239 1234 break;
1240 1235 }
1241 1236
1242 1237 /* serialize access */
1243 1238 ndi_devi_enter(hubd->h_dip, &circ);
1244 1239
1245 1240 switch (ds->when) {
1246 1241 case DDI_PRE:
1247 1242 USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
1248 1243 "DDI_PRE DDI_CTLOPS_DETACH: dip=%p port=%d",
1249 1244 (void *)rdip, port);
1250 1245
1251 1246 mutex_enter(HUBD_MUTEX(hubd));
1252 1247 hubd->h_port_state[port] |= HUBD_CHILD_DETACHING;
1253 1248
1254 1249 /* Go busy here. Matching idle is DDI_POST case. */
1255 1250 (void) hubd_pm_busy_component(hubd, dip, 0);
1256 1251
1257 1252 mutex_exit(HUBD_MUTEX(hubd));
1258 1253 retval = DDI_SUCCESS;
1259 1254
1260 1255 break;
1261 1256 case DDI_POST:
1262 1257 USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
1263 1258 "DDI_POST DDI_CTLOPS_DETACH: dip=%p port=%d",
1264 1259 (void *)rdip, port);
1265 1260
1266 1261 mutex_enter(HUBD_MUTEX(hubd));
1267 1262 hubd->h_port_state[port] &= ~HUBD_CHILD_DETACHING;
1268 1263 mutex_exit(HUBD_MUTEX(hubd));
1269 1264
1270 1265 /* Matching idle call for DDI_PRE busy call. */
1271 1266 hubd_post_detach(hubd, port, (struct detachspec *)arg);
1272 1267 retval = DDI_SUCCESS;
1273 1268 mutex_enter(HUBD_MUTEX(hubd));
1274 1269 (void) hubd_pm_idle_component(hubd, dip, 0);
1275 1270 mutex_exit(HUBD_MUTEX(hubd));
1276 1271
1277 1272 break;
1278 1273 }
1279 1274 ndi_devi_exit(hubd->h_dip, circ);
1280 1275
1281 1276 break;
1282 1277 default:
1283 1278 retval = usba_bus_ctl(root_hub_dip, rdip, op, arg, result);
1284 1279 }
1285 1280
1286 1281 /* decrement bus_ctls count */
1287 1282 mutex_enter(HUBD_MUTEX(hubd));
1288 1283 hubd->h_bus_ctls--;
1289 1284 ASSERT(hubd->h_bus_ctls >= 0);
1290 1285 mutex_exit(HUBD_MUTEX(hubd));
1291 1286
1292 1287 return (retval);
1293 1288 }
1294 1289
1295 1290 /*
1296 1291 * hubd_config_one:
1297 1292 * enumerate one child according to 'port'
1298 1293 */
1299 1294
1300 1295 static boolean_t
1301 1296 hubd_config_one(hubd_t *hubd, int port)
1302 1297 {
1303 1298 uint16_t status, change;
1304 1299 dev_info_t *hdip = hubd->h_dip;
1305 1300 dev_info_t *rh_dip = hubd->h_usba_device->usb_root_hub_dip;
1306 1301 boolean_t online_child = B_FALSE, found = B_FALSE;
1307 1302 int prh_circ, rh_circ, circ;
1308 1303
1309 1304 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
1310 1305 "hubd_config_one: started, hubd_reset_port = 0x%x", port);
1311 1306
1312 1307 ndi_hold_devi(hdip); /* so we don't race with detach */
1313 1308
1314 1309 /*
1315 1310 * this ensures one config activity per system at a time.
1316 1311 * we enter the parent PCI node to have this serialization.
1317 1312 * this also excludes ioctls and deathrow thread
1318 1313 */
1319 1314 ndi_devi_enter(ddi_get_parent(rh_dip), &prh_circ);
1320 1315 ndi_devi_enter(rh_dip, &rh_circ);
1321 1316
1322 1317 /* exclude other threads */
1323 1318 ndi_devi_enter(hdip, &circ);
1324 1319 mutex_enter(HUBD_MUTEX(hubd));
1325 1320
1326 1321 hubd_pm_busy_component(hubd, hubd->h_dip, 0);
1327 1322
1328 1323 if (!hubd->h_children_dips[port]) {
1329 1324
1330 1325 (void) hubd_determine_port_status(hubd, port,
1331 1326 &status, &change, HUBD_ACK_ALL_CHANGES);
1332 1327
1333 1328 if (status & PORT_STATUS_CCS) {
1334 1329 online_child |= (hubd_handle_port_connect(hubd,
1335 1330 port) == USB_SUCCESS);
1336 1331 found = online_child;
1337 1332 }
1338 1333 } else {
1339 1334 found = B_TRUE;
1340 1335 }
1341 1336
1342 1337 mutex_exit(HUBD_MUTEX(hubd));
1343 1338
1344 1339 ndi_devi_exit(hdip, circ);
1345 1340 ndi_devi_exit(rh_dip, rh_circ);
1346 1341 ndi_devi_exit(ddi_get_parent(rh_dip), prh_circ);
1347 1342
1348 1343 if (online_child) {
1349 1344 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
1350 1345 "hubd_config_one: onlining child");
1351 1346
1352 1347 (void) ndi_devi_online(hubd->h_dip, 0);
1353 1348 }
1354 1349
1355 1350 mutex_enter(HUBD_MUTEX(hubd));
1356 1351
1357 1352 (void) hubd_pm_idle_component(hubd, hubd->h_dip, 0);
1358 1353
1359 1354 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
1360 1355 "hubd_config_one: exit");
1361 1356
1362 1357 mutex_exit(HUBD_MUTEX(hubd));
1363 1358
1364 1359 ndi_rele_devi(hdip);
1365 1360
1366 1361 return (found);
1367 1362 }
1368 1363
1369 1364 /*
1370 1365 * bus enumeration entry points
1371 1366 */
1372 1367 static int
1373 1368 hubd_bus_config(dev_info_t *dip, uint_t flag, ddi_bus_config_op_t op,
1374 1369 void *arg, dev_info_t **child)
1375 1370 {
1376 1371 hubd_t *hubd = hubd_get_soft_state(dip);
1377 1372 int rval, circ;
1378 1373 long port;
1379 1374
1380 1375 USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
1381 1376 "hubd_bus_config: op=%d", op);
1382 1377
1383 1378 if (hubdi_bus_config_debug) {
1384 1379 flag |= NDI_DEVI_DEBUG;
1385 1380 }
1386 1381
1387 1382 if (op == BUS_CONFIG_ONE) {
1388 1383 boolean_t found;
1389 1384 char cname[80];
1390 1385 char *name, *addr;
1391 1386
1392 1387 USB_DPRINTF_L2(DPRINT_MASK_PM, hubd->h_log_handle,
1393 1388 "hubd_bus_config: op=%d (BUS_CONFIG_ONE)", op);
1394 1389
1395 1390 (void) snprintf(cname, 80, "%s", (char *)arg);
1396 1391 /* split name into "name@addr" parts */
1397 1392 i_ddi_parse_name(cname, &name, &addr, NULL);
1398 1393 if (addr && *addr) {
1399 1394 (void) ddi_strtol(addr, NULL, 16, &port);
1400 1395 } else {
1401 1396 return (NDI_FAILURE);
1402 1397 }
1403 1398
1404 1399 found = hubd_config_one(hubd, port);
1405 1400
1406 1401 if (found == 0) {
1407 1402 return (NDI_FAILURE);
1408 1403 }
1409 1404
1410 1405 }
1411 1406 ndi_devi_enter(hubd->h_dip, &circ);
1412 1407 rval = ndi_busop_bus_config(dip, flag, op, arg, child, 0);
1413 1408 ndi_devi_exit(hubd->h_dip, circ);
1414 1409
1415 1410 return (rval);
1416 1411 }
1417 1412
1418 1413
1419 1414 static int
1420 1415 hubd_bus_unconfig(dev_info_t *dip, uint_t flag, ddi_bus_config_op_t op,
1421 1416 void *arg)
1422 1417 {
1423 1418 hubd_t *hubd = hubd_get_soft_state(dip);
1424 1419 dev_info_t *cdip;
1425 1420 usb_port_t port;
1426 1421 int circ;
1427 1422 int rval;
1428 1423
1429 1424 USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
1430 1425 "hubd_bus_unconfig: op=%d", op);
1431 1426
1432 1427 if (hubdi_bus_config_debug) {
1433 1428 flag |= NDI_DEVI_DEBUG;
1434 1429 }
1435 1430
1436 1431 if ((op == BUS_UNCONFIG_ALL) && (flag & NDI_AUTODETACH) == 0) {
1437 1432 flag |= NDI_DEVI_REMOVE;
1438 1433 }
1439 1434
1440 1435 /* serialize access */
1441 1436 ndi_devi_enter(dip, &circ);
1442 1437
1443 1438 rval = ndi_busop_bus_unconfig(dip, flag, op, arg);
1444 1439
1445 1440 /* logically zap children's list */
1446 1441 mutex_enter(HUBD_MUTEX(hubd));
1447 1442 for (port = 1; port <= hubd->h_hub_descr.bNbrPorts; port++) {
1448 1443 hubd->h_port_state[port] |= HUBD_CHILD_ZAP;
1449 1444 }
1450 1445 mutex_exit(HUBD_MUTEX(hubd));
1451 1446
1452 1447 /* fill in what's left */
1453 1448 for (cdip = ddi_get_child(dip); cdip;
1454 1449 cdip = ddi_get_next_sibling(cdip)) {
1455 1450 usba_device_t *usba_device = usba_get_usba_device(cdip);
1456 1451
1457 1452 if (usba_device == NULL) {
1458 1453
1459 1454 continue;
1460 1455 }
1461 1456 mutex_enter(HUBD_MUTEX(hubd));
1462 1457 port = usba_device->usb_port;
1463 1458 hubd->h_children_dips[port] = cdip;
1464 1459 hubd->h_port_state[port] &= ~HUBD_CHILD_ZAP;
1465 1460 mutex_exit(HUBD_MUTEX(hubd));
1466 1461 }
1467 1462
1468 1463 /* physically zap the children we didn't find */
1469 1464 mutex_enter(HUBD_MUTEX(hubd));
1470 1465 for (port = 1; port <= hubd->h_hub_descr.bNbrPorts; port++) {
1471 1466 if (hubd->h_port_state[port] & HUBD_CHILD_ZAP) {
1472 1467 /* zap the dip and usba_device structure as well */
1473 1468 hubd_free_usba_device(hubd, hubd->h_usba_devices[port]);
1474 1469 hubd->h_children_dips[port] = NULL;
1475 1470 hubd->h_port_state[port] &= ~HUBD_CHILD_ZAP;
1476 1471 }
1477 1472 }
1478 1473 mutex_exit(HUBD_MUTEX(hubd));
1479 1474
1480 1475 ndi_devi_exit(dip, circ);
1481 1476
1482 1477 USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
1483 1478 "hubd_bus_unconfig: rval=%d", rval);
1484 1479
1485 1480 return (rval);
1486 1481 }
1487 1482
1488 1483
1489 1484 /* bus_power entry point */
1490 1485 static int
1491 1486 hubd_bus_power(dev_info_t *dip, void *impl_arg, pm_bus_power_op_t op,
1492 1487 void *arg, void *result)
1493 1488 {
1494 1489 hubd_t *hubd;
1495 1490 int rval, pwrup_res;
1496 1491 usb_port_t port;
1497 1492 int retval = DDI_FAILURE;
1498 1493 pm_bp_child_pwrchg_t *bpc;
1499 1494 pm_bp_nexus_pwrup_t bpn;
1500 1495
1501 1496 hubd = hubd_get_soft_state(dip);
1502 1497
1503 1498 USB_DPRINTF_L4(DPRINT_MASK_HUBDI, hubd->h_log_handle,
1504 1499 "hubd_bus_power: dip=%p, impl_arg=%p, power_op=%d, arg=%p, "
1505 1500 "result=%d\n", (void *)dip, impl_arg, op, arg, *(int *)result);
1506 1501
1507 1502 bpc = (pm_bp_child_pwrchg_t *)arg;
1508 1503
1509 1504 mutex_enter(HUBD_MUTEX(hubd));
1510 1505 hubd->h_bus_pwr++;
1511 1506 mutex_exit(HUBD_MUTEX(hubd));
1512 1507
1513 1508 switch (op) {
1514 1509 case BUS_POWER_PRE_NOTIFICATION:
1515 1510 port = hubd_child_dip2port(hubd, bpc->bpc_dip);
1516 1511 USB_DPRINTF_L3(DPRINT_MASK_HUBDI, hubd->h_log_handle,
1517 1512 "hubd_bus_power: BUS_POWER_PRE_NOTIFICATION, port=%d",
1518 1513 port);
1519 1514
1520 1515 /* go to full power if we are powered down */
1521 1516 mutex_enter(HUBD_MUTEX(hubd));
1522 1517
1523 1518 /*
1524 1519 * If this case completes normally, idle will be in
1525 1520 * hubd_bus_power / BUS_POWER_POST_NOTIFICATION
1526 1521 */
1527 1522 hubd_pm_busy_component(hubd, dip, 0);
1528 1523
1529 1524 /*
1530 1525 * raise power only if we have created the components
1531 1526 * and are currently in low power
1532 1527 */
1533 1528 if ((hubd->h_dev_state == USB_DEV_PWRED_DOWN) &&
1534 1529 hubd->h_hubpm->hubp_wakeup_enabled) {
1535 1530 mutex_exit(HUBD_MUTEX(hubd));
1536 1531
1537 1532 bpn.bpn_comp = 0;
1538 1533 bpn.bpn_dip = dip;
1539 1534 bpn.bpn_level = USB_DEV_OS_FULL_PWR;
1540 1535 bpn.bpn_private = bpc->bpc_private;
1541 1536
1542 1537 rval = pm_busop_bus_power(dip, impl_arg,
1543 1538 BUS_POWER_NEXUS_PWRUP, (void *)&bpn,
1544 1539 (void *)&pwrup_res);
1545 1540
1546 1541 if (rval != DDI_SUCCESS || pwrup_res != DDI_SUCCESS) {
1547 1542 mutex_enter(HUBD_MUTEX(hubd));
1548 1543 hubd_pm_idle_component(hubd, dip, 0);
1549 1544 mutex_exit(HUBD_MUTEX(hubd));
1550 1545
1551 1546 break;
1552 1547 }
1553 1548 mutex_enter(HUBD_MUTEX(hubd));
1554 1549 }
1555 1550
1556 1551 /* indicate that child is changing power level */
1557 1552 hubd->h_port_state[port] |= HUBD_CHILD_PWRLVL_CHNG;
1558 1553 mutex_exit(HUBD_MUTEX(hubd));
1559 1554
1560 1555 if ((bpc->bpc_olevel == 0) &&
1561 1556 (bpc->bpc_nlevel > bpc->bpc_olevel)) {
1562 1557 /*
1563 1558 * this child is transitioning from power off
1564 1559 * to power on state - resume port
1565 1560 */
1566 1561 rval = hubd_resume_port(hubd, port);
1567 1562 if (rval == USB_SUCCESS) {
1568 1563 retval = DDI_SUCCESS;
1569 1564 } else {
1570 1565 /* reset this flag on failure */
1571 1566 mutex_enter(HUBD_MUTEX(hubd));
1572 1567 hubd->h_port_state[port] &=
1573 1568 ~HUBD_CHILD_PWRLVL_CHNG;
1574 1569 hubd_pm_idle_component(hubd, dip, 0);
1575 1570 mutex_exit(HUBD_MUTEX(hubd));
1576 1571 }
1577 1572 } else {
1578 1573 retval = DDI_SUCCESS;
1579 1574 }
1580 1575
1581 1576 break;
1582 1577 case BUS_POWER_POST_NOTIFICATION:
1583 1578 port = hubd_child_dip2port(hubd, bpc->bpc_dip);
1584 1579 USB_DPRINTF_L3(DPRINT_MASK_HUBDI, hubd->h_log_handle,
1585 1580 "hubd_bus_power: BUS_POWER_POST_NOTIFICATION, port=%d",
1586 1581 port);
1587 1582
1588 1583 mutex_enter(HUBD_MUTEX(hubd));
1589 1584 hubd->h_port_state[port] &= ~HUBD_CHILD_PWRLVL_CHNG;
1590 1585 mutex_exit(HUBD_MUTEX(hubd));
1591 1586
1592 1587 /* record child's pwr and suspend port if required */
1593 1588 rval = hubd_post_power(hubd, port, bpc, *(int *)result);
1594 1589 if (rval == USB_SUCCESS) {
1595 1590
1596 1591 retval = DDI_SUCCESS;
1597 1592 }
1598 1593
1599 1594 mutex_enter(HUBD_MUTEX(hubd));
1600 1595
1601 1596 /*
1602 1597 * Matching idle for the busy in
1603 1598 * hubd_bus_power / BUS_POWER_PRE_NOTIFICATION
1604 1599 */
1605 1600 hubd_pm_idle_component(hubd, dip, 0);
1606 1601
1607 1602 mutex_exit(HUBD_MUTEX(hubd));
1608 1603
1609 1604 break;
1610 1605 default:
1611 1606 retval = pm_busop_bus_power(dip, impl_arg, op, arg, result);
1612 1607
1613 1608 break;
1614 1609 }
1615 1610
1616 1611 mutex_enter(HUBD_MUTEX(hubd));
1617 1612 hubd->h_bus_pwr--;
1618 1613 mutex_exit(HUBD_MUTEX(hubd));
1619 1614
1620 1615 return (retval);
1621 1616 }
1622 1617
1623 1618
1624 1619 /*
1625 1620 * functions to handle power transition for OS levels 0 -> 3
1626 1621 */
1627 1622 static int
1628 1623 hubd_pwrlvl0(hubd_t *hubd)
1629 1624 {
1630 1625 hub_power_t *hubpm;
1631 1626
1632 1627 /* We can't power down if hotplug thread is running */
1633 1628 if (hubd->h_hotplug_thread || hubd->h_hubpm->hubp_busy_pm ||
1634 1629 (hubd_can_suspend(hubd) == USB_FAILURE)) {
1635 1630
1636 1631 return (USB_FAILURE);
1637 1632 }
1638 1633
1639 1634 switch (hubd->h_dev_state) {
1640 1635 case USB_DEV_ONLINE:
1641 1636 hubpm = hubd->h_hubpm;
1642 1637
1643 1638 /*
1644 1639 * To avoid race with bus_power pre_notify on check over
1645 1640 * dev_state, we need to correctly set the dev state
1646 1641 * before the mutex is dropped in stop polling.
1647 1642 */
1648 1643 hubd->h_dev_state = USB_DEV_PWRED_DOWN;
1649 1644 hubpm->hubp_current_power = USB_DEV_OS_PWR_OFF;
1650 1645
1651 1646 /*
1652 1647 * if we are the root hub, do not stop polling
1653 1648 * otherwise, we will never see a resume
1654 1649 */
1655 1650 if (usba_is_root_hub(hubd->h_dip)) {
1656 1651 /* place holder to implement Global Suspend */
1657 1652 USB_DPRINTF_L2(DPRINT_MASK_PM, hubd->h_log_handle,
1658 1653 "Global Suspend: Not Yet Implemented");
1659 1654 } else {
1660 1655 hubd_stop_polling(hubd);
1661 1656 }
1662 1657
1663 1658 /* Issue USB D3 command to the device here */
1664 1659 (void) usb_set_device_pwrlvl3(hubd->h_dip);
1665 1660
1666 1661 break;
1667 1662 case USB_DEV_DISCONNECTED:
1668 1663 case USB_DEV_SUSPENDED:
1669 1664 case USB_DEV_PWRED_DOWN:
1670 1665 default:
1671 1666
1672 1667 break;
1673 1668 }
1674 1669
1675 1670 return (USB_SUCCESS);
1676 1671 }
1677 1672
1678 1673
1679 1674 /* ARGSUSED */
1680 1675 static int
1681 1676 hubd_pwrlvl1(hubd_t *hubd)
1682 1677 {
1683 1678 /* Issue USB D2 command to the device here */
1684 1679 (void) usb_set_device_pwrlvl2(hubd->h_dip);
1685 1680
1686 1681 return (USB_FAILURE);
1687 1682 }
1688 1683
1689 1684
1690 1685 /* ARGSUSED */
1691 1686 static int
1692 1687 hubd_pwrlvl2(hubd_t *hubd)
1693 1688 {
1694 1689 /* Issue USB D1 command to the device here */
1695 1690 (void) usb_set_device_pwrlvl1(hubd->h_dip);
1696 1691
1697 1692 return (USB_FAILURE);
1698 1693 }
1699 1694
1700 1695
1701 1696 static int
1702 1697 hubd_pwrlvl3(hubd_t *hubd)
1703 1698 {
1704 1699 hub_power_t *hubpm;
1705 1700 int rval;
1706 1701
1707 1702 USB_DPRINTF_L2(DPRINT_MASK_PM, hubd->h_log_handle, "hubd_pwrlvl3");
1708 1703
1709 1704 hubpm = hubd->h_hubpm;
1710 1705 switch (hubd->h_dev_state) {
1711 1706 case USB_DEV_PWRED_DOWN:
1712 1707 ASSERT(hubpm->hubp_current_power == USB_DEV_OS_PWR_OFF);
1713 1708 if (usba_is_root_hub(hubd->h_dip)) {
1714 1709 /* implement global resume here */
1715 1710 USB_DPRINTF_L2(DPRINT_MASK_PM,
1716 1711 hubd->h_log_handle,
1717 1712 "Global Resume: Not Yet Implemented");
1718 1713 }
1719 1714 /* Issue USB D0 command to the device here */
1720 1715 rval = usb_set_device_pwrlvl0(hubd->h_dip);
1721 1716 ASSERT(rval == USB_SUCCESS);
1722 1717 hubd->h_dev_state = USB_DEV_ONLINE;
1723 1718 hubpm->hubp_current_power = USB_DEV_OS_FULL_PWR;
1724 1719 hubpm->hubp_time_at_full_power = gethrtime();
1725 1720 hubd_start_polling(hubd, 0);
1726 1721
1727 1722 /* FALLTHRU */
1728 1723 case USB_DEV_ONLINE:
1729 1724 /* we are already in full power */
1730 1725
1731 1726 /* FALLTHRU */
1732 1727 case USB_DEV_DISCONNECTED:
1733 1728 case USB_DEV_SUSPENDED:
1734 1729 /*
1735 1730 * PM framework tries to put you in full power
1736 1731 * during system shutdown. If we are disconnected
1737 1732 * return success. Also, we should not change state
1738 1733 * when we are disconnected or suspended or about to
1739 1734 * transition to that state
1740 1735 */
1741 1736
1742 1737 return (USB_SUCCESS);
1743 1738 default:
1744 1739 USB_DPRINTF_L2(DPRINT_MASK_PM, hubd->h_log_handle,
1745 1740 "hubd_pwrlvl3: Illegal dev_state=%d", hubd->h_dev_state);
1746 1741
1747 1742 return (USB_FAILURE);
1748 1743 }
1749 1744 }
1750 1745
1751 1746
1752 1747 /* power entry point */
1753 1748 /* ARGSUSED */
1754 1749 int
1755 1750 usba_hubdi_power(dev_info_t *dip, int comp, int level)
1756 1751 {
1757 1752 hubd_t *hubd;
1758 1753 hub_power_t *hubpm;
1759 1754 int retval;
1760 1755 int circ;
1761 1756
1762 1757 hubd = hubd_get_soft_state(dip);
1763 1758 USB_DPRINTF_L3(DPRINT_MASK_HUBDI, hubd->h_log_handle,
1764 1759 "usba_hubdi_power: level=%d", level);
1765 1760
1766 1761 ndi_devi_enter(dip, &circ);
1767 1762
1768 1763 mutex_enter(HUBD_MUTEX(hubd));
1769 1764 hubpm = hubd->h_hubpm;
1770 1765
1771 1766 /* check if we are transitioning to a legal power level */
1772 1767 if (USB_DEV_PWRSTATE_OK(hubpm->hubp_pwr_states, level)) {
1773 1768 USB_DPRINTF_L2(DPRINT_MASK_HUBDI, hubd->h_log_handle,
1774 1769 "usba_hubdi_power: illegal power level=%d "
1775 1770 "hubp_pwr_states=0x%x", level, hubpm->hubp_pwr_states);
1776 1771 mutex_exit(HUBD_MUTEX(hubd));
1777 1772
1778 1773 ndi_devi_exit(dip, circ);
1779 1774
1780 1775 return (DDI_FAILURE);
1781 1776 }
1782 1777
1783 1778 switch (level) {
1784 1779 case USB_DEV_OS_PWR_OFF:
1785 1780 retval = hubd_pwrlvl0(hubd);
1786 1781
1787 1782 break;
1788 1783 case USB_DEV_OS_PWR_1:
1789 1784 retval = hubd_pwrlvl1(hubd);
1790 1785
1791 1786 break;
1792 1787 case USB_DEV_OS_PWR_2:
1793 1788 retval = hubd_pwrlvl2(hubd);
1794 1789
1795 1790 break;
1796 1791 case USB_DEV_OS_FULL_PWR:
1797 1792 retval = hubd_pwrlvl3(hubd);
1798 1793
1799 1794 break;
1800 1795 }
1801 1796 mutex_exit(HUBD_MUTEX(hubd));
1802 1797
1803 1798 ndi_devi_exit(dip, circ);
1804 1799
1805 1800 return ((retval == USB_SUCCESS) ? DDI_SUCCESS : DDI_FAILURE);
1806 1801 }
1807 1802
1808 1803
1809 1804 /* power entry point for the root hub */
1810 1805 int
1811 1806 usba_hubdi_root_hub_power(dev_info_t *dip, int comp, int level)
1812 1807 {
1813 1808 return (usba_hubdi_power(dip, comp, level));
1814 1809 }
1815 1810
1816 1811
1817 1812 /*
1818 1813 * standard driver entry points support code
1819 1814 */
1820 1815 int
1821 1816 usba_hubdi_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
1822 1817 {
1823 1818 int instance = ddi_get_instance(dip);
1824 1819 hubd_t *hubd = NULL;
1825 1820 int i, rval;
1826 1821 int minor;
1827 1822 uint8_t ports_count;
1828 1823 char *log_name = NULL;
1829 1824 const char *root_hub_drvname;
1830 1825 usb_ep_data_t *ep_data;
1831 1826 usba_device_t *child_ud = NULL;
1832 1827 usb_dev_descr_t *usb_dev_descr;
1833 1828 usb_port_status_t parent_port_status, child_port_status;
1834 1829
1835 1830 USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubdi_log_handle,
1836 1831 "hubd_attach instance %d, cmd=0x%x", instance, cmd);
1837 1832
1838 1833 switch (cmd) {
1839 1834 case DDI_ATTACH:
1840 1835
1841 1836 break;
1842 1837 case DDI_RESUME:
1843 1838 hubd_cpr_resume(dip);
1844 1839
1845 1840 return (DDI_SUCCESS);
1846 1841 default:
1847 1842 return (DDI_FAILURE);
1848 1843 }
1849 1844
1850 1845 /*
1851 1846 * Allocate softc information.
1852 1847 */
1853 1848 if (usba_is_root_hub(dip)) {
1854 1849 /* soft state has already been allocated */
1855 1850 hubd = hubd_get_soft_state(dip);
1856 1851 minor = HUBD_IS_ROOT_HUB;
1857 1852
1858 1853 /* generate readable labels for different root hubs */
1859 1854 root_hub_drvname = ddi_driver_name(dip);
1860 1855 if (strcmp(root_hub_drvname, "ehci") == 0) {
1861 1856 log_name = "eusb";
1862 1857 } else if (strcmp(root_hub_drvname, "uhci") == 0) {
1863 1858 log_name = "uusb";
1864 1859 } else {
1865 1860 /* std. for ohci */
1866 1861 log_name = "usb";
1867 1862 }
1868 1863 } else {
1869 1864 rval = ddi_soft_state_zalloc(hubd_statep, instance);
1870 1865 minor = 0;
1871 1866
1872 1867 if (rval != DDI_SUCCESS) {
1873 1868 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubdi_log_handle,
1874 1869 "cannot allocate soft state (%d)", instance);
1875 1870 goto fail;
1876 1871 }
1877 1872
1878 1873 hubd = hubd_get_soft_state(dip);
1879 1874 if (hubd == NULL) {
1880 1875 goto fail;
1881 1876 }
1882 1877 }
1883 1878
1884 1879 hubd->h_log_handle = usb_alloc_log_hdl(dip, log_name, &hubd_errlevel,
1885 1880 &hubd_errmask, &hubd_instance_debug, 0);
1886 1881
1887 1882 hubd->h_usba_device = child_ud = usba_get_usba_device(dip);
1888 1883 hubd->h_dip = dip;
1889 1884 hubd->h_instance = instance;
1890 1885
1891 1886 mutex_enter(&child_ud->usb_mutex);
1892 1887 child_port_status = child_ud->usb_port_status;
1893 1888 usb_dev_descr = child_ud->usb_dev_descr;
1894 1889 parent_port_status = (child_ud->usb_hs_hub_usba_dev) ?
1895 1890 child_ud->usb_hs_hub_usba_dev->usb_port_status : 0;
1896 1891 mutex_exit(&child_ud->usb_mutex);
1897 1892
1898 1893 if ((child_port_status == USBA_FULL_SPEED_DEV) &&
1899 1894 (parent_port_status == USBA_HIGH_SPEED_DEV) &&
1900 1895 (usb_dev_descr->bcdUSB == 0x100)) {
1901 1896 USB_DPRINTF_L0(DPRINT_MASK_ATTA, hubd->h_log_handle,
1902 1897 "Use of a USB1.0 hub behind a high speed port may "
1903 1898 "cause unexpected failures");
1904 1899 }
1905 1900
1906 1901 hubd->h_pipe_policy.pp_max_async_reqs = 1;
1907 1902
1908 1903 /* register with USBA as client driver */
1909 1904 if (usb_client_attach(dip, USBDRV_VERSION, 0) != USB_SUCCESS) {
1910 1905 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
1911 1906 "client attach failed");
1912 1907
1913 1908 goto fail;
1914 1909 }
1915 1910
1916 1911 if (usb_get_dev_data(dip, &hubd->h_dev_data,
1917 1912 USB_PARSE_LVL_IF, 0) != USB_SUCCESS) {
1918 1913 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
1919 1914 "cannot get dev_data");
1920 1915
1921 1916 goto fail;
1922 1917 }
1923 1918
1924 1919 if ((ep_data = usb_lookup_ep_data(dip, hubd->h_dev_data,
1925 1920 hubd->h_dev_data->dev_curr_if, 0, 0,
1926 1921 (uint_t)USB_EP_ATTR_INTR, (uint_t)USB_EP_DIR_IN)) == NULL) {
1927 1922 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
1928 1923 "no interrupt IN endpoint found");
1929 1924
1930 1925 goto fail;
1931 1926 }
1932 1927
1933 1928 hubd->h_ep1_descr = ep_data->ep_descr;
1934 1929 hubd->h_default_pipe = hubd->h_dev_data->dev_default_ph;
1935 1930
1936 1931 mutex_init(HUBD_MUTEX(hubd), NULL, MUTEX_DRIVER,
1937 1932 hubd->h_dev_data->dev_iblock_cookie);
1938 1933 cv_init(&hubd->h_cv_reset_port, NULL, CV_DRIVER, NULL);
1939 1934 cv_init(&hubd->h_cv_hotplug_dev, NULL, CV_DRIVER, NULL);
1940 1935
1941 1936 hubd->h_init_state |= HUBD_LOCKS_DONE;
1942 1937
1943 1938 usb_free_descr_tree(dip, hubd->h_dev_data);
1944 1939
1945 1940 /*
1946 1941 * register this hub instance with usba
1947 1942 */
1948 1943 rval = usba_hubdi_register(dip, 0);
1949 1944 if (rval != USB_SUCCESS) {
1950 1945 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
1951 1946 "usba_hubdi_register failed");
1952 1947 goto fail;
1953 1948 }
1954 1949
1955 1950 mutex_enter(HUBD_MUTEX(hubd));
1956 1951 hubd->h_init_state |= HUBD_HUBDI_REGISTERED;
1957 1952 hubd->h_dev_state = USB_DEV_ONLINE;
1958 1953 mutex_exit(HUBD_MUTEX(hubd));
1959 1954
1960 1955 /* now create components to power manage this device */
1961 1956 hubd_create_pm_components(dip, hubd);
1962 1957
1963 1958 /*
1964 1959 * Event handling: definition and registration
1965 1960 *
1966 1961 * first the definition:
1967 1962 * get event handle
1968 1963 */
1969 1964 (void) ndi_event_alloc_hdl(dip, 0, &hubd->h_ndi_event_hdl, NDI_SLEEP);
1970 1965
1971 1966 /* bind event set to the handle */
1972 1967 if (ndi_event_bind_set(hubd->h_ndi_event_hdl, &hubd_ndi_events,
1973 1968 NDI_SLEEP)) {
1974 1969 USB_DPRINTF_L3(DPRINT_MASK_ATTA, hubd->h_log_handle,
1975 1970 "binding event set failed");
1976 1971
1977 1972 goto fail;
1978 1973 }
1979 1974
1980 1975 /* event registration */
1981 1976 if (hubd_register_events(hubd) != USB_SUCCESS) {
1982 1977 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
1983 1978 "hubd_register_events failed");
1984 1979
1985 1980 goto fail;
1986 1981 }
1987 1982
1988 1983 mutex_enter(HUBD_MUTEX(hubd));
1989 1984 hubd->h_init_state |= HUBD_EVENTS_REGISTERED;
1990 1985
1991 1986 if ((hubd_get_hub_descriptor(hubd)) != USB_SUCCESS) {
1992 1987 mutex_exit(HUBD_MUTEX(hubd));
1993 1988
1994 1989 goto fail;
1995 1990 }
1996 1991
1997 1992 if (ddi_prop_exists(DDI_DEV_T_ANY, dip,
1998 1993 (DDI_PROP_DONTPASS | DDI_PROP_NOTPROM),
1999 1994 "hub-ignore-power-budget") == 1) {
2000 1995 hubd->h_ignore_pwr_budget = B_TRUE;
2001 1996 } else {
2002 1997 hubd->h_ignore_pwr_budget = B_FALSE;
2003 1998
2004 1999 /* initialize hub power budget variables */
2005 2000 if (hubd_init_power_budget(hubd) != USB_SUCCESS) {
2006 2001 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
2007 2002 "hubd_init_power_budget failed");
2008 2003 mutex_exit(HUBD_MUTEX(hubd));
2009 2004
2010 2005 goto fail;
2011 2006 }
2012 2007 }
2013 2008
2014 2009 /* initialize and create children */
2015 2010 if (hubd_check_ports(hubd) != USB_SUCCESS) {
2016 2011 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
2017 2012 "hubd_check_ports failed");
2018 2013 mutex_exit(HUBD_MUTEX(hubd));
2019 2014
2020 2015 goto fail;
2021 2016 }
2022 2017
2023 2018 /*
2024 2019 * create cfgadm nodes
2025 2020 */
2026 2021 hubd->h_ancestry_str = (char *)kmem_zalloc(HUBD_APID_NAMELEN, KM_SLEEP);
2027 2022 hubd_get_ancestry_str(hubd);
2028 2023
2029 2024 USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
2030 2025 "#ports=0x%x", hubd->h_hub_descr.bNbrPorts);
2031 2026
2032 2027 for (i = 1; i <= hubd->h_hub_descr.bNbrPorts; i++) {
2033 2028 char ap_name[HUBD_APID_NAMELEN];
2034 2029
2035 2030 (void) snprintf(ap_name, HUBD_APID_NAMELEN, "%s%d",
2036 2031 hubd->h_ancestry_str, i);
2037 2032 USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
2038 2033 "ap_name=%s", ap_name);
2039 2034
2040 2035 if (ddi_create_minor_node(dip, ap_name, S_IFCHR, instance,
2041 2036 DDI_NT_USB_ATTACHMENT_POINT, 0) != DDI_SUCCESS) {
2042 2037 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
2043 2038 "cannot create attachment point node (%d)",
2044 2039 instance);
2045 2040 mutex_exit(HUBD_MUTEX(hubd));
2046 2041
2047 2042 goto fail;
2048 2043 }
2049 2044 }
2050 2045
2051 2046 ports_count = hubd->h_hub_descr.bNbrPorts;
2052 2047 mutex_exit(HUBD_MUTEX(hubd));
2053 2048
2054 2049 /* create minor nodes */
2055 2050 if (ddi_create_minor_node(dip, "hubd", S_IFCHR,
2056 2051 instance | minor, DDI_NT_NEXUS, 0) != DDI_SUCCESS) {
2057 2052
2058 2053 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
2059 2054 "cannot create devctl minor node (%d)", instance);
2060 2055
2061 2056 goto fail;
2062 2057 }
2063 2058
2064 2059 mutex_enter(HUBD_MUTEX(hubd));
2065 2060 hubd->h_init_state |= HUBD_MINOR_NODE_CREATED;
2066 2061 mutex_exit(HUBD_MUTEX(hubd));
2067 2062
2068 2063 if (ndi_prop_update_int(DDI_DEV_T_NONE, dip,
2069 2064 "usb-port-count", ports_count) != DDI_PROP_SUCCESS) {
2070 2065 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
2071 2066 "usb-port-count update failed");
2072 2067 }
2073 2068
2074 2069 /*
2075 2070 * host controller driver has already reported this dev
2076 2071 * if we are the root hub
2077 2072 */
2078 2073 if (!usba_is_root_hub(dip)) {
2079 2074 ddi_report_dev(dip);
2080 2075 }
2081 2076
2082 2077 /* enable deathrow thread */
2083 2078 hubd->h_cleanup_enabled = B_TRUE;
2084 2079 mutex_enter(HUBD_MUTEX(hubd));
2085 2080 hubd_pm_idle_component(hubd, dip, 0);
2086 2081 mutex_exit(HUBD_MUTEX(hubd));
2087 2082
2088 2083 return (DDI_SUCCESS);
2089 2084
2090 2085 fail:
2091 2086 {
2092 2087 char *pathname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
2093 2088
2094 2089 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubdi_log_handle,
2095 2090 "cannot attach %s", ddi_pathname(dip, pathname));
2096 2091
2097 2092 kmem_free(pathname, MAXPATHLEN);
2098 2093 }
2099 2094
2100 2095 mutex_enter(HUBD_MUTEX(hubd));
2101 2096 hubd_pm_idle_component(hubd, dip, 0);
2102 2097 mutex_exit(HUBD_MUTEX(hubd));
2103 2098
2104 2099 if (hubd) {
2105 2100 rval = hubd_cleanup(dip, hubd);
2106 2101 if (rval != USB_SUCCESS) {
2107 2102 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubdi_log_handle,
2108 2103 "failure to complete cleanup after attach failure");
2109 2104 }
2110 2105 }
2111 2106
2112 2107 return (DDI_FAILURE);
2113 2108 }
2114 2109
2115 2110
2116 2111 int
2117 2112 usba_hubdi_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
2118 2113 {
2119 2114 hubd_t *hubd = hubd_get_soft_state(dip);
2120 2115 int rval;
2121 2116
2122 2117 USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
2123 2118 "hubd_detach: cmd=0x%x", cmd);
2124 2119
2125 2120 switch (cmd) {
2126 2121 case DDI_DETACH:
2127 2122 rval = hubd_cleanup(dip, hubd);
2128 2123
2129 2124 return ((rval == USB_SUCCESS) ? DDI_SUCCESS : DDI_FAILURE);
2130 2125 case DDI_SUSPEND:
2131 2126 rval = hubd_cpr_suspend(hubd);
2132 2127
2133 2128 return ((rval == USB_SUCCESS) ? DDI_SUCCESS : DDI_FAILURE);
2134 2129 default:
2135 2130 return (DDI_FAILURE);
2136 2131 }
2137 2132 }
2138 2133
2139 2134
2140 2135 /*
2141 2136 * hubd_setdevaddr
2142 2137 * set the device addrs on this port
2143 2138 */
2144 2139 static int
2145 2140 hubd_setdevaddr(hubd_t *hubd, usb_port_t port)
2146 2141 {
2147 2142 int rval;
2148 2143 usb_cr_t completion_reason;
2149 2144 usb_cb_flags_t cb_flags;
2150 2145 usb_pipe_handle_t ph;
2151 2146 dev_info_t *child_dip = NULL;
2152 2147 uchar_t address = 0;
2153 2148 usba_device_t *usba_device;
2154 2149 int retry = 0;
2155 2150 long time_delay;
2156 2151
2157 2152 USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
2158 2153 "hubd_setdevaddr: port=%d", port);
2159 2154
2160 2155 ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
2161 2156
2162 2157 child_dip = hubd->h_children_dips[port];
2163 2158 address = hubd->h_usba_devices[port]->usb_addr;
2164 2159 usba_device = hubd->h_usba_devices[port];
2165 2160
2166 2161 /* close the default pipe with addr x */
2167 2162 mutex_exit(HUBD_MUTEX(hubd));
2168 2163 ph = usba_get_dflt_pipe_handle(child_dip);
2169 2164 usb_pipe_close(child_dip, ph,
2170 2165 USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, NULL, NULL);
2171 2166 mutex_enter(HUBD_MUTEX(hubd));
2172 2167
2173 2168 /*
2174 2169 * As this device has been reset, temporarily
2175 2170 * assign the default address
2176 2171 */
2177 2172 mutex_enter(&usba_device->usb_mutex);
2178 2173 address = usba_device->usb_addr;
2179 2174 usba_device->usb_addr = USBA_DEFAULT_ADDR;
2180 2175 mutex_exit(&usba_device->usb_mutex);
2181 2176
2182 2177 mutex_exit(HUBD_MUTEX(hubd));
2183 2178
2184 2179 time_delay = drv_usectohz(hubd_device_delay / 20);
2185 2180 for (retry = 0; retry < hubd_retry_enumerate; retry++) {
2186 2181
2187 2182 /* open child's default pipe with USBA_DEFAULT_ADDR */
2188 2183 if (usb_pipe_open(child_dip, NULL, NULL,
2189 2184 USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, &ph) !=
2190 2185 USB_SUCCESS) {
2191 2186 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
2192 2187 "hubd_setdevaddr: Unable to open default pipe");
2193 2188
2194 2189 break;
2195 2190 }
2196 2191
2197 2192 /* Set the address of the device */
2198 2193 if ((rval = usb_pipe_sync_ctrl_xfer(child_dip, ph,
2199 2194 USB_DEV_REQ_HOST_TO_DEV,
2200 2195 USB_REQ_SET_ADDRESS, /* bRequest */
2201 2196 address, /* wValue */
2202 2197 0, /* wIndex */
2203 2198 0, /* wLength */
2204 2199 NULL, 0,
2205 2200 &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
2206 2201 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
2207 2202 "hubd_setdevaddr(%d): rval=%d cr=%d cb_fl=0x%x",
2208 2203 retry, rval, completion_reason, cb_flags);
2209 2204 }
2210 2205
2211 2206 usb_pipe_close(child_dip, ph,
2212 2207 USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, NULL, NULL);
2213 2208
2214 2209 if (rval == USB_SUCCESS) {
2215 2210
2216 2211 break;
2217 2212 }
2218 2213
2219 2214 delay(time_delay);
2220 2215 }
2221 2216
2222 2217 /* Reset to the old address */
2223 2218 mutex_enter(&usba_device->usb_mutex);
2224 2219 usba_device->usb_addr = address;
2225 2220 mutex_exit(&usba_device->usb_mutex);
2226 2221 mutex_enter(HUBD_MUTEX(hubd));
2227 2222
2228 2223 usba_clear_data_toggle(usba_device);
2229 2224
2230 2225 return (rval);
2231 2226 }
2232 2227
2233 2228
2234 2229 /*
2235 2230 * hubd_setdevconfig
2236 2231 * set the device addrs on this port
2237 2232 */
2238 2233 static void
2239 2234 hubd_setdevconfig(hubd_t *hubd, usb_port_t port)
2240 2235 {
2241 2236 int rval;
2242 2237 usb_cr_t completion_reason;
2243 2238 usb_cb_flags_t cb_flags;
2244 2239 usb_pipe_handle_t ph;
2245 2240 dev_info_t *child_dip = NULL;
2246 2241 usba_device_t *usba_device = NULL;
2247 2242 uint16_t config_value;
2248 2243
2249 2244 USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
2250 2245 "hubd_setdevconfig: port=%d", port);
2251 2246
2252 2247 ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
2253 2248
2254 2249 child_dip = hubd->h_children_dips[port];
2255 2250 usba_device = hubd->h_usba_devices[port];
2256 2251 config_value = hubd->h_usba_devices[port]->usb_cfg_value;
2257 2252 mutex_exit(HUBD_MUTEX(hubd));
2258 2253
2259 2254 /* open the default control pipe */
2260 2255 if ((rval = usb_pipe_open(child_dip, NULL, NULL,
2261 2256 USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, &ph)) ==
2262 2257 USB_SUCCESS) {
2263 2258
2264 2259 /* Set the default configuration of the device */
2265 2260 if ((rval = usb_pipe_sync_ctrl_xfer(child_dip, ph,
2266 2261 USB_DEV_REQ_HOST_TO_DEV,
2267 2262 USB_REQ_SET_CFG, /* bRequest */
2268 2263 config_value, /* wValue */
2269 2264 0, /* wIndex */
2270 2265 0, /* wLength */
2271 2266 NULL, 0,
2272 2267 &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
2273 2268 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
2274 2269 "hubd_setdevconfig: set device config failed: "
2275 2270 "cr=%d cb_fl=0x%x rval=%d",
2276 2271 completion_reason, cb_flags, rval);
2277 2272 }
2278 2273 /*
2279 2274 * After setting the configuration, we make this default
2280 2275 * control pipe persistent, so that it gets re-opened
2281 2276 * on posting a connect event
2282 2277 */
2283 2278 usba_persistent_pipe_close(usba_device);
2284 2279 } else {
2285 2280 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
2286 2281 "pipe open fails: rval=%d", rval);
2287 2282 }
2288 2283 mutex_enter(HUBD_MUTEX(hubd));
2289 2284 }
2290 2285
2291 2286
2292 2287 /*ARGSUSED*/
2293 2288 static int
2294 2289 hubd_check_disconnected_ports(dev_info_t *dip, void *arg)
2295 2290 {
2296 2291 int circ;
2297 2292 usb_port_t port;
2298 2293 hubd_t *hubd;
2299 2294 major_t hub_major = ddi_name_to_major("hubd");
2300 2295 major_t hwahc_major = ddi_name_to_major("hwahc");
2301 2296 major_t usbmid_major = ddi_name_to_major("usb_mid");
2302 2297
2303 2298 /*
2304 2299 * make sure dip is a usb hub, major of root hub is HCD
2305 2300 * major
2306 2301 */
2307 2302 if (!usba_is_root_hub(dip)) {
2308 2303 if (ddi_driver_major(dip) == usbmid_major) {
2309 2304 /*
2310 2305 * need to walk the children since it might be a
2311 2306 * HWA device
2312 2307 */
2313 2308
2314 2309 return (DDI_WALK_CONTINUE);
2315 2310 }
2316 2311
2317 2312 /* TODO: DWA device may also need special handling */
2318 2313
2319 2314 if (((ddi_driver_major(dip) != hub_major) &&
2320 2315 (ddi_driver_major(dip) != hwahc_major)) ||
2321 2316 !i_ddi_devi_attached(dip)) {
2322 2317
2323 2318 return (DDI_WALK_PRUNECHILD);
2324 2319 }
2325 2320 }
2326 2321
2327 2322 hubd = hubd_get_soft_state(dip);
2328 2323 if (hubd == NULL) {
2329 2324
2330 2325 return (DDI_WALK_PRUNECHILD);
2331 2326 }
2332 2327
2333 2328 /* walk child list and remove nodes with flag DEVI_DEVICE_REMOVED */
2334 2329 ndi_devi_enter(dip, &circ);
2335 2330
2336 2331 if (ddi_driver_major(dip) != hwahc_major) {
2337 2332 /* for normal usb hub or root hub */
2338 2333 mutex_enter(HUBD_MUTEX(hubd));
2339 2334 for (port = 1; port <= hubd->h_hub_descr.bNbrPorts; port++) {
2340 2335 dev_info_t *cdip = hubd->h_children_dips[port];
2341 2336
2342 2337 if (cdip == NULL || DEVI_IS_DEVICE_REMOVED(cdip) == 0) {
2343 2338
2344 2339 continue;
2345 2340 }
2346 2341
2347 2342 (void) hubd_delete_child(hubd, port, NDI_DEVI_REMOVE,
2348 2343 B_TRUE);
2349 2344 }
2350 2345 mutex_exit(HUBD_MUTEX(hubd));
2351 2346 } else {
2352 2347 /* for HWA */
2353 2348 if (hubd->h_cleanup_child != NULL) {
2354 2349 if (hubd->h_cleanup_child(dip) != USB_SUCCESS) {
2355 2350 ndi_devi_exit(dip, circ);
2356 2351
2357 2352 return (DDI_WALK_PRUNECHILD);
2358 2353 }
2359 2354 } else {
2360 2355 ndi_devi_exit(dip, circ);
2361 2356
2362 2357 return (DDI_WALK_PRUNECHILD);
2363 2358 }
2364 2359 }
2365 2360
2366 2361 ndi_devi_exit(dip, circ);
2367 2362
2368 2363 /* skip siblings of root hub */
2369 2364 if (usba_is_root_hub(dip)) {
2370 2365
2371 2366 return (DDI_WALK_PRUNESIB);
2372 2367 }
2373 2368
2374 2369 return (DDI_WALK_CONTINUE);
2375 2370 }
2376 2371
2377 2372
2378 2373 /*
2379 2374 * this thread will walk all children under the root hub for this
2380 2375 * USB bus instance and attempt to remove them
2381 2376 */
2382 2377 static void
2383 2378 hubd_root_hub_cleanup_thread(void *arg)
2384 2379 {
2385 2380 int circ;
2386 2381 hubd_t *root_hubd = (hubd_t *)arg;
2387 2382 dev_info_t *rh_dip = root_hubd->h_dip;
2388 2383 #ifndef __lock_lint
2389 2384 callb_cpr_t cprinfo;
2390 2385
2391 2386 CALLB_CPR_INIT(&cprinfo, HUBD_MUTEX(root_hubd), callb_generic_cpr,
2392 2387 "USB root hub");
2393 2388 #endif
2394 2389
2395 2390 for (;;) {
2396 2391 /* don't race with detach */
2397 2392 ndi_hold_devi(rh_dip);
2398 2393
2399 2394 mutex_enter(HUBD_MUTEX(root_hubd));
2400 2395 root_hubd->h_cleanup_needed = 0;
2401 2396 mutex_exit(HUBD_MUTEX(root_hubd));
2402 2397
2403 2398 (void) devfs_clean(rh_dip, NULL, 0);
2404 2399
2405 2400 ndi_devi_enter(ddi_get_parent(rh_dip), &circ);
2406 2401 ddi_walk_devs(rh_dip, hubd_check_disconnected_ports,
2407 2402 NULL);
2408 2403 #ifdef __lock_lint
2409 2404 (void) hubd_check_disconnected_ports(rh_dip, NULL);
2410 2405 #endif
2411 2406 ndi_devi_exit(ddi_get_parent(rh_dip), circ);
2412 2407
2413 2408 /* quit if we are not enabled anymore */
2414 2409 mutex_enter(HUBD_MUTEX(root_hubd));
2415 2410 if ((root_hubd->h_cleanup_enabled == B_FALSE) ||
2416 2411 (root_hubd->h_cleanup_needed == B_FALSE)) {
2417 2412 root_hubd->h_cleanup_active = B_FALSE;
2418 2413 mutex_exit(HUBD_MUTEX(root_hubd));
2419 2414 ndi_rele_devi(rh_dip);
2420 2415
2421 2416 break;
2422 2417 }
2423 2418 mutex_exit(HUBD_MUTEX(root_hubd));
2424 2419 ndi_rele_devi(rh_dip);
2425 2420
2426 2421 #ifndef __lock_lint
2427 2422 mutex_enter(HUBD_MUTEX(root_hubd));
2428 2423 CALLB_CPR_SAFE_BEGIN(&cprinfo);
2429 2424 mutex_exit(HUBD_MUTEX(root_hubd));
2430 2425
2431 2426 delay(drv_usectohz(hubd_dip_cleanup_delay));
2432 2427
2433 2428 mutex_enter(HUBD_MUTEX(root_hubd));
2434 2429 CALLB_CPR_SAFE_END(&cprinfo, HUBD_MUTEX(root_hubd));
2435 2430 mutex_exit(HUBD_MUTEX(root_hubd));
2436 2431 #endif
2437 2432 }
2438 2433
2439 2434 #ifndef __lock_lint
2440 2435 mutex_enter(HUBD_MUTEX(root_hubd));
2441 2436 CALLB_CPR_EXIT(&cprinfo);
2442 2437 #endif
2443 2438 }
2444 2439
2445 2440
2446 2441 void
2447 2442 hubd_schedule_cleanup(dev_info_t *rh_dip)
2448 2443 {
2449 2444 hubd_t *root_hubd;
2450 2445
2451 2446 /*
2452 2447 * The usb_root_hub_dip pointer for the child hub of the WUSB
2453 2448 * wire adapter class device points to the wire adapter, not
2454 2449 * the root hub. Need to find the real root hub dip so that
2455 2450 * the cleanup thread only starts from the root hub.
2456 2451 */
2457 2452 while (!usba_is_root_hub(rh_dip)) {
2458 2453 root_hubd = hubd_get_soft_state(rh_dip);
2459 2454 if (root_hubd != NULL) {
2460 2455 rh_dip = root_hubd->h_usba_device->usb_root_hub_dip;
2461 2456 if (rh_dip == NULL) {
2462 2457 USB_DPRINTF_L2(DPRINT_MASK_ATTA,
2463 2458 root_hubd->h_log_handle,
2464 2459 "hubd_schedule_cleanup: null rh dip");
2465 2460
2466 2461 return;
2467 2462 }
2468 2463 } else {
2469 2464 USB_DPRINTF_L2(DPRINT_MASK_ATTA,
2470 2465 root_hubd->h_log_handle,
2471 2466 "hubd_schedule_cleanup: cannot find root hub");
2472 2467
2473 2468 return;
2474 2469 }
2475 2470 }
2476 2471 root_hubd = hubd_get_soft_state(rh_dip);
2477 2472
2478 2473 mutex_enter(HUBD_MUTEX(root_hubd));
2479 2474 root_hubd->h_cleanup_needed = B_TRUE;
2480 2475 if (root_hubd->h_cleanup_enabled && !(root_hubd->h_cleanup_active)) {
2481 2476 root_hubd->h_cleanup_active = B_TRUE;
2482 2477 mutex_exit(HUBD_MUTEX(root_hubd));
2483 2478 (void) thread_create(NULL, 0,
2484 2479 hubd_root_hub_cleanup_thread,
2485 2480 (void *)root_hubd, 0, &p0, TS_RUN,
2486 2481 minclsyspri);
2487 2482 } else {
2488 2483 mutex_exit(HUBD_MUTEX(root_hubd));
2489 2484 }
2490 2485 }
2491 2486
2492 2487
2493 2488 /*
2494 2489 * hubd_restore_device_state:
2495 2490 * - set config for the hub
2496 2491 * - power cycle all the ports
2497 2492 * - for each port that was connected
2498 2493 * - reset port
2499 2494 * - assign addrs to the device on this port
2500 2495 * - restart polling
2501 2496 * - reset suspend flag
2502 2497 */
2503 2498 static void
2504 2499 hubd_restore_device_state(dev_info_t *dip, hubd_t *hubd)
2505 2500 {
2506 2501 int rval;
2507 2502 int retry;
2508 2503 uint_t hub_prev_state;
2509 2504 usb_port_t port;
2510 2505 uint16_t status;
2511 2506 uint16_t change;
2512 2507 dev_info_t *ch_dip;
2513 2508 boolean_t ehci_root_hub;
2514 2509
2515 2510 USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
2516 2511 "hubd_restore_device_state:");
2517 2512
2518 2513 mutex_enter(HUBD_MUTEX(hubd));
2519 2514 hub_prev_state = hubd->h_dev_state;
2520 2515 ASSERT(hub_prev_state != USB_DEV_PWRED_DOWN);
2521 2516
2522 2517 /* First bring the device to full power */
2523 2518 (void) hubd_pm_busy_component(hubd, dip, 0);
2524 2519 mutex_exit(HUBD_MUTEX(hubd));
2525 2520
2526 2521 (void) pm_raise_power(dip, 0, USB_DEV_OS_FULL_PWR);
2527 2522
2528 2523 if (!usba_is_root_hub(dip) &&
2529 2524 (usb_check_same_device(dip, hubd->h_log_handle, USB_LOG_L0,
2530 2525 DPRINT_MASK_HOTPLUG,
2531 2526 USB_CHK_BASIC|USB_CHK_CFG, NULL) != USB_SUCCESS)) {
2532 2527
2533 2528 /* change the device state to disconnected */
2534 2529 mutex_enter(HUBD_MUTEX(hubd));
2535 2530 hubd->h_dev_state = USB_DEV_DISCONNECTED;
2536 2531 (void) hubd_pm_idle_component(hubd, dip, 0);
2537 2532 mutex_exit(HUBD_MUTEX(hubd));
2538 2533
2539 2534 return;
2540 2535 }
2541 2536
2542 2537 ehci_root_hub = (strcmp(ddi_driver_name(dip), "ehci") == 0);
2543 2538
2544 2539 mutex_enter(HUBD_MUTEX(hubd));
2545 2540 /* First turn off all port power */
2546 2541 rval = hubd_disable_all_port_power(hubd);
2547 2542 if (rval != USB_SUCCESS) {
2548 2543 USB_DPRINTF_L3(DPRINT_MASK_ATTA, hubd->h_log_handle,
2549 2544 "hubd_restore_device_state:"
2550 2545 "turning off port power failed");
2551 2546 }
2552 2547
2553 2548 /* Settling time before turning on again */
2554 2549 mutex_exit(HUBD_MUTEX(hubd));
2555 2550 delay(drv_usectohz(hubd_device_delay / 100));
2556 2551 mutex_enter(HUBD_MUTEX(hubd));
2557 2552
2558 2553 /* enable power on all ports so we can see connects */
2559 2554 if (hubd_enable_all_port_power(hubd) != USB_SUCCESS) {
2560 2555 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
2561 2556 "hubd_restore_device_state: turn on port power failed");
2562 2557
2563 2558 /* disable whatever was enabled */
2564 2559 (void) hubd_disable_all_port_power(hubd);
2565 2560
2566 2561 (void) hubd_pm_idle_component(hubd, dip, 0);
2567 2562 mutex_exit(HUBD_MUTEX(hubd));
2568 2563
2569 2564 return;
2570 2565 }
2571 2566
2572 2567 /*
2573 2568 * wait at least 3 frames before accessing devices
2574 2569 * (note that delay's minimal time is one clock tick which
2575 2570 * is 10ms unless hires_tick has been changed)
2576 2571 */
2577 2572 mutex_exit(HUBD_MUTEX(hubd));
2578 2573 delay(drv_usectohz(10000));
2579 2574 mutex_enter(HUBD_MUTEX(hubd));
2580 2575
2581 2576 hubd->h_dev_state = USB_DEV_HUB_STATE_RECOVER;
2582 2577
2583 2578 for (port = 1; port <= hubd->h_hub_descr.bNbrPorts; port++) {
2584 2579 USB_DPRINTF_L3(DPRINT_MASK_ATTA, hubd->h_log_handle,
2585 2580 "hubd_restore_device_state: port=%d", port);
2586 2581
2587 2582 /*
2588 2583 * the childen_dips list may have dips that have been
2589 2584 * already deallocated. we only get a post_detach notification
2590 2585 * but not a destroy notification
2591 2586 */
2592 2587 ch_dip = hubd->h_children_dips[port];
2593 2588 if (ch_dip) {
2594 2589 /* get port status */
2595 2590 (void) hubd_determine_port_status(hubd, port,
2596 2591 &status, &change, PORT_CHANGE_CSC);
2597 2592
2598 2593 /* check if it is truly connected */
2599 2594 if (status & PORT_STATUS_CCS) {
2600 2595 /*
2601 2596 * Now reset port and assign the device
2602 2597 * its original address
2603 2598 */
2604 2599 retry = 0;
2605 2600 do {
2606 2601 (void) hubd_reset_port(hubd, port);
2607 2602
2608 2603 /* required for ppx */
2609 2604 (void) hubd_enable_port(hubd, port);
2610 2605
2611 2606 if (retry) {
2612 2607 mutex_exit(HUBD_MUTEX(hubd));
2613 2608 delay(drv_usectohz(
2614 2609 hubd_device_delay/2));
2615 2610 mutex_enter(HUBD_MUTEX(hubd));
2616 2611 }
2617 2612
2618 2613 rval = hubd_setdevaddr(hubd, port);
2619 2614 retry++;
2620 2615 } while ((rval != USB_SUCCESS) &&
2621 2616 (retry < hubd_retry_enumerate));
2622 2617
2623 2618 hubd_setdevconfig(hubd, port);
2624 2619
2625 2620 if (hub_prev_state == USB_DEV_DISCONNECTED) {
2626 2621 /* post a connect event */
2627 2622 mutex_exit(HUBD_MUTEX(hubd));
2628 2623 hubd_post_event(hubd, port,
2629 2624 USBA_EVENT_TAG_HOT_INSERTION);
2630 2625 mutex_enter(HUBD_MUTEX(hubd));
2631 2626 } else {
2632 2627 /*
2633 2628 * Since we have this device connected
2634 2629 * mark it reinserted to prevent
2635 2630 * cleanup thread from stepping in.
2636 2631 */
2637 2632 mutex_exit(HUBD_MUTEX(hubd));
2638 2633 mutex_enter(&(DEVI(ch_dip)->devi_lock));
2639 2634 DEVI_SET_DEVICE_REINSERTED(ch_dip);
2640 2635 mutex_exit(&(DEVI(ch_dip)->devi_lock));
2641 2636
2642 2637 /*
2643 2638 * reopen pipes for children for
2644 2639 * their DDI_RESUME
2645 2640 */
2646 2641 rval = usba_persistent_pipe_open(
2647 2642 usba_get_usba_device(ch_dip));
2648 2643 mutex_enter(HUBD_MUTEX(hubd));
2649 2644 ASSERT(rval == USB_SUCCESS);
2650 2645 }
2651 2646 } else {
2652 2647 /*
2653 2648 * Mark this dip for deletion as the device
2654 2649 * is not physically present, and schedule
2655 2650 * cleanup thread upon post resume
2656 2651 */
2657 2652 mutex_exit(HUBD_MUTEX(hubd));
2658 2653
2659 2654 USB_DPRINTF_L2(DPRINT_MASK_ATTA,
2660 2655 hubd->h_log_handle,
2661 2656 "hubd_restore_device_state: "
2662 2657 "dip=%p on port=%d marked for cleanup",
2663 2658 (void *)ch_dip, port);
2664 2659 mutex_enter(&(DEVI(ch_dip)->devi_lock));
2665 2660 DEVI_SET_DEVICE_REMOVED(ch_dip);
2666 2661 mutex_exit(&(DEVI(ch_dip)->devi_lock));
2667 2662
2668 2663 mutex_enter(HUBD_MUTEX(hubd));
2669 2664 }
2670 2665 } else if (ehci_root_hub) {
2671 2666 /* get port status */
2672 2667 (void) hubd_determine_port_status(hubd, port,
2673 2668 &status, &change, PORT_CHANGE_CSC);
2674 2669
2675 2670 /* check if it is truly connected */
2676 2671 if (status & PORT_STATUS_CCS) {
2677 2672 /*
2678 2673 * reset the port to find out if we have
2679 2674 * 2.0 device connected or 1.X. A 2.0
2680 2675 * device will still be seen as connected,
2681 2676 * while a 1.X device will switch over to
2682 2677 * the companion controller.
2683 2678 */
2684 2679 (void) hubd_reset_port(hubd, port);
2685 2680
2686 2681 (void) hubd_determine_port_status(hubd, port,
2687 2682 &status, &change, PORT_CHANGE_CSC);
2688 2683
2689 2684 if (status &
2690 2685 (PORT_STATUS_CCS | PORT_STATUS_HSDA)) {
2691 2686 /*
2692 2687 * We have a USB 2.0 device
2693 2688 * connected. Power cycle this port
2694 2689 * so that hotplug thread can
2695 2690 * enumerate this device.
2696 2691 */
2697 2692 (void) hubd_toggle_port(hubd, port);
2698 2693 } else {
2699 2694 USB_DPRINTF_L2(DPRINT_MASK_ATTA,
2700 2695 hubd->h_log_handle,
2701 2696 "hubd_restore_device_state: "
2702 2697 "device on port %d switched over",
2703 2698 port);
2704 2699 }
2705 2700 }
2706 2701
2707 2702 }
2708 2703 }
2709 2704
2710 2705
2711 2706 /* if the device had remote wakeup earlier, enable it again */
2712 2707 if (hubd->h_hubpm->hubp_wakeup_enabled) {
2713 2708 mutex_exit(HUBD_MUTEX(hubd));
2714 2709 (void) usb_handle_remote_wakeup(hubd->h_dip,
2715 2710 USB_REMOTE_WAKEUP_ENABLE);
2716 2711 mutex_enter(HUBD_MUTEX(hubd));
2717 2712 }
2718 2713
2719 2714 hubd->h_dev_state = USB_DEV_ONLINE;
2720 2715 hubd_start_polling(hubd, 0);
2721 2716 (void) hubd_pm_idle_component(hubd, dip, 0);
2722 2717 mutex_exit(HUBD_MUTEX(hubd));
2723 2718 }
2724 2719
2725 2720
2726 2721 /*
2727 2722 * hubd_cleanup:
2728 2723 * cleanup hubd and deallocate. this function is called for
2729 2724 * handling attach failures and detaching including dynamic
2730 2725 * reconfiguration. If called from attaching, it must clean
2731 2726 * up the whole thing and return success.
2732 2727 */
2733 2728 /*ARGSUSED*/
2734 2729 static int
2735 2730 hubd_cleanup(dev_info_t *dip, hubd_t *hubd)
2736 2731 {
2737 2732 int circ, rval, old_dev_state;
2738 2733 hub_power_t *hubpm;
2739 2734 #ifdef DEBUG
2740 2735 usb_port_t port;
2741 2736 #endif
2742 2737
2743 2738 USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
2744 2739 "hubd_cleanup:");
2745 2740
2746 2741 if ((hubd->h_init_state & HUBD_LOCKS_DONE) == 0) {
2747 2742 goto done;
2748 2743 }
2749 2744
2750 2745 /* ensure we are the only one active */
2751 2746 ndi_devi_enter(dip, &circ);
2752 2747
2753 2748 mutex_enter(HUBD_MUTEX(hubd));
2754 2749
2755 2750 /* Cleanup failure is only allowed if called from detach */
2756 2751 if (DEVI_IS_DETACHING(dip)) {
2757 2752 dev_info_t *rh_dip = hubd->h_usba_device->usb_root_hub_dip;
2758 2753
2759 2754 /*
2760 2755 * We are being called from detach.
2761 2756 * Fail immediately if the hotplug thread is running
2762 2757 * else set the dev_state to disconnected so that
2763 2758 * hotplug thread just exits without doing anything.
2764 2759 */
2765 2760 if (hubd->h_bus_ctls || hubd->h_bus_pwr ||
2766 2761 hubd->h_hotplug_thread) {
2767 2762 mutex_exit(HUBD_MUTEX(hubd));
2768 2763 ndi_devi_exit(dip, circ);
2769 2764
2770 2765 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
2771 2766 "hubd_cleanup: hotplug thread/bus ctl active "
2772 2767 "- failing detach");
2773 2768
2774 2769 return (USB_FAILURE);
2775 2770 }
2776 2771
2777 2772 /*
2778 2773 * if the deathrow thread is still active or about
2779 2774 * to become active, fail detach
2780 2775 * the roothup can only be detached if nexus drivers
2781 2776 * are unloaded or explicitly offlined
2782 2777 */
2783 2778 if (rh_dip == dip) {
2784 2779 if (hubd->h_cleanup_needed ||
2785 2780 hubd->h_cleanup_active) {
2786 2781 mutex_exit(HUBD_MUTEX(hubd));
2787 2782 ndi_devi_exit(dip, circ);
2788 2783
2789 2784 USB_DPRINTF_L2(DPRINT_MASK_ATTA,
2790 2785 hubd->h_log_handle,
2791 2786 "hubd_cleanup: deathrow still active?"
2792 2787 "- failing detach");
2793 2788
2794 2789 return (USB_FAILURE);
2795 2790 }
2796 2791 }
2797 2792 }
2798 2793
2799 2794 old_dev_state = hubd->h_dev_state;
2800 2795 hubd->h_dev_state = USB_DEV_DISCONNECTED;
2801 2796
2802 2797 USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
2803 2798 "hubd_cleanup: stop polling");
2804 2799 hubd_close_intr_pipe(hubd);
2805 2800
2806 2801 ASSERT((hubd->h_bus_ctls || hubd->h_bus_pwr ||
2807 2802 hubd->h_hotplug_thread) == 0);
2808 2803 mutex_exit(HUBD_MUTEX(hubd));
2809 2804
2810 2805 /*
2811 2806 * deallocate events, if events are still registered
2812 2807 * (ie. children still attached) then we have to fail the detach
2813 2808 */
2814 2809 if (hubd->h_ndi_event_hdl) {
2815 2810
2816 2811 rval = ndi_event_free_hdl(hubd->h_ndi_event_hdl);
2817 2812 if (DEVI_IS_ATTACHING(dip)) {
2818 2813
2819 2814 /* It must return success if attaching. */
2820 2815 ASSERT(rval == NDI_SUCCESS);
2821 2816
2822 2817 } else if (rval != NDI_SUCCESS) {
2823 2818
2824 2819 USB_DPRINTF_L2(DPRINT_MASK_ALL, hubd->h_log_handle,
2825 2820 "hubd_cleanup: ndi_event_free_hdl failed");
2826 2821 ndi_devi_exit(dip, circ);
2827 2822
2828 2823 return (USB_FAILURE);
2829 2824
2830 2825 }
2831 2826 }
2832 2827
2833 2828 mutex_enter(HUBD_MUTEX(hubd));
2834 2829
2835 2830 if (hubd->h_init_state & HUBD_CHILDREN_CREATED) {
2836 2831 #ifdef DEBUG
2837 2832 for (port = 1; port <= hubd->h_hub_descr.bNbrPorts; port++) {
2838 2833 ASSERT(hubd->h_usba_devices[port] == NULL);
2839 2834 ASSERT(hubd->h_children_dips[port] == NULL);
2840 2835 }
2841 2836 #endif
2842 2837 kmem_free(hubd->h_children_dips, hubd->h_cd_list_length);
2843 2838 kmem_free(hubd->h_usba_devices, hubd->h_cd_list_length);
2844 2839 }
2845 2840
2846 2841 /*
2847 2842 * Disable the event callbacks first, after this point, event
2848 2843 * callbacks will never get called. Note we shouldn't hold
2849 2844 * mutex while unregistering events because there may be a
2850 2845 * competing event callback thread. Event callbacks are done
2851 2846 * with ndi mutex held and this can cause a potential deadlock.
2852 2847 * Note that cleanup can't fail after deregistration of events.
2853 2848 */
2854 2849 if (hubd->h_init_state & HUBD_EVENTS_REGISTERED) {
2855 2850 mutex_exit(HUBD_MUTEX(hubd));
2856 2851 usb_unregister_event_cbs(dip, &hubd_events);
2857 2852 hubd_unregister_cpr_callback(hubd);
2858 2853 mutex_enter(HUBD_MUTEX(hubd));
2859 2854 }
2860 2855
2861 2856 /* restore the old dev state so that device can be put into low power */
2862 2857 hubd->h_dev_state = old_dev_state;
2863 2858 hubpm = hubd->h_hubpm;
2864 2859
2865 2860 if ((hubpm) && (hubd->h_dev_state != USB_DEV_DISCONNECTED)) {
2866 2861 (void) hubd_pm_busy_component(hubd, dip, 0);
2867 2862 mutex_exit(HUBD_MUTEX(hubd));
2868 2863 if (hubd->h_hubpm->hubp_wakeup_enabled) {
2869 2864 /*
2870 2865 * Bring the hub to full power before
2871 2866 * issuing the disable remote wakeup command
2872 2867 */
2873 2868 (void) pm_raise_power(dip, 0, USB_DEV_OS_FULL_PWR);
2874 2869
2875 2870 if ((rval = usb_handle_remote_wakeup(hubd->h_dip,
2876 2871 USB_REMOTE_WAKEUP_DISABLE)) != USB_SUCCESS) {
2877 2872 USB_DPRINTF_L2(DPRINT_MASK_PM,
2878 2873 hubd->h_log_handle,
2879 2874 "hubd_cleanup: disable remote wakeup "
2880 2875 "fails=%d", rval);
2881 2876 }
2882 2877 }
2883 2878
2884 2879 (void) pm_lower_power(hubd->h_dip, 0, USB_DEV_OS_PWR_OFF);
2885 2880
2886 2881 mutex_enter(HUBD_MUTEX(hubd));
2887 2882 (void) hubd_pm_idle_component(hubd, dip, 0);
2888 2883 }
2889 2884
2890 2885 if (hubpm) {
2891 2886 if (hubpm->hubp_child_pwrstate) {
2892 2887 kmem_free(hubpm->hubp_child_pwrstate,
2893 2888 MAX_PORTS + 1);
2894 2889 }
2895 2890 kmem_free(hubpm, sizeof (hub_power_t));
2896 2891 }
2897 2892 mutex_exit(HUBD_MUTEX(hubd));
2898 2893
2899 2894 USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
2900 2895 "hubd_cleanup: freeing space");
2901 2896
2902 2897 if (hubd->h_init_state & HUBD_HUBDI_REGISTERED) {
2903 2898 rval = usba_hubdi_unregister(dip);
2904 2899 ASSERT(rval == USB_SUCCESS);
2905 2900 }
2906 2901
2907 2902 if (hubd->h_init_state & HUBD_LOCKS_DONE) {
2908 2903 mutex_destroy(HUBD_MUTEX(hubd));
2909 2904 cv_destroy(&hubd->h_cv_reset_port);
2910 2905 cv_destroy(&hubd->h_cv_hotplug_dev);
2911 2906 }
2912 2907
2913 2908 ndi_devi_exit(dip, circ);
2914 2909
2915 2910 if (hubd->h_init_state & HUBD_MINOR_NODE_CREATED) {
2916 2911 ddi_remove_minor_node(dip, NULL);
2917 2912 }
2918 2913
2919 2914 if (usba_is_root_hub(dip)) {
2920 2915 usb_pipe_close(dip, hubd->h_default_pipe,
2921 2916 USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, NULL, NULL);
2922 2917 }
2923 2918
2924 2919 done:
2925 2920 if (hubd->h_ancestry_str) {
2926 2921 kmem_free(hubd->h_ancestry_str, HUBD_APID_NAMELEN);
2927 2922 }
2928 2923
2929 2924 usb_client_detach(dip, hubd->h_dev_data);
2930 2925
2931 2926 usb_free_log_hdl(hubd->h_log_handle);
2932 2927
2933 2928 if (!usba_is_root_hub(dip)) {
2934 2929 ddi_soft_state_free(hubd_statep, ddi_get_instance(dip));
2935 2930 }
2936 2931
2937 2932 ddi_prop_remove_all(dip);
2938 2933
2939 2934 return (USB_SUCCESS);
2940 2935 }
2941 2936
2942 2937
2943 2938 /*
2944 2939 * hubd_determine_port_connection:
2945 2940 * Determine which port is in connect status but does not
2946 2941 * have connect status change bit set, and mark port change
2947 2942 * bit accordingly.
2948 2943 * This function is applied during hub attach time.
2949 2944 */
2950 2945 static usb_port_mask_t
2951 2946 hubd_determine_port_connection(hubd_t *hubd)
2952 2947 {
2953 2948 usb_port_t port;
2954 2949 usb_hub_descr_t *hub_descr;
2955 2950 uint16_t status;
2956 2951 uint16_t change;
2957 2952 usb_port_mask_t port_change = 0;
2958 2953
2959 2954 ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
2960 2955
2961 2956 hub_descr = &hubd->h_hub_descr;
2962 2957
2963 2958 for (port = 1; port <= hub_descr->bNbrPorts; port++) {
2964 2959
2965 2960 (void) hubd_determine_port_status(hubd, port, &status,
2966 2961 &change, 0);
2967 2962
2968 2963 /* Check if port is in connect status */
2969 2964 if (!(status & PORT_STATUS_CCS)) {
2970 2965
2971 2966 continue;
2972 2967 }
2973 2968
2974 2969 /*
2975 2970 * Check if port Connect Status Change bit has been set.
2976 2971 * If already set, the connection will be handled by
2977 2972 * intr polling callback, not during attach.
2978 2973 */
2979 2974 if (change & PORT_CHANGE_CSC) {
2980 2975
2981 2976 continue;
2982 2977 }
2983 2978
2984 2979 port_change |= 1 << port;
2985 2980 }
2986 2981
2987 2982 return (port_change);
2988 2983 }
2989 2984
2990 2985
2991 2986 /*
2992 2987 * hubd_check_ports:
2993 2988 * - get hub descriptor
2994 2989 * - check initial port status
2995 2990 * - enable power on all ports
2996 2991 * - enable polling on ep1
2997 2992 */
2998 2993 static int
2999 2994 hubd_check_ports(hubd_t *hubd)
3000 2995 {
3001 2996 int rval;
3002 2997 usb_port_mask_t port_change = 0;
3003 2998 hubd_hotplug_arg_t *arg;
3004 2999
3005 3000 ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
3006 3001
3007 3002 USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
3008 3003 "hubd_check_ports: addr=0x%x", usb_get_addr(hubd->h_dip));
3009 3004
3010 3005 /*
3011 3006 * First turn off all port power
3012 3007 */
3013 3008 if ((rval = hubd_disable_all_port_power(hubd)) != USB_SUCCESS) {
3014 3009
3015 3010 /* disable whatever was enabled */
3016 3011 (void) hubd_disable_all_port_power(hubd);
3017 3012
3018 3013 return (rval);
3019 3014 }
3020 3015
3021 3016 /*
3022 3017 * do not switch on immediately (instantly on root hub)
3023 3018 * and allow time to settle
3024 3019 */
3025 3020 mutex_exit(HUBD_MUTEX(hubd));
3026 3021 delay(drv_usectohz(10000));
3027 3022 mutex_enter(HUBD_MUTEX(hubd));
3028 3023
3029 3024 /*
3030 3025 * enable power on all ports so we can see connects
3031 3026 */
3032 3027 if ((rval = hubd_enable_all_port_power(hubd)) != USB_SUCCESS) {
3033 3028 /* disable whatever was enabled */
3034 3029 (void) hubd_disable_all_port_power(hubd);
3035 3030
3036 3031 return (rval);
3037 3032 }
3038 3033
3039 3034 /* wait at least 3 frames before accessing devices */
3040 3035 mutex_exit(HUBD_MUTEX(hubd));
3041 3036 delay(drv_usectohz(10000));
3042 3037 mutex_enter(HUBD_MUTEX(hubd));
3043 3038
3044 3039 /*
3045 3040 * allocate arrays for saving the dips of each child per port
3046 3041 *
3047 3042 * ports go from 1 - n, allocate 1 more entry
3048 3043 */
3049 3044 hubd->h_cd_list_length =
3050 3045 (sizeof (dev_info_t **)) * (hubd->h_hub_descr.bNbrPorts + 1);
3051 3046
3052 3047 hubd->h_children_dips = (dev_info_t **)kmem_zalloc(
3053 3048 hubd->h_cd_list_length, KM_SLEEP);
3054 3049 hubd->h_usba_devices = (usba_device_t **)kmem_zalloc(
3055 3050 hubd->h_cd_list_length, KM_SLEEP);
3056 3051
3057 3052 hubd->h_init_state |= HUBD_CHILDREN_CREATED;
3058 3053
3059 3054 mutex_exit(HUBD_MUTEX(hubd));
3060 3055 arg = (hubd_hotplug_arg_t *)kmem_zalloc(
3061 3056 sizeof (hubd_hotplug_arg_t), KM_SLEEP);
3062 3057 mutex_enter(HUBD_MUTEX(hubd));
3063 3058
3064 3059 if ((rval = hubd_open_intr_pipe(hubd)) != USB_SUCCESS) {
3065 3060 kmem_free(arg, sizeof (hubd_hotplug_arg_t));
3066 3061
3067 3062 return (rval);
3068 3063 }
3069 3064
3070 3065 hubd_start_polling(hubd, 0);
3071 3066
3072 3067 /*
3073 3068 * Some hub devices, like the embedded hub in the CKS ErgoMagic
3074 3069 * keyboard, may only have connection status bit set, but not
3075 3070 * have connect status change bit set when a device has been
3076 3071 * connected to its downstream port before the hub is enumerated.
3077 3072 * Then when the hub is in enumeration, the devices connected to
3078 3073 * it cannot be detected by the intr pipe and won't be enumerated.
3079 3074 * We need to check such situation here and enumerate the downstream
3080 3075 * devices for such hubs.
3081 3076 */
3082 3077 port_change = hubd_determine_port_connection(hubd);
3083 3078
3084 3079 if (port_change) {
3085 3080 hubd_pm_busy_component(hubd, hubd->h_dip, 0);
3086 3081
3087 3082 arg->hubd = hubd;
3088 3083 arg->hotplug_during_attach = B_TRUE;
3089 3084 hubd->h_port_change |= port_change;
3090 3085
3091 3086 USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
3092 3087 "hubd_check_ports: port change=0x%x, need to connect",
3093 3088 hubd->h_port_change);
3094 3089
3095 3090 if (usb_async_req(hubd->h_dip, hubd_hotplug_thread,
3096 3091 (void *)arg, 0) == USB_SUCCESS) {
3097 3092 hubd->h_hotplug_thread++;
3098 3093 } else {
3099 3094 /* mark this device as idle */
3100 3095 hubd_pm_idle_component(hubd, hubd->h_dip, 0);
3101 3096 kmem_free(arg, sizeof (hubd_hotplug_arg_t));
3102 3097 }
3103 3098 } else {
3104 3099 kmem_free(arg, sizeof (hubd_hotplug_arg_t));
3105 3100 }
3106 3101
3107 3102 USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
3108 3103 "hubd_check_ports done");
3109 3104
3110 3105 return (USB_SUCCESS);
3111 3106 }
3112 3107
3113 3108
3114 3109 /*
3115 3110 * hubd_get_hub_descriptor:
3116 3111 */
3117 3112 static int
3118 3113 hubd_get_hub_descriptor(hubd_t *hubd)
3119 3114 {
3120 3115 usb_hub_descr_t *hub_descr = &hubd->h_hub_descr;
3121 3116 mblk_t *data = NULL;
3122 3117 usb_cr_t completion_reason;
3123 3118 usb_cb_flags_t cb_flags;
3124 3119 uint16_t length;
3125 3120 int rval;
3126 3121 usb_req_attrs_t attr = 0;
3127 3122
3128 3123 USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle,
3129 3124 "hubd_get_hub_descriptor:");
3130 3125
3131 3126 if ((hubd->h_dev_data->dev_descr->idVendor == USB_HUB_INTEL_VID) &&
3132 3127 (hubd->h_dev_data->dev_descr->idProduct == USB_HUB_INTEL_PID)) {
3133 3128 attr = USB_ATTRS_SHORT_XFER_OK;
3134 3129 }
3135 3130
3136 3131 ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
3137 3132 ASSERT(hubd->h_default_pipe != 0);
3138 3133
3139 3134 /* get hub descriptor length first by requesting 8 bytes only */
3140 3135 mutex_exit(HUBD_MUTEX(hubd));
3141 3136
3142 3137 if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
3143 3138 hubd->h_default_pipe,
3144 3139 HUB_CLASS_REQ_TYPE,
3145 3140 USB_REQ_GET_DESCR, /* bRequest */
3146 3141 USB_DESCR_TYPE_SETUP_HUB, /* wValue */
3147 3142 0, /* wIndex */
3148 3143 8, /* wLength */
3149 3144 &data, 0,
3150 3145 &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
3151 3146 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
3152 3147 "get hub descriptor failed: cr=%d cb_fl=0x%x rval=%d",
3153 3148 completion_reason, cb_flags, rval);
3154 3149 freemsg(data);
3155 3150 mutex_enter(HUBD_MUTEX(hubd));
3156 3151
3157 3152 return (rval);
3158 3153 }
3159 3154
3160 3155 length = *(data->b_rptr);
3161 3156
3162 3157 if (length > 8) {
3163 3158 freemsg(data);
3164 3159 data = NULL;
3165 3160
3166 3161 /* get complete hub descriptor */
3167 3162 rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
3168 3163 hubd->h_default_pipe,
3169 3164 HUB_CLASS_REQ_TYPE,
3170 3165 USB_REQ_GET_DESCR, /* bRequest */
3171 3166 USB_DESCR_TYPE_SETUP_HUB, /* wValue */
3172 3167 0, /* wIndex */
3173 3168 length, /* wLength */
3174 3169 &data, attr,
3175 3170 &completion_reason, &cb_flags, 0);
3176 3171
3177 3172 /*
3178 3173 * Hub descriptor data less than 9 bytes is not valid and
3179 3174 * may cause trouble if we use it. See USB2.0 Tab11-13.
3180 3175 */
3181 3176 if ((rval != USB_SUCCESS) || (MBLKL(data) <= 8)) {
3182 3177 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
3183 3178 "get hub descriptor failed: "
3184 3179 "cr=%d cb_fl=0x%x rval=%d, len=%ld",
3185 3180 completion_reason, cb_flags, rval,
3186 3181 (data)?MBLKL(data):0);
3187 3182 freemsg(data);
3188 3183 mutex_enter(HUBD_MUTEX(hubd));
3189 3184
3190 3185 return (rval);
3191 3186 }
3192 3187 }
3193 3188
3194 3189 mutex_enter(HUBD_MUTEX(hubd));
3195 3190
3196 3191 /* parse the hub descriptor */
3197 3192 /* only 32 ports are supported at present */
3198 3193 ASSERT(*(data->b_rptr + 2) <= 32);
3199 3194 if (usb_parse_CV_descr("cccscccccc",
3200 3195 data->b_rptr, MBLKL(data),
3201 3196 (void *)hub_descr, sizeof (usb_hub_descr_t)) == 0) {
3202 3197 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
3203 3198 "parsing hub descriptor failed");
3204 3199
3205 3200 freemsg(data);
3206 3201
3207 3202 return (USB_FAILURE);
3208 3203 }
3209 3204
3210 3205 freemsg(data);
3211 3206
3212 3207 USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
3213 3208 "rval=0x%x bNbrPorts=0x%x wHubChars=0x%x "
3214 3209 "PwrOn2PwrGood=0x%x HubContrCurrent=%dmA", rval,
3215 3210 hub_descr->bNbrPorts, hub_descr->wHubCharacteristics,
3216 3211 hub_descr->bPwrOn2PwrGood, hub_descr->bHubContrCurrent);
3217 3212
3218 3213 if (hub_descr->bNbrPorts > MAX_PORTS) {
3219 3214 USB_DPRINTF_L0(DPRINT_MASK_ATTA, hubd->h_log_handle,
3220 3215 "Hub driver supports max of %d ports on hub. "
3221 3216 "Hence using the first %d port of %d ports available",
3222 3217 MAX_PORTS, MAX_PORTS, hub_descr->bNbrPorts);
3223 3218
3224 3219 hub_descr->bNbrPorts = MAX_PORTS;
3225 3220 }
3226 3221
3227 3222 return (USB_SUCCESS);
3228 3223 }
3229 3224
3230 3225
3231 3226 /*
3232 3227 * hubd_get_hub_status_words:
3233 3228 */
3234 3229 static int
3235 3230 hubd_get_hub_status_words(hubd_t *hubd, uint16_t *status)
3236 3231 {
3237 3232 usb_cr_t completion_reason;
3238 3233 usb_cb_flags_t cb_flags;
3239 3234 mblk_t *data = NULL;
3240 3235
3241 3236 ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
3242 3237
3243 3238 mutex_exit(HUBD_MUTEX(hubd));
3244 3239
3245 3240 if (usb_pipe_sync_ctrl_xfer(hubd->h_dip, hubd->h_default_pipe,
3246 3241 HUB_CLASS_REQ_TYPE,
3247 3242 USB_REQ_GET_STATUS,
3248 3243 0,
3249 3244 0,
3250 3245 GET_STATUS_LENGTH,
3251 3246 &data, 0,
3252 3247 &completion_reason, &cb_flags, 0) != USB_SUCCESS) {
3253 3248 USB_DPRINTF_L2(DPRINT_MASK_HUB, hubd->h_log_handle,
3254 3249 "get hub status failed: cr=%d cb=0x%x",
3255 3250 completion_reason, cb_flags);
3256 3251
3257 3252 if (data) {
3258 3253 freemsg(data);
3259 3254 }
3260 3255
3261 3256 mutex_enter(HUBD_MUTEX(hubd));
3262 3257
3263 3258 return (USB_FAILURE);
3264 3259 }
3265 3260
3266 3261 mutex_enter(HUBD_MUTEX(hubd));
3267 3262
3268 3263 status[0] = (*(data->b_rptr + 1) << 8) | *(data->b_rptr);
3269 3264 status[1] = (*(data->b_rptr + 3) << 8) | *(data->b_rptr + 2);
3270 3265
3271 3266 USB_DPRINTF_L3(DPRINT_MASK_HUB, hubd->h_log_handle,
3272 3267 "hub status=0x%x change=0x%x", status[0], status[1]);
3273 3268
3274 3269 freemsg(data);
3275 3270
3276 3271 return (USB_SUCCESS);
3277 3272 }
3278 3273
3279 3274
3280 3275 /*
3281 3276 * hubd_open_intr_pipe:
3282 3277 * we read all descriptors first for curiosity and then simply
3283 3278 * open the pipe
3284 3279 */
3285 3280 static int
3286 3281 hubd_open_intr_pipe(hubd_t *hubd)
3287 3282 {
3288 3283 int rval;
3289 3284
3290 3285 USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle,
3291 3286 "hubd_open_intr_pipe:");
3292 3287
3293 3288 ASSERT(hubd->h_intr_pipe_state == HUBD_INTR_PIPE_IDLE);
3294 3289
3295 3290 hubd->h_intr_pipe_state = HUBD_INTR_PIPE_OPENING;
3296 3291 mutex_exit(HUBD_MUTEX(hubd));
3297 3292
3298 3293 if ((rval = usb_pipe_open(hubd->h_dip,
3299 3294 &hubd->h_ep1_descr, &hubd->h_pipe_policy,
3300 3295 0, &hubd->h_ep1_ph)) != USB_SUCCESS) {
3301 3296 USB_DPRINTF_L2(DPRINT_MASK_HUB, hubd->h_log_handle,
3302 3297 "open intr pipe failed (%d)", rval);
3303 3298
3304 3299 mutex_enter(HUBD_MUTEX(hubd));
3305 3300 hubd->h_intr_pipe_state = HUBD_INTR_PIPE_IDLE;
3306 3301
3307 3302 return (rval);
3308 3303 }
3309 3304
3310 3305 mutex_enter(HUBD_MUTEX(hubd));
3311 3306 hubd->h_intr_pipe_state = HUBD_INTR_PIPE_ACTIVE;
3312 3307
3313 3308 USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle,
3314 3309 "open intr pipe succeeded, ph=0x%p", (void *)hubd->h_ep1_ph);
3315 3310
3316 3311 return (USB_SUCCESS);
3317 3312 }
3318 3313
3319 3314
3320 3315 /*
3321 3316 * hubd_start_polling:
3322 3317 * start or restart the polling
3323 3318 */
3324 3319 static void
3325 3320 hubd_start_polling(hubd_t *hubd, int always)
3326 3321 {
3327 3322 usb_intr_req_t *reqp;
3328 3323 int rval;
3329 3324 usb_pipe_state_t pipe_state;
3330 3325
3331 3326 USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle,
3332 3327 "start polling: always=%d dev_state=%d pipe_state=%d\n\t"
3333 3328 "thread=%d ep1_ph=0x%p",
3334 3329 always, hubd->h_dev_state, hubd->h_intr_pipe_state,
3335 3330 hubd->h_hotplug_thread, (void *)hubd->h_ep1_ph);
3336 3331
3337 3332 /*
3338 3333 * start or restart polling on the intr pipe
3339 3334 * only if hotplug thread is not running
3340 3335 */
3341 3336 if ((always == HUBD_ALWAYS_START_POLLING) ||
3342 3337 ((hubd->h_dev_state == USB_DEV_ONLINE) &&
3343 3338 (hubd->h_intr_pipe_state == HUBD_INTR_PIPE_ACTIVE) &&
3344 3339 (hubd->h_hotplug_thread == 0) && hubd->h_ep1_ph)) {
3345 3340 USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle,
3346 3341 "start polling requested");
3347 3342
3348 3343 reqp = usb_alloc_intr_req(hubd->h_dip, 0, USB_FLAGS_SLEEP);
3349 3344
3350 3345 reqp->intr_client_private = (usb_opaque_t)hubd;
3351 3346 reqp->intr_attributes = USB_ATTRS_SHORT_XFER_OK |
3352 3347 USB_ATTRS_AUTOCLEARING;
3353 3348 reqp->intr_len = hubd->h_ep1_descr.wMaxPacketSize;
3354 3349 reqp->intr_cb = hubd_read_cb;
3355 3350 reqp->intr_exc_cb = hubd_exception_cb;
3356 3351 mutex_exit(HUBD_MUTEX(hubd));
3357 3352 if ((rval = usb_pipe_intr_xfer(hubd->h_ep1_ph, reqp,
3358 3353 USB_FLAGS_SLEEP)) != USB_SUCCESS) {
3359 3354 USB_DPRINTF_L2(DPRINT_MASK_HUB, hubd->h_log_handle,
3360 3355 "start polling failed, rval=%d", rval);
3361 3356 usb_free_intr_req(reqp);
3362 3357 }
3363 3358
3364 3359 rval = usb_pipe_get_state(hubd->h_ep1_ph, &pipe_state,
3365 3360 USB_FLAGS_SLEEP);
3366 3361 if (pipe_state != USB_PIPE_STATE_ACTIVE) {
3367 3362 USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
3368 3363 "intr pipe state=%d, rval=%d", pipe_state, rval);
3369 3364 }
3370 3365 USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle,
3371 3366 "start polling request 0x%p", (void *)reqp);
3372 3367
3373 3368 mutex_enter(HUBD_MUTEX(hubd));
3374 3369 }
3375 3370 }
3376 3371
3377 3372
3378 3373 /*
3379 3374 * hubd_stop_polling
3380 3375 * stop polling but do not close the pipe
3381 3376 */
3382 3377 static void
3383 3378 hubd_stop_polling(hubd_t *hubd)
3384 3379 {
3385 3380 int rval;
3386 3381 usb_pipe_state_t pipe_state;
3387 3382
3388 3383 if (hubd->h_ep1_ph) {
3389 3384 USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
3390 3385 "hubd_stop_polling:");
3391 3386 hubd->h_intr_pipe_state = HUBD_INTR_PIPE_STOPPED;
3392 3387 mutex_exit(HUBD_MUTEX(hubd));
3393 3388
3394 3389 usb_pipe_stop_intr_polling(hubd->h_ep1_ph, USB_FLAGS_SLEEP);
3395 3390 rval = usb_pipe_get_state(hubd->h_ep1_ph, &pipe_state,
3396 3391 USB_FLAGS_SLEEP);
3397 3392
3398 3393 if (pipe_state != USB_PIPE_STATE_IDLE) {
3399 3394 USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
3400 3395 "intr pipe state=%d, rval=%d", pipe_state, rval);
3401 3396 }
3402 3397 mutex_enter(HUBD_MUTEX(hubd));
3403 3398 if (hubd->h_intr_pipe_state == HUBD_INTR_PIPE_STOPPED) {
3404 3399 hubd->h_intr_pipe_state = HUBD_INTR_PIPE_ACTIVE;
3405 3400 }
3406 3401 }
3407 3402 }
3408 3403
3409 3404
3410 3405 /*
3411 3406 * hubd_close_intr_pipe:
3412 3407 * close the pipe (which also stops the polling
3413 3408 * and wait for the hotplug thread to exit
3414 3409 */
3415 3410 static void
3416 3411 hubd_close_intr_pipe(hubd_t *hubd)
3417 3412 {
3418 3413 USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle,
3419 3414 "hubd_close_intr_pipe:");
3420 3415
3421 3416 /*
3422 3417 * Now that no async operation is outstanding on pipe,
3423 3418 * we can change the state to HUBD_INTR_PIPE_CLOSING
3424 3419 */
3425 3420 hubd->h_intr_pipe_state = HUBD_INTR_PIPE_CLOSING;
3426 3421
3427 3422 ASSERT(hubd->h_hotplug_thread == 0);
3428 3423
3429 3424 if (hubd->h_ep1_ph) {
3430 3425 mutex_exit(HUBD_MUTEX(hubd));
3431 3426 usb_pipe_close(hubd->h_dip, hubd->h_ep1_ph, USB_FLAGS_SLEEP,
3432 3427 NULL, NULL);
3433 3428 mutex_enter(HUBD_MUTEX(hubd));
3434 3429 hubd->h_ep1_ph = NULL;
3435 3430 }
3436 3431
3437 3432 hubd->h_intr_pipe_state = HUBD_INTR_PIPE_IDLE;
3438 3433 }
3439 3434
3440 3435
3441 3436 /*
3442 3437 * hubd_exception_cb
3443 3438 * interrupt ep1 exception callback function.
3444 3439 * this callback executes in taskq thread context and assumes
3445 3440 * autoclearing
3446 3441 */
3447 3442 /*ARGSUSED*/
3448 3443 static void
3449 3444 hubd_exception_cb(usb_pipe_handle_t pipe, usb_intr_req_t *reqp)
3450 3445 {
3451 3446 hubd_t *hubd = (hubd_t *)(reqp->intr_client_private);
3452 3447
3453 3448 USB_DPRINTF_L2(DPRINT_MASK_CALLBACK, hubd->h_log_handle,
3454 3449 "hubd_exception_cb: "
3455 3450 "req=0x%p cr=%d data=0x%p cb_flags=0x%x", (void *)reqp,
3456 3451 reqp->intr_completion_reason, (void *)reqp->intr_data,
3457 3452 reqp->intr_cb_flags);
3458 3453
3459 3454 ASSERT((reqp->intr_cb_flags & USB_CB_INTR_CONTEXT) == 0);
3460 3455
3461 3456 mutex_enter(HUBD_MUTEX(hubd));
3462 3457 (void) hubd_pm_busy_component(hubd, hubd->h_dip, 0);
3463 3458
3464 3459 switch (reqp->intr_completion_reason) {
3465 3460 case USB_CR_PIPE_RESET:
3466 3461 /* only restart polling after autoclearing */
3467 3462 if ((hubd->h_intr_pipe_state == HUBD_INTR_PIPE_ACTIVE) &&
3468 3463 (hubd->h_port_reset_wait == 0)) {
3469 3464 hubd_start_polling(hubd, 0);
3470 3465 }
3471 3466
3472 3467 break;
3473 3468 case USB_CR_DEV_NOT_RESP:
3474 3469 case USB_CR_STOPPED_POLLING:
3475 3470 case USB_CR_PIPE_CLOSING:
3476 3471 case USB_CR_UNSPECIFIED_ERR:
3477 3472 /* never restart polling on these conditions */
3478 3473 default:
3479 3474 /* for all others, wait for the autoclearing PIPE_RESET cb */
3480 3475
3481 3476 break;
3482 3477 }
3483 3478
3484 3479 usb_free_intr_req(reqp);
3485 3480 (void) hubd_pm_idle_component(hubd, hubd->h_dip, 0);
3486 3481 mutex_exit(HUBD_MUTEX(hubd));
3487 3482 }
3488 3483
3489 3484
3490 3485 /*
3491 3486 * helper function to convert LE bytes to a portmask
3492 3487 */
3493 3488 static usb_port_mask_t
3494 3489 hubd_mblk2portmask(mblk_t *data)
3495 3490 {
3496 3491 int len = min(MBLKL(data), sizeof (usb_port_mask_t));
3497 3492 usb_port_mask_t rval = 0;
3498 3493 int i;
3499 3494
3500 3495 for (i = 0; i < len; i++) {
3501 3496 rval |= data->b_rptr[i] << (i * 8);
3502 3497 }
3503 3498
3504 3499 return (rval);
3505 3500 }
3506 3501
3507 3502
3508 3503 /*
3509 3504 * hubd_read_cb:
3510 3505 * interrupt ep1 callback function
3511 3506 *
3512 3507 * the status indicates just a change on the pipe with no indication
3513 3508 * of what the change was
3514 3509 *
3515 3510 * known conditions:
3516 3511 * - reset port completion
3517 3512 * - connect
3518 3513 * - disconnect
3519 3514 *
3520 3515 * for handling the hotplugging, create a new thread that can do
3521 3516 * synchronous usba calls
3522 3517 */
3523 3518 static void
3524 3519 hubd_read_cb(usb_pipe_handle_t pipe, usb_intr_req_t *reqp)
3525 3520 {
3526 3521 hubd_t *hubd = (hubd_t *)(reqp->intr_client_private);
3527 3522 size_t length;
3528 3523 mblk_t *data = reqp->intr_data;
3529 3524 int mem_flag = 0;
3530 3525 hubd_hotplug_arg_t *arg;
3531 3526
3532 3527 USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle,
3533 3528 "hubd_read_cb: ph=0x%p req=0x%p", (void *)pipe, (void *)reqp);
3534 3529
3535 3530 ASSERT((reqp->intr_cb_flags & USB_CB_INTR_CONTEXT) == 0);
3536 3531
3537 3532 /*
3538 3533 * At present, we are not handling notification for completion of
3539 3534 * asynchronous pipe reset, for which this data ptr could be NULL
3540 3535 */
3541 3536
3542 3537 if (data == NULL) {
3543 3538 usb_free_intr_req(reqp);
3544 3539
3545 3540 return;
3546 3541 }
3547 3542
3548 3543 arg = (hubd_hotplug_arg_t *)kmem_zalloc(
3549 3544 sizeof (hubd_hotplug_arg_t), KM_SLEEP);
3550 3545 mem_flag = 1;
3551 3546
3552 3547 mutex_enter(HUBD_MUTEX(hubd));
3553 3548
3554 3549 if ((hubd->h_dev_state == USB_DEV_SUSPENDED) ||
3555 3550 (hubd->h_intr_pipe_state != HUBD_INTR_PIPE_ACTIVE)) {
3556 3551 mutex_exit(HUBD_MUTEX(hubd));
3557 3552 usb_free_intr_req(reqp);
3558 3553 kmem_free(arg, sizeof (hubd_hotplug_arg_t));
3559 3554
3560 3555 return;
3561 3556 }
3562 3557
3563 3558 ASSERT(hubd->h_ep1_ph == pipe);
3564 3559
3565 3560 length = MBLKL(data);
3566 3561
3567 3562 /*
3568 3563 * Only look at the data and startup the hotplug thread if
3569 3564 * there actually is data.
3570 3565 */
3571 3566 if (length != 0) {
3572 3567 usb_port_mask_t port_change = hubd_mblk2portmask(data);
3573 3568
3574 3569 /*
3575 3570 * if a port change was already reported and we are waiting for
3576 3571 * reset port completion then wake up the hotplug thread which
3577 3572 * should be waiting on reset port completion
3578 3573 *
3579 3574 * if there is disconnect event instead of reset completion, let
3580 3575 * the hotplug thread figure this out
3581 3576 */
3582 3577
3583 3578 /* remove the reset wait bits from the status */
3584 3579 hubd->h_port_change |= port_change &
3585 3580 ~hubd->h_port_reset_wait;
3586 3581
3587 3582 USB_DPRINTF_L3(DPRINT_MASK_CALLBACK, hubd->h_log_handle,
3588 3583 "port change=0x%x port_reset_wait=0x%x",
3589 3584 hubd->h_port_change, hubd->h_port_reset_wait);
3590 3585
3591 3586 /* there should be only one reset bit active at the time */
3592 3587 if (hubd->h_port_reset_wait & port_change) {
3593 3588 hubd->h_port_reset_wait = 0;
3594 3589 cv_signal(&hubd->h_cv_reset_port);
3595 3590 }
3596 3591
3597 3592 /*
3598 3593 * kick off the thread only if device is ONLINE and it is not
3599 3594 * during attaching or detaching
3600 3595 */
3601 3596 if ((hubd->h_dev_state == USB_DEV_ONLINE) &&
3602 3597 (!DEVI_IS_ATTACHING(hubd->h_dip)) &&
3603 3598 (!DEVI_IS_DETACHING(hubd->h_dip)) &&
3604 3599 (hubd->h_port_change) &&
3605 3600 (hubd->h_hotplug_thread == 0)) {
3606 3601 USB_DPRINTF_L3(DPRINT_MASK_CALLBACK, hubd->h_log_handle,
3607 3602 "creating hotplug thread: "
3608 3603 "dev_state=%d", hubd->h_dev_state);
3609 3604
3610 3605 /*
3611 3606 * Mark this device as busy. The will be marked idle
3612 3607 * if the async req fails or at the exit of hotplug
3613 3608 * thread
3614 3609 */
3615 3610 (void) hubd_pm_busy_component(hubd, hubd->h_dip, 0);
3616 3611
3617 3612 arg->hubd = hubd;
3618 3613 arg->hotplug_during_attach = B_FALSE;
3619 3614
3620 3615 if (usb_async_req(hubd->h_dip,
3621 3616 hubd_hotplug_thread,
3622 3617 (void *)arg, 0) == USB_SUCCESS) {
3623 3618 hubd->h_hotplug_thread++;
3624 3619 mem_flag = 0;
3625 3620 } else {
3626 3621 /* mark this device as idle */
3627 3622 (void) hubd_pm_idle_component(hubd,
3628 3623 hubd->h_dip, 0);
3629 3624 }
3630 3625 }
3631 3626 }
3632 3627 mutex_exit(HUBD_MUTEX(hubd));
3633 3628
3634 3629 if (mem_flag == 1) {
3635 3630 kmem_free(arg, sizeof (hubd_hotplug_arg_t));
3636 3631 }
3637 3632
3638 3633 usb_free_intr_req(reqp);
3639 3634 }
3640 3635
3641 3636
3642 3637 /*
3643 3638 * hubd_hotplug_thread:
3644 3639 * handles resetting of port, and creating children
3645 3640 *
3646 3641 * the ports to check are indicated in h_port_change bit mask
3647 3642 * XXX note that one time poll doesn't work on the root hub
3648 3643 */
3649 3644 static void
3650 3645 hubd_hotplug_thread(void *arg)
3651 3646 {
3652 3647 hubd_hotplug_arg_t *hd_arg = (hubd_hotplug_arg_t *)arg;
3653 3648 hubd_t *hubd = hd_arg->hubd;
3654 3649 boolean_t attach_flg = hd_arg->hotplug_during_attach;
3655 3650 usb_port_t port;
3656 3651 uint16_t nports;
3657 3652 uint16_t status, change;
3658 3653 hub_power_t *hubpm;
3659 3654 dev_info_t *hdip = hubd->h_dip;
3660 3655 dev_info_t *rh_dip = hubd->h_usba_device->usb_root_hub_dip;
3661 3656 dev_info_t *child_dip;
3662 3657 boolean_t online_child = B_FALSE;
3663 3658 boolean_t offline_child = B_FALSE;
3664 3659 boolean_t pwrup_child = B_FALSE;
3665 3660 int prh_circ, rh_circ, chld_circ, circ, old_state;
3666 3661
3667 3662 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
3668 3663 "hubd_hotplug_thread: started");
3669 3664
3670 3665 /*
3671 3666 * Before console is init'd, we temporarily block the hotplug
3672 3667 * threads so that BUS_CONFIG_ONE through hubd_bus_config() can be
3673 3668 * processed quickly. This reduces the time needed for vfs_mountroot()
3674 3669 * to mount the root FS from a USB disk. And on SPARC platform,
3675 3670 * in order to load 'consconfig' successfully after OBP is gone,
3676 3671 * we need to check 'modrootloaded' to make sure root filesystem is
3677 3672 * available.
3678 3673 */
3679 3674 while (!modrootloaded || !consconfig_console_is_ready()) {
3680 3675 delay(drv_usectohz(10000));
3681 3676 }
3682 3677
3683 3678 kmem_free(arg, sizeof (hubd_hotplug_arg_t));
3684 3679
3685 3680 /*
3686 3681 * if our bus power entry point is active, process the change
3687 3682 * on the next notification of interrupt pipe
3688 3683 */
3689 3684 mutex_enter(HUBD_MUTEX(hubd));
3690 3685 if (hubd->h_bus_pwr || (hubd->h_hotplug_thread > 1)) {
3691 3686 hubd->h_hotplug_thread--;
3692 3687
3693 3688 /* mark this device as idle */
3694 3689 hubd_pm_idle_component(hubd, hubd->h_dip, 0);
3695 3690 mutex_exit(HUBD_MUTEX(hubd));
3696 3691
3697 3692 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
3698 3693 "hubd_hotplug_thread: "
3699 3694 "bus_power in progress/hotplugging undesirable - quit");
3700 3695
3701 3696 return;
3702 3697 }
3703 3698 mutex_exit(HUBD_MUTEX(hubd));
3704 3699
3705 3700 ndi_hold_devi(hdip); /* so we don't race with detach */
3706 3701
3707 3702 mutex_enter(HUBD_MUTEX(hubd));
3708 3703
3709 3704 /* is this the root hub? */
3710 3705 if (hdip == rh_dip) {
3711 3706 if (hubd->h_dev_state == USB_DEV_PWRED_DOWN) {
3712 3707 hubpm = hubd->h_hubpm;
3713 3708
3714 3709 /* mark the root hub as full power */
3715 3710 hubpm->hubp_current_power = USB_DEV_OS_FULL_PWR;
3716 3711 hubpm->hubp_time_at_full_power = gethrtime();
3717 3712 mutex_exit(HUBD_MUTEX(hubd));
3718 3713
3719 3714 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
3720 3715 "hubd_hotplug_thread: call pm_power_has_changed");
3721 3716
3722 3717 (void) pm_power_has_changed(hdip, 0,
3723 3718 USB_DEV_OS_FULL_PWR);
3724 3719
3725 3720 mutex_enter(HUBD_MUTEX(hubd));
3726 3721 hubd->h_dev_state = USB_DEV_ONLINE;
3727 3722 }
3728 3723
3729 3724 } else {
3730 3725 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
3731 3726 "hubd_hotplug_thread: not root hub");
3732 3727 }
3733 3728
3734 3729 mutex_exit(HUBD_MUTEX(hubd));
3735 3730
3736 3731 /*
3737 3732 * this ensures one hotplug activity per system at a time.
3738 3733 * we enter the parent PCI node to have this serialization.
3739 3734 * this also excludes ioctls and deathrow thread
3740 3735 * (a bit crude but easier to debug)
3741 3736 */
3742 3737 ndi_devi_enter(ddi_get_parent(rh_dip), &prh_circ);
3743 3738 ndi_devi_enter(rh_dip, &rh_circ);
3744 3739
3745 3740 /* exclude other threads */
3746 3741 ndi_devi_enter(hdip, &circ);
3747 3742 mutex_enter(HUBD_MUTEX(hubd));
3748 3743
3749 3744 ASSERT(hubd->h_intr_pipe_state == HUBD_INTR_PIPE_ACTIVE);
3750 3745
3751 3746 nports = hubd->h_hub_descr.bNbrPorts;
3752 3747
3753 3748 hubd_stop_polling(hubd);
3754 3749
3755 3750 while ((hubd->h_dev_state == USB_DEV_ONLINE) &&
3756 3751 (hubd->h_port_change)) {
3757 3752 /*
3758 3753 * The 0th bit is the hub status change bit.
3759 3754 * handle loss of local power here
3760 3755 */
3761 3756 if (hubd->h_port_change & HUB_CHANGE_STATUS) {
3762 3757 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
3763 3758 "hubd_hotplug_thread: hub status change!");
3764 3759
3765 3760 /*
3766 3761 * This should be handled properly. For now,
3767 3762 * mask off the bit.
3768 3763 */
3769 3764 hubd->h_port_change &= ~HUB_CHANGE_STATUS;
3770 3765
3771 3766 /*
3772 3767 * check and ack hub status
3773 3768 * this causes stall conditions
3774 3769 * when local power is removed
3775 3770 */
3776 3771 (void) hubd_get_hub_status(hubd);
3777 3772 }
3778 3773
3779 3774 for (port = 1; port <= nports; port++) {
3780 3775 usb_port_mask_t port_mask;
3781 3776 boolean_t was_connected;
3782 3777
3783 3778 port_mask = 1 << port;
3784 3779 was_connected =
3785 3780 (hubd->h_port_state[port] & PORT_STATUS_CCS) &&
3786 3781 (hubd->h_children_dips[port]);
3787 3782
3788 3783 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
3789 3784 "hubd_hotplug_thread: "
3790 3785 "port %d mask=0x%x change=0x%x connected=0x%x",
3791 3786 port, port_mask, hubd->h_port_change,
3792 3787 was_connected);
3793 3788
3794 3789 /*
3795 3790 * is this a port connection that changed?
3796 3791 */
3797 3792 if ((hubd->h_port_change & port_mask) == 0) {
3798 3793
3799 3794 continue;
3800 3795 }
3801 3796 hubd->h_port_change &= ~port_mask;
3802 3797
3803 3798 /* ack all changes */
3804 3799 (void) hubd_determine_port_status(hubd, port,
3805 3800 &status, &change, HUBD_ACK_ALL_CHANGES);
3806 3801
3807 3802 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
3808 3803 "handle port %d:\n\t"
3809 3804 "new status=0x%x change=0x%x was_conn=0x%x ",
3810 3805 port, status, change, was_connected);
3811 3806
3812 3807 /* Recover a disabled port */
3813 3808 if (change & PORT_CHANGE_PESC) {
3814 3809 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG,
3815 3810 hubd->h_log_handle,
3816 3811 "port%d Disabled - "
3817 3812 "status=0x%x, change=0x%x",
3818 3813 port, status, change);
3819 3814
3820 3815 /*
3821 3816 * if the port was connected and is still
3822 3817 * connected, recover the port
3823 3818 */
3824 3819 if (was_connected && (status &
3825 3820 PORT_STATUS_CCS)) {
3826 3821 online_child |=
3827 3822 (hubd_recover_disabled_port(hubd,
3828 3823 port) == USB_SUCCESS);
3829 3824 }
3830 3825 }
3831 3826
3832 3827 /*
3833 3828 * Now check what changed on the port
3834 3829 */
3835 3830 if ((change & PORT_CHANGE_CSC) || attach_flg) {
3836 3831 if ((status & PORT_STATUS_CCS) &&
3837 3832 (!was_connected)) {
3838 3833 /* new device plugged in */
3839 3834 online_child |=
3840 3835 (hubd_handle_port_connect(hubd,
3841 3836 port) == USB_SUCCESS);
3842 3837
3843 3838 } else if ((status & PORT_STATUS_CCS) &&
3844 3839 was_connected) {
3845 3840 /*
3846 3841 * In this case we can never be sure
3847 3842 * if the device indeed got hotplugged
3848 3843 * or the hub is falsely reporting the
3849 3844 * change.
3850 3845 */
3851 3846 child_dip = hubd->h_children_dips[port];
3852 3847
3853 3848 mutex_exit(HUBD_MUTEX(hubd));
3854 3849 /*
3855 3850 * this ensures we do not race with
3856 3851 * other threads which are detaching
3857 3852 * the child driver at the same time.
3858 3853 */
3859 3854 ndi_devi_enter(child_dip, &chld_circ);
3860 3855 /*
3861 3856 * Now check if the driver remains
3862 3857 * attached.
3863 3858 */
3864 3859 if (i_ddi_devi_attached(child_dip)) {
3865 3860 /*
3866 3861 * first post a disconnect event
3867 3862 * to the child.
3868 3863 */
3869 3864 hubd_post_event(hubd, port,
3870 3865 USBA_EVENT_TAG_HOT_REMOVAL);
3871 3866 mutex_enter(HUBD_MUTEX(hubd));
3872 3867
3873 3868 /*
3874 3869 * then reset the port and
3875 3870 * recover the device
3876 3871 */
3877 3872 online_child |=
3878 3873 (hubd_handle_port_connect(
3879 3874 hubd, port) == USB_SUCCESS);
3880 3875
3881 3876 mutex_exit(HUBD_MUTEX(hubd));
3882 3877 }
3883 3878
3884 3879 ndi_devi_exit(child_dip, chld_circ);
3885 3880 mutex_enter(HUBD_MUTEX(hubd));
3886 3881 } else if (was_connected) {
3887 3882 /* this is a disconnect */
3888 3883 mutex_exit(HUBD_MUTEX(hubd));
3889 3884 hubd_post_event(hubd, port,
3890 3885 USBA_EVENT_TAG_HOT_REMOVAL);
3891 3886 mutex_enter(HUBD_MUTEX(hubd));
3892 3887
3893 3888 offline_child = B_TRUE;
3894 3889 }
3895 3890 }
3896 3891
3897 3892 /*
3898 3893 * Check if any port is coming out of suspend
3899 3894 */
3900 3895 if (change & PORT_CHANGE_PSSC) {
3901 3896 /* a resuming device could have disconnected */
3902 3897 if (was_connected &&
3903 3898 hubd->h_children_dips[port]) {
3904 3899
3905 3900 /* device on this port resuming */
3906 3901 dev_info_t *dip;
3907 3902
3908 3903 dip = hubd->h_children_dips[port];
3909 3904
3910 3905 /*
3911 3906 * Don't raise power on detaching child
3912 3907 */
3913 3908 if (!DEVI_IS_DETACHING(dip)) {
3914 3909 /*
3915 3910 * As this child is not
3916 3911 * detaching, we set this
3917 3912 * flag, causing bus_ctls
3918 3913 * to stall detach till
3919 3914 * pm_raise_power returns
3920 3915 * and flag it for a deferred
3921 3916 * raise_power.
3922 3917 *
3923 3918 * pm_raise_power is deferred
3924 3919 * because we need to release
3925 3920 * the locks first.
3926 3921 */
3927 3922 hubd->h_port_state[port] |=
3928 3923 HUBD_CHILD_RAISE_POWER;
3929 3924 pwrup_child = B_TRUE;
3930 3925 mutex_exit(HUBD_MUTEX(hubd));
3931 3926
3932 3927 /*
3933 3928 * make sure that child
3934 3929 * doesn't disappear
3935 3930 */
3936 3931 ndi_hold_devi(dip);
3937 3932
3938 3933 mutex_enter(HUBD_MUTEX(hubd));
3939 3934 }
3940 3935 }
3941 3936 }
3942 3937
3943 3938 /*
3944 3939 * Check if the port is over-current
3945 3940 */
3946 3941 if (change & PORT_CHANGE_OCIC) {
3947 3942 USB_DPRINTF_L1(DPRINT_MASK_HOTPLUG,
3948 3943 hubd->h_log_handle,
3949 3944 "Port%d in over current condition, "
3950 3945 "please check the attached device to "
3951 3946 "clear the condition. The system will "
3952 3947 "try to recover the port, but if not "
3953 3948 "successful, you need to re-connect "
3954 3949 "the hub or reboot the system to bring "
3955 3950 "the port back to work", port);
3956 3951
3957 3952 if (!(status & PORT_STATUS_PPS)) {
3958 3953 /*
3959 3954 * Try to enable port power, but
3960 3955 * possibly fail. Ignore failure
3961 3956 */
3962 3957 (void) hubd_enable_port_power(hubd,
3963 3958 port);
3964 3959
3965 3960 /*
3966 3961 * Delay some time to avoid
3967 3962 * over-current event to happen
3968 3963 * too frequently in some cases
3969 3964 */
3970 3965 mutex_exit(HUBD_MUTEX(hubd));
3971 3966 delay(drv_usectohz(500000));
3972 3967 mutex_enter(HUBD_MUTEX(hubd));
3973 3968 }
3974 3969 }
3975 3970 }
3976 3971 }
3977 3972
3978 3973 /* release locks so we can do a devfs_clean */
3979 3974 mutex_exit(HUBD_MUTEX(hubd));
3980 3975
3981 3976 /* delete cached dv_node's but drop locks first */
3982 3977 ndi_devi_exit(hdip, circ);
3983 3978 ndi_devi_exit(rh_dip, rh_circ);
3984 3979 ndi_devi_exit(ddi_get_parent(rh_dip), prh_circ);
3985 3980
3986 3981 (void) devfs_clean(rh_dip, NULL, 0);
3987 3982
3988 3983 /* now check if any children need onlining */
3989 3984 if (online_child) {
3990 3985 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
3991 3986 "hubd_hotplug_thread: onlining children");
3992 3987
3993 3988 (void) ndi_devi_online(hubd->h_dip, 0);
3994 3989 }
3995 3990
3996 3991 /* now check if any disconnected devices need to be cleaned up */
3997 3992 if (offline_child) {
3998 3993 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
3999 3994 "hubd_hotplug_thread: scheduling cleanup");
4000 3995
4001 3996 hubd_schedule_cleanup(hubd->h_usba_device->usb_root_hub_dip);
4002 3997 }
4003 3998
4004 3999 mutex_enter(HUBD_MUTEX(hubd));
4005 4000
4006 4001 /* now raise power on the children that have woken up */
4007 4002 if (pwrup_child) {
4008 4003 old_state = hubd->h_dev_state;
4009 4004 hubd->h_dev_state = USB_DEV_HUB_CHILD_PWRLVL;
4010 4005 for (port = 1; port <= nports; port++) {
4011 4006 if (hubd->h_port_state[port] & HUBD_CHILD_RAISE_POWER) {
4012 4007 dev_info_t *dip = hubd->h_children_dips[port];
4013 4008
4014 4009 mutex_exit(HUBD_MUTEX(hubd));
4015 4010
4016 4011 /* Get the device to full power */
4017 4012 (void) pm_busy_component(dip, 0);
4018 4013 (void) pm_raise_power(dip, 0,
4019 4014 USB_DEV_OS_FULL_PWR);
4020 4015 (void) pm_idle_component(dip, 0);
4021 4016
4022 4017 /* release the hold on the child */
4023 4018 ndi_rele_devi(dip);
4024 4019 mutex_enter(HUBD_MUTEX(hubd));
4025 4020 hubd->h_port_state[port] &=
4026 4021 ~HUBD_CHILD_RAISE_POWER;
4027 4022 }
4028 4023 }
4029 4024 /*
4030 4025 * make sure that we don't accidentally
4031 4026 * over write the disconnect state
4032 4027 */
4033 4028 if (hubd->h_dev_state == USB_DEV_HUB_CHILD_PWRLVL) {
4034 4029 hubd->h_dev_state = old_state;
4035 4030 }
4036 4031 }
4037 4032
4038 4033 /*
4039 4034 * start polling can immediately kick off read callback
4040 4035 * we need to set the h_hotplug_thread to 0 so that
4041 4036 * the callback is not dropped
4042 4037 *
4043 4038 * if there is device during reset, still stop polling to avoid the
4044 4039 * read callback interrupting the reset, the polling will be started
4045 4040 * in hubd_reset_thread.
4046 4041 */
4047 4042 for (port = 1; port <= MAX_PORTS; port++) {
4048 4043 if (hubd->h_reset_port[port]) {
4049 4044
4050 4045 break;
4051 4046 }
4052 4047 }
4053 4048 if (port > MAX_PORTS) {
4054 4049 hubd_start_polling(hubd, HUBD_ALWAYS_START_POLLING);
4055 4050 }
4056 4051
4057 4052 /*
4058 4053 * Earlier we would set the h_hotplug_thread = 0 before
4059 4054 * polling was restarted so that
4060 4055 * if there is any root hub status change interrupt, we can still kick
4061 4056 * off the hotplug thread. This was valid when this interrupt was
4062 4057 * delivered in hardware, and only ONE interrupt would be delivered.
4063 4058 * Now that we poll on the root hub looking for status change in
4064 4059 * software, this assignment is no longer required.
4065 4060 */
4066 4061 hubd->h_hotplug_thread--;
4067 4062
4068 4063 /* mark this device as idle */
4069 4064 (void) hubd_pm_idle_component(hubd, hubd->h_dip, 0);
4070 4065
4071 4066 cv_broadcast(&hubd->h_cv_hotplug_dev);
4072 4067
4073 4068 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
4074 4069 "hubd_hotplug_thread: exit");
4075 4070
4076 4071 mutex_exit(HUBD_MUTEX(hubd));
4077 4072
4078 4073 ndi_rele_devi(hdip);
4079 4074 }
4080 4075
4081 4076
4082 4077 /*
4083 4078 * hubd_handle_port_connect:
4084 4079 * Transition a port from Disabled to Enabled. Ensure that the
4085 4080 * port is in the correct state before attempting to
4086 4081 * access the device.
4087 4082 */
4088 4083 static int
4089 4084 hubd_handle_port_connect(hubd_t *hubd, usb_port_t port)
4090 4085 {
4091 4086 int rval;
4092 4087 int retry;
4093 4088 long time_delay;
4094 4089 long settling_time;
4095 4090 uint16_t status;
4096 4091 uint16_t change;
4097 4092 usb_addr_t hubd_usb_addr;
4098 4093 usba_device_t *usba_device;
4099 4094 usb_port_status_t port_status = 0;
4100 4095 usb_port_status_t hub_port_status = 0;
4101 4096
4102 4097 /* Get the hub address and port status */
4103 4098 usba_device = hubd->h_usba_device;
4104 4099 mutex_enter(&usba_device->usb_mutex);
4105 4100 hubd_usb_addr = usba_device->usb_addr;
4106 4101 hub_port_status = usba_device->usb_port_status;
4107 4102 mutex_exit(&usba_device->usb_mutex);
4108 4103
4109 4104 /*
4110 4105 * If a device is connected, transition the
4111 4106 * port from Disabled to the Enabled state.
4112 4107 * The device will receive downstream packets
4113 4108 * in the Enabled state.
4114 4109 *
4115 4110 * reset port and wait for the hub to report
4116 4111 * completion
4117 4112 */
4118 4113 change = status = 0;
4119 4114
4120 4115 /*
4121 4116 * According to section 9.1.2 of USB 2.0 spec, the host should
4122 4117 * wait for atleast 100ms to allow completion of an insertion
4123 4118 * process and for power at the device to become stable.
4124 4119 * We wait for 200 ms
4125 4120 */
4126 4121 settling_time = drv_usectohz(hubd_device_delay / 5);
4127 4122 mutex_exit(HUBD_MUTEX(hubd));
4128 4123 delay(settling_time);
4129 4124 mutex_enter(HUBD_MUTEX(hubd));
4130 4125
4131 4126 /* calculate 600 ms delay time */
4132 4127 time_delay = (6 * drv_usectohz(hubd_device_delay)) / 10;
4133 4128
4134 4129 for (retry = 0; (hubd->h_dev_state == USB_DEV_ONLINE) &&
4135 4130 (retry < hubd_retry_enumerate); retry++) {
4136 4131 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
4137 4132 "resetting port%d, retry=%d", port, retry);
4138 4133
4139 4134 if ((rval = hubd_reset_port(hubd, port)) != USB_SUCCESS) {
4140 4135 (void) hubd_determine_port_status(hubd,
4141 4136 port, &status, &change, 0);
4142 4137
4143 4138 /* continue only if port is still connected */
4144 4139 if (status & PORT_STATUS_CCS) {
4145 4140 continue;
4146 4141 }
4147 4142
4148 4143 /* carry on regardless */
4149 4144 }
4150 4145
4151 4146 /*
4152 4147 * according to USB 2.0 spec section 11.24.2.7.1.2
4153 4148 * at the end of port reset, the hub enables the port.
4154 4149 * But for some strange reasons, uhci port remains disabled.
4155 4150 * And because the port remains disabled for the settling
4156 4151 * time below, the device connected to the port gets wedged
4157 4152 * - fails to enumerate (device not responding)
4158 4153 * Hence, we enable it here immediately and later again after
4159 4154 * the delay
4160 4155 */
4161 4156 (void) hubd_enable_port(hubd, port);
4162 4157
4163 4158 /* we skip this delay in the first iteration */
4164 4159 if (retry) {
4165 4160 /*
4166 4161 * delay for device to signal disconnect/connect so
4167 4162 * that hub properly recognizes the speed of the device
4168 4163 */
4169 4164 mutex_exit(HUBD_MUTEX(hubd));
4170 4165 delay(settling_time);
4171 4166 mutex_enter(HUBD_MUTEX(hubd));
4172 4167
4173 4168 /*
4174 4169 * When a low speed device is connected to any port of
4175 4170 * PPX it has to be explicitly enabled
4176 4171 * Also, if device intentionally signals
4177 4172 * disconnect/connect, it will disable the port.
4178 4173 * So enable it again.
4179 4174 */
4180 4175 (void) hubd_enable_port(hubd, port);
4181 4176 }
4182 4177
4183 4178 if ((rval = hubd_determine_port_status(hubd, port, &status,
4184 4179 &change, 0)) != USB_SUCCESS) {
4185 4180
4186 4181 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
4187 4182 "getting status failed (%d)", rval);
4188 4183
4189 4184 (void) hubd_disable_port(hubd, port);
4190 4185
4191 4186 continue;
4192 4187 }
4193 4188
4194 4189 if (status & PORT_STATUS_POCI) {
4195 4190 USB_DPRINTF_L0(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
4196 4191 "port %d overcurrent", port);
4197 4192
4198 4193 (void) hubd_disable_port(hubd, port);
4199 4194
4200 4195 /* ack changes */
4201 4196 (void) hubd_determine_port_status(hubd,
4202 4197 port, &status, &change, PORT_CHANGE_OCIC);
4203 4198
4204 4199 continue;
4205 4200 }
4206 4201
4207 4202 /* is status really OK? */
4208 4203 if ((status & PORT_STATUS_OK) != PORT_STATUS_OK) {
4209 4204 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
4210 4205 "port %d status (0x%x) not OK on retry %d",
4211 4206 port, status, retry);
4212 4207
4213 4208 /* check if we still have the connection */
4214 4209 if (!(status & PORT_STATUS_CCS)) {
4215 4210 /* lost connection, set exit condition */
4216 4211 retry = hubd_retry_enumerate;
4217 4212
4218 4213 break;
4219 4214 }
4220 4215 } else {
4221 4216 /*
4222 4217 * Determine if the device is high or full
4223 4218 * or low speed.
4224 4219 */
4225 4220 if (status & PORT_STATUS_LSDA) {
4226 4221 port_status = USBA_LOW_SPEED_DEV;
4227 4222 } else if (status & PORT_STATUS_HSDA) {
4228 4223 port_status = USBA_HIGH_SPEED_DEV;
4229 4224 } else {
4230 4225 port_status = USBA_FULL_SPEED_DEV;
4231 4226 }
4232 4227
4233 4228 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
4234 4229 "creating child port%d, status=0x%x "
4235 4230 "port status=0x%x",
4236 4231 port, status, port_status);
4237 4232
4238 4233 /*
4239 4234 * if the child already exists, set addrs and config
4240 4235 * to the device post connect event to the child
4241 4236 */
4242 4237 if (hubd->h_children_dips[port]) {
4243 4238 /* set addrs to this device */
4244 4239 rval = hubd_setdevaddr(hubd, port);
4245 4240
4246 4241 /*
4247 4242 * This delay is important for the CATC hub
4248 4243 * to enumerate. But, avoid delay in the first
4249 4244 * iteration
4250 4245 */
4251 4246 if (retry) {
4252 4247 mutex_exit(HUBD_MUTEX(hubd));
4253 4248 delay(drv_usectohz(
4254 4249 hubd_device_delay/100));
4255 4250 mutex_enter(HUBD_MUTEX(hubd));
4256 4251 }
4257 4252
4258 4253 if (rval == USB_SUCCESS) {
4259 4254 /*
4260 4255 * if the port is resetting, check if
4261 4256 * device's descriptors have changed.
4262 4257 */
4263 4258 if ((hubd->h_reset_port[port]) &&
4264 4259 (hubd_check_same_device(hubd,
4265 4260 port) != USB_SUCCESS)) {
4266 4261 retry = hubd_retry_enumerate;
4267 4262
4268 4263 break;
4269 4264 }
4270 4265
4271 4266 /*
4272 4267 * set the default config for
4273 4268 * this device
4274 4269 */
4275 4270 hubd_setdevconfig(hubd, port);
4276 4271
4277 4272 /*
4278 4273 * if we are doing Default reset, do
4279 4274 * not post reconnect event since we
4280 4275 * don't know where reset function is
4281 4276 * called.
4282 4277 */
4283 4278 if (hubd->h_reset_port[port]) {
4284 4279
4285 4280 return (USB_SUCCESS);
4286 4281 }
4287 4282
4288 4283 /*
4289 4284 * indicate to the child that
4290 4285 * it is online again
4291 4286 */
4292 4287 mutex_exit(HUBD_MUTEX(hubd));
4293 4288 hubd_post_event(hubd, port,
4294 4289 USBA_EVENT_TAG_HOT_INSERTION);
4295 4290 mutex_enter(HUBD_MUTEX(hubd));
4296 4291
4297 4292 return (USB_SUCCESS);
4298 4293 }
4299 4294 } else {
4300 4295 /*
4301 4296 * We need to release access here
4302 4297 * so that busctls on other ports can
4303 4298 * continue and don't cause a deadlock
4304 4299 * when busctl and removal of prom node
4305 4300 * takes concurrently. This also ensures
4306 4301 * busctls for attach of successfully
4307 4302 * enumerated devices on other ports can
4308 4303 * continue concurrently with the process
4309 4304 * of enumerating the new devices. This
4310 4305 * reduces the overall boot time of the system.
4311 4306 */
4312 4307 rval = hubd_create_child(hubd->h_dip,
4313 4308 hubd,
4314 4309 hubd->h_usba_device,
4315 4310 port_status, port,
4316 4311 retry);
4317 4312 if (rval == USB_SUCCESS) {
4318 4313 usba_update_hotplug_stats(hubd->h_dip,
4319 4314 USBA_TOTAL_HOTPLUG_SUCCESS|
4320 4315 USBA_HOTPLUG_SUCCESS);
4321 4316 hubd->h_total_hotplug_success++;
4322 4317
4323 4318 if (retry > 0) {
4324 4319 USB_DPRINTF_L2(
4325 4320 DPRINT_MASK_HOTPLUG,
4326 4321 hubd->h_log_handle,
4327 4322 "device on port %d "
4328 4323 "enumerated after %d %s",
4329 4324 port, retry,
4330 4325 (retry > 1) ? "retries" :
4331 4326 "retry");
4332 4327
4333 4328 }
4334 4329
4335 4330 return (USB_SUCCESS);
4336 4331 }
4337 4332 }
4338 4333 }
4339 4334
4340 4335 /* wait a while until it settles? */
4341 4336 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
4342 4337 "disabling port %d again", port);
4343 4338
4344 4339 (void) hubd_disable_port(hubd, port);
4345 4340 if (retry) {
4346 4341 mutex_exit(HUBD_MUTEX(hubd));
4347 4342 delay(time_delay);
4348 4343 mutex_enter(HUBD_MUTEX(hubd));
4349 4344 }
4350 4345
4351 4346 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
4352 4347 "retrying on port %d", port);
4353 4348 }
4354 4349
4355 4350 if (retry >= hubd_retry_enumerate) {
4356 4351 /*
4357 4352 * If it is a High Speed Root Hub and connected device
4358 4353 * Is a Low/Full Speed, it will be handled by USB 1.1
4359 4354 * Host Controller. In this case, USB 2.0 Host Controller
4360 4355 * will transfer the ownership of this port to USB 1.1
4361 4356 * Host Controller. So don't display any error message on
4362 4357 * the console.
4363 4358 */
4364 4359 if ((hubd_usb_addr == ROOT_HUB_ADDR) &&
4365 4360 (hub_port_status == USBA_HIGH_SPEED_DEV) &&
4366 4361 (port_status != USBA_HIGH_SPEED_DEV)) {
4367 4362 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG,
4368 4363 hubd->h_log_handle,
4369 4364 "hubd_handle_port_connect: Low/Full speed "
4370 4365 "device is connected to High Speed root hub");
4371 4366 } else {
4372 4367 USB_DPRINTF_L0(DPRINT_MASK_HOTPLUG,
4373 4368 hubd->h_log_handle,
4374 4369 "Connecting device on port %d failed", port);
4375 4370 }
4376 4371
4377 4372 (void) hubd_disable_port(hubd, port);
4378 4373 usba_update_hotplug_stats(hubd->h_dip,
4379 4374 USBA_TOTAL_HOTPLUG_FAILURE|USBA_HOTPLUG_FAILURE);
4380 4375 hubd->h_total_hotplug_failure++;
4381 4376
4382 4377 /*
4383 4378 * the port should be automagically
4384 4379 * disabled but just in case, we do
4385 4380 * it here
4386 4381 */
4387 4382 (void) hubd_disable_port(hubd, port);
4388 4383
4389 4384 /* ack all changes because we disabled this port */
4390 4385 (void) hubd_determine_port_status(hubd,
4391 4386 port, &status, &change, HUBD_ACK_ALL_CHANGES);
4392 4387
4393 4388 }
4394 4389
4395 4390 return (USB_FAILURE);
4396 4391 }
4397 4392
4398 4393
4399 4394 /*
4400 4395 * hubd_get_hub_status:
4401 4396 */
4402 4397 static int
4403 4398 hubd_get_hub_status(hubd_t *hubd)
4404 4399 {
4405 4400 int rval;
4406 4401 usb_cr_t completion_reason;
4407 4402 usb_cb_flags_t cb_flags;
4408 4403 uint16_t stword[2];
4409 4404 uint16_t status;
4410 4405 uint16_t change;
4411 4406 usb_cfg_descr_t cfg_descr;
4412 4407 size_t cfg_length;
4413 4408 uchar_t *usb_cfg;
4414 4409 uint8_t MaxPower;
4415 4410 usb_hub_descr_t *hub_descr;
4416 4411 usb_port_t port;
4417 4412
4418 4413 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
4419 4414 "hubd_get_hub_status:");
4420 4415
4421 4416 ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
4422 4417
4423 4418 if ((hubd_get_hub_status_words(hubd, stword)) != USB_SUCCESS) {
4424 4419
4425 4420 return (USB_FAILURE);
4426 4421 }
4427 4422 status = stword[0];
4428 4423 change = stword[1];
4429 4424
4430 4425 mutex_exit(HUBD_MUTEX(hubd));
4431 4426
4432 4427 /* Obtain the raw configuration descriptor */
4433 4428 usb_cfg = usb_get_raw_cfg_data(hubd->h_dip, &cfg_length);
4434 4429
4435 4430 /* get configuration descriptor */
4436 4431 rval = usb_parse_cfg_descr(usb_cfg, cfg_length,
4437 4432 &cfg_descr, USB_CFG_DESCR_SIZE);
4438 4433
4439 4434 if (rval != USB_CFG_DESCR_SIZE) {
4440 4435
4441 4436 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
4442 4437 "get hub configuration descriptor failed.");
4443 4438
4444 4439 mutex_enter(HUBD_MUTEX(hubd));
4445 4440
4446 4441 return (USB_FAILURE);
4447 4442 } else {
4448 4443 MaxPower = cfg_descr.bMaxPower;
4449 4444 }
4450 4445
4451 4446 /* check if local power status changed. */
4452 4447 if (change & C_HUB_LOCAL_POWER_STATUS) {
4453 4448
4454 4449 /*
4455 4450 * local power has been lost, check the maximum
4456 4451 * power consumption of current configuration.
4457 4452 * see USB2.0 spec Table 11-12.
4458 4453 */
4459 4454 if (status & HUB_LOCAL_POWER_STATUS) {
4460 4455
4461 4456 if (MaxPower == 0) {
4462 4457
4463 4458 /*
4464 4459 * Self-powered only hub. Because it could
4465 4460 * not draw any power from USB bus.
4466 4461 * It can't work well on this condition.
4467 4462 */
4468 4463 USB_DPRINTF_L1(DPRINT_MASK_HOTPLUG,
4469 4464 hubd->h_log_handle,
4470 4465 "local power has been lost, "
4471 4466 "please disconnect hub");
4472 4467 } else {
4473 4468
4474 4469 /*
4475 4470 * Bus-powered only or self/bus-powered hub.
4476 4471 */
4477 4472 USB_DPRINTF_L1(DPRINT_MASK_HOTPLUG,
4478 4473 hubd->h_log_handle,
4479 4474 "local power has been lost,"
4480 4475 "the hub could draw %d"
4481 4476 " mA power from the USB bus.",
4482 4477 2*MaxPower);
4483 4478 }
4484 4479
4485 4480 }
4486 4481
4487 4482 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
4488 4483 "clearing feature C_HUB_LOCAL_POWER ");
4489 4484
4490 4485 if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
4491 4486 hubd->h_default_pipe,
4492 4487 HUB_HANDLE_HUB_FEATURE_TYPE,
4493 4488 USB_REQ_CLEAR_FEATURE,
4494 4489 CFS_C_HUB_LOCAL_POWER,
4495 4490 0,
4496 4491 0,
4497 4492 NULL, 0,
4498 4493 &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
4499 4494 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG,
4500 4495 hubd->h_log_handle,
4501 4496 "clear feature C_HUB_LOCAL_POWER "
4502 4497 "failed (%d 0x%x %d)",
4503 4498 rval, completion_reason, cb_flags);
4504 4499 }
4505 4500
4506 4501 }
4507 4502
4508 4503 if (change & C_HUB_OVER_CURRENT) {
4509 4504
4510 4505 if (status & HUB_OVER_CURRENT) {
4511 4506
4512 4507 if (usba_is_root_hub(hubd->h_dip)) {
4513 4508 /*
4514 4509 * The root hub should be automatically
4515 4510 * recovered when over-current condition is
4516 4511 * cleared. But there might be exception and
4517 4512 * need user interaction to recover.
4518 4513 */
4519 4514 USB_DPRINTF_L0(DPRINT_MASK_HOTPLUG,
4520 4515 hubd->h_log_handle,
4521 4516 "Root hub over current condition, "
4522 4517 "please check your system to clear the "
4523 4518 "condition as soon as possible. And you "
4524 4519 "may need to reboot the system to bring "
4525 4520 "the root hub back to work if it cannot "
4526 4521 "recover automatically");
4527 4522 } else {
4528 4523 /*
4529 4524 * The driver would try to recover port power
4530 4525 * on over current condition. When the recovery
4531 4526 * fails, the user may still need to offline
4532 4527 * this hub in order to recover.
4533 4528 * The port power is automatically disabled,
4534 4529 * so we won't see disconnects.
4535 4530 */
4536 4531 USB_DPRINTF_L0(DPRINT_MASK_HOTPLUG,
4537 4532 hubd->h_log_handle,
4538 4533 "Hub global over current condition, "
4539 4534 "please disconnect the devices connected "
4540 4535 "to the hub to clear the condition. And "
4541 4536 "you may need to re-connect the hub if "
4542 4537 "the ports do not work");
4543 4538 }
4544 4539 }
4545 4540
4546 4541 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
4547 4542 "clearing feature C_HUB_OVER_CURRENT");
4548 4543
4549 4544 if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
4550 4545 hubd->h_default_pipe,
4551 4546 HUB_HANDLE_HUB_FEATURE_TYPE,
4552 4547 USB_REQ_CLEAR_FEATURE,
4553 4548 CFS_C_HUB_OVER_CURRENT,
4554 4549 0,
4555 4550 0,
4556 4551 NULL, 0,
4557 4552 &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
4558 4553 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG,
4559 4554 hubd->h_log_handle,
4560 4555 "clear feature C_HUB_OVER_CURRENT "
4561 4556 "failed (%d 0x%x %d)",
4562 4557 rval, completion_reason, cb_flags);
4563 4558 }
4564 4559
4565 4560 /*
4566 4561 * Try to recover all port power if they are turned off.
4567 4562 * Don't do this for root hub, but rely on the root hub
4568 4563 * to recover itself.
4569 4564 */
4570 4565 if (!usba_is_root_hub(hubd->h_dip)) {
4571 4566
4572 4567 mutex_enter(HUBD_MUTEX(hubd));
4573 4568
4574 4569 /*
4575 4570 * Only check the power status of the 1st port
4576 4571 * since all port power status should be the same.
4577 4572 */
4578 4573 (void) hubd_determine_port_status(hubd, 1, &status,
4579 4574 &change, 0);
4580 4575
4581 4576 if (status & PORT_STATUS_PPS) {
4582 4577
4583 4578 return (USB_SUCCESS);
4584 4579 }
4585 4580
4586 4581 hub_descr = &hubd->h_hub_descr;
4587 4582
4588 4583 for (port = 1; port <= hub_descr->bNbrPorts;
4589 4584 port++) {
4590 4585
4591 4586 (void) hubd_enable_port_power(hubd, port);
4592 4587 }
4593 4588
4594 4589 mutex_exit(HUBD_MUTEX(hubd));
4595 4590
4596 4591 /*
4597 4592 * Delay some time to avoid over-current event
4598 4593 * to happen too frequently in some cases
4599 4594 */
4600 4595 delay(drv_usectohz(500000));
4601 4596 }
4602 4597 }
4603 4598
4604 4599 mutex_enter(HUBD_MUTEX(hubd));
4605 4600
4606 4601 return (USB_SUCCESS);
4607 4602 }
4608 4603
4609 4604
4610 4605 /*
4611 4606 * hubd_reset_port:
4612 4607 */
4613 4608 static int
4614 4609 hubd_reset_port(hubd_t *hubd, usb_port_t port)
4615 4610 {
4616 4611 int rval;
4617 4612 usb_cr_t completion_reason;
4618 4613 usb_cb_flags_t cb_flags;
4619 4614 usb_port_mask_t port_mask = 1 << port;
4620 4615 mblk_t *data;
4621 4616 uint16_t status;
4622 4617 uint16_t change;
4623 4618 int i;
4624 4619 clock_t delta;
4625 4620
4626 4621 USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
4627 4622 "hubd_reset_port: port=%d", port);
4628 4623
4629 4624 ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
4630 4625
4631 4626 hubd->h_port_reset_wait |= port_mask;
4632 4627
4633 4628 mutex_exit(HUBD_MUTEX(hubd));
4634 4629
4635 4630 if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
4636 4631 hubd->h_default_pipe,
4637 4632 HUB_HANDLE_PORT_FEATURE_TYPE,
4638 4633 USB_REQ_SET_FEATURE,
4639 4634 CFS_PORT_RESET,
4640 4635 port,
4641 4636 0,
4642 4637 NULL, 0,
4643 4638 &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
4644 4639 USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
4645 4640 "reset port%d failed (%d 0x%x %d)",
4646 4641 port, completion_reason, cb_flags, rval);
4647 4642
4648 4643 mutex_enter(HUBD_MUTEX(hubd));
4649 4644
4650 4645 return (USB_FAILURE);
4651 4646 }
4652 4647
4653 4648 mutex_enter(HUBD_MUTEX(hubd));
4654 4649
4655 4650 USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
4656 4651 "waiting on cv for reset completion");
4657 4652
4658 4653 /*
4659 4654 * wait for port status change event
4660 4655 */
4661 4656 delta = drv_usectohz(hubd_device_delay / 10);
4662 4657 for (i = 0; i < hubd_retry_enumerate; i++) {
4663 4658 /*
4664 4659 * start polling ep1 for receiving notification on
4665 4660 * reset completion
4666 4661 */
4667 4662 hubd_start_polling(hubd, HUBD_ALWAYS_START_POLLING);
4668 4663
4669 4664 /*
4670 4665 * sleep a max of 100ms for reset completion
4671 4666 * notification to be received
4672 4667 */
4673 4668 if (hubd->h_port_reset_wait & port_mask) {
4674 4669 rval = cv_reltimedwait(&hubd->h_cv_reset_port,
4675 4670 &hubd->h_mutex, delta, TR_CLOCK_TICK);
4676 4671 if ((rval <= 0) &&
4677 4672 (hubd->h_port_reset_wait & port_mask)) {
4678 4673 /* we got woken up because of a timeout */
4679 4674 USB_DPRINTF_L2(DPRINT_MASK_PORT,
4680 4675 hubd->h_log_handle,
4681 4676 "timeout: reset port=%d failed", port);
4682 4677
4683 4678 hubd->h_port_reset_wait &= ~port_mask;
4684 4679
4685 4680 hubd_stop_polling(hubd);
4686 4681
4687 4682 return (USB_FAILURE);
4688 4683 }
4689 4684 }
4690 4685
4691 4686 USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
4692 4687 "reset completion received");
4693 4688
4694 4689 hubd_stop_polling(hubd);
4695 4690
4696 4691 data = NULL;
4697 4692
4698 4693 /* check status to determine whether reset completed */
4699 4694 mutex_exit(HUBD_MUTEX(hubd));
4700 4695 if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
4701 4696 hubd->h_default_pipe,
4702 4697 HUB_GET_PORT_STATUS_TYPE,
4703 4698 USB_REQ_GET_STATUS,
4704 4699 0,
4705 4700 port,
4706 4701 GET_STATUS_LENGTH,
4707 4702 &data, 0,
4708 4703 &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
4709 4704 USB_DPRINTF_L2(DPRINT_MASK_PORT,
4710 4705 hubd->h_log_handle,
4711 4706 "get status port%d failed (%d 0x%x %d)",
4712 4707 port, completion_reason, cb_flags, rval);
4713 4708
4714 4709 if (data) {
4715 4710 freemsg(data);
4716 4711 data = NULL;
4717 4712 }
4718 4713 mutex_enter(HUBD_MUTEX(hubd));
4719 4714
4720 4715 continue;
4721 4716 }
4722 4717
4723 4718 status = (*(data->b_rptr + 1) << 8) | *(data->b_rptr);
4724 4719 change = (*(data->b_rptr + 3) << 8) | *(data->b_rptr + 2);
4725 4720
4726 4721 freemsg(data);
4727 4722
4728 4723 /* continue only if port is still connected */
4729 4724 if (!(status & PORT_STATUS_CCS)) {
4730 4725
4731 4726 /* lost connection, set exit condition */
4732 4727 i = hubd_retry_enumerate;
4733 4728
4734 4729 mutex_enter(HUBD_MUTEX(hubd));
4735 4730
4736 4731 break;
4737 4732 }
4738 4733
4739 4734 if (status & PORT_STATUS_PRS) {
4740 4735 USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
4741 4736 "port%d reset active", port);
4742 4737 mutex_enter(HUBD_MUTEX(hubd));
4743 4738
4744 4739 continue;
4745 4740 } else {
4746 4741 USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
4747 4742 "port%d reset inactive", port);
4748 4743 }
4749 4744
4750 4745 if (change & PORT_CHANGE_PRSC) {
4751 4746 USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
4752 4747 "clearing feature CFS_C_PORT_RESET");
4753 4748
4754 4749 if (usb_pipe_sync_ctrl_xfer(hubd->h_dip,
4755 4750 hubd->h_default_pipe,
4756 4751 HUB_HANDLE_PORT_FEATURE_TYPE,
4757 4752 USB_REQ_CLEAR_FEATURE,
4758 4753 CFS_C_PORT_RESET,
4759 4754 port,
4760 4755 0,
4761 4756 NULL, 0,
4762 4757 &completion_reason, &cb_flags, 0) != USB_SUCCESS) {
4763 4758 USB_DPRINTF_L2(DPRINT_MASK_PORT,
4764 4759 hubd->h_log_handle,
4765 4760 "clear feature CFS_C_PORT_RESET"
4766 4761 " port%d failed (%d 0x%x %d)",
4767 4762 port, completion_reason, cb_flags, rval);
4768 4763 }
4769 4764 }
4770 4765 mutex_enter(HUBD_MUTEX(hubd));
4771 4766
4772 4767 break;
4773 4768 }
4774 4769
4775 4770 if (i >= hubd_retry_enumerate) {
4776 4771 /* port reset has failed */
4777 4772 rval = USB_FAILURE;
4778 4773 }
4779 4774
4780 4775 return (rval);
4781 4776 }
4782 4777
4783 4778
4784 4779 /*
4785 4780 * hubd_enable_port:
4786 4781 * this may fail if the hub as been disconnected
4787 4782 */
4788 4783 static int
4789 4784 hubd_enable_port(hubd_t *hubd, usb_port_t port)
4790 4785 {
4791 4786 int rval;
4792 4787 usb_cr_t completion_reason;
4793 4788 usb_cb_flags_t cb_flags;
4794 4789
4795 4790 USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
4796 4791 "hubd_enable_port: port=%d", port);
4797 4792
4798 4793 ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
4799 4794
4800 4795 mutex_exit(HUBD_MUTEX(hubd));
4801 4796
4802 4797 /* Do not issue a SetFeature(PORT_ENABLE) on external hubs */
4803 4798 if (!usba_is_root_hub(hubd->h_dip)) {
4804 4799 mutex_enter(HUBD_MUTEX(hubd));
4805 4800
4806 4801 return (USB_SUCCESS);
4807 4802 }
4808 4803
4809 4804 if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
4810 4805 hubd->h_default_pipe,
4811 4806 HUB_HANDLE_PORT_FEATURE_TYPE,
4812 4807 USB_REQ_SET_FEATURE,
4813 4808 CFS_PORT_ENABLE,
4814 4809 port,
4815 4810 0,
4816 4811 NULL, 0,
4817 4812 &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
4818 4813 USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
4819 4814 "enable port%d failed (%d 0x%x %d)",
4820 4815 port, completion_reason, cb_flags, rval);
4821 4816 }
4822 4817
4823 4818 mutex_enter(HUBD_MUTEX(hubd));
4824 4819
4825 4820 USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
4826 4821 "enabling port done");
4827 4822
4828 4823 return (rval);
4829 4824 }
4830 4825
4831 4826
4832 4827 /*
4833 4828 * hubd_disable_port
4834 4829 */
4835 4830 static int
4836 4831 hubd_disable_port(hubd_t *hubd, usb_port_t port)
4837 4832 {
4838 4833 int rval;
4839 4834 usb_cr_t completion_reason;
4840 4835 usb_cb_flags_t cb_flags;
4841 4836
4842 4837 USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
4843 4838 "hubd_disable_port: port=%d", port);
4844 4839
4845 4840 ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
4846 4841
4847 4842 mutex_exit(HUBD_MUTEX(hubd));
4848 4843
4849 4844 if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
4850 4845 hubd->h_default_pipe,
4851 4846 HUB_HANDLE_PORT_FEATURE_TYPE,
4852 4847 USB_REQ_CLEAR_FEATURE,
4853 4848 CFS_PORT_ENABLE,
4854 4849 port,
4855 4850 0,
4856 4851 NULL, 0,
4857 4852 &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
4858 4853 USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
4859 4854 "disable port%d failed (%d 0x%x %d)", port,
4860 4855 completion_reason, cb_flags, rval);
4861 4856 mutex_enter(HUBD_MUTEX(hubd));
4862 4857
4863 4858 return (USB_FAILURE);
4864 4859 }
4865 4860
4866 4861 USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
4867 4862 "clearing feature CFS_C_PORT_ENABLE");
4868 4863
4869 4864 if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
4870 4865 hubd->h_default_pipe,
4871 4866 HUB_HANDLE_PORT_FEATURE_TYPE,
4872 4867 USB_REQ_CLEAR_FEATURE,
4873 4868 CFS_C_PORT_ENABLE,
4874 4869 port,
4875 4870 0,
4876 4871 NULL, 0,
4877 4872 &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
4878 4873 USB_DPRINTF_L2(DPRINT_MASK_PORT,
4879 4874 hubd->h_log_handle,
4880 4875 "clear feature CFS_C_PORT_ENABLE port%d failed "
4881 4876 "(%d 0x%x %d)",
4882 4877 port, completion_reason, cb_flags, rval);
4883 4878
4884 4879 mutex_enter(HUBD_MUTEX(hubd));
4885 4880
4886 4881 return (USB_FAILURE);
4887 4882 }
4888 4883
4889 4884 mutex_enter(HUBD_MUTEX(hubd));
4890 4885
4891 4886 return (USB_SUCCESS);
4892 4887 }
4893 4888
4894 4889
4895 4890 /*
4896 4891 * hubd_determine_port_status:
4897 4892 */
4898 4893 static int
4899 4894 hubd_determine_port_status(hubd_t *hubd, usb_port_t port,
4900 4895 uint16_t *status, uint16_t *change, uint_t ack_flag)
4901 4896 {
4902 4897 int rval;
4903 4898 mblk_t *data = NULL;
4904 4899 usb_cr_t completion_reason;
4905 4900 usb_cb_flags_t cb_flags;
4906 4901
4907 4902 USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
4908 4903 "hubd_determine_port_status: port=%d, state=0x%x ack=0x%x", port,
4909 4904 hubd->h_port_state[port], ack_flag);
4910 4905
4911 4906 ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
4912 4907
4913 4908 mutex_exit(HUBD_MUTEX(hubd));
4914 4909
4915 4910 if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
4916 4911 hubd->h_default_pipe,
4917 4912 HUB_GET_PORT_STATUS_TYPE,
4918 4913 USB_REQ_GET_STATUS,
4919 4914 0,
4920 4915 port,
4921 4916 GET_STATUS_LENGTH,
4922 4917 &data, 0,
4923 4918 &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
4924 4919 USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
4925 4920 "port=%d get status failed (%d 0x%x %d)",
4926 4921 port, completion_reason, cb_flags, rval);
4927 4922
4928 4923 if (data) {
4929 4924 freemsg(data);
4930 4925 }
4931 4926
4932 4927 *status = *change = 0;
4933 4928 mutex_enter(HUBD_MUTEX(hubd));
4934 4929
4935 4930 return (rval);
4936 4931 }
4937 4932
4938 4933 mutex_enter(HUBD_MUTEX(hubd));
4939 4934 if (MBLKL(data) != GET_STATUS_LENGTH) {
4940 4935 USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
4941 4936 "port %d: length incorrect %ld",
4942 4937 port, MBLKL(data));
4943 4938 freemsg(data);
4944 4939 *status = *change = 0;
4945 4940
4946 4941 return (rval);
4947 4942 }
4948 4943
4949 4944
4950 4945 *status = (*(data->b_rptr + 1) << 8) | *(data->b_rptr);
4951 4946 *change = (*(data->b_rptr + 3) << 8) | *(data->b_rptr + 2);
4952 4947
4953 4948 USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
4954 4949 "port%d status=0x%x, change=0x%x", port, *status, *change);
4955 4950
4956 4951 freemsg(data);
4957 4952
4958 4953 if (*status & PORT_STATUS_CCS) {
4959 4954 USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
4960 4955 "port%d connected", port);
4961 4956
4962 4957 hubd->h_port_state[port] |= (PORT_STATUS_CCS & ack_flag);
4963 4958 } else {
4964 4959 USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
4965 4960 "port%d disconnected", port);
4966 4961
4967 4962 hubd->h_port_state[port] &= ~(PORT_STATUS_CCS & ack_flag);
4968 4963 }
4969 4964
4970 4965 if (*status & PORT_STATUS_PES) {
4971 4966 USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
4972 4967 "port%d enabled", port);
4973 4968
4974 4969 hubd->h_port_state[port] |= (PORT_STATUS_PES & ack_flag);
4975 4970 } else {
4976 4971 USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
4977 4972 "port%d disabled", port);
4978 4973
4979 4974 hubd->h_port_state[port] &= ~(PORT_STATUS_PES & ack_flag);
4980 4975 }
4981 4976
4982 4977 if (*status & PORT_STATUS_PSS) {
4983 4978 USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
4984 4979 "port%d suspended", port);
4985 4980
4986 4981 hubd->h_port_state[port] |= (PORT_STATUS_PSS & ack_flag);
4987 4982 } else {
4988 4983 USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
4989 4984 "port%d not suspended", port);
4990 4985
4991 4986 hubd->h_port_state[port] &= ~(PORT_STATUS_PSS & ack_flag);
4992 4987 }
4993 4988
4994 4989 if (*change & PORT_CHANGE_PRSC) {
4995 4990 USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
4996 4991 "port%d reset completed", port);
4997 4992
4998 4993 hubd->h_port_state[port] |= (PORT_CHANGE_PRSC & ack_flag);
4999 4994 } else {
5000 4995
5001 4996 hubd->h_port_state[port] &= ~(PORT_CHANGE_PRSC & ack_flag);
5002 4997 }
5003 4998
5004 4999 if (*status & PORT_STATUS_POCI) {
5005 5000 USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
5006 5001 "port%d overcurrent!", port);
5007 5002
5008 5003 hubd->h_port_state[port] |= (PORT_STATUS_POCI & ack_flag);
5009 5004 } else {
5010 5005
5011 5006 hubd->h_port_state[port] &= ~(PORT_STATUS_POCI & ack_flag);
5012 5007 }
5013 5008
5014 5009 if (*status & PORT_STATUS_PRS) {
5015 5010 USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
5016 5011 "port%d reset active", port);
5017 5012
5018 5013 hubd->h_port_state[port] |= (PORT_STATUS_PRS & ack_flag);
5019 5014 } else {
5020 5015 USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
5021 5016 "port%d reset inactive", port);
5022 5017
5023 5018 hubd->h_port_state[port] &= ~(PORT_STATUS_PRS & ack_flag);
5024 5019 }
5025 5020 if (*status & PORT_STATUS_PPS) {
5026 5021 USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
5027 5022 "port%d power on", port);
5028 5023
5029 5024 hubd->h_port_state[port] |= (PORT_STATUS_PPS & ack_flag);
5030 5025 } else {
5031 5026 USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
5032 5027 "port%d power off", port);
5033 5028
5034 5029 hubd->h_port_state[port] &= ~(PORT_STATUS_PPS & ack_flag);
5035 5030 }
5036 5031 if (*status & PORT_STATUS_LSDA) {
5037 5032 USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
5038 5033 "port%d low speed", port);
5039 5034
5040 5035 hubd->h_port_state[port] |= (PORT_STATUS_LSDA & ack_flag);
5041 5036 } else {
5042 5037 hubd->h_port_state[port] &= ~(PORT_STATUS_LSDA & ack_flag);
5043 5038 if (*status & PORT_STATUS_HSDA) {
5044 5039 USB_DPRINTF_L3(DPRINT_MASK_PORT,
5045 5040 hubd->h_log_handle, "port%d "
5046 5041 "high speed", port);
5047 5042
5048 5043 hubd->h_port_state[port] |=
5049 5044 (PORT_STATUS_HSDA & ack_flag);
5050 5045 } else {
5051 5046 USB_DPRINTF_L3(DPRINT_MASK_PORT,
5052 5047 hubd->h_log_handle, "port%d "
5053 5048 "full speed", port);
5054 5049
5055 5050 hubd->h_port_state[port] &=
5056 5051 ~(PORT_STATUS_HSDA & ack_flag);
5057 5052 }
5058 5053 }
5059 5054
5060 5055 /*
5061 5056 * Acknowledge connection, enable, reset status
5062 5057 */
5063 5058 if (ack_flag) {
5064 5059 mutex_exit(HUBD_MUTEX(hubd));
5065 5060 if (*change & PORT_CHANGE_CSC & ack_flag) {
5066 5061 USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
5067 5062 "clearing feature CFS_C_PORT_CONNECTION");
5068 5063 if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
5069 5064 hubd->h_default_pipe,
5070 5065 HUB_HANDLE_PORT_FEATURE_TYPE,
5071 5066 USB_REQ_CLEAR_FEATURE,
5072 5067 CFS_C_PORT_CONNECTION,
5073 5068 port,
5074 5069 0, NULL, 0,
5075 5070 &completion_reason, &cb_flags, 0)) !=
5076 5071 USB_SUCCESS) {
5077 5072 USB_DPRINTF_L2(DPRINT_MASK_PORT,
5078 5073 hubd->h_log_handle,
5079 5074 "clear feature CFS_C_PORT_CONNECTION"
5080 5075 " port%d failed (%d 0x%x %d)",
5081 5076 port, completion_reason, cb_flags, rval);
5082 5077 }
5083 5078 }
5084 5079 if (*change & PORT_CHANGE_PESC & ack_flag) {
5085 5080 USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
5086 5081 "clearing feature CFS_C_PORT_ENABLE");
5087 5082 if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
5088 5083 hubd->h_default_pipe,
5089 5084 HUB_HANDLE_PORT_FEATURE_TYPE,
5090 5085 USB_REQ_CLEAR_FEATURE,
5091 5086 CFS_C_PORT_ENABLE,
5092 5087 port,
5093 5088 0, NULL, 0,
5094 5089 &completion_reason, &cb_flags, 0)) !=
5095 5090 USB_SUCCESS) {
5096 5091 USB_DPRINTF_L2(DPRINT_MASK_PORT,
5097 5092 hubd->h_log_handle,
5098 5093 "clear feature CFS_C_PORT_ENABLE"
5099 5094 " port%d failed (%d 0x%x %d)",
5100 5095 port, completion_reason, cb_flags, rval);
5101 5096 }
5102 5097 }
5103 5098 if (*change & PORT_CHANGE_PSSC & ack_flag) {
5104 5099 USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
5105 5100 "clearing feature CFS_C_PORT_SUSPEND");
5106 5101
5107 5102 if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
5108 5103 hubd->h_default_pipe,
5109 5104 HUB_HANDLE_PORT_FEATURE_TYPE,
5110 5105 USB_REQ_CLEAR_FEATURE,
5111 5106 CFS_C_PORT_SUSPEND,
5112 5107 port,
5113 5108 0, NULL, 0,
5114 5109 &completion_reason, &cb_flags, 0)) !=
5115 5110 USB_SUCCESS) {
5116 5111 USB_DPRINTF_L2(DPRINT_MASK_PORT,
5117 5112 hubd->h_log_handle,
5118 5113 "clear feature CFS_C_PORT_SUSPEND"
5119 5114 " port%d failed (%d 0x%x %d)",
5120 5115 port, completion_reason, cb_flags, rval);
5121 5116 }
5122 5117 }
5123 5118 if (*change & PORT_CHANGE_OCIC & ack_flag) {
5124 5119 USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
5125 5120 "clearing feature CFS_C_PORT_OVER_CURRENT");
5126 5121
5127 5122 if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
5128 5123 hubd->h_default_pipe,
5129 5124 HUB_HANDLE_PORT_FEATURE_TYPE,
5130 5125 USB_REQ_CLEAR_FEATURE,
5131 5126 CFS_C_PORT_OVER_CURRENT,
5132 5127 port,
5133 5128 0, NULL, 0,
5134 5129 &completion_reason, &cb_flags, 0)) !=
5135 5130 USB_SUCCESS) {
5136 5131 USB_DPRINTF_L2(DPRINT_MASK_PORT,
5137 5132 hubd->h_log_handle,
5138 5133 "clear feature CFS_C_PORT_OVER_CURRENT"
5139 5134 " port%d failed (%d 0x%x %d)",
5140 5135 port, completion_reason, cb_flags, rval);
5141 5136 }
5142 5137 }
5143 5138 if (*change & PORT_CHANGE_PRSC & ack_flag) {
5144 5139 USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
5145 5140 "clearing feature CFS_C_PORT_RESET");
5146 5141 if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
5147 5142 hubd->h_default_pipe,
5148 5143 HUB_HANDLE_PORT_FEATURE_TYPE,
5149 5144 USB_REQ_CLEAR_FEATURE,
5150 5145 CFS_C_PORT_RESET,
5151 5146 port,
5152 5147 0, NULL, 0,
5153 5148 &completion_reason, &cb_flags, 0)) !=
5154 5149 USB_SUCCESS) {
5155 5150 USB_DPRINTF_L2(DPRINT_MASK_PORT,
5156 5151 hubd->h_log_handle,
5157 5152 "clear feature CFS_C_PORT_RESET"
5158 5153 " port%d failed (%d 0x%x %d)",
5159 5154 port, completion_reason, cb_flags, rval);
5160 5155 }
5161 5156 }
5162 5157 mutex_enter(HUBD_MUTEX(hubd));
5163 5158 }
5164 5159
5165 5160 USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
5166 5161 "new port%d state 0x%x", port, hubd->h_port_state[port]);
5167 5162
5168 5163
5169 5164 return (USB_SUCCESS);
5170 5165 }
5171 5166
5172 5167
5173 5168 /*
5174 5169 * hubd_recover_disabled_port
5175 5170 * if the port got disabled because of an error
5176 5171 * enable it. If hub doesn't suport enable port,
5177 5172 * reset the port to bring the device to life again
5178 5173 */
5179 5174 static int
5180 5175 hubd_recover_disabled_port(hubd_t *hubd, usb_port_t port)
5181 5176 {
5182 5177 uint16_t status;
5183 5178 uint16_t change;
5184 5179 int rval = USB_FAILURE;
5185 5180
5186 5181 /* first try enabling the port */
5187 5182 (void) hubd_enable_port(hubd, port);
5188 5183
5189 5184 /* read the port status */
5190 5185 (void) hubd_determine_port_status(hubd, port, &status, &change,
5191 5186 PORT_CHANGE_PESC);
5192 5187
5193 5188 if (status & PORT_STATUS_PES) {
5194 5189 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
5195 5190 "Port%d now Enabled", port);
5196 5191 } else if (status & PORT_STATUS_CCS) {
5197 5192 /* first post a disconnect event to the child */
5198 5193 mutex_exit(HUBD_MUTEX(hubd));
5199 5194 hubd_post_event(hubd, port, USBA_EVENT_TAG_HOT_REMOVAL);
5200 5195 mutex_enter(HUBD_MUTEX(hubd));
5201 5196
5202 5197 /* then reset the port and recover the device */
5203 5198 rval = hubd_handle_port_connect(hubd, port);
5204 5199
5205 5200 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
5206 5201 "Port%d now Enabled by force", port);
5207 5202 }
5208 5203
5209 5204 return (rval);
5210 5205 }
5211 5206
5212 5207
5213 5208 /*
5214 5209 * hubd_enable_all_port_power:
5215 5210 */
5216 5211 static int
5217 5212 hubd_enable_all_port_power(hubd_t *hubd)
5218 5213 {
5219 5214 usb_hub_descr_t *hub_descr;
5220 5215 int wait;
5221 5216 usb_port_t port;
5222 5217 uint_t retry;
5223 5218 uint16_t status;
5224 5219 uint16_t change;
5225 5220
5226 5221 USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
5227 5222 "hubd_enable_all_port_power");
5228 5223
5229 5224 ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
5230 5225
5231 5226 hub_descr = &hubd->h_hub_descr;
5232 5227
5233 5228 /*
5234 5229 * According to section 11.11 of USB, for hubs with no power
5235 5230 * switches, bPwrOn2PwrGood is zero. But we wait for some
5236 5231 * arbitrary time to enable power to become stable.
5237 5232 *
5238 5233 * If an hub supports port power switching, we need to wait
5239 5234 * at least 20ms before accessing corresponding usb port.
5240 5235 */
5241 5236 if ((hub_descr->wHubCharacteristics &
5242 5237 HUB_CHARS_NO_POWER_SWITCHING) || (!hub_descr->bPwrOn2PwrGood)) {
5243 5238 wait = hubd_device_delay / 10;
5244 5239 } else {
5245 5240 wait = max(HUB_DEFAULT_POPG,
5246 5241 hub_descr->bPwrOn2PwrGood) * 2 * 1000;
5247 5242 }
5248 5243
5249 5244 USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
5250 5245 "hubd_enable_all_port_power: popg=%d wait=%d",
5251 5246 hub_descr->bPwrOn2PwrGood, wait);
5252 5247
5253 5248 /*
5254 5249 * Enable power per port. we ignore gang power and power mask
5255 5250 * and always enable all ports one by one.
5256 5251 */
5257 5252 for (port = 1; port <= hub_descr->bNbrPorts; port++) {
5258 5253 /*
5259 5254 * Transition the port from the Powered Off to the
5260 5255 * Disconnected state by supplying power to the port.
5261 5256 */
5262 5257 USB_DPRINTF_L4(DPRINT_MASK_PORT,
5263 5258 hubd->h_log_handle,
5264 5259 "hubd_enable_all_port_power: power port=%d", port);
5265 5260
5266 5261 (void) hubd_enable_port_power(hubd, port);
5267 5262 }
5268 5263
5269 5264 mutex_exit(HUBD_MUTEX(hubd));
5270 5265 delay(drv_usectohz(wait));
5271 5266 mutex_enter(HUBD_MUTEX(hubd));
5272 5267
5273 5268 /* For retry if any, use some extra delay */
5274 5269 wait = max(wait, hubd_device_delay / 10);
5275 5270
5276 5271 /* Check each port power status for a given usb hub */
5277 5272 for (port = 1; port <= hub_descr->bNbrPorts; port++) {
5278 5273
5279 5274 /* Get port status */
5280 5275 (void) hubd_determine_port_status(hubd, port,
5281 5276 &status, &change, 0);
5282 5277
5283 5278 for (retry = 0; ((!(status & PORT_STATUS_PPS)) &&
5284 5279 (retry < HUBD_PORT_RETRY)); retry++) {
5285 5280
5286 5281 USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
5287 5282 "Retry is in progress %d: port %d status %d",
5288 5283 retry, port, status);
5289 5284
5290 5285 (void) hubd_enable_port_power(hubd, port);
5291 5286
5292 5287 mutex_exit(HUBD_MUTEX(hubd));
5293 5288 delay(drv_usectohz(wait));
5294 5289 mutex_enter(HUBD_MUTEX(hubd));
5295 5290
5296 5291 /* Get port status */
5297 5292 (void) hubd_determine_port_status(hubd, port,
5298 5293 &status, &change, 0);
5299 5294 }
5300 5295
5301 5296 /* Print warning message if port has no power */
5302 5297 if (!(status & PORT_STATUS_PPS)) {
5303 5298
5304 5299 USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
5305 5300 "hubd_enable_all_port_power: port %d power-on "
5306 5301 "failed, port status 0x%x", port, status);
5307 5302 }
5308 5303 }
5309 5304
5310 5305 return (USB_SUCCESS);
5311 5306 }
5312 5307
5313 5308
5314 5309 /*
5315 5310 * hubd_enable_port_power:
5316 5311 * enable individual port power
5317 5312 */
5318 5313 static int
5319 5314 hubd_enable_port_power(hubd_t *hubd, usb_port_t port)
5320 5315 {
5321 5316 int rval;
5322 5317 usb_cr_t completion_reason;
5323 5318 usb_cb_flags_t cb_flags;
5324 5319
5325 5320 USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
5326 5321 "hubd_enable_port_power: port=%d", port);
5327 5322
5328 5323 ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
5329 5324 ASSERT(hubd->h_default_pipe != 0);
5330 5325
5331 5326 mutex_exit(HUBD_MUTEX(hubd));
5332 5327
5333 5328 if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
5334 5329 hubd->h_default_pipe,
5335 5330 HUB_HANDLE_PORT_FEATURE_TYPE,
5336 5331 USB_REQ_SET_FEATURE,
5337 5332 CFS_PORT_POWER,
5338 5333 port,
5339 5334 0, NULL, 0,
5340 5335 &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
5341 5336 USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
5342 5337 "set port power failed (%d 0x%x %d)",
5343 5338 completion_reason, cb_flags, rval);
5344 5339 mutex_enter(HUBD_MUTEX(hubd));
5345 5340
5346 5341 return (USB_FAILURE);
5347 5342 } else {
5348 5343 mutex_enter(HUBD_MUTEX(hubd));
5349 5344 hubd->h_port_state[port] |= PORT_STATUS_PPS;
5350 5345
5351 5346 return (USB_SUCCESS);
5352 5347 }
5353 5348 }
5354 5349
5355 5350
5356 5351 /*
5357 5352 * hubd_disable_all_port_power:
5358 5353 */
5359 5354 static int
5360 5355 hubd_disable_all_port_power(hubd_t *hubd)
5361 5356 {
5362 5357 usb_port_t port;
5363 5358
5364 5359 USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
5365 5360 "hubd_disable_all_port_power");
5366 5361
5367 5362 ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
5368 5363
5369 5364 /*
5370 5365 * disable power per port, ignore gang power and power mask
5371 5366 */
5372 5367 for (port = 1; port <= hubd->h_hub_descr.bNbrPorts; port++) {
5373 5368 (void) hubd_disable_port_power(hubd, port);
5374 5369 }
5375 5370
5376 5371 return (USB_SUCCESS);
5377 5372 }
5378 5373
5379 5374
5380 5375 /*
5381 5376 * hubd_disable_port_power:
5382 5377 * disable individual port power
5383 5378 */
5384 5379 static int
5385 5380 hubd_disable_port_power(hubd_t *hubd, usb_port_t port)
5386 5381 {
5387 5382 int rval;
5388 5383 usb_cr_t completion_reason;
5389 5384 usb_cb_flags_t cb_flags;
5390 5385
5391 5386 USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
5392 5387 "hubd_disable_port_power: port=%d", port);
5393 5388
5394 5389 ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
5395 5390
5396 5391 mutex_exit(HUBD_MUTEX(hubd));
5397 5392
5398 5393 if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
5399 5394 hubd->h_default_pipe,
5400 5395 HUB_HANDLE_PORT_FEATURE_TYPE,
5401 5396 USB_REQ_CLEAR_FEATURE,
5402 5397 CFS_PORT_POWER,
5403 5398 port,
5404 5399 0, NULL, 0,
5405 5400 &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
5406 5401 USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
5407 5402 "clearing port%d power failed (%d 0x%x %d)",
5408 5403 port, completion_reason, cb_flags, rval);
5409 5404
5410 5405 mutex_enter(HUBD_MUTEX(hubd));
5411 5406
5412 5407 return (USB_FAILURE);
5413 5408 } else {
5414 5409
5415 5410 mutex_enter(HUBD_MUTEX(hubd));
5416 5411 ASSERT(completion_reason == 0);
5417 5412 hubd->h_port_state[port] &= ~PORT_STATUS_PPS;
5418 5413
5419 5414 return (USB_SUCCESS);
5420 5415 }
5421 5416 }
5422 5417
5423 5418
5424 5419 /*
5425 5420 * Search the database of user preferences and find out the preferred
5426 5421 * configuration for this new device
5427 5422 */
5428 5423 int
5429 5424 hubd_select_device_configuration(hubd_t *hubd, usb_port_t port,
5430 5425 dev_info_t *child_dip, usba_device_t *child_ud)
5431 5426 {
5432 5427 char *pathname = NULL;
5433 5428 char *tmp_path = NULL;
5434 5429 int user_conf;
5435 5430 int pathlen;
5436 5431 usb_dev_descr_t *usbdev_ptr;
5437 5432 usba_configrec_t *user_pref;
5438 5433
5439 5434 mutex_enter(&child_ud->usb_mutex);
5440 5435 usbdev_ptr = child_ud->usb_dev_descr;
5441 5436 mutex_exit(&child_ud->usb_mutex);
5442 5437
5443 5438 /* try to get pathname for this device */
5444 5439 tmp_path = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
5445 5440 (void) ddi_pathname(child_dip, tmp_path);
5446 5441
5447 5442 pathlen = strlen(tmp_path) + 32;
5448 5443 pathname = kmem_zalloc(pathlen, KM_SLEEP);
5449 5444
5450 5445 /*
5451 5446 * We haven't initialized the node and it doesn't have an address
5452 5447 * yet. Append port number to the physical pathname
5453 5448 */
5454 5449 (void) sprintf(pathname, "%s@%d", tmp_path, port);
5455 5450
5456 5451 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
5457 5452 "hubd_select_device_configuration: Device=%s\n\t"
5458 5453 "Child path=%s",
5459 5454 usba_get_mfg_prod_sn_str(child_dip, tmp_path, MAXPATHLEN),
5460 5455 pathname);
5461 5456 kmem_free(tmp_path, MAXPATHLEN);
5462 5457
5463 5458
5464 5459 /* database search for user preferences */
5465 5460 user_pref = usba_devdb_get_user_preferences(usbdev_ptr->idVendor,
5466 5461 usbdev_ptr->idProduct, child_ud->usb_serialno_str, pathname);
5467 5462
5468 5463 if (user_pref) {
5469 5464 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
5470 5465 "hubd_select_device_configuration: "
5471 5466 "usba_devdb_get_user_preferences "
5472 5467 "return user_conf=%d\npreferred driver=%s path=%s",
5473 5468 user_pref->cfg_index, user_pref->driver,
5474 5469 user_pref->pathname);
5475 5470
5476 5471 user_conf = user_pref->cfg_index;
5477 5472
5478 5473 if (user_pref->driver) {
5479 5474 mutex_enter(&child_ud->usb_mutex);
5480 5475 child_ud->usb_preferred_driver = user_pref->driver;
5481 5476 mutex_exit(&child_ud->usb_mutex);
5482 5477 }
5483 5478 } else {
5484 5479 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
5485 5480 "hubd_select_device_configuration: No match found");
5486 5481
5487 5482 /* select default configuration for this device */
5488 5483 user_conf = USBA_DEV_CONFIG_INDEX_UNDEFINED;
5489 5484 }
5490 5485 kmem_free(pathname, pathlen);
5491 5486
5492 5487 /* if the device has just one configuration, set default value */
5493 5488 if (usbdev_ptr->bNumConfigurations == 1) {
5494 5489 user_conf = USB_DEV_DEFAULT_CONFIG_INDEX;
5495 5490 }
5496 5491
5497 5492 return (user_conf);
5498 5493 }
5499 5494
5500 5495
5501 5496 /*
5502 5497 * Retrieves config cloud for this configuration
5503 5498 */
5504 5499 int
5505 5500 hubd_get_this_config_cloud(hubd_t *hubd, dev_info_t *dip,
5506 5501 usba_device_t *child_ud, uint16_t conf_index)
5507 5502 {
5508 5503 usb_cfg_descr_t *confdescr;
5509 5504 mblk_t *pdata = NULL;
5510 5505 int rval;
5511 5506 size_t size;
5512 5507 char *tmpbuf;
5513 5508 usb_cr_t completion_reason;
5514 5509 usb_cb_flags_t cb_flags;
5515 5510 usb_pipe_handle_t def_ph;
5516 5511
5517 5512 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
5518 5513 "hubd_get_this_config_cloud: conf_index=%d", conf_index);
5519 5514
5520 5515
5521 5516 /* alloc temporary space for config descriptor */
5522 5517 confdescr = (usb_cfg_descr_t *)kmem_zalloc(USB_CFG_DESCR_SIZE,
5523 5518 KM_SLEEP);
5524 5519
5525 5520 /* alloc temporary space for string descriptor */
5526 5521 tmpbuf = kmem_zalloc(USB_MAXSTRINGLEN, KM_SLEEP);
5527 5522
5528 5523 def_ph = usba_get_dflt_pipe_handle(dip);
5529 5524
5530 5525 if ((rval = usb_pipe_sync_ctrl_xfer(dip, def_ph,
5531 5526 USB_DEV_REQ_DEV_TO_HOST | USB_DEV_REQ_TYPE_STANDARD,
5532 5527 USB_REQ_GET_DESCR,
5533 5528 USB_DESCR_TYPE_SETUP_CFG | conf_index,
5534 5529 0,
5535 5530 USB_CFG_DESCR_SIZE,
5536 5531 &pdata,
5537 5532 0,
5538 5533 &completion_reason,
5539 5534 &cb_flags,
5540 5535 0)) == USB_SUCCESS) {
5541 5536
5542 5537 /* this must be true since we didn't allow data underruns */
5543 5538 if (MBLKL(pdata) != USB_CFG_DESCR_SIZE) {
5544 5539 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
5545 5540 "device returned incorrect configuration "
5546 5541 "descriptor size.");
5547 5542
5548 5543 rval = USB_FAILURE;
5549 5544 goto done;
5550 5545 }
5551 5546
5552 5547 /*
5553 5548 * Parse the configuration descriptor
5554 5549 */
5555 5550 size = usb_parse_cfg_descr(pdata->b_rptr,
5556 5551 MBLKL(pdata), confdescr,
5557 5552 USB_CFG_DESCR_SIZE);
5558 5553
5559 5554 /* if parse cfg descr error, it should return failure */
5560 5555 if (size == USB_PARSE_ERROR) {
5561 5556
5562 5557 if (pdata->b_rptr[1] != USB_DESCR_TYPE_CFG) {
5563 5558 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG,
5564 5559 hubd->h_log_handle,
5565 5560 "device returned incorrect "
5566 5561 "configuration descriptor type.");
5567 5562 }
5568 5563 rval = USB_FAILURE;
5569 5564 goto done;
5570 5565 }
5571 5566
5572 5567 if (confdescr->wTotalLength < USB_CFG_DESCR_SIZE) {
5573 5568 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG,
5574 5569 hubd->h_log_handle,
5575 5570 "device returned incorrect "
5576 5571 "configuration descriptor size.");
5577 5572
5578 5573 rval = USB_FAILURE;
5579 5574 goto done;
5580 5575 }
5581 5576
5582 5577 freemsg(pdata);
5583 5578 pdata = NULL;
5584 5579
5585 5580 /* Now fetch the complete config cloud */
5586 5581 if ((rval = usb_pipe_sync_ctrl_xfer(dip, def_ph,
5587 5582 USB_DEV_REQ_DEV_TO_HOST | USB_DEV_REQ_TYPE_STANDARD,
5588 5583 USB_REQ_GET_DESCR,
5589 5584 USB_DESCR_TYPE_SETUP_CFG | conf_index,
5590 5585 0,
5591 5586 confdescr->wTotalLength,
5592 5587 &pdata,
5593 5588 0,
5594 5589 &completion_reason,
5595 5590 &cb_flags,
5596 5591 0)) == USB_SUCCESS) {
5597 5592
5598 5593 if (MBLKL(pdata) !=
5599 5594 confdescr->wTotalLength) {
5600 5595
5601 5596 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG,
5602 5597 hubd->h_log_handle,
5603 5598 "device returned incorrect "
5604 5599 "configuration descriptor.");
5605 5600
5606 5601 rval = USB_FAILURE;
5607 5602 goto done;
5608 5603 }
5609 5604
5610 5605 /*
5611 5606 * copy config descriptor into usba_device
5612 5607 */
5613 5608 mutex_enter(&child_ud->usb_mutex);
5614 5609 child_ud->usb_cfg_array[conf_index] =
5615 5610 kmem_alloc(confdescr->wTotalLength, KM_SLEEP);
5616 5611 child_ud->usb_cfg_array_len[conf_index] =
5617 5612 confdescr->wTotalLength;
5618 5613 bcopy((caddr_t)pdata->b_rptr,
5619 5614 (caddr_t)child_ud->usb_cfg_array[conf_index],
5620 5615 confdescr->wTotalLength);
5621 5616 mutex_exit(&child_ud->usb_mutex);
5622 5617
5623 5618 /*
5624 5619 * retrieve string descriptor describing this
5625 5620 * configuration
5626 5621 */
5627 5622 if (confdescr->iConfiguration) {
5628 5623
5629 5624 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG,
5630 5625 hubd->h_log_handle,
5631 5626 "Get conf str descr for config_index=%d",
5632 5627 conf_index);
5633 5628
5634 5629 /*
5635 5630 * Now fetch the string descriptor describing
5636 5631 * this configuration
5637 5632 */
5638 5633 if ((rval = usb_get_string_descr(dip,
5639 5634 USB_LANG_ID, confdescr->iConfiguration,
5640 5635 tmpbuf, USB_MAXSTRINGLEN)) ==
5641 5636 USB_SUCCESS) {
5642 5637 size = strlen(tmpbuf);
5643 5638 if (size > 0) {
5644 5639 child_ud->usb_cfg_str_descr
5645 5640 [conf_index] = (char *)
5646 5641 kmem_zalloc(size + 1,
5647 5642 KM_SLEEP);
5648 5643 (void) strcpy(
5649 5644 child_ud->usb_cfg_str_descr
5650 5645 [conf_index], tmpbuf);
5651 5646 }
5652 5647 } else {
5653 5648 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG,
5654 5649 hubd->h_log_handle,
5655 5650 "hubd_get_this_config_cloud: "
5656 5651 "getting config string (%d) "
5657 5652 "failed",
5658 5653 confdescr->iConfiguration);
5659 5654
5660 5655 /* ignore this error */
5661 5656 rval = USB_SUCCESS;
5662 5657 }
5663 5658 }
5664 5659 }
5665 5660 }
5666 5661
5667 5662 done:
5668 5663 if (rval != USB_SUCCESS) {
5669 5664 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
5670 5665 "hubd_get_this_config_cloud: "
5671 5666 "error in retrieving config descriptor for "
5672 5667 "config index=%d rval=%d cr=%d",
5673 5668 conf_index, rval, completion_reason);
5674 5669 }
5675 5670
5676 5671 if (pdata) {
5677 5672 freemsg(pdata);
5678 5673 pdata = NULL;
5679 5674 }
5680 5675
5681 5676 kmem_free(confdescr, USB_CFG_DESCR_SIZE);
5682 5677 kmem_free(tmpbuf, USB_MAXSTRINGLEN);
5683 5678
5684 5679 return (rval);
5685 5680 }
5686 5681
5687 5682
5688 5683 /*
5689 5684 * Retrieves the entire config cloud for all configurations of the device
5690 5685 */
5691 5686 int
5692 5687 hubd_get_all_device_config_cloud(hubd_t *hubd, dev_info_t *dip,
5693 5688 usba_device_t *child_ud)
5694 5689 {
5695 5690 int rval = USB_SUCCESS;
5696 5691 int ncfgs;
5697 5692 uint16_t size;
5698 5693 uint16_t conf_index;
5699 5694 uchar_t **cfg_array;
5700 5695 uint16_t *cfg_array_len;
5701 5696 char **str_descr;
5702 5697
5703 5698 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
5704 5699 "hubd_get_all_device_config_cloud: Start");
5705 5700
5706 5701 /* alloc pointer array for conf. descriptors */
5707 5702 mutex_enter(&child_ud->usb_mutex);
5708 5703 ncfgs = child_ud->usb_n_cfgs;
5709 5704 mutex_exit(&child_ud->usb_mutex);
5710 5705
5711 5706 size = sizeof (uchar_t *) * ncfgs;
5712 5707 cfg_array = kmem_zalloc(size, KM_SLEEP);
5713 5708 cfg_array_len = kmem_zalloc(ncfgs * sizeof (uint16_t), KM_SLEEP);
5714 5709 str_descr = kmem_zalloc(size, KM_SLEEP);
5715 5710
5716 5711 mutex_enter(&child_ud->usb_mutex);
5717 5712 child_ud->usb_cfg_array = cfg_array;
5718 5713 child_ud->usb_cfg_array_len = cfg_array_len;
5719 5714 child_ud->usb_cfg_array_length = size;
5720 5715 child_ud->usb_cfg_array_len_length = ncfgs * sizeof (uint16_t);
5721 5716 child_ud->usb_cfg_str_descr = str_descr;
5722 5717 mutex_exit(&child_ud->usb_mutex);
5723 5718
5724 5719 /* Get configuration descriptor for each configuration */
5725 5720 for (conf_index = 0; (conf_index < ncfgs) &&
5726 5721 (rval == USB_SUCCESS); conf_index++) {
5727 5722
5728 5723 rval = hubd_get_this_config_cloud(hubd, dip, child_ud,
5729 5724 conf_index);
5730 5725 }
5731 5726
5732 5727 return (rval);
5733 5728 }
5734 5729
5735 5730
5736 5731 /*
5737 5732 * hubd_ready_device:
5738 5733 * Update the usba_device structure
5739 5734 * Set the given configuration
5740 5735 * Prepares the device node for driver to online. If an existing
5741 5736 * OBP node is found, it will switch to the OBP node.
5742 5737 */
5743 5738 dev_info_t *
5744 5739 hubd_ready_device(hubd_t *hubd, dev_info_t *child_dip, usba_device_t *child_ud,
5745 5740 uint_t config_index)
5746 5741 {
5747 5742 usb_cr_t completion_reason;
5748 5743 usb_cb_flags_t cb_flags;
5749 5744 size_t size;
5750 5745 usb_cfg_descr_t config_descriptor;
5751 5746 usb_pipe_handle_t def_ph;
5752 5747 usba_pipe_handle_data_t *ph;
5753 5748
5754 5749 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
5755 5750 "hubd_ready_device: dip=0x%p, user_conf_index=%d",
5756 5751 (void *)child_dip, config_index);
5757 5752
5758 5753 size = usb_parse_cfg_descr(
5759 5754 child_ud->usb_cfg_array[config_index], USB_CFG_DESCR_SIZE,
5760 5755 &config_descriptor, USB_CFG_DESCR_SIZE);
5761 5756 ASSERT(size == USB_CFG_DESCR_SIZE);
5762 5757
5763 5758 def_ph = usba_get_dflt_pipe_handle(child_dip);
5764 5759
5765 5760 /* Set the configuration */
5766 5761 (void) usb_pipe_sync_ctrl_xfer(child_dip, def_ph,
5767 5762 USB_DEV_REQ_HOST_TO_DEV,
5768 5763 USB_REQ_SET_CFG, /* bRequest */
5769 5764 config_descriptor.bConfigurationValue, /* wValue */
5770 5765 0, /* wIndex */
5771 5766 0, /* wLength */
5772 5767 NULL,
5773 5768 0,
5774 5769 &completion_reason,
5775 5770 &cb_flags,
5776 5771 0);
5777 5772
5778 5773 mutex_enter(&child_ud->usb_mutex);
5779 5774 child_ud->usb_active_cfg_ndx = config_index;
5780 5775 child_ud->usb_cfg = child_ud->usb_cfg_array[config_index];
5781 5776 child_ud->usb_cfg_length = config_descriptor.wTotalLength;
5782 5777 child_ud->usb_cfg_value = config_descriptor.bConfigurationValue;
5783 5778 child_ud->usb_n_ifs = config_descriptor.bNumInterfaces;
5784 5779 child_ud->usb_dip = child_dip;
5785 5780
5786 5781 child_ud->usb_client_flags = kmem_zalloc(
5787 5782 child_ud->usb_n_ifs * USBA_CLIENT_FLAG_SIZE, KM_SLEEP);
5788 5783
5789 5784 child_ud->usb_client_attach_list = kmem_zalloc(
5790 5785 child_ud->usb_n_ifs *
5791 5786 sizeof (*child_ud->usb_client_attach_list), KM_SLEEP);
5792 5787
5793 5788 child_ud->usb_client_ev_cb_list = kmem_zalloc(
5794 5789 child_ud->usb_n_ifs *
5795 5790 sizeof (*child_ud->usb_client_ev_cb_list), KM_SLEEP);
5796 5791
5797 5792 mutex_exit(&child_ud->usb_mutex);
5798 5793
5799 5794 /* ready the device node */
5800 5795 child_dip = usba_ready_device_node(child_dip);
5801 5796
5802 5797 /* set owner of default pipe to child dip */
5803 5798 ph = usba_get_ph_data(def_ph);
5804 5799 mutex_enter(&ph->p_mutex);
5805 5800 mutex_enter(&ph->p_ph_impl->usba_ph_mutex);
5806 5801 ph->p_ph_impl->usba_ph_dip = ph->p_dip = child_dip;
5807 5802 mutex_exit(&ph->p_ph_impl->usba_ph_mutex);
5808 5803 mutex_exit(&ph->p_mutex);
5809 5804
5810 5805 return (child_dip);
5811 5806 }
5812 5807
5813 5808
5814 5809 /*
5815 5810 * hubd_create_child
5816 5811 * - create child dip
5817 5812 * - open default pipe
5818 5813 * - get device descriptor
5819 5814 * - set the address
5820 5815 * - get device string descriptors
5821 5816 * - get the entire config cloud (all configurations) of the device
5822 5817 * - set user preferred configuration
5823 5818 * - close default pipe
5824 5819 * - load appropriate driver(s)
5825 5820 */
5826 5821 static int
5827 5822 hubd_create_child(dev_info_t *dip,
5828 5823 hubd_t *hubd,
5829 5824 usba_device_t *hubd_ud,
5830 5825 usb_port_status_t port_status,
5831 5826 usb_port_t port,
5832 5827 int iteration)
5833 5828 {
5834 5829 dev_info_t *child_dip = NULL;
5835 5830 usb_dev_descr_t usb_dev_descr;
5836 5831 int rval;
5837 5832 usba_device_t *child_ud = NULL;
5838 5833 usba_device_t *parent_ud = NULL;
5839 5834 usb_pipe_handle_t ph = NULL; /* default pipe handle */
5840 5835 mblk_t *pdata = NULL;
5841 5836 usb_cr_t completion_reason;
5842 5837 int user_conf_index;
5843 5838 uint_t config_index;
5844 5839 usb_cb_flags_t cb_flags;
5845 5840 uchar_t address = 0;
5846 5841 uint16_t length;
5847 5842 size_t size;
5848 5843 usb_addr_t parent_usb_addr;
5849 5844 usb_port_t parent_usb_port;
5850 5845 usba_device_t *parent_usba_dev;
5851 5846 usb_port_status_t parent_port_status;
5852 5847
5853 5848 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
5854 5849 "hubd_create_child: port=%d", port);
5855 5850
5856 5851 ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
5857 5852 ASSERT(hubd->h_usba_devices[port] == NULL);
5858 5853
5859 5854 mutex_exit(HUBD_MUTEX(hubd));
5860 5855
5861 5856 /*
5862 5857 * create a dip which can be used to open the pipe. we set
5863 5858 * the name after getting the descriptors from the device
5864 5859 */
5865 5860 rval = usba_create_child_devi(dip,
5866 5861 "device", /* driver name */
5867 5862 hubd_ud->usb_hcdi_ops, /* usba_hcdi ops */
5868 5863 hubd_ud->usb_root_hub_dip,
5869 5864 port_status, /* low speed device */
5870 5865 child_ud,
5871 5866 &child_dip);
5872 5867
5873 5868 if (rval != USB_SUCCESS) {
5874 5869
5875 5870 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
5876 5871 "usb_create_child_devi failed (%d)", rval);
5877 5872
5878 5873 goto fail_cleanup;
5879 5874 }
5880 5875
5881 5876 child_ud = usba_get_usba_device(child_dip);
5882 5877 ASSERT(child_ud != NULL);
5883 5878
5884 5879 parent_ud = hubd->h_usba_device;
5885 5880 mutex_enter(&parent_ud->usb_mutex);
5886 5881 parent_port_status = parent_ud->usb_port_status;
5887 5882
5888 5883 /*
5889 5884 * To support split transactions, update address and port
5890 5885 * of high speed hub to which given device is connected.
5891 5886 */
5892 5887 if (parent_port_status == USBA_HIGH_SPEED_DEV) {
5893 5888 parent_usba_dev = parent_ud;
5894 5889 parent_usb_addr = parent_ud->usb_addr;
5895 5890 parent_usb_port = port;
5896 5891 } else {
5897 5892 parent_usba_dev = parent_ud->usb_hs_hub_usba_dev;
5898 5893 parent_usb_addr = parent_ud->usb_hs_hub_addr;
5899 5894 parent_usb_port = parent_ud->usb_hs_hub_port;
5900 5895 }
5901 5896 mutex_exit(&parent_ud->usb_mutex);
5902 5897
5903 5898 mutex_enter(&child_ud->usb_mutex);
5904 5899 address = child_ud->usb_addr;
5905 5900 child_ud->usb_addr = 0;
5906 5901 child_ud->usb_dev_descr = kmem_alloc(sizeof (usb_dev_descr_t),
5907 5902 KM_SLEEP);
5908 5903 bzero(&usb_dev_descr, sizeof (usb_dev_descr_t));
5909 5904 usb_dev_descr.bMaxPacketSize0 =
5910 5905 (port_status == USBA_LOW_SPEED_DEV) ? 8 : 64;
5911 5906 bcopy(&usb_dev_descr, child_ud->usb_dev_descr,
5912 5907 sizeof (usb_dev_descr_t));
5913 5908 child_ud->usb_port = port;
5914 5909 child_ud->usb_hs_hub_usba_dev = parent_usba_dev;
5915 5910 child_ud->usb_hs_hub_addr = parent_usb_addr;
5916 5911 child_ud->usb_hs_hub_port = parent_usb_port;
5917 5912 mutex_exit(&child_ud->usb_mutex);
5918 5913
5919 5914 /* Open the default pipe */
5920 5915 if ((rval = usb_pipe_open(child_dip, NULL, NULL,
5921 5916 USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, &ph)) != USB_SUCCESS) {
5922 5917 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
5923 5918 "usb_pipe_open failed (%d)", rval);
5924 5919
5925 5920 goto fail_cleanup;
5926 5921 }
5927 5922
5928 5923 /*
5929 5924 * get device descriptor
5930 5925 */
5931 5926 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
5932 5927 "hubd_create_child: get device descriptor: 64 bytes");
5933 5928
5934 5929 rval = usb_pipe_sync_ctrl_xfer(child_dip, ph,
5935 5930 USB_DEV_REQ_DEV_TO_HOST | USB_DEV_REQ_TYPE_STANDARD,
5936 5931 USB_REQ_GET_DESCR, /* bRequest */
5937 5932 USB_DESCR_TYPE_SETUP_DEV, /* wValue */
5938 5933 0, /* wIndex */
5939 5934 64, /* wLength */
5940 5935 &pdata, USB_ATTRS_SHORT_XFER_OK,
5941 5936 &completion_reason, &cb_flags, 0);
5942 5937
5943 5938 if ((rval != USB_SUCCESS) &&
5944 5939 (!((completion_reason == USB_CR_DATA_OVERRUN) && pdata))) {
5945 5940
5946 5941 /*
5947 5942 * rval != USB_SUCCESS AND
5948 5943 * completion_reason != USB_CR_DATA_OVERRUN
5949 5944 * pdata could be != NULL.
5950 5945 * Free pdata now to prevent memory leak.
5951 5946 */
5952 5947 freemsg(pdata);
5953 5948 pdata = NULL;
5954 5949
5955 5950 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
5956 5951 "hubd_create_child: get device descriptor: 8 bytes");
5957 5952
5958 5953 rval = usb_pipe_sync_ctrl_xfer(child_dip, ph,
5959 5954 USB_DEV_REQ_DEV_TO_HOST | USB_DEV_REQ_TYPE_STANDARD,
5960 5955 USB_REQ_GET_DESCR, /* bRequest */
5961 5956 USB_DESCR_TYPE_SETUP_DEV, /* wValue */
5962 5957 0, /* wIndex */
5963 5958 8, /* wLength */
5964 5959 &pdata, USB_ATTRS_NONE,
5965 5960 &completion_reason, &cb_flags, 0);
5966 5961
5967 5962 if (rval != USB_SUCCESS) {
5968 5963 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
5969 5964 "getting device descriptor failed (%s 0x%x %d)",
5970 5965 usb_str_cr(completion_reason), cb_flags, rval);
5971 5966 goto fail_cleanup;
5972 5967 }
5973 5968 } else {
5974 5969 ASSERT(completion_reason == USB_CR_OK);
5975 5970 }
5976 5971
5977 5972 ASSERT(pdata != NULL);
5978 5973
5979 5974 size = usb_parse_dev_descr(
5980 5975 pdata->b_rptr,
5981 5976 MBLKL(pdata),
5982 5977 &usb_dev_descr,
5983 5978 sizeof (usb_dev_descr_t));
5984 5979
5985 5980 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
5986 5981 "parsing device descriptor returned %lu", size);
5987 5982
5988 5983 length = *(pdata->b_rptr);
5989 5984 freemsg(pdata);
5990 5985 pdata = NULL;
5991 5986 if (size < 8) {
5992 5987 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
5993 5988 "get device descriptor returned %lu bytes", size);
5994 5989
5995 5990 goto fail_cleanup;
5996 5991 }
5997 5992
5998 5993 if (length < 8) {
5999 5994 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
6000 5995 "fail enumeration: bLength=%d", length);
6001 5996
6002 5997 goto fail_cleanup;
6003 5998 }
6004 5999
6005 6000 /* Set the address of the device */
6006 6001 if ((rval = usb_pipe_sync_ctrl_xfer(child_dip, ph,
6007 6002 USB_DEV_REQ_HOST_TO_DEV,
6008 6003 USB_REQ_SET_ADDRESS, /* bRequest */
6009 6004 address, /* wValue */
6010 6005 0, /* wIndex */
6011 6006 0, /* wLength */
6012 6007 NULL, 0,
6013 6008 &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
6014 6009 char buffer[64];
6015 6010 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
6016 6011 "setting address failed (cr=%s cb_flags=%s rval=%d)",
6017 6012 usb_str_cr(completion_reason),
6018 6013 usb_str_cb_flags(cb_flags, buffer, sizeof (buffer)),
6019 6014 rval);
6020 6015
6021 6016 goto fail_cleanup;
6022 6017 }
6023 6018
6024 6019 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
6025 6020 "set address 0x%x done", address);
6026 6021
6027 6022 /* now close the pipe for addr 0 */
6028 6023 usb_pipe_close(child_dip, ph,
6029 6024 USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, NULL, NULL);
6030 6025
6031 6026 /*
6032 6027 * This delay is important for the CATC hub to enumerate
6033 6028 * But, avoid delay in the first iteration
6034 6029 */
6035 6030 if (iteration) {
6036 6031 delay(drv_usectohz(hubd_device_delay/100));
6037 6032 }
6038 6033
6039 6034 /* assign the address in the usba_device structure */
6040 6035 mutex_enter(&child_ud->usb_mutex);
6041 6036 child_ud->usb_addr = address;
6042 6037 child_ud->usb_no_cpr = 0;
6043 6038 child_ud->usb_port_status = port_status;
6044 6039 /* save this device descriptor */
6045 6040 bcopy(&usb_dev_descr, child_ud->usb_dev_descr,
6046 6041 sizeof (usb_dev_descr_t));
6047 6042 child_ud->usb_n_cfgs = usb_dev_descr.bNumConfigurations;
6048 6043 mutex_exit(&child_ud->usb_mutex);
6049 6044
6050 6045 /* re-open the pipe for the device with the new address */
6051 6046 if ((rval = usb_pipe_open(child_dip, NULL, NULL,
6052 6047 USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, &ph)) != USB_SUCCESS) {
6053 6048 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
6054 6049 "usb_pipe_open failed (%d)", rval);
6055 6050
6056 6051 goto fail_cleanup;
6057 6052 }
6058 6053
6059 6054 /*
6060 6055 * Get full device descriptor only if we have not received full
6061 6056 * device descriptor earlier.
6062 6057 */
6063 6058 if (size < length) {
6064 6059 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
6065 6060 "hubd_create_child: get full device descriptor: "
6066 6061 "%d bytes", length);
6067 6062
6068 6063 if ((rval = usb_pipe_sync_ctrl_xfer(child_dip, ph,
6069 6064 USB_DEV_REQ_DEV_TO_HOST | USB_DEV_REQ_TYPE_STANDARD,
6070 6065 USB_REQ_GET_DESCR, /* bRequest */
6071 6066 USB_DESCR_TYPE_SETUP_DEV, /* wValue */
6072 6067 0, /* wIndex */
6073 6068 length, /* wLength */
6074 6069 &pdata, 0,
6075 6070 &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
6076 6071 freemsg(pdata);
6077 6072 pdata = NULL;
6078 6073
6079 6074 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG,
6080 6075 hubd->h_log_handle,
6081 6076 "hubd_create_child: get full device descriptor: "
6082 6077 "64 bytes");
6083 6078
6084 6079 rval = usb_pipe_sync_ctrl_xfer(child_dip, ph,
6085 6080 USB_DEV_REQ_DEV_TO_HOST |
6086 6081 USB_DEV_REQ_TYPE_STANDARD,
6087 6082 USB_REQ_GET_DESCR, /* bRequest */
6088 6083 USB_DESCR_TYPE_SETUP_DEV, /* wValue */
6089 6084 0, /* wIndex */
6090 6085 64, /* wLength */
6091 6086 &pdata, USB_ATTRS_SHORT_XFER_OK,
6092 6087 &completion_reason, &cb_flags, 0);
6093 6088
6094 6089 /* we have to trust the data now */
6095 6090 if (pdata) {
6096 6091 int len = *(pdata->b_rptr);
6097 6092
6098 6093 length = MBLKL(pdata);
6099 6094 if (length < len) {
6100 6095
6101 6096 goto fail_cleanup;
6102 6097 }
6103 6098 } else if (rval != USB_SUCCESS) {
6104 6099 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG,
6105 6100 hubd->h_log_handle,
6106 6101 "getting device descriptor failed "
6107 6102 "(%d 0x%x %d)",
6108 6103 completion_reason, cb_flags, rval);
6109 6104
6110 6105 goto fail_cleanup;
6111 6106 }
6112 6107 }
6113 6108
6114 6109 size = usb_parse_dev_descr(
6115 6110 pdata->b_rptr,
6116 6111 MBLKL(pdata),
6117 6112 &usb_dev_descr,
6118 6113 sizeof (usb_dev_descr_t));
6119 6114
6120 6115 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
6121 6116 "parsing device descriptor returned %lu", size);
6122 6117
6123 6118 /*
6124 6119 * For now, free the data
6125 6120 * eventually, each configuration may need to be looked at
6126 6121 */
6127 6122 freemsg(pdata);
6128 6123 pdata = NULL;
6129 6124
6130 6125 if (size != USB_DEV_DESCR_SIZE) {
6131 6126 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
6132 6127 "fail enumeration: descriptor size=%lu "
6133 6128 "expected size=%u", size, USB_DEV_DESCR_SIZE);
6134 6129
6135 6130 goto fail_cleanup;
6136 6131 }
6137 6132
6138 6133 /*
6139 6134 * save the device descriptor in usba_device since it is needed
6140 6135 * later on again
6141 6136 */
6142 6137 mutex_enter(&child_ud->usb_mutex);
6143 6138 bcopy(&usb_dev_descr, child_ud->usb_dev_descr,
6144 6139 sizeof (usb_dev_descr_t));
6145 6140 child_ud->usb_n_cfgs = usb_dev_descr.bNumConfigurations;
6146 6141 mutex_exit(&child_ud->usb_mutex);
6147 6142 }
6148 6143
6149 6144 if (usb_dev_descr.bNumConfigurations == 0) {
6150 6145 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
6151 6146 "device descriptor:\n\t"
6152 6147 "l=0x%x type=0x%x USB=0x%x class=0x%x subclass=0x%x\n\t"
6153 6148 "protocol=0x%x maxpktsize=0x%x "
6154 6149 "Vid=0x%x Pid=0x%x rel=0x%x\n\t"
6155 6150 "Mfg=0x%x P=0x%x sn=0x%x #config=0x%x",
6156 6151 usb_dev_descr.bLength, usb_dev_descr.bDescriptorType,
6157 6152 usb_dev_descr.bcdUSB, usb_dev_descr.bDeviceClass,
6158 6153 usb_dev_descr.bDeviceSubClass,
6159 6154 usb_dev_descr.bDeviceProtocol,
6160 6155 usb_dev_descr.bMaxPacketSize0,
6161 6156 usb_dev_descr.idVendor,
6162 6157 usb_dev_descr.idProduct, usb_dev_descr.bcdDevice,
6163 6158 usb_dev_descr.iManufacturer, usb_dev_descr.iProduct,
6164 6159 usb_dev_descr.iSerialNumber,
6165 6160 usb_dev_descr.bNumConfigurations);
6166 6161 goto fail_cleanup;
6167 6162 }
6168 6163
6169 6164
6170 6165 /* get the device string descriptor(s) */
6171 6166 usba_get_dev_string_descrs(child_dip, child_ud);
6172 6167
6173 6168 /* retrieve config cloud for all configurations */
6174 6169 rval = hubd_get_all_device_config_cloud(hubd, child_dip, child_ud);
6175 6170 if (rval != USB_SUCCESS) {
6176 6171 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
6177 6172 "failed to get configuration descriptor(s)");
6178 6173
6179 6174 goto fail_cleanup;
6180 6175 }
6181 6176
6182 6177 /* get the preferred configuration for this device */
6183 6178 user_conf_index = hubd_select_device_configuration(hubd, port,
6184 6179 child_dip, child_ud);
6185 6180
6186 6181 /* Check if the user selected configuration index is in range */
6187 6182 if ((user_conf_index >= usb_dev_descr.bNumConfigurations) ||
6188 6183 (user_conf_index < 0)) {
6189 6184 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
6190 6185 "Configuration index for device idVendor=%d "
6191 6186 "idProduct=%d is=%d, and is out of range[0..%d]",
6192 6187 usb_dev_descr.idVendor, usb_dev_descr.idProduct,
6193 6188 user_conf_index, usb_dev_descr.bNumConfigurations - 1);
6194 6189
6195 6190 /* treat this as user didn't specify configuration */
6196 6191 user_conf_index = USBA_DEV_CONFIG_INDEX_UNDEFINED;
6197 6192 }
6198 6193
6199 6194
6200 6195 /*
6201 6196 * Warn users of a performance hit if connecting a
6202 6197 * High Speed behind a 1.1 hub, which is behind a
6203 6198 * 2.0 port.
6204 6199 */
6205 6200 if ((parent_port_status != USBA_HIGH_SPEED_DEV) &&
6206 6201 !(usba_is_root_hub(parent_ud->usb_dip)) &&
6207 6202 (parent_usb_addr)) {
6208 6203
6209 6204 /*
6210 6205 * Now that we know the root port is a high speed port
6211 6206 * and that the parent port is not a high speed port,
6212 6207 * let's find out if the device itself is a high speed
6213 6208 * device. If it is a high speed device,
6214 6209 * USB_DESCR_TYPE_SETUP_DEV_QLF should return a value,
6215 6210 * otherwise the command will fail.
6216 6211 */
6217 6212 rval = usb_pipe_sync_ctrl_xfer(child_dip, ph,
6218 6213 USB_DEV_REQ_DEV_TO_HOST | USB_DEV_REQ_TYPE_STANDARD,
6219 6214 USB_REQ_GET_DESCR, /* bRequest */
6220 6215 USB_DESCR_TYPE_SETUP_DEV_QLF, /* wValue */
6221 6216 0, /* wIndex */
6222 6217 10, /* wLength */
6223 6218 &pdata, USB_ATTRS_SHORT_XFER_OK,
6224 6219 &completion_reason, &cb_flags, 0);
6225 6220
6226 6221 if (pdata) {
6227 6222 freemsg(pdata);
6228 6223 pdata = NULL;
6229 6224 }
6230 6225
6231 6226 /*
6232 6227 * USB_DESCR_TYPE_SETUP_DEV_QLF query was successful
6233 6228 * that means this is a high speed device behind a
6234 6229 * high speed root hub, but running at full speed
6235 6230 * because there is a full speed hub in the middle.
6236 6231 */
6237 6232 if (rval == USB_SUCCESS) {
6238 6233 USB_DPRINTF_L0(DPRINT_MASK_HOTPLUG,
6239 6234 hubd->h_log_handle,
6240 6235 "Connecting a high speed device to a "
6241 6236 "non high speed hub (port %d) will result "
6242 6237 "in a loss of performance. Please connect "
6243 6238 "the device to a high speed hub to get "
6244 6239 "the maximum performance.",
6245 6240 port);
6246 6241 }
6247 6242 }
6248 6243
6249 6244 /*
6250 6245 * Now we try to online the device by attaching a driver
6251 6246 * The following truth table illustrates the logic:-
6252 6247 * Cfgndx Driver Action
6253 6248 * 0 0 loop all configs for driver with full
6254 6249 * compatible properties.
6255 6250 * 0 1 set first configuration,
6256 6251 * compatible prop = drivername.
6257 6252 * 1 0 Set config, full compatible prop
6258 6253 * 1 1 Set config, compatible prop = drivername.
6259 6254 *
6260 6255 * Note:
6261 6256 * cfgndx = user_conf_index
6262 6257 * Driver = usb_preferred_driver
6263 6258 */
6264 6259 if (user_conf_index == USBA_DEV_CONFIG_INDEX_UNDEFINED) {
6265 6260 if (child_ud->usb_preferred_driver) {
6266 6261 /*
6267 6262 * It is the job of the "preferred driver" to put the
6268 6263 * device in the desired configuration. Till then
6269 6264 * put the device in config index 0.
6270 6265 */
6271 6266 if ((rval = usba_hubdi_check_power_budget(dip, child_ud,
6272 6267 USB_DEV_DEFAULT_CONFIG_INDEX)) != USB_SUCCESS) {
6273 6268
6274 6269 goto fail_cleanup;
6275 6270 }
6276 6271
6277 6272 child_dip = hubd_ready_device(hubd, child_dip,
6278 6273 child_ud, USB_DEV_DEFAULT_CONFIG_INDEX);
6279 6274
6280 6275 /*
6281 6276 * Assign the dip before onlining to avoid race
6282 6277 * with busctl
6283 6278 */
6284 6279 mutex_enter(HUBD_MUTEX(hubd));
6285 6280 hubd->h_children_dips[port] = child_dip;
6286 6281 mutex_exit(HUBD_MUTEX(hubd));
6287 6282
6288 6283 (void) usba_bind_driver(child_dip);
6289 6284 } else {
6290 6285 /*
6291 6286 * loop through all the configurations to see if we
6292 6287 * can find a driver for any one config. If not, set
6293 6288 * the device in config_index 0
6294 6289 */
6295 6290 rval = USB_FAILURE;
6296 6291 for (config_index = 0;
6297 6292 (config_index < usb_dev_descr.bNumConfigurations) &&
6298 6293 (rval != USB_SUCCESS); config_index++) {
6299 6294
6300 6295 child_dip = hubd_ready_device(hubd, child_dip,
6301 6296 child_ud, config_index);
6302 6297
6303 6298 /*
6304 6299 * Assign the dip before onlining to avoid race
6305 6300 * with busctl
6306 6301 */
6307 6302 mutex_enter(HUBD_MUTEX(hubd));
6308 6303 hubd->h_children_dips[port] = child_dip;
6309 6304 mutex_exit(HUBD_MUTEX(hubd));
6310 6305
6311 6306 rval = usba_bind_driver(child_dip);
6312 6307
6313 6308 /*
6314 6309 * Normally power budget should be checked
6315 6310 * before device is configured. A failure in
6316 6311 * power budget checking will stop the device
6317 6312 * from being configured with current
6318 6313 * config_index and may enable the device to
6319 6314 * be configured in another configuration.
6320 6315 * This may break the user experience that a
6321 6316 * device which previously worked in config
6322 6317 * A now works in config B after power budget
6323 6318 * control is enabled. To avoid such situation,
6324 6319 * power budget checking is moved here and will
6325 6320 * fail the child creation directly if config
6326 6321 * A exceeds the power available.
6327 6322 */
6328 6323 if (rval == USB_SUCCESS) {
6329 6324 if ((usba_hubdi_check_power_budget(dip,
6330 6325 child_ud, config_index)) !=
6331 6326 USB_SUCCESS) {
6332 6327
6333 6328 goto fail_cleanup;
6334 6329 }
6335 6330 }
6336 6331 }
6337 6332 if (rval != USB_SUCCESS) {
6338 6333
6339 6334 if ((usba_hubdi_check_power_budget(dip,
6340 6335 child_ud, 0)) != USB_SUCCESS) {
6341 6336
6342 6337 goto fail_cleanup;
6343 6338 }
6344 6339
6345 6340 child_dip = hubd_ready_device(hubd, child_dip,
6346 6341 child_ud, 0);
6347 6342 mutex_enter(HUBD_MUTEX(hubd));
6348 6343 hubd->h_children_dips[port] = child_dip;
6349 6344 mutex_exit(HUBD_MUTEX(hubd));
6350 6345 }
6351 6346 } /* end else loop all configs */
6352 6347 } else {
6353 6348
6354 6349 if ((usba_hubdi_check_power_budget(dip, child_ud,
6355 6350 (uint_t)user_conf_index)) != USB_SUCCESS) {
6356 6351
6357 6352 goto fail_cleanup;
6358 6353 }
6359 6354
6360 6355 child_dip = hubd_ready_device(hubd, child_dip,
6361 6356 child_ud, (uint_t)user_conf_index);
6362 6357
6363 6358 /*
6364 6359 * Assign the dip before onlining to avoid race
6365 6360 * with busctl
6366 6361 */
6367 6362 mutex_enter(HUBD_MUTEX(hubd));
6368 6363 hubd->h_children_dips[port] = child_dip;
6369 6364 mutex_exit(HUBD_MUTEX(hubd));
6370 6365
6371 6366 (void) usba_bind_driver(child_dip);
6372 6367 }
6373 6368
6374 6369 usba_hubdi_decr_power_budget(dip, child_ud);
6375 6370
6376 6371 mutex_enter(HUBD_MUTEX(hubd));
6377 6372 if (hubd->h_usba_devices[port] == NULL) {
6378 6373 hubd->h_usba_devices[port] = usba_get_usba_device(child_dip);
6379 6374 } else {
6380 6375 ASSERT(hubd->h_usba_devices[port] ==
6381 6376 usba_get_usba_device(child_dip));
6382 6377 }
6383 6378
6384 6379 return (USB_SUCCESS);
6385 6380
6386 6381
6387 6382 fail_cleanup:
6388 6383 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
6389 6384 "hubd_create_child: fail_cleanup");
6390 6385
6391 6386 mutex_enter(HUBD_MUTEX(hubd));
6392 6387 hubd->h_children_dips[port] = NULL;
6393 6388 mutex_exit(HUBD_MUTEX(hubd));
6394 6389
6395 6390 if (pdata) {
6396 6391 freemsg(pdata);
6397 6392 }
6398 6393
6399 6394 if (ph) {
6400 6395 usb_pipe_close(child_dip, ph,
6401 6396 USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, NULL, NULL);
6402 6397 }
6403 6398
6404 6399 if (child_dip) {
6405 6400 int rval = usba_destroy_child_devi(child_dip,
6406 6401 NDI_DEVI_REMOVE);
6407 6402 if (rval != USB_SUCCESS) {
6408 6403 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
6409 6404 "failure to remove child node");
6410 6405 }
6411 6406 }
6412 6407
6413 6408 if (child_ud) {
6414 6409 /* to make sure we free the address */
6415 6410 mutex_enter(&child_ud->usb_mutex);
6416 6411 child_ud->usb_addr = address;
6417 6412 ASSERT(child_ud->usb_ref_count == 0);
6418 6413 mutex_exit(&child_ud->usb_mutex);
6419 6414
6420 6415 mutex_enter(HUBD_MUTEX(hubd));
6421 6416 if (hubd->h_usba_devices[port] == NULL) {
6422 6417 mutex_exit(HUBD_MUTEX(hubd));
6423 6418 usba_free_usba_device(child_ud);
6424 6419 } else {
6425 6420 hubd_free_usba_device(hubd, hubd->h_usba_devices[port]);
6426 6421 mutex_exit(HUBD_MUTEX(hubd));
6427 6422 }
6428 6423 }
6429 6424
6430 6425 mutex_enter(HUBD_MUTEX(hubd));
6431 6426
6432 6427 return (USB_FAILURE);
6433 6428 }
6434 6429
6435 6430
6436 6431 /*
6437 6432 * hubd_delete_child:
6438 6433 * - free usb address
6439 6434 * - lookup child dips, there may be multiple on this port
6440 6435 * - offline each child devi
6441 6436 */
6442 6437 static int
6443 6438 hubd_delete_child(hubd_t *hubd, usb_port_t port, uint_t flag, boolean_t retry)
6444 6439 {
6445 6440 dev_info_t *child_dip;
6446 6441 usba_device_t *usba_device;
6447 6442 int rval = USB_SUCCESS;
6448 6443
6449 6444 child_dip = hubd->h_children_dips[port];
6450 6445 usba_device = hubd->h_usba_devices[port];
6451 6446
6452 6447 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
6453 6448 "hubd_delete_child: port=%d, dip=0x%p usba_device=0x%p",
6454 6449 port, (void *)child_dip, (void *)usba_device);
6455 6450
6456 6451 mutex_exit(HUBD_MUTEX(hubd));
6457 6452 if (child_dip) {
6458 6453 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
↓ open down ↓ |
5834 lines elided |
↑ open up ↑ |
6459 6454 "hubd_delete_child:\n\t"
6460 6455 "dip = 0x%p (%s) at port %d",
6461 6456 (void *)child_dip, ddi_node_name(child_dip), port);
6462 6457
6463 6458 if (usba_device) {
6464 6459 usba_hubdi_incr_power_budget(hubd->h_dip, usba_device);
6465 6460 }
6466 6461
6467 6462 rval = usba_destroy_child_devi(child_dip, flag);
6468 6463
6469 - if ((rval != USB_SUCCESS) && usba_is_hwa(child_dip)) {
6470 - /*
6471 - * This is only useful for HWA device node.
6472 - * Since hwahc interface must hold hwarc interface
6473 - * open until hwahc is detached, the first call to
6474 - * ndi_devi_unconfig_one() can only offline hwahc
6475 - * driver but not hwarc driver. Need to make a second
6476 - * call to ndi_devi_unconfig_one() to make the hwarc
6477 - * driver detach.
6478 - */
6479 - rval = usba_destroy_child_devi(child_dip, flag);
6480 - }
6481 -
6482 6464 if ((rval == USB_SUCCESS) && (flag & NDI_DEVI_REMOVE)) {
6483 6465 /*
6484 6466 * if the child was still < DS_INITIALIZED
6485 6467 * then our bus_unconfig was not called and
6486 6468 * we have to zap the child here
6487 6469 */
6488 6470 mutex_enter(HUBD_MUTEX(hubd));
6489 6471 if (hubd->h_children_dips[port] == child_dip) {
6490 6472 usba_device_t *ud =
6491 6473 hubd->h_usba_devices[port];
6492 6474 hubd->h_children_dips[port] = NULL;
6493 6475 if (ud) {
6494 6476 mutex_exit(HUBD_MUTEX(hubd));
6495 6477
6496 6478 mutex_enter(&ud->usb_mutex);
6497 6479 ud->usb_ref_count = 0;
6498 6480 mutex_exit(&ud->usb_mutex);
6499 6481
6500 6482 usba_free_usba_device(ud);
6501 6483 mutex_enter(HUBD_MUTEX(hubd));
6502 6484 hubd->h_usba_devices[port] = NULL;
6503 6485 }
6504 6486 }
6505 6487 mutex_exit(HUBD_MUTEX(hubd));
6506 6488 }
6507 6489 }
6508 6490
6509 6491 if ((rval != USB_SUCCESS) && retry) {
6510 6492
6511 6493 hubd_schedule_cleanup(usba_device->usb_root_hub_dip);
6512 6494 }
6513 6495 mutex_enter(HUBD_MUTEX(hubd));
6514 6496
6515 6497 return (rval);
6516 6498 }
6517 6499
6518 6500
6519 6501 /*
6520 6502 * hubd_free_usba_device:
6521 6503 * free usb device structure unless it is associated with
6522 6504 * the root hub which is handled differently
6523 6505 */
6524 6506 static void
6525 6507 hubd_free_usba_device(hubd_t *hubd, usba_device_t *usba_device)
6526 6508 {
6527 6509 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
6528 6510 "hubd_free_usba_device: hubd=0x%p, usba_device=0x%p",
6529 6511 (void *)hubd, (void *)usba_device);
6530 6512
6531 6513 if (usba_device && (usba_device->usb_addr != ROOT_HUB_ADDR)) {
6532 6514 usb_port_t port = usba_device->usb_port;
6533 6515 dev_info_t *dip = hubd->h_children_dips[port];
6534 6516
6535 6517 #ifdef DEBUG
6536 6518 if (dip) {
6537 6519 ASSERT(i_ddi_node_state(dip) < DS_INITIALIZED);
6538 6520 }
6539 6521 #endif
6540 6522
6541 6523 port = usba_device->usb_port;
6542 6524 hubd->h_usba_devices[port] = NULL;
6543 6525
6544 6526 mutex_exit(HUBD_MUTEX(hubd));
6545 6527 usba_free_usba_device(usba_device);
6546 6528 mutex_enter(HUBD_MUTEX(hubd));
6547 6529 }
6548 6530 }
6549 6531
6550 6532
6551 6533 /*
6552 6534 * event support
6553 6535 *
6554 6536 * busctl event support
6555 6537 */
6556 6538 static int
6557 6539 hubd_busop_get_eventcookie(dev_info_t *dip,
6558 6540 dev_info_t *rdip,
6559 6541 char *eventname,
6560 6542 ddi_eventcookie_t *cookie)
6561 6543 {
6562 6544 hubd_t *hubd = (hubd_t *)hubd_get_soft_state(dip);
6563 6545
6564 6546 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
6565 6547 "hubd_busop_get_eventcookie: dip=0x%p, rdip=0x%p, "
6566 6548 "event=%s", (void *)dip, (void *)rdip, eventname);
6567 6549 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
6568 6550 "(dip=%s%d, rdip=%s%d)",
6569 6551 ddi_driver_name(dip), ddi_get_instance(dip),
6570 6552 ddi_driver_name(rdip), ddi_get_instance(rdip));
6571 6553
6572 6554 /* return event cookie, iblock cookie, and level */
6573 6555 return (ndi_event_retrieve_cookie(hubd->h_ndi_event_hdl,
6574 6556 rdip, eventname, cookie, NDI_EVENT_NOPASS));
6575 6557 }
6576 6558
6577 6559
6578 6560 static int
6579 6561 hubd_busop_add_eventcall(dev_info_t *dip,
6580 6562 dev_info_t *rdip,
6581 6563 ddi_eventcookie_t cookie,
6582 6564 void (*callback)(dev_info_t *dip,
6583 6565 ddi_eventcookie_t cookie, void *arg,
6584 6566 void *bus_impldata),
6585 6567 void *arg, ddi_callback_id_t *cb_id)
6586 6568 {
6587 6569 hubd_t *hubd = (hubd_t *)hubd_get_soft_state(dip);
6588 6570 usb_port_t port = hubd_child_dip2port(hubd, rdip);
6589 6571
6590 6572 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
6591 6573 "hubd_busop_add_eventcall: dip=0x%p, rdip=0x%p "
6592 6574 "cookie=0x%p, cb=0x%p, arg=0x%p",
6593 6575 (void *)dip, (void *)rdip, (void *)cookie, (void *)callback, arg);
6594 6576 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
6595 6577 "(dip=%s%d, rdip=%s%d, event=%s)",
6596 6578 ddi_driver_name(dip), ddi_get_instance(dip),
6597 6579 ddi_driver_name(rdip), ddi_get_instance(rdip),
6598 6580 ndi_event_cookie_to_name(hubd->h_ndi_event_hdl, cookie));
6599 6581
6600 6582 /* Set flag on children registering events */
6601 6583 switch (ndi_event_cookie_to_tag(hubd->h_ndi_event_hdl, cookie)) {
6602 6584 case USBA_EVENT_TAG_HOT_REMOVAL:
6603 6585 mutex_enter(HUBD_MUTEX(hubd));
6604 6586 hubd->h_child_events[port] |= HUBD_CHILD_EVENT_DISCONNECT;
6605 6587 mutex_exit(HUBD_MUTEX(hubd));
6606 6588
6607 6589 break;
6608 6590 case USBA_EVENT_TAG_PRE_SUSPEND:
6609 6591 mutex_enter(HUBD_MUTEX(hubd));
6610 6592 hubd->h_child_events[port] |= HUBD_CHILD_EVENT_PRESUSPEND;
6611 6593 mutex_exit(HUBD_MUTEX(hubd));
6612 6594
6613 6595 break;
6614 6596 default:
6615 6597
6616 6598 break;
6617 6599 }
6618 6600
6619 6601 /* add callback to our event set */
6620 6602 return (ndi_event_add_callback(hubd->h_ndi_event_hdl,
6621 6603 rdip, cookie, callback, arg, NDI_SLEEP, cb_id));
6622 6604 }
6623 6605
6624 6606
6625 6607 static int
6626 6608 hubd_busop_remove_eventcall(dev_info_t *dip, ddi_callback_id_t cb_id)
6627 6609 {
6628 6610 hubd_t *hubd = (hubd_t *)hubd_get_soft_state(dip);
6629 6611 ndi_event_callbacks_t *id = (ndi_event_callbacks_t *)cb_id;
6630 6612
6631 6613 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
6632 6614 "hubd_busop_remove_eventcall: dip=0x%p, rdip=0x%p "
6633 6615 "cookie=0x%p", (void *)dip, (void *)id->ndi_evtcb_dip,
6634 6616 (void *)id->ndi_evtcb_cookie);
6635 6617 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
6636 6618 "(dip=%s%d, rdip=%s%d, event=%s)",
6637 6619 ddi_driver_name(dip), ddi_get_instance(dip),
6638 6620 ddi_driver_name(id->ndi_evtcb_dip),
6639 6621 ddi_get_instance(id->ndi_evtcb_dip),
6640 6622 ndi_event_cookie_to_name(hubd->h_ndi_event_hdl,
6641 6623 id->ndi_evtcb_cookie));
6642 6624
6643 6625 /* remove event registration from our event set */
6644 6626 return (ndi_event_remove_callback(hubd->h_ndi_event_hdl, cb_id));
6645 6627 }
6646 6628
6647 6629
6648 6630 /*
6649 6631 * event distribution
6650 6632 *
6651 6633 * hubd_do_callback:
6652 6634 * Post this event to the specified child
6653 6635 */
6654 6636 static void
6655 6637 hubd_do_callback(hubd_t *hubd, dev_info_t *cdip, ddi_eventcookie_t cookie)
6656 6638 {
6657 6639 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
6658 6640 "hubd_do_callback");
6659 6641
6660 6642 (void) ndi_event_do_callback(hubd->h_ndi_event_hdl, cdip, cookie, NULL);
6661 6643 }
6662 6644
6663 6645
6664 6646 /*
6665 6647 * hubd_run_callbacks:
6666 6648 * Send this event to all children
6667 6649 */
6668 6650 static void
6669 6651 hubd_run_callbacks(hubd_t *hubd, usba_event_t type)
6670 6652 {
6671 6653 usb_port_t port;
6672 6654
6673 6655 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
6674 6656 "hubd_run_callbacks");
6675 6657
6676 6658 mutex_enter(HUBD_MUTEX(hubd));
6677 6659 for (port = 1; port <= hubd->h_hub_descr.bNbrPorts; port++) {
6678 6660 /*
6679 6661 * the childen_dips list may have dips that have been
6680 6662 * already deallocated. we only get a post_detach notification
6681 6663 * but not a destroy notification
6682 6664 */
6683 6665 if (hubd->h_children_dips[port]) {
6684 6666 mutex_exit(HUBD_MUTEX(hubd));
6685 6667 hubd_post_event(hubd, port, type);
6686 6668 mutex_enter(HUBD_MUTEX(hubd));
6687 6669 }
6688 6670 }
6689 6671 mutex_exit(HUBD_MUTEX(hubd));
6690 6672 }
6691 6673
6692 6674
6693 6675 /*
6694 6676 * hubd_post_event
6695 6677 * post event to a child on the port depending on the type
6696 6678 */
6697 6679 static void
6698 6680 hubd_post_event(hubd_t *hubd, usb_port_t port, usba_event_t type)
6699 6681 {
6700 6682 int rval;
6701 6683 dev_info_t *dip;
6702 6684 usba_device_t *usba_device;
6703 6685 ddi_eventcookie_t cookie, rm_cookie, suspend_cookie;
6704 6686
6705 6687 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
6706 6688 "hubd_post_event: port=%d event=%s", port,
6707 6689 ndi_event_tag_to_name(hubd->h_ndi_event_hdl, type));
6708 6690
6709 6691 cookie = ndi_event_tag_to_cookie(hubd->h_ndi_event_hdl, type);
6710 6692 rm_cookie = ndi_event_tag_to_cookie(hubd->h_ndi_event_hdl,
6711 6693 USBA_EVENT_TAG_HOT_REMOVAL);
6712 6694 suspend_cookie = ndi_event_tag_to_cookie(hubd->h_ndi_event_hdl,
6713 6695 USBA_EVENT_TAG_PRE_SUSPEND);
6714 6696
6715 6697 /*
6716 6698 * Hotplug daemon may be attaching a driver that may be registering
6717 6699 * event callbacks. So it already has got the device tree lock and
6718 6700 * event handle mutex. So to prevent a deadlock while posting events,
6719 6701 * we grab and release the locks in the same order.
6720 6702 */
6721 6703 mutex_enter(HUBD_MUTEX(hubd));
6722 6704 dip = hubd->h_children_dips[port];
6723 6705 usba_device = hubd->h_usba_devices[port];
6724 6706 mutex_exit(HUBD_MUTEX(hubd));
6725 6707
6726 6708 switch (type) {
6727 6709 case USBA_EVENT_TAG_HOT_REMOVAL:
6728 6710 /* Clear the registered event flag */
6729 6711 mutex_enter(HUBD_MUTEX(hubd));
6730 6712 hubd->h_child_events[port] &= ~HUBD_CHILD_EVENT_DISCONNECT;
6731 6713 mutex_exit(HUBD_MUTEX(hubd));
6732 6714
6733 6715 hubd_do_callback(hubd, dip, cookie);
6734 6716 usba_persistent_pipe_close(usba_device);
6735 6717
6736 6718 /*
6737 6719 * Mark the dip for deletion only after the driver has
6738 6720 * seen the disconnect event to prevent cleanup thread
6739 6721 * from stepping in between.
6740 6722 */
6741 6723 mutex_enter(&(DEVI(dip)->devi_lock));
6742 6724 DEVI_SET_DEVICE_REMOVED(dip);
6743 6725 mutex_exit(&(DEVI(dip)->devi_lock));
6744 6726
6745 6727 break;
6746 6728 case USBA_EVENT_TAG_PRE_SUSPEND:
6747 6729 mutex_enter(HUBD_MUTEX(hubd));
6748 6730 hubd->h_child_events[port] &= ~HUBD_CHILD_EVENT_PRESUSPEND;
6749 6731 mutex_exit(HUBD_MUTEX(hubd));
6750 6732
6751 6733 hubd_do_callback(hubd, dip, cookie);
6752 6734 /*
6753 6735 * persistent pipe close for this event is taken care by the
6754 6736 * caller after verfying that all children can suspend
6755 6737 */
6756 6738
6757 6739 break;
6758 6740 case USBA_EVENT_TAG_HOT_INSERTION:
6759 6741 /*
6760 6742 * Check if this child has missed the disconnect event before
6761 6743 * it registered for event callbacks
6762 6744 */
6763 6745 mutex_enter(HUBD_MUTEX(hubd));
6764 6746 if (hubd->h_child_events[port] & HUBD_CHILD_EVENT_DISCONNECT) {
6765 6747 /* clear the flag and post disconnect event */
6766 6748 hubd->h_child_events[port] &=
6767 6749 ~HUBD_CHILD_EVENT_DISCONNECT;
6768 6750 mutex_exit(HUBD_MUTEX(hubd));
6769 6751 hubd_do_callback(hubd, dip, rm_cookie);
6770 6752 usba_persistent_pipe_close(usba_device);
6771 6753 mutex_enter(HUBD_MUTEX(hubd));
6772 6754 }
6773 6755 mutex_exit(HUBD_MUTEX(hubd));
6774 6756
6775 6757 /*
6776 6758 * Mark the dip as reinserted to prevent cleanup thread
6777 6759 * from stepping in.
6778 6760 */
6779 6761 mutex_enter(&(DEVI(dip)->devi_lock));
6780 6762 DEVI_SET_DEVICE_REINSERTED(dip);
6781 6763 mutex_exit(&(DEVI(dip)->devi_lock));
6782 6764
6783 6765 rval = usba_persistent_pipe_open(usba_device);
6784 6766 if (rval != USB_SUCCESS) {
6785 6767 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG,
6786 6768 hubd->h_log_handle,
6787 6769 "failed to reopen all pipes on reconnect");
6788 6770 }
6789 6771
6790 6772 hubd_do_callback(hubd, dip, cookie);
6791 6773
6792 6774 /*
6793 6775 * We might see a connect event only if hotplug thread for
6794 6776 * disconnect event don't run in time.
6795 6777 * Set the flag again, so we don't miss posting a
6796 6778 * disconnect event.
6797 6779 */
6798 6780 mutex_enter(HUBD_MUTEX(hubd));
6799 6781 hubd->h_child_events[port] |= HUBD_CHILD_EVENT_DISCONNECT;
6800 6782 mutex_exit(HUBD_MUTEX(hubd));
6801 6783
6802 6784 break;
6803 6785 case USBA_EVENT_TAG_POST_RESUME:
6804 6786 /*
6805 6787 * Check if this child has missed the pre-suspend event before
6806 6788 * it registered for event callbacks
6807 6789 */
6808 6790 mutex_enter(HUBD_MUTEX(hubd));
6809 6791 if (hubd->h_child_events[port] & HUBD_CHILD_EVENT_PRESUSPEND) {
6810 6792 /* clear the flag and post pre_suspend event */
6811 6793 hubd->h_port_state[port] &=
6812 6794 ~HUBD_CHILD_EVENT_PRESUSPEND;
6813 6795 mutex_exit(HUBD_MUTEX(hubd));
6814 6796 hubd_do_callback(hubd, dip, suspend_cookie);
6815 6797 mutex_enter(HUBD_MUTEX(hubd));
6816 6798 }
6817 6799 mutex_exit(HUBD_MUTEX(hubd));
6818 6800
6819 6801 mutex_enter(&usba_device->usb_mutex);
6820 6802 usba_device->usb_no_cpr = 0;
6821 6803 mutex_exit(&usba_device->usb_mutex);
6822 6804
6823 6805 /*
6824 6806 * Since the pipe has already been opened by hub
6825 6807 * at DDI_RESUME time, there is no need for a
6826 6808 * persistent pipe open
6827 6809 */
6828 6810 hubd_do_callback(hubd, dip, cookie);
6829 6811
6830 6812 /*
6831 6813 * Set the flag again, so we don't miss posting a
6832 6814 * pre-suspend event. This enforces a tighter
6833 6815 * dev_state model.
6834 6816 */
6835 6817 mutex_enter(HUBD_MUTEX(hubd));
6836 6818 hubd->h_child_events[port] |= HUBD_CHILD_EVENT_PRESUSPEND;
6837 6819 mutex_exit(HUBD_MUTEX(hubd));
6838 6820 break;
6839 6821 }
6840 6822 }
6841 6823
6842 6824
6843 6825 /*
6844 6826 * handling of events coming from above
6845 6827 */
6846 6828 static int
6847 6829 hubd_disconnect_event_cb(dev_info_t *dip)
6848 6830 {
6849 6831 hubd_t *hubd = (hubd_t *)hubd_get_soft_state(dip);
6850 6832 usb_port_t port, nports;
6851 6833 usba_device_t *usba_dev;
6852 6834 usba_event_t tag = USBA_EVENT_TAG_HOT_REMOVAL;
6853 6835 int circ;
6854 6836
6855 6837 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
6856 6838 "hubd_disconnect_event_cb: tag=%d", tag);
6857 6839
6858 6840 ndi_devi_enter(dip, &circ);
6859 6841
6860 6842 mutex_enter(HUBD_MUTEX(hubd));
6861 6843 switch (hubd->h_dev_state) {
6862 6844 case USB_DEV_ONLINE:
6863 6845 case USB_DEV_PWRED_DOWN:
6864 6846 hubd->h_dev_state = USB_DEV_DISCONNECTED;
6865 6847 /* stop polling on the interrupt pipe */
6866 6848 hubd_stop_polling(hubd);
6867 6849
6868 6850 /* FALLTHROUGH */
6869 6851 case USB_DEV_SUSPENDED:
6870 6852 /* we remain in this state */
6871 6853 mutex_exit(HUBD_MUTEX(hubd));
6872 6854 hubd_run_callbacks(hubd, tag);
6873 6855 mutex_enter(HUBD_MUTEX(hubd));
6874 6856
6875 6857 /* close all the open pipes of our children */
6876 6858 nports = hubd->h_hub_descr.bNbrPorts;
6877 6859 for (port = 1; port <= nports; port++) {
6878 6860 usba_dev = hubd->h_usba_devices[port];
6879 6861 if (usba_dev != NULL) {
6880 6862 mutex_exit(HUBD_MUTEX(hubd));
6881 6863 usba_persistent_pipe_close(usba_dev);
6882 6864 mutex_enter(HUBD_MUTEX(hubd));
6883 6865 }
6884 6866 }
6885 6867
6886 6868 break;
6887 6869 case USB_DEV_DISCONNECTED:
6888 6870 /* avoid passing multiple disconnects to children */
6889 6871 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
6890 6872 "hubd_disconnect_event_cb: Already disconnected");
6891 6873
6892 6874 break;
6893 6875 default:
6894 6876 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
6895 6877 "hubd_disconnect_event_cb: Illegal devstate=%d",
6896 6878 hubd->h_dev_state);
6897 6879
6898 6880 break;
6899 6881 }
6900 6882 mutex_exit(HUBD_MUTEX(hubd));
6901 6883
6902 6884 ndi_devi_exit(dip, circ);
6903 6885
6904 6886 return (USB_SUCCESS);
6905 6887 }
6906 6888
6907 6889
6908 6890 static int
6909 6891 hubd_reconnect_event_cb(dev_info_t *dip)
6910 6892 {
6911 6893 int rval, circ;
6912 6894
6913 6895 ndi_devi_enter(dip, &circ);
6914 6896 rval = hubd_restore_state_cb(dip);
6915 6897 ndi_devi_exit(dip, circ);
6916 6898
6917 6899 return (rval);
6918 6900 }
6919 6901
6920 6902
6921 6903 /*
6922 6904 * hubd_pre_suspend_event_cb
6923 6905 * propogate event for binary compatibility of old drivers
6924 6906 */
6925 6907 static int
6926 6908 hubd_pre_suspend_event_cb(dev_info_t *dip)
6927 6909 {
6928 6910 int circ;
6929 6911 hubd_t *hubd = (hubd_t *)hubd_get_soft_state(dip);
6930 6912
6931 6913 USB_DPRINTF_L4(DPRINT_MASK_EVENTS, hubd->h_log_handle,
6932 6914 "hubd_pre_suspend_event_cb");
6933 6915
6934 6916 /* disable hotplug thread */
6935 6917 mutex_enter(HUBD_MUTEX(hubd));
6936 6918 hubd->h_hotplug_thread++;
6937 6919 hubd_stop_polling(hubd);
6938 6920
6939 6921 /* keep PM out till we see a cpr resume */
6940 6922 (void) hubd_pm_busy_component(hubd, hubd->h_dip, 0);
6941 6923 mutex_exit(HUBD_MUTEX(hubd));
6942 6924
6943 6925 ndi_devi_enter(dip, &circ);
6944 6926 hubd_run_callbacks(hubd, USBA_EVENT_TAG_PRE_SUSPEND);
6945 6927 ndi_devi_exit(dip, circ);
6946 6928
6947 6929 return (USB_SUCCESS);
6948 6930 }
6949 6931
6950 6932
6951 6933 /*
6952 6934 * hubd_post_resume_event_cb
6953 6935 * propogate event for binary compatibility of old drivers
6954 6936 */
6955 6937 static int
6956 6938 hubd_post_resume_event_cb(dev_info_t *dip)
6957 6939 {
6958 6940 int circ;
6959 6941 hubd_t *hubd = (hubd_t *)hubd_get_soft_state(dip);
6960 6942
6961 6943 USB_DPRINTF_L4(DPRINT_MASK_EVENTS, hubd->h_log_handle,
6962 6944 "hubd_post_resume_event_cb");
6963 6945
6964 6946 ndi_devi_enter(dip, &circ);
6965 6947 hubd_run_callbacks(hubd, USBA_EVENT_TAG_POST_RESUME);
6966 6948 ndi_devi_exit(dip, circ);
6967 6949
6968 6950 mutex_enter(HUBD_MUTEX(hubd));
6969 6951
6970 6952 /* enable PM */
6971 6953 (void) hubd_pm_idle_component(hubd, hubd->h_dip, 0);
6972 6954
6973 6955 /* allow hotplug thread */
6974 6956 hubd->h_hotplug_thread--;
6975 6957
6976 6958 /* start polling */
6977 6959 hubd_start_polling(hubd, 0);
6978 6960 mutex_exit(HUBD_MUTEX(hubd));
6979 6961
6980 6962 return (USB_SUCCESS);
6981 6963 }
6982 6964
6983 6965
6984 6966 /*
6985 6967 * hubd_cpr_suspend
6986 6968 * save the current state of the driver/device
6987 6969 */
6988 6970 static int
6989 6971 hubd_cpr_suspend(hubd_t *hubd)
6990 6972 {
6991 6973 usb_port_t port, nports;
6992 6974 usba_device_t *usba_dev;
6993 6975 uchar_t no_cpr = 0;
6994 6976 int rval = USB_FAILURE;
6995 6977
6996 6978 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
6997 6979 "hubd_cpr_suspend: Begin");
6998 6980
6999 6981 /* Make sure device is powered up to save state. */
7000 6982 mutex_enter(HUBD_MUTEX(hubd));
7001 6983 hubd_pm_busy_component(hubd, hubd->h_dip, 0);
7002 6984 mutex_exit(HUBD_MUTEX(hubd));
7003 6985
7004 6986 /* bring the device to full power */
7005 6987 (void) pm_raise_power(hubd->h_dip, 0, USB_DEV_OS_FULL_PWR);
7006 6988 mutex_enter(HUBD_MUTEX(hubd));
7007 6989
7008 6990 switch (hubd->h_dev_state) {
7009 6991 case USB_DEV_ONLINE:
7010 6992 case USB_DEV_PWRED_DOWN:
7011 6993 case USB_DEV_DISCONNECTED:
7012 6994 /* find out if all our children have been quiesced */
7013 6995 nports = hubd->h_hub_descr.bNbrPorts;
7014 6996 for (port = 1; (no_cpr == 0) && (port <= nports); port++) {
7015 6997 usba_dev = hubd->h_usba_devices[port];
7016 6998 if (usba_dev != NULL) {
7017 6999 mutex_enter(&usba_dev->usb_mutex);
7018 7000 no_cpr += usba_dev->usb_no_cpr;
7019 7001 mutex_exit(&usba_dev->usb_mutex);
7020 7002 }
7021 7003 }
7022 7004 if (no_cpr > 0) {
7023 7005 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
7024 7006 "Children busy - can't checkpoint");
7025 7007 /* remain in same state to fail checkpoint */
7026 7008
7027 7009 break;
7028 7010 } else {
7029 7011 /*
7030 7012 * do not suspend if our hotplug thread
7031 7013 * or the deathrow thread is active
7032 7014 */
7033 7015 if ((hubd->h_hotplug_thread > 1) ||
7034 7016 (hubd->h_cleanup_active == B_TRUE)) {
7035 7017 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG,
7036 7018 hubd->h_log_handle,
7037 7019 "hotplug thread active - can't cpr");
7038 7020 /* remain in same state to fail checkpoint */
7039 7021
7040 7022 break;
7041 7023 }
7042 7024
7043 7025 /* quiesce ourselves now */
7044 7026 hubd_stop_polling(hubd);
7045 7027
7046 7028 /* close all the open pipes of our children */
7047 7029 for (port = 1; port <= nports; port++) {
7048 7030 usba_dev = hubd->h_usba_devices[port];
7049 7031 if (usba_dev != NULL) {
7050 7032 mutex_exit(HUBD_MUTEX(hubd));
7051 7033 usba_persistent_pipe_close(usba_dev);
7052 7034 if (hubd_suspend_port(hubd, port)) {
7053 7035 USB_DPRINTF_L0(
7054 7036 DPRINT_MASK_HOTPLUG,
7055 7037 hubd->h_log_handle,
7056 7038 "suspending port %d failed",
7057 7039 port);
7058 7040 }
7059 7041 mutex_enter(HUBD_MUTEX(hubd));
7060 7042 }
7061 7043
7062 7044 }
7063 7045 hubd->h_dev_state = USB_DEV_SUSPENDED;
7064 7046
7065 7047 /*
7066 7048 * if we are the root hub, we close our pipes
7067 7049 * ourselves.
7068 7050 */
7069 7051 if (usba_is_root_hub(hubd->h_dip)) {
7070 7052 mutex_exit(HUBD_MUTEX(hubd));
7071 7053 usba_persistent_pipe_close(
7072 7054 usba_get_usba_device(hubd->h_dip));
7073 7055 mutex_enter(HUBD_MUTEX(hubd));
7074 7056 }
7075 7057 rval = USB_SUCCESS;
7076 7058
7077 7059 break;
7078 7060 }
7079 7061 case USB_DEV_SUSPENDED:
7080 7062 default:
7081 7063 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
7082 7064 "hubd_cpr_suspend: Illegal dev state=%d",
7083 7065 hubd->h_dev_state);
7084 7066
7085 7067 break;
7086 7068 }
7087 7069
7088 7070 hubd_pm_idle_component(hubd, hubd->h_dip, 0);
7089 7071 mutex_exit(HUBD_MUTEX(hubd));
7090 7072
7091 7073 return (rval);
7092 7074 }
7093 7075
7094 7076 static void
7095 7077 hubd_cpr_resume(dev_info_t *dip)
7096 7078 {
7097 7079 int rval, circ;
7098 7080
7099 7081 ndi_devi_enter(dip, &circ);
7100 7082 /*
7101 7083 * if we are the root hub, we open our pipes
7102 7084 * ourselves.
7103 7085 */
7104 7086 if (usba_is_root_hub(dip)) {
7105 7087 rval = usba_persistent_pipe_open(
7106 7088 usba_get_usba_device(dip));
7107 7089 ASSERT(rval == USB_SUCCESS);
7108 7090 }
7109 7091 (void) hubd_restore_state_cb(dip);
7110 7092 ndi_devi_exit(dip, circ);
7111 7093 }
7112 7094
7113 7095
7114 7096 /*
7115 7097 * hubd_restore_state_cb
7116 7098 * Event callback to restore device state
7117 7099 */
7118 7100 static int
7119 7101 hubd_restore_state_cb(dev_info_t *dip)
7120 7102 {
7121 7103 hubd_t *hubd = (hubd_t *)hubd_get_soft_state(dip);
7122 7104
7123 7105 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
7124 7106 "hubd_restore_state_cb: Begin");
7125 7107
7126 7108 /* restore the state of this device */
7127 7109 hubd_restore_device_state(dip, hubd);
7128 7110
7129 7111 return (USB_SUCCESS);
7130 7112 }
7131 7113
7132 7114
7133 7115 /*
7134 7116 * registering for events
7135 7117 */
7136 7118 static int
7137 7119 hubd_register_events(hubd_t *hubd)
7138 7120 {
7139 7121 int rval = USB_SUCCESS;
7140 7122
7141 7123 if (usba_is_root_hub(hubd->h_dip)) {
7142 7124 hubd_register_cpr_callback(hubd);
7143 7125 } else {
7144 7126 rval = usb_register_event_cbs(hubd->h_dip, &hubd_events, 0);
7145 7127 }
7146 7128
7147 7129 return (rval);
7148 7130 }
7149 7131
7150 7132
7151 7133 /*
7152 7134 * hubd cpr callback related functions
7153 7135 *
7154 7136 * hubd_cpr_post_user_callb:
7155 7137 * This function is called during checkpoint & resume -
7156 7138 * 1. after user threads are stopped during checkpoint
7157 7139 * 2. after kernel threads are resumed during resume
7158 7140 */
7159 7141 /* ARGSUSED */
7160 7142 static boolean_t
7161 7143 hubd_cpr_post_user_callb(void *arg, int code)
7162 7144 {
7163 7145 hubd_cpr_t *cpr_cb = (hubd_cpr_t *)arg;
7164 7146 hubd_t *hubd = cpr_cb->statep;
7165 7147 int retry = 0;
7166 7148
7167 7149 USB_DPRINTF_L4(DPRINT_MASK_EVENTS, hubd->h_log_handle,
7168 7150 "hubd_cpr_post_user_callb");
7169 7151
7170 7152 switch (code) {
7171 7153 case CB_CODE_CPR_CHKPT:
7172 7154 USB_DPRINTF_L3(DPRINT_MASK_EVENTS, hubd->h_log_handle,
7173 7155 "hubd_cpr_post_user_callb: CB_CODE_CPR_CHKPT");
7174 7156
7175 7157 mutex_enter(HUBD_MUTEX(hubd));
7176 7158
7177 7159 /* turn off deathrow thread */
7178 7160 hubd->h_cleanup_enabled = B_FALSE;
7179 7161
7180 7162 /* give up if deathrow thread doesn't exit */
7181 7163 while ((hubd->h_cleanup_active == B_TRUE) && (retry++ < 3)) {
7182 7164 mutex_exit(HUBD_MUTEX(hubd));
7183 7165 delay(drv_usectohz(hubd_dip_cleanup_delay));
7184 7166
7185 7167 USB_DPRINTF_L2(DPRINT_MASK_EVENTS, hubd->h_log_handle,
7186 7168 "hubd_cpr_post_user_callb, waiting for "
7187 7169 "deathrow thread to exit");
7188 7170 mutex_enter(HUBD_MUTEX(hubd));
7189 7171 }
7190 7172
7191 7173 mutex_exit(HUBD_MUTEX(hubd));
7192 7174
7193 7175 /* save the state of the device */
7194 7176 (void) hubd_pre_suspend_event_cb(hubd->h_dip);
7195 7177
7196 7178 return (B_TRUE);
7197 7179 case CB_CODE_CPR_RESUME:
7198 7180 USB_DPRINTF_L3(DPRINT_MASK_EVENTS, hubd->h_log_handle,
7199 7181 "hubd_cpr_post_user_callb: CB_CODE_CPR_RESUME");
7200 7182
7201 7183 /* restore the state of the device */
7202 7184 (void) hubd_post_resume_event_cb(hubd->h_dip);
7203 7185
7204 7186 /* turn on deathrow thread */
7205 7187 mutex_enter(HUBD_MUTEX(hubd));
7206 7188 hubd->h_cleanup_enabled = B_TRUE;
7207 7189 mutex_exit(HUBD_MUTEX(hubd));
7208 7190
7209 7191 hubd_schedule_cleanup(hubd->h_usba_device->usb_root_hub_dip);
7210 7192
7211 7193 return (B_TRUE);
7212 7194 default:
7213 7195
7214 7196 return (B_FALSE);
7215 7197 }
7216 7198
7217 7199 }
7218 7200
7219 7201
7220 7202 /* register callback with cpr framework */
7221 7203 void
7222 7204 hubd_register_cpr_callback(hubd_t *hubd)
7223 7205 {
7224 7206 USB_DPRINTF_L4(DPRINT_MASK_EVENTS, hubd->h_log_handle,
7225 7207 "hubd_register_cpr_callback");
7226 7208
7227 7209 mutex_enter(HUBD_MUTEX(hubd));
7228 7210 hubd->h_cpr_cb =
7229 7211 (hubd_cpr_t *)kmem_zalloc(sizeof (hubd_cpr_t), KM_SLEEP);
7230 7212 mutex_exit(HUBD_MUTEX(hubd));
7231 7213 mutex_init(&hubd->h_cpr_cb->lockp, NULL, MUTEX_DRIVER,
7232 7214 hubd->h_dev_data->dev_iblock_cookie);
7233 7215 hubd->h_cpr_cb->statep = hubd;
7234 7216 hubd->h_cpr_cb->cpr.cc_lockp = &hubd->h_cpr_cb->lockp;
7235 7217 hubd->h_cpr_cb->cpr.cc_id = callb_add(hubd_cpr_post_user_callb,
7236 7218 (void *)hubd->h_cpr_cb, CB_CL_CPR_POST_USER, "hubd");
7237 7219 }
7238 7220
7239 7221
7240 7222 /* unregister callback with cpr framework */
7241 7223 void
7242 7224 hubd_unregister_cpr_callback(hubd_t *hubd)
7243 7225 {
7244 7226 USB_DPRINTF_L4(DPRINT_MASK_EVENTS, hubd->h_log_handle,
7245 7227 "hubd_unregister_cpr_callback");
7246 7228
7247 7229 if (hubd->h_cpr_cb) {
7248 7230 (void) callb_delete(hubd->h_cpr_cb->cpr.cc_id);
7249 7231 mutex_destroy(&hubd->h_cpr_cb->lockp);
7250 7232 mutex_enter(HUBD_MUTEX(hubd));
7251 7233 kmem_free(hubd->h_cpr_cb, sizeof (hubd_cpr_t));
7252 7234 mutex_exit(HUBD_MUTEX(hubd));
7253 7235 }
7254 7236 }
7255 7237
7256 7238
7257 7239 /*
7258 7240 * Power management
7259 7241 *
7260 7242 * create the pm components required for power management
7261 7243 */
7262 7244 static void
7263 7245 hubd_create_pm_components(dev_info_t *dip, hubd_t *hubd)
7264 7246 {
7265 7247 hub_power_t *hubpm;
7266 7248
7267 7249 USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
7268 7250 "hubd_create_pm_components: Begin");
7269 7251
7270 7252 /* Allocate the state structure */
7271 7253 hubpm = kmem_zalloc(sizeof (hub_power_t), KM_SLEEP);
7272 7254
7273 7255 hubd->h_hubpm = hubpm;
7274 7256 hubpm->hubp_hubd = hubd;
7275 7257 hubpm->hubp_pm_capabilities = 0;
7276 7258 hubpm->hubp_current_power = USB_DEV_OS_FULL_PWR;
7277 7259 hubpm->hubp_time_at_full_power = gethrtime();
7278 7260 hubpm->hubp_min_pm_threshold = hubdi_min_pm_threshold * NANOSEC;
7279 7261
7280 7262 /* alloc memory to save power states of children */
7281 7263 hubpm->hubp_child_pwrstate = (uint8_t *)
7282 7264 kmem_zalloc(MAX_PORTS + 1, KM_SLEEP);
7283 7265
7284 7266 /*
7285 7267 * if the enable remote wakeup fails
7286 7268 * we still want to enable
7287 7269 * parent notification so we can PM the children
7288 7270 */
7289 7271 usb_enable_parent_notification(dip);
7290 7272
7291 7273 if (usb_handle_remote_wakeup(dip,
7292 7274 USB_REMOTE_WAKEUP_ENABLE) == USB_SUCCESS) {
7293 7275 uint_t pwr_states;
7294 7276
7295 7277 USB_DPRINTF_L2(DPRINT_MASK_PM, hubd->h_log_handle,
7296 7278 "hubd_create_pm_components: "
7297 7279 "Remote Wakeup Enabled");
7298 7280
7299 7281 if (usb_create_pm_components(dip, &pwr_states) ==
7300 7282 USB_SUCCESS) {
7301 7283 mutex_enter(HUBD_MUTEX(hubd));
7302 7284 hubpm->hubp_wakeup_enabled = 1;
7303 7285 hubpm->hubp_pwr_states = (uint8_t)pwr_states;
7304 7286
7305 7287 /* we are busy now till end of the attach */
7306 7288 hubd_pm_busy_component(hubd, dip, 0);
7307 7289 mutex_exit(HUBD_MUTEX(hubd));
7308 7290
7309 7291 /* bring the device to full power */
7310 7292 (void) pm_raise_power(dip, 0,
7311 7293 USB_DEV_OS_FULL_PWR);
7312 7294 }
7313 7295 }
7314 7296
7315 7297 USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
7316 7298 "hubd_create_pm_components: END");
7317 7299 }
7318 7300
7319 7301
7320 7302 /*
7321 7303 * Attachment point management
7322 7304 */
7323 7305 /* ARGSUSED */
7324 7306 int
7325 7307 usba_hubdi_open(dev_info_t *dip, dev_t *devp, int flags, int otyp,
7326 7308 cred_t *credp)
7327 7309 {
7328 7310 hubd_t *hubd;
7329 7311
7330 7312 if (otyp != OTYP_CHR)
7331 7313 return (EINVAL);
7332 7314
7333 7315 hubd = hubd_get_soft_state(dip);
7334 7316 if (hubd == NULL) {
7335 7317 return (ENXIO);
7336 7318 }
7337 7319
7338 7320 USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle,
7339 7321 "hubd_open:");
7340 7322
7341 7323 mutex_enter(HUBD_MUTEX(hubd));
7342 7324 if ((flags & FEXCL) && (hubd->h_softstate & HUBD_SS_ISOPEN)) {
7343 7325 mutex_exit(HUBD_MUTEX(hubd));
7344 7326
7345 7327 return (EBUSY);
7346 7328 }
7347 7329
7348 7330 hubd->h_softstate |= HUBD_SS_ISOPEN;
7349 7331 mutex_exit(HUBD_MUTEX(hubd));
7350 7332
7351 7333 USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle, "opened");
7352 7334
7353 7335 return (0);
7354 7336 }
7355 7337
7356 7338
7357 7339 /* ARGSUSED */
7358 7340 int
7359 7341 usba_hubdi_close(dev_info_t *dip, dev_t dev, int flag, int otyp,
7360 7342 cred_t *credp)
7361 7343 {
7362 7344 hubd_t *hubd;
7363 7345
7364 7346 if (otyp != OTYP_CHR) {
7365 7347 return (EINVAL);
7366 7348 }
7367 7349
7368 7350 hubd = hubd_get_soft_state(dip);
7369 7351
7370 7352 if (hubd == NULL) {
7371 7353 return (ENXIO);
7372 7354 }
7373 7355
7374 7356 USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle, "hubd_close:");
7375 7357
7376 7358 mutex_enter(HUBD_MUTEX(hubd));
7377 7359 hubd->h_softstate &= ~HUBD_SS_ISOPEN;
7378 7360 mutex_exit(HUBD_MUTEX(hubd));
7379 7361
7380 7362 USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle, "closed");
7381 7363
7382 7364 return (0);
7383 7365 }
7384 7366
7385 7367
7386 7368 /*
7387 7369 * hubd_ioctl: cfgadm controls
7388 7370 */
7389 7371 /* ARGSUSED */
7390 7372 int
7391 7373 usba_hubdi_ioctl(dev_info_t *self, dev_t dev, int cmd, intptr_t arg,
7392 7374 int mode, cred_t *credp, int *rvalp)
7393 7375 {
7394 7376 int rv = 0;
7395 7377 char *msg; /* for messages */
7396 7378 hubd_t *hubd;
7397 7379 usb_port_t port = 0;
7398 7380 dev_info_t *child_dip = NULL;
7399 7381 dev_info_t *rh_dip;
7400 7382 devctl_ap_state_t ap_state;
7401 7383 struct devctl_iocdata *dcp = NULL;
7402 7384 usb_pipe_state_t prev_pipe_state = 0;
7403 7385 int circ, rh_circ, prh_circ;
7404 7386
7405 7387 if ((hubd = hubd_get_soft_state(self)) == NULL) {
7406 7388
7407 7389 return (ENXIO);
7408 7390 }
7409 7391
7410 7392 rh_dip = hubd->h_usba_device->usb_root_hub_dip;
7411 7393
7412 7394 USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle,
7413 7395 "usba_hubdi_ioctl: "
7414 7396 "cmd=%x, arg=%lx, mode=%x, cred=%p, rval=%p dev=0x%lx",
7415 7397 cmd, arg, mode, (void *)credp, (void *)rvalp, dev);
7416 7398
7417 7399 /* read devctl ioctl data */
7418 7400 if ((cmd != DEVCTL_AP_CONTROL) &&
7419 7401 (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS)) {
7420 7402
7421 7403 return (EFAULT);
7422 7404 }
7423 7405
7424 7406 /*
7425 7407 * make sure the hub is connected before trying any
7426 7408 * of the following operations:
7427 7409 * configure, connect, disconnect
7428 7410 */
7429 7411 mutex_enter(HUBD_MUTEX(hubd));
7430 7412
7431 7413 switch (cmd) {
7432 7414 case DEVCTL_AP_DISCONNECT:
7433 7415 case DEVCTL_AP_UNCONFIGURE:
7434 7416 case DEVCTL_AP_CONFIGURE:
7435 7417 if (hubd->h_dev_state == USB_DEV_DISCONNECTED) {
7436 7418 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
7437 7419 "hubd: already gone");
7438 7420 mutex_exit(HUBD_MUTEX(hubd));
7439 7421 if (dcp) {
7440 7422 ndi_dc_freehdl(dcp);
7441 7423 }
7442 7424
7443 7425 return (EIO);
7444 7426 }
7445 7427
7446 7428 /* FALLTHROUGH */
7447 7429 case DEVCTL_AP_GETSTATE:
7448 7430 if ((port = hubd_get_port_num(hubd, dcp)) == 0) {
7449 7431 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
7450 7432 "hubd: bad port");
7451 7433 mutex_exit(HUBD_MUTEX(hubd));
7452 7434 if (dcp) {
7453 7435 ndi_dc_freehdl(dcp);
7454 7436 }
7455 7437
7456 7438 return (EINVAL);
7457 7439 }
7458 7440 break;
7459 7441
7460 7442 case DEVCTL_AP_CONTROL:
7461 7443
7462 7444 break;
7463 7445 default:
7464 7446 mutex_exit(HUBD_MUTEX(hubd));
7465 7447 if (dcp) {
7466 7448 ndi_dc_freehdl(dcp);
7467 7449 }
7468 7450
7469 7451 return (ENOTTY);
7470 7452 }
7471 7453
7472 7454 /* should not happen, just in case */
7473 7455 if (hubd->h_dev_state == USB_DEV_SUSPENDED) {
7474 7456 mutex_exit(HUBD_MUTEX(hubd));
7475 7457 if (dcp) {
7476 7458 ndi_dc_freehdl(dcp);
7477 7459 }
7478 7460
7479 7461 return (EIO);
7480 7462 }
7481 7463
7482 7464 if (hubd->h_reset_port[port]) {
7483 7465 USB_DPRINTF_L2(DPRINT_MASK_CBOPS, hubd->h_log_handle,
7484 7466 "This port is resetting, just return");
7485 7467 mutex_exit(HUBD_MUTEX(hubd));
7486 7468 if (dcp) {
7487 7469 ndi_dc_freehdl(dcp);
7488 7470 }
7489 7471
7490 7472 return (EIO);
7491 7473 }
7492 7474
7493 7475 hubd_pm_busy_component(hubd, hubd->h_dip, 0);
7494 7476 mutex_exit(HUBD_MUTEX(hubd));
7495 7477
7496 7478 /* go full power */
7497 7479 (void) pm_raise_power(hubd->h_dip, 0, USB_DEV_OS_FULL_PWR);
7498 7480
7499 7481 ndi_devi_enter(ddi_get_parent(rh_dip), &prh_circ);
7500 7482 ndi_devi_enter(rh_dip, &rh_circ);
7501 7483 ndi_devi_enter(hubd->h_dip, &circ);
7502 7484
7503 7485 mutex_enter(HUBD_MUTEX(hubd));
7504 7486
7505 7487 hubd->h_hotplug_thread++;
7506 7488
7507 7489 /* stop polling if it was active */
7508 7490 if (hubd->h_ep1_ph) {
7509 7491 mutex_exit(HUBD_MUTEX(hubd));
7510 7492 (void) usb_pipe_get_state(hubd->h_ep1_ph, &prev_pipe_state,
7511 7493 USB_FLAGS_SLEEP);
7512 7494 mutex_enter(HUBD_MUTEX(hubd));
7513 7495
7514 7496 if (prev_pipe_state == USB_PIPE_STATE_ACTIVE) {
7515 7497 hubd_stop_polling(hubd);
7516 7498 }
7517 7499 }
7518 7500
7519 7501 switch (cmd) {
7520 7502 case DEVCTL_AP_DISCONNECT:
7521 7503 if (hubd_delete_child(hubd, port,
7522 7504 NDI_DEVI_REMOVE, B_FALSE) != USB_SUCCESS) {
7523 7505 rv = EIO;
7524 7506 }
7525 7507
7526 7508 break;
7527 7509 case DEVCTL_AP_UNCONFIGURE:
7528 7510 if (hubd_delete_child(hubd, port,
7529 7511 NDI_UNCONFIG, B_FALSE) != USB_SUCCESS) {
7530 7512 rv = EIO;
7531 7513 }
7532 7514
7533 7515 break;
7534 7516 case DEVCTL_AP_CONFIGURE:
7535 7517 /* toggle port */
7536 7518 if (hubd_toggle_port(hubd, port) != USB_SUCCESS) {
7537 7519 rv = EIO;
7538 7520
7539 7521 break;
7540 7522 }
7541 7523
7542 7524 (void) hubd_handle_port_connect(hubd, port);
7543 7525 child_dip = hubd_get_child_dip(hubd, port);
7544 7526 mutex_exit(HUBD_MUTEX(hubd));
7545 7527
7546 7528 ndi_devi_exit(hubd->h_dip, circ);
7547 7529 ndi_devi_exit(rh_dip, rh_circ);
7548 7530 ndi_devi_exit(ddi_get_parent(rh_dip), prh_circ);
7549 7531 if (child_dip == NULL) {
7550 7532 rv = EIO;
7551 7533 } else {
7552 7534 ndi_hold_devi(child_dip);
7553 7535 if (ndi_devi_online(child_dip, 0) != NDI_SUCCESS)
7554 7536 rv = EIO;
7555 7537 ndi_rele_devi(child_dip);
7556 7538 }
7557 7539 ndi_devi_enter(ddi_get_parent(rh_dip), &prh_circ);
7558 7540 ndi_devi_enter(rh_dip, &rh_circ);
7559 7541 ndi_devi_enter(hubd->h_dip, &circ);
7560 7542
7561 7543 mutex_enter(HUBD_MUTEX(hubd));
7562 7544
7563 7545 break;
7564 7546 case DEVCTL_AP_GETSTATE:
7565 7547 switch (hubd_cfgadm_state(hubd, port)) {
7566 7548 case HUBD_CFGADM_DISCONNECTED:
7567 7549 /* port previously 'disconnected' by cfgadm */
7568 7550 ap_state.ap_rstate = AP_RSTATE_DISCONNECTED;
7569 7551 ap_state.ap_ostate = AP_OSTATE_UNCONFIGURED;
7570 7552 ap_state.ap_condition = AP_COND_OK;
7571 7553
7572 7554 break;
7573 7555 case HUBD_CFGADM_UNCONFIGURED:
7574 7556 ap_state.ap_rstate = AP_RSTATE_CONNECTED;
7575 7557 ap_state.ap_ostate = AP_OSTATE_UNCONFIGURED;
7576 7558 ap_state.ap_condition = AP_COND_OK;
7577 7559
7578 7560 break;
7579 7561 case HUBD_CFGADM_CONFIGURED:
7580 7562 ap_state.ap_rstate = AP_RSTATE_CONNECTED;
7581 7563 ap_state.ap_ostate = AP_OSTATE_CONFIGURED;
7582 7564 ap_state.ap_condition = AP_COND_OK;
7583 7565
7584 7566 break;
7585 7567 case HUBD_CFGADM_STILL_REFERENCED:
7586 7568 ap_state.ap_rstate = AP_RSTATE_EMPTY;
7587 7569 ap_state.ap_ostate = AP_OSTATE_CONFIGURED;
7588 7570 ap_state.ap_condition = AP_COND_UNUSABLE;
7589 7571
7590 7572 break;
7591 7573 case HUBD_CFGADM_EMPTY:
7592 7574 default:
7593 7575 ap_state.ap_rstate = AP_RSTATE_EMPTY;
7594 7576 ap_state.ap_ostate = AP_OSTATE_UNCONFIGURED;
7595 7577 ap_state.ap_condition = AP_COND_OK;
7596 7578
7597 7579 break;
7598 7580 }
7599 7581
7600 7582 ap_state.ap_last_change = (time_t)-1;
7601 7583 ap_state.ap_error_code = 0;
7602 7584 ap_state.ap_in_transition = 0;
7603 7585
7604 7586 USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle,
7605 7587 "DEVCTL_AP_GETSTATE: "
7606 7588 "ostate=0x%x, rstate=0x%x, condition=0x%x",
7607 7589 ap_state.ap_ostate,
7608 7590 ap_state.ap_rstate, ap_state.ap_condition);
7609 7591
7610 7592 /* copy the return-AP-state information to the user space */
7611 7593 if (ndi_dc_return_ap_state(&ap_state, dcp) != NDI_SUCCESS) {
7612 7594 rv = EFAULT;
7613 7595 }
7614 7596
7615 7597 break;
7616 7598 case DEVCTL_AP_CONTROL:
7617 7599 {
7618 7600 /*
7619 7601 * Generic devctl for hardware-specific functionality.
7620 7602 * For list of sub-commands see hubd_impl.h
7621 7603 */
7622 7604 hubd_ioctl_data_t ioc; /* for 64 byte copies */
7623 7605
7624 7606 /* copy user ioctl data in first */
7625 7607 #ifdef _MULTI_DATAMODEL
7626 7608 if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
7627 7609 hubd_ioctl_data_32_t ioc32;
7628 7610
7629 7611 if (ddi_copyin((void *)arg, (void *)&ioc32,
7630 7612 sizeof (ioc32), mode) != 0) {
7631 7613 rv = EFAULT;
7632 7614
7633 7615 break;
7634 7616 }
7635 7617 ioc.cmd = (uint_t)ioc32.cmd;
7636 7618 ioc.port = (uint_t)ioc32.port;
7637 7619 ioc.get_size = (uint_t)ioc32.get_size;
7638 7620 ioc.buf = (caddr_t)(uintptr_t)ioc32.buf;
7639 7621 ioc.bufsiz = (uint_t)ioc32.bufsiz;
7640 7622 ioc.misc_arg = (uint_t)ioc32.misc_arg;
7641 7623 } else
7642 7624 #endif /* _MULTI_DATAMODEL */
7643 7625 if (ddi_copyin((void *)arg, (void *)&ioc, sizeof (ioc),
7644 7626 mode) != 0) {
7645 7627 rv = EFAULT;
7646 7628
7647 7629 break;
7648 7630 }
7649 7631
7650 7632 USB_DPRINTF_L3(DPRINT_MASK_CBOPS, hubd->h_log_handle,
7651 7633 "DEVCTL_AP_CONTROL: ioc: cmd=0x%x port=%d get_size=%d"
7652 7634 "\n\tbuf=0x%p, bufsiz=%d, misc_arg=%d", ioc.cmd,
7653 7635 ioc.port, ioc.get_size, (void *)ioc.buf, ioc.bufsiz,
7654 7636 ioc.misc_arg);
7655 7637
7656 7638 /*
7657 7639 * To avoid BE/LE and 32/64 issues, a get_size always
7658 7640 * returns a 32-bit number.
7659 7641 */
7660 7642 if (ioc.get_size != 0 && ioc.bufsiz != (sizeof (uint32_t))) {
7661 7643 rv = EINVAL;
7662 7644
7663 7645 break;
7664 7646 }
7665 7647
7666 7648 switch (ioc.cmd) {
7667 7649 case USB_DESCR_TYPE_DEV:
7668 7650 msg = "DEVCTL_AP_CONTROL: GET_DEVICE_DESC";
7669 7651 if (ioc.get_size) {
7670 7652 /* uint32 so this works 32/64 */
7671 7653 uint32_t size = sizeof (usb_dev_descr_t);
7672 7654
7673 7655 if (ddi_copyout((void *)&size, ioc.buf,
7674 7656 ioc.bufsiz, mode) != 0) {
7675 7657 USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
7676 7658 hubd->h_log_handle,
7677 7659 "%s: get_size copyout failed", msg);
7678 7660 rv = EIO;
7679 7661
7680 7662 break;
7681 7663 }
7682 7664 } else { /* send out the actual descr */
7683 7665 usb_dev_descr_t *dev_descrp;
7684 7666
7685 7667 /* check child_dip */
7686 7668 if ((child_dip = hubd_get_child_dip(hubd,
7687 7669 ioc.port)) == NULL) {
7688 7670 rv = EINVAL;
7689 7671
7690 7672 break;
7691 7673 }
7692 7674
7693 7675 dev_descrp = usb_get_dev_descr(child_dip);
7694 7676 if (ioc.bufsiz != sizeof (*dev_descrp)) {
7695 7677 USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
7696 7678 hubd->h_log_handle,
7697 7679 "%s: bufsize passed (%d) != sizeof "
7698 7680 "usba_device_descr_t (%d)", msg,
7699 7681 ioc.bufsiz, dev_descrp->bLength);
7700 7682 rv = EINVAL;
7701 7683
7702 7684 break;
7703 7685 }
7704 7686
7705 7687 if (ddi_copyout((void *)dev_descrp,
7706 7688 ioc.buf, ioc.bufsiz, mode) != 0) {
7707 7689 USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
7708 7690 hubd->h_log_handle,
7709 7691 "%s: copyout failed.", msg);
7710 7692 rv = EIO;
7711 7693
7712 7694 break;
7713 7695 }
7714 7696 }
7715 7697 break;
7716 7698 case USB_DESCR_TYPE_STRING:
7717 7699 {
7718 7700 char *str;
7719 7701 uint32_t size;
7720 7702 usba_device_t *usba_device;
7721 7703
7722 7704 msg = "DEVCTL_AP_CONTROL: GET_STRING_DESCR";
7723 7705 USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle,
7724 7706 "%s: string request: %d", msg, ioc.misc_arg);
7725 7707
7726 7708 /* recheck */
7727 7709 if ((child_dip = hubd_get_child_dip(hubd, ioc.port)) ==
7728 7710 NULL) {
7729 7711 rv = EINVAL;
7730 7712
7731 7713 break;
7732 7714 }
7733 7715 usba_device = usba_get_usba_device(child_dip);
7734 7716
7735 7717 switch (ioc.misc_arg) {
7736 7718 case HUBD_MFG_STR:
7737 7719 str = usba_device->usb_mfg_str;
7738 7720
7739 7721 break;
7740 7722 case HUBD_PRODUCT_STR:
7741 7723 str = usba_device->usb_product_str;
7742 7724
7743 7725 break;
7744 7726 case HUBD_SERIALNO_STR:
7745 7727 str = usba_device->usb_serialno_str;
7746 7728
7747 7729 break;
7748 7730 case HUBD_CFG_DESCR_STR:
7749 7731 mutex_enter(&usba_device->usb_mutex);
7750 7732 str = usba_device->usb_cfg_str_descr[
7751 7733 usba_device->usb_active_cfg_ndx];
7752 7734 mutex_exit(&usba_device->usb_mutex);
7753 7735
7754 7736 break;
7755 7737 default:
7756 7738 USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
7757 7739 hubd->h_log_handle,
7758 7740 "%s: Invalid string request", msg);
7759 7741 rv = EINVAL;
7760 7742
7761 7743 break;
7762 7744 } /* end of switch */
7763 7745
7764 7746 if (rv != 0) {
7765 7747
7766 7748 break;
7767 7749 }
7768 7750
7769 7751 size = (str != NULL) ? strlen(str) + 1 : 0;
7770 7752 if (ioc.get_size) {
7771 7753 if (ddi_copyout((void *)&size, ioc.buf,
7772 7754 ioc.bufsiz, mode) != 0) {
7773 7755 USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
7774 7756 hubd->h_log_handle,
7775 7757 "%s: copyout of size failed.", msg);
7776 7758 rv = EIO;
7777 7759
7778 7760 break;
7779 7761 }
7780 7762 } else {
7781 7763 if (size == 0) {
7782 7764 USB_DPRINTF_L3(DPRINT_MASK_CBOPS,
7783 7765 hubd->h_log_handle,
7784 7766 "%s: String is NULL", msg);
7785 7767 rv = EINVAL;
7786 7768
7787 7769 break;
7788 7770 }
7789 7771
7790 7772 if (ioc.bufsiz != size) {
7791 7773 USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
7792 7774 hubd->h_log_handle,
7793 7775 "%s: string buf size wrong", msg);
7794 7776 rv = EINVAL;
7795 7777
7796 7778 break;
7797 7779 }
7798 7780
7799 7781 if (ddi_copyout((void *)str, ioc.buf,
7800 7782 ioc.bufsiz, mode) != 0) {
7801 7783 USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
7802 7784 hubd->h_log_handle,
7803 7785 "%s: copyout failed.", msg);
7804 7786 rv = EIO;
7805 7787
7806 7788 break;
7807 7789 }
7808 7790 }
7809 7791 break;
7810 7792 }
7811 7793 case HUBD_GET_CFGADM_NAME:
7812 7794 {
7813 7795 uint32_t name_len;
7814 7796 const char *name;
7815 7797
7816 7798 /* recheck */
7817 7799 if ((child_dip = hubd_get_child_dip(hubd, ioc.port)) ==
7818 7800 NULL) {
7819 7801 rv = EINVAL;
7820 7802
7821 7803 break;
7822 7804 }
7823 7805 name = ddi_node_name(child_dip);
7824 7806 if (name == NULL) {
7825 7807 name = "unsupported";
7826 7808 }
7827 7809 name_len = strlen(name) + 1;
7828 7810
7829 7811 msg = "DEVCTL_AP_CONTROL: HUBD_GET_CFGADM_NAME";
7830 7812 USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle,
7831 7813 "%s: name=%s name_len=%d", msg, name, name_len);
7832 7814
7833 7815 if (ioc.get_size) {
7834 7816 if (ddi_copyout((void *)&name_len,
7835 7817 ioc.buf, ioc.bufsiz, mode) != 0) {
7836 7818 USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
7837 7819 hubd->h_log_handle,
7838 7820 "%s: copyout of size failed", msg);
7839 7821 rv = EIO;
7840 7822
7841 7823 break;
7842 7824 }
7843 7825 } else {
7844 7826 if (ioc.bufsiz != name_len) {
7845 7827 USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
7846 7828 hubd->h_log_handle,
7847 7829 "%s: string buf length wrong", msg);
7848 7830 rv = EINVAL;
7849 7831
7850 7832 break;
7851 7833 }
7852 7834
7853 7835 if (ddi_copyout((void *)name, ioc.buf,
7854 7836 ioc.bufsiz, mode) != 0) {
7855 7837 USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
7856 7838 hubd->h_log_handle,
7857 7839 "%s: copyout failed.", msg);
7858 7840 rv = EIO;
7859 7841
7860 7842 break;
7861 7843 }
7862 7844 }
7863 7845
7864 7846 break;
7865 7847 }
7866 7848
7867 7849 /*
7868 7850 * Return the config index for the currently-configured
7869 7851 * configuration.
7870 7852 */
7871 7853 case HUBD_GET_CURRENT_CONFIG:
7872 7854 {
7873 7855 uint_t config_index;
7874 7856 uint32_t size = sizeof (config_index);
7875 7857 usba_device_t *usba_device;
7876 7858
7877 7859 msg = "DEVCTL_AP_CONTROL: GET_CURRENT_CONFIG";
7878 7860 USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle,
7879 7861 "%s", msg);
7880 7862
7881 7863 /*
7882 7864 * Return the config index for the configuration
7883 7865 * currently in use.
7884 7866 * Recheck if child_dip exists
7885 7867 */
7886 7868 if ((child_dip = hubd_get_child_dip(hubd, ioc.port)) ==
7887 7869 NULL) {
7888 7870 rv = EINVAL;
7889 7871
7890 7872 break;
7891 7873 }
7892 7874
7893 7875 usba_device = usba_get_usba_device(child_dip);
7894 7876 mutex_enter(&usba_device->usb_mutex);
7895 7877 config_index = usba_device->usb_active_cfg_ndx;
7896 7878 mutex_exit(&usba_device->usb_mutex);
7897 7879
7898 7880 if (ioc.get_size) {
7899 7881 if (ddi_copyout((void *)&size,
7900 7882 ioc.buf, ioc.bufsiz, mode) != 0) {
7901 7883 USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
7902 7884 hubd->h_log_handle,
7903 7885 "%s: copyout of size failed.", msg);
7904 7886 rv = EIO;
7905 7887
7906 7888 break;
7907 7889 }
7908 7890 } else {
7909 7891 if (ioc.bufsiz != size) {
7910 7892 USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
7911 7893 hubd->h_log_handle,
7912 7894 "%s: buffer size wrong", msg);
7913 7895 rv = EINVAL;
7914 7896
7915 7897 break;
7916 7898 }
7917 7899 if (ddi_copyout((void *)&config_index,
7918 7900 ioc.buf, ioc.bufsiz, mode) != 0) {
7919 7901 USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
7920 7902 hubd->h_log_handle,
7921 7903 "%s: copyout failed", msg);
7922 7904 rv = EIO;
7923 7905 }
7924 7906 }
7925 7907
7926 7908 break;
7927 7909 }
7928 7910 case HUBD_GET_DEVICE_PATH:
7929 7911 {
7930 7912 char *path;
7931 7913 uint32_t size;
7932 7914
7933 7915 msg = "DEVCTL_AP_CONTROL: GET_DEVICE_PATH";
7934 7916 USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle,
7935 7917 "%s", msg);
7936 7918
7937 7919 /* Recheck if child_dip exists */
7938 7920 if ((child_dip = hubd_get_child_dip(hubd, ioc.port)) ==
7939 7921 NULL) {
7940 7922 rv = EINVAL;
7941 7923
7942 7924 break;
7943 7925 }
7944 7926
7945 7927 /* ddi_pathname doesn't supply /devices, so we do. */
7946 7928 path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
7947 7929 (void) strcpy(path, "/devices");
7948 7930 (void) ddi_pathname(child_dip, path + strlen(path));
7949 7931 size = strlen(path) + 1;
7950 7932
7951 7933 USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle,
7952 7934 "%s: device path=%s size=%d", msg, path, size);
7953 7935
7954 7936 if (ioc.get_size) {
7955 7937 if (ddi_copyout((void *)&size,
7956 7938 ioc.buf, ioc.bufsiz, mode) != 0) {
7957 7939
7958 7940 USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
7959 7941 hubd->h_log_handle,
7960 7942 "%s: copyout of size failed.", msg);
7961 7943 rv = EIO;
7962 7944 }
7963 7945 } else {
7964 7946 if (ioc.bufsiz != size) {
7965 7947 USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
7966 7948 hubd->h_log_handle,
7967 7949 "%s: buffer wrong size.", msg);
7968 7950 rv = EINVAL;
7969 7951 } else if (ddi_copyout((void *)path,
7970 7952 ioc.buf, ioc.bufsiz, mode) != 0) {
7971 7953 USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
7972 7954 hubd->h_log_handle,
7973 7955 "%s: copyout failed.", msg);
7974 7956 rv = EIO;
7975 7957 }
7976 7958 }
7977 7959 kmem_free(path, MAXPATHLEN);
7978 7960
7979 7961 break;
7980 7962 }
7981 7963 case HUBD_REFRESH_DEVDB:
7982 7964 msg = "DEVCTL_AP_CONTROL: HUBD_REFRESH_DEVDB";
7983 7965 USB_DPRINTF_L3(DPRINT_MASK_CBOPS, hubd->h_log_handle,
7984 7966 "%s", msg);
7985 7967
7986 7968 if ((rv = usba_devdb_refresh()) != USB_SUCCESS) {
7987 7969 USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
7988 7970 hubd->h_log_handle,
7989 7971 "%s: Failed: %d", msg, rv);
7990 7972 rv = EIO;
7991 7973 }
7992 7974
7993 7975 break;
7994 7976 default:
7995 7977 rv = ENOTSUP;
7996 7978 } /* end switch */
7997 7979
7998 7980 break;
7999 7981 }
8000 7982
8001 7983 default:
8002 7984 rv = ENOTTY;
8003 7985 }
8004 7986
8005 7987 if (dcp) {
8006 7988 ndi_dc_freehdl(dcp);
8007 7989 }
8008 7990
8009 7991 /* allow hotplug thread now */
8010 7992 hubd->h_hotplug_thread--;
8011 7993
8012 7994 if ((hubd->h_dev_state == USB_DEV_ONLINE) &&
8013 7995 hubd->h_ep1_ph && (prev_pipe_state == USB_PIPE_STATE_ACTIVE)) {
8014 7996 hubd_start_polling(hubd, 0);
8015 7997 }
8016 7998 mutex_exit(HUBD_MUTEX(hubd));
8017 7999
8018 8000 ndi_devi_exit(hubd->h_dip, circ);
8019 8001 ndi_devi_exit(rh_dip, rh_circ);
8020 8002 ndi_devi_exit(ddi_get_parent(rh_dip), prh_circ);
8021 8003
8022 8004 mutex_enter(HUBD_MUTEX(hubd));
8023 8005 hubd_pm_idle_component(hubd, hubd->h_dip, 0);
8024 8006 mutex_exit(HUBD_MUTEX(hubd));
8025 8007
8026 8008 return (rv);
8027 8009 }
8028 8010
8029 8011
8030 8012 /*
8031 8013 * Helper func used only to help construct the names for the attachment point
8032 8014 * minor nodes. Used only in usba_hubdi_attach.
8033 8015 * Returns whether it found ancestry or not (USB_SUCCESS if yes).
8034 8016 * ports between the root hub and the device represented by dip.
8035 8017 * E.g., "2.4.3.1" means this device is
8036 8018 * plugged into port 1 of a hub that is
8037 8019 * plugged into port 3 of a hub that is
8038 8020 * plugged into port 4 of a hub that is
8039 8021 * plugged into port 2 of the root hub.
8040 8022 * NOTE: Max ap_id path len is HUBD_APID_NAMELEN (32 chars), which is
8041 8023 * more than sufficient (as hubs are a max 6 levels deep, port needs 3
8042 8024 * chars plus NULL each)
8043 8025 */
8044 8026 void
8045 8027 hubd_get_ancestry_str(hubd_t *hubd)
8046 8028 {
8047 8029 char ap_name[HUBD_APID_NAMELEN];
8048 8030 dev_info_t *pdip;
8049 8031 hubd_t *phubd;
8050 8032 usb_port_t port;
8051 8033
8052 8034 USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
8053 8035 "hubd_get_ancestry_str: hubd=0x%p", (void *)hubd);
8054 8036
8055 8037 ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
8056 8038
8057 8039 /*
8058 8040 * The function is extended to support wire adapter class
8059 8041 * devices introduced by WUSB spec. The node name is no
8060 8042 * longer "hub" only.
8061 8043 * Generate the ap_id str based on the parent and child
8062 8044 * relationship instead of retrieving it from the hub
8063 8045 * device path, which simplifies the algorithm.
8064 8046 */
8065 8047 if (usba_is_root_hub(hubd->h_dip)) {
8066 8048 hubd->h_ancestry_str[0] = '\0';
8067 8049 } else {
8068 8050 port = hubd->h_usba_device->usb_port;
8069 8051 mutex_exit(HUBD_MUTEX(hubd));
8070 8052
8071 8053 pdip = ddi_get_parent(hubd->h_dip);
8072 8054 /*
8073 8055 * The parent of wire adapter device might be usb_mid.
8074 8056 * Need to look further up for hub device
8075 8057 */
8076 8058 if (strcmp(ddi_driver_name(pdip), "usb_mid") == 0) {
8077 8059 pdip = ddi_get_parent(pdip);
8078 8060 ASSERT(pdip != NULL);
8079 8061 }
8080 8062
8081 8063 phubd = hubd_get_soft_state(pdip);
8082 8064
8083 8065 mutex_enter(HUBD_MUTEX(phubd));
8084 8066 (void) snprintf(ap_name, HUBD_APID_NAMELEN, "%s%d",
8085 8067 phubd->h_ancestry_str, port);
8086 8068 mutex_exit(HUBD_MUTEX(phubd));
8087 8069
8088 8070 mutex_enter(HUBD_MUTEX(hubd));
8089 8071 (void) strcpy(hubd->h_ancestry_str, ap_name);
8090 8072 (void) strcat(hubd->h_ancestry_str, ".");
8091 8073 }
8092 8074 }
8093 8075
8094 8076
8095 8077 /* Get which port to operate on. */
8096 8078 static usb_port_t
8097 8079 hubd_get_port_num(hubd_t *hubd, struct devctl_iocdata *dcp)
8098 8080 {
8099 8081 int32_t port;
8100 8082
8101 8083 ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
8102 8084
8103 8085 /* Get which port to operate on. */
8104 8086 if (nvlist_lookup_int32(ndi_dc_get_ap_data(dcp), "port", &port) != 0) {
8105 8087 USB_DPRINTF_L2(DPRINT_MASK_CBOPS, hubd->h_log_handle,
8106 8088 "hubd_get_port_num: port lookup failed");
8107 8089 port = 0;
8108 8090 }
8109 8091
8110 8092 USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle,
8111 8093 "hubd_get_port_num: hubd=0x%p, port=%d", (void *)hubd, port);
8112 8094
8113 8095 return ((usb_port_t)port);
8114 8096 }
8115 8097
8116 8098
8117 8099 /* check if child still exists */
8118 8100 static dev_info_t *
8119 8101 hubd_get_child_dip(hubd_t *hubd, usb_port_t port)
8120 8102 {
8121 8103 dev_info_t *child_dip = hubd->h_children_dips[port];
8122 8104
8123 8105 USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle,
8124 8106 "hubd_get_child_dip: hubd=0x%p, port=%d", (void *)hubd, port);
8125 8107
8126 8108 ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
8127 8109
8128 8110 return (child_dip);
8129 8111 }
8130 8112
8131 8113
8132 8114 /*
8133 8115 * hubd_cfgadm_state:
8134 8116 *
8135 8117 * child_dip list port_state cfgadm_state
8136 8118 * -------------- ---------- ------------
8137 8119 * != NULL connected configured or
8138 8120 * unconfigured
8139 8121 * != NULL not connected disconnect but
8140 8122 * busy/still referenced
8141 8123 * NULL connected logically disconnected
8142 8124 * NULL not connected empty
8143 8125 */
8144 8126 static uint_t
8145 8127 hubd_cfgadm_state(hubd_t *hubd, usb_port_t port)
8146 8128 {
8147 8129 uint_t state;
8148 8130 dev_info_t *child_dip = hubd_get_child_dip(hubd, port);
8149 8131
8150 8132 if (child_dip) {
8151 8133 if (hubd->h_port_state[port] & PORT_STATUS_CCS) {
8152 8134 /*
8153 8135 * connected, now check if driver exists
8154 8136 */
8155 8137 if (DEVI_IS_DEVICE_OFFLINE(child_dip) ||
8156 8138 !i_ddi_devi_attached(child_dip)) {
8157 8139 state = HUBD_CFGADM_UNCONFIGURED;
8158 8140 } else {
8159 8141 state = HUBD_CFGADM_CONFIGURED;
8160 8142 }
8161 8143 } else {
8162 8144 /*
8163 8145 * this means that the dip is around for
8164 8146 * a device that is still referenced but
8165 8147 * has been yanked out. So the cfgadm info
8166 8148 * for this state should be EMPTY (port empty)
8167 8149 * and CONFIGURED (dip still valid).
8168 8150 */
8169 8151 state = HUBD_CFGADM_STILL_REFERENCED;
8170 8152 }
8171 8153 } else {
8172 8154 /* connected but no child dip */
8173 8155 if (hubd->h_port_state[port] & PORT_STATUS_CCS) {
8174 8156 /* logically disconnected */
8175 8157 state = HUBD_CFGADM_DISCONNECTED;
8176 8158 } else {
8177 8159 /* physically disconnected */
8178 8160 state = HUBD_CFGADM_EMPTY;
8179 8161 }
8180 8162 }
8181 8163
8182 8164 USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle,
8183 8165 "hubd_cfgadm_state: hubd=0x%p, port=%d state=0x%x",
8184 8166 (void *)hubd, port, state);
8185 8167
8186 8168 return (state);
8187 8169 }
8188 8170
8189 8171
8190 8172 /*
8191 8173 * hubd_toggle_port:
8192 8174 */
8193 8175 static int
8194 8176 hubd_toggle_port(hubd_t *hubd, usb_port_t port)
8195 8177 {
8196 8178 usb_hub_descr_t *hub_descr;
8197 8179 int wait;
8198 8180 uint_t retry;
8199 8181 uint16_t status;
8200 8182 uint16_t change;
8201 8183
8202 8184 USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle,
8203 8185 "hubd_toggle_port: hubd=0x%p, port=%d", (void *)hubd, port);
8204 8186
8205 8187 if ((hubd_disable_port_power(hubd, port)) != USB_SUCCESS) {
8206 8188
8207 8189 return (USB_FAILURE);
8208 8190 }
8209 8191
8210 8192 /*
8211 8193 * see hubd_enable_all_port_power() which
8212 8194 * requires longer delay for hubs.
8213 8195 */
8214 8196 mutex_exit(HUBD_MUTEX(hubd));
8215 8197 delay(drv_usectohz(hubd_device_delay / 10));
8216 8198 mutex_enter(HUBD_MUTEX(hubd));
8217 8199
8218 8200 hub_descr = &hubd->h_hub_descr;
8219 8201
8220 8202 /*
8221 8203 * According to section 11.11 of USB, for hubs with no power
8222 8204 * switches, bPwrOn2PwrGood is zero. But we wait for some
8223 8205 * arbitrary time to enable power to become stable.
8224 8206 *
8225 8207 * If an hub supports port power swicthing, we need to wait
8226 8208 * at least 20ms before accesing corresonding usb port.
8227 8209 */
8228 8210 if ((hub_descr->wHubCharacteristics &
8229 8211 HUB_CHARS_NO_POWER_SWITCHING) || (!hub_descr->bPwrOn2PwrGood)) {
8230 8212 wait = hubd_device_delay / 10;
8231 8213 } else {
8232 8214 wait = max(HUB_DEFAULT_POPG,
8233 8215 hub_descr->bPwrOn2PwrGood) * 2 * 1000;
8234 8216 }
8235 8217
8236 8218 USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
8237 8219 "hubd_toggle_port: popg=%d wait=%d",
8238 8220 hub_descr->bPwrOn2PwrGood, wait);
8239 8221
8240 8222 retry = 0;
8241 8223
8242 8224 do {
8243 8225 (void) hubd_enable_port_power(hubd, port);
8244 8226
8245 8227 mutex_exit(HUBD_MUTEX(hubd));
8246 8228 delay(drv_usectohz(wait));
8247 8229 mutex_enter(HUBD_MUTEX(hubd));
8248 8230
8249 8231 /* Get port status */
8250 8232 (void) hubd_determine_port_status(hubd, port,
8251 8233 &status, &change, 0);
8252 8234
8253 8235 /* For retry if any, use some extra delay */
8254 8236 wait = max(wait, hubd_device_delay / 10);
8255 8237
8256 8238 retry++;
8257 8239
8258 8240 } while ((!(status & PORT_STATUS_PPS)) && (retry < HUBD_PORT_RETRY));
8259 8241
8260 8242 /* Print warning message if port has no power */
8261 8243 if (!(status & PORT_STATUS_PPS)) {
8262 8244
8263 8245 USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
8264 8246 "hubd_toggle_port: port %d power-on failed, "
8265 8247 "port status 0x%x", port, status);
8266 8248
8267 8249 return (USB_FAILURE);
8268 8250 }
8269 8251
8270 8252 return (USB_SUCCESS);
8271 8253 }
8272 8254
8273 8255
8274 8256 /*
8275 8257 * hubd_init_power_budget:
8276 8258 * Init power budget variables in hubd structure. According
8277 8259 * to USB spec, the power budget rules are:
8278 8260 * 1. local-powered hubs including root-hubs can supply
8279 8261 * 500mA to each port at maximum
8280 8262 * 2. two bus-powered hubs are not allowed to concatenate
8281 8263 * 3. bus-powered hubs can supply 100mA to each port at
8282 8264 * maximum, and the power consumed by all downstream
8283 8265 * ports and the hub itself cannot exceed the max power
8284 8266 * supplied by the upstream port, i.e., 500mA
8285 8267 * The routine is only called during hub attach time
8286 8268 */
8287 8269 static int
8288 8270 hubd_init_power_budget(hubd_t *hubd)
8289 8271 {
8290 8272 uint16_t status = 0;
8291 8273 usba_device_t *hubd_ud = NULL;
8292 8274 size_t size;
8293 8275 usb_cfg_descr_t cfg_descr;
8294 8276 dev_info_t *pdip = NULL;
8295 8277 hubd_t *phubd = NULL;
8296 8278
8297 8279 if (hubd->h_ignore_pwr_budget) {
8298 8280
8299 8281 return (USB_SUCCESS);
8300 8282 }
8301 8283
8302 8284 USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle,
8303 8285 "hubd_init_power_budget:");
8304 8286
8305 8287 ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
8306 8288 ASSERT(hubd->h_default_pipe != 0);
8307 8289 mutex_exit(HUBD_MUTEX(hubd));
8308 8290
8309 8291 /* get device status */
8310 8292 if ((usb_get_status(hubd->h_dip, hubd->h_default_pipe,
8311 8293 HUB_GET_DEVICE_STATUS_TYPE,
8312 8294 0, &status, 0)) != USB_SUCCESS) {
8313 8295 mutex_enter(HUBD_MUTEX(hubd));
8314 8296
8315 8297 return (USB_FAILURE);
8316 8298 }
8317 8299
8318 8300 hubd_ud = usba_get_usba_device(hubd->h_dip);
8319 8301
8320 8302 size = usb_parse_cfg_descr(hubd_ud->usb_cfg, hubd_ud->usb_cfg_length,
8321 8303 &cfg_descr, USB_CFG_DESCR_SIZE);
8322 8304
8323 8305 if (size != USB_CFG_DESCR_SIZE) {
8324 8306 USB_DPRINTF_L2(DPRINT_MASK_HUB, hubd->h_log_handle,
8325 8307 "get hub configuration descriptor failed");
8326 8308 mutex_enter(HUBD_MUTEX(hubd));
8327 8309
8328 8310 return (USB_FAILURE);
8329 8311 }
8330 8312
8331 8313 mutex_enter(HUBD_MUTEX(hubd));
8332 8314
8333 8315 hubd->h_local_pwr_capable = (cfg_descr.bmAttributes &
8334 8316 USB_CFG_ATTR_SELFPWR);
8335 8317
8336 8318 if (hubd->h_local_pwr_capable) {
8337 8319 USB_DPRINTF_L3(DPRINT_MASK_HUB, hubd->h_log_handle,
8338 8320 "hub is capable of local power");
8339 8321 }
8340 8322
8341 8323 hubd->h_local_pwr_on = (status &
8342 8324 USB_DEV_SLF_PWRD_STATUS) && hubd->h_local_pwr_capable;
8343 8325
8344 8326 if (hubd->h_local_pwr_on) {
8345 8327 USB_DPRINTF_L3(DPRINT_MASK_HUB, hubd->h_log_handle,
8346 8328 "hub is local-powered");
8347 8329
8348 8330 hubd->h_pwr_limit = (USB_PWR_UNIT_LOAD *
8349 8331 USB_HIGH_PWR_VALUE) / USB_CFG_DESCR_PWR_UNIT;
8350 8332 } else {
8351 8333 hubd->h_pwr_limit = (USB_PWR_UNIT_LOAD *
8352 8334 USB_LOW_PWR_VALUE) / USB_CFG_DESCR_PWR_UNIT;
8353 8335
8354 8336 hubd->h_pwr_left = (USB_PWR_UNIT_LOAD *
8355 8337 USB_HIGH_PWR_VALUE) / USB_CFG_DESCR_PWR_UNIT;
8356 8338
8357 8339 ASSERT(!usba_is_root_hub(hubd->h_dip));
8358 8340
8359 8341 if (!usba_is_root_hub(hubd->h_dip)) {
8360 8342 /*
8361 8343 * two bus-powered hubs are not
8362 8344 * allowed to be concatenated
8363 8345 */
8364 8346 mutex_exit(HUBD_MUTEX(hubd));
8365 8347
8366 8348 pdip = ddi_get_parent(hubd->h_dip);
8367 8349 phubd = hubd_get_soft_state(pdip);
8368 8350 ASSERT(phubd != NULL);
8369 8351
8370 8352 if (!phubd->h_ignore_pwr_budget) {
8371 8353 mutex_enter(HUBD_MUTEX(phubd));
8372 8354 if (phubd->h_local_pwr_on == B_FALSE) {
8373 8355 USB_DPRINTF_L1(DPRINT_MASK_HUB,
8374 8356 hubd->h_log_handle,
8375 8357 "two bus-powered hubs cannot "
8376 8358 "be concatenated");
8377 8359
8378 8360 mutex_exit(HUBD_MUTEX(phubd));
8379 8361 mutex_enter(HUBD_MUTEX(hubd));
8380 8362
8381 8363 return (USB_FAILURE);
8382 8364 }
8383 8365 mutex_exit(HUBD_MUTEX(phubd));
8384 8366 }
8385 8367
8386 8368 mutex_enter(HUBD_MUTEX(hubd));
8387 8369
8388 8370 USB_DPRINTF_L3(DPRINT_MASK_HUB, hubd->h_log_handle,
8389 8371 "hub is bus-powered");
8390 8372 } else {
8391 8373 USB_DPRINTF_L3(DPRINT_MASK_HUB, hubd->h_log_handle,
8392 8374 "root-hub must be local-powered");
8393 8375 }
8394 8376
8395 8377 /*
8396 8378 * Subtract the power consumed by the hub itself
8397 8379 * and get the power that can be supplied to
8398 8380 * downstream ports
8399 8381 */
8400 8382 hubd->h_pwr_left -=
8401 8383 hubd->h_hub_descr.bHubContrCurrent /
8402 8384 USB_CFG_DESCR_PWR_UNIT;
8403 8385 if (hubd->h_pwr_left < 0) {
8404 8386 USB_DPRINTF_L2(DPRINT_MASK_HUB, hubd->h_log_handle,
8405 8387 "hubd->h_pwr_left is less than bHubContrCurrent, "
8406 8388 "should fail");
8407 8389
8408 8390 return (USB_FAILURE);
8409 8391 }
8410 8392 }
8411 8393
8412 8394 return (USB_SUCCESS);
8413 8395 }
8414 8396
8415 8397
8416 8398 /*
8417 8399 * usba_hubdi_check_power_budget:
8418 8400 * Check if the hub has enough power budget to allow a
8419 8401 * child device to select a configuration of config_index.
8420 8402 */
8421 8403 int
8422 8404 usba_hubdi_check_power_budget(dev_info_t *dip, usba_device_t *child_ud,
8423 8405 uint_t config_index)
8424 8406 {
8425 8407 int16_t pwr_left, pwr_limit, pwr_required;
8426 8408 size_t size;
8427 8409 usb_cfg_descr_t cfg_descr;
8428 8410 hubd_t *hubd;
8429 8411
8430 8412 if ((hubd = hubd_get_soft_state(dip)) == NULL) {
8431 8413
8432 8414 return (USB_FAILURE);
8433 8415 }
8434 8416
8435 8417 if (hubd->h_ignore_pwr_budget) {
8436 8418
8437 8419 return (USB_SUCCESS);
8438 8420 }
8439 8421
8440 8422 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
8441 8423 "usba_hubdi_check_power_budget: "
8442 8424 "dip=0x%p child_ud=0x%p conf_index=%d", (void *)dip,
8443 8425 (void *)child_ud, config_index);
8444 8426
8445 8427 mutex_enter(HUBD_MUTEX(hubd));
8446 8428 pwr_limit = hubd->h_pwr_limit;
8447 8429 if (hubd->h_local_pwr_on == B_FALSE) {
8448 8430 pwr_left = hubd->h_pwr_left;
8449 8431 pwr_limit = (pwr_limit <= pwr_left) ? pwr_limit : pwr_left;
8450 8432 }
8451 8433 mutex_exit(HUBD_MUTEX(hubd));
8452 8434
8453 8435 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
8454 8436 "usba_hubdi_check_power_budget: "
8455 8437 "available power is %dmA", pwr_limit * USB_CFG_DESCR_PWR_UNIT);
8456 8438
8457 8439 size = usb_parse_cfg_descr(
8458 8440 child_ud->usb_cfg_array[config_index], USB_CFG_DESCR_SIZE,
8459 8441 &cfg_descr, USB_CFG_DESCR_SIZE);
8460 8442
8461 8443 if (size != USB_CFG_DESCR_SIZE) {
8462 8444 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
8463 8445 "get hub configuration descriptor failed");
8464 8446
8465 8447 return (USB_FAILURE);
8466 8448 }
8467 8449
8468 8450 pwr_required = cfg_descr.bMaxPower;
8469 8451
8470 8452 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
8471 8453 "usba_hubdi_check_power_budget: "
8472 8454 "child bmAttributes=0x%x bMaxPower=%d "
8473 8455 "with config_index=%d", cfg_descr.bmAttributes,
8474 8456 pwr_required, config_index);
8475 8457
8476 8458 if (pwr_required > pwr_limit) {
8477 8459 USB_DPRINTF_L1(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
8478 8460 "configuration %d for device %s %s at port %d "
8479 8461 "exceeds power available for this port, please "
8480 8462 "re-insert your device into another hub port which "
8481 8463 "has enough power",
8482 8464 config_index,
8483 8465 child_ud->usb_mfg_str,
8484 8466 child_ud->usb_product_str,
8485 8467 child_ud->usb_port);
8486 8468
8487 8469 return (USB_FAILURE);
8488 8470 }
8489 8471
8490 8472 return (USB_SUCCESS);
8491 8473 }
8492 8474
8493 8475
8494 8476 /*
8495 8477 * usba_hubdi_incr_power_budget:
8496 8478 * Increase the hub power budget value when a child device
8497 8479 * is removed from a bus-powered hub port.
8498 8480 */
8499 8481 void
8500 8482 usba_hubdi_incr_power_budget(dev_info_t *dip, usba_device_t *child_ud)
8501 8483 {
8502 8484 uint16_t pwr_value;
8503 8485 hubd_t *hubd = hubd_get_soft_state(dip);
8504 8486
8505 8487 ASSERT(hubd != NULL);
8506 8488
8507 8489 if (hubd->h_ignore_pwr_budget) {
8508 8490
8509 8491 return;
8510 8492 }
8511 8493
8512 8494 USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
8513 8495 "usba_hubdi_incr_power_budget: "
8514 8496 "dip=0x%p child_ud=0x%p", (void *)dip, (void *)child_ud);
8515 8497
8516 8498 mutex_enter(HUBD_MUTEX(hubd));
8517 8499 if (hubd->h_local_pwr_on == B_TRUE) {
8518 8500 USB_DPRINTF_L3(DPRINT_MASK_ATTA, hubd->h_log_handle,
8519 8501 "usba_hubdi_incr_power_budget: "
8520 8502 "hub is local powered");
8521 8503 mutex_exit(HUBD_MUTEX(hubd));
8522 8504
8523 8505 return;
8524 8506 }
8525 8507 mutex_exit(HUBD_MUTEX(hubd));
8526 8508
8527 8509 mutex_enter(&child_ud->usb_mutex);
8528 8510 if (child_ud->usb_pwr_from_hub == 0) {
8529 8511 mutex_exit(&child_ud->usb_mutex);
8530 8512
8531 8513 return;
8532 8514 }
8533 8515 pwr_value = child_ud->usb_pwr_from_hub;
8534 8516 mutex_exit(&child_ud->usb_mutex);
8535 8517
8536 8518 mutex_enter(HUBD_MUTEX(hubd));
8537 8519 hubd->h_pwr_left += pwr_value;
8538 8520
8539 8521 USB_DPRINTF_L3(DPRINT_MASK_ATTA, hubd->h_log_handle,
8540 8522 "usba_hubdi_incr_power_budget: "
8541 8523 "available power is %dmA, increased by %dmA",
8542 8524 hubd->h_pwr_left * USB_CFG_DESCR_PWR_UNIT,
8543 8525 pwr_value * USB_CFG_DESCR_PWR_UNIT);
8544 8526
8545 8527 mutex_exit(HUBD_MUTEX(hubd));
8546 8528
8547 8529 mutex_enter(&child_ud->usb_mutex);
8548 8530 child_ud->usb_pwr_from_hub = 0;
8549 8531 mutex_exit(&child_ud->usb_mutex);
8550 8532 }
8551 8533
8552 8534
8553 8535 /*
8554 8536 * usba_hubdi_decr_power_budget:
8555 8537 * Decrease the hub power budget value when a child device
8556 8538 * is inserted to a bus-powered hub port.
8557 8539 */
8558 8540 void
8559 8541 usba_hubdi_decr_power_budget(dev_info_t *dip, usba_device_t *child_ud)
8560 8542 {
8561 8543 uint16_t pwr_value;
8562 8544 size_t size;
8563 8545 usb_cfg_descr_t cfg_descr;
8564 8546 hubd_t *hubd = hubd_get_soft_state(dip);
8565 8547
8566 8548 ASSERT(hubd != NULL);
8567 8549
8568 8550 if (hubd->h_ignore_pwr_budget) {
8569 8551
8570 8552 return;
8571 8553 }
8572 8554
8573 8555 USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
8574 8556 "usba_hubdi_decr_power_budget: "
8575 8557 "dip=0x%p child_ud=0x%p", (void *)dip, (void *)child_ud);
8576 8558
8577 8559 mutex_enter(HUBD_MUTEX(hubd));
8578 8560 if (hubd->h_local_pwr_on == B_TRUE) {
8579 8561 USB_DPRINTF_L3(DPRINT_MASK_ATTA, hubd->h_log_handle,
8580 8562 "usba_hubdi_decr_power_budget: "
8581 8563 "hub is local powered");
8582 8564 mutex_exit(HUBD_MUTEX(hubd));
8583 8565
8584 8566 return;
8585 8567 }
8586 8568 mutex_exit(HUBD_MUTEX(hubd));
8587 8569
8588 8570 mutex_enter(&child_ud->usb_mutex);
8589 8571 if (child_ud->usb_pwr_from_hub > 0) {
8590 8572 mutex_exit(&child_ud->usb_mutex);
8591 8573
8592 8574 return;
8593 8575 }
8594 8576 mutex_exit(&child_ud->usb_mutex);
8595 8577
8596 8578 size = usb_parse_cfg_descr(
8597 8579 child_ud->usb_cfg, child_ud->usb_cfg_length,
8598 8580 &cfg_descr, USB_CFG_DESCR_SIZE);
8599 8581 ASSERT(size == USB_CFG_DESCR_SIZE);
8600 8582
8601 8583 mutex_enter(HUBD_MUTEX(hubd));
8602 8584 pwr_value = cfg_descr.bMaxPower;
8603 8585 hubd->h_pwr_left -= pwr_value;
8604 8586 ASSERT(hubd->h_pwr_left >= 0);
8605 8587
8606 8588 USB_DPRINTF_L3(DPRINT_MASK_ATTA, hubd->h_log_handle,
8607 8589 "usba_hubdi_decr_power_budget: "
8608 8590 "available power is %dmA, decreased by %dmA",
8609 8591 hubd->h_pwr_left * USB_CFG_DESCR_PWR_UNIT,
8610 8592 pwr_value * USB_CFG_DESCR_PWR_UNIT);
8611 8593
8612 8594 mutex_exit(HUBD_MUTEX(hubd));
8613 8595
8614 8596 mutex_enter(&child_ud->usb_mutex);
8615 8597 child_ud->usb_pwr_from_hub = pwr_value;
8616 8598 mutex_exit(&child_ud->usb_mutex);
8617 8599 }
8618 8600
8619 8601 /*
8620 8602 * hubd_wait_for_hotplug_exit:
8621 8603 * Waiting for the exit of the running hotplug thread or ioctl thread.
8622 8604 */
8623 8605 static int
8624 8606 hubd_wait_for_hotplug_exit(hubd_t *hubd)
8625 8607 {
8626 8608 clock_t until = drv_usectohz(1000000);
8627 8609 int rval;
8628 8610
8629 8611 ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
8630 8612
8631 8613 if (hubd->h_hotplug_thread) {
8632 8614 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
8633 8615 "waiting for hubd hotplug thread exit");
8634 8616 rval = cv_reltimedwait(&hubd->h_cv_hotplug_dev,
8635 8617 &hubd->h_mutex, until, TR_CLOCK_TICK);
8636 8618
8637 8619 if ((rval <= 0) && (hubd->h_hotplug_thread)) {
8638 8620
8639 8621 return (USB_FAILURE);
8640 8622 }
8641 8623 }
8642 8624
8643 8625 return (USB_SUCCESS);
8644 8626 }
8645 8627
8646 8628 /*
8647 8629 * hubd_reset_thread:
8648 8630 * handles the "USB_RESET_LVL_REATTACH" reset of usb device.
8649 8631 *
8650 8632 * - delete the child (force detaching the device and its children)
8651 8633 * - reset the corresponding parent hub port
8652 8634 * - create the child (force re-attaching the device and its children)
8653 8635 */
8654 8636 static void
8655 8637 hubd_reset_thread(void *arg)
8656 8638 {
8657 8639 hubd_reset_arg_t *hd_arg = (hubd_reset_arg_t *)arg;
8658 8640 hubd_t *hubd = hd_arg->hubd;
8659 8641 uint16_t reset_port = hd_arg->reset_port;
8660 8642 uint16_t status, change;
8661 8643 hub_power_t *hubpm;
8662 8644 dev_info_t *hdip = hubd->h_dip;
8663 8645 dev_info_t *rh_dip = hubd->h_usba_device->usb_root_hub_dip;
8664 8646 dev_info_t *child_dip;
8665 8647 boolean_t online_child = B_FALSE;
8666 8648 int prh_circ, rh_circ, circ, devinst;
8667 8649 char *devname;
8668 8650 int i = 0;
8669 8651 int rval = USB_FAILURE;
8670 8652
8671 8653 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
8672 8654 "hubd_reset_thread: started, hubd_reset_port = 0x%x", reset_port);
8673 8655
8674 8656 kmem_free(arg, sizeof (hubd_reset_arg_t));
8675 8657
8676 8658 mutex_enter(HUBD_MUTEX(hubd));
8677 8659
8678 8660 child_dip = hubd->h_children_dips[reset_port];
8679 8661 ASSERT(child_dip != NULL);
8680 8662
8681 8663 devname = (char *)ddi_driver_name(child_dip);
8682 8664 devinst = ddi_get_instance(child_dip);
8683 8665
8684 8666 /* if our bus power entry point is active, quit the reset */
8685 8667 if (hubd->h_bus_pwr) {
8686 8668 USB_DPRINTF_L0(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
8687 8669 "%s%d is under bus power management, cannot be reset. "
8688 8670 "Please disconnect and reconnect this device.",
8689 8671 devname, devinst);
8690 8672
8691 8673 goto Fail;
8692 8674 }
8693 8675
8694 8676 if (hubd_wait_for_hotplug_exit(hubd) == USB_FAILURE) {
8695 8677 /* we got woken up because of a timeout */
8696 8678 USB_DPRINTF_L0(DPRINT_MASK_HOTPLUG,
8697 8679 hubd->h_log_handle, "Time out when resetting the device"
8698 8680 " %s%d. Please disconnect and reconnect this device.",
8699 8681 devname, devinst);
8700 8682
8701 8683 goto Fail;
8702 8684 }
8703 8685
8704 8686 hubd->h_hotplug_thread++;
8705 8687
8706 8688 /* is this the root hub? */
8707 8689 if ((hdip == rh_dip) &&
8708 8690 (hubd->h_dev_state == USB_DEV_PWRED_DOWN)) {
8709 8691 hubpm = hubd->h_hubpm;
8710 8692
8711 8693 /* mark the root hub as full power */
8712 8694 hubpm->hubp_current_power = USB_DEV_OS_FULL_PWR;
8713 8695 hubpm->hubp_time_at_full_power = gethrtime();
8714 8696 mutex_exit(HUBD_MUTEX(hubd));
8715 8697
8716 8698 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
8717 8699 "hubd_reset_thread: call pm_power_has_changed");
8718 8700
8719 8701 (void) pm_power_has_changed(hdip, 0,
8720 8702 USB_DEV_OS_FULL_PWR);
8721 8703
8722 8704 mutex_enter(HUBD_MUTEX(hubd));
8723 8705 hubd->h_dev_state = USB_DEV_ONLINE;
8724 8706 }
8725 8707
8726 8708 mutex_exit(HUBD_MUTEX(hubd));
8727 8709
8728 8710 /*
8729 8711 * this ensures one reset activity per system at a time.
8730 8712 * we enter the parent PCI node to have this serialization.
8731 8713 * this also excludes ioctls and deathrow thread
8732 8714 */
8733 8715 ndi_devi_enter(ddi_get_parent(rh_dip), &prh_circ);
8734 8716 ndi_devi_enter(rh_dip, &rh_circ);
8735 8717
8736 8718 /* exclude other threads */
8737 8719 ndi_devi_enter(hdip, &circ);
8738 8720 mutex_enter(HUBD_MUTEX(hubd));
8739 8721
8740 8722 /*
8741 8723 * We need to make sure that the child is still online for a hotplug
8742 8724 * thread could have inserted which detached the child.
8743 8725 */
8744 8726 if (hubd->h_children_dips[reset_port]) {
8745 8727 mutex_exit(HUBD_MUTEX(hubd));
8746 8728 /* First disconnect the device */
8747 8729 hubd_post_event(hubd, reset_port, USBA_EVENT_TAG_HOT_REMOVAL);
8748 8730
8749 8731 /* delete cached dv_node's but drop locks first */
8750 8732 ndi_devi_exit(hdip, circ);
8751 8733 ndi_devi_exit(rh_dip, rh_circ);
8752 8734 ndi_devi_exit(ddi_get_parent(rh_dip), prh_circ);
8753 8735
8754 8736 (void) devfs_clean(rh_dip, NULL, DV_CLEAN_FORCE);
8755 8737
8756 8738 /*
8757 8739 * workaround only for storage device. When it's able to force
8758 8740 * detach a driver, this code can be removed safely.
8759 8741 *
8760 8742 * If we're to reset storage device and the device is used, we
8761 8743 * will wait at most extra 20s for applications to exit and
8762 8744 * close the device. This is especially useful for HAL-based
8763 8745 * applications.
8764 8746 */
8765 8747 if ((strcmp(devname, "scsa2usb") == 0) &&
8766 8748 DEVI(child_dip)->devi_ref != 0) {
8767 8749 while (i++ < hubdi_reset_delay) {
8768 8750 mutex_enter(HUBD_MUTEX(hubd));
8769 8751 rval = hubd_delete_child(hubd, reset_port,
8770 8752 NDI_DEVI_REMOVE, B_FALSE);
8771 8753 mutex_exit(HUBD_MUTEX(hubd));
8772 8754 if (rval == USB_SUCCESS)
8773 8755 break;
8774 8756
8775 8757 delay(drv_usectohz(1000000)); /* 1s */
8776 8758 }
8777 8759 }
8778 8760
8779 8761 ndi_devi_enter(ddi_get_parent(rh_dip), &prh_circ);
8780 8762 ndi_devi_enter(rh_dip, &rh_circ);
8781 8763 ndi_devi_enter(hdip, &circ);
8782 8764
8783 8765 mutex_enter(HUBD_MUTEX(hubd));
8784 8766
8785 8767 /* Then force detaching the device */
8786 8768 if ((rval != USB_SUCCESS) && (hubd_delete_child(hubd,
8787 8769 reset_port, NDI_DEVI_REMOVE, B_FALSE) != USB_SUCCESS)) {
8788 8770 USB_DPRINTF_L0(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
8789 8771 "%s%d cannot be reset due to other applications "
8790 8772 "are using it, please first close these "
8791 8773 "applications, then disconnect and reconnect"
8792 8774 "the device.", devname, devinst);
8793 8775
8794 8776 mutex_exit(HUBD_MUTEX(hubd));
8795 8777 /* post a re-connect event */
8796 8778 hubd_post_event(hubd, reset_port,
8797 8779 USBA_EVENT_TAG_HOT_INSERTION);
8798 8780 mutex_enter(HUBD_MUTEX(hubd));
8799 8781 } else {
8800 8782 (void) hubd_determine_port_status(hubd, reset_port,
8801 8783 &status, &change, HUBD_ACK_ALL_CHANGES);
8802 8784
8803 8785 /* Reset the parent hubd port and create new child */
8804 8786 if (status & PORT_STATUS_CCS) {
8805 8787 online_child |= (hubd_handle_port_connect(hubd,
8806 8788 reset_port) == USB_SUCCESS);
8807 8789 }
8808 8790 }
8809 8791 }
8810 8792
8811 8793 /* release locks so we can do a devfs_clean */
8812 8794 mutex_exit(HUBD_MUTEX(hubd));
8813 8795
8814 8796 /* delete cached dv_node's but drop locks first */
8815 8797 ndi_devi_exit(hdip, circ);
8816 8798 ndi_devi_exit(rh_dip, rh_circ);
8817 8799 ndi_devi_exit(ddi_get_parent(rh_dip), prh_circ);
8818 8800
8819 8801 (void) devfs_clean(rh_dip, NULL, 0);
8820 8802
8821 8803 /* now check if any children need onlining */
8822 8804 if (online_child) {
8823 8805 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
8824 8806 "hubd_reset_thread: onlining children");
8825 8807
8826 8808 (void) ndi_devi_online(hubd->h_dip, 0);
8827 8809 }
8828 8810
8829 8811 mutex_enter(HUBD_MUTEX(hubd));
8830 8812
8831 8813 /* allow hotplug thread now */
8832 8814 hubd->h_hotplug_thread--;
8833 8815 Fail:
8834 8816 hubd_start_polling(hubd, 0);
8835 8817
8836 8818 /* mark this device as idle */
8837 8819 (void) hubd_pm_idle_component(hubd, hubd->h_dip, 0);
8838 8820
8839 8821 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
8840 8822 "hubd_reset_thread: exit, %d", hubd->h_hotplug_thread);
8841 8823
8842 8824 hubd->h_reset_port[reset_port] = B_FALSE;
8843 8825
8844 8826 mutex_exit(HUBD_MUTEX(hubd));
8845 8827
8846 8828 ndi_rele_devi(hdip);
8847 8829 }
8848 8830
8849 8831 /*
8850 8832 * hubd_check_same_device:
8851 8833 * - open the default pipe of the device.
8852 8834 * - compare the old and new descriptors of the device.
8853 8835 * - close the default pipe.
8854 8836 */
8855 8837 static int
8856 8838 hubd_check_same_device(hubd_t *hubd, usb_port_t port)
8857 8839 {
8858 8840 dev_info_t *dip = hubd->h_children_dips[port];
8859 8841 usb_pipe_handle_t ph;
8860 8842 int rval = USB_FAILURE;
8861 8843
8862 8844 ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
8863 8845
8864 8846 mutex_exit(HUBD_MUTEX(hubd));
8865 8847 /* Open the default pipe to operate the device */
8866 8848 if (usb_pipe_open(dip, NULL, NULL,
8867 8849 USB_FLAGS_SLEEP| USBA_FLAGS_PRIVILEGED,
8868 8850 &ph) == USB_SUCCESS) {
8869 8851 /*
8870 8852 * Check that if the device's descriptors are different
8871 8853 * from the values saved before the port reset.
8872 8854 */
8873 8855 rval = usb_check_same_device(dip,
8874 8856 hubd->h_log_handle, USB_LOG_L0,
8875 8857 DPRINT_MASK_ALL, USB_CHK_ALL, NULL);
8876 8858
8877 8859 usb_pipe_close(dip, ph, USB_FLAGS_SLEEP |
8878 8860 USBA_FLAGS_PRIVILEGED, NULL, NULL);
8879 8861 }
8880 8862 mutex_enter(HUBD_MUTEX(hubd));
8881 8863
8882 8864 return (rval);
8883 8865 }
8884 8866
8885 8867 /*
8886 8868 * usba_hubdi_reset_device
8887 8869 * Called by usb_reset_device to handle usb device reset.
8888 8870 */
8889 8871 int
8890 8872 usba_hubdi_reset_device(dev_info_t *dip, usb_dev_reset_lvl_t reset_level)
8891 8873 {
8892 8874 hubd_t *hubd;
8893 8875 usb_port_t port = 0;
8894 8876 dev_info_t *hdip;
8895 8877 usb_pipe_state_t prev_pipe_state = 0;
8896 8878 usba_device_t *usba_device;
8897 8879 hubd_reset_arg_t *arg;
8898 8880 int i, ph_open_cnt;
8899 8881 int rval = USB_FAILURE;
8900 8882
8901 8883 if ((!dip) || usba_is_root_hub(dip)) {
8902 8884 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubdi_log_handle,
8903 8885 "usba_hubdi_reset_device: NULL dip or root hub");
8904 8886
8905 8887 return (USB_INVALID_ARGS);
8906 8888 }
8907 8889
8908 8890 if (!usb_owns_device(dip)) {
8909 8891 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubdi_log_handle,
8910 8892 "usba_hubdi_reset_device: Not owns the device");
8911 8893
8912 8894 return (USB_INVALID_PERM);
8913 8895 }
8914 8896
8915 8897 if ((reset_level != USB_RESET_LVL_REATTACH) &&
8916 8898 (reset_level != USB_RESET_LVL_DEFAULT)) {
8917 8899 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubdi_log_handle,
8918 8900 "usba_hubdi_reset_device: Unknown flags");
8919 8901
8920 8902 return (USB_INVALID_ARGS);
8921 8903 }
8922 8904
8923 8905 if ((hdip = ddi_get_parent(dip)) == NULL) {
8924 8906 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubdi_log_handle,
8925 8907 "usba_hubdi_reset_device: fail to get parent hub");
8926 8908
8927 8909 return (USB_INVALID_ARGS);
8928 8910 }
8929 8911
8930 8912 if ((hubd = hubd_get_soft_state(hdip)) == NULL) {
8931 8913 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubdi_log_handle,
8932 8914 "usba_hubdi_reset_device: fail to get hub softstate");
8933 8915
8934 8916 return (USB_INVALID_ARGS);
8935 8917 }
8936 8918
8937 8919 mutex_enter(HUBD_MUTEX(hubd));
8938 8920
8939 8921 /* make sure the hub is connected before trying any kinds of reset. */
8940 8922 if ((hubd->h_dev_state == USB_DEV_DISCONNECTED) ||
8941 8923 (hubd->h_dev_state == USB_DEV_SUSPENDED)) {
8942 8924 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
8943 8925 "usb_reset_device: the state %d of the hub/roothub "
8944 8926 "associated to the device 0x%p is incorrect",
8945 8927 hubd->h_dev_state, (void *)dip);
8946 8928 mutex_exit(HUBD_MUTEX(hubd));
8947 8929
8948 8930 return (USB_INVALID_ARGS);
8949 8931 }
8950 8932
8951 8933 mutex_exit(HUBD_MUTEX(hubd));
8952 8934
8953 8935 port = hubd_child_dip2port(hubd, dip);
8954 8936
8955 8937 mutex_enter(HUBD_MUTEX(hubd));
8956 8938
8957 8939 if (hubd->h_reset_port[port]) {
8958 8940 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
8959 8941 "usb_reset_device: the corresponding port is resetting");
8960 8942 mutex_exit(HUBD_MUTEX(hubd));
8961 8943
8962 8944 return (USB_SUCCESS);
8963 8945 }
8964 8946
8965 8947 /*
8966 8948 * For Default reset, client drivers should first close all the pipes
8967 8949 * except default pipe before calling the function, also should not
8968 8950 * call the function during interrupt context.
8969 8951 */
8970 8952 if (reset_level == USB_RESET_LVL_DEFAULT) {
8971 8953 usba_device = hubd->h_usba_devices[port];
8972 8954 mutex_exit(HUBD_MUTEX(hubd));
8973 8955
8974 8956 if (servicing_interrupt()) {
8975 8957 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
8976 8958 "usb_reset_device: during interrput context, quit");
8977 8959
8978 8960 return (USB_INVALID_CONTEXT);
8979 8961 }
8980 8962 /* Check if all the pipes have been closed */
8981 8963 for (ph_open_cnt = 0, i = 1; i < USBA_N_ENDPOINTS; i++) {
8982 8964 if (usba_device->usb_ph_list[i].usba_ph_data) {
8983 8965 ph_open_cnt++;
8984 8966 break;
8985 8967 }
8986 8968 }
8987 8969 if (ph_open_cnt) {
8988 8970 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
8989 8971 "usb_reset_device: %d pipes are still open",
8990 8972 ph_open_cnt);
8991 8973
8992 8974 return (USB_BUSY);
8993 8975 }
8994 8976 mutex_enter(HUBD_MUTEX(hubd));
8995 8977 }
8996 8978
8997 8979 /* Don't perform reset while the device is detaching */
8998 8980 if (hubd->h_port_state[port] & HUBD_CHILD_DETACHING) {
8999 8981 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
9000 8982 "usb_reset_device: the device is detaching, "
9001 8983 "cannot be reset");
9002 8984 mutex_exit(HUBD_MUTEX(hubd));
9003 8985
9004 8986 return (USB_FAILURE);
9005 8987 }
9006 8988
9007 8989 hubd->h_reset_port[port] = B_TRUE;
9008 8990 hdip = hubd->h_dip;
9009 8991 mutex_exit(HUBD_MUTEX(hubd));
9010 8992
9011 8993 /* Don't allow hub detached during the reset */
9012 8994 ndi_hold_devi(hdip);
9013 8995
9014 8996 mutex_enter(HUBD_MUTEX(hubd));
9015 8997 hubd_pm_busy_component(hubd, hdip, 0);
9016 8998 mutex_exit(HUBD_MUTEX(hubd));
9017 8999 /* go full power */
9018 9000 (void) pm_raise_power(hdip, 0, USB_DEV_OS_FULL_PWR);
9019 9001 mutex_enter(HUBD_MUTEX(hubd));
9020 9002
9021 9003 hubd->h_hotplug_thread++;
9022 9004
9023 9005 /* stop polling if it was active */
9024 9006 if (hubd->h_ep1_ph) {
9025 9007 mutex_exit(HUBD_MUTEX(hubd));
9026 9008 (void) usb_pipe_get_state(hubd->h_ep1_ph, &prev_pipe_state,
9027 9009 USB_FLAGS_SLEEP);
9028 9010 mutex_enter(HUBD_MUTEX(hubd));
9029 9011
9030 9012 if (prev_pipe_state == USB_PIPE_STATE_ACTIVE) {
9031 9013 hubd_stop_polling(hubd);
9032 9014 }
9033 9015 }
9034 9016
9035 9017 switch (reset_level) {
9036 9018 case USB_RESET_LVL_REATTACH:
9037 9019 mutex_exit(HUBD_MUTEX(hubd));
9038 9020 arg = (hubd_reset_arg_t *)kmem_zalloc(
9039 9021 sizeof (hubd_reset_arg_t), KM_SLEEP);
9040 9022 arg->hubd = hubd;
9041 9023 arg->reset_port = port;
9042 9024 mutex_enter(HUBD_MUTEX(hubd));
9043 9025
9044 9026 if ((rval = usb_async_req(hdip, hubd_reset_thread,
9045 9027 (void *)arg, 0)) == USB_SUCCESS) {
9046 9028 hubd->h_hotplug_thread--;
9047 9029 mutex_exit(HUBD_MUTEX(hubd));
9048 9030
9049 9031 return (USB_SUCCESS);
9050 9032 } else {
9051 9033 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
9052 9034 "Cannot create reset thread, the device %s%d failed"
9053 9035 " to reset", ddi_driver_name(dip),
9054 9036 ddi_get_instance(dip));
9055 9037
9056 9038 kmem_free(arg, sizeof (hubd_reset_arg_t));
9057 9039 }
9058 9040
9059 9041 break;
9060 9042 case USB_RESET_LVL_DEFAULT:
9061 9043 /*
9062 9044 * Reset hub port and then recover device's address, set back
9063 9045 * device's configuration, hubd_handle_port_connect() will
9064 9046 * handle errors happened during this process.
9065 9047 */
9066 9048 if ((rval = hubd_handle_port_connect(hubd, port))
9067 9049 == USB_SUCCESS) {
9068 9050 mutex_exit(HUBD_MUTEX(hubd));
9069 9051 /* re-open the default pipe */
9070 9052 rval = usba_persistent_pipe_open(usba_device);
9071 9053 mutex_enter(HUBD_MUTEX(hubd));
9072 9054 if (rval != USB_SUCCESS) {
9073 9055 USB_DPRINTF_L2(DPRINT_MASK_ATTA,
9074 9056 hubd->h_log_handle, "failed to reopen "
9075 9057 "default pipe after reset, disable hub"
9076 9058 "port for %s%d", ddi_driver_name(dip),
9077 9059 ddi_get_instance(dip));
9078 9060 /*
9079 9061 * Disable port to set out a hotplug thread
9080 9062 * which will handle errors.
9081 9063 */
9082 9064 (void) hubd_disable_port(hubd, port);
9083 9065 }
9084 9066 }
9085 9067
9086 9068 break;
9087 9069 default:
9088 9070
9089 9071 break;
9090 9072 }
9091 9073
9092 9074 /* allow hotplug thread now */
9093 9075 hubd->h_hotplug_thread--;
9094 9076
9095 9077 if ((hubd->h_dev_state == USB_DEV_ONLINE) && hubd->h_ep1_ph &&
9096 9078 (prev_pipe_state == USB_PIPE_STATE_ACTIVE)) {
9097 9079 hubd_start_polling(hubd, 0);
9098 9080 }
9099 9081
9100 9082 hubd_pm_idle_component(hubd, hdip, 0);
9101 9083
9102 9084 /* Clear reset mark for the port. */
9103 9085 hubd->h_reset_port[port] = B_FALSE;
9104 9086
9105 9087 mutex_exit(HUBD_MUTEX(hubd));
9106 9088
9107 9089 ndi_rele_devi(hdip);
9108 9090
9109 9091 return (rval);
9110 9092 }
↓ open down ↓ |
2619 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX