blob: 22837a3f2f899e963f0d944814d222bcd2e494f5 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#include "cypress_m8.h"
71
72
73#ifdef CONFIG_USB_SERIAL_DEBUG
74 static int debug = 1;
75#else
76 static int debug;
77#endif
78static int stats;
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -050079static int interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81/*
82 * Version Information
83 */
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -050084#define DRIVER_VERSION "v1.09"
Linus Torvalds1da177e2005-04-16 15:20:36 -070085#define DRIVER_AUTHOR "Lonnie Mendez <dignome@gmail.com>, Neil Whelchel <koyama@firstlight.net>"
86#define DRIVER_DESC "Cypress USB to Serial Driver"
87
88/* write buffer size defines */
89#define CYPRESS_BUF_SIZE 1024
90#define CYPRESS_CLOSING_WAIT (30*HZ)
91
92static struct usb_device_id id_table_earthmate [] = {
93 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB) },
Lonnie Mendez25b6f082005-05-10 17:09:52 -050094 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB_LT20) },
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 { } /* Terminating entry */
96};
97
98static struct usb_device_id id_table_cyphidcomrs232 [] = {
99 { USB_DEVICE(VENDOR_ID_CYPRESS, PRODUCT_ID_CYPHIDCOM) },
Dmitry Shapin6f6f06e2008-03-04 15:25:10 -0800100 { USB_DEVICE(VENDOR_ID_POWERCOM, PRODUCT_ID_UPS) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 { } /* Terminating entry */
102};
103
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600104static struct usb_device_id id_table_nokiaca42v2 [] = {
105 { USB_DEVICE(VENDOR_ID_DAZZLE, PRODUCT_ID_CA42) },
106 { } /* Terminating entry */
107};
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109static struct usb_device_id id_table_combined [] = {
110 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB) },
Lonnie Mendez25b6f082005-05-10 17:09:52 -0500111 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB_LT20) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 { USB_DEVICE(VENDOR_ID_CYPRESS, PRODUCT_ID_CYPHIDCOM) },
Dmitry Shapin6f6f06e2008-03-04 15:25:10 -0800113 { USB_DEVICE(VENDOR_ID_POWERCOM, PRODUCT_ID_UPS) },
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600114 { USB_DEVICE(VENDOR_ID_DAZZLE, PRODUCT_ID_CA42) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 { } /* Terminating entry */
116};
117
Alan Cox813a2242008-07-22 11:10:36 +0100118MODULE_DEVICE_TABLE(usb, id_table_combined);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120static struct usb_driver cypress_driver = {
121 .name = "cypress",
122 .probe = usb_serial_probe,
123 .disconnect = usb_serial_disconnect,
124 .id_table = id_table_combined,
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800125 .no_dynamic_id = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126};
127
Mike Isely3416eaa2008-02-10 20:23:19 -0600128enum packet_format {
129 packet_format_1, /* b0:status, b1:payload count */
130 packet_format_2 /* b0[7:3]:status, b0[2:0]:payload count */
131};
132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133struct cypress_private {
134 spinlock_t lock; /* private lock */
135 int chiptype; /* identifier of device, for quirks/etc */
136 int bytes_in; /* used for statistics */
137 int bytes_out; /* used for statistics */
138 int cmd_count; /* used for statistics */
139 int cmd_ctrl; /* always set this to 1 before issuing a command */
140 struct cypress_buf *buf; /* write buffer */
141 int write_urb_in_use; /* write urb in use indicator */
Mike Isely0257fa92006-08-29 22:06:59 -0500142 int write_urb_interval; /* interval to use for write urb */
143 int read_urb_interval; /* interval to use for read urb */
Mike Isely78aef512006-08-29 22:07:11 -0500144 int comm_is_ok; /* true if communication is (still) ok */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 int termios_initialized;
146 __u8 line_control; /* holds dtr / rts value */
147 __u8 current_status; /* received from last read - info on dsr,cts,cd,ri,etc */
148 __u8 current_config; /* stores the current configuration byte */
149 __u8 rx_flags; /* throttling - used from whiteheat/ftdi_sio */
Mike Isely3416eaa2008-02-10 20:23:19 -0600150 enum packet_format pkt_fmt; /* format to use for packet send / receive */
Mike Isely3d6aa322008-02-10 20:23:24 -0600151 int get_cfg_unsafe; /* If true, the CYPRESS_GET_CONFIG is unsafe */
Alan Cox813a2242008-07-22 11:10:36 +0100152 int baud_rate; /* stores current baud rate in
153 integer form */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 int isthrottled; /* if throttled, discard reads */
155 wait_queue_head_t delta_msr_wait; /* used for TIOCMIWAIT */
156 char prev_status, diff_status; /* used for TIOCMIWAIT */
Alan Cox813a2242008-07-22 11:10:36 +0100157 /* we pass a pointer to this as the arguement sent to
158 cypress_set_termios old_termios */
Alan Cox606d0992006-12-08 02:38:45 -0800159 struct ktermios tmp_termios; /* stores the old termios settings */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160};
161
162/* write buffer structure */
163struct cypress_buf {
164 unsigned int buf_size;
165 char *buf_buf;
166 char *buf_get;
167 char *buf_put;
168};
169
170/* function prototypes for the Cypress USB to serial device */
Alan Cox813a2242008-07-22 11:10:36 +0100171static int cypress_earthmate_startup(struct usb_serial *serial);
172static int cypress_hidcom_startup(struct usb_serial *serial);
173static int cypress_ca42v2_startup(struct usb_serial *serial);
174static void cypress_shutdown(struct usb_serial *serial);
175static int cypress_open(struct tty_struct *tty,
176 struct usb_serial_port *port, struct file *filp);
177static void cypress_close(struct tty_struct *tty,
178 struct usb_serial_port *port, struct file *filp);
179static int cypress_write(struct tty_struct *tty, struct usb_serial_port *port,
180 const unsigned char *buf, int count);
181static void cypress_send(struct usb_serial_port *port);
182static int cypress_write_room(struct tty_struct *tty);
183static int cypress_ioctl(struct tty_struct *tty, struct file *file,
184 unsigned int cmd, unsigned long arg);
185static void cypress_set_termios(struct tty_struct *tty,
186 struct usb_serial_port *port, struct ktermios *old);
187static int cypress_tiocmget(struct tty_struct *tty, struct file *file);
188static int cypress_tiocmset(struct tty_struct *tty, struct file *file,
189 unsigned int set, unsigned int clear);
190static int cypress_chars_in_buffer(struct tty_struct *tty);
191static void cypress_throttle(struct tty_struct *tty);
192static void cypress_unthrottle(struct tty_struct *tty);
193static void cypress_set_dead(struct usb_serial_port *port);
194static void cypress_read_int_callback(struct urb *urb);
195static void cypress_write_int_callback(struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196/* write buffer functions */
197static struct cypress_buf *cypress_buf_alloc(unsigned int size);
Alan Cox813a2242008-07-22 11:10:36 +0100198static void cypress_buf_free(struct cypress_buf *cb);
199static void cypress_buf_clear(struct cypress_buf *cb);
200static unsigned int cypress_buf_data_avail(struct cypress_buf *cb);
201static unsigned int cypress_buf_space_avail(struct cypress_buf *cb);
202static unsigned int cypress_buf_put(struct cypress_buf *cb,
203 const char *buf, unsigned int count);
204static unsigned int cypress_buf_get(struct cypress_buf *cb,
205 char *buf, unsigned int count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700208static struct usb_serial_driver cypress_earthmate_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700209 .driver = {
210 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700211 .name = "earthmate",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700212 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700213 .description = "DeLorme Earthmate USB",
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100214 .usb_driver = &cypress_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 .id_table = id_table_earthmate,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 .num_ports = 1,
217 .attach = cypress_earthmate_startup,
218 .shutdown = cypress_shutdown,
219 .open = cypress_open,
220 .close = cypress_close,
221 .write = cypress_write,
222 .write_room = cypress_write_room,
223 .ioctl = cypress_ioctl,
224 .set_termios = cypress_set_termios,
225 .tiocmget = cypress_tiocmget,
226 .tiocmset = cypress_tiocmset,
227 .chars_in_buffer = cypress_chars_in_buffer,
228 .throttle = cypress_throttle,
229 .unthrottle = cypress_unthrottle,
230 .read_int_callback = cypress_read_int_callback,
231 .write_int_callback = cypress_write_int_callback,
232};
233
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700234static struct usb_serial_driver cypress_hidcom_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700235 .driver = {
236 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700237 .name = "cyphidcom",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700238 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700239 .description = "HID->COM RS232 Adapter",
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100240 .usb_driver = &cypress_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 .id_table = id_table_cyphidcomrs232,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 .num_ports = 1,
243 .attach = cypress_hidcom_startup,
244 .shutdown = cypress_shutdown,
245 .open = cypress_open,
246 .close = cypress_close,
247 .write = cypress_write,
248 .write_room = cypress_write_room,
249 .ioctl = cypress_ioctl,
250 .set_termios = cypress_set_termios,
251 .tiocmget = cypress_tiocmget,
252 .tiocmset = cypress_tiocmset,
253 .chars_in_buffer = cypress_chars_in_buffer,
254 .throttle = cypress_throttle,
255 .unthrottle = cypress_unthrottle,
256 .read_int_callback = cypress_read_int_callback,
257 .write_int_callback = cypress_write_int_callback,
258};
259
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600260static struct usb_serial_driver cypress_ca42v2_device = {
261 .driver = {
262 .owner = THIS_MODULE,
Alan Cox813a2242008-07-22 11:10:36 +0100263 .name = "nokiaca42v2",
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600264 },
265 .description = "Nokia CA-42 V2 Adapter",
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100266 .usb_driver = &cypress_driver,
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600267 .id_table = id_table_nokiaca42v2,
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600268 .num_ports = 1,
269 .attach = cypress_ca42v2_startup,
270 .shutdown = cypress_shutdown,
271 .open = cypress_open,
272 .close = cypress_close,
273 .write = cypress_write,
274 .write_room = cypress_write_room,
275 .ioctl = cypress_ioctl,
276 .set_termios = cypress_set_termios,
277 .tiocmget = cypress_tiocmget,
278 .tiocmset = cypress_tiocmset,
279 .chars_in_buffer = cypress_chars_in_buffer,
280 .throttle = cypress_throttle,
281 .unthrottle = cypress_unthrottle,
282 .read_int_callback = cypress_read_int_callback,
283 .write_int_callback = cypress_write_int_callback,
284};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286/*****************************************************************************
287 * Cypress serial helper functions
288 *****************************************************************************/
289
290
Alan Cox8873aaa2008-03-10 21:59:28 +0000291static int analyze_baud_rate(struct usb_serial_port *port, speed_t new_rate)
Mike Isely92983c22008-02-10 20:23:32 -0600292{
Mike Isely92983c22008-02-10 20:23:32 -0600293 struct cypress_private *priv;
294 priv = usb_get_serial_port_data(port);
295
296 /*
297 * The general purpose firmware for the Cypress M8 allows for
298 * a maximum speed of 57600bps (I have no idea whether DeLorme
299 * chose to use the general purpose firmware or not), if you
300 * need to modify this speed setting for your own project
301 * please add your own chiptype and modify the code likewise.
302 * The Cypress HID->COM device will work successfully up to
303 * 115200bps (but the actual throughput is around 3kBps).
304 */
Mike Isely92983c22008-02-10 20:23:32 -0600305 if (port->serial->dev->speed == USB_SPEED_LOW) {
306 /*
307 * Mike Isely <isely@pobox.com> 2-Feb-2008: The
308 * Cypress app note that describes this mechanism
309 * states the the low-speed part can't handle more
310 * than 800 bytes/sec, in which case 4800 baud is the
311 * safest speed for a part like that.
312 */
313 if (new_rate > 4800) {
314 dbg("%s - failed setting baud rate, device incapable "
315 "speed %d", __func__, new_rate);
316 return -1;
317 }
318 }
319 switch (priv->chiptype) {
320 case CT_EARTHMATE:
321 if (new_rate <= 600) {
322 /* 300 and 600 baud rates are supported under
323 * the generic firmware, but are not used with
324 * NMEA and SiRF protocols */
325 dbg("%s - failed setting baud rate, unsupported speed "
326 "of %d on Earthmate GPS", __func__, new_rate);
327 return -1;
328 }
329 break;
330 default:
331 break;
332 }
333 return new_rate;
334}
335
336
Steven Cole093cf722005-05-03 19:07:24 -0600337/* This function can either set or retrieve the current serial line settings */
Alan Cox813a2242008-07-22 11:10:36 +0100338static int cypress_serial_control(struct tty_struct *tty,
Alan Cox95da3102008-07-22 11:09:07 +0100339 struct usb_serial_port *port, speed_t baud_rate, int data_bits,
340 int stop_bits, int parity_enable, int parity_type, int reset,
341 int cypress_request_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500343 int new_baudrate = 0, retval = 0, tries = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 struct cypress_private *priv;
Mike Isely93075542008-02-10 20:23:14 -0600345 __u8 feature_buffer[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 unsigned long flags;
347
Harvey Harrison441b62c2008-03-03 16:08:34 -0800348 dbg("%s", __func__);
Alan Cox813a2242008-07-22 11:10:36 +0100349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 priv = usb_get_serial_port_data(port);
351
Mike Isely78aef512006-08-29 22:07:11 -0500352 if (!priv->comm_is_ok)
353 return -ENODEV;
354
Alan Cox813a2242008-07-22 11:10:36 +0100355 switch (cypress_request_type) {
356 case CYPRESS_SET_CONFIG:
357 new_baudrate = priv->baud_rate;
358 /* 0 means 'Hang up' so doesn't change the true bit rate */
359 if (baud_rate == 0)
Mike Isely92983c22008-02-10 20:23:32 -0600360 new_baudrate = priv->baud_rate;
Alan Cox813a2242008-07-22 11:10:36 +0100361 /* Change of speed ? */
362 else if (baud_rate != priv->baud_rate) {
363 dbg("%s - baud rate is changing", __func__);
364 retval = analyze_baud_rate(port, baud_rate);
365 if (retval >= 0) {
366 new_baudrate = retval;
367 dbg("%s - New baud rate set to %d",
368 __func__, new_baudrate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 }
Alan Cox813a2242008-07-22 11:10:36 +0100370 }
371 dbg("%s - baud rate is being sent as %d",
372 __func__, new_baudrate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Alan Cox813a2242008-07-22 11:10:36 +0100374 memset(feature_buffer, 0, sizeof(feature_buffer));
375 /* fill the feature_buffer with new configuration */
376 *((u_int32_t *)feature_buffer) = new_baudrate;
377 feature_buffer[4] |= data_bits; /* assign data bits in 2 bit space ( max 3 ) */
378 /* 1 bit gap */
379 feature_buffer[4] |= (stop_bits << 3); /* assign stop bits in 1 bit space */
380 feature_buffer[4] |= (parity_enable << 4); /* assign parity flag in 1 bit space */
381 feature_buffer[4] |= (parity_type << 5); /* assign parity type in 1 bit space */
382 /* 1 bit gap */
383 feature_buffer[4] |= (reset << 7); /* assign reset at end of byte, 1 bit space */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Alan Cox813a2242008-07-22 11:10:36 +0100385 dbg("%s - device is being sent this feature report:",
386 __func__);
387 dbg("%s - %02X - %02X - %02X - %02X - %02X", __func__,
388 feature_buffer[0], feature_buffer[1],
389 feature_buffer[2], feature_buffer[3],
390 feature_buffer[4]);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500391
Alan Cox813a2242008-07-22 11:10:36 +0100392 do {
393 retval = usb_control_msg(port->serial->dev,
394 usb_sndctrlpipe(port->serial->dev, 0),
395 HID_REQ_SET_REPORT,
396 USB_DIR_OUT | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
397 0x0300, 0, feature_buffer,
398 sizeof(feature_buffer), 500);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500399
Alan Cox813a2242008-07-22 11:10:36 +0100400 if (tries++ >= 3)
401 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Alan Cox813a2242008-07-22 11:10:36 +0100403 } while (retval != sizeof(feature_buffer) &&
404 retval != -ENODEV);
Mike Isely93075542008-02-10 20:23:14 -0600405
Alan Cox813a2242008-07-22 11:10:36 +0100406 if (retval != sizeof(feature_buffer)) {
407 err("%s - failed sending serial line settings - %d",
408 __func__, retval);
409 cypress_set_dead(port);
410 } else {
411 spin_lock_irqsave(&priv->lock, flags);
412 priv->baud_rate = new_baudrate;
413 priv->current_config = feature_buffer[4];
414 spin_unlock_irqrestore(&priv->lock, flags);
415 /* If we asked for a speed change encode it */
416 if (baud_rate)
417 tty_encode_baud_rate(tty,
418 new_baudrate, new_baudrate);
419 }
420 break;
421 case CYPRESS_GET_CONFIG:
422 if (priv->get_cfg_unsafe) {
423 /* Not implemented for this device,
424 and if we try to do it we're likely
425 to crash the hardware. */
426 return -ENOTTY;
427 }
428 dbg("%s - retreiving serial line settings", __func__);
429 /* set initial values in feature buffer */
430 memset(feature_buffer, 0, sizeof(feature_buffer));
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500431
Alan Cox813a2242008-07-22 11:10:36 +0100432 do {
433 retval = usb_control_msg(port->serial->dev,
434 usb_rcvctrlpipe(port->serial->dev, 0),
435 HID_REQ_GET_REPORT,
436 USB_DIR_IN | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
437 0x0300, 0, feature_buffer,
438 sizeof(feature_buffer), 500);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500439
Alan Cox813a2242008-07-22 11:10:36 +0100440 if (tries++ >= 3)
441 break;
442 } while (retval != sizeof(feature_buffer)
443 && retval != -ENODEV);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500444
Alan Cox813a2242008-07-22 11:10:36 +0100445 if (retval != sizeof(feature_buffer)) {
446 err("%s - failed to retrieve serial line settings - %d", __func__, retval);
447 cypress_set_dead(port);
448 return retval;
449 } else {
450 spin_lock_irqsave(&priv->lock, flags);
451 /* store the config in one byte, and later
452 use bit masks to check values */
453 priv->current_config = feature_buffer[4];
454 priv->baud_rate = *((u_int32_t *)feature_buffer);
455 spin_unlock_irqrestore(&priv->lock, flags);
456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 }
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500458 spin_lock_irqsave(&priv->lock, flags);
459 ++priv->cmd_count;
460 spin_unlock_irqrestore(&priv->lock, flags);
461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 return retval;
463} /* cypress_serial_control */
464
465
Mike Isely78aef512006-08-29 22:07:11 -0500466static void cypress_set_dead(struct usb_serial_port *port)
467{
468 struct cypress_private *priv = usb_get_serial_port_data(port);
469 unsigned long flags;
470
471 spin_lock_irqsave(&priv->lock, flags);
472 if (!priv->comm_is_ok) {
473 spin_unlock_irqrestore(&priv->lock, flags);
474 return;
475 }
476 priv->comm_is_ok = 0;
477 spin_unlock_irqrestore(&priv->lock, flags);
478
479 err("cypress_m8 suspending failing port %d - interval might be too short",
480 port->number);
481}
482
483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484/*****************************************************************************
485 * Cypress serial driver functions
486 *****************************************************************************/
487
488
Alan Cox813a2242008-07-22 11:10:36 +0100489static int generic_startup(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
491 struct cypress_private *priv;
Mike Isely0257fa92006-08-29 22:06:59 -0500492 struct usb_serial_port *port = serial->port[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Harvey Harrison441b62c2008-03-03 16:08:34 -0800494 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Alan Cox813a2242008-07-22 11:10:36 +0100496 priv = kzalloc(sizeof(struct cypress_private), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 if (!priv)
498 return -ENOMEM;
499
Mike Isely78aef512006-08-29 22:07:11 -0500500 priv->comm_is_ok = !0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 spin_lock_init(&priv->lock);
502 priv->buf = cypress_buf_alloc(CYPRESS_BUF_SIZE);
503 if (priv->buf == NULL) {
504 kfree(priv);
505 return -ENOMEM;
506 }
507 init_waitqueue_head(&priv->delta_msr_wait);
Alan Cox813a2242008-07-22 11:10:36 +0100508
509 usb_reset_configuration(serial->dev);
510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 priv->cmd_ctrl = 0;
512 priv->line_control = 0;
513 priv->termios_initialized = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 priv->rx_flags = 0;
Mike Isely3416eaa2008-02-10 20:23:19 -0600515 /* Default packet format setting is determined by packet size.
516 Anything with a size larger then 9 must have a separate
517 count field since the 3 bit count field is otherwise too
518 small. Otherwise we can use the slightly more compact
519 format. This is in accordance with the cypress_m8 serial
520 converter app note. */
Alan Cox813a2242008-07-22 11:10:36 +0100521 if (port->interrupt_out_size > 9)
Mike Isely3416eaa2008-02-10 20:23:19 -0600522 priv->pkt_fmt = packet_format_1;
Alan Cox813a2242008-07-22 11:10:36 +0100523 else
Mike Isely3416eaa2008-02-10 20:23:19 -0600524 priv->pkt_fmt = packet_format_2;
Alan Cox813a2242008-07-22 11:10:36 +0100525
Mike Isely0257fa92006-08-29 22:06:59 -0500526 if (interval > 0) {
527 priv->write_urb_interval = interval;
528 priv->read_urb_interval = interval;
529 dbg("%s - port %d read & write intervals forced to %d",
Alan Cox813a2242008-07-22 11:10:36 +0100530 __func__, port->number, interval);
Mike Isely0257fa92006-08-29 22:06:59 -0500531 } else {
532 priv->write_urb_interval = port->interrupt_out_urb->interval;
533 priv->read_urb_interval = port->interrupt_in_urb->interval;
534 dbg("%s - port %d intervals: read=%d write=%d",
Alan Cox813a2242008-07-22 11:10:36 +0100535 __func__, port->number,
536 priv->read_urb_interval, priv->write_urb_interval);
Mike Isely0257fa92006-08-29 22:06:59 -0500537 }
538 usb_set_serial_port_data(port, priv);
Alan Cox813a2242008-07-22 11:10:36 +0100539
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500540 return 0;
541}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
543
Alan Cox813a2242008-07-22 11:10:36 +0100544static int cypress_earthmate_startup(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
546 struct cypress_private *priv;
Mike Isely3d6aa322008-02-10 20:23:24 -0600547 struct usb_serial_port *port = serial->port[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Harvey Harrison441b62c2008-03-03 16:08:34 -0800549 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551 if (generic_startup(serial)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800552 dbg("%s - Failed setting up port %d", __func__,
Mike Isely3d6aa322008-02-10 20:23:24 -0600553 port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 return 1;
555 }
556
Mike Isely3d6aa322008-02-10 20:23:24 -0600557 priv = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 priv->chiptype = CT_EARTHMATE;
Mike Isely3416eaa2008-02-10 20:23:19 -0600559 /* All Earthmate devices use the separated-count packet
560 format! Idiotic. */
561 priv->pkt_fmt = packet_format_1;
Alan Cox813a2242008-07-22 11:10:36 +0100562 if (serial->dev->descriptor.idProduct !=
563 cpu_to_le16(PRODUCT_ID_EARTHMATEUSB)) {
Mike Isely3d6aa322008-02-10 20:23:24 -0600564 /* The old original USB Earthmate seemed able to
565 handle GET_CONFIG requests; everything they've
566 produced since that time crashes if this command is
567 attempted :-( */
568 dbg("%s - Marking this device as unsafe for GET_CONFIG "
569 "commands", __func__);
570 priv->get_cfg_unsafe = !0;
571 }
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500572
573 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574} /* cypress_earthmate_startup */
575
576
Alan Cox813a2242008-07-22 11:10:36 +0100577static int cypress_hidcom_startup(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
579 struct cypress_private *priv;
580
Harvey Harrison441b62c2008-03-03 16:08:34 -0800581 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
583 if (generic_startup(serial)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800584 dbg("%s - Failed setting up port %d", __func__,
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500585 serial->port[0]->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 return 1;
587 }
588
589 priv = usb_get_serial_port_data(serial->port[0]);
590 priv->chiptype = CT_CYPHIDCOM;
Alan Cox813a2242008-07-22 11:10:36 +0100591
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500592 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593} /* cypress_hidcom_startup */
594
595
Alan Cox813a2242008-07-22 11:10:36 +0100596static int cypress_ca42v2_startup(struct usb_serial *serial)
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600597{
598 struct cypress_private *priv;
599
Harvey Harrison441b62c2008-03-03 16:08:34 -0800600 dbg("%s", __func__);
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600601
602 if (generic_startup(serial)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800603 dbg("%s - Failed setting up port %d", __func__,
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600604 serial->port[0]->number);
605 return 1;
606 }
607
608 priv = usb_get_serial_port_data(serial->port[0]);
609 priv->chiptype = CT_CA42V2;
610
611 return 0;
612} /* cypress_ca42v2_startup */
613
614
Alan Cox813a2242008-07-22 11:10:36 +0100615static void cypress_shutdown(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
617 struct cypress_private *priv;
618
Alan Cox813a2242008-07-22 11:10:36 +0100619 dbg("%s - port %d", __func__, serial->port[0]->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
621 /* all open ports are closed at this point */
622
623 priv = usb_get_serial_port_data(serial->port[0]);
624
625 if (priv) {
626 cypress_buf_free(priv->buf);
627 kfree(priv);
628 usb_set_serial_port_data(serial->port[0], NULL);
629 }
630}
631
632
Alan Cox95da3102008-07-22 11:09:07 +0100633static int cypress_open(struct tty_struct *tty,
634 struct usb_serial_port *port, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
636 struct cypress_private *priv = usb_get_serial_port_data(port);
637 struct usb_serial *serial = port->serial;
638 unsigned long flags;
639 int result = 0;
640
Harvey Harrison441b62c2008-03-03 16:08:34 -0800641 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Mike Isely78aef512006-08-29 22:07:11 -0500643 if (!priv->comm_is_ok)
644 return -EIO;
645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 /* clear halts before open */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 usb_clear_halt(serial->dev, 0x81);
648 usb_clear_halt(serial->dev, 0x02);
649
650 spin_lock_irqsave(&priv->lock, flags);
651 /* reset read/write statistics */
652 priv->bytes_in = 0;
653 priv->bytes_out = 0;
654 priv->cmd_count = 0;
655 priv->rx_flags = 0;
656 spin_unlock_irqrestore(&priv->lock, flags);
657
658 /* setting to zero could cause data loss */
Alan Cox95da3102008-07-22 11:09:07 +0100659 if (tty)
660 tty->low_latency = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662 /* raise both lines and set termios */
663 spin_lock_irqsave(&priv->lock, flags);
664 priv->line_control = CONTROL_DTR | CONTROL_RTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 priv->cmd_ctrl = 1;
666 spin_unlock_irqrestore(&priv->lock, flags);
Alan Cox95da3102008-07-22 11:09:07 +0100667 result = cypress_write(tty, port, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
669 if (result) {
Alan Cox813a2242008-07-22 11:10:36 +0100670 dev_err(&port->dev,
671 "%s - failed setting the control lines - error %d\n",
672 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 return result;
674 } else
Harvey Harrison441b62c2008-03-03 16:08:34 -0800675 dbg("%s - success setting the control lines", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Alan Cox95da3102008-07-22 11:09:07 +0100677 if (tty)
678 cypress_set_termios(tty, port, &priv->tmp_termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
680 /* setup the port and start reading from the device */
Alan Cox813a2242008-07-22 11:10:36 +0100681 if (!port->interrupt_in_urb) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800682 err("%s - interrupt_in_urb is empty!", __func__);
Alan Cox813a2242008-07-22 11:10:36 +0100683 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 }
685
686 usb_fill_int_urb(port->interrupt_in_urb, serial->dev,
687 usb_rcvintpipe(serial->dev, port->interrupt_in_endpointAddress),
Alan Cox813a2242008-07-22 11:10:36 +0100688 port->interrupt_in_urb->transfer_buffer,
689 port->interrupt_in_urb->transfer_buffer_length,
Mike Isely0257fa92006-08-29 22:06:59 -0500690 cypress_read_int_callback, port, priv->read_urb_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
692
Alan Cox813a2242008-07-22 11:10:36 +0100693 if (result) {
694 dev_err(&port->dev,
695 "%s - failed submitting read urb, error %d\n",
696 __func__, result);
Mike Isely78aef512006-08-29 22:07:11 -0500697 cypress_set_dead(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 }
699
700 return result;
701} /* cypress_open */
702
703
Alan Cox95da3102008-07-22 11:09:07 +0100704static void cypress_close(struct tty_struct *tty,
Alan Cox813a2242008-07-22 11:10:36 +0100705 struct usb_serial_port *port, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706{
707 struct cypress_private *priv = usb_get_serial_port_data(port);
708 unsigned int c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 int bps;
710 long timeout;
711 wait_queue_t wait;
712
Harvey Harrison441b62c2008-03-03 16:08:34 -0800713 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
715 /* wait for data to drain from buffer */
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100716 spin_lock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 timeout = CYPRESS_CLOSING_WAIT;
718 init_waitqueue_entry(&wait, current);
Alan Cox95da3102008-07-22 11:09:07 +0100719 add_wait_queue(&tty->write_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 for (;;) {
721 set_current_state(TASK_INTERRUPTIBLE);
722 if (cypress_buf_data_avail(priv->buf) == 0
723 || timeout == 0 || signal_pending(current)
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100724 /* without mutex, allowed due to harmless failure mode */
725 || port->serial->disconnected)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 break;
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100727 spin_unlock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 timeout = schedule_timeout(timeout);
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100729 spin_lock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 }
731 set_current_state(TASK_RUNNING);
Alan Cox95da3102008-07-22 11:09:07 +0100732 remove_wait_queue(&tty->write_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 /* clear out any remaining data in the buffer */
734 cypress_buf_clear(priv->buf);
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100735 spin_unlock_irq(&priv->lock);
736
737 /* writing is potentially harmful, lock must be taken */
738 mutex_lock(&port->serial->disc_mutex);
739 if (port->serial->disconnected) {
740 mutex_unlock(&port->serial->disc_mutex);
741 return;
742 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 /* wait for characters to drain from device */
Alan Cox95da3102008-07-22 11:09:07 +0100744 if (tty) {
745 bps = tty_get_baud_rate(tty);
746 if (bps > 1200)
Alan Cox813a2242008-07-22 11:10:36 +0100747 timeout = max((HZ * 2560) / bps, HZ / 10);
Alan Cox95da3102008-07-22 11:09:07 +0100748 else
Alan Cox813a2242008-07-22 11:10:36 +0100749 timeout = 2 * HZ;
Alan Cox95da3102008-07-22 11:09:07 +0100750 schedule_timeout_interruptible(timeout);
751 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Harvey Harrison441b62c2008-03-03 16:08:34 -0800753 dbg("%s - stopping urbs", __func__);
Alan Cox813a2242008-07-22 11:10:36 +0100754 usb_kill_urb(port->interrupt_in_urb);
755 usb_kill_urb(port->interrupt_out_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
Alan Cox95da3102008-07-22 11:09:07 +0100757 if (tty) {
758 c_cflag = tty->termios->c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 if (c_cflag & HUPCL) {
760 /* drop dtr and rts */
761 priv = usb_get_serial_port_data(port);
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100762 spin_lock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 priv->line_control = 0;
764 priv->cmd_ctrl = 1;
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100765 spin_unlock_irq(&priv->lock);
Alan Cox95da3102008-07-22 11:09:07 +0100766 cypress_write(tty, port, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 }
768 }
769
770 if (stats)
Alan Cox813a2242008-07-22 11:10:36 +0100771 dev_info(&port->dev, "Statistics: %d Bytes In | %d Bytes Out | %d Commands Issued\n",
772 priv->bytes_in, priv->bytes_out, priv->cmd_count);
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100773 mutex_unlock(&port->serial->disc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774} /* cypress_close */
775
776
Alan Cox95da3102008-07-22 11:09:07 +0100777static int cypress_write(struct tty_struct *tty, struct usb_serial_port *port,
778 const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
780 struct cypress_private *priv = usb_get_serial_port_data(port);
781 unsigned long flags;
Alan Cox813a2242008-07-22 11:10:36 +0100782
Harvey Harrison441b62c2008-03-03 16:08:34 -0800783 dbg("%s - port %d, %d bytes", __func__, port->number, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
785 /* line control commands, which need to be executed immediately,
786 are not put into the buffer for obvious reasons.
787 */
788 if (priv->cmd_ctrl) {
789 count = 0;
790 goto finish;
791 }
Alan Cox813a2242008-07-22 11:10:36 +0100792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 if (!count)
794 return count;
Alan Cox813a2242008-07-22 11:10:36 +0100795
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 spin_lock_irqsave(&priv->lock, flags);
797 count = cypress_buf_put(priv->buf, buf, count);
798 spin_unlock_irqrestore(&priv->lock, flags);
799
800finish:
801 cypress_send(port);
802
803 return count;
804} /* cypress_write */
805
806
807static void cypress_send(struct usb_serial_port *port)
808{
809 int count = 0, result, offset, actual_size;
810 struct cypress_private *priv = usb_get_serial_port_data(port);
811 unsigned long flags;
Alan Cox813a2242008-07-22 11:10:36 +0100812
Mike Isely78aef512006-08-29 22:07:11 -0500813 if (!priv->comm_is_ok)
814 return;
815
Harvey Harrison441b62c2008-03-03 16:08:34 -0800816 dbg("%s - port %d", __func__, port->number);
Alan Cox813a2242008-07-22 11:10:36 +0100817 dbg("%s - interrupt out size is %d", __func__,
818 port->interrupt_out_size);
819
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 spin_lock_irqsave(&priv->lock, flags);
821 if (priv->write_urb_in_use) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800822 dbg("%s - can't write, urb in use", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 spin_unlock_irqrestore(&priv->lock, flags);
824 return;
825 }
826 spin_unlock_irqrestore(&priv->lock, flags);
827
828 /* clear buffer */
Alan Cox813a2242008-07-22 11:10:36 +0100829 memset(port->interrupt_out_urb->transfer_buffer, 0,
830 port->interrupt_out_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
832 spin_lock_irqsave(&priv->lock, flags);
Mike Isely3416eaa2008-02-10 20:23:19 -0600833 switch (priv->pkt_fmt) {
834 default:
835 case packet_format_1:
836 /* this is for the CY7C64013... */
837 offset = 2;
838 port->interrupt_out_buffer[0] = priv->line_control;
839 break;
840 case packet_format_2:
841 /* this is for the CY7C63743... */
842 offset = 1;
843 port->interrupt_out_buffer[0] = priv->line_control;
844 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 }
846
847 if (priv->line_control & CONTROL_RESET)
848 priv->line_control &= ~CONTROL_RESET;
849
850 if (priv->cmd_ctrl) {
851 priv->cmd_count++;
Harvey Harrison441b62c2008-03-03 16:08:34 -0800852 dbg("%s - line control command being issued", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 spin_unlock_irqrestore(&priv->lock, flags);
854 goto send;
855 } else
856 spin_unlock_irqrestore(&priv->lock, flags);
857
858 count = cypress_buf_get(priv->buf, &port->interrupt_out_buffer[offset],
859 port->interrupt_out_size-offset);
860
Alan Cox813a2242008-07-22 11:10:36 +0100861 if (count == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
Mike Isely3416eaa2008-02-10 20:23:19 -0600864 switch (priv->pkt_fmt) {
865 default:
866 case packet_format_1:
867 port->interrupt_out_buffer[1] = count;
868 break;
869 case packet_format_2:
870 port->interrupt_out_buffer[0] |= count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 }
872
Harvey Harrison441b62c2008-03-03 16:08:34 -0800873 dbg("%s - count is %d", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
875send:
876 spin_lock_irqsave(&priv->lock, flags);
877 priv->write_urb_in_use = 1;
878 spin_unlock_irqrestore(&priv->lock, flags);
879
880 if (priv->cmd_ctrl)
881 actual_size = 1;
882 else
Mike Isely3416eaa2008-02-10 20:23:19 -0600883 actual_size = count +
884 (priv->pkt_fmt == packet_format_1 ? 2 : 1);
885
Alan Cox813a2242008-07-22 11:10:36 +0100886 usb_serial_debug_data(debug, &port->dev, __func__,
887 port->interrupt_out_size,
888 port->interrupt_out_urb->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
Mike Isely9aa8dae2006-08-29 22:07:04 -0500890 usb_fill_int_urb(port->interrupt_out_urb, port->serial->dev,
891 usb_sndintpipe(port->serial->dev, port->interrupt_out_endpointAddress),
892 port->interrupt_out_buffer, port->interrupt_out_size,
893 cypress_write_int_callback, port, priv->write_urb_interval);
Alan Cox813a2242008-07-22 11:10:36 +0100894 result = usb_submit_urb(port->interrupt_out_urb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 if (result) {
Alan Cox813a2242008-07-22 11:10:36 +0100896 dev_err(&port->dev,
897 "%s - failed submitting write urb, error %d\n",
898 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 priv->write_urb_in_use = 0;
Mike Isely78aef512006-08-29 22:07:11 -0500900 cypress_set_dead(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 }
902
903 spin_lock_irqsave(&priv->lock, flags);
Alan Cox813a2242008-07-22 11:10:36 +0100904 if (priv->cmd_ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 priv->cmd_ctrl = 0;
Alan Cox813a2242008-07-22 11:10:36 +0100906
907 /* do not count the line control and size bytes */
908 priv->bytes_out += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 spin_unlock_irqrestore(&priv->lock, flags);
910
Pete Zaitcevcf2c7482006-05-22 21:58:49 -0700911 usb_serial_port_softint(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912} /* cypress_send */
913
914
915/* returns how much space is available in the soft buffer */
Alan Cox95da3102008-07-22 11:09:07 +0100916static int cypress_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
Alan Cox95da3102008-07-22 11:09:07 +0100918 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 struct cypress_private *priv = usb_get_serial_port_data(port);
920 int room = 0;
921 unsigned long flags;
922
Harvey Harrison441b62c2008-03-03 16:08:34 -0800923 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
925 spin_lock_irqsave(&priv->lock, flags);
926 room = cypress_buf_space_avail(priv->buf);
927 spin_unlock_irqrestore(&priv->lock, flags);
928
Harvey Harrison441b62c2008-03-03 16:08:34 -0800929 dbg("%s - returns %d", __func__, room);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 return room;
931}
932
933
Alan Cox95da3102008-07-22 11:09:07 +0100934static int cypress_tiocmget(struct tty_struct *tty, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935{
Alan Cox95da3102008-07-22 11:09:07 +0100936 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 struct cypress_private *priv = usb_get_serial_port_data(port);
938 __u8 status, control;
939 unsigned int result = 0;
940 unsigned long flags;
Alan Cox813a2242008-07-22 11:10:36 +0100941
Harvey Harrison441b62c2008-03-03 16:08:34 -0800942 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
944 spin_lock_irqsave(&priv->lock, flags);
945 control = priv->line_control;
946 status = priv->current_status;
947 spin_unlock_irqrestore(&priv->lock, flags);
948
949 result = ((control & CONTROL_DTR) ? TIOCM_DTR : 0)
950 | ((control & CONTROL_RTS) ? TIOCM_RTS : 0)
951 | ((status & UART_CTS) ? TIOCM_CTS : 0)
952 | ((status & UART_DSR) ? TIOCM_DSR : 0)
953 | ((status & UART_RI) ? TIOCM_RI : 0)
954 | ((status & UART_CD) ? TIOCM_CD : 0);
955
Harvey Harrison441b62c2008-03-03 16:08:34 -0800956 dbg("%s - result = %x", __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
958 return result;
959}
960
961
Alan Cox95da3102008-07-22 11:09:07 +0100962static int cypress_tiocmset(struct tty_struct *tty, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 unsigned int set, unsigned int clear)
964{
Alan Cox95da3102008-07-22 11:09:07 +0100965 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 struct cypress_private *priv = usb_get_serial_port_data(port);
967 unsigned long flags;
Alan Cox813a2242008-07-22 11:10:36 +0100968
Harvey Harrison441b62c2008-03-03 16:08:34 -0800969 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
971 spin_lock_irqsave(&priv->lock, flags);
972 if (set & TIOCM_RTS)
973 priv->line_control |= CONTROL_RTS;
974 if (set & TIOCM_DTR)
975 priv->line_control |= CONTROL_DTR;
976 if (clear & TIOCM_RTS)
977 priv->line_control &= ~CONTROL_RTS;
978 if (clear & TIOCM_DTR)
979 priv->line_control &= ~CONTROL_DTR;
Alan Cox8873aaa2008-03-10 21:59:28 +0000980 priv->cmd_ctrl = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 spin_unlock_irqrestore(&priv->lock, flags);
982
Alan Cox95da3102008-07-22 11:09:07 +0100983 return cypress_write(tty, port, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984}
985
986
Alan Cox813a2242008-07-22 11:10:36 +0100987static int cypress_ioctl(struct tty_struct *tty, struct file *file,
Alan Cox95da3102008-07-22 11:09:07 +0100988 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989{
Alan Cox95da3102008-07-22 11:09:07 +0100990 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 struct cypress_private *priv = usb_get_serial_port_data(port);
992
Harvey Harrison441b62c2008-03-03 16:08:34 -0800993 dbg("%s - port %d, cmd 0x%.4x", __func__, port->number, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
995 switch (cmd) {
Alan Cox813a2242008-07-22 11:10:36 +0100996 /* This code comes from drivers/char/serial.c and ftdi_sio.c */
997 case TIOCMIWAIT:
998 while (priv != NULL) {
999 interruptible_sleep_on(&priv->delta_msr_wait);
1000 /* see if a signal did it */
1001 if (signal_pending(current))
1002 return -ERESTARTSYS;
1003 else {
1004 char diff = priv->diff_status;
1005 if (diff == 0)
1006 return -EIO; /* no change => error */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
Alan Cox813a2242008-07-22 11:10:36 +01001008 /* consume all events */
1009 priv->diff_status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
Alan Cox813a2242008-07-22 11:10:36 +01001011 /* return 0 if caller wanted to know about
1012 these bits */
1013 if (((arg & TIOCM_RNG) && (diff & UART_RI)) ||
1014 ((arg & TIOCM_DSR) && (diff & UART_DSR)) ||
1015 ((arg & TIOCM_CD) && (diff & UART_CD)) ||
1016 ((arg & TIOCM_CTS) && (diff & UART_CTS)))
1017 return 0;
1018 /* otherwise caller can't care less about what
1019 * happened, and so we continue to wait for
1020 * more events.
1021 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 }
Alan Cox813a2242008-07-22 11:10:36 +01001023 }
1024 return 0;
1025 default:
1026 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 }
Harvey Harrison441b62c2008-03-03 16:08:34 -08001028 dbg("%s - arg not supported - it was 0x%04x - check include/asm/ioctls.h", __func__, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 return -ENOIOCTLCMD;
1030} /* cypress_ioctl */
1031
1032
Alan Cox95da3102008-07-22 11:09:07 +01001033static void cypress_set_termios(struct tty_struct *tty,
1034 struct usb_serial_port *port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035{
1036 struct cypress_private *priv = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 int data_bits, stop_bits, parity_type, parity_enable;
Alan Cox8873aaa2008-03-10 21:59:28 +00001038 unsigned cflag, iflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 unsigned long flags;
1040 __u8 oldlines;
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001041 int linechange = 0;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001042
Harvey Harrison441b62c2008-03-03 16:08:34 -08001043 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 spin_lock_irqsave(&priv->lock, flags);
1046 if (!priv->termios_initialized) {
1047 if (priv->chiptype == CT_EARTHMATE) {
1048 *(tty->termios) = tty_std_termios;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001049 tty->termios->c_cflag = B4800 | CS8 | CREAD | HUPCL |
1050 CLOCAL;
Alan Cox8873aaa2008-03-10 21:59:28 +00001051 tty->termios->c_ispeed = 4800;
1052 tty->termios->c_ospeed = 4800;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 } else if (priv->chiptype == CT_CYPHIDCOM) {
1054 *(tty->termios) = tty_std_termios;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001055 tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
1056 CLOCAL;
Alan Cox8873aaa2008-03-10 21:59:28 +00001057 tty->termios->c_ispeed = 9600;
1058 tty->termios->c_ospeed = 9600;
Lonnie Mendeza5c44e22006-03-01 10:45:24 -06001059 } else if (priv->chiptype == CT_CA42V2) {
1060 *(tty->termios) = tty_std_termios;
1061 tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
1062 CLOCAL;
Alan Cox8873aaa2008-03-10 21:59:28 +00001063 tty->termios->c_ispeed = 9600;
1064 tty->termios->c_ospeed = 9600;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 }
1066 priv->termios_initialized = 1;
1067 }
1068 spin_unlock_irqrestore(&priv->lock, flags);
1069
Alan Cox8873aaa2008-03-10 21:59:28 +00001070 /* Unsupported features need clearing */
1071 tty->termios->c_cflag &= ~(CMSPAR|CRTSCTS);
1072
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 cflag = tty->termios->c_cflag;
1074 iflag = tty->termios->c_iflag;
1075
1076 /* check if there are new settings */
1077 if (old_termios) {
Alan Cox8873aaa2008-03-10 21:59:28 +00001078 spin_lock_irqsave(&priv->lock, flags);
1079 priv->tmp_termios = *(tty->termios);
1080 spin_unlock_irqrestore(&priv->lock, flags);
1081 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
1083 /* set number of data bits, parity, stop bits */
1084 /* when parity is disabled the parity type bit is ignored */
1085
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001086 /* 1 means 2 stop bits, 0 means 1 stop bit */
1087 stop_bits = cflag & CSTOPB ? 1 : 0;
1088
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 if (cflag & PARENB) {
1090 parity_enable = 1;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001091 /* 1 means odd parity, 0 means even parity */
1092 parity_type = cflag & PARODD ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 } else
1094 parity_enable = parity_type = 0;
1095
Alan Cox77336822008-07-22 11:10:53 +01001096 switch (cflag & CSIZE) {
1097 case CS5:
1098 data_bits = 0;
1099 break;
1100 case CS6:
1101 data_bits = 1;
1102 break;
1103 case CS7:
1104 data_bits = 2;
1105 break;
1106 case CS8:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 data_bits = 3;
Alan Cox77336822008-07-22 11:10:53 +01001108 break;
1109 default:
1110 err("%s - CSIZE was set, but not CS5-CS8",
1111 __func__);
1112 data_bits = 3;
1113 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 spin_lock_irqsave(&priv->lock, flags);
1115 oldlines = priv->line_control;
1116 if ((cflag & CBAUD) == B0) {
1117 /* drop dtr and rts */
Harvey Harrison441b62c2008-03-03 16:08:34 -08001118 dbg("%s - dropping the lines, baud rate 0bps", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
Alan Cox8873aaa2008-03-10 21:59:28 +00001120 } else
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001121 priv->line_control = (CONTROL_DTR | CONTROL_RTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001124 dbg("%s - sending %d stop_bits, %d parity_enable, %d parity_type, "
Harvey Harrison441b62c2008-03-03 16:08:34 -08001125 "%d data_bits (+5)", __func__, stop_bits,
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001126 parity_enable, parity_type, data_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
Alan Cox813a2242008-07-22 11:10:36 +01001128 cypress_serial_control(tty, port, tty_get_baud_rate(tty),
1129 data_bits, stop_bits,
1130 parity_enable, parity_type,
1131 0, CYPRESS_SET_CONFIG);
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001132
1133 /* we perform a CYPRESS_GET_CONFIG so that the current settings are
1134 * filled into the private structure this should confirm that all is
1135 * working if it returns what we just set */
Alan Cox95da3102008-07-22 11:09:07 +01001136 cypress_serial_control(tty, port, 0, 0, 0, 0, 0, 0, CYPRESS_GET_CONFIG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001138 /* Here we can define custom tty settings for devices; the main tty
1139 * termios flag base comes from empeg.c */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001141 spin_lock_irqsave(&priv->lock, flags);
Alan Cox813a2242008-07-22 11:10:36 +01001142 if (priv->chiptype == CT_EARTHMATE && priv->baud_rate == 4800) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001143 dbg("Using custom termios settings for a baud rate of "
1144 "4800bps.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 /* define custom termios settings for NMEA protocol */
1146
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 tty->termios->c_iflag /* input modes - */
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001148 &= ~(IGNBRK /* disable ignore break */
1149 | BRKINT /* disable break causes interrupt */
1150 | PARMRK /* disable mark parity errors */
1151 | ISTRIP /* disable clear high bit of input char */
1152 | INLCR /* disable translate NL to CR */
1153 | IGNCR /* disable ignore CR */
1154 | ICRNL /* disable translate CR to NL */
1155 | IXON); /* disable enable XON/XOFF flow control */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001157 tty->termios->c_oflag /* output modes */
1158 &= ~OPOST; /* disable postprocess output char */
1159
1160 tty->termios->c_lflag /* line discipline modes */
1161 &= ~(ECHO /* disable echo input characters */
1162 | ECHONL /* disable echo new line */
1163 | ICANON /* disable erase, kill, werase, and rprnt
1164 special characters */
1165 | ISIG /* disable interrupt, quit, and suspend
1166 special characters */
1167 | IEXTEN); /* disable non-POSIX special characters */
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001168 } /* CT_CYPHIDCOM: Application should handle this for device */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 linechange = (priv->line_control != oldlines);
1171 spin_unlock_irqrestore(&priv->lock, flags);
1172
1173 /* if necessary, set lines */
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001174 if (linechange) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 priv->cmd_ctrl = 1;
Alan Cox95da3102008-07-22 11:09:07 +01001176 cypress_write(tty, port, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178} /* cypress_set_termios */
1179
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001180
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181/* returns amount of data still left in soft buffer */
Alan Cox95da3102008-07-22 11:09:07 +01001182static int cypress_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183{
Alan Cox95da3102008-07-22 11:09:07 +01001184 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 struct cypress_private *priv = usb_get_serial_port_data(port);
1186 int chars = 0;
1187 unsigned long flags;
1188
Harvey Harrison441b62c2008-03-03 16:08:34 -08001189 dbg("%s - port %d", __func__, port->number);
Alan Cox813a2242008-07-22 11:10:36 +01001190
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 spin_lock_irqsave(&priv->lock, flags);
1192 chars = cypress_buf_data_avail(priv->buf);
1193 spin_unlock_irqrestore(&priv->lock, flags);
1194
Harvey Harrison441b62c2008-03-03 16:08:34 -08001195 dbg("%s - returns %d", __func__, chars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 return chars;
1197}
1198
1199
Alan Cox95da3102008-07-22 11:09:07 +01001200static void cypress_throttle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201{
Alan Cox95da3102008-07-22 11:09:07 +01001202 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 struct cypress_private *priv = usb_get_serial_port_data(port);
1204 unsigned long flags;
1205
Harvey Harrison441b62c2008-03-03 16:08:34 -08001206 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
1208 spin_lock_irqsave(&priv->lock, flags);
1209 priv->rx_flags = THROTTLED;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001210 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211}
1212
1213
Alan Cox95da3102008-07-22 11:09:07 +01001214static void cypress_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215{
Alan Cox95da3102008-07-22 11:09:07 +01001216 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 struct cypress_private *priv = usb_get_serial_port_data(port);
1218 int actually_throttled, result;
1219 unsigned long flags;
1220
Harvey Harrison441b62c2008-03-03 16:08:34 -08001221 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
1223 spin_lock_irqsave(&priv->lock, flags);
1224 actually_throttled = priv->rx_flags & ACTUALLY_THROTTLED;
1225 priv->rx_flags = 0;
1226 spin_unlock_irqrestore(&priv->lock, flags);
1227
Mike Isely78aef512006-08-29 22:07:11 -05001228 if (!priv->comm_is_ok)
1229 return;
1230
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 if (actually_throttled) {
1232 port->interrupt_in_urb->dev = port->serial->dev;
1233
1234 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
Mike Isely78aef512006-08-29 22:07:11 -05001235 if (result) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001236 dev_err(&port->dev, "%s - failed submitting read urb, "
Harvey Harrison441b62c2008-03-03 16:08:34 -08001237 "error %d\n", __func__, result);
Mike Isely78aef512006-08-29 22:07:11 -05001238 cypress_set_dead(port);
1239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 }
1241}
1242
1243
David Howells7d12e782006-10-05 14:55:46 +01001244static void cypress_read_int_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245{
Ming Leicdc97792008-02-24 18:41:47 +08001246 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 struct cypress_private *priv = usb_get_serial_port_data(port);
1248 struct tty_struct *tty;
1249 unsigned char *data = urb->transfer_buffer;
1250 unsigned long flags;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001251 char tty_flag = TTY_NORMAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 int havedata = 0;
1253 int bytes = 0;
1254 int result;
1255 int i = 0;
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001256 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Harvey Harrison441b62c2008-03-03 16:08:34 -08001258 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001260 switch (status) {
Mike Isely78aef512006-08-29 22:07:11 -05001261 case 0: /* success */
1262 break;
1263 case -ECONNRESET:
1264 case -ENOENT:
1265 case -ESHUTDOWN:
1266 /* precursor to disconnect so just go away */
1267 return;
1268 case -EPIPE:
Alan Cox813a2242008-07-22 11:10:36 +01001269 usb_clear_halt(port->serial->dev, 0x81);
Mike Isely78aef512006-08-29 22:07:11 -05001270 break;
1271 default:
1272 /* something ugly is going on... */
Alan Cox813a2242008-07-22 11:10:36 +01001273 dev_err(&urb->dev->dev,
1274 "%s - unexpected nonzero read status received: %d\n",
1275 __func__, status);
Mike Isely78aef512006-08-29 22:07:11 -05001276 cypress_set_dead(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 return;
1278 }
1279
1280 spin_lock_irqsave(&priv->lock, flags);
1281 if (priv->rx_flags & THROTTLED) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001282 dbg("%s - now throttling", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 priv->rx_flags |= ACTUALLY_THROTTLED;
1284 spin_unlock_irqrestore(&priv->lock, flags);
1285 return;
1286 }
1287 spin_unlock_irqrestore(&priv->lock, flags);
1288
Alan Cox95da3102008-07-22 11:09:07 +01001289 tty = port->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 if (!tty) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001291 dbg("%s - bad tty pointer - exiting", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 return;
1293 }
1294
1295 spin_lock_irqsave(&priv->lock, flags);
Mike Isely3416eaa2008-02-10 20:23:19 -06001296 result = urb->actual_length;
1297 switch (priv->pkt_fmt) {
1298 default:
1299 case packet_format_1:
1300 /* This is for the CY7C64013... */
1301 priv->current_status = data[0] & 0xF8;
1302 bytes = data[1] + 2;
1303 i = 2;
1304 if (bytes > 2)
1305 havedata = 1;
1306 break;
1307 case packet_format_2:
1308 /* This is for the CY7C63743... */
1309 priv->current_status = data[0] & 0xF8;
1310 bytes = (data[0] & 0x07) + 1;
1311 i = 1;
1312 if (bytes > 1)
1313 havedata = 1;
1314 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 }
1316 spin_unlock_irqrestore(&priv->lock, flags);
Mike Isely3416eaa2008-02-10 20:23:19 -06001317 if (result < bytes) {
1318 dbg("%s - wrong packet size - received %d bytes but packet "
1319 "said %d bytes", __func__, result, bytes);
1320 goto continue_read;
1321 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
Alan Cox813a2242008-07-22 11:10:36 +01001323 usb_serial_debug_data(debug, &port->dev, __func__,
1324 urb->actual_length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
1326 spin_lock_irqsave(&priv->lock, flags);
1327 /* check to see if status has changed */
Mike Isely67683062008-02-10 20:23:28 -06001328 if (priv->current_status != priv->prev_status) {
1329 priv->diff_status |= priv->current_status ^
1330 priv->prev_status;
1331 wake_up_interruptible(&priv->delta_msr_wait);
1332 priv->prev_status = priv->current_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 }
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001334 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001336 /* hangup, as defined in acm.c... this might be a bad place for it
1337 * though */
1338 if (tty && !(tty->termios->c_cflag & CLOCAL) &&
1339 !(priv->current_status & UART_CD)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001340 dbg("%s - calling hangup", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 tty_hangup(tty);
1342 goto continue_read;
1343 }
1344
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001345 /* There is one error bit... I'm assuming it is a parity error
1346 * indicator as the generic firmware will set this bit to 1 if a
1347 * parity error occurs.
1348 * I can not find reference to any other error events. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 spin_lock_irqsave(&priv->lock, flags);
1350 if (priv->current_status & CYP_ERROR) {
1351 spin_unlock_irqrestore(&priv->lock, flags);
1352 tty_flag = TTY_PARITY;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001353 dbg("%s - Parity Error detected", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 } else
1355 spin_unlock_irqrestore(&priv->lock, flags);
1356
1357 /* process read if there is data other than line status */
1358 if (tty && (bytes > i)) {
Alan Cox33f0f882006-01-09 20:54:13 -08001359 bytes = tty_buffer_request_room(tty, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 for (; i < bytes ; ++i) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001361 dbg("pushing byte number %d - %d - %c", i, data[i],
1362 data[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 tty_insert_flip_char(tty, data[i], tty_flag);
1364 }
Alan Cox95da3102008-07-22 11:09:07 +01001365 tty_flip_buffer_push(port->port.tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 }
1367
1368 spin_lock_irqsave(&priv->lock, flags);
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001369 /* control and status byte(s) are also counted */
1370 priv->bytes_in += bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 spin_unlock_irqrestore(&priv->lock, flags);
1372
1373continue_read:
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001374
1375 /* Continue trying to always read... unless the port has closed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
Alan Cox95da3102008-07-22 11:09:07 +01001377 if (port->port.count > 0 && priv->comm_is_ok) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001378 usb_fill_int_urb(port->interrupt_in_urb, port->serial->dev,
1379 usb_rcvintpipe(port->serial->dev,
1380 port->interrupt_in_endpointAddress),
1381 port->interrupt_in_urb->transfer_buffer,
1382 port->interrupt_in_urb->transfer_buffer_length,
Alan Cox813a2242008-07-22 11:10:36 +01001383 cypress_read_int_callback, port,
1384 priv->read_urb_interval);
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001385 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
Mike Isely78aef512006-08-29 22:07:11 -05001386 if (result) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001387 dev_err(&urb->dev->dev, "%s - failed resubmitting "
Harvey Harrison441b62c2008-03-03 16:08:34 -08001388 "read urb, error %d\n", __func__,
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001389 result);
Mike Isely78aef512006-08-29 22:07:11 -05001390 cypress_set_dead(port);
1391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 }
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001393
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 return;
1395} /* cypress_read_int_callback */
1396
1397
David Howells7d12e782006-10-05 14:55:46 +01001398static void cypress_write_int_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399{
Ming Leicdc97792008-02-24 18:41:47 +08001400 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 struct cypress_private *priv = usb_get_serial_port_data(port);
1402 int result;
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001403 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
Harvey Harrison441b62c2008-03-03 16:08:34 -08001405 dbg("%s - port %d", __func__, port->number);
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001406
1407 switch (status) {
Alan Cox813a2242008-07-22 11:10:36 +01001408 case 0:
1409 /* success */
1410 break;
1411 case -ECONNRESET:
1412 case -ENOENT:
1413 case -ESHUTDOWN:
1414 /* this urb is terminated, clean up */
1415 dbg("%s - urb shutting down with status: %d",
1416 __func__, status);
1417 priv->write_urb_in_use = 0;
1418 return;
1419 case -EPIPE: /* no break needed; clear halt and resubmit */
1420 if (!priv->comm_is_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 break;
Alan Cox813a2242008-07-22 11:10:36 +01001422 usb_clear_halt(port->serial->dev, 0x02);
1423 /* error in the urb, so we have to resubmit it */
1424 dbg("%s - nonzero write bulk status received: %d",
1425 __func__, status);
1426 port->interrupt_out_urb->transfer_buffer_length = 1;
1427 port->interrupt_out_urb->dev = port->serial->dev;
1428 result = usb_submit_urb(port->interrupt_out_urb, GFP_ATOMIC);
1429 if (!result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 return;
Alan Cox813a2242008-07-22 11:10:36 +01001431 dev_err(&urb->dev->dev,
1432 "%s - failed resubmitting write urb, error %d\n",
1433 __func__, result);
1434 cypress_set_dead(port);
1435 break;
1436 default:
1437 dev_err(&urb->dev->dev,
1438 "%s - unexpected nonzero write status received: %d\n",
1439 __func__, status);
1440 cypress_set_dead(port);
1441 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 priv->write_urb_in_use = 0;
Alan Cox813a2242008-07-22 11:10:36 +01001444
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 /* send any buffered data */
1446 cypress_send(port);
1447}
1448
1449
1450/*****************************************************************************
1451 * Write buffer functions - buffering code from pl2303 used
1452 *****************************************************************************/
1453
1454/*
1455 * cypress_buf_alloc
1456 *
1457 * Allocate a circular buffer and all associated memory.
1458 */
1459
1460static struct cypress_buf *cypress_buf_alloc(unsigned int size)
1461{
1462
1463 struct cypress_buf *cb;
1464
1465
1466 if (size == 0)
1467 return NULL;
1468
Robert P. J. Day5cbded52006-12-13 00:35:56 -08001469 cb = kmalloc(sizeof(struct cypress_buf), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 if (cb == NULL)
1471 return NULL;
1472
1473 cb->buf_buf = kmalloc(size, GFP_KERNEL);
1474 if (cb->buf_buf == NULL) {
1475 kfree(cb);
1476 return NULL;
1477 }
1478
1479 cb->buf_size = size;
1480 cb->buf_get = cb->buf_put = cb->buf_buf;
1481
1482 return cb;
1483
1484}
1485
1486
1487/*
1488 * cypress_buf_free
1489 *
1490 * Free the buffer and all associated memory.
1491 */
1492
1493static void cypress_buf_free(struct cypress_buf *cb)
1494{
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001495 if (cb) {
1496 kfree(cb->buf_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 kfree(cb);
1498 }
1499}
1500
1501
1502/*
1503 * cypress_buf_clear
1504 *
1505 * Clear out all data in the circular buffer.
1506 */
1507
1508static void cypress_buf_clear(struct cypress_buf *cb)
1509{
1510 if (cb != NULL)
1511 cb->buf_get = cb->buf_put;
1512 /* equivalent to a get of all data available */
1513}
1514
1515
1516/*
1517 * cypress_buf_data_avail
1518 *
1519 * Return the number of bytes of data available in the circular
1520 * buffer.
1521 */
1522
1523static unsigned int cypress_buf_data_avail(struct cypress_buf *cb)
1524{
1525 if (cb != NULL)
Alan Cox813a2242008-07-22 11:10:36 +01001526 return (cb->buf_size + cb->buf_put - cb->buf_get)
1527 % cb->buf_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 else
1529 return 0;
1530}
1531
1532
1533/*
1534 * cypress_buf_space_avail
1535 *
1536 * Return the number of bytes of space available in the circular
1537 * buffer.
1538 */
1539
1540static unsigned int cypress_buf_space_avail(struct cypress_buf *cb)
1541{
1542 if (cb != NULL)
Alan Cox813a2242008-07-22 11:10:36 +01001543 return (cb->buf_size + cb->buf_get - cb->buf_put - 1)
1544 % cb->buf_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 else
1546 return 0;
1547}
1548
1549
1550/*
1551 * cypress_buf_put
1552 *
1553 * Copy data data from a user buffer and put it into the circular buffer.
1554 * Restrict to the amount of space available.
1555 *
1556 * Return the number of bytes copied.
1557 */
1558
1559static unsigned int cypress_buf_put(struct cypress_buf *cb, const char *buf,
1560 unsigned int count)
1561{
1562
1563 unsigned int len;
1564
1565
1566 if (cb == NULL)
1567 return 0;
1568
1569 len = cypress_buf_space_avail(cb);
1570 if (count > len)
1571 count = len;
1572
1573 if (count == 0)
1574 return 0;
1575
1576 len = cb->buf_buf + cb->buf_size - cb->buf_put;
1577 if (count > len) {
1578 memcpy(cb->buf_put, buf, len);
1579 memcpy(cb->buf_buf, buf+len, count - len);
1580 cb->buf_put = cb->buf_buf + count - len;
1581 } else {
1582 memcpy(cb->buf_put, buf, count);
1583 if (count < len)
1584 cb->buf_put += count;
1585 else /* count == len */
1586 cb->buf_put = cb->buf_buf;
1587 }
1588
1589 return count;
1590
1591}
1592
1593
1594/*
1595 * cypress_buf_get
1596 *
1597 * Get data from the circular buffer and copy to the given buffer.
1598 * Restrict to the amount of data available.
1599 *
1600 * Return the number of bytes copied.
1601 */
1602
1603static unsigned int cypress_buf_get(struct cypress_buf *cb, char *buf,
1604 unsigned int count)
1605{
1606
1607 unsigned int len;
1608
1609
1610 if (cb == NULL)
1611 return 0;
1612
1613 len = cypress_buf_data_avail(cb);
1614 if (count > len)
1615 count = len;
1616
1617 if (count == 0)
1618 return 0;
1619
1620 len = cb->buf_buf + cb->buf_size - cb->buf_get;
1621 if (count > len) {
1622 memcpy(buf, cb->buf_get, len);
1623 memcpy(buf+len, cb->buf_buf, count - len);
1624 cb->buf_get = cb->buf_buf + count - len;
1625 } else {
1626 memcpy(buf, cb->buf_get, count);
1627 if (count < len)
1628 cb->buf_get += count;
1629 else /* count == len */
1630 cb->buf_get = cb->buf_buf;
1631 }
1632
1633 return count;
1634
1635}
1636
1637/*****************************************************************************
1638 * Module functions
1639 *****************************************************************************/
1640
1641static int __init cypress_init(void)
1642{
1643 int retval;
Alan Cox813a2242008-07-22 11:10:36 +01001644
Harvey Harrison441b62c2008-03-03 16:08:34 -08001645 dbg("%s", __func__);
Alan Cox813a2242008-07-22 11:10:36 +01001646
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 retval = usb_serial_register(&cypress_earthmate_device);
1648 if (retval)
1649 goto failed_em_register;
1650 retval = usb_serial_register(&cypress_hidcom_device);
1651 if (retval)
1652 goto failed_hidcom_register;
Lonnie Mendeza5c44e22006-03-01 10:45:24 -06001653 retval = usb_serial_register(&cypress_ca42v2_device);
1654 if (retval)
1655 goto failed_ca42v2_register;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 retval = usb_register(&cypress_driver);
1657 if (retval)
1658 goto failed_usb_register;
1659
1660 info(DRIVER_DESC " " DRIVER_VERSION);
1661 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
Mariusz Kozlowski2e46b742006-11-17 17:49:22 +01001663failed_usb_register:
1664 usb_serial_deregister(&cypress_ca42v2_device);
1665failed_ca42v2_register:
1666 usb_serial_deregister(&cypress_hidcom_device);
1667failed_hidcom_register:
1668 usb_serial_deregister(&cypress_earthmate_device);
1669failed_em_register:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 return retval;
1671}
1672
1673
Alan Cox813a2242008-07-22 11:10:36 +01001674static void __exit cypress_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675{
Harvey Harrison441b62c2008-03-03 16:08:34 -08001676 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
Alan Cox813a2242008-07-22 11:10:36 +01001678 usb_deregister(&cypress_driver);
1679 usb_serial_deregister(&cypress_earthmate_device);
1680 usb_serial_deregister(&cypress_hidcom_device);
1681 usb_serial_deregister(&cypress_ca42v2_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682}
1683
1684
1685module_init(cypress_init);
1686module_exit(cypress_exit);
1687
Alan Cox813a2242008-07-22 11:10:36 +01001688MODULE_AUTHOR(DRIVER_AUTHOR);
1689MODULE_DESCRIPTION(DRIVER_DESC);
1690MODULE_VERSION(DRIVER_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691MODULE_LICENSE("GPL");
1692
1693module_param(debug, bool, S_IRUGO | S_IWUSR);
1694MODULE_PARM_DESC(debug, "Debug enabled or not");
1695module_param(stats, bool, S_IRUGO | S_IWUSR);
1696MODULE_PARM_DESC(stats, "Enable statistics or not");
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001697module_param(interval, int, S_IRUGO | S_IWUSR);
1698MODULE_PARM_DESC(interval, "Overrides interrupt interval");