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