blob: 155b82a25d18b3b57c93f3959992e2e602dca608 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * USB Cypress M8 driver
3 *
4 * Copyright (C) 2004
5 * Lonnie Mendez (dignome@gmail.com)
6 * 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 *
14 * See Documentation/usb/usb-serial.txt for more information on using this driver
15 *
16 * See http://geocities.com/i0xox0i for information on this driver and the
17 * earthmate usb device.
18 *
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -050019 * Lonnie Mendez <dignome@gmail.com>
20 * 4-29-2005
21 * Fixed problem where setting or retreiving the serial config would fail with
22 * EPIPE. Removed CRTS toggling so the driver behaves more like other usbserial
23 * adapters. Issued new interval of 1ms instead of the default 10ms. As a
24 * result, transfer speed has been substantially increased. From avg. 850bps to
25 * avg. 3300bps. initial termios has also been modified. Cleaned up code and
26 * formatting issues so it is more readable. Replaced the C++ style comments.
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 *
28 * Lonnie Mendez <dignome@gmail.com>
29 * 12-15-2004
30 * Incorporated write buffering from pl2303 driver. Fixed bug with line
31 * handling so both lines are raised in cypress_open. (was dropping rts)
32 * Various code cleanups made as well along with other misc bug fixes.
33 *
34 * Lonnie Mendez <dignome@gmail.com>
35 * 04-10-2004
36 * Driver modified to support dynamic line settings. Various improvments
37 * and features.
38 *
39 * Neil Whelchel
40 * 10-2003
41 * Driver first released.
42 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 */
44
45/* Thanks to Neil Whelchel for writing the first cypress m8 implementation for linux. */
46/* Thanks to cypress for providing references for the hid reports. */
47/* Thanks to Jiang Zhang for providing links and for general help. */
48/* Code originates and was built up from ftdi_sio, belkin, pl2303 and others. */
49
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/kernel.h>
52#include <linux/errno.h>
53#include <linux/init.h>
54#include <linux/slab.h>
55#include <linux/tty.h>
56#include <linux/tty_driver.h>
57#include <linux/tty_flip.h>
58#include <linux/module.h>
59#include <linux/moduleparam.h>
60#include <linux/spinlock.h>
61#include <linux/usb.h>
Greg Kroah-Hartmana9698882006-07-11 21:22:58 -070062#include <linux/usb/serial.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include <linux/serial.h>
64#include <linux/delay.h>
65#include <asm/uaccess.h>
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#include "cypress_m8.h"
68
69
70#ifdef CONFIG_USB_SERIAL_DEBUG
71 static int debug = 1;
72#else
73 static int debug;
74#endif
75static int stats;
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -050076static int interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78/*
79 * Version Information
80 */
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -050081#define DRIVER_VERSION "v1.09"
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#define DRIVER_AUTHOR "Lonnie Mendez <dignome@gmail.com>, Neil Whelchel <koyama@firstlight.net>"
83#define DRIVER_DESC "Cypress USB to Serial Driver"
84
85/* write buffer size defines */
86#define CYPRESS_BUF_SIZE 1024
87#define CYPRESS_CLOSING_WAIT (30*HZ)
88
89static struct usb_device_id id_table_earthmate [] = {
90 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB) },
Lonnie Mendez25b6f082005-05-10 17:09:52 -050091 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB_LT20) },
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 { } /* Terminating entry */
93};
94
95static struct usb_device_id id_table_cyphidcomrs232 [] = {
96 { USB_DEVICE(VENDOR_ID_CYPRESS, PRODUCT_ID_CYPHIDCOM) },
Dmitry Shapin6f6f06e2008-03-04 15:25:10 -080097 { USB_DEVICE(VENDOR_ID_POWERCOM, PRODUCT_ID_UPS) },
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 { } /* Terminating entry */
99};
100
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600101static struct usb_device_id id_table_nokiaca42v2 [] = {
102 { USB_DEVICE(VENDOR_ID_DAZZLE, PRODUCT_ID_CA42) },
103 { } /* Terminating entry */
104};
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106static struct usb_device_id id_table_combined [] = {
107 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB) },
Lonnie Mendez25b6f082005-05-10 17:09:52 -0500108 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB_LT20) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 { USB_DEVICE(VENDOR_ID_CYPRESS, PRODUCT_ID_CYPHIDCOM) },
Dmitry Shapin6f6f06e2008-03-04 15:25:10 -0800110 { USB_DEVICE(VENDOR_ID_POWERCOM, PRODUCT_ID_UPS) },
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600111 { USB_DEVICE(VENDOR_ID_DAZZLE, PRODUCT_ID_CA42) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 { } /* Terminating entry */
113};
114
115MODULE_DEVICE_TABLE (usb, id_table_combined);
116
117static struct usb_driver cypress_driver = {
118 .name = "cypress",
119 .probe = usb_serial_probe,
120 .disconnect = usb_serial_disconnect,
121 .id_table = id_table_combined,
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800122 .no_dynamic_id = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123};
124
125struct cypress_private {
126 spinlock_t lock; /* private lock */
127 int chiptype; /* identifier of device, for quirks/etc */
128 int bytes_in; /* used for statistics */
129 int bytes_out; /* used for statistics */
130 int cmd_count; /* used for statistics */
131 int cmd_ctrl; /* always set this to 1 before issuing a command */
132 struct cypress_buf *buf; /* write buffer */
133 int write_urb_in_use; /* write urb in use indicator */
Mike Isely0257fa92006-08-29 22:06:59 -0500134 int write_urb_interval; /* interval to use for write urb */
135 int read_urb_interval; /* interval to use for read urb */
Mike Isely78aef512006-08-29 22:07:11 -0500136 int comm_is_ok; /* true if communication is (still) ok */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 int termios_initialized;
138 __u8 line_control; /* holds dtr / rts value */
139 __u8 current_status; /* received from last read - info on dsr,cts,cd,ri,etc */
140 __u8 current_config; /* stores the current configuration byte */
141 __u8 rx_flags; /* throttling - used from whiteheat/ftdi_sio */
142 int baud_rate; /* stores current baud rate in integer form */
143 int cbr_mask; /* stores current baud rate in masked form */
144 int isthrottled; /* if throttled, discard reads */
145 wait_queue_head_t delta_msr_wait; /* used for TIOCMIWAIT */
146 char prev_status, diff_status; /* used for TIOCMIWAIT */
147 /* we pass a pointer to this as the arguement sent to cypress_set_termios old_termios */
Alan Cox606d0992006-12-08 02:38:45 -0800148 struct ktermios tmp_termios; /* stores the old termios settings */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149};
150
151/* write buffer structure */
152struct cypress_buf {
153 unsigned int buf_size;
154 char *buf_buf;
155 char *buf_get;
156 char *buf_put;
157};
158
159/* function prototypes for the Cypress USB to serial device */
160static int cypress_earthmate_startup (struct usb_serial *serial);
161static int cypress_hidcom_startup (struct usb_serial *serial);
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600162static int cypress_ca42v2_startup (struct usb_serial *serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163static void cypress_shutdown (struct usb_serial *serial);
164static int cypress_open (struct usb_serial_port *port, struct file *filp);
165static void cypress_close (struct usb_serial_port *port, struct file *filp);
166static int cypress_write (struct usb_serial_port *port, const unsigned char *buf, int count);
167static void cypress_send (struct usb_serial_port *port);
168static int cypress_write_room (struct usb_serial_port *port);
169static int cypress_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg);
Alan Cox606d0992006-12-08 02:38:45 -0800170static void cypress_set_termios (struct usb_serial_port *port, struct ktermios * old);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171static int cypress_tiocmget (struct usb_serial_port *port, struct file *file);
172static int cypress_tiocmset (struct usb_serial_port *port, struct file *file, unsigned int set, unsigned int clear);
173static int cypress_chars_in_buffer (struct usb_serial_port *port);
174static void cypress_throttle (struct usb_serial_port *port);
175static void cypress_unthrottle (struct usb_serial_port *port);
Mike Isely78aef512006-08-29 22:07:11 -0500176static void cypress_set_dead (struct usb_serial_port *port);
David Howells7d12e782006-10-05 14:55:46 +0100177static void cypress_read_int_callback (struct urb *urb);
178static void cypress_write_int_callback (struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179/* baud helper functions */
180static int mask_to_rate (unsigned mask);
181static unsigned rate_to_mask (int rate);
182/* write buffer functions */
183static struct cypress_buf *cypress_buf_alloc(unsigned int size);
184static void cypress_buf_free(struct cypress_buf *cb);
185static void cypress_buf_clear(struct cypress_buf *cb);
186static unsigned int cypress_buf_data_avail(struct cypress_buf *cb);
187static unsigned int cypress_buf_space_avail(struct cypress_buf *cb);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500188static unsigned int cypress_buf_put(struct cypress_buf *cb, const char *buf, unsigned int count);
189static unsigned int cypress_buf_get(struct cypress_buf *cb, char *buf, unsigned int count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700192static struct usb_serial_driver cypress_earthmate_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700193 .driver = {
194 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700195 .name = "earthmate",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700196 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700197 .description = "DeLorme Earthmate USB",
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100198 .usb_driver = &cypress_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 .id_table = id_table_earthmate,
200 .num_interrupt_in = 1,
201 .num_interrupt_out = 1,
202 .num_bulk_in = NUM_DONT_CARE,
203 .num_bulk_out = NUM_DONT_CARE,
204 .num_ports = 1,
205 .attach = cypress_earthmate_startup,
206 .shutdown = cypress_shutdown,
207 .open = cypress_open,
208 .close = cypress_close,
209 .write = cypress_write,
210 .write_room = cypress_write_room,
211 .ioctl = cypress_ioctl,
212 .set_termios = cypress_set_termios,
213 .tiocmget = cypress_tiocmget,
214 .tiocmset = cypress_tiocmset,
215 .chars_in_buffer = cypress_chars_in_buffer,
216 .throttle = cypress_throttle,
217 .unthrottle = cypress_unthrottle,
218 .read_int_callback = cypress_read_int_callback,
219 .write_int_callback = cypress_write_int_callback,
220};
221
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700222static struct usb_serial_driver cypress_hidcom_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700223 .driver = {
224 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700225 .name = "cyphidcom",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700226 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700227 .description = "HID->COM RS232 Adapter",
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100228 .usb_driver = &cypress_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 .id_table = id_table_cyphidcomrs232,
230 .num_interrupt_in = 1,
231 .num_interrupt_out = 1,
232 .num_bulk_in = NUM_DONT_CARE,
233 .num_bulk_out = NUM_DONT_CARE,
234 .num_ports = 1,
235 .attach = cypress_hidcom_startup,
236 .shutdown = cypress_shutdown,
237 .open = cypress_open,
238 .close = cypress_close,
239 .write = cypress_write,
240 .write_room = cypress_write_room,
241 .ioctl = cypress_ioctl,
242 .set_termios = cypress_set_termios,
243 .tiocmget = cypress_tiocmget,
244 .tiocmset = cypress_tiocmset,
245 .chars_in_buffer = cypress_chars_in_buffer,
246 .throttle = cypress_throttle,
247 .unthrottle = cypress_unthrottle,
248 .read_int_callback = cypress_read_int_callback,
249 .write_int_callback = cypress_write_int_callback,
250};
251
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600252static struct usb_serial_driver cypress_ca42v2_device = {
253 .driver = {
254 .owner = THIS_MODULE,
255 .name = "nokiaca42v2",
256 },
257 .description = "Nokia CA-42 V2 Adapter",
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100258 .usb_driver = &cypress_driver,
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600259 .id_table = id_table_nokiaca42v2,
260 .num_interrupt_in = 1,
261 .num_interrupt_out = 1,
262 .num_bulk_in = NUM_DONT_CARE,
263 .num_bulk_out = NUM_DONT_CARE,
264 .num_ports = 1,
265 .attach = cypress_ca42v2_startup,
266 .shutdown = cypress_shutdown,
267 .open = cypress_open,
268 .close = cypress_close,
269 .write = cypress_write,
270 .write_room = cypress_write_room,
271 .ioctl = cypress_ioctl,
272 .set_termios = cypress_set_termios,
273 .tiocmget = cypress_tiocmget,
274 .tiocmset = cypress_tiocmset,
275 .chars_in_buffer = cypress_chars_in_buffer,
276 .throttle = cypress_throttle,
277 .unthrottle = cypress_unthrottle,
278 .read_int_callback = cypress_read_int_callback,
279 .write_int_callback = cypress_write_int_callback,
280};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282/*****************************************************************************
283 * Cypress serial helper functions
284 *****************************************************************************/
285
286
Steven Cole093cf722005-05-03 19:07:24 -0600287/* This function can either set or retrieve the current serial line settings */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288static int cypress_serial_control (struct usb_serial_port *port, unsigned baud_mask, int data_bits, int stop_bits,
289 int parity_enable, int parity_type, int reset, int cypress_request_type)
290{
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500291 int new_baudrate = 0, retval = 0, tries = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 struct cypress_private *priv;
Mike Isely93075542008-02-10 20:23:14 -0600293 __u8 feature_buffer[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 unsigned long flags;
295
296 dbg("%s", __FUNCTION__);
297
298 priv = usb_get_serial_port_data(port);
299
Mike Isely78aef512006-08-29 22:07:11 -0500300 if (!priv->comm_is_ok)
301 return -ENODEV;
302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 switch(cypress_request_type) {
304 case CYPRESS_SET_CONFIG:
305
306 /*
307 * The general purpose firmware for the Cypress M8 allows for a maximum speed
308 * of 57600bps (I have no idea whether DeLorme chose to use the general purpose
309 * firmware or not), if you need to modify this speed setting for your own
310 * project please add your own chiptype and modify the code likewise. The
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500311 * Cypress HID->COM device will work successfully up to 115200bps (but the
312 * actual throughput is around 3kBps).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 */
314 if (baud_mask != priv->cbr_mask) {
315 dbg("%s - baud rate is changing", __FUNCTION__);
316 if ( priv->chiptype == CT_EARTHMATE ) {
317 /* 300 and 600 baud rates are supported under the generic firmware,
318 * but are not used with NMEA and SiRF protocols */
319
320 if ( (baud_mask == B300) || (baud_mask == B600) ) {
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500321 err("%s - failed setting baud rate, unsupported speed",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 __FUNCTION__);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500323 new_baudrate = priv->baud_rate;
324 } else if ( (new_baudrate = mask_to_rate(baud_mask)) == -1) {
325 err("%s - failed setting baud rate, unsupported speed",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 __FUNCTION__);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500327 new_baudrate = priv->baud_rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 }
329 } else if (priv->chiptype == CT_CYPHIDCOM) {
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500330 if ( (new_baudrate = mask_to_rate(baud_mask)) == -1) {
331 err("%s - failed setting baud rate, unsupported speed",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 __FUNCTION__);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500333 new_baudrate = priv->baud_rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 }
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600335 } else if (priv->chiptype == CT_CA42V2) {
336 if ( (new_baudrate = mask_to_rate(baud_mask)) == -1) {
337 err("%s - failed setting baud rate, unsupported speed",
338 __FUNCTION__);
339 new_baudrate = priv->baud_rate;
340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 } else if (priv->chiptype == CT_GENERIC) {
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500342 if ( (new_baudrate = mask_to_rate(baud_mask)) == -1) {
343 err("%s - failed setting baud rate, unsupported speed",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 __FUNCTION__);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500345 new_baudrate = priv->baud_rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 }
347 } else {
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500348 info("%s - please define your chiptype", __FUNCTION__);
349 new_baudrate = priv->baud_rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 }
351 } else { /* baud rate not changing, keep the old */
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500352 new_baudrate = priv->baud_rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 }
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500354 dbg("%s - baud rate is being sent as %d", __FUNCTION__, new_baudrate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Mike Isely93075542008-02-10 20:23:14 -0600356 memset(feature_buffer, 0, sizeof(feature_buffer));
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500357 /* fill the feature_buffer with new configuration */
358 *((u_int32_t *)feature_buffer) = new_baudrate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500360 feature_buffer[4] |= data_bits; /* assign data bits in 2 bit space ( max 3 ) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 /* 1 bit gap */
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500362 feature_buffer[4] |= (stop_bits << 3); /* assign stop bits in 1 bit space */
363 feature_buffer[4] |= (parity_enable << 4); /* assign parity flag in 1 bit space */
364 feature_buffer[4] |= (parity_type << 5); /* assign parity type in 1 bit space */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 /* 1 bit gap */
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500366 feature_buffer[4] |= (reset << 7); /* assign reset at end of byte, 1 bit space */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368 dbg("%s - device is being sent this feature report:", __FUNCTION__);
369 dbg("%s - %02X - %02X - %02X - %02X - %02X", __FUNCTION__, feature_buffer[0], feature_buffer[1],
370 feature_buffer[2], feature_buffer[3], feature_buffer[4]);
371
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500372 do {
Mike Isely93075542008-02-10 20:23:14 -0600373 retval = usb_control_msg(port->serial->dev,
374 usb_sndctrlpipe(port->serial->dev, 0),
375 HID_REQ_SET_REPORT,
376 USB_DIR_OUT | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
377 0x0300, 0, feature_buffer,
378 sizeof(feature_buffer), 500);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500380 if (tries++ >= 3)
381 break;
382
Mike Isely93075542008-02-10 20:23:14 -0600383 } while (retval != sizeof(feature_buffer) &&
384 retval != -ENODEV);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500385
Mike Isely93075542008-02-10 20:23:14 -0600386 if (retval != sizeof(feature_buffer)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 err("%s - failed sending serial line settings - %d", __FUNCTION__, retval);
Mike Isely78aef512006-08-29 22:07:11 -0500388 cypress_set_dead(port);
389 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 spin_lock_irqsave(&priv->lock, flags);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500391 priv->baud_rate = new_baudrate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 priv->cbr_mask = baud_mask;
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500393 priv->current_config = feature_buffer[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 spin_unlock_irqrestore(&priv->lock, flags);
395 }
396 break;
397 case CYPRESS_GET_CONFIG:
398 dbg("%s - retreiving serial line settings", __FUNCTION__);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500399 /* set initial values in feature buffer */
Mike Isely93075542008-02-10 20:23:14 -0600400 memset(feature_buffer, 0, sizeof(feature_buffer));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500402 do {
Mike Isely93075542008-02-10 20:23:14 -0600403 retval = usb_control_msg(port->serial->dev,
404 usb_rcvctrlpipe(port->serial->dev, 0),
405 HID_REQ_GET_REPORT,
406 USB_DIR_IN | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
407 0x0300, 0, feature_buffer,
408 sizeof(feature_buffer), 500);
409
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500410 if (tries++ >= 3)
411 break;
412
Mike Isely93075542008-02-10 20:23:14 -0600413 } while (retval != sizeof(feature_buffer) &&
414 retval != -ENODEV);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500415
Mike Isely93075542008-02-10 20:23:14 -0600416 if (retval != sizeof(feature_buffer)) {
Adrian Bunk943ffb52006-01-10 00:10:13 +0100417 err("%s - failed to retrieve serial line settings - %d", __FUNCTION__, retval);
Mike Isely78aef512006-08-29 22:07:11 -0500418 cypress_set_dead(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 return retval;
420 } else {
421 spin_lock_irqsave(&priv->lock, flags);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 /* store the config in one byte, and later use bit masks to check values */
424 priv->current_config = feature_buffer[4];
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500425 priv->baud_rate = *((u_int32_t *)feature_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500427 if ( (priv->cbr_mask = rate_to_mask(priv->baud_rate)) == 0x40)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 dbg("%s - failed setting the baud mask (not defined)", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 spin_unlock_irqrestore(&priv->lock, flags);
430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 }
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500432 spin_lock_irqsave(&priv->lock, flags);
433 ++priv->cmd_count;
434 spin_unlock_irqrestore(&priv->lock, flags);
435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 return retval;
437} /* cypress_serial_control */
438
439
Mike Isely78aef512006-08-29 22:07:11 -0500440static void cypress_set_dead(struct usb_serial_port *port)
441{
442 struct cypress_private *priv = usb_get_serial_port_data(port);
443 unsigned long flags;
444
445 spin_lock_irqsave(&priv->lock, flags);
446 if (!priv->comm_is_ok) {
447 spin_unlock_irqrestore(&priv->lock, flags);
448 return;
449 }
450 priv->comm_is_ok = 0;
451 spin_unlock_irqrestore(&priv->lock, flags);
452
453 err("cypress_m8 suspending failing port %d - interval might be too short",
454 port->number);
455}
456
457
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500458/* given a baud mask, it will return integer baud on success */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459static int mask_to_rate (unsigned mask)
460{
461 int rate;
462
463 switch (mask) {
464 case B0: rate = 0; break;
465 case B300: rate = 300; break;
466 case B600: rate = 600; break;
467 case B1200: rate = 1200; break;
468 case B2400: rate = 2400; break;
469 case B4800: rate = 4800; break;
470 case B9600: rate = 9600; break;
471 case B19200: rate = 19200; break;
472 case B38400: rate = 38400; break;
473 case B57600: rate = 57600; break;
474 case B115200: rate = 115200; break;
475 default: rate = -1;
476 }
477
478 return rate;
479}
480
481
482static unsigned rate_to_mask (int rate)
483{
484 unsigned mask;
485
486 switch (rate) {
487 case 0: mask = B0; break;
488 case 300: mask = B300; break;
489 case 600: mask = B600; break;
490 case 1200: mask = B1200; break;
491 case 2400: mask = B2400; break;
492 case 4800: mask = B4800; break;
493 case 9600: mask = B9600; break;
494 case 19200: mask = B19200; break;
495 case 38400: mask = B38400; break;
496 case 57600: mask = B57600; break;
497 case 115200: mask = B115200; break;
498 default: mask = 0x40;
499 }
500
501 return mask;
502}
503/*****************************************************************************
504 * Cypress serial driver functions
505 *****************************************************************************/
506
507
508static int generic_startup (struct usb_serial *serial)
509{
510 struct cypress_private *priv;
Mike Isely0257fa92006-08-29 22:06:59 -0500511 struct usb_serial_port *port = serial->port[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
Mike Isely0257fa92006-08-29 22:06:59 -0500513 dbg("%s - port %d", __FUNCTION__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +0100515 priv = kzalloc(sizeof (struct cypress_private), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 if (!priv)
517 return -ENOMEM;
518
Mike Isely78aef512006-08-29 22:07:11 -0500519 priv->comm_is_ok = !0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 spin_lock_init(&priv->lock);
521 priv->buf = cypress_buf_alloc(CYPRESS_BUF_SIZE);
522 if (priv->buf == NULL) {
523 kfree(priv);
524 return -ENOMEM;
525 }
526 init_waitqueue_head(&priv->delta_msr_wait);
527
528 usb_reset_configuration (serial->dev);
529
530 priv->cmd_ctrl = 0;
531 priv->line_control = 0;
532 priv->termios_initialized = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 priv->rx_flags = 0;
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500534 priv->cbr_mask = B300;
Mike Isely0257fa92006-08-29 22:06:59 -0500535 if (interval > 0) {
536 priv->write_urb_interval = interval;
537 priv->read_urb_interval = interval;
538 dbg("%s - port %d read & write intervals forced to %d",
539 __FUNCTION__,port->number,interval);
540 } else {
541 priv->write_urb_interval = port->interrupt_out_urb->interval;
542 priv->read_urb_interval = port->interrupt_in_urb->interval;
543 dbg("%s - port %d intervals: read=%d write=%d",
544 __FUNCTION__,port->number,
545 priv->read_urb_interval,priv->write_urb_interval);
546 }
547 usb_set_serial_port_data(port, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500549 return 0;
550}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
552
553static int cypress_earthmate_startup (struct usb_serial *serial)
554{
555 struct cypress_private *priv;
556
557 dbg("%s", __FUNCTION__);
558
559 if (generic_startup(serial)) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500560 dbg("%s - Failed setting up port %d", __FUNCTION__,
561 serial->port[0]->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 return 1;
563 }
564
565 priv = usb_get_serial_port_data(serial->port[0]);
566 priv->chiptype = CT_EARTHMATE;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500567
568 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569} /* cypress_earthmate_startup */
570
571
572static int cypress_hidcom_startup (struct usb_serial *serial)
573{
574 struct cypress_private *priv;
575
576 dbg("%s", __FUNCTION__);
577
578 if (generic_startup(serial)) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500579 dbg("%s - Failed setting up port %d", __FUNCTION__,
580 serial->port[0]->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 return 1;
582 }
583
584 priv = usb_get_serial_port_data(serial->port[0]);
585 priv->chiptype = CT_CYPHIDCOM;
586
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500587 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588} /* cypress_hidcom_startup */
589
590
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600591static int cypress_ca42v2_startup (struct usb_serial *serial)
592{
593 struct cypress_private *priv;
594
595 dbg("%s", __FUNCTION__);
596
597 if (generic_startup(serial)) {
598 dbg("%s - Failed setting up port %d", __FUNCTION__,
599 serial->port[0]->number);
600 return 1;
601 }
602
603 priv = usb_get_serial_port_data(serial->port[0]);
604 priv->chiptype = CT_CA42V2;
605
606 return 0;
607} /* cypress_ca42v2_startup */
608
609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610static void cypress_shutdown (struct usb_serial *serial)
611{
612 struct cypress_private *priv;
613
614 dbg ("%s - port %d", __FUNCTION__, serial->port[0]->number);
615
616 /* all open ports are closed at this point */
617
618 priv = usb_get_serial_port_data(serial->port[0]);
619
620 if (priv) {
621 cypress_buf_free(priv->buf);
622 kfree(priv);
623 usb_set_serial_port_data(serial->port[0], NULL);
624 }
625}
626
627
628static int cypress_open (struct usb_serial_port *port, struct file *filp)
629{
630 struct cypress_private *priv = usb_get_serial_port_data(port);
631 struct usb_serial *serial = port->serial;
632 unsigned long flags;
633 int result = 0;
634
635 dbg("%s - port %d", __FUNCTION__, port->number);
636
Mike Isely78aef512006-08-29 22:07:11 -0500637 if (!priv->comm_is_ok)
638 return -EIO;
639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 /* clear halts before open */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 usb_clear_halt(serial->dev, 0x81);
642 usb_clear_halt(serial->dev, 0x02);
643
644 spin_lock_irqsave(&priv->lock, flags);
645 /* reset read/write statistics */
646 priv->bytes_in = 0;
647 priv->bytes_out = 0;
648 priv->cmd_count = 0;
649 priv->rx_flags = 0;
650 spin_unlock_irqrestore(&priv->lock, flags);
651
652 /* setting to zero could cause data loss */
653 port->tty->low_latency = 1;
654
655 /* raise both lines and set termios */
656 spin_lock_irqsave(&priv->lock, flags);
657 priv->line_control = CONTROL_DTR | CONTROL_RTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 priv->cmd_ctrl = 1;
659 spin_unlock_irqrestore(&priv->lock, flags);
660 result = cypress_write(port, NULL, 0);
661
662 if (result) {
663 dev_err(&port->dev, "%s - failed setting the control lines - error %d\n", __FUNCTION__, result);
664 return result;
665 } else
666 dbg("%s - success setting the control lines", __FUNCTION__);
667
668 cypress_set_termios(port, &priv->tmp_termios);
669
670 /* setup the port and start reading from the device */
671 if(!port->interrupt_in_urb){
672 err("%s - interrupt_in_urb is empty!", __FUNCTION__);
673 return(-1);
674 }
675
676 usb_fill_int_urb(port->interrupt_in_urb, serial->dev,
677 usb_rcvintpipe(serial->dev, port->interrupt_in_endpointAddress),
678 port->interrupt_in_urb->transfer_buffer, port->interrupt_in_urb->transfer_buffer_length,
Mike Isely0257fa92006-08-29 22:06:59 -0500679 cypress_read_int_callback, port, priv->read_urb_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
681
682 if (result){
683 dev_err(&port->dev, "%s - failed submitting read urb, error %d\n", __FUNCTION__, result);
Mike Isely78aef512006-08-29 22:07:11 -0500684 cypress_set_dead(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 }
686
687 return result;
688} /* cypress_open */
689
690
691static void cypress_close(struct usb_serial_port *port, struct file * filp)
692{
693 struct cypress_private *priv = usb_get_serial_port_data(port);
694 unsigned int c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 int bps;
696 long timeout;
697 wait_queue_t wait;
698
699 dbg("%s - port %d", __FUNCTION__, port->number);
700
701 /* wait for data to drain from buffer */
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100702 spin_lock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 timeout = CYPRESS_CLOSING_WAIT;
704 init_waitqueue_entry(&wait, current);
705 add_wait_queue(&port->tty->write_wait, &wait);
706 for (;;) {
707 set_current_state(TASK_INTERRUPTIBLE);
708 if (cypress_buf_data_avail(priv->buf) == 0
709 || timeout == 0 || signal_pending(current)
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100710 /* without mutex, allowed due to harmless failure mode */
711 || port->serial->disconnected)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 break;
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100713 spin_unlock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 timeout = schedule_timeout(timeout);
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100715 spin_lock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 }
717 set_current_state(TASK_RUNNING);
718 remove_wait_queue(&port->tty->write_wait, &wait);
719 /* clear out any remaining data in the buffer */
720 cypress_buf_clear(priv->buf);
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100721 spin_unlock_irq(&priv->lock);
722
723 /* writing is potentially harmful, lock must be taken */
724 mutex_lock(&port->serial->disc_mutex);
725 if (port->serial->disconnected) {
726 mutex_unlock(&port->serial->disc_mutex);
727 return;
728 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 /* wait for characters to drain from device */
730 bps = tty_get_baud_rate(port->tty);
731 if (bps > 1200)
732 timeout = max((HZ*2560)/bps,HZ/10);
733 else
734 timeout = 2*HZ;
Nishanth Aravamudan22c43862005-08-15 11:30:11 -0700735 schedule_timeout_interruptible(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
737 dbg("%s - stopping urbs", __FUNCTION__);
738 usb_kill_urb (port->interrupt_in_urb);
739 usb_kill_urb (port->interrupt_out_urb);
740
741 if (port->tty) {
742 c_cflag = port->tty->termios->c_cflag;
743 if (c_cflag & HUPCL) {
744 /* drop dtr and rts */
745 priv = usb_get_serial_port_data(port);
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100746 spin_lock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 priv->line_control = 0;
748 priv->cmd_ctrl = 1;
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100749 spin_unlock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 cypress_write(port, NULL, 0);
751 }
752 }
753
754 if (stats)
755 dev_info (&port->dev, "Statistics: %d Bytes In | %d Bytes Out | %d Commands Issued\n",
756 priv->bytes_in, priv->bytes_out, priv->cmd_count);
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100757 mutex_unlock(&port->serial->disc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758} /* cypress_close */
759
760
761static int cypress_write(struct usb_serial_port *port, const unsigned char *buf, int count)
762{
763 struct cypress_private *priv = usb_get_serial_port_data(port);
764 unsigned long flags;
765
766 dbg("%s - port %d, %d bytes", __FUNCTION__, port->number, count);
767
768 /* line control commands, which need to be executed immediately,
769 are not put into the buffer for obvious reasons.
770 */
771 if (priv->cmd_ctrl) {
772 count = 0;
773 goto finish;
774 }
775
776 if (!count)
777 return count;
778
779 spin_lock_irqsave(&priv->lock, flags);
780 count = cypress_buf_put(priv->buf, buf, count);
781 spin_unlock_irqrestore(&priv->lock, flags);
782
783finish:
784 cypress_send(port);
785
786 return count;
787} /* cypress_write */
788
789
790static void cypress_send(struct usb_serial_port *port)
791{
792 int count = 0, result, offset, actual_size;
793 struct cypress_private *priv = usb_get_serial_port_data(port);
794 unsigned long flags;
795
Mike Isely78aef512006-08-29 22:07:11 -0500796 if (!priv->comm_is_ok)
797 return;
798
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 dbg("%s - port %d", __FUNCTION__, port->number);
800 dbg("%s - interrupt out size is %d", __FUNCTION__, port->interrupt_out_size);
801
802 spin_lock_irqsave(&priv->lock, flags);
803 if (priv->write_urb_in_use) {
804 dbg("%s - can't write, urb in use", __FUNCTION__);
805 spin_unlock_irqrestore(&priv->lock, flags);
806 return;
807 }
808 spin_unlock_irqrestore(&priv->lock, flags);
809
810 /* clear buffer */
811 memset(port->interrupt_out_urb->transfer_buffer, 0, port->interrupt_out_size);
812
813 spin_lock_irqsave(&priv->lock, flags);
814 switch (port->interrupt_out_size) {
815 case 32:
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500816 /* this is for the CY7C64013... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 offset = 2;
818 port->interrupt_out_buffer[0] = priv->line_control;
819 break;
820 case 8:
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500821 /* this is for the CY7C63743... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 offset = 1;
823 port->interrupt_out_buffer[0] = priv->line_control;
824 break;
825 default:
826 dbg("%s - wrong packet size", __FUNCTION__);
827 spin_unlock_irqrestore(&priv->lock, flags);
828 return;
829 }
830
831 if (priv->line_control & CONTROL_RESET)
832 priv->line_control &= ~CONTROL_RESET;
833
834 if (priv->cmd_ctrl) {
835 priv->cmd_count++;
836 dbg("%s - line control command being issued", __FUNCTION__);
837 spin_unlock_irqrestore(&priv->lock, flags);
838 goto send;
839 } else
840 spin_unlock_irqrestore(&priv->lock, flags);
841
842 count = cypress_buf_get(priv->buf, &port->interrupt_out_buffer[offset],
843 port->interrupt_out_size-offset);
844
845 if (count == 0) {
846 return;
847 }
848
849 switch (port->interrupt_out_size) {
850 case 32:
851 port->interrupt_out_buffer[1] = count;
852 break;
853 case 8:
854 port->interrupt_out_buffer[0] |= count;
855 }
856
857 dbg("%s - count is %d", __FUNCTION__, count);
858
859send:
860 spin_lock_irqsave(&priv->lock, flags);
861 priv->write_urb_in_use = 1;
862 spin_unlock_irqrestore(&priv->lock, flags);
863
864 if (priv->cmd_ctrl)
865 actual_size = 1;
866 else
867 actual_size = count + (port->interrupt_out_size == 32 ? 2 : 1);
868
869 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, port->interrupt_out_size,
870 port->interrupt_out_urb->transfer_buffer);
871
Mike Isely9aa8dae2006-08-29 22:07:04 -0500872 usb_fill_int_urb(port->interrupt_out_urb, port->serial->dev,
873 usb_sndintpipe(port->serial->dev, port->interrupt_out_endpointAddress),
874 port->interrupt_out_buffer, port->interrupt_out_size,
875 cypress_write_int_callback, port, priv->write_urb_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 result = usb_submit_urb (port->interrupt_out_urb, GFP_ATOMIC);
877 if (result) {
878 dev_err(&port->dev, "%s - failed submitting write urb, error %d\n", __FUNCTION__,
879 result);
880 priv->write_urb_in_use = 0;
Mike Isely78aef512006-08-29 22:07:11 -0500881 cypress_set_dead(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 }
883
884 spin_lock_irqsave(&priv->lock, flags);
885 if (priv->cmd_ctrl) {
886 priv->cmd_ctrl = 0;
887 }
888 priv->bytes_out += count; /* do not count the line control and size bytes */
889 spin_unlock_irqrestore(&priv->lock, flags);
890
Pete Zaitcevcf2c7482006-05-22 21:58:49 -0700891 usb_serial_port_softint(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892} /* cypress_send */
893
894
895/* returns how much space is available in the soft buffer */
896static int cypress_write_room(struct usb_serial_port *port)
897{
898 struct cypress_private *priv = usb_get_serial_port_data(port);
899 int room = 0;
900 unsigned long flags;
901
902 dbg("%s - port %d", __FUNCTION__, port->number);
903
904 spin_lock_irqsave(&priv->lock, flags);
905 room = cypress_buf_space_avail(priv->buf);
906 spin_unlock_irqrestore(&priv->lock, flags);
907
908 dbg("%s - returns %d", __FUNCTION__, room);
909 return room;
910}
911
912
913static int cypress_tiocmget (struct usb_serial_port *port, struct file *file)
914{
915 struct cypress_private *priv = usb_get_serial_port_data(port);
916 __u8 status, control;
917 unsigned int result = 0;
918 unsigned long flags;
919
920 dbg("%s - port %d", __FUNCTION__, port->number);
921
922 spin_lock_irqsave(&priv->lock, flags);
923 control = priv->line_control;
924 status = priv->current_status;
925 spin_unlock_irqrestore(&priv->lock, flags);
926
927 result = ((control & CONTROL_DTR) ? TIOCM_DTR : 0)
928 | ((control & CONTROL_RTS) ? TIOCM_RTS : 0)
929 | ((status & UART_CTS) ? TIOCM_CTS : 0)
930 | ((status & UART_DSR) ? TIOCM_DSR : 0)
931 | ((status & UART_RI) ? TIOCM_RI : 0)
932 | ((status & UART_CD) ? TIOCM_CD : 0);
933
934 dbg("%s - result = %x", __FUNCTION__, result);
935
936 return result;
937}
938
939
940static int cypress_tiocmset (struct usb_serial_port *port, struct file *file,
941 unsigned int set, unsigned int clear)
942{
943 struct cypress_private *priv = usb_get_serial_port_data(port);
944 unsigned long flags;
945
946 dbg("%s - port %d", __FUNCTION__, port->number);
947
948 spin_lock_irqsave(&priv->lock, flags);
949 if (set & TIOCM_RTS)
950 priv->line_control |= CONTROL_RTS;
951 if (set & TIOCM_DTR)
952 priv->line_control |= CONTROL_DTR;
953 if (clear & TIOCM_RTS)
954 priv->line_control &= ~CONTROL_RTS;
955 if (clear & TIOCM_DTR)
956 priv->line_control &= ~CONTROL_DTR;
957 spin_unlock_irqrestore(&priv->lock, flags);
958
959 priv->cmd_ctrl = 1;
960 return cypress_write(port, NULL, 0);
961}
962
963
964static int cypress_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg)
965{
966 struct cypress_private *priv = usb_get_serial_port_data(port);
967
968 dbg("%s - port %d, cmd 0x%.4x", __FUNCTION__, port->number, cmd);
969
970 switch (cmd) {
971 case TIOCGSERIAL:
Alan Cox606d0992006-12-08 02:38:45 -0800972 if (copy_to_user((void __user *)arg, port->tty->termios, sizeof(struct ktermios))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 return -EFAULT;
974 }
975 return (0);
976 break;
977 case TIOCSSERIAL:
Alan Cox606d0992006-12-08 02:38:45 -0800978 if (copy_from_user(port->tty->termios, (void __user *)arg, sizeof(struct ktermios))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 return -EFAULT;
980 }
981 /* here we need to call cypress_set_termios to invoke the new settings */
982 cypress_set_termios(port, &priv->tmp_termios);
983 return (0);
984 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 /* This code comes from drivers/char/serial.c and ftdi_sio.c */
986 case TIOCMIWAIT:
987 while (priv != NULL) {
988 interruptible_sleep_on(&priv->delta_msr_wait);
989 /* see if a signal did it */
990 if (signal_pending(current))
991 return -ERESTARTSYS;
992 else {
993 char diff = priv->diff_status;
994
995 if (diff == 0) {
996 return -EIO; /* no change => error */
997 }
998
999 /* consume all events */
1000 priv->diff_status = 0;
1001
1002 /* return 0 if caller wanted to know about these bits */
1003 if ( ((arg & TIOCM_RNG) && (diff & UART_RI)) ||
1004 ((arg & TIOCM_DSR) && (diff & UART_DSR)) ||
1005 ((arg & TIOCM_CD) && (diff & UART_CD)) ||
1006 ((arg & TIOCM_CTS) && (diff & UART_CTS)) ) {
1007 return 0;
1008 }
1009 /* otherwise caller can't care less about what happened,
1010 * and so we continue to wait for more events.
1011 */
1012 }
1013 }
1014 return 0;
1015 break;
1016 default:
1017 break;
1018 }
1019
1020 dbg("%s - arg not supported - it was 0x%04x - check include/asm/ioctls.h", __FUNCTION__, cmd);
1021
1022 return -ENOIOCTLCMD;
1023} /* cypress_ioctl */
1024
1025
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001026static void cypress_set_termios (struct usb_serial_port *port,
Alan Cox606d0992006-12-08 02:38:45 -08001027 struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028{
1029 struct cypress_private *priv = usb_get_serial_port_data(port);
1030 struct tty_struct *tty;
1031 int data_bits, stop_bits, parity_type, parity_enable;
1032 unsigned cflag, iflag, baud_mask;
1033 unsigned long flags;
1034 __u8 oldlines;
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001035 int linechange = 0;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001036
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 dbg("%s - port %d", __FUNCTION__, port->number);
1038
1039 tty = port->tty;
1040 if ((!tty) || (!tty->termios)) {
1041 dbg("%s - no tty structures", __FUNCTION__);
1042 return;
1043 }
1044
1045 spin_lock_irqsave(&priv->lock, flags);
1046 if (!priv->termios_initialized) {
1047 if (priv->chiptype == CT_EARTHMATE) {
1048 *(tty->termios) = tty_std_termios;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001049 tty->termios->c_cflag = B4800 | CS8 | CREAD | HUPCL |
1050 CLOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 } else if (priv->chiptype == CT_CYPHIDCOM) {
1052 *(tty->termios) = tty_std_termios;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001053 tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
1054 CLOCAL;
Lonnie Mendeza5c44e22006-03-01 10:45:24 -06001055 } else if (priv->chiptype == CT_CA42V2) {
1056 *(tty->termios) = tty_std_termios;
1057 tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
1058 CLOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 }
1060 priv->termios_initialized = 1;
1061 }
1062 spin_unlock_irqrestore(&priv->lock, flags);
1063
1064 cflag = tty->termios->c_cflag;
1065 iflag = tty->termios->c_iflag;
1066
1067 /* check if there are new settings */
1068 if (old_termios) {
1069 if ((cflag != old_termios->c_cflag) ||
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001070 (RELEVANT_IFLAG(iflag) !=
1071 RELEVANT_IFLAG(old_termios->c_iflag))) {
1072 dbg("%s - attempting to set new termios settings",
1073 __FUNCTION__);
1074 /* should make a copy of this in case something goes
1075 * wrong in the function, we can restore it */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 spin_lock_irqsave(&priv->lock, flags);
1077 priv->tmp_termios = *(tty->termios);
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001078 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 } else {
1080 dbg("%s - nothing to do, exiting", __FUNCTION__);
1081 return;
1082 }
1083 } else
1084 return;
1085
1086 /* set number of data bits, parity, stop bits */
1087 /* when parity is disabled the parity type bit is ignored */
1088
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001089 /* 1 means 2 stop bits, 0 means 1 stop bit */
1090 stop_bits = cflag & CSTOPB ? 1 : 0;
1091
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 if (cflag & PARENB) {
1093 parity_enable = 1;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001094 /* 1 means odd parity, 0 means even parity */
1095 parity_type = cflag & PARODD ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 } else
1097 parity_enable = parity_type = 0;
1098
1099 if (cflag & CSIZE) {
1100 switch (cflag & CSIZE) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001101 case CS5:
1102 data_bits = 0;
1103 break;
1104 case CS6:
1105 data_bits = 1;
1106 break;
1107 case CS7:
1108 data_bits = 2;
1109 break;
1110 case CS8:
1111 data_bits = 3;
1112 break;
1113 default:
1114 err("%s - CSIZE was set, but not CS5-CS8",
1115 __FUNCTION__);
1116 data_bits = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 }
1118 } else
1119 data_bits = 3;
1120
1121 spin_lock_irqsave(&priv->lock, flags);
1122 oldlines = priv->line_control;
1123 if ((cflag & CBAUD) == B0) {
1124 /* drop dtr and rts */
1125 dbg("%s - dropping the lines, baud rate 0bps", __FUNCTION__);
1126 baud_mask = B0;
1127 priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
1128 } else {
1129 baud_mask = (cflag & CBAUD);
1130 switch(baud_mask) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001131 case B300:
1132 dbg("%s - setting baud 300bps", __FUNCTION__);
1133 break;
1134 case B600:
1135 dbg("%s - setting baud 600bps", __FUNCTION__);
1136 break;
1137 case B1200:
1138 dbg("%s - setting baud 1200bps", __FUNCTION__);
1139 break;
1140 case B2400:
1141 dbg("%s - setting baud 2400bps", __FUNCTION__);
1142 break;
1143 case B4800:
1144 dbg("%s - setting baud 4800bps", __FUNCTION__);
1145 break;
1146 case B9600:
1147 dbg("%s - setting baud 9600bps", __FUNCTION__);
1148 break;
1149 case B19200:
1150 dbg("%s - setting baud 19200bps", __FUNCTION__);
1151 break;
1152 case B38400:
1153 dbg("%s - setting baud 38400bps", __FUNCTION__);
1154 break;
1155 case B57600:
1156 dbg("%s - setting baud 57600bps", __FUNCTION__);
1157 break;
1158 case B115200:
1159 dbg("%s - setting baud 115200bps", __FUNCTION__);
1160 break;
1161 default:
1162 dbg("%s - unknown masked baud rate", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 }
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001164 priv->line_control = (CONTROL_DTR | CONTROL_RTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 }
1166 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001168 dbg("%s - sending %d stop_bits, %d parity_enable, %d parity_type, "
1169 "%d data_bits (+5)", __FUNCTION__, stop_bits,
1170 parity_enable, parity_type, data_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001172 cypress_serial_control(port, baud_mask, data_bits, stop_bits,
1173 parity_enable, parity_type, 0, CYPRESS_SET_CONFIG);
1174
1175 /* we perform a CYPRESS_GET_CONFIG so that the current settings are
1176 * filled into the private structure this should confirm that all is
1177 * working if it returns what we just set */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 cypress_serial_control(port, 0, 0, 0, 0, 0, 0, CYPRESS_GET_CONFIG);
1179
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001180 /* Here we can define custom tty settings for devices; the main tty
1181 * termios flag base comes from empeg.c */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001183 spin_lock_irqsave(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 if ( (priv->chiptype == CT_EARTHMATE) && (priv->baud_rate == 4800) ) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001185 dbg("Using custom termios settings for a baud rate of "
1186 "4800bps.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 /* define custom termios settings for NMEA protocol */
1188
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 tty->termios->c_iflag /* input modes - */
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001190 &= ~(IGNBRK /* disable ignore break */
1191 | BRKINT /* disable break causes interrupt */
1192 | PARMRK /* disable mark parity errors */
1193 | ISTRIP /* disable clear high bit of input char */
1194 | INLCR /* disable translate NL to CR */
1195 | IGNCR /* disable ignore CR */
1196 | ICRNL /* disable translate CR to NL */
1197 | IXON); /* disable enable XON/XOFF flow control */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001199 tty->termios->c_oflag /* output modes */
1200 &= ~OPOST; /* disable postprocess output char */
1201
1202 tty->termios->c_lflag /* line discipline modes */
1203 &= ~(ECHO /* disable echo input characters */
1204 | ECHONL /* disable echo new line */
1205 | ICANON /* disable erase, kill, werase, and rprnt
1206 special characters */
1207 | ISIG /* disable interrupt, quit, and suspend
1208 special characters */
1209 | IEXTEN); /* disable non-POSIX special characters */
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001210 } /* CT_CYPHIDCOM: Application should handle this for device */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 linechange = (priv->line_control != oldlines);
1213 spin_unlock_irqrestore(&priv->lock, flags);
1214
1215 /* if necessary, set lines */
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001216 if (linechange) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 priv->cmd_ctrl = 1;
1218 cypress_write(port, NULL, 0);
1219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220} /* cypress_set_termios */
1221
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001222
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223/* returns amount of data still left in soft buffer */
1224static int cypress_chars_in_buffer(struct usb_serial_port *port)
1225{
1226 struct cypress_private *priv = usb_get_serial_port_data(port);
1227 int chars = 0;
1228 unsigned long flags;
1229
1230 dbg("%s - port %d", __FUNCTION__, port->number);
1231
1232 spin_lock_irqsave(&priv->lock, flags);
1233 chars = cypress_buf_data_avail(priv->buf);
1234 spin_unlock_irqrestore(&priv->lock, flags);
1235
1236 dbg("%s - returns %d", __FUNCTION__, chars);
1237 return chars;
1238}
1239
1240
1241static void cypress_throttle (struct usb_serial_port *port)
1242{
1243 struct cypress_private *priv = usb_get_serial_port_data(port);
1244 unsigned long flags;
1245
1246 dbg("%s - port %d", __FUNCTION__, port->number);
1247
1248 spin_lock_irqsave(&priv->lock, flags);
1249 priv->rx_flags = THROTTLED;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001250 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251}
1252
1253
1254static void cypress_unthrottle (struct usb_serial_port *port)
1255{
1256 struct cypress_private *priv = usb_get_serial_port_data(port);
1257 int actually_throttled, result;
1258 unsigned long flags;
1259
1260 dbg("%s - port %d", __FUNCTION__, port->number);
1261
1262 spin_lock_irqsave(&priv->lock, flags);
1263 actually_throttled = priv->rx_flags & ACTUALLY_THROTTLED;
1264 priv->rx_flags = 0;
1265 spin_unlock_irqrestore(&priv->lock, flags);
1266
Mike Isely78aef512006-08-29 22:07:11 -05001267 if (!priv->comm_is_ok)
1268 return;
1269
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 if (actually_throttled) {
1271 port->interrupt_in_urb->dev = port->serial->dev;
1272
1273 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
Mike Isely78aef512006-08-29 22:07:11 -05001274 if (result) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001275 dev_err(&port->dev, "%s - failed submitting read urb, "
1276 "error %d\n", __FUNCTION__, result);
Mike Isely78aef512006-08-29 22:07:11 -05001277 cypress_set_dead(port);
1278 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 }
1280}
1281
1282
David Howells7d12e782006-10-05 14:55:46 +01001283static void cypress_read_int_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284{
1285 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
1286 struct cypress_private *priv = usb_get_serial_port_data(port);
1287 struct tty_struct *tty;
1288 unsigned char *data = urb->transfer_buffer;
1289 unsigned long flags;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001290 char tty_flag = TTY_NORMAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 int havedata = 0;
1292 int bytes = 0;
1293 int result;
1294 int i = 0;
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001295 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
1297 dbg("%s - port %d", __FUNCTION__, port->number);
1298
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001299 switch (status) {
Mike Isely78aef512006-08-29 22:07:11 -05001300 case 0: /* success */
1301 break;
1302 case -ECONNRESET:
1303 case -ENOENT:
1304 case -ESHUTDOWN:
1305 /* precursor to disconnect so just go away */
1306 return;
1307 case -EPIPE:
1308 usb_clear_halt(port->serial->dev,0x81);
1309 break;
1310 default:
1311 /* something ugly is going on... */
1312 dev_err(&urb->dev->dev,"%s - unexpected nonzero read status received: %d\n",
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001313 __FUNCTION__, status);
Mike Isely78aef512006-08-29 22:07:11 -05001314 cypress_set_dead(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 return;
1316 }
1317
1318 spin_lock_irqsave(&priv->lock, flags);
1319 if (priv->rx_flags & THROTTLED) {
1320 dbg("%s - now throttling", __FUNCTION__);
1321 priv->rx_flags |= ACTUALLY_THROTTLED;
1322 spin_unlock_irqrestore(&priv->lock, flags);
1323 return;
1324 }
1325 spin_unlock_irqrestore(&priv->lock, flags);
1326
1327 tty = port->tty;
1328 if (!tty) {
1329 dbg("%s - bad tty pointer - exiting", __FUNCTION__);
1330 return;
1331 }
1332
1333 spin_lock_irqsave(&priv->lock, flags);
1334 switch(urb->actual_length) {
1335 case 32:
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001336 /* This is for the CY7C64013... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 priv->current_status = data[0] & 0xF8;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001338 bytes = data[1] + 2;
1339 i = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 if (bytes > 2)
1341 havedata = 1;
1342 break;
1343 case 8:
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001344 /* This is for the CY7C63743... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 priv->current_status = data[0] & 0xF8;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001346 bytes = (data[0] & 0x07) + 1;
1347 i = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 if (bytes > 1)
1349 havedata = 1;
1350 break;
1351 default:
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001352 dbg("%s - wrong packet size - received %d bytes",
1353 __FUNCTION__, urb->actual_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 spin_unlock_irqrestore(&priv->lock, flags);
1355 goto continue_read;
1356 }
1357 spin_unlock_irqrestore(&priv->lock, flags);
1358
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001359 usb_serial_debug_data (debug, &port->dev, __FUNCTION__,
1360 urb->actual_length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
1362 spin_lock_irqsave(&priv->lock, flags);
1363 /* check to see if status has changed */
1364 if (priv != NULL) {
1365 if (priv->current_status != priv->prev_status) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001366 priv->diff_status |= priv->current_status ^
1367 priv->prev_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 wake_up_interruptible(&priv->delta_msr_wait);
1369 priv->prev_status = priv->current_status;
1370 }
1371 }
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001372 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001374 /* hangup, as defined in acm.c... this might be a bad place for it
1375 * though */
1376 if (tty && !(tty->termios->c_cflag & CLOCAL) &&
1377 !(priv->current_status & UART_CD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 dbg("%s - calling hangup", __FUNCTION__);
1379 tty_hangup(tty);
1380 goto continue_read;
1381 }
1382
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001383 /* There is one error bit... I'm assuming it is a parity error
1384 * indicator as the generic firmware will set this bit to 1 if a
1385 * parity error occurs.
1386 * I can not find reference to any other error events. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 spin_lock_irqsave(&priv->lock, flags);
1388 if (priv->current_status & CYP_ERROR) {
1389 spin_unlock_irqrestore(&priv->lock, flags);
1390 tty_flag = TTY_PARITY;
1391 dbg("%s - Parity Error detected", __FUNCTION__);
1392 } else
1393 spin_unlock_irqrestore(&priv->lock, flags);
1394
1395 /* process read if there is data other than line status */
1396 if (tty && (bytes > i)) {
Alan Cox33f0f882006-01-09 20:54:13 -08001397 bytes = tty_buffer_request_room(tty, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 for (; i < bytes ; ++i) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001399 dbg("pushing byte number %d - %d - %c", i, data[i],
1400 data[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 tty_insert_flip_char(tty, data[i], tty_flag);
1402 }
1403 tty_flip_buffer_push(port->tty);
1404 }
1405
1406 spin_lock_irqsave(&priv->lock, flags);
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001407 /* control and status byte(s) are also counted */
1408 priv->bytes_in += bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 spin_unlock_irqrestore(&priv->lock, flags);
1410
1411continue_read:
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001412
1413 /* Continue trying to always read... unless the port has closed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
Mike Isely78aef512006-08-29 22:07:11 -05001415 if (port->open_count > 0 && priv->comm_is_ok) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001416 usb_fill_int_urb(port->interrupt_in_urb, port->serial->dev,
1417 usb_rcvintpipe(port->serial->dev,
1418 port->interrupt_in_endpointAddress),
1419 port->interrupt_in_urb->transfer_buffer,
1420 port->interrupt_in_urb->transfer_buffer_length,
Mike Isely0257fa92006-08-29 22:06:59 -05001421 cypress_read_int_callback, port, priv->read_urb_interval);
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001422 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
Mike Isely78aef512006-08-29 22:07:11 -05001423 if (result) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001424 dev_err(&urb->dev->dev, "%s - failed resubmitting "
1425 "read urb, error %d\n", __FUNCTION__,
1426 result);
Mike Isely78aef512006-08-29 22:07:11 -05001427 cypress_set_dead(port);
1428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 }
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001430
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 return;
1432} /* cypress_read_int_callback */
1433
1434
David Howells7d12e782006-10-05 14:55:46 +01001435static void cypress_write_int_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436{
1437 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
1438 struct cypress_private *priv = usb_get_serial_port_data(port);
1439 int result;
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001440 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
1442 dbg("%s - port %d", __FUNCTION__, port->number);
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001443
1444 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 case 0:
1446 /* success */
1447 break;
1448 case -ECONNRESET:
1449 case -ENOENT:
1450 case -ESHUTDOWN:
1451 /* this urb is terminated, clean up */
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001452 dbg("%s - urb shutting down with status: %d",
1453 __FUNCTION__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 priv->write_urb_in_use = 0;
1455 return;
Mike Isely78aef512006-08-29 22:07:11 -05001456 case -EPIPE: /* no break needed; clear halt and resubmit */
1457 if (!priv->comm_is_ok)
1458 break;
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001459 usb_clear_halt(port->serial->dev, 0x02);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 /* error in the urb, so we have to resubmit it */
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001461 dbg("%s - nonzero write bulk status received: %d",
1462 __FUNCTION__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 port->interrupt_out_urb->transfer_buffer_length = 1;
1464 port->interrupt_out_urb->dev = port->serial->dev;
1465 result = usb_submit_urb(port->interrupt_out_urb, GFP_ATOMIC);
Mike Isely78aef512006-08-29 22:07:11 -05001466 if (!result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 return;
Mike Isely78aef512006-08-29 22:07:11 -05001468 dev_err(&urb->dev->dev, "%s - failed resubmitting write urb, error %d\n",
1469 __FUNCTION__, result);
1470 cypress_set_dead(port);
1471 break;
1472 default:
1473 dev_err(&urb->dev->dev,"%s - unexpected nonzero write status received: %d\n",
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001474 __FUNCTION__, status);
Mike Isely78aef512006-08-29 22:07:11 -05001475 cypress_set_dead(port);
1476 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 }
1478
1479 priv->write_urb_in_use = 0;
1480
1481 /* send any buffered data */
1482 cypress_send(port);
1483}
1484
1485
1486/*****************************************************************************
1487 * Write buffer functions - buffering code from pl2303 used
1488 *****************************************************************************/
1489
1490/*
1491 * cypress_buf_alloc
1492 *
1493 * Allocate a circular buffer and all associated memory.
1494 */
1495
1496static struct cypress_buf *cypress_buf_alloc(unsigned int size)
1497{
1498
1499 struct cypress_buf *cb;
1500
1501
1502 if (size == 0)
1503 return NULL;
1504
Robert P. J. Day5cbded52006-12-13 00:35:56 -08001505 cb = kmalloc(sizeof(struct cypress_buf), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 if (cb == NULL)
1507 return NULL;
1508
1509 cb->buf_buf = kmalloc(size, GFP_KERNEL);
1510 if (cb->buf_buf == NULL) {
1511 kfree(cb);
1512 return NULL;
1513 }
1514
1515 cb->buf_size = size;
1516 cb->buf_get = cb->buf_put = cb->buf_buf;
1517
1518 return cb;
1519
1520}
1521
1522
1523/*
1524 * cypress_buf_free
1525 *
1526 * Free the buffer and all associated memory.
1527 */
1528
1529static void cypress_buf_free(struct cypress_buf *cb)
1530{
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001531 if (cb) {
1532 kfree(cb->buf_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 kfree(cb);
1534 }
1535}
1536
1537
1538/*
1539 * cypress_buf_clear
1540 *
1541 * Clear out all data in the circular buffer.
1542 */
1543
1544static void cypress_buf_clear(struct cypress_buf *cb)
1545{
1546 if (cb != NULL)
1547 cb->buf_get = cb->buf_put;
1548 /* equivalent to a get of all data available */
1549}
1550
1551
1552/*
1553 * cypress_buf_data_avail
1554 *
1555 * Return the number of bytes of data available in the circular
1556 * buffer.
1557 */
1558
1559static unsigned int cypress_buf_data_avail(struct cypress_buf *cb)
1560{
1561 if (cb != NULL)
1562 return ((cb->buf_size + cb->buf_put - cb->buf_get) % cb->buf_size);
1563 else
1564 return 0;
1565}
1566
1567
1568/*
1569 * cypress_buf_space_avail
1570 *
1571 * Return the number of bytes of space available in the circular
1572 * buffer.
1573 */
1574
1575static unsigned int cypress_buf_space_avail(struct cypress_buf *cb)
1576{
1577 if (cb != NULL)
1578 return ((cb->buf_size + cb->buf_get - cb->buf_put - 1) % cb->buf_size);
1579 else
1580 return 0;
1581}
1582
1583
1584/*
1585 * cypress_buf_put
1586 *
1587 * Copy data data from a user buffer and put it into the circular buffer.
1588 * Restrict to the amount of space available.
1589 *
1590 * Return the number of bytes copied.
1591 */
1592
1593static unsigned int cypress_buf_put(struct cypress_buf *cb, const char *buf,
1594 unsigned int count)
1595{
1596
1597 unsigned int len;
1598
1599
1600 if (cb == NULL)
1601 return 0;
1602
1603 len = cypress_buf_space_avail(cb);
1604 if (count > len)
1605 count = len;
1606
1607 if (count == 0)
1608 return 0;
1609
1610 len = cb->buf_buf + cb->buf_size - cb->buf_put;
1611 if (count > len) {
1612 memcpy(cb->buf_put, buf, len);
1613 memcpy(cb->buf_buf, buf+len, count - len);
1614 cb->buf_put = cb->buf_buf + count - len;
1615 } else {
1616 memcpy(cb->buf_put, buf, count);
1617 if (count < len)
1618 cb->buf_put += count;
1619 else /* count == len */
1620 cb->buf_put = cb->buf_buf;
1621 }
1622
1623 return count;
1624
1625}
1626
1627
1628/*
1629 * cypress_buf_get
1630 *
1631 * Get data from the circular buffer and copy to the given buffer.
1632 * Restrict to the amount of data available.
1633 *
1634 * Return the number of bytes copied.
1635 */
1636
1637static unsigned int cypress_buf_get(struct cypress_buf *cb, char *buf,
1638 unsigned int count)
1639{
1640
1641 unsigned int len;
1642
1643
1644 if (cb == NULL)
1645 return 0;
1646
1647 len = cypress_buf_data_avail(cb);
1648 if (count > len)
1649 count = len;
1650
1651 if (count == 0)
1652 return 0;
1653
1654 len = cb->buf_buf + cb->buf_size - cb->buf_get;
1655 if (count > len) {
1656 memcpy(buf, cb->buf_get, len);
1657 memcpy(buf+len, cb->buf_buf, count - len);
1658 cb->buf_get = cb->buf_buf + count - len;
1659 } else {
1660 memcpy(buf, cb->buf_get, count);
1661 if (count < len)
1662 cb->buf_get += count;
1663 else /* count == len */
1664 cb->buf_get = cb->buf_buf;
1665 }
1666
1667 return count;
1668
1669}
1670
1671/*****************************************************************************
1672 * Module functions
1673 *****************************************************************************/
1674
1675static int __init cypress_init(void)
1676{
1677 int retval;
1678
1679 dbg("%s", __FUNCTION__);
1680
1681 retval = usb_serial_register(&cypress_earthmate_device);
1682 if (retval)
1683 goto failed_em_register;
1684 retval = usb_serial_register(&cypress_hidcom_device);
1685 if (retval)
1686 goto failed_hidcom_register;
Lonnie Mendeza5c44e22006-03-01 10:45:24 -06001687 retval = usb_serial_register(&cypress_ca42v2_device);
1688 if (retval)
1689 goto failed_ca42v2_register;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 retval = usb_register(&cypress_driver);
1691 if (retval)
1692 goto failed_usb_register;
1693
1694 info(DRIVER_DESC " " DRIVER_VERSION);
1695 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
Mariusz Kozlowski2e46b742006-11-17 17:49:22 +01001697failed_usb_register:
1698 usb_serial_deregister(&cypress_ca42v2_device);
1699failed_ca42v2_register:
1700 usb_serial_deregister(&cypress_hidcom_device);
1701failed_hidcom_register:
1702 usb_serial_deregister(&cypress_earthmate_device);
1703failed_em_register:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 return retval;
1705}
1706
1707
1708static void __exit cypress_exit (void)
1709{
1710 dbg("%s", __FUNCTION__);
1711
1712 usb_deregister (&cypress_driver);
1713 usb_serial_deregister (&cypress_earthmate_device);
1714 usb_serial_deregister (&cypress_hidcom_device);
Lonnie Mendeza5c44e22006-03-01 10:45:24 -06001715 usb_serial_deregister (&cypress_ca42v2_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716}
1717
1718
1719module_init(cypress_init);
1720module_exit(cypress_exit);
1721
1722MODULE_AUTHOR( DRIVER_AUTHOR );
1723MODULE_DESCRIPTION( DRIVER_DESC );
1724MODULE_VERSION( DRIVER_VERSION );
1725MODULE_LICENSE("GPL");
1726
1727module_param(debug, bool, S_IRUGO | S_IWUSR);
1728MODULE_PARM_DESC(debug, "Debug enabled or not");
1729module_param(stats, bool, S_IRUGO | S_IWUSR);
1730MODULE_PARM_DESC(stats, "Enable statistics or not");
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001731module_param(interval, int, S_IRUGO | S_IWUSR);
1732MODULE_PARM_DESC(interval, "Overrides interrupt interval");