blob: 4e9637eb6137836b3e62a0b7eb2e39aee099cf32 [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,
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800115 .no_dynamic_id = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116};
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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139};
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);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500176static 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700180static struct usb_serial_driver cypress_earthmate_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700181 .driver = {
182 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700183 .name = "earthmate",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700184 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700185 .description = "DeLorme Earthmate USB",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 .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
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700209static struct usb_serial_driver cypress_hidcom_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700210 .driver = {
211 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700212 .name = "cyphidcom",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700213 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700214 .description = "HID->COM RS232 Adapter",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 .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
Steven Cole093cf722005-05-03 19:07:24 -0600244/* This function can either set or retrieve the current serial line settings */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245static 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{
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500248 int new_baudrate = 0, retval = 0, tries = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 struct cypress_private *priv;
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500250 __u8 feature_buffer[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 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
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500265 * Cypress HID->COM device will work successfully up to 115200bps (but the
266 * actual throughput is around 3kBps).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 */
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) ) {
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500275 err("%s - failed setting baud rate, unsupported speed",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 __FUNCTION__);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500277 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",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 __FUNCTION__);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500281 new_baudrate = priv->baud_rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 }
283 } else if (priv->chiptype == CT_CYPHIDCOM) {
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500284 if ( (new_baudrate = mask_to_rate(baud_mask)) == -1) {
285 err("%s - failed setting baud rate, unsupported speed",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 __FUNCTION__);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500287 new_baudrate = priv->baud_rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 }
289 } else if (priv->chiptype == CT_GENERIC) {
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500290 if ( (new_baudrate = mask_to_rate(baud_mask)) == -1) {
291 err("%s - failed setting baud rate, unsupported speed",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 __FUNCTION__);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500293 new_baudrate = priv->baud_rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 }
295 } else {
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500296 info("%s - please define your chiptype", __FUNCTION__);
297 new_baudrate = priv->baud_rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 }
299 } else { /* baud rate not changing, keep the old */
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500300 new_baudrate = priv->baud_rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 }
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500302 dbg("%s - baud rate is being sent as %d", __FUNCTION__, new_baudrate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500304 memset(feature_buffer, 0, 8);
305 /* fill the feature_buffer with new configuration */
306 *((u_int32_t *)feature_buffer) = new_baudrate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500308 feature_buffer[4] |= data_bits; /* assign data bits in 2 bit space ( max 3 ) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 /* 1 bit gap */
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500310 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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 /* 1 bit gap */
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500314 feature_buffer[4] |= (reset << 7); /* assign reset at end of byte, 1 bit space */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
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
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500320 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 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,
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500323 0x0300, 0, feature_buffer, 8, 500);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500325 if (tries++ >= 3)
326 break;
327
328 if (retval == EPIPE)
329 usb_clear_halt(port->serial->dev, 0x00);
330 } while (retval != 8 && retval != ENODEV);
331
332 if (retval != 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 err("%s - failed sending serial line settings - %d", __FUNCTION__, retval);
334 else {
335 spin_lock_irqsave(&priv->lock, flags);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500336 priv->baud_rate = new_baudrate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 priv->cbr_mask = baud_mask;
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500338 priv->current_config = feature_buffer[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 spin_unlock_irqrestore(&priv->lock, flags);
340 }
341 break;
342 case CYPRESS_GET_CONFIG:
343 dbg("%s - retreiving serial line settings", __FUNCTION__);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500344 /* set initial values in feature buffer */
345 memset(feature_buffer, 0, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500347 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 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,
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500350 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 if (retval != 5) {
Adrian Bunk943ffb52006-01-10 00:10:13 +0100360 err("%s - failed to retrieve serial line settings - %d", __FUNCTION__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 return retval;
362 } else {
363 spin_lock_irqsave(&priv->lock, flags);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 /* store the config in one byte, and later use bit masks to check values */
366 priv->current_config = feature_buffer[4];
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500367 priv->baud_rate = *((u_int32_t *)feature_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500369 if ( (priv->cbr_mask = rate_to_mask(priv->baud_rate)) == 0x40)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 dbg("%s - failed setting the baud mask (not defined)", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 spin_unlock_irqrestore(&priv->lock, flags);
372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 }
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500374 spin_lock_irqsave(&priv->lock, flags);
375 ++priv->cmd_count;
376 spin_unlock_irqrestore(&priv->lock, flags);
377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 return retval;
379} /* cypress_serial_control */
380
381
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500382/* given a baud mask, it will return integer baud on success */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383static 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
438 priv = kmalloc(sizeof (struct cypress_private), GFP_KERNEL);
439 if (!priv)
440 return -ENOMEM;
441
442 memset(priv, 0x00, sizeof (struct cypress_private));
443 spin_lock_init(&priv->lock);
444 priv->buf = cypress_buf_alloc(CYPRESS_BUF_SIZE);
445 if (priv->buf == NULL) {
446 kfree(priv);
447 return -ENOMEM;
448 }
449 init_waitqueue_head(&priv->delta_msr_wait);
450
451 usb_reset_configuration (serial->dev);
452
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500453 interval = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 priv->cmd_ctrl = 0;
455 priv->line_control = 0;
456 priv->termios_initialized = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 priv->rx_flags = 0;
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500458 priv->cbr_mask = B300;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 usb_set_serial_port_data(serial->port[0], priv);
460
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500461 return 0;
462}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
464
465static int cypress_earthmate_startup (struct usb_serial *serial)
466{
467 struct cypress_private *priv;
468
469 dbg("%s", __FUNCTION__);
470
471 if (generic_startup(serial)) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500472 dbg("%s - Failed setting up port %d", __FUNCTION__,
473 serial->port[0]->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 return 1;
475 }
476
477 priv = usb_get_serial_port_data(serial->port[0]);
478 priv->chiptype = CT_EARTHMATE;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500479
480 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481} /* cypress_earthmate_startup */
482
483
484static int cypress_hidcom_startup (struct usb_serial *serial)
485{
486 struct cypress_private *priv;
487
488 dbg("%s", __FUNCTION__);
489
490 if (generic_startup(serial)) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500491 dbg("%s - Failed setting up port %d", __FUNCTION__,
492 serial->port[0]->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 return 1;
494 }
495
496 priv = usb_get_serial_port_data(serial->port[0]);
497 priv->chiptype = CT_CYPHIDCOM;
498
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500499 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500} /* cypress_hidcom_startup */
501
502
503static void cypress_shutdown (struct usb_serial *serial)
504{
505 struct cypress_private *priv;
506
507 dbg ("%s - port %d", __FUNCTION__, serial->port[0]->number);
508
509 /* all open ports are closed at this point */
510
511 priv = usb_get_serial_port_data(serial->port[0]);
512
513 if (priv) {
514 cypress_buf_free(priv->buf);
515 kfree(priv);
516 usb_set_serial_port_data(serial->port[0], NULL);
517 }
518}
519
520
521static int cypress_open (struct usb_serial_port *port, struct file *filp)
522{
523 struct cypress_private *priv = usb_get_serial_port_data(port);
524 struct usb_serial *serial = port->serial;
525 unsigned long flags;
526 int result = 0;
527
528 dbg("%s - port %d", __FUNCTION__, port->number);
529
530 /* clear halts before open */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 usb_clear_halt(serial->dev, 0x81);
532 usb_clear_halt(serial->dev, 0x02);
533
534 spin_lock_irqsave(&priv->lock, flags);
535 /* reset read/write statistics */
536 priv->bytes_in = 0;
537 priv->bytes_out = 0;
538 priv->cmd_count = 0;
539 priv->rx_flags = 0;
540 spin_unlock_irqrestore(&priv->lock, flags);
541
542 /* setting to zero could cause data loss */
543 port->tty->low_latency = 1;
544
545 /* raise both lines and set termios */
546 spin_lock_irqsave(&priv->lock, flags);
547 priv->line_control = CONTROL_DTR | CONTROL_RTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 priv->cmd_ctrl = 1;
549 spin_unlock_irqrestore(&priv->lock, flags);
550 result = cypress_write(port, NULL, 0);
551
552 if (result) {
553 dev_err(&port->dev, "%s - failed setting the control lines - error %d\n", __FUNCTION__, result);
554 return result;
555 } else
556 dbg("%s - success setting the control lines", __FUNCTION__);
557
558 cypress_set_termios(port, &priv->tmp_termios);
559
560 /* setup the port and start reading from the device */
561 if(!port->interrupt_in_urb){
562 err("%s - interrupt_in_urb is empty!", __FUNCTION__);
563 return(-1);
564 }
565
566 usb_fill_int_urb(port->interrupt_in_urb, serial->dev,
567 usb_rcvintpipe(serial->dev, port->interrupt_in_endpointAddress),
568 port->interrupt_in_urb->transfer_buffer, port->interrupt_in_urb->transfer_buffer_length,
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500569 cypress_read_int_callback, port, interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
571
572 if (result){
573 dev_err(&port->dev, "%s - failed submitting read urb, error %d\n", __FUNCTION__, result);
574 }
575
576 return result;
577} /* cypress_open */
578
579
580static void cypress_close(struct usb_serial_port *port, struct file * filp)
581{
582 struct cypress_private *priv = usb_get_serial_port_data(port);
583 unsigned int c_cflag;
584 unsigned long flags;
585 int bps;
586 long timeout;
587 wait_queue_t wait;
588
589 dbg("%s - port %d", __FUNCTION__, port->number);
590
591 /* wait for data to drain from buffer */
592 spin_lock_irqsave(&priv->lock, flags);
593 timeout = CYPRESS_CLOSING_WAIT;
594 init_waitqueue_entry(&wait, current);
595 add_wait_queue(&port->tty->write_wait, &wait);
596 for (;;) {
597 set_current_state(TASK_INTERRUPTIBLE);
598 if (cypress_buf_data_avail(priv->buf) == 0
599 || timeout == 0 || signal_pending(current)
600 || !usb_get_intfdata(port->serial->interface))
601 break;
602 spin_unlock_irqrestore(&priv->lock, flags);
603 timeout = schedule_timeout(timeout);
604 spin_lock_irqsave(&priv->lock, flags);
605 }
606 set_current_state(TASK_RUNNING);
607 remove_wait_queue(&port->tty->write_wait, &wait);
608 /* clear out any remaining data in the buffer */
609 cypress_buf_clear(priv->buf);
610 spin_unlock_irqrestore(&priv->lock, flags);
611
612 /* wait for characters to drain from device */
613 bps = tty_get_baud_rate(port->tty);
614 if (bps > 1200)
615 timeout = max((HZ*2560)/bps,HZ/10);
616 else
617 timeout = 2*HZ;
Nishanth Aravamudan22c43862005-08-15 11:30:11 -0700618 schedule_timeout_interruptible(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
620 dbg("%s - stopping urbs", __FUNCTION__);
621 usb_kill_urb (port->interrupt_in_urb);
622 usb_kill_urb (port->interrupt_out_urb);
623
624 if (port->tty) {
625 c_cflag = port->tty->termios->c_cflag;
626 if (c_cflag & HUPCL) {
627 /* drop dtr and rts */
628 priv = usb_get_serial_port_data(port);
629 spin_lock_irqsave(&priv->lock, flags);
630 priv->line_control = 0;
631 priv->cmd_ctrl = 1;
632 spin_unlock_irqrestore(&priv->lock, flags);
633 cypress_write(port, NULL, 0);
634 }
635 }
636
637 if (stats)
638 dev_info (&port->dev, "Statistics: %d Bytes In | %d Bytes Out | %d Commands Issued\n",
639 priv->bytes_in, priv->bytes_out, priv->cmd_count);
640} /* cypress_close */
641
642
643static int cypress_write(struct usb_serial_port *port, const unsigned char *buf, int count)
644{
645 struct cypress_private *priv = usb_get_serial_port_data(port);
646 unsigned long flags;
647
648 dbg("%s - port %d, %d bytes", __FUNCTION__, port->number, count);
649
650 /* line control commands, which need to be executed immediately,
651 are not put into the buffer for obvious reasons.
652 */
653 if (priv->cmd_ctrl) {
654 count = 0;
655 goto finish;
656 }
657
658 if (!count)
659 return count;
660
661 spin_lock_irqsave(&priv->lock, flags);
662 count = cypress_buf_put(priv->buf, buf, count);
663 spin_unlock_irqrestore(&priv->lock, flags);
664
665finish:
666 cypress_send(port);
667
668 return count;
669} /* cypress_write */
670
671
672static void cypress_send(struct usb_serial_port *port)
673{
674 int count = 0, result, offset, actual_size;
675 struct cypress_private *priv = usb_get_serial_port_data(port);
676 unsigned long flags;
677
678 dbg("%s - port %d", __FUNCTION__, port->number);
679 dbg("%s - interrupt out size is %d", __FUNCTION__, port->interrupt_out_size);
680
681 spin_lock_irqsave(&priv->lock, flags);
682 if (priv->write_urb_in_use) {
683 dbg("%s - can't write, urb in use", __FUNCTION__);
684 spin_unlock_irqrestore(&priv->lock, flags);
685 return;
686 }
687 spin_unlock_irqrestore(&priv->lock, flags);
688
689 /* clear buffer */
690 memset(port->interrupt_out_urb->transfer_buffer, 0, port->interrupt_out_size);
691
692 spin_lock_irqsave(&priv->lock, flags);
693 switch (port->interrupt_out_size) {
694 case 32:
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500695 /* this is for the CY7C64013... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 offset = 2;
697 port->interrupt_out_buffer[0] = priv->line_control;
698 break;
699 case 8:
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500700 /* this is for the CY7C63743... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 offset = 1;
702 port->interrupt_out_buffer[0] = priv->line_control;
703 break;
704 default:
705 dbg("%s - wrong packet size", __FUNCTION__);
706 spin_unlock_irqrestore(&priv->lock, flags);
707 return;
708 }
709
710 if (priv->line_control & CONTROL_RESET)
711 priv->line_control &= ~CONTROL_RESET;
712
713 if (priv->cmd_ctrl) {
714 priv->cmd_count++;
715 dbg("%s - line control command being issued", __FUNCTION__);
716 spin_unlock_irqrestore(&priv->lock, flags);
717 goto send;
718 } else
719 spin_unlock_irqrestore(&priv->lock, flags);
720
721 count = cypress_buf_get(priv->buf, &port->interrupt_out_buffer[offset],
722 port->interrupt_out_size-offset);
723
724 if (count == 0) {
725 return;
726 }
727
728 switch (port->interrupt_out_size) {
729 case 32:
730 port->interrupt_out_buffer[1] = count;
731 break;
732 case 8:
733 port->interrupt_out_buffer[0] |= count;
734 }
735
736 dbg("%s - count is %d", __FUNCTION__, count);
737
738send:
739 spin_lock_irqsave(&priv->lock, flags);
740 priv->write_urb_in_use = 1;
741 spin_unlock_irqrestore(&priv->lock, flags);
742
743 if (priv->cmd_ctrl)
744 actual_size = 1;
745 else
746 actual_size = count + (port->interrupt_out_size == 32 ? 2 : 1);
747
748 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, port->interrupt_out_size,
749 port->interrupt_out_urb->transfer_buffer);
750
751 port->interrupt_out_urb->transfer_buffer_length = actual_size;
752 port->interrupt_out_urb->dev = port->serial->dev;
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500753 port->interrupt_out_urb->interval = interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 result = usb_submit_urb (port->interrupt_out_urb, GFP_ATOMIC);
755 if (result) {
756 dev_err(&port->dev, "%s - failed submitting write urb, error %d\n", __FUNCTION__,
757 result);
758 priv->write_urb_in_use = 0;
759 }
760
761 spin_lock_irqsave(&priv->lock, flags);
762 if (priv->cmd_ctrl) {
763 priv->cmd_ctrl = 0;
764 }
765 priv->bytes_out += count; /* do not count the line control and size bytes */
766 spin_unlock_irqrestore(&priv->lock, flags);
767
768 schedule_work(&port->work);
769} /* cypress_send */
770
771
772/* returns how much space is available in the soft buffer */
773static int cypress_write_room(struct usb_serial_port *port)
774{
775 struct cypress_private *priv = usb_get_serial_port_data(port);
776 int room = 0;
777 unsigned long flags;
778
779 dbg("%s - port %d", __FUNCTION__, port->number);
780
781 spin_lock_irqsave(&priv->lock, flags);
782 room = cypress_buf_space_avail(priv->buf);
783 spin_unlock_irqrestore(&priv->lock, flags);
784
785 dbg("%s - returns %d", __FUNCTION__, room);
786 return room;
787}
788
789
790static int cypress_tiocmget (struct usb_serial_port *port, struct file *file)
791{
792 struct cypress_private *priv = usb_get_serial_port_data(port);
793 __u8 status, control;
794 unsigned int result = 0;
795 unsigned long flags;
796
797 dbg("%s - port %d", __FUNCTION__, port->number);
798
799 spin_lock_irqsave(&priv->lock, flags);
800 control = priv->line_control;
801 status = priv->current_status;
802 spin_unlock_irqrestore(&priv->lock, flags);
803
804 result = ((control & CONTROL_DTR) ? TIOCM_DTR : 0)
805 | ((control & CONTROL_RTS) ? TIOCM_RTS : 0)
806 | ((status & UART_CTS) ? TIOCM_CTS : 0)
807 | ((status & UART_DSR) ? TIOCM_DSR : 0)
808 | ((status & UART_RI) ? TIOCM_RI : 0)
809 | ((status & UART_CD) ? TIOCM_CD : 0);
810
811 dbg("%s - result = %x", __FUNCTION__, result);
812
813 return result;
814}
815
816
817static int cypress_tiocmset (struct usb_serial_port *port, struct file *file,
818 unsigned int set, unsigned int clear)
819{
820 struct cypress_private *priv = usb_get_serial_port_data(port);
821 unsigned long flags;
822
823 dbg("%s - port %d", __FUNCTION__, port->number);
824
825 spin_lock_irqsave(&priv->lock, flags);
826 if (set & TIOCM_RTS)
827 priv->line_control |= CONTROL_RTS;
828 if (set & TIOCM_DTR)
829 priv->line_control |= CONTROL_DTR;
830 if (clear & TIOCM_RTS)
831 priv->line_control &= ~CONTROL_RTS;
832 if (clear & TIOCM_DTR)
833 priv->line_control &= ~CONTROL_DTR;
834 spin_unlock_irqrestore(&priv->lock, flags);
835
836 priv->cmd_ctrl = 1;
837 return cypress_write(port, NULL, 0);
838}
839
840
841static int cypress_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg)
842{
843 struct cypress_private *priv = usb_get_serial_port_data(port);
844
845 dbg("%s - port %d, cmd 0x%.4x", __FUNCTION__, port->number, cmd);
846
847 switch (cmd) {
848 case TIOCGSERIAL:
849 if (copy_to_user((void __user *)arg, port->tty->termios, sizeof(struct termios))) {
850 return -EFAULT;
851 }
852 return (0);
853 break;
854 case TIOCSSERIAL:
855 if (copy_from_user(port->tty->termios, (void __user *)arg, sizeof(struct termios))) {
856 return -EFAULT;
857 }
858 /* here we need to call cypress_set_termios to invoke the new settings */
859 cypress_set_termios(port, &priv->tmp_termios);
860 return (0);
861 break;
862 /* these are called when setting baud rate from gpsd */
863 case TCGETS:
864 if (copy_to_user((void __user *)arg, port->tty->termios, sizeof(struct termios))) {
865 return -EFAULT;
866 }
867 return (0);
868 break;
869 case TCSETS:
870 if (copy_from_user(port->tty->termios, (void __user *)arg, sizeof(struct termios))) {
871 return -EFAULT;
872 }
873 /* here we need to call cypress_set_termios to invoke the new settings */
874 cypress_set_termios(port, &priv->tmp_termios);
875 return (0);
876 break;
877 /* This code comes from drivers/char/serial.c and ftdi_sio.c */
878 case TIOCMIWAIT:
879 while (priv != NULL) {
880 interruptible_sleep_on(&priv->delta_msr_wait);
881 /* see if a signal did it */
882 if (signal_pending(current))
883 return -ERESTARTSYS;
884 else {
885 char diff = priv->diff_status;
886
887 if (diff == 0) {
888 return -EIO; /* no change => error */
889 }
890
891 /* consume all events */
892 priv->diff_status = 0;
893
894 /* return 0 if caller wanted to know about these bits */
895 if ( ((arg & TIOCM_RNG) && (diff & UART_RI)) ||
896 ((arg & TIOCM_DSR) && (diff & UART_DSR)) ||
897 ((arg & TIOCM_CD) && (diff & UART_CD)) ||
898 ((arg & TIOCM_CTS) && (diff & UART_CTS)) ) {
899 return 0;
900 }
901 /* otherwise caller can't care less about what happened,
902 * and so we continue to wait for more events.
903 */
904 }
905 }
906 return 0;
907 break;
908 default:
909 break;
910 }
911
912 dbg("%s - arg not supported - it was 0x%04x - check include/asm/ioctls.h", __FUNCTION__, cmd);
913
914 return -ENOIOCTLCMD;
915} /* cypress_ioctl */
916
917
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500918static void cypress_set_termios (struct usb_serial_port *port,
919 struct termios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
921 struct cypress_private *priv = usb_get_serial_port_data(port);
922 struct tty_struct *tty;
923 int data_bits, stop_bits, parity_type, parity_enable;
924 unsigned cflag, iflag, baud_mask;
925 unsigned long flags;
926 __u8 oldlines;
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500927 int linechange = 0;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500928
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 dbg("%s - port %d", __FUNCTION__, port->number);
930
931 tty = port->tty;
932 if ((!tty) || (!tty->termios)) {
933 dbg("%s - no tty structures", __FUNCTION__);
934 return;
935 }
936
937 spin_lock_irqsave(&priv->lock, flags);
938 if (!priv->termios_initialized) {
939 if (priv->chiptype == CT_EARTHMATE) {
940 *(tty->termios) = tty_std_termios;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500941 tty->termios->c_cflag = B4800 | CS8 | CREAD | HUPCL |
942 CLOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 } else if (priv->chiptype == CT_CYPHIDCOM) {
944 *(tty->termios) = tty_std_termios;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500945 tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
946 CLOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 }
948 priv->termios_initialized = 1;
949 }
950 spin_unlock_irqrestore(&priv->lock, flags);
951
952 cflag = tty->termios->c_cflag;
953 iflag = tty->termios->c_iflag;
954
955 /* check if there are new settings */
956 if (old_termios) {
957 if ((cflag != old_termios->c_cflag) ||
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500958 (RELEVANT_IFLAG(iflag) !=
959 RELEVANT_IFLAG(old_termios->c_iflag))) {
960 dbg("%s - attempting to set new termios settings",
961 __FUNCTION__);
962 /* should make a copy of this in case something goes
963 * wrong in the function, we can restore it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 spin_lock_irqsave(&priv->lock, flags);
965 priv->tmp_termios = *(tty->termios);
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500966 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 } else {
968 dbg("%s - nothing to do, exiting", __FUNCTION__);
969 return;
970 }
971 } else
972 return;
973
974 /* set number of data bits, parity, stop bits */
975 /* when parity is disabled the parity type bit is ignored */
976
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500977 /* 1 means 2 stop bits, 0 means 1 stop bit */
978 stop_bits = cflag & CSTOPB ? 1 : 0;
979
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 if (cflag & PARENB) {
981 parity_enable = 1;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500982 /* 1 means odd parity, 0 means even parity */
983 parity_type = cflag & PARODD ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 } else
985 parity_enable = parity_type = 0;
986
987 if (cflag & CSIZE) {
988 switch (cflag & CSIZE) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500989 case CS5:
990 data_bits = 0;
991 break;
992 case CS6:
993 data_bits = 1;
994 break;
995 case CS7:
996 data_bits = 2;
997 break;
998 case CS8:
999 data_bits = 3;
1000 break;
1001 default:
1002 err("%s - CSIZE was set, but not CS5-CS8",
1003 __FUNCTION__);
1004 data_bits = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 }
1006 } else
1007 data_bits = 3;
1008
1009 spin_lock_irqsave(&priv->lock, flags);
1010 oldlines = priv->line_control;
1011 if ((cflag & CBAUD) == B0) {
1012 /* drop dtr and rts */
1013 dbg("%s - dropping the lines, baud rate 0bps", __FUNCTION__);
1014 baud_mask = B0;
1015 priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
1016 } else {
1017 baud_mask = (cflag & CBAUD);
1018 switch(baud_mask) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001019 case B300:
1020 dbg("%s - setting baud 300bps", __FUNCTION__);
1021 break;
1022 case B600:
1023 dbg("%s - setting baud 600bps", __FUNCTION__);
1024 break;
1025 case B1200:
1026 dbg("%s - setting baud 1200bps", __FUNCTION__);
1027 break;
1028 case B2400:
1029 dbg("%s - setting baud 2400bps", __FUNCTION__);
1030 break;
1031 case B4800:
1032 dbg("%s - setting baud 4800bps", __FUNCTION__);
1033 break;
1034 case B9600:
1035 dbg("%s - setting baud 9600bps", __FUNCTION__);
1036 break;
1037 case B19200:
1038 dbg("%s - setting baud 19200bps", __FUNCTION__);
1039 break;
1040 case B38400:
1041 dbg("%s - setting baud 38400bps", __FUNCTION__);
1042 break;
1043 case B57600:
1044 dbg("%s - setting baud 57600bps", __FUNCTION__);
1045 break;
1046 case B115200:
1047 dbg("%s - setting baud 115200bps", __FUNCTION__);
1048 break;
1049 default:
1050 dbg("%s - unknown masked baud rate", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 }
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001052 priv->line_control = (CONTROL_DTR | CONTROL_RTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 }
1054 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001056 dbg("%s - sending %d stop_bits, %d parity_enable, %d parity_type, "
1057 "%d data_bits (+5)", __FUNCTION__, stop_bits,
1058 parity_enable, parity_type, data_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001060 cypress_serial_control(port, baud_mask, data_bits, stop_bits,
1061 parity_enable, parity_type, 0, CYPRESS_SET_CONFIG);
1062
1063 /* we perform a CYPRESS_GET_CONFIG so that the current settings are
1064 * filled into the private structure this should confirm that all is
1065 * working if it returns what we just set */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 cypress_serial_control(port, 0, 0, 0, 0, 0, 0, CYPRESS_GET_CONFIG);
1067
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001068 /* Here we can define custom tty settings for devices; the main tty
1069 * termios flag base comes from empeg.c */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001071 spin_lock_irqsave(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 if ( (priv->chiptype == CT_EARTHMATE) && (priv->baud_rate == 4800) ) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001073 dbg("Using custom termios settings for a baud rate of "
1074 "4800bps.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 /* define custom termios settings for NMEA protocol */
1076
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 tty->termios->c_iflag /* input modes - */
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001078 &= ~(IGNBRK /* disable ignore break */
1079 | BRKINT /* disable break causes interrupt */
1080 | PARMRK /* disable mark parity errors */
1081 | ISTRIP /* disable clear high bit of input char */
1082 | INLCR /* disable translate NL to CR */
1083 | IGNCR /* disable ignore CR */
1084 | ICRNL /* disable translate CR to NL */
1085 | IXON); /* disable enable XON/XOFF flow control */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001087 tty->termios->c_oflag /* output modes */
1088 &= ~OPOST; /* disable postprocess output char */
1089
1090 tty->termios->c_lflag /* line discipline modes */
1091 &= ~(ECHO /* disable echo input characters */
1092 | ECHONL /* disable echo new line */
1093 | ICANON /* disable erase, kill, werase, and rprnt
1094 special characters */
1095 | ISIG /* disable interrupt, quit, and suspend
1096 special characters */
1097 | IEXTEN); /* disable non-POSIX special characters */
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001098 } /* CT_CYPHIDCOM: Application should handle this for device */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 linechange = (priv->line_control != oldlines);
1101 spin_unlock_irqrestore(&priv->lock, flags);
1102
1103 /* if necessary, set lines */
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001104 if (linechange) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 priv->cmd_ctrl = 1;
1106 cypress_write(port, NULL, 0);
1107 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108} /* cypress_set_termios */
1109
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001110
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111/* returns amount of data still left in soft buffer */
1112static int cypress_chars_in_buffer(struct usb_serial_port *port)
1113{
1114 struct cypress_private *priv = usb_get_serial_port_data(port);
1115 int chars = 0;
1116 unsigned long flags;
1117
1118 dbg("%s - port %d", __FUNCTION__, port->number);
1119
1120 spin_lock_irqsave(&priv->lock, flags);
1121 chars = cypress_buf_data_avail(priv->buf);
1122 spin_unlock_irqrestore(&priv->lock, flags);
1123
1124 dbg("%s - returns %d", __FUNCTION__, chars);
1125 return chars;
1126}
1127
1128
1129static void cypress_throttle (struct usb_serial_port *port)
1130{
1131 struct cypress_private *priv = usb_get_serial_port_data(port);
1132 unsigned long flags;
1133
1134 dbg("%s - port %d", __FUNCTION__, port->number);
1135
1136 spin_lock_irqsave(&priv->lock, flags);
1137 priv->rx_flags = THROTTLED;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001138 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139}
1140
1141
1142static void cypress_unthrottle (struct usb_serial_port *port)
1143{
1144 struct cypress_private *priv = usb_get_serial_port_data(port);
1145 int actually_throttled, result;
1146 unsigned long flags;
1147
1148 dbg("%s - port %d", __FUNCTION__, port->number);
1149
1150 spin_lock_irqsave(&priv->lock, flags);
1151 actually_throttled = priv->rx_flags & ACTUALLY_THROTTLED;
1152 priv->rx_flags = 0;
1153 spin_unlock_irqrestore(&priv->lock, flags);
1154
1155 if (actually_throttled) {
1156 port->interrupt_in_urb->dev = port->serial->dev;
1157
1158 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
1159 if (result)
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001160 dev_err(&port->dev, "%s - failed submitting read urb, "
1161 "error %d\n", __FUNCTION__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 }
1163}
1164
1165
1166static void cypress_read_int_callback(struct urb *urb, struct pt_regs *regs)
1167{
1168 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
1169 struct cypress_private *priv = usb_get_serial_port_data(port);
1170 struct tty_struct *tty;
1171 unsigned char *data = urb->transfer_buffer;
1172 unsigned long flags;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001173 char tty_flag = TTY_NORMAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 int havedata = 0;
1175 int bytes = 0;
1176 int result;
1177 int i = 0;
1178
1179 dbg("%s - port %d", __FUNCTION__, port->number);
1180
1181 if (urb->status) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001182 dbg("%s - nonzero read status received: %d", __FUNCTION__,
1183 urb->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 return;
1185 }
1186
1187 spin_lock_irqsave(&priv->lock, flags);
1188 if (priv->rx_flags & THROTTLED) {
1189 dbg("%s - now throttling", __FUNCTION__);
1190 priv->rx_flags |= ACTUALLY_THROTTLED;
1191 spin_unlock_irqrestore(&priv->lock, flags);
1192 return;
1193 }
1194 spin_unlock_irqrestore(&priv->lock, flags);
1195
1196 tty = port->tty;
1197 if (!tty) {
1198 dbg("%s - bad tty pointer - exiting", __FUNCTION__);
1199 return;
1200 }
1201
1202 spin_lock_irqsave(&priv->lock, flags);
1203 switch(urb->actual_length) {
1204 case 32:
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001205 /* This is for the CY7C64013... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 priv->current_status = data[0] & 0xF8;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001207 bytes = data[1] + 2;
1208 i = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 if (bytes > 2)
1210 havedata = 1;
1211 break;
1212 case 8:
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001213 /* This is for the CY7C63743... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 priv->current_status = data[0] & 0xF8;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001215 bytes = (data[0] & 0x07) + 1;
1216 i = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 if (bytes > 1)
1218 havedata = 1;
1219 break;
1220 default:
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001221 dbg("%s - wrong packet size - received %d bytes",
1222 __FUNCTION__, urb->actual_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 spin_unlock_irqrestore(&priv->lock, flags);
1224 goto continue_read;
1225 }
1226 spin_unlock_irqrestore(&priv->lock, flags);
1227
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001228 usb_serial_debug_data (debug, &port->dev, __FUNCTION__,
1229 urb->actual_length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
1231 spin_lock_irqsave(&priv->lock, flags);
1232 /* check to see if status has changed */
1233 if (priv != NULL) {
1234 if (priv->current_status != priv->prev_status) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001235 priv->diff_status |= priv->current_status ^
1236 priv->prev_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 wake_up_interruptible(&priv->delta_msr_wait);
1238 priv->prev_status = priv->current_status;
1239 }
1240 }
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001241 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001243 /* hangup, as defined in acm.c... this might be a bad place for it
1244 * though */
1245 if (tty && !(tty->termios->c_cflag & CLOCAL) &&
1246 !(priv->current_status & UART_CD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 dbg("%s - calling hangup", __FUNCTION__);
1248 tty_hangup(tty);
1249 goto continue_read;
1250 }
1251
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001252 /* There is one error bit... I'm assuming it is a parity error
1253 * indicator as the generic firmware will set this bit to 1 if a
1254 * parity error occurs.
1255 * I can not find reference to any other error events. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 spin_lock_irqsave(&priv->lock, flags);
1257 if (priv->current_status & CYP_ERROR) {
1258 spin_unlock_irqrestore(&priv->lock, flags);
1259 tty_flag = TTY_PARITY;
1260 dbg("%s - Parity Error detected", __FUNCTION__);
1261 } else
1262 spin_unlock_irqrestore(&priv->lock, flags);
1263
1264 /* process read if there is data other than line status */
1265 if (tty && (bytes > i)) {
1266 for (; i < bytes ; ++i) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001267 dbg("pushing byte number %d - %d - %c", i, data[i],
1268 data[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
1270 tty_flip_buffer_push(tty);
1271 }
1272 tty_insert_flip_char(tty, data[i], tty_flag);
1273 }
1274 tty_flip_buffer_push(port->tty);
1275 }
1276
1277 spin_lock_irqsave(&priv->lock, flags);
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001278 /* control and status byte(s) are also counted */
1279 priv->bytes_in += bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 spin_unlock_irqrestore(&priv->lock, flags);
1281
1282continue_read:
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001283
1284 /* Continue trying to always read... unless the port has closed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
1286 if (port->open_count > 0) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001287 usb_fill_int_urb(port->interrupt_in_urb, port->serial->dev,
1288 usb_rcvintpipe(port->serial->dev,
1289 port->interrupt_in_endpointAddress),
1290 port->interrupt_in_urb->transfer_buffer,
1291 port->interrupt_in_urb->transfer_buffer_length,
1292 cypress_read_int_callback, port, interval);
1293 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
1294 if (result)
1295 dev_err(&urb->dev->dev, "%s - failed resubmitting "
1296 "read urb, error %d\n", __FUNCTION__,
1297 result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 }
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001299
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 return;
1301} /* cypress_read_int_callback */
1302
1303
1304static void cypress_write_int_callback(struct urb *urb, struct pt_regs *regs)
1305{
1306 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
1307 struct cypress_private *priv = usb_get_serial_port_data(port);
1308 int result;
1309
1310 dbg("%s - port %d", __FUNCTION__, port->number);
1311
1312 switch (urb->status) {
1313 case 0:
1314 /* success */
1315 break;
1316 case -ECONNRESET:
1317 case -ENOENT:
1318 case -ESHUTDOWN:
1319 /* this urb is terminated, clean up */
1320 dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status);
1321 priv->write_urb_in_use = 0;
1322 return;
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001323 case -EPIPE: /* no break needed */
1324 usb_clear_halt(port->serial->dev, 0x02);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 default:
1326 /* error in the urb, so we have to resubmit it */
1327 dbg("%s - Overflow in write", __FUNCTION__);
1328 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
1329 port->interrupt_out_urb->transfer_buffer_length = 1;
1330 port->interrupt_out_urb->dev = port->serial->dev;
1331 result = usb_submit_urb(port->interrupt_out_urb, GFP_ATOMIC);
1332 if (result)
1333 dev_err(&urb->dev->dev, "%s - failed resubmitting write urb, error %d\n",
1334 __FUNCTION__, result);
1335 else
1336 return;
1337 }
1338
1339 priv->write_urb_in_use = 0;
1340
1341 /* send any buffered data */
1342 cypress_send(port);
1343}
1344
1345
1346/*****************************************************************************
1347 * Write buffer functions - buffering code from pl2303 used
1348 *****************************************************************************/
1349
1350/*
1351 * cypress_buf_alloc
1352 *
1353 * Allocate a circular buffer and all associated memory.
1354 */
1355
1356static struct cypress_buf *cypress_buf_alloc(unsigned int size)
1357{
1358
1359 struct cypress_buf *cb;
1360
1361
1362 if (size == 0)
1363 return NULL;
1364
1365 cb = (struct cypress_buf *)kmalloc(sizeof(struct cypress_buf), GFP_KERNEL);
1366 if (cb == NULL)
1367 return NULL;
1368
1369 cb->buf_buf = kmalloc(size, GFP_KERNEL);
1370 if (cb->buf_buf == NULL) {
1371 kfree(cb);
1372 return NULL;
1373 }
1374
1375 cb->buf_size = size;
1376 cb->buf_get = cb->buf_put = cb->buf_buf;
1377
1378 return cb;
1379
1380}
1381
1382
1383/*
1384 * cypress_buf_free
1385 *
1386 * Free the buffer and all associated memory.
1387 */
1388
1389static void cypress_buf_free(struct cypress_buf *cb)
1390{
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001391 if (cb) {
1392 kfree(cb->buf_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 kfree(cb);
1394 }
1395}
1396
1397
1398/*
1399 * cypress_buf_clear
1400 *
1401 * Clear out all data in the circular buffer.
1402 */
1403
1404static void cypress_buf_clear(struct cypress_buf *cb)
1405{
1406 if (cb != NULL)
1407 cb->buf_get = cb->buf_put;
1408 /* equivalent to a get of all data available */
1409}
1410
1411
1412/*
1413 * cypress_buf_data_avail
1414 *
1415 * Return the number of bytes of data available in the circular
1416 * buffer.
1417 */
1418
1419static unsigned int cypress_buf_data_avail(struct cypress_buf *cb)
1420{
1421 if (cb != NULL)
1422 return ((cb->buf_size + cb->buf_put - cb->buf_get) % cb->buf_size);
1423 else
1424 return 0;
1425}
1426
1427
1428/*
1429 * cypress_buf_space_avail
1430 *
1431 * Return the number of bytes of space available in the circular
1432 * buffer.
1433 */
1434
1435static unsigned int cypress_buf_space_avail(struct cypress_buf *cb)
1436{
1437 if (cb != NULL)
1438 return ((cb->buf_size + cb->buf_get - cb->buf_put - 1) % cb->buf_size);
1439 else
1440 return 0;
1441}
1442
1443
1444/*
1445 * cypress_buf_put
1446 *
1447 * Copy data data from a user buffer and put it into the circular buffer.
1448 * Restrict to the amount of space available.
1449 *
1450 * Return the number of bytes copied.
1451 */
1452
1453static unsigned int cypress_buf_put(struct cypress_buf *cb, const char *buf,
1454 unsigned int count)
1455{
1456
1457 unsigned int len;
1458
1459
1460 if (cb == NULL)
1461 return 0;
1462
1463 len = cypress_buf_space_avail(cb);
1464 if (count > len)
1465 count = len;
1466
1467 if (count == 0)
1468 return 0;
1469
1470 len = cb->buf_buf + cb->buf_size - cb->buf_put;
1471 if (count > len) {
1472 memcpy(cb->buf_put, buf, len);
1473 memcpy(cb->buf_buf, buf+len, count - len);
1474 cb->buf_put = cb->buf_buf + count - len;
1475 } else {
1476 memcpy(cb->buf_put, buf, count);
1477 if (count < len)
1478 cb->buf_put += count;
1479 else /* count == len */
1480 cb->buf_put = cb->buf_buf;
1481 }
1482
1483 return count;
1484
1485}
1486
1487
1488/*
1489 * cypress_buf_get
1490 *
1491 * Get data from the circular buffer and copy to the given buffer.
1492 * Restrict to the amount of data available.
1493 *
1494 * Return the number of bytes copied.
1495 */
1496
1497static unsigned int cypress_buf_get(struct cypress_buf *cb, char *buf,
1498 unsigned int count)
1499{
1500
1501 unsigned int len;
1502
1503
1504 if (cb == NULL)
1505 return 0;
1506
1507 len = cypress_buf_data_avail(cb);
1508 if (count > len)
1509 count = len;
1510
1511 if (count == 0)
1512 return 0;
1513
1514 len = cb->buf_buf + cb->buf_size - cb->buf_get;
1515 if (count > len) {
1516 memcpy(buf, cb->buf_get, len);
1517 memcpy(buf+len, cb->buf_buf, count - len);
1518 cb->buf_get = cb->buf_buf + count - len;
1519 } else {
1520 memcpy(buf, cb->buf_get, count);
1521 if (count < len)
1522 cb->buf_get += count;
1523 else /* count == len */
1524 cb->buf_get = cb->buf_buf;
1525 }
1526
1527 return count;
1528
1529}
1530
1531/*****************************************************************************
1532 * Module functions
1533 *****************************************************************************/
1534
1535static int __init cypress_init(void)
1536{
1537 int retval;
1538
1539 dbg("%s", __FUNCTION__);
1540
1541 retval = usb_serial_register(&cypress_earthmate_device);
1542 if (retval)
1543 goto failed_em_register;
1544 retval = usb_serial_register(&cypress_hidcom_device);
1545 if (retval)
1546 goto failed_hidcom_register;
1547 retval = usb_register(&cypress_driver);
1548 if (retval)
1549 goto failed_usb_register;
1550
1551 info(DRIVER_DESC " " DRIVER_VERSION);
1552 return 0;
1553failed_usb_register:
1554 usb_deregister(&cypress_driver);
1555failed_hidcom_register:
1556 usb_serial_deregister(&cypress_hidcom_device);
1557failed_em_register:
1558 usb_serial_deregister(&cypress_earthmate_device);
1559
1560 return retval;
1561}
1562
1563
1564static void __exit cypress_exit (void)
1565{
1566 dbg("%s", __FUNCTION__);
1567
1568 usb_deregister (&cypress_driver);
1569 usb_serial_deregister (&cypress_earthmate_device);
1570 usb_serial_deregister (&cypress_hidcom_device);
1571}
1572
1573
1574module_init(cypress_init);
1575module_exit(cypress_exit);
1576
1577MODULE_AUTHOR( DRIVER_AUTHOR );
1578MODULE_DESCRIPTION( DRIVER_DESC );
1579MODULE_VERSION( DRIVER_VERSION );
1580MODULE_LICENSE("GPL");
1581
1582module_param(debug, bool, S_IRUGO | S_IWUSR);
1583MODULE_PARM_DESC(debug, "Debug enabled or not");
1584module_param(stats, bool, S_IRUGO | S_IWUSR);
1585MODULE_PARM_DESC(stats, "Enable statistics or not");
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001586module_param(interval, int, S_IRUGO | S_IWUSR);
1587MODULE_PARM_DESC(interval, "Overrides interrupt interval");