blob: 4fb8e59d15275b2fa94fba954e33cb2a3e8b9f91 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * USB Cypress M8 driver
3 *
4 * Copyright (C) 2004
5 * Lonnie Mendez (dignome@gmail.com)
6 * Copyright (C) 2003,2004
7 * Neil Whelchel (koyama@firstlight.net)
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * See Documentation/usb/usb-serial.txt for more information on using this driver
15 *
16 * See http://geocities.com/i0xox0i for information on this driver and the
17 * earthmate usb device.
18 *
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -050019 * Lonnie Mendez <dignome@gmail.com>
20 * 4-29-2005
21 * Fixed problem where setting or retreiving the serial config would fail with
22 * EPIPE. Removed CRTS toggling so the driver behaves more like other usbserial
23 * adapters. Issued new interval of 1ms instead of the default 10ms. As a
24 * result, transfer speed has been substantially increased. From avg. 850bps to
25 * avg. 3300bps. initial termios has also been modified. Cleaned up code and
26 * formatting issues so it is more readable. Replaced the C++ style comments.
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 *
28 * Lonnie Mendez <dignome@gmail.com>
29 * 12-15-2004
30 * Incorporated write buffering from pl2303 driver. Fixed bug with line
31 * handling so both lines are raised in cypress_open. (was dropping rts)
32 * Various code cleanups made as well along with other misc bug fixes.
33 *
34 * Lonnie Mendez <dignome@gmail.com>
35 * 04-10-2004
36 * Driver modified to support dynamic line settings. Various improvments
37 * and features.
38 *
39 * Neil Whelchel
40 * 10-2003
41 * Driver first released.
42 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 */
44
45/* Thanks to Neil Whelchel for writing the first cypress m8 implementation for linux. */
46/* Thanks to cypress for providing references for the hid reports. */
47/* Thanks to Jiang Zhang for providing links and for general help. */
48/* Code originates and was built up from ftdi_sio, belkin, pl2303 and others. */
49
50
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;
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -050077static int interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79/*
80 * Version Information
81 */
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -050082#define DRIVER_VERSION "v1.09"
Linus Torvalds1da177e2005-04-16 15:20:36 -070083#define DRIVER_AUTHOR "Lonnie Mendez <dignome@gmail.com>, Neil Whelchel <koyama@firstlight.net>"
84#define DRIVER_DESC "Cypress USB to Serial Driver"
85
86/* write buffer size defines */
87#define CYPRESS_BUF_SIZE 1024
88#define CYPRESS_CLOSING_WAIT (30*HZ)
89
90static struct usb_device_id id_table_earthmate [] = {
91 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB) },
Lonnie Mendez25b6f082005-05-10 17:09:52 -050092 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB_LT20) },
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 { } /* Terminating entry */
94};
95
96static struct usb_device_id id_table_cyphidcomrs232 [] = {
97 { USB_DEVICE(VENDOR_ID_CYPRESS, PRODUCT_ID_CYPHIDCOM) },
98 { } /* Terminating entry */
99};
100
101static struct usb_device_id id_table_combined [] = {
102 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB) },
Lonnie Mendez25b6f082005-05-10 17:09:52 -0500103 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB_LT20) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 { 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,
115};
116
117struct cypress_private {
118 spinlock_t lock; /* private lock */
119 int chiptype; /* identifier of device, for quirks/etc */
120 int bytes_in; /* used for statistics */
121 int bytes_out; /* used for statistics */
122 int cmd_count; /* used for statistics */
123 int cmd_ctrl; /* always set this to 1 before issuing a command */
124 struct cypress_buf *buf; /* write buffer */
125 int write_urb_in_use; /* write urb in use indicator */
126 int termios_initialized;
127 __u8 line_control; /* holds dtr / rts value */
128 __u8 current_status; /* received from last read - info on dsr,cts,cd,ri,etc */
129 __u8 current_config; /* stores the current configuration byte */
130 __u8 rx_flags; /* throttling - used from whiteheat/ftdi_sio */
131 int baud_rate; /* stores current baud rate in integer form */
132 int cbr_mask; /* stores current baud rate in masked form */
133 int isthrottled; /* if throttled, discard reads */
134 wait_queue_head_t delta_msr_wait; /* used for TIOCMIWAIT */
135 char prev_status, diff_status; /* used for TIOCMIWAIT */
136 /* we pass a pointer to this as the arguement sent to cypress_set_termios old_termios */
137 struct termios tmp_termios; /* stores the old termios settings */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138};
139
140/* write buffer structure */
141struct cypress_buf {
142 unsigned int buf_size;
143 char *buf_buf;
144 char *buf_get;
145 char *buf_put;
146};
147
148/* function prototypes for the Cypress USB to serial device */
149static int cypress_earthmate_startup (struct usb_serial *serial);
150static int cypress_hidcom_startup (struct usb_serial *serial);
151static void cypress_shutdown (struct usb_serial *serial);
152static int cypress_open (struct usb_serial_port *port, struct file *filp);
153static void cypress_close (struct usb_serial_port *port, struct file *filp);
154static int cypress_write (struct usb_serial_port *port, const unsigned char *buf, int count);
155static void cypress_send (struct usb_serial_port *port);
156static int cypress_write_room (struct usb_serial_port *port);
157static int cypress_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg);
158static void cypress_set_termios (struct usb_serial_port *port, struct termios * old);
159static int cypress_tiocmget (struct usb_serial_port *port, struct file *file);
160static int cypress_tiocmset (struct usb_serial_port *port, struct file *file, unsigned int set, unsigned int clear);
161static int cypress_chars_in_buffer (struct usb_serial_port *port);
162static void cypress_throttle (struct usb_serial_port *port);
163static void cypress_unthrottle (struct usb_serial_port *port);
164static void cypress_read_int_callback (struct urb *urb, struct pt_regs *regs);
165static void cypress_write_int_callback (struct urb *urb, struct pt_regs *regs);
166/* baud helper functions */
167static int mask_to_rate (unsigned mask);
168static unsigned rate_to_mask (int rate);
169/* write buffer functions */
170static struct cypress_buf *cypress_buf_alloc(unsigned int size);
171static void cypress_buf_free(struct cypress_buf *cb);
172static void cypress_buf_clear(struct cypress_buf *cb);
173static unsigned int cypress_buf_data_avail(struct cypress_buf *cb);
174static unsigned int cypress_buf_space_avail(struct cypress_buf *cb);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500175static unsigned int cypress_buf_put(struct cypress_buf *cb, const char *buf, unsigned int count);
176static unsigned int cypress_buf_get(struct cypress_buf *cb, char *buf, unsigned int count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700179static struct usb_serial_driver cypress_earthmate_device = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 .owner = THIS_MODULE,
181 .name = "DeLorme Earthmate USB",
182 .short_name = "earthmate",
183 .id_table = id_table_earthmate,
184 .num_interrupt_in = 1,
185 .num_interrupt_out = 1,
186 .num_bulk_in = NUM_DONT_CARE,
187 .num_bulk_out = NUM_DONT_CARE,
188 .num_ports = 1,
189 .attach = cypress_earthmate_startup,
190 .shutdown = cypress_shutdown,
191 .open = cypress_open,
192 .close = cypress_close,
193 .write = cypress_write,
194 .write_room = cypress_write_room,
195 .ioctl = cypress_ioctl,
196 .set_termios = cypress_set_termios,
197 .tiocmget = cypress_tiocmget,
198 .tiocmset = cypress_tiocmset,
199 .chars_in_buffer = cypress_chars_in_buffer,
200 .throttle = cypress_throttle,
201 .unthrottle = cypress_unthrottle,
202 .read_int_callback = cypress_read_int_callback,
203 .write_int_callback = cypress_write_int_callback,
204};
205
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700206static struct usb_serial_driver cypress_hidcom_device = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 .owner = THIS_MODULE,
208 .name = "HID->COM RS232 Adapter",
209 .short_name = "cyphidcom",
210 .id_table = id_table_cyphidcomrs232,
211 .num_interrupt_in = 1,
212 .num_interrupt_out = 1,
213 .num_bulk_in = NUM_DONT_CARE,
214 .num_bulk_out = NUM_DONT_CARE,
215 .num_ports = 1,
216 .attach = cypress_hidcom_startup,
217 .shutdown = cypress_shutdown,
218 .open = cypress_open,
219 .close = cypress_close,
220 .write = cypress_write,
221 .write_room = cypress_write_room,
222 .ioctl = cypress_ioctl,
223 .set_termios = cypress_set_termios,
224 .tiocmget = cypress_tiocmget,
225 .tiocmset = cypress_tiocmset,
226 .chars_in_buffer = cypress_chars_in_buffer,
227 .throttle = cypress_throttle,
228 .unthrottle = cypress_unthrottle,
229 .read_int_callback = cypress_read_int_callback,
230 .write_int_callback = cypress_write_int_callback,
231};
232
233
234/*****************************************************************************
235 * Cypress serial helper functions
236 *****************************************************************************/
237
238
Steven Cole093cf722005-05-03 19:07:24 -0600239/* This function can either set or retrieve the current serial line settings */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240static int cypress_serial_control (struct usb_serial_port *port, unsigned baud_mask, int data_bits, int stop_bits,
241 int parity_enable, int parity_type, int reset, int cypress_request_type)
242{
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500243 int new_baudrate = 0, retval = 0, tries = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 struct cypress_private *priv;
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500245 __u8 feature_buffer[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 unsigned long flags;
247
248 dbg("%s", __FUNCTION__);
249
250 priv = usb_get_serial_port_data(port);
251
252 switch(cypress_request_type) {
253 case CYPRESS_SET_CONFIG:
254
255 /*
256 * The general purpose firmware for the Cypress M8 allows for a maximum speed
257 * of 57600bps (I have no idea whether DeLorme chose to use the general purpose
258 * firmware or not), if you need to modify this speed setting for your own
259 * project please add your own chiptype and modify the code likewise. The
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500260 * Cypress HID->COM device will work successfully up to 115200bps (but the
261 * actual throughput is around 3kBps).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 */
263 if (baud_mask != priv->cbr_mask) {
264 dbg("%s - baud rate is changing", __FUNCTION__);
265 if ( priv->chiptype == CT_EARTHMATE ) {
266 /* 300 and 600 baud rates are supported under the generic firmware,
267 * but are not used with NMEA and SiRF protocols */
268
269 if ( (baud_mask == B300) || (baud_mask == B600) ) {
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500270 err("%s - failed setting baud rate, unsupported speed",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 __FUNCTION__);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500272 new_baudrate = priv->baud_rate;
273 } else if ( (new_baudrate = mask_to_rate(baud_mask)) == -1) {
274 err("%s - failed setting baud rate, unsupported speed",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 __FUNCTION__);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500276 new_baudrate = priv->baud_rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 }
278 } else if (priv->chiptype == CT_CYPHIDCOM) {
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500279 if ( (new_baudrate = mask_to_rate(baud_mask)) == -1) {
280 err("%s - failed setting baud rate, unsupported speed",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 __FUNCTION__);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500282 new_baudrate = priv->baud_rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 }
284 } else if (priv->chiptype == CT_GENERIC) {
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500285 if ( (new_baudrate = mask_to_rate(baud_mask)) == -1) {
286 err("%s - failed setting baud rate, unsupported speed",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 __FUNCTION__);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500288 new_baudrate = priv->baud_rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 }
290 } else {
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500291 info("%s - please define your chiptype", __FUNCTION__);
292 new_baudrate = priv->baud_rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 }
294 } else { /* baud rate not changing, keep the old */
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500295 new_baudrate = priv->baud_rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 }
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500297 dbg("%s - baud rate is being sent as %d", __FUNCTION__, new_baudrate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500299 memset(feature_buffer, 0, 8);
300 /* fill the feature_buffer with new configuration */
301 *((u_int32_t *)feature_buffer) = new_baudrate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500303 feature_buffer[4] |= data_bits; /* assign data bits in 2 bit space ( max 3 ) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 /* 1 bit gap */
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500305 feature_buffer[4] |= (stop_bits << 3); /* assign stop bits in 1 bit space */
306 feature_buffer[4] |= (parity_enable << 4); /* assign parity flag in 1 bit space */
307 feature_buffer[4] |= (parity_type << 5); /* assign parity type in 1 bit space */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 /* 1 bit gap */
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500309 feature_buffer[4] |= (reset << 7); /* assign reset at end of byte, 1 bit space */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
311 dbg("%s - device is being sent this feature report:", __FUNCTION__);
312 dbg("%s - %02X - %02X - %02X - %02X - %02X", __FUNCTION__, feature_buffer[0], feature_buffer[1],
313 feature_buffer[2], feature_buffer[3], feature_buffer[4]);
314
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500315 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 retval = usb_control_msg (port->serial->dev, usb_sndctrlpipe(port->serial->dev, 0),
317 HID_REQ_SET_REPORT, USB_DIR_OUT | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500318 0x0300, 0, feature_buffer, 8, 500);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500320 if (tries++ >= 3)
321 break;
322
323 if (retval == EPIPE)
324 usb_clear_halt(port->serial->dev, 0x00);
325 } while (retval != 8 && retval != ENODEV);
326
327 if (retval != 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 err("%s - failed sending serial line settings - %d", __FUNCTION__, retval);
329 else {
330 spin_lock_irqsave(&priv->lock, flags);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500331 priv->baud_rate = new_baudrate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 priv->cbr_mask = baud_mask;
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500333 priv->current_config = feature_buffer[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 spin_unlock_irqrestore(&priv->lock, flags);
335 }
336 break;
337 case CYPRESS_GET_CONFIG:
338 dbg("%s - retreiving serial line settings", __FUNCTION__);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500339 /* set initial values in feature buffer */
340 memset(feature_buffer, 0, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500342 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 retval = usb_control_msg (port->serial->dev, usb_rcvctrlpipe(port->serial->dev, 0),
344 HID_REQ_GET_REPORT, USB_DIR_IN | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500345 0x0300, 0, feature_buffer, 8, 500);
346
347 if (tries++ >= 3)
348 break;
349
350 if (retval == EPIPE)
351 usb_clear_halt(port->serial->dev, 0x00);
352 } while (retval != 5 && retval != ENODEV);
353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 if (retval != 5) {
355 err("%s - failed to retreive serial line settings - %d", __FUNCTION__, retval);
356 return retval;
357 } else {
358 spin_lock_irqsave(&priv->lock, flags);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 /* store the config in one byte, and later use bit masks to check values */
361 priv->current_config = feature_buffer[4];
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500362 priv->baud_rate = *((u_int32_t *)feature_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500364 if ( (priv->cbr_mask = rate_to_mask(priv->baud_rate)) == 0x40)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 dbg("%s - failed setting the baud mask (not defined)", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 spin_unlock_irqrestore(&priv->lock, flags);
367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 }
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500369 spin_lock_irqsave(&priv->lock, flags);
370 ++priv->cmd_count;
371 spin_unlock_irqrestore(&priv->lock, flags);
372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 return retval;
374} /* cypress_serial_control */
375
376
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500377/* given a baud mask, it will return integer baud on success */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378static int mask_to_rate (unsigned mask)
379{
380 int rate;
381
382 switch (mask) {
383 case B0: rate = 0; break;
384 case B300: rate = 300; break;
385 case B600: rate = 600; break;
386 case B1200: rate = 1200; break;
387 case B2400: rate = 2400; break;
388 case B4800: rate = 4800; break;
389 case B9600: rate = 9600; break;
390 case B19200: rate = 19200; break;
391 case B38400: rate = 38400; break;
392 case B57600: rate = 57600; break;
393 case B115200: rate = 115200; break;
394 default: rate = -1;
395 }
396
397 return rate;
398}
399
400
401static unsigned rate_to_mask (int rate)
402{
403 unsigned mask;
404
405 switch (rate) {
406 case 0: mask = B0; break;
407 case 300: mask = B300; break;
408 case 600: mask = B600; break;
409 case 1200: mask = B1200; break;
410 case 2400: mask = B2400; break;
411 case 4800: mask = B4800; break;
412 case 9600: mask = B9600; break;
413 case 19200: mask = B19200; break;
414 case 38400: mask = B38400; break;
415 case 57600: mask = B57600; break;
416 case 115200: mask = B115200; break;
417 default: mask = 0x40;
418 }
419
420 return mask;
421}
422/*****************************************************************************
423 * Cypress serial driver functions
424 *****************************************************************************/
425
426
427static int generic_startup (struct usb_serial *serial)
428{
429 struct cypress_private *priv;
430
431 dbg("%s - port %d", __FUNCTION__, serial->port[0]->number);
432
433 priv = kmalloc(sizeof (struct cypress_private), GFP_KERNEL);
434 if (!priv)
435 return -ENOMEM;
436
437 memset(priv, 0x00, sizeof (struct cypress_private));
438 spin_lock_init(&priv->lock);
439 priv->buf = cypress_buf_alloc(CYPRESS_BUF_SIZE);
440 if (priv->buf == NULL) {
441 kfree(priv);
442 return -ENOMEM;
443 }
444 init_waitqueue_head(&priv->delta_msr_wait);
445
446 usb_reset_configuration (serial->dev);
447
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500448 interval = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 priv->cmd_ctrl = 0;
450 priv->line_control = 0;
451 priv->termios_initialized = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 priv->rx_flags = 0;
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500453 priv->cbr_mask = B300;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 usb_set_serial_port_data(serial->port[0], priv);
455
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500456 return 0;
457}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
459
460static int cypress_earthmate_startup (struct usb_serial *serial)
461{
462 struct cypress_private *priv;
463
464 dbg("%s", __FUNCTION__);
465
466 if (generic_startup(serial)) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500467 dbg("%s - Failed setting up port %d", __FUNCTION__,
468 serial->port[0]->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 return 1;
470 }
471
472 priv = usb_get_serial_port_data(serial->port[0]);
473 priv->chiptype = CT_EARTHMATE;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500474
475 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476} /* cypress_earthmate_startup */
477
478
479static int cypress_hidcom_startup (struct usb_serial *serial)
480{
481 struct cypress_private *priv;
482
483 dbg("%s", __FUNCTION__);
484
485 if (generic_startup(serial)) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500486 dbg("%s - Failed setting up port %d", __FUNCTION__,
487 serial->port[0]->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 return 1;
489 }
490
491 priv = usb_get_serial_port_data(serial->port[0]);
492 priv->chiptype = CT_CYPHIDCOM;
493
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500494 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495} /* cypress_hidcom_startup */
496
497
498static void cypress_shutdown (struct usb_serial *serial)
499{
500 struct cypress_private *priv;
501
502 dbg ("%s - port %d", __FUNCTION__, serial->port[0]->number);
503
504 /* all open ports are closed at this point */
505
506 priv = usb_get_serial_port_data(serial->port[0]);
507
508 if (priv) {
509 cypress_buf_free(priv->buf);
510 kfree(priv);
511 usb_set_serial_port_data(serial->port[0], NULL);
512 }
513}
514
515
516static int cypress_open (struct usb_serial_port *port, struct file *filp)
517{
518 struct cypress_private *priv = usb_get_serial_port_data(port);
519 struct usb_serial *serial = port->serial;
520 unsigned long flags;
521 int result = 0;
522
523 dbg("%s - port %d", __FUNCTION__, port->number);
524
525 /* clear halts before open */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 usb_clear_halt(serial->dev, 0x81);
527 usb_clear_halt(serial->dev, 0x02);
528
529 spin_lock_irqsave(&priv->lock, flags);
530 /* reset read/write statistics */
531 priv->bytes_in = 0;
532 priv->bytes_out = 0;
533 priv->cmd_count = 0;
534 priv->rx_flags = 0;
535 spin_unlock_irqrestore(&priv->lock, flags);
536
537 /* setting to zero could cause data loss */
538 port->tty->low_latency = 1;
539
540 /* raise both lines and set termios */
541 spin_lock_irqsave(&priv->lock, flags);
542 priv->line_control = CONTROL_DTR | CONTROL_RTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 priv->cmd_ctrl = 1;
544 spin_unlock_irqrestore(&priv->lock, flags);
545 result = cypress_write(port, NULL, 0);
546
547 if (result) {
548 dev_err(&port->dev, "%s - failed setting the control lines - error %d\n", __FUNCTION__, result);
549 return result;
550 } else
551 dbg("%s - success setting the control lines", __FUNCTION__);
552
553 cypress_set_termios(port, &priv->tmp_termios);
554
555 /* setup the port and start reading from the device */
556 if(!port->interrupt_in_urb){
557 err("%s - interrupt_in_urb is empty!", __FUNCTION__);
558 return(-1);
559 }
560
561 usb_fill_int_urb(port->interrupt_in_urb, serial->dev,
562 usb_rcvintpipe(serial->dev, port->interrupt_in_endpointAddress),
563 port->interrupt_in_urb->transfer_buffer, port->interrupt_in_urb->transfer_buffer_length,
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500564 cypress_read_int_callback, port, interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
566
567 if (result){
568 dev_err(&port->dev, "%s - failed submitting read urb, error %d\n", __FUNCTION__, result);
569 }
570
571 return result;
572} /* cypress_open */
573
574
575static void cypress_close(struct usb_serial_port *port, struct file * filp)
576{
577 struct cypress_private *priv = usb_get_serial_port_data(port);
578 unsigned int c_cflag;
579 unsigned long flags;
580 int bps;
581 long timeout;
582 wait_queue_t wait;
583
584 dbg("%s - port %d", __FUNCTION__, port->number);
585
586 /* wait for data to drain from buffer */
587 spin_lock_irqsave(&priv->lock, flags);
588 timeout = CYPRESS_CLOSING_WAIT;
589 init_waitqueue_entry(&wait, current);
590 add_wait_queue(&port->tty->write_wait, &wait);
591 for (;;) {
592 set_current_state(TASK_INTERRUPTIBLE);
593 if (cypress_buf_data_avail(priv->buf) == 0
594 || timeout == 0 || signal_pending(current)
595 || !usb_get_intfdata(port->serial->interface))
596 break;
597 spin_unlock_irqrestore(&priv->lock, flags);
598 timeout = schedule_timeout(timeout);
599 spin_lock_irqsave(&priv->lock, flags);
600 }
601 set_current_state(TASK_RUNNING);
602 remove_wait_queue(&port->tty->write_wait, &wait);
603 /* clear out any remaining data in the buffer */
604 cypress_buf_clear(priv->buf);
605 spin_unlock_irqrestore(&priv->lock, flags);
606
607 /* wait for characters to drain from device */
608 bps = tty_get_baud_rate(port->tty);
609 if (bps > 1200)
610 timeout = max((HZ*2560)/bps,HZ/10);
611 else
612 timeout = 2*HZ;
Nishanth Aravamudan22c43862005-08-15 11:30:11 -0700613 schedule_timeout_interruptible(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615 dbg("%s - stopping urbs", __FUNCTION__);
616 usb_kill_urb (port->interrupt_in_urb);
617 usb_kill_urb (port->interrupt_out_urb);
618
619 if (port->tty) {
620 c_cflag = port->tty->termios->c_cflag;
621 if (c_cflag & HUPCL) {
622 /* drop dtr and rts */
623 priv = usb_get_serial_port_data(port);
624 spin_lock_irqsave(&priv->lock, flags);
625 priv->line_control = 0;
626 priv->cmd_ctrl = 1;
627 spin_unlock_irqrestore(&priv->lock, flags);
628 cypress_write(port, NULL, 0);
629 }
630 }
631
632 if (stats)
633 dev_info (&port->dev, "Statistics: %d Bytes In | %d Bytes Out | %d Commands Issued\n",
634 priv->bytes_in, priv->bytes_out, priv->cmd_count);
635} /* cypress_close */
636
637
638static int cypress_write(struct usb_serial_port *port, const unsigned char *buf, int count)
639{
640 struct cypress_private *priv = usb_get_serial_port_data(port);
641 unsigned long flags;
642
643 dbg("%s - port %d, %d bytes", __FUNCTION__, port->number, count);
644
645 /* line control commands, which need to be executed immediately,
646 are not put into the buffer for obvious reasons.
647 */
648 if (priv->cmd_ctrl) {
649 count = 0;
650 goto finish;
651 }
652
653 if (!count)
654 return count;
655
656 spin_lock_irqsave(&priv->lock, flags);
657 count = cypress_buf_put(priv->buf, buf, count);
658 spin_unlock_irqrestore(&priv->lock, flags);
659
660finish:
661 cypress_send(port);
662
663 return count;
664} /* cypress_write */
665
666
667static void cypress_send(struct usb_serial_port *port)
668{
669 int count = 0, result, offset, actual_size;
670 struct cypress_private *priv = usb_get_serial_port_data(port);
671 unsigned long flags;
672
673 dbg("%s - port %d", __FUNCTION__, port->number);
674 dbg("%s - interrupt out size is %d", __FUNCTION__, port->interrupt_out_size);
675
676 spin_lock_irqsave(&priv->lock, flags);
677 if (priv->write_urb_in_use) {
678 dbg("%s - can't write, urb in use", __FUNCTION__);
679 spin_unlock_irqrestore(&priv->lock, flags);
680 return;
681 }
682 spin_unlock_irqrestore(&priv->lock, flags);
683
684 /* clear buffer */
685 memset(port->interrupt_out_urb->transfer_buffer, 0, port->interrupt_out_size);
686
687 spin_lock_irqsave(&priv->lock, flags);
688 switch (port->interrupt_out_size) {
689 case 32:
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500690 /* this is for the CY7C64013... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 offset = 2;
692 port->interrupt_out_buffer[0] = priv->line_control;
693 break;
694 case 8:
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500695 /* this is for the CY7C63743... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 offset = 1;
697 port->interrupt_out_buffer[0] = priv->line_control;
698 break;
699 default:
700 dbg("%s - wrong packet size", __FUNCTION__);
701 spin_unlock_irqrestore(&priv->lock, flags);
702 return;
703 }
704
705 if (priv->line_control & CONTROL_RESET)
706 priv->line_control &= ~CONTROL_RESET;
707
708 if (priv->cmd_ctrl) {
709 priv->cmd_count++;
710 dbg("%s - line control command being issued", __FUNCTION__);
711 spin_unlock_irqrestore(&priv->lock, flags);
712 goto send;
713 } else
714 spin_unlock_irqrestore(&priv->lock, flags);
715
716 count = cypress_buf_get(priv->buf, &port->interrupt_out_buffer[offset],
717 port->interrupt_out_size-offset);
718
719 if (count == 0) {
720 return;
721 }
722
723 switch (port->interrupt_out_size) {
724 case 32:
725 port->interrupt_out_buffer[1] = count;
726 break;
727 case 8:
728 port->interrupt_out_buffer[0] |= count;
729 }
730
731 dbg("%s - count is %d", __FUNCTION__, count);
732
733send:
734 spin_lock_irqsave(&priv->lock, flags);
735 priv->write_urb_in_use = 1;
736 spin_unlock_irqrestore(&priv->lock, flags);
737
738 if (priv->cmd_ctrl)
739 actual_size = 1;
740 else
741 actual_size = count + (port->interrupt_out_size == 32 ? 2 : 1);
742
743 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, port->interrupt_out_size,
744 port->interrupt_out_urb->transfer_buffer);
745
746 port->interrupt_out_urb->transfer_buffer_length = actual_size;
747 port->interrupt_out_urb->dev = port->serial->dev;
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500748 port->interrupt_out_urb->interval = interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 result = usb_submit_urb (port->interrupt_out_urb, GFP_ATOMIC);
750 if (result) {
751 dev_err(&port->dev, "%s - failed submitting write urb, error %d\n", __FUNCTION__,
752 result);
753 priv->write_urb_in_use = 0;
754 }
755
756 spin_lock_irqsave(&priv->lock, flags);
757 if (priv->cmd_ctrl) {
758 priv->cmd_ctrl = 0;
759 }
760 priv->bytes_out += count; /* do not count the line control and size bytes */
761 spin_unlock_irqrestore(&priv->lock, flags);
762
763 schedule_work(&port->work);
764} /* cypress_send */
765
766
767/* returns how much space is available in the soft buffer */
768static int cypress_write_room(struct usb_serial_port *port)
769{
770 struct cypress_private *priv = usb_get_serial_port_data(port);
771 int room = 0;
772 unsigned long flags;
773
774 dbg("%s - port %d", __FUNCTION__, port->number);
775
776 spin_lock_irqsave(&priv->lock, flags);
777 room = cypress_buf_space_avail(priv->buf);
778 spin_unlock_irqrestore(&priv->lock, flags);
779
780 dbg("%s - returns %d", __FUNCTION__, room);
781 return room;
782}
783
784
785static int cypress_tiocmget (struct usb_serial_port *port, struct file *file)
786{
787 struct cypress_private *priv = usb_get_serial_port_data(port);
788 __u8 status, control;
789 unsigned int result = 0;
790 unsigned long flags;
791
792 dbg("%s - port %d", __FUNCTION__, port->number);
793
794 spin_lock_irqsave(&priv->lock, flags);
795 control = priv->line_control;
796 status = priv->current_status;
797 spin_unlock_irqrestore(&priv->lock, flags);
798
799 result = ((control & CONTROL_DTR) ? TIOCM_DTR : 0)
800 | ((control & CONTROL_RTS) ? TIOCM_RTS : 0)
801 | ((status & UART_CTS) ? TIOCM_CTS : 0)
802 | ((status & UART_DSR) ? TIOCM_DSR : 0)
803 | ((status & UART_RI) ? TIOCM_RI : 0)
804 | ((status & UART_CD) ? TIOCM_CD : 0);
805
806 dbg("%s - result = %x", __FUNCTION__, result);
807
808 return result;
809}
810
811
812static int cypress_tiocmset (struct usb_serial_port *port, struct file *file,
813 unsigned int set, unsigned int clear)
814{
815 struct cypress_private *priv = usb_get_serial_port_data(port);
816 unsigned long flags;
817
818 dbg("%s - port %d", __FUNCTION__, port->number);
819
820 spin_lock_irqsave(&priv->lock, flags);
821 if (set & TIOCM_RTS)
822 priv->line_control |= CONTROL_RTS;
823 if (set & TIOCM_DTR)
824 priv->line_control |= CONTROL_DTR;
825 if (clear & TIOCM_RTS)
826 priv->line_control &= ~CONTROL_RTS;
827 if (clear & TIOCM_DTR)
828 priv->line_control &= ~CONTROL_DTR;
829 spin_unlock_irqrestore(&priv->lock, flags);
830
831 priv->cmd_ctrl = 1;
832 return cypress_write(port, NULL, 0);
833}
834
835
836static int cypress_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg)
837{
838 struct cypress_private *priv = usb_get_serial_port_data(port);
839
840 dbg("%s - port %d, cmd 0x%.4x", __FUNCTION__, port->number, cmd);
841
842 switch (cmd) {
843 case TIOCGSERIAL:
844 if (copy_to_user((void __user *)arg, port->tty->termios, sizeof(struct termios))) {
845 return -EFAULT;
846 }
847 return (0);
848 break;
849 case TIOCSSERIAL:
850 if (copy_from_user(port->tty->termios, (void __user *)arg, sizeof(struct termios))) {
851 return -EFAULT;
852 }
853 /* here we need to call cypress_set_termios to invoke the new settings */
854 cypress_set_termios(port, &priv->tmp_termios);
855 return (0);
856 break;
857 /* these are called when setting baud rate from gpsd */
858 case TCGETS:
859 if (copy_to_user((void __user *)arg, port->tty->termios, sizeof(struct termios))) {
860 return -EFAULT;
861 }
862 return (0);
863 break;
864 case TCSETS:
865 if (copy_from_user(port->tty->termios, (void __user *)arg, sizeof(struct termios))) {
866 return -EFAULT;
867 }
868 /* here we need to call cypress_set_termios to invoke the new settings */
869 cypress_set_termios(port, &priv->tmp_termios);
870 return (0);
871 break;
872 /* This code comes from drivers/char/serial.c and ftdi_sio.c */
873 case TIOCMIWAIT:
874 while (priv != NULL) {
875 interruptible_sleep_on(&priv->delta_msr_wait);
876 /* see if a signal did it */
877 if (signal_pending(current))
878 return -ERESTARTSYS;
879 else {
880 char diff = priv->diff_status;
881
882 if (diff == 0) {
883 return -EIO; /* no change => error */
884 }
885
886 /* consume all events */
887 priv->diff_status = 0;
888
889 /* return 0 if caller wanted to know about these bits */
890 if ( ((arg & TIOCM_RNG) && (diff & UART_RI)) ||
891 ((arg & TIOCM_DSR) && (diff & UART_DSR)) ||
892 ((arg & TIOCM_CD) && (diff & UART_CD)) ||
893 ((arg & TIOCM_CTS) && (diff & UART_CTS)) ) {
894 return 0;
895 }
896 /* otherwise caller can't care less about what happened,
897 * and so we continue to wait for more events.
898 */
899 }
900 }
901 return 0;
902 break;
903 default:
904 break;
905 }
906
907 dbg("%s - arg not supported - it was 0x%04x - check include/asm/ioctls.h", __FUNCTION__, cmd);
908
909 return -ENOIOCTLCMD;
910} /* cypress_ioctl */
911
912
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500913static void cypress_set_termios (struct usb_serial_port *port,
914 struct termios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915{
916 struct cypress_private *priv = usb_get_serial_port_data(port);
917 struct tty_struct *tty;
918 int data_bits, stop_bits, parity_type, parity_enable;
919 unsigned cflag, iflag, baud_mask;
920 unsigned long flags;
921 __u8 oldlines;
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500922 int linechange = 0;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500923
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 dbg("%s - port %d", __FUNCTION__, port->number);
925
926 tty = port->tty;
927 if ((!tty) || (!tty->termios)) {
928 dbg("%s - no tty structures", __FUNCTION__);
929 return;
930 }
931
932 spin_lock_irqsave(&priv->lock, flags);
933 if (!priv->termios_initialized) {
934 if (priv->chiptype == CT_EARTHMATE) {
935 *(tty->termios) = tty_std_termios;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500936 tty->termios->c_cflag = B4800 | CS8 | CREAD | HUPCL |
937 CLOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 } else if (priv->chiptype == CT_CYPHIDCOM) {
939 *(tty->termios) = tty_std_termios;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500940 tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
941 CLOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 }
943 priv->termios_initialized = 1;
944 }
945 spin_unlock_irqrestore(&priv->lock, flags);
946
947 cflag = tty->termios->c_cflag;
948 iflag = tty->termios->c_iflag;
949
950 /* check if there are new settings */
951 if (old_termios) {
952 if ((cflag != old_termios->c_cflag) ||
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500953 (RELEVANT_IFLAG(iflag) !=
954 RELEVANT_IFLAG(old_termios->c_iflag))) {
955 dbg("%s - attempting to set new termios settings",
956 __FUNCTION__);
957 /* should make a copy of this in case something goes
958 * wrong in the function, we can restore it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 spin_lock_irqsave(&priv->lock, flags);
960 priv->tmp_termios = *(tty->termios);
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500961 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 } else {
963 dbg("%s - nothing to do, exiting", __FUNCTION__);
964 return;
965 }
966 } else
967 return;
968
969 /* set number of data bits, parity, stop bits */
970 /* when parity is disabled the parity type bit is ignored */
971
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500972 /* 1 means 2 stop bits, 0 means 1 stop bit */
973 stop_bits = cflag & CSTOPB ? 1 : 0;
974
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 if (cflag & PARENB) {
976 parity_enable = 1;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500977 /* 1 means odd parity, 0 means even parity */
978 parity_type = cflag & PARODD ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 } else
980 parity_enable = parity_type = 0;
981
982 if (cflag & CSIZE) {
983 switch (cflag & CSIZE) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500984 case CS5:
985 data_bits = 0;
986 break;
987 case CS6:
988 data_bits = 1;
989 break;
990 case CS7:
991 data_bits = 2;
992 break;
993 case CS8:
994 data_bits = 3;
995 break;
996 default:
997 err("%s - CSIZE was set, but not CS5-CS8",
998 __FUNCTION__);
999 data_bits = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 }
1001 } else
1002 data_bits = 3;
1003
1004 spin_lock_irqsave(&priv->lock, flags);
1005 oldlines = priv->line_control;
1006 if ((cflag & CBAUD) == B0) {
1007 /* drop dtr and rts */
1008 dbg("%s - dropping the lines, baud rate 0bps", __FUNCTION__);
1009 baud_mask = B0;
1010 priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
1011 } else {
1012 baud_mask = (cflag & CBAUD);
1013 switch(baud_mask) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001014 case B300:
1015 dbg("%s - setting baud 300bps", __FUNCTION__);
1016 break;
1017 case B600:
1018 dbg("%s - setting baud 600bps", __FUNCTION__);
1019 break;
1020 case B1200:
1021 dbg("%s - setting baud 1200bps", __FUNCTION__);
1022 break;
1023 case B2400:
1024 dbg("%s - setting baud 2400bps", __FUNCTION__);
1025 break;
1026 case B4800:
1027 dbg("%s - setting baud 4800bps", __FUNCTION__);
1028 break;
1029 case B9600:
1030 dbg("%s - setting baud 9600bps", __FUNCTION__);
1031 break;
1032 case B19200:
1033 dbg("%s - setting baud 19200bps", __FUNCTION__);
1034 break;
1035 case B38400:
1036 dbg("%s - setting baud 38400bps", __FUNCTION__);
1037 break;
1038 case B57600:
1039 dbg("%s - setting baud 57600bps", __FUNCTION__);
1040 break;
1041 case B115200:
1042 dbg("%s - setting baud 115200bps", __FUNCTION__);
1043 break;
1044 default:
1045 dbg("%s - unknown masked baud rate", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 }
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001047 priv->line_control = (CONTROL_DTR | CONTROL_RTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 }
1049 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001051 dbg("%s - sending %d stop_bits, %d parity_enable, %d parity_type, "
1052 "%d data_bits (+5)", __FUNCTION__, stop_bits,
1053 parity_enable, parity_type, data_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001055 cypress_serial_control(port, baud_mask, data_bits, stop_bits,
1056 parity_enable, parity_type, 0, CYPRESS_SET_CONFIG);
1057
1058 /* we perform a CYPRESS_GET_CONFIG so that the current settings are
1059 * filled into the private structure this should confirm that all is
1060 * working if it returns what we just set */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 cypress_serial_control(port, 0, 0, 0, 0, 0, 0, CYPRESS_GET_CONFIG);
1062
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001063 /* Here we can define custom tty settings for devices; the main tty
1064 * termios flag base comes from empeg.c */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001066 spin_lock_irqsave(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 if ( (priv->chiptype == CT_EARTHMATE) && (priv->baud_rate == 4800) ) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001068 dbg("Using custom termios settings for a baud rate of "
1069 "4800bps.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 /* define custom termios settings for NMEA protocol */
1071
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 tty->termios->c_iflag /* input modes - */
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001073 &= ~(IGNBRK /* disable ignore break */
1074 | BRKINT /* disable break causes interrupt */
1075 | PARMRK /* disable mark parity errors */
1076 | ISTRIP /* disable clear high bit of input char */
1077 | INLCR /* disable translate NL to CR */
1078 | IGNCR /* disable ignore CR */
1079 | ICRNL /* disable translate CR to NL */
1080 | IXON); /* disable enable XON/XOFF flow control */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001082 tty->termios->c_oflag /* output modes */
1083 &= ~OPOST; /* disable postprocess output char */
1084
1085 tty->termios->c_lflag /* line discipline modes */
1086 &= ~(ECHO /* disable echo input characters */
1087 | ECHONL /* disable echo new line */
1088 | ICANON /* disable erase, kill, werase, and rprnt
1089 special characters */
1090 | ISIG /* disable interrupt, quit, and suspend
1091 special characters */
1092 | IEXTEN); /* disable non-POSIX special characters */
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001093 } /* CT_CYPHIDCOM: Application should handle this for device */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 linechange = (priv->line_control != oldlines);
1096 spin_unlock_irqrestore(&priv->lock, flags);
1097
1098 /* if necessary, set lines */
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001099 if (linechange) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 priv->cmd_ctrl = 1;
1101 cypress_write(port, NULL, 0);
1102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103} /* cypress_set_termios */
1104
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001105
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106/* returns amount of data still left in soft buffer */
1107static int cypress_chars_in_buffer(struct usb_serial_port *port)
1108{
1109 struct cypress_private *priv = usb_get_serial_port_data(port);
1110 int chars = 0;
1111 unsigned long flags;
1112
1113 dbg("%s - port %d", __FUNCTION__, port->number);
1114
1115 spin_lock_irqsave(&priv->lock, flags);
1116 chars = cypress_buf_data_avail(priv->buf);
1117 spin_unlock_irqrestore(&priv->lock, flags);
1118
1119 dbg("%s - returns %d", __FUNCTION__, chars);
1120 return chars;
1121}
1122
1123
1124static void cypress_throttle (struct usb_serial_port *port)
1125{
1126 struct cypress_private *priv = usb_get_serial_port_data(port);
1127 unsigned long flags;
1128
1129 dbg("%s - port %d", __FUNCTION__, port->number);
1130
1131 spin_lock_irqsave(&priv->lock, flags);
1132 priv->rx_flags = THROTTLED;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001133 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134}
1135
1136
1137static void cypress_unthrottle (struct usb_serial_port *port)
1138{
1139 struct cypress_private *priv = usb_get_serial_port_data(port);
1140 int actually_throttled, result;
1141 unsigned long flags;
1142
1143 dbg("%s - port %d", __FUNCTION__, port->number);
1144
1145 spin_lock_irqsave(&priv->lock, flags);
1146 actually_throttled = priv->rx_flags & ACTUALLY_THROTTLED;
1147 priv->rx_flags = 0;
1148 spin_unlock_irqrestore(&priv->lock, flags);
1149
1150 if (actually_throttled) {
1151 port->interrupt_in_urb->dev = port->serial->dev;
1152
1153 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
1154 if (result)
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001155 dev_err(&port->dev, "%s - failed submitting read urb, "
1156 "error %d\n", __FUNCTION__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 }
1158}
1159
1160
1161static void cypress_read_int_callback(struct urb *urb, struct pt_regs *regs)
1162{
1163 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
1164 struct cypress_private *priv = usb_get_serial_port_data(port);
1165 struct tty_struct *tty;
1166 unsigned char *data = urb->transfer_buffer;
1167 unsigned long flags;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001168 char tty_flag = TTY_NORMAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 int havedata = 0;
1170 int bytes = 0;
1171 int result;
1172 int i = 0;
1173
1174 dbg("%s - port %d", __FUNCTION__, port->number);
1175
1176 if (urb->status) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001177 dbg("%s - nonzero read status received: %d", __FUNCTION__,
1178 urb->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 return;
1180 }
1181
1182 spin_lock_irqsave(&priv->lock, flags);
1183 if (priv->rx_flags & THROTTLED) {
1184 dbg("%s - now throttling", __FUNCTION__);
1185 priv->rx_flags |= ACTUALLY_THROTTLED;
1186 spin_unlock_irqrestore(&priv->lock, flags);
1187 return;
1188 }
1189 spin_unlock_irqrestore(&priv->lock, flags);
1190
1191 tty = port->tty;
1192 if (!tty) {
1193 dbg("%s - bad tty pointer - exiting", __FUNCTION__);
1194 return;
1195 }
1196
1197 spin_lock_irqsave(&priv->lock, flags);
1198 switch(urb->actual_length) {
1199 case 32:
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001200 /* This is for the CY7C64013... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 priv->current_status = data[0] & 0xF8;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001202 bytes = data[1] + 2;
1203 i = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 if (bytes > 2)
1205 havedata = 1;
1206 break;
1207 case 8:
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001208 /* This is for the CY7C63743... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 priv->current_status = data[0] & 0xF8;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001210 bytes = (data[0] & 0x07) + 1;
1211 i = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 if (bytes > 1)
1213 havedata = 1;
1214 break;
1215 default:
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001216 dbg("%s - wrong packet size - received %d bytes",
1217 __FUNCTION__, urb->actual_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 spin_unlock_irqrestore(&priv->lock, flags);
1219 goto continue_read;
1220 }
1221 spin_unlock_irqrestore(&priv->lock, flags);
1222
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001223 usb_serial_debug_data (debug, &port->dev, __FUNCTION__,
1224 urb->actual_length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
1226 spin_lock_irqsave(&priv->lock, flags);
1227 /* check to see if status has changed */
1228 if (priv != NULL) {
1229 if (priv->current_status != priv->prev_status) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001230 priv->diff_status |= priv->current_status ^
1231 priv->prev_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 wake_up_interruptible(&priv->delta_msr_wait);
1233 priv->prev_status = priv->current_status;
1234 }
1235 }
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001236 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001238 /* hangup, as defined in acm.c... this might be a bad place for it
1239 * though */
1240 if (tty && !(tty->termios->c_cflag & CLOCAL) &&
1241 !(priv->current_status & UART_CD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 dbg("%s - calling hangup", __FUNCTION__);
1243 tty_hangup(tty);
1244 goto continue_read;
1245 }
1246
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001247 /* There is one error bit... I'm assuming it is a parity error
1248 * indicator as the generic firmware will set this bit to 1 if a
1249 * parity error occurs.
1250 * I can not find reference to any other error events. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 spin_lock_irqsave(&priv->lock, flags);
1252 if (priv->current_status & CYP_ERROR) {
1253 spin_unlock_irqrestore(&priv->lock, flags);
1254 tty_flag = TTY_PARITY;
1255 dbg("%s - Parity Error detected", __FUNCTION__);
1256 } else
1257 spin_unlock_irqrestore(&priv->lock, flags);
1258
1259 /* process read if there is data other than line status */
1260 if (tty && (bytes > i)) {
1261 for (; i < bytes ; ++i) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001262 dbg("pushing byte number %d - %d - %c", i, data[i],
1263 data[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
1265 tty_flip_buffer_push(tty);
1266 }
1267 tty_insert_flip_char(tty, data[i], tty_flag);
1268 }
1269 tty_flip_buffer_push(port->tty);
1270 }
1271
1272 spin_lock_irqsave(&priv->lock, flags);
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001273 /* control and status byte(s) are also counted */
1274 priv->bytes_in += bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 spin_unlock_irqrestore(&priv->lock, flags);
1276
1277continue_read:
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001278
1279 /* Continue trying to always read... unless the port has closed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
1281 if (port->open_count > 0) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001282 usb_fill_int_urb(port->interrupt_in_urb, port->serial->dev,
1283 usb_rcvintpipe(port->serial->dev,
1284 port->interrupt_in_endpointAddress),
1285 port->interrupt_in_urb->transfer_buffer,
1286 port->interrupt_in_urb->transfer_buffer_length,
1287 cypress_read_int_callback, port, interval);
1288 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
1289 if (result)
1290 dev_err(&urb->dev->dev, "%s - failed resubmitting "
1291 "read urb, error %d\n", __FUNCTION__,
1292 result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 }
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001294
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 return;
1296} /* cypress_read_int_callback */
1297
1298
1299static void cypress_write_int_callback(struct urb *urb, struct pt_regs *regs)
1300{
1301 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
1302 struct cypress_private *priv = usb_get_serial_port_data(port);
1303 int result;
1304
1305 dbg("%s - port %d", __FUNCTION__, port->number);
1306
1307 switch (urb->status) {
1308 case 0:
1309 /* success */
1310 break;
1311 case -ECONNRESET:
1312 case -ENOENT:
1313 case -ESHUTDOWN:
1314 /* this urb is terminated, clean up */
1315 dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status);
1316 priv->write_urb_in_use = 0;
1317 return;
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001318 case -EPIPE: /* no break needed */
1319 usb_clear_halt(port->serial->dev, 0x02);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 default:
1321 /* error in the urb, so we have to resubmit it */
1322 dbg("%s - Overflow in write", __FUNCTION__);
1323 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
1324 port->interrupt_out_urb->transfer_buffer_length = 1;
1325 port->interrupt_out_urb->dev = port->serial->dev;
1326 result = usb_submit_urb(port->interrupt_out_urb, GFP_ATOMIC);
1327 if (result)
1328 dev_err(&urb->dev->dev, "%s - failed resubmitting write urb, error %d\n",
1329 __FUNCTION__, result);
1330 else
1331 return;
1332 }
1333
1334 priv->write_urb_in_use = 0;
1335
1336 /* send any buffered data */
1337 cypress_send(port);
1338}
1339
1340
1341/*****************************************************************************
1342 * Write buffer functions - buffering code from pl2303 used
1343 *****************************************************************************/
1344
1345/*
1346 * cypress_buf_alloc
1347 *
1348 * Allocate a circular buffer and all associated memory.
1349 */
1350
1351static struct cypress_buf *cypress_buf_alloc(unsigned int size)
1352{
1353
1354 struct cypress_buf *cb;
1355
1356
1357 if (size == 0)
1358 return NULL;
1359
1360 cb = (struct cypress_buf *)kmalloc(sizeof(struct cypress_buf), GFP_KERNEL);
1361 if (cb == NULL)
1362 return NULL;
1363
1364 cb->buf_buf = kmalloc(size, GFP_KERNEL);
1365 if (cb->buf_buf == NULL) {
1366 kfree(cb);
1367 return NULL;
1368 }
1369
1370 cb->buf_size = size;
1371 cb->buf_get = cb->buf_put = cb->buf_buf;
1372
1373 return cb;
1374
1375}
1376
1377
1378/*
1379 * cypress_buf_free
1380 *
1381 * Free the buffer and all associated memory.
1382 */
1383
1384static void cypress_buf_free(struct cypress_buf *cb)
1385{
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001386 if (cb) {
1387 kfree(cb->buf_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 kfree(cb);
1389 }
1390}
1391
1392
1393/*
1394 * cypress_buf_clear
1395 *
1396 * Clear out all data in the circular buffer.
1397 */
1398
1399static void cypress_buf_clear(struct cypress_buf *cb)
1400{
1401 if (cb != NULL)
1402 cb->buf_get = cb->buf_put;
1403 /* equivalent to a get of all data available */
1404}
1405
1406
1407/*
1408 * cypress_buf_data_avail
1409 *
1410 * Return the number of bytes of data available in the circular
1411 * buffer.
1412 */
1413
1414static unsigned int cypress_buf_data_avail(struct cypress_buf *cb)
1415{
1416 if (cb != NULL)
1417 return ((cb->buf_size + cb->buf_put - cb->buf_get) % cb->buf_size);
1418 else
1419 return 0;
1420}
1421
1422
1423/*
1424 * cypress_buf_space_avail
1425 *
1426 * Return the number of bytes of space available in the circular
1427 * buffer.
1428 */
1429
1430static unsigned int cypress_buf_space_avail(struct cypress_buf *cb)
1431{
1432 if (cb != NULL)
1433 return ((cb->buf_size + cb->buf_get - cb->buf_put - 1) % cb->buf_size);
1434 else
1435 return 0;
1436}
1437
1438
1439/*
1440 * cypress_buf_put
1441 *
1442 * Copy data data from a user buffer and put it into the circular buffer.
1443 * Restrict to the amount of space available.
1444 *
1445 * Return the number of bytes copied.
1446 */
1447
1448static unsigned int cypress_buf_put(struct cypress_buf *cb, const char *buf,
1449 unsigned int count)
1450{
1451
1452 unsigned int len;
1453
1454
1455 if (cb == NULL)
1456 return 0;
1457
1458 len = cypress_buf_space_avail(cb);
1459 if (count > len)
1460 count = len;
1461
1462 if (count == 0)
1463 return 0;
1464
1465 len = cb->buf_buf + cb->buf_size - cb->buf_put;
1466 if (count > len) {
1467 memcpy(cb->buf_put, buf, len);
1468 memcpy(cb->buf_buf, buf+len, count - len);
1469 cb->buf_put = cb->buf_buf + count - len;
1470 } else {
1471 memcpy(cb->buf_put, buf, count);
1472 if (count < len)
1473 cb->buf_put += count;
1474 else /* count == len */
1475 cb->buf_put = cb->buf_buf;
1476 }
1477
1478 return count;
1479
1480}
1481
1482
1483/*
1484 * cypress_buf_get
1485 *
1486 * Get data from the circular buffer and copy to the given buffer.
1487 * Restrict to the amount of data available.
1488 *
1489 * Return the number of bytes copied.
1490 */
1491
1492static unsigned int cypress_buf_get(struct cypress_buf *cb, char *buf,
1493 unsigned int count)
1494{
1495
1496 unsigned int len;
1497
1498
1499 if (cb == NULL)
1500 return 0;
1501
1502 len = cypress_buf_data_avail(cb);
1503 if (count > len)
1504 count = len;
1505
1506 if (count == 0)
1507 return 0;
1508
1509 len = cb->buf_buf + cb->buf_size - cb->buf_get;
1510 if (count > len) {
1511 memcpy(buf, cb->buf_get, len);
1512 memcpy(buf+len, cb->buf_buf, count - len);
1513 cb->buf_get = cb->buf_buf + count - len;
1514 } else {
1515 memcpy(buf, cb->buf_get, count);
1516 if (count < len)
1517 cb->buf_get += count;
1518 else /* count == len */
1519 cb->buf_get = cb->buf_buf;
1520 }
1521
1522 return count;
1523
1524}
1525
1526/*****************************************************************************
1527 * Module functions
1528 *****************************************************************************/
1529
1530static int __init cypress_init(void)
1531{
1532 int retval;
1533
1534 dbg("%s", __FUNCTION__);
1535
1536 retval = usb_serial_register(&cypress_earthmate_device);
1537 if (retval)
1538 goto failed_em_register;
1539 retval = usb_serial_register(&cypress_hidcom_device);
1540 if (retval)
1541 goto failed_hidcom_register;
1542 retval = usb_register(&cypress_driver);
1543 if (retval)
1544 goto failed_usb_register;
1545
1546 info(DRIVER_DESC " " DRIVER_VERSION);
1547 return 0;
1548failed_usb_register:
1549 usb_deregister(&cypress_driver);
1550failed_hidcom_register:
1551 usb_serial_deregister(&cypress_hidcom_device);
1552failed_em_register:
1553 usb_serial_deregister(&cypress_earthmate_device);
1554
1555 return retval;
1556}
1557
1558
1559static void __exit cypress_exit (void)
1560{
1561 dbg("%s", __FUNCTION__);
1562
1563 usb_deregister (&cypress_driver);
1564 usb_serial_deregister (&cypress_earthmate_device);
1565 usb_serial_deregister (&cypress_hidcom_device);
1566}
1567
1568
1569module_init(cypress_init);
1570module_exit(cypress_exit);
1571
1572MODULE_AUTHOR( DRIVER_AUTHOR );
1573MODULE_DESCRIPTION( DRIVER_DESC );
1574MODULE_VERSION( DRIVER_VERSION );
1575MODULE_LICENSE("GPL");
1576
1577module_param(debug, bool, S_IRUGO | S_IWUSR);
1578MODULE_PARM_DESC(debug, "Debug enabled or not");
1579module_param(stats, bool, S_IRUGO | S_IWUSR);
1580MODULE_PARM_DESC(stats, "Enable statistics or not");
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001581module_param(interval, int, S_IRUGO | S_IWUSR);
1582MODULE_PARM_DESC(interval, "Overrides interrupt interval");