blob: 4bf45c711b9d4b09510c5945610bc97eeab81d8f [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 */
Mike Isely3d6aa322008-02-10 20:23:24 -0600148 int get_cfg_unsafe; /* If true, the CYPRESS_GET_CONFIG is unsafe */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 int baud_rate; /* stores current baud rate in integer form */
150 int cbr_mask; /* stores current baud rate in masked form */
151 int isthrottled; /* if throttled, discard reads */
152 wait_queue_head_t delta_msr_wait; /* used for TIOCMIWAIT */
153 char prev_status, diff_status; /* used for TIOCMIWAIT */
154 /* we pass a pointer to this as the arguement sent to cypress_set_termios old_termios */
Alan Cox606d0992006-12-08 02:38:45 -0800155 struct ktermios tmp_termios; /* stores the old termios settings */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156};
157
158/* write buffer structure */
159struct cypress_buf {
160 unsigned int buf_size;
161 char *buf_buf;
162 char *buf_get;
163 char *buf_put;
164};
165
166/* function prototypes for the Cypress USB to serial device */
167static int cypress_earthmate_startup (struct usb_serial *serial);
168static int cypress_hidcom_startup (struct usb_serial *serial);
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600169static int cypress_ca42v2_startup (struct usb_serial *serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170static void cypress_shutdown (struct usb_serial *serial);
171static int cypress_open (struct usb_serial_port *port, struct file *filp);
172static void cypress_close (struct usb_serial_port *port, struct file *filp);
173static int cypress_write (struct usb_serial_port *port, const unsigned char *buf, int count);
174static void cypress_send (struct usb_serial_port *port);
175static int cypress_write_room (struct usb_serial_port *port);
176static int cypress_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg);
Alan Cox606d0992006-12-08 02:38:45 -0800177static void cypress_set_termios (struct usb_serial_port *port, struct ktermios * old);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178static int cypress_tiocmget (struct usb_serial_port *port, struct file *file);
179static int cypress_tiocmset (struct usb_serial_port *port, struct file *file, unsigned int set, unsigned int clear);
180static int cypress_chars_in_buffer (struct usb_serial_port *port);
181static void cypress_throttle (struct usb_serial_port *port);
182static void cypress_unthrottle (struct usb_serial_port *port);
Mike Isely78aef512006-08-29 22:07:11 -0500183static void cypress_set_dead (struct usb_serial_port *port);
David Howells7d12e782006-10-05 14:55:46 +0100184static void cypress_read_int_callback (struct urb *urb);
185static void cypress_write_int_callback (struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186/* baud helper functions */
187static int mask_to_rate (unsigned mask);
188static unsigned rate_to_mask (int rate);
189/* write buffer functions */
190static struct cypress_buf *cypress_buf_alloc(unsigned int size);
191static void cypress_buf_free(struct cypress_buf *cb);
192static void cypress_buf_clear(struct cypress_buf *cb);
193static unsigned int cypress_buf_data_avail(struct cypress_buf *cb);
194static unsigned int cypress_buf_space_avail(struct cypress_buf *cb);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500195static unsigned int cypress_buf_put(struct cypress_buf *cb, const char *buf, unsigned int count);
196static unsigned int cypress_buf_get(struct cypress_buf *cb, char *buf, unsigned int count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700199static struct usb_serial_driver cypress_earthmate_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700200 .driver = {
201 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700202 .name = "earthmate",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700203 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700204 .description = "DeLorme Earthmate USB",
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100205 .usb_driver = &cypress_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 .id_table = id_table_earthmate,
207 .num_interrupt_in = 1,
208 .num_interrupt_out = 1,
209 .num_bulk_in = NUM_DONT_CARE,
210 .num_bulk_out = NUM_DONT_CARE,
211 .num_ports = 1,
212 .attach = cypress_earthmate_startup,
213 .shutdown = cypress_shutdown,
214 .open = cypress_open,
215 .close = cypress_close,
216 .write = cypress_write,
217 .write_room = cypress_write_room,
218 .ioctl = cypress_ioctl,
219 .set_termios = cypress_set_termios,
220 .tiocmget = cypress_tiocmget,
221 .tiocmset = cypress_tiocmset,
222 .chars_in_buffer = cypress_chars_in_buffer,
223 .throttle = cypress_throttle,
224 .unthrottle = cypress_unthrottle,
225 .read_int_callback = cypress_read_int_callback,
226 .write_int_callback = cypress_write_int_callback,
227};
228
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700229static struct usb_serial_driver cypress_hidcom_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700230 .driver = {
231 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700232 .name = "cyphidcom",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700233 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700234 .description = "HID->COM RS232 Adapter",
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100235 .usb_driver = &cypress_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 .id_table = id_table_cyphidcomrs232,
237 .num_interrupt_in = 1,
238 .num_interrupt_out = 1,
239 .num_bulk_in = NUM_DONT_CARE,
240 .num_bulk_out = NUM_DONT_CARE,
241 .num_ports = 1,
242 .attach = cypress_hidcom_startup,
243 .shutdown = cypress_shutdown,
244 .open = cypress_open,
245 .close = cypress_close,
246 .write = cypress_write,
247 .write_room = cypress_write_room,
248 .ioctl = cypress_ioctl,
249 .set_termios = cypress_set_termios,
250 .tiocmget = cypress_tiocmget,
251 .tiocmset = cypress_tiocmset,
252 .chars_in_buffer = cypress_chars_in_buffer,
253 .throttle = cypress_throttle,
254 .unthrottle = cypress_unthrottle,
255 .read_int_callback = cypress_read_int_callback,
256 .write_int_callback = cypress_write_int_callback,
257};
258
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600259static struct usb_serial_driver cypress_ca42v2_device = {
260 .driver = {
261 .owner = THIS_MODULE,
262 .name = "nokiaca42v2",
263 },
264 .description = "Nokia CA-42 V2 Adapter",
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100265 .usb_driver = &cypress_driver,
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600266 .id_table = id_table_nokiaca42v2,
267 .num_interrupt_in = 1,
268 .num_interrupt_out = 1,
269 .num_bulk_in = NUM_DONT_CARE,
270 .num_bulk_out = NUM_DONT_CARE,
271 .num_ports = 1,
272 .attach = cypress_ca42v2_startup,
273 .shutdown = cypress_shutdown,
274 .open = cypress_open,
275 .close = cypress_close,
276 .write = cypress_write,
277 .write_room = cypress_write_room,
278 .ioctl = cypress_ioctl,
279 .set_termios = cypress_set_termios,
280 .tiocmget = cypress_tiocmget,
281 .tiocmset = cypress_tiocmset,
282 .chars_in_buffer = cypress_chars_in_buffer,
283 .throttle = cypress_throttle,
284 .unthrottle = cypress_unthrottle,
285 .read_int_callback = cypress_read_int_callback,
286 .write_int_callback = cypress_write_int_callback,
287};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
289/*****************************************************************************
290 * Cypress serial helper functions
291 *****************************************************************************/
292
293
Mike Isely92983c22008-02-10 20:23:32 -0600294static int analyze_baud_rate(struct usb_serial_port *port, unsigned baud_mask)
295{
296 int new_rate;
297 struct cypress_private *priv;
298 priv = usb_get_serial_port_data(port);
299
300 /*
301 * The general purpose firmware for the Cypress M8 allows for
302 * a maximum speed of 57600bps (I have no idea whether DeLorme
303 * chose to use the general purpose firmware or not), if you
304 * need to modify this speed setting for your own project
305 * please add your own chiptype and modify the code likewise.
306 * The Cypress HID->COM device will work successfully up to
307 * 115200bps (but the actual throughput is around 3kBps).
308 */
309 new_rate = mask_to_rate(baud_mask);
310 if (new_rate < 0) {
311 dbg("%s - failed setting baud rate, untranslatable speed",
312 __func__);
313 return -1;
314 }
315 if (port->serial->dev->speed == USB_SPEED_LOW) {
316 /*
317 * Mike Isely <isely@pobox.com> 2-Feb-2008: The
318 * Cypress app note that describes this mechanism
319 * states the the low-speed part can't handle more
320 * than 800 bytes/sec, in which case 4800 baud is the
321 * safest speed for a part like that.
322 */
323 if (new_rate > 4800) {
324 dbg("%s - failed setting baud rate, device incapable "
325 "speed %d", __func__, new_rate);
326 return -1;
327 }
328 }
329 switch (priv->chiptype) {
330 case CT_EARTHMATE:
331 if (new_rate <= 600) {
332 /* 300 and 600 baud rates are supported under
333 * the generic firmware, but are not used with
334 * NMEA and SiRF protocols */
335 dbg("%s - failed setting baud rate, unsupported speed "
336 "of %d on Earthmate GPS", __func__, new_rate);
337 return -1;
338 }
339 break;
340 default:
341 break;
342 }
343 return new_rate;
344}
345
346
Steven Cole093cf722005-05-03 19:07:24 -0600347/* This function can either set or retrieve the current serial line settings */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348static int cypress_serial_control (struct usb_serial_port *port, unsigned baud_mask, int data_bits, int stop_bits,
349 int parity_enable, int parity_type, int reset, int cypress_request_type)
350{
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500351 int new_baudrate = 0, retval = 0, tries = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 struct cypress_private *priv;
Mike Isely93075542008-02-10 20:23:14 -0600353 __u8 feature_buffer[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 unsigned long flags;
355
356 dbg("%s", __FUNCTION__);
357
358 priv = usb_get_serial_port_data(port);
359
Mike Isely78aef512006-08-29 22:07:11 -0500360 if (!priv->comm_is_ok)
361 return -ENODEV;
362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 switch(cypress_request_type) {
364 case CYPRESS_SET_CONFIG:
Mike Isely92983c22008-02-10 20:23:32 -0600365 new_baudrate = priv->baud_rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 if (baud_mask != priv->cbr_mask) {
367 dbg("%s - baud rate is changing", __FUNCTION__);
Mike Isely92983c22008-02-10 20:23:32 -0600368 retval = analyze_baud_rate(port, baud_mask);
369 if (retval >= 0) {
370 new_baudrate = retval;
371 dbg("%s - New baud rate set to %d",
372 __func__, new_baudrate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 }
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500375 dbg("%s - baud rate is being sent as %d", __FUNCTION__, new_baudrate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Mike Isely93075542008-02-10 20:23:14 -0600377 memset(feature_buffer, 0, sizeof(feature_buffer));
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500378 /* fill the feature_buffer with new configuration */
379 *((u_int32_t *)feature_buffer) = new_baudrate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500381 feature_buffer[4] |= data_bits; /* assign data bits in 2 bit space ( max 3 ) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 /* 1 bit gap */
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500383 feature_buffer[4] |= (stop_bits << 3); /* assign stop bits in 1 bit space */
384 feature_buffer[4] |= (parity_enable << 4); /* assign parity flag in 1 bit space */
385 feature_buffer[4] |= (parity_type << 5); /* assign parity type in 1 bit space */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 /* 1 bit gap */
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500387 feature_buffer[4] |= (reset << 7); /* assign reset at end of byte, 1 bit space */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389 dbg("%s - device is being sent this feature report:", __FUNCTION__);
390 dbg("%s - %02X - %02X - %02X - %02X - %02X", __FUNCTION__, feature_buffer[0], feature_buffer[1],
391 feature_buffer[2], feature_buffer[3], feature_buffer[4]);
392
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500393 do {
Mike Isely93075542008-02-10 20:23:14 -0600394 retval = usb_control_msg(port->serial->dev,
395 usb_sndctrlpipe(port->serial->dev, 0),
396 HID_REQ_SET_REPORT,
397 USB_DIR_OUT | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
398 0x0300, 0, feature_buffer,
399 sizeof(feature_buffer), 500);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500401 if (tries++ >= 3)
402 break;
403
Mike Isely93075542008-02-10 20:23:14 -0600404 } while (retval != sizeof(feature_buffer) &&
405 retval != -ENODEV);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500406
Mike Isely93075542008-02-10 20:23:14 -0600407 if (retval != sizeof(feature_buffer)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 err("%s - failed sending serial line settings - %d", __FUNCTION__, retval);
Mike Isely78aef512006-08-29 22:07:11 -0500409 cypress_set_dead(port);
410 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 spin_lock_irqsave(&priv->lock, flags);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500412 priv->baud_rate = new_baudrate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 priv->cbr_mask = baud_mask;
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500414 priv->current_config = feature_buffer[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 spin_unlock_irqrestore(&priv->lock, flags);
416 }
417 break;
418 case CYPRESS_GET_CONFIG:
Mike Isely3d6aa322008-02-10 20:23:24 -0600419 if (priv->get_cfg_unsafe) {
420 /* Not implemented for this device,
421 and if we try to do it we're likely
422 to crash the hardware. */
423 return -ENOTTY;
424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 dbg("%s - retreiving serial line settings", __FUNCTION__);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500426 /* set initial values in feature buffer */
Mike Isely93075542008-02-10 20:23:14 -0600427 memset(feature_buffer, 0, sizeof(feature_buffer));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500429 do {
Mike Isely93075542008-02-10 20:23:14 -0600430 retval = usb_control_msg(port->serial->dev,
431 usb_rcvctrlpipe(port->serial->dev, 0),
432 HID_REQ_GET_REPORT,
433 USB_DIR_IN | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
434 0x0300, 0, feature_buffer,
435 sizeof(feature_buffer), 500);
436
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500437 if (tries++ >= 3)
438 break;
439
Mike Isely93075542008-02-10 20:23:14 -0600440 } while (retval != sizeof(feature_buffer) &&
441 retval != -ENODEV);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500442
Mike Isely93075542008-02-10 20:23:14 -0600443 if (retval != sizeof(feature_buffer)) {
Adrian Bunk943ffb52006-01-10 00:10:13 +0100444 err("%s - failed to retrieve serial line settings - %d", __FUNCTION__, retval);
Mike Isely78aef512006-08-29 22:07:11 -0500445 cypress_set_dead(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 return retval;
447 } else {
448 spin_lock_irqsave(&priv->lock, flags);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 /* store the config in one byte, and later use bit masks to check values */
451 priv->current_config = feature_buffer[4];
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500452 priv->baud_rate = *((u_int32_t *)feature_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500454 if ( (priv->cbr_mask = rate_to_mask(priv->baud_rate)) == 0x40)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 dbg("%s - failed setting the baud mask (not defined)", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 spin_unlock_irqrestore(&priv->lock, flags);
457 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 }
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500459 spin_lock_irqsave(&priv->lock, flags);
460 ++priv->cmd_count;
461 spin_unlock_irqrestore(&priv->lock, flags);
462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 return retval;
464} /* cypress_serial_control */
465
466
Mike Isely78aef512006-08-29 22:07:11 -0500467static void cypress_set_dead(struct usb_serial_port *port)
468{
469 struct cypress_private *priv = usb_get_serial_port_data(port);
470 unsigned long flags;
471
472 spin_lock_irqsave(&priv->lock, flags);
473 if (!priv->comm_is_ok) {
474 spin_unlock_irqrestore(&priv->lock, flags);
475 return;
476 }
477 priv->comm_is_ok = 0;
478 spin_unlock_irqrestore(&priv->lock, flags);
479
480 err("cypress_m8 suspending failing port %d - interval might be too short",
481 port->number);
482}
483
484
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500485/* given a baud mask, it will return integer baud on success */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486static int mask_to_rate (unsigned mask)
487{
488 int rate;
489
490 switch (mask) {
491 case B0: rate = 0; break;
492 case B300: rate = 300; break;
493 case B600: rate = 600; break;
494 case B1200: rate = 1200; break;
495 case B2400: rate = 2400; break;
496 case B4800: rate = 4800; break;
497 case B9600: rate = 9600; break;
498 case B19200: rate = 19200; break;
499 case B38400: rate = 38400; break;
500 case B57600: rate = 57600; break;
501 case B115200: rate = 115200; break;
502 default: rate = -1;
503 }
504
505 return rate;
506}
507
508
509static unsigned rate_to_mask (int rate)
510{
511 unsigned mask;
512
513 switch (rate) {
514 case 0: mask = B0; break;
515 case 300: mask = B300; break;
516 case 600: mask = B600; break;
517 case 1200: mask = B1200; break;
518 case 2400: mask = B2400; break;
519 case 4800: mask = B4800; break;
520 case 9600: mask = B9600; break;
521 case 19200: mask = B19200; break;
522 case 38400: mask = B38400; break;
523 case 57600: mask = B57600; break;
524 case 115200: mask = B115200; break;
525 default: mask = 0x40;
526 }
527
528 return mask;
529}
530/*****************************************************************************
531 * Cypress serial driver functions
532 *****************************************************************************/
533
534
535static int generic_startup (struct usb_serial *serial)
536{
537 struct cypress_private *priv;
Mike Isely0257fa92006-08-29 22:06:59 -0500538 struct usb_serial_port *port = serial->port[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Mike Isely0257fa92006-08-29 22:06:59 -0500540 dbg("%s - port %d", __FUNCTION__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +0100542 priv = kzalloc(sizeof (struct cypress_private), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 if (!priv)
544 return -ENOMEM;
545
Mike Isely78aef512006-08-29 22:07:11 -0500546 priv->comm_is_ok = !0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 spin_lock_init(&priv->lock);
548 priv->buf = cypress_buf_alloc(CYPRESS_BUF_SIZE);
549 if (priv->buf == NULL) {
550 kfree(priv);
551 return -ENOMEM;
552 }
553 init_waitqueue_head(&priv->delta_msr_wait);
554
555 usb_reset_configuration (serial->dev);
556
557 priv->cmd_ctrl = 0;
558 priv->line_control = 0;
559 priv->termios_initialized = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 priv->rx_flags = 0;
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500561 priv->cbr_mask = B300;
Mike Isely3416eaa2008-02-10 20:23:19 -0600562 /* Default packet format setting is determined by packet size.
563 Anything with a size larger then 9 must have a separate
564 count field since the 3 bit count field is otherwise too
565 small. Otherwise we can use the slightly more compact
566 format. This is in accordance with the cypress_m8 serial
567 converter app note. */
568 if (port->interrupt_out_size > 9) {
569 priv->pkt_fmt = packet_format_1;
570 } else {
571 priv->pkt_fmt = packet_format_2;
572 }
Mike Isely0257fa92006-08-29 22:06:59 -0500573 if (interval > 0) {
574 priv->write_urb_interval = interval;
575 priv->read_urb_interval = interval;
576 dbg("%s - port %d read & write intervals forced to %d",
577 __FUNCTION__,port->number,interval);
578 } else {
579 priv->write_urb_interval = port->interrupt_out_urb->interval;
580 priv->read_urb_interval = port->interrupt_in_urb->interval;
581 dbg("%s - port %d intervals: read=%d write=%d",
582 __FUNCTION__,port->number,
583 priv->read_urb_interval,priv->write_urb_interval);
584 }
585 usb_set_serial_port_data(port, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500587 return 0;
588}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
590
591static int cypress_earthmate_startup (struct usb_serial *serial)
592{
593 struct cypress_private *priv;
Mike Isely3d6aa322008-02-10 20:23:24 -0600594 struct usb_serial_port *port = serial->port[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
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__,
Mike Isely3d6aa322008-02-10 20:23:24 -0600600 port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 return 1;
602 }
603
Mike Isely3d6aa322008-02-10 20:23:24 -0600604 priv = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 priv->chiptype = CT_EARTHMATE;
Mike Isely3416eaa2008-02-10 20:23:19 -0600606 /* All Earthmate devices use the separated-count packet
607 format! Idiotic. */
608 priv->pkt_fmt = packet_format_1;
Mike Isely3d6aa322008-02-10 20:23:24 -0600609 if (serial->dev->descriptor.idProduct != PRODUCT_ID_EARTHMATEUSB) {
610 /* The old original USB Earthmate seemed able to
611 handle GET_CONFIG requests; everything they've
612 produced since that time crashes if this command is
613 attempted :-( */
614 dbg("%s - Marking this device as unsafe for GET_CONFIG "
615 "commands", __func__);
616 priv->get_cfg_unsafe = !0;
617 }
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500618
619 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620} /* cypress_earthmate_startup */
621
622
623static int cypress_hidcom_startup (struct usb_serial *serial)
624{
625 struct cypress_private *priv;
626
627 dbg("%s", __FUNCTION__);
628
629 if (generic_startup(serial)) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500630 dbg("%s - Failed setting up port %d", __FUNCTION__,
631 serial->port[0]->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 return 1;
633 }
634
635 priv = usb_get_serial_port_data(serial->port[0]);
636 priv->chiptype = CT_CYPHIDCOM;
637
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500638 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639} /* cypress_hidcom_startup */
640
641
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600642static int cypress_ca42v2_startup (struct usb_serial *serial)
643{
644 struct cypress_private *priv;
645
646 dbg("%s", __FUNCTION__);
647
648 if (generic_startup(serial)) {
649 dbg("%s - Failed setting up port %d", __FUNCTION__,
650 serial->port[0]->number);
651 return 1;
652 }
653
654 priv = usb_get_serial_port_data(serial->port[0]);
655 priv->chiptype = CT_CA42V2;
656
657 return 0;
658} /* cypress_ca42v2_startup */
659
660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661static void cypress_shutdown (struct usb_serial *serial)
662{
663 struct cypress_private *priv;
664
665 dbg ("%s - port %d", __FUNCTION__, serial->port[0]->number);
666
667 /* all open ports are closed at this point */
668
669 priv = usb_get_serial_port_data(serial->port[0]);
670
671 if (priv) {
672 cypress_buf_free(priv->buf);
673 kfree(priv);
674 usb_set_serial_port_data(serial->port[0], NULL);
675 }
676}
677
678
679static int cypress_open (struct usb_serial_port *port, struct file *filp)
680{
681 struct cypress_private *priv = usb_get_serial_port_data(port);
682 struct usb_serial *serial = port->serial;
683 unsigned long flags;
684 int result = 0;
685
686 dbg("%s - port %d", __FUNCTION__, port->number);
687
Mike Isely78aef512006-08-29 22:07:11 -0500688 if (!priv->comm_is_ok)
689 return -EIO;
690
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 /* clear halts before open */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 usb_clear_halt(serial->dev, 0x81);
693 usb_clear_halt(serial->dev, 0x02);
694
695 spin_lock_irqsave(&priv->lock, flags);
696 /* reset read/write statistics */
697 priv->bytes_in = 0;
698 priv->bytes_out = 0;
699 priv->cmd_count = 0;
700 priv->rx_flags = 0;
701 spin_unlock_irqrestore(&priv->lock, flags);
702
703 /* setting to zero could cause data loss */
704 port->tty->low_latency = 1;
705
706 /* raise both lines and set termios */
707 spin_lock_irqsave(&priv->lock, flags);
708 priv->line_control = CONTROL_DTR | CONTROL_RTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 priv->cmd_ctrl = 1;
710 spin_unlock_irqrestore(&priv->lock, flags);
711 result = cypress_write(port, NULL, 0);
712
713 if (result) {
714 dev_err(&port->dev, "%s - failed setting the control lines - error %d\n", __FUNCTION__, result);
715 return result;
716 } else
717 dbg("%s - success setting the control lines", __FUNCTION__);
718
719 cypress_set_termios(port, &priv->tmp_termios);
720
721 /* setup the port and start reading from the device */
722 if(!port->interrupt_in_urb){
723 err("%s - interrupt_in_urb is empty!", __FUNCTION__);
724 return(-1);
725 }
726
727 usb_fill_int_urb(port->interrupt_in_urb, serial->dev,
728 usb_rcvintpipe(serial->dev, port->interrupt_in_endpointAddress),
729 port->interrupt_in_urb->transfer_buffer, port->interrupt_in_urb->transfer_buffer_length,
Mike Isely0257fa92006-08-29 22:06:59 -0500730 cypress_read_int_callback, port, priv->read_urb_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
732
733 if (result){
734 dev_err(&port->dev, "%s - failed submitting read urb, error %d\n", __FUNCTION__, result);
Mike Isely78aef512006-08-29 22:07:11 -0500735 cypress_set_dead(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 }
737
738 return result;
739} /* cypress_open */
740
741
742static void cypress_close(struct usb_serial_port *port, struct file * filp)
743{
744 struct cypress_private *priv = usb_get_serial_port_data(port);
745 unsigned int c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 int bps;
747 long timeout;
748 wait_queue_t wait;
749
750 dbg("%s - port %d", __FUNCTION__, port->number);
751
752 /* wait for data to drain from buffer */
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100753 spin_lock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 timeout = CYPRESS_CLOSING_WAIT;
755 init_waitqueue_entry(&wait, current);
756 add_wait_queue(&port->tty->write_wait, &wait);
757 for (;;) {
758 set_current_state(TASK_INTERRUPTIBLE);
759 if (cypress_buf_data_avail(priv->buf) == 0
760 || timeout == 0 || signal_pending(current)
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100761 /* without mutex, allowed due to harmless failure mode */
762 || port->serial->disconnected)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 break;
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100764 spin_unlock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 timeout = schedule_timeout(timeout);
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100766 spin_lock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 }
768 set_current_state(TASK_RUNNING);
769 remove_wait_queue(&port->tty->write_wait, &wait);
770 /* clear out any remaining data in the buffer */
771 cypress_buf_clear(priv->buf);
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100772 spin_unlock_irq(&priv->lock);
773
774 /* writing is potentially harmful, lock must be taken */
775 mutex_lock(&port->serial->disc_mutex);
776 if (port->serial->disconnected) {
777 mutex_unlock(&port->serial->disc_mutex);
778 return;
779 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 /* wait for characters to drain from device */
781 bps = tty_get_baud_rate(port->tty);
782 if (bps > 1200)
783 timeout = max((HZ*2560)/bps,HZ/10);
784 else
785 timeout = 2*HZ;
Nishanth Aravamudan22c43862005-08-15 11:30:11 -0700786 schedule_timeout_interruptible(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
788 dbg("%s - stopping urbs", __FUNCTION__);
789 usb_kill_urb (port->interrupt_in_urb);
790 usb_kill_urb (port->interrupt_out_urb);
791
792 if (port->tty) {
793 c_cflag = port->tty->termios->c_cflag;
794 if (c_cflag & HUPCL) {
795 /* drop dtr and rts */
796 priv = usb_get_serial_port_data(port);
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100797 spin_lock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 priv->line_control = 0;
799 priv->cmd_ctrl = 1;
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100800 spin_unlock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 cypress_write(port, NULL, 0);
802 }
803 }
804
805 if (stats)
806 dev_info (&port->dev, "Statistics: %d Bytes In | %d Bytes Out | %d Commands Issued\n",
807 priv->bytes_in, priv->bytes_out, priv->cmd_count);
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100808 mutex_unlock(&port->serial->disc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809} /* cypress_close */
810
811
812static int cypress_write(struct usb_serial_port *port, const unsigned char *buf, int count)
813{
814 struct cypress_private *priv = usb_get_serial_port_data(port);
815 unsigned long flags;
816
817 dbg("%s - port %d, %d bytes", __FUNCTION__, port->number, count);
818
819 /* line control commands, which need to be executed immediately,
820 are not put into the buffer for obvious reasons.
821 */
822 if (priv->cmd_ctrl) {
823 count = 0;
824 goto finish;
825 }
826
827 if (!count)
828 return count;
829
830 spin_lock_irqsave(&priv->lock, flags);
831 count = cypress_buf_put(priv->buf, buf, count);
832 spin_unlock_irqrestore(&priv->lock, flags);
833
834finish:
835 cypress_send(port);
836
837 return count;
838} /* cypress_write */
839
840
841static void cypress_send(struct usb_serial_port *port)
842{
843 int count = 0, result, offset, actual_size;
844 struct cypress_private *priv = usb_get_serial_port_data(port);
845 unsigned long flags;
846
Mike Isely78aef512006-08-29 22:07:11 -0500847 if (!priv->comm_is_ok)
848 return;
849
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 dbg("%s - port %d", __FUNCTION__, port->number);
851 dbg("%s - interrupt out size is %d", __FUNCTION__, port->interrupt_out_size);
852
853 spin_lock_irqsave(&priv->lock, flags);
854 if (priv->write_urb_in_use) {
855 dbg("%s - can't write, urb in use", __FUNCTION__);
856 spin_unlock_irqrestore(&priv->lock, flags);
857 return;
858 }
859 spin_unlock_irqrestore(&priv->lock, flags);
860
861 /* clear buffer */
862 memset(port->interrupt_out_urb->transfer_buffer, 0, port->interrupt_out_size);
863
864 spin_lock_irqsave(&priv->lock, flags);
Mike Isely3416eaa2008-02-10 20:23:19 -0600865 switch (priv->pkt_fmt) {
866 default:
867 case packet_format_1:
868 /* this is for the CY7C64013... */
869 offset = 2;
870 port->interrupt_out_buffer[0] = priv->line_control;
871 break;
872 case packet_format_2:
873 /* this is for the CY7C63743... */
874 offset = 1;
875 port->interrupt_out_buffer[0] = priv->line_control;
876 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 }
878
879 if (priv->line_control & CONTROL_RESET)
880 priv->line_control &= ~CONTROL_RESET;
881
882 if (priv->cmd_ctrl) {
883 priv->cmd_count++;
884 dbg("%s - line control command being issued", __FUNCTION__);
885 spin_unlock_irqrestore(&priv->lock, flags);
886 goto send;
887 } else
888 spin_unlock_irqrestore(&priv->lock, flags);
889
890 count = cypress_buf_get(priv->buf, &port->interrupt_out_buffer[offset],
891 port->interrupt_out_size-offset);
892
893 if (count == 0) {
894 return;
895 }
896
Mike Isely3416eaa2008-02-10 20:23:19 -0600897 switch (priv->pkt_fmt) {
898 default:
899 case packet_format_1:
900 port->interrupt_out_buffer[1] = count;
901 break;
902 case packet_format_2:
903 port->interrupt_out_buffer[0] |= count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 }
905
906 dbg("%s - count is %d", __FUNCTION__, count);
907
908send:
909 spin_lock_irqsave(&priv->lock, flags);
910 priv->write_urb_in_use = 1;
911 spin_unlock_irqrestore(&priv->lock, flags);
912
913 if (priv->cmd_ctrl)
914 actual_size = 1;
915 else
Mike Isely3416eaa2008-02-10 20:23:19 -0600916 actual_size = count +
917 (priv->pkt_fmt == packet_format_1 ? 2 : 1);
918
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, port->interrupt_out_size,
920 port->interrupt_out_urb->transfer_buffer);
921
Mike Isely9aa8dae2006-08-29 22:07:04 -0500922 usb_fill_int_urb(port->interrupt_out_urb, port->serial->dev,
923 usb_sndintpipe(port->serial->dev, port->interrupt_out_endpointAddress),
924 port->interrupt_out_buffer, port->interrupt_out_size,
925 cypress_write_int_callback, port, priv->write_urb_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 result = usb_submit_urb (port->interrupt_out_urb, GFP_ATOMIC);
927 if (result) {
928 dev_err(&port->dev, "%s - failed submitting write urb, error %d\n", __FUNCTION__,
929 result);
930 priv->write_urb_in_use = 0;
Mike Isely78aef512006-08-29 22:07:11 -0500931 cypress_set_dead(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 }
933
934 spin_lock_irqsave(&priv->lock, flags);
935 if (priv->cmd_ctrl) {
936 priv->cmd_ctrl = 0;
937 }
938 priv->bytes_out += count; /* do not count the line control and size bytes */
939 spin_unlock_irqrestore(&priv->lock, flags);
940
Pete Zaitcevcf2c7482006-05-22 21:58:49 -0700941 usb_serial_port_softint(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942} /* cypress_send */
943
944
945/* returns how much space is available in the soft buffer */
946static int cypress_write_room(struct usb_serial_port *port)
947{
948 struct cypress_private *priv = usb_get_serial_port_data(port);
949 int room = 0;
950 unsigned long flags;
951
952 dbg("%s - port %d", __FUNCTION__, port->number);
953
954 spin_lock_irqsave(&priv->lock, flags);
955 room = cypress_buf_space_avail(priv->buf);
956 spin_unlock_irqrestore(&priv->lock, flags);
957
958 dbg("%s - returns %d", __FUNCTION__, room);
959 return room;
960}
961
962
963static int cypress_tiocmget (struct usb_serial_port *port, struct file *file)
964{
965 struct cypress_private *priv = usb_get_serial_port_data(port);
966 __u8 status, control;
967 unsigned int result = 0;
968 unsigned long flags;
969
970 dbg("%s - port %d", __FUNCTION__, port->number);
971
972 spin_lock_irqsave(&priv->lock, flags);
973 control = priv->line_control;
974 status = priv->current_status;
975 spin_unlock_irqrestore(&priv->lock, flags);
976
977 result = ((control & CONTROL_DTR) ? TIOCM_DTR : 0)
978 | ((control & CONTROL_RTS) ? TIOCM_RTS : 0)
979 | ((status & UART_CTS) ? TIOCM_CTS : 0)
980 | ((status & UART_DSR) ? TIOCM_DSR : 0)
981 | ((status & UART_RI) ? TIOCM_RI : 0)
982 | ((status & UART_CD) ? TIOCM_CD : 0);
983
984 dbg("%s - result = %x", __FUNCTION__, result);
985
986 return result;
987}
988
989
990static int cypress_tiocmset (struct usb_serial_port *port, struct file *file,
991 unsigned int set, unsigned int clear)
992{
993 struct cypress_private *priv = usb_get_serial_port_data(port);
994 unsigned long flags;
995
996 dbg("%s - port %d", __FUNCTION__, port->number);
997
998 spin_lock_irqsave(&priv->lock, flags);
999 if (set & TIOCM_RTS)
1000 priv->line_control |= CONTROL_RTS;
1001 if (set & TIOCM_DTR)
1002 priv->line_control |= CONTROL_DTR;
1003 if (clear & TIOCM_RTS)
1004 priv->line_control &= ~CONTROL_RTS;
1005 if (clear & TIOCM_DTR)
1006 priv->line_control &= ~CONTROL_DTR;
1007 spin_unlock_irqrestore(&priv->lock, flags);
1008
1009 priv->cmd_ctrl = 1;
1010 return cypress_write(port, NULL, 0);
1011}
1012
1013
1014static int cypress_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg)
1015{
1016 struct cypress_private *priv = usb_get_serial_port_data(port);
1017
1018 dbg("%s - port %d, cmd 0x%.4x", __FUNCTION__, port->number, cmd);
1019
1020 switch (cmd) {
1021 case TIOCGSERIAL:
Alan Cox606d0992006-12-08 02:38:45 -08001022 if (copy_to_user((void __user *)arg, port->tty->termios, sizeof(struct ktermios))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 return -EFAULT;
1024 }
1025 return (0);
1026 break;
1027 case TIOCSSERIAL:
Alan Cox606d0992006-12-08 02:38:45 -08001028 if (copy_from_user(port->tty->termios, (void __user *)arg, sizeof(struct ktermios))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 return -EFAULT;
1030 }
1031 /* here we need to call cypress_set_termios to invoke the new settings */
1032 cypress_set_termios(port, &priv->tmp_termios);
1033 return (0);
1034 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 /* This code comes from drivers/char/serial.c and ftdi_sio.c */
1036 case TIOCMIWAIT:
1037 while (priv != NULL) {
1038 interruptible_sleep_on(&priv->delta_msr_wait);
1039 /* see if a signal did it */
1040 if (signal_pending(current))
1041 return -ERESTARTSYS;
1042 else {
1043 char diff = priv->diff_status;
1044
1045 if (diff == 0) {
1046 return -EIO; /* no change => error */
1047 }
1048
1049 /* consume all events */
1050 priv->diff_status = 0;
1051
1052 /* return 0 if caller wanted to know about these bits */
1053 if ( ((arg & TIOCM_RNG) && (diff & UART_RI)) ||
1054 ((arg & TIOCM_DSR) && (diff & UART_DSR)) ||
1055 ((arg & TIOCM_CD) && (diff & UART_CD)) ||
1056 ((arg & TIOCM_CTS) && (diff & UART_CTS)) ) {
1057 return 0;
1058 }
1059 /* otherwise caller can't care less about what happened,
1060 * and so we continue to wait for more events.
1061 */
1062 }
1063 }
1064 return 0;
1065 break;
1066 default:
1067 break;
1068 }
1069
1070 dbg("%s - arg not supported - it was 0x%04x - check include/asm/ioctls.h", __FUNCTION__, cmd);
1071
1072 return -ENOIOCTLCMD;
1073} /* cypress_ioctl */
1074
1075
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001076static void cypress_set_termios (struct usb_serial_port *port,
Alan Cox606d0992006-12-08 02:38:45 -08001077 struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078{
1079 struct cypress_private *priv = usb_get_serial_port_data(port);
1080 struct tty_struct *tty;
1081 int data_bits, stop_bits, parity_type, parity_enable;
1082 unsigned cflag, iflag, baud_mask;
1083 unsigned long flags;
1084 __u8 oldlines;
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001085 int linechange = 0;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001086
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 dbg("%s - port %d", __FUNCTION__, port->number);
1088
1089 tty = port->tty;
1090 if ((!tty) || (!tty->termios)) {
1091 dbg("%s - no tty structures", __FUNCTION__);
1092 return;
1093 }
1094
1095 spin_lock_irqsave(&priv->lock, flags);
1096 if (!priv->termios_initialized) {
1097 if (priv->chiptype == CT_EARTHMATE) {
1098 *(tty->termios) = tty_std_termios;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001099 tty->termios->c_cflag = B4800 | CS8 | CREAD | HUPCL |
1100 CLOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 } else if (priv->chiptype == CT_CYPHIDCOM) {
1102 *(tty->termios) = tty_std_termios;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001103 tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
1104 CLOCAL;
Lonnie Mendeza5c44e22006-03-01 10:45:24 -06001105 } else if (priv->chiptype == CT_CA42V2) {
1106 *(tty->termios) = tty_std_termios;
1107 tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
1108 CLOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 }
1110 priv->termios_initialized = 1;
1111 }
1112 spin_unlock_irqrestore(&priv->lock, flags);
1113
1114 cflag = tty->termios->c_cflag;
1115 iflag = tty->termios->c_iflag;
1116
1117 /* check if there are new settings */
1118 if (old_termios) {
1119 if ((cflag != old_termios->c_cflag) ||
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001120 (RELEVANT_IFLAG(iflag) !=
1121 RELEVANT_IFLAG(old_termios->c_iflag))) {
1122 dbg("%s - attempting to set new termios settings",
1123 __FUNCTION__);
1124 /* should make a copy of this in case something goes
1125 * wrong in the function, we can restore it */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 spin_lock_irqsave(&priv->lock, flags);
1127 priv->tmp_termios = *(tty->termios);
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001128 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 } else {
1130 dbg("%s - nothing to do, exiting", __FUNCTION__);
1131 return;
1132 }
1133 } else
1134 return;
1135
1136 /* set number of data bits, parity, stop bits */
1137 /* when parity is disabled the parity type bit is ignored */
1138
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001139 /* 1 means 2 stop bits, 0 means 1 stop bit */
1140 stop_bits = cflag & CSTOPB ? 1 : 0;
1141
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 if (cflag & PARENB) {
1143 parity_enable = 1;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001144 /* 1 means odd parity, 0 means even parity */
1145 parity_type = cflag & PARODD ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 } else
1147 parity_enable = parity_type = 0;
1148
1149 if (cflag & CSIZE) {
1150 switch (cflag & CSIZE) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001151 case CS5:
1152 data_bits = 0;
1153 break;
1154 case CS6:
1155 data_bits = 1;
1156 break;
1157 case CS7:
1158 data_bits = 2;
1159 break;
1160 case CS8:
1161 data_bits = 3;
1162 break;
1163 default:
1164 err("%s - CSIZE was set, but not CS5-CS8",
1165 __FUNCTION__);
1166 data_bits = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 }
1168 } else
1169 data_bits = 3;
1170
1171 spin_lock_irqsave(&priv->lock, flags);
1172 oldlines = priv->line_control;
1173 if ((cflag & CBAUD) == B0) {
1174 /* drop dtr and rts */
1175 dbg("%s - dropping the lines, baud rate 0bps", __FUNCTION__);
1176 baud_mask = B0;
1177 priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
1178 } else {
1179 baud_mask = (cflag & CBAUD);
1180 switch(baud_mask) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001181 case B300:
1182 dbg("%s - setting baud 300bps", __FUNCTION__);
1183 break;
1184 case B600:
1185 dbg("%s - setting baud 600bps", __FUNCTION__);
1186 break;
1187 case B1200:
1188 dbg("%s - setting baud 1200bps", __FUNCTION__);
1189 break;
1190 case B2400:
1191 dbg("%s - setting baud 2400bps", __FUNCTION__);
1192 break;
1193 case B4800:
1194 dbg("%s - setting baud 4800bps", __FUNCTION__);
1195 break;
1196 case B9600:
1197 dbg("%s - setting baud 9600bps", __FUNCTION__);
1198 break;
1199 case B19200:
1200 dbg("%s - setting baud 19200bps", __FUNCTION__);
1201 break;
1202 case B38400:
1203 dbg("%s - setting baud 38400bps", __FUNCTION__);
1204 break;
1205 case B57600:
1206 dbg("%s - setting baud 57600bps", __FUNCTION__);
1207 break;
1208 case B115200:
1209 dbg("%s - setting baud 115200bps", __FUNCTION__);
1210 break;
1211 default:
1212 dbg("%s - unknown masked baud rate", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 }
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001214 priv->line_control = (CONTROL_DTR | CONTROL_RTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 }
1216 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001218 dbg("%s - sending %d stop_bits, %d parity_enable, %d parity_type, "
1219 "%d data_bits (+5)", __FUNCTION__, stop_bits,
1220 parity_enable, parity_type, data_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001222 cypress_serial_control(port, baud_mask, data_bits, stop_bits,
1223 parity_enable, parity_type, 0, CYPRESS_SET_CONFIG);
1224
1225 /* we perform a CYPRESS_GET_CONFIG so that the current settings are
1226 * filled into the private structure this should confirm that all is
1227 * working if it returns what we just set */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 cypress_serial_control(port, 0, 0, 0, 0, 0, 0, CYPRESS_GET_CONFIG);
1229
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001230 /* Here we can define custom tty settings for devices; the main tty
1231 * termios flag base comes from empeg.c */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001233 spin_lock_irqsave(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 if ( (priv->chiptype == CT_EARTHMATE) && (priv->baud_rate == 4800) ) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001235 dbg("Using custom termios settings for a baud rate of "
1236 "4800bps.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 /* define custom termios settings for NMEA protocol */
1238
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 tty->termios->c_iflag /* input modes - */
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001240 &= ~(IGNBRK /* disable ignore break */
1241 | BRKINT /* disable break causes interrupt */
1242 | PARMRK /* disable mark parity errors */
1243 | ISTRIP /* disable clear high bit of input char */
1244 | INLCR /* disable translate NL to CR */
1245 | IGNCR /* disable ignore CR */
1246 | ICRNL /* disable translate CR to NL */
1247 | IXON); /* disable enable XON/XOFF flow control */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001249 tty->termios->c_oflag /* output modes */
1250 &= ~OPOST; /* disable postprocess output char */
1251
1252 tty->termios->c_lflag /* line discipline modes */
1253 &= ~(ECHO /* disable echo input characters */
1254 | ECHONL /* disable echo new line */
1255 | ICANON /* disable erase, kill, werase, and rprnt
1256 special characters */
1257 | ISIG /* disable interrupt, quit, and suspend
1258 special characters */
1259 | IEXTEN); /* disable non-POSIX special characters */
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001260 } /* CT_CYPHIDCOM: Application should handle this for device */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 linechange = (priv->line_control != oldlines);
1263 spin_unlock_irqrestore(&priv->lock, flags);
1264
1265 /* if necessary, set lines */
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001266 if (linechange) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 priv->cmd_ctrl = 1;
1268 cypress_write(port, NULL, 0);
1269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270} /* cypress_set_termios */
1271
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001272
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273/* returns amount of data still left in soft buffer */
1274static int cypress_chars_in_buffer(struct usb_serial_port *port)
1275{
1276 struct cypress_private *priv = usb_get_serial_port_data(port);
1277 int chars = 0;
1278 unsigned long flags;
1279
1280 dbg("%s - port %d", __FUNCTION__, port->number);
1281
1282 spin_lock_irqsave(&priv->lock, flags);
1283 chars = cypress_buf_data_avail(priv->buf);
1284 spin_unlock_irqrestore(&priv->lock, flags);
1285
1286 dbg("%s - returns %d", __FUNCTION__, chars);
1287 return chars;
1288}
1289
1290
1291static void cypress_throttle (struct usb_serial_port *port)
1292{
1293 struct cypress_private *priv = usb_get_serial_port_data(port);
1294 unsigned long flags;
1295
1296 dbg("%s - port %d", __FUNCTION__, port->number);
1297
1298 spin_lock_irqsave(&priv->lock, flags);
1299 priv->rx_flags = THROTTLED;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001300 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301}
1302
1303
1304static void cypress_unthrottle (struct usb_serial_port *port)
1305{
1306 struct cypress_private *priv = usb_get_serial_port_data(port);
1307 int actually_throttled, result;
1308 unsigned long flags;
1309
1310 dbg("%s - port %d", __FUNCTION__, port->number);
1311
1312 spin_lock_irqsave(&priv->lock, flags);
1313 actually_throttled = priv->rx_flags & ACTUALLY_THROTTLED;
1314 priv->rx_flags = 0;
1315 spin_unlock_irqrestore(&priv->lock, flags);
1316
Mike Isely78aef512006-08-29 22:07:11 -05001317 if (!priv->comm_is_ok)
1318 return;
1319
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 if (actually_throttled) {
1321 port->interrupt_in_urb->dev = port->serial->dev;
1322
1323 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
Mike Isely78aef512006-08-29 22:07:11 -05001324 if (result) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001325 dev_err(&port->dev, "%s - failed submitting read urb, "
1326 "error %d\n", __FUNCTION__, result);
Mike Isely78aef512006-08-29 22:07:11 -05001327 cypress_set_dead(port);
1328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 }
1330}
1331
1332
David Howells7d12e782006-10-05 14:55:46 +01001333static void cypress_read_int_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334{
1335 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
1336 struct cypress_private *priv = usb_get_serial_port_data(port);
1337 struct tty_struct *tty;
1338 unsigned char *data = urb->transfer_buffer;
1339 unsigned long flags;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001340 char tty_flag = TTY_NORMAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 int havedata = 0;
1342 int bytes = 0;
1343 int result;
1344 int i = 0;
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001345 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
1347 dbg("%s - port %d", __FUNCTION__, port->number);
1348
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001349 switch (status) {
Mike Isely78aef512006-08-29 22:07:11 -05001350 case 0: /* success */
1351 break;
1352 case -ECONNRESET:
1353 case -ENOENT:
1354 case -ESHUTDOWN:
1355 /* precursor to disconnect so just go away */
1356 return;
1357 case -EPIPE:
1358 usb_clear_halt(port->serial->dev,0x81);
1359 break;
1360 default:
1361 /* something ugly is going on... */
1362 dev_err(&urb->dev->dev,"%s - unexpected nonzero read status received: %d\n",
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001363 __FUNCTION__, status);
Mike Isely78aef512006-08-29 22:07:11 -05001364 cypress_set_dead(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 return;
1366 }
1367
1368 spin_lock_irqsave(&priv->lock, flags);
1369 if (priv->rx_flags & THROTTLED) {
1370 dbg("%s - now throttling", __FUNCTION__);
1371 priv->rx_flags |= ACTUALLY_THROTTLED;
1372 spin_unlock_irqrestore(&priv->lock, flags);
1373 return;
1374 }
1375 spin_unlock_irqrestore(&priv->lock, flags);
1376
1377 tty = port->tty;
1378 if (!tty) {
1379 dbg("%s - bad tty pointer - exiting", __FUNCTION__);
1380 return;
1381 }
1382
1383 spin_lock_irqsave(&priv->lock, flags);
Mike Isely3416eaa2008-02-10 20:23:19 -06001384 result = urb->actual_length;
1385 switch (priv->pkt_fmt) {
1386 default:
1387 case packet_format_1:
1388 /* This is for the CY7C64013... */
1389 priv->current_status = data[0] & 0xF8;
1390 bytes = data[1] + 2;
1391 i = 2;
1392 if (bytes > 2)
1393 havedata = 1;
1394 break;
1395 case packet_format_2:
1396 /* This is for the CY7C63743... */
1397 priv->current_status = data[0] & 0xF8;
1398 bytes = (data[0] & 0x07) + 1;
1399 i = 1;
1400 if (bytes > 1)
1401 havedata = 1;
1402 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 }
1404 spin_unlock_irqrestore(&priv->lock, flags);
Mike Isely3416eaa2008-02-10 20:23:19 -06001405 if (result < bytes) {
1406 dbg("%s - wrong packet size - received %d bytes but packet "
1407 "said %d bytes", __func__, result, bytes);
1408 goto continue_read;
1409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001411 usb_serial_debug_data (debug, &port->dev, __FUNCTION__,
1412 urb->actual_length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
1414 spin_lock_irqsave(&priv->lock, flags);
1415 /* check to see if status has changed */
Mike Isely67683062008-02-10 20:23:28 -06001416 if (priv->current_status != priv->prev_status) {
1417 priv->diff_status |= priv->current_status ^
1418 priv->prev_status;
1419 wake_up_interruptible(&priv->delta_msr_wait);
1420 priv->prev_status = priv->current_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 }
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001422 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001424 /* hangup, as defined in acm.c... this might be a bad place for it
1425 * though */
1426 if (tty && !(tty->termios->c_cflag & CLOCAL) &&
1427 !(priv->current_status & UART_CD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 dbg("%s - calling hangup", __FUNCTION__);
1429 tty_hangup(tty);
1430 goto continue_read;
1431 }
1432
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001433 /* There is one error bit... I'm assuming it is a parity error
1434 * indicator as the generic firmware will set this bit to 1 if a
1435 * parity error occurs.
1436 * I can not find reference to any other error events. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 spin_lock_irqsave(&priv->lock, flags);
1438 if (priv->current_status & CYP_ERROR) {
1439 spin_unlock_irqrestore(&priv->lock, flags);
1440 tty_flag = TTY_PARITY;
1441 dbg("%s - Parity Error detected", __FUNCTION__);
1442 } else
1443 spin_unlock_irqrestore(&priv->lock, flags);
1444
1445 /* process read if there is data other than line status */
1446 if (tty && (bytes > i)) {
Alan Cox33f0f882006-01-09 20:54:13 -08001447 bytes = tty_buffer_request_room(tty, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 for (; i < bytes ; ++i) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001449 dbg("pushing byte number %d - %d - %c", i, data[i],
1450 data[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 tty_insert_flip_char(tty, data[i], tty_flag);
1452 }
1453 tty_flip_buffer_push(port->tty);
1454 }
1455
1456 spin_lock_irqsave(&priv->lock, flags);
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001457 /* control and status byte(s) are also counted */
1458 priv->bytes_in += bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 spin_unlock_irqrestore(&priv->lock, flags);
1460
1461continue_read:
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001462
1463 /* Continue trying to always read... unless the port has closed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
Mike Isely78aef512006-08-29 22:07:11 -05001465 if (port->open_count > 0 && priv->comm_is_ok) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001466 usb_fill_int_urb(port->interrupt_in_urb, port->serial->dev,
1467 usb_rcvintpipe(port->serial->dev,
1468 port->interrupt_in_endpointAddress),
1469 port->interrupt_in_urb->transfer_buffer,
1470 port->interrupt_in_urb->transfer_buffer_length,
Mike Isely0257fa92006-08-29 22:06:59 -05001471 cypress_read_int_callback, port, priv->read_urb_interval);
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001472 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
Mike Isely78aef512006-08-29 22:07:11 -05001473 if (result) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001474 dev_err(&urb->dev->dev, "%s - failed resubmitting "
1475 "read urb, error %d\n", __FUNCTION__,
1476 result);
Mike Isely78aef512006-08-29 22:07:11 -05001477 cypress_set_dead(port);
1478 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 }
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001480
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 return;
1482} /* cypress_read_int_callback */
1483
1484
David Howells7d12e782006-10-05 14:55:46 +01001485static void cypress_write_int_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486{
1487 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
1488 struct cypress_private *priv = usb_get_serial_port_data(port);
1489 int result;
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001490 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
1492 dbg("%s - port %d", __FUNCTION__, port->number);
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001493
1494 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 case 0:
1496 /* success */
1497 break;
1498 case -ECONNRESET:
1499 case -ENOENT:
1500 case -ESHUTDOWN:
1501 /* this urb is terminated, clean up */
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001502 dbg("%s - urb shutting down with status: %d",
1503 __FUNCTION__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 priv->write_urb_in_use = 0;
1505 return;
Mike Isely78aef512006-08-29 22:07:11 -05001506 case -EPIPE: /* no break needed; clear halt and resubmit */
1507 if (!priv->comm_is_ok)
1508 break;
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001509 usb_clear_halt(port->serial->dev, 0x02);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 /* error in the urb, so we have to resubmit it */
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001511 dbg("%s - nonzero write bulk status received: %d",
1512 __FUNCTION__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 port->interrupt_out_urb->transfer_buffer_length = 1;
1514 port->interrupt_out_urb->dev = port->serial->dev;
1515 result = usb_submit_urb(port->interrupt_out_urb, GFP_ATOMIC);
Mike Isely78aef512006-08-29 22:07:11 -05001516 if (!result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 return;
Mike Isely78aef512006-08-29 22:07:11 -05001518 dev_err(&urb->dev->dev, "%s - failed resubmitting write urb, error %d\n",
1519 __FUNCTION__, result);
1520 cypress_set_dead(port);
1521 break;
1522 default:
1523 dev_err(&urb->dev->dev,"%s - unexpected nonzero write status received: %d\n",
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001524 __FUNCTION__, status);
Mike Isely78aef512006-08-29 22:07:11 -05001525 cypress_set_dead(port);
1526 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 }
1528
1529 priv->write_urb_in_use = 0;
1530
1531 /* send any buffered data */
1532 cypress_send(port);
1533}
1534
1535
1536/*****************************************************************************
1537 * Write buffer functions - buffering code from pl2303 used
1538 *****************************************************************************/
1539
1540/*
1541 * cypress_buf_alloc
1542 *
1543 * Allocate a circular buffer and all associated memory.
1544 */
1545
1546static struct cypress_buf *cypress_buf_alloc(unsigned int size)
1547{
1548
1549 struct cypress_buf *cb;
1550
1551
1552 if (size == 0)
1553 return NULL;
1554
Robert P. J. Day5cbded52006-12-13 00:35:56 -08001555 cb = kmalloc(sizeof(struct cypress_buf), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 if (cb == NULL)
1557 return NULL;
1558
1559 cb->buf_buf = kmalloc(size, GFP_KERNEL);
1560 if (cb->buf_buf == NULL) {
1561 kfree(cb);
1562 return NULL;
1563 }
1564
1565 cb->buf_size = size;
1566 cb->buf_get = cb->buf_put = cb->buf_buf;
1567
1568 return cb;
1569
1570}
1571
1572
1573/*
1574 * cypress_buf_free
1575 *
1576 * Free the buffer and all associated memory.
1577 */
1578
1579static void cypress_buf_free(struct cypress_buf *cb)
1580{
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001581 if (cb) {
1582 kfree(cb->buf_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 kfree(cb);
1584 }
1585}
1586
1587
1588/*
1589 * cypress_buf_clear
1590 *
1591 * Clear out all data in the circular buffer.
1592 */
1593
1594static void cypress_buf_clear(struct cypress_buf *cb)
1595{
1596 if (cb != NULL)
1597 cb->buf_get = cb->buf_put;
1598 /* equivalent to a get of all data available */
1599}
1600
1601
1602/*
1603 * cypress_buf_data_avail
1604 *
1605 * Return the number of bytes of data available in the circular
1606 * buffer.
1607 */
1608
1609static unsigned int cypress_buf_data_avail(struct cypress_buf *cb)
1610{
1611 if (cb != NULL)
1612 return ((cb->buf_size + cb->buf_put - cb->buf_get) % cb->buf_size);
1613 else
1614 return 0;
1615}
1616
1617
1618/*
1619 * cypress_buf_space_avail
1620 *
1621 * Return the number of bytes of space available in the circular
1622 * buffer.
1623 */
1624
1625static unsigned int cypress_buf_space_avail(struct cypress_buf *cb)
1626{
1627 if (cb != NULL)
1628 return ((cb->buf_size + cb->buf_get - cb->buf_put - 1) % cb->buf_size);
1629 else
1630 return 0;
1631}
1632
1633
1634/*
1635 * cypress_buf_put
1636 *
1637 * Copy data data from a user buffer and put it into the circular buffer.
1638 * Restrict to the amount of space available.
1639 *
1640 * Return the number of bytes copied.
1641 */
1642
1643static unsigned int cypress_buf_put(struct cypress_buf *cb, const char *buf,
1644 unsigned int count)
1645{
1646
1647 unsigned int len;
1648
1649
1650 if (cb == NULL)
1651 return 0;
1652
1653 len = cypress_buf_space_avail(cb);
1654 if (count > len)
1655 count = len;
1656
1657 if (count == 0)
1658 return 0;
1659
1660 len = cb->buf_buf + cb->buf_size - cb->buf_put;
1661 if (count > len) {
1662 memcpy(cb->buf_put, buf, len);
1663 memcpy(cb->buf_buf, buf+len, count - len);
1664 cb->buf_put = cb->buf_buf + count - len;
1665 } else {
1666 memcpy(cb->buf_put, buf, count);
1667 if (count < len)
1668 cb->buf_put += count;
1669 else /* count == len */
1670 cb->buf_put = cb->buf_buf;
1671 }
1672
1673 return count;
1674
1675}
1676
1677
1678/*
1679 * cypress_buf_get
1680 *
1681 * Get data from the circular buffer and copy to the given buffer.
1682 * Restrict to the amount of data available.
1683 *
1684 * Return the number of bytes copied.
1685 */
1686
1687static unsigned int cypress_buf_get(struct cypress_buf *cb, char *buf,
1688 unsigned int count)
1689{
1690
1691 unsigned int len;
1692
1693
1694 if (cb == NULL)
1695 return 0;
1696
1697 len = cypress_buf_data_avail(cb);
1698 if (count > len)
1699 count = len;
1700
1701 if (count == 0)
1702 return 0;
1703
1704 len = cb->buf_buf + cb->buf_size - cb->buf_get;
1705 if (count > len) {
1706 memcpy(buf, cb->buf_get, len);
1707 memcpy(buf+len, cb->buf_buf, count - len);
1708 cb->buf_get = cb->buf_buf + count - len;
1709 } else {
1710 memcpy(buf, cb->buf_get, count);
1711 if (count < len)
1712 cb->buf_get += count;
1713 else /* count == len */
1714 cb->buf_get = cb->buf_buf;
1715 }
1716
1717 return count;
1718
1719}
1720
1721/*****************************************************************************
1722 * Module functions
1723 *****************************************************************************/
1724
1725static int __init cypress_init(void)
1726{
1727 int retval;
1728
1729 dbg("%s", __FUNCTION__);
1730
1731 retval = usb_serial_register(&cypress_earthmate_device);
1732 if (retval)
1733 goto failed_em_register;
1734 retval = usb_serial_register(&cypress_hidcom_device);
1735 if (retval)
1736 goto failed_hidcom_register;
Lonnie Mendeza5c44e22006-03-01 10:45:24 -06001737 retval = usb_serial_register(&cypress_ca42v2_device);
1738 if (retval)
1739 goto failed_ca42v2_register;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 retval = usb_register(&cypress_driver);
1741 if (retval)
1742 goto failed_usb_register;
1743
1744 info(DRIVER_DESC " " DRIVER_VERSION);
1745 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
Mariusz Kozlowski2e46b742006-11-17 17:49:22 +01001747failed_usb_register:
1748 usb_serial_deregister(&cypress_ca42v2_device);
1749failed_ca42v2_register:
1750 usb_serial_deregister(&cypress_hidcom_device);
1751failed_hidcom_register:
1752 usb_serial_deregister(&cypress_earthmate_device);
1753failed_em_register:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 return retval;
1755}
1756
1757
1758static void __exit cypress_exit (void)
1759{
1760 dbg("%s", __FUNCTION__);
1761
1762 usb_deregister (&cypress_driver);
1763 usb_serial_deregister (&cypress_earthmate_device);
1764 usb_serial_deregister (&cypress_hidcom_device);
Lonnie Mendeza5c44e22006-03-01 10:45:24 -06001765 usb_serial_deregister (&cypress_ca42v2_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766}
1767
1768
1769module_init(cypress_init);
1770module_exit(cypress_exit);
1771
1772MODULE_AUTHOR( DRIVER_AUTHOR );
1773MODULE_DESCRIPTION( DRIVER_DESC );
1774MODULE_VERSION( DRIVER_VERSION );
1775MODULE_LICENSE("GPL");
1776
1777module_param(debug, bool, S_IRUGO | S_IWUSR);
1778MODULE_PARM_DESC(debug, "Debug enabled or not");
1779module_param(stats, bool, S_IRUGO | S_IWUSR);
1780MODULE_PARM_DESC(stats, "Enable statistics or not");
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001781module_param(interval, int, S_IRUGO | S_IWUSR);
1782MODULE_PARM_DESC(interval, "Overrides interrupt interval");