Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * USB Cypress M8 driver |
| 3 | * |
| 4 | * Copyright (C) 2004 |
| 5 | * Lonnie Mendez (dignome@gmail.com) |
| 6 | * Copyright (C) 2003,2004 |
| 7 | * Neil Whelchel (koyama@firstlight.net) |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * See Documentation/usb/usb-serial.txt for more information on using this driver |
| 15 | * |
| 16 | * See http://geocities.com/i0xox0i for information on this driver and the |
| 17 | * earthmate usb device. |
| 18 | * |
Lonnie Mendez | 3cb4a4f | 2005-05-03 17:02:20 -0500 | [diff] [blame] | 19 | * Lonnie Mendez <dignome@gmail.com> |
| 20 | * 4-29-2005 |
| 21 | * Fixed problem where setting or retreiving the serial config would fail with |
| 22 | * EPIPE. Removed CRTS toggling so the driver behaves more like other usbserial |
| 23 | * adapters. Issued new interval of 1ms instead of the default 10ms. As a |
| 24 | * result, transfer speed has been substantially increased. From avg. 850bps to |
| 25 | * avg. 3300bps. initial termios has also been modified. Cleaned up code and |
| 26 | * formatting issues so it is more readable. Replaced the C++ style comments. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | * |
| 28 | * Lonnie Mendez <dignome@gmail.com> |
| 29 | * 12-15-2004 |
| 30 | * Incorporated write buffering from pl2303 driver. Fixed bug with line |
| 31 | * handling so both lines are raised in cypress_open. (was dropping rts) |
| 32 | * Various code cleanups made as well along with other misc bug fixes. |
| 33 | * |
| 34 | * Lonnie Mendez <dignome@gmail.com> |
| 35 | * 04-10-2004 |
| 36 | * Driver modified to support dynamic line settings. Various improvments |
| 37 | * and features. |
| 38 | * |
| 39 | * Neil Whelchel |
| 40 | * 10-2003 |
| 41 | * Driver first released. |
| 42 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | */ |
| 44 | |
| 45 | /* Thanks to Neil Whelchel for writing the first cypress m8 implementation for linux. */ |
| 46 | /* Thanks to cypress for providing references for the hid reports. */ |
| 47 | /* Thanks to Jiang Zhang for providing links and for general help. */ |
| 48 | /* Code originates and was built up from ftdi_sio, belkin, pl2303 and others. */ |
| 49 | |
| 50 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | #include <linux/kernel.h> |
| 52 | #include <linux/errno.h> |
| 53 | #include <linux/init.h> |
| 54 | #include <linux/slab.h> |
| 55 | #include <linux/tty.h> |
| 56 | #include <linux/tty_driver.h> |
| 57 | #include <linux/tty_flip.h> |
| 58 | #include <linux/module.h> |
| 59 | #include <linux/moduleparam.h> |
| 60 | #include <linux/spinlock.h> |
| 61 | #include <linux/usb.h> |
Greg Kroah-Hartman | a969888 | 2006-07-11 21:22:58 -0700 | [diff] [blame] | 62 | #include <linux/usb/serial.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | #include <linux/serial.h> |
| 64 | #include <linux/delay.h> |
| 65 | #include <asm/uaccess.h> |
| 66 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | #include "cypress_m8.h" |
| 68 | |
| 69 | |
| 70 | #ifdef CONFIG_USB_SERIAL_DEBUG |
| 71 | static int debug = 1; |
| 72 | #else |
| 73 | static int debug; |
| 74 | #endif |
| 75 | static int stats; |
Lonnie Mendez | 3cb4a4f | 2005-05-03 17:02:20 -0500 | [diff] [blame] | 76 | static int interval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | |
| 78 | /* |
| 79 | * Version Information |
| 80 | */ |
Lonnie Mendez | 3cb4a4f | 2005-05-03 17:02:20 -0500 | [diff] [blame] | 81 | #define DRIVER_VERSION "v1.09" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | #define DRIVER_AUTHOR "Lonnie Mendez <dignome@gmail.com>, Neil Whelchel <koyama@firstlight.net>" |
| 83 | #define DRIVER_DESC "Cypress USB to Serial Driver" |
| 84 | |
| 85 | /* write buffer size defines */ |
| 86 | #define CYPRESS_BUF_SIZE 1024 |
| 87 | #define CYPRESS_CLOSING_WAIT (30*HZ) |
| 88 | |
| 89 | static struct usb_device_id id_table_earthmate [] = { |
| 90 | { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB) }, |
Lonnie Mendez | 25b6f08 | 2005-05-10 17:09:52 -0500 | [diff] [blame] | 91 | { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB_LT20) }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | { } /* Terminating entry */ |
| 93 | }; |
| 94 | |
| 95 | static struct usb_device_id id_table_cyphidcomrs232 [] = { |
| 96 | { USB_DEVICE(VENDOR_ID_CYPRESS, PRODUCT_ID_CYPHIDCOM) }, |
Dmitry Shapin | 6f6f06e | 2008-03-04 15:25:10 -0800 | [diff] [blame] | 97 | { USB_DEVICE(VENDOR_ID_POWERCOM, PRODUCT_ID_UPS) }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | { } /* Terminating entry */ |
| 99 | }; |
| 100 | |
Lonnie Mendez | a5c44e2 | 2006-03-01 10:45:24 -0600 | [diff] [blame] | 101 | static struct usb_device_id id_table_nokiaca42v2 [] = { |
| 102 | { USB_DEVICE(VENDOR_ID_DAZZLE, PRODUCT_ID_CA42) }, |
| 103 | { } /* Terminating entry */ |
| 104 | }; |
| 105 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | static struct usb_device_id id_table_combined [] = { |
| 107 | { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB) }, |
Lonnie Mendez | 25b6f08 | 2005-05-10 17:09:52 -0500 | [diff] [blame] | 108 | { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB_LT20) }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | { USB_DEVICE(VENDOR_ID_CYPRESS, PRODUCT_ID_CYPHIDCOM) }, |
Dmitry Shapin | 6f6f06e | 2008-03-04 15:25:10 -0800 | [diff] [blame] | 110 | { USB_DEVICE(VENDOR_ID_POWERCOM, PRODUCT_ID_UPS) }, |
Lonnie Mendez | a5c44e2 | 2006-03-01 10:45:24 -0600 | [diff] [blame] | 111 | { USB_DEVICE(VENDOR_ID_DAZZLE, PRODUCT_ID_CA42) }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | { } /* Terminating entry */ |
| 113 | }; |
| 114 | |
| 115 | MODULE_DEVICE_TABLE (usb, id_table_combined); |
| 116 | |
| 117 | static struct usb_driver cypress_driver = { |
| 118 | .name = "cypress", |
| 119 | .probe = usb_serial_probe, |
| 120 | .disconnect = usb_serial_disconnect, |
| 121 | .id_table = id_table_combined, |
Greg Kroah-Hartman | ba9dc65 | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 122 | .no_dynamic_id = 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | }; |
| 124 | |
Mike Isely | 3416eaa | 2008-02-10 20:23:19 -0600 | [diff] [blame] | 125 | enum packet_format { |
| 126 | packet_format_1, /* b0:status, b1:payload count */ |
| 127 | packet_format_2 /* b0[7:3]:status, b0[2:0]:payload count */ |
| 128 | }; |
| 129 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | struct cypress_private { |
| 131 | spinlock_t lock; /* private lock */ |
| 132 | int chiptype; /* identifier of device, for quirks/etc */ |
| 133 | int bytes_in; /* used for statistics */ |
| 134 | int bytes_out; /* used for statistics */ |
| 135 | int cmd_count; /* used for statistics */ |
| 136 | int cmd_ctrl; /* always set this to 1 before issuing a command */ |
| 137 | struct cypress_buf *buf; /* write buffer */ |
| 138 | int write_urb_in_use; /* write urb in use indicator */ |
Mike Isely | 0257fa9 | 2006-08-29 22:06:59 -0500 | [diff] [blame] | 139 | int write_urb_interval; /* interval to use for write urb */ |
| 140 | int read_urb_interval; /* interval to use for read urb */ |
Mike Isely | 78aef51 | 2006-08-29 22:07:11 -0500 | [diff] [blame] | 141 | int comm_is_ok; /* true if communication is (still) ok */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | int termios_initialized; |
| 143 | __u8 line_control; /* holds dtr / rts value */ |
| 144 | __u8 current_status; /* received from last read - info on dsr,cts,cd,ri,etc */ |
| 145 | __u8 current_config; /* stores the current configuration byte */ |
| 146 | __u8 rx_flags; /* throttling - used from whiteheat/ftdi_sio */ |
Mike Isely | 3416eaa | 2008-02-10 20:23:19 -0600 | [diff] [blame] | 147 | enum packet_format pkt_fmt; /* format to use for packet send / receive */ |
Mike Isely | 3d6aa32 | 2008-02-10 20:23:24 -0600 | [diff] [blame] | 148 | int get_cfg_unsafe; /* If true, the CYPRESS_GET_CONFIG is unsafe */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | int baud_rate; /* stores current baud rate in integer form */ |
| 150 | int cbr_mask; /* stores current baud rate in masked form */ |
| 151 | int isthrottled; /* if throttled, discard reads */ |
| 152 | wait_queue_head_t delta_msr_wait; /* used for TIOCMIWAIT */ |
| 153 | char prev_status, diff_status; /* used for TIOCMIWAIT */ |
| 154 | /* we pass a pointer to this as the arguement sent to cypress_set_termios old_termios */ |
Alan Cox | 606d099 | 2006-12-08 02:38:45 -0800 | [diff] [blame] | 155 | struct ktermios tmp_termios; /* stores the old termios settings */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | }; |
| 157 | |
| 158 | /* write buffer structure */ |
| 159 | struct cypress_buf { |
| 160 | unsigned int buf_size; |
| 161 | char *buf_buf; |
| 162 | char *buf_get; |
| 163 | char *buf_put; |
| 164 | }; |
| 165 | |
| 166 | /* function prototypes for the Cypress USB to serial device */ |
| 167 | static int cypress_earthmate_startup (struct usb_serial *serial); |
| 168 | static int cypress_hidcom_startup (struct usb_serial *serial); |
Lonnie Mendez | a5c44e2 | 2006-03-01 10:45:24 -0600 | [diff] [blame] | 169 | static int cypress_ca42v2_startup (struct usb_serial *serial); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | static void cypress_shutdown (struct usb_serial *serial); |
| 171 | static int cypress_open (struct usb_serial_port *port, struct file *filp); |
| 172 | static void cypress_close (struct usb_serial_port *port, struct file *filp); |
| 173 | static int cypress_write (struct usb_serial_port *port, const unsigned char *buf, int count); |
| 174 | static void cypress_send (struct usb_serial_port *port); |
| 175 | static int cypress_write_room (struct usb_serial_port *port); |
| 176 | static int cypress_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg); |
Alan Cox | 606d099 | 2006-12-08 02:38:45 -0800 | [diff] [blame] | 177 | static void cypress_set_termios (struct usb_serial_port *port, struct ktermios * old); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | static int cypress_tiocmget (struct usb_serial_port *port, struct file *file); |
| 179 | static int cypress_tiocmset (struct usb_serial_port *port, struct file *file, unsigned int set, unsigned int clear); |
| 180 | static int cypress_chars_in_buffer (struct usb_serial_port *port); |
| 181 | static void cypress_throttle (struct usb_serial_port *port); |
| 182 | static void cypress_unthrottle (struct usb_serial_port *port); |
Mike Isely | 78aef51 | 2006-08-29 22:07:11 -0500 | [diff] [blame] | 183 | static void cypress_set_dead (struct usb_serial_port *port); |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 184 | static void cypress_read_int_callback (struct urb *urb); |
| 185 | static void cypress_write_int_callback (struct urb *urb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | /* baud helper functions */ |
| 187 | static int mask_to_rate (unsigned mask); |
| 188 | static unsigned rate_to_mask (int rate); |
| 189 | /* write buffer functions */ |
| 190 | static struct cypress_buf *cypress_buf_alloc(unsigned int size); |
| 191 | static void cypress_buf_free(struct cypress_buf *cb); |
| 192 | static void cypress_buf_clear(struct cypress_buf *cb); |
| 193 | static unsigned int cypress_buf_data_avail(struct cypress_buf *cb); |
| 194 | static unsigned int cypress_buf_space_avail(struct cypress_buf *cb); |
Lonnie Mendez | 3cb4a4f | 2005-05-03 17:02:20 -0500 | [diff] [blame] | 195 | static unsigned int cypress_buf_put(struct cypress_buf *cb, const char *buf, unsigned int count); |
| 196 | static unsigned int cypress_buf_get(struct cypress_buf *cb, char *buf, unsigned int count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | |
| 198 | |
Greg Kroah-Hartman | ea65370 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 199 | static struct usb_serial_driver cypress_earthmate_device = { |
Greg Kroah-Hartman | 18fcac3 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 200 | .driver = { |
| 201 | .owner = THIS_MODULE, |
Greg Kroah-Hartman | 269bda1 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 202 | .name = "earthmate", |
Greg Kroah-Hartman | 18fcac3 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 203 | }, |
Greg Kroah-Hartman | 269bda1 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 204 | .description = "DeLorme Earthmate USB", |
Johannes Hölzl | d9b1b78 | 2006-12-17 21:50:24 +0100 | [diff] [blame] | 205 | .usb_driver = &cypress_driver, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | .id_table = id_table_earthmate, |
| 207 | .num_interrupt_in = 1, |
| 208 | .num_interrupt_out = 1, |
| 209 | .num_bulk_in = NUM_DONT_CARE, |
| 210 | .num_bulk_out = NUM_DONT_CARE, |
| 211 | .num_ports = 1, |
| 212 | .attach = cypress_earthmate_startup, |
| 213 | .shutdown = cypress_shutdown, |
| 214 | .open = cypress_open, |
| 215 | .close = cypress_close, |
| 216 | .write = cypress_write, |
| 217 | .write_room = cypress_write_room, |
| 218 | .ioctl = cypress_ioctl, |
| 219 | .set_termios = cypress_set_termios, |
| 220 | .tiocmget = cypress_tiocmget, |
| 221 | .tiocmset = cypress_tiocmset, |
| 222 | .chars_in_buffer = cypress_chars_in_buffer, |
| 223 | .throttle = cypress_throttle, |
| 224 | .unthrottle = cypress_unthrottle, |
| 225 | .read_int_callback = cypress_read_int_callback, |
| 226 | .write_int_callback = cypress_write_int_callback, |
| 227 | }; |
| 228 | |
Greg Kroah-Hartman | ea65370 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 229 | static struct usb_serial_driver cypress_hidcom_device = { |
Greg Kroah-Hartman | 18fcac3 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 230 | .driver = { |
| 231 | .owner = THIS_MODULE, |
Greg Kroah-Hartman | 269bda1 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 232 | .name = "cyphidcom", |
Greg Kroah-Hartman | 18fcac3 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 233 | }, |
Greg Kroah-Hartman | 269bda1 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 234 | .description = "HID->COM RS232 Adapter", |
Johannes Hölzl | d9b1b78 | 2006-12-17 21:50:24 +0100 | [diff] [blame] | 235 | .usb_driver = &cypress_driver, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | .id_table = id_table_cyphidcomrs232, |
| 237 | .num_interrupt_in = 1, |
| 238 | .num_interrupt_out = 1, |
| 239 | .num_bulk_in = NUM_DONT_CARE, |
| 240 | .num_bulk_out = NUM_DONT_CARE, |
| 241 | .num_ports = 1, |
| 242 | .attach = cypress_hidcom_startup, |
| 243 | .shutdown = cypress_shutdown, |
| 244 | .open = cypress_open, |
| 245 | .close = cypress_close, |
| 246 | .write = cypress_write, |
| 247 | .write_room = cypress_write_room, |
| 248 | .ioctl = cypress_ioctl, |
| 249 | .set_termios = cypress_set_termios, |
| 250 | .tiocmget = cypress_tiocmget, |
| 251 | .tiocmset = cypress_tiocmset, |
| 252 | .chars_in_buffer = cypress_chars_in_buffer, |
| 253 | .throttle = cypress_throttle, |
| 254 | .unthrottle = cypress_unthrottle, |
| 255 | .read_int_callback = cypress_read_int_callback, |
| 256 | .write_int_callback = cypress_write_int_callback, |
| 257 | }; |
| 258 | |
Lonnie Mendez | a5c44e2 | 2006-03-01 10:45:24 -0600 | [diff] [blame] | 259 | static struct usb_serial_driver cypress_ca42v2_device = { |
| 260 | .driver = { |
| 261 | .owner = THIS_MODULE, |
| 262 | .name = "nokiaca42v2", |
| 263 | }, |
| 264 | .description = "Nokia CA-42 V2 Adapter", |
Johannes Hölzl | d9b1b78 | 2006-12-17 21:50:24 +0100 | [diff] [blame] | 265 | .usb_driver = &cypress_driver, |
Lonnie Mendez | a5c44e2 | 2006-03-01 10:45:24 -0600 | [diff] [blame] | 266 | .id_table = id_table_nokiaca42v2, |
| 267 | .num_interrupt_in = 1, |
| 268 | .num_interrupt_out = 1, |
| 269 | .num_bulk_in = NUM_DONT_CARE, |
| 270 | .num_bulk_out = NUM_DONT_CARE, |
| 271 | .num_ports = 1, |
| 272 | .attach = cypress_ca42v2_startup, |
| 273 | .shutdown = cypress_shutdown, |
| 274 | .open = cypress_open, |
| 275 | .close = cypress_close, |
| 276 | .write = cypress_write, |
| 277 | .write_room = cypress_write_room, |
| 278 | .ioctl = cypress_ioctl, |
| 279 | .set_termios = cypress_set_termios, |
| 280 | .tiocmget = cypress_tiocmget, |
| 281 | .tiocmset = cypress_tiocmset, |
| 282 | .chars_in_buffer = cypress_chars_in_buffer, |
| 283 | .throttle = cypress_throttle, |
| 284 | .unthrottle = cypress_unthrottle, |
| 285 | .read_int_callback = cypress_read_int_callback, |
| 286 | .write_int_callback = cypress_write_int_callback, |
| 287 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | |
| 289 | /***************************************************************************** |
| 290 | * Cypress serial helper functions |
| 291 | *****************************************************************************/ |
| 292 | |
| 293 | |
Steven Cole | 093cf72 | 2005-05-03 19:07:24 -0600 | [diff] [blame] | 294 | /* This function can either set or retrieve the current serial line settings */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | static int cypress_serial_control (struct usb_serial_port *port, unsigned baud_mask, int data_bits, int stop_bits, |
| 296 | int parity_enable, int parity_type, int reset, int cypress_request_type) |
| 297 | { |
Lonnie Mendez | 3cb4a4f | 2005-05-03 17:02:20 -0500 | [diff] [blame] | 298 | int new_baudrate = 0, retval = 0, tries = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | struct cypress_private *priv; |
Mike Isely | 9307554 | 2008-02-10 20:23:14 -0600 | [diff] [blame] | 300 | __u8 feature_buffer[5]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | unsigned long flags; |
| 302 | |
| 303 | dbg("%s", __FUNCTION__); |
| 304 | |
| 305 | priv = usb_get_serial_port_data(port); |
| 306 | |
Mike Isely | 78aef51 | 2006-08-29 22:07:11 -0500 | [diff] [blame] | 307 | if (!priv->comm_is_ok) |
| 308 | return -ENODEV; |
| 309 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | switch(cypress_request_type) { |
| 311 | case CYPRESS_SET_CONFIG: |
| 312 | |
| 313 | /* |
| 314 | * The general purpose firmware for the Cypress M8 allows for a maximum speed |
| 315 | * of 57600bps (I have no idea whether DeLorme chose to use the general purpose |
| 316 | * firmware or not), if you need to modify this speed setting for your own |
| 317 | * project please add your own chiptype and modify the code likewise. The |
Lonnie Mendez | 3cb4a4f | 2005-05-03 17:02:20 -0500 | [diff] [blame] | 318 | * Cypress HID->COM device will work successfully up to 115200bps (but the |
| 319 | * actual throughput is around 3kBps). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | */ |
| 321 | if (baud_mask != priv->cbr_mask) { |
| 322 | dbg("%s - baud rate is changing", __FUNCTION__); |
| 323 | if ( priv->chiptype == CT_EARTHMATE ) { |
| 324 | /* 300 and 600 baud rates are supported under the generic firmware, |
| 325 | * but are not used with NMEA and SiRF protocols */ |
| 326 | |
| 327 | if ( (baud_mask == B300) || (baud_mask == B600) ) { |
Lonnie Mendez | 3cb4a4f | 2005-05-03 17:02:20 -0500 | [diff] [blame] | 328 | err("%s - failed setting baud rate, unsupported speed", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | __FUNCTION__); |
Lonnie Mendez | 3cb4a4f | 2005-05-03 17:02:20 -0500 | [diff] [blame] | 330 | new_baudrate = priv->baud_rate; |
| 331 | } else if ( (new_baudrate = mask_to_rate(baud_mask)) == -1) { |
| 332 | err("%s - failed setting baud rate, unsupported speed", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | __FUNCTION__); |
Lonnie Mendez | 3cb4a4f | 2005-05-03 17:02:20 -0500 | [diff] [blame] | 334 | new_baudrate = priv->baud_rate; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | } |
| 336 | } else if (priv->chiptype == CT_CYPHIDCOM) { |
Lonnie Mendez | 3cb4a4f | 2005-05-03 17:02:20 -0500 | [diff] [blame] | 337 | if ( (new_baudrate = mask_to_rate(baud_mask)) == -1) { |
| 338 | err("%s - failed setting baud rate, unsupported speed", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | __FUNCTION__); |
Lonnie Mendez | 3cb4a4f | 2005-05-03 17:02:20 -0500 | [diff] [blame] | 340 | new_baudrate = priv->baud_rate; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | } |
Lonnie Mendez | a5c44e2 | 2006-03-01 10:45:24 -0600 | [diff] [blame] | 342 | } else if (priv->chiptype == CT_CA42V2) { |
| 343 | if ( (new_baudrate = mask_to_rate(baud_mask)) == -1) { |
| 344 | err("%s - failed setting baud rate, unsupported speed", |
| 345 | __FUNCTION__); |
| 346 | new_baudrate = priv->baud_rate; |
| 347 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | } else if (priv->chiptype == CT_GENERIC) { |
Lonnie Mendez | 3cb4a4f | 2005-05-03 17:02:20 -0500 | [diff] [blame] | 349 | if ( (new_baudrate = mask_to_rate(baud_mask)) == -1) { |
| 350 | err("%s - failed setting baud rate, unsupported speed", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | __FUNCTION__); |
Lonnie Mendez | 3cb4a4f | 2005-05-03 17:02:20 -0500 | [diff] [blame] | 352 | new_baudrate = priv->baud_rate; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | } |
| 354 | } else { |
Lonnie Mendez | 3cb4a4f | 2005-05-03 17:02:20 -0500 | [diff] [blame] | 355 | info("%s - please define your chiptype", __FUNCTION__); |
| 356 | new_baudrate = priv->baud_rate; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | } |
| 358 | } else { /* baud rate not changing, keep the old */ |
Lonnie Mendez | 3cb4a4f | 2005-05-03 17:02:20 -0500 | [diff] [blame] | 359 | new_baudrate = priv->baud_rate; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | } |
Lonnie Mendez | 3cb4a4f | 2005-05-03 17:02:20 -0500 | [diff] [blame] | 361 | dbg("%s - baud rate is being sent as %d", __FUNCTION__, new_baudrate); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | |
Mike Isely | 9307554 | 2008-02-10 20:23:14 -0600 | [diff] [blame] | 363 | memset(feature_buffer, 0, sizeof(feature_buffer)); |
Lonnie Mendez | 3cb4a4f | 2005-05-03 17:02:20 -0500 | [diff] [blame] | 364 | /* fill the feature_buffer with new configuration */ |
| 365 | *((u_int32_t *)feature_buffer) = new_baudrate; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | |
Lonnie Mendez | 3cb4a4f | 2005-05-03 17:02:20 -0500 | [diff] [blame] | 367 | feature_buffer[4] |= data_bits; /* assign data bits in 2 bit space ( max 3 ) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | /* 1 bit gap */ |
Lonnie Mendez | 3cb4a4f | 2005-05-03 17:02:20 -0500 | [diff] [blame] | 369 | feature_buffer[4] |= (stop_bits << 3); /* assign stop bits in 1 bit space */ |
| 370 | feature_buffer[4] |= (parity_enable << 4); /* assign parity flag in 1 bit space */ |
| 371 | feature_buffer[4] |= (parity_type << 5); /* assign parity type in 1 bit space */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | /* 1 bit gap */ |
Lonnie Mendez | 3cb4a4f | 2005-05-03 17:02:20 -0500 | [diff] [blame] | 373 | feature_buffer[4] |= (reset << 7); /* assign reset at end of byte, 1 bit space */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | |
| 375 | dbg("%s - device is being sent this feature report:", __FUNCTION__); |
| 376 | dbg("%s - %02X - %02X - %02X - %02X - %02X", __FUNCTION__, feature_buffer[0], feature_buffer[1], |
| 377 | feature_buffer[2], feature_buffer[3], feature_buffer[4]); |
| 378 | |
Lonnie Mendez | 3cb4a4f | 2005-05-03 17:02:20 -0500 | [diff] [blame] | 379 | do { |
Mike Isely | 9307554 | 2008-02-10 20:23:14 -0600 | [diff] [blame] | 380 | retval = usb_control_msg(port->serial->dev, |
| 381 | usb_sndctrlpipe(port->serial->dev, 0), |
| 382 | HID_REQ_SET_REPORT, |
| 383 | USB_DIR_OUT | USB_RECIP_INTERFACE | USB_TYPE_CLASS, |
| 384 | 0x0300, 0, feature_buffer, |
| 385 | sizeof(feature_buffer), 500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | |
Lonnie Mendez | 3cb4a4f | 2005-05-03 17:02:20 -0500 | [diff] [blame] | 387 | if (tries++ >= 3) |
| 388 | break; |
| 389 | |
Mike Isely | 9307554 | 2008-02-10 20:23:14 -0600 | [diff] [blame] | 390 | } while (retval != sizeof(feature_buffer) && |
| 391 | retval != -ENODEV); |
Lonnie Mendez | 3cb4a4f | 2005-05-03 17:02:20 -0500 | [diff] [blame] | 392 | |
Mike Isely | 9307554 | 2008-02-10 20:23:14 -0600 | [diff] [blame] | 393 | if (retval != sizeof(feature_buffer)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | err("%s - failed sending serial line settings - %d", __FUNCTION__, retval); |
Mike Isely | 78aef51 | 2006-08-29 22:07:11 -0500 | [diff] [blame] | 395 | cypress_set_dead(port); |
| 396 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | spin_lock_irqsave(&priv->lock, flags); |
Lonnie Mendez | 3cb4a4f | 2005-05-03 17:02:20 -0500 | [diff] [blame] | 398 | priv->baud_rate = new_baudrate; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | priv->cbr_mask = baud_mask; |
Lonnie Mendez | 3cb4a4f | 2005-05-03 17:02:20 -0500 | [diff] [blame] | 400 | priv->current_config = feature_buffer[4]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | spin_unlock_irqrestore(&priv->lock, flags); |
| 402 | } |
| 403 | break; |
| 404 | case CYPRESS_GET_CONFIG: |
Mike Isely | 3d6aa32 | 2008-02-10 20:23:24 -0600 | [diff] [blame] | 405 | if (priv->get_cfg_unsafe) { |
| 406 | /* Not implemented for this device, |
| 407 | and if we try to do it we're likely |
| 408 | to crash the hardware. */ |
| 409 | return -ENOTTY; |
| 410 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | dbg("%s - retreiving serial line settings", __FUNCTION__); |
Lonnie Mendez | 3cb4a4f | 2005-05-03 17:02:20 -0500 | [diff] [blame] | 412 | /* set initial values in feature buffer */ |
Mike Isely | 9307554 | 2008-02-10 20:23:14 -0600 | [diff] [blame] | 413 | memset(feature_buffer, 0, sizeof(feature_buffer)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | |
Lonnie Mendez | 3cb4a4f | 2005-05-03 17:02:20 -0500 | [diff] [blame] | 415 | do { |
Mike Isely | 9307554 | 2008-02-10 20:23:14 -0600 | [diff] [blame] | 416 | retval = usb_control_msg(port->serial->dev, |
| 417 | usb_rcvctrlpipe(port->serial->dev, 0), |
| 418 | HID_REQ_GET_REPORT, |
| 419 | USB_DIR_IN | USB_RECIP_INTERFACE | USB_TYPE_CLASS, |
| 420 | 0x0300, 0, feature_buffer, |
| 421 | sizeof(feature_buffer), 500); |
| 422 | |
Lonnie Mendez | 3cb4a4f | 2005-05-03 17:02:20 -0500 | [diff] [blame] | 423 | if (tries++ >= 3) |
| 424 | break; |
| 425 | |
Mike Isely | 9307554 | 2008-02-10 20:23:14 -0600 | [diff] [blame] | 426 | } while (retval != sizeof(feature_buffer) && |
| 427 | retval != -ENODEV); |
Lonnie Mendez | 3cb4a4f | 2005-05-03 17:02:20 -0500 | [diff] [blame] | 428 | |
Mike Isely | 9307554 | 2008-02-10 20:23:14 -0600 | [diff] [blame] | 429 | if (retval != sizeof(feature_buffer)) { |
Adrian Bunk | 943ffb5 | 2006-01-10 00:10:13 +0100 | [diff] [blame] | 430 | err("%s - failed to retrieve serial line settings - %d", __FUNCTION__, retval); |
Mike Isely | 78aef51 | 2006-08-29 22:07:11 -0500 | [diff] [blame] | 431 | cypress_set_dead(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | return retval; |
| 433 | } else { |
| 434 | spin_lock_irqsave(&priv->lock, flags); |
Lonnie Mendez | 3cb4a4f | 2005-05-03 17:02:20 -0500 | [diff] [blame] | 435 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | /* store the config in one byte, and later use bit masks to check values */ |
| 437 | priv->current_config = feature_buffer[4]; |
Lonnie Mendez | 3cb4a4f | 2005-05-03 17:02:20 -0500 | [diff] [blame] | 438 | priv->baud_rate = *((u_int32_t *)feature_buffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | |
Lonnie Mendez | 3cb4a4f | 2005-05-03 17:02:20 -0500 | [diff] [blame] | 440 | if ( (priv->cbr_mask = rate_to_mask(priv->baud_rate)) == 0x40) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | dbg("%s - failed setting the baud mask (not defined)", __FUNCTION__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | spin_unlock_irqrestore(&priv->lock, flags); |
| 443 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | } |
Lonnie Mendez | 3cb4a4f | 2005-05-03 17:02:20 -0500 | [diff] [blame] | 445 | spin_lock_irqsave(&priv->lock, flags); |
| 446 | ++priv->cmd_count; |
| 447 | spin_unlock_irqrestore(&priv->lock, flags); |
| 448 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | return retval; |
| 450 | } /* cypress_serial_control */ |
| 451 | |
| 452 | |
Mike Isely | 78aef51 | 2006-08-29 22:07:11 -0500 | [diff] [blame] | 453 | static void cypress_set_dead(struct usb_serial_port *port) |
| 454 | { |
| 455 | struct cypress_private *priv = usb_get_serial_port_data(port); |
| 456 | unsigned long flags; |
| 457 | |
| 458 | spin_lock_irqsave(&priv->lock, flags); |
| 459 | if (!priv->comm_is_ok) { |
| 460 | spin_unlock_irqrestore(&priv->lock, flags); |
| 461 | return; |
| 462 | } |
| 463 | priv->comm_is_ok = 0; |
| 464 | spin_unlock_irqrestore(&priv->lock, flags); |
| 465 | |
| 466 | err("cypress_m8 suspending failing port %d - interval might be too short", |
| 467 | port->number); |
| 468 | } |
| 469 | |
| 470 | |
Lonnie Mendez | 3cb4a4f | 2005-05-03 17:02:20 -0500 | [diff] [blame] | 471 | /* given a baud mask, it will return integer baud on success */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | static int mask_to_rate (unsigned mask) |
| 473 | { |
| 474 | int rate; |
| 475 | |
| 476 | switch (mask) { |
| 477 | case B0: rate = 0; break; |
| 478 | case B300: rate = 300; break; |
| 479 | case B600: rate = 600; break; |
| 480 | case B1200: rate = 1200; break; |
| 481 | case B2400: rate = 2400; break; |
| 482 | case B4800: rate = 4800; break; |
| 483 | case B9600: rate = 9600; break; |
| 484 | case B19200: rate = 19200; break; |
| 485 | case B38400: rate = 38400; break; |
| 486 | case B57600: rate = 57600; break; |
| 487 | case B115200: rate = 115200; break; |
| 488 | default: rate = -1; |
| 489 | } |
| 490 | |
| 491 | return rate; |
| 492 | } |
| 493 | |
| 494 | |
| 495 | static unsigned rate_to_mask (int rate) |
| 496 | { |
| 497 | unsigned mask; |
| 498 | |
| 499 | switch (rate) { |
| 500 | case 0: mask = B0; break; |
| 501 | case 300: mask = B300; break; |
| 502 | case 600: mask = B600; break; |
| 503 | case 1200: mask = B1200; break; |
| 504 | case 2400: mask = B2400; break; |
| 505 | case 4800: mask = B4800; break; |
| 506 | case 9600: mask = B9600; break; |
| 507 | case 19200: mask = B19200; break; |
| 508 | case 38400: mask = B38400; break; |
| 509 | case 57600: mask = B57600; break; |
| 510 | case 115200: mask = B115200; break; |
| 511 | default: mask = 0x40; |
| 512 | } |
| 513 | |
| 514 | return mask; |
| 515 | } |
| 516 | /***************************************************************************** |
| 517 | * Cypress serial driver functions |
| 518 | *****************************************************************************/ |
| 519 | |
| 520 | |
| 521 | static int generic_startup (struct usb_serial *serial) |
| 522 | { |
| 523 | struct cypress_private *priv; |
Mike Isely | 0257fa9 | 2006-08-29 22:06:59 -0500 | [diff] [blame] | 524 | struct usb_serial_port *port = serial->port[0]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | |
Mike Isely | 0257fa9 | 2006-08-29 22:06:59 -0500 | [diff] [blame] | 526 | dbg("%s - port %d", __FUNCTION__, port->number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | |
Eric Sesterhenn | 80b6ca4 | 2006-02-27 21:29:43 +0100 | [diff] [blame] | 528 | priv = kzalloc(sizeof (struct cypress_private), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | if (!priv) |
| 530 | return -ENOMEM; |
| 531 | |
Mike Isely | 78aef51 | 2006-08-29 22:07:11 -0500 | [diff] [blame] | 532 | priv->comm_is_ok = !0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | spin_lock_init(&priv->lock); |
| 534 | priv->buf = cypress_buf_alloc(CYPRESS_BUF_SIZE); |
| 535 | if (priv->buf == NULL) { |
| 536 | kfree(priv); |
| 537 | return -ENOMEM; |
| 538 | } |
| 539 | init_waitqueue_head(&priv->delta_msr_wait); |
| 540 | |
| 541 | usb_reset_configuration (serial->dev); |
| 542 | |
| 543 | priv->cmd_ctrl = 0; |
| 544 | priv->line_control = 0; |
| 545 | priv->termios_initialized = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | priv->rx_flags = 0; |
Lonnie Mendez | 3cb4a4f | 2005-05-03 17:02:20 -0500 | [diff] [blame] | 547 | priv->cbr_mask = B300; |
Mike Isely | 3416eaa | 2008-02-10 20:23:19 -0600 | [diff] [blame] | 548 | /* Default packet format setting is determined by packet size. |
| 549 | Anything with a size larger then 9 must have a separate |
| 550 | count field since the 3 bit count field is otherwise too |
| 551 | small. Otherwise we can use the slightly more compact |
| 552 | format. This is in accordance with the cypress_m8 serial |
| 553 | converter app note. */ |
| 554 | if (port->interrupt_out_size > 9) { |
| 555 | priv->pkt_fmt = packet_format_1; |
| 556 | } else { |
| 557 | priv->pkt_fmt = packet_format_2; |
| 558 | } |
Mike Isely | 0257fa9 | 2006-08-29 22:06:59 -0500 | [diff] [blame] | 559 | if (interval > 0) { |
| 560 | priv->write_urb_interval = interval; |
| 561 | priv->read_urb_interval = interval; |
| 562 | dbg("%s - port %d read & write intervals forced to %d", |
| 563 | __FUNCTION__,port->number,interval); |
| 564 | } else { |
| 565 | priv->write_urb_interval = port->interrupt_out_urb->interval; |
| 566 | priv->read_urb_interval = port->interrupt_in_urb->interval; |
| 567 | dbg("%s - port %d intervals: read=%d write=%d", |
| 568 | __FUNCTION__,port->number, |
| 569 | priv->read_urb_interval,priv->write_urb_interval); |
| 570 | } |
| 571 | usb_set_serial_port_data(port, priv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | |
Lonnie Mendez | b9db07f | 2005-07-12 17:21:31 -0500 | [diff] [blame] | 573 | return 0; |
| 574 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | |
| 576 | |
| 577 | static int cypress_earthmate_startup (struct usb_serial *serial) |
| 578 | { |
| 579 | struct cypress_private *priv; |
Mike Isely | 3d6aa32 | 2008-02-10 20:23:24 -0600 | [diff] [blame] | 580 | struct usb_serial_port *port = serial->port[0]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | |
| 582 | dbg("%s", __FUNCTION__); |
| 583 | |
| 584 | if (generic_startup(serial)) { |
Lonnie Mendez | b9db07f | 2005-07-12 17:21:31 -0500 | [diff] [blame] | 585 | dbg("%s - Failed setting up port %d", __FUNCTION__, |
Mike Isely | 3d6aa32 | 2008-02-10 20:23:24 -0600 | [diff] [blame] | 586 | port->number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | return 1; |
| 588 | } |
| 589 | |
Mike Isely | 3d6aa32 | 2008-02-10 20:23:24 -0600 | [diff] [blame] | 590 | priv = usb_get_serial_port_data(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | priv->chiptype = CT_EARTHMATE; |
Mike Isely | 3416eaa | 2008-02-10 20:23:19 -0600 | [diff] [blame] | 592 | /* All Earthmate devices use the separated-count packet |
| 593 | format! Idiotic. */ |
| 594 | priv->pkt_fmt = packet_format_1; |
Mike Isely | 3d6aa32 | 2008-02-10 20:23:24 -0600 | [diff] [blame] | 595 | if (serial->dev->descriptor.idProduct != PRODUCT_ID_EARTHMATEUSB) { |
| 596 | /* The old original USB Earthmate seemed able to |
| 597 | handle GET_CONFIG requests; everything they've |
| 598 | produced since that time crashes if this command is |
| 599 | attempted :-( */ |
| 600 | dbg("%s - Marking this device as unsafe for GET_CONFIG " |
| 601 | "commands", __func__); |
| 602 | priv->get_cfg_unsafe = !0; |
| 603 | } |
Lonnie Mendez | b9db07f | 2005-07-12 17:21:31 -0500 | [diff] [blame] | 604 | |
| 605 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | } /* cypress_earthmate_startup */ |
| 607 | |
| 608 | |
| 609 | static int cypress_hidcom_startup (struct usb_serial *serial) |
| 610 | { |
| 611 | struct cypress_private *priv; |
| 612 | |
| 613 | dbg("%s", __FUNCTION__); |
| 614 | |
| 615 | if (generic_startup(serial)) { |
Lonnie Mendez | b9db07f | 2005-07-12 17:21:31 -0500 | [diff] [blame] | 616 | dbg("%s - Failed setting up port %d", __FUNCTION__, |
| 617 | serial->port[0]->number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | return 1; |
| 619 | } |
| 620 | |
| 621 | priv = usb_get_serial_port_data(serial->port[0]); |
| 622 | priv->chiptype = CT_CYPHIDCOM; |
| 623 | |
Lonnie Mendez | b9db07f | 2005-07-12 17:21:31 -0500 | [diff] [blame] | 624 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | } /* cypress_hidcom_startup */ |
| 626 | |
| 627 | |
Lonnie Mendez | a5c44e2 | 2006-03-01 10:45:24 -0600 | [diff] [blame] | 628 | static int cypress_ca42v2_startup (struct usb_serial *serial) |
| 629 | { |
| 630 | struct cypress_private *priv; |
| 631 | |
| 632 | dbg("%s", __FUNCTION__); |
| 633 | |
| 634 | if (generic_startup(serial)) { |
| 635 | dbg("%s - Failed setting up port %d", __FUNCTION__, |
| 636 | serial->port[0]->number); |
| 637 | return 1; |
| 638 | } |
| 639 | |
| 640 | priv = usb_get_serial_port_data(serial->port[0]); |
| 641 | priv->chiptype = CT_CA42V2; |
| 642 | |
| 643 | return 0; |
| 644 | } /* cypress_ca42v2_startup */ |
| 645 | |
| 646 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | static void cypress_shutdown (struct usb_serial *serial) |
| 648 | { |
| 649 | struct cypress_private *priv; |
| 650 | |
| 651 | dbg ("%s - port %d", __FUNCTION__, serial->port[0]->number); |
| 652 | |
| 653 | /* all open ports are closed at this point */ |
| 654 | |
| 655 | priv = usb_get_serial_port_data(serial->port[0]); |
| 656 | |
| 657 | if (priv) { |
| 658 | cypress_buf_free(priv->buf); |
| 659 | kfree(priv); |
| 660 | usb_set_serial_port_data(serial->port[0], NULL); |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | |
| 665 | static int cypress_open (struct usb_serial_port *port, struct file *filp) |
| 666 | { |
| 667 | struct cypress_private *priv = usb_get_serial_port_data(port); |
| 668 | struct usb_serial *serial = port->serial; |
| 669 | unsigned long flags; |
| 670 | int result = 0; |
| 671 | |
| 672 | dbg("%s - port %d", __FUNCTION__, port->number); |
| 673 | |
Mike Isely | 78aef51 | 2006-08-29 22:07:11 -0500 | [diff] [blame] | 674 | if (!priv->comm_is_ok) |
| 675 | return -EIO; |
| 676 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | /* clear halts before open */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | usb_clear_halt(serial->dev, 0x81); |
| 679 | usb_clear_halt(serial->dev, 0x02); |
| 680 | |
| 681 | spin_lock_irqsave(&priv->lock, flags); |
| 682 | /* reset read/write statistics */ |
| 683 | priv->bytes_in = 0; |
| 684 | priv->bytes_out = 0; |
| 685 | priv->cmd_count = 0; |
| 686 | priv->rx_flags = 0; |
| 687 | spin_unlock_irqrestore(&priv->lock, flags); |
| 688 | |
| 689 | /* setting to zero could cause data loss */ |
| 690 | port->tty->low_latency = 1; |
| 691 | |
| 692 | /* raise both lines and set termios */ |
| 693 | spin_lock_irqsave(&priv->lock, flags); |
| 694 | priv->line_control = CONTROL_DTR | CONTROL_RTS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | priv->cmd_ctrl = 1; |
| 696 | spin_unlock_irqrestore(&priv->lock, flags); |
| 697 | result = cypress_write(port, NULL, 0); |
| 698 | |
| 699 | if (result) { |
| 700 | dev_err(&port->dev, "%s - failed setting the control lines - error %d\n", __FUNCTION__, result); |
| 701 | return result; |
| 702 | } else |
| 703 | dbg("%s - success setting the control lines", __FUNCTION__); |
| 704 | |
| 705 | cypress_set_termios(port, &priv->tmp_termios); |
| 706 | |
| 707 | /* setup the port and start reading from the device */ |
| 708 | if(!port->interrupt_in_urb){ |
| 709 | err("%s - interrupt_in_urb is empty!", __FUNCTION__); |
| 710 | return(-1); |
| 711 | } |
| 712 | |
| 713 | usb_fill_int_urb(port->interrupt_in_urb, serial->dev, |
| 714 | usb_rcvintpipe(serial->dev, port->interrupt_in_endpointAddress), |
| 715 | port->interrupt_in_urb->transfer_buffer, port->interrupt_in_urb->transfer_buffer_length, |
Mike Isely | 0257fa9 | 2006-08-29 22:06:59 -0500 | [diff] [blame] | 716 | cypress_read_int_callback, port, priv->read_urb_interval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL); |
| 718 | |
| 719 | if (result){ |
| 720 | dev_err(&port->dev, "%s - failed submitting read urb, error %d\n", __FUNCTION__, result); |
Mike Isely | 78aef51 | 2006-08-29 22:07:11 -0500 | [diff] [blame] | 721 | cypress_set_dead(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | return result; |
| 725 | } /* cypress_open */ |
| 726 | |
| 727 | |
| 728 | static void cypress_close(struct usb_serial_port *port, struct file * filp) |
| 729 | { |
| 730 | struct cypress_private *priv = usb_get_serial_port_data(port); |
| 731 | unsigned int c_cflag; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | int bps; |
| 733 | long timeout; |
| 734 | wait_queue_t wait; |
| 735 | |
| 736 | dbg("%s - port %d", __FUNCTION__, port->number); |
| 737 | |
| 738 | /* wait for data to drain from buffer */ |
Oliver Neukum | 9e3b1d8 | 2008-01-22 15:54:54 +0100 | [diff] [blame] | 739 | spin_lock_irq(&priv->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | timeout = CYPRESS_CLOSING_WAIT; |
| 741 | init_waitqueue_entry(&wait, current); |
| 742 | add_wait_queue(&port->tty->write_wait, &wait); |
| 743 | for (;;) { |
| 744 | set_current_state(TASK_INTERRUPTIBLE); |
| 745 | if (cypress_buf_data_avail(priv->buf) == 0 |
| 746 | || timeout == 0 || signal_pending(current) |
Oliver Neukum | 9e3b1d8 | 2008-01-22 15:54:54 +0100 | [diff] [blame] | 747 | /* without mutex, allowed due to harmless failure mode */ |
| 748 | || port->serial->disconnected) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | break; |
Oliver Neukum | 9e3b1d8 | 2008-01-22 15:54:54 +0100 | [diff] [blame] | 750 | spin_unlock_irq(&priv->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | timeout = schedule_timeout(timeout); |
Oliver Neukum | 9e3b1d8 | 2008-01-22 15:54:54 +0100 | [diff] [blame] | 752 | spin_lock_irq(&priv->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | } |
| 754 | set_current_state(TASK_RUNNING); |
| 755 | remove_wait_queue(&port->tty->write_wait, &wait); |
| 756 | /* clear out any remaining data in the buffer */ |
| 757 | cypress_buf_clear(priv->buf); |
Oliver Neukum | 9e3b1d8 | 2008-01-22 15:54:54 +0100 | [diff] [blame] | 758 | spin_unlock_irq(&priv->lock); |
| 759 | |
| 760 | /* writing is potentially harmful, lock must be taken */ |
| 761 | mutex_lock(&port->serial->disc_mutex); |
| 762 | if (port->serial->disconnected) { |
| 763 | mutex_unlock(&port->serial->disc_mutex); |
| 764 | return; |
| 765 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | /* wait for characters to drain from device */ |
| 767 | bps = tty_get_baud_rate(port->tty); |
| 768 | if (bps > 1200) |
| 769 | timeout = max((HZ*2560)/bps,HZ/10); |
| 770 | else |
| 771 | timeout = 2*HZ; |
Nishanth Aravamudan | 22c4386 | 2005-08-15 11:30:11 -0700 | [diff] [blame] | 772 | schedule_timeout_interruptible(timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | |
| 774 | dbg("%s - stopping urbs", __FUNCTION__); |
| 775 | usb_kill_urb (port->interrupt_in_urb); |
| 776 | usb_kill_urb (port->interrupt_out_urb); |
| 777 | |
| 778 | if (port->tty) { |
| 779 | c_cflag = port->tty->termios->c_cflag; |
| 780 | if (c_cflag & HUPCL) { |
| 781 | /* drop dtr and rts */ |
| 782 | priv = usb_get_serial_port_data(port); |
Oliver Neukum | 9e3b1d8 | 2008-01-22 15:54:54 +0100 | [diff] [blame] | 783 | spin_lock_irq(&priv->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | priv->line_control = 0; |
| 785 | priv->cmd_ctrl = 1; |
Oliver Neukum | 9e3b1d8 | 2008-01-22 15:54:54 +0100 | [diff] [blame] | 786 | spin_unlock_irq(&priv->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | cypress_write(port, NULL, 0); |
| 788 | } |
| 789 | } |
| 790 | |
| 791 | if (stats) |
| 792 | dev_info (&port->dev, "Statistics: %d Bytes In | %d Bytes Out | %d Commands Issued\n", |
| 793 | priv->bytes_in, priv->bytes_out, priv->cmd_count); |
Oliver Neukum | 9e3b1d8 | 2008-01-22 15:54:54 +0100 | [diff] [blame] | 794 | mutex_unlock(&port->serial->disc_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 795 | } /* cypress_close */ |
| 796 | |
| 797 | |
| 798 | static int cypress_write(struct usb_serial_port *port, const unsigned char *buf, int count) |
| 799 | { |
| 800 | struct cypress_private *priv = usb_get_serial_port_data(port); |
| 801 | unsigned long flags; |
| 802 | |
| 803 | dbg("%s - port %d, %d bytes", __FUNCTION__, port->number, count); |
| 804 | |
| 805 | /* line control commands, which need to be executed immediately, |
| 806 | are not put into the buffer for obvious reasons. |
| 807 | */ |
| 808 | if (priv->cmd_ctrl) { |
| 809 | count = 0; |
| 810 | goto finish; |
| 811 | } |
| 812 | |
| 813 | if (!count) |
| 814 | return count; |
| 815 | |
| 816 | spin_lock_irqsave(&priv->lock, flags); |
| 817 | count = cypress_buf_put(priv->buf, buf, count); |
| 818 | spin_unlock_irqrestore(&priv->lock, flags); |
| 819 | |
| 820 | finish: |
| 821 | cypress_send(port); |
| 822 | |
| 823 | return count; |
| 824 | } /* cypress_write */ |
| 825 | |
| 826 | |
| 827 | static void cypress_send(struct usb_serial_port *port) |
| 828 | { |
| 829 | int count = 0, result, offset, actual_size; |
| 830 | struct cypress_private *priv = usb_get_serial_port_data(port); |
| 831 | unsigned long flags; |
| 832 | |
Mike Isely | 78aef51 | 2006-08-29 22:07:11 -0500 | [diff] [blame] | 833 | if (!priv->comm_is_ok) |
| 834 | return; |
| 835 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 836 | dbg("%s - port %d", __FUNCTION__, port->number); |
| 837 | dbg("%s - interrupt out size is %d", __FUNCTION__, port->interrupt_out_size); |
| 838 | |
| 839 | spin_lock_irqsave(&priv->lock, flags); |
| 840 | if (priv->write_urb_in_use) { |
| 841 | dbg("%s - can't write, urb in use", __FUNCTION__); |
| 842 | spin_unlock_irqrestore(&priv->lock, flags); |
| 843 | return; |
| 844 | } |
| 845 | spin_unlock_irqrestore(&priv->lock, flags); |
| 846 | |
| 847 | /* clear buffer */ |
| 848 | memset(port->interrupt_out_urb->transfer_buffer, 0, port->interrupt_out_size); |
| 849 | |
| 850 | spin_lock_irqsave(&priv->lock, flags); |
Mike Isely | 3416eaa | 2008-02-10 20:23:19 -0600 | [diff] [blame] | 851 | switch (priv->pkt_fmt) { |
| 852 | default: |
| 853 | case packet_format_1: |
| 854 | /* this is for the CY7C64013... */ |
| 855 | offset = 2; |
| 856 | port->interrupt_out_buffer[0] = priv->line_control; |
| 857 | break; |
| 858 | case packet_format_2: |
| 859 | /* this is for the CY7C63743... */ |
| 860 | offset = 1; |
| 861 | port->interrupt_out_buffer[0] = priv->line_control; |
| 862 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | } |
| 864 | |
| 865 | if (priv->line_control & CONTROL_RESET) |
| 866 | priv->line_control &= ~CONTROL_RESET; |
| 867 | |
| 868 | if (priv->cmd_ctrl) { |
| 869 | priv->cmd_count++; |
| 870 | dbg("%s - line control command being issued", __FUNCTION__); |
| 871 | spin_unlock_irqrestore(&priv->lock, flags); |
| 872 | goto send; |
| 873 | } else |
| 874 | spin_unlock_irqrestore(&priv->lock, flags); |
| 875 | |
| 876 | count = cypress_buf_get(priv->buf, &port->interrupt_out_buffer[offset], |
| 877 | port->interrupt_out_size-offset); |
| 878 | |
| 879 | if (count == 0) { |
| 880 | return; |
| 881 | } |
| 882 | |
Mike Isely | 3416eaa | 2008-02-10 20:23:19 -0600 | [diff] [blame] | 883 | switch (priv->pkt_fmt) { |
| 884 | default: |
| 885 | case packet_format_1: |
| 886 | port->interrupt_out_buffer[1] = count; |
| 887 | break; |
| 888 | case packet_format_2: |
| 889 | port->interrupt_out_buffer[0] |= count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 890 | } |
| 891 | |
| 892 | dbg("%s - count is %d", __FUNCTION__, count); |
| 893 | |
| 894 | send: |
| 895 | spin_lock_irqsave(&priv->lock, flags); |
| 896 | priv->write_urb_in_use = 1; |
| 897 | spin_unlock_irqrestore(&priv->lock, flags); |
| 898 | |
| 899 | if (priv->cmd_ctrl) |
| 900 | actual_size = 1; |
| 901 | else |
Mike Isely | 3416eaa | 2008-02-10 20:23:19 -0600 | [diff] [blame] | 902 | actual_size = count + |
| 903 | (priv->pkt_fmt == packet_format_1 ? 2 : 1); |
| 904 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | usb_serial_debug_data(debug, &port->dev, __FUNCTION__, port->interrupt_out_size, |
| 906 | port->interrupt_out_urb->transfer_buffer); |
| 907 | |
Mike Isely | 9aa8dae | 2006-08-29 22:07:04 -0500 | [diff] [blame] | 908 | usb_fill_int_urb(port->interrupt_out_urb, port->serial->dev, |
| 909 | usb_sndintpipe(port->serial->dev, port->interrupt_out_endpointAddress), |
| 910 | port->interrupt_out_buffer, port->interrupt_out_size, |
| 911 | cypress_write_int_callback, port, priv->write_urb_interval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 912 | result = usb_submit_urb (port->interrupt_out_urb, GFP_ATOMIC); |
| 913 | if (result) { |
| 914 | dev_err(&port->dev, "%s - failed submitting write urb, error %d\n", __FUNCTION__, |
| 915 | result); |
| 916 | priv->write_urb_in_use = 0; |
Mike Isely | 78aef51 | 2006-08-29 22:07:11 -0500 | [diff] [blame] | 917 | cypress_set_dead(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | } |
| 919 | |
| 920 | spin_lock_irqsave(&priv->lock, flags); |
| 921 | if (priv->cmd_ctrl) { |
| 922 | priv->cmd_ctrl = 0; |
| 923 | } |
| 924 | priv->bytes_out += count; /* do not count the line control and size bytes */ |
| 925 | spin_unlock_irqrestore(&priv->lock, flags); |
| 926 | |
Pete Zaitcev | cf2c748 | 2006-05-22 21:58:49 -0700 | [diff] [blame] | 927 | usb_serial_port_softint(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | } /* cypress_send */ |
| 929 | |
| 930 | |
| 931 | /* returns how much space is available in the soft buffer */ |
| 932 | static int cypress_write_room(struct usb_serial_port *port) |
| 933 | { |
| 934 | struct cypress_private *priv = usb_get_serial_port_data(port); |
| 935 | int room = 0; |
| 936 | unsigned long flags; |
| 937 | |
| 938 | dbg("%s - port %d", __FUNCTION__, port->number); |
| 939 | |
| 940 | spin_lock_irqsave(&priv->lock, flags); |
| 941 | room = cypress_buf_space_avail(priv->buf); |
| 942 | spin_unlock_irqrestore(&priv->lock, flags); |
| 943 | |
| 944 | dbg("%s - returns %d", __FUNCTION__, room); |
| 945 | return room; |
| 946 | } |
| 947 | |
| 948 | |
| 949 | static int cypress_tiocmget (struct usb_serial_port *port, struct file *file) |
| 950 | { |
| 951 | struct cypress_private *priv = usb_get_serial_port_data(port); |
| 952 | __u8 status, control; |
| 953 | unsigned int result = 0; |
| 954 | unsigned long flags; |
| 955 | |
| 956 | dbg("%s - port %d", __FUNCTION__, port->number); |
| 957 | |
| 958 | spin_lock_irqsave(&priv->lock, flags); |
| 959 | control = priv->line_control; |
| 960 | status = priv->current_status; |
| 961 | spin_unlock_irqrestore(&priv->lock, flags); |
| 962 | |
| 963 | result = ((control & CONTROL_DTR) ? TIOCM_DTR : 0) |
| 964 | | ((control & CONTROL_RTS) ? TIOCM_RTS : 0) |
| 965 | | ((status & UART_CTS) ? TIOCM_CTS : 0) |
| 966 | | ((status & UART_DSR) ? TIOCM_DSR : 0) |
| 967 | | ((status & UART_RI) ? TIOCM_RI : 0) |
| 968 | | ((status & UART_CD) ? TIOCM_CD : 0); |
| 969 | |
| 970 | dbg("%s - result = %x", __FUNCTION__, result); |
| 971 | |
| 972 | return result; |
| 973 | } |
| 974 | |
| 975 | |
| 976 | static int cypress_tiocmset (struct usb_serial_port *port, struct file *file, |
| 977 | unsigned int set, unsigned int clear) |
| 978 | { |
| 979 | struct cypress_private *priv = usb_get_serial_port_data(port); |
| 980 | unsigned long flags; |
| 981 | |
| 982 | dbg("%s - port %d", __FUNCTION__, port->number); |
| 983 | |
| 984 | spin_lock_irqsave(&priv->lock, flags); |
| 985 | if (set & TIOCM_RTS) |
| 986 | priv->line_control |= CONTROL_RTS; |
| 987 | if (set & TIOCM_DTR) |
| 988 | priv->line_control |= CONTROL_DTR; |
| 989 | if (clear & TIOCM_RTS) |
| 990 | priv->line_control &= ~CONTROL_RTS; |
| 991 | if (clear & TIOCM_DTR) |
| 992 | priv->line_control &= ~CONTROL_DTR; |
| 993 | spin_unlock_irqrestore(&priv->lock, flags); |
| 994 | |
| 995 | priv->cmd_ctrl = 1; |
| 996 | return cypress_write(port, NULL, 0); |
| 997 | } |
| 998 | |
| 999 | |
| 1000 | static int cypress_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg) |
| 1001 | { |
| 1002 | struct cypress_private *priv = usb_get_serial_port_data(port); |
| 1003 | |
| 1004 | dbg("%s - port %d, cmd 0x%.4x", __FUNCTION__, port->number, cmd); |
| 1005 | |
| 1006 | switch (cmd) { |
| 1007 | case TIOCGSERIAL: |
Alan Cox | 606d099 | 2006-12-08 02:38:45 -0800 | [diff] [blame] | 1008 | if (copy_to_user((void __user *)arg, port->tty->termios, sizeof(struct ktermios))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1009 | return -EFAULT; |
| 1010 | } |
| 1011 | return (0); |
| 1012 | break; |
| 1013 | case TIOCSSERIAL: |
Alan Cox | 606d099 | 2006-12-08 02:38:45 -0800 | [diff] [blame] | 1014 | if (copy_from_user(port->tty->termios, (void __user *)arg, sizeof(struct ktermios))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | return -EFAULT; |
| 1016 | } |
| 1017 | /* here we need to call cypress_set_termios to invoke the new settings */ |
| 1018 | cypress_set_termios(port, &priv->tmp_termios); |
| 1019 | return (0); |
| 1020 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1021 | /* This code comes from drivers/char/serial.c and ftdi_sio.c */ |
| 1022 | case TIOCMIWAIT: |
| 1023 | while (priv != NULL) { |
| 1024 | interruptible_sleep_on(&priv->delta_msr_wait); |
| 1025 | /* see if a signal did it */ |
| 1026 | if (signal_pending(current)) |
| 1027 | return -ERESTARTSYS; |
| 1028 | else { |
| 1029 | char diff = priv->diff_status; |
| 1030 | |
| 1031 | if (diff == 0) { |
| 1032 | return -EIO; /* no change => error */ |
| 1033 | } |
| 1034 | |
| 1035 | /* consume all events */ |
| 1036 | priv->diff_status = 0; |
| 1037 | |
| 1038 | /* return 0 if caller wanted to know about these bits */ |
| 1039 | if ( ((arg & TIOCM_RNG) && (diff & UART_RI)) || |
| 1040 | ((arg & TIOCM_DSR) && (diff & UART_DSR)) || |
| 1041 | ((arg & TIOCM_CD) && (diff & UART_CD)) || |
| 1042 | ((arg & TIOCM_CTS) && (diff & UART_CTS)) ) { |
| 1043 | return 0; |
| 1044 | } |
| 1045 | /* otherwise caller can't care less about what happened, |
| 1046 | * and so we continue to wait for more events. |
| 1047 | */ |
| 1048 | } |
| 1049 | } |
| 1050 | return 0; |
| 1051 | break; |
| 1052 | default: |
| 1053 | break; |
| 1054 | } |
| 1055 | |
| 1056 | dbg("%s - arg not supported - it was 0x%04x - check include/asm/ioctls.h", __FUNCTION__, cmd); |
| 1057 | |
| 1058 | return -ENOIOCTLCMD; |
| 1059 | } /* cypress_ioctl */ |
| 1060 | |
| 1061 | |
Lonnie Mendez | b9db07f | 2005-07-12 17:21:31 -0500 | [diff] [blame] | 1062 | static void cypress_set_termios (struct usb_serial_port *port, |
Alan Cox | 606d099 | 2006-12-08 02:38:45 -0800 | [diff] [blame] | 1063 | struct ktermios *old_termios) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1064 | { |
| 1065 | struct cypress_private *priv = usb_get_serial_port_data(port); |
| 1066 | struct tty_struct *tty; |
| 1067 | int data_bits, stop_bits, parity_type, parity_enable; |
| 1068 | unsigned cflag, iflag, baud_mask; |
| 1069 | unsigned long flags; |
| 1070 | __u8 oldlines; |
Lonnie Mendez | 3cb4a4f | 2005-05-03 17:02:20 -0500 | [diff] [blame] | 1071 | int linechange = 0; |
Lonnie Mendez | b9db07f | 2005-07-12 17:21:31 -0500 | [diff] [blame] | 1072 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | dbg("%s - port %d", __FUNCTION__, port->number); |
| 1074 | |
| 1075 | tty = port->tty; |
| 1076 | if ((!tty) || (!tty->termios)) { |
| 1077 | dbg("%s - no tty structures", __FUNCTION__); |
| 1078 | return; |
| 1079 | } |
| 1080 | |
| 1081 | spin_lock_irqsave(&priv->lock, flags); |
| 1082 | if (!priv->termios_initialized) { |
| 1083 | if (priv->chiptype == CT_EARTHMATE) { |
| 1084 | *(tty->termios) = tty_std_termios; |
Lonnie Mendez | b9db07f | 2005-07-12 17:21:31 -0500 | [diff] [blame] | 1085 | tty->termios->c_cflag = B4800 | CS8 | CREAD | HUPCL | |
| 1086 | CLOCAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1087 | } else if (priv->chiptype == CT_CYPHIDCOM) { |
| 1088 | *(tty->termios) = tty_std_termios; |
Lonnie Mendez | b9db07f | 2005-07-12 17:21:31 -0500 | [diff] [blame] | 1089 | tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL | |
| 1090 | CLOCAL; |
Lonnie Mendez | a5c44e2 | 2006-03-01 10:45:24 -0600 | [diff] [blame] | 1091 | } else if (priv->chiptype == CT_CA42V2) { |
| 1092 | *(tty->termios) = tty_std_termios; |
| 1093 | tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL | |
| 1094 | CLOCAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1095 | } |
| 1096 | priv->termios_initialized = 1; |
| 1097 | } |
| 1098 | spin_unlock_irqrestore(&priv->lock, flags); |
| 1099 | |
| 1100 | cflag = tty->termios->c_cflag; |
| 1101 | iflag = tty->termios->c_iflag; |
| 1102 | |
| 1103 | /* check if there are new settings */ |
| 1104 | if (old_termios) { |
| 1105 | if ((cflag != old_termios->c_cflag) || |
Lonnie Mendez | b9db07f | 2005-07-12 17:21:31 -0500 | [diff] [blame] | 1106 | (RELEVANT_IFLAG(iflag) != |
| 1107 | RELEVANT_IFLAG(old_termios->c_iflag))) { |
| 1108 | dbg("%s - attempting to set new termios settings", |
| 1109 | __FUNCTION__); |
| 1110 | /* should make a copy of this in case something goes |
| 1111 | * wrong in the function, we can restore it */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1112 | spin_lock_irqsave(&priv->lock, flags); |
| 1113 | priv->tmp_termios = *(tty->termios); |
Lonnie Mendez | b9db07f | 2005-07-12 17:21:31 -0500 | [diff] [blame] | 1114 | spin_unlock_irqrestore(&priv->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1115 | } else { |
| 1116 | dbg("%s - nothing to do, exiting", __FUNCTION__); |
| 1117 | return; |
| 1118 | } |
| 1119 | } else |
| 1120 | return; |
| 1121 | |
| 1122 | /* set number of data bits, parity, stop bits */ |
| 1123 | /* when parity is disabled the parity type bit is ignored */ |
| 1124 | |
Lonnie Mendez | b9db07f | 2005-07-12 17:21:31 -0500 | [diff] [blame] | 1125 | /* 1 means 2 stop bits, 0 means 1 stop bit */ |
| 1126 | stop_bits = cflag & CSTOPB ? 1 : 0; |
| 1127 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1128 | if (cflag & PARENB) { |
| 1129 | parity_enable = 1; |
Lonnie Mendez | b9db07f | 2005-07-12 17:21:31 -0500 | [diff] [blame] | 1130 | /* 1 means odd parity, 0 means even parity */ |
| 1131 | parity_type = cflag & PARODD ? 1 : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1132 | } else |
| 1133 | parity_enable = parity_type = 0; |
| 1134 | |
| 1135 | if (cflag & CSIZE) { |
| 1136 | switch (cflag & CSIZE) { |
Lonnie Mendez | b9db07f | 2005-07-12 17:21:31 -0500 | [diff] [blame] | 1137 | case CS5: |
| 1138 | data_bits = 0; |
| 1139 | break; |
| 1140 | case CS6: |
| 1141 | data_bits = 1; |
| 1142 | break; |
| 1143 | case CS7: |
| 1144 | data_bits = 2; |
| 1145 | break; |
| 1146 | case CS8: |
| 1147 | data_bits = 3; |
| 1148 | break; |
| 1149 | default: |
| 1150 | err("%s - CSIZE was set, but not CS5-CS8", |
| 1151 | __FUNCTION__); |
| 1152 | data_bits = 3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1153 | } |
| 1154 | } else |
| 1155 | data_bits = 3; |
| 1156 | |
| 1157 | spin_lock_irqsave(&priv->lock, flags); |
| 1158 | oldlines = priv->line_control; |
| 1159 | if ((cflag & CBAUD) == B0) { |
| 1160 | /* drop dtr and rts */ |
| 1161 | dbg("%s - dropping the lines, baud rate 0bps", __FUNCTION__); |
| 1162 | baud_mask = B0; |
| 1163 | priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS); |
| 1164 | } else { |
| 1165 | baud_mask = (cflag & CBAUD); |
| 1166 | switch(baud_mask) { |
Lonnie Mendez | b9db07f | 2005-07-12 17:21:31 -0500 | [diff] [blame] | 1167 | case B300: |
| 1168 | dbg("%s - setting baud 300bps", __FUNCTION__); |
| 1169 | break; |
| 1170 | case B600: |
| 1171 | dbg("%s - setting baud 600bps", __FUNCTION__); |
| 1172 | break; |
| 1173 | case B1200: |
| 1174 | dbg("%s - setting baud 1200bps", __FUNCTION__); |
| 1175 | break; |
| 1176 | case B2400: |
| 1177 | dbg("%s - setting baud 2400bps", __FUNCTION__); |
| 1178 | break; |
| 1179 | case B4800: |
| 1180 | dbg("%s - setting baud 4800bps", __FUNCTION__); |
| 1181 | break; |
| 1182 | case B9600: |
| 1183 | dbg("%s - setting baud 9600bps", __FUNCTION__); |
| 1184 | break; |
| 1185 | case B19200: |
| 1186 | dbg("%s - setting baud 19200bps", __FUNCTION__); |
| 1187 | break; |
| 1188 | case B38400: |
| 1189 | dbg("%s - setting baud 38400bps", __FUNCTION__); |
| 1190 | break; |
| 1191 | case B57600: |
| 1192 | dbg("%s - setting baud 57600bps", __FUNCTION__); |
| 1193 | break; |
| 1194 | case B115200: |
| 1195 | dbg("%s - setting baud 115200bps", __FUNCTION__); |
| 1196 | break; |
| 1197 | default: |
| 1198 | dbg("%s - unknown masked baud rate", __FUNCTION__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1199 | } |
Lonnie Mendez | 3cb4a4f | 2005-05-03 17:02:20 -0500 | [diff] [blame] | 1200 | priv->line_control = (CONTROL_DTR | CONTROL_RTS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1201 | } |
| 1202 | spin_unlock_irqrestore(&priv->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1203 | |
Lonnie Mendez | b9db07f | 2005-07-12 17:21:31 -0500 | [diff] [blame] | 1204 | dbg("%s - sending %d stop_bits, %d parity_enable, %d parity_type, " |
| 1205 | "%d data_bits (+5)", __FUNCTION__, stop_bits, |
| 1206 | parity_enable, parity_type, data_bits); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1207 | |
Lonnie Mendez | b9db07f | 2005-07-12 17:21:31 -0500 | [diff] [blame] | 1208 | cypress_serial_control(port, baud_mask, data_bits, stop_bits, |
| 1209 | parity_enable, parity_type, 0, CYPRESS_SET_CONFIG); |
| 1210 | |
| 1211 | /* we perform a CYPRESS_GET_CONFIG so that the current settings are |
| 1212 | * filled into the private structure this should confirm that all is |
| 1213 | * working if it returns what we just set */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1214 | cypress_serial_control(port, 0, 0, 0, 0, 0, 0, CYPRESS_GET_CONFIG); |
| 1215 | |
Lonnie Mendez | b9db07f | 2005-07-12 17:21:31 -0500 | [diff] [blame] | 1216 | /* Here we can define custom tty settings for devices; the main tty |
| 1217 | * termios flag base comes from empeg.c */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1218 | |
Lonnie Mendez | b9db07f | 2005-07-12 17:21:31 -0500 | [diff] [blame] | 1219 | spin_lock_irqsave(&priv->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1220 | if ( (priv->chiptype == CT_EARTHMATE) && (priv->baud_rate == 4800) ) { |
Lonnie Mendez | b9db07f | 2005-07-12 17:21:31 -0500 | [diff] [blame] | 1221 | dbg("Using custom termios settings for a baud rate of " |
| 1222 | "4800bps."); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1223 | /* define custom termios settings for NMEA protocol */ |
| 1224 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1225 | tty->termios->c_iflag /* input modes - */ |
Lonnie Mendez | b9db07f | 2005-07-12 17:21:31 -0500 | [diff] [blame] | 1226 | &= ~(IGNBRK /* disable ignore break */ |
| 1227 | | BRKINT /* disable break causes interrupt */ |
| 1228 | | PARMRK /* disable mark parity errors */ |
| 1229 | | ISTRIP /* disable clear high bit of input char */ |
| 1230 | | INLCR /* disable translate NL to CR */ |
| 1231 | | IGNCR /* disable ignore CR */ |
| 1232 | | ICRNL /* disable translate CR to NL */ |
| 1233 | | IXON); /* disable enable XON/XOFF flow control */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1234 | |
Lonnie Mendez | b9db07f | 2005-07-12 17:21:31 -0500 | [diff] [blame] | 1235 | tty->termios->c_oflag /* output modes */ |
| 1236 | &= ~OPOST; /* disable postprocess output char */ |
| 1237 | |
| 1238 | tty->termios->c_lflag /* line discipline modes */ |
| 1239 | &= ~(ECHO /* disable echo input characters */ |
| 1240 | | ECHONL /* disable echo new line */ |
| 1241 | | ICANON /* disable erase, kill, werase, and rprnt |
| 1242 | special characters */ |
| 1243 | | ISIG /* disable interrupt, quit, and suspend |
| 1244 | special characters */ |
| 1245 | | IEXTEN); /* disable non-POSIX special characters */ |
Lonnie Mendez | 3cb4a4f | 2005-05-03 17:02:20 -0500 | [diff] [blame] | 1246 | } /* CT_CYPHIDCOM: Application should handle this for device */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1247 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1248 | linechange = (priv->line_control != oldlines); |
| 1249 | spin_unlock_irqrestore(&priv->lock, flags); |
| 1250 | |
| 1251 | /* if necessary, set lines */ |
Lonnie Mendez | 3cb4a4f | 2005-05-03 17:02:20 -0500 | [diff] [blame] | 1252 | if (linechange) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1253 | priv->cmd_ctrl = 1; |
| 1254 | cypress_write(port, NULL, 0); |
| 1255 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1256 | } /* cypress_set_termios */ |
| 1257 | |
Lonnie Mendez | b9db07f | 2005-07-12 17:21:31 -0500 | [diff] [blame] | 1258 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1259 | /* returns amount of data still left in soft buffer */ |
| 1260 | static int cypress_chars_in_buffer(struct usb_serial_port *port) |
| 1261 | { |
| 1262 | struct cypress_private *priv = usb_get_serial_port_data(port); |
| 1263 | int chars = 0; |
| 1264 | unsigned long flags; |
| 1265 | |
| 1266 | dbg("%s - port %d", __FUNCTION__, port->number); |
| 1267 | |
| 1268 | spin_lock_irqsave(&priv->lock, flags); |
| 1269 | chars = cypress_buf_data_avail(priv->buf); |
| 1270 | spin_unlock_irqrestore(&priv->lock, flags); |
| 1271 | |
| 1272 | dbg("%s - returns %d", __FUNCTION__, chars); |
| 1273 | return chars; |
| 1274 | } |
| 1275 | |
| 1276 | |
| 1277 | static void cypress_throttle (struct usb_serial_port *port) |
| 1278 | { |
| 1279 | struct cypress_private *priv = usb_get_serial_port_data(port); |
| 1280 | unsigned long flags; |
| 1281 | |
| 1282 | dbg("%s - port %d", __FUNCTION__, port->number); |
| 1283 | |
| 1284 | spin_lock_irqsave(&priv->lock, flags); |
| 1285 | priv->rx_flags = THROTTLED; |
Lonnie Mendez | b9db07f | 2005-07-12 17:21:31 -0500 | [diff] [blame] | 1286 | spin_unlock_irqrestore(&priv->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1287 | } |
| 1288 | |
| 1289 | |
| 1290 | static void cypress_unthrottle (struct usb_serial_port *port) |
| 1291 | { |
| 1292 | struct cypress_private *priv = usb_get_serial_port_data(port); |
| 1293 | int actually_throttled, result; |
| 1294 | unsigned long flags; |
| 1295 | |
| 1296 | dbg("%s - port %d", __FUNCTION__, port->number); |
| 1297 | |
| 1298 | spin_lock_irqsave(&priv->lock, flags); |
| 1299 | actually_throttled = priv->rx_flags & ACTUALLY_THROTTLED; |
| 1300 | priv->rx_flags = 0; |
| 1301 | spin_unlock_irqrestore(&priv->lock, flags); |
| 1302 | |
Mike Isely | 78aef51 | 2006-08-29 22:07:11 -0500 | [diff] [blame] | 1303 | if (!priv->comm_is_ok) |
| 1304 | return; |
| 1305 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1306 | if (actually_throttled) { |
| 1307 | port->interrupt_in_urb->dev = port->serial->dev; |
| 1308 | |
| 1309 | result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC); |
Mike Isely | 78aef51 | 2006-08-29 22:07:11 -0500 | [diff] [blame] | 1310 | if (result) { |
Lonnie Mendez | b9db07f | 2005-07-12 17:21:31 -0500 | [diff] [blame] | 1311 | dev_err(&port->dev, "%s - failed submitting read urb, " |
| 1312 | "error %d\n", __FUNCTION__, result); |
Mike Isely | 78aef51 | 2006-08-29 22:07:11 -0500 | [diff] [blame] | 1313 | cypress_set_dead(port); |
| 1314 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1315 | } |
| 1316 | } |
| 1317 | |
| 1318 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 1319 | static void cypress_read_int_callback(struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1320 | { |
| 1321 | struct usb_serial_port *port = (struct usb_serial_port *)urb->context; |
| 1322 | struct cypress_private *priv = usb_get_serial_port_data(port); |
| 1323 | struct tty_struct *tty; |
| 1324 | unsigned char *data = urb->transfer_buffer; |
| 1325 | unsigned long flags; |
Lonnie Mendez | b9db07f | 2005-07-12 17:21:31 -0500 | [diff] [blame] | 1326 | char tty_flag = TTY_NORMAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1327 | int havedata = 0; |
| 1328 | int bytes = 0; |
| 1329 | int result; |
| 1330 | int i = 0; |
Greg Kroah-Hartman | 8d7bc55 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 1331 | int status = urb->status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1332 | |
| 1333 | dbg("%s - port %d", __FUNCTION__, port->number); |
| 1334 | |
Greg Kroah-Hartman | 8d7bc55 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 1335 | switch (status) { |
Mike Isely | 78aef51 | 2006-08-29 22:07:11 -0500 | [diff] [blame] | 1336 | case 0: /* success */ |
| 1337 | break; |
| 1338 | case -ECONNRESET: |
| 1339 | case -ENOENT: |
| 1340 | case -ESHUTDOWN: |
| 1341 | /* precursor to disconnect so just go away */ |
| 1342 | return; |
| 1343 | case -EPIPE: |
| 1344 | usb_clear_halt(port->serial->dev,0x81); |
| 1345 | break; |
| 1346 | default: |
| 1347 | /* something ugly is going on... */ |
| 1348 | dev_err(&urb->dev->dev,"%s - unexpected nonzero read status received: %d\n", |
Greg Kroah-Hartman | 8d7bc55 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 1349 | __FUNCTION__, status); |
Mike Isely | 78aef51 | 2006-08-29 22:07:11 -0500 | [diff] [blame] | 1350 | cypress_set_dead(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1351 | return; |
| 1352 | } |
| 1353 | |
| 1354 | spin_lock_irqsave(&priv->lock, flags); |
| 1355 | if (priv->rx_flags & THROTTLED) { |
| 1356 | dbg("%s - now throttling", __FUNCTION__); |
| 1357 | priv->rx_flags |= ACTUALLY_THROTTLED; |
| 1358 | spin_unlock_irqrestore(&priv->lock, flags); |
| 1359 | return; |
| 1360 | } |
| 1361 | spin_unlock_irqrestore(&priv->lock, flags); |
| 1362 | |
| 1363 | tty = port->tty; |
| 1364 | if (!tty) { |
| 1365 | dbg("%s - bad tty pointer - exiting", __FUNCTION__); |
| 1366 | return; |
| 1367 | } |
| 1368 | |
| 1369 | spin_lock_irqsave(&priv->lock, flags); |
Mike Isely | 3416eaa | 2008-02-10 20:23:19 -0600 | [diff] [blame] | 1370 | result = urb->actual_length; |
| 1371 | switch (priv->pkt_fmt) { |
| 1372 | default: |
| 1373 | case packet_format_1: |
| 1374 | /* This is for the CY7C64013... */ |
| 1375 | priv->current_status = data[0] & 0xF8; |
| 1376 | bytes = data[1] + 2; |
| 1377 | i = 2; |
| 1378 | if (bytes > 2) |
| 1379 | havedata = 1; |
| 1380 | break; |
| 1381 | case packet_format_2: |
| 1382 | /* This is for the CY7C63743... */ |
| 1383 | priv->current_status = data[0] & 0xF8; |
| 1384 | bytes = (data[0] & 0x07) + 1; |
| 1385 | i = 1; |
| 1386 | if (bytes > 1) |
| 1387 | havedata = 1; |
| 1388 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1389 | } |
| 1390 | spin_unlock_irqrestore(&priv->lock, flags); |
Mike Isely | 3416eaa | 2008-02-10 20:23:19 -0600 | [diff] [blame] | 1391 | if (result < bytes) { |
| 1392 | dbg("%s - wrong packet size - received %d bytes but packet " |
| 1393 | "said %d bytes", __func__, result, bytes); |
| 1394 | goto continue_read; |
| 1395 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1396 | |
Lonnie Mendez | b9db07f | 2005-07-12 17:21:31 -0500 | [diff] [blame] | 1397 | usb_serial_debug_data (debug, &port->dev, __FUNCTION__, |
| 1398 | urb->actual_length, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1399 | |
| 1400 | spin_lock_irqsave(&priv->lock, flags); |
| 1401 | /* check to see if status has changed */ |
Mike Isely | 6768306 | 2008-02-10 20:23:28 -0600 | [diff] [blame^] | 1402 | if (priv->current_status != priv->prev_status) { |
| 1403 | priv->diff_status |= priv->current_status ^ |
| 1404 | priv->prev_status; |
| 1405 | wake_up_interruptible(&priv->delta_msr_wait); |
| 1406 | priv->prev_status = priv->current_status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1407 | } |
Lonnie Mendez | b9db07f | 2005-07-12 17:21:31 -0500 | [diff] [blame] | 1408 | spin_unlock_irqrestore(&priv->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1409 | |
Lonnie Mendez | b9db07f | 2005-07-12 17:21:31 -0500 | [diff] [blame] | 1410 | /* hangup, as defined in acm.c... this might be a bad place for it |
| 1411 | * though */ |
| 1412 | if (tty && !(tty->termios->c_cflag & CLOCAL) && |
| 1413 | !(priv->current_status & UART_CD)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1414 | dbg("%s - calling hangup", __FUNCTION__); |
| 1415 | tty_hangup(tty); |
| 1416 | goto continue_read; |
| 1417 | } |
| 1418 | |
Lonnie Mendez | b9db07f | 2005-07-12 17:21:31 -0500 | [diff] [blame] | 1419 | /* There is one error bit... I'm assuming it is a parity error |
| 1420 | * indicator as the generic firmware will set this bit to 1 if a |
| 1421 | * parity error occurs. |
| 1422 | * I can not find reference to any other error events. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1423 | spin_lock_irqsave(&priv->lock, flags); |
| 1424 | if (priv->current_status & CYP_ERROR) { |
| 1425 | spin_unlock_irqrestore(&priv->lock, flags); |
| 1426 | tty_flag = TTY_PARITY; |
| 1427 | dbg("%s - Parity Error detected", __FUNCTION__); |
| 1428 | } else |
| 1429 | spin_unlock_irqrestore(&priv->lock, flags); |
| 1430 | |
| 1431 | /* process read if there is data other than line status */ |
| 1432 | if (tty && (bytes > i)) { |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 1433 | bytes = tty_buffer_request_room(tty, bytes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1434 | for (; i < bytes ; ++i) { |
Lonnie Mendez | b9db07f | 2005-07-12 17:21:31 -0500 | [diff] [blame] | 1435 | dbg("pushing byte number %d - %d - %c", i, data[i], |
| 1436 | data[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1437 | tty_insert_flip_char(tty, data[i], tty_flag); |
| 1438 | } |
| 1439 | tty_flip_buffer_push(port->tty); |
| 1440 | } |
| 1441 | |
| 1442 | spin_lock_irqsave(&priv->lock, flags); |
Lonnie Mendez | b9db07f | 2005-07-12 17:21:31 -0500 | [diff] [blame] | 1443 | /* control and status byte(s) are also counted */ |
| 1444 | priv->bytes_in += bytes; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1445 | spin_unlock_irqrestore(&priv->lock, flags); |
| 1446 | |
| 1447 | continue_read: |
Lonnie Mendez | b9db07f | 2005-07-12 17:21:31 -0500 | [diff] [blame] | 1448 | |
| 1449 | /* Continue trying to always read... unless the port has closed. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1450 | |
Mike Isely | 78aef51 | 2006-08-29 22:07:11 -0500 | [diff] [blame] | 1451 | if (port->open_count > 0 && priv->comm_is_ok) { |
Lonnie Mendez | b9db07f | 2005-07-12 17:21:31 -0500 | [diff] [blame] | 1452 | usb_fill_int_urb(port->interrupt_in_urb, port->serial->dev, |
| 1453 | usb_rcvintpipe(port->serial->dev, |
| 1454 | port->interrupt_in_endpointAddress), |
| 1455 | port->interrupt_in_urb->transfer_buffer, |
| 1456 | port->interrupt_in_urb->transfer_buffer_length, |
Mike Isely | 0257fa9 | 2006-08-29 22:06:59 -0500 | [diff] [blame] | 1457 | cypress_read_int_callback, port, priv->read_urb_interval); |
Lonnie Mendez | b9db07f | 2005-07-12 17:21:31 -0500 | [diff] [blame] | 1458 | result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC); |
Mike Isely | 78aef51 | 2006-08-29 22:07:11 -0500 | [diff] [blame] | 1459 | if (result) { |
Lonnie Mendez | b9db07f | 2005-07-12 17:21:31 -0500 | [diff] [blame] | 1460 | dev_err(&urb->dev->dev, "%s - failed resubmitting " |
| 1461 | "read urb, error %d\n", __FUNCTION__, |
| 1462 | result); |
Mike Isely | 78aef51 | 2006-08-29 22:07:11 -0500 | [diff] [blame] | 1463 | cypress_set_dead(port); |
| 1464 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1465 | } |
Lonnie Mendez | b9db07f | 2005-07-12 17:21:31 -0500 | [diff] [blame] | 1466 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1467 | return; |
| 1468 | } /* cypress_read_int_callback */ |
| 1469 | |
| 1470 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 1471 | static void cypress_write_int_callback(struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1472 | { |
| 1473 | struct usb_serial_port *port = (struct usb_serial_port *)urb->context; |
| 1474 | struct cypress_private *priv = usb_get_serial_port_data(port); |
| 1475 | int result; |
Greg Kroah-Hartman | 8d7bc55 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 1476 | int status = urb->status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1477 | |
| 1478 | dbg("%s - port %d", __FUNCTION__, port->number); |
Greg Kroah-Hartman | 8d7bc55 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 1479 | |
| 1480 | switch (status) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1481 | case 0: |
| 1482 | /* success */ |
| 1483 | break; |
| 1484 | case -ECONNRESET: |
| 1485 | case -ENOENT: |
| 1486 | case -ESHUTDOWN: |
| 1487 | /* this urb is terminated, clean up */ |
Greg Kroah-Hartman | 8d7bc55 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 1488 | dbg("%s - urb shutting down with status: %d", |
| 1489 | __FUNCTION__, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1490 | priv->write_urb_in_use = 0; |
| 1491 | return; |
Mike Isely | 78aef51 | 2006-08-29 22:07:11 -0500 | [diff] [blame] | 1492 | case -EPIPE: /* no break needed; clear halt and resubmit */ |
| 1493 | if (!priv->comm_is_ok) |
| 1494 | break; |
Lonnie Mendez | 3cb4a4f | 2005-05-03 17:02:20 -0500 | [diff] [blame] | 1495 | usb_clear_halt(port->serial->dev, 0x02); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1496 | /* error in the urb, so we have to resubmit it */ |
Greg Kroah-Hartman | 8d7bc55 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 1497 | dbg("%s - nonzero write bulk status received: %d", |
| 1498 | __FUNCTION__, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1499 | port->interrupt_out_urb->transfer_buffer_length = 1; |
| 1500 | port->interrupt_out_urb->dev = port->serial->dev; |
| 1501 | result = usb_submit_urb(port->interrupt_out_urb, GFP_ATOMIC); |
Mike Isely | 78aef51 | 2006-08-29 22:07:11 -0500 | [diff] [blame] | 1502 | if (!result) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1503 | return; |
Mike Isely | 78aef51 | 2006-08-29 22:07:11 -0500 | [diff] [blame] | 1504 | dev_err(&urb->dev->dev, "%s - failed resubmitting write urb, error %d\n", |
| 1505 | __FUNCTION__, result); |
| 1506 | cypress_set_dead(port); |
| 1507 | break; |
| 1508 | default: |
| 1509 | dev_err(&urb->dev->dev,"%s - unexpected nonzero write status received: %d\n", |
Greg Kroah-Hartman | 8d7bc55 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 1510 | __FUNCTION__, status); |
Mike Isely | 78aef51 | 2006-08-29 22:07:11 -0500 | [diff] [blame] | 1511 | cypress_set_dead(port); |
| 1512 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1513 | } |
| 1514 | |
| 1515 | priv->write_urb_in_use = 0; |
| 1516 | |
| 1517 | /* send any buffered data */ |
| 1518 | cypress_send(port); |
| 1519 | } |
| 1520 | |
| 1521 | |
| 1522 | /***************************************************************************** |
| 1523 | * Write buffer functions - buffering code from pl2303 used |
| 1524 | *****************************************************************************/ |
| 1525 | |
| 1526 | /* |
| 1527 | * cypress_buf_alloc |
| 1528 | * |
| 1529 | * Allocate a circular buffer and all associated memory. |
| 1530 | */ |
| 1531 | |
| 1532 | static struct cypress_buf *cypress_buf_alloc(unsigned int size) |
| 1533 | { |
| 1534 | |
| 1535 | struct cypress_buf *cb; |
| 1536 | |
| 1537 | |
| 1538 | if (size == 0) |
| 1539 | return NULL; |
| 1540 | |
Robert P. J. Day | 5cbded5 | 2006-12-13 00:35:56 -0800 | [diff] [blame] | 1541 | cb = kmalloc(sizeof(struct cypress_buf), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1542 | if (cb == NULL) |
| 1543 | return NULL; |
| 1544 | |
| 1545 | cb->buf_buf = kmalloc(size, GFP_KERNEL); |
| 1546 | if (cb->buf_buf == NULL) { |
| 1547 | kfree(cb); |
| 1548 | return NULL; |
| 1549 | } |
| 1550 | |
| 1551 | cb->buf_size = size; |
| 1552 | cb->buf_get = cb->buf_put = cb->buf_buf; |
| 1553 | |
| 1554 | return cb; |
| 1555 | |
| 1556 | } |
| 1557 | |
| 1558 | |
| 1559 | /* |
| 1560 | * cypress_buf_free |
| 1561 | * |
| 1562 | * Free the buffer and all associated memory. |
| 1563 | */ |
| 1564 | |
| 1565 | static void cypress_buf_free(struct cypress_buf *cb) |
| 1566 | { |
Jesper Juhl | 1bc3c9e | 2005-04-18 17:39:34 -0700 | [diff] [blame] | 1567 | if (cb) { |
| 1568 | kfree(cb->buf_buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1569 | kfree(cb); |
| 1570 | } |
| 1571 | } |
| 1572 | |
| 1573 | |
| 1574 | /* |
| 1575 | * cypress_buf_clear |
| 1576 | * |
| 1577 | * Clear out all data in the circular buffer. |
| 1578 | */ |
| 1579 | |
| 1580 | static void cypress_buf_clear(struct cypress_buf *cb) |
| 1581 | { |
| 1582 | if (cb != NULL) |
| 1583 | cb->buf_get = cb->buf_put; |
| 1584 | /* equivalent to a get of all data available */ |
| 1585 | } |
| 1586 | |
| 1587 | |
| 1588 | /* |
| 1589 | * cypress_buf_data_avail |
| 1590 | * |
| 1591 | * Return the number of bytes of data available in the circular |
| 1592 | * buffer. |
| 1593 | */ |
| 1594 | |
| 1595 | static unsigned int cypress_buf_data_avail(struct cypress_buf *cb) |
| 1596 | { |
| 1597 | if (cb != NULL) |
| 1598 | return ((cb->buf_size + cb->buf_put - cb->buf_get) % cb->buf_size); |
| 1599 | else |
| 1600 | return 0; |
| 1601 | } |
| 1602 | |
| 1603 | |
| 1604 | /* |
| 1605 | * cypress_buf_space_avail |
| 1606 | * |
| 1607 | * Return the number of bytes of space available in the circular |
| 1608 | * buffer. |
| 1609 | */ |
| 1610 | |
| 1611 | static unsigned int cypress_buf_space_avail(struct cypress_buf *cb) |
| 1612 | { |
| 1613 | if (cb != NULL) |
| 1614 | return ((cb->buf_size + cb->buf_get - cb->buf_put - 1) % cb->buf_size); |
| 1615 | else |
| 1616 | return 0; |
| 1617 | } |
| 1618 | |
| 1619 | |
| 1620 | /* |
| 1621 | * cypress_buf_put |
| 1622 | * |
| 1623 | * Copy data data from a user buffer and put it into the circular buffer. |
| 1624 | * Restrict to the amount of space available. |
| 1625 | * |
| 1626 | * Return the number of bytes copied. |
| 1627 | */ |
| 1628 | |
| 1629 | static unsigned int cypress_buf_put(struct cypress_buf *cb, const char *buf, |
| 1630 | unsigned int count) |
| 1631 | { |
| 1632 | |
| 1633 | unsigned int len; |
| 1634 | |
| 1635 | |
| 1636 | if (cb == NULL) |
| 1637 | return 0; |
| 1638 | |
| 1639 | len = cypress_buf_space_avail(cb); |
| 1640 | if (count > len) |
| 1641 | count = len; |
| 1642 | |
| 1643 | if (count == 0) |
| 1644 | return 0; |
| 1645 | |
| 1646 | len = cb->buf_buf + cb->buf_size - cb->buf_put; |
| 1647 | if (count > len) { |
| 1648 | memcpy(cb->buf_put, buf, len); |
| 1649 | memcpy(cb->buf_buf, buf+len, count - len); |
| 1650 | cb->buf_put = cb->buf_buf + count - len; |
| 1651 | } else { |
| 1652 | memcpy(cb->buf_put, buf, count); |
| 1653 | if (count < len) |
| 1654 | cb->buf_put += count; |
| 1655 | else /* count == len */ |
| 1656 | cb->buf_put = cb->buf_buf; |
| 1657 | } |
| 1658 | |
| 1659 | return count; |
| 1660 | |
| 1661 | } |
| 1662 | |
| 1663 | |
| 1664 | /* |
| 1665 | * cypress_buf_get |
| 1666 | * |
| 1667 | * Get data from the circular buffer and copy to the given buffer. |
| 1668 | * Restrict to the amount of data available. |
| 1669 | * |
| 1670 | * Return the number of bytes copied. |
| 1671 | */ |
| 1672 | |
| 1673 | static unsigned int cypress_buf_get(struct cypress_buf *cb, char *buf, |
| 1674 | unsigned int count) |
| 1675 | { |
| 1676 | |
| 1677 | unsigned int len; |
| 1678 | |
| 1679 | |
| 1680 | if (cb == NULL) |
| 1681 | return 0; |
| 1682 | |
| 1683 | len = cypress_buf_data_avail(cb); |
| 1684 | if (count > len) |
| 1685 | count = len; |
| 1686 | |
| 1687 | if (count == 0) |
| 1688 | return 0; |
| 1689 | |
| 1690 | len = cb->buf_buf + cb->buf_size - cb->buf_get; |
| 1691 | if (count > len) { |
| 1692 | memcpy(buf, cb->buf_get, len); |
| 1693 | memcpy(buf+len, cb->buf_buf, count - len); |
| 1694 | cb->buf_get = cb->buf_buf + count - len; |
| 1695 | } else { |
| 1696 | memcpy(buf, cb->buf_get, count); |
| 1697 | if (count < len) |
| 1698 | cb->buf_get += count; |
| 1699 | else /* count == len */ |
| 1700 | cb->buf_get = cb->buf_buf; |
| 1701 | } |
| 1702 | |
| 1703 | return count; |
| 1704 | |
| 1705 | } |
| 1706 | |
| 1707 | /***************************************************************************** |
| 1708 | * Module functions |
| 1709 | *****************************************************************************/ |
| 1710 | |
| 1711 | static int __init cypress_init(void) |
| 1712 | { |
| 1713 | int retval; |
| 1714 | |
| 1715 | dbg("%s", __FUNCTION__); |
| 1716 | |
| 1717 | retval = usb_serial_register(&cypress_earthmate_device); |
| 1718 | if (retval) |
| 1719 | goto failed_em_register; |
| 1720 | retval = usb_serial_register(&cypress_hidcom_device); |
| 1721 | if (retval) |
| 1722 | goto failed_hidcom_register; |
Lonnie Mendez | a5c44e2 | 2006-03-01 10:45:24 -0600 | [diff] [blame] | 1723 | retval = usb_serial_register(&cypress_ca42v2_device); |
| 1724 | if (retval) |
| 1725 | goto failed_ca42v2_register; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1726 | retval = usb_register(&cypress_driver); |
| 1727 | if (retval) |
| 1728 | goto failed_usb_register; |
| 1729 | |
| 1730 | info(DRIVER_DESC " " DRIVER_VERSION); |
| 1731 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1732 | |
Mariusz Kozlowski | 2e46b74 | 2006-11-17 17:49:22 +0100 | [diff] [blame] | 1733 | failed_usb_register: |
| 1734 | usb_serial_deregister(&cypress_ca42v2_device); |
| 1735 | failed_ca42v2_register: |
| 1736 | usb_serial_deregister(&cypress_hidcom_device); |
| 1737 | failed_hidcom_register: |
| 1738 | usb_serial_deregister(&cypress_earthmate_device); |
| 1739 | failed_em_register: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1740 | return retval; |
| 1741 | } |
| 1742 | |
| 1743 | |
| 1744 | static void __exit cypress_exit (void) |
| 1745 | { |
| 1746 | dbg("%s", __FUNCTION__); |
| 1747 | |
| 1748 | usb_deregister (&cypress_driver); |
| 1749 | usb_serial_deregister (&cypress_earthmate_device); |
| 1750 | usb_serial_deregister (&cypress_hidcom_device); |
Lonnie Mendez | a5c44e2 | 2006-03-01 10:45:24 -0600 | [diff] [blame] | 1751 | usb_serial_deregister (&cypress_ca42v2_device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1752 | } |
| 1753 | |
| 1754 | |
| 1755 | module_init(cypress_init); |
| 1756 | module_exit(cypress_exit); |
| 1757 | |
| 1758 | MODULE_AUTHOR( DRIVER_AUTHOR ); |
| 1759 | MODULE_DESCRIPTION( DRIVER_DESC ); |
| 1760 | MODULE_VERSION( DRIVER_VERSION ); |
| 1761 | MODULE_LICENSE("GPL"); |
| 1762 | |
| 1763 | module_param(debug, bool, S_IRUGO | S_IWUSR); |
| 1764 | MODULE_PARM_DESC(debug, "Debug enabled or not"); |
| 1765 | module_param(stats, bool, S_IRUGO | S_IWUSR); |
| 1766 | MODULE_PARM_DESC(stats, "Enable statistics or not"); |
Lonnie Mendez | 3cb4a4f | 2005-05-03 17:02:20 -0500 | [diff] [blame] | 1767 | module_param(interval, int, S_IRUGO | S_IWUSR); |
| 1768 | MODULE_PARM_DESC(interval, "Overrides interrupt interval"); |