X-Git-Url: http://ftp.safe.ca/?a=blobdiff_plain;f=drivers%2Fusb%2Fserial%2Fcypress_m8.c;h=a1fdb85b8c0ae91225c447f5cb79b6c19792e991;hb=606d099cdd1080bbb50ea50dc52d98252f8f10a1;hp=db8f472d9e3f279ef72ab8dba68296cf8a841b07;hpb=1da177e4c3f41524e886b7f1b8a0c1fc7321cac2;p=safe%2Fjmp%2Flinux-2.6 diff --git a/drivers/usb/serial/cypress_m8.c b/drivers/usb/serial/cypress_m8.c index db8f472..a1fdb85 100644 --- a/drivers/usb/serial/cypress_m8.c +++ b/drivers/usb/serial/cypress_m8.c @@ -16,6 +16,14 @@ * See http://geocities.com/i0xox0i for information on this driver and the * earthmate usb device. * + * Lonnie Mendez + * 4-29-2005 + * Fixed problem where setting or retreiving the serial config would fail with + * EPIPE. Removed CRTS toggling so the driver behaves more like other usbserial + * adapters. Issued new interval of 1ms instead of the default 10ms. As a + * result, transfer speed has been substantially increased. From avg. 850bps to + * avg. 3300bps. initial termios has also been modified. Cleaned up code and + * formatting issues so it is more readable. Replaced the C++ style comments. * * Lonnie Mendez * 12-15-2004 @@ -32,12 +40,6 @@ * 10-2003 * Driver first released. * - * - * Long Term TODO: - * Improve transfer speeds - both read/write are somewhat slow - * at this point. - * Improve debugging. Show modem line status with debug output and - * implement filtering for certain data as a module parameter. */ /* Thanks to Neil Whelchel for writing the first cypress m8 implementation for linux. */ @@ -46,7 +48,6 @@ /* Code originates and was built up from ftdi_sio, belkin, pl2303 and others. */ -#include #include #include #include @@ -58,11 +59,11 @@ #include #include #include +#include #include #include #include -#include "usb-serial.h" #include "cypress_m8.h" @@ -72,11 +73,12 @@ static int debug; #endif static int stats; +static int interval; /* * Version Information */ -#define DRIVER_VERSION "v1.08" +#define DRIVER_VERSION "v1.09" #define DRIVER_AUTHOR "Lonnie Mendez , Neil Whelchel " #define DRIVER_DESC "Cypress USB to Serial Driver" @@ -86,6 +88,7 @@ static int stats; static struct usb_device_id id_table_earthmate [] = { { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB) }, + { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB_LT20) }, { } /* Terminating entry */ }; @@ -94,9 +97,16 @@ static struct usb_device_id id_table_cyphidcomrs232 [] = { { } /* Terminating entry */ }; +static struct usb_device_id id_table_nokiaca42v2 [] = { + { USB_DEVICE(VENDOR_ID_DAZZLE, PRODUCT_ID_CA42) }, + { } /* Terminating entry */ +}; + static struct usb_device_id id_table_combined [] = { { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB) }, + { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB_LT20) }, { USB_DEVICE(VENDOR_ID_CYPRESS, PRODUCT_ID_CYPHIDCOM) }, + { USB_DEVICE(VENDOR_ID_DAZZLE, PRODUCT_ID_CA42) }, { } /* Terminating entry */ }; @@ -107,6 +117,7 @@ static struct usb_driver cypress_driver = { .probe = usb_serial_probe, .disconnect = usb_serial_disconnect, .id_table = id_table_combined, + .no_dynamic_id = 1, }; struct cypress_private { @@ -118,6 +129,9 @@ struct cypress_private { int cmd_ctrl; /* always set this to 1 before issuing a command */ struct cypress_buf *buf; /* write buffer */ int write_urb_in_use; /* write urb in use indicator */ + int write_urb_interval; /* interval to use for write urb */ + int read_urb_interval; /* interval to use for read urb */ + int comm_is_ok; /* true if communication is (still) ok */ int termios_initialized; __u8 line_control; /* holds dtr / rts value */ __u8 current_status; /* received from last read - info on dsr,cts,cd,ri,etc */ @@ -129,8 +143,7 @@ struct cypress_private { wait_queue_head_t delta_msr_wait; /* used for TIOCMIWAIT */ char prev_status, diff_status; /* used for TIOCMIWAIT */ /* we pass a pointer to this as the arguement sent to cypress_set_termios old_termios */ - struct termios tmp_termios; /* stores the old termios settings */ - char calledfromopen; /* used when issuing lines on open - fixes rts drop bug */ + struct ktermios tmp_termios; /* stores the old termios settings */ }; /* write buffer structure */ @@ -144,6 +157,7 @@ struct cypress_buf { /* function prototypes for the Cypress USB to serial device */ static int cypress_earthmate_startup (struct usb_serial *serial); static int cypress_hidcom_startup (struct usb_serial *serial); +static int cypress_ca42v2_startup (struct usb_serial *serial); static void cypress_shutdown (struct usb_serial *serial); static int cypress_open (struct usb_serial_port *port, struct file *filp); static void cypress_close (struct usb_serial_port *port, struct file *filp); @@ -151,14 +165,15 @@ static int cypress_write (struct usb_serial_port *port, const unsigned char *b static void cypress_send (struct usb_serial_port *port); static int cypress_write_room (struct usb_serial_port *port); static int cypress_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg); -static void cypress_set_termios (struct usb_serial_port *port, struct termios * old); +static void cypress_set_termios (struct usb_serial_port *port, struct ktermios * old); static int cypress_tiocmget (struct usb_serial_port *port, struct file *file); static int cypress_tiocmset (struct usb_serial_port *port, struct file *file, unsigned int set, unsigned int clear); static int cypress_chars_in_buffer (struct usb_serial_port *port); static void cypress_throttle (struct usb_serial_port *port); static void cypress_unthrottle (struct usb_serial_port *port); -static void cypress_read_int_callback (struct urb *urb, struct pt_regs *regs); -static void cypress_write_int_callback (struct urb *urb, struct pt_regs *regs); +static void cypress_set_dead (struct usb_serial_port *port); +static void cypress_read_int_callback (struct urb *urb); +static void cypress_write_int_callback (struct urb *urb); /* baud helper functions */ static int mask_to_rate (unsigned mask); static unsigned rate_to_mask (int rate); @@ -168,16 +183,16 @@ static void cypress_buf_free(struct cypress_buf *cb); static void cypress_buf_clear(struct cypress_buf *cb); static unsigned int cypress_buf_data_avail(struct cypress_buf *cb); static unsigned int cypress_buf_space_avail(struct cypress_buf *cb); -static unsigned int cypress_buf_put(struct cypress_buf *cb, const char *buf, - unsigned int count); -static unsigned int cypress_buf_get(struct cypress_buf *cb, char *buf, - unsigned int count); +static unsigned int cypress_buf_put(struct cypress_buf *cb, const char *buf, unsigned int count); +static unsigned int cypress_buf_get(struct cypress_buf *cb, char *buf, unsigned int count); -static struct usb_serial_device_type cypress_earthmate_device = { - .owner = THIS_MODULE, - .name = "DeLorme Earthmate USB", - .short_name = "earthmate", +static struct usb_serial_driver cypress_earthmate_device = { + .driver = { + .owner = THIS_MODULE, + .name = "earthmate", + }, + .description = "DeLorme Earthmate USB", .id_table = id_table_earthmate, .num_interrupt_in = 1, .num_interrupt_out = 1, @@ -201,10 +216,12 @@ static struct usb_serial_device_type cypress_earthmate_device = { .write_int_callback = cypress_write_int_callback, }; -static struct usb_serial_device_type cypress_hidcom_device = { - .owner = THIS_MODULE, - .name = "HID->COM RS232 Adapter", - .short_name = "cyphidcom", +static struct usb_serial_driver cypress_hidcom_device = { + .driver = { + .owner = THIS_MODULE, + .name = "cyphidcom", + }, + .description = "HID->COM RS232 Adapter", .id_table = id_table_cyphidcomrs232, .num_interrupt_in = 1, .num_interrupt_out = 1, @@ -228,26 +245,56 @@ static struct usb_serial_device_type cypress_hidcom_device = { .write_int_callback = cypress_write_int_callback, }; +static struct usb_serial_driver cypress_ca42v2_device = { + .driver = { + .owner = THIS_MODULE, + .name = "nokiaca42v2", + }, + .description = "Nokia CA-42 V2 Adapter", + .id_table = id_table_nokiaca42v2, + .num_interrupt_in = 1, + .num_interrupt_out = 1, + .num_bulk_in = NUM_DONT_CARE, + .num_bulk_out = NUM_DONT_CARE, + .num_ports = 1, + .attach = cypress_ca42v2_startup, + .shutdown = cypress_shutdown, + .open = cypress_open, + .close = cypress_close, + .write = cypress_write, + .write_room = cypress_write_room, + .ioctl = cypress_ioctl, + .set_termios = cypress_set_termios, + .tiocmget = cypress_tiocmget, + .tiocmset = cypress_tiocmset, + .chars_in_buffer = cypress_chars_in_buffer, + .throttle = cypress_throttle, + .unthrottle = cypress_unthrottle, + .read_int_callback = cypress_read_int_callback, + .write_int_callback = cypress_write_int_callback, +}; /***************************************************************************** * Cypress serial helper functions *****************************************************************************/ -/* This function can either set or retreive the current serial line settings */ +/* This function can either set or retrieve the current serial line settings */ static int cypress_serial_control (struct usb_serial_port *port, unsigned baud_mask, int data_bits, int stop_bits, int parity_enable, int parity_type, int reset, int cypress_request_type) { - int i, n_baud_rate = 0, retval = 0; + int new_baudrate = 0, retval = 0, tries = 0; struct cypress_private *priv; - __u8 feature_buffer[5]; - __u8 config; + __u8 feature_buffer[8]; unsigned long flags; dbg("%s", __FUNCTION__); priv = usb_get_serial_port_data(port); + if (!priv->comm_is_ok) + return -ENODEV; + switch(cypress_request_type) { case CYPRESS_SET_CONFIG: @@ -256,7 +303,8 @@ static int cypress_serial_control (struct usb_serial_port *port, unsigned baud_m * of 57600bps (I have no idea whether DeLorme chose to use the general purpose * firmware or not), if you need to modify this speed setting for your own * project please add your own chiptype and modify the code likewise. The - * Cypress HID->COM device will work successfully up to 115200bps. + * Cypress HID->COM device will work successfully up to 115200bps (but the + * actual throughput is around 3kBps). */ if (baud_mask != priv->cbr_mask) { dbg("%s - baud rate is changing", __FUNCTION__); @@ -265,109 +313,136 @@ static int cypress_serial_control (struct usb_serial_port *port, unsigned baud_m * but are not used with NMEA and SiRF protocols */ if ( (baud_mask == B300) || (baud_mask == B600) ) { - err("%s - failed setting baud rate, unsupported speed (default to 4800)", + err("%s - failed setting baud rate, unsupported speed", __FUNCTION__); - n_baud_rate = 4800; - } else if ( (n_baud_rate = mask_to_rate(baud_mask)) == -1) { - err("%s - failed setting baud rate, unsupported speed (default to 4800)", + new_baudrate = priv->baud_rate; + } else if ( (new_baudrate = mask_to_rate(baud_mask)) == -1) { + err("%s - failed setting baud rate, unsupported speed", __FUNCTION__); - n_baud_rate = 4800; + new_baudrate = priv->baud_rate; } } else if (priv->chiptype == CT_CYPHIDCOM) { - if ( (n_baud_rate = mask_to_rate(baud_mask)) == -1) { - err("%s - failed setting baud rate, unsupported speed (default to 4800)", + if ( (new_baudrate = mask_to_rate(baud_mask)) == -1) { + err("%s - failed setting baud rate, unsupported speed", __FUNCTION__); - n_baud_rate = 4800; + new_baudrate = priv->baud_rate; + } + } else if (priv->chiptype == CT_CA42V2) { + if ( (new_baudrate = mask_to_rate(baud_mask)) == -1) { + err("%s - failed setting baud rate, unsupported speed", + __FUNCTION__); + new_baudrate = priv->baud_rate; } } else if (priv->chiptype == CT_GENERIC) { - if ( (n_baud_rate = mask_to_rate(baud_mask)) == -1) { - err("%s - failed setting baud rate, unsupported speed (default to 4800)", + if ( (new_baudrate = mask_to_rate(baud_mask)) == -1) { + err("%s - failed setting baud rate, unsupported speed", __FUNCTION__); - n_baud_rate = 4800; + new_baudrate = priv->baud_rate; } } else { - info("%s - please define your chiptype, using 4800bps default", __FUNCTION__); - n_baud_rate = 4800; + info("%s - please define your chiptype", __FUNCTION__); + new_baudrate = priv->baud_rate; } } else { /* baud rate not changing, keep the old */ - n_baud_rate = priv->baud_rate; + new_baudrate = priv->baud_rate; } - dbg("%s - baud rate is being sent as %d", __FUNCTION__, n_baud_rate); - + dbg("%s - baud rate is being sent as %d", __FUNCTION__, new_baudrate); - /* - * This algorithm accredited to Jiang Jay Zhang... thanks for all the help! - */ - for (i = 0; i < 4; ++i) { - feature_buffer[i] = ( n_baud_rate >> (i*8) & 0xFF ); - } + memset(feature_buffer, 0, 8); + /* fill the feature_buffer with new configuration */ + *((u_int32_t *)feature_buffer) = new_baudrate; - config = 0; // reset config byte - config |= data_bits; // assign data bits in 2 bit space ( max 3 ) + feature_buffer[4] |= data_bits; /* assign data bits in 2 bit space ( max 3 ) */ /* 1 bit gap */ - config |= (stop_bits << 3); // assign stop bits in 1 bit space - config |= (parity_enable << 4); // assign parity flag in 1 bit space - config |= (parity_type << 5); // assign parity type in 1 bit space + feature_buffer[4] |= (stop_bits << 3); /* assign stop bits in 1 bit space */ + feature_buffer[4] |= (parity_enable << 4); /* assign parity flag in 1 bit space */ + feature_buffer[4] |= (parity_type << 5); /* assign parity type in 1 bit space */ /* 1 bit gap */ - config |= (reset << 7); // assign reset at end of byte, 1 bit space - - feature_buffer[4] = config; + feature_buffer[4] |= (reset << 7); /* assign reset at end of byte, 1 bit space */ dbg("%s - device is being sent this feature report:", __FUNCTION__); dbg("%s - %02X - %02X - %02X - %02X - %02X", __FUNCTION__, feature_buffer[0], feature_buffer[1], feature_buffer[2], feature_buffer[3], feature_buffer[4]); + do { retval = usb_control_msg (port->serial->dev, usb_sndctrlpipe(port->serial->dev, 0), HID_REQ_SET_REPORT, USB_DIR_OUT | USB_RECIP_INTERFACE | USB_TYPE_CLASS, - 0x0300, 0, feature_buffer, 5, 500); + 0x0300, 0, feature_buffer, 8, 500); + + if (tries++ >= 3) + break; - if (retval != 5) + } while (retval != 8 && retval != -ENODEV); + + if (retval != 8) { err("%s - failed sending serial line settings - %d", __FUNCTION__, retval); - else { + cypress_set_dead(port); + } else { spin_lock_irqsave(&priv->lock, flags); - priv->baud_rate = n_baud_rate; + priv->baud_rate = new_baudrate; priv->cbr_mask = baud_mask; - priv->current_config = config; - ++priv->cmd_count; + priv->current_config = feature_buffer[4]; spin_unlock_irqrestore(&priv->lock, flags); } break; case CYPRESS_GET_CONFIG: dbg("%s - retreiving serial line settings", __FUNCTION__); - /* reset values in feature buffer */ - memset(feature_buffer, 0, 5); + /* set initial values in feature buffer */ + memset(feature_buffer, 0, 8); + do { retval = usb_control_msg (port->serial->dev, usb_rcvctrlpipe(port->serial->dev, 0), HID_REQ_GET_REPORT, USB_DIR_IN | USB_RECIP_INTERFACE | USB_TYPE_CLASS, - 0x0300, 0, feature_buffer, 5, 500); + 0x0300, 0, feature_buffer, 8, 500); + + if (tries++ >= 3) + break; + + } while (retval != 5 && retval != -ENODEV); + if (retval != 5) { - err("%s - failed to retreive serial line settings - %d", __FUNCTION__, retval); + err("%s - failed to retrieve serial line settings - %d", __FUNCTION__, retval); + cypress_set_dead(port); return retval; } else { spin_lock_irqsave(&priv->lock, flags); + /* store the config in one byte, and later use bit masks to check values */ priv->current_config = feature_buffer[4]; - /* reverse the process above to get the baud_mask value */ - n_baud_rate = 0; // reset bits - for (i = 0; i < 4; ++i) { - n_baud_rate |= ( feature_buffer[i] << (i*8) ); - } + priv->baud_rate = *((u_int32_t *)feature_buffer); - priv->baud_rate = n_baud_rate; - if ( (priv->cbr_mask = rate_to_mask(n_baud_rate)) == 0x40) + if ( (priv->cbr_mask = rate_to_mask(priv->baud_rate)) == 0x40) dbg("%s - failed setting the baud mask (not defined)", __FUNCTION__); - ++priv->cmd_count; spin_unlock_irqrestore(&priv->lock, flags); } - break; - default: - err("%s - unsupported serial control command issued", __FUNCTION__); } + spin_lock_irqsave(&priv->lock, flags); + ++priv->cmd_count; + spin_unlock_irqrestore(&priv->lock, flags); + return retval; } /* cypress_serial_control */ -/* given a baud mask, it will return speed on success */ +static void cypress_set_dead(struct usb_serial_port *port) +{ + struct cypress_private *priv = usb_get_serial_port_data(port); + unsigned long flags; + + spin_lock_irqsave(&priv->lock, flags); + if (!priv->comm_is_ok) { + spin_unlock_irqrestore(&priv->lock, flags); + return; + } + priv->comm_is_ok = 0; + spin_unlock_irqrestore(&priv->lock, flags); + + err("cypress_m8 suspending failing port %d - interval might be too short", + port->number); +} + + +/* given a baud mask, it will return integer baud on success */ static int mask_to_rate (unsigned mask) { int rate; @@ -420,14 +495,15 @@ static unsigned rate_to_mask (int rate) static int generic_startup (struct usb_serial *serial) { struct cypress_private *priv; + struct usb_serial_port *port = serial->port[0]; - dbg("%s - port %d", __FUNCTION__, serial->port[0]->number); + dbg("%s - port %d", __FUNCTION__, port->number); - priv = kmalloc(sizeof (struct cypress_private), GFP_KERNEL); + priv = kzalloc(sizeof (struct cypress_private), GFP_KERNEL); if (!priv) return -ENOMEM; - memset(priv, 0x00, sizeof (struct cypress_private)); + priv->comm_is_ok = !0; spin_lock_init(&priv->lock); priv->buf = cypress_buf_alloc(CYPRESS_BUF_SIZE); if (priv->buf == NULL) { @@ -441,12 +517,24 @@ static int generic_startup (struct usb_serial *serial) priv->cmd_ctrl = 0; priv->line_control = 0; priv->termios_initialized = 0; - priv->calledfromopen = 0; priv->rx_flags = 0; - usb_set_serial_port_data(serial->port[0], priv); + priv->cbr_mask = B300; + if (interval > 0) { + priv->write_urb_interval = interval; + priv->read_urb_interval = interval; + dbg("%s - port %d read & write intervals forced to %d", + __FUNCTION__,port->number,interval); + } else { + priv->write_urb_interval = port->interrupt_out_urb->interval; + priv->read_urb_interval = port->interrupt_in_urb->interval; + dbg("%s - port %d intervals: read=%d write=%d", + __FUNCTION__,port->number, + priv->read_urb_interval,priv->write_urb_interval); + } + usb_set_serial_port_data(port, priv); - return (0); -} + return 0; +} static int cypress_earthmate_startup (struct usb_serial *serial) @@ -456,14 +544,15 @@ static int cypress_earthmate_startup (struct usb_serial *serial) dbg("%s", __FUNCTION__); if (generic_startup(serial)) { - dbg("%s - Failed setting up port %d", __FUNCTION__, serial->port[0]->number); + dbg("%s - Failed setting up port %d", __FUNCTION__, + serial->port[0]->number); return 1; } priv = usb_get_serial_port_data(serial->port[0]); priv->chiptype = CT_EARTHMATE; - - return (0); + + return 0; } /* cypress_earthmate_startup */ @@ -474,17 +563,37 @@ static int cypress_hidcom_startup (struct usb_serial *serial) dbg("%s", __FUNCTION__); if (generic_startup(serial)) { - dbg("%s - Failed setting up port %d", __FUNCTION__, serial->port[0]->number); + dbg("%s - Failed setting up port %d", __FUNCTION__, + serial->port[0]->number); return 1; } priv = usb_get_serial_port_data(serial->port[0]); priv->chiptype = CT_CYPHIDCOM; - return (0); + return 0; } /* cypress_hidcom_startup */ +static int cypress_ca42v2_startup (struct usb_serial *serial) +{ + struct cypress_private *priv; + + dbg("%s", __FUNCTION__); + + if (generic_startup(serial)) { + dbg("%s - Failed setting up port %d", __FUNCTION__, + serial->port[0]->number); + return 1; + } + + priv = usb_get_serial_port_data(serial->port[0]); + priv->chiptype = CT_CA42V2; + + return 0; +} /* cypress_ca42v2_startup */ + + static void cypress_shutdown (struct usb_serial *serial) { struct cypress_private *priv; @@ -512,8 +621,10 @@ static int cypress_open (struct usb_serial_port *port, struct file *filp) dbg("%s - port %d", __FUNCTION__, port->number); + if (!priv->comm_is_ok) + return -EIO; + /* clear halts before open */ - usb_clear_halt(serial->dev, 0x00); usb_clear_halt(serial->dev, 0x81); usb_clear_halt(serial->dev, 0x02); @@ -531,7 +642,6 @@ static int cypress_open (struct usb_serial_port *port, struct file *filp) /* raise both lines and set termios */ spin_lock_irqsave(&priv->lock, flags); priv->line_control = CONTROL_DTR | CONTROL_RTS; - priv->calledfromopen = 1; priv->cmd_ctrl = 1; spin_unlock_irqrestore(&priv->lock, flags); result = cypress_write(port, NULL, 0); @@ -553,11 +663,12 @@ static int cypress_open (struct usb_serial_port *port, struct file *filp) usb_fill_int_urb(port->interrupt_in_urb, serial->dev, usb_rcvintpipe(serial->dev, port->interrupt_in_endpointAddress), port->interrupt_in_urb->transfer_buffer, port->interrupt_in_urb->transfer_buffer_length, - cypress_read_int_callback, port, port->interrupt_in_urb->interval); + cypress_read_int_callback, port, priv->read_urb_interval); result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL); if (result){ dev_err(&port->dev, "%s - failed submitting read urb, error %d\n", __FUNCTION__, result); + cypress_set_dead(port); } return result; @@ -602,8 +713,7 @@ static void cypress_close(struct usb_serial_port *port, struct file * filp) timeout = max((HZ*2560)/bps,HZ/10); else timeout = 2*HZ; - set_current_state(TASK_INTERRUPTIBLE); - schedule_timeout(timeout); + schedule_timeout_interruptible(timeout); dbg("%s - stopping urbs", __FUNCTION__); usb_kill_urb (port->interrupt_in_urb); @@ -663,6 +773,9 @@ static void cypress_send(struct usb_serial_port *port) struct cypress_private *priv = usb_get_serial_port_data(port); unsigned long flags; + if (!priv->comm_is_ok) + return; + dbg("%s - port %d", __FUNCTION__, port->number); dbg("%s - interrupt out size is %d", __FUNCTION__, port->interrupt_out_size); @@ -680,12 +793,12 @@ static void cypress_send(struct usb_serial_port *port) spin_lock_irqsave(&priv->lock, flags); switch (port->interrupt_out_size) { case 32: - // this is for the CY7C64013... + /* this is for the CY7C64013... */ offset = 2; port->interrupt_out_buffer[0] = priv->line_control; break; case 8: - // this is for the CY7C63743... + /* this is for the CY7C63743... */ offset = 1; port->interrupt_out_buffer[0] = priv->line_control; break; @@ -736,13 +849,16 @@ send: usb_serial_debug_data(debug, &port->dev, __FUNCTION__, port->interrupt_out_size, port->interrupt_out_urb->transfer_buffer); - port->interrupt_out_urb->transfer_buffer_length = actual_size; - port->interrupt_out_urb->dev = port->serial->dev; + usb_fill_int_urb(port->interrupt_out_urb, port->serial->dev, + usb_sndintpipe(port->serial->dev, port->interrupt_out_endpointAddress), + port->interrupt_out_buffer, port->interrupt_out_size, + cypress_write_int_callback, port, priv->write_urb_interval); result = usb_submit_urb (port->interrupt_out_urb, GFP_ATOMIC); if (result) { dev_err(&port->dev, "%s - failed submitting write urb, error %d\n", __FUNCTION__, result); priv->write_urb_in_use = 0; + cypress_set_dead(port); } spin_lock_irqsave(&priv->lock, flags); @@ -752,7 +868,7 @@ send: priv->bytes_out += count; /* do not count the line control and size bytes */ spin_unlock_irqrestore(&priv->lock, flags); - schedule_work(&port->work); + usb_serial_port_softint(port); } /* cypress_send */ @@ -833,13 +949,13 @@ static int cypress_ioctl (struct usb_serial_port *port, struct file * file, unsi switch (cmd) { case TIOCGSERIAL: - if (copy_to_user((void __user *)arg, port->tty->termios, sizeof(struct termios))) { + if (copy_to_user((void __user *)arg, port->tty->termios, sizeof(struct ktermios))) { return -EFAULT; } return (0); break; case TIOCSSERIAL: - if (copy_from_user(port->tty->termios, (void __user *)arg, sizeof(struct termios))) { + if (copy_from_user(port->tty->termios, (void __user *)arg, sizeof(struct ktermios))) { return -EFAULT; } /* here we need to call cypress_set_termios to invoke the new settings */ @@ -902,7 +1018,8 @@ static int cypress_ioctl (struct usb_serial_port *port, struct file * file, unsi } /* cypress_ioctl */ -static void cypress_set_termios (struct usb_serial_port *port, struct termios *old_termios) +static void cypress_set_termios (struct usb_serial_port *port, + struct ktermios *old_termios) { struct cypress_private *priv = usb_get_serial_port_data(port); struct tty_struct *tty; @@ -910,8 +1027,8 @@ static void cypress_set_termios (struct usb_serial_port *port, struct termios *o unsigned cflag, iflag, baud_mask; unsigned long flags; __u8 oldlines; - int linechange; - + int linechange = 0; + dbg("%s - port %d", __FUNCTION__, port->number); tty = port->tty; @@ -924,10 +1041,16 @@ static void cypress_set_termios (struct usb_serial_port *port, struct termios *o if (!priv->termios_initialized) { if (priv->chiptype == CT_EARTHMATE) { *(tty->termios) = tty_std_termios; - tty->termios->c_cflag = B4800 | CS8 | CREAD | HUPCL | CLOCAL; + tty->termios->c_cflag = B4800 | CS8 | CREAD | HUPCL | + CLOCAL; } else if (priv->chiptype == CT_CYPHIDCOM) { *(tty->termios) = tty_std_termios; - tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL; + tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL | + CLOCAL; + } else if (priv->chiptype == CT_CA42V2) { + *(tty->termios) = tty_std_termios; + tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL | + CLOCAL; } priv->termios_initialized = 1; } @@ -939,12 +1062,15 @@ static void cypress_set_termios (struct usb_serial_port *port, struct termios *o /* check if there are new settings */ if (old_termios) { if ((cflag != old_termios->c_cflag) || - (RELEVANT_IFLAG(iflag) != RELEVANT_IFLAG(old_termios->c_iflag))) { - dbg("%s - attempting to set new termios settings", __FUNCTION__); - /* should make a copy of this in case something goes wrong in the function, we can restore it */ + (RELEVANT_IFLAG(iflag) != + RELEVANT_IFLAG(old_termios->c_iflag))) { + dbg("%s - attempting to set new termios settings", + __FUNCTION__); + /* should make a copy of this in case something goes + * wrong in the function, we can restore it */ spin_lock_irqsave(&priv->lock, flags); priv->tmp_termios = *(tty->termios); - spin_unlock_irqrestore(&priv->lock, flags); + spin_unlock_irqrestore(&priv->lock, flags); } else { dbg("%s - nothing to do, exiting", __FUNCTION__); return; @@ -955,21 +1081,34 @@ static void cypress_set_termios (struct usb_serial_port *port, struct termios *o /* set number of data bits, parity, stop bits */ /* when parity is disabled the parity type bit is ignored */ - stop_bits = cflag & CSTOPB ? 1 : 0; /* 1 means 2 stop bits, 0 means 1 stop bit */ - + /* 1 means 2 stop bits, 0 means 1 stop bit */ + stop_bits = cflag & CSTOPB ? 1 : 0; + if (cflag & PARENB) { parity_enable = 1; - parity_type = cflag & PARODD ? 1 : 0; /* 1 means odd parity, 0 means even parity */ + /* 1 means odd parity, 0 means even parity */ + parity_type = cflag & PARODD ? 1 : 0; } else parity_enable = parity_type = 0; if (cflag & CSIZE) { switch (cflag & CSIZE) { - case CS5: data_bits = 0; break; - case CS6: data_bits = 1; break; - case CS7: data_bits = 2; break; - case CS8: data_bits = 3; break; - default: err("%s - CSIZE was set, but not CS5-CS8", __FUNCTION__); data_bits = 3; + case CS5: + data_bits = 0; + break; + case CS6: + data_bits = 1; + break; + case CS7: + data_bits = 2; + break; + case CS8: + data_bits = 3; + break; + default: + err("%s - CSIZE was set, but not CS5-CS8", + __FUNCTION__); + data_bits = 3; } } else data_bits = 3; @@ -984,94 +1123,98 @@ static void cypress_set_termios (struct usb_serial_port *port, struct termios *o } else { baud_mask = (cflag & CBAUD); switch(baud_mask) { - case B300: dbg("%s - setting baud 300bps", __FUNCTION__); break; - case B600: dbg("%s - setting baud 600bps", __FUNCTION__); break; - case B1200: dbg("%s - setting baud 1200bps", __FUNCTION__); break; - case B2400: dbg("%s - setting baud 2400bps", __FUNCTION__); break; - case B4800: dbg("%s - setting baud 4800bps", __FUNCTION__); break; - case B9600: dbg("%s - setting baud 9600bps", __FUNCTION__); break; - case B19200: dbg("%s - setting baud 19200bps", __FUNCTION__); break; - case B38400: dbg("%s - setting baud 38400bps", __FUNCTION__); break; - case B57600: dbg("%s - setting baud 57600bps", __FUNCTION__); break; - case B115200: dbg("%s - setting baud 115200bps", __FUNCTION__); break; - default: dbg("%s - unknown masked baud rate", __FUNCTION__); - } - priv->line_control |= CONTROL_DTR; - - /* toggle CRTSCTS? - don't do this if being called from cypress_open */ - if (!priv->calledfromopen) { - if (cflag & CRTSCTS) - priv->line_control |= CONTROL_RTS; - else - priv->line_control &= ~CONTROL_RTS; + case B300: + dbg("%s - setting baud 300bps", __FUNCTION__); + break; + case B600: + dbg("%s - setting baud 600bps", __FUNCTION__); + break; + case B1200: + dbg("%s - setting baud 1200bps", __FUNCTION__); + break; + case B2400: + dbg("%s - setting baud 2400bps", __FUNCTION__); + break; + case B4800: + dbg("%s - setting baud 4800bps", __FUNCTION__); + break; + case B9600: + dbg("%s - setting baud 9600bps", __FUNCTION__); + break; + case B19200: + dbg("%s - setting baud 19200bps", __FUNCTION__); + break; + case B38400: + dbg("%s - setting baud 38400bps", __FUNCTION__); + break; + case B57600: + dbg("%s - setting baud 57600bps", __FUNCTION__); + break; + case B115200: + dbg("%s - setting baud 115200bps", __FUNCTION__); + break; + default: + dbg("%s - unknown masked baud rate", __FUNCTION__); } + priv->line_control = (CONTROL_DTR | CONTROL_RTS); } spin_unlock_irqrestore(&priv->lock, flags); - - dbg("%s - sending %d stop_bits, %d parity_enable, %d parity_type, %d data_bits (+5)", __FUNCTION__, - stop_bits, parity_enable, parity_type, data_bits); - cypress_serial_control(port, baud_mask, data_bits, stop_bits, parity_enable, - parity_type, 0, CYPRESS_SET_CONFIG); + dbg("%s - sending %d stop_bits, %d parity_enable, %d parity_type, " + "%d data_bits (+5)", __FUNCTION__, stop_bits, + parity_enable, parity_type, data_bits); - msleep(50); /* give some time between change and read (50ms) */ + cypress_serial_control(port, baud_mask, data_bits, stop_bits, + parity_enable, parity_type, 0, CYPRESS_SET_CONFIG); - /* we perform a CYPRESS_GET_CONFIG so that the current settings are filled into the private structure - * this should confirm that all is working if it returns what we just set */ + /* we perform a CYPRESS_GET_CONFIG so that the current settings are + * filled into the private structure this should confirm that all is + * working if it returns what we just set */ cypress_serial_control(port, 0, 0, 0, 0, 0, 0, CYPRESS_GET_CONFIG); - /* Here we can define custom tty settings for devices - * - * the main tty termios flag base comes from empeg.c - */ + /* Here we can define custom tty settings for devices; the main tty + * termios flag base comes from empeg.c */ - spin_lock_irqsave(&priv->lock, flags); + spin_lock_irqsave(&priv->lock, flags); if ( (priv->chiptype == CT_EARTHMATE) && (priv->baud_rate == 4800) ) { - - dbg("Using custom termios settings for a baud rate of 4800bps."); + dbg("Using custom termios settings for a baud rate of " + "4800bps."); /* define custom termios settings for NMEA protocol */ - tty->termios->c_iflag /* input modes - */ - &= ~(IGNBRK /* disable ignore break */ - | BRKINT /* disable break causes interrupt */ - | PARMRK /* disable mark parity errors */ - | ISTRIP /* disable clear high bit of input characters */ - | INLCR /* disable translate NL to CR */ - | IGNCR /* disable ignore CR */ - | ICRNL /* disable translate CR to NL */ - | IXON); /* disable enable XON/XOFF flow control */ - - tty->termios->c_oflag /* output modes */ - &= ~OPOST; /* disable postprocess output characters */ - - tty->termios->c_lflag /* line discipline modes */ - &= ~(ECHO /* disable echo input characters */ - | ECHONL /* disable echo new line */ - | ICANON /* disable erase, kill, werase, and rprnt special characters */ - | ISIG /* disable interrupt, quit, and suspend special characters */ - | IEXTEN); /* disable non-POSIX special characters */ + &= ~(IGNBRK /* disable ignore break */ + | BRKINT /* disable break causes interrupt */ + | PARMRK /* disable mark parity errors */ + | ISTRIP /* disable clear high bit of input char */ + | INLCR /* disable translate NL to CR */ + | IGNCR /* disable ignore CR */ + | ICRNL /* disable translate CR to NL */ + | IXON); /* disable enable XON/XOFF flow control */ - } else if (priv->chiptype == CT_CYPHIDCOM) { + tty->termios->c_oflag /* output modes */ + &= ~OPOST; /* disable postprocess output char */ - // Software app handling it for device... + tty->termios->c_lflag /* line discipline modes */ + &= ~(ECHO /* disable echo input characters */ + | ECHONL /* disable echo new line */ + | ICANON /* disable erase, kill, werase, and rprnt + special characters */ + | ISIG /* disable interrupt, quit, and suspend + special characters */ + | IEXTEN); /* disable non-POSIX special characters */ + } /* CT_CYPHIDCOM: Application should handle this for device */ - } linechange = (priv->line_control != oldlines); spin_unlock_irqrestore(&priv->lock, flags); /* if necessary, set lines */ - if (!priv->calledfromopen && linechange) { + if (linechange) { priv->cmd_ctrl = 1; cypress_write(port, NULL, 0); } - - if (priv->calledfromopen) - priv->calledfromopen = 0; - } /* cypress_set_termios */ - + /* returns amount of data still left in soft buffer */ static int cypress_chars_in_buffer(struct usb_serial_port *port) { @@ -1099,7 +1242,7 @@ static void cypress_throttle (struct usb_serial_port *port) spin_lock_irqsave(&priv->lock, flags); priv->rx_flags = THROTTLED; - spin_unlock_irqrestore(&priv->lock, flags); + spin_unlock_irqrestore(&priv->lock, flags); } @@ -1116,24 +1259,30 @@ static void cypress_unthrottle (struct usb_serial_port *port) priv->rx_flags = 0; spin_unlock_irqrestore(&priv->lock, flags); + if (!priv->comm_is_ok) + return; + if (actually_throttled) { port->interrupt_in_urb->dev = port->serial->dev; result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC); - if (result) - dev_err(&port->dev, "%s - failed submitting read urb, error %d\n", __FUNCTION__, result); + if (result) { + dev_err(&port->dev, "%s - failed submitting read urb, " + "error %d\n", __FUNCTION__, result); + cypress_set_dead(port); + } } } -static void cypress_read_int_callback(struct urb *urb, struct pt_regs *regs) +static void cypress_read_int_callback(struct urb *urb) { struct usb_serial_port *port = (struct usb_serial_port *)urb->context; struct cypress_private *priv = usb_get_serial_port_data(port); struct tty_struct *tty; unsigned char *data = urb->transfer_buffer; unsigned long flags; - char tty_flag = TTY_NORMAL; + char tty_flag = TTY_NORMAL; int havedata = 0; int bytes = 0; int result; @@ -1141,8 +1290,22 @@ static void cypress_read_int_callback(struct urb *urb, struct pt_regs *regs) dbg("%s - port %d", __FUNCTION__, port->number); - if (urb->status) { - dbg("%s - nonzero read status received: %d", __FUNCTION__, urb->status); + switch (urb->status) { + case 0: /* success */ + break; + case -ECONNRESET: + case -ENOENT: + case -ESHUTDOWN: + /* precursor to disconnect so just go away */ + return; + case -EPIPE: + usb_clear_halt(port->serial->dev,0x81); + break; + default: + /* something ugly is going on... */ + dev_err(&urb->dev->dev,"%s - unexpected nonzero read status received: %d\n", + __FUNCTION__,urb->status); + cypress_set_dead(port); return; } @@ -1164,53 +1327,57 @@ static void cypress_read_int_callback(struct urb *urb, struct pt_regs *regs) spin_lock_irqsave(&priv->lock, flags); switch(urb->actual_length) { case 32: - // This is for the CY7C64013... + /* This is for the CY7C64013... */ priv->current_status = data[0] & 0xF8; - bytes = data[1]+2; - i=2; + bytes = data[1] + 2; + i = 2; if (bytes > 2) havedata = 1; break; case 8: - // This is for the CY7C63743... + /* This is for the CY7C63743... */ priv->current_status = data[0] & 0xF8; - bytes = (data[0] & 0x07)+1; - i=1; + bytes = (data[0] & 0x07) + 1; + i = 1; if (bytes > 1) havedata = 1; break; default: - dbg("%s - wrong packet size - received %d bytes", __FUNCTION__, urb->actual_length); + dbg("%s - wrong packet size - received %d bytes", + __FUNCTION__, urb->actual_length); spin_unlock_irqrestore(&priv->lock, flags); goto continue_read; } spin_unlock_irqrestore(&priv->lock, flags); - usb_serial_debug_data (debug, &port->dev, __FUNCTION__, urb->actual_length, data); + usb_serial_debug_data (debug, &port->dev, __FUNCTION__, + urb->actual_length, data); spin_lock_irqsave(&priv->lock, flags); /* check to see if status has changed */ if (priv != NULL) { if (priv->current_status != priv->prev_status) { - priv->diff_status |= priv->current_status ^ priv->prev_status; + priv->diff_status |= priv->current_status ^ + priv->prev_status; wake_up_interruptible(&priv->delta_msr_wait); priv->prev_status = priv->current_status; } } - spin_unlock_irqrestore(&priv->lock, flags); + spin_unlock_irqrestore(&priv->lock, flags); - /* hangup, as defined in acm.c... this might be a bad place for it though */ - if (tty && !(tty->termios->c_cflag & CLOCAL) && !(priv->current_status & UART_CD)) { + /* hangup, as defined in acm.c... this might be a bad place for it + * though */ + if (tty && !(tty->termios->c_cflag & CLOCAL) && + !(priv->current_status & UART_CD)) { dbg("%s - calling hangup", __FUNCTION__); tty_hangup(tty); goto continue_read; } - /* There is one error bit... I'm assuming it is a parity error indicator - * as the generic firmware will set this bit to 1 if a parity error occurs. - * I can not find reference to any other error events. - * - */ + /* There is one error bit... I'm assuming it is a parity error + * indicator as the generic firmware will set this bit to 1 if a + * parity error occurs. + * I can not find reference to any other error events. */ spin_lock_irqsave(&priv->lock, flags); if (priv->current_status & CYP_ERROR) { spin_unlock_irqrestore(&priv->lock, flags); @@ -1221,41 +1388,45 @@ static void cypress_read_int_callback(struct urb *urb, struct pt_regs *regs) /* process read if there is data other than line status */ if (tty && (bytes > i)) { + bytes = tty_buffer_request_room(tty, bytes); for (; i < bytes ; ++i) { - dbg("pushing byte number %d - %d - %c",i,data[i],data[i]); - if(tty->flip.count >= TTY_FLIPBUF_SIZE) { - tty_flip_buffer_push(tty); - } + dbg("pushing byte number %d - %d - %c", i, data[i], + data[i]); tty_insert_flip_char(tty, data[i], tty_flag); } tty_flip_buffer_push(port->tty); } spin_lock_irqsave(&priv->lock, flags); - priv->bytes_in += bytes; /* control and status byte(s) are also counted */ + /* control and status byte(s) are also counted */ + priv->bytes_in += bytes; spin_unlock_irqrestore(&priv->lock, flags); continue_read: - - /* Continue trying to always read... unless the port has closed. */ - - if (port->open_count > 0) { - usb_fill_int_urb(port->interrupt_in_urb, port->serial->dev, - usb_rcvintpipe(port->serial->dev, port->interrupt_in_endpointAddress), - port->interrupt_in_urb->transfer_buffer, - port->interrupt_in_urb->transfer_buffer_length, - cypress_read_int_callback, port, - port->interrupt_in_urb->interval); - result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC); - if (result) - dev_err(&urb->dev->dev, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__, result); + + /* Continue trying to always read... unless the port has closed. */ + + if (port->open_count > 0 && priv->comm_is_ok) { + usb_fill_int_urb(port->interrupt_in_urb, port->serial->dev, + usb_rcvintpipe(port->serial->dev, + port->interrupt_in_endpointAddress), + port->interrupt_in_urb->transfer_buffer, + port->interrupt_in_urb->transfer_buffer_length, + cypress_read_int_callback, port, priv->read_urb_interval); + result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC); + if (result) { + dev_err(&urb->dev->dev, "%s - failed resubmitting " + "read urb, error %d\n", __FUNCTION__, + result); + cypress_set_dead(port); + } } - + return; } /* cypress_read_int_callback */ -static void cypress_write_int_callback(struct urb *urb, struct pt_regs *regs) +static void cypress_write_int_callback(struct urb *urb) { struct usb_serial_port *port = (struct usb_serial_port *)urb->context; struct cypress_private *priv = usb_get_serial_port_data(port); @@ -1274,18 +1445,26 @@ static void cypress_write_int_callback(struct urb *urb, struct pt_regs *regs) dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status); priv->write_urb_in_use = 0; return; - default: + case -EPIPE: /* no break needed; clear halt and resubmit */ + if (!priv->comm_is_ok) + break; + usb_clear_halt(port->serial->dev, 0x02); /* error in the urb, so we have to resubmit it */ - dbg("%s - Overflow in write", __FUNCTION__); dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status); port->interrupt_out_urb->transfer_buffer_length = 1; port->interrupt_out_urb->dev = port->serial->dev; result = usb_submit_urb(port->interrupt_out_urb, GFP_ATOMIC); - if (result) - dev_err(&urb->dev->dev, "%s - failed resubmitting write urb, error %d\n", - __FUNCTION__, result); - else + if (!result) return; + dev_err(&urb->dev->dev, "%s - failed resubmitting write urb, error %d\n", + __FUNCTION__, result); + cypress_set_dead(port); + break; + default: + dev_err(&urb->dev->dev,"%s - unexpected nonzero write status received: %d\n", + __FUNCTION__,urb->status); + cypress_set_dead(port); + break; } priv->write_urb_in_use = 0; @@ -1340,9 +1519,8 @@ static struct cypress_buf *cypress_buf_alloc(unsigned int size) static void cypress_buf_free(struct cypress_buf *cb) { - if (cb != NULL) { - if (cb->buf_buf != NULL) - kfree(cb->buf_buf); + if (cb) { + kfree(cb->buf_buf); kfree(cb); } } @@ -1497,19 +1675,23 @@ static int __init cypress_init(void) retval = usb_serial_register(&cypress_hidcom_device); if (retval) goto failed_hidcom_register; + retval = usb_serial_register(&cypress_ca42v2_device); + if (retval) + goto failed_ca42v2_register; retval = usb_register(&cypress_driver); if (retval) goto failed_usb_register; info(DRIVER_DESC " " DRIVER_VERSION); return 0; + failed_usb_register: - usb_deregister(&cypress_driver); -failed_hidcom_register: + usb_serial_deregister(&cypress_ca42v2_device); +failed_ca42v2_register: usb_serial_deregister(&cypress_hidcom_device); -failed_em_register: +failed_hidcom_register: usb_serial_deregister(&cypress_earthmate_device); - +failed_em_register: return retval; } @@ -1521,6 +1703,7 @@ static void __exit cypress_exit (void) usb_deregister (&cypress_driver); usb_serial_deregister (&cypress_earthmate_device); usb_serial_deregister (&cypress_hidcom_device); + usb_serial_deregister (&cypress_ca42v2_device); } @@ -1536,3 +1719,5 @@ module_param(debug, bool, S_IRUGO | S_IWUSR); MODULE_PARM_DESC(debug, "Debug enabled or not"); module_param(stats, bool, S_IRUGO | S_IWUSR); MODULE_PARM_DESC(stats, "Enable statistics or not"); +module_param(interval, int, S_IRUGO | S_IWUSR); +MODULE_PARM_DESC(interval, "Overrides interrupt interval");