Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * mos7720.c
0004  *   Controls the Moschip 7720 usb to dual port serial converter
0005  *
0006  * Copyright 2006 Moschip Semiconductor Tech. Ltd.
0007  *
0008  * Developed by:
0009  *  Vijaya Kumar <vijaykumar.gn@gmail.com>
0010  *  Ajay Kumar <naanuajay@yahoo.com>
0011  *  Gurudeva <ngurudeva@yahoo.com>
0012  *
0013  * Cleaned up from the original by:
0014  *  Greg Kroah-Hartman <gregkh@suse.de>
0015  *
0016  * Originally based on drivers/usb/serial/io_edgeport.c which is:
0017  *  Copyright (C) 2000 Inside Out Networks, All rights reserved.
0018  *  Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
0019  */
0020 #include <linux/kernel.h>
0021 #include <linux/errno.h>
0022 #include <linux/slab.h>
0023 #include <linux/tty.h>
0024 #include <linux/tty_driver.h>
0025 #include <linux/tty_flip.h>
0026 #include <linux/module.h>
0027 #include <linux/spinlock.h>
0028 #include <linux/serial.h>
0029 #include <linux/serial_reg.h>
0030 #include <linux/usb.h>
0031 #include <linux/usb/serial.h>
0032 #include <linux/uaccess.h>
0033 #include <linux/parport.h>
0034 
0035 #define DRIVER_AUTHOR "Aspire Communications pvt Ltd."
0036 #define DRIVER_DESC "Moschip USB Serial Driver"
0037 
0038 /* default urb timeout */
0039 #define MOS_WDR_TIMEOUT 5000
0040 
0041 #define MOS_MAX_PORT    0x02
0042 #define MOS_WRITE   0x0E
0043 #define MOS_READ    0x0D
0044 
0045 /* Interrupt Routines Defines   */
0046 #define SERIAL_IIR_RLS  0x06
0047 #define SERIAL_IIR_RDA  0x04
0048 #define SERIAL_IIR_CTI  0x0c
0049 #define SERIAL_IIR_THR  0x02
0050 #define SERIAL_IIR_MS   0x00
0051 
0052 #define NUM_URBS            16  /* URB Count */
0053 #define URB_TRANSFER_BUFFER_SIZE    32  /* URB Size */
0054 
0055 /* This structure holds all of the local serial port information */
0056 struct moschip_port {
0057     __u8    shadowLCR;      /* last LCR value received */
0058     __u8    shadowMCR;      /* last MCR value received */
0059     __u8    shadowMSR;      /* last MSR value received */
0060     char            open;
0061     struct usb_serial_port  *port;  /* loop back to the owner */
0062     struct urb      *write_urb_pool[NUM_URBS];
0063 };
0064 
0065 #define USB_VENDOR_ID_MOSCHIP       0x9710
0066 #define MOSCHIP_DEVICE_ID_7720      0x7720
0067 #define MOSCHIP_DEVICE_ID_7715      0x7715
0068 
0069 static const struct usb_device_id id_table[] = {
0070     { USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7720) },
0071     { USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7715) },
0072     { } /* terminating entry */
0073 };
0074 MODULE_DEVICE_TABLE(usb, id_table);
0075 
0076 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
0077 
0078 /* initial values for parport regs */
0079 #define DCR_INIT_VAL       0x0c /* SLCTIN, nINIT */
0080 #define ECR_INIT_VAL       0x00 /* SPP mode */
0081 
0082 enum mos7715_pp_modes {
0083     SPP = 0<<5,
0084     PS2 = 1<<5,      /* moschip calls this 'NIBBLE' mode */
0085     PPF = 2<<5,  /* moschip calls this 'CB-FIFO mode */
0086 };
0087 
0088 struct mos7715_parport {
0089     struct parport          *pp;           /* back to containing struct */
0090     struct kref             ref_count;     /* to instance of this struct */
0091     bool                    msg_pending;   /* usb sync call pending */
0092     struct completion       syncmsg_compl; /* usb sync call completed */
0093     struct work_struct      work;          /* restore deferred writes */
0094     struct usb_serial       *serial;       /* back to containing struct */
0095     __u8                    shadowECR;     /* parallel port regs... */
0096     __u8                    shadowDCR;
0097     atomic_t                shadowDSR;     /* updated in int-in callback */
0098 };
0099 
0100 /* lock guards against dereferencing NULL ptr in parport ops callbacks */
0101 static DEFINE_SPINLOCK(release_lock);
0102 
0103 #endif  /* CONFIG_USB_SERIAL_MOS7715_PARPORT */
0104 
0105 static const unsigned int dummy; /* for clarity in register access fns */
0106 
0107 enum mos_regs {
0108     MOS7720_THR,          /* serial port regs */
0109     MOS7720_RHR,
0110     MOS7720_IER,
0111     MOS7720_FCR,
0112     MOS7720_ISR,
0113     MOS7720_LCR,
0114     MOS7720_MCR,
0115     MOS7720_LSR,
0116     MOS7720_MSR,
0117     MOS7720_SPR,
0118     MOS7720_DLL,
0119     MOS7720_DLM,
0120     MOS7720_DPR,          /* parallel port regs */
0121     MOS7720_DSR,
0122     MOS7720_DCR,
0123     MOS7720_ECR,
0124     MOS7720_SP1_REG,      /* device control regs */
0125     MOS7720_SP2_REG,      /* serial port 2 (7720 only) */
0126     MOS7720_PP_REG,
0127     MOS7720_SP_CONTROL_REG,
0128 };
0129 
0130 /*
0131  * Return the correct value for the Windex field of the setup packet
0132  * for a control endpoint message.  See the 7715 datasheet.
0133  */
0134 static inline __u16 get_reg_index(enum mos_regs reg)
0135 {
0136     static const __u16 mos7715_index_lookup_table[] = {
0137         0x00,       /* MOS7720_THR */
0138         0x00,       /* MOS7720_RHR */
0139         0x01,       /* MOS7720_IER */
0140         0x02,       /* MOS7720_FCR */
0141         0x02,       /* MOS7720_ISR */
0142         0x03,       /* MOS7720_LCR */
0143         0x04,       /* MOS7720_MCR */
0144         0x05,       /* MOS7720_LSR */
0145         0x06,       /* MOS7720_MSR */
0146         0x07,       /* MOS7720_SPR */
0147         0x00,       /* MOS7720_DLL */
0148         0x01,       /* MOS7720_DLM */
0149         0x00,       /* MOS7720_DPR */
0150         0x01,       /* MOS7720_DSR */
0151         0x02,       /* MOS7720_DCR */
0152         0x0a,       /* MOS7720_ECR */
0153         0x01,       /* MOS7720_SP1_REG */
0154         0x02,       /* MOS7720_SP2_REG (7720 only) */
0155         0x04,       /* MOS7720_PP_REG (7715 only) */
0156         0x08,       /* MOS7720_SP_CONTROL_REG */
0157     };
0158     return mos7715_index_lookup_table[reg];
0159 }
0160 
0161 /*
0162  * Return the correct value for the upper byte of the Wvalue field of
0163  * the setup packet for a control endpoint message.
0164  */
0165 static inline __u16 get_reg_value(enum mos_regs reg,
0166                   unsigned int serial_portnum)
0167 {
0168     if (reg >= MOS7720_SP1_REG) /* control reg */
0169         return 0x0000;
0170 
0171     else if (reg >= MOS7720_DPR)    /* parallel port reg (7715 only) */
0172         return 0x0100;
0173 
0174     else                  /* serial port reg */
0175         return (serial_portnum + 2) << 8;
0176 }
0177 
0178 /*
0179  * Write data byte to the specified device register.  The data is embedded in
0180  * the value field of the setup packet. serial_portnum is ignored for registers
0181  * not specific to a particular serial port.
0182  */
0183 static int write_mos_reg(struct usb_serial *serial, unsigned int serial_portnum,
0184              enum mos_regs reg, __u8 data)
0185 {
0186     struct usb_device *usbdev = serial->dev;
0187     unsigned int pipe = usb_sndctrlpipe(usbdev, 0);
0188     __u8 request = (__u8)0x0e;
0189     __u8 requesttype = (__u8)0x40;
0190     __u16 index = get_reg_index(reg);
0191     __u16 value = get_reg_value(reg, serial_portnum) + data;
0192     int status = usb_control_msg(usbdev, pipe, request, requesttype, value,
0193                      index, NULL, 0, MOS_WDR_TIMEOUT);
0194     if (status < 0)
0195         dev_err(&usbdev->dev,
0196             "mos7720: usb_control_msg() failed: %d\n", status);
0197     return status;
0198 }
0199 
0200 /*
0201  * Read data byte from the specified device register.  The data returned by the
0202  * device is embedded in the value field of the setup packet.  serial_portnum is
0203  * ignored for registers that are not specific to a particular serial port.
0204  */
0205 static int read_mos_reg(struct usb_serial *serial, unsigned int serial_portnum,
0206             enum mos_regs reg, __u8 *data)
0207 {
0208     struct usb_device *usbdev = serial->dev;
0209     unsigned int pipe = usb_rcvctrlpipe(usbdev, 0);
0210     __u8 request = (__u8)0x0d;
0211     __u8 requesttype = (__u8)0xc0;
0212     __u16 index = get_reg_index(reg);
0213     __u16 value = get_reg_value(reg, serial_portnum);
0214     u8 *buf;
0215     int status;
0216 
0217     buf = kmalloc(1, GFP_KERNEL);
0218     if (!buf) {
0219         *data = 0;
0220         return -ENOMEM;
0221     }
0222 
0223     status = usb_control_msg(usbdev, pipe, request, requesttype, value,
0224                      index, buf, 1, MOS_WDR_TIMEOUT);
0225     if (status == 1) {
0226         *data = *buf;
0227     } else {
0228         dev_err(&usbdev->dev,
0229             "mos7720: usb_control_msg() failed: %d\n", status);
0230         if (status >= 0)
0231             status = -EIO;
0232         *data = 0;
0233     }
0234 
0235     kfree(buf);
0236 
0237     return status;
0238 }
0239 
0240 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
0241 
0242 static inline int mos7715_change_mode(struct mos7715_parport *mos_parport,
0243                       enum mos7715_pp_modes mode)
0244 {
0245     mos_parport->shadowECR = mode;
0246     write_mos_reg(mos_parport->serial, dummy, MOS7720_ECR,
0247               mos_parport->shadowECR);
0248     return 0;
0249 }
0250 
0251 static void destroy_mos_parport(struct kref *kref)
0252 {
0253     struct mos7715_parport *mos_parport =
0254         container_of(kref, struct mos7715_parport, ref_count);
0255 
0256     kfree(mos_parport);
0257 }
0258 
0259 /*
0260  * This is the common top part of all parallel port callback operations that
0261  * send synchronous messages to the device.  This implements convoluted locking
0262  * that avoids two scenarios: (1) a port operation is called after usbserial
0263  * has called our release function, at which point struct mos7715_parport has
0264  * been destroyed, and (2) the device has been disconnected, but usbserial has
0265  * not called the release function yet because someone has a serial port open.
0266  * The shared release_lock prevents the first, and the mutex and disconnected
0267  * flag maintained by usbserial covers the second.  We also use the msg_pending
0268  * flag to ensure that all synchronous usb message calls have completed before
0269  * our release function can return.
0270  */
0271 static int parport_prologue(struct parport *pp)
0272 {
0273     struct mos7715_parport *mos_parport;
0274 
0275     spin_lock(&release_lock);
0276     mos_parport = pp->private_data;
0277     if (unlikely(mos_parport == NULL)) {
0278         /* release fn called, port struct destroyed */
0279         spin_unlock(&release_lock);
0280         return -1;
0281     }
0282     mos_parport->msg_pending = true;   /* synch usb call pending */
0283     reinit_completion(&mos_parport->syncmsg_compl);
0284     spin_unlock(&release_lock);
0285 
0286     /* ensure writes from restore are submitted before new requests */
0287     if (work_pending(&mos_parport->work))
0288         flush_work(&mos_parport->work);
0289 
0290     mutex_lock(&mos_parport->serial->disc_mutex);
0291     if (mos_parport->serial->disconnected) {
0292         /* device disconnected */
0293         mutex_unlock(&mos_parport->serial->disc_mutex);
0294         mos_parport->msg_pending = false;
0295         complete(&mos_parport->syncmsg_compl);
0296         return -1;
0297     }
0298 
0299     return 0;
0300 }
0301 
0302 /*
0303  * This is the common bottom part of all parallel port functions that send
0304  * synchronous messages to the device.
0305  */
0306 static inline void parport_epilogue(struct parport *pp)
0307 {
0308     struct mos7715_parport *mos_parport = pp->private_data;
0309     mutex_unlock(&mos_parport->serial->disc_mutex);
0310     mos_parport->msg_pending = false;
0311     complete(&mos_parport->syncmsg_compl);
0312 }
0313 
0314 static void deferred_restore_writes(struct work_struct *work)
0315 {
0316     struct mos7715_parport *mos_parport;
0317 
0318     mos_parport = container_of(work, struct mos7715_parport, work);
0319 
0320     mutex_lock(&mos_parport->serial->disc_mutex);
0321 
0322     /* if device disconnected, game over */
0323     if (mos_parport->serial->disconnected)
0324         goto done;
0325 
0326     write_mos_reg(mos_parport->serial, dummy, MOS7720_DCR,
0327               mos_parport->shadowDCR);
0328     write_mos_reg(mos_parport->serial, dummy, MOS7720_ECR,
0329               mos_parport->shadowECR);
0330 done:
0331     mutex_unlock(&mos_parport->serial->disc_mutex);
0332 }
0333 
0334 static void parport_mos7715_write_data(struct parport *pp, unsigned char d)
0335 {
0336     struct mos7715_parport *mos_parport = pp->private_data;
0337 
0338     if (parport_prologue(pp) < 0)
0339         return;
0340     mos7715_change_mode(mos_parport, SPP);
0341     write_mos_reg(mos_parport->serial, dummy, MOS7720_DPR, (__u8)d);
0342     parport_epilogue(pp);
0343 }
0344 
0345 static unsigned char parport_mos7715_read_data(struct parport *pp)
0346 {
0347     struct mos7715_parport *mos_parport = pp->private_data;
0348     unsigned char d;
0349 
0350     if (parport_prologue(pp) < 0)
0351         return 0;
0352     read_mos_reg(mos_parport->serial, dummy, MOS7720_DPR, &d);
0353     parport_epilogue(pp);
0354     return d;
0355 }
0356 
0357 static void parport_mos7715_write_control(struct parport *pp, unsigned char d)
0358 {
0359     struct mos7715_parport *mos_parport = pp->private_data;
0360     __u8 data;
0361 
0362     if (parport_prologue(pp) < 0)
0363         return;
0364     data = ((__u8)d & 0x0f) | (mos_parport->shadowDCR & 0xf0);
0365     write_mos_reg(mos_parport->serial, dummy, MOS7720_DCR, data);
0366     mos_parport->shadowDCR = data;
0367     parport_epilogue(pp);
0368 }
0369 
0370 static unsigned char parport_mos7715_read_control(struct parport *pp)
0371 {
0372     struct mos7715_parport *mos_parport;
0373     __u8 dcr;
0374 
0375     spin_lock(&release_lock);
0376     mos_parport = pp->private_data;
0377     if (unlikely(mos_parport == NULL)) {
0378         spin_unlock(&release_lock);
0379         return 0;
0380     }
0381     dcr = mos_parport->shadowDCR & 0x0f;
0382     spin_unlock(&release_lock);
0383     return dcr;
0384 }
0385 
0386 static unsigned char parport_mos7715_frob_control(struct parport *pp,
0387                           unsigned char mask,
0388                           unsigned char val)
0389 {
0390     struct mos7715_parport *mos_parport = pp->private_data;
0391     __u8 dcr;
0392 
0393     mask &= 0x0f;
0394     val &= 0x0f;
0395     if (parport_prologue(pp) < 0)
0396         return 0;
0397     mos_parport->shadowDCR = (mos_parport->shadowDCR & (~mask)) ^ val;
0398     write_mos_reg(mos_parport->serial, dummy, MOS7720_DCR,
0399               mos_parport->shadowDCR);
0400     dcr = mos_parport->shadowDCR & 0x0f;
0401     parport_epilogue(pp);
0402     return dcr;
0403 }
0404 
0405 static unsigned char parport_mos7715_read_status(struct parport *pp)
0406 {
0407     unsigned char status;
0408     struct mos7715_parport *mos_parport;
0409 
0410     spin_lock(&release_lock);
0411     mos_parport = pp->private_data;
0412     if (unlikely(mos_parport == NULL)) {    /* release called */
0413         spin_unlock(&release_lock);
0414         return 0;
0415     }
0416     status = atomic_read(&mos_parport->shadowDSR) & 0xf8;
0417     spin_unlock(&release_lock);
0418     return status;
0419 }
0420 
0421 static void parport_mos7715_enable_irq(struct parport *pp)
0422 {
0423 }
0424 
0425 static void parport_mos7715_disable_irq(struct parport *pp)
0426 {
0427 }
0428 
0429 static void parport_mos7715_data_forward(struct parport *pp)
0430 {
0431     struct mos7715_parport *mos_parport = pp->private_data;
0432 
0433     if (parport_prologue(pp) < 0)
0434         return;
0435     mos7715_change_mode(mos_parport, PS2);
0436     mos_parport->shadowDCR &=  ~0x20;
0437     write_mos_reg(mos_parport->serial, dummy, MOS7720_DCR,
0438               mos_parport->shadowDCR);
0439     parport_epilogue(pp);
0440 }
0441 
0442 static void parport_mos7715_data_reverse(struct parport *pp)
0443 {
0444     struct mos7715_parport *mos_parport = pp->private_data;
0445 
0446     if (parport_prologue(pp) < 0)
0447         return;
0448     mos7715_change_mode(mos_parport, PS2);
0449     mos_parport->shadowDCR |= 0x20;
0450     write_mos_reg(mos_parport->serial, dummy, MOS7720_DCR,
0451               mos_parport->shadowDCR);
0452     parport_epilogue(pp);
0453 }
0454 
0455 static void parport_mos7715_init_state(struct pardevice *dev,
0456                        struct parport_state *s)
0457 {
0458     s->u.pc.ctr = DCR_INIT_VAL;
0459     s->u.pc.ecr = ECR_INIT_VAL;
0460 }
0461 
0462 /* N.B. Parport core code requires that this function not block */
0463 static void parport_mos7715_save_state(struct parport *pp,
0464                        struct parport_state *s)
0465 {
0466     struct mos7715_parport *mos_parport;
0467 
0468     spin_lock(&release_lock);
0469     mos_parport = pp->private_data;
0470     if (unlikely(mos_parport == NULL)) {    /* release called */
0471         spin_unlock(&release_lock);
0472         return;
0473     }
0474     s->u.pc.ctr = mos_parport->shadowDCR;
0475     s->u.pc.ecr = mos_parport->shadowECR;
0476     spin_unlock(&release_lock);
0477 }
0478 
0479 /* N.B. Parport core code requires that this function not block */
0480 static void parport_mos7715_restore_state(struct parport *pp,
0481                       struct parport_state *s)
0482 {
0483     struct mos7715_parport *mos_parport;
0484 
0485     spin_lock(&release_lock);
0486     mos_parport = pp->private_data;
0487     if (unlikely(mos_parport == NULL)) {    /* release called */
0488         spin_unlock(&release_lock);
0489         return;
0490     }
0491     mos_parport->shadowDCR = s->u.pc.ctr;
0492     mos_parport->shadowECR = s->u.pc.ecr;
0493 
0494     schedule_work(&mos_parport->work);
0495     spin_unlock(&release_lock);
0496 }
0497 
0498 static size_t parport_mos7715_write_compat(struct parport *pp,
0499                        const void *buffer,
0500                        size_t len, int flags)
0501 {
0502     int retval;
0503     struct mos7715_parport *mos_parport = pp->private_data;
0504     int actual_len;
0505 
0506     if (parport_prologue(pp) < 0)
0507         return 0;
0508     mos7715_change_mode(mos_parport, PPF);
0509     retval = usb_bulk_msg(mos_parport->serial->dev,
0510                   usb_sndbulkpipe(mos_parport->serial->dev, 2),
0511                   (void *)buffer, len, &actual_len,
0512                   MOS_WDR_TIMEOUT);
0513     parport_epilogue(pp);
0514     if (retval) {
0515         dev_err(&mos_parport->serial->dev->dev,
0516             "mos7720: usb_bulk_msg() failed: %d\n", retval);
0517         return 0;
0518     }
0519     return actual_len;
0520 }
0521 
0522 static struct parport_operations parport_mos7715_ops = {
0523     .owner =        THIS_MODULE,
0524     .write_data =       parport_mos7715_write_data,
0525     .read_data =        parport_mos7715_read_data,
0526 
0527     .write_control =    parport_mos7715_write_control,
0528     .read_control =     parport_mos7715_read_control,
0529     .frob_control =     parport_mos7715_frob_control,
0530 
0531     .read_status =      parport_mos7715_read_status,
0532 
0533     .enable_irq =       parport_mos7715_enable_irq,
0534     .disable_irq =      parport_mos7715_disable_irq,
0535 
0536     .data_forward =     parport_mos7715_data_forward,
0537     .data_reverse =     parport_mos7715_data_reverse,
0538 
0539     .init_state =       parport_mos7715_init_state,
0540     .save_state =       parport_mos7715_save_state,
0541     .restore_state =    parport_mos7715_restore_state,
0542 
0543     .compat_write_data =    parport_mos7715_write_compat,
0544 
0545     .nibble_read_data = parport_ieee1284_read_nibble,
0546     .byte_read_data =   parport_ieee1284_read_byte,
0547 };
0548 
0549 /*
0550  * Allocate and initialize parallel port control struct, initialize
0551  * the parallel port hardware device, and register with the parport subsystem.
0552  */
0553 static int mos7715_parport_init(struct usb_serial *serial)
0554 {
0555     struct mos7715_parport *mos_parport;
0556 
0557     /* allocate and initialize parallel port control struct */
0558     mos_parport = kzalloc(sizeof(struct mos7715_parport), GFP_KERNEL);
0559     if (!mos_parport)
0560         return -ENOMEM;
0561 
0562     mos_parport->msg_pending = false;
0563     kref_init(&mos_parport->ref_count);
0564     usb_set_serial_data(serial, mos_parport); /* hijack private pointer */
0565     mos_parport->serial = serial;
0566     INIT_WORK(&mos_parport->work, deferred_restore_writes);
0567     init_completion(&mos_parport->syncmsg_compl);
0568 
0569     /* cycle parallel port reset bit */
0570     write_mos_reg(mos_parport->serial, dummy, MOS7720_PP_REG, (__u8)0x80);
0571     write_mos_reg(mos_parport->serial, dummy, MOS7720_PP_REG, (__u8)0x00);
0572 
0573     /* initialize device registers */
0574     mos_parport->shadowDCR = DCR_INIT_VAL;
0575     write_mos_reg(mos_parport->serial, dummy, MOS7720_DCR,
0576               mos_parport->shadowDCR);
0577     mos_parport->shadowECR = ECR_INIT_VAL;
0578     write_mos_reg(mos_parport->serial, dummy, MOS7720_ECR,
0579               mos_parport->shadowECR);
0580 
0581     /* register with parport core */
0582     mos_parport->pp = parport_register_port(0, PARPORT_IRQ_NONE,
0583                         PARPORT_DMA_NONE,
0584                         &parport_mos7715_ops);
0585     if (mos_parport->pp == NULL) {
0586         dev_err(&serial->interface->dev,
0587             "Could not register parport\n");
0588         kref_put(&mos_parport->ref_count, destroy_mos_parport);
0589         return -EIO;
0590     }
0591     mos_parport->pp->private_data = mos_parport;
0592     mos_parport->pp->modes = PARPORT_MODE_COMPAT | PARPORT_MODE_PCSPP;
0593     mos_parport->pp->dev = &serial->interface->dev;
0594     parport_announce_port(mos_parport->pp);
0595 
0596     return 0;
0597 }
0598 #endif  /* CONFIG_USB_SERIAL_MOS7715_PARPORT */
0599 
0600 /*
0601  * mos7720_interrupt_callback
0602  *  this is the callback function for when we have received data on the
0603  *  interrupt endpoint.
0604  */
0605 static void mos7720_interrupt_callback(struct urb *urb)
0606 {
0607     int result;
0608     int length;
0609     int status = urb->status;
0610     struct device *dev = &urb->dev->dev;
0611     __u8 *data;
0612     __u8 sp1;
0613     __u8 sp2;
0614 
0615     switch (status) {
0616     case 0:
0617         /* success */
0618         break;
0619     case -ECONNRESET:
0620     case -ENOENT:
0621     case -ESHUTDOWN:
0622         /* this urb is terminated, clean up */
0623         dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status);
0624         return;
0625     default:
0626         dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status);
0627         goto exit;
0628     }
0629 
0630     length = urb->actual_length;
0631     data = urb->transfer_buffer;
0632 
0633     /* Moschip get 4 bytes
0634      * Byte 1 IIR Port 1 (port.number is 0)
0635      * Byte 2 IIR Port 2 (port.number is 1)
0636      * Byte 3 --------------
0637      * Byte 4 FIFO status for both */
0638 
0639     /* the above description is inverted
0640      *  oneukum 2007-03-14 */
0641 
0642     if (unlikely(length != 4)) {
0643         dev_dbg(dev, "Wrong data !!!\n");
0644         return;
0645     }
0646 
0647     sp1 = data[3];
0648     sp2 = data[2];
0649 
0650     if ((sp1 | sp2) & 0x01) {
0651         /* No Interrupt Pending in both the ports */
0652         dev_dbg(dev, "No Interrupt !!!\n");
0653     } else {
0654         switch (sp1 & 0x0f) {
0655         case SERIAL_IIR_RLS:
0656             dev_dbg(dev, "Serial Port 1: Receiver status error or address bit detected in 9-bit mode\n");
0657             break;
0658         case SERIAL_IIR_CTI:
0659             dev_dbg(dev, "Serial Port 1: Receiver time out\n");
0660             break;
0661         case SERIAL_IIR_MS:
0662             /* dev_dbg(dev, "Serial Port 1: Modem status change\n"); */
0663             break;
0664         }
0665 
0666         switch (sp2 & 0x0f) {
0667         case SERIAL_IIR_RLS:
0668             dev_dbg(dev, "Serial Port 2: Receiver status error or address bit detected in 9-bit mode\n");
0669             break;
0670         case SERIAL_IIR_CTI:
0671             dev_dbg(dev, "Serial Port 2: Receiver time out\n");
0672             break;
0673         case SERIAL_IIR_MS:
0674             /* dev_dbg(dev, "Serial Port 2: Modem status change\n"); */
0675             break;
0676         }
0677     }
0678 
0679 exit:
0680     result = usb_submit_urb(urb, GFP_ATOMIC);
0681     if (result)
0682         dev_err(dev, "%s - Error %d submitting control urb\n", __func__, result);
0683 }
0684 
0685 /*
0686  * mos7715_interrupt_callback
0687  *  this is the 7715's callback function for when we have received data on
0688  *  the interrupt endpoint.
0689  */
0690 static void mos7715_interrupt_callback(struct urb *urb)
0691 {
0692     int result;
0693     int length;
0694     int status = urb->status;
0695     struct device *dev = &urb->dev->dev;
0696     __u8 *data;
0697     __u8 iir;
0698 
0699     switch (status) {
0700     case 0:
0701         /* success */
0702         break;
0703     case -ECONNRESET:
0704     case -ENOENT:
0705     case -ESHUTDOWN:
0706     case -ENODEV:
0707         /* this urb is terminated, clean up */
0708         dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status);
0709         return;
0710     default:
0711         dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status);
0712         goto exit;
0713     }
0714 
0715     length = urb->actual_length;
0716     data = urb->transfer_buffer;
0717 
0718     /* Structure of data from 7715 device:
0719      * Byte 1: IIR serial Port
0720      * Byte 2: unused
0721      * Byte 2: DSR parallel port
0722      * Byte 4: FIFO status for both */
0723 
0724     if (unlikely(length != 4)) {
0725         dev_dbg(dev, "Wrong data !!!\n");
0726         return;
0727     }
0728 
0729     iir = data[0];
0730     if (!(iir & 0x01)) {    /* serial port interrupt pending */
0731         switch (iir & 0x0f) {
0732         case SERIAL_IIR_RLS:
0733             dev_dbg(dev, "Serial Port: Receiver status error or address bit detected in 9-bit mode\n");
0734             break;
0735         case SERIAL_IIR_CTI:
0736             dev_dbg(dev, "Serial Port: Receiver time out\n");
0737             break;
0738         case SERIAL_IIR_MS:
0739             /* dev_dbg(dev, "Serial Port: Modem status change\n"); */
0740             break;
0741         }
0742     }
0743 
0744 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
0745     {       /* update local copy of DSR reg */
0746         struct usb_serial_port *port = urb->context;
0747         struct mos7715_parport *mos_parport = port->serial->private;
0748         if (unlikely(mos_parport == NULL))
0749             return;
0750         atomic_set(&mos_parport->shadowDSR, data[2]);
0751     }
0752 #endif
0753 
0754 exit:
0755     result = usb_submit_urb(urb, GFP_ATOMIC);
0756     if (result)
0757         dev_err(dev, "%s - Error %d submitting control urb\n", __func__, result);
0758 }
0759 
0760 /*
0761  * mos7720_bulk_in_callback
0762  *  this is the callback function for when we have received data on the
0763  *  bulk in endpoint.
0764  */
0765 static void mos7720_bulk_in_callback(struct urb *urb)
0766 {
0767     int retval;
0768     unsigned char *data ;
0769     struct usb_serial_port *port;
0770     int status = urb->status;
0771 
0772     if (status) {
0773         dev_dbg(&urb->dev->dev, "nonzero read bulk status received: %d\n", status);
0774         return;
0775     }
0776 
0777     port = urb->context;
0778 
0779     dev_dbg(&port->dev, "Entering...%s\n", __func__);
0780 
0781     data = urb->transfer_buffer;
0782 
0783     if (urb->actual_length) {
0784         tty_insert_flip_string(&port->port, data, urb->actual_length);
0785         tty_flip_buffer_push(&port->port);
0786     }
0787 
0788     if (port->read_urb->status != -EINPROGRESS) {
0789         retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
0790         if (retval)
0791             dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, retval = %d\n", retval);
0792     }
0793 }
0794 
0795 /*
0796  * mos7720_bulk_out_data_callback
0797  *  this is the callback function for when we have finished sending serial
0798  *  data on the bulk out endpoint.
0799  */
0800 static void mos7720_bulk_out_data_callback(struct urb *urb)
0801 {
0802     struct moschip_port *mos7720_port;
0803     int status = urb->status;
0804 
0805     if (status) {
0806         dev_dbg(&urb->dev->dev, "nonzero write bulk status received:%d\n", status);
0807         return;
0808     }
0809 
0810     mos7720_port = urb->context;
0811     if (!mos7720_port) {
0812         dev_dbg(&urb->dev->dev, "NULL mos7720_port pointer\n");
0813         return ;
0814     }
0815 
0816     if (mos7720_port->open)
0817         tty_port_tty_wakeup(&mos7720_port->port->port);
0818 }
0819 
0820 static int mos77xx_calc_num_ports(struct usb_serial *serial,
0821                     struct usb_serial_endpoints *epds)
0822 {
0823     u16 product = le16_to_cpu(serial->dev->descriptor.idProduct);
0824 
0825     if (product == MOSCHIP_DEVICE_ID_7715) {
0826         /*
0827          * The 7715 uses the first bulk in/out endpoint pair for the
0828          * parallel port, and the second for the serial port. We swap
0829          * the endpoint descriptors here so that the first and
0830          * only registered port structure uses the serial-port
0831          * endpoints.
0832          */
0833         swap(epds->bulk_in[0], epds->bulk_in[1]);
0834         swap(epds->bulk_out[0], epds->bulk_out[1]);
0835 
0836         return 1;
0837     }
0838 
0839     return 2;
0840 }
0841 
0842 static int mos7720_open(struct tty_struct *tty, struct usb_serial_port *port)
0843 {
0844     struct usb_serial *serial;
0845     struct urb *urb;
0846     struct moschip_port *mos7720_port;
0847     int response;
0848     int port_number;
0849     __u8 data;
0850     int allocated_urbs = 0;
0851     int j;
0852 
0853     serial = port->serial;
0854 
0855     mos7720_port = usb_get_serial_port_data(port);
0856     if (mos7720_port == NULL)
0857         return -ENODEV;
0858 
0859     usb_clear_halt(serial->dev, port->write_urb->pipe);
0860     usb_clear_halt(serial->dev, port->read_urb->pipe);
0861 
0862     /* Initialising the write urb pool */
0863     for (j = 0; j < NUM_URBS; ++j) {
0864         urb = usb_alloc_urb(0, GFP_KERNEL);
0865         mos7720_port->write_urb_pool[j] = urb;
0866         if (!urb)
0867             continue;
0868 
0869         urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
0870                            GFP_KERNEL);
0871         if (!urb->transfer_buffer) {
0872             usb_free_urb(mos7720_port->write_urb_pool[j]);
0873             mos7720_port->write_urb_pool[j] = NULL;
0874             continue;
0875         }
0876         allocated_urbs++;
0877     }
0878 
0879     if (!allocated_urbs)
0880         return -ENOMEM;
0881 
0882      /* Initialize MCS7720 -- Write Init values to corresponding Registers
0883       *
0884       * Register Index
0885       * 0 : MOS7720_THR/MOS7720_RHR
0886       * 1 : MOS7720_IER
0887       * 2 : MOS7720_FCR
0888       * 3 : MOS7720_LCR
0889       * 4 : MOS7720_MCR
0890       * 5 : MOS7720_LSR
0891       * 6 : MOS7720_MSR
0892       * 7 : MOS7720_SPR
0893       *
0894       * 0x08 : SP1/2 Control Reg
0895       */
0896     port_number = port->port_number;
0897     read_mos_reg(serial, port_number, MOS7720_LSR, &data);
0898 
0899     dev_dbg(&port->dev, "SS::%p LSR:%x\n", mos7720_port, data);
0900 
0901     write_mos_reg(serial, dummy, MOS7720_SP1_REG, 0x02);
0902     write_mos_reg(serial, dummy, MOS7720_SP2_REG, 0x02);
0903 
0904     write_mos_reg(serial, port_number, MOS7720_IER, 0x00);
0905     write_mos_reg(serial, port_number, MOS7720_FCR, 0x00);
0906 
0907     write_mos_reg(serial, port_number, MOS7720_FCR, 0xcf);
0908     mos7720_port->shadowLCR = 0x03;
0909     write_mos_reg(serial, port_number, MOS7720_LCR,
0910               mos7720_port->shadowLCR);
0911     mos7720_port->shadowMCR = 0x0b;
0912     write_mos_reg(serial, port_number, MOS7720_MCR,
0913               mos7720_port->shadowMCR);
0914 
0915     write_mos_reg(serial, port_number, MOS7720_SP_CONTROL_REG, 0x00);
0916     read_mos_reg(serial, dummy, MOS7720_SP_CONTROL_REG, &data);
0917     data = data | (port->port_number + 1);
0918     write_mos_reg(serial, dummy, MOS7720_SP_CONTROL_REG, data);
0919     mos7720_port->shadowLCR = 0x83;
0920     write_mos_reg(serial, port_number, MOS7720_LCR,
0921               mos7720_port->shadowLCR);
0922     write_mos_reg(serial, port_number, MOS7720_THR, 0x0c);
0923     write_mos_reg(serial, port_number, MOS7720_IER, 0x00);
0924     mos7720_port->shadowLCR = 0x03;
0925     write_mos_reg(serial, port_number, MOS7720_LCR,
0926               mos7720_port->shadowLCR);
0927     write_mos_reg(serial, port_number, MOS7720_IER, 0x0c);
0928 
0929     response = usb_submit_urb(port->read_urb, GFP_KERNEL);
0930     if (response)
0931         dev_err(&port->dev, "%s - Error %d submitting read urb\n",
0932                             __func__, response);
0933 
0934     /* initialize our port settings */
0935     mos7720_port->shadowMCR = UART_MCR_OUT2; /* Must set to enable ints! */
0936 
0937     /* send a open port command */
0938     mos7720_port->open = 1;
0939 
0940     return 0;
0941 }
0942 
0943 /*
0944  * mos7720_chars_in_buffer
0945  *  this function is called by the tty driver when it wants to know how many
0946  *  bytes of data we currently have outstanding in the port (data that has
0947  *  been written, but hasn't made it out the port yet)
0948  */
0949 static unsigned int mos7720_chars_in_buffer(struct tty_struct *tty)
0950 {
0951     struct usb_serial_port *port = tty->driver_data;
0952     struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
0953     int i;
0954     unsigned int chars = 0;
0955 
0956     for (i = 0; i < NUM_URBS; ++i) {
0957         if (mos7720_port->write_urb_pool[i] &&
0958             mos7720_port->write_urb_pool[i]->status == -EINPROGRESS)
0959             chars += URB_TRANSFER_BUFFER_SIZE;
0960     }
0961     dev_dbg(&port->dev, "%s - returns %u\n", __func__, chars);
0962     return chars;
0963 }
0964 
0965 static void mos7720_close(struct usb_serial_port *port)
0966 {
0967     struct usb_serial *serial;
0968     struct moschip_port *mos7720_port;
0969     int j;
0970 
0971     serial = port->serial;
0972 
0973     mos7720_port = usb_get_serial_port_data(port);
0974     if (mos7720_port == NULL)
0975         return;
0976 
0977     for (j = 0; j < NUM_URBS; ++j)
0978         usb_kill_urb(mos7720_port->write_urb_pool[j]);
0979 
0980     /* Freeing Write URBs */
0981     for (j = 0; j < NUM_URBS; ++j) {
0982         if (mos7720_port->write_urb_pool[j]) {
0983             kfree(mos7720_port->write_urb_pool[j]->transfer_buffer);
0984             usb_free_urb(mos7720_port->write_urb_pool[j]);
0985         }
0986     }
0987 
0988     /* While closing port, shutdown all bulk read, write  *
0989      * and interrupt read if they exists, otherwise nop   */
0990     usb_kill_urb(port->write_urb);
0991     usb_kill_urb(port->read_urb);
0992 
0993     write_mos_reg(serial, port->port_number, MOS7720_MCR, 0x00);
0994     write_mos_reg(serial, port->port_number, MOS7720_IER, 0x00);
0995 
0996     mos7720_port->open = 0;
0997 }
0998 
0999 static void mos7720_break(struct tty_struct *tty, int break_state)
1000 {
1001     struct usb_serial_port *port = tty->driver_data;
1002     unsigned char data;
1003     struct usb_serial *serial;
1004     struct moschip_port *mos7720_port;
1005 
1006     serial = port->serial;
1007 
1008     mos7720_port = usb_get_serial_port_data(port);
1009     if (mos7720_port == NULL)
1010         return;
1011 
1012     if (break_state == -1)
1013         data = mos7720_port->shadowLCR | UART_LCR_SBC;
1014     else
1015         data = mos7720_port->shadowLCR & ~UART_LCR_SBC;
1016 
1017     mos7720_port->shadowLCR  = data;
1018     write_mos_reg(serial, port->port_number, MOS7720_LCR,
1019               mos7720_port->shadowLCR);
1020 }
1021 
1022 /*
1023  * mos7720_write_room
1024  *  this function is called by the tty driver when it wants to know how many
1025  *  bytes of data we can accept for a specific port.
1026  */
1027 static unsigned int mos7720_write_room(struct tty_struct *tty)
1028 {
1029     struct usb_serial_port *port = tty->driver_data;
1030     struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1031     unsigned int room = 0;
1032     int i;
1033 
1034     /* FIXME: Locking */
1035     for (i = 0; i < NUM_URBS; ++i) {
1036         if (mos7720_port->write_urb_pool[i] &&
1037             mos7720_port->write_urb_pool[i]->status != -EINPROGRESS)
1038             room += URB_TRANSFER_BUFFER_SIZE;
1039     }
1040 
1041     dev_dbg(&port->dev, "%s - returns %u\n", __func__, room);
1042     return room;
1043 }
1044 
1045 static int mos7720_write(struct tty_struct *tty, struct usb_serial_port *port,
1046                  const unsigned char *data, int count)
1047 {
1048     int status;
1049     int i;
1050     int bytes_sent = 0;
1051     int transfer_size;
1052 
1053     struct moschip_port *mos7720_port;
1054     struct usb_serial *serial;
1055     struct urb    *urb;
1056     const unsigned char *current_position = data;
1057 
1058     serial = port->serial;
1059 
1060     mos7720_port = usb_get_serial_port_data(port);
1061     if (mos7720_port == NULL)
1062         return -ENODEV;
1063 
1064     /* try to find a free urb in the list */
1065     urb = NULL;
1066 
1067     for (i = 0; i < NUM_URBS; ++i) {
1068         if (mos7720_port->write_urb_pool[i] &&
1069             mos7720_port->write_urb_pool[i]->status != -EINPROGRESS) {
1070             urb = mos7720_port->write_urb_pool[i];
1071             dev_dbg(&port->dev, "URB:%d\n", i);
1072             break;
1073         }
1074     }
1075 
1076     if (urb == NULL) {
1077         dev_dbg(&port->dev, "%s - no more free urbs\n", __func__);
1078         goto exit;
1079     }
1080 
1081     if (urb->transfer_buffer == NULL) {
1082         urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
1083                            GFP_ATOMIC);
1084         if (!urb->transfer_buffer) {
1085             bytes_sent = -ENOMEM;
1086             goto exit;
1087         }
1088     }
1089     transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
1090 
1091     memcpy(urb->transfer_buffer, current_position, transfer_size);
1092     usb_serial_debug_data(&port->dev, __func__, transfer_size,
1093                   urb->transfer_buffer);
1094 
1095     /* fill urb with data and submit  */
1096     usb_fill_bulk_urb(urb, serial->dev,
1097               usb_sndbulkpipe(serial->dev,
1098                     port->bulk_out_endpointAddress),
1099               urb->transfer_buffer, transfer_size,
1100               mos7720_bulk_out_data_callback, mos7720_port);
1101 
1102     /* send it down the pipe */
1103     status = usb_submit_urb(urb, GFP_ATOMIC);
1104     if (status) {
1105         dev_err_console(port, "%s - usb_submit_urb(write bulk) failed "
1106             "with status = %d\n", __func__, status);
1107         bytes_sent = status;
1108         goto exit;
1109     }
1110     bytes_sent = transfer_size;
1111 
1112 exit:
1113     return bytes_sent;
1114 }
1115 
1116 static void mos7720_throttle(struct tty_struct *tty)
1117 {
1118     struct usb_serial_port *port = tty->driver_data;
1119     struct moschip_port *mos7720_port;
1120     int status;
1121 
1122     mos7720_port = usb_get_serial_port_data(port);
1123 
1124     if (mos7720_port == NULL)
1125         return;
1126 
1127     if (!mos7720_port->open) {
1128         dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1129         return;
1130     }
1131 
1132     /* if we are implementing XON/XOFF, send the stop character */
1133     if (I_IXOFF(tty)) {
1134         unsigned char stop_char = STOP_CHAR(tty);
1135         status = mos7720_write(tty, port, &stop_char, 1);
1136         if (status <= 0)
1137             return;
1138     }
1139 
1140     /* if we are implementing RTS/CTS, toggle that line */
1141     if (C_CRTSCTS(tty)) {
1142         mos7720_port->shadowMCR &= ~UART_MCR_RTS;
1143         write_mos_reg(port->serial, port->port_number, MOS7720_MCR,
1144                   mos7720_port->shadowMCR);
1145     }
1146 }
1147 
1148 static void mos7720_unthrottle(struct tty_struct *tty)
1149 {
1150     struct usb_serial_port *port = tty->driver_data;
1151     struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1152     int status;
1153 
1154     if (mos7720_port == NULL)
1155         return;
1156 
1157     if (!mos7720_port->open) {
1158         dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1159         return;
1160     }
1161 
1162     /* if we are implementing XON/XOFF, send the start character */
1163     if (I_IXOFF(tty)) {
1164         unsigned char start_char = START_CHAR(tty);
1165         status = mos7720_write(tty, port, &start_char, 1);
1166         if (status <= 0)
1167             return;
1168     }
1169 
1170     /* if we are implementing RTS/CTS, toggle that line */
1171     if (C_CRTSCTS(tty)) {
1172         mos7720_port->shadowMCR |= UART_MCR_RTS;
1173         write_mos_reg(port->serial, port->port_number, MOS7720_MCR,
1174                   mos7720_port->shadowMCR);
1175     }
1176 }
1177 
1178 /* FIXME: this function does not work */
1179 static int set_higher_rates(struct moschip_port *mos7720_port,
1180                 unsigned int baud)
1181 {
1182     struct usb_serial_port *port;
1183     struct usb_serial *serial;
1184     int port_number;
1185     enum mos_regs sp_reg;
1186     if (mos7720_port == NULL)
1187         return -EINVAL;
1188 
1189     port = mos7720_port->port;
1190     serial = port->serial;
1191 
1192      /***********************************************
1193      *      Init Sequence for higher rates
1194      ***********************************************/
1195     dev_dbg(&port->dev, "Sending Setting Commands ..........\n");
1196     port_number = port->port_number;
1197 
1198     write_mos_reg(serial, port_number, MOS7720_IER, 0x00);
1199     write_mos_reg(serial, port_number, MOS7720_FCR, 0x00);
1200     write_mos_reg(serial, port_number, MOS7720_FCR, 0xcf);
1201     mos7720_port->shadowMCR = 0x0b;
1202     write_mos_reg(serial, port_number, MOS7720_MCR,
1203               mos7720_port->shadowMCR);
1204     write_mos_reg(serial, dummy, MOS7720_SP_CONTROL_REG, 0x00);
1205 
1206     /***********************************************
1207      *              Set for higher rates           *
1208      ***********************************************/
1209     /* writing baud rate verbatum into uart clock field clearly not right */
1210     if (port_number == 0)
1211         sp_reg = MOS7720_SP1_REG;
1212     else
1213         sp_reg = MOS7720_SP2_REG;
1214     write_mos_reg(serial, dummy, sp_reg, baud * 0x10);
1215     write_mos_reg(serial, dummy, MOS7720_SP_CONTROL_REG, 0x03);
1216     mos7720_port->shadowMCR = 0x2b;
1217     write_mos_reg(serial, port_number, MOS7720_MCR,
1218               mos7720_port->shadowMCR);
1219 
1220     /***********************************************
1221      *              Set DLL/DLM
1222      ***********************************************/
1223     mos7720_port->shadowLCR = mos7720_port->shadowLCR | UART_LCR_DLAB;
1224     write_mos_reg(serial, port_number, MOS7720_LCR,
1225               mos7720_port->shadowLCR);
1226     write_mos_reg(serial, port_number, MOS7720_DLL, 0x01);
1227     write_mos_reg(serial, port_number, MOS7720_DLM, 0x00);
1228     mos7720_port->shadowLCR = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
1229     write_mos_reg(serial, port_number, MOS7720_LCR,
1230               mos7720_port->shadowLCR);
1231 
1232     return 0;
1233 }
1234 
1235 /* baud rate information */
1236 struct divisor_table_entry {
1237     __u32  baudrate;
1238     __u16  divisor;
1239 };
1240 
1241 /* Define table of divisors for moschip 7720 hardware      *
1242  * These assume a 3.6864MHz crystal, the standard /16, and *
1243  * MCR.7 = 0.                          */
1244 static const struct divisor_table_entry divisor_table[] = {
1245     {   50,     2304},
1246     {   110,    1047},  /* 2094.545455 => 230450   => .0217 % over */
1247     {   134,    857},   /* 1713.011152 => 230398.5 => .00065% under */
1248     {   150,    768},
1249     {   300,    384},
1250     {   600,    192},
1251     {   1200,   96},
1252     {   1800,   64},
1253     {   2400,   48},
1254     {   4800,   24},
1255     {   7200,   16},
1256     {   9600,   12},
1257     {   19200,  6},
1258     {   38400,  3},
1259     {   57600,  2},
1260     {   115200, 1},
1261 };
1262 
1263 /*****************************************************************************
1264  * calc_baud_rate_divisor
1265  *  this function calculates the proper baud rate divisor for the specified
1266  *  baud rate.
1267  *****************************************************************************/
1268 static int calc_baud_rate_divisor(struct usb_serial_port *port, int baudrate, int *divisor)
1269 {
1270     int i;
1271     __u16 custom;
1272     __u16 round1;
1273     __u16 round;
1274 
1275 
1276     dev_dbg(&port->dev, "%s - %d\n", __func__, baudrate);
1277 
1278     for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
1279         if (divisor_table[i].baudrate == baudrate) {
1280             *divisor = divisor_table[i].divisor;
1281             return 0;
1282         }
1283     }
1284 
1285     /* After trying for all the standard baud rates    *
1286      * Try calculating the divisor for this baud rate  */
1287     if (baudrate > 75 &&  baudrate < 230400) {
1288         /* get the divisor */
1289         custom = (__u16)(230400L  / baudrate);
1290 
1291         /* Check for round off */
1292         round1 = (__u16)(2304000L / baudrate);
1293         round = (__u16)(round1 - (custom * 10));
1294         if (round > 4)
1295             custom++;
1296         *divisor = custom;
1297 
1298         dev_dbg(&port->dev, "Baud %d = %d\n", baudrate, custom);
1299         return 0;
1300     }
1301 
1302     dev_dbg(&port->dev, "Baud calculation Failed...\n");
1303     return -EINVAL;
1304 }
1305 
1306 /*
1307  * send_cmd_write_baud_rate
1308  *  this function sends the proper command to change the baud rate of the
1309  *  specified port.
1310  */
1311 static int send_cmd_write_baud_rate(struct moschip_port *mos7720_port,
1312                     int baudrate)
1313 {
1314     struct usb_serial_port *port;
1315     struct usb_serial *serial;
1316     int divisor;
1317     int status;
1318     unsigned char number;
1319 
1320     if (mos7720_port == NULL)
1321         return -1;
1322 
1323     port = mos7720_port->port;
1324     serial = port->serial;
1325 
1326     number = port->port_number;
1327     dev_dbg(&port->dev, "%s - baud = %d\n", __func__, baudrate);
1328 
1329     /* Calculate the Divisor */
1330     status = calc_baud_rate_divisor(port, baudrate, &divisor);
1331     if (status) {
1332         dev_err(&port->dev, "%s - bad baud rate\n", __func__);
1333         return status;
1334     }
1335 
1336     /* Enable access to divisor latch */
1337     mos7720_port->shadowLCR = mos7720_port->shadowLCR | UART_LCR_DLAB;
1338     write_mos_reg(serial, number, MOS7720_LCR, mos7720_port->shadowLCR);
1339 
1340     /* Write the divisor */
1341     write_mos_reg(serial, number, MOS7720_DLL, (__u8)(divisor & 0xff));
1342     write_mos_reg(serial, number, MOS7720_DLM,
1343               (__u8)((divisor & 0xff00) >> 8));
1344 
1345     /* Disable access to divisor latch */
1346     mos7720_port->shadowLCR = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
1347     write_mos_reg(serial, number, MOS7720_LCR, mos7720_port->shadowLCR);
1348 
1349     return status;
1350 }
1351 
1352 /*
1353  * change_port_settings
1354  *  This routine is called to set the UART on the device to match
1355  *      the specified new settings.
1356  */
1357 static void change_port_settings(struct tty_struct *tty,
1358                  struct moschip_port *mos7720_port,
1359                  struct ktermios *old_termios)
1360 {
1361     struct usb_serial_port *port;
1362     struct usb_serial *serial;
1363     int baud;
1364     unsigned cflag;
1365     __u8 lData;
1366     __u8 lParity;
1367     __u8 lStop;
1368     int status;
1369     int port_number;
1370 
1371     if (mos7720_port == NULL)
1372         return ;
1373 
1374     port = mos7720_port->port;
1375     serial = port->serial;
1376     port_number = port->port_number;
1377 
1378     if (!mos7720_port->open) {
1379         dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1380         return;
1381     }
1382 
1383     lStop = 0x00;   /* 1 stop bit */
1384     lParity = 0x00; /* No parity */
1385 
1386     cflag = tty->termios.c_cflag;
1387 
1388     lData = UART_LCR_WLEN(tty_get_char_size(cflag));
1389 
1390     /* Change the Parity bit */
1391     if (cflag & PARENB) {
1392         if (cflag & PARODD) {
1393             lParity = UART_LCR_PARITY;
1394             dev_dbg(&port->dev, "%s - parity = odd\n", __func__);
1395         } else {
1396             lParity = (UART_LCR_EPAR | UART_LCR_PARITY);
1397             dev_dbg(&port->dev, "%s - parity = even\n", __func__);
1398         }
1399 
1400     } else {
1401         dev_dbg(&port->dev, "%s - parity = none\n", __func__);
1402     }
1403 
1404     if (cflag & CMSPAR)
1405         lParity = lParity | 0x20;
1406 
1407     /* Change the Stop bit */
1408     if (cflag & CSTOPB) {
1409         lStop = UART_LCR_STOP;
1410         dev_dbg(&port->dev, "%s - stop bits = 2\n", __func__);
1411     } else {
1412         lStop = 0x00;
1413         dev_dbg(&port->dev, "%s - stop bits = 1\n", __func__);
1414     }
1415 
1416 #define LCR_BITS_MASK       0x03    /* Mask for bits/char field */
1417 #define LCR_STOP_MASK       0x04    /* Mask for stop bits field */
1418 #define LCR_PAR_MASK        0x38    /* Mask for parity field */
1419 
1420     /* Update the LCR with the correct value */
1421     mos7720_port->shadowLCR &=
1422         ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
1423     mos7720_port->shadowLCR |= (lData | lParity | lStop);
1424 
1425 
1426     /* Disable Interrupts */
1427     write_mos_reg(serial, port_number, MOS7720_IER, 0x00);
1428     write_mos_reg(serial, port_number, MOS7720_FCR, 0x00);
1429     write_mos_reg(serial, port_number, MOS7720_FCR, 0xcf);
1430 
1431     /* Send the updated LCR value to the mos7720 */
1432     write_mos_reg(serial, port_number, MOS7720_LCR,
1433               mos7720_port->shadowLCR);
1434     mos7720_port->shadowMCR = 0x0b;
1435     write_mos_reg(serial, port_number, MOS7720_MCR,
1436               mos7720_port->shadowMCR);
1437 
1438     /* set up the MCR register and send it to the mos7720 */
1439     mos7720_port->shadowMCR = UART_MCR_OUT2;
1440     if (cflag & CBAUD)
1441         mos7720_port->shadowMCR |= (UART_MCR_DTR | UART_MCR_RTS);
1442 
1443     if (cflag & CRTSCTS) {
1444         mos7720_port->shadowMCR |= (UART_MCR_XONANY);
1445         /* To set hardware flow control to the specified *
1446          * serial port, in SP1/2_CONTROL_REG             */
1447         if (port_number)
1448             write_mos_reg(serial, dummy, MOS7720_SP_CONTROL_REG,
1449                       0x01);
1450         else
1451             write_mos_reg(serial, dummy, MOS7720_SP_CONTROL_REG,
1452                       0x02);
1453 
1454     } else
1455         mos7720_port->shadowMCR &= ~(UART_MCR_XONANY);
1456 
1457     write_mos_reg(serial, port_number, MOS7720_MCR,
1458               mos7720_port->shadowMCR);
1459 
1460     /* Determine divisor based on baud rate */
1461     baud = tty_get_baud_rate(tty);
1462     if (!baud) {
1463         /* pick a default, any default... */
1464         dev_dbg(&port->dev, "Picked default baud...\n");
1465         baud = 9600;
1466     }
1467 
1468     if (baud >= 230400) {
1469         set_higher_rates(mos7720_port, baud);
1470         /* Enable Interrupts */
1471         write_mos_reg(serial, port_number, MOS7720_IER, 0x0c);
1472         return;
1473     }
1474 
1475     dev_dbg(&port->dev, "%s - baud rate = %d\n", __func__, baud);
1476     status = send_cmd_write_baud_rate(mos7720_port, baud);
1477     /* FIXME: needs to write actual resulting baud back not just
1478        blindly do so */
1479     if (cflag & CBAUD)
1480         tty_encode_baud_rate(tty, baud, baud);
1481     /* Enable Interrupts */
1482     write_mos_reg(serial, port_number, MOS7720_IER, 0x0c);
1483 
1484     if (port->read_urb->status != -EINPROGRESS) {
1485         status = usb_submit_urb(port->read_urb, GFP_KERNEL);
1486         if (status)
1487             dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n", status);
1488     }
1489 }
1490 
1491 /*
1492  * mos7720_set_termios
1493  *  this function is called by the tty driver when it wants to change the
1494  *  termios structure.
1495  */
1496 static void mos7720_set_termios(struct tty_struct *tty,
1497         struct usb_serial_port *port, struct ktermios *old_termios)
1498 {
1499     int status;
1500     struct moschip_port *mos7720_port;
1501 
1502     mos7720_port = usb_get_serial_port_data(port);
1503 
1504     if (mos7720_port == NULL)
1505         return;
1506 
1507     if (!mos7720_port->open) {
1508         dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1509         return;
1510     }
1511 
1512     /* change the port settings to the new ones specified */
1513     change_port_settings(tty, mos7720_port, old_termios);
1514 
1515     if (port->read_urb->status != -EINPROGRESS) {
1516         status = usb_submit_urb(port->read_urb, GFP_KERNEL);
1517         if (status)
1518             dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n", status);
1519     }
1520 }
1521 
1522 /*
1523  * get_lsr_info - get line status register info
1524  *
1525  * Purpose: Let user call ioctl() to get info when the UART physically
1526  *      is emptied.  On bus types like RS485, the transmitter must
1527  *      release the bus after transmitting. This must be done when
1528  *      the transmit shift register is empty, not be done when the
1529  *      transmit holding register is empty.  This functionality
1530  *      allows an RS485 driver to be written in user space.
1531  */
1532 static int get_lsr_info(struct tty_struct *tty,
1533         struct moschip_port *mos7720_port, unsigned int __user *value)
1534 {
1535     struct usb_serial_port *port = tty->driver_data;
1536     unsigned int result = 0;
1537     unsigned char data = 0;
1538     int port_number = port->port_number;
1539     int count;
1540 
1541     count = mos7720_chars_in_buffer(tty);
1542     if (count == 0) {
1543         read_mos_reg(port->serial, port_number, MOS7720_LSR, &data);
1544         if ((data & (UART_LSR_TEMT | UART_LSR_THRE))
1545                     == (UART_LSR_TEMT | UART_LSR_THRE)) {
1546             dev_dbg(&port->dev, "%s -- Empty\n", __func__);
1547             result = TIOCSER_TEMT;
1548         }
1549     }
1550     if (copy_to_user(value, &result, sizeof(int)))
1551         return -EFAULT;
1552     return 0;
1553 }
1554 
1555 static int mos7720_tiocmget(struct tty_struct *tty)
1556 {
1557     struct usb_serial_port *port = tty->driver_data;
1558     struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1559     unsigned int result = 0;
1560     unsigned int mcr ;
1561     unsigned int msr ;
1562 
1563     mcr = mos7720_port->shadowMCR;
1564     msr = mos7720_port->shadowMSR;
1565 
1566     result = ((mcr & UART_MCR_DTR)  ? TIOCM_DTR : 0)   /* 0x002 */
1567       | ((mcr & UART_MCR_RTS)   ? TIOCM_RTS : 0)   /* 0x004 */
1568       | ((msr & UART_MSR_CTS)   ? TIOCM_CTS : 0)   /* 0x020 */
1569       | ((msr & UART_MSR_DCD)   ? TIOCM_CAR : 0)   /* 0x040 */
1570       | ((msr & UART_MSR_RI)    ? TIOCM_RI :  0)   /* 0x080 */
1571       | ((msr & UART_MSR_DSR)   ? TIOCM_DSR : 0);  /* 0x100 */
1572 
1573     return result;
1574 }
1575 
1576 static int mos7720_tiocmset(struct tty_struct *tty,
1577                 unsigned int set, unsigned int clear)
1578 {
1579     struct usb_serial_port *port = tty->driver_data;
1580     struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1581     unsigned int mcr ;
1582 
1583     mcr = mos7720_port->shadowMCR;
1584 
1585     if (set & TIOCM_RTS)
1586         mcr |= UART_MCR_RTS;
1587     if (set & TIOCM_DTR)
1588         mcr |= UART_MCR_DTR;
1589     if (set & TIOCM_LOOP)
1590         mcr |= UART_MCR_LOOP;
1591 
1592     if (clear & TIOCM_RTS)
1593         mcr &= ~UART_MCR_RTS;
1594     if (clear & TIOCM_DTR)
1595         mcr &= ~UART_MCR_DTR;
1596     if (clear & TIOCM_LOOP)
1597         mcr &= ~UART_MCR_LOOP;
1598 
1599     mos7720_port->shadowMCR = mcr;
1600     write_mos_reg(port->serial, port->port_number, MOS7720_MCR,
1601               mos7720_port->shadowMCR);
1602 
1603     return 0;
1604 }
1605 
1606 static int mos7720_ioctl(struct tty_struct *tty,
1607              unsigned int cmd, unsigned long arg)
1608 {
1609     struct usb_serial_port *port = tty->driver_data;
1610     struct moschip_port *mos7720_port;
1611 
1612     mos7720_port = usb_get_serial_port_data(port);
1613     if (mos7720_port == NULL)
1614         return -ENODEV;
1615 
1616     switch (cmd) {
1617     case TIOCSERGETLSR:
1618         dev_dbg(&port->dev, "%s TIOCSERGETLSR\n", __func__);
1619         return get_lsr_info(tty, mos7720_port,
1620                     (unsigned int __user *)arg);
1621     }
1622 
1623     return -ENOIOCTLCMD;
1624 }
1625 
1626 static int mos7720_startup(struct usb_serial *serial)
1627 {
1628     struct usb_device *dev;
1629     char data;
1630     u16 product;
1631     int ret_val;
1632 
1633     product = le16_to_cpu(serial->dev->descriptor.idProduct);
1634     dev = serial->dev;
1635 
1636     if (product == MOSCHIP_DEVICE_ID_7715) {
1637         struct urb *urb = serial->port[0]->interrupt_in_urb;
1638 
1639         urb->complete = mos7715_interrupt_callback;
1640 
1641 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
1642         ret_val = mos7715_parport_init(serial);
1643         if (ret_val < 0)
1644             return ret_val;
1645 #endif
1646     }
1647     /* start the interrupt urb */
1648     ret_val = usb_submit_urb(serial->port[0]->interrupt_in_urb, GFP_KERNEL);
1649     if (ret_val) {
1650         dev_err(&dev->dev, "failed to submit interrupt urb: %d\n",
1651             ret_val);
1652     }
1653 
1654     /* LSR For Port 1 */
1655     read_mos_reg(serial, 0, MOS7720_LSR, &data);
1656     dev_dbg(&dev->dev, "LSR:%x\n", data);
1657 
1658     return 0;
1659 }
1660 
1661 static void mos7720_release(struct usb_serial *serial)
1662 {
1663     usb_kill_urb(serial->port[0]->interrupt_in_urb);
1664 
1665 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
1666     /* close the parallel port */
1667 
1668     if (le16_to_cpu(serial->dev->descriptor.idProduct)
1669         == MOSCHIP_DEVICE_ID_7715) {
1670         struct mos7715_parport *mos_parport =
1671             usb_get_serial_data(serial);
1672 
1673         /* prevent NULL ptr dereference in port callbacks */
1674         spin_lock(&release_lock);
1675         mos_parport->pp->private_data = NULL;
1676         spin_unlock(&release_lock);
1677 
1678         /* wait for synchronous usb calls to return */
1679         if (mos_parport->msg_pending)
1680             wait_for_completion_timeout(&mos_parport->syncmsg_compl,
1681                         msecs_to_jiffies(MOS_WDR_TIMEOUT));
1682         /*
1683          * If delayed work is currently scheduled, wait for it to
1684          * complete. This also implies barriers that ensure the
1685          * below serial clearing is not hoisted above the ->work.
1686          */
1687         cancel_work_sync(&mos_parport->work);
1688 
1689         parport_remove_port(mos_parport->pp);
1690         usb_set_serial_data(serial, NULL);
1691         mos_parport->serial = NULL;
1692 
1693         parport_del_port(mos_parport->pp);
1694 
1695         kref_put(&mos_parport->ref_count, destroy_mos_parport);
1696     }
1697 #endif
1698 }
1699 
1700 static int mos7720_port_probe(struct usb_serial_port *port)
1701 {
1702     struct moschip_port *mos7720_port;
1703 
1704     mos7720_port = kzalloc(sizeof(*mos7720_port), GFP_KERNEL);
1705     if (!mos7720_port)
1706         return -ENOMEM;
1707 
1708     mos7720_port->port = port;
1709 
1710     usb_set_serial_port_data(port, mos7720_port);
1711 
1712     return 0;
1713 }
1714 
1715 static void mos7720_port_remove(struct usb_serial_port *port)
1716 {
1717     struct moschip_port *mos7720_port;
1718 
1719     mos7720_port = usb_get_serial_port_data(port);
1720     kfree(mos7720_port);
1721 }
1722 
1723 static struct usb_serial_driver moschip7720_2port_driver = {
1724     .driver = {
1725         .owner =    THIS_MODULE,
1726         .name =     "moschip7720",
1727     },
1728     .description        = "Moschip 2 port adapter",
1729     .id_table       = id_table,
1730     .num_bulk_in        = 2,
1731     .num_bulk_out       = 2,
1732     .num_interrupt_in   = 1,
1733     .calc_num_ports     = mos77xx_calc_num_ports,
1734     .open           = mos7720_open,
1735     .close          = mos7720_close,
1736     .throttle       = mos7720_throttle,
1737     .unthrottle     = mos7720_unthrottle,
1738     .attach         = mos7720_startup,
1739     .release        = mos7720_release,
1740     .port_probe     = mos7720_port_probe,
1741     .port_remove        = mos7720_port_remove,
1742     .ioctl          = mos7720_ioctl,
1743     .tiocmget       = mos7720_tiocmget,
1744     .tiocmset       = mos7720_tiocmset,
1745     .set_termios        = mos7720_set_termios,
1746     .write          = mos7720_write,
1747     .write_room     = mos7720_write_room,
1748     .chars_in_buffer    = mos7720_chars_in_buffer,
1749     .break_ctl      = mos7720_break,
1750     .read_bulk_callback = mos7720_bulk_in_callback,
1751     .read_int_callback  = mos7720_interrupt_callback,
1752 };
1753 
1754 static struct usb_serial_driver * const serial_drivers[] = {
1755     &moschip7720_2port_driver, NULL
1756 };
1757 
1758 module_usb_serial_driver(serial_drivers, id_table);
1759 
1760 MODULE_AUTHOR(DRIVER_AUTHOR);
1761 MODULE_DESCRIPTION(DRIVER_DESC);
1762 MODULE_LICENSE("GPL v2");