blob: 6999d3372d85428627ace1a7e9e94af452bd7564 [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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 int isthrottled; /* if throttled, discard reads */
151 wait_queue_head_t delta_msr_wait; /* used for TIOCMIWAIT */
152 char prev_status, diff_status; /* used for TIOCMIWAIT */
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);
Alan Cox95da3102008-07-22 11:09:07 +0100170static int cypress_open (struct tty_struct *tty, struct usb_serial_port *port, struct file *filp);
171static void cypress_close (struct tty_struct *tty, struct usb_serial_port *port, struct file *filp);
172static int cypress_write (struct tty_struct *tty, struct usb_serial_port *port, const unsigned char *buf, int count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173static void cypress_send (struct usb_serial_port *port);
Alan Cox95da3102008-07-22 11:09:07 +0100174static int cypress_write_room (struct tty_struct *tty);
175static int cypress_ioctl (struct tty_struct *tty, struct file * file, unsigned int cmd, unsigned long arg);
176static void cypress_set_termios (struct tty_struct *tty, struct usb_serial_port *port, struct ktermios * old);
177static int cypress_tiocmget (struct tty_struct *tty, struct file *file);
178static int cypress_tiocmset (struct tty_struct *tty, struct file *file, unsigned int set, unsigned int clear);
179static int cypress_chars_in_buffer (struct tty_struct *tty);
180static void cypress_throttle (struct tty_struct *tty);
181static void cypress_unthrottle (struct tty_struct *tty);
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/* write buffer functions */
186static struct cypress_buf *cypress_buf_alloc(unsigned int size);
187static void cypress_buf_free(struct cypress_buf *cb);
188static void cypress_buf_clear(struct cypress_buf *cb);
189static unsigned int cypress_buf_data_avail(struct cypress_buf *cb);
190static unsigned int cypress_buf_space_avail(struct cypress_buf *cb);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500191static unsigned int cypress_buf_put(struct cypress_buf *cb, const char *buf, unsigned int count);
192static unsigned int cypress_buf_get(struct cypress_buf *cb, char *buf, unsigned int count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700195static struct usb_serial_driver cypress_earthmate_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700196 .driver = {
197 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700198 .name = "earthmate",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700199 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700200 .description = "DeLorme Earthmate USB",
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100201 .usb_driver = &cypress_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 .id_table = id_table_earthmate,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 .num_ports = 1,
204 .attach = cypress_earthmate_startup,
205 .shutdown = cypress_shutdown,
206 .open = cypress_open,
207 .close = cypress_close,
208 .write = cypress_write,
209 .write_room = cypress_write_room,
210 .ioctl = cypress_ioctl,
211 .set_termios = cypress_set_termios,
212 .tiocmget = cypress_tiocmget,
213 .tiocmset = cypress_tiocmset,
214 .chars_in_buffer = cypress_chars_in_buffer,
215 .throttle = cypress_throttle,
216 .unthrottle = cypress_unthrottle,
217 .read_int_callback = cypress_read_int_callback,
218 .write_int_callback = cypress_write_int_callback,
219};
220
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700221static struct usb_serial_driver cypress_hidcom_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700222 .driver = {
223 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700224 .name = "cyphidcom",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700225 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700226 .description = "HID->COM RS232 Adapter",
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100227 .usb_driver = &cypress_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 .id_table = id_table_cyphidcomrs232,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 .num_ports = 1,
230 .attach = cypress_hidcom_startup,
231 .shutdown = cypress_shutdown,
232 .open = cypress_open,
233 .close = cypress_close,
234 .write = cypress_write,
235 .write_room = cypress_write_room,
236 .ioctl = cypress_ioctl,
237 .set_termios = cypress_set_termios,
238 .tiocmget = cypress_tiocmget,
239 .tiocmset = cypress_tiocmset,
240 .chars_in_buffer = cypress_chars_in_buffer,
241 .throttle = cypress_throttle,
242 .unthrottle = cypress_unthrottle,
243 .read_int_callback = cypress_read_int_callback,
244 .write_int_callback = cypress_write_int_callback,
245};
246
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600247static struct usb_serial_driver cypress_ca42v2_device = {
248 .driver = {
249 .owner = THIS_MODULE,
250 .name = "nokiaca42v2",
251 },
252 .description = "Nokia CA-42 V2 Adapter",
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100253 .usb_driver = &cypress_driver,
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600254 .id_table = id_table_nokiaca42v2,
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600255 .num_ports = 1,
256 .attach = cypress_ca42v2_startup,
257 .shutdown = cypress_shutdown,
258 .open = cypress_open,
259 .close = cypress_close,
260 .write = cypress_write,
261 .write_room = cypress_write_room,
262 .ioctl = cypress_ioctl,
263 .set_termios = cypress_set_termios,
264 .tiocmget = cypress_tiocmget,
265 .tiocmset = cypress_tiocmset,
266 .chars_in_buffer = cypress_chars_in_buffer,
267 .throttle = cypress_throttle,
268 .unthrottle = cypress_unthrottle,
269 .read_int_callback = cypress_read_int_callback,
270 .write_int_callback = cypress_write_int_callback,
271};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273/*****************************************************************************
274 * Cypress serial helper functions
275 *****************************************************************************/
276
277
Alan Cox8873aaa2008-03-10 21:59:28 +0000278static int analyze_baud_rate(struct usb_serial_port *port, speed_t new_rate)
Mike Isely92983c22008-02-10 20:23:32 -0600279{
Mike Isely92983c22008-02-10 20:23:32 -0600280 struct cypress_private *priv;
281 priv = usb_get_serial_port_data(port);
282
283 /*
284 * The general purpose firmware for the Cypress M8 allows for
285 * a maximum speed of 57600bps (I have no idea whether DeLorme
286 * chose to use the general purpose firmware or not), if you
287 * need to modify this speed setting for your own project
288 * please add your own chiptype and modify the code likewise.
289 * The Cypress HID->COM device will work successfully up to
290 * 115200bps (but the actual throughput is around 3kBps).
291 */
Mike Isely92983c22008-02-10 20:23:32 -0600292 if (port->serial->dev->speed == USB_SPEED_LOW) {
293 /*
294 * Mike Isely <isely@pobox.com> 2-Feb-2008: The
295 * Cypress app note that describes this mechanism
296 * states the the low-speed part can't handle more
297 * than 800 bytes/sec, in which case 4800 baud is the
298 * safest speed for a part like that.
299 */
300 if (new_rate > 4800) {
301 dbg("%s - failed setting baud rate, device incapable "
302 "speed %d", __func__, new_rate);
303 return -1;
304 }
305 }
306 switch (priv->chiptype) {
307 case CT_EARTHMATE:
308 if (new_rate <= 600) {
309 /* 300 and 600 baud rates are supported under
310 * the generic firmware, but are not used with
311 * NMEA and SiRF protocols */
312 dbg("%s - failed setting baud rate, unsupported speed "
313 "of %d on Earthmate GPS", __func__, new_rate);
314 return -1;
315 }
316 break;
317 default:
318 break;
319 }
320 return new_rate;
321}
322
323
Steven Cole093cf722005-05-03 19:07:24 -0600324/* This function can either set or retrieve the current serial line settings */
Alan Cox95da3102008-07-22 11:09:07 +0100325static int cypress_serial_control (struct tty_struct *tty,
326 struct usb_serial_port *port, speed_t baud_rate, int data_bits,
327 int stop_bits, int parity_enable, int parity_type, int reset,
328 int cypress_request_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500330 int new_baudrate = 0, retval = 0, tries = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 struct cypress_private *priv;
Mike Isely93075542008-02-10 20:23:14 -0600332 __u8 feature_buffer[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 unsigned long flags;
334
Harvey Harrison441b62c2008-03-03 16:08:34 -0800335 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337 priv = usb_get_serial_port_data(port);
338
Mike Isely78aef512006-08-29 22:07:11 -0500339 if (!priv->comm_is_ok)
340 return -ENODEV;
341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 switch(cypress_request_type) {
343 case CYPRESS_SET_CONFIG:
Mike Isely92983c22008-02-10 20:23:32 -0600344 new_baudrate = priv->baud_rate;
Alan Cox8873aaa2008-03-10 21:59:28 +0000345 /* 0 means 'Hang up' so doesn't change the true bit rate */
346 if (baud_rate == 0)
347 new_baudrate = priv->baud_rate;
348 /* Change of speed ? */
349 else if (baud_rate != priv->baud_rate) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800350 dbg("%s - baud rate is changing", __func__);
Alan Cox8873aaa2008-03-10 21:59:28 +0000351 retval = analyze_baud_rate(port, baud_rate);
Mike Isely92983c22008-02-10 20:23:32 -0600352 if (retval >= 0) {
353 new_baudrate = retval;
354 dbg("%s - New baud rate set to %d",
355 __func__, new_baudrate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 }
Harvey Harrison441b62c2008-03-03 16:08:34 -0800358 dbg("%s - baud rate is being sent as %d", __func__, new_baudrate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Mike Isely93075542008-02-10 20:23:14 -0600360 memset(feature_buffer, 0, sizeof(feature_buffer));
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500361 /* fill the feature_buffer with new configuration */
362 *((u_int32_t *)feature_buffer) = new_baudrate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500364 feature_buffer[4] |= data_bits; /* assign data bits in 2 bit space ( max 3 ) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 /* 1 bit gap */
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500366 feature_buffer[4] |= (stop_bits << 3); /* assign stop bits in 1 bit space */
367 feature_buffer[4] |= (parity_enable << 4); /* assign parity flag in 1 bit space */
368 feature_buffer[4] |= (parity_type << 5); /* assign parity type in 1 bit space */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 /* 1 bit gap */
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500370 feature_buffer[4] |= (reset << 7); /* assign reset at end of byte, 1 bit space */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Harvey Harrison441b62c2008-03-03 16:08:34 -0800372 dbg("%s - device is being sent this feature report:", __func__);
373 dbg("%s - %02X - %02X - %02X - %02X - %02X", __func__, feature_buffer[0], feature_buffer[1],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 feature_buffer[2], feature_buffer[3], feature_buffer[4]);
375
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500376 do {
Mike Isely93075542008-02-10 20:23:14 -0600377 retval = usb_control_msg(port->serial->dev,
378 usb_sndctrlpipe(port->serial->dev, 0),
379 HID_REQ_SET_REPORT,
380 USB_DIR_OUT | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
381 0x0300, 0, feature_buffer,
382 sizeof(feature_buffer), 500);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500384 if (tries++ >= 3)
385 break;
386
Mike Isely93075542008-02-10 20:23:14 -0600387 } while (retval != sizeof(feature_buffer) &&
388 retval != -ENODEV);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500389
Mike Isely93075542008-02-10 20:23:14 -0600390 if (retval != sizeof(feature_buffer)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800391 err("%s - failed sending serial line settings - %d", __func__, retval);
Mike Isely78aef512006-08-29 22:07:11 -0500392 cypress_set_dead(port);
393 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 spin_lock_irqsave(&priv->lock, flags);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500395 priv->baud_rate = new_baudrate;
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500396 priv->current_config = feature_buffer[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 spin_unlock_irqrestore(&priv->lock, flags);
Alan Cox8873aaa2008-03-10 21:59:28 +0000398 /* If we asked for a speed change encode it */
399 if (baud_rate)
Alan Cox95da3102008-07-22 11:09:07 +0100400 tty_encode_baud_rate(tty,
Alan Cox8873aaa2008-03-10 21:59:28 +0000401 new_baudrate, new_baudrate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 }
403 break;
404 case CYPRESS_GET_CONFIG:
Mike Isely3d6aa322008-02-10 20:23:24 -0600405 if (priv->get_cfg_unsafe) {
406 /* Not implemented for this device,
407 and if we try to do it we're likely
408 to crash the hardware. */
409 return -ENOTTY;
410 }
Harvey Harrison441b62c2008-03-03 16:08:34 -0800411 dbg("%s - retreiving serial line settings", __func__);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500412 /* set initial values in feature buffer */
Mike Isely93075542008-02-10 20:23:14 -0600413 memset(feature_buffer, 0, sizeof(feature_buffer));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500415 do {
Mike Isely93075542008-02-10 20:23:14 -0600416 retval = usb_control_msg(port->serial->dev,
417 usb_rcvctrlpipe(port->serial->dev, 0),
418 HID_REQ_GET_REPORT,
419 USB_DIR_IN | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
420 0x0300, 0, feature_buffer,
421 sizeof(feature_buffer), 500);
422
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500423 if (tries++ >= 3)
424 break;
425
Mike Isely93075542008-02-10 20:23:14 -0600426 } while (retval != sizeof(feature_buffer) &&
427 retval != -ENODEV);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500428
Mike Isely93075542008-02-10 20:23:14 -0600429 if (retval != sizeof(feature_buffer)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800430 err("%s - failed to retrieve serial line settings - %d", __func__, retval);
Mike Isely78aef512006-08-29 22:07:11 -0500431 cypress_set_dead(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 return retval;
433 } else {
434 spin_lock_irqsave(&priv->lock, flags);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 /* store the config in one byte, and later use bit masks to check values */
437 priv->current_config = feature_buffer[4];
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500438 priv->baud_rate = *((u_int32_t *)feature_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 spin_unlock_irqrestore(&priv->lock, flags);
440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 }
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500442 spin_lock_irqsave(&priv->lock, flags);
443 ++priv->cmd_count;
444 spin_unlock_irqrestore(&priv->lock, flags);
445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 return retval;
447} /* cypress_serial_control */
448
449
Mike Isely78aef512006-08-29 22:07:11 -0500450static void cypress_set_dead(struct usb_serial_port *port)
451{
452 struct cypress_private *priv = usb_get_serial_port_data(port);
453 unsigned long flags;
454
455 spin_lock_irqsave(&priv->lock, flags);
456 if (!priv->comm_is_ok) {
457 spin_unlock_irqrestore(&priv->lock, flags);
458 return;
459 }
460 priv->comm_is_ok = 0;
461 spin_unlock_irqrestore(&priv->lock, flags);
462
463 err("cypress_m8 suspending failing port %d - interval might be too short",
464 port->number);
465}
466
467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468/*****************************************************************************
469 * Cypress serial driver functions
470 *****************************************************************************/
471
472
473static int generic_startup (struct usb_serial *serial)
474{
475 struct cypress_private *priv;
Mike Isely0257fa92006-08-29 22:06:59 -0500476 struct usb_serial_port *port = serial->port[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
Harvey Harrison441b62c2008-03-03 16:08:34 -0800478 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +0100480 priv = kzalloc(sizeof (struct cypress_private), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 if (!priv)
482 return -ENOMEM;
483
Mike Isely78aef512006-08-29 22:07:11 -0500484 priv->comm_is_ok = !0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 spin_lock_init(&priv->lock);
486 priv->buf = cypress_buf_alloc(CYPRESS_BUF_SIZE);
487 if (priv->buf == NULL) {
488 kfree(priv);
489 return -ENOMEM;
490 }
491 init_waitqueue_head(&priv->delta_msr_wait);
492
493 usb_reset_configuration (serial->dev);
494
495 priv->cmd_ctrl = 0;
496 priv->line_control = 0;
497 priv->termios_initialized = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 priv->rx_flags = 0;
Mike Isely3416eaa2008-02-10 20:23:19 -0600499 /* Default packet format setting is determined by packet size.
500 Anything with a size larger then 9 must have a separate
501 count field since the 3 bit count field is otherwise too
502 small. Otherwise we can use the slightly more compact
503 format. This is in accordance with the cypress_m8 serial
504 converter app note. */
505 if (port->interrupt_out_size > 9) {
506 priv->pkt_fmt = packet_format_1;
507 } else {
508 priv->pkt_fmt = packet_format_2;
509 }
Mike Isely0257fa92006-08-29 22:06:59 -0500510 if (interval > 0) {
511 priv->write_urb_interval = interval;
512 priv->read_urb_interval = interval;
513 dbg("%s - port %d read & write intervals forced to %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800514 __func__,port->number,interval);
Mike Isely0257fa92006-08-29 22:06:59 -0500515 } else {
516 priv->write_urb_interval = port->interrupt_out_urb->interval;
517 priv->read_urb_interval = port->interrupt_in_urb->interval;
518 dbg("%s - port %d intervals: read=%d write=%d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800519 __func__,port->number,
Mike Isely0257fa92006-08-29 22:06:59 -0500520 priv->read_urb_interval,priv->write_urb_interval);
521 }
522 usb_set_serial_port_data(port, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500524 return 0;
525}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
527
528static int cypress_earthmate_startup (struct usb_serial *serial)
529{
530 struct cypress_private *priv;
Mike Isely3d6aa322008-02-10 20:23:24 -0600531 struct usb_serial_port *port = serial->port[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Harvey Harrison441b62c2008-03-03 16:08:34 -0800533 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
535 if (generic_startup(serial)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800536 dbg("%s - Failed setting up port %d", __func__,
Mike Isely3d6aa322008-02-10 20:23:24 -0600537 port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 return 1;
539 }
540
Mike Isely3d6aa322008-02-10 20:23:24 -0600541 priv = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 priv->chiptype = CT_EARTHMATE;
Mike Isely3416eaa2008-02-10 20:23:19 -0600543 /* All Earthmate devices use the separated-count packet
544 format! Idiotic. */
545 priv->pkt_fmt = packet_format_1;
Al Virofd05e722008-04-28 07:00:16 +0100546 if (serial->dev->descriptor.idProduct != cpu_to_le16(PRODUCT_ID_EARTHMATEUSB)) {
Mike Isely3d6aa322008-02-10 20:23:24 -0600547 /* The old original USB Earthmate seemed able to
548 handle GET_CONFIG requests; everything they've
549 produced since that time crashes if this command is
550 attempted :-( */
551 dbg("%s - Marking this device as unsafe for GET_CONFIG "
552 "commands", __func__);
553 priv->get_cfg_unsafe = !0;
554 }
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500555
556 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557} /* cypress_earthmate_startup */
558
559
560static int cypress_hidcom_startup (struct usb_serial *serial)
561{
562 struct cypress_private *priv;
563
Harvey Harrison441b62c2008-03-03 16:08:34 -0800564 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566 if (generic_startup(serial)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800567 dbg("%s - Failed setting up port %d", __func__,
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500568 serial->port[0]->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 return 1;
570 }
571
572 priv = usb_get_serial_port_data(serial->port[0]);
573 priv->chiptype = CT_CYPHIDCOM;
574
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500575 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576} /* cypress_hidcom_startup */
577
578
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600579static int cypress_ca42v2_startup (struct usb_serial *serial)
580{
581 struct cypress_private *priv;
582
Harvey Harrison441b62c2008-03-03 16:08:34 -0800583 dbg("%s", __func__);
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600584
585 if (generic_startup(serial)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800586 dbg("%s - Failed setting up port %d", __func__,
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600587 serial->port[0]->number);
588 return 1;
589 }
590
591 priv = usb_get_serial_port_data(serial->port[0]);
592 priv->chiptype = CT_CA42V2;
593
594 return 0;
595} /* cypress_ca42v2_startup */
596
597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598static void cypress_shutdown (struct usb_serial *serial)
599{
600 struct cypress_private *priv;
601
Harvey Harrison441b62c2008-03-03 16:08:34 -0800602 dbg ("%s - port %d", __func__, serial->port[0]->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604 /* all open ports are closed at this point */
605
606 priv = usb_get_serial_port_data(serial->port[0]);
607
608 if (priv) {
609 cypress_buf_free(priv->buf);
610 kfree(priv);
611 usb_set_serial_port_data(serial->port[0], NULL);
612 }
613}
614
615
Alan Cox95da3102008-07-22 11:09:07 +0100616static int cypress_open(struct tty_struct *tty,
617 struct usb_serial_port *port, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
619 struct cypress_private *priv = usb_get_serial_port_data(port);
620 struct usb_serial *serial = port->serial;
621 unsigned long flags;
622 int result = 0;
623
Harvey Harrison441b62c2008-03-03 16:08:34 -0800624 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Mike Isely78aef512006-08-29 22:07:11 -0500626 if (!priv->comm_is_ok)
627 return -EIO;
628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 /* clear halts before open */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 usb_clear_halt(serial->dev, 0x81);
631 usb_clear_halt(serial->dev, 0x02);
632
633 spin_lock_irqsave(&priv->lock, flags);
634 /* reset read/write statistics */
635 priv->bytes_in = 0;
636 priv->bytes_out = 0;
637 priv->cmd_count = 0;
638 priv->rx_flags = 0;
639 spin_unlock_irqrestore(&priv->lock, flags);
640
641 /* setting to zero could cause data loss */
Alan Cox95da3102008-07-22 11:09:07 +0100642 if (tty)
643 tty->low_latency = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
645 /* raise both lines and set termios */
646 spin_lock_irqsave(&priv->lock, flags);
647 priv->line_control = CONTROL_DTR | CONTROL_RTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 priv->cmd_ctrl = 1;
649 spin_unlock_irqrestore(&priv->lock, flags);
Alan Cox95da3102008-07-22 11:09:07 +0100650 result = cypress_write(tty, port, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
652 if (result) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800653 dev_err(&port->dev, "%s - failed setting the control lines - error %d\n", __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 return result;
655 } else
Harvey Harrison441b62c2008-03-03 16:08:34 -0800656 dbg("%s - success setting the control lines", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Alan Cox95da3102008-07-22 11:09:07 +0100658 if (tty)
659 cypress_set_termios(tty, port, &priv->tmp_termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
661 /* setup the port and start reading from the device */
662 if(!port->interrupt_in_urb){
Harvey Harrison441b62c2008-03-03 16:08:34 -0800663 err("%s - interrupt_in_urb is empty!", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 return(-1);
665 }
666
667 usb_fill_int_urb(port->interrupt_in_urb, serial->dev,
668 usb_rcvintpipe(serial->dev, port->interrupt_in_endpointAddress),
669 port->interrupt_in_urb->transfer_buffer, port->interrupt_in_urb->transfer_buffer_length,
Mike Isely0257fa92006-08-29 22:06:59 -0500670 cypress_read_int_callback, port, priv->read_urb_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
672
673 if (result){
Harvey Harrison441b62c2008-03-03 16:08:34 -0800674 dev_err(&port->dev, "%s - failed submitting read urb, error %d\n", __func__, result);
Mike Isely78aef512006-08-29 22:07:11 -0500675 cypress_set_dead(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 }
677
678 return result;
679} /* cypress_open */
680
681
Alan Cox95da3102008-07-22 11:09:07 +0100682static void cypress_close(struct tty_struct *tty,
683 struct usb_serial_port *port, struct file * filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
685 struct cypress_private *priv = usb_get_serial_port_data(port);
686 unsigned int c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 int bps;
688 long timeout;
689 wait_queue_t wait;
690
Harvey Harrison441b62c2008-03-03 16:08:34 -0800691 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693 /* wait for data to drain from buffer */
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100694 spin_lock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 timeout = CYPRESS_CLOSING_WAIT;
696 init_waitqueue_entry(&wait, current);
Alan Cox95da3102008-07-22 11:09:07 +0100697 add_wait_queue(&tty->write_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 for (;;) {
699 set_current_state(TASK_INTERRUPTIBLE);
700 if (cypress_buf_data_avail(priv->buf) == 0
701 || timeout == 0 || signal_pending(current)
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100702 /* without mutex, allowed due to harmless failure mode */
703 || port->serial->disconnected)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 break;
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100705 spin_unlock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 timeout = schedule_timeout(timeout);
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100707 spin_lock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 }
709 set_current_state(TASK_RUNNING);
Alan Cox95da3102008-07-22 11:09:07 +0100710 remove_wait_queue(&tty->write_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 /* clear out any remaining data in the buffer */
712 cypress_buf_clear(priv->buf);
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100713 spin_unlock_irq(&priv->lock);
714
715 /* writing is potentially harmful, lock must be taken */
716 mutex_lock(&port->serial->disc_mutex);
717 if (port->serial->disconnected) {
718 mutex_unlock(&port->serial->disc_mutex);
719 return;
720 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 /* wait for characters to drain from device */
Alan Cox95da3102008-07-22 11:09:07 +0100722 if (tty) {
723 bps = tty_get_baud_rate(tty);
724 if (bps > 1200)
725 timeout = max((HZ*2560)/bps,HZ/10);
726 else
727 timeout = 2*HZ;
728 schedule_timeout_interruptible(timeout);
729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Harvey Harrison441b62c2008-03-03 16:08:34 -0800731 dbg("%s - stopping urbs", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 usb_kill_urb (port->interrupt_in_urb);
733 usb_kill_urb (port->interrupt_out_urb);
734
Alan Cox95da3102008-07-22 11:09:07 +0100735 if (tty) {
736 c_cflag = tty->termios->c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 if (c_cflag & HUPCL) {
738 /* drop dtr and rts */
739 priv = usb_get_serial_port_data(port);
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100740 spin_lock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 priv->line_control = 0;
742 priv->cmd_ctrl = 1;
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100743 spin_unlock_irq(&priv->lock);
Alan Cox95da3102008-07-22 11:09:07 +0100744 cypress_write(tty, port, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 }
746 }
747
748 if (stats)
749 dev_info (&port->dev, "Statistics: %d Bytes In | %d Bytes Out | %d Commands Issued\n",
750 priv->bytes_in, priv->bytes_out, priv->cmd_count);
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100751 mutex_unlock(&port->serial->disc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752} /* cypress_close */
753
754
Alan Cox95da3102008-07-22 11:09:07 +0100755static int cypress_write(struct tty_struct *tty, struct usb_serial_port *port,
756 const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
758 struct cypress_private *priv = usb_get_serial_port_data(port);
759 unsigned long flags;
760
Harvey Harrison441b62c2008-03-03 16:08:34 -0800761 dbg("%s - port %d, %d bytes", __func__, port->number, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
763 /* line control commands, which need to be executed immediately,
764 are not put into the buffer for obvious reasons.
765 */
766 if (priv->cmd_ctrl) {
767 count = 0;
768 goto finish;
769 }
770
771 if (!count)
772 return count;
773
774 spin_lock_irqsave(&priv->lock, flags);
775 count = cypress_buf_put(priv->buf, buf, count);
776 spin_unlock_irqrestore(&priv->lock, flags);
777
778finish:
779 cypress_send(port);
780
781 return count;
782} /* cypress_write */
783
784
785static void cypress_send(struct usb_serial_port *port)
786{
787 int count = 0, result, offset, actual_size;
788 struct cypress_private *priv = usb_get_serial_port_data(port);
789 unsigned long flags;
790
Mike Isely78aef512006-08-29 22:07:11 -0500791 if (!priv->comm_is_ok)
792 return;
793
Harvey Harrison441b62c2008-03-03 16:08:34 -0800794 dbg("%s - port %d", __func__, port->number);
795 dbg("%s - interrupt out size is %d", __func__, port->interrupt_out_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
797 spin_lock_irqsave(&priv->lock, flags);
798 if (priv->write_urb_in_use) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800799 dbg("%s - can't write, urb in use", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 spin_unlock_irqrestore(&priv->lock, flags);
801 return;
802 }
803 spin_unlock_irqrestore(&priv->lock, flags);
804
805 /* clear buffer */
806 memset(port->interrupt_out_urb->transfer_buffer, 0, port->interrupt_out_size);
807
808 spin_lock_irqsave(&priv->lock, flags);
Mike Isely3416eaa2008-02-10 20:23:19 -0600809 switch (priv->pkt_fmt) {
810 default:
811 case packet_format_1:
812 /* this is for the CY7C64013... */
813 offset = 2;
814 port->interrupt_out_buffer[0] = priv->line_control;
815 break;
816 case packet_format_2:
817 /* this is for the CY7C63743... */
818 offset = 1;
819 port->interrupt_out_buffer[0] = priv->line_control;
820 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 }
822
823 if (priv->line_control & CONTROL_RESET)
824 priv->line_control &= ~CONTROL_RESET;
825
826 if (priv->cmd_ctrl) {
827 priv->cmd_count++;
Harvey Harrison441b62c2008-03-03 16:08:34 -0800828 dbg("%s - line control command being issued", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 spin_unlock_irqrestore(&priv->lock, flags);
830 goto send;
831 } else
832 spin_unlock_irqrestore(&priv->lock, flags);
833
834 count = cypress_buf_get(priv->buf, &port->interrupt_out_buffer[offset],
835 port->interrupt_out_size-offset);
836
837 if (count == 0) {
838 return;
839 }
840
Mike Isely3416eaa2008-02-10 20:23:19 -0600841 switch (priv->pkt_fmt) {
842 default:
843 case packet_format_1:
844 port->interrupt_out_buffer[1] = count;
845 break;
846 case packet_format_2:
847 port->interrupt_out_buffer[0] |= count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 }
849
Harvey Harrison441b62c2008-03-03 16:08:34 -0800850 dbg("%s - count is %d", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852send:
853 spin_lock_irqsave(&priv->lock, flags);
854 priv->write_urb_in_use = 1;
855 spin_unlock_irqrestore(&priv->lock, flags);
856
857 if (priv->cmd_ctrl)
858 actual_size = 1;
859 else
Mike Isely3416eaa2008-02-10 20:23:19 -0600860 actual_size = count +
861 (priv->pkt_fmt == packet_format_1 ? 2 : 1);
862
Harvey Harrison441b62c2008-03-03 16:08:34 -0800863 usb_serial_debug_data(debug, &port->dev, __func__, port->interrupt_out_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 port->interrupt_out_urb->transfer_buffer);
865
Mike Isely9aa8dae2006-08-29 22:07:04 -0500866 usb_fill_int_urb(port->interrupt_out_urb, port->serial->dev,
867 usb_sndintpipe(port->serial->dev, port->interrupt_out_endpointAddress),
868 port->interrupt_out_buffer, port->interrupt_out_size,
869 cypress_write_int_callback, port, priv->write_urb_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 result = usb_submit_urb (port->interrupt_out_urb, GFP_ATOMIC);
871 if (result) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800872 dev_err(&port->dev, "%s - failed submitting write urb, error %d\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 result);
874 priv->write_urb_in_use = 0;
Mike Isely78aef512006-08-29 22:07:11 -0500875 cypress_set_dead(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 }
877
878 spin_lock_irqsave(&priv->lock, flags);
879 if (priv->cmd_ctrl) {
880 priv->cmd_ctrl = 0;
881 }
882 priv->bytes_out += count; /* do not count the line control and size bytes */
883 spin_unlock_irqrestore(&priv->lock, flags);
884
Pete Zaitcevcf2c7482006-05-22 21:58:49 -0700885 usb_serial_port_softint(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886} /* cypress_send */
887
888
889/* returns how much space is available in the soft buffer */
Alan Cox95da3102008-07-22 11:09:07 +0100890static int cypress_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891{
Alan Cox95da3102008-07-22 11:09:07 +0100892 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 struct cypress_private *priv = usb_get_serial_port_data(port);
894 int room = 0;
895 unsigned long flags;
896
Harvey Harrison441b62c2008-03-03 16:08:34 -0800897 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
899 spin_lock_irqsave(&priv->lock, flags);
900 room = cypress_buf_space_avail(priv->buf);
901 spin_unlock_irqrestore(&priv->lock, flags);
902
Harvey Harrison441b62c2008-03-03 16:08:34 -0800903 dbg("%s - returns %d", __func__, room);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 return room;
905}
906
907
Alan Cox95da3102008-07-22 11:09:07 +0100908static int cypress_tiocmget(struct tty_struct *tty, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909{
Alan Cox95da3102008-07-22 11:09:07 +0100910 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 struct cypress_private *priv = usb_get_serial_port_data(port);
912 __u8 status, control;
913 unsigned int result = 0;
914 unsigned long flags;
915
Harvey Harrison441b62c2008-03-03 16:08:34 -0800916 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
918 spin_lock_irqsave(&priv->lock, flags);
919 control = priv->line_control;
920 status = priv->current_status;
921 spin_unlock_irqrestore(&priv->lock, flags);
922
923 result = ((control & CONTROL_DTR) ? TIOCM_DTR : 0)
924 | ((control & CONTROL_RTS) ? TIOCM_RTS : 0)
925 | ((status & UART_CTS) ? TIOCM_CTS : 0)
926 | ((status & UART_DSR) ? TIOCM_DSR : 0)
927 | ((status & UART_RI) ? TIOCM_RI : 0)
928 | ((status & UART_CD) ? TIOCM_CD : 0);
929
Harvey Harrison441b62c2008-03-03 16:08:34 -0800930 dbg("%s - result = %x", __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
932 return result;
933}
934
935
Alan Cox95da3102008-07-22 11:09:07 +0100936static int cypress_tiocmset(struct tty_struct *tty, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 unsigned int set, unsigned int clear)
938{
Alan Cox95da3102008-07-22 11:09:07 +0100939 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 struct cypress_private *priv = usb_get_serial_port_data(port);
941 unsigned long flags;
942
Harvey Harrison441b62c2008-03-03 16:08:34 -0800943 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
945 spin_lock_irqsave(&priv->lock, flags);
946 if (set & TIOCM_RTS)
947 priv->line_control |= CONTROL_RTS;
948 if (set & TIOCM_DTR)
949 priv->line_control |= CONTROL_DTR;
950 if (clear & TIOCM_RTS)
951 priv->line_control &= ~CONTROL_RTS;
952 if (clear & TIOCM_DTR)
953 priv->line_control &= ~CONTROL_DTR;
Alan Cox8873aaa2008-03-10 21:59:28 +0000954 priv->cmd_ctrl = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 spin_unlock_irqrestore(&priv->lock, flags);
956
Alan Cox95da3102008-07-22 11:09:07 +0100957 return cypress_write(tty, port, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958}
959
960
Alan Cox95da3102008-07-22 11:09:07 +0100961static int cypress_ioctl(struct tty_struct *tty, struct file * file,
962 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963{
Alan Cox95da3102008-07-22 11:09:07 +0100964 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 struct cypress_private *priv = usb_get_serial_port_data(port);
966
Harvey Harrison441b62c2008-03-03 16:08:34 -0800967 dbg("%s - port %d, cmd 0x%.4x", __func__, port->number, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
969 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 /* This code comes from drivers/char/serial.c and ftdi_sio.c */
971 case TIOCMIWAIT:
972 while (priv != NULL) {
973 interruptible_sleep_on(&priv->delta_msr_wait);
974 /* see if a signal did it */
975 if (signal_pending(current))
976 return -ERESTARTSYS;
977 else {
978 char diff = priv->diff_status;
979
980 if (diff == 0) {
981 return -EIO; /* no change => error */
982 }
983
984 /* consume all events */
985 priv->diff_status = 0;
986
987 /* return 0 if caller wanted to know about these bits */
988 if ( ((arg & TIOCM_RNG) && (diff & UART_RI)) ||
989 ((arg & TIOCM_DSR) && (diff & UART_DSR)) ||
990 ((arg & TIOCM_CD) && (diff & UART_CD)) ||
991 ((arg & TIOCM_CTS) && (diff & UART_CTS)) ) {
992 return 0;
993 }
994 /* otherwise caller can't care less about what happened,
995 * and so we continue to wait for more events.
996 */
997 }
998 }
999 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 default:
1001 break;
1002 }
Harvey Harrison441b62c2008-03-03 16:08:34 -08001003 dbg("%s - arg not supported - it was 0x%04x - check include/asm/ioctls.h", __func__, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 return -ENOIOCTLCMD;
1005} /* cypress_ioctl */
1006
1007
Alan Cox95da3102008-07-22 11:09:07 +01001008static void cypress_set_termios(struct tty_struct *tty,
1009 struct usb_serial_port *port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010{
1011 struct cypress_private *priv = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 int data_bits, stop_bits, parity_type, parity_enable;
Alan Cox8873aaa2008-03-10 21:59:28 +00001013 unsigned cflag, iflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 unsigned long flags;
1015 __u8 oldlines;
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001016 int linechange = 0;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001017
Harvey Harrison441b62c2008-03-03 16:08:34 -08001018 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 spin_lock_irqsave(&priv->lock, flags);
1021 if (!priv->termios_initialized) {
1022 if (priv->chiptype == CT_EARTHMATE) {
1023 *(tty->termios) = tty_std_termios;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001024 tty->termios->c_cflag = B4800 | CS8 | CREAD | HUPCL |
1025 CLOCAL;
Alan Cox8873aaa2008-03-10 21:59:28 +00001026 tty->termios->c_ispeed = 4800;
1027 tty->termios->c_ospeed = 4800;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 } else if (priv->chiptype == CT_CYPHIDCOM) {
1029 *(tty->termios) = tty_std_termios;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001030 tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
1031 CLOCAL;
Alan Cox8873aaa2008-03-10 21:59:28 +00001032 tty->termios->c_ispeed = 9600;
1033 tty->termios->c_ospeed = 9600;
Lonnie Mendeza5c44e22006-03-01 10:45:24 -06001034 } else if (priv->chiptype == CT_CA42V2) {
1035 *(tty->termios) = tty_std_termios;
1036 tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
1037 CLOCAL;
Alan Cox8873aaa2008-03-10 21:59:28 +00001038 tty->termios->c_ispeed = 9600;
1039 tty->termios->c_ospeed = 9600;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 }
1041 priv->termios_initialized = 1;
1042 }
1043 spin_unlock_irqrestore(&priv->lock, flags);
1044
Alan Cox8873aaa2008-03-10 21:59:28 +00001045 /* Unsupported features need clearing */
1046 tty->termios->c_cflag &= ~(CMSPAR|CRTSCTS);
1047
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 cflag = tty->termios->c_cflag;
1049 iflag = tty->termios->c_iflag;
1050
1051 /* check if there are new settings */
1052 if (old_termios) {
Alan Cox8873aaa2008-03-10 21:59:28 +00001053 spin_lock_irqsave(&priv->lock, flags);
1054 priv->tmp_termios = *(tty->termios);
1055 spin_unlock_irqrestore(&priv->lock, flags);
1056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
1058 /* set number of data bits, parity, stop bits */
1059 /* when parity is disabled the parity type bit is ignored */
1060
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001061 /* 1 means 2 stop bits, 0 means 1 stop bit */
1062 stop_bits = cflag & CSTOPB ? 1 : 0;
1063
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 if (cflag & PARENB) {
1065 parity_enable = 1;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001066 /* 1 means odd parity, 0 means even parity */
1067 parity_type = cflag & PARODD ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 } else
1069 parity_enable = parity_type = 0;
1070
1071 if (cflag & CSIZE) {
1072 switch (cflag & CSIZE) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001073 case CS5:
1074 data_bits = 0;
1075 break;
1076 case CS6:
1077 data_bits = 1;
1078 break;
1079 case CS7:
1080 data_bits = 2;
1081 break;
1082 case CS8:
1083 data_bits = 3;
1084 break;
1085 default:
1086 err("%s - CSIZE was set, but not CS5-CS8",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001087 __func__);
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001088 data_bits = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 }
1090 } else
1091 data_bits = 3;
1092
1093 spin_lock_irqsave(&priv->lock, flags);
1094 oldlines = priv->line_control;
1095 if ((cflag & CBAUD) == B0) {
1096 /* drop dtr and rts */
Harvey Harrison441b62c2008-03-03 16:08:34 -08001097 dbg("%s - dropping the lines, baud rate 0bps", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
Alan Cox8873aaa2008-03-10 21:59:28 +00001099 } else
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001100 priv->line_control = (CONTROL_DTR | CONTROL_RTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001103 dbg("%s - sending %d stop_bits, %d parity_enable, %d parity_type, "
Harvey Harrison441b62c2008-03-03 16:08:34 -08001104 "%d data_bits (+5)", __func__, stop_bits,
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001105 parity_enable, parity_type, data_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Alan Cox95da3102008-07-22 11:09:07 +01001107 cypress_serial_control(tty, port, tty_get_baud_rate(tty), data_bits, stop_bits,
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001108 parity_enable, parity_type, 0, CYPRESS_SET_CONFIG);
1109
1110 /* we perform a CYPRESS_GET_CONFIG so that the current settings are
1111 * filled into the private structure this should confirm that all is
1112 * working if it returns what we just set */
Alan Cox95da3102008-07-22 11:09:07 +01001113 cypress_serial_control(tty, port, 0, 0, 0, 0, 0, 0, CYPRESS_GET_CONFIG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001115 /* Here we can define custom tty settings for devices; the main tty
1116 * termios flag base comes from empeg.c */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001118 spin_lock_irqsave(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 if ( (priv->chiptype == CT_EARTHMATE) && (priv->baud_rate == 4800) ) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001120 dbg("Using custom termios settings for a baud rate of "
1121 "4800bps.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 /* define custom termios settings for NMEA protocol */
1123
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 tty->termios->c_iflag /* input modes - */
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001125 &= ~(IGNBRK /* disable ignore break */
1126 | BRKINT /* disable break causes interrupt */
1127 | PARMRK /* disable mark parity errors */
1128 | ISTRIP /* disable clear high bit of input char */
1129 | INLCR /* disable translate NL to CR */
1130 | IGNCR /* disable ignore CR */
1131 | ICRNL /* disable translate CR to NL */
1132 | IXON); /* disable enable XON/XOFF flow control */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001134 tty->termios->c_oflag /* output modes */
1135 &= ~OPOST; /* disable postprocess output char */
1136
1137 tty->termios->c_lflag /* line discipline modes */
1138 &= ~(ECHO /* disable echo input characters */
1139 | ECHONL /* disable echo new line */
1140 | ICANON /* disable erase, kill, werase, and rprnt
1141 special characters */
1142 | ISIG /* disable interrupt, quit, and suspend
1143 special characters */
1144 | IEXTEN); /* disable non-POSIX special characters */
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001145 } /* CT_CYPHIDCOM: Application should handle this for device */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 linechange = (priv->line_control != oldlines);
1148 spin_unlock_irqrestore(&priv->lock, flags);
1149
1150 /* if necessary, set lines */
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001151 if (linechange) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 priv->cmd_ctrl = 1;
Alan Cox95da3102008-07-22 11:09:07 +01001153 cypress_write(tty, port, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155} /* cypress_set_termios */
1156
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001157
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158/* returns amount of data still left in soft buffer */
Alan Cox95da3102008-07-22 11:09:07 +01001159static int cypress_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160{
Alan Cox95da3102008-07-22 11:09:07 +01001161 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 struct cypress_private *priv = usb_get_serial_port_data(port);
1163 int chars = 0;
1164 unsigned long flags;
1165
Harvey Harrison441b62c2008-03-03 16:08:34 -08001166 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
1168 spin_lock_irqsave(&priv->lock, flags);
1169 chars = cypress_buf_data_avail(priv->buf);
1170 spin_unlock_irqrestore(&priv->lock, flags);
1171
Harvey Harrison441b62c2008-03-03 16:08:34 -08001172 dbg("%s - returns %d", __func__, chars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 return chars;
1174}
1175
1176
Alan Cox95da3102008-07-22 11:09:07 +01001177static void cypress_throttle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178{
Alan Cox95da3102008-07-22 11:09:07 +01001179 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 struct cypress_private *priv = usb_get_serial_port_data(port);
1181 unsigned long flags;
1182
Harvey Harrison441b62c2008-03-03 16:08:34 -08001183 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
1185 spin_lock_irqsave(&priv->lock, flags);
1186 priv->rx_flags = THROTTLED;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001187 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188}
1189
1190
Alan Cox95da3102008-07-22 11:09:07 +01001191static void cypress_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192{
Alan Cox95da3102008-07-22 11:09:07 +01001193 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 struct cypress_private *priv = usb_get_serial_port_data(port);
1195 int actually_throttled, result;
1196 unsigned long flags;
1197
Harvey Harrison441b62c2008-03-03 16:08:34 -08001198 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
1200 spin_lock_irqsave(&priv->lock, flags);
1201 actually_throttled = priv->rx_flags & ACTUALLY_THROTTLED;
1202 priv->rx_flags = 0;
1203 spin_unlock_irqrestore(&priv->lock, flags);
1204
Mike Isely78aef512006-08-29 22:07:11 -05001205 if (!priv->comm_is_ok)
1206 return;
1207
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 if (actually_throttled) {
1209 port->interrupt_in_urb->dev = port->serial->dev;
1210
1211 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
Mike Isely78aef512006-08-29 22:07:11 -05001212 if (result) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001213 dev_err(&port->dev, "%s - failed submitting read urb, "
Harvey Harrison441b62c2008-03-03 16:08:34 -08001214 "error %d\n", __func__, result);
Mike Isely78aef512006-08-29 22:07:11 -05001215 cypress_set_dead(port);
1216 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 }
1218}
1219
1220
David Howells7d12e782006-10-05 14:55:46 +01001221static void cypress_read_int_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222{
Ming Leicdc97792008-02-24 18:41:47 +08001223 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 struct cypress_private *priv = usb_get_serial_port_data(port);
1225 struct tty_struct *tty;
1226 unsigned char *data = urb->transfer_buffer;
1227 unsigned long flags;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001228 char tty_flag = TTY_NORMAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 int havedata = 0;
1230 int bytes = 0;
1231 int result;
1232 int i = 0;
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001233 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
Harvey Harrison441b62c2008-03-03 16:08:34 -08001235 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001237 switch (status) {
Mike Isely78aef512006-08-29 22:07:11 -05001238 case 0: /* success */
1239 break;
1240 case -ECONNRESET:
1241 case -ENOENT:
1242 case -ESHUTDOWN:
1243 /* precursor to disconnect so just go away */
1244 return;
1245 case -EPIPE:
1246 usb_clear_halt(port->serial->dev,0x81);
1247 break;
1248 default:
1249 /* something ugly is going on... */
1250 dev_err(&urb->dev->dev,"%s - unexpected nonzero read status received: %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001251 __func__, status);
Mike Isely78aef512006-08-29 22:07:11 -05001252 cypress_set_dead(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 return;
1254 }
1255
1256 spin_lock_irqsave(&priv->lock, flags);
1257 if (priv->rx_flags & THROTTLED) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001258 dbg("%s - now throttling", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 priv->rx_flags |= ACTUALLY_THROTTLED;
1260 spin_unlock_irqrestore(&priv->lock, flags);
1261 return;
1262 }
1263 spin_unlock_irqrestore(&priv->lock, flags);
1264
Alan Cox95da3102008-07-22 11:09:07 +01001265 tty = port->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 if (!tty) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001267 dbg("%s - bad tty pointer - exiting", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 return;
1269 }
1270
1271 spin_lock_irqsave(&priv->lock, flags);
Mike Isely3416eaa2008-02-10 20:23:19 -06001272 result = urb->actual_length;
1273 switch (priv->pkt_fmt) {
1274 default:
1275 case packet_format_1:
1276 /* This is for the CY7C64013... */
1277 priv->current_status = data[0] & 0xF8;
1278 bytes = data[1] + 2;
1279 i = 2;
1280 if (bytes > 2)
1281 havedata = 1;
1282 break;
1283 case packet_format_2:
1284 /* This is for the CY7C63743... */
1285 priv->current_status = data[0] & 0xF8;
1286 bytes = (data[0] & 0x07) + 1;
1287 i = 1;
1288 if (bytes > 1)
1289 havedata = 1;
1290 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 }
1292 spin_unlock_irqrestore(&priv->lock, flags);
Mike Isely3416eaa2008-02-10 20:23:19 -06001293 if (result < bytes) {
1294 dbg("%s - wrong packet size - received %d bytes but packet "
1295 "said %d bytes", __func__, result, bytes);
1296 goto continue_read;
1297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
Harvey Harrison441b62c2008-03-03 16:08:34 -08001299 usb_serial_debug_data (debug, &port->dev, __func__,
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001300 urb->actual_length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
1302 spin_lock_irqsave(&priv->lock, flags);
1303 /* check to see if status has changed */
Mike Isely67683062008-02-10 20:23:28 -06001304 if (priv->current_status != priv->prev_status) {
1305 priv->diff_status |= priv->current_status ^
1306 priv->prev_status;
1307 wake_up_interruptible(&priv->delta_msr_wait);
1308 priv->prev_status = priv->current_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 }
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001310 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001312 /* hangup, as defined in acm.c... this might be a bad place for it
1313 * though */
1314 if (tty && !(tty->termios->c_cflag & CLOCAL) &&
1315 !(priv->current_status & UART_CD)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001316 dbg("%s - calling hangup", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 tty_hangup(tty);
1318 goto continue_read;
1319 }
1320
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001321 /* There is one error bit... I'm assuming it is a parity error
1322 * indicator as the generic firmware will set this bit to 1 if a
1323 * parity error occurs.
1324 * I can not find reference to any other error events. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 spin_lock_irqsave(&priv->lock, flags);
1326 if (priv->current_status & CYP_ERROR) {
1327 spin_unlock_irqrestore(&priv->lock, flags);
1328 tty_flag = TTY_PARITY;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001329 dbg("%s - Parity Error detected", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 } else
1331 spin_unlock_irqrestore(&priv->lock, flags);
1332
1333 /* process read if there is data other than line status */
1334 if (tty && (bytes > i)) {
Alan Cox33f0f882006-01-09 20:54:13 -08001335 bytes = tty_buffer_request_room(tty, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 for (; i < bytes ; ++i) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001337 dbg("pushing byte number %d - %d - %c", i, data[i],
1338 data[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 tty_insert_flip_char(tty, data[i], tty_flag);
1340 }
Alan Cox95da3102008-07-22 11:09:07 +01001341 tty_flip_buffer_push(port->port.tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 }
1343
1344 spin_lock_irqsave(&priv->lock, flags);
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001345 /* control and status byte(s) are also counted */
1346 priv->bytes_in += bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 spin_unlock_irqrestore(&priv->lock, flags);
1348
1349continue_read:
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001350
1351 /* Continue trying to always read... unless the port has closed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
Alan Cox95da3102008-07-22 11:09:07 +01001353 if (port->port.count > 0 && priv->comm_is_ok) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001354 usb_fill_int_urb(port->interrupt_in_urb, port->serial->dev,
1355 usb_rcvintpipe(port->serial->dev,
1356 port->interrupt_in_endpointAddress),
1357 port->interrupt_in_urb->transfer_buffer,
1358 port->interrupt_in_urb->transfer_buffer_length,
Mike Isely0257fa92006-08-29 22:06:59 -05001359 cypress_read_int_callback, port, priv->read_urb_interval);
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001360 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
Mike Isely78aef512006-08-29 22:07:11 -05001361 if (result) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001362 dev_err(&urb->dev->dev, "%s - failed resubmitting "
Harvey Harrison441b62c2008-03-03 16:08:34 -08001363 "read urb, error %d\n", __func__,
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001364 result);
Mike Isely78aef512006-08-29 22:07:11 -05001365 cypress_set_dead(port);
1366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 }
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001368
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 return;
1370} /* cypress_read_int_callback */
1371
1372
David Howells7d12e782006-10-05 14:55:46 +01001373static void cypress_write_int_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374{
Ming Leicdc97792008-02-24 18:41:47 +08001375 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 struct cypress_private *priv = usb_get_serial_port_data(port);
1377 int result;
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001378 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
Harvey Harrison441b62c2008-03-03 16:08:34 -08001380 dbg("%s - port %d", __func__, port->number);
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001381
1382 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 case 0:
1384 /* success */
1385 break;
1386 case -ECONNRESET:
1387 case -ENOENT:
1388 case -ESHUTDOWN:
1389 /* this urb is terminated, clean up */
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001390 dbg("%s - urb shutting down with status: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001391 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 priv->write_urb_in_use = 0;
1393 return;
Mike Isely78aef512006-08-29 22:07:11 -05001394 case -EPIPE: /* no break needed; clear halt and resubmit */
1395 if (!priv->comm_is_ok)
1396 break;
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001397 usb_clear_halt(port->serial->dev, 0x02);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 /* error in the urb, so we have to resubmit it */
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001399 dbg("%s - nonzero write bulk status received: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001400 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 port->interrupt_out_urb->transfer_buffer_length = 1;
1402 port->interrupt_out_urb->dev = port->serial->dev;
1403 result = usb_submit_urb(port->interrupt_out_urb, GFP_ATOMIC);
Mike Isely78aef512006-08-29 22:07:11 -05001404 if (!result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 return;
Mike Isely78aef512006-08-29 22:07:11 -05001406 dev_err(&urb->dev->dev, "%s - failed resubmitting write urb, error %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001407 __func__, result);
Mike Isely78aef512006-08-29 22:07:11 -05001408 cypress_set_dead(port);
1409 break;
1410 default:
1411 dev_err(&urb->dev->dev,"%s - unexpected nonzero write status received: %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001412 __func__, status);
Mike Isely78aef512006-08-29 22:07:11 -05001413 cypress_set_dead(port);
1414 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 }
1416
1417 priv->write_urb_in_use = 0;
1418
1419 /* send any buffered data */
1420 cypress_send(port);
1421}
1422
1423
1424/*****************************************************************************
1425 * Write buffer functions - buffering code from pl2303 used
1426 *****************************************************************************/
1427
1428/*
1429 * cypress_buf_alloc
1430 *
1431 * Allocate a circular buffer and all associated memory.
1432 */
1433
1434static struct cypress_buf *cypress_buf_alloc(unsigned int size)
1435{
1436
1437 struct cypress_buf *cb;
1438
1439
1440 if (size == 0)
1441 return NULL;
1442
Robert P. J. Day5cbded52006-12-13 00:35:56 -08001443 cb = kmalloc(sizeof(struct cypress_buf), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 if (cb == NULL)
1445 return NULL;
1446
1447 cb->buf_buf = kmalloc(size, GFP_KERNEL);
1448 if (cb->buf_buf == NULL) {
1449 kfree(cb);
1450 return NULL;
1451 }
1452
1453 cb->buf_size = size;
1454 cb->buf_get = cb->buf_put = cb->buf_buf;
1455
1456 return cb;
1457
1458}
1459
1460
1461/*
1462 * cypress_buf_free
1463 *
1464 * Free the buffer and all associated memory.
1465 */
1466
1467static void cypress_buf_free(struct cypress_buf *cb)
1468{
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001469 if (cb) {
1470 kfree(cb->buf_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 kfree(cb);
1472 }
1473}
1474
1475
1476/*
1477 * cypress_buf_clear
1478 *
1479 * Clear out all data in the circular buffer.
1480 */
1481
1482static void cypress_buf_clear(struct cypress_buf *cb)
1483{
1484 if (cb != NULL)
1485 cb->buf_get = cb->buf_put;
1486 /* equivalent to a get of all data available */
1487}
1488
1489
1490/*
1491 * cypress_buf_data_avail
1492 *
1493 * Return the number of bytes of data available in the circular
1494 * buffer.
1495 */
1496
1497static unsigned int cypress_buf_data_avail(struct cypress_buf *cb)
1498{
1499 if (cb != NULL)
1500 return ((cb->buf_size + cb->buf_put - cb->buf_get) % cb->buf_size);
1501 else
1502 return 0;
1503}
1504
1505
1506/*
1507 * cypress_buf_space_avail
1508 *
1509 * Return the number of bytes of space available in the circular
1510 * buffer.
1511 */
1512
1513static unsigned int cypress_buf_space_avail(struct cypress_buf *cb)
1514{
1515 if (cb != NULL)
1516 return ((cb->buf_size + cb->buf_get - cb->buf_put - 1) % cb->buf_size);
1517 else
1518 return 0;
1519}
1520
1521
1522/*
1523 * cypress_buf_put
1524 *
1525 * Copy data data from a user buffer and put it into the circular buffer.
1526 * Restrict to the amount of space available.
1527 *
1528 * Return the number of bytes copied.
1529 */
1530
1531static unsigned int cypress_buf_put(struct cypress_buf *cb, const char *buf,
1532 unsigned int count)
1533{
1534
1535 unsigned int len;
1536
1537
1538 if (cb == NULL)
1539 return 0;
1540
1541 len = cypress_buf_space_avail(cb);
1542 if (count > len)
1543 count = len;
1544
1545 if (count == 0)
1546 return 0;
1547
1548 len = cb->buf_buf + cb->buf_size - cb->buf_put;
1549 if (count > len) {
1550 memcpy(cb->buf_put, buf, len);
1551 memcpy(cb->buf_buf, buf+len, count - len);
1552 cb->buf_put = cb->buf_buf + count - len;
1553 } else {
1554 memcpy(cb->buf_put, buf, count);
1555 if (count < len)
1556 cb->buf_put += count;
1557 else /* count == len */
1558 cb->buf_put = cb->buf_buf;
1559 }
1560
1561 return count;
1562
1563}
1564
1565
1566/*
1567 * cypress_buf_get
1568 *
1569 * Get data from the circular buffer and copy to the given buffer.
1570 * Restrict to the amount of data available.
1571 *
1572 * Return the number of bytes copied.
1573 */
1574
1575static unsigned int cypress_buf_get(struct cypress_buf *cb, char *buf,
1576 unsigned int count)
1577{
1578
1579 unsigned int len;
1580
1581
1582 if (cb == NULL)
1583 return 0;
1584
1585 len = cypress_buf_data_avail(cb);
1586 if (count > len)
1587 count = len;
1588
1589 if (count == 0)
1590 return 0;
1591
1592 len = cb->buf_buf + cb->buf_size - cb->buf_get;
1593 if (count > len) {
1594 memcpy(buf, cb->buf_get, len);
1595 memcpy(buf+len, cb->buf_buf, count - len);
1596 cb->buf_get = cb->buf_buf + count - len;
1597 } else {
1598 memcpy(buf, cb->buf_get, count);
1599 if (count < len)
1600 cb->buf_get += count;
1601 else /* count == len */
1602 cb->buf_get = cb->buf_buf;
1603 }
1604
1605 return count;
1606
1607}
1608
1609/*****************************************************************************
1610 * Module functions
1611 *****************************************************************************/
1612
1613static int __init cypress_init(void)
1614{
1615 int retval;
1616
Harvey Harrison441b62c2008-03-03 16:08:34 -08001617 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618
1619 retval = usb_serial_register(&cypress_earthmate_device);
1620 if (retval)
1621 goto failed_em_register;
1622 retval = usb_serial_register(&cypress_hidcom_device);
1623 if (retval)
1624 goto failed_hidcom_register;
Lonnie Mendeza5c44e22006-03-01 10:45:24 -06001625 retval = usb_serial_register(&cypress_ca42v2_device);
1626 if (retval)
1627 goto failed_ca42v2_register;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 retval = usb_register(&cypress_driver);
1629 if (retval)
1630 goto failed_usb_register;
1631
1632 info(DRIVER_DESC " " DRIVER_VERSION);
1633 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
Mariusz Kozlowski2e46b742006-11-17 17:49:22 +01001635failed_usb_register:
1636 usb_serial_deregister(&cypress_ca42v2_device);
1637failed_ca42v2_register:
1638 usb_serial_deregister(&cypress_hidcom_device);
1639failed_hidcom_register:
1640 usb_serial_deregister(&cypress_earthmate_device);
1641failed_em_register:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 return retval;
1643}
1644
1645
1646static void __exit cypress_exit (void)
1647{
Harvey Harrison441b62c2008-03-03 16:08:34 -08001648 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
1650 usb_deregister (&cypress_driver);
1651 usb_serial_deregister (&cypress_earthmate_device);
1652 usb_serial_deregister (&cypress_hidcom_device);
Lonnie Mendeza5c44e22006-03-01 10:45:24 -06001653 usb_serial_deregister (&cypress_ca42v2_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654}
1655
1656
1657module_init(cypress_init);
1658module_exit(cypress_exit);
1659
1660MODULE_AUTHOR( DRIVER_AUTHOR );
1661MODULE_DESCRIPTION( DRIVER_DESC );
1662MODULE_VERSION( DRIVER_VERSION );
1663MODULE_LICENSE("GPL");
1664
1665module_param(debug, bool, S_IRUGO | S_IWUSR);
1666MODULE_PARM_DESC(debug, "Debug enabled or not");
1667module_param(stats, bool, S_IRUGO | S_IWUSR);
1668MODULE_PARM_DESC(stats, "Enable statistics or not");
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001669module_param(interval, int, S_IRUGO | S_IWUSR);
1670MODULE_PARM_DESC(interval, "Overrides interrupt interval");