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 improvments
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/delay.h>
68 #include <linux/uaccess.h>
70 #include "cypress_m8.h"
80 #define DRIVER_VERSION "v1.09"
81 #define DRIVER_AUTHOR "Lonnie Mendez <dignome@gmail.com>, Neil Whelchel <koyama@firstlight.net>"
82 #define DRIVER_DESC "Cypress USB to Serial Driver"
84 /* write buffer size defines */
85 #define CYPRESS_BUF_SIZE 1024
86 #define CYPRESS_CLOSING_WAIT (30*HZ)
88 static struct usb_device_id id_table_earthmate [] = {
89 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB) },
90 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB_LT20) },
91 { } /* Terminating entry */
94 static struct usb_device_id id_table_cyphidcomrs232 [] = {
95 { USB_DEVICE(VENDOR_ID_CYPRESS, PRODUCT_ID_CYPHIDCOM) },
96 { USB_DEVICE(VENDOR_ID_POWERCOM, PRODUCT_ID_UPS) },
97 { } /* Terminating entry */
100 static struct usb_device_id id_table_nokiaca42v2 [] = {
101 { USB_DEVICE(VENDOR_ID_DAZZLE, PRODUCT_ID_CA42) },
102 { } /* Terminating entry */
105 static struct usb_device_id id_table_combined [] = {
106 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB) },
107 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB_LT20) },
108 { USB_DEVICE(VENDOR_ID_CYPRESS, PRODUCT_ID_CYPHIDCOM) },
109 { USB_DEVICE(VENDOR_ID_POWERCOM, PRODUCT_ID_UPS) },
110 { USB_DEVICE(VENDOR_ID_DAZZLE, PRODUCT_ID_CA42) },
111 { } /* Terminating entry */
114 MODULE_DEVICE_TABLE(usb, id_table_combined);
116 static struct usb_driver cypress_driver = {
118 .probe = usb_serial_probe,
119 .disconnect = usb_serial_disconnect,
120 .id_table = id_table_combined,
125 packet_format_1, /* b0:status, b1:payload count */
126 packet_format_2 /* b0[7:3]:status, b0[2:0]:payload count */
129 struct cypress_private {
130 spinlock_t lock; /* private lock */
131 int chiptype; /* identifier of device, for quirks/etc */
132 int bytes_in; /* used for statistics */
133 int bytes_out; /* used for statistics */
134 int cmd_count; /* used for statistics */
135 int cmd_ctrl; /* always set this to 1 before issuing a command */
136 struct cypress_buf *buf; /* write buffer */
137 int write_urb_in_use; /* write urb in use indicator */
138 int write_urb_interval; /* interval to use for write urb */
139 int read_urb_interval; /* interval to use for read urb */
140 int comm_is_ok; /* true if communication is (still) ok */
141 int termios_initialized;
142 __u8 line_control; /* holds dtr / rts value */
143 __u8 current_status; /* received from last read - info on dsr,cts,cd,ri,etc */
144 __u8 current_config; /* stores the current configuration byte */
145 __u8 rx_flags; /* throttling - used from whiteheat/ftdi_sio */
146 enum packet_format pkt_fmt; /* format to use for packet send / receive */
147 int get_cfg_unsafe; /* If true, the CYPRESS_GET_CONFIG is unsafe */
148 int baud_rate; /* stores current baud rate in
150 int isthrottled; /* if throttled, discard reads */
151 wait_queue_head_t delta_msr_wait; /* used for TIOCMIWAIT */
152 char prev_status, diff_status; /* used for TIOCMIWAIT */
153 /* we pass a pointer to this as the arguement sent to
154 cypress_set_termios old_termios */
155 struct ktermios tmp_termios; /* stores the old termios settings */
158 /* write buffer structure */
160 unsigned int buf_size;
166 /* function prototypes for the Cypress USB to serial device */
167 static int cypress_earthmate_startup(struct usb_serial *serial);
168 static int cypress_hidcom_startup(struct usb_serial *serial);
169 static int cypress_ca42v2_startup(struct usb_serial *serial);
170 static void cypress_release(struct usb_serial *serial);
171 static int cypress_open(struct tty_struct *tty, struct usb_serial_port *port);
172 static void cypress_close(struct usb_serial_port *port);
173 static void cypress_dtr_rts(struct usb_serial_port *port, int on);
174 static int cypress_write(struct tty_struct *tty, struct usb_serial_port *port,
175 const unsigned char *buf, int count);
176 static void cypress_send(struct usb_serial_port *port);
177 static int cypress_write_room(struct tty_struct *tty);
178 static int cypress_ioctl(struct tty_struct *tty, struct file *file,
179 unsigned int cmd, unsigned long arg);
180 static void cypress_set_termios(struct tty_struct *tty,
181 struct usb_serial_port *port, struct ktermios *old);
182 static int cypress_tiocmget(struct tty_struct *tty, struct file *file);
183 static int cypress_tiocmset(struct tty_struct *tty, struct file *file,
184 unsigned int set, unsigned int clear);
185 static int cypress_chars_in_buffer(struct tty_struct *tty);
186 static void cypress_throttle(struct tty_struct *tty);
187 static void cypress_unthrottle(struct tty_struct *tty);
188 static void cypress_set_dead(struct usb_serial_port *port);
189 static void cypress_read_int_callback(struct urb *urb);
190 static void cypress_write_int_callback(struct urb *urb);
191 /* write buffer functions */
192 static struct cypress_buf *cypress_buf_alloc(unsigned int size);
193 static void cypress_buf_free(struct cypress_buf *cb);
194 static void cypress_buf_clear(struct cypress_buf *cb);
195 static unsigned int cypress_buf_data_avail(struct cypress_buf *cb);
196 static unsigned int cypress_buf_space_avail(struct cypress_buf *cb);
197 static unsigned int cypress_buf_put(struct cypress_buf *cb,
198 const char *buf, unsigned int count);
199 static unsigned int cypress_buf_get(struct cypress_buf *cb,
200 char *buf, unsigned int count);
203 static struct usb_serial_driver cypress_earthmate_device = {
205 .owner = THIS_MODULE,
208 .description = "DeLorme Earthmate USB",
209 .usb_driver = &cypress_driver,
210 .id_table = id_table_earthmate,
212 .attach = cypress_earthmate_startup,
213 .release = cypress_release,
214 .open = cypress_open,
215 .close = cypress_close,
216 .dtr_rts = cypress_dtr_rts,
217 .write = cypress_write,
218 .write_room = cypress_write_room,
219 .ioctl = cypress_ioctl,
220 .set_termios = cypress_set_termios,
221 .tiocmget = cypress_tiocmget,
222 .tiocmset = cypress_tiocmset,
223 .chars_in_buffer = cypress_chars_in_buffer,
224 .throttle = cypress_throttle,
225 .unthrottle = cypress_unthrottle,
226 .read_int_callback = cypress_read_int_callback,
227 .write_int_callback = cypress_write_int_callback,
230 static struct usb_serial_driver cypress_hidcom_device = {
232 .owner = THIS_MODULE,
235 .description = "HID->COM RS232 Adapter",
236 .usb_driver = &cypress_driver,
237 .id_table = id_table_cyphidcomrs232,
239 .attach = cypress_hidcom_startup,
240 .release = cypress_release,
241 .open = cypress_open,
242 .close = cypress_close,
243 .dtr_rts = cypress_dtr_rts,
244 .write = cypress_write,
245 .write_room = cypress_write_room,
246 .ioctl = cypress_ioctl,
247 .set_termios = cypress_set_termios,
248 .tiocmget = cypress_tiocmget,
249 .tiocmset = cypress_tiocmset,
250 .chars_in_buffer = cypress_chars_in_buffer,
251 .throttle = cypress_throttle,
252 .unthrottle = cypress_unthrottle,
253 .read_int_callback = cypress_read_int_callback,
254 .write_int_callback = cypress_write_int_callback,
257 static struct usb_serial_driver cypress_ca42v2_device = {
259 .owner = THIS_MODULE,
260 .name = "nokiaca42v2",
262 .description = "Nokia CA-42 V2 Adapter",
263 .usb_driver = &cypress_driver,
264 .id_table = id_table_nokiaca42v2,
266 .attach = cypress_ca42v2_startup,
267 .release = cypress_release,
268 .open = cypress_open,
269 .close = cypress_close,
270 .dtr_rts = cypress_dtr_rts,
271 .write = cypress_write,
272 .write_room = cypress_write_room,
273 .ioctl = cypress_ioctl,
274 .set_termios = cypress_set_termios,
275 .tiocmget = cypress_tiocmget,
276 .tiocmset = cypress_tiocmset,
277 .chars_in_buffer = cypress_chars_in_buffer,
278 .throttle = cypress_throttle,
279 .unthrottle = cypress_unthrottle,
280 .read_int_callback = cypress_read_int_callback,
281 .write_int_callback = cypress_write_int_callback,
284 /*****************************************************************************
285 * Cypress serial helper functions
286 *****************************************************************************/
289 static int analyze_baud_rate(struct usb_serial_port *port, speed_t new_rate)
291 struct cypress_private *priv;
292 priv = usb_get_serial_port_data(port);
295 * The general purpose firmware for the Cypress M8 allows for
296 * a maximum speed of 57600bps (I have no idea whether DeLorme
297 * chose to use the general purpose firmware or not), if you
298 * need to modify this speed setting for your own project
299 * please add your own chiptype and modify the code likewise.
300 * The Cypress HID->COM device will work successfully up to
301 * 115200bps (but the actual throughput is around 3kBps).
303 if (port->serial->dev->speed == USB_SPEED_LOW) {
305 * Mike Isely <isely@pobox.com> 2-Feb-2008: The
306 * Cypress app note that describes this mechanism
307 * states the the low-speed part can't handle more
308 * than 800 bytes/sec, in which case 4800 baud is the
309 * safest speed for a part like that.
311 if (new_rate > 4800) {
312 dbg("%s - failed setting baud rate, device incapable "
313 "speed %d", __func__, new_rate);
317 switch (priv->chiptype) {
319 if (new_rate <= 600) {
320 /* 300 and 600 baud rates are supported under
321 * the generic firmware, but are not used with
322 * NMEA and SiRF protocols */
323 dbg("%s - failed setting baud rate, unsupported speed "
324 "of %d on Earthmate GPS", __func__, new_rate);
335 /* This function can either set or retrieve the current serial line settings */
336 static int cypress_serial_control(struct tty_struct *tty,
337 struct usb_serial_port *port, speed_t baud_rate, int data_bits,
338 int stop_bits, int parity_enable, int parity_type, int reset,
339 int cypress_request_type)
341 int new_baudrate = 0, retval = 0, tries = 0;
342 struct cypress_private *priv;
343 __u8 feature_buffer[5];
348 priv = usb_get_serial_port_data(port);
350 if (!priv->comm_is_ok)
353 switch (cypress_request_type) {
354 case CYPRESS_SET_CONFIG:
355 /* 0 means 'Hang up' so doesn't change the true bit rate */
356 new_baudrate = priv->baud_rate;
357 if (baud_rate && baud_rate != priv->baud_rate) {
358 dbg("%s - baud rate is changing", __func__);
359 retval = analyze_baud_rate(port, baud_rate);
361 new_baudrate = retval;
362 dbg("%s - New baud rate set to %d",
363 __func__, new_baudrate);
366 dbg("%s - baud rate is being sent as %d",
367 __func__, new_baudrate);
369 memset(feature_buffer, 0, sizeof(feature_buffer));
370 /* fill the feature_buffer with new configuration */
371 *((u_int32_t *)feature_buffer) = new_baudrate;
372 feature_buffer[4] |= data_bits; /* assign data bits in 2 bit space ( max 3 ) */
374 feature_buffer[4] |= (stop_bits << 3); /* assign stop bits in 1 bit space */
375 feature_buffer[4] |= (parity_enable << 4); /* assign parity flag in 1 bit space */
376 feature_buffer[4] |= (parity_type << 5); /* assign parity type in 1 bit space */
378 feature_buffer[4] |= (reset << 7); /* assign reset at end of byte, 1 bit space */
380 dbg("%s - device is being sent this feature report:",
382 dbg("%s - %02X - %02X - %02X - %02X - %02X", __func__,
383 feature_buffer[0], feature_buffer[1],
384 feature_buffer[2], feature_buffer[3],
388 retval = usb_control_msg(port->serial->dev,
389 usb_sndctrlpipe(port->serial->dev, 0),
391 USB_DIR_OUT | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
392 0x0300, 0, feature_buffer,
393 sizeof(feature_buffer), 500);
398 } while (retval != sizeof(feature_buffer) &&
401 if (retval != sizeof(feature_buffer)) {
402 dev_err(&port->dev, "%s - failed sending serial "
403 "line settings - %d\n", __func__, retval);
404 cypress_set_dead(port);
406 spin_lock_irqsave(&priv->lock, flags);
407 priv->baud_rate = new_baudrate;
408 priv->current_config = feature_buffer[4];
409 spin_unlock_irqrestore(&priv->lock, flags);
410 /* If we asked for a speed change encode it */
412 tty_encode_baud_rate(tty,
413 new_baudrate, new_baudrate);
416 case CYPRESS_GET_CONFIG:
417 if (priv->get_cfg_unsafe) {
418 /* Not implemented for this device,
419 and if we try to do it we're likely
420 to crash the hardware. */
423 dbg("%s - retreiving serial line settings", __func__);
424 /* set initial values in feature buffer */
425 memset(feature_buffer, 0, sizeof(feature_buffer));
428 retval = usb_control_msg(port->serial->dev,
429 usb_rcvctrlpipe(port->serial->dev, 0),
431 USB_DIR_IN | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
432 0x0300, 0, feature_buffer,
433 sizeof(feature_buffer), 500);
437 } while (retval != sizeof(feature_buffer)
438 && retval != -ENODEV);
440 if (retval != sizeof(feature_buffer)) {
441 dev_err(&port->dev, "%s - failed to retrieve serial "
442 "line settings - %d\n", __func__, retval);
443 cypress_set_dead(port);
446 spin_lock_irqsave(&priv->lock, flags);
447 /* store the config in one byte, and later
448 use bit masks to check values */
449 priv->current_config = feature_buffer[4];
450 priv->baud_rate = *((u_int32_t *)feature_buffer);
451 spin_unlock_irqrestore(&priv->lock, flags);
454 spin_lock_irqsave(&priv->lock, flags);
456 spin_unlock_irqrestore(&priv->lock, flags);
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 priv->buf = cypress_buf_alloc(CYPRESS_BUF_SIZE);
499 if (priv->buf == NULL) {
503 init_waitqueue_head(&priv->delta_msr_wait);
505 usb_reset_configuration(serial->dev);
508 priv->line_control = 0;
509 priv->termios_initialized = 0;
511 /* Default packet format setting is determined by packet size.
512 Anything with a size larger then 9 must have a separate
513 count field since the 3 bit count field is otherwise too
514 small. Otherwise we can use the slightly more compact
515 format. This is in accordance with the cypress_m8 serial
516 converter app note. */
517 if (port->interrupt_out_size > 9)
518 priv->pkt_fmt = packet_format_1;
520 priv->pkt_fmt = packet_format_2;
523 priv->write_urb_interval = interval;
524 priv->read_urb_interval = interval;
525 dbg("%s - port %d read & write intervals forced to %d",
526 __func__, port->number, interval);
528 priv->write_urb_interval = port->interrupt_out_urb->interval;
529 priv->read_urb_interval = port->interrupt_in_urb->interval;
530 dbg("%s - port %d intervals: read=%d write=%d",
531 __func__, port->number,
532 priv->read_urb_interval, priv->write_urb_interval);
534 usb_set_serial_port_data(port, priv);
540 static int cypress_earthmate_startup(struct usb_serial *serial)
542 struct cypress_private *priv;
543 struct usb_serial_port *port = serial->port[0];
547 if (generic_startup(serial)) {
548 dbg("%s - Failed setting up port %d", __func__,
553 priv = usb_get_serial_port_data(port);
554 priv->chiptype = CT_EARTHMATE;
555 /* All Earthmate devices use the separated-count packet
557 priv->pkt_fmt = packet_format_1;
558 if (serial->dev->descriptor.idProduct !=
559 cpu_to_le16(PRODUCT_ID_EARTHMATEUSB)) {
560 /* The old original USB Earthmate seemed able to
561 handle GET_CONFIG requests; everything they've
562 produced since that time crashes if this command is
564 dbg("%s - Marking this device as unsafe for GET_CONFIG "
565 "commands", __func__);
566 priv->get_cfg_unsafe = !0;
570 } /* cypress_earthmate_startup */
573 static int cypress_hidcom_startup(struct usb_serial *serial)
575 struct cypress_private *priv;
579 if (generic_startup(serial)) {
580 dbg("%s - Failed setting up port %d", __func__,
581 serial->port[0]->number);
585 priv = usb_get_serial_port_data(serial->port[0]);
586 priv->chiptype = CT_CYPHIDCOM;
589 } /* cypress_hidcom_startup */
592 static int cypress_ca42v2_startup(struct usb_serial *serial)
594 struct cypress_private *priv;
598 if (generic_startup(serial)) {
599 dbg("%s - Failed setting up port %d", __func__,
600 serial->port[0]->number);
604 priv = usb_get_serial_port_data(serial->port[0]);
605 priv->chiptype = CT_CA42V2;
608 } /* cypress_ca42v2_startup */
611 static void cypress_release(struct usb_serial *serial)
613 struct cypress_private *priv;
615 dbg("%s - port %d", __func__, serial->port[0]->number);
617 /* all open ports are closed at this point */
619 priv = usb_get_serial_port_data(serial->port[0]);
622 cypress_buf_free(priv->buf);
628 static int cypress_open(struct tty_struct *tty, struct usb_serial_port *port)
630 struct cypress_private *priv = usb_get_serial_port_data(port);
631 struct usb_serial *serial = port->serial;
635 dbg("%s - port %d", __func__, port->number);
637 if (!priv->comm_is_ok)
640 /* clear halts before open */
641 usb_clear_halt(serial->dev, 0x81);
642 usb_clear_halt(serial->dev, 0x02);
644 spin_lock_irqsave(&priv->lock, flags);
645 /* reset read/write statistics */
650 spin_unlock_irqrestore(&priv->lock, flags);
656 cypress_set_termios(tty, port, &priv->tmp_termios);
658 /* setup the port and start reading from the device */
659 if (!port->interrupt_in_urb) {
660 dev_err(&port->dev, "%s - interrupt_in_urb is empty!\n",
665 usb_fill_int_urb(port->interrupt_in_urb, serial->dev,
666 usb_rcvintpipe(serial->dev, port->interrupt_in_endpointAddress),
667 port->interrupt_in_urb->transfer_buffer,
668 port->interrupt_in_urb->transfer_buffer_length,
669 cypress_read_int_callback, port, priv->read_urb_interval);
670 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
674 "%s - failed submitting read urb, error %d\n",
676 cypress_set_dead(port);
678 port->port.drain_delay = 256;
682 static void cypress_dtr_rts(struct usb_serial_port *port, int on)
684 struct cypress_private *priv = usb_get_serial_port_data(port);
685 /* drop dtr and rts */
686 priv = usb_get_serial_port_data(port);
687 spin_lock_irq(&priv->lock);
689 priv->line_control = 0;
691 priv->line_control = CONTROL_DTR | CONTROL_RTS;
693 spin_unlock_irq(&priv->lock);
694 cypress_write(NULL, port, NULL, 0);
697 static void cypress_close(struct usb_serial_port *port)
699 struct cypress_private *priv = usb_get_serial_port_data(port);
701 dbg("%s - port %d", __func__, port->number);
703 /* writing is potentially harmful, lock must be taken */
704 mutex_lock(&port->serial->disc_mutex);
705 if (port->serial->disconnected) {
706 mutex_unlock(&port->serial->disc_mutex);
709 cypress_buf_clear(priv->buf);
710 dbg("%s - stopping urbs", __func__);
711 usb_kill_urb(port->interrupt_in_urb);
712 usb_kill_urb(port->interrupt_out_urb);
716 dev_info(&port->dev, "Statistics: %d Bytes In | %d Bytes Out | %d Commands Issued\n",
717 priv->bytes_in, priv->bytes_out, priv->cmd_count);
718 mutex_unlock(&port->serial->disc_mutex);
719 } /* cypress_close */
722 static int cypress_write(struct tty_struct *tty, struct usb_serial_port *port,
723 const unsigned char *buf, int count)
725 struct cypress_private *priv = usb_get_serial_port_data(port);
728 dbg("%s - port %d, %d bytes", __func__, port->number, count);
730 /* line control commands, which need to be executed immediately,
731 are not put into the buffer for obvious reasons.
733 if (priv->cmd_ctrl) {
741 spin_lock_irqsave(&priv->lock, flags);
742 count = cypress_buf_put(priv->buf, buf, count);
743 spin_unlock_irqrestore(&priv->lock, flags);
749 } /* cypress_write */
752 static void cypress_send(struct usb_serial_port *port)
754 int count = 0, result, offset, actual_size;
755 struct cypress_private *priv = usb_get_serial_port_data(port);
758 if (!priv->comm_is_ok)
761 dbg("%s - port %d", __func__, port->number);
762 dbg("%s - interrupt out size is %d", __func__,
763 port->interrupt_out_size);
765 spin_lock_irqsave(&priv->lock, flags);
766 if (priv->write_urb_in_use) {
767 dbg("%s - can't write, urb in use", __func__);
768 spin_unlock_irqrestore(&priv->lock, flags);
771 spin_unlock_irqrestore(&priv->lock, flags);
774 memset(port->interrupt_out_urb->transfer_buffer, 0,
775 port->interrupt_out_size);
777 spin_lock_irqsave(&priv->lock, flags);
778 switch (priv->pkt_fmt) {
780 case packet_format_1:
781 /* this is for the CY7C64013... */
783 port->interrupt_out_buffer[0] = priv->line_control;
785 case packet_format_2:
786 /* this is for the CY7C63743... */
788 port->interrupt_out_buffer[0] = priv->line_control;
792 if (priv->line_control & CONTROL_RESET)
793 priv->line_control &= ~CONTROL_RESET;
795 if (priv->cmd_ctrl) {
797 dbg("%s - line control command being issued", __func__);
798 spin_unlock_irqrestore(&priv->lock, flags);
801 spin_unlock_irqrestore(&priv->lock, flags);
803 count = cypress_buf_get(priv->buf, &port->interrupt_out_buffer[offset],
804 port->interrupt_out_size-offset);
809 switch (priv->pkt_fmt) {
811 case packet_format_1:
812 port->interrupt_out_buffer[1] = count;
814 case packet_format_2:
815 port->interrupt_out_buffer[0] |= count;
818 dbg("%s - count is %d", __func__, count);
821 spin_lock_irqsave(&priv->lock, flags);
822 priv->write_urb_in_use = 1;
823 spin_unlock_irqrestore(&priv->lock, flags);
828 actual_size = count +
829 (priv->pkt_fmt == packet_format_1 ? 2 : 1);
831 usb_serial_debug_data(debug, &port->dev, __func__,
832 port->interrupt_out_size,
833 port->interrupt_out_urb->transfer_buffer);
835 usb_fill_int_urb(port->interrupt_out_urb, port->serial->dev,
836 usb_sndintpipe(port->serial->dev, port->interrupt_out_endpointAddress),
837 port->interrupt_out_buffer, port->interrupt_out_size,
838 cypress_write_int_callback, port, priv->write_urb_interval);
839 result = usb_submit_urb(port->interrupt_out_urb, GFP_ATOMIC);
842 "%s - failed submitting write urb, error %d\n",
844 priv->write_urb_in_use = 0;
845 cypress_set_dead(port);
848 spin_lock_irqsave(&priv->lock, flags);
852 /* do not count the line control and size bytes */
853 priv->bytes_out += count;
854 spin_unlock_irqrestore(&priv->lock, flags);
856 usb_serial_port_softint(port);
860 /* returns how much space is available in the soft buffer */
861 static int cypress_write_room(struct tty_struct *tty)
863 struct usb_serial_port *port = tty->driver_data;
864 struct cypress_private *priv = usb_get_serial_port_data(port);
868 dbg("%s - port %d", __func__, port->number);
870 spin_lock_irqsave(&priv->lock, flags);
871 room = cypress_buf_space_avail(priv->buf);
872 spin_unlock_irqrestore(&priv->lock, flags);
874 dbg("%s - returns %d", __func__, room);
879 static int cypress_tiocmget(struct tty_struct *tty, struct file *file)
881 struct usb_serial_port *port = tty->driver_data;
882 struct cypress_private *priv = usb_get_serial_port_data(port);
883 __u8 status, control;
884 unsigned int result = 0;
887 dbg("%s - port %d", __func__, port->number);
889 spin_lock_irqsave(&priv->lock, flags);
890 control = priv->line_control;
891 status = priv->current_status;
892 spin_unlock_irqrestore(&priv->lock, flags);
894 result = ((control & CONTROL_DTR) ? TIOCM_DTR : 0)
895 | ((control & CONTROL_RTS) ? TIOCM_RTS : 0)
896 | ((status & UART_CTS) ? TIOCM_CTS : 0)
897 | ((status & UART_DSR) ? TIOCM_DSR : 0)
898 | ((status & UART_RI) ? TIOCM_RI : 0)
899 | ((status & UART_CD) ? TIOCM_CD : 0);
901 dbg("%s - result = %x", __func__, result);
907 static int cypress_tiocmset(struct tty_struct *tty, struct file *file,
908 unsigned int set, unsigned int clear)
910 struct usb_serial_port *port = tty->driver_data;
911 struct cypress_private *priv = usb_get_serial_port_data(port);
914 dbg("%s - port %d", __func__, port->number);
916 spin_lock_irqsave(&priv->lock, flags);
918 priv->line_control |= CONTROL_RTS;
920 priv->line_control |= CONTROL_DTR;
921 if (clear & TIOCM_RTS)
922 priv->line_control &= ~CONTROL_RTS;
923 if (clear & TIOCM_DTR)
924 priv->line_control &= ~CONTROL_DTR;
926 spin_unlock_irqrestore(&priv->lock, flags);
928 return cypress_write(tty, port, NULL, 0);
932 static int cypress_ioctl(struct tty_struct *tty, struct file *file,
933 unsigned int cmd, unsigned long arg)
935 struct usb_serial_port *port = tty->driver_data;
936 struct cypress_private *priv = usb_get_serial_port_data(port);
938 dbg("%s - port %d, cmd 0x%.4x", __func__, port->number, cmd);
941 /* This code comes from drivers/char/serial.c and ftdi_sio.c */
943 while (priv != NULL) {
944 interruptible_sleep_on(&priv->delta_msr_wait);
945 /* see if a signal did it */
946 if (signal_pending(current))
949 char diff = priv->diff_status;
951 return -EIO; /* no change => error */
953 /* consume all events */
954 priv->diff_status = 0;
956 /* return 0 if caller wanted to know about
958 if (((arg & TIOCM_RNG) && (diff & UART_RI)) ||
959 ((arg & TIOCM_DSR) && (diff & UART_DSR)) ||
960 ((arg & TIOCM_CD) && (diff & UART_CD)) ||
961 ((arg & TIOCM_CTS) && (diff & UART_CTS)))
963 /* otherwise caller can't care less about what
964 * happened, and so we continue to wait for
973 dbg("%s - arg not supported - it was 0x%04x - check include/asm/ioctls.h", __func__, cmd);
975 } /* cypress_ioctl */
978 static void cypress_set_termios(struct tty_struct *tty,
979 struct usb_serial_port *port, struct ktermios *old_termios)
981 struct cypress_private *priv = usb_get_serial_port_data(port);
982 int data_bits, stop_bits, parity_type, parity_enable;
983 unsigned cflag, iflag;
988 dbg("%s - port %d", __func__, port->number);
990 spin_lock_irqsave(&priv->lock, flags);
991 /* We can't clean this one up as we don't know the device type
993 if (!priv->termios_initialized) {
994 if (priv->chiptype == CT_EARTHMATE) {
995 *(tty->termios) = tty_std_termios;
996 tty->termios->c_cflag = B4800 | CS8 | CREAD | HUPCL |
998 tty->termios->c_ispeed = 4800;
999 tty->termios->c_ospeed = 4800;
1000 } else if (priv->chiptype == CT_CYPHIDCOM) {
1001 *(tty->termios) = tty_std_termios;
1002 tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
1004 tty->termios->c_ispeed = 9600;
1005 tty->termios->c_ospeed = 9600;
1006 } else if (priv->chiptype == CT_CA42V2) {
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;
1013 priv->termios_initialized = 1;
1015 spin_unlock_irqrestore(&priv->lock, flags);
1017 /* Unsupported features need clearing */
1018 tty->termios->c_cflag &= ~(CMSPAR|CRTSCTS);
1020 cflag = tty->termios->c_cflag;
1021 iflag = tty->termios->c_iflag;
1023 /* check if there are new settings */
1025 spin_lock_irqsave(&priv->lock, flags);
1026 priv->tmp_termios = *(tty->termios);
1027 spin_unlock_irqrestore(&priv->lock, flags);
1030 /* set number of data bits, parity, stop bits */
1031 /* when parity is disabled the parity type bit is ignored */
1033 /* 1 means 2 stop bits, 0 means 1 stop bit */
1034 stop_bits = cflag & CSTOPB ? 1 : 0;
1036 if (cflag & PARENB) {
1038 /* 1 means odd parity, 0 means even parity */
1039 parity_type = cflag & PARODD ? 1 : 0;
1041 parity_enable = parity_type = 0;
1043 switch (cflag & CSIZE) {
1057 dev_err(&port->dev, "%s - CSIZE was set, but not CS5-CS8\n",
1061 spin_lock_irqsave(&priv->lock, flags);
1062 oldlines = priv->line_control;
1063 if ((cflag & CBAUD) == B0) {
1064 /* drop dtr and rts */
1065 dbg("%s - dropping the lines, baud rate 0bps", __func__);
1066 priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
1068 priv->line_control = (CONTROL_DTR | CONTROL_RTS);
1069 spin_unlock_irqrestore(&priv->lock, flags);
1071 dbg("%s - sending %d stop_bits, %d parity_enable, %d parity_type, "
1072 "%d data_bits (+5)", __func__, stop_bits,
1073 parity_enable, parity_type, data_bits);
1075 cypress_serial_control(tty, port, tty_get_baud_rate(tty),
1076 data_bits, stop_bits,
1077 parity_enable, parity_type,
1078 0, CYPRESS_SET_CONFIG);
1080 /* we perform a CYPRESS_GET_CONFIG so that the current settings are
1081 * filled into the private structure this should confirm that all is
1082 * working if it returns what we just set */
1083 cypress_serial_control(tty, port, 0, 0, 0, 0, 0, 0, CYPRESS_GET_CONFIG);
1085 /* Here we can define custom tty settings for devices; the main tty
1086 * termios flag base comes from empeg.c */
1088 spin_lock_irqsave(&priv->lock, flags);
1089 if (priv->chiptype == CT_EARTHMATE && priv->baud_rate == 4800) {
1090 dbg("Using custom termios settings for a baud rate of "
1092 /* define custom termios settings for NMEA protocol */
1094 tty->termios->c_iflag /* input modes - */
1095 &= ~(IGNBRK /* disable ignore break */
1096 | BRKINT /* disable break causes interrupt */
1097 | PARMRK /* disable mark parity errors */
1098 | ISTRIP /* disable clear high bit of input char */
1099 | INLCR /* disable translate NL to CR */
1100 | IGNCR /* disable ignore CR */
1101 | ICRNL /* disable translate CR to NL */
1102 | IXON); /* disable enable XON/XOFF flow control */
1104 tty->termios->c_oflag /* output modes */
1105 &= ~OPOST; /* disable postprocess output char */
1107 tty->termios->c_lflag /* line discipline modes */
1108 &= ~(ECHO /* disable echo input characters */
1109 | ECHONL /* disable echo new line */
1110 | ICANON /* disable erase, kill, werase, and rprnt
1111 special characters */
1112 | ISIG /* disable interrupt, quit, and suspend
1113 special characters */
1114 | IEXTEN); /* disable non-POSIX special characters */
1115 } /* CT_CYPHIDCOM: Application should handle this for device */
1117 linechange = (priv->line_control != oldlines);
1118 spin_unlock_irqrestore(&priv->lock, flags);
1120 /* if necessary, set lines */
1123 cypress_write(tty, port, NULL, 0);
1125 } /* cypress_set_termios */
1128 /* returns amount of data still left in soft buffer */
1129 static int cypress_chars_in_buffer(struct tty_struct *tty)
1131 struct usb_serial_port *port = tty->driver_data;
1132 struct cypress_private *priv = usb_get_serial_port_data(port);
1134 unsigned long flags;
1136 dbg("%s - port %d", __func__, port->number);
1138 spin_lock_irqsave(&priv->lock, flags);
1139 chars = cypress_buf_data_avail(priv->buf);
1140 spin_unlock_irqrestore(&priv->lock, flags);
1142 dbg("%s - returns %d", __func__, chars);
1147 static void cypress_throttle(struct tty_struct *tty)
1149 struct usb_serial_port *port = tty->driver_data;
1150 struct cypress_private *priv = usb_get_serial_port_data(port);
1152 dbg("%s - port %d", __func__, port->number);
1154 spin_lock_irq(&priv->lock);
1155 priv->rx_flags = THROTTLED;
1156 spin_unlock_irq(&priv->lock);
1160 static void cypress_unthrottle(struct tty_struct *tty)
1162 struct usb_serial_port *port = tty->driver_data;
1163 struct cypress_private *priv = usb_get_serial_port_data(port);
1164 int actually_throttled, result;
1166 dbg("%s - port %d", __func__, port->number);
1168 spin_lock_irq(&priv->lock);
1169 actually_throttled = priv->rx_flags & ACTUALLY_THROTTLED;
1171 spin_unlock_irq(&priv->lock);
1173 if (!priv->comm_is_ok)
1176 if (actually_throttled) {
1177 port->interrupt_in_urb->dev = port->serial->dev;
1179 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
1181 dev_err(&port->dev, "%s - failed submitting read urb, "
1182 "error %d\n", __func__, result);
1183 cypress_set_dead(port);
1189 static void cypress_read_int_callback(struct urb *urb)
1191 struct usb_serial_port *port = urb->context;
1192 struct cypress_private *priv = usb_get_serial_port_data(port);
1193 struct tty_struct *tty;
1194 unsigned char *data = urb->transfer_buffer;
1195 unsigned long flags;
1196 char tty_flag = TTY_NORMAL;
1201 int status = urb->status;
1203 dbg("%s - port %d", __func__, port->number);
1206 case 0: /* success */
1211 /* precursor to disconnect so just go away */
1214 /* Can't call usb_clear_halt while in_interrupt */
1217 /* something ugly is going on... */
1218 dev_err(&urb->dev->dev,
1219 "%s - unexpected nonzero read status received: %d\n",
1221 cypress_set_dead(port);
1225 spin_lock_irqsave(&priv->lock, flags);
1226 if (priv->rx_flags & THROTTLED) {
1227 dbg("%s - now throttling", __func__);
1228 priv->rx_flags |= ACTUALLY_THROTTLED;
1229 spin_unlock_irqrestore(&priv->lock, flags);
1232 spin_unlock_irqrestore(&priv->lock, flags);
1234 tty = tty_port_tty_get(&port->port);
1236 dbg("%s - bad tty pointer - exiting", __func__);
1240 spin_lock_irqsave(&priv->lock, flags);
1241 result = urb->actual_length;
1242 switch (priv->pkt_fmt) {
1244 case packet_format_1:
1245 /* This is for the CY7C64013... */
1246 priv->current_status = data[0] & 0xF8;
1247 bytes = data[1] + 2;
1252 case packet_format_2:
1253 /* This is for the CY7C63743... */
1254 priv->current_status = data[0] & 0xF8;
1255 bytes = (data[0] & 0x07) + 1;
1261 spin_unlock_irqrestore(&priv->lock, flags);
1262 if (result < bytes) {
1263 dbg("%s - wrong packet size - received %d bytes but packet "
1264 "said %d bytes", __func__, result, bytes);
1268 usb_serial_debug_data(debug, &port->dev, __func__,
1269 urb->actual_length, data);
1271 spin_lock_irqsave(&priv->lock, flags);
1272 /* check to see if status has changed */
1273 if (priv->current_status != priv->prev_status) {
1274 priv->diff_status |= priv->current_status ^
1276 wake_up_interruptible(&priv->delta_msr_wait);
1277 priv->prev_status = priv->current_status;
1279 spin_unlock_irqrestore(&priv->lock, flags);
1281 /* hangup, as defined in acm.c... this might be a bad place for it
1283 if (tty && !(tty->termios->c_cflag & CLOCAL) &&
1284 !(priv->current_status & UART_CD)) {
1285 dbg("%s - calling hangup", __func__);
1290 /* There is one error bit... I'm assuming it is a parity error
1291 * indicator as the generic firmware will set this bit to 1 if a
1292 * parity error occurs.
1293 * I can not find reference to any other error events. */
1294 spin_lock_irqsave(&priv->lock, flags);
1295 if (priv->current_status & CYP_ERROR) {
1296 spin_unlock_irqrestore(&priv->lock, flags);
1297 tty_flag = TTY_PARITY;
1298 dbg("%s - Parity Error detected", __func__);
1300 spin_unlock_irqrestore(&priv->lock, flags);
1302 /* process read if there is data other than line status */
1303 if (tty && (bytes > i)) {
1304 bytes = tty_buffer_request_room(tty, bytes);
1305 for (; i < bytes ; ++i) {
1306 dbg("pushing byte number %d - %d - %c", i, data[i],
1308 tty_insert_flip_char(tty, data[i], tty_flag);
1310 tty_flip_buffer_push(tty);
1313 spin_lock_irqsave(&priv->lock, flags);
1314 /* control and status byte(s) are also counted */
1315 priv->bytes_in += bytes;
1316 spin_unlock_irqrestore(&priv->lock, flags);
1321 /* Continue trying to always read... unless the port has closed. */
1323 if (port->port.count > 0 && priv->comm_is_ok) {
1324 usb_fill_int_urb(port->interrupt_in_urb, port->serial->dev,
1325 usb_rcvintpipe(port->serial->dev,
1326 port->interrupt_in_endpointAddress),
1327 port->interrupt_in_urb->transfer_buffer,
1328 port->interrupt_in_urb->transfer_buffer_length,
1329 cypress_read_int_callback, port,
1330 priv->read_urb_interval);
1331 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
1333 dev_err(&urb->dev->dev, "%s - failed resubmitting "
1334 "read urb, error %d\n", __func__,
1336 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 /*****************************************************************************
1397 * Write buffer functions - buffering code from pl2303 used
1398 *****************************************************************************/
1403 * Allocate a circular buffer and all associated memory.
1406 static struct cypress_buf *cypress_buf_alloc(unsigned int size)
1409 struct cypress_buf *cb;
1415 cb = kmalloc(sizeof(struct cypress_buf), GFP_KERNEL);
1419 cb->buf_buf = kmalloc(size, GFP_KERNEL);
1420 if (cb->buf_buf == NULL) {
1425 cb->buf_size = size;
1426 cb->buf_get = cb->buf_put = cb->buf_buf;
1436 * Free the buffer and all associated memory.
1439 static void cypress_buf_free(struct cypress_buf *cb)
1451 * Clear out all data in the circular buffer.
1454 static void cypress_buf_clear(struct cypress_buf *cb)
1457 cb->buf_get = cb->buf_put;
1458 /* equivalent to a get of all data available */
1463 * cypress_buf_data_avail
1465 * Return the number of bytes of data available in the circular
1469 static unsigned int cypress_buf_data_avail(struct cypress_buf *cb)
1472 return (cb->buf_size + cb->buf_put - cb->buf_get)
1480 * cypress_buf_space_avail
1482 * Return the number of bytes of space available in the circular
1486 static unsigned int cypress_buf_space_avail(struct cypress_buf *cb)
1489 return (cb->buf_size + cb->buf_get - cb->buf_put - 1)
1499 * Copy data data from a user buffer and put it into the circular buffer.
1500 * Restrict to the amount of space available.
1502 * Return the number of bytes copied.
1505 static unsigned int cypress_buf_put(struct cypress_buf *cb, const char *buf,
1515 len = cypress_buf_space_avail(cb);
1522 len = cb->buf_buf + cb->buf_size - cb->buf_put;
1524 memcpy(cb->buf_put, buf, len);
1525 memcpy(cb->buf_buf, buf+len, count - len);
1526 cb->buf_put = cb->buf_buf + count - len;
1528 memcpy(cb->buf_put, buf, count);
1530 cb->buf_put += count;
1531 else /* count == len */
1532 cb->buf_put = cb->buf_buf;
1543 * Get data from the circular buffer and copy to the given buffer.
1544 * Restrict to the amount of data available.
1546 * Return the number of bytes copied.
1549 static unsigned int cypress_buf_get(struct cypress_buf *cb, char *buf,
1559 len = cypress_buf_data_avail(cb);
1566 len = cb->buf_buf + cb->buf_size - cb->buf_get;
1568 memcpy(buf, cb->buf_get, len);
1569 memcpy(buf+len, cb->buf_buf, count - len);
1570 cb->buf_get = cb->buf_buf + count - len;
1572 memcpy(buf, cb->buf_get, count);
1574 cb->buf_get += count;
1575 else /* count == len */
1576 cb->buf_get = cb->buf_buf;
1583 /*****************************************************************************
1585 *****************************************************************************/
1587 static int __init cypress_init(void)
1591 dbg("%s", __func__);
1593 retval = usb_serial_register(&cypress_earthmate_device);
1595 goto failed_em_register;
1596 retval = usb_serial_register(&cypress_hidcom_device);
1598 goto failed_hidcom_register;
1599 retval = usb_serial_register(&cypress_ca42v2_device);
1601 goto failed_ca42v2_register;
1602 retval = usb_register(&cypress_driver);
1604 goto failed_usb_register;
1606 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
1610 failed_usb_register:
1611 usb_serial_deregister(&cypress_ca42v2_device);
1612 failed_ca42v2_register:
1613 usb_serial_deregister(&cypress_hidcom_device);
1614 failed_hidcom_register:
1615 usb_serial_deregister(&cypress_earthmate_device);
1621 static void __exit cypress_exit(void)
1623 dbg("%s", __func__);
1625 usb_deregister(&cypress_driver);
1626 usb_serial_deregister(&cypress_earthmate_device);
1627 usb_serial_deregister(&cypress_hidcom_device);
1628 usb_serial_deregister(&cypress_ca42v2_device);
1632 module_init(cypress_init);
1633 module_exit(cypress_exit);
1635 MODULE_AUTHOR(DRIVER_AUTHOR);
1636 MODULE_DESCRIPTION(DRIVER_DESC);
1637 MODULE_VERSION(DRIVER_VERSION);
1638 MODULE_LICENSE("GPL");
1640 module_param(debug, bool, S_IRUGO | S_IWUSR);
1641 MODULE_PARM_DESC(debug, "Debug enabled or not");
1642 module_param(stats, bool, S_IRUGO | S_IWUSR);
1643 MODULE_PARM_DESC(stats, "Enable statistics or not");
1644 module_param(interval, int, S_IRUGO | S_IWUSR);
1645 MODULE_PARM_DESC(interval, "Overrides interrupt interval");