28 #include <linux/kernel.h>
29 #include <linux/errno.h>
31 #include <linux/slab.h>
32 #include <linux/tty.h>
35 #include <linux/module.h>
40 #include <linux/serial.h>
44 #include <asm/unaligned.h>
51 static bool unstable_bauds;
56 #define DRIVER_VERSION "v1.10"
58 #define DRIVER_DESC "Cypress USB to Serial Driver"
61 #define CYPRESS_BUF_SIZE 1024
69 static const struct usb_device_id id_table_cyphidcomrs232[] = {
134 const unsigned char *
buf,
int count);
136 static int cypress_write_room(
struct tty_struct *tty);
137 static int cypress_ioctl(
struct tty_struct *tty,
138 unsigned int cmd,
unsigned long arg);
139 static void cypress_set_termios(
struct tty_struct *tty,
141 static int cypress_tiocmget(
struct tty_struct *tty);
142 static int cypress_tiocmset(
struct tty_struct *tty,
143 unsigned int set,
unsigned int clear);
144 static int cypress_chars_in_buffer(
struct tty_struct *tty);
145 static void cypress_throttle(
struct tty_struct *tty);
146 static void cypress_unthrottle(
struct tty_struct *tty);
148 static void cypress_read_int_callback(
struct urb *
urb);
149 static void cypress_write_int_callback(
struct urb *
urb);
156 .description =
"DeLorme Earthmate USB",
157 .id_table = id_table_earthmate,
159 .port_probe = cypress_earthmate_port_probe,
160 .port_remove = cypress_port_remove,
161 .open = cypress_open,
162 .close = cypress_close,
163 .dtr_rts = cypress_dtr_rts,
164 .write = cypress_write,
165 .write_room = cypress_write_room,
166 .ioctl = cypress_ioctl,
167 .set_termios = cypress_set_termios,
168 .tiocmget = cypress_tiocmget,
169 .tiocmset = cypress_tiocmset,
170 .chars_in_buffer = cypress_chars_in_buffer,
171 .throttle = cypress_throttle,
172 .unthrottle = cypress_unthrottle,
173 .read_int_callback = cypress_read_int_callback,
174 .write_int_callback = cypress_write_int_callback,
182 .description =
"HID->COM RS232 Adapter",
183 .id_table = id_table_cyphidcomrs232,
185 .port_probe = cypress_hidcom_port_probe,
186 .port_remove = cypress_port_remove,
187 .open = cypress_open,
188 .close = cypress_close,
189 .dtr_rts = cypress_dtr_rts,
190 .write = cypress_write,
191 .write_room = cypress_write_room,
192 .ioctl = cypress_ioctl,
193 .set_termios = cypress_set_termios,
194 .tiocmget = cypress_tiocmget,
195 .tiocmset = cypress_tiocmset,
196 .chars_in_buffer = cypress_chars_in_buffer,
197 .throttle = cypress_throttle,
198 .unthrottle = cypress_unthrottle,
199 .read_int_callback = cypress_read_int_callback,
200 .write_int_callback = cypress_write_int_callback,
206 .name =
"nokiaca42v2",
208 .description =
"Nokia CA-42 V2 Adapter",
209 .id_table = id_table_nokiaca42v2,
211 .port_probe = cypress_ca42v2_port_probe,
212 .port_remove = cypress_port_remove,
213 .open = cypress_open,
214 .close = cypress_close,
215 .dtr_rts = cypress_dtr_rts,
216 .write = cypress_write,
217 .write_room = cypress_write_room,
218 .ioctl = cypress_ioctl,
219 .set_termios = cypress_set_termios,
220 .tiocmget = cypress_tiocmget,
221 .tiocmset = cypress_tiocmset,
222 .chars_in_buffer = cypress_chars_in_buffer,
223 .throttle = cypress_throttle,
224 .unthrottle = cypress_unthrottle,
225 .read_int_callback = cypress_read_int_callback,
226 .write_int_callback = cypress_write_int_callback,
230 &cypress_earthmate_device, &cypress_hidcom_device,
231 &cypress_ca42v2_device,
NULL
242 priv = usb_get_serial_port_data(port);
264 if (new_rate > 4800) {
266 "%s - failed setting baud rate, device incapable speed %d\n",
273 if (new_rate <= 600) {
278 "%s - failed setting baud rate, unsupported speed of %d on Earthmate GPS",
291 static int cypress_serial_control(
struct tty_struct *tty,
293 int stop_bits,
int parity_enable,
int parity_type,
int reset,
294 int cypress_request_type)
296 int new_baudrate = 0,
retval = 0, tries = 0;
303 priv = usb_get_serial_port_data(port);
308 feature_buffer = kcalloc(feature_len,
sizeof(
u8),
GFP_KERNEL);
312 switch (cypress_request_type) {
316 if (baud_rate && baud_rate != priv->
baud_rate) {
317 dev_dbg(dev,
"%s - baud rate is changing\n", __func__);
318 retval = analyze_baud_rate(port, baud_rate);
321 dev_dbg(dev,
"%s - New baud rate set to %d\n",
322 __func__, new_baudrate);
325 dev_dbg(dev,
"%s - baud rate is being sent as %d\n", __func__,
330 feature_buffer[4] |= data_bits;
332 feature_buffer[4] |= (stop_bits << 3);
333 feature_buffer[4] |= (parity_enable << 4);
334 feature_buffer[4] |= (parity_type << 5);
336 feature_buffer[4] |= (reset << 7);
338 dev_dbg(dev,
"%s - device is being sent this feature report:\n", __func__);
339 dev_dbg(dev,
"%s - %02X - %02X - %02X - %02X - %02X\n", __func__,
340 feature_buffer[0], feature_buffer[1],
341 feature_buffer[2], feature_buffer[3],
346 usb_sndctrlpipe(port->
serial->dev, 0),
349 0x0300, 0, feature_buffer,
355 }
while (
retval != feature_len &&
358 if (
retval != feature_len) {
359 dev_err(dev,
"%s - failed sending serial line settings - %d\n",
361 cypress_set_dead(port);
366 spin_unlock_irqrestore(&priv->
lock, flags);
370 new_baudrate, new_baudrate);
381 dev_dbg(dev,
"%s - retreiving serial line settings\n", __func__);
384 usb_rcvctrlpipe(port->
serial->dev, 0),
387 0x0300, 0, feature_buffer,
392 }
while (
retval != feature_len
395 if (
retval != feature_len) {
396 dev_err(dev,
"%s - failed to retrieve serial line settings - %d\n",
398 cypress_set_dead(port);
406 spin_unlock_irqrestore(&priv->
lock, flags);
411 spin_unlock_irqrestore(&priv->
lock, flags);
413 kfree(feature_buffer);
425 spin_unlock_irqrestore(&priv->
lock, flags);
429 spin_unlock_irqrestore(&priv->
lock, flags);
431 dev_err(&port->
dev,
"cypress_m8 suspending failing port %d - "
432 "interval might be too short\n", port->
number);
478 dev_dbg(&port->
dev,
"%s - read & write intervals forced to %d\n",
483 dev_dbg(&port->
dev,
"%s - intervals: read=%d write=%d\n",
487 usb_set_serial_port_data(port, priv);
499 ret = cypress_generic_port_probe(port);
501 dev_dbg(&port->
dev,
"%s - Failed setting up port\n", __func__);
505 priv = usb_get_serial_port_data(port);
510 if (serial->
dev->descriptor.idProduct !=
517 "%s - Marking this device as unsafe for GET_CONFIG commands\n",
530 ret = cypress_generic_port_probe(port);
532 dev_dbg(&port->
dev,
"%s - Failed setting up port\n", __func__);
536 priv = usb_get_serial_port_data(port);
547 ret = cypress_generic_port_probe(port);
549 dev_dbg(&port->
dev,
"%s - Failed setting up port\n", __func__);
553 priv = usb_get_serial_port_data(port);
563 priv = usb_get_serial_port_data(port);
591 spin_unlock_irqrestore(&priv->
lock, flags);
597 cypress_set_termios(tty, port, &priv->
tmp_termios);
601 dev_err(&port->
dev,
"%s - interrupt_in_urb is empty!\n",
615 "%s - failed submitting read urb, error %d\n",
617 cypress_set_dead(port);
619 port->
port.drain_delay = 256;
627 spin_lock_irq(&priv->
lock);
633 spin_unlock_irq(&priv->
lock);
644 if (port->
serial->disconnected) {
650 spin_unlock_irqrestore(&priv->
lock, flags);
652 dev_dbg(&port->
dev,
"%s - stopping urbs\n", __func__);
657 dev_info(&port->
dev,
"Statistics: %d Bytes In | %d Bytes Out | %d Commands Issued\n",
664 const unsigned char *
buf,
int count)
668 dev_dbg(&port->
dev,
"%s - port %d, %d bytes\n", __func__, port->
number, count);
700 dev_dbg(dev,
"%s - interrupt out size is %d\n", __func__,
705 dev_dbg(dev,
"%s - can't write, urb in use\n", __func__);
706 spin_unlock_irqrestore(&priv->
lock, flags);
709 spin_unlock_irqrestore(&priv->
lock, flags);
735 dev_dbg(dev,
"%s - line control command being issued\n", __func__);
736 spin_unlock_irqrestore(&priv->
lock, flags);
739 spin_unlock_irqrestore(&priv->
lock, flags);
757 dev_dbg(dev,
"%s - count is %d\n", __func__, count);
762 spin_unlock_irqrestore(&priv->
lock, flags);
767 actual_size = count +
780 "%s - failed submitting write urb, error %d\n",
783 cypress_set_dead(port);
792 spin_unlock_irqrestore(&priv->
lock, flags);
799 static int cypress_write_room(
struct tty_struct *tty)
808 spin_unlock_irqrestore(&priv->
lock, flags);
810 dev_dbg(&port->
dev,
"%s - returns %d\n", __func__, room);
815 static int cypress_tiocmget(
struct tty_struct *tty)
820 unsigned int result = 0;
826 spin_unlock_irqrestore(&priv->
lock, flags);
835 dev_dbg(&port->
dev,
"%s - result = %x\n", __func__, result);
841 static int cypress_tiocmset(
struct tty_struct *tty,
842 unsigned int set,
unsigned int clear)
858 spin_unlock_irqrestore(&priv->
lock, flags);
860 return cypress_write(tty, port,
NULL, 0);
864 static int cypress_ioctl(
struct tty_struct *tty,
865 unsigned int cmd,
unsigned long arg)
870 dev_dbg(&port->
dev,
"%s - port %d, cmd 0x%.4x\n", __func__, port->
number, cmd);
875 while (priv !=
NULL) {
905 dev_dbg(&port->
dev,
"%s - arg not supported - it was 0x%04x - check include/asm/ioctls.h\n", __func__, cmd);
910 static void cypress_set_termios(
struct tty_struct *tty,
915 int data_bits,
stop_bits, parity_type, parity_enable;
916 unsigned cflag, iflag;
929 tty->termios.c_ispeed = 4800;
930 tty->termios.c_ospeed = 4800;
935 tty->termios.c_ispeed = 9600;
936 tty->termios.c_ospeed = 9600;
941 tty->termios.c_ispeed = 9600;
942 tty->termios.c_ospeed = 9600;
946 spin_unlock_irqrestore(&priv->
lock, flags);
951 cflag = tty->termios.c_cflag;
952 iflag = tty->termios.c_iflag;
958 spin_unlock_irqrestore(&priv->
lock, flags);
965 stop_bits = cflag &
CSTOPB ? 1 : 0;
970 parity_type = cflag &
PARODD ? 1 : 0;
972 parity_enable = parity_type = 0;
974 switch (cflag &
CSIZE) {
988 dev_err(dev,
"%s - CSIZE was set, but not CS5-CS8\n", __func__);
995 dev_dbg(dev,
"%s - dropping the lines, baud rate 0bps\n", __func__);
999 spin_unlock_irqrestore(&priv->
lock, flags);
1001 dev_dbg(dev,
"%s - sending %d stop_bits, %d parity_enable, %d parity_type, %d data_bits (+5)\n",
1002 __func__, stop_bits, parity_enable, parity_type, data_bits);
1005 data_bits, stop_bits,
1006 parity_enable, parity_type,
1019 dev_dbg(dev,
"Using custom termios settings for a baud rate of 4800bps.\n");
1022 tty->termios.c_iflag
1032 tty->termios.c_oflag
1035 tty->termios.c_lflag
1046 spin_unlock_irqrestore(&priv->
lock, flags);
1051 cypress_write(tty, port,
NULL, 0);
1057 static int cypress_chars_in_buffer(
struct tty_struct *tty)
1062 unsigned long flags;
1066 spin_unlock_irqrestore(&priv->
lock, flags);
1068 dev_dbg(&port->
dev,
"%s - returns %d\n", __func__, chars);
1073 static void cypress_throttle(
struct tty_struct *tty)
1078 spin_lock_irq(&priv->
lock);
1080 spin_unlock_irq(&priv->
lock);
1084 static void cypress_unthrottle(
struct tty_struct *tty)
1088 int actually_throttled,
result;
1090 spin_lock_irq(&priv->
lock);
1093 spin_unlock_irq(&priv->
lock);
1098 if (actually_throttled) {
1101 dev_err(&port->
dev,
"%s - failed submitting read urb, "
1102 "error %d\n", __func__, result);
1103 cypress_set_dead(port);
1109 static void cypress_read_int_callback(
struct urb *
urb)
1113 struct device *dev = &urb->dev->dev;
1115 unsigned char *
data = urb->transfer_buffer;
1116 unsigned long flags;
1122 int status = urb->status;
1137 dev_err(dev,
"%s - unexpected nonzero read status received: %d\n",
1139 cypress_set_dead(port);
1145 dev_dbg(dev,
"%s - now throttling\n", __func__);
1147 spin_unlock_irqrestore(&priv->
lock, flags);
1150 spin_unlock_irqrestore(&priv->
lock, flags);
1154 dev_dbg(dev,
"%s - bad tty pointer - exiting\n", __func__);
1159 result = urb->actual_length;
1165 bytes = data[1] + 2;
1173 bytes = (data[0] & 0x07) + 1;
1179 spin_unlock_irqrestore(&priv->
lock, flags);
1180 if (result < bytes) {
1182 "%s - wrong packet size - received %d bytes but packet said %d bytes\n",
1183 __func__, result, bytes);
1187 usb_serial_debug_data(&port->
dev, __func__, urb->actual_length, data);
1197 spin_unlock_irqrestore(&priv->
lock, flags);
1201 if (tty && !(tty->termios.c_cflag &
CLOCAL) &&
1203 dev_dbg(dev,
"%s - calling hangup\n", __func__);
1214 spin_unlock_irqrestore(&priv->
lock, flags);
1216 dev_dbg(dev,
"%s - Parity Error detected\n", __func__);
1218 spin_unlock_irqrestore(&priv->
lock, flags);
1221 if (tty && bytes > i) {
1223 tty_flag, bytes - i);
1230 spin_unlock_irqrestore(&priv->
lock, flags);
1239 usb_rcvintpipe(port->
serial->dev,
1243 cypress_read_int_callback, port,
1246 if (result && result != -
EPERM) {
1247 dev_err(dev,
"%s - failed resubmitting read urb, error %d\n",
1249 cypress_set_dead(port);
1255 static void cypress_write_int_callback(
struct urb *urb)
1259 struct device *dev = &urb->dev->dev;
1261 int status = urb->status;
1271 dev_dbg(dev,
"%s - urb shutting down with status: %d\n",
1280 dev_dbg(dev,
"%s - nonzero write bulk status received: %d\n",
1286 dev_err(dev,
"%s - failed resubmitting write urb, error %d\n",
1288 cypress_set_dead(port);
1291 dev_err(dev,
"%s - unexpected nonzero write status received: %d\n",
1293 cypress_set_dead(port);