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 { USB_DEVICE(VENDOR_ID_POWERCOM, PRODUCT_ID_UPS) },
98 { } /* Terminating entry */
101 static struct usb_device_id id_table_nokiaca42v2 [] = {
102 { USB_DEVICE(VENDOR_ID_DAZZLE, PRODUCT_ID_CA42) },
103 { } /* Terminating entry */
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 */
115 MODULE_DEVICE_TABLE (usb, id_table_combined);
117 static struct usb_driver cypress_driver = {
119 .probe = usb_serial_probe,
120 .disconnect = usb_serial_disconnect,
121 .id_table = id_table_combined,
126 packet_format_1, /* b0:status, b1:payload count */
127 packet_format_2 /* b0[7:3]:status, b0[2:0]:payload count */
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 baud_rate; /* stores current baud rate in integer form */
149 int cbr_mask; /* stores current baud rate in masked 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 */
157 /* write buffer structure */
159 unsigned int buf_size;
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 /* baud helper functions */
186 static int mask_to_rate (unsigned mask);
187 static unsigned rate_to_mask (int rate);
188 /* write buffer functions */
189 static struct cypress_buf *cypress_buf_alloc(unsigned int size);
190 static void cypress_buf_free(struct cypress_buf *cb);
191 static void cypress_buf_clear(struct cypress_buf *cb);
192 static unsigned int cypress_buf_data_avail(struct cypress_buf *cb);
193 static unsigned int cypress_buf_space_avail(struct cypress_buf *cb);
194 static unsigned int cypress_buf_put(struct cypress_buf *cb, const char *buf, unsigned int count);
195 static unsigned int cypress_buf_get(struct cypress_buf *cb, char *buf, unsigned int count);
198 static struct usb_serial_driver cypress_earthmate_device = {
200 .owner = THIS_MODULE,
203 .description = "DeLorme Earthmate USB",
204 .usb_driver = &cypress_driver,
205 .id_table = id_table_earthmate,
206 .num_interrupt_in = 1,
207 .num_interrupt_out = 1,
208 .num_bulk_in = NUM_DONT_CARE,
209 .num_bulk_out = NUM_DONT_CARE,
211 .attach = cypress_earthmate_startup,
212 .shutdown = cypress_shutdown,
213 .open = cypress_open,
214 .close = cypress_close,
215 .write = cypress_write,
216 .write_room = cypress_write_room,
217 .ioctl = cypress_ioctl,
218 .set_termios = cypress_set_termios,
219 .tiocmget = cypress_tiocmget,
220 .tiocmset = cypress_tiocmset,
221 .chars_in_buffer = cypress_chars_in_buffer,
222 .throttle = cypress_throttle,
223 .unthrottle = cypress_unthrottle,
224 .read_int_callback = cypress_read_int_callback,
225 .write_int_callback = cypress_write_int_callback,
228 static struct usb_serial_driver cypress_hidcom_device = {
230 .owner = THIS_MODULE,
233 .description = "HID->COM RS232 Adapter",
234 .usb_driver = &cypress_driver,
235 .id_table = id_table_cyphidcomrs232,
236 .num_interrupt_in = 1,
237 .num_interrupt_out = 1,
238 .num_bulk_in = NUM_DONT_CARE,
239 .num_bulk_out = NUM_DONT_CARE,
241 .attach = cypress_hidcom_startup,
242 .shutdown = cypress_shutdown,
243 .open = cypress_open,
244 .close = cypress_close,
245 .write = cypress_write,
246 .write_room = cypress_write_room,
247 .ioctl = cypress_ioctl,
248 .set_termios = cypress_set_termios,
249 .tiocmget = cypress_tiocmget,
250 .tiocmset = cypress_tiocmset,
251 .chars_in_buffer = cypress_chars_in_buffer,
252 .throttle = cypress_throttle,
253 .unthrottle = cypress_unthrottle,
254 .read_int_callback = cypress_read_int_callback,
255 .write_int_callback = cypress_write_int_callback,
258 static struct usb_serial_driver cypress_ca42v2_device = {
260 .owner = THIS_MODULE,
261 .name = "nokiaca42v2",
263 .description = "Nokia CA-42 V2 Adapter",
264 .usb_driver = &cypress_driver,
265 .id_table = id_table_nokiaca42v2,
266 .num_interrupt_in = 1,
267 .num_interrupt_out = 1,
268 .num_bulk_in = NUM_DONT_CARE,
269 .num_bulk_out = NUM_DONT_CARE,
271 .attach = cypress_ca42v2_startup,
272 .shutdown = cypress_shutdown,
273 .open = cypress_open,
274 .close = cypress_close,
275 .write = cypress_write,
276 .write_room = cypress_write_room,
277 .ioctl = cypress_ioctl,
278 .set_termios = cypress_set_termios,
279 .tiocmget = cypress_tiocmget,
280 .tiocmset = cypress_tiocmset,
281 .chars_in_buffer = cypress_chars_in_buffer,
282 .throttle = cypress_throttle,
283 .unthrottle = cypress_unthrottle,
284 .read_int_callback = cypress_read_int_callback,
285 .write_int_callback = cypress_write_int_callback,
288 /*****************************************************************************
289 * Cypress serial helper functions
290 *****************************************************************************/
293 /* This function can either set or retrieve the current serial line settings */
294 static int cypress_serial_control (struct usb_serial_port *port, unsigned baud_mask, int data_bits, int stop_bits,
295 int parity_enable, int parity_type, int reset, int cypress_request_type)
297 int new_baudrate = 0, retval = 0, tries = 0;
298 struct cypress_private *priv;
299 __u8 feature_buffer[5];
302 dbg("%s", __FUNCTION__);
304 priv = usb_get_serial_port_data(port);
306 if (!priv->comm_is_ok)
309 switch(cypress_request_type) {
310 case CYPRESS_SET_CONFIG:
313 * The general purpose firmware for the Cypress M8 allows for a maximum speed
314 * of 57600bps (I have no idea whether DeLorme chose to use the general purpose
315 * firmware or not), if you need to modify this speed setting for your own
316 * project please add your own chiptype and modify the code likewise. The
317 * Cypress HID->COM device will work successfully up to 115200bps (but the
318 * actual throughput is around 3kBps).
320 if (baud_mask != priv->cbr_mask) {
321 dbg("%s - baud rate is changing", __FUNCTION__);
322 if ( priv->chiptype == CT_EARTHMATE ) {
323 /* 300 and 600 baud rates are supported under the generic firmware,
324 * but are not used with NMEA and SiRF protocols */
326 if ( (baud_mask == B300) || (baud_mask == B600) ) {
327 err("%s - failed setting baud rate, unsupported speed",
329 new_baudrate = priv->baud_rate;
330 } else if ( (new_baudrate = mask_to_rate(baud_mask)) == -1) {
331 err("%s - failed setting baud rate, unsupported speed",
333 new_baudrate = priv->baud_rate;
335 } else if (priv->chiptype == CT_CYPHIDCOM) {
336 if ( (new_baudrate = mask_to_rate(baud_mask)) == -1) {
337 err("%s - failed setting baud rate, unsupported speed",
339 new_baudrate = priv->baud_rate;
341 } else if (priv->chiptype == CT_CA42V2) {
342 if ( (new_baudrate = mask_to_rate(baud_mask)) == -1) {
343 err("%s - failed setting baud rate, unsupported speed",
345 new_baudrate = priv->baud_rate;
347 } else if (priv->chiptype == CT_GENERIC) {
348 if ( (new_baudrate = mask_to_rate(baud_mask)) == -1) {
349 err("%s - failed setting baud rate, unsupported speed",
351 new_baudrate = priv->baud_rate;
354 info("%s - please define your chiptype", __FUNCTION__);
355 new_baudrate = priv->baud_rate;
357 } else { /* baud rate not changing, keep the old */
358 new_baudrate = priv->baud_rate;
360 dbg("%s - baud rate is being sent as %d", __FUNCTION__, new_baudrate);
362 memset(feature_buffer, 0, sizeof(feature_buffer));
363 /* fill the feature_buffer with new configuration */
364 *((u_int32_t *)feature_buffer) = new_baudrate;
366 feature_buffer[4] |= data_bits; /* assign data bits in 2 bit space ( max 3 ) */
368 feature_buffer[4] |= (stop_bits << 3); /* assign stop bits in 1 bit space */
369 feature_buffer[4] |= (parity_enable << 4); /* assign parity flag in 1 bit space */
370 feature_buffer[4] |= (parity_type << 5); /* assign parity type in 1 bit space */
372 feature_buffer[4] |= (reset << 7); /* assign reset at end of byte, 1 bit space */
374 dbg("%s - device is being sent this feature report:", __FUNCTION__);
375 dbg("%s - %02X - %02X - %02X - %02X - %02X", __FUNCTION__, feature_buffer[0], feature_buffer[1],
376 feature_buffer[2], feature_buffer[3], feature_buffer[4]);
379 retval = usb_control_msg(port->serial->dev,
380 usb_sndctrlpipe(port->serial->dev, 0),
382 USB_DIR_OUT | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
383 0x0300, 0, feature_buffer,
384 sizeof(feature_buffer), 500);
389 } while (retval != sizeof(feature_buffer) &&
392 if (retval != sizeof(feature_buffer)) {
393 err("%s - failed sending serial line settings - %d", __FUNCTION__, retval);
394 cypress_set_dead(port);
396 spin_lock_irqsave(&priv->lock, flags);
397 priv->baud_rate = new_baudrate;
398 priv->cbr_mask = baud_mask;
399 priv->current_config = feature_buffer[4];
400 spin_unlock_irqrestore(&priv->lock, flags);
403 case CYPRESS_GET_CONFIG:
404 dbg("%s - retreiving serial line settings", __FUNCTION__);
405 /* set initial values in feature buffer */
406 memset(feature_buffer, 0, sizeof(feature_buffer));
409 retval = usb_control_msg(port->serial->dev,
410 usb_rcvctrlpipe(port->serial->dev, 0),
412 USB_DIR_IN | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
413 0x0300, 0, feature_buffer,
414 sizeof(feature_buffer), 500);
419 } while (retval != sizeof(feature_buffer) &&
422 if (retval != sizeof(feature_buffer)) {
423 err("%s - failed to retrieve serial line settings - %d", __FUNCTION__, retval);
424 cypress_set_dead(port);
427 spin_lock_irqsave(&priv->lock, flags);
429 /* store the config in one byte, and later use bit masks to check values */
430 priv->current_config = feature_buffer[4];
431 priv->baud_rate = *((u_int32_t *)feature_buffer);
433 if ( (priv->cbr_mask = rate_to_mask(priv->baud_rate)) == 0x40)
434 dbg("%s - failed setting the baud mask (not defined)", __FUNCTION__);
435 spin_unlock_irqrestore(&priv->lock, flags);
438 spin_lock_irqsave(&priv->lock, flags);
440 spin_unlock_irqrestore(&priv->lock, flags);
443 } /* cypress_serial_control */
446 static void cypress_set_dead(struct usb_serial_port *port)
448 struct cypress_private *priv = usb_get_serial_port_data(port);
451 spin_lock_irqsave(&priv->lock, flags);
452 if (!priv->comm_is_ok) {
453 spin_unlock_irqrestore(&priv->lock, flags);
456 priv->comm_is_ok = 0;
457 spin_unlock_irqrestore(&priv->lock, flags);
459 err("cypress_m8 suspending failing port %d - interval might be too short",
464 /* given a baud mask, it will return integer baud on success */
465 static int mask_to_rate (unsigned mask)
470 case B0: rate = 0; break;
471 case B300: rate = 300; break;
472 case B600: rate = 600; break;
473 case B1200: rate = 1200; break;
474 case B2400: rate = 2400; break;
475 case B4800: rate = 4800; break;
476 case B9600: rate = 9600; break;
477 case B19200: rate = 19200; break;
478 case B38400: rate = 38400; break;
479 case B57600: rate = 57600; break;
480 case B115200: rate = 115200; break;
488 static unsigned rate_to_mask (int rate)
493 case 0: mask = B0; break;
494 case 300: mask = B300; break;
495 case 600: mask = B600; break;
496 case 1200: mask = B1200; break;
497 case 2400: mask = B2400; break;
498 case 4800: mask = B4800; break;
499 case 9600: mask = B9600; break;
500 case 19200: mask = B19200; break;
501 case 38400: mask = B38400; break;
502 case 57600: mask = B57600; break;
503 case 115200: mask = B115200; break;
504 default: mask = 0x40;
509 /*****************************************************************************
510 * Cypress serial driver functions
511 *****************************************************************************/
514 static int generic_startup (struct usb_serial *serial)
516 struct cypress_private *priv;
517 struct usb_serial_port *port = serial->port[0];
519 dbg("%s - port %d", __FUNCTION__, port->number);
521 priv = kzalloc(sizeof (struct cypress_private), GFP_KERNEL);
525 priv->comm_is_ok = !0;
526 spin_lock_init(&priv->lock);
527 priv->buf = cypress_buf_alloc(CYPRESS_BUF_SIZE);
528 if (priv->buf == NULL) {
532 init_waitqueue_head(&priv->delta_msr_wait);
534 usb_reset_configuration (serial->dev);
537 priv->line_control = 0;
538 priv->termios_initialized = 0;
540 priv->cbr_mask = B300;
541 /* Default packet format setting is determined by packet size.
542 Anything with a size larger then 9 must have a separate
543 count field since the 3 bit count field is otherwise too
544 small. Otherwise we can use the slightly more compact
545 format. This is in accordance with the cypress_m8 serial
546 converter app note. */
547 if (port->interrupt_out_size > 9) {
548 priv->pkt_fmt = packet_format_1;
550 priv->pkt_fmt = packet_format_2;
553 priv->write_urb_interval = interval;
554 priv->read_urb_interval = interval;
555 dbg("%s - port %d read & write intervals forced to %d",
556 __FUNCTION__,port->number,interval);
558 priv->write_urb_interval = port->interrupt_out_urb->interval;
559 priv->read_urb_interval = port->interrupt_in_urb->interval;
560 dbg("%s - port %d intervals: read=%d write=%d",
561 __FUNCTION__,port->number,
562 priv->read_urb_interval,priv->write_urb_interval);
564 usb_set_serial_port_data(port, priv);
570 static int cypress_earthmate_startup (struct usb_serial *serial)
572 struct cypress_private *priv;
574 dbg("%s", __FUNCTION__);
576 if (generic_startup(serial)) {
577 dbg("%s - Failed setting up port %d", __FUNCTION__,
578 serial->port[0]->number);
582 priv = usb_get_serial_port_data(serial->port[0]);
583 priv->chiptype = CT_EARTHMATE;
584 /* All Earthmate devices use the separated-count packet
586 priv->pkt_fmt = packet_format_1;
589 } /* cypress_earthmate_startup */
592 static int cypress_hidcom_startup (struct usb_serial *serial)
594 struct cypress_private *priv;
596 dbg("%s", __FUNCTION__);
598 if (generic_startup(serial)) {
599 dbg("%s - Failed setting up port %d", __FUNCTION__,
600 serial->port[0]->number);
604 priv = usb_get_serial_port_data(serial->port[0]);
605 priv->chiptype = CT_CYPHIDCOM;
608 } /* cypress_hidcom_startup */
611 static int cypress_ca42v2_startup (struct usb_serial *serial)
613 struct cypress_private *priv;
615 dbg("%s", __FUNCTION__);
617 if (generic_startup(serial)) {
618 dbg("%s - Failed setting up port %d", __FUNCTION__,
619 serial->port[0]->number);
623 priv = usb_get_serial_port_data(serial->port[0]);
624 priv->chiptype = CT_CA42V2;
627 } /* cypress_ca42v2_startup */
630 static void cypress_shutdown (struct usb_serial *serial)
632 struct cypress_private *priv;
634 dbg ("%s - port %d", __FUNCTION__, serial->port[0]->number);
636 /* all open ports are closed at this point */
638 priv = usb_get_serial_port_data(serial->port[0]);
641 cypress_buf_free(priv->buf);
643 usb_set_serial_port_data(serial->port[0], NULL);
648 static int cypress_open (struct usb_serial_port *port, struct file *filp)
650 struct cypress_private *priv = usb_get_serial_port_data(port);
651 struct usb_serial *serial = port->serial;
655 dbg("%s - port %d", __FUNCTION__, port->number);
657 if (!priv->comm_is_ok)
660 /* clear halts before open */
661 usb_clear_halt(serial->dev, 0x81);
662 usb_clear_halt(serial->dev, 0x02);
664 spin_lock_irqsave(&priv->lock, flags);
665 /* reset read/write statistics */
670 spin_unlock_irqrestore(&priv->lock, flags);
672 /* setting to zero could cause data loss */
673 port->tty->low_latency = 1;
675 /* raise both lines and set termios */
676 spin_lock_irqsave(&priv->lock, flags);
677 priv->line_control = CONTROL_DTR | CONTROL_RTS;
679 spin_unlock_irqrestore(&priv->lock, flags);
680 result = cypress_write(port, NULL, 0);
683 dev_err(&port->dev, "%s - failed setting the control lines - error %d\n", __FUNCTION__, result);
686 dbg("%s - success setting the control lines", __FUNCTION__);
688 cypress_set_termios(port, &priv->tmp_termios);
690 /* setup the port and start reading from the device */
691 if(!port->interrupt_in_urb){
692 err("%s - interrupt_in_urb is empty!", __FUNCTION__);
696 usb_fill_int_urb(port->interrupt_in_urb, serial->dev,
697 usb_rcvintpipe(serial->dev, port->interrupt_in_endpointAddress),
698 port->interrupt_in_urb->transfer_buffer, port->interrupt_in_urb->transfer_buffer_length,
699 cypress_read_int_callback, port, priv->read_urb_interval);
700 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
703 dev_err(&port->dev, "%s - failed submitting read urb, error %d\n", __FUNCTION__, result);
704 cypress_set_dead(port);
711 static void cypress_close(struct usb_serial_port *port, struct file * filp)
713 struct cypress_private *priv = usb_get_serial_port_data(port);
714 unsigned int c_cflag;
719 dbg("%s - port %d", __FUNCTION__, port->number);
721 /* wait for data to drain from buffer */
722 spin_lock_irq(&priv->lock);
723 timeout = CYPRESS_CLOSING_WAIT;
724 init_waitqueue_entry(&wait, current);
725 add_wait_queue(&port->tty->write_wait, &wait);
727 set_current_state(TASK_INTERRUPTIBLE);
728 if (cypress_buf_data_avail(priv->buf) == 0
729 || timeout == 0 || signal_pending(current)
730 /* without mutex, allowed due to harmless failure mode */
731 || port->serial->disconnected)
733 spin_unlock_irq(&priv->lock);
734 timeout = schedule_timeout(timeout);
735 spin_lock_irq(&priv->lock);
737 set_current_state(TASK_RUNNING);
738 remove_wait_queue(&port->tty->write_wait, &wait);
739 /* clear out any remaining data in the buffer */
740 cypress_buf_clear(priv->buf);
741 spin_unlock_irq(&priv->lock);
743 /* writing is potentially harmful, lock must be taken */
744 mutex_lock(&port->serial->disc_mutex);
745 if (port->serial->disconnected) {
746 mutex_unlock(&port->serial->disc_mutex);
749 /* wait for characters to drain from device */
750 bps = tty_get_baud_rate(port->tty);
752 timeout = max((HZ*2560)/bps,HZ/10);
755 schedule_timeout_interruptible(timeout);
757 dbg("%s - stopping urbs", __FUNCTION__);
758 usb_kill_urb (port->interrupt_in_urb);
759 usb_kill_urb (port->interrupt_out_urb);
762 c_cflag = port->tty->termios->c_cflag;
763 if (c_cflag & HUPCL) {
764 /* drop dtr and rts */
765 priv = usb_get_serial_port_data(port);
766 spin_lock_irq(&priv->lock);
767 priv->line_control = 0;
769 spin_unlock_irq(&priv->lock);
770 cypress_write(port, NULL, 0);
775 dev_info (&port->dev, "Statistics: %d Bytes In | %d Bytes Out | %d Commands Issued\n",
776 priv->bytes_in, priv->bytes_out, priv->cmd_count);
777 mutex_unlock(&port->serial->disc_mutex);
778 } /* cypress_close */
781 static int cypress_write(struct usb_serial_port *port, const unsigned char *buf, int count)
783 struct cypress_private *priv = usb_get_serial_port_data(port);
786 dbg("%s - port %d, %d bytes", __FUNCTION__, port->number, count);
788 /* line control commands, which need to be executed immediately,
789 are not put into the buffer for obvious reasons.
791 if (priv->cmd_ctrl) {
799 spin_lock_irqsave(&priv->lock, flags);
800 count = cypress_buf_put(priv->buf, buf, count);
801 spin_unlock_irqrestore(&priv->lock, flags);
807 } /* cypress_write */
810 static void cypress_send(struct usb_serial_port *port)
812 int count = 0, result, offset, actual_size;
813 struct cypress_private *priv = usb_get_serial_port_data(port);
816 if (!priv->comm_is_ok)
819 dbg("%s - port %d", __FUNCTION__, port->number);
820 dbg("%s - interrupt out size is %d", __FUNCTION__, port->interrupt_out_size);
822 spin_lock_irqsave(&priv->lock, flags);
823 if (priv->write_urb_in_use) {
824 dbg("%s - can't write, urb in use", __FUNCTION__);
825 spin_unlock_irqrestore(&priv->lock, flags);
828 spin_unlock_irqrestore(&priv->lock, flags);
831 memset(port->interrupt_out_urb->transfer_buffer, 0, port->interrupt_out_size);
833 spin_lock_irqsave(&priv->lock, flags);
834 switch (priv->pkt_fmt) {
836 case packet_format_1:
837 /* this is for the CY7C64013... */
839 port->interrupt_out_buffer[0] = priv->line_control;
841 case packet_format_2:
842 /* this is for the CY7C63743... */
844 port->interrupt_out_buffer[0] = priv->line_control;
848 if (priv->line_control & CONTROL_RESET)
849 priv->line_control &= ~CONTROL_RESET;
851 if (priv->cmd_ctrl) {
853 dbg("%s - line control command being issued", __FUNCTION__);
854 spin_unlock_irqrestore(&priv->lock, flags);
857 spin_unlock_irqrestore(&priv->lock, flags);
859 count = cypress_buf_get(priv->buf, &port->interrupt_out_buffer[offset],
860 port->interrupt_out_size-offset);
866 switch (priv->pkt_fmt) {
868 case packet_format_1:
869 port->interrupt_out_buffer[1] = count;
871 case packet_format_2:
872 port->interrupt_out_buffer[0] |= count;
875 dbg("%s - count is %d", __FUNCTION__, count);
878 spin_lock_irqsave(&priv->lock, flags);
879 priv->write_urb_in_use = 1;
880 spin_unlock_irqrestore(&priv->lock, flags);
885 actual_size = count +
886 (priv->pkt_fmt == packet_format_1 ? 2 : 1);
888 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, port->interrupt_out_size,
889 port->interrupt_out_urb->transfer_buffer);
891 usb_fill_int_urb(port->interrupt_out_urb, port->serial->dev,
892 usb_sndintpipe(port->serial->dev, port->interrupt_out_endpointAddress),
893 port->interrupt_out_buffer, port->interrupt_out_size,
894 cypress_write_int_callback, port, priv->write_urb_interval);
895 result = usb_submit_urb (port->interrupt_out_urb, GFP_ATOMIC);
897 dev_err(&port->dev, "%s - failed submitting write urb, error %d\n", __FUNCTION__,
899 priv->write_urb_in_use = 0;
900 cypress_set_dead(port);
903 spin_lock_irqsave(&priv->lock, flags);
904 if (priv->cmd_ctrl) {
907 priv->bytes_out += count; /* do not count the line control and size bytes */
908 spin_unlock_irqrestore(&priv->lock, flags);
910 usb_serial_port_softint(port);
914 /* returns how much space is available in the soft buffer */
915 static int cypress_write_room(struct usb_serial_port *port)
917 struct cypress_private *priv = usb_get_serial_port_data(port);
921 dbg("%s - port %d", __FUNCTION__, port->number);
923 spin_lock_irqsave(&priv->lock, flags);
924 room = cypress_buf_space_avail(priv->buf);
925 spin_unlock_irqrestore(&priv->lock, flags);
927 dbg("%s - returns %d", __FUNCTION__, room);
932 static int cypress_tiocmget (struct usb_serial_port *port, struct file *file)
934 struct cypress_private *priv = usb_get_serial_port_data(port);
935 __u8 status, control;
936 unsigned int result = 0;
939 dbg("%s - port %d", __FUNCTION__, port->number);
941 spin_lock_irqsave(&priv->lock, flags);
942 control = priv->line_control;
943 status = priv->current_status;
944 spin_unlock_irqrestore(&priv->lock, flags);
946 result = ((control & CONTROL_DTR) ? TIOCM_DTR : 0)
947 | ((control & CONTROL_RTS) ? TIOCM_RTS : 0)
948 | ((status & UART_CTS) ? TIOCM_CTS : 0)
949 | ((status & UART_DSR) ? TIOCM_DSR : 0)
950 | ((status & UART_RI) ? TIOCM_RI : 0)
951 | ((status & UART_CD) ? TIOCM_CD : 0);
953 dbg("%s - result = %x", __FUNCTION__, result);
959 static int cypress_tiocmset (struct usb_serial_port *port, struct file *file,
960 unsigned int set, unsigned int clear)
962 struct cypress_private *priv = usb_get_serial_port_data(port);
965 dbg("%s - port %d", __FUNCTION__, port->number);
967 spin_lock_irqsave(&priv->lock, flags);
969 priv->line_control |= CONTROL_RTS;
971 priv->line_control |= CONTROL_DTR;
972 if (clear & TIOCM_RTS)
973 priv->line_control &= ~CONTROL_RTS;
974 if (clear & TIOCM_DTR)
975 priv->line_control &= ~CONTROL_DTR;
976 spin_unlock_irqrestore(&priv->lock, flags);
979 return cypress_write(port, NULL, 0);
983 static int cypress_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg)
985 struct cypress_private *priv = usb_get_serial_port_data(port);
987 dbg("%s - port %d, cmd 0x%.4x", __FUNCTION__, port->number, cmd);
991 if (copy_to_user((void __user *)arg, port->tty->termios, sizeof(struct ktermios))) {
997 if (copy_from_user(port->tty->termios, (void __user *)arg, sizeof(struct ktermios))) {
1000 /* here we need to call cypress_set_termios to invoke the new settings */
1001 cypress_set_termios(port, &priv->tmp_termios);
1004 /* This code comes from drivers/char/serial.c and ftdi_sio.c */
1006 while (priv != NULL) {
1007 interruptible_sleep_on(&priv->delta_msr_wait);
1008 /* see if a signal did it */
1009 if (signal_pending(current))
1010 return -ERESTARTSYS;
1012 char diff = priv->diff_status;
1015 return -EIO; /* no change => error */
1018 /* consume all events */
1019 priv->diff_status = 0;
1021 /* return 0 if caller wanted to know about these bits */
1022 if ( ((arg & TIOCM_RNG) && (diff & UART_RI)) ||
1023 ((arg & TIOCM_DSR) && (diff & UART_DSR)) ||
1024 ((arg & TIOCM_CD) && (diff & UART_CD)) ||
1025 ((arg & TIOCM_CTS) && (diff & UART_CTS)) ) {
1028 /* otherwise caller can't care less about what happened,
1029 * and so we continue to wait for more events.
1039 dbg("%s - arg not supported - it was 0x%04x - check include/asm/ioctls.h", __FUNCTION__, cmd);
1041 return -ENOIOCTLCMD;
1042 } /* cypress_ioctl */
1045 static void cypress_set_termios (struct usb_serial_port *port,
1046 struct ktermios *old_termios)
1048 struct cypress_private *priv = usb_get_serial_port_data(port);
1049 struct tty_struct *tty;
1050 int data_bits, stop_bits, parity_type, parity_enable;
1051 unsigned cflag, iflag, baud_mask;
1052 unsigned long flags;
1056 dbg("%s - port %d", __FUNCTION__, port->number);
1059 if ((!tty) || (!tty->termios)) {
1060 dbg("%s - no tty structures", __FUNCTION__);
1064 spin_lock_irqsave(&priv->lock, flags);
1065 if (!priv->termios_initialized) {
1066 if (priv->chiptype == CT_EARTHMATE) {
1067 *(tty->termios) = tty_std_termios;
1068 tty->termios->c_cflag = B4800 | CS8 | CREAD | HUPCL |
1070 } else if (priv->chiptype == CT_CYPHIDCOM) {
1071 *(tty->termios) = tty_std_termios;
1072 tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
1074 } else if (priv->chiptype == CT_CA42V2) {
1075 *(tty->termios) = tty_std_termios;
1076 tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
1079 priv->termios_initialized = 1;
1081 spin_unlock_irqrestore(&priv->lock, flags);
1083 cflag = tty->termios->c_cflag;
1084 iflag = tty->termios->c_iflag;
1086 /* check if there are new settings */
1088 if ((cflag != old_termios->c_cflag) ||
1089 (RELEVANT_IFLAG(iflag) !=
1090 RELEVANT_IFLAG(old_termios->c_iflag))) {
1091 dbg("%s - attempting to set new termios settings",
1093 /* should make a copy of this in case something goes
1094 * wrong in the function, we can restore it */
1095 spin_lock_irqsave(&priv->lock, flags);
1096 priv->tmp_termios = *(tty->termios);
1097 spin_unlock_irqrestore(&priv->lock, flags);
1099 dbg("%s - nothing to do, exiting", __FUNCTION__);
1105 /* set number of data bits, parity, stop bits */
1106 /* when parity is disabled the parity type bit is ignored */
1108 /* 1 means 2 stop bits, 0 means 1 stop bit */
1109 stop_bits = cflag & CSTOPB ? 1 : 0;
1111 if (cflag & PARENB) {
1113 /* 1 means odd parity, 0 means even parity */
1114 parity_type = cflag & PARODD ? 1 : 0;
1116 parity_enable = parity_type = 0;
1118 if (cflag & CSIZE) {
1119 switch (cflag & CSIZE) {
1133 err("%s - CSIZE was set, but not CS5-CS8",
1140 spin_lock_irqsave(&priv->lock, flags);
1141 oldlines = priv->line_control;
1142 if ((cflag & CBAUD) == B0) {
1143 /* drop dtr and rts */
1144 dbg("%s - dropping the lines, baud rate 0bps", __FUNCTION__);
1146 priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
1148 baud_mask = (cflag & CBAUD);
1151 dbg("%s - setting baud 300bps", __FUNCTION__);
1154 dbg("%s - setting baud 600bps", __FUNCTION__);
1157 dbg("%s - setting baud 1200bps", __FUNCTION__);
1160 dbg("%s - setting baud 2400bps", __FUNCTION__);
1163 dbg("%s - setting baud 4800bps", __FUNCTION__);
1166 dbg("%s - setting baud 9600bps", __FUNCTION__);
1169 dbg("%s - setting baud 19200bps", __FUNCTION__);
1172 dbg("%s - setting baud 38400bps", __FUNCTION__);
1175 dbg("%s - setting baud 57600bps", __FUNCTION__);
1178 dbg("%s - setting baud 115200bps", __FUNCTION__);
1181 dbg("%s - unknown masked baud rate", __FUNCTION__);
1183 priv->line_control = (CONTROL_DTR | CONTROL_RTS);
1185 spin_unlock_irqrestore(&priv->lock, flags);
1187 dbg("%s - sending %d stop_bits, %d parity_enable, %d parity_type, "
1188 "%d data_bits (+5)", __FUNCTION__, stop_bits,
1189 parity_enable, parity_type, data_bits);
1191 cypress_serial_control(port, baud_mask, data_bits, stop_bits,
1192 parity_enable, parity_type, 0, CYPRESS_SET_CONFIG);
1194 /* we perform a CYPRESS_GET_CONFIG so that the current settings are
1195 * filled into the private structure this should confirm that all is
1196 * working if it returns what we just set */
1197 cypress_serial_control(port, 0, 0, 0, 0, 0, 0, CYPRESS_GET_CONFIG);
1199 /* Here we can define custom tty settings for devices; the main tty
1200 * termios flag base comes from empeg.c */
1202 spin_lock_irqsave(&priv->lock, flags);
1203 if ( (priv->chiptype == CT_EARTHMATE) && (priv->baud_rate == 4800) ) {
1204 dbg("Using custom termios settings for a baud rate of "
1206 /* define custom termios settings for NMEA protocol */
1208 tty->termios->c_iflag /* input modes - */
1209 &= ~(IGNBRK /* disable ignore break */
1210 | BRKINT /* disable break causes interrupt */
1211 | PARMRK /* disable mark parity errors */
1212 | ISTRIP /* disable clear high bit of input char */
1213 | INLCR /* disable translate NL to CR */
1214 | IGNCR /* disable ignore CR */
1215 | ICRNL /* disable translate CR to NL */
1216 | IXON); /* disable enable XON/XOFF flow control */
1218 tty->termios->c_oflag /* output modes */
1219 &= ~OPOST; /* disable postprocess output char */
1221 tty->termios->c_lflag /* line discipline modes */
1222 &= ~(ECHO /* disable echo input characters */
1223 | ECHONL /* disable echo new line */
1224 | ICANON /* disable erase, kill, werase, and rprnt
1225 special characters */
1226 | ISIG /* disable interrupt, quit, and suspend
1227 special characters */
1228 | IEXTEN); /* disable non-POSIX special characters */
1229 } /* CT_CYPHIDCOM: Application should handle this for device */
1231 linechange = (priv->line_control != oldlines);
1232 spin_unlock_irqrestore(&priv->lock, flags);
1234 /* if necessary, set lines */
1237 cypress_write(port, NULL, 0);
1239 } /* cypress_set_termios */
1242 /* returns amount of data still left in soft buffer */
1243 static int cypress_chars_in_buffer(struct usb_serial_port *port)
1245 struct cypress_private *priv = usb_get_serial_port_data(port);
1247 unsigned long flags;
1249 dbg("%s - port %d", __FUNCTION__, port->number);
1251 spin_lock_irqsave(&priv->lock, flags);
1252 chars = cypress_buf_data_avail(priv->buf);
1253 spin_unlock_irqrestore(&priv->lock, flags);
1255 dbg("%s - returns %d", __FUNCTION__, chars);
1260 static void cypress_throttle (struct usb_serial_port *port)
1262 struct cypress_private *priv = usb_get_serial_port_data(port);
1263 unsigned long flags;
1265 dbg("%s - port %d", __FUNCTION__, port->number);
1267 spin_lock_irqsave(&priv->lock, flags);
1268 priv->rx_flags = THROTTLED;
1269 spin_unlock_irqrestore(&priv->lock, flags);
1273 static void cypress_unthrottle (struct usb_serial_port *port)
1275 struct cypress_private *priv = usb_get_serial_port_data(port);
1276 int actually_throttled, result;
1277 unsigned long flags;
1279 dbg("%s - port %d", __FUNCTION__, port->number);
1281 spin_lock_irqsave(&priv->lock, flags);
1282 actually_throttled = priv->rx_flags & ACTUALLY_THROTTLED;
1284 spin_unlock_irqrestore(&priv->lock, flags);
1286 if (!priv->comm_is_ok)
1289 if (actually_throttled) {
1290 port->interrupt_in_urb->dev = port->serial->dev;
1292 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
1294 dev_err(&port->dev, "%s - failed submitting read urb, "
1295 "error %d\n", __FUNCTION__, result);
1296 cypress_set_dead(port);
1302 static void cypress_read_int_callback(struct urb *urb)
1304 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
1305 struct cypress_private *priv = usb_get_serial_port_data(port);
1306 struct tty_struct *tty;
1307 unsigned char *data = urb->transfer_buffer;
1308 unsigned long flags;
1309 char tty_flag = TTY_NORMAL;
1314 int status = urb->status;
1316 dbg("%s - port %d", __FUNCTION__, port->number);
1319 case 0: /* success */
1324 /* precursor to disconnect so just go away */
1327 usb_clear_halt(port->serial->dev,0x81);
1330 /* something ugly is going on... */
1331 dev_err(&urb->dev->dev,"%s - unexpected nonzero read status received: %d\n",
1332 __FUNCTION__, status);
1333 cypress_set_dead(port);
1337 spin_lock_irqsave(&priv->lock, flags);
1338 if (priv->rx_flags & THROTTLED) {
1339 dbg("%s - now throttling", __FUNCTION__);
1340 priv->rx_flags |= ACTUALLY_THROTTLED;
1341 spin_unlock_irqrestore(&priv->lock, flags);
1344 spin_unlock_irqrestore(&priv->lock, flags);
1348 dbg("%s - bad tty pointer - exiting", __FUNCTION__);
1352 spin_lock_irqsave(&priv->lock, flags);
1353 result = urb->actual_length;
1354 switch (priv->pkt_fmt) {
1356 case packet_format_1:
1357 /* This is for the CY7C64013... */
1358 priv->current_status = data[0] & 0xF8;
1359 bytes = data[1] + 2;
1364 case packet_format_2:
1365 /* This is for the CY7C63743... */
1366 priv->current_status = data[0] & 0xF8;
1367 bytes = (data[0] & 0x07) + 1;
1373 spin_unlock_irqrestore(&priv->lock, flags);
1374 if (result < bytes) {
1375 dbg("%s - wrong packet size - received %d bytes but packet "
1376 "said %d bytes", __func__, result, bytes);
1380 usb_serial_debug_data (debug, &port->dev, __FUNCTION__,
1381 urb->actual_length, data);
1383 spin_lock_irqsave(&priv->lock, flags);
1384 /* check to see if status has changed */
1386 if (priv->current_status != priv->prev_status) {
1387 priv->diff_status |= priv->current_status ^
1389 wake_up_interruptible(&priv->delta_msr_wait);
1390 priv->prev_status = priv->current_status;
1393 spin_unlock_irqrestore(&priv->lock, flags);
1395 /* hangup, as defined in acm.c... this might be a bad place for it
1397 if (tty && !(tty->termios->c_cflag & CLOCAL) &&
1398 !(priv->current_status & UART_CD)) {
1399 dbg("%s - calling hangup", __FUNCTION__);
1404 /* There is one error bit... I'm assuming it is a parity error
1405 * indicator as the generic firmware will set this bit to 1 if a
1406 * parity error occurs.
1407 * I can not find reference to any other error events. */
1408 spin_lock_irqsave(&priv->lock, flags);
1409 if (priv->current_status & CYP_ERROR) {
1410 spin_unlock_irqrestore(&priv->lock, flags);
1411 tty_flag = TTY_PARITY;
1412 dbg("%s - Parity Error detected", __FUNCTION__);
1414 spin_unlock_irqrestore(&priv->lock, flags);
1416 /* process read if there is data other than line status */
1417 if (tty && (bytes > i)) {
1418 bytes = tty_buffer_request_room(tty, bytes);
1419 for (; i < bytes ; ++i) {
1420 dbg("pushing byte number %d - %d - %c", i, data[i],
1422 tty_insert_flip_char(tty, data[i], tty_flag);
1424 tty_flip_buffer_push(port->tty);
1427 spin_lock_irqsave(&priv->lock, flags);
1428 /* control and status byte(s) are also counted */
1429 priv->bytes_in += bytes;
1430 spin_unlock_irqrestore(&priv->lock, flags);
1434 /* Continue trying to always read... unless the port has closed. */
1436 if (port->open_count > 0 && priv->comm_is_ok) {
1437 usb_fill_int_urb(port->interrupt_in_urb, port->serial->dev,
1438 usb_rcvintpipe(port->serial->dev,
1439 port->interrupt_in_endpointAddress),
1440 port->interrupt_in_urb->transfer_buffer,
1441 port->interrupt_in_urb->transfer_buffer_length,
1442 cypress_read_int_callback, port, priv->read_urb_interval);
1443 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
1445 dev_err(&urb->dev->dev, "%s - failed resubmitting "
1446 "read urb, error %d\n", __FUNCTION__,
1448 cypress_set_dead(port);
1453 } /* cypress_read_int_callback */
1456 static void cypress_write_int_callback(struct urb *urb)
1458 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
1459 struct cypress_private *priv = usb_get_serial_port_data(port);
1461 int status = urb->status;
1463 dbg("%s - port %d", __FUNCTION__, port->number);
1472 /* this urb is terminated, clean up */
1473 dbg("%s - urb shutting down with status: %d",
1474 __FUNCTION__, status);
1475 priv->write_urb_in_use = 0;
1477 case -EPIPE: /* no break needed; clear halt and resubmit */
1478 if (!priv->comm_is_ok)
1480 usb_clear_halt(port->serial->dev, 0x02);
1481 /* error in the urb, so we have to resubmit it */
1482 dbg("%s - nonzero write bulk status received: %d",
1483 __FUNCTION__, status);
1484 port->interrupt_out_urb->transfer_buffer_length = 1;
1485 port->interrupt_out_urb->dev = port->serial->dev;
1486 result = usb_submit_urb(port->interrupt_out_urb, GFP_ATOMIC);
1489 dev_err(&urb->dev->dev, "%s - failed resubmitting write urb, error %d\n",
1490 __FUNCTION__, result);
1491 cypress_set_dead(port);
1494 dev_err(&urb->dev->dev,"%s - unexpected nonzero write status received: %d\n",
1495 __FUNCTION__, status);
1496 cypress_set_dead(port);
1500 priv->write_urb_in_use = 0;
1502 /* send any buffered data */
1507 /*****************************************************************************
1508 * Write buffer functions - buffering code from pl2303 used
1509 *****************************************************************************/
1514 * Allocate a circular buffer and all associated memory.
1517 static struct cypress_buf *cypress_buf_alloc(unsigned int size)
1520 struct cypress_buf *cb;
1526 cb = kmalloc(sizeof(struct cypress_buf), GFP_KERNEL);
1530 cb->buf_buf = kmalloc(size, GFP_KERNEL);
1531 if (cb->buf_buf == NULL) {
1536 cb->buf_size = size;
1537 cb->buf_get = cb->buf_put = cb->buf_buf;
1547 * Free the buffer and all associated memory.
1550 static void cypress_buf_free(struct cypress_buf *cb)
1562 * Clear out all data in the circular buffer.
1565 static void cypress_buf_clear(struct cypress_buf *cb)
1568 cb->buf_get = cb->buf_put;
1569 /* equivalent to a get of all data available */
1574 * cypress_buf_data_avail
1576 * Return the number of bytes of data available in the circular
1580 static unsigned int cypress_buf_data_avail(struct cypress_buf *cb)
1583 return ((cb->buf_size + cb->buf_put - cb->buf_get) % cb->buf_size);
1590 * cypress_buf_space_avail
1592 * Return the number of bytes of space available in the circular
1596 static unsigned int cypress_buf_space_avail(struct cypress_buf *cb)
1599 return ((cb->buf_size + cb->buf_get - cb->buf_put - 1) % cb->buf_size);
1608 * Copy data data from a user buffer and put it into the circular buffer.
1609 * Restrict to the amount of space available.
1611 * Return the number of bytes copied.
1614 static unsigned int cypress_buf_put(struct cypress_buf *cb, const char *buf,
1624 len = cypress_buf_space_avail(cb);
1631 len = cb->buf_buf + cb->buf_size - cb->buf_put;
1633 memcpy(cb->buf_put, buf, len);
1634 memcpy(cb->buf_buf, buf+len, count - len);
1635 cb->buf_put = cb->buf_buf + count - len;
1637 memcpy(cb->buf_put, buf, count);
1639 cb->buf_put += count;
1640 else /* count == len */
1641 cb->buf_put = cb->buf_buf;
1652 * Get data from the circular buffer and copy to the given buffer.
1653 * Restrict to the amount of data available.
1655 * Return the number of bytes copied.
1658 static unsigned int cypress_buf_get(struct cypress_buf *cb, char *buf,
1668 len = cypress_buf_data_avail(cb);
1675 len = cb->buf_buf + cb->buf_size - cb->buf_get;
1677 memcpy(buf, cb->buf_get, len);
1678 memcpy(buf+len, cb->buf_buf, count - len);
1679 cb->buf_get = cb->buf_buf + count - len;
1681 memcpy(buf, cb->buf_get, count);
1683 cb->buf_get += count;
1684 else /* count == len */
1685 cb->buf_get = cb->buf_buf;
1692 /*****************************************************************************
1694 *****************************************************************************/
1696 static int __init cypress_init(void)
1700 dbg("%s", __FUNCTION__);
1702 retval = usb_serial_register(&cypress_earthmate_device);
1704 goto failed_em_register;
1705 retval = usb_serial_register(&cypress_hidcom_device);
1707 goto failed_hidcom_register;
1708 retval = usb_serial_register(&cypress_ca42v2_device);
1710 goto failed_ca42v2_register;
1711 retval = usb_register(&cypress_driver);
1713 goto failed_usb_register;
1715 info(DRIVER_DESC " " DRIVER_VERSION);
1718 failed_usb_register:
1719 usb_serial_deregister(&cypress_ca42v2_device);
1720 failed_ca42v2_register:
1721 usb_serial_deregister(&cypress_hidcom_device);
1722 failed_hidcom_register:
1723 usb_serial_deregister(&cypress_earthmate_device);
1729 static void __exit cypress_exit (void)
1731 dbg("%s", __FUNCTION__);
1733 usb_deregister (&cypress_driver);
1734 usb_serial_deregister (&cypress_earthmate_device);
1735 usb_serial_deregister (&cypress_hidcom_device);
1736 usb_serial_deregister (&cypress_ca42v2_device);
1740 module_init(cypress_init);
1741 module_exit(cypress_exit);
1743 MODULE_AUTHOR( DRIVER_AUTHOR );
1744 MODULE_DESCRIPTION( DRIVER_DESC );
1745 MODULE_VERSION( DRIVER_VERSION );
1746 MODULE_LICENSE("GPL");
1748 module_param(debug, bool, S_IRUGO | S_IWUSR);
1749 MODULE_PARM_DESC(debug, "Debug enabled or not");
1750 module_param(stats, bool, S_IRUGO | S_IWUSR);
1751 MODULE_PARM_DESC(stats, "Enable statistics or not");
1752 module_param(interval, int, S_IRUGO | S_IWUSR);
1753 MODULE_PARM_DESC(interval, "Overrides interrupt interval");