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