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
17 * See http://geocities.com/i0xox0i for information on this driver and the
18 * earthmate usb device.
20 * Lonnie Mendez <dignome@gmail.com>
22 * Fixed problem where setting or retreiving the serial config would fail
23 * with EPIPE. Removed CRTS toggling so the driver behaves more like
24 * other usbserial adapters. Issued new interval of 1ms instead of the
25 * default 10ms. As a result, transfer speed has been substantially
26 * increased from avg. 850bps to avg. 3300bps. initial termios has also
27 * been modified. Cleaned up code and formatting issues so it is more
28 * readable. Replaced the C++ style comments.
30 * Lonnie Mendez <dignome@gmail.com>
32 * Incorporated write buffering from pl2303 driver. Fixed bug with line
33 * handling so both lines are raised in cypress_open. (was dropping rts)
34 * Various code cleanups made as well along with other misc bug fixes.
36 * Lonnie Mendez <dignome@gmail.com>
38 * Driver modified to support dynamic line settings. Various improvements
43 * Driver first released.
47 /* Thanks to Neil Whelchel for writing the first cypress m8 implementation
49 /* Thanks to cypress for providing references for the hid reports. */
50 /* Thanks to Jiang Zhang for providing links and for general help. */
51 /* Code originates and was built up from ftdi_sio, belkin, pl2303 and others.*/
54 #include <linux/kernel.h>
55 #include <linux/errno.h>
56 #include <linux/init.h>
57 #include <linux/slab.h>
58 #include <linux/tty.h>
59 #include <linux/tty_driver.h>
60 #include <linux/tty_flip.h>
61 #include <linux/module.h>
62 #include <linux/moduleparam.h>
63 #include <linux/spinlock.h>
64 #include <linux/usb.h>
65 #include <linux/usb/serial.h>
66 #include <linux/serial.h>
67 #include <linux/kfifo.h>
68 #include <linux/delay.h>
69 #include <linux/uaccess.h>
70 #include <asm/unaligned.h>
72 #include "cypress_m8.h"
78 static int unstable_bauds;
83 #define DRIVER_VERSION "v1.10"
84 #define DRIVER_AUTHOR "Lonnie Mendez <dignome@gmail.com>, Neil Whelchel <koyama@firstlight.net>"
85 #define DRIVER_DESC "Cypress USB to Serial Driver"
87 /* write buffer size defines */
88 #define CYPRESS_BUF_SIZE 1024
90 static const struct usb_device_id id_table_earthmate[] = {
91 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB) },
92 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB_LT20) },
93 { } /* Terminating entry */
96 static const struct usb_device_id id_table_cyphidcomrs232[] = {
97 { USB_DEVICE(VENDOR_ID_CYPRESS, PRODUCT_ID_CYPHIDCOM) },
98 { USB_DEVICE(VENDOR_ID_POWERCOM, PRODUCT_ID_UPS) },
99 { USB_DEVICE(VENDOR_ID_FRWD, PRODUCT_ID_CYPHIDCOM_FRWD) },
100 { } /* Terminating entry */
103 static const struct usb_device_id id_table_nokiaca42v2[] = {
104 { USB_DEVICE(VENDOR_ID_DAZZLE, PRODUCT_ID_CA42) },
105 { } /* Terminating entry */
108 static const struct usb_device_id id_table_combined[] = {
109 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB) },
110 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB_LT20) },
111 { USB_DEVICE(VENDOR_ID_CYPRESS, PRODUCT_ID_CYPHIDCOM) },
112 { USB_DEVICE(VENDOR_ID_POWERCOM, PRODUCT_ID_UPS) },
113 { USB_DEVICE(VENDOR_ID_FRWD, PRODUCT_ID_CYPHIDCOM_FRWD) },
114 { USB_DEVICE(VENDOR_ID_DAZZLE, PRODUCT_ID_CA42) },
115 { } /* Terminating entry */
118 MODULE_DEVICE_TABLE(usb, id_table_combined);
120 static struct usb_driver cypress_driver = {
122 .probe = usb_serial_probe,
123 .disconnect = usb_serial_disconnect,
124 .id_table = id_table_combined,
129 packet_format_1, /* b0:status, b1:payload count */
130 packet_format_2 /* b0[7:3]:status, b0[2:0]:payload count */
133 struct cypress_private {
134 spinlock_t lock; /* private lock */
135 int chiptype; /* identifier of device, for quirks/etc */
136 int bytes_in; /* used for statistics */
137 int bytes_out; /* used for statistics */
138 int cmd_count; /* used for statistics */
139 int cmd_ctrl; /* always set this to 1 before issuing a command */
140 struct kfifo write_fifo; /* write fifo */
141 int write_urb_in_use; /* write urb in use indicator */
142 int write_urb_interval; /* interval to use for write urb */
143 int read_urb_interval; /* interval to use for read urb */
144 int comm_is_ok; /* true if communication is (still) ok */
145 int termios_initialized;
146 __u8 line_control; /* holds dtr / rts value */
147 __u8 current_status; /* received from last read - info on dsr,cts,cd,ri,etc */
148 __u8 current_config; /* stores the current configuration byte */
149 __u8 rx_flags; /* throttling - used from whiteheat/ftdi_sio */
150 enum packet_format pkt_fmt; /* format to use for packet send / receive */
151 int get_cfg_unsafe; /* If true, the CYPRESS_GET_CONFIG is unsafe */
152 int baud_rate; /* stores current baud rate in
154 int isthrottled; /* if throttled, discard reads */
155 char prev_status, diff_status; /* used for TIOCMIWAIT */
156 /* we pass a pointer to this as the argument sent to
157 cypress_set_termios old_termios */
158 struct ktermios tmp_termios; /* stores the old termios settings */
161 /* function prototypes for the Cypress USB to serial device */
162 static int cypress_earthmate_startup(struct usb_serial *serial);
163 static int cypress_hidcom_startup(struct usb_serial *serial);
164 static int cypress_ca42v2_startup(struct usb_serial *serial);
165 static void cypress_release(struct usb_serial *serial);
166 static int cypress_open(struct tty_struct *tty, struct usb_serial_port *port);
167 static void cypress_close(struct usb_serial_port *port);
168 static void cypress_dtr_rts(struct usb_serial_port *port, int on);
169 static int cypress_write(struct tty_struct *tty, struct usb_serial_port *port,
170 const unsigned char *buf, int count);
171 static void cypress_send(struct usb_serial_port *port);
172 static int cypress_write_room(struct tty_struct *tty);
173 static int cypress_ioctl(struct tty_struct *tty,
174 unsigned int cmd, unsigned long arg);
175 static void cypress_set_termios(struct tty_struct *tty,
176 struct usb_serial_port *port, struct ktermios *old);
177 static int cypress_tiocmget(struct tty_struct *tty);
178 static int cypress_tiocmset(struct tty_struct *tty,
179 unsigned int set, unsigned int clear);
180 static int cypress_chars_in_buffer(struct tty_struct *tty);
181 static void cypress_throttle(struct tty_struct *tty);
182 static void cypress_unthrottle(struct tty_struct *tty);
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);
187 static struct usb_serial_driver cypress_earthmate_device = {
189 .owner = THIS_MODULE,
192 .description = "DeLorme Earthmate USB",
193 .usb_driver = &cypress_driver,
194 .id_table = id_table_earthmate,
196 .attach = cypress_earthmate_startup,
197 .release = cypress_release,
198 .open = cypress_open,
199 .close = cypress_close,
200 .dtr_rts = cypress_dtr_rts,
201 .write = cypress_write,
202 .write_room = cypress_write_room,
203 .ioctl = cypress_ioctl,
204 .set_termios = cypress_set_termios,
205 .tiocmget = cypress_tiocmget,
206 .tiocmset = cypress_tiocmset,
207 .chars_in_buffer = cypress_chars_in_buffer,
208 .throttle = cypress_throttle,
209 .unthrottle = cypress_unthrottle,
210 .read_int_callback = cypress_read_int_callback,
211 .write_int_callback = cypress_write_int_callback,
214 static struct usb_serial_driver cypress_hidcom_device = {
216 .owner = THIS_MODULE,
219 .description = "HID->COM RS232 Adapter",
220 .usb_driver = &cypress_driver,
221 .id_table = id_table_cyphidcomrs232,
223 .attach = cypress_hidcom_startup,
224 .release = cypress_release,
225 .open = cypress_open,
226 .close = cypress_close,
227 .dtr_rts = cypress_dtr_rts,
228 .write = cypress_write,
229 .write_room = cypress_write_room,
230 .ioctl = cypress_ioctl,
231 .set_termios = cypress_set_termios,
232 .tiocmget = cypress_tiocmget,
233 .tiocmset = cypress_tiocmset,
234 .chars_in_buffer = cypress_chars_in_buffer,
235 .throttle = cypress_throttle,
236 .unthrottle = cypress_unthrottle,
237 .read_int_callback = cypress_read_int_callback,
238 .write_int_callback = cypress_write_int_callback,
241 static struct usb_serial_driver cypress_ca42v2_device = {
243 .owner = THIS_MODULE,
244 .name = "nokiaca42v2",
246 .description = "Nokia CA-42 V2 Adapter",
247 .usb_driver = &cypress_driver,
248 .id_table = id_table_nokiaca42v2,
250 .attach = cypress_ca42v2_startup,
251 .release = cypress_release,
252 .open = cypress_open,
253 .close = cypress_close,
254 .dtr_rts = cypress_dtr_rts,
255 .write = cypress_write,
256 .write_room = cypress_write_room,
257 .ioctl = cypress_ioctl,
258 .set_termios = cypress_set_termios,
259 .tiocmget = cypress_tiocmget,
260 .tiocmset = cypress_tiocmset,
261 .chars_in_buffer = cypress_chars_in_buffer,
262 .throttle = cypress_throttle,
263 .unthrottle = cypress_unthrottle,
264 .read_int_callback = cypress_read_int_callback,
265 .write_int_callback = cypress_write_int_callback,
268 /*****************************************************************************
269 * Cypress serial helper functions
270 *****************************************************************************/
272 /* FRWD Dongle hidcom needs to skip reset and speed checks */
273 static inline bool is_frwd(struct usb_device *dev)
275 return ((le16_to_cpu(dev->descriptor.idVendor) == VENDOR_ID_FRWD) &&
276 (le16_to_cpu(dev->descriptor.idProduct) == PRODUCT_ID_CYPHIDCOM_FRWD));
279 static int analyze_baud_rate(struct usb_serial_port *port, speed_t new_rate)
281 struct cypress_private *priv;
282 priv = usb_get_serial_port_data(port);
287 /* FRWD Dongle uses 115200 bps */
288 if (is_frwd(port->serial->dev))
292 * The general purpose firmware for the Cypress M8 allows for
293 * a maximum speed of 57600bps (I have no idea whether DeLorme
294 * chose to use the general purpose firmware or not), if you
295 * need to modify this speed setting for your own project
296 * please add your own chiptype and modify the code likewise.
297 * The Cypress HID->COM device will work successfully up to
298 * 115200bps (but the actual throughput is around 3kBps).
300 if (port->serial->dev->speed == USB_SPEED_LOW) {
302 * Mike Isely <isely@pobox.com> 2-Feb-2008: The
303 * Cypress app note that describes this mechanism
304 * states the the low-speed part can't handle more
305 * than 800 bytes/sec, in which case 4800 baud is the
306 * safest speed for a part like that.
308 if (new_rate > 4800) {
309 dbg("%s - failed setting baud rate, device incapable "
310 "speed %d", __func__, new_rate);
314 switch (priv->chiptype) {
316 if (new_rate <= 600) {
317 /* 300 and 600 baud rates are supported under
318 * the generic firmware, but are not used with
319 * NMEA and SiRF protocols */
320 dbg("%s - failed setting baud rate, unsupported speed "
321 "of %d on Earthmate GPS", __func__, new_rate);
332 /* This function can either set or retrieve the current serial line settings */
333 static int cypress_serial_control(struct tty_struct *tty,
334 struct usb_serial_port *port, speed_t baud_rate, int data_bits,
335 int stop_bits, int parity_enable, int parity_type, int reset,
336 int cypress_request_type)
338 int new_baudrate = 0, retval = 0, tries = 0;
339 struct cypress_private *priv;
341 const unsigned int feature_len = 5;
346 priv = usb_get_serial_port_data(port);
348 if (!priv->comm_is_ok)
351 feature_buffer = kcalloc(feature_len, sizeof(u8), GFP_KERNEL);
355 switch (cypress_request_type) {
356 case CYPRESS_SET_CONFIG:
357 /* 0 means 'Hang up' so doesn't change the true bit rate */
358 new_baudrate = priv->baud_rate;
359 if (baud_rate && baud_rate != priv->baud_rate) {
360 dbg("%s - baud rate is changing", __func__);
361 retval = analyze_baud_rate(port, baud_rate);
363 new_baudrate = retval;
364 dbg("%s - New baud rate set to %d",
365 __func__, new_baudrate);
368 dbg("%s - baud rate is being sent as %d",
369 __func__, new_baudrate);
371 /* fill the feature_buffer with new configuration */
372 put_unaligned_le32(new_baudrate, feature_buffer);
373 feature_buffer[4] |= data_bits; /* assign data bits in 2 bit space ( max 3 ) */
375 feature_buffer[4] |= (stop_bits << 3); /* assign stop bits in 1 bit space */
376 feature_buffer[4] |= (parity_enable << 4); /* assign parity flag in 1 bit space */
377 feature_buffer[4] |= (parity_type << 5); /* assign parity type in 1 bit space */
379 feature_buffer[4] |= (reset << 7); /* assign reset at end of byte, 1 bit space */
381 dbg("%s - device is being sent this feature report:",
383 dbg("%s - %02X - %02X - %02X - %02X - %02X", __func__,
384 feature_buffer[0], feature_buffer[1],
385 feature_buffer[2], feature_buffer[3],
389 retval = usb_control_msg(port->serial->dev,
390 usb_sndctrlpipe(port->serial->dev, 0),
392 USB_DIR_OUT | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
393 0x0300, 0, feature_buffer,
399 } while (retval != feature_len &&
402 if (retval != feature_len) {
403 dev_err(&port->dev, "%s - failed sending serial "
404 "line settings - %d\n", __func__, retval);
405 cypress_set_dead(port);
407 spin_lock_irqsave(&priv->lock, flags);
408 priv->baud_rate = new_baudrate;
409 priv->current_config = feature_buffer[4];
410 spin_unlock_irqrestore(&priv->lock, flags);
411 /* If we asked for a speed change encode it */
413 tty_encode_baud_rate(tty,
414 new_baudrate, new_baudrate);
417 case CYPRESS_GET_CONFIG:
418 if (priv->get_cfg_unsafe) {
419 /* Not implemented for this device,
420 and if we try to do it we're likely
421 to crash the hardware. */
425 dbg("%s - retreiving serial line settings", __func__);
427 retval = usb_control_msg(port->serial->dev,
428 usb_rcvctrlpipe(port->serial->dev, 0),
430 USB_DIR_IN | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
431 0x0300, 0, feature_buffer,
436 } while (retval != feature_len
437 && retval != -ENODEV);
439 if (retval != feature_len) {
440 dev_err(&port->dev, "%s - failed to retrieve serial "
441 "line settings - %d\n", __func__, retval);
442 cypress_set_dead(port);
445 spin_lock_irqsave(&priv->lock, flags);
446 /* store the config in one byte, and later
447 use bit masks to check values */
448 priv->current_config = feature_buffer[4];
449 priv->baud_rate = get_unaligned_le32(feature_buffer);
450 spin_unlock_irqrestore(&priv->lock, flags);
453 spin_lock_irqsave(&priv->lock, flags);
455 spin_unlock_irqrestore(&priv->lock, flags);
457 kfree(feature_buffer);
459 } /* cypress_serial_control */
462 static void cypress_set_dead(struct usb_serial_port *port)
464 struct cypress_private *priv = usb_get_serial_port_data(port);
467 spin_lock_irqsave(&priv->lock, flags);
468 if (!priv->comm_is_ok) {
469 spin_unlock_irqrestore(&priv->lock, flags);
472 priv->comm_is_ok = 0;
473 spin_unlock_irqrestore(&priv->lock, flags);
475 dev_err(&port->dev, "cypress_m8 suspending failing port %d - "
476 "interval might be too short\n", port->number);
480 /*****************************************************************************
481 * Cypress serial driver functions
482 *****************************************************************************/
485 static int generic_startup(struct usb_serial *serial)
487 struct cypress_private *priv;
488 struct usb_serial_port *port = serial->port[0];
490 dbg("%s - port %d", __func__, port->number);
492 if (!port->interrupt_out_urb || !port->interrupt_in_urb) {
493 dev_err(&port->dev, "required endpoint is missing\n");
497 priv = kzalloc(sizeof(struct cypress_private), GFP_KERNEL);
501 priv->comm_is_ok = !0;
502 spin_lock_init(&priv->lock);
503 if (kfifo_alloc(&priv->write_fifo, CYPRESS_BUF_SIZE, GFP_KERNEL)) {
508 /* Skip reset for FRWD device. It is a workaound:
509 device hangs if it receives SET_CONFIGURE in Configured
511 if (!is_frwd(serial->dev))
512 usb_reset_configuration(serial->dev);
515 priv->line_control = 0;
516 priv->termios_initialized = 0;
518 /* Default packet format setting is determined by packet size.
519 Anything with a size larger then 9 must have a separate
520 count field since the 3 bit count field is otherwise too
521 small. Otherwise we can use the slightly more compact
522 format. This is in accordance with the cypress_m8 serial
523 converter app note. */
524 if (port->interrupt_out_size > 9)
525 priv->pkt_fmt = packet_format_1;
527 priv->pkt_fmt = packet_format_2;
530 priv->write_urb_interval = interval;
531 priv->read_urb_interval = interval;
532 dbg("%s - port %d read & write intervals forced to %d",
533 __func__, port->number, interval);
535 priv->write_urb_interval = port->interrupt_out_urb->interval;
536 priv->read_urb_interval = port->interrupt_in_urb->interval;
537 dbg("%s - port %d intervals: read=%d write=%d",
538 __func__, port->number,
539 priv->read_urb_interval, priv->write_urb_interval);
541 usb_set_serial_port_data(port, priv);
547 static int cypress_earthmate_startup(struct usb_serial *serial)
549 struct cypress_private *priv;
550 struct usb_serial_port *port = serial->port[0];
554 if (generic_startup(serial)) {
555 dbg("%s - Failed setting up port %d", __func__,
560 priv = usb_get_serial_port_data(port);
561 priv->chiptype = CT_EARTHMATE;
562 /* All Earthmate devices use the separated-count packet
564 priv->pkt_fmt = packet_format_1;
565 if (serial->dev->descriptor.idProduct !=
566 cpu_to_le16(PRODUCT_ID_EARTHMATEUSB)) {
567 /* The old original USB Earthmate seemed able to
568 handle GET_CONFIG requests; everything they've
569 produced since that time crashes if this command is
571 dbg("%s - Marking this device as unsafe for GET_CONFIG "
572 "commands", __func__);
573 priv->get_cfg_unsafe = !0;
577 } /* cypress_earthmate_startup */
580 static int cypress_hidcom_startup(struct usb_serial *serial)
582 struct cypress_private *priv;
586 if (generic_startup(serial)) {
587 dbg("%s - Failed setting up port %d", __func__,
588 serial->port[0]->number);
592 priv = usb_get_serial_port_data(serial->port[0]);
593 priv->chiptype = CT_CYPHIDCOM;
596 } /* cypress_hidcom_startup */
599 static int cypress_ca42v2_startup(struct usb_serial *serial)
601 struct cypress_private *priv;
605 if (generic_startup(serial)) {
606 dbg("%s - Failed setting up port %d", __func__,
607 serial->port[0]->number);
611 priv = usb_get_serial_port_data(serial->port[0]);
612 priv->chiptype = CT_CA42V2;
615 } /* cypress_ca42v2_startup */
618 static void cypress_release(struct usb_serial *serial)
620 struct cypress_private *priv;
622 dbg("%s - port %d", __func__, serial->port[0]->number);
624 /* all open ports are closed at this point */
626 priv = usb_get_serial_port_data(serial->port[0]);
629 kfifo_free(&priv->write_fifo);
635 static int cypress_open(struct tty_struct *tty, struct usb_serial_port *port)
637 struct cypress_private *priv = usb_get_serial_port_data(port);
638 struct usb_serial *serial = port->serial;
642 dbg("%s - port %d", __func__, port->number);
644 if (!priv->comm_is_ok)
647 /* clear halts before open */
648 usb_clear_halt(serial->dev, 0x81);
649 usb_clear_halt(serial->dev, 0x02);
651 spin_lock_irqsave(&priv->lock, flags);
652 /* reset read/write statistics */
657 spin_unlock_irqrestore(&priv->lock, flags);
663 cypress_set_termios(tty, port, &priv->tmp_termios);
665 /* setup the port and start reading from the device */
666 usb_fill_int_urb(port->interrupt_in_urb, serial->dev,
667 usb_rcvintpipe(serial->dev, port->interrupt_in_endpointAddress),
668 port->interrupt_in_urb->transfer_buffer,
669 port->interrupt_in_urb->transfer_buffer_length,
670 cypress_read_int_callback, port, priv->read_urb_interval);
671 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
675 "%s - failed submitting read urb, error %d\n",
677 cypress_set_dead(port);
679 port->port.drain_delay = 256;
683 static void cypress_dtr_rts(struct usb_serial_port *port, int on)
685 struct cypress_private *priv = usb_get_serial_port_data(port);
686 /* drop dtr and rts */
687 spin_lock_irq(&priv->lock);
689 priv->line_control = 0;
691 priv->line_control = CONTROL_DTR | CONTROL_RTS;
693 spin_unlock_irq(&priv->lock);
694 cypress_write(NULL, port, NULL, 0);
697 static void cypress_close(struct usb_serial_port *port)
699 struct cypress_private *priv = usb_get_serial_port_data(port);
702 dbg("%s - port %d", __func__, port->number);
704 /* writing is potentially harmful, lock must be taken */
705 mutex_lock(&port->serial->disc_mutex);
706 if (port->serial->disconnected) {
707 mutex_unlock(&port->serial->disc_mutex);
710 spin_lock_irqsave(&priv->lock, flags);
711 kfifo_reset_out(&priv->write_fifo);
712 spin_unlock_irqrestore(&priv->lock, flags);
714 dbg("%s - stopping urbs", __func__);
715 usb_kill_urb(port->interrupt_in_urb);
716 usb_kill_urb(port->interrupt_out_urb);
719 dev_info(&port->dev, "Statistics: %d Bytes In | %d Bytes Out | %d Commands Issued\n",
720 priv->bytes_in, priv->bytes_out, priv->cmd_count);
721 mutex_unlock(&port->serial->disc_mutex);
722 } /* cypress_close */
725 static int cypress_write(struct tty_struct *tty, struct usb_serial_port *port,
726 const unsigned char *buf, int count)
728 struct cypress_private *priv = usb_get_serial_port_data(port);
730 dbg("%s - port %d, %d bytes", __func__, port->number, count);
732 /* line control commands, which need to be executed immediately,
733 are not put into the buffer for obvious reasons.
735 if (priv->cmd_ctrl) {
743 count = kfifo_in_locked(&priv->write_fifo, buf, count, &priv->lock);
749 } /* cypress_write */
752 static void cypress_send(struct usb_serial_port *port)
754 int count = 0, result, offset, actual_size;
755 struct cypress_private *priv = usb_get_serial_port_data(port);
758 if (!priv->comm_is_ok)
761 dbg("%s - port %d", __func__, port->number);
762 dbg("%s - interrupt out size is %d", __func__,
763 port->interrupt_out_size);
765 spin_lock_irqsave(&priv->lock, flags);
766 if (priv->write_urb_in_use) {
767 dbg("%s - can't write, urb in use", __func__);
768 spin_unlock_irqrestore(&priv->lock, flags);
771 spin_unlock_irqrestore(&priv->lock, flags);
774 memset(port->interrupt_out_urb->transfer_buffer, 0,
775 port->interrupt_out_size);
777 spin_lock_irqsave(&priv->lock, flags);
778 switch (priv->pkt_fmt) {
780 case packet_format_1:
781 /* this is for the CY7C64013... */
783 port->interrupt_out_buffer[0] = priv->line_control;
785 case packet_format_2:
786 /* this is for the CY7C63743... */
788 port->interrupt_out_buffer[0] = priv->line_control;
792 if (priv->line_control & CONTROL_RESET)
793 priv->line_control &= ~CONTROL_RESET;
795 if (priv->cmd_ctrl) {
797 dbg("%s - line control command being issued", __func__);
798 spin_unlock_irqrestore(&priv->lock, flags);
801 spin_unlock_irqrestore(&priv->lock, flags);
803 count = kfifo_out_locked(&priv->write_fifo,
804 &port->interrupt_out_buffer[offset],
805 port->interrupt_out_size - offset,
810 switch (priv->pkt_fmt) {
812 case packet_format_1:
813 port->interrupt_out_buffer[1] = count;
815 case packet_format_2:
816 port->interrupt_out_buffer[0] |= count;
819 dbg("%s - count is %d", __func__, count);
822 spin_lock_irqsave(&priv->lock, flags);
823 priv->write_urb_in_use = 1;
824 spin_unlock_irqrestore(&priv->lock, flags);
829 actual_size = count +
830 (priv->pkt_fmt == packet_format_1 ? 2 : 1);
832 usb_serial_debug_data(debug, &port->dev, __func__,
833 port->interrupt_out_size,
834 port->interrupt_out_urb->transfer_buffer);
836 usb_fill_int_urb(port->interrupt_out_urb, port->serial->dev,
837 usb_sndintpipe(port->serial->dev, port->interrupt_out_endpointAddress),
838 port->interrupt_out_buffer, port->interrupt_out_size,
839 cypress_write_int_callback, port, priv->write_urb_interval);
840 result = usb_submit_urb(port->interrupt_out_urb, GFP_ATOMIC);
843 "%s - failed submitting write urb, error %d\n",
845 priv->write_urb_in_use = 0;
846 cypress_set_dead(port);
849 spin_lock_irqsave(&priv->lock, flags);
853 /* do not count the line control and size bytes */
854 priv->bytes_out += count;
855 spin_unlock_irqrestore(&priv->lock, flags);
857 usb_serial_port_softint(port);
861 /* returns how much space is available in the soft buffer */
862 static int cypress_write_room(struct tty_struct *tty)
864 struct usb_serial_port *port = tty->driver_data;
865 struct cypress_private *priv = usb_get_serial_port_data(port);
869 dbg("%s - port %d", __func__, port->number);
871 spin_lock_irqsave(&priv->lock, flags);
872 room = kfifo_avail(&priv->write_fifo);
873 spin_unlock_irqrestore(&priv->lock, flags);
875 dbg("%s - returns %d", __func__, room);
880 static int cypress_tiocmget(struct tty_struct *tty)
882 struct usb_serial_port *port = tty->driver_data;
883 struct cypress_private *priv = usb_get_serial_port_data(port);
884 __u8 status, control;
885 unsigned int result = 0;
888 dbg("%s - port %d", __func__, port->number);
890 spin_lock_irqsave(&priv->lock, flags);
891 control = priv->line_control;
892 status = priv->current_status;
893 spin_unlock_irqrestore(&priv->lock, flags);
895 result = ((control & CONTROL_DTR) ? TIOCM_DTR : 0)
896 | ((control & CONTROL_RTS) ? TIOCM_RTS : 0)
897 | ((status & UART_CTS) ? TIOCM_CTS : 0)
898 | ((status & UART_DSR) ? TIOCM_DSR : 0)
899 | ((status & UART_RI) ? TIOCM_RI : 0)
900 | ((status & UART_CD) ? TIOCM_CD : 0);
902 dbg("%s - result = %x", __func__, result);
908 static int cypress_tiocmset(struct tty_struct *tty,
909 unsigned int set, unsigned int clear)
911 struct usb_serial_port *port = tty->driver_data;
912 struct cypress_private *priv = usb_get_serial_port_data(port);
915 dbg("%s - port %d", __func__, port->number);
917 spin_lock_irqsave(&priv->lock, flags);
919 priv->line_control |= CONTROL_RTS;
921 priv->line_control |= CONTROL_DTR;
922 if (clear & TIOCM_RTS)
923 priv->line_control &= ~CONTROL_RTS;
924 if (clear & TIOCM_DTR)
925 priv->line_control &= ~CONTROL_DTR;
927 spin_unlock_irqrestore(&priv->lock, flags);
929 return cypress_write(tty, port, NULL, 0);
933 static int cypress_ioctl(struct tty_struct *tty,
934 unsigned int cmd, unsigned long arg)
936 struct usb_serial_port *port = tty->driver_data;
937 struct cypress_private *priv = usb_get_serial_port_data(port);
939 dbg("%s - port %d, cmd 0x%.4x", __func__, port->number, cmd);
942 /* This code comes from drivers/char/serial.c and ftdi_sio.c */
945 interruptible_sleep_on(&port->delta_msr_wait);
946 /* see if a signal did it */
947 if (signal_pending(current))
950 if (port->serial->disconnected)
954 char diff = priv->diff_status;
956 return -EIO; /* no change => error */
958 /* consume all events */
959 priv->diff_status = 0;
961 /* return 0 if caller wanted to know about
963 if (((arg & TIOCM_RNG) && (diff & UART_RI)) ||
964 ((arg & TIOCM_DSR) && (diff & UART_DSR)) ||
965 ((arg & TIOCM_CD) && (diff & UART_CD)) ||
966 ((arg & TIOCM_CTS) && (diff & UART_CTS)))
968 /* otherwise caller can't care less about what
969 * happened, and so we continue to wait for
978 dbg("%s - arg not supported - it was 0x%04x - check include/asm/ioctls.h", __func__, cmd);
980 } /* cypress_ioctl */
983 static void cypress_set_termios(struct tty_struct *tty,
984 struct usb_serial_port *port, struct ktermios *old_termios)
986 struct cypress_private *priv = usb_get_serial_port_data(port);
987 int data_bits, stop_bits, parity_type, parity_enable;
988 unsigned cflag, iflag;
993 dbg("%s - port %d", __func__, port->number);
995 spin_lock_irqsave(&priv->lock, flags);
996 /* We can't clean this one up as we don't know the device type
998 if (!priv->termios_initialized) {
999 if (priv->chiptype == CT_EARTHMATE) {
1000 *(tty->termios) = tty_std_termios;
1001 tty->termios->c_cflag = B4800 | CS8 | CREAD | HUPCL |
1003 tty->termios->c_ispeed = 4800;
1004 tty->termios->c_ospeed = 4800;
1005 } else if (priv->chiptype == CT_CYPHIDCOM) {
1006 *(tty->termios) = tty_std_termios;
1007 tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
1009 tty->termios->c_ispeed = 9600;
1010 tty->termios->c_ospeed = 9600;
1011 } else if (priv->chiptype == CT_CA42V2) {
1012 *(tty->termios) = tty_std_termios;
1013 tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
1015 tty->termios->c_ispeed = 9600;
1016 tty->termios->c_ospeed = 9600;
1018 priv->termios_initialized = 1;
1020 spin_unlock_irqrestore(&priv->lock, flags);
1022 /* Unsupported features need clearing */
1023 tty->termios->c_cflag &= ~(CMSPAR|CRTSCTS);
1025 cflag = tty->termios->c_cflag;
1026 iflag = tty->termios->c_iflag;
1028 /* check if there are new settings */
1030 spin_lock_irqsave(&priv->lock, flags);
1031 priv->tmp_termios = *(tty->termios);
1032 spin_unlock_irqrestore(&priv->lock, flags);
1035 /* set number of data bits, parity, stop bits */
1036 /* when parity is disabled the parity type bit is ignored */
1038 /* 1 means 2 stop bits, 0 means 1 stop bit */
1039 stop_bits = cflag & CSTOPB ? 1 : 0;
1041 if (cflag & PARENB) {
1043 /* 1 means odd parity, 0 means even parity */
1044 parity_type = cflag & PARODD ? 1 : 0;
1046 parity_enable = parity_type = 0;
1048 switch (cflag & CSIZE) {
1062 dev_err(&port->dev, "%s - CSIZE was set, but not CS5-CS8\n",
1066 spin_lock_irqsave(&priv->lock, flags);
1067 oldlines = priv->line_control;
1068 if ((cflag & CBAUD) == B0) {
1069 /* drop dtr and rts */
1070 dbg("%s - dropping the lines, baud rate 0bps", __func__);
1071 priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
1073 priv->line_control = (CONTROL_DTR | CONTROL_RTS);
1074 spin_unlock_irqrestore(&priv->lock, flags);
1076 dbg("%s - sending %d stop_bits, %d parity_enable, %d parity_type, "
1077 "%d data_bits (+5)", __func__, stop_bits,
1078 parity_enable, parity_type, data_bits);
1080 cypress_serial_control(tty, port, tty_get_baud_rate(tty),
1081 data_bits, stop_bits,
1082 parity_enable, parity_type,
1083 0, CYPRESS_SET_CONFIG);
1085 /* we perform a CYPRESS_GET_CONFIG so that the current settings are
1086 * filled into the private structure this should confirm that all is
1087 * working if it returns what we just set */
1088 cypress_serial_control(tty, port, 0, 0, 0, 0, 0, 0, CYPRESS_GET_CONFIG);
1090 /* Here we can define custom tty settings for devices; the main tty
1091 * termios flag base comes from empeg.c */
1093 spin_lock_irqsave(&priv->lock, flags);
1094 if (priv->chiptype == CT_EARTHMATE && priv->baud_rate == 4800) {
1095 dbg("Using custom termios settings for a baud rate of "
1097 /* define custom termios settings for NMEA protocol */
1099 tty->termios->c_iflag /* input modes - */
1100 &= ~(IGNBRK /* disable ignore break */
1101 | BRKINT /* disable break causes interrupt */
1102 | PARMRK /* disable mark parity errors */
1103 | ISTRIP /* disable clear high bit of input char */
1104 | INLCR /* disable translate NL to CR */
1105 | IGNCR /* disable ignore CR */
1106 | ICRNL /* disable translate CR to NL */
1107 | IXON); /* disable enable XON/XOFF flow control */
1109 tty->termios->c_oflag /* output modes */
1110 &= ~OPOST; /* disable postprocess output char */
1112 tty->termios->c_lflag /* line discipline modes */
1113 &= ~(ECHO /* disable echo input characters */
1114 | ECHONL /* disable echo new line */
1115 | ICANON /* disable erase, kill, werase, and rprnt
1116 special characters */
1117 | ISIG /* disable interrupt, quit, and suspend
1118 special characters */
1119 | IEXTEN); /* disable non-POSIX special characters */
1120 } /* CT_CYPHIDCOM: Application should handle this for device */
1122 linechange = (priv->line_control != oldlines);
1123 spin_unlock_irqrestore(&priv->lock, flags);
1125 /* if necessary, set lines */
1128 cypress_write(tty, port, NULL, 0);
1130 } /* cypress_set_termios */
1133 /* returns amount of data still left in soft buffer */
1134 static int cypress_chars_in_buffer(struct tty_struct *tty)
1136 struct usb_serial_port *port = tty->driver_data;
1137 struct cypress_private *priv = usb_get_serial_port_data(port);
1139 unsigned long flags;
1141 dbg("%s - port %d", __func__, port->number);
1143 spin_lock_irqsave(&priv->lock, flags);
1144 chars = kfifo_len(&priv->write_fifo);
1145 spin_unlock_irqrestore(&priv->lock, flags);
1147 dbg("%s - returns %d", __func__, chars);
1152 static void cypress_throttle(struct tty_struct *tty)
1154 struct usb_serial_port *port = tty->driver_data;
1155 struct cypress_private *priv = usb_get_serial_port_data(port);
1157 dbg("%s - port %d", __func__, port->number);
1159 spin_lock_irq(&priv->lock);
1160 priv->rx_flags = THROTTLED;
1161 spin_unlock_irq(&priv->lock);
1165 static void cypress_unthrottle(struct tty_struct *tty)
1167 struct usb_serial_port *port = tty->driver_data;
1168 struct cypress_private *priv = usb_get_serial_port_data(port);
1169 int actually_throttled, result;
1171 dbg("%s - port %d", __func__, port->number);
1173 spin_lock_irq(&priv->lock);
1174 actually_throttled = priv->rx_flags & ACTUALLY_THROTTLED;
1176 spin_unlock_irq(&priv->lock);
1178 if (!priv->comm_is_ok)
1181 if (actually_throttled) {
1182 port->interrupt_in_urb->dev = port->serial->dev;
1184 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
1186 dev_err(&port->dev, "%s - failed submitting read urb, "
1187 "error %d\n", __func__, result);
1188 cypress_set_dead(port);
1194 static void cypress_read_int_callback(struct urb *urb)
1196 struct usb_serial_port *port = urb->context;
1197 struct cypress_private *priv = usb_get_serial_port_data(port);
1198 struct tty_struct *tty;
1199 unsigned char *data = urb->transfer_buffer;
1200 unsigned long flags;
1201 char tty_flag = TTY_NORMAL;
1206 int status = urb->status;
1208 dbg("%s - port %d", __func__, port->number);
1211 case 0: /* success */
1216 /* precursor to disconnect so just go away */
1219 /* Can't call usb_clear_halt while in_interrupt */
1222 /* something ugly is going on... */
1223 dev_err(&urb->dev->dev,
1224 "%s - unexpected nonzero read status received: %d\n",
1226 cypress_set_dead(port);
1230 spin_lock_irqsave(&priv->lock, flags);
1231 if (priv->rx_flags & THROTTLED) {
1232 dbg("%s - now throttling", __func__);
1233 priv->rx_flags |= ACTUALLY_THROTTLED;
1234 spin_unlock_irqrestore(&priv->lock, flags);
1237 spin_unlock_irqrestore(&priv->lock, flags);
1239 tty = tty_port_tty_get(&port->port);
1241 dbg("%s - bad tty pointer - exiting", __func__);
1245 spin_lock_irqsave(&priv->lock, flags);
1246 result = urb->actual_length;
1247 switch (priv->pkt_fmt) {
1249 case packet_format_1:
1250 /* This is for the CY7C64013... */
1251 priv->current_status = data[0] & 0xF8;
1252 bytes = data[1] + 2;
1257 case packet_format_2:
1258 /* This is for the CY7C63743... */
1259 priv->current_status = data[0] & 0xF8;
1260 bytes = (data[0] & 0x07) + 1;
1266 spin_unlock_irqrestore(&priv->lock, flags);
1267 if (result < bytes) {
1268 dbg("%s - wrong packet size - received %d bytes but packet "
1269 "said %d bytes", __func__, result, bytes);
1273 usb_serial_debug_data(debug, &port->dev, __func__,
1274 urb->actual_length, data);
1276 spin_lock_irqsave(&priv->lock, flags);
1277 /* check to see if status has changed */
1278 if (priv->current_status != priv->prev_status) {
1279 priv->diff_status |= priv->current_status ^
1281 wake_up_interruptible(&port->delta_msr_wait);
1282 priv->prev_status = priv->current_status;
1284 spin_unlock_irqrestore(&priv->lock, flags);
1286 /* hangup, as defined in acm.c... this might be a bad place for it
1288 if (tty && !(tty->termios->c_cflag & CLOCAL) &&
1289 !(priv->current_status & UART_CD)) {
1290 dbg("%s - calling hangup", __func__);
1295 /* There is one error bit... I'm assuming it is a parity error
1296 * indicator as the generic firmware will set this bit to 1 if a
1297 * parity error occurs.
1298 * I can not find reference to any other error events. */
1299 spin_lock_irqsave(&priv->lock, flags);
1300 if (priv->current_status & CYP_ERROR) {
1301 spin_unlock_irqrestore(&priv->lock, flags);
1302 tty_flag = TTY_PARITY;
1303 dbg("%s - Parity Error detected", __func__);
1305 spin_unlock_irqrestore(&priv->lock, flags);
1307 /* process read if there is data other than line status */
1308 if (tty && bytes > i) {
1309 tty_insert_flip_string_fixed_flag(tty, data + i,
1310 tty_flag, bytes - i);
1311 tty_flip_buffer_push(tty);
1314 spin_lock_irqsave(&priv->lock, flags);
1315 /* control and status byte(s) are also counted */
1316 priv->bytes_in += bytes;
1317 spin_unlock_irqrestore(&priv->lock, flags);
1322 /* Continue trying to always read */
1324 if (priv->comm_is_ok) {
1325 usb_fill_int_urb(port->interrupt_in_urb, port->serial->dev,
1326 usb_rcvintpipe(port->serial->dev,
1327 port->interrupt_in_endpointAddress),
1328 port->interrupt_in_urb->transfer_buffer,
1329 port->interrupt_in_urb->transfer_buffer_length,
1330 cypress_read_int_callback, port,
1331 priv->read_urb_interval);
1332 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
1333 if (result && result != -EPERM) {
1334 dev_err(&urb->dev->dev, "%s - failed resubmitting "
1335 "read urb, error %d\n", __func__,
1337 cypress_set_dead(port);
1340 } /* cypress_read_int_callback */
1343 static void cypress_write_int_callback(struct urb *urb)
1345 struct usb_serial_port *port = urb->context;
1346 struct cypress_private *priv = usb_get_serial_port_data(port);
1348 int status = urb->status;
1350 dbg("%s - port %d", __func__, port->number);
1359 /* this urb is terminated, clean up */
1360 dbg("%s - urb shutting down with status: %d",
1362 priv->write_urb_in_use = 0;
1364 case -EPIPE: /* no break needed; clear halt and resubmit */
1365 if (!priv->comm_is_ok)
1367 usb_clear_halt(port->serial->dev, 0x02);
1368 /* error in the urb, so we have to resubmit it */
1369 dbg("%s - nonzero write bulk status received: %d",
1371 port->interrupt_out_urb->transfer_buffer_length = 1;
1372 port->interrupt_out_urb->dev = port->serial->dev;
1373 result = usb_submit_urb(port->interrupt_out_urb, GFP_ATOMIC);
1376 dev_err(&urb->dev->dev,
1377 "%s - failed resubmitting write urb, error %d\n",
1379 cypress_set_dead(port);
1382 dev_err(&urb->dev->dev,
1383 "%s - unexpected nonzero write status received: %d\n",
1385 cypress_set_dead(port);
1388 priv->write_urb_in_use = 0;
1390 /* send any buffered data */
1395 /*****************************************************************************
1397 *****************************************************************************/
1399 static int __init cypress_init(void)
1403 dbg("%s", __func__);
1405 retval = usb_serial_register(&cypress_earthmate_device);
1407 goto failed_em_register;
1408 retval = usb_serial_register(&cypress_hidcom_device);
1410 goto failed_hidcom_register;
1411 retval = usb_serial_register(&cypress_ca42v2_device);
1413 goto failed_ca42v2_register;
1414 retval = usb_register(&cypress_driver);
1416 goto failed_usb_register;
1418 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
1422 failed_usb_register:
1423 usb_serial_deregister(&cypress_ca42v2_device);
1424 failed_ca42v2_register:
1425 usb_serial_deregister(&cypress_hidcom_device);
1426 failed_hidcom_register:
1427 usb_serial_deregister(&cypress_earthmate_device);
1433 static void __exit cypress_exit(void)
1435 dbg("%s", __func__);
1437 usb_deregister(&cypress_driver);
1438 usb_serial_deregister(&cypress_earthmate_device);
1439 usb_serial_deregister(&cypress_hidcom_device);
1440 usb_serial_deregister(&cypress_ca42v2_device);
1444 module_init(cypress_init);
1445 module_exit(cypress_exit);
1447 MODULE_AUTHOR(DRIVER_AUTHOR);
1448 MODULE_DESCRIPTION(DRIVER_DESC);
1449 MODULE_VERSION(DRIVER_VERSION);
1450 MODULE_LICENSE("GPL");
1452 module_param(debug, bool, S_IRUGO | S_IWUSR);
1453 MODULE_PARM_DESC(debug, "Debug enabled or not");
1454 module_param(stats, bool, S_IRUGO | S_IWUSR);
1455 MODULE_PARM_DESC(stats, "Enable statistics or not");
1456 module_param(interval, int, S_IRUGO | S_IWUSR);
1457 MODULE_PARM_DESC(interval, "Overrides interrupt interval");
1458 module_param(unstable_bauds, bool, S_IRUGO | S_IWUSR);
1459 MODULE_PARM_DESC(unstable_bauds, "Allow unstable baud rates");