blob: 779d07851a4d820a4e32c372ed2e745798c82a27 [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;
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500293 __u8 feature_buffer[8];
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
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500356 memset(feature_buffer, 0, 8);
357 /* 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 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 retval = usb_control_msg (port->serial->dev, usb_sndctrlpipe(port->serial->dev, 0),
374 HID_REQ_SET_REPORT, USB_DIR_OUT | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500375 0x0300, 0, feature_buffer, 8, 500);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500377 if (tries++ >= 3)
378 break;
379
Mike Isely48298e52006-08-29 22:07:07 -0500380 } while (retval != 8 && retval != -ENODEV);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500381
Mike Isely78aef512006-08-29 22:07:11 -0500382 if (retval != 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 err("%s - failed sending serial line settings - %d", __FUNCTION__, retval);
Mike Isely78aef512006-08-29 22:07:11 -0500384 cypress_set_dead(port);
385 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 spin_lock_irqsave(&priv->lock, flags);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500387 priv->baud_rate = new_baudrate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 priv->cbr_mask = baud_mask;
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500389 priv->current_config = feature_buffer[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 spin_unlock_irqrestore(&priv->lock, flags);
391 }
392 break;
393 case CYPRESS_GET_CONFIG:
394 dbg("%s - retreiving serial line settings", __FUNCTION__);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500395 /* set initial values in feature buffer */
396 memset(feature_buffer, 0, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500398 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 retval = usb_control_msg (port->serial->dev, usb_rcvctrlpipe(port->serial->dev, 0),
400 HID_REQ_GET_REPORT, USB_DIR_IN | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500401 0x0300, 0, feature_buffer, 8, 500);
402
403 if (tries++ >= 3)
404 break;
405
Mike Isely48298e52006-08-29 22:07:07 -0500406 } while (retval != 5 && retval != -ENODEV);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 if (retval != 5) {
Adrian Bunk943ffb52006-01-10 00:10:13 +0100409 err("%s - failed to retrieve serial line settings - %d", __FUNCTION__, retval);
Mike Isely78aef512006-08-29 22:07:11 -0500410 cypress_set_dead(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 return retval;
412 } else {
413 spin_lock_irqsave(&priv->lock, flags);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 /* store the config in one byte, and later use bit masks to check values */
416 priv->current_config = feature_buffer[4];
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500417 priv->baud_rate = *((u_int32_t *)feature_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500419 if ( (priv->cbr_mask = rate_to_mask(priv->baud_rate)) == 0x40)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 dbg("%s - failed setting the baud mask (not defined)", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 spin_unlock_irqrestore(&priv->lock, flags);
422 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 }
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500424 spin_lock_irqsave(&priv->lock, flags);
425 ++priv->cmd_count;
426 spin_unlock_irqrestore(&priv->lock, flags);
427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 return retval;
429} /* cypress_serial_control */
430
431
Mike Isely78aef512006-08-29 22:07:11 -0500432static void cypress_set_dead(struct usb_serial_port *port)
433{
434 struct cypress_private *priv = usb_get_serial_port_data(port);
435 unsigned long flags;
436
437 spin_lock_irqsave(&priv->lock, flags);
438 if (!priv->comm_is_ok) {
439 spin_unlock_irqrestore(&priv->lock, flags);
440 return;
441 }
442 priv->comm_is_ok = 0;
443 spin_unlock_irqrestore(&priv->lock, flags);
444
445 err("cypress_m8 suspending failing port %d - interval might be too short",
446 port->number);
447}
448
449
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500450/* given a baud mask, it will return integer baud on success */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451static int mask_to_rate (unsigned mask)
452{
453 int rate;
454
455 switch (mask) {
456 case B0: rate = 0; break;
457 case B300: rate = 300; break;
458 case B600: rate = 600; break;
459 case B1200: rate = 1200; break;
460 case B2400: rate = 2400; break;
461 case B4800: rate = 4800; break;
462 case B9600: rate = 9600; break;
463 case B19200: rate = 19200; break;
464 case B38400: rate = 38400; break;
465 case B57600: rate = 57600; break;
466 case B115200: rate = 115200; break;
467 default: rate = -1;
468 }
469
470 return rate;
471}
472
473
474static unsigned rate_to_mask (int rate)
475{
476 unsigned mask;
477
478 switch (rate) {
479 case 0: mask = B0; break;
480 case 300: mask = B300; break;
481 case 600: mask = B600; break;
482 case 1200: mask = B1200; break;
483 case 2400: mask = B2400; break;
484 case 4800: mask = B4800; break;
485 case 9600: mask = B9600; break;
486 case 19200: mask = B19200; break;
487 case 38400: mask = B38400; break;
488 case 57600: mask = B57600; break;
489 case 115200: mask = B115200; break;
490 default: mask = 0x40;
491 }
492
493 return mask;
494}
495/*****************************************************************************
496 * Cypress serial driver functions
497 *****************************************************************************/
498
499
500static int generic_startup (struct usb_serial *serial)
501{
502 struct cypress_private *priv;
Mike Isely0257fa92006-08-29 22:06:59 -0500503 struct usb_serial_port *port = serial->port[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Mike Isely0257fa92006-08-29 22:06:59 -0500505 dbg("%s - port %d", __FUNCTION__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +0100507 priv = kzalloc(sizeof (struct cypress_private), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 if (!priv)
509 return -ENOMEM;
510
Mike Isely78aef512006-08-29 22:07:11 -0500511 priv->comm_is_ok = !0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 spin_lock_init(&priv->lock);
513 priv->buf = cypress_buf_alloc(CYPRESS_BUF_SIZE);
514 if (priv->buf == NULL) {
515 kfree(priv);
516 return -ENOMEM;
517 }
518 init_waitqueue_head(&priv->delta_msr_wait);
519
520 usb_reset_configuration (serial->dev);
521
522 priv->cmd_ctrl = 0;
523 priv->line_control = 0;
524 priv->termios_initialized = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 priv->rx_flags = 0;
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500526 priv->cbr_mask = B300;
Mike Isely0257fa92006-08-29 22:06:59 -0500527 if (interval > 0) {
528 priv->write_urb_interval = interval;
529 priv->read_urb_interval = interval;
530 dbg("%s - port %d read & write intervals forced to %d",
531 __FUNCTION__,port->number,interval);
532 } else {
533 priv->write_urb_interval = port->interrupt_out_urb->interval;
534 priv->read_urb_interval = port->interrupt_in_urb->interval;
535 dbg("%s - port %d intervals: read=%d write=%d",
536 __FUNCTION__,port->number,
537 priv->read_urb_interval,priv->write_urb_interval);
538 }
539 usb_set_serial_port_data(port, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500541 return 0;
542}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
544
545static int cypress_earthmate_startup (struct usb_serial *serial)
546{
547 struct cypress_private *priv;
548
549 dbg("%s", __FUNCTION__);
550
551 if (generic_startup(serial)) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500552 dbg("%s - Failed setting up port %d", __FUNCTION__,
553 serial->port[0]->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 return 1;
555 }
556
557 priv = usb_get_serial_port_data(serial->port[0]);
558 priv->chiptype = CT_EARTHMATE;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500559
560 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561} /* cypress_earthmate_startup */
562
563
564static int cypress_hidcom_startup (struct usb_serial *serial)
565{
566 struct cypress_private *priv;
567
568 dbg("%s", __FUNCTION__);
569
570 if (generic_startup(serial)) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500571 dbg("%s - Failed setting up port %d", __FUNCTION__,
572 serial->port[0]->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 return 1;
574 }
575
576 priv = usb_get_serial_port_data(serial->port[0]);
577 priv->chiptype = CT_CYPHIDCOM;
578
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500579 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580} /* cypress_hidcom_startup */
581
582
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600583static int cypress_ca42v2_startup (struct usb_serial *serial)
584{
585 struct cypress_private *priv;
586
587 dbg("%s", __FUNCTION__);
588
589 if (generic_startup(serial)) {
590 dbg("%s - Failed setting up port %d", __FUNCTION__,
591 serial->port[0]->number);
592 return 1;
593 }
594
595 priv = usb_get_serial_port_data(serial->port[0]);
596 priv->chiptype = CT_CA42V2;
597
598 return 0;
599} /* cypress_ca42v2_startup */
600
601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602static void cypress_shutdown (struct usb_serial *serial)
603{
604 struct cypress_private *priv;
605
606 dbg ("%s - port %d", __FUNCTION__, serial->port[0]->number);
607
608 /* all open ports are closed at this point */
609
610 priv = usb_get_serial_port_data(serial->port[0]);
611
612 if (priv) {
613 cypress_buf_free(priv->buf);
614 kfree(priv);
615 usb_set_serial_port_data(serial->port[0], NULL);
616 }
617}
618
619
620static int cypress_open (struct usb_serial_port *port, struct file *filp)
621{
622 struct cypress_private *priv = usb_get_serial_port_data(port);
623 struct usb_serial *serial = port->serial;
624 unsigned long flags;
625 int result = 0;
626
627 dbg("%s - port %d", __FUNCTION__, port->number);
628
Mike Isely78aef512006-08-29 22:07:11 -0500629 if (!priv->comm_is_ok)
630 return -EIO;
631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 /* clear halts before open */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 usb_clear_halt(serial->dev, 0x81);
634 usb_clear_halt(serial->dev, 0x02);
635
636 spin_lock_irqsave(&priv->lock, flags);
637 /* reset read/write statistics */
638 priv->bytes_in = 0;
639 priv->bytes_out = 0;
640 priv->cmd_count = 0;
641 priv->rx_flags = 0;
642 spin_unlock_irqrestore(&priv->lock, flags);
643
644 /* setting to zero could cause data loss */
645 port->tty->low_latency = 1;
646
647 /* raise both lines and set termios */
648 spin_lock_irqsave(&priv->lock, flags);
649 priv->line_control = CONTROL_DTR | CONTROL_RTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 priv->cmd_ctrl = 1;
651 spin_unlock_irqrestore(&priv->lock, flags);
652 result = cypress_write(port, NULL, 0);
653
654 if (result) {
655 dev_err(&port->dev, "%s - failed setting the control lines - error %d\n", __FUNCTION__, result);
656 return result;
657 } else
658 dbg("%s - success setting the control lines", __FUNCTION__);
659
660 cypress_set_termios(port, &priv->tmp_termios);
661
662 /* setup the port and start reading from the device */
663 if(!port->interrupt_in_urb){
664 err("%s - interrupt_in_urb is empty!", __FUNCTION__);
665 return(-1);
666 }
667
668 usb_fill_int_urb(port->interrupt_in_urb, serial->dev,
669 usb_rcvintpipe(serial->dev, port->interrupt_in_endpointAddress),
670 port->interrupt_in_urb->transfer_buffer, port->interrupt_in_urb->transfer_buffer_length,
Mike Isely0257fa92006-08-29 22:06:59 -0500671 cypress_read_int_callback, port, priv->read_urb_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
673
674 if (result){
675 dev_err(&port->dev, "%s - failed submitting read urb, error %d\n", __FUNCTION__, result);
Mike Isely78aef512006-08-29 22:07:11 -0500676 cypress_set_dead(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 }
678
679 return result;
680} /* cypress_open */
681
682
683static void cypress_close(struct usb_serial_port *port, struct file * filp)
684{
685 struct cypress_private *priv = usb_get_serial_port_data(port);
686 unsigned int c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 int bps;
688 long timeout;
689 wait_queue_t wait;
690
691 dbg("%s - port %d", __FUNCTION__, port->number);
692
693 /* wait for data to drain from buffer */
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100694 spin_lock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 timeout = CYPRESS_CLOSING_WAIT;
696 init_waitqueue_entry(&wait, current);
697 add_wait_queue(&port->tty->write_wait, &wait);
698 for (;;) {
699 set_current_state(TASK_INTERRUPTIBLE);
700 if (cypress_buf_data_avail(priv->buf) == 0
701 || timeout == 0 || signal_pending(current)
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100702 /* without mutex, allowed due to harmless failure mode */
703 || port->serial->disconnected)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 break;
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100705 spin_unlock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 timeout = schedule_timeout(timeout);
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100707 spin_lock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 }
709 set_current_state(TASK_RUNNING);
710 remove_wait_queue(&port->tty->write_wait, &wait);
711 /* clear out any remaining data in the buffer */
712 cypress_buf_clear(priv->buf);
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100713 spin_unlock_irq(&priv->lock);
714
715 /* writing is potentially harmful, lock must be taken */
716 mutex_lock(&port->serial->disc_mutex);
717 if (port->serial->disconnected) {
718 mutex_unlock(&port->serial->disc_mutex);
719 return;
720 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 /* wait for characters to drain from device */
722 bps = tty_get_baud_rate(port->tty);
723 if (bps > 1200)
724 timeout = max((HZ*2560)/bps,HZ/10);
725 else
726 timeout = 2*HZ;
Nishanth Aravamudan22c43862005-08-15 11:30:11 -0700727 schedule_timeout_interruptible(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
729 dbg("%s - stopping urbs", __FUNCTION__);
730 usb_kill_urb (port->interrupt_in_urb);
731 usb_kill_urb (port->interrupt_out_urb);
732
733 if (port->tty) {
734 c_cflag = port->tty->termios->c_cflag;
735 if (c_cflag & HUPCL) {
736 /* drop dtr and rts */
737 priv = usb_get_serial_port_data(port);
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100738 spin_lock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 priv->line_control = 0;
740 priv->cmd_ctrl = 1;
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100741 spin_unlock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 cypress_write(port, NULL, 0);
743 }
744 }
745
746 if (stats)
747 dev_info (&port->dev, "Statistics: %d Bytes In | %d Bytes Out | %d Commands Issued\n",
748 priv->bytes_in, priv->bytes_out, priv->cmd_count);
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100749 mutex_unlock(&port->serial->disc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750} /* cypress_close */
751
752
753static int cypress_write(struct usb_serial_port *port, const unsigned char *buf, int count)
754{
755 struct cypress_private *priv = usb_get_serial_port_data(port);
756 unsigned long flags;
757
758 dbg("%s - port %d, %d bytes", __FUNCTION__, port->number, count);
759
760 /* line control commands, which need to be executed immediately,
761 are not put into the buffer for obvious reasons.
762 */
763 if (priv->cmd_ctrl) {
764 count = 0;
765 goto finish;
766 }
767
768 if (!count)
769 return count;
770
771 spin_lock_irqsave(&priv->lock, flags);
772 count = cypress_buf_put(priv->buf, buf, count);
773 spin_unlock_irqrestore(&priv->lock, flags);
774
775finish:
776 cypress_send(port);
777
778 return count;
779} /* cypress_write */
780
781
782static void cypress_send(struct usb_serial_port *port)
783{
784 int count = 0, result, offset, actual_size;
785 struct cypress_private *priv = usb_get_serial_port_data(port);
786 unsigned long flags;
787
Mike Isely78aef512006-08-29 22:07:11 -0500788 if (!priv->comm_is_ok)
789 return;
790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 dbg("%s - port %d", __FUNCTION__, port->number);
792 dbg("%s - interrupt out size is %d", __FUNCTION__, port->interrupt_out_size);
793
794 spin_lock_irqsave(&priv->lock, flags);
795 if (priv->write_urb_in_use) {
796 dbg("%s - can't write, urb in use", __FUNCTION__);
797 spin_unlock_irqrestore(&priv->lock, flags);
798 return;
799 }
800 spin_unlock_irqrestore(&priv->lock, flags);
801
802 /* clear buffer */
803 memset(port->interrupt_out_urb->transfer_buffer, 0, port->interrupt_out_size);
804
805 spin_lock_irqsave(&priv->lock, flags);
806 switch (port->interrupt_out_size) {
807 case 32:
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500808 /* this is for the CY7C64013... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 offset = 2;
810 port->interrupt_out_buffer[0] = priv->line_control;
811 break;
812 case 8:
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500813 /* this is for the CY7C63743... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 offset = 1;
815 port->interrupt_out_buffer[0] = priv->line_control;
816 break;
817 default:
818 dbg("%s - wrong packet size", __FUNCTION__);
819 spin_unlock_irqrestore(&priv->lock, flags);
820 return;
821 }
822
823 if (priv->line_control & CONTROL_RESET)
824 priv->line_control &= ~CONTROL_RESET;
825
826 if (priv->cmd_ctrl) {
827 priv->cmd_count++;
828 dbg("%s - line control command being issued", __FUNCTION__);
829 spin_unlock_irqrestore(&priv->lock, flags);
830 goto send;
831 } else
832 spin_unlock_irqrestore(&priv->lock, flags);
833
834 count = cypress_buf_get(priv->buf, &port->interrupt_out_buffer[offset],
835 port->interrupt_out_size-offset);
836
837 if (count == 0) {
838 return;
839 }
840
841 switch (port->interrupt_out_size) {
842 case 32:
843 port->interrupt_out_buffer[1] = count;
844 break;
845 case 8:
846 port->interrupt_out_buffer[0] |= count;
847 }
848
849 dbg("%s - count is %d", __FUNCTION__, count);
850
851send:
852 spin_lock_irqsave(&priv->lock, flags);
853 priv->write_urb_in_use = 1;
854 spin_unlock_irqrestore(&priv->lock, flags);
855
856 if (priv->cmd_ctrl)
857 actual_size = 1;
858 else
859 actual_size = count + (port->interrupt_out_size == 32 ? 2 : 1);
860
861 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, port->interrupt_out_size,
862 port->interrupt_out_urb->transfer_buffer);
863
Mike Isely9aa8dae2006-08-29 22:07:04 -0500864 usb_fill_int_urb(port->interrupt_out_urb, port->serial->dev,
865 usb_sndintpipe(port->serial->dev, port->interrupt_out_endpointAddress),
866 port->interrupt_out_buffer, port->interrupt_out_size,
867 cypress_write_int_callback, port, priv->write_urb_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 result = usb_submit_urb (port->interrupt_out_urb, GFP_ATOMIC);
869 if (result) {
870 dev_err(&port->dev, "%s - failed submitting write urb, error %d\n", __FUNCTION__,
871 result);
872 priv->write_urb_in_use = 0;
Mike Isely78aef512006-08-29 22:07:11 -0500873 cypress_set_dead(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 }
875
876 spin_lock_irqsave(&priv->lock, flags);
877 if (priv->cmd_ctrl) {
878 priv->cmd_ctrl = 0;
879 }
880 priv->bytes_out += count; /* do not count the line control and size bytes */
881 spin_unlock_irqrestore(&priv->lock, flags);
882
Pete Zaitcevcf2c7482006-05-22 21:58:49 -0700883 usb_serial_port_softint(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884} /* cypress_send */
885
886
887/* returns how much space is available in the soft buffer */
888static int cypress_write_room(struct usb_serial_port *port)
889{
890 struct cypress_private *priv = usb_get_serial_port_data(port);
891 int room = 0;
892 unsigned long flags;
893
894 dbg("%s - port %d", __FUNCTION__, port->number);
895
896 spin_lock_irqsave(&priv->lock, flags);
897 room = cypress_buf_space_avail(priv->buf);
898 spin_unlock_irqrestore(&priv->lock, flags);
899
900 dbg("%s - returns %d", __FUNCTION__, room);
901 return room;
902}
903
904
905static int cypress_tiocmget (struct usb_serial_port *port, struct file *file)
906{
907 struct cypress_private *priv = usb_get_serial_port_data(port);
908 __u8 status, control;
909 unsigned int result = 0;
910 unsigned long flags;
911
912 dbg("%s - port %d", __FUNCTION__, port->number);
913
914 spin_lock_irqsave(&priv->lock, flags);
915 control = priv->line_control;
916 status = priv->current_status;
917 spin_unlock_irqrestore(&priv->lock, flags);
918
919 result = ((control & CONTROL_DTR) ? TIOCM_DTR : 0)
920 | ((control & CONTROL_RTS) ? TIOCM_RTS : 0)
921 | ((status & UART_CTS) ? TIOCM_CTS : 0)
922 | ((status & UART_DSR) ? TIOCM_DSR : 0)
923 | ((status & UART_RI) ? TIOCM_RI : 0)
924 | ((status & UART_CD) ? TIOCM_CD : 0);
925
926 dbg("%s - result = %x", __FUNCTION__, result);
927
928 return result;
929}
930
931
932static int cypress_tiocmset (struct usb_serial_port *port, struct file *file,
933 unsigned int set, unsigned int clear)
934{
935 struct cypress_private *priv = usb_get_serial_port_data(port);
936 unsigned long flags;
937
938 dbg("%s - port %d", __FUNCTION__, port->number);
939
940 spin_lock_irqsave(&priv->lock, flags);
941 if (set & TIOCM_RTS)
942 priv->line_control |= CONTROL_RTS;
943 if (set & TIOCM_DTR)
944 priv->line_control |= CONTROL_DTR;
945 if (clear & TIOCM_RTS)
946 priv->line_control &= ~CONTROL_RTS;
947 if (clear & TIOCM_DTR)
948 priv->line_control &= ~CONTROL_DTR;
949 spin_unlock_irqrestore(&priv->lock, flags);
950
951 priv->cmd_ctrl = 1;
952 return cypress_write(port, NULL, 0);
953}
954
955
956static int cypress_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg)
957{
958 struct cypress_private *priv = usb_get_serial_port_data(port);
959
960 dbg("%s - port %d, cmd 0x%.4x", __FUNCTION__, port->number, cmd);
961
962 switch (cmd) {
963 case TIOCGSERIAL:
Alan Cox606d0992006-12-08 02:38:45 -0800964 if (copy_to_user((void __user *)arg, port->tty->termios, sizeof(struct ktermios))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 return -EFAULT;
966 }
967 return (0);
968 break;
969 case TIOCSSERIAL:
Alan Cox606d0992006-12-08 02:38:45 -0800970 if (copy_from_user(port->tty->termios, (void __user *)arg, sizeof(struct ktermios))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 return -EFAULT;
972 }
973 /* here we need to call cypress_set_termios to invoke the new settings */
974 cypress_set_termios(port, &priv->tmp_termios);
975 return (0);
976 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 /* This code comes from drivers/char/serial.c and ftdi_sio.c */
978 case TIOCMIWAIT:
979 while (priv != NULL) {
980 interruptible_sleep_on(&priv->delta_msr_wait);
981 /* see if a signal did it */
982 if (signal_pending(current))
983 return -ERESTARTSYS;
984 else {
985 char diff = priv->diff_status;
986
987 if (diff == 0) {
988 return -EIO; /* no change => error */
989 }
990
991 /* consume all events */
992 priv->diff_status = 0;
993
994 /* return 0 if caller wanted to know about these bits */
995 if ( ((arg & TIOCM_RNG) && (diff & UART_RI)) ||
996 ((arg & TIOCM_DSR) && (diff & UART_DSR)) ||
997 ((arg & TIOCM_CD) && (diff & UART_CD)) ||
998 ((arg & TIOCM_CTS) && (diff & UART_CTS)) ) {
999 return 0;
1000 }
1001 /* otherwise caller can't care less about what happened,
1002 * and so we continue to wait for more events.
1003 */
1004 }
1005 }
1006 return 0;
1007 break;
1008 default:
1009 break;
1010 }
1011
1012 dbg("%s - arg not supported - it was 0x%04x - check include/asm/ioctls.h", __FUNCTION__, cmd);
1013
1014 return -ENOIOCTLCMD;
1015} /* cypress_ioctl */
1016
1017
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001018static void cypress_set_termios (struct usb_serial_port *port,
Alan Cox606d0992006-12-08 02:38:45 -08001019 struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020{
1021 struct cypress_private *priv = usb_get_serial_port_data(port);
1022 struct tty_struct *tty;
1023 int data_bits, stop_bits, parity_type, parity_enable;
1024 unsigned cflag, iflag, baud_mask;
1025 unsigned long flags;
1026 __u8 oldlines;
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001027 int linechange = 0;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001028
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 dbg("%s - port %d", __FUNCTION__, port->number);
1030
1031 tty = port->tty;
1032 if ((!tty) || (!tty->termios)) {
1033 dbg("%s - no tty structures", __FUNCTION__);
1034 return;
1035 }
1036
1037 spin_lock_irqsave(&priv->lock, flags);
1038 if (!priv->termios_initialized) {
1039 if (priv->chiptype == CT_EARTHMATE) {
1040 *(tty->termios) = tty_std_termios;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001041 tty->termios->c_cflag = B4800 | CS8 | CREAD | HUPCL |
1042 CLOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 } else if (priv->chiptype == CT_CYPHIDCOM) {
1044 *(tty->termios) = tty_std_termios;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001045 tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
1046 CLOCAL;
Lonnie Mendeza5c44e22006-03-01 10:45:24 -06001047 } else if (priv->chiptype == CT_CA42V2) {
1048 *(tty->termios) = tty_std_termios;
1049 tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
1050 CLOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 }
1052 priv->termios_initialized = 1;
1053 }
1054 spin_unlock_irqrestore(&priv->lock, flags);
1055
1056 cflag = tty->termios->c_cflag;
1057 iflag = tty->termios->c_iflag;
1058
1059 /* check if there are new settings */
1060 if (old_termios) {
1061 if ((cflag != old_termios->c_cflag) ||
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001062 (RELEVANT_IFLAG(iflag) !=
1063 RELEVANT_IFLAG(old_termios->c_iflag))) {
1064 dbg("%s - attempting to set new termios settings",
1065 __FUNCTION__);
1066 /* should make a copy of this in case something goes
1067 * wrong in the function, we can restore it */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 spin_lock_irqsave(&priv->lock, flags);
1069 priv->tmp_termios = *(tty->termios);
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001070 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 } else {
1072 dbg("%s - nothing to do, exiting", __FUNCTION__);
1073 return;
1074 }
1075 } else
1076 return;
1077
1078 /* set number of data bits, parity, stop bits */
1079 /* when parity is disabled the parity type bit is ignored */
1080
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001081 /* 1 means 2 stop bits, 0 means 1 stop bit */
1082 stop_bits = cflag & CSTOPB ? 1 : 0;
1083
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 if (cflag & PARENB) {
1085 parity_enable = 1;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001086 /* 1 means odd parity, 0 means even parity */
1087 parity_type = cflag & PARODD ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 } else
1089 parity_enable = parity_type = 0;
1090
1091 if (cflag & CSIZE) {
1092 switch (cflag & CSIZE) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001093 case CS5:
1094 data_bits = 0;
1095 break;
1096 case CS6:
1097 data_bits = 1;
1098 break;
1099 case CS7:
1100 data_bits = 2;
1101 break;
1102 case CS8:
1103 data_bits = 3;
1104 break;
1105 default:
1106 err("%s - CSIZE was set, but not CS5-CS8",
1107 __FUNCTION__);
1108 data_bits = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 }
1110 } else
1111 data_bits = 3;
1112
1113 spin_lock_irqsave(&priv->lock, flags);
1114 oldlines = priv->line_control;
1115 if ((cflag & CBAUD) == B0) {
1116 /* drop dtr and rts */
1117 dbg("%s - dropping the lines, baud rate 0bps", __FUNCTION__);
1118 baud_mask = B0;
1119 priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
1120 } else {
1121 baud_mask = (cflag & CBAUD);
1122 switch(baud_mask) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001123 case B300:
1124 dbg("%s - setting baud 300bps", __FUNCTION__);
1125 break;
1126 case B600:
1127 dbg("%s - setting baud 600bps", __FUNCTION__);
1128 break;
1129 case B1200:
1130 dbg("%s - setting baud 1200bps", __FUNCTION__);
1131 break;
1132 case B2400:
1133 dbg("%s - setting baud 2400bps", __FUNCTION__);
1134 break;
1135 case B4800:
1136 dbg("%s - setting baud 4800bps", __FUNCTION__);
1137 break;
1138 case B9600:
1139 dbg("%s - setting baud 9600bps", __FUNCTION__);
1140 break;
1141 case B19200:
1142 dbg("%s - setting baud 19200bps", __FUNCTION__);
1143 break;
1144 case B38400:
1145 dbg("%s - setting baud 38400bps", __FUNCTION__);
1146 break;
1147 case B57600:
1148 dbg("%s - setting baud 57600bps", __FUNCTION__);
1149 break;
1150 case B115200:
1151 dbg("%s - setting baud 115200bps", __FUNCTION__);
1152 break;
1153 default:
1154 dbg("%s - unknown masked baud rate", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 }
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001156 priv->line_control = (CONTROL_DTR | CONTROL_RTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 }
1158 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001160 dbg("%s - sending %d stop_bits, %d parity_enable, %d parity_type, "
1161 "%d data_bits (+5)", __FUNCTION__, stop_bits,
1162 parity_enable, parity_type, data_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001164 cypress_serial_control(port, baud_mask, data_bits, stop_bits,
1165 parity_enable, parity_type, 0, CYPRESS_SET_CONFIG);
1166
1167 /* we perform a CYPRESS_GET_CONFIG so that the current settings are
1168 * filled into the private structure this should confirm that all is
1169 * working if it returns what we just set */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 cypress_serial_control(port, 0, 0, 0, 0, 0, 0, CYPRESS_GET_CONFIG);
1171
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001172 /* Here we can define custom tty settings for devices; the main tty
1173 * termios flag base comes from empeg.c */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001175 spin_lock_irqsave(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 if ( (priv->chiptype == CT_EARTHMATE) && (priv->baud_rate == 4800) ) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001177 dbg("Using custom termios settings for a baud rate of "
1178 "4800bps.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 /* define custom termios settings for NMEA protocol */
1180
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 tty->termios->c_iflag /* input modes - */
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001182 &= ~(IGNBRK /* disable ignore break */
1183 | BRKINT /* disable break causes interrupt */
1184 | PARMRK /* disable mark parity errors */
1185 | ISTRIP /* disable clear high bit of input char */
1186 | INLCR /* disable translate NL to CR */
1187 | IGNCR /* disable ignore CR */
1188 | ICRNL /* disable translate CR to NL */
1189 | IXON); /* disable enable XON/XOFF flow control */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001191 tty->termios->c_oflag /* output modes */
1192 &= ~OPOST; /* disable postprocess output char */
1193
1194 tty->termios->c_lflag /* line discipline modes */
1195 &= ~(ECHO /* disable echo input characters */
1196 | ECHONL /* disable echo new line */
1197 | ICANON /* disable erase, kill, werase, and rprnt
1198 special characters */
1199 | ISIG /* disable interrupt, quit, and suspend
1200 special characters */
1201 | IEXTEN); /* disable non-POSIX special characters */
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001202 } /* CT_CYPHIDCOM: Application should handle this for device */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 linechange = (priv->line_control != oldlines);
1205 spin_unlock_irqrestore(&priv->lock, flags);
1206
1207 /* if necessary, set lines */
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001208 if (linechange) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 priv->cmd_ctrl = 1;
1210 cypress_write(port, NULL, 0);
1211 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212} /* cypress_set_termios */
1213
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001214
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215/* returns amount of data still left in soft buffer */
1216static int cypress_chars_in_buffer(struct usb_serial_port *port)
1217{
1218 struct cypress_private *priv = usb_get_serial_port_data(port);
1219 int chars = 0;
1220 unsigned long flags;
1221
1222 dbg("%s - port %d", __FUNCTION__, port->number);
1223
1224 spin_lock_irqsave(&priv->lock, flags);
1225 chars = cypress_buf_data_avail(priv->buf);
1226 spin_unlock_irqrestore(&priv->lock, flags);
1227
1228 dbg("%s - returns %d", __FUNCTION__, chars);
1229 return chars;
1230}
1231
1232
1233static void cypress_throttle (struct usb_serial_port *port)
1234{
1235 struct cypress_private *priv = usb_get_serial_port_data(port);
1236 unsigned long flags;
1237
1238 dbg("%s - port %d", __FUNCTION__, port->number);
1239
1240 spin_lock_irqsave(&priv->lock, flags);
1241 priv->rx_flags = THROTTLED;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001242 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243}
1244
1245
1246static void cypress_unthrottle (struct usb_serial_port *port)
1247{
1248 struct cypress_private *priv = usb_get_serial_port_data(port);
1249 int actually_throttled, result;
1250 unsigned long flags;
1251
1252 dbg("%s - port %d", __FUNCTION__, port->number);
1253
1254 spin_lock_irqsave(&priv->lock, flags);
1255 actually_throttled = priv->rx_flags & ACTUALLY_THROTTLED;
1256 priv->rx_flags = 0;
1257 spin_unlock_irqrestore(&priv->lock, flags);
1258
Mike Isely78aef512006-08-29 22:07:11 -05001259 if (!priv->comm_is_ok)
1260 return;
1261
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 if (actually_throttled) {
1263 port->interrupt_in_urb->dev = port->serial->dev;
1264
1265 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
Mike Isely78aef512006-08-29 22:07:11 -05001266 if (result) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001267 dev_err(&port->dev, "%s - failed submitting read urb, "
1268 "error %d\n", __FUNCTION__, result);
Mike Isely78aef512006-08-29 22:07:11 -05001269 cypress_set_dead(port);
1270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 }
1272}
1273
1274
David Howells7d12e782006-10-05 14:55:46 +01001275static void cypress_read_int_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276{
1277 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
1278 struct cypress_private *priv = usb_get_serial_port_data(port);
1279 struct tty_struct *tty;
1280 unsigned char *data = urb->transfer_buffer;
1281 unsigned long flags;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001282 char tty_flag = TTY_NORMAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 int havedata = 0;
1284 int bytes = 0;
1285 int result;
1286 int i = 0;
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001287 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
1289 dbg("%s - port %d", __FUNCTION__, port->number);
1290
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001291 switch (status) {
Mike Isely78aef512006-08-29 22:07:11 -05001292 case 0: /* success */
1293 break;
1294 case -ECONNRESET:
1295 case -ENOENT:
1296 case -ESHUTDOWN:
1297 /* precursor to disconnect so just go away */
1298 return;
1299 case -EPIPE:
1300 usb_clear_halt(port->serial->dev,0x81);
1301 break;
1302 default:
1303 /* something ugly is going on... */
1304 dev_err(&urb->dev->dev,"%s - unexpected nonzero read status received: %d\n",
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001305 __FUNCTION__, status);
Mike Isely78aef512006-08-29 22:07:11 -05001306 cypress_set_dead(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 return;
1308 }
1309
1310 spin_lock_irqsave(&priv->lock, flags);
1311 if (priv->rx_flags & THROTTLED) {
1312 dbg("%s - now throttling", __FUNCTION__);
1313 priv->rx_flags |= ACTUALLY_THROTTLED;
1314 spin_unlock_irqrestore(&priv->lock, flags);
1315 return;
1316 }
1317 spin_unlock_irqrestore(&priv->lock, flags);
1318
1319 tty = port->tty;
1320 if (!tty) {
1321 dbg("%s - bad tty pointer - exiting", __FUNCTION__);
1322 return;
1323 }
1324
1325 spin_lock_irqsave(&priv->lock, flags);
1326 switch(urb->actual_length) {
1327 case 32:
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001328 /* This is for the CY7C64013... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 priv->current_status = data[0] & 0xF8;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001330 bytes = data[1] + 2;
1331 i = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 if (bytes > 2)
1333 havedata = 1;
1334 break;
1335 case 8:
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001336 /* This is for the CY7C63743... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 priv->current_status = data[0] & 0xF8;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001338 bytes = (data[0] & 0x07) + 1;
1339 i = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 if (bytes > 1)
1341 havedata = 1;
1342 break;
1343 default:
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001344 dbg("%s - wrong packet size - received %d bytes",
1345 __FUNCTION__, urb->actual_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 spin_unlock_irqrestore(&priv->lock, flags);
1347 goto continue_read;
1348 }
1349 spin_unlock_irqrestore(&priv->lock, flags);
1350
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001351 usb_serial_debug_data (debug, &port->dev, __FUNCTION__,
1352 urb->actual_length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
1354 spin_lock_irqsave(&priv->lock, flags);
1355 /* check to see if status has changed */
1356 if (priv != NULL) {
1357 if (priv->current_status != priv->prev_status) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001358 priv->diff_status |= priv->current_status ^
1359 priv->prev_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 wake_up_interruptible(&priv->delta_msr_wait);
1361 priv->prev_status = priv->current_status;
1362 }
1363 }
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001364 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001366 /* hangup, as defined in acm.c... this might be a bad place for it
1367 * though */
1368 if (tty && !(tty->termios->c_cflag & CLOCAL) &&
1369 !(priv->current_status & UART_CD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 dbg("%s - calling hangup", __FUNCTION__);
1371 tty_hangup(tty);
1372 goto continue_read;
1373 }
1374
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001375 /* There is one error bit... I'm assuming it is a parity error
1376 * indicator as the generic firmware will set this bit to 1 if a
1377 * parity error occurs.
1378 * I can not find reference to any other error events. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 spin_lock_irqsave(&priv->lock, flags);
1380 if (priv->current_status & CYP_ERROR) {
1381 spin_unlock_irqrestore(&priv->lock, flags);
1382 tty_flag = TTY_PARITY;
1383 dbg("%s - Parity Error detected", __FUNCTION__);
1384 } else
1385 spin_unlock_irqrestore(&priv->lock, flags);
1386
1387 /* process read if there is data other than line status */
1388 if (tty && (bytes > i)) {
Alan Cox33f0f882006-01-09 20:54:13 -08001389 bytes = tty_buffer_request_room(tty, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 for (; i < bytes ; ++i) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001391 dbg("pushing byte number %d - %d - %c", i, data[i],
1392 data[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 tty_insert_flip_char(tty, data[i], tty_flag);
1394 }
1395 tty_flip_buffer_push(port->tty);
1396 }
1397
1398 spin_lock_irqsave(&priv->lock, flags);
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001399 /* control and status byte(s) are also counted */
1400 priv->bytes_in += bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 spin_unlock_irqrestore(&priv->lock, flags);
1402
1403continue_read:
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001404
1405 /* Continue trying to always read... unless the port has closed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
Mike Isely78aef512006-08-29 22:07:11 -05001407 if (port->open_count > 0 && priv->comm_is_ok) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001408 usb_fill_int_urb(port->interrupt_in_urb, port->serial->dev,
1409 usb_rcvintpipe(port->serial->dev,
1410 port->interrupt_in_endpointAddress),
1411 port->interrupt_in_urb->transfer_buffer,
1412 port->interrupt_in_urb->transfer_buffer_length,
Mike Isely0257fa92006-08-29 22:06:59 -05001413 cypress_read_int_callback, port, priv->read_urb_interval);
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001414 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
Mike Isely78aef512006-08-29 22:07:11 -05001415 if (result) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001416 dev_err(&urb->dev->dev, "%s - failed resubmitting "
1417 "read urb, error %d\n", __FUNCTION__,
1418 result);
Mike Isely78aef512006-08-29 22:07:11 -05001419 cypress_set_dead(port);
1420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 }
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001422
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 return;
1424} /* cypress_read_int_callback */
1425
1426
David Howells7d12e782006-10-05 14:55:46 +01001427static void cypress_write_int_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428{
1429 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
1430 struct cypress_private *priv = usb_get_serial_port_data(port);
1431 int result;
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001432 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
1434 dbg("%s - port %d", __FUNCTION__, port->number);
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001435
1436 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 case 0:
1438 /* success */
1439 break;
1440 case -ECONNRESET:
1441 case -ENOENT:
1442 case -ESHUTDOWN:
1443 /* this urb is terminated, clean up */
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001444 dbg("%s - urb shutting down with status: %d",
1445 __FUNCTION__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 priv->write_urb_in_use = 0;
1447 return;
Mike Isely78aef512006-08-29 22:07:11 -05001448 case -EPIPE: /* no break needed; clear halt and resubmit */
1449 if (!priv->comm_is_ok)
1450 break;
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001451 usb_clear_halt(port->serial->dev, 0x02);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 /* error in the urb, so we have to resubmit it */
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001453 dbg("%s - nonzero write bulk status received: %d",
1454 __FUNCTION__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 port->interrupt_out_urb->transfer_buffer_length = 1;
1456 port->interrupt_out_urb->dev = port->serial->dev;
1457 result = usb_submit_urb(port->interrupt_out_urb, GFP_ATOMIC);
Mike Isely78aef512006-08-29 22:07:11 -05001458 if (!result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 return;
Mike Isely78aef512006-08-29 22:07:11 -05001460 dev_err(&urb->dev->dev, "%s - failed resubmitting write urb, error %d\n",
1461 __FUNCTION__, result);
1462 cypress_set_dead(port);
1463 break;
1464 default:
1465 dev_err(&urb->dev->dev,"%s - unexpected nonzero write status received: %d\n",
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001466 __FUNCTION__, status);
Mike Isely78aef512006-08-29 22:07:11 -05001467 cypress_set_dead(port);
1468 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 }
1470
1471 priv->write_urb_in_use = 0;
1472
1473 /* send any buffered data */
1474 cypress_send(port);
1475}
1476
1477
1478/*****************************************************************************
1479 * Write buffer functions - buffering code from pl2303 used
1480 *****************************************************************************/
1481
1482/*
1483 * cypress_buf_alloc
1484 *
1485 * Allocate a circular buffer and all associated memory.
1486 */
1487
1488static struct cypress_buf *cypress_buf_alloc(unsigned int size)
1489{
1490
1491 struct cypress_buf *cb;
1492
1493
1494 if (size == 0)
1495 return NULL;
1496
Robert P. J. Day5cbded52006-12-13 00:35:56 -08001497 cb = kmalloc(sizeof(struct cypress_buf), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 if (cb == NULL)
1499 return NULL;
1500
1501 cb->buf_buf = kmalloc(size, GFP_KERNEL);
1502 if (cb->buf_buf == NULL) {
1503 kfree(cb);
1504 return NULL;
1505 }
1506
1507 cb->buf_size = size;
1508 cb->buf_get = cb->buf_put = cb->buf_buf;
1509
1510 return cb;
1511
1512}
1513
1514
1515/*
1516 * cypress_buf_free
1517 *
1518 * Free the buffer and all associated memory.
1519 */
1520
1521static void cypress_buf_free(struct cypress_buf *cb)
1522{
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001523 if (cb) {
1524 kfree(cb->buf_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 kfree(cb);
1526 }
1527}
1528
1529
1530/*
1531 * cypress_buf_clear
1532 *
1533 * Clear out all data in the circular buffer.
1534 */
1535
1536static void cypress_buf_clear(struct cypress_buf *cb)
1537{
1538 if (cb != NULL)
1539 cb->buf_get = cb->buf_put;
1540 /* equivalent to a get of all data available */
1541}
1542
1543
1544/*
1545 * cypress_buf_data_avail
1546 *
1547 * Return the number of bytes of data available in the circular
1548 * buffer.
1549 */
1550
1551static unsigned int cypress_buf_data_avail(struct cypress_buf *cb)
1552{
1553 if (cb != NULL)
1554 return ((cb->buf_size + cb->buf_put - cb->buf_get) % cb->buf_size);
1555 else
1556 return 0;
1557}
1558
1559
1560/*
1561 * cypress_buf_space_avail
1562 *
1563 * Return the number of bytes of space available in the circular
1564 * buffer.
1565 */
1566
1567static unsigned int cypress_buf_space_avail(struct cypress_buf *cb)
1568{
1569 if (cb != NULL)
1570 return ((cb->buf_size + cb->buf_get - cb->buf_put - 1) % cb->buf_size);
1571 else
1572 return 0;
1573}
1574
1575
1576/*
1577 * cypress_buf_put
1578 *
1579 * Copy data data from a user buffer and put it into the circular buffer.
1580 * Restrict to the amount of space available.
1581 *
1582 * Return the number of bytes copied.
1583 */
1584
1585static unsigned int cypress_buf_put(struct cypress_buf *cb, const char *buf,
1586 unsigned int count)
1587{
1588
1589 unsigned int len;
1590
1591
1592 if (cb == NULL)
1593 return 0;
1594
1595 len = cypress_buf_space_avail(cb);
1596 if (count > len)
1597 count = len;
1598
1599 if (count == 0)
1600 return 0;
1601
1602 len = cb->buf_buf + cb->buf_size - cb->buf_put;
1603 if (count > len) {
1604 memcpy(cb->buf_put, buf, len);
1605 memcpy(cb->buf_buf, buf+len, count - len);
1606 cb->buf_put = cb->buf_buf + count - len;
1607 } else {
1608 memcpy(cb->buf_put, buf, count);
1609 if (count < len)
1610 cb->buf_put += count;
1611 else /* count == len */
1612 cb->buf_put = cb->buf_buf;
1613 }
1614
1615 return count;
1616
1617}
1618
1619
1620/*
1621 * cypress_buf_get
1622 *
1623 * Get data from the circular buffer and copy to the given buffer.
1624 * Restrict to the amount of data available.
1625 *
1626 * Return the number of bytes copied.
1627 */
1628
1629static unsigned int cypress_buf_get(struct cypress_buf *cb, char *buf,
1630 unsigned int count)
1631{
1632
1633 unsigned int len;
1634
1635
1636 if (cb == NULL)
1637 return 0;
1638
1639 len = cypress_buf_data_avail(cb);
1640 if (count > len)
1641 count = len;
1642
1643 if (count == 0)
1644 return 0;
1645
1646 len = cb->buf_buf + cb->buf_size - cb->buf_get;
1647 if (count > len) {
1648 memcpy(buf, cb->buf_get, len);
1649 memcpy(buf+len, cb->buf_buf, count - len);
1650 cb->buf_get = cb->buf_buf + count - len;
1651 } else {
1652 memcpy(buf, cb->buf_get, count);
1653 if (count < len)
1654 cb->buf_get += count;
1655 else /* count == len */
1656 cb->buf_get = cb->buf_buf;
1657 }
1658
1659 return count;
1660
1661}
1662
1663/*****************************************************************************
1664 * Module functions
1665 *****************************************************************************/
1666
1667static int __init cypress_init(void)
1668{
1669 int retval;
1670
1671 dbg("%s", __FUNCTION__);
1672
1673 retval = usb_serial_register(&cypress_earthmate_device);
1674 if (retval)
1675 goto failed_em_register;
1676 retval = usb_serial_register(&cypress_hidcom_device);
1677 if (retval)
1678 goto failed_hidcom_register;
Lonnie Mendeza5c44e22006-03-01 10:45:24 -06001679 retval = usb_serial_register(&cypress_ca42v2_device);
1680 if (retval)
1681 goto failed_ca42v2_register;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 retval = usb_register(&cypress_driver);
1683 if (retval)
1684 goto failed_usb_register;
1685
1686 info(DRIVER_DESC " " DRIVER_VERSION);
1687 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
Mariusz Kozlowski2e46b742006-11-17 17:49:22 +01001689failed_usb_register:
1690 usb_serial_deregister(&cypress_ca42v2_device);
1691failed_ca42v2_register:
1692 usb_serial_deregister(&cypress_hidcom_device);
1693failed_hidcom_register:
1694 usb_serial_deregister(&cypress_earthmate_device);
1695failed_em_register:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 return retval;
1697}
1698
1699
1700static void __exit cypress_exit (void)
1701{
1702 dbg("%s", __FUNCTION__);
1703
1704 usb_deregister (&cypress_driver);
1705 usb_serial_deregister (&cypress_earthmate_device);
1706 usb_serial_deregister (&cypress_hidcom_device);
Lonnie Mendeza5c44e22006-03-01 10:45:24 -06001707 usb_serial_deregister (&cypress_ca42v2_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708}
1709
1710
1711module_init(cypress_init);
1712module_exit(cypress_exit);
1713
1714MODULE_AUTHOR( DRIVER_AUTHOR );
1715MODULE_DESCRIPTION( DRIVER_DESC );
1716MODULE_VERSION( DRIVER_VERSION );
1717MODULE_LICENSE("GPL");
1718
1719module_param(debug, bool, S_IRUGO | S_IWUSR);
1720MODULE_PARM_DESC(debug, "Debug enabled or not");
1721module_param(stats, bool, S_IRUGO | S_IWUSR);
1722MODULE_PARM_DESC(stats, "Enable statistics or not");
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001723module_param(interval, int, S_IRUGO | S_IWUSR);
1724MODULE_PARM_DESC(interval, "Overrides interrupt interval");