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 priv = kzalloc(sizeof(struct cypress_private), GFP_KERNEL);
496 priv->comm_is_ok = !0;
497 spin_lock_init(&priv->lock);
498 if (kfifo_alloc(&priv->write_fifo, CYPRESS_BUF_SIZE, GFP_KERNEL)) {
503 /* Skip reset for FRWD device. It is a workaound:
504 device hangs if it receives SET_CONFIGURE in Configured
506 if (!is_frwd(serial->dev))
507 usb_reset_configuration(serial->dev);
510 priv->line_control = 0;
511 priv->termios_initialized = 0;
513 /* Default packet format setting is determined by packet size.
514 Anything with a size larger then 9 must have a separate
515 count field since the 3 bit count field is otherwise too
516 small. Otherwise we can use the slightly more compact
517 format. This is in accordance with the cypress_m8 serial
518 converter app note. */
519 if (port->interrupt_out_size > 9)
520 priv->pkt_fmt = packet_format_1;
522 priv->pkt_fmt = packet_format_2;
525 priv->write_urb_interval = interval;
526 priv->read_urb_interval = interval;
527 dbg("%s - port %d read & write intervals forced to %d",
528 __func__, port->number, interval);
530 priv->write_urb_interval = port->interrupt_out_urb->interval;
531 priv->read_urb_interval = port->interrupt_in_urb->interval;
532 dbg("%s - port %d intervals: read=%d write=%d",
533 __func__, port->number,
534 priv->read_urb_interval, priv->write_urb_interval);
536 usb_set_serial_port_data(port, priv);
542 static int cypress_earthmate_startup(struct usb_serial *serial)
544 struct cypress_private *priv;
545 struct usb_serial_port *port = serial->port[0];
549 if (generic_startup(serial)) {
550 dbg("%s - Failed setting up port %d", __func__,
555 priv = usb_get_serial_port_data(port);
556 priv->chiptype = CT_EARTHMATE;
557 /* All Earthmate devices use the separated-count packet
559 priv->pkt_fmt = packet_format_1;
560 if (serial->dev->descriptor.idProduct !=
561 cpu_to_le16(PRODUCT_ID_EARTHMATEUSB)) {
562 /* The old original USB Earthmate seemed able to
563 handle GET_CONFIG requests; everything they've
564 produced since that time crashes if this command is
566 dbg("%s - Marking this device as unsafe for GET_CONFIG "
567 "commands", __func__);
568 priv->get_cfg_unsafe = !0;
572 } /* cypress_earthmate_startup */
575 static int cypress_hidcom_startup(struct usb_serial *serial)
577 struct cypress_private *priv;
581 if (generic_startup(serial)) {
582 dbg("%s - Failed setting up port %d", __func__,
583 serial->port[0]->number);
587 priv = usb_get_serial_port_data(serial->port[0]);
588 priv->chiptype = CT_CYPHIDCOM;
591 } /* cypress_hidcom_startup */
594 static int cypress_ca42v2_startup(struct usb_serial *serial)
596 struct cypress_private *priv;
600 if (generic_startup(serial)) {
601 dbg("%s - Failed setting up port %d", __func__,
602 serial->port[0]->number);
606 priv = usb_get_serial_port_data(serial->port[0]);
607 priv->chiptype = CT_CA42V2;
610 } /* cypress_ca42v2_startup */
613 static void cypress_release(struct usb_serial *serial)
615 struct cypress_private *priv;
617 dbg("%s - port %d", __func__, serial->port[0]->number);
619 /* all open ports are closed at this point */
621 priv = usb_get_serial_port_data(serial->port[0]);
624 kfifo_free(&priv->write_fifo);
630 static int cypress_open(struct tty_struct *tty, struct usb_serial_port *port)
632 struct cypress_private *priv = usb_get_serial_port_data(port);
633 struct usb_serial *serial = port->serial;
637 dbg("%s - port %d", __func__, port->number);
639 if (!priv->comm_is_ok)
642 /* clear halts before open */
643 usb_clear_halt(serial->dev, 0x81);
644 usb_clear_halt(serial->dev, 0x02);
646 spin_lock_irqsave(&priv->lock, flags);
647 /* reset read/write statistics */
652 spin_unlock_irqrestore(&priv->lock, flags);
658 cypress_set_termios(tty, port, &priv->tmp_termios);
660 /* setup the port and start reading from the device */
661 if (!port->interrupt_in_urb) {
662 dev_err(&port->dev, "%s - interrupt_in_urb is empty!\n",
667 usb_fill_int_urb(port->interrupt_in_urb, serial->dev,
668 usb_rcvintpipe(serial->dev, port->interrupt_in_endpointAddress),
669 port->interrupt_in_urb->transfer_buffer,
670 port->interrupt_in_urb->transfer_buffer_length,
671 cypress_read_int_callback, port, priv->read_urb_interval);
672 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
676 "%s - failed submitting read urb, error %d\n",
678 cypress_set_dead(port);
680 port->port.drain_delay = 256;
684 static void cypress_dtr_rts(struct usb_serial_port *port, int on)
686 struct cypress_private *priv = usb_get_serial_port_data(port);
687 /* drop dtr and rts */
688 spin_lock_irq(&priv->lock);
690 priv->line_control = 0;
692 priv->line_control = CONTROL_DTR | CONTROL_RTS;
694 spin_unlock_irq(&priv->lock);
695 cypress_write(NULL, port, NULL, 0);
698 static void cypress_close(struct usb_serial_port *port)
700 struct cypress_private *priv = usb_get_serial_port_data(port);
703 dbg("%s - port %d", __func__, port->number);
705 /* writing is potentially harmful, lock must be taken */
706 mutex_lock(&port->serial->disc_mutex);
707 if (port->serial->disconnected) {
708 mutex_unlock(&port->serial->disc_mutex);
711 spin_lock_irqsave(&priv->lock, flags);
712 kfifo_reset_out(&priv->write_fifo);
713 spin_unlock_irqrestore(&priv->lock, flags);
715 dbg("%s - stopping urbs", __func__);
716 usb_kill_urb(port->interrupt_in_urb);
717 usb_kill_urb(port->interrupt_out_urb);
720 dev_info(&port->dev, "Statistics: %d Bytes In | %d Bytes Out | %d Commands Issued\n",
721 priv->bytes_in, priv->bytes_out, priv->cmd_count);
722 mutex_unlock(&port->serial->disc_mutex);
723 } /* cypress_close */
726 static int cypress_write(struct tty_struct *tty, struct usb_serial_port *port,
727 const unsigned char *buf, int count)
729 struct cypress_private *priv = usb_get_serial_port_data(port);
731 dbg("%s - port %d, %d bytes", __func__, port->number, count);
733 /* line control commands, which need to be executed immediately,
734 are not put into the buffer for obvious reasons.
736 if (priv->cmd_ctrl) {
744 count = kfifo_in_locked(&priv->write_fifo, buf, count, &priv->lock);
750 } /* cypress_write */
753 static void cypress_send(struct usb_serial_port *port)
755 int count = 0, result, offset, actual_size;
756 struct cypress_private *priv = usb_get_serial_port_data(port);
759 if (!priv->comm_is_ok)
762 dbg("%s - port %d", __func__, port->number);
763 dbg("%s - interrupt out size is %d", __func__,
764 port->interrupt_out_size);
766 spin_lock_irqsave(&priv->lock, flags);
767 if (priv->write_urb_in_use) {
768 dbg("%s - can't write, urb in use", __func__);
769 spin_unlock_irqrestore(&priv->lock, flags);
772 spin_unlock_irqrestore(&priv->lock, flags);
775 memset(port->interrupt_out_urb->transfer_buffer, 0,
776 port->interrupt_out_size);
778 spin_lock_irqsave(&priv->lock, flags);
779 switch (priv->pkt_fmt) {
781 case packet_format_1:
782 /* this is for the CY7C64013... */
784 port->interrupt_out_buffer[0] = priv->line_control;
786 case packet_format_2:
787 /* this is for the CY7C63743... */
789 port->interrupt_out_buffer[0] = priv->line_control;
793 if (priv->line_control & CONTROL_RESET)
794 priv->line_control &= ~CONTROL_RESET;
796 if (priv->cmd_ctrl) {
798 dbg("%s - line control command being issued", __func__);
799 spin_unlock_irqrestore(&priv->lock, flags);
802 spin_unlock_irqrestore(&priv->lock, flags);
804 count = kfifo_out_locked(&priv->write_fifo,
805 &port->interrupt_out_buffer[offset],
806 port->interrupt_out_size - offset,
811 switch (priv->pkt_fmt) {
813 case packet_format_1:
814 port->interrupt_out_buffer[1] = count;
816 case packet_format_2:
817 port->interrupt_out_buffer[0] |= count;
820 dbg("%s - count is %d", __func__, count);
823 spin_lock_irqsave(&priv->lock, flags);
824 priv->write_urb_in_use = 1;
825 spin_unlock_irqrestore(&priv->lock, flags);
830 actual_size = count +
831 (priv->pkt_fmt == packet_format_1 ? 2 : 1);
833 usb_serial_debug_data(debug, &port->dev, __func__,
834 port->interrupt_out_size,
835 port->interrupt_out_urb->transfer_buffer);
837 usb_fill_int_urb(port->interrupt_out_urb, port->serial->dev,
838 usb_sndintpipe(port->serial->dev, port->interrupt_out_endpointAddress),
839 port->interrupt_out_buffer, port->interrupt_out_size,
840 cypress_write_int_callback, port, priv->write_urb_interval);
841 result = usb_submit_urb(port->interrupt_out_urb, GFP_ATOMIC);
844 "%s - failed submitting write urb, error %d\n",
846 priv->write_urb_in_use = 0;
847 cypress_set_dead(port);
850 spin_lock_irqsave(&priv->lock, flags);
854 /* do not count the line control and size bytes */
855 priv->bytes_out += count;
856 spin_unlock_irqrestore(&priv->lock, flags);
858 usb_serial_port_softint(port);
862 /* returns how much space is available in the soft buffer */
863 static int cypress_write_room(struct tty_struct *tty)
865 struct usb_serial_port *port = tty->driver_data;
866 struct cypress_private *priv = usb_get_serial_port_data(port);
870 dbg("%s - port %d", __func__, port->number);
872 spin_lock_irqsave(&priv->lock, flags);
873 room = kfifo_avail(&priv->write_fifo);
874 spin_unlock_irqrestore(&priv->lock, flags);
876 dbg("%s - returns %d", __func__, room);
881 static int cypress_tiocmget(struct tty_struct *tty)
883 struct usb_serial_port *port = tty->driver_data;
884 struct cypress_private *priv = usb_get_serial_port_data(port);
885 __u8 status, control;
886 unsigned int result = 0;
889 dbg("%s - port %d", __func__, port->number);
891 spin_lock_irqsave(&priv->lock, flags);
892 control = priv->line_control;
893 status = priv->current_status;
894 spin_unlock_irqrestore(&priv->lock, flags);
896 result = ((control & CONTROL_DTR) ? TIOCM_DTR : 0)
897 | ((control & CONTROL_RTS) ? TIOCM_RTS : 0)
898 | ((status & UART_CTS) ? TIOCM_CTS : 0)
899 | ((status & UART_DSR) ? TIOCM_DSR : 0)
900 | ((status & UART_RI) ? TIOCM_RI : 0)
901 | ((status & UART_CD) ? TIOCM_CD : 0);
903 dbg("%s - result = %x", __func__, result);
909 static int cypress_tiocmset(struct tty_struct *tty,
910 unsigned int set, unsigned int clear)
912 struct usb_serial_port *port = tty->driver_data;
913 struct cypress_private *priv = usb_get_serial_port_data(port);
916 dbg("%s - port %d", __func__, port->number);
918 spin_lock_irqsave(&priv->lock, flags);
920 priv->line_control |= CONTROL_RTS;
922 priv->line_control |= CONTROL_DTR;
923 if (clear & TIOCM_RTS)
924 priv->line_control &= ~CONTROL_RTS;
925 if (clear & TIOCM_DTR)
926 priv->line_control &= ~CONTROL_DTR;
928 spin_unlock_irqrestore(&priv->lock, flags);
930 return cypress_write(tty, port, NULL, 0);
934 static int cypress_ioctl(struct tty_struct *tty,
935 unsigned int cmd, unsigned long arg)
937 struct usb_serial_port *port = tty->driver_data;
938 struct cypress_private *priv = usb_get_serial_port_data(port);
940 dbg("%s - port %d, cmd 0x%.4x", __func__, port->number, cmd);
943 /* This code comes from drivers/char/serial.c and ftdi_sio.c */
946 interruptible_sleep_on(&port->delta_msr_wait);
947 /* see if a signal did it */
948 if (signal_pending(current))
951 if (port->serial->disconnected)
955 char diff = priv->diff_status;
957 return -EIO; /* no change => error */
959 /* consume all events */
960 priv->diff_status = 0;
962 /* return 0 if caller wanted to know about
964 if (((arg & TIOCM_RNG) && (diff & UART_RI)) ||
965 ((arg & TIOCM_DSR) && (diff & UART_DSR)) ||
966 ((arg & TIOCM_CD) && (diff & UART_CD)) ||
967 ((arg & TIOCM_CTS) && (diff & UART_CTS)))
969 /* otherwise caller can't care less about what
970 * happened, and so we continue to wait for
979 dbg("%s - arg not supported - it was 0x%04x - check include/asm/ioctls.h", __func__, cmd);
981 } /* cypress_ioctl */
984 static void cypress_set_termios(struct tty_struct *tty,
985 struct usb_serial_port *port, struct ktermios *old_termios)
987 struct cypress_private *priv = usb_get_serial_port_data(port);
988 int data_bits, stop_bits, parity_type, parity_enable;
989 unsigned cflag, iflag;
994 dbg("%s - port %d", __func__, port->number);
996 spin_lock_irqsave(&priv->lock, flags);
997 /* We can't clean this one up as we don't know the device type
999 if (!priv->termios_initialized) {
1000 if (priv->chiptype == CT_EARTHMATE) {
1001 *(tty->termios) = tty_std_termios;
1002 tty->termios->c_cflag = B4800 | CS8 | CREAD | HUPCL |
1004 tty->termios->c_ispeed = 4800;
1005 tty->termios->c_ospeed = 4800;
1006 } else if (priv->chiptype == CT_CYPHIDCOM) {
1007 *(tty->termios) = tty_std_termios;
1008 tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
1010 tty->termios->c_ispeed = 9600;
1011 tty->termios->c_ospeed = 9600;
1012 } else if (priv->chiptype == CT_CA42V2) {
1013 *(tty->termios) = tty_std_termios;
1014 tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
1016 tty->termios->c_ispeed = 9600;
1017 tty->termios->c_ospeed = 9600;
1019 priv->termios_initialized = 1;
1021 spin_unlock_irqrestore(&priv->lock, flags);
1023 /* Unsupported features need clearing */
1024 tty->termios->c_cflag &= ~(CMSPAR|CRTSCTS);
1026 cflag = tty->termios->c_cflag;
1027 iflag = tty->termios->c_iflag;
1029 /* check if there are new settings */
1031 spin_lock_irqsave(&priv->lock, flags);
1032 priv->tmp_termios = *(tty->termios);
1033 spin_unlock_irqrestore(&priv->lock, flags);
1036 /* set number of data bits, parity, stop bits */
1037 /* when parity is disabled the parity type bit is ignored */
1039 /* 1 means 2 stop bits, 0 means 1 stop bit */
1040 stop_bits = cflag & CSTOPB ? 1 : 0;
1042 if (cflag & PARENB) {
1044 /* 1 means odd parity, 0 means even parity */
1045 parity_type = cflag & PARODD ? 1 : 0;
1047 parity_enable = parity_type = 0;
1049 switch (cflag & CSIZE) {
1063 dev_err(&port->dev, "%s - CSIZE was set, but not CS5-CS8\n",
1067 spin_lock_irqsave(&priv->lock, flags);
1068 oldlines = priv->line_control;
1069 if ((cflag & CBAUD) == B0) {
1070 /* drop dtr and rts */
1071 dbg("%s - dropping the lines, baud rate 0bps", __func__);
1072 priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
1074 priv->line_control = (CONTROL_DTR | CONTROL_RTS);
1075 spin_unlock_irqrestore(&priv->lock, flags);
1077 dbg("%s - sending %d stop_bits, %d parity_enable, %d parity_type, "
1078 "%d data_bits (+5)", __func__, stop_bits,
1079 parity_enable, parity_type, data_bits);
1081 cypress_serial_control(tty, port, tty_get_baud_rate(tty),
1082 data_bits, stop_bits,
1083 parity_enable, parity_type,
1084 0, CYPRESS_SET_CONFIG);
1086 /* we perform a CYPRESS_GET_CONFIG so that the current settings are
1087 * filled into the private structure this should confirm that all is
1088 * working if it returns what we just set */
1089 cypress_serial_control(tty, port, 0, 0, 0, 0, 0, 0, CYPRESS_GET_CONFIG);
1091 /* Here we can define custom tty settings for devices; the main tty
1092 * termios flag base comes from empeg.c */
1094 spin_lock_irqsave(&priv->lock, flags);
1095 if (priv->chiptype == CT_EARTHMATE && priv->baud_rate == 4800) {
1096 dbg("Using custom termios settings for a baud rate of "
1098 /* define custom termios settings for NMEA protocol */
1100 tty->termios->c_iflag /* input modes - */
1101 &= ~(IGNBRK /* disable ignore break */
1102 | BRKINT /* disable break causes interrupt */
1103 | PARMRK /* disable mark parity errors */
1104 | ISTRIP /* disable clear high bit of input char */
1105 | INLCR /* disable translate NL to CR */
1106 | IGNCR /* disable ignore CR */
1107 | ICRNL /* disable translate CR to NL */
1108 | IXON); /* disable enable XON/XOFF flow control */
1110 tty->termios->c_oflag /* output modes */
1111 &= ~OPOST; /* disable postprocess output char */
1113 tty->termios->c_lflag /* line discipline modes */
1114 &= ~(ECHO /* disable echo input characters */
1115 | ECHONL /* disable echo new line */
1116 | ICANON /* disable erase, kill, werase, and rprnt
1117 special characters */
1118 | ISIG /* disable interrupt, quit, and suspend
1119 special characters */
1120 | IEXTEN); /* disable non-POSIX special characters */
1121 } /* CT_CYPHIDCOM: Application should handle this for device */
1123 linechange = (priv->line_control != oldlines);
1124 spin_unlock_irqrestore(&priv->lock, flags);
1126 /* if necessary, set lines */
1129 cypress_write(tty, port, NULL, 0);
1131 } /* cypress_set_termios */
1134 /* returns amount of data still left in soft buffer */
1135 static int cypress_chars_in_buffer(struct tty_struct *tty)
1137 struct usb_serial_port *port = tty->driver_data;
1138 struct cypress_private *priv = usb_get_serial_port_data(port);
1140 unsigned long flags;
1142 dbg("%s - port %d", __func__, port->number);
1144 spin_lock_irqsave(&priv->lock, flags);
1145 chars = kfifo_len(&priv->write_fifo);
1146 spin_unlock_irqrestore(&priv->lock, flags);
1148 dbg("%s - returns %d", __func__, chars);
1153 static void cypress_throttle(struct tty_struct *tty)
1155 struct usb_serial_port *port = tty->driver_data;
1156 struct cypress_private *priv = usb_get_serial_port_data(port);
1158 dbg("%s - port %d", __func__, port->number);
1160 spin_lock_irq(&priv->lock);
1161 priv->rx_flags = THROTTLED;
1162 spin_unlock_irq(&priv->lock);
1166 static void cypress_unthrottle(struct tty_struct *tty)
1168 struct usb_serial_port *port = tty->driver_data;
1169 struct cypress_private *priv = usb_get_serial_port_data(port);
1170 int actually_throttled, result;
1172 dbg("%s - port %d", __func__, port->number);
1174 spin_lock_irq(&priv->lock);
1175 actually_throttled = priv->rx_flags & ACTUALLY_THROTTLED;
1177 spin_unlock_irq(&priv->lock);
1179 if (!priv->comm_is_ok)
1182 if (actually_throttled) {
1183 port->interrupt_in_urb->dev = port->serial->dev;
1185 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
1187 dev_err(&port->dev, "%s - failed submitting read urb, "
1188 "error %d\n", __func__, result);
1189 cypress_set_dead(port);
1195 static void cypress_read_int_callback(struct urb *urb)
1197 struct usb_serial_port *port = urb->context;
1198 struct cypress_private *priv = usb_get_serial_port_data(port);
1199 struct tty_struct *tty;
1200 unsigned char *data = urb->transfer_buffer;
1201 unsigned long flags;
1202 char tty_flag = TTY_NORMAL;
1207 int status = urb->status;
1209 dbg("%s - port %d", __func__, port->number);
1212 case 0: /* success */
1217 /* precursor to disconnect so just go away */
1220 /* Can't call usb_clear_halt while in_interrupt */
1223 /* something ugly is going on... */
1224 dev_err(&urb->dev->dev,
1225 "%s - unexpected nonzero read status received: %d\n",
1227 cypress_set_dead(port);
1231 spin_lock_irqsave(&priv->lock, flags);
1232 if (priv->rx_flags & THROTTLED) {
1233 dbg("%s - now throttling", __func__);
1234 priv->rx_flags |= ACTUALLY_THROTTLED;
1235 spin_unlock_irqrestore(&priv->lock, flags);
1238 spin_unlock_irqrestore(&priv->lock, flags);
1240 tty = tty_port_tty_get(&port->port);
1242 dbg("%s - bad tty pointer - exiting", __func__);
1246 spin_lock_irqsave(&priv->lock, flags);
1247 result = urb->actual_length;
1248 switch (priv->pkt_fmt) {
1250 case packet_format_1:
1251 /* This is for the CY7C64013... */
1252 priv->current_status = data[0] & 0xF8;
1253 bytes = data[1] + 2;
1258 case packet_format_2:
1259 /* This is for the CY7C63743... */
1260 priv->current_status = data[0] & 0xF8;
1261 bytes = (data[0] & 0x07) + 1;
1267 spin_unlock_irqrestore(&priv->lock, flags);
1268 if (result < bytes) {
1269 dbg("%s - wrong packet size - received %d bytes but packet "
1270 "said %d bytes", __func__, result, bytes);
1274 usb_serial_debug_data(debug, &port->dev, __func__,
1275 urb->actual_length, data);
1277 spin_lock_irqsave(&priv->lock, flags);
1278 /* check to see if status has changed */
1279 if (priv->current_status != priv->prev_status) {
1280 priv->diff_status |= priv->current_status ^
1282 wake_up_interruptible(&port->delta_msr_wait);
1283 priv->prev_status = priv->current_status;
1285 spin_unlock_irqrestore(&priv->lock, flags);
1287 /* hangup, as defined in acm.c... this might be a bad place for it
1289 if (tty && !(tty->termios->c_cflag & CLOCAL) &&
1290 !(priv->current_status & UART_CD)) {
1291 dbg("%s - calling hangup", __func__);
1296 /* There is one error bit... I'm assuming it is a parity error
1297 * indicator as the generic firmware will set this bit to 1 if a
1298 * parity error occurs.
1299 * I can not find reference to any other error events. */
1300 spin_lock_irqsave(&priv->lock, flags);
1301 if (priv->current_status & CYP_ERROR) {
1302 spin_unlock_irqrestore(&priv->lock, flags);
1303 tty_flag = TTY_PARITY;
1304 dbg("%s - Parity Error detected", __func__);
1306 spin_unlock_irqrestore(&priv->lock, flags);
1308 /* process read if there is data other than line status */
1309 if (tty && bytes > i) {
1310 tty_insert_flip_string_fixed_flag(tty, data + i,
1311 tty_flag, bytes - i);
1312 tty_flip_buffer_push(tty);
1315 spin_lock_irqsave(&priv->lock, flags);
1316 /* control and status byte(s) are also counted */
1317 priv->bytes_in += bytes;
1318 spin_unlock_irqrestore(&priv->lock, flags);
1323 /* Continue trying to always read */
1325 if (priv->comm_is_ok) {
1326 usb_fill_int_urb(port->interrupt_in_urb, port->serial->dev,
1327 usb_rcvintpipe(port->serial->dev,
1328 port->interrupt_in_endpointAddress),
1329 port->interrupt_in_urb->transfer_buffer,
1330 port->interrupt_in_urb->transfer_buffer_length,
1331 cypress_read_int_callback, port,
1332 priv->read_urb_interval);
1333 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
1334 if (result && result != -EPERM) {
1335 dev_err(&urb->dev->dev, "%s - failed resubmitting "
1336 "read urb, error %d\n", __func__,
1338 cypress_set_dead(port);
1341 } /* cypress_read_int_callback */
1344 static void cypress_write_int_callback(struct urb *urb)
1346 struct usb_serial_port *port = urb->context;
1347 struct cypress_private *priv = usb_get_serial_port_data(port);
1349 int status = urb->status;
1351 dbg("%s - port %d", __func__, port->number);
1360 /* this urb is terminated, clean up */
1361 dbg("%s - urb shutting down with status: %d",
1363 priv->write_urb_in_use = 0;
1365 case -EPIPE: /* no break needed; clear halt and resubmit */
1366 if (!priv->comm_is_ok)
1368 usb_clear_halt(port->serial->dev, 0x02);
1369 /* error in the urb, so we have to resubmit it */
1370 dbg("%s - nonzero write bulk status received: %d",
1372 port->interrupt_out_urb->transfer_buffer_length = 1;
1373 port->interrupt_out_urb->dev = port->serial->dev;
1374 result = usb_submit_urb(port->interrupt_out_urb, GFP_ATOMIC);
1377 dev_err(&urb->dev->dev,
1378 "%s - failed resubmitting write urb, error %d\n",
1380 cypress_set_dead(port);
1383 dev_err(&urb->dev->dev,
1384 "%s - unexpected nonzero write status received: %d\n",
1386 cypress_set_dead(port);
1389 priv->write_urb_in_use = 0;
1391 /* send any buffered data */
1396 /*****************************************************************************
1398 *****************************************************************************/
1400 static int __init cypress_init(void)
1404 dbg("%s", __func__);
1406 retval = usb_serial_register(&cypress_earthmate_device);
1408 goto failed_em_register;
1409 retval = usb_serial_register(&cypress_hidcom_device);
1411 goto failed_hidcom_register;
1412 retval = usb_serial_register(&cypress_ca42v2_device);
1414 goto failed_ca42v2_register;
1415 retval = usb_register(&cypress_driver);
1417 goto failed_usb_register;
1419 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
1423 failed_usb_register:
1424 usb_serial_deregister(&cypress_ca42v2_device);
1425 failed_ca42v2_register:
1426 usb_serial_deregister(&cypress_hidcom_device);
1427 failed_hidcom_register:
1428 usb_serial_deregister(&cypress_earthmate_device);
1434 static void __exit cypress_exit(void)
1436 dbg("%s", __func__);
1438 usb_deregister(&cypress_driver);
1439 usb_serial_deregister(&cypress_earthmate_device);
1440 usb_serial_deregister(&cypress_hidcom_device);
1441 usb_serial_deregister(&cypress_ca42v2_device);
1445 module_init(cypress_init);
1446 module_exit(cypress_exit);
1448 MODULE_AUTHOR(DRIVER_AUTHOR);
1449 MODULE_DESCRIPTION(DRIVER_DESC);
1450 MODULE_VERSION(DRIVER_VERSION);
1451 MODULE_LICENSE("GPL");
1453 module_param(debug, bool, S_IRUGO | S_IWUSR);
1454 MODULE_PARM_DESC(debug, "Debug enabled or not");
1455 module_param(stats, bool, S_IRUGO | S_IWUSR);
1456 MODULE_PARM_DESC(stats, "Enable statistics or not");
1457 module_param(interval, int, S_IRUGO | S_IWUSR);
1458 MODULE_PARM_DESC(interval, "Overrides interrupt interval");
1459 module_param(unstable_bauds, bool, S_IRUGO | S_IWUSR);
1460 MODULE_PARM_DESC(unstable_bauds, "Allow unstable baud rates");