blob: 741dcec36d41e52f203f2fa5365ea641a9dcc891 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * USB Cypress M8 driver
3 *
4 * Copyright (C) 2004
5 * Lonnie Mendez (dignome@gmail.com)
6 * Copyright (C) 2003,2004
7 * Neil Whelchel (koyama@firstlight.net)
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * See Documentation/usb/usb-serial.txt for more information on using this driver
15 *
16 * See http://geocities.com/i0xox0i for information on this driver and the
17 * earthmate usb device.
18 *
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -050019 * Lonnie Mendez <dignome@gmail.com>
20 * 4-29-2005
21 * Fixed problem where setting or retreiving the serial config would fail with
22 * EPIPE. Removed CRTS toggling so the driver behaves more like other usbserial
23 * adapters. Issued new interval of 1ms instead of the default 10ms. As a
24 * result, transfer speed has been substantially increased. From avg. 850bps to
25 * avg. 3300bps. initial termios has also been modified. Cleaned up code and
26 * formatting issues so it is more readable. Replaced the C++ style comments.
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 *
28 * Lonnie Mendez <dignome@gmail.com>
29 * 12-15-2004
30 * Incorporated write buffering from pl2303 driver. Fixed bug with line
31 * handling so both lines are raised in cypress_open. (was dropping rts)
32 * Various code cleanups made as well along with other misc bug fixes.
33 *
34 * Lonnie Mendez <dignome@gmail.com>
35 * 04-10-2004
36 * Driver modified to support dynamic line settings. Various improvments
37 * and features.
38 *
39 * Neil Whelchel
40 * 10-2003
41 * Driver first released.
42 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 */
44
45/* Thanks to Neil Whelchel for writing the first cypress m8 implementation for linux. */
46/* Thanks to cypress for providing references for the hid reports. */
47/* Thanks to Jiang Zhang for providing links and for general help. */
48/* Code originates and was built up from ftdi_sio, belkin, pl2303 and others. */
49
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/kernel.h>
52#include <linux/errno.h>
53#include <linux/init.h>
54#include <linux/slab.h>
55#include <linux/tty.h>
56#include <linux/tty_driver.h>
57#include <linux/tty_flip.h>
58#include <linux/module.h>
59#include <linux/moduleparam.h>
60#include <linux/spinlock.h>
61#include <linux/usb.h>
Greg Kroah-Hartmana9698882006-07-11 21:22:58 -070062#include <linux/usb/serial.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include <linux/serial.h>
64#include <linux/delay.h>
65#include <asm/uaccess.h>
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#include "cypress_m8.h"
68
69
70#ifdef CONFIG_USB_SERIAL_DEBUG
71 static int debug = 1;
72#else
73 static int debug;
74#endif
75static int stats;
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -050076static int interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78/*
79 * Version Information
80 */
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -050081#define DRIVER_VERSION "v1.09"
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#define DRIVER_AUTHOR "Lonnie Mendez <dignome@gmail.com>, Neil Whelchel <koyama@firstlight.net>"
83#define DRIVER_DESC "Cypress USB to Serial Driver"
84
85/* write buffer size defines */
86#define CYPRESS_BUF_SIZE 1024
87#define CYPRESS_CLOSING_WAIT (30*HZ)
88
89static struct usb_device_id id_table_earthmate [] = {
90 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB) },
Lonnie Mendez25b6f082005-05-10 17:09:52 -050091 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB_LT20) },
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 { } /* Terminating entry */
93};
94
95static struct usb_device_id id_table_cyphidcomrs232 [] = {
96 { USB_DEVICE(VENDOR_ID_CYPRESS, PRODUCT_ID_CYPHIDCOM) },
97 { } /* Terminating entry */
98};
99
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600100static struct usb_device_id id_table_nokiaca42v2 [] = {
101 { USB_DEVICE(VENDOR_ID_DAZZLE, PRODUCT_ID_CA42) },
102 { } /* Terminating entry */
103};
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105static struct usb_device_id id_table_combined [] = {
106 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB) },
Lonnie Mendez25b6f082005-05-10 17:09:52 -0500107 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB_LT20) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 { USB_DEVICE(VENDOR_ID_CYPRESS, PRODUCT_ID_CYPHIDCOM) },
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600109 { USB_DEVICE(VENDOR_ID_DAZZLE, PRODUCT_ID_CA42) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 { } /* Terminating entry */
111};
112
113MODULE_DEVICE_TABLE (usb, id_table_combined);
114
115static struct usb_driver cypress_driver = {
116 .name = "cypress",
117 .probe = usb_serial_probe,
118 .disconnect = usb_serial_disconnect,
119 .id_table = id_table_combined,
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800120 .no_dynamic_id = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121};
122
123struct cypress_private {
124 spinlock_t lock; /* private lock */
125 int chiptype; /* identifier of device, for quirks/etc */
126 int bytes_in; /* used for statistics */
127 int bytes_out; /* used for statistics */
128 int cmd_count; /* used for statistics */
129 int cmd_ctrl; /* always set this to 1 before issuing a command */
130 struct cypress_buf *buf; /* write buffer */
131 int write_urb_in_use; /* write urb in use indicator */
Mike Isely0257fa92006-08-29 22:06:59 -0500132 int write_urb_interval; /* interval to use for write urb */
133 int read_urb_interval; /* interval to use for read urb */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 int termios_initialized;
135 __u8 line_control; /* holds dtr / rts value */
136 __u8 current_status; /* received from last read - info on dsr,cts,cd,ri,etc */
137 __u8 current_config; /* stores the current configuration byte */
138 __u8 rx_flags; /* throttling - used from whiteheat/ftdi_sio */
139 int baud_rate; /* stores current baud rate in integer form */
140 int cbr_mask; /* stores current baud rate in masked form */
141 int isthrottled; /* if throttled, discard reads */
142 wait_queue_head_t delta_msr_wait; /* used for TIOCMIWAIT */
143 char prev_status, diff_status; /* used for TIOCMIWAIT */
144 /* we pass a pointer to this as the arguement sent to cypress_set_termios old_termios */
145 struct termios tmp_termios; /* stores the old termios settings */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146};
147
148/* write buffer structure */
149struct cypress_buf {
150 unsigned int buf_size;
151 char *buf_buf;
152 char *buf_get;
153 char *buf_put;
154};
155
156/* function prototypes for the Cypress USB to serial device */
157static int cypress_earthmate_startup (struct usb_serial *serial);
158static int cypress_hidcom_startup (struct usb_serial *serial);
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600159static int cypress_ca42v2_startup (struct usb_serial *serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160static void cypress_shutdown (struct usb_serial *serial);
161static int cypress_open (struct usb_serial_port *port, struct file *filp);
162static void cypress_close (struct usb_serial_port *port, struct file *filp);
163static int cypress_write (struct usb_serial_port *port, const unsigned char *buf, int count);
164static void cypress_send (struct usb_serial_port *port);
165static int cypress_write_room (struct usb_serial_port *port);
166static int cypress_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg);
167static void cypress_set_termios (struct usb_serial_port *port, struct termios * old);
168static int cypress_tiocmget (struct usb_serial_port *port, struct file *file);
169static int cypress_tiocmset (struct usb_serial_port *port, struct file *file, unsigned int set, unsigned int clear);
170static int cypress_chars_in_buffer (struct usb_serial_port *port);
171static void cypress_throttle (struct usb_serial_port *port);
172static void cypress_unthrottle (struct usb_serial_port *port);
173static void cypress_read_int_callback (struct urb *urb, struct pt_regs *regs);
174static void cypress_write_int_callback (struct urb *urb, struct pt_regs *regs);
175/* baud helper functions */
176static int mask_to_rate (unsigned mask);
177static unsigned rate_to_mask (int rate);
178/* write buffer functions */
179static struct cypress_buf *cypress_buf_alloc(unsigned int size);
180static void cypress_buf_free(struct cypress_buf *cb);
181static void cypress_buf_clear(struct cypress_buf *cb);
182static unsigned int cypress_buf_data_avail(struct cypress_buf *cb);
183static unsigned int cypress_buf_space_avail(struct cypress_buf *cb);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500184static unsigned int cypress_buf_put(struct cypress_buf *cb, const char *buf, unsigned int count);
185static unsigned int cypress_buf_get(struct cypress_buf *cb, char *buf, unsigned int count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700188static struct usb_serial_driver cypress_earthmate_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700189 .driver = {
190 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700191 .name = "earthmate",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700192 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700193 .description = "DeLorme Earthmate USB",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 .id_table = id_table_earthmate,
195 .num_interrupt_in = 1,
196 .num_interrupt_out = 1,
197 .num_bulk_in = NUM_DONT_CARE,
198 .num_bulk_out = NUM_DONT_CARE,
199 .num_ports = 1,
200 .attach = cypress_earthmate_startup,
201 .shutdown = cypress_shutdown,
202 .open = cypress_open,
203 .close = cypress_close,
204 .write = cypress_write,
205 .write_room = cypress_write_room,
206 .ioctl = cypress_ioctl,
207 .set_termios = cypress_set_termios,
208 .tiocmget = cypress_tiocmget,
209 .tiocmset = cypress_tiocmset,
210 .chars_in_buffer = cypress_chars_in_buffer,
211 .throttle = cypress_throttle,
212 .unthrottle = cypress_unthrottle,
213 .read_int_callback = cypress_read_int_callback,
214 .write_int_callback = cypress_write_int_callback,
215};
216
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700217static struct usb_serial_driver cypress_hidcom_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700218 .driver = {
219 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700220 .name = "cyphidcom",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700221 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700222 .description = "HID->COM RS232 Adapter",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 .id_table = id_table_cyphidcomrs232,
224 .num_interrupt_in = 1,
225 .num_interrupt_out = 1,
226 .num_bulk_in = NUM_DONT_CARE,
227 .num_bulk_out = NUM_DONT_CARE,
228 .num_ports = 1,
229 .attach = cypress_hidcom_startup,
230 .shutdown = cypress_shutdown,
231 .open = cypress_open,
232 .close = cypress_close,
233 .write = cypress_write,
234 .write_room = cypress_write_room,
235 .ioctl = cypress_ioctl,
236 .set_termios = cypress_set_termios,
237 .tiocmget = cypress_tiocmget,
238 .tiocmset = cypress_tiocmset,
239 .chars_in_buffer = cypress_chars_in_buffer,
240 .throttle = cypress_throttle,
241 .unthrottle = cypress_unthrottle,
242 .read_int_callback = cypress_read_int_callback,
243 .write_int_callback = cypress_write_int_callback,
244};
245
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600246static struct usb_serial_driver cypress_ca42v2_device = {
247 .driver = {
248 .owner = THIS_MODULE,
249 .name = "nokiaca42v2",
250 },
251 .description = "Nokia CA-42 V2 Adapter",
252 .id_table = id_table_nokiaca42v2,
253 .num_interrupt_in = 1,
254 .num_interrupt_out = 1,
255 .num_bulk_in = NUM_DONT_CARE,
256 .num_bulk_out = NUM_DONT_CARE,
257 .num_ports = 1,
258 .attach = cypress_ca42v2_startup,
259 .shutdown = cypress_shutdown,
260 .open = cypress_open,
261 .close = cypress_close,
262 .write = cypress_write,
263 .write_room = cypress_write_room,
264 .ioctl = cypress_ioctl,
265 .set_termios = cypress_set_termios,
266 .tiocmget = cypress_tiocmget,
267 .tiocmset = cypress_tiocmset,
268 .chars_in_buffer = cypress_chars_in_buffer,
269 .throttle = cypress_throttle,
270 .unthrottle = cypress_unthrottle,
271 .read_int_callback = cypress_read_int_callback,
272 .write_int_callback = cypress_write_int_callback,
273};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275/*****************************************************************************
276 * Cypress serial helper functions
277 *****************************************************************************/
278
279
Steven Cole093cf722005-05-03 19:07:24 -0600280/* This function can either set or retrieve the current serial line settings */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281static int cypress_serial_control (struct usb_serial_port *port, unsigned baud_mask, int data_bits, int stop_bits,
282 int parity_enable, int parity_type, int reset, int cypress_request_type)
283{
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500284 int new_baudrate = 0, retval = 0, tries = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 struct cypress_private *priv;
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500286 __u8 feature_buffer[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 unsigned long flags;
288
289 dbg("%s", __FUNCTION__);
290
291 priv = usb_get_serial_port_data(port);
292
293 switch(cypress_request_type) {
294 case CYPRESS_SET_CONFIG:
295
296 /*
297 * The general purpose firmware for the Cypress M8 allows for a maximum speed
298 * of 57600bps (I have no idea whether DeLorme chose to use the general purpose
299 * firmware or not), if you need to modify this speed setting for your own
300 * project please add your own chiptype and modify the code likewise. The
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500301 * Cypress HID->COM device will work successfully up to 115200bps (but the
302 * actual throughput is around 3kBps).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 */
304 if (baud_mask != priv->cbr_mask) {
305 dbg("%s - baud rate is changing", __FUNCTION__);
306 if ( priv->chiptype == CT_EARTHMATE ) {
307 /* 300 and 600 baud rates are supported under the generic firmware,
308 * but are not used with NMEA and SiRF protocols */
309
310 if ( (baud_mask == B300) || (baud_mask == B600) ) {
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500311 err("%s - failed setting baud rate, unsupported speed",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 __FUNCTION__);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500313 new_baudrate = priv->baud_rate;
314 } else if ( (new_baudrate = mask_to_rate(baud_mask)) == -1) {
315 err("%s - failed setting baud rate, unsupported speed",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 __FUNCTION__);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500317 new_baudrate = priv->baud_rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 }
319 } else if (priv->chiptype == CT_CYPHIDCOM) {
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500320 if ( (new_baudrate = mask_to_rate(baud_mask)) == -1) {
321 err("%s - failed setting baud rate, unsupported speed",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 __FUNCTION__);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500323 new_baudrate = priv->baud_rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 }
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600325 } else if (priv->chiptype == CT_CA42V2) {
326 if ( (new_baudrate = mask_to_rate(baud_mask)) == -1) {
327 err("%s - failed setting baud rate, unsupported speed",
328 __FUNCTION__);
329 new_baudrate = priv->baud_rate;
330 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 } else if (priv->chiptype == CT_GENERIC) {
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500332 if ( (new_baudrate = mask_to_rate(baud_mask)) == -1) {
333 err("%s - failed setting baud rate, unsupported speed",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 __FUNCTION__);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500335 new_baudrate = priv->baud_rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 }
337 } else {
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500338 info("%s - please define your chiptype", __FUNCTION__);
339 new_baudrate = priv->baud_rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 }
341 } else { /* baud rate not changing, keep the old */
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500342 new_baudrate = priv->baud_rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 }
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500344 dbg("%s - baud rate is being sent as %d", __FUNCTION__, new_baudrate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500346 memset(feature_buffer, 0, 8);
347 /* fill the feature_buffer with new configuration */
348 *((u_int32_t *)feature_buffer) = new_baudrate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500350 feature_buffer[4] |= data_bits; /* assign data bits in 2 bit space ( max 3 ) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 /* 1 bit gap */
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500352 feature_buffer[4] |= (stop_bits << 3); /* assign stop bits in 1 bit space */
353 feature_buffer[4] |= (parity_enable << 4); /* assign parity flag in 1 bit space */
354 feature_buffer[4] |= (parity_type << 5); /* assign parity type in 1 bit space */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 /* 1 bit gap */
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500356 feature_buffer[4] |= (reset << 7); /* assign reset at end of byte, 1 bit space */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
358 dbg("%s - device is being sent this feature report:", __FUNCTION__);
359 dbg("%s - %02X - %02X - %02X - %02X - %02X", __FUNCTION__, feature_buffer[0], feature_buffer[1],
360 feature_buffer[2], feature_buffer[3], feature_buffer[4]);
361
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500362 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 retval = usb_control_msg (port->serial->dev, usb_sndctrlpipe(port->serial->dev, 0),
364 HID_REQ_SET_REPORT, USB_DIR_OUT | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500365 0x0300, 0, feature_buffer, 8, 500);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500367 if (tries++ >= 3)
368 break;
369
370 if (retval == EPIPE)
371 usb_clear_halt(port->serial->dev, 0x00);
372 } while (retval != 8 && retval != ENODEV);
373
374 if (retval != 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 err("%s - failed sending serial line settings - %d", __FUNCTION__, retval);
376 else {
377 spin_lock_irqsave(&priv->lock, flags);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500378 priv->baud_rate = new_baudrate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 priv->cbr_mask = baud_mask;
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500380 priv->current_config = feature_buffer[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 spin_unlock_irqrestore(&priv->lock, flags);
382 }
383 break;
384 case CYPRESS_GET_CONFIG:
385 dbg("%s - retreiving serial line settings", __FUNCTION__);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500386 /* set initial values in feature buffer */
387 memset(feature_buffer, 0, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500389 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 retval = usb_control_msg (port->serial->dev, usb_rcvctrlpipe(port->serial->dev, 0),
391 HID_REQ_GET_REPORT, USB_DIR_IN | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500392 0x0300, 0, feature_buffer, 8, 500);
393
394 if (tries++ >= 3)
395 break;
396
397 if (retval == EPIPE)
398 usb_clear_halt(port->serial->dev, 0x00);
399 } while (retval != 5 && retval != ENODEV);
400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 if (retval != 5) {
Adrian Bunk943ffb52006-01-10 00:10:13 +0100402 err("%s - failed to retrieve serial line settings - %d", __FUNCTION__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 return retval;
404 } else {
405 spin_lock_irqsave(&priv->lock, flags);
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500406
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 /* store the config in one byte, and later use bit masks to check values */
408 priv->current_config = feature_buffer[4];
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500409 priv->baud_rate = *((u_int32_t *)feature_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500411 if ( (priv->cbr_mask = rate_to_mask(priv->baud_rate)) == 0x40)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 dbg("%s - failed setting the baud mask (not defined)", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 spin_unlock_irqrestore(&priv->lock, flags);
414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 }
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500416 spin_lock_irqsave(&priv->lock, flags);
417 ++priv->cmd_count;
418 spin_unlock_irqrestore(&priv->lock, flags);
419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 return retval;
421} /* cypress_serial_control */
422
423
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500424/* given a baud mask, it will return integer baud on success */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425static int mask_to_rate (unsigned mask)
426{
427 int rate;
428
429 switch (mask) {
430 case B0: rate = 0; break;
431 case B300: rate = 300; break;
432 case B600: rate = 600; break;
433 case B1200: rate = 1200; break;
434 case B2400: rate = 2400; break;
435 case B4800: rate = 4800; break;
436 case B9600: rate = 9600; break;
437 case B19200: rate = 19200; break;
438 case B38400: rate = 38400; break;
439 case B57600: rate = 57600; break;
440 case B115200: rate = 115200; break;
441 default: rate = -1;
442 }
443
444 return rate;
445}
446
447
448static unsigned rate_to_mask (int rate)
449{
450 unsigned mask;
451
452 switch (rate) {
453 case 0: mask = B0; break;
454 case 300: mask = B300; break;
455 case 600: mask = B600; break;
456 case 1200: mask = B1200; break;
457 case 2400: mask = B2400; break;
458 case 4800: mask = B4800; break;
459 case 9600: mask = B9600; break;
460 case 19200: mask = B19200; break;
461 case 38400: mask = B38400; break;
462 case 57600: mask = B57600; break;
463 case 115200: mask = B115200; break;
464 default: mask = 0x40;
465 }
466
467 return mask;
468}
469/*****************************************************************************
470 * Cypress serial driver functions
471 *****************************************************************************/
472
473
474static int generic_startup (struct usb_serial *serial)
475{
476 struct cypress_private *priv;
Mike Isely0257fa92006-08-29 22:06:59 -0500477 struct usb_serial_port *port = serial->port[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Mike Isely0257fa92006-08-29 22:06:59 -0500479 dbg("%s - port %d", __FUNCTION__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +0100481 priv = kzalloc(sizeof (struct cypress_private), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 if (!priv)
483 return -ENOMEM;
484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 spin_lock_init(&priv->lock);
486 priv->buf = cypress_buf_alloc(CYPRESS_BUF_SIZE);
487 if (priv->buf == NULL) {
488 kfree(priv);
489 return -ENOMEM;
490 }
491 init_waitqueue_head(&priv->delta_msr_wait);
492
493 usb_reset_configuration (serial->dev);
494
495 priv->cmd_ctrl = 0;
496 priv->line_control = 0;
497 priv->termios_initialized = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 priv->rx_flags = 0;
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500499 priv->cbr_mask = B300;
Mike Isely0257fa92006-08-29 22:06:59 -0500500 if (interval > 0) {
501 priv->write_urb_interval = interval;
502 priv->read_urb_interval = interval;
503 dbg("%s - port %d read & write intervals forced to %d",
504 __FUNCTION__,port->number,interval);
505 } else {
506 priv->write_urb_interval = port->interrupt_out_urb->interval;
507 priv->read_urb_interval = port->interrupt_in_urb->interval;
508 dbg("%s - port %d intervals: read=%d write=%d",
509 __FUNCTION__,port->number,
510 priv->read_urb_interval,priv->write_urb_interval);
511 }
512 usb_set_serial_port_data(port, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500514 return 0;
515}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
517
518static int cypress_earthmate_startup (struct usb_serial *serial)
519{
520 struct cypress_private *priv;
521
522 dbg("%s", __FUNCTION__);
523
524 if (generic_startup(serial)) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500525 dbg("%s - Failed setting up port %d", __FUNCTION__,
526 serial->port[0]->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 return 1;
528 }
529
530 priv = usb_get_serial_port_data(serial->port[0]);
531 priv->chiptype = CT_EARTHMATE;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500532
533 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534} /* cypress_earthmate_startup */
535
536
537static int cypress_hidcom_startup (struct usb_serial *serial)
538{
539 struct cypress_private *priv;
540
541 dbg("%s", __FUNCTION__);
542
543 if (generic_startup(serial)) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500544 dbg("%s - Failed setting up port %d", __FUNCTION__,
545 serial->port[0]->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 return 1;
547 }
548
549 priv = usb_get_serial_port_data(serial->port[0]);
550 priv->chiptype = CT_CYPHIDCOM;
551
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500552 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553} /* cypress_hidcom_startup */
554
555
Lonnie Mendeza5c44e22006-03-01 10:45:24 -0600556static int cypress_ca42v2_startup (struct usb_serial *serial)
557{
558 struct cypress_private *priv;
559
560 dbg("%s", __FUNCTION__);
561
562 if (generic_startup(serial)) {
563 dbg("%s - Failed setting up port %d", __FUNCTION__,
564 serial->port[0]->number);
565 return 1;
566 }
567
568 priv = usb_get_serial_port_data(serial->port[0]);
569 priv->chiptype = CT_CA42V2;
570
571 return 0;
572} /* cypress_ca42v2_startup */
573
574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575static void cypress_shutdown (struct usb_serial *serial)
576{
577 struct cypress_private *priv;
578
579 dbg ("%s - port %d", __FUNCTION__, serial->port[0]->number);
580
581 /* all open ports are closed at this point */
582
583 priv = usb_get_serial_port_data(serial->port[0]);
584
585 if (priv) {
586 cypress_buf_free(priv->buf);
587 kfree(priv);
588 usb_set_serial_port_data(serial->port[0], NULL);
589 }
590}
591
592
593static int cypress_open (struct usb_serial_port *port, struct file *filp)
594{
595 struct cypress_private *priv = usb_get_serial_port_data(port);
596 struct usb_serial *serial = port->serial;
597 unsigned long flags;
598 int result = 0;
599
600 dbg("%s - port %d", __FUNCTION__, port->number);
601
602 /* clear halts before open */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 usb_clear_halt(serial->dev, 0x81);
604 usb_clear_halt(serial->dev, 0x02);
605
606 spin_lock_irqsave(&priv->lock, flags);
607 /* reset read/write statistics */
608 priv->bytes_in = 0;
609 priv->bytes_out = 0;
610 priv->cmd_count = 0;
611 priv->rx_flags = 0;
612 spin_unlock_irqrestore(&priv->lock, flags);
613
614 /* setting to zero could cause data loss */
615 port->tty->low_latency = 1;
616
617 /* raise both lines and set termios */
618 spin_lock_irqsave(&priv->lock, flags);
619 priv->line_control = CONTROL_DTR | CONTROL_RTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 priv->cmd_ctrl = 1;
621 spin_unlock_irqrestore(&priv->lock, flags);
622 result = cypress_write(port, NULL, 0);
623
624 if (result) {
625 dev_err(&port->dev, "%s - failed setting the control lines - error %d\n", __FUNCTION__, result);
626 return result;
627 } else
628 dbg("%s - success setting the control lines", __FUNCTION__);
629
630 cypress_set_termios(port, &priv->tmp_termios);
631
632 /* setup the port and start reading from the device */
633 if(!port->interrupt_in_urb){
634 err("%s - interrupt_in_urb is empty!", __FUNCTION__);
635 return(-1);
636 }
637
638 usb_fill_int_urb(port->interrupt_in_urb, serial->dev,
639 usb_rcvintpipe(serial->dev, port->interrupt_in_endpointAddress),
640 port->interrupt_in_urb->transfer_buffer, port->interrupt_in_urb->transfer_buffer_length,
Mike Isely0257fa92006-08-29 22:06:59 -0500641 cypress_read_int_callback, port, priv->read_urb_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
643
644 if (result){
645 dev_err(&port->dev, "%s - failed submitting read urb, error %d\n", __FUNCTION__, result);
646 }
647
648 return result;
649} /* cypress_open */
650
651
652static void cypress_close(struct usb_serial_port *port, struct file * filp)
653{
654 struct cypress_private *priv = usb_get_serial_port_data(port);
655 unsigned int c_cflag;
656 unsigned long flags;
657 int bps;
658 long timeout;
659 wait_queue_t wait;
660
661 dbg("%s - port %d", __FUNCTION__, port->number);
662
663 /* wait for data to drain from buffer */
664 spin_lock_irqsave(&priv->lock, flags);
665 timeout = CYPRESS_CLOSING_WAIT;
666 init_waitqueue_entry(&wait, current);
667 add_wait_queue(&port->tty->write_wait, &wait);
668 for (;;) {
669 set_current_state(TASK_INTERRUPTIBLE);
670 if (cypress_buf_data_avail(priv->buf) == 0
671 || timeout == 0 || signal_pending(current)
672 || !usb_get_intfdata(port->serial->interface))
673 break;
674 spin_unlock_irqrestore(&priv->lock, flags);
675 timeout = schedule_timeout(timeout);
676 spin_lock_irqsave(&priv->lock, flags);
677 }
678 set_current_state(TASK_RUNNING);
679 remove_wait_queue(&port->tty->write_wait, &wait);
680 /* clear out any remaining data in the buffer */
681 cypress_buf_clear(priv->buf);
682 spin_unlock_irqrestore(&priv->lock, flags);
683
684 /* wait for characters to drain from device */
685 bps = tty_get_baud_rate(port->tty);
686 if (bps > 1200)
687 timeout = max((HZ*2560)/bps,HZ/10);
688 else
689 timeout = 2*HZ;
Nishanth Aravamudan22c43862005-08-15 11:30:11 -0700690 schedule_timeout_interruptible(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
692 dbg("%s - stopping urbs", __FUNCTION__);
693 usb_kill_urb (port->interrupt_in_urb);
694 usb_kill_urb (port->interrupt_out_urb);
695
696 if (port->tty) {
697 c_cflag = port->tty->termios->c_cflag;
698 if (c_cflag & HUPCL) {
699 /* drop dtr and rts */
700 priv = usb_get_serial_port_data(port);
701 spin_lock_irqsave(&priv->lock, flags);
702 priv->line_control = 0;
703 priv->cmd_ctrl = 1;
704 spin_unlock_irqrestore(&priv->lock, flags);
705 cypress_write(port, NULL, 0);
706 }
707 }
708
709 if (stats)
710 dev_info (&port->dev, "Statistics: %d Bytes In | %d Bytes Out | %d Commands Issued\n",
711 priv->bytes_in, priv->bytes_out, priv->cmd_count);
712} /* cypress_close */
713
714
715static int cypress_write(struct usb_serial_port *port, const unsigned char *buf, int count)
716{
717 struct cypress_private *priv = usb_get_serial_port_data(port);
718 unsigned long flags;
719
720 dbg("%s - port %d, %d bytes", __FUNCTION__, port->number, count);
721
722 /* line control commands, which need to be executed immediately,
723 are not put into the buffer for obvious reasons.
724 */
725 if (priv->cmd_ctrl) {
726 count = 0;
727 goto finish;
728 }
729
730 if (!count)
731 return count;
732
733 spin_lock_irqsave(&priv->lock, flags);
734 count = cypress_buf_put(priv->buf, buf, count);
735 spin_unlock_irqrestore(&priv->lock, flags);
736
737finish:
738 cypress_send(port);
739
740 return count;
741} /* cypress_write */
742
743
744static void cypress_send(struct usb_serial_port *port)
745{
746 int count = 0, result, offset, actual_size;
747 struct cypress_private *priv = usb_get_serial_port_data(port);
748 unsigned long flags;
749
750 dbg("%s - port %d", __FUNCTION__, port->number);
751 dbg("%s - interrupt out size is %d", __FUNCTION__, port->interrupt_out_size);
752
753 spin_lock_irqsave(&priv->lock, flags);
754 if (priv->write_urb_in_use) {
755 dbg("%s - can't write, urb in use", __FUNCTION__);
756 spin_unlock_irqrestore(&priv->lock, flags);
757 return;
758 }
759 spin_unlock_irqrestore(&priv->lock, flags);
760
761 /* clear buffer */
762 memset(port->interrupt_out_urb->transfer_buffer, 0, port->interrupt_out_size);
763
764 spin_lock_irqsave(&priv->lock, flags);
765 switch (port->interrupt_out_size) {
766 case 32:
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500767 /* this is for the CY7C64013... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 offset = 2;
769 port->interrupt_out_buffer[0] = priv->line_control;
770 break;
771 case 8:
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -0500772 /* this is for the CY7C63743... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 offset = 1;
774 port->interrupt_out_buffer[0] = priv->line_control;
775 break;
776 default:
777 dbg("%s - wrong packet size", __FUNCTION__);
778 spin_unlock_irqrestore(&priv->lock, flags);
779 return;
780 }
781
782 if (priv->line_control & CONTROL_RESET)
783 priv->line_control &= ~CONTROL_RESET;
784
785 if (priv->cmd_ctrl) {
786 priv->cmd_count++;
787 dbg("%s - line control command being issued", __FUNCTION__);
788 spin_unlock_irqrestore(&priv->lock, flags);
789 goto send;
790 } else
791 spin_unlock_irqrestore(&priv->lock, flags);
792
793 count = cypress_buf_get(priv->buf, &port->interrupt_out_buffer[offset],
794 port->interrupt_out_size-offset);
795
796 if (count == 0) {
797 return;
798 }
799
800 switch (port->interrupt_out_size) {
801 case 32:
802 port->interrupt_out_buffer[1] = count;
803 break;
804 case 8:
805 port->interrupt_out_buffer[0] |= count;
806 }
807
808 dbg("%s - count is %d", __FUNCTION__, count);
809
810send:
811 spin_lock_irqsave(&priv->lock, flags);
812 priv->write_urb_in_use = 1;
813 spin_unlock_irqrestore(&priv->lock, flags);
814
815 if (priv->cmd_ctrl)
816 actual_size = 1;
817 else
818 actual_size = count + (port->interrupt_out_size == 32 ? 2 : 1);
819
820 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, port->interrupt_out_size,
821 port->interrupt_out_urb->transfer_buffer);
822
Mike Isely9aa8dae2006-08-29 22:07:04 -0500823 usb_fill_int_urb(port->interrupt_out_urb, port->serial->dev,
824 usb_sndintpipe(port->serial->dev, port->interrupt_out_endpointAddress),
825 port->interrupt_out_buffer, port->interrupt_out_size,
826 cypress_write_int_callback, port, priv->write_urb_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 result = usb_submit_urb (port->interrupt_out_urb, GFP_ATOMIC);
828 if (result) {
829 dev_err(&port->dev, "%s - failed submitting write urb, error %d\n", __FUNCTION__,
830 result);
831 priv->write_urb_in_use = 0;
832 }
833
834 spin_lock_irqsave(&priv->lock, flags);
835 if (priv->cmd_ctrl) {
836 priv->cmd_ctrl = 0;
837 }
838 priv->bytes_out += count; /* do not count the line control and size bytes */
839 spin_unlock_irqrestore(&priv->lock, flags);
840
Pete Zaitcevcf2c7482006-05-22 21:58:49 -0700841 usb_serial_port_softint(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842} /* cypress_send */
843
844
845/* returns how much space is available in the soft buffer */
846static int cypress_write_room(struct usb_serial_port *port)
847{
848 struct cypress_private *priv = usb_get_serial_port_data(port);
849 int room = 0;
850 unsigned long flags;
851
852 dbg("%s - port %d", __FUNCTION__, port->number);
853
854 spin_lock_irqsave(&priv->lock, flags);
855 room = cypress_buf_space_avail(priv->buf);
856 spin_unlock_irqrestore(&priv->lock, flags);
857
858 dbg("%s - returns %d", __FUNCTION__, room);
859 return room;
860}
861
862
863static int cypress_tiocmget (struct usb_serial_port *port, struct file *file)
864{
865 struct cypress_private *priv = usb_get_serial_port_data(port);
866 __u8 status, control;
867 unsigned int result = 0;
868 unsigned long flags;
869
870 dbg("%s - port %d", __FUNCTION__, port->number);
871
872 spin_lock_irqsave(&priv->lock, flags);
873 control = priv->line_control;
874 status = priv->current_status;
875 spin_unlock_irqrestore(&priv->lock, flags);
876
877 result = ((control & CONTROL_DTR) ? TIOCM_DTR : 0)
878 | ((control & CONTROL_RTS) ? TIOCM_RTS : 0)
879 | ((status & UART_CTS) ? TIOCM_CTS : 0)
880 | ((status & UART_DSR) ? TIOCM_DSR : 0)
881 | ((status & UART_RI) ? TIOCM_RI : 0)
882 | ((status & UART_CD) ? TIOCM_CD : 0);
883
884 dbg("%s - result = %x", __FUNCTION__, result);
885
886 return result;
887}
888
889
890static int cypress_tiocmset (struct usb_serial_port *port, struct file *file,
891 unsigned int set, unsigned int clear)
892{
893 struct cypress_private *priv = usb_get_serial_port_data(port);
894 unsigned long flags;
895
896 dbg("%s - port %d", __FUNCTION__, port->number);
897
898 spin_lock_irqsave(&priv->lock, flags);
899 if (set & TIOCM_RTS)
900 priv->line_control |= CONTROL_RTS;
901 if (set & TIOCM_DTR)
902 priv->line_control |= CONTROL_DTR;
903 if (clear & TIOCM_RTS)
904 priv->line_control &= ~CONTROL_RTS;
905 if (clear & TIOCM_DTR)
906 priv->line_control &= ~CONTROL_DTR;
907 spin_unlock_irqrestore(&priv->lock, flags);
908
909 priv->cmd_ctrl = 1;
910 return cypress_write(port, NULL, 0);
911}
912
913
914static int cypress_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg)
915{
916 struct cypress_private *priv = usb_get_serial_port_data(port);
917
918 dbg("%s - port %d, cmd 0x%.4x", __FUNCTION__, port->number, cmd);
919
920 switch (cmd) {
921 case TIOCGSERIAL:
922 if (copy_to_user((void __user *)arg, port->tty->termios, sizeof(struct termios))) {
923 return -EFAULT;
924 }
925 return (0);
926 break;
927 case TIOCSSERIAL:
928 if (copy_from_user(port->tty->termios, (void __user *)arg, sizeof(struct termios))) {
929 return -EFAULT;
930 }
931 /* here we need to call cypress_set_termios to invoke the new settings */
932 cypress_set_termios(port, &priv->tmp_termios);
933 return (0);
934 break;
935 /* these are called when setting baud rate from gpsd */
936 case TCGETS:
937 if (copy_to_user((void __user *)arg, port->tty->termios, sizeof(struct termios))) {
938 return -EFAULT;
939 }
940 return (0);
941 break;
942 case TCSETS:
943 if (copy_from_user(port->tty->termios, (void __user *)arg, sizeof(struct termios))) {
944 return -EFAULT;
945 }
946 /* here we need to call cypress_set_termios to invoke the new settings */
947 cypress_set_termios(port, &priv->tmp_termios);
948 return (0);
949 break;
950 /* This code comes from drivers/char/serial.c and ftdi_sio.c */
951 case TIOCMIWAIT:
952 while (priv != NULL) {
953 interruptible_sleep_on(&priv->delta_msr_wait);
954 /* see if a signal did it */
955 if (signal_pending(current))
956 return -ERESTARTSYS;
957 else {
958 char diff = priv->diff_status;
959
960 if (diff == 0) {
961 return -EIO; /* no change => error */
962 }
963
964 /* consume all events */
965 priv->diff_status = 0;
966
967 /* return 0 if caller wanted to know about these bits */
968 if ( ((arg & TIOCM_RNG) && (diff & UART_RI)) ||
969 ((arg & TIOCM_DSR) && (diff & UART_DSR)) ||
970 ((arg & TIOCM_CD) && (diff & UART_CD)) ||
971 ((arg & TIOCM_CTS) && (diff & UART_CTS)) ) {
972 return 0;
973 }
974 /* otherwise caller can't care less about what happened,
975 * and so we continue to wait for more events.
976 */
977 }
978 }
979 return 0;
980 break;
981 default:
982 break;
983 }
984
985 dbg("%s - arg not supported - it was 0x%04x - check include/asm/ioctls.h", __FUNCTION__, cmd);
986
987 return -ENOIOCTLCMD;
988} /* cypress_ioctl */
989
990
Lonnie Mendezb9db07f2005-07-12 17:21:31 -0500991static void cypress_set_termios (struct usb_serial_port *port,
992 struct termios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993{
994 struct cypress_private *priv = usb_get_serial_port_data(port);
995 struct tty_struct *tty;
996 int data_bits, stop_bits, parity_type, parity_enable;
997 unsigned cflag, iflag, baud_mask;
998 unsigned long flags;
999 __u8 oldlines;
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001000 int linechange = 0;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001001
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 dbg("%s - port %d", __FUNCTION__, port->number);
1003
1004 tty = port->tty;
1005 if ((!tty) || (!tty->termios)) {
1006 dbg("%s - no tty structures", __FUNCTION__);
1007 return;
1008 }
1009
1010 spin_lock_irqsave(&priv->lock, flags);
1011 if (!priv->termios_initialized) {
1012 if (priv->chiptype == CT_EARTHMATE) {
1013 *(tty->termios) = tty_std_termios;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001014 tty->termios->c_cflag = B4800 | CS8 | CREAD | HUPCL |
1015 CLOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 } else if (priv->chiptype == CT_CYPHIDCOM) {
1017 *(tty->termios) = tty_std_termios;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001018 tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
1019 CLOCAL;
Lonnie Mendeza5c44e22006-03-01 10:45:24 -06001020 } else if (priv->chiptype == CT_CA42V2) {
1021 *(tty->termios) = tty_std_termios;
1022 tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
1023 CLOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 }
1025 priv->termios_initialized = 1;
1026 }
1027 spin_unlock_irqrestore(&priv->lock, flags);
1028
1029 cflag = tty->termios->c_cflag;
1030 iflag = tty->termios->c_iflag;
1031
1032 /* check if there are new settings */
1033 if (old_termios) {
1034 if ((cflag != old_termios->c_cflag) ||
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001035 (RELEVANT_IFLAG(iflag) !=
1036 RELEVANT_IFLAG(old_termios->c_iflag))) {
1037 dbg("%s - attempting to set new termios settings",
1038 __FUNCTION__);
1039 /* should make a copy of this in case something goes
1040 * wrong in the function, we can restore it */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 spin_lock_irqsave(&priv->lock, flags);
1042 priv->tmp_termios = *(tty->termios);
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001043 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 } else {
1045 dbg("%s - nothing to do, exiting", __FUNCTION__);
1046 return;
1047 }
1048 } else
1049 return;
1050
1051 /* set number of data bits, parity, stop bits */
1052 /* when parity is disabled the parity type bit is ignored */
1053
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001054 /* 1 means 2 stop bits, 0 means 1 stop bit */
1055 stop_bits = cflag & CSTOPB ? 1 : 0;
1056
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 if (cflag & PARENB) {
1058 parity_enable = 1;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001059 /* 1 means odd parity, 0 means even parity */
1060 parity_type = cflag & PARODD ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 } else
1062 parity_enable = parity_type = 0;
1063
1064 if (cflag & CSIZE) {
1065 switch (cflag & CSIZE) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001066 case CS5:
1067 data_bits = 0;
1068 break;
1069 case CS6:
1070 data_bits = 1;
1071 break;
1072 case CS7:
1073 data_bits = 2;
1074 break;
1075 case CS8:
1076 data_bits = 3;
1077 break;
1078 default:
1079 err("%s - CSIZE was set, but not CS5-CS8",
1080 __FUNCTION__);
1081 data_bits = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 }
1083 } else
1084 data_bits = 3;
1085
1086 spin_lock_irqsave(&priv->lock, flags);
1087 oldlines = priv->line_control;
1088 if ((cflag & CBAUD) == B0) {
1089 /* drop dtr and rts */
1090 dbg("%s - dropping the lines, baud rate 0bps", __FUNCTION__);
1091 baud_mask = B0;
1092 priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
1093 } else {
1094 baud_mask = (cflag & CBAUD);
1095 switch(baud_mask) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001096 case B300:
1097 dbg("%s - setting baud 300bps", __FUNCTION__);
1098 break;
1099 case B600:
1100 dbg("%s - setting baud 600bps", __FUNCTION__);
1101 break;
1102 case B1200:
1103 dbg("%s - setting baud 1200bps", __FUNCTION__);
1104 break;
1105 case B2400:
1106 dbg("%s - setting baud 2400bps", __FUNCTION__);
1107 break;
1108 case B4800:
1109 dbg("%s - setting baud 4800bps", __FUNCTION__);
1110 break;
1111 case B9600:
1112 dbg("%s - setting baud 9600bps", __FUNCTION__);
1113 break;
1114 case B19200:
1115 dbg("%s - setting baud 19200bps", __FUNCTION__);
1116 break;
1117 case B38400:
1118 dbg("%s - setting baud 38400bps", __FUNCTION__);
1119 break;
1120 case B57600:
1121 dbg("%s - setting baud 57600bps", __FUNCTION__);
1122 break;
1123 case B115200:
1124 dbg("%s - setting baud 115200bps", __FUNCTION__);
1125 break;
1126 default:
1127 dbg("%s - unknown masked baud rate", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 }
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001129 priv->line_control = (CONTROL_DTR | CONTROL_RTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 }
1131 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001133 dbg("%s - sending %d stop_bits, %d parity_enable, %d parity_type, "
1134 "%d data_bits (+5)", __FUNCTION__, stop_bits,
1135 parity_enable, parity_type, data_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001137 cypress_serial_control(port, baud_mask, data_bits, stop_bits,
1138 parity_enable, parity_type, 0, CYPRESS_SET_CONFIG);
1139
1140 /* we perform a CYPRESS_GET_CONFIG so that the current settings are
1141 * filled into the private structure this should confirm that all is
1142 * working if it returns what we just set */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 cypress_serial_control(port, 0, 0, 0, 0, 0, 0, CYPRESS_GET_CONFIG);
1144
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001145 /* Here we can define custom tty settings for devices; the main tty
1146 * termios flag base comes from empeg.c */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001148 spin_lock_irqsave(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 if ( (priv->chiptype == CT_EARTHMATE) && (priv->baud_rate == 4800) ) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001150 dbg("Using custom termios settings for a baud rate of "
1151 "4800bps.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 /* define custom termios settings for NMEA protocol */
1153
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 tty->termios->c_iflag /* input modes - */
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001155 &= ~(IGNBRK /* disable ignore break */
1156 | BRKINT /* disable break causes interrupt */
1157 | PARMRK /* disable mark parity errors */
1158 | ISTRIP /* disable clear high bit of input char */
1159 | INLCR /* disable translate NL to CR */
1160 | IGNCR /* disable ignore CR */
1161 | ICRNL /* disable translate CR to NL */
1162 | IXON); /* disable enable XON/XOFF flow control */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001164 tty->termios->c_oflag /* output modes */
1165 &= ~OPOST; /* disable postprocess output char */
1166
1167 tty->termios->c_lflag /* line discipline modes */
1168 &= ~(ECHO /* disable echo input characters */
1169 | ECHONL /* disable echo new line */
1170 | ICANON /* disable erase, kill, werase, and rprnt
1171 special characters */
1172 | ISIG /* disable interrupt, quit, and suspend
1173 special characters */
1174 | IEXTEN); /* disable non-POSIX special characters */
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001175 } /* CT_CYPHIDCOM: Application should handle this for device */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 linechange = (priv->line_control != oldlines);
1178 spin_unlock_irqrestore(&priv->lock, flags);
1179
1180 /* if necessary, set lines */
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001181 if (linechange) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 priv->cmd_ctrl = 1;
1183 cypress_write(port, NULL, 0);
1184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185} /* cypress_set_termios */
1186
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001187
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188/* returns amount of data still left in soft buffer */
1189static int cypress_chars_in_buffer(struct usb_serial_port *port)
1190{
1191 struct cypress_private *priv = usb_get_serial_port_data(port);
1192 int chars = 0;
1193 unsigned long flags;
1194
1195 dbg("%s - port %d", __FUNCTION__, port->number);
1196
1197 spin_lock_irqsave(&priv->lock, flags);
1198 chars = cypress_buf_data_avail(priv->buf);
1199 spin_unlock_irqrestore(&priv->lock, flags);
1200
1201 dbg("%s - returns %d", __FUNCTION__, chars);
1202 return chars;
1203}
1204
1205
1206static void cypress_throttle (struct usb_serial_port *port)
1207{
1208 struct cypress_private *priv = usb_get_serial_port_data(port);
1209 unsigned long flags;
1210
1211 dbg("%s - port %d", __FUNCTION__, port->number);
1212
1213 spin_lock_irqsave(&priv->lock, flags);
1214 priv->rx_flags = THROTTLED;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001215 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216}
1217
1218
1219static void cypress_unthrottle (struct usb_serial_port *port)
1220{
1221 struct cypress_private *priv = usb_get_serial_port_data(port);
1222 int actually_throttled, result;
1223 unsigned long flags;
1224
1225 dbg("%s - port %d", __FUNCTION__, port->number);
1226
1227 spin_lock_irqsave(&priv->lock, flags);
1228 actually_throttled = priv->rx_flags & ACTUALLY_THROTTLED;
1229 priv->rx_flags = 0;
1230 spin_unlock_irqrestore(&priv->lock, flags);
1231
1232 if (actually_throttled) {
1233 port->interrupt_in_urb->dev = port->serial->dev;
1234
1235 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
1236 if (result)
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001237 dev_err(&port->dev, "%s - failed submitting read urb, "
1238 "error %d\n", __FUNCTION__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 }
1240}
1241
1242
1243static void cypress_read_int_callback(struct urb *urb, struct pt_regs *regs)
1244{
1245 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
1246 struct cypress_private *priv = usb_get_serial_port_data(port);
1247 struct tty_struct *tty;
1248 unsigned char *data = urb->transfer_buffer;
1249 unsigned long flags;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001250 char tty_flag = TTY_NORMAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 int havedata = 0;
1252 int bytes = 0;
1253 int result;
1254 int i = 0;
1255
1256 dbg("%s - port %d", __FUNCTION__, port->number);
1257
1258 if (urb->status) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001259 dbg("%s - nonzero read status received: %d", __FUNCTION__,
1260 urb->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 return;
1262 }
1263
1264 spin_lock_irqsave(&priv->lock, flags);
1265 if (priv->rx_flags & THROTTLED) {
1266 dbg("%s - now throttling", __FUNCTION__);
1267 priv->rx_flags |= ACTUALLY_THROTTLED;
1268 spin_unlock_irqrestore(&priv->lock, flags);
1269 return;
1270 }
1271 spin_unlock_irqrestore(&priv->lock, flags);
1272
1273 tty = port->tty;
1274 if (!tty) {
1275 dbg("%s - bad tty pointer - exiting", __FUNCTION__);
1276 return;
1277 }
1278
1279 spin_lock_irqsave(&priv->lock, flags);
1280 switch(urb->actual_length) {
1281 case 32:
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001282 /* This is for the CY7C64013... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 priv->current_status = data[0] & 0xF8;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001284 bytes = data[1] + 2;
1285 i = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 if (bytes > 2)
1287 havedata = 1;
1288 break;
1289 case 8:
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001290 /* This is for the CY7C63743... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 priv->current_status = data[0] & 0xF8;
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001292 bytes = (data[0] & 0x07) + 1;
1293 i = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 if (bytes > 1)
1295 havedata = 1;
1296 break;
1297 default:
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001298 dbg("%s - wrong packet size - received %d bytes",
1299 __FUNCTION__, urb->actual_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 spin_unlock_irqrestore(&priv->lock, flags);
1301 goto continue_read;
1302 }
1303 spin_unlock_irqrestore(&priv->lock, flags);
1304
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001305 usb_serial_debug_data (debug, &port->dev, __FUNCTION__,
1306 urb->actual_length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
1308 spin_lock_irqsave(&priv->lock, flags);
1309 /* check to see if status has changed */
1310 if (priv != NULL) {
1311 if (priv->current_status != priv->prev_status) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001312 priv->diff_status |= priv->current_status ^
1313 priv->prev_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 wake_up_interruptible(&priv->delta_msr_wait);
1315 priv->prev_status = priv->current_status;
1316 }
1317 }
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001318 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001320 /* hangup, as defined in acm.c... this might be a bad place for it
1321 * though */
1322 if (tty && !(tty->termios->c_cflag & CLOCAL) &&
1323 !(priv->current_status & UART_CD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 dbg("%s - calling hangup", __FUNCTION__);
1325 tty_hangup(tty);
1326 goto continue_read;
1327 }
1328
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001329 /* There is one error bit... I'm assuming it is a parity error
1330 * indicator as the generic firmware will set this bit to 1 if a
1331 * parity error occurs.
1332 * I can not find reference to any other error events. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 spin_lock_irqsave(&priv->lock, flags);
1334 if (priv->current_status & CYP_ERROR) {
1335 spin_unlock_irqrestore(&priv->lock, flags);
1336 tty_flag = TTY_PARITY;
1337 dbg("%s - Parity Error detected", __FUNCTION__);
1338 } else
1339 spin_unlock_irqrestore(&priv->lock, flags);
1340
1341 /* process read if there is data other than line status */
1342 if (tty && (bytes > i)) {
Alan Cox33f0f882006-01-09 20:54:13 -08001343 bytes = tty_buffer_request_room(tty, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 for (; i < bytes ; ++i) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001345 dbg("pushing byte number %d - %d - %c", i, data[i],
1346 data[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 tty_insert_flip_char(tty, data[i], tty_flag);
1348 }
1349 tty_flip_buffer_push(port->tty);
1350 }
1351
1352 spin_lock_irqsave(&priv->lock, flags);
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001353 /* control and status byte(s) are also counted */
1354 priv->bytes_in += bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 spin_unlock_irqrestore(&priv->lock, flags);
1356
1357continue_read:
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001358
1359 /* Continue trying to always read... unless the port has closed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
1361 if (port->open_count > 0) {
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001362 usb_fill_int_urb(port->interrupt_in_urb, port->serial->dev,
1363 usb_rcvintpipe(port->serial->dev,
1364 port->interrupt_in_endpointAddress),
1365 port->interrupt_in_urb->transfer_buffer,
1366 port->interrupt_in_urb->transfer_buffer_length,
Mike Isely0257fa92006-08-29 22:06:59 -05001367 cypress_read_int_callback, port, priv->read_urb_interval);
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001368 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
1369 if (result)
1370 dev_err(&urb->dev->dev, "%s - failed resubmitting "
1371 "read urb, error %d\n", __FUNCTION__,
1372 result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 }
Lonnie Mendezb9db07f2005-07-12 17:21:31 -05001374
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 return;
1376} /* cypress_read_int_callback */
1377
1378
1379static void cypress_write_int_callback(struct urb *urb, struct pt_regs *regs)
1380{
1381 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
1382 struct cypress_private *priv = usb_get_serial_port_data(port);
1383 int result;
1384
1385 dbg("%s - port %d", __FUNCTION__, port->number);
1386
1387 switch (urb->status) {
1388 case 0:
1389 /* success */
1390 break;
1391 case -ECONNRESET:
1392 case -ENOENT:
1393 case -ESHUTDOWN:
1394 /* this urb is terminated, clean up */
1395 dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status);
1396 priv->write_urb_in_use = 0;
1397 return;
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001398 case -EPIPE: /* no break needed */
1399 usb_clear_halt(port->serial->dev, 0x02);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 default:
1401 /* error in the urb, so we have to resubmit it */
1402 dbg("%s - Overflow in write", __FUNCTION__);
1403 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
1404 port->interrupt_out_urb->transfer_buffer_length = 1;
1405 port->interrupt_out_urb->dev = port->serial->dev;
1406 result = usb_submit_urb(port->interrupt_out_urb, GFP_ATOMIC);
1407 if (result)
1408 dev_err(&urb->dev->dev, "%s - failed resubmitting write urb, error %d\n",
1409 __FUNCTION__, result);
1410 else
1411 return;
1412 }
1413
1414 priv->write_urb_in_use = 0;
1415
1416 /* send any buffered data */
1417 cypress_send(port);
1418}
1419
1420
1421/*****************************************************************************
1422 * Write buffer functions - buffering code from pl2303 used
1423 *****************************************************************************/
1424
1425/*
1426 * cypress_buf_alloc
1427 *
1428 * Allocate a circular buffer and all associated memory.
1429 */
1430
1431static struct cypress_buf *cypress_buf_alloc(unsigned int size)
1432{
1433
1434 struct cypress_buf *cb;
1435
1436
1437 if (size == 0)
1438 return NULL;
1439
1440 cb = (struct cypress_buf *)kmalloc(sizeof(struct cypress_buf), GFP_KERNEL);
1441 if (cb == NULL)
1442 return NULL;
1443
1444 cb->buf_buf = kmalloc(size, GFP_KERNEL);
1445 if (cb->buf_buf == NULL) {
1446 kfree(cb);
1447 return NULL;
1448 }
1449
1450 cb->buf_size = size;
1451 cb->buf_get = cb->buf_put = cb->buf_buf;
1452
1453 return cb;
1454
1455}
1456
1457
1458/*
1459 * cypress_buf_free
1460 *
1461 * Free the buffer and all associated memory.
1462 */
1463
1464static void cypress_buf_free(struct cypress_buf *cb)
1465{
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001466 if (cb) {
1467 kfree(cb->buf_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 kfree(cb);
1469 }
1470}
1471
1472
1473/*
1474 * cypress_buf_clear
1475 *
1476 * Clear out all data in the circular buffer.
1477 */
1478
1479static void cypress_buf_clear(struct cypress_buf *cb)
1480{
1481 if (cb != NULL)
1482 cb->buf_get = cb->buf_put;
1483 /* equivalent to a get of all data available */
1484}
1485
1486
1487/*
1488 * cypress_buf_data_avail
1489 *
1490 * Return the number of bytes of data available in the circular
1491 * buffer.
1492 */
1493
1494static unsigned int cypress_buf_data_avail(struct cypress_buf *cb)
1495{
1496 if (cb != NULL)
1497 return ((cb->buf_size + cb->buf_put - cb->buf_get) % cb->buf_size);
1498 else
1499 return 0;
1500}
1501
1502
1503/*
1504 * cypress_buf_space_avail
1505 *
1506 * Return the number of bytes of space available in the circular
1507 * buffer.
1508 */
1509
1510static unsigned int cypress_buf_space_avail(struct cypress_buf *cb)
1511{
1512 if (cb != NULL)
1513 return ((cb->buf_size + cb->buf_get - cb->buf_put - 1) % cb->buf_size);
1514 else
1515 return 0;
1516}
1517
1518
1519/*
1520 * cypress_buf_put
1521 *
1522 * Copy data data from a user buffer and put it into the circular buffer.
1523 * Restrict to the amount of space available.
1524 *
1525 * Return the number of bytes copied.
1526 */
1527
1528static unsigned int cypress_buf_put(struct cypress_buf *cb, const char *buf,
1529 unsigned int count)
1530{
1531
1532 unsigned int len;
1533
1534
1535 if (cb == NULL)
1536 return 0;
1537
1538 len = cypress_buf_space_avail(cb);
1539 if (count > len)
1540 count = len;
1541
1542 if (count == 0)
1543 return 0;
1544
1545 len = cb->buf_buf + cb->buf_size - cb->buf_put;
1546 if (count > len) {
1547 memcpy(cb->buf_put, buf, len);
1548 memcpy(cb->buf_buf, buf+len, count - len);
1549 cb->buf_put = cb->buf_buf + count - len;
1550 } else {
1551 memcpy(cb->buf_put, buf, count);
1552 if (count < len)
1553 cb->buf_put += count;
1554 else /* count == len */
1555 cb->buf_put = cb->buf_buf;
1556 }
1557
1558 return count;
1559
1560}
1561
1562
1563/*
1564 * cypress_buf_get
1565 *
1566 * Get data from the circular buffer and copy to the given buffer.
1567 * Restrict to the amount of data available.
1568 *
1569 * Return the number of bytes copied.
1570 */
1571
1572static unsigned int cypress_buf_get(struct cypress_buf *cb, char *buf,
1573 unsigned int count)
1574{
1575
1576 unsigned int len;
1577
1578
1579 if (cb == NULL)
1580 return 0;
1581
1582 len = cypress_buf_data_avail(cb);
1583 if (count > len)
1584 count = len;
1585
1586 if (count == 0)
1587 return 0;
1588
1589 len = cb->buf_buf + cb->buf_size - cb->buf_get;
1590 if (count > len) {
1591 memcpy(buf, cb->buf_get, len);
1592 memcpy(buf+len, cb->buf_buf, count - len);
1593 cb->buf_get = cb->buf_buf + count - len;
1594 } else {
1595 memcpy(buf, cb->buf_get, count);
1596 if (count < len)
1597 cb->buf_get += count;
1598 else /* count == len */
1599 cb->buf_get = cb->buf_buf;
1600 }
1601
1602 return count;
1603
1604}
1605
1606/*****************************************************************************
1607 * Module functions
1608 *****************************************************************************/
1609
1610static int __init cypress_init(void)
1611{
1612 int retval;
1613
1614 dbg("%s", __FUNCTION__);
1615
1616 retval = usb_serial_register(&cypress_earthmate_device);
1617 if (retval)
1618 goto failed_em_register;
1619 retval = usb_serial_register(&cypress_hidcom_device);
1620 if (retval)
1621 goto failed_hidcom_register;
Lonnie Mendeza5c44e22006-03-01 10:45:24 -06001622 retval = usb_serial_register(&cypress_ca42v2_device);
1623 if (retval)
1624 goto failed_ca42v2_register;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 retval = usb_register(&cypress_driver);
1626 if (retval)
1627 goto failed_usb_register;
1628
1629 info(DRIVER_DESC " " DRIVER_VERSION);
1630 return 0;
1631failed_usb_register:
1632 usb_deregister(&cypress_driver);
Lonnie Mendeza5c44e22006-03-01 10:45:24 -06001633failed_ca42v2_register:
1634 usb_serial_deregister(&cypress_ca42v2_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635failed_hidcom_register:
1636 usb_serial_deregister(&cypress_hidcom_device);
1637failed_em_register:
1638 usb_serial_deregister(&cypress_earthmate_device);
1639
1640 return retval;
1641}
1642
1643
1644static void __exit cypress_exit (void)
1645{
1646 dbg("%s", __FUNCTION__);
1647
1648 usb_deregister (&cypress_driver);
1649 usb_serial_deregister (&cypress_earthmate_device);
1650 usb_serial_deregister (&cypress_hidcom_device);
Lonnie Mendeza5c44e22006-03-01 10:45:24 -06001651 usb_serial_deregister (&cypress_ca42v2_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652}
1653
1654
1655module_init(cypress_init);
1656module_exit(cypress_exit);
1657
1658MODULE_AUTHOR( DRIVER_AUTHOR );
1659MODULE_DESCRIPTION( DRIVER_DESC );
1660MODULE_VERSION( DRIVER_VERSION );
1661MODULE_LICENSE("GPL");
1662
1663module_param(debug, bool, S_IRUGO | S_IWUSR);
1664MODULE_PARM_DESC(debug, "Debug enabled or not");
1665module_param(stats, bool, S_IRUGO | S_IWUSR);
1666MODULE_PARM_DESC(stats, "Enable statistics or not");
Lonnie Mendez3cb4a4f2005-05-03 17:02:20 -05001667module_param(interval, int, S_IRUGO | S_IWUSR);
1668MODULE_PARM_DESC(interval, "Overrides interrupt interval");