1 // SPDX-License-Identifier: GPL-2.0+
3 * USB Cypress M8 driver
6 * Lonnie Mendez (dignome@gmail.com)
7 * Copyright (C) 2003,2004
8 * Neil Whelchel (koyama@firstlight.net)
10 * See Documentation/usb/usb-serial.txt for more information on using this
13 * See http://geocities.com/i0xox0i for information on this driver and the
14 * earthmate usb device.
17 /* Thanks to Neil Whelchel for writing the first cypress m8 implementation
19 /* Thanks to cypress for providing references for the hid reports. */
20 /* Thanks to Jiang Zhang for providing links and for general help. */
21 /* Code originates and was built up from ftdi_sio, belkin, pl2303 and others.*/
24 #include <linux/kernel.h>
25 #include <linux/errno.h>
26 #include <linux/slab.h>
27 #include <linux/tty.h>
28 #include <linux/tty_driver.h>
29 #include <linux/tty_flip.h>
30 #include <linux/module.h>
31 #include <linux/moduleparam.h>
32 #include <linux/spinlock.h>
33 #include <linux/usb.h>
34 #include <linux/usb/serial.h>
35 #include <linux/serial.h>
36 #include <linux/kfifo.h>
37 #include <linux/delay.h>
38 #include <linux/uaccess.h>
39 #include <asm/unaligned.h>
41 #include "cypress_m8.h"
46 static bool unstable_bauds;
48 #define DRIVER_AUTHOR "Lonnie Mendez <dignome@gmail.com>, Neil Whelchel <koyama@firstlight.net>"
49 #define DRIVER_DESC "Cypress USB to Serial Driver"
51 /* write buffer size defines */
52 #define CYPRESS_BUF_SIZE 1024
54 static const struct usb_device_id id_table_earthmate[] = {
55 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB) },
56 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB_LT20) },
57 { } /* Terminating entry */
60 static const struct usb_device_id id_table_cyphidcomrs232[] = {
61 { USB_DEVICE(VENDOR_ID_CYPRESS, PRODUCT_ID_CYPHIDCOM) },
62 { USB_DEVICE(VENDOR_ID_SAI, PRODUCT_ID_CYPHIDCOM) },
63 { USB_DEVICE(VENDOR_ID_POWERCOM, PRODUCT_ID_UPS) },
64 { USB_DEVICE(VENDOR_ID_FRWD, PRODUCT_ID_CYPHIDCOM_FRWD) },
65 { } /* Terminating entry */
68 static const struct usb_device_id id_table_nokiaca42v2[] = {
69 { USB_DEVICE(VENDOR_ID_DAZZLE, PRODUCT_ID_CA42) },
70 { } /* Terminating entry */
73 static const struct usb_device_id id_table_combined[] = {
74 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB) },
75 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB_LT20) },
76 { USB_DEVICE(VENDOR_ID_CYPRESS, PRODUCT_ID_CYPHIDCOM) },
77 { USB_DEVICE(VENDOR_ID_SAI, PRODUCT_ID_CYPHIDCOM) },
78 { USB_DEVICE(VENDOR_ID_POWERCOM, PRODUCT_ID_UPS) },
79 { USB_DEVICE(VENDOR_ID_FRWD, PRODUCT_ID_CYPHIDCOM_FRWD) },
80 { USB_DEVICE(VENDOR_ID_DAZZLE, PRODUCT_ID_CA42) },
81 { } /* Terminating entry */
84 MODULE_DEVICE_TABLE(usb, id_table_combined);
87 packet_format_1, /* b0:status, b1:payload count */
88 packet_format_2 /* b0[7:3]:status, b0[2:0]:payload count */
91 struct cypress_private {
92 spinlock_t lock; /* private lock */
93 int chiptype; /* identifier of device, for quirks/etc */
94 int bytes_in; /* used for statistics */
95 int bytes_out; /* used for statistics */
96 int cmd_count; /* used for statistics */
97 int cmd_ctrl; /* always set this to 1 before issuing a command */
98 struct kfifo write_fifo; /* write fifo */
99 int write_urb_in_use; /* write urb in use indicator */
100 int write_urb_interval; /* interval to use for write urb */
101 int read_urb_interval; /* interval to use for read urb */
102 int comm_is_ok; /* true if communication is (still) ok */
103 int termios_initialized;
104 __u8 line_control; /* holds dtr / rts value */
105 __u8 current_status; /* received from last read - info on dsr,cts,cd,ri,etc */
106 __u8 current_config; /* stores the current configuration byte */
107 __u8 rx_flags; /* throttling - used from whiteheat/ftdi_sio */
108 enum packet_format pkt_fmt; /* format to use for packet send / receive */
109 int get_cfg_unsafe; /* If true, the CYPRESS_GET_CONFIG is unsafe */
110 int baud_rate; /* stores current baud rate in
112 int isthrottled; /* if throttled, discard reads */
113 char prev_status; /* used for TIOCMIWAIT */
114 /* we pass a pointer to this as the argument sent to
115 cypress_set_termios old_termios */
116 struct ktermios tmp_termios; /* stores the old termios settings */
119 /* function prototypes for the Cypress USB to serial device */
120 static int cypress_earthmate_port_probe(struct usb_serial_port *port);
121 static int cypress_hidcom_port_probe(struct usb_serial_port *port);
122 static int cypress_ca42v2_port_probe(struct usb_serial_port *port);
123 static int cypress_port_remove(struct usb_serial_port *port);
124 static int cypress_open(struct tty_struct *tty, struct usb_serial_port *port);
125 static void cypress_close(struct usb_serial_port *port);
126 static void cypress_dtr_rts(struct usb_serial_port *port, int on);
127 static int cypress_write(struct tty_struct *tty, struct usb_serial_port *port,
128 const unsigned char *buf, int count);
129 static void cypress_send(struct usb_serial_port *port);
130 static int cypress_write_room(struct tty_struct *tty);
131 static void cypress_set_termios(struct tty_struct *tty,
132 struct usb_serial_port *port, struct ktermios *old);
133 static int cypress_tiocmget(struct tty_struct *tty);
134 static int cypress_tiocmset(struct tty_struct *tty,
135 unsigned int set, unsigned int clear);
136 static int cypress_chars_in_buffer(struct tty_struct *tty);
137 static void cypress_throttle(struct tty_struct *tty);
138 static void cypress_unthrottle(struct tty_struct *tty);
139 static void cypress_set_dead(struct usb_serial_port *port);
140 static void cypress_read_int_callback(struct urb *urb);
141 static void cypress_write_int_callback(struct urb *urb);
143 static struct usb_serial_driver cypress_earthmate_device = {
145 .owner = THIS_MODULE,
148 .description = "DeLorme Earthmate USB",
149 .id_table = id_table_earthmate,
151 .port_probe = cypress_earthmate_port_probe,
152 .port_remove = cypress_port_remove,
153 .open = cypress_open,
154 .close = cypress_close,
155 .dtr_rts = cypress_dtr_rts,
156 .write = cypress_write,
157 .write_room = cypress_write_room,
158 .set_termios = cypress_set_termios,
159 .tiocmget = cypress_tiocmget,
160 .tiocmset = cypress_tiocmset,
161 .tiocmiwait = usb_serial_generic_tiocmiwait,
162 .chars_in_buffer = cypress_chars_in_buffer,
163 .throttle = cypress_throttle,
164 .unthrottle = cypress_unthrottle,
165 .read_int_callback = cypress_read_int_callback,
166 .write_int_callback = cypress_write_int_callback,
169 static struct usb_serial_driver cypress_hidcom_device = {
171 .owner = THIS_MODULE,
174 .description = "HID->COM RS232 Adapter",
175 .id_table = id_table_cyphidcomrs232,
177 .port_probe = cypress_hidcom_port_probe,
178 .port_remove = cypress_port_remove,
179 .open = cypress_open,
180 .close = cypress_close,
181 .dtr_rts = cypress_dtr_rts,
182 .write = cypress_write,
183 .write_room = cypress_write_room,
184 .set_termios = cypress_set_termios,
185 .tiocmget = cypress_tiocmget,
186 .tiocmset = cypress_tiocmset,
187 .tiocmiwait = usb_serial_generic_tiocmiwait,
188 .chars_in_buffer = cypress_chars_in_buffer,
189 .throttle = cypress_throttle,
190 .unthrottle = cypress_unthrottle,
191 .read_int_callback = cypress_read_int_callback,
192 .write_int_callback = cypress_write_int_callback,
195 static struct usb_serial_driver cypress_ca42v2_device = {
197 .owner = THIS_MODULE,
198 .name = "nokiaca42v2",
200 .description = "Nokia CA-42 V2 Adapter",
201 .id_table = id_table_nokiaca42v2,
203 .port_probe = cypress_ca42v2_port_probe,
204 .port_remove = cypress_port_remove,
205 .open = cypress_open,
206 .close = cypress_close,
207 .dtr_rts = cypress_dtr_rts,
208 .write = cypress_write,
209 .write_room = cypress_write_room,
210 .set_termios = cypress_set_termios,
211 .tiocmget = cypress_tiocmget,
212 .tiocmset = cypress_tiocmset,
213 .tiocmiwait = usb_serial_generic_tiocmiwait,
214 .chars_in_buffer = cypress_chars_in_buffer,
215 .throttle = cypress_throttle,
216 .unthrottle = cypress_unthrottle,
217 .read_int_callback = cypress_read_int_callback,
218 .write_int_callback = cypress_write_int_callback,
221 static struct usb_serial_driver * const serial_drivers[] = {
222 &cypress_earthmate_device, &cypress_hidcom_device,
223 &cypress_ca42v2_device, NULL
226 /*****************************************************************************
227 * Cypress serial helper functions
228 *****************************************************************************/
230 /* FRWD Dongle hidcom needs to skip reset and speed checks */
231 static inline bool is_frwd(struct usb_device *dev)
233 return ((le16_to_cpu(dev->descriptor.idVendor) == VENDOR_ID_FRWD) &&
234 (le16_to_cpu(dev->descriptor.idProduct) == PRODUCT_ID_CYPHIDCOM_FRWD));
237 static int analyze_baud_rate(struct usb_serial_port *port, speed_t new_rate)
239 struct cypress_private *priv;
240 priv = usb_get_serial_port_data(port);
245 /* FRWD Dongle uses 115200 bps */
246 if (is_frwd(port->serial->dev))
250 * The general purpose firmware for the Cypress M8 allows for
251 * a maximum speed of 57600bps (I have no idea whether DeLorme
252 * chose to use the general purpose firmware or not), if you
253 * need to modify this speed setting for your own project
254 * please add your own chiptype and modify the code likewise.
255 * The Cypress HID->COM device will work successfully up to
256 * 115200bps (but the actual throughput is around 3kBps).
258 if (port->serial->dev->speed == USB_SPEED_LOW) {
260 * Mike Isely <isely@pobox.com> 2-Feb-2008: The
261 * Cypress app note that describes this mechanism
262 * states the the low-speed part can't handle more
263 * than 800 bytes/sec, in which case 4800 baud is the
264 * safest speed for a part like that.
266 if (new_rate > 4800) {
268 "%s - failed setting baud rate, device incapable speed %d\n",
273 switch (priv->chiptype) {
275 if (new_rate <= 600) {
276 /* 300 and 600 baud rates are supported under
277 * the generic firmware, but are not used with
278 * NMEA and SiRF protocols */
280 "%s - failed setting baud rate, unsupported speed of %d on Earthmate GPS\n",
292 /* This function can either set or retrieve the current serial line settings */
293 static int cypress_serial_control(struct tty_struct *tty,
294 struct usb_serial_port *port, speed_t baud_rate, int data_bits,
295 int stop_bits, int parity_enable, int parity_type, int reset,
296 int cypress_request_type)
298 int new_baudrate = 0, retval = 0, tries = 0;
299 struct cypress_private *priv;
300 struct device *dev = &port->dev;
302 const unsigned int feature_len = 5;
305 priv = usb_get_serial_port_data(port);
307 if (!priv->comm_is_ok)
310 feature_buffer = kcalloc(feature_len, sizeof(u8), GFP_KERNEL);
314 switch (cypress_request_type) {
315 case CYPRESS_SET_CONFIG:
316 /* 0 means 'Hang up' so doesn't change the true bit rate */
317 new_baudrate = priv->baud_rate;
318 if (baud_rate && baud_rate != priv->baud_rate) {
319 dev_dbg(dev, "%s - baud rate is changing\n", __func__);
320 retval = analyze_baud_rate(port, baud_rate);
322 new_baudrate = retval;
323 dev_dbg(dev, "%s - New baud rate set to %d\n",
324 __func__, new_baudrate);
327 dev_dbg(dev, "%s - baud rate is being sent as %d\n", __func__,
330 /* fill the feature_buffer with new configuration */
331 put_unaligned_le32(new_baudrate, feature_buffer);
332 feature_buffer[4] |= data_bits; /* assign data bits in 2 bit space ( max 3 ) */
334 feature_buffer[4] |= (stop_bits << 3); /* assign stop bits in 1 bit space */
335 feature_buffer[4] |= (parity_enable << 4); /* assign parity flag in 1 bit space */
336 feature_buffer[4] |= (parity_type << 5); /* assign parity type in 1 bit space */
338 feature_buffer[4] |= (reset << 7); /* assign reset at end of byte, 1 bit space */
340 dev_dbg(dev, "%s - device is being sent this feature report:\n", __func__);
341 dev_dbg(dev, "%s - %02X - %02X - %02X - %02X - %02X\n", __func__,
342 feature_buffer[0], feature_buffer[1],
343 feature_buffer[2], feature_buffer[3],
347 retval = usb_control_msg(port->serial->dev,
348 usb_sndctrlpipe(port->serial->dev, 0),
350 USB_DIR_OUT | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
351 0x0300, 0, feature_buffer,
357 } while (retval != feature_len &&
360 if (retval != feature_len) {
361 dev_err(dev, "%s - failed sending serial line settings - %d\n",
363 cypress_set_dead(port);
365 spin_lock_irqsave(&priv->lock, flags);
366 priv->baud_rate = new_baudrate;
367 priv->current_config = feature_buffer[4];
368 spin_unlock_irqrestore(&priv->lock, flags);
369 /* If we asked for a speed change encode it */
371 tty_encode_baud_rate(tty,
372 new_baudrate, new_baudrate);
375 case CYPRESS_GET_CONFIG:
376 if (priv->get_cfg_unsafe) {
377 /* Not implemented for this device,
378 and if we try to do it we're likely
379 to crash the hardware. */
383 dev_dbg(dev, "%s - retreiving serial line settings\n", __func__);
385 retval = usb_control_msg(port->serial->dev,
386 usb_rcvctrlpipe(port->serial->dev, 0),
388 USB_DIR_IN | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
389 0x0300, 0, feature_buffer,
394 } while (retval != feature_len
395 && retval != -ENODEV);
397 if (retval != feature_len) {
398 dev_err(dev, "%s - failed to retrieve serial line settings - %d\n",
400 cypress_set_dead(port);
403 spin_lock_irqsave(&priv->lock, flags);
404 /* store the config in one byte, and later
405 use bit masks to check values */
406 priv->current_config = feature_buffer[4];
407 priv->baud_rate = get_unaligned_le32(feature_buffer);
408 spin_unlock_irqrestore(&priv->lock, flags);
411 spin_lock_irqsave(&priv->lock, flags);
413 spin_unlock_irqrestore(&priv->lock, flags);
415 kfree(feature_buffer);
417 } /* cypress_serial_control */
420 static void cypress_set_dead(struct usb_serial_port *port)
422 struct cypress_private *priv = usb_get_serial_port_data(port);
425 spin_lock_irqsave(&priv->lock, flags);
426 if (!priv->comm_is_ok) {
427 spin_unlock_irqrestore(&priv->lock, flags);
430 priv->comm_is_ok = 0;
431 spin_unlock_irqrestore(&priv->lock, flags);
433 dev_err(&port->dev, "cypress_m8 suspending failing port %d - "
434 "interval might be too short\n", port->port_number);
438 /*****************************************************************************
439 * Cypress serial driver functions
440 *****************************************************************************/
443 static int cypress_generic_port_probe(struct usb_serial_port *port)
445 struct usb_serial *serial = port->serial;
446 struct cypress_private *priv;
448 if (!port->interrupt_out_urb || !port->interrupt_in_urb) {
449 dev_err(&port->dev, "required endpoint is missing\n");
453 priv = kzalloc(sizeof(struct cypress_private), GFP_KERNEL);
457 priv->comm_is_ok = !0;
458 spin_lock_init(&priv->lock);
459 if (kfifo_alloc(&priv->write_fifo, CYPRESS_BUF_SIZE, GFP_KERNEL)) {
464 /* Skip reset for FRWD device. It is a workaound:
465 device hangs if it receives SET_CONFIGURE in Configured
467 if (!is_frwd(serial->dev))
468 usb_reset_configuration(serial->dev);
471 priv->line_control = 0;
472 priv->termios_initialized = 0;
474 /* Default packet format setting is determined by packet size.
475 Anything with a size larger then 9 must have a separate
476 count field since the 3 bit count field is otherwise too
477 small. Otherwise we can use the slightly more compact
478 format. This is in accordance with the cypress_m8 serial
479 converter app note. */
480 if (port->interrupt_out_size > 9)
481 priv->pkt_fmt = packet_format_1;
483 priv->pkt_fmt = packet_format_2;
486 priv->write_urb_interval = interval;
487 priv->read_urb_interval = interval;
488 dev_dbg(&port->dev, "%s - read & write intervals forced to %d\n",
491 priv->write_urb_interval = port->interrupt_out_urb->interval;
492 priv->read_urb_interval = port->interrupt_in_urb->interval;
493 dev_dbg(&port->dev, "%s - intervals: read=%d write=%d\n",
494 __func__, priv->read_urb_interval,
495 priv->write_urb_interval);
497 usb_set_serial_port_data(port, priv);
499 port->port.drain_delay = 256;
505 static int cypress_earthmate_port_probe(struct usb_serial_port *port)
507 struct usb_serial *serial = port->serial;
508 struct cypress_private *priv;
511 ret = cypress_generic_port_probe(port);
513 dev_dbg(&port->dev, "%s - Failed setting up port\n", __func__);
517 priv = usb_get_serial_port_data(port);
518 priv->chiptype = CT_EARTHMATE;
519 /* All Earthmate devices use the separated-count packet
521 priv->pkt_fmt = packet_format_1;
522 if (serial->dev->descriptor.idProduct !=
523 cpu_to_le16(PRODUCT_ID_EARTHMATEUSB)) {
524 /* The old original USB Earthmate seemed able to
525 handle GET_CONFIG requests; everything they've
526 produced since that time crashes if this command is
529 "%s - Marking this device as unsafe for GET_CONFIG commands\n",
531 priv->get_cfg_unsafe = !0;
537 static int cypress_hidcom_port_probe(struct usb_serial_port *port)
539 struct cypress_private *priv;
542 ret = cypress_generic_port_probe(port);
544 dev_dbg(&port->dev, "%s - Failed setting up port\n", __func__);
548 priv = usb_get_serial_port_data(port);
549 priv->chiptype = CT_CYPHIDCOM;
554 static int cypress_ca42v2_port_probe(struct usb_serial_port *port)
556 struct cypress_private *priv;
559 ret = cypress_generic_port_probe(port);
561 dev_dbg(&port->dev, "%s - Failed setting up port\n", __func__);
565 priv = usb_get_serial_port_data(port);
566 priv->chiptype = CT_CA42V2;
571 static int cypress_port_remove(struct usb_serial_port *port)
573 struct cypress_private *priv;
575 priv = usb_get_serial_port_data(port);
577 kfifo_free(&priv->write_fifo);
583 static int cypress_open(struct tty_struct *tty, struct usb_serial_port *port)
585 struct cypress_private *priv = usb_get_serial_port_data(port);
586 struct usb_serial *serial = port->serial;
590 if (!priv->comm_is_ok)
593 /* clear halts before open */
594 usb_clear_halt(serial->dev, 0x81);
595 usb_clear_halt(serial->dev, 0x02);
597 spin_lock_irqsave(&priv->lock, flags);
598 /* reset read/write statistics */
603 spin_unlock_irqrestore(&priv->lock, flags);
609 cypress_set_termios(tty, port, &priv->tmp_termios);
611 /* setup the port and start reading from the device */
612 usb_fill_int_urb(port->interrupt_in_urb, serial->dev,
613 usb_rcvintpipe(serial->dev, port->interrupt_in_endpointAddress),
614 port->interrupt_in_urb->transfer_buffer,
615 port->interrupt_in_urb->transfer_buffer_length,
616 cypress_read_int_callback, port, priv->read_urb_interval);
617 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
621 "%s - failed submitting read urb, error %d\n",
623 cypress_set_dead(port);
629 static void cypress_dtr_rts(struct usb_serial_port *port, int on)
631 struct cypress_private *priv = usb_get_serial_port_data(port);
632 /* drop dtr and rts */
633 spin_lock_irq(&priv->lock);
635 priv->line_control = 0;
637 priv->line_control = CONTROL_DTR | CONTROL_RTS;
639 spin_unlock_irq(&priv->lock);
640 cypress_write(NULL, port, NULL, 0);
643 static void cypress_close(struct usb_serial_port *port)
645 struct cypress_private *priv = usb_get_serial_port_data(port);
648 spin_lock_irqsave(&priv->lock, flags);
649 kfifo_reset_out(&priv->write_fifo);
650 spin_unlock_irqrestore(&priv->lock, flags);
652 dev_dbg(&port->dev, "%s - stopping urbs\n", __func__);
653 usb_kill_urb(port->interrupt_in_urb);
654 usb_kill_urb(port->interrupt_out_urb);
657 dev_info(&port->dev, "Statistics: %d Bytes In | %d Bytes Out | %d Commands Issued\n",
658 priv->bytes_in, priv->bytes_out, priv->cmd_count);
659 } /* cypress_close */
662 static int cypress_write(struct tty_struct *tty, struct usb_serial_port *port,
663 const unsigned char *buf, int count)
665 struct cypress_private *priv = usb_get_serial_port_data(port);
667 dev_dbg(&port->dev, "%s - %d bytes\n", __func__, count);
669 /* line control commands, which need to be executed immediately,
670 are not put into the buffer for obvious reasons.
672 if (priv->cmd_ctrl) {
680 count = kfifo_in_locked(&priv->write_fifo, buf, count, &priv->lock);
686 } /* cypress_write */
689 static void cypress_send(struct usb_serial_port *port)
691 int count = 0, result, offset, actual_size;
692 struct cypress_private *priv = usb_get_serial_port_data(port);
693 struct device *dev = &port->dev;
696 if (!priv->comm_is_ok)
699 dev_dbg(dev, "%s - interrupt out size is %d\n", __func__,
700 port->interrupt_out_size);
702 spin_lock_irqsave(&priv->lock, flags);
703 if (priv->write_urb_in_use) {
704 dev_dbg(dev, "%s - can't write, urb in use\n", __func__);
705 spin_unlock_irqrestore(&priv->lock, flags);
708 spin_unlock_irqrestore(&priv->lock, flags);
711 memset(port->interrupt_out_urb->transfer_buffer, 0,
712 port->interrupt_out_size);
714 spin_lock_irqsave(&priv->lock, flags);
715 switch (priv->pkt_fmt) {
717 case packet_format_1:
718 /* this is for the CY7C64013... */
720 port->interrupt_out_buffer[0] = priv->line_control;
722 case packet_format_2:
723 /* this is for the CY7C63743... */
725 port->interrupt_out_buffer[0] = priv->line_control;
729 if (priv->line_control & CONTROL_RESET)
730 priv->line_control &= ~CONTROL_RESET;
732 if (priv->cmd_ctrl) {
734 dev_dbg(dev, "%s - line control command being issued\n", __func__);
735 spin_unlock_irqrestore(&priv->lock, flags);
738 spin_unlock_irqrestore(&priv->lock, flags);
740 count = kfifo_out_locked(&priv->write_fifo,
741 &port->interrupt_out_buffer[offset],
742 port->interrupt_out_size - offset,
747 switch (priv->pkt_fmt) {
749 case packet_format_1:
750 port->interrupt_out_buffer[1] = count;
752 case packet_format_2:
753 port->interrupt_out_buffer[0] |= count;
756 dev_dbg(dev, "%s - count is %d\n", __func__, count);
759 spin_lock_irqsave(&priv->lock, flags);
760 priv->write_urb_in_use = 1;
761 spin_unlock_irqrestore(&priv->lock, flags);
766 actual_size = count +
767 (priv->pkt_fmt == packet_format_1 ? 2 : 1);
769 usb_serial_debug_data(dev, __func__, port->interrupt_out_size,
770 port->interrupt_out_urb->transfer_buffer);
772 usb_fill_int_urb(port->interrupt_out_urb, port->serial->dev,
773 usb_sndintpipe(port->serial->dev, port->interrupt_out_endpointAddress),
774 port->interrupt_out_buffer, actual_size,
775 cypress_write_int_callback, port, priv->write_urb_interval);
776 result = usb_submit_urb(port->interrupt_out_urb, GFP_ATOMIC);
778 dev_err_console(port,
779 "%s - failed submitting write urb, error %d\n",
781 priv->write_urb_in_use = 0;
782 cypress_set_dead(port);
785 spin_lock_irqsave(&priv->lock, flags);
789 /* do not count the line control and size bytes */
790 priv->bytes_out += count;
791 spin_unlock_irqrestore(&priv->lock, flags);
793 usb_serial_port_softint(port);
797 /* returns how much space is available in the soft buffer */
798 static int cypress_write_room(struct tty_struct *tty)
800 struct usb_serial_port *port = tty->driver_data;
801 struct cypress_private *priv = usb_get_serial_port_data(port);
805 spin_lock_irqsave(&priv->lock, flags);
806 room = kfifo_avail(&priv->write_fifo);
807 spin_unlock_irqrestore(&priv->lock, flags);
809 dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
814 static int cypress_tiocmget(struct tty_struct *tty)
816 struct usb_serial_port *port = tty->driver_data;
817 struct cypress_private *priv = usb_get_serial_port_data(port);
818 __u8 status, control;
819 unsigned int result = 0;
822 spin_lock_irqsave(&priv->lock, flags);
823 control = priv->line_control;
824 status = priv->current_status;
825 spin_unlock_irqrestore(&priv->lock, flags);
827 result = ((control & CONTROL_DTR) ? TIOCM_DTR : 0)
828 | ((control & CONTROL_RTS) ? TIOCM_RTS : 0)
829 | ((status & UART_CTS) ? TIOCM_CTS : 0)
830 | ((status & UART_DSR) ? TIOCM_DSR : 0)
831 | ((status & UART_RI) ? TIOCM_RI : 0)
832 | ((status & UART_CD) ? TIOCM_CD : 0);
834 dev_dbg(&port->dev, "%s - result = %x\n", __func__, result);
840 static int cypress_tiocmset(struct tty_struct *tty,
841 unsigned int set, unsigned int clear)
843 struct usb_serial_port *port = tty->driver_data;
844 struct cypress_private *priv = usb_get_serial_port_data(port);
847 spin_lock_irqsave(&priv->lock, flags);
849 priv->line_control |= CONTROL_RTS;
851 priv->line_control |= CONTROL_DTR;
852 if (clear & TIOCM_RTS)
853 priv->line_control &= ~CONTROL_RTS;
854 if (clear & TIOCM_DTR)
855 priv->line_control &= ~CONTROL_DTR;
857 spin_unlock_irqrestore(&priv->lock, flags);
859 return cypress_write(tty, port, NULL, 0);
862 static void cypress_set_termios(struct tty_struct *tty,
863 struct usb_serial_port *port, struct ktermios *old_termios)
865 struct cypress_private *priv = usb_get_serial_port_data(port);
866 struct device *dev = &port->dev;
867 int data_bits, stop_bits, parity_type, parity_enable;
868 unsigned cflag, iflag;
873 spin_lock_irqsave(&priv->lock, flags);
874 /* We can't clean this one up as we don't know the device type
876 if (!priv->termios_initialized) {
877 if (priv->chiptype == CT_EARTHMATE) {
878 tty->termios = tty_std_termios;
879 tty->termios.c_cflag = B4800 | CS8 | CREAD | HUPCL |
881 tty->termios.c_ispeed = 4800;
882 tty->termios.c_ospeed = 4800;
883 } else if (priv->chiptype == CT_CYPHIDCOM) {
884 tty->termios = tty_std_termios;
885 tty->termios.c_cflag = B9600 | CS8 | CREAD | HUPCL |
887 tty->termios.c_ispeed = 9600;
888 tty->termios.c_ospeed = 9600;
889 } else if (priv->chiptype == CT_CA42V2) {
890 tty->termios = tty_std_termios;
891 tty->termios.c_cflag = B9600 | CS8 | CREAD | HUPCL |
893 tty->termios.c_ispeed = 9600;
894 tty->termios.c_ospeed = 9600;
896 priv->termios_initialized = 1;
898 spin_unlock_irqrestore(&priv->lock, flags);
900 /* Unsupported features need clearing */
901 tty->termios.c_cflag &= ~(CMSPAR|CRTSCTS);
903 cflag = tty->termios.c_cflag;
904 iflag = tty->termios.c_iflag;
906 /* check if there are new settings */
908 spin_lock_irqsave(&priv->lock, flags);
909 priv->tmp_termios = tty->termios;
910 spin_unlock_irqrestore(&priv->lock, flags);
913 /* set number of data bits, parity, stop bits */
914 /* when parity is disabled the parity type bit is ignored */
916 /* 1 means 2 stop bits, 0 means 1 stop bit */
917 stop_bits = cflag & CSTOPB ? 1 : 0;
919 if (cflag & PARENB) {
921 /* 1 means odd parity, 0 means even parity */
922 parity_type = cflag & PARODD ? 1 : 0;
924 parity_enable = parity_type = 0;
926 switch (cflag & CSIZE) {
940 dev_err(dev, "%s - CSIZE was set, but not CS5-CS8\n", __func__);
943 spin_lock_irqsave(&priv->lock, flags);
944 oldlines = priv->line_control;
945 if ((cflag & CBAUD) == B0) {
946 /* drop dtr and rts */
947 dev_dbg(dev, "%s - dropping the lines, baud rate 0bps\n", __func__);
948 priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
950 priv->line_control = (CONTROL_DTR | CONTROL_RTS);
951 spin_unlock_irqrestore(&priv->lock, flags);
953 dev_dbg(dev, "%s - sending %d stop_bits, %d parity_enable, %d parity_type, %d data_bits (+5)\n",
954 __func__, stop_bits, parity_enable, parity_type, data_bits);
956 cypress_serial_control(tty, port, tty_get_baud_rate(tty),
957 data_bits, stop_bits,
958 parity_enable, parity_type,
959 0, CYPRESS_SET_CONFIG);
961 /* we perform a CYPRESS_GET_CONFIG so that the current settings are
962 * filled into the private structure this should confirm that all is
963 * working if it returns what we just set */
964 cypress_serial_control(tty, port, 0, 0, 0, 0, 0, 0, CYPRESS_GET_CONFIG);
966 /* Here we can define custom tty settings for devices; the main tty
967 * termios flag base comes from empeg.c */
969 spin_lock_irqsave(&priv->lock, flags);
970 if (priv->chiptype == CT_EARTHMATE && priv->baud_rate == 4800) {
971 dev_dbg(dev, "Using custom termios settings for a baud rate of 4800bps.\n");
972 /* define custom termios settings for NMEA protocol */
974 tty->termios.c_iflag /* input modes - */
975 &= ~(IGNBRK /* disable ignore break */
976 | BRKINT /* disable break causes interrupt */
977 | PARMRK /* disable mark parity errors */
978 | ISTRIP /* disable clear high bit of input char */
979 | INLCR /* disable translate NL to CR */
980 | IGNCR /* disable ignore CR */
981 | ICRNL /* disable translate CR to NL */
982 | IXON); /* disable enable XON/XOFF flow control */
984 tty->termios.c_oflag /* output modes */
985 &= ~OPOST; /* disable postprocess output char */
987 tty->termios.c_lflag /* line discipline modes */
988 &= ~(ECHO /* disable echo input characters */
989 | ECHONL /* disable echo new line */
990 | ICANON /* disable erase, kill, werase, and rprnt
991 special characters */
992 | ISIG /* disable interrupt, quit, and suspend
993 special characters */
994 | IEXTEN); /* disable non-POSIX special characters */
995 } /* CT_CYPHIDCOM: Application should handle this for device */
997 linechange = (priv->line_control != oldlines);
998 spin_unlock_irqrestore(&priv->lock, flags);
1000 /* if necessary, set lines */
1003 cypress_write(tty, port, NULL, 0);
1005 } /* cypress_set_termios */
1008 /* returns amount of data still left in soft buffer */
1009 static int cypress_chars_in_buffer(struct tty_struct *tty)
1011 struct usb_serial_port *port = tty->driver_data;
1012 struct cypress_private *priv = usb_get_serial_port_data(port);
1014 unsigned long flags;
1016 spin_lock_irqsave(&priv->lock, flags);
1017 chars = kfifo_len(&priv->write_fifo);
1018 spin_unlock_irqrestore(&priv->lock, flags);
1020 dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
1025 static void cypress_throttle(struct tty_struct *tty)
1027 struct usb_serial_port *port = tty->driver_data;
1028 struct cypress_private *priv = usb_get_serial_port_data(port);
1030 spin_lock_irq(&priv->lock);
1031 priv->rx_flags = THROTTLED;
1032 spin_unlock_irq(&priv->lock);
1036 static void cypress_unthrottle(struct tty_struct *tty)
1038 struct usb_serial_port *port = tty->driver_data;
1039 struct cypress_private *priv = usb_get_serial_port_data(port);
1040 int actually_throttled, result;
1042 spin_lock_irq(&priv->lock);
1043 actually_throttled = priv->rx_flags & ACTUALLY_THROTTLED;
1045 spin_unlock_irq(&priv->lock);
1047 if (!priv->comm_is_ok)
1050 if (actually_throttled) {
1051 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
1053 dev_err(&port->dev, "%s - failed submitting read urb, "
1054 "error %d\n", __func__, result);
1055 cypress_set_dead(port);
1061 static void cypress_read_int_callback(struct urb *urb)
1063 struct usb_serial_port *port = urb->context;
1064 struct cypress_private *priv = usb_get_serial_port_data(port);
1065 struct device *dev = &urb->dev->dev;
1066 struct tty_struct *tty;
1067 unsigned char *data = urb->transfer_buffer;
1068 unsigned long flags;
1069 char tty_flag = TTY_NORMAL;
1073 int status = urb->status;
1076 case 0: /* success */
1081 /* precursor to disconnect so just go away */
1084 /* Can't call usb_clear_halt while in_interrupt */
1087 /* something ugly is going on... */
1088 dev_err(dev, "%s - unexpected nonzero read status received: %d\n",
1090 cypress_set_dead(port);
1094 spin_lock_irqsave(&priv->lock, flags);
1095 if (priv->rx_flags & THROTTLED) {
1096 dev_dbg(dev, "%s - now throttling\n", __func__);
1097 priv->rx_flags |= ACTUALLY_THROTTLED;
1098 spin_unlock_irqrestore(&priv->lock, flags);
1101 spin_unlock_irqrestore(&priv->lock, flags);
1103 tty = tty_port_tty_get(&port->port);
1105 dev_dbg(dev, "%s - bad tty pointer - exiting\n", __func__);
1109 spin_lock_irqsave(&priv->lock, flags);
1110 result = urb->actual_length;
1111 switch (priv->pkt_fmt) {
1113 case packet_format_1:
1114 /* This is for the CY7C64013... */
1115 priv->current_status = data[0] & 0xF8;
1116 bytes = data[1] + 2;
1119 case packet_format_2:
1120 /* This is for the CY7C63743... */
1121 priv->current_status = data[0] & 0xF8;
1122 bytes = (data[0] & 0x07) + 1;
1126 spin_unlock_irqrestore(&priv->lock, flags);
1127 if (result < bytes) {
1129 "%s - wrong packet size - received %d bytes but packet said %d bytes\n",
1130 __func__, result, bytes);
1134 usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data);
1136 spin_lock_irqsave(&priv->lock, flags);
1137 /* check to see if status has changed */
1138 if (priv->current_status != priv->prev_status) {
1139 u8 delta = priv->current_status ^ priv->prev_status;
1141 if (delta & UART_MSR_MASK) {
1142 if (delta & UART_CTS)
1144 if (delta & UART_DSR)
1146 if (delta & UART_RI)
1148 if (delta & UART_CD)
1151 wake_up_interruptible(&port->port.delta_msr_wait);
1154 priv->prev_status = priv->current_status;
1156 spin_unlock_irqrestore(&priv->lock, flags);
1158 /* hangup, as defined in acm.c... this might be a bad place for it
1160 if (tty && !C_CLOCAL(tty) && !(priv->current_status & UART_CD)) {
1161 dev_dbg(dev, "%s - calling hangup\n", __func__);
1166 /* There is one error bit... I'm assuming it is a parity error
1167 * indicator as the generic firmware will set this bit to 1 if a
1168 * parity error occurs.
1169 * I can not find reference to any other error events. */
1170 spin_lock_irqsave(&priv->lock, flags);
1171 if (priv->current_status & CYP_ERROR) {
1172 spin_unlock_irqrestore(&priv->lock, flags);
1173 tty_flag = TTY_PARITY;
1174 dev_dbg(dev, "%s - Parity Error detected\n", __func__);
1176 spin_unlock_irqrestore(&priv->lock, flags);
1178 /* process read if there is data other than line status */
1180 tty_insert_flip_string_fixed_flag(&port->port, data + i,
1181 tty_flag, bytes - i);
1182 tty_flip_buffer_push(&port->port);
1185 spin_lock_irqsave(&priv->lock, flags);
1186 /* control and status byte(s) are also counted */
1187 priv->bytes_in += bytes;
1188 spin_unlock_irqrestore(&priv->lock, flags);
1193 /* Continue trying to always read */
1195 if (priv->comm_is_ok) {
1196 usb_fill_int_urb(port->interrupt_in_urb, port->serial->dev,
1197 usb_rcvintpipe(port->serial->dev,
1198 port->interrupt_in_endpointAddress),
1199 port->interrupt_in_urb->transfer_buffer,
1200 port->interrupt_in_urb->transfer_buffer_length,
1201 cypress_read_int_callback, port,
1202 priv->read_urb_interval);
1203 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
1204 if (result && result != -EPERM) {
1205 dev_err(dev, "%s - failed resubmitting read urb, error %d\n",
1207 cypress_set_dead(port);
1210 } /* cypress_read_int_callback */
1213 static void cypress_write_int_callback(struct urb *urb)
1215 struct usb_serial_port *port = urb->context;
1216 struct cypress_private *priv = usb_get_serial_port_data(port);
1217 struct device *dev = &urb->dev->dev;
1218 int status = urb->status;
1227 /* this urb is terminated, clean up */
1228 dev_dbg(dev, "%s - urb shutting down with status: %d\n",
1230 priv->write_urb_in_use = 0;
1233 /* Cannot call usb_clear_halt while in_interrupt */
1236 dev_err(dev, "%s - unexpected nonzero write status received: %d\n",
1238 cypress_set_dead(port);
1241 priv->write_urb_in_use = 0;
1243 /* send any buffered data */
1247 module_usb_serial_driver(serial_drivers, id_table_combined);
1249 MODULE_AUTHOR(DRIVER_AUTHOR);
1250 MODULE_DESCRIPTION(DRIVER_DESC);
1251 MODULE_LICENSE("GPL");
1253 module_param(stats, bool, S_IRUGO | S_IWUSR);
1254 MODULE_PARM_DESC(stats, "Enable statistics or not");
1255 module_param(interval, int, S_IRUGO | S_IWUSR);
1256 MODULE_PARM_DESC(interval, "Overrides interrupt interval");
1257 module_param(unstable_bauds, bool, S_IRUGO | S_IWUSR);
1258 MODULE_PARM_DESC(unstable_bauds, "Allow unstable baud rates");