blob: 270bda809d8179fe54474b0309693fd45f15b113 [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) },
Robert Butora617d12d2013-05-31 18:09:51 +030073 { USB_DEVICE(VENDOR_ID_FRWD, PRODUCT_ID_CYPHIDCOM_FRWD) },
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 { } /* Terminating entry */
75};
76
Németh Márton7d40d7e2010-01-10 15:34:24 +010077static const struct usb_device_id id_table_nokiaca42v2[] = {
Lonnie Mendeza5c44e22006-03-01 10:45:24 -060078 { USB_DEVICE(VENDOR_ID_DAZZLE, PRODUCT_ID_CA42) },
79 { } /* Terminating entry */
80};
81
Németh Márton7d40d7e2010-01-10 15:34:24 +010082static const struct usb_device_id id_table_combined[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB) },
Lonnie Mendez25b6f082005-05-10 17:09:52 -050084 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB_LT20) },
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 { USB_DEVICE(VENDOR_ID_CYPRESS, PRODUCT_ID_CYPHIDCOM) },
Dmitry Shapin6f6f06e2008-03-04 15:25:10 -080086 { USB_DEVICE(VENDOR_ID_POWERCOM, PRODUCT_ID_UPS) },
Robert Butora617d12d2013-05-31 18:09:51 +030087 { USB_DEVICE(VENDOR_ID_FRWD, PRODUCT_ID_CYPHIDCOM_FRWD) },
Lonnie Mendeza5c44e22006-03-01 10:45:24 -060088 { USB_DEVICE(VENDOR_ID_DAZZLE, PRODUCT_ID_CA42) },
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 { } /* Terminating entry */
90};
91
Alan Cox813a2242008-07-22 11:10:36 +010092MODULE_DEVICE_TABLE(usb, id_table_combined);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94static struct usb_driver cypress_driver = {
95 .name = "cypress",
96 .probe = usb_serial_probe,
97 .disconnect = usb_serial_disconnect,
98 .id_table = id_table_combined,
99};
100
Mike Isely3416eaa2008-02-10 20:23:19 -0600101enum packet_format {
102 packet_format_1, /* b0:status, b1:payload count */
103 packet_format_2 /* b0[7:3]:status, b0[2:0]:payload count */
104};
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106struct cypress_private {
107 spinlock_t lock; /* private lock */
108 int chiptype; /* identifier of device, for quirks/etc */
109 int bytes_in; /* used for statistics */
110 int bytes_out; /* used for statistics */
111 int cmd_count; /* used for statistics */
112 int cmd_ctrl; /* always set this to 1 before issuing a command */
Johan Hovold117fb8d2010-05-16 20:33:50 +0200113 struct kfifo write_fifo; /* write fifo */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 int write_urb_in_use; /* write urb in use indicator */
Mike Isely0257fa92006-08-29 22:06:59 -0500115 int write_urb_interval; /* interval to use for write urb */
116 int read_urb_interval; /* interval to use for read urb */
Mike Isely78aef512006-08-29 22:07:11 -0500117 int comm_is_ok; /* true if communication is (still) ok */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 int termios_initialized;
119 __u8 line_control; /* holds dtr / rts value */
120 __u8 current_status; /* received from last read - info on dsr,cts,cd,ri,etc */
121 __u8 current_config; /* stores the current configuration byte */
122 __u8 rx_flags; /* throttling - used from whiteheat/ftdi_sio */
Mike Isely3416eaa2008-02-10 20:23:19 -0600123 enum packet_format pkt_fmt; /* format to use for packet send / receive */
Mike Isely3d6aa322008-02-10 20:23:24 -0600124 int get_cfg_unsafe; /* If true, the CYPRESS_GET_CONFIG is unsafe */
Alan Cox813a2242008-07-22 11:10:36 +0100125 int baud_rate; /* stores current baud rate in
126 integer form */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 int isthrottled; /* if throttled, discard reads */
128 wait_queue_head_t delta_msr_wait; /* used for TIOCMIWAIT */
129 char prev_status, diff_status; /* used for TIOCMIWAIT */
Daniel Mack3ad2f3f2010-02-03 08:01:28 +0800130 /* we pass a pointer to this as the argument sent to
Alan Cox813a2242008-07-22 11:10:36 +0100131 cypress_set_termios old_termios */
Alan Cox606d0992006-12-08 02:38:45 -0800132 struct ktermios tmp_termios; /* stores the old termios settings */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133};
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135/* function prototypes for the Cypress USB to serial device */
Alan Cox813a2242008-07-22 11:10:36 +0100136static int cypress_earthmate_startup(struct usb_serial *serial);
137static int cypress_hidcom_startup(struct usb_serial *serial);
138static int cypress_ca42v2_startup(struct usb_serial *serial);
Alan Sternf9c99bb2009-06-02 11:53:55 -0400139static void cypress_release(struct usb_serial *serial);
Alan Coxa509a7e2009-09-19 13:13:26 -0700140static int cypress_open(struct tty_struct *tty, struct usb_serial_port *port);
Alan Cox335f8512009-06-11 12:26:29 +0100141static void cypress_close(struct usb_serial_port *port);
142static void cypress_dtr_rts(struct usb_serial_port *port, int on);
Alan Cox813a2242008-07-22 11:10:36 +0100143static int cypress_write(struct tty_struct *tty, struct usb_serial_port *port,
144 const unsigned char *buf, int count);
145static void cypress_send(struct usb_serial_port *port);
146static int cypress_write_room(struct tty_struct *tty);
Alan Cox00a0d0d2011-02-14 16:27:06 +0000147static int cypress_ioctl(struct tty_struct *tty,
Alan Cox813a2242008-07-22 11:10:36 +0100148 unsigned int cmd, unsigned long arg);
149static void cypress_set_termios(struct tty_struct *tty,
150 struct usb_serial_port *port, struct ktermios *old);
Alan Cox60b33c12011-02-14 16:26:14 +0000151static int cypress_tiocmget(struct tty_struct *tty);
Alan Cox20b9d172011-02-14 16:26:50 +0000152static int cypress_tiocmset(struct tty_struct *tty,
Alan Cox813a2242008-07-22 11:10:36 +0100153 unsigned int set, unsigned int clear);
154static int cypress_chars_in_buffer(struct tty_struct *tty);
155static void cypress_throttle(struct tty_struct *tty);
156static void cypress_unthrottle(struct tty_struct *tty);
157static void cypress_set_dead(struct usb_serial_port *port);
158static void cypress_read_int_callback(struct urb *urb);
159static void cypress_write_int_callback(struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700161static struct usb_serial_driver cypress_earthmate_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700162 .driver = {
163 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700164 .name = "earthmate",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700165 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700166 .description = "DeLorme Earthmate USB",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 .id_table = id_table_earthmate,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 .num_ports = 1,
169 .attach = cypress_earthmate_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -0400170 .release = cypress_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 .open = cypress_open,
172 .close = cypress_close,
Alan Cox335f8512009-06-11 12:26:29 +0100173 .dtr_rts = cypress_dtr_rts,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 .write = cypress_write,
175 .write_room = cypress_write_room,
176 .ioctl = cypress_ioctl,
177 .set_termios = cypress_set_termios,
178 .tiocmget = cypress_tiocmget,
179 .tiocmset = cypress_tiocmset,
180 .chars_in_buffer = cypress_chars_in_buffer,
181 .throttle = cypress_throttle,
182 .unthrottle = cypress_unthrottle,
183 .read_int_callback = cypress_read_int_callback,
184 .write_int_callback = cypress_write_int_callback,
185};
186
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700187static struct usb_serial_driver cypress_hidcom_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700188 .driver = {
189 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700190 .name = "cyphidcom",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700191 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700192 .description = "HID->COM RS232 Adapter",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 .id_table = id_table_cyphidcomrs232,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 .num_ports = 1,
195 .attach = cypress_hidcom_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -0400196 .release = cypress_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 .open = cypress_open,
198 .close = cypress_close,
Alan Cox335f8512009-06-11 12:26:29 +0100199 .dtr_rts = cypress_dtr_rts,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 .write = cypress_write,
201 .write_room = cypress_write_room,
202 .ioctl = cypress_ioctl,
203 .set_termios = cypress_set_termios,
204 .tiocmget = cypress_tiocmget,
205 .tiocmset = cypress_tiocmset,
206 .chars_in_buffer = cypress_chars_in_buffer,
207 .throttle = cypress_throttle,
208 .unthrottle = cypress_unthrottle,
209 .read_int_callback = cypress_read_int_callback,
210 .write_int_callback = cypress_write_int_callback,
211};
212
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600213static struct usb_serial_driver cypress_ca42v2_device = {
214 .driver = {
215 .owner = THIS_MODULE,
Alan Cox813a2242008-07-22 11:10:36 +0100216 .name = "nokiaca42v2",
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600217 },
218 .description = "Nokia CA-42 V2 Adapter",
219 .id_table = id_table_nokiaca42v2,
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600220 .num_ports = 1,
221 .attach = cypress_ca42v2_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -0400222 .release = cypress_release,
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600223 .open = cypress_open,
224 .close = cypress_close,
Alan Cox335f8512009-06-11 12:26:29 +0100225 .dtr_rts = cypress_dtr_rts,
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600226 .write = cypress_write,
227 .write_room = cypress_write_room,
228 .ioctl = cypress_ioctl,
229 .set_termios = cypress_set_termios,
230 .tiocmget = cypress_tiocmget,
231 .tiocmset = cypress_tiocmset,
232 .chars_in_buffer = cypress_chars_in_buffer,
233 .throttle = cypress_throttle,
234 .unthrottle = cypress_unthrottle,
235 .read_int_callback = cypress_read_int_callback,
236 .write_int_callback = cypress_write_int_callback,
237};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Alan Stern08a4f6b2012-02-23 14:56:17 -0500239static struct usb_serial_driver * const serial_drivers[] = {
240 &cypress_earthmate_device, &cypress_hidcom_device,
241 &cypress_ca42v2_device, NULL
242};
243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244/*****************************************************************************
245 * Cypress serial helper functions
246 *****************************************************************************/
247
Robert Butora617d12d2013-05-31 18:09:51 +0300248/* FRWD Dongle hidcom needs to skip reset and speed checks */
249static inline bool is_frwd(struct usb_device *dev)
250{
251 return ((le16_to_cpu(dev->descriptor.idVendor) == VENDOR_ID_FRWD) &&
252 (le16_to_cpu(dev->descriptor.idProduct) == PRODUCT_ID_CYPHIDCOM_FRWD));
253}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Alan Cox8873aaa2008-03-10 21:59:28 +0000255static int analyze_baud_rate(struct usb_serial_port *port, speed_t new_rate)
Mike Isely92983c22008-02-10 20:23:32 -0600256{
Mike Isely92983c22008-02-10 20:23:32 -0600257 struct cypress_private *priv;
258 priv = usb_get_serial_port_data(port);
259
Mike Frysingerc3126592009-12-18 16:33:03 -0500260 if (unstable_bauds)
261 return new_rate;
262
Robert Butora617d12d2013-05-31 18:09:51 +0300263 /* FRWD Dongle uses 115200 bps */
264 if (is_frwd(port->serial->dev))
265 return new_rate;
266
Mike Isely92983c22008-02-10 20:23:32 -0600267 /*
268 * The general purpose firmware for the Cypress M8 allows for
269 * a maximum speed of 57600bps (I have no idea whether DeLorme
270 * chose to use the general purpose firmware or not), if you
271 * need to modify this speed setting for your own project
272 * please add your own chiptype and modify the code likewise.
273 * The Cypress HID->COM device will work successfully up to
274 * 115200bps (but the actual throughput is around 3kBps).
275 */
Mike Isely92983c22008-02-10 20:23:32 -0600276 if (port->serial->dev->speed == USB_SPEED_LOW) {
277 /*
278 * Mike Isely <isely@pobox.com> 2-Feb-2008: The
279 * Cypress app note that describes this mechanism
280 * states the the low-speed part can't handle more
281 * than 800 bytes/sec, in which case 4800 baud is the
282 * safest speed for a part like that.
283 */
284 if (new_rate > 4800) {
285 dbg("%s - failed setting baud rate, device incapable "
286 "speed %d", __func__, new_rate);
287 return -1;
288 }
289 }
290 switch (priv->chiptype) {
291 case CT_EARTHMATE:
292 if (new_rate <= 600) {
293 /* 300 and 600 baud rates are supported under
294 * the generic firmware, but are not used with
295 * NMEA and SiRF protocols */
296 dbg("%s - failed setting baud rate, unsupported speed "
297 "of %d on Earthmate GPS", __func__, new_rate);
298 return -1;
299 }
300 break;
301 default:
302 break;
303 }
304 return new_rate;
305}
306
307
Steven Cole093cf722005-05-03 19:07:24 -0600308/* This function can either set or retrieve the current serial line settings */
Alan Cox813a2242008-07-22 11:10:36 +0100309static int cypress_serial_control(struct tty_struct *tty,
Alan Cox95da3102008-07-22 11:09:07 +0100310 struct usb_serial_port *port, speed_t baud_rate, int data_bits,
311 int stop_bits, int parity_enable, int parity_type, int reset,
312 int cypress_request_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500314 int new_baudrate = 0, retval = 0, tries = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 struct cypress_private *priv;
Johan Hovold09546442009-12-28 23:01:48 +0100316 u8 *feature_buffer;
317 const unsigned int feature_len = 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 unsigned long flags;
319
Harvey Harrison441b62c2008-03-03 16:08:34 -0800320 dbg("%s", __func__);
Alan Cox813a2242008-07-22 11:10:36 +0100321
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 priv = usb_get_serial_port_data(port);
323
Mike Isely78aef512006-08-29 22:07:11 -0500324 if (!priv->comm_is_ok)
325 return -ENODEV;
326
Johan Hovold09546442009-12-28 23:01:48 +0100327 feature_buffer = kcalloc(feature_len, sizeof(u8), GFP_KERNEL);
328 if (!feature_buffer)
329 return -ENOMEM;
330
Alan Cox813a2242008-07-22 11:10:36 +0100331 switch (cypress_request_type) {
332 case CYPRESS_SET_CONFIG:
Alan Cox813a2242008-07-22 11:10:36 +0100333 /* 0 means 'Hang up' so doesn't change the true bit rate */
Mike Frysinger2805eb12009-12-18 16:33:02 -0500334 new_baudrate = priv->baud_rate;
335 if (baud_rate && baud_rate != priv->baud_rate) {
Alan Cox813a2242008-07-22 11:10:36 +0100336 dbg("%s - baud rate is changing", __func__);
337 retval = analyze_baud_rate(port, baud_rate);
Mike Frysinger2805eb12009-12-18 16:33:02 -0500338 if (retval >= 0) {
Alan Cox813a2242008-07-22 11:10:36 +0100339 new_baudrate = retval;
340 dbg("%s - New baud rate set to %d",
341 __func__, new_baudrate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 }
Alan Cox813a2242008-07-22 11:10:36 +0100343 }
344 dbg("%s - baud rate is being sent as %d",
345 __func__, new_baudrate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Alan Cox813a2242008-07-22 11:10:36 +0100347 /* fill the feature_buffer with new configuration */
Johan Hovold0f2c2d72009-12-31 16:48:01 +0100348 put_unaligned_le32(new_baudrate, feature_buffer);
Alan Cox813a2242008-07-22 11:10:36 +0100349 feature_buffer[4] |= data_bits; /* assign data bits in 2 bit space ( max 3 ) */
350 /* 1 bit gap */
351 feature_buffer[4] |= (stop_bits << 3); /* assign stop bits in 1 bit space */
352 feature_buffer[4] |= (parity_enable << 4); /* assign parity flag in 1 bit space */
353 feature_buffer[4] |= (parity_type << 5); /* assign parity type in 1 bit space */
354 /* 1 bit gap */
355 feature_buffer[4] |= (reset << 7); /* assign reset at end of byte, 1 bit space */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Alan Cox813a2242008-07-22 11:10:36 +0100357 dbg("%s - device is being sent this feature report:",
358 __func__);
359 dbg("%s - %02X - %02X - %02X - %02X - %02X", __func__,
360 feature_buffer[0], feature_buffer[1],
361 feature_buffer[2], feature_buffer[3],
362 feature_buffer[4]);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500363
Alan Cox813a2242008-07-22 11:10:36 +0100364 do {
365 retval = usb_control_msg(port->serial->dev,
366 usb_sndctrlpipe(port->serial->dev, 0),
367 HID_REQ_SET_REPORT,
368 USB_DIR_OUT | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
369 0x0300, 0, feature_buffer,
Johan Hovold09546442009-12-28 23:01:48 +0100370 feature_len, 500);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500371
Alan Cox813a2242008-07-22 11:10:36 +0100372 if (tries++ >= 3)
373 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Johan Hovold09546442009-12-28 23:01:48 +0100375 } while (retval != feature_len &&
Alan Cox813a2242008-07-22 11:10:36 +0100376 retval != -ENODEV);
Mike Isely93075542008-02-10 20:23:14 -0600377
Johan Hovold09546442009-12-28 23:01:48 +0100378 if (retval != feature_len) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700379 dev_err(&port->dev, "%s - failed sending serial "
380 "line settings - %d\n", __func__, retval);
Alan Cox813a2242008-07-22 11:10:36 +0100381 cypress_set_dead(port);
382 } else {
383 spin_lock_irqsave(&priv->lock, flags);
384 priv->baud_rate = new_baudrate;
385 priv->current_config = feature_buffer[4];
386 spin_unlock_irqrestore(&priv->lock, flags);
387 /* If we asked for a speed change encode it */
388 if (baud_rate)
389 tty_encode_baud_rate(tty,
390 new_baudrate, new_baudrate);
391 }
392 break;
393 case CYPRESS_GET_CONFIG:
394 if (priv->get_cfg_unsafe) {
395 /* Not implemented for this device,
396 and if we try to do it we're likely
397 to crash the hardware. */
Johan Hovold09546442009-12-28 23:01:48 +0100398 retval = -ENOTTY;
399 goto out;
Alan Cox813a2242008-07-22 11:10:36 +0100400 }
401 dbg("%s - retreiving serial line settings", __func__);
Alan Cox813a2242008-07-22 11:10:36 +0100402 do {
403 retval = usb_control_msg(port->serial->dev,
404 usb_rcvctrlpipe(port->serial->dev, 0),
405 HID_REQ_GET_REPORT,
406 USB_DIR_IN | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
407 0x0300, 0, feature_buffer,
Johan Hovold09546442009-12-28 23:01:48 +0100408 feature_len, 500);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500409
Alan Cox813a2242008-07-22 11:10:36 +0100410 if (tries++ >= 3)
411 break;
Johan Hovold09546442009-12-28 23:01:48 +0100412 } while (retval != feature_len
Alan Cox813a2242008-07-22 11:10:36 +0100413 && retval != -ENODEV);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500414
Johan Hovold09546442009-12-28 23:01:48 +0100415 if (retval != feature_len) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700416 dev_err(&port->dev, "%s - failed to retrieve serial "
417 "line settings - %d\n", __func__, retval);
Alan Cox813a2242008-07-22 11:10:36 +0100418 cypress_set_dead(port);
Johan Hovold09546442009-12-28 23:01:48 +0100419 goto out;
Alan Cox813a2242008-07-22 11:10:36 +0100420 } else {
421 spin_lock_irqsave(&priv->lock, flags);
422 /* store the config in one byte, and later
423 use bit masks to check values */
424 priv->current_config = feature_buffer[4];
Johan Hovold0f2c2d72009-12-31 16:48:01 +0100425 priv->baud_rate = get_unaligned_le32(feature_buffer);
Alan Cox813a2242008-07-22 11:10:36 +0100426 spin_unlock_irqrestore(&priv->lock, flags);
427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 }
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500429 spin_lock_irqsave(&priv->lock, flags);
430 ++priv->cmd_count;
431 spin_unlock_irqrestore(&priv->lock, flags);
Johan Hovold09546442009-12-28 23:01:48 +0100432out:
433 kfree(feature_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 return retval;
435} /* cypress_serial_control */
436
437
Mike Isely78aef512006-08-29 22:07:11 -0500438static void cypress_set_dead(struct usb_serial_port *port)
439{
440 struct cypress_private *priv = usb_get_serial_port_data(port);
441 unsigned long flags;
442
443 spin_lock_irqsave(&priv->lock, flags);
444 if (!priv->comm_is_ok) {
445 spin_unlock_irqrestore(&priv->lock, flags);
446 return;
447 }
448 priv->comm_is_ok = 0;
449 spin_unlock_irqrestore(&priv->lock, flags);
450
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700451 dev_err(&port->dev, "cypress_m8 suspending failing port %d - "
452 "interval might be too short\n", port->number);
Mike Isely78aef512006-08-29 22:07:11 -0500453}
454
455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456/*****************************************************************************
457 * Cypress serial driver functions
458 *****************************************************************************/
459
460
Alan Cox813a2242008-07-22 11:10:36 +0100461static int generic_startup(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
463 struct cypress_private *priv;
Mike Isely0257fa92006-08-29 22:06:59 -0500464 struct usb_serial_port *port = serial->port[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Harvey Harrison441b62c2008-03-03 16:08:34 -0800466 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Alan Cox813a2242008-07-22 11:10:36 +0100468 priv = kzalloc(sizeof(struct cypress_private), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 if (!priv)
470 return -ENOMEM;
471
Mike Isely78aef512006-08-29 22:07:11 -0500472 priv->comm_is_ok = !0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 spin_lock_init(&priv->lock);
Johan Hovold117fb8d2010-05-16 20:33:50 +0200474 if (kfifo_alloc(&priv->write_fifo, CYPRESS_BUF_SIZE, GFP_KERNEL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 kfree(priv);
476 return -ENOMEM;
477 }
478 init_waitqueue_head(&priv->delta_msr_wait);
Alan Cox813a2242008-07-22 11:10:36 +0100479
Robert Butora617d12d2013-05-31 18:09:51 +0300480 /* Skip reset for FRWD device. It is a workaound:
481 device hangs if it receives SET_CONFIGURE in Configured
482 state. */
483 if (!is_frwd(serial->dev))
484 usb_reset_configuration(serial->dev);
Alan Cox813a2242008-07-22 11:10:36 +0100485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 priv->cmd_ctrl = 0;
487 priv->line_control = 0;
488 priv->termios_initialized = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 priv->rx_flags = 0;
Mike Isely3416eaa2008-02-10 20:23:19 -0600490 /* Default packet format setting is determined by packet size.
491 Anything with a size larger then 9 must have a separate
492 count field since the 3 bit count field is otherwise too
493 small. Otherwise we can use the slightly more compact
494 format. This is in accordance with the cypress_m8 serial
495 converter app note. */
Alan Cox813a2242008-07-22 11:10:36 +0100496 if (port->interrupt_out_size > 9)
Mike Isely3416eaa2008-02-10 20:23:19 -0600497 priv->pkt_fmt = packet_format_1;
Alan Cox813a2242008-07-22 11:10:36 +0100498 else
Mike Isely3416eaa2008-02-10 20:23:19 -0600499 priv->pkt_fmt = packet_format_2;
Alan Cox813a2242008-07-22 11:10:36 +0100500
Mike Isely0257fa92006-08-29 22:06:59 -0500501 if (interval > 0) {
502 priv->write_urb_interval = interval;
503 priv->read_urb_interval = interval;
504 dbg("%s - port %d read & write intervals forced to %d",
Alan Cox813a2242008-07-22 11:10:36 +0100505 __func__, port->number, interval);
Mike Isely0257fa92006-08-29 22:06:59 -0500506 } else {
507 priv->write_urb_interval = port->interrupt_out_urb->interval;
508 priv->read_urb_interval = port->interrupt_in_urb->interval;
509 dbg("%s - port %d intervals: read=%d write=%d",
Alan Cox813a2242008-07-22 11:10:36 +0100510 __func__, port->number,
511 priv->read_urb_interval, priv->write_urb_interval);
Mike Isely0257fa92006-08-29 22:06:59 -0500512 }
513 usb_set_serial_port_data(port, priv);
Alan Cox813a2242008-07-22 11:10:36 +0100514
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500515 return 0;
516}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
518
Alan Cox813a2242008-07-22 11:10:36 +0100519static int cypress_earthmate_startup(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
521 struct cypress_private *priv;
Mike Isely3d6aa322008-02-10 20:23:24 -0600522 struct usb_serial_port *port = serial->port[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Harvey Harrison441b62c2008-03-03 16:08:34 -0800524 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
526 if (generic_startup(serial)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800527 dbg("%s - Failed setting up port %d", __func__,
Mike Isely3d6aa322008-02-10 20:23:24 -0600528 port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 return 1;
530 }
531
Mike Isely3d6aa322008-02-10 20:23:24 -0600532 priv = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 priv->chiptype = CT_EARTHMATE;
Mike Isely3416eaa2008-02-10 20:23:19 -0600534 /* All Earthmate devices use the separated-count packet
535 format! Idiotic. */
536 priv->pkt_fmt = packet_format_1;
Alan Cox813a2242008-07-22 11:10:36 +0100537 if (serial->dev->descriptor.idProduct !=
538 cpu_to_le16(PRODUCT_ID_EARTHMATEUSB)) {
Mike Isely3d6aa322008-02-10 20:23:24 -0600539 /* The old original USB Earthmate seemed able to
540 handle GET_CONFIG requests; everything they've
541 produced since that time crashes if this command is
542 attempted :-( */
543 dbg("%s - Marking this device as unsafe for GET_CONFIG "
544 "commands", __func__);
545 priv->get_cfg_unsafe = !0;
546 }
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500547
548 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549} /* cypress_earthmate_startup */
550
551
Alan Cox813a2242008-07-22 11:10:36 +0100552static int cypress_hidcom_startup(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
554 struct cypress_private *priv;
555
Harvey Harrison441b62c2008-03-03 16:08:34 -0800556 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
558 if (generic_startup(serial)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800559 dbg("%s - Failed setting up port %d", __func__,
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500560 serial->port[0]->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 return 1;
562 }
563
564 priv = usb_get_serial_port_data(serial->port[0]);
565 priv->chiptype = CT_CYPHIDCOM;
Alan Cox813a2242008-07-22 11:10:36 +0100566
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500567 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568} /* cypress_hidcom_startup */
569
570
Alan Cox813a2242008-07-22 11:10:36 +0100571static int cypress_ca42v2_startup(struct usb_serial *serial)
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600572{
573 struct cypress_private *priv;
574
Harvey Harrison441b62c2008-03-03 16:08:34 -0800575 dbg("%s", __func__);
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600576
577 if (generic_startup(serial)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800578 dbg("%s - Failed setting up port %d", __func__,
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600579 serial->port[0]->number);
580 return 1;
581 }
582
583 priv = usb_get_serial_port_data(serial->port[0]);
584 priv->chiptype = CT_CA42V2;
585
586 return 0;
587} /* cypress_ca42v2_startup */
588
589
Alan Sternf9c99bb2009-06-02 11:53:55 -0400590static void cypress_release(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
592 struct cypress_private *priv;
593
Alan Cox813a2242008-07-22 11:10:36 +0100594 dbg("%s - port %d", __func__, serial->port[0]->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
596 /* all open ports are closed at this point */
597
598 priv = usb_get_serial_port_data(serial->port[0]);
599
600 if (priv) {
Johan Hovold117fb8d2010-05-16 20:33:50 +0200601 kfifo_free(&priv->write_fifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 kfree(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 }
604}
605
606
Alan Coxa509a7e2009-09-19 13:13:26 -0700607static int cypress_open(struct tty_struct *tty, struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608{
609 struct cypress_private *priv = usb_get_serial_port_data(port);
610 struct usb_serial *serial = port->serial;
611 unsigned long flags;
612 int result = 0;
613
Harvey Harrison441b62c2008-03-03 16:08:34 -0800614 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Mike Isely78aef512006-08-29 22:07:11 -0500616 if (!priv->comm_is_ok)
617 return -EIO;
618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 /* clear halts before open */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 usb_clear_halt(serial->dev, 0x81);
621 usb_clear_halt(serial->dev, 0x02);
622
623 spin_lock_irqsave(&priv->lock, flags);
624 /* reset read/write statistics */
625 priv->bytes_in = 0;
626 priv->bytes_out = 0;
627 priv->cmd_count = 0;
628 priv->rx_flags = 0;
629 spin_unlock_irqrestore(&priv->lock, flags);
630
Alan Cox335f8512009-06-11 12:26:29 +0100631 /* Set termios */
Alan Coxfe1ae7f2009-09-19 13:13:33 -0700632 cypress_send(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Alan Cox95da3102008-07-22 11:09:07 +0100634 if (tty)
635 cypress_set_termios(tty, port, &priv->tmp_termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
637 /* setup the port and start reading from the device */
Alan Cox813a2242008-07-22 11:10:36 +0100638 if (!port->interrupt_in_urb) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700639 dev_err(&port->dev, "%s - interrupt_in_urb is empty!\n",
640 __func__);
Alan Cox813a2242008-07-22 11:10:36 +0100641 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 }
643
644 usb_fill_int_urb(port->interrupt_in_urb, serial->dev,
645 usb_rcvintpipe(serial->dev, port->interrupt_in_endpointAddress),
Alan Cox813a2242008-07-22 11:10:36 +0100646 port->interrupt_in_urb->transfer_buffer,
647 port->interrupt_in_urb->transfer_buffer_length,
Mike Isely0257fa92006-08-29 22:06:59 -0500648 cypress_read_int_callback, port, priv->read_urb_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
650
Alan Cox813a2242008-07-22 11:10:36 +0100651 if (result) {
652 dev_err(&port->dev,
653 "%s - failed submitting read urb, error %d\n",
654 __func__, result);
Mike Isely78aef512006-08-29 22:07:11 -0500655 cypress_set_dead(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 }
Alan Cox335f8512009-06-11 12:26:29 +0100657 port->port.drain_delay = 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 return result;
659} /* cypress_open */
660
Alan Cox335f8512009-06-11 12:26:29 +0100661static void cypress_dtr_rts(struct usb_serial_port *port, int on)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662{
663 struct cypress_private *priv = usb_get_serial_port_data(port);
Alan Cox335f8512009-06-11 12:26:29 +0100664 /* drop dtr and rts */
Alan Cox335f8512009-06-11 12:26:29 +0100665 spin_lock_irq(&priv->lock);
666 if (on == 0)
667 priv->line_control = 0;
668 else
669 priv->line_control = CONTROL_DTR | CONTROL_RTS;
670 priv->cmd_ctrl = 1;
671 spin_unlock_irq(&priv->lock);
672 cypress_write(NULL, port, NULL, 0);
673}
674
675static void cypress_close(struct usb_serial_port *port)
676{
677 struct cypress_private *priv = usb_get_serial_port_data(port);
Johan Hovold117fb8d2010-05-16 20:33:50 +0200678 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Harvey Harrison441b62c2008-03-03 16:08:34 -0800680 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100682 /* writing is potentially harmful, lock must be taken */
683 mutex_lock(&port->serial->disc_mutex);
684 if (port->serial->disconnected) {
685 mutex_unlock(&port->serial->disc_mutex);
686 return;
687 }
Johan Hovold117fb8d2010-05-16 20:33:50 +0200688 spin_lock_irqsave(&priv->lock, flags);
689 kfifo_reset_out(&priv->write_fifo);
690 spin_unlock_irqrestore(&priv->lock, flags);
691
Harvey Harrison441b62c2008-03-03 16:08:34 -0800692 dbg("%s - stopping urbs", __func__);
Alan Cox813a2242008-07-22 11:10:36 +0100693 usb_kill_urb(port->interrupt_in_urb);
694 usb_kill_urb(port->interrupt_out_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 if (stats)
Alan Cox813a2242008-07-22 11:10:36 +0100697 dev_info(&port->dev, "Statistics: %d Bytes In | %d Bytes Out | %d Commands Issued\n",
698 priv->bytes_in, priv->bytes_out, priv->cmd_count);
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100699 mutex_unlock(&port->serial->disc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700} /* cypress_close */
701
702
Alan Cox95da3102008-07-22 11:09:07 +0100703static int cypress_write(struct tty_struct *tty, struct usb_serial_port *port,
704 const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705{
706 struct cypress_private *priv = usb_get_serial_port_data(port);
Alan Cox813a2242008-07-22 11:10:36 +0100707
Harvey Harrison441b62c2008-03-03 16:08:34 -0800708 dbg("%s - port %d, %d bytes", __func__, port->number, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
710 /* line control commands, which need to be executed immediately,
711 are not put into the buffer for obvious reasons.
712 */
713 if (priv->cmd_ctrl) {
714 count = 0;
715 goto finish;
716 }
Alan Cox813a2242008-07-22 11:10:36 +0100717
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 if (!count)
719 return count;
Alan Cox813a2242008-07-22 11:10:36 +0100720
Johan Hovold117fb8d2010-05-16 20:33:50 +0200721 count = kfifo_in_locked(&priv->write_fifo, buf, count, &priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723finish:
724 cypress_send(port);
725
726 return count;
727} /* cypress_write */
728
729
730static void cypress_send(struct usb_serial_port *port)
731{
732 int count = 0, result, offset, actual_size;
733 struct cypress_private *priv = usb_get_serial_port_data(port);
734 unsigned long flags;
Alan Cox813a2242008-07-22 11:10:36 +0100735
Mike Isely78aef512006-08-29 22:07:11 -0500736 if (!priv->comm_is_ok)
737 return;
738
Harvey Harrison441b62c2008-03-03 16:08:34 -0800739 dbg("%s - port %d", __func__, port->number);
Alan Cox813a2242008-07-22 11:10:36 +0100740 dbg("%s - interrupt out size is %d", __func__,
741 port->interrupt_out_size);
742
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 spin_lock_irqsave(&priv->lock, flags);
744 if (priv->write_urb_in_use) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800745 dbg("%s - can't write, urb in use", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 spin_unlock_irqrestore(&priv->lock, flags);
747 return;
748 }
749 spin_unlock_irqrestore(&priv->lock, flags);
750
751 /* clear buffer */
Alan Cox813a2242008-07-22 11:10:36 +0100752 memset(port->interrupt_out_urb->transfer_buffer, 0,
753 port->interrupt_out_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
755 spin_lock_irqsave(&priv->lock, flags);
Mike Isely3416eaa2008-02-10 20:23:19 -0600756 switch (priv->pkt_fmt) {
757 default:
758 case packet_format_1:
759 /* this is for the CY7C64013... */
760 offset = 2;
761 port->interrupt_out_buffer[0] = priv->line_control;
762 break;
763 case packet_format_2:
764 /* this is for the CY7C63743... */
765 offset = 1;
766 port->interrupt_out_buffer[0] = priv->line_control;
767 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 }
769
770 if (priv->line_control & CONTROL_RESET)
771 priv->line_control &= ~CONTROL_RESET;
772
773 if (priv->cmd_ctrl) {
774 priv->cmd_count++;
Harvey Harrison441b62c2008-03-03 16:08:34 -0800775 dbg("%s - line control command being issued", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 spin_unlock_irqrestore(&priv->lock, flags);
777 goto send;
778 } else
779 spin_unlock_irqrestore(&priv->lock, flags);
780
Johan Hovold117fb8d2010-05-16 20:33:50 +0200781 count = kfifo_out_locked(&priv->write_fifo,
782 &port->interrupt_out_buffer[offset],
783 port->interrupt_out_size - offset,
784 &priv->lock);
Alan Cox813a2242008-07-22 11:10:36 +0100785 if (count == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Mike Isely3416eaa2008-02-10 20:23:19 -0600788 switch (priv->pkt_fmt) {
789 default:
790 case packet_format_1:
791 port->interrupt_out_buffer[1] = count;
792 break;
793 case packet_format_2:
794 port->interrupt_out_buffer[0] |= count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 }
796
Harvey Harrison441b62c2008-03-03 16:08:34 -0800797 dbg("%s - count is %d", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
799send:
800 spin_lock_irqsave(&priv->lock, flags);
801 priv->write_urb_in_use = 1;
802 spin_unlock_irqrestore(&priv->lock, flags);
803
804 if (priv->cmd_ctrl)
805 actual_size = 1;
806 else
Mike Isely3416eaa2008-02-10 20:23:19 -0600807 actual_size = count +
808 (priv->pkt_fmt == packet_format_1 ? 2 : 1);
809
Alan Cox813a2242008-07-22 11:10:36 +0100810 usb_serial_debug_data(debug, &port->dev, __func__,
811 port->interrupt_out_size,
812 port->interrupt_out_urb->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Mike Isely9aa8dae2006-08-29 22:07:04 -0500814 usb_fill_int_urb(port->interrupt_out_urb, port->serial->dev,
815 usb_sndintpipe(port->serial->dev, port->interrupt_out_endpointAddress),
816 port->interrupt_out_buffer, port->interrupt_out_size,
817 cypress_write_int_callback, port, priv->write_urb_interval);
Alan Cox813a2242008-07-22 11:10:36 +0100818 result = usb_submit_urb(port->interrupt_out_urb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 if (result) {
Johan Hovold22a416c2012-02-10 13:20:51 +0100820 dev_err_console(port,
Alan Cox813a2242008-07-22 11:10:36 +0100821 "%s - failed submitting write urb, error %d\n",
822 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 priv->write_urb_in_use = 0;
Mike Isely78aef512006-08-29 22:07:11 -0500824 cypress_set_dead(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 }
826
827 spin_lock_irqsave(&priv->lock, flags);
Alan Cox813a2242008-07-22 11:10:36 +0100828 if (priv->cmd_ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 priv->cmd_ctrl = 0;
Alan Cox813a2242008-07-22 11:10:36 +0100830
831 /* do not count the line control and size bytes */
832 priv->bytes_out += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 spin_unlock_irqrestore(&priv->lock, flags);
834
Pete Zaitcevcf2c7482006-05-22 21:58:49 -0700835 usb_serial_port_softint(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836} /* cypress_send */
837
838
839/* returns how much space is available in the soft buffer */
Alan Cox95da3102008-07-22 11:09:07 +0100840static int cypress_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841{
Alan Cox95da3102008-07-22 11:09:07 +0100842 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 struct cypress_private *priv = usb_get_serial_port_data(port);
844 int room = 0;
845 unsigned long flags;
846
Harvey Harrison441b62c2008-03-03 16:08:34 -0800847 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849 spin_lock_irqsave(&priv->lock, flags);
Johan Hovold117fb8d2010-05-16 20:33:50 +0200850 room = kfifo_avail(&priv->write_fifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 spin_unlock_irqrestore(&priv->lock, flags);
852
Harvey Harrison441b62c2008-03-03 16:08:34 -0800853 dbg("%s - returns %d", __func__, room);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 return room;
855}
856
857
Alan Cox60b33c12011-02-14 16:26:14 +0000858static int cypress_tiocmget(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859{
Alan Cox95da3102008-07-22 11:09:07 +0100860 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 struct cypress_private *priv = usb_get_serial_port_data(port);
862 __u8 status, control;
863 unsigned int result = 0;
864 unsigned long flags;
Alan Cox813a2242008-07-22 11:10:36 +0100865
Harvey Harrison441b62c2008-03-03 16:08:34 -0800866 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
868 spin_lock_irqsave(&priv->lock, flags);
869 control = priv->line_control;
870 status = priv->current_status;
871 spin_unlock_irqrestore(&priv->lock, flags);
872
873 result = ((control & CONTROL_DTR) ? TIOCM_DTR : 0)
874 | ((control & CONTROL_RTS) ? TIOCM_RTS : 0)
875 | ((status & UART_CTS) ? TIOCM_CTS : 0)
876 | ((status & UART_DSR) ? TIOCM_DSR : 0)
877 | ((status & UART_RI) ? TIOCM_RI : 0)
878 | ((status & UART_CD) ? TIOCM_CD : 0);
879
Harvey Harrison441b62c2008-03-03 16:08:34 -0800880 dbg("%s - result = %x", __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
882 return result;
883}
884
885
Alan Cox20b9d172011-02-14 16:26:50 +0000886static int cypress_tiocmset(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 unsigned int set, unsigned int clear)
888{
Alan Cox95da3102008-07-22 11:09:07 +0100889 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 struct cypress_private *priv = usb_get_serial_port_data(port);
891 unsigned long flags;
Alan Cox813a2242008-07-22 11:10:36 +0100892
Harvey Harrison441b62c2008-03-03 16:08:34 -0800893 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
895 spin_lock_irqsave(&priv->lock, flags);
896 if (set & TIOCM_RTS)
897 priv->line_control |= CONTROL_RTS;
898 if (set & TIOCM_DTR)
899 priv->line_control |= CONTROL_DTR;
900 if (clear & TIOCM_RTS)
901 priv->line_control &= ~CONTROL_RTS;
902 if (clear & TIOCM_DTR)
903 priv->line_control &= ~CONTROL_DTR;
Alan Cox8873aaa2008-03-10 21:59:28 +0000904 priv->cmd_ctrl = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 spin_unlock_irqrestore(&priv->lock, flags);
906
Alan Cox95da3102008-07-22 11:09:07 +0100907 return cypress_write(tty, port, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908}
909
910
Alan Cox00a0d0d2011-02-14 16:27:06 +0000911static int cypress_ioctl(struct tty_struct *tty,
Alan Cox95da3102008-07-22 11:09:07 +0100912 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913{
Alan Cox95da3102008-07-22 11:09:07 +0100914 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 struct cypress_private *priv = usb_get_serial_port_data(port);
916
Harvey Harrison441b62c2008-03-03 16:08:34 -0800917 dbg("%s - port %d, cmd 0x%.4x", __func__, port->number, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
919 switch (cmd) {
Alan Cox813a2242008-07-22 11:10:36 +0100920 /* This code comes from drivers/char/serial.c and ftdi_sio.c */
921 case TIOCMIWAIT:
922 while (priv != NULL) {
923 interruptible_sleep_on(&priv->delta_msr_wait);
924 /* see if a signal did it */
925 if (signal_pending(current))
926 return -ERESTARTSYS;
927 else {
928 char diff = priv->diff_status;
929 if (diff == 0)
930 return -EIO; /* no change => error */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Alan Cox813a2242008-07-22 11:10:36 +0100932 /* consume all events */
933 priv->diff_status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
Alan Cox813a2242008-07-22 11:10:36 +0100935 /* return 0 if caller wanted to know about
936 these bits */
937 if (((arg & TIOCM_RNG) && (diff & UART_RI)) ||
938 ((arg & TIOCM_DSR) && (diff & UART_DSR)) ||
939 ((arg & TIOCM_CD) && (diff & UART_CD)) ||
940 ((arg & TIOCM_CTS) && (diff & UART_CTS)))
941 return 0;
942 /* otherwise caller can't care less about what
943 * happened, and so we continue to wait for
944 * more events.
945 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 }
Alan Cox813a2242008-07-22 11:10:36 +0100947 }
948 return 0;
949 default:
950 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 }
Harvey Harrison441b62c2008-03-03 16:08:34 -0800952 dbg("%s - arg not supported - it was 0x%04x - check include/asm/ioctls.h", __func__, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 return -ENOIOCTLCMD;
954} /* cypress_ioctl */
955
956
Alan Cox95da3102008-07-22 11:09:07 +0100957static void cypress_set_termios(struct tty_struct *tty,
958 struct usb_serial_port *port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959{
960 struct cypress_private *priv = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 int data_bits, stop_bits, parity_type, parity_enable;
Alan Cox8873aaa2008-03-10 21:59:28 +0000962 unsigned cflag, iflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 unsigned long flags;
964 __u8 oldlines;
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500965 int linechange = 0;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500966
Harvey Harrison441b62c2008-03-03 16:08:34 -0800967 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 spin_lock_irqsave(&priv->lock, flags);
Alan Coxfe1ae7f2009-09-19 13:13:33 -0700970 /* We can't clean this one up as we don't know the device type
971 early enough */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 if (!priv->termios_initialized) {
973 if (priv->chiptype == CT_EARTHMATE) {
974 *(tty->termios) = tty_std_termios;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500975 tty->termios->c_cflag = B4800 | CS8 | CREAD | HUPCL |
976 CLOCAL;
Alan Cox8873aaa2008-03-10 21:59:28 +0000977 tty->termios->c_ispeed = 4800;
978 tty->termios->c_ospeed = 4800;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 } else if (priv->chiptype == CT_CYPHIDCOM) {
980 *(tty->termios) = tty_std_termios;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500981 tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
982 CLOCAL;
Alan Cox8873aaa2008-03-10 21:59:28 +0000983 tty->termios->c_ispeed = 9600;
984 tty->termios->c_ospeed = 9600;
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600985 } else if (priv->chiptype == CT_CA42V2) {
986 *(tty->termios) = tty_std_termios;
987 tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
988 CLOCAL;
Alan Cox8873aaa2008-03-10 21:59:28 +0000989 tty->termios->c_ispeed = 9600;
990 tty->termios->c_ospeed = 9600;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 }
992 priv->termios_initialized = 1;
993 }
994 spin_unlock_irqrestore(&priv->lock, flags);
995
Alan Cox8873aaa2008-03-10 21:59:28 +0000996 /* Unsupported features need clearing */
997 tty->termios->c_cflag &= ~(CMSPAR|CRTSCTS);
998
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 cflag = tty->termios->c_cflag;
1000 iflag = tty->termios->c_iflag;
1001
1002 /* check if there are new settings */
1003 if (old_termios) {
Alan Cox8873aaa2008-03-10 21:59:28 +00001004 spin_lock_irqsave(&priv->lock, flags);
1005 priv->tmp_termios = *(tty->termios);
1006 spin_unlock_irqrestore(&priv->lock, flags);
1007 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
1009 /* set number of data bits, parity, stop bits */
1010 /* when parity is disabled the parity type bit is ignored */
1011
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001012 /* 1 means 2 stop bits, 0 means 1 stop bit */
1013 stop_bits = cflag & CSTOPB ? 1 : 0;
1014
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 if (cflag & PARENB) {
1016 parity_enable = 1;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001017 /* 1 means odd parity, 0 means even parity */
1018 parity_type = cflag & PARODD ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 } else
1020 parity_enable = parity_type = 0;
1021
Alan Cox77336822008-07-22 11:10:53 +01001022 switch (cflag & CSIZE) {
1023 case CS5:
1024 data_bits = 0;
1025 break;
1026 case CS6:
1027 data_bits = 1;
1028 break;
1029 case CS7:
1030 data_bits = 2;
1031 break;
1032 case CS8:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 data_bits = 3;
Alan Cox77336822008-07-22 11:10:53 +01001034 break;
1035 default:
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001036 dev_err(&port->dev, "%s - CSIZE was set, but not CS5-CS8\n",
1037 __func__);
Alan Cox77336822008-07-22 11:10:53 +01001038 data_bits = 3;
1039 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 spin_lock_irqsave(&priv->lock, flags);
1041 oldlines = priv->line_control;
1042 if ((cflag & CBAUD) == B0) {
1043 /* drop dtr and rts */
Harvey Harrison441b62c2008-03-03 16:08:34 -08001044 dbg("%s - dropping the lines, baud rate 0bps", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
Alan Cox8873aaa2008-03-10 21:59:28 +00001046 } else
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001047 priv->line_control = (CONTROL_DTR | CONTROL_RTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001050 dbg("%s - sending %d stop_bits, %d parity_enable, %d parity_type, "
Harvey Harrison441b62c2008-03-03 16:08:34 -08001051 "%d data_bits (+5)", __func__, stop_bits,
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001052 parity_enable, parity_type, data_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
Alan Cox813a2242008-07-22 11:10:36 +01001054 cypress_serial_control(tty, port, tty_get_baud_rate(tty),
1055 data_bits, stop_bits,
1056 parity_enable, parity_type,
1057 0, CYPRESS_SET_CONFIG);
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001058
1059 /* we perform a CYPRESS_GET_CONFIG so that the current settings are
1060 * filled into the private structure this should confirm that all is
1061 * working if it returns what we just set */
Alan Cox95da3102008-07-22 11:09:07 +01001062 cypress_serial_control(tty, port, 0, 0, 0, 0, 0, 0, CYPRESS_GET_CONFIG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001064 /* Here we can define custom tty settings for devices; the main tty
1065 * termios flag base comes from empeg.c */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001067 spin_lock_irqsave(&priv->lock, flags);
Alan Cox813a2242008-07-22 11:10:36 +01001068 if (priv->chiptype == CT_EARTHMATE && priv->baud_rate == 4800) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001069 dbg("Using custom termios settings for a baud rate of "
1070 "4800bps.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 /* define custom termios settings for NMEA protocol */
1072
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 tty->termios->c_iflag /* input modes - */
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001074 &= ~(IGNBRK /* disable ignore break */
1075 | BRKINT /* disable break causes interrupt */
1076 | PARMRK /* disable mark parity errors */
1077 | ISTRIP /* disable clear high bit of input char */
1078 | INLCR /* disable translate NL to CR */
1079 | IGNCR /* disable ignore CR */
1080 | ICRNL /* disable translate CR to NL */
1081 | IXON); /* disable enable XON/XOFF flow control */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001083 tty->termios->c_oflag /* output modes */
1084 &= ~OPOST; /* disable postprocess output char */
1085
1086 tty->termios->c_lflag /* line discipline modes */
1087 &= ~(ECHO /* disable echo input characters */
1088 | ECHONL /* disable echo new line */
1089 | ICANON /* disable erase, kill, werase, and rprnt
1090 special characters */
1091 | ISIG /* disable interrupt, quit, and suspend
1092 special characters */
1093 | IEXTEN); /* disable non-POSIX special characters */
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001094 } /* CT_CYPHIDCOM: Application should handle this for device */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 linechange = (priv->line_control != oldlines);
1097 spin_unlock_irqrestore(&priv->lock, flags);
1098
1099 /* if necessary, set lines */
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001100 if (linechange) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 priv->cmd_ctrl = 1;
Alan Cox95da3102008-07-22 11:09:07 +01001102 cypress_write(tty, port, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104} /* cypress_set_termios */
1105
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001106
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107/* returns amount of data still left in soft buffer */
Alan Cox95da3102008-07-22 11:09:07 +01001108static int cypress_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109{
Alan Cox95da3102008-07-22 11:09:07 +01001110 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 struct cypress_private *priv = usb_get_serial_port_data(port);
1112 int chars = 0;
1113 unsigned long flags;
1114
Harvey Harrison441b62c2008-03-03 16:08:34 -08001115 dbg("%s - port %d", __func__, port->number);
Alan Cox813a2242008-07-22 11:10:36 +01001116
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 spin_lock_irqsave(&priv->lock, flags);
Johan Hovold117fb8d2010-05-16 20:33:50 +02001118 chars = kfifo_len(&priv->write_fifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 spin_unlock_irqrestore(&priv->lock, flags);
1120
Harvey Harrison441b62c2008-03-03 16:08:34 -08001121 dbg("%s - returns %d", __func__, chars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 return chars;
1123}
1124
1125
Alan Cox95da3102008-07-22 11:09:07 +01001126static void cypress_throttle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127{
Alan Cox95da3102008-07-22 11:09:07 +01001128 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 struct cypress_private *priv = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
Harvey Harrison441b62c2008-03-03 16:08:34 -08001131 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Oliver Neukum63832512009-10-07 10:50:23 +02001133 spin_lock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 priv->rx_flags = THROTTLED;
Oliver Neukum63832512009-10-07 10:50:23 +02001135 spin_unlock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136}
1137
1138
Alan Cox95da3102008-07-22 11:09:07 +01001139static void cypress_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140{
Alan Cox95da3102008-07-22 11:09:07 +01001141 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 struct cypress_private *priv = usb_get_serial_port_data(port);
1143 int actually_throttled, result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
Harvey Harrison441b62c2008-03-03 16:08:34 -08001145 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
Oliver Neukum63832512009-10-07 10:50:23 +02001147 spin_lock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 actually_throttled = priv->rx_flags & ACTUALLY_THROTTLED;
1149 priv->rx_flags = 0;
Oliver Neukum63832512009-10-07 10:50:23 +02001150 spin_unlock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
Mike Isely78aef512006-08-29 22:07:11 -05001152 if (!priv->comm_is_ok)
1153 return;
1154
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 if (actually_throttled) {
Oliver Neukum63832512009-10-07 10:50:23 +02001156 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
Mike Isely78aef512006-08-29 22:07:11 -05001157 if (result) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001158 dev_err(&port->dev, "%s - failed submitting read urb, "
Harvey Harrison441b62c2008-03-03 16:08:34 -08001159 "error %d\n", __func__, result);
Mike Isely78aef512006-08-29 22:07:11 -05001160 cypress_set_dead(port);
1161 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 }
1163}
1164
1165
David Howells7d12e782006-10-05 14:55:46 +01001166static void cypress_read_int_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167{
Ming Leicdc97792008-02-24 18:41:47 +08001168 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 struct cypress_private *priv = usb_get_serial_port_data(port);
1170 struct tty_struct *tty;
1171 unsigned char *data = urb->transfer_buffer;
1172 unsigned long flags;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001173 char tty_flag = TTY_NORMAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 int havedata = 0;
1175 int bytes = 0;
1176 int result;
1177 int i = 0;
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001178 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
Harvey Harrison441b62c2008-03-03 16:08:34 -08001180 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001182 switch (status) {
Mike Isely78aef512006-08-29 22:07:11 -05001183 case 0: /* success */
1184 break;
1185 case -ECONNRESET:
1186 case -ENOENT:
1187 case -ESHUTDOWN:
1188 /* precursor to disconnect so just go away */
1189 return;
1190 case -EPIPE:
Alan Stern4d2fae82009-07-09 12:59:57 -04001191 /* Can't call usb_clear_halt while in_interrupt */
1192 /* FALLS THROUGH */
Mike Isely78aef512006-08-29 22:07:11 -05001193 default:
1194 /* something ugly is going on... */
Alan Cox813a2242008-07-22 11:10:36 +01001195 dev_err(&urb->dev->dev,
1196 "%s - unexpected nonzero read status received: %d\n",
1197 __func__, status);
Mike Isely78aef512006-08-29 22:07:11 -05001198 cypress_set_dead(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 return;
1200 }
1201
1202 spin_lock_irqsave(&priv->lock, flags);
1203 if (priv->rx_flags & THROTTLED) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001204 dbg("%s - now throttling", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 priv->rx_flags |= ACTUALLY_THROTTLED;
1206 spin_unlock_irqrestore(&priv->lock, flags);
1207 return;
1208 }
1209 spin_unlock_irqrestore(&priv->lock, flags);
1210
Alan Cox4a90f092008-10-13 10:39:46 +01001211 tty = tty_port_tty_get(&port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 if (!tty) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001213 dbg("%s - bad tty pointer - exiting", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 return;
1215 }
1216
1217 spin_lock_irqsave(&priv->lock, flags);
Mike Isely3416eaa2008-02-10 20:23:19 -06001218 result = urb->actual_length;
1219 switch (priv->pkt_fmt) {
1220 default:
1221 case packet_format_1:
1222 /* This is for the CY7C64013... */
1223 priv->current_status = data[0] & 0xF8;
1224 bytes = data[1] + 2;
1225 i = 2;
1226 if (bytes > 2)
1227 havedata = 1;
1228 break;
1229 case packet_format_2:
1230 /* This is for the CY7C63743... */
1231 priv->current_status = data[0] & 0xF8;
1232 bytes = (data[0] & 0x07) + 1;
1233 i = 1;
1234 if (bytes > 1)
1235 havedata = 1;
1236 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 }
1238 spin_unlock_irqrestore(&priv->lock, flags);
Mike Isely3416eaa2008-02-10 20:23:19 -06001239 if (result < bytes) {
1240 dbg("%s - wrong packet size - received %d bytes but packet "
1241 "said %d bytes", __func__, result, bytes);
1242 goto continue_read;
1243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
Alan Cox813a2242008-07-22 11:10:36 +01001245 usb_serial_debug_data(debug, &port->dev, __func__,
1246 urb->actual_length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
1248 spin_lock_irqsave(&priv->lock, flags);
1249 /* check to see if status has changed */
Mike Isely67683062008-02-10 20:23:28 -06001250 if (priv->current_status != priv->prev_status) {
1251 priv->diff_status |= priv->current_status ^
1252 priv->prev_status;
1253 wake_up_interruptible(&priv->delta_msr_wait);
1254 priv->prev_status = priv->current_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 }
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001256 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001258 /* hangup, as defined in acm.c... this might be a bad place for it
1259 * though */
1260 if (tty && !(tty->termios->c_cflag & CLOCAL) &&
1261 !(priv->current_status & UART_CD)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001262 dbg("%s - calling hangup", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 tty_hangup(tty);
1264 goto continue_read;
1265 }
1266
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001267 /* There is one error bit... I'm assuming it is a parity error
1268 * indicator as the generic firmware will set this bit to 1 if a
1269 * parity error occurs.
1270 * I can not find reference to any other error events. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 spin_lock_irqsave(&priv->lock, flags);
1272 if (priv->current_status & CYP_ERROR) {
1273 spin_unlock_irqrestore(&priv->lock, flags);
1274 tty_flag = TTY_PARITY;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001275 dbg("%s - Parity Error detected", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 } else
1277 spin_unlock_irqrestore(&priv->lock, flags);
1278
1279 /* process read if there is data other than line status */
Alan Coxa108bfc2010-02-18 16:44:01 +00001280 if (tty && bytes > i) {
1281 tty_insert_flip_string_fixed_flag(tty, data + i,
Johan Hovold70ced222010-05-07 19:46:56 +02001282 tty_flag, bytes - i);
Alan Cox4a90f092008-10-13 10:39:46 +01001283 tty_flip_buffer_push(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 }
1285
1286 spin_lock_irqsave(&priv->lock, flags);
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001287 /* control and status byte(s) are also counted */
1288 priv->bytes_in += bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 spin_unlock_irqrestore(&priv->lock, flags);
1290
1291continue_read:
Alan Cox4a90f092008-10-13 10:39:46 +01001292 tty_kref_put(tty);
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001293
Alan Stern1f871582010-02-17 10:05:47 -05001294 /* Continue trying to always read */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
Alan Stern1f871582010-02-17 10:05:47 -05001296 if (priv->comm_is_ok) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001297 usb_fill_int_urb(port->interrupt_in_urb, port->serial->dev,
1298 usb_rcvintpipe(port->serial->dev,
1299 port->interrupt_in_endpointAddress),
1300 port->interrupt_in_urb->transfer_buffer,
1301 port->interrupt_in_urb->transfer_buffer_length,
Alan Cox813a2242008-07-22 11:10:36 +01001302 cypress_read_int_callback, port,
1303 priv->read_urb_interval);
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001304 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
Alan Stern1f871582010-02-17 10:05:47 -05001305 if (result && result != -EPERM) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001306 dev_err(&urb->dev->dev, "%s - failed resubmitting "
Harvey Harrison441b62c2008-03-03 16:08:34 -08001307 "read urb, error %d\n", __func__,
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001308 result);
Mike Isely78aef512006-08-29 22:07:11 -05001309 cypress_set_dead(port);
1310 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312} /* cypress_read_int_callback */
1313
1314
David Howells7d12e782006-10-05 14:55:46 +01001315static void cypress_write_int_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316{
Ming Leicdc97792008-02-24 18:41:47 +08001317 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 struct cypress_private *priv = usb_get_serial_port_data(port);
1319 int result;
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001320 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
Harvey Harrison441b62c2008-03-03 16:08:34 -08001322 dbg("%s - port %d", __func__, port->number);
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001323
1324 switch (status) {
Alan Cox813a2242008-07-22 11:10:36 +01001325 case 0:
1326 /* success */
1327 break;
1328 case -ECONNRESET:
1329 case -ENOENT:
1330 case -ESHUTDOWN:
1331 /* this urb is terminated, clean up */
1332 dbg("%s - urb shutting down with status: %d",
1333 __func__, status);
1334 priv->write_urb_in_use = 0;
1335 return;
1336 case -EPIPE: /* no break needed; clear halt and resubmit */
1337 if (!priv->comm_is_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 break;
Alan Cox813a2242008-07-22 11:10:36 +01001339 usb_clear_halt(port->serial->dev, 0x02);
1340 /* error in the urb, so we have to resubmit it */
1341 dbg("%s - nonzero write bulk status received: %d",
1342 __func__, status);
1343 port->interrupt_out_urb->transfer_buffer_length = 1;
Alan Cox813a2242008-07-22 11:10:36 +01001344 result = usb_submit_urb(port->interrupt_out_urb, GFP_ATOMIC);
1345 if (!result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 return;
Alan Cox813a2242008-07-22 11:10:36 +01001347 dev_err(&urb->dev->dev,
1348 "%s - failed resubmitting write urb, error %d\n",
1349 __func__, result);
1350 cypress_set_dead(port);
1351 break;
1352 default:
1353 dev_err(&urb->dev->dev,
1354 "%s - unexpected nonzero write status received: %d\n",
1355 __func__, status);
1356 cypress_set_dead(port);
1357 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 priv->write_urb_in_use = 0;
Alan Cox813a2242008-07-22 11:10:36 +01001360
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 /* send any buffered data */
1362 cypress_send(port);
1363}
1364
Greg Kroah-Hartman7e3131f2012-02-28 13:11:41 -08001365module_usb_serial_driver(cypress_driver, serial_drivers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
Alan Cox813a2242008-07-22 11:10:36 +01001367MODULE_AUTHOR(DRIVER_AUTHOR);
1368MODULE_DESCRIPTION(DRIVER_DESC);
1369MODULE_VERSION(DRIVER_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370MODULE_LICENSE("GPL");
1371
1372module_param(debug, bool, S_IRUGO | S_IWUSR);
1373MODULE_PARM_DESC(debug, "Debug enabled or not");
1374module_param(stats, bool, S_IRUGO | S_IWUSR);
1375MODULE_PARM_DESC(stats, "Enable statistics or not");
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001376module_param(interval, int, S_IRUGO | S_IWUSR);
1377MODULE_PARM_DESC(interval, "Overrides interrupt interval");
Mike Frysingerc3126592009-12-18 16:33:03 -05001378module_param(unstable_bauds, bool, S_IRUGO | S_IWUSR);
1379MODULE_PARM_DESC(unstable_bauds, "Allow unstable baud rates");