]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/usb/serial/cypress_m8.c
IRQ: Maintain regs pointer globally rather than passing to IRQ handlers
[mirror_ubuntu-artful-kernel.git] / drivers / usb / serial / cypress_m8.c
CommitLineData
1da177e4
LT
1/*
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 *
3cb4a4f7
LM
19 * 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.
1da177e4
LT
27 *
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 *
1da177e4
LT
43 */
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
1da177e4
LT
51#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>
a969888c 62#include <linux/usb/serial.h>
1da177e4
LT
63#include <linux/serial.h>
64#include <linux/delay.h>
65#include <asm/uaccess.h>
66
1da177e4
LT
67#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;
3cb4a4f7 76static int interval;
1da177e4
LT
77
78/*
79 * Version Information
80 */
3cb4a4f7 81#define DRIVER_VERSION "v1.09"
1da177e4
LT
82#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) },
25b6f08e 91 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB_LT20) },
1da177e4
LT
92 { } /* Terminating entry */
93};
94
95static struct usb_device_id id_table_cyphidcomrs232 [] = {
96 { USB_DEVICE(VENDOR_ID_CYPRESS, PRODUCT_ID_CYPHIDCOM) },
97 { } /* Terminating entry */
98};
99
a5c44e29
LM
100static struct usb_device_id id_table_nokiaca42v2 [] = {
101 { USB_DEVICE(VENDOR_ID_DAZZLE, PRODUCT_ID_CA42) },
102 { } /* Terminating entry */
103};
104
1da177e4
LT
105static struct usb_device_id id_table_combined [] = {
106 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB) },
25b6f08e 107 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB_LT20) },
1da177e4 108 { USB_DEVICE(VENDOR_ID_CYPRESS, PRODUCT_ID_CYPHIDCOM) },
a5c44e29 109 { USB_DEVICE(VENDOR_ID_DAZZLE, PRODUCT_ID_CA42) },
1da177e4
LT
110 { } /* Terminating entry */
111};
112
113MODULE_DEVICE_TABLE (usb, id_table_combined);
114
115static struct usb_driver cypress_driver = {
116 .name = "cypress",
117 .probe = usb_serial_probe,
118 .disconnect = usb_serial_disconnect,
119 .id_table = id_table_combined,
ba9dc657 120 .no_dynamic_id = 1,
1da177e4
LT
121};
122
123struct cypress_private {
124 spinlock_t lock; /* private lock */
125 int chiptype; /* identifier of device, for quirks/etc */
126 int bytes_in; /* used for statistics */
127 int bytes_out; /* used for statistics */
128 int cmd_count; /* used for statistics */
129 int cmd_ctrl; /* always set this to 1 before issuing a command */
130 struct cypress_buf *buf; /* write buffer */
131 int write_urb_in_use; /* write urb in use indicator */
0257fa9f
MI
132 int write_urb_interval; /* interval to use for write urb */
133 int read_urb_interval; /* interval to use for read urb */
78aef519 134 int comm_is_ok; /* true if communication is (still) ok */
1da177e4
LT
135 int termios_initialized;
136 __u8 line_control; /* holds dtr / rts value */
137 __u8 current_status; /* received from last read - info on dsr,cts,cd,ri,etc */
138 __u8 current_config; /* stores the current configuration byte */
139 __u8 rx_flags; /* throttling - used from whiteheat/ftdi_sio */
140 int baud_rate; /* stores current baud rate in integer form */
141 int cbr_mask; /* stores current baud rate in masked form */
142 int isthrottled; /* if throttled, discard reads */
143 wait_queue_head_t delta_msr_wait; /* used for TIOCMIWAIT */
144 char prev_status, diff_status; /* used for TIOCMIWAIT */
145 /* we pass a pointer to this as the arguement sent to cypress_set_termios old_termios */
146 struct termios tmp_termios; /* stores the old termios settings */
1da177e4
LT
147};
148
149/* write buffer structure */
150struct cypress_buf {
151 unsigned int buf_size;
152 char *buf_buf;
153 char *buf_get;
154 char *buf_put;
155};
156
157/* function prototypes for the Cypress USB to serial device */
158static int cypress_earthmate_startup (struct usb_serial *serial);
159static int cypress_hidcom_startup (struct usb_serial *serial);
a5c44e29 160static int cypress_ca42v2_startup (struct usb_serial *serial);
1da177e4
LT
161static void cypress_shutdown (struct usb_serial *serial);
162static int cypress_open (struct usb_serial_port *port, struct file *filp);
163static void cypress_close (struct usb_serial_port *port, struct file *filp);
164static int cypress_write (struct usb_serial_port *port, const unsigned char *buf, int count);
165static void cypress_send (struct usb_serial_port *port);
166static int cypress_write_room (struct usb_serial_port *port);
167static int cypress_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg);
168static void cypress_set_termios (struct usb_serial_port *port, struct termios * old);
169static int cypress_tiocmget (struct usb_serial_port *port, struct file *file);
170static int cypress_tiocmset (struct usb_serial_port *port, struct file *file, unsigned int set, unsigned int clear);
171static int cypress_chars_in_buffer (struct usb_serial_port *port);
172static void cypress_throttle (struct usb_serial_port *port);
173static void cypress_unthrottle (struct usb_serial_port *port);
78aef519 174static void cypress_set_dead (struct usb_serial_port *port);
7d12e780
DH
175static void cypress_read_int_callback (struct urb *urb);
176static void cypress_write_int_callback (struct urb *urb);
1da177e4
LT
177/* baud helper functions */
178static int mask_to_rate (unsigned mask);
179static unsigned rate_to_mask (int rate);
180/* write buffer functions */
181static struct cypress_buf *cypress_buf_alloc(unsigned int size);
182static void cypress_buf_free(struct cypress_buf *cb);
183static void cypress_buf_clear(struct cypress_buf *cb);
184static unsigned int cypress_buf_data_avail(struct cypress_buf *cb);
185static unsigned int cypress_buf_space_avail(struct cypress_buf *cb);
3cb4a4f7
LM
186static unsigned int cypress_buf_put(struct cypress_buf *cb, const char *buf, unsigned int count);
187static unsigned int cypress_buf_get(struct cypress_buf *cb, char *buf, unsigned int count);
1da177e4
LT
188
189
ea65370d 190static struct usb_serial_driver cypress_earthmate_device = {
18fcac35
GKH
191 .driver = {
192 .owner = THIS_MODULE,
269bda1c 193 .name = "earthmate",
18fcac35 194 },
269bda1c 195 .description = "DeLorme Earthmate USB",
1da177e4
LT
196 .id_table = id_table_earthmate,
197 .num_interrupt_in = 1,
198 .num_interrupt_out = 1,
199 .num_bulk_in = NUM_DONT_CARE,
200 .num_bulk_out = NUM_DONT_CARE,
201 .num_ports = 1,
202 .attach = cypress_earthmate_startup,
203 .shutdown = cypress_shutdown,
204 .open = cypress_open,
205 .close = cypress_close,
206 .write = cypress_write,
207 .write_room = cypress_write_room,
208 .ioctl = cypress_ioctl,
209 .set_termios = cypress_set_termios,
210 .tiocmget = cypress_tiocmget,
211 .tiocmset = cypress_tiocmset,
212 .chars_in_buffer = cypress_chars_in_buffer,
213 .throttle = cypress_throttle,
214 .unthrottle = cypress_unthrottle,
215 .read_int_callback = cypress_read_int_callback,
216 .write_int_callback = cypress_write_int_callback,
217};
218
ea65370d 219static struct usb_serial_driver cypress_hidcom_device = {
18fcac35
GKH
220 .driver = {
221 .owner = THIS_MODULE,
269bda1c 222 .name = "cyphidcom",
18fcac35 223 },
269bda1c 224 .description = "HID->COM RS232 Adapter",
1da177e4
LT
225 .id_table = id_table_cyphidcomrs232,
226 .num_interrupt_in = 1,
227 .num_interrupt_out = 1,
228 .num_bulk_in = NUM_DONT_CARE,
229 .num_bulk_out = NUM_DONT_CARE,
230 .num_ports = 1,
231 .attach = cypress_hidcom_startup,
232 .shutdown = cypress_shutdown,
233 .open = cypress_open,
234 .close = cypress_close,
235 .write = cypress_write,
236 .write_room = cypress_write_room,
237 .ioctl = cypress_ioctl,
238 .set_termios = cypress_set_termios,
239 .tiocmget = cypress_tiocmget,
240 .tiocmset = cypress_tiocmset,
241 .chars_in_buffer = cypress_chars_in_buffer,
242 .throttle = cypress_throttle,
243 .unthrottle = cypress_unthrottle,
244 .read_int_callback = cypress_read_int_callback,
245 .write_int_callback = cypress_write_int_callback,
246};
247
a5c44e29
LM
248static struct usb_serial_driver cypress_ca42v2_device = {
249 .driver = {
250 .owner = THIS_MODULE,
251 .name = "nokiaca42v2",
252 },
253 .description = "Nokia CA-42 V2 Adapter",
254 .id_table = id_table_nokiaca42v2,
255 .num_interrupt_in = 1,
256 .num_interrupt_out = 1,
257 .num_bulk_in = NUM_DONT_CARE,
258 .num_bulk_out = NUM_DONT_CARE,
259 .num_ports = 1,
260 .attach = cypress_ca42v2_startup,
261 .shutdown = cypress_shutdown,
262 .open = cypress_open,
263 .close = cypress_close,
264 .write = cypress_write,
265 .write_room = cypress_write_room,
266 .ioctl = cypress_ioctl,
267 .set_termios = cypress_set_termios,
268 .tiocmget = cypress_tiocmget,
269 .tiocmset = cypress_tiocmset,
270 .chars_in_buffer = cypress_chars_in_buffer,
271 .throttle = cypress_throttle,
272 .unthrottle = cypress_unthrottle,
273 .read_int_callback = cypress_read_int_callback,
274 .write_int_callback = cypress_write_int_callback,
275};
1da177e4
LT
276
277/*****************************************************************************
278 * Cypress serial helper functions
279 *****************************************************************************/
280
281
093cf723 282/* This function can either set or retrieve the current serial line settings */
1da177e4
LT
283static int cypress_serial_control (struct usb_serial_port *port, unsigned baud_mask, int data_bits, int stop_bits,
284 int parity_enable, int parity_type, int reset, int cypress_request_type)
285{
3cb4a4f7 286 int new_baudrate = 0, retval = 0, tries = 0;
1da177e4 287 struct cypress_private *priv;
3cb4a4f7 288 __u8 feature_buffer[8];
1da177e4
LT
289 unsigned long flags;
290
291 dbg("%s", __FUNCTION__);
292
293 priv = usb_get_serial_port_data(port);
294
78aef519
MI
295 if (!priv->comm_is_ok)
296 return -ENODEV;
297
1da177e4
LT
298 switch(cypress_request_type) {
299 case CYPRESS_SET_CONFIG:
300
301 /*
302 * The general purpose firmware for the Cypress M8 allows for a maximum speed
303 * of 57600bps (I have no idea whether DeLorme chose to use the general purpose
304 * firmware or not), if you need to modify this speed setting for your own
305 * project please add your own chiptype and modify the code likewise. The
3cb4a4f7
LM
306 * Cypress HID->COM device will work successfully up to 115200bps (but the
307 * actual throughput is around 3kBps).
1da177e4
LT
308 */
309 if (baud_mask != priv->cbr_mask) {
310 dbg("%s - baud rate is changing", __FUNCTION__);
311 if ( priv->chiptype == CT_EARTHMATE ) {
312 /* 300 and 600 baud rates are supported under the generic firmware,
313 * but are not used with NMEA and SiRF protocols */
314
315 if ( (baud_mask == B300) || (baud_mask == B600) ) {
3cb4a4f7 316 err("%s - failed setting baud rate, unsupported speed",
1da177e4 317 __FUNCTION__);
3cb4a4f7
LM
318 new_baudrate = priv->baud_rate;
319 } else if ( (new_baudrate = mask_to_rate(baud_mask)) == -1) {
320 err("%s - failed setting baud rate, unsupported speed",
1da177e4 321 __FUNCTION__);
3cb4a4f7 322 new_baudrate = priv->baud_rate;
1da177e4
LT
323 }
324 } else if (priv->chiptype == CT_CYPHIDCOM) {
3cb4a4f7
LM
325 if ( (new_baudrate = mask_to_rate(baud_mask)) == -1) {
326 err("%s - failed setting baud rate, unsupported speed",
1da177e4 327 __FUNCTION__);
3cb4a4f7 328 new_baudrate = priv->baud_rate;
1da177e4 329 }
a5c44e29
LM
330 } else if (priv->chiptype == CT_CA42V2) {
331 if ( (new_baudrate = mask_to_rate(baud_mask)) == -1) {
332 err("%s - failed setting baud rate, unsupported speed",
333 __FUNCTION__);
334 new_baudrate = priv->baud_rate;
335 }
1da177e4 336 } else if (priv->chiptype == CT_GENERIC) {
3cb4a4f7
LM
337 if ( (new_baudrate = mask_to_rate(baud_mask)) == -1) {
338 err("%s - failed setting baud rate, unsupported speed",
1da177e4 339 __FUNCTION__);
3cb4a4f7 340 new_baudrate = priv->baud_rate;
1da177e4
LT
341 }
342 } else {
3cb4a4f7
LM
343 info("%s - please define your chiptype", __FUNCTION__);
344 new_baudrate = priv->baud_rate;
1da177e4
LT
345 }
346 } else { /* baud rate not changing, keep the old */
3cb4a4f7 347 new_baudrate = priv->baud_rate;
1da177e4 348 }
3cb4a4f7 349 dbg("%s - baud rate is being sent as %d", __FUNCTION__, new_baudrate);
1da177e4 350
3cb4a4f7
LM
351 memset(feature_buffer, 0, 8);
352 /* fill the feature_buffer with new configuration */
353 *((u_int32_t *)feature_buffer) = new_baudrate;
1da177e4 354
3cb4a4f7 355 feature_buffer[4] |= data_bits; /* assign data bits in 2 bit space ( max 3 ) */
1da177e4 356 /* 1 bit gap */
3cb4a4f7
LM
357 feature_buffer[4] |= (stop_bits << 3); /* assign stop bits in 1 bit space */
358 feature_buffer[4] |= (parity_enable << 4); /* assign parity flag in 1 bit space */
359 feature_buffer[4] |= (parity_type << 5); /* assign parity type in 1 bit space */
1da177e4 360 /* 1 bit gap */
3cb4a4f7 361 feature_buffer[4] |= (reset << 7); /* assign reset at end of byte, 1 bit space */
1da177e4
LT
362
363 dbg("%s - device is being sent this feature report:", __FUNCTION__);
364 dbg("%s - %02X - %02X - %02X - %02X - %02X", __FUNCTION__, feature_buffer[0], feature_buffer[1],
365 feature_buffer[2], feature_buffer[3], feature_buffer[4]);
366
3cb4a4f7 367 do {
1da177e4
LT
368 retval = usb_control_msg (port->serial->dev, usb_sndctrlpipe(port->serial->dev, 0),
369 HID_REQ_SET_REPORT, USB_DIR_OUT | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
3cb4a4f7
LM
370 0x0300, 0, feature_buffer, 8, 500);
371
372 if (tries++ >= 3)
373 break;
1da177e4 374
48298e50 375 } while (retval != 8 && retval != -ENODEV);
3cb4a4f7 376
78aef519 377 if (retval != 8) {
1da177e4 378 err("%s - failed sending serial line settings - %d", __FUNCTION__, retval);
78aef519
MI
379 cypress_set_dead(port);
380 } else {
1da177e4 381 spin_lock_irqsave(&priv->lock, flags);
3cb4a4f7 382 priv->baud_rate = new_baudrate;
1da177e4 383 priv->cbr_mask = baud_mask;
3cb4a4f7 384 priv->current_config = feature_buffer[4];
1da177e4
LT
385 spin_unlock_irqrestore(&priv->lock, flags);
386 }
387 break;
388 case CYPRESS_GET_CONFIG:
389 dbg("%s - retreiving serial line settings", __FUNCTION__);
3cb4a4f7
LM
390 /* set initial values in feature buffer */
391 memset(feature_buffer, 0, 8);
1da177e4 392
3cb4a4f7 393 do {
1da177e4
LT
394 retval = usb_control_msg (port->serial->dev, usb_rcvctrlpipe(port->serial->dev, 0),
395 HID_REQ_GET_REPORT, USB_DIR_IN | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
3cb4a4f7
LM
396 0x0300, 0, feature_buffer, 8, 500);
397
398 if (tries++ >= 3)
399 break;
400
48298e50 401 } while (retval != 5 && retval != -ENODEV);
3cb4a4f7 402
1da177e4 403 if (retval != 5) {
943ffb58 404 err("%s - failed to retrieve serial line settings - %d", __FUNCTION__, retval);
78aef519 405 cypress_set_dead(port);
1da177e4
LT
406 return retval;
407 } else {
408 spin_lock_irqsave(&priv->lock, flags);
3cb4a4f7 409
1da177e4
LT
410 /* store the config in one byte, and later use bit masks to check values */
411 priv->current_config = feature_buffer[4];
3cb4a4f7 412 priv->baud_rate = *((u_int32_t *)feature_buffer);
1da177e4 413
3cb4a4f7 414 if ( (priv->cbr_mask = rate_to_mask(priv->baud_rate)) == 0x40)
1da177e4 415 dbg("%s - failed setting the baud mask (not defined)", __FUNCTION__);
1da177e4
LT
416 spin_unlock_irqrestore(&priv->lock, flags);
417 }
1da177e4 418 }
3cb4a4f7
LM
419 spin_lock_irqsave(&priv->lock, flags);
420 ++priv->cmd_count;
421 spin_unlock_irqrestore(&priv->lock, flags);
422
1da177e4
LT
423 return retval;
424} /* cypress_serial_control */
425
426
78aef519
MI
427static void cypress_set_dead(struct usb_serial_port *port)
428{
429 struct cypress_private *priv = usb_get_serial_port_data(port);
430 unsigned long flags;
431
432 spin_lock_irqsave(&priv->lock, flags);
433 if (!priv->comm_is_ok) {
434 spin_unlock_irqrestore(&priv->lock, flags);
435 return;
436 }
437 priv->comm_is_ok = 0;
438 spin_unlock_irqrestore(&priv->lock, flags);
439
440 err("cypress_m8 suspending failing port %d - interval might be too short",
441 port->number);
442}
443
444
3cb4a4f7 445/* given a baud mask, it will return integer baud on success */
1da177e4
LT
446static int mask_to_rate (unsigned mask)
447{
448 int rate;
449
450 switch (mask) {
451 case B0: rate = 0; break;
452 case B300: rate = 300; break;
453 case B600: rate = 600; break;
454 case B1200: rate = 1200; break;
455 case B2400: rate = 2400; break;
456 case B4800: rate = 4800; break;
457 case B9600: rate = 9600; break;
458 case B19200: rate = 19200; break;
459 case B38400: rate = 38400; break;
460 case B57600: rate = 57600; break;
461 case B115200: rate = 115200; break;
462 default: rate = -1;
463 }
464
465 return rate;
466}
467
468
469static unsigned rate_to_mask (int rate)
470{
471 unsigned mask;
472
473 switch (rate) {
474 case 0: mask = B0; break;
475 case 300: mask = B300; break;
476 case 600: mask = B600; break;
477 case 1200: mask = B1200; break;
478 case 2400: mask = B2400; break;
479 case 4800: mask = B4800; break;
480 case 9600: mask = B9600; break;
481 case 19200: mask = B19200; break;
482 case 38400: mask = B38400; break;
483 case 57600: mask = B57600; break;
484 case 115200: mask = B115200; break;
485 default: mask = 0x40;
486 }
487
488 return mask;
489}
490/*****************************************************************************
491 * Cypress serial driver functions
492 *****************************************************************************/
493
494
495static int generic_startup (struct usb_serial *serial)
496{
497 struct cypress_private *priv;
0257fa9f 498 struct usb_serial_port *port = serial->port[0];
1da177e4 499
0257fa9f 500 dbg("%s - port %d", __FUNCTION__, port->number);
1da177e4 501
80b6ca48 502 priv = kzalloc(sizeof (struct cypress_private), GFP_KERNEL);
1da177e4
LT
503 if (!priv)
504 return -ENOMEM;
505
78aef519 506 priv->comm_is_ok = !0;
1da177e4
LT
507 spin_lock_init(&priv->lock);
508 priv->buf = cypress_buf_alloc(CYPRESS_BUF_SIZE);
509 if (priv->buf == NULL) {
510 kfree(priv);
511 return -ENOMEM;
512 }
513 init_waitqueue_head(&priv->delta_msr_wait);
514
515 usb_reset_configuration (serial->dev);
516
517 priv->cmd_ctrl = 0;
518 priv->line_control = 0;
519 priv->termios_initialized = 0;
1da177e4 520 priv->rx_flags = 0;
3cb4a4f7 521 priv->cbr_mask = B300;
0257fa9f
MI
522 if (interval > 0) {
523 priv->write_urb_interval = interval;
524 priv->read_urb_interval = interval;
525 dbg("%s - port %d read & write intervals forced to %d",
526 __FUNCTION__,port->number,interval);
527 } else {
528 priv->write_urb_interval = port->interrupt_out_urb->interval;
529 priv->read_urb_interval = port->interrupt_in_urb->interval;
530 dbg("%s - port %d intervals: read=%d write=%d",
531 __FUNCTION__,port->number,
532 priv->read_urb_interval,priv->write_urb_interval);
533 }
534 usb_set_serial_port_data(port, priv);
1da177e4 535
b9db07fb
LM
536 return 0;
537}
1da177e4
LT
538
539
540static int cypress_earthmate_startup (struct usb_serial *serial)
541{
542 struct cypress_private *priv;
543
544 dbg("%s", __FUNCTION__);
545
546 if (generic_startup(serial)) {
b9db07fb
LM
547 dbg("%s - Failed setting up port %d", __FUNCTION__,
548 serial->port[0]->number);
1da177e4
LT
549 return 1;
550 }
551
552 priv = usb_get_serial_port_data(serial->port[0]);
553 priv->chiptype = CT_EARTHMATE;
b9db07fb
LM
554
555 return 0;
1da177e4
LT
556} /* cypress_earthmate_startup */
557
558
559static int cypress_hidcom_startup (struct usb_serial *serial)
560{
561 struct cypress_private *priv;
562
563 dbg("%s", __FUNCTION__);
564
565 if (generic_startup(serial)) {
b9db07fb
LM
566 dbg("%s - Failed setting up port %d", __FUNCTION__,
567 serial->port[0]->number);
1da177e4
LT
568 return 1;
569 }
570
571 priv = usb_get_serial_port_data(serial->port[0]);
572 priv->chiptype = CT_CYPHIDCOM;
573
b9db07fb 574 return 0;
1da177e4
LT
575} /* cypress_hidcom_startup */
576
577
a5c44e29
LM
578static int cypress_ca42v2_startup (struct usb_serial *serial)
579{
580 struct cypress_private *priv;
581
582 dbg("%s", __FUNCTION__);
583
584 if (generic_startup(serial)) {
585 dbg("%s - Failed setting up port %d", __FUNCTION__,
586 serial->port[0]->number);
587 return 1;
588 }
589
590 priv = usb_get_serial_port_data(serial->port[0]);
591 priv->chiptype = CT_CA42V2;
592
593 return 0;
594} /* cypress_ca42v2_startup */
595
596
1da177e4
LT
597static void cypress_shutdown (struct usb_serial *serial)
598{
599 struct cypress_private *priv;
600
601 dbg ("%s - port %d", __FUNCTION__, serial->port[0]->number);
602
603 /* all open ports are closed at this point */
604
605 priv = usb_get_serial_port_data(serial->port[0]);
606
607 if (priv) {
608 cypress_buf_free(priv->buf);
609 kfree(priv);
610 usb_set_serial_port_data(serial->port[0], NULL);
611 }
612}
613
614
615static int cypress_open (struct usb_serial_port *port, struct file *filp)
616{
617 struct cypress_private *priv = usb_get_serial_port_data(port);
618 struct usb_serial *serial = port->serial;
619 unsigned long flags;
620 int result = 0;
621
622 dbg("%s - port %d", __FUNCTION__, port->number);
623
78aef519
MI
624 if (!priv->comm_is_ok)
625 return -EIO;
626
1da177e4 627 /* clear halts before open */
1da177e4
LT
628 usb_clear_halt(serial->dev, 0x81);
629 usb_clear_halt(serial->dev, 0x02);
630
631 spin_lock_irqsave(&priv->lock, flags);
632 /* reset read/write statistics */
633 priv->bytes_in = 0;
634 priv->bytes_out = 0;
635 priv->cmd_count = 0;
636 priv->rx_flags = 0;
637 spin_unlock_irqrestore(&priv->lock, flags);
638
639 /* setting to zero could cause data loss */
640 port->tty->low_latency = 1;
641
642 /* raise both lines and set termios */
643 spin_lock_irqsave(&priv->lock, flags);
644 priv->line_control = CONTROL_DTR | CONTROL_RTS;
1da177e4
LT
645 priv->cmd_ctrl = 1;
646 spin_unlock_irqrestore(&priv->lock, flags);
647 result = cypress_write(port, NULL, 0);
648
649 if (result) {
650 dev_err(&port->dev, "%s - failed setting the control lines - error %d\n", __FUNCTION__, result);
651 return result;
652 } else
653 dbg("%s - success setting the control lines", __FUNCTION__);
654
655 cypress_set_termios(port, &priv->tmp_termios);
656
657 /* setup the port and start reading from the device */
658 if(!port->interrupt_in_urb){
659 err("%s - interrupt_in_urb is empty!", __FUNCTION__);
660 return(-1);
661 }
662
663 usb_fill_int_urb(port->interrupt_in_urb, serial->dev,
664 usb_rcvintpipe(serial->dev, port->interrupt_in_endpointAddress),
665 port->interrupt_in_urb->transfer_buffer, port->interrupt_in_urb->transfer_buffer_length,
0257fa9f 666 cypress_read_int_callback, port, priv->read_urb_interval);
1da177e4
LT
667 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
668
669 if (result){
670 dev_err(&port->dev, "%s - failed submitting read urb, error %d\n", __FUNCTION__, result);
78aef519 671 cypress_set_dead(port);
1da177e4
LT
672 }
673
674 return result;
675} /* cypress_open */
676
677
678static void cypress_close(struct usb_serial_port *port, struct file * filp)
679{
680 struct cypress_private *priv = usb_get_serial_port_data(port);
681 unsigned int c_cflag;
682 unsigned long flags;
683 int bps;
684 long timeout;
685 wait_queue_t wait;
686
687 dbg("%s - port %d", __FUNCTION__, port->number);
688
689 /* wait for data to drain from buffer */
690 spin_lock_irqsave(&priv->lock, flags);
691 timeout = CYPRESS_CLOSING_WAIT;
692 init_waitqueue_entry(&wait, current);
693 add_wait_queue(&port->tty->write_wait, &wait);
694 for (;;) {
695 set_current_state(TASK_INTERRUPTIBLE);
696 if (cypress_buf_data_avail(priv->buf) == 0
697 || timeout == 0 || signal_pending(current)
698 || !usb_get_intfdata(port->serial->interface))
699 break;
700 spin_unlock_irqrestore(&priv->lock, flags);
701 timeout = schedule_timeout(timeout);
702 spin_lock_irqsave(&priv->lock, flags);
703 }
704 set_current_state(TASK_RUNNING);
705 remove_wait_queue(&port->tty->write_wait, &wait);
706 /* clear out any remaining data in the buffer */
707 cypress_buf_clear(priv->buf);
708 spin_unlock_irqrestore(&priv->lock, flags);
709
710 /* wait for characters to drain from device */
711 bps = tty_get_baud_rate(port->tty);
712 if (bps > 1200)
713 timeout = max((HZ*2560)/bps,HZ/10);
714 else
715 timeout = 2*HZ;
22c43863 716 schedule_timeout_interruptible(timeout);
1da177e4
LT
717
718 dbg("%s - stopping urbs", __FUNCTION__);
719 usb_kill_urb (port->interrupt_in_urb);
720 usb_kill_urb (port->interrupt_out_urb);
721
722 if (port->tty) {
723 c_cflag = port->tty->termios->c_cflag;
724 if (c_cflag & HUPCL) {
725 /* drop dtr and rts */
726 priv = usb_get_serial_port_data(port);
727 spin_lock_irqsave(&priv->lock, flags);
728 priv->line_control = 0;
729 priv->cmd_ctrl = 1;
730 spin_unlock_irqrestore(&priv->lock, flags);
731 cypress_write(port, NULL, 0);
732 }
733 }
734
735 if (stats)
736 dev_info (&port->dev, "Statistics: %d Bytes In | %d Bytes Out | %d Commands Issued\n",
737 priv->bytes_in, priv->bytes_out, priv->cmd_count);
738} /* cypress_close */
739
740
741static int cypress_write(struct usb_serial_port *port, const unsigned char *buf, int count)
742{
743 struct cypress_private *priv = usb_get_serial_port_data(port);
744 unsigned long flags;
745
746 dbg("%s - port %d, %d bytes", __FUNCTION__, port->number, count);
747
748 /* line control commands, which need to be executed immediately,
749 are not put into the buffer for obvious reasons.
750 */
751 if (priv->cmd_ctrl) {
752 count = 0;
753 goto finish;
754 }
755
756 if (!count)
757 return count;
758
759 spin_lock_irqsave(&priv->lock, flags);
760 count = cypress_buf_put(priv->buf, buf, count);
761 spin_unlock_irqrestore(&priv->lock, flags);
762
763finish:
764 cypress_send(port);
765
766 return count;
767} /* cypress_write */
768
769
770static void cypress_send(struct usb_serial_port *port)
771{
772 int count = 0, result, offset, actual_size;
773 struct cypress_private *priv = usb_get_serial_port_data(port);
774 unsigned long flags;
775
78aef519
MI
776 if (!priv->comm_is_ok)
777 return;
778
1da177e4
LT
779 dbg("%s - port %d", __FUNCTION__, port->number);
780 dbg("%s - interrupt out size is %d", __FUNCTION__, port->interrupt_out_size);
781
782 spin_lock_irqsave(&priv->lock, flags);
783 if (priv->write_urb_in_use) {
784 dbg("%s - can't write, urb in use", __FUNCTION__);
785 spin_unlock_irqrestore(&priv->lock, flags);
786 return;
787 }
788 spin_unlock_irqrestore(&priv->lock, flags);
789
790 /* clear buffer */
791 memset(port->interrupt_out_urb->transfer_buffer, 0, port->interrupt_out_size);
792
793 spin_lock_irqsave(&priv->lock, flags);
794 switch (port->interrupt_out_size) {
795 case 32:
3cb4a4f7 796 /* this is for the CY7C64013... */
1da177e4
LT
797 offset = 2;
798 port->interrupt_out_buffer[0] = priv->line_control;
799 break;
800 case 8:
3cb4a4f7 801 /* this is for the CY7C63743... */
1da177e4
LT
802 offset = 1;
803 port->interrupt_out_buffer[0] = priv->line_control;
804 break;
805 default:
806 dbg("%s - wrong packet size", __FUNCTION__);
807 spin_unlock_irqrestore(&priv->lock, flags);
808 return;
809 }
810
811 if (priv->line_control & CONTROL_RESET)
812 priv->line_control &= ~CONTROL_RESET;
813
814 if (priv->cmd_ctrl) {
815 priv->cmd_count++;
816 dbg("%s - line control command being issued", __FUNCTION__);
817 spin_unlock_irqrestore(&priv->lock, flags);
818 goto send;
819 } else
820 spin_unlock_irqrestore(&priv->lock, flags);
821
822 count = cypress_buf_get(priv->buf, &port->interrupt_out_buffer[offset],
823 port->interrupt_out_size-offset);
824
825 if (count == 0) {
826 return;
827 }
828
829 switch (port->interrupt_out_size) {
830 case 32:
831 port->interrupt_out_buffer[1] = count;
832 break;
833 case 8:
834 port->interrupt_out_buffer[0] |= count;
835 }
836
837 dbg("%s - count is %d", __FUNCTION__, count);
838
839send:
840 spin_lock_irqsave(&priv->lock, flags);
841 priv->write_urb_in_use = 1;
842 spin_unlock_irqrestore(&priv->lock, flags);
843
844 if (priv->cmd_ctrl)
845 actual_size = 1;
846 else
847 actual_size = count + (port->interrupt_out_size == 32 ? 2 : 1);
848
849 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, port->interrupt_out_size,
850 port->interrupt_out_urb->transfer_buffer);
851
9aa8dae7
MI
852 usb_fill_int_urb(port->interrupt_out_urb, port->serial->dev,
853 usb_sndintpipe(port->serial->dev, port->interrupt_out_endpointAddress),
854 port->interrupt_out_buffer, port->interrupt_out_size,
855 cypress_write_int_callback, port, priv->write_urb_interval);
1da177e4
LT
856 result = usb_submit_urb (port->interrupt_out_urb, GFP_ATOMIC);
857 if (result) {
858 dev_err(&port->dev, "%s - failed submitting write urb, error %d\n", __FUNCTION__,
859 result);
860 priv->write_urb_in_use = 0;
78aef519 861 cypress_set_dead(port);
1da177e4
LT
862 }
863
864 spin_lock_irqsave(&priv->lock, flags);
865 if (priv->cmd_ctrl) {
866 priv->cmd_ctrl = 0;
867 }
868 priv->bytes_out += count; /* do not count the line control and size bytes */
869 spin_unlock_irqrestore(&priv->lock, flags);
870
cf2c7481 871 usb_serial_port_softint(port);
1da177e4
LT
872} /* cypress_send */
873
874
875/* returns how much space is available in the soft buffer */
876static int cypress_write_room(struct usb_serial_port *port)
877{
878 struct cypress_private *priv = usb_get_serial_port_data(port);
879 int room = 0;
880 unsigned long flags;
881
882 dbg("%s - port %d", __FUNCTION__, port->number);
883
884 spin_lock_irqsave(&priv->lock, flags);
885 room = cypress_buf_space_avail(priv->buf);
886 spin_unlock_irqrestore(&priv->lock, flags);
887
888 dbg("%s - returns %d", __FUNCTION__, room);
889 return room;
890}
891
892
893static int cypress_tiocmget (struct usb_serial_port *port, struct file *file)
894{
895 struct cypress_private *priv = usb_get_serial_port_data(port);
896 __u8 status, control;
897 unsigned int result = 0;
898 unsigned long flags;
899
900 dbg("%s - port %d", __FUNCTION__, port->number);
901
902 spin_lock_irqsave(&priv->lock, flags);
903 control = priv->line_control;
904 status = priv->current_status;
905 spin_unlock_irqrestore(&priv->lock, flags);
906
907 result = ((control & CONTROL_DTR) ? TIOCM_DTR : 0)
908 | ((control & CONTROL_RTS) ? TIOCM_RTS : 0)
909 | ((status & UART_CTS) ? TIOCM_CTS : 0)
910 | ((status & UART_DSR) ? TIOCM_DSR : 0)
911 | ((status & UART_RI) ? TIOCM_RI : 0)
912 | ((status & UART_CD) ? TIOCM_CD : 0);
913
914 dbg("%s - result = %x", __FUNCTION__, result);
915
916 return result;
917}
918
919
920static int cypress_tiocmset (struct usb_serial_port *port, struct file *file,
921 unsigned int set, unsigned int clear)
922{
923 struct cypress_private *priv = usb_get_serial_port_data(port);
924 unsigned long flags;
925
926 dbg("%s - port %d", __FUNCTION__, port->number);
927
928 spin_lock_irqsave(&priv->lock, flags);
929 if (set & TIOCM_RTS)
930 priv->line_control |= CONTROL_RTS;
931 if (set & TIOCM_DTR)
932 priv->line_control |= CONTROL_DTR;
933 if (clear & TIOCM_RTS)
934 priv->line_control &= ~CONTROL_RTS;
935 if (clear & TIOCM_DTR)
936 priv->line_control &= ~CONTROL_DTR;
937 spin_unlock_irqrestore(&priv->lock, flags);
938
939 priv->cmd_ctrl = 1;
940 return cypress_write(port, NULL, 0);
941}
942
943
944static int cypress_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg)
945{
946 struct cypress_private *priv = usb_get_serial_port_data(port);
947
948 dbg("%s - port %d, cmd 0x%.4x", __FUNCTION__, port->number, cmd);
949
950 switch (cmd) {
951 case TIOCGSERIAL:
952 if (copy_to_user((void __user *)arg, port->tty->termios, sizeof(struct termios))) {
953 return -EFAULT;
954 }
955 return (0);
956 break;
957 case TIOCSSERIAL:
958 if (copy_from_user(port->tty->termios, (void __user *)arg, sizeof(struct termios))) {
959 return -EFAULT;
960 }
961 /* here we need to call cypress_set_termios to invoke the new settings */
962 cypress_set_termios(port, &priv->tmp_termios);
963 return (0);
964 break;
965 /* these are called when setting baud rate from gpsd */
966 case TCGETS:
967 if (copy_to_user((void __user *)arg, port->tty->termios, sizeof(struct termios))) {
968 return -EFAULT;
969 }
970 return (0);
971 break;
972 case TCSETS:
973 if (copy_from_user(port->tty->termios, (void __user *)arg, sizeof(struct termios))) {
974 return -EFAULT;
975 }
976 /* here we need to call cypress_set_termios to invoke the new settings */
977 cypress_set_termios(port, &priv->tmp_termios);
978 return (0);
979 break;
980 /* This code comes from drivers/char/serial.c and ftdi_sio.c */
981 case TIOCMIWAIT:
982 while (priv != NULL) {
983 interruptible_sleep_on(&priv->delta_msr_wait);
984 /* see if a signal did it */
985 if (signal_pending(current))
986 return -ERESTARTSYS;
987 else {
988 char diff = priv->diff_status;
989
990 if (diff == 0) {
991 return -EIO; /* no change => error */
992 }
993
994 /* consume all events */
995 priv->diff_status = 0;
996
997 /* return 0 if caller wanted to know about these bits */
998 if ( ((arg & TIOCM_RNG) && (diff & UART_RI)) ||
999 ((arg & TIOCM_DSR) && (diff & UART_DSR)) ||
1000 ((arg & TIOCM_CD) && (diff & UART_CD)) ||
1001 ((arg & TIOCM_CTS) && (diff & UART_CTS)) ) {
1002 return 0;
1003 }
1004 /* otherwise caller can't care less about what happened,
1005 * and so we continue to wait for more events.
1006 */
1007 }
1008 }
1009 return 0;
1010 break;
1011 default:
1012 break;
1013 }
1014
1015 dbg("%s - arg not supported - it was 0x%04x - check include/asm/ioctls.h", __FUNCTION__, cmd);
1016
1017 return -ENOIOCTLCMD;
1018} /* cypress_ioctl */
1019
1020
b9db07fb
LM
1021static void cypress_set_termios (struct usb_serial_port *port,
1022 struct termios *old_termios)
1da177e4
LT
1023{
1024 struct cypress_private *priv = usb_get_serial_port_data(port);
1025 struct tty_struct *tty;
1026 int data_bits, stop_bits, parity_type, parity_enable;
1027 unsigned cflag, iflag, baud_mask;
1028 unsigned long flags;
1029 __u8 oldlines;
3cb4a4f7 1030 int linechange = 0;
b9db07fb 1031
1da177e4
LT
1032 dbg("%s - port %d", __FUNCTION__, port->number);
1033
1034 tty = port->tty;
1035 if ((!tty) || (!tty->termios)) {
1036 dbg("%s - no tty structures", __FUNCTION__);
1037 return;
1038 }
1039
1040 spin_lock_irqsave(&priv->lock, flags);
1041 if (!priv->termios_initialized) {
1042 if (priv->chiptype == CT_EARTHMATE) {
1043 *(tty->termios) = tty_std_termios;
b9db07fb
LM
1044 tty->termios->c_cflag = B4800 | CS8 | CREAD | HUPCL |
1045 CLOCAL;
1da177e4
LT
1046 } else if (priv->chiptype == CT_CYPHIDCOM) {
1047 *(tty->termios) = tty_std_termios;
b9db07fb
LM
1048 tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
1049 CLOCAL;
a5c44e29
LM
1050 } else if (priv->chiptype == CT_CA42V2) {
1051 *(tty->termios) = tty_std_termios;
1052 tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
1053 CLOCAL;
1da177e4
LT
1054 }
1055 priv->termios_initialized = 1;
1056 }
1057 spin_unlock_irqrestore(&priv->lock, flags);
1058
1059 cflag = tty->termios->c_cflag;
1060 iflag = tty->termios->c_iflag;
1061
1062 /* check if there are new settings */
1063 if (old_termios) {
1064 if ((cflag != old_termios->c_cflag) ||
b9db07fb
LM
1065 (RELEVANT_IFLAG(iflag) !=
1066 RELEVANT_IFLAG(old_termios->c_iflag))) {
1067 dbg("%s - attempting to set new termios settings",
1068 __FUNCTION__);
1069 /* should make a copy of this in case something goes
1070 * wrong in the function, we can restore it */
1da177e4
LT
1071 spin_lock_irqsave(&priv->lock, flags);
1072 priv->tmp_termios = *(tty->termios);
b9db07fb 1073 spin_unlock_irqrestore(&priv->lock, flags);
1da177e4
LT
1074 } else {
1075 dbg("%s - nothing to do, exiting", __FUNCTION__);
1076 return;
1077 }
1078 } else
1079 return;
1080
1081 /* set number of data bits, parity, stop bits */
1082 /* when parity is disabled the parity type bit is ignored */
1083
b9db07fb
LM
1084 /* 1 means 2 stop bits, 0 means 1 stop bit */
1085 stop_bits = cflag & CSTOPB ? 1 : 0;
1086
1da177e4
LT
1087 if (cflag & PARENB) {
1088 parity_enable = 1;
b9db07fb
LM
1089 /* 1 means odd parity, 0 means even parity */
1090 parity_type = cflag & PARODD ? 1 : 0;
1da177e4
LT
1091 } else
1092 parity_enable = parity_type = 0;
1093
1094 if (cflag & CSIZE) {
1095 switch (cflag & CSIZE) {
b9db07fb
LM
1096 case CS5:
1097 data_bits = 0;
1098 break;
1099 case CS6:
1100 data_bits = 1;
1101 break;
1102 case CS7:
1103 data_bits = 2;
1104 break;
1105 case CS8:
1106 data_bits = 3;
1107 break;
1108 default:
1109 err("%s - CSIZE was set, but not CS5-CS8",
1110 __FUNCTION__);
1111 data_bits = 3;
1da177e4
LT
1112 }
1113 } else
1114 data_bits = 3;
1115
1116 spin_lock_irqsave(&priv->lock, flags);
1117 oldlines = priv->line_control;
1118 if ((cflag & CBAUD) == B0) {
1119 /* drop dtr and rts */
1120 dbg("%s - dropping the lines, baud rate 0bps", __FUNCTION__);
1121 baud_mask = B0;
1122 priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
1123 } else {
1124 baud_mask = (cflag & CBAUD);
1125 switch(baud_mask) {
b9db07fb
LM
1126 case B300:
1127 dbg("%s - setting baud 300bps", __FUNCTION__);
1128 break;
1129 case B600:
1130 dbg("%s - setting baud 600bps", __FUNCTION__);
1131 break;
1132 case B1200:
1133 dbg("%s - setting baud 1200bps", __FUNCTION__);
1134 break;
1135 case B2400:
1136 dbg("%s - setting baud 2400bps", __FUNCTION__);
1137 break;
1138 case B4800:
1139 dbg("%s - setting baud 4800bps", __FUNCTION__);
1140 break;
1141 case B9600:
1142 dbg("%s - setting baud 9600bps", __FUNCTION__);
1143 break;
1144 case B19200:
1145 dbg("%s - setting baud 19200bps", __FUNCTION__);
1146 break;
1147 case B38400:
1148 dbg("%s - setting baud 38400bps", __FUNCTION__);
1149 break;
1150 case B57600:
1151 dbg("%s - setting baud 57600bps", __FUNCTION__);
1152 break;
1153 case B115200:
1154 dbg("%s - setting baud 115200bps", __FUNCTION__);
1155 break;
1156 default:
1157 dbg("%s - unknown masked baud rate", __FUNCTION__);
1da177e4 1158 }
3cb4a4f7 1159 priv->line_control = (CONTROL_DTR | CONTROL_RTS);
1da177e4
LT
1160 }
1161 spin_unlock_irqrestore(&priv->lock, flags);
1da177e4 1162
b9db07fb
LM
1163 dbg("%s - sending %d stop_bits, %d parity_enable, %d parity_type, "
1164 "%d data_bits (+5)", __FUNCTION__, stop_bits,
1165 parity_enable, parity_type, data_bits);
1166
1167 cypress_serial_control(port, baud_mask, data_bits, stop_bits,
1168 parity_enable, parity_type, 0, CYPRESS_SET_CONFIG);
1da177e4 1169
b9db07fb
LM
1170 /* we perform a CYPRESS_GET_CONFIG so that the current settings are
1171 * filled into the private structure this should confirm that all is
1172 * working if it returns what we just set */
1da177e4
LT
1173 cypress_serial_control(port, 0, 0, 0, 0, 0, 0, CYPRESS_GET_CONFIG);
1174
b9db07fb
LM
1175 /* Here we can define custom tty settings for devices; the main tty
1176 * termios flag base comes from empeg.c */
1da177e4 1177
b9db07fb 1178 spin_lock_irqsave(&priv->lock, flags);
1da177e4 1179 if ( (priv->chiptype == CT_EARTHMATE) && (priv->baud_rate == 4800) ) {
b9db07fb
LM
1180 dbg("Using custom termios settings for a baud rate of "
1181 "4800bps.");
1da177e4
LT
1182 /* define custom termios settings for NMEA protocol */
1183
1da177e4 1184 tty->termios->c_iflag /* input modes - */
b9db07fb
LM
1185 &= ~(IGNBRK /* disable ignore break */
1186 | BRKINT /* disable break causes interrupt */
1187 | PARMRK /* disable mark parity errors */
1188 | ISTRIP /* disable clear high bit of input char */
1189 | INLCR /* disable translate NL to CR */
1190 | IGNCR /* disable ignore CR */
1191 | ICRNL /* disable translate CR to NL */
1192 | IXON); /* disable enable XON/XOFF flow control */
1193
1da177e4 1194 tty->termios->c_oflag /* output modes */
b9db07fb 1195 &= ~OPOST; /* disable postprocess output char */
1da177e4 1196
b9db07fb
LM
1197 tty->termios->c_lflag /* line discipline modes */
1198 &= ~(ECHO /* disable echo input characters */
1199 | ECHONL /* disable echo new line */
1200 | ICANON /* disable erase, kill, werase, and rprnt
1201 special characters */
1202 | ISIG /* disable interrupt, quit, and suspend
1203 special characters */
1204 | IEXTEN); /* disable non-POSIX special characters */
3cb4a4f7 1205 } /* CT_CYPHIDCOM: Application should handle this for device */
1da177e4 1206
1da177e4
LT
1207 linechange = (priv->line_control != oldlines);
1208 spin_unlock_irqrestore(&priv->lock, flags);
1209
1210 /* if necessary, set lines */
3cb4a4f7 1211 if (linechange) {
1da177e4
LT
1212 priv->cmd_ctrl = 1;
1213 cypress_write(port, NULL, 0);
1214 }
1da177e4
LT
1215} /* cypress_set_termios */
1216
b9db07fb 1217
1da177e4
LT
1218/* returns amount of data still left in soft buffer */
1219static int cypress_chars_in_buffer(struct usb_serial_port *port)
1220{
1221 struct cypress_private *priv = usb_get_serial_port_data(port);
1222 int chars = 0;
1223 unsigned long flags;
1224
1225 dbg("%s - port %d", __FUNCTION__, port->number);
1226
1227 spin_lock_irqsave(&priv->lock, flags);
1228 chars = cypress_buf_data_avail(priv->buf);
1229 spin_unlock_irqrestore(&priv->lock, flags);
1230
1231 dbg("%s - returns %d", __FUNCTION__, chars);
1232 return chars;
1233}
1234
1235
1236static void cypress_throttle (struct usb_serial_port *port)
1237{
1238 struct cypress_private *priv = usb_get_serial_port_data(port);
1239 unsigned long flags;
1240
1241 dbg("%s - port %d", __FUNCTION__, port->number);
1242
1243 spin_lock_irqsave(&priv->lock, flags);
1244 priv->rx_flags = THROTTLED;
b9db07fb 1245 spin_unlock_irqrestore(&priv->lock, flags);
1da177e4
LT
1246}
1247
1248
1249static void cypress_unthrottle (struct usb_serial_port *port)
1250{
1251 struct cypress_private *priv = usb_get_serial_port_data(port);
1252 int actually_throttled, result;
1253 unsigned long flags;
1254
1255 dbg("%s - port %d", __FUNCTION__, port->number);
1256
1257 spin_lock_irqsave(&priv->lock, flags);
1258 actually_throttled = priv->rx_flags & ACTUALLY_THROTTLED;
1259 priv->rx_flags = 0;
1260 spin_unlock_irqrestore(&priv->lock, flags);
1261
78aef519
MI
1262 if (!priv->comm_is_ok)
1263 return;
1264
1da177e4
LT
1265 if (actually_throttled) {
1266 port->interrupt_in_urb->dev = port->serial->dev;
1267
1268 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
78aef519 1269 if (result) {
b9db07fb
LM
1270 dev_err(&port->dev, "%s - failed submitting read urb, "
1271 "error %d\n", __FUNCTION__, result);
78aef519
MI
1272 cypress_set_dead(port);
1273 }
1da177e4
LT
1274 }
1275}
1276
1277
7d12e780 1278static void cypress_read_int_callback(struct urb *urb)
1da177e4
LT
1279{
1280 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
1281 struct cypress_private *priv = usb_get_serial_port_data(port);
1282 struct tty_struct *tty;
1283 unsigned char *data = urb->transfer_buffer;
1284 unsigned long flags;
b9db07fb 1285 char tty_flag = TTY_NORMAL;
1da177e4
LT
1286 int havedata = 0;
1287 int bytes = 0;
1288 int result;
1289 int i = 0;
1290
1291 dbg("%s - port %d", __FUNCTION__, port->number);
1292
78aef519
MI
1293 switch (urb->status) {
1294 case 0: /* success */
1295 break;
1296 case -ECONNRESET:
1297 case -ENOENT:
1298 case -ESHUTDOWN:
1299 /* precursor to disconnect so just go away */
1300 return;
1301 case -EPIPE:
1302 usb_clear_halt(port->serial->dev,0x81);
1303 break;
1304 default:
1305 /* something ugly is going on... */
1306 dev_err(&urb->dev->dev,"%s - unexpected nonzero read status received: %d\n",
1307 __FUNCTION__,urb->status);
1308 cypress_set_dead(port);
1da177e4
LT
1309 return;
1310 }
1311
1312 spin_lock_irqsave(&priv->lock, flags);
1313 if (priv->rx_flags & THROTTLED) {
1314 dbg("%s - now throttling", __FUNCTION__);
1315 priv->rx_flags |= ACTUALLY_THROTTLED;
1316 spin_unlock_irqrestore(&priv->lock, flags);
1317 return;
1318 }
1319 spin_unlock_irqrestore(&priv->lock, flags);
1320
1321 tty = port->tty;
1322 if (!tty) {
1323 dbg("%s - bad tty pointer - exiting", __FUNCTION__);
1324 return;
1325 }
1326
1327 spin_lock_irqsave(&priv->lock, flags);
1328 switch(urb->actual_length) {
1329 case 32:
3cb4a4f7 1330 /* This is for the CY7C64013... */
1da177e4 1331 priv->current_status = data[0] & 0xF8;
b9db07fb
LM
1332 bytes = data[1] + 2;
1333 i = 2;
1da177e4
LT
1334 if (bytes > 2)
1335 havedata = 1;
1336 break;
1337 case 8:
3cb4a4f7 1338 /* This is for the CY7C63743... */
1da177e4 1339 priv->current_status = data[0] & 0xF8;
b9db07fb
LM
1340 bytes = (data[0] & 0x07) + 1;
1341 i = 1;
1da177e4
LT
1342 if (bytes > 1)
1343 havedata = 1;
1344 break;
1345 default:
b9db07fb
LM
1346 dbg("%s - wrong packet size - received %d bytes",
1347 __FUNCTION__, urb->actual_length);
1da177e4
LT
1348 spin_unlock_irqrestore(&priv->lock, flags);
1349 goto continue_read;
1350 }
1351 spin_unlock_irqrestore(&priv->lock, flags);
1352
b9db07fb
LM
1353 usb_serial_debug_data (debug, &port->dev, __FUNCTION__,
1354 urb->actual_length, data);
1da177e4
LT
1355
1356 spin_lock_irqsave(&priv->lock, flags);
1357 /* check to see if status has changed */
1358 if (priv != NULL) {
1359 if (priv->current_status != priv->prev_status) {
b9db07fb
LM
1360 priv->diff_status |= priv->current_status ^
1361 priv->prev_status;
1da177e4
LT
1362 wake_up_interruptible(&priv->delta_msr_wait);
1363 priv->prev_status = priv->current_status;
1364 }
1365 }
b9db07fb 1366 spin_unlock_irqrestore(&priv->lock, flags);
1da177e4 1367
b9db07fb
LM
1368 /* hangup, as defined in acm.c... this might be a bad place for it
1369 * though */
1370 if (tty && !(tty->termios->c_cflag & CLOCAL) &&
1371 !(priv->current_status & UART_CD)) {
1da177e4
LT
1372 dbg("%s - calling hangup", __FUNCTION__);
1373 tty_hangup(tty);
1374 goto continue_read;
1375 }
1376
b9db07fb
LM
1377 /* There is one error bit... I'm assuming it is a parity error
1378 * indicator as the generic firmware will set this bit to 1 if a
1379 * parity error occurs.
1380 * I can not find reference to any other error events. */
1da177e4
LT
1381 spin_lock_irqsave(&priv->lock, flags);
1382 if (priv->current_status & CYP_ERROR) {
1383 spin_unlock_irqrestore(&priv->lock, flags);
1384 tty_flag = TTY_PARITY;
1385 dbg("%s - Parity Error detected", __FUNCTION__);
1386 } else
1387 spin_unlock_irqrestore(&priv->lock, flags);
1388
1389 /* process read if there is data other than line status */
1390 if (tty && (bytes > i)) {
33f0f88f 1391 bytes = tty_buffer_request_room(tty, bytes);
1da177e4 1392 for (; i < bytes ; ++i) {
b9db07fb
LM
1393 dbg("pushing byte number %d - %d - %c", i, data[i],
1394 data[i]);
1da177e4
LT
1395 tty_insert_flip_char(tty, data[i], tty_flag);
1396 }
1397 tty_flip_buffer_push(port->tty);
1398 }
1399
1400 spin_lock_irqsave(&priv->lock, flags);
b9db07fb
LM
1401 /* control and status byte(s) are also counted */
1402 priv->bytes_in += bytes;
1da177e4
LT
1403 spin_unlock_irqrestore(&priv->lock, flags);
1404
1405continue_read:
b9db07fb
LM
1406
1407 /* Continue trying to always read... unless the port has closed. */
1da177e4 1408
78aef519 1409 if (port->open_count > 0 && priv->comm_is_ok) {
b9db07fb
LM
1410 usb_fill_int_urb(port->interrupt_in_urb, port->serial->dev,
1411 usb_rcvintpipe(port->serial->dev,
1412 port->interrupt_in_endpointAddress),
1413 port->interrupt_in_urb->transfer_buffer,
1414 port->interrupt_in_urb->transfer_buffer_length,
0257fa9f 1415 cypress_read_int_callback, port, priv->read_urb_interval);
b9db07fb 1416 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
78aef519 1417 if (result) {
b9db07fb
LM
1418 dev_err(&urb->dev->dev, "%s - failed resubmitting "
1419 "read urb, error %d\n", __FUNCTION__,
1420 result);
78aef519
MI
1421 cypress_set_dead(port);
1422 }
1da177e4 1423 }
b9db07fb 1424
1da177e4
LT
1425 return;
1426} /* cypress_read_int_callback */
1427
1428
7d12e780 1429static void cypress_write_int_callback(struct urb *urb)
1da177e4
LT
1430{
1431 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
1432 struct cypress_private *priv = usb_get_serial_port_data(port);
1433 int result;
1434
1435 dbg("%s - port %d", __FUNCTION__, port->number);
1436
1437 switch (urb->status) {
1438 case 0:
1439 /* success */
1440 break;
1441 case -ECONNRESET:
1442 case -ENOENT:
1443 case -ESHUTDOWN:
1444 /* this urb is terminated, clean up */
1445 dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status);
1446 priv->write_urb_in_use = 0;
1447 return;
78aef519
MI
1448 case -EPIPE: /* no break needed; clear halt and resubmit */
1449 if (!priv->comm_is_ok)
1450 break;
3cb4a4f7 1451 usb_clear_halt(port->serial->dev, 0x02);
1da177e4 1452 /* error in the urb, so we have to resubmit it */
1da177e4
LT
1453 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
1454 port->interrupt_out_urb->transfer_buffer_length = 1;
1455 port->interrupt_out_urb->dev = port->serial->dev;
1456 result = usb_submit_urb(port->interrupt_out_urb, GFP_ATOMIC);
78aef519 1457 if (!result)
1da177e4 1458 return;
78aef519
MI
1459 dev_err(&urb->dev->dev, "%s - failed resubmitting write urb, error %d\n",
1460 __FUNCTION__, result);
1461 cypress_set_dead(port);
1462 break;
1463 default:
1464 dev_err(&urb->dev->dev,"%s - unexpected nonzero write status received: %d\n",
1465 __FUNCTION__,urb->status);
1466 cypress_set_dead(port);
1467 break;
1da177e4
LT
1468 }
1469
1470 priv->write_urb_in_use = 0;
1471
1472 /* send any buffered data */
1473 cypress_send(port);
1474}
1475
1476
1477/*****************************************************************************
1478 * Write buffer functions - buffering code from pl2303 used
1479 *****************************************************************************/
1480
1481/*
1482 * cypress_buf_alloc
1483 *
1484 * Allocate a circular buffer and all associated memory.
1485 */
1486
1487static struct cypress_buf *cypress_buf_alloc(unsigned int size)
1488{
1489
1490 struct cypress_buf *cb;
1491
1492
1493 if (size == 0)
1494 return NULL;
1495
1496 cb = (struct cypress_buf *)kmalloc(sizeof(struct cypress_buf), GFP_KERNEL);
1497 if (cb == NULL)
1498 return NULL;
1499
1500 cb->buf_buf = kmalloc(size, GFP_KERNEL);
1501 if (cb->buf_buf == NULL) {
1502 kfree(cb);
1503 return NULL;
1504 }
1505
1506 cb->buf_size = size;
1507 cb->buf_get = cb->buf_put = cb->buf_buf;
1508
1509 return cb;
1510
1511}
1512
1513
1514/*
1515 * cypress_buf_free
1516 *
1517 * Free the buffer and all associated memory.
1518 */
1519
1520static void cypress_buf_free(struct cypress_buf *cb)
1521{
1bc3c9e1
JJ
1522 if (cb) {
1523 kfree(cb->buf_buf);
1da177e4
LT
1524 kfree(cb);
1525 }
1526}
1527
1528
1529/*
1530 * cypress_buf_clear
1531 *
1532 * Clear out all data in the circular buffer.
1533 */
1534
1535static void cypress_buf_clear(struct cypress_buf *cb)
1536{
1537 if (cb != NULL)
1538 cb->buf_get = cb->buf_put;
1539 /* equivalent to a get of all data available */
1540}
1541
1542
1543/*
1544 * cypress_buf_data_avail
1545 *
1546 * Return the number of bytes of data available in the circular
1547 * buffer.
1548 */
1549
1550static unsigned int cypress_buf_data_avail(struct cypress_buf *cb)
1551{
1552 if (cb != NULL)
1553 return ((cb->buf_size + cb->buf_put - cb->buf_get) % cb->buf_size);
1554 else
1555 return 0;
1556}
1557
1558
1559/*
1560 * cypress_buf_space_avail
1561 *
1562 * Return the number of bytes of space available in the circular
1563 * buffer.
1564 */
1565
1566static unsigned int cypress_buf_space_avail(struct cypress_buf *cb)
1567{
1568 if (cb != NULL)
1569 return ((cb->buf_size + cb->buf_get - cb->buf_put - 1) % cb->buf_size);
1570 else
1571 return 0;
1572}
1573
1574
1575/*
1576 * cypress_buf_put
1577 *
1578 * Copy data data from a user buffer and put it into the circular buffer.
1579 * Restrict to the amount of space available.
1580 *
1581 * Return the number of bytes copied.
1582 */
1583
1584static unsigned int cypress_buf_put(struct cypress_buf *cb, const char *buf,
1585 unsigned int count)
1586{
1587
1588 unsigned int len;
1589
1590
1591 if (cb == NULL)
1592 return 0;
1593
1594 len = cypress_buf_space_avail(cb);
1595 if (count > len)
1596 count = len;
1597
1598 if (count == 0)
1599 return 0;
1600
1601 len = cb->buf_buf + cb->buf_size - cb->buf_put;
1602 if (count > len) {
1603 memcpy(cb->buf_put, buf, len);
1604 memcpy(cb->buf_buf, buf+len, count - len);
1605 cb->buf_put = cb->buf_buf + count - len;
1606 } else {
1607 memcpy(cb->buf_put, buf, count);
1608 if (count < len)
1609 cb->buf_put += count;
1610 else /* count == len */
1611 cb->buf_put = cb->buf_buf;
1612 }
1613
1614 return count;
1615
1616}
1617
1618
1619/*
1620 * cypress_buf_get
1621 *
1622 * Get data from the circular buffer and copy to the given buffer.
1623 * Restrict to the amount of data available.
1624 *
1625 * Return the number of bytes copied.
1626 */
1627
1628static unsigned int cypress_buf_get(struct cypress_buf *cb, char *buf,
1629 unsigned int count)
1630{
1631
1632 unsigned int len;
1633
1634
1635 if (cb == NULL)
1636 return 0;
1637
1638 len = cypress_buf_data_avail(cb);
1639 if (count > len)
1640 count = len;
1641
1642 if (count == 0)
1643 return 0;
1644
1645 len = cb->buf_buf + cb->buf_size - cb->buf_get;
1646 if (count > len) {
1647 memcpy(buf, cb->buf_get, len);
1648 memcpy(buf+len, cb->buf_buf, count - len);
1649 cb->buf_get = cb->buf_buf + count - len;
1650 } else {
1651 memcpy(buf, cb->buf_get, count);
1652 if (count < len)
1653 cb->buf_get += count;
1654 else /* count == len */
1655 cb->buf_get = cb->buf_buf;
1656 }
1657
1658 return count;
1659
1660}
1661
1662/*****************************************************************************
1663 * Module functions
1664 *****************************************************************************/
1665
1666static int __init cypress_init(void)
1667{
1668 int retval;
1669
1670 dbg("%s", __FUNCTION__);
1671
1672 retval = usb_serial_register(&cypress_earthmate_device);
1673 if (retval)
1674 goto failed_em_register;
1675 retval = usb_serial_register(&cypress_hidcom_device);
1676 if (retval)
1677 goto failed_hidcom_register;
a5c44e29
LM
1678 retval = usb_serial_register(&cypress_ca42v2_device);
1679 if (retval)
1680 goto failed_ca42v2_register;
1da177e4
LT
1681 retval = usb_register(&cypress_driver);
1682 if (retval)
1683 goto failed_usb_register;
1684
1685 info(DRIVER_DESC " " DRIVER_VERSION);
1686 return 0;
1687failed_usb_register:
1688 usb_deregister(&cypress_driver);
a5c44e29
LM
1689failed_ca42v2_register:
1690 usb_serial_deregister(&cypress_ca42v2_device);
1da177e4
LT
1691failed_hidcom_register:
1692 usb_serial_deregister(&cypress_hidcom_device);
1693failed_em_register:
1694 usb_serial_deregister(&cypress_earthmate_device);
1695
1696 return retval;
1697}
1698
1699
1700static void __exit cypress_exit (void)
1701{
1702 dbg("%s", __FUNCTION__);
1703
1704 usb_deregister (&cypress_driver);
1705 usb_serial_deregister (&cypress_earthmate_device);
1706 usb_serial_deregister (&cypress_hidcom_device);
a5c44e29 1707 usb_serial_deregister (&cypress_ca42v2_device);
1da177e4
LT
1708}
1709
1710
1711module_init(cypress_init);
1712module_exit(cypress_exit);
1713
1714MODULE_AUTHOR( DRIVER_AUTHOR );
1715MODULE_DESCRIPTION( DRIVER_DESC );
1716MODULE_VERSION( DRIVER_VERSION );
1717MODULE_LICENSE("GPL");
1718
1719module_param(debug, bool, S_IRUGO | S_IWUSR);
1720MODULE_PARM_DESC(debug, "Debug enabled or not");
1721module_param(stats, bool, S_IRUGO | S_IWUSR);
1722MODULE_PARM_DESC(stats, "Enable statistics or not");
3cb4a4f7
LM
1723module_param(interval, int, S_IRUGO | S_IWUSR);
1724MODULE_PARM_DESC(interval, "Overrides interrupt interval");