USB: cypress_m8: add endpoint sanity check
[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         if (!port->interrupt_out_urb || !port->interrupt_in_urb) {
493                 dev_err(&port->dev, "required endpoint is missing\n");
494                 return -ENODEV;
495         }
496
497         priv = kzalloc(sizeof(struct cypress_private), GFP_KERNEL);
498         if (!priv)
499                 return -ENOMEM;
500
501         priv->comm_is_ok = !0;
502         spin_lock_init(&priv->lock);
503         if (kfifo_alloc(&priv->write_fifo, CYPRESS_BUF_SIZE, GFP_KERNEL)) {
504                 kfree(priv);
505                 return -ENOMEM;
506         }
507
508         /* Skip reset for FRWD device. It is a workaound:
509            device hangs if it receives SET_CONFIGURE in Configured
510            state. */
511         if (!is_frwd(serial->dev))
512                 usb_reset_configuration(serial->dev);
513
514         priv->cmd_ctrl = 0;
515         priv->line_control = 0;
516         priv->termios_initialized = 0;
517         priv->rx_flags = 0;
518         /* Default packet format setting is determined by packet size.
519            Anything with a size larger then 9 must have a separate
520            count field since the 3 bit count field is otherwise too
521            small.  Otherwise we can use the slightly more compact
522            format.  This is in accordance with the cypress_m8 serial
523            converter app note. */
524         if (port->interrupt_out_size > 9)
525                 priv->pkt_fmt = packet_format_1;
526         else
527                 priv->pkt_fmt = packet_format_2;
528
529         if (interval > 0) {
530                 priv->write_urb_interval = interval;
531                 priv->read_urb_interval = interval;
532                 dbg("%s - port %d read & write intervals forced to %d",
533                     __func__, port->number, interval);
534         } else {
535                 priv->write_urb_interval = port->interrupt_out_urb->interval;
536                 priv->read_urb_interval = port->interrupt_in_urb->interval;
537                 dbg("%s - port %d intervals: read=%d write=%d",
538                     __func__, port->number,
539                     priv->read_urb_interval, priv->write_urb_interval);
540         }
541         usb_set_serial_port_data(port, priv);
542
543         return 0;
544 }
545
546
547 static int cypress_earthmate_startup(struct usb_serial *serial)
548 {
549         struct cypress_private *priv;
550         struct usb_serial_port *port = serial->port[0];
551
552         dbg("%s", __func__);
553
554         if (generic_startup(serial)) {
555                 dbg("%s - Failed setting up port %d", __func__,
556                                 port->number);
557                 return 1;
558         }
559
560         priv = usb_get_serial_port_data(port);
561         priv->chiptype = CT_EARTHMATE;
562         /* All Earthmate devices use the separated-count packet
563            format!  Idiotic. */
564         priv->pkt_fmt = packet_format_1;
565         if (serial->dev->descriptor.idProduct !=
566                                 cpu_to_le16(PRODUCT_ID_EARTHMATEUSB)) {
567                 /* The old original USB Earthmate seemed able to
568                    handle GET_CONFIG requests; everything they've
569                    produced since that time crashes if this command is
570                    attempted :-( */
571                 dbg("%s - Marking this device as unsafe for GET_CONFIG "
572                     "commands", __func__);
573                 priv->get_cfg_unsafe = !0;
574         }
575
576         return 0;
577 } /* cypress_earthmate_startup */
578
579
580 static int cypress_hidcom_startup(struct usb_serial *serial)
581 {
582         struct cypress_private *priv;
583
584         dbg("%s", __func__);
585
586         if (generic_startup(serial)) {
587                 dbg("%s - Failed setting up port %d", __func__,
588                                 serial->port[0]->number);
589                 return 1;
590         }
591
592         priv = usb_get_serial_port_data(serial->port[0]);
593         priv->chiptype = CT_CYPHIDCOM;
594
595         return 0;
596 } /* cypress_hidcom_startup */
597
598
599 static int cypress_ca42v2_startup(struct usb_serial *serial)
600 {
601         struct cypress_private *priv;
602
603         dbg("%s", __func__);
604
605         if (generic_startup(serial)) {
606                 dbg("%s - Failed setting up port %d", __func__,
607                                 serial->port[0]->number);
608                 return 1;
609         }
610
611         priv = usb_get_serial_port_data(serial->port[0]);
612         priv->chiptype = CT_CA42V2;
613
614         return 0;
615 } /* cypress_ca42v2_startup */
616
617
618 static void cypress_release(struct usb_serial *serial)
619 {
620         struct cypress_private *priv;
621
622         dbg("%s - port %d", __func__, serial->port[0]->number);
623
624         /* all open ports are closed at this point */
625
626         priv = usb_get_serial_port_data(serial->port[0]);
627
628         if (priv) {
629                 kfifo_free(&priv->write_fifo);
630                 kfree(priv);
631         }
632 }
633
634
635 static int cypress_open(struct tty_struct *tty, struct usb_serial_port *port)
636 {
637         struct cypress_private *priv = usb_get_serial_port_data(port);
638         struct usb_serial *serial = port->serial;
639         unsigned long flags;
640         int result = 0;
641
642         dbg("%s - port %d", __func__, port->number);
643
644         if (!priv->comm_is_ok)
645                 return -EIO;
646
647         /* clear halts before open */
648         usb_clear_halt(serial->dev, 0x81);
649         usb_clear_halt(serial->dev, 0x02);
650
651         spin_lock_irqsave(&priv->lock, flags);
652         /* reset read/write statistics */
653         priv->bytes_in = 0;
654         priv->bytes_out = 0;
655         priv->cmd_count = 0;
656         priv->rx_flags = 0;
657         spin_unlock_irqrestore(&priv->lock, flags);
658
659         /* Set termios */
660         cypress_send(port);
661
662         if (tty)
663                 cypress_set_termios(tty, port, &priv->tmp_termios);
664
665         /* setup the port and start reading from the device */
666         usb_fill_int_urb(port->interrupt_in_urb, serial->dev,
667                 usb_rcvintpipe(serial->dev, port->interrupt_in_endpointAddress),
668                 port->interrupt_in_urb->transfer_buffer,
669                 port->interrupt_in_urb->transfer_buffer_length,
670                 cypress_read_int_callback, port, priv->read_urb_interval);
671         result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
672
673         if (result) {
674                 dev_err(&port->dev,
675                         "%s - failed submitting read urb, error %d\n",
676                                                         __func__, result);
677                 cypress_set_dead(port);
678         }
679         port->port.drain_delay = 256;
680         return result;
681 } /* cypress_open */
682
683 static void cypress_dtr_rts(struct usb_serial_port *port, int on)
684 {
685         struct cypress_private *priv = usb_get_serial_port_data(port);
686         /* drop dtr and rts */
687         spin_lock_irq(&priv->lock);
688         if (on == 0)
689                 priv->line_control = 0;
690         else 
691                 priv->line_control = CONTROL_DTR | CONTROL_RTS;
692         priv->cmd_ctrl = 1;
693         spin_unlock_irq(&priv->lock);
694         cypress_write(NULL, port, NULL, 0);
695 }
696
697 static void cypress_close(struct usb_serial_port *port)
698 {
699         struct cypress_private *priv = usb_get_serial_port_data(port);
700         unsigned long flags;
701
702         dbg("%s - port %d", __func__, port->number);
703
704         /* writing is potentially harmful, lock must be taken */
705         mutex_lock(&port->serial->disc_mutex);
706         if (port->serial->disconnected) {
707                 mutex_unlock(&port->serial->disc_mutex);
708                 return;
709         }
710         spin_lock_irqsave(&priv->lock, flags);
711         kfifo_reset_out(&priv->write_fifo);
712         spin_unlock_irqrestore(&priv->lock, flags);
713
714         dbg("%s - stopping urbs", __func__);
715         usb_kill_urb(port->interrupt_in_urb);
716         usb_kill_urb(port->interrupt_out_urb);
717
718         if (stats)
719                 dev_info(&port->dev, "Statistics: %d Bytes In | %d Bytes Out | %d Commands Issued\n",
720                         priv->bytes_in, priv->bytes_out, priv->cmd_count);
721         mutex_unlock(&port->serial->disc_mutex);
722 } /* cypress_close */
723
724
725 static int cypress_write(struct tty_struct *tty, struct usb_serial_port *port,
726                                         const unsigned char *buf, int count)
727 {
728         struct cypress_private *priv = usb_get_serial_port_data(port);
729
730         dbg("%s - port %d, %d bytes", __func__, port->number, count);
731
732         /* line control commands, which need to be executed immediately,
733            are not put into the buffer for obvious reasons.
734          */
735         if (priv->cmd_ctrl) {
736                 count = 0;
737                 goto finish;
738         }
739
740         if (!count)
741                 return count;
742
743         count = kfifo_in_locked(&priv->write_fifo, buf, count, &priv->lock);
744
745 finish:
746         cypress_send(port);
747
748         return count;
749 } /* cypress_write */
750
751
752 static void cypress_send(struct usb_serial_port *port)
753 {
754         int count = 0, result, offset, actual_size;
755         struct cypress_private *priv = usb_get_serial_port_data(port);
756         unsigned long flags;
757
758         if (!priv->comm_is_ok)
759                 return;
760
761         dbg("%s - port %d", __func__, port->number);
762         dbg("%s - interrupt out size is %d", __func__,
763                                                 port->interrupt_out_size);
764
765         spin_lock_irqsave(&priv->lock, flags);
766         if (priv->write_urb_in_use) {
767                 dbg("%s - can't write, urb in use", __func__);
768                 spin_unlock_irqrestore(&priv->lock, flags);
769                 return;
770         }
771         spin_unlock_irqrestore(&priv->lock, flags);
772
773         /* clear buffer */
774         memset(port->interrupt_out_urb->transfer_buffer, 0,
775                                                 port->interrupt_out_size);
776
777         spin_lock_irqsave(&priv->lock, flags);
778         switch (priv->pkt_fmt) {
779         default:
780         case packet_format_1:
781                 /* this is for the CY7C64013... */
782                 offset = 2;
783                 port->interrupt_out_buffer[0] = priv->line_control;
784                 break;
785         case packet_format_2:
786                 /* this is for the CY7C63743... */
787                 offset = 1;
788                 port->interrupt_out_buffer[0] = priv->line_control;
789                 break;
790         }
791
792         if (priv->line_control & CONTROL_RESET)
793                 priv->line_control &= ~CONTROL_RESET;
794
795         if (priv->cmd_ctrl) {
796                 priv->cmd_count++;
797                 dbg("%s - line control command being issued", __func__);
798                 spin_unlock_irqrestore(&priv->lock, flags);
799                 goto send;
800         } else
801                 spin_unlock_irqrestore(&priv->lock, flags);
802
803         count = kfifo_out_locked(&priv->write_fifo,
804                                         &port->interrupt_out_buffer[offset],
805                                         port->interrupt_out_size - offset,
806                                         &priv->lock);
807         if (count == 0)
808                 return;
809
810         switch (priv->pkt_fmt) {
811         default:
812         case packet_format_1:
813                 port->interrupt_out_buffer[1] = count;
814                 break;
815         case packet_format_2:
816                 port->interrupt_out_buffer[0] |= count;
817         }
818
819         dbg("%s - count is %d", __func__, count);
820
821 send:
822         spin_lock_irqsave(&priv->lock, flags);
823         priv->write_urb_in_use = 1;
824         spin_unlock_irqrestore(&priv->lock, flags);
825
826         if (priv->cmd_ctrl)
827                 actual_size = 1;
828         else
829                 actual_size = count +
830                               (priv->pkt_fmt == packet_format_1 ? 2 : 1);
831
832         usb_serial_debug_data(debug, &port->dev, __func__,
833                 port->interrupt_out_size,
834                 port->interrupt_out_urb->transfer_buffer);
835
836         usb_fill_int_urb(port->interrupt_out_urb, port->serial->dev,
837                 usb_sndintpipe(port->serial->dev, port->interrupt_out_endpointAddress),
838                 port->interrupt_out_buffer, port->interrupt_out_size,
839                 cypress_write_int_callback, port, priv->write_urb_interval);
840         result = usb_submit_urb(port->interrupt_out_urb, GFP_ATOMIC);
841         if (result) {
842                 dev_err(&port->dev,
843                                 "%s - failed submitting write urb, error %d\n",
844                                                         __func__, result);
845                 priv->write_urb_in_use = 0;
846                 cypress_set_dead(port);
847         }
848
849         spin_lock_irqsave(&priv->lock, flags);
850         if (priv->cmd_ctrl)
851                 priv->cmd_ctrl = 0;
852
853         /* do not count the line control and size bytes */
854         priv->bytes_out += count;
855         spin_unlock_irqrestore(&priv->lock, flags);
856
857         usb_serial_port_softint(port);
858 } /* cypress_send */
859
860
861 /* returns how much space is available in the soft buffer */
862 static int cypress_write_room(struct tty_struct *tty)
863 {
864         struct usb_serial_port *port = tty->driver_data;
865         struct cypress_private *priv = usb_get_serial_port_data(port);
866         int room = 0;
867         unsigned long flags;
868
869         dbg("%s - port %d", __func__, port->number);
870
871         spin_lock_irqsave(&priv->lock, flags);
872         room = kfifo_avail(&priv->write_fifo);
873         spin_unlock_irqrestore(&priv->lock, flags);
874
875         dbg("%s - returns %d", __func__, room);
876         return room;
877 }
878
879
880 static int cypress_tiocmget(struct tty_struct *tty)
881 {
882         struct usb_serial_port *port = tty->driver_data;
883         struct cypress_private *priv = usb_get_serial_port_data(port);
884         __u8 status, control;
885         unsigned int result = 0;
886         unsigned long flags;
887
888         dbg("%s - port %d", __func__, port->number);
889
890         spin_lock_irqsave(&priv->lock, flags);
891         control = priv->line_control;
892         status = priv->current_status;
893         spin_unlock_irqrestore(&priv->lock, flags);
894
895         result = ((control & CONTROL_DTR)        ? TIOCM_DTR : 0)
896                 | ((control & CONTROL_RTS)       ? TIOCM_RTS : 0)
897                 | ((status & UART_CTS)        ? TIOCM_CTS : 0)
898                 | ((status & UART_DSR)        ? TIOCM_DSR : 0)
899                 | ((status & UART_RI)         ? TIOCM_RI  : 0)
900                 | ((status & UART_CD)         ? TIOCM_CD  : 0);
901
902         dbg("%s - result = %x", __func__, result);
903
904         return result;
905 }
906
907
908 static int cypress_tiocmset(struct tty_struct *tty,
909                                unsigned int set, unsigned int clear)
910 {
911         struct usb_serial_port *port = tty->driver_data;
912         struct cypress_private *priv = usb_get_serial_port_data(port);
913         unsigned long flags;
914
915         dbg("%s - port %d", __func__, port->number);
916
917         spin_lock_irqsave(&priv->lock, flags);
918         if (set & TIOCM_RTS)
919                 priv->line_control |= CONTROL_RTS;
920         if (set & TIOCM_DTR)
921                 priv->line_control |= CONTROL_DTR;
922         if (clear & TIOCM_RTS)
923                 priv->line_control &= ~CONTROL_RTS;
924         if (clear & TIOCM_DTR)
925                 priv->line_control &= ~CONTROL_DTR;
926         priv->cmd_ctrl = 1;
927         spin_unlock_irqrestore(&priv->lock, flags);
928
929         return cypress_write(tty, port, NULL, 0);
930 }
931
932
933 static int cypress_ioctl(struct tty_struct *tty,
934                                         unsigned int cmd, unsigned long arg)
935 {
936         struct usb_serial_port *port = tty->driver_data;
937         struct cypress_private *priv = usb_get_serial_port_data(port);
938
939         dbg("%s - port %d, cmd 0x%.4x", __func__, port->number, cmd);
940
941         switch (cmd) {
942         /* This code comes from drivers/char/serial.c and ftdi_sio.c */
943         case TIOCMIWAIT:
944                 for (;;) {
945                         interruptible_sleep_on(&port->delta_msr_wait);
946                         /* see if a signal did it */
947                         if (signal_pending(current))
948                                 return -ERESTARTSYS;
949
950                         if (port->serial->disconnected)
951                                 return -EIO;
952
953                         {
954                                 char diff = priv->diff_status;
955                                 if (diff == 0)
956                                         return -EIO; /* no change => error */
957
958                                 /* consume all events */
959                                 priv->diff_status = 0;
960
961                                 /* return 0 if caller wanted to know about
962                                    these bits */
963                                 if (((arg & TIOCM_RNG) && (diff & UART_RI)) ||
964                                     ((arg & TIOCM_DSR) && (diff & UART_DSR)) ||
965                                     ((arg & TIOCM_CD) && (diff & UART_CD)) ||
966                                     ((arg & TIOCM_CTS) && (diff & UART_CTS)))
967                                         return 0;
968                                 /* otherwise caller can't care less about what
969                                  * happened, and so we continue to wait for
970                                  * more events.
971                                  */
972                         }
973                 }
974                 return 0;
975         default:
976                 break;
977         }
978         dbg("%s - arg not supported - it was 0x%04x - check include/asm/ioctls.h", __func__, cmd);
979         return -ENOIOCTLCMD;
980 } /* cypress_ioctl */
981
982
983 static void cypress_set_termios(struct tty_struct *tty,
984         struct usb_serial_port *port, struct ktermios *old_termios)
985 {
986         struct cypress_private *priv = usb_get_serial_port_data(port);
987         int data_bits, stop_bits, parity_type, parity_enable;
988         unsigned cflag, iflag;
989         unsigned long flags;
990         __u8 oldlines;
991         int linechange = 0;
992
993         dbg("%s - port %d", __func__, port->number);
994
995         spin_lock_irqsave(&priv->lock, flags);
996         /* We can't clean this one up as we don't know the device type
997            early enough */
998         if (!priv->termios_initialized) {
999                 if (priv->chiptype == CT_EARTHMATE) {
1000                         *(tty->termios) = tty_std_termios;
1001                         tty->termios->c_cflag = B4800 | CS8 | CREAD | HUPCL |
1002                                 CLOCAL;
1003                         tty->termios->c_ispeed = 4800;
1004                         tty->termios->c_ospeed = 4800;
1005                 } else if (priv->chiptype == CT_CYPHIDCOM) {
1006                         *(tty->termios) = tty_std_termios;
1007                         tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
1008                                 CLOCAL;
1009                         tty->termios->c_ispeed = 9600;
1010                         tty->termios->c_ospeed = 9600;
1011                 } else if (priv->chiptype == CT_CA42V2) {
1012                         *(tty->termios) = tty_std_termios;
1013                         tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
1014                                 CLOCAL;
1015                         tty->termios->c_ispeed = 9600;
1016                         tty->termios->c_ospeed = 9600;
1017                 }
1018                 priv->termios_initialized = 1;
1019         }
1020         spin_unlock_irqrestore(&priv->lock, flags);
1021
1022         /* Unsupported features need clearing */
1023         tty->termios->c_cflag &= ~(CMSPAR|CRTSCTS);
1024
1025         cflag = tty->termios->c_cflag;
1026         iflag = tty->termios->c_iflag;
1027
1028         /* check if there are new settings */
1029         if (old_termios) {
1030                 spin_lock_irqsave(&priv->lock, flags);
1031                 priv->tmp_termios = *(tty->termios);
1032                 spin_unlock_irqrestore(&priv->lock, flags);
1033         }
1034
1035         /* set number of data bits, parity, stop bits */
1036         /* when parity is disabled the parity type bit is ignored */
1037
1038         /* 1 means 2 stop bits, 0 means 1 stop bit */
1039         stop_bits = cflag & CSTOPB ? 1 : 0;
1040
1041         if (cflag & PARENB) {
1042                 parity_enable = 1;
1043                 /* 1 means odd parity, 0 means even parity */
1044                 parity_type = cflag & PARODD ? 1 : 0;
1045         } else
1046                 parity_enable = parity_type = 0;
1047
1048         switch (cflag & CSIZE) {
1049         case CS5:
1050                 data_bits = 0;
1051                 break;
1052         case CS6:
1053                 data_bits = 1;
1054                 break;
1055         case CS7:
1056                 data_bits = 2;
1057                 break;
1058         case CS8:
1059                 data_bits = 3;
1060                 break;
1061         default:
1062                 dev_err(&port->dev, "%s - CSIZE was set, but not CS5-CS8\n",
1063                         __func__);
1064                 data_bits = 3;
1065         }
1066         spin_lock_irqsave(&priv->lock, flags);
1067         oldlines = priv->line_control;
1068         if ((cflag & CBAUD) == B0) {
1069                 /* drop dtr and rts */
1070                 dbg("%s - dropping the lines, baud rate 0bps", __func__);
1071                 priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
1072         } else
1073                 priv->line_control = (CONTROL_DTR | CONTROL_RTS);
1074         spin_unlock_irqrestore(&priv->lock, flags);
1075
1076         dbg("%s - sending %d stop_bits, %d parity_enable, %d parity_type, "
1077                         "%d data_bits (+5)", __func__, stop_bits,
1078                         parity_enable, parity_type, data_bits);
1079
1080         cypress_serial_control(tty, port, tty_get_baud_rate(tty),
1081                         data_bits, stop_bits,
1082                         parity_enable, parity_type,
1083                         0, CYPRESS_SET_CONFIG);
1084
1085         /* we perform a CYPRESS_GET_CONFIG so that the current settings are
1086          * filled into the private structure this should confirm that all is
1087          * working if it returns what we just set */
1088         cypress_serial_control(tty, port, 0, 0, 0, 0, 0, 0, CYPRESS_GET_CONFIG);
1089
1090         /* Here we can define custom tty settings for devices; the main tty
1091          * termios flag base comes from empeg.c */
1092
1093         spin_lock_irqsave(&priv->lock, flags);
1094         if (priv->chiptype == CT_EARTHMATE && priv->baud_rate == 4800) {
1095                 dbg("Using custom termios settings for a baud rate of "
1096                                 "4800bps.");
1097                 /* define custom termios settings for NMEA protocol */
1098
1099                 tty->termios->c_iflag /* input modes - */
1100                         &= ~(IGNBRK  /* disable ignore break */
1101                         | BRKINT     /* disable break causes interrupt */
1102                         | PARMRK     /* disable mark parity errors */
1103                         | ISTRIP     /* disable clear high bit of input char */
1104                         | INLCR      /* disable translate NL to CR */
1105                         | IGNCR      /* disable ignore CR */
1106                         | ICRNL      /* disable translate CR to NL */
1107                         | IXON);     /* disable enable XON/XOFF flow control */
1108
1109                 tty->termios->c_oflag /* output modes */
1110                         &= ~OPOST;    /* disable postprocess output char */
1111
1112                 tty->termios->c_lflag /* line discipline modes */
1113                         &= ~(ECHO     /* disable echo input characters */
1114                         | ECHONL      /* disable echo new line */
1115                         | ICANON      /* disable erase, kill, werase, and rprnt
1116                                          special characters */
1117                         | ISIG        /* disable interrupt, quit, and suspend
1118                                          special characters */
1119                         | IEXTEN);    /* disable non-POSIX special characters */
1120         } /* CT_CYPHIDCOM: Application should handle this for device */
1121
1122         linechange = (priv->line_control != oldlines);
1123         spin_unlock_irqrestore(&priv->lock, flags);
1124
1125         /* if necessary, set lines */
1126         if (linechange) {
1127                 priv->cmd_ctrl = 1;
1128                 cypress_write(tty, port, NULL, 0);
1129         }
1130 } /* cypress_set_termios */
1131
1132
1133 /* returns amount of data still left in soft buffer */
1134 static int cypress_chars_in_buffer(struct tty_struct *tty)
1135 {
1136         struct usb_serial_port *port = tty->driver_data;
1137         struct cypress_private *priv = usb_get_serial_port_data(port);
1138         int chars = 0;
1139         unsigned long flags;
1140
1141         dbg("%s - port %d", __func__, port->number);
1142
1143         spin_lock_irqsave(&priv->lock, flags);
1144         chars = kfifo_len(&priv->write_fifo);
1145         spin_unlock_irqrestore(&priv->lock, flags);
1146
1147         dbg("%s - returns %d", __func__, chars);
1148         return chars;
1149 }
1150
1151
1152 static void cypress_throttle(struct tty_struct *tty)
1153 {
1154         struct usb_serial_port *port = tty->driver_data;
1155         struct cypress_private *priv = usb_get_serial_port_data(port);
1156
1157         dbg("%s - port %d", __func__, port->number);
1158
1159         spin_lock_irq(&priv->lock);
1160         priv->rx_flags = THROTTLED;
1161         spin_unlock_irq(&priv->lock);
1162 }
1163
1164
1165 static void cypress_unthrottle(struct tty_struct *tty)
1166 {
1167         struct usb_serial_port *port = tty->driver_data;
1168         struct cypress_private *priv = usb_get_serial_port_data(port);
1169         int actually_throttled, result;
1170
1171         dbg("%s - port %d", __func__, port->number);
1172
1173         spin_lock_irq(&priv->lock);
1174         actually_throttled = priv->rx_flags & ACTUALLY_THROTTLED;
1175         priv->rx_flags = 0;
1176         spin_unlock_irq(&priv->lock);
1177
1178         if (!priv->comm_is_ok)
1179                 return;
1180
1181         if (actually_throttled) {
1182                 port->interrupt_in_urb->dev = port->serial->dev;
1183
1184                 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
1185                 if (result) {
1186                         dev_err(&port->dev, "%s - failed submitting read urb, "
1187                                         "error %d\n", __func__, result);
1188                         cypress_set_dead(port);
1189                 }
1190         }
1191 }
1192
1193
1194 static void cypress_read_int_callback(struct urb *urb)
1195 {
1196         struct usb_serial_port *port = urb->context;
1197         struct cypress_private *priv = usb_get_serial_port_data(port);
1198         struct tty_struct *tty;
1199         unsigned char *data = urb->transfer_buffer;
1200         unsigned long flags;
1201         char tty_flag = TTY_NORMAL;
1202         int havedata = 0;
1203         int bytes = 0;
1204         int result;
1205         int i = 0;
1206         int status = urb->status;
1207
1208         dbg("%s - port %d", __func__, port->number);
1209
1210         switch (status) {
1211         case 0: /* success */
1212                 break;
1213         case -ECONNRESET:
1214         case -ENOENT:
1215         case -ESHUTDOWN:
1216                 /* precursor to disconnect so just go away */
1217                 return;
1218         case -EPIPE:
1219                 /* Can't call usb_clear_halt while in_interrupt */
1220                 /* FALLS THROUGH */
1221         default:
1222                 /* something ugly is going on... */
1223                 dev_err(&urb->dev->dev,
1224                         "%s - unexpected nonzero read status received: %d\n",
1225                                                         __func__, status);
1226                 cypress_set_dead(port);
1227                 return;
1228         }
1229
1230         spin_lock_irqsave(&priv->lock, flags);
1231         if (priv->rx_flags & THROTTLED) {
1232                 dbg("%s - now throttling", __func__);
1233                 priv->rx_flags |= ACTUALLY_THROTTLED;
1234                 spin_unlock_irqrestore(&priv->lock, flags);
1235                 return;
1236         }
1237         spin_unlock_irqrestore(&priv->lock, flags);
1238
1239         tty = tty_port_tty_get(&port->port);
1240         if (!tty) {
1241                 dbg("%s - bad tty pointer - exiting", __func__);
1242                 return;
1243         }
1244
1245         spin_lock_irqsave(&priv->lock, flags);
1246         result = urb->actual_length;
1247         switch (priv->pkt_fmt) {
1248         default:
1249         case packet_format_1:
1250                 /* This is for the CY7C64013... */
1251                 priv->current_status = data[0] & 0xF8;
1252                 bytes = data[1] + 2;
1253                 i = 2;
1254                 if (bytes > 2)
1255                         havedata = 1;
1256                 break;
1257         case packet_format_2:
1258                 /* This is for the CY7C63743... */
1259                 priv->current_status = data[0] & 0xF8;
1260                 bytes = (data[0] & 0x07) + 1;
1261                 i = 1;
1262                 if (bytes > 1)
1263                         havedata = 1;
1264                 break;
1265         }
1266         spin_unlock_irqrestore(&priv->lock, flags);
1267         if (result < bytes) {
1268                 dbg("%s - wrong packet size - received %d bytes but packet "
1269                     "said %d bytes", __func__, result, bytes);
1270                 goto continue_read;
1271         }
1272
1273         usb_serial_debug_data(debug, &port->dev, __func__,
1274                                                 urb->actual_length, data);
1275
1276         spin_lock_irqsave(&priv->lock, flags);
1277         /* check to see if status has changed */
1278         if (priv->current_status != priv->prev_status) {
1279                 priv->diff_status |= priv->current_status ^
1280                         priv->prev_status;
1281                 wake_up_interruptible(&port->delta_msr_wait);
1282                 priv->prev_status = priv->current_status;
1283         }
1284         spin_unlock_irqrestore(&priv->lock, flags);
1285
1286         /* hangup, as defined in acm.c... this might be a bad place for it
1287          * though */
1288         if (tty && !(tty->termios->c_cflag & CLOCAL) &&
1289                         !(priv->current_status & UART_CD)) {
1290                 dbg("%s - calling hangup", __func__);
1291                 tty_hangup(tty);
1292                 goto continue_read;
1293         }
1294
1295         /* There is one error bit... I'm assuming it is a parity error
1296          * indicator as the generic firmware will set this bit to 1 if a
1297          * parity error occurs.
1298          * I can not find reference to any other error events. */
1299         spin_lock_irqsave(&priv->lock, flags);
1300         if (priv->current_status & CYP_ERROR) {
1301                 spin_unlock_irqrestore(&priv->lock, flags);
1302                 tty_flag = TTY_PARITY;
1303                 dbg("%s - Parity Error detected", __func__);
1304         } else
1305                 spin_unlock_irqrestore(&priv->lock, flags);
1306
1307         /* process read if there is data other than line status */
1308         if (tty && bytes > i) {
1309                 tty_insert_flip_string_fixed_flag(tty, data + i,
1310                                 tty_flag, bytes - i);
1311                 tty_flip_buffer_push(tty);
1312         }
1313
1314         spin_lock_irqsave(&priv->lock, flags);
1315         /* control and status byte(s) are also counted */
1316         priv->bytes_in += bytes;
1317         spin_unlock_irqrestore(&priv->lock, flags);
1318
1319 continue_read:
1320         tty_kref_put(tty);
1321
1322         /* Continue trying to always read */
1323
1324         if (priv->comm_is_ok) {
1325                 usb_fill_int_urb(port->interrupt_in_urb, port->serial->dev,
1326                                 usb_rcvintpipe(port->serial->dev,
1327                                         port->interrupt_in_endpointAddress),
1328                                 port->interrupt_in_urb->transfer_buffer,
1329                                 port->interrupt_in_urb->transfer_buffer_length,
1330                                 cypress_read_int_callback, port,
1331                                 priv->read_urb_interval);
1332                 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
1333                 if (result && result != -EPERM) {
1334                         dev_err(&urb->dev->dev, "%s - failed resubmitting "
1335                                         "read urb, error %d\n", __func__,
1336                                         result);
1337                         cypress_set_dead(port);
1338                 }
1339         }
1340 } /* cypress_read_int_callback */
1341
1342
1343 static void cypress_write_int_callback(struct urb *urb)
1344 {
1345         struct usb_serial_port *port = urb->context;
1346         struct cypress_private *priv = usb_get_serial_port_data(port);
1347         int result;
1348         int status = urb->status;
1349
1350         dbg("%s - port %d", __func__, port->number);
1351
1352         switch (status) {
1353         case 0:
1354                 /* success */
1355                 break;
1356         case -ECONNRESET:
1357         case -ENOENT:
1358         case -ESHUTDOWN:
1359                 /* this urb is terminated, clean up */
1360                 dbg("%s - urb shutting down with status: %d",
1361                                                 __func__, status);
1362                 priv->write_urb_in_use = 0;
1363                 return;
1364         case -EPIPE: /* no break needed; clear halt and resubmit */
1365                 if (!priv->comm_is_ok)
1366                         break;
1367                 usb_clear_halt(port->serial->dev, 0x02);
1368                 /* error in the urb, so we have to resubmit it */
1369                 dbg("%s - nonzero write bulk status received: %d",
1370                         __func__, status);
1371                 port->interrupt_out_urb->transfer_buffer_length = 1;
1372                 port->interrupt_out_urb->dev = port->serial->dev;
1373                 result = usb_submit_urb(port->interrupt_out_urb, GFP_ATOMIC);
1374                 if (!result)
1375                         return;
1376                 dev_err(&urb->dev->dev,
1377                         "%s - failed resubmitting write urb, error %d\n",
1378                                                         __func__, result);
1379                 cypress_set_dead(port);
1380                 break;
1381         default:
1382                 dev_err(&urb->dev->dev,
1383                          "%s - unexpected nonzero write status received: %d\n",
1384                                                         __func__, status);
1385                 cypress_set_dead(port);
1386                 break;
1387         }
1388         priv->write_urb_in_use = 0;
1389
1390         /* send any buffered data */
1391         cypress_send(port);
1392 }
1393
1394
1395 /*****************************************************************************
1396  * Module functions
1397  *****************************************************************************/
1398
1399 static int __init cypress_init(void)
1400 {
1401         int retval;
1402
1403         dbg("%s", __func__);
1404
1405         retval = usb_serial_register(&cypress_earthmate_device);
1406         if (retval)
1407                 goto failed_em_register;
1408         retval = usb_serial_register(&cypress_hidcom_device);
1409         if (retval)
1410                 goto failed_hidcom_register;
1411         retval = usb_serial_register(&cypress_ca42v2_device);
1412         if (retval)
1413                 goto failed_ca42v2_register;
1414         retval = usb_register(&cypress_driver);
1415         if (retval)
1416                 goto failed_usb_register;
1417
1418         printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
1419                DRIVER_DESC "\n");
1420         return 0;
1421
1422 failed_usb_register:
1423         usb_serial_deregister(&cypress_ca42v2_device);
1424 failed_ca42v2_register:
1425         usb_serial_deregister(&cypress_hidcom_device);
1426 failed_hidcom_register:
1427         usb_serial_deregister(&cypress_earthmate_device);
1428 failed_em_register:
1429         return retval;
1430 }
1431
1432
1433 static void __exit cypress_exit(void)
1434 {
1435         dbg("%s", __func__);
1436
1437         usb_deregister(&cypress_driver);
1438         usb_serial_deregister(&cypress_earthmate_device);
1439         usb_serial_deregister(&cypress_hidcom_device);
1440         usb_serial_deregister(&cypress_ca42v2_device);
1441 }
1442
1443
1444 module_init(cypress_init);
1445 module_exit(cypress_exit);
1446
1447 MODULE_AUTHOR(DRIVER_AUTHOR);
1448 MODULE_DESCRIPTION(DRIVER_DESC);
1449 MODULE_VERSION(DRIVER_VERSION);
1450 MODULE_LICENSE("GPL");
1451
1452 module_param(debug, bool, S_IRUGO | S_IWUSR);
1453 MODULE_PARM_DESC(debug, "Debug enabled or not");
1454 module_param(stats, bool, S_IRUGO | S_IWUSR);
1455 MODULE_PARM_DESC(stats, "Enable statistics or not");
1456 module_param(interval, int, S_IRUGO | S_IWUSR);
1457 MODULE_PARM_DESC(interval, "Overrides interrupt interval");
1458 module_param(unstable_bauds, bool, S_IRUGO | S_IWUSR);
1459 MODULE_PARM_DESC(unstable_bauds, "Allow unstable baud rates");