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 get_cfg_unsafe; /* If true, the CYPRESS_GET_CONFIG is unsafe */
149 int baud_rate; /* stores current baud rate in integer form */
150 int cbr_mask; /* stores current baud rate in masked form */
151 int isthrottled; /* if throttled, discard reads */
152 wait_queue_head_t delta_msr_wait; /* used for TIOCMIWAIT */
153 char prev_status, diff_status; /* used for TIOCMIWAIT */
154 /* we pass a pointer to this as the arguement sent to cypress_set_termios old_termios */
155 struct ktermios tmp_termios; /* stores the old termios settings */
158 /* write buffer structure */
160 unsigned int buf_size;
166 /* function prototypes for the Cypress USB to serial device */
167 static int cypress_earthmate_startup (struct usb_serial *serial);
168 static int cypress_hidcom_startup (struct usb_serial *serial);
169 static int cypress_ca42v2_startup (struct usb_serial *serial);
170 static void cypress_shutdown (struct usb_serial *serial);
171 static int cypress_open (struct usb_serial_port *port, struct file *filp);
172 static void cypress_close (struct usb_serial_port *port, struct file *filp);
173 static int cypress_write (struct usb_serial_port *port, const unsigned char *buf, int count);
174 static void cypress_send (struct usb_serial_port *port);
175 static int cypress_write_room (struct usb_serial_port *port);
176 static int cypress_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg);
177 static void cypress_set_termios (struct usb_serial_port *port, struct ktermios * old);
178 static int cypress_tiocmget (struct usb_serial_port *port, struct file *file);
179 static int cypress_tiocmset (struct usb_serial_port *port, struct file *file, unsigned int set, unsigned int clear);
180 static int cypress_chars_in_buffer (struct usb_serial_port *port);
181 static void cypress_throttle (struct usb_serial_port *port);
182 static void cypress_unthrottle (struct usb_serial_port *port);
183 static void cypress_set_dead (struct usb_serial_port *port);
184 static void cypress_read_int_callback (struct urb *urb);
185 static void cypress_write_int_callback (struct urb *urb);
186 /* baud helper functions */
187 static int mask_to_rate (unsigned mask);
188 static unsigned rate_to_mask (int rate);
189 /* write buffer functions */
190 static struct cypress_buf *cypress_buf_alloc(unsigned int size);
191 static void cypress_buf_free(struct cypress_buf *cb);
192 static void cypress_buf_clear(struct cypress_buf *cb);
193 static unsigned int cypress_buf_data_avail(struct cypress_buf *cb);
194 static unsigned int cypress_buf_space_avail(struct cypress_buf *cb);
195 static unsigned int cypress_buf_put(struct cypress_buf *cb, const char *buf, unsigned int count);
196 static unsigned int cypress_buf_get(struct cypress_buf *cb, char *buf, unsigned int count);
199 static struct usb_serial_driver cypress_earthmate_device = {
201 .owner = THIS_MODULE,
204 .description = "DeLorme Earthmate USB",
205 .usb_driver = &cypress_driver,
206 .id_table = id_table_earthmate,
207 .num_interrupt_in = 1,
208 .num_interrupt_out = 1,
209 .num_bulk_in = NUM_DONT_CARE,
210 .num_bulk_out = NUM_DONT_CARE,
212 .attach = cypress_earthmate_startup,
213 .shutdown = cypress_shutdown,
214 .open = cypress_open,
215 .close = cypress_close,
216 .write = cypress_write,
217 .write_room = cypress_write_room,
218 .ioctl = cypress_ioctl,
219 .set_termios = cypress_set_termios,
220 .tiocmget = cypress_tiocmget,
221 .tiocmset = cypress_tiocmset,
222 .chars_in_buffer = cypress_chars_in_buffer,
223 .throttle = cypress_throttle,
224 .unthrottle = cypress_unthrottle,
225 .read_int_callback = cypress_read_int_callback,
226 .write_int_callback = cypress_write_int_callback,
229 static struct usb_serial_driver cypress_hidcom_device = {
231 .owner = THIS_MODULE,
234 .description = "HID->COM RS232 Adapter",
235 .usb_driver = &cypress_driver,
236 .id_table = id_table_cyphidcomrs232,
237 .num_interrupt_in = 1,
238 .num_interrupt_out = 1,
239 .num_bulk_in = NUM_DONT_CARE,
240 .num_bulk_out = NUM_DONT_CARE,
242 .attach = cypress_hidcom_startup,
243 .shutdown = cypress_shutdown,
244 .open = cypress_open,
245 .close = cypress_close,
246 .write = cypress_write,
247 .write_room = cypress_write_room,
248 .ioctl = cypress_ioctl,
249 .set_termios = cypress_set_termios,
250 .tiocmget = cypress_tiocmget,
251 .tiocmset = cypress_tiocmset,
252 .chars_in_buffer = cypress_chars_in_buffer,
253 .throttle = cypress_throttle,
254 .unthrottle = cypress_unthrottle,
255 .read_int_callback = cypress_read_int_callback,
256 .write_int_callback = cypress_write_int_callback,
259 static struct usb_serial_driver cypress_ca42v2_device = {
261 .owner = THIS_MODULE,
262 .name = "nokiaca42v2",
264 .description = "Nokia CA-42 V2 Adapter",
265 .usb_driver = &cypress_driver,
266 .id_table = id_table_nokiaca42v2,
267 .num_interrupt_in = 1,
268 .num_interrupt_out = 1,
269 .num_bulk_in = NUM_DONT_CARE,
270 .num_bulk_out = NUM_DONT_CARE,
272 .attach = cypress_ca42v2_startup,
273 .shutdown = cypress_shutdown,
274 .open = cypress_open,
275 .close = cypress_close,
276 .write = cypress_write,
277 .write_room = cypress_write_room,
278 .ioctl = cypress_ioctl,
279 .set_termios = cypress_set_termios,
280 .tiocmget = cypress_tiocmget,
281 .tiocmset = cypress_tiocmset,
282 .chars_in_buffer = cypress_chars_in_buffer,
283 .throttle = cypress_throttle,
284 .unthrottle = cypress_unthrottle,
285 .read_int_callback = cypress_read_int_callback,
286 .write_int_callback = cypress_write_int_callback,
289 /*****************************************************************************
290 * Cypress serial helper functions
291 *****************************************************************************/
294 static int analyze_baud_rate(struct usb_serial_port *port, unsigned baud_mask)
297 struct cypress_private *priv;
298 priv = usb_get_serial_port_data(port);
301 * The general purpose firmware for the Cypress M8 allows for
302 * a maximum speed of 57600bps (I have no idea whether DeLorme
303 * chose to use the general purpose firmware or not), if you
304 * need to modify this speed setting for your own project
305 * please add your own chiptype and modify the code likewise.
306 * The Cypress HID->COM device will work successfully up to
307 * 115200bps (but the actual throughput is around 3kBps).
309 new_rate = mask_to_rate(baud_mask);
311 dbg("%s - failed setting baud rate, untranslatable speed",
315 if (port->serial->dev->speed == USB_SPEED_LOW) {
317 * Mike Isely <isely@pobox.com> 2-Feb-2008: The
318 * Cypress app note that describes this mechanism
319 * states the the low-speed part can't handle more
320 * than 800 bytes/sec, in which case 4800 baud is the
321 * safest speed for a part like that.
323 if (new_rate > 4800) {
324 dbg("%s - failed setting baud rate, device incapable "
325 "speed %d", __func__, new_rate);
329 switch (priv->chiptype) {
331 if (new_rate <= 600) {
332 /* 300 and 600 baud rates are supported under
333 * the generic firmware, but are not used with
334 * NMEA and SiRF protocols */
335 dbg("%s - failed setting baud rate, unsupported speed "
336 "of %d on Earthmate GPS", __func__, new_rate);
347 /* This function can either set or retrieve the current serial line settings */
348 static int cypress_serial_control (struct usb_serial_port *port, unsigned baud_mask, int data_bits, int stop_bits,
349 int parity_enable, int parity_type, int reset, int cypress_request_type)
351 int new_baudrate = 0, retval = 0, tries = 0;
352 struct cypress_private *priv;
353 __u8 feature_buffer[5];
356 dbg("%s", __FUNCTION__);
358 priv = usb_get_serial_port_data(port);
360 if (!priv->comm_is_ok)
363 switch(cypress_request_type) {
364 case CYPRESS_SET_CONFIG:
365 new_baudrate = priv->baud_rate;
366 if (baud_mask != priv->cbr_mask) {
367 dbg("%s - baud rate is changing", __FUNCTION__);
368 retval = analyze_baud_rate(port, baud_mask);
370 new_baudrate = retval;
371 dbg("%s - New baud rate set to %d",
372 __func__, new_baudrate);
375 dbg("%s - baud rate is being sent as %d", __FUNCTION__, new_baudrate);
377 memset(feature_buffer, 0, sizeof(feature_buffer));
378 /* fill the feature_buffer with new configuration */
379 *((u_int32_t *)feature_buffer) = new_baudrate;
381 feature_buffer[4] |= data_bits; /* assign data bits in 2 bit space ( max 3 ) */
383 feature_buffer[4] |= (stop_bits << 3); /* assign stop bits in 1 bit space */
384 feature_buffer[4] |= (parity_enable << 4); /* assign parity flag in 1 bit space */
385 feature_buffer[4] |= (parity_type << 5); /* assign parity type in 1 bit space */
387 feature_buffer[4] |= (reset << 7); /* assign reset at end of byte, 1 bit space */
389 dbg("%s - device is being sent this feature report:", __FUNCTION__);
390 dbg("%s - %02X - %02X - %02X - %02X - %02X", __FUNCTION__, feature_buffer[0], feature_buffer[1],
391 feature_buffer[2], feature_buffer[3], feature_buffer[4]);
394 retval = usb_control_msg(port->serial->dev,
395 usb_sndctrlpipe(port->serial->dev, 0),
397 USB_DIR_OUT | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
398 0x0300, 0, feature_buffer,
399 sizeof(feature_buffer), 500);
404 } while (retval != sizeof(feature_buffer) &&
407 if (retval != sizeof(feature_buffer)) {
408 err("%s - failed sending serial line settings - %d", __FUNCTION__, retval);
409 cypress_set_dead(port);
411 spin_lock_irqsave(&priv->lock, flags);
412 priv->baud_rate = new_baudrate;
413 priv->cbr_mask = baud_mask;
414 priv->current_config = feature_buffer[4];
415 spin_unlock_irqrestore(&priv->lock, flags);
418 case CYPRESS_GET_CONFIG:
419 if (priv->get_cfg_unsafe) {
420 /* Not implemented for this device,
421 and if we try to do it we're likely
422 to crash the hardware. */
425 dbg("%s - retreiving serial line settings", __FUNCTION__);
426 /* set initial values in feature buffer */
427 memset(feature_buffer, 0, sizeof(feature_buffer));
430 retval = usb_control_msg(port->serial->dev,
431 usb_rcvctrlpipe(port->serial->dev, 0),
433 USB_DIR_IN | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
434 0x0300, 0, feature_buffer,
435 sizeof(feature_buffer), 500);
440 } while (retval != sizeof(feature_buffer) &&
443 if (retval != sizeof(feature_buffer)) {
444 err("%s - failed to retrieve serial line settings - %d", __FUNCTION__, retval);
445 cypress_set_dead(port);
448 spin_lock_irqsave(&priv->lock, flags);
450 /* store the config in one byte, and later use bit masks to check values */
451 priv->current_config = feature_buffer[4];
452 priv->baud_rate = *((u_int32_t *)feature_buffer);
454 if ( (priv->cbr_mask = rate_to_mask(priv->baud_rate)) == 0x40)
455 dbg("%s - failed setting the baud mask (not defined)", __FUNCTION__);
456 spin_unlock_irqrestore(&priv->lock, flags);
459 spin_lock_irqsave(&priv->lock, flags);
461 spin_unlock_irqrestore(&priv->lock, flags);
464 } /* cypress_serial_control */
467 static void cypress_set_dead(struct usb_serial_port *port)
469 struct cypress_private *priv = usb_get_serial_port_data(port);
472 spin_lock_irqsave(&priv->lock, flags);
473 if (!priv->comm_is_ok) {
474 spin_unlock_irqrestore(&priv->lock, flags);
477 priv->comm_is_ok = 0;
478 spin_unlock_irqrestore(&priv->lock, flags);
480 err("cypress_m8 suspending failing port %d - interval might be too short",
485 /* given a baud mask, it will return integer baud on success */
486 static int mask_to_rate (unsigned mask)
491 case B0: rate = 0; break;
492 case B300: rate = 300; break;
493 case B600: rate = 600; break;
494 case B1200: rate = 1200; break;
495 case B2400: rate = 2400; break;
496 case B4800: rate = 4800; break;
497 case B9600: rate = 9600; break;
498 case B19200: rate = 19200; break;
499 case B38400: rate = 38400; break;
500 case B57600: rate = 57600; break;
501 case B115200: rate = 115200; break;
509 static unsigned rate_to_mask (int rate)
514 case 0: mask = B0; break;
515 case 300: mask = B300; break;
516 case 600: mask = B600; break;
517 case 1200: mask = B1200; break;
518 case 2400: mask = B2400; break;
519 case 4800: mask = B4800; break;
520 case 9600: mask = B9600; break;
521 case 19200: mask = B19200; break;
522 case 38400: mask = B38400; break;
523 case 57600: mask = B57600; break;
524 case 115200: mask = B115200; break;
525 default: mask = 0x40;
530 /*****************************************************************************
531 * Cypress serial driver functions
532 *****************************************************************************/
535 static int generic_startup (struct usb_serial *serial)
537 struct cypress_private *priv;
538 struct usb_serial_port *port = serial->port[0];
540 dbg("%s - port %d", __FUNCTION__, port->number);
542 priv = kzalloc(sizeof (struct cypress_private), GFP_KERNEL);
546 priv->comm_is_ok = !0;
547 spin_lock_init(&priv->lock);
548 priv->buf = cypress_buf_alloc(CYPRESS_BUF_SIZE);
549 if (priv->buf == NULL) {
553 init_waitqueue_head(&priv->delta_msr_wait);
555 usb_reset_configuration (serial->dev);
558 priv->line_control = 0;
559 priv->termios_initialized = 0;
561 priv->cbr_mask = B300;
562 /* Default packet format setting is determined by packet size.
563 Anything with a size larger then 9 must have a separate
564 count field since the 3 bit count field is otherwise too
565 small. Otherwise we can use the slightly more compact
566 format. This is in accordance with the cypress_m8 serial
567 converter app note. */
568 if (port->interrupt_out_size > 9) {
569 priv->pkt_fmt = packet_format_1;
571 priv->pkt_fmt = packet_format_2;
574 priv->write_urb_interval = interval;
575 priv->read_urb_interval = interval;
576 dbg("%s - port %d read & write intervals forced to %d",
577 __FUNCTION__,port->number,interval);
579 priv->write_urb_interval = port->interrupt_out_urb->interval;
580 priv->read_urb_interval = port->interrupt_in_urb->interval;
581 dbg("%s - port %d intervals: read=%d write=%d",
582 __FUNCTION__,port->number,
583 priv->read_urb_interval,priv->write_urb_interval);
585 usb_set_serial_port_data(port, priv);
591 static int cypress_earthmate_startup (struct usb_serial *serial)
593 struct cypress_private *priv;
594 struct usb_serial_port *port = serial->port[0];
596 dbg("%s", __FUNCTION__);
598 if (generic_startup(serial)) {
599 dbg("%s - Failed setting up port %d", __FUNCTION__,
604 priv = usb_get_serial_port_data(port);
605 priv->chiptype = CT_EARTHMATE;
606 /* All Earthmate devices use the separated-count packet
608 priv->pkt_fmt = packet_format_1;
609 if (serial->dev->descriptor.idProduct != PRODUCT_ID_EARTHMATEUSB) {
610 /* The old original USB Earthmate seemed able to
611 handle GET_CONFIG requests; everything they've
612 produced since that time crashes if this command is
614 dbg("%s - Marking this device as unsafe for GET_CONFIG "
615 "commands", __func__);
616 priv->get_cfg_unsafe = !0;
620 } /* cypress_earthmate_startup */
623 static int cypress_hidcom_startup (struct usb_serial *serial)
625 struct cypress_private *priv;
627 dbg("%s", __FUNCTION__);
629 if (generic_startup(serial)) {
630 dbg("%s - Failed setting up port %d", __FUNCTION__,
631 serial->port[0]->number);
635 priv = usb_get_serial_port_data(serial->port[0]);
636 priv->chiptype = CT_CYPHIDCOM;
639 } /* cypress_hidcom_startup */
642 static int cypress_ca42v2_startup (struct usb_serial *serial)
644 struct cypress_private *priv;
646 dbg("%s", __FUNCTION__);
648 if (generic_startup(serial)) {
649 dbg("%s - Failed setting up port %d", __FUNCTION__,
650 serial->port[0]->number);
654 priv = usb_get_serial_port_data(serial->port[0]);
655 priv->chiptype = CT_CA42V2;
658 } /* cypress_ca42v2_startup */
661 static void cypress_shutdown (struct usb_serial *serial)
663 struct cypress_private *priv;
665 dbg ("%s - port %d", __FUNCTION__, serial->port[0]->number);
667 /* all open ports are closed at this point */
669 priv = usb_get_serial_port_data(serial->port[0]);
672 cypress_buf_free(priv->buf);
674 usb_set_serial_port_data(serial->port[0], NULL);
679 static int cypress_open (struct usb_serial_port *port, struct file *filp)
681 struct cypress_private *priv = usb_get_serial_port_data(port);
682 struct usb_serial *serial = port->serial;
686 dbg("%s - port %d", __FUNCTION__, port->number);
688 if (!priv->comm_is_ok)
691 /* clear halts before open */
692 usb_clear_halt(serial->dev, 0x81);
693 usb_clear_halt(serial->dev, 0x02);
695 spin_lock_irqsave(&priv->lock, flags);
696 /* reset read/write statistics */
701 spin_unlock_irqrestore(&priv->lock, flags);
703 /* setting to zero could cause data loss */
704 port->tty->low_latency = 1;
706 /* raise both lines and set termios */
707 spin_lock_irqsave(&priv->lock, flags);
708 priv->line_control = CONTROL_DTR | CONTROL_RTS;
710 spin_unlock_irqrestore(&priv->lock, flags);
711 result = cypress_write(port, NULL, 0);
714 dev_err(&port->dev, "%s - failed setting the control lines - error %d\n", __FUNCTION__, result);
717 dbg("%s - success setting the control lines", __FUNCTION__);
719 cypress_set_termios(port, &priv->tmp_termios);
721 /* setup the port and start reading from the device */
722 if(!port->interrupt_in_urb){
723 err("%s - interrupt_in_urb is empty!", __FUNCTION__);
727 usb_fill_int_urb(port->interrupt_in_urb, serial->dev,
728 usb_rcvintpipe(serial->dev, port->interrupt_in_endpointAddress),
729 port->interrupt_in_urb->transfer_buffer, port->interrupt_in_urb->transfer_buffer_length,
730 cypress_read_int_callback, port, priv->read_urb_interval);
731 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
734 dev_err(&port->dev, "%s - failed submitting read urb, error %d\n", __FUNCTION__, result);
735 cypress_set_dead(port);
742 static void cypress_close(struct usb_serial_port *port, struct file * filp)
744 struct cypress_private *priv = usb_get_serial_port_data(port);
745 unsigned int c_cflag;
750 dbg("%s - port %d", __FUNCTION__, port->number);
752 /* wait for data to drain from buffer */
753 spin_lock_irq(&priv->lock);
754 timeout = CYPRESS_CLOSING_WAIT;
755 init_waitqueue_entry(&wait, current);
756 add_wait_queue(&port->tty->write_wait, &wait);
758 set_current_state(TASK_INTERRUPTIBLE);
759 if (cypress_buf_data_avail(priv->buf) == 0
760 || timeout == 0 || signal_pending(current)
761 /* without mutex, allowed due to harmless failure mode */
762 || port->serial->disconnected)
764 spin_unlock_irq(&priv->lock);
765 timeout = schedule_timeout(timeout);
766 spin_lock_irq(&priv->lock);
768 set_current_state(TASK_RUNNING);
769 remove_wait_queue(&port->tty->write_wait, &wait);
770 /* clear out any remaining data in the buffer */
771 cypress_buf_clear(priv->buf);
772 spin_unlock_irq(&priv->lock);
774 /* writing is potentially harmful, lock must be taken */
775 mutex_lock(&port->serial->disc_mutex);
776 if (port->serial->disconnected) {
777 mutex_unlock(&port->serial->disc_mutex);
780 /* wait for characters to drain from device */
781 bps = tty_get_baud_rate(port->tty);
783 timeout = max((HZ*2560)/bps,HZ/10);
786 schedule_timeout_interruptible(timeout);
788 dbg("%s - stopping urbs", __FUNCTION__);
789 usb_kill_urb (port->interrupt_in_urb);
790 usb_kill_urb (port->interrupt_out_urb);
793 c_cflag = port->tty->termios->c_cflag;
794 if (c_cflag & HUPCL) {
795 /* drop dtr and rts */
796 priv = usb_get_serial_port_data(port);
797 spin_lock_irq(&priv->lock);
798 priv->line_control = 0;
800 spin_unlock_irq(&priv->lock);
801 cypress_write(port, NULL, 0);
806 dev_info (&port->dev, "Statistics: %d Bytes In | %d Bytes Out | %d Commands Issued\n",
807 priv->bytes_in, priv->bytes_out, priv->cmd_count);
808 mutex_unlock(&port->serial->disc_mutex);
809 } /* cypress_close */
812 static int cypress_write(struct usb_serial_port *port, const unsigned char *buf, int count)
814 struct cypress_private *priv = usb_get_serial_port_data(port);
817 dbg("%s - port %d, %d bytes", __FUNCTION__, port->number, count);
819 /* line control commands, which need to be executed immediately,
820 are not put into the buffer for obvious reasons.
822 if (priv->cmd_ctrl) {
830 spin_lock_irqsave(&priv->lock, flags);
831 count = cypress_buf_put(priv->buf, buf, count);
832 spin_unlock_irqrestore(&priv->lock, flags);
838 } /* cypress_write */
841 static void cypress_send(struct usb_serial_port *port)
843 int count = 0, result, offset, actual_size;
844 struct cypress_private *priv = usb_get_serial_port_data(port);
847 if (!priv->comm_is_ok)
850 dbg("%s - port %d", __FUNCTION__, port->number);
851 dbg("%s - interrupt out size is %d", __FUNCTION__, port->interrupt_out_size);
853 spin_lock_irqsave(&priv->lock, flags);
854 if (priv->write_urb_in_use) {
855 dbg("%s - can't write, urb in use", __FUNCTION__);
856 spin_unlock_irqrestore(&priv->lock, flags);
859 spin_unlock_irqrestore(&priv->lock, flags);
862 memset(port->interrupt_out_urb->transfer_buffer, 0, port->interrupt_out_size);
864 spin_lock_irqsave(&priv->lock, flags);
865 switch (priv->pkt_fmt) {
867 case packet_format_1:
868 /* this is for the CY7C64013... */
870 port->interrupt_out_buffer[0] = priv->line_control;
872 case packet_format_2:
873 /* this is for the CY7C63743... */
875 port->interrupt_out_buffer[0] = priv->line_control;
879 if (priv->line_control & CONTROL_RESET)
880 priv->line_control &= ~CONTROL_RESET;
882 if (priv->cmd_ctrl) {
884 dbg("%s - line control command being issued", __FUNCTION__);
885 spin_unlock_irqrestore(&priv->lock, flags);
888 spin_unlock_irqrestore(&priv->lock, flags);
890 count = cypress_buf_get(priv->buf, &port->interrupt_out_buffer[offset],
891 port->interrupt_out_size-offset);
897 switch (priv->pkt_fmt) {
899 case packet_format_1:
900 port->interrupt_out_buffer[1] = count;
902 case packet_format_2:
903 port->interrupt_out_buffer[0] |= count;
906 dbg("%s - count is %d", __FUNCTION__, count);
909 spin_lock_irqsave(&priv->lock, flags);
910 priv->write_urb_in_use = 1;
911 spin_unlock_irqrestore(&priv->lock, flags);
916 actual_size = count +
917 (priv->pkt_fmt == packet_format_1 ? 2 : 1);
919 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, port->interrupt_out_size,
920 port->interrupt_out_urb->transfer_buffer);
922 usb_fill_int_urb(port->interrupt_out_urb, port->serial->dev,
923 usb_sndintpipe(port->serial->dev, port->interrupt_out_endpointAddress),
924 port->interrupt_out_buffer, port->interrupt_out_size,
925 cypress_write_int_callback, port, priv->write_urb_interval);
926 result = usb_submit_urb (port->interrupt_out_urb, GFP_ATOMIC);
928 dev_err(&port->dev, "%s - failed submitting write urb, error %d\n", __FUNCTION__,
930 priv->write_urb_in_use = 0;
931 cypress_set_dead(port);
934 spin_lock_irqsave(&priv->lock, flags);
935 if (priv->cmd_ctrl) {
938 priv->bytes_out += count; /* do not count the line control and size bytes */
939 spin_unlock_irqrestore(&priv->lock, flags);
941 usb_serial_port_softint(port);
945 /* returns how much space is available in the soft buffer */
946 static int cypress_write_room(struct usb_serial_port *port)
948 struct cypress_private *priv = usb_get_serial_port_data(port);
952 dbg("%s - port %d", __FUNCTION__, port->number);
954 spin_lock_irqsave(&priv->lock, flags);
955 room = cypress_buf_space_avail(priv->buf);
956 spin_unlock_irqrestore(&priv->lock, flags);
958 dbg("%s - returns %d", __FUNCTION__, room);
963 static int cypress_tiocmget (struct usb_serial_port *port, struct file *file)
965 struct cypress_private *priv = usb_get_serial_port_data(port);
966 __u8 status, control;
967 unsigned int result = 0;
970 dbg("%s - port %d", __FUNCTION__, port->number);
972 spin_lock_irqsave(&priv->lock, flags);
973 control = priv->line_control;
974 status = priv->current_status;
975 spin_unlock_irqrestore(&priv->lock, flags);
977 result = ((control & CONTROL_DTR) ? TIOCM_DTR : 0)
978 | ((control & CONTROL_RTS) ? TIOCM_RTS : 0)
979 | ((status & UART_CTS) ? TIOCM_CTS : 0)
980 | ((status & UART_DSR) ? TIOCM_DSR : 0)
981 | ((status & UART_RI) ? TIOCM_RI : 0)
982 | ((status & UART_CD) ? TIOCM_CD : 0);
984 dbg("%s - result = %x", __FUNCTION__, result);
990 static int cypress_tiocmset (struct usb_serial_port *port, struct file *file,
991 unsigned int set, unsigned int clear)
993 struct cypress_private *priv = usb_get_serial_port_data(port);
996 dbg("%s - port %d", __FUNCTION__, port->number);
998 spin_lock_irqsave(&priv->lock, flags);
1000 priv->line_control |= CONTROL_RTS;
1001 if (set & TIOCM_DTR)
1002 priv->line_control |= CONTROL_DTR;
1003 if (clear & TIOCM_RTS)
1004 priv->line_control &= ~CONTROL_RTS;
1005 if (clear & TIOCM_DTR)
1006 priv->line_control &= ~CONTROL_DTR;
1007 spin_unlock_irqrestore(&priv->lock, flags);
1010 return cypress_write(port, NULL, 0);
1014 static int cypress_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg)
1016 struct cypress_private *priv = usb_get_serial_port_data(port);
1018 dbg("%s - port %d, cmd 0x%.4x", __FUNCTION__, port->number, cmd);
1022 if (copy_to_user((void __user *)arg, port->tty->termios, sizeof(struct ktermios))) {
1028 if (copy_from_user(port->tty->termios, (void __user *)arg, sizeof(struct ktermios))) {
1031 /* here we need to call cypress_set_termios to invoke the new settings */
1032 cypress_set_termios(port, &priv->tmp_termios);
1035 /* This code comes from drivers/char/serial.c and ftdi_sio.c */
1037 while (priv != NULL) {
1038 interruptible_sleep_on(&priv->delta_msr_wait);
1039 /* see if a signal did it */
1040 if (signal_pending(current))
1041 return -ERESTARTSYS;
1043 char diff = priv->diff_status;
1046 return -EIO; /* no change => error */
1049 /* consume all events */
1050 priv->diff_status = 0;
1052 /* return 0 if caller wanted to know about these bits */
1053 if ( ((arg & TIOCM_RNG) && (diff & UART_RI)) ||
1054 ((arg & TIOCM_DSR) && (diff & UART_DSR)) ||
1055 ((arg & TIOCM_CD) && (diff & UART_CD)) ||
1056 ((arg & TIOCM_CTS) && (diff & UART_CTS)) ) {
1059 /* otherwise caller can't care less about what happened,
1060 * and so we continue to wait for more events.
1070 dbg("%s - arg not supported - it was 0x%04x - check include/asm/ioctls.h", __FUNCTION__, cmd);
1072 return -ENOIOCTLCMD;
1073 } /* cypress_ioctl */
1076 static void cypress_set_termios (struct usb_serial_port *port,
1077 struct ktermios *old_termios)
1079 struct cypress_private *priv = usb_get_serial_port_data(port);
1080 struct tty_struct *tty;
1081 int data_bits, stop_bits, parity_type, parity_enable;
1082 unsigned cflag, iflag, baud_mask;
1083 unsigned long flags;
1087 dbg("%s - port %d", __FUNCTION__, port->number);
1090 if ((!tty) || (!tty->termios)) {
1091 dbg("%s - no tty structures", __FUNCTION__);
1095 spin_lock_irqsave(&priv->lock, flags);
1096 if (!priv->termios_initialized) {
1097 if (priv->chiptype == CT_EARTHMATE) {
1098 *(tty->termios) = tty_std_termios;
1099 tty->termios->c_cflag = B4800 | CS8 | CREAD | HUPCL |
1101 } else if (priv->chiptype == CT_CYPHIDCOM) {
1102 *(tty->termios) = tty_std_termios;
1103 tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
1105 } else if (priv->chiptype == CT_CA42V2) {
1106 *(tty->termios) = tty_std_termios;
1107 tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
1110 priv->termios_initialized = 1;
1112 spin_unlock_irqrestore(&priv->lock, flags);
1114 cflag = tty->termios->c_cflag;
1115 iflag = tty->termios->c_iflag;
1117 /* check if there are new settings */
1119 if ((cflag != old_termios->c_cflag) ||
1120 (RELEVANT_IFLAG(iflag) !=
1121 RELEVANT_IFLAG(old_termios->c_iflag))) {
1122 dbg("%s - attempting to set new termios settings",
1124 /* should make a copy of this in case something goes
1125 * wrong in the function, we can restore it */
1126 spin_lock_irqsave(&priv->lock, flags);
1127 priv->tmp_termios = *(tty->termios);
1128 spin_unlock_irqrestore(&priv->lock, flags);
1130 dbg("%s - nothing to do, exiting", __FUNCTION__);
1136 /* set number of data bits, parity, stop bits */
1137 /* when parity is disabled the parity type bit is ignored */
1139 /* 1 means 2 stop bits, 0 means 1 stop bit */
1140 stop_bits = cflag & CSTOPB ? 1 : 0;
1142 if (cflag & PARENB) {
1144 /* 1 means odd parity, 0 means even parity */
1145 parity_type = cflag & PARODD ? 1 : 0;
1147 parity_enable = parity_type = 0;
1149 if (cflag & CSIZE) {
1150 switch (cflag & CSIZE) {
1164 err("%s - CSIZE was set, but not CS5-CS8",
1171 spin_lock_irqsave(&priv->lock, flags);
1172 oldlines = priv->line_control;
1173 if ((cflag & CBAUD) == B0) {
1174 /* drop dtr and rts */
1175 dbg("%s - dropping the lines, baud rate 0bps", __FUNCTION__);
1177 priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
1179 baud_mask = (cflag & CBAUD);
1182 dbg("%s - setting baud 300bps", __FUNCTION__);
1185 dbg("%s - setting baud 600bps", __FUNCTION__);
1188 dbg("%s - setting baud 1200bps", __FUNCTION__);
1191 dbg("%s - setting baud 2400bps", __FUNCTION__);
1194 dbg("%s - setting baud 4800bps", __FUNCTION__);
1197 dbg("%s - setting baud 9600bps", __FUNCTION__);
1200 dbg("%s - setting baud 19200bps", __FUNCTION__);
1203 dbg("%s - setting baud 38400bps", __FUNCTION__);
1206 dbg("%s - setting baud 57600bps", __FUNCTION__);
1209 dbg("%s - setting baud 115200bps", __FUNCTION__);
1212 dbg("%s - unknown masked baud rate", __FUNCTION__);
1214 priv->line_control = (CONTROL_DTR | CONTROL_RTS);
1216 spin_unlock_irqrestore(&priv->lock, flags);
1218 dbg("%s - sending %d stop_bits, %d parity_enable, %d parity_type, "
1219 "%d data_bits (+5)", __FUNCTION__, stop_bits,
1220 parity_enable, parity_type, data_bits);
1222 cypress_serial_control(port, baud_mask, data_bits, stop_bits,
1223 parity_enable, parity_type, 0, CYPRESS_SET_CONFIG);
1225 /* we perform a CYPRESS_GET_CONFIG so that the current settings are
1226 * filled into the private structure this should confirm that all is
1227 * working if it returns what we just set */
1228 cypress_serial_control(port, 0, 0, 0, 0, 0, 0, CYPRESS_GET_CONFIG);
1230 /* Here we can define custom tty settings for devices; the main tty
1231 * termios flag base comes from empeg.c */
1233 spin_lock_irqsave(&priv->lock, flags);
1234 if ( (priv->chiptype == CT_EARTHMATE) && (priv->baud_rate == 4800) ) {
1235 dbg("Using custom termios settings for a baud rate of "
1237 /* define custom termios settings for NMEA protocol */
1239 tty->termios->c_iflag /* input modes - */
1240 &= ~(IGNBRK /* disable ignore break */
1241 | BRKINT /* disable break causes interrupt */
1242 | PARMRK /* disable mark parity errors */
1243 | ISTRIP /* disable clear high bit of input char */
1244 | INLCR /* disable translate NL to CR */
1245 | IGNCR /* disable ignore CR */
1246 | ICRNL /* disable translate CR to NL */
1247 | IXON); /* disable enable XON/XOFF flow control */
1249 tty->termios->c_oflag /* output modes */
1250 &= ~OPOST; /* disable postprocess output char */
1252 tty->termios->c_lflag /* line discipline modes */
1253 &= ~(ECHO /* disable echo input characters */
1254 | ECHONL /* disable echo new line */
1255 | ICANON /* disable erase, kill, werase, and rprnt
1256 special characters */
1257 | ISIG /* disable interrupt, quit, and suspend
1258 special characters */
1259 | IEXTEN); /* disable non-POSIX special characters */
1260 } /* CT_CYPHIDCOM: Application should handle this for device */
1262 linechange = (priv->line_control != oldlines);
1263 spin_unlock_irqrestore(&priv->lock, flags);
1265 /* if necessary, set lines */
1268 cypress_write(port, NULL, 0);
1270 } /* cypress_set_termios */
1273 /* returns amount of data still left in soft buffer */
1274 static int cypress_chars_in_buffer(struct usb_serial_port *port)
1276 struct cypress_private *priv = usb_get_serial_port_data(port);
1278 unsigned long flags;
1280 dbg("%s - port %d", __FUNCTION__, port->number);
1282 spin_lock_irqsave(&priv->lock, flags);
1283 chars = cypress_buf_data_avail(priv->buf);
1284 spin_unlock_irqrestore(&priv->lock, flags);
1286 dbg("%s - returns %d", __FUNCTION__, chars);
1291 static void cypress_throttle (struct usb_serial_port *port)
1293 struct cypress_private *priv = usb_get_serial_port_data(port);
1294 unsigned long flags;
1296 dbg("%s - port %d", __FUNCTION__, port->number);
1298 spin_lock_irqsave(&priv->lock, flags);
1299 priv->rx_flags = THROTTLED;
1300 spin_unlock_irqrestore(&priv->lock, flags);
1304 static void cypress_unthrottle (struct usb_serial_port *port)
1306 struct cypress_private *priv = usb_get_serial_port_data(port);
1307 int actually_throttled, result;
1308 unsigned long flags;
1310 dbg("%s - port %d", __FUNCTION__, port->number);
1312 spin_lock_irqsave(&priv->lock, flags);
1313 actually_throttled = priv->rx_flags & ACTUALLY_THROTTLED;
1315 spin_unlock_irqrestore(&priv->lock, flags);
1317 if (!priv->comm_is_ok)
1320 if (actually_throttled) {
1321 port->interrupt_in_urb->dev = port->serial->dev;
1323 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
1325 dev_err(&port->dev, "%s - failed submitting read urb, "
1326 "error %d\n", __FUNCTION__, result);
1327 cypress_set_dead(port);
1333 static void cypress_read_int_callback(struct urb *urb)
1335 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
1336 struct cypress_private *priv = usb_get_serial_port_data(port);
1337 struct tty_struct *tty;
1338 unsigned char *data = urb->transfer_buffer;
1339 unsigned long flags;
1340 char tty_flag = TTY_NORMAL;
1345 int status = urb->status;
1347 dbg("%s - port %d", __FUNCTION__, port->number);
1350 case 0: /* success */
1355 /* precursor to disconnect so just go away */
1358 usb_clear_halt(port->serial->dev,0x81);
1361 /* something ugly is going on... */
1362 dev_err(&urb->dev->dev,"%s - unexpected nonzero read status received: %d\n",
1363 __FUNCTION__, status);
1364 cypress_set_dead(port);
1368 spin_lock_irqsave(&priv->lock, flags);
1369 if (priv->rx_flags & THROTTLED) {
1370 dbg("%s - now throttling", __FUNCTION__);
1371 priv->rx_flags |= ACTUALLY_THROTTLED;
1372 spin_unlock_irqrestore(&priv->lock, flags);
1375 spin_unlock_irqrestore(&priv->lock, flags);
1379 dbg("%s - bad tty pointer - exiting", __FUNCTION__);
1383 spin_lock_irqsave(&priv->lock, flags);
1384 result = urb->actual_length;
1385 switch (priv->pkt_fmt) {
1387 case packet_format_1:
1388 /* This is for the CY7C64013... */
1389 priv->current_status = data[0] & 0xF8;
1390 bytes = data[1] + 2;
1395 case packet_format_2:
1396 /* This is for the CY7C63743... */
1397 priv->current_status = data[0] & 0xF8;
1398 bytes = (data[0] & 0x07) + 1;
1404 spin_unlock_irqrestore(&priv->lock, flags);
1405 if (result < bytes) {
1406 dbg("%s - wrong packet size - received %d bytes but packet "
1407 "said %d bytes", __func__, result, bytes);
1411 usb_serial_debug_data (debug, &port->dev, __FUNCTION__,
1412 urb->actual_length, data);
1414 spin_lock_irqsave(&priv->lock, flags);
1415 /* check to see if status has changed */
1416 if (priv->current_status != priv->prev_status) {
1417 priv->diff_status |= priv->current_status ^
1419 wake_up_interruptible(&priv->delta_msr_wait);
1420 priv->prev_status = priv->current_status;
1422 spin_unlock_irqrestore(&priv->lock, flags);
1424 /* hangup, as defined in acm.c... this might be a bad place for it
1426 if (tty && !(tty->termios->c_cflag & CLOCAL) &&
1427 !(priv->current_status & UART_CD)) {
1428 dbg("%s - calling hangup", __FUNCTION__);
1433 /* There is one error bit... I'm assuming it is a parity error
1434 * indicator as the generic firmware will set this bit to 1 if a
1435 * parity error occurs.
1436 * I can not find reference to any other error events. */
1437 spin_lock_irqsave(&priv->lock, flags);
1438 if (priv->current_status & CYP_ERROR) {
1439 spin_unlock_irqrestore(&priv->lock, flags);
1440 tty_flag = TTY_PARITY;
1441 dbg("%s - Parity Error detected", __FUNCTION__);
1443 spin_unlock_irqrestore(&priv->lock, flags);
1445 /* process read if there is data other than line status */
1446 if (tty && (bytes > i)) {
1447 bytes = tty_buffer_request_room(tty, bytes);
1448 for (; i < bytes ; ++i) {
1449 dbg("pushing byte number %d - %d - %c", i, data[i],
1451 tty_insert_flip_char(tty, data[i], tty_flag);
1453 tty_flip_buffer_push(port->tty);
1456 spin_lock_irqsave(&priv->lock, flags);
1457 /* control and status byte(s) are also counted */
1458 priv->bytes_in += bytes;
1459 spin_unlock_irqrestore(&priv->lock, flags);
1463 /* Continue trying to always read... unless the port has closed. */
1465 if (port->open_count > 0 && priv->comm_is_ok) {
1466 usb_fill_int_urb(port->interrupt_in_urb, port->serial->dev,
1467 usb_rcvintpipe(port->serial->dev,
1468 port->interrupt_in_endpointAddress),
1469 port->interrupt_in_urb->transfer_buffer,
1470 port->interrupt_in_urb->transfer_buffer_length,
1471 cypress_read_int_callback, port, priv->read_urb_interval);
1472 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
1474 dev_err(&urb->dev->dev, "%s - failed resubmitting "
1475 "read urb, error %d\n", __FUNCTION__,
1477 cypress_set_dead(port);
1482 } /* cypress_read_int_callback */
1485 static void cypress_write_int_callback(struct urb *urb)
1487 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
1488 struct cypress_private *priv = usb_get_serial_port_data(port);
1490 int status = urb->status;
1492 dbg("%s - port %d", __FUNCTION__, port->number);
1501 /* this urb is terminated, clean up */
1502 dbg("%s - urb shutting down with status: %d",
1503 __FUNCTION__, status);
1504 priv->write_urb_in_use = 0;
1506 case -EPIPE: /* no break needed; clear halt and resubmit */
1507 if (!priv->comm_is_ok)
1509 usb_clear_halt(port->serial->dev, 0x02);
1510 /* error in the urb, so we have to resubmit it */
1511 dbg("%s - nonzero write bulk status received: %d",
1512 __FUNCTION__, status);
1513 port->interrupt_out_urb->transfer_buffer_length = 1;
1514 port->interrupt_out_urb->dev = port->serial->dev;
1515 result = usb_submit_urb(port->interrupt_out_urb, GFP_ATOMIC);
1518 dev_err(&urb->dev->dev, "%s - failed resubmitting write urb, error %d\n",
1519 __FUNCTION__, result);
1520 cypress_set_dead(port);
1523 dev_err(&urb->dev->dev,"%s - unexpected nonzero write status received: %d\n",
1524 __FUNCTION__, status);
1525 cypress_set_dead(port);
1529 priv->write_urb_in_use = 0;
1531 /* send any buffered data */
1536 /*****************************************************************************
1537 * Write buffer functions - buffering code from pl2303 used
1538 *****************************************************************************/
1543 * Allocate a circular buffer and all associated memory.
1546 static struct cypress_buf *cypress_buf_alloc(unsigned int size)
1549 struct cypress_buf *cb;
1555 cb = kmalloc(sizeof(struct cypress_buf), GFP_KERNEL);
1559 cb->buf_buf = kmalloc(size, GFP_KERNEL);
1560 if (cb->buf_buf == NULL) {
1565 cb->buf_size = size;
1566 cb->buf_get = cb->buf_put = cb->buf_buf;
1576 * Free the buffer and all associated memory.
1579 static void cypress_buf_free(struct cypress_buf *cb)
1591 * Clear out all data in the circular buffer.
1594 static void cypress_buf_clear(struct cypress_buf *cb)
1597 cb->buf_get = cb->buf_put;
1598 /* equivalent to a get of all data available */
1603 * cypress_buf_data_avail
1605 * Return the number of bytes of data available in the circular
1609 static unsigned int cypress_buf_data_avail(struct cypress_buf *cb)
1612 return ((cb->buf_size + cb->buf_put - cb->buf_get) % cb->buf_size);
1619 * cypress_buf_space_avail
1621 * Return the number of bytes of space available in the circular
1625 static unsigned int cypress_buf_space_avail(struct cypress_buf *cb)
1628 return ((cb->buf_size + cb->buf_get - cb->buf_put - 1) % cb->buf_size);
1637 * Copy data data from a user buffer and put it into the circular buffer.
1638 * Restrict to the amount of space available.
1640 * Return the number of bytes copied.
1643 static unsigned int cypress_buf_put(struct cypress_buf *cb, const char *buf,
1653 len = cypress_buf_space_avail(cb);
1660 len = cb->buf_buf + cb->buf_size - cb->buf_put;
1662 memcpy(cb->buf_put, buf, len);
1663 memcpy(cb->buf_buf, buf+len, count - len);
1664 cb->buf_put = cb->buf_buf + count - len;
1666 memcpy(cb->buf_put, buf, count);
1668 cb->buf_put += count;
1669 else /* count == len */
1670 cb->buf_put = cb->buf_buf;
1681 * Get data from the circular buffer and copy to the given buffer.
1682 * Restrict to the amount of data available.
1684 * Return the number of bytes copied.
1687 static unsigned int cypress_buf_get(struct cypress_buf *cb, char *buf,
1697 len = cypress_buf_data_avail(cb);
1704 len = cb->buf_buf + cb->buf_size - cb->buf_get;
1706 memcpy(buf, cb->buf_get, len);
1707 memcpy(buf+len, cb->buf_buf, count - len);
1708 cb->buf_get = cb->buf_buf + count - len;
1710 memcpy(buf, cb->buf_get, count);
1712 cb->buf_get += count;
1713 else /* count == len */
1714 cb->buf_get = cb->buf_buf;
1721 /*****************************************************************************
1723 *****************************************************************************/
1725 static int __init cypress_init(void)
1729 dbg("%s", __FUNCTION__);
1731 retval = usb_serial_register(&cypress_earthmate_device);
1733 goto failed_em_register;
1734 retval = usb_serial_register(&cypress_hidcom_device);
1736 goto failed_hidcom_register;
1737 retval = usb_serial_register(&cypress_ca42v2_device);
1739 goto failed_ca42v2_register;
1740 retval = usb_register(&cypress_driver);
1742 goto failed_usb_register;
1744 info(DRIVER_DESC " " DRIVER_VERSION);
1747 failed_usb_register:
1748 usb_serial_deregister(&cypress_ca42v2_device);
1749 failed_ca42v2_register:
1750 usb_serial_deregister(&cypress_hidcom_device);
1751 failed_hidcom_register:
1752 usb_serial_deregister(&cypress_earthmate_device);
1758 static void __exit cypress_exit (void)
1760 dbg("%s", __FUNCTION__);
1762 usb_deregister (&cypress_driver);
1763 usb_serial_deregister (&cypress_earthmate_device);
1764 usb_serial_deregister (&cypress_hidcom_device);
1765 usb_serial_deregister (&cypress_ca42v2_device);
1769 module_init(cypress_init);
1770 module_exit(cypress_exit);
1772 MODULE_AUTHOR( DRIVER_AUTHOR );
1773 MODULE_DESCRIPTION( DRIVER_DESC );
1774 MODULE_VERSION( DRIVER_VERSION );
1775 MODULE_LICENSE("GPL");
1777 module_param(debug, bool, S_IRUGO | S_IWUSR);
1778 MODULE_PARM_DESC(debug, "Debug enabled or not");
1779 module_param(stats, bool, S_IRUGO | S_IWUSR);
1780 MODULE_PARM_DESC(stats, "Enable statistics or not");
1781 module_param(interval, int, S_IRUGO | S_IWUSR);
1782 MODULE_PARM_DESC(interval, "Overrides interrupt interval");