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