linux/drivers/usb/serial/mos7720.c
<<
>>
Prefs
   1/*
   2 * mos7720.c
   3 *   Controls the Moschip 7720 usb to dual port serial convertor
   4 *
   5 * Copyright 2006 Moschip Semiconductor Tech. Ltd.
   6 *
   7 * This program is free software; you can redistribute it and/or modify
   8 * it under the terms of the GNU General Public License as published by
   9 * the Free Software Foundation, version 2 of the License.
  10 *
  11 * Developed by:
  12 *      Vijaya Kumar <vijaykumar.gn@gmail.com>
  13 *      Ajay Kumar <naanuajay@yahoo.com>
  14 *      Gurudeva <ngurudeva@yahoo.com>
  15 *
  16 * Cleaned up from the original by:
  17 *      Greg Kroah-Hartman <gregkh@suse.de>
  18 *
  19 * Originally based on drivers/usb/serial/io_edgeport.c which is:
  20 *      Copyright (C) 2000 Inside Out Networks, All rights reserved.
  21 *      Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
  22 */
  23#include <linux/kernel.h>
  24#include <linux/errno.h>
  25#include <linux/init.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 <asm/uaccess.h>
  37
  38
  39/*
  40 * Version Information
  41 */
  42#define DRIVER_VERSION "1.0.0.4F"
  43#define DRIVER_AUTHOR "Aspire Communications pvt Ltd."
  44#define DRIVER_DESC "Moschip USB Serial Driver"
  45
  46/* default urb timeout */
  47#define MOS_WDR_TIMEOUT (HZ * 5)
  48
  49#define MOS_PORT1       0x0200
  50#define MOS_PORT2       0x0300
  51#define MOS_VENREG      0x0000
  52#define MOS_MAX_PORT    0x02
  53#define MOS_WRITE       0x0E
  54#define MOS_READ        0x0D
  55
  56/* Interrupt Rotinue Defines    */
  57#define SERIAL_IIR_RLS  0x06
  58#define SERIAL_IIR_RDA  0x04
  59#define SERIAL_IIR_CTI  0x0c
  60#define SERIAL_IIR_THR  0x02
  61#define SERIAL_IIR_MS   0x00
  62
  63#define NUM_URBS                        16      /* URB Count */
  64#define URB_TRANSFER_BUFFER_SIZE        32      /* URB Size */
  65
  66/* This structure holds all of the local port information */
  67struct moschip_port
  68{
  69        __u8    shadowLCR;              /* last LCR value received */
  70        __u8    shadowMCR;              /* last MCR value received */
  71        __u8    shadowMSR;              /* last MSR value received */
  72        char                    open;
  73        struct async_icount     icount;
  74        struct usb_serial_port  *port;  /* loop back to the owner */
  75        struct urb              *write_urb_pool[NUM_URBS];
  76};
  77
  78/* This structure holds all of the individual serial device information */
  79struct moschip_serial
  80{
  81        int interrupt_started;
  82};
  83
  84static int debug;
  85
  86#define USB_VENDOR_ID_MOSCHIP           0x9710
  87#define MOSCHIP_DEVICE_ID_7720          0x7720
  88#define MOSCHIP_DEVICE_ID_7715          0x7715
  89
  90static struct usb_device_id moschip_port_id_table [] = {
  91        { USB_DEVICE(USB_VENDOR_ID_MOSCHIP,MOSCHIP_DEVICE_ID_7720) },
  92        { } /* terminating entry */
  93};
  94MODULE_DEVICE_TABLE(usb, moschip_port_id_table);
  95
  96
  97/*
  98 * mos7720_interrupt_callback
  99 *      this is the callback function for when we have received data on the
 100 *      interrupt endpoint.
 101 */
 102static void mos7720_interrupt_callback(struct urb *urb)
 103{
 104        int result;
 105        int length;
 106        int status = urb->status;
 107        __u8 *data;
 108        __u8 sp1;
 109        __u8 sp2;
 110
 111        dbg("%s"," : Entering\n");
 112
 113        switch (status) {
 114        case 0:
 115                /* success */
 116                break;
 117        case -ECONNRESET:
 118        case -ENOENT:
 119        case -ESHUTDOWN:
 120                /* this urb is terminated, clean up */
 121                dbg("%s - urb shutting down with status: %d", __FUNCTION__,
 122                    status);
 123                return;
 124        default:
 125                dbg("%s - nonzero urb status received: %d", __FUNCTION__,
 126                    status);
 127                goto exit;
 128        }
 129
 130        length = urb->actual_length;
 131        data = urb->transfer_buffer;
 132
 133        /* Moschip get 4 bytes
 134         * Byte 1 IIR Port 1 (port.number is 0)
 135         * Byte 2 IIR Port 2 (port.number is 1)
 136         * Byte 3 --------------
 137         * Byte 4 FIFO status for both */
 138
 139        /* the above description is inverted
 140         *      oneukum 2007-03-14 */
 141
 142        if (unlikely(length != 4)) {
 143                dbg("Wrong data !!!");
 144                return;
 145        }
 146
 147        sp1 = data[3];
 148        sp2 = data[2];
 149
 150        if ((sp1 | sp2) & 0x01) {
 151                /* No Interrupt Pending in both the ports */
 152                dbg("No Interrupt !!!");
 153        } else {
 154                switch (sp1 & 0x0f) {
 155                case SERIAL_IIR_RLS:
 156                        dbg("Serial Port 1: Receiver status error or address "
 157                            "bit detected in 9-bit mode\n");
 158                        break;
 159                case SERIAL_IIR_CTI:
 160                        dbg("Serial Port 1: Receiver time out");
 161                        break;
 162                case SERIAL_IIR_MS:
 163                        dbg("Serial Port 1: Modem status change");
 164                        break;
 165                }
 166
 167                switch (sp2 & 0x0f) {
 168                case SERIAL_IIR_RLS:
 169                        dbg("Serial Port 2: Receiver status error or address "
 170                            "bit detected in 9-bit mode");
 171                        break;
 172                case SERIAL_IIR_CTI:
 173                        dbg("Serial Port 2: Receiver time out");
 174                        break;
 175                case SERIAL_IIR_MS:
 176                        dbg("Serial Port 2: Modem status change");
 177                        break;
 178                }
 179        }
 180
 181exit:
 182        result = usb_submit_urb(urb, GFP_ATOMIC);
 183        if (result)
 184                dev_err(&urb->dev->dev,
 185                        "%s - Error %d submitting control urb\n",
 186                        __FUNCTION__, result);
 187        return;
 188}
 189
 190/*
 191 * mos7720_bulk_in_callback
 192 *      this is the callback function for when we have received data on the
 193 *      bulk in endpoint.
 194 */
 195static void mos7720_bulk_in_callback(struct urb *urb)
 196{
 197        int retval;
 198        unsigned char *data ;
 199        struct usb_serial_port *port;
 200        struct moschip_port *mos7720_port;
 201        struct tty_struct *tty;
 202        int status = urb->status;
 203
 204        if (status) {
 205                dbg("nonzero read bulk status received: %d", status);
 206                return;
 207        }
 208
 209        mos7720_port = urb->context;
 210        if (!mos7720_port) {
 211                dbg("%s","NULL mos7720_port pointer \n");
 212                return ;
 213        }
 214
 215        port = mos7720_port->port;
 216
 217        dbg("Entering...%s", __FUNCTION__);
 218
 219        data = urb->transfer_buffer;
 220
 221        tty = port->tty;
 222        if (tty && urb->actual_length) {
 223                tty_buffer_request_room(tty, urb->actual_length);
 224                tty_insert_flip_string(tty, data, urb->actual_length);
 225                tty_flip_buffer_push(tty);
 226        }
 227
 228        if (!port->read_urb) {
 229                dbg("URB KILLED !!!");
 230                return;
 231        }
 232
 233        if (port->read_urb->status != -EINPROGRESS) {
 234                port->read_urb->dev = port->serial->dev;
 235
 236                retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
 237                if (retval)
 238                        dbg("usb_submit_urb(read bulk) failed, retval = %d",
 239                            retval);
 240        }
 241}
 242
 243/*
 244 * mos7720_bulk_out_data_callback
 245 *      this is the callback function for when we have finished sending serial
 246 *      data on the bulk out endpoint.
 247 */
 248static void mos7720_bulk_out_data_callback(struct urb *urb)
 249{
 250        struct moschip_port *mos7720_port;
 251        struct tty_struct *tty;
 252        int status = urb->status;
 253
 254        if (status) {
 255                dbg("nonzero write bulk status received:%d", status);
 256                return;
 257        }
 258
 259        mos7720_port = urb->context;
 260        if (!mos7720_port) {
 261                dbg("NULL mos7720_port pointer");
 262                return ;
 263        }
 264
 265        dbg("Entering .........");
 266
 267        tty = mos7720_port->port->tty;
 268
 269        if (tty && mos7720_port->open)
 270                tty_wakeup(tty);
 271}
 272
 273/*
 274 * send_mos_cmd
 275 *      this function will be used for sending command to device
 276 */
 277static int send_mos_cmd(struct usb_serial *serial, __u8 request, __u16 value,
 278                        __u16 index, void *data)
 279{
 280        int status;
 281        unsigned int pipe;
 282        u16 product = le16_to_cpu(serial->dev->descriptor.idProduct);
 283        __u8 requesttype;
 284        __u16 size = 0x0000;
 285
 286        if (value < MOS_MAX_PORT) {
 287                if (product == MOSCHIP_DEVICE_ID_7715) {
 288                        value = value*0x100+0x100;
 289                } else {
 290                        value = value*0x100+0x200;
 291                }
 292        } else {
 293                value = 0x0000;
 294                if ((product == MOSCHIP_DEVICE_ID_7715) &&
 295                    (index != 0x08)) {
 296                        dbg("serial->product== MOSCHIP_DEVICE_ID_7715");
 297                        //index = 0x01 ;
 298                }
 299        }
 300
 301        if (request == MOS_WRITE) {
 302                request = (__u8)MOS_WRITE;
 303                requesttype = (__u8)0x40;
 304                value  = value + (__u16)*((unsigned char *)data);
 305                data = NULL;
 306                pipe = usb_sndctrlpipe(serial->dev, 0);
 307        } else {
 308                request = (__u8)MOS_READ;
 309                requesttype = (__u8)0xC0;
 310                size = 0x01;
 311                pipe = usb_rcvctrlpipe(serial->dev,0);
 312        }
 313
 314        status = usb_control_msg(serial->dev, pipe, request, requesttype,
 315                                 value, index, data, size, MOS_WDR_TIMEOUT);
 316
 317        if (status < 0)
 318                dbg("Command Write failed Value %x index %x\n",value,index);
 319
 320        return status;
 321}
 322
 323static int mos7720_open(struct usb_serial_port *port, struct file * filp)
 324{
 325        struct usb_serial *serial;
 326        struct usb_serial_port *port0;
 327        struct urb *urb;
 328        struct moschip_serial *mos7720_serial;
 329        struct moschip_port *mos7720_port;
 330        int response;
 331        int port_number;
 332        char data;
 333        int allocated_urbs = 0;
 334        int j;
 335
 336        serial = port->serial;
 337
 338        mos7720_port = usb_get_serial_port_data(port);
 339        if (mos7720_port == NULL)
 340                return -ENODEV;
 341
 342        port0 = serial->port[0];
 343
 344        mos7720_serial = usb_get_serial_data(serial);
 345
 346        if (mos7720_serial == NULL || port0 == NULL)
 347                return -ENODEV;
 348
 349        usb_clear_halt(serial->dev, port->write_urb->pipe);
 350        usb_clear_halt(serial->dev, port->read_urb->pipe);
 351
 352        /* Initialising the write urb pool */
 353        for (j = 0; j < NUM_URBS; ++j) {
 354                urb = usb_alloc_urb(0,GFP_KERNEL);
 355                mos7720_port->write_urb_pool[j] = urb;
 356
 357                if (urb == NULL) {
 358                        err("No more urbs???");
 359                        continue;
 360                }
 361
 362                urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
 363                                               GFP_KERNEL);
 364                if (!urb->transfer_buffer) {
 365                        err("%s-out of memory for urb buffers.", __FUNCTION__);
 366                        usb_free_urb(mos7720_port->write_urb_pool[j]);
 367                        mos7720_port->write_urb_pool[j] = NULL;
 368                        continue;
 369                }
 370                allocated_urbs++;
 371        }
 372
 373        if (!allocated_urbs)
 374                return -ENOMEM;
 375
 376         /* Initialize MCS7720 -- Write Init values to corresponding Registers
 377          *
 378          * Register Index
 379          * 1 : IER
 380          * 2 : FCR
 381          * 3 : LCR
 382          * 4 : MCR
 383          *
 384          * 0x08 : SP1/2 Control Reg
 385          */
 386        port_number = port->number - port->serial->minor;
 387        send_mos_cmd(port->serial, MOS_READ, port_number, UART_LSR, &data);
 388        dbg("SS::%p LSR:%x\n",mos7720_port, data);
 389
 390        dbg("Check:Sending Command ..........");
 391
 392        data = 0x02;
 393        send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x01, &data);
 394        data = 0x02;
 395        send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x02, &data);
 396
 397        data = 0x00;
 398        send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
 399        data = 0x00;
 400        send_mos_cmd(serial, MOS_WRITE, port_number, 0x02, &data);
 401
 402        data = 0xCF;
 403        send_mos_cmd(serial, MOS_WRITE, port_number, 0x02, &data);
 404        data = 0x03;
 405        mos7720_port->shadowLCR  = data;
 406        send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
 407        data = 0x0b;
 408        mos7720_port->shadowMCR  = data;
 409        send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
 410        data = 0x0b;
 411        send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
 412
 413        data = 0x00;
 414        send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
 415        data = 0x00;
 416        send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
 417
 418/*      data = 0x00;
 419        send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, port_number + 1, &data);
 420        data = 0x03;
 421        send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, port_number + 1, &data);
 422        data = 0x00;
 423        send_mos_cmd(port->serial, MOS_WRITE, MOS_MAX_PORT, port_number + 1, &data);
 424*/
 425        data = 0x00;
 426        send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
 427
 428        data = data | (port->number - port->serial->minor + 1);
 429        send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
 430
 431        data = 0x83;
 432        mos7720_port->shadowLCR  = data;
 433        send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
 434        data = 0x0c;
 435        send_mos_cmd(serial, MOS_WRITE, port_number, 0x00, &data);
 436        data = 0x00;
 437        send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
 438        data = 0x03;
 439        mos7720_port->shadowLCR  = data;
 440        send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
 441        data = 0x0c;
 442        send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
 443        data = 0x0c;
 444        send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
 445
 446//Matrix
 447
 448        /* force low_latency on so that our tty_push actually forces *
 449         * the data through,otherwise it is scheduled, and with      *
 450         * high data rates (like with OHCI) data can get lost.       */
 451
 452        if (port->tty)
 453                port->tty->low_latency = 1;
 454
 455        /* see if we've set up our endpoint info yet   *
 456         * (can't set it up in mos7720_startup as the  *
 457         * structures were not set up at that time.)   */
 458        if (!mos7720_serial->interrupt_started) {
 459                dbg("Interrupt buffer NULL !!!");
 460
 461                /* not set up yet, so do it now */
 462                mos7720_serial->interrupt_started = 1;
 463
 464                dbg("To Submit URB !!!");
 465
 466                /* set up our interrupt urb */
 467                usb_fill_int_urb(port0->interrupt_in_urb, serial->dev,
 468                                 usb_rcvintpipe(serial->dev,
 469                                                port->interrupt_in_endpointAddress),
 470                                 port0->interrupt_in_buffer,
 471                                 port0->interrupt_in_urb->transfer_buffer_length,
 472                                 mos7720_interrupt_callback, mos7720_port,
 473                                 port0->interrupt_in_urb->interval);
 474
 475                /* start interrupt read for this mos7720 this interrupt *
 476                 * will continue as long as the mos7720 is connected    */
 477                dbg("Submit URB over !!!");
 478                response = usb_submit_urb(port0->interrupt_in_urb, GFP_KERNEL);
 479                if (response)
 480                        dev_err(&port->dev,
 481                                "%s - Error %d submitting control urb\n",
 482                                __FUNCTION__, response);
 483        }
 484
 485        /* set up our bulk in urb */
 486        usb_fill_bulk_urb(port->read_urb, serial->dev,
 487                          usb_rcvbulkpipe(serial->dev,
 488                                          port->bulk_in_endpointAddress),
 489                          port->bulk_in_buffer,
 490                          port->read_urb->transfer_buffer_length,
 491                          mos7720_bulk_in_callback, mos7720_port);
 492        response = usb_submit_urb(port->read_urb, GFP_KERNEL);
 493        if (response)
 494                dev_err(&port->dev,
 495                        "%s - Error %d submitting read urb\n", __FUNCTION__, response);
 496
 497        /* initialize our icount structure */
 498        memset(&(mos7720_port->icount), 0x00, sizeof(mos7720_port->icount));
 499
 500        /* initialize our port settings */
 501        mos7720_port->shadowMCR = UART_MCR_OUT2; /* Must set to enable ints! */
 502
 503        /* send a open port command */
 504        mos7720_port->open = 1;
 505
 506        return 0;
 507}
 508
 509/*
 510 * mos7720_chars_in_buffer
 511 *      this function is called by the tty driver when it wants to know how many
 512 *      bytes of data we currently have outstanding in the port (data that has
 513 *      been written, but hasn't made it out the port yet)
 514 *      If successful, we return the number of bytes left to be written in the
 515 *      system,
 516 *      Otherwise we return a negative error number.
 517 */
 518static int mos7720_chars_in_buffer(struct usb_serial_port *port)
 519{
 520        int i;
 521        int chars = 0;
 522        struct moschip_port *mos7720_port;
 523
 524        dbg("%s:entering ...........", __FUNCTION__);
 525
 526        mos7720_port = usb_get_serial_port_data(port);
 527        if (mos7720_port == NULL) {
 528                dbg("%s:leaving ...........", __FUNCTION__);
 529                return -ENODEV;
 530        }
 531
 532        for (i = 0; i < NUM_URBS; ++i) {
 533                if (mos7720_port->write_urb_pool[i] && mos7720_port->write_urb_pool[i]->status == -EINPROGRESS)
 534                        chars += URB_TRANSFER_BUFFER_SIZE;
 535        }
 536        dbg("%s - returns %d", __FUNCTION__, chars);
 537        return chars;
 538}
 539
 540static void mos7720_close(struct usb_serial_port *port, struct file *filp)
 541{
 542        struct usb_serial *serial;
 543        struct moschip_port *mos7720_port;
 544        char data;
 545        int j;
 546
 547        dbg("mos7720_close:entering...");
 548
 549        serial = port->serial;
 550
 551        mos7720_port = usb_get_serial_port_data(port);
 552        if (mos7720_port == NULL)
 553                return;
 554
 555        for (j = 0; j < NUM_URBS; ++j)
 556                usb_kill_urb(mos7720_port->write_urb_pool[j]);
 557
 558        /* Freeing Write URBs */
 559        for (j = 0; j < NUM_URBS; ++j) {
 560                if (mos7720_port->write_urb_pool[j]) {
 561                        kfree(mos7720_port->write_urb_pool[j]->transfer_buffer);
 562                        usb_free_urb(mos7720_port->write_urb_pool[j]);
 563                }
 564        }
 565
 566        /* While closing port, shutdown all bulk read, write  *
 567         * and interrupt read if they exists                  */
 568        if (serial->dev) {
 569                dbg("Shutdown bulk write");
 570                usb_kill_urb(port->write_urb);
 571                dbg("Shutdown bulk read");
 572                usb_kill_urb(port->read_urb);
 573        }
 574
 575        data = 0x00;
 576        send_mos_cmd(serial, MOS_WRITE, port->number - port->serial->minor,
 577                     0x04, &data);
 578
 579        data = 0x00;
 580        send_mos_cmd(serial, MOS_WRITE, port->number - port->serial->minor,
 581                     0x01, &data);
 582
 583        mos7720_port->open = 0;
 584
 585        dbg("Leaving %s", __FUNCTION__);
 586}
 587
 588static void mos7720_break(struct usb_serial_port *port, int break_state)
 589{
 590        unsigned char data;
 591        struct usb_serial *serial;
 592        struct moschip_port *mos7720_port;
 593
 594        dbg("Entering %s", __FUNCTION__);
 595
 596        serial = port->serial;
 597
 598        mos7720_port = usb_get_serial_port_data(port);
 599        if (mos7720_port == NULL)
 600                return;
 601
 602        if (break_state == -1)
 603                data = mos7720_port->shadowLCR | UART_LCR_SBC;
 604        else
 605                data = mos7720_port->shadowLCR & ~UART_LCR_SBC;
 606
 607        mos7720_port->shadowLCR  = data;
 608        send_mos_cmd(serial, MOS_WRITE, port->number - port->serial->minor,
 609                     0x03, &data);
 610
 611        return;
 612}
 613
 614/*
 615 * mos7720_write_room
 616 *      this function is called by the tty driver when it wants to know how many
 617 *      bytes of data we can accept for a specific port.
 618 *      If successful, we return the amount of room that we have for this port
 619 *      Otherwise we return a negative error number.
 620 */
 621static int mos7720_write_room(struct usb_serial_port *port)
 622{
 623        struct moschip_port *mos7720_port;
 624        int room = 0;
 625        int i;
 626
 627        dbg("%s:entering ...........", __FUNCTION__);
 628
 629        mos7720_port = usb_get_serial_port_data(port);
 630        if (mos7720_port == NULL) {
 631                dbg("%s:leaving ...........", __FUNCTION__);
 632                return -ENODEV;
 633        }
 634
 635        for (i = 0; i < NUM_URBS; ++i) {
 636                if (mos7720_port->write_urb_pool[i] && mos7720_port->write_urb_pool[i]->status != -EINPROGRESS)
 637                        room += URB_TRANSFER_BUFFER_SIZE;
 638        }
 639
 640        dbg("%s - returns %d", __FUNCTION__, room);
 641        return room;
 642}
 643
 644static int mos7720_write(struct usb_serial_port *port,
 645                         const unsigned char *data, int count)
 646{
 647        int status;
 648        int i;
 649        int bytes_sent = 0;
 650        int transfer_size;
 651
 652        struct moschip_port *mos7720_port;
 653        struct usb_serial *serial;
 654        struct urb    *urb;
 655        const unsigned char *current_position = data;
 656
 657        dbg("%s:entering ...........", __FUNCTION__);
 658
 659        serial = port->serial;
 660
 661        mos7720_port = usb_get_serial_port_data(port);
 662        if (mos7720_port == NULL) {
 663                dbg("mos7720_port is NULL");
 664                return -ENODEV;
 665        }
 666
 667        /* try to find a free urb in the list */
 668        urb = NULL;
 669
 670        for (i = 0; i < NUM_URBS; ++i) {
 671                if (mos7720_port->write_urb_pool[i] && mos7720_port->write_urb_pool[i]->status != -EINPROGRESS) {
 672                        urb = mos7720_port->write_urb_pool[i];
 673                        dbg("URB:%d",i);
 674                        break;
 675                }
 676        }
 677
 678        if (urb == NULL) {
 679                dbg("%s - no more free urbs", __FUNCTION__);
 680                goto exit;
 681        }
 682
 683        if (urb->transfer_buffer == NULL) {
 684                urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
 685                                               GFP_KERNEL);
 686                if (urb->transfer_buffer == NULL) {
 687                        err("%s no more kernel memory...", __FUNCTION__);
 688                        goto exit;
 689                }
 690        }
 691        transfer_size = min (count, URB_TRANSFER_BUFFER_SIZE);
 692
 693        memcpy(urb->transfer_buffer, current_position, transfer_size);
 694        usb_serial_debug_data(debug, &port->dev, __FUNCTION__, transfer_size,
 695                              urb->transfer_buffer);
 696
 697        /* fill urb with data and submit  */
 698        usb_fill_bulk_urb(urb, serial->dev,
 699                          usb_sndbulkpipe(serial->dev,
 700                                          port->bulk_out_endpointAddress),
 701                          urb->transfer_buffer, transfer_size,
 702                          mos7720_bulk_out_data_callback, mos7720_port);
 703
 704        /* send it down the pipe */
 705        status = usb_submit_urb(urb,GFP_ATOMIC);
 706        if (status) {
 707                err("%s - usb_submit_urb(write bulk) failed with status = %d",
 708                    __FUNCTION__, status);
 709                bytes_sent = status;
 710                goto exit;
 711        }
 712        bytes_sent = transfer_size;
 713
 714exit:
 715        return bytes_sent;
 716}
 717
 718static void mos7720_throttle(struct usb_serial_port *port)
 719{
 720        struct moschip_port *mos7720_port;
 721        struct tty_struct *tty;
 722        int status;
 723
 724        dbg("%s- port %d\n", __FUNCTION__, port->number);
 725
 726        mos7720_port = usb_get_serial_port_data(port);
 727
 728        if (mos7720_port == NULL)
 729                return;
 730
 731        if (!mos7720_port->open) {
 732                dbg("port not opened");
 733                return;
 734        }
 735
 736        dbg("%s: Entering ..........", __FUNCTION__);
 737
 738        tty = port->tty;
 739        if (!tty) {
 740                dbg("%s - no tty available", __FUNCTION__);
 741                return;
 742        }
 743
 744        /* if we are implementing XON/XOFF, send the stop character */
 745        if (I_IXOFF(tty)) {
 746                unsigned char stop_char = STOP_CHAR(tty);
 747                status = mos7720_write(port, &stop_char, 1);
 748                if (status <= 0)
 749                        return;
 750        }
 751
 752        /* if we are implementing RTS/CTS, toggle that line */
 753        if (tty->termios->c_cflag & CRTSCTS) {
 754                mos7720_port->shadowMCR &= ~UART_MCR_RTS;
 755                status = send_mos_cmd(port->serial, MOS_WRITE,
 756                                      port->number - port->serial->minor,
 757                                      UART_MCR, &mos7720_port->shadowMCR);
 758                if (status != 0)
 759                        return;
 760        }
 761}
 762
 763static void mos7720_unthrottle(struct usb_serial_port *port)
 764{
 765        struct tty_struct *tty;
 766        int status;
 767        struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
 768
 769        if (mos7720_port == NULL)
 770                return;
 771
 772        if (!mos7720_port->open) {
 773                dbg("%s - port not opened", __FUNCTION__);
 774                return;
 775        }
 776
 777        dbg("%s: Entering ..........", __FUNCTION__);
 778
 779        tty = port->tty;
 780        if (!tty) {
 781                dbg("%s - no tty available", __FUNCTION__);
 782                return;
 783        }
 784
 785        /* if we are implementing XON/XOFF, send the start character */
 786        if (I_IXOFF(tty)) {
 787                unsigned char start_char = START_CHAR(tty);
 788                status = mos7720_write(port, &start_char, 1);
 789                if (status <= 0)
 790                        return;
 791        }
 792
 793        /* if we are implementing RTS/CTS, toggle that line */
 794        if (tty->termios->c_cflag & CRTSCTS) {
 795                mos7720_port->shadowMCR |= UART_MCR_RTS;
 796                status = send_mos_cmd(port->serial, MOS_WRITE,
 797                                      port->number - port->serial->minor,
 798                                      UART_MCR, &mos7720_port->shadowMCR);
 799                if (status != 0)
 800                        return;
 801        }
 802}
 803
 804static int set_higher_rates(struct moschip_port *mos7720_port,
 805                            unsigned int baud)
 806{
 807        unsigned char data;
 808        struct usb_serial_port *port;
 809        struct usb_serial *serial;
 810        int port_number;
 811
 812        if (mos7720_port == NULL)
 813                return -EINVAL;
 814
 815        port = mos7720_port->port;
 816        serial = port->serial;
 817
 818        /***********************************************
 819         *      Init Sequence for higher rates
 820         ***********************************************/
 821        dbg("Sending Setting Commands ..........");
 822        port_number = port->number - port->serial->minor;
 823
 824        data = 0x000;
 825        send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
 826        data = 0x000;
 827        send_mos_cmd(serial, MOS_WRITE, port_number, 0x02, &data);
 828        data = 0x0CF;
 829        send_mos_cmd(serial, MOS_WRITE, port->number, 0x02, &data);
 830        data = 0x00b;
 831        mos7720_port->shadowMCR  = data;
 832        send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
 833        data = 0x00b;
 834        send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
 835
 836        data = 0x000;
 837        send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
 838        data = 0x000;
 839        send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
 840
 841
 842        /***********************************************
 843         *              Set for higher rates           *
 844         ***********************************************/
 845
 846        data = baud * 0x10;
 847        send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, port_number + 1,&data);
 848
 849        data = 0x003;
 850        send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
 851        data = 0x003;
 852        send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
 853
 854        data = 0x02b;
 855        mos7720_port->shadowMCR  = data;
 856        send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
 857        data = 0x02b;
 858        send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
 859
 860        /***********************************************
 861         *              Set DLL/DLM
 862         ***********************************************/
 863
 864        data = mos7720_port->shadowLCR | UART_LCR_DLAB;
 865        mos7720_port->shadowLCR  = data;
 866        send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
 867
 868        data =  0x001; /* DLL */
 869        send_mos_cmd(serial, MOS_WRITE, port_number, 0x00, &data);
 870        data =  0x000; /* DLM */
 871        send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
 872
 873        data = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
 874        mos7720_port->shadowLCR  = data;
 875        send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
 876
 877        return 0;
 878}
 879
 880/* baud rate information */
 881struct divisor_table_entry
 882{
 883        __u32  baudrate;
 884        __u16  divisor;
 885};
 886
 887/* Define table of divisors for moschip 7720 hardware      *
 888 * These assume a 3.6864MHz crystal, the standard /16, and *
 889 * MCR.7 = 0.                                              */
 890static struct divisor_table_entry divisor_table[] = {
 891        {   50,         2304},
 892        {   110,        1047},  /* 2094.545455 => 230450   => .0217 % over */
 893        {   134,        857},   /* 1713.011152 => 230398.5 => .00065% under */
 894        {   150,        768},
 895        {   300,        384},
 896        {   600,        192},
 897        {   1200,       96},
 898        {   1800,       64},
 899        {   2400,       48},
 900        {   4800,       24},
 901        {   7200,       16},
 902        {   9600,       12},
 903        {   19200,      6},
 904        {   38400,      3},
 905        {   57600,      2},
 906        {   115200,     1},
 907};
 908
 909/*****************************************************************************
 910 * calc_baud_rate_divisor
 911 *      this function calculates the proper baud rate divisor for the specified
 912 *      baud rate.
 913 *****************************************************************************/
 914static int calc_baud_rate_divisor(int baudrate, int *divisor)
 915{
 916        int i;
 917        __u16 custom;
 918        __u16 round1;
 919        __u16 round;
 920
 921
 922        dbg("%s - %d", __FUNCTION__, baudrate);
 923
 924        for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
 925                if (divisor_table[i].baudrate == baudrate) {
 926                        *divisor = divisor_table[i].divisor;
 927                        return 0;
 928                }
 929        }
 930
 931        /* After trying for all the standard baud rates    *
 932         * Try calculating the divisor for this baud rate  */
 933        if (baudrate > 75 &&  baudrate < 230400) {
 934                /* get the divisor */
 935                custom = (__u16)(230400L  / baudrate);
 936
 937                /* Check for round off */
 938                round1 = (__u16)(2304000L / baudrate);
 939                round = (__u16)(round1 - (custom * 10));
 940                if (round > 4)
 941                        custom++;
 942                *divisor = custom;
 943
 944                dbg("Baud %d = %d",baudrate, custom);
 945                return 0;
 946        }
 947
 948        dbg("Baud calculation Failed...");
 949        return -EINVAL;
 950}
 951
 952/*
 953 * send_cmd_write_baud_rate
 954 *      this function sends the proper command to change the baud rate of the
 955 *      specified port.
 956 */
 957static int send_cmd_write_baud_rate(struct moschip_port *mos7720_port,
 958                                    int baudrate)
 959{
 960        struct usb_serial_port *port;
 961        struct usb_serial *serial;
 962        int divisor;
 963        int status;
 964        unsigned char data;
 965        unsigned char number;
 966
 967        if (mos7720_port == NULL)
 968                return -1;
 969
 970        port = mos7720_port->port;
 971        serial = port->serial;
 972
 973        dbg("%s: Entering ..........", __FUNCTION__);
 974
 975        number = port->number - port->serial->minor;
 976        dbg("%s - port = %d, baud = %d", __FUNCTION__, port->number, baudrate);
 977
 978        /* Calculate the Divisor */
 979        status = calc_baud_rate_divisor(baudrate, &divisor);
 980        if (status) {
 981                err("%s - bad baud rate", __FUNCTION__);
 982                return status;
 983        }
 984
 985        /* Enable access to divisor latch */
 986        data = mos7720_port->shadowLCR | UART_LCR_DLAB;
 987        mos7720_port->shadowLCR  = data;
 988        send_mos_cmd(serial, MOS_WRITE, number, UART_LCR, &data);
 989
 990        /* Write the divisor */
 991        data = ((unsigned char)(divisor & 0xff));
 992        send_mos_cmd(serial, MOS_WRITE, number, 0x00, &data);
 993
 994        data = ((unsigned char)((divisor & 0xff00) >> 8));
 995        send_mos_cmd(serial, MOS_WRITE, number, 0x01, &data);
 996
 997        /* Disable access to divisor latch */
 998        data = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
 999        mos7720_port->shadowLCR = data;
1000        send_mos_cmd(serial, MOS_WRITE, number, 0x03, &data);
1001
1002        return status;
1003}
1004
1005/*
1006 * change_port_settings
1007 *      This routine is called to set the UART on the device to match
1008 *      the specified new settings.
1009 */
1010static void change_port_settings(struct moschip_port *mos7720_port,
1011                                 struct ktermios *old_termios)
1012{
1013        struct usb_serial_port *port;
1014        struct usb_serial *serial;
1015        struct tty_struct *tty;
1016        int baud;
1017        unsigned cflag;
1018        unsigned iflag;
1019        __u8 mask = 0xff;
1020        __u8 lData;
1021        __u8 lParity;
1022        __u8 lStop;
1023        int status;
1024        int port_number;
1025        char data;
1026
1027        if (mos7720_port == NULL)
1028                return ;
1029
1030        port = mos7720_port->port;
1031        serial = port->serial;
1032        port_number = port->number - port->serial->minor;
1033
1034        dbg("%s - port %d", __FUNCTION__, port->number);
1035
1036        if (!mos7720_port->open) {
1037                dbg("%s - port not opened", __FUNCTION__);
1038                return;
1039        }
1040
1041        tty = mos7720_port->port->tty;
1042
1043        if ((!tty) || (!tty->termios)) {
1044                dbg("%s - no tty structures", __FUNCTION__);
1045                return;
1046        }
1047
1048        dbg("%s: Entering ..........", __FUNCTION__);
1049
1050        lData = UART_LCR_WLEN8;
1051        lStop = 0x00;   /* 1 stop bit */
1052        lParity = 0x00; /* No parity */
1053
1054        cflag = tty->termios->c_cflag;
1055        iflag = tty->termios->c_iflag;
1056
1057        /* Change the number of bits */
1058        switch (cflag & CSIZE) {
1059        case CS5:
1060                lData = UART_LCR_WLEN5;
1061                mask = 0x1f;
1062                break;
1063
1064        case CS6:
1065                lData = UART_LCR_WLEN6;
1066                mask = 0x3f;
1067                break;
1068
1069        case CS7:
1070                lData = UART_LCR_WLEN7;
1071                mask = 0x7f;
1072                break;
1073        default:
1074        case CS8:
1075                lData = UART_LCR_WLEN8;
1076                break;
1077        }
1078
1079        /* Change the Parity bit */
1080        if (cflag & PARENB) {
1081                if (cflag & PARODD) {
1082                        lParity = UART_LCR_PARITY;
1083                        dbg("%s - parity = odd", __FUNCTION__);
1084                } else {
1085                        lParity = (UART_LCR_EPAR | UART_LCR_PARITY);
1086                        dbg("%s - parity = even", __FUNCTION__);
1087                }
1088
1089        } else {
1090                dbg("%s - parity = none", __FUNCTION__);
1091        }
1092
1093        if (cflag & CMSPAR)
1094                lParity = lParity | 0x20;
1095
1096        /* Change the Stop bit */
1097        if (cflag & CSTOPB) {
1098                lStop = UART_LCR_STOP;
1099                dbg("%s - stop bits = 2", __FUNCTION__);
1100        } else {
1101                lStop = 0x00;
1102                dbg("%s - stop bits = 1", __FUNCTION__);
1103        }
1104
1105#define LCR_BITS_MASK           0x03    /* Mask for bits/char field */
1106#define LCR_STOP_MASK           0x04    /* Mask for stop bits field */
1107#define LCR_PAR_MASK            0x38    /* Mask for parity field */
1108
1109        /* Update the LCR with the correct value */
1110        mos7720_port->shadowLCR &= ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
1111        mos7720_port->shadowLCR |= (lData | lParity | lStop);
1112
1113
1114        /* Disable Interrupts */
1115        data = 0x00;
1116        send_mos_cmd(serial,MOS_WRITE,port->number - port->serial->minor, UART_IER, &data);
1117
1118        data = 0x00;
1119        send_mos_cmd(serial, MOS_WRITE, port_number, UART_FCR, &data);
1120
1121        data = 0xcf;
1122        send_mos_cmd(serial, MOS_WRITE, port_number, UART_FCR, &data);
1123
1124        /* Send the updated LCR value to the mos7720 */
1125        data = mos7720_port->shadowLCR;
1126        send_mos_cmd(serial, MOS_WRITE, port_number, UART_LCR, &data);
1127
1128        data = 0x00b;
1129        mos7720_port->shadowMCR = data;
1130        send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
1131        data = 0x00b;
1132        send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
1133
1134        /* set up the MCR register and send it to the mos7720 */
1135        mos7720_port->shadowMCR = UART_MCR_OUT2;
1136        if (cflag & CBAUD)
1137                mos7720_port->shadowMCR |= (UART_MCR_DTR | UART_MCR_RTS);
1138
1139        if (cflag & CRTSCTS) {
1140                mos7720_port->shadowMCR |= (UART_MCR_XONANY);
1141
1142                /* To set hardware flow control to the specified *
1143                 * serial port, in SP1/2_CONTROL_REG             */
1144                if (port->number) {
1145                        data = 0x001;
1146                        send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT,
1147                                     0x08, &data);
1148                } else {
1149                        data = 0x002;
1150                        send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT,
1151                                     0x08, &data);
1152                }
1153        } else {
1154                mos7720_port->shadowMCR &= ~(UART_MCR_XONANY);
1155        }
1156
1157        data = mos7720_port->shadowMCR;
1158        send_mos_cmd(serial, MOS_WRITE, port_number, UART_MCR, &data);
1159
1160        /* Determine divisor based on baud rate */
1161        baud = tty_get_baud_rate(tty);
1162        if (!baud) {
1163                /* pick a default, any default... */
1164                dbg("Picked default baud...");
1165                baud = 9600;
1166        }
1167
1168        if (baud >= 230400) {
1169                set_higher_rates(mos7720_port, baud);
1170                /* Enable Interrupts */
1171                data = 0x0c;
1172                send_mos_cmd(serial, MOS_WRITE, port_number, UART_IER, &data);
1173                return;
1174        }
1175
1176        dbg("%s - baud rate = %d", __FUNCTION__, baud);
1177        status = send_cmd_write_baud_rate(mos7720_port, baud);
1178
1179        /* Enable Interrupts */
1180        data = 0x0c;
1181        send_mos_cmd(serial, MOS_WRITE, port_number, UART_IER, &data);
1182
1183        if (port->read_urb->status != -EINPROGRESS) {
1184                port->read_urb->dev = serial->dev;
1185
1186                status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1187                if (status)
1188                        dbg("usb_submit_urb(read bulk) failed, status = %d",
1189                            status);
1190        }
1191        return;
1192}
1193
1194/*
1195 * mos7720_set_termios
1196 *      this function is called by the tty driver when it wants to change the
1197 *      termios structure.
1198 */
1199static void mos7720_set_termios(struct usb_serial_port *port,
1200                                struct ktermios *old_termios)
1201{
1202        int status;
1203        unsigned int cflag;
1204        struct usb_serial *serial;
1205        struct moschip_port *mos7720_port;
1206        struct tty_struct *tty;
1207
1208        serial = port->serial;
1209
1210        mos7720_port = usb_get_serial_port_data(port);
1211
1212        if (mos7720_port == NULL)
1213                return;
1214
1215        tty = port->tty;
1216
1217        if (!port->tty || !port->tty->termios) {
1218                dbg("%s - no tty or termios", __FUNCTION__);
1219                return;
1220        }
1221
1222        if (!mos7720_port->open) {
1223                dbg("%s - port not opened", __FUNCTION__);
1224                return;
1225        }
1226
1227        dbg("%s\n","setting termios - ASPIRE");
1228
1229        cflag = tty->termios->c_cflag;
1230
1231        if (!cflag) {
1232                printk("%s %s\n",__FUNCTION__,"cflag is NULL");
1233                return;
1234        }
1235
1236        dbg("%s - clfag %08x iflag %08x", __FUNCTION__,
1237            tty->termios->c_cflag,
1238            RELEVANT_IFLAG(tty->termios->c_iflag));
1239
1240        if (old_termios)
1241                dbg("%s - old clfag %08x old iflag %08x", __FUNCTION__,
1242                    old_termios->c_cflag,
1243                    RELEVANT_IFLAG(old_termios->c_iflag));
1244
1245        dbg("%s - port %d", __FUNCTION__, port->number);
1246
1247        /* change the port settings to the new ones specified */
1248        change_port_settings(mos7720_port, old_termios);
1249
1250        if(!port->read_urb) {
1251                dbg("%s","URB KILLED !!!!!\n");
1252                return;
1253        }
1254
1255        if(port->read_urb->status != -EINPROGRESS) {
1256                port->read_urb->dev = serial->dev;
1257                status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1258                if (status)
1259                        dbg("usb_submit_urb(read bulk) failed, status = %d",
1260                            status);
1261        }
1262        return;
1263}
1264
1265/*
1266 * get_lsr_info - get line status register info
1267 *
1268 * Purpose: Let user call ioctl() to get info when the UART physically
1269 *          is emptied.  On bus types like RS485, the transmitter must
1270 *          release the bus after transmitting. This must be done when
1271 *          the transmit shift register is empty, not be done when the
1272 *          transmit holding register is empty.  This functionality
1273 *          allows an RS485 driver to be written in user space.
1274 */
1275static int get_lsr_info(struct moschip_port *mos7720_port,
1276                        unsigned int __user *value)
1277{
1278        int count;
1279        unsigned int result = 0;
1280
1281        count = mos7720_chars_in_buffer(mos7720_port->port);
1282        if (count == 0) {
1283                dbg("%s -- Empty", __FUNCTION__);
1284                result = TIOCSER_TEMT;
1285        }
1286
1287        if (copy_to_user(value, &result, sizeof(int)))
1288                return -EFAULT;
1289        return 0;
1290}
1291
1292/*
1293 * get_number_bytes_avail - get number of bytes available
1294 *
1295 * Purpose: Let user call ioctl to get the count of number of bytes available.
1296 */
1297static int get_number_bytes_avail(struct moschip_port *mos7720_port,
1298                                  unsigned int __user *value)
1299{
1300        unsigned int result = 0;
1301        struct tty_struct *tty = mos7720_port->port->tty;
1302
1303        if (!tty)
1304                return -ENOIOCTLCMD;
1305
1306        result = tty->read_cnt;
1307
1308        dbg("%s(%d) = %d", __FUNCTION__,  mos7720_port->port->number, result);
1309        if (copy_to_user(value, &result, sizeof(int)))
1310                return -EFAULT;
1311
1312        return -ENOIOCTLCMD;
1313}
1314
1315static int set_modem_info(struct moschip_port *mos7720_port, unsigned int cmd,
1316                          unsigned int __user *value)
1317{
1318        unsigned int mcr ;
1319        unsigned int arg;
1320        unsigned char data;
1321
1322        struct usb_serial_port *port;
1323
1324        if (mos7720_port == NULL)
1325                return -1;
1326
1327        port = (struct usb_serial_port*)mos7720_port->port;
1328        mcr = mos7720_port->shadowMCR;
1329
1330        if (copy_from_user(&arg, value, sizeof(int)))
1331                return -EFAULT;
1332
1333        switch (cmd) {
1334        case TIOCMBIS:
1335                if (arg & TIOCM_RTS)
1336                        mcr |= UART_MCR_RTS;
1337                if (arg & TIOCM_DTR)
1338                        mcr |= UART_MCR_RTS;
1339                if (arg & TIOCM_LOOP)
1340                        mcr |= UART_MCR_LOOP;
1341                break;
1342
1343        case TIOCMBIC:
1344                if (arg & TIOCM_RTS)
1345                        mcr &= ~UART_MCR_RTS;
1346                if (arg & TIOCM_DTR)
1347                        mcr &= ~UART_MCR_RTS;
1348                if (arg & TIOCM_LOOP)
1349                        mcr &= ~UART_MCR_LOOP;
1350                break;
1351
1352        case TIOCMSET:
1353                /* turn off the RTS and DTR and LOOPBACK
1354                 * and then only turn on what was asked to */
1355                mcr &=  ~(UART_MCR_RTS | UART_MCR_DTR | UART_MCR_LOOP);
1356                mcr |= ((arg & TIOCM_RTS) ? UART_MCR_RTS : 0);
1357                mcr |= ((arg & TIOCM_DTR) ? UART_MCR_DTR : 0);
1358                mcr |= ((arg & TIOCM_LOOP) ? UART_MCR_LOOP : 0);
1359                break;
1360        }
1361
1362        mos7720_port->shadowMCR = mcr;
1363
1364        data = mos7720_port->shadowMCR;
1365        send_mos_cmd(port->serial, MOS_WRITE,
1366                     port->number - port->serial->minor, UART_MCR, &data);
1367
1368        return 0;
1369}
1370
1371static int get_modem_info(struct moschip_port *mos7720_port,
1372                          unsigned int __user *value)
1373{
1374        unsigned int result = 0;
1375        unsigned int msr = mos7720_port->shadowMSR;
1376        unsigned int mcr = mos7720_port->shadowMCR;
1377
1378        result = ((mcr & UART_MCR_DTR)  ? TIOCM_DTR: 0)   /* 0x002 */
1379                  | ((mcr & UART_MCR_RTS)       ? TIOCM_RTS: 0)   /* 0x004 */
1380                  | ((msr & UART_MSR_CTS)       ? TIOCM_CTS: 0)   /* 0x020 */
1381                  | ((msr & UART_MSR_DCD)       ? TIOCM_CAR: 0)   /* 0x040 */
1382                  | ((msr & UART_MSR_RI)        ? TIOCM_RI:  0)   /* 0x080 */
1383                  | ((msr & UART_MSR_DSR)       ? TIOCM_DSR: 0);  /* 0x100 */
1384
1385
1386        dbg("%s -- %x", __FUNCTION__, result);
1387
1388        if (copy_to_user(value, &result, sizeof(int)))
1389                return -EFAULT;
1390        return 0;
1391}
1392
1393static int get_serial_info(struct moschip_port *mos7720_port,
1394                           struct serial_struct __user *retinfo)
1395{
1396        struct serial_struct tmp;
1397
1398        if (!retinfo)
1399                return -EFAULT;
1400
1401        memset(&tmp, 0, sizeof(tmp));
1402
1403        tmp.type                = PORT_16550A;
1404        tmp.line                = mos7720_port->port->serial->minor;
1405        tmp.port                = mos7720_port->port->number;
1406        tmp.irq                 = 0;
1407        tmp.flags               = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
1408        tmp.xmit_fifo_size      = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
1409        tmp.baud_base           = 9600;
1410        tmp.close_delay         = 5*HZ;
1411        tmp.closing_wait        = 30*HZ;
1412
1413        if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1414                return -EFAULT;
1415        return 0;
1416}
1417
1418static int mos7720_ioctl(struct usb_serial_port *port, struct file *file,
1419                         unsigned int cmd, unsigned long arg)
1420{
1421        struct moschip_port *mos7720_port;
1422        struct async_icount cnow;
1423        struct async_icount cprev;
1424        struct serial_icounter_struct icount;
1425
1426        mos7720_port = usb_get_serial_port_data(port);
1427        if (mos7720_port == NULL)
1428                return -ENODEV;
1429
1430        dbg("%s - port %d, cmd = 0x%x", __FUNCTION__, port->number, cmd);
1431
1432        switch (cmd) {
1433        case TIOCINQ:
1434                /* return number of bytes available */
1435                dbg("%s (%d) TIOCINQ", __FUNCTION__,  port->number);
1436                return get_number_bytes_avail(mos7720_port,
1437                                              (unsigned int __user *)arg);
1438                break;
1439
1440        case TIOCSERGETLSR:
1441                dbg("%s (%d) TIOCSERGETLSR", __FUNCTION__,  port->number);
1442                return get_lsr_info(mos7720_port, (unsigned int __user *)arg);
1443                return 0;
1444
1445        case TIOCMBIS:
1446        case TIOCMBIC:
1447        case TIOCMSET:
1448                dbg("%s (%d) TIOCMSET/TIOCMBIC/TIOCMSET", __FUNCTION__,
1449                    port->number);
1450                return set_modem_info(mos7720_port, cmd,
1451                                      (unsigned int __user *)arg);
1452
1453        case TIOCMGET:
1454                dbg("%s (%d) TIOCMGET", __FUNCTION__,  port->number);
1455                return get_modem_info(mos7720_port,
1456                                      (unsigned int __user *)arg);
1457
1458        case TIOCGSERIAL:
1459                dbg("%s (%d) TIOCGSERIAL", __FUNCTION__,  port->number);
1460                return get_serial_info(mos7720_port,
1461                                       (struct serial_struct __user *)arg);
1462
1463        case TIOCSSERIAL:
1464                dbg("%s (%d) TIOCSSERIAL", __FUNCTION__,  port->number);
1465                break;
1466
1467        case TIOCMIWAIT:
1468                dbg("%s (%d) TIOCMIWAIT", __FUNCTION__,  port->number);
1469                cprev = mos7720_port->icount;
1470                while (1) {
1471                        if (signal_pending(current))
1472                                return -ERESTARTSYS;
1473                        cnow = mos7720_port->icount;
1474                        if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1475                            cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1476                                return -EIO; /* no change => error */
1477                        if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1478                            ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1479                            ((arg & TIOCM_CD)  && (cnow.dcd != cprev.dcd)) ||
1480                            ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
1481                                return 0;
1482                        }
1483                        cprev = cnow;
1484                }
1485                /* NOTREACHED */
1486                break;
1487
1488        case TIOCGICOUNT:
1489                cnow = mos7720_port->icount;
1490                icount.cts = cnow.cts;
1491                icount.dsr = cnow.dsr;
1492                icount.rng = cnow.rng;
1493                icount.dcd = cnow.dcd;
1494                icount.rx = cnow.rx;
1495                icount.tx = cnow.tx;
1496                icount.frame = cnow.frame;
1497                icount.overrun = cnow.overrun;
1498                icount.parity = cnow.parity;
1499                icount.brk = cnow.brk;
1500                icount.buf_overrun = cnow.buf_overrun;
1501
1502                dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __FUNCTION__,
1503                    port->number, icount.rx, icount.tx );
1504                if (copy_to_user((void __user *)arg, &icount, sizeof(icount)))
1505                        return -EFAULT;
1506                return 0;
1507        }
1508
1509        return -ENOIOCTLCMD;
1510}
1511
1512static int mos7720_startup(struct usb_serial *serial)
1513{
1514        struct moschip_serial *mos7720_serial;
1515        struct moschip_port *mos7720_port;
1516        struct usb_device *dev;
1517        int i;
1518        char data;
1519
1520        dbg("%s: Entering ..........", __FUNCTION__);
1521
1522        if (!serial) {
1523                dbg("Invalid Handler");
1524                return -ENODEV;
1525        }
1526
1527        dev = serial->dev;
1528
1529        /* create our private serial structure */
1530        mos7720_serial = kzalloc(sizeof(struct moschip_serial), GFP_KERNEL);
1531        if (mos7720_serial == NULL) {
1532                err("%s - Out of memory", __FUNCTION__);
1533                return -ENOMEM;
1534        }
1535
1536        usb_set_serial_data(serial, mos7720_serial);
1537
1538        /* we set up the pointers to the endpoints in the mos7720_open *
1539         * function, as the structures aren't created yet.             */
1540
1541        /* set up port private structures */
1542        for (i = 0; i < serial->num_ports; ++i) {
1543                mos7720_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
1544                if (mos7720_port == NULL) {
1545                        err("%s - Out of memory", __FUNCTION__);
1546                        usb_set_serial_data(serial, NULL);
1547                        kfree(mos7720_serial);
1548                        return -ENOMEM;
1549                }
1550
1551                /* Initialize all port interrupt end point to port 0 int
1552                 * endpoint.  Our device has only one interrupt endpoint
1553                 * comman to all ports */
1554                serial->port[i]->interrupt_in_endpointAddress = serial->port[0]->interrupt_in_endpointAddress;
1555
1556                mos7720_port->port = serial->port[i];
1557                usb_set_serial_port_data(serial->port[i], mos7720_port);
1558
1559                dbg("port number is %d", serial->port[i]->number);
1560                dbg("serial number is %d", serial->minor);
1561        }
1562
1563
1564        /* setting configuration feature to one */
1565        usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
1566                        (__u8)0x03, 0x00,0x01,0x00, NULL, 0x00, 5*HZ);
1567
1568        send_mos_cmd(serial,MOS_READ,0x00, UART_LSR, &data);  // LSR For Port 1
1569        dbg("LSR:%x",data);
1570
1571        send_mos_cmd(serial,MOS_READ,0x01, UART_LSR, &data);  // LSR For Port 2
1572        dbg("LSR:%x",data);
1573
1574        return 0;
1575}
1576
1577static void mos7720_shutdown(struct usb_serial *serial)
1578{
1579        int i;
1580
1581        /* free private structure allocated for serial port */
1582        for (i=0; i < serial->num_ports; ++i) {
1583                kfree(usb_get_serial_port_data(serial->port[i]));
1584                usb_set_serial_port_data(serial->port[i], NULL);
1585        }
1586
1587        /* free private structure allocated for serial device */
1588        kfree(usb_get_serial_data(serial));
1589        usb_set_serial_data(serial, NULL);
1590}
1591
1592static struct usb_driver usb_driver = {
1593        .name =         "moschip7720",
1594        .probe =        usb_serial_probe,
1595        .disconnect =   usb_serial_disconnect,
1596        .id_table =     moschip_port_id_table,
1597        .no_dynamic_id =        1,
1598};
1599
1600static struct usb_serial_driver moschip7720_2port_driver = {
1601        .driver = {
1602                .owner =        THIS_MODULE,
1603                .name =         "moschip7720",
1604        },
1605        .description            = "Moschip 2 port adapter",
1606        .usb_driver             = &usb_driver,
1607        .id_table               = moschip_port_id_table,
1608        .num_interrupt_in       = 1,
1609        .num_bulk_in            = 2,
1610        .num_bulk_out           = 2,
1611        .num_ports              = 2,
1612        .open                   = mos7720_open,
1613        .close                  = mos7720_close,
1614        .throttle               = mos7720_throttle,
1615        .unthrottle             = mos7720_unthrottle,
1616        .attach                 = mos7720_startup,
1617        .shutdown               = mos7720_shutdown,
1618        .ioctl                  = mos7720_ioctl,
1619        .set_termios            = mos7720_set_termios,
1620        .write                  = mos7720_write,
1621        .write_room             = mos7720_write_room,
1622        .chars_in_buffer        = mos7720_chars_in_buffer,
1623        .break_ctl              = mos7720_break,
1624        .read_bulk_callback     = mos7720_bulk_in_callback,
1625        .read_int_callback      = mos7720_interrupt_callback,
1626};
1627
1628static int __init moschip7720_init(void)
1629{
1630        int retval;
1631
1632        dbg("%s: Entering ..........", __FUNCTION__);
1633
1634        /* Register with the usb serial */
1635        retval = usb_serial_register(&moschip7720_2port_driver);
1636        if (retval)
1637                goto failed_port_device_register;
1638
1639        info(DRIVER_DESC " " DRIVER_VERSION);
1640
1641        /* Register with the usb */
1642        retval = usb_register(&usb_driver);
1643        if (retval)
1644                goto failed_usb_register;
1645
1646        return 0;
1647
1648failed_usb_register:
1649        usb_serial_deregister(&moschip7720_2port_driver);
1650
1651failed_port_device_register:
1652        return retval;
1653}
1654
1655static void __exit moschip7720_exit(void)
1656{
1657        usb_deregister(&usb_driver);
1658        usb_serial_deregister(&moschip7720_2port_driver);
1659}
1660
1661module_init(moschip7720_init);
1662module_exit(moschip7720_exit);
1663
1664/* Module information */
1665MODULE_AUTHOR( DRIVER_AUTHOR );
1666MODULE_DESCRIPTION( DRIVER_DESC );
1667MODULE_LICENSE("GPL");
1668
1669module_param(debug, bool, S_IRUGO | S_IWUSR);
1670MODULE_PARM_DESC(debug, "Debug enabled or not");
1671