2 * USB Cypress M8 driver
5 * Lonnie Mendez (dignome@gmail.com)
6 * Copyright (C) 2003,2004
7 * Neil Whelchel (koyama@firstlight.net)
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.
14 * See Documentation/usb/usb-serial.txt for more information on using this driver
16 * See http://geocities.com/i0xox0i for information on this driver and the
17 * earthmate usb device.
19 * Lonnie Mendez <dignome@gmail.com>
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.
28 * Lonnie Mendez <dignome@gmail.com>
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.
34 * Lonnie Mendez <dignome@gmail.com>
36 * Driver modified to support dynamic line settings. Various improvments
41 * Driver first released.
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. */
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>
67 #include "cypress_m8.h"
70 #ifdef CONFIG_USB_SERIAL_DEBUG
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"
85 /* write buffer size defines */
86 #define CYPRESS_BUF_SIZE 1024
87 #define CYPRESS_CLOSING_WAIT (30*HZ)
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 */
95 static struct usb_device_id id_table_cyphidcomrs232 [] = {
96 { USB_DEVICE(VENDOR_ID_CYPRESS, PRODUCT_ID_CYPHIDCOM) },
97 { } /* Terminating entry */
100 static struct usb_device_id id_table_nokiaca42v2 [] = {
101 { USB_DEVICE(VENDOR_ID_DAZZLE, PRODUCT_ID_CA42) },
102 { } /* Terminating entry */
105 static struct usb_device_id id_table_combined [] = {
106 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB) },
107 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB_LT20) },
108 { USB_DEVICE(VENDOR_ID_CYPRESS, PRODUCT_ID_CYPHIDCOM) },
109 { USB_DEVICE(VENDOR_ID_DAZZLE, PRODUCT_ID_CA42) },
110 { } /* Terminating entry */
113 MODULE_DEVICE_TABLE (usb, id_table_combined);
115 static struct usb_driver cypress_driver = {
117 .probe = usb_serial_probe,
118 .disconnect = usb_serial_disconnect,
119 .id_table = id_table_combined,
123 struct cypress_private {
124 spinlock_t lock; /* private lock */
125 int chiptype; /* identifier of device, for quirks/etc */
126 int bytes_in; /* used for statistics */
127 int bytes_out; /* used for statistics */
128 int cmd_count; /* used for statistics */
129 int cmd_ctrl; /* always set this to 1 before issuing a command */
130 struct cypress_buf *buf; /* write buffer */
131 int write_urb_in_use; /* write urb in use indicator */
132 int write_urb_interval; /* interval to use for write urb */
133 int read_urb_interval; /* interval to use for read urb */
134 int comm_is_ok; /* true if communication is (still) ok */
135 int termios_initialized;
136 __u8 line_control; /* holds dtr / rts value */
137 __u8 current_status; /* received from last read - info on dsr,cts,cd,ri,etc */
138 __u8 current_config; /* stores the current configuration byte */
139 __u8 rx_flags; /* throttling - used from whiteheat/ftdi_sio */
140 int baud_rate; /* stores current baud rate in integer form */
141 int cbr_mask; /* stores current baud rate in masked form */
142 int isthrottled; /* if throttled, discard reads */
143 wait_queue_head_t delta_msr_wait; /* used for TIOCMIWAIT */
144 char prev_status, diff_status; /* used for TIOCMIWAIT */
145 /* we pass a pointer to this as the arguement sent to cypress_set_termios old_termios */
146 struct ktermios tmp_termios; /* stores the old termios settings */
149 /* write buffer structure */
151 unsigned int buf_size;
157 /* function prototypes for the Cypress USB to serial device */
158 static int cypress_earthmate_startup (struct usb_serial *serial);
159 static int cypress_hidcom_startup (struct usb_serial *serial);
160 static int cypress_ca42v2_startup (struct usb_serial *serial);
161 static void cypress_shutdown (struct usb_serial *serial);
162 static int cypress_open (struct usb_serial_port *port, struct file *filp);
163 static void cypress_close (struct usb_serial_port *port, struct file *filp);
164 static int cypress_write (struct usb_serial_port *port, const unsigned char *buf, int count);
165 static void cypress_send (struct usb_serial_port *port);
166 static int cypress_write_room (struct usb_serial_port *port);
167 static int cypress_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg);
168 static void cypress_set_termios (struct usb_serial_port *port, struct ktermios * old);
169 static int cypress_tiocmget (struct usb_serial_port *port, struct file *file);
170 static int cypress_tiocmset (struct usb_serial_port *port, struct file *file, unsigned int set, unsigned int clear);
171 static int cypress_chars_in_buffer (struct usb_serial_port *port);
172 static void cypress_throttle (struct usb_serial_port *port);
173 static void cypress_unthrottle (struct usb_serial_port *port);
174 static void cypress_set_dead (struct usb_serial_port *port);
175 static void cypress_read_int_callback (struct urb *urb);
176 static void cypress_write_int_callback (struct urb *urb);
177 /* baud helper functions */
178 static int mask_to_rate (unsigned mask);
179 static unsigned rate_to_mask (int rate);
180 /* write buffer functions */
181 static struct cypress_buf *cypress_buf_alloc(unsigned int size);
182 static void cypress_buf_free(struct cypress_buf *cb);
183 static void cypress_buf_clear(struct cypress_buf *cb);
184 static unsigned int cypress_buf_data_avail(struct cypress_buf *cb);
185 static unsigned int cypress_buf_space_avail(struct cypress_buf *cb);
186 static unsigned int cypress_buf_put(struct cypress_buf *cb, const char *buf, unsigned int count);
187 static unsigned int cypress_buf_get(struct cypress_buf *cb, char *buf, unsigned int count);
190 static struct usb_serial_driver cypress_earthmate_device = {
192 .owner = THIS_MODULE,
195 .description = "DeLorme Earthmate USB",
196 .id_table = id_table_earthmate,
197 .num_interrupt_in = 1,
198 .num_interrupt_out = 1,
199 .num_bulk_in = NUM_DONT_CARE,
200 .num_bulk_out = NUM_DONT_CARE,
202 .attach = cypress_earthmate_startup,
203 .shutdown = cypress_shutdown,
204 .open = cypress_open,
205 .close = cypress_close,
206 .write = cypress_write,
207 .write_room = cypress_write_room,
208 .ioctl = cypress_ioctl,
209 .set_termios = cypress_set_termios,
210 .tiocmget = cypress_tiocmget,
211 .tiocmset = cypress_tiocmset,
212 .chars_in_buffer = cypress_chars_in_buffer,
213 .throttle = cypress_throttle,
214 .unthrottle = cypress_unthrottle,
215 .read_int_callback = cypress_read_int_callback,
216 .write_int_callback = cypress_write_int_callback,
219 static struct usb_serial_driver cypress_hidcom_device = {
221 .owner = THIS_MODULE,
224 .description = "HID->COM RS232 Adapter",
225 .id_table = id_table_cyphidcomrs232,
226 .num_interrupt_in = 1,
227 .num_interrupt_out = 1,
228 .num_bulk_in = NUM_DONT_CARE,
229 .num_bulk_out = NUM_DONT_CARE,
231 .attach = cypress_hidcom_startup,
232 .shutdown = cypress_shutdown,
233 .open = cypress_open,
234 .close = cypress_close,
235 .write = cypress_write,
236 .write_room = cypress_write_room,
237 .ioctl = cypress_ioctl,
238 .set_termios = cypress_set_termios,
239 .tiocmget = cypress_tiocmget,
240 .tiocmset = cypress_tiocmset,
241 .chars_in_buffer = cypress_chars_in_buffer,
242 .throttle = cypress_throttle,
243 .unthrottle = cypress_unthrottle,
244 .read_int_callback = cypress_read_int_callback,
245 .write_int_callback = cypress_write_int_callback,
248 static struct usb_serial_driver cypress_ca42v2_device = {
250 .owner = THIS_MODULE,
251 .name = "nokiaca42v2",
253 .description = "Nokia CA-42 V2 Adapter",
254 .id_table = id_table_nokiaca42v2,
255 .num_interrupt_in = 1,
256 .num_interrupt_out = 1,
257 .num_bulk_in = NUM_DONT_CARE,
258 .num_bulk_out = NUM_DONT_CARE,
260 .attach = cypress_ca42v2_startup,
261 .shutdown = cypress_shutdown,
262 .open = cypress_open,
263 .close = cypress_close,
264 .write = cypress_write,
265 .write_room = cypress_write_room,
266 .ioctl = cypress_ioctl,
267 .set_termios = cypress_set_termios,
268 .tiocmget = cypress_tiocmget,
269 .tiocmset = cypress_tiocmset,
270 .chars_in_buffer = cypress_chars_in_buffer,
271 .throttle = cypress_throttle,
272 .unthrottle = cypress_unthrottle,
273 .read_int_callback = cypress_read_int_callback,
274 .write_int_callback = cypress_write_int_callback,
277 /*****************************************************************************
278 * Cypress serial helper functions
279 *****************************************************************************/
282 /* This function can either set or retrieve the current serial line settings */
283 static int cypress_serial_control (struct usb_serial_port *port, unsigned baud_mask, int data_bits, int stop_bits,
284 int parity_enable, int parity_type, int reset, int cypress_request_type)
286 int new_baudrate = 0, retval = 0, tries = 0;
287 struct cypress_private *priv;
288 __u8 feature_buffer[8];
291 dbg("%s", __FUNCTION__);
293 priv = usb_get_serial_port_data(port);
295 if (!priv->comm_is_ok)
298 switch(cypress_request_type) {
299 case CYPRESS_SET_CONFIG:
302 * The general purpose firmware for the Cypress M8 allows for a maximum speed
303 * of 57600bps (I have no idea whether DeLorme chose to use the general purpose
304 * firmware or not), if you need to modify this speed setting for your own
305 * project please add your own chiptype and modify the code likewise. The
306 * Cypress HID->COM device will work successfully up to 115200bps (but the
307 * actual throughput is around 3kBps).
309 if (baud_mask != priv->cbr_mask) {
310 dbg("%s - baud rate is changing", __FUNCTION__);
311 if ( priv->chiptype == CT_EARTHMATE ) {
312 /* 300 and 600 baud rates are supported under the generic firmware,
313 * but are not used with NMEA and SiRF protocols */
315 if ( (baud_mask == B300) || (baud_mask == B600) ) {
316 err("%s - failed setting baud rate, unsupported speed",
318 new_baudrate = priv->baud_rate;
319 } else if ( (new_baudrate = mask_to_rate(baud_mask)) == -1) {
320 err("%s - failed setting baud rate, unsupported speed",
322 new_baudrate = priv->baud_rate;
324 } else if (priv->chiptype == CT_CYPHIDCOM) {
325 if ( (new_baudrate = mask_to_rate(baud_mask)) == -1) {
326 err("%s - failed setting baud rate, unsupported speed",
328 new_baudrate = priv->baud_rate;
330 } else if (priv->chiptype == CT_CA42V2) {
331 if ( (new_baudrate = mask_to_rate(baud_mask)) == -1) {
332 err("%s - failed setting baud rate, unsupported speed",
334 new_baudrate = priv->baud_rate;
336 } else if (priv->chiptype == CT_GENERIC) {
337 if ( (new_baudrate = mask_to_rate(baud_mask)) == -1) {
338 err("%s - failed setting baud rate, unsupported speed",
340 new_baudrate = priv->baud_rate;
343 info("%s - please define your chiptype", __FUNCTION__);
344 new_baudrate = priv->baud_rate;
346 } else { /* baud rate not changing, keep the old */
347 new_baudrate = priv->baud_rate;
349 dbg("%s - baud rate is being sent as %d", __FUNCTION__, new_baudrate);
351 memset(feature_buffer, 0, 8);
352 /* fill the feature_buffer with new configuration */
353 *((u_int32_t *)feature_buffer) = new_baudrate;
355 feature_buffer[4] |= data_bits; /* assign data bits in 2 bit space ( max 3 ) */
357 feature_buffer[4] |= (stop_bits << 3); /* assign stop bits in 1 bit space */
358 feature_buffer[4] |= (parity_enable << 4); /* assign parity flag in 1 bit space */
359 feature_buffer[4] |= (parity_type << 5); /* assign parity type in 1 bit space */
361 feature_buffer[4] |= (reset << 7); /* assign reset at end of byte, 1 bit space */
363 dbg("%s - device is being sent this feature report:", __FUNCTION__);
364 dbg("%s - %02X - %02X - %02X - %02X - %02X", __FUNCTION__, feature_buffer[0], feature_buffer[1],
365 feature_buffer[2], feature_buffer[3], feature_buffer[4]);
368 retval = usb_control_msg (port->serial->dev, usb_sndctrlpipe(port->serial->dev, 0),
369 HID_REQ_SET_REPORT, USB_DIR_OUT | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
370 0x0300, 0, feature_buffer, 8, 500);
375 } while (retval != 8 && retval != -ENODEV);
378 err("%s - failed sending serial line settings - %d", __FUNCTION__, retval);
379 cypress_set_dead(port);
381 spin_lock_irqsave(&priv->lock, flags);
382 priv->baud_rate = new_baudrate;
383 priv->cbr_mask = baud_mask;
384 priv->current_config = feature_buffer[4];
385 spin_unlock_irqrestore(&priv->lock, flags);
388 case CYPRESS_GET_CONFIG:
389 dbg("%s - retreiving serial line settings", __FUNCTION__);
390 /* set initial values in feature buffer */
391 memset(feature_buffer, 0, 8);
394 retval = usb_control_msg (port->serial->dev, usb_rcvctrlpipe(port->serial->dev, 0),
395 HID_REQ_GET_REPORT, USB_DIR_IN | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
396 0x0300, 0, feature_buffer, 8, 500);
401 } while (retval != 5 && retval != -ENODEV);
404 err("%s - failed to retrieve serial line settings - %d", __FUNCTION__, retval);
405 cypress_set_dead(port);
408 spin_lock_irqsave(&priv->lock, flags);
410 /* store the config in one byte, and later use bit masks to check values */
411 priv->current_config = feature_buffer[4];
412 priv->baud_rate = *((u_int32_t *)feature_buffer);
414 if ( (priv->cbr_mask = rate_to_mask(priv->baud_rate)) == 0x40)
415 dbg("%s - failed setting the baud mask (not defined)", __FUNCTION__);
416 spin_unlock_irqrestore(&priv->lock, flags);
419 spin_lock_irqsave(&priv->lock, flags);
421 spin_unlock_irqrestore(&priv->lock, flags);
424 } /* cypress_serial_control */
427 static void cypress_set_dead(struct usb_serial_port *port)
429 struct cypress_private *priv = usb_get_serial_port_data(port);
432 spin_lock_irqsave(&priv->lock, flags);
433 if (!priv->comm_is_ok) {
434 spin_unlock_irqrestore(&priv->lock, flags);
437 priv->comm_is_ok = 0;
438 spin_unlock_irqrestore(&priv->lock, flags);
440 err("cypress_m8 suspending failing port %d - interval might be too short",
445 /* given a baud mask, it will return integer baud on success */
446 static int mask_to_rate (unsigned mask)
451 case B0: rate = 0; break;
452 case B300: rate = 300; break;
453 case B600: rate = 600; break;
454 case B1200: rate = 1200; break;
455 case B2400: rate = 2400; break;
456 case B4800: rate = 4800; break;
457 case B9600: rate = 9600; break;
458 case B19200: rate = 19200; break;
459 case B38400: rate = 38400; break;
460 case B57600: rate = 57600; break;
461 case B115200: rate = 115200; break;
469 static unsigned rate_to_mask (int rate)
474 case 0: mask = B0; break;
475 case 300: mask = B300; break;
476 case 600: mask = B600; break;
477 case 1200: mask = B1200; break;
478 case 2400: mask = B2400; break;
479 case 4800: mask = B4800; break;
480 case 9600: mask = B9600; break;
481 case 19200: mask = B19200; break;
482 case 38400: mask = B38400; break;
483 case 57600: mask = B57600; break;
484 case 115200: mask = B115200; break;
485 default: mask = 0x40;
490 /*****************************************************************************
491 * Cypress serial driver functions
492 *****************************************************************************/
495 static int generic_startup (struct usb_serial *serial)
497 struct cypress_private *priv;
498 struct usb_serial_port *port = serial->port[0];
500 dbg("%s - port %d", __FUNCTION__, port->number);
502 priv = kzalloc(sizeof (struct cypress_private), GFP_KERNEL);
506 priv->comm_is_ok = !0;
507 spin_lock_init(&priv->lock);
508 priv->buf = cypress_buf_alloc(CYPRESS_BUF_SIZE);
509 if (priv->buf == NULL) {
513 init_waitqueue_head(&priv->delta_msr_wait);
515 usb_reset_configuration (serial->dev);
518 priv->line_control = 0;
519 priv->termios_initialized = 0;
521 priv->cbr_mask = B300;
523 priv->write_urb_interval = interval;
524 priv->read_urb_interval = interval;
525 dbg("%s - port %d read & write intervals forced to %d",
526 __FUNCTION__,port->number,interval);
528 priv->write_urb_interval = port->interrupt_out_urb->interval;
529 priv->read_urb_interval = port->interrupt_in_urb->interval;
530 dbg("%s - port %d intervals: read=%d write=%d",
531 __FUNCTION__,port->number,
532 priv->read_urb_interval,priv->write_urb_interval);
534 usb_set_serial_port_data(port, priv);
540 static int cypress_earthmate_startup (struct usb_serial *serial)
542 struct cypress_private *priv;
544 dbg("%s", __FUNCTION__);
546 if (generic_startup(serial)) {
547 dbg("%s - Failed setting up port %d", __FUNCTION__,
548 serial->port[0]->number);
552 priv = usb_get_serial_port_data(serial->port[0]);
553 priv->chiptype = CT_EARTHMATE;
556 } /* cypress_earthmate_startup */
559 static int cypress_hidcom_startup (struct usb_serial *serial)
561 struct cypress_private *priv;
563 dbg("%s", __FUNCTION__);
565 if (generic_startup(serial)) {
566 dbg("%s - Failed setting up port %d", __FUNCTION__,
567 serial->port[0]->number);
571 priv = usb_get_serial_port_data(serial->port[0]);
572 priv->chiptype = CT_CYPHIDCOM;
575 } /* cypress_hidcom_startup */
578 static int cypress_ca42v2_startup (struct usb_serial *serial)
580 struct cypress_private *priv;
582 dbg("%s", __FUNCTION__);
584 if (generic_startup(serial)) {
585 dbg("%s - Failed setting up port %d", __FUNCTION__,
586 serial->port[0]->number);
590 priv = usb_get_serial_port_data(serial->port[0]);
591 priv->chiptype = CT_CA42V2;
594 } /* cypress_ca42v2_startup */
597 static void cypress_shutdown (struct usb_serial *serial)
599 struct cypress_private *priv;
601 dbg ("%s - port %d", __FUNCTION__, serial->port[0]->number);
603 /* all open ports are closed at this point */
605 priv = usb_get_serial_port_data(serial->port[0]);
608 cypress_buf_free(priv->buf);
610 usb_set_serial_port_data(serial->port[0], NULL);
615 static int cypress_open (struct usb_serial_port *port, struct file *filp)
617 struct cypress_private *priv = usb_get_serial_port_data(port);
618 struct usb_serial *serial = port->serial;
622 dbg("%s - port %d", __FUNCTION__, port->number);
624 if (!priv->comm_is_ok)
627 /* clear halts before open */
628 usb_clear_halt(serial->dev, 0x81);
629 usb_clear_halt(serial->dev, 0x02);
631 spin_lock_irqsave(&priv->lock, flags);
632 /* reset read/write statistics */
637 spin_unlock_irqrestore(&priv->lock, flags);
639 /* setting to zero could cause data loss */
640 port->tty->low_latency = 1;
642 /* raise both lines and set termios */
643 spin_lock_irqsave(&priv->lock, flags);
644 priv->line_control = CONTROL_DTR | CONTROL_RTS;
646 spin_unlock_irqrestore(&priv->lock, flags);
647 result = cypress_write(port, NULL, 0);
650 dev_err(&port->dev, "%s - failed setting the control lines - error %d\n", __FUNCTION__, result);
653 dbg("%s - success setting the control lines", __FUNCTION__);
655 cypress_set_termios(port, &priv->tmp_termios);
657 /* setup the port and start reading from the device */
658 if(!port->interrupt_in_urb){
659 err("%s - interrupt_in_urb is empty!", __FUNCTION__);
663 usb_fill_int_urb(port->interrupt_in_urb, serial->dev,
664 usb_rcvintpipe(serial->dev, port->interrupt_in_endpointAddress),
665 port->interrupt_in_urb->transfer_buffer, port->interrupt_in_urb->transfer_buffer_length,
666 cypress_read_int_callback, port, priv->read_urb_interval);
667 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
670 dev_err(&port->dev, "%s - failed submitting read urb, error %d\n", __FUNCTION__, result);
671 cypress_set_dead(port);
678 static void cypress_close(struct usb_serial_port *port, struct file * filp)
680 struct cypress_private *priv = usb_get_serial_port_data(port);
681 unsigned int c_cflag;
687 dbg("%s - port %d", __FUNCTION__, port->number);
689 /* wait for data to drain from buffer */
690 spin_lock_irqsave(&priv->lock, flags);
691 timeout = CYPRESS_CLOSING_WAIT;
692 init_waitqueue_entry(&wait, current);
693 add_wait_queue(&port->tty->write_wait, &wait);
695 set_current_state(TASK_INTERRUPTIBLE);
696 if (cypress_buf_data_avail(priv->buf) == 0
697 || timeout == 0 || signal_pending(current)
698 || !usb_get_intfdata(port->serial->interface))
700 spin_unlock_irqrestore(&priv->lock, flags);
701 timeout = schedule_timeout(timeout);
702 spin_lock_irqsave(&priv->lock, flags);
704 set_current_state(TASK_RUNNING);
705 remove_wait_queue(&port->tty->write_wait, &wait);
706 /* clear out any remaining data in the buffer */
707 cypress_buf_clear(priv->buf);
708 spin_unlock_irqrestore(&priv->lock, flags);
710 /* wait for characters to drain from device */
711 bps = tty_get_baud_rate(port->tty);
713 timeout = max((HZ*2560)/bps,HZ/10);
716 schedule_timeout_interruptible(timeout);
718 dbg("%s - stopping urbs", __FUNCTION__);
719 usb_kill_urb (port->interrupt_in_urb);
720 usb_kill_urb (port->interrupt_out_urb);
723 c_cflag = port->tty->termios->c_cflag;
724 if (c_cflag & HUPCL) {
725 /* drop dtr and rts */
726 priv = usb_get_serial_port_data(port);
727 spin_lock_irqsave(&priv->lock, flags);
728 priv->line_control = 0;
730 spin_unlock_irqrestore(&priv->lock, flags);
731 cypress_write(port, NULL, 0);
736 dev_info (&port->dev, "Statistics: %d Bytes In | %d Bytes Out | %d Commands Issued\n",
737 priv->bytes_in, priv->bytes_out, priv->cmd_count);
738 } /* cypress_close */
741 static int cypress_write(struct usb_serial_port *port, const unsigned char *buf, int count)
743 struct cypress_private *priv = usb_get_serial_port_data(port);
746 dbg("%s - port %d, %d bytes", __FUNCTION__, port->number, count);
748 /* line control commands, which need to be executed immediately,
749 are not put into the buffer for obvious reasons.
751 if (priv->cmd_ctrl) {
759 spin_lock_irqsave(&priv->lock, flags);
760 count = cypress_buf_put(priv->buf, buf, count);
761 spin_unlock_irqrestore(&priv->lock, flags);
767 } /* cypress_write */
770 static void cypress_send(struct usb_serial_port *port)
772 int count = 0, result, offset, actual_size;
773 struct cypress_private *priv = usb_get_serial_port_data(port);
776 if (!priv->comm_is_ok)
779 dbg("%s - port %d", __FUNCTION__, port->number);
780 dbg("%s - interrupt out size is %d", __FUNCTION__, port->interrupt_out_size);
782 spin_lock_irqsave(&priv->lock, flags);
783 if (priv->write_urb_in_use) {
784 dbg("%s - can't write, urb in use", __FUNCTION__);
785 spin_unlock_irqrestore(&priv->lock, flags);
788 spin_unlock_irqrestore(&priv->lock, flags);
791 memset(port->interrupt_out_urb->transfer_buffer, 0, port->interrupt_out_size);
793 spin_lock_irqsave(&priv->lock, flags);
794 switch (port->interrupt_out_size) {
796 /* this is for the CY7C64013... */
798 port->interrupt_out_buffer[0] = priv->line_control;
801 /* this is for the CY7C63743... */
803 port->interrupt_out_buffer[0] = priv->line_control;
806 dbg("%s - wrong packet size", __FUNCTION__);
807 spin_unlock_irqrestore(&priv->lock, flags);
811 if (priv->line_control & CONTROL_RESET)
812 priv->line_control &= ~CONTROL_RESET;
814 if (priv->cmd_ctrl) {
816 dbg("%s - line control command being issued", __FUNCTION__);
817 spin_unlock_irqrestore(&priv->lock, flags);
820 spin_unlock_irqrestore(&priv->lock, flags);
822 count = cypress_buf_get(priv->buf, &port->interrupt_out_buffer[offset],
823 port->interrupt_out_size-offset);
829 switch (port->interrupt_out_size) {
831 port->interrupt_out_buffer[1] = count;
834 port->interrupt_out_buffer[0] |= count;
837 dbg("%s - count is %d", __FUNCTION__, count);
840 spin_lock_irqsave(&priv->lock, flags);
841 priv->write_urb_in_use = 1;
842 spin_unlock_irqrestore(&priv->lock, flags);
847 actual_size = count + (port->interrupt_out_size == 32 ? 2 : 1);
849 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, port->interrupt_out_size,
850 port->interrupt_out_urb->transfer_buffer);
852 usb_fill_int_urb(port->interrupt_out_urb, port->serial->dev,
853 usb_sndintpipe(port->serial->dev, port->interrupt_out_endpointAddress),
854 port->interrupt_out_buffer, port->interrupt_out_size,
855 cypress_write_int_callback, port, priv->write_urb_interval);
856 result = usb_submit_urb (port->interrupt_out_urb, GFP_ATOMIC);
858 dev_err(&port->dev, "%s - failed submitting write urb, error %d\n", __FUNCTION__,
860 priv->write_urb_in_use = 0;
861 cypress_set_dead(port);
864 spin_lock_irqsave(&priv->lock, flags);
865 if (priv->cmd_ctrl) {
868 priv->bytes_out += count; /* do not count the line control and size bytes */
869 spin_unlock_irqrestore(&priv->lock, flags);
871 usb_serial_port_softint(port);
875 /* returns how much space is available in the soft buffer */
876 static int cypress_write_room(struct usb_serial_port *port)
878 struct cypress_private *priv = usb_get_serial_port_data(port);
882 dbg("%s - port %d", __FUNCTION__, port->number);
884 spin_lock_irqsave(&priv->lock, flags);
885 room = cypress_buf_space_avail(priv->buf);
886 spin_unlock_irqrestore(&priv->lock, flags);
888 dbg("%s - returns %d", __FUNCTION__, room);
893 static int cypress_tiocmget (struct usb_serial_port *port, struct file *file)
895 struct cypress_private *priv = usb_get_serial_port_data(port);
896 __u8 status, control;
897 unsigned int result = 0;
900 dbg("%s - port %d", __FUNCTION__, port->number);
902 spin_lock_irqsave(&priv->lock, flags);
903 control = priv->line_control;
904 status = priv->current_status;
905 spin_unlock_irqrestore(&priv->lock, flags);
907 result = ((control & CONTROL_DTR) ? TIOCM_DTR : 0)
908 | ((control & CONTROL_RTS) ? TIOCM_RTS : 0)
909 | ((status & UART_CTS) ? TIOCM_CTS : 0)
910 | ((status & UART_DSR) ? TIOCM_DSR : 0)
911 | ((status & UART_RI) ? TIOCM_RI : 0)
912 | ((status & UART_CD) ? TIOCM_CD : 0);
914 dbg("%s - result = %x", __FUNCTION__, result);
920 static int cypress_tiocmset (struct usb_serial_port *port, struct file *file,
921 unsigned int set, unsigned int clear)
923 struct cypress_private *priv = usb_get_serial_port_data(port);
926 dbg("%s - port %d", __FUNCTION__, port->number);
928 spin_lock_irqsave(&priv->lock, flags);
930 priv->line_control |= CONTROL_RTS;
932 priv->line_control |= CONTROL_DTR;
933 if (clear & TIOCM_RTS)
934 priv->line_control &= ~CONTROL_RTS;
935 if (clear & TIOCM_DTR)
936 priv->line_control &= ~CONTROL_DTR;
937 spin_unlock_irqrestore(&priv->lock, flags);
940 return cypress_write(port, NULL, 0);
944 static int cypress_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg)
946 struct cypress_private *priv = usb_get_serial_port_data(port);
948 dbg("%s - port %d, cmd 0x%.4x", __FUNCTION__, port->number, cmd);
952 if (copy_to_user((void __user *)arg, port->tty->termios, sizeof(struct ktermios))) {
958 if (copy_from_user(port->tty->termios, (void __user *)arg, sizeof(struct ktermios))) {
961 /* here we need to call cypress_set_termios to invoke the new settings */
962 cypress_set_termios(port, &priv->tmp_termios);
965 /* This code comes from drivers/char/serial.c and ftdi_sio.c */
967 while (priv != NULL) {
968 interruptible_sleep_on(&priv->delta_msr_wait);
969 /* see if a signal did it */
970 if (signal_pending(current))
973 char diff = priv->diff_status;
976 return -EIO; /* no change => error */
979 /* consume all events */
980 priv->diff_status = 0;
982 /* return 0 if caller wanted to know about these bits */
983 if ( ((arg & TIOCM_RNG) && (diff & UART_RI)) ||
984 ((arg & TIOCM_DSR) && (diff & UART_DSR)) ||
985 ((arg & TIOCM_CD) && (diff & UART_CD)) ||
986 ((arg & TIOCM_CTS) && (diff & UART_CTS)) ) {
989 /* otherwise caller can't care less about what happened,
990 * and so we continue to wait for more events.
1000 dbg("%s - arg not supported - it was 0x%04x - check include/asm/ioctls.h", __FUNCTION__, cmd);
1002 return -ENOIOCTLCMD;
1003 } /* cypress_ioctl */
1006 static void cypress_set_termios (struct usb_serial_port *port,
1007 struct ktermios *old_termios)
1009 struct cypress_private *priv = usb_get_serial_port_data(port);
1010 struct tty_struct *tty;
1011 int data_bits, stop_bits, parity_type, parity_enable;
1012 unsigned cflag, iflag, baud_mask;
1013 unsigned long flags;
1017 dbg("%s - port %d", __FUNCTION__, port->number);
1020 if ((!tty) || (!tty->termios)) {
1021 dbg("%s - no tty structures", __FUNCTION__);
1025 spin_lock_irqsave(&priv->lock, flags);
1026 if (!priv->termios_initialized) {
1027 if (priv->chiptype == CT_EARTHMATE) {
1028 *(tty->termios) = tty_std_termios;
1029 tty->termios->c_cflag = B4800 | CS8 | CREAD | HUPCL |
1031 } else if (priv->chiptype == CT_CYPHIDCOM) {
1032 *(tty->termios) = tty_std_termios;
1033 tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
1035 } else if (priv->chiptype == CT_CA42V2) {
1036 *(tty->termios) = tty_std_termios;
1037 tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
1040 priv->termios_initialized = 1;
1042 spin_unlock_irqrestore(&priv->lock, flags);
1044 cflag = tty->termios->c_cflag;
1045 iflag = tty->termios->c_iflag;
1047 /* check if there are new settings */
1049 if ((cflag != old_termios->c_cflag) ||
1050 (RELEVANT_IFLAG(iflag) !=
1051 RELEVANT_IFLAG(old_termios->c_iflag))) {
1052 dbg("%s - attempting to set new termios settings",
1054 /* should make a copy of this in case something goes
1055 * wrong in the function, we can restore it */
1056 spin_lock_irqsave(&priv->lock, flags);
1057 priv->tmp_termios = *(tty->termios);
1058 spin_unlock_irqrestore(&priv->lock, flags);
1060 dbg("%s - nothing to do, exiting", __FUNCTION__);
1066 /* set number of data bits, parity, stop bits */
1067 /* when parity is disabled the parity type bit is ignored */
1069 /* 1 means 2 stop bits, 0 means 1 stop bit */
1070 stop_bits = cflag & CSTOPB ? 1 : 0;
1072 if (cflag & PARENB) {
1074 /* 1 means odd parity, 0 means even parity */
1075 parity_type = cflag & PARODD ? 1 : 0;
1077 parity_enable = parity_type = 0;
1079 if (cflag & CSIZE) {
1080 switch (cflag & CSIZE) {
1094 err("%s - CSIZE was set, but not CS5-CS8",
1101 spin_lock_irqsave(&priv->lock, flags);
1102 oldlines = priv->line_control;
1103 if ((cflag & CBAUD) == B0) {
1104 /* drop dtr and rts */
1105 dbg("%s - dropping the lines, baud rate 0bps", __FUNCTION__);
1107 priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
1109 baud_mask = (cflag & CBAUD);
1112 dbg("%s - setting baud 300bps", __FUNCTION__);
1115 dbg("%s - setting baud 600bps", __FUNCTION__);
1118 dbg("%s - setting baud 1200bps", __FUNCTION__);
1121 dbg("%s - setting baud 2400bps", __FUNCTION__);
1124 dbg("%s - setting baud 4800bps", __FUNCTION__);
1127 dbg("%s - setting baud 9600bps", __FUNCTION__);
1130 dbg("%s - setting baud 19200bps", __FUNCTION__);
1133 dbg("%s - setting baud 38400bps", __FUNCTION__);
1136 dbg("%s - setting baud 57600bps", __FUNCTION__);
1139 dbg("%s - setting baud 115200bps", __FUNCTION__);
1142 dbg("%s - unknown masked baud rate", __FUNCTION__);
1144 priv->line_control = (CONTROL_DTR | CONTROL_RTS);
1146 spin_unlock_irqrestore(&priv->lock, flags);
1148 dbg("%s - sending %d stop_bits, %d parity_enable, %d parity_type, "
1149 "%d data_bits (+5)", __FUNCTION__, stop_bits,
1150 parity_enable, parity_type, data_bits);
1152 cypress_serial_control(port, baud_mask, data_bits, stop_bits,
1153 parity_enable, parity_type, 0, CYPRESS_SET_CONFIG);
1155 /* we perform a CYPRESS_GET_CONFIG so that the current settings are
1156 * filled into the private structure this should confirm that all is
1157 * working if it returns what we just set */
1158 cypress_serial_control(port, 0, 0, 0, 0, 0, 0, CYPRESS_GET_CONFIG);
1160 /* Here we can define custom tty settings for devices; the main tty
1161 * termios flag base comes from empeg.c */
1163 spin_lock_irqsave(&priv->lock, flags);
1164 if ( (priv->chiptype == CT_EARTHMATE) && (priv->baud_rate == 4800) ) {
1165 dbg("Using custom termios settings for a baud rate of "
1167 /* define custom termios settings for NMEA protocol */
1169 tty->termios->c_iflag /* input modes - */
1170 &= ~(IGNBRK /* disable ignore break */
1171 | BRKINT /* disable break causes interrupt */
1172 | PARMRK /* disable mark parity errors */
1173 | ISTRIP /* disable clear high bit of input char */
1174 | INLCR /* disable translate NL to CR */
1175 | IGNCR /* disable ignore CR */
1176 | ICRNL /* disable translate CR to NL */
1177 | IXON); /* disable enable XON/XOFF flow control */
1179 tty->termios->c_oflag /* output modes */
1180 &= ~OPOST; /* disable postprocess output char */
1182 tty->termios->c_lflag /* line discipline modes */
1183 &= ~(ECHO /* disable echo input characters */
1184 | ECHONL /* disable echo new line */
1185 | ICANON /* disable erase, kill, werase, and rprnt
1186 special characters */
1187 | ISIG /* disable interrupt, quit, and suspend
1188 special characters */
1189 | IEXTEN); /* disable non-POSIX special characters */
1190 } /* CT_CYPHIDCOM: Application should handle this for device */
1192 linechange = (priv->line_control != oldlines);
1193 spin_unlock_irqrestore(&priv->lock, flags);
1195 /* if necessary, set lines */
1198 cypress_write(port, NULL, 0);
1200 } /* cypress_set_termios */
1203 /* returns amount of data still left in soft buffer */
1204 static int cypress_chars_in_buffer(struct usb_serial_port *port)
1206 struct cypress_private *priv = usb_get_serial_port_data(port);
1208 unsigned long flags;
1210 dbg("%s - port %d", __FUNCTION__, port->number);
1212 spin_lock_irqsave(&priv->lock, flags);
1213 chars = cypress_buf_data_avail(priv->buf);
1214 spin_unlock_irqrestore(&priv->lock, flags);
1216 dbg("%s - returns %d", __FUNCTION__, chars);
1221 static void cypress_throttle (struct usb_serial_port *port)
1223 struct cypress_private *priv = usb_get_serial_port_data(port);
1224 unsigned long flags;
1226 dbg("%s - port %d", __FUNCTION__, port->number);
1228 spin_lock_irqsave(&priv->lock, flags);
1229 priv->rx_flags = THROTTLED;
1230 spin_unlock_irqrestore(&priv->lock, flags);
1234 static void cypress_unthrottle (struct usb_serial_port *port)
1236 struct cypress_private *priv = usb_get_serial_port_data(port);
1237 int actually_throttled, result;
1238 unsigned long flags;
1240 dbg("%s - port %d", __FUNCTION__, port->number);
1242 spin_lock_irqsave(&priv->lock, flags);
1243 actually_throttled = priv->rx_flags & ACTUALLY_THROTTLED;
1245 spin_unlock_irqrestore(&priv->lock, flags);
1247 if (!priv->comm_is_ok)
1250 if (actually_throttled) {
1251 port->interrupt_in_urb->dev = port->serial->dev;
1253 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
1255 dev_err(&port->dev, "%s - failed submitting read urb, "
1256 "error %d\n", __FUNCTION__, result);
1257 cypress_set_dead(port);
1263 static void cypress_read_int_callback(struct urb *urb)
1265 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
1266 struct cypress_private *priv = usb_get_serial_port_data(port);
1267 struct tty_struct *tty;
1268 unsigned char *data = urb->transfer_buffer;
1269 unsigned long flags;
1270 char tty_flag = TTY_NORMAL;
1276 dbg("%s - port %d", __FUNCTION__, port->number);
1278 switch (urb->status) {
1279 case 0: /* success */
1284 /* precursor to disconnect so just go away */
1287 usb_clear_halt(port->serial->dev,0x81);
1290 /* something ugly is going on... */
1291 dev_err(&urb->dev->dev,"%s - unexpected nonzero read status received: %d\n",
1292 __FUNCTION__,urb->status);
1293 cypress_set_dead(port);
1297 spin_lock_irqsave(&priv->lock, flags);
1298 if (priv->rx_flags & THROTTLED) {
1299 dbg("%s - now throttling", __FUNCTION__);
1300 priv->rx_flags |= ACTUALLY_THROTTLED;
1301 spin_unlock_irqrestore(&priv->lock, flags);
1304 spin_unlock_irqrestore(&priv->lock, flags);
1308 dbg("%s - bad tty pointer - exiting", __FUNCTION__);
1312 spin_lock_irqsave(&priv->lock, flags);
1313 switch(urb->actual_length) {
1315 /* This is for the CY7C64013... */
1316 priv->current_status = data[0] & 0xF8;
1317 bytes = data[1] + 2;
1323 /* This is for the CY7C63743... */
1324 priv->current_status = data[0] & 0xF8;
1325 bytes = (data[0] & 0x07) + 1;
1331 dbg("%s - wrong packet size - received %d bytes",
1332 __FUNCTION__, urb->actual_length);
1333 spin_unlock_irqrestore(&priv->lock, flags);
1336 spin_unlock_irqrestore(&priv->lock, flags);
1338 usb_serial_debug_data (debug, &port->dev, __FUNCTION__,
1339 urb->actual_length, data);
1341 spin_lock_irqsave(&priv->lock, flags);
1342 /* check to see if status has changed */
1344 if (priv->current_status != priv->prev_status) {
1345 priv->diff_status |= priv->current_status ^
1347 wake_up_interruptible(&priv->delta_msr_wait);
1348 priv->prev_status = priv->current_status;
1351 spin_unlock_irqrestore(&priv->lock, flags);
1353 /* hangup, as defined in acm.c... this might be a bad place for it
1355 if (tty && !(tty->termios->c_cflag & CLOCAL) &&
1356 !(priv->current_status & UART_CD)) {
1357 dbg("%s - calling hangup", __FUNCTION__);
1362 /* There is one error bit... I'm assuming it is a parity error
1363 * indicator as the generic firmware will set this bit to 1 if a
1364 * parity error occurs.
1365 * I can not find reference to any other error events. */
1366 spin_lock_irqsave(&priv->lock, flags);
1367 if (priv->current_status & CYP_ERROR) {
1368 spin_unlock_irqrestore(&priv->lock, flags);
1369 tty_flag = TTY_PARITY;
1370 dbg("%s - Parity Error detected", __FUNCTION__);
1372 spin_unlock_irqrestore(&priv->lock, flags);
1374 /* process read if there is data other than line status */
1375 if (tty && (bytes > i)) {
1376 bytes = tty_buffer_request_room(tty, bytes);
1377 for (; i < bytes ; ++i) {
1378 dbg("pushing byte number %d - %d - %c", i, data[i],
1380 tty_insert_flip_char(tty, data[i], tty_flag);
1382 tty_flip_buffer_push(port->tty);
1385 spin_lock_irqsave(&priv->lock, flags);
1386 /* control and status byte(s) are also counted */
1387 priv->bytes_in += bytes;
1388 spin_unlock_irqrestore(&priv->lock, flags);
1392 /* Continue trying to always read... unless the port has closed. */
1394 if (port->open_count > 0 && priv->comm_is_ok) {
1395 usb_fill_int_urb(port->interrupt_in_urb, port->serial->dev,
1396 usb_rcvintpipe(port->serial->dev,
1397 port->interrupt_in_endpointAddress),
1398 port->interrupt_in_urb->transfer_buffer,
1399 port->interrupt_in_urb->transfer_buffer_length,
1400 cypress_read_int_callback, port, priv->read_urb_interval);
1401 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
1403 dev_err(&urb->dev->dev, "%s - failed resubmitting "
1404 "read urb, error %d\n", __FUNCTION__,
1406 cypress_set_dead(port);
1411 } /* cypress_read_int_callback */
1414 static void cypress_write_int_callback(struct urb *urb)
1416 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
1417 struct cypress_private *priv = usb_get_serial_port_data(port);
1420 dbg("%s - port %d", __FUNCTION__, port->number);
1422 switch (urb->status) {
1429 /* this urb is terminated, clean up */
1430 dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status);
1431 priv->write_urb_in_use = 0;
1433 case -EPIPE: /* no break needed; clear halt and resubmit */
1434 if (!priv->comm_is_ok)
1436 usb_clear_halt(port->serial->dev, 0x02);
1437 /* error in the urb, so we have to resubmit it */
1438 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
1439 port->interrupt_out_urb->transfer_buffer_length = 1;
1440 port->interrupt_out_urb->dev = port->serial->dev;
1441 result = usb_submit_urb(port->interrupt_out_urb, GFP_ATOMIC);
1444 dev_err(&urb->dev->dev, "%s - failed resubmitting write urb, error %d\n",
1445 __FUNCTION__, result);
1446 cypress_set_dead(port);
1449 dev_err(&urb->dev->dev,"%s - unexpected nonzero write status received: %d\n",
1450 __FUNCTION__,urb->status);
1451 cypress_set_dead(port);
1455 priv->write_urb_in_use = 0;
1457 /* send any buffered data */
1462 /*****************************************************************************
1463 * Write buffer functions - buffering code from pl2303 used
1464 *****************************************************************************/
1469 * Allocate a circular buffer and all associated memory.
1472 static struct cypress_buf *cypress_buf_alloc(unsigned int size)
1475 struct cypress_buf *cb;
1481 cb = kmalloc(sizeof(struct cypress_buf), GFP_KERNEL);
1485 cb->buf_buf = kmalloc(size, GFP_KERNEL);
1486 if (cb->buf_buf == NULL) {
1491 cb->buf_size = size;
1492 cb->buf_get = cb->buf_put = cb->buf_buf;
1502 * Free the buffer and all associated memory.
1505 static void cypress_buf_free(struct cypress_buf *cb)
1517 * Clear out all data in the circular buffer.
1520 static void cypress_buf_clear(struct cypress_buf *cb)
1523 cb->buf_get = cb->buf_put;
1524 /* equivalent to a get of all data available */
1529 * cypress_buf_data_avail
1531 * Return the number of bytes of data available in the circular
1535 static unsigned int cypress_buf_data_avail(struct cypress_buf *cb)
1538 return ((cb->buf_size + cb->buf_put - cb->buf_get) % cb->buf_size);
1545 * cypress_buf_space_avail
1547 * Return the number of bytes of space available in the circular
1551 static unsigned int cypress_buf_space_avail(struct cypress_buf *cb)
1554 return ((cb->buf_size + cb->buf_get - cb->buf_put - 1) % cb->buf_size);
1563 * Copy data data from a user buffer and put it into the circular buffer.
1564 * Restrict to the amount of space available.
1566 * Return the number of bytes copied.
1569 static unsigned int cypress_buf_put(struct cypress_buf *cb, const char *buf,
1579 len = cypress_buf_space_avail(cb);
1586 len = cb->buf_buf + cb->buf_size - cb->buf_put;
1588 memcpy(cb->buf_put, buf, len);
1589 memcpy(cb->buf_buf, buf+len, count - len);
1590 cb->buf_put = cb->buf_buf + count - len;
1592 memcpy(cb->buf_put, buf, count);
1594 cb->buf_put += count;
1595 else /* count == len */
1596 cb->buf_put = cb->buf_buf;
1607 * Get data from the circular buffer and copy to the given buffer.
1608 * Restrict to the amount of data available.
1610 * Return the number of bytes copied.
1613 static unsigned int cypress_buf_get(struct cypress_buf *cb, char *buf,
1623 len = cypress_buf_data_avail(cb);
1630 len = cb->buf_buf + cb->buf_size - cb->buf_get;
1632 memcpy(buf, cb->buf_get, len);
1633 memcpy(buf+len, cb->buf_buf, count - len);
1634 cb->buf_get = cb->buf_buf + count - len;
1636 memcpy(buf, cb->buf_get, count);
1638 cb->buf_get += count;
1639 else /* count == len */
1640 cb->buf_get = cb->buf_buf;
1647 /*****************************************************************************
1649 *****************************************************************************/
1651 static int __init cypress_init(void)
1655 dbg("%s", __FUNCTION__);
1657 retval = usb_serial_register(&cypress_earthmate_device);
1659 goto failed_em_register;
1660 retval = usb_serial_register(&cypress_hidcom_device);
1662 goto failed_hidcom_register;
1663 retval = usb_serial_register(&cypress_ca42v2_device);
1665 goto failed_ca42v2_register;
1666 retval = usb_register(&cypress_driver);
1668 goto failed_usb_register;
1670 info(DRIVER_DESC " " DRIVER_VERSION);
1673 failed_usb_register:
1674 usb_serial_deregister(&cypress_ca42v2_device);
1675 failed_ca42v2_register:
1676 usb_serial_deregister(&cypress_hidcom_device);
1677 failed_hidcom_register:
1678 usb_serial_deregister(&cypress_earthmate_device);
1684 static void __exit cypress_exit (void)
1686 dbg("%s", __FUNCTION__);
1688 usb_deregister (&cypress_driver);
1689 usb_serial_deregister (&cypress_earthmate_device);
1690 usb_serial_deregister (&cypress_hidcom_device);
1691 usb_serial_deregister (&cypress_ca42v2_device);
1695 module_init(cypress_init);
1696 module_exit(cypress_exit);
1698 MODULE_AUTHOR( DRIVER_AUTHOR );
1699 MODULE_DESCRIPTION( DRIVER_DESC );
1700 MODULE_VERSION( DRIVER_VERSION );
1701 MODULE_LICENSE("GPL");
1703 module_param(debug, bool, S_IRUGO | S_IWUSR);
1704 MODULE_PARM_DESC(debug, "Debug enabled or not");
1705 module_param(stats, bool, S_IRUGO | S_IWUSR);
1706 MODULE_PARM_DESC(stats, "Enable statistics or not");
1707 module_param(interval, int, S_IRUGO | S_IWUSR);
1708 MODULE_PARM_DESC(interval, "Overrides interrupt interval");