1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 * 21 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 22 * Use is subject to license terms. 23 */ 24 25 #pragma ident "%Z%%M% %I% %E% SMI" 26 27 #include <devfsadm.h> 28 #include <stdio.h> 29 #include <stdlib.h> 30 #include <limits.h> 31 #include <string.h> 32 #include <unistd.h> 33 #include <sys/types.h> 34 #include <sys/stat.h> 35 #include <strings.h> 36 37 extern char *devfsadm_get_devices_dir(); 38 static int usb_process(di_minor_t minor, di_node_t node); 39 40 static void ugen_create_link(char *p_path, char *node_name, 41 di_node_t node, di_minor_t minor); 42 43 44 /* Rules for creating links */ 45 static devfsadm_create_t usb_cbt[] = { 46 { "usb", NULL, "usb_ac", DRV_EXACT, 47 ILEVEL_0, usb_process }, 48 { "usb", NULL, "usb_as", DRV_EXACT, 49 ILEVEL_0, usb_process }, 50 { "usb", NULL, "ddivs_usbc", DRV_EXACT, 51 ILEVEL_0, usb_process }, 52 { "usb", NULL, "hid", DRV_EXACT, 53 ILEVEL_0, usb_process }, 54 { "usb", DDI_NT_NEXUS, "hubd", DRV_EXACT|TYPE_EXACT, 55 ILEVEL_0, usb_process }, 56 { "usb", DDI_NT_NEXUS, "ohci", DRV_EXACT|TYPE_EXACT, 57 ILEVEL_0, usb_process }, 58 { "usb", DDI_NT_NEXUS, "ehci", DRV_EXACT|TYPE_EXACT, 59 ILEVEL_0, usb_process }, 60 { "usb", DDI_NT_SCSI_NEXUS, "scsa2usb", DRV_EXACT|TYPE_EXACT, 61 ILEVEL_0, usb_process }, 62 { "usb", DDI_NT_UGEN, "scsa2usb", DRV_EXACT|TYPE_EXACT, 63 ILEVEL_0, usb_process }, 64 { "usb", DDI_NT_NEXUS, "uhci", DRV_EXACT|TYPE_EXACT, 65 ILEVEL_0, usb_process }, 66 { "usb", DDI_NT_UGEN, "ugen", DRV_EXACT|TYPE_EXACT, 67 ILEVEL_0, usb_process }, 68 { "usb", DDI_NT_NEXUS, "usb_mid", DRV_EXACT|TYPE_EXACT, 69 ILEVEL_0, usb_process }, 70 { "usb", DDI_NT_UGEN, "usb_mid", DRV_EXACT|TYPE_EXACT, 71 ILEVEL_0, usb_process }, 72 { "usb", DDI_NT_PRINTER, "usbprn", DRV_EXACT|TYPE_EXACT, 73 ILEVEL_0, usb_process }, 74 { "usb", DDI_NT_UGEN, "usbprn", DRV_EXACT|TYPE_EXACT, 75 ILEVEL_0, usb_process }, 76 }; 77 78 /* For debug printing (-V filter) */ 79 static char *debug_mid = "usb_mid"; 80 81 DEVFSADM_CREATE_INIT_V0(usb_cbt); 82 83 /* USB device links */ 84 #define USB_LINK_RE_AUDIO "^usb/audio[0-9]+$" 85 #define USB_LINK_RE_AUDIOMUX "^usb/audio-mux[0-9]+$" 86 #define USB_LINK_RE_AUDIOCTL "^usb/audio-control[0-9]+$" 87 #define USB_LINK_RE_AUDIOSTREAM "^usb/audio-stream[0-9]+$" 88 #define USB_LINK_RE_DDIVS_USBC "^usb/ddivs_usbc[0-9]+$" 89 #define USB_LINK_RE_DEVICE "^usb/device[0-9]+$" 90 #define USB_LINK_RE_HID "^usb/hid[0-9]+$" 91 #define USB_LINK_RE_HUB "^usb/hub[0-9]+$" 92 #define USB_LINK_RE_MASS_STORE "^usb/mass-storage[0-9]+$" 93 #define USB_LINK_RE_UGEN "^usb/[0-9,a-f]+\\.[0-9,a-f]+/[0-9]+/.+$" 94 #define USB_LINK_RE_USBPRN "^usb/printer[0-9]+$" 95 96 /* Rules for removing links */ 97 static devfsadm_remove_t usb_remove_cbt[] = { 98 { "usb", USB_LINK_RE_AUDIO, RM_POST | RM_HOT | RM_ALWAYS, ILEVEL_0, 99 devfsadm_rm_all }, 100 { "usb", USB_LINK_RE_AUDIOMUX, RM_POST | RM_HOT | RM_ALWAYS, ILEVEL_0, 101 devfsadm_rm_all }, 102 { "usb", USB_LINK_RE_AUDIOCTL, RM_POST | RM_HOT | RM_ALWAYS, ILEVEL_0, 103 devfsadm_rm_all }, 104 { "usb", USB_LINK_RE_AUDIOSTREAM, RM_POST | RM_HOT | RM_ALWAYS, 105 ILEVEL_0, devfsadm_rm_all }, 106 { "usb", USB_LINK_RE_DDIVS_USBC, RM_POST | RM_HOT | RM_ALWAYS, 107 ILEVEL_0, devfsadm_rm_all }, 108 { "usb", USB_LINK_RE_DEVICE, RM_POST | RM_HOT, ILEVEL_0, 109 devfsadm_rm_all }, 110 { "usb", USB_LINK_RE_HID, RM_POST | RM_HOT | RM_ALWAYS, ILEVEL_0, 111 devfsadm_rm_all }, 112 { "usb", USB_LINK_RE_HUB, RM_POST | RM_HOT, ILEVEL_0, devfsadm_rm_all }, 113 { "usb", USB_LINK_RE_MASS_STORE, RM_POST | RM_HOT | RM_ALWAYS, 114 ILEVEL_0, devfsadm_rm_all }, 115 { "usb", USB_LINK_RE_UGEN, RM_POST | RM_HOT | RM_ALWAYS, ILEVEL_0, 116 devfsadm_rm_all }, 117 { "usb", USB_LINK_RE_USBPRN, RM_POST | RM_HOT | RM_ALWAYS, ILEVEL_0, 118 devfsadm_rm_link }, 119 }; 120 121 /* 122 * Rules for different USB devices except ugen which is dynamically 123 * created 124 */ 125 static devfsadm_enumerate_t audio_rules[1] = 126 {"^usb$/^audio([0-9]+)$", 1, MATCH_ALL}; 127 static devfsadm_enumerate_t audio_mux_rules[1] = 128 {"^usb$/^audio-mux([0-9]+)$", 1, MATCH_ALL}; 129 static devfsadm_enumerate_t audio_control_rules[1] = 130 {"^usb$/^audio-control([0-9]+)$", 1, MATCH_ALL}; 131 static devfsadm_enumerate_t audio_stream_rules[1] = 132 {"^usb$/^audio-stream([0-9]+)$", 1, MATCH_ALL}; 133 static devfsadm_enumerate_t ddivs_usbc_rules[1] = 134 {"^usb$/^ddivs_usbc([0-9]+)$", 1, MATCH_ALL}; 135 static devfsadm_enumerate_t device_rules[1] = 136 {"^usb$/^device([0-9]+)$", 1, MATCH_ALL}; 137 static devfsadm_enumerate_t hid_rules[1] = 138 {"^usb$/^hid([0-9]+)$", 1, MATCH_ALL}; 139 static devfsadm_enumerate_t hub_rules[1] = 140 {"^usb$/^hub([0-9]+)$", 1, MATCH_ALL}; 141 static devfsadm_enumerate_t mass_storage_rules[1] = 142 {"^usb$/^mass-storage([0-9]+)$", 1, MATCH_ALL}; 143 static devfsadm_enumerate_t usbprn_rules[1] = 144 {"^usb$/^printer([0-9]+)$", 1, MATCH_ALL}; 145 146 DEVFSADM_REMOVE_INIT_V0(usb_remove_cbt); 147 148 int 149 minor_init(void) 150 { 151 devfsadm_print(debug_mid, "usb_link: minor_init\n"); 152 return (DEVFSADM_SUCCESS); 153 } 154 155 int 156 minor_fini(void) 157 { 158 devfsadm_print(debug_mid, "usb_link: minor_fini\n"); 159 return (DEVFSADM_SUCCESS); 160 } 161 162 typedef enum { 163 DRIVER_HUBD = 0, 164 DRIVER_OHCI = 1, 165 DRIVER_EHCI = 2, 166 DRIVER_UHCI = 3, 167 DRIVER_USB_AC = 4, 168 DRIVER_USB_AS = 5, 169 DRIVER_HID = 6, 170 DRIVER_USB_MID = 7, 171 DRIVER_DDIVS_USBC = 8, 172 DRIVER_SCSA2USB = 9, 173 DRIVER_USBPRN = 10, 174 DRIVER_UGEN = 11, 175 DRIVER_UNKNOWN = 12 176 } driver_defs_t; 177 178 typedef struct { 179 char *driver_name; 180 int index; 181 } driver_name_table_entry_t; 182 183 driver_name_table_entry_t driver_name_table[] = { 184 { "hubd", DRIVER_HUBD }, 185 { "ohci", DRIVER_OHCI }, 186 { "ehci", DRIVER_EHCI }, 187 { "uhci", DRIVER_UHCI }, 188 { "usb_ac", DRIVER_USB_AC }, 189 { "usb_as", DRIVER_USB_AS }, 190 { "hid", DRIVER_HID }, 191 { "usb_mid", DRIVER_USB_MID }, 192 { "ddivs_usbc", DRIVER_DDIVS_USBC }, 193 { "scsa2usb", DRIVER_SCSA2USB }, 194 { "usbprn", DRIVER_USBPRN }, 195 { "ugen", DRIVER_UGEN }, 196 { NULL, DRIVER_UNKNOWN } 197 }; 198 199 200 /* 201 * This function is called for every usb minor node. 202 * Calls enumerate to assign a logical usb id, and then 203 * devfsadm_mklink to make the link. 204 */ 205 static int 206 usb_process(di_minor_t minor, di_node_t node) 207 { 208 devfsadm_enumerate_t rules[1]; 209 char *l_path, *p_path, *buf, *devfspath; 210 char *minor_nm, *drvr_nm, *name = (char *)NULL; 211 int i, index; 212 int flags = 0; 213 int create_secondary_link = 0; 214 215 minor_nm = di_minor_name(minor); 216 drvr_nm = di_driver_name(node); 217 if ((minor_nm == NULL) || (drvr_nm == NULL)) { 218 return (DEVFSADM_CONTINUE); 219 } 220 221 devfsadm_print(debug_mid, "usb_process: minor=%s node=%s type=%s\n", 222 minor_nm, di_node_name(node), di_minor_nodetype(minor)); 223 224 devfspath = di_devfs_path(node); 225 if (devfspath == NULL) { 226 devfsadm_print(debug_mid, 227 "USB_process: devfspath is NULL\n"); 228 return (DEVFSADM_CONTINUE); 229 } 230 231 l_path = (char *)malloc(PATH_MAX); 232 if (l_path == NULL) { 233 di_devfs_path_free(devfspath); 234 devfsadm_print(debug_mid, "usb_process: malloc() failed\n"); 235 return (DEVFSADM_CONTINUE); 236 } 237 238 p_path = (char *)malloc(PATH_MAX); 239 if (p_path == NULL) { 240 devfsadm_print(debug_mid, "usb_process: malloc() failed\n"); 241 di_devfs_path_free(devfspath); 242 free(l_path); 243 return (DEVFSADM_CONTINUE); 244 } 245 246 (void) strcpy(p_path, devfspath); 247 (void) strcat(p_path, ":"); 248 (void) strcat(p_path, minor_nm); 249 di_devfs_path_free(devfspath); 250 251 devfsadm_print(debug_mid, "usb_process: path %s\n", p_path); 252 253 for (i = 0; ; i++) { 254 if ((driver_name_table[i].driver_name == NULL) || 255 (strcmp(drvr_nm, driver_name_table[i].driver_name) == 0)) { 256 index = driver_name_table[i].index; 257 break; 258 } 259 } 260 261 if (strcmp(di_minor_nodetype(minor), DDI_NT_UGEN) == 0) { 262 ugen_create_link(p_path, minor_nm, node, minor); 263 free(l_path); 264 free(p_path); 265 return (DEVFSADM_CONTINUE); 266 } 267 268 /* Figure out which rules to apply */ 269 switch (index) { 270 case DRIVER_HUBD: 271 case DRIVER_OHCI: 272 case DRIVER_EHCI: 273 case DRIVER_UHCI: 274 rules[0] = hub_rules[0]; /* For HUBs */ 275 name = "hub"; 276 277 break; 278 case DRIVER_USB_AC: 279 if (strcmp(minor_nm, "sound,audio") == 0) { 280 rules[0] = audio_rules[0]; 281 name = "audio"; /* For audio */ 282 create_secondary_link = 1; 283 } else if (strcmp(minor_nm, "sound,audioctl") == 0) { 284 rules[0] = audio_control_rules[0]; 285 name = "audio-control"; /* For audio */ 286 create_secondary_link = 1; 287 } else if (strcmp(minor_nm, "mux") == 0) { 288 rules[0] = audio_mux_rules[0]; 289 name = "audio-mux"; /* For audio */ 290 } else { 291 free(l_path); 292 free(p_path); 293 return (DEVFSADM_CONTINUE); 294 } 295 break; 296 case DRIVER_USB_AS: 297 rules[0] = audio_stream_rules[0]; 298 name = "audio-stream"; /* For audio */ 299 break; 300 case DRIVER_HID: 301 rules[0] = hid_rules[0]; 302 name = "hid"; /* For HIDs */ 303 break; 304 case DRIVER_USB_MID: 305 rules[0] = device_rules[0]; 306 name = "device"; /* For other USB devices */ 307 break; 308 case DRIVER_DDIVS_USBC: 309 rules[0] = ddivs_usbc_rules[0]; 310 name = "device"; /* For other USB devices */ 311 break; 312 case DRIVER_SCSA2USB: 313 rules[0] = mass_storage_rules[0]; 314 name = "mass-storage"; /* For mass-storage devices */ 315 break; 316 case DRIVER_USBPRN: 317 rules[0] = usbprn_rules[0]; 318 name = "printer"; 319 break; 320 default: 321 devfsadm_print(debug_mid, "usb_process: unknown driver=%s\n", 322 drvr_nm); 323 free(l_path); 324 free(p_path); 325 return (DEVFSADM_CONTINUE); 326 } 327 328 /* 329 * build the physical path from the components. 330 * find the logical usb id, and stuff it in buf 331 */ 332 if (devfsadm_enumerate_int(p_path, 0, &buf, rules, 1)) { 333 devfsadm_print(debug_mid, "usb_process: exit/continue\n"); 334 free(l_path); 335 free(p_path); 336 return (DEVFSADM_CONTINUE); 337 } 338 339 (void) snprintf(l_path, PATH_MAX, "usb/%s%s", name, buf); 340 341 devfsadm_print(debug_mid, "usb_process: p_path=%s buf=%s\n", 342 p_path, buf); 343 344 free(buf); 345 346 devfsadm_print(debug_mid, "mklink %s -> %s\n", l_path, p_path); 347 348 (void) devfsadm_mklink(l_path, node, minor, flags); 349 350 if (create_secondary_link) { 351 /* 352 * Create secondary links to make newly hotplugged 353 * usb audio device the primary device. 354 */ 355 if (strcmp(name, "audio") == 0) { 356 (void) devfsadm_secondary_link("audio", l_path, 0); 357 } else if (strcmp(name, "audio-control") == 0) { 358 (void) devfsadm_secondary_link("audioctl", l_path, 0); 359 } 360 } 361 362 free(p_path); 363 free(l_path); 364 365 return (DEVFSADM_CONTINUE); 366 } 367 368 static void 369 ugen_create_link(char *p_path, char *node_name, 370 di_node_t node, di_minor_t minor) 371 { 372 char *buf, s[MAXPATHLEN]; 373 char *lasts = s; 374 char *vid, *pid; 375 char *minor_name; 376 char ugen_RE[128]; 377 devfsadm_enumerate_t ugen_rules[1]; 378 char l_path[PATH_MAX]; 379 int flags = 0; 380 381 devfsadm_print(debug_mid, "ugen_create_link: p_path=%s name=%s\n", 382 p_path, node_name); 383 384 (void) strlcpy(s, node_name, sizeof (s)); 385 386 /* get vid, pid and minor name strings */ 387 vid = strtok_r(lasts, ".", &lasts); 388 pid = strtok_r(NULL, ".", &lasts); 389 minor_name = lasts; 390 391 if ((vid == NULL) || (pid == NULL) || (minor_name == NULL)) { 392 return; 393 } 394 395 /* create regular expression contain vid and pid */ 396 (void) snprintf(ugen_RE, sizeof (ugen_RE), 397 "^usb$/^%s\\.%s$/^([0-9]+)$", vid, pid); 398 devfsadm_print(debug_mid, 399 "ugen_create_link: ugen_RE=%s minor_name=%s\n", 400 ugen_RE, minor_name); 401 402 bzero(ugen_rules, sizeof (ugen_rules)); 403 404 ugen_rules[0].re = ugen_RE; 405 ugen_rules[0].subexp = 1; 406 ugen_rules[0].flags = MATCH_ADDR; 407 408 /* 409 * build the physical path from the components. 410 * find the logical usb id, and stuff it in buf 411 */ 412 if (devfsadm_enumerate_int(p_path, 0, &buf, ugen_rules, 1)) { 413 devfsadm_print(debug_mid, "ugen_create_link: exit/continue\n"); 414 return; 415 } 416 417 (void) snprintf(l_path, sizeof (l_path), "usb/%s.%s/%s/%s", 418 vid, pid, buf, minor_name); 419 420 devfsadm_print(debug_mid, "mklink %s -> %s\n", l_path, p_path); 421 422 (void) devfsadm_mklink(l_path, node, minor, flags); 423 424 free(buf); 425 } 426