blob: bc5057513d5d69963b33abd9296d2e0ca5253613 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * USB Cypress M8 driver
3 *
4 * Copyright (C) 2004
Alan Cox813a2242008-07-22 11:10:36 +01005 * Lonnie Mendez (dignome@gmail.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Copyright (C) 2003,2004
7 * Neil Whelchel (koyama@firstlight.net)
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
Alan Cox813a2242008-07-22 11:10:36 +010014 * See Documentation/usb/usb-serial.txt for more information on using this
15 * driver
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 *
17 * See http://geocities.com/i0xox0i for information on this driver and the
18 * earthmate usb device.
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 */
20
Alan Cox813a2242008-07-22 11:10:36 +010021/* Thanks to Neil Whelchel for writing the first cypress m8 implementation
22 for linux. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070023/* Thanks to cypress for providing references for the hid reports. */
24/* Thanks to Jiang Zhang for providing links and for general help. */
Alan Cox813a2242008-07-22 11:10:36 +010025/* Code originates and was built up from ftdi_sio, belkin, pl2303 and others.*/
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/kernel.h>
29#include <linux/errno.h>
30#include <linux/init.h>
31#include <linux/slab.h>
32#include <linux/tty.h>
33#include <linux/tty_driver.h>
34#include <linux/tty_flip.h>
35#include <linux/module.h>
36#include <linux/moduleparam.h>
37#include <linux/spinlock.h>
38#include <linux/usb.h>
Greg Kroah-Hartmana9698882006-07-11 21:22:58 -070039#include <linux/usb/serial.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/serial.h>
Johan Hovold117fb8d2010-05-16 20:33:50 +020041#include <linux/kfifo.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/delay.h>
Alan Cox813a2242008-07-22 11:10:36 +010043#include <linux/uaccess.h>
Johan Hovold0f2c2d72009-12-31 16:48:01 +010044#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include "cypress_m8.h"
47
48
Rusty Russell90ab5ee2012-01-13 09:32:20 +103049static bool debug;
50static bool stats;
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -050051static int interval;
Rusty Russell90ab5ee2012-01-13 09:32:20 +103052static bool unstable_bauds;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54/*
55 * Version Information
56 */
Johan Hovold117fb8d2010-05-16 20:33:50 +020057#define DRIVER_VERSION "v1.10"
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#define DRIVER_AUTHOR "Lonnie Mendez <dignome@gmail.com>, Neil Whelchel <koyama@firstlight.net>"
59#define DRIVER_DESC "Cypress USB to Serial Driver"
60
61/* write buffer size defines */
62#define CYPRESS_BUF_SIZE 1024
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Németh Márton7d40d7e2010-01-10 15:34:24 +010064static const struct usb_device_id id_table_earthmate[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB) },
Lonnie Mendez25b6f082005-05-10 17:09:52 -050066 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB_LT20) },
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 { } /* Terminating entry */
68};
69
Németh Márton7d40d7e2010-01-10 15:34:24 +010070static const struct usb_device_id id_table_cyphidcomrs232[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 { USB_DEVICE(VENDOR_ID_CYPRESS, PRODUCT_ID_CYPHIDCOM) },
Dmitry Shapin6f6f06e2008-03-04 15:25:10 -080072 { USB_DEVICE(VENDOR_ID_POWERCOM, PRODUCT_ID_UPS) },
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 { } /* Terminating entry */
74};
75
Németh Márton7d40d7e2010-01-10 15:34:24 +010076static const struct usb_device_id id_table_nokiaca42v2[] = {
Lonnie Mendeza5c44e22006-03-01 10:45:24 -060077 { USB_DEVICE(VENDOR_ID_DAZZLE, PRODUCT_ID_CA42) },
78 { } /* Terminating entry */
79};
80
Németh Márton7d40d7e2010-01-10 15:34:24 +010081static const struct usb_device_id id_table_combined[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB) },
Lonnie Mendez25b6f082005-05-10 17:09:52 -050083 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB_LT20) },
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 { USB_DEVICE(VENDOR_ID_CYPRESS, PRODUCT_ID_CYPHIDCOM) },
Dmitry Shapin6f6f06e2008-03-04 15:25:10 -080085 { USB_DEVICE(VENDOR_ID_POWERCOM, PRODUCT_ID_UPS) },
Lonnie Mendeza5c44e22006-03-01 10:45:24 -060086 { USB_DEVICE(VENDOR_ID_DAZZLE, PRODUCT_ID_CA42) },
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 { } /* Terminating entry */
88};
89
Alan Cox813a2242008-07-22 11:10:36 +010090MODULE_DEVICE_TABLE(usb, id_table_combined);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92static struct usb_driver cypress_driver = {
93 .name = "cypress",
94 .probe = usb_serial_probe,
95 .disconnect = usb_serial_disconnect,
96 .id_table = id_table_combined,
97};
98
Mike Isely3416eaa2008-02-10 20:23:19 -060099enum packet_format {
100 packet_format_1, /* b0:status, b1:payload count */
101 packet_format_2 /* b0[7:3]:status, b0[2:0]:payload count */
102};
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104struct cypress_private {
105 spinlock_t lock; /* private lock */
106 int chiptype; /* identifier of device, for quirks/etc */
107 int bytes_in; /* used for statistics */
108 int bytes_out; /* used for statistics */
109 int cmd_count; /* used for statistics */
110 int cmd_ctrl; /* always set this to 1 before issuing a command */
Johan Hovold117fb8d2010-05-16 20:33:50 +0200111 struct kfifo write_fifo; /* write fifo */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 int write_urb_in_use; /* write urb in use indicator */
Mike Isely0257fa92006-08-29 22:06:59 -0500113 int write_urb_interval; /* interval to use for write urb */
114 int read_urb_interval; /* interval to use for read urb */
Mike Isely78aef512006-08-29 22:07:11 -0500115 int comm_is_ok; /* true if communication is (still) ok */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 int termios_initialized;
117 __u8 line_control; /* holds dtr / rts value */
118 __u8 current_status; /* received from last read - info on dsr,cts,cd,ri,etc */
119 __u8 current_config; /* stores the current configuration byte */
120 __u8 rx_flags; /* throttling - used from whiteheat/ftdi_sio */
Mike Isely3416eaa2008-02-10 20:23:19 -0600121 enum packet_format pkt_fmt; /* format to use for packet send / receive */
Mike Isely3d6aa322008-02-10 20:23:24 -0600122 int get_cfg_unsafe; /* If true, the CYPRESS_GET_CONFIG is unsafe */
Alan Cox813a2242008-07-22 11:10:36 +0100123 int baud_rate; /* stores current baud rate in
124 integer form */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 int isthrottled; /* if throttled, discard reads */
126 wait_queue_head_t delta_msr_wait; /* used for TIOCMIWAIT */
127 char prev_status, diff_status; /* used for TIOCMIWAIT */
Daniel Mack3ad2f3f2010-02-03 08:01:28 +0800128 /* we pass a pointer to this as the argument sent to
Alan Cox813a2242008-07-22 11:10:36 +0100129 cypress_set_termios old_termios */
Alan Cox606d0992006-12-08 02:38:45 -0800130 struct ktermios tmp_termios; /* stores the old termios settings */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131};
132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133/* function prototypes for the Cypress USB to serial device */
Alan Cox813a2242008-07-22 11:10:36 +0100134static int cypress_earthmate_startup(struct usb_serial *serial);
135static int cypress_hidcom_startup(struct usb_serial *serial);
136static int cypress_ca42v2_startup(struct usb_serial *serial);
Alan Sternf9c99bb2009-06-02 11:53:55 -0400137static void cypress_release(struct usb_serial *serial);
Alan Coxa509a7e2009-09-19 13:13:26 -0700138static int cypress_open(struct tty_struct *tty, struct usb_serial_port *port);
Alan Cox335f8512009-06-11 12:26:29 +0100139static void cypress_close(struct usb_serial_port *port);
140static void cypress_dtr_rts(struct usb_serial_port *port, int on);
Alan Cox813a2242008-07-22 11:10:36 +0100141static int cypress_write(struct tty_struct *tty, struct usb_serial_port *port,
142 const unsigned char *buf, int count);
143static void cypress_send(struct usb_serial_port *port);
144static int cypress_write_room(struct tty_struct *tty);
Alan Cox00a0d0d2011-02-14 16:27:06 +0000145static int cypress_ioctl(struct tty_struct *tty,
Alan Cox813a2242008-07-22 11:10:36 +0100146 unsigned int cmd, unsigned long arg);
147static void cypress_set_termios(struct tty_struct *tty,
148 struct usb_serial_port *port, struct ktermios *old);
Alan Cox60b33c12011-02-14 16:26:14 +0000149static int cypress_tiocmget(struct tty_struct *tty);
Alan Cox20b9d172011-02-14 16:26:50 +0000150static int cypress_tiocmset(struct tty_struct *tty,
Alan Cox813a2242008-07-22 11:10:36 +0100151 unsigned int set, unsigned int clear);
152static int cypress_chars_in_buffer(struct tty_struct *tty);
153static void cypress_throttle(struct tty_struct *tty);
154static void cypress_unthrottle(struct tty_struct *tty);
155static void cypress_set_dead(struct usb_serial_port *port);
156static void cypress_read_int_callback(struct urb *urb);
157static void cypress_write_int_callback(struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700159static struct usb_serial_driver cypress_earthmate_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700160 .driver = {
161 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700162 .name = "earthmate",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700163 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700164 .description = "DeLorme Earthmate USB",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 .id_table = id_table_earthmate,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 .num_ports = 1,
167 .attach = cypress_earthmate_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -0400168 .release = cypress_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 .open = cypress_open,
170 .close = cypress_close,
Alan Cox335f8512009-06-11 12:26:29 +0100171 .dtr_rts = cypress_dtr_rts,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 .write = cypress_write,
173 .write_room = cypress_write_room,
174 .ioctl = cypress_ioctl,
175 .set_termios = cypress_set_termios,
176 .tiocmget = cypress_tiocmget,
177 .tiocmset = cypress_tiocmset,
178 .chars_in_buffer = cypress_chars_in_buffer,
179 .throttle = cypress_throttle,
180 .unthrottle = cypress_unthrottle,
181 .read_int_callback = cypress_read_int_callback,
182 .write_int_callback = cypress_write_int_callback,
183};
184
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700185static struct usb_serial_driver cypress_hidcom_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700186 .driver = {
187 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700188 .name = "cyphidcom",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700189 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700190 .description = "HID->COM RS232 Adapter",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 .id_table = id_table_cyphidcomrs232,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 .num_ports = 1,
193 .attach = cypress_hidcom_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -0400194 .release = cypress_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 .open = cypress_open,
196 .close = cypress_close,
Alan Cox335f8512009-06-11 12:26:29 +0100197 .dtr_rts = cypress_dtr_rts,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 .write = cypress_write,
199 .write_room = cypress_write_room,
200 .ioctl = cypress_ioctl,
201 .set_termios = cypress_set_termios,
202 .tiocmget = cypress_tiocmget,
203 .tiocmset = cypress_tiocmset,
204 .chars_in_buffer = cypress_chars_in_buffer,
205 .throttle = cypress_throttle,
206 .unthrottle = cypress_unthrottle,
207 .read_int_callback = cypress_read_int_callback,
208 .write_int_callback = cypress_write_int_callback,
209};
210
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600211static struct usb_serial_driver cypress_ca42v2_device = {
212 .driver = {
213 .owner = THIS_MODULE,
Alan Cox813a2242008-07-22 11:10:36 +0100214 .name = "nokiaca42v2",
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600215 },
216 .description = "Nokia CA-42 V2 Adapter",
217 .id_table = id_table_nokiaca42v2,
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600218 .num_ports = 1,
219 .attach = cypress_ca42v2_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -0400220 .release = cypress_release,
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600221 .open = cypress_open,
222 .close = cypress_close,
Alan Cox335f8512009-06-11 12:26:29 +0100223 .dtr_rts = cypress_dtr_rts,
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600224 .write = cypress_write,
225 .write_room = cypress_write_room,
226 .ioctl = cypress_ioctl,
227 .set_termios = cypress_set_termios,
228 .tiocmget = cypress_tiocmget,
229 .tiocmset = cypress_tiocmset,
230 .chars_in_buffer = cypress_chars_in_buffer,
231 .throttle = cypress_throttle,
232 .unthrottle = cypress_unthrottle,
233 .read_int_callback = cypress_read_int_callback,
234 .write_int_callback = cypress_write_int_callback,
235};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Alan Stern08a4f6b2012-02-23 14:56:17 -0500237static struct usb_serial_driver * const serial_drivers[] = {
238 &cypress_earthmate_device, &cypress_hidcom_device,
239 &cypress_ca42v2_device, NULL
240};
241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242/*****************************************************************************
243 * Cypress serial helper functions
244 *****************************************************************************/
245
246
Alan Cox8873aaa2008-03-10 21:59:28 +0000247static int analyze_baud_rate(struct usb_serial_port *port, speed_t new_rate)
Mike Isely92983c22008-02-10 20:23:32 -0600248{
Mike Isely92983c22008-02-10 20:23:32 -0600249 struct cypress_private *priv;
250 priv = usb_get_serial_port_data(port);
251
Mike Frysingerc3126592009-12-18 16:33:03 -0500252 if (unstable_bauds)
253 return new_rate;
254
Mike Isely92983c22008-02-10 20:23:32 -0600255 /*
256 * The general purpose firmware for the Cypress M8 allows for
257 * a maximum speed of 57600bps (I have no idea whether DeLorme
258 * chose to use the general purpose firmware or not), if you
259 * need to modify this speed setting for your own project
260 * please add your own chiptype and modify the code likewise.
261 * The Cypress HID->COM device will work successfully up to
262 * 115200bps (but the actual throughput is around 3kBps).
263 */
Mike Isely92983c22008-02-10 20:23:32 -0600264 if (port->serial->dev->speed == USB_SPEED_LOW) {
265 /*
266 * Mike Isely <isely@pobox.com> 2-Feb-2008: The
267 * Cypress app note that describes this mechanism
268 * states the the low-speed part can't handle more
269 * than 800 bytes/sec, in which case 4800 baud is the
270 * safest speed for a part like that.
271 */
272 if (new_rate > 4800) {
273 dbg("%s - failed setting baud rate, device incapable "
274 "speed %d", __func__, new_rate);
275 return -1;
276 }
277 }
278 switch (priv->chiptype) {
279 case CT_EARTHMATE:
280 if (new_rate <= 600) {
281 /* 300 and 600 baud rates are supported under
282 * the generic firmware, but are not used with
283 * NMEA and SiRF protocols */
284 dbg("%s - failed setting baud rate, unsupported speed "
285 "of %d on Earthmate GPS", __func__, new_rate);
286 return -1;
287 }
288 break;
289 default:
290 break;
291 }
292 return new_rate;
293}
294
295
Steven Cole093cf722005-05-03 19:07:24 -0600296/* This function can either set or retrieve the current serial line settings */
Alan Cox813a2242008-07-22 11:10:36 +0100297static int cypress_serial_control(struct tty_struct *tty,
Alan Cox95da3102008-07-22 11:09:07 +0100298 struct usb_serial_port *port, speed_t baud_rate, int data_bits,
299 int stop_bits, int parity_enable, int parity_type, int reset,
300 int cypress_request_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500302 int new_baudrate = 0, retval = 0, tries = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 struct cypress_private *priv;
Johan Hovold09546442009-12-28 23:01:48 +0100304 u8 *feature_buffer;
305 const unsigned int feature_len = 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 unsigned long flags;
307
Harvey Harrison441b62c2008-03-03 16:08:34 -0800308 dbg("%s", __func__);
Alan Cox813a2242008-07-22 11:10:36 +0100309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 priv = usb_get_serial_port_data(port);
311
Mike Isely78aef512006-08-29 22:07:11 -0500312 if (!priv->comm_is_ok)
313 return -ENODEV;
314
Johan Hovold09546442009-12-28 23:01:48 +0100315 feature_buffer = kcalloc(feature_len, sizeof(u8), GFP_KERNEL);
316 if (!feature_buffer)
317 return -ENOMEM;
318
Alan Cox813a2242008-07-22 11:10:36 +0100319 switch (cypress_request_type) {
320 case CYPRESS_SET_CONFIG:
Alan Cox813a2242008-07-22 11:10:36 +0100321 /* 0 means 'Hang up' so doesn't change the true bit rate */
Mike Frysinger2805eb12009-12-18 16:33:02 -0500322 new_baudrate = priv->baud_rate;
323 if (baud_rate && baud_rate != priv->baud_rate) {
Alan Cox813a2242008-07-22 11:10:36 +0100324 dbg("%s - baud rate is changing", __func__);
325 retval = analyze_baud_rate(port, baud_rate);
Mike Frysinger2805eb12009-12-18 16:33:02 -0500326 if (retval >= 0) {
Alan Cox813a2242008-07-22 11:10:36 +0100327 new_baudrate = retval;
328 dbg("%s - New baud rate set to %d",
329 __func__, new_baudrate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 }
Alan Cox813a2242008-07-22 11:10:36 +0100331 }
332 dbg("%s - baud rate is being sent as %d",
333 __func__, new_baudrate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Alan Cox813a2242008-07-22 11:10:36 +0100335 /* fill the feature_buffer with new configuration */
Johan Hovold0f2c2d72009-12-31 16:48:01 +0100336 put_unaligned_le32(new_baudrate, feature_buffer);
Alan Cox813a2242008-07-22 11:10:36 +0100337 feature_buffer[4] |= data_bits; /* assign data bits in 2 bit space ( max 3 ) */
338 /* 1 bit gap */
339 feature_buffer[4] |= (stop_bits << 3); /* assign stop bits in 1 bit space */
340 feature_buffer[4] |= (parity_enable << 4); /* assign parity flag in 1 bit space */
341 feature_buffer[4] |= (parity_type << 5); /* assign parity type in 1 bit space */
342 /* 1 bit gap */
343 feature_buffer[4] |= (reset << 7); /* assign reset at end of byte, 1 bit space */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Alan Cox813a2242008-07-22 11:10:36 +0100345 dbg("%s - device is being sent this feature report:",
346 __func__);
347 dbg("%s - %02X - %02X - %02X - %02X - %02X", __func__,
348 feature_buffer[0], feature_buffer[1],
349 feature_buffer[2], feature_buffer[3],
350 feature_buffer[4]);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500351
Alan Cox813a2242008-07-22 11:10:36 +0100352 do {
353 retval = usb_control_msg(port->serial->dev,
354 usb_sndctrlpipe(port->serial->dev, 0),
355 HID_REQ_SET_REPORT,
356 USB_DIR_OUT | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
357 0x0300, 0, feature_buffer,
Johan Hovold09546442009-12-28 23:01:48 +0100358 feature_len, 500);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500359
Alan Cox813a2242008-07-22 11:10:36 +0100360 if (tries++ >= 3)
361 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Johan Hovold09546442009-12-28 23:01:48 +0100363 } while (retval != feature_len &&
Alan Cox813a2242008-07-22 11:10:36 +0100364 retval != -ENODEV);
Mike Isely93075542008-02-10 20:23:14 -0600365
Johan Hovold09546442009-12-28 23:01:48 +0100366 if (retval != feature_len) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700367 dev_err(&port->dev, "%s - failed sending serial "
368 "line settings - %d\n", __func__, retval);
Alan Cox813a2242008-07-22 11:10:36 +0100369 cypress_set_dead(port);
370 } else {
371 spin_lock_irqsave(&priv->lock, flags);
372 priv->baud_rate = new_baudrate;
373 priv->current_config = feature_buffer[4];
374 spin_unlock_irqrestore(&priv->lock, flags);
375 /* If we asked for a speed change encode it */
376 if (baud_rate)
377 tty_encode_baud_rate(tty,
378 new_baudrate, new_baudrate);
379 }
380 break;
381 case CYPRESS_GET_CONFIG:
382 if (priv->get_cfg_unsafe) {
383 /* Not implemented for this device,
384 and if we try to do it we're likely
385 to crash the hardware. */
Johan Hovold09546442009-12-28 23:01:48 +0100386 retval = -ENOTTY;
387 goto out;
Alan Cox813a2242008-07-22 11:10:36 +0100388 }
389 dbg("%s - retreiving serial line settings", __func__);
Alan Cox813a2242008-07-22 11:10:36 +0100390 do {
391 retval = usb_control_msg(port->serial->dev,
392 usb_rcvctrlpipe(port->serial->dev, 0),
393 HID_REQ_GET_REPORT,
394 USB_DIR_IN | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
395 0x0300, 0, feature_buffer,
Johan Hovold09546442009-12-28 23:01:48 +0100396 feature_len, 500);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500397
Alan Cox813a2242008-07-22 11:10:36 +0100398 if (tries++ >= 3)
399 break;
Johan Hovold09546442009-12-28 23:01:48 +0100400 } while (retval != feature_len
Alan Cox813a2242008-07-22 11:10:36 +0100401 && retval != -ENODEV);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500402
Johan Hovold09546442009-12-28 23:01:48 +0100403 if (retval != feature_len) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700404 dev_err(&port->dev, "%s - failed to retrieve serial "
405 "line settings - %d\n", __func__, retval);
Alan Cox813a2242008-07-22 11:10:36 +0100406 cypress_set_dead(port);
Johan Hovold09546442009-12-28 23:01:48 +0100407 goto out;
Alan Cox813a2242008-07-22 11:10:36 +0100408 } else {
409 spin_lock_irqsave(&priv->lock, flags);
410 /* store the config in one byte, and later
411 use bit masks to check values */
412 priv->current_config = feature_buffer[4];
Johan Hovold0f2c2d72009-12-31 16:48:01 +0100413 priv->baud_rate = get_unaligned_le32(feature_buffer);
Alan Cox813a2242008-07-22 11:10:36 +0100414 spin_unlock_irqrestore(&priv->lock, flags);
415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 }
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500417 spin_lock_irqsave(&priv->lock, flags);
418 ++priv->cmd_count;
419 spin_unlock_irqrestore(&priv->lock, flags);
Johan Hovold09546442009-12-28 23:01:48 +0100420out:
421 kfree(feature_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 return retval;
423} /* cypress_serial_control */
424
425
Mike Isely78aef512006-08-29 22:07:11 -0500426static void cypress_set_dead(struct usb_serial_port *port)
427{
428 struct cypress_private *priv = usb_get_serial_port_data(port);
429 unsigned long flags;
430
431 spin_lock_irqsave(&priv->lock, flags);
432 if (!priv->comm_is_ok) {
433 spin_unlock_irqrestore(&priv->lock, flags);
434 return;
435 }
436 priv->comm_is_ok = 0;
437 spin_unlock_irqrestore(&priv->lock, flags);
438
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700439 dev_err(&port->dev, "cypress_m8 suspending failing port %d - "
440 "interval might be too short\n", port->number);
Mike Isely78aef512006-08-29 22:07:11 -0500441}
442
443
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444/*****************************************************************************
445 * Cypress serial driver functions
446 *****************************************************************************/
447
448
Alan Cox813a2242008-07-22 11:10:36 +0100449static int generic_startup(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
451 struct cypress_private *priv;
Mike Isely0257fa92006-08-29 22:06:59 -0500452 struct usb_serial_port *port = serial->port[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Harvey Harrison441b62c2008-03-03 16:08:34 -0800454 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Alan Cox813a2242008-07-22 11:10:36 +0100456 priv = kzalloc(sizeof(struct cypress_private), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 if (!priv)
458 return -ENOMEM;
459
Mike Isely78aef512006-08-29 22:07:11 -0500460 priv->comm_is_ok = !0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 spin_lock_init(&priv->lock);
Johan Hovold117fb8d2010-05-16 20:33:50 +0200462 if (kfifo_alloc(&priv->write_fifo, CYPRESS_BUF_SIZE, GFP_KERNEL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 kfree(priv);
464 return -ENOMEM;
465 }
466 init_waitqueue_head(&priv->delta_msr_wait);
Alan Cox813a2242008-07-22 11:10:36 +0100467
468 usb_reset_configuration(serial->dev);
469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 priv->cmd_ctrl = 0;
471 priv->line_control = 0;
472 priv->termios_initialized = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 priv->rx_flags = 0;
Mike Isely3416eaa2008-02-10 20:23:19 -0600474 /* 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. */
Alan Cox813a2242008-07-22 11:10:36 +0100480 if (port->interrupt_out_size > 9)
Mike Isely3416eaa2008-02-10 20:23:19 -0600481 priv->pkt_fmt = packet_format_1;
Alan Cox813a2242008-07-22 11:10:36 +0100482 else
Mike Isely3416eaa2008-02-10 20:23:19 -0600483 priv->pkt_fmt = packet_format_2;
Alan Cox813a2242008-07-22 11:10:36 +0100484
Mike Isely0257fa92006-08-29 22:06:59 -0500485 if (interval > 0) {
486 priv->write_urb_interval = interval;
487 priv->read_urb_interval = interval;
488 dbg("%s - port %d read & write intervals forced to %d",
Alan Cox813a2242008-07-22 11:10:36 +0100489 __func__, port->number, interval);
Mike Isely0257fa92006-08-29 22:06:59 -0500490 } else {
491 priv->write_urb_interval = port->interrupt_out_urb->interval;
492 priv->read_urb_interval = port->interrupt_in_urb->interval;
493 dbg("%s - port %d intervals: read=%d write=%d",
Alan Cox813a2242008-07-22 11:10:36 +0100494 __func__, port->number,
495 priv->read_urb_interval, priv->write_urb_interval);
Mike Isely0257fa92006-08-29 22:06:59 -0500496 }
497 usb_set_serial_port_data(port, priv);
Alan Cox813a2242008-07-22 11:10:36 +0100498
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500499 return 0;
500}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502
Alan Cox813a2242008-07-22 11:10:36 +0100503static int cypress_earthmate_startup(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
505 struct cypress_private *priv;
Mike Isely3d6aa322008-02-10 20:23:24 -0600506 struct usb_serial_port *port = serial->port[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Harvey Harrison441b62c2008-03-03 16:08:34 -0800508 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
510 if (generic_startup(serial)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800511 dbg("%s - Failed setting up port %d", __func__,
Mike Isely3d6aa322008-02-10 20:23:24 -0600512 port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 return 1;
514 }
515
Mike Isely3d6aa322008-02-10 20:23:24 -0600516 priv = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 priv->chiptype = CT_EARTHMATE;
Mike Isely3416eaa2008-02-10 20:23:19 -0600518 /* All Earthmate devices use the separated-count packet
519 format! Idiotic. */
520 priv->pkt_fmt = packet_format_1;
Alan Cox813a2242008-07-22 11:10:36 +0100521 if (serial->dev->descriptor.idProduct !=
522 cpu_to_le16(PRODUCT_ID_EARTHMATEUSB)) {
Mike Isely3d6aa322008-02-10 20:23:24 -0600523 /* The old original USB Earthmate seemed able to
524 handle GET_CONFIG requests; everything they've
525 produced since that time crashes if this command is
526 attempted :-( */
527 dbg("%s - Marking this device as unsafe for GET_CONFIG "
528 "commands", __func__);
529 priv->get_cfg_unsafe = !0;
530 }
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500531
532 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533} /* cypress_earthmate_startup */
534
535
Alan Cox813a2242008-07-22 11:10:36 +0100536static int cypress_hidcom_startup(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537{
538 struct cypress_private *priv;
539
Harvey Harrison441b62c2008-03-03 16:08:34 -0800540 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
542 if (generic_startup(serial)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800543 dbg("%s - Failed setting up port %d", __func__,
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500544 serial->port[0]->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 return 1;
546 }
547
548 priv = usb_get_serial_port_data(serial->port[0]);
549 priv->chiptype = CT_CYPHIDCOM;
Alan Cox813a2242008-07-22 11:10:36 +0100550
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500551 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552} /* cypress_hidcom_startup */
553
554
Alan Cox813a2242008-07-22 11:10:36 +0100555static int cypress_ca42v2_startup(struct usb_serial *serial)
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600556{
557 struct cypress_private *priv;
558
Harvey Harrison441b62c2008-03-03 16:08:34 -0800559 dbg("%s", __func__);
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600560
561 if (generic_startup(serial)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800562 dbg("%s - Failed setting up port %d", __func__,
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600563 serial->port[0]->number);
564 return 1;
565 }
566
567 priv = usb_get_serial_port_data(serial->port[0]);
568 priv->chiptype = CT_CA42V2;
569
570 return 0;
571} /* cypress_ca42v2_startup */
572
573
Alan Sternf9c99bb2009-06-02 11:53:55 -0400574static void cypress_release(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575{
576 struct cypress_private *priv;
577
Alan Cox813a2242008-07-22 11:10:36 +0100578 dbg("%s - port %d", __func__, serial->port[0]->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580 /* all open ports are closed at this point */
581
582 priv = usb_get_serial_port_data(serial->port[0]);
583
584 if (priv) {
Johan Hovold117fb8d2010-05-16 20:33:50 +0200585 kfifo_free(&priv->write_fifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 kfree(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 }
588}
589
590
Alan Coxa509a7e2009-09-19 13:13:26 -0700591static int cypress_open(struct tty_struct *tty, struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
593 struct cypress_private *priv = usb_get_serial_port_data(port);
594 struct usb_serial *serial = port->serial;
595 unsigned long flags;
596 int result = 0;
597
Harvey Harrison441b62c2008-03-03 16:08:34 -0800598 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Mike Isely78aef512006-08-29 22:07:11 -0500600 if (!priv->comm_is_ok)
601 return -EIO;
602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 /* clear halts before open */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 usb_clear_halt(serial->dev, 0x81);
605 usb_clear_halt(serial->dev, 0x02);
606
607 spin_lock_irqsave(&priv->lock, flags);
608 /* reset read/write statistics */
609 priv->bytes_in = 0;
610 priv->bytes_out = 0;
611 priv->cmd_count = 0;
612 priv->rx_flags = 0;
613 spin_unlock_irqrestore(&priv->lock, flags);
614
Alan Cox335f8512009-06-11 12:26:29 +0100615 /* Set termios */
Alan Coxfe1ae7f2009-09-19 13:13:33 -0700616 cypress_send(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Alan Cox95da3102008-07-22 11:09:07 +0100618 if (tty)
619 cypress_set_termios(tty, port, &priv->tmp_termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
621 /* setup the port and start reading from the device */
Alan Cox813a2242008-07-22 11:10:36 +0100622 if (!port->interrupt_in_urb) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700623 dev_err(&port->dev, "%s - interrupt_in_urb is empty!\n",
624 __func__);
Alan Cox813a2242008-07-22 11:10:36 +0100625 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 }
627
628 usb_fill_int_urb(port->interrupt_in_urb, serial->dev,
629 usb_rcvintpipe(serial->dev, port->interrupt_in_endpointAddress),
Alan Cox813a2242008-07-22 11:10:36 +0100630 port->interrupt_in_urb->transfer_buffer,
631 port->interrupt_in_urb->transfer_buffer_length,
Mike Isely0257fa92006-08-29 22:06:59 -0500632 cypress_read_int_callback, port, priv->read_urb_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
634
Alan Cox813a2242008-07-22 11:10:36 +0100635 if (result) {
636 dev_err(&port->dev,
637 "%s - failed submitting read urb, error %d\n",
638 __func__, result);
Mike Isely78aef512006-08-29 22:07:11 -0500639 cypress_set_dead(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 }
Alan Cox335f8512009-06-11 12:26:29 +0100641 port->port.drain_delay = 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 return result;
643} /* cypress_open */
644
Alan Cox335f8512009-06-11 12:26:29 +0100645static void cypress_dtr_rts(struct usb_serial_port *port, int on)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
647 struct cypress_private *priv = usb_get_serial_port_data(port);
Alan Cox335f8512009-06-11 12:26:29 +0100648 /* drop dtr and rts */
Alan Cox335f8512009-06-11 12:26:29 +0100649 spin_lock_irq(&priv->lock);
650 if (on == 0)
651 priv->line_control = 0;
652 else
653 priv->line_control = CONTROL_DTR | CONTROL_RTS;
654 priv->cmd_ctrl = 1;
655 spin_unlock_irq(&priv->lock);
656 cypress_write(NULL, port, NULL, 0);
657}
658
659static void cypress_close(struct usb_serial_port *port)
660{
661 struct cypress_private *priv = usb_get_serial_port_data(port);
Johan Hovold117fb8d2010-05-16 20:33:50 +0200662 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Harvey Harrison441b62c2008-03-03 16:08:34 -0800664 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100666 /* writing is potentially harmful, lock must be taken */
667 mutex_lock(&port->serial->disc_mutex);
668 if (port->serial->disconnected) {
669 mutex_unlock(&port->serial->disc_mutex);
670 return;
671 }
Johan Hovold117fb8d2010-05-16 20:33:50 +0200672 spin_lock_irqsave(&priv->lock, flags);
673 kfifo_reset_out(&priv->write_fifo);
674 spin_unlock_irqrestore(&priv->lock, flags);
675
Harvey Harrison441b62c2008-03-03 16:08:34 -0800676 dbg("%s - stopping urbs", __func__);
Alan Cox813a2242008-07-22 11:10:36 +0100677 usb_kill_urb(port->interrupt_in_urb);
678 usb_kill_urb(port->interrupt_out_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 if (stats)
Alan Cox813a2242008-07-22 11:10:36 +0100681 dev_info(&port->dev, "Statistics: %d Bytes In | %d Bytes Out | %d Commands Issued\n",
682 priv->bytes_in, priv->bytes_out, priv->cmd_count);
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100683 mutex_unlock(&port->serial->disc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684} /* cypress_close */
685
686
Alan Cox95da3102008-07-22 11:09:07 +0100687static int cypress_write(struct tty_struct *tty, struct usb_serial_port *port,
688 const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
690 struct cypress_private *priv = usb_get_serial_port_data(port);
Alan Cox813a2242008-07-22 11:10:36 +0100691
Harvey Harrison441b62c2008-03-03 16:08:34 -0800692 dbg("%s - port %d, %d bytes", __func__, port->number, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
694 /* line control commands, which need to be executed immediately,
695 are not put into the buffer for obvious reasons.
696 */
697 if (priv->cmd_ctrl) {
698 count = 0;
699 goto finish;
700 }
Alan Cox813a2242008-07-22 11:10:36 +0100701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 if (!count)
703 return count;
Alan Cox813a2242008-07-22 11:10:36 +0100704
Johan Hovold117fb8d2010-05-16 20:33:50 +0200705 count = kfifo_in_locked(&priv->write_fifo, buf, count, &priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
707finish:
708 cypress_send(port);
709
710 return count;
711} /* cypress_write */
712
713
714static void cypress_send(struct usb_serial_port *port)
715{
716 int count = 0, result, offset, actual_size;
717 struct cypress_private *priv = usb_get_serial_port_data(port);
718 unsigned long flags;
Alan Cox813a2242008-07-22 11:10:36 +0100719
Mike Isely78aef512006-08-29 22:07:11 -0500720 if (!priv->comm_is_ok)
721 return;
722
Harvey Harrison441b62c2008-03-03 16:08:34 -0800723 dbg("%s - port %d", __func__, port->number);
Alan Cox813a2242008-07-22 11:10:36 +0100724 dbg("%s - interrupt out size is %d", __func__,
725 port->interrupt_out_size);
726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 spin_lock_irqsave(&priv->lock, flags);
728 if (priv->write_urb_in_use) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800729 dbg("%s - can't write, urb in use", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 spin_unlock_irqrestore(&priv->lock, flags);
731 return;
732 }
733 spin_unlock_irqrestore(&priv->lock, flags);
734
735 /* clear buffer */
Alan Cox813a2242008-07-22 11:10:36 +0100736 memset(port->interrupt_out_urb->transfer_buffer, 0,
737 port->interrupt_out_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
739 spin_lock_irqsave(&priv->lock, flags);
Mike Isely3416eaa2008-02-10 20:23:19 -0600740 switch (priv->pkt_fmt) {
741 default:
742 case packet_format_1:
743 /* this is for the CY7C64013... */
744 offset = 2;
745 port->interrupt_out_buffer[0] = priv->line_control;
746 break;
747 case packet_format_2:
748 /* this is for the CY7C63743... */
749 offset = 1;
750 port->interrupt_out_buffer[0] = priv->line_control;
751 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 }
753
754 if (priv->line_control & CONTROL_RESET)
755 priv->line_control &= ~CONTROL_RESET;
756
757 if (priv->cmd_ctrl) {
758 priv->cmd_count++;
Harvey Harrison441b62c2008-03-03 16:08:34 -0800759 dbg("%s - line control command being issued", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 spin_unlock_irqrestore(&priv->lock, flags);
761 goto send;
762 } else
763 spin_unlock_irqrestore(&priv->lock, flags);
764
Johan Hovold117fb8d2010-05-16 20:33:50 +0200765 count = kfifo_out_locked(&priv->write_fifo,
766 &port->interrupt_out_buffer[offset],
767 port->interrupt_out_size - offset,
768 &priv->lock);
Alan Cox813a2242008-07-22 11:10:36 +0100769 if (count == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Mike Isely3416eaa2008-02-10 20:23:19 -0600772 switch (priv->pkt_fmt) {
773 default:
774 case packet_format_1:
775 port->interrupt_out_buffer[1] = count;
776 break;
777 case packet_format_2:
778 port->interrupt_out_buffer[0] |= count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 }
780
Harvey Harrison441b62c2008-03-03 16:08:34 -0800781 dbg("%s - count is %d", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
783send:
784 spin_lock_irqsave(&priv->lock, flags);
785 priv->write_urb_in_use = 1;
786 spin_unlock_irqrestore(&priv->lock, flags);
787
788 if (priv->cmd_ctrl)
789 actual_size = 1;
790 else
Mike Isely3416eaa2008-02-10 20:23:19 -0600791 actual_size = count +
792 (priv->pkt_fmt == packet_format_1 ? 2 : 1);
793
Alan Cox813a2242008-07-22 11:10:36 +0100794 usb_serial_debug_data(debug, &port->dev, __func__,
795 port->interrupt_out_size,
796 port->interrupt_out_urb->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Mike Isely9aa8dae2006-08-29 22:07:04 -0500798 usb_fill_int_urb(port->interrupt_out_urb, port->serial->dev,
799 usb_sndintpipe(port->serial->dev, port->interrupt_out_endpointAddress),
800 port->interrupt_out_buffer, port->interrupt_out_size,
801 cypress_write_int_callback, port, priv->write_urb_interval);
Alan Cox813a2242008-07-22 11:10:36 +0100802 result = usb_submit_urb(port->interrupt_out_urb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 if (result) {
Johan Hovold22a416c2012-02-10 13:20:51 +0100804 dev_err_console(port,
Alan Cox813a2242008-07-22 11:10:36 +0100805 "%s - failed submitting write urb, error %d\n",
806 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 priv->write_urb_in_use = 0;
Mike Isely78aef512006-08-29 22:07:11 -0500808 cypress_set_dead(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 }
810
811 spin_lock_irqsave(&priv->lock, flags);
Alan Cox813a2242008-07-22 11:10:36 +0100812 if (priv->cmd_ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 priv->cmd_ctrl = 0;
Alan Cox813a2242008-07-22 11:10:36 +0100814
815 /* do not count the line control and size bytes */
816 priv->bytes_out += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 spin_unlock_irqrestore(&priv->lock, flags);
818
Pete Zaitcevcf2c7482006-05-22 21:58:49 -0700819 usb_serial_port_softint(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820} /* cypress_send */
821
822
823/* returns how much space is available in the soft buffer */
Alan Cox95da3102008-07-22 11:09:07 +0100824static int cypress_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
Alan Cox95da3102008-07-22 11:09:07 +0100826 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 struct cypress_private *priv = usb_get_serial_port_data(port);
828 int room = 0;
829 unsigned long flags;
830
Harvey Harrison441b62c2008-03-03 16:08:34 -0800831 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
833 spin_lock_irqsave(&priv->lock, flags);
Johan Hovold117fb8d2010-05-16 20:33:50 +0200834 room = kfifo_avail(&priv->write_fifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 spin_unlock_irqrestore(&priv->lock, flags);
836
Harvey Harrison441b62c2008-03-03 16:08:34 -0800837 dbg("%s - returns %d", __func__, room);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 return room;
839}
840
841
Alan Cox60b33c12011-02-14 16:26:14 +0000842static int cypress_tiocmget(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843{
Alan Cox95da3102008-07-22 11:09:07 +0100844 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 struct cypress_private *priv = usb_get_serial_port_data(port);
846 __u8 status, control;
847 unsigned int result = 0;
848 unsigned long flags;
Alan Cox813a2242008-07-22 11:10:36 +0100849
Harvey Harrison441b62c2008-03-03 16:08:34 -0800850 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852 spin_lock_irqsave(&priv->lock, flags);
853 control = priv->line_control;
854 status = priv->current_status;
855 spin_unlock_irqrestore(&priv->lock, flags);
856
857 result = ((control & CONTROL_DTR) ? TIOCM_DTR : 0)
858 | ((control & CONTROL_RTS) ? TIOCM_RTS : 0)
859 | ((status & UART_CTS) ? TIOCM_CTS : 0)
860 | ((status & UART_DSR) ? TIOCM_DSR : 0)
861 | ((status & UART_RI) ? TIOCM_RI : 0)
862 | ((status & UART_CD) ? TIOCM_CD : 0);
863
Harvey Harrison441b62c2008-03-03 16:08:34 -0800864 dbg("%s - result = %x", __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
866 return result;
867}
868
869
Alan Cox20b9d172011-02-14 16:26:50 +0000870static int cypress_tiocmset(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 unsigned int set, unsigned int clear)
872{
Alan Cox95da3102008-07-22 11:09:07 +0100873 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 struct cypress_private *priv = usb_get_serial_port_data(port);
875 unsigned long flags;
Alan Cox813a2242008-07-22 11:10:36 +0100876
Harvey Harrison441b62c2008-03-03 16:08:34 -0800877 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
879 spin_lock_irqsave(&priv->lock, flags);
880 if (set & TIOCM_RTS)
881 priv->line_control |= CONTROL_RTS;
882 if (set & TIOCM_DTR)
883 priv->line_control |= CONTROL_DTR;
884 if (clear & TIOCM_RTS)
885 priv->line_control &= ~CONTROL_RTS;
886 if (clear & TIOCM_DTR)
887 priv->line_control &= ~CONTROL_DTR;
Alan Cox8873aaa2008-03-10 21:59:28 +0000888 priv->cmd_ctrl = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 spin_unlock_irqrestore(&priv->lock, flags);
890
Alan Cox95da3102008-07-22 11:09:07 +0100891 return cypress_write(tty, port, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892}
893
894
Alan Cox00a0d0d2011-02-14 16:27:06 +0000895static int cypress_ioctl(struct tty_struct *tty,
Alan Cox95da3102008-07-22 11:09:07 +0100896 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897{
Alan Cox95da3102008-07-22 11:09:07 +0100898 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 struct cypress_private *priv = usb_get_serial_port_data(port);
900
Harvey Harrison441b62c2008-03-03 16:08:34 -0800901 dbg("%s - port %d, cmd 0x%.4x", __func__, port->number, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
903 switch (cmd) {
Alan Cox813a2242008-07-22 11:10:36 +0100904 /* This code comes from drivers/char/serial.c and ftdi_sio.c */
905 case TIOCMIWAIT:
906 while (priv != NULL) {
907 interruptible_sleep_on(&priv->delta_msr_wait);
908 /* see if a signal did it */
909 if (signal_pending(current))
910 return -ERESTARTSYS;
911 else {
912 char diff = priv->diff_status;
913 if (diff == 0)
914 return -EIO; /* no change => error */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Alan Cox813a2242008-07-22 11:10:36 +0100916 /* consume all events */
917 priv->diff_status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
Alan Cox813a2242008-07-22 11:10:36 +0100919 /* return 0 if caller wanted to know about
920 these bits */
921 if (((arg & TIOCM_RNG) && (diff & UART_RI)) ||
922 ((arg & TIOCM_DSR) && (diff & UART_DSR)) ||
923 ((arg & TIOCM_CD) && (diff & UART_CD)) ||
924 ((arg & TIOCM_CTS) && (diff & UART_CTS)))
925 return 0;
926 /* otherwise caller can't care less about what
927 * happened, and so we continue to wait for
928 * more events.
929 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 }
Alan Cox813a2242008-07-22 11:10:36 +0100931 }
932 return 0;
933 default:
934 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 }
Harvey Harrison441b62c2008-03-03 16:08:34 -0800936 dbg("%s - arg not supported - it was 0x%04x - check include/asm/ioctls.h", __func__, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 return -ENOIOCTLCMD;
938} /* cypress_ioctl */
939
940
Alan Cox95da3102008-07-22 11:09:07 +0100941static void cypress_set_termios(struct tty_struct *tty,
942 struct usb_serial_port *port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943{
944 struct cypress_private *priv = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 int data_bits, stop_bits, parity_type, parity_enable;
Alan Cox8873aaa2008-03-10 21:59:28 +0000946 unsigned cflag, iflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 unsigned long flags;
948 __u8 oldlines;
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500949 int linechange = 0;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500950
Harvey Harrison441b62c2008-03-03 16:08:34 -0800951 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 spin_lock_irqsave(&priv->lock, flags);
Alan Coxfe1ae7f2009-09-19 13:13:33 -0700954 /* We can't clean this one up as we don't know the device type
955 early enough */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 if (!priv->termios_initialized) {
957 if (priv->chiptype == CT_EARTHMATE) {
958 *(tty->termios) = tty_std_termios;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500959 tty->termios->c_cflag = B4800 | CS8 | CREAD | HUPCL |
960 CLOCAL;
Alan Cox8873aaa2008-03-10 21:59:28 +0000961 tty->termios->c_ispeed = 4800;
962 tty->termios->c_ospeed = 4800;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 } else if (priv->chiptype == CT_CYPHIDCOM) {
964 *(tty->termios) = tty_std_termios;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500965 tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
966 CLOCAL;
Alan Cox8873aaa2008-03-10 21:59:28 +0000967 tty->termios->c_ispeed = 9600;
968 tty->termios->c_ospeed = 9600;
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600969 } else if (priv->chiptype == CT_CA42V2) {
970 *(tty->termios) = tty_std_termios;
971 tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
972 CLOCAL;
Alan Cox8873aaa2008-03-10 21:59:28 +0000973 tty->termios->c_ispeed = 9600;
974 tty->termios->c_ospeed = 9600;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 }
976 priv->termios_initialized = 1;
977 }
978 spin_unlock_irqrestore(&priv->lock, flags);
979
Alan Cox8873aaa2008-03-10 21:59:28 +0000980 /* Unsupported features need clearing */
981 tty->termios->c_cflag &= ~(CMSPAR|CRTSCTS);
982
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 cflag = tty->termios->c_cflag;
984 iflag = tty->termios->c_iflag;
985
986 /* check if there are new settings */
987 if (old_termios) {
Alan Cox8873aaa2008-03-10 21:59:28 +0000988 spin_lock_irqsave(&priv->lock, flags);
989 priv->tmp_termios = *(tty->termios);
990 spin_unlock_irqrestore(&priv->lock, flags);
991 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
993 /* set number of data bits, parity, stop bits */
994 /* when parity is disabled the parity type bit is ignored */
995
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500996 /* 1 means 2 stop bits, 0 means 1 stop bit */
997 stop_bits = cflag & CSTOPB ? 1 : 0;
998
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 if (cflag & PARENB) {
1000 parity_enable = 1;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001001 /* 1 means odd parity, 0 means even parity */
1002 parity_type = cflag & PARODD ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 } else
1004 parity_enable = parity_type = 0;
1005
Alan Cox77336822008-07-22 11:10:53 +01001006 switch (cflag & CSIZE) {
1007 case CS5:
1008 data_bits = 0;
1009 break;
1010 case CS6:
1011 data_bits = 1;
1012 break;
1013 case CS7:
1014 data_bits = 2;
1015 break;
1016 case CS8:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 data_bits = 3;
Alan Cox77336822008-07-22 11:10:53 +01001018 break;
1019 default:
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001020 dev_err(&port->dev, "%s - CSIZE was set, but not CS5-CS8\n",
1021 __func__);
Alan Cox77336822008-07-22 11:10:53 +01001022 data_bits = 3;
1023 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 spin_lock_irqsave(&priv->lock, flags);
1025 oldlines = priv->line_control;
1026 if ((cflag & CBAUD) == B0) {
1027 /* drop dtr and rts */
Harvey Harrison441b62c2008-03-03 16:08:34 -08001028 dbg("%s - dropping the lines, baud rate 0bps", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
Alan Cox8873aaa2008-03-10 21:59:28 +00001030 } else
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001031 priv->line_control = (CONTROL_DTR | CONTROL_RTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001034 dbg("%s - sending %d stop_bits, %d parity_enable, %d parity_type, "
Harvey Harrison441b62c2008-03-03 16:08:34 -08001035 "%d data_bits (+5)", __func__, stop_bits,
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001036 parity_enable, parity_type, data_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
Alan Cox813a2242008-07-22 11:10:36 +01001038 cypress_serial_control(tty, port, tty_get_baud_rate(tty),
1039 data_bits, stop_bits,
1040 parity_enable, parity_type,
1041 0, CYPRESS_SET_CONFIG);
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001042
1043 /* we perform a CYPRESS_GET_CONFIG so that the current settings are
1044 * filled into the private structure this should confirm that all is
1045 * working if it returns what we just set */
Alan Cox95da3102008-07-22 11:09:07 +01001046 cypress_serial_control(tty, port, 0, 0, 0, 0, 0, 0, CYPRESS_GET_CONFIG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001048 /* Here we can define custom tty settings for devices; the main tty
1049 * termios flag base comes from empeg.c */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001051 spin_lock_irqsave(&priv->lock, flags);
Alan Cox813a2242008-07-22 11:10:36 +01001052 if (priv->chiptype == CT_EARTHMATE && priv->baud_rate == 4800) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001053 dbg("Using custom termios settings for a baud rate of "
1054 "4800bps.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 /* define custom termios settings for NMEA protocol */
1056
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 tty->termios->c_iflag /* input modes - */
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001058 &= ~(IGNBRK /* disable ignore break */
1059 | BRKINT /* disable break causes interrupt */
1060 | PARMRK /* disable mark parity errors */
1061 | ISTRIP /* disable clear high bit of input char */
1062 | INLCR /* disable translate NL to CR */
1063 | IGNCR /* disable ignore CR */
1064 | ICRNL /* disable translate CR to NL */
1065 | IXON); /* disable enable XON/XOFF flow control */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001067 tty->termios->c_oflag /* output modes */
1068 &= ~OPOST; /* disable postprocess output char */
1069
1070 tty->termios->c_lflag /* line discipline modes */
1071 &= ~(ECHO /* disable echo input characters */
1072 | ECHONL /* disable echo new line */
1073 | ICANON /* disable erase, kill, werase, and rprnt
1074 special characters */
1075 | ISIG /* disable interrupt, quit, and suspend
1076 special characters */
1077 | IEXTEN); /* disable non-POSIX special characters */
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001078 } /* CT_CYPHIDCOM: Application should handle this for device */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 linechange = (priv->line_control != oldlines);
1081 spin_unlock_irqrestore(&priv->lock, flags);
1082
1083 /* if necessary, set lines */
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001084 if (linechange) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 priv->cmd_ctrl = 1;
Alan Cox95da3102008-07-22 11:09:07 +01001086 cypress_write(tty, port, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088} /* cypress_set_termios */
1089
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001090
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091/* returns amount of data still left in soft buffer */
Alan Cox95da3102008-07-22 11:09:07 +01001092static int cypress_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093{
Alan Cox95da3102008-07-22 11:09:07 +01001094 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 struct cypress_private *priv = usb_get_serial_port_data(port);
1096 int chars = 0;
1097 unsigned long flags;
1098
Harvey Harrison441b62c2008-03-03 16:08:34 -08001099 dbg("%s - port %d", __func__, port->number);
Alan Cox813a2242008-07-22 11:10:36 +01001100
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 spin_lock_irqsave(&priv->lock, flags);
Johan Hovold117fb8d2010-05-16 20:33:50 +02001102 chars = kfifo_len(&priv->write_fifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 spin_unlock_irqrestore(&priv->lock, flags);
1104
Harvey Harrison441b62c2008-03-03 16:08:34 -08001105 dbg("%s - returns %d", __func__, chars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 return chars;
1107}
1108
1109
Alan Cox95da3102008-07-22 11:09:07 +01001110static void cypress_throttle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111{
Alan Cox95da3102008-07-22 11:09:07 +01001112 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 struct cypress_private *priv = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
Harvey Harrison441b62c2008-03-03 16:08:34 -08001115 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
Oliver Neukum63832512009-10-07 10:50:23 +02001117 spin_lock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 priv->rx_flags = THROTTLED;
Oliver Neukum63832512009-10-07 10:50:23 +02001119 spin_unlock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120}
1121
1122
Alan Cox95da3102008-07-22 11:09:07 +01001123static void cypress_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124{
Alan Cox95da3102008-07-22 11:09:07 +01001125 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 struct cypress_private *priv = usb_get_serial_port_data(port);
1127 int actually_throttled, result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
Harvey Harrison441b62c2008-03-03 16:08:34 -08001129 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
Oliver Neukum63832512009-10-07 10:50:23 +02001131 spin_lock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 actually_throttled = priv->rx_flags & ACTUALLY_THROTTLED;
1133 priv->rx_flags = 0;
Oliver Neukum63832512009-10-07 10:50:23 +02001134 spin_unlock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
Mike Isely78aef512006-08-29 22:07:11 -05001136 if (!priv->comm_is_ok)
1137 return;
1138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 if (actually_throttled) {
Oliver Neukum63832512009-10-07 10:50:23 +02001140 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
Mike Isely78aef512006-08-29 22:07:11 -05001141 if (result) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001142 dev_err(&port->dev, "%s - failed submitting read urb, "
Harvey Harrison441b62c2008-03-03 16:08:34 -08001143 "error %d\n", __func__, result);
Mike Isely78aef512006-08-29 22:07:11 -05001144 cypress_set_dead(port);
1145 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 }
1147}
1148
1149
David Howells7d12e782006-10-05 14:55:46 +01001150static void cypress_read_int_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151{
Ming Leicdc97792008-02-24 18:41:47 +08001152 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 struct cypress_private *priv = usb_get_serial_port_data(port);
1154 struct tty_struct *tty;
1155 unsigned char *data = urb->transfer_buffer;
1156 unsigned long flags;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001157 char tty_flag = TTY_NORMAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 int havedata = 0;
1159 int bytes = 0;
1160 int result;
1161 int i = 0;
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001162 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
Harvey Harrison441b62c2008-03-03 16:08:34 -08001164 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001166 switch (status) {
Mike Isely78aef512006-08-29 22:07:11 -05001167 case 0: /* success */
1168 break;
1169 case -ECONNRESET:
1170 case -ENOENT:
1171 case -ESHUTDOWN:
1172 /* precursor to disconnect so just go away */
1173 return;
1174 case -EPIPE:
Alan Stern4d2fae82009-07-09 12:59:57 -04001175 /* Can't call usb_clear_halt while in_interrupt */
1176 /* FALLS THROUGH */
Mike Isely78aef512006-08-29 22:07:11 -05001177 default:
1178 /* something ugly is going on... */
Alan Cox813a2242008-07-22 11:10:36 +01001179 dev_err(&urb->dev->dev,
1180 "%s - unexpected nonzero read status received: %d\n",
1181 __func__, status);
Mike Isely78aef512006-08-29 22:07:11 -05001182 cypress_set_dead(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 return;
1184 }
1185
1186 spin_lock_irqsave(&priv->lock, flags);
1187 if (priv->rx_flags & THROTTLED) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001188 dbg("%s - now throttling", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 priv->rx_flags |= ACTUALLY_THROTTLED;
1190 spin_unlock_irqrestore(&priv->lock, flags);
1191 return;
1192 }
1193 spin_unlock_irqrestore(&priv->lock, flags);
1194
Alan Cox4a90f092008-10-13 10:39:46 +01001195 tty = tty_port_tty_get(&port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 if (!tty) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001197 dbg("%s - bad tty pointer - exiting", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 return;
1199 }
1200
1201 spin_lock_irqsave(&priv->lock, flags);
Mike Isely3416eaa2008-02-10 20:23:19 -06001202 result = urb->actual_length;
1203 switch (priv->pkt_fmt) {
1204 default:
1205 case packet_format_1:
1206 /* This is for the CY7C64013... */
1207 priv->current_status = data[0] & 0xF8;
1208 bytes = data[1] + 2;
1209 i = 2;
1210 if (bytes > 2)
1211 havedata = 1;
1212 break;
1213 case packet_format_2:
1214 /* This is for the CY7C63743... */
1215 priv->current_status = data[0] & 0xF8;
1216 bytes = (data[0] & 0x07) + 1;
1217 i = 1;
1218 if (bytes > 1)
1219 havedata = 1;
1220 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 }
1222 spin_unlock_irqrestore(&priv->lock, flags);
Mike Isely3416eaa2008-02-10 20:23:19 -06001223 if (result < bytes) {
1224 dbg("%s - wrong packet size - received %d bytes but packet "
1225 "said %d bytes", __func__, result, bytes);
1226 goto continue_read;
1227 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
Alan Cox813a2242008-07-22 11:10:36 +01001229 usb_serial_debug_data(debug, &port->dev, __func__,
1230 urb->actual_length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
1232 spin_lock_irqsave(&priv->lock, flags);
1233 /* check to see if status has changed */
Mike Isely67683062008-02-10 20:23:28 -06001234 if (priv->current_status != priv->prev_status) {
1235 priv->diff_status |= priv->current_status ^
1236 priv->prev_status;
1237 wake_up_interruptible(&priv->delta_msr_wait);
1238 priv->prev_status = priv->current_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 }
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001240 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001242 /* hangup, as defined in acm.c... this might be a bad place for it
1243 * though */
1244 if (tty && !(tty->termios->c_cflag & CLOCAL) &&
1245 !(priv->current_status & UART_CD)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001246 dbg("%s - calling hangup", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 tty_hangup(tty);
1248 goto continue_read;
1249 }
1250
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001251 /* There is one error bit... I'm assuming it is a parity error
1252 * indicator as the generic firmware will set this bit to 1 if a
1253 * parity error occurs.
1254 * I can not find reference to any other error events. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 spin_lock_irqsave(&priv->lock, flags);
1256 if (priv->current_status & CYP_ERROR) {
1257 spin_unlock_irqrestore(&priv->lock, flags);
1258 tty_flag = TTY_PARITY;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001259 dbg("%s - Parity Error detected", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 } else
1261 spin_unlock_irqrestore(&priv->lock, flags);
1262
1263 /* process read if there is data other than line status */
Alan Coxa108bfc2010-02-18 16:44:01 +00001264 if (tty && bytes > i) {
1265 tty_insert_flip_string_fixed_flag(tty, data + i,
Johan Hovold70ced222010-05-07 19:46:56 +02001266 tty_flag, bytes - i);
Alan Cox4a90f092008-10-13 10:39:46 +01001267 tty_flip_buffer_push(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 }
1269
1270 spin_lock_irqsave(&priv->lock, flags);
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001271 /* control and status byte(s) are also counted */
1272 priv->bytes_in += bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 spin_unlock_irqrestore(&priv->lock, flags);
1274
1275continue_read:
Alan Cox4a90f092008-10-13 10:39:46 +01001276 tty_kref_put(tty);
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001277
Alan Stern1f871582010-02-17 10:05:47 -05001278 /* Continue trying to always read */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
Alan Stern1f871582010-02-17 10:05:47 -05001280 if (priv->comm_is_ok) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001281 usb_fill_int_urb(port->interrupt_in_urb, port->serial->dev,
1282 usb_rcvintpipe(port->serial->dev,
1283 port->interrupt_in_endpointAddress),
1284 port->interrupt_in_urb->transfer_buffer,
1285 port->interrupt_in_urb->transfer_buffer_length,
Alan Cox813a2242008-07-22 11:10:36 +01001286 cypress_read_int_callback, port,
1287 priv->read_urb_interval);
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001288 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
Alan Stern1f871582010-02-17 10:05:47 -05001289 if (result && result != -EPERM) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001290 dev_err(&urb->dev->dev, "%s - failed resubmitting "
Harvey Harrison441b62c2008-03-03 16:08:34 -08001291 "read urb, error %d\n", __func__,
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001292 result);
Mike Isely78aef512006-08-29 22:07:11 -05001293 cypress_set_dead(port);
1294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296} /* cypress_read_int_callback */
1297
1298
David Howells7d12e782006-10-05 14:55:46 +01001299static void cypress_write_int_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300{
Ming Leicdc97792008-02-24 18:41:47 +08001301 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 struct cypress_private *priv = usb_get_serial_port_data(port);
1303 int result;
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001304 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
Harvey Harrison441b62c2008-03-03 16:08:34 -08001306 dbg("%s - port %d", __func__, port->number);
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001307
1308 switch (status) {
Alan Cox813a2242008-07-22 11:10:36 +01001309 case 0:
1310 /* success */
1311 break;
1312 case -ECONNRESET:
1313 case -ENOENT:
1314 case -ESHUTDOWN:
1315 /* this urb is terminated, clean up */
1316 dbg("%s - urb shutting down with status: %d",
1317 __func__, status);
1318 priv->write_urb_in_use = 0;
1319 return;
1320 case -EPIPE: /* no break needed; clear halt and resubmit */
1321 if (!priv->comm_is_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 break;
Alan Cox813a2242008-07-22 11:10:36 +01001323 usb_clear_halt(port->serial->dev, 0x02);
1324 /* error in the urb, so we have to resubmit it */
1325 dbg("%s - nonzero write bulk status received: %d",
1326 __func__, status);
1327 port->interrupt_out_urb->transfer_buffer_length = 1;
Alan Cox813a2242008-07-22 11:10:36 +01001328 result = usb_submit_urb(port->interrupt_out_urb, GFP_ATOMIC);
1329 if (!result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 return;
Alan Cox813a2242008-07-22 11:10:36 +01001331 dev_err(&urb->dev->dev,
1332 "%s - failed resubmitting write urb, error %d\n",
1333 __func__, result);
1334 cypress_set_dead(port);
1335 break;
1336 default:
1337 dev_err(&urb->dev->dev,
1338 "%s - unexpected nonzero write status received: %d\n",
1339 __func__, status);
1340 cypress_set_dead(port);
1341 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 priv->write_urb_in_use = 0;
Alan Cox813a2242008-07-22 11:10:36 +01001344
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 /* send any buffered data */
1346 cypress_send(port);
1347}
1348
1349
1350/*****************************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 * Module functions
1352 *****************************************************************************/
1353
1354static int __init cypress_init(void)
1355{
1356 int retval;
Alan Cox813a2242008-07-22 11:10:36 +01001357
Harvey Harrison441b62c2008-03-03 16:08:34 -08001358 dbg("%s", __func__);
Alan Cox813a2242008-07-22 11:10:36 +01001359
Alan Stern08a4f6b2012-02-23 14:56:17 -05001360 retval = usb_serial_register_drivers(&cypress_driver, serial_drivers);
1361 if (retval == 0)
1362 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
1363 DRIVER_DESC "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 return retval;
1365}
1366
1367
Alan Cox813a2242008-07-22 11:10:36 +01001368static void __exit cypress_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369{
Harvey Harrison441b62c2008-03-03 16:08:34 -08001370 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
Alan Stern08a4f6b2012-02-23 14:56:17 -05001372 usb_serial_deregister_drivers(&cypress_driver, serial_drivers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373}
1374
1375
1376module_init(cypress_init);
1377module_exit(cypress_exit);
1378
Alan Cox813a2242008-07-22 11:10:36 +01001379MODULE_AUTHOR(DRIVER_AUTHOR);
1380MODULE_DESCRIPTION(DRIVER_DESC);
1381MODULE_VERSION(DRIVER_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382MODULE_LICENSE("GPL");
1383
1384module_param(debug, bool, S_IRUGO | S_IWUSR);
1385MODULE_PARM_DESC(debug, "Debug enabled or not");
1386module_param(stats, bool, S_IRUGO | S_IWUSR);
1387MODULE_PARM_DESC(stats, "Enable statistics or not");
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001388module_param(interval, int, S_IRUGO | S_IWUSR);
1389MODULE_PARM_DESC(interval, "Overrides interrupt interval");
Mike Frysingerc3126592009-12-18 16:33:03 -05001390module_param(unstable_bauds, bool, S_IRUGO | S_IWUSR);
1391MODULE_PARM_DESC(unstable_bauds, "Allow unstable baud rates");