cypress_m8.c 35.9 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0+
L
Linus Torvalds 已提交
2 3 4 5
/*
 * USB Cypress M8 driver
 *
 * 	Copyright (C) 2004
A
Alan Cox 已提交
6
 * 	    Lonnie Mendez (dignome@gmail.com)
L
Linus Torvalds 已提交
7 8 9
 *	Copyright (C) 2003,2004
 *	    Neil Whelchel (koyama@firstlight.net)
 *
10
 * See Documentation/usb/usb-serial.rst for more information on using this
A
Alan Cox 已提交
11
 * driver
L
Linus Torvalds 已提交
12 13 14 15 16
 *
 * See http://geocities.com/i0xox0i for information on this driver and the
 * earthmate usb device.
 */

A
Alan Cox 已提交
17 18
/* Thanks to Neil Whelchel for writing the first cypress m8 implementation
   for linux. */
L
Linus Torvalds 已提交
19 20
/* Thanks to cypress for providing references for the hid reports. */
/* Thanks to Jiang Zhang for providing links and for general help. */
A
Alan Cox 已提交
21
/* Code originates and was built up from ftdi_sio, belkin, pl2303 and others.*/
L
Linus Torvalds 已提交
22 23 24 25 26 27 28 29 30 31 32 33


#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/slab.h>
#include <linux/tty.h>
#include <linux/tty_driver.h>
#include <linux/tty_flip.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/spinlock.h>
#include <linux/usb.h>
34
#include <linux/usb/serial.h>
L
Linus Torvalds 已提交
35
#include <linux/serial.h>
36
#include <linux/kfifo.h>
L
Linus Torvalds 已提交
37
#include <linux/delay.h>
A
Alan Cox 已提交
38
#include <linux/uaccess.h>
39
#include <asm/unaligned.h>
L
Linus Torvalds 已提交
40 41 42 43

#include "cypress_m8.h"


44
static bool stats;
45
static int interval;
46
static bool unstable_bauds;
L
Linus Torvalds 已提交
47 48 49 50 51 52 53

#define DRIVER_AUTHOR "Lonnie Mendez <dignome@gmail.com>, Neil Whelchel <koyama@firstlight.net>"
#define DRIVER_DESC "Cypress USB to Serial Driver"

/* write buffer size defines */
#define CYPRESS_BUF_SIZE	1024

54
static const struct usb_device_id id_table_earthmate[] = {
L
Linus Torvalds 已提交
55
	{ USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB) },
56
	{ USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB_LT20) },
L
Linus Torvalds 已提交
57 58 59
	{ }						/* Terminating entry */
};

60
static const struct usb_device_id id_table_cyphidcomrs232[] = {
L
Linus Torvalds 已提交
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) },
L
Linus Torvalds 已提交
65 66 67
	{ }						/* Terminating entry */
};

68
static const struct usb_device_id id_table_nokiaca42v2[] = {
69 70 71 72
	{ USB_DEVICE(VENDOR_ID_DAZZLE, PRODUCT_ID_CA42) },
	{ }						/* Terminating entry */
};

73
static const struct usb_device_id id_table_combined[] = {
L
Linus Torvalds 已提交
74
	{ USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB) },
75
	{ USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB_LT20) },
L
Linus Torvalds 已提交
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) },
L
Linus Torvalds 已提交
81 82 83
	{ }						/* Terminating entry */
};

A
Alan Cox 已提交
84
MODULE_DEVICE_TABLE(usb, id_table_combined);
L
Linus Torvalds 已提交
85

86 87 88 89 90
enum packet_format {
	packet_format_1,  /* b0:status, b1:payload count */
	packet_format_2   /* b0[7:3]:status, b0[2:0]:payload count */
};

L
Linus Torvalds 已提交
91 92 93 94 95 96 97
struct cypress_private {
	spinlock_t lock;		   /* private lock */
	int chiptype;			   /* identifier of device, for quirks/etc */
	int bytes_in;			   /* used for statistics */
	int bytes_out;			   /* used for statistics */
	int cmd_count;			   /* used for statistics */
	int cmd_ctrl;			   /* always set this to 1 before issuing a command */
98
	struct kfifo write_fifo;	   /* write fifo */
L
Linus Torvalds 已提交
99
	int write_urb_in_use;		   /* write urb in use indicator */
100 101
	int write_urb_interval;            /* interval to use for write urb */
	int read_urb_interval;             /* interval to use for read urb */
102
	int comm_is_ok;                    /* true if communication is (still) ok */
L
Linus Torvalds 已提交
103 104 105 106
	__u8 line_control;	   	   /* holds dtr / rts value */
	__u8 current_status;	   	   /* received from last read - info on dsr,cts,cd,ri,etc */
	__u8 current_config;	   	   /* stores the current configuration byte */
	__u8 rx_flags;			   /* throttling - used from whiteheat/ftdi_sio */
107
	enum packet_format pkt_fmt;	   /* format to use for packet send / receive */
108
	int get_cfg_unsafe;		   /* If true, the CYPRESS_GET_CONFIG is unsafe */
A
Alan Cox 已提交
109 110
	int baud_rate;			   /* stores current baud rate in
					      integer form */
111
	char prev_status;		   /* used for TIOCMIWAIT */
L
Linus Torvalds 已提交
112 113 114
};

/* function prototypes for the Cypress USB to serial device */
115 116 117 118
static int  cypress_earthmate_port_probe(struct usb_serial_port *port);
static int  cypress_hidcom_port_probe(struct usb_serial_port *port);
static int  cypress_ca42v2_port_probe(struct usb_serial_port *port);
static int  cypress_port_remove(struct usb_serial_port *port);
119
static int  cypress_open(struct tty_struct *tty, struct usb_serial_port *port);
120 121
static void cypress_close(struct usb_serial_port *port);
static void cypress_dtr_rts(struct usb_serial_port *port, int on);
A
Alan Cox 已提交
122 123 124 125
static int  cypress_write(struct tty_struct *tty, struct usb_serial_port *port,
			const unsigned char *buf, int count);
static void cypress_send(struct usb_serial_port *port);
static int  cypress_write_room(struct tty_struct *tty);
126
static void cypress_earthmate_init_termios(struct tty_struct *tty);
A
Alan Cox 已提交
127 128
static void cypress_set_termios(struct tty_struct *tty,
			struct usb_serial_port *port, struct ktermios *old);
129
static int  cypress_tiocmget(struct tty_struct *tty);
130
static int  cypress_tiocmset(struct tty_struct *tty,
A
Alan Cox 已提交
131 132 133 134 135 136 137
			unsigned int set, unsigned int clear);
static int  cypress_chars_in_buffer(struct tty_struct *tty);
static void cypress_throttle(struct tty_struct *tty);
static void cypress_unthrottle(struct tty_struct *tty);
static void cypress_set_dead(struct usb_serial_port *port);
static void cypress_read_int_callback(struct urb *urb);
static void cypress_write_int_callback(struct urb *urb);
L
Linus Torvalds 已提交
138

139
static struct usb_serial_driver cypress_earthmate_device = {
140 141
	.driver = {
		.owner =		THIS_MODULE,
142
		.name =			"earthmate",
143
	},
144
	.description =			"DeLorme Earthmate USB",
L
Linus Torvalds 已提交
145 146
	.id_table =			id_table_earthmate,
	.num_ports =			1,
147 148
	.port_probe =			cypress_earthmate_port_probe,
	.port_remove =			cypress_port_remove,
L
Linus Torvalds 已提交
149 150
	.open =				cypress_open,
	.close =			cypress_close,
151
	.dtr_rts =			cypress_dtr_rts,
L
Linus Torvalds 已提交
152 153
	.write =			cypress_write,
	.write_room =			cypress_write_room,
154
	.init_termios =			cypress_earthmate_init_termios,
L
Linus Torvalds 已提交
155 156 157
	.set_termios =			cypress_set_termios,
	.tiocmget =			cypress_tiocmget,
	.tiocmset =			cypress_tiocmset,
158
	.tiocmiwait =			usb_serial_generic_tiocmiwait,
L
Linus Torvalds 已提交
159 160 161 162 163 164 165
	.chars_in_buffer =		cypress_chars_in_buffer,
	.throttle =		 	cypress_throttle,
	.unthrottle =			cypress_unthrottle,
	.read_int_callback =		cypress_read_int_callback,
	.write_int_callback =		cypress_write_int_callback,
};

166
static struct usb_serial_driver cypress_hidcom_device = {
167 168
	.driver = {
		.owner =		THIS_MODULE,
169
		.name =			"cyphidcom",
170
	},
171
	.description =			"HID->COM RS232 Adapter",
L
Linus Torvalds 已提交
172 173
	.id_table =			id_table_cyphidcomrs232,
	.num_ports =			1,
174 175
	.port_probe =			cypress_hidcom_port_probe,
	.port_remove =			cypress_port_remove,
L
Linus Torvalds 已提交
176 177
	.open =				cypress_open,
	.close =			cypress_close,
178
	.dtr_rts =			cypress_dtr_rts,
L
Linus Torvalds 已提交
179 180 181 182 183
	.write =			cypress_write,
	.write_room =			cypress_write_room,
	.set_termios =			cypress_set_termios,
	.tiocmget =			cypress_tiocmget,
	.tiocmset =			cypress_tiocmset,
184
	.tiocmiwait =			usb_serial_generic_tiocmiwait,
L
Linus Torvalds 已提交
185 186 187 188 189 190 191
	.chars_in_buffer =		cypress_chars_in_buffer,
	.throttle =			cypress_throttle,
	.unthrottle =			cypress_unthrottle,
	.read_int_callback =		cypress_read_int_callback,
	.write_int_callback =		cypress_write_int_callback,
};

192 193 194
static struct usb_serial_driver cypress_ca42v2_device = {
	.driver = {
		.owner =		THIS_MODULE,
A
Alan Cox 已提交
195
		.name =			"nokiaca42v2",
196 197 198 199
	},
	.description =			"Nokia CA-42 V2 Adapter",
	.id_table =			id_table_nokiaca42v2,
	.num_ports =			1,
200 201
	.port_probe =			cypress_ca42v2_port_probe,
	.port_remove =			cypress_port_remove,
202 203
	.open =				cypress_open,
	.close =			cypress_close,
204
	.dtr_rts =			cypress_dtr_rts,
205 206 207 208 209
	.write =			cypress_write,
	.write_room =			cypress_write_room,
	.set_termios =			cypress_set_termios,
	.tiocmget =			cypress_tiocmget,
	.tiocmset =			cypress_tiocmset,
210
	.tiocmiwait =			usb_serial_generic_tiocmiwait,
211 212 213 214 215 216
	.chars_in_buffer =		cypress_chars_in_buffer,
	.throttle =			cypress_throttle,
	.unthrottle =			cypress_unthrottle,
	.read_int_callback =		cypress_read_int_callback,
	.write_int_callback =		cypress_write_int_callback,
};
L
Linus Torvalds 已提交
217

218 219 220 221 222
static struct usb_serial_driver * const serial_drivers[] = {
	&cypress_earthmate_device, &cypress_hidcom_device,
	&cypress_ca42v2_device, NULL
};

L
Linus Torvalds 已提交
223 224 225 226
/*****************************************************************************
 * Cypress serial helper functions
 *****************************************************************************/

227 228 229 230 231 232
/* FRWD Dongle hidcom needs to skip reset and speed checks */
static inline bool is_frwd(struct usb_device *dev)
{
	return ((le16_to_cpu(dev->descriptor.idVendor) == VENDOR_ID_FRWD) &&
		(le16_to_cpu(dev->descriptor.idProduct) == PRODUCT_ID_CYPHIDCOM_FRWD));
}
L
Linus Torvalds 已提交
233

A
Alan Cox 已提交
234
static int analyze_baud_rate(struct usb_serial_port *port, speed_t new_rate)
235 236 237 238
{
	struct cypress_private *priv;
	priv = usb_get_serial_port_data(port);

239 240 241
	if (unstable_bauds)
		return new_rate;

242 243 244 245
	/* FRWD Dongle uses 115200 bps */
	if (is_frwd(port->serial->dev))
		return new_rate;

246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
	/*
	 * The general purpose firmware for the Cypress M8 allows for
	 * a maximum speed of 57600bps (I have no idea whether DeLorme
	 * chose to use the general purpose firmware or not), if you
	 * need to modify this speed setting for your own project
	 * please add your own chiptype and modify the code likewise.
	 * The Cypress HID->COM device will work successfully up to
	 * 115200bps (but the actual throughput is around 3kBps).
	 */
	if (port->serial->dev->speed == USB_SPEED_LOW) {
		/*
		 * Mike Isely <isely@pobox.com> 2-Feb-2008: The
		 * Cypress app note that describes this mechanism
		 * states the the low-speed part can't handle more
		 * than 800 bytes/sec, in which case 4800 baud is the
		 * safest speed for a part like that.
		 */
		if (new_rate > 4800) {
264 265 266
			dev_dbg(&port->dev,
				"%s - failed setting baud rate, device incapable speed %d\n",
				__func__, new_rate);
267 268 269 270 271 272 273 274 275
			return -1;
		}
	}
	switch (priv->chiptype) {
	case CT_EARTHMATE:
		if (new_rate <= 600) {
			/* 300 and 600 baud rates are supported under
			 * the generic firmware, but are not used with
			 * NMEA and SiRF protocols */
276
			dev_dbg(&port->dev,
277
				"%s - failed setting baud rate, unsupported speed of %d on Earthmate GPS\n",
278
				__func__, new_rate);
279 280 281 282 283 284 285 286 287 288
			return -1;
		}
		break;
	default:
		break;
	}
	return new_rate;
}


289
/* This function can either set or retrieve the current serial line settings */
A
Alan Cox 已提交
290
static int cypress_serial_control(struct tty_struct *tty,
A
Alan Cox 已提交
291 292 293
	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)
L
Linus Torvalds 已提交
294
{
295
	int new_baudrate = 0, retval = 0, tries = 0;
L
Linus Torvalds 已提交
296
	struct cypress_private *priv;
297
	struct device *dev = &port->dev;
298 299
	u8 *feature_buffer;
	const unsigned int feature_len = 5;
L
Linus Torvalds 已提交
300 301 302 303
	unsigned long flags;

	priv = usb_get_serial_port_data(port);

304 305 306
	if (!priv->comm_is_ok)
		return -ENODEV;

307 308 309 310
	feature_buffer = kcalloc(feature_len, sizeof(u8), GFP_KERNEL);
	if (!feature_buffer)
		return -ENOMEM;

A
Alan Cox 已提交
311 312 313
	switch (cypress_request_type) {
	case CYPRESS_SET_CONFIG:
		/* 0 means 'Hang up' so doesn't change the true bit rate */
314 315
		new_baudrate = priv->baud_rate;
		if (baud_rate && baud_rate != priv->baud_rate) {
316
			dev_dbg(dev, "%s - baud rate is changing\n", __func__);
A
Alan Cox 已提交
317
			retval = analyze_baud_rate(port, baud_rate);
318
			if (retval >= 0) {
A
Alan Cox 已提交
319
				new_baudrate = retval;
320 321
				dev_dbg(dev, "%s - New baud rate set to %d\n",
					__func__, new_baudrate);
L
Linus Torvalds 已提交
322
			}
A
Alan Cox 已提交
323
		}
324 325
		dev_dbg(dev, "%s - baud rate is being sent as %d\n", __func__,
			new_baudrate);
A
Alan Cox 已提交
326 327

		/* fill the feature_buffer with new configuration */
328
		put_unaligned_le32(new_baudrate, feature_buffer);
A
Alan Cox 已提交
329 330 331 332 333 334 335 336
		feature_buffer[4] |= data_bits;   /* assign data bits in 2 bit space ( max 3 ) */
		/* 1 bit gap */
		feature_buffer[4] |= (stop_bits << 3);   /* assign stop bits in 1 bit space */
		feature_buffer[4] |= (parity_enable << 4);   /* assign parity flag in 1 bit space */
		feature_buffer[4] |= (parity_type << 5);   /* assign parity type in 1 bit space */
		/* 1 bit gap */
		feature_buffer[4] |= (reset << 7);   /* assign reset at end of byte, 1 bit space */

337 338
		dev_dbg(dev, "%s - device is being sent this feature report:\n", __func__);
		dev_dbg(dev, "%s - %02X - %02X - %02X - %02X - %02X\n", __func__,
A
Alan Cox 已提交
339 340 341 342 343 344 345 346 347 348
			feature_buffer[0], feature_buffer[1],
			feature_buffer[2], feature_buffer[3],
			feature_buffer[4]);

		do {
			retval = usb_control_msg(port->serial->dev,
					usb_sndctrlpipe(port->serial->dev, 0),
					HID_REQ_SET_REPORT,
					USB_DIR_OUT | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
					0x0300, 0, feature_buffer,
349
					feature_len, 500);
A
Alan Cox 已提交
350 351 352 353

			if (tries++ >= 3)
				break;

354
		} while (retval != feature_len &&
A
Alan Cox 已提交
355 356
			 retval != -ENODEV);

357
		if (retval != feature_len) {
358 359
			dev_err(dev, "%s - failed sending serial line settings - %d\n",
				__func__, retval);
A
Alan Cox 已提交
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
			cypress_set_dead(port);
		} else {
			spin_lock_irqsave(&priv->lock, flags);
			priv->baud_rate = new_baudrate;
			priv->current_config = feature_buffer[4];
			spin_unlock_irqrestore(&priv->lock, flags);
			/* If we asked for a speed change encode it */
			if (baud_rate)
				tty_encode_baud_rate(tty,
					new_baudrate, new_baudrate);
		}
	break;
	case CYPRESS_GET_CONFIG:
		if (priv->get_cfg_unsafe) {
			/* Not implemented for this device,
			   and if we try to do it we're likely
			   to crash the hardware. */
377 378
			retval = -ENOTTY;
			goto out;
A
Alan Cox 已提交
379
		}
380
		dev_dbg(dev, "%s - retrieving serial line settings\n", __func__);
A
Alan Cox 已提交
381 382 383 384 385 386
		do {
			retval = usb_control_msg(port->serial->dev,
					usb_rcvctrlpipe(port->serial->dev, 0),
					HID_REQ_GET_REPORT,
					USB_DIR_IN | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
					0x0300, 0, feature_buffer,
387
					feature_len, 500);
A
Alan Cox 已提交
388 389 390

			if (tries++ >= 3)
				break;
391
		} while (retval != feature_len
A
Alan Cox 已提交
392 393
						&& retval != -ENODEV);

394
		if (retval != feature_len) {
395 396
			dev_err(dev, "%s - failed to retrieve serial line settings - %d\n",
				__func__, retval);
A
Alan Cox 已提交
397
			cypress_set_dead(port);
398
			goto out;
A
Alan Cox 已提交
399 400 401 402 403
		} else {
			spin_lock_irqsave(&priv->lock, flags);
			/* store the config in one byte, and later
			   use bit masks to check values */
			priv->current_config = feature_buffer[4];
404
			priv->baud_rate = get_unaligned_le32(feature_buffer);
A
Alan Cox 已提交
405 406
			spin_unlock_irqrestore(&priv->lock, flags);
		}
L
Linus Torvalds 已提交
407
	}
408 409 410
	spin_lock_irqsave(&priv->lock, flags);
	++priv->cmd_count;
	spin_unlock_irqrestore(&priv->lock, flags);
411 412
out:
	kfree(feature_buffer);
L
Linus Torvalds 已提交
413 414 415 416
	return retval;
} /* cypress_serial_control */


417 418 419 420 421 422 423 424 425 426 427 428 429
static void cypress_set_dead(struct usb_serial_port *port)
{
	struct cypress_private *priv = usb_get_serial_port_data(port);
	unsigned long flags;

	spin_lock_irqsave(&priv->lock, flags);
	if (!priv->comm_is_ok) {
		spin_unlock_irqrestore(&priv->lock, flags);
		return;
	}
	priv->comm_is_ok = 0;
	spin_unlock_irqrestore(&priv->lock, flags);

430
	dev_err(&port->dev, "cypress_m8 suspending failing port %d - "
431
		"interval might be too short\n", port->port_number);
432 433 434
}


L
Linus Torvalds 已提交
435 436 437 438 439
/*****************************************************************************
 * Cypress serial driver functions
 *****************************************************************************/


440
static int cypress_generic_port_probe(struct usb_serial_port *port)
L
Linus Torvalds 已提交
441
{
442
	struct usb_serial *serial = port->serial;
L
Linus Torvalds 已提交
443 444
	struct cypress_private *priv;

445 446 447 448 449
	if (!port->interrupt_out_urb || !port->interrupt_in_urb) {
		dev_err(&port->dev, "required endpoint is missing\n");
		return -ENODEV;
	}

A
Alan Cox 已提交
450
	priv = kzalloc(sizeof(struct cypress_private), GFP_KERNEL);
L
Linus Torvalds 已提交
451 452 453
	if (!priv)
		return -ENOMEM;

454
	priv->comm_is_ok = !0;
L
Linus Torvalds 已提交
455
	spin_lock_init(&priv->lock);
456
	if (kfifo_alloc(&priv->write_fifo, CYPRESS_BUF_SIZE, GFP_KERNEL)) {
L
Linus Torvalds 已提交
457 458 459
		kfree(priv);
		return -ENOMEM;
	}
A
Alan Cox 已提交
460

461 462 463 464 465
	/* Skip reset for FRWD device. It is a workaound:
	   device hangs if it receives SET_CONFIGURE in Configured
	   state. */
	if (!is_frwd(serial->dev))
		usb_reset_configuration(serial->dev);
A
Alan Cox 已提交
466

L
Linus Torvalds 已提交
467 468 469
	priv->cmd_ctrl = 0;
	priv->line_control = 0;
	priv->rx_flags = 0;
470 471 472 473 474 475
	/* Default packet format setting is determined by packet size.
	   Anything with a size larger then 9 must have a separate
	   count field since the 3 bit count field is otherwise too
	   small.  Otherwise we can use the slightly more compact
	   format.  This is in accordance with the cypress_m8 serial
	   converter app note. */
A
Alan Cox 已提交
476
	if (port->interrupt_out_size > 9)
477
		priv->pkt_fmt = packet_format_1;
A
Alan Cox 已提交
478
	else
479
		priv->pkt_fmt = packet_format_2;
A
Alan Cox 已提交
480

481 482 483
	if (interval > 0) {
		priv->write_urb_interval = interval;
		priv->read_urb_interval = interval;
484 485
		dev_dbg(&port->dev, "%s - read & write intervals forced to %d\n",
			__func__, interval);
486 487 488
	} else {
		priv->write_urb_interval = port->interrupt_out_urb->interval;
		priv->read_urb_interval = port->interrupt_in_urb->interval;
489 490 491
		dev_dbg(&port->dev, "%s - intervals: read=%d write=%d\n",
			__func__, priv->read_urb_interval,
			priv->write_urb_interval);
492 493
	}
	usb_set_serial_port_data(port, priv);
A
Alan Cox 已提交
494

495 496
	port->port.drain_delay = 256;

497 498
	return 0;
}
L
Linus Torvalds 已提交
499 500


501
static int cypress_earthmate_port_probe(struct usb_serial_port *port)
L
Linus Torvalds 已提交
502
{
503
	struct usb_serial *serial = port->serial;
L
Linus Torvalds 已提交
504
	struct cypress_private *priv;
505
	int ret;
L
Linus Torvalds 已提交
506

507 508
	ret = cypress_generic_port_probe(port);
	if (ret) {
509
		dev_dbg(&port->dev, "%s - Failed setting up port\n", __func__);
510
		return ret;
L
Linus Torvalds 已提交
511 512
	}

513
	priv = usb_get_serial_port_data(port);
L
Linus Torvalds 已提交
514
	priv->chiptype = CT_EARTHMATE;
515 516 517
	/* All Earthmate devices use the separated-count packet
	   format!  Idiotic. */
	priv->pkt_fmt = packet_format_1;
A
Alan Cox 已提交
518 519
	if (serial->dev->descriptor.idProduct !=
				cpu_to_le16(PRODUCT_ID_EARTHMATEUSB)) {
520 521 522 523
		/* The old original USB Earthmate seemed able to
		   handle GET_CONFIG requests; everything they've
		   produced since that time crashes if this command is
		   attempted :-( */
524 525 526
		dev_dbg(&port->dev,
			"%s - Marking this device as unsafe for GET_CONFIG commands\n",
			__func__);
527 528
		priv->get_cfg_unsafe = !0;
	}
529 530

	return 0;
531
}
L
Linus Torvalds 已提交
532

533
static int cypress_hidcom_port_probe(struct usb_serial_port *port)
L
Linus Torvalds 已提交
534 535
{
	struct cypress_private *priv;
536
	int ret;
L
Linus Torvalds 已提交
537

538 539
	ret = cypress_generic_port_probe(port);
	if (ret) {
540
		dev_dbg(&port->dev, "%s - Failed setting up port\n", __func__);
541
		return ret;
L
Linus Torvalds 已提交
542 543
	}

544
	priv = usb_get_serial_port_data(port);
L
Linus Torvalds 已提交
545
	priv->chiptype = CT_CYPHIDCOM;
A
Alan Cox 已提交
546

547
	return 0;
548
}
L
Linus Torvalds 已提交
549

550
static int cypress_ca42v2_port_probe(struct usb_serial_port *port)
551 552
{
	struct cypress_private *priv;
553
	int ret;
554

555 556
	ret = cypress_generic_port_probe(port);
	if (ret) {
557
		dev_dbg(&port->dev, "%s - Failed setting up port\n", __func__);
558
		return ret;
559 560
	}

561
	priv = usb_get_serial_port_data(port);
562 563 564
	priv->chiptype = CT_CA42V2;

	return 0;
565
}
566

567
static int cypress_port_remove(struct usb_serial_port *port)
L
Linus Torvalds 已提交
568 569 570
{
	struct cypress_private *priv;

571
	priv = usb_get_serial_port_data(port);
L
Linus Torvalds 已提交
572

573 574
	kfifo_free(&priv->write_fifo);
	kfree(priv);
L
Linus Torvalds 已提交
575

576 577
	return 0;
}
L
Linus Torvalds 已提交
578

579
static int cypress_open(struct tty_struct *tty, struct usb_serial_port *port)
L
Linus Torvalds 已提交
580 581 582 583 584 585
{
	struct cypress_private *priv = usb_get_serial_port_data(port);
	struct usb_serial *serial = port->serial;
	unsigned long flags;
	int result = 0;

586 587 588
	if (!priv->comm_is_ok)
		return -EIO;

L
Linus Torvalds 已提交
589 590 591 592 593 594 595 596 597 598 599 600
	/* clear halts before open */
	usb_clear_halt(serial->dev, 0x81);
	usb_clear_halt(serial->dev, 0x02);

	spin_lock_irqsave(&priv->lock, flags);
	/* reset read/write statistics */
	priv->bytes_in = 0;
	priv->bytes_out = 0;
	priv->cmd_count = 0;
	priv->rx_flags = 0;
	spin_unlock_irqrestore(&priv->lock, flags);

601
	/* Set termios */
A
Alan Cox 已提交
602
	cypress_send(port);
L
Linus Torvalds 已提交
603

A
Alan Cox 已提交
604
	if (tty)
605
		cypress_set_termios(tty, port, NULL);
L
Linus Torvalds 已提交
606 607 608 609

	/* setup the port and start reading from the device */
	usb_fill_int_urb(port->interrupt_in_urb, serial->dev,
		usb_rcvintpipe(serial->dev, port->interrupt_in_endpointAddress),
A
Alan Cox 已提交
610 611
		port->interrupt_in_urb->transfer_buffer,
		port->interrupt_in_urb->transfer_buffer_length,
612
		cypress_read_int_callback, port, priv->read_urb_interval);
L
Linus Torvalds 已提交
613 614
	result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);

A
Alan Cox 已提交
615 616 617 618
	if (result) {
		dev_err(&port->dev,
			"%s - failed submitting read urb, error %d\n",
							__func__, result);
619
		cypress_set_dead(port);
L
Linus Torvalds 已提交
620
	}
621

L
Linus Torvalds 已提交
622 623 624
	return result;
} /* cypress_open */

625 626 627 628 629 630 631 632 633 634 635 636 637
static void cypress_dtr_rts(struct usb_serial_port *port, int on)
{
	struct cypress_private *priv = usb_get_serial_port_data(port);
	/* drop dtr and rts */
	spin_lock_irq(&priv->lock);
	if (on == 0)
		priv->line_control = 0;
	else 
		priv->line_control = CONTROL_DTR | CONTROL_RTS;
	priv->cmd_ctrl = 1;
	spin_unlock_irq(&priv->lock);
	cypress_write(NULL, port, NULL, 0);
}
L
Linus Torvalds 已提交
638

639
static void cypress_close(struct usb_serial_port *port)
L
Linus Torvalds 已提交
640 641
{
	struct cypress_private *priv = usb_get_serial_port_data(port);
642
	unsigned long flags;
L
Linus Torvalds 已提交
643

644 645 646 647
	spin_lock_irqsave(&priv->lock, flags);
	kfifo_reset_out(&priv->write_fifo);
	spin_unlock_irqrestore(&priv->lock, flags);

648
	dev_dbg(&port->dev, "%s - stopping urbs\n", __func__);
A
Alan Cox 已提交
649 650
	usb_kill_urb(port->interrupt_in_urb);
	usb_kill_urb(port->interrupt_out_urb);
L
Linus Torvalds 已提交
651 652

	if (stats)
A
Alan Cox 已提交
653 654
		dev_info(&port->dev, "Statistics: %d Bytes In | %d Bytes Out | %d Commands Issued\n",
			priv->bytes_in, priv->bytes_out, priv->cmd_count);
L
Linus Torvalds 已提交
655 656 657
} /* cypress_close */


A
Alan Cox 已提交
658 659
static int cypress_write(struct tty_struct *tty, struct usb_serial_port *port,
					const unsigned char *buf, int count)
L
Linus Torvalds 已提交
660 661
{
	struct cypress_private *priv = usb_get_serial_port_data(port);
A
Alan Cox 已提交
662

663
	dev_dbg(&port->dev, "%s - %d bytes\n", __func__, count);
L
Linus Torvalds 已提交
664 665 666 667 668 669 670 671

	/* line control commands, which need to be executed immediately,
	   are not put into the buffer for obvious reasons.
	 */
	if (priv->cmd_ctrl) {
		count = 0;
		goto finish;
	}
A
Alan Cox 已提交
672

L
Linus Torvalds 已提交
673 674
	if (!count)
		return count;
A
Alan Cox 已提交
675

676
	count = kfifo_in_locked(&priv->write_fifo, buf, count, &priv->lock);
L
Linus Torvalds 已提交
677 678 679 680 681 682 683 684 685 686 687 688

finish:
	cypress_send(port);

	return count;
} /* cypress_write */


static void cypress_send(struct usb_serial_port *port)
{
	int count = 0, result, offset, actual_size;
	struct cypress_private *priv = usb_get_serial_port_data(port);
689
	struct device *dev = &port->dev;
L
Linus Torvalds 已提交
690
	unsigned long flags;
A
Alan Cox 已提交
691

692 693 694
	if (!priv->comm_is_ok)
		return;

695 696
	dev_dbg(dev, "%s - interrupt out size is %d\n", __func__,
		port->interrupt_out_size);
A
Alan Cox 已提交
697

L
Linus Torvalds 已提交
698 699
	spin_lock_irqsave(&priv->lock, flags);
	if (priv->write_urb_in_use) {
700
		dev_dbg(dev, "%s - can't write, urb in use\n", __func__);
L
Linus Torvalds 已提交
701 702 703 704 705 706
		spin_unlock_irqrestore(&priv->lock, flags);
		return;
	}
	spin_unlock_irqrestore(&priv->lock, flags);

	/* clear buffer */
A
Alan Cox 已提交
707 708
	memset(port->interrupt_out_urb->transfer_buffer, 0,
						port->interrupt_out_size);
L
Linus Torvalds 已提交
709 710

	spin_lock_irqsave(&priv->lock, flags);
711 712 713 714 715 716 717 718 719 720 721 722
	switch (priv->pkt_fmt) {
	default:
	case packet_format_1:
		/* this is for the CY7C64013... */
		offset = 2;
		port->interrupt_out_buffer[0] = priv->line_control;
		break;
	case packet_format_2:
		/* this is for the CY7C63743... */
		offset = 1;
		port->interrupt_out_buffer[0] = priv->line_control;
		break;
L
Linus Torvalds 已提交
723 724 725 726 727 728 729
	}

	if (priv->line_control & CONTROL_RESET)
		priv->line_control &= ~CONTROL_RESET;

	if (priv->cmd_ctrl) {
		priv->cmd_count++;
730
		dev_dbg(dev, "%s - line control command being issued\n", __func__);
L
Linus Torvalds 已提交
731 732 733 734 735
		spin_unlock_irqrestore(&priv->lock, flags);
		goto send;
	} else
		spin_unlock_irqrestore(&priv->lock, flags);

736 737 738 739
	count = kfifo_out_locked(&priv->write_fifo,
					&port->interrupt_out_buffer[offset],
					port->interrupt_out_size - offset,
					&priv->lock);
A
Alan Cox 已提交
740
	if (count == 0)
L
Linus Torvalds 已提交
741 742
		return;

743 744 745 746 747 748 749
	switch (priv->pkt_fmt) {
	default:
	case packet_format_1:
		port->interrupt_out_buffer[1] = count;
		break;
	case packet_format_2:
		port->interrupt_out_buffer[0] |= count;
L
Linus Torvalds 已提交
750 751
	}

752
	dev_dbg(dev, "%s - count is %d\n", __func__, count);
L
Linus Torvalds 已提交
753 754 755 756 757 758 759 760 761

send:
	spin_lock_irqsave(&priv->lock, flags);
	priv->write_urb_in_use = 1;
	spin_unlock_irqrestore(&priv->lock, flags);

	if (priv->cmd_ctrl)
		actual_size = 1;
	else
762 763 764
		actual_size = count +
			      (priv->pkt_fmt == packet_format_1 ? 2 : 1);

765 766
	usb_serial_debug_data(dev, __func__, port->interrupt_out_size,
			      port->interrupt_out_urb->transfer_buffer);
L
Linus Torvalds 已提交
767

768 769
	usb_fill_int_urb(port->interrupt_out_urb, port->serial->dev,
		usb_sndintpipe(port->serial->dev, port->interrupt_out_endpointAddress),
770
		port->interrupt_out_buffer, actual_size,
771
		cypress_write_int_callback, port, priv->write_urb_interval);
A
Alan Cox 已提交
772
	result = usb_submit_urb(port->interrupt_out_urb, GFP_ATOMIC);
L
Linus Torvalds 已提交
773
	if (result) {
774
		dev_err_console(port,
A
Alan Cox 已提交
775 776
				"%s - failed submitting write urb, error %d\n",
							__func__, result);
L
Linus Torvalds 已提交
777
		priv->write_urb_in_use = 0;
778
		cypress_set_dead(port);
L
Linus Torvalds 已提交
779 780 781
	}

	spin_lock_irqsave(&priv->lock, flags);
A
Alan Cox 已提交
782
	if (priv->cmd_ctrl)
L
Linus Torvalds 已提交
783
		priv->cmd_ctrl = 0;
A
Alan Cox 已提交
784 785 786

	/* do not count the line control and size bytes */
	priv->bytes_out += count;
L
Linus Torvalds 已提交
787 788
	spin_unlock_irqrestore(&priv->lock, flags);

789
	usb_serial_port_softint(port);
L
Linus Torvalds 已提交
790 791 792 793
} /* cypress_send */


/* returns how much space is available in the soft buffer */
A
Alan Cox 已提交
794
static int cypress_write_room(struct tty_struct *tty)
L
Linus Torvalds 已提交
795
{
A
Alan Cox 已提交
796
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
797 798 799 800 801
	struct cypress_private *priv = usb_get_serial_port_data(port);
	int room = 0;
	unsigned long flags;

	spin_lock_irqsave(&priv->lock, flags);
802
	room = kfifo_avail(&priv->write_fifo);
L
Linus Torvalds 已提交
803 804
	spin_unlock_irqrestore(&priv->lock, flags);

805
	dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
L
Linus Torvalds 已提交
806 807 808 809
	return room;
}


810
static int cypress_tiocmget(struct tty_struct *tty)
L
Linus Torvalds 已提交
811
{
A
Alan Cox 已提交
812
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
813 814 815 816
	struct cypress_private *priv = usb_get_serial_port_data(port);
	__u8 status, control;
	unsigned int result = 0;
	unsigned long flags;
A
Alan Cox 已提交
817

L
Linus Torvalds 已提交
818 819 820 821 822 823 824 825 826 827 828 829
	spin_lock_irqsave(&priv->lock, flags);
	control = priv->line_control;
	status = priv->current_status;
	spin_unlock_irqrestore(&priv->lock, flags);

	result = ((control & CONTROL_DTR)        ? TIOCM_DTR : 0)
		| ((control & CONTROL_RTS)       ? TIOCM_RTS : 0)
		| ((status & UART_CTS)        ? TIOCM_CTS : 0)
		| ((status & UART_DSR)        ? TIOCM_DSR : 0)
		| ((status & UART_RI)         ? TIOCM_RI  : 0)
		| ((status & UART_CD)         ? TIOCM_CD  : 0);

830
	dev_dbg(&port->dev, "%s - result = %x\n", __func__, result);
L
Linus Torvalds 已提交
831 832 833 834 835

	return result;
}


836
static int cypress_tiocmset(struct tty_struct *tty,
L
Linus Torvalds 已提交
837 838
			       unsigned int set, unsigned int clear)
{
A
Alan Cox 已提交
839
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
840 841
	struct cypress_private *priv = usb_get_serial_port_data(port);
	unsigned long flags;
A
Alan Cox 已提交
842

L
Linus Torvalds 已提交
843 844 845 846 847 848 849 850 851
	spin_lock_irqsave(&priv->lock, flags);
	if (set & TIOCM_RTS)
		priv->line_control |= CONTROL_RTS;
	if (set & TIOCM_DTR)
		priv->line_control |= CONTROL_DTR;
	if (clear & TIOCM_RTS)
		priv->line_control &= ~CONTROL_RTS;
	if (clear & TIOCM_DTR)
		priv->line_control &= ~CONTROL_DTR;
A
Alan Cox 已提交
852
	priv->cmd_ctrl = 1;
L
Linus Torvalds 已提交
853 854
	spin_unlock_irqrestore(&priv->lock, flags);

A
Alan Cox 已提交
855
	return cypress_write(tty, port, NULL, 0);
L
Linus Torvalds 已提交
856 857
}

858 859 860 861 862
static void cypress_earthmate_init_termios(struct tty_struct *tty)
{
	tty_encode_baud_rate(tty, 4800, 4800);
}

A
Alan Cox 已提交
863 864
static void cypress_set_termios(struct tty_struct *tty,
	struct usb_serial_port *port, struct ktermios *old_termios)
L
Linus Torvalds 已提交
865 866
{
	struct cypress_private *priv = usb_get_serial_port_data(port);
867
	struct device *dev = &port->dev;
L
Linus Torvalds 已提交
868
	int data_bits, stop_bits, parity_type, parity_enable;
869
	unsigned int cflag;
L
Linus Torvalds 已提交
870 871
	unsigned long flags;
	__u8 oldlines;
872
	int linechange = 0;
873

A
Alan Cox 已提交
874
	/* Unsupported features need clearing */
875
	tty->termios.c_cflag &= ~(CMSPAR|CRTSCTS);
A
Alan Cox 已提交
876

877
	cflag = tty->termios.c_cflag;
L
Linus Torvalds 已提交
878 879 880 881

	/* set number of data bits, parity, stop bits */
	/* when parity is disabled the parity type bit is ignored */

882 883 884
	/* 1 means 2 stop bits, 0 means 1 stop bit */
	stop_bits = cflag & CSTOPB ? 1 : 0;

L
Linus Torvalds 已提交
885 886
	if (cflag & PARENB) {
		parity_enable = 1;
887 888
		/* 1 means odd parity, 0 means even parity */
		parity_type = cflag & PARODD ? 1 : 0;
L
Linus Torvalds 已提交
889 890 891
	} else
		parity_enable = parity_type = 0;

892 893 894 895 896 897 898 899 900 901 902
	switch (cflag & CSIZE) {
	case CS5:
		data_bits = 0;
		break;
	case CS6:
		data_bits = 1;
		break;
	case CS7:
		data_bits = 2;
		break;
	case CS8:
L
Linus Torvalds 已提交
903
		data_bits = 3;
904 905
		break;
	default:
906
		dev_err(dev, "%s - CSIZE was set, but not CS5-CS8\n", __func__);
907 908
		data_bits = 3;
	}
L
Linus Torvalds 已提交
909 910 911 912
	spin_lock_irqsave(&priv->lock, flags);
	oldlines = priv->line_control;
	if ((cflag & CBAUD) == B0) {
		/* drop dtr and rts */
913
		dev_dbg(dev, "%s - dropping the lines, baud rate 0bps\n", __func__);
L
Linus Torvalds 已提交
914
		priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
A
Alan Cox 已提交
915
	} else
916
		priv->line_control = (CONTROL_DTR | CONTROL_RTS);
L
Linus Torvalds 已提交
917 918
	spin_unlock_irqrestore(&priv->lock, flags);

919 920
	dev_dbg(dev, "%s - sending %d stop_bits, %d parity_enable, %d parity_type, %d data_bits (+5)\n",
		__func__, stop_bits, parity_enable, parity_type, data_bits);
921

A
Alan Cox 已提交
922 923 924 925
	cypress_serial_control(tty, port, tty_get_baud_rate(tty),
			data_bits, stop_bits,
			parity_enable, parity_type,
			0, CYPRESS_SET_CONFIG);
L
Linus Torvalds 已提交
926

927 928 929
	/* we perform a CYPRESS_GET_CONFIG so that the current settings are
	 * filled into the private structure this should confirm that all is
	 * working if it returns what we just set */
A
Alan Cox 已提交
930
	cypress_serial_control(tty, port, 0, 0, 0, 0, 0, 0, CYPRESS_GET_CONFIG);
L
Linus Torvalds 已提交
931

932 933
	/* Here we can define custom tty settings for devices; the main tty
	 * termios flag base comes from empeg.c */
L
Linus Torvalds 已提交
934

935
	spin_lock_irqsave(&priv->lock, flags);
A
Alan Cox 已提交
936
	if (priv->chiptype == CT_EARTHMATE && priv->baud_rate == 4800) {
937
		dev_dbg(dev, "Using custom termios settings for a baud rate of 4800bps.\n");
L
Linus Torvalds 已提交
938 939
		/* define custom termios settings for NMEA protocol */

940
		tty->termios.c_iflag /* input modes - */
941 942 943 944 945 946 947 948 949
			&= ~(IGNBRK  /* disable ignore break */
			| BRKINT     /* disable break causes interrupt */
			| PARMRK     /* disable mark parity errors */
			| ISTRIP     /* disable clear high bit of input char */
			| INLCR      /* disable translate NL to CR */
			| IGNCR      /* disable ignore CR */
			| ICRNL      /* disable translate CR to NL */
			| IXON);     /* disable enable XON/XOFF flow control */

950
		tty->termios.c_oflag /* output modes */
951
			&= ~OPOST;    /* disable postprocess output char */
L
Linus Torvalds 已提交
952

953
		tty->termios.c_lflag /* line discipline modes */
954 955 956 957 958 959 960
			&= ~(ECHO     /* disable echo input characters */
			| ECHONL      /* disable echo new line */
			| ICANON      /* disable erase, kill, werase, and rprnt
					 special characters */
			| ISIG        /* disable interrupt, quit, and suspend
					 special characters */
			| IEXTEN);    /* disable non-POSIX special characters */
961
	} /* CT_CYPHIDCOM: Application should handle this for device */
L
Linus Torvalds 已提交
962 963 964 965 966

	linechange = (priv->line_control != oldlines);
	spin_unlock_irqrestore(&priv->lock, flags);

	/* if necessary, set lines */
967
	if (linechange) {
L
Linus Torvalds 已提交
968
		priv->cmd_ctrl = 1;
A
Alan Cox 已提交
969
		cypress_write(tty, port, NULL, 0);
L
Linus Torvalds 已提交
970 971 972
	}
} /* cypress_set_termios */

973

L
Linus Torvalds 已提交
974
/* returns amount of data still left in soft buffer */
A
Alan Cox 已提交
975
static int cypress_chars_in_buffer(struct tty_struct *tty)
L
Linus Torvalds 已提交
976
{
A
Alan Cox 已提交
977
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
978 979 980 981 982
	struct cypress_private *priv = usb_get_serial_port_data(port);
	int chars = 0;
	unsigned long flags;

	spin_lock_irqsave(&priv->lock, flags);
983
	chars = kfifo_len(&priv->write_fifo);
L
Linus Torvalds 已提交
984 985
	spin_unlock_irqrestore(&priv->lock, flags);

986
	dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
L
Linus Torvalds 已提交
987 988 989 990
	return chars;
}


A
Alan Cox 已提交
991
static void cypress_throttle(struct tty_struct *tty)
L
Linus Torvalds 已提交
992
{
A
Alan Cox 已提交
993
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
994 995
	struct cypress_private *priv = usb_get_serial_port_data(port);

996
	spin_lock_irq(&priv->lock);
L
Linus Torvalds 已提交
997
	priv->rx_flags = THROTTLED;
998
	spin_unlock_irq(&priv->lock);
L
Linus Torvalds 已提交
999 1000 1001
}


A
Alan Cox 已提交
1002
static void cypress_unthrottle(struct tty_struct *tty)
L
Linus Torvalds 已提交
1003
{
A
Alan Cox 已提交
1004
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
1005 1006 1007
	struct cypress_private *priv = usb_get_serial_port_data(port);
	int actually_throttled, result;

1008
	spin_lock_irq(&priv->lock);
L
Linus Torvalds 已提交
1009 1010
	actually_throttled = priv->rx_flags & ACTUALLY_THROTTLED;
	priv->rx_flags = 0;
1011
	spin_unlock_irq(&priv->lock);
L
Linus Torvalds 已提交
1012

1013 1014 1015
	if (!priv->comm_is_ok)
		return;

L
Linus Torvalds 已提交
1016
	if (actually_throttled) {
1017
		result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
1018
		if (result) {
1019
			dev_err(&port->dev, "%s - failed submitting read urb, "
1020
					"error %d\n", __func__, result);
1021 1022
			cypress_set_dead(port);
		}
L
Linus Torvalds 已提交
1023 1024 1025 1026
	}
}


1027
static void cypress_read_int_callback(struct urb *urb)
L
Linus Torvalds 已提交
1028
{
1029
	struct usb_serial_port *port = urb->context;
L
Linus Torvalds 已提交
1030
	struct cypress_private *priv = usb_get_serial_port_data(port);
1031
	struct device *dev = &urb->dev->dev;
L
Linus Torvalds 已提交
1032 1033 1034
	struct tty_struct *tty;
	unsigned char *data = urb->transfer_buffer;
	unsigned long flags;
1035
	char tty_flag = TTY_NORMAL;
L
Linus Torvalds 已提交
1036 1037 1038
	int bytes = 0;
	int result;
	int i = 0;
1039
	int status = urb->status;
L
Linus Torvalds 已提交
1040

1041
	switch (status) {
1042 1043 1044 1045 1046 1047 1048 1049
	case 0: /* success */
		break;
	case -ECONNRESET:
	case -ENOENT:
	case -ESHUTDOWN:
		/* precursor to disconnect so just go away */
		return;
	case -EPIPE:
1050
		/* Can't call usb_clear_halt while in_interrupt */
1051
		fallthrough;
1052 1053
	default:
		/* something ugly is going on... */
1054 1055
		dev_err(dev, "%s - unexpected nonzero read status received: %d\n",
			__func__, status);
1056
		cypress_set_dead(port);
L
Linus Torvalds 已提交
1057 1058 1059 1060 1061
		return;
	}

	spin_lock_irqsave(&priv->lock, flags);
	if (priv->rx_flags & THROTTLED) {
1062
		dev_dbg(dev, "%s - now throttling\n", __func__);
L
Linus Torvalds 已提交
1063 1064 1065 1066 1067 1068
		priv->rx_flags |= ACTUALLY_THROTTLED;
		spin_unlock_irqrestore(&priv->lock, flags);
		return;
	}
	spin_unlock_irqrestore(&priv->lock, flags);

A
Alan Cox 已提交
1069
	tty = tty_port_tty_get(&port->port);
L
Linus Torvalds 已提交
1070
	if (!tty) {
1071
		dev_dbg(dev, "%s - bad tty pointer - exiting\n", __func__);
L
Linus Torvalds 已提交
1072 1073 1074 1075
		return;
	}

	spin_lock_irqsave(&priv->lock, flags);
1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090
	result = urb->actual_length;
	switch (priv->pkt_fmt) {
	default:
	case packet_format_1:
		/* This is for the CY7C64013... */
		priv->current_status = data[0] & 0xF8;
		bytes = data[1] + 2;
		i = 2;
		break;
	case packet_format_2:
		/* This is for the CY7C63743... */
		priv->current_status = data[0] & 0xF8;
		bytes = (data[0] & 0x07) + 1;
		i = 1;
		break;
L
Linus Torvalds 已提交
1091 1092
	}
	spin_unlock_irqrestore(&priv->lock, flags);
1093
	if (result < bytes) {
1094 1095 1096
		dev_dbg(dev,
			"%s - wrong packet size - received %d bytes but packet said %d bytes\n",
			__func__, result, bytes);
1097 1098
		goto continue_read;
	}
L
Linus Torvalds 已提交
1099

1100
	usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data);
L
Linus Torvalds 已提交
1101 1102 1103

	spin_lock_irqsave(&priv->lock, flags);
	/* check to see if status has changed */
1104
	if (priv->current_status != priv->prev_status) {
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115
		u8 delta = priv->current_status ^ priv->prev_status;

		if (delta & UART_MSR_MASK) {
			if (delta & UART_CTS)
				port->icount.cts++;
			if (delta & UART_DSR)
				port->icount.dsr++;
			if (delta & UART_RI)
				port->icount.rng++;
			if (delta & UART_CD)
				port->icount.dcd++;
1116 1117

			wake_up_interruptible(&port->port.delta_msr_wait);
1118
		}
1119

1120
		priv->prev_status = priv->current_status;
L
Linus Torvalds 已提交
1121
	}
1122
	spin_unlock_irqrestore(&priv->lock, flags);
L
Linus Torvalds 已提交
1123

1124 1125
	/* hangup, as defined in acm.c... this might be a bad place for it
	 * though */
P
Peter Hurley 已提交
1126
	if (tty && !C_CLOCAL(tty) && !(priv->current_status & UART_CD)) {
1127
		dev_dbg(dev, "%s - calling hangup\n", __func__);
L
Linus Torvalds 已提交
1128 1129 1130 1131
		tty_hangup(tty);
		goto continue_read;
	}

1132 1133 1134 1135
	/* There is one error bit... I'm assuming it is a parity error
	 * indicator as the generic firmware will set this bit to 1 if a
	 * parity error occurs.
	 * I can not find reference to any other error events. */
L
Linus Torvalds 已提交
1136 1137 1138 1139
	spin_lock_irqsave(&priv->lock, flags);
	if (priv->current_status & CYP_ERROR) {
		spin_unlock_irqrestore(&priv->lock, flags);
		tty_flag = TTY_PARITY;
1140
		dev_dbg(dev, "%s - Parity Error detected\n", __func__);
L
Linus Torvalds 已提交
1141 1142 1143 1144
	} else
		spin_unlock_irqrestore(&priv->lock, flags);

	/* process read if there is data other than line status */
J
Jiri Slaby 已提交
1145
	if (bytes > i) {
1146
		tty_insert_flip_string_fixed_flag(&port->port, data + i,
1147
				tty_flag, bytes - i);
J
Jiri Slaby 已提交
1148
		tty_flip_buffer_push(&port->port);
L
Linus Torvalds 已提交
1149 1150 1151
	}

	spin_lock_irqsave(&priv->lock, flags);
1152 1153
	/* control and status byte(s) are also counted */
	priv->bytes_in += bytes;
L
Linus Torvalds 已提交
1154 1155 1156
	spin_unlock_irqrestore(&priv->lock, flags);

continue_read:
A
Alan Cox 已提交
1157
	tty_kref_put(tty);
1158

1159
	/* Continue trying to always read */
L
Linus Torvalds 已提交
1160

1161
	if (priv->comm_is_ok) {
1162 1163 1164 1165 1166
		usb_fill_int_urb(port->interrupt_in_urb, port->serial->dev,
				usb_rcvintpipe(port->serial->dev,
					port->interrupt_in_endpointAddress),
				port->interrupt_in_urb->transfer_buffer,
				port->interrupt_in_urb->transfer_buffer_length,
A
Alan Cox 已提交
1167 1168
				cypress_read_int_callback, port,
				priv->read_urb_interval);
1169
		result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
1170
		if (result && result != -EPERM) {
1171 1172
			dev_err(dev, "%s - failed resubmitting read urb, error %d\n",
				__func__, result);
1173 1174
			cypress_set_dead(port);
		}
L
Linus Torvalds 已提交
1175 1176 1177 1178
	}
} /* cypress_read_int_callback */


1179
static void cypress_write_int_callback(struct urb *urb)
L
Linus Torvalds 已提交
1180
{
1181
	struct usb_serial_port *port = urb->context;
L
Linus Torvalds 已提交
1182
	struct cypress_private *priv = usb_get_serial_port_data(port);
1183
	struct device *dev = &urb->dev->dev;
1184
	int status = urb->status;
L
Linus Torvalds 已提交
1185

1186
	switch (status) {
A
Alan Cox 已提交
1187 1188 1189 1190 1191 1192 1193
	case 0:
		/* success */
		break;
	case -ECONNRESET:
	case -ENOENT:
	case -ESHUTDOWN:
		/* this urb is terminated, clean up */
1194 1195
		dev_dbg(dev, "%s - urb shutting down with status: %d\n",
			__func__, status);
A
Alan Cox 已提交
1196 1197
		priv->write_urb_in_use = 0;
		return;
1198 1199
	case -EPIPE:
		/* Cannot call usb_clear_halt while in_interrupt */
1200
		fallthrough;
A
Alan Cox 已提交
1201
	default:
1202 1203
		dev_err(dev, "%s - unexpected nonzero write status received: %d\n",
			__func__, status);
A
Alan Cox 已提交
1204 1205
		cypress_set_dead(port);
		break;
L
Linus Torvalds 已提交
1206 1207
	}
	priv->write_urb_in_use = 0;
A
Alan Cox 已提交
1208

L
Linus Torvalds 已提交
1209 1210 1211 1212
	/* send any buffered data */
	cypress_send(port);
}

1213
module_usb_serial_driver(serial_drivers, id_table_combined);
L
Linus Torvalds 已提交
1214

A
Alan Cox 已提交
1215 1216
MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);
L
Linus Torvalds 已提交
1217 1218 1219 1220
MODULE_LICENSE("GPL");

module_param(stats, bool, S_IRUGO | S_IWUSR);
MODULE_PARM_DESC(stats, "Enable statistics or not");
1221 1222
module_param(interval, int, S_IRUGO | S_IWUSR);
MODULE_PARM_DESC(interval, "Overrides interrupt interval");
1223 1224
module_param(unstable_bauds, bool, S_IRUGO | S_IWUSR);
MODULE_PARM_DESC(unstable_bauds, "Allow unstable baud rates");