Greg Kroah-Hartman | 619a6f1 | 2008-02-07 23:59:03 +0100 | [diff] [blame] | 1 | /* |
| 2 | * spcp8x5 USB to serial adaptor driver |
| 3 | * |
| 4 | * Copyright (C) 2006 Linxb (xubin.lin@worldplus.com.cn) |
| 5 | * Copyright (C) 2006 S1 Corp. |
| 6 | * |
| 7 | * Original driver for 2.6.10 pl2303 driver by |
| 8 | * Greg Kroah-Hartman (greg@kroah.com) |
| 9 | * Changes for 2.6.20 by Harald Klein <hari@vt100.at> |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * |
| 17 | */ |
| 18 | #include <linux/kernel.h> |
| 19 | #include <linux/errno.h> |
| 20 | #include <linux/init.h> |
| 21 | #include <linux/slab.h> |
| 22 | #include <linux/tty.h> |
| 23 | #include <linux/tty_driver.h> |
| 24 | #include <linux/tty_flip.h> |
| 25 | #include <linux/module.h> |
| 26 | #include <linux/spinlock.h> |
| 27 | #include <linux/usb.h> |
| 28 | #include <linux/usb/serial.h> |
| 29 | |
| 30 | |
| 31 | /* Version Information */ |
| 32 | #define DRIVER_VERSION "v0.04" |
| 33 | #define DRIVER_DESC "SPCP8x5 USB to serial adaptor driver" |
| 34 | |
| 35 | static int debug; |
| 36 | |
| 37 | #define SPCP8x5_007_VID 0x04FC |
| 38 | #define SPCP8x5_007_PID 0x0201 |
| 39 | #define SPCP8x5_008_VID 0x04fc |
| 40 | #define SPCP8x5_008_PID 0x0235 |
| 41 | #define SPCP8x5_PHILIPS_VID 0x0471 |
| 42 | #define SPCP8x5_PHILIPS_PID 0x081e |
| 43 | #define SPCP8x5_INTERMATIC_VID 0x04FC |
| 44 | #define SPCP8x5_INTERMATIC_PID 0x0204 |
| 45 | #define SPCP8x5_835_VID 0x04fc |
| 46 | #define SPCP8x5_835_PID 0x0231 |
| 47 | |
| 48 | static struct usb_device_id id_table [] = { |
| 49 | { USB_DEVICE(SPCP8x5_PHILIPS_VID , SPCP8x5_PHILIPS_PID)}, |
| 50 | { USB_DEVICE(SPCP8x5_INTERMATIC_VID, SPCP8x5_INTERMATIC_PID)}, |
| 51 | { USB_DEVICE(SPCP8x5_835_VID, SPCP8x5_835_PID)}, |
| 52 | { USB_DEVICE(SPCP8x5_008_VID, SPCP8x5_008_PID)}, |
| 53 | { USB_DEVICE(SPCP8x5_007_VID, SPCP8x5_007_PID)}, |
| 54 | { } /* Terminating entry */ |
| 55 | }; |
| 56 | MODULE_DEVICE_TABLE(usb, id_table); |
| 57 | |
| 58 | struct spcp8x5_usb_ctrl_arg { |
| 59 | u8 type; |
| 60 | u8 cmd; |
| 61 | u8 cmd_type; |
| 62 | u16 value; |
| 63 | u16 index; |
| 64 | u16 length; |
| 65 | }; |
| 66 | |
| 67 | /* wait 30s before close */ |
| 68 | #define SPCP8x5_CLOSING_WAIT (30*HZ) |
| 69 | |
| 70 | #define SPCP8x5_BUF_SIZE 1024 |
| 71 | |
| 72 | |
| 73 | /* spcp8x5 spec register define */ |
| 74 | #define MCR_CONTROL_LINE_RTS 0x02 |
| 75 | #define MCR_CONTROL_LINE_DTR 0x01 |
| 76 | #define MCR_DTR 0x01 |
| 77 | #define MCR_RTS 0x02 |
| 78 | |
| 79 | #define MSR_STATUS_LINE_DCD 0x80 |
| 80 | #define MSR_STATUS_LINE_RI 0x40 |
| 81 | #define MSR_STATUS_LINE_DSR 0x20 |
| 82 | #define MSR_STATUS_LINE_CTS 0x10 |
| 83 | |
| 84 | /* verdor command here , we should define myself */ |
| 85 | #define SET_DEFAULT 0x40 |
| 86 | #define SET_DEFAULT_TYPE 0x20 |
| 87 | |
| 88 | #define SET_UART_FORMAT 0x40 |
| 89 | #define SET_UART_FORMAT_TYPE 0x21 |
| 90 | #define SET_UART_FORMAT_SIZE_5 0x00 |
| 91 | #define SET_UART_FORMAT_SIZE_6 0x01 |
| 92 | #define SET_UART_FORMAT_SIZE_7 0x02 |
| 93 | #define SET_UART_FORMAT_SIZE_8 0x03 |
| 94 | #define SET_UART_FORMAT_STOP_1 0x00 |
| 95 | #define SET_UART_FORMAT_STOP_2 0x04 |
| 96 | #define SET_UART_FORMAT_PAR_NONE 0x00 |
| 97 | #define SET_UART_FORMAT_PAR_ODD 0x10 |
| 98 | #define SET_UART_FORMAT_PAR_EVEN 0x30 |
| 99 | #define SET_UART_FORMAT_PAR_MASK 0xD0 |
| 100 | #define SET_UART_FORMAT_PAR_SPACE 0x90 |
| 101 | |
| 102 | #define GET_UART_STATUS_TYPE 0xc0 |
| 103 | #define GET_UART_STATUS 0x22 |
| 104 | #define GET_UART_STATUS_MSR 0x06 |
| 105 | |
| 106 | #define SET_UART_STATUS 0x40 |
| 107 | #define SET_UART_STATUS_TYPE 0x23 |
| 108 | #define SET_UART_STATUS_MCR 0x0004 |
| 109 | #define SET_UART_STATUS_MCR_DTR 0x01 |
| 110 | #define SET_UART_STATUS_MCR_RTS 0x02 |
| 111 | #define SET_UART_STATUS_MCR_LOOP 0x10 |
| 112 | |
| 113 | #define SET_WORKING_MODE 0x40 |
| 114 | #define SET_WORKING_MODE_TYPE 0x24 |
| 115 | #define SET_WORKING_MODE_U2C 0x00 |
| 116 | #define SET_WORKING_MODE_RS485 0x01 |
| 117 | #define SET_WORKING_MODE_PDMA 0x02 |
| 118 | #define SET_WORKING_MODE_SPP 0x03 |
| 119 | |
| 120 | #define SET_FLOWCTL_CHAR 0x40 |
| 121 | #define SET_FLOWCTL_CHAR_TYPE 0x25 |
| 122 | |
| 123 | #define GET_VERSION 0xc0 |
| 124 | #define GET_VERSION_TYPE 0x26 |
| 125 | |
| 126 | #define SET_REGISTER 0x40 |
| 127 | #define SET_REGISTER_TYPE 0x27 |
| 128 | |
| 129 | #define GET_REGISTER 0xc0 |
| 130 | #define GET_REGISTER_TYPE 0x28 |
| 131 | |
| 132 | #define SET_RAM 0x40 |
| 133 | #define SET_RAM_TYPE 0x31 |
| 134 | |
| 135 | #define GET_RAM 0xc0 |
| 136 | #define GET_RAM_TYPE 0x32 |
| 137 | |
| 138 | /* how come ??? */ |
| 139 | #define UART_STATE 0x08 |
| 140 | #define UART_STATE_TRANSIENT_MASK 0x74 |
| 141 | #define UART_DCD 0x01 |
| 142 | #define UART_DSR 0x02 |
| 143 | #define UART_BREAK_ERROR 0x04 |
| 144 | #define UART_RING 0x08 |
| 145 | #define UART_FRAME_ERROR 0x10 |
| 146 | #define UART_PARITY_ERROR 0x20 |
| 147 | #define UART_OVERRUN_ERROR 0x40 |
| 148 | #define UART_CTS 0x80 |
| 149 | |
| 150 | enum spcp8x5_type { |
| 151 | SPCP825_007_TYPE, |
| 152 | SPCP825_008_TYPE, |
| 153 | SPCP825_PHILIP_TYPE, |
| 154 | SPCP825_INTERMATIC_TYPE, |
| 155 | SPCP835_TYPE, |
| 156 | }; |
| 157 | |
| 158 | /* 1st in 1st out buffer 4 driver */ |
| 159 | struct ringbuf { |
| 160 | unsigned int buf_size; |
| 161 | char *buf_buf; |
| 162 | char *buf_get; |
| 163 | char *buf_put; |
| 164 | }; |
| 165 | |
| 166 | /* alloc the ring buf and alloc the buffer itself */ |
| 167 | static inline struct ringbuf *alloc_ringbuf(unsigned int size) |
| 168 | { |
| 169 | struct ringbuf *pb; |
| 170 | |
| 171 | if (size == 0) |
| 172 | return NULL; |
| 173 | |
| 174 | pb = kmalloc(sizeof(*pb), GFP_KERNEL); |
| 175 | if (pb == NULL) |
| 176 | return NULL; |
| 177 | |
| 178 | pb->buf_buf = kmalloc(size, GFP_KERNEL); |
| 179 | if (pb->buf_buf == NULL) { |
| 180 | kfree(pb); |
| 181 | return NULL; |
| 182 | } |
| 183 | |
| 184 | pb->buf_size = size; |
| 185 | pb->buf_get = pb->buf_put = pb->buf_buf; |
| 186 | |
| 187 | return pb; |
| 188 | } |
| 189 | |
| 190 | /* free the ring buf and the buffer itself */ |
| 191 | static inline void free_ringbuf(struct ringbuf *pb) |
| 192 | { |
| 193 | if (pb != NULL) { |
| 194 | kfree(pb->buf_buf); |
| 195 | kfree(pb); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | /* clear pipo , juest repoint the pointer here */ |
| 200 | static inline void clear_ringbuf(struct ringbuf *pb) |
| 201 | { |
| 202 | if (pb != NULL) |
| 203 | pb->buf_get = pb->buf_put; |
| 204 | } |
| 205 | |
| 206 | /* get the number of data in the pipo */ |
| 207 | static inline unsigned int ringbuf_avail_data(struct ringbuf *pb) |
| 208 | { |
| 209 | if (pb == NULL) |
| 210 | return 0; |
Alan Cox | 0487b58 | 2008-07-22 11:15:08 +0100 | [diff] [blame] | 211 | return (pb->buf_size + pb->buf_put - pb->buf_get) % pb->buf_size; |
Greg Kroah-Hartman | 619a6f1 | 2008-02-07 23:59:03 +0100 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | /* get the number of space in the pipo */ |
| 215 | static inline unsigned int ringbuf_avail_space(struct ringbuf *pb) |
| 216 | { |
| 217 | if (pb == NULL) |
| 218 | return 0; |
Alan Cox | 0487b58 | 2008-07-22 11:15:08 +0100 | [diff] [blame] | 219 | return (pb->buf_size + pb->buf_get - pb->buf_put - 1) % pb->buf_size; |
Greg Kroah-Hartman | 619a6f1 | 2008-02-07 23:59:03 +0100 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | /* put count data into pipo */ |
| 223 | static unsigned int put_ringbuf(struct ringbuf *pb, const char *buf, |
| 224 | unsigned int count) |
| 225 | { |
| 226 | unsigned int len; |
| 227 | |
| 228 | if (pb == NULL) |
| 229 | return 0; |
| 230 | |
| 231 | len = ringbuf_avail_space(pb); |
| 232 | if (count > len) |
| 233 | count = len; |
| 234 | |
| 235 | if (count == 0) |
| 236 | return 0; |
| 237 | |
| 238 | len = pb->buf_buf + pb->buf_size - pb->buf_put; |
| 239 | if (count > len) { |
| 240 | memcpy(pb->buf_put, buf, len); |
| 241 | memcpy(pb->buf_buf, buf+len, count - len); |
| 242 | pb->buf_put = pb->buf_buf + count - len; |
| 243 | } else { |
| 244 | memcpy(pb->buf_put, buf, count); |
| 245 | if (count < len) |
| 246 | pb->buf_put += count; |
| 247 | else /* count == len */ |
| 248 | pb->buf_put = pb->buf_buf; |
| 249 | } |
| 250 | return count; |
| 251 | } |
| 252 | |
| 253 | /* get count data from pipo */ |
| 254 | static unsigned int get_ringbuf(struct ringbuf *pb, char *buf, |
| 255 | unsigned int count) |
| 256 | { |
| 257 | unsigned int len; |
| 258 | |
| 259 | if (pb == NULL || buf == NULL) |
| 260 | return 0; |
| 261 | |
| 262 | len = ringbuf_avail_data(pb); |
| 263 | if (count > len) |
| 264 | count = len; |
| 265 | |
| 266 | if (count == 0) |
| 267 | return 0; |
| 268 | |
| 269 | len = pb->buf_buf + pb->buf_size - pb->buf_get; |
| 270 | if (count > len) { |
| 271 | memcpy(buf, pb->buf_get, len); |
| 272 | memcpy(buf+len, pb->buf_buf, count - len); |
| 273 | pb->buf_get = pb->buf_buf + count - len; |
| 274 | } else { |
| 275 | memcpy(buf, pb->buf_get, count); |
| 276 | if (count < len) |
| 277 | pb->buf_get += count; |
| 278 | else /* count == len */ |
| 279 | pb->buf_get = pb->buf_buf; |
| 280 | } |
| 281 | |
| 282 | return count; |
| 283 | } |
| 284 | |
| 285 | static struct usb_driver spcp8x5_driver = { |
| 286 | .name = "spcp8x5", |
| 287 | .probe = usb_serial_probe, |
| 288 | .disconnect = usb_serial_disconnect, |
| 289 | .id_table = id_table, |
| 290 | .no_dynamic_id = 1, |
| 291 | }; |
| 292 | |
| 293 | |
| 294 | struct spcp8x5_private { |
| 295 | spinlock_t lock; |
| 296 | struct ringbuf *buf; |
| 297 | int write_urb_in_use; |
| 298 | enum spcp8x5_type type; |
| 299 | wait_queue_head_t delta_msr_wait; |
| 300 | u8 line_control; |
| 301 | u8 line_status; |
| 302 | u8 termios_initialized; |
| 303 | }; |
| 304 | |
| 305 | /* desc : when device plug in,this function would be called. |
| 306 | * thanks to usb_serial subsystem,then do almost every things for us. And what |
| 307 | * we should do just alloc the buffer */ |
| 308 | static int spcp8x5_startup(struct usb_serial *serial) |
| 309 | { |
| 310 | struct spcp8x5_private *priv; |
| 311 | int i; |
| 312 | enum spcp8x5_type type = SPCP825_007_TYPE; |
Al Viro | fd05e72 | 2008-04-28 07:00:16 +0100 | [diff] [blame] | 313 | u16 product = le16_to_cpu(serial->dev->descriptor.idProduct); |
Greg Kroah-Hartman | 619a6f1 | 2008-02-07 23:59:03 +0100 | [diff] [blame] | 314 | |
Al Viro | fd05e72 | 2008-04-28 07:00:16 +0100 | [diff] [blame] | 315 | if (product == 0x0201) |
Greg Kroah-Hartman | 619a6f1 | 2008-02-07 23:59:03 +0100 | [diff] [blame] | 316 | type = SPCP825_007_TYPE; |
Al Viro | fd05e72 | 2008-04-28 07:00:16 +0100 | [diff] [blame] | 317 | else if (product == 0x0231) |
Greg Kroah-Hartman | 619a6f1 | 2008-02-07 23:59:03 +0100 | [diff] [blame] | 318 | type = SPCP835_TYPE; |
Al Viro | fd05e72 | 2008-04-28 07:00:16 +0100 | [diff] [blame] | 319 | else if (product == 0x0235) |
Greg Kroah-Hartman | 619a6f1 | 2008-02-07 23:59:03 +0100 | [diff] [blame] | 320 | type = SPCP825_008_TYPE; |
Al Viro | fd05e72 | 2008-04-28 07:00:16 +0100 | [diff] [blame] | 321 | else if (product == 0x0204) |
Greg Kroah-Hartman | 619a6f1 | 2008-02-07 23:59:03 +0100 | [diff] [blame] | 322 | type = SPCP825_INTERMATIC_TYPE; |
Al Viro | fd05e72 | 2008-04-28 07:00:16 +0100 | [diff] [blame] | 323 | else if (product == 0x0471 && |
| 324 | serial->dev->descriptor.idVendor == cpu_to_le16(0x081e)) |
Greg Kroah-Hartman | 619a6f1 | 2008-02-07 23:59:03 +0100 | [diff] [blame] | 325 | type = SPCP825_PHILIP_TYPE; |
| 326 | dev_dbg(&serial->dev->dev, "device type = %d\n", (int)type); |
| 327 | |
| 328 | for (i = 0; i < serial->num_ports; ++i) { |
| 329 | priv = kzalloc(sizeof(struct spcp8x5_private), GFP_KERNEL); |
| 330 | if (!priv) |
| 331 | goto cleanup; |
| 332 | |
| 333 | spin_lock_init(&priv->lock); |
| 334 | priv->buf = alloc_ringbuf(SPCP8x5_BUF_SIZE); |
| 335 | if (priv->buf == NULL) |
| 336 | goto cleanup2; |
| 337 | |
| 338 | init_waitqueue_head(&priv->delta_msr_wait); |
| 339 | priv->type = type; |
| 340 | usb_set_serial_port_data(serial->port[i] , priv); |
| 341 | |
| 342 | } |
| 343 | |
| 344 | return 0; |
| 345 | |
| 346 | cleanup2: |
| 347 | kfree(priv); |
| 348 | cleanup: |
| 349 | for (--i; i >= 0; --i) { |
| 350 | priv = usb_get_serial_port_data(serial->port[i]); |
| 351 | free_ringbuf(priv->buf); |
| 352 | kfree(priv); |
| 353 | usb_set_serial_port_data(serial->port[i] , NULL); |
| 354 | } |
| 355 | return -ENOMEM; |
| 356 | } |
| 357 | |
| 358 | /* call when the device plug out. free all the memory alloced by probe */ |
| 359 | static void spcp8x5_shutdown(struct usb_serial *serial) |
| 360 | { |
| 361 | int i; |
| 362 | struct spcp8x5_private *priv; |
| 363 | |
| 364 | for (i = 0; i < serial->num_ports; i++) { |
| 365 | priv = usb_get_serial_port_data(serial->port[i]); |
| 366 | if (priv) { |
| 367 | free_ringbuf(priv->buf); |
| 368 | kfree(priv); |
| 369 | usb_set_serial_port_data(serial->port[i] , NULL); |
| 370 | } |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | /* set the modem control line of the device. |
| 375 | * NOTE spcp825-007 not supported this */ |
| 376 | static int spcp8x5_set_ctrlLine(struct usb_device *dev, u8 value, |
| 377 | enum spcp8x5_type type) |
| 378 | { |
| 379 | int retval; |
| 380 | u8 mcr = 0 ; |
| 381 | |
| 382 | if (type == SPCP825_007_TYPE) |
| 383 | return -EPERM; |
| 384 | |
| 385 | mcr = (unsigned short)value; |
| 386 | retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), |
| 387 | SET_UART_STATUS_TYPE, SET_UART_STATUS, |
| 388 | mcr, 0x04, NULL, 0, 100); |
| 389 | if (retval != 0) |
| 390 | dev_dbg(&dev->dev, "usb_control_msg return %#x\n", retval); |
| 391 | return retval; |
| 392 | } |
| 393 | |
| 394 | /* get the modem status register of the device |
| 395 | * NOTE spcp825-007 not supported this */ |
| 396 | static int spcp8x5_get_msr(struct usb_device *dev, u8 *status, |
| 397 | enum spcp8x5_type type) |
| 398 | { |
| 399 | u8 *status_buffer; |
| 400 | int ret; |
| 401 | |
| 402 | /* I return Permited not support here but seem inval device |
| 403 | * is more fix */ |
| 404 | if (type == SPCP825_007_TYPE) |
| 405 | return -EPERM; |
| 406 | if (status == NULL) |
| 407 | return -EINVAL; |
| 408 | |
| 409 | status_buffer = kmalloc(1, GFP_KERNEL); |
| 410 | if (!status_buffer) |
| 411 | return -ENOMEM; |
| 412 | status_buffer[0] = status[0]; |
| 413 | |
| 414 | ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), |
| 415 | GET_UART_STATUS, GET_UART_STATUS_TYPE, |
| 416 | 0, GET_UART_STATUS_MSR, status_buffer, 1, 100); |
| 417 | if (ret < 0) |
| 418 | dev_dbg(&dev->dev, "Get MSR = 0x%p failed (error = %d)", |
| 419 | status_buffer, ret); |
| 420 | |
| 421 | dev_dbg(&dev->dev, "0xc0:0x22:0:6 %d - 0x%p ", ret, status_buffer); |
| 422 | status[0] = status_buffer[0]; |
| 423 | kfree(status_buffer); |
| 424 | |
| 425 | return ret; |
| 426 | } |
| 427 | |
| 428 | /* select the work mode. |
| 429 | * NOTE this function not supported by spcp825-007 */ |
| 430 | static void spcp8x5_set_workMode(struct usb_device *dev, u16 value, |
| 431 | u16 index, enum spcp8x5_type type) |
| 432 | { |
| 433 | int ret; |
| 434 | |
| 435 | /* I return Permited not support here but seem inval device |
| 436 | * is more fix */ |
| 437 | if (type == SPCP825_007_TYPE) |
| 438 | return; |
| 439 | |
| 440 | ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), |
| 441 | SET_WORKING_MODE_TYPE, SET_WORKING_MODE, |
| 442 | value, index, NULL, 0, 100); |
| 443 | dev_dbg(&dev->dev, "value = %#x , index = %#x\n", value, index); |
| 444 | if (ret < 0) |
| 445 | dev_dbg(&dev->dev, |
| 446 | "RTSCTS usb_control_msg(enable flowctrl) = %d\n", ret); |
| 447 | } |
| 448 | |
| 449 | /* close the serial port. We should wait for data sending to device 1st and |
| 450 | * then kill all urb. */ |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 451 | static void spcp8x5_close(struct tty_struct *tty, |
| 452 | struct usb_serial_port *port, struct file *filp) |
Greg Kroah-Hartman | 619a6f1 | 2008-02-07 23:59:03 +0100 | [diff] [blame] | 453 | { |
| 454 | struct spcp8x5_private *priv = usb_get_serial_port_data(port); |
| 455 | unsigned long flags; |
| 456 | unsigned int c_cflag; |
| 457 | int bps; |
| 458 | long timeout; |
| 459 | wait_queue_t wait; |
| 460 | int result; |
| 461 | |
| 462 | dbg("%s - port %d", __func__, port->number); |
| 463 | |
| 464 | /* wait for data to drain from the buffer */ |
| 465 | spin_lock_irqsave(&priv->lock, flags); |
| 466 | timeout = SPCP8x5_CLOSING_WAIT; |
| 467 | init_waitqueue_entry(&wait, current); |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 468 | add_wait_queue(&tty->write_wait, &wait); |
Greg Kroah-Hartman | 619a6f1 | 2008-02-07 23:59:03 +0100 | [diff] [blame] | 469 | for (;;) { |
| 470 | set_current_state(TASK_INTERRUPTIBLE); |
| 471 | if (ringbuf_avail_data(priv->buf) == 0 || |
| 472 | timeout == 0 || signal_pending(current)) |
| 473 | break; |
| 474 | spin_unlock_irqrestore(&priv->lock, flags); |
| 475 | timeout = schedule_timeout(timeout); |
| 476 | spin_lock_irqsave(&priv->lock, flags); |
| 477 | } |
| 478 | set_current_state(TASK_RUNNING); |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 479 | remove_wait_queue(&tty->write_wait, &wait); |
Greg Kroah-Hartman | 619a6f1 | 2008-02-07 23:59:03 +0100 | [diff] [blame] | 480 | |
| 481 | /* clear out any remaining data in the buffer */ |
| 482 | clear_ringbuf(priv->buf); |
| 483 | spin_unlock_irqrestore(&priv->lock, flags); |
| 484 | |
| 485 | /* wait for characters to drain from the device (this is long enough |
| 486 | * for the entire all byte spcp8x5 hardware buffer to drain with no |
| 487 | * flow control for data rates of 1200 bps or more, for lower rates we |
| 488 | * should really know how much data is in the buffer to compute a delay |
| 489 | * that is not unnecessarily long) */ |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 490 | bps = tty_get_baud_rate(tty); |
Greg Kroah-Hartman | 619a6f1 | 2008-02-07 23:59:03 +0100 | [diff] [blame] | 491 | if (bps > 1200) |
| 492 | timeout = max((HZ*2560) / bps, HZ/10); |
| 493 | else |
| 494 | timeout = 2*HZ; |
| 495 | set_current_state(TASK_INTERRUPTIBLE); |
| 496 | schedule_timeout(timeout); |
| 497 | |
| 498 | /* clear control lines */ |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 499 | if (tty) { |
| 500 | c_cflag = tty->termios->c_cflag; |
Greg Kroah-Hartman | 619a6f1 | 2008-02-07 23:59:03 +0100 | [diff] [blame] | 501 | if (c_cflag & HUPCL) { |
| 502 | spin_lock_irqsave(&priv->lock, flags); |
| 503 | priv->line_control = 0; |
| 504 | spin_unlock_irqrestore(&priv->lock, flags); |
| 505 | spcp8x5_set_ctrlLine(port->serial->dev, 0 , priv->type); |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | /* kill urb */ |
| 510 | if (port->write_urb != NULL) { |
| 511 | result = usb_unlink_urb(port->write_urb); |
| 512 | if (result) |
| 513 | dev_dbg(&port->dev, |
| 514 | "usb_unlink_urb(write_urb) = %d\n", result); |
| 515 | } |
| 516 | result = usb_unlink_urb(port->read_urb); |
| 517 | if (result) |
| 518 | dev_dbg(&port->dev, "usb_unlink_urb(read_urb) = %d\n", result); |
| 519 | } |
| 520 | |
| 521 | /* set the serial param for transfer. we should check if we really need to |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 522 | * transfer. if we set flow control we should do this too. */ |
| 523 | static void spcp8x5_set_termios(struct tty_struct *tty, |
| 524 | struct usb_serial_port *port, struct ktermios *old_termios) |
Greg Kroah-Hartman | 619a6f1 | 2008-02-07 23:59:03 +0100 | [diff] [blame] | 525 | { |
| 526 | struct usb_serial *serial = port->serial; |
| 527 | struct spcp8x5_private *priv = usb_get_serial_port_data(port); |
| 528 | unsigned long flags; |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 529 | unsigned int cflag = tty->termios->c_cflag; |
Greg Kroah-Hartman | 619a6f1 | 2008-02-07 23:59:03 +0100 | [diff] [blame] | 530 | unsigned int old_cflag = old_termios->c_cflag; |
| 531 | unsigned short uartdata; |
| 532 | unsigned char buf[2] = {0, 0}; |
| 533 | int baud; |
| 534 | int i; |
| 535 | u8 control; |
| 536 | |
Greg Kroah-Hartman | 619a6f1 | 2008-02-07 23:59:03 +0100 | [diff] [blame] | 537 | /* for the 1st time call this function */ |
| 538 | spin_lock_irqsave(&priv->lock, flags); |
| 539 | if (!priv->termios_initialized) { |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 540 | *(tty->termios) = tty_std_termios; |
| 541 | tty->termios->c_cflag = B115200 | CS8 | CREAD | HUPCL | CLOCAL; |
| 542 | tty->termios->c_ispeed = 115200; |
| 543 | tty->termios->c_ospeed = 115200; |
Greg Kroah-Hartman | 619a6f1 | 2008-02-07 23:59:03 +0100 | [diff] [blame] | 544 | priv->termios_initialized = 1; |
| 545 | } |
| 546 | spin_unlock_irqrestore(&priv->lock, flags); |
| 547 | |
| 548 | /* check that they really want us to change something */ |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 549 | if (!tty_termios_hw_change(tty->termios, old_termios)) |
Greg Kroah-Hartman | 619a6f1 | 2008-02-07 23:59:03 +0100 | [diff] [blame] | 550 | return; |
| 551 | |
| 552 | /* set DTR/RTS active */ |
| 553 | spin_lock_irqsave(&priv->lock, flags); |
| 554 | control = priv->line_control; |
| 555 | if ((old_cflag & CBAUD) == B0) { |
| 556 | priv->line_control |= MCR_DTR; |
| 557 | if (!(old_cflag & CRTSCTS)) |
| 558 | priv->line_control |= MCR_RTS; |
| 559 | } |
| 560 | if (control != priv->line_control) { |
| 561 | control = priv->line_control; |
| 562 | spin_unlock_irqrestore(&priv->lock, flags); |
| 563 | spcp8x5_set_ctrlLine(serial->dev, control , priv->type); |
| 564 | } else { |
| 565 | spin_unlock_irqrestore(&priv->lock, flags); |
| 566 | } |
| 567 | |
| 568 | /* Set Baud Rate */ |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 569 | baud = tty_get_baud_rate(tty);; |
Greg Kroah-Hartman | 619a6f1 | 2008-02-07 23:59:03 +0100 | [diff] [blame] | 570 | switch (baud) { |
| 571 | case 300: buf[0] = 0x00; break; |
| 572 | case 600: buf[0] = 0x01; break; |
| 573 | case 1200: buf[0] = 0x02; break; |
| 574 | case 2400: buf[0] = 0x03; break; |
| 575 | case 4800: buf[0] = 0x04; break; |
| 576 | case 9600: buf[0] = 0x05; break; |
| 577 | case 19200: buf[0] = 0x07; break; |
| 578 | case 38400: buf[0] = 0x09; break; |
| 579 | case 57600: buf[0] = 0x0a; break; |
| 580 | case 115200: buf[0] = 0x0b; break; |
| 581 | case 230400: buf[0] = 0x0c; break; |
| 582 | case 460800: buf[0] = 0x0d; break; |
| 583 | case 921600: buf[0] = 0x0e; break; |
| 584 | /* case 1200000: buf[0] = 0x0f; break; */ |
| 585 | /* case 2400000: buf[0] = 0x10; break; */ |
| 586 | case 3000000: buf[0] = 0x11; break; |
| 587 | /* case 6000000: buf[0] = 0x12; break; */ |
| 588 | case 0: |
| 589 | case 1000000: |
| 590 | buf[0] = 0x0b; break; |
| 591 | default: |
Greg Kroah-Hartman | 194343d | 2008-08-20 16:56:34 -0700 | [diff] [blame] | 592 | dev_err(&port->dev, "spcp825 driver does not support the " |
| 593 | "baudrate requested, using default of 9600.\n"); |
Greg Kroah-Hartman | 619a6f1 | 2008-02-07 23:59:03 +0100 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | /* Set Data Length : 00:5bit, 01:6bit, 10:7bit, 11:8bit */ |
| 597 | if (cflag & CSIZE) { |
| 598 | switch (cflag & CSIZE) { |
| 599 | case CS5: |
| 600 | buf[1] |= SET_UART_FORMAT_SIZE_5; |
| 601 | break; |
| 602 | case CS6: |
| 603 | buf[1] |= SET_UART_FORMAT_SIZE_6; |
| 604 | break; |
| 605 | case CS7: |
| 606 | buf[1] |= SET_UART_FORMAT_SIZE_7; |
| 607 | break; |
| 608 | default: |
| 609 | case CS8: |
| 610 | buf[1] |= SET_UART_FORMAT_SIZE_8; |
| 611 | break; |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | /* Set Stop bit2 : 0:1bit 1:2bit */ |
| 616 | buf[1] |= (cflag & CSTOPB) ? SET_UART_FORMAT_STOP_2 : |
| 617 | SET_UART_FORMAT_STOP_1; |
| 618 | |
| 619 | /* Set Parity bit3-4 01:Odd 11:Even */ |
| 620 | if (cflag & PARENB) { |
| 621 | buf[1] |= (cflag & PARODD) ? |
| 622 | SET_UART_FORMAT_PAR_ODD : SET_UART_FORMAT_PAR_EVEN ; |
| 623 | } else |
| 624 | buf[1] |= SET_UART_FORMAT_PAR_NONE; |
| 625 | |
| 626 | uartdata = buf[0] | buf[1]<<8; |
| 627 | |
| 628 | i = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), |
| 629 | SET_UART_FORMAT_TYPE, SET_UART_FORMAT, |
| 630 | uartdata, 0, NULL, 0, 100); |
| 631 | if (i < 0) |
Greg Kroah-Hartman | 194343d | 2008-08-20 16:56:34 -0700 | [diff] [blame] | 632 | dev_err(&port->dev, "Set UART format %#x failed (error = %d)\n", |
| 633 | uartdata, i); |
Greg Kroah-Hartman | 619a6f1 | 2008-02-07 23:59:03 +0100 | [diff] [blame] | 634 | dbg("0x21:0x40:0:0 %d\n", i); |
| 635 | |
| 636 | if (cflag & CRTSCTS) { |
| 637 | /* enable hardware flow control */ |
| 638 | spcp8x5_set_workMode(serial->dev, 0x000a, |
| 639 | SET_WORKING_MODE_U2C, priv->type); |
| 640 | } |
| 641 | return; |
| 642 | } |
| 643 | |
| 644 | /* open the serial port. do some usb system call. set termios and get the line |
| 645 | * status of the device. then submit the read urb */ |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 646 | static int spcp8x5_open(struct tty_struct *tty, |
| 647 | struct usb_serial_port *port, struct file *filp) |
Greg Kroah-Hartman | 619a6f1 | 2008-02-07 23:59:03 +0100 | [diff] [blame] | 648 | { |
| 649 | struct ktermios tmp_termios; |
| 650 | struct usb_serial *serial = port->serial; |
| 651 | struct spcp8x5_private *priv = usb_get_serial_port_data(port); |
| 652 | int ret; |
| 653 | unsigned long flags; |
| 654 | u8 status = 0x30; |
| 655 | /* status 0x30 means DSR and CTS = 1 other CDC RI and delta = 0 */ |
| 656 | |
| 657 | dbg("%s - port %d", __func__, port->number); |
| 658 | |
| 659 | usb_clear_halt(serial->dev, port->write_urb->pipe); |
| 660 | usb_clear_halt(serial->dev, port->read_urb->pipe); |
| 661 | |
| 662 | ret = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), |
| 663 | 0x09, 0x00, |
| 664 | 0x01, 0x00, NULL, 0x00, 100); |
| 665 | if (ret) |
| 666 | return ret; |
| 667 | |
| 668 | spin_lock_irqsave(&priv->lock, flags); |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 669 | if (tty && (tty->termios->c_cflag & CBAUD)) |
Greg Kroah-Hartman | 619a6f1 | 2008-02-07 23:59:03 +0100 | [diff] [blame] | 670 | priv->line_control = MCR_DTR | MCR_RTS; |
| 671 | else |
| 672 | priv->line_control = 0; |
| 673 | spin_unlock_irqrestore(&priv->lock, flags); |
| 674 | |
| 675 | spcp8x5_set_ctrlLine(serial->dev, priv->line_control , priv->type); |
| 676 | |
| 677 | /* Setup termios */ |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 678 | if (tty) |
| 679 | spcp8x5_set_termios(tty, port, &tmp_termios); |
Greg Kroah-Hartman | 619a6f1 | 2008-02-07 23:59:03 +0100 | [diff] [blame] | 680 | |
| 681 | spcp8x5_get_msr(serial->dev, &status, priv->type); |
| 682 | |
| 683 | /* may be we should update uart status here but now we did not do */ |
| 684 | spin_lock_irqsave(&priv->lock, flags); |
| 685 | priv->line_status = status & 0xf0 ; |
| 686 | spin_unlock_irqrestore(&priv->lock, flags); |
| 687 | |
| 688 | /* FIXME: need to assert RTS and DTR if CRTSCTS off */ |
| 689 | |
| 690 | dbg("%s - submitting read urb", __func__); |
| 691 | port->read_urb->dev = serial->dev; |
| 692 | ret = usb_submit_urb(port->read_urb, GFP_KERNEL); |
| 693 | if (ret) { |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 694 | spcp8x5_close(tty, port, NULL); |
Greg Kroah-Hartman | 619a6f1 | 2008-02-07 23:59:03 +0100 | [diff] [blame] | 695 | return -EPROTO; |
| 696 | } |
| 697 | return 0; |
| 698 | } |
| 699 | |
| 700 | /* bulk read call back function. check the status of the urb. if transfer |
| 701 | * failed return. then update the status and the tty send data to tty subsys. |
| 702 | * submit urb again. |
| 703 | */ |
| 704 | static void spcp8x5_read_bulk_callback(struct urb *urb) |
| 705 | { |
| 706 | struct usb_serial_port *port = urb->context; |
| 707 | struct spcp8x5_private *priv = usb_get_serial_port_data(port); |
| 708 | struct tty_struct *tty; |
| 709 | unsigned char *data = urb->transfer_buffer; |
| 710 | unsigned long flags; |
| 711 | int i; |
Greg Kroah-Hartman | 50de36f | 2008-12-10 16:00:30 -0800 | [diff] [blame] | 712 | int result = urb->status; |
| 713 | u8 status; |
Greg Kroah-Hartman | 619a6f1 | 2008-02-07 23:59:03 +0100 | [diff] [blame] | 714 | char tty_flag; |
| 715 | |
Greg Kroah-Hartman | 50de36f | 2008-12-10 16:00:30 -0800 | [diff] [blame] | 716 | dev_dbg(&port->dev, "start, result = %d, urb->actual_length = %d\n,", |
| 717 | result, urb->actual_length); |
Greg Kroah-Hartman | 619a6f1 | 2008-02-07 23:59:03 +0100 | [diff] [blame] | 718 | |
| 719 | /* check the urb status */ |
Greg Kroah-Hartman | 50de36f | 2008-12-10 16:00:30 -0800 | [diff] [blame] | 720 | if (result) { |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 721 | if (!port->port.count) |
Greg Kroah-Hartman | 619a6f1 | 2008-02-07 23:59:03 +0100 | [diff] [blame] | 722 | return; |
Greg Kroah-Hartman | 50de36f | 2008-12-10 16:00:30 -0800 | [diff] [blame] | 723 | if (result == -EPROTO) { |
Greg Kroah-Hartman | 619a6f1 | 2008-02-07 23:59:03 +0100 | [diff] [blame] | 724 | /* spcp8x5 mysteriously fails with -EPROTO */ |
| 725 | /* reschedule the read */ |
Greg Kroah-Hartman | 619a6f1 | 2008-02-07 23:59:03 +0100 | [diff] [blame] | 726 | urb->dev = port->serial->dev; |
| 727 | result = usb_submit_urb(urb , GFP_ATOMIC); |
| 728 | if (result) |
| 729 | dev_dbg(&port->dev, |
| 730 | "failed submitting read urb %d\n", |
| 731 | result); |
| 732 | return; |
| 733 | } |
| 734 | dev_dbg(&port->dev, "unable to handle the error, exiting.\n"); |
| 735 | return; |
| 736 | } |
| 737 | |
| 738 | /* get tty_flag from status */ |
| 739 | tty_flag = TTY_NORMAL; |
| 740 | |
| 741 | spin_lock_irqsave(&priv->lock, flags); |
| 742 | status = priv->line_status; |
| 743 | priv->line_status &= ~UART_STATE_TRANSIENT_MASK; |
| 744 | spin_unlock_irqrestore(&priv->lock, flags); |
| 745 | /* wake up the wait for termios */ |
| 746 | wake_up_interruptible(&priv->delta_msr_wait); |
| 747 | |
| 748 | /* break takes precedence over parity, which takes precedence over |
| 749 | * framing errors */ |
| 750 | if (status & UART_BREAK_ERROR) |
| 751 | tty_flag = TTY_BREAK; |
| 752 | else if (status & UART_PARITY_ERROR) |
| 753 | tty_flag = TTY_PARITY; |
| 754 | else if (status & UART_FRAME_ERROR) |
| 755 | tty_flag = TTY_FRAME; |
| 756 | dev_dbg(&port->dev, "tty_flag = %d\n", tty_flag); |
| 757 | |
Alan Cox | 4a90f09 | 2008-10-13 10:39:46 +0100 | [diff] [blame] | 758 | tty = tty_port_tty_get(&port->port); |
Greg Kroah-Hartman | 619a6f1 | 2008-02-07 23:59:03 +0100 | [diff] [blame] | 759 | if (tty && urb->actual_length) { |
| 760 | tty_buffer_request_room(tty, urb->actual_length + 1); |
| 761 | /* overrun is special, not associated with a char */ |
| 762 | if (status & UART_OVERRUN_ERROR) |
| 763 | tty_insert_flip_char(tty, 0, TTY_OVERRUN); |
| 764 | for (i = 0; i < urb->actual_length; ++i) |
| 765 | tty_insert_flip_char(tty, data[i], tty_flag); |
| 766 | tty_flip_buffer_push(tty); |
| 767 | } |
Alan Cox | 4a90f09 | 2008-10-13 10:39:46 +0100 | [diff] [blame] | 768 | tty_kref_put(tty); |
Greg Kroah-Hartman | 619a6f1 | 2008-02-07 23:59:03 +0100 | [diff] [blame] | 769 | |
| 770 | /* Schedule the next read _if_ we are still open */ |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 771 | if (port->port.count) { |
Greg Kroah-Hartman | 619a6f1 | 2008-02-07 23:59:03 +0100 | [diff] [blame] | 772 | urb->dev = port->serial->dev; |
| 773 | result = usb_submit_urb(urb , GFP_ATOMIC); |
| 774 | if (result) |
| 775 | dev_dbg(&port->dev, "failed submitting read urb %d\n", |
| 776 | result); |
| 777 | } |
| 778 | |
| 779 | return; |
| 780 | } |
| 781 | |
| 782 | /* get data from ring buffer and then write to usb bus */ |
| 783 | static void spcp8x5_send(struct usb_serial_port *port) |
| 784 | { |
| 785 | int count, result; |
| 786 | struct spcp8x5_private *priv = usb_get_serial_port_data(port); |
| 787 | unsigned long flags; |
| 788 | |
| 789 | spin_lock_irqsave(&priv->lock, flags); |
| 790 | |
| 791 | if (priv->write_urb_in_use) { |
| 792 | dev_dbg(&port->dev, "write urb still used\n"); |
| 793 | spin_unlock_irqrestore(&priv->lock, flags); |
| 794 | return; |
| 795 | } |
| 796 | |
| 797 | /* send the 1st urb for writting */ |
| 798 | memset(port->write_urb->transfer_buffer , 0x00 , port->bulk_out_size); |
| 799 | count = get_ringbuf(priv->buf, port->write_urb->transfer_buffer, |
| 800 | port->bulk_out_size); |
| 801 | |
| 802 | if (count == 0) { |
| 803 | spin_unlock_irqrestore(&priv->lock, flags); |
| 804 | return; |
| 805 | } |
| 806 | |
| 807 | /* update the urb status */ |
| 808 | priv->write_urb_in_use = 1; |
| 809 | |
| 810 | spin_unlock_irqrestore(&priv->lock, flags); |
| 811 | |
| 812 | port->write_urb->transfer_buffer_length = count; |
| 813 | port->write_urb->dev = port->serial->dev; |
| 814 | |
| 815 | result = usb_submit_urb(port->write_urb, GFP_ATOMIC); |
| 816 | if (result) { |
| 817 | dev_dbg(&port->dev, "failed submitting write urb, error %d\n", |
| 818 | result); |
| 819 | priv->write_urb_in_use = 0; |
| 820 | /* TODO: reschedule spcp8x5_send */ |
| 821 | } |
| 822 | |
| 823 | |
| 824 | schedule_work(&port->work); |
| 825 | } |
| 826 | |
| 827 | /* this is the call back function for write urb. NOTE we should not sleep in |
| 828 | * this routine. check the urb return code and then submit the write urb again |
| 829 | * to hold the write loop */ |
| 830 | static void spcp8x5_write_bulk_callback(struct urb *urb) |
| 831 | { |
| 832 | struct usb_serial_port *port = urb->context; |
| 833 | struct spcp8x5_private *priv = usb_get_serial_port_data(port); |
| 834 | int result; |
Greg Kroah-Hartman | 50de36f | 2008-12-10 16:00:30 -0800 | [diff] [blame] | 835 | int status = urb->status; |
Greg Kroah-Hartman | 619a6f1 | 2008-02-07 23:59:03 +0100 | [diff] [blame] | 836 | |
Greg Kroah-Hartman | 50de36f | 2008-12-10 16:00:30 -0800 | [diff] [blame] | 837 | switch (status) { |
Greg Kroah-Hartman | 619a6f1 | 2008-02-07 23:59:03 +0100 | [diff] [blame] | 838 | case 0: |
| 839 | /* success */ |
| 840 | break; |
| 841 | case -ECONNRESET: |
| 842 | case -ENOENT: |
| 843 | case -ESHUTDOWN: |
| 844 | /* this urb is terminated, clean up */ |
| 845 | dev_dbg(&port->dev, "urb shutting down with status: %d\n", |
Greg Kroah-Hartman | 50de36f | 2008-12-10 16:00:30 -0800 | [diff] [blame] | 846 | status); |
Greg Kroah-Hartman | 619a6f1 | 2008-02-07 23:59:03 +0100 | [diff] [blame] | 847 | priv->write_urb_in_use = 0; |
| 848 | return; |
| 849 | default: |
| 850 | /* error in the urb, so we have to resubmit it */ |
| 851 | dbg("%s - Overflow in write", __func__); |
| 852 | dbg("%s - nonzero write bulk status received: %d", |
Greg Kroah-Hartman | 50de36f | 2008-12-10 16:00:30 -0800 | [diff] [blame] | 853 | __func__, status); |
Greg Kroah-Hartman | 619a6f1 | 2008-02-07 23:59:03 +0100 | [diff] [blame] | 854 | port->write_urb->transfer_buffer_length = 1; |
| 855 | port->write_urb->dev = port->serial->dev; |
| 856 | result = usb_submit_urb(port->write_urb, GFP_ATOMIC); |
| 857 | if (result) |
| 858 | dev_dbg(&port->dev, |
| 859 | "failed resubmitting write urb %d\n", result); |
| 860 | else |
| 861 | return; |
| 862 | } |
| 863 | |
| 864 | priv->write_urb_in_use = 0; |
| 865 | |
| 866 | /* send any buffered data */ |
| 867 | spcp8x5_send(port); |
| 868 | } |
| 869 | |
| 870 | /* write data to ring buffer. and then start the write transfer */ |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 871 | static int spcp8x5_write(struct tty_struct *tty, struct usb_serial_port *port, |
Greg Kroah-Hartman | 619a6f1 | 2008-02-07 23:59:03 +0100 | [diff] [blame] | 872 | const unsigned char *buf, int count) |
| 873 | { |
| 874 | struct spcp8x5_private *priv = usb_get_serial_port_data(port); |
| 875 | unsigned long flags; |
| 876 | |
| 877 | dev_dbg(&port->dev, "%d bytes\n", count); |
| 878 | |
| 879 | if (!count) |
| 880 | return count; |
| 881 | |
| 882 | spin_lock_irqsave(&priv->lock, flags); |
| 883 | count = put_ringbuf(priv->buf, buf, count); |
| 884 | spin_unlock_irqrestore(&priv->lock, flags); |
| 885 | |
| 886 | spcp8x5_send(port); |
| 887 | |
| 888 | return count; |
| 889 | } |
| 890 | |
| 891 | static int spcp8x5_wait_modem_info(struct usb_serial_port *port, |
| 892 | unsigned int arg) |
| 893 | { |
| 894 | struct spcp8x5_private *priv = usb_get_serial_port_data(port); |
| 895 | unsigned long flags; |
| 896 | unsigned int prevstatus; |
| 897 | unsigned int status; |
| 898 | unsigned int changed; |
| 899 | |
| 900 | spin_lock_irqsave(&priv->lock, flags); |
| 901 | prevstatus = priv->line_status; |
| 902 | spin_unlock_irqrestore(&priv->lock, flags); |
| 903 | |
| 904 | while (1) { |
| 905 | /* wake up in bulk read */ |
| 906 | interruptible_sleep_on(&priv->delta_msr_wait); |
| 907 | |
| 908 | /* see if a signal did it */ |
| 909 | if (signal_pending(current)) |
| 910 | return -ERESTARTSYS; |
| 911 | |
| 912 | spin_lock_irqsave(&priv->lock, flags); |
| 913 | status = priv->line_status; |
| 914 | spin_unlock_irqrestore(&priv->lock, flags); |
| 915 | |
| 916 | changed = prevstatus^status; |
| 917 | |
| 918 | if (((arg & TIOCM_RNG) && (changed & MSR_STATUS_LINE_RI)) || |
| 919 | ((arg & TIOCM_DSR) && (changed & MSR_STATUS_LINE_DSR)) || |
| 920 | ((arg & TIOCM_CD) && (changed & MSR_STATUS_LINE_DCD)) || |
| 921 | ((arg & TIOCM_CTS) && (changed & MSR_STATUS_LINE_CTS))) |
| 922 | return 0; |
| 923 | |
| 924 | prevstatus = status; |
| 925 | } |
| 926 | /* NOTREACHED */ |
| 927 | return 0; |
| 928 | } |
| 929 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 930 | static int spcp8x5_ioctl(struct tty_struct *tty, struct file *file, |
Greg Kroah-Hartman | 619a6f1 | 2008-02-07 23:59:03 +0100 | [diff] [blame] | 931 | unsigned int cmd, unsigned long arg) |
| 932 | { |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 933 | struct usb_serial_port *port = tty->driver_data; |
Greg Kroah-Hartman | 619a6f1 | 2008-02-07 23:59:03 +0100 | [diff] [blame] | 934 | dbg("%s (%d) cmd = 0x%04x", __func__, port->number, cmd); |
| 935 | |
| 936 | switch (cmd) { |
| 937 | case TIOCMIWAIT: |
| 938 | dbg("%s (%d) TIOCMIWAIT", __func__, port->number); |
| 939 | return spcp8x5_wait_modem_info(port, arg); |
| 940 | |
| 941 | default: |
| 942 | dbg("%s not supported = 0x%04x", __func__, cmd); |
| 943 | break; |
| 944 | } |
| 945 | |
| 946 | return -ENOIOCTLCMD; |
| 947 | } |
| 948 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 949 | static int spcp8x5_tiocmset(struct tty_struct *tty, struct file *file, |
Greg Kroah-Hartman | 619a6f1 | 2008-02-07 23:59:03 +0100 | [diff] [blame] | 950 | unsigned int set, unsigned int clear) |
| 951 | { |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 952 | struct usb_serial_port *port = tty->driver_data; |
Greg Kroah-Hartman | 619a6f1 | 2008-02-07 23:59:03 +0100 | [diff] [blame] | 953 | struct spcp8x5_private *priv = usb_get_serial_port_data(port); |
| 954 | unsigned long flags; |
| 955 | u8 control; |
| 956 | |
| 957 | spin_lock_irqsave(&priv->lock, flags); |
| 958 | if (set & TIOCM_RTS) |
| 959 | priv->line_control |= MCR_RTS; |
| 960 | if (set & TIOCM_DTR) |
| 961 | priv->line_control |= MCR_DTR; |
| 962 | if (clear & TIOCM_RTS) |
| 963 | priv->line_control &= ~MCR_RTS; |
| 964 | if (clear & TIOCM_DTR) |
| 965 | priv->line_control &= ~MCR_DTR; |
| 966 | control = priv->line_control; |
| 967 | spin_unlock_irqrestore(&priv->lock, flags); |
| 968 | |
| 969 | return spcp8x5_set_ctrlLine(port->serial->dev, control , priv->type); |
| 970 | } |
| 971 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 972 | static int spcp8x5_tiocmget(struct tty_struct *tty, struct file *file) |
Greg Kroah-Hartman | 619a6f1 | 2008-02-07 23:59:03 +0100 | [diff] [blame] | 973 | { |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 974 | struct usb_serial_port *port = tty->driver_data; |
Greg Kroah-Hartman | 619a6f1 | 2008-02-07 23:59:03 +0100 | [diff] [blame] | 975 | struct spcp8x5_private *priv = usb_get_serial_port_data(port); |
| 976 | unsigned long flags; |
| 977 | unsigned int mcr; |
| 978 | unsigned int status; |
| 979 | unsigned int result; |
| 980 | |
| 981 | spin_lock_irqsave(&priv->lock, flags); |
| 982 | mcr = priv->line_control; |
| 983 | status = priv->line_status; |
| 984 | spin_unlock_irqrestore(&priv->lock, flags); |
| 985 | |
| 986 | result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0) |
| 987 | | ((mcr & MCR_RTS) ? TIOCM_RTS : 0) |
| 988 | | ((status & MSR_STATUS_LINE_CTS) ? TIOCM_CTS : 0) |
| 989 | | ((status & MSR_STATUS_LINE_DSR) ? TIOCM_DSR : 0) |
| 990 | | ((status & MSR_STATUS_LINE_RI) ? TIOCM_RI : 0) |
| 991 | | ((status & MSR_STATUS_LINE_DCD) ? TIOCM_CD : 0); |
| 992 | |
| 993 | return result; |
| 994 | } |
| 995 | |
| 996 | /* get the avail space room in ring buffer */ |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 997 | static int spcp8x5_write_room(struct tty_struct *tty) |
Greg Kroah-Hartman | 619a6f1 | 2008-02-07 23:59:03 +0100 | [diff] [blame] | 998 | { |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 999 | struct usb_serial_port *port = tty->driver_data; |
Greg Kroah-Hartman | 619a6f1 | 2008-02-07 23:59:03 +0100 | [diff] [blame] | 1000 | struct spcp8x5_private *priv = usb_get_serial_port_data(port); |
| 1001 | int room = 0; |
| 1002 | unsigned long flags; |
| 1003 | |
| 1004 | spin_lock_irqsave(&priv->lock, flags); |
| 1005 | room = ringbuf_avail_space(priv->buf); |
| 1006 | spin_unlock_irqrestore(&priv->lock, flags); |
| 1007 | |
| 1008 | return room; |
| 1009 | } |
| 1010 | |
| 1011 | /* get the number of avail data in write ring buffer */ |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 1012 | static int spcp8x5_chars_in_buffer(struct tty_struct *tty) |
Greg Kroah-Hartman | 619a6f1 | 2008-02-07 23:59:03 +0100 | [diff] [blame] | 1013 | { |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 1014 | struct usb_serial_port *port = tty->driver_data; |
Greg Kroah-Hartman | 619a6f1 | 2008-02-07 23:59:03 +0100 | [diff] [blame] | 1015 | struct spcp8x5_private *priv = usb_get_serial_port_data(port); |
| 1016 | int chars = 0; |
| 1017 | unsigned long flags; |
| 1018 | |
| 1019 | spin_lock_irqsave(&priv->lock, flags); |
| 1020 | chars = ringbuf_avail_data(priv->buf); |
| 1021 | spin_unlock_irqrestore(&priv->lock, flags); |
| 1022 | |
| 1023 | return chars; |
| 1024 | } |
| 1025 | |
| 1026 | /* All of the device info needed for the spcp8x5 SIO serial converter */ |
| 1027 | static struct usb_serial_driver spcp8x5_device = { |
| 1028 | .driver = { |
| 1029 | .owner = THIS_MODULE, |
| 1030 | .name = "SPCP8x5", |
| 1031 | }, |
| 1032 | .id_table = id_table, |
Greg Kroah-Hartman | 619a6f1 | 2008-02-07 23:59:03 +0100 | [diff] [blame] | 1033 | .num_ports = 1, |
| 1034 | .open = spcp8x5_open, |
| 1035 | .close = spcp8x5_close, |
| 1036 | .write = spcp8x5_write, |
| 1037 | .set_termios = spcp8x5_set_termios, |
| 1038 | .ioctl = spcp8x5_ioctl, |
| 1039 | .tiocmget = spcp8x5_tiocmget, |
| 1040 | .tiocmset = spcp8x5_tiocmset, |
| 1041 | .write_room = spcp8x5_write_room, |
| 1042 | .read_bulk_callback = spcp8x5_read_bulk_callback, |
| 1043 | .write_bulk_callback = spcp8x5_write_bulk_callback, |
| 1044 | .chars_in_buffer = spcp8x5_chars_in_buffer, |
| 1045 | .attach = spcp8x5_startup, |
| 1046 | .shutdown = spcp8x5_shutdown, |
| 1047 | }; |
| 1048 | |
| 1049 | static int __init spcp8x5_init(void) |
| 1050 | { |
| 1051 | int retval; |
| 1052 | retval = usb_serial_register(&spcp8x5_device); |
| 1053 | if (retval) |
| 1054 | goto failed_usb_serial_register; |
| 1055 | retval = usb_register(&spcp8x5_driver); |
| 1056 | if (retval) |
| 1057 | goto failed_usb_register; |
Greg Kroah-Hartman | c197a8d | 2008-08-18 13:21:04 -0700 | [diff] [blame] | 1058 | printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":" |
| 1059 | DRIVER_DESC "\n"); |
Greg Kroah-Hartman | 619a6f1 | 2008-02-07 23:59:03 +0100 | [diff] [blame] | 1060 | return 0; |
| 1061 | failed_usb_register: |
| 1062 | usb_serial_deregister(&spcp8x5_device); |
| 1063 | failed_usb_serial_register: |
| 1064 | return retval; |
| 1065 | } |
| 1066 | |
| 1067 | static void __exit spcp8x5_exit(void) |
| 1068 | { |
| 1069 | usb_deregister(&spcp8x5_driver); |
| 1070 | usb_serial_deregister(&spcp8x5_device); |
| 1071 | } |
| 1072 | |
| 1073 | module_init(spcp8x5_init); |
| 1074 | module_exit(spcp8x5_exit); |
| 1075 | |
| 1076 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 1077 | MODULE_VERSION(DRIVER_VERSION); |
| 1078 | MODULE_LICENSE("GPL"); |
| 1079 | |
| 1080 | module_param(debug, bool, S_IRUGO | S_IWUSR); |
| 1081 | MODULE_PARM_DESC(debug, "Debug enabled or not"); |