tty: USB does not need the filp argument in the drivers
[linux-2.6.git] / drivers / usb / serial / cypress_m8.c
blobdf1adb9a4367284b63f5119400e0438bb227c62a
1 /*
2 * USB Cypress M8 driver
4 * Copyright (C) 2004
5 * Lonnie Mendez (dignome@gmail.com)
6 * Copyright (C) 2003,2004
7 * Neil Whelchel (koyama@firstlight.net)
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.
14 * See Documentation/usb/usb-serial.txt for more information on using this
15 * driver
17 * See http://geocities.com/i0xox0i for information on this driver and the
18 * earthmate usb device.
20 * Lonnie Mendez <dignome@gmail.com>
21 * 4-29-2005
22 * Fixed problem where setting or retreiving the serial config would fail
23 * with EPIPE. Removed CRTS toggling so the driver behaves more like
24 * other usbserial adapters. Issued new interval of 1ms instead of the
25 * default 10ms. As a result, transfer speed has been substantially
26 * increased from avg. 850bps to avg. 3300bps. initial termios has also
27 * been modified. Cleaned up code and formatting issues so it is more
28 * readable. Replaced the C++ style comments.
30 * Lonnie Mendez <dignome@gmail.com>
31 * 12-15-2004
32 * Incorporated write buffering from pl2303 driver. Fixed bug with line
33 * handling so both lines are raised in cypress_open. (was dropping rts)
34 * Various code cleanups made as well along with other misc bug fixes.
36 * Lonnie Mendez <dignome@gmail.com>
37 * 04-10-2004
38 * Driver modified to support dynamic line settings. Various improvments
39 * and features.
41 * Neil Whelchel
42 * 10-2003
43 * Driver first released.
47 /* Thanks to Neil Whelchel for writing the first cypress m8 implementation
48 for linux. */
49 /* Thanks to cypress for providing references for the hid reports. */
50 /* Thanks to Jiang Zhang for providing links and for general help. */
51 /* Code originates and was built up from ftdi_sio, belkin, pl2303 and others.*/
54 #include <linux/kernel.h>
55 #include <linux/errno.h>
56 #include <linux/init.h>
57 #include <linux/slab.h>
58 #include <linux/tty.h>
59 #include <linux/tty_driver.h>
60 #include <linux/tty_flip.h>
61 #include <linux/module.h>
62 #include <linux/moduleparam.h>
63 #include <linux/spinlock.h>
64 #include <linux/usb.h>
65 #include <linux/usb/serial.h>
66 #include <linux/serial.h>
67 #include <linux/delay.h>
68 #include <linux/uaccess.h>
70 #include "cypress_m8.h"
73 #ifdef CONFIG_USB_SERIAL_DEBUG
74 static int debug = 1;
75 #else
76 static int debug;
77 #endif
78 static int stats;
79 static int interval;
82 * Version Information
84 #define DRIVER_VERSION "v1.09"
85 #define DRIVER_AUTHOR "Lonnie Mendez <dignome@gmail.com>, Neil Whelchel <koyama@firstlight.net>"
86 #define DRIVER_DESC "Cypress USB to Serial Driver"
88 /* write buffer size defines */
89 #define CYPRESS_BUF_SIZE 1024
90 #define CYPRESS_CLOSING_WAIT (30*HZ)
92 static struct usb_device_id id_table_earthmate [] = {
93 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB) },
94 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB_LT20) },
95 { } /* Terminating entry */
98 static struct usb_device_id id_table_cyphidcomrs232 [] = {
99 { USB_DEVICE(VENDOR_ID_CYPRESS, PRODUCT_ID_CYPHIDCOM) },
100 { USB_DEVICE(VENDOR_ID_POWERCOM, PRODUCT_ID_UPS) },
101 { } /* Terminating entry */
104 static struct usb_device_id id_table_nokiaca42v2 [] = {
105 { USB_DEVICE(VENDOR_ID_DAZZLE, PRODUCT_ID_CA42) },
106 { } /* Terminating entry */
109 static struct usb_device_id id_table_combined [] = {
110 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB) },
111 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB_LT20) },
112 { USB_DEVICE(VENDOR_ID_CYPRESS, PRODUCT_ID_CYPHIDCOM) },
113 { USB_DEVICE(VENDOR_ID_POWERCOM, PRODUCT_ID_UPS) },
114 { USB_DEVICE(VENDOR_ID_DAZZLE, PRODUCT_ID_CA42) },
115 { } /* Terminating entry */
118 MODULE_DEVICE_TABLE(usb, id_table_combined);
120 static struct usb_driver cypress_driver = {
121 .name = "cypress",
122 .probe = usb_serial_probe,
123 .disconnect = usb_serial_disconnect,
124 .id_table = id_table_combined,
125 .no_dynamic_id = 1,
128 enum packet_format {
129 packet_format_1, /* b0:status, b1:payload count */
130 packet_format_2 /* b0[7:3]:status, b0[2:0]:payload count */
133 struct cypress_private {
134 spinlock_t lock; /* private lock */
135 int chiptype; /* identifier of device, for quirks/etc */
136 int bytes_in; /* used for statistics */
137 int bytes_out; /* used for statistics */
138 int cmd_count; /* used for statistics */
139 int cmd_ctrl; /* always set this to 1 before issuing a command */
140 struct cypress_buf *buf; /* write buffer */
141 int write_urb_in_use; /* write urb in use indicator */
142 int write_urb_interval; /* interval to use for write urb */
143 int read_urb_interval; /* interval to use for read urb */
144 int comm_is_ok; /* true if communication is (still) ok */
145 int termios_initialized;
146 __u8 line_control; /* holds dtr / rts value */
147 __u8 current_status; /* received from last read - info on dsr,cts,cd,ri,etc */
148 __u8 current_config; /* stores the current configuration byte */
149 __u8 rx_flags; /* throttling - used from whiteheat/ftdi_sio */
150 enum packet_format pkt_fmt; /* format to use for packet send / receive */
151 int get_cfg_unsafe; /* If true, the CYPRESS_GET_CONFIG is unsafe */
152 int baud_rate; /* stores current baud rate in
153 integer form */
154 int isthrottled; /* if throttled, discard reads */
155 wait_queue_head_t delta_msr_wait; /* used for TIOCMIWAIT */
156 char prev_status, diff_status; /* used for TIOCMIWAIT */
157 /* we pass a pointer to this as the arguement sent to
158 cypress_set_termios old_termios */
159 struct ktermios tmp_termios; /* stores the old termios settings */
162 /* write buffer structure */
163 struct cypress_buf {
164 unsigned int buf_size;
165 char *buf_buf;
166 char *buf_get;
167 char *buf_put;
170 /* function prototypes for the Cypress USB to serial device */
171 static int cypress_earthmate_startup(struct usb_serial *serial);
172 static int cypress_hidcom_startup(struct usb_serial *serial);
173 static int cypress_ca42v2_startup(struct usb_serial *serial);
174 static void cypress_release(struct usb_serial *serial);
175 static int cypress_open(struct tty_struct *tty, struct usb_serial_port *port);
176 static void cypress_close(struct usb_serial_port *port);
177 static void cypress_dtr_rts(struct usb_serial_port *port, int on);
178 static int cypress_write(struct tty_struct *tty, struct usb_serial_port *port,
179 const unsigned char *buf, int count);
180 static void cypress_send(struct usb_serial_port *port);
181 static int cypress_write_room(struct tty_struct *tty);
182 static int cypress_ioctl(struct tty_struct *tty, struct file *file,
183 unsigned int cmd, unsigned long arg);
184 static void cypress_set_termios(struct tty_struct *tty,
185 struct usb_serial_port *port, struct ktermios *old);
186 static int cypress_tiocmget(struct tty_struct *tty, struct file *file);
187 static int cypress_tiocmset(struct tty_struct *tty, struct file *file,
188 unsigned int set, unsigned int clear);
189 static int cypress_chars_in_buffer(struct tty_struct *tty);
190 static void cypress_throttle(struct tty_struct *tty);
191 static void cypress_unthrottle(struct tty_struct *tty);
192 static void cypress_set_dead(struct usb_serial_port *port);
193 static void cypress_read_int_callback(struct urb *urb);
194 static void cypress_write_int_callback(struct urb *urb);
195 /* write buffer functions */
196 static struct cypress_buf *cypress_buf_alloc(unsigned int size);
197 static void cypress_buf_free(struct cypress_buf *cb);
198 static void cypress_buf_clear(struct cypress_buf *cb);
199 static unsigned int cypress_buf_data_avail(struct cypress_buf *cb);
200 static unsigned int cypress_buf_space_avail(struct cypress_buf *cb);
201 static unsigned int cypress_buf_put(struct cypress_buf *cb,
202 const char *buf, unsigned int count);
203 static unsigned int cypress_buf_get(struct cypress_buf *cb,
204 char *buf, unsigned int count);
207 static struct usb_serial_driver cypress_earthmate_device = {
208 .driver = {
209 .owner = THIS_MODULE,
210 .name = "earthmate",
212 .description = "DeLorme Earthmate USB",
213 .usb_driver = &cypress_driver,
214 .id_table = id_table_earthmate,
215 .num_ports = 1,
216 .attach = cypress_earthmate_startup,
217 .release = cypress_release,
218 .open = cypress_open,
219 .close = cypress_close,
220 .dtr_rts = cypress_dtr_rts,
221 .write = cypress_write,
222 .write_room = cypress_write_room,
223 .ioctl = cypress_ioctl,
224 .set_termios = cypress_set_termios,
225 .tiocmget = cypress_tiocmget,
226 .tiocmset = cypress_tiocmset,
227 .chars_in_buffer = cypress_chars_in_buffer,
228 .throttle = cypress_throttle,
229 .unthrottle = cypress_unthrottle,
230 .read_int_callback = cypress_read_int_callback,
231 .write_int_callback = cypress_write_int_callback,
234 static struct usb_serial_driver cypress_hidcom_device = {
235 .driver = {
236 .owner = THIS_MODULE,
237 .name = "cyphidcom",
239 .description = "HID->COM RS232 Adapter",
240 .usb_driver = &cypress_driver,
241 .id_table = id_table_cyphidcomrs232,
242 .num_ports = 1,
243 .attach = cypress_hidcom_startup,
244 .release = cypress_release,
245 .open = cypress_open,
246 .close = cypress_close,
247 .dtr_rts = cypress_dtr_rts,
248 .write = cypress_write,
249 .write_room = cypress_write_room,
250 .ioctl = cypress_ioctl,
251 .set_termios = cypress_set_termios,
252 .tiocmget = cypress_tiocmget,
253 .tiocmset = cypress_tiocmset,
254 .chars_in_buffer = cypress_chars_in_buffer,
255 .throttle = cypress_throttle,
256 .unthrottle = cypress_unthrottle,
257 .read_int_callback = cypress_read_int_callback,
258 .write_int_callback = cypress_write_int_callback,
261 static struct usb_serial_driver cypress_ca42v2_device = {
262 .driver = {
263 .owner = THIS_MODULE,
264 .name = "nokiaca42v2",
266 .description = "Nokia CA-42 V2 Adapter",
267 .usb_driver = &cypress_driver,
268 .id_table = id_table_nokiaca42v2,
269 .num_ports = 1,
270 .attach = cypress_ca42v2_startup,
271 .release = cypress_release,
272 .open = cypress_open,
273 .close = cypress_close,
274 .dtr_rts = cypress_dtr_rts,
275 .write = cypress_write,
276 .write_room = cypress_write_room,
277 .ioctl = cypress_ioctl,
278 .set_termios = cypress_set_termios,
279 .tiocmget = cypress_tiocmget,
280 .tiocmset = cypress_tiocmset,
281 .chars_in_buffer = cypress_chars_in_buffer,
282 .throttle = cypress_throttle,
283 .unthrottle = cypress_unthrottle,
284 .read_int_callback = cypress_read_int_callback,
285 .write_int_callback = cypress_write_int_callback,
288 /*****************************************************************************
289 * Cypress serial helper functions
290 *****************************************************************************/
293 static int analyze_baud_rate(struct usb_serial_port *port, speed_t new_rate)
295 struct cypress_private *priv;
296 priv = usb_get_serial_port_data(port);
299 * The general purpose firmware for the Cypress M8 allows for
300 * a maximum speed of 57600bps (I have no idea whether DeLorme
301 * chose to use the general purpose firmware or not), if you
302 * need to modify this speed setting for your own project
303 * please add your own chiptype and modify the code likewise.
304 * The Cypress HID->COM device will work successfully up to
305 * 115200bps (but the actual throughput is around 3kBps).
307 if (port->serial->dev->speed == USB_SPEED_LOW) {
309 * Mike Isely <isely@pobox.com> 2-Feb-2008: The
310 * Cypress app note that describes this mechanism
311 * states the the low-speed part can't handle more
312 * than 800 bytes/sec, in which case 4800 baud is the
313 * safest speed for a part like that.
315 if (new_rate > 4800) {
316 dbg("%s - failed setting baud rate, device incapable "
317 "speed %d", __func__, new_rate);
318 return -1;
321 switch (priv->chiptype) {
322 case CT_EARTHMATE:
323 if (new_rate <= 600) {
324 /* 300 and 600 baud rates are supported under
325 * the generic firmware, but are not used with
326 * NMEA and SiRF protocols */
327 dbg("%s - failed setting baud rate, unsupported speed "
328 "of %d on Earthmate GPS", __func__, new_rate);
329 return -1;
331 break;
332 default:
333 break;
335 return new_rate;
339 /* This function can either set or retrieve the current serial line settings */
340 static int cypress_serial_control(struct tty_struct *tty,
341 struct usb_serial_port *port, speed_t baud_rate, int data_bits,
342 int stop_bits, int parity_enable, int parity_type, int reset,
343 int cypress_request_type)
345 int new_baudrate = 0, retval = 0, tries = 0;
346 struct cypress_private *priv;
347 __u8 feature_buffer[5];
348 unsigned long flags;
350 dbg("%s", __func__);
352 priv = usb_get_serial_port_data(port);
354 if (!priv->comm_is_ok)
355 return -ENODEV;
357 switch (cypress_request_type) {
358 case CYPRESS_SET_CONFIG:
359 new_baudrate = priv->baud_rate;
360 /* 0 means 'Hang up' so doesn't change the true bit rate */
361 if (baud_rate == 0)
362 new_baudrate = priv->baud_rate;
363 /* Change of speed ? */
364 else if (baud_rate != priv->baud_rate) {
365 dbg("%s - baud rate is changing", __func__);
366 retval = analyze_baud_rate(port, baud_rate);
367 if (retval >= 0) {
368 new_baudrate = retval;
369 dbg("%s - New baud rate set to %d",
370 __func__, new_baudrate);
373 dbg("%s - baud rate is being sent as %d",
374 __func__, new_baudrate);
376 memset(feature_buffer, 0, sizeof(feature_buffer));
377 /* fill the feature_buffer with new configuration */
378 *((u_int32_t *)feature_buffer) = new_baudrate;
379 feature_buffer[4] |= data_bits; /* assign data bits in 2 bit space ( max 3 ) */
380 /* 1 bit gap */
381 feature_buffer[4] |= (stop_bits << 3); /* assign stop bits in 1 bit space */
382 feature_buffer[4] |= (parity_enable << 4); /* assign parity flag in 1 bit space */
383 feature_buffer[4] |= (parity_type << 5); /* assign parity type in 1 bit space */
384 /* 1 bit gap */
385 feature_buffer[4] |= (reset << 7); /* assign reset at end of byte, 1 bit space */
387 dbg("%s - device is being sent this feature report:",
388 __func__);
389 dbg("%s - %02X - %02X - %02X - %02X - %02X", __func__,
390 feature_buffer[0], feature_buffer[1],
391 feature_buffer[2], feature_buffer[3],
392 feature_buffer[4]);
394 do {
395 retval = usb_control_msg(port->serial->dev,
396 usb_sndctrlpipe(port->serial->dev, 0),
397 HID_REQ_SET_REPORT,
398 USB_DIR_OUT | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
399 0x0300, 0, feature_buffer,
400 sizeof(feature_buffer), 500);
402 if (tries++ >= 3)
403 break;
405 } while (retval != sizeof(feature_buffer) &&
406 retval != -ENODEV);
408 if (retval != sizeof(feature_buffer)) {
409 dev_err(&port->dev, "%s - failed sending serial "
410 "line settings - %d\n", __func__, retval);
411 cypress_set_dead(port);
412 } else {
413 spin_lock_irqsave(&priv->lock, flags);
414 priv->baud_rate = new_baudrate;
415 priv->current_config = feature_buffer[4];
416 spin_unlock_irqrestore(&priv->lock, flags);
417 /* If we asked for a speed change encode it */
418 if (baud_rate)
419 tty_encode_baud_rate(tty,
420 new_baudrate, new_baudrate);
422 break;
423 case CYPRESS_GET_CONFIG:
424 if (priv->get_cfg_unsafe) {
425 /* Not implemented for this device,
426 and if we try to do it we're likely
427 to crash the hardware. */
428 return -ENOTTY;
430 dbg("%s - retreiving serial line settings", __func__);
431 /* set initial values in feature buffer */
432 memset(feature_buffer, 0, sizeof(feature_buffer));
434 do {
435 retval = usb_control_msg(port->serial->dev,
436 usb_rcvctrlpipe(port->serial->dev, 0),
437 HID_REQ_GET_REPORT,
438 USB_DIR_IN | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
439 0x0300, 0, feature_buffer,
440 sizeof(feature_buffer), 500);
442 if (tries++ >= 3)
443 break;
444 } while (retval != sizeof(feature_buffer)
445 && retval != -ENODEV);
447 if (retval != sizeof(feature_buffer)) {
448 dev_err(&port->dev, "%s - failed to retrieve serial "
449 "line settings - %d\n", __func__, retval);
450 cypress_set_dead(port);
451 return retval;
452 } else {
453 spin_lock_irqsave(&priv->lock, flags);
454 /* store the config in one byte, and later
455 use bit masks to check values */
456 priv->current_config = feature_buffer[4];
457 priv->baud_rate = *((u_int32_t *)feature_buffer);
458 spin_unlock_irqrestore(&priv->lock, flags);
461 spin_lock_irqsave(&priv->lock, flags);
462 ++priv->cmd_count;
463 spin_unlock_irqrestore(&priv->lock, flags);
465 return retval;
466 } /* cypress_serial_control */
469 static void cypress_set_dead(struct usb_serial_port *port)
471 struct cypress_private *priv = usb_get_serial_port_data(port);
472 unsigned long flags;
474 spin_lock_irqsave(&priv->lock, flags);
475 if (!priv->comm_is_ok) {
476 spin_unlock_irqrestore(&priv->lock, flags);
477 return;
479 priv->comm_is_ok = 0;
480 spin_unlock_irqrestore(&priv->lock, flags);
482 dev_err(&port->dev, "cypress_m8 suspending failing port %d - "
483 "interval might be too short\n", port->number);
487 /*****************************************************************************
488 * Cypress serial driver functions
489 *****************************************************************************/
492 static int generic_startup(struct usb_serial *serial)
494 struct cypress_private *priv;
495 struct usb_serial_port *port = serial->port[0];
497 dbg("%s - port %d", __func__, port->number);
499 priv = kzalloc(sizeof(struct cypress_private), GFP_KERNEL);
500 if (!priv)
501 return -ENOMEM;
503 priv->comm_is_ok = !0;
504 spin_lock_init(&priv->lock);
505 priv->buf = cypress_buf_alloc(CYPRESS_BUF_SIZE);
506 if (priv->buf == NULL) {
507 kfree(priv);
508 return -ENOMEM;
510 init_waitqueue_head(&priv->delta_msr_wait);
512 usb_reset_configuration(serial->dev);
514 priv->cmd_ctrl = 0;
515 priv->line_control = 0;
516 priv->termios_initialized = 0;
517 priv->rx_flags = 0;
518 /* Default packet format setting is determined by packet size.
519 Anything with a size larger then 9 must have a separate
520 count field since the 3 bit count field is otherwise too
521 small. Otherwise we can use the slightly more compact
522 format. This is in accordance with the cypress_m8 serial
523 converter app note. */
524 if (port->interrupt_out_size > 9)
525 priv->pkt_fmt = packet_format_1;
526 else
527 priv->pkt_fmt = packet_format_2;
529 if (interval > 0) {
530 priv->write_urb_interval = interval;
531 priv->read_urb_interval = interval;
532 dbg("%s - port %d read & write intervals forced to %d",
533 __func__, port->number, interval);
534 } else {
535 priv->write_urb_interval = port->interrupt_out_urb->interval;
536 priv->read_urb_interval = port->interrupt_in_urb->interval;
537 dbg("%s - port %d intervals: read=%d write=%d",
538 __func__, port->number,
539 priv->read_urb_interval, priv->write_urb_interval);
541 usb_set_serial_port_data(port, priv);
543 return 0;
547 static int cypress_earthmate_startup(struct usb_serial *serial)
549 struct cypress_private *priv;
550 struct usb_serial_port *port = serial->port[0];
552 dbg("%s", __func__);
554 if (generic_startup(serial)) {
555 dbg("%s - Failed setting up port %d", __func__,
556 port->number);
557 return 1;
560 priv = usb_get_serial_port_data(port);
561 priv->chiptype = CT_EARTHMATE;
562 /* All Earthmate devices use the separated-count packet
563 format! Idiotic. */
564 priv->pkt_fmt = packet_format_1;
565 if (serial->dev->descriptor.idProduct !=
566 cpu_to_le16(PRODUCT_ID_EARTHMATEUSB)) {
567 /* The old original USB Earthmate seemed able to
568 handle GET_CONFIG requests; everything they've
569 produced since that time crashes if this command is
570 attempted :-( */
571 dbg("%s - Marking this device as unsafe for GET_CONFIG "
572 "commands", __func__);
573 priv->get_cfg_unsafe = !0;
576 return 0;
577 } /* cypress_earthmate_startup */
580 static int cypress_hidcom_startup(struct usb_serial *serial)
582 struct cypress_private *priv;
584 dbg("%s", __func__);
586 if (generic_startup(serial)) {
587 dbg("%s - Failed setting up port %d", __func__,
588 serial->port[0]->number);
589 return 1;
592 priv = usb_get_serial_port_data(serial->port[0]);
593 priv->chiptype = CT_CYPHIDCOM;
595 return 0;
596 } /* cypress_hidcom_startup */
599 static int cypress_ca42v2_startup(struct usb_serial *serial)
601 struct cypress_private *priv;
603 dbg("%s", __func__);
605 if (generic_startup(serial)) {
606 dbg("%s - Failed setting up port %d", __func__,
607 serial->port[0]->number);
608 return 1;
611 priv = usb_get_serial_port_data(serial->port[0]);
612 priv->chiptype = CT_CA42V2;
614 return 0;
615 } /* cypress_ca42v2_startup */
618 static void cypress_release(struct usb_serial *serial)
620 struct cypress_private *priv;
622 dbg("%s - port %d", __func__, serial->port[0]->number);
624 /* all open ports are closed at this point */
626 priv = usb_get_serial_port_data(serial->port[0]);
628 if (priv) {
629 cypress_buf_free(priv->buf);
630 kfree(priv);
635 static int cypress_open(struct tty_struct *tty, struct usb_serial_port *port)
637 struct cypress_private *priv = usb_get_serial_port_data(port);
638 struct usb_serial *serial = port->serial;
639 unsigned long flags;
640 int result = 0;
642 dbg("%s - port %d", __func__, port->number);
644 if (!priv->comm_is_ok)
645 return -EIO;
647 /* clear halts before open */
648 usb_clear_halt(serial->dev, 0x81);
649 usb_clear_halt(serial->dev, 0x02);
651 spin_lock_irqsave(&priv->lock, flags);
652 /* reset read/write statistics */
653 priv->bytes_in = 0;
654 priv->bytes_out = 0;
655 priv->cmd_count = 0;
656 priv->rx_flags = 0;
657 spin_unlock_irqrestore(&priv->lock, flags);
659 /* Set termios */
660 result = cypress_write(tty, port, NULL, 0);
662 if (result) {
663 dev_err(&port->dev,
664 "%s - failed setting the control lines - error %d\n",
665 __func__, result);
666 return result;
667 } else
668 dbg("%s - success setting the control lines", __func__);
670 if (tty)
671 cypress_set_termios(tty, port, &priv->tmp_termios);
673 /* setup the port and start reading from the device */
674 if (!port->interrupt_in_urb) {
675 dev_err(&port->dev, "%s - interrupt_in_urb is empty!\n",
676 __func__);
677 return -1;
680 usb_fill_int_urb(port->interrupt_in_urb, serial->dev,
681 usb_rcvintpipe(serial->dev, port->interrupt_in_endpointAddress),
682 port->interrupt_in_urb->transfer_buffer,
683 port->interrupt_in_urb->transfer_buffer_length,
684 cypress_read_int_callback, port, priv->read_urb_interval);
685 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
687 if (result) {
688 dev_err(&port->dev,
689 "%s - failed submitting read urb, error %d\n",
690 __func__, result);
691 cypress_set_dead(port);
693 port->port.drain_delay = 256;
694 return result;
695 } /* cypress_open */
697 static void cypress_dtr_rts(struct usb_serial_port *port, int on)
699 struct cypress_private *priv = usb_get_serial_port_data(port);
700 /* drop dtr and rts */
701 priv = usb_get_serial_port_data(port);
702 spin_lock_irq(&priv->lock);
703 if (on == 0)
704 priv->line_control = 0;
705 else
706 priv->line_control = CONTROL_DTR | CONTROL_RTS;
707 priv->cmd_ctrl = 1;
708 spin_unlock_irq(&priv->lock);
709 cypress_write(NULL, port, NULL, 0);
712 static void cypress_close(struct usb_serial_port *port)
714 struct cypress_private *priv = usb_get_serial_port_data(port);
716 dbg("%s - port %d", __func__, port->number);
718 /* writing is potentially harmful, lock must be taken */
719 mutex_lock(&port->serial->disc_mutex);
720 if (port->serial->disconnected) {
721 mutex_unlock(&port->serial->disc_mutex);
722 return;
724 cypress_buf_clear(priv->buf);
725 dbg("%s - stopping urbs", __func__);
726 usb_kill_urb(port->interrupt_in_urb);
727 usb_kill_urb(port->interrupt_out_urb);
730 if (stats)
731 dev_info(&port->dev, "Statistics: %d Bytes In | %d Bytes Out | %d Commands Issued\n",
732 priv->bytes_in, priv->bytes_out, priv->cmd_count);
733 mutex_unlock(&port->serial->disc_mutex);
734 } /* cypress_close */
737 static int cypress_write(struct tty_struct *tty, struct usb_serial_port *port,
738 const unsigned char *buf, int count)
740 struct cypress_private *priv = usb_get_serial_port_data(port);
741 unsigned long flags;
743 dbg("%s - port %d, %d bytes", __func__, port->number, count);
745 /* line control commands, which need to be executed immediately,
746 are not put into the buffer for obvious reasons.
748 if (priv->cmd_ctrl) {
749 count = 0;
750 goto finish;
753 if (!count)
754 return count;
756 spin_lock_irqsave(&priv->lock, flags);
757 count = cypress_buf_put(priv->buf, buf, count);
758 spin_unlock_irqrestore(&priv->lock, flags);
760 finish:
761 cypress_send(port);
763 return count;
764 } /* cypress_write */
767 static void cypress_send(struct usb_serial_port *port)
769 int count = 0, result, offset, actual_size;
770 struct cypress_private *priv = usb_get_serial_port_data(port);
771 unsigned long flags;
773 if (!priv->comm_is_ok)
774 return;
776 dbg("%s - port %d", __func__, port->number);
777 dbg("%s - interrupt out size is %d", __func__,
778 port->interrupt_out_size);
780 spin_lock_irqsave(&priv->lock, flags);
781 if (priv->write_urb_in_use) {
782 dbg("%s - can't write, urb in use", __func__);
783 spin_unlock_irqrestore(&priv->lock, flags);
784 return;
786 spin_unlock_irqrestore(&priv->lock, flags);
788 /* clear buffer */
789 memset(port->interrupt_out_urb->transfer_buffer, 0,
790 port->interrupt_out_size);
792 spin_lock_irqsave(&priv->lock, flags);
793 switch (priv->pkt_fmt) {
794 default:
795 case packet_format_1:
796 /* this is for the CY7C64013... */
797 offset = 2;
798 port->interrupt_out_buffer[0] = priv->line_control;
799 break;
800 case packet_format_2:
801 /* this is for the CY7C63743... */
802 offset = 1;
803 port->interrupt_out_buffer[0] = priv->line_control;
804 break;
807 if (priv->line_control & CONTROL_RESET)
808 priv->line_control &= ~CONTROL_RESET;
810 if (priv->cmd_ctrl) {
811 priv->cmd_count++;
812 dbg("%s - line control command being issued", __func__);
813 spin_unlock_irqrestore(&priv->lock, flags);
814 goto send;
815 } else
816 spin_unlock_irqrestore(&priv->lock, flags);
818 count = cypress_buf_get(priv->buf, &port->interrupt_out_buffer[offset],
819 port->interrupt_out_size-offset);
821 if (count == 0)
822 return;
824 switch (priv->pkt_fmt) {
825 default:
826 case packet_format_1:
827 port->interrupt_out_buffer[1] = count;
828 break;
829 case packet_format_2:
830 port->interrupt_out_buffer[0] |= count;
833 dbg("%s - count is %d", __func__, count);
835 send:
836 spin_lock_irqsave(&priv->lock, flags);
837 priv->write_urb_in_use = 1;
838 spin_unlock_irqrestore(&priv->lock, flags);
840 if (priv->cmd_ctrl)
841 actual_size = 1;
842 else
843 actual_size = count +
844 (priv->pkt_fmt == packet_format_1 ? 2 : 1);
846 usb_serial_debug_data(debug, &port->dev, __func__,
847 port->interrupt_out_size,
848 port->interrupt_out_urb->transfer_buffer);
850 usb_fill_int_urb(port->interrupt_out_urb, port->serial->dev,
851 usb_sndintpipe(port->serial->dev, port->interrupt_out_endpointAddress),
852 port->interrupt_out_buffer, port->interrupt_out_size,
853 cypress_write_int_callback, port, priv->write_urb_interval);
854 result = usb_submit_urb(port->interrupt_out_urb, GFP_ATOMIC);
855 if (result) {
856 dev_err(&port->dev,
857 "%s - failed submitting write urb, error %d\n",
858 __func__, result);
859 priv->write_urb_in_use = 0;
860 cypress_set_dead(port);
863 spin_lock_irqsave(&priv->lock, flags);
864 if (priv->cmd_ctrl)
865 priv->cmd_ctrl = 0;
867 /* do not count the line control and size bytes */
868 priv->bytes_out += count;
869 spin_unlock_irqrestore(&priv->lock, flags);
871 usb_serial_port_softint(port);
872 } /* cypress_send */
875 /* returns how much space is available in the soft buffer */
876 static int cypress_write_room(struct tty_struct *tty)
878 struct usb_serial_port *port = tty->driver_data;
879 struct cypress_private *priv = usb_get_serial_port_data(port);
880 int room = 0;
881 unsigned long flags;
883 dbg("%s - port %d", __func__, port->number);
885 spin_lock_irqsave(&priv->lock, flags);
886 room = cypress_buf_space_avail(priv->buf);
887 spin_unlock_irqrestore(&priv->lock, flags);
889 dbg("%s - returns %d", __func__, room);
890 return room;
894 static int cypress_tiocmget(struct tty_struct *tty, struct file *file)
896 struct usb_serial_port *port = tty->driver_data;
897 struct cypress_private *priv = usb_get_serial_port_data(port);
898 __u8 status, control;
899 unsigned int result = 0;
900 unsigned long flags;
902 dbg("%s - port %d", __func__, port->number);
904 spin_lock_irqsave(&priv->lock, flags);
905 control = priv->line_control;
906 status = priv->current_status;
907 spin_unlock_irqrestore(&priv->lock, flags);
909 result = ((control & CONTROL_DTR) ? TIOCM_DTR : 0)
910 | ((control & CONTROL_RTS) ? TIOCM_RTS : 0)
911 | ((status & UART_CTS) ? TIOCM_CTS : 0)
912 | ((status & UART_DSR) ? TIOCM_DSR : 0)
913 | ((status & UART_RI) ? TIOCM_RI : 0)
914 | ((status & UART_CD) ? TIOCM_CD : 0);
916 dbg("%s - result = %x", __func__, result);
918 return result;
922 static int cypress_tiocmset(struct tty_struct *tty, struct file *file,
923 unsigned int set, unsigned int clear)
925 struct usb_serial_port *port = tty->driver_data;
926 struct cypress_private *priv = usb_get_serial_port_data(port);
927 unsigned long flags;
929 dbg("%s - port %d", __func__, port->number);
931 spin_lock_irqsave(&priv->lock, flags);
932 if (set & TIOCM_RTS)
933 priv->line_control |= CONTROL_RTS;
934 if (set & TIOCM_DTR)
935 priv->line_control |= CONTROL_DTR;
936 if (clear & TIOCM_RTS)
937 priv->line_control &= ~CONTROL_RTS;
938 if (clear & TIOCM_DTR)
939 priv->line_control &= ~CONTROL_DTR;
940 priv->cmd_ctrl = 1;
941 spin_unlock_irqrestore(&priv->lock, flags);
943 return cypress_write(tty, port, NULL, 0);
947 static int cypress_ioctl(struct tty_struct *tty, struct file *file,
948 unsigned int cmd, unsigned long arg)
950 struct usb_serial_port *port = tty->driver_data;
951 struct cypress_private *priv = usb_get_serial_port_data(port);
953 dbg("%s - port %d, cmd 0x%.4x", __func__, port->number, cmd);
955 switch (cmd) {
956 /* This code comes from drivers/char/serial.c and ftdi_sio.c */
957 case TIOCMIWAIT:
958 while (priv != NULL) {
959 interruptible_sleep_on(&priv->delta_msr_wait);
960 /* see if a signal did it */
961 if (signal_pending(current))
962 return -ERESTARTSYS;
963 else {
964 char diff = priv->diff_status;
965 if (diff == 0)
966 return -EIO; /* no change => error */
968 /* consume all events */
969 priv->diff_status = 0;
971 /* return 0 if caller wanted to know about
972 these bits */
973 if (((arg & TIOCM_RNG) && (diff & UART_RI)) ||
974 ((arg & TIOCM_DSR) && (diff & UART_DSR)) ||
975 ((arg & TIOCM_CD) && (diff & UART_CD)) ||
976 ((arg & TIOCM_CTS) && (diff & UART_CTS)))
977 return 0;
978 /* otherwise caller can't care less about what
979 * happened, and so we continue to wait for
980 * more events.
984 return 0;
985 default:
986 break;
988 dbg("%s - arg not supported - it was 0x%04x - check include/asm/ioctls.h", __func__, cmd);
989 return -ENOIOCTLCMD;
990 } /* cypress_ioctl */
993 static void cypress_set_termios(struct tty_struct *tty,
994 struct usb_serial_port *port, struct ktermios *old_termios)
996 struct cypress_private *priv = usb_get_serial_port_data(port);
997 int data_bits, stop_bits, parity_type, parity_enable;
998 unsigned cflag, iflag;
999 unsigned long flags;
1000 __u8 oldlines;
1001 int linechange = 0;
1003 dbg("%s - port %d", __func__, port->number);
1005 spin_lock_irqsave(&priv->lock, flags);
1006 if (!priv->termios_initialized) {
1007 if (priv->chiptype == CT_EARTHMATE) {
1008 *(tty->termios) = tty_std_termios;
1009 tty->termios->c_cflag = B4800 | CS8 | CREAD | HUPCL |
1010 CLOCAL;
1011 tty->termios->c_ispeed = 4800;
1012 tty->termios->c_ospeed = 4800;
1013 } else if (priv->chiptype == CT_CYPHIDCOM) {
1014 *(tty->termios) = tty_std_termios;
1015 tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
1016 CLOCAL;
1017 tty->termios->c_ispeed = 9600;
1018 tty->termios->c_ospeed = 9600;
1019 } else if (priv->chiptype == CT_CA42V2) {
1020 *(tty->termios) = tty_std_termios;
1021 tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
1022 CLOCAL;
1023 tty->termios->c_ispeed = 9600;
1024 tty->termios->c_ospeed = 9600;
1026 priv->termios_initialized = 1;
1028 spin_unlock_irqrestore(&priv->lock, flags);
1030 /* Unsupported features need clearing */
1031 tty->termios->c_cflag &= ~(CMSPAR|CRTSCTS);
1033 cflag = tty->termios->c_cflag;
1034 iflag = tty->termios->c_iflag;
1036 /* check if there are new settings */
1037 if (old_termios) {
1038 spin_lock_irqsave(&priv->lock, flags);
1039 priv->tmp_termios = *(tty->termios);
1040 spin_unlock_irqrestore(&priv->lock, flags);
1043 /* set number of data bits, parity, stop bits */
1044 /* when parity is disabled the parity type bit is ignored */
1046 /* 1 means 2 stop bits, 0 means 1 stop bit */
1047 stop_bits = cflag & CSTOPB ? 1 : 0;
1049 if (cflag & PARENB) {
1050 parity_enable = 1;
1051 /* 1 means odd parity, 0 means even parity */
1052 parity_type = cflag & PARODD ? 1 : 0;
1053 } else
1054 parity_enable = parity_type = 0;
1056 switch (cflag & CSIZE) {
1057 case CS5:
1058 data_bits = 0;
1059 break;
1060 case CS6:
1061 data_bits = 1;
1062 break;
1063 case CS7:
1064 data_bits = 2;
1065 break;
1066 case CS8:
1067 data_bits = 3;
1068 break;
1069 default:
1070 dev_err(&port->dev, "%s - CSIZE was set, but not CS5-CS8\n",
1071 __func__);
1072 data_bits = 3;
1074 spin_lock_irqsave(&priv->lock, flags);
1075 oldlines = priv->line_control;
1076 if ((cflag & CBAUD) == B0) {
1077 /* drop dtr and rts */
1078 dbg("%s - dropping the lines, baud rate 0bps", __func__);
1079 priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
1080 } else
1081 priv->line_control = (CONTROL_DTR | CONTROL_RTS);
1082 spin_unlock_irqrestore(&priv->lock, flags);
1084 dbg("%s - sending %d stop_bits, %d parity_enable, %d parity_type, "
1085 "%d data_bits (+5)", __func__, stop_bits,
1086 parity_enable, parity_type, data_bits);
1088 cypress_serial_control(tty, port, tty_get_baud_rate(tty),
1089 data_bits, stop_bits,
1090 parity_enable, parity_type,
1091 0, CYPRESS_SET_CONFIG);
1093 /* we perform a CYPRESS_GET_CONFIG so that the current settings are
1094 * filled into the private structure this should confirm that all is
1095 * working if it returns what we just set */
1096 cypress_serial_control(tty, port, 0, 0, 0, 0, 0, 0, CYPRESS_GET_CONFIG);
1098 /* Here we can define custom tty settings for devices; the main tty
1099 * termios flag base comes from empeg.c */
1101 spin_lock_irqsave(&priv->lock, flags);
1102 if (priv->chiptype == CT_EARTHMATE && priv->baud_rate == 4800) {
1103 dbg("Using custom termios settings for a baud rate of "
1104 "4800bps.");
1105 /* define custom termios settings for NMEA protocol */
1107 tty->termios->c_iflag /* input modes - */
1108 &= ~(IGNBRK /* disable ignore break */
1109 | BRKINT /* disable break causes interrupt */
1110 | PARMRK /* disable mark parity errors */
1111 | ISTRIP /* disable clear high bit of input char */
1112 | INLCR /* disable translate NL to CR */
1113 | IGNCR /* disable ignore CR */
1114 | ICRNL /* disable translate CR to NL */
1115 | IXON); /* disable enable XON/XOFF flow control */
1117 tty->termios->c_oflag /* output modes */
1118 &= ~OPOST; /* disable postprocess output char */
1120 tty->termios->c_lflag /* line discipline modes */
1121 &= ~(ECHO /* disable echo input characters */
1122 | ECHONL /* disable echo new line */
1123 | ICANON /* disable erase, kill, werase, and rprnt
1124 special characters */
1125 | ISIG /* disable interrupt, quit, and suspend
1126 special characters */
1127 | IEXTEN); /* disable non-POSIX special characters */
1128 } /* CT_CYPHIDCOM: Application should handle this for device */
1130 linechange = (priv->line_control != oldlines);
1131 spin_unlock_irqrestore(&priv->lock, flags);
1133 /* if necessary, set lines */
1134 if (linechange) {
1135 priv->cmd_ctrl = 1;
1136 cypress_write(tty, port, NULL, 0);
1138 } /* cypress_set_termios */
1141 /* returns amount of data still left in soft buffer */
1142 static int cypress_chars_in_buffer(struct tty_struct *tty)
1144 struct usb_serial_port *port = tty->driver_data;
1145 struct cypress_private *priv = usb_get_serial_port_data(port);
1146 int chars = 0;
1147 unsigned long flags;
1149 dbg("%s - port %d", __func__, port->number);
1151 spin_lock_irqsave(&priv->lock, flags);
1152 chars = cypress_buf_data_avail(priv->buf);
1153 spin_unlock_irqrestore(&priv->lock, flags);
1155 dbg("%s - returns %d", __func__, chars);
1156 return chars;
1160 static void cypress_throttle(struct tty_struct *tty)
1162 struct usb_serial_port *port = tty->driver_data;
1163 struct cypress_private *priv = usb_get_serial_port_data(port);
1164 unsigned long flags;
1166 dbg("%s - port %d", __func__, port->number);
1168 spin_lock_irqsave(&priv->lock, flags);
1169 priv->rx_flags = THROTTLED;
1170 spin_unlock_irqrestore(&priv->lock, flags);
1174 static void cypress_unthrottle(struct tty_struct *tty)
1176 struct usb_serial_port *port = tty->driver_data;
1177 struct cypress_private *priv = usb_get_serial_port_data(port);
1178 int actually_throttled, result;
1179 unsigned long flags;
1181 dbg("%s - port %d", __func__, port->number);
1183 spin_lock_irqsave(&priv->lock, flags);
1184 actually_throttled = priv->rx_flags & ACTUALLY_THROTTLED;
1185 priv->rx_flags = 0;
1186 spin_unlock_irqrestore(&priv->lock, flags);
1188 if (!priv->comm_is_ok)
1189 return;
1191 if (actually_throttled) {
1192 port->interrupt_in_urb->dev = port->serial->dev;
1194 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
1195 if (result) {
1196 dev_err(&port->dev, "%s - failed submitting read urb, "
1197 "error %d\n", __func__, result);
1198 cypress_set_dead(port);
1204 static void cypress_read_int_callback(struct urb *urb)
1206 struct usb_serial_port *port = urb->context;
1207 struct cypress_private *priv = usb_get_serial_port_data(port);
1208 struct tty_struct *tty;
1209 unsigned char *data = urb->transfer_buffer;
1210 unsigned long flags;
1211 char tty_flag = TTY_NORMAL;
1212 int havedata = 0;
1213 int bytes = 0;
1214 int result;
1215 int i = 0;
1216 int status = urb->status;
1218 dbg("%s - port %d", __func__, port->number);
1220 switch (status) {
1221 case 0: /* success */
1222 break;
1223 case -ECONNRESET:
1224 case -ENOENT:
1225 case -ESHUTDOWN:
1226 /* precursor to disconnect so just go away */
1227 return;
1228 case -EPIPE:
1229 /* Can't call usb_clear_halt while in_interrupt */
1230 /* FALLS THROUGH */
1231 default:
1232 /* something ugly is going on... */
1233 dev_err(&urb->dev->dev,
1234 "%s - unexpected nonzero read status received: %d\n",
1235 __func__, status);
1236 cypress_set_dead(port);
1237 return;
1240 spin_lock_irqsave(&priv->lock, flags);
1241 if (priv->rx_flags & THROTTLED) {
1242 dbg("%s - now throttling", __func__);
1243 priv->rx_flags |= ACTUALLY_THROTTLED;
1244 spin_unlock_irqrestore(&priv->lock, flags);
1245 return;
1247 spin_unlock_irqrestore(&priv->lock, flags);
1249 tty = tty_port_tty_get(&port->port);
1250 if (!tty) {
1251 dbg("%s - bad tty pointer - exiting", __func__);
1252 return;
1255 spin_lock_irqsave(&priv->lock, flags);
1256 result = urb->actual_length;
1257 switch (priv->pkt_fmt) {
1258 default:
1259 case packet_format_1:
1260 /* This is for the CY7C64013... */
1261 priv->current_status = data[0] & 0xF8;
1262 bytes = data[1] + 2;
1263 i = 2;
1264 if (bytes > 2)
1265 havedata = 1;
1266 break;
1267 case packet_format_2:
1268 /* This is for the CY7C63743... */
1269 priv->current_status = data[0] & 0xF8;
1270 bytes = (data[0] & 0x07) + 1;
1271 i = 1;
1272 if (bytes > 1)
1273 havedata = 1;
1274 break;
1276 spin_unlock_irqrestore(&priv->lock, flags);
1277 if (result < bytes) {
1278 dbg("%s - wrong packet size - received %d bytes but packet "
1279 "said %d bytes", __func__, result, bytes);
1280 goto continue_read;
1283 usb_serial_debug_data(debug, &port->dev, __func__,
1284 urb->actual_length, data);
1286 spin_lock_irqsave(&priv->lock, flags);
1287 /* check to see if status has changed */
1288 if (priv->current_status != priv->prev_status) {
1289 priv->diff_status |= priv->current_status ^
1290 priv->prev_status;
1291 wake_up_interruptible(&priv->delta_msr_wait);
1292 priv->prev_status = priv->current_status;
1294 spin_unlock_irqrestore(&priv->lock, flags);
1296 /* hangup, as defined in acm.c... this might be a bad place for it
1297 * though */
1298 if (tty && !(tty->termios->c_cflag & CLOCAL) &&
1299 !(priv->current_status & UART_CD)) {
1300 dbg("%s - calling hangup", __func__);
1301 tty_hangup(tty);
1302 goto continue_read;
1305 /* There is one error bit... I'm assuming it is a parity error
1306 * indicator as the generic firmware will set this bit to 1 if a
1307 * parity error occurs.
1308 * I can not find reference to any other error events. */
1309 spin_lock_irqsave(&priv->lock, flags);
1310 if (priv->current_status & CYP_ERROR) {
1311 spin_unlock_irqrestore(&priv->lock, flags);
1312 tty_flag = TTY_PARITY;
1313 dbg("%s - Parity Error detected", __func__);
1314 } else
1315 spin_unlock_irqrestore(&priv->lock, flags);
1317 /* process read if there is data other than line status */
1318 if (tty && (bytes > i)) {
1319 bytes = tty_buffer_request_room(tty, bytes);
1320 for (; i < bytes ; ++i) {
1321 dbg("pushing byte number %d - %d - %c", i, data[i],
1322 data[i]);
1323 tty_insert_flip_char(tty, data[i], tty_flag);
1325 tty_flip_buffer_push(tty);
1328 spin_lock_irqsave(&priv->lock, flags);
1329 /* control and status byte(s) are also counted */
1330 priv->bytes_in += bytes;
1331 spin_unlock_irqrestore(&priv->lock, flags);
1333 continue_read:
1334 tty_kref_put(tty);
1336 /* Continue trying to always read... unless the port has closed. */
1338 if (port->port.count > 0 && priv->comm_is_ok) {
1339 usb_fill_int_urb(port->interrupt_in_urb, port->serial->dev,
1340 usb_rcvintpipe(port->serial->dev,
1341 port->interrupt_in_endpointAddress),
1342 port->interrupt_in_urb->transfer_buffer,
1343 port->interrupt_in_urb->transfer_buffer_length,
1344 cypress_read_int_callback, port,
1345 priv->read_urb_interval);
1346 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
1347 if (result) {
1348 dev_err(&urb->dev->dev, "%s - failed resubmitting "
1349 "read urb, error %d\n", __func__,
1350 result);
1351 cypress_set_dead(port);
1355 return;
1356 } /* cypress_read_int_callback */
1359 static void cypress_write_int_callback(struct urb *urb)
1361 struct usb_serial_port *port = urb->context;
1362 struct cypress_private *priv = usb_get_serial_port_data(port);
1363 int result;
1364 int status = urb->status;
1366 dbg("%s - port %d", __func__, port->number);
1368 switch (status) {
1369 case 0:
1370 /* success */
1371 break;
1372 case -ECONNRESET:
1373 case -ENOENT:
1374 case -ESHUTDOWN:
1375 /* this urb is terminated, clean up */
1376 dbg("%s - urb shutting down with status: %d",
1377 __func__, status);
1378 priv->write_urb_in_use = 0;
1379 return;
1380 case -EPIPE: /* no break needed; clear halt and resubmit */
1381 if (!priv->comm_is_ok)
1382 break;
1383 usb_clear_halt(port->serial->dev, 0x02);
1384 /* error in the urb, so we have to resubmit it */
1385 dbg("%s - nonzero write bulk status received: %d",
1386 __func__, status);
1387 port->interrupt_out_urb->transfer_buffer_length = 1;
1388 port->interrupt_out_urb->dev = port->serial->dev;
1389 result = usb_submit_urb(port->interrupt_out_urb, GFP_ATOMIC);
1390 if (!result)
1391 return;
1392 dev_err(&urb->dev->dev,
1393 "%s - failed resubmitting write urb, error %d\n",
1394 __func__, result);
1395 cypress_set_dead(port);
1396 break;
1397 default:
1398 dev_err(&urb->dev->dev,
1399 "%s - unexpected nonzero write status received: %d\n",
1400 __func__, status);
1401 cypress_set_dead(port);
1402 break;
1404 priv->write_urb_in_use = 0;
1406 /* send any buffered data */
1407 cypress_send(port);
1411 /*****************************************************************************
1412 * Write buffer functions - buffering code from pl2303 used
1413 *****************************************************************************/
1416 * cypress_buf_alloc
1418 * Allocate a circular buffer and all associated memory.
1421 static struct cypress_buf *cypress_buf_alloc(unsigned int size)
1424 struct cypress_buf *cb;
1427 if (size == 0)
1428 return NULL;
1430 cb = kmalloc(sizeof(struct cypress_buf), GFP_KERNEL);
1431 if (cb == NULL)
1432 return NULL;
1434 cb->buf_buf = kmalloc(size, GFP_KERNEL);
1435 if (cb->buf_buf == NULL) {
1436 kfree(cb);
1437 return NULL;
1440 cb->buf_size = size;
1441 cb->buf_get = cb->buf_put = cb->buf_buf;
1443 return cb;
1449 * cypress_buf_free
1451 * Free the buffer and all associated memory.
1454 static void cypress_buf_free(struct cypress_buf *cb)
1456 if (cb) {
1457 kfree(cb->buf_buf);
1458 kfree(cb);
1464 * cypress_buf_clear
1466 * Clear out all data in the circular buffer.
1469 static void cypress_buf_clear(struct cypress_buf *cb)
1471 if (cb != NULL)
1472 cb->buf_get = cb->buf_put;
1473 /* equivalent to a get of all data available */
1478 * cypress_buf_data_avail
1480 * Return the number of bytes of data available in the circular
1481 * buffer.
1484 static unsigned int cypress_buf_data_avail(struct cypress_buf *cb)
1486 if (cb != NULL)
1487 return (cb->buf_size + cb->buf_put - cb->buf_get)
1488 % cb->buf_size;
1489 else
1490 return 0;
1495 * cypress_buf_space_avail
1497 * Return the number of bytes of space available in the circular
1498 * buffer.
1501 static unsigned int cypress_buf_space_avail(struct cypress_buf *cb)
1503 if (cb != NULL)
1504 return (cb->buf_size + cb->buf_get - cb->buf_put - 1)
1505 % cb->buf_size;
1506 else
1507 return 0;
1512 * cypress_buf_put
1514 * Copy data data from a user buffer and put it into the circular buffer.
1515 * Restrict to the amount of space available.
1517 * Return the number of bytes copied.
1520 static unsigned int cypress_buf_put(struct cypress_buf *cb, const char *buf,
1521 unsigned int count)
1524 unsigned int len;
1527 if (cb == NULL)
1528 return 0;
1530 len = cypress_buf_space_avail(cb);
1531 if (count > len)
1532 count = len;
1534 if (count == 0)
1535 return 0;
1537 len = cb->buf_buf + cb->buf_size - cb->buf_put;
1538 if (count > len) {
1539 memcpy(cb->buf_put, buf, len);
1540 memcpy(cb->buf_buf, buf+len, count - len);
1541 cb->buf_put = cb->buf_buf + count - len;
1542 } else {
1543 memcpy(cb->buf_put, buf, count);
1544 if (count < len)
1545 cb->buf_put += count;
1546 else /* count == len */
1547 cb->buf_put = cb->buf_buf;
1550 return count;
1556 * cypress_buf_get
1558 * Get data from the circular buffer and copy to the given buffer.
1559 * Restrict to the amount of data available.
1561 * Return the number of bytes copied.
1564 static unsigned int cypress_buf_get(struct cypress_buf *cb, char *buf,
1565 unsigned int count)
1568 unsigned int len;
1571 if (cb == NULL)
1572 return 0;
1574 len = cypress_buf_data_avail(cb);
1575 if (count > len)
1576 count = len;
1578 if (count == 0)
1579 return 0;
1581 len = cb->buf_buf + cb->buf_size - cb->buf_get;
1582 if (count > len) {
1583 memcpy(buf, cb->buf_get, len);
1584 memcpy(buf+len, cb->buf_buf, count - len);
1585 cb->buf_get = cb->buf_buf + count - len;
1586 } else {
1587 memcpy(buf, cb->buf_get, count);
1588 if (count < len)
1589 cb->buf_get += count;
1590 else /* count == len */
1591 cb->buf_get = cb->buf_buf;
1594 return count;
1598 /*****************************************************************************
1599 * Module functions
1600 *****************************************************************************/
1602 static int __init cypress_init(void)
1604 int retval;
1606 dbg("%s", __func__);
1608 retval = usb_serial_register(&cypress_earthmate_device);
1609 if (retval)
1610 goto failed_em_register;
1611 retval = usb_serial_register(&cypress_hidcom_device);
1612 if (retval)
1613 goto failed_hidcom_register;
1614 retval = usb_serial_register(&cypress_ca42v2_device);
1615 if (retval)
1616 goto failed_ca42v2_register;
1617 retval = usb_register(&cypress_driver);
1618 if (retval)
1619 goto failed_usb_register;
1621 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
1622 DRIVER_DESC "\n");
1623 return 0;
1625 failed_usb_register:
1626 usb_serial_deregister(&cypress_ca42v2_device);
1627 failed_ca42v2_register:
1628 usb_serial_deregister(&cypress_hidcom_device);
1629 failed_hidcom_register:
1630 usb_serial_deregister(&cypress_earthmate_device);
1631 failed_em_register:
1632 return retval;
1636 static void __exit cypress_exit(void)
1638 dbg("%s", __func__);
1640 usb_deregister(&cypress_driver);
1641 usb_serial_deregister(&cypress_earthmate_device);
1642 usb_serial_deregister(&cypress_hidcom_device);
1643 usb_serial_deregister(&cypress_ca42v2_device);
1647 module_init(cypress_init);
1648 module_exit(cypress_exit);
1650 MODULE_AUTHOR(DRIVER_AUTHOR);
1651 MODULE_DESCRIPTION(DRIVER_DESC);
1652 MODULE_VERSION(DRIVER_VERSION);
1653 MODULE_LICENSE("GPL");
1655 module_param(debug, bool, S_IRUGO | S_IWUSR);
1656 MODULE_PARM_DESC(debug, "Debug enabled or not");
1657 module_param(stats, bool, S_IRUGO | S_IWUSR);
1658 MODULE_PARM_DESC(stats, "Enable statistics or not");
1659 module_param(interval, int, S_IRUGO | S_IWUSR);
1660 MODULE_PARM_DESC(interval, "Overrides interrupt interval");