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