blob: 8c69bc53a083c8d19cf8b7934877dbad84c01988 [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
Mike Frysinger64319dd2009-12-18 16:33:01 -050073static int debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074static int stats;
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -050075static int interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77/*
78 * Version Information
79 */
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -050080#define DRIVER_VERSION "v1.09"
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#define DRIVER_AUTHOR "Lonnie Mendez <dignome@gmail.com>, Neil Whelchel <koyama@firstlight.net>"
82#define DRIVER_DESC "Cypress USB to Serial Driver"
83
84/* write buffer size defines */
85#define CYPRESS_BUF_SIZE 1024
86#define CYPRESS_CLOSING_WAIT (30*HZ)
87
88static struct usb_device_id id_table_earthmate [] = {
89 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB) },
Lonnie Mendez25b6f082005-05-10 17:09:52 -050090 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB_LT20) },
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 { } /* Terminating entry */
92};
93
94static struct usb_device_id id_table_cyphidcomrs232 [] = {
95 { USB_DEVICE(VENDOR_ID_CYPRESS, PRODUCT_ID_CYPHIDCOM) },
Dmitry Shapin6f6f06e2008-03-04 15:25:10 -080096 { USB_DEVICE(VENDOR_ID_POWERCOM, PRODUCT_ID_UPS) },
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 { } /* Terminating entry */
98};
99
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600100static struct usb_device_id id_table_nokiaca42v2 [] = {
101 { USB_DEVICE(VENDOR_ID_DAZZLE, PRODUCT_ID_CA42) },
102 { } /* Terminating entry */
103};
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105static struct usb_device_id id_table_combined [] = {
106 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB) },
Lonnie Mendez25b6f082005-05-10 17:09:52 -0500107 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB_LT20) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 { USB_DEVICE(VENDOR_ID_CYPRESS, PRODUCT_ID_CYPHIDCOM) },
Dmitry Shapin6f6f06e2008-03-04 15:25:10 -0800109 { USB_DEVICE(VENDOR_ID_POWERCOM, PRODUCT_ID_UPS) },
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600110 { USB_DEVICE(VENDOR_ID_DAZZLE, PRODUCT_ID_CA42) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 { } /* Terminating entry */
112};
113
Alan Cox813a2242008-07-22 11:10:36 +0100114MODULE_DEVICE_TABLE(usb, id_table_combined);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116static struct usb_driver cypress_driver = {
117 .name = "cypress",
118 .probe = usb_serial_probe,
119 .disconnect = usb_serial_disconnect,
120 .id_table = id_table_combined,
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800121 .no_dynamic_id = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122};
123
Mike Isely3416eaa2008-02-10 20:23:19 -0600124enum packet_format {
125 packet_format_1, /* b0:status, b1:payload count */
126 packet_format_2 /* b0[7:3]:status, b0[2:0]:payload count */
127};
128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129struct cypress_private {
130 spinlock_t lock; /* private lock */
131 int chiptype; /* identifier of device, for quirks/etc */
132 int bytes_in; /* used for statistics */
133 int bytes_out; /* used for statistics */
134 int cmd_count; /* used for statistics */
135 int cmd_ctrl; /* always set this to 1 before issuing a command */
136 struct cypress_buf *buf; /* write buffer */
137 int write_urb_in_use; /* write urb in use indicator */
Mike Isely0257fa92006-08-29 22:06:59 -0500138 int write_urb_interval; /* interval to use for write urb */
139 int read_urb_interval; /* interval to use for read urb */
Mike Isely78aef512006-08-29 22:07:11 -0500140 int comm_is_ok; /* true if communication is (still) ok */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 int termios_initialized;
142 __u8 line_control; /* holds dtr / rts value */
143 __u8 current_status; /* received from last read - info on dsr,cts,cd,ri,etc */
144 __u8 current_config; /* stores the current configuration byte */
145 __u8 rx_flags; /* throttling - used from whiteheat/ftdi_sio */
Mike Isely3416eaa2008-02-10 20:23:19 -0600146 enum packet_format pkt_fmt; /* format to use for packet send / receive */
Mike Isely3d6aa322008-02-10 20:23:24 -0600147 int get_cfg_unsafe; /* If true, the CYPRESS_GET_CONFIG is unsafe */
Alan Cox813a2242008-07-22 11:10:36 +0100148 int baud_rate; /* stores current baud rate in
149 integer form */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 int isthrottled; /* if throttled, discard reads */
151 wait_queue_head_t delta_msr_wait; /* used for TIOCMIWAIT */
152 char prev_status, diff_status; /* used for TIOCMIWAIT */
Alan Cox813a2242008-07-22 11:10:36 +0100153 /* we pass a pointer to this as the arguement sent to
154 cypress_set_termios old_termios */
Alan Cox606d0992006-12-08 02:38:45 -0800155 struct ktermios tmp_termios; /* stores the old termios settings */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156};
157
158/* write buffer structure */
159struct cypress_buf {
160 unsigned int buf_size;
161 char *buf_buf;
162 char *buf_get;
163 char *buf_put;
164};
165
166/* function prototypes for the Cypress USB to serial device */
Alan Cox813a2242008-07-22 11:10:36 +0100167static int cypress_earthmate_startup(struct usb_serial *serial);
168static int cypress_hidcom_startup(struct usb_serial *serial);
169static int cypress_ca42v2_startup(struct usb_serial *serial);
Alan Sternf9c99bb2009-06-02 11:53:55 -0400170static void cypress_release(struct usb_serial *serial);
Alan Coxa509a7e2009-09-19 13:13:26 -0700171static int cypress_open(struct tty_struct *tty, struct usb_serial_port *port);
Alan Cox335f8512009-06-11 12:26:29 +0100172static void cypress_close(struct usb_serial_port *port);
173static void cypress_dtr_rts(struct usb_serial_port *port, int on);
Alan Cox813a2242008-07-22 11:10:36 +0100174static int cypress_write(struct tty_struct *tty, struct usb_serial_port *port,
175 const unsigned char *buf, int count);
176static void cypress_send(struct usb_serial_port *port);
177static int cypress_write_room(struct tty_struct *tty);
178static int cypress_ioctl(struct tty_struct *tty, struct file *file,
179 unsigned int cmd, unsigned long arg);
180static void cypress_set_termios(struct tty_struct *tty,
181 struct usb_serial_port *port, struct ktermios *old);
182static int cypress_tiocmget(struct tty_struct *tty, struct file *file);
183static int cypress_tiocmset(struct tty_struct *tty, struct file *file,
184 unsigned int set, unsigned int clear);
185static int cypress_chars_in_buffer(struct tty_struct *tty);
186static void cypress_throttle(struct tty_struct *tty);
187static void cypress_unthrottle(struct tty_struct *tty);
188static void cypress_set_dead(struct usb_serial_port *port);
189static void cypress_read_int_callback(struct urb *urb);
190static void cypress_write_int_callback(struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191/* write buffer functions */
192static struct cypress_buf *cypress_buf_alloc(unsigned int size);
Alan Cox813a2242008-07-22 11:10:36 +0100193static void cypress_buf_free(struct cypress_buf *cb);
194static void cypress_buf_clear(struct cypress_buf *cb);
195static unsigned int cypress_buf_data_avail(struct cypress_buf *cb);
196static unsigned int cypress_buf_space_avail(struct cypress_buf *cb);
197static unsigned int cypress_buf_put(struct cypress_buf *cb,
198 const char *buf, unsigned int count);
199static unsigned int cypress_buf_get(struct cypress_buf *cb,
200 char *buf, unsigned int count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700203static struct usb_serial_driver cypress_earthmate_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700204 .driver = {
205 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700206 .name = "earthmate",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700207 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700208 .description = "DeLorme Earthmate USB",
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100209 .usb_driver = &cypress_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 .id_table = id_table_earthmate,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 .num_ports = 1,
212 .attach = cypress_earthmate_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -0400213 .release = cypress_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 .open = cypress_open,
215 .close = cypress_close,
Alan Cox335f8512009-06-11 12:26:29 +0100216 .dtr_rts = cypress_dtr_rts,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 .write = cypress_write,
218 .write_room = cypress_write_room,
219 .ioctl = cypress_ioctl,
220 .set_termios = cypress_set_termios,
221 .tiocmget = cypress_tiocmget,
222 .tiocmset = cypress_tiocmset,
223 .chars_in_buffer = cypress_chars_in_buffer,
224 .throttle = cypress_throttle,
225 .unthrottle = cypress_unthrottle,
226 .read_int_callback = cypress_read_int_callback,
227 .write_int_callback = cypress_write_int_callback,
228};
229
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700230static struct usb_serial_driver cypress_hidcom_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700231 .driver = {
232 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700233 .name = "cyphidcom",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700234 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700235 .description = "HID->COM RS232 Adapter",
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100236 .usb_driver = &cypress_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 .id_table = id_table_cyphidcomrs232,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 .num_ports = 1,
239 .attach = cypress_hidcom_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -0400240 .release = cypress_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 .open = cypress_open,
242 .close = cypress_close,
Alan Cox335f8512009-06-11 12:26:29 +0100243 .dtr_rts = cypress_dtr_rts,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 .write = cypress_write,
245 .write_room = cypress_write_room,
246 .ioctl = cypress_ioctl,
247 .set_termios = cypress_set_termios,
248 .tiocmget = cypress_tiocmget,
249 .tiocmset = cypress_tiocmset,
250 .chars_in_buffer = cypress_chars_in_buffer,
251 .throttle = cypress_throttle,
252 .unthrottle = cypress_unthrottle,
253 .read_int_callback = cypress_read_int_callback,
254 .write_int_callback = cypress_write_int_callback,
255};
256
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600257static struct usb_serial_driver cypress_ca42v2_device = {
258 .driver = {
259 .owner = THIS_MODULE,
Alan Cox813a2242008-07-22 11:10:36 +0100260 .name = "nokiaca42v2",
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600261 },
262 .description = "Nokia CA-42 V2 Adapter",
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100263 .usb_driver = &cypress_driver,
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600264 .id_table = id_table_nokiaca42v2,
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600265 .num_ports = 1,
266 .attach = cypress_ca42v2_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -0400267 .release = cypress_release,
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600268 .open = cypress_open,
269 .close = cypress_close,
Alan Cox335f8512009-06-11 12:26:29 +0100270 .dtr_rts = cypress_dtr_rts,
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600271 .write = cypress_write,
272 .write_room = cypress_write_room,
273 .ioctl = cypress_ioctl,
274 .set_termios = cypress_set_termios,
275 .tiocmget = cypress_tiocmget,
276 .tiocmset = cypress_tiocmset,
277 .chars_in_buffer = cypress_chars_in_buffer,
278 .throttle = cypress_throttle,
279 .unthrottle = cypress_unthrottle,
280 .read_int_callback = cypress_read_int_callback,
281 .write_int_callback = cypress_write_int_callback,
282};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284/*****************************************************************************
285 * Cypress serial helper functions
286 *****************************************************************************/
287
288
Alan Cox8873aaa2008-03-10 21:59:28 +0000289static int analyze_baud_rate(struct usb_serial_port *port, speed_t new_rate)
Mike Isely92983c22008-02-10 20:23:32 -0600290{
Mike Isely92983c22008-02-10 20:23:32 -0600291 struct cypress_private *priv;
292 priv = usb_get_serial_port_data(port);
293
294 /*
295 * The general purpose firmware for the Cypress M8 allows for
296 * a maximum speed of 57600bps (I have no idea whether DeLorme
297 * chose to use the general purpose firmware or not), if you
298 * need to modify this speed setting for your own project
299 * please add your own chiptype and modify the code likewise.
300 * The Cypress HID->COM device will work successfully up to
301 * 115200bps (but the actual throughput is around 3kBps).
302 */
Mike Isely92983c22008-02-10 20:23:32 -0600303 if (port->serial->dev->speed == USB_SPEED_LOW) {
304 /*
305 * Mike Isely <isely@pobox.com> 2-Feb-2008: The
306 * Cypress app note that describes this mechanism
307 * states the the low-speed part can't handle more
308 * than 800 bytes/sec, in which case 4800 baud is the
309 * safest speed for a part like that.
310 */
311 if (new_rate > 4800) {
312 dbg("%s - failed setting baud rate, device incapable "
313 "speed %d", __func__, new_rate);
314 return -1;
315 }
316 }
317 switch (priv->chiptype) {
318 case CT_EARTHMATE:
319 if (new_rate <= 600) {
320 /* 300 and 600 baud rates are supported under
321 * the generic firmware, but are not used with
322 * NMEA and SiRF protocols */
323 dbg("%s - failed setting baud rate, unsupported speed "
324 "of %d on Earthmate GPS", __func__, new_rate);
325 return -1;
326 }
327 break;
328 default:
329 break;
330 }
331 return new_rate;
332}
333
334
Steven Cole093cf722005-05-03 19:07:24 -0600335/* This function can either set or retrieve the current serial line settings */
Alan Cox813a2242008-07-22 11:10:36 +0100336static int cypress_serial_control(struct tty_struct *tty,
Alan Cox95da3102008-07-22 11:09:07 +0100337 struct usb_serial_port *port, speed_t baud_rate, int data_bits,
338 int stop_bits, int parity_enable, int parity_type, int reset,
339 int cypress_request_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500341 int new_baudrate = 0, retval = 0, tries = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 struct cypress_private *priv;
Mike Isely93075542008-02-10 20:23:14 -0600343 __u8 feature_buffer[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 unsigned long flags;
345
Harvey Harrison441b62c2008-03-03 16:08:34 -0800346 dbg("%s", __func__);
Alan Cox813a2242008-07-22 11:10:36 +0100347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 priv = usb_get_serial_port_data(port);
349
Mike Isely78aef512006-08-29 22:07:11 -0500350 if (!priv->comm_is_ok)
351 return -ENODEV;
352
Alan Cox813a2242008-07-22 11:10:36 +0100353 switch (cypress_request_type) {
354 case CYPRESS_SET_CONFIG:
355 new_baudrate = priv->baud_rate;
356 /* 0 means 'Hang up' so doesn't change the true bit rate */
357 if (baud_rate == 0)
Mike Isely92983c22008-02-10 20:23:32 -0600358 new_baudrate = priv->baud_rate;
Alan Cox813a2242008-07-22 11:10:36 +0100359 /* Change of speed ? */
360 else if (baud_rate != priv->baud_rate) {
361 dbg("%s - baud rate is changing", __func__);
362 retval = analyze_baud_rate(port, baud_rate);
363 if (retval >= 0) {
364 new_baudrate = retval;
365 dbg("%s - New baud rate set to %d",
366 __func__, new_baudrate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 }
Alan Cox813a2242008-07-22 11:10:36 +0100368 }
369 dbg("%s - baud rate is being sent as %d",
370 __func__, new_baudrate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Alan Cox813a2242008-07-22 11:10:36 +0100372 memset(feature_buffer, 0, sizeof(feature_buffer));
373 /* fill the feature_buffer with new configuration */
374 *((u_int32_t *)feature_buffer) = new_baudrate;
375 feature_buffer[4] |= data_bits; /* assign data bits in 2 bit space ( max 3 ) */
376 /* 1 bit gap */
377 feature_buffer[4] |= (stop_bits << 3); /* assign stop bits in 1 bit space */
378 feature_buffer[4] |= (parity_enable << 4); /* assign parity flag in 1 bit space */
379 feature_buffer[4] |= (parity_type << 5); /* assign parity type in 1 bit space */
380 /* 1 bit gap */
381 feature_buffer[4] |= (reset << 7); /* assign reset at end of byte, 1 bit space */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Alan Cox813a2242008-07-22 11:10:36 +0100383 dbg("%s - device is being sent this feature report:",
384 __func__);
385 dbg("%s - %02X - %02X - %02X - %02X - %02X", __func__,
386 feature_buffer[0], feature_buffer[1],
387 feature_buffer[2], feature_buffer[3],
388 feature_buffer[4]);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500389
Alan Cox813a2242008-07-22 11:10:36 +0100390 do {
391 retval = usb_control_msg(port->serial->dev,
392 usb_sndctrlpipe(port->serial->dev, 0),
393 HID_REQ_SET_REPORT,
394 USB_DIR_OUT | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
395 0x0300, 0, feature_buffer,
396 sizeof(feature_buffer), 500);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500397
Alan Cox813a2242008-07-22 11:10:36 +0100398 if (tries++ >= 3)
399 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Alan Cox813a2242008-07-22 11:10:36 +0100401 } while (retval != sizeof(feature_buffer) &&
402 retval != -ENODEV);
Mike Isely93075542008-02-10 20:23:14 -0600403
Alan Cox813a2242008-07-22 11:10:36 +0100404 if (retval != sizeof(feature_buffer)) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700405 dev_err(&port->dev, "%s - failed sending serial "
406 "line settings - %d\n", __func__, retval);
Alan Cox813a2242008-07-22 11:10:36 +0100407 cypress_set_dead(port);
408 } else {
409 spin_lock_irqsave(&priv->lock, flags);
410 priv->baud_rate = new_baudrate;
411 priv->current_config = feature_buffer[4];
412 spin_unlock_irqrestore(&priv->lock, flags);
413 /* If we asked for a speed change encode it */
414 if (baud_rate)
415 tty_encode_baud_rate(tty,
416 new_baudrate, new_baudrate);
417 }
418 break;
419 case CYPRESS_GET_CONFIG:
420 if (priv->get_cfg_unsafe) {
421 /* Not implemented for this device,
422 and if we try to do it we're likely
423 to crash the hardware. */
424 return -ENOTTY;
425 }
426 dbg("%s - retreiving serial line settings", __func__);
427 /* set initial values in feature buffer */
428 memset(feature_buffer, 0, sizeof(feature_buffer));
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500429
Alan Cox813a2242008-07-22 11:10:36 +0100430 do {
431 retval = usb_control_msg(port->serial->dev,
432 usb_rcvctrlpipe(port->serial->dev, 0),
433 HID_REQ_GET_REPORT,
434 USB_DIR_IN | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
435 0x0300, 0, feature_buffer,
436 sizeof(feature_buffer), 500);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500437
Alan Cox813a2242008-07-22 11:10:36 +0100438 if (tries++ >= 3)
439 break;
440 } while (retval != sizeof(feature_buffer)
441 && retval != -ENODEV);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500442
Alan Cox813a2242008-07-22 11:10:36 +0100443 if (retval != sizeof(feature_buffer)) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700444 dev_err(&port->dev, "%s - failed to retrieve serial "
445 "line settings - %d\n", __func__, retval);
Alan Cox813a2242008-07-22 11:10:36 +0100446 cypress_set_dead(port);
447 return retval;
448 } else {
449 spin_lock_irqsave(&priv->lock, flags);
450 /* store the config in one byte, and later
451 use bit masks to check values */
452 priv->current_config = feature_buffer[4];
453 priv->baud_rate = *((u_int32_t *)feature_buffer);
454 spin_unlock_irqrestore(&priv->lock, flags);
455 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 }
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500457 spin_lock_irqsave(&priv->lock, flags);
458 ++priv->cmd_count;
459 spin_unlock_irqrestore(&priv->lock, flags);
460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 return retval;
462} /* cypress_serial_control */
463
464
Mike Isely78aef512006-08-29 22:07:11 -0500465static void cypress_set_dead(struct usb_serial_port *port)
466{
467 struct cypress_private *priv = usb_get_serial_port_data(port);
468 unsigned long flags;
469
470 spin_lock_irqsave(&priv->lock, flags);
471 if (!priv->comm_is_ok) {
472 spin_unlock_irqrestore(&priv->lock, flags);
473 return;
474 }
475 priv->comm_is_ok = 0;
476 spin_unlock_irqrestore(&priv->lock, flags);
477
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700478 dev_err(&port->dev, "cypress_m8 suspending failing port %d - "
479 "interval might be too short\n", port->number);
Mike Isely78aef512006-08-29 22:07:11 -0500480}
481
482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483/*****************************************************************************
484 * Cypress serial driver functions
485 *****************************************************************************/
486
487
Alan Cox813a2242008-07-22 11:10:36 +0100488static int generic_startup(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
490 struct cypress_private *priv;
Mike Isely0257fa92006-08-29 22:06:59 -0500491 struct usb_serial_port *port = serial->port[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Harvey Harrison441b62c2008-03-03 16:08:34 -0800493 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Alan Cox813a2242008-07-22 11:10:36 +0100495 priv = kzalloc(sizeof(struct cypress_private), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 if (!priv)
497 return -ENOMEM;
498
Mike Isely78aef512006-08-29 22:07:11 -0500499 priv->comm_is_ok = !0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 spin_lock_init(&priv->lock);
501 priv->buf = cypress_buf_alloc(CYPRESS_BUF_SIZE);
502 if (priv->buf == NULL) {
503 kfree(priv);
504 return -ENOMEM;
505 }
506 init_waitqueue_head(&priv->delta_msr_wait);
Alan Cox813a2242008-07-22 11:10:36 +0100507
508 usb_reset_configuration(serial->dev);
509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 priv->cmd_ctrl = 0;
511 priv->line_control = 0;
512 priv->termios_initialized = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 priv->rx_flags = 0;
Mike Isely3416eaa2008-02-10 20:23:19 -0600514 /* Default packet format setting is determined by packet size.
515 Anything with a size larger then 9 must have a separate
516 count field since the 3 bit count field is otherwise too
517 small. Otherwise we can use the slightly more compact
518 format. This is in accordance with the cypress_m8 serial
519 converter app note. */
Alan Cox813a2242008-07-22 11:10:36 +0100520 if (port->interrupt_out_size > 9)
Mike Isely3416eaa2008-02-10 20:23:19 -0600521 priv->pkt_fmt = packet_format_1;
Alan Cox813a2242008-07-22 11:10:36 +0100522 else
Mike Isely3416eaa2008-02-10 20:23:19 -0600523 priv->pkt_fmt = packet_format_2;
Alan Cox813a2242008-07-22 11:10:36 +0100524
Mike Isely0257fa92006-08-29 22:06:59 -0500525 if (interval > 0) {
526 priv->write_urb_interval = interval;
527 priv->read_urb_interval = interval;
528 dbg("%s - port %d read & write intervals forced to %d",
Alan Cox813a2242008-07-22 11:10:36 +0100529 __func__, port->number, interval);
Mike Isely0257fa92006-08-29 22:06:59 -0500530 } else {
531 priv->write_urb_interval = port->interrupt_out_urb->interval;
532 priv->read_urb_interval = port->interrupt_in_urb->interval;
533 dbg("%s - port %d intervals: read=%d write=%d",
Alan Cox813a2242008-07-22 11:10:36 +0100534 __func__, port->number,
535 priv->read_urb_interval, priv->write_urb_interval);
Mike Isely0257fa92006-08-29 22:06:59 -0500536 }
537 usb_set_serial_port_data(port, priv);
Alan Cox813a2242008-07-22 11:10:36 +0100538
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500539 return 0;
540}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
542
Alan Cox813a2242008-07-22 11:10:36 +0100543static int cypress_earthmate_startup(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
545 struct cypress_private *priv;
Mike Isely3d6aa322008-02-10 20:23:24 -0600546 struct usb_serial_port *port = serial->port[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Harvey Harrison441b62c2008-03-03 16:08:34 -0800548 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550 if (generic_startup(serial)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800551 dbg("%s - Failed setting up port %d", __func__,
Mike Isely3d6aa322008-02-10 20:23:24 -0600552 port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 return 1;
554 }
555
Mike Isely3d6aa322008-02-10 20:23:24 -0600556 priv = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 priv->chiptype = CT_EARTHMATE;
Mike Isely3416eaa2008-02-10 20:23:19 -0600558 /* All Earthmate devices use the separated-count packet
559 format! Idiotic. */
560 priv->pkt_fmt = packet_format_1;
Alan Cox813a2242008-07-22 11:10:36 +0100561 if (serial->dev->descriptor.idProduct !=
562 cpu_to_le16(PRODUCT_ID_EARTHMATEUSB)) {
Mike Isely3d6aa322008-02-10 20:23:24 -0600563 /* The old original USB Earthmate seemed able to
564 handle GET_CONFIG requests; everything they've
565 produced since that time crashes if this command is
566 attempted :-( */
567 dbg("%s - Marking this device as unsafe for GET_CONFIG "
568 "commands", __func__);
569 priv->get_cfg_unsafe = !0;
570 }
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500571
572 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573} /* cypress_earthmate_startup */
574
575
Alan Cox813a2242008-07-22 11:10:36 +0100576static int cypress_hidcom_startup(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577{
578 struct cypress_private *priv;
579
Harvey Harrison441b62c2008-03-03 16:08:34 -0800580 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582 if (generic_startup(serial)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800583 dbg("%s - Failed setting up port %d", __func__,
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500584 serial->port[0]->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 return 1;
586 }
587
588 priv = usb_get_serial_port_data(serial->port[0]);
589 priv->chiptype = CT_CYPHIDCOM;
Alan Cox813a2242008-07-22 11:10:36 +0100590
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500591 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592} /* cypress_hidcom_startup */
593
594
Alan Cox813a2242008-07-22 11:10:36 +0100595static int cypress_ca42v2_startup(struct usb_serial *serial)
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600596{
597 struct cypress_private *priv;
598
Harvey Harrison441b62c2008-03-03 16:08:34 -0800599 dbg("%s", __func__);
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600600
601 if (generic_startup(serial)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800602 dbg("%s - Failed setting up port %d", __func__,
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600603 serial->port[0]->number);
604 return 1;
605 }
606
607 priv = usb_get_serial_port_data(serial->port[0]);
608 priv->chiptype = CT_CA42V2;
609
610 return 0;
611} /* cypress_ca42v2_startup */
612
613
Alan Sternf9c99bb2009-06-02 11:53:55 -0400614static void cypress_release(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
616 struct cypress_private *priv;
617
Alan Cox813a2242008-07-22 11:10:36 +0100618 dbg("%s - port %d", __func__, serial->port[0]->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
620 /* all open ports are closed at this point */
621
622 priv = usb_get_serial_port_data(serial->port[0]);
623
624 if (priv) {
625 cypress_buf_free(priv->buf);
626 kfree(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 }
628}
629
630
Alan Coxa509a7e2009-09-19 13:13:26 -0700631static int cypress_open(struct tty_struct *tty, struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
633 struct cypress_private *priv = usb_get_serial_port_data(port);
634 struct usb_serial *serial = port->serial;
635 unsigned long flags;
636 int result = 0;
637
Harvey Harrison441b62c2008-03-03 16:08:34 -0800638 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Mike Isely78aef512006-08-29 22:07:11 -0500640 if (!priv->comm_is_ok)
641 return -EIO;
642
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 /* clear halts before open */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 usb_clear_halt(serial->dev, 0x81);
645 usb_clear_halt(serial->dev, 0x02);
646
647 spin_lock_irqsave(&priv->lock, flags);
648 /* reset read/write statistics */
649 priv->bytes_in = 0;
650 priv->bytes_out = 0;
651 priv->cmd_count = 0;
652 priv->rx_flags = 0;
653 spin_unlock_irqrestore(&priv->lock, flags);
654
Alan Cox335f8512009-06-11 12:26:29 +0100655 /* Set termios */
Alan Coxfe1ae7f2009-09-19 13:13:33 -0700656 cypress_send(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Alan Cox95da3102008-07-22 11:09:07 +0100658 if (tty)
659 cypress_set_termios(tty, port, &priv->tmp_termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
661 /* setup the port and start reading from the device */
Alan Cox813a2242008-07-22 11:10:36 +0100662 if (!port->interrupt_in_urb) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700663 dev_err(&port->dev, "%s - interrupt_in_urb is empty!\n",
664 __func__);
Alan Cox813a2242008-07-22 11:10:36 +0100665 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 }
667
668 usb_fill_int_urb(port->interrupt_in_urb, serial->dev,
669 usb_rcvintpipe(serial->dev, port->interrupt_in_endpointAddress),
Alan Cox813a2242008-07-22 11:10:36 +0100670 port->interrupt_in_urb->transfer_buffer,
671 port->interrupt_in_urb->transfer_buffer_length,
Mike Isely0257fa92006-08-29 22:06:59 -0500672 cypress_read_int_callback, port, priv->read_urb_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
674
Alan Cox813a2242008-07-22 11:10:36 +0100675 if (result) {
676 dev_err(&port->dev,
677 "%s - failed submitting read urb, error %d\n",
678 __func__, result);
Mike Isely78aef512006-08-29 22:07:11 -0500679 cypress_set_dead(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 }
Alan Cox335f8512009-06-11 12:26:29 +0100681 port->port.drain_delay = 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 return result;
683} /* cypress_open */
684
Alan Cox335f8512009-06-11 12:26:29 +0100685static void cypress_dtr_rts(struct usb_serial_port *port, int on)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
687 struct cypress_private *priv = usb_get_serial_port_data(port);
Alan Cox335f8512009-06-11 12:26:29 +0100688 /* drop dtr and rts */
689 priv = usb_get_serial_port_data(port);
690 spin_lock_irq(&priv->lock);
691 if (on == 0)
692 priv->line_control = 0;
693 else
694 priv->line_control = CONTROL_DTR | CONTROL_RTS;
695 priv->cmd_ctrl = 1;
696 spin_unlock_irq(&priv->lock);
697 cypress_write(NULL, port, NULL, 0);
698}
699
700static void cypress_close(struct usb_serial_port *port)
701{
702 struct cypress_private *priv = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Harvey Harrison441b62c2008-03-03 16:08:34 -0800704 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100706 /* writing is potentially harmful, lock must be taken */
707 mutex_lock(&port->serial->disc_mutex);
708 if (port->serial->disconnected) {
709 mutex_unlock(&port->serial->disc_mutex);
710 return;
711 }
Alan Cox335f8512009-06-11 12:26:29 +0100712 cypress_buf_clear(priv->buf);
Harvey Harrison441b62c2008-03-03 16:08:34 -0800713 dbg("%s - stopping urbs", __func__);
Alan Cox813a2242008-07-22 11:10:36 +0100714 usb_kill_urb(port->interrupt_in_urb);
715 usb_kill_urb(port->interrupt_out_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
718 if (stats)
Alan Cox813a2242008-07-22 11:10:36 +0100719 dev_info(&port->dev, "Statistics: %d Bytes In | %d Bytes Out | %d Commands Issued\n",
720 priv->bytes_in, priv->bytes_out, priv->cmd_count);
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100721 mutex_unlock(&port->serial->disc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722} /* cypress_close */
723
724
Alan Cox95da3102008-07-22 11:09:07 +0100725static int cypress_write(struct tty_struct *tty, struct usb_serial_port *port,
726 const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727{
728 struct cypress_private *priv = usb_get_serial_port_data(port);
729 unsigned long flags;
Alan Cox813a2242008-07-22 11:10:36 +0100730
Harvey Harrison441b62c2008-03-03 16:08:34 -0800731 dbg("%s - port %d, %d bytes", __func__, port->number, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
733 /* line control commands, which need to be executed immediately,
734 are not put into the buffer for obvious reasons.
735 */
736 if (priv->cmd_ctrl) {
737 count = 0;
738 goto finish;
739 }
Alan Cox813a2242008-07-22 11:10:36 +0100740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 if (!count)
742 return count;
Alan Cox813a2242008-07-22 11:10:36 +0100743
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 spin_lock_irqsave(&priv->lock, flags);
745 count = cypress_buf_put(priv->buf, buf, count);
746 spin_unlock_irqrestore(&priv->lock, flags);
747
748finish:
749 cypress_send(port);
750
751 return count;
752} /* cypress_write */
753
754
755static void cypress_send(struct usb_serial_port *port)
756{
757 int count = 0, result, offset, actual_size;
758 struct cypress_private *priv = usb_get_serial_port_data(port);
759 unsigned long flags;
Alan Cox813a2242008-07-22 11:10:36 +0100760
Mike Isely78aef512006-08-29 22:07:11 -0500761 if (!priv->comm_is_ok)
762 return;
763
Harvey Harrison441b62c2008-03-03 16:08:34 -0800764 dbg("%s - port %d", __func__, port->number);
Alan Cox813a2242008-07-22 11:10:36 +0100765 dbg("%s - interrupt out size is %d", __func__,
766 port->interrupt_out_size);
767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 spin_lock_irqsave(&priv->lock, flags);
769 if (priv->write_urb_in_use) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800770 dbg("%s - can't write, urb in use", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 spin_unlock_irqrestore(&priv->lock, flags);
772 return;
773 }
774 spin_unlock_irqrestore(&priv->lock, flags);
775
776 /* clear buffer */
Alan Cox813a2242008-07-22 11:10:36 +0100777 memset(port->interrupt_out_urb->transfer_buffer, 0,
778 port->interrupt_out_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
780 spin_lock_irqsave(&priv->lock, flags);
Mike Isely3416eaa2008-02-10 20:23:19 -0600781 switch (priv->pkt_fmt) {
782 default:
783 case packet_format_1:
784 /* this is for the CY7C64013... */
785 offset = 2;
786 port->interrupt_out_buffer[0] = priv->line_control;
787 break;
788 case packet_format_2:
789 /* this is for the CY7C63743... */
790 offset = 1;
791 port->interrupt_out_buffer[0] = priv->line_control;
792 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 }
794
795 if (priv->line_control & CONTROL_RESET)
796 priv->line_control &= ~CONTROL_RESET;
797
798 if (priv->cmd_ctrl) {
799 priv->cmd_count++;
Harvey Harrison441b62c2008-03-03 16:08:34 -0800800 dbg("%s - line control command being issued", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 spin_unlock_irqrestore(&priv->lock, flags);
802 goto send;
803 } else
804 spin_unlock_irqrestore(&priv->lock, flags);
805
806 count = cypress_buf_get(priv->buf, &port->interrupt_out_buffer[offset],
807 port->interrupt_out_size-offset);
808
Alan Cox813a2242008-07-22 11:10:36 +0100809 if (count == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Mike Isely3416eaa2008-02-10 20:23:19 -0600812 switch (priv->pkt_fmt) {
813 default:
814 case packet_format_1:
815 port->interrupt_out_buffer[1] = count;
816 break;
817 case packet_format_2:
818 port->interrupt_out_buffer[0] |= count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 }
820
Harvey Harrison441b62c2008-03-03 16:08:34 -0800821 dbg("%s - count is %d", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
823send:
824 spin_lock_irqsave(&priv->lock, flags);
825 priv->write_urb_in_use = 1;
826 spin_unlock_irqrestore(&priv->lock, flags);
827
828 if (priv->cmd_ctrl)
829 actual_size = 1;
830 else
Mike Isely3416eaa2008-02-10 20:23:19 -0600831 actual_size = count +
832 (priv->pkt_fmt == packet_format_1 ? 2 : 1);
833
Alan Cox813a2242008-07-22 11:10:36 +0100834 usb_serial_debug_data(debug, &port->dev, __func__,
835 port->interrupt_out_size,
836 port->interrupt_out_urb->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Mike Isely9aa8dae2006-08-29 22:07:04 -0500838 usb_fill_int_urb(port->interrupt_out_urb, port->serial->dev,
839 usb_sndintpipe(port->serial->dev, port->interrupt_out_endpointAddress),
840 port->interrupt_out_buffer, port->interrupt_out_size,
841 cypress_write_int_callback, port, priv->write_urb_interval);
Alan Cox813a2242008-07-22 11:10:36 +0100842 result = usb_submit_urb(port->interrupt_out_urb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 if (result) {
Alan Cox813a2242008-07-22 11:10:36 +0100844 dev_err(&port->dev,
845 "%s - failed submitting write urb, error %d\n",
846 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 priv->write_urb_in_use = 0;
Mike Isely78aef512006-08-29 22:07:11 -0500848 cypress_set_dead(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 }
850
851 spin_lock_irqsave(&priv->lock, flags);
Alan Cox813a2242008-07-22 11:10:36 +0100852 if (priv->cmd_ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 priv->cmd_ctrl = 0;
Alan Cox813a2242008-07-22 11:10:36 +0100854
855 /* do not count the line control and size bytes */
856 priv->bytes_out += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 spin_unlock_irqrestore(&priv->lock, flags);
858
Pete Zaitcevcf2c7482006-05-22 21:58:49 -0700859 usb_serial_port_softint(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860} /* cypress_send */
861
862
863/* returns how much space is available in the soft buffer */
Alan Cox95da3102008-07-22 11:09:07 +0100864static int cypress_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865{
Alan Cox95da3102008-07-22 11:09:07 +0100866 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 struct cypress_private *priv = usb_get_serial_port_data(port);
868 int room = 0;
869 unsigned long flags;
870
Harvey Harrison441b62c2008-03-03 16:08:34 -0800871 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
873 spin_lock_irqsave(&priv->lock, flags);
874 room = cypress_buf_space_avail(priv->buf);
875 spin_unlock_irqrestore(&priv->lock, flags);
876
Harvey Harrison441b62c2008-03-03 16:08:34 -0800877 dbg("%s - returns %d", __func__, room);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 return room;
879}
880
881
Alan Cox95da3102008-07-22 11:09:07 +0100882static int cypress_tiocmget(struct tty_struct *tty, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883{
Alan Cox95da3102008-07-22 11:09:07 +0100884 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 struct cypress_private *priv = usb_get_serial_port_data(port);
886 __u8 status, control;
887 unsigned int result = 0;
888 unsigned long flags;
Alan Cox813a2242008-07-22 11:10:36 +0100889
Harvey Harrison441b62c2008-03-03 16:08:34 -0800890 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
892 spin_lock_irqsave(&priv->lock, flags);
893 control = priv->line_control;
894 status = priv->current_status;
895 spin_unlock_irqrestore(&priv->lock, flags);
896
897 result = ((control & CONTROL_DTR) ? TIOCM_DTR : 0)
898 | ((control & CONTROL_RTS) ? TIOCM_RTS : 0)
899 | ((status & UART_CTS) ? TIOCM_CTS : 0)
900 | ((status & UART_DSR) ? TIOCM_DSR : 0)
901 | ((status & UART_RI) ? TIOCM_RI : 0)
902 | ((status & UART_CD) ? TIOCM_CD : 0);
903
Harvey Harrison441b62c2008-03-03 16:08:34 -0800904 dbg("%s - result = %x", __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
906 return result;
907}
908
909
Alan Cox95da3102008-07-22 11:09:07 +0100910static int cypress_tiocmset(struct tty_struct *tty, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 unsigned int set, unsigned int clear)
912{
Alan Cox95da3102008-07-22 11:09:07 +0100913 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 struct cypress_private *priv = usb_get_serial_port_data(port);
915 unsigned long flags;
Alan Cox813a2242008-07-22 11:10:36 +0100916
Harvey Harrison441b62c2008-03-03 16:08:34 -0800917 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
919 spin_lock_irqsave(&priv->lock, flags);
920 if (set & TIOCM_RTS)
921 priv->line_control |= CONTROL_RTS;
922 if (set & TIOCM_DTR)
923 priv->line_control |= CONTROL_DTR;
924 if (clear & TIOCM_RTS)
925 priv->line_control &= ~CONTROL_RTS;
926 if (clear & TIOCM_DTR)
927 priv->line_control &= ~CONTROL_DTR;
Alan Cox8873aaa2008-03-10 21:59:28 +0000928 priv->cmd_ctrl = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 spin_unlock_irqrestore(&priv->lock, flags);
930
Alan Cox95da3102008-07-22 11:09:07 +0100931 return cypress_write(tty, port, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932}
933
934
Alan Cox813a2242008-07-22 11:10:36 +0100935static int cypress_ioctl(struct tty_struct *tty, struct file *file,
Alan Cox95da3102008-07-22 11:09:07 +0100936 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937{
Alan Cox95da3102008-07-22 11:09:07 +0100938 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 struct cypress_private *priv = usb_get_serial_port_data(port);
940
Harvey Harrison441b62c2008-03-03 16:08:34 -0800941 dbg("%s - port %d, cmd 0x%.4x", __func__, port->number, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
943 switch (cmd) {
Alan Cox813a2242008-07-22 11:10:36 +0100944 /* This code comes from drivers/char/serial.c and ftdi_sio.c */
945 case TIOCMIWAIT:
946 while (priv != NULL) {
947 interruptible_sleep_on(&priv->delta_msr_wait);
948 /* see if a signal did it */
949 if (signal_pending(current))
950 return -ERESTARTSYS;
951 else {
952 char diff = priv->diff_status;
953 if (diff == 0)
954 return -EIO; /* no change => error */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
Alan Cox813a2242008-07-22 11:10:36 +0100956 /* consume all events */
957 priv->diff_status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
Alan Cox813a2242008-07-22 11:10:36 +0100959 /* return 0 if caller wanted to know about
960 these bits */
961 if (((arg & TIOCM_RNG) && (diff & UART_RI)) ||
962 ((arg & TIOCM_DSR) && (diff & UART_DSR)) ||
963 ((arg & TIOCM_CD) && (diff & UART_CD)) ||
964 ((arg & TIOCM_CTS) && (diff & UART_CTS)))
965 return 0;
966 /* otherwise caller can't care less about what
967 * happened, and so we continue to wait for
968 * more events.
969 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 }
Alan Cox813a2242008-07-22 11:10:36 +0100971 }
972 return 0;
973 default:
974 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 }
Harvey Harrison441b62c2008-03-03 16:08:34 -0800976 dbg("%s - arg not supported - it was 0x%04x - check include/asm/ioctls.h", __func__, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 return -ENOIOCTLCMD;
978} /* cypress_ioctl */
979
980
Alan Cox95da3102008-07-22 11:09:07 +0100981static void cypress_set_termios(struct tty_struct *tty,
982 struct usb_serial_port *port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983{
984 struct cypress_private *priv = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 int data_bits, stop_bits, parity_type, parity_enable;
Alan Cox8873aaa2008-03-10 21:59:28 +0000986 unsigned cflag, iflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 unsigned long flags;
988 __u8 oldlines;
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500989 int linechange = 0;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500990
Harvey Harrison441b62c2008-03-03 16:08:34 -0800991 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 spin_lock_irqsave(&priv->lock, flags);
Alan Coxfe1ae7f2009-09-19 13:13:33 -0700994 /* We can't clean this one up as we don't know the device type
995 early enough */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 if (!priv->termios_initialized) {
997 if (priv->chiptype == CT_EARTHMATE) {
998 *(tty->termios) = tty_std_termios;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500999 tty->termios->c_cflag = B4800 | CS8 | CREAD | HUPCL |
1000 CLOCAL;
Alan Cox8873aaa2008-03-10 21:59:28 +00001001 tty->termios->c_ispeed = 4800;
1002 tty->termios->c_ospeed = 4800;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 } else if (priv->chiptype == CT_CYPHIDCOM) {
1004 *(tty->termios) = tty_std_termios;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001005 tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
1006 CLOCAL;
Alan Cox8873aaa2008-03-10 21:59:28 +00001007 tty->termios->c_ispeed = 9600;
1008 tty->termios->c_ospeed = 9600;
Lonnie Mendeza5c44e22006-03-01 10:45:24 -06001009 } else if (priv->chiptype == CT_CA42V2) {
1010 *(tty->termios) = tty_std_termios;
1011 tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
1012 CLOCAL;
Alan Cox8873aaa2008-03-10 21:59:28 +00001013 tty->termios->c_ispeed = 9600;
1014 tty->termios->c_ospeed = 9600;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 }
1016 priv->termios_initialized = 1;
1017 }
1018 spin_unlock_irqrestore(&priv->lock, flags);
1019
Alan Cox8873aaa2008-03-10 21:59:28 +00001020 /* Unsupported features need clearing */
1021 tty->termios->c_cflag &= ~(CMSPAR|CRTSCTS);
1022
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 cflag = tty->termios->c_cflag;
1024 iflag = tty->termios->c_iflag;
1025
1026 /* check if there are new settings */
1027 if (old_termios) {
Alan Cox8873aaa2008-03-10 21:59:28 +00001028 spin_lock_irqsave(&priv->lock, flags);
1029 priv->tmp_termios = *(tty->termios);
1030 spin_unlock_irqrestore(&priv->lock, flags);
1031 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
1033 /* set number of data bits, parity, stop bits */
1034 /* when parity is disabled the parity type bit is ignored */
1035
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001036 /* 1 means 2 stop bits, 0 means 1 stop bit */
1037 stop_bits = cflag & CSTOPB ? 1 : 0;
1038
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 if (cflag & PARENB) {
1040 parity_enable = 1;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001041 /* 1 means odd parity, 0 means even parity */
1042 parity_type = cflag & PARODD ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 } else
1044 parity_enable = parity_type = 0;
1045
Alan Cox77336822008-07-22 11:10:53 +01001046 switch (cflag & CSIZE) {
1047 case CS5:
1048 data_bits = 0;
1049 break;
1050 case CS6:
1051 data_bits = 1;
1052 break;
1053 case CS7:
1054 data_bits = 2;
1055 break;
1056 case CS8:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 data_bits = 3;
Alan Cox77336822008-07-22 11:10:53 +01001058 break;
1059 default:
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001060 dev_err(&port->dev, "%s - CSIZE was set, but not CS5-CS8\n",
1061 __func__);
Alan Cox77336822008-07-22 11:10:53 +01001062 data_bits = 3;
1063 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 spin_lock_irqsave(&priv->lock, flags);
1065 oldlines = priv->line_control;
1066 if ((cflag & CBAUD) == B0) {
1067 /* drop dtr and rts */
Harvey Harrison441b62c2008-03-03 16:08:34 -08001068 dbg("%s - dropping the lines, baud rate 0bps", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
Alan Cox8873aaa2008-03-10 21:59:28 +00001070 } else
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001071 priv->line_control = (CONTROL_DTR | CONTROL_RTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001074 dbg("%s - sending %d stop_bits, %d parity_enable, %d parity_type, "
Harvey Harrison441b62c2008-03-03 16:08:34 -08001075 "%d data_bits (+5)", __func__, stop_bits,
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001076 parity_enable, parity_type, data_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
Alan Cox813a2242008-07-22 11:10:36 +01001078 cypress_serial_control(tty, port, tty_get_baud_rate(tty),
1079 data_bits, stop_bits,
1080 parity_enable, parity_type,
1081 0, CYPRESS_SET_CONFIG);
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001082
1083 /* we perform a CYPRESS_GET_CONFIG so that the current settings are
1084 * filled into the private structure this should confirm that all is
1085 * working if it returns what we just set */
Alan Cox95da3102008-07-22 11:09:07 +01001086 cypress_serial_control(tty, port, 0, 0, 0, 0, 0, 0, CYPRESS_GET_CONFIG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001088 /* Here we can define custom tty settings for devices; the main tty
1089 * termios flag base comes from empeg.c */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001091 spin_lock_irqsave(&priv->lock, flags);
Alan Cox813a2242008-07-22 11:10:36 +01001092 if (priv->chiptype == CT_EARTHMATE && priv->baud_rate == 4800) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001093 dbg("Using custom termios settings for a baud rate of "
1094 "4800bps.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 /* define custom termios settings for NMEA protocol */
1096
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 tty->termios->c_iflag /* input modes - */
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001098 &= ~(IGNBRK /* disable ignore break */
1099 | BRKINT /* disable break causes interrupt */
1100 | PARMRK /* disable mark parity errors */
1101 | ISTRIP /* disable clear high bit of input char */
1102 | INLCR /* disable translate NL to CR */
1103 | IGNCR /* disable ignore CR */
1104 | ICRNL /* disable translate CR to NL */
1105 | IXON); /* disable enable XON/XOFF flow control */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001107 tty->termios->c_oflag /* output modes */
1108 &= ~OPOST; /* disable postprocess output char */
1109
1110 tty->termios->c_lflag /* line discipline modes */
1111 &= ~(ECHO /* disable echo input characters */
1112 | ECHONL /* disable echo new line */
1113 | ICANON /* disable erase, kill, werase, and rprnt
1114 special characters */
1115 | ISIG /* disable interrupt, quit, and suspend
1116 special characters */
1117 | IEXTEN); /* disable non-POSIX special characters */
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001118 } /* CT_CYPHIDCOM: Application should handle this for device */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 linechange = (priv->line_control != oldlines);
1121 spin_unlock_irqrestore(&priv->lock, flags);
1122
1123 /* if necessary, set lines */
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001124 if (linechange) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 priv->cmd_ctrl = 1;
Alan Cox95da3102008-07-22 11:09:07 +01001126 cypress_write(tty, port, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128} /* cypress_set_termios */
1129
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001130
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131/* returns amount of data still left in soft buffer */
Alan Cox95da3102008-07-22 11:09:07 +01001132static int cypress_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133{
Alan Cox95da3102008-07-22 11:09:07 +01001134 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 struct cypress_private *priv = usb_get_serial_port_data(port);
1136 int chars = 0;
1137 unsigned long flags;
1138
Harvey Harrison441b62c2008-03-03 16:08:34 -08001139 dbg("%s - port %d", __func__, port->number);
Alan Cox813a2242008-07-22 11:10:36 +01001140
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 spin_lock_irqsave(&priv->lock, flags);
1142 chars = cypress_buf_data_avail(priv->buf);
1143 spin_unlock_irqrestore(&priv->lock, flags);
1144
Harvey Harrison441b62c2008-03-03 16:08:34 -08001145 dbg("%s - returns %d", __func__, chars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 return chars;
1147}
1148
1149
Alan Cox95da3102008-07-22 11:09:07 +01001150static void cypress_throttle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151{
Alan Cox95da3102008-07-22 11:09:07 +01001152 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 struct cypress_private *priv = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
Harvey Harrison441b62c2008-03-03 16:08:34 -08001155 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
Oliver Neukum63832512009-10-07 10:50:23 +02001157 spin_lock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 priv->rx_flags = THROTTLED;
Oliver Neukum63832512009-10-07 10:50:23 +02001159 spin_unlock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160}
1161
1162
Alan Cox95da3102008-07-22 11:09:07 +01001163static void cypress_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164{
Alan Cox95da3102008-07-22 11:09:07 +01001165 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 struct cypress_private *priv = usb_get_serial_port_data(port);
1167 int actually_throttled, result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
Harvey Harrison441b62c2008-03-03 16:08:34 -08001169 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
Oliver Neukum63832512009-10-07 10:50:23 +02001171 spin_lock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 actually_throttled = priv->rx_flags & ACTUALLY_THROTTLED;
1173 priv->rx_flags = 0;
Oliver Neukum63832512009-10-07 10:50:23 +02001174 spin_unlock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
Mike Isely78aef512006-08-29 22:07:11 -05001176 if (!priv->comm_is_ok)
1177 return;
1178
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 if (actually_throttled) {
1180 port->interrupt_in_urb->dev = port->serial->dev;
1181
Oliver Neukum63832512009-10-07 10:50:23 +02001182 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
Mike Isely78aef512006-08-29 22:07:11 -05001183 if (result) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001184 dev_err(&port->dev, "%s - failed submitting read urb, "
Harvey Harrison441b62c2008-03-03 16:08:34 -08001185 "error %d\n", __func__, result);
Mike Isely78aef512006-08-29 22:07:11 -05001186 cypress_set_dead(port);
1187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 }
1189}
1190
1191
David Howells7d12e782006-10-05 14:55:46 +01001192static void cypress_read_int_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193{
Ming Leicdc97792008-02-24 18:41:47 +08001194 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 struct cypress_private *priv = usb_get_serial_port_data(port);
1196 struct tty_struct *tty;
1197 unsigned char *data = urb->transfer_buffer;
1198 unsigned long flags;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001199 char tty_flag = TTY_NORMAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 int havedata = 0;
1201 int bytes = 0;
1202 int result;
1203 int i = 0;
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001204 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
Harvey Harrison441b62c2008-03-03 16:08:34 -08001206 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001208 switch (status) {
Mike Isely78aef512006-08-29 22:07:11 -05001209 case 0: /* success */
1210 break;
1211 case -ECONNRESET:
1212 case -ENOENT:
1213 case -ESHUTDOWN:
1214 /* precursor to disconnect so just go away */
1215 return;
1216 case -EPIPE:
Alan Stern4d2fae82009-07-09 12:59:57 -04001217 /* Can't call usb_clear_halt while in_interrupt */
1218 /* FALLS THROUGH */
Mike Isely78aef512006-08-29 22:07:11 -05001219 default:
1220 /* something ugly is going on... */
Alan Cox813a2242008-07-22 11:10:36 +01001221 dev_err(&urb->dev->dev,
1222 "%s - unexpected nonzero read status received: %d\n",
1223 __func__, status);
Mike Isely78aef512006-08-29 22:07:11 -05001224 cypress_set_dead(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 return;
1226 }
1227
1228 spin_lock_irqsave(&priv->lock, flags);
1229 if (priv->rx_flags & THROTTLED) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001230 dbg("%s - now throttling", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 priv->rx_flags |= ACTUALLY_THROTTLED;
1232 spin_unlock_irqrestore(&priv->lock, flags);
1233 return;
1234 }
1235 spin_unlock_irqrestore(&priv->lock, flags);
1236
Alan Cox4a90f092008-10-13 10:39:46 +01001237 tty = tty_port_tty_get(&port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 if (!tty) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001239 dbg("%s - bad tty pointer - exiting", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 return;
1241 }
1242
1243 spin_lock_irqsave(&priv->lock, flags);
Mike Isely3416eaa2008-02-10 20:23:19 -06001244 result = urb->actual_length;
1245 switch (priv->pkt_fmt) {
1246 default:
1247 case packet_format_1:
1248 /* This is for the CY7C64013... */
1249 priv->current_status = data[0] & 0xF8;
1250 bytes = data[1] + 2;
1251 i = 2;
1252 if (bytes > 2)
1253 havedata = 1;
1254 break;
1255 case packet_format_2:
1256 /* This is for the CY7C63743... */
1257 priv->current_status = data[0] & 0xF8;
1258 bytes = (data[0] & 0x07) + 1;
1259 i = 1;
1260 if (bytes > 1)
1261 havedata = 1;
1262 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 }
1264 spin_unlock_irqrestore(&priv->lock, flags);
Mike Isely3416eaa2008-02-10 20:23:19 -06001265 if (result < bytes) {
1266 dbg("%s - wrong packet size - received %d bytes but packet "
1267 "said %d bytes", __func__, result, bytes);
1268 goto continue_read;
1269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
Alan Cox813a2242008-07-22 11:10:36 +01001271 usb_serial_debug_data(debug, &port->dev, __func__,
1272 urb->actual_length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
1274 spin_lock_irqsave(&priv->lock, flags);
1275 /* check to see if status has changed */
Mike Isely67683062008-02-10 20:23:28 -06001276 if (priv->current_status != priv->prev_status) {
1277 priv->diff_status |= priv->current_status ^
1278 priv->prev_status;
1279 wake_up_interruptible(&priv->delta_msr_wait);
1280 priv->prev_status = priv->current_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 }
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001282 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001284 /* hangup, as defined in acm.c... this might be a bad place for it
1285 * though */
1286 if (tty && !(tty->termios->c_cflag & CLOCAL) &&
1287 !(priv->current_status & UART_CD)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001288 dbg("%s - calling hangup", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 tty_hangup(tty);
1290 goto continue_read;
1291 }
1292
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001293 /* There is one error bit... I'm assuming it is a parity error
1294 * indicator as the generic firmware will set this bit to 1 if a
1295 * parity error occurs.
1296 * I can not find reference to any other error events. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 spin_lock_irqsave(&priv->lock, flags);
1298 if (priv->current_status & CYP_ERROR) {
1299 spin_unlock_irqrestore(&priv->lock, flags);
1300 tty_flag = TTY_PARITY;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001301 dbg("%s - Parity Error detected", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 } else
1303 spin_unlock_irqrestore(&priv->lock, flags);
1304
1305 /* process read if there is data other than line status */
1306 if (tty && (bytes > i)) {
Alan Cox33f0f882006-01-09 20:54:13 -08001307 bytes = tty_buffer_request_room(tty, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 for (; i < bytes ; ++i) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001309 dbg("pushing byte number %d - %d - %c", i, data[i],
1310 data[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 tty_insert_flip_char(tty, data[i], tty_flag);
1312 }
Alan Cox4a90f092008-10-13 10:39:46 +01001313 tty_flip_buffer_push(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 }
1315
1316 spin_lock_irqsave(&priv->lock, flags);
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001317 /* control and status byte(s) are also counted */
1318 priv->bytes_in += bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 spin_unlock_irqrestore(&priv->lock, flags);
1320
1321continue_read:
Alan Cox4a90f092008-10-13 10:39:46 +01001322 tty_kref_put(tty);
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001323
1324 /* Continue trying to always read... unless the port has closed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
Alan Cox95da3102008-07-22 11:09:07 +01001326 if (port->port.count > 0 && priv->comm_is_ok) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001327 usb_fill_int_urb(port->interrupt_in_urb, port->serial->dev,
1328 usb_rcvintpipe(port->serial->dev,
1329 port->interrupt_in_endpointAddress),
1330 port->interrupt_in_urb->transfer_buffer,
1331 port->interrupt_in_urb->transfer_buffer_length,
Alan Cox813a2242008-07-22 11:10:36 +01001332 cypress_read_int_callback, port,
1333 priv->read_urb_interval);
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001334 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
Mike Isely78aef512006-08-29 22:07:11 -05001335 if (result) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001336 dev_err(&urb->dev->dev, "%s - failed resubmitting "
Harvey Harrison441b62c2008-03-03 16:08:34 -08001337 "read urb, error %d\n", __func__,
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001338 result);
Mike Isely78aef512006-08-29 22:07:11 -05001339 cypress_set_dead(port);
1340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 }
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001342
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 return;
1344} /* cypress_read_int_callback */
1345
1346
David Howells7d12e782006-10-05 14:55:46 +01001347static void cypress_write_int_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348{
Ming Leicdc97792008-02-24 18:41:47 +08001349 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 struct cypress_private *priv = usb_get_serial_port_data(port);
1351 int result;
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001352 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
Harvey Harrison441b62c2008-03-03 16:08:34 -08001354 dbg("%s - port %d", __func__, port->number);
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001355
1356 switch (status) {
Alan Cox813a2242008-07-22 11:10:36 +01001357 case 0:
1358 /* success */
1359 break;
1360 case -ECONNRESET:
1361 case -ENOENT:
1362 case -ESHUTDOWN:
1363 /* this urb is terminated, clean up */
1364 dbg("%s - urb shutting down with status: %d",
1365 __func__, status);
1366 priv->write_urb_in_use = 0;
1367 return;
1368 case -EPIPE: /* no break needed; clear halt and resubmit */
1369 if (!priv->comm_is_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 break;
Alan Cox813a2242008-07-22 11:10:36 +01001371 usb_clear_halt(port->serial->dev, 0x02);
1372 /* error in the urb, so we have to resubmit it */
1373 dbg("%s - nonzero write bulk status received: %d",
1374 __func__, status);
1375 port->interrupt_out_urb->transfer_buffer_length = 1;
1376 port->interrupt_out_urb->dev = port->serial->dev;
1377 result = usb_submit_urb(port->interrupt_out_urb, GFP_ATOMIC);
1378 if (!result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 return;
Alan Cox813a2242008-07-22 11:10:36 +01001380 dev_err(&urb->dev->dev,
1381 "%s - failed resubmitting write urb, error %d\n",
1382 __func__, result);
1383 cypress_set_dead(port);
1384 break;
1385 default:
1386 dev_err(&urb->dev->dev,
1387 "%s - unexpected nonzero write status received: %d\n",
1388 __func__, status);
1389 cypress_set_dead(port);
1390 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 priv->write_urb_in_use = 0;
Alan Cox813a2242008-07-22 11:10:36 +01001393
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 /* send any buffered data */
1395 cypress_send(port);
1396}
1397
1398
1399/*****************************************************************************
1400 * Write buffer functions - buffering code from pl2303 used
1401 *****************************************************************************/
1402
1403/*
1404 * cypress_buf_alloc
1405 *
1406 * Allocate a circular buffer and all associated memory.
1407 */
1408
1409static struct cypress_buf *cypress_buf_alloc(unsigned int size)
1410{
1411
1412 struct cypress_buf *cb;
1413
1414
1415 if (size == 0)
1416 return NULL;
1417
Robert P. J. Day5cbded52006-12-13 00:35:56 -08001418 cb = kmalloc(sizeof(struct cypress_buf), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 if (cb == NULL)
1420 return NULL;
1421
1422 cb->buf_buf = kmalloc(size, GFP_KERNEL);
1423 if (cb->buf_buf == NULL) {
1424 kfree(cb);
1425 return NULL;
1426 }
1427
1428 cb->buf_size = size;
1429 cb->buf_get = cb->buf_put = cb->buf_buf;
1430
1431 return cb;
1432
1433}
1434
1435
1436/*
1437 * cypress_buf_free
1438 *
1439 * Free the buffer and all associated memory.
1440 */
1441
1442static void cypress_buf_free(struct cypress_buf *cb)
1443{
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001444 if (cb) {
1445 kfree(cb->buf_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 kfree(cb);
1447 }
1448}
1449
1450
1451/*
1452 * cypress_buf_clear
1453 *
1454 * Clear out all data in the circular buffer.
1455 */
1456
1457static void cypress_buf_clear(struct cypress_buf *cb)
1458{
1459 if (cb != NULL)
1460 cb->buf_get = cb->buf_put;
1461 /* equivalent to a get of all data available */
1462}
1463
1464
1465/*
1466 * cypress_buf_data_avail
1467 *
1468 * Return the number of bytes of data available in the circular
1469 * buffer.
1470 */
1471
1472static unsigned int cypress_buf_data_avail(struct cypress_buf *cb)
1473{
1474 if (cb != NULL)
Alan Cox813a2242008-07-22 11:10:36 +01001475 return (cb->buf_size + cb->buf_put - cb->buf_get)
1476 % cb->buf_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 else
1478 return 0;
1479}
1480
1481
1482/*
1483 * cypress_buf_space_avail
1484 *
1485 * Return the number of bytes of space available in the circular
1486 * buffer.
1487 */
1488
1489static unsigned int cypress_buf_space_avail(struct cypress_buf *cb)
1490{
1491 if (cb != NULL)
Alan Cox813a2242008-07-22 11:10:36 +01001492 return (cb->buf_size + cb->buf_get - cb->buf_put - 1)
1493 % cb->buf_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 else
1495 return 0;
1496}
1497
1498
1499/*
1500 * cypress_buf_put
1501 *
1502 * Copy data data from a user buffer and put it into the circular buffer.
1503 * Restrict to the amount of space available.
1504 *
1505 * Return the number of bytes copied.
1506 */
1507
1508static unsigned int cypress_buf_put(struct cypress_buf *cb, const char *buf,
1509 unsigned int count)
1510{
1511
1512 unsigned int len;
1513
1514
1515 if (cb == NULL)
1516 return 0;
1517
1518 len = cypress_buf_space_avail(cb);
1519 if (count > len)
1520 count = len;
1521
1522 if (count == 0)
1523 return 0;
1524
1525 len = cb->buf_buf + cb->buf_size - cb->buf_put;
1526 if (count > len) {
1527 memcpy(cb->buf_put, buf, len);
1528 memcpy(cb->buf_buf, buf+len, count - len);
1529 cb->buf_put = cb->buf_buf + count - len;
1530 } else {
1531 memcpy(cb->buf_put, buf, count);
1532 if (count < len)
1533 cb->buf_put += count;
1534 else /* count == len */
1535 cb->buf_put = cb->buf_buf;
1536 }
1537
1538 return count;
1539
1540}
1541
1542
1543/*
1544 * cypress_buf_get
1545 *
1546 * Get data from the circular buffer and copy to the given buffer.
1547 * Restrict to the amount of data available.
1548 *
1549 * Return the number of bytes copied.
1550 */
1551
1552static unsigned int cypress_buf_get(struct cypress_buf *cb, char *buf,
1553 unsigned int count)
1554{
1555
1556 unsigned int len;
1557
1558
1559 if (cb == NULL)
1560 return 0;
1561
1562 len = cypress_buf_data_avail(cb);
1563 if (count > len)
1564 count = len;
1565
1566 if (count == 0)
1567 return 0;
1568
1569 len = cb->buf_buf + cb->buf_size - cb->buf_get;
1570 if (count > len) {
1571 memcpy(buf, cb->buf_get, len);
1572 memcpy(buf+len, cb->buf_buf, count - len);
1573 cb->buf_get = cb->buf_buf + count - len;
1574 } else {
1575 memcpy(buf, cb->buf_get, count);
1576 if (count < len)
1577 cb->buf_get += count;
1578 else /* count == len */
1579 cb->buf_get = cb->buf_buf;
1580 }
1581
1582 return count;
1583
1584}
1585
1586/*****************************************************************************
1587 * Module functions
1588 *****************************************************************************/
1589
1590static int __init cypress_init(void)
1591{
1592 int retval;
Alan Cox813a2242008-07-22 11:10:36 +01001593
Harvey Harrison441b62c2008-03-03 16:08:34 -08001594 dbg("%s", __func__);
Alan Cox813a2242008-07-22 11:10:36 +01001595
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 retval = usb_serial_register(&cypress_earthmate_device);
1597 if (retval)
1598 goto failed_em_register;
1599 retval = usb_serial_register(&cypress_hidcom_device);
1600 if (retval)
1601 goto failed_hidcom_register;
Lonnie Mendeza5c44e22006-03-01 10:45:24 -06001602 retval = usb_serial_register(&cypress_ca42v2_device);
1603 if (retval)
1604 goto failed_ca42v2_register;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 retval = usb_register(&cypress_driver);
1606 if (retval)
1607 goto failed_usb_register;
1608
Greg Kroah-Hartmanc197a8d2008-08-18 13:21:04 -07001609 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
1610 DRIVER_DESC "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
Mariusz Kozlowski2e46b742006-11-17 17:49:22 +01001613failed_usb_register:
1614 usb_serial_deregister(&cypress_ca42v2_device);
1615failed_ca42v2_register:
1616 usb_serial_deregister(&cypress_hidcom_device);
1617failed_hidcom_register:
1618 usb_serial_deregister(&cypress_earthmate_device);
1619failed_em_register:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 return retval;
1621}
1622
1623
Alan Cox813a2242008-07-22 11:10:36 +01001624static void __exit cypress_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625{
Harvey Harrison441b62c2008-03-03 16:08:34 -08001626 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
Alan Cox813a2242008-07-22 11:10:36 +01001628 usb_deregister(&cypress_driver);
1629 usb_serial_deregister(&cypress_earthmate_device);
1630 usb_serial_deregister(&cypress_hidcom_device);
1631 usb_serial_deregister(&cypress_ca42v2_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632}
1633
1634
1635module_init(cypress_init);
1636module_exit(cypress_exit);
1637
Alan Cox813a2242008-07-22 11:10:36 +01001638MODULE_AUTHOR(DRIVER_AUTHOR);
1639MODULE_DESCRIPTION(DRIVER_DESC);
1640MODULE_VERSION(DRIVER_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641MODULE_LICENSE("GPL");
1642
1643module_param(debug, bool, S_IRUGO | S_IWUSR);
1644MODULE_PARM_DESC(debug, "Debug enabled or not");
1645module_param(stats, bool, S_IRUGO | S_IWUSR);
1646MODULE_PARM_DESC(stats, "Enable statistics or not");
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001647module_param(interval, int, S_IRUGO | S_IWUSR);
1648MODULE_PARM_DESC(interval, "Overrides interrupt interval");