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 driver
16 * See http://geocities.com/i0xox0i for information on this driver and the
17 * earthmate usb device.
19 * Lonnie Mendez <dignome@gmail.com>
21 * Fixed problem where setting or retreiving the serial config would fail with
22 * EPIPE. Removed CRTS toggling so the driver behaves more like other usbserial
23 * adapters. Issued new interval of 1ms instead of the default 10ms. As a
24 * result, transfer speed has been substantially increased. From avg. 850bps to
25 * avg. 3300bps. initial termios has also been modified. Cleaned up code and
26 * formatting issues so it is more readable. Replaced the C++ style comments.
28 * Lonnie Mendez <dignome@gmail.com>
30 * Incorporated write buffering from pl2303 driver. Fixed bug with line
31 * handling so both lines are raised in cypress_open. (was dropping rts)
32 * Various code cleanups made as well along with other misc bug fixes.
34 * Lonnie Mendez <dignome@gmail.com>
36 * Driver modified to support dynamic line settings. Various improvments
41 * Driver first released.
45 /* Thanks to Neil Whelchel for writing the first cypress m8 implementation for linux. */
46 /* Thanks to cypress for providing references for the hid reports. */
47 /* Thanks to Jiang Zhang for providing links and for general help. */
48 /* Code originates and was built up from ftdi_sio, belkin, pl2303 and others. */
51 #include <linux/config.h>
52 #include <linux/kernel.h>
53 #include <linux/errno.h>
54 #include <linux/init.h>
55 #include <linux/slab.h>
56 #include <linux/tty.h>
57 #include <linux/tty_driver.h>
58 #include <linux/tty_flip.h>
59 #include <linux/module.h>
60 #include <linux/moduleparam.h>
61 #include <linux/spinlock.h>
62 #include <linux/usb.h>
63 #include <linux/serial.h>
64 #include <linux/delay.h>
65 #include <asm/uaccess.h>
67 #include "usb-serial.h"
68 #include "cypress_m8.h"
71 #ifdef CONFIG_USB_SERIAL_DEBUG
82 #define DRIVER_VERSION "v1.09"
83 #define DRIVER_AUTHOR "Lonnie Mendez <dignome@gmail.com>, Neil Whelchel <koyama@firstlight.net>"
84 #define DRIVER_DESC "Cypress USB to Serial Driver"
86 /* write buffer size defines */
87 #define CYPRESS_BUF_SIZE 1024
88 #define CYPRESS_CLOSING_WAIT (30*HZ)
90 static struct usb_device_id id_table_earthmate [] = {
91 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB) },
92 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB_LT20) },
93 { } /* Terminating entry */
96 static struct usb_device_id id_table_cyphidcomrs232 [] = {
97 { USB_DEVICE(VENDOR_ID_CYPRESS, PRODUCT_ID_CYPHIDCOM) },
98 { } /* Terminating entry */
101 static struct usb_device_id id_table_combined [] = {
102 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB) },
103 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB_LT20) },
104 { USB_DEVICE(VENDOR_ID_CYPRESS, PRODUCT_ID_CYPHIDCOM) },
105 { } /* Terminating entry */
108 MODULE_DEVICE_TABLE (usb, id_table_combined);
110 static struct usb_driver cypress_driver = {
112 .probe = usb_serial_probe,
113 .disconnect = usb_serial_disconnect,
114 .id_table = id_table_combined,
117 struct cypress_private {
118 spinlock_t lock; /* private lock */
119 int chiptype; /* identifier of device, for quirks/etc */
120 int bytes_in; /* used for statistics */
121 int bytes_out; /* used for statistics */
122 int cmd_count; /* used for statistics */
123 int cmd_ctrl; /* always set this to 1 before issuing a command */
124 struct cypress_buf *buf; /* write buffer */
125 int write_urb_in_use; /* write urb in use indicator */
126 int termios_initialized;
127 __u8 line_control; /* holds dtr / rts value */
128 __u8 current_status; /* received from last read - info on dsr,cts,cd,ri,etc */
129 __u8 current_config; /* stores the current configuration byte */
130 __u8 rx_flags; /* throttling - used from whiteheat/ftdi_sio */
131 int baud_rate; /* stores current baud rate in integer form */
132 int cbr_mask; /* stores current baud rate in masked form */
133 int isthrottled; /* if throttled, discard reads */
134 wait_queue_head_t delta_msr_wait; /* used for TIOCMIWAIT */
135 char prev_status, diff_status; /* used for TIOCMIWAIT */
136 /* we pass a pointer to this as the arguement sent to cypress_set_termios old_termios */
137 struct termios tmp_termios; /* stores the old termios settings */
140 /* write buffer structure */
142 unsigned int buf_size;
148 /* function prototypes for the Cypress USB to serial device */
149 static int cypress_earthmate_startup (struct usb_serial *serial);
150 static int cypress_hidcom_startup (struct usb_serial *serial);
151 static void cypress_shutdown (struct usb_serial *serial);
152 static int cypress_open (struct usb_serial_port *port, struct file *filp);
153 static void cypress_close (struct usb_serial_port *port, struct file *filp);
154 static int cypress_write (struct usb_serial_port *port, const unsigned char *buf, int count);
155 static void cypress_send (struct usb_serial_port *port);
156 static int cypress_write_room (struct usb_serial_port *port);
157 static int cypress_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg);
158 static void cypress_set_termios (struct usb_serial_port *port, struct termios * old);
159 static int cypress_tiocmget (struct usb_serial_port *port, struct file *file);
160 static int cypress_tiocmset (struct usb_serial_port *port, struct file *file, unsigned int set, unsigned int clear);
161 static int cypress_chars_in_buffer (struct usb_serial_port *port);
162 static void cypress_throttle (struct usb_serial_port *port);
163 static void cypress_unthrottle (struct usb_serial_port *port);
164 static void cypress_read_int_callback (struct urb *urb, struct pt_regs *regs);
165 static void cypress_write_int_callback (struct urb *urb, struct pt_regs *regs);
166 /* baud helper functions */
167 static int mask_to_rate (unsigned mask);
168 static unsigned rate_to_mask (int rate);
169 /* write buffer functions */
170 static struct cypress_buf *cypress_buf_alloc(unsigned int size);
171 static void cypress_buf_free(struct cypress_buf *cb);
172 static void cypress_buf_clear(struct cypress_buf *cb);
173 static unsigned int cypress_buf_data_avail(struct cypress_buf *cb);
174 static unsigned int cypress_buf_space_avail(struct cypress_buf *cb);
175 static unsigned int cypress_buf_put(struct cypress_buf *cb, const char *buf, unsigned int count);
176 static unsigned int cypress_buf_get(struct cypress_buf *cb, char *buf, unsigned int count);
179 static struct usb_serial_device_type cypress_earthmate_device = {
180 .owner = THIS_MODULE,
181 .name = "DeLorme Earthmate USB",
182 .short_name = "earthmate",
183 .id_table = id_table_earthmate,
184 .num_interrupt_in = 1,
185 .num_interrupt_out = 1,
186 .num_bulk_in = NUM_DONT_CARE,
187 .num_bulk_out = NUM_DONT_CARE,
189 .attach = cypress_earthmate_startup,
190 .shutdown = cypress_shutdown,
191 .open = cypress_open,
192 .close = cypress_close,
193 .write = cypress_write,
194 .write_room = cypress_write_room,
195 .ioctl = cypress_ioctl,
196 .set_termios = cypress_set_termios,
197 .tiocmget = cypress_tiocmget,
198 .tiocmset = cypress_tiocmset,
199 .chars_in_buffer = cypress_chars_in_buffer,
200 .throttle = cypress_throttle,
201 .unthrottle = cypress_unthrottle,
202 .read_int_callback = cypress_read_int_callback,
203 .write_int_callback = cypress_write_int_callback,
206 static struct usb_serial_device_type cypress_hidcom_device = {
207 .owner = THIS_MODULE,
208 .name = "HID->COM RS232 Adapter",
209 .short_name = "cyphidcom",
210 .id_table = id_table_cyphidcomrs232,
211 .num_interrupt_in = 1,
212 .num_interrupt_out = 1,
213 .num_bulk_in = NUM_DONT_CARE,
214 .num_bulk_out = NUM_DONT_CARE,
216 .attach = cypress_hidcom_startup,
217 .shutdown = cypress_shutdown,
218 .open = cypress_open,
219 .close = cypress_close,
220 .write = cypress_write,
221 .write_room = cypress_write_room,
222 .ioctl = cypress_ioctl,
223 .set_termios = cypress_set_termios,
224 .tiocmget = cypress_tiocmget,
225 .tiocmset = cypress_tiocmset,
226 .chars_in_buffer = cypress_chars_in_buffer,
227 .throttle = cypress_throttle,
228 .unthrottle = cypress_unthrottle,
229 .read_int_callback = cypress_read_int_callback,
230 .write_int_callback = cypress_write_int_callback,
234 /*****************************************************************************
235 * Cypress serial helper functions
236 *****************************************************************************/
239 /* This function can either set or retrieve the current serial line settings */
240 static int cypress_serial_control (struct usb_serial_port *port, unsigned baud_mask, int data_bits, int stop_bits,
241 int parity_enable, int parity_type, int reset, int cypress_request_type)
243 int new_baudrate = 0, retval = 0, tries = 0;
244 struct cypress_private *priv;
245 __u8 feature_buffer[8];
248 dbg("%s", __FUNCTION__);
250 priv = usb_get_serial_port_data(port);
252 switch(cypress_request_type) {
253 case CYPRESS_SET_CONFIG:
256 * The general purpose firmware for the Cypress M8 allows for a maximum speed
257 * of 57600bps (I have no idea whether DeLorme chose to use the general purpose
258 * firmware or not), if you need to modify this speed setting for your own
259 * project please add your own chiptype and modify the code likewise. The
260 * Cypress HID->COM device will work successfully up to 115200bps (but the
261 * actual throughput is around 3kBps).
263 if (baud_mask != priv->cbr_mask) {
264 dbg("%s - baud rate is changing", __FUNCTION__);
265 if ( priv->chiptype == CT_EARTHMATE ) {
266 /* 300 and 600 baud rates are supported under the generic firmware,
267 * but are not used with NMEA and SiRF protocols */
269 if ( (baud_mask == B300) || (baud_mask == B600) ) {
270 err("%s - failed setting baud rate, unsupported speed",
272 new_baudrate = priv->baud_rate;
273 } else if ( (new_baudrate = mask_to_rate(baud_mask)) == -1) {
274 err("%s - failed setting baud rate, unsupported speed",
276 new_baudrate = priv->baud_rate;
278 } else if (priv->chiptype == CT_CYPHIDCOM) {
279 if ( (new_baudrate = mask_to_rate(baud_mask)) == -1) {
280 err("%s - failed setting baud rate, unsupported speed",
282 new_baudrate = priv->baud_rate;
284 } else if (priv->chiptype == CT_GENERIC) {
285 if ( (new_baudrate = mask_to_rate(baud_mask)) == -1) {
286 err("%s - failed setting baud rate, unsupported speed",
288 new_baudrate = priv->baud_rate;
291 info("%s - please define your chiptype", __FUNCTION__);
292 new_baudrate = priv->baud_rate;
294 } else { /* baud rate not changing, keep the old */
295 new_baudrate = priv->baud_rate;
297 dbg("%s - baud rate is being sent as %d", __FUNCTION__, new_baudrate);
299 memset(feature_buffer, 0, 8);
300 /* fill the feature_buffer with new configuration */
301 *((u_int32_t *)feature_buffer) = new_baudrate;
303 feature_buffer[4] |= data_bits; /* assign data bits in 2 bit space ( max 3 ) */
305 feature_buffer[4] |= (stop_bits << 3); /* assign stop bits in 1 bit space */
306 feature_buffer[4] |= (parity_enable << 4); /* assign parity flag in 1 bit space */
307 feature_buffer[4] |= (parity_type << 5); /* assign parity type in 1 bit space */
309 feature_buffer[4] |= (reset << 7); /* assign reset at end of byte, 1 bit space */
311 dbg("%s - device is being sent this feature report:", __FUNCTION__);
312 dbg("%s - %02X - %02X - %02X - %02X - %02X", __FUNCTION__, feature_buffer[0], feature_buffer[1],
313 feature_buffer[2], feature_buffer[3], feature_buffer[4]);
316 retval = usb_control_msg (port->serial->dev, usb_sndctrlpipe(port->serial->dev, 0),
317 HID_REQ_SET_REPORT, USB_DIR_OUT | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
318 0x0300, 0, feature_buffer, 8, 500);
324 usb_clear_halt(port->serial->dev, 0x00);
325 } while (retval != 8 && retval != ENODEV);
328 err("%s - failed sending serial line settings - %d", __FUNCTION__, retval);
330 spin_lock_irqsave(&priv->lock, flags);
331 priv->baud_rate = new_baudrate;
332 priv->cbr_mask = baud_mask;
333 priv->current_config = feature_buffer[4];
334 spin_unlock_irqrestore(&priv->lock, flags);
337 case CYPRESS_GET_CONFIG:
338 dbg("%s - retreiving serial line settings", __FUNCTION__);
339 /* set initial values in feature buffer */
340 memset(feature_buffer, 0, 8);
343 retval = usb_control_msg (port->serial->dev, usb_rcvctrlpipe(port->serial->dev, 0),
344 HID_REQ_GET_REPORT, USB_DIR_IN | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
345 0x0300, 0, feature_buffer, 8, 500);
351 usb_clear_halt(port->serial->dev, 0x00);
352 } while (retval != 5 && retval != ENODEV);
355 err("%s - failed to retreive serial line settings - %d", __FUNCTION__, retval);
358 spin_lock_irqsave(&priv->lock, flags);
360 /* store the config in one byte, and later use bit masks to check values */
361 priv->current_config = feature_buffer[4];
362 priv->baud_rate = *((u_int32_t *)feature_buffer);
364 if ( (priv->cbr_mask = rate_to_mask(priv->baud_rate)) == 0x40)
365 dbg("%s - failed setting the baud mask (not defined)", __FUNCTION__);
366 spin_unlock_irqrestore(&priv->lock, flags);
369 spin_lock_irqsave(&priv->lock, flags);
371 spin_unlock_irqrestore(&priv->lock, flags);
374 } /* cypress_serial_control */
377 /* given a baud mask, it will return integer baud on success */
378 static int mask_to_rate (unsigned mask)
383 case B0: rate = 0; break;
384 case B300: rate = 300; break;
385 case B600: rate = 600; break;
386 case B1200: rate = 1200; break;
387 case B2400: rate = 2400; break;
388 case B4800: rate = 4800; break;
389 case B9600: rate = 9600; break;
390 case B19200: rate = 19200; break;
391 case B38400: rate = 38400; break;
392 case B57600: rate = 57600; break;
393 case B115200: rate = 115200; break;
401 static unsigned rate_to_mask (int rate)
406 case 0: mask = B0; break;
407 case 300: mask = B300; break;
408 case 600: mask = B600; break;
409 case 1200: mask = B1200; break;
410 case 2400: mask = B2400; break;
411 case 4800: mask = B4800; break;
412 case 9600: mask = B9600; break;
413 case 19200: mask = B19200; break;
414 case 38400: mask = B38400; break;
415 case 57600: mask = B57600; break;
416 case 115200: mask = B115200; break;
417 default: mask = 0x40;
422 /*****************************************************************************
423 * Cypress serial driver functions
424 *****************************************************************************/
427 static int generic_startup (struct usb_serial *serial)
429 struct cypress_private *priv;
431 dbg("%s - port %d", __FUNCTION__, serial->port[0]->number);
433 priv = kmalloc(sizeof (struct cypress_private), GFP_KERNEL);
437 memset(priv, 0x00, sizeof (struct cypress_private));
438 spin_lock_init(&priv->lock);
439 priv->buf = cypress_buf_alloc(CYPRESS_BUF_SIZE);
440 if (priv->buf == NULL) {
444 init_waitqueue_head(&priv->delta_msr_wait);
446 usb_reset_configuration (serial->dev);
450 priv->line_control = 0;
451 priv->termios_initialized = 0;
453 priv->cbr_mask = B300;
454 usb_set_serial_port_data(serial->port[0], priv);
460 static int cypress_earthmate_startup (struct usb_serial *serial)
462 struct cypress_private *priv;
464 dbg("%s", __FUNCTION__);
466 if (generic_startup(serial)) {
467 dbg("%s - Failed setting up port %d", __FUNCTION__, serial->port[0]->number);
471 priv = usb_get_serial_port_data(serial->port[0]);
472 priv->chiptype = CT_EARTHMATE;
475 } /* cypress_earthmate_startup */
478 static int cypress_hidcom_startup (struct usb_serial *serial)
480 struct cypress_private *priv;
482 dbg("%s", __FUNCTION__);
484 if (generic_startup(serial)) {
485 dbg("%s - Failed setting up port %d", __FUNCTION__, serial->port[0]->number);
489 priv = usb_get_serial_port_data(serial->port[0]);
490 priv->chiptype = CT_CYPHIDCOM;
493 } /* cypress_hidcom_startup */
496 static void cypress_shutdown (struct usb_serial *serial)
498 struct cypress_private *priv;
500 dbg ("%s - port %d", __FUNCTION__, serial->port[0]->number);
502 /* all open ports are closed at this point */
504 priv = usb_get_serial_port_data(serial->port[0]);
507 cypress_buf_free(priv->buf);
509 usb_set_serial_port_data(serial->port[0], NULL);
514 static int cypress_open (struct usb_serial_port *port, struct file *filp)
516 struct cypress_private *priv = usb_get_serial_port_data(port);
517 struct usb_serial *serial = port->serial;
521 dbg("%s - port %d", __FUNCTION__, port->number);
523 /* clear halts before open */
524 usb_clear_halt(serial->dev, 0x81);
525 usb_clear_halt(serial->dev, 0x02);
527 spin_lock_irqsave(&priv->lock, flags);
528 /* reset read/write statistics */
533 spin_unlock_irqrestore(&priv->lock, flags);
535 /* setting to zero could cause data loss */
536 port->tty->low_latency = 1;
538 /* raise both lines and set termios */
539 spin_lock_irqsave(&priv->lock, flags);
540 priv->line_control = CONTROL_DTR | CONTROL_RTS;
542 spin_unlock_irqrestore(&priv->lock, flags);
543 result = cypress_write(port, NULL, 0);
546 dev_err(&port->dev, "%s - failed setting the control lines - error %d\n", __FUNCTION__, result);
549 dbg("%s - success setting the control lines", __FUNCTION__);
551 cypress_set_termios(port, &priv->tmp_termios);
553 /* setup the port and start reading from the device */
554 if(!port->interrupt_in_urb){
555 err("%s - interrupt_in_urb is empty!", __FUNCTION__);
559 usb_fill_int_urb(port->interrupt_in_urb, serial->dev,
560 usb_rcvintpipe(serial->dev, port->interrupt_in_endpointAddress),
561 port->interrupt_in_urb->transfer_buffer, port->interrupt_in_urb->transfer_buffer_length,
562 cypress_read_int_callback, port, interval);
563 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
566 dev_err(&port->dev, "%s - failed submitting read urb, error %d\n", __FUNCTION__, result);
573 static void cypress_close(struct usb_serial_port *port, struct file * filp)
575 struct cypress_private *priv = usb_get_serial_port_data(port);
576 unsigned int c_cflag;
582 dbg("%s - port %d", __FUNCTION__, port->number);
584 /* wait for data to drain from buffer */
585 spin_lock_irqsave(&priv->lock, flags);
586 timeout = CYPRESS_CLOSING_WAIT;
587 init_waitqueue_entry(&wait, current);
588 add_wait_queue(&port->tty->write_wait, &wait);
590 set_current_state(TASK_INTERRUPTIBLE);
591 if (cypress_buf_data_avail(priv->buf) == 0
592 || timeout == 0 || signal_pending(current)
593 || !usb_get_intfdata(port->serial->interface))
595 spin_unlock_irqrestore(&priv->lock, flags);
596 timeout = schedule_timeout(timeout);
597 spin_lock_irqsave(&priv->lock, flags);
599 set_current_state(TASK_RUNNING);
600 remove_wait_queue(&port->tty->write_wait, &wait);
601 /* clear out any remaining data in the buffer */
602 cypress_buf_clear(priv->buf);
603 spin_unlock_irqrestore(&priv->lock, flags);
605 /* wait for characters to drain from device */
606 bps = tty_get_baud_rate(port->tty);
608 timeout = max((HZ*2560)/bps,HZ/10);
611 set_current_state(TASK_INTERRUPTIBLE);
612 schedule_timeout(timeout);
614 dbg("%s - stopping urbs", __FUNCTION__);
615 usb_kill_urb (port->interrupt_in_urb);
616 usb_kill_urb (port->interrupt_out_urb);
619 c_cflag = port->tty->termios->c_cflag;
620 if (c_cflag & HUPCL) {
621 /* drop dtr and rts */
622 priv = usb_get_serial_port_data(port);
623 spin_lock_irqsave(&priv->lock, flags);
624 priv->line_control = 0;
626 spin_unlock_irqrestore(&priv->lock, flags);
627 cypress_write(port, NULL, 0);
632 dev_info (&port->dev, "Statistics: %d Bytes In | %d Bytes Out | %d Commands Issued\n",
633 priv->bytes_in, priv->bytes_out, priv->cmd_count);
634 } /* cypress_close */
637 static int cypress_write(struct usb_serial_port *port, const unsigned char *buf, int count)
639 struct cypress_private *priv = usb_get_serial_port_data(port);
642 dbg("%s - port %d, %d bytes", __FUNCTION__, port->number, count);
644 /* line control commands, which need to be executed immediately,
645 are not put into the buffer for obvious reasons.
647 if (priv->cmd_ctrl) {
655 spin_lock_irqsave(&priv->lock, flags);
656 count = cypress_buf_put(priv->buf, buf, count);
657 spin_unlock_irqrestore(&priv->lock, flags);
663 } /* cypress_write */
666 static void cypress_send(struct usb_serial_port *port)
668 int count = 0, result, offset, actual_size;
669 struct cypress_private *priv = usb_get_serial_port_data(port);
672 dbg("%s - port %d", __FUNCTION__, port->number);
673 dbg("%s - interrupt out size is %d", __FUNCTION__, port->interrupt_out_size);
675 spin_lock_irqsave(&priv->lock, flags);
676 if (priv->write_urb_in_use) {
677 dbg("%s - can't write, urb in use", __FUNCTION__);
678 spin_unlock_irqrestore(&priv->lock, flags);
681 spin_unlock_irqrestore(&priv->lock, flags);
684 memset(port->interrupt_out_urb->transfer_buffer, 0, port->interrupt_out_size);
686 spin_lock_irqsave(&priv->lock, flags);
687 switch (port->interrupt_out_size) {
689 /* this is for the CY7C64013... */
691 port->interrupt_out_buffer[0] = priv->line_control;
694 /* this is for the CY7C63743... */
696 port->interrupt_out_buffer[0] = priv->line_control;
699 dbg("%s - wrong packet size", __FUNCTION__);
700 spin_unlock_irqrestore(&priv->lock, flags);
704 if (priv->line_control & CONTROL_RESET)
705 priv->line_control &= ~CONTROL_RESET;
707 if (priv->cmd_ctrl) {
709 dbg("%s - line control command being issued", __FUNCTION__);
710 spin_unlock_irqrestore(&priv->lock, flags);
713 spin_unlock_irqrestore(&priv->lock, flags);
715 count = cypress_buf_get(priv->buf, &port->interrupt_out_buffer[offset],
716 port->interrupt_out_size-offset);
722 switch (port->interrupt_out_size) {
724 port->interrupt_out_buffer[1] = count;
727 port->interrupt_out_buffer[0] |= count;
730 dbg("%s - count is %d", __FUNCTION__, count);
733 spin_lock_irqsave(&priv->lock, flags);
734 priv->write_urb_in_use = 1;
735 spin_unlock_irqrestore(&priv->lock, flags);
740 actual_size = count + (port->interrupt_out_size == 32 ? 2 : 1);
742 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, port->interrupt_out_size,
743 port->interrupt_out_urb->transfer_buffer);
745 port->interrupt_out_urb->transfer_buffer_length = actual_size;
746 port->interrupt_out_urb->dev = port->serial->dev;
747 port->interrupt_out_urb->interval = interval;
748 result = usb_submit_urb (port->interrupt_out_urb, GFP_ATOMIC);
750 dev_err(&port->dev, "%s - failed submitting write urb, error %d\n", __FUNCTION__,
752 priv->write_urb_in_use = 0;
755 spin_lock_irqsave(&priv->lock, flags);
756 if (priv->cmd_ctrl) {
759 priv->bytes_out += count; /* do not count the line control and size bytes */
760 spin_unlock_irqrestore(&priv->lock, flags);
762 schedule_work(&port->work);
766 /* returns how much space is available in the soft buffer */
767 static int cypress_write_room(struct usb_serial_port *port)
769 struct cypress_private *priv = usb_get_serial_port_data(port);
773 dbg("%s - port %d", __FUNCTION__, port->number);
775 spin_lock_irqsave(&priv->lock, flags);
776 room = cypress_buf_space_avail(priv->buf);
777 spin_unlock_irqrestore(&priv->lock, flags);
779 dbg("%s - returns %d", __FUNCTION__, room);
784 static int cypress_tiocmget (struct usb_serial_port *port, struct file *file)
786 struct cypress_private *priv = usb_get_serial_port_data(port);
787 __u8 status, control;
788 unsigned int result = 0;
791 dbg("%s - port %d", __FUNCTION__, port->number);
793 spin_lock_irqsave(&priv->lock, flags);
794 control = priv->line_control;
795 status = priv->current_status;
796 spin_unlock_irqrestore(&priv->lock, flags);
798 result = ((control & CONTROL_DTR) ? TIOCM_DTR : 0)
799 | ((control & CONTROL_RTS) ? TIOCM_RTS : 0)
800 | ((status & UART_CTS) ? TIOCM_CTS : 0)
801 | ((status & UART_DSR) ? TIOCM_DSR : 0)
802 | ((status & UART_RI) ? TIOCM_RI : 0)
803 | ((status & UART_CD) ? TIOCM_CD : 0);
805 dbg("%s - result = %x", __FUNCTION__, result);
811 static int cypress_tiocmset (struct usb_serial_port *port, struct file *file,
812 unsigned int set, unsigned int clear)
814 struct cypress_private *priv = usb_get_serial_port_data(port);
817 dbg("%s - port %d", __FUNCTION__, port->number);
819 spin_lock_irqsave(&priv->lock, flags);
821 priv->line_control |= CONTROL_RTS;
823 priv->line_control |= CONTROL_DTR;
824 if (clear & TIOCM_RTS)
825 priv->line_control &= ~CONTROL_RTS;
826 if (clear & TIOCM_DTR)
827 priv->line_control &= ~CONTROL_DTR;
828 spin_unlock_irqrestore(&priv->lock, flags);
831 return cypress_write(port, NULL, 0);
835 static int cypress_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg)
837 struct cypress_private *priv = usb_get_serial_port_data(port);
839 dbg("%s - port %d, cmd 0x%.4x", __FUNCTION__, port->number, cmd);
843 if (copy_to_user((void __user *)arg, port->tty->termios, sizeof(struct termios))) {
849 if (copy_from_user(port->tty->termios, (void __user *)arg, sizeof(struct termios))) {
852 /* here we need to call cypress_set_termios to invoke the new settings */
853 cypress_set_termios(port, &priv->tmp_termios);
856 /* these are called when setting baud rate from gpsd */
858 if (copy_to_user((void __user *)arg, port->tty->termios, sizeof(struct termios))) {
864 if (copy_from_user(port->tty->termios, (void __user *)arg, sizeof(struct termios))) {
867 /* here we need to call cypress_set_termios to invoke the new settings */
868 cypress_set_termios(port, &priv->tmp_termios);
871 /* This code comes from drivers/char/serial.c and ftdi_sio.c */
873 while (priv != NULL) {
874 interruptible_sleep_on(&priv->delta_msr_wait);
875 /* see if a signal did it */
876 if (signal_pending(current))
879 char diff = priv->diff_status;
882 return -EIO; /* no change => error */
885 /* consume all events */
886 priv->diff_status = 0;
888 /* return 0 if caller wanted to know about these bits */
889 if ( ((arg & TIOCM_RNG) && (diff & UART_RI)) ||
890 ((arg & TIOCM_DSR) && (diff & UART_DSR)) ||
891 ((arg & TIOCM_CD) && (diff & UART_CD)) ||
892 ((arg & TIOCM_CTS) && (diff & UART_CTS)) ) {
895 /* otherwise caller can't care less about what happened,
896 * and so we continue to wait for more events.
906 dbg("%s - arg not supported - it was 0x%04x - check include/asm/ioctls.h", __FUNCTION__, cmd);
909 } /* cypress_ioctl */
912 static void cypress_set_termios (struct usb_serial_port *port, struct termios *old_termios)
914 struct cypress_private *priv = usb_get_serial_port_data(port);
915 struct tty_struct *tty;
916 int data_bits, stop_bits, parity_type, parity_enable;
917 unsigned cflag, iflag, baud_mask;
922 dbg("%s - port %d", __FUNCTION__, port->number);
925 if ((!tty) || (!tty->termios)) {
926 dbg("%s - no tty structures", __FUNCTION__);
930 spin_lock_irqsave(&priv->lock, flags);
931 if (!priv->termios_initialized) {
932 if (priv->chiptype == CT_EARTHMATE) {
933 *(tty->termios) = tty_std_termios;
934 tty->termios->c_cflag = B4800 | CS8 | CREAD | HUPCL | CLOCAL;
935 } else if (priv->chiptype == CT_CYPHIDCOM) {
936 *(tty->termios) = tty_std_termios;
937 tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
939 priv->termios_initialized = 1;
941 spin_unlock_irqrestore(&priv->lock, flags);
943 cflag = tty->termios->c_cflag;
944 iflag = tty->termios->c_iflag;
946 /* check if there are new settings */
948 if ((cflag != old_termios->c_cflag) ||
949 (RELEVANT_IFLAG(iflag) != RELEVANT_IFLAG(old_termios->c_iflag))) {
950 dbg("%s - attempting to set new termios settings", __FUNCTION__);
951 /* should make a copy of this in case something goes wrong in the function, we can restore it */
952 spin_lock_irqsave(&priv->lock, flags);
953 priv->tmp_termios = *(tty->termios);
954 spin_unlock_irqrestore(&priv->lock, flags);
956 dbg("%s - nothing to do, exiting", __FUNCTION__);
962 /* set number of data bits, parity, stop bits */
963 /* when parity is disabled the parity type bit is ignored */
965 stop_bits = cflag & CSTOPB ? 1 : 0; /* 1 means 2 stop bits, 0 means 1 stop bit */
967 if (cflag & PARENB) {
969 parity_type = cflag & PARODD ? 1 : 0; /* 1 means odd parity, 0 means even parity */
971 parity_enable = parity_type = 0;
974 switch (cflag & CSIZE) {
975 case CS5: data_bits = 0; break;
976 case CS6: data_bits = 1; break;
977 case CS7: data_bits = 2; break;
978 case CS8: data_bits = 3; break;
979 default: err("%s - CSIZE was set, but not CS5-CS8", __FUNCTION__); data_bits = 3;
984 spin_lock_irqsave(&priv->lock, flags);
985 oldlines = priv->line_control;
986 if ((cflag & CBAUD) == B0) {
987 /* drop dtr and rts */
988 dbg("%s - dropping the lines, baud rate 0bps", __FUNCTION__);
990 priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
992 baud_mask = (cflag & CBAUD);
994 case B300: dbg("%s - setting baud 300bps", __FUNCTION__); break;
995 case B600: dbg("%s - setting baud 600bps", __FUNCTION__); break;
996 case B1200: dbg("%s - setting baud 1200bps", __FUNCTION__); break;
997 case B2400: dbg("%s - setting baud 2400bps", __FUNCTION__); break;
998 case B4800: dbg("%s - setting baud 4800bps", __FUNCTION__); break;
999 case B9600: dbg("%s - setting baud 9600bps", __FUNCTION__); break;
1000 case B19200: dbg("%s - setting baud 19200bps", __FUNCTION__); break;
1001 case B38400: dbg("%s - setting baud 38400bps", __FUNCTION__); break;
1002 case B57600: dbg("%s - setting baud 57600bps", __FUNCTION__); break;
1003 case B115200: dbg("%s - setting baud 115200bps", __FUNCTION__); break;
1004 default: dbg("%s - unknown masked baud rate", __FUNCTION__);
1006 priv->line_control = (CONTROL_DTR | CONTROL_RTS);
1008 spin_unlock_irqrestore(&priv->lock, flags);
1010 dbg("%s - sending %d stop_bits, %d parity_enable, %d parity_type, %d data_bits (+5)", __FUNCTION__,
1011 stop_bits, parity_enable, parity_type, data_bits);
1013 cypress_serial_control(port, baud_mask, data_bits, stop_bits, parity_enable,
1014 parity_type, 0, CYPRESS_SET_CONFIG);
1016 /* we perform a CYPRESS_GET_CONFIG so that the current settings are filled into the private structure
1017 * this should confirm that all is working if it returns what we just set */
1018 cypress_serial_control(port, 0, 0, 0, 0, 0, 0, CYPRESS_GET_CONFIG);
1020 /* Here we can define custom tty settings for devices
1022 * the main tty termios flag base comes from empeg.c
1025 spin_lock_irqsave(&priv->lock, flags);
1026 if ( (priv->chiptype == CT_EARTHMATE) && (priv->baud_rate == 4800) ) {
1028 dbg("Using custom termios settings for a baud rate of 4800bps.");
1029 /* define custom termios settings for NMEA protocol */
1031 tty->termios->c_iflag /* input modes - */
1032 &= ~(IGNBRK /* disable ignore break */
1033 | BRKINT /* disable break causes interrupt */
1034 | PARMRK /* disable mark parity errors */
1035 | ISTRIP /* disable clear high bit of input characters */
1036 | INLCR /* disable translate NL to CR */
1037 | IGNCR /* disable ignore CR */
1038 | ICRNL /* disable translate CR to NL */
1039 | IXON); /* disable enable XON/XOFF flow control */
1041 tty->termios->c_oflag /* output modes */
1042 &= ~OPOST; /* disable postprocess output characters */
1044 tty->termios->c_lflag /* line discipline modes */
1045 &= ~(ECHO /* disable echo input characters */
1046 | ECHONL /* disable echo new line */
1047 | ICANON /* disable erase, kill, werase, and rprnt special characters */
1048 | ISIG /* disable interrupt, quit, and suspend special characters */
1049 | 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(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 usb_serial_port *port)
1067 struct cypress_private *priv = usb_get_serial_port_data(port);
1069 unsigned long flags;
1071 dbg("%s - port %d", __FUNCTION__, port->number);
1073 spin_lock_irqsave(&priv->lock, flags);
1074 chars = cypress_buf_data_avail(priv->buf);
1075 spin_unlock_irqrestore(&priv->lock, flags);
1077 dbg("%s - returns %d", __FUNCTION__, chars);
1082 static void cypress_throttle (struct usb_serial_port *port)
1084 struct cypress_private *priv = usb_get_serial_port_data(port);
1085 unsigned long flags;
1087 dbg("%s - port %d", __FUNCTION__, port->number);
1089 spin_lock_irqsave(&priv->lock, flags);
1090 priv->rx_flags = THROTTLED;
1091 spin_unlock_irqrestore(&priv->lock, flags);
1095 static void cypress_unthrottle (struct usb_serial_port *port)
1097 struct cypress_private *priv = usb_get_serial_port_data(port);
1098 int actually_throttled, result;
1099 unsigned long flags;
1101 dbg("%s - port %d", __FUNCTION__, port->number);
1103 spin_lock_irqsave(&priv->lock, flags);
1104 actually_throttled = priv->rx_flags & ACTUALLY_THROTTLED;
1106 spin_unlock_irqrestore(&priv->lock, flags);
1108 if (actually_throttled) {
1109 port->interrupt_in_urb->dev = port->serial->dev;
1111 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
1113 dev_err(&port->dev, "%s - failed submitting read urb, error %d\n", __FUNCTION__, result);
1118 static void cypress_read_int_callback(struct urb *urb, struct pt_regs *regs)
1120 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
1121 struct cypress_private *priv = usb_get_serial_port_data(port);
1122 struct tty_struct *tty;
1123 unsigned char *data = urb->transfer_buffer;
1124 unsigned long flags;
1125 char tty_flag = TTY_NORMAL;
1131 dbg("%s - port %d", __FUNCTION__, port->number);
1134 dbg("%s - nonzero read status received: %d", __FUNCTION__, urb->status);
1138 spin_lock_irqsave(&priv->lock, flags);
1139 if (priv->rx_flags & THROTTLED) {
1140 dbg("%s - now throttling", __FUNCTION__);
1141 priv->rx_flags |= ACTUALLY_THROTTLED;
1142 spin_unlock_irqrestore(&priv->lock, flags);
1145 spin_unlock_irqrestore(&priv->lock, flags);
1149 dbg("%s - bad tty pointer - exiting", __FUNCTION__);
1153 spin_lock_irqsave(&priv->lock, flags);
1154 switch(urb->actual_length) {
1156 /* This is for the CY7C64013... */
1157 priv->current_status = data[0] & 0xF8;
1164 /* This is for the CY7C63743... */
1165 priv->current_status = data[0] & 0xF8;
1166 bytes = (data[0] & 0x07)+1;
1172 dbg("%s - wrong packet size - received %d bytes", __FUNCTION__, urb->actual_length);
1173 spin_unlock_irqrestore(&priv->lock, flags);
1176 spin_unlock_irqrestore(&priv->lock, flags);
1178 usb_serial_debug_data (debug, &port->dev, __FUNCTION__, urb->actual_length, data);
1180 spin_lock_irqsave(&priv->lock, flags);
1181 /* check to see if status has changed */
1183 if (priv->current_status != priv->prev_status) {
1184 priv->diff_status |= priv->current_status ^ priv->prev_status;
1185 wake_up_interruptible(&priv->delta_msr_wait);
1186 priv->prev_status = priv->current_status;
1189 spin_unlock_irqrestore(&priv->lock, flags);
1191 /* hangup, as defined in acm.c... this might be a bad place for it though */
1192 if (tty && !(tty->termios->c_cflag & CLOCAL) && !(priv->current_status & UART_CD)) {
1193 dbg("%s - calling hangup", __FUNCTION__);
1198 /* There is one error bit... I'm assuming it is a parity error indicator
1199 * as the generic firmware will set this bit to 1 if a parity error occurs.
1200 * I can not find reference to any other error events.
1203 spin_lock_irqsave(&priv->lock, flags);
1204 if (priv->current_status & CYP_ERROR) {
1205 spin_unlock_irqrestore(&priv->lock, flags);
1206 tty_flag = TTY_PARITY;
1207 dbg("%s - Parity Error detected", __FUNCTION__);
1209 spin_unlock_irqrestore(&priv->lock, flags);
1211 /* process read if there is data other than line status */
1212 if (tty && (bytes > i)) {
1213 for (; i < bytes ; ++i) {
1214 dbg("pushing byte number %d - %d - %c",i,data[i],data[i]);
1215 if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
1216 tty_flip_buffer_push(tty);
1218 tty_insert_flip_char(tty, data[i], tty_flag);
1220 tty_flip_buffer_push(port->tty);
1223 spin_lock_irqsave(&priv->lock, flags);
1224 priv->bytes_in += bytes; /* control and status byte(s) are also counted */
1225 spin_unlock_irqrestore(&priv->lock, flags);
1229 /* Continue trying to always read... unless the port has closed. */
1231 if (port->open_count > 0) {
1232 usb_fill_int_urb(port->interrupt_in_urb, port->serial->dev,
1233 usb_rcvintpipe(port->serial->dev, port->interrupt_in_endpointAddress),
1234 port->interrupt_in_urb->transfer_buffer,
1235 port->interrupt_in_urb->transfer_buffer_length,
1236 cypress_read_int_callback, port,
1238 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
1240 dev_err(&urb->dev->dev, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__, result);
1244 } /* cypress_read_int_callback */
1247 static void cypress_write_int_callback(struct urb *urb, struct pt_regs *regs)
1249 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
1250 struct cypress_private *priv = usb_get_serial_port_data(port);
1253 dbg("%s - port %d", __FUNCTION__, port->number);
1255 switch (urb->status) {
1262 /* this urb is terminated, clean up */
1263 dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status);
1264 priv->write_urb_in_use = 0;
1266 case -EPIPE: /* no break needed */
1267 usb_clear_halt(port->serial->dev, 0x02);
1269 /* error in the urb, so we have to resubmit it */
1270 dbg("%s - Overflow in write", __FUNCTION__);
1271 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
1272 port->interrupt_out_urb->transfer_buffer_length = 1;
1273 port->interrupt_out_urb->dev = port->serial->dev;
1274 result = usb_submit_urb(port->interrupt_out_urb, GFP_ATOMIC);
1276 dev_err(&urb->dev->dev, "%s - failed resubmitting write urb, error %d\n",
1277 __FUNCTION__, result);
1282 priv->write_urb_in_use = 0;
1284 /* send any buffered data */
1289 /*****************************************************************************
1290 * Write buffer functions - buffering code from pl2303 used
1291 *****************************************************************************/
1296 * Allocate a circular buffer and all associated memory.
1299 static struct cypress_buf *cypress_buf_alloc(unsigned int size)
1302 struct cypress_buf *cb;
1308 cb = (struct cypress_buf *)kmalloc(sizeof(struct cypress_buf), GFP_KERNEL);
1312 cb->buf_buf = kmalloc(size, GFP_KERNEL);
1313 if (cb->buf_buf == NULL) {
1318 cb->buf_size = size;
1319 cb->buf_get = cb->buf_put = cb->buf_buf;
1329 * Free the buffer and all associated memory.
1332 static void cypress_buf_free(struct cypress_buf *cb)
1344 * Clear out all data in the circular buffer.
1347 static void cypress_buf_clear(struct cypress_buf *cb)
1350 cb->buf_get = cb->buf_put;
1351 /* equivalent to a get of all data available */
1356 * cypress_buf_data_avail
1358 * Return the number of bytes of data available in the circular
1362 static unsigned int cypress_buf_data_avail(struct cypress_buf *cb)
1365 return ((cb->buf_size + cb->buf_put - cb->buf_get) % cb->buf_size);
1372 * cypress_buf_space_avail
1374 * Return the number of bytes of space available in the circular
1378 static unsigned int cypress_buf_space_avail(struct cypress_buf *cb)
1381 return ((cb->buf_size + cb->buf_get - cb->buf_put - 1) % cb->buf_size);
1390 * Copy data data from a user buffer and put it into the circular buffer.
1391 * Restrict to the amount of space available.
1393 * Return the number of bytes copied.
1396 static unsigned int cypress_buf_put(struct cypress_buf *cb, const char *buf,
1406 len = cypress_buf_space_avail(cb);
1413 len = cb->buf_buf + cb->buf_size - cb->buf_put;
1415 memcpy(cb->buf_put, buf, len);
1416 memcpy(cb->buf_buf, buf+len, count - len);
1417 cb->buf_put = cb->buf_buf + count - len;
1419 memcpy(cb->buf_put, buf, count);
1421 cb->buf_put += count;
1422 else /* count == len */
1423 cb->buf_put = cb->buf_buf;
1434 * Get data from the circular buffer and copy to the given buffer.
1435 * Restrict to the amount of data available.
1437 * Return the number of bytes copied.
1440 static unsigned int cypress_buf_get(struct cypress_buf *cb, char *buf,
1450 len = cypress_buf_data_avail(cb);
1457 len = cb->buf_buf + cb->buf_size - cb->buf_get;
1459 memcpy(buf, cb->buf_get, len);
1460 memcpy(buf+len, cb->buf_buf, count - len);
1461 cb->buf_get = cb->buf_buf + count - len;
1463 memcpy(buf, cb->buf_get, count);
1465 cb->buf_get += count;
1466 else /* count == len */
1467 cb->buf_get = cb->buf_buf;
1474 /*****************************************************************************
1476 *****************************************************************************/
1478 static int __init cypress_init(void)
1482 dbg("%s", __FUNCTION__);
1484 retval = usb_serial_register(&cypress_earthmate_device);
1486 goto failed_em_register;
1487 retval = usb_serial_register(&cypress_hidcom_device);
1489 goto failed_hidcom_register;
1490 retval = usb_register(&cypress_driver);
1492 goto failed_usb_register;
1494 info(DRIVER_DESC " " DRIVER_VERSION);
1496 failed_usb_register:
1497 usb_deregister(&cypress_driver);
1498 failed_hidcom_register:
1499 usb_serial_deregister(&cypress_hidcom_device);
1501 usb_serial_deregister(&cypress_earthmate_device);
1507 static void __exit cypress_exit (void)
1509 dbg("%s", __FUNCTION__);
1511 usb_deregister (&cypress_driver);
1512 usb_serial_deregister (&cypress_earthmate_device);
1513 usb_serial_deregister (&cypress_hidcom_device);
1517 module_init(cypress_init);
1518 module_exit(cypress_exit);
1520 MODULE_AUTHOR( DRIVER_AUTHOR );
1521 MODULE_DESCRIPTION( DRIVER_DESC );
1522 MODULE_VERSION( DRIVER_VERSION );
1523 MODULE_LICENSE("GPL");
1525 module_param(debug, bool, S_IRUGO | S_IWUSR);
1526 MODULE_PARM_DESC(debug, "Debug enabled or not");
1527 module_param(stats, bool, S_IRUGO | S_IWUSR);
1528 MODULE_PARM_DESC(stats, "Enable statistics or not");
1529 module_param(interval, int, S_IRUGO | S_IWUSR);
1530 MODULE_PARM_DESC(interval, "Overrides interrupt interval");