1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * mos7720.c 4 * Controls the Moschip 7720 usb to dual port serial converter 5 * 6 * Copyright 2006 Moschip Semiconductor Tech. Ltd. 7 * 8 * Developed by: 9 * Vijaya Kumar <vijaykumar.gn@gmail.com> 10 * Ajay Kumar <naanuajay@yahoo.com> 11 * Gurudeva <ngurudeva@yahoo.com> 12 * 13 * Cleaned up from the original by: 14 * Greg Kroah-Hartman <gregkh@suse.de> 15 * 16 * Originally based on drivers/usb/serial/io_edgeport.c which is: 17 * Copyright (C) 2000 Inside Out Networks, All rights reserved. 18 * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com> 19 */ 20 21 #include <linux/atomic.h> 22 #include <linux/kernel.h> 23 #include <linux/errno.h> 24 #include <linux/slab.h> 25 #include <linux/tty.h> 26 #include <linux/tty_flip.h> 27 #include <linux/module.h> 28 #include <linux/spinlock.h> 29 #include <linux/serial.h> 30 #include <linux/serial_reg.h> 31 #include <linux/usb.h> 32 #include <linux/usb/serial.h> 33 #include <linux/uaccess.h> 34 #include <linux/parport.h> 35 36 #define DRIVER_AUTHOR "Aspire Communications pvt Ltd." 37 #define DRIVER_DESC "Moschip USB Serial Driver" 38 39 /* default urb timeout */ 40 #define MOS_WDR_TIMEOUT 5000 41 42 #define MOS_MAX_PORT 0x02 43 #define MOS_WRITE 0x0E 44 #define MOS_READ 0x0D 45 46 /* Interrupt Routines Defines */ 47 #define SERIAL_IIR_RLS 0x06 48 #define SERIAL_IIR_RDA 0x04 49 #define SERIAL_IIR_CTI 0x0c 50 #define SERIAL_IIR_THR 0x02 51 #define SERIAL_IIR_MS 0x00 52 53 #define NUM_URBS 16 /* URB Count */ 54 #define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */ 55 56 /* This structure holds all of the local serial port information */ 57 struct moschip_port { 58 __u8 shadowLCR; /* last LCR value received */ 59 __u8 shadowMCR; /* last MCR value received */ 60 __u8 shadowMSR; /* last MSR value received */ 61 char open; 62 struct usb_serial_port *port; /* loop back to the owner */ 63 struct urb *write_urb_pool[NUM_URBS]; 64 }; 65 66 #define USB_VENDOR_ID_MOSCHIP 0x9710 67 #define MOSCHIP_DEVICE_ID_7720 0x7720 68 #define MOSCHIP_DEVICE_ID_7715 0x7715 69 70 static const struct usb_device_id id_table[] = { 71 { USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7720) }, 72 { USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7715) }, 73 { } /* terminating entry */ 74 }; 75 MODULE_DEVICE_TABLE(usb, id_table); 76 77 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT 78 79 /* initial values for parport regs */ 80 #define DCR_INIT_VAL 0x0c /* SLCTIN, nINIT */ 81 #define ECR_INIT_VAL 0x00 /* SPP mode */ 82 83 enum mos7715_pp_modes { 84 SPP = 0<<5, 85 PS2 = 1<<5, /* moschip calls this 'NIBBLE' mode */ 86 PPF = 2<<5, /* moschip calls this 'CB-FIFO mode */ 87 }; 88 89 struct mos7715_parport { 90 struct parport *pp; /* back to containing struct */ 91 struct kref ref_count; /* to instance of this struct */ 92 bool msg_pending; /* usb sync call pending */ 93 struct completion syncmsg_compl; /* usb sync call completed */ 94 struct work_struct work; /* restore deferred writes */ 95 struct usb_serial *serial; /* back to containing struct */ 96 __u8 shadowECR; /* parallel port regs... */ 97 __u8 shadowDCR; 98 atomic_t shadowDSR; /* updated in int-in callback */ 99 }; 100 101 /* lock guards against dereferencing NULL ptr in parport ops callbacks */ 102 static DEFINE_SPINLOCK(release_lock); 103 104 #endif /* CONFIG_USB_SERIAL_MOS7715_PARPORT */ 105 106 static const unsigned int dummy; /* for clarity in register access fns */ 107 108 enum mos_regs { 109 MOS7720_THR, /* serial port regs */ 110 MOS7720_RHR, 111 MOS7720_IER, 112 MOS7720_FCR, 113 MOS7720_ISR, 114 MOS7720_LCR, 115 MOS7720_MCR, 116 MOS7720_LSR, 117 MOS7720_MSR, 118 MOS7720_SPR, 119 MOS7720_DLL, 120 MOS7720_DLM, 121 MOS7720_DPR, /* parallel port regs */ 122 MOS7720_DSR, 123 MOS7720_DCR, 124 MOS7720_ECR, 125 MOS7720_SP1_REG, /* device control regs */ 126 MOS7720_SP2_REG, /* serial port 2 (7720 only) */ 127 MOS7720_PP_REG, 128 MOS7720_SP_CONTROL_REG, 129 }; 130 131 /* 132 * Return the correct value for the Windex field of the setup packet 133 * for a control endpoint message. See the 7715 datasheet. 134 */ 135 static inline __u16 get_reg_index(enum mos_regs reg) 136 { 137 static const __u16 mos7715_index_lookup_table[] = { 138 0x00, /* MOS7720_THR */ 139 0x00, /* MOS7720_RHR */ 140 0x01, /* MOS7720_IER */ 141 0x02, /* MOS7720_FCR */ 142 0x02, /* MOS7720_ISR */ 143 0x03, /* MOS7720_LCR */ 144 0x04, /* MOS7720_MCR */ 145 0x05, /* MOS7720_LSR */ 146 0x06, /* MOS7720_MSR */ 147 0x07, /* MOS7720_SPR */ 148 0x00, /* MOS7720_DLL */ 149 0x01, /* MOS7720_DLM */ 150 0x00, /* MOS7720_DPR */ 151 0x01, /* MOS7720_DSR */ 152 0x02, /* MOS7720_DCR */ 153 0x0a, /* MOS7720_ECR */ 154 0x01, /* MOS7720_SP1_REG */ 155 0x02, /* MOS7720_SP2_REG (7720 only) */ 156 0x04, /* MOS7720_PP_REG (7715 only) */ 157 0x08, /* MOS7720_SP_CONTROL_REG */ 158 }; 159 return mos7715_index_lookup_table[reg]; 160 } 161 162 /* 163 * Return the correct value for the upper byte of the Wvalue field of 164 * the setup packet for a control endpoint message. 165 */ 166 static inline __u16 get_reg_value(enum mos_regs reg, 167 unsigned int serial_portnum) 168 { 169 if (reg >= MOS7720_SP1_REG) /* control reg */ 170 return 0x0000; 171 172 else if (reg >= MOS7720_DPR) /* parallel port reg (7715 only) */ 173 return 0x0100; 174 175 else /* serial port reg */ 176 return (serial_portnum + 2) << 8; 177 } 178 179 /* 180 * Write data byte to the specified device register. The data is embedded in 181 * the value field of the setup packet. serial_portnum is ignored for registers 182 * not specific to a particular serial port. 183 */ 184 static int write_mos_reg(struct usb_serial *serial, unsigned int serial_portnum, 185 enum mos_regs reg, __u8 data) 186 { 187 struct usb_device *usbdev = serial->dev; 188 unsigned int pipe = usb_sndctrlpipe(usbdev, 0); 189 __u8 request = (__u8)0x0e; 190 __u8 requesttype = (__u8)0x40; 191 __u16 index = get_reg_index(reg); 192 __u16 value = get_reg_value(reg, serial_portnum) + data; 193 int status = usb_control_msg(usbdev, pipe, request, requesttype, value, 194 index, NULL, 0, MOS_WDR_TIMEOUT); 195 if (status < 0) 196 dev_err(&usbdev->dev, 197 "mos7720: usb_control_msg() failed: %d\n", status); 198 return status; 199 } 200 201 /* 202 * Read data byte from the specified device register. The data returned by the 203 * device is embedded in the value field of the setup packet. serial_portnum is 204 * ignored for registers that are not specific to a particular serial port. 205 */ 206 static int read_mos_reg(struct usb_serial *serial, unsigned int serial_portnum, 207 enum mos_regs reg, __u8 *data) 208 { 209 struct usb_device *usbdev = serial->dev; 210 unsigned int pipe = usb_rcvctrlpipe(usbdev, 0); 211 __u8 request = (__u8)0x0d; 212 __u8 requesttype = (__u8)0xc0; 213 __u16 index = get_reg_index(reg); 214 __u16 value = get_reg_value(reg, serial_portnum); 215 u8 *buf; 216 int status; 217 218 buf = kmalloc(1, GFP_KERNEL); 219 if (!buf) { 220 *data = 0; 221 return -ENOMEM; 222 } 223 224 status = usb_control_msg(usbdev, pipe, request, requesttype, value, 225 index, buf, 1, MOS_WDR_TIMEOUT); 226 if (status == 1) { 227 *data = *buf; 228 } else { 229 dev_err(&usbdev->dev, 230 "mos7720: usb_control_msg() failed: %d\n", status); 231 if (status >= 0) 232 status = -EIO; 233 *data = 0; 234 } 235 236 kfree(buf); 237 238 return status; 239 } 240 241 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT 242 243 static inline int mos7715_change_mode(struct mos7715_parport *mos_parport, 244 enum mos7715_pp_modes mode) 245 { 246 mos_parport->shadowECR = mode; 247 write_mos_reg(mos_parport->serial, dummy, MOS7720_ECR, 248 mos_parport->shadowECR); 249 return 0; 250 } 251 252 static void destroy_mos_parport(struct kref *kref) 253 { 254 struct mos7715_parport *mos_parport = 255 container_of(kref, struct mos7715_parport, ref_count); 256 257 kfree(mos_parport); 258 } 259 260 /* 261 * This is the common top part of all parallel port callback operations that 262 * send synchronous messages to the device. This implements convoluted locking 263 * that avoids two scenarios: (1) a port operation is called after usbserial 264 * has called our release function, at which point struct mos7715_parport has 265 * been destroyed, and (2) the device has been disconnected, but usbserial has 266 * not called the release function yet because someone has a serial port open. 267 * The shared release_lock prevents the first, and the mutex and disconnected 268 * flag maintained by usbserial covers the second. We also use the msg_pending 269 * flag to ensure that all synchronous usb message calls have completed before 270 * our release function can return. 271 */ 272 static int parport_prologue(struct parport *pp) 273 { 274 struct mos7715_parport *mos_parport; 275 276 spin_lock(&release_lock); 277 mos_parport = pp->private_data; 278 if (unlikely(mos_parport == NULL)) { 279 /* release fn called, port struct destroyed */ 280 spin_unlock(&release_lock); 281 return -1; 282 } 283 mos_parport->msg_pending = true; /* synch usb call pending */ 284 reinit_completion(&mos_parport->syncmsg_compl); 285 spin_unlock(&release_lock); 286 287 /* ensure writes from restore are submitted before new requests */ 288 if (work_pending(&mos_parport->work)) 289 flush_work(&mos_parport->work); 290 291 mutex_lock(&mos_parport->serial->disc_mutex); 292 if (mos_parport->serial->disconnected) { 293 /* device disconnected */ 294 mutex_unlock(&mos_parport->serial->disc_mutex); 295 mos_parport->msg_pending = false; 296 complete(&mos_parport->syncmsg_compl); 297 return -1; 298 } 299 300 return 0; 301 } 302 303 /* 304 * This is the common bottom part of all parallel port functions that send 305 * synchronous messages to the device. 306 */ 307 static inline void parport_epilogue(struct parport *pp) 308 { 309 struct mos7715_parport *mos_parport = pp->private_data; 310 mutex_unlock(&mos_parport->serial->disc_mutex); 311 mos_parport->msg_pending = false; 312 complete(&mos_parport->syncmsg_compl); 313 } 314 315 static void deferred_restore_writes(struct work_struct *work) 316 { 317 struct mos7715_parport *mos_parport; 318 319 mos_parport = container_of(work, struct mos7715_parport, work); 320 321 mutex_lock(&mos_parport->serial->disc_mutex); 322 323 /* if device disconnected, game over */ 324 if (mos_parport->serial->disconnected) 325 goto done; 326 327 write_mos_reg(mos_parport->serial, dummy, MOS7720_DCR, 328 mos_parport->shadowDCR); 329 write_mos_reg(mos_parport->serial, dummy, MOS7720_ECR, 330 mos_parport->shadowECR); 331 done: 332 mutex_unlock(&mos_parport->serial->disc_mutex); 333 } 334 335 static void parport_mos7715_write_data(struct parport *pp, unsigned char d) 336 { 337 struct mos7715_parport *mos_parport = pp->private_data; 338 339 if (parport_prologue(pp) < 0) 340 return; 341 mos7715_change_mode(mos_parport, SPP); 342 write_mos_reg(mos_parport->serial, dummy, MOS7720_DPR, (__u8)d); 343 parport_epilogue(pp); 344 } 345 346 static unsigned char parport_mos7715_read_data(struct parport *pp) 347 { 348 struct mos7715_parport *mos_parport = pp->private_data; 349 unsigned char d; 350 351 if (parport_prologue(pp) < 0) 352 return 0; 353 read_mos_reg(mos_parport->serial, dummy, MOS7720_DPR, &d); 354 parport_epilogue(pp); 355 return d; 356 } 357 358 static void parport_mos7715_write_control(struct parport *pp, unsigned char d) 359 { 360 struct mos7715_parport *mos_parport = pp->private_data; 361 __u8 data; 362 363 if (parport_prologue(pp) < 0) 364 return; 365 data = ((__u8)d & 0x0f) | (mos_parport->shadowDCR & 0xf0); 366 write_mos_reg(mos_parport->serial, dummy, MOS7720_DCR, data); 367 mos_parport->shadowDCR = data; 368 parport_epilogue(pp); 369 } 370 371 static unsigned char parport_mos7715_read_control(struct parport *pp) 372 { 373 struct mos7715_parport *mos_parport; 374 __u8 dcr; 375 376 spin_lock(&release_lock); 377 mos_parport = pp->private_data; 378 if (unlikely(mos_parport == NULL)) { 379 spin_unlock(&release_lock); 380 return 0; 381 } 382 dcr = mos_parport->shadowDCR & 0x0f; 383 spin_unlock(&release_lock); 384 return dcr; 385 } 386 387 static unsigned char parport_mos7715_frob_control(struct parport *pp, 388 unsigned char mask, 389 unsigned char val) 390 { 391 struct mos7715_parport *mos_parport = pp->private_data; 392 __u8 dcr; 393 394 mask &= 0x0f; 395 val &= 0x0f; 396 if (parport_prologue(pp) < 0) 397 return 0; 398 mos_parport->shadowDCR = (mos_parport->shadowDCR & (~mask)) ^ val; 399 write_mos_reg(mos_parport->serial, dummy, MOS7720_DCR, 400 mos_parport->shadowDCR); 401 dcr = mos_parport->shadowDCR & 0x0f; 402 parport_epilogue(pp); 403 return dcr; 404 } 405 406 static unsigned char parport_mos7715_read_status(struct parport *pp) 407 { 408 unsigned char status; 409 struct mos7715_parport *mos_parport; 410 411 spin_lock(&release_lock); 412 mos_parport = pp->private_data; 413 if (unlikely(mos_parport == NULL)) { /* release called */ 414 spin_unlock(&release_lock); 415 return 0; 416 } 417 status = atomic_read(&mos_parport->shadowDSR) & 0xf8; 418 spin_unlock(&release_lock); 419 return status; 420 } 421 422 static void parport_mos7715_enable_irq(struct parport *pp) 423 { 424 } 425 426 static void parport_mos7715_disable_irq(struct parport *pp) 427 { 428 } 429 430 static void parport_mos7715_data_forward(struct parport *pp) 431 { 432 struct mos7715_parport *mos_parport = pp->private_data; 433 434 if (parport_prologue(pp) < 0) 435 return; 436 mos7715_change_mode(mos_parport, PS2); 437 mos_parport->shadowDCR &= ~0x20; 438 write_mos_reg(mos_parport->serial, dummy, MOS7720_DCR, 439 mos_parport->shadowDCR); 440 parport_epilogue(pp); 441 } 442 443 static void parport_mos7715_data_reverse(struct parport *pp) 444 { 445 struct mos7715_parport *mos_parport = pp->private_data; 446 447 if (parport_prologue(pp) < 0) 448 return; 449 mos7715_change_mode(mos_parport, PS2); 450 mos_parport->shadowDCR |= 0x20; 451 write_mos_reg(mos_parport->serial, dummy, MOS7720_DCR, 452 mos_parport->shadowDCR); 453 parport_epilogue(pp); 454 } 455 456 static void parport_mos7715_init_state(struct pardevice *dev, 457 struct parport_state *s) 458 { 459 s->u.pc.ctr = DCR_INIT_VAL; 460 s->u.pc.ecr = ECR_INIT_VAL; 461 } 462 463 /* N.B. Parport core code requires that this function not block */ 464 static void parport_mos7715_save_state(struct parport *pp, 465 struct parport_state *s) 466 { 467 struct mos7715_parport *mos_parport; 468 469 spin_lock(&release_lock); 470 mos_parport = pp->private_data; 471 if (unlikely(mos_parport == NULL)) { /* release called */ 472 spin_unlock(&release_lock); 473 return; 474 } 475 s->u.pc.ctr = mos_parport->shadowDCR; 476 s->u.pc.ecr = mos_parport->shadowECR; 477 spin_unlock(&release_lock); 478 } 479 480 /* N.B. Parport core code requires that this function not block */ 481 static void parport_mos7715_restore_state(struct parport *pp, 482 struct parport_state *s) 483 { 484 struct mos7715_parport *mos_parport; 485 486 spin_lock(&release_lock); 487 mos_parport = pp->private_data; 488 if (unlikely(mos_parport == NULL)) { /* release called */ 489 spin_unlock(&release_lock); 490 return; 491 } 492 mos_parport->shadowDCR = s->u.pc.ctr; 493 mos_parport->shadowECR = s->u.pc.ecr; 494 495 schedule_work(&mos_parport->work); 496 spin_unlock(&release_lock); 497 } 498 499 static size_t parport_mos7715_write_compat(struct parport *pp, 500 const void *buffer, 501 size_t len, int flags) 502 { 503 int retval; 504 struct mos7715_parport *mos_parport = pp->private_data; 505 int actual_len; 506 507 if (parport_prologue(pp) < 0) 508 return 0; 509 mos7715_change_mode(mos_parport, PPF); 510 retval = usb_bulk_msg(mos_parport->serial->dev, 511 usb_sndbulkpipe(mos_parport->serial->dev, 2), 512 (void *)buffer, len, &actual_len, 513 MOS_WDR_TIMEOUT); 514 parport_epilogue(pp); 515 if (retval) { 516 dev_err(&mos_parport->serial->dev->dev, 517 "mos7720: usb_bulk_msg() failed: %d\n", retval); 518 return 0; 519 } 520 return actual_len; 521 } 522 523 static struct parport_operations parport_mos7715_ops = { 524 .owner = THIS_MODULE, 525 .write_data = parport_mos7715_write_data, 526 .read_data = parport_mos7715_read_data, 527 528 .write_control = parport_mos7715_write_control, 529 .read_control = parport_mos7715_read_control, 530 .frob_control = parport_mos7715_frob_control, 531 532 .read_status = parport_mos7715_read_status, 533 534 .enable_irq = parport_mos7715_enable_irq, 535 .disable_irq = parport_mos7715_disable_irq, 536 537 .data_forward = parport_mos7715_data_forward, 538 .data_reverse = parport_mos7715_data_reverse, 539 540 .init_state = parport_mos7715_init_state, 541 .save_state = parport_mos7715_save_state, 542 .restore_state = parport_mos7715_restore_state, 543 544 .compat_write_data = parport_mos7715_write_compat, 545 546 .nibble_read_data = parport_ieee1284_read_nibble, 547 .byte_read_data = parport_ieee1284_read_byte, 548 }; 549 550 /* 551 * Allocate and initialize parallel port control struct, initialize 552 * the parallel port hardware device, and register with the parport subsystem. 553 */ 554 static int mos7715_parport_init(struct usb_serial *serial) 555 { 556 struct mos7715_parport *mos_parport; 557 558 /* allocate and initialize parallel port control struct */ 559 mos_parport = kzalloc_obj(struct mos7715_parport); 560 if (!mos_parport) 561 return -ENOMEM; 562 563 mos_parport->msg_pending = false; 564 kref_init(&mos_parport->ref_count); 565 usb_set_serial_data(serial, mos_parport); /* hijack private pointer */ 566 mos_parport->serial = serial; 567 INIT_WORK(&mos_parport->work, deferred_restore_writes); 568 init_completion(&mos_parport->syncmsg_compl); 569 570 /* cycle parallel port reset bit */ 571 write_mos_reg(mos_parport->serial, dummy, MOS7720_PP_REG, (__u8)0x80); 572 write_mos_reg(mos_parport->serial, dummy, MOS7720_PP_REG, (__u8)0x00); 573 574 /* initialize device registers */ 575 mos_parport->shadowDCR = DCR_INIT_VAL; 576 write_mos_reg(mos_parport->serial, dummy, MOS7720_DCR, 577 mos_parport->shadowDCR); 578 mos_parport->shadowECR = ECR_INIT_VAL; 579 write_mos_reg(mos_parport->serial, dummy, MOS7720_ECR, 580 mos_parport->shadowECR); 581 582 /* register with parport core */ 583 mos_parport->pp = parport_register_port(0, PARPORT_IRQ_NONE, 584 PARPORT_DMA_NONE, 585 &parport_mos7715_ops); 586 if (mos_parport->pp == NULL) { 587 dev_err(&serial->interface->dev, 588 "Could not register parport\n"); 589 kref_put(&mos_parport->ref_count, destroy_mos_parport); 590 return -EIO; 591 } 592 mos_parport->pp->private_data = mos_parport; 593 mos_parport->pp->modes = PARPORT_MODE_COMPAT | PARPORT_MODE_PCSPP; 594 mos_parport->pp->dev = &serial->interface->dev; 595 parport_announce_port(mos_parport->pp); 596 597 return 0; 598 } 599 #endif /* CONFIG_USB_SERIAL_MOS7715_PARPORT */ 600 601 /* 602 * mos7720_interrupt_callback 603 * this is the callback function for when we have received data on the 604 * interrupt endpoint. 605 */ 606 static void mos7720_interrupt_callback(struct urb *urb) 607 { 608 int result; 609 int length; 610 int status = urb->status; 611 struct device *dev = &urb->dev->dev; 612 __u8 *data; 613 __u8 sp1; 614 __u8 sp2; 615 616 switch (status) { 617 case 0: 618 /* success */ 619 break; 620 case -ECONNRESET: 621 case -ENOENT: 622 case -ESHUTDOWN: 623 /* this urb is terminated, clean up */ 624 dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status); 625 return; 626 default: 627 dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status); 628 goto exit; 629 } 630 631 length = urb->actual_length; 632 data = urb->transfer_buffer; 633 634 /* Moschip get 4 bytes 635 * Byte 1 IIR Port 1 (port.number is 0) 636 * Byte 2 IIR Port 2 (port.number is 1) 637 * Byte 3 -------------- 638 * Byte 4 FIFO status for both */ 639 640 /* the above description is inverted 641 * oneukum 2007-03-14 */ 642 643 if (unlikely(length != 4)) { 644 dev_dbg(dev, "Wrong data !!!\n"); 645 return; 646 } 647 648 sp1 = data[3]; 649 sp2 = data[2]; 650 651 if ((sp1 | sp2) & 0x01) { 652 /* No Interrupt Pending in both the ports */ 653 dev_dbg(dev, "No Interrupt !!!\n"); 654 } else { 655 switch (sp1 & 0x0f) { 656 case SERIAL_IIR_RLS: 657 dev_dbg(dev, "Serial Port 1: Receiver status error or address bit detected in 9-bit mode\n"); 658 break; 659 case SERIAL_IIR_CTI: 660 dev_dbg(dev, "Serial Port 1: Receiver time out\n"); 661 break; 662 case SERIAL_IIR_MS: 663 /* dev_dbg(dev, "Serial Port 1: Modem status change\n"); */ 664 break; 665 } 666 667 switch (sp2 & 0x0f) { 668 case SERIAL_IIR_RLS: 669 dev_dbg(dev, "Serial Port 2: Receiver status error or address bit detected in 9-bit mode\n"); 670 break; 671 case SERIAL_IIR_CTI: 672 dev_dbg(dev, "Serial Port 2: Receiver time out\n"); 673 break; 674 case SERIAL_IIR_MS: 675 /* dev_dbg(dev, "Serial Port 2: Modem status change\n"); */ 676 break; 677 } 678 } 679 680 exit: 681 result = usb_submit_urb(urb, GFP_ATOMIC); 682 if (result) 683 dev_err(dev, "%s - Error %d submitting control urb\n", __func__, result); 684 } 685 686 /* 687 * mos7715_interrupt_callback 688 * this is the 7715's callback function for when we have received data on 689 * the interrupt endpoint. 690 */ 691 static void mos7715_interrupt_callback(struct urb *urb) 692 { 693 int result; 694 int length; 695 int status = urb->status; 696 struct device *dev = &urb->dev->dev; 697 __u8 *data; 698 __u8 iir; 699 700 switch (status) { 701 case 0: 702 /* success */ 703 break; 704 case -ECONNRESET: 705 case -ENOENT: 706 case -ESHUTDOWN: 707 case -ENODEV: 708 /* this urb is terminated, clean up */ 709 dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status); 710 return; 711 default: 712 dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status); 713 goto exit; 714 } 715 716 length = urb->actual_length; 717 data = urb->transfer_buffer; 718 719 /* Structure of data from 7715 device: 720 * Byte 1: IIR serial Port 721 * Byte 2: unused 722 * Byte 2: DSR parallel port 723 * Byte 4: FIFO status for both */ 724 725 if (unlikely(length != 4)) { 726 dev_dbg(dev, "Wrong data !!!\n"); 727 return; 728 } 729 730 iir = data[0]; 731 if (!(iir & 0x01)) { /* serial port interrupt pending */ 732 switch (iir & 0x0f) { 733 case SERIAL_IIR_RLS: 734 dev_dbg(dev, "Serial Port: Receiver status error or address bit detected in 9-bit mode\n"); 735 break; 736 case SERIAL_IIR_CTI: 737 dev_dbg(dev, "Serial Port: Receiver time out\n"); 738 break; 739 case SERIAL_IIR_MS: 740 /* dev_dbg(dev, "Serial Port: Modem status change\n"); */ 741 break; 742 } 743 } 744 745 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT 746 { /* update local copy of DSR reg */ 747 struct usb_serial_port *port = urb->context; 748 struct mos7715_parport *mos_parport = port->serial->private; 749 if (unlikely(mos_parport == NULL)) 750 return; 751 atomic_set(&mos_parport->shadowDSR, data[2]); 752 } 753 #endif 754 755 exit: 756 result = usb_submit_urb(urb, GFP_ATOMIC); 757 if (result) 758 dev_err(dev, "%s - Error %d submitting control urb\n", __func__, result); 759 } 760 761 /* 762 * mos7720_bulk_in_callback 763 * this is the callback function for when we have received data on the 764 * bulk in endpoint. 765 */ 766 static void mos7720_bulk_in_callback(struct urb *urb) 767 { 768 int retval; 769 unsigned char *data ; 770 struct usb_serial_port *port; 771 int status = urb->status; 772 773 if (status) { 774 dev_dbg(&urb->dev->dev, "nonzero read bulk status received: %d\n", status); 775 return; 776 } 777 778 port = urb->context; 779 780 dev_dbg(&port->dev, "Entering...%s\n", __func__); 781 782 data = urb->transfer_buffer; 783 784 if (urb->actual_length) { 785 tty_insert_flip_string(&port->port, data, urb->actual_length); 786 tty_flip_buffer_push(&port->port); 787 } 788 789 if (port->read_urb->status != -EINPROGRESS) { 790 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC); 791 if (retval) 792 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, retval = %d\n", retval); 793 } 794 } 795 796 /* 797 * mos7720_bulk_out_data_callback 798 * this is the callback function for when we have finished sending serial 799 * data on the bulk out endpoint. 800 */ 801 static void mos7720_bulk_out_data_callback(struct urb *urb) 802 { 803 struct moschip_port *mos7720_port; 804 int status = urb->status; 805 806 if (status) { 807 dev_dbg(&urb->dev->dev, "nonzero write bulk status received:%d\n", status); 808 return; 809 } 810 811 mos7720_port = urb->context; 812 if (!mos7720_port) { 813 dev_dbg(&urb->dev->dev, "NULL mos7720_port pointer\n"); 814 return ; 815 } 816 817 if (mos7720_port->open) 818 tty_port_tty_wakeup(&mos7720_port->port->port); 819 } 820 821 static int mos77xx_calc_num_ports(struct usb_serial *serial, 822 struct usb_serial_endpoints *epds) 823 { 824 u16 product = le16_to_cpu(serial->dev->descriptor.idProduct); 825 826 if (product == MOSCHIP_DEVICE_ID_7715) { 827 /* 828 * The 7715 uses the first bulk in/out endpoint pair for the 829 * parallel port, and the second for the serial port. We swap 830 * the endpoint descriptors here so that the first and 831 * only registered port structure uses the serial-port 832 * endpoints. 833 */ 834 swap(epds->bulk_in[0], epds->bulk_in[1]); 835 swap(epds->bulk_out[0], epds->bulk_out[1]); 836 837 return 1; 838 } 839 840 return 2; 841 } 842 843 static int mos7720_open(struct tty_struct *tty, struct usb_serial_port *port) 844 { 845 struct usb_serial *serial; 846 struct urb *urb; 847 struct moschip_port *mos7720_port; 848 int response; 849 int port_number; 850 __u8 data; 851 int allocated_urbs = 0; 852 int j; 853 854 serial = port->serial; 855 856 mos7720_port = usb_get_serial_port_data(port); 857 if (mos7720_port == NULL) 858 return -ENODEV; 859 860 usb_clear_halt(serial->dev, port->write_urb->pipe); 861 usb_clear_halt(serial->dev, port->read_urb->pipe); 862 863 /* Initialising the write urb pool */ 864 for (j = 0; j < NUM_URBS; ++j) { 865 urb = usb_alloc_urb(0, GFP_KERNEL); 866 mos7720_port->write_urb_pool[j] = urb; 867 if (!urb) 868 continue; 869 870 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE, 871 GFP_KERNEL); 872 if (!urb->transfer_buffer) { 873 usb_free_urb(mos7720_port->write_urb_pool[j]); 874 mos7720_port->write_urb_pool[j] = NULL; 875 continue; 876 } 877 allocated_urbs++; 878 } 879 880 if (!allocated_urbs) 881 return -ENOMEM; 882 883 /* Initialize MCS7720 -- Write Init values to corresponding Registers 884 * 885 * Register Index 886 * 0 : MOS7720_THR/MOS7720_RHR 887 * 1 : MOS7720_IER 888 * 2 : MOS7720_FCR 889 * 3 : MOS7720_LCR 890 * 4 : MOS7720_MCR 891 * 5 : MOS7720_LSR 892 * 6 : MOS7720_MSR 893 * 7 : MOS7720_SPR 894 * 895 * 0x08 : SP1/2 Control Reg 896 */ 897 port_number = port->port_number; 898 read_mos_reg(serial, port_number, MOS7720_LSR, &data); 899 900 dev_dbg(&port->dev, "SS::%p LSR:%x\n", mos7720_port, data); 901 902 write_mos_reg(serial, dummy, MOS7720_SP1_REG, 0x02); 903 write_mos_reg(serial, dummy, MOS7720_SP2_REG, 0x02); 904 905 write_mos_reg(serial, port_number, MOS7720_IER, 0x00); 906 write_mos_reg(serial, port_number, MOS7720_FCR, 0x00); 907 908 write_mos_reg(serial, port_number, MOS7720_FCR, 0xcf); 909 mos7720_port->shadowLCR = 0x03; 910 write_mos_reg(serial, port_number, MOS7720_LCR, 911 mos7720_port->shadowLCR); 912 mos7720_port->shadowMCR = 0x0b; 913 write_mos_reg(serial, port_number, MOS7720_MCR, 914 mos7720_port->shadowMCR); 915 916 write_mos_reg(serial, port_number, MOS7720_SP_CONTROL_REG, 0x00); 917 read_mos_reg(serial, dummy, MOS7720_SP_CONTROL_REG, &data); 918 data = data | (port->port_number + 1); 919 write_mos_reg(serial, dummy, MOS7720_SP_CONTROL_REG, data); 920 mos7720_port->shadowLCR = 0x83; 921 write_mos_reg(serial, port_number, MOS7720_LCR, 922 mos7720_port->shadowLCR); 923 write_mos_reg(serial, port_number, MOS7720_THR, 0x0c); 924 write_mos_reg(serial, port_number, MOS7720_IER, 0x00); 925 mos7720_port->shadowLCR = 0x03; 926 write_mos_reg(serial, port_number, MOS7720_LCR, 927 mos7720_port->shadowLCR); 928 write_mos_reg(serial, port_number, MOS7720_IER, 0x0c); 929 930 response = usb_submit_urb(port->read_urb, GFP_KERNEL); 931 if (response) 932 dev_err(&port->dev, "%s - Error %d submitting read urb\n", 933 __func__, response); 934 935 /* initialize our port settings */ 936 mos7720_port->shadowMCR = UART_MCR_OUT2; /* Must set to enable ints! */ 937 938 /* send a open port command */ 939 mos7720_port->open = 1; 940 941 return 0; 942 } 943 944 /* 945 * mos7720_chars_in_buffer 946 * this function is called by the tty driver when it wants to know how many 947 * bytes of data we currently have outstanding in the port (data that has 948 * been written, but hasn't made it out the port yet) 949 */ 950 static unsigned int mos7720_chars_in_buffer(struct tty_struct *tty) 951 { 952 struct usb_serial_port *port = tty->driver_data; 953 struct moschip_port *mos7720_port = usb_get_serial_port_data(port); 954 int i; 955 unsigned int chars = 0; 956 957 for (i = 0; i < NUM_URBS; ++i) { 958 if (mos7720_port->write_urb_pool[i] && 959 mos7720_port->write_urb_pool[i]->status == -EINPROGRESS) 960 chars += URB_TRANSFER_BUFFER_SIZE; 961 } 962 dev_dbg(&port->dev, "%s - returns %u\n", __func__, chars); 963 return chars; 964 } 965 966 static void mos7720_close(struct usb_serial_port *port) 967 { 968 struct usb_serial *serial; 969 struct moschip_port *mos7720_port; 970 int j; 971 972 serial = port->serial; 973 974 mos7720_port = usb_get_serial_port_data(port); 975 if (mos7720_port == NULL) 976 return; 977 978 for (j = 0; j < NUM_URBS; ++j) 979 usb_kill_urb(mos7720_port->write_urb_pool[j]); 980 981 /* Freeing Write URBs */ 982 for (j = 0; j < NUM_URBS; ++j) { 983 if (mos7720_port->write_urb_pool[j]) { 984 kfree(mos7720_port->write_urb_pool[j]->transfer_buffer); 985 usb_free_urb(mos7720_port->write_urb_pool[j]); 986 } 987 } 988 989 /* While closing port, shutdown all bulk read, write * 990 * and interrupt read if they exists, otherwise nop */ 991 usb_kill_urb(port->write_urb); 992 usb_kill_urb(port->read_urb); 993 994 write_mos_reg(serial, port->port_number, MOS7720_MCR, 0x00); 995 write_mos_reg(serial, port->port_number, MOS7720_IER, 0x00); 996 997 mos7720_port->open = 0; 998 } 999 1000 static int mos7720_break(struct tty_struct *tty, int break_state) 1001 { 1002 struct usb_serial_port *port = tty->driver_data; 1003 unsigned char data; 1004 struct usb_serial *serial; 1005 struct moschip_port *mos7720_port; 1006 1007 serial = port->serial; 1008 1009 mos7720_port = usb_get_serial_port_data(port); 1010 if (mos7720_port == NULL) 1011 return -ENODEV; 1012 1013 if (break_state == -1) 1014 data = mos7720_port->shadowLCR | UART_LCR_SBC; 1015 else 1016 data = mos7720_port->shadowLCR & ~UART_LCR_SBC; 1017 1018 mos7720_port->shadowLCR = data; 1019 1020 return write_mos_reg(serial, port->port_number, MOS7720_LCR, 1021 mos7720_port->shadowLCR); 1022 } 1023 1024 /* 1025 * mos7720_write_room 1026 * this function is called by the tty driver when it wants to know how many 1027 * bytes of data we can accept for a specific port. 1028 */ 1029 static unsigned int mos7720_write_room(struct tty_struct *tty) 1030 { 1031 struct usb_serial_port *port = tty->driver_data; 1032 struct moschip_port *mos7720_port = usb_get_serial_port_data(port); 1033 unsigned int room = 0; 1034 int i; 1035 1036 /* FIXME: Locking */ 1037 for (i = 0; i < NUM_URBS; ++i) { 1038 if (mos7720_port->write_urb_pool[i] && 1039 mos7720_port->write_urb_pool[i]->status != -EINPROGRESS) 1040 room += URB_TRANSFER_BUFFER_SIZE; 1041 } 1042 1043 dev_dbg(&port->dev, "%s - returns %u\n", __func__, room); 1044 return room; 1045 } 1046 1047 static int mos7720_write(struct tty_struct *tty, struct usb_serial_port *port, 1048 const unsigned char *data, int count) 1049 { 1050 int status; 1051 int i; 1052 int bytes_sent = 0; 1053 int transfer_size; 1054 1055 struct moschip_port *mos7720_port; 1056 struct usb_serial *serial; 1057 struct urb *urb; 1058 const unsigned char *current_position = data; 1059 1060 serial = port->serial; 1061 1062 mos7720_port = usb_get_serial_port_data(port); 1063 if (mos7720_port == NULL) 1064 return -ENODEV; 1065 1066 /* try to find a free urb in the list */ 1067 urb = NULL; 1068 1069 for (i = 0; i < NUM_URBS; ++i) { 1070 if (mos7720_port->write_urb_pool[i] && 1071 mos7720_port->write_urb_pool[i]->status != -EINPROGRESS) { 1072 urb = mos7720_port->write_urb_pool[i]; 1073 dev_dbg(&port->dev, "URB:%d\n", i); 1074 break; 1075 } 1076 } 1077 1078 if (urb == NULL) { 1079 dev_dbg(&port->dev, "%s - no more free urbs\n", __func__); 1080 goto exit; 1081 } 1082 1083 if (urb->transfer_buffer == NULL) { 1084 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE, 1085 GFP_ATOMIC); 1086 if (!urb->transfer_buffer) { 1087 bytes_sent = -ENOMEM; 1088 goto exit; 1089 } 1090 } 1091 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE); 1092 1093 memcpy(urb->transfer_buffer, current_position, transfer_size); 1094 usb_serial_debug_data(&port->dev, __func__, transfer_size, 1095 urb->transfer_buffer); 1096 1097 /* fill urb with data and submit */ 1098 usb_fill_bulk_urb(urb, serial->dev, 1099 usb_sndbulkpipe(serial->dev, 1100 port->bulk_out_endpointAddress), 1101 urb->transfer_buffer, transfer_size, 1102 mos7720_bulk_out_data_callback, mos7720_port); 1103 1104 /* send it down the pipe */ 1105 status = usb_submit_urb(urb, GFP_ATOMIC); 1106 if (status) { 1107 dev_err_console(port, "%s - usb_submit_urb(write bulk) failed " 1108 "with status = %d\n", __func__, status); 1109 bytes_sent = status; 1110 goto exit; 1111 } 1112 bytes_sent = transfer_size; 1113 1114 exit: 1115 return bytes_sent; 1116 } 1117 1118 static void mos7720_throttle(struct tty_struct *tty) 1119 { 1120 struct usb_serial_port *port = tty->driver_data; 1121 struct moschip_port *mos7720_port; 1122 int status; 1123 1124 mos7720_port = usb_get_serial_port_data(port); 1125 1126 if (mos7720_port == NULL) 1127 return; 1128 1129 if (!mos7720_port->open) { 1130 dev_dbg(&port->dev, "%s - port not opened\n", __func__); 1131 return; 1132 } 1133 1134 /* if we are implementing XON/XOFF, send the stop character */ 1135 if (I_IXOFF(tty)) { 1136 unsigned char stop_char = STOP_CHAR(tty); 1137 status = mos7720_write(tty, port, &stop_char, 1); 1138 if (status <= 0) 1139 return; 1140 } 1141 1142 /* if we are implementing RTS/CTS, toggle that line */ 1143 if (C_CRTSCTS(tty)) { 1144 mos7720_port->shadowMCR &= ~UART_MCR_RTS; 1145 write_mos_reg(port->serial, port->port_number, MOS7720_MCR, 1146 mos7720_port->shadowMCR); 1147 } 1148 } 1149 1150 static void mos7720_unthrottle(struct tty_struct *tty) 1151 { 1152 struct usb_serial_port *port = tty->driver_data; 1153 struct moschip_port *mos7720_port = usb_get_serial_port_data(port); 1154 int status; 1155 1156 if (mos7720_port == NULL) 1157 return; 1158 1159 if (!mos7720_port->open) { 1160 dev_dbg(&port->dev, "%s - port not opened\n", __func__); 1161 return; 1162 } 1163 1164 /* if we are implementing XON/XOFF, send the start character */ 1165 if (I_IXOFF(tty)) { 1166 unsigned char start_char = START_CHAR(tty); 1167 status = mos7720_write(tty, port, &start_char, 1); 1168 if (status <= 0) 1169 return; 1170 } 1171 1172 /* if we are implementing RTS/CTS, toggle that line */ 1173 if (C_CRTSCTS(tty)) { 1174 mos7720_port->shadowMCR |= UART_MCR_RTS; 1175 write_mos_reg(port->serial, port->port_number, MOS7720_MCR, 1176 mos7720_port->shadowMCR); 1177 } 1178 } 1179 1180 /* FIXME: this function does not work */ 1181 static int set_higher_rates(struct moschip_port *mos7720_port, 1182 unsigned int baud) 1183 { 1184 struct usb_serial_port *port; 1185 struct usb_serial *serial; 1186 int port_number; 1187 enum mos_regs sp_reg; 1188 if (mos7720_port == NULL) 1189 return -EINVAL; 1190 1191 port = mos7720_port->port; 1192 serial = port->serial; 1193 1194 /*********************************************** 1195 * Init Sequence for higher rates 1196 ***********************************************/ 1197 dev_dbg(&port->dev, "Sending Setting Commands ..........\n"); 1198 port_number = port->port_number; 1199 1200 write_mos_reg(serial, port_number, MOS7720_IER, 0x00); 1201 write_mos_reg(serial, port_number, MOS7720_FCR, 0x00); 1202 write_mos_reg(serial, port_number, MOS7720_FCR, 0xcf); 1203 mos7720_port->shadowMCR = 0x0b; 1204 write_mos_reg(serial, port_number, MOS7720_MCR, 1205 mos7720_port->shadowMCR); 1206 write_mos_reg(serial, dummy, MOS7720_SP_CONTROL_REG, 0x00); 1207 1208 /*********************************************** 1209 * Set for higher rates * 1210 ***********************************************/ 1211 /* writing baud rate verbatum into uart clock field clearly not right */ 1212 if (port_number == 0) 1213 sp_reg = MOS7720_SP1_REG; 1214 else 1215 sp_reg = MOS7720_SP2_REG; 1216 write_mos_reg(serial, dummy, sp_reg, baud * 0x10); 1217 write_mos_reg(serial, dummy, MOS7720_SP_CONTROL_REG, 0x03); 1218 mos7720_port->shadowMCR = 0x2b; 1219 write_mos_reg(serial, port_number, MOS7720_MCR, 1220 mos7720_port->shadowMCR); 1221 1222 /*********************************************** 1223 * Set DLL/DLM 1224 ***********************************************/ 1225 mos7720_port->shadowLCR = mos7720_port->shadowLCR | UART_LCR_DLAB; 1226 write_mos_reg(serial, port_number, MOS7720_LCR, 1227 mos7720_port->shadowLCR); 1228 write_mos_reg(serial, port_number, MOS7720_DLL, 0x01); 1229 write_mos_reg(serial, port_number, MOS7720_DLM, 0x00); 1230 mos7720_port->shadowLCR = mos7720_port->shadowLCR & ~UART_LCR_DLAB; 1231 write_mos_reg(serial, port_number, MOS7720_LCR, 1232 mos7720_port->shadowLCR); 1233 1234 return 0; 1235 } 1236 1237 /* baud rate information */ 1238 struct divisor_table_entry { 1239 __u32 baudrate; 1240 __u16 divisor; 1241 }; 1242 1243 /* Define table of divisors for moschip 7720 hardware * 1244 * These assume a 3.6864MHz crystal, the standard /16, and * 1245 * MCR.7 = 0. */ 1246 static const struct divisor_table_entry divisor_table[] = { 1247 { 50, 2304}, 1248 { 110, 1047}, /* 2094.545455 => 230450 => .0217 % over */ 1249 { 134, 857}, /* 1713.011152 => 230398.5 => .00065% under */ 1250 { 150, 768}, 1251 { 300, 384}, 1252 { 600, 192}, 1253 { 1200, 96}, 1254 { 1800, 64}, 1255 { 2400, 48}, 1256 { 4800, 24}, 1257 { 7200, 16}, 1258 { 9600, 12}, 1259 { 19200, 6}, 1260 { 38400, 3}, 1261 { 57600, 2}, 1262 { 115200, 1}, 1263 }; 1264 1265 /***************************************************************************** 1266 * calc_baud_rate_divisor 1267 * this function calculates the proper baud rate divisor for the specified 1268 * baud rate. 1269 *****************************************************************************/ 1270 static int calc_baud_rate_divisor(struct usb_serial_port *port, int baudrate, int *divisor) 1271 { 1272 int i; 1273 __u16 custom; 1274 __u16 round1; 1275 __u16 round; 1276 1277 1278 dev_dbg(&port->dev, "%s - %d\n", __func__, baudrate); 1279 1280 for (i = 0; i < ARRAY_SIZE(divisor_table); i++) { 1281 if (divisor_table[i].baudrate == baudrate) { 1282 *divisor = divisor_table[i].divisor; 1283 return 0; 1284 } 1285 } 1286 1287 /* After trying for all the standard baud rates * 1288 * Try calculating the divisor for this baud rate */ 1289 if (baudrate > 75 && baudrate < 230400) { 1290 /* get the divisor */ 1291 custom = (__u16)(230400L / baudrate); 1292 1293 /* Check for round off */ 1294 round1 = (__u16)(2304000L / baudrate); 1295 round = (__u16)(round1 - (custom * 10)); 1296 if (round > 4) 1297 custom++; 1298 *divisor = custom; 1299 1300 dev_dbg(&port->dev, "Baud %d = %d\n", baudrate, custom); 1301 return 0; 1302 } 1303 1304 dev_dbg(&port->dev, "Baud calculation Failed...\n"); 1305 return -EINVAL; 1306 } 1307 1308 /* 1309 * send_cmd_write_baud_rate 1310 * this function sends the proper command to change the baud rate of the 1311 * specified port. 1312 */ 1313 static int send_cmd_write_baud_rate(struct moschip_port *mos7720_port, 1314 int baudrate) 1315 { 1316 struct usb_serial_port *port; 1317 struct usb_serial *serial; 1318 int divisor; 1319 int status; 1320 unsigned char number; 1321 1322 if (mos7720_port == NULL) 1323 return -1; 1324 1325 port = mos7720_port->port; 1326 serial = port->serial; 1327 1328 number = port->port_number; 1329 dev_dbg(&port->dev, "%s - baud = %d\n", __func__, baudrate); 1330 1331 /* Calculate the Divisor */ 1332 status = calc_baud_rate_divisor(port, baudrate, &divisor); 1333 if (status) { 1334 dev_err(&port->dev, "%s - bad baud rate\n", __func__); 1335 return status; 1336 } 1337 1338 /* Enable access to divisor latch */ 1339 mos7720_port->shadowLCR = mos7720_port->shadowLCR | UART_LCR_DLAB; 1340 write_mos_reg(serial, number, MOS7720_LCR, mos7720_port->shadowLCR); 1341 1342 /* Write the divisor */ 1343 write_mos_reg(serial, number, MOS7720_DLL, (__u8)(divisor & 0xff)); 1344 write_mos_reg(serial, number, MOS7720_DLM, 1345 (__u8)((divisor & 0xff00) >> 8)); 1346 1347 /* Disable access to divisor latch */ 1348 mos7720_port->shadowLCR = mos7720_port->shadowLCR & ~UART_LCR_DLAB; 1349 write_mos_reg(serial, number, MOS7720_LCR, mos7720_port->shadowLCR); 1350 1351 return status; 1352 } 1353 1354 /* 1355 * change_port_settings 1356 * This routine is called to set the UART on the device to match 1357 * the specified new settings. 1358 */ 1359 static void change_port_settings(struct tty_struct *tty, 1360 struct moschip_port *mos7720_port, 1361 const struct ktermios *old_termios) 1362 { 1363 struct usb_serial_port *port; 1364 struct usb_serial *serial; 1365 int baud; 1366 unsigned cflag; 1367 __u8 lData; 1368 __u8 lParity; 1369 __u8 lStop; 1370 int status; 1371 int port_number; 1372 1373 if (mos7720_port == NULL) 1374 return ; 1375 1376 port = mos7720_port->port; 1377 serial = port->serial; 1378 port_number = port->port_number; 1379 1380 if (!mos7720_port->open) { 1381 dev_dbg(&port->dev, "%s - port not opened\n", __func__); 1382 return; 1383 } 1384 1385 lStop = 0x00; /* 1 stop bit */ 1386 lParity = 0x00; /* No parity */ 1387 1388 cflag = tty->termios.c_cflag; 1389 1390 lData = UART_LCR_WLEN(tty_get_char_size(cflag)); 1391 1392 /* Change the Parity bit */ 1393 if (cflag & PARENB) { 1394 if (cflag & PARODD) { 1395 lParity = UART_LCR_PARITY; 1396 dev_dbg(&port->dev, "%s - parity = odd\n", __func__); 1397 } else { 1398 lParity = (UART_LCR_EPAR | UART_LCR_PARITY); 1399 dev_dbg(&port->dev, "%s - parity = even\n", __func__); 1400 } 1401 1402 } else { 1403 dev_dbg(&port->dev, "%s - parity = none\n", __func__); 1404 } 1405 1406 if (cflag & CMSPAR) 1407 lParity = lParity | 0x20; 1408 1409 /* Change the Stop bit */ 1410 if (cflag & CSTOPB) { 1411 lStop = UART_LCR_STOP; 1412 dev_dbg(&port->dev, "%s - stop bits = 2\n", __func__); 1413 } else { 1414 lStop = 0x00; 1415 dev_dbg(&port->dev, "%s - stop bits = 1\n", __func__); 1416 } 1417 1418 #define LCR_BITS_MASK 0x03 /* Mask for bits/char field */ 1419 #define LCR_STOP_MASK 0x04 /* Mask for stop bits field */ 1420 #define LCR_PAR_MASK 0x38 /* Mask for parity field */ 1421 1422 /* Update the LCR with the correct value */ 1423 mos7720_port->shadowLCR &= 1424 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK); 1425 mos7720_port->shadowLCR |= (lData | lParity | lStop); 1426 1427 1428 /* Disable Interrupts */ 1429 write_mos_reg(serial, port_number, MOS7720_IER, 0x00); 1430 write_mos_reg(serial, port_number, MOS7720_FCR, 0x00); 1431 write_mos_reg(serial, port_number, MOS7720_FCR, 0xcf); 1432 1433 /* Send the updated LCR value to the mos7720 */ 1434 write_mos_reg(serial, port_number, MOS7720_LCR, 1435 mos7720_port->shadowLCR); 1436 mos7720_port->shadowMCR = 0x0b; 1437 write_mos_reg(serial, port_number, MOS7720_MCR, 1438 mos7720_port->shadowMCR); 1439 1440 /* set up the MCR register and send it to the mos7720 */ 1441 mos7720_port->shadowMCR = UART_MCR_OUT2; 1442 if (cflag & CBAUD) 1443 mos7720_port->shadowMCR |= (UART_MCR_DTR | UART_MCR_RTS); 1444 1445 if (cflag & CRTSCTS) { 1446 mos7720_port->shadowMCR |= (UART_MCR_XONANY); 1447 /* To set hardware flow control to the specified * 1448 * serial port, in SP1/2_CONTROL_REG */ 1449 if (port_number) 1450 write_mos_reg(serial, dummy, MOS7720_SP_CONTROL_REG, 1451 0x01); 1452 else 1453 write_mos_reg(serial, dummy, MOS7720_SP_CONTROL_REG, 1454 0x02); 1455 1456 } else 1457 mos7720_port->shadowMCR &= ~(UART_MCR_XONANY); 1458 1459 write_mos_reg(serial, port_number, MOS7720_MCR, 1460 mos7720_port->shadowMCR); 1461 1462 /* Determine divisor based on baud rate */ 1463 baud = tty_get_baud_rate(tty); 1464 if (!baud) { 1465 /* pick a default, any default... */ 1466 dev_dbg(&port->dev, "Picked default baud...\n"); 1467 baud = 9600; 1468 } 1469 1470 if (baud >= 230400) { 1471 set_higher_rates(mos7720_port, baud); 1472 /* Enable Interrupts */ 1473 write_mos_reg(serial, port_number, MOS7720_IER, 0x0c); 1474 return; 1475 } 1476 1477 dev_dbg(&port->dev, "%s - baud rate = %d\n", __func__, baud); 1478 status = send_cmd_write_baud_rate(mos7720_port, baud); 1479 /* FIXME: needs to write actual resulting baud back not just 1480 blindly do so */ 1481 if (cflag & CBAUD) 1482 tty_encode_baud_rate(tty, baud, baud); 1483 /* Enable Interrupts */ 1484 write_mos_reg(serial, port_number, MOS7720_IER, 0x0c); 1485 1486 if (port->read_urb->status != -EINPROGRESS) { 1487 status = usb_submit_urb(port->read_urb, GFP_KERNEL); 1488 if (status) 1489 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n", status); 1490 } 1491 } 1492 1493 /* 1494 * mos7720_set_termios 1495 * this function is called by the tty driver when it wants to change the 1496 * termios structure. 1497 */ 1498 static void mos7720_set_termios(struct tty_struct *tty, 1499 struct usb_serial_port *port, 1500 const struct ktermios *old_termios) 1501 { 1502 int status; 1503 struct moschip_port *mos7720_port; 1504 1505 mos7720_port = usb_get_serial_port_data(port); 1506 1507 if (mos7720_port == NULL) 1508 return; 1509 1510 if (!mos7720_port->open) { 1511 dev_dbg(&port->dev, "%s - port not opened\n", __func__); 1512 return; 1513 } 1514 1515 /* change the port settings to the new ones specified */ 1516 change_port_settings(tty, mos7720_port, old_termios); 1517 1518 if (port->read_urb->status != -EINPROGRESS) { 1519 status = usb_submit_urb(port->read_urb, GFP_KERNEL); 1520 if (status) 1521 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n", status); 1522 } 1523 } 1524 1525 /* 1526 * get_lsr_info - get line status register info 1527 * 1528 * Purpose: Let user call ioctl() to get info when the UART physically 1529 * is emptied. On bus types like RS485, the transmitter must 1530 * release the bus after transmitting. This must be done when 1531 * the transmit shift register is empty, not be done when the 1532 * transmit holding register is empty. This functionality 1533 * allows an RS485 driver to be written in user space. 1534 */ 1535 static int get_lsr_info(struct tty_struct *tty, 1536 struct moschip_port *mos7720_port, unsigned int __user *value) 1537 { 1538 struct usb_serial_port *port = tty->driver_data; 1539 unsigned int result = 0; 1540 unsigned char data = 0; 1541 int port_number = port->port_number; 1542 int count; 1543 1544 count = mos7720_chars_in_buffer(tty); 1545 if (count == 0) { 1546 read_mos_reg(port->serial, port_number, MOS7720_LSR, &data); 1547 if ((data & (UART_LSR_TEMT | UART_LSR_THRE)) 1548 == (UART_LSR_TEMT | UART_LSR_THRE)) { 1549 dev_dbg(&port->dev, "%s -- Empty\n", __func__); 1550 result = TIOCSER_TEMT; 1551 } 1552 } 1553 if (copy_to_user(value, &result, sizeof(int))) 1554 return -EFAULT; 1555 return 0; 1556 } 1557 1558 static int mos7720_tiocmget(struct tty_struct *tty) 1559 { 1560 struct usb_serial_port *port = tty->driver_data; 1561 struct moschip_port *mos7720_port = usb_get_serial_port_data(port); 1562 unsigned int result = 0; 1563 unsigned int mcr ; 1564 unsigned int msr ; 1565 1566 mcr = mos7720_port->shadowMCR; 1567 msr = mos7720_port->shadowMSR; 1568 1569 result = ((mcr & UART_MCR_DTR) ? TIOCM_DTR : 0) /* 0x002 */ 1570 | ((mcr & UART_MCR_RTS) ? TIOCM_RTS : 0) /* 0x004 */ 1571 | ((msr & UART_MSR_CTS) ? TIOCM_CTS : 0) /* 0x020 */ 1572 | ((msr & UART_MSR_DCD) ? TIOCM_CAR : 0) /* 0x040 */ 1573 | ((msr & UART_MSR_RI) ? TIOCM_RI : 0) /* 0x080 */ 1574 | ((msr & UART_MSR_DSR) ? TIOCM_DSR : 0); /* 0x100 */ 1575 1576 return result; 1577 } 1578 1579 static int mos7720_tiocmset(struct tty_struct *tty, 1580 unsigned int set, unsigned int clear) 1581 { 1582 struct usb_serial_port *port = tty->driver_data; 1583 struct moschip_port *mos7720_port = usb_get_serial_port_data(port); 1584 unsigned int mcr ; 1585 1586 mcr = mos7720_port->shadowMCR; 1587 1588 if (set & TIOCM_RTS) 1589 mcr |= UART_MCR_RTS; 1590 if (set & TIOCM_DTR) 1591 mcr |= UART_MCR_DTR; 1592 if (set & TIOCM_LOOP) 1593 mcr |= UART_MCR_LOOP; 1594 1595 if (clear & TIOCM_RTS) 1596 mcr &= ~UART_MCR_RTS; 1597 if (clear & TIOCM_DTR) 1598 mcr &= ~UART_MCR_DTR; 1599 if (clear & TIOCM_LOOP) 1600 mcr &= ~UART_MCR_LOOP; 1601 1602 mos7720_port->shadowMCR = mcr; 1603 write_mos_reg(port->serial, port->port_number, MOS7720_MCR, 1604 mos7720_port->shadowMCR); 1605 1606 return 0; 1607 } 1608 1609 static int mos7720_ioctl(struct tty_struct *tty, 1610 unsigned int cmd, unsigned long arg) 1611 { 1612 struct usb_serial_port *port = tty->driver_data; 1613 struct moschip_port *mos7720_port; 1614 1615 mos7720_port = usb_get_serial_port_data(port); 1616 if (mos7720_port == NULL) 1617 return -ENODEV; 1618 1619 switch (cmd) { 1620 case TIOCSERGETLSR: 1621 dev_dbg(&port->dev, "%s TIOCSERGETLSR\n", __func__); 1622 return get_lsr_info(tty, mos7720_port, 1623 (unsigned int __user *)arg); 1624 } 1625 1626 return -ENOIOCTLCMD; 1627 } 1628 1629 static int mos7720_startup(struct usb_serial *serial) 1630 { 1631 struct usb_device *dev; 1632 char data; 1633 u16 product; 1634 int ret_val; 1635 1636 product = le16_to_cpu(serial->dev->descriptor.idProduct); 1637 dev = serial->dev; 1638 1639 if (product == MOSCHIP_DEVICE_ID_7715) { 1640 struct urb *urb = serial->port[0]->interrupt_in_urb; 1641 1642 urb->complete = mos7715_interrupt_callback; 1643 1644 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT 1645 ret_val = mos7715_parport_init(serial); 1646 if (ret_val < 0) 1647 return ret_val; 1648 #endif 1649 } 1650 /* start the interrupt urb */ 1651 ret_val = usb_submit_urb(serial->port[0]->interrupt_in_urb, GFP_KERNEL); 1652 if (ret_val) { 1653 dev_err(&dev->dev, "failed to submit interrupt urb: %d\n", 1654 ret_val); 1655 } 1656 1657 /* LSR For Port 1 */ 1658 read_mos_reg(serial, 0, MOS7720_LSR, &data); 1659 dev_dbg(&dev->dev, "LSR:%x\n", data); 1660 1661 return 0; 1662 } 1663 1664 static void mos7720_release(struct usb_serial *serial) 1665 { 1666 usb_kill_urb(serial->port[0]->interrupt_in_urb); 1667 1668 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT 1669 /* close the parallel port */ 1670 1671 if (le16_to_cpu(serial->dev->descriptor.idProduct) 1672 == MOSCHIP_DEVICE_ID_7715) { 1673 struct mos7715_parport *mos_parport = 1674 usb_get_serial_data(serial); 1675 1676 /* prevent NULL ptr dereference in port callbacks */ 1677 spin_lock(&release_lock); 1678 mos_parport->pp->private_data = NULL; 1679 spin_unlock(&release_lock); 1680 1681 /* wait for synchronous usb calls to return */ 1682 if (mos_parport->msg_pending) 1683 wait_for_completion_timeout(&mos_parport->syncmsg_compl, 1684 msecs_to_jiffies(MOS_WDR_TIMEOUT)); 1685 /* 1686 * If delayed work is currently scheduled, wait for it to 1687 * complete. This also implies barriers that ensure the 1688 * below serial clearing is not hoisted above the ->work. 1689 */ 1690 cancel_work_sync(&mos_parport->work); 1691 1692 parport_remove_port(mos_parport->pp); 1693 usb_set_serial_data(serial, NULL); 1694 mos_parport->serial = NULL; 1695 1696 parport_del_port(mos_parport->pp); 1697 1698 kref_put(&mos_parport->ref_count, destroy_mos_parport); 1699 } 1700 #endif 1701 } 1702 1703 static int mos7720_port_probe(struct usb_serial_port *port) 1704 { 1705 struct moschip_port *mos7720_port; 1706 1707 mos7720_port = kzalloc_obj(*mos7720_port); 1708 if (!mos7720_port) 1709 return -ENOMEM; 1710 1711 mos7720_port->port = port; 1712 1713 usb_set_serial_port_data(port, mos7720_port); 1714 1715 return 0; 1716 } 1717 1718 static void mos7720_port_remove(struct usb_serial_port *port) 1719 { 1720 struct moschip_port *mos7720_port; 1721 1722 mos7720_port = usb_get_serial_port_data(port); 1723 kfree(mos7720_port); 1724 } 1725 1726 static struct usb_serial_driver moschip7720_2port_driver = { 1727 .driver = { 1728 .name = "moschip7720", 1729 }, 1730 .description = "Moschip 2 port adapter", 1731 .id_table = id_table, 1732 .num_bulk_in = 2, 1733 .num_bulk_out = 2, 1734 .num_interrupt_in = 1, 1735 .calc_num_ports = mos77xx_calc_num_ports, 1736 .open = mos7720_open, 1737 .close = mos7720_close, 1738 .throttle = mos7720_throttle, 1739 .unthrottle = mos7720_unthrottle, 1740 .attach = mos7720_startup, 1741 .release = mos7720_release, 1742 .port_probe = mos7720_port_probe, 1743 .port_remove = mos7720_port_remove, 1744 .ioctl = mos7720_ioctl, 1745 .tiocmget = mos7720_tiocmget, 1746 .tiocmset = mos7720_tiocmset, 1747 .set_termios = mos7720_set_termios, 1748 .write = mos7720_write, 1749 .write_room = mos7720_write_room, 1750 .chars_in_buffer = mos7720_chars_in_buffer, 1751 .break_ctl = mos7720_break, 1752 .read_bulk_callback = mos7720_bulk_in_callback, 1753 .read_int_callback = mos7720_interrupt_callback, 1754 }; 1755 1756 static struct usb_serial_driver * const serial_drivers[] = { 1757 &moschip7720_2port_driver, NULL 1758 }; 1759 1760 module_usb_serial_driver(serial_drivers, id_table); 1761 1762 MODULE_AUTHOR(DRIVER_AUTHOR); 1763 MODULE_DESCRIPTION(DRIVER_DESC); 1764 MODULE_LICENSE("GPL v2"); 1765