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