Print this page
7127 remove -Wno-missing-braces from Makefile.uts
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/common/io/usb/usba/usba.c
+++ new/usr/src/uts/common/io/usb/usba/usba.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 2009 Sun Microsystems, Inc. All rights reserved.
23 23 * Use is subject to license terms.
24 24 *
25 25 * Copyright 2014 Garrett D'Amore <garrett@damore.org>
26 26 */
27 27
28 28
29 29 /*
30 30 * USBA: Solaris USB Architecture support
31 31 */
32 32 #define USBA_FRAMEWORK
33 33 #include <sys/usb/usba/usba_impl.h>
34 34 #include <sys/usb/usba/hcdi_impl.h>
35 35 #include <sys/usb/hubd/hub.h>
36 36 #include <sys/fs/dv_node.h>
37 37
38 38 static int usba_str_startcmp(char *, char *);
39 39
40 40 /*
41 41 * USBA private variables and tunables
42 42 */
43 43 static kmutex_t usba_mutex;
44 44
45 45 /* mutex to protect usba_root_hubs */
46 46 static kmutex_t usba_hub_mutex;
47 47
48 48 typedef struct usba_root_hub_ent {
49 49 dev_info_t *dip;
50 50 struct usba_root_hub_ent *next;
51 51 }usba_root_hub_ent_t;
52 52
53 53 static usba_root_hub_ent_t *usba_root_hubs = NULL;
54 54
55 55 /*
56 56 * ddivs forced binding:
57 57 *
58 58 * usbc usbc_xhubs usbc_xaddress node name
59 59 *
60 60 * 0 x x class name or "device"
61 61 *
62 62 * 1 0 0 ddivs_usbc
63 63 * 1 0 >1 ddivs_usbc except device
64 64 * at usbc_xaddress
65 65 * 1 1 0 ddivs_usbc except hubs
66 66 * 1 1 >1 ddivs_usbc except hubs and
67 67 * device at usbc_xaddress
68 68 */
69 69 uint_t usba_ddivs_usbc;
70 70 uint_t usba_ddivs_usbc_xhubs;
71 71 uint_t usba_ddivs_usbc_xaddress;
72 72
73 73 uint_t usba_ugen_force_binding;
74 74
75 75 /*
76 76 * compatible name handling
77 77 */
78 78 /*
79 79 * allowing for 15 compat names, plus one force bind name and
80 80 * one possible specified client driver name
81 81 */
82 82 #define USBA_MAX_COMPAT_NAMES 17
83 83 #define USBA_MAX_COMPAT_NAME_LEN 64
84 84
85 85 /* double linked list for usba_devices */
86 86 usba_list_entry_t usba_device_list;
87 87
88 88 _NOTE(MUTEX_PROTECTS_DATA(usba_mutex, usba_device_list))
89 89
↓ open down ↓ |
89 lines elided |
↑ open up ↑ |
90 90 /*
91 91 * modload support
92 92 */
93 93
94 94 static struct modlmisc modlmisc = {
95 95 &mod_miscops, /* Type of module */
96 96 "USBA: USB Architecture 2.0 1.66"
97 97 };
98 98
99 99 static struct modlinkage modlinkage = {
100 - MODREV_1, (void *)&modlmisc, NULL
100 + MODREV_1, { (void *)&modlmisc, NULL }
101 101 };
102 102
103 103
104 104 static usb_log_handle_t usba_log_handle;
105 105 uint_t usba_errlevel = USB_LOG_L4;
106 106 uint_t usba_errmask = (uint_t)-1;
107 107
108 108 extern usb_log_handle_t hubdi_log_handle;
109 109
110 110 int
111 111 _init(void)
112 112 {
113 113 int rval;
114 114
115 115 /*
116 116 * usbai providing log support needs to be init'ed first
117 117 * and destroyed last
118 118 */
119 119 usba_usbai_initialization();
120 120 usba_usba_initialization();
121 121 usba_usbai_register_initialization();
122 122 usba_hcdi_initialization();
123 123 usba_hubdi_initialization();
124 124 usba_devdb_initialization();
125 125
126 126 if ((rval = mod_install(&modlinkage)) != 0) {
127 127 usba_devdb_destroy();
128 128 usba_hubdi_destroy();
129 129 usba_hcdi_destroy();
130 130 usba_usbai_register_destroy();
131 131 usba_usba_destroy();
132 132 usba_usbai_destroy();
133 133 }
134 134
135 135 return (rval);
136 136 }
137 137
138 138 int
139 139 _fini()
140 140 {
141 141 int rval;
142 142
143 143 if ((rval = mod_remove(&modlinkage)) == 0) {
144 144 usba_devdb_destroy();
145 145 usba_hubdi_destroy();
146 146 usba_hcdi_destroy();
147 147 usba_usbai_register_destroy();
148 148 usba_usba_destroy();
149 149 usba_usbai_destroy();
150 150 }
151 151
152 152 return (rval);
153 153 }
154 154
155 155 int
156 156 _info(struct modinfo *modinfop)
157 157 {
158 158 return (mod_info(&modlinkage, modinfop));
159 159 }
160 160
161 161 boolean_t
162 162 usba_owns_ia(dev_info_t *dip)
163 163 {
164 164 int if_count = ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
165 165 "interface-count", 0);
166 166
167 167 return ((if_count) ? B_TRUE : B_FALSE);
168 168 }
169 169
170 170 /*
171 171 * common bus ctl for hcd, usb_mid, and hubd
172 172 */
173 173 int
174 174 usba_bus_ctl(dev_info_t *dip,
175 175 dev_info_t *rdip,
176 176 ddi_ctl_enum_t op,
177 177 void *arg,
178 178 void *result)
179 179 {
180 180 dev_info_t *child_dip = (dev_info_t *)arg;
181 181 usba_device_t *usba_device;
182 182 usba_hcdi_t *usba_hcdi;
183 183 usba_hcdi_ops_t *usba_hcdi_ops;
184 184
185 185 USB_DPRINTF_L4(DPRINT_MASK_USBA, hubdi_log_handle,
186 186 "usba_bus_ctl: %s%d %s%d op=%d", ddi_node_name(rdip),
187 187 ddi_get_instance(rdip), ddi_node_name(dip),
188 188 ddi_get_instance(dip), op);
189 189
190 190 switch (op) {
191 191
192 192 case DDI_CTLOPS_REPORTDEV:
193 193 {
194 194 char *name, compat_name[64], *speed;
195 195 usba_device_t *hub_usba_device;
196 196 dev_info_t *hubdip;
197 197
198 198 usba_device = usba_get_usba_device(rdip);
199 199
200 200 /* find the parent hub */
201 201 hubdip = ddi_get_parent(rdip);
202 202 while ((strcmp(ddi_driver_name(hubdip), "hubd") != 0) &&
203 203 !(usba_is_root_hub(hubdip))) {
204 204 hubdip = ddi_get_parent(hubdip);
205 205 }
206 206
207 207 hub_usba_device = usba_get_usba_device(hubdip);
208 208
209 209 if (usba_device) {
210 210 if (usb_owns_device(rdip)) {
211 211 (void) snprintf(compat_name,
212 212 sizeof (compat_name),
213 213 "usb%x,%x",
214 214 usba_device->usb_dev_descr->idVendor,
215 215 usba_device->usb_dev_descr->idProduct);
216 216 } else if (usba_owns_ia(rdip)) {
217 217 (void) snprintf(compat_name,
218 218 sizeof (compat_name),
219 219 "usbia%x,%x.config%x.%x",
220 220 usba_device->usb_dev_descr->idVendor,
221 221 usba_device->usb_dev_descr->idProduct,
222 222 usba_device->usb_cfg_value,
223 223 usb_get_if_number(rdip));
224 224 } else {
225 225 (void) snprintf(compat_name,
226 226 sizeof (compat_name),
227 227 "usbif%x,%x.config%x.%x",
228 228 usba_device->usb_dev_descr->idVendor,
229 229 usba_device->usb_dev_descr->idProduct,
230 230 usba_device->usb_cfg_value,
231 231 usb_get_if_number(rdip));
232 232 }
233 233 switch (usba_device->usb_port_status) {
234 234 case USBA_HIGH_SPEED_DEV:
235 235 speed = "hi speed (USB 2.x)";
236 236
237 237 break;
238 238 case USBA_LOW_SPEED_DEV:
239 239 speed = "low speed (USB 1.x)";
240 240
241 241 break;
242 242 case USBA_FULL_SPEED_DEV:
243 243 default:
244 244 speed = "full speed (USB 1.x)";
245 245
246 246 break;
247 247 }
248 248
249 249 cmn_err(CE_CONT,
250 250 "?USB %x.%x %s (%s) operating at %s on "
251 251 "USB %x.%x %s hub: "
252 252 "%s@%s, %s%d at bus address %d\n",
253 253 (usba_device->usb_dev_descr->bcdUSB & 0xff00) >> 8,
254 254 usba_device->usb_dev_descr->bcdUSB & 0xff,
255 255 (usb_owns_device(rdip) ? "device" :
256 256 ((usba_owns_ia(rdip) ? "interface-association" :
257 257 "interface"))),
258 258 compat_name, speed,
259 259 (hub_usba_device->usb_dev_descr->bcdUSB &
260 260 0xff00) >> 8,
261 261 hub_usba_device->usb_dev_descr->bcdUSB & 0xff,
262 262 usba_is_root_hub(hubdip) ? "root" : "external",
263 263 ddi_node_name(rdip), ddi_get_name_addr(rdip),
264 264 ddi_driver_name(rdip),
265 265 ddi_get_instance(rdip), usba_device->usb_addr);
266 266
267 267 name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
268 268 (void) usba_get_mfg_prod_sn_str(rdip, name, MAXNAMELEN);
269 269 if (name[0] != '\0') {
270 270 cmn_err(CE_CONT, "?\t%s\n", name);
271 271 }
272 272 kmem_free(name, MAXNAMELEN);
273 273
274 274 } else { /* harden USBA against this case; if it happens */
275 275
276 276 cmn_err(CE_CONT,
277 277 "?USB-device: %s@%s, %s%d\n",
278 278 ddi_node_name(rdip), ddi_get_name_addr(rdip),
279 279 ddi_driver_name(rdip), ddi_get_instance(rdip));
280 280 }
281 281
282 282 return (DDI_SUCCESS);
283 283 }
284 284
285 285 case DDI_CTLOPS_INITCHILD:
286 286 {
287 287 int usb_addr;
288 288 uint_t n;
289 289 char name[32];
290 290 int *data;
291 291 int rval;
292 292 int len = sizeof (usb_addr);
293 293
294 294 usba_hcdi = usba_hcdi_get_hcdi(dip);
295 295 usba_hcdi_ops = usba_hcdi->hcdi_ops;
296 296 ASSERT(usba_hcdi_ops != NULL);
297 297
298 298 /*
299 299 * as long as the dip exists, it should have
300 300 * usba_device structure associated with it
301 301 */
302 302 usba_device = usba_get_usba_device(child_dip);
303 303 if (usba_device == NULL) {
304 304
305 305 USB_DPRINTF_L2(DPRINT_MASK_USBA, hubdi_log_handle,
306 306 "usba_bus_ctl: DDI_NOT_WELL_FORMED (%s (0x%p))",
307 307 ddi_node_name(child_dip), (void *)child_dip);
308 308
309 309 return (DDI_NOT_WELL_FORMED);
310 310 }
311 311
312 312 /* the dip should have an address and reg property */
313 313 if (ddi_prop_op(DDI_DEV_T_NONE, child_dip, PROP_LEN_AND_VAL_BUF,
314 314 DDI_PROP_DONTPASS | DDI_PROP_CANSLEEP, "assigned-address",
315 315 (caddr_t)&usb_addr, &len) != DDI_SUCCESS) {
316 316
317 317 USB_DPRINTF_L2(DPRINT_MASK_USBA, hubdi_log_handle,
318 318 "usba_bus_ctl:\n\t"
319 319 "%s%d %s%d op=%d rdip = 0x%p dip = 0x%p",
320 320 ddi_node_name(rdip), ddi_get_instance(rdip),
321 321 ddi_node_name(dip), ddi_get_instance(dip), op,
322 322 (void *)rdip, (void *)dip);
323 323
324 324 USB_DPRINTF_L2(DPRINT_MASK_USBA, hubdi_log_handle,
325 325 "usba_bus_ctl: DDI_NOT_WELL_FORMED (%s (0x%p))",
326 326 ddi_node_name(child_dip), (void *)child_dip);
327 327
328 328 return (DDI_NOT_WELL_FORMED);
329 329 }
330 330
331 331 if ((rval = ddi_prop_lookup_int_array(DDI_DEV_T_ANY, child_dip,
332 332 DDI_PROP_DONTPASS, "reg",
333 333 &data, &n)) != DDI_SUCCESS) {
334 334
335 335 USB_DPRINTF_L2(DPRINT_MASK_USBA, hubdi_log_handle,
336 336 "usba_bus_ctl: %d, DDI_NOT_WELL_FORMED", rval);
337 337
338 338 return (DDI_NOT_WELL_FORMED);
339 339 }
340 340
341 341
342 342 /*
343 343 * if the configuration is 1, the unit address is
344 344 * just the interface number
345 345 */
346 346 if ((n == 1) || ((n > 1) && (data[1] == 1))) {
347 347 (void) sprintf(name, "%x", data[0]);
348 348 } else {
349 349 (void) sprintf(name, "%x,%x", data[0], data[1]);
350 350 }
351 351
352 352 USB_DPRINTF_L3(DPRINT_MASK_USBA,
353 353 hubdi_log_handle, "usba_bus_ctl: name = %s", name);
354 354
355 355 ddi_prop_free(data);
356 356 ddi_set_name_addr(child_dip, name);
357 357
358 358 /*
359 359 * increment the reference count for each child using this
360 360 * usba_device structure
361 361 */
362 362 mutex_enter(&usba_device->usb_mutex);
363 363 usba_device->usb_ref_count++;
364 364
365 365 USB_DPRINTF_L3(DPRINT_MASK_USBA, hubdi_log_handle,
366 366 "usba_bus_ctl: init usba_device = 0x%p ref_count = %d",
367 367 (void *)usba_device, usba_device->usb_ref_count);
368 368
369 369 mutex_exit(&usba_device->usb_mutex);
370 370
371 371 return (DDI_SUCCESS);
372 372 }
373 373
374 374 case DDI_CTLOPS_UNINITCHILD:
375 375 {
376 376 usba_device = usba_get_usba_device(child_dip);
377 377
378 378 if (usba_device != NULL) {
379 379 /*
380 380 * decrement the reference count for each child
381 381 * using this usba_device structure
382 382 */
383 383 mutex_enter(&usba_device->usb_mutex);
384 384 usba_device->usb_ref_count--;
385 385
386 386 USB_DPRINTF_L3(DPRINT_MASK_USBA, hubdi_log_handle,
387 387 "usba_hcdi_bus_ctl: uninit usba_device=0x%p "
388 388 "ref_count=%d",
389 389 (void *)usba_device, usba_device->usb_ref_count);
390 390
391 391 mutex_exit(&usba_device->usb_mutex);
392 392 }
393 393 ddi_set_name_addr(child_dip, NULL);
394 394
395 395 return (DDI_SUCCESS);
396 396 }
397 397
398 398 case DDI_CTLOPS_IOMIN:
399 399 /* Do nothing */
400 400 return (DDI_SUCCESS);
401 401
402 402 /*
403 403 * These ops correspond to functions that "shouldn't" be called
404 404 * by a USB client driver. So we whine when we're called.
405 405 */
406 406 case DDI_CTLOPS_DMAPMAPC:
407 407 case DDI_CTLOPS_REPORTINT:
408 408 case DDI_CTLOPS_REGSIZE:
409 409 case DDI_CTLOPS_NREGS:
410 410 case DDI_CTLOPS_SIDDEV:
411 411 case DDI_CTLOPS_SLAVEONLY:
412 412 case DDI_CTLOPS_AFFINITY:
413 413 case DDI_CTLOPS_POKE:
414 414 case DDI_CTLOPS_PEEK:
415 415 cmn_err(CE_CONT, "%s%d: invalid op (%d) from %s%d",
416 416 ddi_node_name(dip), ddi_get_instance(dip),
417 417 op, ddi_node_name(rdip), ddi_get_instance(rdip));
418 418 return (DDI_FAILURE);
419 419
420 420 /*
421 421 * Everything else (e.g. PTOB/BTOP/BTOPR requests) we pass up
422 422 */
423 423 default:
424 424 return (ddi_ctlops(dip, rdip, op, arg, result));
425 425 }
426 426 }
427 427
428 428
429 429 /*
430 430 * initialize and destroy USBA module
431 431 */
432 432 void
433 433 usba_usba_initialization()
434 434 {
435 435 usba_log_handle = usb_alloc_log_hdl(NULL, "usba", &usba_errlevel,
436 436 &usba_errmask, NULL, 0);
437 437
438 438 USB_DPRINTF_L4(DPRINT_MASK_USBA,
439 439 usba_log_handle, "usba_usba_initialization");
440 440
441 441 mutex_init(&usba_mutex, NULL, MUTEX_DRIVER, NULL);
442 442 mutex_init(&usba_hub_mutex, NULL, MUTEX_DRIVER, NULL);
443 443 usba_init_list(&usba_device_list, NULL, NULL);
444 444 }
445 445
446 446
447 447 void
448 448 usba_usba_destroy()
449 449 {
450 450 USB_DPRINTF_L4(DPRINT_MASK_USBA, usba_log_handle, "usba_usba_destroy");
451 451
452 452 mutex_destroy(&usba_hub_mutex);
453 453 mutex_destroy(&usba_mutex);
454 454 usba_destroy_list(&usba_device_list);
455 455
456 456 usb_free_log_hdl(usba_log_handle);
457 457 }
458 458
459 459
460 460 /*
461 461 * usba_set_usb_address:
462 462 * set usb address in usba_device structure
463 463 */
464 464 int
465 465 usba_set_usb_address(usba_device_t *usba_device)
466 466 {
467 467 usb_addr_t address;
468 468 uchar_t s = 8;
469 469 usba_hcdi_t *hcdi;
470 470 char *usb_address_in_use;
471 471
472 472 mutex_enter(&usba_device->usb_mutex);
473 473
474 474 hcdi = usba_hcdi_get_hcdi(usba_device->usb_root_hub_dip);
475 475
476 476 mutex_enter(&hcdi->hcdi_mutex);
477 477 usb_address_in_use = hcdi->hcdi_usb_address_in_use;
478 478
479 479 for (address = ROOT_HUB_ADDR + 1;
480 480 address <= USBA_MAX_ADDRESS; address++) {
481 481 if (usb_address_in_use[address/s] & (1 << (address % s))) {
482 482 continue;
483 483 }
484 484 usb_address_in_use[address/s] |= (1 << (address % s));
485 485 hcdi->hcdi_device_count++;
486 486 HCDI_HOTPLUG_STATS_DATA(hcdi)->hcdi_device_count.value.ui64++;
487 487 mutex_exit(&hcdi->hcdi_mutex);
488 488
489 489 USB_DPRINTF_L3(DPRINT_MASK_USBA, usba_log_handle,
490 490 "usba_set_usb_address: %d", address);
491 491
492 492 usba_device->usb_addr = address;
493 493
494 494 mutex_exit(&usba_device->usb_mutex);
495 495
496 496 return (USB_SUCCESS);
497 497 }
498 498
499 499 usba_device->usb_addr = 0;
500 500
501 501 USB_DPRINTF_L2(DPRINT_MASK_USBA, usba_log_handle,
502 502 "no usb address available");
503 503
504 504 mutex_exit(&hcdi->hcdi_mutex);
505 505 mutex_exit(&usba_device->usb_mutex);
506 506
507 507 return (USB_FAILURE);
508 508 }
509 509
510 510
511 511 /*
512 512 * usba_unset_usb_address:
513 513 * unset usb_address in usba_device structure
514 514 */
515 515 void
516 516 usba_unset_usb_address(usba_device_t *usba_device)
517 517 {
518 518 usb_addr_t address;
519 519 usba_hcdi_t *hcdi;
520 520 uchar_t s = 8;
521 521 char *usb_address_in_use;
522 522
523 523 mutex_enter(&usba_device->usb_mutex);
524 524 address = usba_device->usb_addr;
525 525 hcdi = usba_hcdi_get_hcdi(usba_device->usb_root_hub_dip);
526 526
527 527 if (address > ROOT_HUB_ADDR) {
528 528 USB_DPRINTF_L3(DPRINT_MASK_USBA, usba_log_handle,
529 529 "usba_unset_usb_address: address=%d", address);
530 530
531 531 mutex_enter(&hcdi->hcdi_mutex);
532 532 usb_address_in_use = hcdi->hcdi_usb_address_in_use;
533 533
534 534 ASSERT(usb_address_in_use[address/s] & (1 << (address % s)));
535 535
536 536 usb_address_in_use[address/s] &= ~(1 << (address % s));
537 537
538 538 hcdi->hcdi_device_count--;
539 539 HCDI_HOTPLUG_STATS_DATA(hcdi)->hcdi_device_count.value.ui64--;
540 540
541 541 mutex_exit(&hcdi->hcdi_mutex);
542 542
543 543 usba_device->usb_addr = 0;
544 544 }
545 545 mutex_exit(&usba_device->usb_mutex);
546 546 }
547 547
548 548
549 549 struct usba_evdata *
550 550 usba_get_evdata(dev_info_t *dip)
551 551 {
552 552 usba_evdata_t *evdata;
553 553 usba_device_t *usba_device = usba_get_usba_device(dip);
554 554
555 555 /* called when dip attaches */
556 556 ASSERT(usba_device != NULL);
557 557
558 558 mutex_enter(&usba_device->usb_mutex);
559 559 evdata = usba_device->usb_evdata;
560 560 while (evdata) {
561 561 if (evdata->ev_dip == dip) {
562 562 mutex_exit(&usba_device->usb_mutex);
563 563
564 564 return (evdata);
565 565 }
566 566 evdata = evdata->ev_next;
567 567 }
568 568
569 569 evdata = kmem_zalloc(sizeof (usba_evdata_t), KM_SLEEP);
570 570 evdata->ev_dip = dip;
571 571 evdata->ev_next = usba_device->usb_evdata;
572 572 usba_device->usb_evdata = evdata;
573 573 mutex_exit(&usba_device->usb_mutex);
574 574
575 575 return (evdata);
576 576 }
577 577
578 578
579 579 /*
580 580 * allocate a usb device structure and link it in the list
581 581 */
582 582 usba_device_t *
583 583 usba_alloc_usba_device(dev_info_t *root_hub_dip)
584 584 {
585 585 usba_device_t *usba_device;
586 586 int ep_idx;
587 587 ddi_iblock_cookie_t iblock_cookie =
588 588 usba_hcdi_get_hcdi(root_hub_dip)->hcdi_iblock_cookie;
589 589
590 590 /*
591 591 * create a new usba_device structure
592 592 */
593 593 usba_device = kmem_zalloc(sizeof (usba_device_t), KM_SLEEP);
594 594
595 595 /*
596 596 * initialize usba_device
597 597 */
598 598 mutex_init(&usba_device->usb_mutex, NULL, MUTEX_DRIVER,
599 599 iblock_cookie);
600 600
601 601 usba_init_list(&usba_device->usb_device_list, (usb_opaque_t)usba_device,
602 602 iblock_cookie);
603 603 usba_init_list(&usba_device->usb_allocated, (usb_opaque_t)usba_device,
604 604 iblock_cookie);
605 605 mutex_enter(&usba_device->usb_mutex);
606 606 usba_device->usb_root_hub_dip = root_hub_dip;
607 607
608 608 /*
609 609 * add to list of usba_devices
610 610 */
611 611 usba_add_to_list(&usba_device_list, &usba_device->usb_device_list);
612 612
613 613 /* init mutex in each usba_ph_impl structure */
614 614 for (ep_idx = 0; ep_idx < USBA_N_ENDPOINTS; ep_idx++) {
615 615 mutex_init(&usba_device->usb_ph_list[ep_idx].usba_ph_mutex,
616 616 NULL, MUTEX_DRIVER, iblock_cookie);
617 617 }
618 618
619 619 USB_DPRINTF_L2(DPRINT_MASK_USBA, usba_log_handle,
620 620 "allocated usba_device 0x%p", (void *)usba_device);
621 621
622 622 mutex_exit(&usba_device->usb_mutex);
623 623
624 624 return (usba_device);
625 625 }
626 626
627 627
628 628 /* free NDI event data associated with usba_device */
629 629 void
630 630 usba_free_evdata(usba_evdata_t *evdata)
631 631 {
632 632 usba_evdata_t *next;
633 633
634 634 while (evdata) {
635 635 next = evdata->ev_next;
636 636 kmem_free(evdata, sizeof (usba_evdata_t));
637 637 evdata = next;
638 638 }
639 639 }
640 640
641 641
642 642 /*
643 643 * free usb device structure
644 644 */
645 645 void
646 646 usba_free_usba_device(usba_device_t *usba_device)
647 647 {
648 648 int i, ep_idx;
649 649 usb_pipe_handle_t def_ph;
650 650
651 651 if (usba_device == NULL) {
652 652
653 653 return;
654 654 }
655 655
656 656 mutex_enter(&usba_device->usb_mutex);
657 657 if (usba_device->usb_ref_count) {
658 658 mutex_exit(&usba_device->usb_mutex);
659 659
660 660 return;
661 661 }
662 662
663 663 USB_DPRINTF_L3(DPRINT_MASK_USBA, usba_log_handle,
664 664 "usba_free_usba_device 0x%p, address=0x%x, ref cnt=%d",
665 665 (void *)usba_device, usba_device->usb_addr,
666 666 usba_device->usb_ref_count);
667 667
668 668 usba_free_evdata(usba_device->usb_evdata);
669 669 mutex_exit(&usba_device->usb_mutex);
670 670
671 671 def_ph = usba_usbdev_to_dflt_pipe_handle(usba_device);
672 672 if (def_ph != NULL) {
673 673 usba_pipe_handle_data_t *ph_data = usba_get_ph_data(def_ph);
674 674
675 675 if (ph_data) {
676 676 usb_pipe_close(ph_data->p_dip, def_ph,
677 677 USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED,
678 678 NULL, NULL);
679 679 }
680 680 }
681 681
682 682 mutex_enter(&usba_mutex);
683 683
684 684 /* destroy mutex in each usba_ph_impl structure */
685 685 for (ep_idx = 0; ep_idx < USBA_N_ENDPOINTS; ep_idx++) {
686 686 mutex_destroy(&usba_device->usb_ph_list[ep_idx].usba_ph_mutex);
687 687 }
688 688
689 689 (void) usba_rm_from_list(&usba_device_list,
690 690 &usba_device->usb_device_list);
691 691
692 692 mutex_exit(&usba_mutex);
693 693
694 694 usba_destroy_list(&usba_device->usb_device_list);
695 695 usba_destroy_list(&usba_device->usb_allocated);
696 696
697 697 USB_DPRINTF_L2(DPRINT_MASK_USBA, usba_log_handle,
698 698 "deallocating usba_device = 0x%p, address = 0x%x",
699 699 (void *)usba_device, usba_device->usb_addr);
700 700
701 701 /*
702 702 * ohci allocates descriptors for root hub so we can't
703 703 * deallocate these here
704 704 */
705 705
706 706 if (usba_device->usb_addr != ROOT_HUB_ADDR) {
707 707 if (usba_device->usb_cfg_array) {
708 708 USB_DPRINTF_L3(DPRINT_MASK_USBA, usba_log_handle,
709 709 "deallocating usb_config_array: 0x%p",
710 710 (void *)usba_device->usb_cfg_array);
711 711 mutex_enter(&usba_device->usb_mutex);
712 712 for (i = 0;
713 713 i < usba_device->usb_dev_descr->bNumConfigurations;
714 714 i++) {
715 715 if (usba_device->usb_cfg_array[i]) {
716 716 kmem_free(
717 717 usba_device->usb_cfg_array[i],
718 718 usba_device->usb_cfg_array_len[i]);
719 719 }
720 720 }
721 721
722 722 /* free the array pointers */
723 723 kmem_free(usba_device->usb_cfg_array,
724 724 usba_device->usb_cfg_array_length);
725 725 kmem_free(usba_device->usb_cfg_array_len,
726 726 usba_device->usb_cfg_array_len_length);
727 727
728 728 mutex_exit(&usba_device->usb_mutex);
729 729 }
730 730
731 731 if (usba_device->usb_cfg_str_descr) {
732 732 USB_DPRINTF_L3(DPRINT_MASK_USBA, usba_log_handle,
733 733 "deallocating usb_cfg_str_descr: 0x%p",
734 734 (void *)usba_device->usb_cfg_str_descr);
735 735 for (i = 0;
736 736 i < usba_device->usb_dev_descr->bNumConfigurations;
737 737 i++) {
738 738 if (usba_device->usb_cfg_str_descr[i]) {
739 739 kmem_free(
740 740 usba_device->usb_cfg_str_descr[i],
741 741 strlen(usba_device->
742 742 usb_cfg_str_descr[i]) + 1);
743 743 }
744 744 }
745 745 /* free the array pointers */
746 746 kmem_free(usba_device->usb_cfg_str_descr,
747 747 sizeof (uchar_t *) * usba_device->usb_n_cfgs);
748 748 }
749 749
750 750 if (usba_device->usb_dev_descr) {
751 751 kmem_free(usba_device->usb_dev_descr,
752 752 sizeof (usb_dev_descr_t));
753 753 }
754 754
755 755 if (usba_device->usb_mfg_str) {
756 756 kmem_free(usba_device->usb_mfg_str,
757 757 strlen(usba_device->usb_mfg_str) + 1);
758 758 }
759 759
760 760 if (usba_device->usb_product_str) {
761 761 kmem_free(usba_device->usb_product_str,
762 762 strlen(usba_device->usb_product_str) + 1);
763 763 }
764 764
765 765 if (usba_device->usb_serialno_str) {
766 766 kmem_free(usba_device->usb_serialno_str,
767 767 strlen(usba_device->usb_serialno_str) + 1);
768 768 }
769 769
770 770 usba_unset_usb_address(usba_device);
771 771 }
772 772
773 773 #ifndef __lock_lint
774 774 ASSERT(usba_device->usb_client_dev_data_list.cddl_next == NULL);
775 775 #endif
776 776
777 777 if (usba_device->usb_client_flags) {
778 778 #ifndef __lock_lint
779 779 int i;
780 780
781 781 for (i = 0; i < usba_device->usb_n_ifs; i++) {
782 782 ASSERT(usba_device->usb_client_flags[i] == 0);
783 783 }
784 784 #endif
785 785 kmem_free(usba_device->usb_client_flags,
786 786 usba_device->usb_n_ifs * USBA_CLIENT_FLAG_SIZE);
787 787 }
788 788
789 789
790 790 if (usba_device->usb_client_attach_list) {
791 791 kmem_free(usba_device->usb_client_attach_list,
792 792 usba_device->usb_n_ifs *
793 793 sizeof (*usba_device->usb_client_attach_list));
794 794 }
795 795 if (usba_device->usb_client_ev_cb_list) {
796 796 kmem_free(usba_device->usb_client_ev_cb_list,
797 797 usba_device->usb_n_ifs *
798 798 sizeof (*usba_device->usb_client_ev_cb_list));
799 799 }
800 800
801 801 /*
802 802 * finally ready to destroy the structure
803 803 */
804 804 mutex_destroy(&usba_device->usb_mutex);
805 805
806 806 kmem_free((caddr_t)usba_device, sizeof (usba_device_t));
807 807 }
808 808
809 809
810 810 /* clear the data toggle for all endpoints on this device */
811 811 void
812 812 usba_clear_data_toggle(usba_device_t *usba_device)
813 813 {
814 814 int i;
815 815
816 816 if (usba_device != NULL) {
817 817 mutex_enter(&usba_device->usb_mutex);
818 818 for (i = 0; i < USBA_N_ENDPOINTS; i++) {
819 819 usba_device->usb_ph_list[i].usba_ph_flags &=
820 820 ~USBA_PH_DATA_TOGGLE;
821 821 }
822 822 mutex_exit(&usba_device->usb_mutex);
823 823 }
824 824 }
825 825
826 826
827 827 /*
828 828 * usba_create_child_devi():
829 829 * create a child devinfo node, usba_device, attach properties.
830 830 * the usba_device structure is shared between all interfaces
831 831 */
832 832 int
833 833 usba_create_child_devi(dev_info_t *dip,
834 834 char *node_name,
835 835 usba_hcdi_ops_t *usba_hcdi_ops,
836 836 dev_info_t *usb_root_hub_dip,
837 837 usb_port_status_t port_status,
838 838 usba_device_t *usba_device,
839 839 dev_info_t **child_dip)
840 840 {
841 841 int rval = USB_FAILURE;
842 842 int usba_device_allocated = 0;
843 843 usb_addr_t address;
844 844
845 845 USB_DPRINTF_L4(DPRINT_MASK_USBA, usba_log_handle,
846 846 "usba_create_child_devi: %s usba_device=0x%p "
847 847 "port status=0x%x", node_name,
848 848 (void *)usba_device, port_status);
849 849
850 850 ndi_devi_alloc_sleep(dip, node_name, (pnode_t)DEVI_SID_NODEID,
851 851 child_dip);
852 852
853 853 USB_DPRINTF_L3(DPRINT_MASK_USBA, usba_log_handle,
854 854 "child dip=0x%p", (void *)*child_dip);
855 855
856 856 if (usba_device == NULL) {
857 857
858 858 usba_device = usba_alloc_usba_device(usb_root_hub_dip);
859 859
860 860 /* grab the mutex to keep warlock happy */
861 861 mutex_enter(&usba_device->usb_mutex);
862 862 usba_device->usb_hcdi_ops = usba_hcdi_ops;
863 863 usba_device->usb_port_status = port_status;
864 864 mutex_exit(&usba_device->usb_mutex);
865 865
866 866 usba_device_allocated++;
867 867 } else {
868 868 mutex_enter(&usba_device->usb_mutex);
869 869 if (usba_hcdi_ops) {
870 870 ASSERT(usba_device->usb_hcdi_ops == usba_hcdi_ops);
871 871 }
872 872 if (usb_root_hub_dip) {
873 873 ASSERT(usba_device->usb_root_hub_dip ==
874 874 usb_root_hub_dip);
875 875 }
876 876
877 877 usba_device->usb_port_status = port_status;
878 878
879 879 mutex_exit(&usba_device->usb_mutex);
880 880 }
881 881
882 882 if (usba_device->usb_addr == 0) {
883 883 if (usba_set_usb_address(usba_device) == USB_FAILURE) {
884 884 address = 0;
885 885
886 886 USB_DPRINTF_L2(DPRINT_MASK_USBA, usba_log_handle,
887 887 "cannot set usb address for dip=0x%p",
888 888 (void *)*child_dip);
889 889
890 890 goto fail;
891 891 }
892 892 }
893 893 address = usba_device->usb_addr;
894 894
895 895 /* attach properties */
896 896 rval = ndi_prop_update_int(DDI_DEV_T_NONE, *child_dip,
897 897 "assigned-address", address);
898 898 if (rval != DDI_PROP_SUCCESS) {
899 899 USB_DPRINTF_L2(DPRINT_MASK_USBA, usba_log_handle,
900 900 "cannot set usb address property for dip=0x%p",
901 901 (void *)*child_dip);
902 902 rval = USB_FAILURE;
903 903
904 904 goto fail;
905 905 }
906 906
907 907 /*
908 908 * store the usba_device point in the dip
909 909 */
910 910 usba_set_usba_device(*child_dip, usba_device);
911 911
912 912 USB_DPRINTF_L4(DPRINT_MASK_USBA, usba_log_handle,
913 913 "usba_create_child_devi: devi=0x%p (%s) ud=0x%p",
914 914 (void *)*child_dip, ddi_driver_name(*child_dip),
915 915 (void *)usba_device);
916 916
917 917 return (USB_SUCCESS);
918 918
919 919 fail:
920 920 if (*child_dip) {
921 921 int rval = usba_destroy_child_devi(*child_dip, NDI_DEVI_REMOVE);
922 922 ASSERT(rval == USB_SUCCESS);
923 923 *child_dip = NULL;
924 924 }
925 925
926 926 if (usba_device_allocated) {
927 927 usba_free_usba_device(usba_device);
928 928 } else if (address && usba_device) {
929 929 usba_unset_usb_address(usba_device);
930 930 }
931 931
932 932 USB_DPRINTF_L2(DPRINT_MASK_USBA, usba_log_handle,
933 933 "usba_create_child_devi failed: rval=%d", rval);
934 934
935 935 return (rval);
936 936 }
937 937
938 938
939 939 int
940 940 usba_destroy_child_devi(dev_info_t *dip, uint_t flag)
941 941 {
942 942 usba_device_t *usba_device;
943 943 int rval = NDI_SUCCESS;
944 944
945 945 USB_DPRINTF_L2(DPRINT_MASK_USBA, usba_log_handle,
946 946 "usba_destroy_child_devi: %s%d (0x%p)",
947 947 ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip);
948 948
949 949 usba_device = usba_get_usba_device(dip);
950 950
951 951 /*
952 952 * if the child hasn't been bound yet, we can just
953 953 * free the dip
954 954 */
955 955 if (i_ddi_node_state(dip) < DS_INITIALIZED) {
956 956 /*
957 957 * do not call ndi_devi_free() since it might
958 958 * deadlock
959 959 */
960 960 rval = ddi_remove_child(dip, 0);
961 961
962 962 } else {
963 963 char *devnm = kmem_alloc(MAXNAMELEN + 1, KM_SLEEP);
964 964 dev_info_t *pdip = ddi_get_parent(dip);
965 965
966 966 (void) ddi_deviname(dip, devnm);
967 967
968 968 USB_DPRINTF_L3(DPRINT_MASK_USBA, usba_log_handle,
969 969 "usba_destroy_child_devi:\n\t"
970 970 "offlining dip 0x%p usba_device=0x%p (%s)", (void *)dip,
971 971 (void *)usba_device, devnm);
972 972
973 973 (void) devfs_clean(pdip, NULL, DV_CLEAN_FORCE);
974 974 rval = ndi_devi_unconfig_one(pdip, devnm + 1, NULL,
975 975 flag | NDI_UNCONFIG | NDI_DEVI_OFFLINE);
976 976 if (rval != NDI_SUCCESS) {
977 977 USB_DPRINTF_L2(DPRINT_MASK_USBA, usba_log_handle,
978 978 " ndi_devi_unconfig_one %s%d failed (%d)",
979 979 ddi_driver_name(dip), ddi_get_instance(dip),
980 980 rval);
981 981 }
982 982 kmem_free(devnm, MAXNAMELEN + 1);
983 983 }
984 984
985 985 USB_DPRINTF_L4(DPRINT_MASK_USBA, usba_log_handle,
986 986 "usba_destroy_child_devi: rval=%d", rval);
987 987
988 988 return (rval == NDI_SUCCESS ? USB_SUCCESS : USB_FAILURE);
989 989 }
990 990
991 991
992 992 /*
993 993 * list management
994 994 */
995 995 void
996 996 usba_init_list(usba_list_entry_t *element, usb_opaque_t private,
997 997 ddi_iblock_cookie_t iblock_cookie)
998 998 {
999 999 mutex_init(&element->list_mutex, NULL, MUTEX_DRIVER,
1000 1000 iblock_cookie);
1001 1001 mutex_enter(&element->list_mutex);
1002 1002 element->private = private;
1003 1003 mutex_exit(&element->list_mutex);
1004 1004 }
1005 1005
1006 1006
1007 1007 void
1008 1008 usba_destroy_list(usba_list_entry_t *head)
1009 1009 {
1010 1010 mutex_enter(&head->list_mutex);
1011 1011 ASSERT(head->next == NULL);
1012 1012 ASSERT(head->prev == NULL);
1013 1013 mutex_exit(&head->list_mutex);
1014 1014
1015 1015 mutex_destroy(&head->list_mutex);
1016 1016 }
1017 1017
1018 1018
1019 1019 void
1020 1020 usba_add_to_list(usba_list_entry_t *head, usba_list_entry_t *element)
1021 1021 {
1022 1022 usba_list_entry_t *next;
1023 1023 int remaining;
1024 1024
1025 1025 mutex_enter(&head->list_mutex);
1026 1026 mutex_enter(&element->list_mutex);
1027 1027
1028 1028 remaining = head->count;
1029 1029
1030 1030 /* check if it is not in another list */
1031 1031 ASSERT(element->next == NULL);
1032 1032 ASSERT(element->prev == NULL);
1033 1033
1034 1034 #ifdef DEBUG
1035 1035 /*
1036 1036 * only verify the list when not in interrupt context, we
1037 1037 * have to trust the HCD
1038 1038 */
1039 1039 if (!servicing_interrupt()) {
1040 1040
1041 1041 /* check if not already in this list */
1042 1042 for (next = head->next; (next != NULL);
1043 1043 next = next->next) {
1044 1044 if (next == element) {
1045 1045 USB_DPRINTF_L0(DPRINT_MASK_USBA,
1046 1046 usba_log_handle,
1047 1047 "Attempt to corrupt USB list at 0x%p",
1048 1048 (void *)head);
1049 1049 ASSERT(next == element);
1050 1050
1051 1051 goto done;
1052 1052 }
1053 1053 remaining--;
1054 1054
1055 1055 /*
1056 1056 * Detect incorrect circ links or found
1057 1057 * unexpected elements.
1058 1058 */
1059 1059 if ((next->next && (remaining == 0)) ||
1060 1060 ((next->next == NULL) && remaining)) {
1061 1061 panic("Corrupted USB list at 0x%p",
1062 1062 (void *)head);
1063 1063 /*NOTREACHED*/
1064 1064 }
1065 1065 }
1066 1066 }
1067 1067 #endif
1068 1068
1069 1069 if (head->next == NULL) {
1070 1070 head->prev = head->next = element;
1071 1071 } else {
1072 1072 /* add to tail */
1073 1073 head->prev->next = element;
1074 1074 element->prev = head->prev;
1075 1075 head->prev = element;
1076 1076 }
1077 1077
1078 1078 head->count++;
1079 1079
1080 1080 USB_DPRINTF_L4(DPRINT_MASK_USBA, usba_log_handle,
1081 1081 "usba_add_to_list: head=0x%p element=0x%p count=%d",
1082 1082 (void *)head, (void *)element, head->count);
1083 1083
1084 1084 done:
1085 1085 mutex_exit(&head->list_mutex);
1086 1086 mutex_exit(&element->list_mutex);
1087 1087 }
1088 1088
1089 1089
1090 1090 int
1091 1091 usba_rm_from_list(usba_list_entry_t *head, usba_list_entry_t *element)
1092 1092 {
1093 1093 usba_list_entry_t *e;
1094 1094 int found = 0;
1095 1095 int remaining;
1096 1096
1097 1097 /* find the element in the list first */
1098 1098 mutex_enter(&head->list_mutex);
1099 1099
1100 1100 USB_DPRINTF_L4(DPRINT_MASK_USBA, usba_log_handle,
1101 1101 "usba_rm_from_list: head=0x%p element=0x%p count=%d",
1102 1102 (void *)head, (void *)element, head->count);
1103 1103
1104 1104 remaining = head->count;
1105 1105 e = head->next;
1106 1106
1107 1107 while (e) {
1108 1108 if (e == element) {
1109 1109 found++;
1110 1110 break;
1111 1111 }
1112 1112 e = e->next;
1113 1113
1114 1114 remaining--;
1115 1115
1116 1116 /* Detect incorrect circ links or found unexpected elements. */
1117 1117 if ((e && (remaining == 0)) ||
1118 1118 ((e == NULL) && (remaining))) {
1119 1119 panic("Corrupted USB list at 0x%p", (void *)head);
1120 1120 /*NOTREACHED*/
1121 1121 }
1122 1122 }
1123 1123
1124 1124 if (!found) {
1125 1125 mutex_exit(&head->list_mutex);
1126 1126
1127 1127 return (USB_FAILURE);
1128 1128 }
1129 1129
1130 1130 /* now remove the element */
1131 1131 mutex_enter(&element->list_mutex);
1132 1132
1133 1133 if (element->next) {
1134 1134 element->next->prev = element->prev;
1135 1135 }
1136 1136 if (element->prev) {
1137 1137 element->prev->next = element->next;
1138 1138 }
1139 1139 if (head->next == element) {
1140 1140 head->next = element->next;
1141 1141 }
1142 1142 if (head->prev == element) {
1143 1143 head->prev = element->prev;
1144 1144 }
1145 1145
1146 1146 element->prev = element->next = NULL;
1147 1147 if (head->next == NULL) {
1148 1148 ASSERT(head->prev == NULL);
1149 1149 } else {
1150 1150 ASSERT(head->next->prev == NULL);
1151 1151 }
1152 1152 if (head->prev == NULL) {
1153 1153 ASSERT(head->next == NULL);
1154 1154 } else {
1155 1155 ASSERT(head->prev->next == NULL);
1156 1156 }
1157 1157
1158 1158 head->count--;
1159 1159
1160 1160 USB_DPRINTF_L4(DPRINT_MASK_USBA, usba_log_handle,
1161 1161 "usba_rm_from_list success: head=0x%p element=0x%p cnt=%d",
1162 1162 (void *)head, (void *)element, head->count);
1163 1163
1164 1164 mutex_exit(&element->list_mutex);
1165 1165 mutex_exit(&head->list_mutex);
1166 1166
1167 1167 return (USB_SUCCESS);
1168 1168 }
1169 1169
1170 1170
1171 1171 usba_list_entry_t *
1172 1172 usba_rm_first_from_list(usba_list_entry_t *head)
1173 1173 {
1174 1174 usba_list_entry_t *element = NULL;
1175 1175
1176 1176 if (head) {
1177 1177 mutex_enter(&head->list_mutex);
1178 1178 element = head->next;
1179 1179 if (element) {
1180 1180 /* now remove the element */
1181 1181 mutex_enter(&element->list_mutex);
1182 1182 head->next = element->next;
1183 1183 if (head->next) {
1184 1184 head->next->prev = NULL;
1185 1185 }
1186 1186 if (head->prev == element) {
1187 1187 head->prev = element->next;
1188 1188 }
1189 1189 element->prev = element->next = NULL;
1190 1190 mutex_exit(&element->list_mutex);
1191 1191 head->count--;
1192 1192 }
1193 1193 if (head->next == NULL) {
1194 1194 ASSERT(head->prev == NULL);
1195 1195 } else {
1196 1196 ASSERT(head->next->prev == NULL);
1197 1197 }
1198 1198 if (head->prev == NULL) {
1199 1199 ASSERT(head->next == NULL);
1200 1200 } else {
1201 1201 ASSERT(head->prev->next == NULL);
1202 1202 }
1203 1203 USB_DPRINTF_L4(DPRINT_MASK_USBA, usba_log_handle,
1204 1204 "usba_rm_first_from_list: head=0x%p el=0x%p cnt=%d",
1205 1205 (void *)head, (void *)element, head->count);
1206 1206
1207 1207 mutex_exit(&head->list_mutex);
1208 1208 }
1209 1209
1210 1210 return (element);
1211 1211 }
1212 1212
1213 1213
1214 1214 usb_opaque_t
1215 1215 usba_rm_first_pvt_from_list(usba_list_entry_t *head)
1216 1216 {
1217 1217 usba_list_entry_t *element = usba_rm_first_from_list(head);
1218 1218 usb_opaque_t private = NULL;
1219 1219
1220 1220 if (element) {
1221 1221 mutex_enter(&element->list_mutex);
1222 1222 private = element->private;
1223 1223 mutex_exit(&element->list_mutex);
1224 1224 }
1225 1225
1226 1226 return (private);
1227 1227 }
1228 1228
1229 1229
1230 1230 /*
1231 1231 * move list to new list and zero original list
1232 1232 */
1233 1233 void
1234 1234 usba_move_list(usba_list_entry_t *head, usba_list_entry_t *new,
1235 1235 ddi_iblock_cookie_t iblock_cookie)
1236 1236 {
1237 1237 usba_init_list(new, NULL, iblock_cookie);
1238 1238 mutex_enter(&head->list_mutex);
1239 1239 mutex_enter(&new->list_mutex);
1240 1240
1241 1241 new->next = head->next;
1242 1242 new->prev = head->prev;
1243 1243 new->count = head->count;
1244 1244 new->private = head->private;
1245 1245
1246 1246 head->next = NULL;
1247 1247 head->prev = NULL;
1248 1248 head->count = 0;
1249 1249 head->private = NULL;
1250 1250 mutex_exit(&head->list_mutex);
1251 1251 mutex_exit(&new->list_mutex);
1252 1252 }
1253 1253
1254 1254
1255 1255 int
1256 1256 usba_check_in_list(usba_list_entry_t *head, usba_list_entry_t *element)
1257 1257 {
1258 1258 int rval = USB_FAILURE;
1259 1259 int remaining;
1260 1260 usba_list_entry_t *next;
1261 1261
1262 1262 mutex_enter(&head->list_mutex);
1263 1263 remaining = head->count;
1264 1264
1265 1265 mutex_enter(&element->list_mutex);
1266 1266 for (next = head->next; next != NULL; next = next->next) {
1267 1267 if (next == element) {
1268 1268 rval = USB_SUCCESS;
1269 1269 break;
1270 1270 }
1271 1271 remaining--;
1272 1272
1273 1273 /* Detect incorrect circ links or found unexpected elements. */
1274 1274 if ((next->next && (remaining == 0)) ||
1275 1275 ((next->next == NULL) && remaining)) {
1276 1276 panic("Corrupted USB list at 0x%p", (void *)head);
1277 1277 /*NOTREACHED*/
1278 1278 }
1279 1279 }
1280 1280 mutex_exit(&element->list_mutex);
1281 1281 mutex_exit(&head->list_mutex);
1282 1282
1283 1283 return (rval);
1284 1284 }
1285 1285
1286 1286
1287 1287 int
1288 1288 usba_list_entry_leaks(usba_list_entry_t *head, char *what)
1289 1289 {
1290 1290 int count = 0;
1291 1291 int remaining;
1292 1292 usba_list_entry_t *next;
1293 1293
1294 1294 mutex_enter(&head->list_mutex);
1295 1295 remaining = head->count;
1296 1296 for (next = head->next; next != NULL; next = next->next) {
1297 1297 USB_DPRINTF_L2(DPRINT_MASK_HCDI, usba_log_handle,
1298 1298 "leaking %s 0x%p", what, (void *)next->private);
1299 1299 count++;
1300 1300
1301 1301 remaining--;
1302 1302
1303 1303 /* Detect incorrect circ links or found unexpected elements. */
1304 1304 if ((next->next && (remaining == 0)) ||
1305 1305 ((next->next == NULL) && remaining)) {
1306 1306 panic("Corrupted USB list at 0x%p", (void *)head);
1307 1307 /*NOTREACHED*/
1308 1308 }
1309 1309 }
1310 1310 ASSERT(count == head->count);
1311 1311 mutex_exit(&head->list_mutex);
1312 1312
1313 1313 if (count) {
1314 1314 USB_DPRINTF_L2(DPRINT_MASK_HCDI, usba_log_handle,
1315 1315 "usba_list_entry_count: leaking %d", count);
1316 1316 }
1317 1317
1318 1318 return (count);
1319 1319 }
1320 1320
1321 1321
1322 1322 int
1323 1323 usba_list_entry_count(usba_list_entry_t *head)
1324 1324 {
1325 1325 int count;
1326 1326
1327 1327 mutex_enter(&head->list_mutex);
1328 1328 count = head->count;
1329 1329 mutex_exit(&head->list_mutex);
1330 1330
1331 1331 return (count);
1332 1332 }
1333 1333
1334 1334 /* add a new root hub to the usba_root_hubs list */
1335 1335
1336 1336 void
1337 1337 usba_add_root_hub(dev_info_t *dip)
1338 1338 {
1339 1339 usba_root_hub_ent_t *hub;
1340 1340
1341 1341 hub = (usba_root_hub_ent_t *)
1342 1342 kmem_zalloc(sizeof (usba_root_hub_ent_t), KM_SLEEP);
1343 1343
1344 1344 mutex_enter(&usba_hub_mutex);
1345 1345 hub->dip = dip;
1346 1346 hub->next = usba_root_hubs;
1347 1347 usba_root_hubs = hub;
1348 1348 mutex_exit(&usba_hub_mutex);
1349 1349 }
1350 1350
1351 1351 /* remove a root hub from the usba_root_hubs list */
1352 1352
1353 1353 void
1354 1354 usba_rem_root_hub(dev_info_t *dip)
1355 1355 {
1356 1356 usba_root_hub_ent_t **hubp, *hub;
1357 1357
1358 1358 mutex_enter(&usba_hub_mutex);
1359 1359 hubp = &usba_root_hubs;
1360 1360 while (*hubp) {
1361 1361 if ((*hubp)->dip == dip) {
1362 1362 hub = *hubp;
1363 1363 *hubp = hub->next;
1364 1364 kmem_free(hub, sizeof (struct usba_root_hub_ent));
1365 1365 mutex_exit(&usba_hub_mutex);
1366 1366
1367 1367 return;
1368 1368 }
1369 1369 hubp = &(*hubp)->next;
1370 1370 }
1371 1371 mutex_exit(&usba_hub_mutex);
1372 1372 }
1373 1373
1374 1374 /*
1375 1375 * check whether this dip is the root hub. Any root hub known by
1376 1376 * usba is recorded in the linked list pointed to by usba_root_hubs
1377 1377 */
1378 1378 int
1379 1379 usba_is_root_hub(dev_info_t *dip)
1380 1380 {
1381 1381 usba_root_hub_ent_t *hub;
1382 1382
1383 1383 mutex_enter(&usba_hub_mutex);
1384 1384 hub = usba_root_hubs;
1385 1385 while (hub) {
1386 1386 if (hub->dip == dip) {
1387 1387 mutex_exit(&usba_hub_mutex);
1388 1388
1389 1389 return (1);
1390 1390 }
1391 1391 hub = hub->next;
1392 1392 }
1393 1393 mutex_exit(&usba_hub_mutex);
1394 1394
1395 1395 return (0);
1396 1396 }
1397 1397
1398 1398 /*
1399 1399 * get and store usba_device pointer in the devi
1400 1400 */
1401 1401 usba_device_t *
1402 1402 usba_get_usba_device(dev_info_t *dip)
1403 1403 {
1404 1404 /*
1405 1405 * we cannot use parent_data in the usb node because its
1406 1406 * bus parent (eg. PCI nexus driver) uses this data
1407 1407 *
1408 1408 * we cannot use driver data in the other usb nodes since
1409 1409 * usb drivers may need to use this
1410 1410 */
1411 1411 if (usba_is_root_hub(dip)) {
1412 1412 usba_hcdi_t *hcdi = usba_hcdi_get_hcdi(dip);
1413 1413
1414 1414 return (hcdi->hcdi_usba_device);
1415 1415 } else {
1416 1416
1417 1417 return (ddi_get_parent_data(dip));
1418 1418 }
1419 1419 }
1420 1420
1421 1421
1422 1422 /*
1423 1423 * Retrieve the usba_device pointer from the dev without checking for
1424 1424 * the root hub first. This function is only used in polled mode.
1425 1425 */
1426 1426 usba_device_t *
1427 1427 usba_polled_get_usba_device(dev_info_t *dip)
1428 1428 {
1429 1429 /*
1430 1430 * Don't call usba_is_root_hub() to find out if this is
1431 1431 * the root hub usba_is_root_hub() calls into the DDI
1432 1432 * where there are locking issues. The dip sent in during
1433 1433 * polled mode will never be the root hub, so just get
1434 1434 * the usba_device pointer from the dip.
1435 1435 */
1436 1436 return (ddi_get_parent_data(dip));
1437 1437 }
1438 1438
1439 1439
1440 1440 void
1441 1441 usba_set_usba_device(dev_info_t *dip, usba_device_t *usba_device)
1442 1442 {
1443 1443 if (usba_is_root_hub(dip)) {
1444 1444 usba_hcdi_t *hcdi = usba_hcdi_get_hcdi(dip);
1445 1445 /* no locking is needed here */
1446 1446 _NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(hcdi->hcdi_usba_device))
1447 1447 hcdi->hcdi_usba_device = usba_device;
1448 1448 _NOTE(NOW_VISIBLE_TO_OTHER_THREADS(hcdi->hcdi_usba_device))
1449 1449 } else {
1450 1450 ddi_set_parent_data(dip, usba_device);
1451 1451 }
1452 1452 }
1453 1453
1454 1454
1455 1455 /*
1456 1456 * usba_set_node_name() according to class, subclass, and protocol
1457 1457 * following the 1275 USB binding tables.
1458 1458 */
1459 1459
1460 1460 /* device node table, refer to section 3.2.2.1 of 1275 binding */
1461 1461 static node_name_entry_t device_node_name_table[] = {
1462 1462 { USB_CLASS_COMM, DONTCARE, DONTCARE, "communications" },
1463 1463 { USB_CLASS_HUB, DONTCARE, DONTCARE, "hub" },
1464 1464 { USB_CLASS_DIAG, DONTCARE, DONTCARE, "diagnostics" },
1465 1465 { USB_CLASS_MISC, DONTCARE, DONTCARE, "miscellaneous" },
1466 1466 { DONTCARE, DONTCARE, DONTCARE, "device" }
1467 1467 };
1468 1468
1469 1469 /* interface-association node table */
1470 1470 static node_name_entry_t ia_node_name_table[] = {
1471 1471 { USB_CLASS_AUDIO, DONTCARE, DONTCARE, "audio" },
1472 1472 { USB_CLASS_VIDEO, DONTCARE, DONTCARE, "video" },
1473 1473 { USB_CLASS_WIRELESS, USB_SUBCLS_WUSB_2, USB_PROTO_WUSB_DWA,
1474 1474 "device-wire-adaptor" },
1475 1475 { USB_CLASS_WIRELESS, DONTCARE, DONTCARE, "wireless-controller" },
1476 1476 { DONTCARE, DONTCARE, DONTCARE, "interface-association" }
1477 1477 };
1478 1478
1479 1479 /* interface node table, refer to section 3.3.2.1 */
1480 1480 static node_name_entry_t if_node_name_table[] = {
1481 1481 { USB_CLASS_AUDIO, USB_SUBCLS_AUD_CONTROL, DONTCARE, "sound-control" },
1482 1482 { USB_CLASS_AUDIO, USB_SUBCLS_AUD_STREAMING, DONTCARE, "sound" },
1483 1483 { USB_CLASS_AUDIO, USB_SUBCLS_AUD_MIDI_STREAMING, DONTCARE, "midi" },
1484 1484 { USB_CLASS_AUDIO, DONTCARE, DONTCARE, "sound" },
1485 1485
1486 1486 { USB_CLASS_COMM, USB_SUBCLS_CDCC_DIRECT_LINE, DONTCARE, "line" },
1487 1487 { USB_CLASS_COMM, USB_SUBCLS_CDCC_ABSTRCT_CTRL, DONTCARE, "modem" },
1488 1488 { USB_CLASS_COMM, USB_SUBCLS_CDCC_PHONE_CTRL, DONTCARE, "telephone" },
1489 1489 { USB_CLASS_COMM, USB_SUBCLS_CDCC_MULTCNL_ISDN, DONTCARE, "isdn" },
1490 1490 { USB_CLASS_COMM, USB_SUBCLS_CDCC_ISDN, DONTCARE, "isdn" },
1491 1491 { USB_CLASS_COMM, USB_SUBCLS_CDCC_ETHERNET, DONTCARE, "ethernet" },
1492 1492 { USB_CLASS_COMM, USB_SUBCLS_CDCC_ATM_NETWORK, DONTCARE, "atm-network" },
1493 1493 { USB_CLASS_COMM, DONTCARE, DONTCARE, "communications" },
1494 1494
1495 1495 { USB_CLASS_HID, USB_SUBCLS_HID_1, USB_PROTO_HID_KEYBOARD, "keyboard" },
1496 1496 { USB_CLASS_HID, USB_SUBCLS_HID_1, USB_PROTO_HID_MOUSE, "mouse" },
1497 1497 { USB_CLASS_HID, DONTCARE, DONTCARE, "input" },
1498 1498
1499 1499 { USB_CLASS_HUB, DONTCARE, DONTCARE, "hub" },
1500 1500
1501 1501 { USB_CLASS_PHYSICAL, DONTCARE, DONTCARE, "physical" },
1502 1502
1503 1503 { USB_CLASS_IMAGE, DONTCARE, DONTCARE, "image" },
1504 1504
1505 1505 { USB_CLASS_PRINTER, DONTCARE, DONTCARE, "printer" },
1506 1506
1507 1507 { USB_CLASS_MASS_STORAGE, DONTCARE, DONTCARE, "storage" },
1508 1508
1509 1509 { USB_CLASS_CDC_DATA, DONTCARE, DONTCARE, "data" },
1510 1510
1511 1511 { USB_CLASS_SECURITY, DONTCARE, DONTCARE, "security" },
1512 1512
1513 1513 { USB_CLASS_VIDEO, USB_SUBCLS_VIDEO_CONTROL, DONTCARE, "video-control" },
1514 1514 { USB_CLASS_VIDEO, USB_SUBCLS_VIDEO_STREAM, DONTCARE, "video-stream" },
1515 1515 { USB_CLASS_VIDEO, DONTCARE, DONTCARE, "video" },
1516 1516
1517 1517 { USB_CLASS_APP, USB_SUBCLS_APP_FIRMWARE, DONTCARE, "firmware" },
1518 1518 { USB_CLASS_APP, USB_SUBCLS_APP_IRDA, DONTCARE, "IrDa" },
1519 1519 { USB_CLASS_APP, USB_SUBCLS_APP_TEST, DONTCARE, "test" },
1520 1520
1521 1521 { USB_CLASS_MISC, USB_SUBCLS_CBAF, USB_PROTO_CBAF, "wusb_ca"},
1522 1522 { USB_CLASS_WIRELESS, USB_SUBCLS_WUSB_1, USB_PROTO_WUSB_RC, "hwa-radio" },
1523 1523 { USB_CLASS_WIRELESS, USB_SUBCLS_WUSB_2, USB_PROTO_WUSB_HWA, "hwa-host" },
1524 1524 { USB_CLASS_WIRELESS, USB_SUBCLS_WUSB_2, USB_PROTO_WUSB_DWA, "dwa-control" },
1525 1525 { USB_CLASS_WIRELESS, USB_SUBCLS_WUSB_2, USB_PROTO_WUSB_DWA_ISO, "dwa-isoc" },
1526 1526 { USB_CLASS_WIRELESS, DONTCARE, DONTCARE, "wireless" },
1527 1527
1528 1528 { DONTCARE, DONTCARE, DONTCARE, "interface" },
1529 1529
1530 1530 };
1531 1531
1532 1532 /* combined node table, refer to section 3.4.2.1 */
1533 1533 static node_name_entry_t combined_node_name_table[] = {
1534 1534 { USB_CLASS_AUDIO, USB_SUBCLS_AUD_CONTROL, DONTCARE, "sound-control" },
1535 1535 { USB_CLASS_AUDIO, USB_SUBCLS_AUD_STREAMING, DONTCARE, "sound" },
1536 1536 { USB_CLASS_AUDIO, USB_SUBCLS_AUD_MIDI_STREAMING, DONTCARE, "midi" },
1537 1537 { USB_CLASS_AUDIO, DONTCARE, DONTCARE, "sound" },
1538 1538
1539 1539 { USB_CLASS_COMM, USB_SUBCLS_CDCC_DIRECT_LINE, DONTCARE, "line" },
1540 1540 { USB_CLASS_COMM, USB_SUBCLS_CDCC_ABSTRCT_CTRL, DONTCARE, "modem" },
1541 1541 { USB_CLASS_COMM, USB_SUBCLS_CDCC_PHONE_CTRL, DONTCARE, "telephone" },
1542 1542 { USB_CLASS_COMM, USB_SUBCLS_CDCC_MULTCNL_ISDN, DONTCARE, "isdn" },
1543 1543 { USB_CLASS_COMM, USB_SUBCLS_CDCC_ISDN, DONTCARE, "isdn" },
1544 1544 { USB_CLASS_COMM, USB_SUBCLS_CDCC_ETHERNET, DONTCARE, "ethernet" },
1545 1545 { USB_CLASS_COMM, USB_SUBCLS_CDCC_ATM_NETWORK, DONTCARE, "atm-network" },
1546 1546 { USB_CLASS_COMM, DONTCARE, DONTCARE, "communications" },
1547 1547
1548 1548 { USB_CLASS_HID, USB_SUBCLS_HID_1, USB_PROTO_HID_KEYBOARD, "keyboard" },
1549 1549 { USB_CLASS_HID, USB_SUBCLS_HID_1, USB_PROTO_HID_MOUSE, "mouse" },
1550 1550 { USB_CLASS_HID, DONTCARE, DONTCARE, "input" },
1551 1551
1552 1552 { USB_CLASS_PHYSICAL, DONTCARE, DONTCARE, "physical" },
1553 1553
1554 1554 { USB_CLASS_IMAGE, DONTCARE, DONTCARE, "image" },
1555 1555
1556 1556 { USB_CLASS_PRINTER, DONTCARE, DONTCARE, "printer" },
1557 1557
1558 1558 { USB_CLASS_MASS_STORAGE, USB_SUBCLS_MS_RBC_T10, DONTCARE, "storage" },
1559 1559 { USB_CLASS_MASS_STORAGE, USB_SUBCLS_MS_SFF8020I, DONTCARE, "cdrom" },
1560 1560 { USB_CLASS_MASS_STORAGE, USB_SUBCLS_MS_QIC_157, DONTCARE, "tape" },
1561 1561 { USB_CLASS_MASS_STORAGE, USB_SUBCLS_MS_UFI, DONTCARE, "floppy" },
1562 1562 { USB_CLASS_MASS_STORAGE, USB_SUBCLS_MS_SFF8070I, DONTCARE, "storage" },
1563 1563 { USB_CLASS_MASS_STORAGE, USB_SUBCLS_MS_SCSI, DONTCARE, "storage" },
1564 1564 { USB_CLASS_MASS_STORAGE, DONTCARE, DONTCARE, "storage" },
1565 1565
1566 1566 { USB_CLASS_CDC_DATA, DONTCARE, DONTCARE, "data" },
1567 1567
1568 1568 { USB_CLASS_SECURITY, DONTCARE, DONTCARE, "security" },
1569 1569
1570 1570 { USB_CLASS_VIDEO, USB_SUBCLS_VIDEO_CONTROL, DONTCARE, "video-control" },
1571 1571 { USB_CLASS_VIDEO, USB_SUBCLS_VIDEO_STREAM, DONTCARE, "video-stream" },
1572 1572 { USB_CLASS_VIDEO, DONTCARE, DONTCARE, "video" },
1573 1573
1574 1574 { USB_CLASS_APP, USB_SUBCLS_APP_FIRMWARE, DONTCARE, "firmware" },
1575 1575 { USB_CLASS_APP, USB_SUBCLS_APP_IRDA, DONTCARE, "IrDa" },
1576 1576 { USB_CLASS_APP, USB_SUBCLS_APP_TEST, DONTCARE, "test" },
1577 1577
1578 1578 { USB_CLASS_COMM, DONTCARE, DONTCARE, "communications" },
1579 1579 { USB_CLASS_HUB, DONTCARE, DONTCARE, "hub" },
1580 1580 { USB_CLASS_DIAG, DONTCARE, DONTCARE, "diagnostics" },
1581 1581 { USB_CLASS_MISC, DONTCARE, DONTCARE, "miscellaneous" },
1582 1582 { DONTCARE, DONTCARE, DONTCARE, "device" }
1583 1583 };
1584 1584
1585 1585 static size_t device_node_name_table_size =
1586 1586 sizeof (device_node_name_table)/sizeof (struct node_name_entry);
1587 1587 static size_t ia_node_name_table_size =
1588 1588 sizeof (ia_node_name_table)/sizeof (struct node_name_entry);
1589 1589 static size_t if_node_name_table_size =
1590 1590 sizeof (if_node_name_table)/sizeof (struct node_name_entry);
1591 1591 static size_t combined_node_name_table_size =
1592 1592 sizeof (combined_node_name_table)/sizeof (struct node_name_entry);
1593 1593
1594 1594
1595 1595 static void
1596 1596 usba_set_node_name(dev_info_t *dip, uint8_t class, uint8_t subclass,
1597 1597 uint8_t protocol, uint_t flag)
1598 1598 {
1599 1599 int i;
1600 1600 size_t size;
1601 1601 node_name_entry_t *node_name_table;
1602 1602
1603 1603 switch (flag) {
1604 1604 /* interface share node names with interface-association */
1605 1605 case FLAG_INTERFACE_ASSOCIATION_NODE:
1606 1606 node_name_table = ia_node_name_table;
1607 1607 size = ia_node_name_table_size;
1608 1608 break;
1609 1609 case FLAG_INTERFACE_NODE:
1610 1610 node_name_table = if_node_name_table;
1611 1611 size = if_node_name_table_size;
1612 1612 break;
1613 1613 case FLAG_DEVICE_NODE:
1614 1614 node_name_table = device_node_name_table;
1615 1615 size = device_node_name_table_size;
1616 1616 break;
1617 1617 case FLAG_COMBINED_NODE:
1618 1618 node_name_table = combined_node_name_table;
1619 1619 size = combined_node_name_table_size;
1620 1620 break;
1621 1621 default:
1622 1622
1623 1623 return;
1624 1624 }
1625 1625
1626 1626 for (i = 0; i < size; i++) {
1627 1627 int16_t c = node_name_table[i].class;
1628 1628 int16_t s = node_name_table[i].subclass;
1629 1629 int16_t p = node_name_table[i].protocol;
1630 1630
1631 1631 if (((c == DONTCARE) || (c == class)) &&
1632 1632 ((s == DONTCARE) || (s == subclass)) &&
1633 1633 ((p == DONTCARE) || (p == protocol))) {
1634 1634 char *name = node_name_table[i].name;
1635 1635
1636 1636 (void) ndi_devi_set_nodename(dip, name, 0);
1637 1637 break;
1638 1638 }
1639 1639 }
1640 1640 }
1641 1641
1642 1642
1643 1643 #ifdef DEBUG
1644 1644 /*
1645 1645 * walk the children of the parent of this devi and compare the
1646 1646 * name and reg property of each child. If there is a match
1647 1647 * return this node
1648 1648 */
1649 1649 static dev_info_t *
1650 1650 usba_find_existing_node(dev_info_t *odip)
1651 1651 {
1652 1652 dev_info_t *ndip, *child, *pdip;
1653 1653 int *odata, *ndata;
1654 1654 uint_t n_odata, n_ndata;
1655 1655 int circular;
1656 1656
1657 1657 pdip = ddi_get_parent(odip);
1658 1658 if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY,
1659 1659 odip, DDI_PROP_DONTPASS, "reg",
1660 1660 &odata, &n_odata) != DDI_SUCCESS) {
1661 1661 USB_DPRINTF_L2(DPRINT_MASK_HCDI, usba_log_handle,
1662 1662 "usba_find_existing_node: "
1663 1663 "%s: DDI_NOT_WELL_FORMED", ddi_driver_name(odip));
1664 1664
1665 1665 return (NULL);
1666 1666 }
1667 1667
1668 1668 ndi_devi_enter(pdip, &circular);
1669 1669 ndip = (dev_info_t *)(DEVI(pdip)->devi_child);
1670 1670 while ((child = ndip) != NULL) {
1671 1671
1672 1672 ndip = (dev_info_t *)(DEVI(child)->devi_sibling);
1673 1673
1674 1674 if (child == odip) {
1675 1675 continue;
1676 1676 }
1677 1677
1678 1678 if (strcmp(DEVI(child)->devi_node_name,
1679 1679 DEVI(odip)->devi_node_name)) {
1680 1680 continue;
1681 1681 }
1682 1682
1683 1683 if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY,
1684 1684 child, DDI_PROP_DONTPASS, "reg",
1685 1685 &ndata, &n_ndata) != DDI_SUCCESS) {
1686 1686
1687 1687 USB_DPRINTF_L2(DPRINT_MASK_HCDI, usba_log_handle,
1688 1688 "usba_find_existing_node: "
1689 1689 "%s DDI_NOT_WELL_FORMED", ddi_driver_name(child));
1690 1690
1691 1691 } else if (n_ndata && n_odata && (bcmp(odata, ndata,
1692 1692 max(n_odata, n_ndata) * sizeof (int)) == 0)) {
1693 1693
1694 1694 USB_DPRINTF_L3(DPRINT_MASK_HCDI, usba_log_handle,
1695 1695 "usba_find_existing_node: found %s%d (%p)",
1696 1696 ddi_driver_name(child),
1697 1697 ddi_get_instance(child), (void *)child);
1698 1698
1699 1699 USB_DPRINTF_L3(DPRINT_MASK_HCDI, usba_log_handle,
1700 1700 "usba_find_existing_node: "
1701 1701 "reg: %x %x %x - %x %x %x",
1702 1702 n_odata, odata[0], odata[1],
1703 1703 n_ndata, ndata[0], ndata[1]);
1704 1704
1705 1705 ddi_prop_free(ndata);
1706 1706 break;
1707 1707
1708 1708 } else {
1709 1709 ddi_prop_free(ndata);
1710 1710 }
1711 1711 }
1712 1712
1713 1713 ndi_devi_exit(pdip, circular);
1714 1714
1715 1715 ddi_prop_free(odata);
1716 1716
1717 1717 return (child);
1718 1718 }
1719 1719 #endif
1720 1720
1721 1721 /* change all unprintable characters to spaces */
1722 1722 static void
1723 1723 usba_filter_string(char *instr, char *outstr)
1724 1724 {
1725 1725 while (*instr) {
1726 1726 if ((*instr >= ' ') && (*instr <= '~')) {
1727 1727 *outstr = *instr;
1728 1728 } else {
1729 1729 *outstr = ' ';
1730 1730 }
1731 1731 outstr++;
1732 1732 instr++;
1733 1733 }
1734 1734 *outstr = '\0';
1735 1735 }
1736 1736
1737 1737
1738 1738 /*
1739 1739 * lookup ugen binding specified in property in
1740 1740 * hcd.conf files
1741 1741 */
1742 1742 int
1743 1743 usba_get_ugen_binding(dev_info_t *dip)
1744 1744 {
1745 1745 usba_device_t *usba_device = usba_get_usba_device(dip);
1746 1746 usba_hcdi_t *hcdi =
1747 1747 usba_hcdi_get_hcdi(usba_device->usb_root_hub_dip);
1748 1748
1749 1749 return (hcdi->hcdi_ugen_default_binding);
1750 1750 }
1751 1751
1752 1752
1753 1753 /*
1754 1754 * driver binding support at device level
1755 1755 */
1756 1756 dev_info_t *
1757 1757 usba_ready_device_node(dev_info_t *child_dip)
1758 1758 {
1759 1759 int rval, i;
1760 1760 int n = 0;
1761 1761 usba_device_t *usba_device = usba_get_usba_device(child_dip);
1762 1762 usb_dev_descr_t *usb_dev_descr;
1763 1763 uint_t n_cfgs; /* number of configs */
1764 1764 uint_t n_ifs; /* number of interfaces */
1765 1765 uint_t port, bus_num;
1766 1766 size_t usb_config_length;
1767 1767 uchar_t *usb_config;
1768 1768 int reg[1];
1769 1769 usb_addr_t address = usb_get_addr(child_dip);
1770 1770 usb_if_descr_t if_descr;
1771 1771 size_t size;
1772 1772 int combined_node = 0;
1773 1773 int is_hub;
1774 1774 char *devprop_str;
1775 1775 char *force_bind = NULL;
1776 1776 char *usba_name_buf = NULL;
1777 1777 char *usba_name[USBA_MAX_COMPAT_NAMES];
1778 1778
1779 1779 usb_config = usb_get_raw_cfg_data(child_dip, &usb_config_length);
1780 1780
1781 1781 mutex_enter(&usba_device->usb_mutex);
1782 1782 mutex_enter(&usba_mutex);
1783 1783
1784 1784 USB_DPRINTF_L4(DPRINT_MASK_USBA, usba_log_handle,
1785 1785 "usba_ready_device_node: child=0x%p", (void *)child_dip);
1786 1786
1787 1787 port = usba_device->usb_port;
1788 1788 usb_dev_descr = usba_device->usb_dev_descr;
1789 1789 n_cfgs = usba_device->usb_n_cfgs;
1790 1790 n_ifs = usba_device->usb_n_ifs;
1791 1791 bus_num = usba_device->usb_addr;
1792 1792
1793 1793 if (address != ROOT_HUB_ADDR) {
1794 1794 size = usb_parse_if_descr(
1795 1795 usb_config,
1796 1796 usb_config_length,
1797 1797 0, /* interface index */
1798 1798 0, /* alt interface index */
1799 1799 &if_descr,
1800 1800 USB_IF_DESCR_SIZE);
1801 1801
1802 1802 if (size != USB_IF_DESCR_SIZE) {
1803 1803 USB_DPRINTF_L2(DPRINT_MASK_USBA, usba_log_handle,
1804 1804 "parsing interface: "
1805 1805 "size (%lu) != USB_IF_DESCR_SIZE (%d)",
1806 1806 size, USB_IF_DESCR_SIZE);
1807 1807
1808 1808 mutex_exit(&usba_mutex);
1809 1809 mutex_exit(&usba_device->usb_mutex);
1810 1810
1811 1811 return (child_dip);
1812 1812 }
1813 1813 } else {
1814 1814 /* fake an interface descriptor for the root hub */
1815 1815 bzero(&if_descr, sizeof (if_descr));
1816 1816
1817 1817 if_descr.bInterfaceClass = USB_CLASS_HUB;
1818 1818 }
1819 1819
1820 1820 reg[0] = port;
1821 1821
1822 1822 mutex_exit(&usba_mutex);
1823 1823 mutex_exit(&usba_device->usb_mutex);
1824 1824
1825 1825 rval = ndi_prop_update_int_array(
1826 1826 DDI_DEV_T_NONE, child_dip, "reg", reg, 1);
1827 1827
1828 1828 if (rval != DDI_PROP_SUCCESS) {
1829 1829 USB_DPRINTF_L2(DPRINT_MASK_USBA, usba_log_handle,
1830 1830 "usba_ready_device_node: property update failed");
1831 1831
1832 1832 return (child_dip);
1833 1833 }
1834 1834
1835 1835 combined_node = ((n_cfgs == 1) && (n_ifs == 1) &&
1836 1836 ((usb_dev_descr->bDeviceClass == USB_CLASS_HUB) ||
1837 1837 (usb_dev_descr->bDeviceClass == 0)));
1838 1838
1839 1839 is_hub = (if_descr.bInterfaceClass == USB_CLASS_HUB) ||
1840 1840 (usb_dev_descr->bDeviceClass == USB_CLASS_HUB);
1841 1841
1842 1842 /* set node name */
1843 1843 if (combined_node) {
1844 1844 usba_set_node_name(child_dip,
1845 1845 if_descr.bInterfaceClass,
1846 1846 if_descr.bInterfaceSubClass,
1847 1847 if_descr.bInterfaceProtocol,
1848 1848 FLAG_COMBINED_NODE);
1849 1849 } else {
1850 1850 usba_set_node_name(child_dip,
1851 1851 usb_dev_descr->bDeviceClass,
1852 1852 usb_dev_descr->bDeviceSubClass,
1853 1853 usb_dev_descr->bDeviceProtocol,
1854 1854 FLAG_DEVICE_NODE);
1855 1855 }
1856 1856
1857 1857 /*
1858 1858 * check force binding rules
1859 1859 */
1860 1860 if ((address != ROOT_HUB_ADDR) && usba_ddivs_usbc &&
1861 1861 (address != usba_ddivs_usbc_xaddress) &&
1862 1862 (!(usba_ddivs_usbc_xhubs && is_hub))) {
1863 1863 force_bind = "ddivs_usbc";
1864 1864 (void) ndi_devi_set_nodename(child_dip, "ddivs_usbc", 0);
1865 1865
1866 1866 } else if (usba_device->usb_preferred_driver) {
1867 1867 force_bind = usba_device->usb_preferred_driver;
1868 1868
1869 1869 } else if ((address != ROOT_HUB_ADDR) &&
1870 1870 ((usba_ugen_force_binding == USBA_UGEN_DEVICE_BINDING) ||
1871 1871 ((usba_ugen_force_binding == USBA_UGEN_INTERFACE_BINDING) &&
1872 1872 combined_node)) && (!is_hub)) {
1873 1873 force_bind = "ugen";
1874 1874 }
1875 1875
1876 1876 #ifdef DEBUG
1877 1877 /*
1878 1878 * check whether there is another dip with this name and address
1879 1879 * If the dip contains usba_device, it is held by the previous
1880 1880 * round of configuration.
1881 1881 */
1882 1882 ASSERT(usba_find_existing_node(child_dip) == NULL);
1883 1883 #endif
1884 1884
1885 1885 usba_name_buf = kmem_zalloc(USBA_MAX_COMPAT_NAMES *
1886 1886 USBA_MAX_COMPAT_NAME_LEN, KM_SLEEP);
1887 1887
1888 1888 for (i = 0; i < USBA_MAX_COMPAT_NAMES; i++) {
1889 1889 usba_name[i] = usba_name_buf + (i * USBA_MAX_COMPAT_NAME_LEN);
1890 1890 }
1891 1891
1892 1892 if (force_bind) {
1893 1893 (void) ndi_devi_set_nodename(child_dip, force_bind, 0);
1894 1894 (void) strncpy(usba_name[n++], force_bind,
1895 1895 USBA_MAX_COMPAT_NAME_LEN);
1896 1896 }
1897 1897
1898 1898 /*
1899 1899 * If the callback function of specified driver is registered,
1900 1900 * it will be called here to check whether to take over the device.
1901 1901 */
1902 1902 if (usb_cap.usba_dev_driver_cb != NULL) {
1903 1903 char *dev_drv = NULL;
1904 1904 usb_dev_str_t dev_str;
1905 1905 char *pathname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1906 1906
1907 1907 dev_str.usb_mfg = usba_device->usb_mfg_str;
1908 1908 dev_str.usb_product = usba_device->usb_product_str;
1909 1909 dev_str.usb_serialno = usba_device->usb_serialno_str;
1910 1910
1911 1911 (void) ddi_pathname(child_dip, pathname);
1912 1912
1913 1913 if ((usb_cap.usba_dev_driver_cb(usb_dev_descr, &dev_str,
1914 1914 pathname, bus_num, port, &dev_drv, NULL) == USB_SUCCESS) &&
1915 1915 (dev_drv != NULL)) {
1916 1916 USB_DPRINTF_L3(DPRINT_MASK_USBA, usba_log_handle,
1917 1917 "usba_ready_device_node: dev_driver=%s, port =%d,"
1918 1918 "bus =%d, path=%s\n\t",
1919 1919 dev_drv, port, bus_num, pathname);
1920 1920
1921 1921 (void) strncpy(usba_name[n++], dev_drv,
1922 1922 USBA_MAX_COMPAT_NAME_LEN);
1923 1923 }
1924 1924 kmem_free(pathname, MAXPATHLEN);
1925 1925 }
1926 1926
1927 1927 /* create compatible names */
1928 1928 if (combined_node) {
1929 1929
1930 1930 /* 1. usbVID,PID.REV */
1931 1931 (void) sprintf(usba_name[n++],
1932 1932 "usb%x,%x.%x",
1933 1933 usb_dev_descr->idVendor,
1934 1934 usb_dev_descr->idProduct,
1935 1935 usb_dev_descr->bcdDevice);
1936 1936
1937 1937 /* 2. usbVID,PID */
1938 1938 (void) sprintf(usba_name[n++],
1939 1939 "usb%x,%x",
1940 1940 usb_dev_descr->idVendor,
1941 1941 usb_dev_descr->idProduct);
1942 1942
1943 1943 if (usb_dev_descr->bDeviceClass != 0) {
1944 1944 /* 3. usbVID,classDC.DSC.DPROTO */
1945 1945 (void) sprintf(usba_name[n++],
1946 1946 "usb%x,class%x.%x.%x",
1947 1947 usb_dev_descr->idVendor,
1948 1948 usb_dev_descr->bDeviceClass,
1949 1949 usb_dev_descr->bDeviceSubClass,
1950 1950 usb_dev_descr->bDeviceProtocol);
1951 1951
1952 1952 /* 4. usbVID,classDC.DSC */
1953 1953 (void) sprintf(usba_name[n++],
1954 1954 "usb%x,class%x.%x",
1955 1955 usb_dev_descr->idVendor,
1956 1956 usb_dev_descr->bDeviceClass,
1957 1957 usb_dev_descr->bDeviceSubClass);
1958 1958
1959 1959 /* 5. usbVID,classDC */
1960 1960 (void) sprintf(usba_name[n++],
1961 1961 "usb%x,class%x",
1962 1962 usb_dev_descr->idVendor,
1963 1963 usb_dev_descr->bDeviceClass);
1964 1964
1965 1965 /* 6. usb,classDC.DSC.DPROTO */
1966 1966 (void) sprintf(usba_name[n++],
1967 1967 "usb,class%x.%x.%x",
1968 1968 usb_dev_descr->bDeviceClass,
1969 1969 usb_dev_descr->bDeviceSubClass,
1970 1970 usb_dev_descr->bDeviceProtocol);
1971 1971
1972 1972 /* 7. usb,classDC.DSC */
1973 1973 (void) sprintf(usba_name[n++],
1974 1974 "usb,class%x.%x",
1975 1975 usb_dev_descr->bDeviceClass,
1976 1976 usb_dev_descr->bDeviceSubClass);
1977 1977
1978 1978 /* 8. usb,classDC */
1979 1979 (void) sprintf(usba_name[n++],
1980 1980 "usb,class%x",
1981 1981 usb_dev_descr->bDeviceClass);
1982 1982 }
1983 1983
1984 1984 if (if_descr.bInterfaceClass != 0) {
1985 1985 /* 9. usbifVID,classIC.ISC.IPROTO */
1986 1986 (void) sprintf(usba_name[n++],
1987 1987 "usbif%x,class%x.%x.%x",
1988 1988 usb_dev_descr->idVendor,
1989 1989 if_descr.bInterfaceClass,
1990 1990 if_descr.bInterfaceSubClass,
1991 1991 if_descr.bInterfaceProtocol);
1992 1992
1993 1993 /* 10. usbifVID,classIC.ISC */
1994 1994 (void) sprintf(usba_name[n++],
1995 1995 "usbif%x,class%x.%x",
1996 1996 usb_dev_descr->idVendor,
1997 1997 if_descr.bInterfaceClass,
1998 1998 if_descr.bInterfaceSubClass);
1999 1999
2000 2000 /* 11. usbifVID,classIC */
2001 2001 (void) sprintf(usba_name[n++],
2002 2002 "usbif%x,class%x",
2003 2003 usb_dev_descr->idVendor,
2004 2004 if_descr.bInterfaceClass);
2005 2005
2006 2006 /* 12. usbif,classIC.ISC.IPROTO */
2007 2007 (void) sprintf(usba_name[n++],
2008 2008 "usbif,class%x.%x.%x",
2009 2009 if_descr.bInterfaceClass,
2010 2010 if_descr.bInterfaceSubClass,
2011 2011 if_descr.bInterfaceProtocol);
2012 2012
2013 2013 /* 13. usbif,classIC.ISC */
2014 2014 (void) sprintf(usba_name[n++],
2015 2015 "usbif,class%x.%x",
2016 2016 if_descr.bInterfaceClass,
2017 2017 if_descr.bInterfaceSubClass);
2018 2018
2019 2019 /* 14. usbif,classIC */
2020 2020 (void) sprintf(usba_name[n++],
2021 2021 "usbif,class%x",
2022 2022 if_descr.bInterfaceClass);
2023 2023 }
2024 2024
2025 2025 /* 15. ugen or usb_mid */
2026 2026 if (usba_get_ugen_binding(child_dip) ==
2027 2027 USBA_UGEN_DEVICE_BINDING) {
2028 2028 (void) sprintf(usba_name[n++], "ugen");
2029 2029 } else {
2030 2030 (void) sprintf(usba_name[n++], "usb,device");
2031 2031 }
2032 2032
2033 2033 } else {
2034 2034 if (n_cfgs > 1) {
2035 2035 /* 1. usbVID,PID.REV.configCN */
2036 2036 (void) sprintf(usba_name[n++],
2037 2037 "usb%x,%x.%x.config%x",
2038 2038 usb_dev_descr->idVendor,
2039 2039 usb_dev_descr->idProduct,
2040 2040 usb_dev_descr->bcdDevice,
2041 2041 usba_device->usb_cfg_value);
2042 2042 }
2043 2043
2044 2044 /* 2. usbVID,PID.REV */
2045 2045 (void) sprintf(usba_name[n++],
2046 2046 "usb%x,%x.%x",
2047 2047 usb_dev_descr->idVendor,
2048 2048 usb_dev_descr->idProduct,
2049 2049 usb_dev_descr->bcdDevice);
2050 2050
2051 2051 /* 3. usbVID,PID.configCN */
2052 2052 if (n_cfgs > 1) {
2053 2053 (void) sprintf(usba_name[n++],
2054 2054 "usb%x,%x.%x",
2055 2055 usb_dev_descr->idVendor,
2056 2056 usb_dev_descr->idProduct,
2057 2057 usba_device->usb_cfg_value);
2058 2058 }
2059 2059
2060 2060 /* 4. usbVID,PID */
2061 2061 (void) sprintf(usba_name[n++],
2062 2062 "usb%x,%x",
2063 2063 usb_dev_descr->idVendor,
2064 2064 usb_dev_descr->idProduct);
2065 2065
2066 2066 if (usb_dev_descr->bDeviceClass != 0) {
2067 2067 /* 5. usbVID,classDC.DSC.DPROTO */
2068 2068 (void) sprintf(usba_name[n++],
2069 2069 "usb%x,class%x.%x.%x",
2070 2070 usb_dev_descr->idVendor,
2071 2071 usb_dev_descr->bDeviceClass,
2072 2072 usb_dev_descr->bDeviceSubClass,
2073 2073 usb_dev_descr->bDeviceProtocol);
2074 2074
2075 2075 /* 6. usbVID,classDC.DSC */
2076 2076 (void) sprintf(usba_name[n++],
2077 2077 "usb%x.class%x.%x",
2078 2078 usb_dev_descr->idVendor,
2079 2079 usb_dev_descr->bDeviceClass,
2080 2080 usb_dev_descr->bDeviceSubClass);
2081 2081
2082 2082 /* 7. usbVID,classDC */
2083 2083 (void) sprintf(usba_name[n++],
2084 2084 "usb%x.class%x",
2085 2085 usb_dev_descr->idVendor,
2086 2086 usb_dev_descr->bDeviceClass);
2087 2087
2088 2088 /* 8. usb,classDC.DSC.DPROTO */
2089 2089 (void) sprintf(usba_name[n++],
2090 2090 "usb,class%x.%x.%x",
2091 2091 usb_dev_descr->bDeviceClass,
2092 2092 usb_dev_descr->bDeviceSubClass,
2093 2093 usb_dev_descr->bDeviceProtocol);
2094 2094
2095 2095 /* 9. usb,classDC.DSC */
2096 2096 (void) sprintf(usba_name[n++],
2097 2097 "usb,class%x.%x",
2098 2098 usb_dev_descr->bDeviceClass,
2099 2099 usb_dev_descr->bDeviceSubClass);
2100 2100
2101 2101 /* 10. usb,classDC */
2102 2102 (void) sprintf(usba_name[n++],
2103 2103 "usb,class%x",
2104 2104 usb_dev_descr->bDeviceClass);
2105 2105 }
2106 2106
2107 2107 if (usba_get_ugen_binding(child_dip) ==
2108 2108 USBA_UGEN_DEVICE_BINDING) {
2109 2109 /* 11. ugen */
2110 2110 (void) sprintf(usba_name[n++], "ugen");
2111 2111 } else {
2112 2112 /* 11. usb,device */
2113 2113 (void) sprintf(usba_name[n++], "usb,device");
2114 2114 }
2115 2115 }
2116 2116
2117 2117 for (i = 0; i < n; i += 2) {
2118 2118 USB_DPRINTF_L3(DPRINT_MASK_USBA, usba_log_handle,
2119 2119 "compatible name:\t%s\t%s", usba_name[i],
2120 2120 (((i+1) < n)? usba_name[i+1] : ""));
2121 2121 }
2122 2122
2123 2123 rval = ndi_prop_update_string_array(DDI_DEV_T_NONE, child_dip,
2124 2124 "compatible", (char **)usba_name, n);
2125 2125
2126 2126 kmem_free(usba_name_buf, USBA_MAX_COMPAT_NAMES *
2127 2127 USBA_MAX_COMPAT_NAME_LEN);
2128 2128
2129 2129 if (rval != DDI_PROP_SUCCESS) {
2130 2130
2131 2131 USB_DPRINTF_L2(DPRINT_MASK_USBA, usba_log_handle,
2132 2132 "usba_ready_device_node: property update failed");
2133 2133
2134 2134 return (child_dip);
2135 2135 }
2136 2136
2137 2137 /* update the address property */
2138 2138 rval = ndi_prop_update_int(DDI_DEV_T_NONE, child_dip,
2139 2139 "assigned-address", usba_device->usb_addr);
2140 2140 if (rval != DDI_PROP_SUCCESS) {
2141 2141 USB_DPRINTF_L2(DPRINT_MASK_USBA, usba_log_handle,
2142 2142 "usba_ready_device_node: address update failed");
2143 2143 }
2144 2144
2145 2145 /* update the usb device properties (PSARC/2000/454) */
2146 2146 rval = ndi_prop_update_int(DDI_DEV_T_NONE, child_dip,
2147 2147 "usb-vendor-id", usb_dev_descr->idVendor);
2148 2148 if (rval != DDI_PROP_SUCCESS) {
2149 2149 USB_DPRINTF_L2(DPRINT_MASK_USBA, usba_log_handle,
2150 2150 "usba_ready_device_node: usb-vendor-id update failed");
2151 2151 }
2152 2152
2153 2153 rval = ndi_prop_update_int(DDI_DEV_T_NONE, child_dip,
2154 2154 "usb-product-id", usb_dev_descr->idProduct);
2155 2155 if (rval != DDI_PROP_SUCCESS) {
2156 2156 USB_DPRINTF_L2(DPRINT_MASK_USBA, usba_log_handle,
2157 2157 "usba_ready_device_node: usb-product-id update failed");
2158 2158 }
2159 2159
2160 2160 rval = ndi_prop_update_int(DDI_DEV_T_NONE, child_dip,
2161 2161 "usb-revision-id", usb_dev_descr->bcdDevice);
2162 2162 if (rval != DDI_PROP_SUCCESS) {
2163 2163 USB_DPRINTF_L2(DPRINT_MASK_USBA, usba_log_handle,
2164 2164 "usba_ready_device_node: usb-revision-id update failed");
2165 2165 }
2166 2166
2167 2167 rval = ndi_prop_update_int(DDI_DEV_T_NONE, child_dip,
2168 2168 "usb-num-configs", usb_dev_descr->bNumConfigurations);
2169 2169 if (rval != DDI_PROP_SUCCESS) {
2170 2170 USB_DPRINTF_L2(DPRINT_MASK_USBA, usba_log_handle,
2171 2171 "usba_ready_device_node: usb-num-configs update failed");
2172 2172 }
2173 2173
2174 2174 rval = ndi_prop_update_int(DDI_DEV_T_NONE, child_dip,
2175 2175 "usb-release", usb_dev_descr->bcdUSB);
2176 2176 if (rval != DDI_PROP_SUCCESS) {
2177 2177 USB_DPRINTF_L2(DPRINT_MASK_USBA, usba_log_handle,
2178 2178 "usba_ready_device_node: usb-release update failed");
2179 2179 }
2180 2180
2181 2181 rval = ndi_prop_update_byte_array(DDI_DEV_T_NONE, child_dip,
2182 2182 "usb-dev-descriptor", (uchar_t *)usb_dev_descr,
2183 2183 sizeof (usb_dev_descr_t));
2184 2184 if (rval != DDI_PROP_SUCCESS) {
2185 2185 USB_DPRINTF_L2(DPRINT_MASK_USBA, usba_log_handle,
2186 2186 "usba_ready_device_node: usb-descriptor update failed");
2187 2187 }
2188 2188
2189 2189 rval = ndi_prop_update_byte_array(DDI_DEV_T_NONE, child_dip,
2190 2190 "usb-raw-cfg-descriptors", usb_config, usb_config_length);
2191 2191 if (rval != DDI_PROP_SUCCESS) {
2192 2192 USB_DPRINTF_L2(DPRINT_MASK_USBA, usba_log_handle,
2193 2193 "usba_ready_device_node: usb-raw-cfg-descriptors update "
2194 2194 "failed");
2195 2195 }
2196 2196
2197 2197 devprop_str = kmem_zalloc(USB_MAXSTRINGLEN, KM_SLEEP);
2198 2198
2199 2199 if (usba_device->usb_serialno_str) {
2200 2200 usba_filter_string(usba_device->usb_serialno_str, devprop_str);
2201 2201 rval = ndi_prop_update_string(DDI_DEV_T_NONE, child_dip,
2202 2202 "usb-serialno", devprop_str);
2203 2203 if (rval != DDI_PROP_SUCCESS) {
2204 2204 USB_DPRINTF_L2(DPRINT_MASK_USBA, usba_log_handle,
2205 2205 "usba_ready_device_node: "
2206 2206 "usb-serialno update failed");
2207 2207 }
2208 2208 }
2209 2209
2210 2210 if (usba_device->usb_mfg_str) {
2211 2211 usba_filter_string(usba_device->usb_mfg_str, devprop_str);
2212 2212 rval = ndi_prop_update_string(DDI_DEV_T_NONE, child_dip,
2213 2213 "usb-vendor-name", devprop_str);
2214 2214 if (rval != DDI_PROP_SUCCESS) {
2215 2215 USB_DPRINTF_L2(DPRINT_MASK_USBA, usba_log_handle,
2216 2216 "usba_ready_device_node: "
2217 2217 "usb-vendor-name update failed");
2218 2218 }
2219 2219 }
2220 2220
2221 2221 if (usba_device->usb_product_str) {
2222 2222 usba_filter_string(usba_device->usb_product_str, devprop_str);
2223 2223 rval = ndi_prop_update_string(DDI_DEV_T_NONE, child_dip,
2224 2224 "usb-product-name", devprop_str);
2225 2225 if (rval != DDI_PROP_SUCCESS) {
2226 2226 USB_DPRINTF_L2(DPRINT_MASK_USBA, usba_log_handle,
2227 2227 "usba_ready_device_node: "
2228 2228 "usb-product-name update failed");
2229 2229 }
2230 2230 }
2231 2231
2232 2232 kmem_free(devprop_str, USB_MAXSTRINGLEN);
2233 2233
2234 2234 if (!combined_node) {
2235 2235 /* update the configuration property */
2236 2236 rval = ndi_prop_update_int(DDI_DEV_T_NONE, child_dip,
2237 2237 "configuration#", usba_device->usb_cfg_value);
2238 2238 if (rval != DDI_PROP_SUCCESS) {
2239 2239 USB_DPRINTF_L2(DPRINT_MASK_USBA, usba_log_handle,
2240 2240 "usba_ready_device_node: "
2241 2241 "config prop update failed");
2242 2242 }
2243 2243 }
2244 2244
2245 2245 if (usba_device->usb_port_status == USBA_LOW_SPEED_DEV) {
2246 2246 /* create boolean property */
2247 2247 rval = ndi_prop_create_boolean(DDI_DEV_T_NONE, child_dip,
2248 2248 "low-speed");
2249 2249 if (rval != DDI_PROP_SUCCESS) {
2250 2250 USB_DPRINTF_L2(DPRINT_MASK_USBA, usba_log_handle,
2251 2251 "usba_ready_device_node: "
2252 2252 "low speed prop update failed");
2253 2253 }
2254 2254 }
2255 2255
2256 2256 if (usba_device->usb_port_status == USBA_HIGH_SPEED_DEV) {
2257 2257 /* create boolean property */
2258 2258 rval = ndi_prop_create_boolean(DDI_DEV_T_NONE, child_dip,
2259 2259 "high-speed");
2260 2260 if (rval != DDI_PROP_SUCCESS) {
2261 2261 USB_DPRINTF_L2(DPRINT_MASK_USBA, usba_log_handle,
2262 2262 "usba_ready_device_node: "
2263 2263 "high speed prop update failed");
2264 2264 }
2265 2265 }
2266 2266
2267 2267 USB_DPRINTF_L4(DPRINT_MASK_USBA, usba_log_handle,
2268 2268 "%s%d at port %d: %s, dip=0x%p",
2269 2269 ddi_node_name(ddi_get_parent(child_dip)),
2270 2270 ddi_get_instance(ddi_get_parent(child_dip)),
2271 2271 port, ddi_node_name(child_dip), (void *)child_dip);
2272 2272
2273 2273 usba_set_usba_device(child_dip, usba_device);
2274 2274
2275 2275 ASSERT(!mutex_owned(&(usba_get_usba_device(child_dip)->usb_mutex)));
2276 2276
2277 2277 return (child_dip);
2278 2278 }
2279 2279
2280 2280
2281 2281 /*
2282 2282 * driver binding at interface association level. the first arg is the parent
2283 2283 * dip. if_count returns amount of interfaces which are associated within
2284 2284 * this interface-association that starts from first_if.
2285 2285 */
2286 2286 /*ARGSUSED*/
2287 2287 dev_info_t *
2288 2288 usba_ready_interface_association_node(dev_info_t *dip,
2289 2289 uint_t first_if,
2290 2290 uint_t *if_count)
2291 2291 {
2292 2292 dev_info_t *child_dip = NULL;
2293 2293 usba_device_t *child_ud = usba_get_usba_device(dip);
2294 2294 usb_dev_descr_t *usb_dev_descr;
2295 2295 size_t usb_cfg_length;
2296 2296 uchar_t *usb_cfg;
2297 2297 usb_ia_descr_t ia_descr;
2298 2298 int i, n, rval;
2299 2299 int reg[2];
2300 2300 size_t size;
2301 2301 usb_port_status_t port_status;
2302 2302 char *force_bind = NULL;
2303 2303 char *usba_name_buf = NULL;
2304 2304 char *usba_name[USBA_MAX_COMPAT_NAMES];
2305 2305
2306 2306 usb_cfg = usb_get_raw_cfg_data(dip, &usb_cfg_length);
2307 2307
2308 2308 mutex_enter(&child_ud->usb_mutex);
2309 2309
2310 2310 usb_dev_descr = child_ud->usb_dev_descr;
2311 2311
2312 2312 /*
2313 2313 * for each interface association, determine all compatible names
2314 2314 */
2315 2315 USB_DPRINTF_L3(DPRINT_MASK_USBA, usba_log_handle,
2316 2316 "usba_ready_ia_node: "
2317 2317 "port %d, interface = %d, port_status = %x",
2318 2318 child_ud->usb_port, first_if, child_ud->usb_port_status);
2319 2319
2320 2320 /* Parse the interface descriptor */
2321 2321 size = usb_parse_ia_descr(
2322 2322 usb_cfg,
2323 2323 usb_cfg_length,
2324 2324 first_if, /* interface index */
2325 2325 &ia_descr,
2326 2326 USB_IA_DESCR_SIZE);
2327 2327
2328 2328 *if_count = 1;
2329 2329 if (size != USB_IA_DESCR_SIZE) {
2330 2330 USB_DPRINTF_L2(DPRINT_MASK_USBA, usba_log_handle,
2331 2331 "parsing ia: size (%lu) != USB_IA_DESCR_SIZE (%d)",
2332 2332 size, USB_IA_DESCR_SIZE);
2333 2333 mutex_exit(&child_ud->usb_mutex);
2334 2334
2335 2335 return (NULL);
2336 2336 }
2337 2337
2338 2338 port_status = child_ud->usb_port_status;
2339 2339
2340 2340 /* create reg property */
2341 2341 reg[0] = first_if;
2342 2342 reg[1] = child_ud->usb_cfg_value;
2343 2343
2344 2344 mutex_exit(&child_ud->usb_mutex);
2345 2345
2346 2346 /* clone this dip */
2347 2347 rval = usba_create_child_devi(dip,
2348 2348 "interface-association",
2349 2349 NULL, /* usba_hcdi ops */
2350 2350 NULL, /* root hub dip */
2351 2351 port_status, /* port status */
2352 2352 child_ud, /* share this usba_device */
2353 2353 &child_dip);
2354 2354
2355 2355 if (rval != USB_SUCCESS) {
2356 2356
2357 2357 goto fail;
2358 2358 }
2359 2359
2360 2360 rval = ndi_prop_update_int_array(
2361 2361 DDI_DEV_T_NONE, child_dip, "reg", reg, 2);
2362 2362
2363 2363 if (rval != DDI_PROP_SUCCESS) {
2364 2364
2365 2365 goto fail;
2366 2366 }
2367 2367
2368 2368 usba_set_node_name(child_dip, ia_descr.bFunctionClass,
2369 2369 ia_descr.bFunctionSubClass, ia_descr.bFunctionProtocol,
2370 2370 FLAG_INTERFACE_ASSOCIATION_NODE);
2371 2371
2372 2372 /* check force binding */
2373 2373 if (usba_ugen_force_binding ==
2374 2374 USBA_UGEN_INTERFACE_ASSOCIATION_BINDING) {
2375 2375 force_bind = "ugen";
2376 2376 }
2377 2377
2378 2378 /*
2379 2379 * check whether there is another dip with this name and address
2380 2380 */
2381 2381 ASSERT(usba_find_existing_node(child_dip) == NULL);
2382 2382
2383 2383 usba_name_buf = kmem_zalloc(USBA_MAX_COMPAT_NAMES *
2384 2384 USBA_MAX_COMPAT_NAME_LEN, KM_SLEEP);
2385 2385
2386 2386 for (i = 0; i < USBA_MAX_COMPAT_NAMES; i++) {
2387 2387 usba_name[i] = usba_name_buf + (i * USBA_MAX_COMPAT_NAME_LEN);
2388 2388 }
2389 2389
2390 2390 n = 0;
2391 2391
2392 2392 if (force_bind) {
2393 2393 (void) ndi_devi_set_nodename(child_dip, force_bind, 0);
2394 2394 (void) strncpy(usba_name[n++], force_bind,
2395 2395 USBA_MAX_COMPAT_NAME_LEN);
2396 2396 }
2397 2397
2398 2398 /* 1) usbiaVID,PID.REV.configCN.FN */
2399 2399 (void) sprintf(usba_name[n++],
2400 2400 "usbia%x,%x.%x.config%x.%x",
2401 2401 usb_dev_descr->idVendor,
2402 2402 usb_dev_descr->idProduct,
2403 2403 usb_dev_descr->bcdDevice,
2404 2404 child_ud->usb_cfg_value,
2405 2405 first_if);
2406 2406
2407 2407 /* 2) usbiaVID,PID.configCN.FN */
2408 2408 (void) sprintf(usba_name[n++],
2409 2409 "usbia%x,%x.config%x.%x",
2410 2410 usb_dev_descr->idVendor,
2411 2411 usb_dev_descr->idProduct,
2412 2412 child_ud->usb_cfg_value,
2413 2413 first_if);
2414 2414
2415 2415
2416 2416 if (ia_descr.bFunctionClass) {
2417 2417 /* 3) usbiaVID,classFC.FSC.FPROTO */
2418 2418 (void) sprintf(usba_name[n++],
2419 2419 "usbia%x,class%x.%x.%x",
2420 2420 usb_dev_descr->idVendor,
2421 2421 ia_descr.bFunctionClass,
2422 2422 ia_descr.bFunctionSubClass,
2423 2423 ia_descr.bFunctionProtocol);
2424 2424
2425 2425 /* 4) usbiaVID,classFC.FSC */
2426 2426 (void) sprintf(usba_name[n++],
2427 2427 "usbia%x,class%x.%x",
2428 2428 usb_dev_descr->idVendor,
2429 2429 ia_descr.bFunctionClass,
2430 2430 ia_descr.bFunctionSubClass);
2431 2431
2432 2432 /* 5) usbiaVID,classFC */
2433 2433 (void) sprintf(usba_name[n++],
2434 2434 "usbia%x,class%x",
2435 2435 usb_dev_descr->idVendor,
2436 2436 ia_descr.bFunctionClass);
2437 2437
2438 2438 /* 6) usbia,classFC.FSC.FPROTO */
2439 2439 (void) sprintf(usba_name[n++],
2440 2440 "usbia,class%x.%x.%x",
2441 2441 ia_descr.bFunctionClass,
2442 2442 ia_descr.bFunctionSubClass,
2443 2443 ia_descr.bFunctionProtocol);
2444 2444
2445 2445 /* 7) usbia,classFC.FSC */
2446 2446 (void) sprintf(usba_name[n++],
2447 2447 "usbia,class%x.%x",
2448 2448 ia_descr.bFunctionClass,
2449 2449 ia_descr.bFunctionSubClass);
2450 2450
2451 2451 /* 8) usbia,classFC */
2452 2452 (void) sprintf(usba_name[n++],
2453 2453 "usbia,class%x",
2454 2454 ia_descr.bFunctionClass);
2455 2455 }
2456 2456
2457 2457 if (usba_get_ugen_binding(child_dip) ==
2458 2458 USBA_UGEN_INTERFACE_ASSOCIATION_BINDING) {
2459 2459 /* 9) ugen */
2460 2460 (void) sprintf(usba_name[n++], "ugen");
2461 2461 } else {
2462 2462
2463 2463 (void) sprintf(usba_name[n++], "usb,ia");
2464 2464 }
2465 2465
2466 2466 for (i = 0; i < n; i += 2) {
2467 2467 USB_DPRINTF_L3(DPRINT_MASK_USBA, usba_log_handle,
2468 2468 "compatible name:\t%s\t%s", usba_name[i],
2469 2469 (((i+1) < n)? usba_name[i+1] : ""));
2470 2470 }
2471 2471
2472 2472 /* create compatible property */
2473 2473 rval = ndi_prop_update_string_array(DDI_DEV_T_NONE, child_dip,
2474 2474 "compatible", (char **)usba_name, n);
2475 2475
2476 2476 kmem_free(usba_name_buf, USBA_MAX_COMPAT_NAMES *
2477 2477 USBA_MAX_COMPAT_NAME_LEN);
2478 2478
2479 2479 if (rval != DDI_PROP_SUCCESS) {
2480 2480
2481 2481 goto fail;
2482 2482 }
2483 2483
2484 2484 /* update the address property */
2485 2485 rval = ndi_prop_update_int(DDI_DEV_T_NONE, child_dip,
2486 2486 "assigned-address", child_ud->usb_addr);
2487 2487 if (rval != DDI_PROP_SUCCESS) {
2488 2488 USB_DPRINTF_L2(DPRINT_MASK_USBA, usba_log_handle,
2489 2489 "usba_ready_interface_node: address update failed");
2490 2490 }
2491 2491
2492 2492 /* create property with first interface number */
2493 2493 rval = ndi_prop_update_int(DDI_DEV_T_NONE, child_dip,
2494 2494 "interface", ia_descr.bFirstInterface);
2495 2495
2496 2496 if (rval != DDI_PROP_SUCCESS) {
2497 2497
2498 2498 goto fail;
2499 2499 }
2500 2500
2501 2501 /* create property with the count of interfaces in this ia */
2502 2502 rval = ndi_prop_update_int(DDI_DEV_T_NONE, child_dip,
2503 2503 "interface-count", ia_descr.bInterfaceCount);
2504 2504
2505 2505 if (rval != DDI_PROP_SUCCESS) {
2506 2506
2507 2507 goto fail;
2508 2508 }
2509 2509
2510 2510 USB_DPRINTF_L2(DPRINT_MASK_USBA, usba_log_handle,
2511 2511 "%s%d port %d: %s, dip = 0x%p",
2512 2512 ddi_node_name(ddi_get_parent(dip)),
2513 2513 ddi_get_instance(ddi_get_parent(dip)),
2514 2514 child_ud->usb_port, ddi_node_name(child_dip), (void *)child_dip);
2515 2515
2516 2516 *if_count = ia_descr.bInterfaceCount;
2517 2517 usba_set_usba_device(child_dip, child_ud);
2518 2518 ASSERT(!mutex_owned(&(usba_get_usba_device(child_dip)->usb_mutex)));
2519 2519
2520 2520 return (child_dip);
2521 2521
2522 2522 fail:
2523 2523 (void) usba_destroy_child_devi(child_dip, NDI_DEVI_REMOVE);
2524 2524
2525 2525 return (NULL);
2526 2526 }
2527 2527
2528 2528
2529 2529 /*
2530 2530 * driver binding at interface level, the first arg will be the
2531 2531 * the parent dip
2532 2532 */
2533 2533 /*ARGSUSED*/
2534 2534 dev_info_t *
2535 2535 usba_ready_interface_node(dev_info_t *dip, uint_t intf)
2536 2536 {
2537 2537 dev_info_t *child_dip = NULL;
2538 2538 usba_device_t *child_ud = usba_get_usba_device(dip);
2539 2539 usb_dev_descr_t *usb_dev_descr;
2540 2540 size_t usb_cfg_length;
2541 2541 uchar_t *usb_cfg;
2542 2542 usb_if_descr_t if_descr;
2543 2543 int i, n, rval;
2544 2544 int reg[2];
2545 2545 size_t size;
2546 2546 usb_port_status_t port_status;
2547 2547 char *force_bind = NULL;
2548 2548 char *usba_name_buf = NULL;
2549 2549 char *usba_name[USBA_MAX_COMPAT_NAMES];
2550 2550
2551 2551 usb_cfg = usb_get_raw_cfg_data(dip, &usb_cfg_length);
2552 2552
2553 2553 mutex_enter(&child_ud->usb_mutex);
2554 2554
2555 2555 usb_dev_descr = child_ud->usb_dev_descr;
2556 2556
2557 2557 /*
2558 2558 * for each interface, determine all compatible names
2559 2559 */
2560 2560 USB_DPRINTF_L3(DPRINT_MASK_USBA, usba_log_handle,
2561 2561 "usba_ready_interface_node: "
2562 2562 "port %d, interface = %d port status = %x",
2563 2563 child_ud->usb_port, intf, child_ud->usb_port_status);
2564 2564
2565 2565 /* Parse the interface descriptor */
2566 2566 size = usb_parse_if_descr(
2567 2567 usb_cfg,
2568 2568 usb_cfg_length,
2569 2569 intf, /* interface index */
2570 2570 0, /* alt interface index */
2571 2571 &if_descr,
2572 2572 USB_IF_DESCR_SIZE);
2573 2573
2574 2574 if (size != USB_IF_DESCR_SIZE) {
2575 2575 USB_DPRINTF_L2(DPRINT_MASK_USBA, usba_log_handle,
2576 2576 "parsing interface: size (%lu) != USB_IF_DESCR_SIZE (%d)",
2577 2577 size, USB_IF_DESCR_SIZE);
2578 2578 mutex_exit(&child_ud->usb_mutex);
2579 2579
2580 2580 return (NULL);
2581 2581 }
2582 2582
2583 2583 port_status = child_ud->usb_port_status;
2584 2584
2585 2585 /* create reg property */
2586 2586 reg[0] = intf;
2587 2587 reg[1] = child_ud->usb_cfg_value;
2588 2588
2589 2589 mutex_exit(&child_ud->usb_mutex);
2590 2590
2591 2591 /* clone this dip */
2592 2592 rval = usba_create_child_devi(dip,
2593 2593 "interface",
2594 2594 NULL, /* usba_hcdi ops */
2595 2595 NULL, /* root hub dip */
2596 2596 port_status, /* port status */
2597 2597 child_ud, /* share this usba_device */
2598 2598 &child_dip);
2599 2599
2600 2600 if (rval != USB_SUCCESS) {
2601 2601
2602 2602 goto fail;
2603 2603 }
2604 2604
2605 2605 rval = ndi_prop_update_int_array(
2606 2606 DDI_DEV_T_NONE, child_dip, "reg", reg, 2);
2607 2607
2608 2608 if (rval != DDI_PROP_SUCCESS) {
2609 2609
2610 2610 goto fail;
2611 2611 }
2612 2612
2613 2613 usba_set_node_name(child_dip, if_descr.bInterfaceClass,
2614 2614 if_descr.bInterfaceSubClass, if_descr.bInterfaceProtocol,
2615 2615 FLAG_INTERFACE_NODE);
2616 2616
2617 2617 /* check force binding */
2618 2618 if (usba_ugen_force_binding == USBA_UGEN_INTERFACE_BINDING) {
2619 2619 force_bind = "ugen";
2620 2620 }
2621 2621
2622 2622 /*
2623 2623 * check whether there is another dip with this name and address
2624 2624 */
2625 2625 ASSERT(usba_find_existing_node(child_dip) == NULL);
2626 2626
2627 2627 usba_name_buf = kmem_zalloc(USBA_MAX_COMPAT_NAMES *
2628 2628 USBA_MAX_COMPAT_NAME_LEN, KM_SLEEP);
2629 2629
2630 2630 for (i = 0; i < USBA_MAX_COMPAT_NAMES; i++) {
2631 2631 usba_name[i] = usba_name_buf + (i * USBA_MAX_COMPAT_NAME_LEN);
2632 2632 }
2633 2633
2634 2634 n = 0;
2635 2635
2636 2636 if (force_bind) {
2637 2637 (void) ndi_devi_set_nodename(child_dip, force_bind, 0);
2638 2638 (void) strncpy(usba_name[n++], force_bind,
2639 2639 USBA_MAX_COMPAT_NAME_LEN);
2640 2640 }
2641 2641
2642 2642 /* 1) usbifVID,PID.REV.configCN.IN */
2643 2643 (void) sprintf(usba_name[n++],
2644 2644 "usbif%x,%x.%x.config%x.%x",
2645 2645 usb_dev_descr->idVendor,
2646 2646 usb_dev_descr->idProduct,
2647 2647 usb_dev_descr->bcdDevice,
2648 2648 child_ud->usb_cfg_value,
2649 2649 intf);
2650 2650
2651 2651 /* 2) usbifVID,PID.configCN.IN */
2652 2652 (void) sprintf(usba_name[n++],
2653 2653 "usbif%x,%x.config%x.%x",
2654 2654 usb_dev_descr->idVendor,
2655 2655 usb_dev_descr->idProduct,
2656 2656 child_ud->usb_cfg_value,
2657 2657 intf);
2658 2658
2659 2659
2660 2660 if (if_descr.bInterfaceClass) {
2661 2661 /* 3) usbifVID,classIC.ISC.IPROTO */
2662 2662 (void) sprintf(usba_name[n++],
2663 2663 "usbif%x,class%x.%x.%x",
2664 2664 usb_dev_descr->idVendor,
2665 2665 if_descr.bInterfaceClass,
2666 2666 if_descr.bInterfaceSubClass,
2667 2667 if_descr.bInterfaceProtocol);
2668 2668
2669 2669 /* 4) usbifVID,classIC.ISC */
2670 2670 (void) sprintf(usba_name[n++],
2671 2671 "usbif%x,class%x.%x",
2672 2672 usb_dev_descr->idVendor,
2673 2673 if_descr.bInterfaceClass,
2674 2674 if_descr.bInterfaceSubClass);
2675 2675
2676 2676 /* 5) usbifVID,classIC */
2677 2677 (void) sprintf(usba_name[n++],
2678 2678 "usbif%x,class%x",
2679 2679 usb_dev_descr->idVendor,
2680 2680 if_descr.bInterfaceClass);
2681 2681
2682 2682 /* 6) usbif,classIC.ISC.IPROTO */
2683 2683 (void) sprintf(usba_name[n++],
2684 2684 "usbif,class%x.%x.%x",
2685 2685 if_descr.bInterfaceClass,
2686 2686 if_descr.bInterfaceSubClass,
2687 2687 if_descr.bInterfaceProtocol);
2688 2688
2689 2689 /* 7) usbif,classIC.ISC */
2690 2690 (void) sprintf(usba_name[n++],
2691 2691 "usbif,class%x.%x",
2692 2692 if_descr.bInterfaceClass,
2693 2693 if_descr.bInterfaceSubClass);
2694 2694
2695 2695 /* 8) usbif,classIC */
2696 2696 (void) sprintf(usba_name[n++],
2697 2697 "usbif,class%x",
2698 2698 if_descr.bInterfaceClass);
2699 2699 }
2700 2700
2701 2701 if (usba_get_ugen_binding(child_dip) ==
2702 2702 USBA_UGEN_INTERFACE_BINDING) {
2703 2703 /* 9) ugen */
2704 2704 (void) sprintf(usba_name[n++], "ugen");
2705 2705 }
2706 2706
2707 2707 for (i = 0; i < n; i += 2) {
2708 2708 USB_DPRINTF_L3(DPRINT_MASK_USBA, usba_log_handle,
2709 2709 "compatible name:\t%s\t%s", usba_name[i],
2710 2710 (((i+1) < n)? usba_name[i+1] : ""));
2711 2711 }
2712 2712
2713 2713 /* create compatible property */
2714 2714 rval = ndi_prop_update_string_array(DDI_DEV_T_NONE, child_dip,
2715 2715 "compatible", (char **)usba_name, n);
2716 2716
2717 2717 kmem_free(usba_name_buf, USBA_MAX_COMPAT_NAMES *
2718 2718 USBA_MAX_COMPAT_NAME_LEN);
2719 2719
2720 2720 if (rval != DDI_PROP_SUCCESS) {
2721 2721
2722 2722 goto fail;
2723 2723 }
2724 2724
2725 2725 /* update the address property */
2726 2726 rval = ndi_prop_update_int(DDI_DEV_T_NONE, child_dip,
2727 2727 "assigned-address", child_ud->usb_addr);
2728 2728 if (rval != DDI_PROP_SUCCESS) {
2729 2729 USB_DPRINTF_L2(DPRINT_MASK_USBA, usba_log_handle,
2730 2730 "usba_ready_interface_node: address update failed");
2731 2731 }
2732 2732
2733 2733 /* create property with if number */
2734 2734 rval = ndi_prop_update_int(DDI_DEV_T_NONE, child_dip,
2735 2735 "interface", intf);
2736 2736
2737 2737 if (rval != DDI_PROP_SUCCESS) {
2738 2738
2739 2739 goto fail;
2740 2740 }
2741 2741
2742 2742 USB_DPRINTF_L2(DPRINT_MASK_USBA, usba_log_handle,
2743 2743 "%s%d port %d: %s, dip = 0x%p",
2744 2744 ddi_node_name(ddi_get_parent(dip)),
2745 2745 ddi_get_instance(ddi_get_parent(dip)),
2746 2746 child_ud->usb_port, ddi_node_name(child_dip), (void *)child_dip);
2747 2747
2748 2748 usba_set_usba_device(child_dip, child_ud);
2749 2749 ASSERT(!mutex_owned(&(usba_get_usba_device(child_dip)->usb_mutex)));
2750 2750
2751 2751 return (child_dip);
2752 2752
2753 2753 fail:
2754 2754 (void) usba_destroy_child_devi(child_dip, NDI_DEVI_REMOVE);
2755 2755
2756 2756 return (NULL);
2757 2757 }
2758 2758
2759 2759
2760 2760 /*
2761 2761 * retrieve string descriptors for manufacturer, vendor and serial
2762 2762 * number
2763 2763 */
2764 2764 void
2765 2765 usba_get_dev_string_descrs(dev_info_t *dip, usba_device_t *ud)
2766 2766 {
2767 2767 char *tmpbuf, *str;
2768 2768 int l;
2769 2769 usb_dev_descr_t *usb_dev_descr = ud->usb_dev_descr;
2770 2770
2771 2771
2772 2772 USB_DPRINTF_L4(DPRINT_MASK_USBA, usba_log_handle,
2773 2773 "usba_get_usb_string_descr: m=%d, p=%d, s=%d",
2774 2774 usb_dev_descr->iManufacturer,
2775 2775 usb_dev_descr->iProduct,
2776 2776 usb_dev_descr->iSerialNumber);
2777 2777
2778 2778 tmpbuf = kmem_zalloc(USB_MAXSTRINGLEN, KM_SLEEP);
2779 2779
2780 2780 /* fetch manufacturer string */
2781 2781 if ((ud->usb_mfg_str == NULL) && usb_dev_descr->iManufacturer &&
2782 2782 (usb_get_string_descr(dip, USB_LANG_ID,
2783 2783 usb_dev_descr->iManufacturer, tmpbuf, USB_MAXSTRINGLEN) ==
2784 2784 USB_SUCCESS)) {
2785 2785
2786 2786 l = strlen(tmpbuf);
2787 2787 if (l > 0) {
2788 2788 str = kmem_zalloc(l + 1, KM_SLEEP);
2789 2789 mutex_enter(&ud->usb_mutex);
2790 2790 ud->usb_mfg_str = str;
2791 2791 (void) strcpy(ud->usb_mfg_str, tmpbuf);
2792 2792 mutex_exit(&ud->usb_mutex);
2793 2793 }
2794 2794 }
2795 2795
2796 2796 /* fetch product string */
2797 2797 if ((ud->usb_product_str == NULL) && usb_dev_descr->iProduct &&
2798 2798 (usb_get_string_descr(dip, USB_LANG_ID, usb_dev_descr->iProduct,
2799 2799 tmpbuf, USB_MAXSTRINGLEN) ==
2800 2800 USB_SUCCESS)) {
2801 2801
2802 2802 l = strlen(tmpbuf);
2803 2803 if (l > 0) {
2804 2804 str = kmem_zalloc(l + 1, KM_SLEEP);
2805 2805 mutex_enter(&ud->usb_mutex);
2806 2806 ud->usb_product_str = str;
2807 2807 (void) strcpy(ud->usb_product_str, tmpbuf);
2808 2808 mutex_exit(&ud->usb_mutex);
2809 2809 }
2810 2810 }
2811 2811
2812 2812 /* fetch device serial number string */
2813 2813 if ((ud->usb_serialno_str == NULL) && usb_dev_descr->iSerialNumber &&
2814 2814 (usb_get_string_descr(dip, USB_LANG_ID,
2815 2815 usb_dev_descr->iSerialNumber, tmpbuf, USB_MAXSTRINGLEN) ==
2816 2816 USB_SUCCESS)) {
2817 2817
2818 2818 l = strlen(tmpbuf);
2819 2819 if (l > 0) {
2820 2820 str = kmem_zalloc(l + 1, KM_SLEEP);
2821 2821 mutex_enter(&ud->usb_mutex);
2822 2822 ud->usb_serialno_str = str;
2823 2823 (void) strcpy(ud->usb_serialno_str, tmpbuf);
2824 2824 mutex_exit(&ud->usb_mutex);
2825 2825 }
2826 2826 }
2827 2827
2828 2828 kmem_free(tmpbuf, USB_MAXSTRINGLEN);
2829 2829 }
2830 2830
2831 2831
2832 2832 /*
2833 2833 * usba_str_startcmp:
2834 2834 * Return the number of characters duplicated from the beginning of the
2835 2835 * string. Return -1 if a complete duplicate.
2836 2836 *
2837 2837 * Arguments:
2838 2838 * Two strings to compare.
2839 2839 */
2840 2840 static int usba_str_startcmp(char *first, char *second)
2841 2841 {
2842 2842 int num_same_chars = 0;
2843 2843 while (*first == *second++) {
2844 2844 if (*first++ == '\0') {
2845 2845 return (-1);
2846 2846 }
2847 2847 num_same_chars++;
2848 2848 }
2849 2849
2850 2850 return (num_same_chars);
2851 2851 }
2852 2852
2853 2853
2854 2854 /*
2855 2855 * usba_get_mfg_prod_sn_str:
2856 2856 * Return a string containing mfg, product, serial number strings.
2857 2857 * Remove duplicates if some strings are the same.
2858 2858 *
2859 2859 * Arguments:
2860 2860 * dip - pointer to dev info
2861 2861 * buffer - Where string is returned
2862 2862 * buflen - Length of buffer
2863 2863 *
2864 2864 * Returns:
2865 2865 * Same as second arg.
2866 2866 */
2867 2867 char *
2868 2868 usba_get_mfg_prod_sn_str(
2869 2869 dev_info_t *dip,
2870 2870 char *buffer,
2871 2871 int buflen)
2872 2872 {
2873 2873 usba_device_t *usba_device = usba_get_usba_device(dip);
2874 2874 int return_len = 0;
2875 2875 int len = 0;
2876 2876 int duplen;
2877 2877
2878 2878 buffer[0] = '\0';
2879 2879 buffer[buflen-1] = '\0';
2880 2880
2881 2881 if ((usba_device->usb_mfg_str) &&
2882 2882 ((len = strlen(usba_device->usb_mfg_str)) != 0)) {
2883 2883 (void) strncpy(buffer, usba_device->usb_mfg_str, buflen - 1);
2884 2884 return_len = min(buflen - 1, len);
2885 2885 }
2886 2886
2887 2887 /* Product string exists to append. */
2888 2888 if ((usba_device->usb_product_str) &&
2889 2889 ((len = strlen(usba_device->usb_product_str)) != 0)) {
2890 2890
2891 2891 /* Append only parts of string that don't match mfg string. */
2892 2892 duplen = usba_str_startcmp(buffer,
2893 2893 usba_device->usb_product_str);
2894 2894
2895 2895 if (duplen != -1) { /* Not a complete match. */
2896 2896 if (return_len > 0) {
2897 2897 buffer[return_len++] = ' ';
2898 2898 }
2899 2899
2900 2900 /* Skip over the dup part of the concat'ed string. */
2901 2901 len -= duplen;
2902 2902 (void) strncpy(&buffer[return_len],
2903 2903 &usba_device->usb_product_str[duplen],
2904 2904 buflen - return_len - 1);
2905 2905 return_len = min(buflen - 1, return_len + len);
2906 2906 }
2907 2907 }
2908 2908
2909 2909 if ((usba_device->usb_serialno_str) &&
2910 2910 ((len = strlen(usba_device->usb_serialno_str)) != 0)) {
2911 2911 if (return_len > 0) {
2912 2912 buffer[return_len++] = ' ';
2913 2913 }
2914 2914 (void) strncpy(&buffer[return_len],
2915 2915 usba_device->usb_serialno_str,
2916 2916 buflen - return_len - 1);
2917 2917 }
2918 2918
2919 2919 return (buffer);
2920 2920 }
2921 2921
2922 2922
2923 2923 /*
2924 2924 * USB enumeration statistic functions
2925 2925 */
2926 2926
2927 2927 /*
2928 2928 * Increments the hotplug statistics based on flags.
2929 2929 */
2930 2930 void
2931 2931 usba_update_hotplug_stats(dev_info_t *dip, usb_flags_t flags)
2932 2932 {
2933 2933 usba_device_t *usba_device = usba_get_usba_device(dip);
2934 2934 usba_hcdi_t *hcdi =
2935 2935 usba_hcdi_get_hcdi(usba_device->usb_root_hub_dip);
2936 2936
2937 2937 mutex_enter(&hcdi->hcdi_mutex);
2938 2938 if (flags & USBA_TOTAL_HOTPLUG_SUCCESS) {
2939 2939 hcdi->hcdi_total_hotplug_success++;
2940 2940 HCDI_HOTPLUG_STATS_DATA(hcdi)->
2941 2941 hcdi_hotplug_total_success.value.ui64++;
2942 2942 }
2943 2943 if (flags & USBA_HOTPLUG_SUCCESS) {
2944 2944 hcdi->hcdi_hotplug_success++;
2945 2945 HCDI_HOTPLUG_STATS_DATA(hcdi)->
2946 2946 hcdi_hotplug_success.value.ui64++;
2947 2947 }
2948 2948 if (flags & USBA_TOTAL_HOTPLUG_FAILURE) {
2949 2949 hcdi->hcdi_total_hotplug_failure++;
2950 2950 HCDI_HOTPLUG_STATS_DATA(hcdi)->
2951 2951 hcdi_hotplug_total_failure.value.ui64++;
2952 2952 }
2953 2953 if (flags & USBA_HOTPLUG_FAILURE) {
2954 2954 hcdi->hcdi_hotplug_failure++;
2955 2955 HCDI_HOTPLUG_STATS_DATA(hcdi)->
2956 2956 hcdi_hotplug_failure.value.ui64++;
2957 2957 }
2958 2958 mutex_exit(&hcdi->hcdi_mutex);
2959 2959 }
2960 2960
2961 2961
2962 2962 /*
2963 2963 * Retrieve the current enumeration statistics
2964 2964 */
2965 2965 void
2966 2966 usba_get_hotplug_stats(dev_info_t *dip, ulong_t *total_success,
2967 2967 ulong_t *success, ulong_t *total_failure, ulong_t *failure,
2968 2968 uchar_t *device_count)
2969 2969 {
2970 2970 usba_device_t *usba_device = usba_get_usba_device(dip);
2971 2971 usba_hcdi_t *hcdi =
2972 2972 usba_hcdi_get_hcdi(usba_device->usb_root_hub_dip);
2973 2973
2974 2974 mutex_enter(&hcdi->hcdi_mutex);
2975 2975 *total_success = hcdi->hcdi_total_hotplug_success;
2976 2976 *success = hcdi->hcdi_hotplug_success;
2977 2977 *total_failure = hcdi->hcdi_total_hotplug_failure;
2978 2978 *failure = hcdi->hcdi_hotplug_failure;
2979 2979 *device_count = hcdi->hcdi_device_count;
2980 2980 mutex_exit(&hcdi->hcdi_mutex);
2981 2981 }
2982 2982
2983 2983
2984 2984 /*
2985 2985 * Reset the resetable hotplug stats
2986 2986 */
2987 2987 void
2988 2988 usba_reset_hotplug_stats(dev_info_t *dip)
2989 2989 {
2990 2990 usba_device_t *usba_device = usba_get_usba_device(dip);
2991 2991 usba_hcdi_t *hcdi =
2992 2992 usba_hcdi_get_hcdi(usba_device->usb_root_hub_dip);
2993 2993 hcdi_hotplug_stats_t *hsp;
2994 2994
2995 2995 mutex_enter(&hcdi->hcdi_mutex);
2996 2996 hcdi->hcdi_hotplug_success = 0;
2997 2997 hcdi->hcdi_hotplug_failure = 0;
2998 2998
2999 2999 hsp = HCDI_HOTPLUG_STATS_DATA(hcdi);
3000 3000 hsp->hcdi_hotplug_success.value.ui64 = 0;
3001 3001 hsp->hcdi_hotplug_failure.value.ui64 = 0;
3002 3002 mutex_exit(&hcdi->hcdi_mutex);
3003 3003 }
3004 3004
3005 3005
3006 3006 /*
3007 3007 * usba_bind_driver():
3008 3008 * This function calls ndi_devi_bind_driver() which tries to
3009 3009 * bind a driver to the device. If the driver binding fails
3010 3010 * we get an rval of NDI_UNBOUD and report an error to the
3011 3011 * syslog that the driver failed binding.
3012 3012 * If rval is something other than NDI_UNBOUND we report an
3013 3013 * error to the console.
3014 3014 *
3015 3015 * This function returns USB_SUCCESS if no errors were
3016 3016 * encountered while binding.
3017 3017 */
3018 3018 int
3019 3019 usba_bind_driver(dev_info_t *dip)
3020 3020 {
3021 3021 int rval;
3022 3022 char *name;
3023 3023 uint8_t if_num = usba_get_ifno(dip);
3024 3024
3025 3025 USB_DPRINTF_L4(DPRINT_MASK_USBA, usba_log_handle,
3026 3026 "usba_bind_driver: dip = 0x%p, if_num = 0x%x", (void *)dip, if_num);
3027 3027
3028 3028 name = kmem_zalloc(MAXNAMELEN, KM_SLEEP);
3029 3029
3030 3030 /* bind device to the driver */
3031 3031 if ((rval = ndi_devi_bind_driver(dip, 0)) != NDI_SUCCESS) {
3032 3032 /* if we fail to bind report an error */
3033 3033 (void) usba_get_mfg_prod_sn_str(dip, name, MAXNAMELEN);
3034 3034 if (name[0] != '\0') {
3035 3035 if (!usb_owns_device(dip)) {
3036 3036 USB_DPRINTF_L1(DPRINT_MASK_USBA,
3037 3037 usba_log_handle,
3038 3038 "no driver found for "
3039 3039 "interface %d (nodename: '%s') of %s",
3040 3040 if_num, ddi_node_name(dip), name);
3041 3041 } else {
3042 3042 USB_DPRINTF_L1(DPRINT_MASK_USBA,
3043 3043 usba_log_handle,
3044 3044 "no driver found for device %s", name);
3045 3045 }
3046 3046 } else {
3047 3047 (void) ddi_pathname(dip, name);
3048 3048 USB_DPRINTF_L1(DPRINT_MASK_USBA,
3049 3049 usba_log_handle,
3050 3050 "no driver found for device %s", name);
3051 3051 }
3052 3052
3053 3053 kmem_free(name, MAXNAMELEN);
3054 3054
3055 3055 return (USB_FAILURE);
3056 3056 }
3057 3057 kmem_free(name, MAXNAMELEN);
3058 3058
3059 3059 return ((rval == NDI_SUCCESS) ? USB_SUCCESS : USB_FAILURE);
3060 3060 }
3061 3061
3062 3062
3063 3063 /*
3064 3064 * usba_get_hc_dma_attr:
3065 3065 * function returning dma attributes of the HCD
3066 3066 *
3067 3067 * Arguments:
3068 3068 * dip - pointer to devinfo of the client
3069 3069 *
3070 3070 * Return Values:
3071 3071 * hcdi_dma_attr
3072 3072 */
3073 3073 ddi_dma_attr_t *
3074 3074 usba_get_hc_dma_attr(dev_info_t *dip)
3075 3075 {
3076 3076 usba_device_t *usba_device = usba_get_usba_device(dip);
3077 3077 usba_hcdi_t *hcdi = usba_hcdi_get_hcdi(usba_device->usb_root_hub_dip);
3078 3078
3079 3079 return (hcdi->hcdi_dma_attr);
3080 3080 }
3081 3081
3082 3082
3083 3083 /*
3084 3084 * usba_check_for_leaks:
3085 3085 * check usba_device structure for leaks
3086 3086 *
3087 3087 * Arguments:
3088 3088 * usba_device - usba_device structure pointer
3089 3089 */
3090 3090 void
3091 3091 usba_check_for_leaks(usba_device_t *usba_device)
3092 3092 {
3093 3093 int i, ph_open_cnt, req_wrp_leaks, iface;
3094 3094 int leaks = 0;
3095 3095
3096 3096 USB_DPRINTF_L4(DPRINT_MASK_USBA, usba_log_handle,
3097 3097 "usba_check_for_leaks: %s%d usba_device=0x%p",
3098 3098 ddi_driver_name(usba_device->usb_dip),
3099 3099 ddi_get_instance(usba_device->usb_dip), (void *)usba_device);
3100 3100
3101 3101 /*
3102 3102 * default pipe is still open
3103 3103 * all other pipes should be closed
3104 3104 */
3105 3105 for (ph_open_cnt = 0, i = 1; i < USBA_N_ENDPOINTS; i++) {
3106 3106 usba_ph_impl_t *ph_impl =
3107 3107 &usba_device->usb_ph_list[i];
3108 3108 if (ph_impl->usba_ph_data) {
3109 3109 USB_DPRINTF_L2(DPRINT_MASK_USBA,
3110 3110 usba_log_handle,
3111 3111 "%s%d: leaking pipehandle=0x%p (0x%p) ep_addr=0x%x",
3112 3112 ddi_driver_name(ph_impl->usba_ph_data->p_dip),
3113 3113 ddi_get_instance(ph_impl->usba_ph_data->p_dip),
3114 3114 (void *)ph_impl,
3115 3115 (void *)ph_impl->usba_ph_data,
3116 3116 ph_impl->usba_ph_ep.bEndpointAddress);
3117 3117 ph_open_cnt++;
3118 3118 leaks++;
3119 3119 #ifndef DEBUG
3120 3120 usb_pipe_close(ph_impl->usba_ph_data->p_dip,
3121 3121 (usb_pipe_handle_t)ph_impl, USB_FLAGS_SLEEP,
3122 3122 NULL, NULL);
3123 3123 #endif
3124 3124 }
3125 3125 }
3126 3126 req_wrp_leaks = usba_list_entry_leaks(&usba_device->
3127 3127 usb_allocated, "request wrappers");
3128 3128
3129 3129 ASSERT(ph_open_cnt == 0);
3130 3130 ASSERT(req_wrp_leaks == 0);
3131 3131
3132 3132 if (req_wrp_leaks) {
3133 3133 usba_list_entry_t *entry;
3134 3134
3135 3135 while ((entry = usba_rm_first_from_list(
3136 3136 &usba_device->usb_allocated)) != NULL) {
3137 3137 usba_req_wrapper_t *wrp;
3138 3138
3139 3139 mutex_enter(&entry->list_mutex);
3140 3140 wrp = (usba_req_wrapper_t *)entry->private;
3141 3141 mutex_exit(&entry->list_mutex);
3142 3142 leaks++;
3143 3143
3144 3144 USB_DPRINTF_L2(DPRINT_MASK_USBA,
3145 3145 usba_log_handle,
3146 3146 "%s%d: leaking request 0x%p",
3147 3147 ddi_driver_name(wrp->wr_dip),
3148 3148 ddi_get_instance(wrp->wr_dip),
3149 3149 (void *)wrp->wr_req);
3150 3150
3151 3151 /*
3152 3152 * put it back, usba_req_wrapper_free
3153 3153 * expects it on the list
3154 3154 */
3155 3155 usba_add_to_list(&usba_device->usb_allocated,
3156 3156 &wrp->wr_allocated_list);
3157 3157
3158 3158 usba_req_wrapper_free(wrp);
3159 3159 }
3160 3160 }
3161 3161
3162 3162 mutex_enter(&usba_device->usb_mutex);
3163 3163 for (iface = 0; iface < usba_device->usb_n_ifs; iface++) {
3164 3164 USB_DPRINTF_L3(DPRINT_MASK_USBA, usba_log_handle,
3165 3165 "usba_check_for_leaks: if=%d client_flags=0x%x",
3166 3166 iface, usba_device->usb_client_flags[iface]);
3167 3167
3168 3168 if (usba_device->usb_client_flags[iface] &
3169 3169 USBA_CLIENT_FLAG_DEV_DATA) {
3170 3170 usb_client_dev_data_list_t *entry =
3171 3171 usba_device->usb_client_dev_data_list.cddl_next;
3172 3172 usb_client_dev_data_list_t *next;
3173 3173 usb_client_dev_data_t *dev_data;
3174 3174
3175 3175 while (entry) {
3176 3176 dev_info_t *dip = entry->cddl_dip;
3177 3177 next = entry->cddl_next;
3178 3178 dev_data = entry->cddl_dev_data;
3179 3179
3180 3180
3181 3181 if (!i_ddi_devi_attached(dip)) {
3182 3182 USB_DPRINTF_L2(DPRINT_MASK_USBA,
3183 3183 usba_log_handle,
3184 3184 "%s%d: leaking dev_data 0x%p",
3185 3185 ddi_driver_name(dip),
3186 3186 ddi_get_instance(dip),
3187 3187 (void *)dev_data);
3188 3188
3189 3189 leaks++;
3190 3190
3191 3191 mutex_exit(&usba_device->usb_mutex);
3192 3192 usb_free_dev_data(dip, dev_data);
3193 3193 mutex_enter(&usba_device->usb_mutex);
3194 3194 }
3195 3195
3196 3196 entry = next;
3197 3197 }
3198 3198 }
3199 3199 if (usba_device->usb_client_flags[iface] &
3200 3200 USBA_CLIENT_FLAG_ATTACH) {
3201 3201 dev_info_t *dip = usba_device->
3202 3202 usb_client_attach_list[iface].dip;
3203 3203
3204 3204 USB_DPRINTF_L2(DPRINT_MASK_USBA,
3205 3205 usba_log_handle,
3206 3206 "%s%d: did no usb_client_detach",
3207 3207 ddi_driver_name(dip), ddi_get_instance(dip));
3208 3208 leaks++;
3209 3209
3210 3210 mutex_exit(&usba_device->usb_mutex);
3211 3211 usb_client_detach(dip, NULL);
3212 3212 mutex_enter(&usba_device->usb_mutex);
3213 3213
3214 3214 usba_device->
3215 3215 usb_client_attach_list[iface].dip = NULL;
3216 3216
3217 3217 usba_device->usb_client_flags[iface] &=
3218 3218 ~USBA_CLIENT_FLAG_ATTACH;
3219 3219
3220 3220 }
3221 3221 if (usba_device->usb_client_flags[iface] &
3222 3222 USBA_CLIENT_FLAG_EV_CBS) {
3223 3223 dev_info_t *dip =
3224 3224 usba_device->usb_client_ev_cb_list[iface].
3225 3225 dip;
3226 3226 usb_event_t *ev_data =
3227 3227 usba_device->usb_client_ev_cb_list[iface].
3228 3228 ev_data;
3229 3229
3230 3230 USB_DPRINTF_L2(DPRINT_MASK_USBA,
3231 3231 usba_log_handle,
3232 3232 "%s%d: did no usb_unregister_event_cbs",
3233 3233 ddi_driver_name(dip), ddi_get_instance(dip));
3234 3234 leaks++;
3235 3235
3236 3236 mutex_exit(&usba_device->usb_mutex);
3237 3237 usb_unregister_event_cbs(dip, ev_data);
3238 3238 mutex_enter(&usba_device->usb_mutex);
3239 3239
3240 3240 usba_device->usb_client_ev_cb_list[iface].
3241 3241 dip = NULL;
3242 3242 usba_device->usb_client_ev_cb_list[iface].
3243 3243 ev_data = NULL;
3244 3244 usba_device->usb_client_flags[iface] &=
3245 3245 ~USBA_CLIENT_FLAG_EV_CBS;
3246 3246 }
3247 3247 }
3248 3248 mutex_exit(&usba_device->usb_mutex);
3249 3249
3250 3250 if (leaks) {
3251 3251 USB_DPRINTF_L2(DPRINT_MASK_USBA, usba_log_handle,
3252 3252 "all %d leaks fixed", leaks);
3253 3253 }
3254 3254 }
↓ open down ↓ |
3144 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX