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