1 /* 2 * USB Cypress M8 driver 3 * 4 * Copyright (C) 2004 5 * Lonnie Mendez (dignome@gmail.com) 6 * Copyright (C) 2003,2004 7 * Neil Whelchel (koyama@firstlight.net) 8 * 9 * This program is free software; you can redistribute it and/or modify 10 * it under the terms of the GNU General Public License as published by 11 * the Free Software Foundation; either version 2 of the License, or 12 * (at your option) any later version. 13 * 14 * See Documentation/usb/usb-serial.txt for more information on using this driver 15 * 16 * See http://geocities.com/i0xox0i for information on this driver and the 17 * earthmate usb device. 18 * 19 * Lonnie Mendez <dignome@gmail.com> 20 * 4-29-2005 21 * Fixed problem where setting or retreiving the serial config would fail with 22 * EPIPE. Removed CRTS toggling so the driver behaves more like other usbserial 23 * adapters. Issued new interval of 1ms instead of the default 10ms. As a 24 * result, transfer speed has been substantially increased. From avg. 850bps to 25 * avg. 3300bps. initial termios has also been modified. Cleaned up code and 26 * formatting issues so it is more readable. Replaced the C++ style comments. 27 * 28 * Lonnie Mendez <dignome@gmail.com> 29 * 12-15-2004 30 * Incorporated write buffering from pl2303 driver. Fixed bug with line 31 * handling so both lines are raised in cypress_open. (was dropping rts) 32 * Various code cleanups made as well along with other misc bug fixes. 33 * 34 * Lonnie Mendez <dignome@gmail.com> 35 * 04-10-2004 36 * Driver modified to support dynamic line settings. Various improvments 37 * and features. 38 * 39 * Neil Whelchel 40 * 10-2003 41 * Driver first released. 42 * 43 */ 44 45 /* Thanks to Neil Whelchel for writing the first cypress m8 implementation for linux. */ 46 /* Thanks to cypress for providing references for the hid reports. */ 47 /* Thanks to Jiang Zhang for providing links and for general help. */ 48 /* Code originates and was built up from ftdi_sio, belkin, pl2303 and others. */ 49 50 51 #include <linux/kernel.h> 52 #include <linux/errno.h> 53 #include <linux/init.h> 54 #include <linux/slab.h> 55 #include <linux/tty.h> 56 #include <linux/tty_driver.h> 57 #include <linux/tty_flip.h> 58 #include <linux/module.h> 59 #include <linux/moduleparam.h> 60 #include <linux/spinlock.h> 61 #include <linux/usb.h> 62 #include <linux/usb/serial.h> 63 #include <linux/serial.h> 64 #include <linux/delay.h> 65 #include <asm/uaccess.h> 66 67 #include "cypress_m8.h" 68 69 70 #ifdef CONFIG_USB_SERIAL_DEBUG 71 static int debug = 1; 72 #else 73 static int debug; 74 #endif 75 static int stats; 76 static int interval; 77 78 /* 79 * Version Information 80 */ 81 #define DRIVER_VERSION "v1.09" 82 #define DRIVER_AUTHOR "Lonnie Mendez <dignome@gmail.com>, Neil Whelchel <koyama@firstlight.net>" 83 #define DRIVER_DESC "Cypress USB to Serial Driver" 84 85 /* write buffer size defines */ 86 #define CYPRESS_BUF_SIZE 1024 87 #define CYPRESS_CLOSING_WAIT (30*HZ) 88 89 static struct usb_device_id id_table_earthmate [] = { 90 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB) }, 91 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB_LT20) }, 92 { } /* Terminating entry */ 93 }; 94 95 static struct usb_device_id id_table_cyphidcomrs232 [] = { 96 { USB_DEVICE(VENDOR_ID_CYPRESS, PRODUCT_ID_CYPHIDCOM) }, 97 { USB_DEVICE(VENDOR_ID_POWERCOM, PRODUCT_ID_UPS) }, 98 { } /* Terminating entry */ 99 }; 100 101 static struct usb_device_id id_table_nokiaca42v2 [] = { 102 { USB_DEVICE(VENDOR_ID_DAZZLE, PRODUCT_ID_CA42) }, 103 { } /* Terminating entry */ 104 }; 105 106 static struct usb_device_id id_table_combined [] = { 107 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB) }, 108 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB_LT20) }, 109 { USB_DEVICE(VENDOR_ID_CYPRESS, PRODUCT_ID_CYPHIDCOM) }, 110 { USB_DEVICE(VENDOR_ID_POWERCOM, PRODUCT_ID_UPS) }, 111 { USB_DEVICE(VENDOR_ID_DAZZLE, PRODUCT_ID_CA42) }, 112 { } /* Terminating entry */ 113 }; 114 115 MODULE_DEVICE_TABLE (usb, id_table_combined); 116 117 static struct usb_driver cypress_driver = { 118 .name = "cypress", 119 .probe = usb_serial_probe, 120 .disconnect = usb_serial_disconnect, 121 .id_table = id_table_combined, 122 .no_dynamic_id = 1, 123 }; 124 125 struct cypress_private { 126 spinlock_t lock; /* private lock */ 127 int chiptype; /* identifier of device, for quirks/etc */ 128 int bytes_in; /* used for statistics */ 129 int bytes_out; /* used for statistics */ 130 int cmd_count; /* used for statistics */ 131 int cmd_ctrl; /* always set this to 1 before issuing a command */ 132 struct cypress_buf *buf; /* write buffer */ 133 int write_urb_in_use; /* write urb in use indicator */ 134 int write_urb_interval; /* interval to use for write urb */ 135 int read_urb_interval; /* interval to use for read urb */ 136 int comm_is_ok; /* true if communication is (still) ok */ 137 int termios_initialized; 138 __u8 line_control; /* holds dtr / rts value */ 139 __u8 current_status; /* received from last read - info on dsr,cts,cd,ri,etc */ 140 __u8 current_config; /* stores the current configuration byte */ 141 __u8 rx_flags; /* throttling - used from whiteheat/ftdi_sio */ 142 int baud_rate; /* stores current baud rate in integer form */ 143 int cbr_mask; /* stores current baud rate in masked form */ 144 int isthrottled; /* if throttled, discard reads */ 145 wait_queue_head_t delta_msr_wait; /* used for TIOCMIWAIT */ 146 char prev_status, diff_status; /* used for TIOCMIWAIT */ 147 /* we pass a pointer to this as the arguement sent to cypress_set_termios old_termios */ 148 struct ktermios tmp_termios; /* stores the old termios settings */ 149 }; 150 151 /* write buffer structure */ 152 struct cypress_buf { 153 unsigned int buf_size; 154 char *buf_buf; 155 char *buf_get; 156 char *buf_put; 157 }; 158 159 /* function prototypes for the Cypress USB to serial device */ 160 static int cypress_earthmate_startup (struct usb_serial *serial); 161 static int cypress_hidcom_startup (struct usb_serial *serial); 162 static int cypress_ca42v2_startup (struct usb_serial *serial); 163 static void cypress_shutdown (struct usb_serial *serial); 164 static int cypress_open (struct usb_serial_port *port, struct file *filp); 165 static void cypress_close (struct usb_serial_port *port, struct file *filp); 166 static int cypress_write (struct usb_serial_port *port, const unsigned char *buf, int count); 167 static void cypress_send (struct usb_serial_port *port); 168 static int cypress_write_room (struct usb_serial_port *port); 169 static int cypress_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg); 170 static void cypress_set_termios (struct usb_serial_port *port, struct ktermios * old); 171 static int cypress_tiocmget (struct usb_serial_port *port, struct file *file); 172 static int cypress_tiocmset (struct usb_serial_port *port, struct file *file, unsigned int set, unsigned int clear); 173 static int cypress_chars_in_buffer (struct usb_serial_port *port); 174 static void cypress_throttle (struct usb_serial_port *port); 175 static void cypress_unthrottle (struct usb_serial_port *port); 176 static void cypress_set_dead (struct usb_serial_port *port); 177 static void cypress_read_int_callback (struct urb *urb); 178 static void cypress_write_int_callback (struct urb *urb); 179 /* baud helper functions */ 180 static int mask_to_rate (unsigned mask); 181 static unsigned rate_to_mask (int rate); 182 /* write buffer functions */ 183 static struct cypress_buf *cypress_buf_alloc(unsigned int size); 184 static void cypress_buf_free(struct cypress_buf *cb); 185 static void cypress_buf_clear(struct cypress_buf *cb); 186 static unsigned int cypress_buf_data_avail(struct cypress_buf *cb); 187 static unsigned int cypress_buf_space_avail(struct cypress_buf *cb); 188 static unsigned int cypress_buf_put(struct cypress_buf *cb, const char *buf, unsigned int count); 189 static unsigned int cypress_buf_get(struct cypress_buf *cb, char *buf, unsigned int count); 190 191 192 static struct usb_serial_driver cypress_earthmate_device = { 193 .driver = { 194 .owner = THIS_MODULE, 195 .name = "earthmate", 196 }, 197 .description = "DeLorme Earthmate USB", 198 .usb_driver = &cypress_driver, 199 .id_table = id_table_earthmate, 200 .num_interrupt_in = 1, 201 .num_interrupt_out = 1, 202 .num_bulk_in = NUM_DONT_CARE, 203 .num_bulk_out = NUM_DONT_CARE, 204 .num_ports = 1, 205 .attach = cypress_earthmate_startup, 206 .shutdown = cypress_shutdown, 207 .open = cypress_open, 208 .close = cypress_close, 209 .write = cypress_write, 210 .write_room = cypress_write_room, 211 .ioctl = cypress_ioctl, 212 .set_termios = cypress_set_termios, 213 .tiocmget = cypress_tiocmget, 214 .tiocmset = cypress_tiocmset, 215 .chars_in_buffer = cypress_chars_in_buffer, 216 .throttle = cypress_throttle, 217 .unthrottle = cypress_unthrottle, 218 .read_int_callback = cypress_read_int_callback, 219 .write_int_callback = cypress_write_int_callback, 220 }; 221 222 static struct usb_serial_driver cypress_hidcom_device = { 223 .driver = { 224 .owner = THIS_MODULE, 225 .name = "cyphidcom", 226 }, 227 .description = "HID->COM RS232 Adapter", 228 .usb_driver = &cypress_driver, 229 .id_table = id_table_cyphidcomrs232, 230 .num_interrupt_in = 1, 231 .num_interrupt_out = 1, 232 .num_bulk_in = NUM_DONT_CARE, 233 .num_bulk_out = NUM_DONT_CARE, 234 .num_ports = 1, 235 .attach = cypress_hidcom_startup, 236 .shutdown = cypress_shutdown, 237 .open = cypress_open, 238 .close = cypress_close, 239 .write = cypress_write, 240 .write_room = cypress_write_room, 241 .ioctl = cypress_ioctl, 242 .set_termios = cypress_set_termios, 243 .tiocmget = cypress_tiocmget, 244 .tiocmset = cypress_tiocmset, 245 .chars_in_buffer = cypress_chars_in_buffer, 246 .throttle = cypress_throttle, 247 .unthrottle = cypress_unthrottle, 248 .read_int_callback = cypress_read_int_callback, 249 .write_int_callback = cypress_write_int_callback, 250 }; 251 252 static struct usb_serial_driver cypress_ca42v2_device = { 253 .driver = { 254 .owner = THIS_MODULE, 255 .name = "nokiaca42v2", 256 }, 257 .description = "Nokia CA-42 V2 Adapter", 258 .usb_driver = &cypress_driver, 259 .id_table = id_table_nokiaca42v2, 260 .num_interrupt_in = 1, 261 .num_interrupt_out = 1, 262 .num_bulk_in = NUM_DONT_CARE, 263 .num_bulk_out = NUM_DONT_CARE, 264 .num_ports = 1, 265 .attach = cypress_ca42v2_startup, 266 .shutdown = cypress_shutdown, 267 .open = cypress_open, 268 .close = cypress_close, 269 .write = cypress_write, 270 .write_room = cypress_write_room, 271 .ioctl = cypress_ioctl, 272 .set_termios = cypress_set_termios, 273 .tiocmget = cypress_tiocmget, 274 .tiocmset = cypress_tiocmset, 275 .chars_in_buffer = cypress_chars_in_buffer, 276 .throttle = cypress_throttle, 277 .unthrottle = cypress_unthrottle, 278 .read_int_callback = cypress_read_int_callback, 279 .write_int_callback = cypress_write_int_callback, 280 }; 281 282 /***************************************************************************** 283 * Cypress serial helper functions 284 *****************************************************************************/ 285 286 287 /* This function can either set or retrieve the current serial line settings */ 288 static int cypress_serial_control (struct usb_serial_port *port, unsigned baud_mask, int data_bits, int stop_bits, 289 int parity_enable, int parity_type, int reset, int cypress_request_type) 290 { 291 int new_baudrate = 0, retval = 0, tries = 0; 292 struct cypress_private *priv; 293 __u8 feature_buffer[8]; 294 unsigned long flags; 295 296 dbg("%s", __FUNCTION__); 297 298 priv = usb_get_serial_port_data(port); 299 300 if (!priv->comm_is_ok) 301 return -ENODEV; 302 303 switch(cypress_request_type) { 304 case CYPRESS_SET_CONFIG: 305 306 /* 307 * The general purpose firmware for the Cypress M8 allows for a maximum speed 308 * of 57600bps (I have no idea whether DeLorme chose to use the general purpose 309 * firmware or not), if you need to modify this speed setting for your own 310 * project please add your own chiptype and modify the code likewise. The 311 * Cypress HID->COM device will work successfully up to 115200bps (but the 312 * actual throughput is around 3kBps). 313 */ 314 if (baud_mask != priv->cbr_mask) { 315 dbg("%s - baud rate is changing", __FUNCTION__); 316 if ( priv->chiptype == CT_EARTHMATE ) { 317 /* 300 and 600 baud rates are supported under the generic firmware, 318 * but are not used with NMEA and SiRF protocols */ 319 320 if ( (baud_mask == B300) || (baud_mask == B600) ) { 321 err("%s - failed setting baud rate, unsupported speed", 322 __FUNCTION__); 323 new_baudrate = priv->baud_rate; 324 } else if ( (new_baudrate = mask_to_rate(baud_mask)) == -1) { 325 err("%s - failed setting baud rate, unsupported speed", 326 __FUNCTION__); 327 new_baudrate = priv->baud_rate; 328 } 329 } else if (priv->chiptype == CT_CYPHIDCOM) { 330 if ( (new_baudrate = mask_to_rate(baud_mask)) == -1) { 331 err("%s - failed setting baud rate, unsupported speed", 332 __FUNCTION__); 333 new_baudrate = priv->baud_rate; 334 } 335 } else if (priv->chiptype == CT_CA42V2) { 336 if ( (new_baudrate = mask_to_rate(baud_mask)) == -1) { 337 err("%s - failed setting baud rate, unsupported speed", 338 __FUNCTION__); 339 new_baudrate = priv->baud_rate; 340 } 341 } else if (priv->chiptype == CT_GENERIC) { 342 if ( (new_baudrate = mask_to_rate(baud_mask)) == -1) { 343 err("%s - failed setting baud rate, unsupported speed", 344 __FUNCTION__); 345 new_baudrate = priv->baud_rate; 346 } 347 } else { 348 info("%s - please define your chiptype", __FUNCTION__); 349 new_baudrate = priv->baud_rate; 350 } 351 } else { /* baud rate not changing, keep the old */ 352 new_baudrate = priv->baud_rate; 353 } 354 dbg("%s - baud rate is being sent as %d", __FUNCTION__, new_baudrate); 355 356 memset(feature_buffer, 0, 8); 357 /* fill the feature_buffer with new configuration */ 358 *((u_int32_t *)feature_buffer) = new_baudrate; 359 360 feature_buffer[4] |= data_bits; /* assign data bits in 2 bit space ( max 3 ) */ 361 /* 1 bit gap */ 362 feature_buffer[4] |= (stop_bits << 3); /* assign stop bits in 1 bit space */ 363 feature_buffer[4] |= (parity_enable << 4); /* assign parity flag in 1 bit space */ 364 feature_buffer[4] |= (parity_type << 5); /* assign parity type in 1 bit space */ 365 /* 1 bit gap */ 366 feature_buffer[4] |= (reset << 7); /* assign reset at end of byte, 1 bit space */ 367 368 dbg("%s - device is being sent this feature report:", __FUNCTION__); 369 dbg("%s - %02X - %02X - %02X - %02X - %02X", __FUNCTION__, feature_buffer[0], feature_buffer[1], 370 feature_buffer[2], feature_buffer[3], feature_buffer[4]); 371 372 do { 373 retval = usb_control_msg (port->serial->dev, usb_sndctrlpipe(port->serial->dev, 0), 374 HID_REQ_SET_REPORT, USB_DIR_OUT | USB_RECIP_INTERFACE | USB_TYPE_CLASS, 375 0x0300, 0, feature_buffer, 8, 500); 376 377 if (tries++ >= 3) 378 break; 379 380 } while (retval != 8 && retval != -ENODEV); 381 382 if (retval != 8) { 383 err("%s - failed sending serial line settings - %d", __FUNCTION__, retval); 384 cypress_set_dead(port); 385 } else { 386 spin_lock_irqsave(&priv->lock, flags); 387 priv->baud_rate = new_baudrate; 388 priv->cbr_mask = baud_mask; 389 priv->current_config = feature_buffer[4]; 390 spin_unlock_irqrestore(&priv->lock, flags); 391 } 392 break; 393 case CYPRESS_GET_CONFIG: 394 dbg("%s - retreiving serial line settings", __FUNCTION__); 395 /* set initial values in feature buffer */ 396 memset(feature_buffer, 0, 8); 397 398 do { 399 retval = usb_control_msg (port->serial->dev, usb_rcvctrlpipe(port->serial->dev, 0), 400 HID_REQ_GET_REPORT, USB_DIR_IN | USB_RECIP_INTERFACE | USB_TYPE_CLASS, 401 0x0300, 0, feature_buffer, 8, 500); 402 403 if (tries++ >= 3) 404 break; 405 406 } while (retval != 5 && retval != -ENODEV); 407 408 if (retval != 5) { 409 err("%s - failed to retrieve serial line settings - %d", __FUNCTION__, retval); 410 cypress_set_dead(port); 411 return retval; 412 } else { 413 spin_lock_irqsave(&priv->lock, flags); 414 415 /* store the config in one byte, and later use bit masks to check values */ 416 priv->current_config = feature_buffer[4]; 417 priv->baud_rate = *((u_int32_t *)feature_buffer); 418 419 if ( (priv->cbr_mask = rate_to_mask(priv->baud_rate)) == 0x40) 420 dbg("%s - failed setting the baud mask (not defined)", __FUNCTION__); 421 spin_unlock_irqrestore(&priv->lock, flags); 422 } 423 } 424 spin_lock_irqsave(&priv->lock, flags); 425 ++priv->cmd_count; 426 spin_unlock_irqrestore(&priv->lock, flags); 427 428 return retval; 429 } /* cypress_serial_control */ 430 431 432 static void cypress_set_dead(struct usb_serial_port *port) 433 { 434 struct cypress_private *priv = usb_get_serial_port_data(port); 435 unsigned long flags; 436 437 spin_lock_irqsave(&priv->lock, flags); 438 if (!priv->comm_is_ok) { 439 spin_unlock_irqrestore(&priv->lock, flags); 440 return; 441 } 442 priv->comm_is_ok = 0; 443 spin_unlock_irqrestore(&priv->lock, flags); 444 445 err("cypress_m8 suspending failing port %d - interval might be too short", 446 port->number); 447 } 448 449 450 /* given a baud mask, it will return integer baud on success */ 451 static int mask_to_rate (unsigned mask) 452 { 453 int rate; 454 455 switch (mask) { 456 case B0: rate = 0; break; 457 case B300: rate = 300; break; 458 case B600: rate = 600; break; 459 case B1200: rate = 1200; break; 460 case B2400: rate = 2400; break; 461 case B4800: rate = 4800; break; 462 case B9600: rate = 9600; break; 463 case B19200: rate = 19200; break; 464 case B38400: rate = 38400; break; 465 case B57600: rate = 57600; break; 466 case B115200: rate = 115200; break; 467 default: rate = -1; 468 } 469 470 return rate; 471 } 472 473 474 static unsigned rate_to_mask (int rate) 475 { 476 unsigned mask; 477 478 switch (rate) { 479 case 0: mask = B0; break; 480 case 300: mask = B300; break; 481 case 600: mask = B600; break; 482 case 1200: mask = B1200; break; 483 case 2400: mask = B2400; break; 484 case 4800: mask = B4800; break; 485 case 9600: mask = B9600; break; 486 case 19200: mask = B19200; break; 487 case 38400: mask = B38400; break; 488 case 57600: mask = B57600; break; 489 case 115200: mask = B115200; break; 490 default: mask = 0x40; 491 } 492 493 return mask; 494 } 495 /***************************************************************************** 496 * Cypress serial driver functions 497 *****************************************************************************/ 498 499 500 static int generic_startup (struct usb_serial *serial) 501 { 502 struct cypress_private *priv; 503 struct usb_serial_port *port = serial->port[0]; 504 505 dbg("%s - port %d", __FUNCTION__, port->number); 506 507 priv = kzalloc(sizeof (struct cypress_private), GFP_KERNEL); 508 if (!priv) 509 return -ENOMEM; 510 511 priv->comm_is_ok = !0; 512 spin_lock_init(&priv->lock); 513 priv->buf = cypress_buf_alloc(CYPRESS_BUF_SIZE); 514 if (priv->buf == NULL) { 515 kfree(priv); 516 return -ENOMEM; 517 } 518 init_waitqueue_head(&priv->delta_msr_wait); 519 520 usb_reset_configuration (serial->dev); 521 522 priv->cmd_ctrl = 0; 523 priv->line_control = 0; 524 priv->termios_initialized = 0; 525 priv->rx_flags = 0; 526 priv->cbr_mask = B300; 527 if (interval > 0) { 528 priv->write_urb_interval = interval; 529 priv->read_urb_interval = interval; 530 dbg("%s - port %d read & write intervals forced to %d", 531 __FUNCTION__,port->number,interval); 532 } else { 533 priv->write_urb_interval = port->interrupt_out_urb->interval; 534 priv->read_urb_interval = port->interrupt_in_urb->interval; 535 dbg("%s - port %d intervals: read=%d write=%d", 536 __FUNCTION__,port->number, 537 priv->read_urb_interval,priv->write_urb_interval); 538 } 539 usb_set_serial_port_data(port, priv); 540 541 return 0; 542 } 543 544 545 static int cypress_earthmate_startup (struct usb_serial *serial) 546 { 547 struct cypress_private *priv; 548 549 dbg("%s", __FUNCTION__); 550 551 if (generic_startup(serial)) { 552 dbg("%s - Failed setting up port %d", __FUNCTION__, 553 serial->port[0]->number); 554 return 1; 555 } 556 557 priv = usb_get_serial_port_data(serial->port[0]); 558 priv->chiptype = CT_EARTHMATE; 559 560 return 0; 561 } /* cypress_earthmate_startup */ 562 563 564 static int cypress_hidcom_startup (struct usb_serial *serial) 565 { 566 struct cypress_private *priv; 567 568 dbg("%s", __FUNCTION__); 569 570 if (generic_startup(serial)) { 571 dbg("%s - Failed setting up port %d", __FUNCTION__, 572 serial->port[0]->number); 573 return 1; 574 } 575 576 priv = usb_get_serial_port_data(serial->port[0]); 577 priv->chiptype = CT_CYPHIDCOM; 578 579 return 0; 580 } /* cypress_hidcom_startup */ 581 582 583 static int cypress_ca42v2_startup (struct usb_serial *serial) 584 { 585 struct cypress_private *priv; 586 587 dbg("%s", __FUNCTION__); 588 589 if (generic_startup(serial)) { 590 dbg("%s - Failed setting up port %d", __FUNCTION__, 591 serial->port[0]->number); 592 return 1; 593 } 594 595 priv = usb_get_serial_port_data(serial->port[0]); 596 priv->chiptype = CT_CA42V2; 597 598 return 0; 599 } /* cypress_ca42v2_startup */ 600 601 602 static void cypress_shutdown (struct usb_serial *serial) 603 { 604 struct cypress_private *priv; 605 606 dbg ("%s - port %d", __FUNCTION__, serial->port[0]->number); 607 608 /* all open ports are closed at this point */ 609 610 priv = usb_get_serial_port_data(serial->port[0]); 611 612 if (priv) { 613 cypress_buf_free(priv->buf); 614 kfree(priv); 615 usb_set_serial_port_data(serial->port[0], NULL); 616 } 617 } 618 619 620 static int cypress_open (struct usb_serial_port *port, struct file *filp) 621 { 622 struct cypress_private *priv = usb_get_serial_port_data(port); 623 struct usb_serial *serial = port->serial; 624 unsigned long flags; 625 int result = 0; 626 627 dbg("%s - port %d", __FUNCTION__, port->number); 628 629 if (!priv->comm_is_ok) 630 return -EIO; 631 632 /* clear halts before open */ 633 usb_clear_halt(serial->dev, 0x81); 634 usb_clear_halt(serial->dev, 0x02); 635 636 spin_lock_irqsave(&priv->lock, flags); 637 /* reset read/write statistics */ 638 priv->bytes_in = 0; 639 priv->bytes_out = 0; 640 priv->cmd_count = 0; 641 priv->rx_flags = 0; 642 spin_unlock_irqrestore(&priv->lock, flags); 643 644 /* setting to zero could cause data loss */ 645 port->tty->low_latency = 1; 646 647 /* raise both lines and set termios */ 648 spin_lock_irqsave(&priv->lock, flags); 649 priv->line_control = CONTROL_DTR | CONTROL_RTS; 650 priv->cmd_ctrl = 1; 651 spin_unlock_irqrestore(&priv->lock, flags); 652 result = cypress_write(port, NULL, 0); 653 654 if (result) { 655 dev_err(&port->dev, "%s - failed setting the control lines - error %d\n", __FUNCTION__, result); 656 return result; 657 } else 658 dbg("%s - success setting the control lines", __FUNCTION__); 659 660 cypress_set_termios(port, &priv->tmp_termios); 661 662 /* setup the port and start reading from the device */ 663 if(!port->interrupt_in_urb){ 664 err("%s - interrupt_in_urb is empty!", __FUNCTION__); 665 return(-1); 666 } 667 668 usb_fill_int_urb(port->interrupt_in_urb, serial->dev, 669 usb_rcvintpipe(serial->dev, port->interrupt_in_endpointAddress), 670 port->interrupt_in_urb->transfer_buffer, port->interrupt_in_urb->transfer_buffer_length, 671 cypress_read_int_callback, port, priv->read_urb_interval); 672 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL); 673 674 if (result){ 675 dev_err(&port->dev, "%s - failed submitting read urb, error %d\n", __FUNCTION__, result); 676 cypress_set_dead(port); 677 } 678 679 return result; 680 } /* cypress_open */ 681 682 683 static void cypress_close(struct usb_serial_port *port, struct file * filp) 684 { 685 struct cypress_private *priv = usb_get_serial_port_data(port); 686 unsigned int c_cflag; 687 int bps; 688 long timeout; 689 wait_queue_t wait; 690 691 dbg("%s - port %d", __FUNCTION__, port->number); 692 693 /* wait for data to drain from buffer */ 694 spin_lock_irq(&priv->lock); 695 timeout = CYPRESS_CLOSING_WAIT; 696 init_waitqueue_entry(&wait, current); 697 add_wait_queue(&port->tty->write_wait, &wait); 698 for (;;) { 699 set_current_state(TASK_INTERRUPTIBLE); 700 if (cypress_buf_data_avail(priv->buf) == 0 701 || timeout == 0 || signal_pending(current) 702 /* without mutex, allowed due to harmless failure mode */ 703 || port->serial->disconnected) 704 break; 705 spin_unlock_irq(&priv->lock); 706 timeout = schedule_timeout(timeout); 707 spin_lock_irq(&priv->lock); 708 } 709 set_current_state(TASK_RUNNING); 710 remove_wait_queue(&port->tty->write_wait, &wait); 711 /* clear out any remaining data in the buffer */ 712 cypress_buf_clear(priv->buf); 713 spin_unlock_irq(&priv->lock); 714 715 /* writing is potentially harmful, lock must be taken */ 716 mutex_lock(&port->serial->disc_mutex); 717 if (port->serial->disconnected) { 718 mutex_unlock(&port->serial->disc_mutex); 719 return; 720 } 721 /* wait for characters to drain from device */ 722 bps = tty_get_baud_rate(port->tty); 723 if (bps > 1200) 724 timeout = max((HZ*2560)/bps,HZ/10); 725 else 726 timeout = 2*HZ; 727 schedule_timeout_interruptible(timeout); 728 729 dbg("%s - stopping urbs", __FUNCTION__); 730 usb_kill_urb (port->interrupt_in_urb); 731 usb_kill_urb (port->interrupt_out_urb); 732 733 if (port->tty) { 734 c_cflag = port->tty->termios->c_cflag; 735 if (c_cflag & HUPCL) { 736 /* drop dtr and rts */ 737 priv = usb_get_serial_port_data(port); 738 spin_lock_irq(&priv->lock); 739 priv->line_control = 0; 740 priv->cmd_ctrl = 1; 741 spin_unlock_irq(&priv->lock); 742 cypress_write(port, NULL, 0); 743 } 744 } 745 746 if (stats) 747 dev_info (&port->dev, "Statistics: %d Bytes In | %d Bytes Out | %d Commands Issued\n", 748 priv->bytes_in, priv->bytes_out, priv->cmd_count); 749 mutex_unlock(&port->serial->disc_mutex); 750 } /* cypress_close */ 751 752 753 static int cypress_write(struct usb_serial_port *port, const unsigned char *buf, int count) 754 { 755 struct cypress_private *priv = usb_get_serial_port_data(port); 756 unsigned long flags; 757 758 dbg("%s - port %d, %d bytes", __FUNCTION__, port->number, count); 759 760 /* line control commands, which need to be executed immediately, 761 are not put into the buffer for obvious reasons. 762 */ 763 if (priv->cmd_ctrl) { 764 count = 0; 765 goto finish; 766 } 767 768 if (!count) 769 return count; 770 771 spin_lock_irqsave(&priv->lock, flags); 772 count = cypress_buf_put(priv->buf, buf, count); 773 spin_unlock_irqrestore(&priv->lock, flags); 774 775 finish: 776 cypress_send(port); 777 778 return count; 779 } /* cypress_write */ 780 781 782 static void cypress_send(struct usb_serial_port *port) 783 { 784 int count = 0, result, offset, actual_size; 785 struct cypress_private *priv = usb_get_serial_port_data(port); 786 unsigned long flags; 787 788 if (!priv->comm_is_ok) 789 return; 790 791 dbg("%s - port %d", __FUNCTION__, port->number); 792 dbg("%s - interrupt out size is %d", __FUNCTION__, port->interrupt_out_size); 793 794 spin_lock_irqsave(&priv->lock, flags); 795 if (priv->write_urb_in_use) { 796 dbg("%s - can't write, urb in use", __FUNCTION__); 797 spin_unlock_irqrestore(&priv->lock, flags); 798 return; 799 } 800 spin_unlock_irqrestore(&priv->lock, flags); 801 802 /* clear buffer */ 803 memset(port->interrupt_out_urb->transfer_buffer, 0, port->interrupt_out_size); 804 805 spin_lock_irqsave(&priv->lock, flags); 806 switch (port->interrupt_out_size) { 807 case 32: 808 /* this is for the CY7C64013... */ 809 offset = 2; 810 port->interrupt_out_buffer[0] = priv->line_control; 811 break; 812 case 8: 813 /* this is for the CY7C63743... */ 814 offset = 1; 815 port->interrupt_out_buffer[0] = priv->line_control; 816 break; 817 default: 818 dbg("%s - wrong packet size", __FUNCTION__); 819 spin_unlock_irqrestore(&priv->lock, flags); 820 return; 821 } 822 823 if (priv->line_control & CONTROL_RESET) 824 priv->line_control &= ~CONTROL_RESET; 825 826 if (priv->cmd_ctrl) { 827 priv->cmd_count++; 828 dbg("%s - line control command being issued", __FUNCTION__); 829 spin_unlock_irqrestore(&priv->lock, flags); 830 goto send; 831 } else 832 spin_unlock_irqrestore(&priv->lock, flags); 833 834 count = cypress_buf_get(priv->buf, &port->interrupt_out_buffer[offset], 835 port->interrupt_out_size-offset); 836 837 if (count == 0) { 838 return; 839 } 840 841 switch (port->interrupt_out_size) { 842 case 32: 843 port->interrupt_out_buffer[1] = count; 844 break; 845 case 8: 846 port->interrupt_out_buffer[0] |= count; 847 } 848 849 dbg("%s - count is %d", __FUNCTION__, count); 850 851 send: 852 spin_lock_irqsave(&priv->lock, flags); 853 priv->write_urb_in_use = 1; 854 spin_unlock_irqrestore(&priv->lock, flags); 855 856 if (priv->cmd_ctrl) 857 actual_size = 1; 858 else 859 actual_size = count + (port->interrupt_out_size == 32 ? 2 : 1); 860 861 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, port->interrupt_out_size, 862 port->interrupt_out_urb->transfer_buffer); 863 864 usb_fill_int_urb(port->interrupt_out_urb, port->serial->dev, 865 usb_sndintpipe(port->serial->dev, port->interrupt_out_endpointAddress), 866 port->interrupt_out_buffer, port->interrupt_out_size, 867 cypress_write_int_callback, port, priv->write_urb_interval); 868 result = usb_submit_urb (port->interrupt_out_urb, GFP_ATOMIC); 869 if (result) { 870 dev_err(&port->dev, "%s - failed submitting write urb, error %d\n", __FUNCTION__, 871 result); 872 priv->write_urb_in_use = 0; 873 cypress_set_dead(port); 874 } 875 876 spin_lock_irqsave(&priv->lock, flags); 877 if (priv->cmd_ctrl) { 878 priv->cmd_ctrl = 0; 879 } 880 priv->bytes_out += count; /* do not count the line control and size bytes */ 881 spin_unlock_irqrestore(&priv->lock, flags); 882 883 usb_serial_port_softint(port); 884 } /* cypress_send */ 885 886 887 /* returns how much space is available in the soft buffer */ 888 static int cypress_write_room(struct usb_serial_port *port) 889 { 890 struct cypress_private *priv = usb_get_serial_port_data(port); 891 int room = 0; 892 unsigned long flags; 893 894 dbg("%s - port %d", __FUNCTION__, port->number); 895 896 spin_lock_irqsave(&priv->lock, flags); 897 room = cypress_buf_space_avail(priv->buf); 898 spin_unlock_irqrestore(&priv->lock, flags); 899 900 dbg("%s - returns %d", __FUNCTION__, room); 901 return room; 902 } 903 904 905 static int cypress_tiocmget (struct usb_serial_port *port, struct file *file) 906 { 907 struct cypress_private *priv = usb_get_serial_port_data(port); 908 __u8 status, control; 909 unsigned int result = 0; 910 unsigned long flags; 911 912 dbg("%s - port %d", __FUNCTION__, port->number); 913 914 spin_lock_irqsave(&priv->lock, flags); 915 control = priv->line_control; 916 status = priv->current_status; 917 spin_unlock_irqrestore(&priv->lock, flags); 918 919 result = ((control & CONTROL_DTR) ? TIOCM_DTR : 0) 920 | ((control & CONTROL_RTS) ? TIOCM_RTS : 0) 921 | ((status & UART_CTS) ? TIOCM_CTS : 0) 922 | ((status & UART_DSR) ? TIOCM_DSR : 0) 923 | ((status & UART_RI) ? TIOCM_RI : 0) 924 | ((status & UART_CD) ? TIOCM_CD : 0); 925 926 dbg("%s - result = %x", __FUNCTION__, result); 927 928 return result; 929 } 930 931 932 static int cypress_tiocmset (struct usb_serial_port *port, struct file *file, 933 unsigned int set, unsigned int clear) 934 { 935 struct cypress_private *priv = usb_get_serial_port_data(port); 936 unsigned long flags; 937 938 dbg("%s - port %d", __FUNCTION__, port->number); 939 940 spin_lock_irqsave(&priv->lock, flags); 941 if (set & TIOCM_RTS) 942 priv->line_control |= CONTROL_RTS; 943 if (set & TIOCM_DTR) 944 priv->line_control |= CONTROL_DTR; 945 if (clear & TIOCM_RTS) 946 priv->line_control &= ~CONTROL_RTS; 947 if (clear & TIOCM_DTR) 948 priv->line_control &= ~CONTROL_DTR; 949 spin_unlock_irqrestore(&priv->lock, flags); 950 951 priv->cmd_ctrl = 1; 952 return cypress_write(port, NULL, 0); 953 } 954 955 956 static int cypress_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg) 957 { 958 struct cypress_private *priv = usb_get_serial_port_data(port); 959 960 dbg("%s - port %d, cmd 0x%.4x", __FUNCTION__, port->number, cmd); 961 962 switch (cmd) { 963 case TIOCGSERIAL: 964 if (copy_to_user((void __user *)arg, port->tty->termios, sizeof(struct ktermios))) { 965 return -EFAULT; 966 } 967 return (0); 968 break; 969 case TIOCSSERIAL: 970 if (copy_from_user(port->tty->termios, (void __user *)arg, sizeof(struct ktermios))) { 971 return -EFAULT; 972 } 973 /* here we need to call cypress_set_termios to invoke the new settings */ 974 cypress_set_termios(port, &priv->tmp_termios); 975 return (0); 976 break; 977 /* This code comes from drivers/char/serial.c and ftdi_sio.c */ 978 case TIOCMIWAIT: 979 while (priv != NULL) { 980 interruptible_sleep_on(&priv->delta_msr_wait); 981 /* see if a signal did it */ 982 if (signal_pending(current)) 983 return -ERESTARTSYS; 984 else { 985 char diff = priv->diff_status; 986 987 if (diff == 0) { 988 return -EIO; /* no change => error */ 989 } 990 991 /* consume all events */ 992 priv->diff_status = 0; 993 994 /* return 0 if caller wanted to know about these bits */ 995 if ( ((arg & TIOCM_RNG) && (diff & UART_RI)) || 996 ((arg & TIOCM_DSR) && (diff & UART_DSR)) || 997 ((arg & TIOCM_CD) && (diff & UART_CD)) || 998 ((arg & TIOCM_CTS) && (diff & UART_CTS)) ) { 999 return 0; 1000 } 1001 /* otherwise caller can't care less about what happened, 1002 * and so we continue to wait for more events. 1003 */ 1004 } 1005 } 1006 return 0; 1007 break; 1008 default: 1009 break; 1010 } 1011 1012 dbg("%s - arg not supported - it was 0x%04x - check include/asm/ioctls.h", __FUNCTION__, cmd); 1013 1014 return -ENOIOCTLCMD; 1015 } /* cypress_ioctl */ 1016 1017 1018 static void cypress_set_termios (struct usb_serial_port *port, 1019 struct ktermios *old_termios) 1020 { 1021 struct cypress_private *priv = usb_get_serial_port_data(port); 1022 struct tty_struct *tty; 1023 int data_bits, stop_bits, parity_type, parity_enable; 1024 unsigned cflag, iflag, baud_mask; 1025 unsigned long flags; 1026 __u8 oldlines; 1027 int linechange = 0; 1028 1029 dbg("%s - port %d", __FUNCTION__, port->number); 1030 1031 tty = port->tty; 1032 if ((!tty) || (!tty->termios)) { 1033 dbg("%s - no tty structures", __FUNCTION__); 1034 return; 1035 } 1036 1037 spin_lock_irqsave(&priv->lock, flags); 1038 if (!priv->termios_initialized) { 1039 if (priv->chiptype == CT_EARTHMATE) { 1040 *(tty->termios) = tty_std_termios; 1041 tty->termios->c_cflag = B4800 | CS8 | CREAD | HUPCL | 1042 CLOCAL; 1043 } else if (priv->chiptype == CT_CYPHIDCOM) { 1044 *(tty->termios) = tty_std_termios; 1045 tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL | 1046 CLOCAL; 1047 } else if (priv->chiptype == CT_CA42V2) { 1048 *(tty->termios) = tty_std_termios; 1049 tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL | 1050 CLOCAL; 1051 } 1052 priv->termios_initialized = 1; 1053 } 1054 spin_unlock_irqrestore(&priv->lock, flags); 1055 1056 cflag = tty->termios->c_cflag; 1057 iflag = tty->termios->c_iflag; 1058 1059 /* check if there are new settings */ 1060 if (old_termios) { 1061 if ((cflag != old_termios->c_cflag) || 1062 (RELEVANT_IFLAG(iflag) != 1063 RELEVANT_IFLAG(old_termios->c_iflag))) { 1064 dbg("%s - attempting to set new termios settings", 1065 __FUNCTION__); 1066 /* should make a copy of this in case something goes 1067 * wrong in the function, we can restore it */ 1068 spin_lock_irqsave(&priv->lock, flags); 1069 priv->tmp_termios = *(tty->termios); 1070 spin_unlock_irqrestore(&priv->lock, flags); 1071 } else { 1072 dbg("%s - nothing to do, exiting", __FUNCTION__); 1073 return; 1074 } 1075 } else 1076 return; 1077 1078 /* set number of data bits, parity, stop bits */ 1079 /* when parity is disabled the parity type bit is ignored */ 1080 1081 /* 1 means 2 stop bits, 0 means 1 stop bit */ 1082 stop_bits = cflag & CSTOPB ? 1 : 0; 1083 1084 if (cflag & PARENB) { 1085 parity_enable = 1; 1086 /* 1 means odd parity, 0 means even parity */ 1087 parity_type = cflag & PARODD ? 1 : 0; 1088 } else 1089 parity_enable = parity_type = 0; 1090 1091 if (cflag & CSIZE) { 1092 switch (cflag & CSIZE) { 1093 case CS5: 1094 data_bits = 0; 1095 break; 1096 case CS6: 1097 data_bits = 1; 1098 break; 1099 case CS7: 1100 data_bits = 2; 1101 break; 1102 case CS8: 1103 data_bits = 3; 1104 break; 1105 default: 1106 err("%s - CSIZE was set, but not CS5-CS8", 1107 __FUNCTION__); 1108 data_bits = 3; 1109 } 1110 } else 1111 data_bits = 3; 1112 1113 spin_lock_irqsave(&priv->lock, flags); 1114 oldlines = priv->line_control; 1115 if ((cflag & CBAUD) == B0) { 1116 /* drop dtr and rts */ 1117 dbg("%s - dropping the lines, baud rate 0bps", __FUNCTION__); 1118 baud_mask = B0; 1119 priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS); 1120 } else { 1121 baud_mask = (cflag & CBAUD); 1122 switch(baud_mask) { 1123 case B300: 1124 dbg("%s - setting baud 300bps", __FUNCTION__); 1125 break; 1126 case B600: 1127 dbg("%s - setting baud 600bps", __FUNCTION__); 1128 break; 1129 case B1200: 1130 dbg("%s - setting baud 1200bps", __FUNCTION__); 1131 break; 1132 case B2400: 1133 dbg("%s - setting baud 2400bps", __FUNCTION__); 1134 break; 1135 case B4800: 1136 dbg("%s - setting baud 4800bps", __FUNCTION__); 1137 break; 1138 case B9600: 1139 dbg("%s - setting baud 9600bps", __FUNCTION__); 1140 break; 1141 case B19200: 1142 dbg("%s - setting baud 19200bps", __FUNCTION__); 1143 break; 1144 case B38400: 1145 dbg("%s - setting baud 38400bps", __FUNCTION__); 1146 break; 1147 case B57600: 1148 dbg("%s - setting baud 57600bps", __FUNCTION__); 1149 break; 1150 case B115200: 1151 dbg("%s - setting baud 115200bps", __FUNCTION__); 1152 break; 1153 default: 1154 dbg("%s - unknown masked baud rate", __FUNCTION__); 1155 } 1156 priv->line_control = (CONTROL_DTR | CONTROL_RTS); 1157 } 1158 spin_unlock_irqrestore(&priv->lock, flags); 1159 1160 dbg("%s - sending %d stop_bits, %d parity_enable, %d parity_type, " 1161 "%d data_bits (+5)", __FUNCTION__, stop_bits, 1162 parity_enable, parity_type, data_bits); 1163 1164 cypress_serial_control(port, baud_mask, data_bits, stop_bits, 1165 parity_enable, parity_type, 0, CYPRESS_SET_CONFIG); 1166 1167 /* we perform a CYPRESS_GET_CONFIG so that the current settings are 1168 * filled into the private structure this should confirm that all is 1169 * working if it returns what we just set */ 1170 cypress_serial_control(port, 0, 0, 0, 0, 0, 0, CYPRESS_GET_CONFIG); 1171 1172 /* Here we can define custom tty settings for devices; the main tty 1173 * termios flag base comes from empeg.c */ 1174 1175 spin_lock_irqsave(&priv->lock, flags); 1176 if ( (priv->chiptype == CT_EARTHMATE) && (priv->baud_rate == 4800) ) { 1177 dbg("Using custom termios settings for a baud rate of " 1178 "4800bps."); 1179 /* define custom termios settings for NMEA protocol */ 1180 1181 tty->termios->c_iflag /* input modes - */ 1182 &= ~(IGNBRK /* disable ignore break */ 1183 | BRKINT /* disable break causes interrupt */ 1184 | PARMRK /* disable mark parity errors */ 1185 | ISTRIP /* disable clear high bit of input char */ 1186 | INLCR /* disable translate NL to CR */ 1187 | IGNCR /* disable ignore CR */ 1188 | ICRNL /* disable translate CR to NL */ 1189 | IXON); /* disable enable XON/XOFF flow control */ 1190 1191 tty->termios->c_oflag /* output modes */ 1192 &= ~OPOST; /* disable postprocess output char */ 1193 1194 tty->termios->c_lflag /* line discipline modes */ 1195 &= ~(ECHO /* disable echo input characters */ 1196 | ECHONL /* disable echo new line */ 1197 | ICANON /* disable erase, kill, werase, and rprnt 1198 special characters */ 1199 | ISIG /* disable interrupt, quit, and suspend 1200 special characters */ 1201 | IEXTEN); /* disable non-POSIX special characters */ 1202 } /* CT_CYPHIDCOM: Application should handle this for device */ 1203 1204 linechange = (priv->line_control != oldlines); 1205 spin_unlock_irqrestore(&priv->lock, flags); 1206 1207 /* if necessary, set lines */ 1208 if (linechange) { 1209 priv->cmd_ctrl = 1; 1210 cypress_write(port, NULL, 0); 1211 } 1212 } /* cypress_set_termios */ 1213 1214 1215 /* returns amount of data still left in soft buffer */ 1216 static int cypress_chars_in_buffer(struct usb_serial_port *port) 1217 { 1218 struct cypress_private *priv = usb_get_serial_port_data(port); 1219 int chars = 0; 1220 unsigned long flags; 1221 1222 dbg("%s - port %d", __FUNCTION__, port->number); 1223 1224 spin_lock_irqsave(&priv->lock, flags); 1225 chars = cypress_buf_data_avail(priv->buf); 1226 spin_unlock_irqrestore(&priv->lock, flags); 1227 1228 dbg("%s - returns %d", __FUNCTION__, chars); 1229 return chars; 1230 } 1231 1232 1233 static void cypress_throttle (struct usb_serial_port *port) 1234 { 1235 struct cypress_private *priv = usb_get_serial_port_data(port); 1236 unsigned long flags; 1237 1238 dbg("%s - port %d", __FUNCTION__, port->number); 1239 1240 spin_lock_irqsave(&priv->lock, flags); 1241 priv->rx_flags = THROTTLED; 1242 spin_unlock_irqrestore(&priv->lock, flags); 1243 } 1244 1245 1246 static void cypress_unthrottle (struct usb_serial_port *port) 1247 { 1248 struct cypress_private *priv = usb_get_serial_port_data(port); 1249 int actually_throttled, result; 1250 unsigned long flags; 1251 1252 dbg("%s - port %d", __FUNCTION__, port->number); 1253 1254 spin_lock_irqsave(&priv->lock, flags); 1255 actually_throttled = priv->rx_flags & ACTUALLY_THROTTLED; 1256 priv->rx_flags = 0; 1257 spin_unlock_irqrestore(&priv->lock, flags); 1258 1259 if (!priv->comm_is_ok) 1260 return; 1261 1262 if (actually_throttled) { 1263 port->interrupt_in_urb->dev = port->serial->dev; 1264 1265 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC); 1266 if (result) { 1267 dev_err(&port->dev, "%s - failed submitting read urb, " 1268 "error %d\n", __FUNCTION__, result); 1269 cypress_set_dead(port); 1270 } 1271 } 1272 } 1273 1274 1275 static void cypress_read_int_callback(struct urb *urb) 1276 { 1277 struct usb_serial_port *port = (struct usb_serial_port *)urb->context; 1278 struct cypress_private *priv = usb_get_serial_port_data(port); 1279 struct tty_struct *tty; 1280 unsigned char *data = urb->transfer_buffer; 1281 unsigned long flags; 1282 char tty_flag = TTY_NORMAL; 1283 int havedata = 0; 1284 int bytes = 0; 1285 int result; 1286 int i = 0; 1287 int status = urb->status; 1288 1289 dbg("%s - port %d", __FUNCTION__, port->number); 1290 1291 switch (status) { 1292 case 0: /* success */ 1293 break; 1294 case -ECONNRESET: 1295 case -ENOENT: 1296 case -ESHUTDOWN: 1297 /* precursor to disconnect so just go away */ 1298 return; 1299 case -EPIPE: 1300 usb_clear_halt(port->serial->dev,0x81); 1301 break; 1302 default: 1303 /* something ugly is going on... */ 1304 dev_err(&urb->dev->dev,"%s - unexpected nonzero read status received: %d\n", 1305 __FUNCTION__, status); 1306 cypress_set_dead(port); 1307 return; 1308 } 1309 1310 spin_lock_irqsave(&priv->lock, flags); 1311 if (priv->rx_flags & THROTTLED) { 1312 dbg("%s - now throttling", __FUNCTION__); 1313 priv->rx_flags |= ACTUALLY_THROTTLED; 1314 spin_unlock_irqrestore(&priv->lock, flags); 1315 return; 1316 } 1317 spin_unlock_irqrestore(&priv->lock, flags); 1318 1319 tty = port->tty; 1320 if (!tty) { 1321 dbg("%s - bad tty pointer - exiting", __FUNCTION__); 1322 return; 1323 } 1324 1325 spin_lock_irqsave(&priv->lock, flags); 1326 switch(urb->actual_length) { 1327 case 32: 1328 /* This is for the CY7C64013... */ 1329 priv->current_status = data[0] & 0xF8; 1330 bytes = data[1] + 2; 1331 i = 2; 1332 if (bytes > 2) 1333 havedata = 1; 1334 break; 1335 case 8: 1336 /* This is for the CY7C63743... */ 1337 priv->current_status = data[0] & 0xF8; 1338 bytes = (data[0] & 0x07) + 1; 1339 i = 1; 1340 if (bytes > 1) 1341 havedata = 1; 1342 break; 1343 default: 1344 dbg("%s - wrong packet size - received %d bytes", 1345 __FUNCTION__, urb->actual_length); 1346 spin_unlock_irqrestore(&priv->lock, flags); 1347 goto continue_read; 1348 } 1349 spin_unlock_irqrestore(&priv->lock, flags); 1350 1351 usb_serial_debug_data (debug, &port->dev, __FUNCTION__, 1352 urb->actual_length, data); 1353 1354 spin_lock_irqsave(&priv->lock, flags); 1355 /* check to see if status has changed */ 1356 if (priv != NULL) { 1357 if (priv->current_status != priv->prev_status) { 1358 priv->diff_status |= priv->current_status ^ 1359 priv->prev_status; 1360 wake_up_interruptible(&priv->delta_msr_wait); 1361 priv->prev_status = priv->current_status; 1362 } 1363 } 1364 spin_unlock_irqrestore(&priv->lock, flags); 1365 1366 /* hangup, as defined in acm.c... this might be a bad place for it 1367 * though */ 1368 if (tty && !(tty->termios->c_cflag & CLOCAL) && 1369 !(priv->current_status & UART_CD)) { 1370 dbg("%s - calling hangup", __FUNCTION__); 1371 tty_hangup(tty); 1372 goto continue_read; 1373 } 1374 1375 /* There is one error bit... I'm assuming it is a parity error 1376 * indicator as the generic firmware will set this bit to 1 if a 1377 * parity error occurs. 1378 * I can not find reference to any other error events. */ 1379 spin_lock_irqsave(&priv->lock, flags); 1380 if (priv->current_status & CYP_ERROR) { 1381 spin_unlock_irqrestore(&priv->lock, flags); 1382 tty_flag = TTY_PARITY; 1383 dbg("%s - Parity Error detected", __FUNCTION__); 1384 } else 1385 spin_unlock_irqrestore(&priv->lock, flags); 1386 1387 /* process read if there is data other than line status */ 1388 if (tty && (bytes > i)) { 1389 bytes = tty_buffer_request_room(tty, bytes); 1390 for (; i < bytes ; ++i) { 1391 dbg("pushing byte number %d - %d - %c", i, data[i], 1392 data[i]); 1393 tty_insert_flip_char(tty, data[i], tty_flag); 1394 } 1395 tty_flip_buffer_push(port->tty); 1396 } 1397 1398 spin_lock_irqsave(&priv->lock, flags); 1399 /* control and status byte(s) are also counted */ 1400 priv->bytes_in += bytes; 1401 spin_unlock_irqrestore(&priv->lock, flags); 1402 1403 continue_read: 1404 1405 /* Continue trying to always read... unless the port has closed. */ 1406 1407 if (port->open_count > 0 && priv->comm_is_ok) { 1408 usb_fill_int_urb(port->interrupt_in_urb, port->serial->dev, 1409 usb_rcvintpipe(port->serial->dev, 1410 port->interrupt_in_endpointAddress), 1411 port->interrupt_in_urb->transfer_buffer, 1412 port->interrupt_in_urb->transfer_buffer_length, 1413 cypress_read_int_callback, port, priv->read_urb_interval); 1414 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC); 1415 if (result) { 1416 dev_err(&urb->dev->dev, "%s - failed resubmitting " 1417 "read urb, error %d\n", __FUNCTION__, 1418 result); 1419 cypress_set_dead(port); 1420 } 1421 } 1422 1423 return; 1424 } /* cypress_read_int_callback */ 1425 1426 1427 static void cypress_write_int_callback(struct urb *urb) 1428 { 1429 struct usb_serial_port *port = (struct usb_serial_port *)urb->context; 1430 struct cypress_private *priv = usb_get_serial_port_data(port); 1431 int result; 1432 int status = urb->status; 1433 1434 dbg("%s - port %d", __FUNCTION__, port->number); 1435 1436 switch (status) { 1437 case 0: 1438 /* success */ 1439 break; 1440 case -ECONNRESET: 1441 case -ENOENT: 1442 case -ESHUTDOWN: 1443 /* this urb is terminated, clean up */ 1444 dbg("%s - urb shutting down with status: %d", 1445 __FUNCTION__, status); 1446 priv->write_urb_in_use = 0; 1447 return; 1448 case -EPIPE: /* no break needed; clear halt and resubmit */ 1449 if (!priv->comm_is_ok) 1450 break; 1451 usb_clear_halt(port->serial->dev, 0x02); 1452 /* error in the urb, so we have to resubmit it */ 1453 dbg("%s - nonzero write bulk status received: %d", 1454 __FUNCTION__, status); 1455 port->interrupt_out_urb->transfer_buffer_length = 1; 1456 port->interrupt_out_urb->dev = port->serial->dev; 1457 result = usb_submit_urb(port->interrupt_out_urb, GFP_ATOMIC); 1458 if (!result) 1459 return; 1460 dev_err(&urb->dev->dev, "%s - failed resubmitting write urb, error %d\n", 1461 __FUNCTION__, result); 1462 cypress_set_dead(port); 1463 break; 1464 default: 1465 dev_err(&urb->dev->dev,"%s - unexpected nonzero write status received: %d\n", 1466 __FUNCTION__, status); 1467 cypress_set_dead(port); 1468 break; 1469 } 1470 1471 priv->write_urb_in_use = 0; 1472 1473 /* send any buffered data */ 1474 cypress_send(port); 1475 } 1476 1477 1478 /***************************************************************************** 1479 * Write buffer functions - buffering code from pl2303 used 1480 *****************************************************************************/ 1481 1482 /* 1483 * cypress_buf_alloc 1484 * 1485 * Allocate a circular buffer and all associated memory. 1486 */ 1487 1488 static struct cypress_buf *cypress_buf_alloc(unsigned int size) 1489 { 1490 1491 struct cypress_buf *cb; 1492 1493 1494 if (size == 0) 1495 return NULL; 1496 1497 cb = kmalloc(sizeof(struct cypress_buf), GFP_KERNEL); 1498 if (cb == NULL) 1499 return NULL; 1500 1501 cb->buf_buf = kmalloc(size, GFP_KERNEL); 1502 if (cb->buf_buf == NULL) { 1503 kfree(cb); 1504 return NULL; 1505 } 1506 1507 cb->buf_size = size; 1508 cb->buf_get = cb->buf_put = cb->buf_buf; 1509 1510 return cb; 1511 1512 } 1513 1514 1515 /* 1516 * cypress_buf_free 1517 * 1518 * Free the buffer and all associated memory. 1519 */ 1520 1521 static void cypress_buf_free(struct cypress_buf *cb) 1522 { 1523 if (cb) { 1524 kfree(cb->buf_buf); 1525 kfree(cb); 1526 } 1527 } 1528 1529 1530 /* 1531 * cypress_buf_clear 1532 * 1533 * Clear out all data in the circular buffer. 1534 */ 1535 1536 static void cypress_buf_clear(struct cypress_buf *cb) 1537 { 1538 if (cb != NULL) 1539 cb->buf_get = cb->buf_put; 1540 /* equivalent to a get of all data available */ 1541 } 1542 1543 1544 /* 1545 * cypress_buf_data_avail 1546 * 1547 * Return the number of bytes of data available in the circular 1548 * buffer. 1549 */ 1550 1551 static unsigned int cypress_buf_data_avail(struct cypress_buf *cb) 1552 { 1553 if (cb != NULL) 1554 return ((cb->buf_size + cb->buf_put - cb->buf_get) % cb->buf_size); 1555 else 1556 return 0; 1557 } 1558 1559 1560 /* 1561 * cypress_buf_space_avail 1562 * 1563 * Return the number of bytes of space available in the circular 1564 * buffer. 1565 */ 1566 1567 static unsigned int cypress_buf_space_avail(struct cypress_buf *cb) 1568 { 1569 if (cb != NULL) 1570 return ((cb->buf_size + cb->buf_get - cb->buf_put - 1) % cb->buf_size); 1571 else 1572 return 0; 1573 } 1574 1575 1576 /* 1577 * cypress_buf_put 1578 * 1579 * Copy data data from a user buffer and put it into the circular buffer. 1580 * Restrict to the amount of space available. 1581 * 1582 * Return the number of bytes copied. 1583 */ 1584 1585 static unsigned int cypress_buf_put(struct cypress_buf *cb, const char *buf, 1586 unsigned int count) 1587 { 1588 1589 unsigned int len; 1590 1591 1592 if (cb == NULL) 1593 return 0; 1594 1595 len = cypress_buf_space_avail(cb); 1596 if (count > len) 1597 count = len; 1598 1599 if (count == 0) 1600 return 0; 1601 1602 len = cb->buf_buf + cb->buf_size - cb->buf_put; 1603 if (count > len) { 1604 memcpy(cb->buf_put, buf, len); 1605 memcpy(cb->buf_buf, buf+len, count - len); 1606 cb->buf_put = cb->buf_buf + count - len; 1607 } else { 1608 memcpy(cb->buf_put, buf, count); 1609 if (count < len) 1610 cb->buf_put += count; 1611 else /* count == len */ 1612 cb->buf_put = cb->buf_buf; 1613 } 1614 1615 return count; 1616 1617 } 1618 1619 1620 /* 1621 * cypress_buf_get 1622 * 1623 * Get data from the circular buffer and copy to the given buffer. 1624 * Restrict to the amount of data available. 1625 * 1626 * Return the number of bytes copied. 1627 */ 1628 1629 static unsigned int cypress_buf_get(struct cypress_buf *cb, char *buf, 1630 unsigned int count) 1631 { 1632 1633 unsigned int len; 1634 1635 1636 if (cb == NULL) 1637 return 0; 1638 1639 len = cypress_buf_data_avail(cb); 1640 if (count > len) 1641 count = len; 1642 1643 if (count == 0) 1644 return 0; 1645 1646 len = cb->buf_buf + cb->buf_size - cb->buf_get; 1647 if (count > len) { 1648 memcpy(buf, cb->buf_get, len); 1649 memcpy(buf+len, cb->buf_buf, count - len); 1650 cb->buf_get = cb->buf_buf + count - len; 1651 } else { 1652 memcpy(buf, cb->buf_get, count); 1653 if (count < len) 1654 cb->buf_get += count; 1655 else /* count == len */ 1656 cb->buf_get = cb->buf_buf; 1657 } 1658 1659 return count; 1660 1661 } 1662 1663 /***************************************************************************** 1664 * Module functions 1665 *****************************************************************************/ 1666 1667 static int __init cypress_init(void) 1668 { 1669 int retval; 1670 1671 dbg("%s", __FUNCTION__); 1672 1673 retval = usb_serial_register(&cypress_earthmate_device); 1674 if (retval) 1675 goto failed_em_register; 1676 retval = usb_serial_register(&cypress_hidcom_device); 1677 if (retval) 1678 goto failed_hidcom_register; 1679 retval = usb_serial_register(&cypress_ca42v2_device); 1680 if (retval) 1681 goto failed_ca42v2_register; 1682 retval = usb_register(&cypress_driver); 1683 if (retval) 1684 goto failed_usb_register; 1685 1686 info(DRIVER_DESC " " DRIVER_VERSION); 1687 return 0; 1688 1689 failed_usb_register: 1690 usb_serial_deregister(&cypress_ca42v2_device); 1691 failed_ca42v2_register: 1692 usb_serial_deregister(&cypress_hidcom_device); 1693 failed_hidcom_register: 1694 usb_serial_deregister(&cypress_earthmate_device); 1695 failed_em_register: 1696 return retval; 1697 } 1698 1699 1700 static void __exit cypress_exit (void) 1701 { 1702 dbg("%s", __FUNCTION__); 1703 1704 usb_deregister (&cypress_driver); 1705 usb_serial_deregister (&cypress_earthmate_device); 1706 usb_serial_deregister (&cypress_hidcom_device); 1707 usb_serial_deregister (&cypress_ca42v2_device); 1708 } 1709 1710 1711 module_init(cypress_init); 1712 module_exit(cypress_exit); 1713 1714 MODULE_AUTHOR( DRIVER_AUTHOR ); 1715 MODULE_DESCRIPTION( DRIVER_DESC ); 1716 MODULE_VERSION( DRIVER_VERSION ); 1717 MODULE_LICENSE("GPL"); 1718 1719 module_param(debug, bool, S_IRUGO | S_IWUSR); 1720 MODULE_PARM_DESC(debug, "Debug enabled or not"); 1721 module_param(stats, bool, S_IRUGO | S_IWUSR); 1722 MODULE_PARM_DESC(stats, "Enable statistics or not"); 1723 module_param(interval, int, S_IRUGO | S_IWUSR); 1724 MODULE_PARM_DESC(interval, "Overrides interrupt interval"); 1725