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