Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

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