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