USB: serial: remove unnecessary reinitialisations of urb->dev
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / usb / serial / cypress_m8.c
CommitLineData
1da177e4
LT
1/*
2 * USB Cypress M8 driver
3 *
4 * Copyright (C) 2004
813a224f 5 * Lonnie Mendez (dignome@gmail.com)
1da177e4
LT
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 *
813a224f
AC
14 * See Documentation/usb/usb-serial.txt for more information on using this
15 * driver
1da177e4
LT
16 *
17 * See http://geocities.com/i0xox0i for information on this driver and the
18 * earthmate usb device.
1da177e4
LT
19 */
20
813a224f
AC
21/* Thanks to Neil Whelchel for writing the first cypress m8 implementation
22 for linux. */
1da177e4
LT
23/* Thanks to cypress for providing references for the hid reports. */
24/* Thanks to Jiang Zhang for providing links and for general help. */
813a224f 25/* Code originates and was built up from ftdi_sio, belkin, pl2303 and others.*/
1da177e4
LT
26
27
1da177e4
LT
28#include <linux/kernel.h>
29#include <linux/errno.h>
30#include <linux/init.h>
31#include <linux/slab.h>
32#include <linux/tty.h>
33#include <linux/tty_driver.h>
34#include <linux/tty_flip.h>
35#include <linux/module.h>
36#include <linux/moduleparam.h>
37#include <linux/spinlock.h>
38#include <linux/usb.h>
a969888c 39#include <linux/usb/serial.h>
1da177e4 40#include <linux/serial.h>
117fb8d0 41#include <linux/kfifo.h>
1da177e4 42#include <linux/delay.h>
813a224f 43#include <linux/uaccess.h>
0f2c2d7b 44#include <asm/unaligned.h>
1da177e4 45
1da177e4
LT
46#include "cypress_m8.h"
47
48
64319dd7 49static int debug;
1da177e4 50static int stats;
3cb4a4f7 51static int interval;
c312659c 52static int unstable_bauds;
1da177e4
LT
53
54/*
55 * Version Information
56 */
117fb8d0 57#define DRIVER_VERSION "v1.10"
1da177e4
LT
58#define DRIVER_AUTHOR "Lonnie Mendez <dignome@gmail.com>, Neil Whelchel <koyama@firstlight.net>"
59#define DRIVER_DESC "Cypress USB to Serial Driver"
60
61/* write buffer size defines */
62#define CYPRESS_BUF_SIZE 1024
1da177e4 63
7d40d7e8 64static const struct usb_device_id id_table_earthmate[] = {
1da177e4 65 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB) },
25b6f08e 66 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB_LT20) },
1da177e4
LT
67 { } /* Terminating entry */
68};
69
7d40d7e8 70static const struct usb_device_id id_table_cyphidcomrs232[] = {
1da177e4 71 { USB_DEVICE(VENDOR_ID_CYPRESS, PRODUCT_ID_CYPHIDCOM) },
6f6f06ee 72 { USB_DEVICE(VENDOR_ID_POWERCOM, PRODUCT_ID_UPS) },
1da177e4
LT
73 { } /* Terminating entry */
74};
75
7d40d7e8 76static const struct usb_device_id id_table_nokiaca42v2[] = {
a5c44e29
LM
77 { USB_DEVICE(VENDOR_ID_DAZZLE, PRODUCT_ID_CA42) },
78 { } /* Terminating entry */
79};
80
7d40d7e8 81static const struct usb_device_id id_table_combined[] = {
1da177e4 82 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB) },
25b6f08e 83 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB_LT20) },
1da177e4 84 { USB_DEVICE(VENDOR_ID_CYPRESS, PRODUCT_ID_CYPHIDCOM) },
6f6f06ee 85 { USB_DEVICE(VENDOR_ID_POWERCOM, PRODUCT_ID_UPS) },
a5c44e29 86 { USB_DEVICE(VENDOR_ID_DAZZLE, PRODUCT_ID_CA42) },
1da177e4
LT
87 { } /* Terminating entry */
88};
89
813a224f 90MODULE_DEVICE_TABLE(usb, id_table_combined);
1da177e4
LT
91
92static struct usb_driver cypress_driver = {
93 .name = "cypress",
94 .probe = usb_serial_probe,
95 .disconnect = usb_serial_disconnect,
96 .id_table = id_table_combined,
ba9dc657 97 .no_dynamic_id = 1,
1da177e4
LT
98};
99
3416eaa1
MI
100enum packet_format {
101 packet_format_1, /* b0:status, b1:payload count */
102 packet_format_2 /* b0[7:3]:status, b0[2:0]:payload count */
103};
104
1da177e4
LT
105struct cypress_private {
106 spinlock_t lock; /* private lock */
107 int chiptype; /* identifier of device, for quirks/etc */
108 int bytes_in; /* used for statistics */
109 int bytes_out; /* used for statistics */
110 int cmd_count; /* used for statistics */
111 int cmd_ctrl; /* always set this to 1 before issuing a command */
117fb8d0 112 struct kfifo write_fifo; /* write fifo */
1da177e4 113 int write_urb_in_use; /* write urb in use indicator */
0257fa9f
MI
114 int write_urb_interval; /* interval to use for write urb */
115 int read_urb_interval; /* interval to use for read urb */
78aef519 116 int comm_is_ok; /* true if communication is (still) ok */
1da177e4
LT
117 int termios_initialized;
118 __u8 line_control; /* holds dtr / rts value */
119 __u8 current_status; /* received from last read - info on dsr,cts,cd,ri,etc */
120 __u8 current_config; /* stores the current configuration byte */
121 __u8 rx_flags; /* throttling - used from whiteheat/ftdi_sio */
3416eaa1 122 enum packet_format pkt_fmt; /* format to use for packet send / receive */
3d6aa320 123 int get_cfg_unsafe; /* If true, the CYPRESS_GET_CONFIG is unsafe */
813a224f
AC
124 int baud_rate; /* stores current baud rate in
125 integer form */
1da177e4
LT
126 int isthrottled; /* if throttled, discard reads */
127 wait_queue_head_t delta_msr_wait; /* used for TIOCMIWAIT */
128 char prev_status, diff_status; /* used for TIOCMIWAIT */
3ad2f3fb 129 /* we pass a pointer to this as the argument sent to
813a224f 130 cypress_set_termios old_termios */
606d099c 131 struct ktermios tmp_termios; /* stores the old termios settings */
1da177e4
LT
132};
133
1da177e4 134/* function prototypes for the Cypress USB to serial device */
813a224f
AC
135static int cypress_earthmate_startup(struct usb_serial *serial);
136static int cypress_hidcom_startup(struct usb_serial *serial);
137static int cypress_ca42v2_startup(struct usb_serial *serial);
f9c99bb8 138static void cypress_release(struct usb_serial *serial);
a509a7e4 139static int cypress_open(struct tty_struct *tty, struct usb_serial_port *port);
335f8514
AC
140static void cypress_close(struct usb_serial_port *port);
141static void cypress_dtr_rts(struct usb_serial_port *port, int on);
813a224f
AC
142static int cypress_write(struct tty_struct *tty, struct usb_serial_port *port,
143 const unsigned char *buf, int count);
144static void cypress_send(struct usb_serial_port *port);
145static int cypress_write_room(struct tty_struct *tty);
00a0d0d6 146static int cypress_ioctl(struct tty_struct *tty,
813a224f
AC
147 unsigned int cmd, unsigned long arg);
148static void cypress_set_termios(struct tty_struct *tty,
149 struct usb_serial_port *port, struct ktermios *old);
60b33c13 150static int cypress_tiocmget(struct tty_struct *tty);
20b9d177 151static int cypress_tiocmset(struct tty_struct *tty,
813a224f
AC
152 unsigned int set, unsigned int clear);
153static int cypress_chars_in_buffer(struct tty_struct *tty);
154static void cypress_throttle(struct tty_struct *tty);
155static void cypress_unthrottle(struct tty_struct *tty);
156static void cypress_set_dead(struct usb_serial_port *port);
157static void cypress_read_int_callback(struct urb *urb);
158static void cypress_write_int_callback(struct urb *urb);
1da177e4 159
ea65370d 160static struct usb_serial_driver cypress_earthmate_device = {
18fcac35
GKH
161 .driver = {
162 .owner = THIS_MODULE,
269bda1c 163 .name = "earthmate",
18fcac35 164 },
269bda1c 165 .description = "DeLorme Earthmate USB",
d9b1b787 166 .usb_driver = &cypress_driver,
1da177e4 167 .id_table = id_table_earthmate,
1da177e4
LT
168 .num_ports = 1,
169 .attach = cypress_earthmate_startup,
f9c99bb8 170 .release = cypress_release,
1da177e4
LT
171 .open = cypress_open,
172 .close = cypress_close,
335f8514 173 .dtr_rts = cypress_dtr_rts,
1da177e4
LT
174 .write = cypress_write,
175 .write_room = cypress_write_room,
176 .ioctl = cypress_ioctl,
177 .set_termios = cypress_set_termios,
178 .tiocmget = cypress_tiocmget,
179 .tiocmset = cypress_tiocmset,
180 .chars_in_buffer = cypress_chars_in_buffer,
181 .throttle = cypress_throttle,
182 .unthrottle = cypress_unthrottle,
183 .read_int_callback = cypress_read_int_callback,
184 .write_int_callback = cypress_write_int_callback,
185};
186
ea65370d 187static struct usb_serial_driver cypress_hidcom_device = {
18fcac35
GKH
188 .driver = {
189 .owner = THIS_MODULE,
269bda1c 190 .name = "cyphidcom",
18fcac35 191 },
269bda1c 192 .description = "HID->COM RS232 Adapter",
d9b1b787 193 .usb_driver = &cypress_driver,
1da177e4 194 .id_table = id_table_cyphidcomrs232,
1da177e4
LT
195 .num_ports = 1,
196 .attach = cypress_hidcom_startup,
f9c99bb8 197 .release = cypress_release,
1da177e4
LT
198 .open = cypress_open,
199 .close = cypress_close,
335f8514 200 .dtr_rts = cypress_dtr_rts,
1da177e4
LT
201 .write = cypress_write,
202 .write_room = cypress_write_room,
203 .ioctl = cypress_ioctl,
204 .set_termios = cypress_set_termios,
205 .tiocmget = cypress_tiocmget,
206 .tiocmset = cypress_tiocmset,
207 .chars_in_buffer = cypress_chars_in_buffer,
208 .throttle = cypress_throttle,
209 .unthrottle = cypress_unthrottle,
210 .read_int_callback = cypress_read_int_callback,
211 .write_int_callback = cypress_write_int_callback,
212};
213
a5c44e29
LM
214static struct usb_serial_driver cypress_ca42v2_device = {
215 .driver = {
216 .owner = THIS_MODULE,
813a224f 217 .name = "nokiaca42v2",
a5c44e29
LM
218 },
219 .description = "Nokia CA-42 V2 Adapter",
d9b1b787 220 .usb_driver = &cypress_driver,
a5c44e29 221 .id_table = id_table_nokiaca42v2,
a5c44e29
LM
222 .num_ports = 1,
223 .attach = cypress_ca42v2_startup,
f9c99bb8 224 .release = cypress_release,
a5c44e29
LM
225 .open = cypress_open,
226 .close = cypress_close,
335f8514 227 .dtr_rts = cypress_dtr_rts,
a5c44e29
LM
228 .write = cypress_write,
229 .write_room = cypress_write_room,
230 .ioctl = cypress_ioctl,
231 .set_termios = cypress_set_termios,
232 .tiocmget = cypress_tiocmget,
233 .tiocmset = cypress_tiocmset,
234 .chars_in_buffer = cypress_chars_in_buffer,
235 .throttle = cypress_throttle,
236 .unthrottle = cypress_unthrottle,
237 .read_int_callback = cypress_read_int_callback,
238 .write_int_callback = cypress_write_int_callback,
239};
1da177e4
LT
240
241/*****************************************************************************
242 * Cypress serial helper functions
243 *****************************************************************************/
244
245
8873aaa6 246static int analyze_baud_rate(struct usb_serial_port *port, speed_t new_rate)
92983c21 247{
92983c21
MI
248 struct cypress_private *priv;
249 priv = usb_get_serial_port_data(port);
250
c312659c
MF
251 if (unstable_bauds)
252 return new_rate;
253
92983c21
MI
254 /*
255 * The general purpose firmware for the Cypress M8 allows for
256 * a maximum speed of 57600bps (I have no idea whether DeLorme
257 * chose to use the general purpose firmware or not), if you
258 * need to modify this speed setting for your own project
259 * please add your own chiptype and modify the code likewise.
260 * The Cypress HID->COM device will work successfully up to
261 * 115200bps (but the actual throughput is around 3kBps).
262 */
92983c21
MI
263 if (port->serial->dev->speed == USB_SPEED_LOW) {
264 /*
265 * Mike Isely <isely@pobox.com> 2-Feb-2008: The
266 * Cypress app note that describes this mechanism
267 * states the the low-speed part can't handle more
268 * than 800 bytes/sec, in which case 4800 baud is the
269 * safest speed for a part like that.
270 */
271 if (new_rate > 4800) {
272 dbg("%s - failed setting baud rate, device incapable "
273 "speed %d", __func__, new_rate);
274 return -1;
275 }
276 }
277 switch (priv->chiptype) {
278 case CT_EARTHMATE:
279 if (new_rate <= 600) {
280 /* 300 and 600 baud rates are supported under
281 * the generic firmware, but are not used with
282 * NMEA and SiRF protocols */
283 dbg("%s - failed setting baud rate, unsupported speed "
284 "of %d on Earthmate GPS", __func__, new_rate);
285 return -1;
286 }
287 break;
288 default:
289 break;
290 }
291 return new_rate;
292}
293
294
093cf723 295/* This function can either set or retrieve the current serial line settings */
813a224f 296static int cypress_serial_control(struct tty_struct *tty,
95da310e
AC
297 struct usb_serial_port *port, speed_t baud_rate, int data_bits,
298 int stop_bits, int parity_enable, int parity_type, int reset,
299 int cypress_request_type)
1da177e4 300{
3cb4a4f7 301 int new_baudrate = 0, retval = 0, tries = 0;
1da177e4 302 struct cypress_private *priv;
0954644b
JH
303 u8 *feature_buffer;
304 const unsigned int feature_len = 5;
1da177e4
LT
305 unsigned long flags;
306
441b62c1 307 dbg("%s", __func__);
813a224f 308
1da177e4
LT
309 priv = usb_get_serial_port_data(port);
310
78aef519
MI
311 if (!priv->comm_is_ok)
312 return -ENODEV;
313
0954644b
JH
314 feature_buffer = kcalloc(feature_len, sizeof(u8), GFP_KERNEL);
315 if (!feature_buffer)
316 return -ENOMEM;
317
813a224f
AC
318 switch (cypress_request_type) {
319 case CYPRESS_SET_CONFIG:
813a224f 320 /* 0 means 'Hang up' so doesn't change the true bit rate */
2805eb13
MF
321 new_baudrate = priv->baud_rate;
322 if (baud_rate && baud_rate != priv->baud_rate) {
813a224f
AC
323 dbg("%s - baud rate is changing", __func__);
324 retval = analyze_baud_rate(port, baud_rate);
2805eb13 325 if (retval >= 0) {
813a224f
AC
326 new_baudrate = retval;
327 dbg("%s - New baud rate set to %d",
328 __func__, new_baudrate);
1da177e4 329 }
813a224f
AC
330 }
331 dbg("%s - baud rate is being sent as %d",
332 __func__, new_baudrate);
333
813a224f 334 /* fill the feature_buffer with new configuration */
0f2c2d7b 335 put_unaligned_le32(new_baudrate, feature_buffer);
813a224f
AC
336 feature_buffer[4] |= data_bits; /* assign data bits in 2 bit space ( max 3 ) */
337 /* 1 bit gap */
338 feature_buffer[4] |= (stop_bits << 3); /* assign stop bits in 1 bit space */
339 feature_buffer[4] |= (parity_enable << 4); /* assign parity flag in 1 bit space */
340 feature_buffer[4] |= (parity_type << 5); /* assign parity type in 1 bit space */
341 /* 1 bit gap */
342 feature_buffer[4] |= (reset << 7); /* assign reset at end of byte, 1 bit space */
343
344 dbg("%s - device is being sent this feature report:",
345 __func__);
346 dbg("%s - %02X - %02X - %02X - %02X - %02X", __func__,
347 feature_buffer[0], feature_buffer[1],
348 feature_buffer[2], feature_buffer[3],
349 feature_buffer[4]);
350
351 do {
352 retval = usb_control_msg(port->serial->dev,
353 usb_sndctrlpipe(port->serial->dev, 0),
354 HID_REQ_SET_REPORT,
355 USB_DIR_OUT | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
356 0x0300, 0, feature_buffer,
0954644b 357 feature_len, 500);
813a224f
AC
358
359 if (tries++ >= 3)
360 break;
361
0954644b 362 } while (retval != feature_len &&
813a224f
AC
363 retval != -ENODEV);
364
0954644b 365 if (retval != feature_len) {
194343d9
GKH
366 dev_err(&port->dev, "%s - failed sending serial "
367 "line settings - %d\n", __func__, retval);
813a224f
AC
368 cypress_set_dead(port);
369 } else {
370 spin_lock_irqsave(&priv->lock, flags);
371 priv->baud_rate = new_baudrate;
372 priv->current_config = feature_buffer[4];
373 spin_unlock_irqrestore(&priv->lock, flags);
374 /* If we asked for a speed change encode it */
375 if (baud_rate)
376 tty_encode_baud_rate(tty,
377 new_baudrate, new_baudrate);
378 }
379 break;
380 case CYPRESS_GET_CONFIG:
381 if (priv->get_cfg_unsafe) {
382 /* Not implemented for this device,
383 and if we try to do it we're likely
384 to crash the hardware. */
0954644b
JH
385 retval = -ENOTTY;
386 goto out;
813a224f
AC
387 }
388 dbg("%s - retreiving serial line settings", __func__);
813a224f
AC
389 do {
390 retval = usb_control_msg(port->serial->dev,
391 usb_rcvctrlpipe(port->serial->dev, 0),
392 HID_REQ_GET_REPORT,
393 USB_DIR_IN | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
394 0x0300, 0, feature_buffer,
0954644b 395 feature_len, 500);
813a224f
AC
396
397 if (tries++ >= 3)
398 break;
0954644b 399 } while (retval != feature_len
813a224f
AC
400 && retval != -ENODEV);
401
0954644b 402 if (retval != feature_len) {
194343d9
GKH
403 dev_err(&port->dev, "%s - failed to retrieve serial "
404 "line settings - %d\n", __func__, retval);
813a224f 405 cypress_set_dead(port);
0954644b 406 goto out;
813a224f
AC
407 } else {
408 spin_lock_irqsave(&priv->lock, flags);
409 /* store the config in one byte, and later
410 use bit masks to check values */
411 priv->current_config = feature_buffer[4];
0f2c2d7b 412 priv->baud_rate = get_unaligned_le32(feature_buffer);
813a224f
AC
413 spin_unlock_irqrestore(&priv->lock, flags);
414 }
1da177e4 415 }
3cb4a4f7
LM
416 spin_lock_irqsave(&priv->lock, flags);
417 ++priv->cmd_count;
418 spin_unlock_irqrestore(&priv->lock, flags);
0954644b
JH
419out:
420 kfree(feature_buffer);
1da177e4
LT
421 return retval;
422} /* cypress_serial_control */
423
424
78aef519
MI
425static void cypress_set_dead(struct usb_serial_port *port)
426{
427 struct cypress_private *priv = usb_get_serial_port_data(port);
428 unsigned long flags;
429
430 spin_lock_irqsave(&priv->lock, flags);
431 if (!priv->comm_is_ok) {
432 spin_unlock_irqrestore(&priv->lock, flags);
433 return;
434 }
435 priv->comm_is_ok = 0;
436 spin_unlock_irqrestore(&priv->lock, flags);
437
194343d9
GKH
438 dev_err(&port->dev, "cypress_m8 suspending failing port %d - "
439 "interval might be too short\n", port->number);
78aef519
MI
440}
441
442
1da177e4
LT
443/*****************************************************************************
444 * Cypress serial driver functions
445 *****************************************************************************/
446
447
813a224f 448static int generic_startup(struct usb_serial *serial)
1da177e4
LT
449{
450 struct cypress_private *priv;
0257fa9f 451 struct usb_serial_port *port = serial->port[0];
1da177e4 452
441b62c1 453 dbg("%s - port %d", __func__, port->number);
1da177e4 454
813a224f 455 priv = kzalloc(sizeof(struct cypress_private), GFP_KERNEL);
1da177e4
LT
456 if (!priv)
457 return -ENOMEM;
458
78aef519 459 priv->comm_is_ok = !0;
1da177e4 460 spin_lock_init(&priv->lock);
117fb8d0 461 if (kfifo_alloc(&priv->write_fifo, CYPRESS_BUF_SIZE, GFP_KERNEL)) {
1da177e4
LT
462 kfree(priv);
463 return -ENOMEM;
464 }
465 init_waitqueue_head(&priv->delta_msr_wait);
813a224f
AC
466
467 usb_reset_configuration(serial->dev);
468
1da177e4
LT
469 priv->cmd_ctrl = 0;
470 priv->line_control = 0;
471 priv->termios_initialized = 0;
1da177e4 472 priv->rx_flags = 0;
3416eaa1
MI
473 /* Default packet format setting is determined by packet size.
474 Anything with a size larger then 9 must have a separate
475 count field since the 3 bit count field is otherwise too
476 small. Otherwise we can use the slightly more compact
477 format. This is in accordance with the cypress_m8 serial
478 converter app note. */
813a224f 479 if (port->interrupt_out_size > 9)
3416eaa1 480 priv->pkt_fmt = packet_format_1;
813a224f 481 else
3416eaa1 482 priv->pkt_fmt = packet_format_2;
813a224f 483
0257fa9f
MI
484 if (interval > 0) {
485 priv->write_urb_interval = interval;
486 priv->read_urb_interval = interval;
487 dbg("%s - port %d read & write intervals forced to %d",
813a224f 488 __func__, port->number, interval);
0257fa9f
MI
489 } else {
490 priv->write_urb_interval = port->interrupt_out_urb->interval;
491 priv->read_urb_interval = port->interrupt_in_urb->interval;
492 dbg("%s - port %d intervals: read=%d write=%d",
813a224f
AC
493 __func__, port->number,
494 priv->read_urb_interval, priv->write_urb_interval);
0257fa9f
MI
495 }
496 usb_set_serial_port_data(port, priv);
813a224f 497
b9db07fb
LM
498 return 0;
499}
1da177e4
LT
500
501
813a224f 502static int cypress_earthmate_startup(struct usb_serial *serial)
1da177e4
LT
503{
504 struct cypress_private *priv;
3d6aa320 505 struct usb_serial_port *port = serial->port[0];
1da177e4 506
441b62c1 507 dbg("%s", __func__);
1da177e4
LT
508
509 if (generic_startup(serial)) {
441b62c1 510 dbg("%s - Failed setting up port %d", __func__,
3d6aa320 511 port->number);
1da177e4
LT
512 return 1;
513 }
514
3d6aa320 515 priv = usb_get_serial_port_data(port);
1da177e4 516 priv->chiptype = CT_EARTHMATE;
3416eaa1
MI
517 /* All Earthmate devices use the separated-count packet
518 format! Idiotic. */
519 priv->pkt_fmt = packet_format_1;
813a224f
AC
520 if (serial->dev->descriptor.idProduct !=
521 cpu_to_le16(PRODUCT_ID_EARTHMATEUSB)) {
3d6aa320
MI
522 /* The old original USB Earthmate seemed able to
523 handle GET_CONFIG requests; everything they've
524 produced since that time crashes if this command is
525 attempted :-( */
526 dbg("%s - Marking this device as unsafe for GET_CONFIG "
527 "commands", __func__);
528 priv->get_cfg_unsafe = !0;
529 }
b9db07fb
LM
530
531 return 0;
1da177e4
LT
532} /* cypress_earthmate_startup */
533
534
813a224f 535static int cypress_hidcom_startup(struct usb_serial *serial)
1da177e4
LT
536{
537 struct cypress_private *priv;
538
441b62c1 539 dbg("%s", __func__);
1da177e4
LT
540
541 if (generic_startup(serial)) {
441b62c1 542 dbg("%s - Failed setting up port %d", __func__,
b9db07fb 543 serial->port[0]->number);
1da177e4
LT
544 return 1;
545 }
546
547 priv = usb_get_serial_port_data(serial->port[0]);
548 priv->chiptype = CT_CYPHIDCOM;
813a224f 549
b9db07fb 550 return 0;
1da177e4
LT
551} /* cypress_hidcom_startup */
552
553
813a224f 554static int cypress_ca42v2_startup(struct usb_serial *serial)
a5c44e29
LM
555{
556 struct cypress_private *priv;
557
441b62c1 558 dbg("%s", __func__);
a5c44e29
LM
559
560 if (generic_startup(serial)) {
441b62c1 561 dbg("%s - Failed setting up port %d", __func__,
a5c44e29
LM
562 serial->port[0]->number);
563 return 1;
564 }
565
566 priv = usb_get_serial_port_data(serial->port[0]);
567 priv->chiptype = CT_CA42V2;
568
569 return 0;
570} /* cypress_ca42v2_startup */
571
572
f9c99bb8 573static void cypress_release(struct usb_serial *serial)
1da177e4
LT
574{
575 struct cypress_private *priv;
576
813a224f 577 dbg("%s - port %d", __func__, serial->port[0]->number);
1da177e4
LT
578
579 /* all open ports are closed at this point */
580
581 priv = usb_get_serial_port_data(serial->port[0]);
582
583 if (priv) {
117fb8d0 584 kfifo_free(&priv->write_fifo);
1da177e4 585 kfree(priv);
1da177e4
LT
586 }
587}
588
589
a509a7e4 590static int cypress_open(struct tty_struct *tty, struct usb_serial_port *port)
1da177e4
LT
591{
592 struct cypress_private *priv = usb_get_serial_port_data(port);
593 struct usb_serial *serial = port->serial;
594 unsigned long flags;
595 int result = 0;
596
441b62c1 597 dbg("%s - port %d", __func__, port->number);
1da177e4 598
78aef519
MI
599 if (!priv->comm_is_ok)
600 return -EIO;
601
1da177e4 602 /* clear halts before open */
1da177e4
LT
603 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
335f8514 614 /* Set termios */
fe1ae7fd 615 cypress_send(port);
1da177e4 616
95da310e
AC
617 if (tty)
618 cypress_set_termios(tty, port, &priv->tmp_termios);
1da177e4
LT
619
620 /* setup the port and start reading from the device */
813a224f 621 if (!port->interrupt_in_urb) {
194343d9
GKH
622 dev_err(&port->dev, "%s - interrupt_in_urb is empty!\n",
623 __func__);
813a224f 624 return -1;
1da177e4
LT
625 }
626
627 usb_fill_int_urb(port->interrupt_in_urb, serial->dev,
628 usb_rcvintpipe(serial->dev, port->interrupt_in_endpointAddress),
813a224f
AC
629 port->interrupt_in_urb->transfer_buffer,
630 port->interrupt_in_urb->transfer_buffer_length,
0257fa9f 631 cypress_read_int_callback, port, priv->read_urb_interval);
1da177e4
LT
632 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
633
813a224f
AC
634 if (result) {
635 dev_err(&port->dev,
636 "%s - failed submitting read urb, error %d\n",
637 __func__, result);
78aef519 638 cypress_set_dead(port);
1da177e4 639 }
335f8514 640 port->port.drain_delay = 256;
1da177e4
LT
641 return result;
642} /* cypress_open */
643
335f8514
AC
644static void cypress_dtr_rts(struct usb_serial_port *port, int on)
645{
646 struct cypress_private *priv = usb_get_serial_port_data(port);
647 /* drop dtr and rts */
335f8514
AC
648 spin_lock_irq(&priv->lock);
649 if (on == 0)
650 priv->line_control = 0;
651 else
652 priv->line_control = CONTROL_DTR | CONTROL_RTS;
653 priv->cmd_ctrl = 1;
654 spin_unlock_irq(&priv->lock);
655 cypress_write(NULL, port, NULL, 0);
656}
1da177e4 657
335f8514 658static void cypress_close(struct usb_serial_port *port)
1da177e4
LT
659{
660 struct cypress_private *priv = usb_get_serial_port_data(port);
117fb8d0 661 unsigned long flags;
1da177e4 662
441b62c1 663 dbg("%s - port %d", __func__, port->number);
1da177e4 664
9e3b1d8e
ON
665 /* writing is potentially harmful, lock must be taken */
666 mutex_lock(&port->serial->disc_mutex);
667 if (port->serial->disconnected) {
668 mutex_unlock(&port->serial->disc_mutex);
669 return;
670 }
117fb8d0
JH
671 spin_lock_irqsave(&priv->lock, flags);
672 kfifo_reset_out(&priv->write_fifo);
673 spin_unlock_irqrestore(&priv->lock, flags);
674
441b62c1 675 dbg("%s - stopping urbs", __func__);
813a224f
AC
676 usb_kill_urb(port->interrupt_in_urb);
677 usb_kill_urb(port->interrupt_out_urb);
1da177e4 678
1da177e4 679 if (stats)
813a224f
AC
680 dev_info(&port->dev, "Statistics: %d Bytes In | %d Bytes Out | %d Commands Issued\n",
681 priv->bytes_in, priv->bytes_out, priv->cmd_count);
9e3b1d8e 682 mutex_unlock(&port->serial->disc_mutex);
1da177e4
LT
683} /* cypress_close */
684
685
95da310e
AC
686static int cypress_write(struct tty_struct *tty, struct usb_serial_port *port,
687 const unsigned char *buf, int count)
1da177e4
LT
688{
689 struct cypress_private *priv = usb_get_serial_port_data(port);
813a224f 690
441b62c1 691 dbg("%s - port %d, %d bytes", __func__, port->number, count);
1da177e4
LT
692
693 /* line control commands, which need to be executed immediately,
694 are not put into the buffer for obvious reasons.
695 */
696 if (priv->cmd_ctrl) {
697 count = 0;
698 goto finish;
699 }
813a224f 700
1da177e4
LT
701 if (!count)
702 return count;
813a224f 703
117fb8d0 704 count = kfifo_in_locked(&priv->write_fifo, buf, count, &priv->lock);
1da177e4
LT
705
706finish:
707 cypress_send(port);
708
709 return count;
710} /* cypress_write */
711
712
713static void cypress_send(struct usb_serial_port *port)
714{
715 int count = 0, result, offset, actual_size;
716 struct cypress_private *priv = usb_get_serial_port_data(port);
717 unsigned long flags;
813a224f 718
78aef519
MI
719 if (!priv->comm_is_ok)
720 return;
721
441b62c1 722 dbg("%s - port %d", __func__, port->number);
813a224f
AC
723 dbg("%s - interrupt out size is %d", __func__,
724 port->interrupt_out_size);
725
1da177e4
LT
726 spin_lock_irqsave(&priv->lock, flags);
727 if (priv->write_urb_in_use) {
441b62c1 728 dbg("%s - can't write, urb in use", __func__);
1da177e4
LT
729 spin_unlock_irqrestore(&priv->lock, flags);
730 return;
731 }
732 spin_unlock_irqrestore(&priv->lock, flags);
733
734 /* clear buffer */
813a224f
AC
735 memset(port->interrupt_out_urb->transfer_buffer, 0,
736 port->interrupt_out_size);
1da177e4
LT
737
738 spin_lock_irqsave(&priv->lock, flags);
3416eaa1
MI
739 switch (priv->pkt_fmt) {
740 default:
741 case packet_format_1:
742 /* this is for the CY7C64013... */
743 offset = 2;
744 port->interrupt_out_buffer[0] = priv->line_control;
745 break;
746 case packet_format_2:
747 /* this is for the CY7C63743... */
748 offset = 1;
749 port->interrupt_out_buffer[0] = priv->line_control;
750 break;
1da177e4
LT
751 }
752
753 if (priv->line_control & CONTROL_RESET)
754 priv->line_control &= ~CONTROL_RESET;
755
756 if (priv->cmd_ctrl) {
757 priv->cmd_count++;
441b62c1 758 dbg("%s - line control command being issued", __func__);
1da177e4
LT
759 spin_unlock_irqrestore(&priv->lock, flags);
760 goto send;
761 } else
762 spin_unlock_irqrestore(&priv->lock, flags);
763
117fb8d0
JH
764 count = kfifo_out_locked(&priv->write_fifo,
765 &port->interrupt_out_buffer[offset],
766 port->interrupt_out_size - offset,
767 &priv->lock);
813a224f 768 if (count == 0)
1da177e4 769 return;
1da177e4 770
3416eaa1
MI
771 switch (priv->pkt_fmt) {
772 default:
773 case packet_format_1:
774 port->interrupt_out_buffer[1] = count;
775 break;
776 case packet_format_2:
777 port->interrupt_out_buffer[0] |= count;
1da177e4
LT
778 }
779
441b62c1 780 dbg("%s - count is %d", __func__, count);
1da177e4
LT
781
782send:
783 spin_lock_irqsave(&priv->lock, flags);
784 priv->write_urb_in_use = 1;
785 spin_unlock_irqrestore(&priv->lock, flags);
786
787 if (priv->cmd_ctrl)
788 actual_size = 1;
789 else
3416eaa1
MI
790 actual_size = count +
791 (priv->pkt_fmt == packet_format_1 ? 2 : 1);
792
813a224f
AC
793 usb_serial_debug_data(debug, &port->dev, __func__,
794 port->interrupt_out_size,
795 port->interrupt_out_urb->transfer_buffer);
1da177e4 796
9aa8dae7
MI
797 usb_fill_int_urb(port->interrupt_out_urb, port->serial->dev,
798 usb_sndintpipe(port->serial->dev, port->interrupt_out_endpointAddress),
799 port->interrupt_out_buffer, port->interrupt_out_size,
800 cypress_write_int_callback, port, priv->write_urb_interval);
813a224f 801 result = usb_submit_urb(port->interrupt_out_urb, GFP_ATOMIC);
1da177e4 802 if (result) {
813a224f
AC
803 dev_err(&port->dev,
804 "%s - failed submitting write urb, error %d\n",
805 __func__, result);
1da177e4 806 priv->write_urb_in_use = 0;
78aef519 807 cypress_set_dead(port);
1da177e4
LT
808 }
809
810 spin_lock_irqsave(&priv->lock, flags);
813a224f 811 if (priv->cmd_ctrl)
1da177e4 812 priv->cmd_ctrl = 0;
813a224f
AC
813
814 /* do not count the line control and size bytes */
815 priv->bytes_out += count;
1da177e4
LT
816 spin_unlock_irqrestore(&priv->lock, flags);
817
cf2c7481 818 usb_serial_port_softint(port);
1da177e4
LT
819} /* cypress_send */
820
821
822/* returns how much space is available in the soft buffer */
95da310e 823static int cypress_write_room(struct tty_struct *tty)
1da177e4 824{
95da310e 825 struct usb_serial_port *port = tty->driver_data;
1da177e4
LT
826 struct cypress_private *priv = usb_get_serial_port_data(port);
827 int room = 0;
828 unsigned long flags;
829
441b62c1 830 dbg("%s - port %d", __func__, port->number);
1da177e4
LT
831
832 spin_lock_irqsave(&priv->lock, flags);
117fb8d0 833 room = kfifo_avail(&priv->write_fifo);
1da177e4
LT
834 spin_unlock_irqrestore(&priv->lock, flags);
835
441b62c1 836 dbg("%s - returns %d", __func__, room);
1da177e4
LT
837 return room;
838}
839
840
60b33c13 841static int cypress_tiocmget(struct tty_struct *tty)
1da177e4 842{
95da310e 843 struct usb_serial_port *port = tty->driver_data;
1da177e4
LT
844 struct cypress_private *priv = usb_get_serial_port_data(port);
845 __u8 status, control;
846 unsigned int result = 0;
847 unsigned long flags;
813a224f 848
441b62c1 849 dbg("%s - port %d", __func__, port->number);
1da177e4
LT
850
851 spin_lock_irqsave(&priv->lock, flags);
852 control = priv->line_control;
853 status = priv->current_status;
854 spin_unlock_irqrestore(&priv->lock, flags);
855
856 result = ((control & CONTROL_DTR) ? TIOCM_DTR : 0)
857 | ((control & CONTROL_RTS) ? TIOCM_RTS : 0)
858 | ((status & UART_CTS) ? TIOCM_CTS : 0)
859 | ((status & UART_DSR) ? TIOCM_DSR : 0)
860 | ((status & UART_RI) ? TIOCM_RI : 0)
861 | ((status & UART_CD) ? TIOCM_CD : 0);
862
441b62c1 863 dbg("%s - result = %x", __func__, result);
1da177e4
LT
864
865 return result;
866}
867
868
20b9d177 869static int cypress_tiocmset(struct tty_struct *tty,
1da177e4
LT
870 unsigned int set, unsigned int clear)
871{
95da310e 872 struct usb_serial_port *port = tty->driver_data;
1da177e4
LT
873 struct cypress_private *priv = usb_get_serial_port_data(port);
874 unsigned long flags;
813a224f 875
441b62c1 876 dbg("%s - port %d", __func__, port->number);
1da177e4
LT
877
878 spin_lock_irqsave(&priv->lock, flags);
879 if (set & TIOCM_RTS)
880 priv->line_control |= CONTROL_RTS;
881 if (set & TIOCM_DTR)
882 priv->line_control |= CONTROL_DTR;
883 if (clear & TIOCM_RTS)
884 priv->line_control &= ~CONTROL_RTS;
885 if (clear & TIOCM_DTR)
886 priv->line_control &= ~CONTROL_DTR;
8873aaa6 887 priv->cmd_ctrl = 1;
1da177e4
LT
888 spin_unlock_irqrestore(&priv->lock, flags);
889
95da310e 890 return cypress_write(tty, port, NULL, 0);
1da177e4
LT
891}
892
893
00a0d0d6 894static int cypress_ioctl(struct tty_struct *tty,
95da310e 895 unsigned int cmd, unsigned long arg)
1da177e4 896{
95da310e 897 struct usb_serial_port *port = tty->driver_data;
1da177e4
LT
898 struct cypress_private *priv = usb_get_serial_port_data(port);
899
441b62c1 900 dbg("%s - port %d, cmd 0x%.4x", __func__, port->number, cmd);
1da177e4
LT
901
902 switch (cmd) {
813a224f
AC
903 /* This code comes from drivers/char/serial.c and ftdi_sio.c */
904 case TIOCMIWAIT:
905 while (priv != NULL) {
906 interruptible_sleep_on(&priv->delta_msr_wait);
907 /* see if a signal did it */
908 if (signal_pending(current))
909 return -ERESTARTSYS;
910 else {
911 char diff = priv->diff_status;
912 if (diff == 0)
913 return -EIO; /* no change => error */
914
915 /* consume all events */
916 priv->diff_status = 0;
917
918 /* return 0 if caller wanted to know about
919 these bits */
920 if (((arg & TIOCM_RNG) && (diff & UART_RI)) ||
921 ((arg & TIOCM_DSR) && (diff & UART_DSR)) ||
922 ((arg & TIOCM_CD) && (diff & UART_CD)) ||
923 ((arg & TIOCM_CTS) && (diff & UART_CTS)))
924 return 0;
925 /* otherwise caller can't care less about what
926 * happened, and so we continue to wait for
927 * more events.
928 */
1da177e4 929 }
813a224f
AC
930 }
931 return 0;
932 default:
933 break;
1da177e4 934 }
441b62c1 935 dbg("%s - arg not supported - it was 0x%04x - check include/asm/ioctls.h", __func__, cmd);
1da177e4
LT
936 return -ENOIOCTLCMD;
937} /* cypress_ioctl */
938
939
95da310e
AC
940static void cypress_set_termios(struct tty_struct *tty,
941 struct usb_serial_port *port, struct ktermios *old_termios)
1da177e4
LT
942{
943 struct cypress_private *priv = usb_get_serial_port_data(port);
1da177e4 944 int data_bits, stop_bits, parity_type, parity_enable;
8873aaa6 945 unsigned cflag, iflag;
1da177e4
LT
946 unsigned long flags;
947 __u8 oldlines;
3cb4a4f7 948 int linechange = 0;
b9db07fb 949
441b62c1 950 dbg("%s - port %d", __func__, port->number);
1da177e4 951
1da177e4 952 spin_lock_irqsave(&priv->lock, flags);
fe1ae7fd
AC
953 /* We can't clean this one up as we don't know the device type
954 early enough */
1da177e4
LT
955 if (!priv->termios_initialized) {
956 if (priv->chiptype == CT_EARTHMATE) {
957 *(tty->termios) = tty_std_termios;
b9db07fb
LM
958 tty->termios->c_cflag = B4800 | CS8 | CREAD | HUPCL |
959 CLOCAL;
8873aaa6
AC
960 tty->termios->c_ispeed = 4800;
961 tty->termios->c_ospeed = 4800;
1da177e4
LT
962 } else if (priv->chiptype == CT_CYPHIDCOM) {
963 *(tty->termios) = tty_std_termios;
b9db07fb
LM
964 tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
965 CLOCAL;
8873aaa6
AC
966 tty->termios->c_ispeed = 9600;
967 tty->termios->c_ospeed = 9600;
a5c44e29
LM
968 } else if (priv->chiptype == CT_CA42V2) {
969 *(tty->termios) = tty_std_termios;
970 tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
971 CLOCAL;
8873aaa6
AC
972 tty->termios->c_ispeed = 9600;
973 tty->termios->c_ospeed = 9600;
1da177e4
LT
974 }
975 priv->termios_initialized = 1;
976 }
977 spin_unlock_irqrestore(&priv->lock, flags);
978
8873aaa6
AC
979 /* Unsupported features need clearing */
980 tty->termios->c_cflag &= ~(CMSPAR|CRTSCTS);
981
1da177e4
LT
982 cflag = tty->termios->c_cflag;
983 iflag = tty->termios->c_iflag;
984
985 /* check if there are new settings */
986 if (old_termios) {
8873aaa6
AC
987 spin_lock_irqsave(&priv->lock, flags);
988 priv->tmp_termios = *(tty->termios);
989 spin_unlock_irqrestore(&priv->lock, flags);
990 }
1da177e4
LT
991
992 /* set number of data bits, parity, stop bits */
993 /* when parity is disabled the parity type bit is ignored */
994
b9db07fb
LM
995 /* 1 means 2 stop bits, 0 means 1 stop bit */
996 stop_bits = cflag & CSTOPB ? 1 : 0;
997
1da177e4
LT
998 if (cflag & PARENB) {
999 parity_enable = 1;
b9db07fb
LM
1000 /* 1 means odd parity, 0 means even parity */
1001 parity_type = cflag & PARODD ? 1 : 0;
1da177e4
LT
1002 } else
1003 parity_enable = parity_type = 0;
1004
77336828
AC
1005 switch (cflag & CSIZE) {
1006 case CS5:
1007 data_bits = 0;
1008 break;
1009 case CS6:
1010 data_bits = 1;
1011 break;
1012 case CS7:
1013 data_bits = 2;
1014 break;
1015 case CS8:
1da177e4 1016 data_bits = 3;
77336828
AC
1017 break;
1018 default:
194343d9
GKH
1019 dev_err(&port->dev, "%s - CSIZE was set, but not CS5-CS8\n",
1020 __func__);
77336828
AC
1021 data_bits = 3;
1022 }
1da177e4
LT
1023 spin_lock_irqsave(&priv->lock, flags);
1024 oldlines = priv->line_control;
1025 if ((cflag & CBAUD) == B0) {
1026 /* drop dtr and rts */
441b62c1 1027 dbg("%s - dropping the lines, baud rate 0bps", __func__);
1da177e4 1028 priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
8873aaa6 1029 } else
3cb4a4f7 1030 priv->line_control = (CONTROL_DTR | CONTROL_RTS);
1da177e4 1031 spin_unlock_irqrestore(&priv->lock, flags);
1da177e4 1032
b9db07fb 1033 dbg("%s - sending %d stop_bits, %d parity_enable, %d parity_type, "
441b62c1 1034 "%d data_bits (+5)", __func__, stop_bits,
b9db07fb
LM
1035 parity_enable, parity_type, data_bits);
1036
813a224f
AC
1037 cypress_serial_control(tty, port, tty_get_baud_rate(tty),
1038 data_bits, stop_bits,
1039 parity_enable, parity_type,
1040 0, CYPRESS_SET_CONFIG);
1da177e4 1041
b9db07fb
LM
1042 /* we perform a CYPRESS_GET_CONFIG so that the current settings are
1043 * filled into the private structure this should confirm that all is
1044 * working if it returns what we just set */
95da310e 1045 cypress_serial_control(tty, port, 0, 0, 0, 0, 0, 0, CYPRESS_GET_CONFIG);
1da177e4 1046
b9db07fb
LM
1047 /* Here we can define custom tty settings for devices; the main tty
1048 * termios flag base comes from empeg.c */
1da177e4 1049
b9db07fb 1050 spin_lock_irqsave(&priv->lock, flags);
813a224f 1051 if (priv->chiptype == CT_EARTHMATE && priv->baud_rate == 4800) {
b9db07fb
LM
1052 dbg("Using custom termios settings for a baud rate of "
1053 "4800bps.");
1da177e4
LT
1054 /* define custom termios settings for NMEA protocol */
1055
1da177e4 1056 tty->termios->c_iflag /* input modes - */
b9db07fb
LM
1057 &= ~(IGNBRK /* disable ignore break */
1058 | BRKINT /* disable break causes interrupt */
1059 | PARMRK /* disable mark parity errors */
1060 | ISTRIP /* disable clear high bit of input char */
1061 | INLCR /* disable translate NL to CR */
1062 | IGNCR /* disable ignore CR */
1063 | ICRNL /* disable translate CR to NL */
1064 | IXON); /* disable enable XON/XOFF flow control */
1065
1da177e4 1066 tty->termios->c_oflag /* output modes */
b9db07fb 1067 &= ~OPOST; /* disable postprocess output char */
1da177e4 1068
b9db07fb
LM
1069 tty->termios->c_lflag /* line discipline modes */
1070 &= ~(ECHO /* disable echo input characters */
1071 | ECHONL /* disable echo new line */
1072 | ICANON /* disable erase, kill, werase, and rprnt
1073 special characters */
1074 | ISIG /* disable interrupt, quit, and suspend
1075 special characters */
1076 | IEXTEN); /* disable non-POSIX special characters */
3cb4a4f7 1077 } /* CT_CYPHIDCOM: Application should handle this for device */
1da177e4 1078
1da177e4
LT
1079 linechange = (priv->line_control != oldlines);
1080 spin_unlock_irqrestore(&priv->lock, flags);
1081
1082 /* if necessary, set lines */
3cb4a4f7 1083 if (linechange) {
1da177e4 1084 priv->cmd_ctrl = 1;
95da310e 1085 cypress_write(tty, port, NULL, 0);
1da177e4 1086 }
1da177e4
LT
1087} /* cypress_set_termios */
1088
b9db07fb 1089
1da177e4 1090/* returns amount of data still left in soft buffer */
95da310e 1091static int cypress_chars_in_buffer(struct tty_struct *tty)
1da177e4 1092{
95da310e 1093 struct usb_serial_port *port = tty->driver_data;
1da177e4
LT
1094 struct cypress_private *priv = usb_get_serial_port_data(port);
1095 int chars = 0;
1096 unsigned long flags;
1097
441b62c1 1098 dbg("%s - port %d", __func__, port->number);
813a224f 1099
1da177e4 1100 spin_lock_irqsave(&priv->lock, flags);
117fb8d0 1101 chars = kfifo_len(&priv->write_fifo);
1da177e4
LT
1102 spin_unlock_irqrestore(&priv->lock, flags);
1103
441b62c1 1104 dbg("%s - returns %d", __func__, chars);
1da177e4
LT
1105 return chars;
1106}
1107
1108
95da310e 1109static void cypress_throttle(struct tty_struct *tty)
1da177e4 1110{
95da310e 1111 struct usb_serial_port *port = tty->driver_data;
1da177e4 1112 struct cypress_private *priv = usb_get_serial_port_data(port);
1da177e4 1113
441b62c1 1114 dbg("%s - port %d", __func__, port->number);
1da177e4 1115
63832515 1116 spin_lock_irq(&priv->lock);
1da177e4 1117 priv->rx_flags = THROTTLED;
63832515 1118 spin_unlock_irq(&priv->lock);
1da177e4
LT
1119}
1120
1121
95da310e 1122static void cypress_unthrottle(struct tty_struct *tty)
1da177e4 1123{
95da310e 1124 struct usb_serial_port *port = tty->driver_data;
1da177e4
LT
1125 struct cypress_private *priv = usb_get_serial_port_data(port);
1126 int actually_throttled, result;
1da177e4 1127
441b62c1 1128 dbg("%s - port %d", __func__, port->number);
1da177e4 1129
63832515 1130 spin_lock_irq(&priv->lock);
1da177e4
LT
1131 actually_throttled = priv->rx_flags & ACTUALLY_THROTTLED;
1132 priv->rx_flags = 0;
63832515 1133 spin_unlock_irq(&priv->lock);
1da177e4 1134
78aef519
MI
1135 if (!priv->comm_is_ok)
1136 return;
1137
1da177e4 1138 if (actually_throttled) {
63832515 1139 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
78aef519 1140 if (result) {
b9db07fb 1141 dev_err(&port->dev, "%s - failed submitting read urb, "
441b62c1 1142 "error %d\n", __func__, result);
78aef519
MI
1143 cypress_set_dead(port);
1144 }
1da177e4
LT
1145 }
1146}
1147
1148
7d12e780 1149static void cypress_read_int_callback(struct urb *urb)
1da177e4 1150{
cdc97792 1151 struct usb_serial_port *port = urb->context;
1da177e4
LT
1152 struct cypress_private *priv = usb_get_serial_port_data(port);
1153 struct tty_struct *tty;
1154 unsigned char *data = urb->transfer_buffer;
1155 unsigned long flags;
b9db07fb 1156 char tty_flag = TTY_NORMAL;
1da177e4
LT
1157 int havedata = 0;
1158 int bytes = 0;
1159 int result;
1160 int i = 0;
8d7bc55e 1161 int status = urb->status;
1da177e4 1162
441b62c1 1163 dbg("%s - port %d", __func__, port->number);
1da177e4 1164
8d7bc55e 1165 switch (status) {
78aef519
MI
1166 case 0: /* success */
1167 break;
1168 case -ECONNRESET:
1169 case -ENOENT:
1170 case -ESHUTDOWN:
1171 /* precursor to disconnect so just go away */
1172 return;
1173 case -EPIPE:
4d2fae8b
AS
1174 /* Can't call usb_clear_halt while in_interrupt */
1175 /* FALLS THROUGH */
78aef519
MI
1176 default:
1177 /* something ugly is going on... */
813a224f
AC
1178 dev_err(&urb->dev->dev,
1179 "%s - unexpected nonzero read status received: %d\n",
1180 __func__, status);
78aef519 1181 cypress_set_dead(port);
1da177e4
LT
1182 return;
1183 }
1184
1185 spin_lock_irqsave(&priv->lock, flags);
1186 if (priv->rx_flags & THROTTLED) {
441b62c1 1187 dbg("%s - now throttling", __func__);
1da177e4
LT
1188 priv->rx_flags |= ACTUALLY_THROTTLED;
1189 spin_unlock_irqrestore(&priv->lock, flags);
1190 return;
1191 }
1192 spin_unlock_irqrestore(&priv->lock, flags);
1193
4a90f09b 1194 tty = tty_port_tty_get(&port->port);
1da177e4 1195 if (!tty) {
441b62c1 1196 dbg("%s - bad tty pointer - exiting", __func__);
1da177e4
LT
1197 return;
1198 }
1199
1200 spin_lock_irqsave(&priv->lock, flags);
3416eaa1
MI
1201 result = urb->actual_length;
1202 switch (priv->pkt_fmt) {
1203 default:
1204 case packet_format_1:
1205 /* This is for the CY7C64013... */
1206 priv->current_status = data[0] & 0xF8;
1207 bytes = data[1] + 2;
1208 i = 2;
1209 if (bytes > 2)
1210 havedata = 1;
1211 break;
1212 case packet_format_2:
1213 /* This is for the CY7C63743... */
1214 priv->current_status = data[0] & 0xF8;
1215 bytes = (data[0] & 0x07) + 1;
1216 i = 1;
1217 if (bytes > 1)
1218 havedata = 1;
1219 break;
1da177e4
LT
1220 }
1221 spin_unlock_irqrestore(&priv->lock, flags);
3416eaa1
MI
1222 if (result < bytes) {
1223 dbg("%s - wrong packet size - received %d bytes but packet "
1224 "said %d bytes", __func__, result, bytes);
1225 goto continue_read;
1226 }
1da177e4 1227
813a224f
AC
1228 usb_serial_debug_data(debug, &port->dev, __func__,
1229 urb->actual_length, data);
1da177e4
LT
1230
1231 spin_lock_irqsave(&priv->lock, flags);
1232 /* check to see if status has changed */
6768306c
MI
1233 if (priv->current_status != priv->prev_status) {
1234 priv->diff_status |= priv->current_status ^
1235 priv->prev_status;
1236 wake_up_interruptible(&priv->delta_msr_wait);
1237 priv->prev_status = priv->current_status;
1da177e4 1238 }
b9db07fb 1239 spin_unlock_irqrestore(&priv->lock, flags);
1da177e4 1240
b9db07fb
LM
1241 /* hangup, as defined in acm.c... this might be a bad place for it
1242 * though */
1243 if (tty && !(tty->termios->c_cflag & CLOCAL) &&
1244 !(priv->current_status & UART_CD)) {
441b62c1 1245 dbg("%s - calling hangup", __func__);
1da177e4
LT
1246 tty_hangup(tty);
1247 goto continue_read;
1248 }
1249
b9db07fb
LM
1250 /* There is one error bit... I'm assuming it is a parity error
1251 * indicator as the generic firmware will set this bit to 1 if a
1252 * parity error occurs.
1253 * I can not find reference to any other error events. */
1da177e4
LT
1254 spin_lock_irqsave(&priv->lock, flags);
1255 if (priv->current_status & CYP_ERROR) {
1256 spin_unlock_irqrestore(&priv->lock, flags);
1257 tty_flag = TTY_PARITY;
441b62c1 1258 dbg("%s - Parity Error detected", __func__);
1da177e4
LT
1259 } else
1260 spin_unlock_irqrestore(&priv->lock, flags);
1261
1262 /* process read if there is data other than line status */
a108bfcb
AC
1263 if (tty && bytes > i) {
1264 tty_insert_flip_string_fixed_flag(tty, data + i,
70ced221 1265 tty_flag, bytes - i);
4a90f09b 1266 tty_flip_buffer_push(tty);
1da177e4
LT
1267 }
1268
1269 spin_lock_irqsave(&priv->lock, flags);
b9db07fb
LM
1270 /* control and status byte(s) are also counted */
1271 priv->bytes_in += bytes;
1da177e4
LT
1272 spin_unlock_irqrestore(&priv->lock, flags);
1273
1274continue_read:
4a90f09b 1275 tty_kref_put(tty);
b9db07fb 1276
1f87158e 1277 /* Continue trying to always read */
1da177e4 1278
1f87158e 1279 if (priv->comm_is_ok) {
b9db07fb
LM
1280 usb_fill_int_urb(port->interrupt_in_urb, port->serial->dev,
1281 usb_rcvintpipe(port->serial->dev,
1282 port->interrupt_in_endpointAddress),
1283 port->interrupt_in_urb->transfer_buffer,
1284 port->interrupt_in_urb->transfer_buffer_length,
813a224f
AC
1285 cypress_read_int_callback, port,
1286 priv->read_urb_interval);
b9db07fb 1287 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
1f87158e 1288 if (result && result != -EPERM) {
b9db07fb 1289 dev_err(&urb->dev->dev, "%s - failed resubmitting "
441b62c1 1290 "read urb, error %d\n", __func__,
b9db07fb 1291 result);
78aef519
MI
1292 cypress_set_dead(port);
1293 }
1da177e4 1294 }
1da177e4
LT
1295} /* cypress_read_int_callback */
1296
1297
7d12e780 1298static void cypress_write_int_callback(struct urb *urb)
1da177e4 1299{
cdc97792 1300 struct usb_serial_port *port = urb->context;
1da177e4
LT
1301 struct cypress_private *priv = usb_get_serial_port_data(port);
1302 int result;
8d7bc55e 1303 int status = urb->status;
1da177e4 1304
441b62c1 1305 dbg("%s - port %d", __func__, port->number);
8d7bc55e
GKH
1306
1307 switch (status) {
813a224f
AC
1308 case 0:
1309 /* success */
1310 break;
1311 case -ECONNRESET:
1312 case -ENOENT:
1313 case -ESHUTDOWN:
1314 /* this urb is terminated, clean up */
1315 dbg("%s - urb shutting down with status: %d",
1316 __func__, status);
1317 priv->write_urb_in_use = 0;
1318 return;
1319 case -EPIPE: /* no break needed; clear halt and resubmit */
1320 if (!priv->comm_is_ok)
1da177e4 1321 break;
813a224f
AC
1322 usb_clear_halt(port->serial->dev, 0x02);
1323 /* error in the urb, so we have to resubmit it */
1324 dbg("%s - nonzero write bulk status received: %d",
1325 __func__, status);
1326 port->interrupt_out_urb->transfer_buffer_length = 1;
813a224f
AC
1327 result = usb_submit_urb(port->interrupt_out_urb, GFP_ATOMIC);
1328 if (!result)
1da177e4 1329 return;
813a224f
AC
1330 dev_err(&urb->dev->dev,
1331 "%s - failed resubmitting write urb, error %d\n",
1332 __func__, result);
1333 cypress_set_dead(port);
1334 break;
1335 default:
1336 dev_err(&urb->dev->dev,
1337 "%s - unexpected nonzero write status received: %d\n",
1338 __func__, status);
1339 cypress_set_dead(port);
1340 break;
1da177e4 1341 }
1da177e4 1342 priv->write_urb_in_use = 0;
813a224f 1343
1da177e4
LT
1344 /* send any buffered data */
1345 cypress_send(port);
1346}
1347
1348
1da177e4
LT
1349/*****************************************************************************
1350 * Module functions
1351 *****************************************************************************/
1352
1353static int __init cypress_init(void)
1354{
1355 int retval;
813a224f 1356
441b62c1 1357 dbg("%s", __func__);
813a224f 1358
1da177e4
LT
1359 retval = usb_serial_register(&cypress_earthmate_device);
1360 if (retval)
1361 goto failed_em_register;
1362 retval = usb_serial_register(&cypress_hidcom_device);
1363 if (retval)
1364 goto failed_hidcom_register;
a5c44e29
LM
1365 retval = usb_serial_register(&cypress_ca42v2_device);
1366 if (retval)
1367 goto failed_ca42v2_register;
1da177e4
LT
1368 retval = usb_register(&cypress_driver);
1369 if (retval)
1370 goto failed_usb_register;
1371
c197a8db
GKH
1372 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
1373 DRIVER_DESC "\n");
1da177e4 1374 return 0;
2e46b748 1375
1da177e4 1376failed_usb_register:
a5c44e29 1377 usb_serial_deregister(&cypress_ca42v2_device);
2e46b748 1378failed_ca42v2_register:
1da177e4 1379 usb_serial_deregister(&cypress_hidcom_device);
2e46b748 1380failed_hidcom_register:
1da177e4 1381 usb_serial_deregister(&cypress_earthmate_device);
2e46b748 1382failed_em_register:
1da177e4
LT
1383 return retval;
1384}
1385
1386
813a224f 1387static void __exit cypress_exit(void)
1da177e4 1388{
441b62c1 1389 dbg("%s", __func__);
1da177e4 1390
813a224f
AC
1391 usb_deregister(&cypress_driver);
1392 usb_serial_deregister(&cypress_earthmate_device);
1393 usb_serial_deregister(&cypress_hidcom_device);
1394 usb_serial_deregister(&cypress_ca42v2_device);
1da177e4
LT
1395}
1396
1397
1398module_init(cypress_init);
1399module_exit(cypress_exit);
1400
813a224f
AC
1401MODULE_AUTHOR(DRIVER_AUTHOR);
1402MODULE_DESCRIPTION(DRIVER_DESC);
1403MODULE_VERSION(DRIVER_VERSION);
1da177e4
LT
1404MODULE_LICENSE("GPL");
1405
1406module_param(debug, bool, S_IRUGO | S_IWUSR);
1407MODULE_PARM_DESC(debug, "Debug enabled or not");
1408module_param(stats, bool, S_IRUGO | S_IWUSR);
1409MODULE_PARM_DESC(stats, "Enable statistics or not");
3cb4a4f7
LM
1410module_param(interval, int, S_IRUGO | S_IWUSR);
1411MODULE_PARM_DESC(interval, "Overrides interrupt interval");
c312659c
MF
1412module_param(unstable_bauds, bool, S_IRUGO | S_IWUSR);
1413MODULE_PARM_DESC(unstable_bauds, "Allow unstable baud rates");