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