blob: d963748652893bfe9763e5121b8acde5f7afdaef [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.
19 *
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -050020 * Lonnie Mendez <dignome@gmail.com>
21 * 4-29-2005
Alan Cox813a2242008-07-22 11:10:36 +010022 * Fixed problem where setting or retreiving the serial config would fail
23 * with EPIPE. Removed CRTS toggling so the driver behaves more like
24 * other usbserial adapters. Issued new interval of 1ms instead of the
25 * default 10ms. As a result, transfer speed has been substantially
26 * increased from avg. 850bps to avg. 3300bps. initial termios has also
27 * been modified. Cleaned up code and formatting issues so it is more
28 * readable. Replaced the C++ style comments.
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 *
30 * Lonnie Mendez <dignome@gmail.com>
31 * 12-15-2004
32 * Incorporated write buffering from pl2303 driver. Fixed bug with line
33 * handling so both lines are raised in cypress_open. (was dropping rts)
34 * Various code cleanups made as well along with other misc bug fixes.
35 *
36 * Lonnie Mendez <dignome@gmail.com>
37 * 04-10-2004
38 * Driver modified to support dynamic line settings. Various improvments
39 * and features.
40 *
41 * Neil Whelchel
42 * 10-2003
43 * Driver first released.
44 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 */
46
Alan Cox813a2242008-07-22 11:10:36 +010047/* Thanks to Neil Whelchel for writing the first cypress m8 implementation
48 for linux. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070049/* Thanks to cypress for providing references for the hid reports. */
50/* Thanks to Jiang Zhang for providing links and for general help. */
Alan Cox813a2242008-07-22 11:10:36 +010051/* Code originates and was built up from ftdi_sio, belkin, pl2303 and others.*/
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <linux/kernel.h>
55#include <linux/errno.h>
56#include <linux/init.h>
57#include <linux/slab.h>
58#include <linux/tty.h>
59#include <linux/tty_driver.h>
60#include <linux/tty_flip.h>
61#include <linux/module.h>
62#include <linux/moduleparam.h>
63#include <linux/spinlock.h>
64#include <linux/usb.h>
Greg Kroah-Hartmana9698882006-07-11 21:22:58 -070065#include <linux/usb/serial.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#include <linux/serial.h>
67#include <linux/delay.h>
Alan Cox813a2242008-07-22 11:10:36 +010068#include <linux/uaccess.h>
Johan Hovold0f2c2d72009-12-31 16:48:01 +010069#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#include "cypress_m8.h"
72
73
Mike Frysinger64319dd2009-12-18 16:33:01 -050074static int debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075static int stats;
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -050076static int interval;
Mike Frysingerc3126592009-12-18 16:33:03 -050077static int unstable_bauds;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79/*
80 * Version Information
81 */
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -050082#define DRIVER_VERSION "v1.09"
Linus Torvalds1da177e2005-04-16 15:20:36 -070083#define DRIVER_AUTHOR "Lonnie Mendez <dignome@gmail.com>, Neil Whelchel <koyama@firstlight.net>"
84#define DRIVER_DESC "Cypress USB to Serial Driver"
85
86/* write buffer size defines */
87#define CYPRESS_BUF_SIZE 1024
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Németh Márton7d40d7e2010-01-10 15:34:24 +010089static const struct usb_device_id id_table_earthmate[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB) },
Lonnie Mendez25b6f082005-05-10 17:09:52 -050091 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB_LT20) },
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 { } /* Terminating entry */
93};
94
Németh Márton7d40d7e2010-01-10 15:34:24 +010095static const struct usb_device_id id_table_cyphidcomrs232[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 { USB_DEVICE(VENDOR_ID_CYPRESS, PRODUCT_ID_CYPHIDCOM) },
Dmitry Shapin6f6f06e2008-03-04 15:25:10 -080097 { USB_DEVICE(VENDOR_ID_POWERCOM, PRODUCT_ID_UPS) },
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 { } /* Terminating entry */
99};
100
Németh Márton7d40d7e2010-01-10 15:34:24 +0100101static const struct usb_device_id id_table_nokiaca42v2[] = {
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600102 { USB_DEVICE(VENDOR_ID_DAZZLE, PRODUCT_ID_CA42) },
103 { } /* Terminating entry */
104};
105
Németh Márton7d40d7e2010-01-10 15:34:24 +0100106static const struct usb_device_id id_table_combined[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB) },
Lonnie Mendez25b6f082005-05-10 17:09:52 -0500108 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB_LT20) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 { USB_DEVICE(VENDOR_ID_CYPRESS, PRODUCT_ID_CYPHIDCOM) },
Dmitry Shapin6f6f06e2008-03-04 15:25:10 -0800110 { USB_DEVICE(VENDOR_ID_POWERCOM, PRODUCT_ID_UPS) },
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600111 { USB_DEVICE(VENDOR_ID_DAZZLE, PRODUCT_ID_CA42) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 { } /* Terminating entry */
113};
114
Alan Cox813a2242008-07-22 11:10:36 +0100115MODULE_DEVICE_TABLE(usb, id_table_combined);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117static struct usb_driver cypress_driver = {
118 .name = "cypress",
119 .probe = usb_serial_probe,
120 .disconnect = usb_serial_disconnect,
121 .id_table = id_table_combined,
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800122 .no_dynamic_id = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123};
124
Mike Isely3416eaa2008-02-10 20:23:19 -0600125enum packet_format {
126 packet_format_1, /* b0:status, b1:payload count */
127 packet_format_2 /* b0[7:3]:status, b0[2:0]:payload count */
128};
129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130struct cypress_private {
131 spinlock_t lock; /* private lock */
132 int chiptype; /* identifier of device, for quirks/etc */
133 int bytes_in; /* used for statistics */
134 int bytes_out; /* used for statistics */
135 int cmd_count; /* used for statistics */
136 int cmd_ctrl; /* always set this to 1 before issuing a command */
137 struct cypress_buf *buf; /* write buffer */
138 int write_urb_in_use; /* write urb in use indicator */
Mike Isely0257fa92006-08-29 22:06:59 -0500139 int write_urb_interval; /* interval to use for write urb */
140 int read_urb_interval; /* interval to use for read urb */
Mike Isely78aef512006-08-29 22:07:11 -0500141 int comm_is_ok; /* true if communication is (still) ok */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 int termios_initialized;
143 __u8 line_control; /* holds dtr / rts value */
144 __u8 current_status; /* received from last read - info on dsr,cts,cd,ri,etc */
145 __u8 current_config; /* stores the current configuration byte */
146 __u8 rx_flags; /* throttling - used from whiteheat/ftdi_sio */
Mike Isely3416eaa2008-02-10 20:23:19 -0600147 enum packet_format pkt_fmt; /* format to use for packet send / receive */
Mike Isely3d6aa322008-02-10 20:23:24 -0600148 int get_cfg_unsafe; /* If true, the CYPRESS_GET_CONFIG is unsafe */
Alan Cox813a2242008-07-22 11:10:36 +0100149 int baud_rate; /* stores current baud rate in
150 integer form */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 int isthrottled; /* if throttled, discard reads */
152 wait_queue_head_t delta_msr_wait; /* used for TIOCMIWAIT */
153 char prev_status, diff_status; /* used for TIOCMIWAIT */
Daniel Mack3ad2f3f2010-02-03 08:01:28 +0800154 /* we pass a pointer to this as the argument sent to
Alan Cox813a2242008-07-22 11:10:36 +0100155 cypress_set_termios old_termios */
Alan Cox606d0992006-12-08 02:38:45 -0800156 struct ktermios tmp_termios; /* stores the old termios settings */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157};
158
159/* write buffer structure */
160struct cypress_buf {
161 unsigned int buf_size;
162 char *buf_buf;
163 char *buf_get;
164 char *buf_put;
165};
166
167/* function prototypes for the Cypress USB to serial device */
Alan Cox813a2242008-07-22 11:10:36 +0100168static int cypress_earthmate_startup(struct usb_serial *serial);
169static int cypress_hidcom_startup(struct usb_serial *serial);
170static int cypress_ca42v2_startup(struct usb_serial *serial);
Alan Sternf9c99bb2009-06-02 11:53:55 -0400171static void cypress_release(struct usb_serial *serial);
Alan Coxa509a7e2009-09-19 13:13:26 -0700172static int cypress_open(struct tty_struct *tty, struct usb_serial_port *port);
Alan Cox335f8512009-06-11 12:26:29 +0100173static void cypress_close(struct usb_serial_port *port);
174static void cypress_dtr_rts(struct usb_serial_port *port, int on);
Alan Cox813a2242008-07-22 11:10:36 +0100175static int cypress_write(struct tty_struct *tty, struct usb_serial_port *port,
176 const unsigned char *buf, int count);
177static void cypress_send(struct usb_serial_port *port);
178static int cypress_write_room(struct tty_struct *tty);
179static int cypress_ioctl(struct tty_struct *tty, struct file *file,
180 unsigned int cmd, unsigned long arg);
181static void cypress_set_termios(struct tty_struct *tty,
182 struct usb_serial_port *port, struct ktermios *old);
183static int cypress_tiocmget(struct tty_struct *tty, struct file *file);
184static int cypress_tiocmset(struct tty_struct *tty, struct file *file,
185 unsigned int set, unsigned int clear);
186static int cypress_chars_in_buffer(struct tty_struct *tty);
187static void cypress_throttle(struct tty_struct *tty);
188static void cypress_unthrottle(struct tty_struct *tty);
189static void cypress_set_dead(struct usb_serial_port *port);
190static void cypress_read_int_callback(struct urb *urb);
191static void cypress_write_int_callback(struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192/* write buffer functions */
193static struct cypress_buf *cypress_buf_alloc(unsigned int size);
Alan Cox813a2242008-07-22 11:10:36 +0100194static void cypress_buf_free(struct cypress_buf *cb);
195static void cypress_buf_clear(struct cypress_buf *cb);
196static unsigned int cypress_buf_data_avail(struct cypress_buf *cb);
197static unsigned int cypress_buf_space_avail(struct cypress_buf *cb);
198static unsigned int cypress_buf_put(struct cypress_buf *cb,
199 const char *buf, unsigned int count);
200static unsigned int cypress_buf_get(struct cypress_buf *cb,
201 char *buf, unsigned int count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700204static struct usb_serial_driver cypress_earthmate_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700205 .driver = {
206 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700207 .name = "earthmate",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700208 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700209 .description = "DeLorme Earthmate USB",
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100210 .usb_driver = &cypress_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 .id_table = id_table_earthmate,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 .num_ports = 1,
213 .attach = cypress_earthmate_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -0400214 .release = cypress_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 .open = cypress_open,
216 .close = cypress_close,
Alan Cox335f8512009-06-11 12:26:29 +0100217 .dtr_rts = cypress_dtr_rts,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 .write = cypress_write,
219 .write_room = cypress_write_room,
220 .ioctl = cypress_ioctl,
221 .set_termios = cypress_set_termios,
222 .tiocmget = cypress_tiocmget,
223 .tiocmset = cypress_tiocmset,
224 .chars_in_buffer = cypress_chars_in_buffer,
225 .throttle = cypress_throttle,
226 .unthrottle = cypress_unthrottle,
227 .read_int_callback = cypress_read_int_callback,
228 .write_int_callback = cypress_write_int_callback,
229};
230
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700231static struct usb_serial_driver cypress_hidcom_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700232 .driver = {
233 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700234 .name = "cyphidcom",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700235 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700236 .description = "HID->COM RS232 Adapter",
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100237 .usb_driver = &cypress_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 .id_table = id_table_cyphidcomrs232,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 .num_ports = 1,
240 .attach = cypress_hidcom_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -0400241 .release = cypress_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 .open = cypress_open,
243 .close = cypress_close,
Alan Cox335f8512009-06-11 12:26:29 +0100244 .dtr_rts = cypress_dtr_rts,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 .write = cypress_write,
246 .write_room = cypress_write_room,
247 .ioctl = cypress_ioctl,
248 .set_termios = cypress_set_termios,
249 .tiocmget = cypress_tiocmget,
250 .tiocmset = cypress_tiocmset,
251 .chars_in_buffer = cypress_chars_in_buffer,
252 .throttle = cypress_throttle,
253 .unthrottle = cypress_unthrottle,
254 .read_int_callback = cypress_read_int_callback,
255 .write_int_callback = cypress_write_int_callback,
256};
257
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600258static struct usb_serial_driver cypress_ca42v2_device = {
259 .driver = {
260 .owner = THIS_MODULE,
Alan Cox813a2242008-07-22 11:10:36 +0100261 .name = "nokiaca42v2",
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600262 },
263 .description = "Nokia CA-42 V2 Adapter",
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100264 .usb_driver = &cypress_driver,
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600265 .id_table = id_table_nokiaca42v2,
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600266 .num_ports = 1,
267 .attach = cypress_ca42v2_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -0400268 .release = cypress_release,
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600269 .open = cypress_open,
270 .close = cypress_close,
Alan Cox335f8512009-06-11 12:26:29 +0100271 .dtr_rts = cypress_dtr_rts,
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600272 .write = cypress_write,
273 .write_room = cypress_write_room,
274 .ioctl = cypress_ioctl,
275 .set_termios = cypress_set_termios,
276 .tiocmget = cypress_tiocmget,
277 .tiocmset = cypress_tiocmset,
278 .chars_in_buffer = cypress_chars_in_buffer,
279 .throttle = cypress_throttle,
280 .unthrottle = cypress_unthrottle,
281 .read_int_callback = cypress_read_int_callback,
282 .write_int_callback = cypress_write_int_callback,
283};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285/*****************************************************************************
286 * Cypress serial helper functions
287 *****************************************************************************/
288
289
Alan Cox8873aaa2008-03-10 21:59:28 +0000290static int analyze_baud_rate(struct usb_serial_port *port, speed_t new_rate)
Mike Isely92983c22008-02-10 20:23:32 -0600291{
Mike Isely92983c22008-02-10 20:23:32 -0600292 struct cypress_private *priv;
293 priv = usb_get_serial_port_data(port);
294
Mike Frysingerc3126592009-12-18 16:33:03 -0500295 if (unstable_bauds)
296 return new_rate;
297
Mike Isely92983c22008-02-10 20:23:32 -0600298 /*
299 * The general purpose firmware for the Cypress M8 allows for
300 * a maximum speed of 57600bps (I have no idea whether DeLorme
301 * chose to use the general purpose firmware or not), if you
302 * need to modify this speed setting for your own project
303 * please add your own chiptype and modify the code likewise.
304 * The Cypress HID->COM device will work successfully up to
305 * 115200bps (but the actual throughput is around 3kBps).
306 */
Mike Isely92983c22008-02-10 20:23:32 -0600307 if (port->serial->dev->speed == USB_SPEED_LOW) {
308 /*
309 * Mike Isely <isely@pobox.com> 2-Feb-2008: The
310 * Cypress app note that describes this mechanism
311 * states the the low-speed part can't handle more
312 * than 800 bytes/sec, in which case 4800 baud is the
313 * safest speed for a part like that.
314 */
315 if (new_rate > 4800) {
316 dbg("%s - failed setting baud rate, device incapable "
317 "speed %d", __func__, new_rate);
318 return -1;
319 }
320 }
321 switch (priv->chiptype) {
322 case CT_EARTHMATE:
323 if (new_rate <= 600) {
324 /* 300 and 600 baud rates are supported under
325 * the generic firmware, but are not used with
326 * NMEA and SiRF protocols */
327 dbg("%s - failed setting baud rate, unsupported speed "
328 "of %d on Earthmate GPS", __func__, new_rate);
329 return -1;
330 }
331 break;
332 default:
333 break;
334 }
335 return new_rate;
336}
337
338
Steven Cole093cf722005-05-03 19:07:24 -0600339/* This function can either set or retrieve the current serial line settings */
Alan Cox813a2242008-07-22 11:10:36 +0100340static int cypress_serial_control(struct tty_struct *tty,
Alan Cox95da3102008-07-22 11:09:07 +0100341 struct usb_serial_port *port, speed_t baud_rate, int data_bits,
342 int stop_bits, int parity_enable, int parity_type, int reset,
343 int cypress_request_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500345 int new_baudrate = 0, retval = 0, tries = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 struct cypress_private *priv;
Johan Hovold09546442009-12-28 23:01:48 +0100347 u8 *feature_buffer;
348 const unsigned int feature_len = 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 unsigned long flags;
350
Harvey Harrison441b62c2008-03-03 16:08:34 -0800351 dbg("%s", __func__);
Alan Cox813a2242008-07-22 11:10:36 +0100352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 priv = usb_get_serial_port_data(port);
354
Mike Isely78aef512006-08-29 22:07:11 -0500355 if (!priv->comm_is_ok)
356 return -ENODEV;
357
Johan Hovold09546442009-12-28 23:01:48 +0100358 feature_buffer = kcalloc(feature_len, sizeof(u8), GFP_KERNEL);
359 if (!feature_buffer)
360 return -ENOMEM;
361
Alan Cox813a2242008-07-22 11:10:36 +0100362 switch (cypress_request_type) {
363 case CYPRESS_SET_CONFIG:
Alan Cox813a2242008-07-22 11:10:36 +0100364 /* 0 means 'Hang up' so doesn't change the true bit rate */
Mike Frysinger2805eb12009-12-18 16:33:02 -0500365 new_baudrate = priv->baud_rate;
366 if (baud_rate && baud_rate != priv->baud_rate) {
Alan Cox813a2242008-07-22 11:10:36 +0100367 dbg("%s - baud rate is changing", __func__);
368 retval = analyze_baud_rate(port, baud_rate);
Mike Frysinger2805eb12009-12-18 16:33:02 -0500369 if (retval >= 0) {
Alan Cox813a2242008-07-22 11:10:36 +0100370 new_baudrate = retval;
371 dbg("%s - New baud rate set to %d",
372 __func__, new_baudrate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 }
Alan Cox813a2242008-07-22 11:10:36 +0100374 }
375 dbg("%s - baud rate is being sent as %d",
376 __func__, new_baudrate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Alan Cox813a2242008-07-22 11:10:36 +0100378 /* fill the feature_buffer with new configuration */
Johan Hovold0f2c2d72009-12-31 16:48:01 +0100379 put_unaligned_le32(new_baudrate, feature_buffer);
Alan Cox813a2242008-07-22 11:10:36 +0100380 feature_buffer[4] |= data_bits; /* assign data bits in 2 bit space ( max 3 ) */
381 /* 1 bit gap */
382 feature_buffer[4] |= (stop_bits << 3); /* assign stop bits in 1 bit space */
383 feature_buffer[4] |= (parity_enable << 4); /* assign parity flag in 1 bit space */
384 feature_buffer[4] |= (parity_type << 5); /* assign parity type in 1 bit space */
385 /* 1 bit gap */
386 feature_buffer[4] |= (reset << 7); /* assign reset at end of byte, 1 bit space */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Alan Cox813a2242008-07-22 11:10:36 +0100388 dbg("%s - device is being sent this feature report:",
389 __func__);
390 dbg("%s - %02X - %02X - %02X - %02X - %02X", __func__,
391 feature_buffer[0], feature_buffer[1],
392 feature_buffer[2], feature_buffer[3],
393 feature_buffer[4]);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500394
Alan Cox813a2242008-07-22 11:10:36 +0100395 do {
396 retval = usb_control_msg(port->serial->dev,
397 usb_sndctrlpipe(port->serial->dev, 0),
398 HID_REQ_SET_REPORT,
399 USB_DIR_OUT | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
400 0x0300, 0, feature_buffer,
Johan Hovold09546442009-12-28 23:01:48 +0100401 feature_len, 500);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500402
Alan Cox813a2242008-07-22 11:10:36 +0100403 if (tries++ >= 3)
404 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Johan Hovold09546442009-12-28 23:01:48 +0100406 } while (retval != feature_len &&
Alan Cox813a2242008-07-22 11:10:36 +0100407 retval != -ENODEV);
Mike Isely93075542008-02-10 20:23:14 -0600408
Johan Hovold09546442009-12-28 23:01:48 +0100409 if (retval != feature_len) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700410 dev_err(&port->dev, "%s - failed sending serial "
411 "line settings - %d\n", __func__, retval);
Alan Cox813a2242008-07-22 11:10:36 +0100412 cypress_set_dead(port);
413 } else {
414 spin_lock_irqsave(&priv->lock, flags);
415 priv->baud_rate = new_baudrate;
416 priv->current_config = feature_buffer[4];
417 spin_unlock_irqrestore(&priv->lock, flags);
418 /* If we asked for a speed change encode it */
419 if (baud_rate)
420 tty_encode_baud_rate(tty,
421 new_baudrate, new_baudrate);
422 }
423 break;
424 case CYPRESS_GET_CONFIG:
425 if (priv->get_cfg_unsafe) {
426 /* Not implemented for this device,
427 and if we try to do it we're likely
428 to crash the hardware. */
Johan Hovold09546442009-12-28 23:01:48 +0100429 retval = -ENOTTY;
430 goto out;
Alan Cox813a2242008-07-22 11:10:36 +0100431 }
432 dbg("%s - retreiving serial line settings", __func__);
Alan Cox813a2242008-07-22 11:10:36 +0100433 do {
434 retval = usb_control_msg(port->serial->dev,
435 usb_rcvctrlpipe(port->serial->dev, 0),
436 HID_REQ_GET_REPORT,
437 USB_DIR_IN | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
438 0x0300, 0, feature_buffer,
Johan Hovold09546442009-12-28 23:01:48 +0100439 feature_len, 500);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500440
Alan Cox813a2242008-07-22 11:10:36 +0100441 if (tries++ >= 3)
442 break;
Johan Hovold09546442009-12-28 23:01:48 +0100443 } while (retval != feature_len
Alan Cox813a2242008-07-22 11:10:36 +0100444 && retval != -ENODEV);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500445
Johan Hovold09546442009-12-28 23:01:48 +0100446 if (retval != feature_len) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700447 dev_err(&port->dev, "%s - failed to retrieve serial "
448 "line settings - %d\n", __func__, retval);
Alan Cox813a2242008-07-22 11:10:36 +0100449 cypress_set_dead(port);
Johan Hovold09546442009-12-28 23:01:48 +0100450 goto out;
Alan Cox813a2242008-07-22 11:10:36 +0100451 } else {
452 spin_lock_irqsave(&priv->lock, flags);
453 /* store the config in one byte, and later
454 use bit masks to check values */
455 priv->current_config = feature_buffer[4];
Johan Hovold0f2c2d72009-12-31 16:48:01 +0100456 priv->baud_rate = get_unaligned_le32(feature_buffer);
Alan Cox813a2242008-07-22 11:10:36 +0100457 spin_unlock_irqrestore(&priv->lock, flags);
458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 }
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500460 spin_lock_irqsave(&priv->lock, flags);
461 ++priv->cmd_count;
462 spin_unlock_irqrestore(&priv->lock, flags);
Johan Hovold09546442009-12-28 23:01:48 +0100463out:
464 kfree(feature_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 return retval;
466} /* cypress_serial_control */
467
468
Mike Isely78aef512006-08-29 22:07:11 -0500469static void cypress_set_dead(struct usb_serial_port *port)
470{
471 struct cypress_private *priv = usb_get_serial_port_data(port);
472 unsigned long flags;
473
474 spin_lock_irqsave(&priv->lock, flags);
475 if (!priv->comm_is_ok) {
476 spin_unlock_irqrestore(&priv->lock, flags);
477 return;
478 }
479 priv->comm_is_ok = 0;
480 spin_unlock_irqrestore(&priv->lock, flags);
481
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700482 dev_err(&port->dev, "cypress_m8 suspending failing port %d - "
483 "interval might be too short\n", port->number);
Mike Isely78aef512006-08-29 22:07:11 -0500484}
485
486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487/*****************************************************************************
488 * Cypress serial driver functions
489 *****************************************************************************/
490
491
Alan Cox813a2242008-07-22 11:10:36 +0100492static int generic_startup(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
494 struct cypress_private *priv;
Mike Isely0257fa92006-08-29 22:06:59 -0500495 struct usb_serial_port *port = serial->port[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Harvey Harrison441b62c2008-03-03 16:08:34 -0800497 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Alan Cox813a2242008-07-22 11:10:36 +0100499 priv = kzalloc(sizeof(struct cypress_private), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 if (!priv)
501 return -ENOMEM;
502
Mike Isely78aef512006-08-29 22:07:11 -0500503 priv->comm_is_ok = !0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 spin_lock_init(&priv->lock);
505 priv->buf = cypress_buf_alloc(CYPRESS_BUF_SIZE);
506 if (priv->buf == NULL) {
507 kfree(priv);
508 return -ENOMEM;
509 }
510 init_waitqueue_head(&priv->delta_msr_wait);
Alan Cox813a2242008-07-22 11:10:36 +0100511
512 usb_reset_configuration(serial->dev);
513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 priv->cmd_ctrl = 0;
515 priv->line_control = 0;
516 priv->termios_initialized = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 priv->rx_flags = 0;
Mike Isely3416eaa2008-02-10 20:23:19 -0600518 /* Default packet format setting is determined by packet size.
519 Anything with a size larger then 9 must have a separate
520 count field since the 3 bit count field is otherwise too
521 small. Otherwise we can use the slightly more compact
522 format. This is in accordance with the cypress_m8 serial
523 converter app note. */
Alan Cox813a2242008-07-22 11:10:36 +0100524 if (port->interrupt_out_size > 9)
Mike Isely3416eaa2008-02-10 20:23:19 -0600525 priv->pkt_fmt = packet_format_1;
Alan Cox813a2242008-07-22 11:10:36 +0100526 else
Mike Isely3416eaa2008-02-10 20:23:19 -0600527 priv->pkt_fmt = packet_format_2;
Alan Cox813a2242008-07-22 11:10:36 +0100528
Mike Isely0257fa92006-08-29 22:06:59 -0500529 if (interval > 0) {
530 priv->write_urb_interval = interval;
531 priv->read_urb_interval = interval;
532 dbg("%s - port %d read & write intervals forced to %d",
Alan Cox813a2242008-07-22 11:10:36 +0100533 __func__, port->number, interval);
Mike Isely0257fa92006-08-29 22:06:59 -0500534 } else {
535 priv->write_urb_interval = port->interrupt_out_urb->interval;
536 priv->read_urb_interval = port->interrupt_in_urb->interval;
537 dbg("%s - port %d intervals: read=%d write=%d",
Alan Cox813a2242008-07-22 11:10:36 +0100538 __func__, port->number,
539 priv->read_urb_interval, priv->write_urb_interval);
Mike Isely0257fa92006-08-29 22:06:59 -0500540 }
541 usb_set_serial_port_data(port, priv);
Alan Cox813a2242008-07-22 11:10:36 +0100542
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500543 return 0;
544}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
546
Alan Cox813a2242008-07-22 11:10:36 +0100547static int cypress_earthmate_startup(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
549 struct cypress_private *priv;
Mike Isely3d6aa322008-02-10 20:23:24 -0600550 struct usb_serial_port *port = serial->port[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Harvey Harrison441b62c2008-03-03 16:08:34 -0800552 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554 if (generic_startup(serial)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800555 dbg("%s - Failed setting up port %d", __func__,
Mike Isely3d6aa322008-02-10 20:23:24 -0600556 port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 return 1;
558 }
559
Mike Isely3d6aa322008-02-10 20:23:24 -0600560 priv = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 priv->chiptype = CT_EARTHMATE;
Mike Isely3416eaa2008-02-10 20:23:19 -0600562 /* All Earthmate devices use the separated-count packet
563 format! Idiotic. */
564 priv->pkt_fmt = packet_format_1;
Alan Cox813a2242008-07-22 11:10:36 +0100565 if (serial->dev->descriptor.idProduct !=
566 cpu_to_le16(PRODUCT_ID_EARTHMATEUSB)) {
Mike Isely3d6aa322008-02-10 20:23:24 -0600567 /* The old original USB Earthmate seemed able to
568 handle GET_CONFIG requests; everything they've
569 produced since that time crashes if this command is
570 attempted :-( */
571 dbg("%s - Marking this device as unsafe for GET_CONFIG "
572 "commands", __func__);
573 priv->get_cfg_unsafe = !0;
574 }
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500575
576 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577} /* cypress_earthmate_startup */
578
579
Alan Cox813a2242008-07-22 11:10:36 +0100580static int cypress_hidcom_startup(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
582 struct cypress_private *priv;
583
Harvey Harrison441b62c2008-03-03 16:08:34 -0800584 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
586 if (generic_startup(serial)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800587 dbg("%s - Failed setting up port %d", __func__,
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500588 serial->port[0]->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 return 1;
590 }
591
592 priv = usb_get_serial_port_data(serial->port[0]);
593 priv->chiptype = CT_CYPHIDCOM;
Alan Cox813a2242008-07-22 11:10:36 +0100594
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500595 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596} /* cypress_hidcom_startup */
597
598
Alan Cox813a2242008-07-22 11:10:36 +0100599static int cypress_ca42v2_startup(struct usb_serial *serial)
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600600{
601 struct cypress_private *priv;
602
Harvey Harrison441b62c2008-03-03 16:08:34 -0800603 dbg("%s", __func__);
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600604
605 if (generic_startup(serial)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800606 dbg("%s - Failed setting up port %d", __func__,
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600607 serial->port[0]->number);
608 return 1;
609 }
610
611 priv = usb_get_serial_port_data(serial->port[0]);
612 priv->chiptype = CT_CA42V2;
613
614 return 0;
615} /* cypress_ca42v2_startup */
616
617
Alan Sternf9c99bb2009-06-02 11:53:55 -0400618static void cypress_release(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
620 struct cypress_private *priv;
621
Alan Cox813a2242008-07-22 11:10:36 +0100622 dbg("%s - port %d", __func__, serial->port[0]->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
624 /* all open ports are closed at this point */
625
626 priv = usb_get_serial_port_data(serial->port[0]);
627
628 if (priv) {
629 cypress_buf_free(priv->buf);
630 kfree(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 }
632}
633
634
Alan Coxa509a7e2009-09-19 13:13:26 -0700635static int cypress_open(struct tty_struct *tty, struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636{
637 struct cypress_private *priv = usb_get_serial_port_data(port);
638 struct usb_serial *serial = port->serial;
639 unsigned long flags;
640 int result = 0;
641
Harvey Harrison441b62c2008-03-03 16:08:34 -0800642 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Mike Isely78aef512006-08-29 22:07:11 -0500644 if (!priv->comm_is_ok)
645 return -EIO;
646
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 /* clear halts before open */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 usb_clear_halt(serial->dev, 0x81);
649 usb_clear_halt(serial->dev, 0x02);
650
651 spin_lock_irqsave(&priv->lock, flags);
652 /* reset read/write statistics */
653 priv->bytes_in = 0;
654 priv->bytes_out = 0;
655 priv->cmd_count = 0;
656 priv->rx_flags = 0;
657 spin_unlock_irqrestore(&priv->lock, flags);
658
Alan Cox335f8512009-06-11 12:26:29 +0100659 /* Set termios */
Alan Coxfe1ae7f2009-09-19 13:13:33 -0700660 cypress_send(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Alan Cox95da3102008-07-22 11:09:07 +0100662 if (tty)
663 cypress_set_termios(tty, port, &priv->tmp_termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665 /* setup the port and start reading from the device */
Alan Cox813a2242008-07-22 11:10:36 +0100666 if (!port->interrupt_in_urb) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700667 dev_err(&port->dev, "%s - interrupt_in_urb is empty!\n",
668 __func__);
Alan Cox813a2242008-07-22 11:10:36 +0100669 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 }
671
672 usb_fill_int_urb(port->interrupt_in_urb, serial->dev,
673 usb_rcvintpipe(serial->dev, port->interrupt_in_endpointAddress),
Alan Cox813a2242008-07-22 11:10:36 +0100674 port->interrupt_in_urb->transfer_buffer,
675 port->interrupt_in_urb->transfer_buffer_length,
Mike Isely0257fa92006-08-29 22:06:59 -0500676 cypress_read_int_callback, port, priv->read_urb_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
678
Alan Cox813a2242008-07-22 11:10:36 +0100679 if (result) {
680 dev_err(&port->dev,
681 "%s - failed submitting read urb, error %d\n",
682 __func__, result);
Mike Isely78aef512006-08-29 22:07:11 -0500683 cypress_set_dead(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 }
Alan Cox335f8512009-06-11 12:26:29 +0100685 port->port.drain_delay = 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 return result;
687} /* cypress_open */
688
Alan Cox335f8512009-06-11 12:26:29 +0100689static void cypress_dtr_rts(struct usb_serial_port *port, int on)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690{
691 struct cypress_private *priv = usb_get_serial_port_data(port);
Alan Cox335f8512009-06-11 12:26:29 +0100692 /* drop dtr and rts */
Alan Cox335f8512009-06-11 12:26:29 +0100693 spin_lock_irq(&priv->lock);
694 if (on == 0)
695 priv->line_control = 0;
696 else
697 priv->line_control = CONTROL_DTR | CONTROL_RTS;
698 priv->cmd_ctrl = 1;
699 spin_unlock_irq(&priv->lock);
700 cypress_write(NULL, port, NULL, 0);
701}
702
703static void cypress_close(struct usb_serial_port *port)
704{
705 struct cypress_private *priv = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Harvey Harrison441b62c2008-03-03 16:08:34 -0800707 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100709 /* writing is potentially harmful, lock must be taken */
710 mutex_lock(&port->serial->disc_mutex);
711 if (port->serial->disconnected) {
712 mutex_unlock(&port->serial->disc_mutex);
713 return;
714 }
Alan Cox335f8512009-06-11 12:26:29 +0100715 cypress_buf_clear(priv->buf);
Harvey Harrison441b62c2008-03-03 16:08:34 -0800716 dbg("%s - stopping urbs", __func__);
Alan Cox813a2242008-07-22 11:10:36 +0100717 usb_kill_urb(port->interrupt_in_urb);
718 usb_kill_urb(port->interrupt_out_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
721 if (stats)
Alan Cox813a2242008-07-22 11:10:36 +0100722 dev_info(&port->dev, "Statistics: %d Bytes In | %d Bytes Out | %d Commands Issued\n",
723 priv->bytes_in, priv->bytes_out, priv->cmd_count);
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100724 mutex_unlock(&port->serial->disc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725} /* cypress_close */
726
727
Alan Cox95da3102008-07-22 11:09:07 +0100728static int cypress_write(struct tty_struct *tty, struct usb_serial_port *port,
729 const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730{
731 struct cypress_private *priv = usb_get_serial_port_data(port);
732 unsigned long flags;
Alan Cox813a2242008-07-22 11:10:36 +0100733
Harvey Harrison441b62c2008-03-03 16:08:34 -0800734 dbg("%s - port %d, %d bytes", __func__, port->number, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
736 /* line control commands, which need to be executed immediately,
737 are not put into the buffer for obvious reasons.
738 */
739 if (priv->cmd_ctrl) {
740 count = 0;
741 goto finish;
742 }
Alan Cox813a2242008-07-22 11:10:36 +0100743
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 if (!count)
745 return count;
Alan Cox813a2242008-07-22 11:10:36 +0100746
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 spin_lock_irqsave(&priv->lock, flags);
748 count = cypress_buf_put(priv->buf, buf, count);
749 spin_unlock_irqrestore(&priv->lock, flags);
750
751finish:
752 cypress_send(port);
753
754 return count;
755} /* cypress_write */
756
757
758static void cypress_send(struct usb_serial_port *port)
759{
760 int count = 0, result, offset, actual_size;
761 struct cypress_private *priv = usb_get_serial_port_data(port);
762 unsigned long flags;
Alan Cox813a2242008-07-22 11:10:36 +0100763
Mike Isely78aef512006-08-29 22:07:11 -0500764 if (!priv->comm_is_ok)
765 return;
766
Harvey Harrison441b62c2008-03-03 16:08:34 -0800767 dbg("%s - port %d", __func__, port->number);
Alan Cox813a2242008-07-22 11:10:36 +0100768 dbg("%s - interrupt out size is %d", __func__,
769 port->interrupt_out_size);
770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 spin_lock_irqsave(&priv->lock, flags);
772 if (priv->write_urb_in_use) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800773 dbg("%s - can't write, urb in use", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 spin_unlock_irqrestore(&priv->lock, flags);
775 return;
776 }
777 spin_unlock_irqrestore(&priv->lock, flags);
778
779 /* clear buffer */
Alan Cox813a2242008-07-22 11:10:36 +0100780 memset(port->interrupt_out_urb->transfer_buffer, 0,
781 port->interrupt_out_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
783 spin_lock_irqsave(&priv->lock, flags);
Mike Isely3416eaa2008-02-10 20:23:19 -0600784 switch (priv->pkt_fmt) {
785 default:
786 case packet_format_1:
787 /* this is for the CY7C64013... */
788 offset = 2;
789 port->interrupt_out_buffer[0] = priv->line_control;
790 break;
791 case packet_format_2:
792 /* this is for the CY7C63743... */
793 offset = 1;
794 port->interrupt_out_buffer[0] = priv->line_control;
795 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 }
797
798 if (priv->line_control & CONTROL_RESET)
799 priv->line_control &= ~CONTROL_RESET;
800
801 if (priv->cmd_ctrl) {
802 priv->cmd_count++;
Harvey Harrison441b62c2008-03-03 16:08:34 -0800803 dbg("%s - line control command being issued", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 spin_unlock_irqrestore(&priv->lock, flags);
805 goto send;
806 } else
807 spin_unlock_irqrestore(&priv->lock, flags);
808
809 count = cypress_buf_get(priv->buf, &port->interrupt_out_buffer[offset],
810 port->interrupt_out_size-offset);
811
Alan Cox813a2242008-07-22 11:10:36 +0100812 if (count == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
Mike Isely3416eaa2008-02-10 20:23:19 -0600815 switch (priv->pkt_fmt) {
816 default:
817 case packet_format_1:
818 port->interrupt_out_buffer[1] = count;
819 break;
820 case packet_format_2:
821 port->interrupt_out_buffer[0] |= count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 }
823
Harvey Harrison441b62c2008-03-03 16:08:34 -0800824 dbg("%s - count is %d", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
826send:
827 spin_lock_irqsave(&priv->lock, flags);
828 priv->write_urb_in_use = 1;
829 spin_unlock_irqrestore(&priv->lock, flags);
830
831 if (priv->cmd_ctrl)
832 actual_size = 1;
833 else
Mike Isely3416eaa2008-02-10 20:23:19 -0600834 actual_size = count +
835 (priv->pkt_fmt == packet_format_1 ? 2 : 1);
836
Alan Cox813a2242008-07-22 11:10:36 +0100837 usb_serial_debug_data(debug, &port->dev, __func__,
838 port->interrupt_out_size,
839 port->interrupt_out_urb->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
Mike Isely9aa8dae2006-08-29 22:07:04 -0500841 usb_fill_int_urb(port->interrupt_out_urb, port->serial->dev,
842 usb_sndintpipe(port->serial->dev, port->interrupt_out_endpointAddress),
843 port->interrupt_out_buffer, port->interrupt_out_size,
844 cypress_write_int_callback, port, priv->write_urb_interval);
Alan Cox813a2242008-07-22 11:10:36 +0100845 result = usb_submit_urb(port->interrupt_out_urb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 if (result) {
Alan Cox813a2242008-07-22 11:10:36 +0100847 dev_err(&port->dev,
848 "%s - failed submitting write urb, error %d\n",
849 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 priv->write_urb_in_use = 0;
Mike Isely78aef512006-08-29 22:07:11 -0500851 cypress_set_dead(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 }
853
854 spin_lock_irqsave(&priv->lock, flags);
Alan Cox813a2242008-07-22 11:10:36 +0100855 if (priv->cmd_ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 priv->cmd_ctrl = 0;
Alan Cox813a2242008-07-22 11:10:36 +0100857
858 /* do not count the line control and size bytes */
859 priv->bytes_out += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 spin_unlock_irqrestore(&priv->lock, flags);
861
Pete Zaitcevcf2c7482006-05-22 21:58:49 -0700862 usb_serial_port_softint(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863} /* cypress_send */
864
865
866/* returns how much space is available in the soft buffer */
Alan Cox95da3102008-07-22 11:09:07 +0100867static int cypress_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868{
Alan Cox95da3102008-07-22 11:09:07 +0100869 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 struct cypress_private *priv = usb_get_serial_port_data(port);
871 int room = 0;
872 unsigned long flags;
873
Harvey Harrison441b62c2008-03-03 16:08:34 -0800874 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
876 spin_lock_irqsave(&priv->lock, flags);
877 room = cypress_buf_space_avail(priv->buf);
878 spin_unlock_irqrestore(&priv->lock, flags);
879
Harvey Harrison441b62c2008-03-03 16:08:34 -0800880 dbg("%s - returns %d", __func__, room);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 return room;
882}
883
884
Alan Cox95da3102008-07-22 11:09:07 +0100885static int cypress_tiocmget(struct tty_struct *tty, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886{
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 __u8 status, control;
890 unsigned int result = 0;
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 control = priv->line_control;
897 status = priv->current_status;
898 spin_unlock_irqrestore(&priv->lock, flags);
899
900 result = ((control & CONTROL_DTR) ? TIOCM_DTR : 0)
901 | ((control & CONTROL_RTS) ? TIOCM_RTS : 0)
902 | ((status & UART_CTS) ? TIOCM_CTS : 0)
903 | ((status & UART_DSR) ? TIOCM_DSR : 0)
904 | ((status & UART_RI) ? TIOCM_RI : 0)
905 | ((status & UART_CD) ? TIOCM_CD : 0);
906
Harvey Harrison441b62c2008-03-03 16:08:34 -0800907 dbg("%s - result = %x", __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
909 return result;
910}
911
912
Alan Cox95da3102008-07-22 11:09:07 +0100913static int cypress_tiocmset(struct tty_struct *tty, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 unsigned int set, unsigned int clear)
915{
Alan Cox95da3102008-07-22 11:09:07 +0100916 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 struct cypress_private *priv = usb_get_serial_port_data(port);
918 unsigned long flags;
Alan Cox813a2242008-07-22 11:10:36 +0100919
Harvey Harrison441b62c2008-03-03 16:08:34 -0800920 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
922 spin_lock_irqsave(&priv->lock, flags);
923 if (set & TIOCM_RTS)
924 priv->line_control |= CONTROL_RTS;
925 if (set & TIOCM_DTR)
926 priv->line_control |= CONTROL_DTR;
927 if (clear & TIOCM_RTS)
928 priv->line_control &= ~CONTROL_RTS;
929 if (clear & TIOCM_DTR)
930 priv->line_control &= ~CONTROL_DTR;
Alan Cox8873aaa2008-03-10 21:59:28 +0000931 priv->cmd_ctrl = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 spin_unlock_irqrestore(&priv->lock, flags);
933
Alan Cox95da3102008-07-22 11:09:07 +0100934 return cypress_write(tty, port, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935}
936
937
Alan Cox813a2242008-07-22 11:10:36 +0100938static int cypress_ioctl(struct tty_struct *tty, struct file *file,
Alan Cox95da3102008-07-22 11:09:07 +0100939 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940{
Alan Cox95da3102008-07-22 11:09:07 +0100941 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 struct cypress_private *priv = usb_get_serial_port_data(port);
943
Harvey Harrison441b62c2008-03-03 16:08:34 -0800944 dbg("%s - port %d, cmd 0x%.4x", __func__, port->number, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
946 switch (cmd) {
Alan Cox813a2242008-07-22 11:10:36 +0100947 /* This code comes from drivers/char/serial.c and ftdi_sio.c */
948 case TIOCMIWAIT:
949 while (priv != NULL) {
950 interruptible_sleep_on(&priv->delta_msr_wait);
951 /* see if a signal did it */
952 if (signal_pending(current))
953 return -ERESTARTSYS;
954 else {
955 char diff = priv->diff_status;
956 if (diff == 0)
957 return -EIO; /* no change => error */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
Alan Cox813a2242008-07-22 11:10:36 +0100959 /* consume all events */
960 priv->diff_status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
Alan Cox813a2242008-07-22 11:10:36 +0100962 /* return 0 if caller wanted to know about
963 these bits */
964 if (((arg & TIOCM_RNG) && (diff & UART_RI)) ||
965 ((arg & TIOCM_DSR) && (diff & UART_DSR)) ||
966 ((arg & TIOCM_CD) && (diff & UART_CD)) ||
967 ((arg & TIOCM_CTS) && (diff & UART_CTS)))
968 return 0;
969 /* otherwise caller can't care less about what
970 * happened, and so we continue to wait for
971 * more events.
972 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 }
Alan Cox813a2242008-07-22 11:10:36 +0100974 }
975 return 0;
976 default:
977 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 }
Harvey Harrison441b62c2008-03-03 16:08:34 -0800979 dbg("%s - arg not supported - it was 0x%04x - check include/asm/ioctls.h", __func__, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 return -ENOIOCTLCMD;
981} /* cypress_ioctl */
982
983
Alan Cox95da3102008-07-22 11:09:07 +0100984static void cypress_set_termios(struct tty_struct *tty,
985 struct usb_serial_port *port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986{
987 struct cypress_private *priv = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 int data_bits, stop_bits, parity_type, parity_enable;
Alan Cox8873aaa2008-03-10 21:59:28 +0000989 unsigned cflag, iflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 unsigned long flags;
991 __u8 oldlines;
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500992 int linechange = 0;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500993
Harvey Harrison441b62c2008-03-03 16:08:34 -0800994 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 spin_lock_irqsave(&priv->lock, flags);
Alan Coxfe1ae7f2009-09-19 13:13:33 -0700997 /* We can't clean this one up as we don't know the device type
998 early enough */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 if (!priv->termios_initialized) {
1000 if (priv->chiptype == CT_EARTHMATE) {
1001 *(tty->termios) = tty_std_termios;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001002 tty->termios->c_cflag = B4800 | CS8 | CREAD | HUPCL |
1003 CLOCAL;
Alan Cox8873aaa2008-03-10 21:59:28 +00001004 tty->termios->c_ispeed = 4800;
1005 tty->termios->c_ospeed = 4800;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 } else if (priv->chiptype == CT_CYPHIDCOM) {
1007 *(tty->termios) = tty_std_termios;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001008 tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
1009 CLOCAL;
Alan Cox8873aaa2008-03-10 21:59:28 +00001010 tty->termios->c_ispeed = 9600;
1011 tty->termios->c_ospeed = 9600;
Lonnie Mendeza5c44e22006-03-01 10:45:24 -06001012 } else if (priv->chiptype == CT_CA42V2) {
1013 *(tty->termios) = tty_std_termios;
1014 tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
1015 CLOCAL;
Alan Cox8873aaa2008-03-10 21:59:28 +00001016 tty->termios->c_ispeed = 9600;
1017 tty->termios->c_ospeed = 9600;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 }
1019 priv->termios_initialized = 1;
1020 }
1021 spin_unlock_irqrestore(&priv->lock, flags);
1022
Alan Cox8873aaa2008-03-10 21:59:28 +00001023 /* Unsupported features need clearing */
1024 tty->termios->c_cflag &= ~(CMSPAR|CRTSCTS);
1025
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 cflag = tty->termios->c_cflag;
1027 iflag = tty->termios->c_iflag;
1028
1029 /* check if there are new settings */
1030 if (old_termios) {
Alan Cox8873aaa2008-03-10 21:59:28 +00001031 spin_lock_irqsave(&priv->lock, flags);
1032 priv->tmp_termios = *(tty->termios);
1033 spin_unlock_irqrestore(&priv->lock, flags);
1034 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
1036 /* set number of data bits, parity, stop bits */
1037 /* when parity is disabled the parity type bit is ignored */
1038
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001039 /* 1 means 2 stop bits, 0 means 1 stop bit */
1040 stop_bits = cflag & CSTOPB ? 1 : 0;
1041
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 if (cflag & PARENB) {
1043 parity_enable = 1;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001044 /* 1 means odd parity, 0 means even parity */
1045 parity_type = cflag & PARODD ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 } else
1047 parity_enable = parity_type = 0;
1048
Alan Cox77336822008-07-22 11:10:53 +01001049 switch (cflag & CSIZE) {
1050 case CS5:
1051 data_bits = 0;
1052 break;
1053 case CS6:
1054 data_bits = 1;
1055 break;
1056 case CS7:
1057 data_bits = 2;
1058 break;
1059 case CS8:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 data_bits = 3;
Alan Cox77336822008-07-22 11:10:53 +01001061 break;
1062 default:
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001063 dev_err(&port->dev, "%s - CSIZE was set, but not CS5-CS8\n",
1064 __func__);
Alan Cox77336822008-07-22 11:10:53 +01001065 data_bits = 3;
1066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 spin_lock_irqsave(&priv->lock, flags);
1068 oldlines = priv->line_control;
1069 if ((cflag & CBAUD) == B0) {
1070 /* drop dtr and rts */
Harvey Harrison441b62c2008-03-03 16:08:34 -08001071 dbg("%s - dropping the lines, baud rate 0bps", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
Alan Cox8873aaa2008-03-10 21:59:28 +00001073 } else
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001074 priv->line_control = (CONTROL_DTR | CONTROL_RTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001077 dbg("%s - sending %d stop_bits, %d parity_enable, %d parity_type, "
Harvey Harrison441b62c2008-03-03 16:08:34 -08001078 "%d data_bits (+5)", __func__, stop_bits,
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001079 parity_enable, parity_type, data_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
Alan Cox813a2242008-07-22 11:10:36 +01001081 cypress_serial_control(tty, port, tty_get_baud_rate(tty),
1082 data_bits, stop_bits,
1083 parity_enable, parity_type,
1084 0, CYPRESS_SET_CONFIG);
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001085
1086 /* we perform a CYPRESS_GET_CONFIG so that the current settings are
1087 * filled into the private structure this should confirm that all is
1088 * working if it returns what we just set */
Alan Cox95da3102008-07-22 11:09:07 +01001089 cypress_serial_control(tty, port, 0, 0, 0, 0, 0, 0, CYPRESS_GET_CONFIG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001091 /* Here we can define custom tty settings for devices; the main tty
1092 * termios flag base comes from empeg.c */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001094 spin_lock_irqsave(&priv->lock, flags);
Alan Cox813a2242008-07-22 11:10:36 +01001095 if (priv->chiptype == CT_EARTHMATE && priv->baud_rate == 4800) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001096 dbg("Using custom termios settings for a baud rate of "
1097 "4800bps.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 /* define custom termios settings for NMEA protocol */
1099
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 tty->termios->c_iflag /* input modes - */
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001101 &= ~(IGNBRK /* disable ignore break */
1102 | BRKINT /* disable break causes interrupt */
1103 | PARMRK /* disable mark parity errors */
1104 | ISTRIP /* disable clear high bit of input char */
1105 | INLCR /* disable translate NL to CR */
1106 | IGNCR /* disable ignore CR */
1107 | ICRNL /* disable translate CR to NL */
1108 | IXON); /* disable enable XON/XOFF flow control */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001110 tty->termios->c_oflag /* output modes */
1111 &= ~OPOST; /* disable postprocess output char */
1112
1113 tty->termios->c_lflag /* line discipline modes */
1114 &= ~(ECHO /* disable echo input characters */
1115 | ECHONL /* disable echo new line */
1116 | ICANON /* disable erase, kill, werase, and rprnt
1117 special characters */
1118 | ISIG /* disable interrupt, quit, and suspend
1119 special characters */
1120 | IEXTEN); /* disable non-POSIX special characters */
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001121 } /* CT_CYPHIDCOM: Application should handle this for device */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 linechange = (priv->line_control != oldlines);
1124 spin_unlock_irqrestore(&priv->lock, flags);
1125
1126 /* if necessary, set lines */
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001127 if (linechange) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 priv->cmd_ctrl = 1;
Alan Cox95da3102008-07-22 11:09:07 +01001129 cypress_write(tty, port, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131} /* cypress_set_termios */
1132
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001133
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134/* returns amount of data still left in soft buffer */
Alan Cox95da3102008-07-22 11:09:07 +01001135static int cypress_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136{
Alan Cox95da3102008-07-22 11:09:07 +01001137 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 struct cypress_private *priv = usb_get_serial_port_data(port);
1139 int chars = 0;
1140 unsigned long flags;
1141
Harvey Harrison441b62c2008-03-03 16:08:34 -08001142 dbg("%s - port %d", __func__, port->number);
Alan Cox813a2242008-07-22 11:10:36 +01001143
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 spin_lock_irqsave(&priv->lock, flags);
1145 chars = cypress_buf_data_avail(priv->buf);
1146 spin_unlock_irqrestore(&priv->lock, flags);
1147
Harvey Harrison441b62c2008-03-03 16:08:34 -08001148 dbg("%s - returns %d", __func__, chars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 return chars;
1150}
1151
1152
Alan Cox95da3102008-07-22 11:09:07 +01001153static void cypress_throttle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154{
Alan Cox95da3102008-07-22 11:09:07 +01001155 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 struct cypress_private *priv = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
Harvey Harrison441b62c2008-03-03 16:08:34 -08001158 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Oliver Neukum63832512009-10-07 10:50:23 +02001160 spin_lock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 priv->rx_flags = THROTTLED;
Oliver Neukum63832512009-10-07 10:50:23 +02001162 spin_unlock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163}
1164
1165
Alan Cox95da3102008-07-22 11:09:07 +01001166static void cypress_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167{
Alan Cox95da3102008-07-22 11:09:07 +01001168 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 struct cypress_private *priv = usb_get_serial_port_data(port);
1170 int actually_throttled, result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
Harvey Harrison441b62c2008-03-03 16:08:34 -08001172 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
Oliver Neukum63832512009-10-07 10:50:23 +02001174 spin_lock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 actually_throttled = priv->rx_flags & ACTUALLY_THROTTLED;
1176 priv->rx_flags = 0;
Oliver Neukum63832512009-10-07 10:50:23 +02001177 spin_unlock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
Mike Isely78aef512006-08-29 22:07:11 -05001179 if (!priv->comm_is_ok)
1180 return;
1181
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 if (actually_throttled) {
1183 port->interrupt_in_urb->dev = port->serial->dev;
1184
Oliver Neukum63832512009-10-07 10:50:23 +02001185 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
Mike Isely78aef512006-08-29 22:07:11 -05001186 if (result) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001187 dev_err(&port->dev, "%s - failed submitting read urb, "
Harvey Harrison441b62c2008-03-03 16:08:34 -08001188 "error %d\n", __func__, result);
Mike Isely78aef512006-08-29 22:07:11 -05001189 cypress_set_dead(port);
1190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 }
1192}
1193
1194
David Howells7d12e782006-10-05 14:55:46 +01001195static void cypress_read_int_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196{
Ming Leicdc97792008-02-24 18:41:47 +08001197 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 struct cypress_private *priv = usb_get_serial_port_data(port);
1199 struct tty_struct *tty;
1200 unsigned char *data = urb->transfer_buffer;
1201 unsigned long flags;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001202 char tty_flag = TTY_NORMAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 int havedata = 0;
1204 int bytes = 0;
1205 int result;
1206 int i = 0;
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001207 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Harvey Harrison441b62c2008-03-03 16:08:34 -08001209 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001211 switch (status) {
Mike Isely78aef512006-08-29 22:07:11 -05001212 case 0: /* success */
1213 break;
1214 case -ECONNRESET:
1215 case -ENOENT:
1216 case -ESHUTDOWN:
1217 /* precursor to disconnect so just go away */
1218 return;
1219 case -EPIPE:
Alan Stern4d2fae82009-07-09 12:59:57 -04001220 /* Can't call usb_clear_halt while in_interrupt */
1221 /* FALLS THROUGH */
Mike Isely78aef512006-08-29 22:07:11 -05001222 default:
1223 /* something ugly is going on... */
Alan Cox813a2242008-07-22 11:10:36 +01001224 dev_err(&urb->dev->dev,
1225 "%s - unexpected nonzero read status received: %d\n",
1226 __func__, status);
Mike Isely78aef512006-08-29 22:07:11 -05001227 cypress_set_dead(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 return;
1229 }
1230
1231 spin_lock_irqsave(&priv->lock, flags);
1232 if (priv->rx_flags & THROTTLED) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001233 dbg("%s - now throttling", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 priv->rx_flags |= ACTUALLY_THROTTLED;
1235 spin_unlock_irqrestore(&priv->lock, flags);
1236 return;
1237 }
1238 spin_unlock_irqrestore(&priv->lock, flags);
1239
Alan Cox4a90f092008-10-13 10:39:46 +01001240 tty = tty_port_tty_get(&port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 if (!tty) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001242 dbg("%s - bad tty pointer - exiting", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 return;
1244 }
1245
1246 spin_lock_irqsave(&priv->lock, flags);
Mike Isely3416eaa2008-02-10 20:23:19 -06001247 result = urb->actual_length;
1248 switch (priv->pkt_fmt) {
1249 default:
1250 case packet_format_1:
1251 /* This is for the CY7C64013... */
1252 priv->current_status = data[0] & 0xF8;
1253 bytes = data[1] + 2;
1254 i = 2;
1255 if (bytes > 2)
1256 havedata = 1;
1257 break;
1258 case packet_format_2:
1259 /* This is for the CY7C63743... */
1260 priv->current_status = data[0] & 0xF8;
1261 bytes = (data[0] & 0x07) + 1;
1262 i = 1;
1263 if (bytes > 1)
1264 havedata = 1;
1265 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 }
1267 spin_unlock_irqrestore(&priv->lock, flags);
Mike Isely3416eaa2008-02-10 20:23:19 -06001268 if (result < bytes) {
1269 dbg("%s - wrong packet size - received %d bytes but packet "
1270 "said %d bytes", __func__, result, bytes);
1271 goto continue_read;
1272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
Alan Cox813a2242008-07-22 11:10:36 +01001274 usb_serial_debug_data(debug, &port->dev, __func__,
1275 urb->actual_length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
1277 spin_lock_irqsave(&priv->lock, flags);
1278 /* check to see if status has changed */
Mike Isely67683062008-02-10 20:23:28 -06001279 if (priv->current_status != priv->prev_status) {
1280 priv->diff_status |= priv->current_status ^
1281 priv->prev_status;
1282 wake_up_interruptible(&priv->delta_msr_wait);
1283 priv->prev_status = priv->current_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 }
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001285 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001287 /* hangup, as defined in acm.c... this might be a bad place for it
1288 * though */
1289 if (tty && !(tty->termios->c_cflag & CLOCAL) &&
1290 !(priv->current_status & UART_CD)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001291 dbg("%s - calling hangup", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 tty_hangup(tty);
1293 goto continue_read;
1294 }
1295
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001296 /* There is one error bit... I'm assuming it is a parity error
1297 * indicator as the generic firmware will set this bit to 1 if a
1298 * parity error occurs.
1299 * I can not find reference to any other error events. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 spin_lock_irqsave(&priv->lock, flags);
1301 if (priv->current_status & CYP_ERROR) {
1302 spin_unlock_irqrestore(&priv->lock, flags);
1303 tty_flag = TTY_PARITY;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001304 dbg("%s - Parity Error detected", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 } else
1306 spin_unlock_irqrestore(&priv->lock, flags);
1307
1308 /* process read if there is data other than line status */
Alan Coxa108bfc2010-02-18 16:44:01 +00001309 if (tty && bytes > i) {
1310 tty_insert_flip_string_fixed_flag(tty, data + i,
Johan Hovold70ced222010-05-07 19:46:56 +02001311 tty_flag, bytes - i);
Alan Cox4a90f092008-10-13 10:39:46 +01001312 tty_flip_buffer_push(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 }
1314
1315 spin_lock_irqsave(&priv->lock, flags);
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001316 /* control and status byte(s) are also counted */
1317 priv->bytes_in += bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 spin_unlock_irqrestore(&priv->lock, flags);
1319
1320continue_read:
Alan Cox4a90f092008-10-13 10:39:46 +01001321 tty_kref_put(tty);
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001322
Alan Stern1f871582010-02-17 10:05:47 -05001323 /* Continue trying to always read */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
Alan Stern1f871582010-02-17 10:05:47 -05001325 if (priv->comm_is_ok) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001326 usb_fill_int_urb(port->interrupt_in_urb, port->serial->dev,
1327 usb_rcvintpipe(port->serial->dev,
1328 port->interrupt_in_endpointAddress),
1329 port->interrupt_in_urb->transfer_buffer,
1330 port->interrupt_in_urb->transfer_buffer_length,
Alan Cox813a2242008-07-22 11:10:36 +01001331 cypress_read_int_callback, port,
1332 priv->read_urb_interval);
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001333 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
Alan Stern1f871582010-02-17 10:05:47 -05001334 if (result && result != -EPERM) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001335 dev_err(&urb->dev->dev, "%s - failed resubmitting "
Harvey Harrison441b62c2008-03-03 16:08:34 -08001336 "read urb, error %d\n", __func__,
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001337 result);
Mike Isely78aef512006-08-29 22:07:11 -05001338 cypress_set_dead(port);
1339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 }
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001341
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 return;
1343} /* cypress_read_int_callback */
1344
1345
David Howells7d12e782006-10-05 14:55:46 +01001346static void cypress_write_int_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347{
Ming Leicdc97792008-02-24 18:41:47 +08001348 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 struct cypress_private *priv = usb_get_serial_port_data(port);
1350 int result;
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001351 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
Harvey Harrison441b62c2008-03-03 16:08:34 -08001353 dbg("%s - port %d", __func__, port->number);
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001354
1355 switch (status) {
Alan Cox813a2242008-07-22 11:10:36 +01001356 case 0:
1357 /* success */
1358 break;
1359 case -ECONNRESET:
1360 case -ENOENT:
1361 case -ESHUTDOWN:
1362 /* this urb is terminated, clean up */
1363 dbg("%s - urb shutting down with status: %d",
1364 __func__, status);
1365 priv->write_urb_in_use = 0;
1366 return;
1367 case -EPIPE: /* no break needed; clear halt and resubmit */
1368 if (!priv->comm_is_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 break;
Alan Cox813a2242008-07-22 11:10:36 +01001370 usb_clear_halt(port->serial->dev, 0x02);
1371 /* error in the urb, so we have to resubmit it */
1372 dbg("%s - nonzero write bulk status received: %d",
1373 __func__, status);
1374 port->interrupt_out_urb->transfer_buffer_length = 1;
1375 port->interrupt_out_urb->dev = port->serial->dev;
1376 result = usb_submit_urb(port->interrupt_out_urb, GFP_ATOMIC);
1377 if (!result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 return;
Alan Cox813a2242008-07-22 11:10:36 +01001379 dev_err(&urb->dev->dev,
1380 "%s - failed resubmitting write urb, error %d\n",
1381 __func__, result);
1382 cypress_set_dead(port);
1383 break;
1384 default:
1385 dev_err(&urb->dev->dev,
1386 "%s - unexpected nonzero write status received: %d\n",
1387 __func__, status);
1388 cypress_set_dead(port);
1389 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 priv->write_urb_in_use = 0;
Alan Cox813a2242008-07-22 11:10:36 +01001392
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 /* send any buffered data */
1394 cypress_send(port);
1395}
1396
1397
1398/*****************************************************************************
1399 * Write buffer functions - buffering code from pl2303 used
1400 *****************************************************************************/
1401
1402/*
1403 * cypress_buf_alloc
1404 *
1405 * Allocate a circular buffer and all associated memory.
1406 */
1407
1408static struct cypress_buf *cypress_buf_alloc(unsigned int size)
1409{
1410
1411 struct cypress_buf *cb;
1412
1413
1414 if (size == 0)
1415 return NULL;
1416
Robert P. J. Day5cbded52006-12-13 00:35:56 -08001417 cb = kmalloc(sizeof(struct cypress_buf), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 if (cb == NULL)
1419 return NULL;
1420
1421 cb->buf_buf = kmalloc(size, GFP_KERNEL);
1422 if (cb->buf_buf == NULL) {
1423 kfree(cb);
1424 return NULL;
1425 }
1426
1427 cb->buf_size = size;
1428 cb->buf_get = cb->buf_put = cb->buf_buf;
1429
1430 return cb;
1431
1432}
1433
1434
1435/*
1436 * cypress_buf_free
1437 *
1438 * Free the buffer and all associated memory.
1439 */
1440
1441static void cypress_buf_free(struct cypress_buf *cb)
1442{
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001443 if (cb) {
1444 kfree(cb->buf_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 kfree(cb);
1446 }
1447}
1448
1449
1450/*
1451 * cypress_buf_clear
1452 *
1453 * Clear out all data in the circular buffer.
1454 */
1455
1456static void cypress_buf_clear(struct cypress_buf *cb)
1457{
1458 if (cb != NULL)
1459 cb->buf_get = cb->buf_put;
1460 /* equivalent to a get of all data available */
1461}
1462
1463
1464/*
1465 * cypress_buf_data_avail
1466 *
1467 * Return the number of bytes of data available in the circular
1468 * buffer.
1469 */
1470
1471static unsigned int cypress_buf_data_avail(struct cypress_buf *cb)
1472{
1473 if (cb != NULL)
Alan Cox813a2242008-07-22 11:10:36 +01001474 return (cb->buf_size + cb->buf_put - cb->buf_get)
1475 % cb->buf_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 else
1477 return 0;
1478}
1479
1480
1481/*
1482 * cypress_buf_space_avail
1483 *
1484 * Return the number of bytes of space available in the circular
1485 * buffer.
1486 */
1487
1488static unsigned int cypress_buf_space_avail(struct cypress_buf *cb)
1489{
1490 if (cb != NULL)
Alan Cox813a2242008-07-22 11:10:36 +01001491 return (cb->buf_size + cb->buf_get - cb->buf_put - 1)
1492 % cb->buf_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 else
1494 return 0;
1495}
1496
1497
1498/*
1499 * cypress_buf_put
1500 *
1501 * Copy data data from a user buffer and put it into the circular buffer.
1502 * Restrict to the amount of space available.
1503 *
1504 * Return the number of bytes copied.
1505 */
1506
1507static unsigned int cypress_buf_put(struct cypress_buf *cb, const char *buf,
1508 unsigned int count)
1509{
1510
1511 unsigned int len;
1512
1513
1514 if (cb == NULL)
1515 return 0;
1516
1517 len = cypress_buf_space_avail(cb);
1518 if (count > len)
1519 count = len;
1520
1521 if (count == 0)
1522 return 0;
1523
1524 len = cb->buf_buf + cb->buf_size - cb->buf_put;
1525 if (count > len) {
1526 memcpy(cb->buf_put, buf, len);
1527 memcpy(cb->buf_buf, buf+len, count - len);
1528 cb->buf_put = cb->buf_buf + count - len;
1529 } else {
1530 memcpy(cb->buf_put, buf, count);
1531 if (count < len)
1532 cb->buf_put += count;
1533 else /* count == len */
1534 cb->buf_put = cb->buf_buf;
1535 }
1536
1537 return count;
1538
1539}
1540
1541
1542/*
1543 * cypress_buf_get
1544 *
1545 * Get data from the circular buffer and copy to the given buffer.
1546 * Restrict to the amount of data available.
1547 *
1548 * Return the number of bytes copied.
1549 */
1550
1551static unsigned int cypress_buf_get(struct cypress_buf *cb, char *buf,
1552 unsigned int count)
1553{
1554
1555 unsigned int len;
1556
1557
1558 if (cb == NULL)
1559 return 0;
1560
1561 len = cypress_buf_data_avail(cb);
1562 if (count > len)
1563 count = len;
1564
1565 if (count == 0)
1566 return 0;
1567
1568 len = cb->buf_buf + cb->buf_size - cb->buf_get;
1569 if (count > len) {
1570 memcpy(buf, cb->buf_get, len);
1571 memcpy(buf+len, cb->buf_buf, count - len);
1572 cb->buf_get = cb->buf_buf + count - len;
1573 } else {
1574 memcpy(buf, cb->buf_get, count);
1575 if (count < len)
1576 cb->buf_get += count;
1577 else /* count == len */
1578 cb->buf_get = cb->buf_buf;
1579 }
1580
1581 return count;
1582
1583}
1584
1585/*****************************************************************************
1586 * Module functions
1587 *****************************************************************************/
1588
1589static int __init cypress_init(void)
1590{
1591 int retval;
Alan Cox813a2242008-07-22 11:10:36 +01001592
Harvey Harrison441b62c2008-03-03 16:08:34 -08001593 dbg("%s", __func__);
Alan Cox813a2242008-07-22 11:10:36 +01001594
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 retval = usb_serial_register(&cypress_earthmate_device);
1596 if (retval)
1597 goto failed_em_register;
1598 retval = usb_serial_register(&cypress_hidcom_device);
1599 if (retval)
1600 goto failed_hidcom_register;
Lonnie Mendeza5c44e22006-03-01 10:45:24 -06001601 retval = usb_serial_register(&cypress_ca42v2_device);
1602 if (retval)
1603 goto failed_ca42v2_register;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 retval = usb_register(&cypress_driver);
1605 if (retval)
1606 goto failed_usb_register;
1607
Greg Kroah-Hartmanc197a8d2008-08-18 13:21:04 -07001608 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
1609 DRIVER_DESC "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
Mariusz Kozlowski2e46b742006-11-17 17:49:22 +01001612failed_usb_register:
1613 usb_serial_deregister(&cypress_ca42v2_device);
1614failed_ca42v2_register:
1615 usb_serial_deregister(&cypress_hidcom_device);
1616failed_hidcom_register:
1617 usb_serial_deregister(&cypress_earthmate_device);
1618failed_em_register:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 return retval;
1620}
1621
1622
Alan Cox813a2242008-07-22 11:10:36 +01001623static void __exit cypress_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624{
Harvey Harrison441b62c2008-03-03 16:08:34 -08001625 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626
Alan Cox813a2242008-07-22 11:10:36 +01001627 usb_deregister(&cypress_driver);
1628 usb_serial_deregister(&cypress_earthmate_device);
1629 usb_serial_deregister(&cypress_hidcom_device);
1630 usb_serial_deregister(&cypress_ca42v2_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631}
1632
1633
1634module_init(cypress_init);
1635module_exit(cypress_exit);
1636
Alan Cox813a2242008-07-22 11:10:36 +01001637MODULE_AUTHOR(DRIVER_AUTHOR);
1638MODULE_DESCRIPTION(DRIVER_DESC);
1639MODULE_VERSION(DRIVER_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640MODULE_LICENSE("GPL");
1641
1642module_param(debug, bool, S_IRUGO | S_IWUSR);
1643MODULE_PARM_DESC(debug, "Debug enabled or not");
1644module_param(stats, bool, S_IRUGO | S_IWUSR);
1645MODULE_PARM_DESC(stats, "Enable statistics or not");
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001646module_param(interval, int, S_IRUGO | S_IWUSR);
1647MODULE_PARM_DESC(interval, "Overrides interrupt interval");
Mike Frysingerc3126592009-12-18 16:33:03 -05001648module_param(unstable_bauds, bool, S_IRUGO | S_IWUSR);
1649MODULE_PARM_DESC(unstable_bauds, "Allow unstable baud rates");