blob: e0a8b715f2f24bcf6046e1cff52dc9cca1a304ff [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);
Alan Sternf9c99bb2009-06-02 11:53:55 -0400174static void cypress_release(struct usb_serial *serial);
Alan Coxa509a7e2009-09-19 13:13:26 -0700175static int cypress_open(struct tty_struct *tty, struct usb_serial_port *port);
Alan Cox335f8512009-06-11 12:26:29 +0100176static void cypress_close(struct usb_serial_port *port);
177static void cypress_dtr_rts(struct usb_serial_port *port, int on);
Alan Cox813a2242008-07-22 11:10:36 +0100178static int cypress_write(struct tty_struct *tty, struct usb_serial_port *port,
179 const unsigned char *buf, int count);
180static void cypress_send(struct usb_serial_port *port);
181static int cypress_write_room(struct tty_struct *tty);
182static int cypress_ioctl(struct tty_struct *tty, struct file *file,
183 unsigned int cmd, unsigned long arg);
184static void cypress_set_termios(struct tty_struct *tty,
185 struct usb_serial_port *port, struct ktermios *old);
186static int cypress_tiocmget(struct tty_struct *tty, struct file *file);
187static int cypress_tiocmset(struct tty_struct *tty, struct file *file,
188 unsigned int set, unsigned int clear);
189static int cypress_chars_in_buffer(struct tty_struct *tty);
190static void cypress_throttle(struct tty_struct *tty);
191static void cypress_unthrottle(struct tty_struct *tty);
192static void cypress_set_dead(struct usb_serial_port *port);
193static void cypress_read_int_callback(struct urb *urb);
194static void cypress_write_int_callback(struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195/* write buffer functions */
196static struct cypress_buf *cypress_buf_alloc(unsigned int size);
Alan Cox813a2242008-07-22 11:10:36 +0100197static void cypress_buf_free(struct cypress_buf *cb);
198static void cypress_buf_clear(struct cypress_buf *cb);
199static unsigned int cypress_buf_data_avail(struct cypress_buf *cb);
200static unsigned int cypress_buf_space_avail(struct cypress_buf *cb);
201static unsigned int cypress_buf_put(struct cypress_buf *cb,
202 const char *buf, unsigned int count);
203static unsigned int cypress_buf_get(struct cypress_buf *cb,
204 char *buf, unsigned int count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700207static struct usb_serial_driver cypress_earthmate_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700208 .driver = {
209 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700210 .name = "earthmate",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700211 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700212 .description = "DeLorme Earthmate USB",
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100213 .usb_driver = &cypress_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 .id_table = id_table_earthmate,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 .num_ports = 1,
216 .attach = cypress_earthmate_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -0400217 .release = cypress_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 .open = cypress_open,
219 .close = cypress_close,
Alan Cox335f8512009-06-11 12:26:29 +0100220 .dtr_rts = cypress_dtr_rts,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 .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,
Alan Sternf9c99bb2009-06-02 11:53:55 -0400244 .release = cypress_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 .open = cypress_open,
246 .close = cypress_close,
Alan Cox335f8512009-06-11 12:26:29 +0100247 .dtr_rts = cypress_dtr_rts,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 .write = cypress_write,
249 .write_room = cypress_write_room,
250 .ioctl = cypress_ioctl,
251 .set_termios = cypress_set_termios,
252 .tiocmget = cypress_tiocmget,
253 .tiocmset = cypress_tiocmset,
254 .chars_in_buffer = cypress_chars_in_buffer,
255 .throttle = cypress_throttle,
256 .unthrottle = cypress_unthrottle,
257 .read_int_callback = cypress_read_int_callback,
258 .write_int_callback = cypress_write_int_callback,
259};
260
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600261static struct usb_serial_driver cypress_ca42v2_device = {
262 .driver = {
263 .owner = THIS_MODULE,
Alan Cox813a2242008-07-22 11:10:36 +0100264 .name = "nokiaca42v2",
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600265 },
266 .description = "Nokia CA-42 V2 Adapter",
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100267 .usb_driver = &cypress_driver,
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600268 .id_table = id_table_nokiaca42v2,
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600269 .num_ports = 1,
270 .attach = cypress_ca42v2_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -0400271 .release = cypress_release,
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600272 .open = cypress_open,
273 .close = cypress_close,
Alan Cox335f8512009-06-11 12:26:29 +0100274 .dtr_rts = cypress_dtr_rts,
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600275 .write = cypress_write,
276 .write_room = cypress_write_room,
277 .ioctl = cypress_ioctl,
278 .set_termios = cypress_set_termios,
279 .tiocmget = cypress_tiocmget,
280 .tiocmset = cypress_tiocmset,
281 .chars_in_buffer = cypress_chars_in_buffer,
282 .throttle = cypress_throttle,
283 .unthrottle = cypress_unthrottle,
284 .read_int_callback = cypress_read_int_callback,
285 .write_int_callback = cypress_write_int_callback,
286};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288/*****************************************************************************
289 * Cypress serial helper functions
290 *****************************************************************************/
291
292
Alan Cox8873aaa2008-03-10 21:59:28 +0000293static int analyze_baud_rate(struct usb_serial_port *port, speed_t new_rate)
Mike Isely92983c22008-02-10 20:23:32 -0600294{
Mike Isely92983c22008-02-10 20:23:32 -0600295 struct cypress_private *priv;
296 priv = usb_get_serial_port_data(port);
297
298 /*
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;
Mike Isely93075542008-02-10 20:23:14 -0600347 __u8 feature_buffer[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 unsigned long flags;
349
Harvey Harrison441b62c2008-03-03 16:08:34 -0800350 dbg("%s", __func__);
Alan Cox813a2242008-07-22 11:10:36 +0100351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 priv = usb_get_serial_port_data(port);
353
Mike Isely78aef512006-08-29 22:07:11 -0500354 if (!priv->comm_is_ok)
355 return -ENODEV;
356
Alan Cox813a2242008-07-22 11:10:36 +0100357 switch (cypress_request_type) {
358 case CYPRESS_SET_CONFIG:
359 new_baudrate = priv->baud_rate;
360 /* 0 means 'Hang up' so doesn't change the true bit rate */
361 if (baud_rate == 0)
Mike Isely92983c22008-02-10 20:23:32 -0600362 new_baudrate = priv->baud_rate;
Alan Cox813a2242008-07-22 11:10:36 +0100363 /* Change of speed ? */
364 else if (baud_rate != priv->baud_rate) {
365 dbg("%s - baud rate is changing", __func__);
366 retval = analyze_baud_rate(port, baud_rate);
367 if (retval >= 0) {
368 new_baudrate = retval;
369 dbg("%s - New baud rate set to %d",
370 __func__, new_baudrate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 }
Alan Cox813a2242008-07-22 11:10:36 +0100372 }
373 dbg("%s - baud rate is being sent as %d",
374 __func__, new_baudrate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Alan Cox813a2242008-07-22 11:10:36 +0100376 memset(feature_buffer, 0, sizeof(feature_buffer));
377 /* fill the feature_buffer with new configuration */
378 *((u_int32_t *)feature_buffer) = new_baudrate;
379 feature_buffer[4] |= data_bits; /* assign data bits in 2 bit space ( max 3 ) */
380 /* 1 bit gap */
381 feature_buffer[4] |= (stop_bits << 3); /* assign stop bits in 1 bit space */
382 feature_buffer[4] |= (parity_enable << 4); /* assign parity flag in 1 bit space */
383 feature_buffer[4] |= (parity_type << 5); /* assign parity type in 1 bit space */
384 /* 1 bit gap */
385 feature_buffer[4] |= (reset << 7); /* assign reset at end of byte, 1 bit space */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Alan Cox813a2242008-07-22 11:10:36 +0100387 dbg("%s - device is being sent this feature report:",
388 __func__);
389 dbg("%s - %02X - %02X - %02X - %02X - %02X", __func__,
390 feature_buffer[0], feature_buffer[1],
391 feature_buffer[2], feature_buffer[3],
392 feature_buffer[4]);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500393
Alan Cox813a2242008-07-22 11:10:36 +0100394 do {
395 retval = usb_control_msg(port->serial->dev,
396 usb_sndctrlpipe(port->serial->dev, 0),
397 HID_REQ_SET_REPORT,
398 USB_DIR_OUT | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
399 0x0300, 0, feature_buffer,
400 sizeof(feature_buffer), 500);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500401
Alan Cox813a2242008-07-22 11:10:36 +0100402 if (tries++ >= 3)
403 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Alan Cox813a2242008-07-22 11:10:36 +0100405 } while (retval != sizeof(feature_buffer) &&
406 retval != -ENODEV);
Mike Isely93075542008-02-10 20:23:14 -0600407
Alan Cox813a2242008-07-22 11:10:36 +0100408 if (retval != sizeof(feature_buffer)) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700409 dev_err(&port->dev, "%s - failed sending serial "
410 "line settings - %d\n", __func__, retval);
Alan Cox813a2242008-07-22 11:10:36 +0100411 cypress_set_dead(port);
412 } else {
413 spin_lock_irqsave(&priv->lock, flags);
414 priv->baud_rate = new_baudrate;
415 priv->current_config = feature_buffer[4];
416 spin_unlock_irqrestore(&priv->lock, flags);
417 /* If we asked for a speed change encode it */
418 if (baud_rate)
419 tty_encode_baud_rate(tty,
420 new_baudrate, new_baudrate);
421 }
422 break;
423 case CYPRESS_GET_CONFIG:
424 if (priv->get_cfg_unsafe) {
425 /* Not implemented for this device,
426 and if we try to do it we're likely
427 to crash the hardware. */
428 return -ENOTTY;
429 }
430 dbg("%s - retreiving serial line settings", __func__);
431 /* set initial values in feature buffer */
432 memset(feature_buffer, 0, sizeof(feature_buffer));
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500433
Alan Cox813a2242008-07-22 11:10:36 +0100434 do {
435 retval = usb_control_msg(port->serial->dev,
436 usb_rcvctrlpipe(port->serial->dev, 0),
437 HID_REQ_GET_REPORT,
438 USB_DIR_IN | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
439 0x0300, 0, feature_buffer,
440 sizeof(feature_buffer), 500);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500441
Alan Cox813a2242008-07-22 11:10:36 +0100442 if (tries++ >= 3)
443 break;
444 } while (retval != sizeof(feature_buffer)
445 && retval != -ENODEV);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500446
Alan Cox813a2242008-07-22 11:10:36 +0100447 if (retval != sizeof(feature_buffer)) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700448 dev_err(&port->dev, "%s - failed to retrieve serial "
449 "line settings - %d\n", __func__, retval);
Alan Cox813a2242008-07-22 11:10:36 +0100450 cypress_set_dead(port);
451 return retval;
452 } else {
453 spin_lock_irqsave(&priv->lock, flags);
454 /* store the config in one byte, and later
455 use bit masks to check values */
456 priv->current_config = feature_buffer[4];
457 priv->baud_rate = *((u_int32_t *)feature_buffer);
458 spin_unlock_irqrestore(&priv->lock, flags);
459 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 }
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500461 spin_lock_irqsave(&priv->lock, flags);
462 ++priv->cmd_count;
463 spin_unlock_irqrestore(&priv->lock, flags);
464
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 */
693 priv = usb_get_serial_port_data(port);
694 spin_lock_irq(&priv->lock);
695 if (on == 0)
696 priv->line_control = 0;
697 else
698 priv->line_control = CONTROL_DTR | CONTROL_RTS;
699 priv->cmd_ctrl = 1;
700 spin_unlock_irq(&priv->lock);
701 cypress_write(NULL, port, NULL, 0);
702}
703
704static void cypress_close(struct usb_serial_port *port)
705{
706 struct cypress_private *priv = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
Harvey Harrison441b62c2008-03-03 16:08:34 -0800708 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100710 /* writing is potentially harmful, lock must be taken */
711 mutex_lock(&port->serial->disc_mutex);
712 if (port->serial->disconnected) {
713 mutex_unlock(&port->serial->disc_mutex);
714 return;
715 }
Alan Cox335f8512009-06-11 12:26:29 +0100716 cypress_buf_clear(priv->buf);
Harvey Harrison441b62c2008-03-03 16:08:34 -0800717 dbg("%s - stopping urbs", __func__);
Alan Cox813a2242008-07-22 11:10:36 +0100718 usb_kill_urb(port->interrupt_in_urb);
719 usb_kill_urb(port->interrupt_out_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
722 if (stats)
Alan Cox813a2242008-07-22 11:10:36 +0100723 dev_info(&port->dev, "Statistics: %d Bytes In | %d Bytes Out | %d Commands Issued\n",
724 priv->bytes_in, priv->bytes_out, priv->cmd_count);
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100725 mutex_unlock(&port->serial->disc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726} /* cypress_close */
727
728
Alan Cox95da3102008-07-22 11:09:07 +0100729static int cypress_write(struct tty_struct *tty, struct usb_serial_port *port,
730 const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731{
732 struct cypress_private *priv = usb_get_serial_port_data(port);
733 unsigned long flags;
Alan Cox813a2242008-07-22 11:10:36 +0100734
Harvey Harrison441b62c2008-03-03 16:08:34 -0800735 dbg("%s - port %d, %d bytes", __func__, port->number, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
737 /* line control commands, which need to be executed immediately,
738 are not put into the buffer for obvious reasons.
739 */
740 if (priv->cmd_ctrl) {
741 count = 0;
742 goto finish;
743 }
Alan Cox813a2242008-07-22 11:10:36 +0100744
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 if (!count)
746 return count;
Alan Cox813a2242008-07-22 11:10:36 +0100747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 spin_lock_irqsave(&priv->lock, flags);
749 count = cypress_buf_put(priv->buf, buf, count);
750 spin_unlock_irqrestore(&priv->lock, flags);
751
752finish:
753 cypress_send(port);
754
755 return count;
756} /* cypress_write */
757
758
759static void cypress_send(struct usb_serial_port *port)
760{
761 int count = 0, result, offset, actual_size;
762 struct cypress_private *priv = usb_get_serial_port_data(port);
763 unsigned long flags;
Alan Cox813a2242008-07-22 11:10:36 +0100764
Mike Isely78aef512006-08-29 22:07:11 -0500765 if (!priv->comm_is_ok)
766 return;
767
Harvey Harrison441b62c2008-03-03 16:08:34 -0800768 dbg("%s - port %d", __func__, port->number);
Alan Cox813a2242008-07-22 11:10:36 +0100769 dbg("%s - interrupt out size is %d", __func__,
770 port->interrupt_out_size);
771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 spin_lock_irqsave(&priv->lock, flags);
773 if (priv->write_urb_in_use) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800774 dbg("%s - can't write, urb in use", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 spin_unlock_irqrestore(&priv->lock, flags);
776 return;
777 }
778 spin_unlock_irqrestore(&priv->lock, flags);
779
780 /* clear buffer */
Alan Cox813a2242008-07-22 11:10:36 +0100781 memset(port->interrupt_out_urb->transfer_buffer, 0,
782 port->interrupt_out_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
784 spin_lock_irqsave(&priv->lock, flags);
Mike Isely3416eaa2008-02-10 20:23:19 -0600785 switch (priv->pkt_fmt) {
786 default:
787 case packet_format_1:
788 /* this is for the CY7C64013... */
789 offset = 2;
790 port->interrupt_out_buffer[0] = priv->line_control;
791 break;
792 case packet_format_2:
793 /* this is for the CY7C63743... */
794 offset = 1;
795 port->interrupt_out_buffer[0] = priv->line_control;
796 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 }
798
799 if (priv->line_control & CONTROL_RESET)
800 priv->line_control &= ~CONTROL_RESET;
801
802 if (priv->cmd_ctrl) {
803 priv->cmd_count++;
Harvey Harrison441b62c2008-03-03 16:08:34 -0800804 dbg("%s - line control command being issued", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 spin_unlock_irqrestore(&priv->lock, flags);
806 goto send;
807 } else
808 spin_unlock_irqrestore(&priv->lock, flags);
809
810 count = cypress_buf_get(priv->buf, &port->interrupt_out_buffer[offset],
811 port->interrupt_out_size-offset);
812
Alan Cox813a2242008-07-22 11:10:36 +0100813 if (count == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Mike Isely3416eaa2008-02-10 20:23:19 -0600816 switch (priv->pkt_fmt) {
817 default:
818 case packet_format_1:
819 port->interrupt_out_buffer[1] = count;
820 break;
821 case packet_format_2:
822 port->interrupt_out_buffer[0] |= count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 }
824
Harvey Harrison441b62c2008-03-03 16:08:34 -0800825 dbg("%s - count is %d", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
827send:
828 spin_lock_irqsave(&priv->lock, flags);
829 priv->write_urb_in_use = 1;
830 spin_unlock_irqrestore(&priv->lock, flags);
831
832 if (priv->cmd_ctrl)
833 actual_size = 1;
834 else
Mike Isely3416eaa2008-02-10 20:23:19 -0600835 actual_size = count +
836 (priv->pkt_fmt == packet_format_1 ? 2 : 1);
837
Alan Cox813a2242008-07-22 11:10:36 +0100838 usb_serial_debug_data(debug, &port->dev, __func__,
839 port->interrupt_out_size,
840 port->interrupt_out_urb->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Mike Isely9aa8dae2006-08-29 22:07:04 -0500842 usb_fill_int_urb(port->interrupt_out_urb, port->serial->dev,
843 usb_sndintpipe(port->serial->dev, port->interrupt_out_endpointAddress),
844 port->interrupt_out_buffer, port->interrupt_out_size,
845 cypress_write_int_callback, port, priv->write_urb_interval);
Alan Cox813a2242008-07-22 11:10:36 +0100846 result = usb_submit_urb(port->interrupt_out_urb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 if (result) {
Alan Cox813a2242008-07-22 11:10:36 +0100848 dev_err(&port->dev,
849 "%s - failed submitting write urb, error %d\n",
850 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 priv->write_urb_in_use = 0;
Mike Isely78aef512006-08-29 22:07:11 -0500852 cypress_set_dead(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 }
854
855 spin_lock_irqsave(&priv->lock, flags);
Alan Cox813a2242008-07-22 11:10:36 +0100856 if (priv->cmd_ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 priv->cmd_ctrl = 0;
Alan Cox813a2242008-07-22 11:10:36 +0100858
859 /* do not count the line control and size bytes */
860 priv->bytes_out += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 spin_unlock_irqrestore(&priv->lock, flags);
862
Pete Zaitcevcf2c7482006-05-22 21:58:49 -0700863 usb_serial_port_softint(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864} /* cypress_send */
865
866
867/* returns how much space is available in the soft buffer */
Alan Cox95da3102008-07-22 11:09:07 +0100868static int cypress_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869{
Alan Cox95da3102008-07-22 11:09:07 +0100870 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 struct cypress_private *priv = usb_get_serial_port_data(port);
872 int room = 0;
873 unsigned long flags;
874
Harvey Harrison441b62c2008-03-03 16:08:34 -0800875 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
877 spin_lock_irqsave(&priv->lock, flags);
878 room = cypress_buf_space_avail(priv->buf);
879 spin_unlock_irqrestore(&priv->lock, flags);
880
Harvey Harrison441b62c2008-03-03 16:08:34 -0800881 dbg("%s - returns %d", __func__, room);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 return room;
883}
884
885
Alan Cox95da3102008-07-22 11:09:07 +0100886static int cypress_tiocmget(struct tty_struct *tty, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887{
Alan Cox95da3102008-07-22 11:09:07 +0100888 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 struct cypress_private *priv = usb_get_serial_port_data(port);
890 __u8 status, control;
891 unsigned int result = 0;
892 unsigned long flags;
Alan Cox813a2242008-07-22 11:10:36 +0100893
Harvey Harrison441b62c2008-03-03 16:08:34 -0800894 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
896 spin_lock_irqsave(&priv->lock, flags);
897 control = priv->line_control;
898 status = priv->current_status;
899 spin_unlock_irqrestore(&priv->lock, flags);
900
901 result = ((control & CONTROL_DTR) ? TIOCM_DTR : 0)
902 | ((control & CONTROL_RTS) ? TIOCM_RTS : 0)
903 | ((status & UART_CTS) ? TIOCM_CTS : 0)
904 | ((status & UART_DSR) ? TIOCM_DSR : 0)
905 | ((status & UART_RI) ? TIOCM_RI : 0)
906 | ((status & UART_CD) ? TIOCM_CD : 0);
907
Harvey Harrison441b62c2008-03-03 16:08:34 -0800908 dbg("%s - result = %x", __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
910 return result;
911}
912
913
Alan Cox95da3102008-07-22 11:09:07 +0100914static int cypress_tiocmset(struct tty_struct *tty, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 unsigned int set, unsigned int clear)
916{
Alan Cox95da3102008-07-22 11:09:07 +0100917 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 struct cypress_private *priv = usb_get_serial_port_data(port);
919 unsigned long flags;
Alan Cox813a2242008-07-22 11:10:36 +0100920
Harvey Harrison441b62c2008-03-03 16:08:34 -0800921 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
923 spin_lock_irqsave(&priv->lock, flags);
924 if (set & TIOCM_RTS)
925 priv->line_control |= CONTROL_RTS;
926 if (set & TIOCM_DTR)
927 priv->line_control |= CONTROL_DTR;
928 if (clear & TIOCM_RTS)
929 priv->line_control &= ~CONTROL_RTS;
930 if (clear & TIOCM_DTR)
931 priv->line_control &= ~CONTROL_DTR;
Alan Cox8873aaa2008-03-10 21:59:28 +0000932 priv->cmd_ctrl = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 spin_unlock_irqrestore(&priv->lock, flags);
934
Alan Cox95da3102008-07-22 11:09:07 +0100935 return cypress_write(tty, port, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936}
937
938
Alan Cox813a2242008-07-22 11:10:36 +0100939static int cypress_ioctl(struct tty_struct *tty, struct file *file,
Alan Cox95da3102008-07-22 11:09:07 +0100940 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941{
Alan Cox95da3102008-07-22 11:09:07 +0100942 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 struct cypress_private *priv = usb_get_serial_port_data(port);
944
Harvey Harrison441b62c2008-03-03 16:08:34 -0800945 dbg("%s - port %d, cmd 0x%.4x", __func__, port->number, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
947 switch (cmd) {
Alan Cox813a2242008-07-22 11:10:36 +0100948 /* This code comes from drivers/char/serial.c and ftdi_sio.c */
949 case TIOCMIWAIT:
950 while (priv != NULL) {
951 interruptible_sleep_on(&priv->delta_msr_wait);
952 /* see if a signal did it */
953 if (signal_pending(current))
954 return -ERESTARTSYS;
955 else {
956 char diff = priv->diff_status;
957 if (diff == 0)
958 return -EIO; /* no change => error */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
Alan Cox813a2242008-07-22 11:10:36 +0100960 /* consume all events */
961 priv->diff_status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
Alan Cox813a2242008-07-22 11:10:36 +0100963 /* return 0 if caller wanted to know about
964 these bits */
965 if (((arg & TIOCM_RNG) && (diff & UART_RI)) ||
966 ((arg & TIOCM_DSR) && (diff & UART_DSR)) ||
967 ((arg & TIOCM_CD) && (diff & UART_CD)) ||
968 ((arg & TIOCM_CTS) && (diff & UART_CTS)))
969 return 0;
970 /* otherwise caller can't care less about what
971 * happened, and so we continue to wait for
972 * more events.
973 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 }
Alan Cox813a2242008-07-22 11:10:36 +0100975 }
976 return 0;
977 default:
978 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 }
Harvey Harrison441b62c2008-03-03 16:08:34 -0800980 dbg("%s - arg not supported - it was 0x%04x - check include/asm/ioctls.h", __func__, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 return -ENOIOCTLCMD;
982} /* cypress_ioctl */
983
984
Alan Cox95da3102008-07-22 11:09:07 +0100985static void cypress_set_termios(struct tty_struct *tty,
986 struct usb_serial_port *port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987{
988 struct cypress_private *priv = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 int data_bits, stop_bits, parity_type, parity_enable;
Alan Cox8873aaa2008-03-10 21:59:28 +0000990 unsigned cflag, iflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 unsigned long flags;
992 __u8 oldlines;
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500993 int linechange = 0;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500994
Harvey Harrison441b62c2008-03-03 16:08:34 -0800995 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 spin_lock_irqsave(&priv->lock, flags);
Alan Coxfe1ae7f2009-09-19 13:13:33 -0700998 /* We can't clean this one up as we don't know the device type
999 early enough */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 if (!priv->termios_initialized) {
1001 if (priv->chiptype == CT_EARTHMATE) {
1002 *(tty->termios) = tty_std_termios;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001003 tty->termios->c_cflag = B4800 | CS8 | CREAD | HUPCL |
1004 CLOCAL;
Alan Cox8873aaa2008-03-10 21:59:28 +00001005 tty->termios->c_ispeed = 4800;
1006 tty->termios->c_ospeed = 4800;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 } else if (priv->chiptype == CT_CYPHIDCOM) {
1008 *(tty->termios) = tty_std_termios;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001009 tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
1010 CLOCAL;
Alan Cox8873aaa2008-03-10 21:59:28 +00001011 tty->termios->c_ispeed = 9600;
1012 tty->termios->c_ospeed = 9600;
Lonnie Mendeza5c44e22006-03-01 10:45:24 -06001013 } else if (priv->chiptype == CT_CA42V2) {
1014 *(tty->termios) = tty_std_termios;
1015 tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
1016 CLOCAL;
Alan Cox8873aaa2008-03-10 21:59:28 +00001017 tty->termios->c_ispeed = 9600;
1018 tty->termios->c_ospeed = 9600;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 }
1020 priv->termios_initialized = 1;
1021 }
1022 spin_unlock_irqrestore(&priv->lock, flags);
1023
Alan Cox8873aaa2008-03-10 21:59:28 +00001024 /* Unsupported features need clearing */
1025 tty->termios->c_cflag &= ~(CMSPAR|CRTSCTS);
1026
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 cflag = tty->termios->c_cflag;
1028 iflag = tty->termios->c_iflag;
1029
1030 /* check if there are new settings */
1031 if (old_termios) {
Alan Cox8873aaa2008-03-10 21:59:28 +00001032 spin_lock_irqsave(&priv->lock, flags);
1033 priv->tmp_termios = *(tty->termios);
1034 spin_unlock_irqrestore(&priv->lock, flags);
1035 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
1037 /* set number of data bits, parity, stop bits */
1038 /* when parity is disabled the parity type bit is ignored */
1039
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001040 /* 1 means 2 stop bits, 0 means 1 stop bit */
1041 stop_bits = cflag & CSTOPB ? 1 : 0;
1042
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 if (cflag & PARENB) {
1044 parity_enable = 1;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001045 /* 1 means odd parity, 0 means even parity */
1046 parity_type = cflag & PARODD ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 } else
1048 parity_enable = parity_type = 0;
1049
Alan Cox77336822008-07-22 11:10:53 +01001050 switch (cflag & CSIZE) {
1051 case CS5:
1052 data_bits = 0;
1053 break;
1054 case CS6:
1055 data_bits = 1;
1056 break;
1057 case CS7:
1058 data_bits = 2;
1059 break;
1060 case CS8:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 data_bits = 3;
Alan Cox77336822008-07-22 11:10:53 +01001062 break;
1063 default:
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001064 dev_err(&port->dev, "%s - CSIZE was set, but not CS5-CS8\n",
1065 __func__);
Alan Cox77336822008-07-22 11:10:53 +01001066 data_bits = 3;
1067 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 spin_lock_irqsave(&priv->lock, flags);
1069 oldlines = priv->line_control;
1070 if ((cflag & CBAUD) == B0) {
1071 /* drop dtr and rts */
Harvey Harrison441b62c2008-03-03 16:08:34 -08001072 dbg("%s - dropping the lines, baud rate 0bps", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
Alan Cox8873aaa2008-03-10 21:59:28 +00001074 } else
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001075 priv->line_control = (CONTROL_DTR | CONTROL_RTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001078 dbg("%s - sending %d stop_bits, %d parity_enable, %d parity_type, "
Harvey Harrison441b62c2008-03-03 16:08:34 -08001079 "%d data_bits (+5)", __func__, stop_bits,
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001080 parity_enable, parity_type, data_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
Alan Cox813a2242008-07-22 11:10:36 +01001082 cypress_serial_control(tty, port, tty_get_baud_rate(tty),
1083 data_bits, stop_bits,
1084 parity_enable, parity_type,
1085 0, CYPRESS_SET_CONFIG);
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001086
1087 /* we perform a CYPRESS_GET_CONFIG so that the current settings are
1088 * filled into the private structure this should confirm that all is
1089 * working if it returns what we just set */
Alan Cox95da3102008-07-22 11:09:07 +01001090 cypress_serial_control(tty, port, 0, 0, 0, 0, 0, 0, CYPRESS_GET_CONFIG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001092 /* Here we can define custom tty settings for devices; the main tty
1093 * termios flag base comes from empeg.c */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001095 spin_lock_irqsave(&priv->lock, flags);
Alan Cox813a2242008-07-22 11:10:36 +01001096 if (priv->chiptype == CT_EARTHMATE && priv->baud_rate == 4800) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001097 dbg("Using custom termios settings for a baud rate of "
1098 "4800bps.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 /* define custom termios settings for NMEA protocol */
1100
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 tty->termios->c_iflag /* input modes - */
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001102 &= ~(IGNBRK /* disable ignore break */
1103 | BRKINT /* disable break causes interrupt */
1104 | PARMRK /* disable mark parity errors */
1105 | ISTRIP /* disable clear high bit of input char */
1106 | INLCR /* disable translate NL to CR */
1107 | IGNCR /* disable ignore CR */
1108 | ICRNL /* disable translate CR to NL */
1109 | IXON); /* disable enable XON/XOFF flow control */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001111 tty->termios->c_oflag /* output modes */
1112 &= ~OPOST; /* disable postprocess output char */
1113
1114 tty->termios->c_lflag /* line discipline modes */
1115 &= ~(ECHO /* disable echo input characters */
1116 | ECHONL /* disable echo new line */
1117 | ICANON /* disable erase, kill, werase, and rprnt
1118 special characters */
1119 | ISIG /* disable interrupt, quit, and suspend
1120 special characters */
1121 | IEXTEN); /* disable non-POSIX special characters */
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001122 } /* CT_CYPHIDCOM: Application should handle this for device */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 linechange = (priv->line_control != oldlines);
1125 spin_unlock_irqrestore(&priv->lock, flags);
1126
1127 /* if necessary, set lines */
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001128 if (linechange) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 priv->cmd_ctrl = 1;
Alan Cox95da3102008-07-22 11:09:07 +01001130 cypress_write(tty, port, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132} /* cypress_set_termios */
1133
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001134
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135/* returns amount of data still left in soft buffer */
Alan Cox95da3102008-07-22 11:09:07 +01001136static int cypress_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137{
Alan Cox95da3102008-07-22 11:09:07 +01001138 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 struct cypress_private *priv = usb_get_serial_port_data(port);
1140 int chars = 0;
1141 unsigned long flags;
1142
Harvey Harrison441b62c2008-03-03 16:08:34 -08001143 dbg("%s - port %d", __func__, port->number);
Alan Cox813a2242008-07-22 11:10:36 +01001144
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 spin_lock_irqsave(&priv->lock, flags);
1146 chars = cypress_buf_data_avail(priv->buf);
1147 spin_unlock_irqrestore(&priv->lock, flags);
1148
Harvey Harrison441b62c2008-03-03 16:08:34 -08001149 dbg("%s - returns %d", __func__, chars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 return chars;
1151}
1152
1153
Alan Cox95da3102008-07-22 11:09:07 +01001154static void cypress_throttle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155{
Alan Cox95da3102008-07-22 11:09:07 +01001156 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 struct cypress_private *priv = usb_get_serial_port_data(port);
1158 unsigned long flags;
1159
Harvey Harrison441b62c2008-03-03 16:08:34 -08001160 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
1162 spin_lock_irqsave(&priv->lock, flags);
1163 priv->rx_flags = THROTTLED;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001164 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165}
1166
1167
Alan Cox95da3102008-07-22 11:09:07 +01001168static void cypress_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169{
Alan Cox95da3102008-07-22 11:09:07 +01001170 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 struct cypress_private *priv = usb_get_serial_port_data(port);
1172 int actually_throttled, result;
1173 unsigned long flags;
1174
Harvey Harrison441b62c2008-03-03 16:08:34 -08001175 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
1177 spin_lock_irqsave(&priv->lock, flags);
1178 actually_throttled = priv->rx_flags & ACTUALLY_THROTTLED;
1179 priv->rx_flags = 0;
1180 spin_unlock_irqrestore(&priv->lock, flags);
1181
Mike Isely78aef512006-08-29 22:07:11 -05001182 if (!priv->comm_is_ok)
1183 return;
1184
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 if (actually_throttled) {
1186 port->interrupt_in_urb->dev = port->serial->dev;
1187
1188 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
Mike Isely78aef512006-08-29 22:07:11 -05001189 if (result) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001190 dev_err(&port->dev, "%s - failed submitting read urb, "
Harvey Harrison441b62c2008-03-03 16:08:34 -08001191 "error %d\n", __func__, result);
Mike Isely78aef512006-08-29 22:07:11 -05001192 cypress_set_dead(port);
1193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 }
1195}
1196
1197
David Howells7d12e782006-10-05 14:55:46 +01001198static void cypress_read_int_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199{
Ming Leicdc97792008-02-24 18:41:47 +08001200 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 struct cypress_private *priv = usb_get_serial_port_data(port);
1202 struct tty_struct *tty;
1203 unsigned char *data = urb->transfer_buffer;
1204 unsigned long flags;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001205 char tty_flag = TTY_NORMAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 int havedata = 0;
1207 int bytes = 0;
1208 int result;
1209 int i = 0;
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001210 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
Harvey Harrison441b62c2008-03-03 16:08:34 -08001212 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001214 switch (status) {
Mike Isely78aef512006-08-29 22:07:11 -05001215 case 0: /* success */
1216 break;
1217 case -ECONNRESET:
1218 case -ENOENT:
1219 case -ESHUTDOWN:
1220 /* precursor to disconnect so just go away */
1221 return;
1222 case -EPIPE:
Alan Stern4d2fae82009-07-09 12:59:57 -04001223 /* Can't call usb_clear_halt while in_interrupt */
1224 /* FALLS THROUGH */
Mike Isely78aef512006-08-29 22:07:11 -05001225 default:
1226 /* something ugly is going on... */
Alan Cox813a2242008-07-22 11:10:36 +01001227 dev_err(&urb->dev->dev,
1228 "%s - unexpected nonzero read status received: %d\n",
1229 __func__, status);
Mike Isely78aef512006-08-29 22:07:11 -05001230 cypress_set_dead(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 return;
1232 }
1233
1234 spin_lock_irqsave(&priv->lock, flags);
1235 if (priv->rx_flags & THROTTLED) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001236 dbg("%s - now throttling", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 priv->rx_flags |= ACTUALLY_THROTTLED;
1238 spin_unlock_irqrestore(&priv->lock, flags);
1239 return;
1240 }
1241 spin_unlock_irqrestore(&priv->lock, flags);
1242
Alan Cox4a90f092008-10-13 10:39:46 +01001243 tty = tty_port_tty_get(&port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 if (!tty) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001245 dbg("%s - bad tty pointer - exiting", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 return;
1247 }
1248
1249 spin_lock_irqsave(&priv->lock, flags);
Mike Isely3416eaa2008-02-10 20:23:19 -06001250 result = urb->actual_length;
1251 switch (priv->pkt_fmt) {
1252 default:
1253 case packet_format_1:
1254 /* This is for the CY7C64013... */
1255 priv->current_status = data[0] & 0xF8;
1256 bytes = data[1] + 2;
1257 i = 2;
1258 if (bytes > 2)
1259 havedata = 1;
1260 break;
1261 case packet_format_2:
1262 /* This is for the CY7C63743... */
1263 priv->current_status = data[0] & 0xF8;
1264 bytes = (data[0] & 0x07) + 1;
1265 i = 1;
1266 if (bytes > 1)
1267 havedata = 1;
1268 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 }
1270 spin_unlock_irqrestore(&priv->lock, flags);
Mike Isely3416eaa2008-02-10 20:23:19 -06001271 if (result < bytes) {
1272 dbg("%s - wrong packet size - received %d bytes but packet "
1273 "said %d bytes", __func__, result, bytes);
1274 goto continue_read;
1275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
Alan Cox813a2242008-07-22 11:10:36 +01001277 usb_serial_debug_data(debug, &port->dev, __func__,
1278 urb->actual_length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
1280 spin_lock_irqsave(&priv->lock, flags);
1281 /* check to see if status has changed */
Mike Isely67683062008-02-10 20:23:28 -06001282 if (priv->current_status != priv->prev_status) {
1283 priv->diff_status |= priv->current_status ^
1284 priv->prev_status;
1285 wake_up_interruptible(&priv->delta_msr_wait);
1286 priv->prev_status = priv->current_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 }
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001288 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001290 /* hangup, as defined in acm.c... this might be a bad place for it
1291 * though */
1292 if (tty && !(tty->termios->c_cflag & CLOCAL) &&
1293 !(priv->current_status & UART_CD)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001294 dbg("%s - calling hangup", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 tty_hangup(tty);
1296 goto continue_read;
1297 }
1298
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001299 /* There is one error bit... I'm assuming it is a parity error
1300 * indicator as the generic firmware will set this bit to 1 if a
1301 * parity error occurs.
1302 * I can not find reference to any other error events. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 spin_lock_irqsave(&priv->lock, flags);
1304 if (priv->current_status & CYP_ERROR) {
1305 spin_unlock_irqrestore(&priv->lock, flags);
1306 tty_flag = TTY_PARITY;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001307 dbg("%s - Parity Error detected", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 } else
1309 spin_unlock_irqrestore(&priv->lock, flags);
1310
1311 /* process read if there is data other than line status */
1312 if (tty && (bytes > i)) {
Alan Cox33f0f882006-01-09 20:54:13 -08001313 bytes = tty_buffer_request_room(tty, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 for (; i < bytes ; ++i) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001315 dbg("pushing byte number %d - %d - %c", i, data[i],
1316 data[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 tty_insert_flip_char(tty, data[i], tty_flag);
1318 }
Alan Cox4a90f092008-10-13 10:39:46 +01001319 tty_flip_buffer_push(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 }
1321
1322 spin_lock_irqsave(&priv->lock, flags);
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001323 /* control and status byte(s) are also counted */
1324 priv->bytes_in += bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 spin_unlock_irqrestore(&priv->lock, flags);
1326
1327continue_read:
Alan Cox4a90f092008-10-13 10:39:46 +01001328 tty_kref_put(tty);
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001329
1330 /* Continue trying to always read... unless the port has closed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
Alan Cox95da3102008-07-22 11:09:07 +01001332 if (port->port.count > 0 && priv->comm_is_ok) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001333 usb_fill_int_urb(port->interrupt_in_urb, port->serial->dev,
1334 usb_rcvintpipe(port->serial->dev,
1335 port->interrupt_in_endpointAddress),
1336 port->interrupt_in_urb->transfer_buffer,
1337 port->interrupt_in_urb->transfer_buffer_length,
Alan Cox813a2242008-07-22 11:10:36 +01001338 cypress_read_int_callback, port,
1339 priv->read_urb_interval);
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001340 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
Mike Isely78aef512006-08-29 22:07:11 -05001341 if (result) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001342 dev_err(&urb->dev->dev, "%s - failed resubmitting "
Harvey Harrison441b62c2008-03-03 16:08:34 -08001343 "read urb, error %d\n", __func__,
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001344 result);
Mike Isely78aef512006-08-29 22:07:11 -05001345 cypress_set_dead(port);
1346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 }
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001348
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 return;
1350} /* cypress_read_int_callback */
1351
1352
David Howells7d12e782006-10-05 14:55:46 +01001353static void cypress_write_int_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354{
Ming Leicdc97792008-02-24 18:41:47 +08001355 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 struct cypress_private *priv = usb_get_serial_port_data(port);
1357 int result;
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001358 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
Harvey Harrison441b62c2008-03-03 16:08:34 -08001360 dbg("%s - port %d", __func__, port->number);
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001361
1362 switch (status) {
Alan Cox813a2242008-07-22 11:10:36 +01001363 case 0:
1364 /* success */
1365 break;
1366 case -ECONNRESET:
1367 case -ENOENT:
1368 case -ESHUTDOWN:
1369 /* this urb is terminated, clean up */
1370 dbg("%s - urb shutting down with status: %d",
1371 __func__, status);
1372 priv->write_urb_in_use = 0;
1373 return;
1374 case -EPIPE: /* no break needed; clear halt and resubmit */
1375 if (!priv->comm_is_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 break;
Alan Cox813a2242008-07-22 11:10:36 +01001377 usb_clear_halt(port->serial->dev, 0x02);
1378 /* error in the urb, so we have to resubmit it */
1379 dbg("%s - nonzero write bulk status received: %d",
1380 __func__, status);
1381 port->interrupt_out_urb->transfer_buffer_length = 1;
1382 port->interrupt_out_urb->dev = port->serial->dev;
1383 result = usb_submit_urb(port->interrupt_out_urb, GFP_ATOMIC);
1384 if (!result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 return;
Alan Cox813a2242008-07-22 11:10:36 +01001386 dev_err(&urb->dev->dev,
1387 "%s - failed resubmitting write urb, error %d\n",
1388 __func__, result);
1389 cypress_set_dead(port);
1390 break;
1391 default:
1392 dev_err(&urb->dev->dev,
1393 "%s - unexpected nonzero write status received: %d\n",
1394 __func__, status);
1395 cypress_set_dead(port);
1396 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 priv->write_urb_in_use = 0;
Alan Cox813a2242008-07-22 11:10:36 +01001399
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 /* send any buffered data */
1401 cypress_send(port);
1402}
1403
1404
1405/*****************************************************************************
1406 * Write buffer functions - buffering code from pl2303 used
1407 *****************************************************************************/
1408
1409/*
1410 * cypress_buf_alloc
1411 *
1412 * Allocate a circular buffer and all associated memory.
1413 */
1414
1415static struct cypress_buf *cypress_buf_alloc(unsigned int size)
1416{
1417
1418 struct cypress_buf *cb;
1419
1420
1421 if (size == 0)
1422 return NULL;
1423
Robert P. J. Day5cbded52006-12-13 00:35:56 -08001424 cb = kmalloc(sizeof(struct cypress_buf), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 if (cb == NULL)
1426 return NULL;
1427
1428 cb->buf_buf = kmalloc(size, GFP_KERNEL);
1429 if (cb->buf_buf == NULL) {
1430 kfree(cb);
1431 return NULL;
1432 }
1433
1434 cb->buf_size = size;
1435 cb->buf_get = cb->buf_put = cb->buf_buf;
1436
1437 return cb;
1438
1439}
1440
1441
1442/*
1443 * cypress_buf_free
1444 *
1445 * Free the buffer and all associated memory.
1446 */
1447
1448static void cypress_buf_free(struct cypress_buf *cb)
1449{
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001450 if (cb) {
1451 kfree(cb->buf_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 kfree(cb);
1453 }
1454}
1455
1456
1457/*
1458 * cypress_buf_clear
1459 *
1460 * Clear out all data in the circular buffer.
1461 */
1462
1463static void cypress_buf_clear(struct cypress_buf *cb)
1464{
1465 if (cb != NULL)
1466 cb->buf_get = cb->buf_put;
1467 /* equivalent to a get of all data available */
1468}
1469
1470
1471/*
1472 * cypress_buf_data_avail
1473 *
1474 * Return the number of bytes of data available in the circular
1475 * buffer.
1476 */
1477
1478static unsigned int cypress_buf_data_avail(struct cypress_buf *cb)
1479{
1480 if (cb != NULL)
Alan Cox813a2242008-07-22 11:10:36 +01001481 return (cb->buf_size + cb->buf_put - cb->buf_get)
1482 % cb->buf_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 else
1484 return 0;
1485}
1486
1487
1488/*
1489 * cypress_buf_space_avail
1490 *
1491 * Return the number of bytes of space available in the circular
1492 * buffer.
1493 */
1494
1495static unsigned int cypress_buf_space_avail(struct cypress_buf *cb)
1496{
1497 if (cb != NULL)
Alan Cox813a2242008-07-22 11:10:36 +01001498 return (cb->buf_size + cb->buf_get - cb->buf_put - 1)
1499 % cb->buf_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 else
1501 return 0;
1502}
1503
1504
1505/*
1506 * cypress_buf_put
1507 *
1508 * Copy data data from a user buffer and put it into the circular buffer.
1509 * Restrict to the amount of space available.
1510 *
1511 * Return the number of bytes copied.
1512 */
1513
1514static unsigned int cypress_buf_put(struct cypress_buf *cb, const char *buf,
1515 unsigned int count)
1516{
1517
1518 unsigned int len;
1519
1520
1521 if (cb == NULL)
1522 return 0;
1523
1524 len = cypress_buf_space_avail(cb);
1525 if (count > len)
1526 count = len;
1527
1528 if (count == 0)
1529 return 0;
1530
1531 len = cb->buf_buf + cb->buf_size - cb->buf_put;
1532 if (count > len) {
1533 memcpy(cb->buf_put, buf, len);
1534 memcpy(cb->buf_buf, buf+len, count - len);
1535 cb->buf_put = cb->buf_buf + count - len;
1536 } else {
1537 memcpy(cb->buf_put, buf, count);
1538 if (count < len)
1539 cb->buf_put += count;
1540 else /* count == len */
1541 cb->buf_put = cb->buf_buf;
1542 }
1543
1544 return count;
1545
1546}
1547
1548
1549/*
1550 * cypress_buf_get
1551 *
1552 * Get data from the circular buffer and copy to the given buffer.
1553 * Restrict to the amount of data available.
1554 *
1555 * Return the number of bytes copied.
1556 */
1557
1558static unsigned int cypress_buf_get(struct cypress_buf *cb, char *buf,
1559 unsigned int count)
1560{
1561
1562 unsigned int len;
1563
1564
1565 if (cb == NULL)
1566 return 0;
1567
1568 len = cypress_buf_data_avail(cb);
1569 if (count > len)
1570 count = len;
1571
1572 if (count == 0)
1573 return 0;
1574
1575 len = cb->buf_buf + cb->buf_size - cb->buf_get;
1576 if (count > len) {
1577 memcpy(buf, cb->buf_get, len);
1578 memcpy(buf+len, cb->buf_buf, count - len);
1579 cb->buf_get = cb->buf_buf + count - len;
1580 } else {
1581 memcpy(buf, cb->buf_get, count);
1582 if (count < len)
1583 cb->buf_get += count;
1584 else /* count == len */
1585 cb->buf_get = cb->buf_buf;
1586 }
1587
1588 return count;
1589
1590}
1591
1592/*****************************************************************************
1593 * Module functions
1594 *****************************************************************************/
1595
1596static int __init cypress_init(void)
1597{
1598 int retval;
Alan Cox813a2242008-07-22 11:10:36 +01001599
Harvey Harrison441b62c2008-03-03 16:08:34 -08001600 dbg("%s", __func__);
Alan Cox813a2242008-07-22 11:10:36 +01001601
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 retval = usb_serial_register(&cypress_earthmate_device);
1603 if (retval)
1604 goto failed_em_register;
1605 retval = usb_serial_register(&cypress_hidcom_device);
1606 if (retval)
1607 goto failed_hidcom_register;
Lonnie Mendeza5c44e22006-03-01 10:45:24 -06001608 retval = usb_serial_register(&cypress_ca42v2_device);
1609 if (retval)
1610 goto failed_ca42v2_register;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 retval = usb_register(&cypress_driver);
1612 if (retval)
1613 goto failed_usb_register;
1614
Greg Kroah-Hartmanc197a8d2008-08-18 13:21:04 -07001615 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
1616 DRIVER_DESC "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618
Mariusz Kozlowski2e46b742006-11-17 17:49:22 +01001619failed_usb_register:
1620 usb_serial_deregister(&cypress_ca42v2_device);
1621failed_ca42v2_register:
1622 usb_serial_deregister(&cypress_hidcom_device);
1623failed_hidcom_register:
1624 usb_serial_deregister(&cypress_earthmate_device);
1625failed_em_register:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 return retval;
1627}
1628
1629
Alan Cox813a2242008-07-22 11:10:36 +01001630static void __exit cypress_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631{
Harvey Harrison441b62c2008-03-03 16:08:34 -08001632 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
Alan Cox813a2242008-07-22 11:10:36 +01001634 usb_deregister(&cypress_driver);
1635 usb_serial_deregister(&cypress_earthmate_device);
1636 usb_serial_deregister(&cypress_hidcom_device);
1637 usb_serial_deregister(&cypress_ca42v2_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638}
1639
1640
1641module_init(cypress_init);
1642module_exit(cypress_exit);
1643
Alan Cox813a2242008-07-22 11:10:36 +01001644MODULE_AUTHOR(DRIVER_AUTHOR);
1645MODULE_DESCRIPTION(DRIVER_DESC);
1646MODULE_VERSION(DRIVER_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647MODULE_LICENSE("GPL");
1648
1649module_param(debug, bool, S_IRUGO | S_IWUSR);
1650MODULE_PARM_DESC(debug, "Debug enabled or not");
1651module_param(stats, bool, S_IRUGO | S_IWUSR);
1652MODULE_PARM_DESC(stats, "Enable statistics or not");
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001653module_param(interval, int, S_IRUGO | S_IWUSR);
1654MODULE_PARM_DESC(interval, "Overrides interrupt interval");