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 .disconnect = usb_serial_disconnect,
95 .id_table = id_table_combined,
99 packet_format_1, /* b0:status, b1:payload count */
100 packet_format_2 /* b0[7:3]:status, b0[2:0]:payload count */
103 struct cypress_private {
104 spinlock_t lock; /* private lock */
105 int chiptype; /* identifier of device, for quirks/etc */
106 int bytes_in; /* used for statistics */
107 int bytes_out; /* used for statistics */
108 int cmd_count; /* used for statistics */
109 int cmd_ctrl; /* always set this to 1 before issuing a command */
110 struct kfifo write_fifo; /* write fifo */
111 int write_urb_in_use; /* write urb in use indicator */
112 int write_urb_interval; /* interval to use for write urb */
113 int read_urb_interval; /* interval to use for read urb */
114 int comm_is_ok; /* true if communication is (still) ok */
115 int termios_initialized;
116 __u8 line_control; /* holds dtr / rts value */
117 __u8 current_status; /* received from last read - info on dsr,cts,cd,ri,etc */
118 __u8 current_config; /* stores the current configuration byte */
119 __u8 rx_flags; /* throttling - used from whiteheat/ftdi_sio */
120 enum packet_format pkt_fmt; /* format to use for packet send / receive */
121 int get_cfg_unsafe; /* If true, the CYPRESS_GET_CONFIG is unsafe */
122 int baud_rate; /* stores current baud rate in
124 int isthrottled; /* if throttled, discard reads */
125 wait_queue_head_t delta_msr_wait; /* used for TIOCMIWAIT */
126 char prev_status, diff_status; /* used for TIOCMIWAIT */
127 /* we pass a pointer to this as the argument sent to
128 cypress_set_termios old_termios */
129 struct ktermios tmp_termios; /* stores the old termios settings */
132 /* function prototypes for the Cypress USB to serial device */
133 static int cypress_earthmate_startup(struct usb_serial *serial);
134 static int cypress_hidcom_startup(struct usb_serial *serial);
135 static int cypress_ca42v2_startup(struct usb_serial *serial);
136 static void cypress_release(struct usb_serial *serial);
137 static int cypress_open(struct tty_struct *tty, struct usb_serial_port *port);
138 static void cypress_close(struct usb_serial_port *port);
139 static void cypress_dtr_rts(struct usb_serial_port *port, int on);
140 static int cypress_write(struct tty_struct *tty, struct usb_serial_port *port,
141 const unsigned char *buf, int count);
142 static void cypress_send(struct usb_serial_port *port);
143 static int cypress_write_room(struct tty_struct *tty);
144 static int cypress_ioctl(struct tty_struct *tty,
145 unsigned int cmd, unsigned long arg);
146 static void cypress_set_termios(struct tty_struct *tty,
147 struct usb_serial_port *port, struct ktermios *old);
148 static int cypress_tiocmget(struct tty_struct *tty);
149 static int cypress_tiocmset(struct tty_struct *tty,
150 unsigned int set, unsigned int clear);
151 static int cypress_chars_in_buffer(struct tty_struct *tty);
152 static void cypress_throttle(struct tty_struct *tty);
153 static void cypress_unthrottle(struct tty_struct *tty);
154 static void cypress_set_dead(struct usb_serial_port *port);
155 static void cypress_read_int_callback(struct urb *urb);
156 static void cypress_write_int_callback(struct urb *urb);
158 static struct usb_serial_driver cypress_earthmate_device = {
160 .owner = THIS_MODULE,
163 .description = "DeLorme Earthmate USB",
164 .id_table = id_table_earthmate,
166 .attach = cypress_earthmate_startup,
167 .release = cypress_release,
168 .open = cypress_open,
169 .close = cypress_close,
170 .dtr_rts = cypress_dtr_rts,
171 .write = cypress_write,
172 .write_room = cypress_write_room,
173 .ioctl = cypress_ioctl,
174 .set_termios = cypress_set_termios,
175 .tiocmget = cypress_tiocmget,
176 .tiocmset = cypress_tiocmset,
177 .chars_in_buffer = cypress_chars_in_buffer,
178 .throttle = cypress_throttle,
179 .unthrottle = cypress_unthrottle,
180 .read_int_callback = cypress_read_int_callback,
181 .write_int_callback = cypress_write_int_callback,
184 static struct usb_serial_driver cypress_hidcom_device = {
186 .owner = THIS_MODULE,
189 .description = "HID->COM RS232 Adapter",
190 .id_table = id_table_cyphidcomrs232,
192 .attach = cypress_hidcom_startup,
193 .release = cypress_release,
194 .open = cypress_open,
195 .close = cypress_close,
196 .dtr_rts = cypress_dtr_rts,
197 .write = cypress_write,
198 .write_room = cypress_write_room,
199 .ioctl = cypress_ioctl,
200 .set_termios = cypress_set_termios,
201 .tiocmget = cypress_tiocmget,
202 .tiocmset = cypress_tiocmset,
203 .chars_in_buffer = cypress_chars_in_buffer,
204 .throttle = cypress_throttle,
205 .unthrottle = cypress_unthrottle,
206 .read_int_callback = cypress_read_int_callback,
207 .write_int_callback = cypress_write_int_callback,
210 static struct usb_serial_driver cypress_ca42v2_device = {
212 .owner = THIS_MODULE,
213 .name = "nokiaca42v2",
215 .description = "Nokia CA-42 V2 Adapter",
216 .id_table = id_table_nokiaca42v2,
218 .attach = cypress_ca42v2_startup,
219 .release = cypress_release,
220 .open = cypress_open,
221 .close = cypress_close,
222 .dtr_rts = cypress_dtr_rts,
223 .write = cypress_write,
224 .write_room = cypress_write_room,
225 .ioctl = cypress_ioctl,
226 .set_termios = cypress_set_termios,
227 .tiocmget = cypress_tiocmget,
228 .tiocmset = cypress_tiocmset,
229 .chars_in_buffer = cypress_chars_in_buffer,
230 .throttle = cypress_throttle,
231 .unthrottle = cypress_unthrottle,
232 .read_int_callback = cypress_read_int_callback,
233 .write_int_callback = cypress_write_int_callback,
236 static struct usb_serial_driver * const serial_drivers[] = {
237 &cypress_earthmate_device, &cypress_hidcom_device,
238 &cypress_ca42v2_device, NULL
241 /*****************************************************************************
242 * Cypress serial helper functions
243 *****************************************************************************/
246 static int analyze_baud_rate(struct usb_serial_port *port, speed_t new_rate)
248 struct cypress_private *priv;
249 priv = usb_get_serial_port_data(port);
255 * The general purpose firmware for the Cypress M8 allows for
256 * a maximum speed of 57600bps (I have no idea whether DeLorme
257 * chose to use the general purpose firmware or not), if you
258 * need to modify this speed setting for your own project
259 * please add your own chiptype and modify the code likewise.
260 * The Cypress HID->COM device will work successfully up to
261 * 115200bps (but the actual throughput is around 3kBps).
263 if (port->serial->dev->speed == USB_SPEED_LOW) {
265 * Mike Isely <isely@pobox.com> 2-Feb-2008: The
266 * Cypress app note that describes this mechanism
267 * states the the low-speed part can't handle more
268 * than 800 bytes/sec, in which case 4800 baud is the
269 * safest speed for a part like that.
271 if (new_rate > 4800) {
272 dbg("%s - failed setting baud rate, device incapable "
273 "speed %d", __func__, new_rate);
277 switch (priv->chiptype) {
279 if (new_rate <= 600) {
280 /* 300 and 600 baud rates are supported under
281 * the generic firmware, but are not used with
282 * NMEA and SiRF protocols */
283 dbg("%s - failed setting baud rate, unsupported speed "
284 "of %d on Earthmate GPS", __func__, new_rate);
295 /* This function can either set or retrieve the current serial line settings */
296 static int cypress_serial_control(struct tty_struct *tty,
297 struct usb_serial_port *port, speed_t baud_rate, int data_bits,
298 int stop_bits, int parity_enable, int parity_type, int reset,
299 int cypress_request_type)
301 int new_baudrate = 0, retval = 0, tries = 0;
302 struct cypress_private *priv;
304 const unsigned int feature_len = 5;
307 priv = usb_get_serial_port_data(port);
309 if (!priv->comm_is_ok)
312 feature_buffer = kcalloc(feature_len, sizeof(u8), GFP_KERNEL);
316 switch (cypress_request_type) {
317 case CYPRESS_SET_CONFIG:
318 /* 0 means 'Hang up' so doesn't change the true bit rate */
319 new_baudrate = priv->baud_rate;
320 if (baud_rate && baud_rate != priv->baud_rate) {
321 dbg("%s - baud rate is changing", __func__);
322 retval = analyze_baud_rate(port, baud_rate);
324 new_baudrate = retval;
325 dbg("%s - New baud rate set to %d",
326 __func__, new_baudrate);
329 dbg("%s - baud rate is being sent as %d",
330 __func__, new_baudrate);
332 /* fill the feature_buffer with new configuration */
333 put_unaligned_le32(new_baudrate, feature_buffer);
334 feature_buffer[4] |= data_bits; /* assign data bits in 2 bit space ( max 3 ) */
336 feature_buffer[4] |= (stop_bits << 3); /* assign stop bits in 1 bit space */
337 feature_buffer[4] |= (parity_enable << 4); /* assign parity flag in 1 bit space */
338 feature_buffer[4] |= (parity_type << 5); /* assign parity type in 1 bit space */
340 feature_buffer[4] |= (reset << 7); /* assign reset at end of byte, 1 bit space */
342 dbg("%s - device is being sent this feature report:",
344 dbg("%s - %02X - %02X - %02X - %02X - %02X", __func__,
345 feature_buffer[0], feature_buffer[1],
346 feature_buffer[2], feature_buffer[3],
350 retval = usb_control_msg(port->serial->dev,
351 usb_sndctrlpipe(port->serial->dev, 0),
353 USB_DIR_OUT | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
354 0x0300, 0, feature_buffer,
360 } while (retval != feature_len &&
363 if (retval != feature_len) {
364 dev_err(&port->dev, "%s - failed sending serial "
365 "line settings - %d\n", __func__, retval);
366 cypress_set_dead(port);
368 spin_lock_irqsave(&priv->lock, flags);
369 priv->baud_rate = new_baudrate;
370 priv->current_config = feature_buffer[4];
371 spin_unlock_irqrestore(&priv->lock, flags);
372 /* If we asked for a speed change encode it */
374 tty_encode_baud_rate(tty,
375 new_baudrate, new_baudrate);
378 case CYPRESS_GET_CONFIG:
379 if (priv->get_cfg_unsafe) {
380 /* Not implemented for this device,
381 and if we try to do it we're likely
382 to crash the hardware. */
386 dbg("%s - retreiving serial line settings", __func__);
388 retval = usb_control_msg(port->serial->dev,
389 usb_rcvctrlpipe(port->serial->dev, 0),
391 USB_DIR_IN | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
392 0x0300, 0, feature_buffer,
397 } while (retval != feature_len
398 && retval != -ENODEV);
400 if (retval != feature_len) {
401 dev_err(&port->dev, "%s - failed to retrieve serial "
402 "line settings - %d\n", __func__, retval);
403 cypress_set_dead(port);
406 spin_lock_irqsave(&priv->lock, flags);
407 /* store the config in one byte, and later
408 use bit masks to check values */
409 priv->current_config = feature_buffer[4];
410 priv->baud_rate = get_unaligned_le32(feature_buffer);
411 spin_unlock_irqrestore(&priv->lock, flags);
414 spin_lock_irqsave(&priv->lock, flags);
416 spin_unlock_irqrestore(&priv->lock, flags);
418 kfree(feature_buffer);
420 } /* cypress_serial_control */
423 static void cypress_set_dead(struct usb_serial_port *port)
425 struct cypress_private *priv = usb_get_serial_port_data(port);
428 spin_lock_irqsave(&priv->lock, flags);
429 if (!priv->comm_is_ok) {
430 spin_unlock_irqrestore(&priv->lock, flags);
433 priv->comm_is_ok = 0;
434 spin_unlock_irqrestore(&priv->lock, flags);
436 dev_err(&port->dev, "cypress_m8 suspending failing port %d - "
437 "interval might be too short\n", port->number);
441 /*****************************************************************************
442 * Cypress serial driver functions
443 *****************************************************************************/
446 static int generic_startup(struct usb_serial *serial)
448 struct cypress_private *priv;
449 struct usb_serial_port *port = serial->port[0];
451 priv = kzalloc(sizeof(struct cypress_private), GFP_KERNEL);
455 priv->comm_is_ok = !0;
456 spin_lock_init(&priv->lock);
457 if (kfifo_alloc(&priv->write_fifo, CYPRESS_BUF_SIZE, GFP_KERNEL)) {
461 init_waitqueue_head(&priv->delta_msr_wait);
463 usb_reset_configuration(serial->dev);
466 priv->line_control = 0;
467 priv->termios_initialized = 0;
469 /* Default packet format setting is determined by packet size.
470 Anything with a size larger then 9 must have a separate
471 count field since the 3 bit count field is otherwise too
472 small. Otherwise we can use the slightly more compact
473 format. This is in accordance with the cypress_m8 serial
474 converter app note. */
475 if (port->interrupt_out_size > 9)
476 priv->pkt_fmt = packet_format_1;
478 priv->pkt_fmt = packet_format_2;
481 priv->write_urb_interval = interval;
482 priv->read_urb_interval = interval;
483 dbg("%s - port %d read & write intervals forced to %d",
484 __func__, port->number, interval);
486 priv->write_urb_interval = port->interrupt_out_urb->interval;
487 priv->read_urb_interval = port->interrupt_in_urb->interval;
488 dbg("%s - port %d intervals: read=%d write=%d",
489 __func__, port->number,
490 priv->read_urb_interval, priv->write_urb_interval);
492 usb_set_serial_port_data(port, priv);
498 static int cypress_earthmate_startup(struct usb_serial *serial)
500 struct cypress_private *priv;
501 struct usb_serial_port *port = serial->port[0];
503 if (generic_startup(serial)) {
504 dbg("%s - Failed setting up port %d", __func__,
509 priv = usb_get_serial_port_data(port);
510 priv->chiptype = CT_EARTHMATE;
511 /* All Earthmate devices use the separated-count packet
513 priv->pkt_fmt = packet_format_1;
514 if (serial->dev->descriptor.idProduct !=
515 cpu_to_le16(PRODUCT_ID_EARTHMATEUSB)) {
516 /* The old original USB Earthmate seemed able to
517 handle GET_CONFIG requests; everything they've
518 produced since that time crashes if this command is
520 dbg("%s - Marking this device as unsafe for GET_CONFIG "
521 "commands", __func__);
522 priv->get_cfg_unsafe = !0;
526 } /* cypress_earthmate_startup */
529 static int cypress_hidcom_startup(struct usb_serial *serial)
531 struct cypress_private *priv;
533 if (generic_startup(serial)) {
534 dbg("%s - Failed setting up port %d", __func__,
535 serial->port[0]->number);
539 priv = usb_get_serial_port_data(serial->port[0]);
540 priv->chiptype = CT_CYPHIDCOM;
543 } /* cypress_hidcom_startup */
546 static int cypress_ca42v2_startup(struct usb_serial *serial)
548 struct cypress_private *priv;
550 if (generic_startup(serial)) {
551 dbg("%s - Failed setting up port %d", __func__,
552 serial->port[0]->number);
556 priv = usb_get_serial_port_data(serial->port[0]);
557 priv->chiptype = CT_CA42V2;
560 } /* cypress_ca42v2_startup */
563 static void cypress_release(struct usb_serial *serial)
565 struct cypress_private *priv;
567 /* all open ports are closed at this point */
568 priv = usb_get_serial_port_data(serial->port[0]);
571 kfifo_free(&priv->write_fifo);
577 static int cypress_open(struct tty_struct *tty, struct usb_serial_port *port)
579 struct cypress_private *priv = usb_get_serial_port_data(port);
580 struct usb_serial *serial = port->serial;
584 if (!priv->comm_is_ok)
587 /* clear halts before open */
588 usb_clear_halt(serial->dev, 0x81);
589 usb_clear_halt(serial->dev, 0x02);
591 spin_lock_irqsave(&priv->lock, flags);
592 /* reset read/write statistics */
597 spin_unlock_irqrestore(&priv->lock, flags);
603 cypress_set_termios(tty, port, &priv->tmp_termios);
605 /* setup the port and start reading from the device */
606 if (!port->interrupt_in_urb) {
607 dev_err(&port->dev, "%s - interrupt_in_urb is empty!\n",
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);
625 port->port.drain_delay = 256;
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 /* writing is potentially harmful, lock must be taken */
649 mutex_lock(&port->serial->disc_mutex);
650 if (port->serial->disconnected) {
651 mutex_unlock(&port->serial->disc_mutex);
654 spin_lock_irqsave(&priv->lock, flags);
655 kfifo_reset_out(&priv->write_fifo);
656 spin_unlock_irqrestore(&priv->lock, flags);
658 dbg("%s - stopping urbs", __func__);
659 usb_kill_urb(port->interrupt_in_urb);
660 usb_kill_urb(port->interrupt_out_urb);
663 dev_info(&port->dev, "Statistics: %d Bytes In | %d Bytes Out | %d Commands Issued\n",
664 priv->bytes_in, priv->bytes_out, priv->cmd_count);
665 mutex_unlock(&port->serial->disc_mutex);
666 } /* cypress_close */
669 static int cypress_write(struct tty_struct *tty, struct usb_serial_port *port,
670 const unsigned char *buf, int count)
672 struct cypress_private *priv = usb_get_serial_port_data(port);
674 dbg("%s - port %d, %d bytes", __func__, port->number, count);
676 /* line control commands, which need to be executed immediately,
677 are not put into the buffer for obvious reasons.
679 if (priv->cmd_ctrl) {
687 count = kfifo_in_locked(&priv->write_fifo, buf, count, &priv->lock);
693 } /* cypress_write */
696 static void cypress_send(struct usb_serial_port *port)
698 int count = 0, result, offset, actual_size;
699 struct cypress_private *priv = usb_get_serial_port_data(port);
702 if (!priv->comm_is_ok)
705 dbg("%s - interrupt out size is %d", __func__,
706 port->interrupt_out_size);
708 spin_lock_irqsave(&priv->lock, flags);
709 if (priv->write_urb_in_use) {
710 dbg("%s - can't write, urb in use", __func__);
711 spin_unlock_irqrestore(&priv->lock, flags);
714 spin_unlock_irqrestore(&priv->lock, flags);
717 memset(port->interrupt_out_urb->transfer_buffer, 0,
718 port->interrupt_out_size);
720 spin_lock_irqsave(&priv->lock, flags);
721 switch (priv->pkt_fmt) {
723 case packet_format_1:
724 /* this is for the CY7C64013... */
726 port->interrupt_out_buffer[0] = priv->line_control;
728 case packet_format_2:
729 /* this is for the CY7C63743... */
731 port->interrupt_out_buffer[0] = priv->line_control;
735 if (priv->line_control & CONTROL_RESET)
736 priv->line_control &= ~CONTROL_RESET;
738 if (priv->cmd_ctrl) {
740 dbg("%s - line control command being issued", __func__);
741 spin_unlock_irqrestore(&priv->lock, flags);
744 spin_unlock_irqrestore(&priv->lock, flags);
746 count = kfifo_out_locked(&priv->write_fifo,
747 &port->interrupt_out_buffer[offset],
748 port->interrupt_out_size - offset,
753 switch (priv->pkt_fmt) {
755 case packet_format_1:
756 port->interrupt_out_buffer[1] = count;
758 case packet_format_2:
759 port->interrupt_out_buffer[0] |= count;
762 dbg("%s - count is %d", __func__, count);
765 spin_lock_irqsave(&priv->lock, flags);
766 priv->write_urb_in_use = 1;
767 spin_unlock_irqrestore(&priv->lock, flags);
772 actual_size = count +
773 (priv->pkt_fmt == packet_format_1 ? 2 : 1);
775 usb_serial_debug_data(debug, &port->dev, __func__,
776 port->interrupt_out_size,
777 port->interrupt_out_urb->transfer_buffer);
779 usb_fill_int_urb(port->interrupt_out_urb, port->serial->dev,
780 usb_sndintpipe(port->serial->dev, port->interrupt_out_endpointAddress),
781 port->interrupt_out_buffer, port->interrupt_out_size,
782 cypress_write_int_callback, port, priv->write_urb_interval);
783 result = usb_submit_urb(port->interrupt_out_urb, GFP_ATOMIC);
785 dev_err_console(port,
786 "%s - failed submitting write urb, error %d\n",
788 priv->write_urb_in_use = 0;
789 cypress_set_dead(port);
792 spin_lock_irqsave(&priv->lock, flags);
796 /* do not count the line control and size bytes */
797 priv->bytes_out += count;
798 spin_unlock_irqrestore(&priv->lock, flags);
800 usb_serial_port_softint(port);
804 /* returns how much space is available in the soft buffer */
805 static int cypress_write_room(struct tty_struct *tty)
807 struct usb_serial_port *port = tty->driver_data;
808 struct cypress_private *priv = usb_get_serial_port_data(port);
812 spin_lock_irqsave(&priv->lock, flags);
813 room = kfifo_avail(&priv->write_fifo);
814 spin_unlock_irqrestore(&priv->lock, flags);
816 dbg("%s - returns %d", __func__, room);
821 static int cypress_tiocmget(struct tty_struct *tty)
823 struct usb_serial_port *port = tty->driver_data;
824 struct cypress_private *priv = usb_get_serial_port_data(port);
825 __u8 status, control;
826 unsigned int result = 0;
829 spin_lock_irqsave(&priv->lock, flags);
830 control = priv->line_control;
831 status = priv->current_status;
832 spin_unlock_irqrestore(&priv->lock, flags);
834 result = ((control & CONTROL_DTR) ? TIOCM_DTR : 0)
835 | ((control & CONTROL_RTS) ? TIOCM_RTS : 0)
836 | ((status & UART_CTS) ? TIOCM_CTS : 0)
837 | ((status & UART_DSR) ? TIOCM_DSR : 0)
838 | ((status & UART_RI) ? TIOCM_RI : 0)
839 | ((status & UART_CD) ? TIOCM_CD : 0);
841 dbg("%s - result = %x", __func__, result);
847 static int cypress_tiocmset(struct tty_struct *tty,
848 unsigned int set, unsigned int clear)
850 struct usb_serial_port *port = tty->driver_data;
851 struct cypress_private *priv = usb_get_serial_port_data(port);
854 spin_lock_irqsave(&priv->lock, flags);
856 priv->line_control |= CONTROL_RTS;
858 priv->line_control |= CONTROL_DTR;
859 if (clear & TIOCM_RTS)
860 priv->line_control &= ~CONTROL_RTS;
861 if (clear & TIOCM_DTR)
862 priv->line_control &= ~CONTROL_DTR;
864 spin_unlock_irqrestore(&priv->lock, flags);
866 return cypress_write(tty, port, NULL, 0);
870 static int cypress_ioctl(struct tty_struct *tty,
871 unsigned int cmd, unsigned long arg)
873 struct usb_serial_port *port = tty->driver_data;
874 struct cypress_private *priv = usb_get_serial_port_data(port);
876 dbg("%s - port %d, cmd 0x%.4x", __func__, port->number, cmd);
879 /* This code comes from drivers/char/serial.c and ftdi_sio.c */
881 while (priv != NULL) {
882 interruptible_sleep_on(&priv->delta_msr_wait);
883 /* see if a signal did it */
884 if (signal_pending(current))
887 char diff = priv->diff_status;
889 return -EIO; /* no change => error */
891 /* consume all events */
892 priv->diff_status = 0;
894 /* return 0 if caller wanted to know about
896 if (((arg & TIOCM_RNG) && (diff & UART_RI)) ||
897 ((arg & TIOCM_DSR) && (diff & UART_DSR)) ||
898 ((arg & TIOCM_CD) && (diff & UART_CD)) ||
899 ((arg & TIOCM_CTS) && (diff & UART_CTS)))
901 /* otherwise caller can't care less about what
902 * happened, and so we continue to wait for
911 dbg("%s - arg not supported - it was 0x%04x - check include/asm/ioctls.h", __func__, cmd);
913 } /* cypress_ioctl */
916 static void cypress_set_termios(struct tty_struct *tty,
917 struct usb_serial_port *port, struct ktermios *old_termios)
919 struct cypress_private *priv = usb_get_serial_port_data(port);
920 int data_bits, stop_bits, parity_type, parity_enable;
921 unsigned cflag, iflag;
926 spin_lock_irqsave(&priv->lock, flags);
927 /* We can't clean this one up as we don't know the device type
929 if (!priv->termios_initialized) {
930 if (priv->chiptype == CT_EARTHMATE) {
931 *(tty->termios) = tty_std_termios;
932 tty->termios->c_cflag = B4800 | CS8 | CREAD | HUPCL |
934 tty->termios->c_ispeed = 4800;
935 tty->termios->c_ospeed = 4800;
936 } else if (priv->chiptype == CT_CYPHIDCOM) {
937 *(tty->termios) = tty_std_termios;
938 tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
940 tty->termios->c_ispeed = 9600;
941 tty->termios->c_ospeed = 9600;
942 } else if (priv->chiptype == CT_CA42V2) {
943 *(tty->termios) = tty_std_termios;
944 tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
946 tty->termios->c_ispeed = 9600;
947 tty->termios->c_ospeed = 9600;
949 priv->termios_initialized = 1;
951 spin_unlock_irqrestore(&priv->lock, flags);
953 /* Unsupported features need clearing */
954 tty->termios->c_cflag &= ~(CMSPAR|CRTSCTS);
956 cflag = tty->termios->c_cflag;
957 iflag = tty->termios->c_iflag;
959 /* check if there are new settings */
961 spin_lock_irqsave(&priv->lock, flags);
962 priv->tmp_termios = *(tty->termios);
963 spin_unlock_irqrestore(&priv->lock, flags);
966 /* set number of data bits, parity, stop bits */
967 /* when parity is disabled the parity type bit is ignored */
969 /* 1 means 2 stop bits, 0 means 1 stop bit */
970 stop_bits = cflag & CSTOPB ? 1 : 0;
972 if (cflag & PARENB) {
974 /* 1 means odd parity, 0 means even parity */
975 parity_type = cflag & PARODD ? 1 : 0;
977 parity_enable = parity_type = 0;
979 switch (cflag & CSIZE) {
993 dev_err(&port->dev, "%s - CSIZE was set, but not CS5-CS8\n",
997 spin_lock_irqsave(&priv->lock, flags);
998 oldlines = priv->line_control;
999 if ((cflag & CBAUD) == B0) {
1000 /* drop dtr and rts */
1001 dbg("%s - dropping the lines, baud rate 0bps", __func__);
1002 priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
1004 priv->line_control = (CONTROL_DTR | CONTROL_RTS);
1005 spin_unlock_irqrestore(&priv->lock, flags);
1007 dbg("%s - sending %d stop_bits, %d parity_enable, %d parity_type, "
1008 "%d data_bits (+5)", __func__, stop_bits,
1009 parity_enable, parity_type, data_bits);
1011 cypress_serial_control(tty, port, tty_get_baud_rate(tty),
1012 data_bits, stop_bits,
1013 parity_enable, parity_type,
1014 0, CYPRESS_SET_CONFIG);
1016 /* we perform a CYPRESS_GET_CONFIG so that the current settings are
1017 * filled into the private structure this should confirm that all is
1018 * working if it returns what we just set */
1019 cypress_serial_control(tty, port, 0, 0, 0, 0, 0, 0, CYPRESS_GET_CONFIG);
1021 /* Here we can define custom tty settings for devices; the main tty
1022 * termios flag base comes from empeg.c */
1024 spin_lock_irqsave(&priv->lock, flags);
1025 if (priv->chiptype == CT_EARTHMATE && priv->baud_rate == 4800) {
1026 dbg("Using custom termios settings for a baud rate of "
1028 /* define custom termios settings for NMEA protocol */
1030 tty->termios->c_iflag /* input modes - */
1031 &= ~(IGNBRK /* disable ignore break */
1032 | BRKINT /* disable break causes interrupt */
1033 | PARMRK /* disable mark parity errors */
1034 | ISTRIP /* disable clear high bit of input char */
1035 | INLCR /* disable translate NL to CR */
1036 | IGNCR /* disable ignore CR */
1037 | ICRNL /* disable translate CR to NL */
1038 | IXON); /* disable enable XON/XOFF flow control */
1040 tty->termios->c_oflag /* output modes */
1041 &= ~OPOST; /* disable postprocess output char */
1043 tty->termios->c_lflag /* line discipline modes */
1044 &= ~(ECHO /* disable echo input characters */
1045 | ECHONL /* disable echo new line */
1046 | ICANON /* disable erase, kill, werase, and rprnt
1047 special characters */
1048 | ISIG /* disable interrupt, quit, and suspend
1049 special characters */
1050 | IEXTEN); /* disable non-POSIX special characters */
1051 } /* CT_CYPHIDCOM: Application should handle this for device */
1053 linechange = (priv->line_control != oldlines);
1054 spin_unlock_irqrestore(&priv->lock, flags);
1056 /* if necessary, set lines */
1059 cypress_write(tty, port, NULL, 0);
1061 } /* cypress_set_termios */
1064 /* returns amount of data still left in soft buffer */
1065 static int cypress_chars_in_buffer(struct tty_struct *tty)
1067 struct usb_serial_port *port = tty->driver_data;
1068 struct cypress_private *priv = usb_get_serial_port_data(port);
1070 unsigned long flags;
1072 spin_lock_irqsave(&priv->lock, flags);
1073 chars = kfifo_len(&priv->write_fifo);
1074 spin_unlock_irqrestore(&priv->lock, flags);
1076 dbg("%s - returns %d", __func__, chars);
1081 static void cypress_throttle(struct tty_struct *tty)
1083 struct usb_serial_port *port = tty->driver_data;
1084 struct cypress_private *priv = usb_get_serial_port_data(port);
1086 spin_lock_irq(&priv->lock);
1087 priv->rx_flags = THROTTLED;
1088 spin_unlock_irq(&priv->lock);
1092 static void cypress_unthrottle(struct tty_struct *tty)
1094 struct usb_serial_port *port = tty->driver_data;
1095 struct cypress_private *priv = usb_get_serial_port_data(port);
1096 int actually_throttled, result;
1098 spin_lock_irq(&priv->lock);
1099 actually_throttled = priv->rx_flags & ACTUALLY_THROTTLED;
1101 spin_unlock_irq(&priv->lock);
1103 if (!priv->comm_is_ok)
1106 if (actually_throttled) {
1107 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
1109 dev_err(&port->dev, "%s - failed submitting read urb, "
1110 "error %d\n", __func__, result);
1111 cypress_set_dead(port);
1117 static void cypress_read_int_callback(struct urb *urb)
1119 struct usb_serial_port *port = urb->context;
1120 struct cypress_private *priv = usb_get_serial_port_data(port);
1121 struct tty_struct *tty;
1122 unsigned char *data = urb->transfer_buffer;
1123 unsigned long flags;
1124 char tty_flag = TTY_NORMAL;
1129 int status = urb->status;
1132 case 0: /* success */
1137 /* precursor to disconnect so just go away */
1140 /* Can't call usb_clear_halt while in_interrupt */
1143 /* something ugly is going on... */
1144 dev_err(&urb->dev->dev,
1145 "%s - unexpected nonzero read status received: %d\n",
1147 cypress_set_dead(port);
1151 spin_lock_irqsave(&priv->lock, flags);
1152 if (priv->rx_flags & THROTTLED) {
1153 dbg("%s - now throttling", __func__);
1154 priv->rx_flags |= ACTUALLY_THROTTLED;
1155 spin_unlock_irqrestore(&priv->lock, flags);
1158 spin_unlock_irqrestore(&priv->lock, flags);
1160 tty = tty_port_tty_get(&port->port);
1162 dbg("%s - bad tty pointer - exiting", __func__);
1166 spin_lock_irqsave(&priv->lock, flags);
1167 result = urb->actual_length;
1168 switch (priv->pkt_fmt) {
1170 case packet_format_1:
1171 /* This is for the CY7C64013... */
1172 priv->current_status = data[0] & 0xF8;
1173 bytes = data[1] + 2;
1178 case packet_format_2:
1179 /* This is for the CY7C63743... */
1180 priv->current_status = data[0] & 0xF8;
1181 bytes = (data[0] & 0x07) + 1;
1187 spin_unlock_irqrestore(&priv->lock, flags);
1188 if (result < bytes) {
1189 dbg("%s - wrong packet size - received %d bytes but packet "
1190 "said %d bytes", __func__, result, bytes);
1194 usb_serial_debug_data(debug, &port->dev, __func__,
1195 urb->actual_length, data);
1197 spin_lock_irqsave(&priv->lock, flags);
1198 /* check to see if status has changed */
1199 if (priv->current_status != priv->prev_status) {
1200 priv->diff_status |= priv->current_status ^
1202 wake_up_interruptible(&priv->delta_msr_wait);
1203 priv->prev_status = priv->current_status;
1205 spin_unlock_irqrestore(&priv->lock, flags);
1207 /* hangup, as defined in acm.c... this might be a bad place for it
1209 if (tty && !(tty->termios->c_cflag & CLOCAL) &&
1210 !(priv->current_status & UART_CD)) {
1211 dbg("%s - calling hangup", __func__);
1216 /* There is one error bit... I'm assuming it is a parity error
1217 * indicator as the generic firmware will set this bit to 1 if a
1218 * parity error occurs.
1219 * I can not find reference to any other error events. */
1220 spin_lock_irqsave(&priv->lock, flags);
1221 if (priv->current_status & CYP_ERROR) {
1222 spin_unlock_irqrestore(&priv->lock, flags);
1223 tty_flag = TTY_PARITY;
1224 dbg("%s - Parity Error detected", __func__);
1226 spin_unlock_irqrestore(&priv->lock, flags);
1228 /* process read if there is data other than line status */
1229 if (tty && bytes > i) {
1230 tty_insert_flip_string_fixed_flag(tty, data + i,
1231 tty_flag, bytes - i);
1232 tty_flip_buffer_push(tty);
1235 spin_lock_irqsave(&priv->lock, flags);
1236 /* control and status byte(s) are also counted */
1237 priv->bytes_in += bytes;
1238 spin_unlock_irqrestore(&priv->lock, flags);
1243 /* Continue trying to always read */
1245 if (priv->comm_is_ok) {
1246 usb_fill_int_urb(port->interrupt_in_urb, port->serial->dev,
1247 usb_rcvintpipe(port->serial->dev,
1248 port->interrupt_in_endpointAddress),
1249 port->interrupt_in_urb->transfer_buffer,
1250 port->interrupt_in_urb->transfer_buffer_length,
1251 cypress_read_int_callback, port,
1252 priv->read_urb_interval);
1253 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
1254 if (result && result != -EPERM) {
1255 dev_err(&urb->dev->dev, "%s - failed resubmitting "
1256 "read urb, error %d\n", __func__,
1258 cypress_set_dead(port);
1261 } /* cypress_read_int_callback */
1264 static void cypress_write_int_callback(struct urb *urb)
1266 struct usb_serial_port *port = urb->context;
1267 struct cypress_private *priv = usb_get_serial_port_data(port);
1269 int status = urb->status;
1278 /* this urb is terminated, clean up */
1279 dbg("%s - urb shutting down with status: %d",
1281 priv->write_urb_in_use = 0;
1283 case -EPIPE: /* no break needed; clear halt and resubmit */
1284 if (!priv->comm_is_ok)
1286 usb_clear_halt(port->serial->dev, 0x02);
1287 /* error in the urb, so we have to resubmit it */
1288 dbg("%s - nonzero write bulk status received: %d",
1290 port->interrupt_out_urb->transfer_buffer_length = 1;
1291 result = usb_submit_urb(port->interrupt_out_urb, GFP_ATOMIC);
1294 dev_err(&urb->dev->dev,
1295 "%s - failed resubmitting write urb, error %d\n",
1297 cypress_set_dead(port);
1300 dev_err(&urb->dev->dev,
1301 "%s - unexpected nonzero write status received: %d\n",
1303 cypress_set_dead(port);
1306 priv->write_urb_in_use = 0;
1308 /* send any buffered data */
1312 module_usb_serial_driver(cypress_driver, serial_drivers);
1314 MODULE_AUTHOR(DRIVER_AUTHOR);
1315 MODULE_DESCRIPTION(DRIVER_DESC);
1316 MODULE_VERSION(DRIVER_VERSION);
1317 MODULE_LICENSE("GPL");
1319 module_param(debug, bool, S_IRUGO | S_IWUSR);
1320 MODULE_PARM_DESC(debug, "Debug enabled or not");
1321 module_param(stats, bool, S_IRUGO | S_IWUSR);
1322 MODULE_PARM_DESC(stats, "Enable statistics or not");
1323 module_param(interval, int, S_IRUGO | S_IWUSR);
1324 MODULE_PARM_DESC(interval, "Overrides interrupt interval");
1325 module_param(unstable_bauds, bool, S_IRUGO | S_IWUSR);
1326 MODULE_PARM_DESC(unstable_bauds, "Allow unstable baud rates");