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