blob: 36f8ef0794791652179229ae33da02686450fedf [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * USB Cypress M8 driver
3 *
4 * Copyright (C) 2004
5 * Lonnie Mendez (dignome@gmail.com)
6 * Copyright (C) 2003,2004
7 * Neil Whelchel (koyama@firstlight.net)
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * See Documentation/usb/usb-serial.txt for more information on using this driver
15 *
16 * See http://geocities.com/i0xox0i for information on this driver and the
17 * earthmate usb device.
18 *
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -050019 * Lonnie Mendez <dignome@gmail.com>
20 * 4-29-2005
21 * Fixed problem where setting or retreiving the serial config would fail with
22 * EPIPE. Removed CRTS toggling so the driver behaves more like other usbserial
23 * adapters. Issued new interval of 1ms instead of the default 10ms. As a
24 * result, transfer speed has been substantially increased. From avg. 850bps to
25 * avg. 3300bps. initial termios has also been modified. Cleaned up code and
26 * formatting issues so it is more readable. Replaced the C++ style comments.
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 *
28 * Lonnie Mendez <dignome@gmail.com>
29 * 12-15-2004
30 * Incorporated write buffering from pl2303 driver. Fixed bug with line
31 * handling so both lines are raised in cypress_open. (was dropping rts)
32 * Various code cleanups made as well along with other misc bug fixes.
33 *
34 * Lonnie Mendez <dignome@gmail.com>
35 * 04-10-2004
36 * Driver modified to support dynamic line settings. Various improvments
37 * and features.
38 *
39 * Neil Whelchel
40 * 10-2003
41 * Driver first released.
42 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 */
44
45/* Thanks to Neil Whelchel for writing the first cypress m8 implementation for linux. */
46/* Thanks to cypress for providing references for the hid reports. */
47/* Thanks to Jiang Zhang for providing links and for general help. */
48/* Code originates and was built up from ftdi_sio, belkin, pl2303 and others. */
49
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/kernel.h>
52#include <linux/errno.h>
53#include <linux/init.h>
54#include <linux/slab.h>
55#include <linux/tty.h>
56#include <linux/tty_driver.h>
57#include <linux/tty_flip.h>
58#include <linux/module.h>
59#include <linux/moduleparam.h>
60#include <linux/spinlock.h>
61#include <linux/usb.h>
Greg Kroah-Hartmana9698882006-07-11 21:22:58 -070062#include <linux/usb/serial.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include <linux/serial.h>
64#include <linux/delay.h>
65#include <asm/uaccess.h>
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#include "cypress_m8.h"
68
69
70#ifdef CONFIG_USB_SERIAL_DEBUG
71 static int debug = 1;
72#else
73 static int debug;
74#endif
75static int stats;
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -050076static int interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78/*
79 * Version Information
80 */
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -050081#define DRIVER_VERSION "v1.09"
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#define DRIVER_AUTHOR "Lonnie Mendez <dignome@gmail.com>, Neil Whelchel <koyama@firstlight.net>"
83#define DRIVER_DESC "Cypress USB to Serial Driver"
84
85/* write buffer size defines */
86#define CYPRESS_BUF_SIZE 1024
87#define CYPRESS_CLOSING_WAIT (30*HZ)
88
89static struct usb_device_id id_table_earthmate [] = {
90 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB) },
Lonnie Mendez25b6f082005-05-10 17:09:52 -050091 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB_LT20) },
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 { } /* Terminating entry */
93};
94
95static struct usb_device_id id_table_cyphidcomrs232 [] = {
96 { USB_DEVICE(VENDOR_ID_CYPRESS, PRODUCT_ID_CYPHIDCOM) },
Dmitry Shapin6f6f06e2008-03-04 15:25:10 -080097 { USB_DEVICE(VENDOR_ID_POWERCOM, PRODUCT_ID_UPS) },
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 { } /* Terminating entry */
99};
100
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600101static struct usb_device_id id_table_nokiaca42v2 [] = {
102 { USB_DEVICE(VENDOR_ID_DAZZLE, PRODUCT_ID_CA42) },
103 { } /* Terminating entry */
104};
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106static struct usb_device_id id_table_combined [] = {
107 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB) },
Lonnie Mendez25b6f082005-05-10 17:09:52 -0500108 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB_LT20) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 { USB_DEVICE(VENDOR_ID_CYPRESS, PRODUCT_ID_CYPHIDCOM) },
Dmitry Shapin6f6f06e2008-03-04 15:25:10 -0800110 { USB_DEVICE(VENDOR_ID_POWERCOM, PRODUCT_ID_UPS) },
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600111 { USB_DEVICE(VENDOR_ID_DAZZLE, PRODUCT_ID_CA42) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 { } /* Terminating entry */
113};
114
115MODULE_DEVICE_TABLE (usb, id_table_combined);
116
117static struct usb_driver cypress_driver = {
118 .name = "cypress",
119 .probe = usb_serial_probe,
120 .disconnect = usb_serial_disconnect,
121 .id_table = id_table_combined,
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800122 .no_dynamic_id = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123};
124
Mike Isely3416eaa2008-02-10 20:23:19 -0600125enum packet_format {
126 packet_format_1, /* b0:status, b1:payload count */
127 packet_format_2 /* b0[7:3]:status, b0[2:0]:payload count */
128};
129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130struct cypress_private {
131 spinlock_t lock; /* private lock */
132 int chiptype; /* identifier of device, for quirks/etc */
133 int bytes_in; /* used for statistics */
134 int bytes_out; /* used for statistics */
135 int cmd_count; /* used for statistics */
136 int cmd_ctrl; /* always set this to 1 before issuing a command */
137 struct cypress_buf *buf; /* write buffer */
138 int write_urb_in_use; /* write urb in use indicator */
Mike Isely0257fa92006-08-29 22:06:59 -0500139 int write_urb_interval; /* interval to use for write urb */
140 int read_urb_interval; /* interval to use for read urb */
Mike Isely78aef512006-08-29 22:07:11 -0500141 int comm_is_ok; /* true if communication is (still) ok */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 int termios_initialized;
143 __u8 line_control; /* holds dtr / rts value */
144 __u8 current_status; /* received from last read - info on dsr,cts,cd,ri,etc */
145 __u8 current_config; /* stores the current configuration byte */
146 __u8 rx_flags; /* throttling - used from whiteheat/ftdi_sio */
Mike Isely3416eaa2008-02-10 20:23:19 -0600147 enum packet_format pkt_fmt; /* format to use for packet send / receive */
Mike Isely3d6aa322008-02-10 20:23:24 -0600148 int get_cfg_unsafe; /* If true, the CYPRESS_GET_CONFIG is unsafe */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 int baud_rate; /* stores current baud rate in integer form */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 int isthrottled; /* if throttled, discard reads */
151 wait_queue_head_t delta_msr_wait; /* used for TIOCMIWAIT */
152 char prev_status, diff_status; /* used for TIOCMIWAIT */
153 /* we pass a pointer to this as the arguement sent to cypress_set_termios old_termios */
Alan Cox606d0992006-12-08 02:38:45 -0800154 struct ktermios tmp_termios; /* stores the old termios settings */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155};
156
157/* write buffer structure */
158struct cypress_buf {
159 unsigned int buf_size;
160 char *buf_buf;
161 char *buf_get;
162 char *buf_put;
163};
164
165/* function prototypes for the Cypress USB to serial device */
166static int cypress_earthmate_startup (struct usb_serial *serial);
167static int cypress_hidcom_startup (struct usb_serial *serial);
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600168static int cypress_ca42v2_startup (struct usb_serial *serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169static void cypress_shutdown (struct usb_serial *serial);
170static int cypress_open (struct usb_serial_port *port, struct file *filp);
171static void cypress_close (struct usb_serial_port *port, struct file *filp);
172static int cypress_write (struct usb_serial_port *port, const unsigned char *buf, int count);
173static void cypress_send (struct usb_serial_port *port);
174static int cypress_write_room (struct usb_serial_port *port);
175static int cypress_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg);
Alan Cox606d0992006-12-08 02:38:45 -0800176static void cypress_set_termios (struct usb_serial_port *port, struct ktermios * old);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177static int cypress_tiocmget (struct usb_serial_port *port, struct file *file);
178static int cypress_tiocmset (struct usb_serial_port *port, struct file *file, unsigned int set, unsigned int clear);
179static int cypress_chars_in_buffer (struct usb_serial_port *port);
180static void cypress_throttle (struct usb_serial_port *port);
181static void cypress_unthrottle (struct usb_serial_port *port);
Mike Isely78aef512006-08-29 22:07:11 -0500182static void cypress_set_dead (struct usb_serial_port *port);
David Howells7d12e782006-10-05 14:55:46 +0100183static void cypress_read_int_callback (struct urb *urb);
184static void cypress_write_int_callback (struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185/* write buffer functions */
186static struct cypress_buf *cypress_buf_alloc(unsigned int size);
187static void cypress_buf_free(struct cypress_buf *cb);
188static void cypress_buf_clear(struct cypress_buf *cb);
189static unsigned int cypress_buf_data_avail(struct cypress_buf *cb);
190static unsigned int cypress_buf_space_avail(struct cypress_buf *cb);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500191static unsigned int cypress_buf_put(struct cypress_buf *cb, const char *buf, unsigned int count);
192static unsigned int cypress_buf_get(struct cypress_buf *cb, char *buf, unsigned int count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700195static struct usb_serial_driver cypress_earthmate_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700196 .driver = {
197 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700198 .name = "earthmate",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700199 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700200 .description = "DeLorme Earthmate USB",
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100201 .usb_driver = &cypress_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 .id_table = id_table_earthmate,
203 .num_interrupt_in = 1,
204 .num_interrupt_out = 1,
205 .num_bulk_in = NUM_DONT_CARE,
206 .num_bulk_out = NUM_DONT_CARE,
207 .num_ports = 1,
208 .attach = cypress_earthmate_startup,
209 .shutdown = cypress_shutdown,
210 .open = cypress_open,
211 .close = cypress_close,
212 .write = cypress_write,
213 .write_room = cypress_write_room,
214 .ioctl = cypress_ioctl,
215 .set_termios = cypress_set_termios,
216 .tiocmget = cypress_tiocmget,
217 .tiocmset = cypress_tiocmset,
218 .chars_in_buffer = cypress_chars_in_buffer,
219 .throttle = cypress_throttle,
220 .unthrottle = cypress_unthrottle,
221 .read_int_callback = cypress_read_int_callback,
222 .write_int_callback = cypress_write_int_callback,
223};
224
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700225static struct usb_serial_driver cypress_hidcom_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700226 .driver = {
227 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700228 .name = "cyphidcom",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700229 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700230 .description = "HID->COM RS232 Adapter",
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100231 .usb_driver = &cypress_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 .id_table = id_table_cyphidcomrs232,
233 .num_interrupt_in = 1,
234 .num_interrupt_out = 1,
235 .num_bulk_in = NUM_DONT_CARE,
236 .num_bulk_out = NUM_DONT_CARE,
237 .num_ports = 1,
238 .attach = cypress_hidcom_startup,
239 .shutdown = cypress_shutdown,
240 .open = cypress_open,
241 .close = cypress_close,
242 .write = cypress_write,
243 .write_room = cypress_write_room,
244 .ioctl = cypress_ioctl,
245 .set_termios = cypress_set_termios,
246 .tiocmget = cypress_tiocmget,
247 .tiocmset = cypress_tiocmset,
248 .chars_in_buffer = cypress_chars_in_buffer,
249 .throttle = cypress_throttle,
250 .unthrottle = cypress_unthrottle,
251 .read_int_callback = cypress_read_int_callback,
252 .write_int_callback = cypress_write_int_callback,
253};
254
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600255static struct usb_serial_driver cypress_ca42v2_device = {
256 .driver = {
257 .owner = THIS_MODULE,
258 .name = "nokiaca42v2",
259 },
260 .description = "Nokia CA-42 V2 Adapter",
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100261 .usb_driver = &cypress_driver,
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600262 .id_table = id_table_nokiaca42v2,
263 .num_interrupt_in = 1,
264 .num_interrupt_out = 1,
265 .num_bulk_in = NUM_DONT_CARE,
266 .num_bulk_out = NUM_DONT_CARE,
267 .num_ports = 1,
268 .attach = cypress_ca42v2_startup,
269 .shutdown = cypress_shutdown,
270 .open = cypress_open,
271 .close = cypress_close,
272 .write = cypress_write,
273 .write_room = cypress_write_room,
274 .ioctl = cypress_ioctl,
275 .set_termios = cypress_set_termios,
276 .tiocmget = cypress_tiocmget,
277 .tiocmset = cypress_tiocmset,
278 .chars_in_buffer = cypress_chars_in_buffer,
279 .throttle = cypress_throttle,
280 .unthrottle = cypress_unthrottle,
281 .read_int_callback = cypress_read_int_callback,
282 .write_int_callback = cypress_write_int_callback,
283};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285/*****************************************************************************
286 * Cypress serial helper functions
287 *****************************************************************************/
288
289
Alan Cox8873aaa2008-03-10 21:59:28 +0000290static int analyze_baud_rate(struct usb_serial_port *port, speed_t new_rate)
Mike Isely92983c22008-02-10 20:23:32 -0600291{
Mike Isely92983c22008-02-10 20:23:32 -0600292 struct cypress_private *priv;
293 priv = usb_get_serial_port_data(port);
294
295 /*
296 * The general purpose firmware for the Cypress M8 allows for
297 * a maximum speed of 57600bps (I have no idea whether DeLorme
298 * chose to use the general purpose firmware or not), if you
299 * need to modify this speed setting for your own project
300 * please add your own chiptype and modify the code likewise.
301 * The Cypress HID->COM device will work successfully up to
302 * 115200bps (but the actual throughput is around 3kBps).
303 */
Mike Isely92983c22008-02-10 20:23:32 -0600304 if (port->serial->dev->speed == USB_SPEED_LOW) {
305 /*
306 * Mike Isely <isely@pobox.com> 2-Feb-2008: The
307 * Cypress app note that describes this mechanism
308 * states the the low-speed part can't handle more
309 * than 800 bytes/sec, in which case 4800 baud is the
310 * safest speed for a part like that.
311 */
312 if (new_rate > 4800) {
313 dbg("%s - failed setting baud rate, device incapable "
314 "speed %d", __func__, new_rate);
315 return -1;
316 }
317 }
318 switch (priv->chiptype) {
319 case CT_EARTHMATE:
320 if (new_rate <= 600) {
321 /* 300 and 600 baud rates are supported under
322 * the generic firmware, but are not used with
323 * NMEA and SiRF protocols */
324 dbg("%s - failed setting baud rate, unsupported speed "
325 "of %d on Earthmate GPS", __func__, new_rate);
326 return -1;
327 }
328 break;
329 default:
330 break;
331 }
332 return new_rate;
333}
334
335
Steven Cole093cf722005-05-03 19:07:24 -0600336/* This function can either set or retrieve the current serial line settings */
Alan Cox8873aaa2008-03-10 21:59:28 +0000337static int cypress_serial_control (struct usb_serial_port *port, speed_t baud_rate, int data_bits, int stop_bits,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 int parity_enable, int parity_type, int reset, int cypress_request_type)
339{
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500340 int new_baudrate = 0, retval = 0, tries = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 struct cypress_private *priv;
Mike Isely93075542008-02-10 20:23:14 -0600342 __u8 feature_buffer[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 unsigned long flags;
344
345 dbg("%s", __FUNCTION__);
346
347 priv = usb_get_serial_port_data(port);
348
Mike Isely78aef512006-08-29 22:07:11 -0500349 if (!priv->comm_is_ok)
350 return -ENODEV;
351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 switch(cypress_request_type) {
353 case CYPRESS_SET_CONFIG:
Mike Isely92983c22008-02-10 20:23:32 -0600354 new_baudrate = priv->baud_rate;
Alan Cox8873aaa2008-03-10 21:59:28 +0000355 /* 0 means 'Hang up' so doesn't change the true bit rate */
356 if (baud_rate == 0)
357 new_baudrate = priv->baud_rate;
358 /* Change of speed ? */
359 else if (baud_rate != priv->baud_rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 dbg("%s - baud rate is changing", __FUNCTION__);
Alan Cox8873aaa2008-03-10 21:59:28 +0000361 retval = analyze_baud_rate(port, baud_rate);
Mike Isely92983c22008-02-10 20:23:32 -0600362 if (retval >= 0) {
363 new_baudrate = retval;
364 dbg("%s - New baud rate set to %d",
365 __func__, new_baudrate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 }
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500368 dbg("%s - baud rate is being sent as %d", __FUNCTION__, new_baudrate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Mike Isely93075542008-02-10 20:23:14 -0600370 memset(feature_buffer, 0, sizeof(feature_buffer));
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500371 /* fill the feature_buffer with new configuration */
372 *((u_int32_t *)feature_buffer) = new_baudrate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500374 feature_buffer[4] |= data_bits; /* assign data bits in 2 bit space ( max 3 ) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 /* 1 bit gap */
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500376 feature_buffer[4] |= (stop_bits << 3); /* assign stop bits in 1 bit space */
377 feature_buffer[4] |= (parity_enable << 4); /* assign parity flag in 1 bit space */
378 feature_buffer[4] |= (parity_type << 5); /* assign parity type in 1 bit space */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 /* 1 bit gap */
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500380 feature_buffer[4] |= (reset << 7); /* assign reset at end of byte, 1 bit space */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
382 dbg("%s - device is being sent this feature report:", __FUNCTION__);
383 dbg("%s - %02X - %02X - %02X - %02X - %02X", __FUNCTION__, feature_buffer[0], feature_buffer[1],
384 feature_buffer[2], feature_buffer[3], feature_buffer[4]);
385
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500386 do {
Mike Isely93075542008-02-10 20:23:14 -0600387 retval = usb_control_msg(port->serial->dev,
388 usb_sndctrlpipe(port->serial->dev, 0),
389 HID_REQ_SET_REPORT,
390 USB_DIR_OUT | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
391 0x0300, 0, feature_buffer,
392 sizeof(feature_buffer), 500);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500394 if (tries++ >= 3)
395 break;
396
Mike Isely93075542008-02-10 20:23:14 -0600397 } while (retval != sizeof(feature_buffer) &&
398 retval != -ENODEV);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500399
Mike Isely93075542008-02-10 20:23:14 -0600400 if (retval != sizeof(feature_buffer)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 err("%s - failed sending serial line settings - %d", __FUNCTION__, retval);
Mike Isely78aef512006-08-29 22:07:11 -0500402 cypress_set_dead(port);
403 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 spin_lock_irqsave(&priv->lock, flags);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500405 priv->baud_rate = new_baudrate;
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500406 priv->current_config = feature_buffer[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 spin_unlock_irqrestore(&priv->lock, flags);
Alan Cox8873aaa2008-03-10 21:59:28 +0000408 /* If we asked for a speed change encode it */
409 if (baud_rate)
410 tty_encode_baud_rate(port->tty,
411 new_baudrate, new_baudrate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 }
413 break;
414 case CYPRESS_GET_CONFIG:
Mike Isely3d6aa322008-02-10 20:23:24 -0600415 if (priv->get_cfg_unsafe) {
416 /* Not implemented for this device,
417 and if we try to do it we're likely
418 to crash the hardware. */
419 return -ENOTTY;
420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 dbg("%s - retreiving serial line settings", __FUNCTION__);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500422 /* set initial values in feature buffer */
Mike Isely93075542008-02-10 20:23:14 -0600423 memset(feature_buffer, 0, sizeof(feature_buffer));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500425 do {
Mike Isely93075542008-02-10 20:23:14 -0600426 retval = usb_control_msg(port->serial->dev,
427 usb_rcvctrlpipe(port->serial->dev, 0),
428 HID_REQ_GET_REPORT,
429 USB_DIR_IN | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
430 0x0300, 0, feature_buffer,
431 sizeof(feature_buffer), 500);
432
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500433 if (tries++ >= 3)
434 break;
435
Mike Isely93075542008-02-10 20:23:14 -0600436 } while (retval != sizeof(feature_buffer) &&
437 retval != -ENODEV);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500438
Mike Isely93075542008-02-10 20:23:14 -0600439 if (retval != sizeof(feature_buffer)) {
Adrian Bunk943ffb52006-01-10 00:10:13 +0100440 err("%s - failed to retrieve serial line settings - %d", __FUNCTION__, retval);
Mike Isely78aef512006-08-29 22:07:11 -0500441 cypress_set_dead(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 return retval;
443 } else {
444 spin_lock_irqsave(&priv->lock, flags);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 /* store the config in one byte, and later use bit masks to check values */
447 priv->current_config = feature_buffer[4];
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500448 priv->baud_rate = *((u_int32_t *)feature_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 spin_unlock_irqrestore(&priv->lock, flags);
450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 }
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500452 spin_lock_irqsave(&priv->lock, flags);
453 ++priv->cmd_count;
454 spin_unlock_irqrestore(&priv->lock, flags);
455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 return retval;
457} /* cypress_serial_control */
458
459
Mike Isely78aef512006-08-29 22:07:11 -0500460static void cypress_set_dead(struct usb_serial_port *port)
461{
462 struct cypress_private *priv = usb_get_serial_port_data(port);
463 unsigned long flags;
464
465 spin_lock_irqsave(&priv->lock, flags);
466 if (!priv->comm_is_ok) {
467 spin_unlock_irqrestore(&priv->lock, flags);
468 return;
469 }
470 priv->comm_is_ok = 0;
471 spin_unlock_irqrestore(&priv->lock, flags);
472
473 err("cypress_m8 suspending failing port %d - interval might be too short",
474 port->number);
475}
476
477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478/*****************************************************************************
479 * Cypress serial driver functions
480 *****************************************************************************/
481
482
483static int generic_startup (struct usb_serial *serial)
484{
485 struct cypress_private *priv;
Mike Isely0257fa92006-08-29 22:06:59 -0500486 struct usb_serial_port *port = serial->port[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Mike Isely0257fa92006-08-29 22:06:59 -0500488 dbg("%s - port %d", __FUNCTION__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +0100490 priv = kzalloc(sizeof (struct cypress_private), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 if (!priv)
492 return -ENOMEM;
493
Mike Isely78aef512006-08-29 22:07:11 -0500494 priv->comm_is_ok = !0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 spin_lock_init(&priv->lock);
496 priv->buf = cypress_buf_alloc(CYPRESS_BUF_SIZE);
497 if (priv->buf == NULL) {
498 kfree(priv);
499 return -ENOMEM;
500 }
501 init_waitqueue_head(&priv->delta_msr_wait);
502
503 usb_reset_configuration (serial->dev);
504
505 priv->cmd_ctrl = 0;
506 priv->line_control = 0;
507 priv->termios_initialized = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 priv->rx_flags = 0;
Mike Isely3416eaa2008-02-10 20:23:19 -0600509 /* Default packet format setting is determined by packet size.
510 Anything with a size larger then 9 must have a separate
511 count field since the 3 bit count field is otherwise too
512 small. Otherwise we can use the slightly more compact
513 format. This is in accordance with the cypress_m8 serial
514 converter app note. */
515 if (port->interrupt_out_size > 9) {
516 priv->pkt_fmt = packet_format_1;
517 } else {
518 priv->pkt_fmt = packet_format_2;
519 }
Mike Isely0257fa92006-08-29 22:06:59 -0500520 if (interval > 0) {
521 priv->write_urb_interval = interval;
522 priv->read_urb_interval = interval;
523 dbg("%s - port %d read & write intervals forced to %d",
524 __FUNCTION__,port->number,interval);
525 } else {
526 priv->write_urb_interval = port->interrupt_out_urb->interval;
527 priv->read_urb_interval = port->interrupt_in_urb->interval;
528 dbg("%s - port %d intervals: read=%d write=%d",
529 __FUNCTION__,port->number,
530 priv->read_urb_interval,priv->write_urb_interval);
531 }
532 usb_set_serial_port_data(port, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500534 return 0;
535}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
537
538static int cypress_earthmate_startup (struct usb_serial *serial)
539{
540 struct cypress_private *priv;
Mike Isely3d6aa322008-02-10 20:23:24 -0600541 struct usb_serial_port *port = serial->port[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
543 dbg("%s", __FUNCTION__);
544
545 if (generic_startup(serial)) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500546 dbg("%s - Failed setting up port %d", __FUNCTION__,
Mike Isely3d6aa322008-02-10 20:23:24 -0600547 port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 return 1;
549 }
550
Mike Isely3d6aa322008-02-10 20:23:24 -0600551 priv = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 priv->chiptype = CT_EARTHMATE;
Mike Isely3416eaa2008-02-10 20:23:19 -0600553 /* All Earthmate devices use the separated-count packet
554 format! Idiotic. */
555 priv->pkt_fmt = packet_format_1;
Mike Isely3d6aa322008-02-10 20:23:24 -0600556 if (serial->dev->descriptor.idProduct != PRODUCT_ID_EARTHMATEUSB) {
557 /* The old original USB Earthmate seemed able to
558 handle GET_CONFIG requests; everything they've
559 produced since that time crashes if this command is
560 attempted :-( */
561 dbg("%s - Marking this device as unsafe for GET_CONFIG "
562 "commands", __func__);
563 priv->get_cfg_unsafe = !0;
564 }
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500565
566 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567} /* cypress_earthmate_startup */
568
569
570static int cypress_hidcom_startup (struct usb_serial *serial)
571{
572 struct cypress_private *priv;
573
574 dbg("%s", __FUNCTION__);
575
576 if (generic_startup(serial)) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500577 dbg("%s - Failed setting up port %d", __FUNCTION__,
578 serial->port[0]->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 return 1;
580 }
581
582 priv = usb_get_serial_port_data(serial->port[0]);
583 priv->chiptype = CT_CYPHIDCOM;
584
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500585 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586} /* cypress_hidcom_startup */
587
588
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600589static int cypress_ca42v2_startup (struct usb_serial *serial)
590{
591 struct cypress_private *priv;
592
593 dbg("%s", __FUNCTION__);
594
595 if (generic_startup(serial)) {
596 dbg("%s - Failed setting up port %d", __FUNCTION__,
597 serial->port[0]->number);
598 return 1;
599 }
600
601 priv = usb_get_serial_port_data(serial->port[0]);
602 priv->chiptype = CT_CA42V2;
603
604 return 0;
605} /* cypress_ca42v2_startup */
606
607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608static void cypress_shutdown (struct usb_serial *serial)
609{
610 struct cypress_private *priv;
611
612 dbg ("%s - port %d", __FUNCTION__, serial->port[0]->number);
613
614 /* all open ports are closed at this point */
615
616 priv = usb_get_serial_port_data(serial->port[0]);
617
618 if (priv) {
619 cypress_buf_free(priv->buf);
620 kfree(priv);
621 usb_set_serial_port_data(serial->port[0], NULL);
622 }
623}
624
625
626static int cypress_open (struct usb_serial_port *port, struct file *filp)
627{
628 struct cypress_private *priv = usb_get_serial_port_data(port);
629 struct usb_serial *serial = port->serial;
630 unsigned long flags;
631 int result = 0;
632
633 dbg("%s - port %d", __FUNCTION__, port->number);
634
Mike Isely78aef512006-08-29 22:07:11 -0500635 if (!priv->comm_is_ok)
636 return -EIO;
637
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 /* clear halts before open */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 usb_clear_halt(serial->dev, 0x81);
640 usb_clear_halt(serial->dev, 0x02);
641
642 spin_lock_irqsave(&priv->lock, flags);
643 /* reset read/write statistics */
644 priv->bytes_in = 0;
645 priv->bytes_out = 0;
646 priv->cmd_count = 0;
647 priv->rx_flags = 0;
648 spin_unlock_irqrestore(&priv->lock, flags);
649
650 /* setting to zero could cause data loss */
651 port->tty->low_latency = 1;
652
653 /* raise both lines and set termios */
654 spin_lock_irqsave(&priv->lock, flags);
655 priv->line_control = CONTROL_DTR | CONTROL_RTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 priv->cmd_ctrl = 1;
657 spin_unlock_irqrestore(&priv->lock, flags);
658 result = cypress_write(port, NULL, 0);
659
660 if (result) {
661 dev_err(&port->dev, "%s - failed setting the control lines - error %d\n", __FUNCTION__, result);
662 return result;
663 } else
664 dbg("%s - success setting the control lines", __FUNCTION__);
665
666 cypress_set_termios(port, &priv->tmp_termios);
667
668 /* setup the port and start reading from the device */
669 if(!port->interrupt_in_urb){
670 err("%s - interrupt_in_urb is empty!", __FUNCTION__);
671 return(-1);
672 }
673
674 usb_fill_int_urb(port->interrupt_in_urb, serial->dev,
675 usb_rcvintpipe(serial->dev, port->interrupt_in_endpointAddress),
676 port->interrupt_in_urb->transfer_buffer, 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
680 if (result){
681 dev_err(&port->dev, "%s - failed submitting read urb, error %d\n", __FUNCTION__, result);
Mike Isely78aef512006-08-29 22:07:11 -0500682 cypress_set_dead(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 }
684
685 return result;
686} /* cypress_open */
687
688
689static void cypress_close(struct usb_serial_port *port, struct file * filp)
690{
691 struct cypress_private *priv = usb_get_serial_port_data(port);
692 unsigned int c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 int bps;
694 long timeout;
695 wait_queue_t wait;
696
697 dbg("%s - port %d", __FUNCTION__, port->number);
698
699 /* wait for data to drain from buffer */
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100700 spin_lock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 timeout = CYPRESS_CLOSING_WAIT;
702 init_waitqueue_entry(&wait, current);
703 add_wait_queue(&port->tty->write_wait, &wait);
704 for (;;) {
705 set_current_state(TASK_INTERRUPTIBLE);
706 if (cypress_buf_data_avail(priv->buf) == 0
707 || timeout == 0 || signal_pending(current)
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100708 /* without mutex, allowed due to harmless failure mode */
709 || port->serial->disconnected)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 break;
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100711 spin_unlock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 timeout = schedule_timeout(timeout);
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100713 spin_lock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 }
715 set_current_state(TASK_RUNNING);
716 remove_wait_queue(&port->tty->write_wait, &wait);
717 /* clear out any remaining data in the buffer */
718 cypress_buf_clear(priv->buf);
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100719 spin_unlock_irq(&priv->lock);
720
721 /* writing is potentially harmful, lock must be taken */
722 mutex_lock(&port->serial->disc_mutex);
723 if (port->serial->disconnected) {
724 mutex_unlock(&port->serial->disc_mutex);
725 return;
726 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 /* wait for characters to drain from device */
728 bps = tty_get_baud_rate(port->tty);
729 if (bps > 1200)
730 timeout = max((HZ*2560)/bps,HZ/10);
731 else
732 timeout = 2*HZ;
Nishanth Aravamudan22c43862005-08-15 11:30:11 -0700733 schedule_timeout_interruptible(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
735 dbg("%s - stopping urbs", __FUNCTION__);
736 usb_kill_urb (port->interrupt_in_urb);
737 usb_kill_urb (port->interrupt_out_urb);
738
739 if (port->tty) {
740 c_cflag = port->tty->termios->c_cflag;
741 if (c_cflag & HUPCL) {
742 /* drop dtr and rts */
743 priv = usb_get_serial_port_data(port);
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100744 spin_lock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 priv->line_control = 0;
746 priv->cmd_ctrl = 1;
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100747 spin_unlock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 cypress_write(port, NULL, 0);
749 }
750 }
751
752 if (stats)
753 dev_info (&port->dev, "Statistics: %d Bytes In | %d Bytes Out | %d Commands Issued\n",
754 priv->bytes_in, priv->bytes_out, priv->cmd_count);
Oliver Neukum9e3b1d82008-01-22 15:54:54 +0100755 mutex_unlock(&port->serial->disc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756} /* cypress_close */
757
758
759static int cypress_write(struct usb_serial_port *port, const unsigned char *buf, int count)
760{
761 struct cypress_private *priv = usb_get_serial_port_data(port);
762 unsigned long flags;
763
764 dbg("%s - port %d, %d bytes", __FUNCTION__, port->number, count);
765
766 /* line control commands, which need to be executed immediately,
767 are not put into the buffer for obvious reasons.
768 */
769 if (priv->cmd_ctrl) {
770 count = 0;
771 goto finish;
772 }
773
774 if (!count)
775 return count;
776
777 spin_lock_irqsave(&priv->lock, flags);
778 count = cypress_buf_put(priv->buf, buf, count);
779 spin_unlock_irqrestore(&priv->lock, flags);
780
781finish:
782 cypress_send(port);
783
784 return count;
785} /* cypress_write */
786
787
788static void cypress_send(struct usb_serial_port *port)
789{
790 int count = 0, result, offset, actual_size;
791 struct cypress_private *priv = usb_get_serial_port_data(port);
792 unsigned long flags;
793
Mike Isely78aef512006-08-29 22:07:11 -0500794 if (!priv->comm_is_ok)
795 return;
796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 dbg("%s - port %d", __FUNCTION__, port->number);
798 dbg("%s - interrupt out size is %d", __FUNCTION__, port->interrupt_out_size);
799
800 spin_lock_irqsave(&priv->lock, flags);
801 if (priv->write_urb_in_use) {
802 dbg("%s - can't write, urb in use", __FUNCTION__);
803 spin_unlock_irqrestore(&priv->lock, flags);
804 return;
805 }
806 spin_unlock_irqrestore(&priv->lock, flags);
807
808 /* clear buffer */
809 memset(port->interrupt_out_urb->transfer_buffer, 0, port->interrupt_out_size);
810
811 spin_lock_irqsave(&priv->lock, flags);
Mike Isely3416eaa2008-02-10 20:23:19 -0600812 switch (priv->pkt_fmt) {
813 default:
814 case packet_format_1:
815 /* this is for the CY7C64013... */
816 offset = 2;
817 port->interrupt_out_buffer[0] = priv->line_control;
818 break;
819 case packet_format_2:
820 /* this is for the CY7C63743... */
821 offset = 1;
822 port->interrupt_out_buffer[0] = priv->line_control;
823 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 }
825
826 if (priv->line_control & CONTROL_RESET)
827 priv->line_control &= ~CONTROL_RESET;
828
829 if (priv->cmd_ctrl) {
830 priv->cmd_count++;
831 dbg("%s - line control command being issued", __FUNCTION__);
832 spin_unlock_irqrestore(&priv->lock, flags);
833 goto send;
834 } else
835 spin_unlock_irqrestore(&priv->lock, flags);
836
837 count = cypress_buf_get(priv->buf, &port->interrupt_out_buffer[offset],
838 port->interrupt_out_size-offset);
839
840 if (count == 0) {
841 return;
842 }
843
Mike Isely3416eaa2008-02-10 20:23:19 -0600844 switch (priv->pkt_fmt) {
845 default:
846 case packet_format_1:
847 port->interrupt_out_buffer[1] = count;
848 break;
849 case packet_format_2:
850 port->interrupt_out_buffer[0] |= count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 }
852
853 dbg("%s - count is %d", __FUNCTION__, count);
854
855send:
856 spin_lock_irqsave(&priv->lock, flags);
857 priv->write_urb_in_use = 1;
858 spin_unlock_irqrestore(&priv->lock, flags);
859
860 if (priv->cmd_ctrl)
861 actual_size = 1;
862 else
Mike Isely3416eaa2008-02-10 20:23:19 -0600863 actual_size = count +
864 (priv->pkt_fmt == packet_format_1 ? 2 : 1);
865
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, port->interrupt_out_size,
867 port->interrupt_out_urb->transfer_buffer);
868
Mike Isely9aa8dae2006-08-29 22:07:04 -0500869 usb_fill_int_urb(port->interrupt_out_urb, port->serial->dev,
870 usb_sndintpipe(port->serial->dev, port->interrupt_out_endpointAddress),
871 port->interrupt_out_buffer, port->interrupt_out_size,
872 cypress_write_int_callback, port, priv->write_urb_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 result = usb_submit_urb (port->interrupt_out_urb, GFP_ATOMIC);
874 if (result) {
875 dev_err(&port->dev, "%s - failed submitting write urb, error %d\n", __FUNCTION__,
876 result);
877 priv->write_urb_in_use = 0;
Mike Isely78aef512006-08-29 22:07:11 -0500878 cypress_set_dead(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 }
880
881 spin_lock_irqsave(&priv->lock, flags);
882 if (priv->cmd_ctrl) {
883 priv->cmd_ctrl = 0;
884 }
885 priv->bytes_out += count; /* do not count the line control and size bytes */
886 spin_unlock_irqrestore(&priv->lock, flags);
887
Pete Zaitcevcf2c7482006-05-22 21:58:49 -0700888 usb_serial_port_softint(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889} /* cypress_send */
890
891
892/* returns how much space is available in the soft buffer */
893static int cypress_write_room(struct usb_serial_port *port)
894{
895 struct cypress_private *priv = usb_get_serial_port_data(port);
896 int room = 0;
897 unsigned long flags;
898
899 dbg("%s - port %d", __FUNCTION__, port->number);
900
901 spin_lock_irqsave(&priv->lock, flags);
902 room = cypress_buf_space_avail(priv->buf);
903 spin_unlock_irqrestore(&priv->lock, flags);
904
905 dbg("%s - returns %d", __FUNCTION__, room);
906 return room;
907}
908
909
910static int cypress_tiocmget (struct usb_serial_port *port, struct file *file)
911{
912 struct cypress_private *priv = usb_get_serial_port_data(port);
913 __u8 status, control;
914 unsigned int result = 0;
915 unsigned long flags;
916
917 dbg("%s - port %d", __FUNCTION__, port->number);
918
919 spin_lock_irqsave(&priv->lock, flags);
920 control = priv->line_control;
921 status = priv->current_status;
922 spin_unlock_irqrestore(&priv->lock, flags);
923
924 result = ((control & CONTROL_DTR) ? TIOCM_DTR : 0)
925 | ((control & CONTROL_RTS) ? TIOCM_RTS : 0)
926 | ((status & UART_CTS) ? TIOCM_CTS : 0)
927 | ((status & UART_DSR) ? TIOCM_DSR : 0)
928 | ((status & UART_RI) ? TIOCM_RI : 0)
929 | ((status & UART_CD) ? TIOCM_CD : 0);
930
931 dbg("%s - result = %x", __FUNCTION__, result);
932
933 return result;
934}
935
936
937static int cypress_tiocmset (struct usb_serial_port *port, struct file *file,
938 unsigned int set, unsigned int clear)
939{
940 struct cypress_private *priv = usb_get_serial_port_data(port);
941 unsigned long flags;
942
943 dbg("%s - port %d", __FUNCTION__, port->number);
944
945 spin_lock_irqsave(&priv->lock, flags);
946 if (set & TIOCM_RTS)
947 priv->line_control |= CONTROL_RTS;
948 if (set & TIOCM_DTR)
949 priv->line_control |= CONTROL_DTR;
950 if (clear & TIOCM_RTS)
951 priv->line_control &= ~CONTROL_RTS;
952 if (clear & TIOCM_DTR)
953 priv->line_control &= ~CONTROL_DTR;
Alan Cox8873aaa2008-03-10 21:59:28 +0000954 priv->cmd_ctrl = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 spin_unlock_irqrestore(&priv->lock, flags);
956
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 return cypress_write(port, NULL, 0);
958}
959
960
961static int cypress_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg)
962{
963 struct cypress_private *priv = usb_get_serial_port_data(port);
964
965 dbg("%s - port %d, cmd 0x%.4x", __FUNCTION__, port->number, cmd);
966
967 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 /* This code comes from drivers/char/serial.c and ftdi_sio.c */
969 case TIOCMIWAIT:
970 while (priv != NULL) {
971 interruptible_sleep_on(&priv->delta_msr_wait);
972 /* see if a signal did it */
973 if (signal_pending(current))
974 return -ERESTARTSYS;
975 else {
976 char diff = priv->diff_status;
977
978 if (diff == 0) {
979 return -EIO; /* no change => error */
980 }
981
982 /* consume all events */
983 priv->diff_status = 0;
984
985 /* return 0 if caller wanted to know about these bits */
986 if ( ((arg & TIOCM_RNG) && (diff & UART_RI)) ||
987 ((arg & TIOCM_DSR) && (diff & UART_DSR)) ||
988 ((arg & TIOCM_CD) && (diff & UART_CD)) ||
989 ((arg & TIOCM_CTS) && (diff & UART_CTS)) ) {
990 return 0;
991 }
992 /* otherwise caller can't care less about what happened,
993 * and so we continue to wait for more events.
994 */
995 }
996 }
997 return 0;
998 break;
999 default:
1000 break;
1001 }
1002
1003 dbg("%s - arg not supported - it was 0x%04x - check include/asm/ioctls.h", __FUNCTION__, cmd);
1004
1005 return -ENOIOCTLCMD;
1006} /* cypress_ioctl */
1007
1008
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001009static void cypress_set_termios (struct usb_serial_port *port,
Alan Cox606d0992006-12-08 02:38:45 -08001010 struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011{
1012 struct cypress_private *priv = usb_get_serial_port_data(port);
1013 struct tty_struct *tty;
1014 int data_bits, stop_bits, parity_type, parity_enable;
Alan Cox8873aaa2008-03-10 21:59:28 +00001015 unsigned cflag, iflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 unsigned long flags;
1017 __u8 oldlines;
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001018 int linechange = 0;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001019
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 dbg("%s - port %d", __FUNCTION__, port->number);
1021
1022 tty = port->tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
1024 spin_lock_irqsave(&priv->lock, flags);
1025 if (!priv->termios_initialized) {
1026 if (priv->chiptype == CT_EARTHMATE) {
1027 *(tty->termios) = tty_std_termios;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001028 tty->termios->c_cflag = B4800 | CS8 | CREAD | HUPCL |
1029 CLOCAL;
Alan Cox8873aaa2008-03-10 21:59:28 +00001030 tty->termios->c_ispeed = 4800;
1031 tty->termios->c_ospeed = 4800;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 } else if (priv->chiptype == CT_CYPHIDCOM) {
1033 *(tty->termios) = tty_std_termios;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001034 tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
1035 CLOCAL;
Alan Cox8873aaa2008-03-10 21:59:28 +00001036 tty->termios->c_ispeed = 9600;
1037 tty->termios->c_ospeed = 9600;
Lonnie Mendeza5c44e22006-03-01 10:45:24 -06001038 } else if (priv->chiptype == CT_CA42V2) {
1039 *(tty->termios) = tty_std_termios;
1040 tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
1041 CLOCAL;
Alan Cox8873aaa2008-03-10 21:59:28 +00001042 tty->termios->c_ispeed = 9600;
1043 tty->termios->c_ospeed = 9600;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 }
1045 priv->termios_initialized = 1;
1046 }
1047 spin_unlock_irqrestore(&priv->lock, flags);
1048
Alan Cox8873aaa2008-03-10 21:59:28 +00001049 /* Unsupported features need clearing */
1050 tty->termios->c_cflag &= ~(CMSPAR|CRTSCTS);
1051
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 cflag = tty->termios->c_cflag;
1053 iflag = tty->termios->c_iflag;
1054
1055 /* check if there are new settings */
1056 if (old_termios) {
Alan Cox8873aaa2008-03-10 21:59:28 +00001057 spin_lock_irqsave(&priv->lock, flags);
1058 priv->tmp_termios = *(tty->termios);
1059 spin_unlock_irqrestore(&priv->lock, flags);
1060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
1062 /* set number of data bits, parity, stop bits */
1063 /* when parity is disabled the parity type bit is ignored */
1064
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001065 /* 1 means 2 stop bits, 0 means 1 stop bit */
1066 stop_bits = cflag & CSTOPB ? 1 : 0;
1067
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 if (cflag & PARENB) {
1069 parity_enable = 1;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001070 /* 1 means odd parity, 0 means even parity */
1071 parity_type = cflag & PARODD ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 } else
1073 parity_enable = parity_type = 0;
1074
1075 if (cflag & CSIZE) {
1076 switch (cflag & CSIZE) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001077 case CS5:
1078 data_bits = 0;
1079 break;
1080 case CS6:
1081 data_bits = 1;
1082 break;
1083 case CS7:
1084 data_bits = 2;
1085 break;
1086 case CS8:
1087 data_bits = 3;
1088 break;
1089 default:
1090 err("%s - CSIZE was set, but not CS5-CS8",
1091 __FUNCTION__);
1092 data_bits = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 }
1094 } else
1095 data_bits = 3;
1096
1097 spin_lock_irqsave(&priv->lock, flags);
1098 oldlines = priv->line_control;
1099 if ((cflag & CBAUD) == B0) {
1100 /* drop dtr and rts */
1101 dbg("%s - dropping the lines, baud rate 0bps", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
Alan Cox8873aaa2008-03-10 21:59:28 +00001103 } else
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001104 priv->line_control = (CONTROL_DTR | CONTROL_RTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001107 dbg("%s - sending %d stop_bits, %d parity_enable, %d parity_type, "
1108 "%d data_bits (+5)", __FUNCTION__, stop_bits,
1109 parity_enable, parity_type, data_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
Alan Cox8873aaa2008-03-10 21:59:28 +00001111 cypress_serial_control(port, tty_get_baud_rate(tty), data_bits, stop_bits,
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001112 parity_enable, parity_type, 0, CYPRESS_SET_CONFIG);
1113
1114 /* we perform a CYPRESS_GET_CONFIG so that the current settings are
1115 * filled into the private structure this should confirm that all is
1116 * working if it returns what we just set */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 cypress_serial_control(port, 0, 0, 0, 0, 0, 0, CYPRESS_GET_CONFIG);
1118
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001119 /* Here we can define custom tty settings for devices; the main tty
1120 * termios flag base comes from empeg.c */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001122 spin_lock_irqsave(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 if ( (priv->chiptype == CT_EARTHMATE) && (priv->baud_rate == 4800) ) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001124 dbg("Using custom termios settings for a baud rate of "
1125 "4800bps.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 /* define custom termios settings for NMEA protocol */
1127
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 tty->termios->c_iflag /* input modes - */
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001129 &= ~(IGNBRK /* disable ignore break */
1130 | BRKINT /* disable break causes interrupt */
1131 | PARMRK /* disable mark parity errors */
1132 | ISTRIP /* disable clear high bit of input char */
1133 | INLCR /* disable translate NL to CR */
1134 | IGNCR /* disable ignore CR */
1135 | ICRNL /* disable translate CR to NL */
1136 | IXON); /* disable enable XON/XOFF flow control */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001138 tty->termios->c_oflag /* output modes */
1139 &= ~OPOST; /* disable postprocess output char */
1140
1141 tty->termios->c_lflag /* line discipline modes */
1142 &= ~(ECHO /* disable echo input characters */
1143 | ECHONL /* disable echo new line */
1144 | ICANON /* disable erase, kill, werase, and rprnt
1145 special characters */
1146 | ISIG /* disable interrupt, quit, and suspend
1147 special characters */
1148 | IEXTEN); /* disable non-POSIX special characters */
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001149 } /* CT_CYPHIDCOM: Application should handle this for device */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 linechange = (priv->line_control != oldlines);
1152 spin_unlock_irqrestore(&priv->lock, flags);
1153
1154 /* if necessary, set lines */
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001155 if (linechange) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 priv->cmd_ctrl = 1;
1157 cypress_write(port, NULL, 0);
1158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159} /* cypress_set_termios */
1160
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001161
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162/* returns amount of data still left in soft buffer */
1163static int cypress_chars_in_buffer(struct usb_serial_port *port)
1164{
1165 struct cypress_private *priv = usb_get_serial_port_data(port);
1166 int chars = 0;
1167 unsigned long flags;
1168
1169 dbg("%s - port %d", __FUNCTION__, port->number);
1170
1171 spin_lock_irqsave(&priv->lock, flags);
1172 chars = cypress_buf_data_avail(priv->buf);
1173 spin_unlock_irqrestore(&priv->lock, flags);
1174
1175 dbg("%s - returns %d", __FUNCTION__, chars);
1176 return chars;
1177}
1178
1179
1180static void cypress_throttle (struct usb_serial_port *port)
1181{
1182 struct cypress_private *priv = usb_get_serial_port_data(port);
1183 unsigned long flags;
1184
1185 dbg("%s - port %d", __FUNCTION__, port->number);
1186
1187 spin_lock_irqsave(&priv->lock, flags);
1188 priv->rx_flags = THROTTLED;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001189 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190}
1191
1192
1193static void cypress_unthrottle (struct usb_serial_port *port)
1194{
1195 struct cypress_private *priv = usb_get_serial_port_data(port);
1196 int actually_throttled, result;
1197 unsigned long flags;
1198
1199 dbg("%s - port %d", __FUNCTION__, port->number);
1200
1201 spin_lock_irqsave(&priv->lock, flags);
1202 actually_throttled = priv->rx_flags & ACTUALLY_THROTTLED;
1203 priv->rx_flags = 0;
1204 spin_unlock_irqrestore(&priv->lock, flags);
1205
Mike Isely78aef512006-08-29 22:07:11 -05001206 if (!priv->comm_is_ok)
1207 return;
1208
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 if (actually_throttled) {
1210 port->interrupt_in_urb->dev = port->serial->dev;
1211
1212 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
Mike Isely78aef512006-08-29 22:07:11 -05001213 if (result) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001214 dev_err(&port->dev, "%s - failed submitting read urb, "
1215 "error %d\n", __FUNCTION__, result);
Mike Isely78aef512006-08-29 22:07:11 -05001216 cypress_set_dead(port);
1217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 }
1219}
1220
1221
David Howells7d12e782006-10-05 14:55:46 +01001222static void cypress_read_int_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223{
1224 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
1225 struct cypress_private *priv = usb_get_serial_port_data(port);
1226 struct tty_struct *tty;
1227 unsigned char *data = urb->transfer_buffer;
1228 unsigned long flags;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001229 char tty_flag = TTY_NORMAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 int havedata = 0;
1231 int bytes = 0;
1232 int result;
1233 int i = 0;
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001234 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
1236 dbg("%s - port %d", __FUNCTION__, port->number);
1237
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001238 switch (status) {
Mike Isely78aef512006-08-29 22:07:11 -05001239 case 0: /* success */
1240 break;
1241 case -ECONNRESET:
1242 case -ENOENT:
1243 case -ESHUTDOWN:
1244 /* precursor to disconnect so just go away */
1245 return;
1246 case -EPIPE:
1247 usb_clear_halt(port->serial->dev,0x81);
1248 break;
1249 default:
1250 /* something ugly is going on... */
1251 dev_err(&urb->dev->dev,"%s - unexpected nonzero read status received: %d\n",
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001252 __FUNCTION__, status);
Mike Isely78aef512006-08-29 22:07:11 -05001253 cypress_set_dead(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 return;
1255 }
1256
1257 spin_lock_irqsave(&priv->lock, flags);
1258 if (priv->rx_flags & THROTTLED) {
1259 dbg("%s - now throttling", __FUNCTION__);
1260 priv->rx_flags |= ACTUALLY_THROTTLED;
1261 spin_unlock_irqrestore(&priv->lock, flags);
1262 return;
1263 }
1264 spin_unlock_irqrestore(&priv->lock, flags);
1265
1266 tty = port->tty;
1267 if (!tty) {
1268 dbg("%s - bad tty pointer - exiting", __FUNCTION__);
1269 return;
1270 }
1271
1272 spin_lock_irqsave(&priv->lock, flags);
Mike Isely3416eaa2008-02-10 20:23:19 -06001273 result = urb->actual_length;
1274 switch (priv->pkt_fmt) {
1275 default:
1276 case packet_format_1:
1277 /* This is for the CY7C64013... */
1278 priv->current_status = data[0] & 0xF8;
1279 bytes = data[1] + 2;
1280 i = 2;
1281 if (bytes > 2)
1282 havedata = 1;
1283 break;
1284 case packet_format_2:
1285 /* This is for the CY7C63743... */
1286 priv->current_status = data[0] & 0xF8;
1287 bytes = (data[0] & 0x07) + 1;
1288 i = 1;
1289 if (bytes > 1)
1290 havedata = 1;
1291 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 }
1293 spin_unlock_irqrestore(&priv->lock, flags);
Mike Isely3416eaa2008-02-10 20:23:19 -06001294 if (result < bytes) {
1295 dbg("%s - wrong packet size - received %d bytes but packet "
1296 "said %d bytes", __func__, result, bytes);
1297 goto continue_read;
1298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001300 usb_serial_debug_data (debug, &port->dev, __FUNCTION__,
1301 urb->actual_length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
1303 spin_lock_irqsave(&priv->lock, flags);
1304 /* check to see if status has changed */
Mike Isely67683062008-02-10 20:23:28 -06001305 if (priv->current_status != priv->prev_status) {
1306 priv->diff_status |= priv->current_status ^
1307 priv->prev_status;
1308 wake_up_interruptible(&priv->delta_msr_wait);
1309 priv->prev_status = priv->current_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 }
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001311 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001313 /* hangup, as defined in acm.c... this might be a bad place for it
1314 * though */
1315 if (tty && !(tty->termios->c_cflag & CLOCAL) &&
1316 !(priv->current_status & UART_CD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 dbg("%s - calling hangup", __FUNCTION__);
1318 tty_hangup(tty);
1319 goto continue_read;
1320 }
1321
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001322 /* There is one error bit... I'm assuming it is a parity error
1323 * indicator as the generic firmware will set this bit to 1 if a
1324 * parity error occurs.
1325 * I can not find reference to any other error events. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 spin_lock_irqsave(&priv->lock, flags);
1327 if (priv->current_status & CYP_ERROR) {
1328 spin_unlock_irqrestore(&priv->lock, flags);
1329 tty_flag = TTY_PARITY;
1330 dbg("%s - Parity Error detected", __FUNCTION__);
1331 } else
1332 spin_unlock_irqrestore(&priv->lock, flags);
1333
1334 /* process read if there is data other than line status */
1335 if (tty && (bytes > i)) {
Alan Cox33f0f882006-01-09 20:54:13 -08001336 bytes = tty_buffer_request_room(tty, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 for (; i < bytes ; ++i) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001338 dbg("pushing byte number %d - %d - %c", i, data[i],
1339 data[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 tty_insert_flip_char(tty, data[i], tty_flag);
1341 }
1342 tty_flip_buffer_push(port->tty);
1343 }
1344
1345 spin_lock_irqsave(&priv->lock, flags);
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001346 /* control and status byte(s) are also counted */
1347 priv->bytes_in += bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 spin_unlock_irqrestore(&priv->lock, flags);
1349
1350continue_read:
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001351
1352 /* Continue trying to always read... unless the port has closed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
Mike Isely78aef512006-08-29 22:07:11 -05001354 if (port->open_count > 0 && priv->comm_is_ok) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001355 usb_fill_int_urb(port->interrupt_in_urb, port->serial->dev,
1356 usb_rcvintpipe(port->serial->dev,
1357 port->interrupt_in_endpointAddress),
1358 port->interrupt_in_urb->transfer_buffer,
1359 port->interrupt_in_urb->transfer_buffer_length,
Mike Isely0257fa92006-08-29 22:06:59 -05001360 cypress_read_int_callback, port, priv->read_urb_interval);
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001361 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
Mike Isely78aef512006-08-29 22:07:11 -05001362 if (result) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001363 dev_err(&urb->dev->dev, "%s - failed resubmitting "
1364 "read urb, error %d\n", __FUNCTION__,
1365 result);
Mike Isely78aef512006-08-29 22:07:11 -05001366 cypress_set_dead(port);
1367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 }
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001369
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 return;
1371} /* cypress_read_int_callback */
1372
1373
David Howells7d12e782006-10-05 14:55:46 +01001374static void cypress_write_int_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375{
1376 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
1377 struct cypress_private *priv = usb_get_serial_port_data(port);
1378 int result;
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001379 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
1381 dbg("%s - port %d", __FUNCTION__, port->number);
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001382
1383 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 case 0:
1385 /* success */
1386 break;
1387 case -ECONNRESET:
1388 case -ENOENT:
1389 case -ESHUTDOWN:
1390 /* this urb is terminated, clean up */
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001391 dbg("%s - urb shutting down with status: %d",
1392 __FUNCTION__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 priv->write_urb_in_use = 0;
1394 return;
Mike Isely78aef512006-08-29 22:07:11 -05001395 case -EPIPE: /* no break needed; clear halt and resubmit */
1396 if (!priv->comm_is_ok)
1397 break;
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001398 usb_clear_halt(port->serial->dev, 0x02);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 /* error in the urb, so we have to resubmit it */
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001400 dbg("%s - nonzero write bulk status received: %d",
1401 __FUNCTION__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 port->interrupt_out_urb->transfer_buffer_length = 1;
1403 port->interrupt_out_urb->dev = port->serial->dev;
1404 result = usb_submit_urb(port->interrupt_out_urb, GFP_ATOMIC);
Mike Isely78aef512006-08-29 22:07:11 -05001405 if (!result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 return;
Mike Isely78aef512006-08-29 22:07:11 -05001407 dev_err(&urb->dev->dev, "%s - failed resubmitting write urb, error %d\n",
1408 __FUNCTION__, result);
1409 cypress_set_dead(port);
1410 break;
1411 default:
1412 dev_err(&urb->dev->dev,"%s - unexpected nonzero write status received: %d\n",
Greg Kroah-Hartman8d7bc552007-06-15 15:44:13 -07001413 __FUNCTION__, status);
Mike Isely78aef512006-08-29 22:07:11 -05001414 cypress_set_dead(port);
1415 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 }
1417
1418 priv->write_urb_in_use = 0;
1419
1420 /* send any buffered data */
1421 cypress_send(port);
1422}
1423
1424
1425/*****************************************************************************
1426 * Write buffer functions - buffering code from pl2303 used
1427 *****************************************************************************/
1428
1429/*
1430 * cypress_buf_alloc
1431 *
1432 * Allocate a circular buffer and all associated memory.
1433 */
1434
1435static struct cypress_buf *cypress_buf_alloc(unsigned int size)
1436{
1437
1438 struct cypress_buf *cb;
1439
1440
1441 if (size == 0)
1442 return NULL;
1443
Robert P. J. Day5cbded52006-12-13 00:35:56 -08001444 cb = kmalloc(sizeof(struct cypress_buf), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 if (cb == NULL)
1446 return NULL;
1447
1448 cb->buf_buf = kmalloc(size, GFP_KERNEL);
1449 if (cb->buf_buf == NULL) {
1450 kfree(cb);
1451 return NULL;
1452 }
1453
1454 cb->buf_size = size;
1455 cb->buf_get = cb->buf_put = cb->buf_buf;
1456
1457 return cb;
1458
1459}
1460
1461
1462/*
1463 * cypress_buf_free
1464 *
1465 * Free the buffer and all associated memory.
1466 */
1467
1468static void cypress_buf_free(struct cypress_buf *cb)
1469{
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001470 if (cb) {
1471 kfree(cb->buf_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 kfree(cb);
1473 }
1474}
1475
1476
1477/*
1478 * cypress_buf_clear
1479 *
1480 * Clear out all data in the circular buffer.
1481 */
1482
1483static void cypress_buf_clear(struct cypress_buf *cb)
1484{
1485 if (cb != NULL)
1486 cb->buf_get = cb->buf_put;
1487 /* equivalent to a get of all data available */
1488}
1489
1490
1491/*
1492 * cypress_buf_data_avail
1493 *
1494 * Return the number of bytes of data available in the circular
1495 * buffer.
1496 */
1497
1498static unsigned int cypress_buf_data_avail(struct cypress_buf *cb)
1499{
1500 if (cb != NULL)
1501 return ((cb->buf_size + cb->buf_put - cb->buf_get) % cb->buf_size);
1502 else
1503 return 0;
1504}
1505
1506
1507/*
1508 * cypress_buf_space_avail
1509 *
1510 * Return the number of bytes of space available in the circular
1511 * buffer.
1512 */
1513
1514static unsigned int cypress_buf_space_avail(struct cypress_buf *cb)
1515{
1516 if (cb != NULL)
1517 return ((cb->buf_size + cb->buf_get - cb->buf_put - 1) % cb->buf_size);
1518 else
1519 return 0;
1520}
1521
1522
1523/*
1524 * cypress_buf_put
1525 *
1526 * Copy data data from a user buffer and put it into the circular buffer.
1527 * Restrict to the amount of space available.
1528 *
1529 * Return the number of bytes copied.
1530 */
1531
1532static unsigned int cypress_buf_put(struct cypress_buf *cb, const char *buf,
1533 unsigned int count)
1534{
1535
1536 unsigned int len;
1537
1538
1539 if (cb == NULL)
1540 return 0;
1541
1542 len = cypress_buf_space_avail(cb);
1543 if (count > len)
1544 count = len;
1545
1546 if (count == 0)
1547 return 0;
1548
1549 len = cb->buf_buf + cb->buf_size - cb->buf_put;
1550 if (count > len) {
1551 memcpy(cb->buf_put, buf, len);
1552 memcpy(cb->buf_buf, buf+len, count - len);
1553 cb->buf_put = cb->buf_buf + count - len;
1554 } else {
1555 memcpy(cb->buf_put, buf, count);
1556 if (count < len)
1557 cb->buf_put += count;
1558 else /* count == len */
1559 cb->buf_put = cb->buf_buf;
1560 }
1561
1562 return count;
1563
1564}
1565
1566
1567/*
1568 * cypress_buf_get
1569 *
1570 * Get data from the circular buffer and copy to the given buffer.
1571 * Restrict to the amount of data available.
1572 *
1573 * Return the number of bytes copied.
1574 */
1575
1576static unsigned int cypress_buf_get(struct cypress_buf *cb, char *buf,
1577 unsigned int count)
1578{
1579
1580 unsigned int len;
1581
1582
1583 if (cb == NULL)
1584 return 0;
1585
1586 len = cypress_buf_data_avail(cb);
1587 if (count > len)
1588 count = len;
1589
1590 if (count == 0)
1591 return 0;
1592
1593 len = cb->buf_buf + cb->buf_size - cb->buf_get;
1594 if (count > len) {
1595 memcpy(buf, cb->buf_get, len);
1596 memcpy(buf+len, cb->buf_buf, count - len);
1597 cb->buf_get = cb->buf_buf + count - len;
1598 } else {
1599 memcpy(buf, cb->buf_get, count);
1600 if (count < len)
1601 cb->buf_get += count;
1602 else /* count == len */
1603 cb->buf_get = cb->buf_buf;
1604 }
1605
1606 return count;
1607
1608}
1609
1610/*****************************************************************************
1611 * Module functions
1612 *****************************************************************************/
1613
1614static int __init cypress_init(void)
1615{
1616 int retval;
1617
1618 dbg("%s", __FUNCTION__);
1619
1620 retval = usb_serial_register(&cypress_earthmate_device);
1621 if (retval)
1622 goto failed_em_register;
1623 retval = usb_serial_register(&cypress_hidcom_device);
1624 if (retval)
1625 goto failed_hidcom_register;
Lonnie Mendeza5c44e22006-03-01 10:45:24 -06001626 retval = usb_serial_register(&cypress_ca42v2_device);
1627 if (retval)
1628 goto failed_ca42v2_register;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 retval = usb_register(&cypress_driver);
1630 if (retval)
1631 goto failed_usb_register;
1632
1633 info(DRIVER_DESC " " DRIVER_VERSION);
1634 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
Mariusz Kozlowski2e46b742006-11-17 17:49:22 +01001636failed_usb_register:
1637 usb_serial_deregister(&cypress_ca42v2_device);
1638failed_ca42v2_register:
1639 usb_serial_deregister(&cypress_hidcom_device);
1640failed_hidcom_register:
1641 usb_serial_deregister(&cypress_earthmate_device);
1642failed_em_register:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 return retval;
1644}
1645
1646
1647static void __exit cypress_exit (void)
1648{
1649 dbg("%s", __FUNCTION__);
1650
1651 usb_deregister (&cypress_driver);
1652 usb_serial_deregister (&cypress_earthmate_device);
1653 usb_serial_deregister (&cypress_hidcom_device);
Lonnie Mendeza5c44e22006-03-01 10:45:24 -06001654 usb_serial_deregister (&cypress_ca42v2_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655}
1656
1657
1658module_init(cypress_init);
1659module_exit(cypress_exit);
1660
1661MODULE_AUTHOR( DRIVER_AUTHOR );
1662MODULE_DESCRIPTION( DRIVER_DESC );
1663MODULE_VERSION( DRIVER_VERSION );
1664MODULE_LICENSE("GPL");
1665
1666module_param(debug, bool, S_IRUGO | S_IWUSR);
1667MODULE_PARM_DESC(debug, "Debug enabled or not");
1668module_param(stats, bool, S_IRUGO | S_IWUSR);
1669MODULE_PARM_DESC(stats, "Enable statistics or not");
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001670module_param(interval, int, S_IRUGO | S_IWUSR);
1671MODULE_PARM_DESC(interval, "Overrides interrupt interval");