1/* 2 * Driver for Quatech Inc USB2.0 to serial adaptors. Largely unrelated to the 3 * serqt_usb driver, based on a re-write of the vendor supplied serqt_usb2 code, 4 * which is unrelated to the serqt_usb2 in the staging kernel 5 */ 6 7#include <linux/errno.h> 8#include <linux/init.h> 9#include <linux/slab.h> 10#include <linux/tty.h> 11#include <linux/tty_driver.h> 12#include <linux/tty_flip.h> 13#include <linux/module.h> 14#include <linux/serial.h> 15#include <linux/usb.h> 16#include <linux/usb/serial.h> 17#include <linux/uaccess.h> 18 19static bool debug; 20 21/* Version Information */ 22#define DRIVER_VERSION "v2.00" 23#define DRIVER_AUTHOR "Tim Gobeli, Quatech, Inc" 24#define DRIVER_DESC "Quatech USB 2.0 to Serial Driver" 25 26/* vendor and device IDs */ 27#define USB_VENDOR_ID_QUATECH 0x061d /* Quatech VID */ 28#define QUATECH_SSU2_100 0xC120 /* RS232 single port */ 29#define QUATECH_DSU2_100 0xC140 /* RS232 dual port */ 30#define QUATECH_DSU2_400 0xC150 /* RS232/422/485 dual port */ 31#define QUATECH_QSU2_100 0xC160 /* RS232 four port */ 32#define QUATECH_QSU2_400 0xC170 /* RS232/422/485 four port */ 33#define QUATECH_ESU2_100 0xC1A0 /* RS232 eight port */ 34#define QUATECH_ESU2_400 0xC180 /* RS232/422/485 eight port */ 35 36/* magic numbers go here, when we find out which ones are needed */ 37 38#define QU2BOXPWRON 0x8000 /* magic number to turn FPGA power on */ 39#define QU2BOX232 0x40 /* RS232 mode on MEI devices */ 40#define QU2BOXSPD9600 0x60 /* set speed to 9600 baud */ 41#define QT2_FIFO_DEPTH 1024 /* size of hardware fifos */ 42#define QT2_TX_HEADER_LENGTH 5 43/* length of the header sent to the box with each write URB */ 44 45/* directions for USB transfers */ 46#define USBD_TRANSFER_DIRECTION_IN 0xc0 47#define USBD_TRANSFER_DIRECTION_OUT 0x40 48 49/* special Quatech command IDs. These are pushed down the 50 USB control pipe to get the box on the end to do things */ 51#define QT_SET_GET_DEVICE 0xc2 52#define QT_OPEN_CLOSE_CHANNEL 0xca 53/*#define QT_GET_SET_PREBUF_TRIG_LVL 0xcc 54#define QT_SET_ATF 0xcd*/ 55#define QT2_GET_SET_REGISTER 0xc0 56#define QT2_GET_SET_UART 0xc1 57#define QT2_HW_FLOW_CONTROL_MASK 0xc5 58#define QT2_SW_FLOW_CONTROL_MASK 0xc6 59#define QT2_SW_FLOW_CONTROL_DISABLE 0xc7 60#define QT2_BREAK_CONTROL 0xc8 61#define QT2_STOP_RECEIVE 0xe0 62#define QT2_FLUSH_DEVICE 0xc4 63#define QT2_GET_SET_QMCR 0xe1 64 65/* sorts of flush we can do on */ 66#define QT2_FLUSH_RX 0x00 67#define QT2_FLUSH_TX 0x01 68 69/* port setting constants, used to set up serial port speeds, flow 70 * control and so on */ 71#define QT2_SERIAL_MCR_DTR 0x01 72#define QT2_SERIAL_MCR_RTS 0x02 73#define QT2_SERIAL_MCR_LOOP 0x10 74 75#define QT2_SERIAL_MSR_CTS 0x10 76#define QT2_SERIAL_MSR_CD 0x80 77#define QT2_SERIAL_MSR_RI 0x40 78#define QT2_SERIAL_MSR_DSR 0x20 79#define QT2_SERIAL_MSR_MASK 0xf0 80 81#define QT2_SERIAL_8_DATA 0x03 82#define QT2_SERIAL_7_DATA 0x02 83#define QT2_SERIAL_6_DATA 0x01 84#define QT2_SERIAL_5_DATA 0x00 85 86#define QT2_SERIAL_ODD_PARITY 0x08 87#define QT2_SERIAL_EVEN_PARITY 0x18 88#define QT2_SERIAL_TWO_STOPB 0x04 89#define QT2_SERIAL_ONE_STOPB 0x00 90 91#define QT2_MAX_BAUD_RATE 921600 92#define QT2_MAX_BAUD_REMAINDER 4608 93 94#define QT2_SERIAL_LSR_OE 0x02 95#define QT2_SERIAL_LSR_PE 0x04 96#define QT2_SERIAL_LSR_FE 0x08 97#define QT2_SERIAL_LSR_BI 0x10 98 99/* value of Line Status Register when UART has completed 100 * emptying data out on the line */ 101#define QT2_LSR_TEMT 0x40 102 103/* register numbers on each UART, for use with qt2_box_[get|set]_register*/ 104#define QT2_XMT_HOLD_REGISTER 0x00 105#define QT2_XVR_BUFFER_REGISTER 0x00 106#define QT2_FIFO_CONTROL_REGISTER 0x02 107#define QT2_LINE_CONTROL_REGISTER 0x03 108#define QT2_MODEM_CONTROL_REGISTER 0x04 109#define QT2_LINE_STATUS_REGISTER 0x05 110#define QT2_MODEM_STATUS_REGISTER 0x06 111 112/* handy macros for doing escape sequence parsing on data reads */ 113#define THISCHAR ((unsigned char *)(urb->transfer_buffer))[i] 114#define NEXTCHAR ((unsigned char *)(urb->transfer_buffer))[i + 1] 115#define THIRDCHAR ((unsigned char *)(urb->transfer_buffer))[i + 2] 116#define FOURTHCHAR ((unsigned char *)(urb->transfer_buffer))[i + 3] 117#define FIFTHCHAR ((unsigned char *)(urb->transfer_buffer))[i + 4] 118 119static const struct usb_device_id quausb2_id_table[] = { 120 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_SSU2_100)}, 121 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_DSU2_100)}, 122 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_DSU2_400)}, 123 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_QSU2_100)}, 124 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_QSU2_400)}, 125 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_ESU2_100)}, 126 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_ESU2_400)}, 127 {} /* Terminating entry */ 128}; 129 130MODULE_DEVICE_TABLE(usb, quausb2_id_table); 131 132/* custom structures we need go here */ 133static struct usb_driver quausb2_usb_driver = { 134 .name = "quatech-usb2-serial", 135 .probe = usb_serial_probe, 136 .disconnect = usb_serial_disconnect, 137 .id_table = quausb2_id_table, 138}; 139 140/** 141 * quatech2_port: Structure in which to keep all the messy stuff that this 142 * driver needs alongside the usb_serial_port structure 143 * @read_urb_busy: Flag indicating that port->read_urb is in use 144 * @close_pending: flag indicating that this port is in the process of 145 * being closed (and so no new reads / writes should be started). 146 * @shadowLSR: Last received state of the line status register, holds the 147 * value of the line status flags from the port 148 * @shadowMSR: Last received state of the modem status register, holds 149 * the value of the modem status received from the port 150 * @rcv_flush: Flag indicating that a receive flush has occurred on 151 * the hardware. 152 * @xmit_flush: Flag indicating that a transmit flush has been processed by 153 * the hardware. 154 * @tx_pending_bytes: Number of bytes waiting to be sent. This total 155 * includes the size (excluding header) of URBs that have been submitted but 156 * have not yet been sent to to the device, and bytes that have been sent out 157 * of the port but not yet reported sent by the "xmit_empty" messages (which 158 * indicate the number of bytes sent each time they are received, despite the 159 * misleading name). 160 * - Starts at zero when port is initialised. 161 * - is incremented by the size of the data to be written (no headers) 162 * each time a write urb is dispatched. 163 * - is decremented each time a "transmit empty" message is received 164 * by the driver in the data stream. 165 * @lock: Mutex to lock access to this structure when we need to ensure that 166 * races don't occur to access bits of it. 167 * @open_count: The number of uses of the port currently having 168 * it open, i.e. the reference count. 169 */ 170struct quatech2_port { 171 int magic; 172 bool read_urb_busy; 173 bool close_pending; 174 __u8 shadowLSR; 175 __u8 shadowMSR; 176 bool rcv_flush; 177 bool xmit_flush; 178 int tx_pending_bytes; 179 struct mutex modelock; 180 int open_count; 181 182 char active; /* someone has this device open */ 183 unsigned char *xfer_to_tty_buffer; 184 wait_queue_head_t wait; 185 __u8 shadowLCR; /* last LCR value received */ 186 __u8 shadowMCR; /* last MCR value received */ 187 char RxHolding; 188 struct semaphore pend_xmit_sem; /* locks this structure */ 189 spinlock_t lock; 190}; 191 192/** 193 * Structure to hold device-wide internal status information 194 * @param ReadBulkStopped The last bulk read attempt ended in tears 195 * @param open_ports The number of serial ports currently in use on the box 196 * @param current_port Pointer to the serial port structure of the port which 197 * the read stream is currently directed to. Escape sequences in the read 198 * stream will change this around as data arrives from different ports on the 199 * box 200 * @buffer_size: The max size buffer each URB can take, used to set the size of 201 * the buffers allocated for writing to each port on the device (we need to 202 * store this because it is known only to the endpoint, but used each time a 203 * port is opened and a new buffer is allocated. 204 */ 205struct quatech2_dev { 206 bool ReadBulkStopped; 207 char open_ports; 208 struct usb_serial_port *current_port; 209 int buffer_size; 210}; 211 212/* structure which holds line and modem status flags */ 213struct qt2_status_data { 214 __u8 line_status; 215 __u8 modem_status; 216}; 217 218/* Function prototypes */ 219static int qt2_boxpoweron(struct usb_serial *serial); 220static int qt2_boxsetQMCR(struct usb_serial *serial, __u16 Uart_Number, 221 __u8 QMCR_Value); 222static int port_paranoia_check(struct usb_serial_port *port, 223 const char *function); 224static int serial_paranoia_check(struct usb_serial *serial, 225 const char *function); 226static inline struct quatech2_port *qt2_get_port_private(struct usb_serial_port 227 *port); 228static inline void qt2_set_port_private(struct usb_serial_port *port, 229 struct quatech2_port *data); 230static inline struct quatech2_dev *qt2_get_dev_private(struct usb_serial 231 *serial); 232static inline void qt2_set_dev_private(struct usb_serial *serial, 233 struct quatech2_dev *data); 234static int qt2_openboxchannel(struct usb_serial *serial, __u16 235 Uart_Number, struct qt2_status_data *pDeviceData); 236static int qt2_closeboxchannel(struct usb_serial *serial, __u16 237 Uart_Number); 238static int qt2_conf_uart(struct usb_serial *serial, unsigned short Uart_Number, 239 unsigned short divisor, unsigned char LCR); 240static void qt2_read_bulk_callback(struct urb *urb); 241static void qt2_write_bulk_callback(struct urb *urb); 242static void qt2_process_line_status(struct usb_serial_port *port, 243 unsigned char LineStatus); 244static void qt2_process_modem_status(struct usb_serial_port *port, 245 unsigned char ModemStatus); 246static void qt2_process_xmit_empty(struct usb_serial_port *port, 247 unsigned char fourth_char, unsigned char fifth_char); 248static void qt2_process_port_change(struct usb_serial_port *port, 249 unsigned char New_Current_Port); 250static void qt2_process_rcv_flush(struct usb_serial_port *port); 251static void qt2_process_xmit_flush(struct usb_serial_port *port); 252static void qt2_process_rx_char(struct usb_serial_port *port, 253 unsigned char data); 254static int qt2_box_get_register(struct usb_serial *serial, 255 unsigned char uart_number, unsigned short register_num, 256 __u8 *pValue); 257static int qt2_box_set_register(struct usb_serial *serial, 258 unsigned short Uart_Number, unsigned short Register_Num, 259 unsigned short Value); 260static int qt2_boxsetuart(struct usb_serial *serial, unsigned short Uart_Number, 261 unsigned short default_divisor, unsigned char default_LCR); 262static int qt2_boxsethw_flowctl(struct usb_serial *serial, 263 unsigned int UartNumber, bool bSet); 264static int qt2_boxsetsw_flowctl(struct usb_serial *serial, __u16 UartNumber, 265 unsigned char stop_char, unsigned char start_char); 266static int qt2_boxunsetsw_flowctl(struct usb_serial *serial, __u16 UartNumber); 267static int qt2_boxstoprx(struct usb_serial *serial, unsigned short uart_number, 268 unsigned short stop); 269 270/* implementation functions, roughly in order of use, are here */ 271static int qt2_calc_num_ports(struct usb_serial *serial) 272{ 273 int num_ports; 274 int flag_as_400; 275 switch (serial->dev->descriptor.idProduct) { 276 case QUATECH_SSU2_100: 277 num_ports = 1; 278 break; 279 280 case QUATECH_DSU2_400: 281 flag_as_400 = true; 282 case QUATECH_DSU2_100: 283 num_ports = 2; 284 break; 285 286 case QUATECH_QSU2_400: 287 flag_as_400 = true; 288 case QUATECH_QSU2_100: 289 num_ports = 4; 290 break; 291 292 case QUATECH_ESU2_400: 293 flag_as_400 = true; 294 case QUATECH_ESU2_100: 295 num_ports = 8; 296 break; 297 default: 298 num_ports = 1; 299 break; 300 } 301 return num_ports; 302} 303 304static int qt2_attach(struct usb_serial *serial) 305{ 306 struct usb_serial_port *port; 307 struct quatech2_port *qt2_port; /* port-specific private data pointer */ 308 struct quatech2_dev *qt2_dev; /* dev-specific private data pointer */ 309 int i; 310 /* stuff for storing endpoint addresses now */ 311 struct usb_endpoint_descriptor *endpoint; 312 struct usb_host_interface *iface_desc; 313 struct usb_serial_port *port0; /* first port structure on device */ 314 315 /* check how many endpoints there are on the device, for 316 * sanity's sake */ 317 dbg("%s(): Endpoints: %d bulk in, %d bulk out, %d interrupt in", 318 __func__, serial->num_bulk_in, 319 serial->num_bulk_out, serial->num_interrupt_in); 320 if ((serial->num_bulk_in != 1) || (serial->num_bulk_out != 1)) { 321 dbg("Device has wrong number of bulk endpoints!"); 322 return -ENODEV; 323 } 324 iface_desc = serial->interface->cur_altsetting; 325 326 /* Set up per-device private data, storing extra data alongside 327 * struct usb_serial */ 328 qt2_dev = kzalloc(sizeof(*qt2_dev), GFP_KERNEL); 329 if (!qt2_dev) { 330 dbg("%s: kmalloc for quatech2_dev failed!", 331 __func__); 332 return -ENOMEM; 333 } 334 qt2_dev->open_ports = 0; /* no ports open */ 335 qt2_set_dev_private(serial, qt2_dev); /* store private data */ 336 337 /* Now setup per port private data, which replaces all the things 338 * that quatech added to standard kernel structures in their driver */ 339 for (i = 0; i < serial->num_ports; i++) { 340 port = serial->port[i]; 341 qt2_port = kzalloc(sizeof(*qt2_port), GFP_KERNEL); 342 if (!qt2_port) { 343 dbg("%s: kmalloc for quatech2_port (%d) failed!.", 344 __func__, i); 345 return -ENOMEM; 346 } 347 /* initialise stuff in the structure */ 348 qt2_port->open_count = 0; /* port is not open */ 349 spin_lock_init(&qt2_port->lock); 350 mutex_init(&qt2_port->modelock); 351 qt2_set_port_private(port, qt2_port); 352 } 353 354 /* gain access to port[0]'s structure because we want to store 355 * device-level stuff in it */ 356 if (serial_paranoia_check(serial, __func__)) 357 return -ENODEV; 358 port0 = serial->port[0]; /* get the first port's device structure */ 359 360 /* print endpoint addresses so we can check them later 361 * by hand */ 362 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) { 363 endpoint = &iface_desc->endpoint[i].desc; 364 if ((endpoint->bEndpointAddress & 0x80) && 365 ((endpoint->bmAttributes & 3) == 0x02)) { 366 /* we found a bulk in endpoint */ 367 dbg("found bulk in at %#.2x", 368 endpoint->bEndpointAddress); 369 } 370 371 if (((endpoint->bEndpointAddress & 0x80) == 0x00) && 372 ((endpoint->bmAttributes & 3) == 0x02)) { 373 /* we found a bulk out endpoint */ 374 dbg("found bulk out at %#.2x", 375 endpoint->bEndpointAddress); 376 qt2_dev->buffer_size = endpoint->wMaxPacketSize; 377 /* max size of URB needs recording for the device */ 378 } 379 } /* end printing endpoint addresses */ 380 381 /* switch on power to the hardware */ 382 if (qt2_boxpoweron(serial) < 0) { 383 dbg("qt2_boxpoweron() failed"); 384 goto startup_error; 385 } 386 /* set all ports to RS232 mode */ 387 for (i = 0; i < serial->num_ports; ++i) { 388 if (qt2_boxsetQMCR(serial, i, QU2BOX232) < 0) { 389 dbg("qt2_boxsetQMCR() on port %d failed", 390 i); 391 goto startup_error; 392 } 393 } 394 395 return 0; 396 397startup_error: 398 for (i = 0; i < serial->num_ports; i++) { 399 port = serial->port[i]; 400 qt2_port = qt2_get_port_private(port); 401 kfree(qt2_port); 402 qt2_set_port_private(port, NULL); 403 } 404 qt2_dev = qt2_get_dev_private(serial); 405 kfree(qt2_dev); 406 qt2_set_dev_private(serial, NULL); 407 408 dbg("Exit fail %s\n", __func__); 409 return -EIO; 410} 411 412static void qt2_release(struct usb_serial *serial) 413{ 414 struct usb_serial_port *port; 415 struct quatech2_port *qt_port; 416 int i; 417 418 dbg("enterting %s", __func__); 419 420 for (i = 0; i < serial->num_ports; i++) { 421 port = serial->port[i]; 422 if (!port) 423 continue; 424 425 qt_port = usb_get_serial_port_data(port); 426 kfree(qt_port); 427 usb_set_serial_port_data(port, NULL); 428 } 429} 430/* This function is called once per serial port on the device, when 431 * that port is opened by a userspace application. 432 * The tty_struct and the usb_serial_port belong to this port, 433 * i.e. there are multiple ones for a multi-port device. 434 * However the usb_serial_port structure has a back-pointer 435 * to the parent usb_serial structure which belongs to the device, 436 * so we can access either the device-wide information or 437 * any other port's information (because there are also forward 438 * pointers) via that pointer. 439 * This is most helpful if the device shares resources (e.g. end 440 * points) between different ports 441 */ 442int qt2_open(struct tty_struct *tty, struct usb_serial_port *port) 443{ 444 struct usb_serial *serial; /* device structure */ 445 struct usb_serial_port *port0; /* first port structure on device */ 446 struct quatech2_port *port_extra; /* extra data for this port */ 447 struct quatech2_port *port0_extra; /* extra data for first port */ 448 struct quatech2_dev *dev_extra; /* extra data for the device */ 449 struct qt2_status_data ChannelData; 450 unsigned short default_divisor = QU2BOXSPD9600; 451 unsigned char default_LCR = QT2_SERIAL_8_DATA; 452 int status; 453 int result; 454 455 if (port_paranoia_check(port, __func__)) 456 return -ENODEV; 457 458 dbg("%s(): port %d", __func__, port->number); 459 460 serial = port->serial; /* get the parent device structure */ 461 if (serial_paranoia_check(serial, __func__)) { 462 dbg("usb_serial struct failed sanity check"); 463 return -ENODEV; 464 } 465 dev_extra = qt2_get_dev_private(serial); 466 /* get the device private data */ 467 if (dev_extra == NULL) { 468 dbg("device extra data pointer is null"); 469 return -ENODEV; 470 } 471 port0 = serial->port[0]; /* get the first port's device structure */ 472 if (port_paranoia_check(port0, __func__)) { 473 dbg("port0 usb_serial_port struct failed sanity check"); 474 return -ENODEV; 475 } 476 477 port_extra = qt2_get_port_private(port); 478 port0_extra = qt2_get_port_private(port0); 479 if (port_extra == NULL || port0_extra == NULL) { 480 dbg("failed to get private data for port or port0"); 481 return -ENODEV; 482 } 483 484 /* FIXME: are these needed? Does it even do anything useful? */ 485 /* get the modem and line status values from the UART */ 486 status = qt2_openboxchannel(serial, port->number, 487 &ChannelData); 488 if (status < 0) { 489 dbg("qt2_openboxchannel on channel %d failed", 490 port->number); 491 return status; 492 } 493 port_extra->shadowLSR = ChannelData.line_status & 494 (QT2_SERIAL_LSR_OE | QT2_SERIAL_LSR_PE | 495 QT2_SERIAL_LSR_FE | QT2_SERIAL_LSR_BI); 496 port_extra->shadowMSR = ChannelData.modem_status & 497 (QT2_SERIAL_MSR_CTS | QT2_SERIAL_MSR_DSR | 498 QT2_SERIAL_MSR_RI | QT2_SERIAL_MSR_CD); 499 500/* port_extra->fifo_empty_flag = true;*/ 501 dbg("qt2_openboxchannel on channel %d completed.", 502 port->number); 503 504 /* Set Baud rate to default and turn off flow control here */ 505 status = qt2_conf_uart(serial, port->number, default_divisor, 506 default_LCR); 507 if (status < 0) { 508 dbg("qt2_conf_uart() failed on channel %d", 509 port->number); 510 return status; 511 } 512 dbg("qt2_conf_uart() completed on channel %d", 513 port->number); 514 515 /* 516 * At this point we will need some end points to make further progress. 517 * Handlily, the correct endpoint addresses have been filled out into 518 * the usb_serial_port structure for us by the driver core, so we 519 * already have access to them. 520 * As there is only one bulk in and one bulk out end-point, these are in 521 * port[0]'s structure, and the rest are uninitialised. Handily, 522 * when we do a write to a port, we will use the same endpoint 523 * regardless of the port, with a 5-byte header added on to 524 * tell the box which port it should eventually come out of, so we only 525 * need the one set of endpoints. We will have one URB per port for 526 * writing, so that multiple ports can be writing at once. 527 * Finally we need a bulk in URB to use for background reads from the 528 * device, which will deal with uplink data from the box to host. 529 */ 530 dbg("port0 bulk in endpoint is %#.2x", port0->bulk_in_endpointAddress); 531 dbg("port0 bulk out endpoint is %#.2x", 532 port0->bulk_out_endpointAddress); 533 534 /* set up write_urb for bulk out transfers on this port. The USB 535 * serial framework will have allocated a blank URB, buffer etc for 536 * port0 when it put the endpoints there, but not for any of the other 537 * ports on the device because there are no more endpoints. Thus we 538 * have to allocate our own URBs for ports 1-7 539 */ 540 if (port->write_urb == NULL) { 541 dbg("port->write_urb == NULL, allocating one"); 542 port->write_urb = usb_alloc_urb(0, GFP_KERNEL); 543 if (!port->write_urb) { 544 err("Allocating write URB failed"); 545 return -ENOMEM; 546 } 547 /* buffer same size as port0 */ 548 port->bulk_out_size = dev_extra->buffer_size; 549 port->bulk_out_buffer = kmalloc(port->bulk_out_size, 550 GFP_KERNEL); 551 if (!port->bulk_out_buffer) { 552 err("Couldn't allocate bulk_out_buffer"); 553 return -ENOMEM; 554 } 555 } 556 if (serial->dev == NULL) 557 dbg("serial->dev == NULL"); 558 dbg("port->bulk_out_size is %d", port->bulk_out_size); 559 560 usb_fill_bulk_urb(port->write_urb, serial->dev, 561 usb_sndbulkpipe(serial->dev, 562 port0->bulk_out_endpointAddress), 563 port->bulk_out_buffer, 564 port->bulk_out_size, 565 qt2_write_bulk_callback, 566 port); 567 port_extra->tx_pending_bytes = 0; 568 569 if (dev_extra->open_ports == 0) { 570 /* this is first port to be opened, so need the read URB 571 * initialised for bulk in transfers (this is shared amongst 572 * all the ports on the device) */ 573 usb_fill_bulk_urb(port0->read_urb, serial->dev, 574 usb_rcvbulkpipe(serial->dev, 575 port0->bulk_in_endpointAddress), 576 port0->bulk_in_buffer, 577 port0->bulk_in_size, 578 qt2_read_bulk_callback, serial); 579 dbg("port0 bulk in URB initialised"); 580 581 /* submit URB, i.e. start reading from device (async) */ 582 dev_extra->ReadBulkStopped = false; 583 port_extra->read_urb_busy = true; 584 result = usb_submit_urb(port->read_urb, GFP_KERNEL); 585 if (result) { 586 dev_err(&port->dev, 587 "%s(): Error %d submitting bulk in urb", 588 __func__, result); 589 port_extra->read_urb_busy = false; 590 dev_extra->ReadBulkStopped = true; 591 } 592 593 /* When the first port is opened, initialise the value of 594 * current_port in dev_extra to this port, so it is set 595 * to something. Once the box sends data it will send the 596 * relevant escape sequences to get it to the right port anyway 597 */ 598 dev_extra->current_port = port; 599 } 600 601 /* initialize our wait queues */ 602 init_waitqueue_head(&port_extra->wait); 603 /* increment the count of openings of this port by one */ 604 port_extra->open_count++; 605 606 /* remember to store dev_extra, port_extra and port0_extra back again at 607 * end !*/ 608 qt2_set_port_private(port, port_extra); 609 qt2_set_port_private(serial->port[0], port0_extra); 610 qt2_set_dev_private(serial, dev_extra); 611 612 dev_extra->open_ports++; /* one more port opened */ 613 614 return 0; 615} 616 617/* called when a port is closed by userspace. It won't be called, however, 618 * until calls to chars_in_buffer() reveal that the port has completed 619 * sending buffered data, and there is nothing else to do. Thus we don't have 620 * to rely on forcing data through in this function. */ 621/* Setting close_pending should keep new data from being written out, 622 * once all the data in the enpoint buffers is moved out we won't get 623 * any more. */ 624/* BoxStopReceive would keep any more data from coming from a given 625 * port, but isn't called by the vendor driver, although their comments 626 * mention it. Should it be used here to stop the inbound data 627 * flow? 628 */ 629static void qt2_close(struct usb_serial_port *port) 630{ 631 /* time out value for flush loops */ 632 unsigned long jift; 633 struct quatech2_port *port_extra; /* extra data for this port */ 634 struct usb_serial *serial; /* device structure */ 635 struct quatech2_dev *dev_extra; /* extra data for the device */ 636 __u8 lsr_value = 0; /* value of Line Status Register */ 637 int status; /* result of last USB comms function */ 638 639 dbg("%s(): port %d", __func__, port->number); 640 serial = port->serial; /* get the parent device structure */ 641 dev_extra = qt2_get_dev_private(serial); 642 /* get the device private data */ 643 port_extra = qt2_get_port_private(port); /* port private data */ 644 645 /* we can now (and only now) stop reading data */ 646 port_extra->close_pending = true; 647 dbg("%s(): port_extra->close_pending = true", __func__); 648 /* although the USB side is now empty, the UART itself may 649 * still be pushing characters out over the line, so we have to 650 * wait testing the actual line status until the lines change 651 * indicating that the data is done transferring. */ 652 /* FIXME: slow this polling down so it doesn't run the USB bus flat out 653 * if it actually has to spend any time in this loop (which it normally 654 * doesn't because the buffer is nearly empty) */ 655 jift = jiffies + (10 * HZ); /* 10 sec timeout */ 656 do { 657 status = qt2_box_get_register(serial, port->number, 658 QT2_LINE_STATUS_REGISTER, &lsr_value); 659 if (status < 0) { 660 dbg("%s(): qt2_box_get_register failed", __func__); 661 break; 662 } 663 if ((lsr_value & QT2_LSR_TEMT)) { 664 dbg("UART done sending"); 665 break; 666 } 667 schedule(); 668 } while (jiffies <= jift); 669 670 status = qt2_closeboxchannel(serial, port->number); 671 if (status < 0) 672 dbg("%s(): port %d qt2_box_open_close_channel failed", 673 __func__, port->number); 674 /* to avoid leaking URBs, we should now free the write_urb for this 675 * port and set the pointer to null so that next time the port is opened 676 * a new URB is allocated. This avoids leaking URBs when the device is 677 * removed */ 678 usb_free_urb(port->write_urb); 679 kfree(port->bulk_out_buffer); 680 port->bulk_out_buffer = NULL; 681 port->bulk_out_size = 0; 682 683 /* decrement the count of openings of this port by one */ 684 port_extra->open_count--; 685 /* one less overall open as well */ 686 dev_extra->open_ports--; 687 dbg("%s(): Exit, dev_extra->open_ports = %d", __func__, 688 dev_extra->open_ports); 689} 690 691/** 692 * qt2_write - write bytes from the tty layer out to the USB device. 693 * @buf: The data to be written, size at least count. 694 * @count: The number of bytes requested for transmission. 695 * @return The number of bytes actually accepted for transmission to the device. 696 */ 697static int qt2_write(struct tty_struct *tty, struct usb_serial_port *port, 698 const unsigned char *buf, int count) 699{ 700 struct usb_serial *serial; /* parent device struct */ 701 __u8 header_array[5]; /* header used to direct writes to the correct 702 port on the device */ 703 struct quatech2_port *port_extra; /* extra data for this port */ 704 int result; 705 706 serial = port->serial; /* get the parent device of the port */ 707 port_extra = qt2_get_port_private(port); /* port extra info */ 708 if (serial == NULL) 709 return -ENODEV; 710 dbg("%s(): port %d, requested to write %d bytes, %d already pending", 711 __func__, port->number, count, port_extra->tx_pending_bytes); 712 713 if (count <= 0) { 714 dbg("%s(): write request of <= 0 bytes", __func__); 715 return 0; /* no bytes written */ 716 } 717 718 /* check if the write urb is already in use, i.e. data already being 719 * sent to this port */ 720 if ((port->write_urb->status == -EINPROGRESS)) { 721 /* Fifo hasn't been emptied since last write to this port */ 722 dbg("%s(): already writing, port->write_urb->status == " 723 "-EINPROGRESS", __func__); 724 /* schedule_work(&port->work); commented in vendor driver */ 725 return 0; 726 } else if (port_extra->tx_pending_bytes >= QT2_FIFO_DEPTH) { 727 /* buffer is full (==). > should not occur, but would indicate 728 * that an overflow had occurred */ 729 dbg("%s(): port transmit buffer is full!", __func__); 730 /* schedule_work(&port->work); commented in vendor driver */ 731 return 0; 732 } 733 734 /* We must fill the first 5 bytes of anything we sent with a transmit 735 * header which directes the data to the correct port. The maximum 736 * size we can send out in one URB is port->bulk_out_size, which caps 737 * the number of bytes of real data we can send in each write. As the 738 * semantics of write allow us to write less than we were give, we cap 739 * the maximum we will ever write to the device as 5 bytes less than 740 * one URB's worth, by reducing the value of the count argument 741 * appropriately*/ 742 if (count > port->bulk_out_size - QT2_TX_HEADER_LENGTH) { 743 count = port->bulk_out_size - QT2_TX_HEADER_LENGTH; 744 dbg("%s(): write request bigger than urb, only accepting " 745 "%d bytes", __func__, count); 746 } 747 /* we must also ensure that the FIFO at the other end can cope with the 748 * URB we send it, otherwise it will have problems. As above, we can 749 * restrict the write size by just shrinking count.*/ 750 if (count > (QT2_FIFO_DEPTH - port_extra->tx_pending_bytes)) { 751 count = QT2_FIFO_DEPTH - port_extra->tx_pending_bytes; 752 dbg("%s(): not enough room in buffer, only accepting %d bytes", 753 __func__, count); 754 } 755 /* now build the header for transmission */ 756 header_array[0] = 0x1b; 757 header_array[1] = 0x1b; 758 header_array[2] = (__u8)port->number; 759 header_array[3] = (__u8)count; 760 header_array[4] = (__u8)count >> 8; 761 /* copy header into URB */ 762 memcpy(port->write_urb->transfer_buffer, header_array, 763 QT2_TX_HEADER_LENGTH); 764 /* and actual data to write */ 765 memcpy(port->write_urb->transfer_buffer + 5, buf, count); 766 767 dbg("%s(): first data byte to send = %#.2x", __func__, *buf); 768 769 /* set up our urb */ 770 usb_fill_bulk_urb(port->write_urb, serial->dev, 771 usb_sndbulkpipe(serial->dev, 772 port->bulk_out_endpointAddress), 773 port->write_urb->transfer_buffer, count + 5, 774 (qt2_write_bulk_callback), port); 775 /* send the data out the bulk port */ 776 result = usb_submit_urb(port->write_urb, GFP_ATOMIC); 777 if (result) { 778 /* error couldn't submit urb */ 779 result = 0; /* return 0 as nothing got written */ 780 dbg("%s(): failed submitting write urb, error %d", 781 __func__, result); 782 } else { 783 port_extra->tx_pending_bytes += count; 784 result = count; /* return number of bytes written, i.e. count */ 785 dbg("%s(): submitted write urb, wrote %d bytes, " 786 "total pending bytes %d", 787 __func__, result, port_extra->tx_pending_bytes); 788 } 789 return result; 790} 791 792/* This is used by the next layer up to know how much space is available 793 * in the buffer on the device. It is used on a device closure to avoid 794 * calling close() until the buffer is reported to be empty. 795 * The returned value must never go down by more than the number of bytes 796 * written for correct behaviour further up the driver stack, i.e. if I call 797 * it, then write 6 bytes, then call again I should get 6 less, or possibly 798 * only 5 less if one was written in the meantime, etc. I should never get 7 799 * less (or any bigger number) because I only wrote 6 bytes. 800 */ 801static int qt2_write_room(struct tty_struct *tty) 802{ 803 struct usb_serial_port *port = tty->driver_data; 804 /* parent usb_serial_port pointer */ 805 struct quatech2_port *port_extra; /* extra data for this port */ 806 int room = 0; 807 port_extra = qt2_get_port_private(port); 808 809 if (port_extra->close_pending == true) { 810 dbg("%s(): port_extra->close_pending == true", __func__); 811 return -ENODEV; 812 } 813 /* Q: how many bytes would a write() call actually succeed in writing 814 * if it happened now? 815 * A: one QT2_FIFO_DEPTH, less the number of bytes waiting to be sent 816 * out of the port, unless this is more than the size of the 817 * write_urb output buffer less the header, which is the maximum 818 * size write we can do. 819 820 * Most of the implementation of this is done when writes to the device 821 * are started or terminate. When we send a write to the device, we 822 * reduce the free space count by the size of the dispatched write. 823 * When a "transmit empty" message comes back up the USB read stream, 824 * we decrement the count by the number of bytes reported sent, thus 825 * keeping track of the difference between sent and received bytes. 826 */ 827 828 room = (QT2_FIFO_DEPTH - port_extra->tx_pending_bytes); 829 /* space in FIFO */ 830 if (room > port->bulk_out_size - QT2_TX_HEADER_LENGTH) 831 room = port->bulk_out_size - QT2_TX_HEADER_LENGTH; 832 /* if more than the URB can hold, then cap to that limit */ 833 834 dbg("%s(): port %d: write room is %d", __func__, port->number, room); 835 return room; 836} 837 838static int qt2_chars_in_buffer(struct tty_struct *tty) 839{ 840 struct usb_serial_port *port = tty->driver_data; 841 /* parent usb_serial_port pointer */ 842 struct quatech2_port *port_extra; /* extra data for this port */ 843 port_extra = qt2_get_port_private(port); 844 845 dbg("%s(): port %d: chars_in_buffer = %d", __func__, 846 port->number, port_extra->tx_pending_bytes); 847 return port_extra->tx_pending_bytes; 848} 849 850/* called when userspace does an ioctl() on the device. Note that 851 * TIOCMGET and TIOCMSET are filtered off to their own methods before they get 852 * here, so we don't have to handle them. 853 */ 854static int qt2_ioctl(struct tty_struct *tty, 855 unsigned int cmd, unsigned long arg) 856{ 857 struct usb_serial_port *port = tty->driver_data; 858 struct usb_serial *serial = port->serial; 859 __u8 mcr_value; /* Modem Control Register value */ 860 __u8 msr_value; /* Modem Status Register value */ 861 unsigned short prev_msr_value; /* Previous value of Modem Status 862 * Register used to implement waiting for a line status change to 863 * occur */ 864 struct quatech2_port *port_extra; /* extra data for this port */ 865 DECLARE_WAITQUEUE(wait, current); 866 /* Declare a wait queue named "wait" */ 867 868 unsigned int value; 869 unsigned int UartNumber; 870 871 if (serial == NULL) 872 return -ENODEV; 873 UartNumber = tty->index - serial->minor; 874 port_extra = qt2_get_port_private(port); 875 876 dbg("%s(): port %d, UartNumber %d, tty =0x%p", __func__, 877 port->number, UartNumber, tty); 878 879 if (cmd == TIOCMBIS || cmd == TIOCMBIC) { 880 if (qt2_box_get_register(port->serial, UartNumber, 881 QT2_MODEM_CONTROL_REGISTER, &mcr_value) < 0) 882 return -ESPIPE; 883 if (copy_from_user(&value, (unsigned int *)arg, 884 sizeof(value))) 885 return -EFAULT; 886 887 switch (cmd) { 888 case TIOCMBIS: 889 if (value & TIOCM_RTS) 890 mcr_value |= QT2_SERIAL_MCR_RTS; 891 if (value & TIOCM_DTR) 892 mcr_value |= QT2_SERIAL_MCR_DTR; 893 if (value & TIOCM_LOOP) 894 mcr_value |= QT2_SERIAL_MCR_LOOP; 895 break; 896 case TIOCMBIC: 897 if (value & TIOCM_RTS) 898 mcr_value &= ~QT2_SERIAL_MCR_RTS; 899 if (value & TIOCM_DTR) 900 mcr_value &= ~QT2_SERIAL_MCR_DTR; 901 if (value & TIOCM_LOOP) 902 mcr_value &= ~QT2_SERIAL_MCR_LOOP; 903 break; 904 default: 905 break; 906 } /* end of local switch on cmd */ 907 if (qt2_box_set_register(port->serial, UartNumber, 908 QT2_MODEM_CONTROL_REGISTER, mcr_value) < 0) { 909 return -ESPIPE; 910 } else { 911 port_extra->shadowMCR = mcr_value; 912 return 0; 913 } 914 } else if (cmd == TIOCMIWAIT) { 915 dbg("%s() port %d, cmd == TIOCMIWAIT enter", 916 __func__, port->number); 917 prev_msr_value = port_extra->shadowMSR & QT2_SERIAL_MSR_MASK; 918 barrier(); 919 __set_current_state(TASK_INTERRUPTIBLE); 920 while (1) { 921 add_wait_queue(&port_extra->wait, &wait); 922 schedule(); 923 dbg("%s(): port %d, cmd == TIOCMIWAIT here\n", 924 __func__, port->number); 925 remove_wait_queue(&port_extra->wait, &wait); 926 /* see if a signal woke us up */ 927 if (signal_pending(current)) 928 return -ERESTARTSYS; 929 set_current_state(TASK_INTERRUPTIBLE); 930 msr_value = port_extra->shadowMSR & QT2_SERIAL_MSR_MASK; 931 if (msr_value == prev_msr_value) { 932 __set_current_state(TASK_RUNNING); 933 return -EIO; /* no change - error */ 934 } 935 if ((arg & TIOCM_RNG && 936 ((prev_msr_value & QT2_SERIAL_MSR_RI) == 937 (msr_value & QT2_SERIAL_MSR_RI))) || 938 (arg & TIOCM_DSR && 939 ((prev_msr_value & QT2_SERIAL_MSR_DSR) == 940 (msr_value & QT2_SERIAL_MSR_DSR))) || 941 (arg & TIOCM_CD && 942 ((prev_msr_value & QT2_SERIAL_MSR_CD) == 943 (msr_value & QT2_SERIAL_MSR_CD))) || 944 (arg & TIOCM_CTS && 945 ((prev_msr_value & QT2_SERIAL_MSR_CTS) == 946 (msr_value & QT2_SERIAL_MSR_CTS)))) { 947 __set_current_state(TASK_RUNNING); 948 return 0; 949 } 950 } /* end inifinite while */ 951 /* FIXME: This while loop needs a way to break out if the device 952 * is disconnected while a process is waiting for the MSR to 953 * change, because once it's disconnected, it isn't going to 954 * change state ... */ 955 } else { 956 /* any other ioctls we don't know about come here */ 957 dbg("%s(): No ioctl for that one. port = %d", __func__, 958 port->number); 959 return -ENOIOCTLCMD; 960 } 961} 962 963/* Called when the user wishes to change the port settings using the termios 964 * userspace interface */ 965static void qt2_set_termios(struct tty_struct *tty, 966 struct usb_serial_port *port, struct ktermios *old_termios) 967{ 968 struct usb_serial *serial; /* parent serial device */ 969 int baud, divisor, remainder; 970 unsigned char LCR_change_to = 0; 971 int status; 972 __u16 UartNumber; 973 974 dbg("%s(): port %d", __func__, port->number); 975 976 serial = port->serial; 977 978 UartNumber = port->number; 979 980 if (old_termios && !tty_termios_hw_change(old_termios, tty->termios)) 981 return; 982 983 switch (tty->termios->c_cflag) { 984 case CS5: 985 LCR_change_to |= QT2_SERIAL_5_DATA; 986 break; 987 case CS6: 988 LCR_change_to |= QT2_SERIAL_6_DATA; 989 break; 990 case CS7: 991 LCR_change_to |= QT2_SERIAL_7_DATA; 992 break; 993 default: 994 case CS8: 995 LCR_change_to |= QT2_SERIAL_8_DATA; 996 break; 997 } 998 999 /* Parity stuff */ 1000 if (tty->termios->c_cflag & PARENB) { 1001 if (tty->termios->c_cflag & PARODD) 1002 LCR_change_to |= QT2_SERIAL_ODD_PARITY; 1003 else 1004 LCR_change_to |= QT2_SERIAL_EVEN_PARITY; 1005 } 1006 /* Because LCR_change_to is initialised to zero, we don't have to worry 1007 * about the case where PARENB is not set or clearing bits, because by 1008 * default all of them are cleared, turning parity off. 1009 * as we don't support mark/space parity, we should clear the 1010 * mark/space parity bit in c_cflag, so the caller can tell we have 1011 * ignored the request */ 1012 tty->termios->c_cflag &= ~CMSPAR; 1013 1014 if (tty->termios->c_cflag & CSTOPB) 1015 LCR_change_to |= QT2_SERIAL_TWO_STOPB; 1016 else 1017 LCR_change_to |= QT2_SERIAL_ONE_STOPB; 1018 1019 /* Thats the LCR stuff, next we need to work out the divisor as the 1020 * LCR and the divisor are set together */ 1021 baud = tty_get_baud_rate(tty); 1022 if (!baud) { 1023 /* pick a default, any default... */ 1024 baud = 9600; 1025 } 1026 dbg("%s(): got baud = %d", __func__, baud); 1027 1028 divisor = QT2_MAX_BAUD_RATE / baud; 1029 remainder = QT2_MAX_BAUD_RATE % baud; 1030 /* Round to nearest divisor */ 1031 if (((remainder * 2) >= baud) && (baud != 110)) 1032 divisor++; 1033 dbg("%s(): setting divisor = %d, QT2_MAX_BAUD_RATE = %d , LCR = %#.2x", 1034 __func__, divisor, QT2_MAX_BAUD_RATE, LCR_change_to); 1035 1036 status = qt2_boxsetuart(serial, UartNumber, (unsigned short) divisor, 1037 LCR_change_to); 1038 if (status < 0) { 1039 dbg("qt2_boxsetuart() failed"); 1040 return; 1041 } else { 1042 /* now encode the baud rate we actually set, which may be 1043 * different to the request */ 1044 baud = QT2_MAX_BAUD_RATE / divisor; 1045 tty_encode_baud_rate(tty, baud, baud); 1046 } 1047 1048 /* Now determine flow control */ 1049 if (tty->termios->c_cflag & CRTSCTS) { 1050 dbg("%s(): Enabling HW flow control port %d", __func__, 1051 port->number); 1052 /* Enable RTS/CTS flow control */ 1053 status = qt2_boxsethw_flowctl(serial, UartNumber, true); 1054 if (status < 0) { 1055 dbg("qt2_boxsethw_flowctl() failed"); 1056 return; 1057 } 1058 } else { 1059 /* Disable RTS/CTS flow control */ 1060 dbg("%s(): disabling HW flow control port %d", __func__, 1061 port->number); 1062 status = qt2_boxsethw_flowctl(serial, UartNumber, false); 1063 if (status < 0) { 1064 dbg("qt2_boxsethw_flowctl failed"); 1065 return; 1066 } 1067 } 1068 /* if we are implementing XON/XOFF, set the start and stop character 1069 * in the device */ 1070 if (I_IXOFF(tty) || I_IXON(tty)) { 1071 unsigned char stop_char = STOP_CHAR(tty); 1072 unsigned char start_char = START_CHAR(tty); 1073 status = qt2_boxsetsw_flowctl(serial, UartNumber, stop_char, 1074 start_char); 1075 if (status < 0) 1076 dbg("qt2_boxsetsw_flowctl (enabled) failed"); 1077 } else { 1078 /* disable SW flow control */ 1079 status = qt2_boxunsetsw_flowctl(serial, UartNumber); 1080 if (status < 0) 1081 dbg("qt2_boxunsetsw_flowctl (disabling) failed"); 1082 } 1083} 1084 1085static int qt2_tiocmget(struct tty_struct *tty) 1086{ 1087 struct usb_serial_port *port = tty->driver_data; 1088 struct usb_serial *serial = port->serial; 1089 1090 __u8 mcr_value; /* Modem Control Register value */ 1091 __u8 msr_value; /* Modem Status Register value */ 1092 unsigned int result = 0; 1093 int status; 1094 unsigned int UartNumber; 1095 1096 if (serial == NULL) 1097 return -ENODEV; 1098 1099 dbg("%s(): port %d, tty =0x%p", __func__, port->number, tty); 1100 UartNumber = tty->index - serial->minor; 1101 dbg("UartNumber is %d", UartNumber); 1102 1103 status = qt2_box_get_register(port->serial, UartNumber, 1104 QT2_MODEM_CONTROL_REGISTER, &mcr_value); 1105 if (status >= 0) { 1106 status = qt2_box_get_register(port->serial, UartNumber, 1107 QT2_MODEM_STATUS_REGISTER, &msr_value); 1108 } 1109 if (status >= 0) { 1110 result = ((mcr_value & QT2_SERIAL_MCR_DTR) ? TIOCM_DTR : 0) 1111 /*DTR set */ 1112 | ((mcr_value & QT2_SERIAL_MCR_RTS) ? TIOCM_RTS : 0) 1113 /*RTS set */ 1114 | ((msr_value & QT2_SERIAL_MSR_CTS) ? TIOCM_CTS : 0) 1115 /* CTS set */ 1116 | ((msr_value & QT2_SERIAL_MSR_CD) ? TIOCM_CAR : 0) 1117 /*Carrier detect set */ 1118 | ((msr_value & QT2_SERIAL_MSR_RI) ? TIOCM_RI : 0) 1119 /* Ring indicator set */ 1120 | ((msr_value & QT2_SERIAL_MSR_DSR) ? TIOCM_DSR : 0); 1121 /* DSR set */ 1122 return result; 1123 } else { 1124 return -ESPIPE; 1125 } 1126} 1127 1128static int qt2_tiocmset(struct tty_struct *tty, 1129 unsigned int set, unsigned int clear) 1130{ 1131 struct usb_serial_port *port = tty->driver_data; 1132 struct usb_serial *serial = port->serial; 1133 __u8 mcr_value; /* Modem Control Register value */ 1134 int status; 1135 unsigned int UartNumber; 1136 1137 if (serial == NULL) 1138 return -ENODEV; 1139 1140 UartNumber = tty->index - serial->minor; 1141 dbg("%s(): port %d, UartNumber %d", __func__, port->number, UartNumber); 1142 1143 status = qt2_box_get_register(port->serial, UartNumber, 1144 QT2_MODEM_CONTROL_REGISTER, &mcr_value); 1145 if (status < 0) 1146 return -ESPIPE; 1147 1148 /* Turn off RTS, DTR and loopback, then only turn on what was asked 1149 * for */ 1150 mcr_value &= ~(QT2_SERIAL_MCR_RTS | QT2_SERIAL_MCR_DTR | 1151 QT2_SERIAL_MCR_LOOP); 1152 if (set & TIOCM_RTS) 1153 mcr_value |= QT2_SERIAL_MCR_RTS; 1154 if (set & TIOCM_DTR) 1155 mcr_value |= QT2_SERIAL_MCR_DTR; 1156 if (set & TIOCM_LOOP) 1157 mcr_value |= QT2_SERIAL_MCR_LOOP; 1158 1159 status = qt2_box_set_register(port->serial, UartNumber, 1160 QT2_MODEM_CONTROL_REGISTER, mcr_value); 1161 if (status < 0) 1162 return -ESPIPE; 1163 else 1164 return 0; 1165} 1166 1167/** qt2_break - Turn BREAK on and off on the UARTs 1168 */ 1169static void qt2_break(struct tty_struct *tty, int break_state) 1170{ 1171 struct usb_serial_port *port = tty->driver_data; /* parent port */ 1172 struct usb_serial *serial = port->serial; /* parent device */ 1173 struct quatech2_port *port_extra; /* extra data for this port */ 1174 __u16 break_value; 1175 unsigned int result; 1176 1177 port_extra = qt2_get_port_private(port); 1178 if (!serial) { 1179 dbg("%s(): port %d: no serial object", __func__, port->number); 1180 return; 1181 } 1182 1183 if (break_state == -1) 1184 break_value = 1; 1185 else 1186 break_value = 0; 1187 dbg("%s(): port %d, break_value %d", __func__, port->number, 1188 break_value); 1189 1190 mutex_lock(&port_extra->modelock); 1191 if (!port_extra->open_count) { 1192 dbg("%s(): port not open", __func__); 1193 goto exit; 1194 } 1195 1196 result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), 1197 QT2_BREAK_CONTROL, 0x40, break_value, 1198 port->number, NULL, 0, 300); 1199exit: 1200 mutex_unlock(&port_extra->modelock); 1201 dbg("%s(): exit port %d", __func__, port->number); 1202 1203} 1204/** 1205 * qt2_throttle: - stop reading new data from the port 1206 */ 1207static void qt2_throttle(struct tty_struct *tty) 1208{ 1209 struct usb_serial_port *port = tty->driver_data; 1210 struct usb_serial *serial = port->serial; 1211 struct quatech2_port *port_extra; /* extra data for this port */ 1212 dbg("%s(): port %d", __func__, port->number); 1213 1214 port_extra = qt2_get_port_private(port); 1215 if (!serial) { 1216 dbg("%s(): enter port %d no serial object", __func__, 1217 port->number); 1218 return; 1219 } 1220 1221 mutex_lock(&port_extra->modelock); /* lock structure */ 1222 if (!port_extra->open_count) { 1223 dbg("%s(): port not open", __func__); 1224 goto exit; 1225 } 1226 /* Send command to box to stop receiving stuff. This will stop this 1227 * particular UART from filling the endpoint - in the multiport case the 1228 * FPGA UART will handle any flow control implemented, but for the single 1229 * port it's handed differently and we just quit submitting urbs 1230 */ 1231 if (serial->dev->descriptor.idProduct != QUATECH_SSU2_100) 1232 qt2_boxstoprx(serial, port->number, 1); 1233 1234 port->throttled = 1; 1235exit: 1236 mutex_unlock(&port_extra->modelock); 1237 dbg("%s(): port %d: setting port->throttled", __func__, port->number); 1238 return; 1239} 1240 1241/** 1242 * qt2_unthrottle: - start receiving data through the port again after being 1243 * throttled 1244 */ 1245static void qt2_unthrottle(struct tty_struct *tty) 1246{ 1247 struct usb_serial_port *port = tty->driver_data; 1248 struct usb_serial *serial = port->serial; 1249 struct quatech2_port *port_extra; /* extra data for this port */ 1250 struct usb_serial_port *port0; /* first port structure on device */ 1251 struct quatech2_dev *dev_extra; /* extra data for the device */ 1252 1253 if (!serial) { 1254 dbg("%s() enter port %d no serial object!", __func__, 1255 port->number); 1256 return; 1257 } 1258 dbg("%s(): enter port %d", __func__, port->number); 1259 dev_extra = qt2_get_dev_private(serial); 1260 port_extra = qt2_get_port_private(port); 1261 port0 = serial->port[0]; /* get the first port's device structure */ 1262 1263 mutex_lock(&port_extra->modelock); 1264 if (!port_extra->open_count) { 1265 dbg("%s(): port %d not open", __func__, port->number); 1266 goto exit; 1267 } 1268 1269 if (port->throttled != 0) { 1270 dbg("%s(): port %d: unsetting port->throttled", __func__, 1271 port->number); 1272 port->throttled = 0; 1273 /* Send command to box to start receiving stuff */ 1274 if (serial->dev->descriptor.idProduct != QUATECH_SSU2_100) { 1275 qt2_boxstoprx(serial, port->number, 0); 1276 } else if (dev_extra->ReadBulkStopped == true) { 1277 usb_fill_bulk_urb(port0->read_urb, serial->dev, 1278 usb_rcvbulkpipe(serial->dev, 1279 port0->bulk_in_endpointAddress), 1280 port0->bulk_in_buffer, 1281 port0->bulk_in_size, 1282 qt2_read_bulk_callback, 1283 serial); 1284 } 1285 } 1286exit: 1287 mutex_unlock(&port_extra->modelock); 1288 dbg("%s(): exit port %d", __func__, port->number); 1289 return; 1290} 1291 1292/* internal, private helper functions for the driver */ 1293 1294/* Power up the FPGA in the box to get it working */ 1295static int qt2_boxpoweron(struct usb_serial *serial) 1296{ 1297 int result; 1298 __u8 Direcion; 1299 unsigned int pipe; 1300 Direcion = USBD_TRANSFER_DIRECTION_OUT; 1301 pipe = usb_rcvctrlpipe(serial->dev, 0); 1302 result = usb_control_msg(serial->dev, pipe, QT_SET_GET_DEVICE, 1303 Direcion, QU2BOXPWRON, 0x00, NULL, 0x00, 1304 5000); 1305 return result; 1306} 1307 1308/* 1309 * qt2_boxsetQMCR Issue a QT2_GET_SET_QMCR vendor-spcific request on the 1310 * default control pipe. If successful return the number of bytes written, 1311 * otherwise return a negative error number of the problem. 1312 */ 1313static int qt2_boxsetQMCR(struct usb_serial *serial, __u16 Uart_Number, 1314 __u8 QMCR_Value) 1315{ 1316 int result; 1317 __u16 PortSettings; 1318 1319 PortSettings = (__u16)(QMCR_Value); 1320 1321 dbg("%s(): Port = %d, PortSettings = 0x%x", __func__, 1322 Uart_Number, PortSettings); 1323 1324 result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), 1325 QT2_GET_SET_QMCR, 0x40, PortSettings, 1326 (__u16)Uart_Number, NULL, 0, 5000); 1327 return result; 1328} 1329 1330static int port_paranoia_check(struct usb_serial_port *port, 1331 const char *function) 1332{ 1333 if (!port) { 1334 dbg("%s - port == NULL", function); 1335 return -1; 1336 } 1337 if (!port->serial) { 1338 dbg("%s - port->serial == NULL\n", function); 1339 return -1; 1340 } 1341 return 0; 1342} 1343 1344static int serial_paranoia_check(struct usb_serial *serial, 1345 const char *function) 1346{ 1347 if (!serial) { 1348 dbg("%s - serial == NULL\n", function); 1349 return -1; 1350 } 1351 1352 if (!serial->type) { 1353 dbg("%s - serial->type == NULL!", function); 1354 return -1; 1355 } 1356 1357 return 0; 1358} 1359 1360static inline struct quatech2_port *qt2_get_port_private(struct usb_serial_port 1361 *port) 1362{ 1363 return (struct quatech2_port *)usb_get_serial_port_data(port); 1364} 1365 1366static inline void qt2_set_port_private(struct usb_serial_port *port, 1367 struct quatech2_port *data) 1368{ 1369 usb_set_serial_port_data(port, (void *)data); 1370} 1371 1372static inline struct quatech2_dev *qt2_get_dev_private(struct usb_serial 1373 *serial) 1374{ 1375 return (struct quatech2_dev *)usb_get_serial_data(serial); 1376} 1377static inline void qt2_set_dev_private(struct usb_serial *serial, 1378 struct quatech2_dev *data) 1379{ 1380 usb_set_serial_data(serial, (void *)data); 1381} 1382 1383static int qt2_openboxchannel(struct usb_serial *serial, __u16 1384 Uart_Number, struct qt2_status_data *status) 1385{ 1386 int result; 1387 __u16 length; 1388 __u8 Direcion; 1389 unsigned int pipe; 1390 length = sizeof(struct qt2_status_data); 1391 Direcion = USBD_TRANSFER_DIRECTION_IN; 1392 pipe = usb_rcvctrlpipe(serial->dev, 0); 1393 result = usb_control_msg(serial->dev, pipe, QT_OPEN_CLOSE_CHANNEL, 1394 Direcion, 0x00, Uart_Number, status, length, 5000); 1395 return result; 1396} 1397static int qt2_closeboxchannel(struct usb_serial *serial, __u16 Uart_Number) 1398{ 1399 int result; 1400 __u8 direcion; 1401 unsigned int pipe; 1402 direcion = USBD_TRANSFER_DIRECTION_OUT; 1403 pipe = usb_sndctrlpipe(serial->dev, 0); 1404 result = usb_control_msg(serial->dev, pipe, QT_OPEN_CLOSE_CHANNEL, 1405 direcion, 0, Uart_Number, NULL, 0, 5000); 1406 return result; 1407} 1408 1409/* qt2_conf_uart Issue a SET_UART vendor-spcific request on the default 1410 * control pipe. If successful sets baud rate divisor and LCR value 1411 */ 1412static int qt2_conf_uart(struct usb_serial *serial, unsigned short Uart_Number, 1413 unsigned short divisor, unsigned char LCR) 1414{ 1415 int result; 1416 unsigned short UartNumandLCR; 1417 1418 UartNumandLCR = (LCR << 8) + Uart_Number; 1419 1420 result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), 1421 QT2_GET_SET_UART, 0x40, divisor, UartNumandLCR, 1422 NULL, 0, 300); 1423 return result; 1424} 1425 1426/** @brief Callback for asynchronous submission of read URBs on bulk in 1427 * endpoints 1428 * 1429 * Registered in qt2_open_port(), used to deal with incomming data 1430 * from the box. 1431 */ 1432static void qt2_read_bulk_callback(struct urb *urb) 1433{ 1434 /* Get the device pointer (struct usb_serial) back out of the URB */ 1435 struct usb_serial *serial = urb->context; 1436 /* get the extra struct for the device */ 1437 struct quatech2_dev *dev_extra = qt2_get_dev_private(serial); 1438 /* Get first port structure from the device */ 1439 struct usb_serial_port *port0 = serial->port[0]; 1440 /* Get the currently active port structure from serial struct */ 1441 struct usb_serial_port *active = dev_extra->current_port; 1442 /* get the extra struct for port 0 */ 1443 struct quatech2_port *port0_extra = qt2_get_port_private(port0); 1444 /* and for the currently active port */ 1445 struct quatech2_port *active_extra = qt2_get_port_private(active); 1446 /* When we finally get to doing some tty stuff, we will need this */ 1447 struct tty_struct *tty_st; 1448 unsigned int RxCount; /* the length of the data to process */ 1449 unsigned int i; /* loop counter over the data to process */ 1450 int result; /* return value cache variable */ 1451 bool escapeflag; /* flag set to true if this loop iteration is 1452 * parsing an escape sequence, rather than 1453 * ordinary data */ 1454 dbg("%s(): callback running, active port is %d", __func__, 1455 active->number); 1456 1457 if (urb->status) { 1458 /* read didn't go well */ 1459 dev_extra->ReadBulkStopped = true; 1460 dbg("%s(): nonzero bulk read status received: %d", 1461 __func__, urb->status); 1462 return; 1463 } 1464 1465 /* inline port_sofrint() here */ 1466 if (port_paranoia_check(port0, __func__) != 0) { 1467 dbg("%s - port_paranoia_check on port0 failed, exiting\n", 1468__func__); 1469 return; 1470 } 1471 if (port_paranoia_check(active, __func__) != 0) { 1472 dbg("%s - port_paranoia_check on current_port " 1473 "failed, exiting", __func__); 1474 return; 1475 } 1476 1477/* This single callback function has to do for all the ports on 1478 * the device. Data being read up the USB can contain certain 1479 * escape sequences which are used to communicate out-of-band 1480 * information from the serial port in-band over the USB. 1481 * These escapes include sending modem and flow control line 1482 * status, and switching the port. The concept of a "Current Port" 1483 * is used, which is where data is going until a port change 1484 * escape seqence is received. This Current Port is kept between 1485 * callbacks so that when this function enters we know which the 1486 * currently active port is and can get to work right away without 1487 * the box having to send repeat escape sequences (anyway, how 1488 * would it know to do so?). 1489 */ 1490 1491 if (active_extra->close_pending == true) { 1492 /* We are closing , stop reading */ 1493 dbg("%s - (active->close_pending == true", __func__); 1494 if (dev_extra->open_ports <= 0) { 1495 /* If this is the only port left open - stop the 1496 * bulk read */ 1497 dev_extra->ReadBulkStopped = true; 1498 dbg("%s - (ReadBulkStopped == true;", __func__); 1499 return; 1500 } 1501 } 1502 1503 /* 1504 * RxHolding is asserted by throttle, if we assert it, we're not 1505 * receiving any more characters and let the box handle the flow 1506 * control 1507 */ 1508 if ((port0_extra->RxHolding == true) && 1509 (serial->dev->descriptor.idProduct == QUATECH_SSU2_100)) { 1510 /* single port device, input is already stopped, so we don't 1511 * need any more input data */ 1512 dev_extra->ReadBulkStopped = true; 1513 return; 1514 } 1515 /* finally, we are in a situation where we might consider the data 1516 * that is contained within the URB, and what to do about it. 1517 * This is likely to involved communicating up to the TTY layer, so 1518 * we will need to get hold of the tty for the port we are currently 1519 * dealing with */ 1520 1521 /* active is a usb_serial_port. It has a member port which is a 1522 * tty_port. From this we get a tty_struct pointer which is what we 1523 * actually wanted, and keep it on tty_st */ 1524 tty_st = tty_port_tty_get(&active->port); 1525 if (!tty_st) { 1526 dbg("%s - bad tty pointer - exiting", __func__); 1527 return; 1528 } 1529 RxCount = urb->actual_length; /* grab length of data handy */ 1530 1531 if (RxCount) { 1532 /* skip all this if no data to process */ 1533 for (i = 0; i < RxCount ; ++i) { 1534 /* Look ahead code here -works on several bytes at onc*/ 1535 if ((i <= (RxCount - 3)) && (THISCHAR == 0x1b) 1536 && (NEXTCHAR == 0x1b)) { 1537 /* we are in an escape sequence, type 1538 * determined by the 3rd char */ 1539 escapeflag = false; 1540 switch (THIRDCHAR) { 1541 case 0x00: 1542 /* Line status change 4th byte must 1543 * follow */ 1544 if (i > (RxCount - 4)) { 1545 dbg("Illegal escape sequences " 1546 "in received data"); 1547 break; 1548 } 1549 qt2_process_line_status(active, 1550 FOURTHCHAR); 1551 i += 3; 1552 escapeflag = true; 1553 break; 1554 case 0x01: 1555 /* Modem status status change 4th byte 1556 * must follow */ 1557 if (i > (RxCount - 4)) { 1558 dbg("Illegal escape sequences " 1559 "in received data"); 1560 break; 1561 } 1562 qt2_process_modem_status(active, 1563 FOURTHCHAR); 1564 i += 3; 1565 escapeflag = true; 1566 break; 1567 case 0x02: 1568 /* xmit hold empty 4th byte 1569 * must follow */ 1570 if (i > (RxCount - 4)) { 1571 dbg("Illegal escape sequences " 1572 "in received data"); 1573 break; 1574 } 1575 qt2_process_xmit_empty(active, 1576 FOURTHCHAR, FIFTHCHAR); 1577 i += 4; 1578 escapeflag = true; 1579 break; 1580 case 0x03: 1581 /* Port number change 4th byte 1582 * must follow */ 1583 if (i > (RxCount - 4)) { 1584 dbg("Illegal escape sequences " 1585 "in received data"); 1586 break; 1587 } 1588 /* Port change. If port open push 1589 * current data up to tty layer */ 1590 if (active_extra->open_count > 0) 1591 tty_flip_buffer_push(tty_st); 1592 1593 dbg("Port Change: new port = %d", 1594 FOURTHCHAR); 1595 qt2_process_port_change(active, 1596 FOURTHCHAR); 1597 i += 3; 1598 escapeflag = true; 1599 /* having changed port, the pointers for 1600 * the currently active port are all out 1601 * of date and need updating */ 1602 active = dev_extra->current_port; 1603 active_extra = 1604 qt2_get_port_private(active); 1605 tty_st = tty_port_tty_get( 1606 &active->port); 1607 break; 1608 case 0x04: 1609 /* Recv flush 3rd byte must 1610 * follow */ 1611 if (i > (RxCount - 3)) { 1612 dbg("Illegal escape sequences " 1613 "in received data"); 1614 break; 1615 } 1616 qt2_process_rcv_flush(active); 1617 i += 2; 1618 escapeflag = true; 1619 break; 1620 case 0x05: 1621 /* xmit flush 3rd byte must follow */ 1622 if (i > (RxCount - 3)) { 1623 dbg("Illegal escape sequences " 1624 "in received data"); 1625 break; 1626 } 1627 qt2_process_xmit_flush(active); 1628 i += 2; 1629 escapeflag = true; 1630 break; 1631 case 0xff: 1632 dbg("No status sequence"); 1633 qt2_process_rx_char(active, THISCHAR); 1634 qt2_process_rx_char(active, NEXTCHAR); 1635 i += 2; 1636 break; 1637 default: 1638 qt2_process_rx_char(active, THISCHAR); 1639 i += 1; 1640 break; 1641 } /*end switch*/ 1642 if (escapeflag == true) 1643 continue; 1644 /* if we did an escape char, we don't need 1645 * to mess around pushing data through the 1646 * tty layer, and can go round again */ 1647 } /*endif*/ 1648 if (tty_st && urb->actual_length) { 1649 tty_buffer_request_room(tty_st, 1); 1650 tty_insert_flip_string(tty_st, &( 1651 (unsigned char *) 1652 (urb->transfer_buffer) 1653 )[i], 1); 1654 } 1655 } /*endfor*/ 1656 tty_flip_buffer_push(tty_st); 1657 } /*endif*/ 1658 1659 /* at this point we have complete dealing with the data for this 1660 * callback. All we have to do now is to start the async read process 1661 * back off again. */ 1662 1663 usb_fill_bulk_urb(port0->read_urb, serial->dev, 1664 usb_rcvbulkpipe(serial->dev, port0->bulk_in_endpointAddress), 1665 port0->bulk_in_buffer, port0->bulk_in_size, 1666 qt2_read_bulk_callback, serial); 1667 result = usb_submit_urb(port0->read_urb, GFP_ATOMIC); 1668 if (result) { 1669 dbg("%s(): failed resubmitting read urb, error %d", 1670 __func__, result); 1671 } else { 1672 dbg("%s() successfully resubmitted read urb", __func__); 1673 if (tty_st && RxCount) { 1674 /* if some inbound data was processed, then 1675 * we need to push that through the tty layer 1676 */ 1677 tty_flip_buffer_push(tty_st); 1678 tty_schedule_flip(tty_st); 1679 } 1680 } 1681 1682 /* cribbed from serqt_usb2 driver, but not sure which work needs 1683 * scheduling - port0 or currently active port? */ 1684 /* schedule_work(&port->work); */ 1685 dbg("%s() completed", __func__); 1686 return; 1687} 1688 1689/** @brief Callback for asynchronous submission of write URBs on bulk in 1690 * endpoints 1691 * 1692 * Registered in qt2_write(), used to deal with outgoing data 1693 * to the box. 1694 */ 1695static void qt2_write_bulk_callback(struct urb *urb) 1696{ 1697 struct usb_serial_port *port = (struct usb_serial_port *)urb->context; 1698 struct usb_serial *serial = port->serial; 1699 dbg("%s(): port %d", __func__, port->number); 1700 if (!serial) { 1701 dbg("%s(): bad serial pointer, exiting", __func__); 1702 return; 1703 } 1704 if (urb->status) { 1705 dbg("%s(): nonzero write bulk status received: %d", 1706 __func__, urb->status); 1707 return; 1708 } 1709 /* FIXME What is supposed to be going on here? 1710 * does this actually do anything useful, and should it? 1711 */ 1712 /*port_softint((void *) serial); commented in vendor driver */ 1713 schedule_work(&port->work); 1714 dbg("%s(): port %d exit", __func__, port->number); 1715 return; 1716} 1717 1718static void qt2_process_line_status(struct usb_serial_port *port, 1719 unsigned char LineStatus) 1720{ 1721 /* obtain the private structure for the port */ 1722 struct quatech2_port *port_extra = qt2_get_port_private(port); 1723 port_extra->shadowLSR = LineStatus & (QT2_SERIAL_LSR_OE | 1724 QT2_SERIAL_LSR_PE | QT2_SERIAL_LSR_FE | QT2_SERIAL_LSR_BI); 1725} 1726static void qt2_process_modem_status(struct usb_serial_port *port, 1727 unsigned char ModemStatus) 1728{ 1729 /* obtain the private structure for the port */ 1730 struct quatech2_port *port_extra = qt2_get_port_private(port); 1731 port_extra->shadowMSR = ModemStatus; 1732 wake_up_interruptible(&port_extra->wait); 1733 /* this wakes up the otherwise indefinitely waiting code for 1734 * the TIOCMIWAIT ioctl, so that it can notice that 1735 * port_extra->shadowMSR has changed and the ioctl needs to return. 1736 */ 1737} 1738 1739static void qt2_process_xmit_empty(struct usb_serial_port *port, 1740 unsigned char fourth_char, unsigned char fifth_char) 1741{ 1742 int byte_count; 1743 /* obtain the private structure for the port */ 1744 struct quatech2_port *port_extra = qt2_get_port_private(port); 1745 1746 byte_count = (int)(fifth_char * 16); 1747 byte_count += (int)fourth_char; 1748 /* byte_count indicates how many bytes the device has written out. This 1749 * message appears to occur regularly, and is used in the vendor driver 1750 * to keep track of the fill state of the port transmit buffer */ 1751 port_extra->tx_pending_bytes -= byte_count; 1752 /* reduce the stored data queue length by the known number of bytes 1753 * sent */ 1754 dbg("port %d: %d bytes reported sent, %d still pending", port->number, 1755 byte_count, port_extra->tx_pending_bytes); 1756 1757 /*port_extra->xmit_fifo_room_bytes = FIFO_DEPTH; ???*/ 1758} 1759 1760static void qt2_process_port_change(struct usb_serial_port *port, 1761 unsigned char New_Current_Port) 1762{ 1763 /* obtain the parent usb serial device structure */ 1764 struct usb_serial *serial = port->serial; 1765 /* obtain the private structure for the device */ 1766 struct quatech2_dev *dev_extra = qt2_get_dev_private(serial); 1767 dev_extra->current_port = serial->port[New_Current_Port]; 1768 /* what should I do with this? commented out in upstream 1769 * driver */ 1770 /*schedule_work(&port->work);*/ 1771} 1772 1773static void qt2_process_rcv_flush(struct usb_serial_port *port) 1774{ 1775 /* obtain the private structure for the port */ 1776 struct quatech2_port *port_extra = qt2_get_port_private(port); 1777 port_extra->rcv_flush = true; 1778} 1779static void qt2_process_xmit_flush(struct usb_serial_port *port) 1780{ 1781 /* obtain the private structure for the port */ 1782 struct quatech2_port *port_extra = qt2_get_port_private(port); 1783 port_extra->xmit_flush = true; 1784} 1785 1786static void qt2_process_rx_char(struct usb_serial_port *port, 1787 unsigned char data) 1788{ 1789 /* get the tty_struct for this port */ 1790 struct tty_struct *tty = tty_port_tty_get(&(port->port)); 1791 /* get the URB with the data in to push */ 1792 struct urb *urb = port->serial->port[0]->read_urb; 1793 1794 if (tty && urb->actual_length) { 1795 tty_buffer_request_room(tty, 1); 1796 tty_insert_flip_string(tty, &data, 1); 1797 /* should this be commented out here? */ 1798 /*tty_flip_buffer_push(tty);*/ 1799 } 1800} 1801 1802/** @brief Retrieve the value of a register from the device 1803 * 1804 * Issues a GET_REGISTER vendor-spcific request over the USB control 1805 * pipe to obtain a value back from a specific register on a specific 1806 * UART 1807 * @param serial Serial device handle to access the device through 1808 * @param uart_number Which UART the value is wanted from 1809 * @param register_num Which register to read the value from 1810 * @param pValue Pointer to somewhere to put the retrieved value 1811 */ 1812static int qt2_box_get_register(struct usb_serial *serial, 1813 unsigned char uart_number, unsigned short register_num, 1814 __u8 *pValue) 1815{ 1816 int result; 1817 result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0), 1818 QT2_GET_SET_REGISTER, 0xC0, register_num, 1819 uart_number, (void *)pValue, sizeof(*pValue), 300); 1820 return result; 1821} 1822 1823/** qt2_box_set_register 1824 * Issue a SET_REGISTER vendor-specific request on the default control pipe 1825 */ 1826static int qt2_box_set_register(struct usb_serial *serial, 1827 unsigned short Uart_Number, unsigned short Register_Num, 1828 unsigned short Value) 1829{ 1830 int result; 1831 unsigned short reg_and_byte; 1832 1833 reg_and_byte = Value; 1834 reg_and_byte = reg_and_byte << 8; 1835 reg_and_byte = reg_and_byte + Register_Num; 1836 1837 result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), 1838 QT2_GET_SET_REGISTER, 0x40, reg_and_byte, 1839 Uart_Number, NULL, 0, 300); 1840 return result; 1841} 1842 1843/** qt2_boxsetuart - Issue a SET_UART vendor-spcific request on the default 1844 * control pipe. If successful sets baud rate divisor and LCR value. 1845 */ 1846static int qt2_boxsetuart(struct usb_serial *serial, unsigned short Uart_Number, 1847 unsigned short default_divisor, unsigned char default_LCR) 1848{ 1849 unsigned short UartNumandLCR; 1850 1851 UartNumandLCR = (default_LCR << 8) + Uart_Number; 1852 1853 return usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), 1854 QT2_GET_SET_UART, 0x40, default_divisor, UartNumandLCR, 1855 NULL, 0, 300); 1856} 1857 1858/** qt2_boxsethw_flowctl - Turn hardware (RTS/CTS) flow control on and off for 1859 * a hardware UART. 1860 */ 1861static int qt2_boxsethw_flowctl(struct usb_serial *serial, 1862 unsigned int UartNumber, bool bSet) 1863{ 1864 __u8 MCR_Value = 0; 1865 __u8 MSR_Value = 0; 1866 __u16 MOUT_Value = 0; 1867 1868 if (bSet == true) { 1869 MCR_Value = QT2_SERIAL_MCR_RTS; 1870 /* flow control, box will clear RTS line to prevent remote 1871 * device from transmitting more chars */ 1872 } else { 1873 /* no flow control to remote device */ 1874 MCR_Value = 0; 1875 } 1876 MOUT_Value = MCR_Value << 8; 1877 1878 if (bSet == true) { 1879 MSR_Value = QT2_SERIAL_MSR_CTS; 1880 /* flow control on, box will inhibit tx data if CTS line is 1881 * asserted */ 1882 } else { 1883 /* Box will not inhibit tx data due to CTS line */ 1884 MSR_Value = 0; 1885 } 1886 MOUT_Value |= MSR_Value; 1887 return usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), 1888 QT2_HW_FLOW_CONTROL_MASK, 0x40, MOUT_Value, UartNumber, 1889 NULL, 0, 300); 1890} 1891 1892/** qt2_boxsetsw_flowctl - Turn software (XON/XOFF) flow control on for 1893 * a hardware UART, and set the XON and XOFF characters. 1894 */ 1895static int qt2_boxsetsw_flowctl(struct usb_serial *serial, __u16 UartNumber, 1896 unsigned char stop_char, unsigned char start_char) 1897{ 1898 __u16 nSWflowout; 1899 1900 nSWflowout = start_char << 8; 1901 nSWflowout = (unsigned short)stop_char; 1902 return usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), 1903 QT2_SW_FLOW_CONTROL_MASK, 0x40, nSWflowout, UartNumber, 1904 NULL, 0, 300); 1905} 1906 1907/** qt2_boxunsetsw_flowctl - Turn software (XON/XOFF) flow control off for 1908 * a hardware UART. 1909 */ 1910static int qt2_boxunsetsw_flowctl(struct usb_serial *serial, __u16 UartNumber) 1911{ 1912 return usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), 1913 QT2_SW_FLOW_CONTROL_DISABLE, 0x40, 0, UartNumber, NULL, 1914 0, 300); 1915} 1916 1917/** 1918 * qt2_boxstoprx - Start and stop reception of data by the FPGA UART in 1919 * response to requests from the tty layer 1920 * @serial: pointer to the usb_serial structure for the parent device 1921 * @uart_number: which UART on the device we are addressing 1922 * @stop: Whether to start or stop data reception. Set to 1 to stop data being 1923 * received, and to 0 to start it being received. 1924 */ 1925static int qt2_boxstoprx(struct usb_serial *serial, unsigned short uart_number, 1926 unsigned short stop) 1927{ 1928 return usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), 1929 QT2_STOP_RECEIVE, 0x40, stop, uart_number, NULL, 0, 300); 1930} 1931 1932 1933/* 1934 * last things in file: stuff to register this driver into the generic 1935 * USB serial framework. 1936 */ 1937 1938static struct usb_serial_driver quatech2_device = { 1939 .driver = { 1940 .owner = THIS_MODULE, 1941 .name = "quatech_usb2", 1942 }, 1943 .description = DRIVER_DESC, 1944 .id_table = quausb2_id_table, 1945 .num_ports = 8, 1946 .open = qt2_open, 1947 .close = qt2_close, 1948 .write = qt2_write, 1949 .write_room = qt2_write_room, 1950 .chars_in_buffer = qt2_chars_in_buffer, 1951 .throttle = qt2_throttle, 1952 .unthrottle = qt2_unthrottle, 1953 .calc_num_ports = qt2_calc_num_ports, 1954 .ioctl = qt2_ioctl, 1955 .set_termios = qt2_set_termios, 1956 .break_ctl = qt2_break, 1957 .tiocmget = qt2_tiocmget, 1958 .tiocmset = qt2_tiocmset, 1959 .attach = qt2_attach, 1960 .release = qt2_release, 1961 .read_bulk_callback = qt2_read_bulk_callback, 1962 .write_bulk_callback = qt2_write_bulk_callback, 1963}; 1964 1965static struct usb_serial_driver * const serial_drivers[] = { 1966 &quatech2_device, NULL 1967}; 1968 1969module_usb_serial_driver(quausb2_usb_driver, serial_drivers); 1970 1971MODULE_AUTHOR(DRIVER_AUTHOR); 1972MODULE_DESCRIPTION(DRIVER_DESC); 1973MODULE_LICENSE("GPL"); 1974 1975module_param(debug, bool, S_IRUGO | S_IWUSR); 1976MODULE_PARM_DESC(debug, "Debug enabled or not"); 1977