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