blob: 47a18193abad7a88f3e5f9e0b504e068161db8ec [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
Mike Frysinger64319dd2009-12-18 16:33:01 -050073static int debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074static int stats;
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -050075static int interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77/*
78 * Version Information
79 */
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -050080#define DRIVER_VERSION "v1.09"
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#define DRIVER_AUTHOR "Lonnie Mendez <dignome@gmail.com>, Neil Whelchel <koyama@firstlight.net>"
82#define DRIVER_DESC "Cypress USB to Serial Driver"
83
84/* write buffer size defines */
85#define CYPRESS_BUF_SIZE 1024
86#define CYPRESS_CLOSING_WAIT (30*HZ)
87
88static struct usb_device_id id_table_earthmate [] = {
89 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB) },
Lonnie Mendez25b6f082005-05-10 17:09:52 -050090 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB_LT20) },
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 { } /* Terminating entry */
92};
93
94static struct usb_device_id id_table_cyphidcomrs232 [] = {
95 { USB_DEVICE(VENDOR_ID_CYPRESS, PRODUCT_ID_CYPHIDCOM) },
Dmitry Shapin6f6f06e2008-03-04 15:25:10 -080096 { USB_DEVICE(VENDOR_ID_POWERCOM, PRODUCT_ID_UPS) },
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 { } /* Terminating entry */
98};
99
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600100static struct usb_device_id id_table_nokiaca42v2 [] = {
101 { USB_DEVICE(VENDOR_ID_DAZZLE, PRODUCT_ID_CA42) },
102 { } /* Terminating entry */
103};
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105static struct usb_device_id id_table_combined [] = {
106 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB) },
Lonnie Mendez25b6f082005-05-10 17:09:52 -0500107 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB_LT20) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 { USB_DEVICE(VENDOR_ID_CYPRESS, PRODUCT_ID_CYPHIDCOM) },
Dmitry Shapin6f6f06e2008-03-04 15:25:10 -0800109 { USB_DEVICE(VENDOR_ID_POWERCOM, PRODUCT_ID_UPS) },
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600110 { USB_DEVICE(VENDOR_ID_DAZZLE, PRODUCT_ID_CA42) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 { } /* Terminating entry */
112};
113
Alan Cox813a2242008-07-22 11:10:36 +0100114MODULE_DEVICE_TABLE(usb, id_table_combined);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116static struct usb_driver cypress_driver = {
117 .name = "cypress",
118 .probe = usb_serial_probe,
119 .disconnect = usb_serial_disconnect,
120 .id_table = id_table_combined,
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800121 .no_dynamic_id = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122};
123
Mike Isely3416eaa2008-02-10 20:23:19 -0600124enum packet_format {
125 packet_format_1, /* b0:status, b1:payload count */
126 packet_format_2 /* b0[7:3]:status, b0[2:0]:payload count */
127};
128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129struct cypress_private {
130 spinlock_t lock; /* private lock */
131 int chiptype; /* identifier of device, for quirks/etc */
132 int bytes_in; /* used for statistics */
133 int bytes_out; /* used for statistics */
134 int cmd_count; /* used for statistics */
135 int cmd_ctrl; /* always set this to 1 before issuing a command */
136 struct cypress_buf *buf; /* write buffer */
137 int write_urb_in_use; /* write urb in use indicator */
Mike Isely0257fa92006-08-29 22:06:59 -0500138 int write_urb_interval; /* interval to use for write urb */
139 int read_urb_interval; /* interval to use for read urb */
Mike Isely78aef512006-08-29 22:07:11 -0500140 int comm_is_ok; /* true if communication is (still) ok */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 int termios_initialized;
142 __u8 line_control; /* holds dtr / rts value */
143 __u8 current_status; /* received from last read - info on dsr,cts,cd,ri,etc */
144 __u8 current_config; /* stores the current configuration byte */
145 __u8 rx_flags; /* throttling - used from whiteheat/ftdi_sio */
Mike Isely3416eaa2008-02-10 20:23:19 -0600146 enum packet_format pkt_fmt; /* format to use for packet send / receive */
Mike Isely3d6aa322008-02-10 20:23:24 -0600147 int get_cfg_unsafe; /* If true, the CYPRESS_GET_CONFIG is unsafe */
Alan Cox813a2242008-07-22 11:10:36 +0100148 int baud_rate; /* stores current baud rate in
149 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 */
Alan Cox813a2242008-07-22 11:10:36 +0100153 /* we pass a pointer to this as the arguement sent to
154 cypress_set_termios old_termios */
Alan Cox606d0992006-12-08 02:38:45 -0800155 struct ktermios tmp_termios; /* stores the old termios settings */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156};
157
158/* write buffer structure */
159struct cypress_buf {
160 unsigned int buf_size;
161 char *buf_buf;
162 char *buf_get;
163 char *buf_put;
164};
165
166/* function prototypes for the Cypress USB to serial device */
Alan Cox813a2242008-07-22 11:10:36 +0100167static int cypress_earthmate_startup(struct usb_serial *serial);
168static int cypress_hidcom_startup(struct usb_serial *serial);
169static int cypress_ca42v2_startup(struct usb_serial *serial);
Alan Sternf9c99bb2009-06-02 11:53:55 -0400170static void cypress_release(struct usb_serial *serial);
Alan Coxa509a7e2009-09-19 13:13:26 -0700171static int cypress_open(struct tty_struct *tty, struct usb_serial_port *port);
Alan Cox335f8512009-06-11 12:26:29 +0100172static void cypress_close(struct usb_serial_port *port);
173static void cypress_dtr_rts(struct usb_serial_port *port, int on);
Alan Cox813a2242008-07-22 11:10:36 +0100174static int cypress_write(struct tty_struct *tty, struct usb_serial_port *port,
175 const unsigned char *buf, int count);
176static void cypress_send(struct usb_serial_port *port);
177static int cypress_write_room(struct tty_struct *tty);
178static int cypress_ioctl(struct tty_struct *tty, struct file *file,
179 unsigned int cmd, unsigned long arg);
180static void cypress_set_termios(struct tty_struct *tty,
181 struct usb_serial_port *port, struct ktermios *old);
182static int cypress_tiocmget(struct tty_struct *tty, struct file *file);
183static int cypress_tiocmset(struct tty_struct *tty, struct file *file,
184 unsigned int set, unsigned int clear);
185static int cypress_chars_in_buffer(struct tty_struct *tty);
186static void cypress_throttle(struct tty_struct *tty);
187static void cypress_unthrottle(struct tty_struct *tty);
188static void cypress_set_dead(struct usb_serial_port *port);
189static void cypress_read_int_callback(struct urb *urb);
190static void cypress_write_int_callback(struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191/* write buffer functions */
192static struct cypress_buf *cypress_buf_alloc(unsigned int size);
Alan Cox813a2242008-07-22 11:10:36 +0100193static void cypress_buf_free(struct cypress_buf *cb);
194static void cypress_buf_clear(struct cypress_buf *cb);
195static unsigned int cypress_buf_data_avail(struct cypress_buf *cb);
196static unsigned int cypress_buf_space_avail(struct cypress_buf *cb);
197static unsigned int cypress_buf_put(struct cypress_buf *cb,
198 const char *buf, unsigned int count);
199static unsigned int cypress_buf_get(struct cypress_buf *cb,
200 char *buf, unsigned int count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700203static struct usb_serial_driver cypress_earthmate_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700204 .driver = {
205 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700206 .name = "earthmate",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700207 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700208 .description = "DeLorme Earthmate USB",
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100209 .usb_driver = &cypress_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 .id_table = id_table_earthmate,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 .num_ports = 1,
212 .attach = cypress_earthmate_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -0400213 .release = cypress_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 .open = cypress_open,
215 .close = cypress_close,
Alan Cox335f8512009-06-11 12:26:29 +0100216 .dtr_rts = cypress_dtr_rts,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 .write = cypress_write,
218 .write_room = cypress_write_room,
219 .ioctl = cypress_ioctl,
220 .set_termios = cypress_set_termios,
221 .tiocmget = cypress_tiocmget,
222 .tiocmset = cypress_tiocmset,
223 .chars_in_buffer = cypress_chars_in_buffer,
224 .throttle = cypress_throttle,
225 .unthrottle = cypress_unthrottle,
226 .read_int_callback = cypress_read_int_callback,
227 .write_int_callback = cypress_write_int_callback,
228};
229
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700230static struct usb_serial_driver cypress_hidcom_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700231 .driver = {
232 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700233 .name = "cyphidcom",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700234 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700235 .description = "HID->COM RS232 Adapter",
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100236 .usb_driver = &cypress_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 .id_table = id_table_cyphidcomrs232,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 .num_ports = 1,
239 .attach = cypress_hidcom_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -0400240 .release = cypress_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 .open = cypress_open,
242 .close = cypress_close,
Alan Cox335f8512009-06-11 12:26:29 +0100243 .dtr_rts = cypress_dtr_rts,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 .write = cypress_write,
245 .write_room = cypress_write_room,
246 .ioctl = cypress_ioctl,
247 .set_termios = cypress_set_termios,
248 .tiocmget = cypress_tiocmget,
249 .tiocmset = cypress_tiocmset,
250 .chars_in_buffer = cypress_chars_in_buffer,
251 .throttle = cypress_throttle,
252 .unthrottle = cypress_unthrottle,
253 .read_int_callback = cypress_read_int_callback,
254 .write_int_callback = cypress_write_int_callback,
255};
256
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600257static struct usb_serial_driver cypress_ca42v2_device = {
258 .driver = {
259 .owner = THIS_MODULE,
Alan Cox813a2242008-07-22 11:10:36 +0100260 .name = "nokiaca42v2",
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600261 },
262 .description = "Nokia CA-42 V2 Adapter",
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100263 .usb_driver = &cypress_driver,
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600264 .id_table = id_table_nokiaca42v2,
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600265 .num_ports = 1,
266 .attach = cypress_ca42v2_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -0400267 .release = cypress_release,
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600268 .open = cypress_open,
269 .close = cypress_close,
Alan Cox335f8512009-06-11 12:26:29 +0100270 .dtr_rts = cypress_dtr_rts,
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600271 .write = cypress_write,
272 .write_room = cypress_write_room,
273 .ioctl = cypress_ioctl,
274 .set_termios = cypress_set_termios,
275 .tiocmget = cypress_tiocmget,
276 .tiocmset = cypress_tiocmset,
277 .chars_in_buffer = cypress_chars_in_buffer,
278 .throttle = cypress_throttle,
279 .unthrottle = cypress_unthrottle,
280 .read_int_callback = cypress_read_int_callback,
281 .write_int_callback = cypress_write_int_callback,
282};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284/*****************************************************************************
285 * Cypress serial helper functions
286 *****************************************************************************/
287
288
Alan Cox8873aaa2008-03-10 21:59:28 +0000289static int analyze_baud_rate(struct usb_serial_port *port, speed_t new_rate)
Mike Isely92983c22008-02-10 20:23:32 -0600290{
Mike Isely92983c22008-02-10 20:23:32 -0600291 struct cypress_private *priv;
292 priv = usb_get_serial_port_data(port);
293
294 /*
295 * The general purpose firmware for the Cypress M8 allows for
296 * a maximum speed of 57600bps (I have no idea whether DeLorme
297 * chose to use the general purpose firmware or not), if you
298 * need to modify this speed setting for your own project
299 * please add your own chiptype and modify the code likewise.
300 * The Cypress HID->COM device will work successfully up to
301 * 115200bps (but the actual throughput is around 3kBps).
302 */
Mike Isely92983c22008-02-10 20:23:32 -0600303 if (port->serial->dev->speed == USB_SPEED_LOW) {
304 /*
305 * Mike Isely <isely@pobox.com> 2-Feb-2008: The
306 * Cypress app note that describes this mechanism
307 * states the the low-speed part can't handle more
308 * than 800 bytes/sec, in which case 4800 baud is the
309 * safest speed for a part like that.
310 */
311 if (new_rate > 4800) {
312 dbg("%s - failed setting baud rate, device incapable "
313 "speed %d", __func__, new_rate);
314 return -1;
315 }
316 }
317 switch (priv->chiptype) {
318 case CT_EARTHMATE:
319 if (new_rate <= 600) {
320 /* 300 and 600 baud rates are supported under
321 * the generic firmware, but are not used with
322 * NMEA and SiRF protocols */
323 dbg("%s - failed setting baud rate, unsupported speed "
324 "of %d on Earthmate GPS", __func__, new_rate);
325 return -1;
326 }
327 break;
328 default:
329 break;
330 }
331 return new_rate;
332}
333
334
Steven Cole093cf722005-05-03 19:07:24 -0600335/* This function can either set or retrieve the current serial line settings */
Alan Cox813a2242008-07-22 11:10:36 +0100336static int cypress_serial_control(struct tty_struct *tty,
Alan Cox95da3102008-07-22 11:09:07 +0100337 struct usb_serial_port *port, speed_t baud_rate, int data_bits,
338 int stop_bits, int parity_enable, int parity_type, int reset,
339 int cypress_request_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500341 int new_baudrate = 0, retval = 0, tries = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 struct cypress_private *priv;
Mike Isely93075542008-02-10 20:23:14 -0600343 __u8 feature_buffer[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 unsigned long flags;
345
Harvey Harrison441b62c2008-03-03 16:08:34 -0800346 dbg("%s", __func__);
Alan Cox813a2242008-07-22 11:10:36 +0100347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 priv = usb_get_serial_port_data(port);
349
Mike Isely78aef512006-08-29 22:07:11 -0500350 if (!priv->comm_is_ok)
351 return -ENODEV;
352
Alan Cox813a2242008-07-22 11:10:36 +0100353 switch (cypress_request_type) {
354 case CYPRESS_SET_CONFIG:
Alan Cox813a2242008-07-22 11:10:36 +0100355 /* 0 means 'Hang up' so doesn't change the true bit rate */
Mike Frysinger2805eb12009-12-18 16:33:02 -0500356 new_baudrate = priv->baud_rate;
357 if (baud_rate && baud_rate != priv->baud_rate) {
Alan Cox813a2242008-07-22 11:10:36 +0100358 dbg("%s - baud rate is changing", __func__);
359 retval = analyze_baud_rate(port, baud_rate);
Mike Frysinger2805eb12009-12-18 16:33:02 -0500360 if (retval >= 0) {
Alan Cox813a2242008-07-22 11:10:36 +0100361 new_baudrate = retval;
362 dbg("%s - New baud rate set to %d",
363 __func__, new_baudrate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 }
Alan Cox813a2242008-07-22 11:10:36 +0100365 }
366 dbg("%s - baud rate is being sent as %d",
367 __func__, new_baudrate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Alan Cox813a2242008-07-22 11:10:36 +0100369 memset(feature_buffer, 0, sizeof(feature_buffer));
370 /* fill the feature_buffer with new configuration */
371 *((u_int32_t *)feature_buffer) = new_baudrate;
372 feature_buffer[4] |= data_bits; /* assign data bits in 2 bit space ( max 3 ) */
373 /* 1 bit gap */
374 feature_buffer[4] |= (stop_bits << 3); /* assign stop bits in 1 bit space */
375 feature_buffer[4] |= (parity_enable << 4); /* assign parity flag in 1 bit space */
376 feature_buffer[4] |= (parity_type << 5); /* assign parity type in 1 bit space */
377 /* 1 bit gap */
378 feature_buffer[4] |= (reset << 7); /* assign reset at end of byte, 1 bit space */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Alan Cox813a2242008-07-22 11:10:36 +0100380 dbg("%s - device is being sent this feature report:",
381 __func__);
382 dbg("%s - %02X - %02X - %02X - %02X - %02X", __func__,
383 feature_buffer[0], feature_buffer[1],
384 feature_buffer[2], feature_buffer[3],
385 feature_buffer[4]);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500386
Alan Cox813a2242008-07-22 11:10:36 +0100387 do {
388 retval = usb_control_msg(port->serial->dev,
389 usb_sndctrlpipe(port->serial->dev, 0),
390 HID_REQ_SET_REPORT,
391 USB_DIR_OUT | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
392 0x0300, 0, feature_buffer,
393 sizeof(feature_buffer), 500);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500394
Alan Cox813a2242008-07-22 11:10:36 +0100395 if (tries++ >= 3)
396 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Alan Cox813a2242008-07-22 11:10:36 +0100398 } while (retval != sizeof(feature_buffer) &&
399 retval != -ENODEV);
Mike Isely93075542008-02-10 20:23:14 -0600400
Alan Cox813a2242008-07-22 11:10:36 +0100401 if (retval != sizeof(feature_buffer)) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700402 dev_err(&port->dev, "%s - failed sending serial "
403 "line settings - %d\n", __func__, retval);
Alan Cox813a2242008-07-22 11:10:36 +0100404 cypress_set_dead(port);
405 } else {
406 spin_lock_irqsave(&priv->lock, flags);
407 priv->baud_rate = new_baudrate;
408 priv->current_config = feature_buffer[4];
409 spin_unlock_irqrestore(&priv->lock, flags);
410 /* If we asked for a speed change encode it */
411 if (baud_rate)
412 tty_encode_baud_rate(tty,
413 new_baudrate, new_baudrate);
414 }
415 break;
416 case CYPRESS_GET_CONFIG:
417 if (priv->get_cfg_unsafe) {
418 /* Not implemented for this device,
419 and if we try to do it we're likely
420 to crash the hardware. */
421 return -ENOTTY;
422 }
423 dbg("%s - retreiving serial line settings", __func__);
424 /* set initial values in feature buffer */
425 memset(feature_buffer, 0, sizeof(feature_buffer));
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500426
Alan Cox813a2242008-07-22 11:10:36 +0100427 do {
428 retval = usb_control_msg(port->serial->dev,
429 usb_rcvctrlpipe(port->serial->dev, 0),
430 HID_REQ_GET_REPORT,
431 USB_DIR_IN | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
432 0x0300, 0, feature_buffer,
433 sizeof(feature_buffer), 500);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500434
Alan Cox813a2242008-07-22 11:10:36 +0100435 if (tries++ >= 3)
436 break;
437 } while (retval != sizeof(feature_buffer)
438 && retval != -ENODEV);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500439
Alan Cox813a2242008-07-22 11:10:36 +0100440 if (retval != sizeof(feature_buffer)) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700441 dev_err(&port->dev, "%s - failed to retrieve serial "
442 "line settings - %d\n", __func__, retval);
Alan Cox813a2242008-07-22 11:10:36 +0100443 cypress_set_dead(port);
444 return retval;
445 } else {
446 spin_lock_irqsave(&priv->lock, flags);
447 /* store the config in one byte, and later
448 use bit masks to check values */
449 priv->current_config = feature_buffer[4];
450 priv->baud_rate = *((u_int32_t *)feature_buffer);
451 spin_unlock_irqrestore(&priv->lock, flags);
452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 }
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500454 spin_lock_irqsave(&priv->lock, flags);
455 ++priv->cmd_count;
456 spin_unlock_irqrestore(&priv->lock, flags);
457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 return retval;
459} /* cypress_serial_control */
460
461
Mike Isely78aef512006-08-29 22:07:11 -0500462static void cypress_set_dead(struct usb_serial_port *port)
463{
464 struct cypress_private *priv = usb_get_serial_port_data(port);
465 unsigned long flags;
466
467 spin_lock_irqsave(&priv->lock, flags);
468 if (!priv->comm_is_ok) {
469 spin_unlock_irqrestore(&priv->lock, flags);
470 return;
471 }
472 priv->comm_is_ok = 0;
473 spin_unlock_irqrestore(&priv->lock, flags);
474
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700475 dev_err(&port->dev, "cypress_m8 suspending failing port %d - "
476 "interval might be too short\n", port->number);
Mike Isely78aef512006-08-29 22:07:11 -0500477}
478
479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480/*****************************************************************************
481 * Cypress serial driver functions
482 *****************************************************************************/
483
484
Alan Cox813a2242008-07-22 11:10:36 +0100485static int generic_startup(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
487 struct cypress_private *priv;
Mike Isely0257fa92006-08-29 22:06:59 -0500488 struct usb_serial_port *port = serial->port[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Harvey Harrison441b62c2008-03-03 16:08:34 -0800490 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Alan Cox813a2242008-07-22 11:10:36 +0100492 priv = kzalloc(sizeof(struct cypress_private), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 if (!priv)
494 return -ENOMEM;
495
Mike Isely78aef512006-08-29 22:07:11 -0500496 priv->comm_is_ok = !0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 spin_lock_init(&priv->lock);
498 priv->buf = cypress_buf_alloc(CYPRESS_BUF_SIZE);
499 if (priv->buf == NULL) {
500 kfree(priv);
501 return -ENOMEM;
502 }
503 init_waitqueue_head(&priv->delta_msr_wait);
Alan Cox813a2242008-07-22 11:10:36 +0100504
505 usb_reset_configuration(serial->dev);
506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 priv->cmd_ctrl = 0;
508 priv->line_control = 0;
509 priv->termios_initialized = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 priv->rx_flags = 0;
Mike Isely3416eaa2008-02-10 20:23:19 -0600511 /* Default packet format setting is determined by packet size.
512 Anything with a size larger then 9 must have a separate
513 count field since the 3 bit count field is otherwise too
514 small. Otherwise we can use the slightly more compact
515 format. This is in accordance with the cypress_m8 serial
516 converter app note. */
Alan Cox813a2242008-07-22 11:10:36 +0100517 if (port->interrupt_out_size > 9)
Mike Isely3416eaa2008-02-10 20:23:19 -0600518 priv->pkt_fmt = packet_format_1;
Alan Cox813a2242008-07-22 11:10:36 +0100519 else
Mike Isely3416eaa2008-02-10 20:23:19 -0600520 priv->pkt_fmt = packet_format_2;
Alan Cox813a2242008-07-22 11:10:36 +0100521
Mike Isely0257fa92006-08-29 22:06:59 -0500522 if (interval > 0) {
523 priv->write_urb_interval = interval;
524 priv->read_urb_interval = interval;
525 dbg("%s - port %d read & write intervals forced to %d",
Alan Cox813a2242008-07-22 11:10:36 +0100526 __func__, port->number, interval);
Mike Isely0257fa92006-08-29 22:06:59 -0500527 } else {
528 priv->write_urb_interval = port->interrupt_out_urb->interval;
529 priv->read_urb_interval = port->interrupt_in_urb->interval;
530 dbg("%s - port %d intervals: read=%d write=%d",
Alan Cox813a2242008-07-22 11:10:36 +0100531 __func__, port->number,
532 priv->read_urb_interval, priv->write_urb_interval);
Mike Isely0257fa92006-08-29 22:06:59 -0500533 }
534 usb_set_serial_port_data(port, priv);
Alan Cox813a2242008-07-22 11:10:36 +0100535
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500536 return 0;
537}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
539
Alan Cox813a2242008-07-22 11:10:36 +0100540static int cypress_earthmate_startup(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
542 struct cypress_private *priv;
Mike Isely3d6aa322008-02-10 20:23:24 -0600543 struct usb_serial_port *port = serial->port[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
Harvey Harrison441b62c2008-03-03 16:08:34 -0800545 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547 if (generic_startup(serial)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800548 dbg("%s - Failed setting up port %d", __func__,
Mike Isely3d6aa322008-02-10 20:23:24 -0600549 port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 return 1;
551 }
552
Mike Isely3d6aa322008-02-10 20:23:24 -0600553 priv = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 priv->chiptype = CT_EARTHMATE;
Mike Isely3416eaa2008-02-10 20:23:19 -0600555 /* All Earthmate devices use the separated-count packet
556 format! Idiotic. */
557 priv->pkt_fmt = packet_format_1;
Alan Cox813a2242008-07-22 11:10:36 +0100558 if (serial->dev->descriptor.idProduct !=
559 cpu_to_le16(PRODUCT_ID_EARTHMATEUSB)) {
Mike Isely3d6aa322008-02-10 20:23:24 -0600560 /* The old original USB Earthmate seemed able to
561 handle GET_CONFIG requests; everything they've
562 produced since that time crashes if this command is
563 attempted :-( */
564 dbg("%s - Marking this device as unsafe for GET_CONFIG "
565 "commands", __func__);
566 priv->get_cfg_unsafe = !0;
567 }
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500568
569 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570} /* cypress_earthmate_startup */
571
572
Alan Cox813a2242008-07-22 11:10:36 +0100573static int cypress_hidcom_startup(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
575 struct cypress_private *priv;
576
Harvey Harrison441b62c2008-03-03 16:08:34 -0800577 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
579 if (generic_startup(serial)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800580 dbg("%s - Failed setting up port %d", __func__,
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500581 serial->port[0]->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 return 1;
583 }
584
585 priv = usb_get_serial_port_data(serial->port[0]);
586 priv->chiptype = CT_CYPHIDCOM;
Alan Cox813a2242008-07-22 11:10:36 +0100587
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500588 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589} /* cypress_hidcom_startup */
590
591
Alan Cox813a2242008-07-22 11:10:36 +0100592static int cypress_ca42v2_startup(struct usb_serial *serial)
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600593{
594 struct cypress_private *priv;
595
Harvey Harrison441b62c2008-03-03 16:08:34 -0800596 dbg("%s", __func__);
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600597
598 if (generic_startup(serial)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800599 dbg("%s - Failed setting up port %d", __func__,
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600600 serial->port[0]->number);
601 return 1;
602 }
603
604 priv = usb_get_serial_port_data(serial->port[0]);
605 priv->chiptype = CT_CA42V2;
606
607 return 0;
608} /* cypress_ca42v2_startup */
609
610
Alan Sternf9c99bb2009-06-02 11:53:55 -0400611static void cypress_release(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
613 struct cypress_private *priv;
614
Alan Cox813a2242008-07-22 11:10:36 +0100615 dbg("%s - port %d", __func__, serial->port[0]->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617 /* all open ports are closed at this point */
618
619 priv = usb_get_serial_port_data(serial->port[0]);
620
621 if (priv) {
622 cypress_buf_free(priv->buf);
623 kfree(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 }
625}
626
627
Alan Coxa509a7e2009-09-19 13:13:26 -0700628static int cypress_open(struct tty_struct *tty, struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629{
630 struct cypress_private *priv = usb_get_serial_port_data(port);
631 struct usb_serial *serial = port->serial;
632 unsigned long flags;
633 int result = 0;
634
Harvey Harrison441b62c2008-03-03 16:08:34 -0800635 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Mike Isely78aef512006-08-29 22:07:11 -0500637 if (!priv->comm_is_ok)
638 return -EIO;
639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 /* clear halts before open */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 usb_clear_halt(serial->dev, 0x81);
642 usb_clear_halt(serial->dev, 0x02);
643
644 spin_lock_irqsave(&priv->lock, flags);
645 /* reset read/write statistics */
646 priv->bytes_in = 0;
647 priv->bytes_out = 0;
648 priv->cmd_count = 0;
649 priv->rx_flags = 0;
650 spin_unlock_irqrestore(&priv->lock, flags);
651
Alan Cox335f8512009-06-11 12:26:29 +0100652 /* Set termios */
Alan Coxfe1ae7f2009-09-19 13:13:33 -0700653 cypress_send(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Alan Cox95da3102008-07-22 11:09:07 +0100655 if (tty)
656 cypress_set_termios(tty, port, &priv->tmp_termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
658 /* setup the port and start reading from the device */
Alan Cox813a2242008-07-22 11:10:36 +0100659 if (!port->interrupt_in_urb) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700660 dev_err(&port->dev, "%s - interrupt_in_urb is empty!\n",
661 __func__);
Alan Cox813a2242008-07-22 11:10:36 +0100662 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 }
664
665 usb_fill_int_urb(port->interrupt_in_urb, serial->dev,
666 usb_rcvintpipe(serial->dev, port->interrupt_in_endpointAddress),
Alan Cox813a2242008-07-22 11:10:36 +0100667 port->interrupt_in_urb->transfer_buffer,
668 port->interrupt_in_urb->transfer_buffer_length,
Mike Isely0257fa92006-08-29 22:06:59 -0500669 cypress_read_int_callback, port, priv->read_urb_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
671
Alan Cox813a2242008-07-22 11:10:36 +0100672 if (result) {
673 dev_err(&port->dev,
674 "%s - failed submitting read urb, error %d\n",
675 __func__, result);
Mike Isely78aef512006-08-29 22:07:11 -0500676 cypress_set_dead(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 }
Alan Cox335f8512009-06-11 12:26:29 +0100678 port->port.drain_delay = 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 return result;
680} /* cypress_open */
681
Alan Cox335f8512009-06-11 12:26:29 +0100682static void cypress_dtr_rts(struct usb_serial_port *port, int on)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
684 struct cypress_private *priv = usb_get_serial_port_data(port);
Alan Cox335f8512009-06-11 12:26:29 +0100685 /* drop dtr and rts */
686 priv = usb_get_serial_port_data(port);
687 spin_lock_irq(&priv->lock);
688 if (on == 0)
689 priv->line_control = 0;
690 else
691 priv->line_control = CONTROL_DTR | CONTROL_RTS;
692 priv->cmd_ctrl = 1;
693 spin_unlock_irq(&priv->lock);
694 cypress_write(NULL, port, NULL, 0);
695}
696
697static void cypress_close(struct usb_serial_port *port)
698{
699 struct cypress_private *priv = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Harvey Harrison441b62c2008-03-03 16:08:34 -0800701 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100703 /* writing is potentially harmful, lock must be taken */
704 mutex_lock(&port->serial->disc_mutex);
705 if (port->serial->disconnected) {
706 mutex_unlock(&port->serial->disc_mutex);
707 return;
708 }
Alan Cox335f8512009-06-11 12:26:29 +0100709 cypress_buf_clear(priv->buf);
Harvey Harrison441b62c2008-03-03 16:08:34 -0800710 dbg("%s - stopping urbs", __func__);
Alan Cox813a2242008-07-22 11:10:36 +0100711 usb_kill_urb(port->interrupt_in_urb);
712 usb_kill_urb(port->interrupt_out_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
715 if (stats)
Alan Cox813a2242008-07-22 11:10:36 +0100716 dev_info(&port->dev, "Statistics: %d Bytes In | %d Bytes Out | %d Commands Issued\n",
717 priv->bytes_in, priv->bytes_out, priv->cmd_count);
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100718 mutex_unlock(&port->serial->disc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719} /* cypress_close */
720
721
Alan Cox95da3102008-07-22 11:09:07 +0100722static int cypress_write(struct tty_struct *tty, struct usb_serial_port *port,
723 const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724{
725 struct cypress_private *priv = usb_get_serial_port_data(port);
726 unsigned long flags;
Alan Cox813a2242008-07-22 11:10:36 +0100727
Harvey Harrison441b62c2008-03-03 16:08:34 -0800728 dbg("%s - port %d, %d bytes", __func__, port->number, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
730 /* line control commands, which need to be executed immediately,
731 are not put into the buffer for obvious reasons.
732 */
733 if (priv->cmd_ctrl) {
734 count = 0;
735 goto finish;
736 }
Alan Cox813a2242008-07-22 11:10:36 +0100737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 if (!count)
739 return count;
Alan Cox813a2242008-07-22 11:10:36 +0100740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 spin_lock_irqsave(&priv->lock, flags);
742 count = cypress_buf_put(priv->buf, buf, count);
743 spin_unlock_irqrestore(&priv->lock, flags);
744
745finish:
746 cypress_send(port);
747
748 return count;
749} /* cypress_write */
750
751
752static void cypress_send(struct usb_serial_port *port)
753{
754 int count = 0, result, offset, actual_size;
755 struct cypress_private *priv = usb_get_serial_port_data(port);
756 unsigned long flags;
Alan Cox813a2242008-07-22 11:10:36 +0100757
Mike Isely78aef512006-08-29 22:07:11 -0500758 if (!priv->comm_is_ok)
759 return;
760
Harvey Harrison441b62c2008-03-03 16:08:34 -0800761 dbg("%s - port %d", __func__, port->number);
Alan Cox813a2242008-07-22 11:10:36 +0100762 dbg("%s - interrupt out size is %d", __func__,
763 port->interrupt_out_size);
764
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 spin_lock_irqsave(&priv->lock, flags);
766 if (priv->write_urb_in_use) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800767 dbg("%s - can't write, urb in use", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 spin_unlock_irqrestore(&priv->lock, flags);
769 return;
770 }
771 spin_unlock_irqrestore(&priv->lock, flags);
772
773 /* clear buffer */
Alan Cox813a2242008-07-22 11:10:36 +0100774 memset(port->interrupt_out_urb->transfer_buffer, 0,
775 port->interrupt_out_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
777 spin_lock_irqsave(&priv->lock, flags);
Mike Isely3416eaa2008-02-10 20:23:19 -0600778 switch (priv->pkt_fmt) {
779 default:
780 case packet_format_1:
781 /* this is for the CY7C64013... */
782 offset = 2;
783 port->interrupt_out_buffer[0] = priv->line_control;
784 break;
785 case packet_format_2:
786 /* this is for the CY7C63743... */
787 offset = 1;
788 port->interrupt_out_buffer[0] = priv->line_control;
789 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 }
791
792 if (priv->line_control & CONTROL_RESET)
793 priv->line_control &= ~CONTROL_RESET;
794
795 if (priv->cmd_ctrl) {
796 priv->cmd_count++;
Harvey Harrison441b62c2008-03-03 16:08:34 -0800797 dbg("%s - line control command being issued", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 spin_unlock_irqrestore(&priv->lock, flags);
799 goto send;
800 } else
801 spin_unlock_irqrestore(&priv->lock, flags);
802
803 count = cypress_buf_get(priv->buf, &port->interrupt_out_buffer[offset],
804 port->interrupt_out_size-offset);
805
Alan Cox813a2242008-07-22 11:10:36 +0100806 if (count == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Mike Isely3416eaa2008-02-10 20:23:19 -0600809 switch (priv->pkt_fmt) {
810 default:
811 case packet_format_1:
812 port->interrupt_out_buffer[1] = count;
813 break;
814 case packet_format_2:
815 port->interrupt_out_buffer[0] |= count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 }
817
Harvey Harrison441b62c2008-03-03 16:08:34 -0800818 dbg("%s - count is %d", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
820send:
821 spin_lock_irqsave(&priv->lock, flags);
822 priv->write_urb_in_use = 1;
823 spin_unlock_irqrestore(&priv->lock, flags);
824
825 if (priv->cmd_ctrl)
826 actual_size = 1;
827 else
Mike Isely3416eaa2008-02-10 20:23:19 -0600828 actual_size = count +
829 (priv->pkt_fmt == packet_format_1 ? 2 : 1);
830
Alan Cox813a2242008-07-22 11:10:36 +0100831 usb_serial_debug_data(debug, &port->dev, __func__,
832 port->interrupt_out_size,
833 port->interrupt_out_urb->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Mike Isely9aa8dae2006-08-29 22:07:04 -0500835 usb_fill_int_urb(port->interrupt_out_urb, port->serial->dev,
836 usb_sndintpipe(port->serial->dev, port->interrupt_out_endpointAddress),
837 port->interrupt_out_buffer, port->interrupt_out_size,
838 cypress_write_int_callback, port, priv->write_urb_interval);
Alan Cox813a2242008-07-22 11:10:36 +0100839 result = usb_submit_urb(port->interrupt_out_urb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 if (result) {
Alan Cox813a2242008-07-22 11:10:36 +0100841 dev_err(&port->dev,
842 "%s - failed submitting write urb, error %d\n",
843 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 priv->write_urb_in_use = 0;
Mike Isely78aef512006-08-29 22:07:11 -0500845 cypress_set_dead(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 }
847
848 spin_lock_irqsave(&priv->lock, flags);
Alan Cox813a2242008-07-22 11:10:36 +0100849 if (priv->cmd_ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 priv->cmd_ctrl = 0;
Alan Cox813a2242008-07-22 11:10:36 +0100851
852 /* do not count the line control and size bytes */
853 priv->bytes_out += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 spin_unlock_irqrestore(&priv->lock, flags);
855
Pete Zaitcevcf2c7482006-05-22 21:58:49 -0700856 usb_serial_port_softint(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857} /* cypress_send */
858
859
860/* returns how much space is available in the soft buffer */
Alan Cox95da3102008-07-22 11:09:07 +0100861static int cypress_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862{
Alan Cox95da3102008-07-22 11:09:07 +0100863 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 struct cypress_private *priv = usb_get_serial_port_data(port);
865 int room = 0;
866 unsigned long flags;
867
Harvey Harrison441b62c2008-03-03 16:08:34 -0800868 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
870 spin_lock_irqsave(&priv->lock, flags);
871 room = cypress_buf_space_avail(priv->buf);
872 spin_unlock_irqrestore(&priv->lock, flags);
873
Harvey Harrison441b62c2008-03-03 16:08:34 -0800874 dbg("%s - returns %d", __func__, room);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 return room;
876}
877
878
Alan Cox95da3102008-07-22 11:09:07 +0100879static int cypress_tiocmget(struct tty_struct *tty, struct file *file)
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 __u8 status, control;
884 unsigned int result = 0;
885 unsigned long flags;
Alan Cox813a2242008-07-22 11:10:36 +0100886
Harvey Harrison441b62c2008-03-03 16:08:34 -0800887 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
889 spin_lock_irqsave(&priv->lock, flags);
890 control = priv->line_control;
891 status = priv->current_status;
892 spin_unlock_irqrestore(&priv->lock, flags);
893
894 result = ((control & CONTROL_DTR) ? TIOCM_DTR : 0)
895 | ((control & CONTROL_RTS) ? TIOCM_RTS : 0)
896 | ((status & UART_CTS) ? TIOCM_CTS : 0)
897 | ((status & UART_DSR) ? TIOCM_DSR : 0)
898 | ((status & UART_RI) ? TIOCM_RI : 0)
899 | ((status & UART_CD) ? TIOCM_CD : 0);
900
Harvey Harrison441b62c2008-03-03 16:08:34 -0800901 dbg("%s - result = %x", __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
903 return result;
904}
905
906
Alan Cox95da3102008-07-22 11:09:07 +0100907static int cypress_tiocmset(struct tty_struct *tty, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 unsigned int set, unsigned int clear)
909{
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 unsigned long flags;
Alan Cox813a2242008-07-22 11:10:36 +0100913
Harvey Harrison441b62c2008-03-03 16:08:34 -0800914 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
916 spin_lock_irqsave(&priv->lock, flags);
917 if (set & TIOCM_RTS)
918 priv->line_control |= CONTROL_RTS;
919 if (set & TIOCM_DTR)
920 priv->line_control |= CONTROL_DTR;
921 if (clear & TIOCM_RTS)
922 priv->line_control &= ~CONTROL_RTS;
923 if (clear & TIOCM_DTR)
924 priv->line_control &= ~CONTROL_DTR;
Alan Cox8873aaa2008-03-10 21:59:28 +0000925 priv->cmd_ctrl = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 spin_unlock_irqrestore(&priv->lock, flags);
927
Alan Cox95da3102008-07-22 11:09:07 +0100928 return cypress_write(tty, port, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929}
930
931
Alan Cox813a2242008-07-22 11:10:36 +0100932static int cypress_ioctl(struct tty_struct *tty, struct file *file,
Alan Cox95da3102008-07-22 11:09:07 +0100933 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934{
Alan Cox95da3102008-07-22 11:09:07 +0100935 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 struct cypress_private *priv = usb_get_serial_port_data(port);
937
Harvey Harrison441b62c2008-03-03 16:08:34 -0800938 dbg("%s - port %d, cmd 0x%.4x", __func__, port->number, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
940 switch (cmd) {
Alan Cox813a2242008-07-22 11:10:36 +0100941 /* This code comes from drivers/char/serial.c and ftdi_sio.c */
942 case TIOCMIWAIT:
943 while (priv != NULL) {
944 interruptible_sleep_on(&priv->delta_msr_wait);
945 /* see if a signal did it */
946 if (signal_pending(current))
947 return -ERESTARTSYS;
948 else {
949 char diff = priv->diff_status;
950 if (diff == 0)
951 return -EIO; /* no change => error */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
Alan Cox813a2242008-07-22 11:10:36 +0100953 /* consume all events */
954 priv->diff_status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
Alan Cox813a2242008-07-22 11:10:36 +0100956 /* return 0 if caller wanted to know about
957 these bits */
958 if (((arg & TIOCM_RNG) && (diff & UART_RI)) ||
959 ((arg & TIOCM_DSR) && (diff & UART_DSR)) ||
960 ((arg & TIOCM_CD) && (diff & UART_CD)) ||
961 ((arg & TIOCM_CTS) && (diff & UART_CTS)))
962 return 0;
963 /* otherwise caller can't care less about what
964 * happened, and so we continue to wait for
965 * more events.
966 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 }
Alan Cox813a2242008-07-22 11:10:36 +0100968 }
969 return 0;
970 default:
971 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 }
Harvey Harrison441b62c2008-03-03 16:08:34 -0800973 dbg("%s - arg not supported - it was 0x%04x - check include/asm/ioctls.h", __func__, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 return -ENOIOCTLCMD;
975} /* cypress_ioctl */
976
977
Alan Cox95da3102008-07-22 11:09:07 +0100978static void cypress_set_termios(struct tty_struct *tty,
979 struct usb_serial_port *port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980{
981 struct cypress_private *priv = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 int data_bits, stop_bits, parity_type, parity_enable;
Alan Cox8873aaa2008-03-10 21:59:28 +0000983 unsigned cflag, iflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 unsigned long flags;
985 __u8 oldlines;
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500986 int linechange = 0;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500987
Harvey Harrison441b62c2008-03-03 16:08:34 -0800988 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 spin_lock_irqsave(&priv->lock, flags);
Alan Coxfe1ae7f2009-09-19 13:13:33 -0700991 /* We can't clean this one up as we don't know the device type
992 early enough */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 if (!priv->termios_initialized) {
994 if (priv->chiptype == CT_EARTHMATE) {
995 *(tty->termios) = tty_std_termios;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500996 tty->termios->c_cflag = B4800 | CS8 | CREAD | HUPCL |
997 CLOCAL;
Alan Cox8873aaa2008-03-10 21:59:28 +0000998 tty->termios->c_ispeed = 4800;
999 tty->termios->c_ospeed = 4800;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 } else if (priv->chiptype == CT_CYPHIDCOM) {
1001 *(tty->termios) = tty_std_termios;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001002 tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
1003 CLOCAL;
Alan Cox8873aaa2008-03-10 21:59:28 +00001004 tty->termios->c_ispeed = 9600;
1005 tty->termios->c_ospeed = 9600;
Lonnie Mendeza5c44e22006-03-01 10:45:24 -06001006 } else if (priv->chiptype == CT_CA42V2) {
1007 *(tty->termios) = tty_std_termios;
1008 tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
1009 CLOCAL;
Alan Cox8873aaa2008-03-10 21:59:28 +00001010 tty->termios->c_ispeed = 9600;
1011 tty->termios->c_ospeed = 9600;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 }
1013 priv->termios_initialized = 1;
1014 }
1015 spin_unlock_irqrestore(&priv->lock, flags);
1016
Alan Cox8873aaa2008-03-10 21:59:28 +00001017 /* Unsupported features need clearing */
1018 tty->termios->c_cflag &= ~(CMSPAR|CRTSCTS);
1019
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 cflag = tty->termios->c_cflag;
1021 iflag = tty->termios->c_iflag;
1022
1023 /* check if there are new settings */
1024 if (old_termios) {
Alan Cox8873aaa2008-03-10 21:59:28 +00001025 spin_lock_irqsave(&priv->lock, flags);
1026 priv->tmp_termios = *(tty->termios);
1027 spin_unlock_irqrestore(&priv->lock, flags);
1028 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
1030 /* set number of data bits, parity, stop bits */
1031 /* when parity is disabled the parity type bit is ignored */
1032
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001033 /* 1 means 2 stop bits, 0 means 1 stop bit */
1034 stop_bits = cflag & CSTOPB ? 1 : 0;
1035
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 if (cflag & PARENB) {
1037 parity_enable = 1;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001038 /* 1 means odd parity, 0 means even parity */
1039 parity_type = cflag & PARODD ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 } else
1041 parity_enable = parity_type = 0;
1042
Alan Cox77336822008-07-22 11:10:53 +01001043 switch (cflag & CSIZE) {
1044 case CS5:
1045 data_bits = 0;
1046 break;
1047 case CS6:
1048 data_bits = 1;
1049 break;
1050 case CS7:
1051 data_bits = 2;
1052 break;
1053 case CS8:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 data_bits = 3;
Alan Cox77336822008-07-22 11:10:53 +01001055 break;
1056 default:
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001057 dev_err(&port->dev, "%s - CSIZE was set, but not CS5-CS8\n",
1058 __func__);
Alan Cox77336822008-07-22 11:10:53 +01001059 data_bits = 3;
1060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 spin_lock_irqsave(&priv->lock, flags);
1062 oldlines = priv->line_control;
1063 if ((cflag & CBAUD) == B0) {
1064 /* drop dtr and rts */
Harvey Harrison441b62c2008-03-03 16:08:34 -08001065 dbg("%s - dropping the lines, baud rate 0bps", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
Alan Cox8873aaa2008-03-10 21:59:28 +00001067 } else
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001068 priv->line_control = (CONTROL_DTR | CONTROL_RTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001071 dbg("%s - sending %d stop_bits, %d parity_enable, %d parity_type, "
Harvey Harrison441b62c2008-03-03 16:08:34 -08001072 "%d data_bits (+5)", __func__, stop_bits,
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001073 parity_enable, parity_type, data_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
Alan Cox813a2242008-07-22 11:10:36 +01001075 cypress_serial_control(tty, port, tty_get_baud_rate(tty),
1076 data_bits, stop_bits,
1077 parity_enable, parity_type,
1078 0, CYPRESS_SET_CONFIG);
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001079
1080 /* we perform a CYPRESS_GET_CONFIG so that the current settings are
1081 * filled into the private structure this should confirm that all is
1082 * working if it returns what we just set */
Alan Cox95da3102008-07-22 11:09:07 +01001083 cypress_serial_control(tty, port, 0, 0, 0, 0, 0, 0, CYPRESS_GET_CONFIG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001085 /* Here we can define custom tty settings for devices; the main tty
1086 * termios flag base comes from empeg.c */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001088 spin_lock_irqsave(&priv->lock, flags);
Alan Cox813a2242008-07-22 11:10:36 +01001089 if (priv->chiptype == CT_EARTHMATE && priv->baud_rate == 4800) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001090 dbg("Using custom termios settings for a baud rate of "
1091 "4800bps.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 /* define custom termios settings for NMEA protocol */
1093
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 tty->termios->c_iflag /* input modes - */
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001095 &= ~(IGNBRK /* disable ignore break */
1096 | BRKINT /* disable break causes interrupt */
1097 | PARMRK /* disable mark parity errors */
1098 | ISTRIP /* disable clear high bit of input char */
1099 | INLCR /* disable translate NL to CR */
1100 | IGNCR /* disable ignore CR */
1101 | ICRNL /* disable translate CR to NL */
1102 | IXON); /* disable enable XON/XOFF flow control */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001104 tty->termios->c_oflag /* output modes */
1105 &= ~OPOST; /* disable postprocess output char */
1106
1107 tty->termios->c_lflag /* line discipline modes */
1108 &= ~(ECHO /* disable echo input characters */
1109 | ECHONL /* disable echo new line */
1110 | ICANON /* disable erase, kill, werase, and rprnt
1111 special characters */
1112 | ISIG /* disable interrupt, quit, and suspend
1113 special characters */
1114 | IEXTEN); /* disable non-POSIX special characters */
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001115 } /* CT_CYPHIDCOM: Application should handle this for device */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 linechange = (priv->line_control != oldlines);
1118 spin_unlock_irqrestore(&priv->lock, flags);
1119
1120 /* if necessary, set lines */
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001121 if (linechange) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 priv->cmd_ctrl = 1;
Alan Cox95da3102008-07-22 11:09:07 +01001123 cypress_write(tty, port, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125} /* cypress_set_termios */
1126
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001127
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128/* returns amount of data still left in soft buffer */
Alan Cox95da3102008-07-22 11:09:07 +01001129static int cypress_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130{
Alan Cox95da3102008-07-22 11:09:07 +01001131 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 struct cypress_private *priv = usb_get_serial_port_data(port);
1133 int chars = 0;
1134 unsigned long flags;
1135
Harvey Harrison441b62c2008-03-03 16:08:34 -08001136 dbg("%s - port %d", __func__, port->number);
Alan Cox813a2242008-07-22 11:10:36 +01001137
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 spin_lock_irqsave(&priv->lock, flags);
1139 chars = cypress_buf_data_avail(priv->buf);
1140 spin_unlock_irqrestore(&priv->lock, flags);
1141
Harvey Harrison441b62c2008-03-03 16:08:34 -08001142 dbg("%s - returns %d", __func__, chars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 return chars;
1144}
1145
1146
Alan Cox95da3102008-07-22 11:09:07 +01001147static void cypress_throttle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148{
Alan Cox95da3102008-07-22 11:09:07 +01001149 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 struct cypress_private *priv = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
Harvey Harrison441b62c2008-03-03 16:08:34 -08001152 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
Oliver Neukum63832512009-10-07 10:50:23 +02001154 spin_lock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 priv->rx_flags = THROTTLED;
Oliver Neukum63832512009-10-07 10:50:23 +02001156 spin_unlock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157}
1158
1159
Alan Cox95da3102008-07-22 11:09:07 +01001160static void cypress_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161{
Alan Cox95da3102008-07-22 11:09:07 +01001162 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 struct cypress_private *priv = usb_get_serial_port_data(port);
1164 int actually_throttled, result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
Harvey Harrison441b62c2008-03-03 16:08:34 -08001166 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
Oliver Neukum63832512009-10-07 10:50:23 +02001168 spin_lock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 actually_throttled = priv->rx_flags & ACTUALLY_THROTTLED;
1170 priv->rx_flags = 0;
Oliver Neukum63832512009-10-07 10:50:23 +02001171 spin_unlock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
Mike Isely78aef512006-08-29 22:07:11 -05001173 if (!priv->comm_is_ok)
1174 return;
1175
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 if (actually_throttled) {
1177 port->interrupt_in_urb->dev = port->serial->dev;
1178
Oliver Neukum63832512009-10-07 10:50:23 +02001179 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
Mike Isely78aef512006-08-29 22:07:11 -05001180 if (result) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001181 dev_err(&port->dev, "%s - failed submitting read urb, "
Harvey Harrison441b62c2008-03-03 16:08:34 -08001182 "error %d\n", __func__, result);
Mike Isely78aef512006-08-29 22:07:11 -05001183 cypress_set_dead(port);
1184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 }
1186}
1187
1188
David Howells7d12e782006-10-05 14:55:46 +01001189static void cypress_read_int_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190{
Ming Leicdc97792008-02-24 18:41:47 +08001191 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 struct cypress_private *priv = usb_get_serial_port_data(port);
1193 struct tty_struct *tty;
1194 unsigned char *data = urb->transfer_buffer;
1195 unsigned long flags;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001196 char tty_flag = TTY_NORMAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 int havedata = 0;
1198 int bytes = 0;
1199 int result;
1200 int i = 0;
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001201 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
Harvey Harrison441b62c2008-03-03 16:08:34 -08001203 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001205 switch (status) {
Mike Isely78aef512006-08-29 22:07:11 -05001206 case 0: /* success */
1207 break;
1208 case -ECONNRESET:
1209 case -ENOENT:
1210 case -ESHUTDOWN:
1211 /* precursor to disconnect so just go away */
1212 return;
1213 case -EPIPE:
Alan Stern4d2fae82009-07-09 12:59:57 -04001214 /* Can't call usb_clear_halt while in_interrupt */
1215 /* FALLS THROUGH */
Mike Isely78aef512006-08-29 22:07:11 -05001216 default:
1217 /* something ugly is going on... */
Alan Cox813a2242008-07-22 11:10:36 +01001218 dev_err(&urb->dev->dev,
1219 "%s - unexpected nonzero read status received: %d\n",
1220 __func__, status);
Mike Isely78aef512006-08-29 22:07:11 -05001221 cypress_set_dead(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 return;
1223 }
1224
1225 spin_lock_irqsave(&priv->lock, flags);
1226 if (priv->rx_flags & THROTTLED) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001227 dbg("%s - now throttling", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 priv->rx_flags |= ACTUALLY_THROTTLED;
1229 spin_unlock_irqrestore(&priv->lock, flags);
1230 return;
1231 }
1232 spin_unlock_irqrestore(&priv->lock, flags);
1233
Alan Cox4a90f092008-10-13 10:39:46 +01001234 tty = tty_port_tty_get(&port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 if (!tty) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001236 dbg("%s - bad tty pointer - exiting", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 return;
1238 }
1239
1240 spin_lock_irqsave(&priv->lock, flags);
Mike Isely3416eaa2008-02-10 20:23:19 -06001241 result = urb->actual_length;
1242 switch (priv->pkt_fmt) {
1243 default:
1244 case packet_format_1:
1245 /* This is for the CY7C64013... */
1246 priv->current_status = data[0] & 0xF8;
1247 bytes = data[1] + 2;
1248 i = 2;
1249 if (bytes > 2)
1250 havedata = 1;
1251 break;
1252 case packet_format_2:
1253 /* This is for the CY7C63743... */
1254 priv->current_status = data[0] & 0xF8;
1255 bytes = (data[0] & 0x07) + 1;
1256 i = 1;
1257 if (bytes > 1)
1258 havedata = 1;
1259 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 }
1261 spin_unlock_irqrestore(&priv->lock, flags);
Mike Isely3416eaa2008-02-10 20:23:19 -06001262 if (result < bytes) {
1263 dbg("%s - wrong packet size - received %d bytes but packet "
1264 "said %d bytes", __func__, result, bytes);
1265 goto continue_read;
1266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
Alan Cox813a2242008-07-22 11:10:36 +01001268 usb_serial_debug_data(debug, &port->dev, __func__,
1269 urb->actual_length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
1271 spin_lock_irqsave(&priv->lock, flags);
1272 /* check to see if status has changed */
Mike Isely67683062008-02-10 20:23:28 -06001273 if (priv->current_status != priv->prev_status) {
1274 priv->diff_status |= priv->current_status ^
1275 priv->prev_status;
1276 wake_up_interruptible(&priv->delta_msr_wait);
1277 priv->prev_status = priv->current_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 }
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001279 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001281 /* hangup, as defined in acm.c... this might be a bad place for it
1282 * though */
1283 if (tty && !(tty->termios->c_cflag & CLOCAL) &&
1284 !(priv->current_status & UART_CD)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001285 dbg("%s - calling hangup", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 tty_hangup(tty);
1287 goto continue_read;
1288 }
1289
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001290 /* There is one error bit... I'm assuming it is a parity error
1291 * indicator as the generic firmware will set this bit to 1 if a
1292 * parity error occurs.
1293 * I can not find reference to any other error events. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 spin_lock_irqsave(&priv->lock, flags);
1295 if (priv->current_status & CYP_ERROR) {
1296 spin_unlock_irqrestore(&priv->lock, flags);
1297 tty_flag = TTY_PARITY;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001298 dbg("%s - Parity Error detected", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 } else
1300 spin_unlock_irqrestore(&priv->lock, flags);
1301
1302 /* process read if there is data other than line status */
1303 if (tty && (bytes > i)) {
Alan Cox33f0f882006-01-09 20:54:13 -08001304 bytes = tty_buffer_request_room(tty, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 for (; i < bytes ; ++i) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001306 dbg("pushing byte number %d - %d - %c", i, data[i],
1307 data[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 tty_insert_flip_char(tty, data[i], tty_flag);
1309 }
Alan Cox4a90f092008-10-13 10:39:46 +01001310 tty_flip_buffer_push(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 }
1312
1313 spin_lock_irqsave(&priv->lock, flags);
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001314 /* control and status byte(s) are also counted */
1315 priv->bytes_in += bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 spin_unlock_irqrestore(&priv->lock, flags);
1317
1318continue_read:
Alan Cox4a90f092008-10-13 10:39:46 +01001319 tty_kref_put(tty);
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001320
1321 /* Continue trying to always read... unless the port has closed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
Alan Cox95da3102008-07-22 11:09:07 +01001323 if (port->port.count > 0 && priv->comm_is_ok) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001324 usb_fill_int_urb(port->interrupt_in_urb, port->serial->dev,
1325 usb_rcvintpipe(port->serial->dev,
1326 port->interrupt_in_endpointAddress),
1327 port->interrupt_in_urb->transfer_buffer,
1328 port->interrupt_in_urb->transfer_buffer_length,
Alan Cox813a2242008-07-22 11:10:36 +01001329 cypress_read_int_callback, port,
1330 priv->read_urb_interval);
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001331 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
Mike Isely78aef512006-08-29 22:07:11 -05001332 if (result) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001333 dev_err(&urb->dev->dev, "%s - failed resubmitting "
Harvey Harrison441b62c2008-03-03 16:08:34 -08001334 "read urb, error %d\n", __func__,
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001335 result);
Mike Isely78aef512006-08-29 22:07:11 -05001336 cypress_set_dead(port);
1337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 }
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001339
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 return;
1341} /* cypress_read_int_callback */
1342
1343
David Howells7d12e782006-10-05 14:55:46 +01001344static void cypress_write_int_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345{
Ming Leicdc97792008-02-24 18:41:47 +08001346 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 struct cypress_private *priv = usb_get_serial_port_data(port);
1348 int result;
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001349 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
Harvey Harrison441b62c2008-03-03 16:08:34 -08001351 dbg("%s - port %d", __func__, port->number);
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001352
1353 switch (status) {
Alan Cox813a2242008-07-22 11:10:36 +01001354 case 0:
1355 /* success */
1356 break;
1357 case -ECONNRESET:
1358 case -ENOENT:
1359 case -ESHUTDOWN:
1360 /* this urb is terminated, clean up */
1361 dbg("%s - urb shutting down with status: %d",
1362 __func__, status);
1363 priv->write_urb_in_use = 0;
1364 return;
1365 case -EPIPE: /* no break needed; clear halt and resubmit */
1366 if (!priv->comm_is_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 break;
Alan Cox813a2242008-07-22 11:10:36 +01001368 usb_clear_halt(port->serial->dev, 0x02);
1369 /* error in the urb, so we have to resubmit it */
1370 dbg("%s - nonzero write bulk status received: %d",
1371 __func__, status);
1372 port->interrupt_out_urb->transfer_buffer_length = 1;
1373 port->interrupt_out_urb->dev = port->serial->dev;
1374 result = usb_submit_urb(port->interrupt_out_urb, GFP_ATOMIC);
1375 if (!result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 return;
Alan Cox813a2242008-07-22 11:10:36 +01001377 dev_err(&urb->dev->dev,
1378 "%s - failed resubmitting write urb, error %d\n",
1379 __func__, result);
1380 cypress_set_dead(port);
1381 break;
1382 default:
1383 dev_err(&urb->dev->dev,
1384 "%s - unexpected nonzero write status received: %d\n",
1385 __func__, status);
1386 cypress_set_dead(port);
1387 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 priv->write_urb_in_use = 0;
Alan Cox813a2242008-07-22 11:10:36 +01001390
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 /* send any buffered data */
1392 cypress_send(port);
1393}
1394
1395
1396/*****************************************************************************
1397 * Write buffer functions - buffering code from pl2303 used
1398 *****************************************************************************/
1399
1400/*
1401 * cypress_buf_alloc
1402 *
1403 * Allocate a circular buffer and all associated memory.
1404 */
1405
1406static struct cypress_buf *cypress_buf_alloc(unsigned int size)
1407{
1408
1409 struct cypress_buf *cb;
1410
1411
1412 if (size == 0)
1413 return NULL;
1414
Robert P. J. Day5cbded52006-12-13 00:35:56 -08001415 cb = kmalloc(sizeof(struct cypress_buf), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 if (cb == NULL)
1417 return NULL;
1418
1419 cb->buf_buf = kmalloc(size, GFP_KERNEL);
1420 if (cb->buf_buf == NULL) {
1421 kfree(cb);
1422 return NULL;
1423 }
1424
1425 cb->buf_size = size;
1426 cb->buf_get = cb->buf_put = cb->buf_buf;
1427
1428 return cb;
1429
1430}
1431
1432
1433/*
1434 * cypress_buf_free
1435 *
1436 * Free the buffer and all associated memory.
1437 */
1438
1439static void cypress_buf_free(struct cypress_buf *cb)
1440{
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001441 if (cb) {
1442 kfree(cb->buf_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 kfree(cb);
1444 }
1445}
1446
1447
1448/*
1449 * cypress_buf_clear
1450 *
1451 * Clear out all data in the circular buffer.
1452 */
1453
1454static void cypress_buf_clear(struct cypress_buf *cb)
1455{
1456 if (cb != NULL)
1457 cb->buf_get = cb->buf_put;
1458 /* equivalent to a get of all data available */
1459}
1460
1461
1462/*
1463 * cypress_buf_data_avail
1464 *
1465 * Return the number of bytes of data available in the circular
1466 * buffer.
1467 */
1468
1469static unsigned int cypress_buf_data_avail(struct cypress_buf *cb)
1470{
1471 if (cb != NULL)
Alan Cox813a2242008-07-22 11:10:36 +01001472 return (cb->buf_size + cb->buf_put - cb->buf_get)
1473 % cb->buf_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 else
1475 return 0;
1476}
1477
1478
1479/*
1480 * cypress_buf_space_avail
1481 *
1482 * Return the number of bytes of space available in the circular
1483 * buffer.
1484 */
1485
1486static unsigned int cypress_buf_space_avail(struct cypress_buf *cb)
1487{
1488 if (cb != NULL)
Alan Cox813a2242008-07-22 11:10:36 +01001489 return (cb->buf_size + cb->buf_get - cb->buf_put - 1)
1490 % cb->buf_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 else
1492 return 0;
1493}
1494
1495
1496/*
1497 * cypress_buf_put
1498 *
1499 * Copy data data from a user buffer and put it into the circular buffer.
1500 * Restrict to the amount of space available.
1501 *
1502 * Return the number of bytes copied.
1503 */
1504
1505static unsigned int cypress_buf_put(struct cypress_buf *cb, const char *buf,
1506 unsigned int count)
1507{
1508
1509 unsigned int len;
1510
1511
1512 if (cb == NULL)
1513 return 0;
1514
1515 len = cypress_buf_space_avail(cb);
1516 if (count > len)
1517 count = len;
1518
1519 if (count == 0)
1520 return 0;
1521
1522 len = cb->buf_buf + cb->buf_size - cb->buf_put;
1523 if (count > len) {
1524 memcpy(cb->buf_put, buf, len);
1525 memcpy(cb->buf_buf, buf+len, count - len);
1526 cb->buf_put = cb->buf_buf + count - len;
1527 } else {
1528 memcpy(cb->buf_put, buf, count);
1529 if (count < len)
1530 cb->buf_put += count;
1531 else /* count == len */
1532 cb->buf_put = cb->buf_buf;
1533 }
1534
1535 return count;
1536
1537}
1538
1539
1540/*
1541 * cypress_buf_get
1542 *
1543 * Get data from the circular buffer and copy to the given buffer.
1544 * Restrict to the amount of data available.
1545 *
1546 * Return the number of bytes copied.
1547 */
1548
1549static unsigned int cypress_buf_get(struct cypress_buf *cb, char *buf,
1550 unsigned int count)
1551{
1552
1553 unsigned int len;
1554
1555
1556 if (cb == NULL)
1557 return 0;
1558
1559 len = cypress_buf_data_avail(cb);
1560 if (count > len)
1561 count = len;
1562
1563 if (count == 0)
1564 return 0;
1565
1566 len = cb->buf_buf + cb->buf_size - cb->buf_get;
1567 if (count > len) {
1568 memcpy(buf, cb->buf_get, len);
1569 memcpy(buf+len, cb->buf_buf, count - len);
1570 cb->buf_get = cb->buf_buf + count - len;
1571 } else {
1572 memcpy(buf, cb->buf_get, count);
1573 if (count < len)
1574 cb->buf_get += count;
1575 else /* count == len */
1576 cb->buf_get = cb->buf_buf;
1577 }
1578
1579 return count;
1580
1581}
1582
1583/*****************************************************************************
1584 * Module functions
1585 *****************************************************************************/
1586
1587static int __init cypress_init(void)
1588{
1589 int retval;
Alan Cox813a2242008-07-22 11:10:36 +01001590
Harvey Harrison441b62c2008-03-03 16:08:34 -08001591 dbg("%s", __func__);
Alan Cox813a2242008-07-22 11:10:36 +01001592
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 retval = usb_serial_register(&cypress_earthmate_device);
1594 if (retval)
1595 goto failed_em_register;
1596 retval = usb_serial_register(&cypress_hidcom_device);
1597 if (retval)
1598 goto failed_hidcom_register;
Lonnie Mendeza5c44e22006-03-01 10:45:24 -06001599 retval = usb_serial_register(&cypress_ca42v2_device);
1600 if (retval)
1601 goto failed_ca42v2_register;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 retval = usb_register(&cypress_driver);
1603 if (retval)
1604 goto failed_usb_register;
1605
Greg Kroah-Hartmanc197a8d2008-08-18 13:21:04 -07001606 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
1607 DRIVER_DESC "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
Mariusz Kozlowski2e46b742006-11-17 17:49:22 +01001610failed_usb_register:
1611 usb_serial_deregister(&cypress_ca42v2_device);
1612failed_ca42v2_register:
1613 usb_serial_deregister(&cypress_hidcom_device);
1614failed_hidcom_register:
1615 usb_serial_deregister(&cypress_earthmate_device);
1616failed_em_register:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 return retval;
1618}
1619
1620
Alan Cox813a2242008-07-22 11:10:36 +01001621static void __exit cypress_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622{
Harvey Harrison441b62c2008-03-03 16:08:34 -08001623 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
Alan Cox813a2242008-07-22 11:10:36 +01001625 usb_deregister(&cypress_driver);
1626 usb_serial_deregister(&cypress_earthmate_device);
1627 usb_serial_deregister(&cypress_hidcom_device);
1628 usb_serial_deregister(&cypress_ca42v2_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629}
1630
1631
1632module_init(cypress_init);
1633module_exit(cypress_exit);
1634
Alan Cox813a2242008-07-22 11:10:36 +01001635MODULE_AUTHOR(DRIVER_AUTHOR);
1636MODULE_DESCRIPTION(DRIVER_DESC);
1637MODULE_VERSION(DRIVER_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638MODULE_LICENSE("GPL");
1639
1640module_param(debug, bool, S_IRUGO | S_IWUSR);
1641MODULE_PARM_DESC(debug, "Debug enabled or not");
1642module_param(stats, bool, S_IRUGO | S_IWUSR);
1643MODULE_PARM_DESC(stats, "Enable statistics or not");
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001644module_param(interval, int, S_IRUGO | S_IWUSR);
1645MODULE_PARM_DESC(interval, "Overrides interrupt interval");