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