• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * USB Cypress M8 driver
4  *
5  * 	Copyright (C) 2004
6  * 	    Lonnie Mendez (dignome@gmail.com)
7  *	Copyright (C) 2003,2004
8  *	    Neil Whelchel (koyama@firstlight.net)
9  *
10  * See Documentation/usb/usb-serial.txt for more information on using this
11  * driver
12  *
13  * See http://geocities.com/i0xox0i for information on this driver and the
14  * earthmate usb device.
15  */
16 
17 /* Thanks to Neil Whelchel for writing the first cypress m8 implementation
18    for linux. */
19 /* Thanks to cypress for providing references for the hid reports. */
20 /* Thanks to Jiang Zhang for providing links and for general help. */
21 /* Code originates and was built up from ftdi_sio, belkin, pl2303 and others.*/
22 
23 
24 #include <linux/kernel.h>
25 #include <linux/errno.h>
26 #include <linux/slab.h>
27 #include <linux/tty.h>
28 #include <linux/tty_driver.h>
29 #include <linux/tty_flip.h>
30 #include <linux/module.h>
31 #include <linux/moduleparam.h>
32 #include <linux/spinlock.h>
33 #include <linux/usb.h>
34 #include <linux/usb/serial.h>
35 #include <linux/serial.h>
36 #include <linux/kfifo.h>
37 #include <linux/delay.h>
38 #include <linux/uaccess.h>
39 #include <asm/unaligned.h>
40 
41 #include "cypress_m8.h"
42 
43 
44 static bool stats;
45 static int interval;
46 static bool unstable_bauds;
47 
48 #define DRIVER_AUTHOR "Lonnie Mendez <dignome@gmail.com>, Neil Whelchel <koyama@firstlight.net>"
49 #define DRIVER_DESC "Cypress USB to Serial Driver"
50 
51 /* write buffer size defines */
52 #define CYPRESS_BUF_SIZE	1024
53 
54 static const struct usb_device_id id_table_earthmate[] = {
55 	{ USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB) },
56 	{ USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB_LT20) },
57 	{ }						/* Terminating entry */
58 };
59 
60 static const struct usb_device_id id_table_cyphidcomrs232[] = {
61 	{ USB_DEVICE(VENDOR_ID_CYPRESS, PRODUCT_ID_CYPHIDCOM) },
62 	{ USB_DEVICE(VENDOR_ID_SAI, PRODUCT_ID_CYPHIDCOM) },
63 	{ USB_DEVICE(VENDOR_ID_POWERCOM, PRODUCT_ID_UPS) },
64 	{ USB_DEVICE(VENDOR_ID_FRWD, PRODUCT_ID_CYPHIDCOM_FRWD) },
65 	{ }						/* Terminating entry */
66 };
67 
68 static const struct usb_device_id id_table_nokiaca42v2[] = {
69 	{ USB_DEVICE(VENDOR_ID_DAZZLE, PRODUCT_ID_CA42) },
70 	{ }						/* Terminating entry */
71 };
72 
73 static const struct usb_device_id id_table_combined[] = {
74 	{ USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB) },
75 	{ USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB_LT20) },
76 	{ USB_DEVICE(VENDOR_ID_CYPRESS, PRODUCT_ID_CYPHIDCOM) },
77 	{ USB_DEVICE(VENDOR_ID_SAI, PRODUCT_ID_CYPHIDCOM) },
78 	{ USB_DEVICE(VENDOR_ID_POWERCOM, PRODUCT_ID_UPS) },
79 	{ USB_DEVICE(VENDOR_ID_FRWD, PRODUCT_ID_CYPHIDCOM_FRWD) },
80 	{ USB_DEVICE(VENDOR_ID_DAZZLE, PRODUCT_ID_CA42) },
81 	{ }						/* Terminating entry */
82 };
83 
84 MODULE_DEVICE_TABLE(usb, id_table_combined);
85 
86 enum packet_format {
87 	packet_format_1,  /* b0:status, b1:payload count */
88 	packet_format_2   /* b0[7:3]:status, b0[2:0]:payload count */
89 };
90 
91 struct cypress_private {
92 	spinlock_t lock;		   /* private lock */
93 	int chiptype;			   /* identifier of device, for quirks/etc */
94 	int bytes_in;			   /* used for statistics */
95 	int bytes_out;			   /* used for statistics */
96 	int cmd_count;			   /* used for statistics */
97 	int cmd_ctrl;			   /* always set this to 1 before issuing a command */
98 	struct kfifo write_fifo;	   /* write fifo */
99 	int write_urb_in_use;		   /* write urb in use indicator */
100 	int write_urb_interval;            /* interval to use for write urb */
101 	int read_urb_interval;             /* interval to use for read urb */
102 	int comm_is_ok;                    /* true if communication is (still) ok */
103 	int termios_initialized;
104 	__u8 line_control;	   	   /* holds dtr / rts value */
105 	__u8 current_status;	   	   /* received from last read - info on dsr,cts,cd,ri,etc */
106 	__u8 current_config;	   	   /* stores the current configuration byte */
107 	__u8 rx_flags;			   /* throttling - used from whiteheat/ftdi_sio */
108 	enum packet_format pkt_fmt;	   /* format to use for packet send / receive */
109 	int get_cfg_unsafe;		   /* If true, the CYPRESS_GET_CONFIG is unsafe */
110 	int baud_rate;			   /* stores current baud rate in
111 					      integer form */
112 	int isthrottled;		   /* if throttled, discard reads */
113 	char prev_status;		   /* used for TIOCMIWAIT */
114 	/* we pass a pointer to this as the argument sent to
115 	   cypress_set_termios old_termios */
116 	struct ktermios tmp_termios; 	   /* stores the old termios settings */
117 };
118 
119 /* function prototypes for the Cypress USB to serial device */
120 static int  cypress_earthmate_port_probe(struct usb_serial_port *port);
121 static int  cypress_hidcom_port_probe(struct usb_serial_port *port);
122 static int  cypress_ca42v2_port_probe(struct usb_serial_port *port);
123 static int  cypress_port_remove(struct usb_serial_port *port);
124 static int  cypress_open(struct tty_struct *tty, struct usb_serial_port *port);
125 static void cypress_close(struct usb_serial_port *port);
126 static void cypress_dtr_rts(struct usb_serial_port *port, int on);
127 static int  cypress_write(struct tty_struct *tty, struct usb_serial_port *port,
128 			const unsigned char *buf, int count);
129 static void cypress_send(struct usb_serial_port *port);
130 static int  cypress_write_room(struct tty_struct *tty);
131 static void cypress_set_termios(struct tty_struct *tty,
132 			struct usb_serial_port *port, struct ktermios *old);
133 static int  cypress_tiocmget(struct tty_struct *tty);
134 static int  cypress_tiocmset(struct tty_struct *tty,
135 			unsigned int set, unsigned int clear);
136 static int  cypress_chars_in_buffer(struct tty_struct *tty);
137 static void cypress_throttle(struct tty_struct *tty);
138 static void cypress_unthrottle(struct tty_struct *tty);
139 static void cypress_set_dead(struct usb_serial_port *port);
140 static void cypress_read_int_callback(struct urb *urb);
141 static void cypress_write_int_callback(struct urb *urb);
142 
143 static struct usb_serial_driver cypress_earthmate_device = {
144 	.driver = {
145 		.owner =		THIS_MODULE,
146 		.name =			"earthmate",
147 	},
148 	.description =			"DeLorme Earthmate USB",
149 	.id_table =			id_table_earthmate,
150 	.num_ports =			1,
151 	.port_probe =			cypress_earthmate_port_probe,
152 	.port_remove =			cypress_port_remove,
153 	.open =				cypress_open,
154 	.close =			cypress_close,
155 	.dtr_rts =			cypress_dtr_rts,
156 	.write =			cypress_write,
157 	.write_room =			cypress_write_room,
158 	.set_termios =			cypress_set_termios,
159 	.tiocmget =			cypress_tiocmget,
160 	.tiocmset =			cypress_tiocmset,
161 	.tiocmiwait =			usb_serial_generic_tiocmiwait,
162 	.chars_in_buffer =		cypress_chars_in_buffer,
163 	.throttle =		 	cypress_throttle,
164 	.unthrottle =			cypress_unthrottle,
165 	.read_int_callback =		cypress_read_int_callback,
166 	.write_int_callback =		cypress_write_int_callback,
167 };
168 
169 static struct usb_serial_driver cypress_hidcom_device = {
170 	.driver = {
171 		.owner =		THIS_MODULE,
172 		.name =			"cyphidcom",
173 	},
174 	.description =			"HID->COM RS232 Adapter",
175 	.id_table =			id_table_cyphidcomrs232,
176 	.num_ports =			1,
177 	.port_probe =			cypress_hidcom_port_probe,
178 	.port_remove =			cypress_port_remove,
179 	.open =				cypress_open,
180 	.close =			cypress_close,
181 	.dtr_rts =			cypress_dtr_rts,
182 	.write =			cypress_write,
183 	.write_room =			cypress_write_room,
184 	.set_termios =			cypress_set_termios,
185 	.tiocmget =			cypress_tiocmget,
186 	.tiocmset =			cypress_tiocmset,
187 	.tiocmiwait =			usb_serial_generic_tiocmiwait,
188 	.chars_in_buffer =		cypress_chars_in_buffer,
189 	.throttle =			cypress_throttle,
190 	.unthrottle =			cypress_unthrottle,
191 	.read_int_callback =		cypress_read_int_callback,
192 	.write_int_callback =		cypress_write_int_callback,
193 };
194 
195 static struct usb_serial_driver cypress_ca42v2_device = {
196 	.driver = {
197 		.owner =		THIS_MODULE,
198 		.name =			"nokiaca42v2",
199 	},
200 	.description =			"Nokia CA-42 V2 Adapter",
201 	.id_table =			id_table_nokiaca42v2,
202 	.num_ports =			1,
203 	.port_probe =			cypress_ca42v2_port_probe,
204 	.port_remove =			cypress_port_remove,
205 	.open =				cypress_open,
206 	.close =			cypress_close,
207 	.dtr_rts =			cypress_dtr_rts,
208 	.write =			cypress_write,
209 	.write_room =			cypress_write_room,
210 	.set_termios =			cypress_set_termios,
211 	.tiocmget =			cypress_tiocmget,
212 	.tiocmset =			cypress_tiocmset,
213 	.tiocmiwait =			usb_serial_generic_tiocmiwait,
214 	.chars_in_buffer =		cypress_chars_in_buffer,
215 	.throttle =			cypress_throttle,
216 	.unthrottle =			cypress_unthrottle,
217 	.read_int_callback =		cypress_read_int_callback,
218 	.write_int_callback =		cypress_write_int_callback,
219 };
220 
221 static struct usb_serial_driver * const serial_drivers[] = {
222 	&cypress_earthmate_device, &cypress_hidcom_device,
223 	&cypress_ca42v2_device, NULL
224 };
225 
226 /*****************************************************************************
227  * Cypress serial helper functions
228  *****************************************************************************/
229 
230 /* FRWD Dongle hidcom needs to skip reset and speed checks */
is_frwd(struct usb_device * dev)231 static inline bool is_frwd(struct usb_device *dev)
232 {
233 	return ((le16_to_cpu(dev->descriptor.idVendor) == VENDOR_ID_FRWD) &&
234 		(le16_to_cpu(dev->descriptor.idProduct) == PRODUCT_ID_CYPHIDCOM_FRWD));
235 }
236 
analyze_baud_rate(struct usb_serial_port * port,speed_t new_rate)237 static int analyze_baud_rate(struct usb_serial_port *port, speed_t new_rate)
238 {
239 	struct cypress_private *priv;
240 	priv = usb_get_serial_port_data(port);
241 
242 	if (unstable_bauds)
243 		return new_rate;
244 
245 	/* FRWD Dongle uses 115200 bps */
246 	if (is_frwd(port->serial->dev))
247 		return new_rate;
248 
249 	/*
250 	 * The general purpose firmware for the Cypress M8 allows for
251 	 * a maximum speed of 57600bps (I have no idea whether DeLorme
252 	 * chose to use the general purpose firmware or not), if you
253 	 * need to modify this speed setting for your own project
254 	 * please add your own chiptype and modify the code likewise.
255 	 * The Cypress HID->COM device will work successfully up to
256 	 * 115200bps (but the actual throughput is around 3kBps).
257 	 */
258 	if (port->serial->dev->speed == USB_SPEED_LOW) {
259 		/*
260 		 * Mike Isely <isely@pobox.com> 2-Feb-2008: The
261 		 * Cypress app note that describes this mechanism
262 		 * states the the low-speed part can't handle more
263 		 * than 800 bytes/sec, in which case 4800 baud is the
264 		 * safest speed for a part like that.
265 		 */
266 		if (new_rate > 4800) {
267 			dev_dbg(&port->dev,
268 				"%s - failed setting baud rate, device incapable speed %d\n",
269 				__func__, new_rate);
270 			return -1;
271 		}
272 	}
273 	switch (priv->chiptype) {
274 	case CT_EARTHMATE:
275 		if (new_rate <= 600) {
276 			/* 300 and 600 baud rates are supported under
277 			 * the generic firmware, but are not used with
278 			 * NMEA and SiRF protocols */
279 			dev_dbg(&port->dev,
280 				"%s - failed setting baud rate, unsupported speed of %d on Earthmate GPS\n",
281 				__func__, new_rate);
282 			return -1;
283 		}
284 		break;
285 	default:
286 		break;
287 	}
288 	return new_rate;
289 }
290 
291 
292 /* This function can either set or retrieve the current serial line settings */
cypress_serial_control(struct tty_struct * tty,struct usb_serial_port * port,speed_t baud_rate,int data_bits,int stop_bits,int parity_enable,int parity_type,int reset,int cypress_request_type)293 static int cypress_serial_control(struct tty_struct *tty,
294 	struct usb_serial_port *port, speed_t baud_rate, int data_bits,
295 	int stop_bits, int parity_enable, int parity_type, int reset,
296 	int cypress_request_type)
297 {
298 	int new_baudrate = 0, retval = 0, tries = 0;
299 	struct cypress_private *priv;
300 	struct device *dev = &port->dev;
301 	u8 *feature_buffer;
302 	const unsigned int feature_len = 5;
303 	unsigned long flags;
304 
305 	priv = usb_get_serial_port_data(port);
306 
307 	if (!priv->comm_is_ok)
308 		return -ENODEV;
309 
310 	feature_buffer = kcalloc(feature_len, sizeof(u8), GFP_KERNEL);
311 	if (!feature_buffer)
312 		return -ENOMEM;
313 
314 	switch (cypress_request_type) {
315 	case CYPRESS_SET_CONFIG:
316 		/* 0 means 'Hang up' so doesn't change the true bit rate */
317 		new_baudrate = priv->baud_rate;
318 		if (baud_rate && baud_rate != priv->baud_rate) {
319 			dev_dbg(dev, "%s - baud rate is changing\n", __func__);
320 			retval = analyze_baud_rate(port, baud_rate);
321 			if (retval >= 0) {
322 				new_baudrate = retval;
323 				dev_dbg(dev, "%s - New baud rate set to %d\n",
324 					__func__, new_baudrate);
325 			}
326 		}
327 		dev_dbg(dev, "%s - baud rate is being sent as %d\n", __func__,
328 			new_baudrate);
329 
330 		/* fill the feature_buffer with new configuration */
331 		put_unaligned_le32(new_baudrate, feature_buffer);
332 		feature_buffer[4] |= data_bits;   /* assign data bits in 2 bit space ( max 3 ) */
333 		/* 1 bit gap */
334 		feature_buffer[4] |= (stop_bits << 3);   /* assign stop bits in 1 bit space */
335 		feature_buffer[4] |= (parity_enable << 4);   /* assign parity flag in 1 bit space */
336 		feature_buffer[4] |= (parity_type << 5);   /* assign parity type in 1 bit space */
337 		/* 1 bit gap */
338 		feature_buffer[4] |= (reset << 7);   /* assign reset at end of byte, 1 bit space */
339 
340 		dev_dbg(dev, "%s - device is being sent this feature report:\n", __func__);
341 		dev_dbg(dev, "%s - %02X - %02X - %02X - %02X - %02X\n", __func__,
342 			feature_buffer[0], feature_buffer[1],
343 			feature_buffer[2], feature_buffer[3],
344 			feature_buffer[4]);
345 
346 		do {
347 			retval = usb_control_msg(port->serial->dev,
348 					usb_sndctrlpipe(port->serial->dev, 0),
349 					HID_REQ_SET_REPORT,
350 					USB_DIR_OUT | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
351 					0x0300, 0, feature_buffer,
352 					feature_len, 500);
353 
354 			if (tries++ >= 3)
355 				break;
356 
357 		} while (retval != feature_len &&
358 			 retval != -ENODEV);
359 
360 		if (retval != feature_len) {
361 			dev_err(dev, "%s - failed sending serial line settings - %d\n",
362 				__func__, retval);
363 			cypress_set_dead(port);
364 		} else {
365 			spin_lock_irqsave(&priv->lock, flags);
366 			priv->baud_rate = new_baudrate;
367 			priv->current_config = feature_buffer[4];
368 			spin_unlock_irqrestore(&priv->lock, flags);
369 			/* If we asked for a speed change encode it */
370 			if (baud_rate)
371 				tty_encode_baud_rate(tty,
372 					new_baudrate, new_baudrate);
373 		}
374 	break;
375 	case CYPRESS_GET_CONFIG:
376 		if (priv->get_cfg_unsafe) {
377 			/* Not implemented for this device,
378 			   and if we try to do it we're likely
379 			   to crash the hardware. */
380 			retval = -ENOTTY;
381 			goto out;
382 		}
383 		dev_dbg(dev, "%s - retreiving serial line settings\n", __func__);
384 		do {
385 			retval = usb_control_msg(port->serial->dev,
386 					usb_rcvctrlpipe(port->serial->dev, 0),
387 					HID_REQ_GET_REPORT,
388 					USB_DIR_IN | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
389 					0x0300, 0, feature_buffer,
390 					feature_len, 500);
391 
392 			if (tries++ >= 3)
393 				break;
394 		} while (retval != feature_len
395 						&& retval != -ENODEV);
396 
397 		if (retval != feature_len) {
398 			dev_err(dev, "%s - failed to retrieve serial line settings - %d\n",
399 				__func__, retval);
400 			cypress_set_dead(port);
401 			goto out;
402 		} else {
403 			spin_lock_irqsave(&priv->lock, flags);
404 			/* store the config in one byte, and later
405 			   use bit masks to check values */
406 			priv->current_config = feature_buffer[4];
407 			priv->baud_rate = get_unaligned_le32(feature_buffer);
408 			spin_unlock_irqrestore(&priv->lock, flags);
409 		}
410 	}
411 	spin_lock_irqsave(&priv->lock, flags);
412 	++priv->cmd_count;
413 	spin_unlock_irqrestore(&priv->lock, flags);
414 out:
415 	kfree(feature_buffer);
416 	return retval;
417 } /* cypress_serial_control */
418 
419 
cypress_set_dead(struct usb_serial_port * port)420 static void cypress_set_dead(struct usb_serial_port *port)
421 {
422 	struct cypress_private *priv = usb_get_serial_port_data(port);
423 	unsigned long flags;
424 
425 	spin_lock_irqsave(&priv->lock, flags);
426 	if (!priv->comm_is_ok) {
427 		spin_unlock_irqrestore(&priv->lock, flags);
428 		return;
429 	}
430 	priv->comm_is_ok = 0;
431 	spin_unlock_irqrestore(&priv->lock, flags);
432 
433 	dev_err(&port->dev, "cypress_m8 suspending failing port %d - "
434 		"interval might be too short\n", port->port_number);
435 }
436 
437 
438 /*****************************************************************************
439  * Cypress serial driver functions
440  *****************************************************************************/
441 
442 
cypress_generic_port_probe(struct usb_serial_port * port)443 static int cypress_generic_port_probe(struct usb_serial_port *port)
444 {
445 	struct usb_serial *serial = port->serial;
446 	struct cypress_private *priv;
447 
448 	if (!port->interrupt_out_urb || !port->interrupt_in_urb) {
449 		dev_err(&port->dev, "required endpoint is missing\n");
450 		return -ENODEV;
451 	}
452 
453 	priv = kzalloc(sizeof(struct cypress_private), GFP_KERNEL);
454 	if (!priv)
455 		return -ENOMEM;
456 
457 	priv->comm_is_ok = !0;
458 	spin_lock_init(&priv->lock);
459 	if (kfifo_alloc(&priv->write_fifo, CYPRESS_BUF_SIZE, GFP_KERNEL)) {
460 		kfree(priv);
461 		return -ENOMEM;
462 	}
463 
464 	/* Skip reset for FRWD device. It is a workaound:
465 	   device hangs if it receives SET_CONFIGURE in Configured
466 	   state. */
467 	if (!is_frwd(serial->dev))
468 		usb_reset_configuration(serial->dev);
469 
470 	priv->cmd_ctrl = 0;
471 	priv->line_control = 0;
472 	priv->termios_initialized = 0;
473 	priv->rx_flags = 0;
474 	/* Default packet format setting is determined by packet size.
475 	   Anything with a size larger then 9 must have a separate
476 	   count field since the 3 bit count field is otherwise too
477 	   small.  Otherwise we can use the slightly more compact
478 	   format.  This is in accordance with the cypress_m8 serial
479 	   converter app note. */
480 	if (port->interrupt_out_size > 9)
481 		priv->pkt_fmt = packet_format_1;
482 	else
483 		priv->pkt_fmt = packet_format_2;
484 
485 	if (interval > 0) {
486 		priv->write_urb_interval = interval;
487 		priv->read_urb_interval = interval;
488 		dev_dbg(&port->dev, "%s - read & write intervals forced to %d\n",
489 			__func__, interval);
490 	} else {
491 		priv->write_urb_interval = port->interrupt_out_urb->interval;
492 		priv->read_urb_interval = port->interrupt_in_urb->interval;
493 		dev_dbg(&port->dev, "%s - intervals: read=%d write=%d\n",
494 			__func__, priv->read_urb_interval,
495 			priv->write_urb_interval);
496 	}
497 	usb_set_serial_port_data(port, priv);
498 
499 	port->port.drain_delay = 256;
500 
501 	return 0;
502 }
503 
504 
cypress_earthmate_port_probe(struct usb_serial_port * port)505 static int cypress_earthmate_port_probe(struct usb_serial_port *port)
506 {
507 	struct usb_serial *serial = port->serial;
508 	struct cypress_private *priv;
509 	int ret;
510 
511 	ret = cypress_generic_port_probe(port);
512 	if (ret) {
513 		dev_dbg(&port->dev, "%s - Failed setting up port\n", __func__);
514 		return ret;
515 	}
516 
517 	priv = usb_get_serial_port_data(port);
518 	priv->chiptype = CT_EARTHMATE;
519 	/* All Earthmate devices use the separated-count packet
520 	   format!  Idiotic. */
521 	priv->pkt_fmt = packet_format_1;
522 	if (serial->dev->descriptor.idProduct !=
523 				cpu_to_le16(PRODUCT_ID_EARTHMATEUSB)) {
524 		/* The old original USB Earthmate seemed able to
525 		   handle GET_CONFIG requests; everything they've
526 		   produced since that time crashes if this command is
527 		   attempted :-( */
528 		dev_dbg(&port->dev,
529 			"%s - Marking this device as unsafe for GET_CONFIG commands\n",
530 			__func__);
531 		priv->get_cfg_unsafe = !0;
532 	}
533 
534 	return 0;
535 }
536 
cypress_hidcom_port_probe(struct usb_serial_port * port)537 static int cypress_hidcom_port_probe(struct usb_serial_port *port)
538 {
539 	struct cypress_private *priv;
540 	int ret;
541 
542 	ret = cypress_generic_port_probe(port);
543 	if (ret) {
544 		dev_dbg(&port->dev, "%s - Failed setting up port\n", __func__);
545 		return ret;
546 	}
547 
548 	priv = usb_get_serial_port_data(port);
549 	priv->chiptype = CT_CYPHIDCOM;
550 
551 	return 0;
552 }
553 
cypress_ca42v2_port_probe(struct usb_serial_port * port)554 static int cypress_ca42v2_port_probe(struct usb_serial_port *port)
555 {
556 	struct cypress_private *priv;
557 	int ret;
558 
559 	ret = cypress_generic_port_probe(port);
560 	if (ret) {
561 		dev_dbg(&port->dev, "%s - Failed setting up port\n", __func__);
562 		return ret;
563 	}
564 
565 	priv = usb_get_serial_port_data(port);
566 	priv->chiptype = CT_CA42V2;
567 
568 	return 0;
569 }
570 
cypress_port_remove(struct usb_serial_port * port)571 static int cypress_port_remove(struct usb_serial_port *port)
572 {
573 	struct cypress_private *priv;
574 
575 	priv = usb_get_serial_port_data(port);
576 
577 	kfifo_free(&priv->write_fifo);
578 	kfree(priv);
579 
580 	return 0;
581 }
582 
cypress_open(struct tty_struct * tty,struct usb_serial_port * port)583 static int cypress_open(struct tty_struct *tty, struct usb_serial_port *port)
584 {
585 	struct cypress_private *priv = usb_get_serial_port_data(port);
586 	struct usb_serial *serial = port->serial;
587 	unsigned long flags;
588 	int result = 0;
589 
590 	if (!priv->comm_is_ok)
591 		return -EIO;
592 
593 	/* clear halts before open */
594 	usb_clear_halt(serial->dev, 0x81);
595 	usb_clear_halt(serial->dev, 0x02);
596 
597 	spin_lock_irqsave(&priv->lock, flags);
598 	/* reset read/write statistics */
599 	priv->bytes_in = 0;
600 	priv->bytes_out = 0;
601 	priv->cmd_count = 0;
602 	priv->rx_flags = 0;
603 	spin_unlock_irqrestore(&priv->lock, flags);
604 
605 	/* Set termios */
606 	cypress_send(port);
607 
608 	if (tty)
609 		cypress_set_termios(tty, port, &priv->tmp_termios);
610 
611 	/* setup the port and start reading from the device */
612 	usb_fill_int_urb(port->interrupt_in_urb, serial->dev,
613 		usb_rcvintpipe(serial->dev, port->interrupt_in_endpointAddress),
614 		port->interrupt_in_urb->transfer_buffer,
615 		port->interrupt_in_urb->transfer_buffer_length,
616 		cypress_read_int_callback, port, priv->read_urb_interval);
617 	result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
618 
619 	if (result) {
620 		dev_err(&port->dev,
621 			"%s - failed submitting read urb, error %d\n",
622 							__func__, result);
623 		cypress_set_dead(port);
624 	}
625 
626 	return result;
627 } /* cypress_open */
628 
cypress_dtr_rts(struct usb_serial_port * port,int on)629 static void cypress_dtr_rts(struct usb_serial_port *port, int on)
630 {
631 	struct cypress_private *priv = usb_get_serial_port_data(port);
632 	/* drop dtr and rts */
633 	spin_lock_irq(&priv->lock);
634 	if (on == 0)
635 		priv->line_control = 0;
636 	else
637 		priv->line_control = CONTROL_DTR | CONTROL_RTS;
638 	priv->cmd_ctrl = 1;
639 	spin_unlock_irq(&priv->lock);
640 	cypress_write(NULL, port, NULL, 0);
641 }
642 
cypress_close(struct usb_serial_port * port)643 static void cypress_close(struct usb_serial_port *port)
644 {
645 	struct cypress_private *priv = usb_get_serial_port_data(port);
646 	unsigned long flags;
647 
648 	spin_lock_irqsave(&priv->lock, flags);
649 	kfifo_reset_out(&priv->write_fifo);
650 	spin_unlock_irqrestore(&priv->lock, flags);
651 
652 	dev_dbg(&port->dev, "%s - stopping urbs\n", __func__);
653 	usb_kill_urb(port->interrupt_in_urb);
654 	usb_kill_urb(port->interrupt_out_urb);
655 
656 	if (stats)
657 		dev_info(&port->dev, "Statistics: %d Bytes In | %d Bytes Out | %d Commands Issued\n",
658 			priv->bytes_in, priv->bytes_out, priv->cmd_count);
659 } /* cypress_close */
660 
661 
cypress_write(struct tty_struct * tty,struct usb_serial_port * port,const unsigned char * buf,int count)662 static int cypress_write(struct tty_struct *tty, struct usb_serial_port *port,
663 					const unsigned char *buf, int count)
664 {
665 	struct cypress_private *priv = usb_get_serial_port_data(port);
666 
667 	dev_dbg(&port->dev, "%s - %d bytes\n", __func__, count);
668 
669 	/* line control commands, which need to be executed immediately,
670 	   are not put into the buffer for obvious reasons.
671 	 */
672 	if (priv->cmd_ctrl) {
673 		count = 0;
674 		goto finish;
675 	}
676 
677 	if (!count)
678 		return count;
679 
680 	count = kfifo_in_locked(&priv->write_fifo, buf, count, &priv->lock);
681 
682 finish:
683 	cypress_send(port);
684 
685 	return count;
686 } /* cypress_write */
687 
688 
cypress_send(struct usb_serial_port * port)689 static void cypress_send(struct usb_serial_port *port)
690 {
691 	int count = 0, result, offset, actual_size;
692 	struct cypress_private *priv = usb_get_serial_port_data(port);
693 	struct device *dev = &port->dev;
694 	unsigned long flags;
695 
696 	if (!priv->comm_is_ok)
697 		return;
698 
699 	dev_dbg(dev, "%s - interrupt out size is %d\n", __func__,
700 		port->interrupt_out_size);
701 
702 	spin_lock_irqsave(&priv->lock, flags);
703 	if (priv->write_urb_in_use) {
704 		dev_dbg(dev, "%s - can't write, urb in use\n", __func__);
705 		spin_unlock_irqrestore(&priv->lock, flags);
706 		return;
707 	}
708 	spin_unlock_irqrestore(&priv->lock, flags);
709 
710 	/* clear buffer */
711 	memset(port->interrupt_out_urb->transfer_buffer, 0,
712 						port->interrupt_out_size);
713 
714 	spin_lock_irqsave(&priv->lock, flags);
715 	switch (priv->pkt_fmt) {
716 	default:
717 	case packet_format_1:
718 		/* this is for the CY7C64013... */
719 		offset = 2;
720 		port->interrupt_out_buffer[0] = priv->line_control;
721 		break;
722 	case packet_format_2:
723 		/* this is for the CY7C63743... */
724 		offset = 1;
725 		port->interrupt_out_buffer[0] = priv->line_control;
726 		break;
727 	}
728 
729 	if (priv->line_control & CONTROL_RESET)
730 		priv->line_control &= ~CONTROL_RESET;
731 
732 	if (priv->cmd_ctrl) {
733 		priv->cmd_count++;
734 		dev_dbg(dev, "%s - line control command being issued\n", __func__);
735 		spin_unlock_irqrestore(&priv->lock, flags);
736 		goto send;
737 	} else
738 		spin_unlock_irqrestore(&priv->lock, flags);
739 
740 	count = kfifo_out_locked(&priv->write_fifo,
741 					&port->interrupt_out_buffer[offset],
742 					port->interrupt_out_size - offset,
743 					&priv->lock);
744 	if (count == 0)
745 		return;
746 
747 	switch (priv->pkt_fmt) {
748 	default:
749 	case packet_format_1:
750 		port->interrupt_out_buffer[1] = count;
751 		break;
752 	case packet_format_2:
753 		port->interrupt_out_buffer[0] |= count;
754 	}
755 
756 	dev_dbg(dev, "%s - count is %d\n", __func__, count);
757 
758 send:
759 	spin_lock_irqsave(&priv->lock, flags);
760 	priv->write_urb_in_use = 1;
761 	spin_unlock_irqrestore(&priv->lock, flags);
762 
763 	if (priv->cmd_ctrl)
764 		actual_size = 1;
765 	else
766 		actual_size = count +
767 			      (priv->pkt_fmt == packet_format_1 ? 2 : 1);
768 
769 	usb_serial_debug_data(dev, __func__, port->interrupt_out_size,
770 			      port->interrupt_out_urb->transfer_buffer);
771 
772 	usb_fill_int_urb(port->interrupt_out_urb, port->serial->dev,
773 		usb_sndintpipe(port->serial->dev, port->interrupt_out_endpointAddress),
774 		port->interrupt_out_buffer, actual_size,
775 		cypress_write_int_callback, port, priv->write_urb_interval);
776 	result = usb_submit_urb(port->interrupt_out_urb, GFP_ATOMIC);
777 	if (result) {
778 		dev_err_console(port,
779 				"%s - failed submitting write urb, error %d\n",
780 							__func__, result);
781 		priv->write_urb_in_use = 0;
782 		cypress_set_dead(port);
783 	}
784 
785 	spin_lock_irqsave(&priv->lock, flags);
786 	if (priv->cmd_ctrl)
787 		priv->cmd_ctrl = 0;
788 
789 	/* do not count the line control and size bytes */
790 	priv->bytes_out += count;
791 	spin_unlock_irqrestore(&priv->lock, flags);
792 
793 	usb_serial_port_softint(port);
794 } /* cypress_send */
795 
796 
797 /* returns how much space is available in the soft buffer */
cypress_write_room(struct tty_struct * tty)798 static int cypress_write_room(struct tty_struct *tty)
799 {
800 	struct usb_serial_port *port = tty->driver_data;
801 	struct cypress_private *priv = usb_get_serial_port_data(port);
802 	int room = 0;
803 	unsigned long flags;
804 
805 	spin_lock_irqsave(&priv->lock, flags);
806 	room = kfifo_avail(&priv->write_fifo);
807 	spin_unlock_irqrestore(&priv->lock, flags);
808 
809 	dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
810 	return room;
811 }
812 
813 
cypress_tiocmget(struct tty_struct * tty)814 static int cypress_tiocmget(struct tty_struct *tty)
815 {
816 	struct usb_serial_port *port = tty->driver_data;
817 	struct cypress_private *priv = usb_get_serial_port_data(port);
818 	__u8 status, control;
819 	unsigned int result = 0;
820 	unsigned long flags;
821 
822 	spin_lock_irqsave(&priv->lock, flags);
823 	control = priv->line_control;
824 	status = priv->current_status;
825 	spin_unlock_irqrestore(&priv->lock, flags);
826 
827 	result = ((control & CONTROL_DTR)        ? TIOCM_DTR : 0)
828 		| ((control & CONTROL_RTS)       ? TIOCM_RTS : 0)
829 		| ((status & UART_CTS)        ? TIOCM_CTS : 0)
830 		| ((status & UART_DSR)        ? TIOCM_DSR : 0)
831 		| ((status & UART_RI)         ? TIOCM_RI  : 0)
832 		| ((status & UART_CD)         ? TIOCM_CD  : 0);
833 
834 	dev_dbg(&port->dev, "%s - result = %x\n", __func__, result);
835 
836 	return result;
837 }
838 
839 
cypress_tiocmset(struct tty_struct * tty,unsigned int set,unsigned int clear)840 static int cypress_tiocmset(struct tty_struct *tty,
841 			       unsigned int set, unsigned int clear)
842 {
843 	struct usb_serial_port *port = tty->driver_data;
844 	struct cypress_private *priv = usb_get_serial_port_data(port);
845 	unsigned long flags;
846 
847 	spin_lock_irqsave(&priv->lock, flags);
848 	if (set & TIOCM_RTS)
849 		priv->line_control |= CONTROL_RTS;
850 	if (set & TIOCM_DTR)
851 		priv->line_control |= CONTROL_DTR;
852 	if (clear & TIOCM_RTS)
853 		priv->line_control &= ~CONTROL_RTS;
854 	if (clear & TIOCM_DTR)
855 		priv->line_control &= ~CONTROL_DTR;
856 	priv->cmd_ctrl = 1;
857 	spin_unlock_irqrestore(&priv->lock, flags);
858 
859 	return cypress_write(tty, port, NULL, 0);
860 }
861 
cypress_set_termios(struct tty_struct * tty,struct usb_serial_port * port,struct ktermios * old_termios)862 static void cypress_set_termios(struct tty_struct *tty,
863 	struct usb_serial_port *port, struct ktermios *old_termios)
864 {
865 	struct cypress_private *priv = usb_get_serial_port_data(port);
866 	struct device *dev = &port->dev;
867 	int data_bits, stop_bits, parity_type, parity_enable;
868 	unsigned cflag, iflag;
869 	unsigned long flags;
870 	__u8 oldlines;
871 	int linechange = 0;
872 
873 	spin_lock_irqsave(&priv->lock, flags);
874 	/* We can't clean this one up as we don't know the device type
875 	   early enough */
876 	if (!priv->termios_initialized) {
877 		if (priv->chiptype == CT_EARTHMATE) {
878 			tty->termios = tty_std_termios;
879 			tty->termios.c_cflag = B4800 | CS8 | CREAD | HUPCL |
880 				CLOCAL;
881 			tty->termios.c_ispeed = 4800;
882 			tty->termios.c_ospeed = 4800;
883 		} else if (priv->chiptype == CT_CYPHIDCOM) {
884 			tty->termios = tty_std_termios;
885 			tty->termios.c_cflag = B9600 | CS8 | CREAD | HUPCL |
886 				CLOCAL;
887 			tty->termios.c_ispeed = 9600;
888 			tty->termios.c_ospeed = 9600;
889 		} else if (priv->chiptype == CT_CA42V2) {
890 			tty->termios = tty_std_termios;
891 			tty->termios.c_cflag = B9600 | CS8 | CREAD | HUPCL |
892 				CLOCAL;
893 			tty->termios.c_ispeed = 9600;
894 			tty->termios.c_ospeed = 9600;
895 		}
896 		priv->termios_initialized = 1;
897 	}
898 	spin_unlock_irqrestore(&priv->lock, flags);
899 
900 	/* Unsupported features need clearing */
901 	tty->termios.c_cflag &= ~(CMSPAR|CRTSCTS);
902 
903 	cflag = tty->termios.c_cflag;
904 	iflag = tty->termios.c_iflag;
905 
906 	/* check if there are new settings */
907 	if (old_termios) {
908 		spin_lock_irqsave(&priv->lock, flags);
909 		priv->tmp_termios = tty->termios;
910 		spin_unlock_irqrestore(&priv->lock, flags);
911 	}
912 
913 	/* set number of data bits, parity, stop bits */
914 	/* when parity is disabled the parity type bit is ignored */
915 
916 	/* 1 means 2 stop bits, 0 means 1 stop bit */
917 	stop_bits = cflag & CSTOPB ? 1 : 0;
918 
919 	if (cflag & PARENB) {
920 		parity_enable = 1;
921 		/* 1 means odd parity, 0 means even parity */
922 		parity_type = cflag & PARODD ? 1 : 0;
923 	} else
924 		parity_enable = parity_type = 0;
925 
926 	switch (cflag & CSIZE) {
927 	case CS5:
928 		data_bits = 0;
929 		break;
930 	case CS6:
931 		data_bits = 1;
932 		break;
933 	case CS7:
934 		data_bits = 2;
935 		break;
936 	case CS8:
937 		data_bits = 3;
938 		break;
939 	default:
940 		dev_err(dev, "%s - CSIZE was set, but not CS5-CS8\n", __func__);
941 		data_bits = 3;
942 	}
943 	spin_lock_irqsave(&priv->lock, flags);
944 	oldlines = priv->line_control;
945 	if ((cflag & CBAUD) == B0) {
946 		/* drop dtr and rts */
947 		dev_dbg(dev, "%s - dropping the lines, baud rate 0bps\n", __func__);
948 		priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
949 	} else
950 		priv->line_control = (CONTROL_DTR | CONTROL_RTS);
951 	spin_unlock_irqrestore(&priv->lock, flags);
952 
953 	dev_dbg(dev, "%s - sending %d stop_bits, %d parity_enable, %d parity_type, %d data_bits (+5)\n",
954 		__func__, stop_bits, parity_enable, parity_type, data_bits);
955 
956 	cypress_serial_control(tty, port, tty_get_baud_rate(tty),
957 			data_bits, stop_bits,
958 			parity_enable, parity_type,
959 			0, CYPRESS_SET_CONFIG);
960 
961 	/* we perform a CYPRESS_GET_CONFIG so that the current settings are
962 	 * filled into the private structure this should confirm that all is
963 	 * working if it returns what we just set */
964 	cypress_serial_control(tty, port, 0, 0, 0, 0, 0, 0, CYPRESS_GET_CONFIG);
965 
966 	/* Here we can define custom tty settings for devices; the main tty
967 	 * termios flag base comes from empeg.c */
968 
969 	spin_lock_irqsave(&priv->lock, flags);
970 	if (priv->chiptype == CT_EARTHMATE && priv->baud_rate == 4800) {
971 		dev_dbg(dev, "Using custom termios settings for a baud rate of 4800bps.\n");
972 		/* define custom termios settings for NMEA protocol */
973 
974 		tty->termios.c_iflag /* input modes - */
975 			&= ~(IGNBRK  /* disable ignore break */
976 			| BRKINT     /* disable break causes interrupt */
977 			| PARMRK     /* disable mark parity errors */
978 			| ISTRIP     /* disable clear high bit of input char */
979 			| INLCR      /* disable translate NL to CR */
980 			| IGNCR      /* disable ignore CR */
981 			| ICRNL      /* disable translate CR to NL */
982 			| IXON);     /* disable enable XON/XOFF flow control */
983 
984 		tty->termios.c_oflag /* output modes */
985 			&= ~OPOST;    /* disable postprocess output char */
986 
987 		tty->termios.c_lflag /* line discipline modes */
988 			&= ~(ECHO     /* disable echo input characters */
989 			| ECHONL      /* disable echo new line */
990 			| ICANON      /* disable erase, kill, werase, and rprnt
991 					 special characters */
992 			| ISIG        /* disable interrupt, quit, and suspend
993 					 special characters */
994 			| IEXTEN);    /* disable non-POSIX special characters */
995 	} /* CT_CYPHIDCOM: Application should handle this for device */
996 
997 	linechange = (priv->line_control != oldlines);
998 	spin_unlock_irqrestore(&priv->lock, flags);
999 
1000 	/* if necessary, set lines */
1001 	if (linechange) {
1002 		priv->cmd_ctrl = 1;
1003 		cypress_write(tty, port, NULL, 0);
1004 	}
1005 } /* cypress_set_termios */
1006 
1007 
1008 /* returns amount of data still left in soft buffer */
cypress_chars_in_buffer(struct tty_struct * tty)1009 static int cypress_chars_in_buffer(struct tty_struct *tty)
1010 {
1011 	struct usb_serial_port *port = tty->driver_data;
1012 	struct cypress_private *priv = usb_get_serial_port_data(port);
1013 	int chars = 0;
1014 	unsigned long flags;
1015 
1016 	spin_lock_irqsave(&priv->lock, flags);
1017 	chars = kfifo_len(&priv->write_fifo);
1018 	spin_unlock_irqrestore(&priv->lock, flags);
1019 
1020 	dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
1021 	return chars;
1022 }
1023 
1024 
cypress_throttle(struct tty_struct * tty)1025 static void cypress_throttle(struct tty_struct *tty)
1026 {
1027 	struct usb_serial_port *port = tty->driver_data;
1028 	struct cypress_private *priv = usb_get_serial_port_data(port);
1029 
1030 	spin_lock_irq(&priv->lock);
1031 	priv->rx_flags = THROTTLED;
1032 	spin_unlock_irq(&priv->lock);
1033 }
1034 
1035 
cypress_unthrottle(struct tty_struct * tty)1036 static void cypress_unthrottle(struct tty_struct *tty)
1037 {
1038 	struct usb_serial_port *port = tty->driver_data;
1039 	struct cypress_private *priv = usb_get_serial_port_data(port);
1040 	int actually_throttled, result;
1041 
1042 	spin_lock_irq(&priv->lock);
1043 	actually_throttled = priv->rx_flags & ACTUALLY_THROTTLED;
1044 	priv->rx_flags = 0;
1045 	spin_unlock_irq(&priv->lock);
1046 
1047 	if (!priv->comm_is_ok)
1048 		return;
1049 
1050 	if (actually_throttled) {
1051 		result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
1052 		if (result) {
1053 			dev_err(&port->dev, "%s - failed submitting read urb, "
1054 					"error %d\n", __func__, result);
1055 			cypress_set_dead(port);
1056 		}
1057 	}
1058 }
1059 
1060 
cypress_read_int_callback(struct urb * urb)1061 static void cypress_read_int_callback(struct urb *urb)
1062 {
1063 	struct usb_serial_port *port = urb->context;
1064 	struct cypress_private *priv = usb_get_serial_port_data(port);
1065 	struct device *dev = &urb->dev->dev;
1066 	struct tty_struct *tty;
1067 	unsigned char *data = urb->transfer_buffer;
1068 	unsigned long flags;
1069 	char tty_flag = TTY_NORMAL;
1070 	int bytes = 0;
1071 	int result;
1072 	int i = 0;
1073 	int status = urb->status;
1074 
1075 	switch (status) {
1076 	case 0: /* success */
1077 		break;
1078 	case -ECONNRESET:
1079 	case -ENOENT:
1080 	case -ESHUTDOWN:
1081 		/* precursor to disconnect so just go away */
1082 		return;
1083 	case -EPIPE:
1084 		/* Can't call usb_clear_halt while in_interrupt */
1085 		/* FALLS THROUGH */
1086 	default:
1087 		/* something ugly is going on... */
1088 		dev_err(dev, "%s - unexpected nonzero read status received: %d\n",
1089 			__func__, status);
1090 		cypress_set_dead(port);
1091 		return;
1092 	}
1093 
1094 	spin_lock_irqsave(&priv->lock, flags);
1095 	if (priv->rx_flags & THROTTLED) {
1096 		dev_dbg(dev, "%s - now throttling\n", __func__);
1097 		priv->rx_flags |= ACTUALLY_THROTTLED;
1098 		spin_unlock_irqrestore(&priv->lock, flags);
1099 		return;
1100 	}
1101 	spin_unlock_irqrestore(&priv->lock, flags);
1102 
1103 	tty = tty_port_tty_get(&port->port);
1104 	if (!tty) {
1105 		dev_dbg(dev, "%s - bad tty pointer - exiting\n", __func__);
1106 		return;
1107 	}
1108 
1109 	spin_lock_irqsave(&priv->lock, flags);
1110 	result = urb->actual_length;
1111 	switch (priv->pkt_fmt) {
1112 	default:
1113 	case packet_format_1:
1114 		/* This is for the CY7C64013... */
1115 		priv->current_status = data[0] & 0xF8;
1116 		bytes = data[1] + 2;
1117 		i = 2;
1118 		break;
1119 	case packet_format_2:
1120 		/* This is for the CY7C63743... */
1121 		priv->current_status = data[0] & 0xF8;
1122 		bytes = (data[0] & 0x07) + 1;
1123 		i = 1;
1124 		break;
1125 	}
1126 	spin_unlock_irqrestore(&priv->lock, flags);
1127 	if (result < bytes) {
1128 		dev_dbg(dev,
1129 			"%s - wrong packet size - received %d bytes but packet said %d bytes\n",
1130 			__func__, result, bytes);
1131 		goto continue_read;
1132 	}
1133 
1134 	usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data);
1135 
1136 	spin_lock_irqsave(&priv->lock, flags);
1137 	/* check to see if status has changed */
1138 	if (priv->current_status != priv->prev_status) {
1139 		u8 delta = priv->current_status ^ priv->prev_status;
1140 
1141 		if (delta & UART_MSR_MASK) {
1142 			if (delta & UART_CTS)
1143 				port->icount.cts++;
1144 			if (delta & UART_DSR)
1145 				port->icount.dsr++;
1146 			if (delta & UART_RI)
1147 				port->icount.rng++;
1148 			if (delta & UART_CD)
1149 				port->icount.dcd++;
1150 
1151 			wake_up_interruptible(&port->port.delta_msr_wait);
1152 		}
1153 
1154 		priv->prev_status = priv->current_status;
1155 	}
1156 	spin_unlock_irqrestore(&priv->lock, flags);
1157 
1158 	/* hangup, as defined in acm.c... this might be a bad place for it
1159 	 * though */
1160 	if (tty && !C_CLOCAL(tty) && !(priv->current_status & UART_CD)) {
1161 		dev_dbg(dev, "%s - calling hangup\n", __func__);
1162 		tty_hangup(tty);
1163 		goto continue_read;
1164 	}
1165 
1166 	/* There is one error bit... I'm assuming it is a parity error
1167 	 * indicator as the generic firmware will set this bit to 1 if a
1168 	 * parity error occurs.
1169 	 * I can not find reference to any other error events. */
1170 	spin_lock_irqsave(&priv->lock, flags);
1171 	if (priv->current_status & CYP_ERROR) {
1172 		spin_unlock_irqrestore(&priv->lock, flags);
1173 		tty_flag = TTY_PARITY;
1174 		dev_dbg(dev, "%s - Parity Error detected\n", __func__);
1175 	} else
1176 		spin_unlock_irqrestore(&priv->lock, flags);
1177 
1178 	/* process read if there is data other than line status */
1179 	if (bytes > i) {
1180 		tty_insert_flip_string_fixed_flag(&port->port, data + i,
1181 				tty_flag, bytes - i);
1182 		tty_flip_buffer_push(&port->port);
1183 	}
1184 
1185 	spin_lock_irqsave(&priv->lock, flags);
1186 	/* control and status byte(s) are also counted */
1187 	priv->bytes_in += bytes;
1188 	spin_unlock_irqrestore(&priv->lock, flags);
1189 
1190 continue_read:
1191 	tty_kref_put(tty);
1192 
1193 	/* Continue trying to always read */
1194 
1195 	if (priv->comm_is_ok) {
1196 		usb_fill_int_urb(port->interrupt_in_urb, port->serial->dev,
1197 				usb_rcvintpipe(port->serial->dev,
1198 					port->interrupt_in_endpointAddress),
1199 				port->interrupt_in_urb->transfer_buffer,
1200 				port->interrupt_in_urb->transfer_buffer_length,
1201 				cypress_read_int_callback, port,
1202 				priv->read_urb_interval);
1203 		result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
1204 		if (result && result != -EPERM) {
1205 			dev_err(dev, "%s - failed resubmitting read urb, error %d\n",
1206 				__func__, result);
1207 			cypress_set_dead(port);
1208 		}
1209 	}
1210 } /* cypress_read_int_callback */
1211 
1212 
cypress_write_int_callback(struct urb * urb)1213 static void cypress_write_int_callback(struct urb *urb)
1214 {
1215 	struct usb_serial_port *port = urb->context;
1216 	struct cypress_private *priv = usb_get_serial_port_data(port);
1217 	struct device *dev = &urb->dev->dev;
1218 	int status = urb->status;
1219 
1220 	switch (status) {
1221 	case 0:
1222 		/* success */
1223 		break;
1224 	case -ECONNRESET:
1225 	case -ENOENT:
1226 	case -ESHUTDOWN:
1227 		/* this urb is terminated, clean up */
1228 		dev_dbg(dev, "%s - urb shutting down with status: %d\n",
1229 			__func__, status);
1230 		priv->write_urb_in_use = 0;
1231 		return;
1232 	case -EPIPE:
1233 		/* Cannot call usb_clear_halt while in_interrupt */
1234 		/* FALLTHROUGH */
1235 	default:
1236 		dev_err(dev, "%s - unexpected nonzero write status received: %d\n",
1237 			__func__, status);
1238 		cypress_set_dead(port);
1239 		break;
1240 	}
1241 	priv->write_urb_in_use = 0;
1242 
1243 	/* send any buffered data */
1244 	cypress_send(port);
1245 }
1246 
1247 module_usb_serial_driver(serial_drivers, id_table_combined);
1248 
1249 MODULE_AUTHOR(DRIVER_AUTHOR);
1250 MODULE_DESCRIPTION(DRIVER_DESC);
1251 MODULE_LICENSE("GPL");
1252 
1253 module_param(stats, bool, S_IRUGO | S_IWUSR);
1254 MODULE_PARM_DESC(stats, "Enable statistics or not");
1255 module_param(interval, int, S_IRUGO | S_IWUSR);
1256 MODULE_PARM_DESC(interval, "Overrides interrupt interval");
1257 module_param(unstable_bauds, bool, S_IRUGO | S_IWUSR);
1258 MODULE_PARM_DESC(unstable_bauds, "Allow unstable baud rates");
1259