blob: 669f93848539560182b589131cc18788487477dc [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * USB Cypress M8 driver
3 *
4 * Copyright (C) 2004
Alan Cox813a2242008-07-22 11:10:36 +01005 * Lonnie Mendez (dignome@gmail.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * 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 *
Alan Cox813a2242008-07-22 11:10:36 +010014 * See Documentation/usb/usb-serial.txt for more information on using this
15 * driver
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 *
17 * See http://geocities.com/i0xox0i for information on this driver and the
18 * earthmate usb device.
19 *
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -050020 * Lonnie Mendez <dignome@gmail.com>
21 * 4-29-2005
Alan Cox813a2242008-07-22 11:10:36 +010022 * Fixed problem where setting or retreiving the serial config would fail
23 * with EPIPE. Removed CRTS toggling so the driver behaves more like
24 * other usbserial adapters. Issued new interval of 1ms instead of the
25 * default 10ms. As a result, transfer speed has been substantially
26 * increased from avg. 850bps to avg. 3300bps. initial termios has also
27 * been modified. Cleaned up code and formatting issues so it is more
28 * readable. Replaced the C++ style comments.
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 *
30 * Lonnie Mendez <dignome@gmail.com>
31 * 12-15-2004
32 * Incorporated write buffering from pl2303 driver. Fixed bug with line
33 * handling so both lines are raised in cypress_open. (was dropping rts)
34 * Various code cleanups made as well along with other misc bug fixes.
35 *
36 * Lonnie Mendez <dignome@gmail.com>
37 * 04-10-2004
38 * Driver modified to support dynamic line settings. Various improvments
39 * and features.
40 *
41 * Neil Whelchel
42 * 10-2003
43 * Driver first released.
44 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 */
46
Alan Cox813a2242008-07-22 11:10:36 +010047/* Thanks to Neil Whelchel for writing the first cypress m8 implementation
48 for linux. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070049/* Thanks to cypress for providing references for the hid reports. */
50/* Thanks to Jiang Zhang for providing links and for general help. */
Alan Cox813a2242008-07-22 11:10:36 +010051/* Code originates and was built up from ftdi_sio, belkin, pl2303 and others.*/
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <linux/kernel.h>
55#include <linux/errno.h>
56#include <linux/init.h>
57#include <linux/slab.h>
58#include <linux/tty.h>
59#include <linux/tty_driver.h>
60#include <linux/tty_flip.h>
61#include <linux/module.h>
62#include <linux/moduleparam.h>
63#include <linux/spinlock.h>
64#include <linux/usb.h>
Greg Kroah-Hartmana9698882006-07-11 21:22:58 -070065#include <linux/usb/serial.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#include <linux/serial.h>
67#include <linux/delay.h>
Alan Cox813a2242008-07-22 11:10:36 +010068#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#include "cypress_m8.h"
71
72
73#ifdef CONFIG_USB_SERIAL_DEBUG
74 static int debug = 1;
75#else
76 static int debug;
77#endif
78static int stats;
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -050079static int interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81/*
82 * Version Information
83 */
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -050084#define DRIVER_VERSION "v1.09"
Linus Torvalds1da177e2005-04-16 15:20:36 -070085#define DRIVER_AUTHOR "Lonnie Mendez <dignome@gmail.com>, Neil Whelchel <koyama@firstlight.net>"
86#define DRIVER_DESC "Cypress USB to Serial Driver"
87
88/* write buffer size defines */
89#define CYPRESS_BUF_SIZE 1024
90#define CYPRESS_CLOSING_WAIT (30*HZ)
91
92static struct usb_device_id id_table_earthmate [] = {
93 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB) },
Lonnie Mendez25b6f082005-05-10 17:09:52 -050094 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB_LT20) },
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 { } /* Terminating entry */
96};
97
98static struct usb_device_id id_table_cyphidcomrs232 [] = {
99 { USB_DEVICE(VENDOR_ID_CYPRESS, PRODUCT_ID_CYPHIDCOM) },
Dmitry Shapin6f6f06e2008-03-04 15:25:10 -0800100 { USB_DEVICE(VENDOR_ID_POWERCOM, PRODUCT_ID_UPS) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 { } /* Terminating entry */
102};
103
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600104static struct usb_device_id id_table_nokiaca42v2 [] = {
105 { USB_DEVICE(VENDOR_ID_DAZZLE, PRODUCT_ID_CA42) },
106 { } /* Terminating entry */
107};
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109static struct usb_device_id id_table_combined [] = {
110 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB) },
Lonnie Mendez25b6f082005-05-10 17:09:52 -0500111 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB_LT20) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 { USB_DEVICE(VENDOR_ID_CYPRESS, PRODUCT_ID_CYPHIDCOM) },
Dmitry Shapin6f6f06e2008-03-04 15:25:10 -0800113 { USB_DEVICE(VENDOR_ID_POWERCOM, PRODUCT_ID_UPS) },
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600114 { USB_DEVICE(VENDOR_ID_DAZZLE, PRODUCT_ID_CA42) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 { } /* Terminating entry */
116};
117
Alan Cox813a2242008-07-22 11:10:36 +0100118MODULE_DEVICE_TABLE(usb, id_table_combined);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120static struct usb_driver cypress_driver = {
121 .name = "cypress",
122 .probe = usb_serial_probe,
123 .disconnect = usb_serial_disconnect,
124 .id_table = id_table_combined,
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800125 .no_dynamic_id = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126};
127
Mike Isely3416eaa2008-02-10 20:23:19 -0600128enum packet_format {
129 packet_format_1, /* b0:status, b1:payload count */
130 packet_format_2 /* b0[7:3]:status, b0[2:0]:payload count */
131};
132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133struct cypress_private {
134 spinlock_t lock; /* private lock */
135 int chiptype; /* identifier of device, for quirks/etc */
136 int bytes_in; /* used for statistics */
137 int bytes_out; /* used for statistics */
138 int cmd_count; /* used for statistics */
139 int cmd_ctrl; /* always set this to 1 before issuing a command */
140 struct cypress_buf *buf; /* write buffer */
141 int write_urb_in_use; /* write urb in use indicator */
Mike Isely0257fa92006-08-29 22:06:59 -0500142 int write_urb_interval; /* interval to use for write urb */
143 int read_urb_interval; /* interval to use for read urb */
Mike Isely78aef512006-08-29 22:07:11 -0500144 int comm_is_ok; /* true if communication is (still) ok */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 int termios_initialized;
146 __u8 line_control; /* holds dtr / rts value */
147 __u8 current_status; /* received from last read - info on dsr,cts,cd,ri,etc */
148 __u8 current_config; /* stores the current configuration byte */
149 __u8 rx_flags; /* throttling - used from whiteheat/ftdi_sio */
Mike Isely3416eaa2008-02-10 20:23:19 -0600150 enum packet_format pkt_fmt; /* format to use for packet send / receive */
Mike Isely3d6aa322008-02-10 20:23:24 -0600151 int get_cfg_unsafe; /* If true, the CYPRESS_GET_CONFIG is unsafe */
Alan Cox813a2242008-07-22 11:10:36 +0100152 int baud_rate; /* stores current baud rate in
153 integer form */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 int isthrottled; /* if throttled, discard reads */
155 wait_queue_head_t delta_msr_wait; /* used for TIOCMIWAIT */
156 char prev_status, diff_status; /* used for TIOCMIWAIT */
Alan Cox813a2242008-07-22 11:10:36 +0100157 /* we pass a pointer to this as the arguement sent to
158 cypress_set_termios old_termios */
Alan Cox606d0992006-12-08 02:38:45 -0800159 struct ktermios tmp_termios; /* stores the old termios settings */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160};
161
162/* write buffer structure */
163struct cypress_buf {
164 unsigned int buf_size;
165 char *buf_buf;
166 char *buf_get;
167 char *buf_put;
168};
169
170/* function prototypes for the Cypress USB to serial device */
Alan Cox813a2242008-07-22 11:10:36 +0100171static int cypress_earthmate_startup(struct usb_serial *serial);
172static int cypress_hidcom_startup(struct usb_serial *serial);
173static int cypress_ca42v2_startup(struct usb_serial *serial);
174static void cypress_shutdown(struct usb_serial *serial);
175static int cypress_open(struct tty_struct *tty,
176 struct usb_serial_port *port, struct file *filp);
Alan Cox335f8512009-06-11 12:26:29 +0100177static void cypress_close(struct usb_serial_port *port);
178static void cypress_dtr_rts(struct usb_serial_port *port, int on);
Alan Cox813a2242008-07-22 11:10:36 +0100179static int cypress_write(struct tty_struct *tty, struct usb_serial_port *port,
180 const unsigned char *buf, int count);
181static void cypress_send(struct usb_serial_port *port);
182static int cypress_write_room(struct tty_struct *tty);
183static int cypress_ioctl(struct tty_struct *tty, struct file *file,
184 unsigned int cmd, unsigned long arg);
185static void cypress_set_termios(struct tty_struct *tty,
186 struct usb_serial_port *port, struct ktermios *old);
187static int cypress_tiocmget(struct tty_struct *tty, struct file *file);
188static int cypress_tiocmset(struct tty_struct *tty, struct file *file,
189 unsigned int set, unsigned int clear);
190static int cypress_chars_in_buffer(struct tty_struct *tty);
191static void cypress_throttle(struct tty_struct *tty);
192static void cypress_unthrottle(struct tty_struct *tty);
193static void cypress_set_dead(struct usb_serial_port *port);
194static void cypress_read_int_callback(struct urb *urb);
195static void cypress_write_int_callback(struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196/* write buffer functions */
197static struct cypress_buf *cypress_buf_alloc(unsigned int size);
Alan Cox813a2242008-07-22 11:10:36 +0100198static void cypress_buf_free(struct cypress_buf *cb);
199static void cypress_buf_clear(struct cypress_buf *cb);
200static unsigned int cypress_buf_data_avail(struct cypress_buf *cb);
201static unsigned int cypress_buf_space_avail(struct cypress_buf *cb);
202static unsigned int cypress_buf_put(struct cypress_buf *cb,
203 const char *buf, unsigned int count);
204static unsigned int cypress_buf_get(struct cypress_buf *cb,
205 char *buf, unsigned int count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700208static struct usb_serial_driver cypress_earthmate_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700209 .driver = {
210 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700211 .name = "earthmate",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700212 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700213 .description = "DeLorme Earthmate USB",
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100214 .usb_driver = &cypress_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 .id_table = id_table_earthmate,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 .num_ports = 1,
217 .attach = cypress_earthmate_startup,
218 .shutdown = cypress_shutdown,
219 .open = cypress_open,
220 .close = cypress_close,
Alan Cox335f8512009-06-11 12:26:29 +0100221 .dtr_rts = cypress_dtr_rts,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 .write = cypress_write,
223 .write_room = cypress_write_room,
224 .ioctl = cypress_ioctl,
225 .set_termios = cypress_set_termios,
226 .tiocmget = cypress_tiocmget,
227 .tiocmset = cypress_tiocmset,
228 .chars_in_buffer = cypress_chars_in_buffer,
229 .throttle = cypress_throttle,
230 .unthrottle = cypress_unthrottle,
231 .read_int_callback = cypress_read_int_callback,
232 .write_int_callback = cypress_write_int_callback,
233};
234
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700235static struct usb_serial_driver cypress_hidcom_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700236 .driver = {
237 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700238 .name = "cyphidcom",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700239 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700240 .description = "HID->COM RS232 Adapter",
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100241 .usb_driver = &cypress_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 .id_table = id_table_cyphidcomrs232,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 .num_ports = 1,
244 .attach = cypress_hidcom_startup,
245 .shutdown = cypress_shutdown,
246 .open = cypress_open,
247 .close = cypress_close,
Alan Cox335f8512009-06-11 12:26:29 +0100248 .dtr_rts = cypress_dtr_rts,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 .write = cypress_write,
250 .write_room = cypress_write_room,
251 .ioctl = cypress_ioctl,
252 .set_termios = cypress_set_termios,
253 .tiocmget = cypress_tiocmget,
254 .tiocmset = cypress_tiocmset,
255 .chars_in_buffer = cypress_chars_in_buffer,
256 .throttle = cypress_throttle,
257 .unthrottle = cypress_unthrottle,
258 .read_int_callback = cypress_read_int_callback,
259 .write_int_callback = cypress_write_int_callback,
260};
261
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600262static struct usb_serial_driver cypress_ca42v2_device = {
263 .driver = {
264 .owner = THIS_MODULE,
Alan Cox813a2242008-07-22 11:10:36 +0100265 .name = "nokiaca42v2",
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600266 },
267 .description = "Nokia CA-42 V2 Adapter",
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100268 .usb_driver = &cypress_driver,
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600269 .id_table = id_table_nokiaca42v2,
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600270 .num_ports = 1,
271 .attach = cypress_ca42v2_startup,
272 .shutdown = cypress_shutdown,
273 .open = cypress_open,
274 .close = cypress_close,
Alan Cox335f8512009-06-11 12:26:29 +0100275 .dtr_rts = cypress_dtr_rts,
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600276 .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
Alan Cox8873aaa2008-03-10 21:59:28 +0000294static int analyze_baud_rate(struct usb_serial_port *port, speed_t new_rate)
Mike Isely92983c22008-02-10 20:23:32 -0600295{
Mike Isely92983c22008-02-10 20:23:32 -0600296 struct cypress_private *priv;
297 priv = usb_get_serial_port_data(port);
298
299 /*
300 * The general purpose firmware for the Cypress M8 allows for
301 * a maximum speed of 57600bps (I have no idea whether DeLorme
302 * chose to use the general purpose firmware or not), if you
303 * need to modify this speed setting for your own project
304 * please add your own chiptype and modify the code likewise.
305 * The Cypress HID->COM device will work successfully up to
306 * 115200bps (but the actual throughput is around 3kBps).
307 */
Mike Isely92983c22008-02-10 20:23:32 -0600308 if (port->serial->dev->speed == USB_SPEED_LOW) {
309 /*
310 * Mike Isely <isely@pobox.com> 2-Feb-2008: The
311 * Cypress app note that describes this mechanism
312 * states the the low-speed part can't handle more
313 * than 800 bytes/sec, in which case 4800 baud is the
314 * safest speed for a part like that.
315 */
316 if (new_rate > 4800) {
317 dbg("%s - failed setting baud rate, device incapable "
318 "speed %d", __func__, new_rate);
319 return -1;
320 }
321 }
322 switch (priv->chiptype) {
323 case CT_EARTHMATE:
324 if (new_rate <= 600) {
325 /* 300 and 600 baud rates are supported under
326 * the generic firmware, but are not used with
327 * NMEA and SiRF protocols */
328 dbg("%s - failed setting baud rate, unsupported speed "
329 "of %d on Earthmate GPS", __func__, new_rate);
330 return -1;
331 }
332 break;
333 default:
334 break;
335 }
336 return new_rate;
337}
338
339
Steven Cole093cf722005-05-03 19:07:24 -0600340/* This function can either set or retrieve the current serial line settings */
Alan Cox813a2242008-07-22 11:10:36 +0100341static int cypress_serial_control(struct tty_struct *tty,
Alan Cox95da3102008-07-22 11:09:07 +0100342 struct usb_serial_port *port, speed_t baud_rate, int data_bits,
343 int stop_bits, int parity_enable, int parity_type, int reset,
344 int cypress_request_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500346 int new_baudrate = 0, retval = 0, tries = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 struct cypress_private *priv;
Mike Isely93075542008-02-10 20:23:14 -0600348 __u8 feature_buffer[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 unsigned long flags;
350
Harvey Harrison441b62c2008-03-03 16:08:34 -0800351 dbg("%s", __func__);
Alan Cox813a2242008-07-22 11:10:36 +0100352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 priv = usb_get_serial_port_data(port);
354
Mike Isely78aef512006-08-29 22:07:11 -0500355 if (!priv->comm_is_ok)
356 return -ENODEV;
357
Alan Cox813a2242008-07-22 11:10:36 +0100358 switch (cypress_request_type) {
359 case CYPRESS_SET_CONFIG:
360 new_baudrate = priv->baud_rate;
361 /* 0 means 'Hang up' so doesn't change the true bit rate */
362 if (baud_rate == 0)
Mike Isely92983c22008-02-10 20:23:32 -0600363 new_baudrate = priv->baud_rate;
Alan Cox813a2242008-07-22 11:10:36 +0100364 /* Change of speed ? */
365 else if (baud_rate != priv->baud_rate) {
366 dbg("%s - baud rate is changing", __func__);
367 retval = analyze_baud_rate(port, baud_rate);
368 if (retval >= 0) {
369 new_baudrate = retval;
370 dbg("%s - New baud rate set to %d",
371 __func__, new_baudrate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 }
Alan Cox813a2242008-07-22 11:10:36 +0100373 }
374 dbg("%s - baud rate is being sent as %d",
375 __func__, new_baudrate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Alan Cox813a2242008-07-22 11:10:36 +0100377 memset(feature_buffer, 0, sizeof(feature_buffer));
378 /* fill the feature_buffer with new configuration */
379 *((u_int32_t *)feature_buffer) = new_baudrate;
380 feature_buffer[4] |= data_bits; /* assign data bits in 2 bit space ( max 3 ) */
381 /* 1 bit gap */
382 feature_buffer[4] |= (stop_bits << 3); /* assign stop bits in 1 bit space */
383 feature_buffer[4] |= (parity_enable << 4); /* assign parity flag in 1 bit space */
384 feature_buffer[4] |= (parity_type << 5); /* assign parity type in 1 bit space */
385 /* 1 bit gap */
386 feature_buffer[4] |= (reset << 7); /* assign reset at end of byte, 1 bit space */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Alan Cox813a2242008-07-22 11:10:36 +0100388 dbg("%s - device is being sent this feature report:",
389 __func__);
390 dbg("%s - %02X - %02X - %02X - %02X - %02X", __func__,
391 feature_buffer[0], feature_buffer[1],
392 feature_buffer[2], feature_buffer[3],
393 feature_buffer[4]);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500394
Alan Cox813a2242008-07-22 11:10:36 +0100395 do {
396 retval = usb_control_msg(port->serial->dev,
397 usb_sndctrlpipe(port->serial->dev, 0),
398 HID_REQ_SET_REPORT,
399 USB_DIR_OUT | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
400 0x0300, 0, feature_buffer,
401 sizeof(feature_buffer), 500);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500402
Alan Cox813a2242008-07-22 11:10:36 +0100403 if (tries++ >= 3)
404 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Alan Cox813a2242008-07-22 11:10:36 +0100406 } while (retval != sizeof(feature_buffer) &&
407 retval != -ENODEV);
Mike Isely93075542008-02-10 20:23:14 -0600408
Alan Cox813a2242008-07-22 11:10:36 +0100409 if (retval != sizeof(feature_buffer)) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700410 dev_err(&port->dev, "%s - failed sending serial "
411 "line settings - %d\n", __func__, retval);
Alan Cox813a2242008-07-22 11:10:36 +0100412 cypress_set_dead(port);
413 } else {
414 spin_lock_irqsave(&priv->lock, flags);
415 priv->baud_rate = new_baudrate;
416 priv->current_config = feature_buffer[4];
417 spin_unlock_irqrestore(&priv->lock, flags);
418 /* If we asked for a speed change encode it */
419 if (baud_rate)
420 tty_encode_baud_rate(tty,
421 new_baudrate, new_baudrate);
422 }
423 break;
424 case CYPRESS_GET_CONFIG:
425 if (priv->get_cfg_unsafe) {
426 /* Not implemented for this device,
427 and if we try to do it we're likely
428 to crash the hardware. */
429 return -ENOTTY;
430 }
431 dbg("%s - retreiving serial line settings", __func__);
432 /* set initial values in feature buffer */
433 memset(feature_buffer, 0, sizeof(feature_buffer));
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500434
Alan Cox813a2242008-07-22 11:10:36 +0100435 do {
436 retval = usb_control_msg(port->serial->dev,
437 usb_rcvctrlpipe(port->serial->dev, 0),
438 HID_REQ_GET_REPORT,
439 USB_DIR_IN | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
440 0x0300, 0, feature_buffer,
441 sizeof(feature_buffer), 500);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500442
Alan Cox813a2242008-07-22 11:10:36 +0100443 if (tries++ >= 3)
444 break;
445 } while (retval != sizeof(feature_buffer)
446 && retval != -ENODEV);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500447
Alan Cox813a2242008-07-22 11:10:36 +0100448 if (retval != sizeof(feature_buffer)) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700449 dev_err(&port->dev, "%s - failed to retrieve serial "
450 "line settings - %d\n", __func__, retval);
Alan Cox813a2242008-07-22 11:10:36 +0100451 cypress_set_dead(port);
452 return retval;
453 } else {
454 spin_lock_irqsave(&priv->lock, flags);
455 /* store the config in one byte, and later
456 use bit masks to check values */
457 priv->current_config = feature_buffer[4];
458 priv->baud_rate = *((u_int32_t *)feature_buffer);
459 spin_unlock_irqrestore(&priv->lock, flags);
460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 }
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500462 spin_lock_irqsave(&priv->lock, flags);
463 ++priv->cmd_count;
464 spin_unlock_irqrestore(&priv->lock, flags);
465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 return retval;
467} /* cypress_serial_control */
468
469
Mike Isely78aef512006-08-29 22:07:11 -0500470static void cypress_set_dead(struct usb_serial_port *port)
471{
472 struct cypress_private *priv = usb_get_serial_port_data(port);
473 unsigned long flags;
474
475 spin_lock_irqsave(&priv->lock, flags);
476 if (!priv->comm_is_ok) {
477 spin_unlock_irqrestore(&priv->lock, flags);
478 return;
479 }
480 priv->comm_is_ok = 0;
481 spin_unlock_irqrestore(&priv->lock, flags);
482
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700483 dev_err(&port->dev, "cypress_m8 suspending failing port %d - "
484 "interval might be too short\n", port->number);
Mike Isely78aef512006-08-29 22:07:11 -0500485}
486
487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488/*****************************************************************************
489 * Cypress serial driver functions
490 *****************************************************************************/
491
492
Alan Cox813a2242008-07-22 11:10:36 +0100493static int generic_startup(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
495 struct cypress_private *priv;
Mike Isely0257fa92006-08-29 22:06:59 -0500496 struct usb_serial_port *port = serial->port[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Harvey Harrison441b62c2008-03-03 16:08:34 -0800498 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Alan Cox813a2242008-07-22 11:10:36 +0100500 priv = kzalloc(sizeof(struct cypress_private), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 if (!priv)
502 return -ENOMEM;
503
Mike Isely78aef512006-08-29 22:07:11 -0500504 priv->comm_is_ok = !0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 spin_lock_init(&priv->lock);
506 priv->buf = cypress_buf_alloc(CYPRESS_BUF_SIZE);
507 if (priv->buf == NULL) {
508 kfree(priv);
509 return -ENOMEM;
510 }
511 init_waitqueue_head(&priv->delta_msr_wait);
Alan Cox813a2242008-07-22 11:10:36 +0100512
513 usb_reset_configuration(serial->dev);
514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 priv->cmd_ctrl = 0;
516 priv->line_control = 0;
517 priv->termios_initialized = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 priv->rx_flags = 0;
Mike Isely3416eaa2008-02-10 20:23:19 -0600519 /* Default packet format setting is determined by packet size.
520 Anything with a size larger then 9 must have a separate
521 count field since the 3 bit count field is otherwise too
522 small. Otherwise we can use the slightly more compact
523 format. This is in accordance with the cypress_m8 serial
524 converter app note. */
Alan Cox813a2242008-07-22 11:10:36 +0100525 if (port->interrupt_out_size > 9)
Mike Isely3416eaa2008-02-10 20:23:19 -0600526 priv->pkt_fmt = packet_format_1;
Alan Cox813a2242008-07-22 11:10:36 +0100527 else
Mike Isely3416eaa2008-02-10 20:23:19 -0600528 priv->pkt_fmt = packet_format_2;
Alan Cox813a2242008-07-22 11:10:36 +0100529
Mike Isely0257fa92006-08-29 22:06:59 -0500530 if (interval > 0) {
531 priv->write_urb_interval = interval;
532 priv->read_urb_interval = interval;
533 dbg("%s - port %d read & write intervals forced to %d",
Alan Cox813a2242008-07-22 11:10:36 +0100534 __func__, port->number, interval);
Mike Isely0257fa92006-08-29 22:06:59 -0500535 } else {
536 priv->write_urb_interval = port->interrupt_out_urb->interval;
537 priv->read_urb_interval = port->interrupt_in_urb->interval;
538 dbg("%s - port %d intervals: read=%d write=%d",
Alan Cox813a2242008-07-22 11:10:36 +0100539 __func__, port->number,
540 priv->read_urb_interval, priv->write_urb_interval);
Mike Isely0257fa92006-08-29 22:06:59 -0500541 }
542 usb_set_serial_port_data(port, priv);
Alan Cox813a2242008-07-22 11:10:36 +0100543
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500544 return 0;
545}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547
Alan Cox813a2242008-07-22 11:10:36 +0100548static int cypress_earthmate_startup(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
550 struct cypress_private *priv;
Mike Isely3d6aa322008-02-10 20:23:24 -0600551 struct usb_serial_port *port = serial->port[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Harvey Harrison441b62c2008-03-03 16:08:34 -0800553 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555 if (generic_startup(serial)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800556 dbg("%s - Failed setting up port %d", __func__,
Mike Isely3d6aa322008-02-10 20:23:24 -0600557 port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 return 1;
559 }
560
Mike Isely3d6aa322008-02-10 20:23:24 -0600561 priv = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 priv->chiptype = CT_EARTHMATE;
Mike Isely3416eaa2008-02-10 20:23:19 -0600563 /* All Earthmate devices use the separated-count packet
564 format! Idiotic. */
565 priv->pkt_fmt = packet_format_1;
Alan Cox813a2242008-07-22 11:10:36 +0100566 if (serial->dev->descriptor.idProduct !=
567 cpu_to_le16(PRODUCT_ID_EARTHMATEUSB)) {
Mike Isely3d6aa322008-02-10 20:23:24 -0600568 /* The old original USB Earthmate seemed able to
569 handle GET_CONFIG requests; everything they've
570 produced since that time crashes if this command is
571 attempted :-( */
572 dbg("%s - Marking this device as unsafe for GET_CONFIG "
573 "commands", __func__);
574 priv->get_cfg_unsafe = !0;
575 }
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500576
577 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578} /* cypress_earthmate_startup */
579
580
Alan Cox813a2242008-07-22 11:10:36 +0100581static int cypress_hidcom_startup(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
583 struct cypress_private *priv;
584
Harvey Harrison441b62c2008-03-03 16:08:34 -0800585 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587 if (generic_startup(serial)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800588 dbg("%s - Failed setting up port %d", __func__,
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500589 serial->port[0]->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 return 1;
591 }
592
593 priv = usb_get_serial_port_data(serial->port[0]);
594 priv->chiptype = CT_CYPHIDCOM;
Alan Cox813a2242008-07-22 11:10:36 +0100595
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500596 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597} /* cypress_hidcom_startup */
598
599
Alan Cox813a2242008-07-22 11:10:36 +0100600static int cypress_ca42v2_startup(struct usb_serial *serial)
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600601{
602 struct cypress_private *priv;
603
Harvey Harrison441b62c2008-03-03 16:08:34 -0800604 dbg("%s", __func__);
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600605
606 if (generic_startup(serial)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800607 dbg("%s - Failed setting up port %d", __func__,
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600608 serial->port[0]->number);
609 return 1;
610 }
611
612 priv = usb_get_serial_port_data(serial->port[0]);
613 priv->chiptype = CT_CA42V2;
614
615 return 0;
616} /* cypress_ca42v2_startup */
617
618
Alan Cox813a2242008-07-22 11:10:36 +0100619static void cypress_shutdown(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
621 struct cypress_private *priv;
622
Alan Cox813a2242008-07-22 11:10:36 +0100623 dbg("%s - port %d", __func__, serial->port[0]->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
625 /* all open ports are closed at this point */
626
627 priv = usb_get_serial_port_data(serial->port[0]);
628
629 if (priv) {
630 cypress_buf_free(priv->buf);
631 kfree(priv);
632 usb_set_serial_port_data(serial->port[0], NULL);
633 }
634}
635
636
Alan Cox95da3102008-07-22 11:09:07 +0100637static int cypress_open(struct tty_struct *tty,
638 struct usb_serial_port *port, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
640 struct cypress_private *priv = usb_get_serial_port_data(port);
641 struct usb_serial *serial = port->serial;
642 unsigned long flags;
643 int result = 0;
644
Harvey Harrison441b62c2008-03-03 16:08:34 -0800645 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Mike Isely78aef512006-08-29 22:07:11 -0500647 if (!priv->comm_is_ok)
648 return -EIO;
649
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 /* clear halts before open */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 usb_clear_halt(serial->dev, 0x81);
652 usb_clear_halt(serial->dev, 0x02);
653
654 spin_lock_irqsave(&priv->lock, flags);
655 /* reset read/write statistics */
656 priv->bytes_in = 0;
657 priv->bytes_out = 0;
658 priv->cmd_count = 0;
659 priv->rx_flags = 0;
660 spin_unlock_irqrestore(&priv->lock, flags);
661
Alan Cox335f8512009-06-11 12:26:29 +0100662 /* Set termios */
Alan Cox95da3102008-07-22 11:09:07 +0100663 result = cypress_write(tty, port, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665 if (result) {
Alan Cox813a2242008-07-22 11:10:36 +0100666 dev_err(&port->dev,
667 "%s - failed setting the control lines - error %d\n",
668 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 return result;
670 } else
Harvey Harrison441b62c2008-03-03 16:08:34 -0800671 dbg("%s - success setting the control lines", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Alan Cox95da3102008-07-22 11:09:07 +0100673 if (tty)
674 cypress_set_termios(tty, port, &priv->tmp_termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
676 /* setup the port and start reading from the device */
Alan Cox813a2242008-07-22 11:10:36 +0100677 if (!port->interrupt_in_urb) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700678 dev_err(&port->dev, "%s - interrupt_in_urb is empty!\n",
679 __func__);
Alan Cox813a2242008-07-22 11:10:36 +0100680 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 }
682
683 usb_fill_int_urb(port->interrupt_in_urb, serial->dev,
684 usb_rcvintpipe(serial->dev, port->interrupt_in_endpointAddress),
Alan Cox813a2242008-07-22 11:10:36 +0100685 port->interrupt_in_urb->transfer_buffer,
686 port->interrupt_in_urb->transfer_buffer_length,
Mike Isely0257fa92006-08-29 22:06:59 -0500687 cypress_read_int_callback, port, priv->read_urb_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
689
Alan Cox813a2242008-07-22 11:10:36 +0100690 if (result) {
691 dev_err(&port->dev,
692 "%s - failed submitting read urb, error %d\n",
693 __func__, result);
Mike Isely78aef512006-08-29 22:07:11 -0500694 cypress_set_dead(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 }
Alan Cox335f8512009-06-11 12:26:29 +0100696 port->port.drain_delay = 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 return result;
698} /* cypress_open */
699
Alan Cox335f8512009-06-11 12:26:29 +0100700static void cypress_dtr_rts(struct usb_serial_port *port, int on)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701{
702 struct cypress_private *priv = usb_get_serial_port_data(port);
Alan Cox335f8512009-06-11 12:26:29 +0100703 /* drop dtr and rts */
704 priv = usb_get_serial_port_data(port);
705 spin_lock_irq(&priv->lock);
706 if (on == 0)
707 priv->line_control = 0;
708 else
709 priv->line_control = CONTROL_DTR | CONTROL_RTS;
710 priv->cmd_ctrl = 1;
711 spin_unlock_irq(&priv->lock);
712 cypress_write(NULL, port, NULL, 0);
713}
714
715static void cypress_close(struct usb_serial_port *port)
716{
717 struct cypress_private *priv = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
Harvey Harrison441b62c2008-03-03 16:08:34 -0800719 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100721 /* writing is potentially harmful, lock must be taken */
722 mutex_lock(&port->serial->disc_mutex);
723 if (port->serial->disconnected) {
724 mutex_unlock(&port->serial->disc_mutex);
725 return;
726 }
Alan Cox335f8512009-06-11 12:26:29 +0100727 cypress_buf_clear(priv->buf);
Harvey Harrison441b62c2008-03-03 16:08:34 -0800728 dbg("%s - stopping urbs", __func__);
Alan Cox813a2242008-07-22 11:10:36 +0100729 usb_kill_urb(port->interrupt_in_urb);
730 usb_kill_urb(port->interrupt_out_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
733 if (stats)
Alan Cox813a2242008-07-22 11:10:36 +0100734 dev_info(&port->dev, "Statistics: %d Bytes In | %d Bytes Out | %d Commands Issued\n",
735 priv->bytes_in, priv->bytes_out, priv->cmd_count);
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100736 mutex_unlock(&port->serial->disc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737} /* cypress_close */
738
739
Alan Cox95da3102008-07-22 11:09:07 +0100740static int cypress_write(struct tty_struct *tty, struct usb_serial_port *port,
741 const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742{
743 struct cypress_private *priv = usb_get_serial_port_data(port);
744 unsigned long flags;
Alan Cox813a2242008-07-22 11:10:36 +0100745
Harvey Harrison441b62c2008-03-03 16:08:34 -0800746 dbg("%s - port %d, %d bytes", __func__, port->number, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
748 /* line control commands, which need to be executed immediately,
749 are not put into the buffer for obvious reasons.
750 */
751 if (priv->cmd_ctrl) {
752 count = 0;
753 goto finish;
754 }
Alan Cox813a2242008-07-22 11:10:36 +0100755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 if (!count)
757 return count;
Alan Cox813a2242008-07-22 11:10:36 +0100758
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 spin_lock_irqsave(&priv->lock, flags);
760 count = cypress_buf_put(priv->buf, buf, count);
761 spin_unlock_irqrestore(&priv->lock, flags);
762
763finish:
764 cypress_send(port);
765
766 return count;
767} /* cypress_write */
768
769
770static void cypress_send(struct usb_serial_port *port)
771{
772 int count = 0, result, offset, actual_size;
773 struct cypress_private *priv = usb_get_serial_port_data(port);
774 unsigned long flags;
Alan Cox813a2242008-07-22 11:10:36 +0100775
Mike Isely78aef512006-08-29 22:07:11 -0500776 if (!priv->comm_is_ok)
777 return;
778
Harvey Harrison441b62c2008-03-03 16:08:34 -0800779 dbg("%s - port %d", __func__, port->number);
Alan Cox813a2242008-07-22 11:10:36 +0100780 dbg("%s - interrupt out size is %d", __func__,
781 port->interrupt_out_size);
782
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 spin_lock_irqsave(&priv->lock, flags);
784 if (priv->write_urb_in_use) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800785 dbg("%s - can't write, urb in use", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 spin_unlock_irqrestore(&priv->lock, flags);
787 return;
788 }
789 spin_unlock_irqrestore(&priv->lock, flags);
790
791 /* clear buffer */
Alan Cox813a2242008-07-22 11:10:36 +0100792 memset(port->interrupt_out_urb->transfer_buffer, 0,
793 port->interrupt_out_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
795 spin_lock_irqsave(&priv->lock, flags);
Mike Isely3416eaa2008-02-10 20:23:19 -0600796 switch (priv->pkt_fmt) {
797 default:
798 case packet_format_1:
799 /* this is for the CY7C64013... */
800 offset = 2;
801 port->interrupt_out_buffer[0] = priv->line_control;
802 break;
803 case packet_format_2:
804 /* this is for the CY7C63743... */
805 offset = 1;
806 port->interrupt_out_buffer[0] = priv->line_control;
807 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 }
809
810 if (priv->line_control & CONTROL_RESET)
811 priv->line_control &= ~CONTROL_RESET;
812
813 if (priv->cmd_ctrl) {
814 priv->cmd_count++;
Harvey Harrison441b62c2008-03-03 16:08:34 -0800815 dbg("%s - line control command being issued", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 spin_unlock_irqrestore(&priv->lock, flags);
817 goto send;
818 } else
819 spin_unlock_irqrestore(&priv->lock, flags);
820
821 count = cypress_buf_get(priv->buf, &port->interrupt_out_buffer[offset],
822 port->interrupt_out_size-offset);
823
Alan Cox813a2242008-07-22 11:10:36 +0100824 if (count == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Mike Isely3416eaa2008-02-10 20:23:19 -0600827 switch (priv->pkt_fmt) {
828 default:
829 case packet_format_1:
830 port->interrupt_out_buffer[1] = count;
831 break;
832 case packet_format_2:
833 port->interrupt_out_buffer[0] |= count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 }
835
Harvey Harrison441b62c2008-03-03 16:08:34 -0800836 dbg("%s - count is %d", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
838send:
839 spin_lock_irqsave(&priv->lock, flags);
840 priv->write_urb_in_use = 1;
841 spin_unlock_irqrestore(&priv->lock, flags);
842
843 if (priv->cmd_ctrl)
844 actual_size = 1;
845 else
Mike Isely3416eaa2008-02-10 20:23:19 -0600846 actual_size = count +
847 (priv->pkt_fmt == packet_format_1 ? 2 : 1);
848
Alan Cox813a2242008-07-22 11:10:36 +0100849 usb_serial_debug_data(debug, &port->dev, __func__,
850 port->interrupt_out_size,
851 port->interrupt_out_urb->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Mike Isely9aa8dae2006-08-29 22:07:04 -0500853 usb_fill_int_urb(port->interrupt_out_urb, port->serial->dev,
854 usb_sndintpipe(port->serial->dev, port->interrupt_out_endpointAddress),
855 port->interrupt_out_buffer, port->interrupt_out_size,
856 cypress_write_int_callback, port, priv->write_urb_interval);
Alan Cox813a2242008-07-22 11:10:36 +0100857 result = usb_submit_urb(port->interrupt_out_urb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 if (result) {
Alan Cox813a2242008-07-22 11:10:36 +0100859 dev_err(&port->dev,
860 "%s - failed submitting write urb, error %d\n",
861 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 priv->write_urb_in_use = 0;
Mike Isely78aef512006-08-29 22:07:11 -0500863 cypress_set_dead(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 }
865
866 spin_lock_irqsave(&priv->lock, flags);
Alan Cox813a2242008-07-22 11:10:36 +0100867 if (priv->cmd_ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 priv->cmd_ctrl = 0;
Alan Cox813a2242008-07-22 11:10:36 +0100869
870 /* do not count the line control and size bytes */
871 priv->bytes_out += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 spin_unlock_irqrestore(&priv->lock, flags);
873
Pete Zaitcevcf2c7482006-05-22 21:58:49 -0700874 usb_serial_port_softint(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875} /* cypress_send */
876
877
878/* returns how much space is available in the soft buffer */
Alan Cox95da3102008-07-22 11:09:07 +0100879static int cypress_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880{
Alan Cox95da3102008-07-22 11:09:07 +0100881 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 struct cypress_private *priv = usb_get_serial_port_data(port);
883 int room = 0;
884 unsigned long flags;
885
Harvey Harrison441b62c2008-03-03 16:08:34 -0800886 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
888 spin_lock_irqsave(&priv->lock, flags);
889 room = cypress_buf_space_avail(priv->buf);
890 spin_unlock_irqrestore(&priv->lock, flags);
891
Harvey Harrison441b62c2008-03-03 16:08:34 -0800892 dbg("%s - returns %d", __func__, room);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 return room;
894}
895
896
Alan Cox95da3102008-07-22 11:09:07 +0100897static int cypress_tiocmget(struct tty_struct *tty, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898{
Alan Cox95da3102008-07-22 11:09:07 +0100899 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 struct cypress_private *priv = usb_get_serial_port_data(port);
901 __u8 status, control;
902 unsigned int result = 0;
903 unsigned long flags;
Alan Cox813a2242008-07-22 11:10:36 +0100904
Harvey Harrison441b62c2008-03-03 16:08:34 -0800905 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
907 spin_lock_irqsave(&priv->lock, flags);
908 control = priv->line_control;
909 status = priv->current_status;
910 spin_unlock_irqrestore(&priv->lock, flags);
911
912 result = ((control & CONTROL_DTR) ? TIOCM_DTR : 0)
913 | ((control & CONTROL_RTS) ? TIOCM_RTS : 0)
914 | ((status & UART_CTS) ? TIOCM_CTS : 0)
915 | ((status & UART_DSR) ? TIOCM_DSR : 0)
916 | ((status & UART_RI) ? TIOCM_RI : 0)
917 | ((status & UART_CD) ? TIOCM_CD : 0);
918
Harvey Harrison441b62c2008-03-03 16:08:34 -0800919 dbg("%s - result = %x", __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921 return result;
922}
923
924
Alan Cox95da3102008-07-22 11:09:07 +0100925static int cypress_tiocmset(struct tty_struct *tty, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 unsigned int set, unsigned int clear)
927{
Alan Cox95da3102008-07-22 11:09:07 +0100928 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 struct cypress_private *priv = usb_get_serial_port_data(port);
930 unsigned long flags;
Alan Cox813a2242008-07-22 11:10:36 +0100931
Harvey Harrison441b62c2008-03-03 16:08:34 -0800932 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
934 spin_lock_irqsave(&priv->lock, flags);
935 if (set & TIOCM_RTS)
936 priv->line_control |= CONTROL_RTS;
937 if (set & TIOCM_DTR)
938 priv->line_control |= CONTROL_DTR;
939 if (clear & TIOCM_RTS)
940 priv->line_control &= ~CONTROL_RTS;
941 if (clear & TIOCM_DTR)
942 priv->line_control &= ~CONTROL_DTR;
Alan Cox8873aaa2008-03-10 21:59:28 +0000943 priv->cmd_ctrl = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 spin_unlock_irqrestore(&priv->lock, flags);
945
Alan Cox95da3102008-07-22 11:09:07 +0100946 return cypress_write(tty, port, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947}
948
949
Alan Cox813a2242008-07-22 11:10:36 +0100950static int cypress_ioctl(struct tty_struct *tty, struct file *file,
Alan Cox95da3102008-07-22 11:09:07 +0100951 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952{
Alan Cox95da3102008-07-22 11:09:07 +0100953 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 struct cypress_private *priv = usb_get_serial_port_data(port);
955
Harvey Harrison441b62c2008-03-03 16:08:34 -0800956 dbg("%s - port %d, cmd 0x%.4x", __func__, port->number, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
958 switch (cmd) {
Alan Cox813a2242008-07-22 11:10:36 +0100959 /* This code comes from drivers/char/serial.c and ftdi_sio.c */
960 case TIOCMIWAIT:
961 while (priv != NULL) {
962 interruptible_sleep_on(&priv->delta_msr_wait);
963 /* see if a signal did it */
964 if (signal_pending(current))
965 return -ERESTARTSYS;
966 else {
967 char diff = priv->diff_status;
968 if (diff == 0)
969 return -EIO; /* no change => error */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Alan Cox813a2242008-07-22 11:10:36 +0100971 /* consume all events */
972 priv->diff_status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Alan Cox813a2242008-07-22 11:10:36 +0100974 /* return 0 if caller wanted to know about
975 these bits */
976 if (((arg & TIOCM_RNG) && (diff & UART_RI)) ||
977 ((arg & TIOCM_DSR) && (diff & UART_DSR)) ||
978 ((arg & TIOCM_CD) && (diff & UART_CD)) ||
979 ((arg & TIOCM_CTS) && (diff & UART_CTS)))
980 return 0;
981 /* otherwise caller can't care less about what
982 * happened, and so we continue to wait for
983 * more events.
984 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 }
Alan Cox813a2242008-07-22 11:10:36 +0100986 }
987 return 0;
988 default:
989 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 }
Harvey Harrison441b62c2008-03-03 16:08:34 -0800991 dbg("%s - arg not supported - it was 0x%04x - check include/asm/ioctls.h", __func__, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 return -ENOIOCTLCMD;
993} /* cypress_ioctl */
994
995
Alan Cox95da3102008-07-22 11:09:07 +0100996static void cypress_set_termios(struct tty_struct *tty,
997 struct usb_serial_port *port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998{
999 struct cypress_private *priv = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 int data_bits, stop_bits, parity_type, parity_enable;
Alan Cox8873aaa2008-03-10 21:59:28 +00001001 unsigned cflag, iflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 unsigned long flags;
1003 __u8 oldlines;
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001004 int linechange = 0;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001005
Harvey Harrison441b62c2008-03-03 16:08:34 -08001006 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 spin_lock_irqsave(&priv->lock, flags);
1009 if (!priv->termios_initialized) {
1010 if (priv->chiptype == CT_EARTHMATE) {
1011 *(tty->termios) = tty_std_termios;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001012 tty->termios->c_cflag = B4800 | CS8 | CREAD | HUPCL |
1013 CLOCAL;
Alan Cox8873aaa2008-03-10 21:59:28 +00001014 tty->termios->c_ispeed = 4800;
1015 tty->termios->c_ospeed = 4800;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 } else if (priv->chiptype == CT_CYPHIDCOM) {
1017 *(tty->termios) = tty_std_termios;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001018 tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
1019 CLOCAL;
Alan Cox8873aaa2008-03-10 21:59:28 +00001020 tty->termios->c_ispeed = 9600;
1021 tty->termios->c_ospeed = 9600;
Lonnie Mendeza5c44e22006-03-01 10:45:24 -06001022 } else if (priv->chiptype == CT_CA42V2) {
1023 *(tty->termios) = tty_std_termios;
1024 tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
1025 CLOCAL;
Alan Cox8873aaa2008-03-10 21:59:28 +00001026 tty->termios->c_ispeed = 9600;
1027 tty->termios->c_ospeed = 9600;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 }
1029 priv->termios_initialized = 1;
1030 }
1031 spin_unlock_irqrestore(&priv->lock, flags);
1032
Alan Cox8873aaa2008-03-10 21:59:28 +00001033 /* Unsupported features need clearing */
1034 tty->termios->c_cflag &= ~(CMSPAR|CRTSCTS);
1035
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 cflag = tty->termios->c_cflag;
1037 iflag = tty->termios->c_iflag;
1038
1039 /* check if there are new settings */
1040 if (old_termios) {
Alan Cox8873aaa2008-03-10 21:59:28 +00001041 spin_lock_irqsave(&priv->lock, flags);
1042 priv->tmp_termios = *(tty->termios);
1043 spin_unlock_irqrestore(&priv->lock, flags);
1044 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
1046 /* set number of data bits, parity, stop bits */
1047 /* when parity is disabled the parity type bit is ignored */
1048
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001049 /* 1 means 2 stop bits, 0 means 1 stop bit */
1050 stop_bits = cflag & CSTOPB ? 1 : 0;
1051
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 if (cflag & PARENB) {
1053 parity_enable = 1;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001054 /* 1 means odd parity, 0 means even parity */
1055 parity_type = cflag & PARODD ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 } else
1057 parity_enable = parity_type = 0;
1058
Alan Cox77336822008-07-22 11:10:53 +01001059 switch (cflag & CSIZE) {
1060 case CS5:
1061 data_bits = 0;
1062 break;
1063 case CS6:
1064 data_bits = 1;
1065 break;
1066 case CS7:
1067 data_bits = 2;
1068 break;
1069 case CS8:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 data_bits = 3;
Alan Cox77336822008-07-22 11:10:53 +01001071 break;
1072 default:
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001073 dev_err(&port->dev, "%s - CSIZE was set, but not CS5-CS8\n",
1074 __func__);
Alan Cox77336822008-07-22 11:10:53 +01001075 data_bits = 3;
1076 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 spin_lock_irqsave(&priv->lock, flags);
1078 oldlines = priv->line_control;
1079 if ((cflag & CBAUD) == B0) {
1080 /* drop dtr and rts */
Harvey Harrison441b62c2008-03-03 16:08:34 -08001081 dbg("%s - dropping the lines, baud rate 0bps", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
Alan Cox8873aaa2008-03-10 21:59:28 +00001083 } else
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001084 priv->line_control = (CONTROL_DTR | CONTROL_RTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001087 dbg("%s - sending %d stop_bits, %d parity_enable, %d parity_type, "
Harvey Harrison441b62c2008-03-03 16:08:34 -08001088 "%d data_bits (+5)", __func__, stop_bits,
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001089 parity_enable, parity_type, data_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Alan Cox813a2242008-07-22 11:10:36 +01001091 cypress_serial_control(tty, port, tty_get_baud_rate(tty),
1092 data_bits, stop_bits,
1093 parity_enable, parity_type,
1094 0, CYPRESS_SET_CONFIG);
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001095
1096 /* we perform a CYPRESS_GET_CONFIG so that the current settings are
1097 * filled into the private structure this should confirm that all is
1098 * working if it returns what we just set */
Alan Cox95da3102008-07-22 11:09:07 +01001099 cypress_serial_control(tty, port, 0, 0, 0, 0, 0, 0, CYPRESS_GET_CONFIG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001101 /* Here we can define custom tty settings for devices; the main tty
1102 * termios flag base comes from empeg.c */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001104 spin_lock_irqsave(&priv->lock, flags);
Alan Cox813a2242008-07-22 11:10:36 +01001105 if (priv->chiptype == CT_EARTHMATE && priv->baud_rate == 4800) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001106 dbg("Using custom termios settings for a baud rate of "
1107 "4800bps.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 /* define custom termios settings for NMEA protocol */
1109
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 tty->termios->c_iflag /* input modes - */
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001111 &= ~(IGNBRK /* disable ignore break */
1112 | BRKINT /* disable break causes interrupt */
1113 | PARMRK /* disable mark parity errors */
1114 | ISTRIP /* disable clear high bit of input char */
1115 | INLCR /* disable translate NL to CR */
1116 | IGNCR /* disable ignore CR */
1117 | ICRNL /* disable translate CR to NL */
1118 | IXON); /* disable enable XON/XOFF flow control */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001120 tty->termios->c_oflag /* output modes */
1121 &= ~OPOST; /* disable postprocess output char */
1122
1123 tty->termios->c_lflag /* line discipline modes */
1124 &= ~(ECHO /* disable echo input characters */
1125 | ECHONL /* disable echo new line */
1126 | ICANON /* disable erase, kill, werase, and rprnt
1127 special characters */
1128 | ISIG /* disable interrupt, quit, and suspend
1129 special characters */
1130 | IEXTEN); /* disable non-POSIX special characters */
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001131 } /* CT_CYPHIDCOM: Application should handle this for device */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 linechange = (priv->line_control != oldlines);
1134 spin_unlock_irqrestore(&priv->lock, flags);
1135
1136 /* if necessary, set lines */
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001137 if (linechange) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 priv->cmd_ctrl = 1;
Alan Cox95da3102008-07-22 11:09:07 +01001139 cypress_write(tty, port, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141} /* cypress_set_termios */
1142
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001143
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144/* returns amount of data still left in soft buffer */
Alan Cox95da3102008-07-22 11:09:07 +01001145static int cypress_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146{
Alan Cox95da3102008-07-22 11:09:07 +01001147 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 struct cypress_private *priv = usb_get_serial_port_data(port);
1149 int chars = 0;
1150 unsigned long flags;
1151
Harvey Harrison441b62c2008-03-03 16:08:34 -08001152 dbg("%s - port %d", __func__, port->number);
Alan Cox813a2242008-07-22 11:10:36 +01001153
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 spin_lock_irqsave(&priv->lock, flags);
1155 chars = cypress_buf_data_avail(priv->buf);
1156 spin_unlock_irqrestore(&priv->lock, flags);
1157
Harvey Harrison441b62c2008-03-03 16:08:34 -08001158 dbg("%s - returns %d", __func__, chars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 return chars;
1160}
1161
1162
Alan Cox95da3102008-07-22 11:09:07 +01001163static void cypress_throttle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164{
Alan Cox95da3102008-07-22 11:09:07 +01001165 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 struct cypress_private *priv = usb_get_serial_port_data(port);
1167 unsigned long flags;
1168
Harvey Harrison441b62c2008-03-03 16:08:34 -08001169 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
1171 spin_lock_irqsave(&priv->lock, flags);
1172 priv->rx_flags = THROTTLED;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001173 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174}
1175
1176
Alan Cox95da3102008-07-22 11:09:07 +01001177static void cypress_unthrottle(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 int actually_throttled, result;
1182 unsigned long flags;
1183
Harvey Harrison441b62c2008-03-03 16:08:34 -08001184 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
1186 spin_lock_irqsave(&priv->lock, flags);
1187 actually_throttled = priv->rx_flags & ACTUALLY_THROTTLED;
1188 priv->rx_flags = 0;
1189 spin_unlock_irqrestore(&priv->lock, flags);
1190
Mike Isely78aef512006-08-29 22:07:11 -05001191 if (!priv->comm_is_ok)
1192 return;
1193
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 if (actually_throttled) {
1195 port->interrupt_in_urb->dev = port->serial->dev;
1196
1197 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
Mike Isely78aef512006-08-29 22:07:11 -05001198 if (result) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001199 dev_err(&port->dev, "%s - failed submitting read urb, "
Harvey Harrison441b62c2008-03-03 16:08:34 -08001200 "error %d\n", __func__, result);
Mike Isely78aef512006-08-29 22:07:11 -05001201 cypress_set_dead(port);
1202 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 }
1204}
1205
1206
David Howells7d12e782006-10-05 14:55:46 +01001207static void cypress_read_int_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208{
Ming Leicdc97792008-02-24 18:41:47 +08001209 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 struct cypress_private *priv = usb_get_serial_port_data(port);
1211 struct tty_struct *tty;
1212 unsigned char *data = urb->transfer_buffer;
1213 unsigned long flags;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001214 char tty_flag = TTY_NORMAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 int havedata = 0;
1216 int bytes = 0;
1217 int result;
1218 int i = 0;
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001219 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
Harvey Harrison441b62c2008-03-03 16:08:34 -08001221 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001223 switch (status) {
Mike Isely78aef512006-08-29 22:07:11 -05001224 case 0: /* success */
1225 break;
1226 case -ECONNRESET:
1227 case -ENOENT:
1228 case -ESHUTDOWN:
1229 /* precursor to disconnect so just go away */
1230 return;
1231 case -EPIPE:
Alan Cox813a2242008-07-22 11:10:36 +01001232 usb_clear_halt(port->serial->dev, 0x81);
Mike Isely78aef512006-08-29 22:07:11 -05001233 break;
1234 default:
1235 /* something ugly is going on... */
Alan Cox813a2242008-07-22 11:10:36 +01001236 dev_err(&urb->dev->dev,
1237 "%s - unexpected nonzero read status received: %d\n",
1238 __func__, status);
Mike Isely78aef512006-08-29 22:07:11 -05001239 cypress_set_dead(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 return;
1241 }
1242
1243 spin_lock_irqsave(&priv->lock, flags);
1244 if (priv->rx_flags & THROTTLED) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001245 dbg("%s - now throttling", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 priv->rx_flags |= ACTUALLY_THROTTLED;
1247 spin_unlock_irqrestore(&priv->lock, flags);
1248 return;
1249 }
1250 spin_unlock_irqrestore(&priv->lock, flags);
1251
Alan Cox4a90f092008-10-13 10:39:46 +01001252 tty = tty_port_tty_get(&port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 if (!tty) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001254 dbg("%s - bad tty pointer - exiting", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 return;
1256 }
1257
1258 spin_lock_irqsave(&priv->lock, flags);
Mike Isely3416eaa2008-02-10 20:23:19 -06001259 result = urb->actual_length;
1260 switch (priv->pkt_fmt) {
1261 default:
1262 case packet_format_1:
1263 /* This is for the CY7C64013... */
1264 priv->current_status = data[0] & 0xF8;
1265 bytes = data[1] + 2;
1266 i = 2;
1267 if (bytes > 2)
1268 havedata = 1;
1269 break;
1270 case packet_format_2:
1271 /* This is for the CY7C63743... */
1272 priv->current_status = data[0] & 0xF8;
1273 bytes = (data[0] & 0x07) + 1;
1274 i = 1;
1275 if (bytes > 1)
1276 havedata = 1;
1277 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 }
1279 spin_unlock_irqrestore(&priv->lock, flags);
Mike Isely3416eaa2008-02-10 20:23:19 -06001280 if (result < bytes) {
1281 dbg("%s - wrong packet size - received %d bytes but packet "
1282 "said %d bytes", __func__, result, bytes);
1283 goto continue_read;
1284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
Alan Cox813a2242008-07-22 11:10:36 +01001286 usb_serial_debug_data(debug, &port->dev, __func__,
1287 urb->actual_length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
1289 spin_lock_irqsave(&priv->lock, flags);
1290 /* check to see if status has changed */
Mike Isely67683062008-02-10 20:23:28 -06001291 if (priv->current_status != priv->prev_status) {
1292 priv->diff_status |= priv->current_status ^
1293 priv->prev_status;
1294 wake_up_interruptible(&priv->delta_msr_wait);
1295 priv->prev_status = priv->current_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 }
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001297 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001299 /* hangup, as defined in acm.c... this might be a bad place for it
1300 * though */
1301 if (tty && !(tty->termios->c_cflag & CLOCAL) &&
1302 !(priv->current_status & UART_CD)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001303 dbg("%s - calling hangup", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 tty_hangup(tty);
1305 goto continue_read;
1306 }
1307
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001308 /* There is one error bit... I'm assuming it is a parity error
1309 * indicator as the generic firmware will set this bit to 1 if a
1310 * parity error occurs.
1311 * I can not find reference to any other error events. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 spin_lock_irqsave(&priv->lock, flags);
1313 if (priv->current_status & CYP_ERROR) {
1314 spin_unlock_irqrestore(&priv->lock, flags);
1315 tty_flag = TTY_PARITY;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001316 dbg("%s - Parity Error detected", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 } else
1318 spin_unlock_irqrestore(&priv->lock, flags);
1319
1320 /* process read if there is data other than line status */
1321 if (tty && (bytes > i)) {
Alan Cox33f0f882006-01-09 20:54:13 -08001322 bytes = tty_buffer_request_room(tty, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 for (; i < bytes ; ++i) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001324 dbg("pushing byte number %d - %d - %c", i, data[i],
1325 data[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 tty_insert_flip_char(tty, data[i], tty_flag);
1327 }
Alan Cox4a90f092008-10-13 10:39:46 +01001328 tty_flip_buffer_push(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 }
1330
1331 spin_lock_irqsave(&priv->lock, flags);
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001332 /* control and status byte(s) are also counted */
1333 priv->bytes_in += bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 spin_unlock_irqrestore(&priv->lock, flags);
1335
1336continue_read:
Alan Cox4a90f092008-10-13 10:39:46 +01001337 tty_kref_put(tty);
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001338
1339 /* Continue trying to always read... unless the port has closed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
Alan Cox95da3102008-07-22 11:09:07 +01001341 if (port->port.count > 0 && priv->comm_is_ok) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001342 usb_fill_int_urb(port->interrupt_in_urb, port->serial->dev,
1343 usb_rcvintpipe(port->serial->dev,
1344 port->interrupt_in_endpointAddress),
1345 port->interrupt_in_urb->transfer_buffer,
1346 port->interrupt_in_urb->transfer_buffer_length,
Alan Cox813a2242008-07-22 11:10:36 +01001347 cypress_read_int_callback, port,
1348 priv->read_urb_interval);
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001349 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
Mike Isely78aef512006-08-29 22:07:11 -05001350 if (result) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001351 dev_err(&urb->dev->dev, "%s - failed resubmitting "
Harvey Harrison441b62c2008-03-03 16:08:34 -08001352 "read urb, error %d\n", __func__,
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001353 result);
Mike Isely78aef512006-08-29 22:07:11 -05001354 cypress_set_dead(port);
1355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 }
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001357
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 return;
1359} /* cypress_read_int_callback */
1360
1361
David Howells7d12e782006-10-05 14:55:46 +01001362static void cypress_write_int_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363{
Ming Leicdc97792008-02-24 18:41:47 +08001364 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 struct cypress_private *priv = usb_get_serial_port_data(port);
1366 int result;
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001367 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
Harvey Harrison441b62c2008-03-03 16:08:34 -08001369 dbg("%s - port %d", __func__, port->number);
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001370
1371 switch (status) {
Alan Cox813a2242008-07-22 11:10:36 +01001372 case 0:
1373 /* success */
1374 break;
1375 case -ECONNRESET:
1376 case -ENOENT:
1377 case -ESHUTDOWN:
1378 /* this urb is terminated, clean up */
1379 dbg("%s - urb shutting down with status: %d",
1380 __func__, status);
1381 priv->write_urb_in_use = 0;
1382 return;
1383 case -EPIPE: /* no break needed; clear halt and resubmit */
1384 if (!priv->comm_is_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 break;
Alan Cox813a2242008-07-22 11:10:36 +01001386 usb_clear_halt(port->serial->dev, 0x02);
1387 /* error in the urb, so we have to resubmit it */
1388 dbg("%s - nonzero write bulk status received: %d",
1389 __func__, status);
1390 port->interrupt_out_urb->transfer_buffer_length = 1;
1391 port->interrupt_out_urb->dev = port->serial->dev;
1392 result = usb_submit_urb(port->interrupt_out_urb, GFP_ATOMIC);
1393 if (!result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 return;
Alan Cox813a2242008-07-22 11:10:36 +01001395 dev_err(&urb->dev->dev,
1396 "%s - failed resubmitting write urb, error %d\n",
1397 __func__, result);
1398 cypress_set_dead(port);
1399 break;
1400 default:
1401 dev_err(&urb->dev->dev,
1402 "%s - unexpected nonzero write status received: %d\n",
1403 __func__, status);
1404 cypress_set_dead(port);
1405 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 priv->write_urb_in_use = 0;
Alan Cox813a2242008-07-22 11:10:36 +01001408
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 /* send any buffered data */
1410 cypress_send(port);
1411}
1412
1413
1414/*****************************************************************************
1415 * Write buffer functions - buffering code from pl2303 used
1416 *****************************************************************************/
1417
1418/*
1419 * cypress_buf_alloc
1420 *
1421 * Allocate a circular buffer and all associated memory.
1422 */
1423
1424static struct cypress_buf *cypress_buf_alloc(unsigned int size)
1425{
1426
1427 struct cypress_buf *cb;
1428
1429
1430 if (size == 0)
1431 return NULL;
1432
Robert P. J. Day5cbded52006-12-13 00:35:56 -08001433 cb = kmalloc(sizeof(struct cypress_buf), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 if (cb == NULL)
1435 return NULL;
1436
1437 cb->buf_buf = kmalloc(size, GFP_KERNEL);
1438 if (cb->buf_buf == NULL) {
1439 kfree(cb);
1440 return NULL;
1441 }
1442
1443 cb->buf_size = size;
1444 cb->buf_get = cb->buf_put = cb->buf_buf;
1445
1446 return cb;
1447
1448}
1449
1450
1451/*
1452 * cypress_buf_free
1453 *
1454 * Free the buffer and all associated memory.
1455 */
1456
1457static void cypress_buf_free(struct cypress_buf *cb)
1458{
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001459 if (cb) {
1460 kfree(cb->buf_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 kfree(cb);
1462 }
1463}
1464
1465
1466/*
1467 * cypress_buf_clear
1468 *
1469 * Clear out all data in the circular buffer.
1470 */
1471
1472static void cypress_buf_clear(struct cypress_buf *cb)
1473{
1474 if (cb != NULL)
1475 cb->buf_get = cb->buf_put;
1476 /* equivalent to a get of all data available */
1477}
1478
1479
1480/*
1481 * cypress_buf_data_avail
1482 *
1483 * Return the number of bytes of data available in the circular
1484 * buffer.
1485 */
1486
1487static unsigned int cypress_buf_data_avail(struct cypress_buf *cb)
1488{
1489 if (cb != NULL)
Alan Cox813a2242008-07-22 11:10:36 +01001490 return (cb->buf_size + cb->buf_put - cb->buf_get)
1491 % cb->buf_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 else
1493 return 0;
1494}
1495
1496
1497/*
1498 * cypress_buf_space_avail
1499 *
1500 * Return the number of bytes of space available in the circular
1501 * buffer.
1502 */
1503
1504static unsigned int cypress_buf_space_avail(struct cypress_buf *cb)
1505{
1506 if (cb != NULL)
Alan Cox813a2242008-07-22 11:10:36 +01001507 return (cb->buf_size + cb->buf_get - cb->buf_put - 1)
1508 % cb->buf_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 else
1510 return 0;
1511}
1512
1513
1514/*
1515 * cypress_buf_put
1516 *
1517 * Copy data data from a user buffer and put it into the circular buffer.
1518 * Restrict to the amount of space available.
1519 *
1520 * Return the number of bytes copied.
1521 */
1522
1523static unsigned int cypress_buf_put(struct cypress_buf *cb, const char *buf,
1524 unsigned int count)
1525{
1526
1527 unsigned int len;
1528
1529
1530 if (cb == NULL)
1531 return 0;
1532
1533 len = cypress_buf_space_avail(cb);
1534 if (count > len)
1535 count = len;
1536
1537 if (count == 0)
1538 return 0;
1539
1540 len = cb->buf_buf + cb->buf_size - cb->buf_put;
1541 if (count > len) {
1542 memcpy(cb->buf_put, buf, len);
1543 memcpy(cb->buf_buf, buf+len, count - len);
1544 cb->buf_put = cb->buf_buf + count - len;
1545 } else {
1546 memcpy(cb->buf_put, buf, count);
1547 if (count < len)
1548 cb->buf_put += count;
1549 else /* count == len */
1550 cb->buf_put = cb->buf_buf;
1551 }
1552
1553 return count;
1554
1555}
1556
1557
1558/*
1559 * cypress_buf_get
1560 *
1561 * Get data from the circular buffer and copy to the given buffer.
1562 * Restrict to the amount of data available.
1563 *
1564 * Return the number of bytes copied.
1565 */
1566
1567static unsigned int cypress_buf_get(struct cypress_buf *cb, char *buf,
1568 unsigned int count)
1569{
1570
1571 unsigned int len;
1572
1573
1574 if (cb == NULL)
1575 return 0;
1576
1577 len = cypress_buf_data_avail(cb);
1578 if (count > len)
1579 count = len;
1580
1581 if (count == 0)
1582 return 0;
1583
1584 len = cb->buf_buf + cb->buf_size - cb->buf_get;
1585 if (count > len) {
1586 memcpy(buf, cb->buf_get, len);
1587 memcpy(buf+len, cb->buf_buf, count - len);
1588 cb->buf_get = cb->buf_buf + count - len;
1589 } else {
1590 memcpy(buf, cb->buf_get, count);
1591 if (count < len)
1592 cb->buf_get += count;
1593 else /* count == len */
1594 cb->buf_get = cb->buf_buf;
1595 }
1596
1597 return count;
1598
1599}
1600
1601/*****************************************************************************
1602 * Module functions
1603 *****************************************************************************/
1604
1605static int __init cypress_init(void)
1606{
1607 int retval;
Alan Cox813a2242008-07-22 11:10:36 +01001608
Harvey Harrison441b62c2008-03-03 16:08:34 -08001609 dbg("%s", __func__);
Alan Cox813a2242008-07-22 11:10:36 +01001610
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 retval = usb_serial_register(&cypress_earthmate_device);
1612 if (retval)
1613 goto failed_em_register;
1614 retval = usb_serial_register(&cypress_hidcom_device);
1615 if (retval)
1616 goto failed_hidcom_register;
Lonnie Mendeza5c44e22006-03-01 10:45:24 -06001617 retval = usb_serial_register(&cypress_ca42v2_device);
1618 if (retval)
1619 goto failed_ca42v2_register;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 retval = usb_register(&cypress_driver);
1621 if (retval)
1622 goto failed_usb_register;
1623
Greg Kroah-Hartmanc197a8d2008-08-18 13:21:04 -07001624 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
1625 DRIVER_DESC "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
Mariusz Kozlowski2e46b742006-11-17 17:49:22 +01001628failed_usb_register:
1629 usb_serial_deregister(&cypress_ca42v2_device);
1630failed_ca42v2_register:
1631 usb_serial_deregister(&cypress_hidcom_device);
1632failed_hidcom_register:
1633 usb_serial_deregister(&cypress_earthmate_device);
1634failed_em_register:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 return retval;
1636}
1637
1638
Alan Cox813a2242008-07-22 11:10:36 +01001639static void __exit cypress_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640{
Harvey Harrison441b62c2008-03-03 16:08:34 -08001641 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
Alan Cox813a2242008-07-22 11:10:36 +01001643 usb_deregister(&cypress_driver);
1644 usb_serial_deregister(&cypress_earthmate_device);
1645 usb_serial_deregister(&cypress_hidcom_device);
1646 usb_serial_deregister(&cypress_ca42v2_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647}
1648
1649
1650module_init(cypress_init);
1651module_exit(cypress_exit);
1652
Alan Cox813a2242008-07-22 11:10:36 +01001653MODULE_AUTHOR(DRIVER_AUTHOR);
1654MODULE_DESCRIPTION(DRIVER_DESC);
1655MODULE_VERSION(DRIVER_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656MODULE_LICENSE("GPL");
1657
1658module_param(debug, bool, S_IRUGO | S_IWUSR);
1659MODULE_PARM_DESC(debug, "Debug enabled or not");
1660module_param(stats, bool, S_IRUGO | S_IWUSR);
1661MODULE_PARM_DESC(stats, "Enable statistics or not");
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001662module_param(interval, int, S_IRUGO | S_IWUSR);
1663MODULE_PARM_DESC(interval, "Overrides interrupt interval");