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 /* 22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #pragma ident "%Z%%M% %I% %E% SMI" 27 28 /* 29 * 30 * USB generic serial driver (GSD) 31 * 32 */ 33 #include <sys/types.h> 34 #include <sys/param.h> 35 #include <sys/stream.h> 36 #include <sys/stropts.h> 37 #include <sys/errno.h> 38 #include <sys/cred.h> 39 #include <sys/conf.h> 40 #include <sys/stat.h> 41 #include <sys/modctl.h> 42 #include <sys/ddi.h> 43 #include <sys/sunddi.h> 44 #include <sys/sunndi.h> 45 #include <sys/termio.h> 46 #include <sys/termiox.h> 47 #include <sys/stropts.h> 48 #include <sys/stream.h> 49 #include <sys/strsubr.h> 50 #include <sys/strsun.h> 51 #include <sys/strtty.h> 52 #include <sys/policy.h> 53 #include <sys/consdev.h> 54 55 #include <sys/usb/usba.h> 56 #include <sys/usb/clients/usbser/usbser_var.h> 57 #include <sys/usb/clients/usbser/usbser_dsdi.h> 58 #include <sys/usb/clients/usbser/usbser_rseq.h> 59 #include <sys/usb/usba/genconsole.h> 60 61 /* autoconfiguration subroutines */ 62 static int usbser_rseq_do_cb(rseq_t *, int, uintptr_t); 63 static int usbser_free_soft_state(usbser_state_t *); 64 static int usbser_init_soft_state(usbser_state_t *); 65 static int usbser_fini_soft_state(usbser_state_t *); 66 static int usbser_attach_dev(usbser_state_t *); 67 static void usbser_detach_dev(usbser_state_t *); 68 static int usbser_attach_ports(usbser_state_t *); 69 static int usbser_create_port_minor_nodes(usbser_state_t *, int); 70 static void usbser_detach_ports(usbser_state_t *); 71 static int usbser_create_taskq(usbser_state_t *); 72 static void usbser_destroy_taskq(usbser_state_t *); 73 static void usbser_set_dev_state_init(usbser_state_t *); 74 75 /* hotplugging and power management */ 76 static int usbser_disconnect_cb(dev_info_t *); 77 static int usbser_reconnect_cb(dev_info_t *); 78 static void usbser_disconnect_ports(usbser_state_t *); 79 static int usbser_cpr_suspend(dev_info_t *); 80 static int usbser_suspend_ports(usbser_state_t *); 81 static void usbser_cpr_resume(dev_info_t *); 82 static int usbser_restore_device_state(usbser_state_t *); 83 static void usbser_restore_ports_state(usbser_state_t *); 84 85 /* STREAMS subroutines */ 86 static int usbser_open_setup(queue_t *, usbser_port_t *, int, int, 87 cred_t *); 88 static int usbser_open_init(usbser_port_t *, int); 89 static void usbser_check_port_props(usbser_port_t *); 90 static void usbser_open_fini(usbser_port_t *); 91 static int usbser_open_line_setup(usbser_port_t *, int, int); 92 static int usbser_open_carrier_check(usbser_port_t *, int, int); 93 static void usbser_open_queues_init(usbser_port_t *, queue_t *); 94 static void usbser_open_queues_fini(usbser_port_t *); 95 static void usbser_close_drain(usbser_port_t *); 96 static void usbser_close_cancel_break(usbser_port_t *); 97 static void usbser_close_hangup(usbser_port_t *); 98 static void usbser_close_cleanup(usbser_port_t *); 99 100 /* threads */ 101 static void usbser_thr_dispatch(usbser_thread_t *); 102 static void usbser_thr_cancel(usbser_thread_t *); 103 static void usbser_thr_wake(usbser_thread_t *); 104 static void usbser_wq_thread(void *); 105 static void usbser_rq_thread(void *); 106 107 /* DSD callbacks */ 108 static void usbser_tx_cb(caddr_t); 109 static void usbser_rx_cb(caddr_t); 110 static void usbser_rx_massage_data(usbser_port_t *, mblk_t *); 111 static void usbser_rx_massage_mbreak(usbser_port_t *, mblk_t *); 112 static void usbser_rx_cb_put(usbser_port_t *, queue_t *, queue_t *, 113 mblk_t *); 114 static void usbser_status_cb(caddr_t); 115 static void usbser_status_proc_cb(usbser_port_t *); 116 117 /* serial support */ 118 static void usbser_wmsg(usbser_port_t *); 119 static int usbser_data(usbser_port_t *, mblk_t *); 120 static int usbser_ioctl(usbser_port_t *, mblk_t *); 121 static void usbser_iocdata(usbser_port_t *, mblk_t *); 122 static void usbser_stop(usbser_port_t *, mblk_t *); 123 static void usbser_start(usbser_port_t *, mblk_t *); 124 static void usbser_stopi(usbser_port_t *, mblk_t *); 125 static void usbser_starti(usbser_port_t *, mblk_t *); 126 static void usbser_flush(usbser_port_t *, mblk_t *); 127 static void usbser_break(usbser_port_t *, mblk_t *); 128 static void usbser_delay(usbser_port_t *, mblk_t *); 129 static void usbser_restart(void *); 130 static int usbser_port_program(usbser_port_t *); 131 static void usbser_inbound_flow_ctl(usbser_port_t *); 132 133 /* misc */ 134 static int usbser_dev_is_online(usbser_state_t *); 135 static void usbser_serialize_port_act(usbser_port_t *, int); 136 static void usbser_release_port_act(usbser_port_t *, int); 137 static char *usbser_msgtype2str(int); 138 static char *usbser_ioctl2str(int); 139 140 141 /* USBA events */ 142 usb_event_t usbser_usb_events = { 143 usbser_disconnect_cb, /* disconnect */ 144 usbser_reconnect_cb, /* reconnect */ 145 NULL, /* pre-suspend */ 146 NULL, /* pre-resume */ 147 }; 148 149 /* debug support */ 150 uint_t usbser_errlevel = USB_LOG_L4; 151 uint_t usbser_errmask = DPRINT_MASK_ALL; 152 uint_t usbser_instance_debug = (uint_t)-1; 153 154 /* usb serial console */ 155 static struct usbser_state *usbser_list; 156 static kmutex_t usbser_lock; 157 static int usbser_console_abort; 158 static usb_console_info_t console_input, console_output; 159 static uchar_t *console_input_buf; 160 static uchar_t *console_input_start, *console_input_end; 161 162 _NOTE(SCHEME_PROTECTS_DATA("unshared", usbser_console_abort)) 163 _NOTE(SCHEME_PROTECTS_DATA("unshared", console_input)) 164 _NOTE(SCHEME_PROTECTS_DATA("unshared", console_output)) 165 _NOTE(SCHEME_PROTECTS_DATA("unshared", console_input_start)) 166 _NOTE(SCHEME_PROTECTS_DATA("unshared", console_input_end)) 167 168 static void usbser_putchar(cons_polledio_arg_t, uchar_t); 169 static int usbser_getchar(cons_polledio_arg_t); 170 static boolean_t usbser_ischar(cons_polledio_arg_t); 171 static void usbser_polledio_enter(cons_polledio_arg_t); 172 static void usbser_polledio_exit(cons_polledio_arg_t); 173 static int usbser_polledio_init(usbser_port_t *); 174 static void usbser_polledio_fini(usbser_port_t *); 175 176 static struct cons_polledio usbser_polledio = { 177 CONSPOLLEDIO_V1, 178 NULL, /* to be set later */ 179 usbser_putchar, 180 usbser_getchar, 181 usbser_ischar, 182 usbser_polledio_enter, 183 usbser_polledio_exit 184 }; 185 186 /* various statistics. TODO: replace with kstats */ 187 static int usbser_st_tx_data_loss = 0; 188 static int usbser_st_rx_data_loss = 0; 189 static int usbser_st_put_stopi = 0; 190 static int usbser_st_mstop = 0; 191 static int usbser_st_mstart = 0; 192 static int usbser_st_mstopi = 0; 193 static int usbser_st_mstarti = 0; 194 static int usbser_st_rsrv = 0; 195 _NOTE(SCHEME_PROTECTS_DATA("monotonic stats", usbser_st_{ 196 tx_data_loss rx_data_loss put_stopi mstop mstart mstopi mstarti rsrv})) 197 _NOTE(SCHEME_PROTECTS_DATA("unshared", usb_bulk_req_t)) 198 _NOTE(SCHEME_PROTECTS_DATA("unshared", usb_intr_req_t)) 199 200 /* taskq parameter */ 201 extern pri_t minclsyspri; 202 203 /* 204 * tell warlock not to worry about STREAMS structures 205 */ 206 _NOTE(SCHEME_PROTECTS_DATA("unique per call", iocblk datab msgb queue copyreq)) 207 208 /* 209 * modload support 210 */ 211 extern struct mod_ops mod_miscops; 212 213 static struct modlmisc modlmisc = { 214 &mod_miscops, /* Type of module */ 215 "USB generic serial module %I%" 216 }; 217 218 static struct modlinkage modlinkage = { 219 MODREV_1, (void *)&modlmisc, NULL 220 }; 221 222 223 #define RSEQ(f1, f2) RSEQE(f1, usbser_rseq_do_cb, f2, NULL) 224 225 226 /* 227 * loadable module entry points 228 * ---------------------------- 229 */ 230 231 int 232 _init(void) 233 { 234 int err; 235 236 mutex_init(&usbser_lock, NULL, MUTEX_DRIVER, (void *)NULL); 237 if (err = mod_install(&modlinkage)) 238 mutex_destroy(&usbser_lock); 239 240 return (err); 241 } 242 243 244 int 245 _fini(void) 246 { 247 int err; 248 249 if (err = mod_remove(&modlinkage)) 250 251 return (err); 252 253 mutex_destroy(&usbser_lock); 254 255 return (0); 256 } 257 258 259 int 260 _info(struct modinfo *modinfop) 261 { 262 return (mod_info(&modlinkage, modinfop)); 263 } 264 265 266 /* 267 * soft state size 268 */ 269 int 270 usbser_soft_state_size() 271 { 272 return (sizeof (usbser_state_t)); 273 } 274 275 276 /* 277 * autoconfiguration entry points 278 * ------------------------------ 279 */ 280 281 /*ARGSUSED*/ 282 int 283 usbser_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, 284 void **result, void *statep) 285 { 286 int instance; 287 int ret = DDI_FAILURE; 288 usbser_state_t *usbserp; 289 290 instance = USBSER_MINOR2INST(getminor((dev_t)arg)); 291 292 switch (infocmd) { 293 case DDI_INFO_DEVT2DEVINFO: 294 *result = NULL; 295 usbserp = ddi_get_soft_state(statep, instance); 296 if (usbserp != NULL) { 297 *result = usbserp->us_dip; 298 if (*result != NULL) { 299 ret = DDI_SUCCESS; 300 } 301 } 302 303 break; 304 case DDI_INFO_DEVT2INSTANCE: 305 *result = (void *)(uintptr_t)instance; 306 ret = DDI_SUCCESS; 307 308 break; 309 default: 310 break; 311 } 312 313 return (ret); 314 } 315 316 /* 317 * device attach 318 */ 319 static rseq_t rseq_att[] = { 320 RSEQ(NULL, usbser_free_soft_state), 321 RSEQ(usbser_init_soft_state, usbser_fini_soft_state), 322 RSEQ(usbser_attach_dev, usbser_detach_dev), 323 RSEQ(usbser_attach_ports, usbser_detach_ports), 324 RSEQ(usbser_create_taskq, usbser_destroy_taskq), 325 RSEQ(NULL, usbser_set_dev_state_init) 326 }; 327 328 static void 329 usbser_insert(struct usbser_state *usp) 330 { 331 struct usbser_state *tmp; 332 333 mutex_enter(&usbser_lock); 334 tmp = usbser_list; 335 if (tmp == NULL) 336 usbser_list = usp; 337 else { 338 while (tmp->us_next) 339 tmp = tmp->us_next; 340 tmp->us_next = usp; 341 } 342 mutex_exit(&usbser_lock); 343 } 344 345 static void 346 usbser_remove(struct usbser_state *usp) 347 { 348 struct usbser_state *tmp, *prev = NULL; 349 350 mutex_enter(&usbser_lock); 351 tmp = usbser_list; 352 while (tmp != usp) { 353 prev = tmp; 354 tmp = tmp->us_next; 355 } 356 ASSERT(tmp == usp); /* must exist, else attach/detach wrong */ 357 if (prev) 358 prev->us_next = usp->us_next; 359 else 360 usbser_list = usp->us_next; 361 usp->us_next = NULL; 362 mutex_exit(&usbser_lock); 363 } 364 365 /* 366 * Return the first serial device, with dip held. This is called 367 * from the console subsystem to place console on usb serial device. 368 */ 369 dev_info_t * 370 usbser_first_device(void) 371 { 372 dev_info_t *dip = NULL; 373 374 mutex_enter(&usbser_lock); 375 if (usbser_list) { 376 dip = usbser_list->us_dip; 377 ndi_hold_devi(dip); 378 } 379 mutex_exit(&usbser_lock); 380 381 return (dip); 382 } 383 384 int 385 usbser_attach(dev_info_t *dip, ddi_attach_cmd_t cmd, 386 void *statep, ds_ops_t *ds_ops) 387 { 388 int instance; 389 usbser_state_t *usp; 390 391 instance = ddi_get_instance(dip); 392 393 switch (cmd) { 394 case DDI_ATTACH: 395 396 break; 397 case DDI_RESUME: 398 usbser_cpr_resume(dip); 399 400 return (DDI_SUCCESS); 401 default: 402 403 return (DDI_FAILURE); 404 } 405 406 /* allocate and get soft state */ 407 if (ddi_soft_state_zalloc(statep, instance) != DDI_SUCCESS) { 408 409 return (DDI_FAILURE); 410 } 411 if ((usp = ddi_get_soft_state(statep, instance)) == NULL) { 412 ddi_soft_state_free(statep, instance); 413 414 return (DDI_FAILURE); 415 } 416 417 usp->us_statep = statep; 418 usp->us_dip = dip; 419 usp->us_instance = instance; 420 usp->us_ds_ops = ds_ops; 421 422 if (rseq_do(rseq_att, NELEM(rseq_att), (uintptr_t)usp, 0) == RSEQ_OK) { 423 ddi_report_dev(dip); 424 usbser_insert(usp); 425 426 return (DDI_SUCCESS); 427 } else { 428 429 return (DDI_FAILURE); 430 } 431 } 432 433 /* 434 * device detach 435 */ 436 int 437 usbser_detach(dev_info_t *dip, ddi_detach_cmd_t cmd, void *statep) 438 { 439 int instance = ddi_get_instance(dip); 440 usbser_state_t *usp; 441 int rval; 442 443 usp = ddi_get_soft_state(statep, instance); 444 445 switch (cmd) { 446 case DDI_DETACH: 447 USB_DPRINTF_L4(DPRINT_DETACH, usp->us_lh, "usbser_detach"); 448 usbser_remove(usp); 449 (void) rseq_undo(rseq_att, NELEM(rseq_att), (uintptr_t)usp, 0); 450 USB_DPRINTF_L4(DPRINT_DETACH, NULL, 451 "usbser_detach.%d: end", instance); 452 453 return (DDI_SUCCESS); 454 case DDI_SUSPEND: 455 rval = usbser_cpr_suspend(dip); 456 457 return ((rval == USB_SUCCESS)? DDI_SUCCESS : DDI_FAILURE); 458 default: 459 460 return (DDI_FAILURE); 461 } 462 } 463 464 /* 465 * STREAMS entry points 466 * -------------------- 467 * 468 * 469 * port open 470 */ 471 /*ARGSUSED*/ 472 int 473 usbser_open(queue_t *rq, dev_t *dev, int flag, int sflag, cred_t *cr, 474 void *statep) 475 { 476 usbser_state_t *usp; 477 usbser_port_t *pp; 478 int minor = getminor(*dev); 479 int instance; 480 uint_t port_num; 481 int rval; 482 483 instance = USBSER_MINOR2INST(minor); 484 if (instance < 0) { 485 486 return (ENXIO); 487 } 488 489 usp = ddi_get_soft_state(statep, instance); 490 if (usp == NULL) { 491 492 return (ENXIO); 493 } 494 495 /* don't allow to open disconnected device */ 496 mutex_enter(&usp->us_mutex); 497 if (usp->us_dev_state == USB_DEV_DISCONNECTED) { 498 mutex_exit(&usp->us_mutex); 499 500 return (ENXIO); 501 } 502 mutex_exit(&usp->us_mutex); 503 504 /* get port soft state */ 505 port_num = USBSER_MINOR2PORT(minor); 506 if (port_num >= usp->us_port_cnt) { 507 508 return (ENXIO); 509 } 510 pp = &usp->us_ports[port_num]; 511 512 /* set up everything for open */ 513 rval = usbser_open_setup(rq, pp, minor, flag, cr); 514 515 USB_DPRINTF_L4(DPRINT_OPEN, pp->port_lh, "usbser_open: rval=%d", rval); 516 517 return (rval); 518 } 519 520 521 /* 522 * port close 523 * 524 * some things driver should do when the last app closes the line: 525 * 526 * drain data; 527 * cancel break/delay; 528 * hangup line (if necessary); 529 * DSD close; 530 * cleanup soft state; 531 */ 532 /*ARGSUSED*/ 533 int 534 usbser_close(queue_t *rq, int flag, cred_t *cr) 535 { 536 usbser_port_t *pp = (usbser_port_t *)rq->q_ptr; 537 int online; 538 539 if (pp == NULL) { 540 541 return (ENXIO); 542 } 543 544 online = usbser_dev_is_online(pp->port_usp); 545 546 /* 547 * in the closing state new activities will not be initiated 548 */ 549 mutex_enter(&pp->port_mutex); 550 pp->port_state = USBSER_PORT_CLOSING; 551 552 if (online) { 553 /* drain the data */ 554 usbser_close_drain(pp); 555 } 556 557 /* stop break/delay */ 558 usbser_close_cancel_break(pp); 559 560 if (online) { 561 /* hangup line */ 562 usbser_close_hangup(pp); 563 } 564 565 /* 566 * close DSD, cleanup state and transition to 'closed' state 567 */ 568 usbser_close_cleanup(pp); 569 mutex_exit(&pp->port_mutex); 570 571 USB_DPRINTF_L4(DPRINT_CLOSE, pp->port_lh, "usbser_close: end"); 572 573 return (0); 574 } 575 576 577 /* 578 * read side service routine: send as much as possible messages upstream 579 * and if there is still place on the queue, enable receive (if not already) 580 */ 581 int 582 usbser_rsrv(queue_t *q) 583 { 584 usbser_port_t *pp = (usbser_port_t *)q->q_ptr; 585 mblk_t *mp; 586 587 usbser_st_rsrv++; 588 USB_DPRINTF_L4(DPRINT_RQ, pp->port_lh, "usbser_rsrv"); 589 590 while (canputnext(q) && (mp = getq(q))) { 591 putnext(q, mp); 592 } 593 594 if (canputnext(q)) { 595 mutex_enter(&pp->port_mutex); 596 ASSERT(pp->port_state != USBSER_PORT_CLOSED); 597 598 if (USBSER_PORT_ACCESS_OK(pp)) { 599 usbser_thr_wake(&pp->port_rq_thread); 600 } 601 mutex_exit(&pp->port_mutex); 602 } 603 604 return (0); 605 } 606 607 608 /* 609 * wput: put message on the queue and wake wq thread 610 */ 611 int 612 usbser_wput(queue_t *q, mblk_t *mp) 613 { 614 usbser_port_t *pp = (usbser_port_t *)q->q_ptr; 615 616 USB_DPRINTF_L4(DPRINT_WQ, pp->port_lh, "usbser_wput"); 617 618 mutex_enter(&pp->port_mutex); 619 ASSERT(pp->port_state != USBSER_PORT_CLOSED); 620 621 /* ignore new messages if port is already closing */ 622 if (pp->port_state == USBSER_PORT_CLOSING) { 623 freemsg(mp); 624 } else if (putq(q, mp)) { 625 /* 626 * this counter represents amount of tx data on the wq. 627 * each time the data is passed to DSD for transmission, 628 * the counter is decremented accordingly 629 */ 630 pp->port_wq_data_cnt += msgdsize(mp); 631 } else { 632 usbser_st_tx_data_loss++; 633 } 634 mutex_exit(&pp->port_mutex); 635 636 return (0); 637 } 638 639 640 /* 641 * we need wsrv() routine to take advantage of STREAMS flow control: 642 * without it the framework will consider we are always able to process msgs 643 */ 644 int 645 usbser_wsrv(queue_t *q) 646 { 647 usbser_port_t *pp = (usbser_port_t *)q->q_ptr; 648 649 USB_DPRINTF_L4(DPRINT_WQ, pp->port_lh, "usbser_wsrv"); 650 651 mutex_enter(&pp->port_mutex); 652 ASSERT(pp->port_state != USBSER_PORT_CLOSED); 653 654 if (USBSER_PORT_ACCESS_OK(pp)) { 655 usbser_thr_wake(&pp->port_wq_thread); 656 } 657 mutex_exit(&pp->port_mutex); 658 659 return (0); 660 } 661 662 663 /* 664 * power entry point 665 */ 666 int 667 usbser_power(dev_info_t *dip, int comp, int level) 668 { 669 void *statep; 670 usbser_state_t *usp; 671 int new_state; 672 int rval; 673 674 statep = ddi_get_driver_private(dip); 675 usp = ddi_get_soft_state(statep, ddi_get_instance(dip)); 676 677 USB_DPRINTF_L3(DPRINT_EVENTS, usp->us_lh, 678 "usbser_power: dip=0x%p, comp=%d, level=%d", dip, comp, level); 679 680 mutex_enter(&usp->us_mutex); 681 new_state = usp->us_dev_state; 682 mutex_exit(&usp->us_mutex); 683 684 /* let DSD do the job */ 685 rval = USBSER_DS_USB_POWER(usp, comp, level, &new_state); 686 687 /* stay in sync with DSD */ 688 mutex_enter(&usp->us_mutex); 689 usp->us_dev_state = new_state; 690 mutex_exit(&usp->us_mutex); 691 692 return ((rval == USB_SUCCESS) ? DDI_SUCCESS : DDI_FAILURE); 693 } 694 695 696 /* 697 * 698 * configuration entry point subroutines 699 * ------------------------------------- 700 * 701 * rseq callback 702 */ 703 static int 704 usbser_rseq_do_cb(rseq_t *rseq, int num, uintptr_t arg) 705 { 706 usbser_state_t *usp = (usbser_state_t *)arg; 707 int rval = rseq[num].r_do.s_rval; 708 char *name = rseq[num].r_do.s_name; 709 710 if (rval != DDI_SUCCESS) { 711 USB_DPRINTF_L2(DPRINT_ATTACH, usp->us_lh, 712 "do %s failed (%d)", name, rval); 713 714 return (RSEQ_UNDO); 715 } else { 716 717 return (RSEQ_OK); 718 } 719 } 720 721 722 /* 723 * free soft state 724 */ 725 static int 726 usbser_free_soft_state(usbser_state_t *usp) 727 { 728 ddi_soft_state_free(usp->us_statep, usp->us_instance); 729 730 return (USB_SUCCESS); 731 } 732 733 /* 734 * init instance soft state 735 */ 736 static int 737 usbser_init_soft_state(usbser_state_t *usp) 738 { 739 usp->us_lh = usb_alloc_log_hdl(usp->us_dip, "usbs[*].", 740 &usbser_errlevel, &usbser_errmask, &usbser_instance_debug, 741 0); 742 mutex_init(&usp->us_mutex, NULL, MUTEX_DRIVER, (void *)NULL); 743 744 /* save state pointer for use in event callbacks */ 745 ddi_set_driver_private(usp->us_dip, usp->us_statep); 746 747 usp->us_dev_state = USBSER_DEV_INIT; 748 749 return (DDI_SUCCESS); 750 } 751 752 /* 753 * fini instance soft state 754 */ 755 static int 756 usbser_fini_soft_state(usbser_state_t *usp) 757 { 758 usb_free_log_hdl(usp->us_lh); 759 mutex_destroy(&usp->us_mutex); 760 ddi_set_driver_private(usp->us_dip, NULL); 761 762 return (DDI_SUCCESS); 763 } 764 765 /* 766 * attach entire device 767 */ 768 static int 769 usbser_attach_dev(usbser_state_t *usp) 770 { 771 ds_attach_info_t ai; 772 int rval; 773 774 usp->us_dev_state = USB_DEV_ONLINE; 775 776 ai.ai_dip = usp->us_dip; 777 ai.ai_usb_events = &usbser_usb_events; 778 ai.ai_hdl = &usp->us_ds_hdl; 779 ai.ai_port_cnt = &usp->us_port_cnt; 780 781 rval = USBSER_DS_ATTACH(usp, &ai); 782 783 if ((rval != USB_SUCCESS) || (usp->us_ds_hdl == NULL) || 784 (usp->us_port_cnt == 0)) { 785 USB_DPRINTF_L4(DPRINT_ATTACH, usp->us_lh, "usbser_attach_dev: " 786 "failed %d %p %d", rval, usp->us_ds_hdl, usp->us_port_cnt); 787 788 return (DDI_FAILURE); 789 } 790 791 USB_DPRINTF_L4(DPRINT_ATTACH, usp->us_lh, 792 "usbser_attach_dev: port_cnt = %d", usp->us_port_cnt); 793 794 return (DDI_SUCCESS); 795 } 796 797 798 /* 799 * detach entire device 800 */ 801 static void 802 usbser_detach_dev(usbser_state_t *usp) 803 { 804 USBSER_DS_DETACH(usp); 805 } 806 807 808 /* 809 * attach each individual port 810 */ 811 static int 812 usbser_attach_ports(usbser_state_t *usp) 813 { 814 int i; 815 usbser_port_t *pp; 816 ds_cb_t ds_cb; 817 818 /* 819 * allocate port array 820 */ 821 usp->us_ports = kmem_zalloc(usp->us_port_cnt * 822 sizeof (usbser_port_t), KM_SLEEP); 823 824 /* callback handlers */ 825 ds_cb.cb_tx = usbser_tx_cb; 826 ds_cb.cb_rx = usbser_rx_cb; 827 ds_cb.cb_status = usbser_status_cb; 828 829 /* 830 * initialize each port 831 */ 832 for (i = 0; i < usp->us_port_cnt; i++) { 833 pp = &usp->us_ports[i]; 834 835 /* 836 * initialize data 837 */ 838 pp->port_num = i; 839 pp->port_usp = usp; 840 pp->port_ds_ops = usp->us_ds_ops; 841 pp->port_ds_hdl = usp->us_ds_hdl; 842 843 /* allocate log handle */ 844 (void) sprintf(pp->port_lh_name, "usbs[%d].", i); 845 pp->port_lh = usb_alloc_log_hdl(usp->us_dip, 846 pp->port_lh_name, &usbser_errlevel, &usbser_errmask, 847 &usbser_instance_debug, 0); 848 849 mutex_init(&pp->port_mutex, NULL, MUTEX_DRIVER, (void *)NULL); 850 cv_init(&pp->port_state_cv, NULL, CV_DEFAULT, NULL); 851 cv_init(&pp->port_act_cv, NULL, CV_DEFAULT, NULL); 852 cv_init(&pp->port_car_cv, NULL, CV_DEFAULT, NULL); 853 854 /* 855 * init threads 856 */ 857 pp->port_wq_thread.thr_port = pp; 858 pp->port_wq_thread.thr_func = usbser_wq_thread; 859 pp->port_wq_thread.thr_arg = (void *)&pp->port_wq_thread; 860 cv_init(&pp->port_wq_thread.thr_cv, NULL, CV_DEFAULT, NULL); 861 862 pp->port_rq_thread.thr_port = pp; 863 pp->port_rq_thread.thr_func = usbser_rq_thread; 864 pp->port_rq_thread.thr_arg = (void *)&pp->port_rq_thread; 865 cv_init(&pp->port_rq_thread.thr_cv, NULL, CV_DEFAULT, NULL); 866 867 /* 868 * register callbacks 869 */ 870 ds_cb.cb_arg = (caddr_t)pp; 871 USBSER_DS_REGISTER_CB(usp, i, &ds_cb); 872 873 pp->port_state = USBSER_PORT_CLOSED; 874 875 if (usbser_create_port_minor_nodes(usp, i) != USB_SUCCESS) { 876 usbser_detach_ports(usp); 877 878 return (DDI_FAILURE); 879 } 880 } 881 882 return (DDI_SUCCESS); 883 } 884 885 886 /* 887 * create a pair of minor nodes for the port 888 */ 889 static int 890 usbser_create_port_minor_nodes(usbser_state_t *usp, int port_num) 891 { 892 int instance = usp->us_instance; 893 minor_t minor; 894 char name[16]; 895 896 /* 897 * tty node 898 */ 899 (void) sprintf(name, "%d", port_num); 900 minor = USBSER_MAKEMINOR(instance, port_num, 0); 901 902 if (ddi_create_minor_node(usp->us_dip, name, 903 S_IFCHR, minor, DDI_NT_SERIAL, NULL) != DDI_SUCCESS) { 904 905 return (USB_FAILURE); 906 } 907 908 /* 909 * dial-out node 910 */ 911 (void) sprintf(name, "%d,cu", port_num); 912 minor = USBSER_MAKEMINOR(instance, port_num, OUTLINE); 913 914 if (ddi_create_minor_node(usp->us_dip, name, 915 S_IFCHR, minor, DDI_NT_SERIAL_DO, NULL) != DDI_SUCCESS) { 916 917 return (USB_FAILURE); 918 } 919 920 return (USB_SUCCESS); 921 } 922 923 924 /* 925 * detach each port individually 926 */ 927 static void 928 usbser_detach_ports(usbser_state_t *usp) 929 { 930 int i; 931 int sz; 932 usbser_port_t *pp; 933 934 /* 935 * remove all minor nodes 936 */ 937 ddi_remove_minor_node(usp->us_dip, NULL); 938 939 for (i = 0; i < usp->us_port_cnt; i++) { 940 pp = &usp->us_ports[i]; 941 942 if (pp->port_state != USBSER_PORT_CLOSED) { 943 ASSERT(pp->port_state == USBSER_PORT_NOT_INIT); 944 945 continue; 946 } 947 948 USBSER_DS_UNREGISTER_CB(usp, i); 949 950 mutex_destroy(&pp->port_mutex); 951 cv_destroy(&pp->port_state_cv); 952 cv_destroy(&pp->port_act_cv); 953 cv_destroy(&pp->port_car_cv); 954 955 cv_destroy(&pp->port_wq_thread.thr_cv); 956 cv_destroy(&pp->port_rq_thread.thr_cv); 957 958 usb_free_log_hdl(pp->port_lh); 959 } 960 961 /* 962 * free memory 963 */ 964 sz = usp->us_port_cnt * sizeof (usbser_port_t); 965 kmem_free(usp->us_ports, sz); 966 usp->us_ports = NULL; 967 } 968 969 970 /* 971 * create a taskq with two threads per port (read and write sides) 972 */ 973 static int 974 usbser_create_taskq(usbser_state_t *usp) 975 { 976 int nthr = usp->us_port_cnt * 2; 977 978 usp->us_taskq = ddi_taskq_create(usp->us_dip, "usbser_taskq", 979 nthr, TASKQ_DEFAULTPRI, 0); 980 981 return ((usp->us_taskq == NULL) ? DDI_FAILURE : DDI_SUCCESS); 982 } 983 984 985 static void 986 usbser_destroy_taskq(usbser_state_t *usp) 987 { 988 ddi_taskq_destroy(usp->us_taskq); 989 } 990 991 992 static void 993 usbser_set_dev_state_init(usbser_state_t *usp) 994 { 995 mutex_enter(&usp->us_mutex); 996 usp->us_dev_state = USBSER_DEV_INIT; 997 mutex_exit(&usp->us_mutex); 998 } 999 1000 /* 1001 * hotplugging and power management 1002 * --------------------------------- 1003 * 1004 * disconnect event callback 1005 */ 1006 /*ARGSUSED*/ 1007 static int 1008 usbser_disconnect_cb(dev_info_t *dip) 1009 { 1010 void *statep; 1011 usbser_state_t *usp; 1012 1013 statep = ddi_get_driver_private(dip); 1014 usp = ddi_get_soft_state(statep, ddi_get_instance(dip)); 1015 1016 USB_DPRINTF_L3(DPRINT_EVENTS, usp->us_lh, 1017 "usbser_disconnect_cb: dip=%p", dip); 1018 1019 mutex_enter(&usp->us_mutex); 1020 switch (usp->us_dev_state) { 1021 case USB_DEV_ONLINE: 1022 case USB_DEV_PWRED_DOWN: 1023 /* prevent further activity */ 1024 usp->us_dev_state = USB_DEV_DISCONNECTED; 1025 mutex_exit(&usp->us_mutex); 1026 1027 /* see if any of the ports are open and do necessary handling */ 1028 usbser_disconnect_ports(usp); 1029 1030 /* call DSD to do any necessary work */ 1031 if (USBSER_DS_DISCONNECT(usp) != USB_DEV_DISCONNECTED) { 1032 USB_DPRINTF_L2(DPRINT_EVENTS, usp->us_lh, 1033 "usbser_disconnect_cb: ds_disconnect failed"); 1034 } 1035 1036 break; 1037 case USB_DEV_SUSPENDED: 1038 /* we remain suspended */ 1039 default: 1040 mutex_exit(&usp->us_mutex); 1041 1042 break; 1043 } 1044 1045 return (USB_SUCCESS); 1046 } 1047 1048 1049 /* 1050 * reconnect event callback 1051 */ 1052 /*ARGSUSED*/ 1053 static int 1054 usbser_reconnect_cb(dev_info_t *dip) 1055 { 1056 void *statep; 1057 usbser_state_t *usp; 1058 1059 statep = ddi_get_driver_private(dip); 1060 usp = ddi_get_soft_state(statep, ddi_get_instance(dip)); 1061 1062 USB_DPRINTF_L3(DPRINT_EVENTS, usp->us_lh, 1063 "usbser_reconnect_cb: dip=%p", dip); 1064 1065 (void) usbser_restore_device_state(usp); 1066 1067 return (USB_SUCCESS); 1068 } 1069 1070 1071 /* 1072 * if any of the ports is open during disconnect, 1073 * send M_HANGUP message upstream and log a warning 1074 */ 1075 static void 1076 usbser_disconnect_ports(usbser_state_t *usp) 1077 { 1078 usbser_port_t *pp; 1079 queue_t *rq; 1080 int complain = 0; 1081 int hangup = 0; 1082 timeout_id_t delay_id = 0; 1083 int i; 1084 1085 if (usp->us_ports == NULL) { 1086 return; 1087 } 1088 1089 for (i = 0; i < usp->us_port_cnt; i++) { 1090 pp = &usp->us_ports[i]; 1091 1092 mutex_enter(&pp->port_mutex); 1093 if (pp->port_state == USBSER_PORT_OPEN || 1094 USBSER_IS_OPENING(pp) || 1095 pp->port_state == USBSER_PORT_CLOSING) { 1096 complain = 1; 1097 } 1098 1099 if (pp->port_state == USBSER_PORT_OPEN) { 1100 rq = pp->port_ttycommon.t_readq; 1101 1102 /* 1103 * hangup the stream; will send actual 1104 * M_HANGUP message after releasing mutex 1105 */ 1106 pp->port_flags |= USBSER_FL_HUNGUP; 1107 hangup = 1; 1108 1109 /* 1110 * cancel all activities 1111 */ 1112 usbser_release_port_act(pp, USBSER_ACT_ALL); 1113 1114 delay_id = pp->port_delay_id; 1115 pp->port_delay_id = 0; 1116 1117 /* mark disconnected */ 1118 pp->port_state = USBSER_PORT_DISCONNECTED; 1119 cv_broadcast(&pp->port_state_cv); 1120 } 1121 mutex_exit(&pp->port_mutex); 1122 1123 if (hangup) { 1124 (void) putnextctl(rq, M_HANGUP); 1125 hangup = 0; 1126 } 1127 1128 /* 1129 * we couldn't untimeout while holding the mutex - do it now 1130 */ 1131 if (delay_id) { 1132 (void) untimeout(delay_id); 1133 delay_id = 0; 1134 } 1135 } 1136 1137 /* 1138 * complain about disconnecting device while open 1139 */ 1140 if (complain) { 1141 USB_DPRINTF_L0(DPRINT_EVENTS, usp->us_lh, "device was " 1142 "disconnected while open. Data may have been lost"); 1143 } 1144 } 1145 1146 1147 /* 1148 * do CPR suspend 1149 * 1150 * We use a trivial CPR strategy - fail if any of the device's ports are open. 1151 * The problem with more sophisticated strategies is that each open port uses 1152 * two threads that sit in the loop until the port is closed, while CPR has to 1153 * stop all kernel threads to succeed. Stopping port threads is a rather 1154 * intrusive and delicate procedure; I leave it as an RFE for now. 1155 * 1156 */ 1157 static int 1158 usbser_cpr_suspend(dev_info_t *dip) 1159 { 1160 void *statep; 1161 usbser_state_t *usp; 1162 int new_state; 1163 int rval; 1164 1165 statep = ddi_get_driver_private(dip); 1166 usp = ddi_get_soft_state(statep, ddi_get_instance(dip)); 1167 1168 USB_DPRINTF_L4(DPRINT_EVENTS, usp->us_lh, "usbser_cpr_suspend"); 1169 1170 /* suspend each port first */ 1171 if (usbser_suspend_ports(usp) != USB_SUCCESS) { 1172 USB_DPRINTF_L3(DPRINT_EVENTS, usp->us_lh, 1173 "usbser_cpr_suspend: GSD failure"); 1174 1175 return (USB_FAILURE); 1176 } 1177 1178 new_state = USBSER_DS_SUSPEND(usp); /* let DSD do its part */ 1179 1180 mutex_enter(&usp->us_mutex); 1181 if (new_state == USB_DEV_SUSPENDED) { 1182 rval = USB_SUCCESS; 1183 } else { 1184 ASSERT(new_state == USB_DEV_ONLINE); 1185 rval = USB_FAILURE; 1186 } 1187 usp->us_dev_state = new_state; 1188 mutex_exit(&usp->us_mutex); 1189 1190 return (rval); 1191 } 1192 1193 1194 static int 1195 usbser_suspend_ports(usbser_state_t *usp) 1196 { 1197 usbser_port_t *pp; 1198 int i; 1199 1200 for (i = 0; i < usp->us_port_cnt; i++) { 1201 pp = &usp->us_ports[i]; 1202 1203 mutex_enter(&pp->port_mutex); 1204 if (pp->port_state != USBSER_PORT_CLOSED) { 1205 mutex_exit(&pp->port_mutex); 1206 1207 return (USB_FAILURE); 1208 } 1209 mutex_exit(&pp->port_mutex); 1210 } 1211 1212 return (USB_SUCCESS); 1213 } 1214 1215 1216 /* 1217 * do CPR resume 1218 * 1219 * DSD will return USB_DEV_ONLINE in case of success 1220 */ 1221 static void 1222 usbser_cpr_resume(dev_info_t *dip) 1223 { 1224 void *statep; 1225 usbser_state_t *usp; 1226 1227 statep = ddi_get_driver_private(dip); 1228 usp = ddi_get_soft_state(statep, ddi_get_instance(dip)); 1229 1230 USB_DPRINTF_L3(DPRINT_EVENTS, usp->us_lh, "usbser_cpr_resume"); 1231 1232 (void) usbser_restore_device_state(usp); 1233 } 1234 1235 1236 /* 1237 * restore device state after CPR resume or reconnect 1238 */ 1239 static int 1240 usbser_restore_device_state(usbser_state_t *usp) 1241 { 1242 int new_state, current_state; 1243 1244 /* needed as power up state of dev is "unknown" to system */ 1245 (void) pm_busy_component(usp->us_dip, 0); 1246 (void) pm_raise_power(usp->us_dip, 0, USB_DEV_OS_FULL_PWR); 1247 1248 mutex_enter(&usp->us_mutex); 1249 current_state = usp->us_dev_state; 1250 mutex_exit(&usp->us_mutex); 1251 1252 ASSERT((current_state == USB_DEV_DISCONNECTED) || 1253 (current_state == USB_DEV_SUSPENDED)); 1254 1255 /* 1256 * call DSD to perform device-specific work 1257 */ 1258 if (current_state == USB_DEV_DISCONNECTED) { 1259 new_state = USBSER_DS_RECONNECT(usp); 1260 } else { 1261 new_state = USBSER_DS_RESUME(usp); 1262 } 1263 1264 mutex_enter(&usp->us_mutex); 1265 usp->us_dev_state = new_state; 1266 mutex_exit(&usp->us_mutex); 1267 1268 if (new_state == USB_DEV_ONLINE) { 1269 /* 1270 * restore ports state 1271 */ 1272 usbser_restore_ports_state(usp); 1273 } 1274 1275 (void) pm_idle_component(usp->us_dip, 0); 1276 1277 return (USB_SUCCESS); 1278 } 1279 1280 1281 /* 1282 * restore ports state after device reconnect/resume 1283 */ 1284 static void 1285 usbser_restore_ports_state(usbser_state_t *usp) 1286 { 1287 usbser_port_t *pp; 1288 queue_t *rq; 1289 int i; 1290 1291 for (i = 0; i < usp->us_port_cnt; i++) { 1292 pp = &usp->us_ports[i]; 1293 1294 mutex_enter(&pp->port_mutex); 1295 /* 1296 * only care about ports that are open 1297 */ 1298 if ((pp->port_state != USBSER_PORT_SUSPENDED) && 1299 (pp->port_state != USBSER_PORT_DISCONNECTED)) { 1300 mutex_exit(&pp->port_mutex); 1301 1302 continue; 1303 } 1304 1305 pp->port_state = USBSER_PORT_OPEN; 1306 1307 /* 1308 * if the stream was hung up during disconnect, restore it 1309 */ 1310 if (pp->port_flags & USBSER_FL_HUNGUP) { 1311 pp->port_flags &= ~USBSER_FL_HUNGUP; 1312 rq = pp->port_ttycommon.t_readq; 1313 1314 mutex_exit(&pp->port_mutex); 1315 (void) putnextctl(rq, M_UNHANGUP); 1316 mutex_enter(&pp->port_mutex); 1317 } 1318 1319 /* 1320 * restore serial parameters 1321 */ 1322 (void) usbser_port_program(pp); 1323 1324 /* 1325 * wake anything that might be sleeping 1326 */ 1327 cv_broadcast(&pp->port_state_cv); 1328 cv_broadcast(&pp->port_act_cv); 1329 usbser_thr_wake(&pp->port_wq_thread); 1330 usbser_thr_wake(&pp->port_rq_thread); 1331 mutex_exit(&pp->port_mutex); 1332 } 1333 } 1334 1335 1336 /* 1337 * STREAMS subroutines 1338 * ------------------- 1339 * 1340 * 1341 * port open state machine 1342 * 1343 * here's a list of things that the driver has to do while open; 1344 * because device can be opened any number of times, 1345 * initial open has additional responsibilities: 1346 * 1347 * if (initial_open) { 1348 * initialize soft state; \ 1349 * DSD open; - see usbser_open_init() 1350 * dispatch threads; / 1351 * } 1352 * raise DTR; 1353 * wait for carrier (if necessary); 1354 * 1355 * we should also take into consideration that two threads can try to open 1356 * the same physical port simultaneously (/dev/term/N and /dev/cua/N). 1357 * 1358 * return values: 1359 * 0 - success; 1360 * >0 - fail with this error code; 1361 */ 1362 static int 1363 usbser_open_setup(queue_t *rq, usbser_port_t *pp, int minor, int flag, 1364 cred_t *cr) 1365 { 1366 int rval = USBSER_CONTINUE; 1367 1368 mutex_enter(&pp->port_mutex); 1369 /* 1370 * refer to port state diagram in the header file 1371 */ 1372 loop: 1373 switch (pp->port_state) { 1374 case USBSER_PORT_CLOSED: 1375 /* 1376 * initial open 1377 */ 1378 rval = usbser_open_init(pp, minor); 1379 1380 break; 1381 case USBSER_PORT_OPENING_TTY: 1382 /* 1383 * dial-out thread can overtake the port 1384 * if tty open thread is sleeping waiting for carrier 1385 */ 1386 if ((minor & OUTLINE) && (pp->port_flags & USBSER_FL_WOPEN)) { 1387 pp->port_state = USBSER_PORT_OPENING_OUT; 1388 1389 USB_DPRINTF_L3(DPRINT_OPEN, pp->port_lh, 1390 "usbser_open_state: overtake"); 1391 } 1392 1393 /* FALLTHRU */ 1394 case USBSER_PORT_OPENING_OUT: 1395 /* 1396 * if no other open in progress, setup the line 1397 */ 1398 if (USBSER_NO_OTHER_OPEN(pp, minor)) { 1399 rval = usbser_open_line_setup(pp, minor, flag); 1400 1401 break; 1402 } 1403 1404 /* FALLTHRU */ 1405 case USBSER_PORT_CLOSING: 1406 /* 1407 * wait until close active phase ends 1408 */ 1409 if (cv_wait_sig(&pp->port_state_cv, &pp->port_mutex) == 0) { 1410 rval = EINTR; 1411 } 1412 1413 break; 1414 case USBSER_PORT_OPEN: 1415 if ((pp->port_ttycommon.t_flags & TS_XCLUDE) && 1416 secpolicy_excl_open(cr) != 0) { 1417 /* 1418 * exclusive use 1419 */ 1420 rval = EBUSY; 1421 } else if (USBSER_OPEN_IN_OTHER_MODE(pp, minor)) { 1422 /* 1423 * tty and dial-out modes are mutually exclusive 1424 */ 1425 rval = EBUSY; 1426 } else { 1427 /* 1428 * port is being re-open in the same mode 1429 */ 1430 rval = usbser_open_line_setup(pp, minor, flag); 1431 } 1432 1433 break; 1434 default: 1435 rval = ENXIO; 1436 1437 break; 1438 } 1439 1440 if (rval == USBSER_CONTINUE) { 1441 1442 goto loop; 1443 } 1444 1445 /* 1446 * initial open requires additional handling 1447 */ 1448 if (USBSER_IS_OPENING(pp)) { 1449 if (rval == USBSER_COMPLETE) { 1450 if (pp->port_state == USBSER_PORT_OPENING_OUT) { 1451 pp->port_flags |= USBSER_FL_OUT; 1452 } 1453 pp->port_state = USBSER_PORT_OPEN; 1454 cv_broadcast(&pp->port_state_cv); 1455 1456 usbser_open_queues_init(pp, rq); 1457 } else { 1458 usbser_open_fini(pp); 1459 } 1460 } 1461 mutex_exit(&pp->port_mutex); 1462 1463 return (rval); 1464 } 1465 1466 1467 /* 1468 * initialize the port when opened for the first time 1469 */ 1470 static int 1471 usbser_open_init(usbser_port_t *pp, int minor) 1472 { 1473 usbser_state_t *usp = pp->port_usp; 1474 tty_common_t *tp = &pp->port_ttycommon; 1475 int rval = ENXIO; 1476 1477 ASSERT(pp->port_state == USBSER_PORT_CLOSED); 1478 1479 /* 1480 * init state 1481 */ 1482 pp->port_act = 0; 1483 pp->port_flags &= USBSER_FL_PRESERVE; 1484 pp->port_flowc = '\0'; 1485 pp->port_wq_data_cnt = 0; 1486 1487 if (minor & OUTLINE) { 1488 pp->port_state = USBSER_PORT_OPENING_OUT; 1489 } else { 1490 pp->port_state = USBSER_PORT_OPENING_TTY; 1491 } 1492 1493 /* 1494 * init termios settings 1495 */ 1496 tp->t_iflag = 0; 1497 tp->t_iocpending = NULL; 1498 tp->t_size.ws_row = tp->t_size.ws_col = 0; 1499 tp->t_size.ws_xpixel = tp->t_size.ws_ypixel = 0; 1500 tp->t_startc = CSTART; 1501 tp->t_stopc = CSTOP; 1502 1503 usbser_check_port_props(pp); 1504 1505 /* 1506 * dispatch wq and rq threads: 1507 * although queues are not enabled at this point, 1508 * we will need wq to run status processing callback 1509 */ 1510 usbser_thr_dispatch(&pp->port_wq_thread); 1511 usbser_thr_dispatch(&pp->port_rq_thread); 1512 1513 /* 1514 * open DSD port 1515 */ 1516 mutex_exit(&pp->port_mutex); 1517 rval = USBSER_DS_OPEN_PORT(usp, pp->port_num); 1518 mutex_enter(&pp->port_mutex); 1519 1520 if (rval != USB_SUCCESS) { 1521 1522 return (ENXIO); 1523 } 1524 pp->port_flags |= USBSER_FL_DSD_OPEN; 1525 1526 /* 1527 * program port with default parameters 1528 */ 1529 if ((rval = usbser_port_program(pp)) != 0) { 1530 1531 return (ENXIO); 1532 } 1533 1534 return (USBSER_CONTINUE); 1535 } 1536 1537 1538 /* 1539 * create a pair of minor nodes for the port 1540 */ 1541 static void 1542 usbser_check_port_props(usbser_port_t *pp) 1543 { 1544 dev_info_t *dip = pp->port_usp->us_dip; 1545 tty_common_t *tp = &pp->port_ttycommon; 1546 struct termios *termiosp; 1547 uint_t len; 1548 char name[20]; 1549 1550 /* 1551 * take default modes from "ttymodes" property if it exists 1552 */ 1553 if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, ddi_root_node(), 0, 1554 "ttymodes", (uchar_t **)&termiosp, &len) == DDI_PROP_SUCCESS) { 1555 1556 if (len == sizeof (struct termios)) { 1557 tp->t_cflag = termiosp->c_cflag; 1558 1559 if (termiosp->c_iflag & (IXON | IXANY)) { 1560 tp->t_iflag = 1561 termiosp->c_iflag & (IXON | IXANY); 1562 tp->t_startc = termiosp->c_cc[VSTART]; 1563 tp->t_stopc = termiosp->c_cc[VSTOP]; 1564 } 1565 } 1566 ddi_prop_free(termiosp); 1567 } 1568 1569 /* 1570 * look for "ignore-cd" or "port-N-ignore-cd" property 1571 */ 1572 (void) sprintf(name, "port-%d-ignore-cd", pp->port_num); 1573 if (ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, 1574 "ignore-cd", 0) || 1575 ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, name, 0)) { 1576 pp->port_flags |= USBSER_FL_IGNORE_CD; 1577 } else { 1578 pp->port_flags &= ~USBSER_FL_IGNORE_CD; 1579 } 1580 } 1581 1582 1583 /* 1584 * undo what was done in usbser_open_init() 1585 */ 1586 static void 1587 usbser_open_fini(usbser_port_t *pp) 1588 { 1589 uint_t port_num = pp->port_num; 1590 usbser_state_t *usp = pp->port_usp; 1591 1592 /* 1593 * close DSD if it is open 1594 */ 1595 if (pp->port_flags & USBSER_FL_DSD_OPEN) { 1596 mutex_exit(&pp->port_mutex); 1597 if (USBSER_DS_CLOSE_PORT(usp, port_num) != USB_SUCCESS) { 1598 USB_DPRINTF_L2(DPRINT_CLOSE, pp->port_lh, 1599 "usbser_open_fini: CLOSE_PORT fail"); 1600 } 1601 mutex_enter(&pp->port_mutex); 1602 } 1603 1604 /* 1605 * cancel threads 1606 */ 1607 usbser_thr_cancel(&pp->port_wq_thread); 1608 usbser_thr_cancel(&pp->port_rq_thread); 1609 1610 /* 1611 * unpdate soft state 1612 */ 1613 pp->port_state = USBSER_PORT_CLOSED; 1614 cv_broadcast(&pp->port_state_cv); 1615 cv_broadcast(&pp->port_car_cv); 1616 } 1617 1618 1619 /* 1620 * setup serial line 1621 */ 1622 static int 1623 usbser_open_line_setup(usbser_port_t *pp, int minor, int flag) 1624 { 1625 int rval; 1626 1627 mutex_exit(&pp->port_mutex); 1628 /* 1629 * prevent opening a disconnected device 1630 */ 1631 if (!usbser_dev_is_online(pp->port_usp)) { 1632 mutex_enter(&pp->port_mutex); 1633 1634 return (ENXIO); 1635 } 1636 1637 /* raise DTR on every open */ 1638 (void) USBSER_DS_SET_MODEM_CTL(pp, TIOCM_DTR, TIOCM_DTR); 1639 1640 mutex_enter(&pp->port_mutex); 1641 /* 1642 * check carrier 1643 */ 1644 rval = usbser_open_carrier_check(pp, minor, flag); 1645 1646 return (rval); 1647 } 1648 1649 1650 /* 1651 * check carrier and wait if needed 1652 */ 1653 static int 1654 usbser_open_carrier_check(usbser_port_t *pp, int minor, int flag) 1655 { 1656 tty_common_t *tp = &pp->port_ttycommon; 1657 int val = 0; 1658 int rval; 1659 1660 if (pp->port_flags & USBSER_FL_IGNORE_CD) { 1661 tp->t_flags |= TS_SOFTCAR; 1662 } 1663 1664 /* 1665 * check carrier 1666 */ 1667 if (tp->t_flags & TS_SOFTCAR) { 1668 pp->port_flags |= USBSER_FL_CARR_ON; 1669 } else if (USBSER_DS_GET_MODEM_CTL(pp, TIOCM_CD, &val) != USB_SUCCESS) { 1670 1671 return (ENXIO); 1672 } else if (val & TIOCM_CD) { 1673 pp->port_flags |= USBSER_FL_CARR_ON; 1674 } else { 1675 pp->port_flags &= ~USBSER_FL_CARR_ON; 1676 } 1677 1678 /* 1679 * don't block if 1) not allowed to, 2) this is a local device, 1680 * 3) opening in dial-out mode, or 4) carrier is already on 1681 */ 1682 if ((flag & (FNDELAY | FNONBLOCK)) || (tp->t_cflag & CLOCAL) || 1683 (minor & OUTLINE) || (pp->port_flags & USBSER_FL_CARR_ON)) { 1684 1685 return (USBSER_COMPLETE); 1686 } 1687 1688 /* 1689 * block until carrier up (only in tty mode) 1690 */ 1691 USB_DPRINTF_L4(DPRINT_OPEN, pp->port_lh, 1692 "usbser_open_carrier_check: waiting for carrier..."); 1693 1694 pp->port_flags |= USBSER_FL_WOPEN; 1695 1696 rval = cv_wait_sig(&pp->port_car_cv, &pp->port_mutex); 1697 1698 pp->port_flags &= ~USBSER_FL_WOPEN; 1699 1700 if (rval == 0) { 1701 /* 1702 * interrupted with a signal 1703 */ 1704 return (EINTR); 1705 } else { 1706 /* 1707 * try again 1708 */ 1709 return (USBSER_CONTINUE); 1710 } 1711 } 1712 1713 1714 /* 1715 * during open, setup queues and message processing 1716 */ 1717 static void 1718 usbser_open_queues_init(usbser_port_t *pp, queue_t *rq) 1719 { 1720 pp->port_ttycommon.t_readq = rq; 1721 pp->port_ttycommon.t_writeq = WR(rq); 1722 rq->q_ptr = WR(rq)->q_ptr = (caddr_t)pp; 1723 1724 qprocson(rq); 1725 } 1726 1727 1728 /* 1729 * clean up queues and message processing 1730 */ 1731 static void 1732 usbser_open_queues_fini(usbser_port_t *pp) 1733 { 1734 queue_t *rq = pp->port_ttycommon.t_readq; 1735 1736 mutex_exit(&pp->port_mutex); 1737 /* 1738 * clean up queues 1739 */ 1740 qprocsoff(rq); 1741 1742 /* 1743 * free unused messages 1744 */ 1745 flushq(rq, FLUSHALL); 1746 flushq(WR(rq), FLUSHALL); 1747 1748 rq->q_ptr = WR(rq)->q_ptr = NULL; 1749 ttycommon_close(&pp->port_ttycommon); 1750 mutex_enter(&pp->port_mutex); 1751 } 1752 1753 1754 /* 1755 * during close, wait until pending data is gone or the signal is sent 1756 */ 1757 static void 1758 usbser_close_drain(usbser_port_t *pp) 1759 { 1760 int need_drain; 1761 clock_t until; 1762 int rval; 1763 1764 /* 1765 * port_wq_data_cnt indicates amount of data on the write queue, 1766 * which becomes zero when all data is submitted to DSD. But usbser 1767 * stays busy until it gets tx callback from DSD, signalling that 1768 * data has been sent over USB. To be continued in the next comment... 1769 */ 1770 until = ddi_get_lbolt() + 1771 drv_usectohz(USBSER_WQ_DRAIN_TIMEOUT * 1000000); 1772 1773 while ((pp->port_wq_data_cnt > 0) && USBSER_PORT_IS_BUSY(pp)) { 1774 if ((rval = cv_timedwait_sig(&pp->port_act_cv, &pp->port_mutex, 1775 until)) <= 0) { 1776 1777 break; 1778 } 1779 } 1780 1781 /* don't drain if timed out or received a signal */ 1782 need_drain = (pp->port_wq_data_cnt == 0) || !USBSER_PORT_IS_BUSY(pp) || 1783 (rval != 0); 1784 1785 mutex_exit(&pp->port_mutex); 1786 /* 1787 * Once the data reaches USB serial box, it may still be stored in its 1788 * internal output buffer (FIFO). We call DSD drain to ensure that all 1789 * the data is transmitted transmitted over the serial line. 1790 */ 1791 if (need_drain) { 1792 rval = USBSER_DS_FIFO_DRAIN(pp, USBSER_TX_FIFO_DRAIN_TIMEOUT); 1793 if (rval != USB_SUCCESS) { 1794 (void) USBSER_DS_FIFO_FLUSH(pp, DS_TX); 1795 } 1796 } else { 1797 (void) USBSER_DS_FIFO_FLUSH(pp, DS_TX); 1798 } 1799 mutex_enter(&pp->port_mutex); 1800 } 1801 1802 1803 /* 1804 * during close, cancel break/delay 1805 */ 1806 static void 1807 usbser_close_cancel_break(usbser_port_t *pp) 1808 { 1809 timeout_id_t delay_id; 1810 1811 if (pp->port_act & USBSER_ACT_BREAK) { 1812 delay_id = pp->port_delay_id; 1813 pp->port_delay_id = 0; 1814 1815 mutex_exit(&pp->port_mutex); 1816 (void) untimeout(delay_id); 1817 (void) USBSER_DS_BREAK_CTL(pp, DS_OFF); 1818 mutex_enter(&pp->port_mutex); 1819 1820 pp->port_act &= ~USBSER_ACT_BREAK; 1821 } 1822 } 1823 1824 1825 /* 1826 * during close, drop RTS/DTR if necessary 1827 */ 1828 static void 1829 usbser_close_hangup(usbser_port_t *pp) 1830 { 1831 /* 1832 * drop DTR and RTS if HUPCL is set 1833 */ 1834 if (pp->port_ttycommon.t_cflag & HUPCL) { 1835 mutex_exit(&pp->port_mutex); 1836 (void) USBSER_DS_SET_MODEM_CTL(pp, TIOCM_RTS | TIOCM_DTR, 0); 1837 mutex_enter(&pp->port_mutex); 1838 } 1839 } 1840 1841 1842 /* 1843 * state cleanup during close 1844 */ 1845 static void 1846 usbser_close_cleanup(usbser_port_t *pp) 1847 { 1848 usbser_open_queues_fini(pp); 1849 1850 usbser_open_fini(pp); 1851 } 1852 1853 1854 /* 1855 * 1856 * thread management 1857 * ----------------- 1858 * 1859 * 1860 * dispatch a thread 1861 */ 1862 static void 1863 usbser_thr_dispatch(usbser_thread_t *thr) 1864 { 1865 usbser_port_t *pp = thr->thr_port; 1866 usbser_state_t *usp = pp->port_usp; 1867 int rval; 1868 1869 ASSERT(mutex_owned(&pp->port_mutex)); 1870 ASSERT((thr->thr_flags & USBSER_THR_RUNNING) == 0); 1871 1872 thr->thr_flags = USBSER_THR_RUNNING; 1873 1874 rval = ddi_taskq_dispatch(usp->us_taskq, thr->thr_func, thr->thr_arg, 1875 DDI_SLEEP); 1876 ASSERT(rval == DDI_SUCCESS); 1877 } 1878 1879 1880 /* 1881 * cancel a thread 1882 */ 1883 static void 1884 usbser_thr_cancel(usbser_thread_t *thr) 1885 { 1886 usbser_port_t *pp = thr->thr_port; 1887 1888 ASSERT(mutex_owned(&pp->port_mutex)); 1889 1890 thr->thr_flags &= ~USBSER_THR_RUNNING; 1891 cv_signal(&thr->thr_cv); 1892 1893 /* wait until the thread actually exits */ 1894 do { 1895 cv_wait(&thr->thr_cv, &pp->port_mutex); 1896 1897 } while ((thr->thr_flags & USBSER_THR_EXITED) == 0); 1898 } 1899 1900 1901 /* 1902 * wake thread 1903 */ 1904 static void 1905 usbser_thr_wake(usbser_thread_t *thr) 1906 { 1907 usbser_port_t *pp = thr->thr_port; 1908 1909 ASSERT(mutex_owned(&pp->port_mutex)); 1910 1911 thr->thr_flags |= USBSER_THR_WAKE; 1912 cv_signal(&thr->thr_cv); 1913 } 1914 1915 1916 /* 1917 * thread handling write queue requests 1918 */ 1919 static void 1920 usbser_wq_thread(void *arg) 1921 { 1922 usbser_thread_t *thr = (usbser_thread_t *)arg; 1923 usbser_port_t *pp = thr->thr_port; 1924 1925 USB_DPRINTF_L4(DPRINT_WQ, pp->port_lh, "usbser_wq_thread: enter"); 1926 1927 mutex_enter(&pp->port_mutex); 1928 while (thr->thr_flags & USBSER_THR_RUNNING) { 1929 /* 1930 * when woken, see what we should do 1931 */ 1932 if (thr->thr_flags & USBSER_THR_WAKE) { 1933 thr->thr_flags &= ~USBSER_THR_WAKE; 1934 1935 /* 1936 * status callback pending? 1937 */ 1938 if (pp->port_flags & USBSER_FL_STATUS_CB) { 1939 usbser_status_proc_cb(pp); 1940 } 1941 1942 usbser_wmsg(pp); 1943 } else { 1944 /* 1945 * sleep until woken up to do some work, e.g: 1946 * - new message arrives; 1947 * - data transmit completes; 1948 * - status callback pending; 1949 * - wq thread is cancelled; 1950 */ 1951 cv_wait(&thr->thr_cv, &pp->port_mutex); 1952 USB_DPRINTF_L4(DPRINT_WQ, pp->port_lh, 1953 "usbser_wq_thread: wakeup"); 1954 } 1955 } 1956 thr->thr_flags |= USBSER_THR_EXITED; 1957 cv_signal(&thr->thr_cv); 1958 USB_DPRINTF_L4(DPRINT_WQ, pp->port_lh, "usbser_wq_thread: exit"); 1959 mutex_exit(&pp->port_mutex); 1960 } 1961 1962 1963 /* 1964 * thread handling read queue requests 1965 */ 1966 static void 1967 usbser_rq_thread(void *arg) 1968 { 1969 usbser_thread_t *thr = (usbser_thread_t *)arg; 1970 usbser_port_t *pp = thr->thr_port; 1971 1972 USB_DPRINTF_L4(DPRINT_WQ, pp->port_lh, "usbser_rq_thread: enter"); 1973 1974 mutex_enter(&pp->port_mutex); 1975 while (thr->thr_flags & USBSER_THR_RUNNING) { 1976 /* 1977 * read service routine will wake us when 1978 * more space is available on the read queue 1979 */ 1980 if (thr->thr_flags & USBSER_THR_WAKE) { 1981 thr->thr_flags &= ~USBSER_THR_WAKE; 1982 1983 /* 1984 * don't process messages until queue is enabled 1985 */ 1986 if (!pp->port_ttycommon.t_readq) { 1987 1988 continue; 1989 } 1990 1991 /* 1992 * check whether we need to resume receive 1993 */ 1994 if (pp->port_flags & USBSER_FL_RX_STOPPED) { 1995 pp->port_flowc = pp->port_ttycommon.t_startc; 1996 usbser_inbound_flow_ctl(pp); 1997 } 1998 1999 /* 2000 * grab more data if available 2001 */ 2002 mutex_exit(&pp->port_mutex); 2003 usbser_rx_cb((caddr_t)pp); 2004 mutex_enter(&pp->port_mutex); 2005 } else { 2006 cv_wait(&thr->thr_cv, &pp->port_mutex); 2007 USB_DPRINTF_L4(DPRINT_WQ, pp->port_lh, 2008 "usbser_rq_thread: wakeup"); 2009 } 2010 } 2011 thr->thr_flags |= USBSER_THR_EXITED; 2012 cv_signal(&thr->thr_cv); 2013 USB_DPRINTF_L4(DPRINT_RQ, pp->port_lh, "usbser_rq_thread: exit"); 2014 mutex_exit(&pp->port_mutex); 2015 } 2016 2017 2018 /* 2019 * DSD callbacks 2020 * ------------- 2021 * 2022 * Note: to avoid deadlocks with DSD, these callbacks 2023 * should not call DSD functions that can block. 2024 * 2025 * 2026 * transmit callback 2027 * 2028 * invoked by DSD when the last byte of data is transmitted over USB 2029 */ 2030 static void 2031 usbser_tx_cb(caddr_t arg) 2032 { 2033 usbser_port_t *pp = (usbser_port_t *)arg; 2034 int online; 2035 2036 online = usbser_dev_is_online(pp->port_usp); 2037 2038 mutex_enter(&pp->port_mutex); 2039 USB_DPRINTF_L4(DPRINT_TX_CB, pp->port_lh, 2040 "usbser_tx_cb: act=%x curthread=%p", pp->port_act, curthread); 2041 2042 usbser_release_port_act(pp, USBSER_ACT_TX); 2043 2044 if (online && USBSER_PORT_ACCESS_OK(pp) && !USBSER_PORT_IS_BUSY(pp)) { 2045 /* 2046 * wake wq thread for further data/ioctl processing 2047 */ 2048 usbser_thr_wake(&pp->port_wq_thread); 2049 } 2050 mutex_exit(&pp->port_mutex); 2051 } 2052 2053 2054 /* 2055 * receive callback 2056 * 2057 * invoked by DSD when there is more data for us to pick 2058 */ 2059 static void 2060 usbser_rx_cb(caddr_t arg) 2061 { 2062 usbser_port_t *pp = (usbser_port_t *)arg; 2063 queue_t *rq, *wq; 2064 mblk_t *mp; /* current mblk */ 2065 mblk_t *data, *data_tail; /* M_DATA mblk list and its tail */ 2066 mblk_t *emp; /* error (M_BREAK) mblk */ 2067 2068 USB_DPRINTF_L4(DPRINT_RX_CB, pp->port_lh, "usbser_rx_cb"); 2069 2070 if (!usbser_dev_is_online(pp->port_usp)) { 2071 2072 return; 2073 } 2074 2075 /* get data from DSD */ 2076 if ((mp = USBSER_DS_RX(pp)) == NULL) { 2077 2078 return; 2079 } 2080 2081 mutex_enter(&pp->port_mutex); 2082 if ((!USBSER_PORT_ACCESS_OK(pp)) || 2083 ((pp->port_ttycommon.t_cflag & CREAD) == 0)) { 2084 freemsg(mp); 2085 mutex_exit(&pp->port_mutex); 2086 USB_DPRINTF_L3(DPRINT_RX_CB, pp->port_lh, 2087 "usbser_rx_cb: access not ok or receiver disabled"); 2088 2089 return; 2090 } 2091 2092 usbser_serialize_port_act(pp, USBSER_ACT_RX); 2093 2094 rq = pp->port_ttycommon.t_readq; 2095 wq = pp->port_ttycommon.t_writeq; 2096 mutex_exit(&pp->port_mutex); 2097 2098 /* 2099 * DSD data is a b_cont-linked list of M_DATA and M_BREAK blocks. 2100 * M_DATA is correctly received data. 2101 * M_BREAK is a character with either framing or parity error. 2102 * 2103 * this loop runs through the list of mblks. when it meets an M_BREAK, 2104 * it sends all leading M_DATA's in one shot, then sends M_BREAK. 2105 * in the trivial case when list contains only M_DATA's, the loop 2106 * does nothing but set data variable. 2107 */ 2108 data = data_tail = NULL; 2109 while (mp) { 2110 /* 2111 * skip data until we meet M_BREAK or end of list 2112 */ 2113 if (DB_TYPE(mp) == M_DATA) { 2114 if (data == NULL) { 2115 data = mp; 2116 } 2117 data_tail = mp; 2118 mp = mp->b_cont; 2119 2120 continue; 2121 } 2122 2123 /* detach data list from mp */ 2124 if (data_tail) { 2125 data_tail->b_cont = NULL; 2126 } 2127 /* detach emp from the list */ 2128 emp = mp; 2129 mp = mp->b_cont; 2130 emp->b_cont = NULL; 2131 2132 /* DSD shouldn't send anything but M_DATA or M_BREAK */ 2133 if ((DB_TYPE(emp) != M_BREAK) || (MBLKL(emp) != 2)) { 2134 freemsg(emp); 2135 USB_DPRINTF_L2(DPRINT_RX_CB, pp->port_lh, 2136 "usbser_rx_cb: bad message"); 2137 2138 continue; 2139 } 2140 2141 /* 2142 * first tweak and send M_DATA's 2143 */ 2144 if (data) { 2145 usbser_rx_massage_data(pp, data); 2146 usbser_rx_cb_put(pp, rq, wq, data); 2147 data = data_tail = NULL; 2148 } 2149 2150 /* 2151 * now tweak and send M_BREAK 2152 */ 2153 mutex_enter(&pp->port_mutex); 2154 usbser_rx_massage_mbreak(pp, emp); 2155 mutex_exit(&pp->port_mutex); 2156 usbser_rx_cb_put(pp, rq, wq, emp); 2157 } 2158 2159 /* send the rest of the data, if any */ 2160 if (data) { 2161 usbser_rx_massage_data(pp, data); 2162 usbser_rx_cb_put(pp, rq, wq, data); 2163 } 2164 2165 mutex_enter(&pp->port_mutex); 2166 usbser_release_port_act(pp, USBSER_ACT_RX); 2167 mutex_exit(&pp->port_mutex); 2168 } 2169 2170 /* 2171 * the joys of termio -- this is to accomodate Unix98 assertion: 2172 * 2173 * If PARENB is supported and is set, when PARMRK is set, and CSIZE is 2174 * set to CS8, and IGNPAR is clear, and ISTRIP is clear, a valid 2175 * character of '\377' is read as '\377', '\377'. 2176 * 2177 * Posix Ref: Assertion 7.1.2.2-16(C) 2178 * 2179 * this requires the driver to scan every incoming valid character 2180 */ 2181 static void 2182 usbser_rx_massage_data(usbser_port_t *pp, mblk_t *mp) 2183 { 2184 tty_common_t *tp = &pp->port_ttycommon; 2185 uchar_t *p; 2186 mblk_t *newmp; 2187 int tailsz; 2188 2189 /* avoid scanning if possible */ 2190 mutex_enter(&pp->port_mutex); 2191 if (!((tp->t_cflag & PARENB) && (tp->t_iflag & PARMRK) && 2192 ((tp->t_cflag & CSIZE) == CS8) && 2193 ((tp->t_iflag & (IGNPAR|ISTRIP)) == 0))) { 2194 mutex_exit(&pp->port_mutex); 2195 2196 return; 2197 } 2198 mutex_exit(&pp->port_mutex); 2199 2200 while (mp) { 2201 for (p = mp->b_rptr; p < mp->b_wptr; ) { 2202 if (*p++ != 0377) { 2203 2204 continue; 2205 } 2206 USB_DPRINTF_L4(DPRINT_RX_CB, pp->port_lh, 2207 "usbser_rx_massage_data: mp=%p off=%d(%d)", 2208 mp, p - mp->b_rptr - 1, MBLKL(mp)); 2209 2210 /* 2211 * insert another 0377 after this one. all data after 2212 * the original 0377 have to be copied to the new mblk 2213 */ 2214 tailsz = mp->b_wptr - p; 2215 if ((newmp = allocb(tailsz + 1, BPRI_HI)) == NULL) { 2216 USB_DPRINTF_L2(DPRINT_RX_CB, pp->port_lh, 2217 "usbser_rx_massage_data: allocb failed"); 2218 2219 continue; 2220 } 2221 2222 /* fill in the new mblk */ 2223 *newmp->b_wptr++ = 0377; 2224 if (tailsz > 0) { 2225 bcopy(p, newmp->b_wptr, tailsz); 2226 newmp->b_wptr += tailsz; 2227 } 2228 /* shrink the original mblk */ 2229 mp->b_wptr = p; 2230 2231 newmp->b_cont = mp->b_cont; 2232 mp->b_cont = newmp; 2233 p = newmp->b_rptr + 1; 2234 mp = newmp; 2235 } 2236 mp = mp->b_cont; 2237 } 2238 } 2239 2240 /* 2241 * more joys of termio 2242 */ 2243 static void 2244 usbser_rx_massage_mbreak(usbser_port_t *pp, mblk_t *mp) 2245 { 2246 tty_common_t *tp = &pp->port_ttycommon; 2247 uchar_t err, c; 2248 2249 err = *mp->b_rptr; 2250 c = *(mp->b_rptr + 1); 2251 2252 if ((err & (DS_FRAMING_ERR | DS_BREAK_ERR)) && (c == 0)) { 2253 /* break */ 2254 mp->b_rptr += 2; 2255 } else if (!(tp->t_iflag & INPCK) && (err & (DS_PARITY_ERR))) { 2256 /* Posix Ref: Assertion 7.1.2.2-20(C) */ 2257 mp->b_rptr++; 2258 DB_TYPE(mp) = M_DATA; 2259 } else { 2260 /* for ldterm to handle */ 2261 mp->b_rptr++; 2262 } 2263 2264 USB_DPRINTF_L4(DPRINT_RX_CB, pp->port_lh, 2265 "usbser_rx_massage_mbreak: type=%x len=%d [0]=0%o", 2266 DB_TYPE(mp), MBLKL(mp), (MBLKL(mp) > 0) ? *mp->b_rptr : 45); 2267 } 2268 2269 2270 /* 2271 * in rx callback, try to send an mblk upstream 2272 */ 2273 static void 2274 usbser_rx_cb_put(usbser_port_t *pp, queue_t *rq, queue_t *wq, mblk_t *mp) 2275 { 2276 if (canputnext(rq)) { 2277 putnext(rq, mp); 2278 } else if (canput(rq) && putq(rq, mp)) { 2279 /* 2280 * full queue indicates the need for inbound flow control 2281 */ 2282 (void) putctl(wq, M_STOPI); 2283 usbser_st_put_stopi++; 2284 2285 USB_DPRINTF_L3(DPRINT_RX_CB, pp->port_lh, 2286 "usbser_rx_cb: cannot putnext, flow ctl"); 2287 } else { 2288 freemsg(mp); 2289 usbser_st_rx_data_loss++; 2290 (void) putctl(wq, M_STOPI); 2291 usbser_st_put_stopi++; 2292 2293 USB_DPRINTF_L1(DPRINT_RX_CB, pp->port_lh, 2294 "input overrun"); 2295 } 2296 } 2297 2298 2299 /* 2300 * modem status change callback 2301 * 2302 * each time external status lines are changed, DSD calls this routine 2303 */ 2304 static void 2305 usbser_status_cb(caddr_t arg) 2306 { 2307 usbser_port_t *pp = (usbser_port_t *)arg; 2308 2309 USB_DPRINTF_L4(DPRINT_STATUS_CB, pp->port_lh, "usbser_status_cb"); 2310 2311 if (!usbser_dev_is_online(pp->port_usp)) { 2312 2313 return; 2314 } 2315 2316 /* 2317 * actual processing will be done in usbser_status_proc_cb() 2318 * running in wq thread 2319 */ 2320 mutex_enter(&pp->port_mutex); 2321 if (USBSER_PORT_ACCESS_OK(pp) || USBSER_IS_OPENING(pp)) { 2322 pp->port_flags |= USBSER_FL_STATUS_CB; 2323 usbser_thr_wake(&pp->port_wq_thread); 2324 } 2325 mutex_exit(&pp->port_mutex); 2326 } 2327 2328 2329 /* 2330 * modem status change 2331 */ 2332 static void 2333 usbser_status_proc_cb(usbser_port_t *pp) 2334 { 2335 tty_common_t *tp = &pp->port_ttycommon; 2336 queue_t *rq, *wq; 2337 int status; 2338 int drop_dtr = 0; 2339 int rq_msg = 0, wq_msg = 0; 2340 2341 USB_DPRINTF_L4(DPRINT_STATUS_CB, pp->port_lh, "usbser_status_proc_cb"); 2342 2343 pp->port_flags &= ~USBSER_FL_STATUS_CB; 2344 2345 mutex_exit(&pp->port_mutex); 2346 if (!usbser_dev_is_online(pp->port_usp)) { 2347 mutex_enter(&pp->port_mutex); 2348 2349 return; 2350 } 2351 2352 /* get modem status */ 2353 if (USBSER_DS_GET_MODEM_CTL(pp, -1, &status) != USB_SUCCESS) { 2354 mutex_enter(&pp->port_mutex); 2355 2356 return; 2357 } 2358 2359 mutex_enter(&pp->port_mutex); 2360 usbser_serialize_port_act(pp, USBSER_ACT_CTL); 2361 2362 rq = pp->port_ttycommon.t_readq; 2363 wq = pp->port_ttycommon.t_writeq; 2364 2365 /* 2366 * outbound flow control 2367 */ 2368 if (tp->t_cflag & CRTSCTS) { 2369 if (!(status & TIOCM_CTS)) { 2370 /* 2371 * CTS dropped, stop xmit 2372 */ 2373 if (!(pp->port_flags & USBSER_FL_TX_STOPPED)) { 2374 wq_msg = M_STOP; 2375 } 2376 } else if (pp->port_flags & USBSER_FL_TX_STOPPED) { 2377 /* 2378 * CTS raised, resume xmit 2379 */ 2380 wq_msg = M_START; 2381 } 2382 } 2383 2384 /* 2385 * check carrier 2386 */ 2387 if ((status & TIOCM_CD) || (tp->t_flags & TS_SOFTCAR)) { 2388 /* 2389 * carrier present 2390 */ 2391 if ((pp->port_flags & USBSER_FL_CARR_ON) == 0) { 2392 pp->port_flags |= USBSER_FL_CARR_ON; 2393 2394 rq_msg = M_UNHANGUP; 2395 /* 2396 * wake open 2397 */ 2398 if (pp->port_flags & USBSER_FL_WOPEN) { 2399 cv_broadcast(&pp->port_car_cv); 2400 } 2401 2402 USB_DPRINTF_L4(DPRINT_STATUS_CB, pp->port_lh, 2403 "usbser_status_cb: carr on"); 2404 } 2405 } else if (pp->port_flags & USBSER_FL_CARR_ON) { 2406 pp->port_flags &= ~USBSER_FL_CARR_ON; 2407 /* 2408 * carrier went away: if not local line, drop DTR 2409 */ 2410 if (!(tp->t_cflag & CLOCAL)) { 2411 drop_dtr = 1; 2412 rq_msg = M_HANGUP; 2413 } 2414 if ((pp->port_flags & USBSER_FL_TX_STOPPED) && (wq_msg == 0)) { 2415 wq_msg = M_START; 2416 } 2417 2418 USB_DPRINTF_L4(DPRINT_STATUS_CB, pp->port_lh, 2419 "usbser_status_cb: carr off"); 2420 } 2421 mutex_exit(&pp->port_mutex); 2422 2423 USB_DPRINTF_L4(DPRINT_STATUS_CB, pp->port_lh, 2424 "usbser_status_cb: rq_msg=%d wq_msg=%d", rq_msg, wq_msg); 2425 2426 /* 2427 * commit postponed actions now 2428 * do so only if port is fully open (queues are enabled) 2429 */ 2430 if (rq) { 2431 if (rq_msg) { 2432 (void) putnextctl(rq, rq_msg); 2433 } 2434 if (drop_dtr) { 2435 (void) USBSER_DS_SET_MODEM_CTL(pp, TIOCM_DTR, 0); 2436 } 2437 if (wq_msg) { 2438 (void) putctl(wq, wq_msg); 2439 } 2440 } 2441 2442 mutex_enter(&pp->port_mutex); 2443 usbser_release_port_act(pp, USBSER_ACT_CTL); 2444 } 2445 2446 2447 /* 2448 * serial support 2449 * -------------- 2450 * 2451 * 2452 * this routine is run by wq thread every time it's woken, 2453 * i.e. when the queue contains messages to process 2454 */ 2455 static void 2456 usbser_wmsg(usbser_port_t *pp) 2457 { 2458 queue_t *q = pp->port_ttycommon.t_writeq; 2459 mblk_t *mp; 2460 int msgtype; 2461 2462 ASSERT(mutex_owned(&pp->port_mutex)); 2463 2464 if (q == NULL) { 2465 USB_DPRINTF_L3(DPRINT_WQ, pp->port_lh, "usbser_wmsg: q=NULL"); 2466 2467 return; 2468 } 2469 USB_DPRINTF_L4(DPRINT_WQ, pp->port_lh, "usbser_wmsg: q=%p act=%x 0x%x", 2470 q, pp->port_act, q->q_first ? DB_TYPE(q->q_first) : 0xff); 2471 2472 while ((mp = getq(q)) != NULL) { 2473 msgtype = DB_TYPE(mp); 2474 USB_DPRINTF_L4(DPRINT_WQ, pp->port_lh, "usbser_wmsg: " 2475 "type=%s (0x%x)", usbser_msgtype2str(msgtype), msgtype); 2476 2477 switch (msgtype) { 2478 /* 2479 * high-priority messages 2480 */ 2481 case M_STOP: 2482 usbser_stop(pp, mp); 2483 2484 break; 2485 case M_START: 2486 usbser_start(pp, mp); 2487 2488 break; 2489 case M_STOPI: 2490 usbser_stopi(pp, mp); 2491 2492 break; 2493 case M_STARTI: 2494 usbser_starti(pp, mp); 2495 2496 break; 2497 case M_IOCDATA: 2498 usbser_iocdata(pp, mp); 2499 2500 break; 2501 case M_FLUSH: 2502 usbser_flush(pp, mp); 2503 2504 break; 2505 /* 2506 * normal-priority messages 2507 */ 2508 case M_BREAK: 2509 usbser_break(pp, mp); 2510 2511 break; 2512 case M_DELAY: 2513 usbser_delay(pp, mp); 2514 2515 break; 2516 case M_DATA: 2517 if (usbser_data(pp, mp) != USB_SUCCESS) { 2518 (void) putbq(q, mp); 2519 2520 return; 2521 } 2522 2523 break; 2524 case M_IOCTL: 2525 if (usbser_ioctl(pp, mp) != USB_SUCCESS) { 2526 (void) putbq(q, mp); 2527 2528 return; 2529 } 2530 2531 break; 2532 default: 2533 freemsg(mp); 2534 2535 break; 2536 } 2537 } 2538 } 2539 2540 2541 /* 2542 * process M_DATA message 2543 */ 2544 static int 2545 usbser_data(usbser_port_t *pp, mblk_t *mp) 2546 { 2547 /* put off until current transfer ends or delay is over */ 2548 if ((pp->port_act & USBSER_ACT_TX) || 2549 (pp->port_act & USBSER_ACT_DELAY)) { 2550 2551 return (USB_FAILURE); 2552 } 2553 if ((MBLKL(mp) <= 0)) { 2554 freemsg(mp); 2555 2556 return (USB_SUCCESS); 2557 } 2558 2559 pp->port_act |= USBSER_ACT_TX; 2560 pp->port_wq_data_cnt -= msgdsize(mp); 2561 2562 mutex_exit(&pp->port_mutex); 2563 /* DSD is required to accept data block in any case */ 2564 (void) USBSER_DS_TX(pp, mp); 2565 mutex_enter(&pp->port_mutex); 2566 2567 return (USB_SUCCESS); 2568 } 2569 2570 2571 /* 2572 * process an M_IOCTL message 2573 */ 2574 static int 2575 usbser_ioctl(usbser_port_t *pp, mblk_t *mp) 2576 { 2577 tty_common_t *tp = &pp->port_ttycommon; 2578 queue_t *q = tp->t_writeq; 2579 struct iocblk *iocp; 2580 int cmd; 2581 mblk_t *datamp; 2582 int error = 0, rval; 2583 int val; 2584 2585 ASSERT(mutex_owned(&pp->port_mutex)); 2586 ASSERT(DB_TYPE(mp) == M_IOCTL); 2587 2588 iocp = (struct iocblk *)mp->b_rptr; 2589 cmd = iocp->ioc_cmd; 2590 2591 USB_DPRINTF_L4(DPRINT_IOCTL, pp->port_lh, "usbser_ioctl: " 2592 "mp=%p %s (0x%x)", mp, usbser_ioctl2str(cmd), cmd); 2593 2594 if (tp->t_iocpending != NULL) { 2595 /* 2596 * We were holding an ioctl response pending the 2597 * availability of an mblk to hold data to be passed up; 2598 * another ioctl came through, which means that ioctl 2599 * must have timed out or been aborted. 2600 */ 2601 freemsg(tp->t_iocpending); 2602 tp->t_iocpending = NULL; 2603 } 2604 2605 switch (cmd) { 2606 case TIOCMGET: 2607 case TIOCMBIC: 2608 case TIOCMBIS: 2609 case TIOCMSET: 2610 case CONSOPENPOLLEDIO: 2611 case CONSCLOSEPOLLEDIO: 2612 case CONSSETABORTENABLE: 2613 case CONSGETABORTENABLE: 2614 /* 2615 * For the above ioctls do not call ttycommon_ioctl() because 2616 * this function frees up the message block (mp->b_cont) that 2617 * contains the address of the user variable where we need to 2618 * pass back the bit array. 2619 */ 2620 error = -1; 2621 usbser_serialize_port_act(pp, USBSER_ACT_CTL); 2622 mutex_exit(&pp->port_mutex); 2623 2624 break; 2625 case TCSBRK: 2626 /* serialize breaks */ 2627 if (pp->port_act & USBSER_ACT_BREAK) { 2628 2629 return (USB_FAILURE); 2630 } 2631 default: 2632 usbser_serialize_port_act(pp, USBSER_ACT_CTL); 2633 mutex_exit(&pp->port_mutex); 2634 (void) ttycommon_ioctl(tp, q, mp, &error); 2635 } 2636 2637 if (error == 0) { 2638 /* 2639 * ttycommon_ioctl() did most of the work 2640 * we just use the data it set up 2641 */ 2642 switch (cmd) { 2643 case TCSETSF: 2644 case TCSETSW: 2645 case TCSETA: 2646 case TCSETAW: 2647 case TCSETAF: 2648 (void) USBSER_DS_FIFO_DRAIN(pp, DS_TX); 2649 2650 /* FALLTHRU */ 2651 case TCSETS: 2652 mutex_enter(&pp->port_mutex); 2653 error = usbser_port_program(pp); 2654 mutex_exit(&pp->port_mutex); 2655 2656 break; 2657 } 2658 2659 goto end; 2660 } else if (error > 0) { 2661 USB_DPRINTF_L3(DPRINT_IOCTL, pp->port_lh, "usbser_ioctl: " 2662 "ttycommon_ioctl returned %d", error); 2663 2664 goto end; 2665 } 2666 2667 /* 2668 * error < 0: ttycommon_ioctl() didn't do anything, we process it here 2669 */ 2670 error = 0; 2671 switch (cmd) { 2672 case TCSBRK: 2673 if ((error = miocpullup(mp, sizeof (int))) != 0) { 2674 2675 break; 2676 } 2677 /* drain output */ 2678 (void) USBSER_DS_FIFO_DRAIN(pp, USBSER_TX_FIFO_DRAIN_TIMEOUT); 2679 /* 2680 * if required, set break 2681 */ 2682 if (*(int *)mp->b_cont->b_rptr == 0) { 2683 if (USBSER_DS_BREAK_CTL(pp, DS_ON) != USB_SUCCESS) { 2684 error = EIO; 2685 2686 break; 2687 } 2688 mutex_enter(&pp->port_mutex); 2689 pp->port_act |= USBSER_ACT_BREAK; 2690 pp->port_delay_id = timeout(usbser_restart, pp, 2691 drv_usectohz(250000)); 2692 mutex_exit(&pp->port_mutex); 2693 } 2694 2695 break; 2696 case TIOCSBRK: 2697 /* set break */ 2698 if (USBSER_DS_BREAK_CTL(pp, DS_ON) != USB_SUCCESS) { 2699 error = EIO; 2700 } 2701 2702 break; 2703 case TIOCCBRK: 2704 /* clear break */ 2705 if (USBSER_DS_BREAK_CTL(pp, DS_OFF) != USB_SUCCESS) { 2706 error = EIO; 2707 } 2708 2709 break; 2710 case TIOCMSET: 2711 case TIOCMBIS: 2712 case TIOCMBIC: 2713 if (iocp->ioc_count == TRANSPARENT) { 2714 mcopyin(mp, NULL, sizeof (int), NULL); 2715 2716 break; 2717 } 2718 if ((error = miocpullup(mp, sizeof (int))) != 0) { 2719 2720 break; 2721 } 2722 2723 val = *(int *)mp->b_cont->b_rptr; 2724 if (cmd == TIOCMSET) { 2725 rval = USBSER_DS_SET_MODEM_CTL(pp, -1, val); 2726 } else if (cmd == TIOCMBIS) { 2727 rval = USBSER_DS_SET_MODEM_CTL(pp, val, -1); 2728 } else if (cmd == TIOCMBIC) { 2729 rval = USBSER_DS_SET_MODEM_CTL(pp, val, 0); 2730 } 2731 if (rval != USB_SUCCESS) { 2732 error = EIO; 2733 } 2734 2735 break; 2736 case (tIOC | 109): /* TIOCSILOOP */ 2737 if (USBSER_DS_LOOPBACK_SUPPORTED(pp)) { 2738 if (USBSER_DS_LOOPBACK(pp, DS_ON) != USB_SUCCESS) { 2739 error = EIO; 2740 } else { 2741 iocp->ioc_error = 0; 2742 mp->b_datap->db_type = M_IOCACK; 2743 } 2744 } else { 2745 error = EINVAL; 2746 } 2747 2748 break; 2749 case (tIOC | 108): /* TIOCCILOOP */ 2750 if (USBSER_DS_LOOPBACK_SUPPORTED(pp)) { 2751 if (USBSER_DS_LOOPBACK(pp, DS_OFF) != USB_SUCCESS) { 2752 error = EIO; 2753 } else { 2754 iocp->ioc_error = 0; 2755 mp->b_datap->db_type = M_IOCACK; 2756 } 2757 } else { 2758 error = EINVAL; 2759 } 2760 2761 break; 2762 case TIOCMGET: 2763 datamp = allocb(sizeof (int), BPRI_MED); 2764 if (datamp == NULL) { 2765 error = EAGAIN; 2766 2767 break; 2768 } 2769 2770 rval = USBSER_DS_GET_MODEM_CTL(pp, -1, (int *)datamp->b_rptr); 2771 if (rval != USB_SUCCESS) { 2772 error = EIO; 2773 2774 break; 2775 } 2776 2777 if (iocp->ioc_count == TRANSPARENT) { 2778 mcopyout(mp, NULL, sizeof (int), NULL, datamp); 2779 } else { 2780 if (mp->b_cont != NULL) { 2781 freemsg(mp->b_cont); 2782 } 2783 mp->b_cont = datamp; 2784 mp->b_cont->b_wptr += sizeof (int); 2785 iocp->ioc_count = sizeof (int); 2786 } 2787 2788 break; 2789 case CONSOPENPOLLEDIO: 2790 error = usbser_polledio_init(pp); 2791 if (error != 0) 2792 2793 break; 2794 2795 error = miocpullup(mp, sizeof (struct cons_polledio *)); 2796 if (error != 0) 2797 2798 break; 2799 2800 *(struct cons_polledio **)mp->b_cont->b_rptr = &usbser_polledio; 2801 2802 mp->b_datap->db_type = M_IOCACK; 2803 2804 break; 2805 case CONSCLOSEPOLLEDIO: 2806 usbser_polledio_fini(pp); 2807 mp->b_datap->db_type = M_IOCACK; 2808 mp->b_datap->db_type = M_IOCACK; 2809 iocp->ioc_error = 0; 2810 iocp->ioc_rval = 0; 2811 2812 break; 2813 case CONSSETABORTENABLE: 2814 error = secpolicy_console(iocp->ioc_cr); 2815 if (error != 0) 2816 2817 break; 2818 2819 if (iocp->ioc_count != TRANSPARENT) { 2820 error = EINVAL; 2821 2822 break; 2823 } 2824 2825 /* 2826 * To do: implement console abort support 2827 * This involves adding a console flag to usbser 2828 * state structure. If flag is set, parse input stream 2829 * for abort sequence (see asy for example). 2830 * 2831 * For now, run mdb -K to get kmdb prompt. 2832 */ 2833 if (*(intptr_t *)mp->b_cont->b_rptr) 2834 usbser_console_abort = 1; 2835 else 2836 usbser_console_abort = 0; 2837 2838 mp->b_datap->db_type = M_IOCACK; 2839 iocp->ioc_error = 0; 2840 iocp->ioc_rval = 0; 2841 2842 break; 2843 case CONSGETABORTENABLE: 2844 /*CONSTANTCONDITION*/ 2845 ASSERT(sizeof (boolean_t) <= sizeof (boolean_t *)); 2846 /* 2847 * Store the return value right in the payload 2848 * we were passed. Crude. 2849 */ 2850 mcopyout(mp, NULL, sizeof (boolean_t), NULL, NULL); 2851 *(boolean_t *)mp->b_cont->b_rptr = (usbser_console_abort != 0); 2852 2853 break; 2854 default: 2855 error = EINVAL; 2856 2857 break; 2858 } 2859 end: 2860 if (error != 0) { 2861 iocp->ioc_error = error; 2862 mp->b_datap->db_type = M_IOCNAK; 2863 } 2864 qreply(q, mp); 2865 2866 mutex_enter(&pp->port_mutex); 2867 usbser_release_port_act(pp, USBSER_ACT_CTL); 2868 2869 return (USB_SUCCESS); 2870 } 2871 2872 2873 /* 2874 * process M_IOCDATA message 2875 */ 2876 static void 2877 usbser_iocdata(usbser_port_t *pp, mblk_t *mp) 2878 { 2879 tty_common_t *tp = &pp->port_ttycommon; 2880 queue_t *q = tp->t_writeq; 2881 struct iocblk *ip; 2882 struct copyresp *csp; 2883 int cmd; 2884 int val; 2885 int rval; 2886 2887 ASSERT(mutex_owned(&pp->port_mutex)); 2888 2889 ip = (struct iocblk *)mp->b_rptr; 2890 csp = (struct copyresp *)mp->b_rptr; 2891 cmd = csp->cp_cmd; 2892 2893 if (csp->cp_rval != 0) { 2894 freemsg(mp); 2895 2896 return; 2897 } 2898 2899 switch (cmd) { 2900 case TIOCMSET: 2901 case TIOCMBIS: 2902 case TIOCMBIC: 2903 if ((mp->b_cont == NULL) || 2904 (MBLKL(mp->b_cont) < sizeof (int))) { 2905 miocnak(q, mp, 0, EINVAL); 2906 2907 break; 2908 } 2909 val = *(int *)mp->b_cont->b_rptr; 2910 2911 usbser_serialize_port_act(pp, USBSER_ACT_CTL); 2912 2913 mutex_exit(&pp->port_mutex); 2914 if (cmd == TIOCMSET) { 2915 rval = USBSER_DS_SET_MODEM_CTL(pp, -1, val); 2916 } else if (cmd == TIOCMBIS) { 2917 rval = USBSER_DS_SET_MODEM_CTL(pp, val, -1); 2918 } else if (cmd == TIOCMBIC) { 2919 rval = USBSER_DS_SET_MODEM_CTL(pp, val, 0); 2920 } 2921 2922 if (mp->b_cont) { 2923 freemsg(mp->b_cont); 2924 mp->b_cont = NULL; 2925 } 2926 ip->ioc_rval = 0; 2927 if (rval == USB_SUCCESS) { 2928 miocack(q, mp, 0, 0); 2929 } else { 2930 miocnak(q, mp, 0, EIO); 2931 } 2932 mutex_enter(&pp->port_mutex); 2933 2934 usbser_release_port_act(pp, USBSER_ACT_CTL); 2935 2936 break; 2937 case TIOCMGET: 2938 mutex_exit(&pp->port_mutex); 2939 if (mp->b_cont) { 2940 freemsg(mp->b_cont); 2941 mp->b_cont = NULL; 2942 } 2943 ip->ioc_rval = 0; 2944 miocack(q, mp, 0, 0); 2945 mutex_enter(&pp->port_mutex); 2946 2947 break; 2948 default: 2949 mutex_exit(&pp->port_mutex); 2950 miocnak(q, mp, 0, EINVAL); 2951 mutex_enter(&pp->port_mutex); 2952 2953 break; 2954 } 2955 } 2956 2957 2958 /* 2959 * handle M_START[I]/M_STOP[I] messages 2960 */ 2961 static void 2962 usbser_stop(usbser_port_t *pp, mblk_t *mp) 2963 { 2964 usbser_st_mstop++; 2965 if (!(pp->port_flags & USBSER_FL_TX_STOPPED)) { 2966 usbser_serialize_port_act(pp, USBSER_ACT_CTL); 2967 pp->port_flags |= USBSER_FL_TX_STOPPED; 2968 2969 mutex_exit(&pp->port_mutex); 2970 USBSER_DS_STOP(pp, DS_TX); 2971 mutex_enter(&pp->port_mutex); 2972 2973 usbser_release_port_act(pp, USBSER_ACT_TX); 2974 usbser_release_port_act(pp, USBSER_ACT_CTL); 2975 } 2976 freemsg(mp); 2977 } 2978 2979 2980 static void 2981 usbser_start(usbser_port_t *pp, mblk_t *mp) 2982 { 2983 usbser_st_mstart++; 2984 if (pp->port_flags & USBSER_FL_TX_STOPPED) { 2985 usbser_serialize_port_act(pp, USBSER_ACT_CTL); 2986 pp->port_flags &= ~USBSER_FL_TX_STOPPED; 2987 2988 mutex_exit(&pp->port_mutex); 2989 USBSER_DS_START(pp, DS_TX); 2990 mutex_enter(&pp->port_mutex); 2991 usbser_release_port_act(pp, USBSER_ACT_CTL); 2992 } 2993 freemsg(mp); 2994 } 2995 2996 2997 static void 2998 usbser_stopi(usbser_port_t *pp, mblk_t *mp) 2999 { 3000 usbser_st_mstopi++; 3001 usbser_serialize_port_act(pp, USBSER_ACT_CTL); 3002 pp->port_flowc = pp->port_ttycommon.t_stopc; 3003 usbser_inbound_flow_ctl(pp); 3004 usbser_release_port_act(pp, USBSER_ACT_CTL); 3005 freemsg(mp); 3006 } 3007 3008 static void 3009 usbser_starti(usbser_port_t *pp, mblk_t *mp) 3010 { 3011 usbser_st_mstarti++; 3012 usbser_serialize_port_act(pp, USBSER_ACT_CTL); 3013 pp->port_flowc = pp->port_ttycommon.t_startc; 3014 usbser_inbound_flow_ctl(pp); 3015 usbser_release_port_act(pp, USBSER_ACT_CTL); 3016 freemsg(mp); 3017 } 3018 3019 /* 3020 * process M_FLUSH message 3021 */ 3022 static void 3023 usbser_flush(usbser_port_t *pp, mblk_t *mp) 3024 { 3025 queue_t *q = pp->port_ttycommon.t_writeq; 3026 3027 if (*mp->b_rptr & FLUSHW) { 3028 mutex_exit(&pp->port_mutex); 3029 (void) USBSER_DS_FIFO_FLUSH(pp, DS_TX); /* flush FIFO buffers */ 3030 flushq(q, FLUSHDATA); /* flush write queue */ 3031 mutex_enter(&pp->port_mutex); 3032 3033 usbser_release_port_act(pp, USBSER_ACT_TX); 3034 3035 *mp->b_rptr &= ~FLUSHW; 3036 } 3037 if (*mp->b_rptr & FLUSHR) { 3038 /* 3039 * flush FIFO buffers 3040 */ 3041 mutex_exit(&pp->port_mutex); 3042 (void) USBSER_DS_FIFO_FLUSH(pp, DS_RX); 3043 flushq(RD(q), FLUSHDATA); 3044 qreply(q, mp); 3045 mutex_enter(&pp->port_mutex); 3046 } else { 3047 freemsg(mp); 3048 } 3049 } 3050 3051 /* 3052 * process M_BREAK message 3053 */ 3054 static void 3055 usbser_break(usbser_port_t *pp, mblk_t *mp) 3056 { 3057 int rval; 3058 3059 /* 3060 * set the break and arrange for usbser_restart() to be called in 1/4 s 3061 */ 3062 mutex_exit(&pp->port_mutex); 3063 rval = USBSER_DS_BREAK_CTL(pp, DS_ON); 3064 mutex_enter(&pp->port_mutex); 3065 3066 if (rval == USB_SUCCESS) { 3067 pp->port_act |= USBSER_ACT_BREAK; 3068 pp->port_delay_id = timeout(usbser_restart, pp, 3069 drv_usectohz(250000)); 3070 } 3071 freemsg(mp); 3072 } 3073 3074 3075 /* 3076 * process M_DELAY message 3077 */ 3078 static void 3079 usbser_delay(usbser_port_t *pp, mblk_t *mp) 3080 { 3081 /* 3082 * arrange for usbser_restart() to be called when the delay expires 3083 */ 3084 pp->port_act |= USBSER_ACT_DELAY; 3085 pp->port_delay_id = timeout(usbser_restart, pp, 3086 (clock_t)(*(uchar_t *)mp->b_rptr + 6)); 3087 freemsg(mp); 3088 } 3089 3090 3091 /* 3092 * restart output on a line after a delay or break timer expired 3093 */ 3094 static void 3095 usbser_restart(void *arg) 3096 { 3097 usbser_port_t *pp = (usbser_port_t *)arg; 3098 3099 USB_DPRINTF_L4(DPRINT_WQ, pp->port_lh, "usbser_restart"); 3100 3101 mutex_enter(&pp->port_mutex); 3102 /* if cancelled, return immediately */ 3103 if (pp->port_delay_id == 0) { 3104 mutex_exit(&pp->port_mutex); 3105 3106 return; 3107 } 3108 pp->port_delay_id = 0; 3109 3110 /* clear break if necessary */ 3111 if (pp->port_act & USBSER_ACT_BREAK) { 3112 mutex_exit(&pp->port_mutex); 3113 (void) USBSER_DS_BREAK_CTL(pp, DS_OFF); 3114 mutex_enter(&pp->port_mutex); 3115 } 3116 3117 usbser_release_port_act(pp, USBSER_ACT_BREAK | USBSER_ACT_DELAY); 3118 3119 /* wake wq thread to resume message processing */ 3120 usbser_thr_wake(&pp->port_wq_thread); 3121 mutex_exit(&pp->port_mutex); 3122 } 3123 3124 3125 /* 3126 * program port hardware with the chosen parameters 3127 * most of the operation is based on the values of 'c_iflag' and 'c_cflag' 3128 */ 3129 static int 3130 usbser_port_program(usbser_port_t *pp) 3131 { 3132 tty_common_t *tp = &pp->port_ttycommon; 3133 int baudrate; 3134 int c_flag; 3135 ds_port_param_entry_t pe[6]; 3136 ds_port_params_t params; 3137 int flow_ctl, ctl_val; 3138 int err = 0; 3139 3140 baudrate = tp->t_cflag & CBAUD; 3141 if (tp->t_cflag & CBAUDEXT) { 3142 baudrate += 16; 3143 } 3144 3145 /* 3146 * set input speed same as output, as split speed not supported 3147 */ 3148 if (tp->t_cflag & (CIBAUD|CIBAUDEXT)) { 3149 tp->t_cflag &= ~(CIBAUD); 3150 if (baudrate > CBAUD) { 3151 tp->t_cflag |= CIBAUDEXT; 3152 tp->t_cflag |= 3153 (((baudrate - CBAUD - 1) << IBSHIFT) & CIBAUD); 3154 } else { 3155 tp->t_cflag &= ~CIBAUDEXT; 3156 tp->t_cflag |= ((baudrate << IBSHIFT) & CIBAUD); 3157 } 3158 } 3159 3160 c_flag = tp->t_cflag; 3161 3162 /* 3163 * flow control 3164 */ 3165 flow_ctl = tp->t_iflag & (IXON | IXANY | IXOFF); 3166 if (c_flag & CRTSCTS) { 3167 flow_ctl |= CTSXON; 3168 } 3169 if (c_flag & CRTSXOFF) { 3170 flow_ctl |= RTSXOFF; 3171 } 3172 3173 /* 3174 * fill in port parameters we need to set: 3175 * 3176 * baud rate 3177 */ 3178 pe[0].param = DS_PARAM_BAUD; 3179 pe[0].val.ui = baudrate; 3180 3181 /* stop bits */ 3182 pe[1].param = DS_PARAM_STOPB; 3183 pe[1].val.ui = c_flag & CSTOPB; 3184 3185 /* parity */ 3186 pe[2].param = DS_PARAM_PARITY; 3187 pe[2].val.ui = c_flag & (PARENB | PARODD); 3188 3189 /* char size */ 3190 pe[3].param = DS_PARAM_CHARSZ; 3191 pe[3].val.ui = c_flag & CSIZE; 3192 3193 /* start & stop chars */ 3194 pe[4].param = DS_PARAM_XON_XOFF; 3195 pe[4].val.uc[0] = tp->t_startc; 3196 pe[4].val.uc[1] = tp->t_stopc; 3197 3198 /* flow control */ 3199 pe[5].param = DS_PARAM_FLOW_CTL; 3200 pe[5].val.ui = flow_ctl; 3201 3202 params.tp_entries = &pe[0]; 3203 params.tp_cnt = 6; 3204 3205 /* control signals */ 3206 ctl_val = TIOCM_DTR | TIOCM_RTS; 3207 if (baudrate == 0) { 3208 ctl_val &= ~TIOCM_DTR; /* zero baudrate means drop DTR */ 3209 } 3210 if (pp->port_flags & USBSER_FL_RX_STOPPED) { 3211 ctl_val &= ~TIOCM_RTS; 3212 } 3213 3214 /* submit */ 3215 mutex_exit(&pp->port_mutex); 3216 err = USBSER_DS_SET_PORT_PARAMS(pp, ¶ms); 3217 if (err != USB_SUCCESS) { 3218 mutex_enter(&pp->port_mutex); 3219 3220 return (EINVAL); 3221 } 3222 3223 err = USBSER_DS_SET_MODEM_CTL(pp, TIOCM_DTR | TIOCM_RTS, ctl_val); 3224 mutex_enter(&pp->port_mutex); 3225 3226 return ((err == USB_SUCCESS) ? 0 : EIO); 3227 } 3228 3229 3230 /* 3231 * check if any inbound flow control action needed 3232 */ 3233 static void 3234 usbser_inbound_flow_ctl(usbser_port_t *pp) 3235 { 3236 tcflag_t need_hw; 3237 int rts; 3238 char c = pp->port_flowc; 3239 mblk_t *mp = NULL; 3240 3241 USB_DPRINTF_L4(DPRINT_WQ, pp->port_lh, 3242 "usbser_inbound_flow_ctl: c=%x cflag=%x port_flags=%x", 3243 c, pp->port_ttycommon.t_cflag, pp->port_flags); 3244 3245 if (c == '\0') { 3246 3247 return; 3248 } 3249 pp->port_flowc = '\0'; 3250 3251 /* 3252 * if inbound hardware flow control enabled, we need to frob RTS 3253 */ 3254 need_hw = (pp->port_ttycommon.t_cflag & CRTSXOFF); 3255 if (c == pp->port_ttycommon.t_startc) { 3256 rts = TIOCM_RTS; 3257 pp->port_flags &= ~USBSER_FL_RX_STOPPED; 3258 } else { 3259 rts = 0; 3260 pp->port_flags |= USBSER_FL_RX_STOPPED; 3261 } 3262 3263 /* 3264 * if character flow control active, transmit a start or stop char, 3265 */ 3266 if (pp->port_ttycommon.t_iflag & IXOFF) { 3267 if ((mp = allocb(1, BPRI_LO)) == NULL) { 3268 USB_DPRINTF_L2(DPRINT_WQ, pp->port_lh, 3269 "usbser_inbound_flow_ctl: allocb failed"); 3270 } else { 3271 *mp->b_wptr++ = c; 3272 pp->port_flags |= USBSER_ACT_TX; 3273 } 3274 } 3275 3276 mutex_exit(&pp->port_mutex); 3277 if (need_hw) { 3278 (void) USBSER_DS_SET_MODEM_CTL(pp, TIOCM_RTS, rts); 3279 } 3280 if (mp) { 3281 (void) USBSER_DS_TX(pp, mp); 3282 } 3283 mutex_enter(&pp->port_mutex); 3284 } 3285 3286 3287 /* 3288 * misc 3289 * ---- 3290 * 3291 * 3292 * returns !=0 if device is online, 0 otherwise 3293 */ 3294 static int 3295 usbser_dev_is_online(usbser_state_t *usp) 3296 { 3297 int rval; 3298 3299 mutex_enter(&usp->us_mutex); 3300 rval = (usp->us_dev_state == USB_DEV_ONLINE); 3301 mutex_exit(&usp->us_mutex); 3302 3303 return (rval); 3304 } 3305 3306 /* 3307 * serialize port activities defined by 'act' mask 3308 */ 3309 static void 3310 usbser_serialize_port_act(usbser_port_t *pp, int act) 3311 { 3312 while (pp->port_act & act) { 3313 cv_wait(&pp->port_act_cv, &pp->port_mutex); 3314 } 3315 3316 pp->port_act |= act; 3317 } 3318 3319 3320 /* 3321 * indicate that port activity is finished 3322 */ 3323 static void 3324 usbser_release_port_act(usbser_port_t *pp, int act) 3325 { 3326 pp->port_act &= ~act; 3327 cv_broadcast(&pp->port_act_cv); 3328 } 3329 3330 3331 /* 3332 * message type to string and back conversion. 3333 * 3334 * pardon breaks on the same line, but as long as cstyle doesn't 3335 * complain, I'd like to keep this form for trivial cases like this. 3336 * associative arrays in the kernel, anyone? 3337 */ 3338 static char * 3339 usbser_msgtype2str(int type) 3340 { 3341 char *str; 3342 3343 switch (type) { 3344 case M_STOP: str = "M_STOP"; break; 3345 case M_START: str = "M_START"; break; 3346 case M_STOPI: str = "M_STOPI"; break; 3347 case M_STARTI: str = "M_STARTI"; break; 3348 case M_DATA: str = "M_DATA"; break; 3349 case M_DELAY: str = "M_DELAY"; break; 3350 case M_BREAK: str = "M_BREAK"; break; 3351 case M_IOCTL: str = "M_IOCTL"; break; 3352 case M_IOCDATA: str = "M_IOCDATA"; break; 3353 case M_FLUSH: str = "M_FLUSH"; break; 3354 case M_CTL: str = "M_CTL"; break; 3355 case M_READ: str = "M_READ"; break; 3356 default: str = "unknown"; break; 3357 } 3358 3359 return (str); 3360 } 3361 3362 3363 static char * 3364 usbser_ioctl2str(int ioctl) 3365 { 3366 char *str; 3367 3368 switch (ioctl) { 3369 case TCGETA: str = "TCGETA"; break; 3370 case TCSETA: str = "TCSETA"; break; 3371 case TCSETAF: str = "TCSETAF"; break; 3372 case TCSETAW: str = "TCSETAW"; break; 3373 case TCSBRK: str = "TCSBRK"; break; 3374 case TCXONC: str = "TCXONC"; break; 3375 case TCFLSH: str = "TCFLSH"; break; 3376 case TCGETS: str = "TCGETS"; break; 3377 case TCSETS: str = "TCSETS"; break; 3378 case TCSETSF: str = "TCSETSF"; break; 3379 case TCSETSW: str = "TCSETSW"; break; 3380 case TIOCSBRK: str = "TIOCSBRK"; break; 3381 case TIOCCBRK: str = "TIOCCBRK"; break; 3382 case TIOCMSET: str = "TIOCMSET"; break; 3383 case TIOCMBIS: str = "TIOCMBIS"; break; 3384 case TIOCMBIC: str = "TIOCMBIC"; break; 3385 case TIOCMGET: str = "TIOCMGET"; break; 3386 case (tIOC | 109): str = "TIOCSILOOP"; break; 3387 case (tIOC | 108): str = "TIOCCILOOP"; break; 3388 case TCGETX: str = "TCGETX"; break; 3389 case TCSETX: str = "TCGETX"; break; 3390 case TCSETXW: str = "TCGETX"; break; 3391 case TCSETXF: str = "TCGETX"; break; 3392 default: str = "unknown"; break; 3393 } 3394 3395 return (str); 3396 } 3397 3398 /* 3399 * Polled IO support 3400 */ 3401 3402 /* called once by consconfig() when polledio is opened */ 3403 static int 3404 usbser_polledio_init(usbser_port_t *pp) 3405 { 3406 int err; 3407 usb_pipe_handle_t hdl; 3408 ds_ops_t *ds_ops = pp->port_ds_ops; 3409 3410 /* only one serial line console supported */ 3411 if (console_input != NULL) 3412 3413 return (USB_FAILURE); 3414 3415 /* check if underlying driver supports polled io */ 3416 if (ds_ops->ds_version < DS_OPS_VERSION_V1 || 3417 ds_ops->ds_out_pipe == NULL || ds_ops->ds_in_pipe == NULL) 3418 3419 return (USB_FAILURE); 3420 3421 /* init polled input pipe */ 3422 hdl = ds_ops->ds_in_pipe(pp->port_ds_hdl, pp->port_num); 3423 err = usb_console_input_init(pp->port_usp->us_dip, hdl, 3424 &console_input_buf, &console_input); 3425 if (err) 3426 3427 return (USB_FAILURE); 3428 3429 /* init polled output pipe */ 3430 hdl = ds_ops->ds_out_pipe(pp->port_ds_hdl, pp->port_num); 3431 err = usb_console_output_init(pp->port_usp->us_dip, hdl, 3432 &console_output); 3433 if (err) { 3434 (void) usb_console_input_fini(console_input); 3435 console_input = NULL; 3436 3437 return (USB_FAILURE); 3438 } 3439 3440 return (USB_SUCCESS); 3441 } 3442 3443 /* called once by consconfig() when polledio is closed */ 3444 /*ARGSUSED*/ 3445 static void usbser_polledio_fini(usbser_port_t *pp) 3446 { 3447 /* Since we can't move the console, there is nothing to do. */ 3448 } 3449 3450 /*ARGSUSED*/ 3451 static void 3452 usbser_polledio_enter(cons_polledio_arg_t arg) 3453 { 3454 (void) usb_console_input_enter(console_input); 3455 (void) usb_console_output_enter(console_output); 3456 } 3457 3458 /*ARGSUSED*/ 3459 static void 3460 usbser_polledio_exit(cons_polledio_arg_t arg) 3461 { 3462 (void) usb_console_output_exit(console_output); 3463 (void) usb_console_input_exit(console_input); 3464 } 3465 3466 /*ARGSUSED*/ 3467 static void 3468 usbser_putchar(cons_polledio_arg_t arg, uchar_t c) 3469 { 3470 static uchar_t cr[2] = {'\r', '\n'}; 3471 uint_t nout; 3472 3473 if (c == '\n') 3474 (void) usb_console_write(console_output, cr, 2, &nout); 3475 else 3476 (void) usb_console_write(console_output, &c, 1, &nout); 3477 } 3478 3479 /*ARGSUSED*/ 3480 static int 3481 usbser_getchar(cons_polledio_arg_t arg) 3482 { 3483 while (!usbser_ischar(arg)) 3484 ; 3485 3486 return (*console_input_start++); 3487 } 3488 3489 /*ARGSUSED*/ 3490 static boolean_t 3491 usbser_ischar(cons_polledio_arg_t arg) 3492 { 3493 uint_t num_bytes; 3494 3495 if (console_input_start < console_input_end) 3496 3497 return (1); 3498 3499 if (usb_console_read(console_input, &num_bytes) != USB_SUCCESS) 3500 3501 return (0); 3502 3503 console_input_start = console_input_buf; 3504 console_input_end = console_input_buf + num_bytes; 3505 3506 return (num_bytes != 0); 3507 } 3508