13f542974SPaul B Schroeder /* 23f542974SPaul B Schroeder * This program is free software; you can redistribute it and/or modify 33f542974SPaul B Schroeder * it under the terms of the GNU General Public License as published by 43f542974SPaul B Schroeder * the Free Software Foundation; either version 2 of the License, or 53f542974SPaul B Schroeder * (at your option) any later version. 63f542974SPaul B Schroeder * 73f542974SPaul B Schroeder * This program is distributed in the hope that it will be useful, 83f542974SPaul B Schroeder * but WITHOUT ANY WARRANTY; without even the implied warranty of 93f542974SPaul B Schroeder * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 103f542974SPaul B Schroeder * GNU General Public License for more details. 113f542974SPaul B Schroeder * 123f542974SPaul B Schroeder * You should have received a copy of the GNU General Public License 133f542974SPaul B Schroeder * along with this program; if not, write to the Free Software 143f542974SPaul B Schroeder * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 153f542974SPaul B Schroeder * 163f542974SPaul B Schroeder * Clean ups from Moschip version and a few ioctl implementations by: 173f542974SPaul B Schroeder * Paul B Schroeder <pschroeder "at" uplogix "dot" com> 183f542974SPaul B Schroeder * 193f542974SPaul B Schroeder * Originally based on drivers/usb/serial/io_edgeport.c which is: 203f542974SPaul B Schroeder * Copyright (C) 2000 Inside Out Networks, All rights reserved. 213f542974SPaul B Schroeder * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com> 223f542974SPaul B Schroeder * 233f542974SPaul B Schroeder */ 243f542974SPaul B Schroeder 253f542974SPaul B Schroeder #include <linux/kernel.h> 263f542974SPaul B Schroeder #include <linux/errno.h> 273f542974SPaul B Schroeder #include <linux/init.h> 283f542974SPaul B Schroeder #include <linux/slab.h> 293f542974SPaul B Schroeder #include <linux/tty.h> 303f542974SPaul B Schroeder #include <linux/tty_driver.h> 313f542974SPaul B Schroeder #include <linux/tty_flip.h> 323f542974SPaul B Schroeder #include <linux/module.h> 333f542974SPaul B Schroeder #include <linux/serial.h> 343f542974SPaul B Schroeder #include <linux/usb.h> 353f542974SPaul B Schroeder #include <linux/usb/serial.h> 36880af9dbSAlan Cox #include <linux/uaccess.h> 373f542974SPaul B Schroeder 383f542974SPaul B Schroeder /* 393f542974SPaul B Schroeder * Version Information 403f542974SPaul B Schroeder */ 4137768adfSTony Cook #define DRIVER_VERSION "1.3.2" 423f542974SPaul B Schroeder #define DRIVER_DESC "Moschip 7840/7820 USB Serial Driver" 433f542974SPaul B Schroeder 443f542974SPaul B Schroeder /* 453f542974SPaul B Schroeder * 16C50 UART register defines 463f542974SPaul B Schroeder */ 473f542974SPaul B Schroeder 483f542974SPaul B Schroeder #define LCR_BITS_5 0x00 /* 5 bits/char */ 493f542974SPaul B Schroeder #define LCR_BITS_6 0x01 /* 6 bits/char */ 503f542974SPaul B Schroeder #define LCR_BITS_7 0x02 /* 7 bits/char */ 513f542974SPaul B Schroeder #define LCR_BITS_8 0x03 /* 8 bits/char */ 523f542974SPaul B Schroeder #define LCR_BITS_MASK 0x03 /* Mask for bits/char field */ 533f542974SPaul B Schroeder 543f542974SPaul B Schroeder #define LCR_STOP_1 0x00 /* 1 stop bit */ 553f542974SPaul B Schroeder #define LCR_STOP_1_5 0x04 /* 1.5 stop bits (if 5 bits/char) */ 563f542974SPaul B Schroeder #define LCR_STOP_2 0x04 /* 2 stop bits (if 6-8 bits/char) */ 573f542974SPaul B Schroeder #define LCR_STOP_MASK 0x04 /* Mask for stop bits field */ 583f542974SPaul B Schroeder 593f542974SPaul B Schroeder #define LCR_PAR_NONE 0x00 /* No parity */ 603f542974SPaul B Schroeder #define LCR_PAR_ODD 0x08 /* Odd parity */ 613f542974SPaul B Schroeder #define LCR_PAR_EVEN 0x18 /* Even parity */ 623f542974SPaul B Schroeder #define LCR_PAR_MARK 0x28 /* Force parity bit to 1 */ 633f542974SPaul B Schroeder #define LCR_PAR_SPACE 0x38 /* Force parity bit to 0 */ 643f542974SPaul B Schroeder #define LCR_PAR_MASK 0x38 /* Mask for parity field */ 653f542974SPaul B Schroeder 663f542974SPaul B Schroeder #define LCR_SET_BREAK 0x40 /* Set Break condition */ 673f542974SPaul B Schroeder #define LCR_DL_ENABLE 0x80 /* Enable access to divisor latch */ 683f542974SPaul B Schroeder 693f542974SPaul B Schroeder #define MCR_DTR 0x01 /* Assert DTR */ 703f542974SPaul B Schroeder #define MCR_RTS 0x02 /* Assert RTS */ 713f542974SPaul B Schroeder #define MCR_OUT1 0x04 /* Loopback only: Sets state of RI */ 723f542974SPaul B Schroeder #define MCR_MASTER_IE 0x08 /* Enable interrupt outputs */ 733f542974SPaul B Schroeder #define MCR_LOOPBACK 0x10 /* Set internal (digital) loopback mode */ 743f542974SPaul B Schroeder #define MCR_XON_ANY 0x20 /* Enable any char to exit XOFF mode */ 753f542974SPaul B Schroeder 763f542974SPaul B Schroeder #define MOS7840_MSR_CTS 0x10 /* Current state of CTS */ 773f542974SPaul B Schroeder #define MOS7840_MSR_DSR 0x20 /* Current state of DSR */ 783f542974SPaul B Schroeder #define MOS7840_MSR_RI 0x40 /* Current state of RI */ 793f542974SPaul B Schroeder #define MOS7840_MSR_CD 0x80 /* Current state of CD */ 803f542974SPaul B Schroeder 813f542974SPaul B Schroeder /* 823f542974SPaul B Schroeder * Defines used for sending commands to port 833f542974SPaul B Schroeder */ 843f542974SPaul B Schroeder 851e658489SMark Ferrell #define MOS_WDR_TIMEOUT 5000 /* default urb timeout */ 863f542974SPaul B Schroeder 873f542974SPaul B Schroeder #define MOS_PORT1 0x0200 883f542974SPaul B Schroeder #define MOS_PORT2 0x0300 893f542974SPaul B Schroeder #define MOS_VENREG 0x0000 903f542974SPaul B Schroeder #define MOS_MAX_PORT 0x02 913f542974SPaul B Schroeder #define MOS_WRITE 0x0E 923f542974SPaul B Schroeder #define MOS_READ 0x0D 933f542974SPaul B Schroeder 943f542974SPaul B Schroeder /* Requests */ 953f542974SPaul B Schroeder #define MCS_RD_RTYPE 0xC0 963f542974SPaul B Schroeder #define MCS_WR_RTYPE 0x40 973f542974SPaul B Schroeder #define MCS_RDREQ 0x0D 983f542974SPaul B Schroeder #define MCS_WRREQ 0x0E 993f542974SPaul B Schroeder #define MCS_CTRL_TIMEOUT 500 1003f542974SPaul B Schroeder #define VENDOR_READ_LENGTH (0x01) 1013f542974SPaul B Schroeder 1023f542974SPaul B Schroeder #define MAX_NAME_LEN 64 1033f542974SPaul B Schroeder 104880af9dbSAlan Cox #define ZLP_REG1 0x3A /* Zero_Flag_Reg1 58 */ 105880af9dbSAlan Cox #define ZLP_REG5 0x3E /* Zero_Flag_Reg5 62 */ 1063f542974SPaul B Schroeder 1073f542974SPaul B Schroeder /* For higher baud Rates use TIOCEXBAUD */ 1083f542974SPaul B Schroeder #define TIOCEXBAUD 0x5462 1093f542974SPaul B Schroeder 1103f542974SPaul B Schroeder /* vendor id and device id defines */ 1113f542974SPaul B Schroeder 11211e1abb4SDavid Ludlow /* The native mos7840/7820 component */ 1133f542974SPaul B Schroeder #define USB_VENDOR_ID_MOSCHIP 0x9710 1143f542974SPaul B Schroeder #define MOSCHIP_DEVICE_ID_7840 0x7840 1153f542974SPaul B Schroeder #define MOSCHIP_DEVICE_ID_7820 0x7820 1160eafe4deSDonald #define MOSCHIP_DEVICE_ID_7810 0x7810 11711e1abb4SDavid Ludlow /* The native component can have its vendor/device id's overridden 11811e1abb4SDavid Ludlow * in vendor-specific implementations. Such devices can be handled 11968e24113SGreg Kroah-Hartman * by making a change here, in id_table. 12011e1abb4SDavid Ludlow */ 12111e1abb4SDavid Ludlow #define USB_VENDOR_ID_BANDB 0x0856 122acf509aeSCliff Brake #define BANDB_DEVICE_ID_USO9ML2_2 0xAC22 123870408c8SDave Ludlow #define BANDB_DEVICE_ID_USO9ML2_2P 0xBC00 124acf509aeSCliff Brake #define BANDB_DEVICE_ID_USO9ML2_4 0xAC24 125870408c8SDave Ludlow #define BANDB_DEVICE_ID_USO9ML2_4P 0xBC01 126acf509aeSCliff Brake #define BANDB_DEVICE_ID_US9ML2_2 0xAC29 127acf509aeSCliff Brake #define BANDB_DEVICE_ID_US9ML2_4 0xAC30 128acf509aeSCliff Brake #define BANDB_DEVICE_ID_USPTL4_2 0xAC31 129acf509aeSCliff Brake #define BANDB_DEVICE_ID_USPTL4_4 0xAC32 13011e1abb4SDavid Ludlow #define BANDB_DEVICE_ID_USOPTL4_2 0xAC42 131caf3a636SDave Ludlow #define BANDB_DEVICE_ID_USOPTL4_2P 0xBC02 132870408c8SDave Ludlow #define BANDB_DEVICE_ID_USOPTL4_4 0xAC44 133870408c8SDave Ludlow #define BANDB_DEVICE_ID_USOPTL4_4P 0xBC03 134870408c8SDave Ludlow #define BANDB_DEVICE_ID_USOPTL2_4 0xAC24 1353f542974SPaul B Schroeder 1369d498beaSRussell Lang /* This driver also supports 1379d498beaSRussell Lang * ATEN UC2324 device using Moschip MCS7840 1389d498beaSRussell Lang * ATEN UC2322 device using Moschip MCS7820 1399d498beaSRussell Lang */ 140e9b8cffaSTony Cook #define USB_VENDOR_ID_ATENINTL 0x0557 141e9b8cffaSTony Cook #define ATENINTL_DEVICE_ID_UC2324 0x2011 1429d498beaSRussell Lang #define ATENINTL_DEVICE_ID_UC2322 0x7820 143e9b8cffaSTony Cook 14411e1abb4SDavid Ludlow /* Interrupt Routine Defines */ 1453f542974SPaul B Schroeder 1463f542974SPaul B Schroeder #define SERIAL_IIR_RLS 0x06 1473f542974SPaul B Schroeder #define SERIAL_IIR_MS 0x00 1483f542974SPaul B Schroeder 1493f542974SPaul B Schroeder /* 1503f542974SPaul B Schroeder * Emulation of the bit mask on the LINE STATUS REGISTER. 1513f542974SPaul B Schroeder */ 1523f542974SPaul B Schroeder #define SERIAL_LSR_DR 0x0001 1533f542974SPaul B Schroeder #define SERIAL_LSR_OE 0x0002 1543f542974SPaul B Schroeder #define SERIAL_LSR_PE 0x0004 1553f542974SPaul B Schroeder #define SERIAL_LSR_FE 0x0008 1563f542974SPaul B Schroeder #define SERIAL_LSR_BI 0x0010 1573f542974SPaul B Schroeder 1583f542974SPaul B Schroeder #define MOS_MSR_DELTA_CTS 0x10 1593f542974SPaul B Schroeder #define MOS_MSR_DELTA_DSR 0x20 1603f542974SPaul B Schroeder #define MOS_MSR_DELTA_RI 0x40 1613f542974SPaul B Schroeder #define MOS_MSR_DELTA_CD 0x80 1623f542974SPaul B Schroeder 163880af9dbSAlan Cox /* Serial Port register Address */ 1643f542974SPaul B Schroeder #define INTERRUPT_ENABLE_REGISTER ((__u16)(0x01)) 1653f542974SPaul B Schroeder #define FIFO_CONTROL_REGISTER ((__u16)(0x02)) 1663f542974SPaul B Schroeder #define LINE_CONTROL_REGISTER ((__u16)(0x03)) 1673f542974SPaul B Schroeder #define MODEM_CONTROL_REGISTER ((__u16)(0x04)) 1683f542974SPaul B Schroeder #define LINE_STATUS_REGISTER ((__u16)(0x05)) 1693f542974SPaul B Schroeder #define MODEM_STATUS_REGISTER ((__u16)(0x06)) 1703f542974SPaul B Schroeder #define SCRATCH_PAD_REGISTER ((__u16)(0x07)) 1713f542974SPaul B Schroeder #define DIVISOR_LATCH_LSB ((__u16)(0x00)) 1723f542974SPaul B Schroeder #define DIVISOR_LATCH_MSB ((__u16)(0x01)) 1733f542974SPaul B Schroeder 1743f542974SPaul B Schroeder #define CLK_MULTI_REGISTER ((__u16)(0x02)) 1753f542974SPaul B Schroeder #define CLK_START_VALUE_REGISTER ((__u16)(0x03)) 176093ea2d3SDonald Lee #define GPIO_REGISTER ((__u16)(0x07)) 1773f542974SPaul B Schroeder 1783f542974SPaul B Schroeder #define SERIAL_LCR_DLAB ((__u16)(0x0080)) 1793f542974SPaul B Schroeder 1803f542974SPaul B Schroeder /* 1813f542974SPaul B Schroeder * URB POOL related defines 1823f542974SPaul B Schroeder */ 1833f542974SPaul B Schroeder #define NUM_URBS 16 /* URB Count */ 1843f542974SPaul B Schroeder #define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */ 1853f542974SPaul B Schroeder 1860eafe4deSDonald /* LED on/off milliseconds*/ 1870eafe4deSDonald #define LED_ON_MS 500 1880eafe4deSDonald #define LED_OFF_MS 500 1890eafe4deSDonald 1900eafe4deSDonald static int device_type; 1913f542974SPaul B Schroeder 192b9c87663STony Zelenoff static const struct usb_device_id id_table[] = { 1933f542974SPaul B Schroeder {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7840)}, 1943f542974SPaul B Schroeder {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7820)}, 1950eafe4deSDonald {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7810)}, 196acf509aeSCliff Brake {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2)}, 197870408c8SDave Ludlow {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2P)}, 198acf509aeSCliff Brake {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4)}, 199870408c8SDave Ludlow {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4P)}, 200acf509aeSCliff Brake {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_2)}, 201acf509aeSCliff Brake {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_4)}, 202acf509aeSCliff Brake {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_2)}, 203acf509aeSCliff Brake {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_4)}, 20411e1abb4SDavid Ludlow {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2)}, 205870408c8SDave Ludlow {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2P)}, 206acf509aeSCliff Brake {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4)}, 207870408c8SDave Ludlow {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4P)}, 20827f1281dSBlaise Gassend {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL2_4)}, 209e9b8cffaSTony Cook {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2324)}, 2109d498beaSRussell Lang {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2322)}, 2113f542974SPaul B Schroeder {} /* terminating entry */ 2123f542974SPaul B Schroeder }; 21368e24113SGreg Kroah-Hartman MODULE_DEVICE_TABLE(usb, id_table); 2143f542974SPaul B Schroeder 2153f542974SPaul B Schroeder /* This structure holds all of the local port information */ 2163f542974SPaul B Schroeder 2173f542974SPaul B Schroeder struct moschip_port { 2183f542974SPaul B Schroeder int port_num; /*Actual port number in the device(1,2,etc) */ 2193f542974SPaul B Schroeder struct urb *write_urb; /* write URB for this port */ 2203f542974SPaul B Schroeder struct urb *read_urb; /* read URB for this port */ 2210de9a702SOliver Neukum struct urb *int_urb; 2223f542974SPaul B Schroeder __u8 shadowLCR; /* last LCR value received */ 2233f542974SPaul B Schroeder __u8 shadowMCR; /* last MCR value received */ 2243f542974SPaul B Schroeder char open; 2250de9a702SOliver Neukum char open_ports; 2260de9a702SOliver Neukum char zombie; 2273f542974SPaul B Schroeder wait_queue_head_t wait_chase; /* for handling sleeping while waiting for chase to finish */ 2283f542974SPaul B Schroeder wait_queue_head_t delta_msr_wait; /* for handling sleeping while waiting for msr change to happen */ 2293f542974SPaul B Schroeder int delta_msr_cond; 2303f542974SPaul B Schroeder struct async_icount icount; 2313f542974SPaul B Schroeder struct usb_serial_port *port; /* loop back to the owner of this object */ 2323f542974SPaul B Schroeder 2333f542974SPaul B Schroeder /* Offsets */ 2343f542974SPaul B Schroeder __u8 SpRegOffset; 2353f542974SPaul B Schroeder __u8 ControlRegOffset; 2363f542974SPaul B Schroeder __u8 DcrRegOffset; 237880af9dbSAlan Cox /* for processing control URBS in interrupt context */ 2383f542974SPaul B Schroeder struct urb *control_urb; 2390de9a702SOliver Neukum struct usb_ctrlrequest *dr; 2403f542974SPaul B Schroeder char *ctrl_buf; 2413f542974SPaul B Schroeder int MsrLsr; 2423f542974SPaul B Schroeder 2430de9a702SOliver Neukum spinlock_t pool_lock; 2443f542974SPaul B Schroeder struct urb *write_urb_pool[NUM_URBS]; 2450de9a702SOliver Neukum char busy[NUM_URBS]; 24650de36f7SGreg Kroah-Hartman bool read_urb_busy; 2473f542974SPaul B Schroeder 2480eafe4deSDonald /* For device(s) with LED indicator */ 2490eafe4deSDonald bool has_led; 2500eafe4deSDonald bool led_flag; 2510eafe4deSDonald struct timer_list led_timer1; /* Timer for LED on */ 2520eafe4deSDonald struct timer_list led_timer2; /* Timer for LED off */ 2530eafe4deSDonald }; 2543f542974SPaul B Schroeder 25590ab5ee9SRusty Russell static bool debug; 2563f542974SPaul B Schroeder 2573f542974SPaul B Schroeder /* 2583f542974SPaul B Schroeder * mos7840_set_reg_sync 2593f542974SPaul B Schroeder * To set the Control register by calling usb_fill_control_urb function 2603f542974SPaul B Schroeder * by passing usb_sndctrlpipe function as parameter. 2613f542974SPaul B Schroeder */ 2623f542974SPaul B Schroeder 2633f542974SPaul B Schroeder static int mos7840_set_reg_sync(struct usb_serial_port *port, __u16 reg, 2643f542974SPaul B Schroeder __u16 val) 2653f542974SPaul B Schroeder { 2663f542974SPaul B Schroeder struct usb_device *dev = port->serial->dev; 2673f542974SPaul B Schroeder val = val & 0x00ff; 268*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "mos7840_set_reg_sync offset is %x, value %x\n", reg, val); 2693f542974SPaul B Schroeder 2703f542974SPaul B Schroeder return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ, 2713f542974SPaul B Schroeder MCS_WR_RTYPE, val, reg, NULL, 0, 2723f542974SPaul B Schroeder MOS_WDR_TIMEOUT); 2733f542974SPaul B Schroeder } 2743f542974SPaul B Schroeder 2753f542974SPaul B Schroeder /* 2763f542974SPaul B Schroeder * mos7840_get_reg_sync 2773f542974SPaul B Schroeder * To set the Uart register by calling usb_fill_control_urb function by 2783f542974SPaul B Schroeder * passing usb_rcvctrlpipe function as parameter. 2793f542974SPaul B Schroeder */ 2803f542974SPaul B Schroeder 2813f542974SPaul B Schroeder static int mos7840_get_reg_sync(struct usb_serial_port *port, __u16 reg, 2823f542974SPaul B Schroeder __u16 *val) 2833f542974SPaul B Schroeder { 2843f542974SPaul B Schroeder struct usb_device *dev = port->serial->dev; 2853f542974SPaul B Schroeder int ret = 0; 2869e221a35SJohan Hovold u8 *buf; 2879e221a35SJohan Hovold 2889e221a35SJohan Hovold buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL); 2899e221a35SJohan Hovold if (!buf) 2909e221a35SJohan Hovold return -ENOMEM; 2913f542974SPaul B Schroeder 2923f542974SPaul B Schroeder ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ, 2939e221a35SJohan Hovold MCS_RD_RTYPE, 0, reg, buf, VENDOR_READ_LENGTH, 2943f542974SPaul B Schroeder MOS_WDR_TIMEOUT); 2959e221a35SJohan Hovold *val = buf[0]; 296*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s offset is %x, return val %x\n", __func__, reg, *val); 2979e221a35SJohan Hovold 2989e221a35SJohan Hovold kfree(buf); 2993f542974SPaul B Schroeder return ret; 3003f542974SPaul B Schroeder } 3013f542974SPaul B Schroeder 3023f542974SPaul B Schroeder /* 3033f542974SPaul B Schroeder * mos7840_set_uart_reg 3043f542974SPaul B Schroeder * To set the Uart register by calling usb_fill_control_urb function by 3053f542974SPaul B Schroeder * passing usb_sndctrlpipe function as parameter. 3063f542974SPaul B Schroeder */ 3073f542974SPaul B Schroeder 3083f542974SPaul B Schroeder static int mos7840_set_uart_reg(struct usb_serial_port *port, __u16 reg, 3093f542974SPaul B Schroeder __u16 val) 3103f542974SPaul B Schroeder { 3113f542974SPaul B Schroeder 3123f542974SPaul B Schroeder struct usb_device *dev = port->serial->dev; 3133f542974SPaul B Schroeder val = val & 0x00ff; 314880af9dbSAlan Cox /* For the UART control registers, the application number need 315880af9dbSAlan Cox to be Or'ed */ 3160de9a702SOliver Neukum if (port->serial->num_ports == 4) { 317880af9dbSAlan Cox val |= (((__u16) port->number - 318880af9dbSAlan Cox (__u16) (port->serial->minor)) + 1) << 8; 3193f542974SPaul B Schroeder } else { 3203f542974SPaul B Schroeder if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) { 321880af9dbSAlan Cox val |= (((__u16) port->number - 3223f542974SPaul B Schroeder (__u16) (port->serial->minor)) + 1) << 8; 3233f542974SPaul B Schroeder } else { 324*9c134a14SGreg Kroah-Hartman val |= (((__u16) port->number - 3253f542974SPaul B Schroeder (__u16) (port->serial->minor)) + 2) << 8; 3263f542974SPaul B Schroeder } 3273f542974SPaul B Schroeder } 328*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s application number is %x\n", __func__, val); 3293f542974SPaul B Schroeder return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ, 3303f542974SPaul B Schroeder MCS_WR_RTYPE, val, reg, NULL, 0, 3313f542974SPaul B Schroeder MOS_WDR_TIMEOUT); 3323f542974SPaul B Schroeder 3333f542974SPaul B Schroeder } 3343f542974SPaul B Schroeder 3353f542974SPaul B Schroeder /* 3363f542974SPaul B Schroeder * mos7840_get_uart_reg 3373f542974SPaul B Schroeder * To set the Control register by calling usb_fill_control_urb function 3383f542974SPaul B Schroeder * by passing usb_rcvctrlpipe function as parameter. 3393f542974SPaul B Schroeder */ 3403f542974SPaul B Schroeder static int mos7840_get_uart_reg(struct usb_serial_port *port, __u16 reg, 3413f542974SPaul B Schroeder __u16 *val) 3423f542974SPaul B Schroeder { 3433f542974SPaul B Schroeder struct usb_device *dev = port->serial->dev; 3443f542974SPaul B Schroeder int ret = 0; 3453f542974SPaul B Schroeder __u16 Wval; 3469e221a35SJohan Hovold u8 *buf; 3479e221a35SJohan Hovold 3489e221a35SJohan Hovold buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL); 3499e221a35SJohan Hovold if (!buf) 3509e221a35SJohan Hovold return -ENOMEM; 3513f542974SPaul B Schroeder 3523f542974SPaul B Schroeder /* Wval is same as application number */ 3530de9a702SOliver Neukum if (port->serial->num_ports == 4) { 3543f542974SPaul B Schroeder Wval = 3553f542974SPaul B Schroeder (((__u16) port->number - (__u16) (port->serial->minor)) + 3563f542974SPaul B Schroeder 1) << 8; 3573f542974SPaul B Schroeder } else { 3583f542974SPaul B Schroeder if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) { 359880af9dbSAlan Cox Wval = (((__u16) port->number - 3603f542974SPaul B Schroeder (__u16) (port->serial->minor)) + 1) << 8; 3613f542974SPaul B Schroeder } else { 362880af9dbSAlan Cox Wval = (((__u16) port->number - 3633f542974SPaul B Schroeder (__u16) (port->serial->minor)) + 2) << 8; 3643f542974SPaul B Schroeder } 3653f542974SPaul B Schroeder } 366*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s application number is %x\n", __func__, Wval); 3673f542974SPaul B Schroeder ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ, 3689e221a35SJohan Hovold MCS_RD_RTYPE, Wval, reg, buf, VENDOR_READ_LENGTH, 3693f542974SPaul B Schroeder MOS_WDR_TIMEOUT); 3709e221a35SJohan Hovold *val = buf[0]; 3719e221a35SJohan Hovold 3729e221a35SJohan Hovold kfree(buf); 3733f542974SPaul B Schroeder return ret; 3743f542974SPaul B Schroeder } 3753f542974SPaul B Schroeder 376*9c134a14SGreg Kroah-Hartman static void mos7840_dump_serial_port(struct usb_serial_port *port, 377*9c134a14SGreg Kroah-Hartman struct moschip_port *mos7840_port) 3783f542974SPaul B Schroeder { 3793f542974SPaul B Schroeder 380*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "SpRegOffset is %2x\n", mos7840_port->SpRegOffset); 381*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "ControlRegOffset is %2x\n", mos7840_port->ControlRegOffset); 382*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "DCRRegOffset is %2x\n", mos7840_port->DcrRegOffset); 3833f542974SPaul B Schroeder 3843f542974SPaul B Schroeder } 3853f542974SPaul B Schroeder 3863f542974SPaul B Schroeder /************************************************************************/ 3873f542974SPaul B Schroeder /************************************************************************/ 3883f542974SPaul B Schroeder /* I N T E R F A C E F U N C T I O N S */ 3893f542974SPaul B Schroeder /* I N T E R F A C E F U N C T I O N S */ 3903f542974SPaul B Schroeder /************************************************************************/ 3913f542974SPaul B Schroeder /************************************************************************/ 3923f542974SPaul B Schroeder 3933f542974SPaul B Schroeder static inline void mos7840_set_port_private(struct usb_serial_port *port, 3943f542974SPaul B Schroeder struct moschip_port *data) 3953f542974SPaul B Schroeder { 3963f542974SPaul B Schroeder usb_set_serial_port_data(port, (void *)data); 3973f542974SPaul B Schroeder } 3983f542974SPaul B Schroeder 3993f542974SPaul B Schroeder static inline struct moschip_port *mos7840_get_port_private(struct 4003f542974SPaul B Schroeder usb_serial_port 4013f542974SPaul B Schroeder *port) 4023f542974SPaul B Schroeder { 4033f542974SPaul B Schroeder return (struct moschip_port *)usb_get_serial_port_data(port); 4043f542974SPaul B Schroeder } 4053f542974SPaul B Schroeder 4060de9a702SOliver Neukum static void mos7840_handle_new_msr(struct moschip_port *port, __u8 new_msr) 4073f542974SPaul B Schroeder { 4083f542974SPaul B Schroeder struct moschip_port *mos7840_port; 4093f542974SPaul B Schroeder struct async_icount *icount; 4103f542974SPaul B Schroeder mos7840_port = port; 4113f542974SPaul B Schroeder icount = &mos7840_port->icount; 4123f542974SPaul B Schroeder if (new_msr & 4133f542974SPaul B Schroeder (MOS_MSR_DELTA_CTS | MOS_MSR_DELTA_DSR | MOS_MSR_DELTA_RI | 4143f542974SPaul B Schroeder MOS_MSR_DELTA_CD)) { 4153f542974SPaul B Schroeder icount = &mos7840_port->icount; 4163f542974SPaul B Schroeder 4173f542974SPaul B Schroeder /* update input line counters */ 4183f542974SPaul B Schroeder if (new_msr & MOS_MSR_DELTA_CTS) { 4193f542974SPaul B Schroeder icount->cts++; 4200de9a702SOliver Neukum smp_wmb(); 4213f542974SPaul B Schroeder } 4223f542974SPaul B Schroeder if (new_msr & MOS_MSR_DELTA_DSR) { 4233f542974SPaul B Schroeder icount->dsr++; 4240de9a702SOliver Neukum smp_wmb(); 4253f542974SPaul B Schroeder } 4263f542974SPaul B Schroeder if (new_msr & MOS_MSR_DELTA_CD) { 4273f542974SPaul B Schroeder icount->dcd++; 4280de9a702SOliver Neukum smp_wmb(); 4293f542974SPaul B Schroeder } 4303f542974SPaul B Schroeder if (new_msr & MOS_MSR_DELTA_RI) { 4313f542974SPaul B Schroeder icount->rng++; 4320de9a702SOliver Neukum smp_wmb(); 4330de9a702SOliver Neukum } 4343f542974SPaul B Schroeder } 4353f542974SPaul B Schroeder } 4363f542974SPaul B Schroeder 4370de9a702SOliver Neukum static void mos7840_handle_new_lsr(struct moschip_port *port, __u8 new_lsr) 4383f542974SPaul B Schroeder { 4393f542974SPaul B Schroeder struct async_icount *icount; 4403f542974SPaul B Schroeder 4413f542974SPaul B Schroeder if (new_lsr & SERIAL_LSR_BI) { 442880af9dbSAlan Cox /* 443880af9dbSAlan Cox * Parity and Framing errors only count if they 444880af9dbSAlan Cox * occur exclusive of a break being 445880af9dbSAlan Cox * received. 446880af9dbSAlan Cox */ 4473f542974SPaul B Schroeder new_lsr &= (__u8) (SERIAL_LSR_OE | SERIAL_LSR_BI); 4483f542974SPaul B Schroeder } 4493f542974SPaul B Schroeder 4503f542974SPaul B Schroeder /* update input line counters */ 4513f542974SPaul B Schroeder icount = &port->icount; 4523f542974SPaul B Schroeder if (new_lsr & SERIAL_LSR_BI) { 4533f542974SPaul B Schroeder icount->brk++; 4540de9a702SOliver Neukum smp_wmb(); 4553f542974SPaul B Schroeder } 4563f542974SPaul B Schroeder if (new_lsr & SERIAL_LSR_OE) { 4573f542974SPaul B Schroeder icount->overrun++; 4580de9a702SOliver Neukum smp_wmb(); 4593f542974SPaul B Schroeder } 4603f542974SPaul B Schroeder if (new_lsr & SERIAL_LSR_PE) { 4613f542974SPaul B Schroeder icount->parity++; 4620de9a702SOliver Neukum smp_wmb(); 4633f542974SPaul B Schroeder } 4643f542974SPaul B Schroeder if (new_lsr & SERIAL_LSR_FE) { 4653f542974SPaul B Schroeder icount->frame++; 4660de9a702SOliver Neukum smp_wmb(); 4673f542974SPaul B Schroeder } 4683f542974SPaul B Schroeder } 4693f542974SPaul B Schroeder 4703f542974SPaul B Schroeder /************************************************************************/ 4713f542974SPaul B Schroeder /************************************************************************/ 4723f542974SPaul B Schroeder /* U S B C A L L B A C K F U N C T I O N S */ 4733f542974SPaul B Schroeder /* U S B C A L L B A C K F U N C T I O N S */ 4743f542974SPaul B Schroeder /************************************************************************/ 4753f542974SPaul B Schroeder /************************************************************************/ 4763f542974SPaul B Schroeder 4777d12e780SDavid Howells static void mos7840_control_callback(struct urb *urb) 4783f542974SPaul B Schroeder { 4793f542974SPaul B Schroeder unsigned char *data; 4803f542974SPaul B Schroeder struct moschip_port *mos7840_port; 481*9c134a14SGreg Kroah-Hartman struct device *dev = &urb->dev->dev; 4823f542974SPaul B Schroeder __u8 regval = 0x0; 4830de9a702SOliver Neukum int result = 0; 4840643c724SGreg Kroah-Hartman int status = urb->status; 4853f542974SPaul B Schroeder 486cdc97792SMing Lei mos7840_port = urb->context; 4870de9a702SOliver Neukum 4880643c724SGreg Kroah-Hartman switch (status) { 4893f542974SPaul B Schroeder case 0: 4903f542974SPaul B Schroeder /* success */ 4913f542974SPaul B Schroeder break; 4923f542974SPaul B Schroeder case -ECONNRESET: 4933f542974SPaul B Schroeder case -ENOENT: 4943f542974SPaul B Schroeder case -ESHUTDOWN: 4953f542974SPaul B Schroeder /* this urb is terminated, clean up */ 496*9c134a14SGreg Kroah-Hartman dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status); 4973f542974SPaul B Schroeder return; 4983f542974SPaul B Schroeder default: 499*9c134a14SGreg Kroah-Hartman dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status); 5003f542974SPaul B Schroeder goto exit; 5013f542974SPaul B Schroeder } 5023f542974SPaul B Schroeder 503*9c134a14SGreg Kroah-Hartman dev_dbg(dev, "%s urb buffer size is %d\n", __func__, urb->actual_length); 504*9c134a14SGreg Kroah-Hartman dev_dbg(dev, "%s mos7840_port->MsrLsr is %d port %d\n", __func__, 5053f542974SPaul B Schroeder mos7840_port->MsrLsr, mos7840_port->port_num); 5063f542974SPaul B Schroeder data = urb->transfer_buffer; 5073f542974SPaul B Schroeder regval = (__u8) data[0]; 508*9c134a14SGreg Kroah-Hartman dev_dbg(dev, "%s data is %x\n", __func__, regval); 5093f542974SPaul B Schroeder if (mos7840_port->MsrLsr == 0) 5103f542974SPaul B Schroeder mos7840_handle_new_msr(mos7840_port, regval); 5113f542974SPaul B Schroeder else if (mos7840_port->MsrLsr == 1) 5123f542974SPaul B Schroeder mos7840_handle_new_lsr(mos7840_port, regval); 5133f542974SPaul B Schroeder 5143f542974SPaul B Schroeder exit: 5150de9a702SOliver Neukum spin_lock(&mos7840_port->pool_lock); 5160de9a702SOliver Neukum if (!mos7840_port->zombie) 5170de9a702SOliver Neukum result = usb_submit_urb(mos7840_port->int_urb, GFP_ATOMIC); 5180de9a702SOliver Neukum spin_unlock(&mos7840_port->pool_lock); 5190de9a702SOliver Neukum if (result) { 520*9c134a14SGreg Kroah-Hartman dev_err(dev, "%s - Error %d submitting interrupt urb\n", 521441b62c1SHarvey Harrison __func__, result); 5220de9a702SOliver Neukum } 5233f542974SPaul B Schroeder } 5243f542974SPaul B Schroeder 5253f542974SPaul B Schroeder static int mos7840_get_reg(struct moschip_port *mcs, __u16 Wval, __u16 reg, 5263f542974SPaul B Schroeder __u16 *val) 5273f542974SPaul B Schroeder { 5283f542974SPaul B Schroeder struct usb_device *dev = mcs->port->serial->dev; 5290de9a702SOliver Neukum struct usb_ctrlrequest *dr = mcs->dr; 5300de9a702SOliver Neukum unsigned char *buffer = mcs->ctrl_buf; 5310de9a702SOliver Neukum int ret; 5323f542974SPaul B Schroeder 5333f542974SPaul B Schroeder dr->bRequestType = MCS_RD_RTYPE; 5343f542974SPaul B Schroeder dr->bRequest = MCS_RDREQ; 535880af9dbSAlan Cox dr->wValue = cpu_to_le16(Wval); /* 0 */ 5363f542974SPaul B Schroeder dr->wIndex = cpu_to_le16(reg); 5373f542974SPaul B Schroeder dr->wLength = cpu_to_le16(2); 5383f542974SPaul B Schroeder 5393f542974SPaul B Schroeder usb_fill_control_urb(mcs->control_urb, dev, usb_rcvctrlpipe(dev, 0), 5403f542974SPaul B Schroeder (unsigned char *)dr, buffer, 2, 5413f542974SPaul B Schroeder mos7840_control_callback, mcs); 5423f542974SPaul B Schroeder mcs->control_urb->transfer_buffer_length = 2; 5433f542974SPaul B Schroeder ret = usb_submit_urb(mcs->control_urb, GFP_ATOMIC); 5443f542974SPaul B Schroeder return ret; 5453f542974SPaul B Schroeder } 5463f542974SPaul B Schroeder 5470eafe4deSDonald static void mos7840_set_led_callback(struct urb *urb) 5480eafe4deSDonald { 5490eafe4deSDonald switch (urb->status) { 5500eafe4deSDonald case 0: 5510eafe4deSDonald /* Success */ 5520eafe4deSDonald break; 5530eafe4deSDonald case -ECONNRESET: 5540eafe4deSDonald case -ENOENT: 5550eafe4deSDonald case -ESHUTDOWN: 5560eafe4deSDonald /* This urb is terminated, clean up */ 557*9c134a14SGreg Kroah-Hartman dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d", 558*9c134a14SGreg Kroah-Hartman __func__, urb->status); 5590eafe4deSDonald break; 5600eafe4deSDonald default: 561*9c134a14SGreg Kroah-Hartman dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d", 562*9c134a14SGreg Kroah-Hartman __func__, urb->status); 5630eafe4deSDonald } 5640eafe4deSDonald } 5650eafe4deSDonald 5660eafe4deSDonald static void mos7840_set_led_async(struct moschip_port *mcs, __u16 wval, 5670eafe4deSDonald __u16 reg) 5680eafe4deSDonald { 5690eafe4deSDonald struct usb_device *dev = mcs->port->serial->dev; 5700eafe4deSDonald struct usb_ctrlrequest *dr = mcs->dr; 5710eafe4deSDonald 5720eafe4deSDonald dr->bRequestType = MCS_WR_RTYPE; 5730eafe4deSDonald dr->bRequest = MCS_WRREQ; 5740eafe4deSDonald dr->wValue = cpu_to_le16(wval); 5750eafe4deSDonald dr->wIndex = cpu_to_le16(reg); 5760eafe4deSDonald dr->wLength = cpu_to_le16(0); 5770eafe4deSDonald 5780eafe4deSDonald usb_fill_control_urb(mcs->control_urb, dev, usb_sndctrlpipe(dev, 0), 5790eafe4deSDonald (unsigned char *)dr, NULL, 0, mos7840_set_led_callback, NULL); 5800eafe4deSDonald 5810eafe4deSDonald usb_submit_urb(mcs->control_urb, GFP_ATOMIC); 5820eafe4deSDonald } 5830eafe4deSDonald 5840eafe4deSDonald static void mos7840_set_led_sync(struct usb_serial_port *port, __u16 reg, 5850eafe4deSDonald __u16 val) 5860eafe4deSDonald { 5870eafe4deSDonald struct usb_device *dev = port->serial->dev; 5880eafe4deSDonald 5890eafe4deSDonald usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ, MCS_WR_RTYPE, 5900eafe4deSDonald val, reg, NULL, 0, MOS_WDR_TIMEOUT); 5910eafe4deSDonald } 5920eafe4deSDonald 5930eafe4deSDonald static void mos7840_led_off(unsigned long arg) 5940eafe4deSDonald { 5950eafe4deSDonald struct moschip_port *mcs = (struct moschip_port *) arg; 5960eafe4deSDonald 5970eafe4deSDonald /* Turn off LED */ 5980eafe4deSDonald mos7840_set_led_async(mcs, 0x0300, MODEM_CONTROL_REGISTER); 5990eafe4deSDonald mod_timer(&mcs->led_timer2, 6000eafe4deSDonald jiffies + msecs_to_jiffies(LED_OFF_MS)); 6010eafe4deSDonald } 6020eafe4deSDonald 6030eafe4deSDonald static void mos7840_led_flag_off(unsigned long arg) 6040eafe4deSDonald { 6050eafe4deSDonald struct moschip_port *mcs = (struct moschip_port *) arg; 6060eafe4deSDonald 6070eafe4deSDonald mcs->led_flag = false; 6080eafe4deSDonald } 6090eafe4deSDonald 6103f542974SPaul B Schroeder /***************************************************************************** 6113f542974SPaul B Schroeder * mos7840_interrupt_callback 6123f542974SPaul B Schroeder * this is the callback function for when we have received data on the 6133f542974SPaul B Schroeder * interrupt endpoint. 6143f542974SPaul B Schroeder *****************************************************************************/ 6153f542974SPaul B Schroeder 6167d12e780SDavid Howells static void mos7840_interrupt_callback(struct urb *urb) 6173f542974SPaul B Schroeder { 6183f542974SPaul B Schroeder int result; 6193f542974SPaul B Schroeder int length; 6203f542974SPaul B Schroeder struct moschip_port *mos7840_port; 6213f542974SPaul B Schroeder struct usb_serial *serial; 6223f542974SPaul B Schroeder __u16 Data; 6233f542974SPaul B Schroeder unsigned char *data; 6243f542974SPaul B Schroeder __u8 sp[5], st; 6250de9a702SOliver Neukum int i, rv = 0; 6260de9a702SOliver Neukum __u16 wval, wreg = 0; 6270643c724SGreg Kroah-Hartman int status = urb->status; 6283f542974SPaul B Schroeder 6290643c724SGreg Kroah-Hartman switch (status) { 6303f542974SPaul B Schroeder case 0: 6313f542974SPaul B Schroeder /* success */ 6323f542974SPaul B Schroeder break; 6333f542974SPaul B Schroeder case -ECONNRESET: 6343f542974SPaul B Schroeder case -ENOENT: 6353f542974SPaul B Schroeder case -ESHUTDOWN: 6363f542974SPaul B Schroeder /* this urb is terminated, clean up */ 637*9c134a14SGreg Kroah-Hartman dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n", 638*9c134a14SGreg Kroah-Hartman __func__, status); 6393f542974SPaul B Schroeder return; 6403f542974SPaul B Schroeder default: 641*9c134a14SGreg Kroah-Hartman dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n", 642*9c134a14SGreg Kroah-Hartman __func__, status); 6433f542974SPaul B Schroeder goto exit; 6443f542974SPaul B Schroeder } 6453f542974SPaul B Schroeder 6463f542974SPaul B Schroeder length = urb->actual_length; 6473f542974SPaul B Schroeder data = urb->transfer_buffer; 6483f542974SPaul B Schroeder 649cdc97792SMing Lei serial = urb->context; 6503f542974SPaul B Schroeder 6513f542974SPaul B Schroeder /* Moschip get 5 bytes 6523f542974SPaul B Schroeder * Byte 1 IIR Port 1 (port.number is 0) 6533f542974SPaul B Schroeder * Byte 2 IIR Port 2 (port.number is 1) 6543f542974SPaul B Schroeder * Byte 3 IIR Port 3 (port.number is 2) 6553f542974SPaul B Schroeder * Byte 4 IIR Port 4 (port.number is 3) 6563f542974SPaul B Schroeder * Byte 5 FIFO status for both */ 6573f542974SPaul B Schroeder 6583f542974SPaul B Schroeder if (length && length > 5) { 659*9c134a14SGreg Kroah-Hartman dev_dbg(&urb->dev->dev, "%s", "Wrong data !!!\n"); 6603f542974SPaul B Schroeder return; 6613f542974SPaul B Schroeder } 6623f542974SPaul B Schroeder 6633f542974SPaul B Schroeder sp[0] = (__u8) data[0]; 6643f542974SPaul B Schroeder sp[1] = (__u8) data[1]; 6653f542974SPaul B Schroeder sp[2] = (__u8) data[2]; 6663f542974SPaul B Schroeder sp[3] = (__u8) data[3]; 6673f542974SPaul B Schroeder st = (__u8) data[4]; 6683f542974SPaul B Schroeder 6693f542974SPaul B Schroeder for (i = 0; i < serial->num_ports; i++) { 6703f542974SPaul B Schroeder mos7840_port = mos7840_get_port_private(serial->port[i]); 6713f542974SPaul B Schroeder wval = 6723f542974SPaul B Schroeder (((__u16) serial->port[i]->number - 6733f542974SPaul B Schroeder (__u16) (serial->minor)) + 1) << 8; 6743f542974SPaul B Schroeder if (mos7840_port->open) { 6753f542974SPaul B Schroeder if (sp[i] & 0x01) { 676*9c134a14SGreg Kroah-Hartman dev_dbg(&urb->dev->dev, "SP%d No Interrupt !!!\n", i); 6773f542974SPaul B Schroeder } else { 6783f542974SPaul B Schroeder switch (sp[i] & 0x0f) { 6793f542974SPaul B Schroeder case SERIAL_IIR_RLS: 680*9c134a14SGreg Kroah-Hartman dev_dbg(&urb->dev->dev, "Serial Port %d: Receiver status error or \n", i); 681*9c134a14SGreg Kroah-Hartman dev_dbg(&urb->dev->dev, "address bit detected in 9-bit mode\n"); 6823f542974SPaul B Schroeder mos7840_port->MsrLsr = 1; 6830de9a702SOliver Neukum wreg = LINE_STATUS_REGISTER; 6843f542974SPaul B Schroeder break; 6853f542974SPaul B Schroeder case SERIAL_IIR_MS: 686*9c134a14SGreg Kroah-Hartman dev_dbg(&urb->dev->dev, "Serial Port %d: Modem status change\n", i); 6873f542974SPaul B Schroeder mos7840_port->MsrLsr = 0; 6880de9a702SOliver Neukum wreg = MODEM_STATUS_REGISTER; 6893f542974SPaul B Schroeder break; 6903f542974SPaul B Schroeder } 6910de9a702SOliver Neukum spin_lock(&mos7840_port->pool_lock); 6920de9a702SOliver Neukum if (!mos7840_port->zombie) { 6930de9a702SOliver Neukum rv = mos7840_get_reg(mos7840_port, wval, wreg, &Data); 6940de9a702SOliver Neukum } else { 6950de9a702SOliver Neukum spin_unlock(&mos7840_port->pool_lock); 6960de9a702SOliver Neukum return; 6970de9a702SOliver Neukum } 6980de9a702SOliver Neukum spin_unlock(&mos7840_port->pool_lock); 6993f542974SPaul B Schroeder } 7003f542974SPaul B Schroeder } 7013f542974SPaul B Schroeder } 702880af9dbSAlan Cox if (!(rv < 0)) 703880af9dbSAlan Cox /* the completion handler for the control urb will resubmit */ 7040de9a702SOliver Neukum return; 7053f542974SPaul B Schroeder exit: 7063f542974SPaul B Schroeder result = usb_submit_urb(urb, GFP_ATOMIC); 7073f542974SPaul B Schroeder if (result) { 7083f542974SPaul B Schroeder dev_err(&urb->dev->dev, 7093f542974SPaul B Schroeder "%s - Error %d submitting interrupt urb\n", 710441b62c1SHarvey Harrison __func__, result); 7113f542974SPaul B Schroeder } 7123f542974SPaul B Schroeder } 7133f542974SPaul B Schroeder 7143f542974SPaul B Schroeder static int mos7840_port_paranoia_check(struct usb_serial_port *port, 7153f542974SPaul B Schroeder const char *function) 7163f542974SPaul B Schroeder { 7173f542974SPaul B Schroeder if (!port) { 718*9c134a14SGreg Kroah-Hartman pr_debug("%s - port == NULL\n", function); 7193f542974SPaul B Schroeder return -1; 7203f542974SPaul B Schroeder } 7213f542974SPaul B Schroeder if (!port->serial) { 722*9c134a14SGreg Kroah-Hartman pr_debug("%s - port->serial == NULL\n", function); 7233f542974SPaul B Schroeder return -1; 7243f542974SPaul B Schroeder } 7253f542974SPaul B Schroeder 7263f542974SPaul B Schroeder return 0; 7273f542974SPaul B Schroeder } 7283f542974SPaul B Schroeder 7293f542974SPaul B Schroeder /* Inline functions to check the sanity of a pointer that is passed to us */ 7303f542974SPaul B Schroeder static int mos7840_serial_paranoia_check(struct usb_serial *serial, 7313f542974SPaul B Schroeder const char *function) 7323f542974SPaul B Schroeder { 7333f542974SPaul B Schroeder if (!serial) { 734*9c134a14SGreg Kroah-Hartman pr_debug("%s - serial == NULL\n", function); 7353f542974SPaul B Schroeder return -1; 7363f542974SPaul B Schroeder } 7373f542974SPaul B Schroeder if (!serial->type) { 738*9c134a14SGreg Kroah-Hartman pr_debug("%s - serial->type == NULL!\n", function); 7393f542974SPaul B Schroeder return -1; 7403f542974SPaul B Schroeder } 7413f542974SPaul B Schroeder 7423f542974SPaul B Schroeder return 0; 7433f542974SPaul B Schroeder } 7443f542974SPaul B Schroeder 7453f542974SPaul B Schroeder static struct usb_serial *mos7840_get_usb_serial(struct usb_serial_port *port, 7463f542974SPaul B Schroeder const char *function) 7473f542974SPaul B Schroeder { 7483f542974SPaul B Schroeder /* if no port was specified, or it fails a paranoia check */ 7493f542974SPaul B Schroeder if (!port || 7503f542974SPaul B Schroeder mos7840_port_paranoia_check(port, function) || 7513f542974SPaul B Schroeder mos7840_serial_paranoia_check(port->serial, function)) { 752880af9dbSAlan Cox /* then say that we don't have a valid usb_serial thing, 753880af9dbSAlan Cox * which will end up genrating -ENODEV return values */ 7543f542974SPaul B Schroeder return NULL; 7553f542974SPaul B Schroeder } 7563f542974SPaul B Schroeder 7573f542974SPaul B Schroeder return port->serial; 7583f542974SPaul B Schroeder } 7593f542974SPaul B Schroeder 7603f542974SPaul B Schroeder /***************************************************************************** 7613f542974SPaul B Schroeder * mos7840_bulk_in_callback 7623f542974SPaul B Schroeder * this is the callback function for when we have received data on the 7633f542974SPaul B Schroeder * bulk in endpoint. 7643f542974SPaul B Schroeder *****************************************************************************/ 7653f542974SPaul B Schroeder 7667d12e780SDavid Howells static void mos7840_bulk_in_callback(struct urb *urb) 7673f542974SPaul B Schroeder { 7680643c724SGreg Kroah-Hartman int retval; 7693f542974SPaul B Schroeder unsigned char *data; 7703f542974SPaul B Schroeder struct usb_serial *serial; 7713f542974SPaul B Schroeder struct usb_serial_port *port; 7723f542974SPaul B Schroeder struct moschip_port *mos7840_port; 7733f542974SPaul B Schroeder struct tty_struct *tty; 7740643c724SGreg Kroah-Hartman int status = urb->status; 7753f542974SPaul B Schroeder 776cdc97792SMing Lei mos7840_port = urb->context; 777*9c134a14SGreg Kroah-Hartman if (!mos7840_port) 77850de36f7SGreg Kroah-Hartman return; 77950de36f7SGreg Kroah-Hartman 78050de36f7SGreg Kroah-Hartman if (status) { 781*9c134a14SGreg Kroah-Hartman dev_dbg(&urb->dev->dev, "nonzero read bulk status received: %d\n", status); 78250de36f7SGreg Kroah-Hartman mos7840_port->read_urb_busy = false; 7833f542974SPaul B Schroeder return; 7843f542974SPaul B Schroeder } 7853f542974SPaul B Schroeder 786*9c134a14SGreg Kroah-Hartman port = mos7840_port->port; 787441b62c1SHarvey Harrison if (mos7840_port_paranoia_check(port, __func__)) { 78850de36f7SGreg Kroah-Hartman mos7840_port->read_urb_busy = false; 7893f542974SPaul B Schroeder return; 7903f542974SPaul B Schroeder } 7913f542974SPaul B Schroeder 792441b62c1SHarvey Harrison serial = mos7840_get_usb_serial(port, __func__); 7933f542974SPaul B Schroeder if (!serial) { 79450de36f7SGreg Kroah-Hartman mos7840_port->read_urb_busy = false; 7953f542974SPaul B Schroeder return; 7963f542974SPaul B Schroeder } 7973f542974SPaul B Schroeder 7983f542974SPaul B Schroeder data = urb->transfer_buffer; 799*9c134a14SGreg Kroah-Hartman usb_serial_debug_data(debug, &port->dev, __func__, urb->actual_length, data); 8003f542974SPaul B Schroeder 8013f542974SPaul B Schroeder if (urb->actual_length) { 8024a90f09bSAlan Cox tty = tty_port_tty_get(&mos7840_port->port->port); 8033f542974SPaul B Schroeder if (tty) { 8043f542974SPaul B Schroeder tty_insert_flip_string(tty, data, urb->actual_length); 8053f542974SPaul B Schroeder tty_flip_buffer_push(tty); 8064a90f09bSAlan Cox tty_kref_put(tty); 8073f542974SPaul B Schroeder } 8083f542974SPaul B Schroeder mos7840_port->icount.rx += urb->actual_length; 8090de9a702SOliver Neukum smp_wmb(); 810*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "mos7840_port->icount.rx is %d:\n", mos7840_port->icount.rx); 8113f542974SPaul B Schroeder } 8123f542974SPaul B Schroeder 8133f542974SPaul B Schroeder if (!mos7840_port->read_urb) { 814*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s", "URB KILLED !!!\n"); 81550de36f7SGreg Kroah-Hartman mos7840_port->read_urb_busy = false; 8163f542974SPaul B Schroeder return; 8173f542974SPaul B Schroeder } 8183f542974SPaul B Schroeder 8190eafe4deSDonald /* Turn on LED */ 8200eafe4deSDonald if (mos7840_port->has_led && !mos7840_port->led_flag) { 8210eafe4deSDonald mos7840_port->led_flag = true; 8220eafe4deSDonald mos7840_set_led_async(mos7840_port, 0x0301, 8230eafe4deSDonald MODEM_CONTROL_REGISTER); 8240eafe4deSDonald mod_timer(&mos7840_port->led_timer1, 8250eafe4deSDonald jiffies + msecs_to_jiffies(LED_ON_MS)); 8260eafe4deSDonald } 8270de9a702SOliver Neukum 82850de36f7SGreg Kroah-Hartman mos7840_port->read_urb_busy = true; 8290643c724SGreg Kroah-Hartman retval = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC); 8303f542974SPaul B Schroeder 8310643c724SGreg Kroah-Hartman if (retval) { 832*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, retval = %d\n", retval); 83350de36f7SGreg Kroah-Hartman mos7840_port->read_urb_busy = false; 8343f542974SPaul B Schroeder } 8353f542974SPaul B Schroeder } 8363f542974SPaul B Schroeder 8373f542974SPaul B Schroeder /***************************************************************************** 8383f542974SPaul B Schroeder * mos7840_bulk_out_data_callback 839880af9dbSAlan Cox * this is the callback function for when we have finished sending 840880af9dbSAlan Cox * serial data on the bulk out endpoint. 8413f542974SPaul B Schroeder *****************************************************************************/ 8423f542974SPaul B Schroeder 8437d12e780SDavid Howells static void mos7840_bulk_out_data_callback(struct urb *urb) 8443f542974SPaul B Schroeder { 8453f542974SPaul B Schroeder struct moschip_port *mos7840_port; 846*9c134a14SGreg Kroah-Hartman struct usb_serial_port *port; 8473f542974SPaul B Schroeder struct tty_struct *tty; 8480643c724SGreg Kroah-Hartman int status = urb->status; 8490de9a702SOliver Neukum int i; 8500de9a702SOliver Neukum 851cdc97792SMing Lei mos7840_port = urb->context; 852*9c134a14SGreg Kroah-Hartman port = mos7840_port->port; 8530de9a702SOliver Neukum spin_lock(&mos7840_port->pool_lock); 8540de9a702SOliver Neukum for (i = 0; i < NUM_URBS; i++) { 8550de9a702SOliver Neukum if (urb == mos7840_port->write_urb_pool[i]) { 8560de9a702SOliver Neukum mos7840_port->busy[i] = 0; 8570de9a702SOliver Neukum break; 8580de9a702SOliver Neukum } 8590de9a702SOliver Neukum } 8600de9a702SOliver Neukum spin_unlock(&mos7840_port->pool_lock); 8610de9a702SOliver Neukum 8620643c724SGreg Kroah-Hartman if (status) { 863*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "nonzero write bulk status received:%d\n", status); 8643f542974SPaul B Schroeder return; 8653f542974SPaul B Schroeder } 8663f542974SPaul B Schroeder 867*9c134a14SGreg Kroah-Hartman if (mos7840_port_paranoia_check(port, __func__)) 8683f542974SPaul B Schroeder return; 8693f542974SPaul B Schroeder 870*9c134a14SGreg Kroah-Hartman tty = tty_port_tty_get(&port->port); 871b963a844SJiri Slaby if (tty && mos7840_port->open) 872b963a844SJiri Slaby tty_wakeup(tty); 8734a90f09bSAlan Cox tty_kref_put(tty); 8743f542974SPaul B Schroeder 8753f542974SPaul B Schroeder } 8763f542974SPaul B Schroeder 8773f542974SPaul B Schroeder /************************************************************************/ 8783f542974SPaul B Schroeder /* D R I V E R T T Y I N T E R F A C E F U N C T I O N S */ 8793f542974SPaul B Schroeder /************************************************************************/ 8803f542974SPaul B Schroeder #ifdef MCSSerialProbe 8813f542974SPaul B Schroeder static int mos7840_serial_probe(struct usb_serial *serial, 8823f542974SPaul B Schroeder const struct usb_device_id *id) 8833f542974SPaul B Schroeder { 8843f542974SPaul B Schroeder 8853f542974SPaul B Schroeder /*need to implement the mode_reg reading and updating\ 8863f542974SPaul B Schroeder structures usb_serial_ device_type\ 8873f542974SPaul B Schroeder (i.e num_ports, num_bulkin,bulkout etc) */ 8883f542974SPaul B Schroeder /* Also we can update the changes attach */ 8893f542974SPaul B Schroeder return 1; 8903f542974SPaul B Schroeder } 8913f542974SPaul B Schroeder #endif 8923f542974SPaul B Schroeder 8933f542974SPaul B Schroeder /***************************************************************************** 8943f542974SPaul B Schroeder * mos7840_open 8953f542974SPaul B Schroeder * this function is called by the tty driver when a port is opened 8963f542974SPaul B Schroeder * If successful, we return 0 8973f542974SPaul B Schroeder * Otherwise we return a negative error number. 8983f542974SPaul B Schroeder *****************************************************************************/ 8993f542974SPaul B Schroeder 900a509a7e4SAlan Cox static int mos7840_open(struct tty_struct *tty, struct usb_serial_port *port) 9013f542974SPaul B Schroeder { 9023f542974SPaul B Schroeder int response; 9033f542974SPaul B Schroeder int j; 9043f542974SPaul B Schroeder struct usb_serial *serial; 9053f542974SPaul B Schroeder struct urb *urb; 9063f542974SPaul B Schroeder __u16 Data; 9073f542974SPaul B Schroeder int status; 9083f542974SPaul B Schroeder struct moschip_port *mos7840_port; 9090de9a702SOliver Neukum struct moschip_port *port0; 9103f542974SPaul B Schroeder 911*9c134a14SGreg Kroah-Hartman if (mos7840_port_paranoia_check(port, __func__)) 9123f542974SPaul B Schroeder return -ENODEV; 9133f542974SPaul B Schroeder 9143f542974SPaul B Schroeder serial = port->serial; 9153f542974SPaul B Schroeder 916*9c134a14SGreg Kroah-Hartman if (mos7840_serial_paranoia_check(serial, __func__)) 9173f542974SPaul B Schroeder return -ENODEV; 9183f542974SPaul B Schroeder 9193f542974SPaul B Schroeder mos7840_port = mos7840_get_port_private(port); 9200de9a702SOliver Neukum port0 = mos7840_get_port_private(serial->port[0]); 9213f542974SPaul B Schroeder 9220de9a702SOliver Neukum if (mos7840_port == NULL || port0 == NULL) 9233f542974SPaul B Schroeder return -ENODEV; 9243f542974SPaul B Schroeder 9253f542974SPaul B Schroeder usb_clear_halt(serial->dev, port->write_urb->pipe); 9263f542974SPaul B Schroeder usb_clear_halt(serial->dev, port->read_urb->pipe); 9270de9a702SOliver Neukum port0->open_ports++; 9283f542974SPaul B Schroeder 9293f542974SPaul B Schroeder /* Initialising the write urb pool */ 9303f542974SPaul B Schroeder for (j = 0; j < NUM_URBS; ++j) { 9310de9a702SOliver Neukum urb = usb_alloc_urb(0, GFP_KERNEL); 9323f542974SPaul B Schroeder mos7840_port->write_urb_pool[j] = urb; 9333f542974SPaul B Schroeder 9343f542974SPaul B Schroeder if (urb == NULL) { 935194343d9SGreg Kroah-Hartman dev_err(&port->dev, "No more urbs???\n"); 9363f542974SPaul B Schroeder continue; 9373f542974SPaul B Schroeder } 9383f542974SPaul B Schroeder 939880af9dbSAlan Cox urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE, 940880af9dbSAlan Cox GFP_KERNEL); 9413f542974SPaul B Schroeder if (!urb->transfer_buffer) { 9420de9a702SOliver Neukum usb_free_urb(urb); 9430de9a702SOliver Neukum mos7840_port->write_urb_pool[j] = NULL; 944194343d9SGreg Kroah-Hartman dev_err(&port->dev, 945194343d9SGreg Kroah-Hartman "%s-out of memory for urb buffers.\n", 946194343d9SGreg Kroah-Hartman __func__); 9473f542974SPaul B Schroeder continue; 9483f542974SPaul B Schroeder } 9493f542974SPaul B Schroeder } 9503f542974SPaul B Schroeder 9513f542974SPaul B Schroeder /***************************************************************************** 9523f542974SPaul B Schroeder * Initialize MCS7840 -- Write Init values to corresponding Registers 9533f542974SPaul B Schroeder * 9543f542974SPaul B Schroeder * Register Index 9553f542974SPaul B Schroeder * 1 : IER 9563f542974SPaul B Schroeder * 2 : FCR 9573f542974SPaul B Schroeder * 3 : LCR 9583f542974SPaul B Schroeder * 4 : MCR 9593f542974SPaul B Schroeder * 9603f542974SPaul B Schroeder * 0x08 : SP1/2 Control Reg 9613f542974SPaul B Schroeder *****************************************************************************/ 9623f542974SPaul B Schroeder 963880af9dbSAlan Cox /* NEED to check the following Block */ 9643f542974SPaul B Schroeder 9653f542974SPaul B Schroeder Data = 0x0; 9663f542974SPaul B Schroeder status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data); 9673f542974SPaul B Schroeder if (status < 0) { 968*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "Reading Spreg failed\n"); 9693f542974SPaul B Schroeder return -1; 9703f542974SPaul B Schroeder } 9713f542974SPaul B Schroeder Data |= 0x80; 9723f542974SPaul B Schroeder status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data); 9733f542974SPaul B Schroeder if (status < 0) { 974*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "writing Spreg failed\n"); 9753f542974SPaul B Schroeder return -1; 9763f542974SPaul B Schroeder } 9773f542974SPaul B Schroeder 9783f542974SPaul B Schroeder Data &= ~0x80; 9793f542974SPaul B Schroeder status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data); 9803f542974SPaul B Schroeder if (status < 0) { 981*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "writing Spreg failed\n"); 9823f542974SPaul B Schroeder return -1; 9833f542974SPaul B Schroeder } 984880af9dbSAlan Cox /* End of block to be checked */ 9853f542974SPaul B Schroeder 9863f542974SPaul B Schroeder Data = 0x0; 987880af9dbSAlan Cox status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset, 988880af9dbSAlan Cox &Data); 9893f542974SPaul B Schroeder if (status < 0) { 990*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "Reading Controlreg failed\n"); 9913f542974SPaul B Schroeder return -1; 9923f542974SPaul B Schroeder } 993880af9dbSAlan Cox Data |= 0x08; /* Driver done bit */ 994880af9dbSAlan Cox Data |= 0x20; /* rx_disable */ 995880af9dbSAlan Cox status = mos7840_set_reg_sync(port, 996880af9dbSAlan Cox mos7840_port->ControlRegOffset, Data); 9973f542974SPaul B Schroeder if (status < 0) { 998*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "writing Controlreg failed\n"); 9993f542974SPaul B Schroeder return -1; 10003f542974SPaul B Schroeder } 1001880af9dbSAlan Cox /* do register settings here */ 1002880af9dbSAlan Cox /* Set all regs to the device default values. */ 1003880af9dbSAlan Cox /*********************************** 1004880af9dbSAlan Cox * First Disable all interrupts. 1005880af9dbSAlan Cox ***********************************/ 10063f542974SPaul B Schroeder Data = 0x00; 10073f542974SPaul B Schroeder status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data); 10083f542974SPaul B Schroeder if (status < 0) { 1009*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "disabling interrupts failed\n"); 10103f542974SPaul B Schroeder return -1; 10113f542974SPaul B Schroeder } 1012880af9dbSAlan Cox /* Set FIFO_CONTROL_REGISTER to the default value */ 10133f542974SPaul B Schroeder Data = 0x00; 10143f542974SPaul B Schroeder status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data); 10153f542974SPaul B Schroeder if (status < 0) { 1016*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "Writing FIFO_CONTROL_REGISTER failed\n"); 10173f542974SPaul B Schroeder return -1; 10183f542974SPaul B Schroeder } 10193f542974SPaul B Schroeder 10203f542974SPaul B Schroeder Data = 0xcf; 10213f542974SPaul B Schroeder status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data); 10223f542974SPaul B Schroeder if (status < 0) { 1023*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "Writing FIFO_CONTROL_REGISTER failed\n"); 10243f542974SPaul B Schroeder return -1; 10253f542974SPaul B Schroeder } 10263f542974SPaul B Schroeder 10273f542974SPaul B Schroeder Data = 0x03; 10283f542974SPaul B Schroeder status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data); 10293f542974SPaul B Schroeder mos7840_port->shadowLCR = Data; 10303f542974SPaul B Schroeder 10313f542974SPaul B Schroeder Data = 0x0b; 10323f542974SPaul B Schroeder status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data); 10333f542974SPaul B Schroeder mos7840_port->shadowMCR = Data; 10343f542974SPaul B Schroeder 10353f542974SPaul B Schroeder Data = 0x00; 10363f542974SPaul B Schroeder status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data); 10373f542974SPaul B Schroeder mos7840_port->shadowLCR = Data; 10383f542974SPaul B Schroeder 1039880af9dbSAlan Cox Data |= SERIAL_LCR_DLAB; /* data latch enable in LCR 0x80 */ 10403f542974SPaul B Schroeder status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data); 10413f542974SPaul B Schroeder 10423f542974SPaul B Schroeder Data = 0x0c; 10433f542974SPaul B Schroeder status = mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data); 10443f542974SPaul B Schroeder 10453f542974SPaul B Schroeder Data = 0x0; 10463f542974SPaul B Schroeder status = mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data); 10473f542974SPaul B Schroeder 10483f542974SPaul B Schroeder Data = 0x00; 10493f542974SPaul B Schroeder status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data); 10503f542974SPaul B Schroeder 10513f542974SPaul B Schroeder Data = Data & ~SERIAL_LCR_DLAB; 10523f542974SPaul B Schroeder status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data); 10533f542974SPaul B Schroeder mos7840_port->shadowLCR = Data; 10543f542974SPaul B Schroeder 1055880af9dbSAlan Cox /* clearing Bulkin and Bulkout Fifo */ 10563f542974SPaul B Schroeder Data = 0x0; 10573f542974SPaul B Schroeder status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data); 10583f542974SPaul B Schroeder 10593f542974SPaul B Schroeder Data = Data | 0x0c; 10603f542974SPaul B Schroeder status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data); 10613f542974SPaul B Schroeder 10623f542974SPaul B Schroeder Data = Data & ~0x0c; 10633f542974SPaul B Schroeder status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data); 1064880af9dbSAlan Cox /* Finally enable all interrupts */ 10653f542974SPaul B Schroeder Data = 0x0c; 10663f542974SPaul B Schroeder status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data); 10673f542974SPaul B Schroeder 1068880af9dbSAlan Cox /* clearing rx_disable */ 10693f542974SPaul B Schroeder Data = 0x0; 1070880af9dbSAlan Cox status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset, 1071880af9dbSAlan Cox &Data); 10723f542974SPaul B Schroeder Data = Data & ~0x20; 1073880af9dbSAlan Cox status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset, 1074880af9dbSAlan Cox Data); 10753f542974SPaul B Schroeder 1076880af9dbSAlan Cox /* rx_negate */ 10773f542974SPaul B Schroeder Data = 0x0; 1078880af9dbSAlan Cox status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset, 1079880af9dbSAlan Cox &Data); 10803f542974SPaul B Schroeder Data = Data | 0x10; 1081880af9dbSAlan Cox status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset, 1082880af9dbSAlan Cox Data); 10833f542974SPaul B Schroeder 10843f542974SPaul B Schroeder /* Check to see if we've set up our endpoint info yet * 10853f542974SPaul B Schroeder * (can't set it up in mos7840_startup as the structures * 10863f542974SPaul B Schroeder * were not set up at that time.) */ 10870de9a702SOliver Neukum if (port0->open_ports == 1) { 10883f542974SPaul B Schroeder if (serial->port[0]->interrupt_in_buffer == NULL) { 10893f542974SPaul B Schroeder /* set up interrupt urb */ 10903f542974SPaul B Schroeder usb_fill_int_urb(serial->port[0]->interrupt_in_urb, 10913f542974SPaul B Schroeder serial->dev, 10923f542974SPaul B Schroeder usb_rcvintpipe(serial->dev, 1093880af9dbSAlan Cox serial->port[0]->interrupt_in_endpointAddress), 10943f542974SPaul B Schroeder serial->port[0]->interrupt_in_buffer, 10953f542974SPaul B Schroeder serial->port[0]->interrupt_in_urb-> 10963f542974SPaul B Schroeder transfer_buffer_length, 10973f542974SPaul B Schroeder mos7840_interrupt_callback, 10983f542974SPaul B Schroeder serial, 1099880af9dbSAlan Cox serial->port[0]->interrupt_in_urb->interval); 11003f542974SPaul B Schroeder 11013f542974SPaul B Schroeder /* start interrupt read for mos7840 * 11023f542974SPaul B Schroeder * will continue as long as mos7840 is connected */ 11033f542974SPaul B Schroeder 11043f542974SPaul B Schroeder response = 11053f542974SPaul B Schroeder usb_submit_urb(serial->port[0]->interrupt_in_urb, 11063f542974SPaul B Schroeder GFP_KERNEL); 11073f542974SPaul B Schroeder if (response) { 1108194343d9SGreg Kroah-Hartman dev_err(&port->dev, "%s - Error %d submitting " 1109194343d9SGreg Kroah-Hartman "interrupt urb\n", __func__, response); 11103f542974SPaul B Schroeder } 11113f542974SPaul B Schroeder 11123f542974SPaul B Schroeder } 11133f542974SPaul B Schroeder 11143f542974SPaul B Schroeder } 11153f542974SPaul B Schroeder 11163f542974SPaul B Schroeder /* see if we've set up our endpoint info yet * 11173f542974SPaul B Schroeder * (can't set it up in mos7840_startup as the * 11183f542974SPaul B Schroeder * structures were not set up at that time.) */ 11193f542974SPaul B Schroeder 1120*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "port number is %d\n", port->number); 1121*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "serial number is %d\n", port->serial->minor); 1122*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "Bulkin endpoint is %d\n", port->bulk_in_endpointAddress); 1123*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "BulkOut endpoint is %d\n", port->bulk_out_endpointAddress); 1124*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "Interrupt endpoint is %d\n", port->interrupt_in_endpointAddress); 1125*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "port's number in the device is %d\n", mos7840_port->port_num); 11263f542974SPaul B Schroeder mos7840_port->read_urb = port->read_urb; 11273f542974SPaul B Schroeder 11283f542974SPaul B Schroeder /* set up our bulk in urb */ 1129093ea2d3SDonald Lee if ((serial->num_ports == 2) 1130093ea2d3SDonald Lee && ((((__u16)port->number - 1131093ea2d3SDonald Lee (__u16)(port->serial->minor)) % 2) != 0)) { 1132093ea2d3SDonald Lee usb_fill_bulk_urb(mos7840_port->read_urb, 1133093ea2d3SDonald Lee serial->dev, 1134093ea2d3SDonald Lee usb_rcvbulkpipe(serial->dev, 1135093ea2d3SDonald Lee (port->bulk_in_endpointAddress) + 2), 1136093ea2d3SDonald Lee port->bulk_in_buffer, 1137093ea2d3SDonald Lee mos7840_port->read_urb->transfer_buffer_length, 1138093ea2d3SDonald Lee mos7840_bulk_in_callback, mos7840_port); 1139093ea2d3SDonald Lee } else { 11403f542974SPaul B Schroeder usb_fill_bulk_urb(mos7840_port->read_urb, 11413f542974SPaul B Schroeder serial->dev, 11423f542974SPaul B Schroeder usb_rcvbulkpipe(serial->dev, 11433f542974SPaul B Schroeder port->bulk_in_endpointAddress), 11443f542974SPaul B Schroeder port->bulk_in_buffer, 11453f542974SPaul B Schroeder mos7840_port->read_urb->transfer_buffer_length, 11463f542974SPaul B Schroeder mos7840_bulk_in_callback, mos7840_port); 1147093ea2d3SDonald Lee } 11483f542974SPaul B Schroeder 1149*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s: bulkin endpoint is %d\n", __func__, port->bulk_in_endpointAddress); 115050de36f7SGreg Kroah-Hartman mos7840_port->read_urb_busy = true; 11513f542974SPaul B Schroeder response = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL); 11523f542974SPaul B Schroeder if (response) { 1153194343d9SGreg Kroah-Hartman dev_err(&port->dev, "%s - Error %d submitting control urb\n", 1154194343d9SGreg Kroah-Hartman __func__, response); 115550de36f7SGreg Kroah-Hartman mos7840_port->read_urb_busy = false; 11563f542974SPaul B Schroeder } 11573f542974SPaul B Schroeder 11583f542974SPaul B Schroeder /* initialize our wait queues */ 11593f542974SPaul B Schroeder init_waitqueue_head(&mos7840_port->wait_chase); 11603f542974SPaul B Schroeder init_waitqueue_head(&mos7840_port->delta_msr_wait); 11613f542974SPaul B Schroeder 11623f542974SPaul B Schroeder /* initialize our icount structure */ 11633f542974SPaul B Schroeder memset(&(mos7840_port->icount), 0x00, sizeof(mos7840_port->icount)); 11643f542974SPaul B Schroeder 11653f542974SPaul B Schroeder /* initialize our port settings */ 1166880af9dbSAlan Cox /* Must set to enable ints! */ 1167880af9dbSAlan Cox mos7840_port->shadowMCR = MCR_MASTER_IE; 11683f542974SPaul B Schroeder /* send a open port command */ 11693f542974SPaul B Schroeder mos7840_port->open = 1; 1170880af9dbSAlan Cox /* mos7840_change_port_settings(mos7840_port,old_termios); */ 11713f542974SPaul B Schroeder mos7840_port->icount.tx = 0; 11723f542974SPaul B Schroeder mos7840_port->icount.rx = 0; 11733f542974SPaul B Schroeder 11743f542974SPaul B Schroeder return 0; 11753f542974SPaul B Schroeder } 11763f542974SPaul B Schroeder 11773f542974SPaul B Schroeder /***************************************************************************** 11783f542974SPaul B Schroeder * mos7840_chars_in_buffer 11793f542974SPaul B Schroeder * this function is called by the tty driver when it wants to know how many 11803f542974SPaul B Schroeder * bytes of data we currently have outstanding in the port (data that has 11813f542974SPaul B Schroeder * been written, but hasn't made it out the port yet) 11823f542974SPaul B Schroeder * If successful, we return the number of bytes left to be written in the 11833f542974SPaul B Schroeder * system, 118495da310eSAlan Cox * Otherwise we return zero. 11853f542974SPaul B Schroeder *****************************************************************************/ 11863f542974SPaul B Schroeder 118795da310eSAlan Cox static int mos7840_chars_in_buffer(struct tty_struct *tty) 11883f542974SPaul B Schroeder { 118995da310eSAlan Cox struct usb_serial_port *port = tty->driver_data; 11903f542974SPaul B Schroeder int i; 11913f542974SPaul B Schroeder int chars = 0; 11920de9a702SOliver Neukum unsigned long flags; 11933f542974SPaul B Schroeder struct moschip_port *mos7840_port; 11943f542974SPaul B Schroeder 1195*9c134a14SGreg Kroah-Hartman if (mos7840_port_paranoia_check(port, __func__)) 119695da310eSAlan Cox return 0; 11973f542974SPaul B Schroeder 11983f542974SPaul B Schroeder mos7840_port = mos7840_get_port_private(port); 11993363155bSGreg Kroah-Hartman if (mos7840_port == NULL) 120095da310eSAlan Cox return 0; 12013f542974SPaul B Schroeder 12020de9a702SOliver Neukum spin_lock_irqsave(&mos7840_port->pool_lock, flags); 12035c263b92SMark Ferrell for (i = 0; i < NUM_URBS; ++i) { 12045c263b92SMark Ferrell if (mos7840_port->busy[i]) { 12055c263b92SMark Ferrell struct urb *urb = mos7840_port->write_urb_pool[i]; 12065c263b92SMark Ferrell chars += urb->transfer_buffer_length; 12075c263b92SMark Ferrell } 12085c263b92SMark Ferrell } 12090de9a702SOliver Neukum spin_unlock_irqrestore(&mos7840_port->pool_lock, flags); 1210*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars); 12110de9a702SOliver Neukum return chars; 12123f542974SPaul B Schroeder 12133f542974SPaul B Schroeder } 12143f542974SPaul B Schroeder 12153f542974SPaul B Schroeder /***************************************************************************** 12163f542974SPaul B Schroeder * mos7840_close 12173f542974SPaul B Schroeder * this function is called by the tty driver when a port is closed 12183f542974SPaul B Schroeder *****************************************************************************/ 12193f542974SPaul B Schroeder 1220335f8514SAlan Cox static void mos7840_close(struct usb_serial_port *port) 12213f542974SPaul B Schroeder { 12223f542974SPaul B Schroeder struct usb_serial *serial; 12233f542974SPaul B Schroeder struct moschip_port *mos7840_port; 12240de9a702SOliver Neukum struct moschip_port *port0; 12253f542974SPaul B Schroeder int j; 12263f542974SPaul B Schroeder __u16 Data; 12273f542974SPaul B Schroeder 1228*9c134a14SGreg Kroah-Hartman if (mos7840_port_paranoia_check(port, __func__)) 12293f542974SPaul B Schroeder return; 12303f542974SPaul B Schroeder 1231441b62c1SHarvey Harrison serial = mos7840_get_usb_serial(port, __func__); 1232*9c134a14SGreg Kroah-Hartman if (!serial) 12333f542974SPaul B Schroeder return; 12343f542974SPaul B Schroeder 12353f542974SPaul B Schroeder mos7840_port = mos7840_get_port_private(port); 12360de9a702SOliver Neukum port0 = mos7840_get_port_private(serial->port[0]); 12373f542974SPaul B Schroeder 12380de9a702SOliver Neukum if (mos7840_port == NULL || port0 == NULL) 12393f542974SPaul B Schroeder return; 12403f542974SPaul B Schroeder 12413f542974SPaul B Schroeder for (j = 0; j < NUM_URBS; ++j) 12423f542974SPaul B Schroeder usb_kill_urb(mos7840_port->write_urb_pool[j]); 12433f542974SPaul B Schroeder 12443f542974SPaul B Schroeder /* Freeing Write URBs */ 12453f542974SPaul B Schroeder for (j = 0; j < NUM_URBS; ++j) { 12463f542974SPaul B Schroeder if (mos7840_port->write_urb_pool[j]) { 12473f542974SPaul B Schroeder if (mos7840_port->write_urb_pool[j]->transfer_buffer) 12483f542974SPaul B Schroeder kfree(mos7840_port->write_urb_pool[j]-> 12493f542974SPaul B Schroeder transfer_buffer); 12503f542974SPaul B Schroeder 12513f542974SPaul B Schroeder usb_free_urb(mos7840_port->write_urb_pool[j]); 12523f542974SPaul B Schroeder } 12533f542974SPaul B Schroeder } 12543f542974SPaul B Schroeder 12553f542974SPaul B Schroeder /* While closing port, shutdown all bulk read, write * 12563f542974SPaul B Schroeder * and interrupt read if they exists */ 12573f542974SPaul B Schroeder if (serial->dev) { 12583f542974SPaul B Schroeder if (mos7840_port->write_urb) { 1259*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s", "Shutdown bulk write\n"); 12603f542974SPaul B Schroeder usb_kill_urb(mos7840_port->write_urb); 12613f542974SPaul B Schroeder } 12623f542974SPaul B Schroeder if (mos7840_port->read_urb) { 1263*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s", "Shutdown bulk read\n"); 12643f542974SPaul B Schroeder usb_kill_urb(mos7840_port->read_urb); 126550de36f7SGreg Kroah-Hartman mos7840_port->read_urb_busy = false; 12663f542974SPaul B Schroeder } 12673f542974SPaul B Schroeder if ((&mos7840_port->control_urb)) { 1268*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s", "Shutdown control read\n"); 1269880af9dbSAlan Cox /*/ usb_kill_urb (mos7840_port->control_urb); */ 12703f542974SPaul B Schroeder } 12713f542974SPaul B Schroeder } 1272880af9dbSAlan Cox /* if(mos7840_port->ctrl_buf != NULL) */ 1273880af9dbSAlan Cox /* kfree(mos7840_port->ctrl_buf); */ 12740de9a702SOliver Neukum port0->open_ports--; 1275*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s in close%d:in port%d\n", __func__, port0->open_ports, port->number); 12760de9a702SOliver Neukum if (port0->open_ports == 0) { 12773f542974SPaul B Schroeder if (serial->port[0]->interrupt_in_urb) { 1278*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "Shutdown interrupt_in_urb\n"); 12790de9a702SOliver Neukum usb_kill_urb(serial->port[0]->interrupt_in_urb); 12803f542974SPaul B Schroeder } 12813f542974SPaul B Schroeder } 12823f542974SPaul B Schroeder 12833f542974SPaul B Schroeder if (mos7840_port->write_urb) { 12843f542974SPaul B Schroeder /* if this urb had a transfer buffer already (old tx) free it */ 1285880af9dbSAlan Cox if (mos7840_port->write_urb->transfer_buffer != NULL) 12863f542974SPaul B Schroeder kfree(mos7840_port->write_urb->transfer_buffer); 12873f542974SPaul B Schroeder usb_free_urb(mos7840_port->write_urb); 12883f542974SPaul B Schroeder } 12893f542974SPaul B Schroeder 12903f542974SPaul B Schroeder Data = 0x0; 12913f542974SPaul B Schroeder mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data); 12923f542974SPaul B Schroeder 12933f542974SPaul B Schroeder Data = 0x00; 12943f542974SPaul B Schroeder mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data); 12953f542974SPaul B Schroeder 12963f542974SPaul B Schroeder mos7840_port->open = 0; 12973f542974SPaul B Schroeder } 12983f542974SPaul B Schroeder 12993f542974SPaul B Schroeder /************************************************************************ 13003f542974SPaul B Schroeder * 13013f542974SPaul B Schroeder * mos7840_block_until_chase_response 13023f542974SPaul B Schroeder * 13033f542974SPaul B Schroeder * This function will block the close until one of the following: 13043f542974SPaul B Schroeder * 1. Response to our Chase comes from mos7840 1305dc0d5c1eSJoe Perches * 2. A timeout of 10 seconds without activity has expired 13063f542974SPaul B Schroeder * (1K of mos7840 data @ 2400 baud ==> 4 sec to empty) 13073f542974SPaul B Schroeder * 13083f542974SPaul B Schroeder ************************************************************************/ 13093f542974SPaul B Schroeder 131095da310eSAlan Cox static void mos7840_block_until_chase_response(struct tty_struct *tty, 131195da310eSAlan Cox struct moschip_port *mos7840_port) 13123f542974SPaul B Schroeder { 13131e658489SMark Ferrell int timeout = msecs_to_jiffies(1000); 13143f542974SPaul B Schroeder int wait = 10; 13153f542974SPaul B Schroeder int count; 13163f542974SPaul B Schroeder 13173f542974SPaul B Schroeder while (1) { 131895da310eSAlan Cox count = mos7840_chars_in_buffer(tty); 13193f542974SPaul B Schroeder 13203f542974SPaul B Schroeder /* Check for Buffer status */ 1321880af9dbSAlan Cox if (count <= 0) 13223f542974SPaul B Schroeder return; 13233f542974SPaul B Schroeder 13243f542974SPaul B Schroeder /* Block the thread for a while */ 13253f542974SPaul B Schroeder interruptible_sleep_on_timeout(&mos7840_port->wait_chase, 13263f542974SPaul B Schroeder timeout); 13273f542974SPaul B Schroeder /* No activity.. count down section */ 13283f542974SPaul B Schroeder wait--; 13293f542974SPaul B Schroeder if (wait == 0) { 1330*9c134a14SGreg Kroah-Hartman dev_dbg(&mos7840_port->port->dev, "%s - TIMEOUT\n", __func__); 13313f542974SPaul B Schroeder return; 13323f542974SPaul B Schroeder } else { 1333dc0d5c1eSJoe Perches /* Reset timeout value back to seconds */ 13343f542974SPaul B Schroeder wait = 10; 13353f542974SPaul B Schroeder } 13363f542974SPaul B Schroeder } 13373f542974SPaul B Schroeder 13383f542974SPaul B Schroeder } 13393f542974SPaul B Schroeder 13403f542974SPaul B Schroeder /***************************************************************************** 13413f542974SPaul B Schroeder * mos7840_break 13423f542974SPaul B Schroeder * this function sends a break to the port 13433f542974SPaul B Schroeder *****************************************************************************/ 134495da310eSAlan Cox static void mos7840_break(struct tty_struct *tty, int break_state) 13453f542974SPaul B Schroeder { 134695da310eSAlan Cox struct usb_serial_port *port = tty->driver_data; 13473f542974SPaul B Schroeder unsigned char data; 13483f542974SPaul B Schroeder struct usb_serial *serial; 13493f542974SPaul B Schroeder struct moschip_port *mos7840_port; 13503f542974SPaul B Schroeder 1351*9c134a14SGreg Kroah-Hartman if (mos7840_port_paranoia_check(port, __func__)) 13523f542974SPaul B Schroeder return; 13533f542974SPaul B Schroeder 1354441b62c1SHarvey Harrison serial = mos7840_get_usb_serial(port, __func__); 1355*9c134a14SGreg Kroah-Hartman if (!serial) 13563f542974SPaul B Schroeder return; 13573f542974SPaul B Schroeder 13583f542974SPaul B Schroeder mos7840_port = mos7840_get_port_private(port); 13593f542974SPaul B Schroeder 1360880af9dbSAlan Cox if (mos7840_port == NULL) 13613f542974SPaul B Schroeder return; 13623f542974SPaul B Schroeder 136395da310eSAlan Cox if (serial->dev) 13643f542974SPaul B Schroeder /* flush and block until tx is empty */ 136595da310eSAlan Cox mos7840_block_until_chase_response(tty, mos7840_port); 13663f542974SPaul B Schroeder 136795da310eSAlan Cox if (break_state == -1) 13683f542974SPaul B Schroeder data = mos7840_port->shadowLCR | LCR_SET_BREAK; 136995da310eSAlan Cox else 13703f542974SPaul B Schroeder data = mos7840_port->shadowLCR & ~LCR_SET_BREAK; 13713f542974SPaul B Schroeder 13726b447f04SAlan Cox /* FIXME: no locking on shadowLCR anywhere in driver */ 13733f542974SPaul B Schroeder mos7840_port->shadowLCR = data; 1374*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s mos7840_port->shadowLCR is %x\n", __func__, mos7840_port->shadowLCR); 13753f542974SPaul B Schroeder mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, 13763f542974SPaul B Schroeder mos7840_port->shadowLCR); 13773f542974SPaul B Schroeder } 13783f542974SPaul B Schroeder 13793f542974SPaul B Schroeder /***************************************************************************** 13803f542974SPaul B Schroeder * mos7840_write_room 13813f542974SPaul B Schroeder * this function is called by the tty driver when it wants to know how many 13823f542974SPaul B Schroeder * bytes of data we can accept for a specific port. 13833f542974SPaul B Schroeder * If successful, we return the amount of room that we have for this port 13843f542974SPaul B Schroeder * Otherwise we return a negative error number. 13853f542974SPaul B Schroeder *****************************************************************************/ 13863f542974SPaul B Schroeder 138795da310eSAlan Cox static int mos7840_write_room(struct tty_struct *tty) 13883f542974SPaul B Schroeder { 138995da310eSAlan Cox struct usb_serial_port *port = tty->driver_data; 13903f542974SPaul B Schroeder int i; 13913f542974SPaul B Schroeder int room = 0; 13920de9a702SOliver Neukum unsigned long flags; 13933f542974SPaul B Schroeder struct moschip_port *mos7840_port; 13943f542974SPaul B Schroeder 1395*9c134a14SGreg Kroah-Hartman if (mos7840_port_paranoia_check(port, __func__)) 13963f542974SPaul B Schroeder return -1; 13973f542974SPaul B Schroeder 13983f542974SPaul B Schroeder mos7840_port = mos7840_get_port_private(port); 1399*9c134a14SGreg Kroah-Hartman if (mos7840_port == NULL) 14003f542974SPaul B Schroeder return -1; 14013f542974SPaul B Schroeder 14020de9a702SOliver Neukum spin_lock_irqsave(&mos7840_port->pool_lock, flags); 14033f542974SPaul B Schroeder for (i = 0; i < NUM_URBS; ++i) { 1404880af9dbSAlan Cox if (!mos7840_port->busy[i]) 14053f542974SPaul B Schroeder room += URB_TRANSFER_BUFFER_SIZE; 14063f542974SPaul B Schroeder } 14070de9a702SOliver Neukum spin_unlock_irqrestore(&mos7840_port->pool_lock, flags); 14083f542974SPaul B Schroeder 14090de9a702SOliver Neukum room = (room == 0) ? 0 : room - URB_TRANSFER_BUFFER_SIZE + 1; 1410*9c134a14SGreg Kroah-Hartman dev_dbg(&mos7840_port->port->dev, "%s - returns %d\n", __func__, room); 14110de9a702SOliver Neukum return room; 14123f542974SPaul B Schroeder 14133f542974SPaul B Schroeder } 14143f542974SPaul B Schroeder 14153f542974SPaul B Schroeder /***************************************************************************** 14163f542974SPaul B Schroeder * mos7840_write 14173f542974SPaul B Schroeder * this function is called by the tty driver when data should be written to 14183f542974SPaul B Schroeder * the port. 14193f542974SPaul B Schroeder * If successful, we return the number of bytes written, otherwise we 14203f542974SPaul B Schroeder * return a negative error number. 14213f542974SPaul B Schroeder *****************************************************************************/ 14223f542974SPaul B Schroeder 142395da310eSAlan Cox static int mos7840_write(struct tty_struct *tty, struct usb_serial_port *port, 14243f542974SPaul B Schroeder const unsigned char *data, int count) 14253f542974SPaul B Schroeder { 14263f542974SPaul B Schroeder int status; 14273f542974SPaul B Schroeder int i; 14283f542974SPaul B Schroeder int bytes_sent = 0; 14293f542974SPaul B Schroeder int transfer_size; 14300de9a702SOliver Neukum unsigned long flags; 14313f542974SPaul B Schroeder 14323f542974SPaul B Schroeder struct moschip_port *mos7840_port; 14333f542974SPaul B Schroeder struct usb_serial *serial; 14343f542974SPaul B Schroeder struct urb *urb; 1435880af9dbSAlan Cox /* __u16 Data; */ 14363f542974SPaul B Schroeder const unsigned char *current_position = data; 14373f542974SPaul B Schroeder unsigned char *data1; 14383f542974SPaul B Schroeder 14393f542974SPaul B Schroeder #ifdef NOTMOS7840 14403f542974SPaul B Schroeder Data = 0x00; 14413f542974SPaul B Schroeder status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data); 14423f542974SPaul B Schroeder mos7840_port->shadowLCR = Data; 1443*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s: LINE_CONTROL_REGISTER is %x\n", __func__, Data); 1444*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s: mos7840_port->shadowLCR is %x\n", __func__, mos7840_port->shadowLCR); 14453f542974SPaul B Schroeder 1446880af9dbSAlan Cox /* Data = 0x03; */ 1447880af9dbSAlan Cox /* status = mos7840_set_uart_reg(port,LINE_CONTROL_REGISTER,Data); */ 1448880af9dbSAlan Cox /* mos7840_port->shadowLCR=Data;//Need to add later */ 14493f542974SPaul B Schroeder 1450880af9dbSAlan Cox Data |= SERIAL_LCR_DLAB; /* data latch enable in LCR 0x80 */ 14513f542974SPaul B Schroeder status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data); 14523f542974SPaul B Schroeder 1453880af9dbSAlan Cox /* Data = 0x0c; */ 1454880af9dbSAlan Cox /* status = mos7840_set_uart_reg(port,DIVISOR_LATCH_LSB,Data); */ 14553f542974SPaul B Schroeder Data = 0x00; 14563f542974SPaul B Schroeder status = mos7840_get_uart_reg(port, DIVISOR_LATCH_LSB, &Data); 1457*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s: DLL value is %x\n", __func__, Data); 14583f542974SPaul B Schroeder 14593f542974SPaul B Schroeder Data = 0x0; 14603f542974SPaul B Schroeder status = mos7840_get_uart_reg(port, DIVISOR_LATCH_MSB, &Data); 1461*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s: DLM value is %x\n", __func__, Data); 14623f542974SPaul B Schroeder 14633f542974SPaul B Schroeder Data = Data & ~SERIAL_LCR_DLAB; 1464*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s: mos7840_port->shadowLCR is %x\n", __func__, mos7840_port->shadowLCR); 14653f542974SPaul B Schroeder status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data); 14663f542974SPaul B Schroeder #endif 14673f542974SPaul B Schroeder 1468*9c134a14SGreg Kroah-Hartman if (mos7840_port_paranoia_check(port, __func__)) 14693f542974SPaul B Schroeder return -1; 14703f542974SPaul B Schroeder 14713f542974SPaul B Schroeder serial = port->serial; 1472*9c134a14SGreg Kroah-Hartman if (mos7840_serial_paranoia_check(serial, __func__)) 14733f542974SPaul B Schroeder return -1; 14743f542974SPaul B Schroeder 14753f542974SPaul B Schroeder mos7840_port = mos7840_get_port_private(port); 1476*9c134a14SGreg Kroah-Hartman if (mos7840_port == NULL) 14773f542974SPaul B Schroeder return -1; 14783f542974SPaul B Schroeder 14793f542974SPaul B Schroeder /* try to find a free urb in the list */ 14803f542974SPaul B Schroeder urb = NULL; 14813f542974SPaul B Schroeder 14820de9a702SOliver Neukum spin_lock_irqsave(&mos7840_port->pool_lock, flags); 14833f542974SPaul B Schroeder for (i = 0; i < NUM_URBS; ++i) { 14840de9a702SOliver Neukum if (!mos7840_port->busy[i]) { 14850de9a702SOliver Neukum mos7840_port->busy[i] = 1; 14863f542974SPaul B Schroeder urb = mos7840_port->write_urb_pool[i]; 1487*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "URB:%d\n", i); 14883f542974SPaul B Schroeder break; 14893f542974SPaul B Schroeder } 14903f542974SPaul B Schroeder } 14910de9a702SOliver Neukum spin_unlock_irqrestore(&mos7840_port->pool_lock, flags); 14923f542974SPaul B Schroeder 14933f542974SPaul B Schroeder if (urb == NULL) { 1494*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - no more free urbs\n", __func__); 14953f542974SPaul B Schroeder goto exit; 14963f542974SPaul B Schroeder } 14973f542974SPaul B Schroeder 14983f542974SPaul B Schroeder if (urb->transfer_buffer == NULL) { 14993f542974SPaul B Schroeder urb->transfer_buffer = 15003f542974SPaul B Schroeder kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL); 15013f542974SPaul B Schroeder 15023f542974SPaul B Schroeder if (urb->transfer_buffer == NULL) { 150322a416c4SJohan Hovold dev_err_console(port, "%s no more kernel memory...\n", 1504194343d9SGreg Kroah-Hartman __func__); 15053f542974SPaul B Schroeder goto exit; 15063f542974SPaul B Schroeder } 15073f542974SPaul B Schroeder } 15083f542974SPaul B Schroeder transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE); 15093f542974SPaul B Schroeder 15103f542974SPaul B Schroeder memcpy(urb->transfer_buffer, current_position, transfer_size); 15113f542974SPaul B Schroeder 15123f542974SPaul B Schroeder /* fill urb with data and submit */ 1513093ea2d3SDonald Lee if ((serial->num_ports == 2) 1514093ea2d3SDonald Lee && ((((__u16)port->number - 1515093ea2d3SDonald Lee (__u16)(port->serial->minor)) % 2) != 0)) { 1516093ea2d3SDonald Lee usb_fill_bulk_urb(urb, 1517093ea2d3SDonald Lee serial->dev, 1518093ea2d3SDonald Lee usb_sndbulkpipe(serial->dev, 1519093ea2d3SDonald Lee (port->bulk_out_endpointAddress) + 2), 1520093ea2d3SDonald Lee urb->transfer_buffer, 1521093ea2d3SDonald Lee transfer_size, 1522093ea2d3SDonald Lee mos7840_bulk_out_data_callback, mos7840_port); 1523093ea2d3SDonald Lee } else { 15243f542974SPaul B Schroeder usb_fill_bulk_urb(urb, 15253f542974SPaul B Schroeder serial->dev, 15263f542974SPaul B Schroeder usb_sndbulkpipe(serial->dev, 15273f542974SPaul B Schroeder port->bulk_out_endpointAddress), 15283f542974SPaul B Schroeder urb->transfer_buffer, 15293f542974SPaul B Schroeder transfer_size, 15303f542974SPaul B Schroeder mos7840_bulk_out_data_callback, mos7840_port); 1531093ea2d3SDonald Lee } 15323f542974SPaul B Schroeder 15333f542974SPaul B Schroeder data1 = urb->transfer_buffer; 1534*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "bulkout endpoint is %d\n", port->bulk_out_endpointAddress); 15353f542974SPaul B Schroeder 15360eafe4deSDonald /* Turn on LED */ 15370eafe4deSDonald if (mos7840_port->has_led && !mos7840_port->led_flag) { 15380eafe4deSDonald mos7840_port->led_flag = true; 15390eafe4deSDonald mos7840_set_led_sync(port, MODEM_CONTROL_REGISTER, 0x0301); 15400eafe4deSDonald mod_timer(&mos7840_port->led_timer1, 15410eafe4deSDonald jiffies + msecs_to_jiffies(LED_ON_MS)); 15420eafe4deSDonald } 15430eafe4deSDonald 15443f542974SPaul B Schroeder /* send it down the pipe */ 15453f542974SPaul B Schroeder status = usb_submit_urb(urb, GFP_ATOMIC); 15463f542974SPaul B Schroeder 15473f542974SPaul B Schroeder if (status) { 15480de9a702SOliver Neukum mos7840_port->busy[i] = 0; 154922a416c4SJohan Hovold dev_err_console(port, "%s - usb_submit_urb(write bulk) failed " 1550194343d9SGreg Kroah-Hartman "with status = %d\n", __func__, status); 15513f542974SPaul B Schroeder bytes_sent = status; 15523f542974SPaul B Schroeder goto exit; 15533f542974SPaul B Schroeder } 15543f542974SPaul B Schroeder bytes_sent = transfer_size; 15553f542974SPaul B Schroeder mos7840_port->icount.tx += transfer_size; 15560de9a702SOliver Neukum smp_wmb(); 1557*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "mos7840_port->icount.tx is %d:\n", mos7840_port->icount.tx); 15583f542974SPaul B Schroeder exit: 15593f542974SPaul B Schroeder return bytes_sent; 15603f542974SPaul B Schroeder 15613f542974SPaul B Schroeder } 15623f542974SPaul B Schroeder 15633f542974SPaul B Schroeder /***************************************************************************** 15643f542974SPaul B Schroeder * mos7840_throttle 15653f542974SPaul B Schroeder * this function is called by the tty driver when it wants to stop the data 15663f542974SPaul B Schroeder * being read from the port. 15673f542974SPaul B Schroeder *****************************************************************************/ 15683f542974SPaul B Schroeder 156995da310eSAlan Cox static void mos7840_throttle(struct tty_struct *tty) 15703f542974SPaul B Schroeder { 157195da310eSAlan Cox struct usb_serial_port *port = tty->driver_data; 15723f542974SPaul B Schroeder struct moschip_port *mos7840_port; 15733f542974SPaul B Schroeder int status; 15743f542974SPaul B Schroeder 1575*9c134a14SGreg Kroah-Hartman if (mos7840_port_paranoia_check(port, __func__)) 15763f542974SPaul B Schroeder return; 15773f542974SPaul B Schroeder 15783f542974SPaul B Schroeder mos7840_port = mos7840_get_port_private(port); 15793f542974SPaul B Schroeder 15803f542974SPaul B Schroeder if (mos7840_port == NULL) 15813f542974SPaul B Schroeder return; 15823f542974SPaul B Schroeder 15833f542974SPaul B Schroeder if (!mos7840_port->open) { 1584*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s", "port not opened\n"); 15853f542974SPaul B Schroeder return; 15863f542974SPaul B Schroeder } 15873f542974SPaul B Schroeder 15883f542974SPaul B Schroeder /* if we are implementing XON/XOFF, send the stop character */ 15893f542974SPaul B Schroeder if (I_IXOFF(tty)) { 15903f542974SPaul B Schroeder unsigned char stop_char = STOP_CHAR(tty); 159195da310eSAlan Cox status = mos7840_write(tty, port, &stop_char, 1); 159295da310eSAlan Cox if (status <= 0) 15933f542974SPaul B Schroeder return; 15943f542974SPaul B Schroeder } 15953f542974SPaul B Schroeder /* if we are implementing RTS/CTS, toggle that line */ 15963f542974SPaul B Schroeder if (tty->termios->c_cflag & CRTSCTS) { 15973f542974SPaul B Schroeder mos7840_port->shadowMCR &= ~MCR_RTS; 159895da310eSAlan Cox status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, 15993f542974SPaul B Schroeder mos7840_port->shadowMCR); 160095da310eSAlan Cox if (status < 0) 16013f542974SPaul B Schroeder return; 16023f542974SPaul B Schroeder } 16033f542974SPaul B Schroeder } 16043f542974SPaul B Schroeder 16053f542974SPaul B Schroeder /***************************************************************************** 16063f542974SPaul B Schroeder * mos7840_unthrottle 1607880af9dbSAlan Cox * this function is called by the tty driver when it wants to resume 1608880af9dbSAlan Cox * the data being read from the port (called after mos7840_throttle is 1609880af9dbSAlan Cox * called) 16103f542974SPaul B Schroeder *****************************************************************************/ 161195da310eSAlan Cox static void mos7840_unthrottle(struct tty_struct *tty) 16123f542974SPaul B Schroeder { 161395da310eSAlan Cox struct usb_serial_port *port = tty->driver_data; 16143f542974SPaul B Schroeder int status; 16153f542974SPaul B Schroeder struct moschip_port *mos7840_port = mos7840_get_port_private(port); 16163f542974SPaul B Schroeder 1617*9c134a14SGreg Kroah-Hartman if (mos7840_port_paranoia_check(port, __func__)) 16183f542974SPaul B Schroeder return; 16193f542974SPaul B Schroeder 16203f542974SPaul B Schroeder if (mos7840_port == NULL) 16213f542974SPaul B Schroeder return; 16223f542974SPaul B Schroeder 16233f542974SPaul B Schroeder if (!mos7840_port->open) { 1624*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - port not opened\n", __func__); 16253f542974SPaul B Schroeder return; 16263f542974SPaul B Schroeder } 16273f542974SPaul B Schroeder 16283f542974SPaul B Schroeder /* if we are implementing XON/XOFF, send the start character */ 16293f542974SPaul B Schroeder if (I_IXOFF(tty)) { 16303f542974SPaul B Schroeder unsigned char start_char = START_CHAR(tty); 163195da310eSAlan Cox status = mos7840_write(tty, port, &start_char, 1); 163295da310eSAlan Cox if (status <= 0) 16333f542974SPaul B Schroeder return; 16343f542974SPaul B Schroeder } 16353f542974SPaul B Schroeder 16363f542974SPaul B Schroeder /* if we are implementing RTS/CTS, toggle that line */ 16373f542974SPaul B Schroeder if (tty->termios->c_cflag & CRTSCTS) { 16383f542974SPaul B Schroeder mos7840_port->shadowMCR |= MCR_RTS; 163995da310eSAlan Cox status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, 16403f542974SPaul B Schroeder mos7840_port->shadowMCR); 164195da310eSAlan Cox if (status < 0) 16423f542974SPaul B Schroeder return; 16433f542974SPaul B Schroeder } 16443f542974SPaul B Schroeder } 16453f542974SPaul B Schroeder 164660b33c13SAlan Cox static int mos7840_tiocmget(struct tty_struct *tty) 16473f542974SPaul B Schroeder { 164895da310eSAlan Cox struct usb_serial_port *port = tty->driver_data; 16493f542974SPaul B Schroeder struct moschip_port *mos7840_port; 16503f542974SPaul B Schroeder unsigned int result; 16513f542974SPaul B Schroeder __u16 msr; 16523f542974SPaul B Schroeder __u16 mcr; 1653880af9dbSAlan Cox int status; 16543f542974SPaul B Schroeder mos7840_port = mos7840_get_port_private(port); 16553f542974SPaul B Schroeder 16563f542974SPaul B Schroeder if (mos7840_port == NULL) 16573f542974SPaul B Schroeder return -ENODEV; 16583f542974SPaul B Schroeder 16593f542974SPaul B Schroeder status = mos7840_get_uart_reg(port, MODEM_STATUS_REGISTER, &msr); 16603f542974SPaul B Schroeder status = mos7840_get_uart_reg(port, MODEM_CONTROL_REGISTER, &mcr); 16613f542974SPaul B Schroeder result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0) 16623f542974SPaul B Schroeder | ((mcr & MCR_RTS) ? TIOCM_RTS : 0) 16633f542974SPaul B Schroeder | ((mcr & MCR_LOOPBACK) ? TIOCM_LOOP : 0) 16643f542974SPaul B Schroeder | ((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0) 16653f542974SPaul B Schroeder | ((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0) 16663f542974SPaul B Schroeder | ((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0) 16673f542974SPaul B Schroeder | ((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0); 16683f542974SPaul B Schroeder 1669*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - 0x%04X\n", __func__, result); 16703f542974SPaul B Schroeder 16713f542974SPaul B Schroeder return result; 16723f542974SPaul B Schroeder } 16733f542974SPaul B Schroeder 167420b9d177SAlan Cox static int mos7840_tiocmset(struct tty_struct *tty, 16753f542974SPaul B Schroeder unsigned int set, unsigned int clear) 16763f542974SPaul B Schroeder { 167795da310eSAlan Cox struct usb_serial_port *port = tty->driver_data; 16783f542974SPaul B Schroeder struct moschip_port *mos7840_port; 16793f542974SPaul B Schroeder unsigned int mcr; 168087521c46SRoel Kluin int status; 16813f542974SPaul B Schroeder 16823f542974SPaul B Schroeder mos7840_port = mos7840_get_port_private(port); 16833f542974SPaul B Schroeder 16843f542974SPaul B Schroeder if (mos7840_port == NULL) 16853f542974SPaul B Schroeder return -ENODEV; 16863f542974SPaul B Schroeder 1687e2984494SAlan Cox /* FIXME: What locks the port registers ? */ 16883f542974SPaul B Schroeder mcr = mos7840_port->shadowMCR; 16893f542974SPaul B Schroeder if (clear & TIOCM_RTS) 16903f542974SPaul B Schroeder mcr &= ~MCR_RTS; 16913f542974SPaul B Schroeder if (clear & TIOCM_DTR) 16923f542974SPaul B Schroeder mcr &= ~MCR_DTR; 16933f542974SPaul B Schroeder if (clear & TIOCM_LOOP) 16943f542974SPaul B Schroeder mcr &= ~MCR_LOOPBACK; 16953f542974SPaul B Schroeder 16963f542974SPaul B Schroeder if (set & TIOCM_RTS) 16973f542974SPaul B Schroeder mcr |= MCR_RTS; 16983f542974SPaul B Schroeder if (set & TIOCM_DTR) 16993f542974SPaul B Schroeder mcr |= MCR_DTR; 17003f542974SPaul B Schroeder if (set & TIOCM_LOOP) 17013f542974SPaul B Schroeder mcr |= MCR_LOOPBACK; 17023f542974SPaul B Schroeder 17033f542974SPaul B Schroeder mos7840_port->shadowMCR = mcr; 17043f542974SPaul B Schroeder 17053f542974SPaul B Schroeder status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, mcr); 17063f542974SPaul B Schroeder if (status < 0) { 1707*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "setting MODEM_CONTROL_REGISTER Failed\n"); 170887521c46SRoel Kluin return status; 17093f542974SPaul B Schroeder } 17103f542974SPaul B Schroeder 17113f542974SPaul B Schroeder return 0; 17123f542974SPaul B Schroeder } 17133f542974SPaul B Schroeder 17143f542974SPaul B Schroeder /***************************************************************************** 17153f542974SPaul B Schroeder * mos7840_calc_baud_rate_divisor 17163f542974SPaul B Schroeder * this function calculates the proper baud rate divisor for the specified 17173f542974SPaul B Schroeder * baud rate. 17183f542974SPaul B Schroeder *****************************************************************************/ 1719*9c134a14SGreg Kroah-Hartman static int mos7840_calc_baud_rate_divisor(struct usb_serial_port *port, 1720*9c134a14SGreg Kroah-Hartman int baudRate, int *divisor, 17213f542974SPaul B Schroeder __u16 *clk_sel_val) 17223f542974SPaul B Schroeder { 1723*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - %d\n", __func__, baudRate); 17243f542974SPaul B Schroeder 17253f542974SPaul B Schroeder if (baudRate <= 115200) { 17263f542974SPaul B Schroeder *divisor = 115200 / baudRate; 17273f542974SPaul B Schroeder *clk_sel_val = 0x0; 17283f542974SPaul B Schroeder } 17293f542974SPaul B Schroeder if ((baudRate > 115200) && (baudRate <= 230400)) { 17303f542974SPaul B Schroeder *divisor = 230400 / baudRate; 17313f542974SPaul B Schroeder *clk_sel_val = 0x10; 17323f542974SPaul B Schroeder } else if ((baudRate > 230400) && (baudRate <= 403200)) { 17333f542974SPaul B Schroeder *divisor = 403200 / baudRate; 17343f542974SPaul B Schroeder *clk_sel_val = 0x20; 17353f542974SPaul B Schroeder } else if ((baudRate > 403200) && (baudRate <= 460800)) { 17363f542974SPaul B Schroeder *divisor = 460800 / baudRate; 17373f542974SPaul B Schroeder *clk_sel_val = 0x30; 17383f542974SPaul B Schroeder } else if ((baudRate > 460800) && (baudRate <= 806400)) { 17393f542974SPaul B Schroeder *divisor = 806400 / baudRate; 17403f542974SPaul B Schroeder *clk_sel_val = 0x40; 17413f542974SPaul B Schroeder } else if ((baudRate > 806400) && (baudRate <= 921600)) { 17423f542974SPaul B Schroeder *divisor = 921600 / baudRate; 17433f542974SPaul B Schroeder *clk_sel_val = 0x50; 17443f542974SPaul B Schroeder } else if ((baudRate > 921600) && (baudRate <= 1572864)) { 17453f542974SPaul B Schroeder *divisor = 1572864 / baudRate; 17463f542974SPaul B Schroeder *clk_sel_val = 0x60; 17473f542974SPaul B Schroeder } else if ((baudRate > 1572864) && (baudRate <= 3145728)) { 17483f542974SPaul B Schroeder *divisor = 3145728 / baudRate; 17493f542974SPaul B Schroeder *clk_sel_val = 0x70; 17503f542974SPaul B Schroeder } 17513f542974SPaul B Schroeder return 0; 17523f542974SPaul B Schroeder 17533f542974SPaul B Schroeder #ifdef NOTMCS7840 17543f542974SPaul B Schroeder 17553f542974SPaul B Schroeder for (i = 0; i < ARRAY_SIZE(mos7840_divisor_table); i++) { 17563f542974SPaul B Schroeder if (mos7840_divisor_table[i].BaudRate == baudrate) { 17573f542974SPaul B Schroeder *divisor = mos7840_divisor_table[i].Divisor; 17583f542974SPaul B Schroeder return 0; 17593f542974SPaul B Schroeder } 17603f542974SPaul B Schroeder } 17613f542974SPaul B Schroeder 17623f542974SPaul B Schroeder /* After trying for all the standard baud rates * 17633f542974SPaul B Schroeder * Try calculating the divisor for this baud rate */ 17643f542974SPaul B Schroeder 17653f542974SPaul B Schroeder if (baudrate > 75 && baudrate < 230400) { 17663f542974SPaul B Schroeder /* get the divisor */ 17673f542974SPaul B Schroeder custom = (__u16) (230400L / baudrate); 17683f542974SPaul B Schroeder 17693f542974SPaul B Schroeder /* Check for round off */ 17703f542974SPaul B Schroeder round1 = (__u16) (2304000L / baudrate); 17713f542974SPaul B Schroeder round = (__u16) (round1 - (custom * 10)); 1772880af9dbSAlan Cox if (round > 4) 17733f542974SPaul B Schroeder custom++; 17743f542974SPaul B Schroeder *divisor = custom; 17753f542974SPaul B Schroeder 1776*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, " Baud %d = %d\n", baudrate, custom); 17773f542974SPaul B Schroeder return 0; 17783f542974SPaul B Schroeder } 17793f542974SPaul B Schroeder 1780*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s", " Baud calculation Failed...\n"); 17813f542974SPaul B Schroeder return -1; 17823f542974SPaul B Schroeder #endif 17833f542974SPaul B Schroeder } 17843f542974SPaul B Schroeder 17853f542974SPaul B Schroeder /***************************************************************************** 17863f542974SPaul B Schroeder * mos7840_send_cmd_write_baud_rate 17873f542974SPaul B Schroeder * this function sends the proper command to change the baud rate of the 17883f542974SPaul B Schroeder * specified port. 17893f542974SPaul B Schroeder *****************************************************************************/ 17903f542974SPaul B Schroeder 17913f542974SPaul B Schroeder static int mos7840_send_cmd_write_baud_rate(struct moschip_port *mos7840_port, 17923f542974SPaul B Schroeder int baudRate) 17933f542974SPaul B Schroeder { 17943f542974SPaul B Schroeder int divisor = 0; 17953f542974SPaul B Schroeder int status; 17963f542974SPaul B Schroeder __u16 Data; 17973f542974SPaul B Schroeder unsigned char number; 17983f542974SPaul B Schroeder __u16 clk_sel_val; 17993f542974SPaul B Schroeder struct usb_serial_port *port; 18003f542974SPaul B Schroeder 18013f542974SPaul B Schroeder if (mos7840_port == NULL) 18023f542974SPaul B Schroeder return -1; 18033f542974SPaul B Schroeder 1804*9c134a14SGreg Kroah-Hartman port = mos7840_port->port; 1805*9c134a14SGreg Kroah-Hartman if (mos7840_port_paranoia_check(port, __func__)) 18063f542974SPaul B Schroeder return -1; 18073f542974SPaul B Schroeder 1808*9c134a14SGreg Kroah-Hartman if (mos7840_serial_paranoia_check(port->serial, __func__)) 18093f542974SPaul B Schroeder return -1; 18103f542974SPaul B Schroeder 18113f542974SPaul B Schroeder number = mos7840_port->port->number - mos7840_port->port->serial->minor; 18123f542974SPaul B Schroeder 1813*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - port = %d, baud = %d\n", __func__, 18143f542974SPaul B Schroeder mos7840_port->port->number, baudRate); 1815880af9dbSAlan Cox /* reset clk_uart_sel in spregOffset */ 18163f542974SPaul B Schroeder if (baudRate > 115200) { 18173f542974SPaul B Schroeder #ifdef HW_flow_control 1818880af9dbSAlan Cox /* NOTE: need to see the pther register to modify */ 1819880af9dbSAlan Cox /* setting h/w flow control bit to 1 */ 18203f542974SPaul B Schroeder Data = 0x2b; 18213f542974SPaul B Schroeder mos7840_port->shadowMCR = Data; 1822880af9dbSAlan Cox status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, 1823880af9dbSAlan Cox Data); 18243f542974SPaul B Schroeder if (status < 0) { 1825*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n"); 18263f542974SPaul B Schroeder return -1; 18273f542974SPaul B Schroeder } 18283f542974SPaul B Schroeder #endif 18293f542974SPaul B Schroeder 18303f542974SPaul B Schroeder } else { 18313f542974SPaul B Schroeder #ifdef HW_flow_control 1832880af9dbSAlan Cox /* setting h/w flow control bit to 0 */ 18333f542974SPaul B Schroeder Data = 0xb; 18343f542974SPaul B Schroeder mos7840_port->shadowMCR = Data; 1835880af9dbSAlan Cox status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, 1836880af9dbSAlan Cox Data); 18373f542974SPaul B Schroeder if (status < 0) { 1838*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n"); 18393f542974SPaul B Schroeder return -1; 18403f542974SPaul B Schroeder } 18413f542974SPaul B Schroeder #endif 18423f542974SPaul B Schroeder 18433f542974SPaul B Schroeder } 18443f542974SPaul B Schroeder 1845880af9dbSAlan Cox if (1) { /* baudRate <= 115200) */ 18463f542974SPaul B Schroeder clk_sel_val = 0x0; 18473f542974SPaul B Schroeder Data = 0x0; 1848*9c134a14SGreg Kroah-Hartman status = mos7840_calc_baud_rate_divisor(port, baudRate, &divisor, 18493f542974SPaul B Schroeder &clk_sel_val); 1850880af9dbSAlan Cox status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, 18513f542974SPaul B Schroeder &Data); 18523f542974SPaul B Schroeder if (status < 0) { 1853*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "reading spreg failed in set_serial_baud\n"); 18543f542974SPaul B Schroeder return -1; 18553f542974SPaul B Schroeder } 18563f542974SPaul B Schroeder Data = (Data & 0x8f) | clk_sel_val; 1857880af9dbSAlan Cox status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, 1858880af9dbSAlan Cox Data); 18593f542974SPaul B Schroeder if (status < 0) { 1860*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n"); 18613f542974SPaul B Schroeder return -1; 18623f542974SPaul B Schroeder } 18633f542974SPaul B Schroeder /* Calculate the Divisor */ 18643f542974SPaul B Schroeder 18653f542974SPaul B Schroeder if (status) { 1866194343d9SGreg Kroah-Hartman dev_err(&port->dev, "%s - bad baud rate\n", __func__); 18673f542974SPaul B Schroeder return status; 18683f542974SPaul B Schroeder } 18693f542974SPaul B Schroeder /* Enable access to divisor latch */ 18703f542974SPaul B Schroeder Data = mos7840_port->shadowLCR | SERIAL_LCR_DLAB; 18713f542974SPaul B Schroeder mos7840_port->shadowLCR = Data; 18723f542974SPaul B Schroeder mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data); 18733f542974SPaul B Schroeder 18743f542974SPaul B Schroeder /* Write the divisor */ 18753f542974SPaul B Schroeder Data = (unsigned char)(divisor & 0xff); 1876*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "set_serial_baud Value to write DLL is %x\n", Data); 18773f542974SPaul B Schroeder mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data); 18783f542974SPaul B Schroeder 18793f542974SPaul B Schroeder Data = (unsigned char)((divisor & 0xff00) >> 8); 1880*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "set_serial_baud Value to write DLM is %x\n", Data); 18813f542974SPaul B Schroeder mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data); 18823f542974SPaul B Schroeder 18833f542974SPaul B Schroeder /* Disable access to divisor latch */ 18843f542974SPaul B Schroeder Data = mos7840_port->shadowLCR & ~SERIAL_LCR_DLAB; 18853f542974SPaul B Schroeder mos7840_port->shadowLCR = Data; 18863f542974SPaul B Schroeder mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data); 18873f542974SPaul B Schroeder 18883f542974SPaul B Schroeder } 18893f542974SPaul B Schroeder return status; 18903f542974SPaul B Schroeder } 18913f542974SPaul B Schroeder 18923f542974SPaul B Schroeder /***************************************************************************** 18933f542974SPaul B Schroeder * mos7840_change_port_settings 18943f542974SPaul B Schroeder * This routine is called to set the UART on the device to match 18953f542974SPaul B Schroeder * the specified new settings. 18963f542974SPaul B Schroeder *****************************************************************************/ 18973f542974SPaul B Schroeder 189895da310eSAlan Cox static void mos7840_change_port_settings(struct tty_struct *tty, 189995da310eSAlan Cox struct moschip_port *mos7840_port, struct ktermios *old_termios) 19003f542974SPaul B Schroeder { 19013f542974SPaul B Schroeder int baud; 19023f542974SPaul B Schroeder unsigned cflag; 19033f542974SPaul B Schroeder unsigned iflag; 19043f542974SPaul B Schroeder __u8 lData; 19053f542974SPaul B Schroeder __u8 lParity; 19063f542974SPaul B Schroeder __u8 lStop; 19073f542974SPaul B Schroeder int status; 19083f542974SPaul B Schroeder __u16 Data; 19093f542974SPaul B Schroeder struct usb_serial_port *port; 19103f542974SPaul B Schroeder struct usb_serial *serial; 19113f542974SPaul B Schroeder 19123f542974SPaul B Schroeder if (mos7840_port == NULL) 19133f542974SPaul B Schroeder return; 19143f542974SPaul B Schroeder 1915*9c134a14SGreg Kroah-Hartman port = mos7840_port->port; 19163f542974SPaul B Schroeder 1917*9c134a14SGreg Kroah-Hartman if (mos7840_port_paranoia_check(port, __func__)) 19183f542974SPaul B Schroeder return; 19193f542974SPaul B Schroeder 1920*9c134a14SGreg Kroah-Hartman if (mos7840_serial_paranoia_check(port->serial, __func__)) 19213f542974SPaul B Schroeder return; 19223f542974SPaul B Schroeder 19233f542974SPaul B Schroeder serial = port->serial; 19243f542974SPaul B Schroeder 19253f542974SPaul B Schroeder if (!mos7840_port->open) { 1926*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - port not opened\n", __func__); 19273f542974SPaul B Schroeder return; 19283f542974SPaul B Schroeder } 19293f542974SPaul B Schroeder 19303f542974SPaul B Schroeder lData = LCR_BITS_8; 19313f542974SPaul B Schroeder lStop = LCR_STOP_1; 19323f542974SPaul B Schroeder lParity = LCR_PAR_NONE; 19333f542974SPaul B Schroeder 19343f542974SPaul B Schroeder cflag = tty->termios->c_cflag; 19353f542974SPaul B Schroeder iflag = tty->termios->c_iflag; 19363f542974SPaul B Schroeder 19373f542974SPaul B Schroeder /* Change the number of bits */ 19383f542974SPaul B Schroeder if (cflag & CSIZE) { 19393f542974SPaul B Schroeder switch (cflag & CSIZE) { 19403f542974SPaul B Schroeder case CS5: 19413f542974SPaul B Schroeder lData = LCR_BITS_5; 19423f542974SPaul B Schroeder break; 19433f542974SPaul B Schroeder 19443f542974SPaul B Schroeder case CS6: 19453f542974SPaul B Schroeder lData = LCR_BITS_6; 19463f542974SPaul B Schroeder break; 19473f542974SPaul B Schroeder 19483f542974SPaul B Schroeder case CS7: 19493f542974SPaul B Schroeder lData = LCR_BITS_7; 19503f542974SPaul B Schroeder break; 19513f542974SPaul B Schroeder default: 19523f542974SPaul B Schroeder case CS8: 19533f542974SPaul B Schroeder lData = LCR_BITS_8; 19543f542974SPaul B Schroeder break; 19553f542974SPaul B Schroeder } 19563f542974SPaul B Schroeder } 19573f542974SPaul B Schroeder /* Change the Parity bit */ 19583f542974SPaul B Schroeder if (cflag & PARENB) { 19593f542974SPaul B Schroeder if (cflag & PARODD) { 19603f542974SPaul B Schroeder lParity = LCR_PAR_ODD; 1961*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - parity = odd\n", __func__); 19623f542974SPaul B Schroeder } else { 19633f542974SPaul B Schroeder lParity = LCR_PAR_EVEN; 1964*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - parity = even\n", __func__); 19653f542974SPaul B Schroeder } 19663f542974SPaul B Schroeder 19673f542974SPaul B Schroeder } else { 1968*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - parity = none\n", __func__); 19693f542974SPaul B Schroeder } 19703f542974SPaul B Schroeder 1971880af9dbSAlan Cox if (cflag & CMSPAR) 19723f542974SPaul B Schroeder lParity = lParity | 0x20; 19733f542974SPaul B Schroeder 19743f542974SPaul B Schroeder /* Change the Stop bit */ 19753f542974SPaul B Schroeder if (cflag & CSTOPB) { 19763f542974SPaul B Schroeder lStop = LCR_STOP_2; 1977*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - stop bits = 2\n", __func__); 19783f542974SPaul B Schroeder } else { 19793f542974SPaul B Schroeder lStop = LCR_STOP_1; 1980*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - stop bits = 1\n", __func__); 19813f542974SPaul B Schroeder } 19823f542974SPaul B Schroeder 19833f542974SPaul B Schroeder /* Update the LCR with the correct value */ 19843f542974SPaul B Schroeder mos7840_port->shadowLCR &= 19853f542974SPaul B Schroeder ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK); 19863f542974SPaul B Schroeder mos7840_port->shadowLCR |= (lData | lParity | lStop); 19873f542974SPaul B Schroeder 1988*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - mos7840_port->shadowLCR is %x\n", __func__, 19893f542974SPaul B Schroeder mos7840_port->shadowLCR); 19903f542974SPaul B Schroeder /* Disable Interrupts */ 19913f542974SPaul B Schroeder Data = 0x00; 19923f542974SPaul B Schroeder mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data); 19933f542974SPaul B Schroeder 19943f542974SPaul B Schroeder Data = 0x00; 19953f542974SPaul B Schroeder mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data); 19963f542974SPaul B Schroeder 19973f542974SPaul B Schroeder Data = 0xcf; 19983f542974SPaul B Schroeder mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data); 19993f542974SPaul B Schroeder 20003f542974SPaul B Schroeder /* Send the updated LCR value to the mos7840 */ 20013f542974SPaul B Schroeder Data = mos7840_port->shadowLCR; 20023f542974SPaul B Schroeder 20033f542974SPaul B Schroeder mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data); 20043f542974SPaul B Schroeder 20053f542974SPaul B Schroeder Data = 0x00b; 20063f542974SPaul B Schroeder mos7840_port->shadowMCR = Data; 20073f542974SPaul B Schroeder mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data); 20083f542974SPaul B Schroeder Data = 0x00b; 20093f542974SPaul B Schroeder mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data); 20103f542974SPaul B Schroeder 20113f542974SPaul B Schroeder /* set up the MCR register and send it to the mos7840 */ 20123f542974SPaul B Schroeder 20133f542974SPaul B Schroeder mos7840_port->shadowMCR = MCR_MASTER_IE; 2014880af9dbSAlan Cox if (cflag & CBAUD) 20153f542974SPaul B Schroeder mos7840_port->shadowMCR |= (MCR_DTR | MCR_RTS); 20163f542974SPaul B Schroeder 2017880af9dbSAlan Cox if (cflag & CRTSCTS) 20183f542974SPaul B Schroeder mos7840_port->shadowMCR |= (MCR_XON_ANY); 2019880af9dbSAlan Cox else 20203f542974SPaul B Schroeder mos7840_port->shadowMCR &= ~(MCR_XON_ANY); 20213f542974SPaul B Schroeder 20223f542974SPaul B Schroeder Data = mos7840_port->shadowMCR; 20233f542974SPaul B Schroeder mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data); 20243f542974SPaul B Schroeder 20253f542974SPaul B Schroeder /* Determine divisor based on baud rate */ 20263f542974SPaul B Schroeder baud = tty_get_baud_rate(tty); 20273f542974SPaul B Schroeder 20283f542974SPaul B Schroeder if (!baud) { 20293f542974SPaul B Schroeder /* pick a default, any default... */ 2030*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s", "Picked default baud...\n"); 20313f542974SPaul B Schroeder baud = 9600; 20323f542974SPaul B Schroeder } 20333f542974SPaul B Schroeder 2034*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - baud rate = %d\n", __func__, baud); 20353f542974SPaul B Schroeder status = mos7840_send_cmd_write_baud_rate(mos7840_port, baud); 20363f542974SPaul B Schroeder 20373f542974SPaul B Schroeder /* Enable Interrupts */ 20383f542974SPaul B Schroeder Data = 0x0c; 20393f542974SPaul B Schroeder mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data); 20403f542974SPaul B Schroeder 204150de36f7SGreg Kroah-Hartman if (mos7840_port->read_urb_busy == false) { 204250de36f7SGreg Kroah-Hartman mos7840_port->read_urb_busy = true; 20433f542974SPaul B Schroeder status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC); 20443f542974SPaul B Schroeder if (status) { 2045*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n", 20463f542974SPaul B Schroeder status); 204750de36f7SGreg Kroah-Hartman mos7840_port->read_urb_busy = false; 20483f542974SPaul B Schroeder } 20493f542974SPaul B Schroeder } 20503f542974SPaul B Schroeder wake_up(&mos7840_port->delta_msr_wait); 20513f542974SPaul B Schroeder mos7840_port->delta_msr_cond = 1; 2052*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - mos7840_port->shadowLCR is End %x\n", __func__, 20533f542974SPaul B Schroeder mos7840_port->shadowLCR); 20543f542974SPaul B Schroeder } 20553f542974SPaul B Schroeder 20563f542974SPaul B Schroeder /***************************************************************************** 20573f542974SPaul B Schroeder * mos7840_set_termios 20583f542974SPaul B Schroeder * this function is called by the tty driver when it wants to change 20593f542974SPaul B Schroeder * the termios structure 20603f542974SPaul B Schroeder *****************************************************************************/ 20613f542974SPaul B Schroeder 206295da310eSAlan Cox static void mos7840_set_termios(struct tty_struct *tty, 206395da310eSAlan Cox struct usb_serial_port *port, 2064606d099cSAlan Cox struct ktermios *old_termios) 20653f542974SPaul B Schroeder { 20663f542974SPaul B Schroeder int status; 20673f542974SPaul B Schroeder unsigned int cflag; 20683f542974SPaul B Schroeder struct usb_serial *serial; 20693f542974SPaul B Schroeder struct moschip_port *mos7840_port; 20703363155bSGreg Kroah-Hartman 2071*9c134a14SGreg Kroah-Hartman if (mos7840_port_paranoia_check(port, __func__)) 20723f542974SPaul B Schroeder return; 20733f542974SPaul B Schroeder 20743f542974SPaul B Schroeder serial = port->serial; 20753f542974SPaul B Schroeder 2076*9c134a14SGreg Kroah-Hartman if (mos7840_serial_paranoia_check(serial, __func__)) 20773f542974SPaul B Schroeder return; 20783f542974SPaul B Schroeder 20793f542974SPaul B Schroeder mos7840_port = mos7840_get_port_private(port); 20803f542974SPaul B Schroeder 20813f542974SPaul B Schroeder if (mos7840_port == NULL) 20823f542974SPaul B Schroeder return; 20833f542974SPaul B Schroeder 20843f542974SPaul B Schroeder if (!mos7840_port->open) { 2085*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - port not opened\n", __func__); 20863f542974SPaul B Schroeder return; 20873f542974SPaul B Schroeder } 20883f542974SPaul B Schroeder 2089*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s", "setting termios - \n"); 20903f542974SPaul B Schroeder 20913f542974SPaul B Schroeder cflag = tty->termios->c_cflag; 20923f542974SPaul B Schroeder 2093*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - clfag %08x iflag %08x\n", __func__, 20943f542974SPaul B Schroeder tty->termios->c_cflag, RELEVANT_IFLAG(tty->termios->c_iflag)); 2095*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - old clfag %08x old iflag %08x\n", __func__, 20963f542974SPaul B Schroeder old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag)); 2097*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - port %d\n", __func__, port->number); 20983f542974SPaul B Schroeder 20993f542974SPaul B Schroeder /* change the port settings to the new ones specified */ 21003f542974SPaul B Schroeder 210195da310eSAlan Cox mos7840_change_port_settings(tty, mos7840_port, old_termios); 21023f542974SPaul B Schroeder 21033f542974SPaul B Schroeder if (!mos7840_port->read_urb) { 2104*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s", "URB KILLED !!!!!\n"); 21053f542974SPaul B Schroeder return; 21063f542974SPaul B Schroeder } 21073f542974SPaul B Schroeder 210850de36f7SGreg Kroah-Hartman if (mos7840_port->read_urb_busy == false) { 210950de36f7SGreg Kroah-Hartman mos7840_port->read_urb_busy = true; 21103f542974SPaul B Schroeder status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC); 21113f542974SPaul B Schroeder if (status) { 2112*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n", 21133f542974SPaul B Schroeder status); 211450de36f7SGreg Kroah-Hartman mos7840_port->read_urb_busy = false; 21153f542974SPaul B Schroeder } 21163f542974SPaul B Schroeder } 21173f542974SPaul B Schroeder } 21183f542974SPaul B Schroeder 21193f542974SPaul B Schroeder /***************************************************************************** 21203f542974SPaul B Schroeder * mos7840_get_lsr_info - get line status register info 21213f542974SPaul B Schroeder * 21223f542974SPaul B Schroeder * Purpose: Let user call ioctl() to get info when the UART physically 21233f542974SPaul B Schroeder * is emptied. On bus types like RS485, the transmitter must 21243f542974SPaul B Schroeder * release the bus after transmitting. This must be done when 21253f542974SPaul B Schroeder * the transmit shift register is empty, not be done when the 21263f542974SPaul B Schroeder * transmit holding register is empty. This functionality 21273f542974SPaul B Schroeder * allows an RS485 driver to be written in user space. 21283f542974SPaul B Schroeder *****************************************************************************/ 21293f542974SPaul B Schroeder 213095da310eSAlan Cox static int mos7840_get_lsr_info(struct tty_struct *tty, 213197c4965dSAl Viro unsigned int __user *value) 21323f542974SPaul B Schroeder { 21333f542974SPaul B Schroeder int count; 21343f542974SPaul B Schroeder unsigned int result = 0; 21353f542974SPaul B Schroeder 213695da310eSAlan Cox count = mos7840_chars_in_buffer(tty); 2137*9c134a14SGreg Kroah-Hartman if (count == 0) 21383f542974SPaul B Schroeder result = TIOCSER_TEMT; 21393f542974SPaul B Schroeder 21403f542974SPaul B Schroeder if (copy_to_user(value, &result, sizeof(int))) 21413f542974SPaul B Schroeder return -EFAULT; 21423f542974SPaul B Schroeder return 0; 21433f542974SPaul B Schroeder } 21443f542974SPaul B Schroeder 21453f542974SPaul B Schroeder /***************************************************************************** 21463f542974SPaul B Schroeder * mos7840_get_serial_info 21473f542974SPaul B Schroeder * function to get information about serial port 21483f542974SPaul B Schroeder *****************************************************************************/ 21493f542974SPaul B Schroeder 21503f542974SPaul B Schroeder static int mos7840_get_serial_info(struct moschip_port *mos7840_port, 215197c4965dSAl Viro struct serial_struct __user *retinfo) 21523f542974SPaul B Schroeder { 21533f542974SPaul B Schroeder struct serial_struct tmp; 21543f542974SPaul B Schroeder 21553f542974SPaul B Schroeder if (mos7840_port == NULL) 21563f542974SPaul B Schroeder return -1; 21573f542974SPaul B Schroeder 21583f542974SPaul B Schroeder if (!retinfo) 21593f542974SPaul B Schroeder return -EFAULT; 21603f542974SPaul B Schroeder 21613f542974SPaul B Schroeder memset(&tmp, 0, sizeof(tmp)); 21623f542974SPaul B Schroeder 21633f542974SPaul B Schroeder tmp.type = PORT_16550A; 21643f542974SPaul B Schroeder tmp.line = mos7840_port->port->serial->minor; 21653f542974SPaul B Schroeder tmp.port = mos7840_port->port->number; 21663f542974SPaul B Schroeder tmp.irq = 0; 21673f542974SPaul B Schroeder tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ; 21683f542974SPaul B Schroeder tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE; 21693f542974SPaul B Schroeder tmp.baud_base = 9600; 21703f542974SPaul B Schroeder tmp.close_delay = 5 * HZ; 21713f542974SPaul B Schroeder tmp.closing_wait = 30 * HZ; 21723f542974SPaul B Schroeder 21733f542974SPaul B Schroeder if (copy_to_user(retinfo, &tmp, sizeof(*retinfo))) 21743f542974SPaul B Schroeder return -EFAULT; 21753f542974SPaul B Schroeder return 0; 21763f542974SPaul B Schroeder } 21773f542974SPaul B Schroeder 21780bca1b91SAlan Cox static int mos7840_get_icount(struct tty_struct *tty, 21790bca1b91SAlan Cox struct serial_icounter_struct *icount) 21800bca1b91SAlan Cox { 21810bca1b91SAlan Cox struct usb_serial_port *port = tty->driver_data; 21820bca1b91SAlan Cox struct moschip_port *mos7840_port; 21830bca1b91SAlan Cox struct async_icount cnow; 21840bca1b91SAlan Cox 21850bca1b91SAlan Cox mos7840_port = mos7840_get_port_private(port); 21860bca1b91SAlan Cox cnow = mos7840_port->icount; 21870bca1b91SAlan Cox 21880bca1b91SAlan Cox smp_rmb(); 21890bca1b91SAlan Cox icount->cts = cnow.cts; 21900bca1b91SAlan Cox icount->dsr = cnow.dsr; 21910bca1b91SAlan Cox icount->rng = cnow.rng; 21920bca1b91SAlan Cox icount->dcd = cnow.dcd; 21930bca1b91SAlan Cox icount->rx = cnow.rx; 21940bca1b91SAlan Cox icount->tx = cnow.tx; 21950bca1b91SAlan Cox icount->frame = cnow.frame; 21960bca1b91SAlan Cox icount->overrun = cnow.overrun; 21970bca1b91SAlan Cox icount->parity = cnow.parity; 21980bca1b91SAlan Cox icount->brk = cnow.brk; 21990bca1b91SAlan Cox icount->buf_overrun = cnow.buf_overrun; 22000bca1b91SAlan Cox 2201*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s TIOCGICOUNT RX=%d, TX=%d\n", __func__, 2202*9c134a14SGreg Kroah-Hartman icount->rx, icount->tx); 22030bca1b91SAlan Cox return 0; 22040bca1b91SAlan Cox } 22050bca1b91SAlan Cox 22063f542974SPaul B Schroeder /***************************************************************************** 22073f542974SPaul B Schroeder * SerialIoctl 22083f542974SPaul B Schroeder * this function handles any ioctl calls to the driver 22093f542974SPaul B Schroeder *****************************************************************************/ 22103f542974SPaul B Schroeder 221100a0d0d6SAlan Cox static int mos7840_ioctl(struct tty_struct *tty, 22123f542974SPaul B Schroeder unsigned int cmd, unsigned long arg) 22133f542974SPaul B Schroeder { 221495da310eSAlan Cox struct usb_serial_port *port = tty->driver_data; 221597c4965dSAl Viro void __user *argp = (void __user *)arg; 22163f542974SPaul B Schroeder struct moschip_port *mos7840_port; 22173f542974SPaul B Schroeder 22183f542974SPaul B Schroeder struct async_icount cnow; 22193f542974SPaul B Schroeder struct async_icount cprev; 22203f542974SPaul B Schroeder 2221*9c134a14SGreg Kroah-Hartman if (mos7840_port_paranoia_check(port, __func__)) 22223f542974SPaul B Schroeder return -1; 22233f542974SPaul B Schroeder 22243f542974SPaul B Schroeder mos7840_port = mos7840_get_port_private(port); 22253f542974SPaul B Schroeder 22263f542974SPaul B Schroeder if (mos7840_port == NULL) 22273f542974SPaul B Schroeder return -1; 22283f542974SPaul B Schroeder 2229*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - cmd = 0x%x\n", __func__, cmd); 22303f542974SPaul B Schroeder 22313f542974SPaul B Schroeder switch (cmd) { 22323f542974SPaul B Schroeder /* return number of bytes available */ 22333f542974SPaul B Schroeder 22343f542974SPaul B Schroeder case TIOCSERGETLSR: 2235*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s TIOCSERGETLSR\n", __func__); 223695da310eSAlan Cox return mos7840_get_lsr_info(tty, argp); 22373f542974SPaul B Schroeder 22383f542974SPaul B Schroeder case TIOCGSERIAL: 2239*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s TIOCGSERIAL\n", __func__); 224097c4965dSAl Viro return mos7840_get_serial_info(mos7840_port, argp); 22413f542974SPaul B Schroeder 22423f542974SPaul B Schroeder case TIOCSSERIAL: 2243*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s TIOCSSERIAL\n", __func__); 22443f542974SPaul B Schroeder break; 22453f542974SPaul B Schroeder 22463f542974SPaul B Schroeder case TIOCMIWAIT: 2247*9c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s TIOCMIWAIT\n", __func__); 22483f542974SPaul B Schroeder cprev = mos7840_port->icount; 22493f542974SPaul B Schroeder while (1) { 2250880af9dbSAlan Cox /* interruptible_sleep_on(&mos7840_port->delta_msr_wait); */ 22513f542974SPaul B Schroeder mos7840_port->delta_msr_cond = 0; 22523f542974SPaul B Schroeder wait_event_interruptible(mos7840_port->delta_msr_wait, 22533f542974SPaul B Schroeder (mos7840_port-> 22543f542974SPaul B Schroeder delta_msr_cond == 1)); 22553f542974SPaul B Schroeder 22563f542974SPaul B Schroeder /* see if a signal did it */ 22573f542974SPaul B Schroeder if (signal_pending(current)) 22583f542974SPaul B Schroeder return -ERESTARTSYS; 22593f542974SPaul B Schroeder cnow = mos7840_port->icount; 22600de9a702SOliver Neukum smp_rmb(); 22613f542974SPaul B Schroeder if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr && 22623f542974SPaul B Schroeder cnow.dcd == cprev.dcd && cnow.cts == cprev.cts) 22633f542974SPaul B Schroeder return -EIO; /* no change => error */ 22643f542974SPaul B Schroeder if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) || 22653f542974SPaul B Schroeder ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) || 22663f542974SPaul B Schroeder ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) || 22673f542974SPaul B Schroeder ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) { 22683f542974SPaul B Schroeder return 0; 22693f542974SPaul B Schroeder } 22703f542974SPaul B Schroeder cprev = cnow; 22713f542974SPaul B Schroeder } 22723f542974SPaul B Schroeder /* NOTREACHED */ 22733f542974SPaul B Schroeder break; 22743f542974SPaul B Schroeder 22753f542974SPaul B Schroeder default: 22763f542974SPaul B Schroeder break; 22773f542974SPaul B Schroeder } 22783f542974SPaul B Schroeder return -ENOIOCTLCMD; 22793f542974SPaul B Schroeder } 22803f542974SPaul B Schroeder 22810eafe4deSDonald static int mos7810_check(struct usb_serial *serial) 22823f542974SPaul B Schroeder { 22830eafe4deSDonald int i, pass_count = 0; 22840eafe4deSDonald __u16 data = 0, mcr_data = 0; 22850eafe4deSDonald __u16 test_pattern = 0x55AA; 22863f542974SPaul B Schroeder 22870eafe4deSDonald /* Store MCR setting */ 22880eafe4deSDonald usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0), 22890eafe4deSDonald MCS_RDREQ, MCS_RD_RTYPE, 0x0300, MODEM_CONTROL_REGISTER, 22900eafe4deSDonald &mcr_data, VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT); 22910eafe4deSDonald 22920eafe4deSDonald for (i = 0; i < 16; i++) { 22930eafe4deSDonald /* Send the 1-bit test pattern out to MCS7810 test pin */ 22940eafe4deSDonald usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), 22950eafe4deSDonald MCS_WRREQ, MCS_WR_RTYPE, 22960eafe4deSDonald (0x0300 | (((test_pattern >> i) & 0x0001) << 1)), 22970eafe4deSDonald MODEM_CONTROL_REGISTER, NULL, 0, MOS_WDR_TIMEOUT); 22980eafe4deSDonald 22990eafe4deSDonald /* Read the test pattern back */ 23000eafe4deSDonald usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0), 23010eafe4deSDonald MCS_RDREQ, MCS_RD_RTYPE, 0, GPIO_REGISTER, &data, 2302093ea2d3SDonald Lee VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT); 2303093ea2d3SDonald Lee 23040eafe4deSDonald /* If this is a MCS7810 device, both test patterns must match */ 23050eafe4deSDonald if (((test_pattern >> i) ^ (~data >> 1)) & 0x0001) 23060eafe4deSDonald break; 23070eafe4deSDonald 23080eafe4deSDonald pass_count++; 23093f542974SPaul B Schroeder } 2310093ea2d3SDonald Lee 23110eafe4deSDonald /* Restore MCR setting */ 23120eafe4deSDonald usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), MCS_WRREQ, 23130eafe4deSDonald MCS_WR_RTYPE, 0x0300 | mcr_data, MODEM_CONTROL_REGISTER, NULL, 23140eafe4deSDonald 0, MOS_WDR_TIMEOUT); 23150eafe4deSDonald 23160eafe4deSDonald if (pass_count == 16) 23170eafe4deSDonald return 1; 23180eafe4deSDonald 23190eafe4deSDonald return 0; 23200eafe4deSDonald } 23210eafe4deSDonald 23220eafe4deSDonald static int mos7840_calc_num_ports(struct usb_serial *serial) 23230eafe4deSDonald { 23240eafe4deSDonald __u16 data = 0x00; 23250eafe4deSDonald int mos7840_num_ports; 23260eafe4deSDonald 23270eafe4deSDonald usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0), 23280eafe4deSDonald MCS_RDREQ, MCS_RD_RTYPE, 0, GPIO_REGISTER, &data, 23290eafe4deSDonald VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT); 23300eafe4deSDonald 23310eafe4deSDonald if (serial->dev->descriptor.idProduct == MOSCHIP_DEVICE_ID_7810 || 23320eafe4deSDonald serial->dev->descriptor.idProduct == MOSCHIP_DEVICE_ID_7820) { 23330eafe4deSDonald device_type = serial->dev->descriptor.idProduct; 23340eafe4deSDonald } else { 23350eafe4deSDonald /* For a MCS7840 device GPIO0 must be set to 1 */ 23360eafe4deSDonald if ((data & 0x01) == 1) 23370eafe4deSDonald device_type = MOSCHIP_DEVICE_ID_7840; 23380eafe4deSDonald else if (mos7810_check(serial)) 23390eafe4deSDonald device_type = MOSCHIP_DEVICE_ID_7810; 23400eafe4deSDonald else 23410eafe4deSDonald device_type = MOSCHIP_DEVICE_ID_7820; 23420eafe4deSDonald } 23430eafe4deSDonald 23440eafe4deSDonald mos7840_num_ports = (device_type >> 4) & 0x000F; 23450eafe4deSDonald serial->num_bulk_in = mos7840_num_ports; 23460eafe4deSDonald serial->num_bulk_out = mos7840_num_ports; 23470eafe4deSDonald serial->num_ports = mos7840_num_ports; 23480eafe4deSDonald 23493f542974SPaul B Schroeder return mos7840_num_ports; 23503f542974SPaul B Schroeder } 23513f542974SPaul B Schroeder 23523f542974SPaul B Schroeder /**************************************************************************** 23533f542974SPaul B Schroeder * mos7840_startup 23543f542974SPaul B Schroeder ****************************************************************************/ 23553f542974SPaul B Schroeder 23563f542974SPaul B Schroeder static int mos7840_startup(struct usb_serial *serial) 23573f542974SPaul B Schroeder { 23583f542974SPaul B Schroeder struct moschip_port *mos7840_port; 23593f542974SPaul B Schroeder struct usb_device *dev; 23603f542974SPaul B Schroeder int i, status; 23613f542974SPaul B Schroeder __u16 Data; 23623f542974SPaul B Schroeder 23633f542974SPaul B Schroeder dev = serial->dev; 23643f542974SPaul B Schroeder 23653f542974SPaul B Schroeder /* we set up the pointers to the endpoints in the mos7840_open * 23663f542974SPaul B Schroeder * function, as the structures aren't created yet. */ 23673f542974SPaul B Schroeder 23683f542974SPaul B Schroeder /* set up port private structures */ 23693f542974SPaul B Schroeder for (i = 0; i < serial->num_ports; ++i) { 2370*9c134a14SGreg Kroah-Hartman dev_dbg(&dev->dev, "mos7840_startup: configuring port %d............\n", i); 23717ac9da10SBurman Yan mos7840_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL); 23723f542974SPaul B Schroeder if (mos7840_port == NULL) { 2373194343d9SGreg Kroah-Hartman dev_err(&dev->dev, "%s - Out of memory\n", __func__); 23740de9a702SOliver Neukum status = -ENOMEM; 23750de9a702SOliver Neukum i--; /* don't follow NULL pointer cleaning up */ 23760de9a702SOliver Neukum goto error; 23773f542974SPaul B Schroeder } 23783f542974SPaul B Schroeder 2379880af9dbSAlan Cox /* Initialize all port interrupt end point to port 0 int 2380880af9dbSAlan Cox * endpoint. Our device has only one interrupt end point 2381880af9dbSAlan Cox * common to all port */ 23823f542974SPaul B Schroeder 23833f542974SPaul B Schroeder mos7840_port->port = serial->port[i]; 23843f542974SPaul B Schroeder mos7840_set_port_private(serial->port[i], mos7840_port); 23850de9a702SOliver Neukum spin_lock_init(&mos7840_port->pool_lock); 23863f542974SPaul B Schroeder 238737768adfSTony Cook /* minor is not initialised until later by 238837768adfSTony Cook * usb-serial.c:get_free_serial() and cannot therefore be used 238937768adfSTony Cook * to index device instances */ 239037768adfSTony Cook mos7840_port->port_num = i + 1; 2391*9c134a14SGreg Kroah-Hartman dev_dbg(&dev->dev, "serial->port[i]->number = %d\n", serial->port[i]->number); 2392*9c134a14SGreg Kroah-Hartman dev_dbg(&dev->dev, "serial->port[i]->serial->minor = %d\n", serial->port[i]->serial->minor); 2393*9c134a14SGreg Kroah-Hartman dev_dbg(&dev->dev, "mos7840_port->port_num = %d\n", mos7840_port->port_num); 2394*9c134a14SGreg Kroah-Hartman dev_dbg(&dev->dev, "serial->minor = %d\n", serial->minor); 23953f542974SPaul B Schroeder 23963f542974SPaul B Schroeder if (mos7840_port->port_num == 1) { 23973f542974SPaul B Schroeder mos7840_port->SpRegOffset = 0x0; 23983f542974SPaul B Schroeder mos7840_port->ControlRegOffset = 0x1; 23993f542974SPaul B Schroeder mos7840_port->DcrRegOffset = 0x4; 24003f542974SPaul B Schroeder } else if ((mos7840_port->port_num == 2) 24010de9a702SOliver Neukum && (serial->num_ports == 4)) { 24023f542974SPaul B Schroeder mos7840_port->SpRegOffset = 0x8; 24033f542974SPaul B Schroeder mos7840_port->ControlRegOffset = 0x9; 24043f542974SPaul B Schroeder mos7840_port->DcrRegOffset = 0x16; 24053f542974SPaul B Schroeder } else if ((mos7840_port->port_num == 2) 24060de9a702SOliver Neukum && (serial->num_ports == 2)) { 24073f542974SPaul B Schroeder mos7840_port->SpRegOffset = 0xa; 24083f542974SPaul B Schroeder mos7840_port->ControlRegOffset = 0xb; 24093f542974SPaul B Schroeder mos7840_port->DcrRegOffset = 0x19; 24103f542974SPaul B Schroeder } else if ((mos7840_port->port_num == 3) 24110de9a702SOliver Neukum && (serial->num_ports == 4)) { 24123f542974SPaul B Schroeder mos7840_port->SpRegOffset = 0xa; 24133f542974SPaul B Schroeder mos7840_port->ControlRegOffset = 0xb; 24143f542974SPaul B Schroeder mos7840_port->DcrRegOffset = 0x19; 24153f542974SPaul B Schroeder } else if ((mos7840_port->port_num == 4) 24160de9a702SOliver Neukum && (serial->num_ports == 4)) { 24173f542974SPaul B Schroeder mos7840_port->SpRegOffset = 0xc; 24183f542974SPaul B Schroeder mos7840_port->ControlRegOffset = 0xd; 24193f542974SPaul B Schroeder mos7840_port->DcrRegOffset = 0x1c; 24203f542974SPaul B Schroeder } 2421*9c134a14SGreg Kroah-Hartman mos7840_dump_serial_port(serial->port[i], mos7840_port); 24223f542974SPaul B Schroeder mos7840_set_port_private(serial->port[i], mos7840_port); 24233f542974SPaul B Schroeder 2424880af9dbSAlan Cox /* enable rx_disable bit in control register */ 2425880af9dbSAlan Cox status = mos7840_get_reg_sync(serial->port[i], 24263f542974SPaul B Schroeder mos7840_port->ControlRegOffset, &Data); 24273f542974SPaul B Schroeder if (status < 0) { 2428*9c134a14SGreg Kroah-Hartman dev_dbg(&dev->dev, "Reading ControlReg failed status-0x%x\n", status); 24293f542974SPaul B Schroeder break; 24303f542974SPaul B Schroeder } else 2431*9c134a14SGreg Kroah-Hartman dev_dbg(&dev->dev, "ControlReg Reading success val is %x, status%d\n", Data, status); 2432880af9dbSAlan Cox Data |= 0x08; /* setting driver done bit */ 2433880af9dbSAlan Cox Data |= 0x04; /* sp1_bit to have cts change reflect in 2434880af9dbSAlan Cox modem status reg */ 24353f542974SPaul B Schroeder 2436880af9dbSAlan Cox /* Data |= 0x20; //rx_disable bit */ 2437880af9dbSAlan Cox status = mos7840_set_reg_sync(serial->port[i], 24383f542974SPaul B Schroeder mos7840_port->ControlRegOffset, Data); 24393f542974SPaul B Schroeder if (status < 0) { 2440*9c134a14SGreg Kroah-Hartman dev_dbg(&dev->dev, "Writing ControlReg failed(rx_disable) status-0x%x\n", status); 24413f542974SPaul B Schroeder break; 24423f542974SPaul B Schroeder } else 2443*9c134a14SGreg Kroah-Hartman dev_dbg(&dev->dev, "ControlReg Writing success(rx_disable) status%d\n", status); 24443f542974SPaul B Schroeder 2445880af9dbSAlan Cox /* Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2 2446880af9dbSAlan Cox and 0x24 in DCR3 */ 24473f542974SPaul B Schroeder Data = 0x01; 2448880af9dbSAlan Cox status = mos7840_set_reg_sync(serial->port[i], 2449880af9dbSAlan Cox (__u16) (mos7840_port->DcrRegOffset + 0), Data); 24503f542974SPaul B Schroeder if (status < 0) { 2451*9c134a14SGreg Kroah-Hartman dev_dbg(&dev->dev, "Writing DCR0 failed status-0x%x\n", status); 24523f542974SPaul B Schroeder break; 24533f542974SPaul B Schroeder } else 2454*9c134a14SGreg Kroah-Hartman dev_dbg(&dev->dev, "DCR0 Writing success status%d\n", status); 24553f542974SPaul B Schroeder 24563f542974SPaul B Schroeder Data = 0x05; 2457880af9dbSAlan Cox status = mos7840_set_reg_sync(serial->port[i], 2458880af9dbSAlan Cox (__u16) (mos7840_port->DcrRegOffset + 1), Data); 24593f542974SPaul B Schroeder if (status < 0) { 2460*9c134a14SGreg Kroah-Hartman dev_dbg(&dev->dev, "Writing DCR1 failed status-0x%x\n", status); 24613f542974SPaul B Schroeder break; 24623f542974SPaul B Schroeder } else 2463*9c134a14SGreg Kroah-Hartman dev_dbg(&dev->dev, "DCR1 Writing success status%d\n", status); 24643f542974SPaul B Schroeder 24653f542974SPaul B Schroeder Data = 0x24; 2466880af9dbSAlan Cox status = mos7840_set_reg_sync(serial->port[i], 2467880af9dbSAlan Cox (__u16) (mos7840_port->DcrRegOffset + 2), Data); 24683f542974SPaul B Schroeder if (status < 0) { 2469*9c134a14SGreg Kroah-Hartman dev_dbg(&dev->dev, "Writing DCR2 failed status-0x%x\n", status); 24703f542974SPaul B Schroeder break; 24713f542974SPaul B Schroeder } else 2472*9c134a14SGreg Kroah-Hartman dev_dbg(&dev->dev, "DCR2 Writing success status%d\n", status); 24733f542974SPaul B Schroeder 2474880af9dbSAlan Cox /* write values in clkstart0x0 and clkmulti 0x20 */ 24753f542974SPaul B Schroeder Data = 0x0; 2476880af9dbSAlan Cox status = mos7840_set_reg_sync(serial->port[i], 24773f542974SPaul B Schroeder CLK_START_VALUE_REGISTER, Data); 24783f542974SPaul B Schroeder if (status < 0) { 2479*9c134a14SGreg Kroah-Hartman dev_dbg(&dev->dev, "Writing CLK_START_VALUE_REGISTER failed status-0x%x\n", status); 24803f542974SPaul B Schroeder break; 24813f542974SPaul B Schroeder } else 2482*9c134a14SGreg Kroah-Hartman dev_dbg(&dev->dev, "CLK_START_VALUE_REGISTER Writing success status%d\n", status); 24833f542974SPaul B Schroeder 24843f542974SPaul B Schroeder Data = 0x20; 2485880af9dbSAlan Cox status = mos7840_set_reg_sync(serial->port[i], 2486880af9dbSAlan Cox CLK_MULTI_REGISTER, Data); 24873f542974SPaul B Schroeder if (status < 0) { 2488*9c134a14SGreg Kroah-Hartman dev_dbg(&dev->dev, "Writing CLK_MULTI_REGISTER failed status-0x%x\n", status); 24890de9a702SOliver Neukum goto error; 24903f542974SPaul B Schroeder } else 2491*9c134a14SGreg Kroah-Hartman dev_dbg(&dev->dev, "CLK_MULTI_REGISTER Writing success status%d\n", status); 24923f542974SPaul B Schroeder 2493880af9dbSAlan Cox /* write value 0x0 to scratchpad register */ 24943f542974SPaul B Schroeder Data = 0x00; 2495880af9dbSAlan Cox status = mos7840_set_uart_reg(serial->port[i], 2496880af9dbSAlan Cox SCRATCH_PAD_REGISTER, Data); 24973f542974SPaul B Schroeder if (status < 0) { 2498*9c134a14SGreg Kroah-Hartman dev_dbg(&dev->dev, "Writing SCRATCH_PAD_REGISTER failed status-0x%x\n", status); 24993f542974SPaul B Schroeder break; 25003f542974SPaul B Schroeder } else 2501*9c134a14SGreg Kroah-Hartman dev_dbg(&dev->dev, "SCRATCH_PAD_REGISTER Writing success status%d\n", status); 25023f542974SPaul B Schroeder 2503880af9dbSAlan Cox /* Zero Length flag register */ 25043f542974SPaul B Schroeder if ((mos7840_port->port_num != 1) 25050de9a702SOliver Neukum && (serial->num_ports == 2)) { 25063f542974SPaul B Schroeder 25073f542974SPaul B Schroeder Data = 0xff; 25083f542974SPaul B Schroeder status = mos7840_set_reg_sync(serial->port[i], 25093f542974SPaul B Schroeder (__u16) (ZLP_REG1 + 2510880af9dbSAlan Cox ((__u16)mos7840_port->port_num)), Data); 2511*9c134a14SGreg Kroah-Hartman dev_dbg(&dev->dev, "ZLIP offset %x\n", 2512*9c134a14SGreg Kroah-Hartman (__u16)(ZLP_REG1 + ((__u16) mos7840_port->port_num))); 25133f542974SPaul B Schroeder if (status < 0) { 2514*9c134a14SGreg Kroah-Hartman dev_dbg(&dev->dev, "Writing ZLP_REG%d failed status-0x%x\n", i + 2, status); 25153f542974SPaul B Schroeder break; 25163f542974SPaul B Schroeder } else 2517*9c134a14SGreg Kroah-Hartman dev_dbg(&dev->dev, "ZLP_REG%d Writing success status%d\n", i + 2, status); 25183f542974SPaul B Schroeder } else { 25193f542974SPaul B Schroeder Data = 0xff; 25203f542974SPaul B Schroeder status = mos7840_set_reg_sync(serial->port[i], 25213f542974SPaul B Schroeder (__u16) (ZLP_REG1 + 2522880af9dbSAlan Cox ((__u16)mos7840_port->port_num) - 0x1), Data); 2523*9c134a14SGreg Kroah-Hartman dev_dbg(&dev->dev, "ZLIP offset %x\n", 2524*9c134a14SGreg Kroah-Hartman (__u16)(ZLP_REG1 + ((__u16) mos7840_port->port_num) - 0x1)); 25253f542974SPaul B Schroeder if (status < 0) { 2526*9c134a14SGreg Kroah-Hartman dev_dbg(&dev->dev, "Writing ZLP_REG%d failed status-0x%x\n", i + 1, status); 25273f542974SPaul B Schroeder break; 25283f542974SPaul B Schroeder } else 2529*9c134a14SGreg Kroah-Hartman dev_dbg(&dev->dev, "ZLP_REG%d Writing success status%d\n", i + 1, status); 25303f542974SPaul B Schroeder 25313f542974SPaul B Schroeder } 25320de9a702SOliver Neukum mos7840_port->control_urb = usb_alloc_urb(0, GFP_KERNEL); 25333f542974SPaul B Schroeder mos7840_port->ctrl_buf = kmalloc(16, GFP_KERNEL); 2534880af9dbSAlan Cox mos7840_port->dr = kmalloc(sizeof(struct usb_ctrlrequest), 2535880af9dbSAlan Cox GFP_KERNEL); 2536880af9dbSAlan Cox if (!mos7840_port->control_urb || !mos7840_port->ctrl_buf || 2537880af9dbSAlan Cox !mos7840_port->dr) { 25380de9a702SOliver Neukum status = -ENOMEM; 25390de9a702SOliver Neukum goto error; 25400de9a702SOliver Neukum } 25410eafe4deSDonald 25420eafe4deSDonald mos7840_port->has_led = false; 25430eafe4deSDonald 25440eafe4deSDonald /* Initialize LED timers */ 25450eafe4deSDonald if (device_type == MOSCHIP_DEVICE_ID_7810) { 25460eafe4deSDonald mos7840_port->has_led = true; 25470eafe4deSDonald 25480eafe4deSDonald init_timer(&mos7840_port->led_timer1); 25490eafe4deSDonald mos7840_port->led_timer1.function = mos7840_led_off; 25500eafe4deSDonald mos7840_port->led_timer1.expires = 25510eafe4deSDonald jiffies + msecs_to_jiffies(LED_ON_MS); 25520eafe4deSDonald mos7840_port->led_timer1.data = 25530eafe4deSDonald (unsigned long)mos7840_port; 25540eafe4deSDonald 25550eafe4deSDonald init_timer(&mos7840_port->led_timer2); 25560eafe4deSDonald mos7840_port->led_timer2.function = 25570eafe4deSDonald mos7840_led_flag_off; 25580eafe4deSDonald mos7840_port->led_timer2.expires = 25590eafe4deSDonald jiffies + msecs_to_jiffies(LED_OFF_MS); 25600eafe4deSDonald mos7840_port->led_timer2.data = 25610eafe4deSDonald (unsigned long)mos7840_port; 25620eafe4deSDonald 25630eafe4deSDonald mos7840_port->led_flag = false; 25640eafe4deSDonald 25650eafe4deSDonald /* Turn off LED */ 25660eafe4deSDonald mos7840_set_led_sync(serial->port[i], 25670eafe4deSDonald MODEM_CONTROL_REGISTER, 0x0300); 25680eafe4deSDonald } 25693f542974SPaul B Schroeder } 25703f542974SPaul B Schroeder 2571880af9dbSAlan Cox /* Zero Length flag enable */ 25723f542974SPaul B Schroeder Data = 0x0f; 25733f542974SPaul B Schroeder status = mos7840_set_reg_sync(serial->port[0], ZLP_REG5, Data); 25743f542974SPaul B Schroeder if (status < 0) { 2575*9c134a14SGreg Kroah-Hartman dev_dbg(&dev->dev, "Writing ZLP_REG5 failed status-0x%x\n", status); 25767ced46c3SRoel Kluin goto error; 25773f542974SPaul B Schroeder } else 2578*9c134a14SGreg Kroah-Hartman dev_dbg(&dev->dev, "ZLP_REG5 Writing success status%d\n", status); 25793f542974SPaul B Schroeder 25803f542974SPaul B Schroeder /* setting configuration feature to one */ 25813f542974SPaul B Schroeder usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), 25821e658489SMark Ferrell (__u8) 0x03, 0x00, 0x01, 0x00, NULL, 0x00, MOS_WDR_TIMEOUT); 25833f542974SPaul B Schroeder return 0; 25840de9a702SOliver Neukum error: 25850de9a702SOliver Neukum for (/* nothing */; i >= 0; i--) { 25860de9a702SOliver Neukum mos7840_port = mos7840_get_port_private(serial->port[i]); 25870de9a702SOliver Neukum 25880de9a702SOliver Neukum kfree(mos7840_port->dr); 25890de9a702SOliver Neukum kfree(mos7840_port->ctrl_buf); 25900de9a702SOliver Neukum usb_free_urb(mos7840_port->control_urb); 25910de9a702SOliver Neukum kfree(mos7840_port); 25920de9a702SOliver Neukum serial->port[i] = NULL; 25930de9a702SOliver Neukum } 25940de9a702SOliver Neukum return status; 25953f542974SPaul B Schroeder } 25963f542974SPaul B Schroeder 25973f542974SPaul B Schroeder /**************************************************************************** 2598f9c99bb8SAlan Stern * mos7840_disconnect 25993f542974SPaul B Schroeder * This function is called whenever the device is removed from the usb bus. 26003f542974SPaul B Schroeder ****************************************************************************/ 26013f542974SPaul B Schroeder 2602f9c99bb8SAlan Stern static void mos7840_disconnect(struct usb_serial *serial) 26033f542974SPaul B Schroeder { 26043f542974SPaul B Schroeder int i; 26050de9a702SOliver Neukum unsigned long flags; 26063f542974SPaul B Schroeder struct moschip_port *mos7840_port; 26073f542974SPaul B Schroeder 26083f542974SPaul B Schroeder /* check for the ports to be closed,close the ports and disconnect */ 26093f542974SPaul B Schroeder 26103f542974SPaul B Schroeder /* free private structure allocated for serial port * 26113f542974SPaul B Schroeder * stop reads and writes on all ports */ 26123f542974SPaul B Schroeder 26133f542974SPaul B Schroeder for (i = 0; i < serial->num_ports; ++i) { 26143f542974SPaul B Schroeder mos7840_port = mos7840_get_port_private(serial->port[i]); 261537768adfSTony Cook if (mos7840_port) { 26160de9a702SOliver Neukum spin_lock_irqsave(&mos7840_port->pool_lock, flags); 26170de9a702SOliver Neukum mos7840_port->zombie = 1; 26180de9a702SOliver Neukum spin_unlock_irqrestore(&mos7840_port->pool_lock, flags); 26193f542974SPaul B Schroeder usb_kill_urb(mos7840_port->control_urb); 2620f9c99bb8SAlan Stern } 2621f9c99bb8SAlan Stern } 2622f9c99bb8SAlan Stern } 2623f9c99bb8SAlan Stern 2624f9c99bb8SAlan Stern /**************************************************************************** 2625f9c99bb8SAlan Stern * mos7840_release 2626f9c99bb8SAlan Stern * This function is called when the usb_serial structure is freed. 2627f9c99bb8SAlan Stern ****************************************************************************/ 2628f9c99bb8SAlan Stern 2629f9c99bb8SAlan Stern static void mos7840_release(struct usb_serial *serial) 2630f9c99bb8SAlan Stern { 2631f9c99bb8SAlan Stern int i; 2632f9c99bb8SAlan Stern struct moschip_port *mos7840_port; 2633f9c99bb8SAlan Stern 2634f9c99bb8SAlan Stern /* check for the ports to be closed,close the ports and disconnect */ 2635f9c99bb8SAlan Stern 2636f9c99bb8SAlan Stern /* free private structure allocated for serial port * 2637f9c99bb8SAlan Stern * stop reads and writes on all ports */ 2638f9c99bb8SAlan Stern 2639f9c99bb8SAlan Stern for (i = 0; i < serial->num_ports; ++i) { 2640f9c99bb8SAlan Stern mos7840_port = mos7840_get_port_private(serial->port[i]); 2641f9c99bb8SAlan Stern if (mos7840_port) { 26420eafe4deSDonald if (mos7840_port->has_led) { 26430eafe4deSDonald /* Turn off LED */ 26440eafe4deSDonald mos7840_set_led_sync(mos7840_port->port, 26450eafe4deSDonald MODEM_CONTROL_REGISTER, 0x0300); 26460eafe4deSDonald 26470eafe4deSDonald del_timer_sync(&mos7840_port->led_timer1); 26480eafe4deSDonald del_timer_sync(&mos7840_port->led_timer2); 26490eafe4deSDonald } 26500de9a702SOliver Neukum kfree(mos7840_port->ctrl_buf); 26510de9a702SOliver Neukum kfree(mos7840_port->dr); 26523f542974SPaul B Schroeder kfree(mos7840_port); 265337768adfSTony Cook } 26543f542974SPaul B Schroeder } 26553f542974SPaul B Schroeder } 26563f542974SPaul B Schroeder 26573f542974SPaul B Schroeder static struct usb_serial_driver moschip7840_4port_device = { 26583f542974SPaul B Schroeder .driver = { 26593f542974SPaul B Schroeder .owner = THIS_MODULE, 26603f542974SPaul B Schroeder .name = "mos7840", 26613f542974SPaul B Schroeder }, 26623f542974SPaul B Schroeder .description = DRIVER_DESC, 266368e24113SGreg Kroah-Hartman .id_table = id_table, 26643f542974SPaul B Schroeder .num_ports = 4, 26653f542974SPaul B Schroeder .open = mos7840_open, 26663f542974SPaul B Schroeder .close = mos7840_close, 26673f542974SPaul B Schroeder .write = mos7840_write, 26683f542974SPaul B Schroeder .write_room = mos7840_write_room, 26693f542974SPaul B Schroeder .chars_in_buffer = mos7840_chars_in_buffer, 26703f542974SPaul B Schroeder .throttle = mos7840_throttle, 26713f542974SPaul B Schroeder .unthrottle = mos7840_unthrottle, 26723f542974SPaul B Schroeder .calc_num_ports = mos7840_calc_num_ports, 26733f542974SPaul B Schroeder #ifdef MCSSerialProbe 26743f542974SPaul B Schroeder .probe = mos7840_serial_probe, 26753f542974SPaul B Schroeder #endif 26763f542974SPaul B Schroeder .ioctl = mos7840_ioctl, 26773f542974SPaul B Schroeder .set_termios = mos7840_set_termios, 26783f542974SPaul B Schroeder .break_ctl = mos7840_break, 26793f542974SPaul B Schroeder .tiocmget = mos7840_tiocmget, 26803f542974SPaul B Schroeder .tiocmset = mos7840_tiocmset, 26810bca1b91SAlan Cox .get_icount = mos7840_get_icount, 26823f542974SPaul B Schroeder .attach = mos7840_startup, 2683f9c99bb8SAlan Stern .disconnect = mos7840_disconnect, 2684f9c99bb8SAlan Stern .release = mos7840_release, 26853f542974SPaul B Schroeder .read_bulk_callback = mos7840_bulk_in_callback, 26863f542974SPaul B Schroeder .read_int_callback = mos7840_interrupt_callback, 26873f542974SPaul B Schroeder }; 26883f542974SPaul B Schroeder 26894d2a7affSAlan Stern static struct usb_serial_driver * const serial_drivers[] = { 26904d2a7affSAlan Stern &moschip7840_4port_device, NULL 26914d2a7affSAlan Stern }; 26924d2a7affSAlan Stern 269368e24113SGreg Kroah-Hartman module_usb_serial_driver(serial_drivers, id_table); 26943f542974SPaul B Schroeder 26953f542974SPaul B Schroeder MODULE_DESCRIPTION(DRIVER_DESC); 26963f542974SPaul B Schroeder MODULE_LICENSE("GPL"); 26973f542974SPaul B Schroeder 26983f542974SPaul B Schroeder module_param(debug, bool, S_IRUGO | S_IWUSR); 26993f542974SPaul B Schroeder MODULE_PARM_DESC(debug, "Debug enabled or not"); 2700