blob: b19e16a539e299a684bf86969068b13f5c64ae79 [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>
Johan Hovold0f2c2d72009-12-31 16:48:01 +010069#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#include "cypress_m8.h"
72
73
Mike Frysinger64319dd2009-12-18 16:33:01 -050074static int debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075static int stats;
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -050076static int interval;
Mike Frysingerc3126592009-12-18 16:33:03 -050077static int unstable_bauds;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79/*
80 * Version Information
81 */
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -050082#define DRIVER_VERSION "v1.09"
Linus Torvalds1da177e2005-04-16 15:20:36 -070083#define DRIVER_AUTHOR "Lonnie Mendez <dignome@gmail.com>, Neil Whelchel <koyama@firstlight.net>"
84#define DRIVER_DESC "Cypress USB to Serial Driver"
85
86/* write buffer size defines */
87#define CYPRESS_BUF_SIZE 1024
88#define CYPRESS_CLOSING_WAIT (30*HZ)
89
90static struct usb_device_id id_table_earthmate [] = {
91 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB) },
Lonnie Mendez25b6f082005-05-10 17:09:52 -050092 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB_LT20) },
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 { } /* Terminating entry */
94};
95
96static struct usb_device_id id_table_cyphidcomrs232 [] = {
97 { USB_DEVICE(VENDOR_ID_CYPRESS, PRODUCT_ID_CYPHIDCOM) },
Dmitry Shapin6f6f06e2008-03-04 15:25:10 -080098 { USB_DEVICE(VENDOR_ID_POWERCOM, PRODUCT_ID_UPS) },
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 { } /* Terminating entry */
100};
101
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600102static struct usb_device_id id_table_nokiaca42v2 [] = {
103 { USB_DEVICE(VENDOR_ID_DAZZLE, PRODUCT_ID_CA42) },
104 { } /* Terminating entry */
105};
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107static struct usb_device_id id_table_combined [] = {
108 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB) },
Lonnie Mendez25b6f082005-05-10 17:09:52 -0500109 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB_LT20) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 { USB_DEVICE(VENDOR_ID_CYPRESS, PRODUCT_ID_CYPHIDCOM) },
Dmitry Shapin6f6f06e2008-03-04 15:25:10 -0800111 { USB_DEVICE(VENDOR_ID_POWERCOM, PRODUCT_ID_UPS) },
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600112 { USB_DEVICE(VENDOR_ID_DAZZLE, PRODUCT_ID_CA42) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 { } /* Terminating entry */
114};
115
Alan Cox813a2242008-07-22 11:10:36 +0100116MODULE_DEVICE_TABLE(usb, id_table_combined);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118static struct usb_driver cypress_driver = {
119 .name = "cypress",
120 .probe = usb_serial_probe,
121 .disconnect = usb_serial_disconnect,
122 .id_table = id_table_combined,
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800123 .no_dynamic_id = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124};
125
Mike Isely3416eaa2008-02-10 20:23:19 -0600126enum packet_format {
127 packet_format_1, /* b0:status, b1:payload count */
128 packet_format_2 /* b0[7:3]:status, b0[2:0]:payload count */
129};
130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131struct cypress_private {
132 spinlock_t lock; /* private lock */
133 int chiptype; /* identifier of device, for quirks/etc */
134 int bytes_in; /* used for statistics */
135 int bytes_out; /* used for statistics */
136 int cmd_count; /* used for statistics */
137 int cmd_ctrl; /* always set this to 1 before issuing a command */
138 struct cypress_buf *buf; /* write buffer */
139 int write_urb_in_use; /* write urb in use indicator */
Mike Isely0257fa92006-08-29 22:06:59 -0500140 int write_urb_interval; /* interval to use for write urb */
141 int read_urb_interval; /* interval to use for read urb */
Mike Isely78aef512006-08-29 22:07:11 -0500142 int comm_is_ok; /* true if communication is (still) ok */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 int termios_initialized;
144 __u8 line_control; /* holds dtr / rts value */
145 __u8 current_status; /* received from last read - info on dsr,cts,cd,ri,etc */
146 __u8 current_config; /* stores the current configuration byte */
147 __u8 rx_flags; /* throttling - used from whiteheat/ftdi_sio */
Mike Isely3416eaa2008-02-10 20:23:19 -0600148 enum packet_format pkt_fmt; /* format to use for packet send / receive */
Mike Isely3d6aa322008-02-10 20:23:24 -0600149 int get_cfg_unsafe; /* If true, the CYPRESS_GET_CONFIG is unsafe */
Alan Cox813a2242008-07-22 11:10:36 +0100150 int baud_rate; /* stores current baud rate in
151 integer form */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 int isthrottled; /* if throttled, discard reads */
153 wait_queue_head_t delta_msr_wait; /* used for TIOCMIWAIT */
154 char prev_status, diff_status; /* used for TIOCMIWAIT */
Alan Cox813a2242008-07-22 11:10:36 +0100155 /* we pass a pointer to this as the arguement sent to
156 cypress_set_termios old_termios */
Alan Cox606d0992006-12-08 02:38:45 -0800157 struct ktermios tmp_termios; /* stores the old termios settings */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158};
159
160/* write buffer structure */
161struct cypress_buf {
162 unsigned int buf_size;
163 char *buf_buf;
164 char *buf_get;
165 char *buf_put;
166};
167
168/* function prototypes for the Cypress USB to serial device */
Alan Cox813a2242008-07-22 11:10:36 +0100169static int cypress_earthmate_startup(struct usb_serial *serial);
170static int cypress_hidcom_startup(struct usb_serial *serial);
171static int cypress_ca42v2_startup(struct usb_serial *serial);
Alan Sternf9c99bb2009-06-02 11:53:55 -0400172static void cypress_release(struct usb_serial *serial);
Alan Coxa509a7e2009-09-19 13:13:26 -0700173static int cypress_open(struct tty_struct *tty, struct usb_serial_port *port);
Alan Cox335f8512009-06-11 12:26:29 +0100174static void cypress_close(struct usb_serial_port *port);
175static void cypress_dtr_rts(struct usb_serial_port *port, int on);
Alan Cox813a2242008-07-22 11:10:36 +0100176static int cypress_write(struct tty_struct *tty, struct usb_serial_port *port,
177 const unsigned char *buf, int count);
178static void cypress_send(struct usb_serial_port *port);
179static int cypress_write_room(struct tty_struct *tty);
180static int cypress_ioctl(struct tty_struct *tty, struct file *file,
181 unsigned int cmd, unsigned long arg);
182static void cypress_set_termios(struct tty_struct *tty,
183 struct usb_serial_port *port, struct ktermios *old);
184static int cypress_tiocmget(struct tty_struct *tty, struct file *file);
185static int cypress_tiocmset(struct tty_struct *tty, struct file *file,
186 unsigned int set, unsigned int clear);
187static int cypress_chars_in_buffer(struct tty_struct *tty);
188static void cypress_throttle(struct tty_struct *tty);
189static void cypress_unthrottle(struct tty_struct *tty);
190static void cypress_set_dead(struct usb_serial_port *port);
191static void cypress_read_int_callback(struct urb *urb);
192static void cypress_write_int_callback(struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193/* write buffer functions */
194static struct cypress_buf *cypress_buf_alloc(unsigned int size);
Alan Cox813a2242008-07-22 11:10:36 +0100195static void cypress_buf_free(struct cypress_buf *cb);
196static void cypress_buf_clear(struct cypress_buf *cb);
197static unsigned int cypress_buf_data_avail(struct cypress_buf *cb);
198static unsigned int cypress_buf_space_avail(struct cypress_buf *cb);
199static unsigned int cypress_buf_put(struct cypress_buf *cb,
200 const char *buf, unsigned int count);
201static unsigned int cypress_buf_get(struct cypress_buf *cb,
202 char *buf, unsigned int count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
204
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700205static struct usb_serial_driver cypress_earthmate_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700206 .driver = {
207 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700208 .name = "earthmate",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700209 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700210 .description = "DeLorme Earthmate USB",
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100211 .usb_driver = &cypress_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 .id_table = id_table_earthmate,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 .num_ports = 1,
214 .attach = cypress_earthmate_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -0400215 .release = cypress_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 .open = cypress_open,
217 .close = cypress_close,
Alan Cox335f8512009-06-11 12:26:29 +0100218 .dtr_rts = cypress_dtr_rts,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 .write = cypress_write,
220 .write_room = cypress_write_room,
221 .ioctl = cypress_ioctl,
222 .set_termios = cypress_set_termios,
223 .tiocmget = cypress_tiocmget,
224 .tiocmset = cypress_tiocmset,
225 .chars_in_buffer = cypress_chars_in_buffer,
226 .throttle = cypress_throttle,
227 .unthrottle = cypress_unthrottle,
228 .read_int_callback = cypress_read_int_callback,
229 .write_int_callback = cypress_write_int_callback,
230};
231
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700232static struct usb_serial_driver cypress_hidcom_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700233 .driver = {
234 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700235 .name = "cyphidcom",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700236 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700237 .description = "HID->COM RS232 Adapter",
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100238 .usb_driver = &cypress_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 .id_table = id_table_cyphidcomrs232,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 .num_ports = 1,
241 .attach = cypress_hidcom_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -0400242 .release = cypress_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 .open = cypress_open,
244 .close = cypress_close,
Alan Cox335f8512009-06-11 12:26:29 +0100245 .dtr_rts = cypress_dtr_rts,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 .write = cypress_write,
247 .write_room = cypress_write_room,
248 .ioctl = cypress_ioctl,
249 .set_termios = cypress_set_termios,
250 .tiocmget = cypress_tiocmget,
251 .tiocmset = cypress_tiocmset,
252 .chars_in_buffer = cypress_chars_in_buffer,
253 .throttle = cypress_throttle,
254 .unthrottle = cypress_unthrottle,
255 .read_int_callback = cypress_read_int_callback,
256 .write_int_callback = cypress_write_int_callback,
257};
258
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600259static struct usb_serial_driver cypress_ca42v2_device = {
260 .driver = {
261 .owner = THIS_MODULE,
Alan Cox813a2242008-07-22 11:10:36 +0100262 .name = "nokiaca42v2",
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600263 },
264 .description = "Nokia CA-42 V2 Adapter",
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100265 .usb_driver = &cypress_driver,
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600266 .id_table = id_table_nokiaca42v2,
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600267 .num_ports = 1,
268 .attach = cypress_ca42v2_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -0400269 .release = cypress_release,
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600270 .open = cypress_open,
271 .close = cypress_close,
Alan Cox335f8512009-06-11 12:26:29 +0100272 .dtr_rts = cypress_dtr_rts,
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600273 .write = cypress_write,
274 .write_room = cypress_write_room,
275 .ioctl = cypress_ioctl,
276 .set_termios = cypress_set_termios,
277 .tiocmget = cypress_tiocmget,
278 .tiocmset = cypress_tiocmset,
279 .chars_in_buffer = cypress_chars_in_buffer,
280 .throttle = cypress_throttle,
281 .unthrottle = cypress_unthrottle,
282 .read_int_callback = cypress_read_int_callback,
283 .write_int_callback = cypress_write_int_callback,
284};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286/*****************************************************************************
287 * Cypress serial helper functions
288 *****************************************************************************/
289
290
Alan Cox8873aaa2008-03-10 21:59:28 +0000291static int analyze_baud_rate(struct usb_serial_port *port, speed_t new_rate)
Mike Isely92983c22008-02-10 20:23:32 -0600292{
Mike Isely92983c22008-02-10 20:23:32 -0600293 struct cypress_private *priv;
294 priv = usb_get_serial_port_data(port);
295
Mike Frysingerc3126592009-12-18 16:33:03 -0500296 if (unstable_bauds)
297 return new_rate;
298
Mike Isely92983c22008-02-10 20:23:32 -0600299 /*
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;
Johan Hovold09546442009-12-28 23:01:48 +0100348 u8 *feature_buffer;
349 const unsigned int feature_len = 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 unsigned long flags;
351
Harvey Harrison441b62c2008-03-03 16:08:34 -0800352 dbg("%s", __func__);
Alan Cox813a2242008-07-22 11:10:36 +0100353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 priv = usb_get_serial_port_data(port);
355
Mike Isely78aef512006-08-29 22:07:11 -0500356 if (!priv->comm_is_ok)
357 return -ENODEV;
358
Johan Hovold09546442009-12-28 23:01:48 +0100359 feature_buffer = kcalloc(feature_len, sizeof(u8), GFP_KERNEL);
360 if (!feature_buffer)
361 return -ENOMEM;
362
Alan Cox813a2242008-07-22 11:10:36 +0100363 switch (cypress_request_type) {
364 case CYPRESS_SET_CONFIG:
Alan Cox813a2242008-07-22 11:10:36 +0100365 /* 0 means 'Hang up' so doesn't change the true bit rate */
Mike Frysinger2805eb12009-12-18 16:33:02 -0500366 new_baudrate = priv->baud_rate;
367 if (baud_rate && baud_rate != priv->baud_rate) {
Alan Cox813a2242008-07-22 11:10:36 +0100368 dbg("%s - baud rate is changing", __func__);
369 retval = analyze_baud_rate(port, baud_rate);
Mike Frysinger2805eb12009-12-18 16:33:02 -0500370 if (retval >= 0) {
Alan Cox813a2242008-07-22 11:10:36 +0100371 new_baudrate = retval;
372 dbg("%s - New baud rate set to %d",
373 __func__, new_baudrate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 }
Alan Cox813a2242008-07-22 11:10:36 +0100375 }
376 dbg("%s - baud rate is being sent as %d",
377 __func__, new_baudrate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Alan Cox813a2242008-07-22 11:10:36 +0100379 /* fill the feature_buffer with new configuration */
Johan Hovold0f2c2d72009-12-31 16:48:01 +0100380 put_unaligned_le32(new_baudrate, feature_buffer);
Alan Cox813a2242008-07-22 11:10:36 +0100381 feature_buffer[4] |= data_bits; /* assign data bits in 2 bit space ( max 3 ) */
382 /* 1 bit gap */
383 feature_buffer[4] |= (stop_bits << 3); /* assign stop bits in 1 bit space */
384 feature_buffer[4] |= (parity_enable << 4); /* assign parity flag in 1 bit space */
385 feature_buffer[4] |= (parity_type << 5); /* assign parity type in 1 bit space */
386 /* 1 bit gap */
387 feature_buffer[4] |= (reset << 7); /* assign reset at end of byte, 1 bit space */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Alan Cox813a2242008-07-22 11:10:36 +0100389 dbg("%s - device is being sent this feature report:",
390 __func__);
391 dbg("%s - %02X - %02X - %02X - %02X - %02X", __func__,
392 feature_buffer[0], feature_buffer[1],
393 feature_buffer[2], feature_buffer[3],
394 feature_buffer[4]);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500395
Alan Cox813a2242008-07-22 11:10:36 +0100396 do {
397 retval = usb_control_msg(port->serial->dev,
398 usb_sndctrlpipe(port->serial->dev, 0),
399 HID_REQ_SET_REPORT,
400 USB_DIR_OUT | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
401 0x0300, 0, feature_buffer,
Johan Hovold09546442009-12-28 23:01:48 +0100402 feature_len, 500);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500403
Alan Cox813a2242008-07-22 11:10:36 +0100404 if (tries++ >= 3)
405 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Johan Hovold09546442009-12-28 23:01:48 +0100407 } while (retval != feature_len &&
Alan Cox813a2242008-07-22 11:10:36 +0100408 retval != -ENODEV);
Mike Isely93075542008-02-10 20:23:14 -0600409
Johan Hovold09546442009-12-28 23:01:48 +0100410 if (retval != feature_len) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700411 dev_err(&port->dev, "%s - failed sending serial "
412 "line settings - %d\n", __func__, retval);
Alan Cox813a2242008-07-22 11:10:36 +0100413 cypress_set_dead(port);
414 } else {
415 spin_lock_irqsave(&priv->lock, flags);
416 priv->baud_rate = new_baudrate;
417 priv->current_config = feature_buffer[4];
418 spin_unlock_irqrestore(&priv->lock, flags);
419 /* If we asked for a speed change encode it */
420 if (baud_rate)
421 tty_encode_baud_rate(tty,
422 new_baudrate, new_baudrate);
423 }
424 break;
425 case CYPRESS_GET_CONFIG:
426 if (priv->get_cfg_unsafe) {
427 /* Not implemented for this device,
428 and if we try to do it we're likely
429 to crash the hardware. */
Johan Hovold09546442009-12-28 23:01:48 +0100430 retval = -ENOTTY;
431 goto out;
Alan Cox813a2242008-07-22 11:10:36 +0100432 }
433 dbg("%s - retreiving serial line settings", __func__);
Alan Cox813a2242008-07-22 11:10:36 +0100434 do {
435 retval = usb_control_msg(port->serial->dev,
436 usb_rcvctrlpipe(port->serial->dev, 0),
437 HID_REQ_GET_REPORT,
438 USB_DIR_IN | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
439 0x0300, 0, feature_buffer,
Johan Hovold09546442009-12-28 23:01:48 +0100440 feature_len, 500);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500441
Alan Cox813a2242008-07-22 11:10:36 +0100442 if (tries++ >= 3)
443 break;
Johan Hovold09546442009-12-28 23:01:48 +0100444 } while (retval != feature_len
Alan Cox813a2242008-07-22 11:10:36 +0100445 && retval != -ENODEV);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500446
Johan Hovold09546442009-12-28 23:01:48 +0100447 if (retval != feature_len) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700448 dev_err(&port->dev, "%s - failed to retrieve serial "
449 "line settings - %d\n", __func__, retval);
Alan Cox813a2242008-07-22 11:10:36 +0100450 cypress_set_dead(port);
Johan Hovold09546442009-12-28 23:01:48 +0100451 goto out;
Alan Cox813a2242008-07-22 11:10:36 +0100452 } else {
453 spin_lock_irqsave(&priv->lock, flags);
454 /* store the config in one byte, and later
455 use bit masks to check values */
456 priv->current_config = feature_buffer[4];
Johan Hovold0f2c2d72009-12-31 16:48:01 +0100457 priv->baud_rate = get_unaligned_le32(feature_buffer);
Alan Cox813a2242008-07-22 11:10:36 +0100458 spin_unlock_irqrestore(&priv->lock, flags);
459 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 }
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500461 spin_lock_irqsave(&priv->lock, flags);
462 ++priv->cmd_count;
463 spin_unlock_irqrestore(&priv->lock, flags);
Johan Hovold09546442009-12-28 23:01:48 +0100464out:
465 kfree(feature_buffer);
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 Sternf9c99bb2009-06-02 11:53:55 -0400619static void cypress_release(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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 }
633}
634
635
Alan Coxa509a7e2009-09-19 13:13:26 -0700636static int cypress_open(struct tty_struct *tty, struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
638 struct cypress_private *priv = usb_get_serial_port_data(port);
639 struct usb_serial *serial = port->serial;
640 unsigned long flags;
641 int result = 0;
642
Harvey Harrison441b62c2008-03-03 16:08:34 -0800643 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Mike Isely78aef512006-08-29 22:07:11 -0500645 if (!priv->comm_is_ok)
646 return -EIO;
647
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 /* clear halts before open */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 usb_clear_halt(serial->dev, 0x81);
650 usb_clear_halt(serial->dev, 0x02);
651
652 spin_lock_irqsave(&priv->lock, flags);
653 /* reset read/write statistics */
654 priv->bytes_in = 0;
655 priv->bytes_out = 0;
656 priv->cmd_count = 0;
657 priv->rx_flags = 0;
658 spin_unlock_irqrestore(&priv->lock, flags);
659
Alan Cox335f8512009-06-11 12:26:29 +0100660 /* Set termios */
Alan Coxfe1ae7f2009-09-19 13:13:33 -0700661 cypress_send(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Alan Cox95da3102008-07-22 11:09:07 +0100663 if (tty)
664 cypress_set_termios(tty, port, &priv->tmp_termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
666 /* setup the port and start reading from the device */
Alan Cox813a2242008-07-22 11:10:36 +0100667 if (!port->interrupt_in_urb) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700668 dev_err(&port->dev, "%s - interrupt_in_urb is empty!\n",
669 __func__);
Alan Cox813a2242008-07-22 11:10:36 +0100670 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 }
672
673 usb_fill_int_urb(port->interrupt_in_urb, serial->dev,
674 usb_rcvintpipe(serial->dev, port->interrupt_in_endpointAddress),
Alan Cox813a2242008-07-22 11:10:36 +0100675 port->interrupt_in_urb->transfer_buffer,
676 port->interrupt_in_urb->transfer_buffer_length,
Mike Isely0257fa92006-08-29 22:06:59 -0500677 cypress_read_int_callback, port, priv->read_urb_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
679
Alan Cox813a2242008-07-22 11:10:36 +0100680 if (result) {
681 dev_err(&port->dev,
682 "%s - failed submitting read urb, error %d\n",
683 __func__, result);
Mike Isely78aef512006-08-29 22:07:11 -0500684 cypress_set_dead(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 }
Alan Cox335f8512009-06-11 12:26:29 +0100686 port->port.drain_delay = 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 return result;
688} /* cypress_open */
689
Alan Cox335f8512009-06-11 12:26:29 +0100690static void cypress_dtr_rts(struct usb_serial_port *port, int on)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
692 struct cypress_private *priv = usb_get_serial_port_data(port);
Alan Cox335f8512009-06-11 12:26:29 +0100693 /* drop dtr and rts */
694 priv = usb_get_serial_port_data(port);
695 spin_lock_irq(&priv->lock);
696 if (on == 0)
697 priv->line_control = 0;
698 else
699 priv->line_control = CONTROL_DTR | CONTROL_RTS;
700 priv->cmd_ctrl = 1;
701 spin_unlock_irq(&priv->lock);
702 cypress_write(NULL, port, NULL, 0);
703}
704
705static void cypress_close(struct usb_serial_port *port)
706{
707 struct cypress_private *priv = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Harvey Harrison441b62c2008-03-03 16:08:34 -0800709 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100711 /* writing is potentially harmful, lock must be taken */
712 mutex_lock(&port->serial->disc_mutex);
713 if (port->serial->disconnected) {
714 mutex_unlock(&port->serial->disc_mutex);
715 return;
716 }
Alan Cox335f8512009-06-11 12:26:29 +0100717 cypress_buf_clear(priv->buf);
Harvey Harrison441b62c2008-03-03 16:08:34 -0800718 dbg("%s - stopping urbs", __func__);
Alan Cox813a2242008-07-22 11:10:36 +0100719 usb_kill_urb(port->interrupt_in_urb);
720 usb_kill_urb(port->interrupt_out_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723 if (stats)
Alan Cox813a2242008-07-22 11:10:36 +0100724 dev_info(&port->dev, "Statistics: %d Bytes In | %d Bytes Out | %d Commands Issued\n",
725 priv->bytes_in, priv->bytes_out, priv->cmd_count);
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100726 mutex_unlock(&port->serial->disc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727} /* cypress_close */
728
729
Alan Cox95da3102008-07-22 11:09:07 +0100730static int cypress_write(struct tty_struct *tty, struct usb_serial_port *port,
731 const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732{
733 struct cypress_private *priv = usb_get_serial_port_data(port);
734 unsigned long flags;
Alan Cox813a2242008-07-22 11:10:36 +0100735
Harvey Harrison441b62c2008-03-03 16:08:34 -0800736 dbg("%s - port %d, %d bytes", __func__, port->number, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
738 /* line control commands, which need to be executed immediately,
739 are not put into the buffer for obvious reasons.
740 */
741 if (priv->cmd_ctrl) {
742 count = 0;
743 goto finish;
744 }
Alan Cox813a2242008-07-22 11:10:36 +0100745
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 if (!count)
747 return count;
Alan Cox813a2242008-07-22 11:10:36 +0100748
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 spin_lock_irqsave(&priv->lock, flags);
750 count = cypress_buf_put(priv->buf, buf, count);
751 spin_unlock_irqrestore(&priv->lock, flags);
752
753finish:
754 cypress_send(port);
755
756 return count;
757} /* cypress_write */
758
759
760static void cypress_send(struct usb_serial_port *port)
761{
762 int count = 0, result, offset, actual_size;
763 struct cypress_private *priv = usb_get_serial_port_data(port);
764 unsigned long flags;
Alan Cox813a2242008-07-22 11:10:36 +0100765
Mike Isely78aef512006-08-29 22:07:11 -0500766 if (!priv->comm_is_ok)
767 return;
768
Harvey Harrison441b62c2008-03-03 16:08:34 -0800769 dbg("%s - port %d", __func__, port->number);
Alan Cox813a2242008-07-22 11:10:36 +0100770 dbg("%s - interrupt out size is %d", __func__,
771 port->interrupt_out_size);
772
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 spin_lock_irqsave(&priv->lock, flags);
774 if (priv->write_urb_in_use) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800775 dbg("%s - can't write, urb in use", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 spin_unlock_irqrestore(&priv->lock, flags);
777 return;
778 }
779 spin_unlock_irqrestore(&priv->lock, flags);
780
781 /* clear buffer */
Alan Cox813a2242008-07-22 11:10:36 +0100782 memset(port->interrupt_out_urb->transfer_buffer, 0,
783 port->interrupt_out_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
785 spin_lock_irqsave(&priv->lock, flags);
Mike Isely3416eaa2008-02-10 20:23:19 -0600786 switch (priv->pkt_fmt) {
787 default:
788 case packet_format_1:
789 /* this is for the CY7C64013... */
790 offset = 2;
791 port->interrupt_out_buffer[0] = priv->line_control;
792 break;
793 case packet_format_2:
794 /* this is for the CY7C63743... */
795 offset = 1;
796 port->interrupt_out_buffer[0] = priv->line_control;
797 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 }
799
800 if (priv->line_control & CONTROL_RESET)
801 priv->line_control &= ~CONTROL_RESET;
802
803 if (priv->cmd_ctrl) {
804 priv->cmd_count++;
Harvey Harrison441b62c2008-03-03 16:08:34 -0800805 dbg("%s - line control command being issued", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 spin_unlock_irqrestore(&priv->lock, flags);
807 goto send;
808 } else
809 spin_unlock_irqrestore(&priv->lock, flags);
810
811 count = cypress_buf_get(priv->buf, &port->interrupt_out_buffer[offset],
812 port->interrupt_out_size-offset);
813
Alan Cox813a2242008-07-22 11:10:36 +0100814 if (count == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Mike Isely3416eaa2008-02-10 20:23:19 -0600817 switch (priv->pkt_fmt) {
818 default:
819 case packet_format_1:
820 port->interrupt_out_buffer[1] = count;
821 break;
822 case packet_format_2:
823 port->interrupt_out_buffer[0] |= count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 }
825
Harvey Harrison441b62c2008-03-03 16:08:34 -0800826 dbg("%s - count is %d", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
828send:
829 spin_lock_irqsave(&priv->lock, flags);
830 priv->write_urb_in_use = 1;
831 spin_unlock_irqrestore(&priv->lock, flags);
832
833 if (priv->cmd_ctrl)
834 actual_size = 1;
835 else
Mike Isely3416eaa2008-02-10 20:23:19 -0600836 actual_size = count +
837 (priv->pkt_fmt == packet_format_1 ? 2 : 1);
838
Alan Cox813a2242008-07-22 11:10:36 +0100839 usb_serial_debug_data(debug, &port->dev, __func__,
840 port->interrupt_out_size,
841 port->interrupt_out_urb->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
Mike Isely9aa8dae2006-08-29 22:07:04 -0500843 usb_fill_int_urb(port->interrupt_out_urb, port->serial->dev,
844 usb_sndintpipe(port->serial->dev, port->interrupt_out_endpointAddress),
845 port->interrupt_out_buffer, port->interrupt_out_size,
846 cypress_write_int_callback, port, priv->write_urb_interval);
Alan Cox813a2242008-07-22 11:10:36 +0100847 result = usb_submit_urb(port->interrupt_out_urb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 if (result) {
Alan Cox813a2242008-07-22 11:10:36 +0100849 dev_err(&port->dev,
850 "%s - failed submitting write urb, error %d\n",
851 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 priv->write_urb_in_use = 0;
Mike Isely78aef512006-08-29 22:07:11 -0500853 cypress_set_dead(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 }
855
856 spin_lock_irqsave(&priv->lock, flags);
Alan Cox813a2242008-07-22 11:10:36 +0100857 if (priv->cmd_ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 priv->cmd_ctrl = 0;
Alan Cox813a2242008-07-22 11:10:36 +0100859
860 /* do not count the line control and size bytes */
861 priv->bytes_out += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 spin_unlock_irqrestore(&priv->lock, flags);
863
Pete Zaitcevcf2c7482006-05-22 21:58:49 -0700864 usb_serial_port_softint(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865} /* cypress_send */
866
867
868/* returns how much space is available in the soft buffer */
Alan Cox95da3102008-07-22 11:09:07 +0100869static int cypress_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870{
Alan Cox95da3102008-07-22 11:09:07 +0100871 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 struct cypress_private *priv = usb_get_serial_port_data(port);
873 int room = 0;
874 unsigned long flags;
875
Harvey Harrison441b62c2008-03-03 16:08:34 -0800876 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
878 spin_lock_irqsave(&priv->lock, flags);
879 room = cypress_buf_space_avail(priv->buf);
880 spin_unlock_irqrestore(&priv->lock, flags);
881
Harvey Harrison441b62c2008-03-03 16:08:34 -0800882 dbg("%s - returns %d", __func__, room);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 return room;
884}
885
886
Alan Cox95da3102008-07-22 11:09:07 +0100887static int cypress_tiocmget(struct tty_struct *tty, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888{
Alan Cox95da3102008-07-22 11:09:07 +0100889 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 struct cypress_private *priv = usb_get_serial_port_data(port);
891 __u8 status, control;
892 unsigned int result = 0;
893 unsigned long flags;
Alan Cox813a2242008-07-22 11:10:36 +0100894
Harvey Harrison441b62c2008-03-03 16:08:34 -0800895 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
897 spin_lock_irqsave(&priv->lock, flags);
898 control = priv->line_control;
899 status = priv->current_status;
900 spin_unlock_irqrestore(&priv->lock, flags);
901
902 result = ((control & CONTROL_DTR) ? TIOCM_DTR : 0)
903 | ((control & CONTROL_RTS) ? TIOCM_RTS : 0)
904 | ((status & UART_CTS) ? TIOCM_CTS : 0)
905 | ((status & UART_DSR) ? TIOCM_DSR : 0)
906 | ((status & UART_RI) ? TIOCM_RI : 0)
907 | ((status & UART_CD) ? TIOCM_CD : 0);
908
Harvey Harrison441b62c2008-03-03 16:08:34 -0800909 dbg("%s - result = %x", __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
911 return result;
912}
913
914
Alan Cox95da3102008-07-22 11:09:07 +0100915static int cypress_tiocmset(struct tty_struct *tty, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 unsigned int set, unsigned int clear)
917{
Alan Cox95da3102008-07-22 11:09:07 +0100918 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 struct cypress_private *priv = usb_get_serial_port_data(port);
920 unsigned long flags;
Alan Cox813a2242008-07-22 11:10:36 +0100921
Harvey Harrison441b62c2008-03-03 16:08:34 -0800922 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
924 spin_lock_irqsave(&priv->lock, flags);
925 if (set & TIOCM_RTS)
926 priv->line_control |= CONTROL_RTS;
927 if (set & TIOCM_DTR)
928 priv->line_control |= CONTROL_DTR;
929 if (clear & TIOCM_RTS)
930 priv->line_control &= ~CONTROL_RTS;
931 if (clear & TIOCM_DTR)
932 priv->line_control &= ~CONTROL_DTR;
Alan Cox8873aaa2008-03-10 21:59:28 +0000933 priv->cmd_ctrl = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 spin_unlock_irqrestore(&priv->lock, flags);
935
Alan Cox95da3102008-07-22 11:09:07 +0100936 return cypress_write(tty, port, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937}
938
939
Alan Cox813a2242008-07-22 11:10:36 +0100940static int cypress_ioctl(struct tty_struct *tty, struct file *file,
Alan Cox95da3102008-07-22 11:09:07 +0100941 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942{
Alan Cox95da3102008-07-22 11:09:07 +0100943 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 struct cypress_private *priv = usb_get_serial_port_data(port);
945
Harvey Harrison441b62c2008-03-03 16:08:34 -0800946 dbg("%s - port %d, cmd 0x%.4x", __func__, port->number, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
948 switch (cmd) {
Alan Cox813a2242008-07-22 11:10:36 +0100949 /* This code comes from drivers/char/serial.c and ftdi_sio.c */
950 case TIOCMIWAIT:
951 while (priv != NULL) {
952 interruptible_sleep_on(&priv->delta_msr_wait);
953 /* see if a signal did it */
954 if (signal_pending(current))
955 return -ERESTARTSYS;
956 else {
957 char diff = priv->diff_status;
958 if (diff == 0)
959 return -EIO; /* no change => error */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
Alan Cox813a2242008-07-22 11:10:36 +0100961 /* consume all events */
962 priv->diff_status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
Alan Cox813a2242008-07-22 11:10:36 +0100964 /* return 0 if caller wanted to know about
965 these bits */
966 if (((arg & TIOCM_RNG) && (diff & UART_RI)) ||
967 ((arg & TIOCM_DSR) && (diff & UART_DSR)) ||
968 ((arg & TIOCM_CD) && (diff & UART_CD)) ||
969 ((arg & TIOCM_CTS) && (diff & UART_CTS)))
970 return 0;
971 /* otherwise caller can't care less about what
972 * happened, and so we continue to wait for
973 * more events.
974 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 }
Alan Cox813a2242008-07-22 11:10:36 +0100976 }
977 return 0;
978 default:
979 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 }
Harvey Harrison441b62c2008-03-03 16:08:34 -0800981 dbg("%s - arg not supported - it was 0x%04x - check include/asm/ioctls.h", __func__, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 return -ENOIOCTLCMD;
983} /* cypress_ioctl */
984
985
Alan Cox95da3102008-07-22 11:09:07 +0100986static void cypress_set_termios(struct tty_struct *tty,
987 struct usb_serial_port *port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988{
989 struct cypress_private *priv = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 int data_bits, stop_bits, parity_type, parity_enable;
Alan Cox8873aaa2008-03-10 21:59:28 +0000991 unsigned cflag, iflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 unsigned long flags;
993 __u8 oldlines;
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500994 int linechange = 0;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500995
Harvey Harrison441b62c2008-03-03 16:08:34 -0800996 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 spin_lock_irqsave(&priv->lock, flags);
Alan Coxfe1ae7f2009-09-19 13:13:33 -0700999 /* We can't clean this one up as we don't know the device type
1000 early enough */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 if (!priv->termios_initialized) {
1002 if (priv->chiptype == CT_EARTHMATE) {
1003 *(tty->termios) = tty_std_termios;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001004 tty->termios->c_cflag = B4800 | CS8 | CREAD | HUPCL |
1005 CLOCAL;
Alan Cox8873aaa2008-03-10 21:59:28 +00001006 tty->termios->c_ispeed = 4800;
1007 tty->termios->c_ospeed = 4800;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 } else if (priv->chiptype == CT_CYPHIDCOM) {
1009 *(tty->termios) = tty_std_termios;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001010 tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
1011 CLOCAL;
Alan Cox8873aaa2008-03-10 21:59:28 +00001012 tty->termios->c_ispeed = 9600;
1013 tty->termios->c_ospeed = 9600;
Lonnie Mendeza5c44e22006-03-01 10:45:24 -06001014 } else if (priv->chiptype == CT_CA42V2) {
1015 *(tty->termios) = tty_std_termios;
1016 tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
1017 CLOCAL;
Alan Cox8873aaa2008-03-10 21:59:28 +00001018 tty->termios->c_ispeed = 9600;
1019 tty->termios->c_ospeed = 9600;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 }
1021 priv->termios_initialized = 1;
1022 }
1023 spin_unlock_irqrestore(&priv->lock, flags);
1024
Alan Cox8873aaa2008-03-10 21:59:28 +00001025 /* Unsupported features need clearing */
1026 tty->termios->c_cflag &= ~(CMSPAR|CRTSCTS);
1027
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 cflag = tty->termios->c_cflag;
1029 iflag = tty->termios->c_iflag;
1030
1031 /* check if there are new settings */
1032 if (old_termios) {
Alan Cox8873aaa2008-03-10 21:59:28 +00001033 spin_lock_irqsave(&priv->lock, flags);
1034 priv->tmp_termios = *(tty->termios);
1035 spin_unlock_irqrestore(&priv->lock, flags);
1036 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
1038 /* set number of data bits, parity, stop bits */
1039 /* when parity is disabled the parity type bit is ignored */
1040
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001041 /* 1 means 2 stop bits, 0 means 1 stop bit */
1042 stop_bits = cflag & CSTOPB ? 1 : 0;
1043
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 if (cflag & PARENB) {
1045 parity_enable = 1;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001046 /* 1 means odd parity, 0 means even parity */
1047 parity_type = cflag & PARODD ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 } else
1049 parity_enable = parity_type = 0;
1050
Alan Cox77336822008-07-22 11:10:53 +01001051 switch (cflag & CSIZE) {
1052 case CS5:
1053 data_bits = 0;
1054 break;
1055 case CS6:
1056 data_bits = 1;
1057 break;
1058 case CS7:
1059 data_bits = 2;
1060 break;
1061 case CS8:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 data_bits = 3;
Alan Cox77336822008-07-22 11:10:53 +01001063 break;
1064 default:
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001065 dev_err(&port->dev, "%s - CSIZE was set, but not CS5-CS8\n",
1066 __func__);
Alan Cox77336822008-07-22 11:10:53 +01001067 data_bits = 3;
1068 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 spin_lock_irqsave(&priv->lock, flags);
1070 oldlines = priv->line_control;
1071 if ((cflag & CBAUD) == B0) {
1072 /* drop dtr and rts */
Harvey Harrison441b62c2008-03-03 16:08:34 -08001073 dbg("%s - dropping the lines, baud rate 0bps", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
Alan Cox8873aaa2008-03-10 21:59:28 +00001075 } else
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001076 priv->line_control = (CONTROL_DTR | CONTROL_RTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001079 dbg("%s - sending %d stop_bits, %d parity_enable, %d parity_type, "
Harvey Harrison441b62c2008-03-03 16:08:34 -08001080 "%d data_bits (+5)", __func__, stop_bits,
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001081 parity_enable, parity_type, data_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
Alan Cox813a2242008-07-22 11:10:36 +01001083 cypress_serial_control(tty, port, tty_get_baud_rate(tty),
1084 data_bits, stop_bits,
1085 parity_enable, parity_type,
1086 0, CYPRESS_SET_CONFIG);
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001087
1088 /* we perform a CYPRESS_GET_CONFIG so that the current settings are
1089 * filled into the private structure this should confirm that all is
1090 * working if it returns what we just set */
Alan Cox95da3102008-07-22 11:09:07 +01001091 cypress_serial_control(tty, port, 0, 0, 0, 0, 0, 0, CYPRESS_GET_CONFIG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001093 /* Here we can define custom tty settings for devices; the main tty
1094 * termios flag base comes from empeg.c */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001096 spin_lock_irqsave(&priv->lock, flags);
Alan Cox813a2242008-07-22 11:10:36 +01001097 if (priv->chiptype == CT_EARTHMATE && priv->baud_rate == 4800) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001098 dbg("Using custom termios settings for a baud rate of "
1099 "4800bps.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 /* define custom termios settings for NMEA protocol */
1101
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 tty->termios->c_iflag /* input modes - */
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001103 &= ~(IGNBRK /* disable ignore break */
1104 | BRKINT /* disable break causes interrupt */
1105 | PARMRK /* disable mark parity errors */
1106 | ISTRIP /* disable clear high bit of input char */
1107 | INLCR /* disable translate NL to CR */
1108 | IGNCR /* disable ignore CR */
1109 | ICRNL /* disable translate CR to NL */
1110 | IXON); /* disable enable XON/XOFF flow control */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001112 tty->termios->c_oflag /* output modes */
1113 &= ~OPOST; /* disable postprocess output char */
1114
1115 tty->termios->c_lflag /* line discipline modes */
1116 &= ~(ECHO /* disable echo input characters */
1117 | ECHONL /* disable echo new line */
1118 | ICANON /* disable erase, kill, werase, and rprnt
1119 special characters */
1120 | ISIG /* disable interrupt, quit, and suspend
1121 special characters */
1122 | IEXTEN); /* disable non-POSIX special characters */
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001123 } /* CT_CYPHIDCOM: Application should handle this for device */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 linechange = (priv->line_control != oldlines);
1126 spin_unlock_irqrestore(&priv->lock, flags);
1127
1128 /* if necessary, set lines */
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001129 if (linechange) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 priv->cmd_ctrl = 1;
Alan Cox95da3102008-07-22 11:09:07 +01001131 cypress_write(tty, port, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133} /* cypress_set_termios */
1134
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001135
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136/* returns amount of data still left in soft buffer */
Alan Cox95da3102008-07-22 11:09:07 +01001137static int cypress_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138{
Alan Cox95da3102008-07-22 11:09:07 +01001139 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 struct cypress_private *priv = usb_get_serial_port_data(port);
1141 int chars = 0;
1142 unsigned long flags;
1143
Harvey Harrison441b62c2008-03-03 16:08:34 -08001144 dbg("%s - port %d", __func__, port->number);
Alan Cox813a2242008-07-22 11:10:36 +01001145
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 spin_lock_irqsave(&priv->lock, flags);
1147 chars = cypress_buf_data_avail(priv->buf);
1148 spin_unlock_irqrestore(&priv->lock, flags);
1149
Harvey Harrison441b62c2008-03-03 16:08:34 -08001150 dbg("%s - returns %d", __func__, chars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 return chars;
1152}
1153
1154
Alan Cox95da3102008-07-22 11:09:07 +01001155static void cypress_throttle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156{
Alan Cox95da3102008-07-22 11:09:07 +01001157 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 struct cypress_private *priv = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Harvey Harrison441b62c2008-03-03 16:08:34 -08001160 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
Oliver Neukum63832512009-10-07 10:50:23 +02001162 spin_lock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 priv->rx_flags = THROTTLED;
Oliver Neukum63832512009-10-07 10:50:23 +02001164 spin_unlock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165}
1166
1167
Alan Cox95da3102008-07-22 11:09:07 +01001168static void cypress_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169{
Alan Cox95da3102008-07-22 11:09:07 +01001170 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 struct cypress_private *priv = usb_get_serial_port_data(port);
1172 int actually_throttled, result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
Harvey Harrison441b62c2008-03-03 16:08:34 -08001174 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
Oliver Neukum63832512009-10-07 10:50:23 +02001176 spin_lock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 actually_throttled = priv->rx_flags & ACTUALLY_THROTTLED;
1178 priv->rx_flags = 0;
Oliver Neukum63832512009-10-07 10:50:23 +02001179 spin_unlock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
Mike Isely78aef512006-08-29 22:07:11 -05001181 if (!priv->comm_is_ok)
1182 return;
1183
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 if (actually_throttled) {
1185 port->interrupt_in_urb->dev = port->serial->dev;
1186
Oliver Neukum63832512009-10-07 10:50:23 +02001187 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
Mike Isely78aef512006-08-29 22:07:11 -05001188 if (result) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001189 dev_err(&port->dev, "%s - failed submitting read urb, "
Harvey Harrison441b62c2008-03-03 16:08:34 -08001190 "error %d\n", __func__, result);
Mike Isely78aef512006-08-29 22:07:11 -05001191 cypress_set_dead(port);
1192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 }
1194}
1195
1196
David Howells7d12e782006-10-05 14:55:46 +01001197static void cypress_read_int_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198{
Ming Leicdc97792008-02-24 18:41:47 +08001199 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 struct cypress_private *priv = usb_get_serial_port_data(port);
1201 struct tty_struct *tty;
1202 unsigned char *data = urb->transfer_buffer;
1203 unsigned long flags;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001204 char tty_flag = TTY_NORMAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 int havedata = 0;
1206 int bytes = 0;
1207 int result;
1208 int i = 0;
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001209 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
Harvey Harrison441b62c2008-03-03 16:08:34 -08001211 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001213 switch (status) {
Mike Isely78aef512006-08-29 22:07:11 -05001214 case 0: /* success */
1215 break;
1216 case -ECONNRESET:
1217 case -ENOENT:
1218 case -ESHUTDOWN:
1219 /* precursor to disconnect so just go away */
1220 return;
1221 case -EPIPE:
Alan Stern4d2fae82009-07-09 12:59:57 -04001222 /* Can't call usb_clear_halt while in_interrupt */
1223 /* FALLS THROUGH */
Mike Isely78aef512006-08-29 22:07:11 -05001224 default:
1225 /* something ugly is going on... */
Alan Cox813a2242008-07-22 11:10:36 +01001226 dev_err(&urb->dev->dev,
1227 "%s - unexpected nonzero read status received: %d\n",
1228 __func__, status);
Mike Isely78aef512006-08-29 22:07:11 -05001229 cypress_set_dead(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 return;
1231 }
1232
1233 spin_lock_irqsave(&priv->lock, flags);
1234 if (priv->rx_flags & THROTTLED) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001235 dbg("%s - now throttling", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 priv->rx_flags |= ACTUALLY_THROTTLED;
1237 spin_unlock_irqrestore(&priv->lock, flags);
1238 return;
1239 }
1240 spin_unlock_irqrestore(&priv->lock, flags);
1241
Alan Cox4a90f092008-10-13 10:39:46 +01001242 tty = tty_port_tty_get(&port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 if (!tty) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001244 dbg("%s - bad tty pointer - exiting", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 return;
1246 }
1247
1248 spin_lock_irqsave(&priv->lock, flags);
Mike Isely3416eaa2008-02-10 20:23:19 -06001249 result = urb->actual_length;
1250 switch (priv->pkt_fmt) {
1251 default:
1252 case packet_format_1:
1253 /* This is for the CY7C64013... */
1254 priv->current_status = data[0] & 0xF8;
1255 bytes = data[1] + 2;
1256 i = 2;
1257 if (bytes > 2)
1258 havedata = 1;
1259 break;
1260 case packet_format_2:
1261 /* This is for the CY7C63743... */
1262 priv->current_status = data[0] & 0xF8;
1263 bytes = (data[0] & 0x07) + 1;
1264 i = 1;
1265 if (bytes > 1)
1266 havedata = 1;
1267 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 }
1269 spin_unlock_irqrestore(&priv->lock, flags);
Mike Isely3416eaa2008-02-10 20:23:19 -06001270 if (result < bytes) {
1271 dbg("%s - wrong packet size - received %d bytes but packet "
1272 "said %d bytes", __func__, result, bytes);
1273 goto continue_read;
1274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
Alan Cox813a2242008-07-22 11:10:36 +01001276 usb_serial_debug_data(debug, &port->dev, __func__,
1277 urb->actual_length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
1279 spin_lock_irqsave(&priv->lock, flags);
1280 /* check to see if status has changed */
Mike Isely67683062008-02-10 20:23:28 -06001281 if (priv->current_status != priv->prev_status) {
1282 priv->diff_status |= priv->current_status ^
1283 priv->prev_status;
1284 wake_up_interruptible(&priv->delta_msr_wait);
1285 priv->prev_status = priv->current_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 }
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001287 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001289 /* hangup, as defined in acm.c... this might be a bad place for it
1290 * though */
1291 if (tty && !(tty->termios->c_cflag & CLOCAL) &&
1292 !(priv->current_status & UART_CD)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001293 dbg("%s - calling hangup", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 tty_hangup(tty);
1295 goto continue_read;
1296 }
1297
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001298 /* There is one error bit... I'm assuming it is a parity error
1299 * indicator as the generic firmware will set this bit to 1 if a
1300 * parity error occurs.
1301 * I can not find reference to any other error events. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 spin_lock_irqsave(&priv->lock, flags);
1303 if (priv->current_status & CYP_ERROR) {
1304 spin_unlock_irqrestore(&priv->lock, flags);
1305 tty_flag = TTY_PARITY;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001306 dbg("%s - Parity Error detected", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 } else
1308 spin_unlock_irqrestore(&priv->lock, flags);
1309
1310 /* process read if there is data other than line status */
1311 if (tty && (bytes > i)) {
Alan Cox33f0f882006-01-09 20:54:13 -08001312 bytes = tty_buffer_request_room(tty, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 for (; i < bytes ; ++i) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001314 dbg("pushing byte number %d - %d - %c", i, data[i],
1315 data[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 tty_insert_flip_char(tty, data[i], tty_flag);
1317 }
Alan Cox4a90f092008-10-13 10:39:46 +01001318 tty_flip_buffer_push(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 }
1320
1321 spin_lock_irqsave(&priv->lock, flags);
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001322 /* control and status byte(s) are also counted */
1323 priv->bytes_in += bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 spin_unlock_irqrestore(&priv->lock, flags);
1325
1326continue_read:
Alan Cox4a90f092008-10-13 10:39:46 +01001327 tty_kref_put(tty);
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001328
1329 /* Continue trying to always read... unless the port has closed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
Alan Cox95da3102008-07-22 11:09:07 +01001331 if (port->port.count > 0 && priv->comm_is_ok) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001332 usb_fill_int_urb(port->interrupt_in_urb, port->serial->dev,
1333 usb_rcvintpipe(port->serial->dev,
1334 port->interrupt_in_endpointAddress),
1335 port->interrupt_in_urb->transfer_buffer,
1336 port->interrupt_in_urb->transfer_buffer_length,
Alan Cox813a2242008-07-22 11:10:36 +01001337 cypress_read_int_callback, port,
1338 priv->read_urb_interval);
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001339 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
Mike Isely78aef512006-08-29 22:07:11 -05001340 if (result) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001341 dev_err(&urb->dev->dev, "%s - failed resubmitting "
Harvey Harrison441b62c2008-03-03 16:08:34 -08001342 "read urb, error %d\n", __func__,
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001343 result);
Mike Isely78aef512006-08-29 22:07:11 -05001344 cypress_set_dead(port);
1345 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 }
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001347
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 return;
1349} /* cypress_read_int_callback */
1350
1351
David Howells7d12e782006-10-05 14:55:46 +01001352static void cypress_write_int_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353{
Ming Leicdc97792008-02-24 18:41:47 +08001354 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 struct cypress_private *priv = usb_get_serial_port_data(port);
1356 int result;
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001357 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
Harvey Harrison441b62c2008-03-03 16:08:34 -08001359 dbg("%s - port %d", __func__, port->number);
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001360
1361 switch (status) {
Alan Cox813a2242008-07-22 11:10:36 +01001362 case 0:
1363 /* success */
1364 break;
1365 case -ECONNRESET:
1366 case -ENOENT:
1367 case -ESHUTDOWN:
1368 /* this urb is terminated, clean up */
1369 dbg("%s - urb shutting down with status: %d",
1370 __func__, status);
1371 priv->write_urb_in_use = 0;
1372 return;
1373 case -EPIPE: /* no break needed; clear halt and resubmit */
1374 if (!priv->comm_is_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 break;
Alan Cox813a2242008-07-22 11:10:36 +01001376 usb_clear_halt(port->serial->dev, 0x02);
1377 /* error in the urb, so we have to resubmit it */
1378 dbg("%s - nonzero write bulk status received: %d",
1379 __func__, status);
1380 port->interrupt_out_urb->transfer_buffer_length = 1;
1381 port->interrupt_out_urb->dev = port->serial->dev;
1382 result = usb_submit_urb(port->interrupt_out_urb, GFP_ATOMIC);
1383 if (!result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 return;
Alan Cox813a2242008-07-22 11:10:36 +01001385 dev_err(&urb->dev->dev,
1386 "%s - failed resubmitting write urb, error %d\n",
1387 __func__, result);
1388 cypress_set_dead(port);
1389 break;
1390 default:
1391 dev_err(&urb->dev->dev,
1392 "%s - unexpected nonzero write status received: %d\n",
1393 __func__, status);
1394 cypress_set_dead(port);
1395 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 priv->write_urb_in_use = 0;
Alan Cox813a2242008-07-22 11:10:36 +01001398
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 /* send any buffered data */
1400 cypress_send(port);
1401}
1402
1403
1404/*****************************************************************************
1405 * Write buffer functions - buffering code from pl2303 used
1406 *****************************************************************************/
1407
1408/*
1409 * cypress_buf_alloc
1410 *
1411 * Allocate a circular buffer and all associated memory.
1412 */
1413
1414static struct cypress_buf *cypress_buf_alloc(unsigned int size)
1415{
1416
1417 struct cypress_buf *cb;
1418
1419
1420 if (size == 0)
1421 return NULL;
1422
Robert P. J. Day5cbded52006-12-13 00:35:56 -08001423 cb = kmalloc(sizeof(struct cypress_buf), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 if (cb == NULL)
1425 return NULL;
1426
1427 cb->buf_buf = kmalloc(size, GFP_KERNEL);
1428 if (cb->buf_buf == NULL) {
1429 kfree(cb);
1430 return NULL;
1431 }
1432
1433 cb->buf_size = size;
1434 cb->buf_get = cb->buf_put = cb->buf_buf;
1435
1436 return cb;
1437
1438}
1439
1440
1441/*
1442 * cypress_buf_free
1443 *
1444 * Free the buffer and all associated memory.
1445 */
1446
1447static void cypress_buf_free(struct cypress_buf *cb)
1448{
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001449 if (cb) {
1450 kfree(cb->buf_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 kfree(cb);
1452 }
1453}
1454
1455
1456/*
1457 * cypress_buf_clear
1458 *
1459 * Clear out all data in the circular buffer.
1460 */
1461
1462static void cypress_buf_clear(struct cypress_buf *cb)
1463{
1464 if (cb != NULL)
1465 cb->buf_get = cb->buf_put;
1466 /* equivalent to a get of all data available */
1467}
1468
1469
1470/*
1471 * cypress_buf_data_avail
1472 *
1473 * Return the number of bytes of data available in the circular
1474 * buffer.
1475 */
1476
1477static unsigned int cypress_buf_data_avail(struct cypress_buf *cb)
1478{
1479 if (cb != NULL)
Alan Cox813a2242008-07-22 11:10:36 +01001480 return (cb->buf_size + cb->buf_put - cb->buf_get)
1481 % cb->buf_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 else
1483 return 0;
1484}
1485
1486
1487/*
1488 * cypress_buf_space_avail
1489 *
1490 * Return the number of bytes of space available in the circular
1491 * buffer.
1492 */
1493
1494static unsigned int cypress_buf_space_avail(struct cypress_buf *cb)
1495{
1496 if (cb != NULL)
Alan Cox813a2242008-07-22 11:10:36 +01001497 return (cb->buf_size + cb->buf_get - cb->buf_put - 1)
1498 % cb->buf_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 else
1500 return 0;
1501}
1502
1503
1504/*
1505 * cypress_buf_put
1506 *
1507 * Copy data data from a user buffer and put it into the circular buffer.
1508 * Restrict to the amount of space available.
1509 *
1510 * Return the number of bytes copied.
1511 */
1512
1513static unsigned int cypress_buf_put(struct cypress_buf *cb, const char *buf,
1514 unsigned int count)
1515{
1516
1517 unsigned int len;
1518
1519
1520 if (cb == NULL)
1521 return 0;
1522
1523 len = cypress_buf_space_avail(cb);
1524 if (count > len)
1525 count = len;
1526
1527 if (count == 0)
1528 return 0;
1529
1530 len = cb->buf_buf + cb->buf_size - cb->buf_put;
1531 if (count > len) {
1532 memcpy(cb->buf_put, buf, len);
1533 memcpy(cb->buf_buf, buf+len, count - len);
1534 cb->buf_put = cb->buf_buf + count - len;
1535 } else {
1536 memcpy(cb->buf_put, buf, count);
1537 if (count < len)
1538 cb->buf_put += count;
1539 else /* count == len */
1540 cb->buf_put = cb->buf_buf;
1541 }
1542
1543 return count;
1544
1545}
1546
1547
1548/*
1549 * cypress_buf_get
1550 *
1551 * Get data from the circular buffer and copy to the given buffer.
1552 * Restrict to the amount of data available.
1553 *
1554 * Return the number of bytes copied.
1555 */
1556
1557static unsigned int cypress_buf_get(struct cypress_buf *cb, char *buf,
1558 unsigned int count)
1559{
1560
1561 unsigned int len;
1562
1563
1564 if (cb == NULL)
1565 return 0;
1566
1567 len = cypress_buf_data_avail(cb);
1568 if (count > len)
1569 count = len;
1570
1571 if (count == 0)
1572 return 0;
1573
1574 len = cb->buf_buf + cb->buf_size - cb->buf_get;
1575 if (count > len) {
1576 memcpy(buf, cb->buf_get, len);
1577 memcpy(buf+len, cb->buf_buf, count - len);
1578 cb->buf_get = cb->buf_buf + count - len;
1579 } else {
1580 memcpy(buf, cb->buf_get, count);
1581 if (count < len)
1582 cb->buf_get += count;
1583 else /* count == len */
1584 cb->buf_get = cb->buf_buf;
1585 }
1586
1587 return count;
1588
1589}
1590
1591/*****************************************************************************
1592 * Module functions
1593 *****************************************************************************/
1594
1595static int __init cypress_init(void)
1596{
1597 int retval;
Alan Cox813a2242008-07-22 11:10:36 +01001598
Harvey Harrison441b62c2008-03-03 16:08:34 -08001599 dbg("%s", __func__);
Alan Cox813a2242008-07-22 11:10:36 +01001600
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 retval = usb_serial_register(&cypress_earthmate_device);
1602 if (retval)
1603 goto failed_em_register;
1604 retval = usb_serial_register(&cypress_hidcom_device);
1605 if (retval)
1606 goto failed_hidcom_register;
Lonnie Mendeza5c44e22006-03-01 10:45:24 -06001607 retval = usb_serial_register(&cypress_ca42v2_device);
1608 if (retval)
1609 goto failed_ca42v2_register;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 retval = usb_register(&cypress_driver);
1611 if (retval)
1612 goto failed_usb_register;
1613
Greg Kroah-Hartmanc197a8d2008-08-18 13:21:04 -07001614 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
1615 DRIVER_DESC "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
Mariusz Kozlowski2e46b742006-11-17 17:49:22 +01001618failed_usb_register:
1619 usb_serial_deregister(&cypress_ca42v2_device);
1620failed_ca42v2_register:
1621 usb_serial_deregister(&cypress_hidcom_device);
1622failed_hidcom_register:
1623 usb_serial_deregister(&cypress_earthmate_device);
1624failed_em_register:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 return retval;
1626}
1627
1628
Alan Cox813a2242008-07-22 11:10:36 +01001629static void __exit cypress_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630{
Harvey Harrison441b62c2008-03-03 16:08:34 -08001631 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
Alan Cox813a2242008-07-22 11:10:36 +01001633 usb_deregister(&cypress_driver);
1634 usb_serial_deregister(&cypress_earthmate_device);
1635 usb_serial_deregister(&cypress_hidcom_device);
1636 usb_serial_deregister(&cypress_ca42v2_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637}
1638
1639
1640module_init(cypress_init);
1641module_exit(cypress_exit);
1642
Alan Cox813a2242008-07-22 11:10:36 +01001643MODULE_AUTHOR(DRIVER_AUTHOR);
1644MODULE_DESCRIPTION(DRIVER_DESC);
1645MODULE_VERSION(DRIVER_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646MODULE_LICENSE("GPL");
1647
1648module_param(debug, bool, S_IRUGO | S_IWUSR);
1649MODULE_PARM_DESC(debug, "Debug enabled or not");
1650module_param(stats, bool, S_IRUGO | S_IWUSR);
1651MODULE_PARM_DESC(stats, "Enable statistics or not");
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001652module_param(interval, int, S_IRUGO | S_IWUSR);
1653MODULE_PARM_DESC(interval, "Overrides interrupt interval");
Mike Frysingerc3126592009-12-18 16:33:03 -05001654module_param(unstable_bauds, bool, S_IRUGO | S_IWUSR);
1655MODULE_PARM_DESC(unstable_bauds, "Allow unstable baud rates");