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