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