blob: b1b846752c4bcb301ea972642e5c2b66a619feac [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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 char prev_status, diff_status; /* used for TIOCMIWAIT */
Daniel Mack3ad2f3f2010-02-03 08:01:28 +0800129 /* we pass a pointer to this as the argument sent to
Alan Cox813a2242008-07-22 11:10:36 +0100130 cypress_set_termios old_termios */
Alan Cox606d0992006-12-08 02:38:45 -0800131 struct ktermios tmp_termios; /* stores the old termios settings */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132};
133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134/* function prototypes for the Cypress USB to serial device */
Alan Cox813a2242008-07-22 11:10:36 +0100135static int cypress_earthmate_startup(struct usb_serial *serial);
136static int cypress_hidcom_startup(struct usb_serial *serial);
137static int cypress_ca42v2_startup(struct usb_serial *serial);
Alan Sternf9c99bb2009-06-02 11:53:55 -0400138static void cypress_release(struct usb_serial *serial);
Alan Coxa509a7e2009-09-19 13:13:26 -0700139static int cypress_open(struct tty_struct *tty, struct usb_serial_port *port);
Alan Cox335f8512009-06-11 12:26:29 +0100140static void cypress_close(struct usb_serial_port *port);
141static void cypress_dtr_rts(struct usb_serial_port *port, int on);
Alan Cox813a2242008-07-22 11:10:36 +0100142static int cypress_write(struct tty_struct *tty, struct usb_serial_port *port,
143 const unsigned char *buf, int count);
144static void cypress_send(struct usb_serial_port *port);
145static int cypress_write_room(struct tty_struct *tty);
Alan Cox00a0d0d2011-02-14 16:27:06 +0000146static int cypress_ioctl(struct tty_struct *tty,
Alan Cox813a2242008-07-22 11:10:36 +0100147 unsigned int cmd, unsigned long arg);
148static void cypress_set_termios(struct tty_struct *tty,
149 struct usb_serial_port *port, struct ktermios *old);
Alan Cox60b33c12011-02-14 16:26:14 +0000150static int cypress_tiocmget(struct tty_struct *tty);
Alan Cox20b9d172011-02-14 16:26:50 +0000151static int cypress_tiocmset(struct tty_struct *tty,
Alan Cox813a2242008-07-22 11:10:36 +0100152 unsigned int set, unsigned int clear);
153static int cypress_chars_in_buffer(struct tty_struct *tty);
154static void cypress_throttle(struct tty_struct *tty);
155static void cypress_unthrottle(struct tty_struct *tty);
156static void cypress_set_dead(struct usb_serial_port *port);
157static void cypress_read_int_callback(struct urb *urb);
158static void cypress_write_int_callback(struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700160static struct usb_serial_driver cypress_earthmate_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700161 .driver = {
162 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700163 .name = "earthmate",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700164 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700165 .description = "DeLorme Earthmate USB",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 .id_table = id_table_earthmate,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 .num_ports = 1,
168 .attach = cypress_earthmate_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -0400169 .release = cypress_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 .open = cypress_open,
171 .close = cypress_close,
Alan Cox335f8512009-06-11 12:26:29 +0100172 .dtr_rts = cypress_dtr_rts,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 .write = cypress_write,
174 .write_room = cypress_write_room,
175 .ioctl = cypress_ioctl,
176 .set_termios = cypress_set_termios,
177 .tiocmget = cypress_tiocmget,
178 .tiocmset = cypress_tiocmset,
179 .chars_in_buffer = cypress_chars_in_buffer,
180 .throttle = cypress_throttle,
181 .unthrottle = cypress_unthrottle,
182 .read_int_callback = cypress_read_int_callback,
183 .write_int_callback = cypress_write_int_callback,
184};
185
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700186static struct usb_serial_driver cypress_hidcom_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700187 .driver = {
188 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700189 .name = "cyphidcom",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700190 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700191 .description = "HID->COM RS232 Adapter",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 .id_table = id_table_cyphidcomrs232,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 .num_ports = 1,
194 .attach = cypress_hidcom_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -0400195 .release = cypress_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 .open = cypress_open,
197 .close = cypress_close,
Alan Cox335f8512009-06-11 12:26:29 +0100198 .dtr_rts = cypress_dtr_rts,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 .write = cypress_write,
200 .write_room = cypress_write_room,
201 .ioctl = cypress_ioctl,
202 .set_termios = cypress_set_termios,
203 .tiocmget = cypress_tiocmget,
204 .tiocmset = cypress_tiocmset,
205 .chars_in_buffer = cypress_chars_in_buffer,
206 .throttle = cypress_throttle,
207 .unthrottle = cypress_unthrottle,
208 .read_int_callback = cypress_read_int_callback,
209 .write_int_callback = cypress_write_int_callback,
210};
211
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600212static struct usb_serial_driver cypress_ca42v2_device = {
213 .driver = {
214 .owner = THIS_MODULE,
Alan Cox813a2242008-07-22 11:10:36 +0100215 .name = "nokiaca42v2",
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600216 },
217 .description = "Nokia CA-42 V2 Adapter",
218 .id_table = id_table_nokiaca42v2,
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600219 .num_ports = 1,
220 .attach = cypress_ca42v2_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -0400221 .release = cypress_release,
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600222 .open = cypress_open,
223 .close = cypress_close,
Alan Cox335f8512009-06-11 12:26:29 +0100224 .dtr_rts = cypress_dtr_rts,
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600225 .write = cypress_write,
226 .write_room = cypress_write_room,
227 .ioctl = cypress_ioctl,
228 .set_termios = cypress_set_termios,
229 .tiocmget = cypress_tiocmget,
230 .tiocmset = cypress_tiocmset,
231 .chars_in_buffer = cypress_chars_in_buffer,
232 .throttle = cypress_throttle,
233 .unthrottle = cypress_unthrottle,
234 .read_int_callback = cypress_read_int_callback,
235 .write_int_callback = cypress_write_int_callback,
236};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Alan Stern08a4f6b2012-02-23 14:56:17 -0500238static struct usb_serial_driver * const serial_drivers[] = {
239 &cypress_earthmate_device, &cypress_hidcom_device,
240 &cypress_ca42v2_device, NULL
241};
242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243/*****************************************************************************
244 * Cypress serial helper functions
245 *****************************************************************************/
246
Robert Butora617d12d2013-05-31 18:09:51 +0300247/* FRWD Dongle hidcom needs to skip reset and speed checks */
248static inline bool is_frwd(struct usb_device *dev)
249{
250 return ((le16_to_cpu(dev->descriptor.idVendor) == VENDOR_ID_FRWD) &&
251 (le16_to_cpu(dev->descriptor.idProduct) == PRODUCT_ID_CYPHIDCOM_FRWD));
252}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Alan Cox8873aaa2008-03-10 21:59:28 +0000254static int analyze_baud_rate(struct usb_serial_port *port, speed_t new_rate)
Mike Isely92983c22008-02-10 20:23:32 -0600255{
Mike Isely92983c22008-02-10 20:23:32 -0600256 struct cypress_private *priv;
257 priv = usb_get_serial_port_data(port);
258
Mike Frysingerc3126592009-12-18 16:33:03 -0500259 if (unstable_bauds)
260 return new_rate;
261
Robert Butora617d12d2013-05-31 18:09:51 +0300262 /* FRWD Dongle uses 115200 bps */
263 if (is_frwd(port->serial->dev))
264 return new_rate;
265
Mike Isely92983c22008-02-10 20:23:32 -0600266 /*
267 * The general purpose firmware for the Cypress M8 allows for
268 * a maximum speed of 57600bps (I have no idea whether DeLorme
269 * chose to use the general purpose firmware or not), if you
270 * need to modify this speed setting for your own project
271 * please add your own chiptype and modify the code likewise.
272 * The Cypress HID->COM device will work successfully up to
273 * 115200bps (but the actual throughput is around 3kBps).
274 */
Mike Isely92983c22008-02-10 20:23:32 -0600275 if (port->serial->dev->speed == USB_SPEED_LOW) {
276 /*
277 * Mike Isely <isely@pobox.com> 2-Feb-2008: The
278 * Cypress app note that describes this mechanism
279 * states the the low-speed part can't handle more
280 * than 800 bytes/sec, in which case 4800 baud is the
281 * safest speed for a part like that.
282 */
283 if (new_rate > 4800) {
284 dbg("%s - failed setting baud rate, device incapable "
285 "speed %d", __func__, new_rate);
286 return -1;
287 }
288 }
289 switch (priv->chiptype) {
290 case CT_EARTHMATE:
291 if (new_rate <= 600) {
292 /* 300 and 600 baud rates are supported under
293 * the generic firmware, but are not used with
294 * NMEA and SiRF protocols */
295 dbg("%s - failed setting baud rate, unsupported speed "
296 "of %d on Earthmate GPS", __func__, new_rate);
297 return -1;
298 }
299 break;
300 default:
301 break;
302 }
303 return new_rate;
304}
305
306
Steven Cole093cf722005-05-03 19:07:24 -0600307/* This function can either set or retrieve the current serial line settings */
Alan Cox813a2242008-07-22 11:10:36 +0100308static int cypress_serial_control(struct tty_struct *tty,
Alan Cox95da3102008-07-22 11:09:07 +0100309 struct usb_serial_port *port, speed_t baud_rate, int data_bits,
310 int stop_bits, int parity_enable, int parity_type, int reset,
311 int cypress_request_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500313 int new_baudrate = 0, retval = 0, tries = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 struct cypress_private *priv;
Johan Hovold09546442009-12-28 23:01:48 +0100315 u8 *feature_buffer;
316 const unsigned int feature_len = 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 unsigned long flags;
318
Harvey Harrison441b62c2008-03-03 16:08:34 -0800319 dbg("%s", __func__);
Alan Cox813a2242008-07-22 11:10:36 +0100320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 priv = usb_get_serial_port_data(port);
322
Mike Isely78aef512006-08-29 22:07:11 -0500323 if (!priv->comm_is_ok)
324 return -ENODEV;
325
Johan Hovold09546442009-12-28 23:01:48 +0100326 feature_buffer = kcalloc(feature_len, sizeof(u8), GFP_KERNEL);
327 if (!feature_buffer)
328 return -ENOMEM;
329
Alan Cox813a2242008-07-22 11:10:36 +0100330 switch (cypress_request_type) {
331 case CYPRESS_SET_CONFIG:
Alan Cox813a2242008-07-22 11:10:36 +0100332 /* 0 means 'Hang up' so doesn't change the true bit rate */
Mike Frysinger2805eb12009-12-18 16:33:02 -0500333 new_baudrate = priv->baud_rate;
334 if (baud_rate && baud_rate != priv->baud_rate) {
Alan Cox813a2242008-07-22 11:10:36 +0100335 dbg("%s - baud rate is changing", __func__);
336 retval = analyze_baud_rate(port, baud_rate);
Mike Frysinger2805eb12009-12-18 16:33:02 -0500337 if (retval >= 0) {
Alan Cox813a2242008-07-22 11:10:36 +0100338 new_baudrate = retval;
339 dbg("%s - New baud rate set to %d",
340 __func__, new_baudrate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 }
Alan Cox813a2242008-07-22 11:10:36 +0100342 }
343 dbg("%s - baud rate is being sent as %d",
344 __func__, new_baudrate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Alan Cox813a2242008-07-22 11:10:36 +0100346 /* fill the feature_buffer with new configuration */
Johan Hovold0f2c2d72009-12-31 16:48:01 +0100347 put_unaligned_le32(new_baudrate, feature_buffer);
Alan Cox813a2242008-07-22 11:10:36 +0100348 feature_buffer[4] |= data_bits; /* assign data bits in 2 bit space ( max 3 ) */
349 /* 1 bit gap */
350 feature_buffer[4] |= (stop_bits << 3); /* assign stop bits in 1 bit space */
351 feature_buffer[4] |= (parity_enable << 4); /* assign parity flag in 1 bit space */
352 feature_buffer[4] |= (parity_type << 5); /* assign parity type in 1 bit space */
353 /* 1 bit gap */
354 feature_buffer[4] |= (reset << 7); /* assign reset at end of byte, 1 bit space */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Alan Cox813a2242008-07-22 11:10:36 +0100356 dbg("%s - device is being sent this feature report:",
357 __func__);
358 dbg("%s - %02X - %02X - %02X - %02X - %02X", __func__,
359 feature_buffer[0], feature_buffer[1],
360 feature_buffer[2], feature_buffer[3],
361 feature_buffer[4]);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500362
Alan Cox813a2242008-07-22 11:10:36 +0100363 do {
364 retval = usb_control_msg(port->serial->dev,
365 usb_sndctrlpipe(port->serial->dev, 0),
366 HID_REQ_SET_REPORT,
367 USB_DIR_OUT | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
368 0x0300, 0, feature_buffer,
Johan Hovold09546442009-12-28 23:01:48 +0100369 feature_len, 500);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500370
Alan Cox813a2242008-07-22 11:10:36 +0100371 if (tries++ >= 3)
372 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Johan Hovold09546442009-12-28 23:01:48 +0100374 } while (retval != feature_len &&
Alan Cox813a2242008-07-22 11:10:36 +0100375 retval != -ENODEV);
Mike Isely93075542008-02-10 20:23:14 -0600376
Johan Hovold09546442009-12-28 23:01:48 +0100377 if (retval != feature_len) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700378 dev_err(&port->dev, "%s - failed sending serial "
379 "line settings - %d\n", __func__, retval);
Alan Cox813a2242008-07-22 11:10:36 +0100380 cypress_set_dead(port);
381 } else {
382 spin_lock_irqsave(&priv->lock, flags);
383 priv->baud_rate = new_baudrate;
384 priv->current_config = feature_buffer[4];
385 spin_unlock_irqrestore(&priv->lock, flags);
386 /* If we asked for a speed change encode it */
387 if (baud_rate)
388 tty_encode_baud_rate(tty,
389 new_baudrate, new_baudrate);
390 }
391 break;
392 case CYPRESS_GET_CONFIG:
393 if (priv->get_cfg_unsafe) {
394 /* Not implemented for this device,
395 and if we try to do it we're likely
396 to crash the hardware. */
Johan Hovold09546442009-12-28 23:01:48 +0100397 retval = -ENOTTY;
398 goto out;
Alan Cox813a2242008-07-22 11:10:36 +0100399 }
400 dbg("%s - retreiving serial line settings", __func__);
Alan Cox813a2242008-07-22 11:10:36 +0100401 do {
402 retval = usb_control_msg(port->serial->dev,
403 usb_rcvctrlpipe(port->serial->dev, 0),
404 HID_REQ_GET_REPORT,
405 USB_DIR_IN | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
406 0x0300, 0, feature_buffer,
Johan Hovold09546442009-12-28 23:01:48 +0100407 feature_len, 500);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500408
Alan Cox813a2242008-07-22 11:10:36 +0100409 if (tries++ >= 3)
410 break;
Johan Hovold09546442009-12-28 23:01:48 +0100411 } while (retval != feature_len
Alan Cox813a2242008-07-22 11:10:36 +0100412 && retval != -ENODEV);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500413
Johan Hovold09546442009-12-28 23:01:48 +0100414 if (retval != feature_len) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700415 dev_err(&port->dev, "%s - failed to retrieve serial "
416 "line settings - %d\n", __func__, retval);
Alan Cox813a2242008-07-22 11:10:36 +0100417 cypress_set_dead(port);
Johan Hovold09546442009-12-28 23:01:48 +0100418 goto out;
Alan Cox813a2242008-07-22 11:10:36 +0100419 } else {
420 spin_lock_irqsave(&priv->lock, flags);
421 /* store the config in one byte, and later
422 use bit masks to check values */
423 priv->current_config = feature_buffer[4];
Johan Hovold0f2c2d72009-12-31 16:48:01 +0100424 priv->baud_rate = get_unaligned_le32(feature_buffer);
Alan Cox813a2242008-07-22 11:10:36 +0100425 spin_unlock_irqrestore(&priv->lock, flags);
426 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 }
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500428 spin_lock_irqsave(&priv->lock, flags);
429 ++priv->cmd_count;
430 spin_unlock_irqrestore(&priv->lock, flags);
Johan Hovold09546442009-12-28 23:01:48 +0100431out:
432 kfree(feature_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 return retval;
434} /* cypress_serial_control */
435
436
Mike Isely78aef512006-08-29 22:07:11 -0500437static void cypress_set_dead(struct usb_serial_port *port)
438{
439 struct cypress_private *priv = usb_get_serial_port_data(port);
440 unsigned long flags;
441
442 spin_lock_irqsave(&priv->lock, flags);
443 if (!priv->comm_is_ok) {
444 spin_unlock_irqrestore(&priv->lock, flags);
445 return;
446 }
447 priv->comm_is_ok = 0;
448 spin_unlock_irqrestore(&priv->lock, flags);
449
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700450 dev_err(&port->dev, "cypress_m8 suspending failing port %d - "
451 "interval might be too short\n", port->number);
Mike Isely78aef512006-08-29 22:07:11 -0500452}
453
454
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455/*****************************************************************************
456 * Cypress serial driver functions
457 *****************************************************************************/
458
459
Alan Cox813a2242008-07-22 11:10:36 +0100460static int generic_startup(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
462 struct cypress_private *priv;
Mike Isely0257fa92006-08-29 22:06:59 -0500463 struct usb_serial_port *port = serial->port[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Harvey Harrison441b62c2008-03-03 16:08:34 -0800465 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Alan Cox813a2242008-07-22 11:10:36 +0100467 priv = kzalloc(sizeof(struct cypress_private), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 if (!priv)
469 return -ENOMEM;
470
Mike Isely78aef512006-08-29 22:07:11 -0500471 priv->comm_is_ok = !0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 spin_lock_init(&priv->lock);
Johan Hovold117fb8d2010-05-16 20:33:50 +0200473 if (kfifo_alloc(&priv->write_fifo, CYPRESS_BUF_SIZE, GFP_KERNEL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 kfree(priv);
475 return -ENOMEM;
476 }
Alan Cox813a2242008-07-22 11:10:36 +0100477
Robert Butora617d12d2013-05-31 18:09:51 +0300478 /* Skip reset for FRWD device. It is a workaound:
479 device hangs if it receives SET_CONFIGURE in Configured
480 state. */
481 if (!is_frwd(serial->dev))
482 usb_reset_configuration(serial->dev);
Alan Cox813a2242008-07-22 11:10:36 +0100483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 priv->cmd_ctrl = 0;
485 priv->line_control = 0;
486 priv->termios_initialized = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 priv->rx_flags = 0;
Mike Isely3416eaa2008-02-10 20:23:19 -0600488 /* Default packet format setting is determined by packet size.
489 Anything with a size larger then 9 must have a separate
490 count field since the 3 bit count field is otherwise too
491 small. Otherwise we can use the slightly more compact
492 format. This is in accordance with the cypress_m8 serial
493 converter app note. */
Alan Cox813a2242008-07-22 11:10:36 +0100494 if (port->interrupt_out_size > 9)
Mike Isely3416eaa2008-02-10 20:23:19 -0600495 priv->pkt_fmt = packet_format_1;
Alan Cox813a2242008-07-22 11:10:36 +0100496 else
Mike Isely3416eaa2008-02-10 20:23:19 -0600497 priv->pkt_fmt = packet_format_2;
Alan Cox813a2242008-07-22 11:10:36 +0100498
Mike Isely0257fa92006-08-29 22:06:59 -0500499 if (interval > 0) {
500 priv->write_urb_interval = interval;
501 priv->read_urb_interval = interval;
502 dbg("%s - port %d read & write intervals forced to %d",
Alan Cox813a2242008-07-22 11:10:36 +0100503 __func__, port->number, interval);
Mike Isely0257fa92006-08-29 22:06:59 -0500504 } else {
505 priv->write_urb_interval = port->interrupt_out_urb->interval;
506 priv->read_urb_interval = port->interrupt_in_urb->interval;
507 dbg("%s - port %d intervals: read=%d write=%d",
Alan Cox813a2242008-07-22 11:10:36 +0100508 __func__, port->number,
509 priv->read_urb_interval, priv->write_urb_interval);
Mike Isely0257fa92006-08-29 22:06:59 -0500510 }
511 usb_set_serial_port_data(port, priv);
Alan Cox813a2242008-07-22 11:10:36 +0100512
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500513 return 0;
514}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
516
Alan Cox813a2242008-07-22 11:10:36 +0100517static int cypress_earthmate_startup(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
519 struct cypress_private *priv;
Mike Isely3d6aa322008-02-10 20:23:24 -0600520 struct usb_serial_port *port = serial->port[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Harvey Harrison441b62c2008-03-03 16:08:34 -0800522 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
524 if (generic_startup(serial)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800525 dbg("%s - Failed setting up port %d", __func__,
Mike Isely3d6aa322008-02-10 20:23:24 -0600526 port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 return 1;
528 }
529
Mike Isely3d6aa322008-02-10 20:23:24 -0600530 priv = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 priv->chiptype = CT_EARTHMATE;
Mike Isely3416eaa2008-02-10 20:23:19 -0600532 /* All Earthmate devices use the separated-count packet
533 format! Idiotic. */
534 priv->pkt_fmt = packet_format_1;
Alan Cox813a2242008-07-22 11:10:36 +0100535 if (serial->dev->descriptor.idProduct !=
536 cpu_to_le16(PRODUCT_ID_EARTHMATEUSB)) {
Mike Isely3d6aa322008-02-10 20:23:24 -0600537 /* The old original USB Earthmate seemed able to
538 handle GET_CONFIG requests; everything they've
539 produced since that time crashes if this command is
540 attempted :-( */
541 dbg("%s - Marking this device as unsafe for GET_CONFIG "
542 "commands", __func__);
543 priv->get_cfg_unsafe = !0;
544 }
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500545
546 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547} /* cypress_earthmate_startup */
548
549
Alan Cox813a2242008-07-22 11:10:36 +0100550static int cypress_hidcom_startup(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551{
552 struct cypress_private *priv;
553
Harvey Harrison441b62c2008-03-03 16:08:34 -0800554 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
556 if (generic_startup(serial)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800557 dbg("%s - Failed setting up port %d", __func__,
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500558 serial->port[0]->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 return 1;
560 }
561
562 priv = usb_get_serial_port_data(serial->port[0]);
563 priv->chiptype = CT_CYPHIDCOM;
Alan Cox813a2242008-07-22 11:10:36 +0100564
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500565 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566} /* cypress_hidcom_startup */
567
568
Alan Cox813a2242008-07-22 11:10:36 +0100569static int cypress_ca42v2_startup(struct usb_serial *serial)
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600570{
571 struct cypress_private *priv;
572
Harvey Harrison441b62c2008-03-03 16:08:34 -0800573 dbg("%s", __func__);
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600574
575 if (generic_startup(serial)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800576 dbg("%s - Failed setting up port %d", __func__,
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600577 serial->port[0]->number);
578 return 1;
579 }
580
581 priv = usb_get_serial_port_data(serial->port[0]);
582 priv->chiptype = CT_CA42V2;
583
584 return 0;
585} /* cypress_ca42v2_startup */
586
587
Alan Sternf9c99bb2009-06-02 11:53:55 -0400588static void cypress_release(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
590 struct cypress_private *priv;
591
Alan Cox813a2242008-07-22 11:10:36 +0100592 dbg("%s - port %d", __func__, serial->port[0]->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 /* all open ports are closed at this point */
595
596 priv = usb_get_serial_port_data(serial->port[0]);
597
598 if (priv) {
Johan Hovold117fb8d2010-05-16 20:33:50 +0200599 kfifo_free(&priv->write_fifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 kfree(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 }
602}
603
604
Alan Coxa509a7e2009-09-19 13:13:26 -0700605static int cypress_open(struct tty_struct *tty, struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
607 struct cypress_private *priv = usb_get_serial_port_data(port);
608 struct usb_serial *serial = port->serial;
609 unsigned long flags;
610 int result = 0;
611
Harvey Harrison441b62c2008-03-03 16:08:34 -0800612 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Mike Isely78aef512006-08-29 22:07:11 -0500614 if (!priv->comm_is_ok)
615 return -EIO;
616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 /* clear halts before open */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 usb_clear_halt(serial->dev, 0x81);
619 usb_clear_halt(serial->dev, 0x02);
620
621 spin_lock_irqsave(&priv->lock, flags);
622 /* reset read/write statistics */
623 priv->bytes_in = 0;
624 priv->bytes_out = 0;
625 priv->cmd_count = 0;
626 priv->rx_flags = 0;
627 spin_unlock_irqrestore(&priv->lock, flags);
628
Alan Cox335f8512009-06-11 12:26:29 +0100629 /* Set termios */
Alan Coxfe1ae7f2009-09-19 13:13:33 -0700630 cypress_send(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Alan Cox95da3102008-07-22 11:09:07 +0100632 if (tty)
633 cypress_set_termios(tty, port, &priv->tmp_termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
635 /* setup the port and start reading from the device */
Alan Cox813a2242008-07-22 11:10:36 +0100636 if (!port->interrupt_in_urb) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700637 dev_err(&port->dev, "%s - interrupt_in_urb is empty!\n",
638 __func__);
Alan Cox813a2242008-07-22 11:10:36 +0100639 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 }
641
642 usb_fill_int_urb(port->interrupt_in_urb, serial->dev,
643 usb_rcvintpipe(serial->dev, port->interrupt_in_endpointAddress),
Alan Cox813a2242008-07-22 11:10:36 +0100644 port->interrupt_in_urb->transfer_buffer,
645 port->interrupt_in_urb->transfer_buffer_length,
Mike Isely0257fa92006-08-29 22:06:59 -0500646 cypress_read_int_callback, port, priv->read_urb_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
648
Alan Cox813a2242008-07-22 11:10:36 +0100649 if (result) {
650 dev_err(&port->dev,
651 "%s - failed submitting read urb, error %d\n",
652 __func__, result);
Mike Isely78aef512006-08-29 22:07:11 -0500653 cypress_set_dead(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 }
Alan Cox335f8512009-06-11 12:26:29 +0100655 port->port.drain_delay = 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 return result;
657} /* cypress_open */
658
Alan Cox335f8512009-06-11 12:26:29 +0100659static void cypress_dtr_rts(struct usb_serial_port *port, int on)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
661 struct cypress_private *priv = usb_get_serial_port_data(port);
Alan Cox335f8512009-06-11 12:26:29 +0100662 /* drop dtr and rts */
Alan Cox335f8512009-06-11 12:26:29 +0100663 spin_lock_irq(&priv->lock);
664 if (on == 0)
665 priv->line_control = 0;
666 else
667 priv->line_control = CONTROL_DTR | CONTROL_RTS;
668 priv->cmd_ctrl = 1;
669 spin_unlock_irq(&priv->lock);
670 cypress_write(NULL, port, NULL, 0);
671}
672
673static void cypress_close(struct usb_serial_port *port)
674{
675 struct cypress_private *priv = usb_get_serial_port_data(port);
Johan Hovold117fb8d2010-05-16 20:33:50 +0200676 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Harvey Harrison441b62c2008-03-03 16:08:34 -0800678 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100680 /* writing is potentially harmful, lock must be taken */
681 mutex_lock(&port->serial->disc_mutex);
682 if (port->serial->disconnected) {
683 mutex_unlock(&port->serial->disc_mutex);
684 return;
685 }
Johan Hovold117fb8d2010-05-16 20:33:50 +0200686 spin_lock_irqsave(&priv->lock, flags);
687 kfifo_reset_out(&priv->write_fifo);
688 spin_unlock_irqrestore(&priv->lock, flags);
689
Harvey Harrison441b62c2008-03-03 16:08:34 -0800690 dbg("%s - stopping urbs", __func__);
Alan Cox813a2242008-07-22 11:10:36 +0100691 usb_kill_urb(port->interrupt_in_urb);
692 usb_kill_urb(port->interrupt_out_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 if (stats)
Alan Cox813a2242008-07-22 11:10:36 +0100695 dev_info(&port->dev, "Statistics: %d Bytes In | %d Bytes Out | %d Commands Issued\n",
696 priv->bytes_in, priv->bytes_out, priv->cmd_count);
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100697 mutex_unlock(&port->serial->disc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698} /* cypress_close */
699
700
Alan Cox95da3102008-07-22 11:09:07 +0100701static int cypress_write(struct tty_struct *tty, struct usb_serial_port *port,
702 const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
704 struct cypress_private *priv = usb_get_serial_port_data(port);
Alan Cox813a2242008-07-22 11:10:36 +0100705
Harvey Harrison441b62c2008-03-03 16:08:34 -0800706 dbg("%s - port %d, %d bytes", __func__, port->number, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
708 /* line control commands, which need to be executed immediately,
709 are not put into the buffer for obvious reasons.
710 */
711 if (priv->cmd_ctrl) {
712 count = 0;
713 goto finish;
714 }
Alan Cox813a2242008-07-22 11:10:36 +0100715
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 if (!count)
717 return count;
Alan Cox813a2242008-07-22 11:10:36 +0100718
Johan Hovold117fb8d2010-05-16 20:33:50 +0200719 count = kfifo_in_locked(&priv->write_fifo, buf, count, &priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
721finish:
722 cypress_send(port);
723
724 return count;
725} /* cypress_write */
726
727
728static void cypress_send(struct usb_serial_port *port)
729{
730 int count = 0, result, offset, actual_size;
731 struct cypress_private *priv = usb_get_serial_port_data(port);
732 unsigned long flags;
Alan Cox813a2242008-07-22 11:10:36 +0100733
Mike Isely78aef512006-08-29 22:07:11 -0500734 if (!priv->comm_is_ok)
735 return;
736
Harvey Harrison441b62c2008-03-03 16:08:34 -0800737 dbg("%s - port %d", __func__, port->number);
Alan Cox813a2242008-07-22 11:10:36 +0100738 dbg("%s - interrupt out size is %d", __func__,
739 port->interrupt_out_size);
740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 spin_lock_irqsave(&priv->lock, flags);
742 if (priv->write_urb_in_use) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800743 dbg("%s - can't write, urb in use", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 spin_unlock_irqrestore(&priv->lock, flags);
745 return;
746 }
747 spin_unlock_irqrestore(&priv->lock, flags);
748
749 /* clear buffer */
Alan Cox813a2242008-07-22 11:10:36 +0100750 memset(port->interrupt_out_urb->transfer_buffer, 0,
751 port->interrupt_out_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
753 spin_lock_irqsave(&priv->lock, flags);
Mike Isely3416eaa2008-02-10 20:23:19 -0600754 switch (priv->pkt_fmt) {
755 default:
756 case packet_format_1:
757 /* this is for the CY7C64013... */
758 offset = 2;
759 port->interrupt_out_buffer[0] = priv->line_control;
760 break;
761 case packet_format_2:
762 /* this is for the CY7C63743... */
763 offset = 1;
764 port->interrupt_out_buffer[0] = priv->line_control;
765 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 }
767
768 if (priv->line_control & CONTROL_RESET)
769 priv->line_control &= ~CONTROL_RESET;
770
771 if (priv->cmd_ctrl) {
772 priv->cmd_count++;
Harvey Harrison441b62c2008-03-03 16:08:34 -0800773 dbg("%s - line control command being issued", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 spin_unlock_irqrestore(&priv->lock, flags);
775 goto send;
776 } else
777 spin_unlock_irqrestore(&priv->lock, flags);
778
Johan Hovold117fb8d2010-05-16 20:33:50 +0200779 count = kfifo_out_locked(&priv->write_fifo,
780 &port->interrupt_out_buffer[offset],
781 port->interrupt_out_size - offset,
782 &priv->lock);
Alan Cox813a2242008-07-22 11:10:36 +0100783 if (count == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Mike Isely3416eaa2008-02-10 20:23:19 -0600786 switch (priv->pkt_fmt) {
787 default:
788 case packet_format_1:
789 port->interrupt_out_buffer[1] = count;
790 break;
791 case packet_format_2:
792 port->interrupt_out_buffer[0] |= count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 }
794
Harvey Harrison441b62c2008-03-03 16:08:34 -0800795 dbg("%s - count is %d", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
797send:
798 spin_lock_irqsave(&priv->lock, flags);
799 priv->write_urb_in_use = 1;
800 spin_unlock_irqrestore(&priv->lock, flags);
801
802 if (priv->cmd_ctrl)
803 actual_size = 1;
804 else
Mike Isely3416eaa2008-02-10 20:23:19 -0600805 actual_size = count +
806 (priv->pkt_fmt == packet_format_1 ? 2 : 1);
807
Alan Cox813a2242008-07-22 11:10:36 +0100808 usb_serial_debug_data(debug, &port->dev, __func__,
809 port->interrupt_out_size,
810 port->interrupt_out_urb->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Mike Isely9aa8dae2006-08-29 22:07:04 -0500812 usb_fill_int_urb(port->interrupt_out_urb, port->serial->dev,
813 usb_sndintpipe(port->serial->dev, port->interrupt_out_endpointAddress),
814 port->interrupt_out_buffer, port->interrupt_out_size,
815 cypress_write_int_callback, port, priv->write_urb_interval);
Alan Cox813a2242008-07-22 11:10:36 +0100816 result = usb_submit_urb(port->interrupt_out_urb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 if (result) {
Johan Hovold22a416c2012-02-10 13:20:51 +0100818 dev_err_console(port,
Alan Cox813a2242008-07-22 11:10:36 +0100819 "%s - failed submitting write urb, error %d\n",
820 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 priv->write_urb_in_use = 0;
Mike Isely78aef512006-08-29 22:07:11 -0500822 cypress_set_dead(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 }
824
825 spin_lock_irqsave(&priv->lock, flags);
Alan Cox813a2242008-07-22 11:10:36 +0100826 if (priv->cmd_ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 priv->cmd_ctrl = 0;
Alan Cox813a2242008-07-22 11:10:36 +0100828
829 /* do not count the line control and size bytes */
830 priv->bytes_out += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 spin_unlock_irqrestore(&priv->lock, flags);
832
Pete Zaitcevcf2c7482006-05-22 21:58:49 -0700833 usb_serial_port_softint(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834} /* cypress_send */
835
836
837/* returns how much space is available in the soft buffer */
Alan Cox95da3102008-07-22 11:09:07 +0100838static int cypress_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839{
Alan Cox95da3102008-07-22 11:09:07 +0100840 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 struct cypress_private *priv = usb_get_serial_port_data(port);
842 int room = 0;
843 unsigned long flags;
844
Harvey Harrison441b62c2008-03-03 16:08:34 -0800845 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
847 spin_lock_irqsave(&priv->lock, flags);
Johan Hovold117fb8d2010-05-16 20:33:50 +0200848 room = kfifo_avail(&priv->write_fifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 spin_unlock_irqrestore(&priv->lock, flags);
850
Harvey Harrison441b62c2008-03-03 16:08:34 -0800851 dbg("%s - returns %d", __func__, room);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 return room;
853}
854
855
Alan Cox60b33c12011-02-14 16:26:14 +0000856static int cypress_tiocmget(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857{
Alan Cox95da3102008-07-22 11:09:07 +0100858 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 struct cypress_private *priv = usb_get_serial_port_data(port);
860 __u8 status, control;
861 unsigned int result = 0;
862 unsigned long flags;
Alan Cox813a2242008-07-22 11:10:36 +0100863
Harvey Harrison441b62c2008-03-03 16:08:34 -0800864 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
866 spin_lock_irqsave(&priv->lock, flags);
867 control = priv->line_control;
868 status = priv->current_status;
869 spin_unlock_irqrestore(&priv->lock, flags);
870
871 result = ((control & CONTROL_DTR) ? TIOCM_DTR : 0)
872 | ((control & CONTROL_RTS) ? TIOCM_RTS : 0)
873 | ((status & UART_CTS) ? TIOCM_CTS : 0)
874 | ((status & UART_DSR) ? TIOCM_DSR : 0)
875 | ((status & UART_RI) ? TIOCM_RI : 0)
876 | ((status & UART_CD) ? TIOCM_CD : 0);
877
Harvey Harrison441b62c2008-03-03 16:08:34 -0800878 dbg("%s - result = %x", __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
880 return result;
881}
882
883
Alan Cox20b9d172011-02-14 16:26:50 +0000884static int cypress_tiocmset(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 unsigned int set, unsigned int clear)
886{
Alan Cox95da3102008-07-22 11:09:07 +0100887 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 struct cypress_private *priv = usb_get_serial_port_data(port);
889 unsigned long flags;
Alan Cox813a2242008-07-22 11:10:36 +0100890
Harvey Harrison441b62c2008-03-03 16:08:34 -0800891 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
893 spin_lock_irqsave(&priv->lock, flags);
894 if (set & TIOCM_RTS)
895 priv->line_control |= CONTROL_RTS;
896 if (set & TIOCM_DTR)
897 priv->line_control |= CONTROL_DTR;
898 if (clear & TIOCM_RTS)
899 priv->line_control &= ~CONTROL_RTS;
900 if (clear & TIOCM_DTR)
901 priv->line_control &= ~CONTROL_DTR;
Alan Cox8873aaa2008-03-10 21:59:28 +0000902 priv->cmd_ctrl = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 spin_unlock_irqrestore(&priv->lock, flags);
904
Alan Cox95da3102008-07-22 11:09:07 +0100905 return cypress_write(tty, port, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906}
907
908
Alan Cox00a0d0d2011-02-14 16:27:06 +0000909static int cypress_ioctl(struct tty_struct *tty,
Alan Cox95da3102008-07-22 11:09:07 +0100910 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911{
Alan Cox95da3102008-07-22 11:09:07 +0100912 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 struct cypress_private *priv = usb_get_serial_port_data(port);
914
Harvey Harrison441b62c2008-03-03 16:08:34 -0800915 dbg("%s - port %d, cmd 0x%.4x", __func__, port->number, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
917 switch (cmd) {
Alan Cox813a2242008-07-22 11:10:36 +0100918 /* This code comes from drivers/char/serial.c and ftdi_sio.c */
919 case TIOCMIWAIT:
Johan Hovoldee49ee82013-03-19 09:21:13 +0100920 for (;;) {
921 interruptible_sleep_on(&port->delta_msr_wait);
Alan Cox813a2242008-07-22 11:10:36 +0100922 /* see if a signal did it */
923 if (signal_pending(current))
924 return -ERESTARTSYS;
Johan Hovoldee49ee82013-03-19 09:21:13 +0100925
926 if (port->serial->disconnected)
927 return -EIO;
928
929 {
Alan Cox813a2242008-07-22 11:10:36 +0100930 char diff = priv->diff_status;
931 if (diff == 0)
932 return -EIO; /* no change => error */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
Alan Cox813a2242008-07-22 11:10:36 +0100934 /* consume all events */
935 priv->diff_status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
Alan Cox813a2242008-07-22 11:10:36 +0100937 /* return 0 if caller wanted to know about
938 these bits */
939 if (((arg & TIOCM_RNG) && (diff & UART_RI)) ||
940 ((arg & TIOCM_DSR) && (diff & UART_DSR)) ||
941 ((arg & TIOCM_CD) && (diff & UART_CD)) ||
942 ((arg & TIOCM_CTS) && (diff & UART_CTS)))
943 return 0;
944 /* otherwise caller can't care less about what
945 * happened, and so we continue to wait for
946 * more events.
947 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 }
Alan Cox813a2242008-07-22 11:10:36 +0100949 }
950 return 0;
951 default:
952 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 }
Harvey Harrison441b62c2008-03-03 16:08:34 -0800954 dbg("%s - arg not supported - it was 0x%04x - check include/asm/ioctls.h", __func__, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 return -ENOIOCTLCMD;
956} /* cypress_ioctl */
957
958
Alan Cox95da3102008-07-22 11:09:07 +0100959static void cypress_set_termios(struct tty_struct *tty,
960 struct usb_serial_port *port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961{
962 struct cypress_private *priv = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 int data_bits, stop_bits, parity_type, parity_enable;
Alan Cox8873aaa2008-03-10 21:59:28 +0000964 unsigned cflag, iflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 unsigned long flags;
966 __u8 oldlines;
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500967 int linechange = 0;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500968
Harvey Harrison441b62c2008-03-03 16:08:34 -0800969 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 spin_lock_irqsave(&priv->lock, flags);
Alan Coxfe1ae7f2009-09-19 13:13:33 -0700972 /* We can't clean this one up as we don't know the device type
973 early enough */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 if (!priv->termios_initialized) {
975 if (priv->chiptype == CT_EARTHMATE) {
976 *(tty->termios) = tty_std_termios;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500977 tty->termios->c_cflag = B4800 | CS8 | CREAD | HUPCL |
978 CLOCAL;
Alan Cox8873aaa2008-03-10 21:59:28 +0000979 tty->termios->c_ispeed = 4800;
980 tty->termios->c_ospeed = 4800;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 } else if (priv->chiptype == CT_CYPHIDCOM) {
982 *(tty->termios) = tty_std_termios;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500983 tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
984 CLOCAL;
Alan Cox8873aaa2008-03-10 21:59:28 +0000985 tty->termios->c_ispeed = 9600;
986 tty->termios->c_ospeed = 9600;
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600987 } else if (priv->chiptype == CT_CA42V2) {
988 *(tty->termios) = tty_std_termios;
989 tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
990 CLOCAL;
Alan Cox8873aaa2008-03-10 21:59:28 +0000991 tty->termios->c_ispeed = 9600;
992 tty->termios->c_ospeed = 9600;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 }
994 priv->termios_initialized = 1;
995 }
996 spin_unlock_irqrestore(&priv->lock, flags);
997
Alan Cox8873aaa2008-03-10 21:59:28 +0000998 /* Unsupported features need clearing */
999 tty->termios->c_cflag &= ~(CMSPAR|CRTSCTS);
1000
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 cflag = tty->termios->c_cflag;
1002 iflag = tty->termios->c_iflag;
1003
1004 /* check if there are new settings */
1005 if (old_termios) {
Alan Cox8873aaa2008-03-10 21:59:28 +00001006 spin_lock_irqsave(&priv->lock, flags);
1007 priv->tmp_termios = *(tty->termios);
1008 spin_unlock_irqrestore(&priv->lock, flags);
1009 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
1011 /* set number of data bits, parity, stop bits */
1012 /* when parity is disabled the parity type bit is ignored */
1013
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001014 /* 1 means 2 stop bits, 0 means 1 stop bit */
1015 stop_bits = cflag & CSTOPB ? 1 : 0;
1016
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 if (cflag & PARENB) {
1018 parity_enable = 1;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001019 /* 1 means odd parity, 0 means even parity */
1020 parity_type = cflag & PARODD ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 } else
1022 parity_enable = parity_type = 0;
1023
Alan Cox77336822008-07-22 11:10:53 +01001024 switch (cflag & CSIZE) {
1025 case CS5:
1026 data_bits = 0;
1027 break;
1028 case CS6:
1029 data_bits = 1;
1030 break;
1031 case CS7:
1032 data_bits = 2;
1033 break;
1034 case CS8:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 data_bits = 3;
Alan Cox77336822008-07-22 11:10:53 +01001036 break;
1037 default:
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001038 dev_err(&port->dev, "%s - CSIZE was set, but not CS5-CS8\n",
1039 __func__);
Alan Cox77336822008-07-22 11:10:53 +01001040 data_bits = 3;
1041 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 spin_lock_irqsave(&priv->lock, flags);
1043 oldlines = priv->line_control;
1044 if ((cflag & CBAUD) == B0) {
1045 /* drop dtr and rts */
Harvey Harrison441b62c2008-03-03 16:08:34 -08001046 dbg("%s - dropping the lines, baud rate 0bps", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
Alan Cox8873aaa2008-03-10 21:59:28 +00001048 } else
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001049 priv->line_control = (CONTROL_DTR | CONTROL_RTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001052 dbg("%s - sending %d stop_bits, %d parity_enable, %d parity_type, "
Harvey Harrison441b62c2008-03-03 16:08:34 -08001053 "%d data_bits (+5)", __func__, stop_bits,
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001054 parity_enable, parity_type, data_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
Alan Cox813a2242008-07-22 11:10:36 +01001056 cypress_serial_control(tty, port, tty_get_baud_rate(tty),
1057 data_bits, stop_bits,
1058 parity_enable, parity_type,
1059 0, CYPRESS_SET_CONFIG);
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001060
1061 /* we perform a CYPRESS_GET_CONFIG so that the current settings are
1062 * filled into the private structure this should confirm that all is
1063 * working if it returns what we just set */
Alan Cox95da3102008-07-22 11:09:07 +01001064 cypress_serial_control(tty, port, 0, 0, 0, 0, 0, 0, CYPRESS_GET_CONFIG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001066 /* Here we can define custom tty settings for devices; the main tty
1067 * termios flag base comes from empeg.c */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001069 spin_lock_irqsave(&priv->lock, flags);
Alan Cox813a2242008-07-22 11:10:36 +01001070 if (priv->chiptype == CT_EARTHMATE && priv->baud_rate == 4800) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001071 dbg("Using custom termios settings for a baud rate of "
1072 "4800bps.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 /* define custom termios settings for NMEA protocol */
1074
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 tty->termios->c_iflag /* input modes - */
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001076 &= ~(IGNBRK /* disable ignore break */
1077 | BRKINT /* disable break causes interrupt */
1078 | PARMRK /* disable mark parity errors */
1079 | ISTRIP /* disable clear high bit of input char */
1080 | INLCR /* disable translate NL to CR */
1081 | IGNCR /* disable ignore CR */
1082 | ICRNL /* disable translate CR to NL */
1083 | IXON); /* disable enable XON/XOFF flow control */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001085 tty->termios->c_oflag /* output modes */
1086 &= ~OPOST; /* disable postprocess output char */
1087
1088 tty->termios->c_lflag /* line discipline modes */
1089 &= ~(ECHO /* disable echo input characters */
1090 | ECHONL /* disable echo new line */
1091 | ICANON /* disable erase, kill, werase, and rprnt
1092 special characters */
1093 | ISIG /* disable interrupt, quit, and suspend
1094 special characters */
1095 | IEXTEN); /* disable non-POSIX special characters */
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001096 } /* CT_CYPHIDCOM: Application should handle this for device */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 linechange = (priv->line_control != oldlines);
1099 spin_unlock_irqrestore(&priv->lock, flags);
1100
1101 /* if necessary, set lines */
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001102 if (linechange) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 priv->cmd_ctrl = 1;
Alan Cox95da3102008-07-22 11:09:07 +01001104 cypress_write(tty, port, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106} /* cypress_set_termios */
1107
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001108
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109/* returns amount of data still left in soft buffer */
Alan Cox95da3102008-07-22 11:09:07 +01001110static int cypress_chars_in_buffer(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);
1114 int chars = 0;
1115 unsigned long flags;
1116
Harvey Harrison441b62c2008-03-03 16:08:34 -08001117 dbg("%s - port %d", __func__, port->number);
Alan Cox813a2242008-07-22 11:10:36 +01001118
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 spin_lock_irqsave(&priv->lock, flags);
Johan Hovold117fb8d2010-05-16 20:33:50 +02001120 chars = kfifo_len(&priv->write_fifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 spin_unlock_irqrestore(&priv->lock, flags);
1122
Harvey Harrison441b62c2008-03-03 16:08:34 -08001123 dbg("%s - returns %d", __func__, chars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 return chars;
1125}
1126
1127
Alan Cox95da3102008-07-22 11:09:07 +01001128static void cypress_throttle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129{
Alan Cox95da3102008-07-22 11:09:07 +01001130 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 struct cypress_private *priv = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Harvey Harrison441b62c2008-03-03 16:08:34 -08001133 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
Oliver Neukum63832512009-10-07 10:50:23 +02001135 spin_lock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 priv->rx_flags = THROTTLED;
Oliver Neukum63832512009-10-07 10:50:23 +02001137 spin_unlock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138}
1139
1140
Alan Cox95da3102008-07-22 11:09:07 +01001141static void cypress_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142{
Alan Cox95da3102008-07-22 11:09:07 +01001143 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 struct cypress_private *priv = usb_get_serial_port_data(port);
1145 int actually_throttled, result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
Harvey Harrison441b62c2008-03-03 16:08:34 -08001147 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
Oliver Neukum63832512009-10-07 10:50:23 +02001149 spin_lock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 actually_throttled = priv->rx_flags & ACTUALLY_THROTTLED;
1151 priv->rx_flags = 0;
Oliver Neukum63832512009-10-07 10:50:23 +02001152 spin_unlock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
Mike Isely78aef512006-08-29 22:07:11 -05001154 if (!priv->comm_is_ok)
1155 return;
1156
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 if (actually_throttled) {
Oliver Neukum63832512009-10-07 10:50:23 +02001158 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
Mike Isely78aef512006-08-29 22:07:11 -05001159 if (result) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001160 dev_err(&port->dev, "%s - failed submitting read urb, "
Harvey Harrison441b62c2008-03-03 16:08:34 -08001161 "error %d\n", __func__, result);
Mike Isely78aef512006-08-29 22:07:11 -05001162 cypress_set_dead(port);
1163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 }
1165}
1166
1167
David Howells7d12e782006-10-05 14:55:46 +01001168static void cypress_read_int_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169{
Ming Leicdc97792008-02-24 18:41:47 +08001170 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 struct cypress_private *priv = usb_get_serial_port_data(port);
1172 struct tty_struct *tty;
1173 unsigned char *data = urb->transfer_buffer;
1174 unsigned long flags;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001175 char tty_flag = TTY_NORMAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 int havedata = 0;
1177 int bytes = 0;
1178 int result;
1179 int i = 0;
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001180 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
Harvey Harrison441b62c2008-03-03 16:08:34 -08001182 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001184 switch (status) {
Mike Isely78aef512006-08-29 22:07:11 -05001185 case 0: /* success */
1186 break;
1187 case -ECONNRESET:
1188 case -ENOENT:
1189 case -ESHUTDOWN:
1190 /* precursor to disconnect so just go away */
1191 return;
1192 case -EPIPE:
Alan Stern4d2fae82009-07-09 12:59:57 -04001193 /* Can't call usb_clear_halt while in_interrupt */
1194 /* FALLS THROUGH */
Mike Isely78aef512006-08-29 22:07:11 -05001195 default:
1196 /* something ugly is going on... */
Alan Cox813a2242008-07-22 11:10:36 +01001197 dev_err(&urb->dev->dev,
1198 "%s - unexpected nonzero read status received: %d\n",
1199 __func__, status);
Mike Isely78aef512006-08-29 22:07:11 -05001200 cypress_set_dead(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 return;
1202 }
1203
1204 spin_lock_irqsave(&priv->lock, flags);
1205 if (priv->rx_flags & THROTTLED) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001206 dbg("%s - now throttling", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 priv->rx_flags |= ACTUALLY_THROTTLED;
1208 spin_unlock_irqrestore(&priv->lock, flags);
1209 return;
1210 }
1211 spin_unlock_irqrestore(&priv->lock, flags);
1212
Alan Cox4a90f092008-10-13 10:39:46 +01001213 tty = tty_port_tty_get(&port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 if (!tty) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001215 dbg("%s - bad tty pointer - exiting", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 return;
1217 }
1218
1219 spin_lock_irqsave(&priv->lock, flags);
Mike Isely3416eaa2008-02-10 20:23:19 -06001220 result = urb->actual_length;
1221 switch (priv->pkt_fmt) {
1222 default:
1223 case packet_format_1:
1224 /* This is for the CY7C64013... */
1225 priv->current_status = data[0] & 0xF8;
1226 bytes = data[1] + 2;
1227 i = 2;
1228 if (bytes > 2)
1229 havedata = 1;
1230 break;
1231 case packet_format_2:
1232 /* This is for the CY7C63743... */
1233 priv->current_status = data[0] & 0xF8;
1234 bytes = (data[0] & 0x07) + 1;
1235 i = 1;
1236 if (bytes > 1)
1237 havedata = 1;
1238 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 }
1240 spin_unlock_irqrestore(&priv->lock, flags);
Mike Isely3416eaa2008-02-10 20:23:19 -06001241 if (result < bytes) {
1242 dbg("%s - wrong packet size - received %d bytes but packet "
1243 "said %d bytes", __func__, result, bytes);
1244 goto continue_read;
1245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
Alan Cox813a2242008-07-22 11:10:36 +01001247 usb_serial_debug_data(debug, &port->dev, __func__,
1248 urb->actual_length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
1250 spin_lock_irqsave(&priv->lock, flags);
1251 /* check to see if status has changed */
Mike Isely67683062008-02-10 20:23:28 -06001252 if (priv->current_status != priv->prev_status) {
1253 priv->diff_status |= priv->current_status ^
1254 priv->prev_status;
Johan Hovoldee49ee82013-03-19 09:21:13 +01001255 wake_up_interruptible(&port->delta_msr_wait);
Mike Isely67683062008-02-10 20:23:28 -06001256 priv->prev_status = priv->current_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 }
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001258 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001260 /* hangup, as defined in acm.c... this might be a bad place for it
1261 * though */
1262 if (tty && !(tty->termios->c_cflag & CLOCAL) &&
1263 !(priv->current_status & UART_CD)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001264 dbg("%s - calling hangup", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 tty_hangup(tty);
1266 goto continue_read;
1267 }
1268
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001269 /* There is one error bit... I'm assuming it is a parity error
1270 * indicator as the generic firmware will set this bit to 1 if a
1271 * parity error occurs.
1272 * I can not find reference to any other error events. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 spin_lock_irqsave(&priv->lock, flags);
1274 if (priv->current_status & CYP_ERROR) {
1275 spin_unlock_irqrestore(&priv->lock, flags);
1276 tty_flag = TTY_PARITY;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001277 dbg("%s - Parity Error detected", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 } else
1279 spin_unlock_irqrestore(&priv->lock, flags);
1280
1281 /* process read if there is data other than line status */
Alan Coxa108bfc2010-02-18 16:44:01 +00001282 if (tty && bytes > i) {
1283 tty_insert_flip_string_fixed_flag(tty, data + i,
Johan Hovold70ced222010-05-07 19:46:56 +02001284 tty_flag, bytes - i);
Alan Cox4a90f092008-10-13 10:39:46 +01001285 tty_flip_buffer_push(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 }
1287
1288 spin_lock_irqsave(&priv->lock, flags);
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001289 /* control and status byte(s) are also counted */
1290 priv->bytes_in += bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 spin_unlock_irqrestore(&priv->lock, flags);
1292
1293continue_read:
Alan Cox4a90f092008-10-13 10:39:46 +01001294 tty_kref_put(tty);
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001295
Alan Stern1f871582010-02-17 10:05:47 -05001296 /* Continue trying to always read */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
Alan Stern1f871582010-02-17 10:05:47 -05001298 if (priv->comm_is_ok) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001299 usb_fill_int_urb(port->interrupt_in_urb, port->serial->dev,
1300 usb_rcvintpipe(port->serial->dev,
1301 port->interrupt_in_endpointAddress),
1302 port->interrupt_in_urb->transfer_buffer,
1303 port->interrupt_in_urb->transfer_buffer_length,
Alan Cox813a2242008-07-22 11:10:36 +01001304 cypress_read_int_callback, port,
1305 priv->read_urb_interval);
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001306 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
Alan Stern1f871582010-02-17 10:05:47 -05001307 if (result && result != -EPERM) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001308 dev_err(&urb->dev->dev, "%s - failed resubmitting "
Harvey Harrison441b62c2008-03-03 16:08:34 -08001309 "read urb, error %d\n", __func__,
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001310 result);
Mike Isely78aef512006-08-29 22:07:11 -05001311 cypress_set_dead(port);
1312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314} /* cypress_read_int_callback */
1315
1316
David Howells7d12e782006-10-05 14:55:46 +01001317static void cypress_write_int_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318{
Ming Leicdc97792008-02-24 18:41:47 +08001319 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 struct cypress_private *priv = usb_get_serial_port_data(port);
1321 int result;
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001322 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
Harvey Harrison441b62c2008-03-03 16:08:34 -08001324 dbg("%s - port %d", __func__, port->number);
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001325
1326 switch (status) {
Alan Cox813a2242008-07-22 11:10:36 +01001327 case 0:
1328 /* success */
1329 break;
1330 case -ECONNRESET:
1331 case -ENOENT:
1332 case -ESHUTDOWN:
1333 /* this urb is terminated, clean up */
1334 dbg("%s - urb shutting down with status: %d",
1335 __func__, status);
1336 priv->write_urb_in_use = 0;
1337 return;
1338 case -EPIPE: /* no break needed; clear halt and resubmit */
1339 if (!priv->comm_is_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 break;
Alan Cox813a2242008-07-22 11:10:36 +01001341 usb_clear_halt(port->serial->dev, 0x02);
1342 /* error in the urb, so we have to resubmit it */
1343 dbg("%s - nonzero write bulk status received: %d",
1344 __func__, status);
1345 port->interrupt_out_urb->transfer_buffer_length = 1;
Alan Cox813a2242008-07-22 11:10:36 +01001346 result = usb_submit_urb(port->interrupt_out_urb, GFP_ATOMIC);
1347 if (!result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 return;
Alan Cox813a2242008-07-22 11:10:36 +01001349 dev_err(&urb->dev->dev,
1350 "%s - failed resubmitting write urb, error %d\n",
1351 __func__, result);
1352 cypress_set_dead(port);
1353 break;
1354 default:
1355 dev_err(&urb->dev->dev,
1356 "%s - unexpected nonzero write status received: %d\n",
1357 __func__, status);
1358 cypress_set_dead(port);
1359 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 priv->write_urb_in_use = 0;
Alan Cox813a2242008-07-22 11:10:36 +01001362
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 /* send any buffered data */
1364 cypress_send(port);
1365}
1366
Greg Kroah-Hartman7e3131f2012-02-28 13:11:41 -08001367module_usb_serial_driver(cypress_driver, serial_drivers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
Alan Cox813a2242008-07-22 11:10:36 +01001369MODULE_AUTHOR(DRIVER_AUTHOR);
1370MODULE_DESCRIPTION(DRIVER_DESC);
1371MODULE_VERSION(DRIVER_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372MODULE_LICENSE("GPL");
1373
1374module_param(debug, bool, S_IRUGO | S_IWUSR);
1375MODULE_PARM_DESC(debug, "Debug enabled or not");
1376module_param(stats, bool, S_IRUGO | S_IWUSR);
1377MODULE_PARM_DESC(stats, "Enable statistics or not");
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001378module_param(interval, int, S_IRUGO | S_IWUSR);
1379MODULE_PARM_DESC(interval, "Overrides interrupt interval");
Mike Frysingerc3126592009-12-18 16:33:03 -05001380module_param(unstable_bauds, bool, S_IRUGO | S_IWUSR);
1381MODULE_PARM_DESC(unstable_bauds, "Allow unstable baud rates");