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.
21 /* Thanks to Neil Whelchel for writing the first cypress m8 implementation
23 /* Thanks to cypress for providing references for the hid reports. */
24 /* Thanks to Jiang Zhang for providing links and for general help. */
25 /* Code originates and was built up from ftdi_sio, belkin, pl2303 and others.*/
28 #include <linux/kernel.h>
29 #include <linux/errno.h>
30 #include <linux/init.h>
31 #include <linux/slab.h>
32 #include <linux/tty.h>
33 #include <linux/tty_driver.h>
34 #include <linux/tty_flip.h>
35 #include <linux/module.h>
36 #include <linux/moduleparam.h>
37 #include <linux/spinlock.h>
38 #include <linux/usb.h>
39 #include <linux/usb/serial.h>
40 #include <linux/serial.h>
41 #include <linux/kfifo.h>
42 #include <linux/delay.h>
43 #include <linux/uaccess.h>
44 #include <asm/unaligned.h>
46 #include "cypress_m8.h"
52 static bool unstable_bauds;
57 #define DRIVER_VERSION "v1.10"
58 #define DRIVER_AUTHOR "Lonnie Mendez <dignome@gmail.com>, Neil Whelchel <koyama@firstlight.net>"
59 #define DRIVER_DESC "Cypress USB to Serial Driver"
61 /* write buffer size defines */
62 #define CYPRESS_BUF_SIZE 1024
64 static const struct usb_device_id id_table_earthmate[] = {
65 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB) },
66 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB_LT20) },
67 { } /* Terminating entry */
70 static const struct usb_device_id id_table_cyphidcomrs232[] = {
71 { USB_DEVICE(VENDOR_ID_CYPRESS, PRODUCT_ID_CYPHIDCOM) },
72 { USB_DEVICE(VENDOR_ID_POWERCOM, PRODUCT_ID_UPS) },
73 { } /* Terminating entry */
76 static const struct usb_device_id id_table_nokiaca42v2[] = {
77 { USB_DEVICE(VENDOR_ID_DAZZLE, PRODUCT_ID_CA42) },
78 { } /* Terminating entry */
81 static const struct usb_device_id id_table_combined[] = {
82 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB) },
83 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB_LT20) },
84 { USB_DEVICE(VENDOR_ID_CYPRESS, PRODUCT_ID_CYPHIDCOM) },
85 { USB_DEVICE(VENDOR_ID_POWERCOM, PRODUCT_ID_UPS) },
86 { USB_DEVICE(VENDOR_ID_DAZZLE, PRODUCT_ID_CA42) },
87 { } /* Terminating entry */
90 MODULE_DEVICE_TABLE(usb, id_table_combined);
92 static struct usb_driver cypress_driver = {
94 .id_table = id_table_combined,
98 packet_format_1, /* b0:status, b1:payload count */
99 packet_format_2 /* b0[7:3]:status, b0[2:0]:payload count */
102 struct cypress_private {
103 spinlock_t lock; /* private lock */
104 int chiptype; /* identifier of device, for quirks/etc */
105 int bytes_in; /* used for statistics */
106 int bytes_out; /* used for statistics */
107 int cmd_count; /* used for statistics */
108 int cmd_ctrl; /* always set this to 1 before issuing a command */
109 struct kfifo write_fifo; /* write fifo */
110 int write_urb_in_use; /* write urb in use indicator */
111 int write_urb_interval; /* interval to use for write urb */
112 int read_urb_interval; /* interval to use for read urb */
113 int comm_is_ok; /* true if communication is (still) ok */
114 int termios_initialized;
115 __u8 line_control; /* holds dtr / rts value */
116 __u8 current_status; /* received from last read - info on dsr,cts,cd,ri,etc */
117 __u8 current_config; /* stores the current configuration byte */
118 __u8 rx_flags; /* throttling - used from whiteheat/ftdi_sio */
119 enum packet_format pkt_fmt; /* format to use for packet send / receive */
120 int get_cfg_unsafe; /* If true, the CYPRESS_GET_CONFIG is unsafe */
121 int baud_rate; /* stores current baud rate in
123 int isthrottled; /* if throttled, discard reads */
124 wait_queue_head_t delta_msr_wait; /* used for TIOCMIWAIT */
125 char prev_status, diff_status; /* used for TIOCMIWAIT */
126 /* we pass a pointer to this as the argument sent to
127 cypress_set_termios old_termios */
128 struct ktermios tmp_termios; /* stores the old termios settings */
131 /* function prototypes for the Cypress USB to serial device */
132 static int cypress_earthmate_startup(struct usb_serial *serial);
133 static int cypress_hidcom_startup(struct usb_serial *serial);
134 static int cypress_ca42v2_startup(struct usb_serial *serial);
135 static void cypress_release(struct usb_serial *serial);
136 static int cypress_open(struct tty_struct *tty, struct usb_serial_port *port);
137 static void cypress_close(struct usb_serial_port *port);
138 static void cypress_dtr_rts(struct usb_serial_port *port, int on);
139 static int cypress_write(struct tty_struct *tty, struct usb_serial_port *port,
140 const unsigned char *buf, int count);
141 static void cypress_send(struct usb_serial_port *port);
142 static int cypress_write_room(struct tty_struct *tty);
143 static int cypress_ioctl(struct tty_struct *tty,
144 unsigned int cmd, unsigned long arg);
145 static void cypress_set_termios(struct tty_struct *tty,
146 struct usb_serial_port *port, struct ktermios *old);
147 static int cypress_tiocmget(struct tty_struct *tty);
148 static int cypress_tiocmset(struct tty_struct *tty,
149 unsigned int set, unsigned int clear);
150 static int cypress_chars_in_buffer(struct tty_struct *tty);
151 static void cypress_throttle(struct tty_struct *tty);
152 static void cypress_unthrottle(struct tty_struct *tty);
153 static void cypress_set_dead(struct usb_serial_port *port);
154 static void cypress_read_int_callback(struct urb *urb);
155 static void cypress_write_int_callback(struct urb *urb);
157 static struct usb_serial_driver cypress_earthmate_device = {
159 .owner = THIS_MODULE,
162 .description = "DeLorme Earthmate USB",
163 .id_table = id_table_earthmate,
165 .attach = cypress_earthmate_startup,
166 .release = cypress_release,
167 .open = cypress_open,
168 .close = cypress_close,
169 .dtr_rts = cypress_dtr_rts,
170 .write = cypress_write,
171 .write_room = cypress_write_room,
172 .ioctl = cypress_ioctl,
173 .set_termios = cypress_set_termios,
174 .tiocmget = cypress_tiocmget,
175 .tiocmset = cypress_tiocmset,
176 .chars_in_buffer = cypress_chars_in_buffer,
177 .throttle = cypress_throttle,
178 .unthrottle = cypress_unthrottle,
179 .read_int_callback = cypress_read_int_callback,
180 .write_int_callback = cypress_write_int_callback,
183 static struct usb_serial_driver cypress_hidcom_device = {
185 .owner = THIS_MODULE,
188 .description = "HID->COM RS232 Adapter",
189 .id_table = id_table_cyphidcomrs232,
191 .attach = cypress_hidcom_startup,
192 .release = cypress_release,
193 .open = cypress_open,
194 .close = cypress_close,
195 .dtr_rts = cypress_dtr_rts,
196 .write = cypress_write,
197 .write_room = cypress_write_room,
198 .ioctl = cypress_ioctl,
199 .set_termios = cypress_set_termios,
200 .tiocmget = cypress_tiocmget,
201 .tiocmset = cypress_tiocmset,
202 .chars_in_buffer = cypress_chars_in_buffer,
203 .throttle = cypress_throttle,
204 .unthrottle = cypress_unthrottle,
205 .read_int_callback = cypress_read_int_callback,
206 .write_int_callback = cypress_write_int_callback,
209 static struct usb_serial_driver cypress_ca42v2_device = {
211 .owner = THIS_MODULE,
212 .name = "nokiaca42v2",
214 .description = "Nokia CA-42 V2 Adapter",
215 .id_table = id_table_nokiaca42v2,
217 .attach = cypress_ca42v2_startup,
218 .release = cypress_release,
219 .open = cypress_open,
220 .close = cypress_close,
221 .dtr_rts = cypress_dtr_rts,
222 .write = cypress_write,
223 .write_room = cypress_write_room,
224 .ioctl = cypress_ioctl,
225 .set_termios = cypress_set_termios,
226 .tiocmget = cypress_tiocmget,
227 .tiocmset = cypress_tiocmset,
228 .chars_in_buffer = cypress_chars_in_buffer,
229 .throttle = cypress_throttle,
230 .unthrottle = cypress_unthrottle,
231 .read_int_callback = cypress_read_int_callback,
232 .write_int_callback = cypress_write_int_callback,
235 static struct usb_serial_driver * const serial_drivers[] = {
236 &cypress_earthmate_device, &cypress_hidcom_device,
237 &cypress_ca42v2_device, NULL
240 /*****************************************************************************
241 * Cypress serial helper functions
242 *****************************************************************************/
245 static int analyze_baud_rate(struct usb_serial_port *port, speed_t new_rate)
247 struct cypress_private *priv;
248 priv = usb_get_serial_port_data(port);
254 * The general purpose firmware for the Cypress M8 allows for
255 * a maximum speed of 57600bps (I have no idea whether DeLorme
256 * chose to use the general purpose firmware or not), if you
257 * need to modify this speed setting for your own project
258 * please add your own chiptype and modify the code likewise.
259 * The Cypress HID->COM device will work successfully up to
260 * 115200bps (but the actual throughput is around 3kBps).
262 if (port->serial->dev->speed == USB_SPEED_LOW) {
264 * Mike Isely <isely@pobox.com> 2-Feb-2008: The
265 * Cypress app note that describes this mechanism
266 * states the the low-speed part can't handle more
267 * than 800 bytes/sec, in which case 4800 baud is the
268 * safest speed for a part like that.
270 if (new_rate > 4800) {
271 dbg("%s - failed setting baud rate, device incapable "
272 "speed %d", __func__, new_rate);
276 switch (priv->chiptype) {
278 if (new_rate <= 600) {
279 /* 300 and 600 baud rates are supported under
280 * the generic firmware, but are not used with
281 * NMEA and SiRF protocols */
282 dbg("%s - failed setting baud rate, unsupported speed "
283 "of %d on Earthmate GPS", __func__, new_rate);
294 /* This function can either set or retrieve the current serial line settings */
295 static int cypress_serial_control(struct tty_struct *tty,
296 struct usb_serial_port *port, speed_t baud_rate, int data_bits,
297 int stop_bits, int parity_enable, int parity_type, int reset,
298 int cypress_request_type)
300 int new_baudrate = 0, retval = 0, tries = 0;
301 struct cypress_private *priv;
303 const unsigned int feature_len = 5;
306 priv = usb_get_serial_port_data(port);
308 if (!priv->comm_is_ok)
311 feature_buffer = kcalloc(feature_len, sizeof(u8), GFP_KERNEL);
315 switch (cypress_request_type) {
316 case CYPRESS_SET_CONFIG:
317 /* 0 means 'Hang up' so doesn't change the true bit rate */
318 new_baudrate = priv->baud_rate;
319 if (baud_rate && baud_rate != priv->baud_rate) {
320 dbg("%s - baud rate is changing", __func__);
321 retval = analyze_baud_rate(port, baud_rate);
323 new_baudrate = retval;
324 dbg("%s - New baud rate set to %d",
325 __func__, new_baudrate);
328 dbg("%s - baud rate is being sent as %d",
329 __func__, new_baudrate);
331 /* fill the feature_buffer with new configuration */
332 put_unaligned_le32(new_baudrate, feature_buffer);
333 feature_buffer[4] |= data_bits; /* assign data bits in 2 bit space ( max 3 ) */
335 feature_buffer[4] |= (stop_bits << 3); /* assign stop bits in 1 bit space */
336 feature_buffer[4] |= (parity_enable << 4); /* assign parity flag in 1 bit space */
337 feature_buffer[4] |= (parity_type << 5); /* assign parity type in 1 bit space */
339 feature_buffer[4] |= (reset << 7); /* assign reset at end of byte, 1 bit space */
341 dbg("%s - device is being sent this feature report:",
343 dbg("%s - %02X - %02X - %02X - %02X - %02X", __func__,
344 feature_buffer[0], feature_buffer[1],
345 feature_buffer[2], feature_buffer[3],
349 retval = usb_control_msg(port->serial->dev,
350 usb_sndctrlpipe(port->serial->dev, 0),
352 USB_DIR_OUT | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
353 0x0300, 0, feature_buffer,
359 } while (retval != feature_len &&
362 if (retval != feature_len) {
363 dev_err(&port->dev, "%s - failed sending serial "
364 "line settings - %d\n", __func__, retval);
365 cypress_set_dead(port);
367 spin_lock_irqsave(&priv->lock, flags);
368 priv->baud_rate = new_baudrate;
369 priv->current_config = feature_buffer[4];
370 spin_unlock_irqrestore(&priv->lock, flags);
371 /* If we asked for a speed change encode it */
373 tty_encode_baud_rate(tty,
374 new_baudrate, new_baudrate);
377 case CYPRESS_GET_CONFIG:
378 if (priv->get_cfg_unsafe) {
379 /* Not implemented for this device,
380 and if we try to do it we're likely
381 to crash the hardware. */
385 dbg("%s - retreiving serial line settings", __func__);
387 retval = usb_control_msg(port->serial->dev,
388 usb_rcvctrlpipe(port->serial->dev, 0),
390 USB_DIR_IN | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
391 0x0300, 0, feature_buffer,
396 } while (retval != feature_len
397 && retval != -ENODEV);
399 if (retval != feature_len) {
400 dev_err(&port->dev, "%s - failed to retrieve serial "
401 "line settings - %d\n", __func__, retval);
402 cypress_set_dead(port);
405 spin_lock_irqsave(&priv->lock, flags);
406 /* store the config in one byte, and later
407 use bit masks to check values */
408 priv->current_config = feature_buffer[4];
409 priv->baud_rate = get_unaligned_le32(feature_buffer);
410 spin_unlock_irqrestore(&priv->lock, flags);
413 spin_lock_irqsave(&priv->lock, flags);
415 spin_unlock_irqrestore(&priv->lock, flags);
417 kfree(feature_buffer);
419 } /* cypress_serial_control */
422 static void cypress_set_dead(struct usb_serial_port *port)
424 struct cypress_private *priv = usb_get_serial_port_data(port);
427 spin_lock_irqsave(&priv->lock, flags);
428 if (!priv->comm_is_ok) {
429 spin_unlock_irqrestore(&priv->lock, flags);
432 priv->comm_is_ok = 0;
433 spin_unlock_irqrestore(&priv->lock, flags);
435 dev_err(&port->dev, "cypress_m8 suspending failing port %d - "
436 "interval might be too short\n", port->number);
440 /*****************************************************************************
441 * Cypress serial driver functions
442 *****************************************************************************/
445 static int generic_startup(struct usb_serial *serial)
447 struct cypress_private *priv;
448 struct usb_serial_port *port = serial->port[0];
450 priv = kzalloc(sizeof(struct cypress_private), GFP_KERNEL);
454 priv->comm_is_ok = !0;
455 spin_lock_init(&priv->lock);
456 if (kfifo_alloc(&priv->write_fifo, CYPRESS_BUF_SIZE, GFP_KERNEL)) {
460 init_waitqueue_head(&priv->delta_msr_wait);
462 usb_reset_configuration(serial->dev);
465 priv->line_control = 0;
466 priv->termios_initialized = 0;
468 /* Default packet format setting is determined by packet size.
469 Anything with a size larger then 9 must have a separate
470 count field since the 3 bit count field is otherwise too
471 small. Otherwise we can use the slightly more compact
472 format. This is in accordance with the cypress_m8 serial
473 converter app note. */
474 if (port->interrupt_out_size > 9)
475 priv->pkt_fmt = packet_format_1;
477 priv->pkt_fmt = packet_format_2;
480 priv->write_urb_interval = interval;
481 priv->read_urb_interval = interval;
482 dbg("%s - port %d read & write intervals forced to %d",
483 __func__, port->number, interval);
485 priv->write_urb_interval = port->interrupt_out_urb->interval;
486 priv->read_urb_interval = port->interrupt_in_urb->interval;
487 dbg("%s - port %d intervals: read=%d write=%d",
488 __func__, port->number,
489 priv->read_urb_interval, priv->write_urb_interval);
491 usb_set_serial_port_data(port, priv);
497 static int cypress_earthmate_startup(struct usb_serial *serial)
499 struct cypress_private *priv;
500 struct usb_serial_port *port = serial->port[0];
502 if (generic_startup(serial)) {
503 dbg("%s - Failed setting up port %d", __func__,
508 priv = usb_get_serial_port_data(port);
509 priv->chiptype = CT_EARTHMATE;
510 /* All Earthmate devices use the separated-count packet
512 priv->pkt_fmt = packet_format_1;
513 if (serial->dev->descriptor.idProduct !=
514 cpu_to_le16(PRODUCT_ID_EARTHMATEUSB)) {
515 /* The old original USB Earthmate seemed able to
516 handle GET_CONFIG requests; everything they've
517 produced since that time crashes if this command is
519 dbg("%s - Marking this device as unsafe for GET_CONFIG "
520 "commands", __func__);
521 priv->get_cfg_unsafe = !0;
525 } /* cypress_earthmate_startup */
528 static int cypress_hidcom_startup(struct usb_serial *serial)
530 struct cypress_private *priv;
532 if (generic_startup(serial)) {
533 dbg("%s - Failed setting up port %d", __func__,
534 serial->port[0]->number);
538 priv = usb_get_serial_port_data(serial->port[0]);
539 priv->chiptype = CT_CYPHIDCOM;
542 } /* cypress_hidcom_startup */
545 static int cypress_ca42v2_startup(struct usb_serial *serial)
547 struct cypress_private *priv;
549 if (generic_startup(serial)) {
550 dbg("%s - Failed setting up port %d", __func__,
551 serial->port[0]->number);
555 priv = usb_get_serial_port_data(serial->port[0]);
556 priv->chiptype = CT_CA42V2;
559 } /* cypress_ca42v2_startup */
562 static void cypress_release(struct usb_serial *serial)
564 struct cypress_private *priv;
566 /* all open ports are closed at this point */
567 priv = usb_get_serial_port_data(serial->port[0]);
570 kfifo_free(&priv->write_fifo);
576 static int cypress_open(struct tty_struct *tty, struct usb_serial_port *port)
578 struct cypress_private *priv = usb_get_serial_port_data(port);
579 struct usb_serial *serial = port->serial;
583 if (!priv->comm_is_ok)
586 /* clear halts before open */
587 usb_clear_halt(serial->dev, 0x81);
588 usb_clear_halt(serial->dev, 0x02);
590 spin_lock_irqsave(&priv->lock, flags);
591 /* reset read/write statistics */
596 spin_unlock_irqrestore(&priv->lock, flags);
602 cypress_set_termios(tty, port, &priv->tmp_termios);
604 /* setup the port and start reading from the device */
605 if (!port->interrupt_in_urb) {
606 dev_err(&port->dev, "%s - interrupt_in_urb is empty!\n",
611 usb_fill_int_urb(port->interrupt_in_urb, serial->dev,
612 usb_rcvintpipe(serial->dev, port->interrupt_in_endpointAddress),
613 port->interrupt_in_urb->transfer_buffer,
614 port->interrupt_in_urb->transfer_buffer_length,
615 cypress_read_int_callback, port, priv->read_urb_interval);
616 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
620 "%s - failed submitting read urb, error %d\n",
622 cypress_set_dead(port);
624 port->port.drain_delay = 256;
628 static void cypress_dtr_rts(struct usb_serial_port *port, int on)
630 struct cypress_private *priv = usb_get_serial_port_data(port);
631 /* drop dtr and rts */
632 spin_lock_irq(&priv->lock);
634 priv->line_control = 0;
636 priv->line_control = CONTROL_DTR | CONTROL_RTS;
638 spin_unlock_irq(&priv->lock);
639 cypress_write(NULL, port, NULL, 0);
642 static void cypress_close(struct usb_serial_port *port)
644 struct cypress_private *priv = usb_get_serial_port_data(port);
647 /* writing is potentially harmful, lock must be taken */
648 mutex_lock(&port->serial->disc_mutex);
649 if (port->serial->disconnected) {
650 mutex_unlock(&port->serial->disc_mutex);
653 spin_lock_irqsave(&priv->lock, flags);
654 kfifo_reset_out(&priv->write_fifo);
655 spin_unlock_irqrestore(&priv->lock, flags);
657 dbg("%s - stopping urbs", __func__);
658 usb_kill_urb(port->interrupt_in_urb);
659 usb_kill_urb(port->interrupt_out_urb);
662 dev_info(&port->dev, "Statistics: %d Bytes In | %d Bytes Out | %d Commands Issued\n",
663 priv->bytes_in, priv->bytes_out, priv->cmd_count);
664 mutex_unlock(&port->serial->disc_mutex);
665 } /* cypress_close */
668 static int cypress_write(struct tty_struct *tty, struct usb_serial_port *port,
669 const unsigned char *buf, int count)
671 struct cypress_private *priv = usb_get_serial_port_data(port);
673 dbg("%s - port %d, %d bytes", __func__, port->number, count);
675 /* line control commands, which need to be executed immediately,
676 are not put into the buffer for obvious reasons.
678 if (priv->cmd_ctrl) {
686 count = kfifo_in_locked(&priv->write_fifo, buf, count, &priv->lock);
692 } /* cypress_write */
695 static void cypress_send(struct usb_serial_port *port)
697 int count = 0, result, offset, actual_size;
698 struct cypress_private *priv = usb_get_serial_port_data(port);
701 if (!priv->comm_is_ok)
704 dbg("%s - interrupt out size is %d", __func__,
705 port->interrupt_out_size);
707 spin_lock_irqsave(&priv->lock, flags);
708 if (priv->write_urb_in_use) {
709 dbg("%s - can't write, urb in use", __func__);
710 spin_unlock_irqrestore(&priv->lock, flags);
713 spin_unlock_irqrestore(&priv->lock, flags);
716 memset(port->interrupt_out_urb->transfer_buffer, 0,
717 port->interrupt_out_size);
719 spin_lock_irqsave(&priv->lock, flags);
720 switch (priv->pkt_fmt) {
722 case packet_format_1:
723 /* this is for the CY7C64013... */
725 port->interrupt_out_buffer[0] = priv->line_control;
727 case packet_format_2:
728 /* this is for the CY7C63743... */
730 port->interrupt_out_buffer[0] = priv->line_control;
734 if (priv->line_control & CONTROL_RESET)
735 priv->line_control &= ~CONTROL_RESET;
737 if (priv->cmd_ctrl) {
739 dbg("%s - line control command being issued", __func__);
740 spin_unlock_irqrestore(&priv->lock, flags);
743 spin_unlock_irqrestore(&priv->lock, flags);
745 count = kfifo_out_locked(&priv->write_fifo,
746 &port->interrupt_out_buffer[offset],
747 port->interrupt_out_size - offset,
752 switch (priv->pkt_fmt) {
754 case packet_format_1:
755 port->interrupt_out_buffer[1] = count;
757 case packet_format_2:
758 port->interrupt_out_buffer[0] |= count;
761 dbg("%s - count is %d", __func__, count);
764 spin_lock_irqsave(&priv->lock, flags);
765 priv->write_urb_in_use = 1;
766 spin_unlock_irqrestore(&priv->lock, flags);
771 actual_size = count +
772 (priv->pkt_fmt == packet_format_1 ? 2 : 1);
774 usb_serial_debug_data(debug, &port->dev, __func__,
775 port->interrupt_out_size,
776 port->interrupt_out_urb->transfer_buffer);
778 usb_fill_int_urb(port->interrupt_out_urb, port->serial->dev,
779 usb_sndintpipe(port->serial->dev, port->interrupt_out_endpointAddress),
780 port->interrupt_out_buffer, port->interrupt_out_size,
781 cypress_write_int_callback, port, priv->write_urb_interval);
782 result = usb_submit_urb(port->interrupt_out_urb, GFP_ATOMIC);
784 dev_err_console(port,
785 "%s - failed submitting write urb, error %d\n",
787 priv->write_urb_in_use = 0;
788 cypress_set_dead(port);
791 spin_lock_irqsave(&priv->lock, flags);
795 /* do not count the line control and size bytes */
796 priv->bytes_out += count;
797 spin_unlock_irqrestore(&priv->lock, flags);
799 usb_serial_port_softint(port);
803 /* returns how much space is available in the soft buffer */
804 static int cypress_write_room(struct tty_struct *tty)
806 struct usb_serial_port *port = tty->driver_data;
807 struct cypress_private *priv = usb_get_serial_port_data(port);
811 spin_lock_irqsave(&priv->lock, flags);
812 room = kfifo_avail(&priv->write_fifo);
813 spin_unlock_irqrestore(&priv->lock, flags);
815 dbg("%s - returns %d", __func__, room);
820 static int cypress_tiocmget(struct tty_struct *tty)
822 struct usb_serial_port *port = tty->driver_data;
823 struct cypress_private *priv = usb_get_serial_port_data(port);
824 __u8 status, control;
825 unsigned int result = 0;
828 spin_lock_irqsave(&priv->lock, flags);
829 control = priv->line_control;
830 status = priv->current_status;
831 spin_unlock_irqrestore(&priv->lock, flags);
833 result = ((control & CONTROL_DTR) ? TIOCM_DTR : 0)
834 | ((control & CONTROL_RTS) ? TIOCM_RTS : 0)
835 | ((status & UART_CTS) ? TIOCM_CTS : 0)
836 | ((status & UART_DSR) ? TIOCM_DSR : 0)
837 | ((status & UART_RI) ? TIOCM_RI : 0)
838 | ((status & UART_CD) ? TIOCM_CD : 0);
840 dbg("%s - result = %x", __func__, result);
846 static int cypress_tiocmset(struct tty_struct *tty,
847 unsigned int set, unsigned int clear)
849 struct usb_serial_port *port = tty->driver_data;
850 struct cypress_private *priv = usb_get_serial_port_data(port);
853 spin_lock_irqsave(&priv->lock, flags);
855 priv->line_control |= CONTROL_RTS;
857 priv->line_control |= CONTROL_DTR;
858 if (clear & TIOCM_RTS)
859 priv->line_control &= ~CONTROL_RTS;
860 if (clear & TIOCM_DTR)
861 priv->line_control &= ~CONTROL_DTR;
863 spin_unlock_irqrestore(&priv->lock, flags);
865 return cypress_write(tty, port, NULL, 0);
869 static int cypress_ioctl(struct tty_struct *tty,
870 unsigned int cmd, unsigned long arg)
872 struct usb_serial_port *port = tty->driver_data;
873 struct cypress_private *priv = usb_get_serial_port_data(port);
875 dbg("%s - port %d, cmd 0x%.4x", __func__, port->number, cmd);
878 /* This code comes from drivers/char/serial.c and ftdi_sio.c */
880 while (priv != NULL) {
881 interruptible_sleep_on(&priv->delta_msr_wait);
882 /* see if a signal did it */
883 if (signal_pending(current))
886 char diff = priv->diff_status;
888 return -EIO; /* no change => error */
890 /* consume all events */
891 priv->diff_status = 0;
893 /* return 0 if caller wanted to know about
895 if (((arg & TIOCM_RNG) && (diff & UART_RI)) ||
896 ((arg & TIOCM_DSR) && (diff & UART_DSR)) ||
897 ((arg & TIOCM_CD) && (diff & UART_CD)) ||
898 ((arg & TIOCM_CTS) && (diff & UART_CTS)))
900 /* otherwise caller can't care less about what
901 * happened, and so we continue to wait for
910 dbg("%s - arg not supported - it was 0x%04x - check include/asm/ioctls.h", __func__, cmd);
912 } /* cypress_ioctl */
915 static void cypress_set_termios(struct tty_struct *tty,
916 struct usb_serial_port *port, struct ktermios *old_termios)
918 struct cypress_private *priv = usb_get_serial_port_data(port);
919 int data_bits, stop_bits, parity_type, parity_enable;
920 unsigned cflag, iflag;
925 spin_lock_irqsave(&priv->lock, flags);
926 /* We can't clean this one up as we don't know the device type
928 if (!priv->termios_initialized) {
929 if (priv->chiptype == CT_EARTHMATE) {
930 *(tty->termios) = tty_std_termios;
931 tty->termios->c_cflag = B4800 | CS8 | CREAD | HUPCL |
933 tty->termios->c_ispeed = 4800;
934 tty->termios->c_ospeed = 4800;
935 } else if (priv->chiptype == CT_CYPHIDCOM) {
936 *(tty->termios) = tty_std_termios;
937 tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
939 tty->termios->c_ispeed = 9600;
940 tty->termios->c_ospeed = 9600;
941 } else if (priv->chiptype == CT_CA42V2) {
942 *(tty->termios) = tty_std_termios;
943 tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
945 tty->termios->c_ispeed = 9600;
946 tty->termios->c_ospeed = 9600;
948 priv->termios_initialized = 1;
950 spin_unlock_irqrestore(&priv->lock, flags);
952 /* Unsupported features need clearing */
953 tty->termios->c_cflag &= ~(CMSPAR|CRTSCTS);
955 cflag = tty->termios->c_cflag;
956 iflag = tty->termios->c_iflag;
958 /* check if there are new settings */
960 spin_lock_irqsave(&priv->lock, flags);
961 priv->tmp_termios = *(tty->termios);
962 spin_unlock_irqrestore(&priv->lock, flags);
965 /* set number of data bits, parity, stop bits */
966 /* when parity is disabled the parity type bit is ignored */
968 /* 1 means 2 stop bits, 0 means 1 stop bit */
969 stop_bits = cflag & CSTOPB ? 1 : 0;
971 if (cflag & PARENB) {
973 /* 1 means odd parity, 0 means even parity */
974 parity_type = cflag & PARODD ? 1 : 0;
976 parity_enable = parity_type = 0;
978 switch (cflag & CSIZE) {
992 dev_err(&port->dev, "%s - CSIZE was set, but not CS5-CS8\n",
996 spin_lock_irqsave(&priv->lock, flags);
997 oldlines = priv->line_control;
998 if ((cflag & CBAUD) == B0) {
999 /* drop dtr and rts */
1000 dbg("%s - dropping the lines, baud rate 0bps", __func__);
1001 priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
1003 priv->line_control = (CONTROL_DTR | CONTROL_RTS);
1004 spin_unlock_irqrestore(&priv->lock, flags);
1006 dbg("%s - sending %d stop_bits, %d parity_enable, %d parity_type, "
1007 "%d data_bits (+5)", __func__, stop_bits,
1008 parity_enable, parity_type, data_bits);
1010 cypress_serial_control(tty, port, tty_get_baud_rate(tty),
1011 data_bits, stop_bits,
1012 parity_enable, parity_type,
1013 0, CYPRESS_SET_CONFIG);
1015 /* we perform a CYPRESS_GET_CONFIG so that the current settings are
1016 * filled into the private structure this should confirm that all is
1017 * working if it returns what we just set */
1018 cypress_serial_control(tty, port, 0, 0, 0, 0, 0, 0, CYPRESS_GET_CONFIG);
1020 /* Here we can define custom tty settings for devices; the main tty
1021 * termios flag base comes from empeg.c */
1023 spin_lock_irqsave(&priv->lock, flags);
1024 if (priv->chiptype == CT_EARTHMATE && priv->baud_rate == 4800) {
1025 dbg("Using custom termios settings for a baud rate of "
1027 /* define custom termios settings for NMEA protocol */
1029 tty->termios->c_iflag /* input modes - */
1030 &= ~(IGNBRK /* disable ignore break */
1031 | BRKINT /* disable break causes interrupt */
1032 | PARMRK /* disable mark parity errors */
1033 | ISTRIP /* disable clear high bit of input char */
1034 | INLCR /* disable translate NL to CR */
1035 | IGNCR /* disable ignore CR */
1036 | ICRNL /* disable translate CR to NL */
1037 | IXON); /* disable enable XON/XOFF flow control */
1039 tty->termios->c_oflag /* output modes */
1040 &= ~OPOST; /* disable postprocess output char */
1042 tty->termios->c_lflag /* line discipline modes */
1043 &= ~(ECHO /* disable echo input characters */
1044 | ECHONL /* disable echo new line */
1045 | ICANON /* disable erase, kill, werase, and rprnt
1046 special characters */
1047 | ISIG /* disable interrupt, quit, and suspend
1048 special characters */
1049 | IEXTEN); /* disable non-POSIX special characters */
1050 } /* CT_CYPHIDCOM: Application should handle this for device */
1052 linechange = (priv->line_control != oldlines);
1053 spin_unlock_irqrestore(&priv->lock, flags);
1055 /* if necessary, set lines */
1058 cypress_write(tty, port, NULL, 0);
1060 } /* cypress_set_termios */
1063 /* returns amount of data still left in soft buffer */
1064 static int cypress_chars_in_buffer(struct tty_struct *tty)
1066 struct usb_serial_port *port = tty->driver_data;
1067 struct cypress_private *priv = usb_get_serial_port_data(port);
1069 unsigned long flags;
1071 spin_lock_irqsave(&priv->lock, flags);
1072 chars = kfifo_len(&priv->write_fifo);
1073 spin_unlock_irqrestore(&priv->lock, flags);
1075 dbg("%s - returns %d", __func__, chars);
1080 static void cypress_throttle(struct tty_struct *tty)
1082 struct usb_serial_port *port = tty->driver_data;
1083 struct cypress_private *priv = usb_get_serial_port_data(port);
1085 spin_lock_irq(&priv->lock);
1086 priv->rx_flags = THROTTLED;
1087 spin_unlock_irq(&priv->lock);
1091 static void cypress_unthrottle(struct tty_struct *tty)
1093 struct usb_serial_port *port = tty->driver_data;
1094 struct cypress_private *priv = usb_get_serial_port_data(port);
1095 int actually_throttled, result;
1097 spin_lock_irq(&priv->lock);
1098 actually_throttled = priv->rx_flags & ACTUALLY_THROTTLED;
1100 spin_unlock_irq(&priv->lock);
1102 if (!priv->comm_is_ok)
1105 if (actually_throttled) {
1106 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
1108 dev_err(&port->dev, "%s - failed submitting read urb, "
1109 "error %d\n", __func__, result);
1110 cypress_set_dead(port);
1116 static void cypress_read_int_callback(struct urb *urb)
1118 struct usb_serial_port *port = urb->context;
1119 struct cypress_private *priv = usb_get_serial_port_data(port);
1120 struct tty_struct *tty;
1121 unsigned char *data = urb->transfer_buffer;
1122 unsigned long flags;
1123 char tty_flag = TTY_NORMAL;
1128 int status = urb->status;
1131 case 0: /* success */
1136 /* precursor to disconnect so just go away */
1139 /* Can't call usb_clear_halt while in_interrupt */
1142 /* something ugly is going on... */
1143 dev_err(&urb->dev->dev,
1144 "%s - unexpected nonzero read status received: %d\n",
1146 cypress_set_dead(port);
1150 spin_lock_irqsave(&priv->lock, flags);
1151 if (priv->rx_flags & THROTTLED) {
1152 dbg("%s - now throttling", __func__);
1153 priv->rx_flags |= ACTUALLY_THROTTLED;
1154 spin_unlock_irqrestore(&priv->lock, flags);
1157 spin_unlock_irqrestore(&priv->lock, flags);
1159 tty = tty_port_tty_get(&port->port);
1161 dbg("%s - bad tty pointer - exiting", __func__);
1165 spin_lock_irqsave(&priv->lock, flags);
1166 result = urb->actual_length;
1167 switch (priv->pkt_fmt) {
1169 case packet_format_1:
1170 /* This is for the CY7C64013... */
1171 priv->current_status = data[0] & 0xF8;
1172 bytes = data[1] + 2;
1177 case packet_format_2:
1178 /* This is for the CY7C63743... */
1179 priv->current_status = data[0] & 0xF8;
1180 bytes = (data[0] & 0x07) + 1;
1186 spin_unlock_irqrestore(&priv->lock, flags);
1187 if (result < bytes) {
1188 dbg("%s - wrong packet size - received %d bytes but packet "
1189 "said %d bytes", __func__, result, bytes);
1193 usb_serial_debug_data(debug, &port->dev, __func__,
1194 urb->actual_length, data);
1196 spin_lock_irqsave(&priv->lock, flags);
1197 /* check to see if status has changed */
1198 if (priv->current_status != priv->prev_status) {
1199 priv->diff_status |= priv->current_status ^
1201 wake_up_interruptible(&priv->delta_msr_wait);
1202 priv->prev_status = priv->current_status;
1204 spin_unlock_irqrestore(&priv->lock, flags);
1206 /* hangup, as defined in acm.c... this might be a bad place for it
1208 if (tty && !(tty->termios->c_cflag & CLOCAL) &&
1209 !(priv->current_status & UART_CD)) {
1210 dbg("%s - calling hangup", __func__);
1215 /* There is one error bit... I'm assuming it is a parity error
1216 * indicator as the generic firmware will set this bit to 1 if a
1217 * parity error occurs.
1218 * I can not find reference to any other error events. */
1219 spin_lock_irqsave(&priv->lock, flags);
1220 if (priv->current_status & CYP_ERROR) {
1221 spin_unlock_irqrestore(&priv->lock, flags);
1222 tty_flag = TTY_PARITY;
1223 dbg("%s - Parity Error detected", __func__);
1225 spin_unlock_irqrestore(&priv->lock, flags);
1227 /* process read if there is data other than line status */
1228 if (tty && bytes > i) {
1229 tty_insert_flip_string_fixed_flag(tty, data + i,
1230 tty_flag, bytes - i);
1231 tty_flip_buffer_push(tty);
1234 spin_lock_irqsave(&priv->lock, flags);
1235 /* control and status byte(s) are also counted */
1236 priv->bytes_in += bytes;
1237 spin_unlock_irqrestore(&priv->lock, flags);
1242 /* Continue trying to always read */
1244 if (priv->comm_is_ok) {
1245 usb_fill_int_urb(port->interrupt_in_urb, port->serial->dev,
1246 usb_rcvintpipe(port->serial->dev,
1247 port->interrupt_in_endpointAddress),
1248 port->interrupt_in_urb->transfer_buffer,
1249 port->interrupt_in_urb->transfer_buffer_length,
1250 cypress_read_int_callback, port,
1251 priv->read_urb_interval);
1252 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
1253 if (result && result != -EPERM) {
1254 dev_err(&urb->dev->dev, "%s - failed resubmitting "
1255 "read urb, error %d\n", __func__,
1257 cypress_set_dead(port);
1260 } /* cypress_read_int_callback */
1263 static void cypress_write_int_callback(struct urb *urb)
1265 struct usb_serial_port *port = urb->context;
1266 struct cypress_private *priv = usb_get_serial_port_data(port);
1268 int status = urb->status;
1277 /* this urb is terminated, clean up */
1278 dbg("%s - urb shutting down with status: %d",
1280 priv->write_urb_in_use = 0;
1282 case -EPIPE: /* no break needed; clear halt and resubmit */
1283 if (!priv->comm_is_ok)
1285 usb_clear_halt(port->serial->dev, 0x02);
1286 /* error in the urb, so we have to resubmit it */
1287 dbg("%s - nonzero write bulk status received: %d",
1289 port->interrupt_out_urb->transfer_buffer_length = 1;
1290 result = usb_submit_urb(port->interrupt_out_urb, GFP_ATOMIC);
1293 dev_err(&urb->dev->dev,
1294 "%s - failed resubmitting write urb, error %d\n",
1296 cypress_set_dead(port);
1299 dev_err(&urb->dev->dev,
1300 "%s - unexpected nonzero write status received: %d\n",
1302 cypress_set_dead(port);
1305 priv->write_urb_in_use = 0;
1307 /* send any buffered data */
1311 module_usb_serial_driver(cypress_driver, serial_drivers);
1313 MODULE_AUTHOR(DRIVER_AUTHOR);
1314 MODULE_DESCRIPTION(DRIVER_DESC);
1315 MODULE_VERSION(DRIVER_VERSION);
1316 MODULE_LICENSE("GPL");
1318 module_param(debug, bool, S_IRUGO | S_IWUSR);
1319 MODULE_PARM_DESC(debug, "Debug enabled or not");
1320 module_param(stats, bool, S_IRUGO | S_IWUSR);
1321 MODULE_PARM_DESC(stats, "Enable statistics or not");
1322 module_param(interval, int, S_IRUGO | S_IWUSR);
1323 MODULE_PARM_DESC(interval, "Overrides interrupt interval");
1324 module_param(unstable_bauds, bool, S_IRUGO | S_IWUSR);
1325 MODULE_PARM_DESC(unstable_bauds, "Allow unstable baud rates");