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