1 /* 2 * This program is free software; you can redistribute it and/or modify 3 * it under the terms of the GNU General Public License as published by 4 * the Free Software Foundation; either version 2 of the License, or 5 * (at your option) any later version. 6 * 7 * This program is distributed in the hope that it will be useful, 8 * but WITHOUT ANY WARRANTY; without even the implied warranty of 9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 * GNU General Public License for more details. 11 * 12 * You should have received a copy of the GNU General Public License 13 * along with this program; if not, write to the Free Software 14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 15 * 16 * Clean ups from Moschip version and a few ioctl implementations by: 17 * Paul B Schroeder <pschroeder "at" uplogix "dot" com> 18 * 19 * Originally based on drivers/usb/serial/io_edgeport.c which is: 20 * Copyright (C) 2000 Inside Out Networks, All rights reserved. 21 * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com> 22 * 23 */ 24 25 #include <linux/kernel.h> 26 #include <linux/errno.h> 27 #include <linux/init.h> 28 #include <linux/slab.h> 29 #include <linux/tty.h> 30 #include <linux/tty_driver.h> 31 #include <linux/tty_flip.h> 32 #include <linux/module.h> 33 #include <linux/serial.h> 34 #include <linux/usb.h> 35 #include <linux/usb/serial.h> 36 #include <linux/uaccess.h> 37 38 /* 39 * Version Information 40 */ 41 #define DRIVER_VERSION "1.3.2" 42 #define DRIVER_DESC "Moschip 7840/7820 USB Serial Driver" 43 44 /* 45 * 16C50 UART register defines 46 */ 47 48 #define LCR_BITS_5 0x00 /* 5 bits/char */ 49 #define LCR_BITS_6 0x01 /* 6 bits/char */ 50 #define LCR_BITS_7 0x02 /* 7 bits/char */ 51 #define LCR_BITS_8 0x03 /* 8 bits/char */ 52 #define LCR_BITS_MASK 0x03 /* Mask for bits/char field */ 53 54 #define LCR_STOP_1 0x00 /* 1 stop bit */ 55 #define LCR_STOP_1_5 0x04 /* 1.5 stop bits (if 5 bits/char) */ 56 #define LCR_STOP_2 0x04 /* 2 stop bits (if 6-8 bits/char) */ 57 #define LCR_STOP_MASK 0x04 /* Mask for stop bits field */ 58 59 #define LCR_PAR_NONE 0x00 /* No parity */ 60 #define LCR_PAR_ODD 0x08 /* Odd parity */ 61 #define LCR_PAR_EVEN 0x18 /* Even parity */ 62 #define LCR_PAR_MARK 0x28 /* Force parity bit to 1 */ 63 #define LCR_PAR_SPACE 0x38 /* Force parity bit to 0 */ 64 #define LCR_PAR_MASK 0x38 /* Mask for parity field */ 65 66 #define LCR_SET_BREAK 0x40 /* Set Break condition */ 67 #define LCR_DL_ENABLE 0x80 /* Enable access to divisor latch */ 68 69 #define MCR_DTR 0x01 /* Assert DTR */ 70 #define MCR_RTS 0x02 /* Assert RTS */ 71 #define MCR_OUT1 0x04 /* Loopback only: Sets state of RI */ 72 #define MCR_MASTER_IE 0x08 /* Enable interrupt outputs */ 73 #define MCR_LOOPBACK 0x10 /* Set internal (digital) loopback mode */ 74 #define MCR_XON_ANY 0x20 /* Enable any char to exit XOFF mode */ 75 76 #define MOS7840_MSR_CTS 0x10 /* Current state of CTS */ 77 #define MOS7840_MSR_DSR 0x20 /* Current state of DSR */ 78 #define MOS7840_MSR_RI 0x40 /* Current state of RI */ 79 #define MOS7840_MSR_CD 0x80 /* Current state of CD */ 80 81 /* 82 * Defines used for sending commands to port 83 */ 84 85 #define MOS_WDR_TIMEOUT 5000 /* default urb timeout */ 86 87 #define MOS_PORT1 0x0200 88 #define MOS_PORT2 0x0300 89 #define MOS_VENREG 0x0000 90 #define MOS_MAX_PORT 0x02 91 #define MOS_WRITE 0x0E 92 #define MOS_READ 0x0D 93 94 /* Requests */ 95 #define MCS_RD_RTYPE 0xC0 96 #define MCS_WR_RTYPE 0x40 97 #define MCS_RDREQ 0x0D 98 #define MCS_WRREQ 0x0E 99 #define MCS_CTRL_TIMEOUT 500 100 #define VENDOR_READ_LENGTH (0x01) 101 102 #define MAX_NAME_LEN 64 103 104 #define ZLP_REG1 0x3A /* Zero_Flag_Reg1 58 */ 105 #define ZLP_REG5 0x3E /* Zero_Flag_Reg5 62 */ 106 107 /* For higher baud Rates use TIOCEXBAUD */ 108 #define TIOCEXBAUD 0x5462 109 110 /* vendor id and device id defines */ 111 112 /* The native mos7840/7820 component */ 113 #define USB_VENDOR_ID_MOSCHIP 0x9710 114 #define MOSCHIP_DEVICE_ID_7840 0x7840 115 #define MOSCHIP_DEVICE_ID_7820 0x7820 116 #define MOSCHIP_DEVICE_ID_7810 0x7810 117 /* The native component can have its vendor/device id's overridden 118 * in vendor-specific implementations. Such devices can be handled 119 * by making a change here, in id_table. 120 */ 121 #define USB_VENDOR_ID_BANDB 0x0856 122 #define BANDB_DEVICE_ID_USO9ML2_2 0xAC22 123 #define BANDB_DEVICE_ID_USO9ML2_2P 0xBC00 124 #define BANDB_DEVICE_ID_USO9ML2_4 0xAC24 125 #define BANDB_DEVICE_ID_USO9ML2_4P 0xBC01 126 #define BANDB_DEVICE_ID_US9ML2_2 0xAC29 127 #define BANDB_DEVICE_ID_US9ML2_4 0xAC30 128 #define BANDB_DEVICE_ID_USPTL4_2 0xAC31 129 #define BANDB_DEVICE_ID_USPTL4_4 0xAC32 130 #define BANDB_DEVICE_ID_USOPTL4_2 0xAC42 131 #define BANDB_DEVICE_ID_USOPTL4_2P 0xBC02 132 #define BANDB_DEVICE_ID_USOPTL4_4 0xAC44 133 #define BANDB_DEVICE_ID_USOPTL4_4P 0xBC03 134 #define BANDB_DEVICE_ID_USOPTL2_4 0xAC24 135 136 /* This driver also supports 137 * ATEN UC2324 device using Moschip MCS7840 138 * ATEN UC2322 device using Moschip MCS7820 139 */ 140 #define USB_VENDOR_ID_ATENINTL 0x0557 141 #define ATENINTL_DEVICE_ID_UC2324 0x2011 142 #define ATENINTL_DEVICE_ID_UC2322 0x7820 143 144 /* Interrupt Routine Defines */ 145 146 #define SERIAL_IIR_RLS 0x06 147 #define SERIAL_IIR_MS 0x00 148 149 /* 150 * Emulation of the bit mask on the LINE STATUS REGISTER. 151 */ 152 #define SERIAL_LSR_DR 0x0001 153 #define SERIAL_LSR_OE 0x0002 154 #define SERIAL_LSR_PE 0x0004 155 #define SERIAL_LSR_FE 0x0008 156 #define SERIAL_LSR_BI 0x0010 157 158 #define MOS_MSR_DELTA_CTS 0x10 159 #define MOS_MSR_DELTA_DSR 0x20 160 #define MOS_MSR_DELTA_RI 0x40 161 #define MOS_MSR_DELTA_CD 0x80 162 163 /* Serial Port register Address */ 164 #define INTERRUPT_ENABLE_REGISTER ((__u16)(0x01)) 165 #define FIFO_CONTROL_REGISTER ((__u16)(0x02)) 166 #define LINE_CONTROL_REGISTER ((__u16)(0x03)) 167 #define MODEM_CONTROL_REGISTER ((__u16)(0x04)) 168 #define LINE_STATUS_REGISTER ((__u16)(0x05)) 169 #define MODEM_STATUS_REGISTER ((__u16)(0x06)) 170 #define SCRATCH_PAD_REGISTER ((__u16)(0x07)) 171 #define DIVISOR_LATCH_LSB ((__u16)(0x00)) 172 #define DIVISOR_LATCH_MSB ((__u16)(0x01)) 173 174 #define CLK_MULTI_REGISTER ((__u16)(0x02)) 175 #define CLK_START_VALUE_REGISTER ((__u16)(0x03)) 176 #define GPIO_REGISTER ((__u16)(0x07)) 177 178 #define SERIAL_LCR_DLAB ((__u16)(0x0080)) 179 180 /* 181 * URB POOL related defines 182 */ 183 #define NUM_URBS 16 /* URB Count */ 184 #define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */ 185 186 /* LED on/off milliseconds*/ 187 #define LED_ON_MS 500 188 #define LED_OFF_MS 500 189 190 static int device_type; 191 192 static const struct usb_device_id id_table[] = { 193 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7840)}, 194 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7820)}, 195 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7810)}, 196 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2)}, 197 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2P)}, 198 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4)}, 199 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4P)}, 200 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_2)}, 201 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_4)}, 202 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_2)}, 203 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_4)}, 204 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2)}, 205 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2P)}, 206 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4)}, 207 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4P)}, 208 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL2_4)}, 209 {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2324)}, 210 {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2322)}, 211 {} /* terminating entry */ 212 }; 213 MODULE_DEVICE_TABLE(usb, id_table); 214 215 /* This structure holds all of the local port information */ 216 217 struct moschip_port { 218 int port_num; /*Actual port number in the device(1,2,etc) */ 219 struct urb *write_urb; /* write URB for this port */ 220 struct urb *read_urb; /* read URB for this port */ 221 struct urb *int_urb; 222 __u8 shadowLCR; /* last LCR value received */ 223 __u8 shadowMCR; /* last MCR value received */ 224 char open; 225 char open_ports; 226 char zombie; 227 wait_queue_head_t wait_chase; /* for handling sleeping while waiting for chase to finish */ 228 wait_queue_head_t delta_msr_wait; /* for handling sleeping while waiting for msr change to happen */ 229 int delta_msr_cond; 230 struct async_icount icount; 231 struct usb_serial_port *port; /* loop back to the owner of this object */ 232 233 /* Offsets */ 234 __u8 SpRegOffset; 235 __u8 ControlRegOffset; 236 __u8 DcrRegOffset; 237 /* for processing control URBS in interrupt context */ 238 struct urb *control_urb; 239 struct usb_ctrlrequest *dr; 240 char *ctrl_buf; 241 int MsrLsr; 242 243 spinlock_t pool_lock; 244 struct urb *write_urb_pool[NUM_URBS]; 245 char busy[NUM_URBS]; 246 bool read_urb_busy; 247 248 /* For device(s) with LED indicator */ 249 bool has_led; 250 bool led_flag; 251 struct timer_list led_timer1; /* Timer for LED on */ 252 struct timer_list led_timer2; /* Timer for LED off */ 253 }; 254 255 static bool debug; 256 257 /* 258 * mos7840_set_reg_sync 259 * To set the Control register by calling usb_fill_control_urb function 260 * by passing usb_sndctrlpipe function as parameter. 261 */ 262 263 static int mos7840_set_reg_sync(struct usb_serial_port *port, __u16 reg, 264 __u16 val) 265 { 266 struct usb_device *dev = port->serial->dev; 267 val = val & 0x00ff; 268 dbg("mos7840_set_reg_sync offset is %x, value %x", reg, val); 269 270 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ, 271 MCS_WR_RTYPE, val, reg, NULL, 0, 272 MOS_WDR_TIMEOUT); 273 } 274 275 /* 276 * mos7840_get_reg_sync 277 * To set the Uart register by calling usb_fill_control_urb function by 278 * passing usb_rcvctrlpipe function as parameter. 279 */ 280 281 static int mos7840_get_reg_sync(struct usb_serial_port *port, __u16 reg, 282 __u16 *val) 283 { 284 struct usb_device *dev = port->serial->dev; 285 int ret = 0; 286 u8 *buf; 287 288 buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL); 289 if (!buf) 290 return -ENOMEM; 291 292 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ, 293 MCS_RD_RTYPE, 0, reg, buf, VENDOR_READ_LENGTH, 294 MOS_WDR_TIMEOUT); 295 *val = buf[0]; 296 dbg("mos7840_get_reg_sync offset is %x, return val %x", reg, *val); 297 298 kfree(buf); 299 return ret; 300 } 301 302 /* 303 * mos7840_set_uart_reg 304 * To set the Uart register by calling usb_fill_control_urb function by 305 * passing usb_sndctrlpipe function as parameter. 306 */ 307 308 static int mos7840_set_uart_reg(struct usb_serial_port *port, __u16 reg, 309 __u16 val) 310 { 311 312 struct usb_device *dev = port->serial->dev; 313 val = val & 0x00ff; 314 /* For the UART control registers, the application number need 315 to be Or'ed */ 316 if (port->serial->num_ports == 4) { 317 val |= (((__u16) port->number - 318 (__u16) (port->serial->minor)) + 1) << 8; 319 dbg("mos7840_set_uart_reg application number is %x", val); 320 } else { 321 if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) { 322 val |= (((__u16) port->number - 323 (__u16) (port->serial->minor)) + 1) << 8; 324 dbg("mos7840_set_uart_reg application number is %x", 325 val); 326 } else { 327 val |= 328 (((__u16) port->number - 329 (__u16) (port->serial->minor)) + 2) << 8; 330 dbg("mos7840_set_uart_reg application number is %x", 331 val); 332 } 333 } 334 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ, 335 MCS_WR_RTYPE, val, reg, NULL, 0, 336 MOS_WDR_TIMEOUT); 337 338 } 339 340 /* 341 * mos7840_get_uart_reg 342 * To set the Control register by calling usb_fill_control_urb function 343 * by passing usb_rcvctrlpipe function as parameter. 344 */ 345 static int mos7840_get_uart_reg(struct usb_serial_port *port, __u16 reg, 346 __u16 *val) 347 { 348 struct usb_device *dev = port->serial->dev; 349 int ret = 0; 350 __u16 Wval; 351 u8 *buf; 352 353 buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL); 354 if (!buf) 355 return -ENOMEM; 356 357 /* dbg("application number is %4x", 358 (((__u16)port->number - (__u16)(port->serial->minor))+1)<<8); */ 359 /* Wval is same as application number */ 360 if (port->serial->num_ports == 4) { 361 Wval = 362 (((__u16) port->number - (__u16) (port->serial->minor)) + 363 1) << 8; 364 dbg("mos7840_get_uart_reg application number is %x", Wval); 365 } else { 366 if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) { 367 Wval = (((__u16) port->number - 368 (__u16) (port->serial->minor)) + 1) << 8; 369 dbg("mos7840_get_uart_reg application number is %x", 370 Wval); 371 } else { 372 Wval = (((__u16) port->number - 373 (__u16) (port->serial->minor)) + 2) << 8; 374 dbg("mos7840_get_uart_reg application number is %x", 375 Wval); 376 } 377 } 378 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ, 379 MCS_RD_RTYPE, Wval, reg, buf, VENDOR_READ_LENGTH, 380 MOS_WDR_TIMEOUT); 381 *val = buf[0]; 382 383 kfree(buf); 384 return ret; 385 } 386 387 static void mos7840_dump_serial_port(struct moschip_port *mos7840_port) 388 { 389 390 dbg("***************************************"); 391 dbg("SpRegOffset is %2x", mos7840_port->SpRegOffset); 392 dbg("ControlRegOffset is %2x", mos7840_port->ControlRegOffset); 393 dbg("DCRRegOffset is %2x", mos7840_port->DcrRegOffset); 394 dbg("***************************************"); 395 396 } 397 398 /************************************************************************/ 399 /************************************************************************/ 400 /* I N T E R F A C E F U N C T I O N S */ 401 /* I N T E R F A C E F U N C T I O N S */ 402 /************************************************************************/ 403 /************************************************************************/ 404 405 static inline void mos7840_set_port_private(struct usb_serial_port *port, 406 struct moschip_port *data) 407 { 408 usb_set_serial_port_data(port, (void *)data); 409 } 410 411 static inline struct moschip_port *mos7840_get_port_private(struct 412 usb_serial_port 413 *port) 414 { 415 return (struct moschip_port *)usb_get_serial_port_data(port); 416 } 417 418 static void mos7840_handle_new_msr(struct moschip_port *port, __u8 new_msr) 419 { 420 struct moschip_port *mos7840_port; 421 struct async_icount *icount; 422 mos7840_port = port; 423 icount = &mos7840_port->icount; 424 if (new_msr & 425 (MOS_MSR_DELTA_CTS | MOS_MSR_DELTA_DSR | MOS_MSR_DELTA_RI | 426 MOS_MSR_DELTA_CD)) { 427 icount = &mos7840_port->icount; 428 429 /* update input line counters */ 430 if (new_msr & MOS_MSR_DELTA_CTS) { 431 icount->cts++; 432 smp_wmb(); 433 } 434 if (new_msr & MOS_MSR_DELTA_DSR) { 435 icount->dsr++; 436 smp_wmb(); 437 } 438 if (new_msr & MOS_MSR_DELTA_CD) { 439 icount->dcd++; 440 smp_wmb(); 441 } 442 if (new_msr & MOS_MSR_DELTA_RI) { 443 icount->rng++; 444 smp_wmb(); 445 } 446 } 447 } 448 449 static void mos7840_handle_new_lsr(struct moschip_port *port, __u8 new_lsr) 450 { 451 struct async_icount *icount; 452 453 dbg("%s - %02x", __func__, new_lsr); 454 455 if (new_lsr & SERIAL_LSR_BI) { 456 /* 457 * Parity and Framing errors only count if they 458 * occur exclusive of a break being 459 * received. 460 */ 461 new_lsr &= (__u8) (SERIAL_LSR_OE | SERIAL_LSR_BI); 462 } 463 464 /* update input line counters */ 465 icount = &port->icount; 466 if (new_lsr & SERIAL_LSR_BI) { 467 icount->brk++; 468 smp_wmb(); 469 } 470 if (new_lsr & SERIAL_LSR_OE) { 471 icount->overrun++; 472 smp_wmb(); 473 } 474 if (new_lsr & SERIAL_LSR_PE) { 475 icount->parity++; 476 smp_wmb(); 477 } 478 if (new_lsr & SERIAL_LSR_FE) { 479 icount->frame++; 480 smp_wmb(); 481 } 482 } 483 484 /************************************************************************/ 485 /************************************************************************/ 486 /* U S B C A L L B A C K F U N C T I O N S */ 487 /* U S B C A L L B A C K F U N C T I O N S */ 488 /************************************************************************/ 489 /************************************************************************/ 490 491 static void mos7840_control_callback(struct urb *urb) 492 { 493 unsigned char *data; 494 struct moschip_port *mos7840_port; 495 __u8 regval = 0x0; 496 int result = 0; 497 int status = urb->status; 498 499 mos7840_port = urb->context; 500 501 switch (status) { 502 case 0: 503 /* success */ 504 break; 505 case -ECONNRESET: 506 case -ENOENT: 507 case -ESHUTDOWN: 508 /* this urb is terminated, clean up */ 509 dbg("%s - urb shutting down with status: %d", __func__, 510 status); 511 return; 512 default: 513 dbg("%s - nonzero urb status received: %d", __func__, 514 status); 515 goto exit; 516 } 517 518 dbg("%s urb buffer size is %d", __func__, urb->actual_length); 519 dbg("%s mos7840_port->MsrLsr is %d port %d", __func__, 520 mos7840_port->MsrLsr, mos7840_port->port_num); 521 data = urb->transfer_buffer; 522 regval = (__u8) data[0]; 523 dbg("%s data is %x", __func__, regval); 524 if (mos7840_port->MsrLsr == 0) 525 mos7840_handle_new_msr(mos7840_port, regval); 526 else if (mos7840_port->MsrLsr == 1) 527 mos7840_handle_new_lsr(mos7840_port, regval); 528 529 exit: 530 spin_lock(&mos7840_port->pool_lock); 531 if (!mos7840_port->zombie) 532 result = usb_submit_urb(mos7840_port->int_urb, GFP_ATOMIC); 533 spin_unlock(&mos7840_port->pool_lock); 534 if (result) { 535 dev_err(&urb->dev->dev, 536 "%s - Error %d submitting interrupt urb\n", 537 __func__, result); 538 } 539 } 540 541 static int mos7840_get_reg(struct moschip_port *mcs, __u16 Wval, __u16 reg, 542 __u16 *val) 543 { 544 struct usb_device *dev = mcs->port->serial->dev; 545 struct usb_ctrlrequest *dr = mcs->dr; 546 unsigned char *buffer = mcs->ctrl_buf; 547 int ret; 548 549 dr->bRequestType = MCS_RD_RTYPE; 550 dr->bRequest = MCS_RDREQ; 551 dr->wValue = cpu_to_le16(Wval); /* 0 */ 552 dr->wIndex = cpu_to_le16(reg); 553 dr->wLength = cpu_to_le16(2); 554 555 usb_fill_control_urb(mcs->control_urb, dev, usb_rcvctrlpipe(dev, 0), 556 (unsigned char *)dr, buffer, 2, 557 mos7840_control_callback, mcs); 558 mcs->control_urb->transfer_buffer_length = 2; 559 ret = usb_submit_urb(mcs->control_urb, GFP_ATOMIC); 560 return ret; 561 } 562 563 static void mos7840_set_led_callback(struct urb *urb) 564 { 565 switch (urb->status) { 566 case 0: 567 /* Success */ 568 break; 569 case -ECONNRESET: 570 case -ENOENT: 571 case -ESHUTDOWN: 572 /* This urb is terminated, clean up */ 573 dbg("%s - urb shutting down with status: %d", __func__, 574 urb->status); 575 break; 576 default: 577 dbg("%s - nonzero urb status received: %d", __func__, 578 urb->status); 579 } 580 } 581 582 static void mos7840_set_led_async(struct moschip_port *mcs, __u16 wval, 583 __u16 reg) 584 { 585 struct usb_device *dev = mcs->port->serial->dev; 586 struct usb_ctrlrequest *dr = mcs->dr; 587 588 dr->bRequestType = MCS_WR_RTYPE; 589 dr->bRequest = MCS_WRREQ; 590 dr->wValue = cpu_to_le16(wval); 591 dr->wIndex = cpu_to_le16(reg); 592 dr->wLength = cpu_to_le16(0); 593 594 usb_fill_control_urb(mcs->control_urb, dev, usb_sndctrlpipe(dev, 0), 595 (unsigned char *)dr, NULL, 0, mos7840_set_led_callback, NULL); 596 597 usb_submit_urb(mcs->control_urb, GFP_ATOMIC); 598 } 599 600 static void mos7840_set_led_sync(struct usb_serial_port *port, __u16 reg, 601 __u16 val) 602 { 603 struct usb_device *dev = port->serial->dev; 604 605 usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ, MCS_WR_RTYPE, 606 val, reg, NULL, 0, MOS_WDR_TIMEOUT); 607 } 608 609 static void mos7840_led_off(unsigned long arg) 610 { 611 struct moschip_port *mcs = (struct moschip_port *) arg; 612 613 /* Turn off LED */ 614 mos7840_set_led_async(mcs, 0x0300, MODEM_CONTROL_REGISTER); 615 mod_timer(&mcs->led_timer2, 616 jiffies + msecs_to_jiffies(LED_OFF_MS)); 617 } 618 619 static void mos7840_led_flag_off(unsigned long arg) 620 { 621 struct moschip_port *mcs = (struct moschip_port *) arg; 622 623 mcs->led_flag = false; 624 } 625 626 /***************************************************************************** 627 * mos7840_interrupt_callback 628 * this is the callback function for when we have received data on the 629 * interrupt endpoint. 630 *****************************************************************************/ 631 632 static void mos7840_interrupt_callback(struct urb *urb) 633 { 634 int result; 635 int length; 636 struct moschip_port *mos7840_port; 637 struct usb_serial *serial; 638 __u16 Data; 639 unsigned char *data; 640 __u8 sp[5], st; 641 int i, rv = 0; 642 __u16 wval, wreg = 0; 643 int status = urb->status; 644 645 switch (status) { 646 case 0: 647 /* success */ 648 break; 649 case -ECONNRESET: 650 case -ENOENT: 651 case -ESHUTDOWN: 652 /* this urb is terminated, clean up */ 653 dbg("%s - urb shutting down with status: %d", __func__, 654 status); 655 return; 656 default: 657 dbg("%s - nonzero urb status received: %d", __func__, 658 status); 659 goto exit; 660 } 661 662 length = urb->actual_length; 663 data = urb->transfer_buffer; 664 665 serial = urb->context; 666 667 /* Moschip get 5 bytes 668 * Byte 1 IIR Port 1 (port.number is 0) 669 * Byte 2 IIR Port 2 (port.number is 1) 670 * Byte 3 IIR Port 3 (port.number is 2) 671 * Byte 4 IIR Port 4 (port.number is 3) 672 * Byte 5 FIFO status for both */ 673 674 if (length && length > 5) { 675 dbg("%s", "Wrong data !!!"); 676 return; 677 } 678 679 sp[0] = (__u8) data[0]; 680 sp[1] = (__u8) data[1]; 681 sp[2] = (__u8) data[2]; 682 sp[3] = (__u8) data[3]; 683 st = (__u8) data[4]; 684 685 for (i = 0; i < serial->num_ports; i++) { 686 mos7840_port = mos7840_get_port_private(serial->port[i]); 687 wval = 688 (((__u16) serial->port[i]->number - 689 (__u16) (serial->minor)) + 1) << 8; 690 if (mos7840_port->open) { 691 if (sp[i] & 0x01) { 692 dbg("SP%d No Interrupt !!!", i); 693 } else { 694 switch (sp[i] & 0x0f) { 695 case SERIAL_IIR_RLS: 696 dbg("Serial Port %d: Receiver status error or ", i); 697 dbg("address bit detected in 9-bit mode"); 698 mos7840_port->MsrLsr = 1; 699 wreg = LINE_STATUS_REGISTER; 700 break; 701 case SERIAL_IIR_MS: 702 dbg("Serial Port %d: Modem status change", i); 703 mos7840_port->MsrLsr = 0; 704 wreg = MODEM_STATUS_REGISTER; 705 break; 706 } 707 spin_lock(&mos7840_port->pool_lock); 708 if (!mos7840_port->zombie) { 709 rv = mos7840_get_reg(mos7840_port, wval, wreg, &Data); 710 } else { 711 spin_unlock(&mos7840_port->pool_lock); 712 return; 713 } 714 spin_unlock(&mos7840_port->pool_lock); 715 } 716 } 717 } 718 if (!(rv < 0)) 719 /* the completion handler for the control urb will resubmit */ 720 return; 721 exit: 722 result = usb_submit_urb(urb, GFP_ATOMIC); 723 if (result) { 724 dev_err(&urb->dev->dev, 725 "%s - Error %d submitting interrupt urb\n", 726 __func__, result); 727 } 728 } 729 730 static int mos7840_port_paranoia_check(struct usb_serial_port *port, 731 const char *function) 732 { 733 if (!port) { 734 dbg("%s - port == NULL", function); 735 return -1; 736 } 737 if (!port->serial) { 738 dbg("%s - port->serial == NULL", function); 739 return -1; 740 } 741 742 return 0; 743 } 744 745 /* Inline functions to check the sanity of a pointer that is passed to us */ 746 static int mos7840_serial_paranoia_check(struct usb_serial *serial, 747 const char *function) 748 { 749 if (!serial) { 750 dbg("%s - serial == NULL", function); 751 return -1; 752 } 753 if (!serial->type) { 754 dbg("%s - serial->type == NULL!", function); 755 return -1; 756 } 757 758 return 0; 759 } 760 761 static struct usb_serial *mos7840_get_usb_serial(struct usb_serial_port *port, 762 const char *function) 763 { 764 /* if no port was specified, or it fails a paranoia check */ 765 if (!port || 766 mos7840_port_paranoia_check(port, function) || 767 mos7840_serial_paranoia_check(port->serial, function)) { 768 /* then say that we don't have a valid usb_serial thing, 769 * which will end up genrating -ENODEV return values */ 770 return NULL; 771 } 772 773 return port->serial; 774 } 775 776 /***************************************************************************** 777 * mos7840_bulk_in_callback 778 * this is the callback function for when we have received data on the 779 * bulk in endpoint. 780 *****************************************************************************/ 781 782 static void mos7840_bulk_in_callback(struct urb *urb) 783 { 784 int retval; 785 unsigned char *data; 786 struct usb_serial *serial; 787 struct usb_serial_port *port; 788 struct moschip_port *mos7840_port; 789 struct tty_struct *tty; 790 int status = urb->status; 791 792 mos7840_port = urb->context; 793 if (!mos7840_port) { 794 dbg("%s", "NULL mos7840_port pointer"); 795 return; 796 } 797 798 if (status) { 799 dbg("nonzero read bulk status received: %d", status); 800 mos7840_port->read_urb_busy = false; 801 return; 802 } 803 804 port = (struct usb_serial_port *)mos7840_port->port; 805 if (mos7840_port_paranoia_check(port, __func__)) { 806 dbg("%s", "Port Paranoia failed"); 807 mos7840_port->read_urb_busy = false; 808 return; 809 } 810 811 serial = mos7840_get_usb_serial(port, __func__); 812 if (!serial) { 813 dbg("%s", "Bad serial pointer"); 814 mos7840_port->read_urb_busy = false; 815 return; 816 } 817 818 data = urb->transfer_buffer; 819 820 if (urb->actual_length) { 821 tty = tty_port_tty_get(&mos7840_port->port->port); 822 if (tty) { 823 tty_insert_flip_string(tty, data, urb->actual_length); 824 dbg(" %s ", data); 825 tty_flip_buffer_push(tty); 826 tty_kref_put(tty); 827 } 828 mos7840_port->icount.rx += urb->actual_length; 829 smp_wmb(); 830 dbg("mos7840_port->icount.rx is %d:", 831 mos7840_port->icount.rx); 832 } 833 834 if (!mos7840_port->read_urb) { 835 dbg("%s", "URB KILLED !!!"); 836 mos7840_port->read_urb_busy = false; 837 return; 838 } 839 840 /* Turn on LED */ 841 if (mos7840_port->has_led && !mos7840_port->led_flag) { 842 mos7840_port->led_flag = true; 843 mos7840_set_led_async(mos7840_port, 0x0301, 844 MODEM_CONTROL_REGISTER); 845 mod_timer(&mos7840_port->led_timer1, 846 jiffies + msecs_to_jiffies(LED_ON_MS)); 847 } 848 849 mos7840_port->read_urb_busy = true; 850 retval = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC); 851 852 if (retval) { 853 dbg("usb_submit_urb(read bulk) failed, retval = %d", retval); 854 mos7840_port->read_urb_busy = false; 855 } 856 } 857 858 /***************************************************************************** 859 * mos7840_bulk_out_data_callback 860 * this is the callback function for when we have finished sending 861 * serial data on the bulk out endpoint. 862 *****************************************************************************/ 863 864 static void mos7840_bulk_out_data_callback(struct urb *urb) 865 { 866 struct moschip_port *mos7840_port; 867 struct tty_struct *tty; 868 int status = urb->status; 869 int i; 870 871 mos7840_port = urb->context; 872 spin_lock(&mos7840_port->pool_lock); 873 for (i = 0; i < NUM_URBS; i++) { 874 if (urb == mos7840_port->write_urb_pool[i]) { 875 mos7840_port->busy[i] = 0; 876 break; 877 } 878 } 879 spin_unlock(&mos7840_port->pool_lock); 880 881 if (status) { 882 dbg("nonzero write bulk status received:%d", status); 883 return; 884 } 885 886 if (mos7840_port_paranoia_check(mos7840_port->port, __func__)) { 887 dbg("%s", "Port Paranoia failed"); 888 return; 889 } 890 891 tty = tty_port_tty_get(&mos7840_port->port->port); 892 if (tty && mos7840_port->open) 893 tty_wakeup(tty); 894 tty_kref_put(tty); 895 896 } 897 898 /************************************************************************/ 899 /* D R I V E R T T Y I N T E R F A C E F U N C T I O N S */ 900 /************************************************************************/ 901 #ifdef MCSSerialProbe 902 static int mos7840_serial_probe(struct usb_serial *serial, 903 const struct usb_device_id *id) 904 { 905 906 /*need to implement the mode_reg reading and updating\ 907 structures usb_serial_ device_type\ 908 (i.e num_ports, num_bulkin,bulkout etc) */ 909 /* Also we can update the changes attach */ 910 return 1; 911 } 912 #endif 913 914 /***************************************************************************** 915 * mos7840_open 916 * this function is called by the tty driver when a port is opened 917 * If successful, we return 0 918 * Otherwise we return a negative error number. 919 *****************************************************************************/ 920 921 static int mos7840_open(struct tty_struct *tty, struct usb_serial_port *port) 922 { 923 int response; 924 int j; 925 struct usb_serial *serial; 926 struct urb *urb; 927 __u16 Data; 928 int status; 929 struct moschip_port *mos7840_port; 930 struct moschip_port *port0; 931 932 if (mos7840_port_paranoia_check(port, __func__)) { 933 dbg("%s", "Port Paranoia failed"); 934 return -ENODEV; 935 } 936 937 serial = port->serial; 938 939 if (mos7840_serial_paranoia_check(serial, __func__)) { 940 dbg("%s", "Serial Paranoia failed"); 941 return -ENODEV; 942 } 943 944 mos7840_port = mos7840_get_port_private(port); 945 port0 = mos7840_get_port_private(serial->port[0]); 946 947 if (mos7840_port == NULL || port0 == NULL) 948 return -ENODEV; 949 950 usb_clear_halt(serial->dev, port->write_urb->pipe); 951 usb_clear_halt(serial->dev, port->read_urb->pipe); 952 port0->open_ports++; 953 954 /* Initialising the write urb pool */ 955 for (j = 0; j < NUM_URBS; ++j) { 956 urb = usb_alloc_urb(0, GFP_KERNEL); 957 mos7840_port->write_urb_pool[j] = urb; 958 959 if (urb == NULL) { 960 dev_err(&port->dev, "No more urbs???\n"); 961 continue; 962 } 963 964 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE, 965 GFP_KERNEL); 966 if (!urb->transfer_buffer) { 967 usb_free_urb(urb); 968 mos7840_port->write_urb_pool[j] = NULL; 969 dev_err(&port->dev, 970 "%s-out of memory for urb buffers.\n", 971 __func__); 972 continue; 973 } 974 } 975 976 /***************************************************************************** 977 * Initialize MCS7840 -- Write Init values to corresponding Registers 978 * 979 * Register Index 980 * 1 : IER 981 * 2 : FCR 982 * 3 : LCR 983 * 4 : MCR 984 * 985 * 0x08 : SP1/2 Control Reg 986 *****************************************************************************/ 987 988 /* NEED to check the following Block */ 989 990 Data = 0x0; 991 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data); 992 if (status < 0) { 993 dbg("Reading Spreg failed"); 994 return -1; 995 } 996 Data |= 0x80; 997 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data); 998 if (status < 0) { 999 dbg("writing Spreg failed"); 1000 return -1; 1001 } 1002 1003 Data &= ~0x80; 1004 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data); 1005 if (status < 0) { 1006 dbg("writing Spreg failed"); 1007 return -1; 1008 } 1009 /* End of block to be checked */ 1010 1011 Data = 0x0; 1012 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset, 1013 &Data); 1014 if (status < 0) { 1015 dbg("Reading Controlreg failed"); 1016 return -1; 1017 } 1018 Data |= 0x08; /* Driver done bit */ 1019 Data |= 0x20; /* rx_disable */ 1020 status = mos7840_set_reg_sync(port, 1021 mos7840_port->ControlRegOffset, Data); 1022 if (status < 0) { 1023 dbg("writing Controlreg failed"); 1024 return -1; 1025 } 1026 /* do register settings here */ 1027 /* Set all regs to the device default values. */ 1028 /*********************************** 1029 * First Disable all interrupts. 1030 ***********************************/ 1031 Data = 0x00; 1032 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data); 1033 if (status < 0) { 1034 dbg("disabling interrupts failed"); 1035 return -1; 1036 } 1037 /* Set FIFO_CONTROL_REGISTER to the default value */ 1038 Data = 0x00; 1039 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data); 1040 if (status < 0) { 1041 dbg("Writing FIFO_CONTROL_REGISTER failed"); 1042 return -1; 1043 } 1044 1045 Data = 0xcf; 1046 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data); 1047 if (status < 0) { 1048 dbg("Writing FIFO_CONTROL_REGISTER failed"); 1049 return -1; 1050 } 1051 1052 Data = 0x03; 1053 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data); 1054 mos7840_port->shadowLCR = Data; 1055 1056 Data = 0x0b; 1057 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data); 1058 mos7840_port->shadowMCR = Data; 1059 1060 Data = 0x00; 1061 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data); 1062 mos7840_port->shadowLCR = Data; 1063 1064 Data |= SERIAL_LCR_DLAB; /* data latch enable in LCR 0x80 */ 1065 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data); 1066 1067 Data = 0x0c; 1068 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data); 1069 1070 Data = 0x0; 1071 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data); 1072 1073 Data = 0x00; 1074 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data); 1075 1076 Data = Data & ~SERIAL_LCR_DLAB; 1077 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data); 1078 mos7840_port->shadowLCR = Data; 1079 1080 /* clearing Bulkin and Bulkout Fifo */ 1081 Data = 0x0; 1082 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data); 1083 1084 Data = Data | 0x0c; 1085 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data); 1086 1087 Data = Data & ~0x0c; 1088 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data); 1089 /* Finally enable all interrupts */ 1090 Data = 0x0c; 1091 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data); 1092 1093 /* clearing rx_disable */ 1094 Data = 0x0; 1095 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset, 1096 &Data); 1097 Data = Data & ~0x20; 1098 status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset, 1099 Data); 1100 1101 /* rx_negate */ 1102 Data = 0x0; 1103 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset, 1104 &Data); 1105 Data = Data | 0x10; 1106 status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset, 1107 Data); 1108 1109 /* Check to see if we've set up our endpoint info yet * 1110 * (can't set it up in mos7840_startup as the structures * 1111 * were not set up at that time.) */ 1112 if (port0->open_ports == 1) { 1113 if (serial->port[0]->interrupt_in_buffer == NULL) { 1114 /* set up interrupt urb */ 1115 usb_fill_int_urb(serial->port[0]->interrupt_in_urb, 1116 serial->dev, 1117 usb_rcvintpipe(serial->dev, 1118 serial->port[0]->interrupt_in_endpointAddress), 1119 serial->port[0]->interrupt_in_buffer, 1120 serial->port[0]->interrupt_in_urb-> 1121 transfer_buffer_length, 1122 mos7840_interrupt_callback, 1123 serial, 1124 serial->port[0]->interrupt_in_urb->interval); 1125 1126 /* start interrupt read for mos7840 * 1127 * will continue as long as mos7840 is connected */ 1128 1129 response = 1130 usb_submit_urb(serial->port[0]->interrupt_in_urb, 1131 GFP_KERNEL); 1132 if (response) { 1133 dev_err(&port->dev, "%s - Error %d submitting " 1134 "interrupt urb\n", __func__, response); 1135 } 1136 1137 } 1138 1139 } 1140 1141 /* see if we've set up our endpoint info yet * 1142 * (can't set it up in mos7840_startup as the * 1143 * structures were not set up at that time.) */ 1144 1145 dbg("port number is %d", port->number); 1146 dbg("serial number is %d", port->serial->minor); 1147 dbg("Bulkin endpoint is %d", port->bulk_in_endpointAddress); 1148 dbg("BulkOut endpoint is %d", port->bulk_out_endpointAddress); 1149 dbg("Interrupt endpoint is %d", port->interrupt_in_endpointAddress); 1150 dbg("port's number in the device is %d", mos7840_port->port_num); 1151 mos7840_port->read_urb = port->read_urb; 1152 1153 /* set up our bulk in urb */ 1154 if ((serial->num_ports == 2) 1155 && ((((__u16)port->number - 1156 (__u16)(port->serial->minor)) % 2) != 0)) { 1157 usb_fill_bulk_urb(mos7840_port->read_urb, 1158 serial->dev, 1159 usb_rcvbulkpipe(serial->dev, 1160 (port->bulk_in_endpointAddress) + 2), 1161 port->bulk_in_buffer, 1162 mos7840_port->read_urb->transfer_buffer_length, 1163 mos7840_bulk_in_callback, mos7840_port); 1164 } else { 1165 usb_fill_bulk_urb(mos7840_port->read_urb, 1166 serial->dev, 1167 usb_rcvbulkpipe(serial->dev, 1168 port->bulk_in_endpointAddress), 1169 port->bulk_in_buffer, 1170 mos7840_port->read_urb->transfer_buffer_length, 1171 mos7840_bulk_in_callback, mos7840_port); 1172 } 1173 1174 dbg("mos7840_open: bulkin endpoint is %d", 1175 port->bulk_in_endpointAddress); 1176 mos7840_port->read_urb_busy = true; 1177 response = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL); 1178 if (response) { 1179 dev_err(&port->dev, "%s - Error %d submitting control urb\n", 1180 __func__, response); 1181 mos7840_port->read_urb_busy = false; 1182 } 1183 1184 /* initialize our wait queues */ 1185 init_waitqueue_head(&mos7840_port->wait_chase); 1186 init_waitqueue_head(&mos7840_port->delta_msr_wait); 1187 1188 /* initialize our icount structure */ 1189 memset(&(mos7840_port->icount), 0x00, sizeof(mos7840_port->icount)); 1190 1191 /* initialize our port settings */ 1192 /* Must set to enable ints! */ 1193 mos7840_port->shadowMCR = MCR_MASTER_IE; 1194 /* send a open port command */ 1195 mos7840_port->open = 1; 1196 /* mos7840_change_port_settings(mos7840_port,old_termios); */ 1197 mos7840_port->icount.tx = 0; 1198 mos7840_port->icount.rx = 0; 1199 1200 dbg("usb_serial serial:%p mos7840_port:%p\n usb_serial_port port:%p", 1201 serial, mos7840_port, port); 1202 1203 return 0; 1204 } 1205 1206 /***************************************************************************** 1207 * mos7840_chars_in_buffer 1208 * this function is called by the tty driver when it wants to know how many 1209 * bytes of data we currently have outstanding in the port (data that has 1210 * been written, but hasn't made it out the port yet) 1211 * If successful, we return the number of bytes left to be written in the 1212 * system, 1213 * Otherwise we return zero. 1214 *****************************************************************************/ 1215 1216 static int mos7840_chars_in_buffer(struct tty_struct *tty) 1217 { 1218 struct usb_serial_port *port = tty->driver_data; 1219 int i; 1220 int chars = 0; 1221 unsigned long flags; 1222 struct moschip_port *mos7840_port; 1223 1224 if (mos7840_port_paranoia_check(port, __func__)) { 1225 dbg("%s", "Invalid port"); 1226 return 0; 1227 } 1228 1229 mos7840_port = mos7840_get_port_private(port); 1230 if (mos7840_port == NULL) 1231 return 0; 1232 1233 spin_lock_irqsave(&mos7840_port->pool_lock, flags); 1234 for (i = 0; i < NUM_URBS; ++i) { 1235 if (mos7840_port->busy[i]) { 1236 struct urb *urb = mos7840_port->write_urb_pool[i]; 1237 chars += urb->transfer_buffer_length; 1238 } 1239 } 1240 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags); 1241 dbg("%s - returns %d", __func__, chars); 1242 return chars; 1243 1244 } 1245 1246 /***************************************************************************** 1247 * mos7840_close 1248 * this function is called by the tty driver when a port is closed 1249 *****************************************************************************/ 1250 1251 static void mos7840_close(struct usb_serial_port *port) 1252 { 1253 struct usb_serial *serial; 1254 struct moschip_port *mos7840_port; 1255 struct moschip_port *port0; 1256 int j; 1257 __u16 Data; 1258 1259 if (mos7840_port_paranoia_check(port, __func__)) { 1260 dbg("%s", "Port Paranoia failed"); 1261 return; 1262 } 1263 1264 serial = mos7840_get_usb_serial(port, __func__); 1265 if (!serial) { 1266 dbg("%s", "Serial Paranoia failed"); 1267 return; 1268 } 1269 1270 mos7840_port = mos7840_get_port_private(port); 1271 port0 = mos7840_get_port_private(serial->port[0]); 1272 1273 if (mos7840_port == NULL || port0 == NULL) 1274 return; 1275 1276 for (j = 0; j < NUM_URBS; ++j) 1277 usb_kill_urb(mos7840_port->write_urb_pool[j]); 1278 1279 /* Freeing Write URBs */ 1280 for (j = 0; j < NUM_URBS; ++j) { 1281 if (mos7840_port->write_urb_pool[j]) { 1282 if (mos7840_port->write_urb_pool[j]->transfer_buffer) 1283 kfree(mos7840_port->write_urb_pool[j]-> 1284 transfer_buffer); 1285 1286 usb_free_urb(mos7840_port->write_urb_pool[j]); 1287 } 1288 } 1289 1290 /* While closing port, shutdown all bulk read, write * 1291 * and interrupt read if they exists */ 1292 if (serial->dev) { 1293 if (mos7840_port->write_urb) { 1294 dbg("%s", "Shutdown bulk write"); 1295 usb_kill_urb(mos7840_port->write_urb); 1296 } 1297 if (mos7840_port->read_urb) { 1298 dbg("%s", "Shutdown bulk read"); 1299 usb_kill_urb(mos7840_port->read_urb); 1300 mos7840_port->read_urb_busy = false; 1301 } 1302 if ((&mos7840_port->control_urb)) { 1303 dbg("%s", "Shutdown control read"); 1304 /*/ usb_kill_urb (mos7840_port->control_urb); */ 1305 } 1306 } 1307 /* if(mos7840_port->ctrl_buf != NULL) */ 1308 /* kfree(mos7840_port->ctrl_buf); */ 1309 port0->open_ports--; 1310 dbg("mos7840_num_open_ports in close%d:in port%d", 1311 port0->open_ports, port->number); 1312 if (port0->open_ports == 0) { 1313 if (serial->port[0]->interrupt_in_urb) { 1314 dbg("%s", "Shutdown interrupt_in_urb"); 1315 usb_kill_urb(serial->port[0]->interrupt_in_urb); 1316 } 1317 } 1318 1319 if (mos7840_port->write_urb) { 1320 /* if this urb had a transfer buffer already (old tx) free it */ 1321 if (mos7840_port->write_urb->transfer_buffer != NULL) 1322 kfree(mos7840_port->write_urb->transfer_buffer); 1323 usb_free_urb(mos7840_port->write_urb); 1324 } 1325 1326 Data = 0x0; 1327 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data); 1328 1329 Data = 0x00; 1330 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data); 1331 1332 mos7840_port->open = 0; 1333 } 1334 1335 /************************************************************************ 1336 * 1337 * mos7840_block_until_chase_response 1338 * 1339 * This function will block the close until one of the following: 1340 * 1. Response to our Chase comes from mos7840 1341 * 2. A timeout of 10 seconds without activity has expired 1342 * (1K of mos7840 data @ 2400 baud ==> 4 sec to empty) 1343 * 1344 ************************************************************************/ 1345 1346 static void mos7840_block_until_chase_response(struct tty_struct *tty, 1347 struct moschip_port *mos7840_port) 1348 { 1349 int timeout = msecs_to_jiffies(1000); 1350 int wait = 10; 1351 int count; 1352 1353 while (1) { 1354 count = mos7840_chars_in_buffer(tty); 1355 1356 /* Check for Buffer status */ 1357 if (count <= 0) 1358 return; 1359 1360 /* Block the thread for a while */ 1361 interruptible_sleep_on_timeout(&mos7840_port->wait_chase, 1362 timeout); 1363 /* No activity.. count down section */ 1364 wait--; 1365 if (wait == 0) { 1366 dbg("%s - TIMEOUT", __func__); 1367 return; 1368 } else { 1369 /* Reset timeout value back to seconds */ 1370 wait = 10; 1371 } 1372 } 1373 1374 } 1375 1376 /***************************************************************************** 1377 * mos7840_break 1378 * this function sends a break to the port 1379 *****************************************************************************/ 1380 static void mos7840_break(struct tty_struct *tty, int break_state) 1381 { 1382 struct usb_serial_port *port = tty->driver_data; 1383 unsigned char data; 1384 struct usb_serial *serial; 1385 struct moschip_port *mos7840_port; 1386 1387 if (mos7840_port_paranoia_check(port, __func__)) { 1388 dbg("%s", "Port Paranoia failed"); 1389 return; 1390 } 1391 1392 serial = mos7840_get_usb_serial(port, __func__); 1393 if (!serial) { 1394 dbg("%s", "Serial Paranoia failed"); 1395 return; 1396 } 1397 1398 mos7840_port = mos7840_get_port_private(port); 1399 1400 if (mos7840_port == NULL) 1401 return; 1402 1403 if (serial->dev) 1404 /* flush and block until tx is empty */ 1405 mos7840_block_until_chase_response(tty, mos7840_port); 1406 1407 if (break_state == -1) 1408 data = mos7840_port->shadowLCR | LCR_SET_BREAK; 1409 else 1410 data = mos7840_port->shadowLCR & ~LCR_SET_BREAK; 1411 1412 /* FIXME: no locking on shadowLCR anywhere in driver */ 1413 mos7840_port->shadowLCR = data; 1414 dbg("mcs7840_break mos7840_port->shadowLCR is %x", 1415 mos7840_port->shadowLCR); 1416 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, 1417 mos7840_port->shadowLCR); 1418 } 1419 1420 /***************************************************************************** 1421 * mos7840_write_room 1422 * this function is called by the tty driver when it wants to know how many 1423 * bytes of data we can accept for a specific port. 1424 * If successful, we return the amount of room that we have for this port 1425 * Otherwise we return a negative error number. 1426 *****************************************************************************/ 1427 1428 static int mos7840_write_room(struct tty_struct *tty) 1429 { 1430 struct usb_serial_port *port = tty->driver_data; 1431 int i; 1432 int room = 0; 1433 unsigned long flags; 1434 struct moschip_port *mos7840_port; 1435 1436 if (mos7840_port_paranoia_check(port, __func__)) { 1437 dbg("%s", "Invalid port"); 1438 dbg("%s", " mos7840_write_room:leaving ..........."); 1439 return -1; 1440 } 1441 1442 mos7840_port = mos7840_get_port_private(port); 1443 if (mos7840_port == NULL) { 1444 dbg("%s", "mos7840_break:leaving ..........."); 1445 return -1; 1446 } 1447 1448 spin_lock_irqsave(&mos7840_port->pool_lock, flags); 1449 for (i = 0; i < NUM_URBS; ++i) { 1450 if (!mos7840_port->busy[i]) 1451 room += URB_TRANSFER_BUFFER_SIZE; 1452 } 1453 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags); 1454 1455 room = (room == 0) ? 0 : room - URB_TRANSFER_BUFFER_SIZE + 1; 1456 dbg("%s - returns %d", __func__, room); 1457 return room; 1458 1459 } 1460 1461 /***************************************************************************** 1462 * mos7840_write 1463 * this function is called by the tty driver when data should be written to 1464 * the port. 1465 * If successful, we return the number of bytes written, otherwise we 1466 * return a negative error number. 1467 *****************************************************************************/ 1468 1469 static int mos7840_write(struct tty_struct *tty, struct usb_serial_port *port, 1470 const unsigned char *data, int count) 1471 { 1472 int status; 1473 int i; 1474 int bytes_sent = 0; 1475 int transfer_size; 1476 unsigned long flags; 1477 1478 struct moschip_port *mos7840_port; 1479 struct usb_serial *serial; 1480 struct urb *urb; 1481 /* __u16 Data; */ 1482 const unsigned char *current_position = data; 1483 unsigned char *data1; 1484 1485 #ifdef NOTMOS7840 1486 Data = 0x00; 1487 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data); 1488 mos7840_port->shadowLCR = Data; 1489 dbg("mos7840_write: LINE_CONTROL_REGISTER is %x", Data); 1490 dbg("mos7840_write: mos7840_port->shadowLCR is %x", 1491 mos7840_port->shadowLCR); 1492 1493 /* Data = 0x03; */ 1494 /* status = mos7840_set_uart_reg(port,LINE_CONTROL_REGISTER,Data); */ 1495 /* mos7840_port->shadowLCR=Data;//Need to add later */ 1496 1497 Data |= SERIAL_LCR_DLAB; /* data latch enable in LCR 0x80 */ 1498 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data); 1499 1500 /* Data = 0x0c; */ 1501 /* status = mos7840_set_uart_reg(port,DIVISOR_LATCH_LSB,Data); */ 1502 Data = 0x00; 1503 status = mos7840_get_uart_reg(port, DIVISOR_LATCH_LSB, &Data); 1504 dbg("mos7840_write:DLL value is %x", Data); 1505 1506 Data = 0x0; 1507 status = mos7840_get_uart_reg(port, DIVISOR_LATCH_MSB, &Data); 1508 dbg("mos7840_write:DLM value is %x", Data); 1509 1510 Data = Data & ~SERIAL_LCR_DLAB; 1511 dbg("mos7840_write: mos7840_port->shadowLCR is %x", 1512 mos7840_port->shadowLCR); 1513 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data); 1514 #endif 1515 1516 if (mos7840_port_paranoia_check(port, __func__)) { 1517 dbg("%s", "Port Paranoia failed"); 1518 return -1; 1519 } 1520 1521 serial = port->serial; 1522 if (mos7840_serial_paranoia_check(serial, __func__)) { 1523 dbg("%s", "Serial Paranoia failed"); 1524 return -1; 1525 } 1526 1527 mos7840_port = mos7840_get_port_private(port); 1528 if (mos7840_port == NULL) { 1529 dbg("%s", "mos7840_port is NULL"); 1530 return -1; 1531 } 1532 1533 /* try to find a free urb in the list */ 1534 urb = NULL; 1535 1536 spin_lock_irqsave(&mos7840_port->pool_lock, flags); 1537 for (i = 0; i < NUM_URBS; ++i) { 1538 if (!mos7840_port->busy[i]) { 1539 mos7840_port->busy[i] = 1; 1540 urb = mos7840_port->write_urb_pool[i]; 1541 dbg("URB:%d", i); 1542 break; 1543 } 1544 } 1545 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags); 1546 1547 if (urb == NULL) { 1548 dbg("%s - no more free urbs", __func__); 1549 goto exit; 1550 } 1551 1552 if (urb->transfer_buffer == NULL) { 1553 urb->transfer_buffer = 1554 kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL); 1555 1556 if (urb->transfer_buffer == NULL) { 1557 dev_err_console(port, "%s no more kernel memory...\n", 1558 __func__); 1559 goto exit; 1560 } 1561 } 1562 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE); 1563 1564 memcpy(urb->transfer_buffer, current_position, transfer_size); 1565 1566 /* fill urb with data and submit */ 1567 if ((serial->num_ports == 2) 1568 && ((((__u16)port->number - 1569 (__u16)(port->serial->minor)) % 2) != 0)) { 1570 usb_fill_bulk_urb(urb, 1571 serial->dev, 1572 usb_sndbulkpipe(serial->dev, 1573 (port->bulk_out_endpointAddress) + 2), 1574 urb->transfer_buffer, 1575 transfer_size, 1576 mos7840_bulk_out_data_callback, mos7840_port); 1577 } else { 1578 usb_fill_bulk_urb(urb, 1579 serial->dev, 1580 usb_sndbulkpipe(serial->dev, 1581 port->bulk_out_endpointAddress), 1582 urb->transfer_buffer, 1583 transfer_size, 1584 mos7840_bulk_out_data_callback, mos7840_port); 1585 } 1586 1587 data1 = urb->transfer_buffer; 1588 dbg("bulkout endpoint is %d", port->bulk_out_endpointAddress); 1589 1590 /* Turn on LED */ 1591 if (mos7840_port->has_led && !mos7840_port->led_flag) { 1592 mos7840_port->led_flag = true; 1593 mos7840_set_led_sync(port, MODEM_CONTROL_REGISTER, 0x0301); 1594 mod_timer(&mos7840_port->led_timer1, 1595 jiffies + msecs_to_jiffies(LED_ON_MS)); 1596 } 1597 1598 /* send it down the pipe */ 1599 status = usb_submit_urb(urb, GFP_ATOMIC); 1600 1601 if (status) { 1602 mos7840_port->busy[i] = 0; 1603 dev_err_console(port, "%s - usb_submit_urb(write bulk) failed " 1604 "with status = %d\n", __func__, status); 1605 bytes_sent = status; 1606 goto exit; 1607 } 1608 bytes_sent = transfer_size; 1609 mos7840_port->icount.tx += transfer_size; 1610 smp_wmb(); 1611 dbg("mos7840_port->icount.tx is %d:", mos7840_port->icount.tx); 1612 exit: 1613 return bytes_sent; 1614 1615 } 1616 1617 /***************************************************************************** 1618 * mos7840_throttle 1619 * this function is called by the tty driver when it wants to stop the data 1620 * being read from the port. 1621 *****************************************************************************/ 1622 1623 static void mos7840_throttle(struct tty_struct *tty) 1624 { 1625 struct usb_serial_port *port = tty->driver_data; 1626 struct moschip_port *mos7840_port; 1627 int status; 1628 1629 if (mos7840_port_paranoia_check(port, __func__)) { 1630 dbg("%s", "Invalid port"); 1631 return; 1632 } 1633 1634 dbg("- port %d", port->number); 1635 1636 mos7840_port = mos7840_get_port_private(port); 1637 1638 if (mos7840_port == NULL) 1639 return; 1640 1641 if (!mos7840_port->open) { 1642 dbg("%s", "port not opened"); 1643 return; 1644 } 1645 1646 /* if we are implementing XON/XOFF, send the stop character */ 1647 if (I_IXOFF(tty)) { 1648 unsigned char stop_char = STOP_CHAR(tty); 1649 status = mos7840_write(tty, port, &stop_char, 1); 1650 if (status <= 0) 1651 return; 1652 } 1653 /* if we are implementing RTS/CTS, toggle that line */ 1654 if (tty->termios->c_cflag & CRTSCTS) { 1655 mos7840_port->shadowMCR &= ~MCR_RTS; 1656 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, 1657 mos7840_port->shadowMCR); 1658 if (status < 0) 1659 return; 1660 } 1661 } 1662 1663 /***************************************************************************** 1664 * mos7840_unthrottle 1665 * this function is called by the tty driver when it wants to resume 1666 * the data being read from the port (called after mos7840_throttle is 1667 * called) 1668 *****************************************************************************/ 1669 static void mos7840_unthrottle(struct tty_struct *tty) 1670 { 1671 struct usb_serial_port *port = tty->driver_data; 1672 int status; 1673 struct moschip_port *mos7840_port = mos7840_get_port_private(port); 1674 1675 if (mos7840_port_paranoia_check(port, __func__)) { 1676 dbg("%s", "Invalid port"); 1677 return; 1678 } 1679 1680 if (mos7840_port == NULL) 1681 return; 1682 1683 if (!mos7840_port->open) { 1684 dbg("%s - port not opened", __func__); 1685 return; 1686 } 1687 1688 /* if we are implementing XON/XOFF, send the start character */ 1689 if (I_IXOFF(tty)) { 1690 unsigned char start_char = START_CHAR(tty); 1691 status = mos7840_write(tty, port, &start_char, 1); 1692 if (status <= 0) 1693 return; 1694 } 1695 1696 /* if we are implementing RTS/CTS, toggle that line */ 1697 if (tty->termios->c_cflag & CRTSCTS) { 1698 mos7840_port->shadowMCR |= MCR_RTS; 1699 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, 1700 mos7840_port->shadowMCR); 1701 if (status < 0) 1702 return; 1703 } 1704 } 1705 1706 static int mos7840_tiocmget(struct tty_struct *tty) 1707 { 1708 struct usb_serial_port *port = tty->driver_data; 1709 struct moschip_port *mos7840_port; 1710 unsigned int result; 1711 __u16 msr; 1712 __u16 mcr; 1713 int status; 1714 mos7840_port = mos7840_get_port_private(port); 1715 1716 if (mos7840_port == NULL) 1717 return -ENODEV; 1718 1719 status = mos7840_get_uart_reg(port, MODEM_STATUS_REGISTER, &msr); 1720 status = mos7840_get_uart_reg(port, MODEM_CONTROL_REGISTER, &mcr); 1721 result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0) 1722 | ((mcr & MCR_RTS) ? TIOCM_RTS : 0) 1723 | ((mcr & MCR_LOOPBACK) ? TIOCM_LOOP : 0) 1724 | ((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0) 1725 | ((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0) 1726 | ((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0) 1727 | ((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0); 1728 1729 dbg("%s - 0x%04X", __func__, result); 1730 1731 return result; 1732 } 1733 1734 static int mos7840_tiocmset(struct tty_struct *tty, 1735 unsigned int set, unsigned int clear) 1736 { 1737 struct usb_serial_port *port = tty->driver_data; 1738 struct moschip_port *mos7840_port; 1739 unsigned int mcr; 1740 int status; 1741 1742 mos7840_port = mos7840_get_port_private(port); 1743 1744 if (mos7840_port == NULL) 1745 return -ENODEV; 1746 1747 /* FIXME: What locks the port registers ? */ 1748 mcr = mos7840_port->shadowMCR; 1749 if (clear & TIOCM_RTS) 1750 mcr &= ~MCR_RTS; 1751 if (clear & TIOCM_DTR) 1752 mcr &= ~MCR_DTR; 1753 if (clear & TIOCM_LOOP) 1754 mcr &= ~MCR_LOOPBACK; 1755 1756 if (set & TIOCM_RTS) 1757 mcr |= MCR_RTS; 1758 if (set & TIOCM_DTR) 1759 mcr |= MCR_DTR; 1760 if (set & TIOCM_LOOP) 1761 mcr |= MCR_LOOPBACK; 1762 1763 mos7840_port->shadowMCR = mcr; 1764 1765 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, mcr); 1766 if (status < 0) { 1767 dbg("setting MODEM_CONTROL_REGISTER Failed"); 1768 return status; 1769 } 1770 1771 return 0; 1772 } 1773 1774 /***************************************************************************** 1775 * mos7840_calc_baud_rate_divisor 1776 * this function calculates the proper baud rate divisor for the specified 1777 * baud rate. 1778 *****************************************************************************/ 1779 static int mos7840_calc_baud_rate_divisor(int baudRate, int *divisor, 1780 __u16 *clk_sel_val) 1781 { 1782 dbg("%s - %d", __func__, baudRate); 1783 1784 if (baudRate <= 115200) { 1785 *divisor = 115200 / baudRate; 1786 *clk_sel_val = 0x0; 1787 } 1788 if ((baudRate > 115200) && (baudRate <= 230400)) { 1789 *divisor = 230400 / baudRate; 1790 *clk_sel_val = 0x10; 1791 } else if ((baudRate > 230400) && (baudRate <= 403200)) { 1792 *divisor = 403200 / baudRate; 1793 *clk_sel_val = 0x20; 1794 } else if ((baudRate > 403200) && (baudRate <= 460800)) { 1795 *divisor = 460800 / baudRate; 1796 *clk_sel_val = 0x30; 1797 } else if ((baudRate > 460800) && (baudRate <= 806400)) { 1798 *divisor = 806400 / baudRate; 1799 *clk_sel_val = 0x40; 1800 } else if ((baudRate > 806400) && (baudRate <= 921600)) { 1801 *divisor = 921600 / baudRate; 1802 *clk_sel_val = 0x50; 1803 } else if ((baudRate > 921600) && (baudRate <= 1572864)) { 1804 *divisor = 1572864 / baudRate; 1805 *clk_sel_val = 0x60; 1806 } else if ((baudRate > 1572864) && (baudRate <= 3145728)) { 1807 *divisor = 3145728 / baudRate; 1808 *clk_sel_val = 0x70; 1809 } 1810 return 0; 1811 1812 #ifdef NOTMCS7840 1813 1814 for (i = 0; i < ARRAY_SIZE(mos7840_divisor_table); i++) { 1815 if (mos7840_divisor_table[i].BaudRate == baudrate) { 1816 *divisor = mos7840_divisor_table[i].Divisor; 1817 return 0; 1818 } 1819 } 1820 1821 /* After trying for all the standard baud rates * 1822 * Try calculating the divisor for this baud rate */ 1823 1824 if (baudrate > 75 && baudrate < 230400) { 1825 /* get the divisor */ 1826 custom = (__u16) (230400L / baudrate); 1827 1828 /* Check for round off */ 1829 round1 = (__u16) (2304000L / baudrate); 1830 round = (__u16) (round1 - (custom * 10)); 1831 if (round > 4) 1832 custom++; 1833 *divisor = custom; 1834 1835 dbg(" Baud %d = %d", baudrate, custom); 1836 return 0; 1837 } 1838 1839 dbg("%s", " Baud calculation Failed..."); 1840 return -1; 1841 #endif 1842 } 1843 1844 /***************************************************************************** 1845 * mos7840_send_cmd_write_baud_rate 1846 * this function sends the proper command to change the baud rate of the 1847 * specified port. 1848 *****************************************************************************/ 1849 1850 static int mos7840_send_cmd_write_baud_rate(struct moschip_port *mos7840_port, 1851 int baudRate) 1852 { 1853 int divisor = 0; 1854 int status; 1855 __u16 Data; 1856 unsigned char number; 1857 __u16 clk_sel_val; 1858 struct usb_serial_port *port; 1859 1860 if (mos7840_port == NULL) 1861 return -1; 1862 1863 port = (struct usb_serial_port *)mos7840_port->port; 1864 if (mos7840_port_paranoia_check(port, __func__)) { 1865 dbg("%s", "Invalid port"); 1866 return -1; 1867 } 1868 1869 if (mos7840_serial_paranoia_check(port->serial, __func__)) { 1870 dbg("%s", "Invalid Serial"); 1871 return -1; 1872 } 1873 1874 number = mos7840_port->port->number - mos7840_port->port->serial->minor; 1875 1876 dbg("%s - port = %d, baud = %d", __func__, 1877 mos7840_port->port->number, baudRate); 1878 /* reset clk_uart_sel in spregOffset */ 1879 if (baudRate > 115200) { 1880 #ifdef HW_flow_control 1881 /* NOTE: need to see the pther register to modify */ 1882 /* setting h/w flow control bit to 1 */ 1883 Data = 0x2b; 1884 mos7840_port->shadowMCR = Data; 1885 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, 1886 Data); 1887 if (status < 0) { 1888 dbg("Writing spreg failed in set_serial_baud"); 1889 return -1; 1890 } 1891 #endif 1892 1893 } else { 1894 #ifdef HW_flow_control 1895 /* setting h/w flow control bit to 0 */ 1896 Data = 0xb; 1897 mos7840_port->shadowMCR = Data; 1898 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, 1899 Data); 1900 if (status < 0) { 1901 dbg("Writing spreg failed in set_serial_baud"); 1902 return -1; 1903 } 1904 #endif 1905 1906 } 1907 1908 if (1) { /* baudRate <= 115200) */ 1909 clk_sel_val = 0x0; 1910 Data = 0x0; 1911 status = mos7840_calc_baud_rate_divisor(baudRate, &divisor, 1912 &clk_sel_val); 1913 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, 1914 &Data); 1915 if (status < 0) { 1916 dbg("reading spreg failed in set_serial_baud"); 1917 return -1; 1918 } 1919 Data = (Data & 0x8f) | clk_sel_val; 1920 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, 1921 Data); 1922 if (status < 0) { 1923 dbg("Writing spreg failed in set_serial_baud"); 1924 return -1; 1925 } 1926 /* Calculate the Divisor */ 1927 1928 if (status) { 1929 dev_err(&port->dev, "%s - bad baud rate\n", __func__); 1930 return status; 1931 } 1932 /* Enable access to divisor latch */ 1933 Data = mos7840_port->shadowLCR | SERIAL_LCR_DLAB; 1934 mos7840_port->shadowLCR = Data; 1935 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data); 1936 1937 /* Write the divisor */ 1938 Data = (unsigned char)(divisor & 0xff); 1939 dbg("set_serial_baud Value to write DLL is %x", Data); 1940 mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data); 1941 1942 Data = (unsigned char)((divisor & 0xff00) >> 8); 1943 dbg("set_serial_baud Value to write DLM is %x", Data); 1944 mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data); 1945 1946 /* Disable access to divisor latch */ 1947 Data = mos7840_port->shadowLCR & ~SERIAL_LCR_DLAB; 1948 mos7840_port->shadowLCR = Data; 1949 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data); 1950 1951 } 1952 return status; 1953 } 1954 1955 /***************************************************************************** 1956 * mos7840_change_port_settings 1957 * This routine is called to set the UART on the device to match 1958 * the specified new settings. 1959 *****************************************************************************/ 1960 1961 static void mos7840_change_port_settings(struct tty_struct *tty, 1962 struct moschip_port *mos7840_port, struct ktermios *old_termios) 1963 { 1964 int baud; 1965 unsigned cflag; 1966 unsigned iflag; 1967 __u8 lData; 1968 __u8 lParity; 1969 __u8 lStop; 1970 int status; 1971 __u16 Data; 1972 struct usb_serial_port *port; 1973 struct usb_serial *serial; 1974 1975 if (mos7840_port == NULL) 1976 return; 1977 1978 port = (struct usb_serial_port *)mos7840_port->port; 1979 1980 if (mos7840_port_paranoia_check(port, __func__)) { 1981 dbg("%s", "Invalid port"); 1982 return; 1983 } 1984 1985 if (mos7840_serial_paranoia_check(port->serial, __func__)) { 1986 dbg("%s", "Invalid Serial"); 1987 return; 1988 } 1989 1990 serial = port->serial; 1991 1992 dbg("%s - port %d", __func__, mos7840_port->port->number); 1993 1994 if (!mos7840_port->open) { 1995 dbg("%s - port not opened", __func__); 1996 return; 1997 } 1998 1999 lData = LCR_BITS_8; 2000 lStop = LCR_STOP_1; 2001 lParity = LCR_PAR_NONE; 2002 2003 cflag = tty->termios->c_cflag; 2004 iflag = tty->termios->c_iflag; 2005 2006 /* Change the number of bits */ 2007 if (cflag & CSIZE) { 2008 switch (cflag & CSIZE) { 2009 case CS5: 2010 lData = LCR_BITS_5; 2011 break; 2012 2013 case CS6: 2014 lData = LCR_BITS_6; 2015 break; 2016 2017 case CS7: 2018 lData = LCR_BITS_7; 2019 break; 2020 default: 2021 case CS8: 2022 lData = LCR_BITS_8; 2023 break; 2024 } 2025 } 2026 /* Change the Parity bit */ 2027 if (cflag & PARENB) { 2028 if (cflag & PARODD) { 2029 lParity = LCR_PAR_ODD; 2030 dbg("%s - parity = odd", __func__); 2031 } else { 2032 lParity = LCR_PAR_EVEN; 2033 dbg("%s - parity = even", __func__); 2034 } 2035 2036 } else { 2037 dbg("%s - parity = none", __func__); 2038 } 2039 2040 if (cflag & CMSPAR) 2041 lParity = lParity | 0x20; 2042 2043 /* Change the Stop bit */ 2044 if (cflag & CSTOPB) { 2045 lStop = LCR_STOP_2; 2046 dbg("%s - stop bits = 2", __func__); 2047 } else { 2048 lStop = LCR_STOP_1; 2049 dbg("%s - stop bits = 1", __func__); 2050 } 2051 2052 /* Update the LCR with the correct value */ 2053 mos7840_port->shadowLCR &= 2054 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK); 2055 mos7840_port->shadowLCR |= (lData | lParity | lStop); 2056 2057 dbg("mos7840_change_port_settings mos7840_port->shadowLCR is %x", 2058 mos7840_port->shadowLCR); 2059 /* Disable Interrupts */ 2060 Data = 0x00; 2061 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data); 2062 2063 Data = 0x00; 2064 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data); 2065 2066 Data = 0xcf; 2067 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data); 2068 2069 /* Send the updated LCR value to the mos7840 */ 2070 Data = mos7840_port->shadowLCR; 2071 2072 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data); 2073 2074 Data = 0x00b; 2075 mos7840_port->shadowMCR = Data; 2076 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data); 2077 Data = 0x00b; 2078 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data); 2079 2080 /* set up the MCR register and send it to the mos7840 */ 2081 2082 mos7840_port->shadowMCR = MCR_MASTER_IE; 2083 if (cflag & CBAUD) 2084 mos7840_port->shadowMCR |= (MCR_DTR | MCR_RTS); 2085 2086 if (cflag & CRTSCTS) 2087 mos7840_port->shadowMCR |= (MCR_XON_ANY); 2088 else 2089 mos7840_port->shadowMCR &= ~(MCR_XON_ANY); 2090 2091 Data = mos7840_port->shadowMCR; 2092 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data); 2093 2094 /* Determine divisor based on baud rate */ 2095 baud = tty_get_baud_rate(tty); 2096 2097 if (!baud) { 2098 /* pick a default, any default... */ 2099 dbg("%s", "Picked default baud..."); 2100 baud = 9600; 2101 } 2102 2103 dbg("%s - baud rate = %d", __func__, baud); 2104 status = mos7840_send_cmd_write_baud_rate(mos7840_port, baud); 2105 2106 /* Enable Interrupts */ 2107 Data = 0x0c; 2108 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data); 2109 2110 if (mos7840_port->read_urb_busy == false) { 2111 mos7840_port->read_urb_busy = true; 2112 status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC); 2113 if (status) { 2114 dbg("usb_submit_urb(read bulk) failed, status = %d", 2115 status); 2116 mos7840_port->read_urb_busy = false; 2117 } 2118 } 2119 wake_up(&mos7840_port->delta_msr_wait); 2120 mos7840_port->delta_msr_cond = 1; 2121 dbg("mos7840_change_port_settings mos7840_port->shadowLCR is End %x", 2122 mos7840_port->shadowLCR); 2123 } 2124 2125 /***************************************************************************** 2126 * mos7840_set_termios 2127 * this function is called by the tty driver when it wants to change 2128 * the termios structure 2129 *****************************************************************************/ 2130 2131 static void mos7840_set_termios(struct tty_struct *tty, 2132 struct usb_serial_port *port, 2133 struct ktermios *old_termios) 2134 { 2135 int status; 2136 unsigned int cflag; 2137 struct usb_serial *serial; 2138 struct moschip_port *mos7840_port; 2139 2140 if (mos7840_port_paranoia_check(port, __func__)) { 2141 dbg("%s", "Invalid port"); 2142 return; 2143 } 2144 2145 serial = port->serial; 2146 2147 if (mos7840_serial_paranoia_check(serial, __func__)) { 2148 dbg("%s", "Invalid Serial"); 2149 return; 2150 } 2151 2152 mos7840_port = mos7840_get_port_private(port); 2153 2154 if (mos7840_port == NULL) 2155 return; 2156 2157 if (!mos7840_port->open) { 2158 dbg("%s - port not opened", __func__); 2159 return; 2160 } 2161 2162 dbg("%s", "setting termios - "); 2163 2164 cflag = tty->termios->c_cflag; 2165 2166 dbg("%s - clfag %08x iflag %08x", __func__, 2167 tty->termios->c_cflag, RELEVANT_IFLAG(tty->termios->c_iflag)); 2168 dbg("%s - old clfag %08x old iflag %08x", __func__, 2169 old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag)); 2170 dbg("%s - port %d", __func__, port->number); 2171 2172 /* change the port settings to the new ones specified */ 2173 2174 mos7840_change_port_settings(tty, mos7840_port, old_termios); 2175 2176 if (!mos7840_port->read_urb) { 2177 dbg("%s", "URB KILLED !!!!!"); 2178 return; 2179 } 2180 2181 if (mos7840_port->read_urb_busy == false) { 2182 mos7840_port->read_urb_busy = true; 2183 status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC); 2184 if (status) { 2185 dbg("usb_submit_urb(read bulk) failed, status = %d", 2186 status); 2187 mos7840_port->read_urb_busy = false; 2188 } 2189 } 2190 } 2191 2192 /***************************************************************************** 2193 * mos7840_get_lsr_info - get line status register info 2194 * 2195 * Purpose: Let user call ioctl() to get info when the UART physically 2196 * is emptied. On bus types like RS485, the transmitter must 2197 * release the bus after transmitting. This must be done when 2198 * the transmit shift register is empty, not be done when the 2199 * transmit holding register is empty. This functionality 2200 * allows an RS485 driver to be written in user space. 2201 *****************************************************************************/ 2202 2203 static int mos7840_get_lsr_info(struct tty_struct *tty, 2204 unsigned int __user *value) 2205 { 2206 int count; 2207 unsigned int result = 0; 2208 2209 count = mos7840_chars_in_buffer(tty); 2210 if (count == 0) { 2211 dbg("%s -- Empty", __func__); 2212 result = TIOCSER_TEMT; 2213 } 2214 2215 if (copy_to_user(value, &result, sizeof(int))) 2216 return -EFAULT; 2217 return 0; 2218 } 2219 2220 /***************************************************************************** 2221 * mos7840_get_serial_info 2222 * function to get information about serial port 2223 *****************************************************************************/ 2224 2225 static int mos7840_get_serial_info(struct moschip_port *mos7840_port, 2226 struct serial_struct __user *retinfo) 2227 { 2228 struct serial_struct tmp; 2229 2230 if (mos7840_port == NULL) 2231 return -1; 2232 2233 if (!retinfo) 2234 return -EFAULT; 2235 2236 memset(&tmp, 0, sizeof(tmp)); 2237 2238 tmp.type = PORT_16550A; 2239 tmp.line = mos7840_port->port->serial->minor; 2240 tmp.port = mos7840_port->port->number; 2241 tmp.irq = 0; 2242 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ; 2243 tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE; 2244 tmp.baud_base = 9600; 2245 tmp.close_delay = 5 * HZ; 2246 tmp.closing_wait = 30 * HZ; 2247 2248 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo))) 2249 return -EFAULT; 2250 return 0; 2251 } 2252 2253 static int mos7840_get_icount(struct tty_struct *tty, 2254 struct serial_icounter_struct *icount) 2255 { 2256 struct usb_serial_port *port = tty->driver_data; 2257 struct moschip_port *mos7840_port; 2258 struct async_icount cnow; 2259 2260 mos7840_port = mos7840_get_port_private(port); 2261 cnow = mos7840_port->icount; 2262 2263 smp_rmb(); 2264 icount->cts = cnow.cts; 2265 icount->dsr = cnow.dsr; 2266 icount->rng = cnow.rng; 2267 icount->dcd = cnow.dcd; 2268 icount->rx = cnow.rx; 2269 icount->tx = cnow.tx; 2270 icount->frame = cnow.frame; 2271 icount->overrun = cnow.overrun; 2272 icount->parity = cnow.parity; 2273 icount->brk = cnow.brk; 2274 icount->buf_overrun = cnow.buf_overrun; 2275 2276 dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __func__, 2277 port->number, icount->rx, icount->tx); 2278 return 0; 2279 } 2280 2281 /***************************************************************************** 2282 * SerialIoctl 2283 * this function handles any ioctl calls to the driver 2284 *****************************************************************************/ 2285 2286 static int mos7840_ioctl(struct tty_struct *tty, 2287 unsigned int cmd, unsigned long arg) 2288 { 2289 struct usb_serial_port *port = tty->driver_data; 2290 void __user *argp = (void __user *)arg; 2291 struct moschip_port *mos7840_port; 2292 2293 struct async_icount cnow; 2294 struct async_icount cprev; 2295 2296 if (mos7840_port_paranoia_check(port, __func__)) { 2297 dbg("%s", "Invalid port"); 2298 return -1; 2299 } 2300 2301 mos7840_port = mos7840_get_port_private(port); 2302 2303 if (mos7840_port == NULL) 2304 return -1; 2305 2306 dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd); 2307 2308 switch (cmd) { 2309 /* return number of bytes available */ 2310 2311 case TIOCSERGETLSR: 2312 dbg("%s (%d) TIOCSERGETLSR", __func__, port->number); 2313 return mos7840_get_lsr_info(tty, argp); 2314 2315 case TIOCGSERIAL: 2316 dbg("%s (%d) TIOCGSERIAL", __func__, port->number); 2317 return mos7840_get_serial_info(mos7840_port, argp); 2318 2319 case TIOCSSERIAL: 2320 dbg("%s (%d) TIOCSSERIAL", __func__, port->number); 2321 break; 2322 2323 case TIOCMIWAIT: 2324 dbg("%s (%d) TIOCMIWAIT", __func__, port->number); 2325 cprev = mos7840_port->icount; 2326 while (1) { 2327 /* interruptible_sleep_on(&mos7840_port->delta_msr_wait); */ 2328 mos7840_port->delta_msr_cond = 0; 2329 wait_event_interruptible(mos7840_port->delta_msr_wait, 2330 (mos7840_port-> 2331 delta_msr_cond == 1)); 2332 2333 /* see if a signal did it */ 2334 if (signal_pending(current)) 2335 return -ERESTARTSYS; 2336 cnow = mos7840_port->icount; 2337 smp_rmb(); 2338 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr && 2339 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts) 2340 return -EIO; /* no change => error */ 2341 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) || 2342 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) || 2343 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) || 2344 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) { 2345 return 0; 2346 } 2347 cprev = cnow; 2348 } 2349 /* NOTREACHED */ 2350 break; 2351 2352 default: 2353 break; 2354 } 2355 return -ENOIOCTLCMD; 2356 } 2357 2358 static int mos7810_check(struct usb_serial *serial) 2359 { 2360 int i, pass_count = 0; 2361 __u16 data = 0, mcr_data = 0; 2362 __u16 test_pattern = 0x55AA; 2363 2364 /* Store MCR setting */ 2365 usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0), 2366 MCS_RDREQ, MCS_RD_RTYPE, 0x0300, MODEM_CONTROL_REGISTER, 2367 &mcr_data, VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT); 2368 2369 for (i = 0; i < 16; i++) { 2370 /* Send the 1-bit test pattern out to MCS7810 test pin */ 2371 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), 2372 MCS_WRREQ, MCS_WR_RTYPE, 2373 (0x0300 | (((test_pattern >> i) & 0x0001) << 1)), 2374 MODEM_CONTROL_REGISTER, NULL, 0, MOS_WDR_TIMEOUT); 2375 2376 /* Read the test pattern back */ 2377 usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0), 2378 MCS_RDREQ, MCS_RD_RTYPE, 0, GPIO_REGISTER, &data, 2379 VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT); 2380 2381 /* If this is a MCS7810 device, both test patterns must match */ 2382 if (((test_pattern >> i) ^ (~data >> 1)) & 0x0001) 2383 break; 2384 2385 pass_count++; 2386 } 2387 2388 /* Restore MCR setting */ 2389 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), MCS_WRREQ, 2390 MCS_WR_RTYPE, 0x0300 | mcr_data, MODEM_CONTROL_REGISTER, NULL, 2391 0, MOS_WDR_TIMEOUT); 2392 2393 if (pass_count == 16) 2394 return 1; 2395 2396 return 0; 2397 } 2398 2399 static int mos7840_calc_num_ports(struct usb_serial *serial) 2400 { 2401 __u16 data = 0x00; 2402 int mos7840_num_ports; 2403 2404 usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0), 2405 MCS_RDREQ, MCS_RD_RTYPE, 0, GPIO_REGISTER, &data, 2406 VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT); 2407 2408 if (serial->dev->descriptor.idProduct == MOSCHIP_DEVICE_ID_7810 || 2409 serial->dev->descriptor.idProduct == MOSCHIP_DEVICE_ID_7820) { 2410 device_type = serial->dev->descriptor.idProduct; 2411 } else { 2412 /* For a MCS7840 device GPIO0 must be set to 1 */ 2413 if ((data & 0x01) == 1) 2414 device_type = MOSCHIP_DEVICE_ID_7840; 2415 else if (mos7810_check(serial)) 2416 device_type = MOSCHIP_DEVICE_ID_7810; 2417 else 2418 device_type = MOSCHIP_DEVICE_ID_7820; 2419 } 2420 2421 mos7840_num_ports = (device_type >> 4) & 0x000F; 2422 serial->num_bulk_in = mos7840_num_ports; 2423 serial->num_bulk_out = mos7840_num_ports; 2424 serial->num_ports = mos7840_num_ports; 2425 2426 return mos7840_num_ports; 2427 } 2428 2429 /**************************************************************************** 2430 * mos7840_startup 2431 ****************************************************************************/ 2432 2433 static int mos7840_startup(struct usb_serial *serial) 2434 { 2435 struct moschip_port *mos7840_port; 2436 struct usb_device *dev; 2437 int i, status; 2438 __u16 Data; 2439 2440 if (!serial) { 2441 dbg("%s", "Invalid Handler"); 2442 return -1; 2443 } 2444 2445 dev = serial->dev; 2446 2447 /* we set up the pointers to the endpoints in the mos7840_open * 2448 * function, as the structures aren't created yet. */ 2449 2450 /* set up port private structures */ 2451 for (i = 0; i < serial->num_ports; ++i) { 2452 dbg ("mos7840_startup: configuring port %d............", i); 2453 mos7840_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL); 2454 if (mos7840_port == NULL) { 2455 dev_err(&dev->dev, "%s - Out of memory\n", __func__); 2456 status = -ENOMEM; 2457 i--; /* don't follow NULL pointer cleaning up */ 2458 goto error; 2459 } 2460 2461 /* Initialize all port interrupt end point to port 0 int 2462 * endpoint. Our device has only one interrupt end point 2463 * common to all port */ 2464 2465 mos7840_port->port = serial->port[i]; 2466 mos7840_set_port_private(serial->port[i], mos7840_port); 2467 spin_lock_init(&mos7840_port->pool_lock); 2468 2469 /* minor is not initialised until later by 2470 * usb-serial.c:get_free_serial() and cannot therefore be used 2471 * to index device instances */ 2472 mos7840_port->port_num = i + 1; 2473 dbg ("serial->port[i]->number = %d", serial->port[i]->number); 2474 dbg ("serial->port[i]->serial->minor = %d", serial->port[i]->serial->minor); 2475 dbg ("mos7840_port->port_num = %d", mos7840_port->port_num); 2476 dbg ("serial->minor = %d", serial->minor); 2477 2478 if (mos7840_port->port_num == 1) { 2479 mos7840_port->SpRegOffset = 0x0; 2480 mos7840_port->ControlRegOffset = 0x1; 2481 mos7840_port->DcrRegOffset = 0x4; 2482 } else if ((mos7840_port->port_num == 2) 2483 && (serial->num_ports == 4)) { 2484 mos7840_port->SpRegOffset = 0x8; 2485 mos7840_port->ControlRegOffset = 0x9; 2486 mos7840_port->DcrRegOffset = 0x16; 2487 } else if ((mos7840_port->port_num == 2) 2488 && (serial->num_ports == 2)) { 2489 mos7840_port->SpRegOffset = 0xa; 2490 mos7840_port->ControlRegOffset = 0xb; 2491 mos7840_port->DcrRegOffset = 0x19; 2492 } else if ((mos7840_port->port_num == 3) 2493 && (serial->num_ports == 4)) { 2494 mos7840_port->SpRegOffset = 0xa; 2495 mos7840_port->ControlRegOffset = 0xb; 2496 mos7840_port->DcrRegOffset = 0x19; 2497 } else if ((mos7840_port->port_num == 4) 2498 && (serial->num_ports == 4)) { 2499 mos7840_port->SpRegOffset = 0xc; 2500 mos7840_port->ControlRegOffset = 0xd; 2501 mos7840_port->DcrRegOffset = 0x1c; 2502 } 2503 mos7840_dump_serial_port(mos7840_port); 2504 mos7840_set_port_private(serial->port[i], mos7840_port); 2505 2506 /* enable rx_disable bit in control register */ 2507 status = mos7840_get_reg_sync(serial->port[i], 2508 mos7840_port->ControlRegOffset, &Data); 2509 if (status < 0) { 2510 dbg("Reading ControlReg failed status-0x%x", status); 2511 break; 2512 } else 2513 dbg("ControlReg Reading success val is %x, status%d", 2514 Data, status); 2515 Data |= 0x08; /* setting driver done bit */ 2516 Data |= 0x04; /* sp1_bit to have cts change reflect in 2517 modem status reg */ 2518 2519 /* Data |= 0x20; //rx_disable bit */ 2520 status = mos7840_set_reg_sync(serial->port[i], 2521 mos7840_port->ControlRegOffset, Data); 2522 if (status < 0) { 2523 dbg("Writing ControlReg failed(rx_disable) status-0x%x", status); 2524 break; 2525 } else 2526 dbg("ControlReg Writing success(rx_disable) status%d", 2527 status); 2528 2529 /* Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2 2530 and 0x24 in DCR3 */ 2531 Data = 0x01; 2532 status = mos7840_set_reg_sync(serial->port[i], 2533 (__u16) (mos7840_port->DcrRegOffset + 0), Data); 2534 if (status < 0) { 2535 dbg("Writing DCR0 failed status-0x%x", status); 2536 break; 2537 } else 2538 dbg("DCR0 Writing success status%d", status); 2539 2540 Data = 0x05; 2541 status = mos7840_set_reg_sync(serial->port[i], 2542 (__u16) (mos7840_port->DcrRegOffset + 1), Data); 2543 if (status < 0) { 2544 dbg("Writing DCR1 failed status-0x%x", status); 2545 break; 2546 } else 2547 dbg("DCR1 Writing success status%d", status); 2548 2549 Data = 0x24; 2550 status = mos7840_set_reg_sync(serial->port[i], 2551 (__u16) (mos7840_port->DcrRegOffset + 2), Data); 2552 if (status < 0) { 2553 dbg("Writing DCR2 failed status-0x%x", status); 2554 break; 2555 } else 2556 dbg("DCR2 Writing success status%d", status); 2557 2558 /* write values in clkstart0x0 and clkmulti 0x20 */ 2559 Data = 0x0; 2560 status = mos7840_set_reg_sync(serial->port[i], 2561 CLK_START_VALUE_REGISTER, Data); 2562 if (status < 0) { 2563 dbg("Writing CLK_START_VALUE_REGISTER failed status-0x%x", status); 2564 break; 2565 } else 2566 dbg("CLK_START_VALUE_REGISTER Writing success status%d", status); 2567 2568 Data = 0x20; 2569 status = mos7840_set_reg_sync(serial->port[i], 2570 CLK_MULTI_REGISTER, Data); 2571 if (status < 0) { 2572 dbg("Writing CLK_MULTI_REGISTER failed status-0x%x", 2573 status); 2574 goto error; 2575 } else 2576 dbg("CLK_MULTI_REGISTER Writing success status%d", 2577 status); 2578 2579 /* write value 0x0 to scratchpad register */ 2580 Data = 0x00; 2581 status = mos7840_set_uart_reg(serial->port[i], 2582 SCRATCH_PAD_REGISTER, Data); 2583 if (status < 0) { 2584 dbg("Writing SCRATCH_PAD_REGISTER failed status-0x%x", 2585 status); 2586 break; 2587 } else 2588 dbg("SCRATCH_PAD_REGISTER Writing success status%d", 2589 status); 2590 2591 /* Zero Length flag register */ 2592 if ((mos7840_port->port_num != 1) 2593 && (serial->num_ports == 2)) { 2594 2595 Data = 0xff; 2596 status = mos7840_set_reg_sync(serial->port[i], 2597 (__u16) (ZLP_REG1 + 2598 ((__u16)mos7840_port->port_num)), Data); 2599 dbg("ZLIP offset %x", 2600 (__u16) (ZLP_REG1 + 2601 ((__u16) mos7840_port->port_num))); 2602 if (status < 0) { 2603 dbg("Writing ZLP_REG%d failed status-0x%x", 2604 i + 2, status); 2605 break; 2606 } else 2607 dbg("ZLP_REG%d Writing success status%d", 2608 i + 2, status); 2609 } else { 2610 Data = 0xff; 2611 status = mos7840_set_reg_sync(serial->port[i], 2612 (__u16) (ZLP_REG1 + 2613 ((__u16)mos7840_port->port_num) - 0x1), Data); 2614 dbg("ZLIP offset %x", 2615 (__u16) (ZLP_REG1 + 2616 ((__u16) mos7840_port->port_num) - 0x1)); 2617 if (status < 0) { 2618 dbg("Writing ZLP_REG%d failed status-0x%x", 2619 i + 1, status); 2620 break; 2621 } else 2622 dbg("ZLP_REG%d Writing success status%d", 2623 i + 1, status); 2624 2625 } 2626 mos7840_port->control_urb = usb_alloc_urb(0, GFP_KERNEL); 2627 mos7840_port->ctrl_buf = kmalloc(16, GFP_KERNEL); 2628 mos7840_port->dr = kmalloc(sizeof(struct usb_ctrlrequest), 2629 GFP_KERNEL); 2630 if (!mos7840_port->control_urb || !mos7840_port->ctrl_buf || 2631 !mos7840_port->dr) { 2632 status = -ENOMEM; 2633 goto error; 2634 } 2635 2636 mos7840_port->has_led = false; 2637 2638 /* Initialize LED timers */ 2639 if (device_type == MOSCHIP_DEVICE_ID_7810) { 2640 mos7840_port->has_led = true; 2641 2642 init_timer(&mos7840_port->led_timer1); 2643 mos7840_port->led_timer1.function = mos7840_led_off; 2644 mos7840_port->led_timer1.expires = 2645 jiffies + msecs_to_jiffies(LED_ON_MS); 2646 mos7840_port->led_timer1.data = 2647 (unsigned long)mos7840_port; 2648 2649 init_timer(&mos7840_port->led_timer2); 2650 mos7840_port->led_timer2.function = 2651 mos7840_led_flag_off; 2652 mos7840_port->led_timer2.expires = 2653 jiffies + msecs_to_jiffies(LED_OFF_MS); 2654 mos7840_port->led_timer2.data = 2655 (unsigned long)mos7840_port; 2656 2657 mos7840_port->led_flag = false; 2658 2659 /* Turn off LED */ 2660 mos7840_set_led_sync(serial->port[i], 2661 MODEM_CONTROL_REGISTER, 0x0300); 2662 } 2663 } 2664 dbg ("mos7840_startup: all ports configured..........."); 2665 2666 /* Zero Length flag enable */ 2667 Data = 0x0f; 2668 status = mos7840_set_reg_sync(serial->port[0], ZLP_REG5, Data); 2669 if (status < 0) { 2670 dbg("Writing ZLP_REG5 failed status-0x%x", status); 2671 goto error; 2672 } else 2673 dbg("ZLP_REG5 Writing success status%d", status); 2674 2675 /* setting configuration feature to one */ 2676 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), 2677 (__u8) 0x03, 0x00, 0x01, 0x00, NULL, 0x00, MOS_WDR_TIMEOUT); 2678 return 0; 2679 error: 2680 for (/* nothing */; i >= 0; i--) { 2681 mos7840_port = mos7840_get_port_private(serial->port[i]); 2682 2683 kfree(mos7840_port->dr); 2684 kfree(mos7840_port->ctrl_buf); 2685 usb_free_urb(mos7840_port->control_urb); 2686 kfree(mos7840_port); 2687 serial->port[i] = NULL; 2688 } 2689 return status; 2690 } 2691 2692 /**************************************************************************** 2693 * mos7840_disconnect 2694 * This function is called whenever the device is removed from the usb bus. 2695 ****************************************************************************/ 2696 2697 static void mos7840_disconnect(struct usb_serial *serial) 2698 { 2699 int i; 2700 unsigned long flags; 2701 struct moschip_port *mos7840_port; 2702 2703 if (!serial) { 2704 dbg("%s", "Invalid Handler"); 2705 return; 2706 } 2707 2708 /* check for the ports to be closed,close the ports and disconnect */ 2709 2710 /* free private structure allocated for serial port * 2711 * stop reads and writes on all ports */ 2712 2713 for (i = 0; i < serial->num_ports; ++i) { 2714 mos7840_port = mos7840_get_port_private(serial->port[i]); 2715 dbg ("mos7840_port %d = %p", i, mos7840_port); 2716 if (mos7840_port) { 2717 spin_lock_irqsave(&mos7840_port->pool_lock, flags); 2718 mos7840_port->zombie = 1; 2719 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags); 2720 usb_kill_urb(mos7840_port->control_urb); 2721 } 2722 } 2723 } 2724 2725 /**************************************************************************** 2726 * mos7840_release 2727 * This function is called when the usb_serial structure is freed. 2728 ****************************************************************************/ 2729 2730 static void mos7840_release(struct usb_serial *serial) 2731 { 2732 int i; 2733 struct moschip_port *mos7840_port; 2734 2735 if (!serial) { 2736 dbg("%s", "Invalid Handler"); 2737 return; 2738 } 2739 2740 /* check for the ports to be closed,close the ports and disconnect */ 2741 2742 /* free private structure allocated for serial port * 2743 * stop reads and writes on all ports */ 2744 2745 for (i = 0; i < serial->num_ports; ++i) { 2746 mos7840_port = mos7840_get_port_private(serial->port[i]); 2747 dbg("mos7840_port %d = %p", i, mos7840_port); 2748 if (mos7840_port) { 2749 if (mos7840_port->has_led) { 2750 /* Turn off LED */ 2751 mos7840_set_led_sync(mos7840_port->port, 2752 MODEM_CONTROL_REGISTER, 0x0300); 2753 2754 del_timer_sync(&mos7840_port->led_timer1); 2755 del_timer_sync(&mos7840_port->led_timer2); 2756 } 2757 kfree(mos7840_port->ctrl_buf); 2758 kfree(mos7840_port->dr); 2759 kfree(mos7840_port); 2760 } 2761 } 2762 } 2763 2764 static struct usb_serial_driver moschip7840_4port_device = { 2765 .driver = { 2766 .owner = THIS_MODULE, 2767 .name = "mos7840", 2768 }, 2769 .description = DRIVER_DESC, 2770 .id_table = id_table, 2771 .num_ports = 4, 2772 .open = mos7840_open, 2773 .close = mos7840_close, 2774 .write = mos7840_write, 2775 .write_room = mos7840_write_room, 2776 .chars_in_buffer = mos7840_chars_in_buffer, 2777 .throttle = mos7840_throttle, 2778 .unthrottle = mos7840_unthrottle, 2779 .calc_num_ports = mos7840_calc_num_ports, 2780 #ifdef MCSSerialProbe 2781 .probe = mos7840_serial_probe, 2782 #endif 2783 .ioctl = mos7840_ioctl, 2784 .set_termios = mos7840_set_termios, 2785 .break_ctl = mos7840_break, 2786 .tiocmget = mos7840_tiocmget, 2787 .tiocmset = mos7840_tiocmset, 2788 .get_icount = mos7840_get_icount, 2789 .attach = mos7840_startup, 2790 .disconnect = mos7840_disconnect, 2791 .release = mos7840_release, 2792 .read_bulk_callback = mos7840_bulk_in_callback, 2793 .read_int_callback = mos7840_interrupt_callback, 2794 }; 2795 2796 static struct usb_serial_driver * const serial_drivers[] = { 2797 &moschip7840_4port_device, NULL 2798 }; 2799 2800 module_usb_serial_driver(serial_drivers, id_table); 2801 2802 MODULE_DESCRIPTION(DRIVER_DESC); 2803 MODULE_LICENSE("GPL"); 2804 2805 module_param(debug, bool, S_IRUGO | S_IWUSR); 2806 MODULE_PARM_DESC(debug, "Debug enabled or not"); 2807