blob: c42d3bdbe98ccb2aa4754f869837134cd7a0c848 [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
Mike Isely3416eaa2008-02-10 20:23:19 -0600125enum packet_format {
126 packet_format_1, /* b0:status, b1:payload count */
127 packet_format_2 /* b0[7:3]:status, b0[2:0]:payload count */
128};
129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130struct cypress_private {
131 spinlock_t lock; /* private lock */
132 int chiptype; /* identifier of device, for quirks/etc */
133 int bytes_in; /* used for statistics */
134 int bytes_out; /* used for statistics */
135 int cmd_count; /* used for statistics */
136 int cmd_ctrl; /* always set this to 1 before issuing a command */
137 struct cypress_buf *buf; /* write buffer */
138 int write_urb_in_use; /* write urb in use indicator */
Mike Isely0257fa92006-08-29 22:06:59 -0500139 int write_urb_interval; /* interval to use for write urb */
140 int read_urb_interval; /* interval to use for read urb */
Mike Isely78aef512006-08-29 22:07:11 -0500141 int comm_is_ok; /* true if communication is (still) ok */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 int termios_initialized;
143 __u8 line_control; /* holds dtr / rts value */
144 __u8 current_status; /* received from last read - info on dsr,cts,cd,ri,etc */
145 __u8 current_config; /* stores the current configuration byte */
146 __u8 rx_flags; /* throttling - used from whiteheat/ftdi_sio */
Mike Isely3416eaa2008-02-10 20:23:19 -0600147 enum packet_format pkt_fmt; /* format to use for packet send / receive */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 int baud_rate; /* stores current baud rate in integer form */
149 int cbr_mask; /* stores current baud rate in masked form */
150 int isthrottled; /* if throttled, discard reads */
151 wait_queue_head_t delta_msr_wait; /* used for TIOCMIWAIT */
152 char prev_status, diff_status; /* used for TIOCMIWAIT */
153 /* we pass a pointer to this as the arguement sent to cypress_set_termios old_termios */
Alan Cox606d0992006-12-08 02:38:45 -0800154 struct ktermios tmp_termios; /* stores the old termios settings */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155};
156
157/* write buffer structure */
158struct cypress_buf {
159 unsigned int buf_size;
160 char *buf_buf;
161 char *buf_get;
162 char *buf_put;
163};
164
165/* function prototypes for the Cypress USB to serial device */
166static int cypress_earthmate_startup (struct usb_serial *serial);
167static int cypress_hidcom_startup (struct usb_serial *serial);
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600168static int cypress_ca42v2_startup (struct usb_serial *serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169static void cypress_shutdown (struct usb_serial *serial);
170static int cypress_open (struct usb_serial_port *port, struct file *filp);
171static void cypress_close (struct usb_serial_port *port, struct file *filp);
172static int cypress_write (struct usb_serial_port *port, const unsigned char *buf, int count);
173static void cypress_send (struct usb_serial_port *port);
174static int cypress_write_room (struct usb_serial_port *port);
175static int cypress_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg);
Alan Cox606d0992006-12-08 02:38:45 -0800176static void cypress_set_termios (struct usb_serial_port *port, struct ktermios * old);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177static int cypress_tiocmget (struct usb_serial_port *port, struct file *file);
178static int cypress_tiocmset (struct usb_serial_port *port, struct file *file, unsigned int set, unsigned int clear);
179static int cypress_chars_in_buffer (struct usb_serial_port *port);
180static void cypress_throttle (struct usb_serial_port *port);
181static void cypress_unthrottle (struct usb_serial_port *port);
Mike Isely78aef512006-08-29 22:07:11 -0500182static void cypress_set_dead (struct usb_serial_port *port);
David Howells7d12e782006-10-05 14:55:46 +0100183static void cypress_read_int_callback (struct urb *urb);
184static void cypress_write_int_callback (struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185/* baud helper functions */
186static int mask_to_rate (unsigned mask);
187static unsigned rate_to_mask (int rate);
188/* write buffer functions */
189static struct cypress_buf *cypress_buf_alloc(unsigned int size);
190static void cypress_buf_free(struct cypress_buf *cb);
191static void cypress_buf_clear(struct cypress_buf *cb);
192static unsigned int cypress_buf_data_avail(struct cypress_buf *cb);
193static unsigned int cypress_buf_space_avail(struct cypress_buf *cb);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500194static unsigned int cypress_buf_put(struct cypress_buf *cb, const char *buf, unsigned int count);
195static unsigned int cypress_buf_get(struct cypress_buf *cb, char *buf, unsigned int count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700198static struct usb_serial_driver cypress_earthmate_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700199 .driver = {
200 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700201 .name = "earthmate",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700202 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700203 .description = "DeLorme Earthmate USB",
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100204 .usb_driver = &cypress_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 .id_table = id_table_earthmate,
206 .num_interrupt_in = 1,
207 .num_interrupt_out = 1,
208 .num_bulk_in = NUM_DONT_CARE,
209 .num_bulk_out = NUM_DONT_CARE,
210 .num_ports = 1,
211 .attach = cypress_earthmate_startup,
212 .shutdown = cypress_shutdown,
213 .open = cypress_open,
214 .close = cypress_close,
215 .write = cypress_write,
216 .write_room = cypress_write_room,
217 .ioctl = cypress_ioctl,
218 .set_termios = cypress_set_termios,
219 .tiocmget = cypress_tiocmget,
220 .tiocmset = cypress_tiocmset,
221 .chars_in_buffer = cypress_chars_in_buffer,
222 .throttle = cypress_throttle,
223 .unthrottle = cypress_unthrottle,
224 .read_int_callback = cypress_read_int_callback,
225 .write_int_callback = cypress_write_int_callback,
226};
227
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700228static struct usb_serial_driver cypress_hidcom_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700229 .driver = {
230 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700231 .name = "cyphidcom",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700232 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700233 .description = "HID->COM RS232 Adapter",
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100234 .usb_driver = &cypress_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 .id_table = id_table_cyphidcomrs232,
236 .num_interrupt_in = 1,
237 .num_interrupt_out = 1,
238 .num_bulk_in = NUM_DONT_CARE,
239 .num_bulk_out = NUM_DONT_CARE,
240 .num_ports = 1,
241 .attach = cypress_hidcom_startup,
242 .shutdown = cypress_shutdown,
243 .open = cypress_open,
244 .close = cypress_close,
245 .write = cypress_write,
246 .write_room = cypress_write_room,
247 .ioctl = cypress_ioctl,
248 .set_termios = cypress_set_termios,
249 .tiocmget = cypress_tiocmget,
250 .tiocmset = cypress_tiocmset,
251 .chars_in_buffer = cypress_chars_in_buffer,
252 .throttle = cypress_throttle,
253 .unthrottle = cypress_unthrottle,
254 .read_int_callback = cypress_read_int_callback,
255 .write_int_callback = cypress_write_int_callback,
256};
257
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600258static struct usb_serial_driver cypress_ca42v2_device = {
259 .driver = {
260 .owner = THIS_MODULE,
261 .name = "nokiaca42v2",
262 },
263 .description = "Nokia CA-42 V2 Adapter",
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100264 .usb_driver = &cypress_driver,
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600265 .id_table = id_table_nokiaca42v2,
266 .num_interrupt_in = 1,
267 .num_interrupt_out = 1,
268 .num_bulk_in = NUM_DONT_CARE,
269 .num_bulk_out = NUM_DONT_CARE,
270 .num_ports = 1,
271 .attach = cypress_ca42v2_startup,
272 .shutdown = cypress_shutdown,
273 .open = cypress_open,
274 .close = cypress_close,
275 .write = cypress_write,
276 .write_room = cypress_write_room,
277 .ioctl = cypress_ioctl,
278 .set_termios = cypress_set_termios,
279 .tiocmget = cypress_tiocmget,
280 .tiocmset = cypress_tiocmset,
281 .chars_in_buffer = cypress_chars_in_buffer,
282 .throttle = cypress_throttle,
283 .unthrottle = cypress_unthrottle,
284 .read_int_callback = cypress_read_int_callback,
285 .write_int_callback = cypress_write_int_callback,
286};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288/*****************************************************************************
289 * Cypress serial helper functions
290 *****************************************************************************/
291
292
Steven Cole093cf722005-05-03 19:07:24 -0600293/* This function can either set or retrieve the current serial line settings */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294static int cypress_serial_control (struct usb_serial_port *port, unsigned baud_mask, int data_bits, int stop_bits,
295 int parity_enable, int parity_type, int reset, int cypress_request_type)
296{
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500297 int new_baudrate = 0, retval = 0, tries = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 struct cypress_private *priv;
Mike Isely93075542008-02-10 20:23:14 -0600299 __u8 feature_buffer[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 unsigned long flags;
301
302 dbg("%s", __FUNCTION__);
303
304 priv = usb_get_serial_port_data(port);
305
Mike Isely78aef512006-08-29 22:07:11 -0500306 if (!priv->comm_is_ok)
307 return -ENODEV;
308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 switch(cypress_request_type) {
310 case CYPRESS_SET_CONFIG:
311
312 /*
313 * The general purpose firmware for the Cypress M8 allows for a maximum speed
314 * of 57600bps (I have no idea whether DeLorme chose to use the general purpose
315 * firmware or not), if you need to modify this speed setting for your own
316 * project please add your own chiptype and modify the code likewise. The
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500317 * Cypress HID->COM device will work successfully up to 115200bps (but the
318 * actual throughput is around 3kBps).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 */
320 if (baud_mask != priv->cbr_mask) {
321 dbg("%s - baud rate is changing", __FUNCTION__);
322 if ( priv->chiptype == CT_EARTHMATE ) {
323 /* 300 and 600 baud rates are supported under the generic firmware,
324 * but are not used with NMEA and SiRF protocols */
325
326 if ( (baud_mask == B300) || (baud_mask == B600) ) {
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500327 err("%s - failed setting baud rate, unsupported speed",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 __FUNCTION__);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500329 new_baudrate = priv->baud_rate;
330 } else 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 }
335 } else if (priv->chiptype == CT_CYPHIDCOM) {
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500336 if ( (new_baudrate = mask_to_rate(baud_mask)) == -1) {
337 err("%s - failed setting baud rate, unsupported speed",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 __FUNCTION__);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500339 new_baudrate = priv->baud_rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 }
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600341 } else if (priv->chiptype == CT_CA42V2) {
342 if ( (new_baudrate = mask_to_rate(baud_mask)) == -1) {
343 err("%s - failed setting baud rate, unsupported speed",
344 __FUNCTION__);
345 new_baudrate = priv->baud_rate;
346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 } else if (priv->chiptype == CT_GENERIC) {
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500348 if ( (new_baudrate = mask_to_rate(baud_mask)) == -1) {
349 err("%s - failed setting baud rate, unsupported speed",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 __FUNCTION__);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500351 new_baudrate = priv->baud_rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 }
353 } else {
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500354 info("%s - please define your chiptype", __FUNCTION__);
355 new_baudrate = priv->baud_rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 }
357 } else { /* baud rate not changing, keep the old */
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500358 new_baudrate = priv->baud_rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 }
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500360 dbg("%s - baud rate is being sent as %d", __FUNCTION__, new_baudrate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Mike Isely93075542008-02-10 20:23:14 -0600362 memset(feature_buffer, 0, sizeof(feature_buffer));
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500363 /* fill the feature_buffer with new configuration */
364 *((u_int32_t *)feature_buffer) = new_baudrate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500366 feature_buffer[4] |= data_bits; /* assign data bits in 2 bit space ( max 3 ) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 /* 1 bit gap */
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500368 feature_buffer[4] |= (stop_bits << 3); /* assign stop bits in 1 bit space */
369 feature_buffer[4] |= (parity_enable << 4); /* assign parity flag in 1 bit space */
370 feature_buffer[4] |= (parity_type << 5); /* assign parity type in 1 bit space */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 /* 1 bit gap */
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500372 feature_buffer[4] |= (reset << 7); /* assign reset at end of byte, 1 bit space */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374 dbg("%s - device is being sent this feature report:", __FUNCTION__);
375 dbg("%s - %02X - %02X - %02X - %02X - %02X", __FUNCTION__, feature_buffer[0], feature_buffer[1],
376 feature_buffer[2], feature_buffer[3], feature_buffer[4]);
377
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500378 do {
Mike Isely93075542008-02-10 20:23:14 -0600379 retval = usb_control_msg(port->serial->dev,
380 usb_sndctrlpipe(port->serial->dev, 0),
381 HID_REQ_SET_REPORT,
382 USB_DIR_OUT | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
383 0x0300, 0, feature_buffer,
384 sizeof(feature_buffer), 500);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500386 if (tries++ >= 3)
387 break;
388
Mike Isely93075542008-02-10 20:23:14 -0600389 } while (retval != sizeof(feature_buffer) &&
390 retval != -ENODEV);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500391
Mike Isely93075542008-02-10 20:23:14 -0600392 if (retval != sizeof(feature_buffer)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 err("%s - failed sending serial line settings - %d", __FUNCTION__, retval);
Mike Isely78aef512006-08-29 22:07:11 -0500394 cypress_set_dead(port);
395 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 spin_lock_irqsave(&priv->lock, flags);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500397 priv->baud_rate = new_baudrate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 priv->cbr_mask = baud_mask;
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500399 priv->current_config = feature_buffer[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 spin_unlock_irqrestore(&priv->lock, flags);
401 }
402 break;
403 case CYPRESS_GET_CONFIG:
404 dbg("%s - retreiving serial line settings", __FUNCTION__);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500405 /* set initial values in feature buffer */
Mike Isely93075542008-02-10 20:23:14 -0600406 memset(feature_buffer, 0, sizeof(feature_buffer));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500408 do {
Mike Isely93075542008-02-10 20:23:14 -0600409 retval = usb_control_msg(port->serial->dev,
410 usb_rcvctrlpipe(port->serial->dev, 0),
411 HID_REQ_GET_REPORT,
412 USB_DIR_IN | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
413 0x0300, 0, feature_buffer,
414 sizeof(feature_buffer), 500);
415
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500416 if (tries++ >= 3)
417 break;
418
Mike Isely93075542008-02-10 20:23:14 -0600419 } while (retval != sizeof(feature_buffer) &&
420 retval != -ENODEV);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500421
Mike Isely93075542008-02-10 20:23:14 -0600422 if (retval != sizeof(feature_buffer)) {
Adrian Bunk943ffb52006-01-10 00:10:13 +0100423 err("%s - failed to retrieve serial line settings - %d", __FUNCTION__, retval);
Mike Isely78aef512006-08-29 22:07:11 -0500424 cypress_set_dead(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 return retval;
426 } else {
427 spin_lock_irqsave(&priv->lock, flags);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 /* store the config in one byte, and later use bit masks to check values */
430 priv->current_config = feature_buffer[4];
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500431 priv->baud_rate = *((u_int32_t *)feature_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500433 if ( (priv->cbr_mask = rate_to_mask(priv->baud_rate)) == 0x40)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 dbg("%s - failed setting the baud mask (not defined)", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 spin_unlock_irqrestore(&priv->lock, flags);
436 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 }
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500438 spin_lock_irqsave(&priv->lock, flags);
439 ++priv->cmd_count;
440 spin_unlock_irqrestore(&priv->lock, flags);
441
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 return retval;
443} /* cypress_serial_control */
444
445
Mike Isely78aef512006-08-29 22:07:11 -0500446static void cypress_set_dead(struct usb_serial_port *port)
447{
448 struct cypress_private *priv = usb_get_serial_port_data(port);
449 unsigned long flags;
450
451 spin_lock_irqsave(&priv->lock, flags);
452 if (!priv->comm_is_ok) {
453 spin_unlock_irqrestore(&priv->lock, flags);
454 return;
455 }
456 priv->comm_is_ok = 0;
457 spin_unlock_irqrestore(&priv->lock, flags);
458
459 err("cypress_m8 suspending failing port %d - interval might be too short",
460 port->number);
461}
462
463
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500464/* given a baud mask, it will return integer baud on success */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465static int mask_to_rate (unsigned mask)
466{
467 int rate;
468
469 switch (mask) {
470 case B0: rate = 0; break;
471 case B300: rate = 300; break;
472 case B600: rate = 600; break;
473 case B1200: rate = 1200; break;
474 case B2400: rate = 2400; break;
475 case B4800: rate = 4800; break;
476 case B9600: rate = 9600; break;
477 case B19200: rate = 19200; break;
478 case B38400: rate = 38400; break;
479 case B57600: rate = 57600; break;
480 case B115200: rate = 115200; break;
481 default: rate = -1;
482 }
483
484 return rate;
485}
486
487
488static unsigned rate_to_mask (int rate)
489{
490 unsigned mask;
491
492 switch (rate) {
493 case 0: mask = B0; break;
494 case 300: mask = B300; break;
495 case 600: mask = B600; break;
496 case 1200: mask = B1200; break;
497 case 2400: mask = B2400; break;
498 case 4800: mask = B4800; break;
499 case 9600: mask = B9600; break;
500 case 19200: mask = B19200; break;
501 case 38400: mask = B38400; break;
502 case 57600: mask = B57600; break;
503 case 115200: mask = B115200; break;
504 default: mask = 0x40;
505 }
506
507 return mask;
508}
509/*****************************************************************************
510 * Cypress serial driver functions
511 *****************************************************************************/
512
513
514static int generic_startup (struct usb_serial *serial)
515{
516 struct cypress_private *priv;
Mike Isely0257fa92006-08-29 22:06:59 -0500517 struct usb_serial_port *port = serial->port[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Mike Isely0257fa92006-08-29 22:06:59 -0500519 dbg("%s - port %d", __FUNCTION__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +0100521 priv = kzalloc(sizeof (struct cypress_private), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 if (!priv)
523 return -ENOMEM;
524
Mike Isely78aef512006-08-29 22:07:11 -0500525 priv->comm_is_ok = !0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 spin_lock_init(&priv->lock);
527 priv->buf = cypress_buf_alloc(CYPRESS_BUF_SIZE);
528 if (priv->buf == NULL) {
529 kfree(priv);
530 return -ENOMEM;
531 }
532 init_waitqueue_head(&priv->delta_msr_wait);
533
534 usb_reset_configuration (serial->dev);
535
536 priv->cmd_ctrl = 0;
537 priv->line_control = 0;
538 priv->termios_initialized = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 priv->rx_flags = 0;
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500540 priv->cbr_mask = B300;
Mike Isely3416eaa2008-02-10 20:23:19 -0600541 /* Default packet format setting is determined by packet size.
542 Anything with a size larger then 9 must have a separate
543 count field since the 3 bit count field is otherwise too
544 small. Otherwise we can use the slightly more compact
545 format. This is in accordance with the cypress_m8 serial
546 converter app note. */
547 if (port->interrupt_out_size > 9) {
548 priv->pkt_fmt = packet_format_1;
549 } else {
550 priv->pkt_fmt = packet_format_2;
551 }
Mike Isely0257fa92006-08-29 22:06:59 -0500552 if (interval > 0) {
553 priv->write_urb_interval = interval;
554 priv->read_urb_interval = interval;
555 dbg("%s - port %d read & write intervals forced to %d",
556 __FUNCTION__,port->number,interval);
557 } else {
558 priv->write_urb_interval = port->interrupt_out_urb->interval;
559 priv->read_urb_interval = port->interrupt_in_urb->interval;
560 dbg("%s - port %d intervals: read=%d write=%d",
561 __FUNCTION__,port->number,
562 priv->read_urb_interval,priv->write_urb_interval);
563 }
564 usb_set_serial_port_data(port, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500566 return 0;
567}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569
570static int cypress_earthmate_startup (struct usb_serial *serial)
571{
572 struct cypress_private *priv;
573
574 dbg("%s", __FUNCTION__);
575
576 if (generic_startup(serial)) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500577 dbg("%s - Failed setting up port %d", __FUNCTION__,
578 serial->port[0]->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 return 1;
580 }
581
582 priv = usb_get_serial_port_data(serial->port[0]);
583 priv->chiptype = CT_EARTHMATE;
Mike Isely3416eaa2008-02-10 20:23:19 -0600584 /* All Earthmate devices use the separated-count packet
585 format! Idiotic. */
586 priv->pkt_fmt = packet_format_1;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500587
588 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589} /* cypress_earthmate_startup */
590
591
592static int cypress_hidcom_startup (struct usb_serial *serial)
593{
594 struct cypress_private *priv;
595
596 dbg("%s", __FUNCTION__);
597
598 if (generic_startup(serial)) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500599 dbg("%s - Failed setting up port %d", __FUNCTION__,
600 serial->port[0]->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 return 1;
602 }
603
604 priv = usb_get_serial_port_data(serial->port[0]);
605 priv->chiptype = CT_CYPHIDCOM;
606
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500607 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608} /* cypress_hidcom_startup */
609
610
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600611static int cypress_ca42v2_startup (struct usb_serial *serial)
612{
613 struct cypress_private *priv;
614
615 dbg("%s", __FUNCTION__);
616
617 if (generic_startup(serial)) {
618 dbg("%s - Failed setting up port %d", __FUNCTION__,
619 serial->port[0]->number);
620 return 1;
621 }
622
623 priv = usb_get_serial_port_data(serial->port[0]);
624 priv->chiptype = CT_CA42V2;
625
626 return 0;
627} /* cypress_ca42v2_startup */
628
629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630static void cypress_shutdown (struct usb_serial *serial)
631{
632 struct cypress_private *priv;
633
634 dbg ("%s - port %d", __FUNCTION__, serial->port[0]->number);
635
636 /* all open ports are closed at this point */
637
638 priv = usb_get_serial_port_data(serial->port[0]);
639
640 if (priv) {
641 cypress_buf_free(priv->buf);
642 kfree(priv);
643 usb_set_serial_port_data(serial->port[0], NULL);
644 }
645}
646
647
648static int cypress_open (struct usb_serial_port *port, struct file *filp)
649{
650 struct cypress_private *priv = usb_get_serial_port_data(port);
651 struct usb_serial *serial = port->serial;
652 unsigned long flags;
653 int result = 0;
654
655 dbg("%s - port %d", __FUNCTION__, port->number);
656
Mike Isely78aef512006-08-29 22:07:11 -0500657 if (!priv->comm_is_ok)
658 return -EIO;
659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 /* clear halts before open */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 usb_clear_halt(serial->dev, 0x81);
662 usb_clear_halt(serial->dev, 0x02);
663
664 spin_lock_irqsave(&priv->lock, flags);
665 /* reset read/write statistics */
666 priv->bytes_in = 0;
667 priv->bytes_out = 0;
668 priv->cmd_count = 0;
669 priv->rx_flags = 0;
670 spin_unlock_irqrestore(&priv->lock, flags);
671
672 /* setting to zero could cause data loss */
673 port->tty->low_latency = 1;
674
675 /* raise both lines and set termios */
676 spin_lock_irqsave(&priv->lock, flags);
677 priv->line_control = CONTROL_DTR | CONTROL_RTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 priv->cmd_ctrl = 1;
679 spin_unlock_irqrestore(&priv->lock, flags);
680 result = cypress_write(port, NULL, 0);
681
682 if (result) {
683 dev_err(&port->dev, "%s - failed setting the control lines - error %d\n", __FUNCTION__, result);
684 return result;
685 } else
686 dbg("%s - success setting the control lines", __FUNCTION__);
687
688 cypress_set_termios(port, &priv->tmp_termios);
689
690 /* setup the port and start reading from the device */
691 if(!port->interrupt_in_urb){
692 err("%s - interrupt_in_urb is empty!", __FUNCTION__);
693 return(-1);
694 }
695
696 usb_fill_int_urb(port->interrupt_in_urb, serial->dev,
697 usb_rcvintpipe(serial->dev, port->interrupt_in_endpointAddress),
698 port->interrupt_in_urb->transfer_buffer, port->interrupt_in_urb->transfer_buffer_length,
Mike Isely0257fa92006-08-29 22:06:59 -0500699 cypress_read_int_callback, port, priv->read_urb_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
701
702 if (result){
703 dev_err(&port->dev, "%s - failed submitting read urb, error %d\n", __FUNCTION__, result);
Mike Isely78aef512006-08-29 22:07:11 -0500704 cypress_set_dead(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 }
706
707 return result;
708} /* cypress_open */
709
710
711static void cypress_close(struct usb_serial_port *port, struct file * filp)
712{
713 struct cypress_private *priv = usb_get_serial_port_data(port);
714 unsigned int c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 int bps;
716 long timeout;
717 wait_queue_t wait;
718
719 dbg("%s - port %d", __FUNCTION__, port->number);
720
721 /* wait for data to drain from buffer */
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100722 spin_lock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 timeout = CYPRESS_CLOSING_WAIT;
724 init_waitqueue_entry(&wait, current);
725 add_wait_queue(&port->tty->write_wait, &wait);
726 for (;;) {
727 set_current_state(TASK_INTERRUPTIBLE);
728 if (cypress_buf_data_avail(priv->buf) == 0
729 || timeout == 0 || signal_pending(current)
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100730 /* without mutex, allowed due to harmless failure mode */
731 || port->serial->disconnected)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 break;
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100733 spin_unlock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 timeout = schedule_timeout(timeout);
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100735 spin_lock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 }
737 set_current_state(TASK_RUNNING);
738 remove_wait_queue(&port->tty->write_wait, &wait);
739 /* clear out any remaining data in the buffer */
740 cypress_buf_clear(priv->buf);
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100741 spin_unlock_irq(&priv->lock);
742
743 /* writing is potentially harmful, lock must be taken */
744 mutex_lock(&port->serial->disc_mutex);
745 if (port->serial->disconnected) {
746 mutex_unlock(&port->serial->disc_mutex);
747 return;
748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 /* wait for characters to drain from device */
750 bps = tty_get_baud_rate(port->tty);
751 if (bps > 1200)
752 timeout = max((HZ*2560)/bps,HZ/10);
753 else
754 timeout = 2*HZ;
Nishanth Aravamudan22c43862005-08-15 11:30:11 -0700755 schedule_timeout_interruptible(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
757 dbg("%s - stopping urbs", __FUNCTION__);
758 usb_kill_urb (port->interrupt_in_urb);
759 usb_kill_urb (port->interrupt_out_urb);
760
761 if (port->tty) {
762 c_cflag = port->tty->termios->c_cflag;
763 if (c_cflag & HUPCL) {
764 /* drop dtr and rts */
765 priv = usb_get_serial_port_data(port);
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100766 spin_lock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 priv->line_control = 0;
768 priv->cmd_ctrl = 1;
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100769 spin_unlock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 cypress_write(port, NULL, 0);
771 }
772 }
773
774 if (stats)
775 dev_info (&port->dev, "Statistics: %d Bytes In | %d Bytes Out | %d Commands Issued\n",
776 priv->bytes_in, priv->bytes_out, priv->cmd_count);
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100777 mutex_unlock(&port->serial->disc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778} /* cypress_close */
779
780
781static int cypress_write(struct usb_serial_port *port, const unsigned char *buf, int count)
782{
783 struct cypress_private *priv = usb_get_serial_port_data(port);
784 unsigned long flags;
785
786 dbg("%s - port %d, %d bytes", __FUNCTION__, port->number, count);
787
788 /* line control commands, which need to be executed immediately,
789 are not put into the buffer for obvious reasons.
790 */
791 if (priv->cmd_ctrl) {
792 count = 0;
793 goto finish;
794 }
795
796 if (!count)
797 return count;
798
799 spin_lock_irqsave(&priv->lock, flags);
800 count = cypress_buf_put(priv->buf, buf, count);
801 spin_unlock_irqrestore(&priv->lock, flags);
802
803finish:
804 cypress_send(port);
805
806 return count;
807} /* cypress_write */
808
809
810static void cypress_send(struct usb_serial_port *port)
811{
812 int count = 0, result, offset, actual_size;
813 struct cypress_private *priv = usb_get_serial_port_data(port);
814 unsigned long flags;
815
Mike Isely78aef512006-08-29 22:07:11 -0500816 if (!priv->comm_is_ok)
817 return;
818
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 dbg("%s - port %d", __FUNCTION__, port->number);
820 dbg("%s - interrupt out size is %d", __FUNCTION__, port->interrupt_out_size);
821
822 spin_lock_irqsave(&priv->lock, flags);
823 if (priv->write_urb_in_use) {
824 dbg("%s - can't write, urb in use", __FUNCTION__);
825 spin_unlock_irqrestore(&priv->lock, flags);
826 return;
827 }
828 spin_unlock_irqrestore(&priv->lock, flags);
829
830 /* clear buffer */
831 memset(port->interrupt_out_urb->transfer_buffer, 0, port->interrupt_out_size);
832
833 spin_lock_irqsave(&priv->lock, flags);
Mike Isely3416eaa2008-02-10 20:23:19 -0600834 switch (priv->pkt_fmt) {
835 default:
836 case packet_format_1:
837 /* this is for the CY7C64013... */
838 offset = 2;
839 port->interrupt_out_buffer[0] = priv->line_control;
840 break;
841 case packet_format_2:
842 /* this is for the CY7C63743... */
843 offset = 1;
844 port->interrupt_out_buffer[0] = priv->line_control;
845 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 }
847
848 if (priv->line_control & CONTROL_RESET)
849 priv->line_control &= ~CONTROL_RESET;
850
851 if (priv->cmd_ctrl) {
852 priv->cmd_count++;
853 dbg("%s - line control command being issued", __FUNCTION__);
854 spin_unlock_irqrestore(&priv->lock, flags);
855 goto send;
856 } else
857 spin_unlock_irqrestore(&priv->lock, flags);
858
859 count = cypress_buf_get(priv->buf, &port->interrupt_out_buffer[offset],
860 port->interrupt_out_size-offset);
861
862 if (count == 0) {
863 return;
864 }
865
Mike Isely3416eaa2008-02-10 20:23:19 -0600866 switch (priv->pkt_fmt) {
867 default:
868 case packet_format_1:
869 port->interrupt_out_buffer[1] = count;
870 break;
871 case packet_format_2:
872 port->interrupt_out_buffer[0] |= count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 }
874
875 dbg("%s - count is %d", __FUNCTION__, count);
876
877send:
878 spin_lock_irqsave(&priv->lock, flags);
879 priv->write_urb_in_use = 1;
880 spin_unlock_irqrestore(&priv->lock, flags);
881
882 if (priv->cmd_ctrl)
883 actual_size = 1;
884 else
Mike Isely3416eaa2008-02-10 20:23:19 -0600885 actual_size = count +
886 (priv->pkt_fmt == packet_format_1 ? 2 : 1);
887
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, port->interrupt_out_size,
889 port->interrupt_out_urb->transfer_buffer);
890
Mike Isely9aa8dae2006-08-29 22:07:04 -0500891 usb_fill_int_urb(port->interrupt_out_urb, port->serial->dev,
892 usb_sndintpipe(port->serial->dev, port->interrupt_out_endpointAddress),
893 port->interrupt_out_buffer, port->interrupt_out_size,
894 cypress_write_int_callback, port, priv->write_urb_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 result = usb_submit_urb (port->interrupt_out_urb, GFP_ATOMIC);
896 if (result) {
897 dev_err(&port->dev, "%s - failed submitting write urb, error %d\n", __FUNCTION__,
898 result);
899 priv->write_urb_in_use = 0;
Mike Isely78aef512006-08-29 22:07:11 -0500900 cypress_set_dead(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 }
902
903 spin_lock_irqsave(&priv->lock, flags);
904 if (priv->cmd_ctrl) {
905 priv->cmd_ctrl = 0;
906 }
907 priv->bytes_out += count; /* do not count the line control and size bytes */
908 spin_unlock_irqrestore(&priv->lock, flags);
909
Pete Zaitcevcf2c7482006-05-22 21:58:49 -0700910 usb_serial_port_softint(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911} /* cypress_send */
912
913
914/* returns how much space is available in the soft buffer */
915static int cypress_write_room(struct usb_serial_port *port)
916{
917 struct cypress_private *priv = usb_get_serial_port_data(port);
918 int room = 0;
919 unsigned long flags;
920
921 dbg("%s - port %d", __FUNCTION__, port->number);
922
923 spin_lock_irqsave(&priv->lock, flags);
924 room = cypress_buf_space_avail(priv->buf);
925 spin_unlock_irqrestore(&priv->lock, flags);
926
927 dbg("%s - returns %d", __FUNCTION__, room);
928 return room;
929}
930
931
932static int cypress_tiocmget (struct usb_serial_port *port, struct file *file)
933{
934 struct cypress_private *priv = usb_get_serial_port_data(port);
935 __u8 status, control;
936 unsigned int result = 0;
937 unsigned long flags;
938
939 dbg("%s - port %d", __FUNCTION__, port->number);
940
941 spin_lock_irqsave(&priv->lock, flags);
942 control = priv->line_control;
943 status = priv->current_status;
944 spin_unlock_irqrestore(&priv->lock, flags);
945
946 result = ((control & CONTROL_DTR) ? TIOCM_DTR : 0)
947 | ((control & CONTROL_RTS) ? TIOCM_RTS : 0)
948 | ((status & UART_CTS) ? TIOCM_CTS : 0)
949 | ((status & UART_DSR) ? TIOCM_DSR : 0)
950 | ((status & UART_RI) ? TIOCM_RI : 0)
951 | ((status & UART_CD) ? TIOCM_CD : 0);
952
953 dbg("%s - result = %x", __FUNCTION__, result);
954
955 return result;
956}
957
958
959static int cypress_tiocmset (struct usb_serial_port *port, struct file *file,
960 unsigned int set, unsigned int clear)
961{
962 struct cypress_private *priv = usb_get_serial_port_data(port);
963 unsigned long flags;
964
965 dbg("%s - port %d", __FUNCTION__, port->number);
966
967 spin_lock_irqsave(&priv->lock, flags);
968 if (set & TIOCM_RTS)
969 priv->line_control |= CONTROL_RTS;
970 if (set & TIOCM_DTR)
971 priv->line_control |= CONTROL_DTR;
972 if (clear & TIOCM_RTS)
973 priv->line_control &= ~CONTROL_RTS;
974 if (clear & TIOCM_DTR)
975 priv->line_control &= ~CONTROL_DTR;
976 spin_unlock_irqrestore(&priv->lock, flags);
977
978 priv->cmd_ctrl = 1;
979 return cypress_write(port, NULL, 0);
980}
981
982
983static int cypress_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg)
984{
985 struct cypress_private *priv = usb_get_serial_port_data(port);
986
987 dbg("%s - port %d, cmd 0x%.4x", __FUNCTION__, port->number, cmd);
988
989 switch (cmd) {
990 case TIOCGSERIAL:
Alan Cox606d0992006-12-08 02:38:45 -0800991 if (copy_to_user((void __user *)arg, port->tty->termios, sizeof(struct ktermios))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 return -EFAULT;
993 }
994 return (0);
995 break;
996 case TIOCSSERIAL:
Alan Cox606d0992006-12-08 02:38:45 -0800997 if (copy_from_user(port->tty->termios, (void __user *)arg, sizeof(struct ktermios))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 return -EFAULT;
999 }
1000 /* here we need to call cypress_set_termios to invoke the new settings */
1001 cypress_set_termios(port, &priv->tmp_termios);
1002 return (0);
1003 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 /* This code comes from drivers/char/serial.c and ftdi_sio.c */
1005 case TIOCMIWAIT:
1006 while (priv != NULL) {
1007 interruptible_sleep_on(&priv->delta_msr_wait);
1008 /* see if a signal did it */
1009 if (signal_pending(current))
1010 return -ERESTARTSYS;
1011 else {
1012 char diff = priv->diff_status;
1013
1014 if (diff == 0) {
1015 return -EIO; /* no change => error */
1016 }
1017
1018 /* consume all events */
1019 priv->diff_status = 0;
1020
1021 /* return 0 if caller wanted to know about these bits */
1022 if ( ((arg & TIOCM_RNG) && (diff & UART_RI)) ||
1023 ((arg & TIOCM_DSR) && (diff & UART_DSR)) ||
1024 ((arg & TIOCM_CD) && (diff & UART_CD)) ||
1025 ((arg & TIOCM_CTS) && (diff & UART_CTS)) ) {
1026 return 0;
1027 }
1028 /* otherwise caller can't care less about what happened,
1029 * and so we continue to wait for more events.
1030 */
1031 }
1032 }
1033 return 0;
1034 break;
1035 default:
1036 break;
1037 }
1038
1039 dbg("%s - arg not supported - it was 0x%04x - check include/asm/ioctls.h", __FUNCTION__, cmd);
1040
1041 return -ENOIOCTLCMD;
1042} /* cypress_ioctl */
1043
1044
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001045static void cypress_set_termios (struct usb_serial_port *port,
Alan Cox606d0992006-12-08 02:38:45 -08001046 struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047{
1048 struct cypress_private *priv = usb_get_serial_port_data(port);
1049 struct tty_struct *tty;
1050 int data_bits, stop_bits, parity_type, parity_enable;
1051 unsigned cflag, iflag, baud_mask;
1052 unsigned long flags;
1053 __u8 oldlines;
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001054 int linechange = 0;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001055
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 dbg("%s - port %d", __FUNCTION__, port->number);
1057
1058 tty = port->tty;
1059 if ((!tty) || (!tty->termios)) {
1060 dbg("%s - no tty structures", __FUNCTION__);
1061 return;
1062 }
1063
1064 spin_lock_irqsave(&priv->lock, flags);
1065 if (!priv->termios_initialized) {
1066 if (priv->chiptype == CT_EARTHMATE) {
1067 *(tty->termios) = tty_std_termios;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001068 tty->termios->c_cflag = B4800 | CS8 | CREAD | HUPCL |
1069 CLOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 } else if (priv->chiptype == CT_CYPHIDCOM) {
1071 *(tty->termios) = tty_std_termios;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001072 tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
1073 CLOCAL;
Lonnie Mendeza5c44e22006-03-01 10:45:24 -06001074 } else if (priv->chiptype == CT_CA42V2) {
1075 *(tty->termios) = tty_std_termios;
1076 tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
1077 CLOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 }
1079 priv->termios_initialized = 1;
1080 }
1081 spin_unlock_irqrestore(&priv->lock, flags);
1082
1083 cflag = tty->termios->c_cflag;
1084 iflag = tty->termios->c_iflag;
1085
1086 /* check if there are new settings */
1087 if (old_termios) {
1088 if ((cflag != old_termios->c_cflag) ||
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001089 (RELEVANT_IFLAG(iflag) !=
1090 RELEVANT_IFLAG(old_termios->c_iflag))) {
1091 dbg("%s - attempting to set new termios settings",
1092 __FUNCTION__);
1093 /* should make a copy of this in case something goes
1094 * wrong in the function, we can restore it */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 spin_lock_irqsave(&priv->lock, flags);
1096 priv->tmp_termios = *(tty->termios);
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001097 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 } else {
1099 dbg("%s - nothing to do, exiting", __FUNCTION__);
1100 return;
1101 }
1102 } else
1103 return;
1104
1105 /* set number of data bits, parity, stop bits */
1106 /* when parity is disabled the parity type bit is ignored */
1107
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001108 /* 1 means 2 stop bits, 0 means 1 stop bit */
1109 stop_bits = cflag & CSTOPB ? 1 : 0;
1110
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 if (cflag & PARENB) {
1112 parity_enable = 1;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001113 /* 1 means odd parity, 0 means even parity */
1114 parity_type = cflag & PARODD ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 } else
1116 parity_enable = parity_type = 0;
1117
1118 if (cflag & CSIZE) {
1119 switch (cflag & CSIZE) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001120 case CS5:
1121 data_bits = 0;
1122 break;
1123 case CS6:
1124 data_bits = 1;
1125 break;
1126 case CS7:
1127 data_bits = 2;
1128 break;
1129 case CS8:
1130 data_bits = 3;
1131 break;
1132 default:
1133 err("%s - CSIZE was set, but not CS5-CS8",
1134 __FUNCTION__);
1135 data_bits = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 }
1137 } else
1138 data_bits = 3;
1139
1140 spin_lock_irqsave(&priv->lock, flags);
1141 oldlines = priv->line_control;
1142 if ((cflag & CBAUD) == B0) {
1143 /* drop dtr and rts */
1144 dbg("%s - dropping the lines, baud rate 0bps", __FUNCTION__);
1145 baud_mask = B0;
1146 priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
1147 } else {
1148 baud_mask = (cflag & CBAUD);
1149 switch(baud_mask) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001150 case B300:
1151 dbg("%s - setting baud 300bps", __FUNCTION__);
1152 break;
1153 case B600:
1154 dbg("%s - setting baud 600bps", __FUNCTION__);
1155 break;
1156 case B1200:
1157 dbg("%s - setting baud 1200bps", __FUNCTION__);
1158 break;
1159 case B2400:
1160 dbg("%s - setting baud 2400bps", __FUNCTION__);
1161 break;
1162 case B4800:
1163 dbg("%s - setting baud 4800bps", __FUNCTION__);
1164 break;
1165 case B9600:
1166 dbg("%s - setting baud 9600bps", __FUNCTION__);
1167 break;
1168 case B19200:
1169 dbg("%s - setting baud 19200bps", __FUNCTION__);
1170 break;
1171 case B38400:
1172 dbg("%s - setting baud 38400bps", __FUNCTION__);
1173 break;
1174 case B57600:
1175 dbg("%s - setting baud 57600bps", __FUNCTION__);
1176 break;
1177 case B115200:
1178 dbg("%s - setting baud 115200bps", __FUNCTION__);
1179 break;
1180 default:
1181 dbg("%s - unknown masked baud rate", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 }
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001183 priv->line_control = (CONTROL_DTR | CONTROL_RTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 }
1185 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001187 dbg("%s - sending %d stop_bits, %d parity_enable, %d parity_type, "
1188 "%d data_bits (+5)", __FUNCTION__, stop_bits,
1189 parity_enable, parity_type, data_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001191 cypress_serial_control(port, baud_mask, data_bits, stop_bits,
1192 parity_enable, parity_type, 0, CYPRESS_SET_CONFIG);
1193
1194 /* we perform a CYPRESS_GET_CONFIG so that the current settings are
1195 * filled into the private structure this should confirm that all is
1196 * working if it returns what we just set */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 cypress_serial_control(port, 0, 0, 0, 0, 0, 0, CYPRESS_GET_CONFIG);
1198
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001199 /* Here we can define custom tty settings for devices; the main tty
1200 * termios flag base comes from empeg.c */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001202 spin_lock_irqsave(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 if ( (priv->chiptype == CT_EARTHMATE) && (priv->baud_rate == 4800) ) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001204 dbg("Using custom termios settings for a baud rate of "
1205 "4800bps.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 /* define custom termios settings for NMEA protocol */
1207
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 tty->termios->c_iflag /* input modes - */
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001209 &= ~(IGNBRK /* disable ignore break */
1210 | BRKINT /* disable break causes interrupt */
1211 | PARMRK /* disable mark parity errors */
1212 | ISTRIP /* disable clear high bit of input char */
1213 | INLCR /* disable translate NL to CR */
1214 | IGNCR /* disable ignore CR */
1215 | ICRNL /* disable translate CR to NL */
1216 | IXON); /* disable enable XON/XOFF flow control */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001218 tty->termios->c_oflag /* output modes */
1219 &= ~OPOST; /* disable postprocess output char */
1220
1221 tty->termios->c_lflag /* line discipline modes */
1222 &= ~(ECHO /* disable echo input characters */
1223 | ECHONL /* disable echo new line */
1224 | ICANON /* disable erase, kill, werase, and rprnt
1225 special characters */
1226 | ISIG /* disable interrupt, quit, and suspend
1227 special characters */
1228 | IEXTEN); /* disable non-POSIX special characters */
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001229 } /* CT_CYPHIDCOM: Application should handle this for device */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 linechange = (priv->line_control != oldlines);
1232 spin_unlock_irqrestore(&priv->lock, flags);
1233
1234 /* if necessary, set lines */
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001235 if (linechange) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 priv->cmd_ctrl = 1;
1237 cypress_write(port, NULL, 0);
1238 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239} /* cypress_set_termios */
1240
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001241
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242/* returns amount of data still left in soft buffer */
1243static int cypress_chars_in_buffer(struct usb_serial_port *port)
1244{
1245 struct cypress_private *priv = usb_get_serial_port_data(port);
1246 int chars = 0;
1247 unsigned long flags;
1248
1249 dbg("%s - port %d", __FUNCTION__, port->number);
1250
1251 spin_lock_irqsave(&priv->lock, flags);
1252 chars = cypress_buf_data_avail(priv->buf);
1253 spin_unlock_irqrestore(&priv->lock, flags);
1254
1255 dbg("%s - returns %d", __FUNCTION__, chars);
1256 return chars;
1257}
1258
1259
1260static void cypress_throttle (struct usb_serial_port *port)
1261{
1262 struct cypress_private *priv = usb_get_serial_port_data(port);
1263 unsigned long flags;
1264
1265 dbg("%s - port %d", __FUNCTION__, port->number);
1266
1267 spin_lock_irqsave(&priv->lock, flags);
1268 priv->rx_flags = THROTTLED;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001269 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270}
1271
1272
1273static void cypress_unthrottle (struct usb_serial_port *port)
1274{
1275 struct cypress_private *priv = usb_get_serial_port_data(port);
1276 int actually_throttled, result;
1277 unsigned long flags;
1278
1279 dbg("%s - port %d", __FUNCTION__, port->number);
1280
1281 spin_lock_irqsave(&priv->lock, flags);
1282 actually_throttled = priv->rx_flags & ACTUALLY_THROTTLED;
1283 priv->rx_flags = 0;
1284 spin_unlock_irqrestore(&priv->lock, flags);
1285
Mike Isely78aef512006-08-29 22:07:11 -05001286 if (!priv->comm_is_ok)
1287 return;
1288
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 if (actually_throttled) {
1290 port->interrupt_in_urb->dev = port->serial->dev;
1291
1292 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
Mike Isely78aef512006-08-29 22:07:11 -05001293 if (result) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001294 dev_err(&port->dev, "%s - failed submitting read urb, "
1295 "error %d\n", __FUNCTION__, result);
Mike Isely78aef512006-08-29 22:07:11 -05001296 cypress_set_dead(port);
1297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 }
1299}
1300
1301
David Howells7d12e782006-10-05 14:55:46 +01001302static void cypress_read_int_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303{
1304 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
1305 struct cypress_private *priv = usb_get_serial_port_data(port);
1306 struct tty_struct *tty;
1307 unsigned char *data = urb->transfer_buffer;
1308 unsigned long flags;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001309 char tty_flag = TTY_NORMAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 int havedata = 0;
1311 int bytes = 0;
1312 int result;
1313 int i = 0;
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001314 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
1316 dbg("%s - port %d", __FUNCTION__, port->number);
1317
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001318 switch (status) {
Mike Isely78aef512006-08-29 22:07:11 -05001319 case 0: /* success */
1320 break;
1321 case -ECONNRESET:
1322 case -ENOENT:
1323 case -ESHUTDOWN:
1324 /* precursor to disconnect so just go away */
1325 return;
1326 case -EPIPE:
1327 usb_clear_halt(port->serial->dev,0x81);
1328 break;
1329 default:
1330 /* something ugly is going on... */
1331 dev_err(&urb->dev->dev,"%s - unexpected nonzero read status received: %d\n",
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001332 __FUNCTION__, status);
Mike Isely78aef512006-08-29 22:07:11 -05001333 cypress_set_dead(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 return;
1335 }
1336
1337 spin_lock_irqsave(&priv->lock, flags);
1338 if (priv->rx_flags & THROTTLED) {
1339 dbg("%s - now throttling", __FUNCTION__);
1340 priv->rx_flags |= ACTUALLY_THROTTLED;
1341 spin_unlock_irqrestore(&priv->lock, flags);
1342 return;
1343 }
1344 spin_unlock_irqrestore(&priv->lock, flags);
1345
1346 tty = port->tty;
1347 if (!tty) {
1348 dbg("%s - bad tty pointer - exiting", __FUNCTION__);
1349 return;
1350 }
1351
1352 spin_lock_irqsave(&priv->lock, flags);
Mike Isely3416eaa2008-02-10 20:23:19 -06001353 result = urb->actual_length;
1354 switch (priv->pkt_fmt) {
1355 default:
1356 case packet_format_1:
1357 /* This is for the CY7C64013... */
1358 priv->current_status = data[0] & 0xF8;
1359 bytes = data[1] + 2;
1360 i = 2;
1361 if (bytes > 2)
1362 havedata = 1;
1363 break;
1364 case packet_format_2:
1365 /* This is for the CY7C63743... */
1366 priv->current_status = data[0] & 0xF8;
1367 bytes = (data[0] & 0x07) + 1;
1368 i = 1;
1369 if (bytes > 1)
1370 havedata = 1;
1371 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 }
1373 spin_unlock_irqrestore(&priv->lock, flags);
Mike Isely3416eaa2008-02-10 20:23:19 -06001374 if (result < bytes) {
1375 dbg("%s - wrong packet size - received %d bytes but packet "
1376 "said %d bytes", __func__, result, bytes);
1377 goto continue_read;
1378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001380 usb_serial_debug_data (debug, &port->dev, __FUNCTION__,
1381 urb->actual_length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
1383 spin_lock_irqsave(&priv->lock, flags);
1384 /* check to see if status has changed */
1385 if (priv != NULL) {
1386 if (priv->current_status != priv->prev_status) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001387 priv->diff_status |= priv->current_status ^
1388 priv->prev_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 wake_up_interruptible(&priv->delta_msr_wait);
1390 priv->prev_status = priv->current_status;
1391 }
1392 }
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001393 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001395 /* hangup, as defined in acm.c... this might be a bad place for it
1396 * though */
1397 if (tty && !(tty->termios->c_cflag & CLOCAL) &&
1398 !(priv->current_status & UART_CD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 dbg("%s - calling hangup", __FUNCTION__);
1400 tty_hangup(tty);
1401 goto continue_read;
1402 }
1403
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001404 /* There is one error bit... I'm assuming it is a parity error
1405 * indicator as the generic firmware will set this bit to 1 if a
1406 * parity error occurs.
1407 * I can not find reference to any other error events. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 spin_lock_irqsave(&priv->lock, flags);
1409 if (priv->current_status & CYP_ERROR) {
1410 spin_unlock_irqrestore(&priv->lock, flags);
1411 tty_flag = TTY_PARITY;
1412 dbg("%s - Parity Error detected", __FUNCTION__);
1413 } else
1414 spin_unlock_irqrestore(&priv->lock, flags);
1415
1416 /* process read if there is data other than line status */
1417 if (tty && (bytes > i)) {
Alan Cox33f0f882006-01-09 20:54:13 -08001418 bytes = tty_buffer_request_room(tty, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 for (; i < bytes ; ++i) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001420 dbg("pushing byte number %d - %d - %c", i, data[i],
1421 data[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 tty_insert_flip_char(tty, data[i], tty_flag);
1423 }
1424 tty_flip_buffer_push(port->tty);
1425 }
1426
1427 spin_lock_irqsave(&priv->lock, flags);
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001428 /* control and status byte(s) are also counted */
1429 priv->bytes_in += bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 spin_unlock_irqrestore(&priv->lock, flags);
1431
1432continue_read:
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001433
1434 /* Continue trying to always read... unless the port has closed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
Mike Isely78aef512006-08-29 22:07:11 -05001436 if (port->open_count > 0 && priv->comm_is_ok) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001437 usb_fill_int_urb(port->interrupt_in_urb, port->serial->dev,
1438 usb_rcvintpipe(port->serial->dev,
1439 port->interrupt_in_endpointAddress),
1440 port->interrupt_in_urb->transfer_buffer,
1441 port->interrupt_in_urb->transfer_buffer_length,
Mike Isely0257fa92006-08-29 22:06:59 -05001442 cypress_read_int_callback, port, priv->read_urb_interval);
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001443 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
Mike Isely78aef512006-08-29 22:07:11 -05001444 if (result) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001445 dev_err(&urb->dev->dev, "%s - failed resubmitting "
1446 "read urb, error %d\n", __FUNCTION__,
1447 result);
Mike Isely78aef512006-08-29 22:07:11 -05001448 cypress_set_dead(port);
1449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 }
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001451
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 return;
1453} /* cypress_read_int_callback */
1454
1455
David Howells7d12e782006-10-05 14:55:46 +01001456static void cypress_write_int_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457{
1458 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
1459 struct cypress_private *priv = usb_get_serial_port_data(port);
1460 int result;
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001461 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
1463 dbg("%s - port %d", __FUNCTION__, port->number);
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001464
1465 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 case 0:
1467 /* success */
1468 break;
1469 case -ECONNRESET:
1470 case -ENOENT:
1471 case -ESHUTDOWN:
1472 /* this urb is terminated, clean up */
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001473 dbg("%s - urb shutting down with status: %d",
1474 __FUNCTION__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 priv->write_urb_in_use = 0;
1476 return;
Mike Isely78aef512006-08-29 22:07:11 -05001477 case -EPIPE: /* no break needed; clear halt and resubmit */
1478 if (!priv->comm_is_ok)
1479 break;
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001480 usb_clear_halt(port->serial->dev, 0x02);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 /* error in the urb, so we have to resubmit it */
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001482 dbg("%s - nonzero write bulk status received: %d",
1483 __FUNCTION__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 port->interrupt_out_urb->transfer_buffer_length = 1;
1485 port->interrupt_out_urb->dev = port->serial->dev;
1486 result = usb_submit_urb(port->interrupt_out_urb, GFP_ATOMIC);
Mike Isely78aef512006-08-29 22:07:11 -05001487 if (!result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 return;
Mike Isely78aef512006-08-29 22:07:11 -05001489 dev_err(&urb->dev->dev, "%s - failed resubmitting write urb, error %d\n",
1490 __FUNCTION__, result);
1491 cypress_set_dead(port);
1492 break;
1493 default:
1494 dev_err(&urb->dev->dev,"%s - unexpected nonzero write status received: %d\n",
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001495 __FUNCTION__, status);
Mike Isely78aef512006-08-29 22:07:11 -05001496 cypress_set_dead(port);
1497 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 }
1499
1500 priv->write_urb_in_use = 0;
1501
1502 /* send any buffered data */
1503 cypress_send(port);
1504}
1505
1506
1507/*****************************************************************************
1508 * Write buffer functions - buffering code from pl2303 used
1509 *****************************************************************************/
1510
1511/*
1512 * cypress_buf_alloc
1513 *
1514 * Allocate a circular buffer and all associated memory.
1515 */
1516
1517static struct cypress_buf *cypress_buf_alloc(unsigned int size)
1518{
1519
1520 struct cypress_buf *cb;
1521
1522
1523 if (size == 0)
1524 return NULL;
1525
Robert P. J. Day5cbded52006-12-13 00:35:56 -08001526 cb = kmalloc(sizeof(struct cypress_buf), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 if (cb == NULL)
1528 return NULL;
1529
1530 cb->buf_buf = kmalloc(size, GFP_KERNEL);
1531 if (cb->buf_buf == NULL) {
1532 kfree(cb);
1533 return NULL;
1534 }
1535
1536 cb->buf_size = size;
1537 cb->buf_get = cb->buf_put = cb->buf_buf;
1538
1539 return cb;
1540
1541}
1542
1543
1544/*
1545 * cypress_buf_free
1546 *
1547 * Free the buffer and all associated memory.
1548 */
1549
1550static void cypress_buf_free(struct cypress_buf *cb)
1551{
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001552 if (cb) {
1553 kfree(cb->buf_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 kfree(cb);
1555 }
1556}
1557
1558
1559/*
1560 * cypress_buf_clear
1561 *
1562 * Clear out all data in the circular buffer.
1563 */
1564
1565static void cypress_buf_clear(struct cypress_buf *cb)
1566{
1567 if (cb != NULL)
1568 cb->buf_get = cb->buf_put;
1569 /* equivalent to a get of all data available */
1570}
1571
1572
1573/*
1574 * cypress_buf_data_avail
1575 *
1576 * Return the number of bytes of data available in the circular
1577 * buffer.
1578 */
1579
1580static unsigned int cypress_buf_data_avail(struct cypress_buf *cb)
1581{
1582 if (cb != NULL)
1583 return ((cb->buf_size + cb->buf_put - cb->buf_get) % cb->buf_size);
1584 else
1585 return 0;
1586}
1587
1588
1589/*
1590 * cypress_buf_space_avail
1591 *
1592 * Return the number of bytes of space available in the circular
1593 * buffer.
1594 */
1595
1596static unsigned int cypress_buf_space_avail(struct cypress_buf *cb)
1597{
1598 if (cb != NULL)
1599 return ((cb->buf_size + cb->buf_get - cb->buf_put - 1) % cb->buf_size);
1600 else
1601 return 0;
1602}
1603
1604
1605/*
1606 * cypress_buf_put
1607 *
1608 * Copy data data from a user buffer and put it into the circular buffer.
1609 * Restrict to the amount of space available.
1610 *
1611 * Return the number of bytes copied.
1612 */
1613
1614static unsigned int cypress_buf_put(struct cypress_buf *cb, const char *buf,
1615 unsigned int count)
1616{
1617
1618 unsigned int len;
1619
1620
1621 if (cb == NULL)
1622 return 0;
1623
1624 len = cypress_buf_space_avail(cb);
1625 if (count > len)
1626 count = len;
1627
1628 if (count == 0)
1629 return 0;
1630
1631 len = cb->buf_buf + cb->buf_size - cb->buf_put;
1632 if (count > len) {
1633 memcpy(cb->buf_put, buf, len);
1634 memcpy(cb->buf_buf, buf+len, count - len);
1635 cb->buf_put = cb->buf_buf + count - len;
1636 } else {
1637 memcpy(cb->buf_put, buf, count);
1638 if (count < len)
1639 cb->buf_put += count;
1640 else /* count == len */
1641 cb->buf_put = cb->buf_buf;
1642 }
1643
1644 return count;
1645
1646}
1647
1648
1649/*
1650 * cypress_buf_get
1651 *
1652 * Get data from the circular buffer and copy to the given buffer.
1653 * Restrict to the amount of data available.
1654 *
1655 * Return the number of bytes copied.
1656 */
1657
1658static unsigned int cypress_buf_get(struct cypress_buf *cb, char *buf,
1659 unsigned int count)
1660{
1661
1662 unsigned int len;
1663
1664
1665 if (cb == NULL)
1666 return 0;
1667
1668 len = cypress_buf_data_avail(cb);
1669 if (count > len)
1670 count = len;
1671
1672 if (count == 0)
1673 return 0;
1674
1675 len = cb->buf_buf + cb->buf_size - cb->buf_get;
1676 if (count > len) {
1677 memcpy(buf, cb->buf_get, len);
1678 memcpy(buf+len, cb->buf_buf, count - len);
1679 cb->buf_get = cb->buf_buf + count - len;
1680 } else {
1681 memcpy(buf, cb->buf_get, count);
1682 if (count < len)
1683 cb->buf_get += count;
1684 else /* count == len */
1685 cb->buf_get = cb->buf_buf;
1686 }
1687
1688 return count;
1689
1690}
1691
1692/*****************************************************************************
1693 * Module functions
1694 *****************************************************************************/
1695
1696static int __init cypress_init(void)
1697{
1698 int retval;
1699
1700 dbg("%s", __FUNCTION__);
1701
1702 retval = usb_serial_register(&cypress_earthmate_device);
1703 if (retval)
1704 goto failed_em_register;
1705 retval = usb_serial_register(&cypress_hidcom_device);
1706 if (retval)
1707 goto failed_hidcom_register;
Lonnie Mendeza5c44e22006-03-01 10:45:24 -06001708 retval = usb_serial_register(&cypress_ca42v2_device);
1709 if (retval)
1710 goto failed_ca42v2_register;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 retval = usb_register(&cypress_driver);
1712 if (retval)
1713 goto failed_usb_register;
1714
1715 info(DRIVER_DESC " " DRIVER_VERSION);
1716 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
Mariusz Kozlowski2e46b742006-11-17 17:49:22 +01001718failed_usb_register:
1719 usb_serial_deregister(&cypress_ca42v2_device);
1720failed_ca42v2_register:
1721 usb_serial_deregister(&cypress_hidcom_device);
1722failed_hidcom_register:
1723 usb_serial_deregister(&cypress_earthmate_device);
1724failed_em_register:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 return retval;
1726}
1727
1728
1729static void __exit cypress_exit (void)
1730{
1731 dbg("%s", __FUNCTION__);
1732
1733 usb_deregister (&cypress_driver);
1734 usb_serial_deregister (&cypress_earthmate_device);
1735 usb_serial_deregister (&cypress_hidcom_device);
Lonnie Mendeza5c44e22006-03-01 10:45:24 -06001736 usb_serial_deregister (&cypress_ca42v2_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737}
1738
1739
1740module_init(cypress_init);
1741module_exit(cypress_exit);
1742
1743MODULE_AUTHOR( DRIVER_AUTHOR );
1744MODULE_DESCRIPTION( DRIVER_DESC );
1745MODULE_VERSION( DRIVER_VERSION );
1746MODULE_LICENSE("GPL");
1747
1748module_param(debug, bool, S_IRUGO | S_IWUSR);
1749MODULE_PARM_DESC(debug, "Debug enabled or not");
1750module_param(stats, bool, S_IRUGO | S_IWUSR);
1751MODULE_PARM_DESC(stats, "Enable statistics or not");
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001752module_param(interval, int, S_IRUGO | S_IWUSR);
1753MODULE_PARM_DESC(interval, "Overrides interrupt interval");