Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * USB ConnectTech WhiteHEAT driver |
| 3 | * |
| 4 | * Copyright (C) 2002 |
| 5 | * Connect Tech Inc. |
| 6 | * |
| 7 | * Copyright (C) 1999 - 2001 |
| 8 | * Greg Kroah-Hartman (greg@kroah.com) |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * See Documentation/usb/usb-serial.txt for more information on using this driver |
| 16 | * |
| 17 | * (10/09/2002) Stuart MacDonald (stuartm@connecttech.com) |
| 18 | * Upgrade to full working driver |
| 19 | * |
| 20 | * (05/30/2001) gkh |
| 21 | * switched from using spinlock to a semaphore, which fixes lots of problems. |
| 22 | * |
| 23 | * (04/08/2001) gb |
| 24 | * Identify version on module load. |
| 25 | * |
| 26 | * 2001_Mar_19 gkh |
| 27 | * Fixed MOD_INC and MOD_DEC logic, the ability to open a port more |
| 28 | * than once, and the got the proper usb_device_id table entries so |
| 29 | * the driver works again. |
| 30 | * |
| 31 | * (11/01/2000) Adam J. Richter |
| 32 | * usb_device_id table support |
| 33 | * |
| 34 | * (10/05/2000) gkh |
| 35 | * Fixed bug with urb->dev not being set properly, now that the usb |
| 36 | * core needs it. |
| 37 | * |
| 38 | * (10/03/2000) smd |
| 39 | * firmware is improved to guard against crap sent to device |
| 40 | * firmware now replies CMD_FAILURE on bad things |
| 41 | * read_callback fix you provided for private info struct |
| 42 | * command_finished now indicates success or fail |
| 43 | * setup_port struct now packed to avoid gcc padding |
| 44 | * firmware uses 1 based port numbering, driver now handles that |
| 45 | * |
| 46 | * (09/11/2000) gkh |
| 47 | * Removed DEBUG #ifdefs with call to usb_serial_debug_data |
| 48 | * |
| 49 | * (07/19/2000) gkh |
| 50 | * Added module_init and module_exit functions to handle the fact that this |
| 51 | * driver is a loadable module now. |
| 52 | * Fixed bug with port->minor that was found by Al Borchers |
| 53 | * |
| 54 | * (07/04/2000) gkh |
| 55 | * Added support for port settings. Baud rate can now be changed. Line signals |
| 56 | * are not transferred to and from the tty layer yet, but things seem to be |
| 57 | * working well now. |
| 58 | * |
| 59 | * (05/04/2000) gkh |
| 60 | * First cut at open and close commands. Data can flow through the ports at |
| 61 | * default speeds now. |
| 62 | * |
| 63 | * (03/26/2000) gkh |
| 64 | * Split driver up into device specific pieces. |
| 65 | * |
| 66 | */ |
| 67 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | #include <linux/kernel.h> |
| 69 | #include <linux/errno.h> |
| 70 | #include <linux/init.h> |
| 71 | #include <linux/slab.h> |
| 72 | #include <linux/tty.h> |
| 73 | #include <linux/tty_driver.h> |
| 74 | #include <linux/tty_flip.h> |
| 75 | #include <linux/module.h> |
| 76 | #include <linux/spinlock.h> |
Oliver Neukum | 08a2b3b | 2007-05-24 13:52:51 +0200 | [diff] [blame] | 77 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | #include <asm/uaccess.h> |
| 79 | #include <asm/termbits.h> |
| 80 | #include <linux/usb.h> |
| 81 | #include <linux/serial_reg.h> |
| 82 | #include <linux/serial.h> |
Greg Kroah-Hartman | a969888 | 2006-07-11 21:22:58 -0700 | [diff] [blame] | 83 | #include <linux/usb/serial.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | #include "whiteheat_fw.h" /* firmware for the ConnectTech WhiteHEAT device */ |
| 85 | #include "whiteheat.h" /* WhiteHEAT specific commands */ |
| 86 | |
| 87 | static int debug; |
| 88 | |
| 89 | #ifndef CMSPAR |
| 90 | #define CMSPAR 0 |
| 91 | #endif |
| 92 | |
| 93 | /* |
| 94 | * Version Information |
| 95 | */ |
| 96 | #define DRIVER_VERSION "v2.0" |
| 97 | #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>, Stuart MacDonald <stuartm@connecttech.com>" |
| 98 | #define DRIVER_DESC "USB ConnectTech WhiteHEAT driver" |
| 99 | |
| 100 | #define CONNECT_TECH_VENDOR_ID 0x0710 |
| 101 | #define CONNECT_TECH_FAKE_WHITE_HEAT_ID 0x0001 |
| 102 | #define CONNECT_TECH_WHITE_HEAT_ID 0x8001 |
| 103 | |
| 104 | /* |
| 105 | ID tables for whiteheat are unusual, because we want to different |
| 106 | things for different versions of the device. Eventually, this |
| 107 | will be doable from a single table. But, for now, we define two |
| 108 | separate ID tables, and then a third table that combines them |
| 109 | just for the purpose of exporting the autoloading information. |
| 110 | */ |
| 111 | static struct usb_device_id id_table_std [] = { |
| 112 | { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_WHITE_HEAT_ID) }, |
| 113 | { } /* Terminating entry */ |
| 114 | }; |
| 115 | |
| 116 | static struct usb_device_id id_table_prerenumeration [] = { |
| 117 | { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_FAKE_WHITE_HEAT_ID) }, |
| 118 | { } /* Terminating entry */ |
| 119 | }; |
| 120 | |
| 121 | static struct usb_device_id id_table_combined [] = { |
| 122 | { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_WHITE_HEAT_ID) }, |
| 123 | { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_FAKE_WHITE_HEAT_ID) }, |
| 124 | { } /* Terminating entry */ |
| 125 | }; |
| 126 | |
| 127 | MODULE_DEVICE_TABLE (usb, id_table_combined); |
| 128 | |
| 129 | static struct usb_driver whiteheat_driver = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | .name = "whiteheat", |
| 131 | .probe = usb_serial_probe, |
| 132 | .disconnect = usb_serial_disconnect, |
| 133 | .id_table = id_table_combined, |
Greg Kroah-Hartman | ba9dc65 | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 134 | .no_dynamic_id = 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | }; |
| 136 | |
| 137 | /* function prototypes for the Connect Tech WhiteHEAT prerenumeration device */ |
| 138 | static int whiteheat_firmware_download (struct usb_serial *serial, const struct usb_device_id *id); |
| 139 | static int whiteheat_firmware_attach (struct usb_serial *serial); |
| 140 | |
| 141 | /* function prototypes for the Connect Tech WhiteHEAT serial converter */ |
| 142 | static int whiteheat_attach (struct usb_serial *serial); |
| 143 | static void whiteheat_shutdown (struct usb_serial *serial); |
| 144 | static int whiteheat_open (struct usb_serial_port *port, struct file *filp); |
| 145 | static void whiteheat_close (struct usb_serial_port *port, struct file *filp); |
| 146 | static int whiteheat_write (struct usb_serial_port *port, const unsigned char *buf, int count); |
| 147 | static int whiteheat_write_room (struct usb_serial_port *port); |
| 148 | static int whiteheat_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg); |
Alan Cox | 606d099 | 2006-12-08 02:38:45 -0800 | [diff] [blame] | 149 | static void whiteheat_set_termios (struct usb_serial_port *port, struct ktermios * old); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | static int whiteheat_tiocmget (struct usb_serial_port *port, struct file *file); |
| 151 | static int whiteheat_tiocmset (struct usb_serial_port *port, struct file *file, unsigned int set, unsigned int clear); |
| 152 | static void whiteheat_break_ctl (struct usb_serial_port *port, int break_state); |
| 153 | static int whiteheat_chars_in_buffer (struct usb_serial_port *port); |
| 154 | static void whiteheat_throttle (struct usb_serial_port *port); |
| 155 | static void whiteheat_unthrottle (struct usb_serial_port *port); |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 156 | static void whiteheat_read_callback (struct urb *urb); |
| 157 | static void whiteheat_write_callback (struct urb *urb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | |
Greg Kroah-Hartman | ea65370 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 159 | static struct usb_serial_driver whiteheat_fake_device = { |
Greg Kroah-Hartman | 18fcac3 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 160 | .driver = { |
| 161 | .owner = THIS_MODULE, |
Greg Kroah-Hartman | 269bda1 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 162 | .name = "whiteheatnofirm", |
Greg Kroah-Hartman | 18fcac3 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 163 | }, |
Greg Kroah-Hartman | 269bda1 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 164 | .description = "Connect Tech - WhiteHEAT - (prerenumeration)", |
Johannes Hölzl | d9b1b78 | 2006-12-17 21:50:24 +0100 | [diff] [blame] | 165 | .usb_driver = &whiteheat_driver, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | .id_table = id_table_prerenumeration, |
| 167 | .num_interrupt_in = NUM_DONT_CARE, |
| 168 | .num_bulk_in = NUM_DONT_CARE, |
| 169 | .num_bulk_out = NUM_DONT_CARE, |
| 170 | .num_ports = 1, |
| 171 | .probe = whiteheat_firmware_download, |
| 172 | .attach = whiteheat_firmware_attach, |
| 173 | }; |
| 174 | |
Greg Kroah-Hartman | ea65370 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 175 | static struct usb_serial_driver whiteheat_device = { |
Greg Kroah-Hartman | 18fcac3 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 176 | .driver = { |
| 177 | .owner = THIS_MODULE, |
Greg Kroah-Hartman | 269bda1 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 178 | .name = "whiteheat", |
Greg Kroah-Hartman | 18fcac3 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 179 | }, |
Greg Kroah-Hartman | 269bda1 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 180 | .description = "Connect Tech - WhiteHEAT", |
Johannes Hölzl | d9b1b78 | 2006-12-17 21:50:24 +0100 | [diff] [blame] | 181 | .usb_driver = &whiteheat_driver, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | .id_table = id_table_std, |
| 183 | .num_interrupt_in = NUM_DONT_CARE, |
| 184 | .num_bulk_in = NUM_DONT_CARE, |
| 185 | .num_bulk_out = NUM_DONT_CARE, |
| 186 | .num_ports = 4, |
| 187 | .attach = whiteheat_attach, |
| 188 | .shutdown = whiteheat_shutdown, |
| 189 | .open = whiteheat_open, |
| 190 | .close = whiteheat_close, |
| 191 | .write = whiteheat_write, |
| 192 | .write_room = whiteheat_write_room, |
| 193 | .ioctl = whiteheat_ioctl, |
| 194 | .set_termios = whiteheat_set_termios, |
| 195 | .break_ctl = whiteheat_break_ctl, |
| 196 | .tiocmget = whiteheat_tiocmget, |
| 197 | .tiocmset = whiteheat_tiocmset, |
| 198 | .chars_in_buffer = whiteheat_chars_in_buffer, |
| 199 | .throttle = whiteheat_throttle, |
| 200 | .unthrottle = whiteheat_unthrottle, |
| 201 | .read_bulk_callback = whiteheat_read_callback, |
| 202 | .write_bulk_callback = whiteheat_write_callback, |
| 203 | }; |
| 204 | |
| 205 | |
| 206 | struct whiteheat_command_private { |
Oliver Neukum | 08a2b3b | 2007-05-24 13:52:51 +0200 | [diff] [blame] | 207 | struct mutex mutex; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | __u8 port_running; |
| 209 | __u8 command_finished; |
| 210 | wait_queue_head_t wait_command; /* for handling sleeping while waiting for a command to finish */ |
| 211 | __u8 result_buffer[64]; |
| 212 | }; |
| 213 | |
| 214 | |
| 215 | #define THROTTLED 0x01 |
| 216 | #define ACTUALLY_THROTTLED 0x02 |
| 217 | |
| 218 | static int urb_pool_size = 8; |
| 219 | |
| 220 | struct whiteheat_urb_wrap { |
| 221 | struct list_head list; |
| 222 | struct urb *urb; |
| 223 | }; |
| 224 | |
| 225 | struct whiteheat_private { |
| 226 | spinlock_t lock; |
| 227 | __u8 flags; |
| 228 | __u8 mcr; |
| 229 | struct list_head rx_urbs_free; |
| 230 | struct list_head rx_urbs_submitted; |
| 231 | struct list_head rx_urb_q; |
| 232 | struct work_struct rx_work; |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 233 | struct usb_serial_port *port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | struct list_head tx_urbs_free; |
| 235 | struct list_head tx_urbs_submitted; |
Oliver Neukum | 08a2b3b | 2007-05-24 13:52:51 +0200 | [diff] [blame] | 236 | struct mutex deathwarrant; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | }; |
| 238 | |
| 239 | |
| 240 | /* local function prototypes */ |
| 241 | static int start_command_port(struct usb_serial *serial); |
| 242 | static void stop_command_port(struct usb_serial *serial); |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 243 | static void command_port_write_callback(struct urb *urb); |
| 244 | static void command_port_read_callback(struct urb *urb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | |
| 246 | static int start_port_read(struct usb_serial_port *port); |
| 247 | static struct whiteheat_urb_wrap *urb_to_wrap(struct urb *urb, struct list_head *head); |
| 248 | static struct list_head *list_first(struct list_head *head); |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 249 | static void rx_data_softint(struct work_struct *work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | |
| 251 | static int firm_send_command(struct usb_serial_port *port, __u8 command, __u8 *data, __u8 datasize); |
| 252 | static int firm_open(struct usb_serial_port *port); |
| 253 | static int firm_close(struct usb_serial_port *port); |
| 254 | static int firm_setup_port(struct usb_serial_port *port); |
| 255 | static int firm_set_rts(struct usb_serial_port *port, __u8 onoff); |
| 256 | static int firm_set_dtr(struct usb_serial_port *port, __u8 onoff); |
| 257 | static int firm_set_break(struct usb_serial_port *port, __u8 onoff); |
| 258 | static int firm_purge(struct usb_serial_port *port, __u8 rxtx); |
| 259 | static int firm_get_dtr_rts(struct usb_serial_port *port); |
| 260 | static int firm_report_tx_done(struct usb_serial_port *port); |
| 261 | |
| 262 | |
| 263 | #define COMMAND_PORT 4 |
| 264 | #define COMMAND_TIMEOUT (2*HZ) /* 2 second timeout for a command */ |
| 265 | #define COMMAND_TIMEOUT_MS 2000 |
| 266 | #define CLOSING_DELAY (30 * HZ) |
| 267 | |
| 268 | |
| 269 | /***************************************************************************** |
| 270 | * Connect Tech's White Heat prerenumeration driver functions |
| 271 | *****************************************************************************/ |
| 272 | |
| 273 | /* steps to download the firmware to the WhiteHEAT device: |
| 274 | - hold the reset (by writing to the reset bit of the CPUCS register) |
| 275 | - download the VEND_AX.HEX file to the chip using VENDOR_REQUEST-ANCHOR_LOAD |
| 276 | - release the reset (by writing to the CPUCS register) |
| 277 | - download the WH.HEX file for all addresses greater than 0x1b3f using |
| 278 | VENDOR_REQUEST-ANCHOR_EXTERNAL_RAM_LOAD |
| 279 | - hold the reset |
| 280 | - download the WH.HEX file for all addresses less than 0x1b40 using |
| 281 | VENDOR_REQUEST_ANCHOR_LOAD |
| 282 | - release the reset |
| 283 | - device renumerated itself and comes up as new device id with all |
| 284 | firmware download completed. |
| 285 | */ |
| 286 | static int whiteheat_firmware_download (struct usb_serial *serial, const struct usb_device_id *id) |
| 287 | { |
| 288 | int response; |
| 289 | const struct whiteheat_hex_record *record; |
| 290 | |
| 291 | dbg("%s", __FUNCTION__); |
| 292 | |
| 293 | response = ezusb_set_reset (serial, 1); |
| 294 | |
| 295 | record = &whiteheat_loader[0]; |
| 296 | while (record->address != 0xffff) { |
| 297 | response = ezusb_writememory (serial, record->address, |
| 298 | (unsigned char *)record->data, record->data_size, 0xa0); |
| 299 | if (response < 0) { |
| 300 | err("%s - ezusb_writememory failed for loader (%d %04X %p %d)", |
| 301 | __FUNCTION__, response, record->address, record->data, record->data_size); |
| 302 | break; |
| 303 | } |
| 304 | ++record; |
| 305 | } |
| 306 | |
| 307 | response = ezusb_set_reset (serial, 0); |
| 308 | |
| 309 | record = &whiteheat_firmware[0]; |
| 310 | while (record->address < 0x1b40) { |
| 311 | ++record; |
| 312 | } |
| 313 | while (record->address != 0xffff) { |
| 314 | response = ezusb_writememory (serial, record->address, |
| 315 | (unsigned char *)record->data, record->data_size, 0xa3); |
| 316 | if (response < 0) { |
| 317 | err("%s - ezusb_writememory failed for first firmware step (%d %04X %p %d)", |
| 318 | __FUNCTION__, response, record->address, record->data, record->data_size); |
| 319 | break; |
| 320 | } |
| 321 | ++record; |
| 322 | } |
| 323 | |
| 324 | response = ezusb_set_reset (serial, 1); |
| 325 | |
| 326 | record = &whiteheat_firmware[0]; |
| 327 | while (record->address < 0x1b40) { |
| 328 | response = ezusb_writememory (serial, record->address, |
| 329 | (unsigned char *)record->data, record->data_size, 0xa0); |
| 330 | if (response < 0) { |
| 331 | err("%s - ezusb_writememory failed for second firmware step (%d %04X %p %d)", |
| 332 | __FUNCTION__, response, record->address, record->data, record->data_size); |
| 333 | break; |
| 334 | } |
| 335 | ++record; |
| 336 | } |
| 337 | |
| 338 | response = ezusb_set_reset (serial, 0); |
| 339 | |
| 340 | return 0; |
| 341 | } |
| 342 | |
| 343 | |
| 344 | static int whiteheat_firmware_attach (struct usb_serial *serial) |
| 345 | { |
| 346 | /* We want this device to fail to have a driver assigned to it */ |
| 347 | return 1; |
| 348 | } |
| 349 | |
| 350 | |
| 351 | /***************************************************************************** |
| 352 | * Connect Tech's White Heat serial driver functions |
| 353 | *****************************************************************************/ |
| 354 | static int whiteheat_attach (struct usb_serial *serial) |
| 355 | { |
| 356 | struct usb_serial_port *command_port; |
| 357 | struct whiteheat_command_private *command_info; |
| 358 | struct usb_serial_port *port; |
| 359 | struct whiteheat_private *info; |
| 360 | struct whiteheat_hw_info *hw_info; |
| 361 | int pipe; |
| 362 | int ret; |
| 363 | int alen; |
| 364 | __u8 *command; |
| 365 | __u8 *result; |
| 366 | int i; |
| 367 | int j; |
| 368 | struct urb *urb; |
| 369 | int buf_size; |
| 370 | struct whiteheat_urb_wrap *wrap; |
| 371 | struct list_head *tmp; |
| 372 | |
| 373 | command_port = serial->port[COMMAND_PORT]; |
| 374 | |
| 375 | pipe = usb_sndbulkpipe (serial->dev, command_port->bulk_out_endpointAddress); |
| 376 | command = kmalloc(2, GFP_KERNEL); |
| 377 | if (!command) |
| 378 | goto no_command_buffer; |
| 379 | command[0] = WHITEHEAT_GET_HW_INFO; |
| 380 | command[1] = 0; |
| 381 | |
| 382 | result = kmalloc(sizeof(*hw_info) + 1, GFP_KERNEL); |
| 383 | if (!result) |
| 384 | goto no_result_buffer; |
| 385 | /* |
| 386 | * When the module is reloaded the firmware is still there and |
| 387 | * the endpoints are still in the usb core unchanged. This is the |
| 388 | * unlinking bug in disguise. Same for the call below. |
| 389 | */ |
| 390 | usb_clear_halt(serial->dev, pipe); |
| 391 | ret = usb_bulk_msg (serial->dev, pipe, command, 2, &alen, COMMAND_TIMEOUT_MS); |
| 392 | if (ret) { |
Greg Kroah-Hartman | 269bda1 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 393 | err("%s: Couldn't send command [%d]", serial->type->description, ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | goto no_firmware; |
Stuart MacDonald | 09fd6bc | 2006-05-31 13:28:40 -0400 | [diff] [blame] | 395 | } else if (alen != 2) { |
Greg Kroah-Hartman | 269bda1 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 396 | err("%s: Send command incomplete [%d]", serial->type->description, alen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | goto no_firmware; |
| 398 | } |
| 399 | |
| 400 | pipe = usb_rcvbulkpipe (serial->dev, command_port->bulk_in_endpointAddress); |
| 401 | /* See the comment on the usb_clear_halt() above */ |
| 402 | usb_clear_halt(serial->dev, pipe); |
| 403 | ret = usb_bulk_msg (serial->dev, pipe, result, sizeof(*hw_info) + 1, &alen, COMMAND_TIMEOUT_MS); |
| 404 | if (ret) { |
Greg Kroah-Hartman | 269bda1 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 405 | err("%s: Couldn't get results [%d]", serial->type->description, ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | goto no_firmware; |
Stuart MacDonald | 09fd6bc | 2006-05-31 13:28:40 -0400 | [diff] [blame] | 407 | } else if (alen != sizeof(*hw_info) + 1) { |
Greg Kroah-Hartman | 269bda1 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 408 | err("%s: Get results incomplete [%d]", serial->type->description, alen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | goto no_firmware; |
| 410 | } else if (result[0] != command[0]) { |
Greg Kroah-Hartman | 269bda1 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 411 | err("%s: Command failed [%d]", serial->type->description, result[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | goto no_firmware; |
| 413 | } |
| 414 | |
| 415 | hw_info = (struct whiteheat_hw_info *)&result[1]; |
| 416 | |
Greg Kroah-Hartman | 269bda1 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 417 | info("%s: Driver %s: Firmware v%d.%02d", serial->type->description, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | DRIVER_VERSION, hw_info->sw_major_rev, hw_info->sw_minor_rev); |
| 419 | |
| 420 | for (i = 0; i < serial->num_ports; i++) { |
| 421 | port = serial->port[i]; |
| 422 | |
Robert P. J. Day | 5cbded5 | 2006-12-13 00:35:56 -0800 | [diff] [blame] | 423 | info = kmalloc(sizeof(struct whiteheat_private), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | if (info == NULL) { |
Greg Kroah-Hartman | 269bda1 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 425 | err("%s: Out of memory for port structures\n", serial->type->description); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | goto no_private; |
| 427 | } |
| 428 | |
| 429 | spin_lock_init(&info->lock); |
Oliver Neukum | 08a2b3b | 2007-05-24 13:52:51 +0200 | [diff] [blame] | 430 | mutex_init(&info->deathwarrant); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | info->flags = 0; |
| 432 | info->mcr = 0; |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 433 | INIT_WORK(&info->rx_work, rx_data_softint); |
| 434 | info->port = port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | |
| 436 | INIT_LIST_HEAD(&info->rx_urbs_free); |
| 437 | INIT_LIST_HEAD(&info->rx_urbs_submitted); |
| 438 | INIT_LIST_HEAD(&info->rx_urb_q); |
| 439 | INIT_LIST_HEAD(&info->tx_urbs_free); |
| 440 | INIT_LIST_HEAD(&info->tx_urbs_submitted); |
| 441 | |
| 442 | for (j = 0; j < urb_pool_size; j++) { |
| 443 | urb = usb_alloc_urb(0, GFP_KERNEL); |
| 444 | if (!urb) { |
| 445 | err("No free urbs available"); |
| 446 | goto no_rx_urb; |
| 447 | } |
| 448 | buf_size = port->read_urb->transfer_buffer_length; |
| 449 | urb->transfer_buffer = kmalloc(buf_size, GFP_KERNEL); |
| 450 | if (!urb->transfer_buffer) { |
| 451 | err("Couldn't allocate urb buffer"); |
| 452 | goto no_rx_buf; |
| 453 | } |
| 454 | wrap = kmalloc(sizeof(*wrap), GFP_KERNEL); |
| 455 | if (!wrap) { |
| 456 | err("Couldn't allocate urb wrapper"); |
| 457 | goto no_rx_wrap; |
| 458 | } |
| 459 | usb_fill_bulk_urb(urb, serial->dev, |
| 460 | usb_rcvbulkpipe(serial->dev, |
| 461 | port->bulk_in_endpointAddress), |
| 462 | urb->transfer_buffer, buf_size, |
| 463 | whiteheat_read_callback, port); |
| 464 | wrap->urb = urb; |
| 465 | list_add(&wrap->list, &info->rx_urbs_free); |
| 466 | |
| 467 | urb = usb_alloc_urb(0, GFP_KERNEL); |
| 468 | if (!urb) { |
| 469 | err("No free urbs available"); |
| 470 | goto no_tx_urb; |
| 471 | } |
| 472 | buf_size = port->write_urb->transfer_buffer_length; |
| 473 | urb->transfer_buffer = kmalloc(buf_size, GFP_KERNEL); |
| 474 | if (!urb->transfer_buffer) { |
| 475 | err("Couldn't allocate urb buffer"); |
| 476 | goto no_tx_buf; |
| 477 | } |
| 478 | wrap = kmalloc(sizeof(*wrap), GFP_KERNEL); |
| 479 | if (!wrap) { |
| 480 | err("Couldn't allocate urb wrapper"); |
| 481 | goto no_tx_wrap; |
| 482 | } |
| 483 | usb_fill_bulk_urb(urb, serial->dev, |
| 484 | usb_sndbulkpipe(serial->dev, |
| 485 | port->bulk_out_endpointAddress), |
| 486 | urb->transfer_buffer, buf_size, |
| 487 | whiteheat_write_callback, port); |
| 488 | wrap->urb = urb; |
| 489 | list_add(&wrap->list, &info->tx_urbs_free); |
| 490 | } |
| 491 | |
| 492 | usb_set_serial_port_data(port, info); |
| 493 | } |
| 494 | |
Robert P. J. Day | 5cbded5 | 2006-12-13 00:35:56 -0800 | [diff] [blame] | 495 | command_info = kmalloc(sizeof(struct whiteheat_command_private), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | if (command_info == NULL) { |
Greg Kroah-Hartman | 269bda1 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 497 | err("%s: Out of memory for port structures\n", serial->type->description); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | goto no_command_private; |
| 499 | } |
| 500 | |
Oliver Neukum | 08a2b3b | 2007-05-24 13:52:51 +0200 | [diff] [blame] | 501 | mutex_init(&command_info->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | command_info->port_running = 0; |
| 503 | init_waitqueue_head(&command_info->wait_command); |
| 504 | usb_set_serial_port_data(command_port, command_info); |
| 505 | command_port->write_urb->complete = command_port_write_callback; |
| 506 | command_port->read_urb->complete = command_port_read_callback; |
| 507 | kfree(result); |
| 508 | kfree(command); |
| 509 | |
| 510 | return 0; |
| 511 | |
| 512 | no_firmware: |
| 513 | /* Firmware likely not running */ |
Greg Kroah-Hartman | 269bda1 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 514 | err("%s: Unable to retrieve firmware version, try replugging\n", serial->type->description); |
| 515 | err("%s: If the firmware is not running (status led not blinking)\n", serial->type->description); |
| 516 | err("%s: please contact support@connecttech.com\n", serial->type->description); |
Jesper Juhl | 67ca028 | 2006-04-23 19:59:23 +0200 | [diff] [blame] | 517 | kfree(result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | return -ENODEV; |
| 519 | |
| 520 | no_command_private: |
| 521 | for (i = serial->num_ports - 1; i >= 0; i--) { |
| 522 | port = serial->port[i]; |
| 523 | info = usb_get_serial_port_data(port); |
| 524 | for (j = urb_pool_size - 1; j >= 0; j--) { |
| 525 | tmp = list_first(&info->tx_urbs_free); |
| 526 | list_del(tmp); |
| 527 | wrap = list_entry(tmp, struct whiteheat_urb_wrap, list); |
| 528 | urb = wrap->urb; |
| 529 | kfree(wrap); |
| 530 | no_tx_wrap: |
| 531 | kfree(urb->transfer_buffer); |
| 532 | no_tx_buf: |
| 533 | usb_free_urb(urb); |
| 534 | no_tx_urb: |
| 535 | tmp = list_first(&info->rx_urbs_free); |
| 536 | list_del(tmp); |
| 537 | wrap = list_entry(tmp, struct whiteheat_urb_wrap, list); |
| 538 | urb = wrap->urb; |
| 539 | kfree(wrap); |
| 540 | no_rx_wrap: |
| 541 | kfree(urb->transfer_buffer); |
| 542 | no_rx_buf: |
| 543 | usb_free_urb(urb); |
| 544 | no_rx_urb: |
| 545 | ; |
| 546 | } |
| 547 | kfree(info); |
| 548 | no_private: |
| 549 | ; |
| 550 | } |
| 551 | kfree(result); |
| 552 | no_result_buffer: |
| 553 | kfree(command); |
| 554 | no_command_buffer: |
| 555 | return -ENOMEM; |
| 556 | } |
| 557 | |
| 558 | |
| 559 | static void whiteheat_shutdown (struct usb_serial *serial) |
| 560 | { |
| 561 | struct usb_serial_port *command_port; |
| 562 | struct usb_serial_port *port; |
| 563 | struct whiteheat_private *info; |
| 564 | struct whiteheat_urb_wrap *wrap; |
| 565 | struct urb *urb; |
| 566 | struct list_head *tmp; |
| 567 | struct list_head *tmp2; |
| 568 | int i; |
| 569 | |
| 570 | dbg("%s", __FUNCTION__); |
| 571 | |
| 572 | /* free up our private data for our command port */ |
| 573 | command_port = serial->port[COMMAND_PORT]; |
| 574 | kfree (usb_get_serial_port_data(command_port)); |
| 575 | |
| 576 | for (i = 0; i < serial->num_ports; i++) { |
| 577 | port = serial->port[i]; |
| 578 | info = usb_get_serial_port_data(port); |
| 579 | list_for_each_safe(tmp, tmp2, &info->rx_urbs_free) { |
| 580 | list_del(tmp); |
| 581 | wrap = list_entry(tmp, struct whiteheat_urb_wrap, list); |
| 582 | urb = wrap->urb; |
| 583 | kfree(wrap); |
| 584 | kfree(urb->transfer_buffer); |
| 585 | usb_free_urb(urb); |
| 586 | } |
| 587 | list_for_each_safe(tmp, tmp2, &info->tx_urbs_free) { |
| 588 | list_del(tmp); |
| 589 | wrap = list_entry(tmp, struct whiteheat_urb_wrap, list); |
| 590 | urb = wrap->urb; |
| 591 | kfree(wrap); |
| 592 | kfree(urb->transfer_buffer); |
| 593 | usb_free_urb(urb); |
| 594 | } |
| 595 | kfree(info); |
| 596 | } |
| 597 | |
| 598 | return; |
| 599 | } |
| 600 | |
| 601 | |
| 602 | static int whiteheat_open (struct usb_serial_port *port, struct file *filp) |
| 603 | { |
| 604 | int retval = 0; |
Alan Cox | 606d099 | 2006-12-08 02:38:45 -0800 | [diff] [blame] | 605 | struct ktermios old_term; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | |
| 607 | dbg("%s - port %d", __FUNCTION__, port->number); |
| 608 | |
| 609 | retval = start_command_port(port->serial); |
| 610 | if (retval) |
| 611 | goto exit; |
| 612 | |
| 613 | if (port->tty) |
| 614 | port->tty->low_latency = 1; |
| 615 | |
| 616 | /* send an open port command */ |
| 617 | retval = firm_open(port); |
| 618 | if (retval) { |
| 619 | stop_command_port(port->serial); |
| 620 | goto exit; |
| 621 | } |
| 622 | |
| 623 | retval = firm_purge(port, WHITEHEAT_PURGE_RX | WHITEHEAT_PURGE_TX); |
| 624 | if (retval) { |
| 625 | firm_close(port); |
| 626 | stop_command_port(port->serial); |
| 627 | goto exit; |
| 628 | } |
| 629 | |
| 630 | old_term.c_cflag = ~port->tty->termios->c_cflag; |
| 631 | old_term.c_iflag = ~port->tty->termios->c_iflag; |
| 632 | whiteheat_set_termios(port, &old_term); |
| 633 | |
| 634 | /* Work around HCD bugs */ |
| 635 | usb_clear_halt(port->serial->dev, port->read_urb->pipe); |
| 636 | usb_clear_halt(port->serial->dev, port->write_urb->pipe); |
| 637 | |
| 638 | /* Start reading from the device */ |
| 639 | retval = start_port_read(port); |
| 640 | if (retval) { |
| 641 | err("%s - failed submitting read urb, error %d", __FUNCTION__, retval); |
| 642 | firm_close(port); |
| 643 | stop_command_port(port->serial); |
| 644 | goto exit; |
| 645 | } |
| 646 | |
| 647 | exit: |
| 648 | dbg("%s - exit, retval = %d", __FUNCTION__, retval); |
| 649 | return retval; |
| 650 | } |
| 651 | |
| 652 | |
| 653 | static void whiteheat_close(struct usb_serial_port *port, struct file * filp) |
| 654 | { |
| 655 | struct whiteheat_private *info = usb_get_serial_port_data(port); |
| 656 | struct whiteheat_urb_wrap *wrap; |
| 657 | struct urb *urb; |
| 658 | struct list_head *tmp; |
| 659 | struct list_head *tmp2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | |
| 661 | dbg("%s - port %d", __FUNCTION__, port->number); |
| 662 | |
| 663 | /* filp is NULL when called from usb_serial_disconnect */ |
| 664 | if (filp && (tty_hung_up_p(filp))) { |
| 665 | return; |
| 666 | } |
| 667 | |
| 668 | port->tty->closing = 1; |
| 669 | |
| 670 | /* |
| 671 | * Not currently in use; tty_wait_until_sent() calls |
| 672 | * serial_chars_in_buffer() which deadlocks on the second semaphore |
| 673 | * acquisition. This should be fixed at some point. Greg's been |
| 674 | * notified. |
| 675 | if ((filp->f_flags & (O_NDELAY | O_NONBLOCK)) == 0) { |
| 676 | tty_wait_until_sent(port->tty, CLOSING_DELAY); |
| 677 | } |
| 678 | */ |
| 679 | |
| 680 | if (port->tty->driver->flush_buffer) |
| 681 | port->tty->driver->flush_buffer(port->tty); |
| 682 | tty_ldisc_flush(port->tty); |
| 683 | |
| 684 | firm_report_tx_done(port); |
| 685 | |
| 686 | firm_close(port); |
| 687 | |
Oliver Neukum | 08a2b3b | 2007-05-24 13:52:51 +0200 | [diff] [blame] | 688 | printk(KERN_ERR"Before processing rx_urbs_submitted.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | /* shutdown our bulk reads and writes */ |
Oliver Neukum | 08a2b3b | 2007-05-24 13:52:51 +0200 | [diff] [blame] | 690 | mutex_lock(&info->deathwarrant); |
| 691 | spin_lock_irq(&info->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | list_for_each_safe(tmp, tmp2, &info->rx_urbs_submitted) { |
| 693 | wrap = list_entry(tmp, struct whiteheat_urb_wrap, list); |
| 694 | urb = wrap->urb; |
Oliver Neukum | 08a2b3b | 2007-05-24 13:52:51 +0200 | [diff] [blame] | 695 | list_del(tmp); |
| 696 | spin_unlock_irq(&info->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | usb_kill_urb(urb); |
Oliver Neukum | 08a2b3b | 2007-05-24 13:52:51 +0200 | [diff] [blame] | 698 | spin_lock_irq(&info->lock); |
| 699 | list_add(tmp, &info->rx_urbs_free); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | } |
Akinobu Mita | 179e091 | 2006-06-26 00:24:41 -0700 | [diff] [blame] | 701 | list_for_each_safe(tmp, tmp2, &info->rx_urb_q) |
| 702 | list_move(tmp, &info->rx_urbs_free); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | list_for_each_safe(tmp, tmp2, &info->tx_urbs_submitted) { |
| 704 | wrap = list_entry(tmp, struct whiteheat_urb_wrap, list); |
| 705 | urb = wrap->urb; |
Oliver Neukum | 08a2b3b | 2007-05-24 13:52:51 +0200 | [diff] [blame] | 706 | list_del(tmp); |
| 707 | spin_unlock_irq(&info->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | usb_kill_urb(urb); |
Oliver Neukum | 08a2b3b | 2007-05-24 13:52:51 +0200 | [diff] [blame] | 709 | spin_lock_irq(&info->lock); |
| 710 | list_add(tmp, &info->tx_urbs_free); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | } |
Oliver Neukum | 08a2b3b | 2007-05-24 13:52:51 +0200 | [diff] [blame] | 712 | spin_unlock_irq(&info->lock); |
| 713 | mutex_unlock(&info->deathwarrant); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | |
| 715 | stop_command_port(port->serial); |
| 716 | |
| 717 | port->tty->closing = 0; |
| 718 | } |
| 719 | |
| 720 | |
| 721 | static int whiteheat_write(struct usb_serial_port *port, const unsigned char *buf, int count) |
| 722 | { |
| 723 | struct usb_serial *serial = port->serial; |
| 724 | struct whiteheat_private *info = usb_get_serial_port_data(port); |
| 725 | struct whiteheat_urb_wrap *wrap; |
| 726 | struct urb *urb; |
| 727 | int result; |
| 728 | int bytes; |
| 729 | int sent = 0; |
| 730 | unsigned long flags; |
| 731 | struct list_head *tmp; |
| 732 | |
| 733 | dbg("%s - port %d", __FUNCTION__, port->number); |
| 734 | |
| 735 | if (count == 0) { |
| 736 | dbg("%s - write request of 0 bytes", __FUNCTION__); |
| 737 | return (0); |
| 738 | } |
| 739 | |
| 740 | while (count) { |
| 741 | spin_lock_irqsave(&info->lock, flags); |
| 742 | if (list_empty(&info->tx_urbs_free)) { |
| 743 | spin_unlock_irqrestore(&info->lock, flags); |
| 744 | break; |
| 745 | } |
| 746 | tmp = list_first(&info->tx_urbs_free); |
| 747 | list_del(tmp); |
| 748 | spin_unlock_irqrestore(&info->lock, flags); |
| 749 | |
| 750 | wrap = list_entry(tmp, struct whiteheat_urb_wrap, list); |
| 751 | urb = wrap->urb; |
| 752 | bytes = (count > port->bulk_out_size) ? port->bulk_out_size : count; |
| 753 | memcpy (urb->transfer_buffer, buf + sent, bytes); |
| 754 | |
| 755 | usb_serial_debug_data(debug, &port->dev, __FUNCTION__, bytes, urb->transfer_buffer); |
| 756 | |
| 757 | urb->dev = serial->dev; |
| 758 | urb->transfer_buffer_length = bytes; |
| 759 | result = usb_submit_urb(urb, GFP_ATOMIC); |
| 760 | if (result) { |
| 761 | err("%s - failed submitting write urb, error %d", __FUNCTION__, result); |
| 762 | sent = result; |
| 763 | spin_lock_irqsave(&info->lock, flags); |
| 764 | list_add(tmp, &info->tx_urbs_free); |
| 765 | spin_unlock_irqrestore(&info->lock, flags); |
| 766 | break; |
| 767 | } else { |
| 768 | sent += bytes; |
| 769 | count -= bytes; |
| 770 | spin_lock_irqsave(&info->lock, flags); |
| 771 | list_add(tmp, &info->tx_urbs_submitted); |
| 772 | spin_unlock_irqrestore(&info->lock, flags); |
| 773 | } |
| 774 | } |
| 775 | |
| 776 | return sent; |
| 777 | } |
| 778 | |
| 779 | |
| 780 | static int whiteheat_write_room(struct usb_serial_port *port) |
| 781 | { |
| 782 | struct whiteheat_private *info = usb_get_serial_port_data(port); |
| 783 | struct list_head *tmp; |
| 784 | int room = 0; |
| 785 | unsigned long flags; |
| 786 | |
| 787 | dbg("%s - port %d", __FUNCTION__, port->number); |
| 788 | |
| 789 | spin_lock_irqsave(&info->lock, flags); |
| 790 | list_for_each(tmp, &info->tx_urbs_free) |
| 791 | room++; |
| 792 | spin_unlock_irqrestore(&info->lock, flags); |
| 793 | room *= port->bulk_out_size; |
| 794 | |
| 795 | dbg("%s - returns %d", __FUNCTION__, room); |
| 796 | return (room); |
| 797 | } |
| 798 | |
| 799 | |
| 800 | static int whiteheat_tiocmget (struct usb_serial_port *port, struct file *file) |
| 801 | { |
| 802 | struct whiteheat_private *info = usb_get_serial_port_data(port); |
| 803 | unsigned int modem_signals = 0; |
| 804 | |
| 805 | dbg("%s - port %d", __FUNCTION__, port->number); |
| 806 | |
| 807 | firm_get_dtr_rts(port); |
| 808 | if (info->mcr & UART_MCR_DTR) |
| 809 | modem_signals |= TIOCM_DTR; |
| 810 | if (info->mcr & UART_MCR_RTS) |
| 811 | modem_signals |= TIOCM_RTS; |
| 812 | |
| 813 | return modem_signals; |
| 814 | } |
| 815 | |
| 816 | |
| 817 | static int whiteheat_tiocmset (struct usb_serial_port *port, struct file *file, |
| 818 | unsigned int set, unsigned int clear) |
| 819 | { |
| 820 | struct whiteheat_private *info = usb_get_serial_port_data(port); |
| 821 | |
| 822 | dbg("%s - port %d", __FUNCTION__, port->number); |
| 823 | |
| 824 | if (set & TIOCM_RTS) |
| 825 | info->mcr |= UART_MCR_RTS; |
| 826 | if (set & TIOCM_DTR) |
| 827 | info->mcr |= UART_MCR_DTR; |
| 828 | |
| 829 | if (clear & TIOCM_RTS) |
| 830 | info->mcr &= ~UART_MCR_RTS; |
| 831 | if (clear & TIOCM_DTR) |
| 832 | info->mcr &= ~UART_MCR_DTR; |
| 833 | |
| 834 | firm_set_dtr(port, info->mcr & UART_MCR_DTR); |
| 835 | firm_set_rts(port, info->mcr & UART_MCR_RTS); |
| 836 | return 0; |
| 837 | } |
| 838 | |
| 839 | |
| 840 | static int whiteheat_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg) |
| 841 | { |
| 842 | struct serial_struct serstruct; |
| 843 | void __user *user_arg = (void __user *)arg; |
| 844 | |
| 845 | dbg("%s - port %d, cmd 0x%.4x", __FUNCTION__, port->number, cmd); |
| 846 | |
| 847 | switch (cmd) { |
| 848 | case TIOCGSERIAL: |
| 849 | memset(&serstruct, 0, sizeof(serstruct)); |
| 850 | serstruct.type = PORT_16654; |
| 851 | serstruct.line = port->serial->minor; |
| 852 | serstruct.port = port->number; |
| 853 | serstruct.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ; |
| 854 | serstruct.xmit_fifo_size = port->bulk_out_size; |
| 855 | serstruct.custom_divisor = 0; |
| 856 | serstruct.baud_base = 460800; |
| 857 | serstruct.close_delay = CLOSING_DELAY; |
| 858 | serstruct.closing_wait = CLOSING_DELAY; |
| 859 | |
| 860 | if (copy_to_user(user_arg, &serstruct, sizeof(serstruct))) |
| 861 | return -EFAULT; |
| 862 | |
| 863 | break; |
| 864 | |
| 865 | case TIOCSSERIAL: |
| 866 | if (copy_from_user(&serstruct, user_arg, sizeof(serstruct))) |
| 867 | return -EFAULT; |
| 868 | |
| 869 | /* |
| 870 | * For now this is ignored. dip sets the ASYNC_[V]HI flags |
| 871 | * but this isn't used by us at all. Maybe someone somewhere |
| 872 | * will need the custom_divisor setting. |
| 873 | */ |
| 874 | |
| 875 | break; |
| 876 | |
| 877 | default: |
| 878 | break; |
| 879 | } |
| 880 | |
| 881 | return -ENOIOCTLCMD; |
| 882 | } |
| 883 | |
| 884 | |
Oliver Neukum | 08a2b3b | 2007-05-24 13:52:51 +0200 | [diff] [blame] | 885 | static void whiteheat_set_termios(struct usb_serial_port *port, struct ktermios *old_termios) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | { |
| 887 | dbg("%s -port %d", __FUNCTION__, port->number); |
| 888 | |
| 889 | if ((!port->tty) || (!port->tty->termios)) { |
| 890 | dbg("%s - no tty structures", __FUNCTION__); |
| 891 | goto exit; |
| 892 | } |
| 893 | |
| 894 | /* check that they really want us to change something */ |
| 895 | if (old_termios) { |
| 896 | if ((port->tty->termios->c_cflag == old_termios->c_cflag) && |
| 897 | (port->tty->termios->c_iflag == old_termios->c_iflag)) { |
| 898 | dbg("%s - nothing to change...", __FUNCTION__); |
| 899 | goto exit; |
| 900 | } |
| 901 | } |
| 902 | |
| 903 | firm_setup_port(port); |
| 904 | |
| 905 | exit: |
| 906 | return; |
| 907 | } |
| 908 | |
| 909 | |
| 910 | static void whiteheat_break_ctl(struct usb_serial_port *port, int break_state) { |
| 911 | firm_set_break(port, break_state); |
| 912 | } |
| 913 | |
| 914 | |
| 915 | static int whiteheat_chars_in_buffer(struct usb_serial_port *port) |
| 916 | { |
| 917 | struct whiteheat_private *info = usb_get_serial_port_data(port); |
| 918 | struct list_head *tmp; |
| 919 | struct whiteheat_urb_wrap *wrap; |
| 920 | int chars = 0; |
| 921 | unsigned long flags; |
| 922 | |
| 923 | dbg("%s - port %d", __FUNCTION__, port->number); |
| 924 | |
| 925 | spin_lock_irqsave(&info->lock, flags); |
| 926 | list_for_each(tmp, &info->tx_urbs_submitted) { |
| 927 | wrap = list_entry(tmp, struct whiteheat_urb_wrap, list); |
| 928 | chars += wrap->urb->transfer_buffer_length; |
| 929 | } |
| 930 | spin_unlock_irqrestore(&info->lock, flags); |
| 931 | |
| 932 | dbg ("%s - returns %d", __FUNCTION__, chars); |
Oliver Neukum | 08a2b3b | 2007-05-24 13:52:51 +0200 | [diff] [blame] | 933 | return chars; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 934 | } |
| 935 | |
| 936 | |
| 937 | static void whiteheat_throttle (struct usb_serial_port *port) |
| 938 | { |
| 939 | struct whiteheat_private *info = usb_get_serial_port_data(port); |
| 940 | unsigned long flags; |
| 941 | |
| 942 | dbg("%s - port %d", __FUNCTION__, port->number); |
| 943 | |
| 944 | spin_lock_irqsave(&info->lock, flags); |
| 945 | info->flags |= THROTTLED; |
| 946 | spin_unlock_irqrestore(&info->lock, flags); |
| 947 | |
| 948 | return; |
| 949 | } |
| 950 | |
| 951 | |
| 952 | static void whiteheat_unthrottle (struct usb_serial_port *port) |
| 953 | { |
| 954 | struct whiteheat_private *info = usb_get_serial_port_data(port); |
| 955 | int actually_throttled; |
| 956 | unsigned long flags; |
| 957 | |
| 958 | dbg("%s - port %d", __FUNCTION__, port->number); |
| 959 | |
| 960 | spin_lock_irqsave(&info->lock, flags); |
| 961 | actually_throttled = info->flags & ACTUALLY_THROTTLED; |
| 962 | info->flags &= ~(THROTTLED | ACTUALLY_THROTTLED); |
| 963 | spin_unlock_irqrestore(&info->lock, flags); |
| 964 | |
| 965 | if (actually_throttled) |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 966 | rx_data_softint(&info->rx_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | |
| 968 | return; |
| 969 | } |
| 970 | |
| 971 | |
| 972 | /***************************************************************************** |
| 973 | * Connect Tech's White Heat callback routines |
| 974 | *****************************************************************************/ |
Oliver Neukum | 08a2b3b | 2007-05-24 13:52:51 +0200 | [diff] [blame] | 975 | static void command_port_write_callback(struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 976 | { |
Greg Kroah-Hartman | 0540001 | 2007-06-15 15:44:13 -0700 | [diff] [blame^] | 977 | int status = urb->status; |
| 978 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | dbg("%s", __FUNCTION__); |
| 980 | |
Greg Kroah-Hartman | 0540001 | 2007-06-15 15:44:13 -0700 | [diff] [blame^] | 981 | if (status) { |
| 982 | dbg("nonzero urb status: %d", status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | return; |
| 984 | } |
| 985 | } |
| 986 | |
| 987 | |
Oliver Neukum | 08a2b3b | 2007-05-24 13:52:51 +0200 | [diff] [blame] | 988 | static void command_port_read_callback(struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 989 | { |
| 990 | struct usb_serial_port *command_port = (struct usb_serial_port *)urb->context; |
| 991 | struct whiteheat_command_private *command_info; |
Greg Kroah-Hartman | 0540001 | 2007-06-15 15:44:13 -0700 | [diff] [blame^] | 992 | int status = urb->status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | unsigned char *data = urb->transfer_buffer; |
| 994 | int result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | |
| 996 | dbg("%s", __FUNCTION__); |
| 997 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | command_info = usb_get_serial_port_data(command_port); |
| 999 | if (!command_info) { |
| 1000 | dbg ("%s - command_info is NULL, exiting.", __FUNCTION__); |
| 1001 | return; |
| 1002 | } |
Greg Kroah-Hartman | 0540001 | 2007-06-15 15:44:13 -0700 | [diff] [blame^] | 1003 | if (status) { |
| 1004 | dbg("%s - nonzero urb status: %d", __FUNCTION__, status); |
| 1005 | if (status != -ENOENT) |
Oliver Neukum | 08a2b3b | 2007-05-24 13:52:51 +0200 | [diff] [blame] | 1006 | command_info->command_finished = WHITEHEAT_CMD_FAILURE; |
| 1007 | wake_up(&command_info->wait_command); |
| 1008 | return; |
| 1009 | } |
| 1010 | |
| 1011 | usb_serial_debug_data(debug, &command_port->dev, __FUNCTION__, urb->actual_length, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1012 | |
| 1013 | if (data[0] == WHITEHEAT_CMD_COMPLETE) { |
| 1014 | command_info->command_finished = WHITEHEAT_CMD_COMPLETE; |
Oliver Neukum | 08a2b3b | 2007-05-24 13:52:51 +0200 | [diff] [blame] | 1015 | wake_up(&command_info->wait_command); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1016 | } else if (data[0] == WHITEHEAT_CMD_FAILURE) { |
| 1017 | command_info->command_finished = WHITEHEAT_CMD_FAILURE; |
Oliver Neukum | 08a2b3b | 2007-05-24 13:52:51 +0200 | [diff] [blame] | 1018 | wake_up(&command_info->wait_command); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1019 | } else if (data[0] == WHITEHEAT_EVENT) { |
| 1020 | /* These are unsolicited reports from the firmware, hence no waiting command to wakeup */ |
| 1021 | dbg("%s - event received", __FUNCTION__); |
| 1022 | } else if (data[0] == WHITEHEAT_GET_DTR_RTS) { |
| 1023 | memcpy(command_info->result_buffer, &data[1], urb->actual_length - 1); |
| 1024 | command_info->command_finished = WHITEHEAT_CMD_COMPLETE; |
Oliver Neukum | 08a2b3b | 2007-05-24 13:52:51 +0200 | [diff] [blame] | 1025 | wake_up(&command_info->wait_command); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1026 | } else { |
| 1027 | dbg("%s - bad reply from firmware", __FUNCTION__); |
| 1028 | } |
| 1029 | |
| 1030 | /* Continue trying to always read */ |
| 1031 | command_port->read_urb->dev = command_port->serial->dev; |
| 1032 | result = usb_submit_urb(command_port->read_urb, GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1033 | if (result) |
| 1034 | dbg("%s - failed resubmitting read urb, error %d", __FUNCTION__, result); |
| 1035 | } |
| 1036 | |
| 1037 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 1038 | static void whiteheat_read_callback(struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1039 | { |
| 1040 | struct usb_serial_port *port = (struct usb_serial_port *)urb->context; |
| 1041 | struct whiteheat_urb_wrap *wrap; |
| 1042 | unsigned char *data = urb->transfer_buffer; |
| 1043 | struct whiteheat_private *info = usb_get_serial_port_data(port); |
Greg Kroah-Hartman | 0540001 | 2007-06-15 15:44:13 -0700 | [diff] [blame^] | 1044 | int status = urb->status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1045 | |
| 1046 | dbg("%s - port %d", __FUNCTION__, port->number); |
| 1047 | |
| 1048 | spin_lock(&info->lock); |
| 1049 | wrap = urb_to_wrap(urb, &info->rx_urbs_submitted); |
| 1050 | if (!wrap) { |
| 1051 | spin_unlock(&info->lock); |
| 1052 | err("%s - Not my urb!", __FUNCTION__); |
| 1053 | return; |
| 1054 | } |
| 1055 | list_del(&wrap->list); |
| 1056 | spin_unlock(&info->lock); |
| 1057 | |
Greg Kroah-Hartman | 0540001 | 2007-06-15 15:44:13 -0700 | [diff] [blame^] | 1058 | if (status) { |
| 1059 | dbg("%s - nonzero read bulk status received: %d", |
| 1060 | __FUNCTION__, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1061 | spin_lock(&info->lock); |
| 1062 | list_add(&wrap->list, &info->rx_urbs_free); |
| 1063 | spin_unlock(&info->lock); |
| 1064 | return; |
| 1065 | } |
| 1066 | |
| 1067 | usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data); |
| 1068 | |
| 1069 | spin_lock(&info->lock); |
| 1070 | list_add_tail(&wrap->list, &info->rx_urb_q); |
| 1071 | if (info->flags & THROTTLED) { |
| 1072 | info->flags |= ACTUALLY_THROTTLED; |
| 1073 | spin_unlock(&info->lock); |
| 1074 | return; |
| 1075 | } |
| 1076 | spin_unlock(&info->lock); |
| 1077 | |
| 1078 | schedule_work(&info->rx_work); |
| 1079 | } |
| 1080 | |
| 1081 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 1082 | static void whiteheat_write_callback(struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1083 | { |
| 1084 | struct usb_serial_port *port = (struct usb_serial_port *)urb->context; |
| 1085 | struct whiteheat_private *info = usb_get_serial_port_data(port); |
| 1086 | struct whiteheat_urb_wrap *wrap; |
Greg Kroah-Hartman | 0540001 | 2007-06-15 15:44:13 -0700 | [diff] [blame^] | 1087 | int status = urb->status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1088 | |
| 1089 | dbg("%s - port %d", __FUNCTION__, port->number); |
| 1090 | |
| 1091 | spin_lock(&info->lock); |
| 1092 | wrap = urb_to_wrap(urb, &info->tx_urbs_submitted); |
| 1093 | if (!wrap) { |
| 1094 | spin_unlock(&info->lock); |
| 1095 | err("%s - Not my urb!", __FUNCTION__); |
| 1096 | return; |
| 1097 | } |
Akinobu Mita | 179e091 | 2006-06-26 00:24:41 -0700 | [diff] [blame] | 1098 | list_move(&wrap->list, &info->tx_urbs_free); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1099 | spin_unlock(&info->lock); |
| 1100 | |
Greg Kroah-Hartman | 0540001 | 2007-06-15 15:44:13 -0700 | [diff] [blame^] | 1101 | if (status) { |
| 1102 | dbg("%s - nonzero write bulk status received: %d", |
| 1103 | __FUNCTION__, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1104 | return; |
| 1105 | } |
| 1106 | |
Pete Zaitcev | cf2c748 | 2006-05-22 21:58:49 -0700 | [diff] [blame] | 1107 | usb_serial_port_softint(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1108 | } |
| 1109 | |
| 1110 | |
| 1111 | /***************************************************************************** |
| 1112 | * Connect Tech's White Heat firmware interface |
| 1113 | *****************************************************************************/ |
Oliver Neukum | 08a2b3b | 2007-05-24 13:52:51 +0200 | [diff] [blame] | 1114 | static int firm_send_command(struct usb_serial_port *port, __u8 command, __u8 *data, __u8 datasize) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1115 | { |
| 1116 | struct usb_serial_port *command_port; |
| 1117 | struct whiteheat_command_private *command_info; |
| 1118 | struct whiteheat_private *info; |
| 1119 | __u8 *transfer_buffer; |
| 1120 | int retval = 0; |
Oliver Neukum | 08a2b3b | 2007-05-24 13:52:51 +0200 | [diff] [blame] | 1121 | int t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1122 | |
| 1123 | dbg("%s - command %d", __FUNCTION__, command); |
| 1124 | |
| 1125 | command_port = port->serial->port[COMMAND_PORT]; |
| 1126 | command_info = usb_get_serial_port_data(command_port); |
Oliver Neukum | 08a2b3b | 2007-05-24 13:52:51 +0200 | [diff] [blame] | 1127 | mutex_lock(&command_info->mutex); |
Richard Knutsson | 38c3cb5 | 2007-03-26 22:00:28 -0800 | [diff] [blame] | 1128 | command_info->command_finished = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1129 | |
| 1130 | transfer_buffer = (__u8 *)command_port->write_urb->transfer_buffer; |
| 1131 | transfer_buffer[0] = command; |
| 1132 | memcpy (&transfer_buffer[1], data, datasize); |
| 1133 | command_port->write_urb->transfer_buffer_length = datasize + 1; |
| 1134 | command_port->write_urb->dev = port->serial->dev; |
Oliver Neukum | 08a2b3b | 2007-05-24 13:52:51 +0200 | [diff] [blame] | 1135 | retval = usb_submit_urb (command_port->write_urb, GFP_NOIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1136 | if (retval) { |
| 1137 | dbg("%s - submit urb failed", __FUNCTION__); |
| 1138 | goto exit; |
| 1139 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1140 | |
| 1141 | /* wait for the command to complete */ |
Oliver Neukum | 08a2b3b | 2007-05-24 13:52:51 +0200 | [diff] [blame] | 1142 | t = wait_event_timeout(command_info->wait_command, |
Richard Knutsson | 38c3cb5 | 2007-03-26 22:00:28 -0800 | [diff] [blame] | 1143 | (bool)command_info->command_finished, COMMAND_TIMEOUT); |
Oliver Neukum | 08a2b3b | 2007-05-24 13:52:51 +0200 | [diff] [blame] | 1144 | if (!t) |
| 1145 | usb_kill_urb(command_port->write_urb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1146 | |
Richard Knutsson | 38c3cb5 | 2007-03-26 22:00:28 -0800 | [diff] [blame] | 1147 | if (command_info->command_finished == false) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1148 | dbg("%s - command timed out.", __FUNCTION__); |
| 1149 | retval = -ETIMEDOUT; |
| 1150 | goto exit; |
| 1151 | } |
| 1152 | |
| 1153 | if (command_info->command_finished == WHITEHEAT_CMD_FAILURE) { |
| 1154 | dbg("%s - command failed.", __FUNCTION__); |
| 1155 | retval = -EIO; |
| 1156 | goto exit; |
| 1157 | } |
| 1158 | |
| 1159 | if (command_info->command_finished == WHITEHEAT_CMD_COMPLETE) { |
| 1160 | dbg("%s - command completed.", __FUNCTION__); |
| 1161 | switch (command) { |
| 1162 | case WHITEHEAT_GET_DTR_RTS: |
| 1163 | info = usb_get_serial_port_data(port); |
| 1164 | memcpy(&info->mcr, command_info->result_buffer, sizeof(struct whiteheat_dr_info)); |
| 1165 | break; |
| 1166 | } |
| 1167 | } |
| 1168 | |
| 1169 | exit: |
Oliver Neukum | 08a2b3b | 2007-05-24 13:52:51 +0200 | [diff] [blame] | 1170 | mutex_unlock(&command_info->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1171 | return retval; |
| 1172 | } |
| 1173 | |
| 1174 | |
| 1175 | static int firm_open(struct usb_serial_port *port) { |
| 1176 | struct whiteheat_simple open_command; |
| 1177 | |
| 1178 | open_command.port = port->number - port->serial->minor + 1; |
| 1179 | return firm_send_command(port, WHITEHEAT_OPEN, (__u8 *)&open_command, sizeof(open_command)); |
| 1180 | } |
| 1181 | |
| 1182 | |
| 1183 | static int firm_close(struct usb_serial_port *port) { |
| 1184 | struct whiteheat_simple close_command; |
| 1185 | |
| 1186 | close_command.port = port->number - port->serial->minor + 1; |
| 1187 | return firm_send_command(port, WHITEHEAT_CLOSE, (__u8 *)&close_command, sizeof(close_command)); |
| 1188 | } |
| 1189 | |
| 1190 | |
| 1191 | static int firm_setup_port(struct usb_serial_port *port) { |
| 1192 | struct whiteheat_port_settings port_settings; |
| 1193 | unsigned int cflag = port->tty->termios->c_cflag; |
| 1194 | |
| 1195 | port_settings.port = port->number + 1; |
| 1196 | |
| 1197 | /* get the byte size */ |
| 1198 | switch (cflag & CSIZE) { |
| 1199 | case CS5: port_settings.bits = 5; break; |
| 1200 | case CS6: port_settings.bits = 6; break; |
| 1201 | case CS7: port_settings.bits = 7; break; |
| 1202 | default: |
| 1203 | case CS8: port_settings.bits = 8; break; |
| 1204 | } |
| 1205 | dbg("%s - data bits = %d", __FUNCTION__, port_settings.bits); |
| 1206 | |
| 1207 | /* determine the parity */ |
| 1208 | if (cflag & PARENB) |
| 1209 | if (cflag & CMSPAR) |
| 1210 | if (cflag & PARODD) |
| 1211 | port_settings.parity = WHITEHEAT_PAR_MARK; |
| 1212 | else |
| 1213 | port_settings.parity = WHITEHEAT_PAR_SPACE; |
| 1214 | else |
| 1215 | if (cflag & PARODD) |
| 1216 | port_settings.parity = WHITEHEAT_PAR_ODD; |
| 1217 | else |
| 1218 | port_settings.parity = WHITEHEAT_PAR_EVEN; |
| 1219 | else |
| 1220 | port_settings.parity = WHITEHEAT_PAR_NONE; |
| 1221 | dbg("%s - parity = %c", __FUNCTION__, port_settings.parity); |
| 1222 | |
| 1223 | /* figure out the stop bits requested */ |
| 1224 | if (cflag & CSTOPB) |
| 1225 | port_settings.stop = 2; |
| 1226 | else |
| 1227 | port_settings.stop = 1; |
| 1228 | dbg("%s - stop bits = %d", __FUNCTION__, port_settings.stop); |
| 1229 | |
| 1230 | /* figure out the flow control settings */ |
| 1231 | if (cflag & CRTSCTS) |
| 1232 | port_settings.hflow = (WHITEHEAT_HFLOW_CTS | WHITEHEAT_HFLOW_RTS); |
| 1233 | else |
| 1234 | port_settings.hflow = WHITEHEAT_HFLOW_NONE; |
| 1235 | dbg("%s - hardware flow control = %s %s %s %s", __FUNCTION__, |
| 1236 | (port_settings.hflow & WHITEHEAT_HFLOW_CTS) ? "CTS" : "", |
| 1237 | (port_settings.hflow & WHITEHEAT_HFLOW_RTS) ? "RTS" : "", |
| 1238 | (port_settings.hflow & WHITEHEAT_HFLOW_DSR) ? "DSR" : "", |
| 1239 | (port_settings.hflow & WHITEHEAT_HFLOW_DTR) ? "DTR" : ""); |
| 1240 | |
| 1241 | /* determine software flow control */ |
| 1242 | if (I_IXOFF(port->tty)) |
| 1243 | port_settings.sflow = WHITEHEAT_SFLOW_RXTX; |
| 1244 | else |
| 1245 | port_settings.sflow = WHITEHEAT_SFLOW_NONE; |
| 1246 | dbg("%s - software flow control = %c", __FUNCTION__, port_settings.sflow); |
| 1247 | |
| 1248 | port_settings.xon = START_CHAR(port->tty); |
| 1249 | port_settings.xoff = STOP_CHAR(port->tty); |
| 1250 | dbg("%s - XON = %2x, XOFF = %2x", __FUNCTION__, port_settings.xon, port_settings.xoff); |
| 1251 | |
| 1252 | /* get the baud rate wanted */ |
| 1253 | port_settings.baud = tty_get_baud_rate(port->tty); |
| 1254 | dbg("%s - baud rate = %d", __FUNCTION__, port_settings.baud); |
| 1255 | |
| 1256 | /* handle any settings that aren't specified in the tty structure */ |
| 1257 | port_settings.lloop = 0; |
| 1258 | |
| 1259 | /* now send the message to the device */ |
| 1260 | return firm_send_command(port, WHITEHEAT_SETUP_PORT, (__u8 *)&port_settings, sizeof(port_settings)); |
| 1261 | } |
| 1262 | |
| 1263 | |
| 1264 | static int firm_set_rts(struct usb_serial_port *port, __u8 onoff) { |
| 1265 | struct whiteheat_set_rdb rts_command; |
| 1266 | |
| 1267 | rts_command.port = port->number - port->serial->minor + 1; |
| 1268 | rts_command.state = onoff; |
| 1269 | return firm_send_command(port, WHITEHEAT_SET_RTS, (__u8 *)&rts_command, sizeof(rts_command)); |
| 1270 | } |
| 1271 | |
| 1272 | |
| 1273 | static int firm_set_dtr(struct usb_serial_port *port, __u8 onoff) { |
| 1274 | struct whiteheat_set_rdb dtr_command; |
| 1275 | |
| 1276 | dtr_command.port = port->number - port->serial->minor + 1; |
| 1277 | dtr_command.state = onoff; |
| 1278 | return firm_send_command(port, WHITEHEAT_SET_RTS, (__u8 *)&dtr_command, sizeof(dtr_command)); |
| 1279 | } |
| 1280 | |
| 1281 | |
| 1282 | static int firm_set_break(struct usb_serial_port *port, __u8 onoff) { |
| 1283 | struct whiteheat_set_rdb break_command; |
| 1284 | |
| 1285 | break_command.port = port->number - port->serial->minor + 1; |
| 1286 | break_command.state = onoff; |
| 1287 | return firm_send_command(port, WHITEHEAT_SET_RTS, (__u8 *)&break_command, sizeof(break_command)); |
| 1288 | } |
| 1289 | |
| 1290 | |
| 1291 | static int firm_purge(struct usb_serial_port *port, __u8 rxtx) { |
| 1292 | struct whiteheat_purge purge_command; |
| 1293 | |
| 1294 | purge_command.port = port->number - port->serial->minor + 1; |
| 1295 | purge_command.what = rxtx; |
| 1296 | return firm_send_command(port, WHITEHEAT_PURGE, (__u8 *)&purge_command, sizeof(purge_command)); |
| 1297 | } |
| 1298 | |
| 1299 | |
| 1300 | static int firm_get_dtr_rts(struct usb_serial_port *port) { |
| 1301 | struct whiteheat_simple get_dr_command; |
| 1302 | |
| 1303 | get_dr_command.port = port->number - port->serial->minor + 1; |
| 1304 | return firm_send_command(port, WHITEHEAT_GET_DTR_RTS, (__u8 *)&get_dr_command, sizeof(get_dr_command)); |
| 1305 | } |
| 1306 | |
| 1307 | |
| 1308 | static int firm_report_tx_done(struct usb_serial_port *port) { |
| 1309 | struct whiteheat_simple close_command; |
| 1310 | |
| 1311 | close_command.port = port->number - port->serial->minor + 1; |
| 1312 | return firm_send_command(port, WHITEHEAT_REPORT_TX_DONE, (__u8 *)&close_command, sizeof(close_command)); |
| 1313 | } |
| 1314 | |
| 1315 | |
| 1316 | /***************************************************************************** |
| 1317 | * Connect Tech's White Heat utility functions |
| 1318 | *****************************************************************************/ |
| 1319 | static int start_command_port(struct usb_serial *serial) |
| 1320 | { |
| 1321 | struct usb_serial_port *command_port; |
| 1322 | struct whiteheat_command_private *command_info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1323 | int retval = 0; |
| 1324 | |
| 1325 | command_port = serial->port[COMMAND_PORT]; |
| 1326 | command_info = usb_get_serial_port_data(command_port); |
Oliver Neukum | 08a2b3b | 2007-05-24 13:52:51 +0200 | [diff] [blame] | 1327 | mutex_lock(&command_info->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1328 | if (!command_info->port_running) { |
| 1329 | /* Work around HCD bugs */ |
| 1330 | usb_clear_halt(serial->dev, command_port->read_urb->pipe); |
| 1331 | |
| 1332 | command_port->read_urb->dev = serial->dev; |
| 1333 | retval = usb_submit_urb(command_port->read_urb, GFP_KERNEL); |
| 1334 | if (retval) { |
| 1335 | err("%s - failed submitting read urb, error %d", __FUNCTION__, retval); |
| 1336 | goto exit; |
| 1337 | } |
| 1338 | } |
| 1339 | command_info->port_running++; |
| 1340 | |
| 1341 | exit: |
Oliver Neukum | 08a2b3b | 2007-05-24 13:52:51 +0200 | [diff] [blame] | 1342 | mutex_unlock(&command_info->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1343 | return retval; |
| 1344 | } |
| 1345 | |
| 1346 | |
| 1347 | static void stop_command_port(struct usb_serial *serial) |
| 1348 | { |
| 1349 | struct usb_serial_port *command_port; |
| 1350 | struct whiteheat_command_private *command_info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1351 | |
| 1352 | command_port = serial->port[COMMAND_PORT]; |
| 1353 | command_info = usb_get_serial_port_data(command_port); |
Oliver Neukum | 08a2b3b | 2007-05-24 13:52:51 +0200 | [diff] [blame] | 1354 | mutex_lock(&command_info->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1355 | command_info->port_running--; |
| 1356 | if (!command_info->port_running) |
| 1357 | usb_kill_urb(command_port->read_urb); |
Oliver Neukum | 08a2b3b | 2007-05-24 13:52:51 +0200 | [diff] [blame] | 1358 | mutex_unlock(&command_info->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1359 | } |
| 1360 | |
| 1361 | |
| 1362 | static int start_port_read(struct usb_serial_port *port) |
| 1363 | { |
| 1364 | struct whiteheat_private *info = usb_get_serial_port_data(port); |
| 1365 | struct whiteheat_urb_wrap *wrap; |
| 1366 | struct urb *urb; |
| 1367 | int retval = 0; |
| 1368 | unsigned long flags; |
| 1369 | struct list_head *tmp; |
| 1370 | struct list_head *tmp2; |
| 1371 | |
| 1372 | spin_lock_irqsave(&info->lock, flags); |
| 1373 | |
| 1374 | list_for_each_safe(tmp, tmp2, &info->rx_urbs_free) { |
| 1375 | list_del(tmp); |
| 1376 | wrap = list_entry(tmp, struct whiteheat_urb_wrap, list); |
| 1377 | urb = wrap->urb; |
| 1378 | urb->dev = port->serial->dev; |
Oliver Neukum | 08a2b3b | 2007-05-24 13:52:51 +0200 | [diff] [blame] | 1379 | spin_unlock_irqrestore(&info->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1380 | retval = usb_submit_urb(urb, GFP_KERNEL); |
| 1381 | if (retval) { |
Oliver Neukum | 08a2b3b | 2007-05-24 13:52:51 +0200 | [diff] [blame] | 1382 | spin_lock_irqsave(&info->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1383 | list_add(tmp, &info->rx_urbs_free); |
| 1384 | list_for_each_safe(tmp, tmp2, &info->rx_urbs_submitted) { |
| 1385 | wrap = list_entry(tmp, struct whiteheat_urb_wrap, list); |
| 1386 | urb = wrap->urb; |
Oliver Neukum | 08a2b3b | 2007-05-24 13:52:51 +0200 | [diff] [blame] | 1387 | list_del(tmp); |
| 1388 | spin_unlock_irqrestore(&info->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1389 | usb_kill_urb(urb); |
Oliver Neukum | 08a2b3b | 2007-05-24 13:52:51 +0200 | [diff] [blame] | 1390 | spin_lock_irqsave(&info->lock, flags); |
| 1391 | list_add(tmp, &info->rx_urbs_free); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1392 | } |
| 1393 | break; |
| 1394 | } |
Oliver Neukum | 08a2b3b | 2007-05-24 13:52:51 +0200 | [diff] [blame] | 1395 | spin_lock_irqsave(&info->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1396 | list_add(tmp, &info->rx_urbs_submitted); |
| 1397 | } |
| 1398 | |
| 1399 | spin_unlock_irqrestore(&info->lock, flags); |
| 1400 | |
| 1401 | return retval; |
| 1402 | } |
| 1403 | |
| 1404 | |
| 1405 | static struct whiteheat_urb_wrap *urb_to_wrap(struct urb* urb, struct list_head *head) |
| 1406 | { |
| 1407 | struct whiteheat_urb_wrap *wrap; |
| 1408 | struct list_head *tmp; |
| 1409 | |
| 1410 | list_for_each(tmp, head) { |
| 1411 | wrap = list_entry(tmp, struct whiteheat_urb_wrap, list); |
| 1412 | if (wrap->urb == urb) |
| 1413 | return wrap; |
| 1414 | } |
| 1415 | |
| 1416 | return NULL; |
| 1417 | } |
| 1418 | |
| 1419 | |
| 1420 | static struct list_head *list_first(struct list_head *head) |
| 1421 | { |
| 1422 | return head->next; |
| 1423 | } |
| 1424 | |
| 1425 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 1426 | static void rx_data_softint(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1427 | { |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 1428 | struct whiteheat_private *info = |
| 1429 | container_of(work, struct whiteheat_private, rx_work); |
| 1430 | struct usb_serial_port *port = info->port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1431 | struct tty_struct *tty = port->tty; |
| 1432 | struct whiteheat_urb_wrap *wrap; |
| 1433 | struct urb *urb; |
| 1434 | unsigned long flags; |
| 1435 | struct list_head *tmp; |
| 1436 | struct list_head *tmp2; |
| 1437 | int result; |
| 1438 | int sent = 0; |
| 1439 | |
| 1440 | spin_lock_irqsave(&info->lock, flags); |
| 1441 | if (info->flags & THROTTLED) { |
| 1442 | spin_unlock_irqrestore(&info->lock, flags); |
| 1443 | return; |
| 1444 | } |
| 1445 | |
| 1446 | list_for_each_safe(tmp, tmp2, &info->rx_urb_q) { |
| 1447 | list_del(tmp); |
| 1448 | spin_unlock_irqrestore(&info->lock, flags); |
| 1449 | |
| 1450 | wrap = list_entry(tmp, struct whiteheat_urb_wrap, list); |
| 1451 | urb = wrap->urb; |
| 1452 | |
| 1453 | if (tty && urb->actual_length) { |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 1454 | int len = tty_buffer_request_room(tty, urb->actual_length); |
| 1455 | /* This stuff can go away now I suspect */ |
| 1456 | if (unlikely(len < urb->actual_length)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1457 | spin_lock_irqsave(&info->lock, flags); |
| 1458 | list_add(tmp, &info->rx_urb_q); |
| 1459 | spin_unlock_irqrestore(&info->lock, flags); |
| 1460 | tty_flip_buffer_push(tty); |
| 1461 | schedule_work(&info->rx_work); |
| 1462 | return; |
| 1463 | } |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 1464 | tty_insert_flip_string(tty, urb->transfer_buffer, len); |
| 1465 | sent += len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1466 | } |
| 1467 | |
| 1468 | urb->dev = port->serial->dev; |
| 1469 | result = usb_submit_urb(urb, GFP_ATOMIC); |
| 1470 | if (result) { |
| 1471 | err("%s - failed resubmitting read urb, error %d", __FUNCTION__, result); |
| 1472 | spin_lock_irqsave(&info->lock, flags); |
| 1473 | list_add(tmp, &info->rx_urbs_free); |
| 1474 | continue; |
| 1475 | } |
| 1476 | |
| 1477 | spin_lock_irqsave(&info->lock, flags); |
| 1478 | list_add(tmp, &info->rx_urbs_submitted); |
| 1479 | } |
| 1480 | spin_unlock_irqrestore(&info->lock, flags); |
| 1481 | |
| 1482 | if (sent) |
| 1483 | tty_flip_buffer_push(tty); |
| 1484 | } |
| 1485 | |
| 1486 | |
| 1487 | /***************************************************************************** |
| 1488 | * Connect Tech's White Heat module functions |
| 1489 | *****************************************************************************/ |
| 1490 | static int __init whiteheat_init (void) |
| 1491 | { |
| 1492 | int retval; |
| 1493 | retval = usb_serial_register(&whiteheat_fake_device); |
| 1494 | if (retval) |
| 1495 | goto failed_fake_register; |
| 1496 | retval = usb_serial_register(&whiteheat_device); |
| 1497 | if (retval) |
| 1498 | goto failed_device_register; |
| 1499 | retval = usb_register(&whiteheat_driver); |
| 1500 | if (retval) |
| 1501 | goto failed_usb_register; |
| 1502 | info(DRIVER_DESC " " DRIVER_VERSION); |
| 1503 | return 0; |
| 1504 | failed_usb_register: |
| 1505 | usb_serial_deregister(&whiteheat_device); |
| 1506 | failed_device_register: |
| 1507 | usb_serial_deregister(&whiteheat_fake_device); |
| 1508 | failed_fake_register: |
| 1509 | return retval; |
| 1510 | } |
| 1511 | |
| 1512 | |
| 1513 | static void __exit whiteheat_exit (void) |
| 1514 | { |
| 1515 | usb_deregister (&whiteheat_driver); |
| 1516 | usb_serial_deregister (&whiteheat_fake_device); |
| 1517 | usb_serial_deregister (&whiteheat_device); |
| 1518 | } |
| 1519 | |
| 1520 | |
| 1521 | module_init(whiteheat_init); |
| 1522 | module_exit(whiteheat_exit); |
| 1523 | |
| 1524 | MODULE_AUTHOR( DRIVER_AUTHOR ); |
| 1525 | MODULE_DESCRIPTION( DRIVER_DESC ); |
| 1526 | MODULE_LICENSE("GPL"); |
| 1527 | |
| 1528 | module_param(urb_pool_size, int, 0); |
| 1529 | MODULE_PARM_DESC(urb_pool_size, "Number of urbs to use for buffering"); |
| 1530 | |
| 1531 | module_param(debug, bool, S_IRUGO | S_IWUSR); |
| 1532 | MODULE_PARM_DESC(debug, "Debug enabled or not"); |