1 /* 2 Keyspan USB to Serial Converter driver 3 4 (C) Copyright (C) 2000-2001 Hugh Blemings <hugh@blemings.org> 5 (C) Copyright (C) 2002 Greg Kroah-Hartman <greg@kroah.com> 6 7 This program is free software; you can redistribute it and/or modify 8 it under the terms of the GNU General Public License as published by 9 the Free Software Foundation; either version 2 of the License, or 10 (at your option) any later version. 11 12 See http://blemings.org/hugh/keyspan.html for more information. 13 14 Code in this driver inspired by and in a number of places taken 15 from Brian Warner's original Keyspan-PDA driver. 16 17 This driver has been put together with the support of Innosys, Inc. 18 and Keyspan, Inc the manufacturers of the Keyspan USB-serial products. 19 Thanks Guys :) 20 21 Thanks to Paulus for miscellaneous tidy ups, some largish chunks 22 of much nicer and/or completely new code and (perhaps most uniquely) 23 having the patience to sit down and explain why and where he'd changed 24 stuff. 25 26 Tip 'o the hat to IBM (and previously Linuxcare :) for supporting 27 staff in their work on open source projects. 28 */ 29 30 31 #include <linux/kernel.h> 32 #include <linux/jiffies.h> 33 #include <linux/errno.h> 34 #include <linux/init.h> 35 #include <linux/slab.h> 36 #include <linux/tty.h> 37 #include <linux/tty_driver.h> 38 #include <linux/tty_flip.h> 39 #include <linux/module.h> 40 #include <linux/spinlock.h> 41 #include <linux/firmware.h> 42 #include <linux/ihex.h> 43 #include <linux/uaccess.h> 44 #include <linux/usb.h> 45 #include <linux/usb/serial.h> 46 #include "keyspan.h" 47 48 static bool debug; 49 50 /* 51 * Version Information 52 */ 53 #define DRIVER_VERSION "v1.1.5" 54 #define DRIVER_AUTHOR "Hugh Blemings <hugh@misc.nu" 55 #define DRIVER_DESC "Keyspan USB to Serial Converter Driver" 56 57 #define INSTAT_BUFLEN 32 58 #define GLOCONT_BUFLEN 64 59 #define INDAT49W_BUFLEN 512 60 61 /* Per device and per port private data */ 62 struct keyspan_serial_private { 63 const struct keyspan_device_details *device_details; 64 65 struct urb *instat_urb; 66 char instat_buf[INSTAT_BUFLEN]; 67 68 /* added to support 49wg, where data from all 4 ports comes in 69 on 1 EP and high-speed supported */ 70 struct urb *indat_urb; 71 char indat_buf[INDAT49W_BUFLEN]; 72 73 /* XXX this one probably will need a lock */ 74 struct urb *glocont_urb; 75 char glocont_buf[GLOCONT_BUFLEN]; 76 char ctrl_buf[8]; /* for EP0 control message */ 77 }; 78 79 struct keyspan_port_private { 80 /* Keep track of which input & output endpoints to use */ 81 int in_flip; 82 int out_flip; 83 84 /* Keep duplicate of device details in each port 85 structure as well - simplifies some of the 86 callback functions etc. */ 87 const struct keyspan_device_details *device_details; 88 89 /* Input endpoints and buffer for this port */ 90 struct urb *in_urbs[2]; 91 char in_buffer[2][64]; 92 /* Output endpoints and buffer for this port */ 93 struct urb *out_urbs[2]; 94 char out_buffer[2][64]; 95 96 /* Input ack endpoint */ 97 struct urb *inack_urb; 98 char inack_buffer[1]; 99 100 /* Output control endpoint */ 101 struct urb *outcont_urb; 102 char outcont_buffer[64]; 103 104 /* Settings for the port */ 105 int baud; 106 int old_baud; 107 unsigned int cflag; 108 unsigned int old_cflag; 109 enum {flow_none, flow_cts, flow_xon} flow_control; 110 int rts_state; /* Handshaking pins (outputs) */ 111 int dtr_state; 112 int cts_state; /* Handshaking pins (inputs) */ 113 int dsr_state; 114 int dcd_state; 115 int ri_state; 116 int break_on; 117 118 unsigned long tx_start_time[2]; 119 int resend_cont; /* need to resend control packet */ 120 }; 121 122 /* Include Keyspan message headers. All current Keyspan Adapters 123 make use of one of five message formats which are referred 124 to as USA-26, USA-28, USA-49, USA-90, USA-67 by Keyspan and 125 within this driver. */ 126 #include "keyspan_usa26msg.h" 127 #include "keyspan_usa28msg.h" 128 #include "keyspan_usa49msg.h" 129 #include "keyspan_usa90msg.h" 130 #include "keyspan_usa67msg.h" 131 132 133 module_usb_serial_driver(keyspan_driver, serial_drivers); 134 135 static void keyspan_break_ctl(struct tty_struct *tty, int break_state) 136 { 137 struct usb_serial_port *port = tty->driver_data; 138 struct keyspan_port_private *p_priv; 139 140 dbg("%s", __func__); 141 142 p_priv = usb_get_serial_port_data(port); 143 144 if (break_state == -1) 145 p_priv->break_on = 1; 146 else 147 p_priv->break_on = 0; 148 149 keyspan_send_setup(port, 0); 150 } 151 152 153 static void keyspan_set_termios(struct tty_struct *tty, 154 struct usb_serial_port *port, struct ktermios *old_termios) 155 { 156 int baud_rate, device_port; 157 struct keyspan_port_private *p_priv; 158 const struct keyspan_device_details *d_details; 159 unsigned int cflag; 160 161 dbg("%s", __func__); 162 163 p_priv = usb_get_serial_port_data(port); 164 d_details = p_priv->device_details; 165 cflag = tty->termios->c_cflag; 166 device_port = port->number - port->serial->minor; 167 168 /* Baud rate calculation takes baud rate as an integer 169 so other rates can be generated if desired. */ 170 baud_rate = tty_get_baud_rate(tty); 171 /* If no match or invalid, don't change */ 172 if (d_details->calculate_baud_rate(baud_rate, d_details->baudclk, 173 NULL, NULL, NULL, device_port) == KEYSPAN_BAUD_RATE_OK) { 174 /* FIXME - more to do here to ensure rate changes cleanly */ 175 /* FIXME - calcuate exact rate from divisor ? */ 176 p_priv->baud = baud_rate; 177 } else 178 baud_rate = tty_termios_baud_rate(old_termios); 179 180 tty_encode_baud_rate(tty, baud_rate, baud_rate); 181 /* set CTS/RTS handshake etc. */ 182 p_priv->cflag = cflag; 183 p_priv->flow_control = (cflag & CRTSCTS)? flow_cts: flow_none; 184 185 /* Mark/Space not supported */ 186 tty->termios->c_cflag &= ~CMSPAR; 187 188 keyspan_send_setup(port, 0); 189 } 190 191 static int keyspan_tiocmget(struct tty_struct *tty) 192 { 193 struct usb_serial_port *port = tty->driver_data; 194 struct keyspan_port_private *p_priv = usb_get_serial_port_data(port); 195 unsigned int value; 196 197 value = ((p_priv->rts_state) ? TIOCM_RTS : 0) | 198 ((p_priv->dtr_state) ? TIOCM_DTR : 0) | 199 ((p_priv->cts_state) ? TIOCM_CTS : 0) | 200 ((p_priv->dsr_state) ? TIOCM_DSR : 0) | 201 ((p_priv->dcd_state) ? TIOCM_CAR : 0) | 202 ((p_priv->ri_state) ? TIOCM_RNG : 0); 203 204 return value; 205 } 206 207 static int keyspan_tiocmset(struct tty_struct *tty, 208 unsigned int set, unsigned int clear) 209 { 210 struct usb_serial_port *port = tty->driver_data; 211 struct keyspan_port_private *p_priv = usb_get_serial_port_data(port); 212 213 if (set & TIOCM_RTS) 214 p_priv->rts_state = 1; 215 if (set & TIOCM_DTR) 216 p_priv->dtr_state = 1; 217 if (clear & TIOCM_RTS) 218 p_priv->rts_state = 0; 219 if (clear & TIOCM_DTR) 220 p_priv->dtr_state = 0; 221 keyspan_send_setup(port, 0); 222 return 0; 223 } 224 225 /* Write function is similar for the four protocols used 226 with only a minor change for usa90 (usa19hs) required */ 227 static int keyspan_write(struct tty_struct *tty, 228 struct usb_serial_port *port, const unsigned char *buf, int count) 229 { 230 struct keyspan_port_private *p_priv; 231 const struct keyspan_device_details *d_details; 232 int flip; 233 int left, todo; 234 struct urb *this_urb; 235 int err, maxDataLen, dataOffset; 236 237 p_priv = usb_get_serial_port_data(port); 238 d_details = p_priv->device_details; 239 240 if (d_details->msg_format == msg_usa90) { 241 maxDataLen = 64; 242 dataOffset = 0; 243 } else { 244 maxDataLen = 63; 245 dataOffset = 1; 246 } 247 248 dbg("%s - for port %d (%d chars), flip=%d", 249 __func__, port->number, count, p_priv->out_flip); 250 251 for (left = count; left > 0; left -= todo) { 252 todo = left; 253 if (todo > maxDataLen) 254 todo = maxDataLen; 255 256 flip = p_priv->out_flip; 257 258 /* Check we have a valid urb/endpoint before we use it... */ 259 this_urb = p_priv->out_urbs[flip]; 260 if (this_urb == NULL) { 261 /* no bulk out, so return 0 bytes written */ 262 dbg("%s - no output urb :(", __func__); 263 return count; 264 } 265 266 dbg("%s - endpoint %d flip %d", 267 __func__, usb_pipeendpoint(this_urb->pipe), flip); 268 269 if (this_urb->status == -EINPROGRESS) { 270 if (time_before(jiffies, 271 p_priv->tx_start_time[flip] + 10 * HZ)) 272 break; 273 usb_unlink_urb(this_urb); 274 break; 275 } 276 277 /* First byte in buffer is "last flag" (except for usa19hx) 278 - unused so for now so set to zero */ 279 ((char *)this_urb->transfer_buffer)[0] = 0; 280 281 memcpy(this_urb->transfer_buffer + dataOffset, buf, todo); 282 buf += todo; 283 284 /* send the data out the bulk port */ 285 this_urb->transfer_buffer_length = todo + dataOffset; 286 287 err = usb_submit_urb(this_urb, GFP_ATOMIC); 288 if (err != 0) 289 dbg("usb_submit_urb(write bulk) failed (%d)", err); 290 p_priv->tx_start_time[flip] = jiffies; 291 292 /* Flip for next time if usa26 or usa28 interface 293 (not used on usa49) */ 294 p_priv->out_flip = (flip + 1) & d_details->outdat_endp_flip; 295 } 296 297 return count - left; 298 } 299 300 static void usa26_indat_callback(struct urb *urb) 301 { 302 int i, err; 303 int endpoint; 304 struct usb_serial_port *port; 305 struct tty_struct *tty; 306 unsigned char *data = urb->transfer_buffer; 307 int status = urb->status; 308 309 dbg("%s", __func__); 310 311 endpoint = usb_pipeendpoint(urb->pipe); 312 313 if (status) { 314 dbg("%s - nonzero status: %x on endpoint %d.", 315 __func__, status, endpoint); 316 return; 317 } 318 319 port = urb->context; 320 tty = tty_port_tty_get(&port->port); 321 if (tty && urb->actual_length) { 322 /* 0x80 bit is error flag */ 323 if ((data[0] & 0x80) == 0) { 324 /* no errors on individual bytes, only 325 possible overrun err */ 326 if (data[0] & RXERROR_OVERRUN) 327 err = TTY_OVERRUN; 328 else 329 err = 0; 330 for (i = 1; i < urb->actual_length ; ++i) 331 tty_insert_flip_char(tty, data[i], err); 332 } else { 333 /* some bytes had errors, every byte has status */ 334 dbg("%s - RX error!!!!", __func__); 335 for (i = 0; i + 1 < urb->actual_length; i += 2) { 336 int stat = data[i], flag = 0; 337 if (stat & RXERROR_OVERRUN) 338 flag |= TTY_OVERRUN; 339 if (stat & RXERROR_FRAMING) 340 flag |= TTY_FRAME; 341 if (stat & RXERROR_PARITY) 342 flag |= TTY_PARITY; 343 /* XXX should handle break (0x10) */ 344 tty_insert_flip_char(tty, data[i+1], flag); 345 } 346 } 347 tty_flip_buffer_push(tty); 348 } 349 tty_kref_put(tty); 350 351 /* Resubmit urb so we continue receiving */ 352 err = usb_submit_urb(urb, GFP_ATOMIC); 353 if (err != 0) 354 dbg("%s - resubmit read urb failed. (%d)", __func__, err); 355 } 356 357 /* Outdat handling is common for all devices */ 358 static void usa2x_outdat_callback(struct urb *urb) 359 { 360 struct usb_serial_port *port; 361 struct keyspan_port_private *p_priv; 362 363 port = urb->context; 364 p_priv = usb_get_serial_port_data(port); 365 dbg("%s - urb %d", __func__, urb == p_priv->out_urbs[1]); 366 367 usb_serial_port_softint(port); 368 } 369 370 static void usa26_inack_callback(struct urb *urb) 371 { 372 dbg("%s", __func__); 373 374 } 375 376 static void usa26_outcont_callback(struct urb *urb) 377 { 378 struct usb_serial_port *port; 379 struct keyspan_port_private *p_priv; 380 381 port = urb->context; 382 p_priv = usb_get_serial_port_data(port); 383 384 if (p_priv->resend_cont) { 385 dbg("%s - sending setup", __func__); 386 keyspan_usa26_send_setup(port->serial, port, 387 p_priv->resend_cont - 1); 388 } 389 } 390 391 static void usa26_instat_callback(struct urb *urb) 392 { 393 unsigned char *data = urb->transfer_buffer; 394 struct keyspan_usa26_portStatusMessage *msg; 395 struct usb_serial *serial; 396 struct usb_serial_port *port; 397 struct keyspan_port_private *p_priv; 398 struct tty_struct *tty; 399 int old_dcd_state, err; 400 int status = urb->status; 401 402 serial = urb->context; 403 404 if (status) { 405 dbg("%s - nonzero status: %x", __func__, status); 406 return; 407 } 408 if (urb->actual_length != 9) { 409 dbg("%s - %d byte report??", __func__, urb->actual_length); 410 goto exit; 411 } 412 413 msg = (struct keyspan_usa26_portStatusMessage *)data; 414 415 #if 0 416 dbg("%s - port status: port %d cts %d dcd %d dsr %d ri %d toff %d txoff %d rxen %d cr %d", 417 __func__, msg->port, msg->hskia_cts, msg->gpia_dcd, msg->dsr, msg->ri, msg->_txOff, 418 msg->_txXoff, msg->rxEnabled, msg->controlResponse); 419 #endif 420 421 /* Now do something useful with the data */ 422 423 424 /* Check port number from message and retrieve private data */ 425 if (msg->port >= serial->num_ports) { 426 dbg("%s - Unexpected port number %d", __func__, msg->port); 427 goto exit; 428 } 429 port = serial->port[msg->port]; 430 p_priv = usb_get_serial_port_data(port); 431 432 /* Update handshaking pin state information */ 433 old_dcd_state = p_priv->dcd_state; 434 p_priv->cts_state = ((msg->hskia_cts) ? 1 : 0); 435 p_priv->dsr_state = ((msg->dsr) ? 1 : 0); 436 p_priv->dcd_state = ((msg->gpia_dcd) ? 1 : 0); 437 p_priv->ri_state = ((msg->ri) ? 1 : 0); 438 439 if (old_dcd_state != p_priv->dcd_state) { 440 tty = tty_port_tty_get(&port->port); 441 if (tty && !C_CLOCAL(tty)) 442 tty_hangup(tty); 443 tty_kref_put(tty); 444 } 445 446 /* Resubmit urb so we continue receiving */ 447 err = usb_submit_urb(urb, GFP_ATOMIC); 448 if (err != 0) 449 dbg("%s - resubmit read urb failed. (%d)", __func__, err); 450 exit: ; 451 } 452 453 static void usa26_glocont_callback(struct urb *urb) 454 { 455 dbg("%s", __func__); 456 } 457 458 459 static void usa28_indat_callback(struct urb *urb) 460 { 461 int err; 462 struct usb_serial_port *port; 463 struct tty_struct *tty; 464 unsigned char *data; 465 struct keyspan_port_private *p_priv; 466 int status = urb->status; 467 468 dbg("%s", __func__); 469 470 port = urb->context; 471 p_priv = usb_get_serial_port_data(port); 472 data = urb->transfer_buffer; 473 474 if (urb != p_priv->in_urbs[p_priv->in_flip]) 475 return; 476 477 do { 478 if (status) { 479 dbg("%s - nonzero status: %x on endpoint %d.", 480 __func__, status, usb_pipeendpoint(urb->pipe)); 481 return; 482 } 483 484 port = urb->context; 485 p_priv = usb_get_serial_port_data(port); 486 data = urb->transfer_buffer; 487 488 tty =tty_port_tty_get(&port->port); 489 if (tty && urb->actual_length) { 490 tty_insert_flip_string(tty, data, urb->actual_length); 491 tty_flip_buffer_push(tty); 492 } 493 tty_kref_put(tty); 494 495 /* Resubmit urb so we continue receiving */ 496 err = usb_submit_urb(urb, GFP_ATOMIC); 497 if (err != 0) 498 dbg("%s - resubmit read urb failed. (%d)", 499 __func__, err); 500 p_priv->in_flip ^= 1; 501 502 urb = p_priv->in_urbs[p_priv->in_flip]; 503 } while (urb->status != -EINPROGRESS); 504 } 505 506 static void usa28_inack_callback(struct urb *urb) 507 { 508 dbg("%s", __func__); 509 } 510 511 static void usa28_outcont_callback(struct urb *urb) 512 { 513 struct usb_serial_port *port; 514 struct keyspan_port_private *p_priv; 515 516 port = urb->context; 517 p_priv = usb_get_serial_port_data(port); 518 519 if (p_priv->resend_cont) { 520 dbg("%s - sending setup", __func__); 521 keyspan_usa28_send_setup(port->serial, port, 522 p_priv->resend_cont - 1); 523 } 524 } 525 526 static void usa28_instat_callback(struct urb *urb) 527 { 528 int err; 529 unsigned char *data = urb->transfer_buffer; 530 struct keyspan_usa28_portStatusMessage *msg; 531 struct usb_serial *serial; 532 struct usb_serial_port *port; 533 struct keyspan_port_private *p_priv; 534 struct tty_struct *tty; 535 int old_dcd_state; 536 int status = urb->status; 537 538 serial = urb->context; 539 540 if (status) { 541 dbg("%s - nonzero status: %x", __func__, status); 542 return; 543 } 544 545 if (urb->actual_length != sizeof(struct keyspan_usa28_portStatusMessage)) { 546 dbg("%s - bad length %d", __func__, urb->actual_length); 547 goto exit; 548 } 549 550 /*dbg("%s %x %x %x %x %x %x %x %x %x %x %x %x", __func__ 551 data[0], data[1], data[2], data[3], data[4], data[5], 552 data[6], data[7], data[8], data[9], data[10], data[11]);*/ 553 554 /* Now do something useful with the data */ 555 msg = (struct keyspan_usa28_portStatusMessage *)data; 556 557 /* Check port number from message and retrieve private data */ 558 if (msg->port >= serial->num_ports) { 559 dbg("%s - Unexpected port number %d", __func__, msg->port); 560 goto exit; 561 } 562 port = serial->port[msg->port]; 563 p_priv = usb_get_serial_port_data(port); 564 565 /* Update handshaking pin state information */ 566 old_dcd_state = p_priv->dcd_state; 567 p_priv->cts_state = ((msg->cts) ? 1 : 0); 568 p_priv->dsr_state = ((msg->dsr) ? 1 : 0); 569 p_priv->dcd_state = ((msg->dcd) ? 1 : 0); 570 p_priv->ri_state = ((msg->ri) ? 1 : 0); 571 572 if( old_dcd_state != p_priv->dcd_state && old_dcd_state) { 573 tty = tty_port_tty_get(&port->port); 574 if (tty && !C_CLOCAL(tty)) 575 tty_hangup(tty); 576 tty_kref_put(tty); 577 } 578 579 /* Resubmit urb so we continue receiving */ 580 err = usb_submit_urb(urb, GFP_ATOMIC); 581 if (err != 0) 582 dbg("%s - resubmit read urb failed. (%d)", __func__, err); 583 exit: ; 584 } 585 586 static void usa28_glocont_callback(struct urb *urb) 587 { 588 dbg("%s", __func__); 589 } 590 591 592 static void usa49_glocont_callback(struct urb *urb) 593 { 594 struct usb_serial *serial; 595 struct usb_serial_port *port; 596 struct keyspan_port_private *p_priv; 597 int i; 598 599 dbg("%s", __func__); 600 601 serial = urb->context; 602 for (i = 0; i < serial->num_ports; ++i) { 603 port = serial->port[i]; 604 p_priv = usb_get_serial_port_data(port); 605 606 if (p_priv->resend_cont) { 607 dbg("%s - sending setup", __func__); 608 keyspan_usa49_send_setup(serial, port, 609 p_priv->resend_cont - 1); 610 break; 611 } 612 } 613 } 614 615 /* This is actually called glostat in the Keyspan 616 doco */ 617 static void usa49_instat_callback(struct urb *urb) 618 { 619 int err; 620 unsigned char *data = urb->transfer_buffer; 621 struct keyspan_usa49_portStatusMessage *msg; 622 struct usb_serial *serial; 623 struct usb_serial_port *port; 624 struct keyspan_port_private *p_priv; 625 int old_dcd_state; 626 int status = urb->status; 627 628 dbg("%s", __func__); 629 630 serial = urb->context; 631 632 if (status) { 633 dbg("%s - nonzero status: %x", __func__, status); 634 return; 635 } 636 637 if (urb->actual_length != 638 sizeof(struct keyspan_usa49_portStatusMessage)) { 639 dbg("%s - bad length %d", __func__, urb->actual_length); 640 goto exit; 641 } 642 643 /*dbg(" %x %x %x %x %x %x %x %x %x %x %x", __func__, 644 data[0], data[1], data[2], data[3], data[4], data[5], 645 data[6], data[7], data[8], data[9], data[10]);*/ 646 647 /* Now do something useful with the data */ 648 msg = (struct keyspan_usa49_portStatusMessage *)data; 649 650 /* Check port number from message and retrieve private data */ 651 if (msg->portNumber >= serial->num_ports) { 652 dbg("%s - Unexpected port number %d", 653 __func__, msg->portNumber); 654 goto exit; 655 } 656 port = serial->port[msg->portNumber]; 657 p_priv = usb_get_serial_port_data(port); 658 659 /* Update handshaking pin state information */ 660 old_dcd_state = p_priv->dcd_state; 661 p_priv->cts_state = ((msg->cts) ? 1 : 0); 662 p_priv->dsr_state = ((msg->dsr) ? 1 : 0); 663 p_priv->dcd_state = ((msg->dcd) ? 1 : 0); 664 p_priv->ri_state = ((msg->ri) ? 1 : 0); 665 666 if (old_dcd_state != p_priv->dcd_state && old_dcd_state) { 667 struct tty_struct *tty = tty_port_tty_get(&port->port); 668 if (tty && !C_CLOCAL(tty)) 669 tty_hangup(tty); 670 tty_kref_put(tty); 671 } 672 673 /* Resubmit urb so we continue receiving */ 674 err = usb_submit_urb(urb, GFP_ATOMIC); 675 if (err != 0) 676 dbg("%s - resubmit read urb failed. (%d)", __func__, err); 677 exit: ; 678 } 679 680 static void usa49_inack_callback(struct urb *urb) 681 { 682 dbg("%s", __func__); 683 } 684 685 static void usa49_indat_callback(struct urb *urb) 686 { 687 int i, err; 688 int endpoint; 689 struct usb_serial_port *port; 690 struct tty_struct *tty; 691 unsigned char *data = urb->transfer_buffer; 692 int status = urb->status; 693 694 dbg("%s", __func__); 695 696 endpoint = usb_pipeendpoint(urb->pipe); 697 698 if (status) { 699 dbg("%s - nonzero status: %x on endpoint %d.", __func__, 700 status, endpoint); 701 return; 702 } 703 704 port = urb->context; 705 tty = tty_port_tty_get(&port->port); 706 if (tty && urb->actual_length) { 707 /* 0x80 bit is error flag */ 708 if ((data[0] & 0x80) == 0) { 709 /* no error on any byte */ 710 tty_insert_flip_string(tty, data + 1, 711 urb->actual_length - 1); 712 } else { 713 /* some bytes had errors, every byte has status */ 714 for (i = 0; i + 1 < urb->actual_length; i += 2) { 715 int stat = data[i], flag = 0; 716 if (stat & RXERROR_OVERRUN) 717 flag |= TTY_OVERRUN; 718 if (stat & RXERROR_FRAMING) 719 flag |= TTY_FRAME; 720 if (stat & RXERROR_PARITY) 721 flag |= TTY_PARITY; 722 /* XXX should handle break (0x10) */ 723 tty_insert_flip_char(tty, data[i+1], flag); 724 } 725 } 726 tty_flip_buffer_push(tty); 727 } 728 tty_kref_put(tty); 729 730 /* Resubmit urb so we continue receiving */ 731 err = usb_submit_urb(urb, GFP_ATOMIC); 732 if (err != 0) 733 dbg("%s - resubmit read urb failed. (%d)", __func__, err); 734 } 735 736 static void usa49wg_indat_callback(struct urb *urb) 737 { 738 int i, len, x, err; 739 struct usb_serial *serial; 740 struct usb_serial_port *port; 741 struct tty_struct *tty; 742 unsigned char *data = urb->transfer_buffer; 743 int status = urb->status; 744 745 dbg("%s", __func__); 746 747 serial = urb->context; 748 749 if (status) { 750 dbg("%s - nonzero status: %x", __func__, status); 751 return; 752 } 753 754 /* inbound data is in the form P#, len, status, data */ 755 i = 0; 756 len = 0; 757 758 if (urb->actual_length) { 759 while (i < urb->actual_length) { 760 761 /* Check port number from message*/ 762 if (data[i] >= serial->num_ports) { 763 dbg("%s - Unexpected port number %d", 764 __func__, data[i]); 765 return; 766 } 767 port = serial->port[data[i++]]; 768 tty = tty_port_tty_get(&port->port); 769 len = data[i++]; 770 771 /* 0x80 bit is error flag */ 772 if ((data[i] & 0x80) == 0) { 773 /* no error on any byte */ 774 i++; 775 for (x = 1; x < len ; ++x) 776 tty_insert_flip_char(tty, data[i++], 0); 777 } else { 778 /* 779 * some bytes had errors, every byte has status 780 */ 781 for (x = 0; x + 1 < len; x += 2) { 782 int stat = data[i], flag = 0; 783 if (stat & RXERROR_OVERRUN) 784 flag |= TTY_OVERRUN; 785 if (stat & RXERROR_FRAMING) 786 flag |= TTY_FRAME; 787 if (stat & RXERROR_PARITY) 788 flag |= TTY_PARITY; 789 /* XXX should handle break (0x10) */ 790 tty_insert_flip_char(tty, 791 data[i+1], flag); 792 i += 2; 793 } 794 } 795 tty_flip_buffer_push(tty); 796 tty_kref_put(tty); 797 } 798 } 799 800 /* Resubmit urb so we continue receiving */ 801 err = usb_submit_urb(urb, GFP_ATOMIC); 802 if (err != 0) 803 dbg("%s - resubmit read urb failed. (%d)", __func__, err); 804 } 805 806 /* not used, usa-49 doesn't have per-port control endpoints */ 807 static void usa49_outcont_callback(struct urb *urb) 808 { 809 dbg("%s", __func__); 810 } 811 812 static void usa90_indat_callback(struct urb *urb) 813 { 814 int i, err; 815 int endpoint; 816 struct usb_serial_port *port; 817 struct keyspan_port_private *p_priv; 818 struct tty_struct *tty; 819 unsigned char *data = urb->transfer_buffer; 820 int status = urb->status; 821 822 dbg("%s", __func__); 823 824 endpoint = usb_pipeendpoint(urb->pipe); 825 826 if (status) { 827 dbg("%s - nonzero status: %x on endpoint %d.", 828 __func__, status, endpoint); 829 return; 830 } 831 832 port = urb->context; 833 p_priv = usb_get_serial_port_data(port); 834 835 if (urb->actual_length) { 836 tty = tty_port_tty_get(&port->port); 837 /* if current mode is DMA, looks like usa28 format 838 otherwise looks like usa26 data format */ 839 840 if (p_priv->baud > 57600) 841 tty_insert_flip_string(tty, data, urb->actual_length); 842 else { 843 /* 0x80 bit is error flag */ 844 if ((data[0] & 0x80) == 0) { 845 /* no errors on individual bytes, only 846 possible overrun err*/ 847 if (data[0] & RXERROR_OVERRUN) 848 err = TTY_OVERRUN; 849 else 850 err = 0; 851 for (i = 1; i < urb->actual_length ; ++i) 852 tty_insert_flip_char(tty, data[i], 853 err); 854 } else { 855 /* some bytes had errors, every byte has status */ 856 dbg("%s - RX error!!!!", __func__); 857 for (i = 0; i + 1 < urb->actual_length; i += 2) { 858 int stat = data[i], flag = 0; 859 if (stat & RXERROR_OVERRUN) 860 flag |= TTY_OVERRUN; 861 if (stat & RXERROR_FRAMING) 862 flag |= TTY_FRAME; 863 if (stat & RXERROR_PARITY) 864 flag |= TTY_PARITY; 865 /* XXX should handle break (0x10) */ 866 tty_insert_flip_char(tty, data[i+1], 867 flag); 868 } 869 } 870 } 871 tty_flip_buffer_push(tty); 872 tty_kref_put(tty); 873 } 874 875 /* Resubmit urb so we continue receiving */ 876 err = usb_submit_urb(urb, GFP_ATOMIC); 877 if (err != 0) 878 dbg("%s - resubmit read urb failed. (%d)", __func__, err); 879 } 880 881 882 static void usa90_instat_callback(struct urb *urb) 883 { 884 unsigned char *data = urb->transfer_buffer; 885 struct keyspan_usa90_portStatusMessage *msg; 886 struct usb_serial *serial; 887 struct usb_serial_port *port; 888 struct keyspan_port_private *p_priv; 889 struct tty_struct *tty; 890 int old_dcd_state, err; 891 int status = urb->status; 892 893 serial = urb->context; 894 895 if (status) { 896 dbg("%s - nonzero status: %x", __func__, status); 897 return; 898 } 899 if (urb->actual_length < 14) { 900 dbg("%s - %d byte report??", __func__, urb->actual_length); 901 goto exit; 902 } 903 904 msg = (struct keyspan_usa90_portStatusMessage *)data; 905 906 /* Now do something useful with the data */ 907 908 port = serial->port[0]; 909 p_priv = usb_get_serial_port_data(port); 910 911 /* Update handshaking pin state information */ 912 old_dcd_state = p_priv->dcd_state; 913 p_priv->cts_state = ((msg->cts) ? 1 : 0); 914 p_priv->dsr_state = ((msg->dsr) ? 1 : 0); 915 p_priv->dcd_state = ((msg->dcd) ? 1 : 0); 916 p_priv->ri_state = ((msg->ri) ? 1 : 0); 917 918 if (old_dcd_state != p_priv->dcd_state && old_dcd_state) { 919 tty = tty_port_tty_get(&port->port); 920 if (tty && !C_CLOCAL(tty)) 921 tty_hangup(tty); 922 tty_kref_put(tty); 923 } 924 925 /* Resubmit urb so we continue receiving */ 926 err = usb_submit_urb(urb, GFP_ATOMIC); 927 if (err != 0) 928 dbg("%s - resubmit read urb failed. (%d)", __func__, err); 929 exit: 930 ; 931 } 932 933 static void usa90_outcont_callback(struct urb *urb) 934 { 935 struct usb_serial_port *port; 936 struct keyspan_port_private *p_priv; 937 938 port = urb->context; 939 p_priv = usb_get_serial_port_data(port); 940 941 if (p_priv->resend_cont) { 942 dbg("%s - sending setup", __func__); 943 keyspan_usa90_send_setup(port->serial, port, 944 p_priv->resend_cont - 1); 945 } 946 } 947 948 /* Status messages from the 28xg */ 949 static void usa67_instat_callback(struct urb *urb) 950 { 951 int err; 952 unsigned char *data = urb->transfer_buffer; 953 struct keyspan_usa67_portStatusMessage *msg; 954 struct usb_serial *serial; 955 struct usb_serial_port *port; 956 struct keyspan_port_private *p_priv; 957 int old_dcd_state; 958 int status = urb->status; 959 960 dbg("%s", __func__); 961 962 serial = urb->context; 963 964 if (status) { 965 dbg("%s - nonzero status: %x", __func__, status); 966 return; 967 } 968 969 if (urb->actual_length != 970 sizeof(struct keyspan_usa67_portStatusMessage)) { 971 dbg("%s - bad length %d", __func__, urb->actual_length); 972 return; 973 } 974 975 976 /* Now do something useful with the data */ 977 msg = (struct keyspan_usa67_portStatusMessage *)data; 978 979 /* Check port number from message and retrieve private data */ 980 if (msg->port >= serial->num_ports) { 981 dbg("%s - Unexpected port number %d", __func__, msg->port); 982 return; 983 } 984 985 port = serial->port[msg->port]; 986 p_priv = usb_get_serial_port_data(port); 987 988 /* Update handshaking pin state information */ 989 old_dcd_state = p_priv->dcd_state; 990 p_priv->cts_state = ((msg->hskia_cts) ? 1 : 0); 991 p_priv->dcd_state = ((msg->gpia_dcd) ? 1 : 0); 992 993 if (old_dcd_state != p_priv->dcd_state && old_dcd_state) { 994 struct tty_struct *tty = tty_port_tty_get(&port->port); 995 if (tty && !C_CLOCAL(tty)) 996 tty_hangup(tty); 997 tty_kref_put(tty); 998 } 999 1000 /* Resubmit urb so we continue receiving */ 1001 err = usb_submit_urb(urb, GFP_ATOMIC); 1002 if (err != 0) 1003 dbg("%s - resubmit read urb failed. (%d)", __func__, err); 1004 } 1005 1006 static void usa67_glocont_callback(struct urb *urb) 1007 { 1008 struct usb_serial *serial; 1009 struct usb_serial_port *port; 1010 struct keyspan_port_private *p_priv; 1011 int i; 1012 1013 dbg("%s", __func__); 1014 1015 serial = urb->context; 1016 for (i = 0; i < serial->num_ports; ++i) { 1017 port = serial->port[i]; 1018 p_priv = usb_get_serial_port_data(port); 1019 1020 if (p_priv->resend_cont) { 1021 dbg("%s - sending setup", __func__); 1022 keyspan_usa67_send_setup(serial, port, 1023 p_priv->resend_cont - 1); 1024 break; 1025 } 1026 } 1027 } 1028 1029 static int keyspan_write_room(struct tty_struct *tty) 1030 { 1031 struct usb_serial_port *port = tty->driver_data; 1032 struct keyspan_port_private *p_priv; 1033 const struct keyspan_device_details *d_details; 1034 int flip; 1035 int data_len; 1036 struct urb *this_urb; 1037 1038 dbg("%s", __func__); 1039 p_priv = usb_get_serial_port_data(port); 1040 d_details = p_priv->device_details; 1041 1042 /* FIXME: locking */ 1043 if (d_details->msg_format == msg_usa90) 1044 data_len = 64; 1045 else 1046 data_len = 63; 1047 1048 flip = p_priv->out_flip; 1049 1050 /* Check both endpoints to see if any are available. */ 1051 this_urb = p_priv->out_urbs[flip]; 1052 if (this_urb != NULL) { 1053 if (this_urb->status != -EINPROGRESS) 1054 return data_len; 1055 flip = (flip + 1) & d_details->outdat_endp_flip; 1056 this_urb = p_priv->out_urbs[flip]; 1057 if (this_urb != NULL) { 1058 if (this_urb->status != -EINPROGRESS) 1059 return data_len; 1060 } 1061 } 1062 return 0; 1063 } 1064 1065 1066 static int keyspan_open(struct tty_struct *tty, struct usb_serial_port *port) 1067 { 1068 struct keyspan_port_private *p_priv; 1069 struct keyspan_serial_private *s_priv; 1070 struct usb_serial *serial = port->serial; 1071 const struct keyspan_device_details *d_details; 1072 int i, err; 1073 int baud_rate, device_port; 1074 struct urb *urb; 1075 unsigned int cflag = 0; 1076 1077 s_priv = usb_get_serial_data(serial); 1078 p_priv = usb_get_serial_port_data(port); 1079 d_details = p_priv->device_details; 1080 1081 dbg("%s - port%d.", __func__, port->number); 1082 1083 /* Set some sane defaults */ 1084 p_priv->rts_state = 1; 1085 p_priv->dtr_state = 1; 1086 p_priv->baud = 9600; 1087 1088 /* force baud and lcr to be set on open */ 1089 p_priv->old_baud = 0; 1090 p_priv->old_cflag = 0; 1091 1092 p_priv->out_flip = 0; 1093 p_priv->in_flip = 0; 1094 1095 /* Reset low level data toggle and start reading from endpoints */ 1096 for (i = 0; i < 2; i++) { 1097 urb = p_priv->in_urbs[i]; 1098 if (urb == NULL) 1099 continue; 1100 1101 /* make sure endpoint data toggle is synchronized 1102 with the device */ 1103 usb_clear_halt(urb->dev, urb->pipe); 1104 err = usb_submit_urb(urb, GFP_KERNEL); 1105 if (err != 0) 1106 dbg("%s - submit urb %d failed (%d)", 1107 __func__, i, err); 1108 } 1109 1110 /* Reset low level data toggle on out endpoints */ 1111 for (i = 0; i < 2; i++) { 1112 urb = p_priv->out_urbs[i]; 1113 if (urb == NULL) 1114 continue; 1115 /* usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe), 1116 usb_pipeout(urb->pipe), 0); */ 1117 } 1118 1119 /* get the terminal config for the setup message now so we don't 1120 * need to send 2 of them */ 1121 1122 device_port = port->number - port->serial->minor; 1123 if (tty) { 1124 cflag = tty->termios->c_cflag; 1125 /* Baud rate calculation takes baud rate as an integer 1126 so other rates can be generated if desired. */ 1127 baud_rate = tty_get_baud_rate(tty); 1128 /* If no match or invalid, leave as default */ 1129 if (baud_rate >= 0 1130 && d_details->calculate_baud_rate(baud_rate, d_details->baudclk, 1131 NULL, NULL, NULL, device_port) == KEYSPAN_BAUD_RATE_OK) { 1132 p_priv->baud = baud_rate; 1133 } 1134 } 1135 /* set CTS/RTS handshake etc. */ 1136 p_priv->cflag = cflag; 1137 p_priv->flow_control = (cflag & CRTSCTS)? flow_cts: flow_none; 1138 1139 keyspan_send_setup(port, 1); 1140 /* mdelay(100); */ 1141 /* keyspan_set_termios(port, NULL); */ 1142 1143 return 0; 1144 } 1145 1146 static inline void stop_urb(struct urb *urb) 1147 { 1148 if (urb && urb->status == -EINPROGRESS) 1149 usb_kill_urb(urb); 1150 } 1151 1152 static void keyspan_dtr_rts(struct usb_serial_port *port, int on) 1153 { 1154 struct keyspan_port_private *p_priv = usb_get_serial_port_data(port); 1155 1156 p_priv->rts_state = on; 1157 p_priv->dtr_state = on; 1158 keyspan_send_setup(port, 0); 1159 } 1160 1161 static void keyspan_close(struct usb_serial_port *port) 1162 { 1163 int i; 1164 struct usb_serial *serial = port->serial; 1165 struct keyspan_serial_private *s_priv; 1166 struct keyspan_port_private *p_priv; 1167 1168 dbg("%s", __func__); 1169 s_priv = usb_get_serial_data(serial); 1170 p_priv = usb_get_serial_port_data(port); 1171 1172 p_priv->rts_state = 0; 1173 p_priv->dtr_state = 0; 1174 1175 if (serial->dev) { 1176 keyspan_send_setup(port, 2); 1177 /* pilot-xfer seems to work best with this delay */ 1178 mdelay(100); 1179 /* keyspan_set_termios(port, NULL); */ 1180 } 1181 1182 /*while (p_priv->outcont_urb->status == -EINPROGRESS) { 1183 dbg("%s - urb in progress", __func__); 1184 }*/ 1185 1186 p_priv->out_flip = 0; 1187 p_priv->in_flip = 0; 1188 1189 if (serial->dev) { 1190 /* Stop reading/writing urbs */ 1191 stop_urb(p_priv->inack_urb); 1192 /* stop_urb(p_priv->outcont_urb); */ 1193 for (i = 0; i < 2; i++) { 1194 stop_urb(p_priv->in_urbs[i]); 1195 stop_urb(p_priv->out_urbs[i]); 1196 } 1197 } 1198 } 1199 1200 /* download the firmware to a pre-renumeration device */ 1201 static int keyspan_fake_startup(struct usb_serial *serial) 1202 { 1203 int response; 1204 const struct ihex_binrec *record; 1205 char *fw_name; 1206 const struct firmware *fw; 1207 1208 dbg("Keyspan startup version %04x product %04x", 1209 le16_to_cpu(serial->dev->descriptor.bcdDevice), 1210 le16_to_cpu(serial->dev->descriptor.idProduct)); 1211 1212 if ((le16_to_cpu(serial->dev->descriptor.bcdDevice) & 0x8000) 1213 != 0x8000) { 1214 dbg("Firmware already loaded. Quitting."); 1215 return 1; 1216 } 1217 1218 /* Select firmware image on the basis of idProduct */ 1219 switch (le16_to_cpu(serial->dev->descriptor.idProduct)) { 1220 case keyspan_usa28_pre_product_id: 1221 fw_name = "keyspan/usa28.fw"; 1222 break; 1223 1224 case keyspan_usa28x_pre_product_id: 1225 fw_name = "keyspan/usa28x.fw"; 1226 break; 1227 1228 case keyspan_usa28xa_pre_product_id: 1229 fw_name = "keyspan/usa28xa.fw"; 1230 break; 1231 1232 case keyspan_usa28xb_pre_product_id: 1233 fw_name = "keyspan/usa28xb.fw"; 1234 break; 1235 1236 case keyspan_usa19_pre_product_id: 1237 fw_name = "keyspan/usa19.fw"; 1238 break; 1239 1240 case keyspan_usa19qi_pre_product_id: 1241 fw_name = "keyspan/usa19qi.fw"; 1242 break; 1243 1244 case keyspan_mpr_pre_product_id: 1245 fw_name = "keyspan/mpr.fw"; 1246 break; 1247 1248 case keyspan_usa19qw_pre_product_id: 1249 fw_name = "keyspan/usa19qw.fw"; 1250 break; 1251 1252 case keyspan_usa18x_pre_product_id: 1253 fw_name = "keyspan/usa18x.fw"; 1254 break; 1255 1256 case keyspan_usa19w_pre_product_id: 1257 fw_name = "keyspan/usa19w.fw"; 1258 break; 1259 1260 case keyspan_usa49w_pre_product_id: 1261 fw_name = "keyspan/usa49w.fw"; 1262 break; 1263 1264 case keyspan_usa49wlc_pre_product_id: 1265 fw_name = "keyspan/usa49wlc.fw"; 1266 break; 1267 1268 default: 1269 dev_err(&serial->dev->dev, "Unknown product ID (%04x)\n", 1270 le16_to_cpu(serial->dev->descriptor.idProduct)); 1271 return 1; 1272 } 1273 1274 if (request_ihex_firmware(&fw, fw_name, &serial->dev->dev)) { 1275 dev_err(&serial->dev->dev, "Required keyspan firmware image (%s) unavailable.\n", fw_name); 1276 return(1); 1277 } 1278 1279 dbg("Uploading Keyspan %s firmware.", fw_name); 1280 1281 /* download the firmware image */ 1282 response = ezusb_set_reset(serial, 1); 1283 1284 record = (const struct ihex_binrec *)fw->data; 1285 1286 while (record) { 1287 response = ezusb_writememory(serial, be32_to_cpu(record->addr), 1288 (unsigned char *)record->data, 1289 be16_to_cpu(record->len), 0xa0); 1290 if (response < 0) { 1291 dev_err(&serial->dev->dev, "ezusb_writememory failed for Keyspan firmware (%d %04X %p %d)\n", 1292 response, be32_to_cpu(record->addr), 1293 record->data, be16_to_cpu(record->len)); 1294 break; 1295 } 1296 record = ihex_next_binrec(record); 1297 } 1298 release_firmware(fw); 1299 /* bring device out of reset. Renumeration will occur in a 1300 moment and the new device will bind to the real driver */ 1301 response = ezusb_set_reset(serial, 0); 1302 1303 /* we don't want this device to have a driver assigned to it. */ 1304 return 1; 1305 } 1306 1307 /* Helper functions used by keyspan_setup_urbs */ 1308 static struct usb_endpoint_descriptor const *find_ep(struct usb_serial const *serial, 1309 int endpoint) 1310 { 1311 struct usb_host_interface *iface_desc; 1312 struct usb_endpoint_descriptor *ep; 1313 int i; 1314 1315 iface_desc = serial->interface->cur_altsetting; 1316 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) { 1317 ep = &iface_desc->endpoint[i].desc; 1318 if (ep->bEndpointAddress == endpoint) 1319 return ep; 1320 } 1321 dev_warn(&serial->interface->dev, "found no endpoint descriptor for " 1322 "endpoint %x\n", endpoint); 1323 return NULL; 1324 } 1325 1326 static struct urb *keyspan_setup_urb(struct usb_serial *serial, int endpoint, 1327 int dir, void *ctx, char *buf, int len, 1328 void (*callback)(struct urb *)) 1329 { 1330 struct urb *urb; 1331 struct usb_endpoint_descriptor const *ep_desc; 1332 char const *ep_type_name; 1333 1334 if (endpoint == -1) 1335 return NULL; /* endpoint not needed */ 1336 1337 dbg("%s - alloc for endpoint %d.", __func__, endpoint); 1338 urb = usb_alloc_urb(0, GFP_KERNEL); /* No ISO */ 1339 if (urb == NULL) { 1340 dbg("%s - alloc for endpoint %d failed.", __func__, endpoint); 1341 return NULL; 1342 } 1343 1344 if (endpoint == 0) { 1345 /* control EP filled in when used */ 1346 return urb; 1347 } 1348 1349 ep_desc = find_ep(serial, endpoint); 1350 if (!ep_desc) { 1351 /* leak the urb, something's wrong and the callers don't care */ 1352 return urb; 1353 } 1354 if (usb_endpoint_xfer_int(ep_desc)) { 1355 ep_type_name = "INT"; 1356 usb_fill_int_urb(urb, serial->dev, 1357 usb_sndintpipe(serial->dev, endpoint) | dir, 1358 buf, len, callback, ctx, 1359 ep_desc->bInterval); 1360 } else if (usb_endpoint_xfer_bulk(ep_desc)) { 1361 ep_type_name = "BULK"; 1362 usb_fill_bulk_urb(urb, serial->dev, 1363 usb_sndbulkpipe(serial->dev, endpoint) | dir, 1364 buf, len, callback, ctx); 1365 } else { 1366 dev_warn(&serial->interface->dev, 1367 "unsupported endpoint type %x\n", 1368 usb_endpoint_type(ep_desc)); 1369 usb_free_urb(urb); 1370 return NULL; 1371 } 1372 1373 dbg("%s - using urb %p for %s endpoint %x", 1374 __func__, urb, ep_type_name, endpoint); 1375 return urb; 1376 } 1377 1378 static struct callbacks { 1379 void (*instat_callback)(struct urb *); 1380 void (*glocont_callback)(struct urb *); 1381 void (*indat_callback)(struct urb *); 1382 void (*outdat_callback)(struct urb *); 1383 void (*inack_callback)(struct urb *); 1384 void (*outcont_callback)(struct urb *); 1385 } keyspan_callbacks[] = { 1386 { 1387 /* msg_usa26 callbacks */ 1388 .instat_callback = usa26_instat_callback, 1389 .glocont_callback = usa26_glocont_callback, 1390 .indat_callback = usa26_indat_callback, 1391 .outdat_callback = usa2x_outdat_callback, 1392 .inack_callback = usa26_inack_callback, 1393 .outcont_callback = usa26_outcont_callback, 1394 }, { 1395 /* msg_usa28 callbacks */ 1396 .instat_callback = usa28_instat_callback, 1397 .glocont_callback = usa28_glocont_callback, 1398 .indat_callback = usa28_indat_callback, 1399 .outdat_callback = usa2x_outdat_callback, 1400 .inack_callback = usa28_inack_callback, 1401 .outcont_callback = usa28_outcont_callback, 1402 }, { 1403 /* msg_usa49 callbacks */ 1404 .instat_callback = usa49_instat_callback, 1405 .glocont_callback = usa49_glocont_callback, 1406 .indat_callback = usa49_indat_callback, 1407 .outdat_callback = usa2x_outdat_callback, 1408 .inack_callback = usa49_inack_callback, 1409 .outcont_callback = usa49_outcont_callback, 1410 }, { 1411 /* msg_usa90 callbacks */ 1412 .instat_callback = usa90_instat_callback, 1413 .glocont_callback = usa28_glocont_callback, 1414 .indat_callback = usa90_indat_callback, 1415 .outdat_callback = usa2x_outdat_callback, 1416 .inack_callback = usa28_inack_callback, 1417 .outcont_callback = usa90_outcont_callback, 1418 }, { 1419 /* msg_usa67 callbacks */ 1420 .instat_callback = usa67_instat_callback, 1421 .glocont_callback = usa67_glocont_callback, 1422 .indat_callback = usa26_indat_callback, 1423 .outdat_callback = usa2x_outdat_callback, 1424 .inack_callback = usa26_inack_callback, 1425 .outcont_callback = usa26_outcont_callback, 1426 } 1427 }; 1428 1429 /* Generic setup urbs function that uses 1430 data in device_details */ 1431 static void keyspan_setup_urbs(struct usb_serial *serial) 1432 { 1433 int i, j; 1434 struct keyspan_serial_private *s_priv; 1435 const struct keyspan_device_details *d_details; 1436 struct usb_serial_port *port; 1437 struct keyspan_port_private *p_priv; 1438 struct callbacks *cback; 1439 int endp; 1440 1441 dbg("%s", __func__); 1442 1443 s_priv = usb_get_serial_data(serial); 1444 d_details = s_priv->device_details; 1445 1446 /* Setup values for the various callback routines */ 1447 cback = &keyspan_callbacks[d_details->msg_format]; 1448 1449 /* Allocate and set up urbs for each one that is in use, 1450 starting with instat endpoints */ 1451 s_priv->instat_urb = keyspan_setup_urb 1452 (serial, d_details->instat_endpoint, USB_DIR_IN, 1453 serial, s_priv->instat_buf, INSTAT_BUFLEN, 1454 cback->instat_callback); 1455 1456 s_priv->indat_urb = keyspan_setup_urb 1457 (serial, d_details->indat_endpoint, USB_DIR_IN, 1458 serial, s_priv->indat_buf, INDAT49W_BUFLEN, 1459 usa49wg_indat_callback); 1460 1461 s_priv->glocont_urb = keyspan_setup_urb 1462 (serial, d_details->glocont_endpoint, USB_DIR_OUT, 1463 serial, s_priv->glocont_buf, GLOCONT_BUFLEN, 1464 cback->glocont_callback); 1465 1466 /* Setup endpoints for each port specific thing */ 1467 for (i = 0; i < d_details->num_ports; i++) { 1468 port = serial->port[i]; 1469 p_priv = usb_get_serial_port_data(port); 1470 1471 /* Do indat endpoints first, once for each flip */ 1472 endp = d_details->indat_endpoints[i]; 1473 for (j = 0; j <= d_details->indat_endp_flip; ++j, ++endp) { 1474 p_priv->in_urbs[j] = keyspan_setup_urb 1475 (serial, endp, USB_DIR_IN, port, 1476 p_priv->in_buffer[j], 64, 1477 cback->indat_callback); 1478 } 1479 for (; j < 2; ++j) 1480 p_priv->in_urbs[j] = NULL; 1481 1482 /* outdat endpoints also have flip */ 1483 endp = d_details->outdat_endpoints[i]; 1484 for (j = 0; j <= d_details->outdat_endp_flip; ++j, ++endp) { 1485 p_priv->out_urbs[j] = keyspan_setup_urb 1486 (serial, endp, USB_DIR_OUT, port, 1487 p_priv->out_buffer[j], 64, 1488 cback->outdat_callback); 1489 } 1490 for (; j < 2; ++j) 1491 p_priv->out_urbs[j] = NULL; 1492 1493 /* inack endpoint */ 1494 p_priv->inack_urb = keyspan_setup_urb 1495 (serial, d_details->inack_endpoints[i], USB_DIR_IN, 1496 port, p_priv->inack_buffer, 1, cback->inack_callback); 1497 1498 /* outcont endpoint */ 1499 p_priv->outcont_urb = keyspan_setup_urb 1500 (serial, d_details->outcont_endpoints[i], USB_DIR_OUT, 1501 port, p_priv->outcont_buffer, 64, 1502 cback->outcont_callback); 1503 } 1504 } 1505 1506 /* usa19 function doesn't require prescaler */ 1507 static int keyspan_usa19_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi, 1508 u8 *rate_low, u8 *prescaler, int portnum) 1509 { 1510 u32 b16, /* baud rate times 16 (actual rate used internally) */ 1511 div, /* divisor */ 1512 cnt; /* inverse of divisor (programmed into 8051) */ 1513 1514 dbg("%s - %d.", __func__, baud_rate); 1515 1516 /* prevent divide by zero... */ 1517 b16 = baud_rate * 16L; 1518 if (b16 == 0) 1519 return KEYSPAN_INVALID_BAUD_RATE; 1520 /* Any "standard" rate over 57k6 is marginal on the USA-19 1521 as we run out of divisor resolution. */ 1522 if (baud_rate > 57600) 1523 return KEYSPAN_INVALID_BAUD_RATE; 1524 1525 /* calculate the divisor and the counter (its inverse) */ 1526 div = baudclk / b16; 1527 if (div == 0) 1528 return KEYSPAN_INVALID_BAUD_RATE; 1529 else 1530 cnt = 0 - div; 1531 1532 if (div > 0xffff) 1533 return KEYSPAN_INVALID_BAUD_RATE; 1534 1535 /* return the counter values if non-null */ 1536 if (rate_low) 1537 *rate_low = (u8) (cnt & 0xff); 1538 if (rate_hi) 1539 *rate_hi = (u8) ((cnt >> 8) & 0xff); 1540 if (rate_low && rate_hi) 1541 dbg("%s - %d %02x %02x.", 1542 __func__, baud_rate, *rate_hi, *rate_low); 1543 return KEYSPAN_BAUD_RATE_OK; 1544 } 1545 1546 /* usa19hs function doesn't require prescaler */ 1547 static int keyspan_usa19hs_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi, 1548 u8 *rate_low, u8 *prescaler, int portnum) 1549 { 1550 u32 b16, /* baud rate times 16 (actual rate used internally) */ 1551 div; /* divisor */ 1552 1553 dbg("%s - %d.", __func__, baud_rate); 1554 1555 /* prevent divide by zero... */ 1556 b16 = baud_rate * 16L; 1557 if (b16 == 0) 1558 return KEYSPAN_INVALID_BAUD_RATE; 1559 1560 /* calculate the divisor */ 1561 div = baudclk / b16; 1562 if (div == 0) 1563 return KEYSPAN_INVALID_BAUD_RATE; 1564 1565 if (div > 0xffff) 1566 return KEYSPAN_INVALID_BAUD_RATE; 1567 1568 /* return the counter values if non-null */ 1569 if (rate_low) 1570 *rate_low = (u8) (div & 0xff); 1571 1572 if (rate_hi) 1573 *rate_hi = (u8) ((div >> 8) & 0xff); 1574 1575 if (rate_low && rate_hi) 1576 dbg("%s - %d %02x %02x.", 1577 __func__, baud_rate, *rate_hi, *rate_low); 1578 1579 return KEYSPAN_BAUD_RATE_OK; 1580 } 1581 1582 static int keyspan_usa19w_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi, 1583 u8 *rate_low, u8 *prescaler, int portnum) 1584 { 1585 u32 b16, /* baud rate times 16 (actual rate used internally) */ 1586 clk, /* clock with 13/8 prescaler */ 1587 div, /* divisor using 13/8 prescaler */ 1588 res, /* resulting baud rate using 13/8 prescaler */ 1589 diff, /* error using 13/8 prescaler */ 1590 smallest_diff; 1591 u8 best_prescaler; 1592 int i; 1593 1594 dbg("%s - %d.", __func__, baud_rate); 1595 1596 /* prevent divide by zero */ 1597 b16 = baud_rate * 16L; 1598 if (b16 == 0) 1599 return KEYSPAN_INVALID_BAUD_RATE; 1600 1601 /* Calculate prescaler by trying them all and looking 1602 for best fit */ 1603 1604 /* start with largest possible difference */ 1605 smallest_diff = 0xffffffff; 1606 1607 /* 0 is an invalid prescaler, used as a flag */ 1608 best_prescaler = 0; 1609 1610 for (i = 8; i <= 0xff; ++i) { 1611 clk = (baudclk * 8) / (u32) i; 1612 1613 div = clk / b16; 1614 if (div == 0) 1615 continue; 1616 1617 res = clk / div; 1618 diff = (res > b16) ? (res-b16) : (b16-res); 1619 1620 if (diff < smallest_diff) { 1621 best_prescaler = i; 1622 smallest_diff = diff; 1623 } 1624 } 1625 1626 if (best_prescaler == 0) 1627 return KEYSPAN_INVALID_BAUD_RATE; 1628 1629 clk = (baudclk * 8) / (u32) best_prescaler; 1630 div = clk / b16; 1631 1632 /* return the divisor and prescaler if non-null */ 1633 if (rate_low) 1634 *rate_low = (u8) (div & 0xff); 1635 if (rate_hi) 1636 *rate_hi = (u8) ((div >> 8) & 0xff); 1637 if (prescaler) { 1638 *prescaler = best_prescaler; 1639 /* dbg("%s - %d %d", __func__, *prescaler, div); */ 1640 } 1641 return KEYSPAN_BAUD_RATE_OK; 1642 } 1643 1644 /* USA-28 supports different maximum baud rates on each port */ 1645 static int keyspan_usa28_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi, 1646 u8 *rate_low, u8 *prescaler, int portnum) 1647 { 1648 u32 b16, /* baud rate times 16 (actual rate used internally) */ 1649 div, /* divisor */ 1650 cnt; /* inverse of divisor (programmed into 8051) */ 1651 1652 dbg("%s - %d.", __func__, baud_rate); 1653 1654 /* prevent divide by zero */ 1655 b16 = baud_rate * 16L; 1656 if (b16 == 0) 1657 return KEYSPAN_INVALID_BAUD_RATE; 1658 1659 /* calculate the divisor and the counter (its inverse) */ 1660 div = KEYSPAN_USA28_BAUDCLK / b16; 1661 if (div == 0) 1662 return KEYSPAN_INVALID_BAUD_RATE; 1663 else 1664 cnt = 0 - div; 1665 1666 /* check for out of range, based on portnum, 1667 and return result */ 1668 if (portnum == 0) { 1669 if (div > 0xffff) 1670 return KEYSPAN_INVALID_BAUD_RATE; 1671 } else { 1672 if (portnum == 1) { 1673 if (div > 0xff) 1674 return KEYSPAN_INVALID_BAUD_RATE; 1675 } else 1676 return KEYSPAN_INVALID_BAUD_RATE; 1677 } 1678 1679 /* return the counter values if not NULL 1680 (port 1 will ignore retHi) */ 1681 if (rate_low) 1682 *rate_low = (u8) (cnt & 0xff); 1683 if (rate_hi) 1684 *rate_hi = (u8) ((cnt >> 8) & 0xff); 1685 dbg("%s - %d OK.", __func__, baud_rate); 1686 return KEYSPAN_BAUD_RATE_OK; 1687 } 1688 1689 static int keyspan_usa26_send_setup(struct usb_serial *serial, 1690 struct usb_serial_port *port, 1691 int reset_port) 1692 { 1693 struct keyspan_usa26_portControlMessage msg; 1694 struct keyspan_serial_private *s_priv; 1695 struct keyspan_port_private *p_priv; 1696 const struct keyspan_device_details *d_details; 1697 int outcont_urb; 1698 struct urb *this_urb; 1699 int device_port, err; 1700 1701 dbg("%s reset=%d", __func__, reset_port); 1702 1703 s_priv = usb_get_serial_data(serial); 1704 p_priv = usb_get_serial_port_data(port); 1705 d_details = s_priv->device_details; 1706 device_port = port->number - port->serial->minor; 1707 1708 outcont_urb = d_details->outcont_endpoints[port->number]; 1709 this_urb = p_priv->outcont_urb; 1710 1711 dbg("%s - endpoint %d", __func__, usb_pipeendpoint(this_urb->pipe)); 1712 1713 /* Make sure we have an urb then send the message */ 1714 if (this_urb == NULL) { 1715 dbg("%s - oops no urb.", __func__); 1716 return -1; 1717 } 1718 1719 /* Save reset port val for resend. 1720 Don't overwrite resend for open/close condition. */ 1721 if ((reset_port + 1) > p_priv->resend_cont) 1722 p_priv->resend_cont = reset_port + 1; 1723 if (this_urb->status == -EINPROGRESS) { 1724 /* dbg("%s - already writing", __func__); */ 1725 mdelay(5); 1726 return -1; 1727 } 1728 1729 memset(&msg, 0, sizeof(struct keyspan_usa26_portControlMessage)); 1730 1731 /* Only set baud rate if it's changed */ 1732 if (p_priv->old_baud != p_priv->baud) { 1733 p_priv->old_baud = p_priv->baud; 1734 msg.setClocking = 0xff; 1735 if (d_details->calculate_baud_rate 1736 (p_priv->baud, d_details->baudclk, &msg.baudHi, 1737 &msg.baudLo, &msg.prescaler, device_port) == KEYSPAN_INVALID_BAUD_RATE) { 1738 dbg("%s - Invalid baud rate %d requested, using 9600.", 1739 __func__, p_priv->baud); 1740 msg.baudLo = 0; 1741 msg.baudHi = 125; /* Values for 9600 baud */ 1742 msg.prescaler = 10; 1743 } 1744 msg.setPrescaler = 0xff; 1745 } 1746 1747 msg.lcr = (p_priv->cflag & CSTOPB)? STOPBITS_678_2: STOPBITS_5678_1; 1748 switch (p_priv->cflag & CSIZE) { 1749 case CS5: 1750 msg.lcr |= USA_DATABITS_5; 1751 break; 1752 case CS6: 1753 msg.lcr |= USA_DATABITS_6; 1754 break; 1755 case CS7: 1756 msg.lcr |= USA_DATABITS_7; 1757 break; 1758 case CS8: 1759 msg.lcr |= USA_DATABITS_8; 1760 break; 1761 } 1762 if (p_priv->cflag & PARENB) { 1763 /* note USA_PARITY_NONE == 0 */ 1764 msg.lcr |= (p_priv->cflag & PARODD)? 1765 USA_PARITY_ODD : USA_PARITY_EVEN; 1766 } 1767 msg.setLcr = 0xff; 1768 1769 msg.ctsFlowControl = (p_priv->flow_control == flow_cts); 1770 msg.xonFlowControl = 0; 1771 msg.setFlowControl = 0xff; 1772 msg.forwardingLength = 16; 1773 msg.xonChar = 17; 1774 msg.xoffChar = 19; 1775 1776 /* Opening port */ 1777 if (reset_port == 1) { 1778 msg._txOn = 1; 1779 msg._txOff = 0; 1780 msg.txFlush = 0; 1781 msg.txBreak = 0; 1782 msg.rxOn = 1; 1783 msg.rxOff = 0; 1784 msg.rxFlush = 1; 1785 msg.rxForward = 0; 1786 msg.returnStatus = 0; 1787 msg.resetDataToggle = 0xff; 1788 } 1789 1790 /* Closing port */ 1791 else if (reset_port == 2) { 1792 msg._txOn = 0; 1793 msg._txOff = 1; 1794 msg.txFlush = 0; 1795 msg.txBreak = 0; 1796 msg.rxOn = 0; 1797 msg.rxOff = 1; 1798 msg.rxFlush = 1; 1799 msg.rxForward = 0; 1800 msg.returnStatus = 0; 1801 msg.resetDataToggle = 0; 1802 } 1803 1804 /* Sending intermediate configs */ 1805 else { 1806 msg._txOn = (!p_priv->break_on); 1807 msg._txOff = 0; 1808 msg.txFlush = 0; 1809 msg.txBreak = (p_priv->break_on); 1810 msg.rxOn = 0; 1811 msg.rxOff = 0; 1812 msg.rxFlush = 0; 1813 msg.rxForward = 0; 1814 msg.returnStatus = 0; 1815 msg.resetDataToggle = 0x0; 1816 } 1817 1818 /* Do handshaking outputs */ 1819 msg.setTxTriState_setRts = 0xff; 1820 msg.txTriState_rts = p_priv->rts_state; 1821 1822 msg.setHskoa_setDtr = 0xff; 1823 msg.hskoa_dtr = p_priv->dtr_state; 1824 1825 p_priv->resend_cont = 0; 1826 memcpy(this_urb->transfer_buffer, &msg, sizeof(msg)); 1827 1828 /* send the data out the device on control endpoint */ 1829 this_urb->transfer_buffer_length = sizeof(msg); 1830 1831 err = usb_submit_urb(this_urb, GFP_ATOMIC); 1832 if (err != 0) 1833 dbg("%s - usb_submit_urb(setup) failed (%d)", __func__, err); 1834 #if 0 1835 else { 1836 dbg("%s - usb_submit_urb(%d) OK %d bytes (end %d)", __func__ 1837 outcont_urb, this_urb->transfer_buffer_length, 1838 usb_pipeendpoint(this_urb->pipe)); 1839 } 1840 #endif 1841 1842 return 0; 1843 } 1844 1845 static int keyspan_usa28_send_setup(struct usb_serial *serial, 1846 struct usb_serial_port *port, 1847 int reset_port) 1848 { 1849 struct keyspan_usa28_portControlMessage msg; 1850 struct keyspan_serial_private *s_priv; 1851 struct keyspan_port_private *p_priv; 1852 const struct keyspan_device_details *d_details; 1853 struct urb *this_urb; 1854 int device_port, err; 1855 1856 dbg("%s", __func__); 1857 1858 s_priv = usb_get_serial_data(serial); 1859 p_priv = usb_get_serial_port_data(port); 1860 d_details = s_priv->device_details; 1861 device_port = port->number - port->serial->minor; 1862 1863 /* only do something if we have a bulk out endpoint */ 1864 this_urb = p_priv->outcont_urb; 1865 if (this_urb == NULL) { 1866 dbg("%s - oops no urb.", __func__); 1867 return -1; 1868 } 1869 1870 /* Save reset port val for resend. 1871 Don't overwrite resend for open/close condition. */ 1872 if ((reset_port + 1) > p_priv->resend_cont) 1873 p_priv->resend_cont = reset_port + 1; 1874 if (this_urb->status == -EINPROGRESS) { 1875 dbg("%s already writing", __func__); 1876 mdelay(5); 1877 return -1; 1878 } 1879 1880 memset(&msg, 0, sizeof(struct keyspan_usa28_portControlMessage)); 1881 1882 msg.setBaudRate = 1; 1883 if (d_details->calculate_baud_rate(p_priv->baud, d_details->baudclk, 1884 &msg.baudHi, &msg.baudLo, NULL, device_port) == KEYSPAN_INVALID_BAUD_RATE) { 1885 dbg("%s - Invalid baud rate requested %d.", 1886 __func__, p_priv->baud); 1887 msg.baudLo = 0xff; 1888 msg.baudHi = 0xb2; /* Values for 9600 baud */ 1889 } 1890 1891 /* If parity is enabled, we must calculate it ourselves. */ 1892 msg.parity = 0; /* XXX for now */ 1893 1894 msg.ctsFlowControl = (p_priv->flow_control == flow_cts); 1895 msg.xonFlowControl = 0; 1896 1897 /* Do handshaking outputs, DTR is inverted relative to RTS */ 1898 msg.rts = p_priv->rts_state; 1899 msg.dtr = p_priv->dtr_state; 1900 1901 msg.forwardingLength = 16; 1902 msg.forwardMs = 10; 1903 msg.breakThreshold = 45; 1904 msg.xonChar = 17; 1905 msg.xoffChar = 19; 1906 1907 /*msg.returnStatus = 1; 1908 msg.resetDataToggle = 0xff;*/ 1909 /* Opening port */ 1910 if (reset_port == 1) { 1911 msg._txOn = 1; 1912 msg._txOff = 0; 1913 msg.txFlush = 0; 1914 msg.txForceXoff = 0; 1915 msg.txBreak = 0; 1916 msg.rxOn = 1; 1917 msg.rxOff = 0; 1918 msg.rxFlush = 1; 1919 msg.rxForward = 0; 1920 msg.returnStatus = 0; 1921 msg.resetDataToggle = 0xff; 1922 } 1923 /* Closing port */ 1924 else if (reset_port == 2) { 1925 msg._txOn = 0; 1926 msg._txOff = 1; 1927 msg.txFlush = 0; 1928 msg.txForceXoff = 0; 1929 msg.txBreak = 0; 1930 msg.rxOn = 0; 1931 msg.rxOff = 1; 1932 msg.rxFlush = 1; 1933 msg.rxForward = 0; 1934 msg.returnStatus = 0; 1935 msg.resetDataToggle = 0; 1936 } 1937 /* Sending intermediate configs */ 1938 else { 1939 msg._txOn = (!p_priv->break_on); 1940 msg._txOff = 0; 1941 msg.txFlush = 0; 1942 msg.txForceXoff = 0; 1943 msg.txBreak = (p_priv->break_on); 1944 msg.rxOn = 0; 1945 msg.rxOff = 0; 1946 msg.rxFlush = 0; 1947 msg.rxForward = 0; 1948 msg.returnStatus = 0; 1949 msg.resetDataToggle = 0x0; 1950 } 1951 1952 p_priv->resend_cont = 0; 1953 memcpy(this_urb->transfer_buffer, &msg, sizeof(msg)); 1954 1955 /* send the data out the device on control endpoint */ 1956 this_urb->transfer_buffer_length = sizeof(msg); 1957 1958 err = usb_submit_urb(this_urb, GFP_ATOMIC); 1959 if (err != 0) 1960 dbg("%s - usb_submit_urb(setup) failed", __func__); 1961 #if 0 1962 else { 1963 dbg("%s - usb_submit_urb(setup) OK %d bytes", __func__, 1964 this_urb->transfer_buffer_length); 1965 } 1966 #endif 1967 1968 return 0; 1969 } 1970 1971 static int keyspan_usa49_send_setup(struct usb_serial *serial, 1972 struct usb_serial_port *port, 1973 int reset_port) 1974 { 1975 struct keyspan_usa49_portControlMessage msg; 1976 struct usb_ctrlrequest *dr = NULL; 1977 struct keyspan_serial_private *s_priv; 1978 struct keyspan_port_private *p_priv; 1979 const struct keyspan_device_details *d_details; 1980 struct urb *this_urb; 1981 int err, device_port; 1982 1983 dbg("%s", __func__); 1984 1985 s_priv = usb_get_serial_data(serial); 1986 p_priv = usb_get_serial_port_data(port); 1987 d_details = s_priv->device_details; 1988 1989 this_urb = s_priv->glocont_urb; 1990 1991 /* Work out which port within the device is being setup */ 1992 device_port = port->number - port->serial->minor; 1993 1994 /* Make sure we have an urb then send the message */ 1995 if (this_urb == NULL) { 1996 dbg("%s - oops no urb for port %d.", __func__, port->number); 1997 return -1; 1998 } 1999 2000 dbg("%s - endpoint %d port %d (%d)", 2001 __func__, usb_pipeendpoint(this_urb->pipe), 2002 port->number, device_port); 2003 2004 /* Save reset port val for resend. 2005 Don't overwrite resend for open/close condition. */ 2006 if ((reset_port + 1) > p_priv->resend_cont) 2007 p_priv->resend_cont = reset_port + 1; 2008 2009 if (this_urb->status == -EINPROGRESS) { 2010 /* dbg("%s - already writing", __func__); */ 2011 mdelay(5); 2012 return -1; 2013 } 2014 2015 memset(&msg, 0, sizeof(struct keyspan_usa49_portControlMessage)); 2016 2017 /*msg.portNumber = port->number;*/ 2018 msg.portNumber = device_port; 2019 2020 /* Only set baud rate if it's changed */ 2021 if (p_priv->old_baud != p_priv->baud) { 2022 p_priv->old_baud = p_priv->baud; 2023 msg.setClocking = 0xff; 2024 if (d_details->calculate_baud_rate 2025 (p_priv->baud, d_details->baudclk, &msg.baudHi, 2026 &msg.baudLo, &msg.prescaler, device_port) == KEYSPAN_INVALID_BAUD_RATE) { 2027 dbg("%s - Invalid baud rate %d requested, using 9600.", 2028 __func__, p_priv->baud); 2029 msg.baudLo = 0; 2030 msg.baudHi = 125; /* Values for 9600 baud */ 2031 msg.prescaler = 10; 2032 } 2033 /* msg.setPrescaler = 0xff; */ 2034 } 2035 2036 msg.lcr = (p_priv->cflag & CSTOPB)? STOPBITS_678_2: STOPBITS_5678_1; 2037 switch (p_priv->cflag & CSIZE) { 2038 case CS5: 2039 msg.lcr |= USA_DATABITS_5; 2040 break; 2041 case CS6: 2042 msg.lcr |= USA_DATABITS_6; 2043 break; 2044 case CS7: 2045 msg.lcr |= USA_DATABITS_7; 2046 break; 2047 case CS8: 2048 msg.lcr |= USA_DATABITS_8; 2049 break; 2050 } 2051 if (p_priv->cflag & PARENB) { 2052 /* note USA_PARITY_NONE == 0 */ 2053 msg.lcr |= (p_priv->cflag & PARODD)? 2054 USA_PARITY_ODD : USA_PARITY_EVEN; 2055 } 2056 msg.setLcr = 0xff; 2057 2058 msg.ctsFlowControl = (p_priv->flow_control == flow_cts); 2059 msg.xonFlowControl = 0; 2060 msg.setFlowControl = 0xff; 2061 2062 msg.forwardingLength = 16; 2063 msg.xonChar = 17; 2064 msg.xoffChar = 19; 2065 2066 /* Opening port */ 2067 if (reset_port == 1) { 2068 msg._txOn = 1; 2069 msg._txOff = 0; 2070 msg.txFlush = 0; 2071 msg.txBreak = 0; 2072 msg.rxOn = 1; 2073 msg.rxOff = 0; 2074 msg.rxFlush = 1; 2075 msg.rxForward = 0; 2076 msg.returnStatus = 0; 2077 msg.resetDataToggle = 0xff; 2078 msg.enablePort = 1; 2079 msg.disablePort = 0; 2080 } 2081 /* Closing port */ 2082 else if (reset_port == 2) { 2083 msg._txOn = 0; 2084 msg._txOff = 1; 2085 msg.txFlush = 0; 2086 msg.txBreak = 0; 2087 msg.rxOn = 0; 2088 msg.rxOff = 1; 2089 msg.rxFlush = 1; 2090 msg.rxForward = 0; 2091 msg.returnStatus = 0; 2092 msg.resetDataToggle = 0; 2093 msg.enablePort = 0; 2094 msg.disablePort = 1; 2095 } 2096 /* Sending intermediate configs */ 2097 else { 2098 msg._txOn = (!p_priv->break_on); 2099 msg._txOff = 0; 2100 msg.txFlush = 0; 2101 msg.txBreak = (p_priv->break_on); 2102 msg.rxOn = 0; 2103 msg.rxOff = 0; 2104 msg.rxFlush = 0; 2105 msg.rxForward = 0; 2106 msg.returnStatus = 0; 2107 msg.resetDataToggle = 0x0; 2108 msg.enablePort = 0; 2109 msg.disablePort = 0; 2110 } 2111 2112 /* Do handshaking outputs */ 2113 msg.setRts = 0xff; 2114 msg.rts = p_priv->rts_state; 2115 2116 msg.setDtr = 0xff; 2117 msg.dtr = p_priv->dtr_state; 2118 2119 p_priv->resend_cont = 0; 2120 2121 /* if the device is a 49wg, we send control message on usb 2122 control EP 0 */ 2123 2124 if (d_details->product_id == keyspan_usa49wg_product_id) { 2125 dr = (void *)(s_priv->ctrl_buf); 2126 dr->bRequestType = USB_TYPE_VENDOR | USB_DIR_OUT; 2127 dr->bRequest = 0xB0; /* 49wg control message */; 2128 dr->wValue = 0; 2129 dr->wIndex = 0; 2130 dr->wLength = cpu_to_le16(sizeof(msg)); 2131 2132 memcpy(s_priv->glocont_buf, &msg, sizeof(msg)); 2133 2134 usb_fill_control_urb(this_urb, serial->dev, 2135 usb_sndctrlpipe(serial->dev, 0), 2136 (unsigned char *)dr, s_priv->glocont_buf, 2137 sizeof(msg), usa49_glocont_callback, serial); 2138 2139 } else { 2140 memcpy(this_urb->transfer_buffer, &msg, sizeof(msg)); 2141 2142 /* send the data out the device on control endpoint */ 2143 this_urb->transfer_buffer_length = sizeof(msg); 2144 } 2145 err = usb_submit_urb(this_urb, GFP_ATOMIC); 2146 if (err != 0) 2147 dbg("%s - usb_submit_urb(setup) failed (%d)", __func__, err); 2148 #if 0 2149 else { 2150 dbg("%s - usb_submit_urb(%d) OK %d bytes (end %d)", __func__, 2151 outcont_urb, this_urb->transfer_buffer_length, 2152 usb_pipeendpoint(this_urb->pipe)); 2153 } 2154 #endif 2155 2156 return 0; 2157 } 2158 2159 static int keyspan_usa90_send_setup(struct usb_serial *serial, 2160 struct usb_serial_port *port, 2161 int reset_port) 2162 { 2163 struct keyspan_usa90_portControlMessage msg; 2164 struct keyspan_serial_private *s_priv; 2165 struct keyspan_port_private *p_priv; 2166 const struct keyspan_device_details *d_details; 2167 struct urb *this_urb; 2168 int err; 2169 u8 prescaler; 2170 2171 dbg("%s", __func__); 2172 2173 s_priv = usb_get_serial_data(serial); 2174 p_priv = usb_get_serial_port_data(port); 2175 d_details = s_priv->device_details; 2176 2177 /* only do something if we have a bulk out endpoint */ 2178 this_urb = p_priv->outcont_urb; 2179 if (this_urb == NULL) { 2180 dbg("%s - oops no urb.", __func__); 2181 return -1; 2182 } 2183 2184 /* Save reset port val for resend. 2185 Don't overwrite resend for open/close condition. */ 2186 if ((reset_port + 1) > p_priv->resend_cont) 2187 p_priv->resend_cont = reset_port + 1; 2188 if (this_urb->status == -EINPROGRESS) { 2189 dbg("%s already writing", __func__); 2190 mdelay(5); 2191 return -1; 2192 } 2193 2194 memset(&msg, 0, sizeof(struct keyspan_usa90_portControlMessage)); 2195 2196 /* Only set baud rate if it's changed */ 2197 if (p_priv->old_baud != p_priv->baud) { 2198 p_priv->old_baud = p_priv->baud; 2199 msg.setClocking = 0x01; 2200 if (d_details->calculate_baud_rate 2201 (p_priv->baud, d_details->baudclk, &msg.baudHi, 2202 &msg.baudLo, &prescaler, 0) == KEYSPAN_INVALID_BAUD_RATE) { 2203 dbg("%s - Invalid baud rate %d requested, using 9600.", 2204 __func__, p_priv->baud); 2205 p_priv->baud = 9600; 2206 d_details->calculate_baud_rate(p_priv->baud, d_details->baudclk, 2207 &msg.baudHi, &msg.baudLo, &prescaler, 0); 2208 } 2209 msg.setRxMode = 1; 2210 msg.setTxMode = 1; 2211 } 2212 2213 /* modes must always be correctly specified */ 2214 if (p_priv->baud > 57600) { 2215 msg.rxMode = RXMODE_DMA; 2216 msg.txMode = TXMODE_DMA; 2217 } else { 2218 msg.rxMode = RXMODE_BYHAND; 2219 msg.txMode = TXMODE_BYHAND; 2220 } 2221 2222 msg.lcr = (p_priv->cflag & CSTOPB)? STOPBITS_678_2: STOPBITS_5678_1; 2223 switch (p_priv->cflag & CSIZE) { 2224 case CS5: 2225 msg.lcr |= USA_DATABITS_5; 2226 break; 2227 case CS6: 2228 msg.lcr |= USA_DATABITS_6; 2229 break; 2230 case CS7: 2231 msg.lcr |= USA_DATABITS_7; 2232 break; 2233 case CS8: 2234 msg.lcr |= USA_DATABITS_8; 2235 break; 2236 } 2237 if (p_priv->cflag & PARENB) { 2238 /* note USA_PARITY_NONE == 0 */ 2239 msg.lcr |= (p_priv->cflag & PARODD)? 2240 USA_PARITY_ODD : USA_PARITY_EVEN; 2241 } 2242 if (p_priv->old_cflag != p_priv->cflag) { 2243 p_priv->old_cflag = p_priv->cflag; 2244 msg.setLcr = 0x01; 2245 } 2246 2247 if (p_priv->flow_control == flow_cts) 2248 msg.txFlowControl = TXFLOW_CTS; 2249 msg.setTxFlowControl = 0x01; 2250 msg.setRxFlowControl = 0x01; 2251 2252 msg.rxForwardingLength = 16; 2253 msg.rxForwardingTimeout = 16; 2254 msg.txAckSetting = 0; 2255 msg.xonChar = 17; 2256 msg.xoffChar = 19; 2257 2258 /* Opening port */ 2259 if (reset_port == 1) { 2260 msg.portEnabled = 1; 2261 msg.rxFlush = 1; 2262 msg.txBreak = (p_priv->break_on); 2263 } 2264 /* Closing port */ 2265 else if (reset_port == 2) 2266 msg.portEnabled = 0; 2267 /* Sending intermediate configs */ 2268 else { 2269 msg.portEnabled = 1; 2270 msg.txBreak = (p_priv->break_on); 2271 } 2272 2273 /* Do handshaking outputs */ 2274 msg.setRts = 0x01; 2275 msg.rts = p_priv->rts_state; 2276 2277 msg.setDtr = 0x01; 2278 msg.dtr = p_priv->dtr_state; 2279 2280 p_priv->resend_cont = 0; 2281 memcpy(this_urb->transfer_buffer, &msg, sizeof(msg)); 2282 2283 /* send the data out the device on control endpoint */ 2284 this_urb->transfer_buffer_length = sizeof(msg); 2285 2286 err = usb_submit_urb(this_urb, GFP_ATOMIC); 2287 if (err != 0) 2288 dbg("%s - usb_submit_urb(setup) failed (%d)", __func__, err); 2289 return 0; 2290 } 2291 2292 static int keyspan_usa67_send_setup(struct usb_serial *serial, 2293 struct usb_serial_port *port, 2294 int reset_port) 2295 { 2296 struct keyspan_usa67_portControlMessage msg; 2297 struct keyspan_serial_private *s_priv; 2298 struct keyspan_port_private *p_priv; 2299 const struct keyspan_device_details *d_details; 2300 struct urb *this_urb; 2301 int err, device_port; 2302 2303 dbg("%s", __func__); 2304 2305 s_priv = usb_get_serial_data(serial); 2306 p_priv = usb_get_serial_port_data(port); 2307 d_details = s_priv->device_details; 2308 2309 this_urb = s_priv->glocont_urb; 2310 2311 /* Work out which port within the device is being setup */ 2312 device_port = port->number - port->serial->minor; 2313 2314 /* Make sure we have an urb then send the message */ 2315 if (this_urb == NULL) { 2316 dbg("%s - oops no urb for port %d.", __func__, 2317 port->number); 2318 return -1; 2319 } 2320 2321 /* Save reset port val for resend. 2322 Don't overwrite resend for open/close condition. */ 2323 if ((reset_port + 1) > p_priv->resend_cont) 2324 p_priv->resend_cont = reset_port + 1; 2325 if (this_urb->status == -EINPROGRESS) { 2326 /* dbg("%s - already writing", __func__); */ 2327 mdelay(5); 2328 return -1; 2329 } 2330 2331 memset(&msg, 0, sizeof(struct keyspan_usa67_portControlMessage)); 2332 2333 msg.port = device_port; 2334 2335 /* Only set baud rate if it's changed */ 2336 if (p_priv->old_baud != p_priv->baud) { 2337 p_priv->old_baud = p_priv->baud; 2338 msg.setClocking = 0xff; 2339 if (d_details->calculate_baud_rate 2340 (p_priv->baud, d_details->baudclk, &msg.baudHi, 2341 &msg.baudLo, &msg.prescaler, device_port) == KEYSPAN_INVALID_BAUD_RATE) { 2342 dbg("%s - Invalid baud rate %d requested, using 9600.", 2343 __func__, p_priv->baud); 2344 msg.baudLo = 0; 2345 msg.baudHi = 125; /* Values for 9600 baud */ 2346 msg.prescaler = 10; 2347 } 2348 msg.setPrescaler = 0xff; 2349 } 2350 2351 msg.lcr = (p_priv->cflag & CSTOPB) ? STOPBITS_678_2 : STOPBITS_5678_1; 2352 switch (p_priv->cflag & CSIZE) { 2353 case CS5: 2354 msg.lcr |= USA_DATABITS_5; 2355 break; 2356 case CS6: 2357 msg.lcr |= USA_DATABITS_6; 2358 break; 2359 case CS7: 2360 msg.lcr |= USA_DATABITS_7; 2361 break; 2362 case CS8: 2363 msg.lcr |= USA_DATABITS_8; 2364 break; 2365 } 2366 if (p_priv->cflag & PARENB) { 2367 /* note USA_PARITY_NONE == 0 */ 2368 msg.lcr |= (p_priv->cflag & PARODD)? 2369 USA_PARITY_ODD : USA_PARITY_EVEN; 2370 } 2371 msg.setLcr = 0xff; 2372 2373 msg.ctsFlowControl = (p_priv->flow_control == flow_cts); 2374 msg.xonFlowControl = 0; 2375 msg.setFlowControl = 0xff; 2376 msg.forwardingLength = 16; 2377 msg.xonChar = 17; 2378 msg.xoffChar = 19; 2379 2380 if (reset_port == 1) { 2381 /* Opening port */ 2382 msg._txOn = 1; 2383 msg._txOff = 0; 2384 msg.txFlush = 0; 2385 msg.txBreak = 0; 2386 msg.rxOn = 1; 2387 msg.rxOff = 0; 2388 msg.rxFlush = 1; 2389 msg.rxForward = 0; 2390 msg.returnStatus = 0; 2391 msg.resetDataToggle = 0xff; 2392 } else if (reset_port == 2) { 2393 /* Closing port */ 2394 msg._txOn = 0; 2395 msg._txOff = 1; 2396 msg.txFlush = 0; 2397 msg.txBreak = 0; 2398 msg.rxOn = 0; 2399 msg.rxOff = 1; 2400 msg.rxFlush = 1; 2401 msg.rxForward = 0; 2402 msg.returnStatus = 0; 2403 msg.resetDataToggle = 0; 2404 } else { 2405 /* Sending intermediate configs */ 2406 msg._txOn = (!p_priv->break_on); 2407 msg._txOff = 0; 2408 msg.txFlush = 0; 2409 msg.txBreak = (p_priv->break_on); 2410 msg.rxOn = 0; 2411 msg.rxOff = 0; 2412 msg.rxFlush = 0; 2413 msg.rxForward = 0; 2414 msg.returnStatus = 0; 2415 msg.resetDataToggle = 0x0; 2416 } 2417 2418 /* Do handshaking outputs */ 2419 msg.setTxTriState_setRts = 0xff; 2420 msg.txTriState_rts = p_priv->rts_state; 2421 2422 msg.setHskoa_setDtr = 0xff; 2423 msg.hskoa_dtr = p_priv->dtr_state; 2424 2425 p_priv->resend_cont = 0; 2426 2427 memcpy(this_urb->transfer_buffer, &msg, sizeof(msg)); 2428 2429 /* send the data out the device on control endpoint */ 2430 this_urb->transfer_buffer_length = sizeof(msg); 2431 2432 err = usb_submit_urb(this_urb, GFP_ATOMIC); 2433 if (err != 0) 2434 dbg("%s - usb_submit_urb(setup) failed (%d)", __func__, 2435 err); 2436 return 0; 2437 } 2438 2439 static void keyspan_send_setup(struct usb_serial_port *port, int reset_port) 2440 { 2441 struct usb_serial *serial = port->serial; 2442 struct keyspan_serial_private *s_priv; 2443 const struct keyspan_device_details *d_details; 2444 2445 dbg("%s", __func__); 2446 2447 s_priv = usb_get_serial_data(serial); 2448 d_details = s_priv->device_details; 2449 2450 switch (d_details->msg_format) { 2451 case msg_usa26: 2452 keyspan_usa26_send_setup(serial, port, reset_port); 2453 break; 2454 case msg_usa28: 2455 keyspan_usa28_send_setup(serial, port, reset_port); 2456 break; 2457 case msg_usa49: 2458 keyspan_usa49_send_setup(serial, port, reset_port); 2459 break; 2460 case msg_usa90: 2461 keyspan_usa90_send_setup(serial, port, reset_port); 2462 break; 2463 case msg_usa67: 2464 keyspan_usa67_send_setup(serial, port, reset_port); 2465 break; 2466 } 2467 } 2468 2469 2470 /* Gets called by the "real" driver (ie once firmware is loaded 2471 and renumeration has taken place. */ 2472 static int keyspan_startup(struct usb_serial *serial) 2473 { 2474 int i, err; 2475 struct usb_serial_port *port; 2476 struct keyspan_serial_private *s_priv; 2477 struct keyspan_port_private *p_priv; 2478 const struct keyspan_device_details *d_details; 2479 2480 dbg("%s", __func__); 2481 2482 for (i = 0; (d_details = keyspan_devices[i]) != NULL; ++i) 2483 if (d_details->product_id == 2484 le16_to_cpu(serial->dev->descriptor.idProduct)) 2485 break; 2486 if (d_details == NULL) { 2487 dev_err(&serial->dev->dev, "%s - unknown product id %x\n", 2488 __func__, le16_to_cpu(serial->dev->descriptor.idProduct)); 2489 return 1; 2490 } 2491 2492 /* Setup private data for serial driver */ 2493 s_priv = kzalloc(sizeof(struct keyspan_serial_private), GFP_KERNEL); 2494 if (!s_priv) { 2495 dbg("%s - kmalloc for keyspan_serial_private failed.", 2496 __func__); 2497 return -ENOMEM; 2498 } 2499 2500 s_priv->device_details = d_details; 2501 usb_set_serial_data(serial, s_priv); 2502 2503 /* Now setup per port private data */ 2504 for (i = 0; i < serial->num_ports; i++) { 2505 port = serial->port[i]; 2506 p_priv = kzalloc(sizeof(struct keyspan_port_private), 2507 GFP_KERNEL); 2508 if (!p_priv) { 2509 dbg("%s - kmalloc for keyspan_port_private (%d) failed!.", __func__, i); 2510 return 1; 2511 } 2512 p_priv->device_details = d_details; 2513 usb_set_serial_port_data(port, p_priv); 2514 } 2515 2516 keyspan_setup_urbs(serial); 2517 2518 if (s_priv->instat_urb != NULL) { 2519 err = usb_submit_urb(s_priv->instat_urb, GFP_KERNEL); 2520 if (err != 0) 2521 dbg("%s - submit instat urb failed %d", __func__, 2522 err); 2523 } 2524 if (s_priv->indat_urb != NULL) { 2525 err = usb_submit_urb(s_priv->indat_urb, GFP_KERNEL); 2526 if (err != 0) 2527 dbg("%s - submit indat urb failed %d", __func__, 2528 err); 2529 } 2530 2531 return 0; 2532 } 2533 2534 static void keyspan_disconnect(struct usb_serial *serial) 2535 { 2536 int i, j; 2537 struct usb_serial_port *port; 2538 struct keyspan_serial_private *s_priv; 2539 struct keyspan_port_private *p_priv; 2540 2541 dbg("%s", __func__); 2542 2543 s_priv = usb_get_serial_data(serial); 2544 2545 /* Stop reading/writing urbs */ 2546 stop_urb(s_priv->instat_urb); 2547 stop_urb(s_priv->glocont_urb); 2548 stop_urb(s_priv->indat_urb); 2549 for (i = 0; i < serial->num_ports; ++i) { 2550 port = serial->port[i]; 2551 p_priv = usb_get_serial_port_data(port); 2552 stop_urb(p_priv->inack_urb); 2553 stop_urb(p_priv->outcont_urb); 2554 for (j = 0; j < 2; j++) { 2555 stop_urb(p_priv->in_urbs[j]); 2556 stop_urb(p_priv->out_urbs[j]); 2557 } 2558 } 2559 2560 /* Now free them */ 2561 usb_free_urb(s_priv->instat_urb); 2562 usb_free_urb(s_priv->indat_urb); 2563 usb_free_urb(s_priv->glocont_urb); 2564 for (i = 0; i < serial->num_ports; ++i) { 2565 port = serial->port[i]; 2566 p_priv = usb_get_serial_port_data(port); 2567 usb_free_urb(p_priv->inack_urb); 2568 usb_free_urb(p_priv->outcont_urb); 2569 for (j = 0; j < 2; j++) { 2570 usb_free_urb(p_priv->in_urbs[j]); 2571 usb_free_urb(p_priv->out_urbs[j]); 2572 } 2573 } 2574 } 2575 2576 static void keyspan_release(struct usb_serial *serial) 2577 { 2578 int i; 2579 struct usb_serial_port *port; 2580 struct keyspan_serial_private *s_priv; 2581 2582 dbg("%s", __func__); 2583 2584 s_priv = usb_get_serial_data(serial); 2585 2586 /* dbg("Freeing serial->private."); */ 2587 kfree(s_priv); 2588 2589 /* dbg("Freeing port->private."); */ 2590 /* Now free per port private data */ 2591 for (i = 0; i < serial->num_ports; i++) { 2592 port = serial->port[i]; 2593 kfree(usb_get_serial_port_data(port)); 2594 } 2595 } 2596 2597 MODULE_AUTHOR(DRIVER_AUTHOR); 2598 MODULE_DESCRIPTION(DRIVER_DESC); 2599 MODULE_LICENSE("GPL"); 2600 2601 MODULE_FIRMWARE("keyspan/usa28.fw"); 2602 MODULE_FIRMWARE("keyspan/usa28x.fw"); 2603 MODULE_FIRMWARE("keyspan/usa28xa.fw"); 2604 MODULE_FIRMWARE("keyspan/usa28xb.fw"); 2605 MODULE_FIRMWARE("keyspan/usa19.fw"); 2606 MODULE_FIRMWARE("keyspan/usa19qi.fw"); 2607 MODULE_FIRMWARE("keyspan/mpr.fw"); 2608 MODULE_FIRMWARE("keyspan/usa19qw.fw"); 2609 MODULE_FIRMWARE("keyspan/usa18x.fw"); 2610 MODULE_FIRMWARE("keyspan/usa19w.fw"); 2611 MODULE_FIRMWARE("keyspan/usa49w.fw"); 2612 MODULE_FIRMWARE("keyspan/usa49wlc.fw"); 2613 2614 module_param(debug, bool, S_IRUGO | S_IWUSR); 2615 MODULE_PARM_DESC(debug, "Debug enabled or not"); 2616 2617