blob: eae4740d448c22c35d4fcdfd3fcf6e9b9bc2a5b5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * USB Cypress M8 driver
3 *
4 * Copyright (C) 2004
Alan Cox813a2242008-07-22 11:10:36 +01005 * Lonnie Mendez (dignome@gmail.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Copyright (C) 2003,2004
7 * Neil Whelchel (koyama@firstlight.net)
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
Alan Cox813a2242008-07-22 11:10:36 +010014 * See Documentation/usb/usb-serial.txt for more information on using this
15 * driver
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 *
17 * See http://geocities.com/i0xox0i for information on this driver and the
18 * earthmate usb device.
19 *
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -050020 * Lonnie Mendez <dignome@gmail.com>
21 * 4-29-2005
Alan Cox813a2242008-07-22 11:10:36 +010022 * Fixed problem where setting or retreiving the serial config would fail
23 * with EPIPE. Removed CRTS toggling so the driver behaves more like
24 * other usbserial adapters. Issued new interval of 1ms instead of the
25 * default 10ms. As a result, transfer speed has been substantially
26 * increased from avg. 850bps to avg. 3300bps. initial termios has also
27 * been modified. Cleaned up code and formatting issues so it is more
28 * readable. Replaced the C++ style comments.
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 *
30 * Lonnie Mendez <dignome@gmail.com>
31 * 12-15-2004
32 * Incorporated write buffering from pl2303 driver. Fixed bug with line
33 * handling so both lines are raised in cypress_open. (was dropping rts)
34 * Various code cleanups made as well along with other misc bug fixes.
35 *
36 * Lonnie Mendez <dignome@gmail.com>
37 * 04-10-2004
38 * Driver modified to support dynamic line settings. Various improvments
39 * and features.
40 *
41 * Neil Whelchel
42 * 10-2003
43 * Driver first released.
44 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 */
46
Alan Cox813a2242008-07-22 11:10:36 +010047/* Thanks to Neil Whelchel for writing the first cypress m8 implementation
48 for linux. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070049/* Thanks to cypress for providing references for the hid reports. */
50/* Thanks to Jiang Zhang for providing links and for general help. */
Alan Cox813a2242008-07-22 11:10:36 +010051/* Code originates and was built up from ftdi_sio, belkin, pl2303 and others.*/
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <linux/kernel.h>
55#include <linux/errno.h>
56#include <linux/init.h>
57#include <linux/slab.h>
58#include <linux/tty.h>
59#include <linux/tty_driver.h>
60#include <linux/tty_flip.h>
61#include <linux/module.h>
62#include <linux/moduleparam.h>
63#include <linux/spinlock.h>
64#include <linux/usb.h>
Greg Kroah-Hartmana9698882006-07-11 21:22:58 -070065#include <linux/usb/serial.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#include <linux/serial.h>
67#include <linux/delay.h>
Alan Cox813a2242008-07-22 11:10:36 +010068#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#include "cypress_m8.h"
71
72
73#ifdef CONFIG_USB_SERIAL_DEBUG
74 static int debug = 1;
75#else
76 static int debug;
77#endif
78static int stats;
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -050079static int interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81/*
82 * Version Information
83 */
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -050084#define DRIVER_VERSION "v1.09"
Linus Torvalds1da177e2005-04-16 15:20:36 -070085#define DRIVER_AUTHOR "Lonnie Mendez <dignome@gmail.com>, Neil Whelchel <koyama@firstlight.net>"
86#define DRIVER_DESC "Cypress USB to Serial Driver"
87
88/* write buffer size defines */
89#define CYPRESS_BUF_SIZE 1024
90#define CYPRESS_CLOSING_WAIT (30*HZ)
91
92static struct usb_device_id id_table_earthmate [] = {
93 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB) },
Lonnie Mendez25b6f082005-05-10 17:09:52 -050094 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB_LT20) },
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 { } /* Terminating entry */
96};
97
98static struct usb_device_id id_table_cyphidcomrs232 [] = {
99 { USB_DEVICE(VENDOR_ID_CYPRESS, PRODUCT_ID_CYPHIDCOM) },
Dmitry Shapin6f6f06e2008-03-04 15:25:10 -0800100 { USB_DEVICE(VENDOR_ID_POWERCOM, PRODUCT_ID_UPS) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 { } /* Terminating entry */
102};
103
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600104static struct usb_device_id id_table_nokiaca42v2 [] = {
105 { USB_DEVICE(VENDOR_ID_DAZZLE, PRODUCT_ID_CA42) },
106 { } /* Terminating entry */
107};
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109static struct usb_device_id id_table_combined [] = {
110 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB) },
Lonnie Mendez25b6f082005-05-10 17:09:52 -0500111 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB_LT20) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 { USB_DEVICE(VENDOR_ID_CYPRESS, PRODUCT_ID_CYPHIDCOM) },
Dmitry Shapin6f6f06e2008-03-04 15:25:10 -0800113 { USB_DEVICE(VENDOR_ID_POWERCOM, PRODUCT_ID_UPS) },
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600114 { USB_DEVICE(VENDOR_ID_DAZZLE, PRODUCT_ID_CA42) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 { } /* Terminating entry */
116};
117
Alan Cox813a2242008-07-22 11:10:36 +0100118MODULE_DEVICE_TABLE(usb, id_table_combined);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120static struct usb_driver cypress_driver = {
121 .name = "cypress",
122 .probe = usb_serial_probe,
123 .disconnect = usb_serial_disconnect,
124 .id_table = id_table_combined,
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800125 .no_dynamic_id = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126};
127
Mike Isely3416eaa2008-02-10 20:23:19 -0600128enum packet_format {
129 packet_format_1, /* b0:status, b1:payload count */
130 packet_format_2 /* b0[7:3]:status, b0[2:0]:payload count */
131};
132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133struct cypress_private {
134 spinlock_t lock; /* private lock */
135 int chiptype; /* identifier of device, for quirks/etc */
136 int bytes_in; /* used for statistics */
137 int bytes_out; /* used for statistics */
138 int cmd_count; /* used for statistics */
139 int cmd_ctrl; /* always set this to 1 before issuing a command */
140 struct cypress_buf *buf; /* write buffer */
141 int write_urb_in_use; /* write urb in use indicator */
Mike Isely0257fa92006-08-29 22:06:59 -0500142 int write_urb_interval; /* interval to use for write urb */
143 int read_urb_interval; /* interval to use for read urb */
Mike Isely78aef512006-08-29 22:07:11 -0500144 int comm_is_ok; /* true if communication is (still) ok */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 int termios_initialized;
146 __u8 line_control; /* holds dtr / rts value */
147 __u8 current_status; /* received from last read - info on dsr,cts,cd,ri,etc */
148 __u8 current_config; /* stores the current configuration byte */
149 __u8 rx_flags; /* throttling - used from whiteheat/ftdi_sio */
Mike Isely3416eaa2008-02-10 20:23:19 -0600150 enum packet_format pkt_fmt; /* format to use for packet send / receive */
Mike Isely3d6aa322008-02-10 20:23:24 -0600151 int get_cfg_unsafe; /* If true, the CYPRESS_GET_CONFIG is unsafe */
Alan Cox813a2242008-07-22 11:10:36 +0100152 int baud_rate; /* stores current baud rate in
153 integer form */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 int isthrottled; /* if throttled, discard reads */
155 wait_queue_head_t delta_msr_wait; /* used for TIOCMIWAIT */
156 char prev_status, diff_status; /* used for TIOCMIWAIT */
Alan Cox813a2242008-07-22 11:10:36 +0100157 /* we pass a pointer to this as the arguement sent to
158 cypress_set_termios old_termios */
Alan Cox606d0992006-12-08 02:38:45 -0800159 struct ktermios tmp_termios; /* stores the old termios settings */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160};
161
162/* write buffer structure */
163struct cypress_buf {
164 unsigned int buf_size;
165 char *buf_buf;
166 char *buf_get;
167 char *buf_put;
168};
169
170/* function prototypes for the Cypress USB to serial device */
Alan Cox813a2242008-07-22 11:10:36 +0100171static int cypress_earthmate_startup(struct usb_serial *serial);
172static int cypress_hidcom_startup(struct usb_serial *serial);
173static int cypress_ca42v2_startup(struct usb_serial *serial);
174static void cypress_shutdown(struct usb_serial *serial);
175static int cypress_open(struct tty_struct *tty,
176 struct usb_serial_port *port, struct file *filp);
177static void cypress_close(struct tty_struct *tty,
178 struct usb_serial_port *port, struct file *filp);
179static int cypress_write(struct tty_struct *tty, struct usb_serial_port *port,
180 const unsigned char *buf, int count);
181static void cypress_send(struct usb_serial_port *port);
182static int cypress_write_room(struct tty_struct *tty);
183static int cypress_ioctl(struct tty_struct *tty, struct file *file,
184 unsigned int cmd, unsigned long arg);
185static void cypress_set_termios(struct tty_struct *tty,
186 struct usb_serial_port *port, struct ktermios *old);
187static int cypress_tiocmget(struct tty_struct *tty, struct file *file);
188static int cypress_tiocmset(struct tty_struct *tty, struct file *file,
189 unsigned int set, unsigned int clear);
190static int cypress_chars_in_buffer(struct tty_struct *tty);
191static void cypress_throttle(struct tty_struct *tty);
192static void cypress_unthrottle(struct tty_struct *tty);
193static void cypress_set_dead(struct usb_serial_port *port);
194static void cypress_read_int_callback(struct urb *urb);
195static void cypress_write_int_callback(struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196/* write buffer functions */
197static struct cypress_buf *cypress_buf_alloc(unsigned int size);
Alan Cox813a2242008-07-22 11:10:36 +0100198static void cypress_buf_free(struct cypress_buf *cb);
199static void cypress_buf_clear(struct cypress_buf *cb);
200static unsigned int cypress_buf_data_avail(struct cypress_buf *cb);
201static unsigned int cypress_buf_space_avail(struct cypress_buf *cb);
202static unsigned int cypress_buf_put(struct cypress_buf *cb,
203 const char *buf, unsigned int count);
204static unsigned int cypress_buf_get(struct cypress_buf *cb,
205 char *buf, unsigned int count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700208static struct usb_serial_driver cypress_earthmate_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700209 .driver = {
210 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700211 .name = "earthmate",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700212 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700213 .description = "DeLorme Earthmate USB",
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100214 .usb_driver = &cypress_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 .id_table = id_table_earthmate,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 .num_ports = 1,
217 .attach = cypress_earthmate_startup,
218 .shutdown = cypress_shutdown,
219 .open = cypress_open,
220 .close = cypress_close,
221 .write = cypress_write,
222 .write_room = cypress_write_room,
223 .ioctl = cypress_ioctl,
224 .set_termios = cypress_set_termios,
225 .tiocmget = cypress_tiocmget,
226 .tiocmset = cypress_tiocmset,
227 .chars_in_buffer = cypress_chars_in_buffer,
228 .throttle = cypress_throttle,
229 .unthrottle = cypress_unthrottle,
230 .read_int_callback = cypress_read_int_callback,
231 .write_int_callback = cypress_write_int_callback,
232};
233
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700234static struct usb_serial_driver cypress_hidcom_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700235 .driver = {
236 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700237 .name = "cyphidcom",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700238 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700239 .description = "HID->COM RS232 Adapter",
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100240 .usb_driver = &cypress_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 .id_table = id_table_cyphidcomrs232,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 .num_ports = 1,
243 .attach = cypress_hidcom_startup,
244 .shutdown = cypress_shutdown,
245 .open = cypress_open,
246 .close = cypress_close,
247 .write = cypress_write,
248 .write_room = cypress_write_room,
249 .ioctl = cypress_ioctl,
250 .set_termios = cypress_set_termios,
251 .tiocmget = cypress_tiocmget,
252 .tiocmset = cypress_tiocmset,
253 .chars_in_buffer = cypress_chars_in_buffer,
254 .throttle = cypress_throttle,
255 .unthrottle = cypress_unthrottle,
256 .read_int_callback = cypress_read_int_callback,
257 .write_int_callback = cypress_write_int_callback,
258};
259
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600260static struct usb_serial_driver cypress_ca42v2_device = {
261 .driver = {
262 .owner = THIS_MODULE,
Alan Cox813a2242008-07-22 11:10:36 +0100263 .name = "nokiaca42v2",
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600264 },
265 .description = "Nokia CA-42 V2 Adapter",
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100266 .usb_driver = &cypress_driver,
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600267 .id_table = id_table_nokiaca42v2,
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600268 .num_ports = 1,
269 .attach = cypress_ca42v2_startup,
270 .shutdown = cypress_shutdown,
271 .open = cypress_open,
272 .close = cypress_close,
273 .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
296 /*
297 * The general purpose firmware for the Cypress M8 allows for
298 * a maximum speed of 57600bps (I have no idea whether DeLorme
299 * chose to use the general purpose firmware or not), if you
300 * need to modify this speed setting for your own project
301 * please add your own chiptype and modify the code likewise.
302 * The Cypress HID->COM device will work successfully up to
303 * 115200bps (but the actual throughput is around 3kBps).
304 */
Mike Isely92983c22008-02-10 20:23:32 -0600305 if (port->serial->dev->speed == USB_SPEED_LOW) {
306 /*
307 * Mike Isely <isely@pobox.com> 2-Feb-2008: The
308 * Cypress app note that describes this mechanism
309 * states the the low-speed part can't handle more
310 * than 800 bytes/sec, in which case 4800 baud is the
311 * safest speed for a part like that.
312 */
313 if (new_rate > 4800) {
314 dbg("%s - failed setting baud rate, device incapable "
315 "speed %d", __func__, new_rate);
316 return -1;
317 }
318 }
319 switch (priv->chiptype) {
320 case CT_EARTHMATE:
321 if (new_rate <= 600) {
322 /* 300 and 600 baud rates are supported under
323 * the generic firmware, but are not used with
324 * NMEA and SiRF protocols */
325 dbg("%s - failed setting baud rate, unsupported speed "
326 "of %d on Earthmate GPS", __func__, new_rate);
327 return -1;
328 }
329 break;
330 default:
331 break;
332 }
333 return new_rate;
334}
335
336
Steven Cole093cf722005-05-03 19:07:24 -0600337/* This function can either set or retrieve the current serial line settings */
Alan Cox813a2242008-07-22 11:10:36 +0100338static int cypress_serial_control(struct tty_struct *tty,
Alan Cox95da3102008-07-22 11:09:07 +0100339 struct usb_serial_port *port, speed_t baud_rate, int data_bits,
340 int stop_bits, int parity_enable, int parity_type, int reset,
341 int cypress_request_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500343 int new_baudrate = 0, retval = 0, tries = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 struct cypress_private *priv;
Mike Isely93075542008-02-10 20:23:14 -0600345 __u8 feature_buffer[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 unsigned long flags;
347
Harvey Harrison441b62c2008-03-03 16:08:34 -0800348 dbg("%s", __func__);
Alan Cox813a2242008-07-22 11:10:36 +0100349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 priv = usb_get_serial_port_data(port);
351
Mike Isely78aef512006-08-29 22:07:11 -0500352 if (!priv->comm_is_ok)
353 return -ENODEV;
354
Alan Cox813a2242008-07-22 11:10:36 +0100355 switch (cypress_request_type) {
356 case CYPRESS_SET_CONFIG:
357 new_baudrate = priv->baud_rate;
358 /* 0 means 'Hang up' so doesn't change the true bit rate */
359 if (baud_rate == 0)
Mike Isely92983c22008-02-10 20:23:32 -0600360 new_baudrate = priv->baud_rate;
Alan Cox813a2242008-07-22 11:10:36 +0100361 /* Change of speed ? */
362 else if (baud_rate != priv->baud_rate) {
363 dbg("%s - baud rate is changing", __func__);
364 retval = analyze_baud_rate(port, baud_rate);
365 if (retval >= 0) {
366 new_baudrate = retval;
367 dbg("%s - New baud rate set to %d",
368 __func__, new_baudrate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 }
Alan Cox813a2242008-07-22 11:10:36 +0100370 }
371 dbg("%s - baud rate is being sent as %d",
372 __func__, new_baudrate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Alan Cox813a2242008-07-22 11:10:36 +0100374 memset(feature_buffer, 0, sizeof(feature_buffer));
375 /* fill the feature_buffer with new configuration */
376 *((u_int32_t *)feature_buffer) = new_baudrate;
377 feature_buffer[4] |= data_bits; /* assign data bits in 2 bit space ( max 3 ) */
378 /* 1 bit gap */
379 feature_buffer[4] |= (stop_bits << 3); /* assign stop bits in 1 bit space */
380 feature_buffer[4] |= (parity_enable << 4); /* assign parity flag in 1 bit space */
381 feature_buffer[4] |= (parity_type << 5); /* assign parity type in 1 bit space */
382 /* 1 bit gap */
383 feature_buffer[4] |= (reset << 7); /* assign reset at end of byte, 1 bit space */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Alan Cox813a2242008-07-22 11:10:36 +0100385 dbg("%s - device is being sent this feature report:",
386 __func__);
387 dbg("%s - %02X - %02X - %02X - %02X - %02X", __func__,
388 feature_buffer[0], feature_buffer[1],
389 feature_buffer[2], feature_buffer[3],
390 feature_buffer[4]);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500391
Alan Cox813a2242008-07-22 11:10:36 +0100392 do {
393 retval = usb_control_msg(port->serial->dev,
394 usb_sndctrlpipe(port->serial->dev, 0),
395 HID_REQ_SET_REPORT,
396 USB_DIR_OUT | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
397 0x0300, 0, feature_buffer,
398 sizeof(feature_buffer), 500);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500399
Alan Cox813a2242008-07-22 11:10:36 +0100400 if (tries++ >= 3)
401 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Alan Cox813a2242008-07-22 11:10:36 +0100403 } while (retval != sizeof(feature_buffer) &&
404 retval != -ENODEV);
Mike Isely93075542008-02-10 20:23:14 -0600405
Alan Cox813a2242008-07-22 11:10:36 +0100406 if (retval != sizeof(feature_buffer)) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700407 dev_err(&port->dev, "%s - failed sending serial "
408 "line settings - %d\n", __func__, retval);
Alan Cox813a2242008-07-22 11:10:36 +0100409 cypress_set_dead(port);
410 } else {
411 spin_lock_irqsave(&priv->lock, flags);
412 priv->baud_rate = new_baudrate;
413 priv->current_config = feature_buffer[4];
414 spin_unlock_irqrestore(&priv->lock, flags);
415 /* If we asked for a speed change encode it */
416 if (baud_rate)
417 tty_encode_baud_rate(tty,
418 new_baudrate, new_baudrate);
419 }
420 break;
421 case CYPRESS_GET_CONFIG:
422 if (priv->get_cfg_unsafe) {
423 /* Not implemented for this device,
424 and if we try to do it we're likely
425 to crash the hardware. */
426 return -ENOTTY;
427 }
428 dbg("%s - retreiving serial line settings", __func__);
429 /* set initial values in feature buffer */
430 memset(feature_buffer, 0, sizeof(feature_buffer));
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500431
Alan Cox813a2242008-07-22 11:10:36 +0100432 do {
433 retval = usb_control_msg(port->serial->dev,
434 usb_rcvctrlpipe(port->serial->dev, 0),
435 HID_REQ_GET_REPORT,
436 USB_DIR_IN | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
437 0x0300, 0, feature_buffer,
438 sizeof(feature_buffer), 500);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500439
Alan Cox813a2242008-07-22 11:10:36 +0100440 if (tries++ >= 3)
441 break;
442 } while (retval != sizeof(feature_buffer)
443 && retval != -ENODEV);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500444
Alan Cox813a2242008-07-22 11:10:36 +0100445 if (retval != sizeof(feature_buffer)) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700446 dev_err(&port->dev, "%s - failed to retrieve serial "
447 "line settings - %d\n", __func__, retval);
Alan Cox813a2242008-07-22 11:10:36 +0100448 cypress_set_dead(port);
449 return retval;
450 } else {
451 spin_lock_irqsave(&priv->lock, flags);
452 /* store the config in one byte, and later
453 use bit masks to check values */
454 priv->current_config = feature_buffer[4];
455 priv->baud_rate = *((u_int32_t *)feature_buffer);
456 spin_unlock_irqrestore(&priv->lock, flags);
457 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 }
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500459 spin_lock_irqsave(&priv->lock, flags);
460 ++priv->cmd_count;
461 spin_unlock_irqrestore(&priv->lock, flags);
462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 return retval;
464} /* cypress_serial_control */
465
466
Mike Isely78aef512006-08-29 22:07:11 -0500467static void cypress_set_dead(struct usb_serial_port *port)
468{
469 struct cypress_private *priv = usb_get_serial_port_data(port);
470 unsigned long flags;
471
472 spin_lock_irqsave(&priv->lock, flags);
473 if (!priv->comm_is_ok) {
474 spin_unlock_irqrestore(&priv->lock, flags);
475 return;
476 }
477 priv->comm_is_ok = 0;
478 spin_unlock_irqrestore(&priv->lock, flags);
479
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700480 dev_err(&port->dev, "cypress_m8 suspending failing port %d - "
481 "interval might be too short\n", port->number);
Mike Isely78aef512006-08-29 22:07:11 -0500482}
483
484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485/*****************************************************************************
486 * Cypress serial driver functions
487 *****************************************************************************/
488
489
Alan Cox813a2242008-07-22 11:10:36 +0100490static int generic_startup(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
492 struct cypress_private *priv;
Mike Isely0257fa92006-08-29 22:06:59 -0500493 struct usb_serial_port *port = serial->port[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Harvey Harrison441b62c2008-03-03 16:08:34 -0800495 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Alan Cox813a2242008-07-22 11:10:36 +0100497 priv = kzalloc(sizeof(struct cypress_private), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 if (!priv)
499 return -ENOMEM;
500
Mike Isely78aef512006-08-29 22:07:11 -0500501 priv->comm_is_ok = !0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 spin_lock_init(&priv->lock);
503 priv->buf = cypress_buf_alloc(CYPRESS_BUF_SIZE);
504 if (priv->buf == NULL) {
505 kfree(priv);
506 return -ENOMEM;
507 }
508 init_waitqueue_head(&priv->delta_msr_wait);
Alan Cox813a2242008-07-22 11:10:36 +0100509
510 usb_reset_configuration(serial->dev);
511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 priv->cmd_ctrl = 0;
513 priv->line_control = 0;
514 priv->termios_initialized = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 priv->rx_flags = 0;
Mike Isely3416eaa2008-02-10 20:23:19 -0600516 /* Default packet format setting is determined by packet size.
517 Anything with a size larger then 9 must have a separate
518 count field since the 3 bit count field is otherwise too
519 small. Otherwise we can use the slightly more compact
520 format. This is in accordance with the cypress_m8 serial
521 converter app note. */
Alan Cox813a2242008-07-22 11:10:36 +0100522 if (port->interrupt_out_size > 9)
Mike Isely3416eaa2008-02-10 20:23:19 -0600523 priv->pkt_fmt = packet_format_1;
Alan Cox813a2242008-07-22 11:10:36 +0100524 else
Mike Isely3416eaa2008-02-10 20:23:19 -0600525 priv->pkt_fmt = packet_format_2;
Alan Cox813a2242008-07-22 11:10:36 +0100526
Mike Isely0257fa92006-08-29 22:06:59 -0500527 if (interval > 0) {
528 priv->write_urb_interval = interval;
529 priv->read_urb_interval = interval;
530 dbg("%s - port %d read & write intervals forced to %d",
Alan Cox813a2242008-07-22 11:10:36 +0100531 __func__, port->number, interval);
Mike Isely0257fa92006-08-29 22:06:59 -0500532 } else {
533 priv->write_urb_interval = port->interrupt_out_urb->interval;
534 priv->read_urb_interval = port->interrupt_in_urb->interval;
535 dbg("%s - port %d intervals: read=%d write=%d",
Alan Cox813a2242008-07-22 11:10:36 +0100536 __func__, port->number,
537 priv->read_urb_interval, priv->write_urb_interval);
Mike Isely0257fa92006-08-29 22:06:59 -0500538 }
539 usb_set_serial_port_data(port, priv);
Alan Cox813a2242008-07-22 11:10:36 +0100540
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500541 return 0;
542}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
544
Alan Cox813a2242008-07-22 11:10:36 +0100545static int cypress_earthmate_startup(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
547 struct cypress_private *priv;
Mike Isely3d6aa322008-02-10 20:23:24 -0600548 struct usb_serial_port *port = serial->port[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Harvey Harrison441b62c2008-03-03 16:08:34 -0800550 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
552 if (generic_startup(serial)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800553 dbg("%s - Failed setting up port %d", __func__,
Mike Isely3d6aa322008-02-10 20:23:24 -0600554 port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 return 1;
556 }
557
Mike Isely3d6aa322008-02-10 20:23:24 -0600558 priv = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 priv->chiptype = CT_EARTHMATE;
Mike Isely3416eaa2008-02-10 20:23:19 -0600560 /* All Earthmate devices use the separated-count packet
561 format! Idiotic. */
562 priv->pkt_fmt = packet_format_1;
Alan Cox813a2242008-07-22 11:10:36 +0100563 if (serial->dev->descriptor.idProduct !=
564 cpu_to_le16(PRODUCT_ID_EARTHMATEUSB)) {
Mike Isely3d6aa322008-02-10 20:23:24 -0600565 /* The old original USB Earthmate seemed able to
566 handle GET_CONFIG requests; everything they've
567 produced since that time crashes if this command is
568 attempted :-( */
569 dbg("%s - Marking this device as unsafe for GET_CONFIG "
570 "commands", __func__);
571 priv->get_cfg_unsafe = !0;
572 }
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500573
574 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575} /* cypress_earthmate_startup */
576
577
Alan Cox813a2242008-07-22 11:10:36 +0100578static int cypress_hidcom_startup(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
580 struct cypress_private *priv;
581
Harvey Harrison441b62c2008-03-03 16:08:34 -0800582 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
584 if (generic_startup(serial)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800585 dbg("%s - Failed setting up port %d", __func__,
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500586 serial->port[0]->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 return 1;
588 }
589
590 priv = usb_get_serial_port_data(serial->port[0]);
591 priv->chiptype = CT_CYPHIDCOM;
Alan Cox813a2242008-07-22 11:10:36 +0100592
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500593 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594} /* cypress_hidcom_startup */
595
596
Alan Cox813a2242008-07-22 11:10:36 +0100597static int cypress_ca42v2_startup(struct usb_serial *serial)
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600598{
599 struct cypress_private *priv;
600
Harvey Harrison441b62c2008-03-03 16:08:34 -0800601 dbg("%s", __func__);
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600602
603 if (generic_startup(serial)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800604 dbg("%s - Failed setting up port %d", __func__,
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600605 serial->port[0]->number);
606 return 1;
607 }
608
609 priv = usb_get_serial_port_data(serial->port[0]);
610 priv->chiptype = CT_CA42V2;
611
612 return 0;
613} /* cypress_ca42v2_startup */
614
615
Alan Cox813a2242008-07-22 11:10:36 +0100616static void cypress_shutdown(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
618 struct cypress_private *priv;
619
Alan Cox813a2242008-07-22 11:10:36 +0100620 dbg("%s - port %d", __func__, serial->port[0]->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622 /* all open ports are closed at this point */
623
624 priv = usb_get_serial_port_data(serial->port[0]);
625
626 if (priv) {
627 cypress_buf_free(priv->buf);
628 kfree(priv);
629 usb_set_serial_port_data(serial->port[0], NULL);
630 }
631}
632
633
Alan Cox95da3102008-07-22 11:09:07 +0100634static int cypress_open(struct tty_struct *tty,
635 struct usb_serial_port *port, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636{
637 struct cypress_private *priv = usb_get_serial_port_data(port);
638 struct usb_serial *serial = port->serial;
639 unsigned long flags;
640 int result = 0;
641
Harvey Harrison441b62c2008-03-03 16:08:34 -0800642 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Mike Isely78aef512006-08-29 22:07:11 -0500644 if (!priv->comm_is_ok)
645 return -EIO;
646
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 /* clear halts before open */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 usb_clear_halt(serial->dev, 0x81);
649 usb_clear_halt(serial->dev, 0x02);
650
651 spin_lock_irqsave(&priv->lock, flags);
652 /* reset read/write statistics */
653 priv->bytes_in = 0;
654 priv->bytes_out = 0;
655 priv->cmd_count = 0;
656 priv->rx_flags = 0;
657 spin_unlock_irqrestore(&priv->lock, flags);
658
659 /* setting to zero could cause data loss */
Alan Cox95da3102008-07-22 11:09:07 +0100660 if (tty)
661 tty->low_latency = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
663 /* raise both lines and set termios */
664 spin_lock_irqsave(&priv->lock, flags);
665 priv->line_control = CONTROL_DTR | CONTROL_RTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 priv->cmd_ctrl = 1;
667 spin_unlock_irqrestore(&priv->lock, flags);
Alan Cox95da3102008-07-22 11:09:07 +0100668 result = cypress_write(tty, port, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
670 if (result) {
Alan Cox813a2242008-07-22 11:10:36 +0100671 dev_err(&port->dev,
672 "%s - failed setting the control lines - error %d\n",
673 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 return result;
675 } else
Harvey Harrison441b62c2008-03-03 16:08:34 -0800676 dbg("%s - success setting the control lines", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Alan Cox95da3102008-07-22 11:09:07 +0100678 if (tty)
679 cypress_set_termios(tty, port, &priv->tmp_termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
681 /* setup the port and start reading from the device */
Alan Cox813a2242008-07-22 11:10:36 +0100682 if (!port->interrupt_in_urb) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700683 dev_err(&port->dev, "%s - interrupt_in_urb is empty!\n",
684 __func__);
Alan Cox813a2242008-07-22 11:10:36 +0100685 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 }
687
688 usb_fill_int_urb(port->interrupt_in_urb, serial->dev,
689 usb_rcvintpipe(serial->dev, port->interrupt_in_endpointAddress),
Alan Cox813a2242008-07-22 11:10:36 +0100690 port->interrupt_in_urb->transfer_buffer,
691 port->interrupt_in_urb->transfer_buffer_length,
Mike Isely0257fa92006-08-29 22:06:59 -0500692 cypress_read_int_callback, port, priv->read_urb_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
694
Alan Cox813a2242008-07-22 11:10:36 +0100695 if (result) {
696 dev_err(&port->dev,
697 "%s - failed submitting read urb, error %d\n",
698 __func__, result);
Mike Isely78aef512006-08-29 22:07:11 -0500699 cypress_set_dead(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 }
701
702 return result;
703} /* cypress_open */
704
705
Alan Cox95da3102008-07-22 11:09:07 +0100706static void cypress_close(struct tty_struct *tty,
Alan Cox813a2242008-07-22 11:10:36 +0100707 struct usb_serial_port *port, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708{
709 struct cypress_private *priv = usb_get_serial_port_data(port);
710 unsigned int c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 int bps;
712 long timeout;
713 wait_queue_t wait;
714
Harvey Harrison441b62c2008-03-03 16:08:34 -0800715 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
717 /* wait for data to drain from buffer */
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100718 spin_lock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 timeout = CYPRESS_CLOSING_WAIT;
720 init_waitqueue_entry(&wait, current);
Alan Cox95da3102008-07-22 11:09:07 +0100721 add_wait_queue(&tty->write_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 for (;;) {
723 set_current_state(TASK_INTERRUPTIBLE);
724 if (cypress_buf_data_avail(priv->buf) == 0
725 || timeout == 0 || signal_pending(current)
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100726 /* without mutex, allowed due to harmless failure mode */
727 || port->serial->disconnected)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 break;
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100729 spin_unlock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 timeout = schedule_timeout(timeout);
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100731 spin_lock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 }
733 set_current_state(TASK_RUNNING);
Alan Cox95da3102008-07-22 11:09:07 +0100734 remove_wait_queue(&tty->write_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 /* clear out any remaining data in the buffer */
736 cypress_buf_clear(priv->buf);
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100737 spin_unlock_irq(&priv->lock);
738
739 /* writing is potentially harmful, lock must be taken */
740 mutex_lock(&port->serial->disc_mutex);
741 if (port->serial->disconnected) {
742 mutex_unlock(&port->serial->disc_mutex);
743 return;
744 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 /* wait for characters to drain from device */
Alan Cox95da3102008-07-22 11:09:07 +0100746 if (tty) {
747 bps = tty_get_baud_rate(tty);
748 if (bps > 1200)
Alan Cox813a2242008-07-22 11:10:36 +0100749 timeout = max((HZ * 2560) / bps, HZ / 10);
Alan Cox95da3102008-07-22 11:09:07 +0100750 else
Alan Cox813a2242008-07-22 11:10:36 +0100751 timeout = 2 * HZ;
Alan Cox95da3102008-07-22 11:09:07 +0100752 schedule_timeout_interruptible(timeout);
753 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Harvey Harrison441b62c2008-03-03 16:08:34 -0800755 dbg("%s - stopping urbs", __func__);
Alan Cox813a2242008-07-22 11:10:36 +0100756 usb_kill_urb(port->interrupt_in_urb);
757 usb_kill_urb(port->interrupt_out_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
Alan Cox95da3102008-07-22 11:09:07 +0100759 if (tty) {
760 c_cflag = tty->termios->c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 if (c_cflag & HUPCL) {
762 /* drop dtr and rts */
763 priv = usb_get_serial_port_data(port);
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100764 spin_lock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 priv->line_control = 0;
766 priv->cmd_ctrl = 1;
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100767 spin_unlock_irq(&priv->lock);
Alan Cox95da3102008-07-22 11:09:07 +0100768 cypress_write(tty, port, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 }
770 }
771
772 if (stats)
Alan Cox813a2242008-07-22 11:10:36 +0100773 dev_info(&port->dev, "Statistics: %d Bytes In | %d Bytes Out | %d Commands Issued\n",
774 priv->bytes_in, priv->bytes_out, priv->cmd_count);
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100775 mutex_unlock(&port->serial->disc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776} /* cypress_close */
777
778
Alan Cox95da3102008-07-22 11:09:07 +0100779static int cypress_write(struct tty_struct *tty, struct usb_serial_port *port,
780 const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
782 struct cypress_private *priv = usb_get_serial_port_data(port);
783 unsigned long flags;
Alan Cox813a2242008-07-22 11:10:36 +0100784
Harvey Harrison441b62c2008-03-03 16:08:34 -0800785 dbg("%s - port %d, %d bytes", __func__, port->number, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
787 /* line control commands, which need to be executed immediately,
788 are not put into the buffer for obvious reasons.
789 */
790 if (priv->cmd_ctrl) {
791 count = 0;
792 goto finish;
793 }
Alan Cox813a2242008-07-22 11:10:36 +0100794
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 if (!count)
796 return count;
Alan Cox813a2242008-07-22 11:10:36 +0100797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 spin_lock_irqsave(&priv->lock, flags);
799 count = cypress_buf_put(priv->buf, buf, count);
800 spin_unlock_irqrestore(&priv->lock, flags);
801
802finish:
803 cypress_send(port);
804
805 return count;
806} /* cypress_write */
807
808
809static void cypress_send(struct usb_serial_port *port)
810{
811 int count = 0, result, offset, actual_size;
812 struct cypress_private *priv = usb_get_serial_port_data(port);
813 unsigned long flags;
Alan Cox813a2242008-07-22 11:10:36 +0100814
Mike Isely78aef512006-08-29 22:07:11 -0500815 if (!priv->comm_is_ok)
816 return;
817
Harvey Harrison441b62c2008-03-03 16:08:34 -0800818 dbg("%s - port %d", __func__, port->number);
Alan Cox813a2242008-07-22 11:10:36 +0100819 dbg("%s - interrupt out size is %d", __func__,
820 port->interrupt_out_size);
821
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 spin_lock_irqsave(&priv->lock, flags);
823 if (priv->write_urb_in_use) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800824 dbg("%s - can't write, urb in use", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 spin_unlock_irqrestore(&priv->lock, flags);
826 return;
827 }
828 spin_unlock_irqrestore(&priv->lock, flags);
829
830 /* clear buffer */
Alan Cox813a2242008-07-22 11:10:36 +0100831 memset(port->interrupt_out_urb->transfer_buffer, 0,
832 port->interrupt_out_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
834 spin_lock_irqsave(&priv->lock, flags);
Mike Isely3416eaa2008-02-10 20:23:19 -0600835 switch (priv->pkt_fmt) {
836 default:
837 case packet_format_1:
838 /* this is for the CY7C64013... */
839 offset = 2;
840 port->interrupt_out_buffer[0] = priv->line_control;
841 break;
842 case packet_format_2:
843 /* this is for the CY7C63743... */
844 offset = 1;
845 port->interrupt_out_buffer[0] = priv->line_control;
846 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 }
848
849 if (priv->line_control & CONTROL_RESET)
850 priv->line_control &= ~CONTROL_RESET;
851
852 if (priv->cmd_ctrl) {
853 priv->cmd_count++;
Harvey Harrison441b62c2008-03-03 16:08:34 -0800854 dbg("%s - line control command being issued", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 spin_unlock_irqrestore(&priv->lock, flags);
856 goto send;
857 } else
858 spin_unlock_irqrestore(&priv->lock, flags);
859
860 count = cypress_buf_get(priv->buf, &port->interrupt_out_buffer[offset],
861 port->interrupt_out_size-offset);
862
Alan Cox813a2242008-07-22 11:10:36 +0100863 if (count == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Mike Isely3416eaa2008-02-10 20:23:19 -0600866 switch (priv->pkt_fmt) {
867 default:
868 case packet_format_1:
869 port->interrupt_out_buffer[1] = count;
870 break;
871 case packet_format_2:
872 port->interrupt_out_buffer[0] |= count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 }
874
Harvey Harrison441b62c2008-03-03 16:08:34 -0800875 dbg("%s - count is %d", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
877send:
878 spin_lock_irqsave(&priv->lock, flags);
879 priv->write_urb_in_use = 1;
880 spin_unlock_irqrestore(&priv->lock, flags);
881
882 if (priv->cmd_ctrl)
883 actual_size = 1;
884 else
Mike Isely3416eaa2008-02-10 20:23:19 -0600885 actual_size = count +
886 (priv->pkt_fmt == packet_format_1 ? 2 : 1);
887
Alan Cox813a2242008-07-22 11:10:36 +0100888 usb_serial_debug_data(debug, &port->dev, __func__,
889 port->interrupt_out_size,
890 port->interrupt_out_urb->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
Mike Isely9aa8dae2006-08-29 22:07:04 -0500892 usb_fill_int_urb(port->interrupt_out_urb, port->serial->dev,
893 usb_sndintpipe(port->serial->dev, port->interrupt_out_endpointAddress),
894 port->interrupt_out_buffer, port->interrupt_out_size,
895 cypress_write_int_callback, port, priv->write_urb_interval);
Alan Cox813a2242008-07-22 11:10:36 +0100896 result = usb_submit_urb(port->interrupt_out_urb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 if (result) {
Alan Cox813a2242008-07-22 11:10:36 +0100898 dev_err(&port->dev,
899 "%s - failed submitting write urb, error %d\n",
900 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 priv->write_urb_in_use = 0;
Mike Isely78aef512006-08-29 22:07:11 -0500902 cypress_set_dead(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 }
904
905 spin_lock_irqsave(&priv->lock, flags);
Alan Cox813a2242008-07-22 11:10:36 +0100906 if (priv->cmd_ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 priv->cmd_ctrl = 0;
Alan Cox813a2242008-07-22 11:10:36 +0100908
909 /* do not count the line control and size bytes */
910 priv->bytes_out += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 spin_unlock_irqrestore(&priv->lock, flags);
912
Pete Zaitcevcf2c7482006-05-22 21:58:49 -0700913 usb_serial_port_softint(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914} /* cypress_send */
915
916
917/* returns how much space is available in the soft buffer */
Alan Cox95da3102008-07-22 11:09:07 +0100918static int cypress_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919{
Alan Cox95da3102008-07-22 11:09:07 +0100920 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 struct cypress_private *priv = usb_get_serial_port_data(port);
922 int room = 0;
923 unsigned long flags;
924
Harvey Harrison441b62c2008-03-03 16:08:34 -0800925 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
927 spin_lock_irqsave(&priv->lock, flags);
928 room = cypress_buf_space_avail(priv->buf);
929 spin_unlock_irqrestore(&priv->lock, flags);
930
Harvey Harrison441b62c2008-03-03 16:08:34 -0800931 dbg("%s - returns %d", __func__, room);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 return room;
933}
934
935
Alan Cox95da3102008-07-22 11:09:07 +0100936static int cypress_tiocmget(struct tty_struct *tty, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937{
Alan Cox95da3102008-07-22 11:09:07 +0100938 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 struct cypress_private *priv = usb_get_serial_port_data(port);
940 __u8 status, control;
941 unsigned int result = 0;
942 unsigned long flags;
Alan Cox813a2242008-07-22 11:10:36 +0100943
Harvey Harrison441b62c2008-03-03 16:08:34 -0800944 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
946 spin_lock_irqsave(&priv->lock, flags);
947 control = priv->line_control;
948 status = priv->current_status;
949 spin_unlock_irqrestore(&priv->lock, flags);
950
951 result = ((control & CONTROL_DTR) ? TIOCM_DTR : 0)
952 | ((control & CONTROL_RTS) ? TIOCM_RTS : 0)
953 | ((status & UART_CTS) ? TIOCM_CTS : 0)
954 | ((status & UART_DSR) ? TIOCM_DSR : 0)
955 | ((status & UART_RI) ? TIOCM_RI : 0)
956 | ((status & UART_CD) ? TIOCM_CD : 0);
957
Harvey Harrison441b62c2008-03-03 16:08:34 -0800958 dbg("%s - result = %x", __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
960 return result;
961}
962
963
Alan Cox95da3102008-07-22 11:09:07 +0100964static int cypress_tiocmset(struct tty_struct *tty, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 unsigned int set, unsigned int clear)
966{
Alan Cox95da3102008-07-22 11:09:07 +0100967 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 struct cypress_private *priv = usb_get_serial_port_data(port);
969 unsigned long flags;
Alan Cox813a2242008-07-22 11:10:36 +0100970
Harvey Harrison441b62c2008-03-03 16:08:34 -0800971 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
973 spin_lock_irqsave(&priv->lock, flags);
974 if (set & TIOCM_RTS)
975 priv->line_control |= CONTROL_RTS;
976 if (set & TIOCM_DTR)
977 priv->line_control |= CONTROL_DTR;
978 if (clear & TIOCM_RTS)
979 priv->line_control &= ~CONTROL_RTS;
980 if (clear & TIOCM_DTR)
981 priv->line_control &= ~CONTROL_DTR;
Alan Cox8873aaa2008-03-10 21:59:28 +0000982 priv->cmd_ctrl = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 spin_unlock_irqrestore(&priv->lock, flags);
984
Alan Cox95da3102008-07-22 11:09:07 +0100985 return cypress_write(tty, port, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986}
987
988
Alan Cox813a2242008-07-22 11:10:36 +0100989static int cypress_ioctl(struct tty_struct *tty, struct file *file,
Alan Cox95da3102008-07-22 11:09:07 +0100990 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991{
Alan Cox95da3102008-07-22 11:09:07 +0100992 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 struct cypress_private *priv = usb_get_serial_port_data(port);
994
Harvey Harrison441b62c2008-03-03 16:08:34 -0800995 dbg("%s - port %d, cmd 0x%.4x", __func__, port->number, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
997 switch (cmd) {
Alan Cox813a2242008-07-22 11:10:36 +0100998 /* This code comes from drivers/char/serial.c and ftdi_sio.c */
999 case TIOCMIWAIT:
1000 while (priv != NULL) {
1001 interruptible_sleep_on(&priv->delta_msr_wait);
1002 /* see if a signal did it */
1003 if (signal_pending(current))
1004 return -ERESTARTSYS;
1005 else {
1006 char diff = priv->diff_status;
1007 if (diff == 0)
1008 return -EIO; /* no change => error */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
Alan Cox813a2242008-07-22 11:10:36 +01001010 /* consume all events */
1011 priv->diff_status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
Alan Cox813a2242008-07-22 11:10:36 +01001013 /* return 0 if caller wanted to know about
1014 these bits */
1015 if (((arg & TIOCM_RNG) && (diff & UART_RI)) ||
1016 ((arg & TIOCM_DSR) && (diff & UART_DSR)) ||
1017 ((arg & TIOCM_CD) && (diff & UART_CD)) ||
1018 ((arg & TIOCM_CTS) && (diff & UART_CTS)))
1019 return 0;
1020 /* otherwise caller can't care less about what
1021 * happened, and so we continue to wait for
1022 * more events.
1023 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 }
Alan Cox813a2242008-07-22 11:10:36 +01001025 }
1026 return 0;
1027 default:
1028 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 }
Harvey Harrison441b62c2008-03-03 16:08:34 -08001030 dbg("%s - arg not supported - it was 0x%04x - check include/asm/ioctls.h", __func__, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 return -ENOIOCTLCMD;
1032} /* cypress_ioctl */
1033
1034
Alan Cox95da3102008-07-22 11:09:07 +01001035static void cypress_set_termios(struct tty_struct *tty,
1036 struct usb_serial_port *port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037{
1038 struct cypress_private *priv = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 int data_bits, stop_bits, parity_type, parity_enable;
Alan Cox8873aaa2008-03-10 21:59:28 +00001040 unsigned cflag, iflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 unsigned long flags;
1042 __u8 oldlines;
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001043 int linechange = 0;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001044
Harvey Harrison441b62c2008-03-03 16:08:34 -08001045 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 spin_lock_irqsave(&priv->lock, flags);
1048 if (!priv->termios_initialized) {
1049 if (priv->chiptype == CT_EARTHMATE) {
1050 *(tty->termios) = tty_std_termios;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001051 tty->termios->c_cflag = B4800 | CS8 | CREAD | HUPCL |
1052 CLOCAL;
Alan Cox8873aaa2008-03-10 21:59:28 +00001053 tty->termios->c_ispeed = 4800;
1054 tty->termios->c_ospeed = 4800;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 } else if (priv->chiptype == CT_CYPHIDCOM) {
1056 *(tty->termios) = tty_std_termios;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001057 tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
1058 CLOCAL;
Alan Cox8873aaa2008-03-10 21:59:28 +00001059 tty->termios->c_ispeed = 9600;
1060 tty->termios->c_ospeed = 9600;
Lonnie Mendeza5c44e22006-03-01 10:45:24 -06001061 } else if (priv->chiptype == CT_CA42V2) {
1062 *(tty->termios) = tty_std_termios;
1063 tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
1064 CLOCAL;
Alan Cox8873aaa2008-03-10 21:59:28 +00001065 tty->termios->c_ispeed = 9600;
1066 tty->termios->c_ospeed = 9600;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 }
1068 priv->termios_initialized = 1;
1069 }
1070 spin_unlock_irqrestore(&priv->lock, flags);
1071
Alan Cox8873aaa2008-03-10 21:59:28 +00001072 /* Unsupported features need clearing */
1073 tty->termios->c_cflag &= ~(CMSPAR|CRTSCTS);
1074
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 cflag = tty->termios->c_cflag;
1076 iflag = tty->termios->c_iflag;
1077
1078 /* check if there are new settings */
1079 if (old_termios) {
Alan Cox8873aaa2008-03-10 21:59:28 +00001080 spin_lock_irqsave(&priv->lock, flags);
1081 priv->tmp_termios = *(tty->termios);
1082 spin_unlock_irqrestore(&priv->lock, flags);
1083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
1085 /* set number of data bits, parity, stop bits */
1086 /* when parity is disabled the parity type bit is ignored */
1087
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001088 /* 1 means 2 stop bits, 0 means 1 stop bit */
1089 stop_bits = cflag & CSTOPB ? 1 : 0;
1090
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 if (cflag & PARENB) {
1092 parity_enable = 1;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001093 /* 1 means odd parity, 0 means even parity */
1094 parity_type = cflag & PARODD ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 } else
1096 parity_enable = parity_type = 0;
1097
Alan Cox77336822008-07-22 11:10:53 +01001098 switch (cflag & CSIZE) {
1099 case CS5:
1100 data_bits = 0;
1101 break;
1102 case CS6:
1103 data_bits = 1;
1104 break;
1105 case CS7:
1106 data_bits = 2;
1107 break;
1108 case CS8:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 data_bits = 3;
Alan Cox77336822008-07-22 11:10:53 +01001110 break;
1111 default:
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001112 dev_err(&port->dev, "%s - CSIZE was set, but not CS5-CS8\n",
1113 __func__);
Alan Cox77336822008-07-22 11:10:53 +01001114 data_bits = 3;
1115 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 spin_lock_irqsave(&priv->lock, flags);
1117 oldlines = priv->line_control;
1118 if ((cflag & CBAUD) == B0) {
1119 /* drop dtr and rts */
Harvey Harrison441b62c2008-03-03 16:08:34 -08001120 dbg("%s - dropping the lines, baud rate 0bps", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
Alan Cox8873aaa2008-03-10 21:59:28 +00001122 } else
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001123 priv->line_control = (CONTROL_DTR | CONTROL_RTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001126 dbg("%s - sending %d stop_bits, %d parity_enable, %d parity_type, "
Harvey Harrison441b62c2008-03-03 16:08:34 -08001127 "%d data_bits (+5)", __func__, stop_bits,
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001128 parity_enable, parity_type, data_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
Alan Cox813a2242008-07-22 11:10:36 +01001130 cypress_serial_control(tty, port, tty_get_baud_rate(tty),
1131 data_bits, stop_bits,
1132 parity_enable, parity_type,
1133 0, CYPRESS_SET_CONFIG);
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001134
1135 /* we perform a CYPRESS_GET_CONFIG so that the current settings are
1136 * filled into the private structure this should confirm that all is
1137 * working if it returns what we just set */
Alan Cox95da3102008-07-22 11:09:07 +01001138 cypress_serial_control(tty, port, 0, 0, 0, 0, 0, 0, CYPRESS_GET_CONFIG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001140 /* Here we can define custom tty settings for devices; the main tty
1141 * termios flag base comes from empeg.c */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001143 spin_lock_irqsave(&priv->lock, flags);
Alan Cox813a2242008-07-22 11:10:36 +01001144 if (priv->chiptype == CT_EARTHMATE && priv->baud_rate == 4800) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001145 dbg("Using custom termios settings for a baud rate of "
1146 "4800bps.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 /* define custom termios settings for NMEA protocol */
1148
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 tty->termios->c_iflag /* input modes - */
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001150 &= ~(IGNBRK /* disable ignore break */
1151 | BRKINT /* disable break causes interrupt */
1152 | PARMRK /* disable mark parity errors */
1153 | ISTRIP /* disable clear high bit of input char */
1154 | INLCR /* disable translate NL to CR */
1155 | IGNCR /* disable ignore CR */
1156 | ICRNL /* disable translate CR to NL */
1157 | IXON); /* disable enable XON/XOFF flow control */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001159 tty->termios->c_oflag /* output modes */
1160 &= ~OPOST; /* disable postprocess output char */
1161
1162 tty->termios->c_lflag /* line discipline modes */
1163 &= ~(ECHO /* disable echo input characters */
1164 | ECHONL /* disable echo new line */
1165 | ICANON /* disable erase, kill, werase, and rprnt
1166 special characters */
1167 | ISIG /* disable interrupt, quit, and suspend
1168 special characters */
1169 | IEXTEN); /* disable non-POSIX special characters */
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001170 } /* CT_CYPHIDCOM: Application should handle this for device */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 linechange = (priv->line_control != oldlines);
1173 spin_unlock_irqrestore(&priv->lock, flags);
1174
1175 /* if necessary, set lines */
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001176 if (linechange) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 priv->cmd_ctrl = 1;
Alan Cox95da3102008-07-22 11:09:07 +01001178 cypress_write(tty, port, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180} /* cypress_set_termios */
1181
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001182
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183/* returns amount of data still left in soft buffer */
Alan Cox95da3102008-07-22 11:09:07 +01001184static int cypress_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185{
Alan Cox95da3102008-07-22 11:09:07 +01001186 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 struct cypress_private *priv = usb_get_serial_port_data(port);
1188 int chars = 0;
1189 unsigned long flags;
1190
Harvey Harrison441b62c2008-03-03 16:08:34 -08001191 dbg("%s - port %d", __func__, port->number);
Alan Cox813a2242008-07-22 11:10:36 +01001192
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 spin_lock_irqsave(&priv->lock, flags);
1194 chars = cypress_buf_data_avail(priv->buf);
1195 spin_unlock_irqrestore(&priv->lock, flags);
1196
Harvey Harrison441b62c2008-03-03 16:08:34 -08001197 dbg("%s - returns %d", __func__, chars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 return chars;
1199}
1200
1201
Alan Cox95da3102008-07-22 11:09:07 +01001202static void cypress_throttle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203{
Alan Cox95da3102008-07-22 11:09:07 +01001204 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 struct cypress_private *priv = usb_get_serial_port_data(port);
1206 unsigned long flags;
1207
Harvey Harrison441b62c2008-03-03 16:08:34 -08001208 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
1210 spin_lock_irqsave(&priv->lock, flags);
1211 priv->rx_flags = THROTTLED;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001212 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213}
1214
1215
Alan Cox95da3102008-07-22 11:09:07 +01001216static void cypress_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217{
Alan Cox95da3102008-07-22 11:09:07 +01001218 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 struct cypress_private *priv = usb_get_serial_port_data(port);
1220 int actually_throttled, result;
1221 unsigned long flags;
1222
Harvey Harrison441b62c2008-03-03 16:08:34 -08001223 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
1225 spin_lock_irqsave(&priv->lock, flags);
1226 actually_throttled = priv->rx_flags & ACTUALLY_THROTTLED;
1227 priv->rx_flags = 0;
1228 spin_unlock_irqrestore(&priv->lock, flags);
1229
Mike Isely78aef512006-08-29 22:07:11 -05001230 if (!priv->comm_is_ok)
1231 return;
1232
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 if (actually_throttled) {
1234 port->interrupt_in_urb->dev = port->serial->dev;
1235
1236 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
Mike Isely78aef512006-08-29 22:07:11 -05001237 if (result) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001238 dev_err(&port->dev, "%s - failed submitting read urb, "
Harvey Harrison441b62c2008-03-03 16:08:34 -08001239 "error %d\n", __func__, result);
Mike Isely78aef512006-08-29 22:07:11 -05001240 cypress_set_dead(port);
1241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 }
1243}
1244
1245
David Howells7d12e782006-10-05 14:55:46 +01001246static void cypress_read_int_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247{
Ming Leicdc97792008-02-24 18:41:47 +08001248 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 struct cypress_private *priv = usb_get_serial_port_data(port);
1250 struct tty_struct *tty;
1251 unsigned char *data = urb->transfer_buffer;
1252 unsigned long flags;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001253 char tty_flag = TTY_NORMAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 int havedata = 0;
1255 int bytes = 0;
1256 int result;
1257 int i = 0;
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001258 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
Harvey Harrison441b62c2008-03-03 16:08:34 -08001260 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001262 switch (status) {
Mike Isely78aef512006-08-29 22:07:11 -05001263 case 0: /* success */
1264 break;
1265 case -ECONNRESET:
1266 case -ENOENT:
1267 case -ESHUTDOWN:
1268 /* precursor to disconnect so just go away */
1269 return;
1270 case -EPIPE:
Alan Cox813a2242008-07-22 11:10:36 +01001271 usb_clear_halt(port->serial->dev, 0x81);
Mike Isely78aef512006-08-29 22:07:11 -05001272 break;
1273 default:
1274 /* something ugly is going on... */
Alan Cox813a2242008-07-22 11:10:36 +01001275 dev_err(&urb->dev->dev,
1276 "%s - unexpected nonzero read status received: %d\n",
1277 __func__, status);
Mike Isely78aef512006-08-29 22:07:11 -05001278 cypress_set_dead(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 return;
1280 }
1281
1282 spin_lock_irqsave(&priv->lock, flags);
1283 if (priv->rx_flags & THROTTLED) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001284 dbg("%s - now throttling", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 priv->rx_flags |= ACTUALLY_THROTTLED;
1286 spin_unlock_irqrestore(&priv->lock, flags);
1287 return;
1288 }
1289 spin_unlock_irqrestore(&priv->lock, flags);
1290
Alan Cox4a90f092008-10-13 10:39:46 +01001291 tty = tty_port_tty_get(&port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 if (!tty) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001293 dbg("%s - bad tty pointer - exiting", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 return;
1295 }
1296
1297 spin_lock_irqsave(&priv->lock, flags);
Mike Isely3416eaa2008-02-10 20:23:19 -06001298 result = urb->actual_length;
1299 switch (priv->pkt_fmt) {
1300 default:
1301 case packet_format_1:
1302 /* This is for the CY7C64013... */
1303 priv->current_status = data[0] & 0xF8;
1304 bytes = data[1] + 2;
1305 i = 2;
1306 if (bytes > 2)
1307 havedata = 1;
1308 break;
1309 case packet_format_2:
1310 /* This is for the CY7C63743... */
1311 priv->current_status = data[0] & 0xF8;
1312 bytes = (data[0] & 0x07) + 1;
1313 i = 1;
1314 if (bytes > 1)
1315 havedata = 1;
1316 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 }
1318 spin_unlock_irqrestore(&priv->lock, flags);
Mike Isely3416eaa2008-02-10 20:23:19 -06001319 if (result < bytes) {
1320 dbg("%s - wrong packet size - received %d bytes but packet "
1321 "said %d bytes", __func__, result, bytes);
1322 goto continue_read;
1323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
Alan Cox813a2242008-07-22 11:10:36 +01001325 usb_serial_debug_data(debug, &port->dev, __func__,
1326 urb->actual_length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
1328 spin_lock_irqsave(&priv->lock, flags);
1329 /* check to see if status has changed */
Mike Isely67683062008-02-10 20:23:28 -06001330 if (priv->current_status != priv->prev_status) {
1331 priv->diff_status |= priv->current_status ^
1332 priv->prev_status;
1333 wake_up_interruptible(&priv->delta_msr_wait);
1334 priv->prev_status = priv->current_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 }
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001336 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001338 /* hangup, as defined in acm.c... this might be a bad place for it
1339 * though */
1340 if (tty && !(tty->termios->c_cflag & CLOCAL) &&
1341 !(priv->current_status & UART_CD)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001342 dbg("%s - calling hangup", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 tty_hangup(tty);
1344 goto continue_read;
1345 }
1346
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001347 /* There is one error bit... I'm assuming it is a parity error
1348 * indicator as the generic firmware will set this bit to 1 if a
1349 * parity error occurs.
1350 * I can not find reference to any other error events. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 spin_lock_irqsave(&priv->lock, flags);
1352 if (priv->current_status & CYP_ERROR) {
1353 spin_unlock_irqrestore(&priv->lock, flags);
1354 tty_flag = TTY_PARITY;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001355 dbg("%s - Parity Error detected", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 } else
1357 spin_unlock_irqrestore(&priv->lock, flags);
1358
1359 /* process read if there is data other than line status */
1360 if (tty && (bytes > i)) {
Alan Cox33f0f882006-01-09 20:54:13 -08001361 bytes = tty_buffer_request_room(tty, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 for (; i < bytes ; ++i) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001363 dbg("pushing byte number %d - %d - %c", i, data[i],
1364 data[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 tty_insert_flip_char(tty, data[i], tty_flag);
1366 }
Alan Cox4a90f092008-10-13 10:39:46 +01001367 tty_flip_buffer_push(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 }
1369
1370 spin_lock_irqsave(&priv->lock, flags);
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001371 /* control and status byte(s) are also counted */
1372 priv->bytes_in += bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 spin_unlock_irqrestore(&priv->lock, flags);
1374
1375continue_read:
Alan Cox4a90f092008-10-13 10:39:46 +01001376 tty_kref_put(tty);
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001377
1378 /* Continue trying to always read... unless the port has closed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
Alan Cox95da3102008-07-22 11:09:07 +01001380 if (port->port.count > 0 && priv->comm_is_ok) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001381 usb_fill_int_urb(port->interrupt_in_urb, port->serial->dev,
1382 usb_rcvintpipe(port->serial->dev,
1383 port->interrupt_in_endpointAddress),
1384 port->interrupt_in_urb->transfer_buffer,
1385 port->interrupt_in_urb->transfer_buffer_length,
Alan Cox813a2242008-07-22 11:10:36 +01001386 cypress_read_int_callback, port,
1387 priv->read_urb_interval);
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001388 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
Mike Isely78aef512006-08-29 22:07:11 -05001389 if (result) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001390 dev_err(&urb->dev->dev, "%s - failed resubmitting "
Harvey Harrison441b62c2008-03-03 16:08:34 -08001391 "read urb, error %d\n", __func__,
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001392 result);
Mike Isely78aef512006-08-29 22:07:11 -05001393 cypress_set_dead(port);
1394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 }
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001396
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 return;
1398} /* cypress_read_int_callback */
1399
1400
David Howells7d12e782006-10-05 14:55:46 +01001401static void cypress_write_int_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402{
Ming Leicdc97792008-02-24 18:41:47 +08001403 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 struct cypress_private *priv = usb_get_serial_port_data(port);
1405 int result;
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001406 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Harvey Harrison441b62c2008-03-03 16:08:34 -08001408 dbg("%s - port %d", __func__, port->number);
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001409
1410 switch (status) {
Alan Cox813a2242008-07-22 11:10:36 +01001411 case 0:
1412 /* success */
1413 break;
1414 case -ECONNRESET:
1415 case -ENOENT:
1416 case -ESHUTDOWN:
1417 /* this urb is terminated, clean up */
1418 dbg("%s - urb shutting down with status: %d",
1419 __func__, status);
1420 priv->write_urb_in_use = 0;
1421 return;
1422 case -EPIPE: /* no break needed; clear halt and resubmit */
1423 if (!priv->comm_is_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 break;
Alan Cox813a2242008-07-22 11:10:36 +01001425 usb_clear_halt(port->serial->dev, 0x02);
1426 /* error in the urb, so we have to resubmit it */
1427 dbg("%s - nonzero write bulk status received: %d",
1428 __func__, status);
1429 port->interrupt_out_urb->transfer_buffer_length = 1;
1430 port->interrupt_out_urb->dev = port->serial->dev;
1431 result = usb_submit_urb(port->interrupt_out_urb, GFP_ATOMIC);
1432 if (!result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 return;
Alan Cox813a2242008-07-22 11:10:36 +01001434 dev_err(&urb->dev->dev,
1435 "%s - failed resubmitting write urb, error %d\n",
1436 __func__, result);
1437 cypress_set_dead(port);
1438 break;
1439 default:
1440 dev_err(&urb->dev->dev,
1441 "%s - unexpected nonzero write status received: %d\n",
1442 __func__, status);
1443 cypress_set_dead(port);
1444 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 priv->write_urb_in_use = 0;
Alan Cox813a2242008-07-22 11:10:36 +01001447
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 /* send any buffered data */
1449 cypress_send(port);
1450}
1451
1452
1453/*****************************************************************************
1454 * Write buffer functions - buffering code from pl2303 used
1455 *****************************************************************************/
1456
1457/*
1458 * cypress_buf_alloc
1459 *
1460 * Allocate a circular buffer and all associated memory.
1461 */
1462
1463static struct cypress_buf *cypress_buf_alloc(unsigned int size)
1464{
1465
1466 struct cypress_buf *cb;
1467
1468
1469 if (size == 0)
1470 return NULL;
1471
Robert P. J. Day5cbded52006-12-13 00:35:56 -08001472 cb = kmalloc(sizeof(struct cypress_buf), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 if (cb == NULL)
1474 return NULL;
1475
1476 cb->buf_buf = kmalloc(size, GFP_KERNEL);
1477 if (cb->buf_buf == NULL) {
1478 kfree(cb);
1479 return NULL;
1480 }
1481
1482 cb->buf_size = size;
1483 cb->buf_get = cb->buf_put = cb->buf_buf;
1484
1485 return cb;
1486
1487}
1488
1489
1490/*
1491 * cypress_buf_free
1492 *
1493 * Free the buffer and all associated memory.
1494 */
1495
1496static void cypress_buf_free(struct cypress_buf *cb)
1497{
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001498 if (cb) {
1499 kfree(cb->buf_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 kfree(cb);
1501 }
1502}
1503
1504
1505/*
1506 * cypress_buf_clear
1507 *
1508 * Clear out all data in the circular buffer.
1509 */
1510
1511static void cypress_buf_clear(struct cypress_buf *cb)
1512{
1513 if (cb != NULL)
1514 cb->buf_get = cb->buf_put;
1515 /* equivalent to a get of all data available */
1516}
1517
1518
1519/*
1520 * cypress_buf_data_avail
1521 *
1522 * Return the number of bytes of data available in the circular
1523 * buffer.
1524 */
1525
1526static unsigned int cypress_buf_data_avail(struct cypress_buf *cb)
1527{
1528 if (cb != NULL)
Alan Cox813a2242008-07-22 11:10:36 +01001529 return (cb->buf_size + cb->buf_put - cb->buf_get)
1530 % cb->buf_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 else
1532 return 0;
1533}
1534
1535
1536/*
1537 * cypress_buf_space_avail
1538 *
1539 * Return the number of bytes of space available in the circular
1540 * buffer.
1541 */
1542
1543static unsigned int cypress_buf_space_avail(struct cypress_buf *cb)
1544{
1545 if (cb != NULL)
Alan Cox813a2242008-07-22 11:10:36 +01001546 return (cb->buf_size + cb->buf_get - cb->buf_put - 1)
1547 % cb->buf_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 else
1549 return 0;
1550}
1551
1552
1553/*
1554 * cypress_buf_put
1555 *
1556 * Copy data data from a user buffer and put it into the circular buffer.
1557 * Restrict to the amount of space available.
1558 *
1559 * Return the number of bytes copied.
1560 */
1561
1562static unsigned int cypress_buf_put(struct cypress_buf *cb, const char *buf,
1563 unsigned int count)
1564{
1565
1566 unsigned int len;
1567
1568
1569 if (cb == NULL)
1570 return 0;
1571
1572 len = cypress_buf_space_avail(cb);
1573 if (count > len)
1574 count = len;
1575
1576 if (count == 0)
1577 return 0;
1578
1579 len = cb->buf_buf + cb->buf_size - cb->buf_put;
1580 if (count > len) {
1581 memcpy(cb->buf_put, buf, len);
1582 memcpy(cb->buf_buf, buf+len, count - len);
1583 cb->buf_put = cb->buf_buf + count - len;
1584 } else {
1585 memcpy(cb->buf_put, buf, count);
1586 if (count < len)
1587 cb->buf_put += count;
1588 else /* count == len */
1589 cb->buf_put = cb->buf_buf;
1590 }
1591
1592 return count;
1593
1594}
1595
1596
1597/*
1598 * cypress_buf_get
1599 *
1600 * Get data from the circular buffer and copy to the given buffer.
1601 * Restrict to the amount of data available.
1602 *
1603 * Return the number of bytes copied.
1604 */
1605
1606static unsigned int cypress_buf_get(struct cypress_buf *cb, char *buf,
1607 unsigned int count)
1608{
1609
1610 unsigned int len;
1611
1612
1613 if (cb == NULL)
1614 return 0;
1615
1616 len = cypress_buf_data_avail(cb);
1617 if (count > len)
1618 count = len;
1619
1620 if (count == 0)
1621 return 0;
1622
1623 len = cb->buf_buf + cb->buf_size - cb->buf_get;
1624 if (count > len) {
1625 memcpy(buf, cb->buf_get, len);
1626 memcpy(buf+len, cb->buf_buf, count - len);
1627 cb->buf_get = cb->buf_buf + count - len;
1628 } else {
1629 memcpy(buf, cb->buf_get, count);
1630 if (count < len)
1631 cb->buf_get += count;
1632 else /* count == len */
1633 cb->buf_get = cb->buf_buf;
1634 }
1635
1636 return count;
1637
1638}
1639
1640/*****************************************************************************
1641 * Module functions
1642 *****************************************************************************/
1643
1644static int __init cypress_init(void)
1645{
1646 int retval;
Alan Cox813a2242008-07-22 11:10:36 +01001647
Harvey Harrison441b62c2008-03-03 16:08:34 -08001648 dbg("%s", __func__);
Alan Cox813a2242008-07-22 11:10:36 +01001649
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 retval = usb_serial_register(&cypress_earthmate_device);
1651 if (retval)
1652 goto failed_em_register;
1653 retval = usb_serial_register(&cypress_hidcom_device);
1654 if (retval)
1655 goto failed_hidcom_register;
Lonnie Mendeza5c44e22006-03-01 10:45:24 -06001656 retval = usb_serial_register(&cypress_ca42v2_device);
1657 if (retval)
1658 goto failed_ca42v2_register;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 retval = usb_register(&cypress_driver);
1660 if (retval)
1661 goto failed_usb_register;
1662
Greg Kroah-Hartmanc197a8d2008-08-18 13:21:04 -07001663 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
1664 DRIVER_DESC "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
Mariusz Kozlowski2e46b742006-11-17 17:49:22 +01001667failed_usb_register:
1668 usb_serial_deregister(&cypress_ca42v2_device);
1669failed_ca42v2_register:
1670 usb_serial_deregister(&cypress_hidcom_device);
1671failed_hidcom_register:
1672 usb_serial_deregister(&cypress_earthmate_device);
1673failed_em_register:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 return retval;
1675}
1676
1677
Alan Cox813a2242008-07-22 11:10:36 +01001678static void __exit cypress_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679{
Harvey Harrison441b62c2008-03-03 16:08:34 -08001680 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
Alan Cox813a2242008-07-22 11:10:36 +01001682 usb_deregister(&cypress_driver);
1683 usb_serial_deregister(&cypress_earthmate_device);
1684 usb_serial_deregister(&cypress_hidcom_device);
1685 usb_serial_deregister(&cypress_ca42v2_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686}
1687
1688
1689module_init(cypress_init);
1690module_exit(cypress_exit);
1691
Alan Cox813a2242008-07-22 11:10:36 +01001692MODULE_AUTHOR(DRIVER_AUTHOR);
1693MODULE_DESCRIPTION(DRIVER_DESC);
1694MODULE_VERSION(DRIVER_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695MODULE_LICENSE("GPL");
1696
1697module_param(debug, bool, S_IRUGO | S_IWUSR);
1698MODULE_PARM_DESC(debug, "Debug enabled or not");
1699module_param(stats, bool, S_IRUGO | S_IWUSR);
1700MODULE_PARM_DESC(stats, "Enable statistics or not");
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001701module_param(interval, int, S_IRUGO | S_IWUSR);
1702MODULE_PARM_DESC(interval, "Overrides interrupt interval");