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