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