15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0+ 23f542974SPaul B Schroeder /* 33f542974SPaul B Schroeder * Clean ups from Moschip version and a few ioctl implementations by: 43f542974SPaul B Schroeder * Paul B Schroeder <pschroeder "at" uplogix "dot" com> 53f542974SPaul B Schroeder * 63f542974SPaul B Schroeder * Originally based on drivers/usb/serial/io_edgeport.c which is: 73f542974SPaul B Schroeder * Copyright (C) 2000 Inside Out Networks, All rights reserved. 83f542974SPaul B Schroeder * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com> 93f542974SPaul B Schroeder * 103f542974SPaul B Schroeder */ 113f542974SPaul B Schroeder 123f542974SPaul B Schroeder #include <linux/kernel.h> 133f542974SPaul B Schroeder #include <linux/errno.h> 143f542974SPaul B Schroeder #include <linux/slab.h> 153f542974SPaul B Schroeder #include <linux/tty.h> 163f542974SPaul B Schroeder #include <linux/tty_driver.h> 173f542974SPaul B Schroeder #include <linux/tty_flip.h> 183f542974SPaul B Schroeder #include <linux/module.h> 193f542974SPaul B Schroeder #include <linux/serial.h> 203f542974SPaul B Schroeder #include <linux/usb.h> 213f542974SPaul B Schroeder #include <linux/usb/serial.h> 22880af9dbSAlan Cox #include <linux/uaccess.h> 233f542974SPaul B Schroeder 243f542974SPaul B Schroeder #define DRIVER_DESC "Moschip 7840/7820 USB Serial Driver" 253f542974SPaul B Schroeder 263f542974SPaul B Schroeder /* 273f542974SPaul B Schroeder * 16C50 UART register defines 283f542974SPaul B Schroeder */ 293f542974SPaul B Schroeder 303f542974SPaul B Schroeder #define LCR_BITS_5 0x00 /* 5 bits/char */ 313f542974SPaul B Schroeder #define LCR_BITS_6 0x01 /* 6 bits/char */ 323f542974SPaul B Schroeder #define LCR_BITS_7 0x02 /* 7 bits/char */ 333f542974SPaul B Schroeder #define LCR_BITS_8 0x03 /* 8 bits/char */ 343f542974SPaul B Schroeder #define LCR_BITS_MASK 0x03 /* Mask for bits/char field */ 353f542974SPaul B Schroeder 363f542974SPaul B Schroeder #define LCR_STOP_1 0x00 /* 1 stop bit */ 373f542974SPaul B Schroeder #define LCR_STOP_1_5 0x04 /* 1.5 stop bits (if 5 bits/char) */ 383f542974SPaul B Schroeder #define LCR_STOP_2 0x04 /* 2 stop bits (if 6-8 bits/char) */ 393f542974SPaul B Schroeder #define LCR_STOP_MASK 0x04 /* Mask for stop bits field */ 403f542974SPaul B Schroeder 413f542974SPaul B Schroeder #define LCR_PAR_NONE 0x00 /* No parity */ 423f542974SPaul B Schroeder #define LCR_PAR_ODD 0x08 /* Odd parity */ 433f542974SPaul B Schroeder #define LCR_PAR_EVEN 0x18 /* Even parity */ 443f542974SPaul B Schroeder #define LCR_PAR_MARK 0x28 /* Force parity bit to 1 */ 453f542974SPaul B Schroeder #define LCR_PAR_SPACE 0x38 /* Force parity bit to 0 */ 463f542974SPaul B Schroeder #define LCR_PAR_MASK 0x38 /* Mask for parity field */ 473f542974SPaul B Schroeder 483f542974SPaul B Schroeder #define LCR_SET_BREAK 0x40 /* Set Break condition */ 493f542974SPaul B Schroeder #define LCR_DL_ENABLE 0x80 /* Enable access to divisor latch */ 503f542974SPaul B Schroeder 513f542974SPaul B Schroeder #define MCR_DTR 0x01 /* Assert DTR */ 523f542974SPaul B Schroeder #define MCR_RTS 0x02 /* Assert RTS */ 533f542974SPaul B Schroeder #define MCR_OUT1 0x04 /* Loopback only: Sets state of RI */ 543f542974SPaul B Schroeder #define MCR_MASTER_IE 0x08 /* Enable interrupt outputs */ 553f542974SPaul B Schroeder #define MCR_LOOPBACK 0x10 /* Set internal (digital) loopback mode */ 563f542974SPaul B Schroeder #define MCR_XON_ANY 0x20 /* Enable any char to exit XOFF mode */ 573f542974SPaul B Schroeder 583f542974SPaul B Schroeder #define MOS7840_MSR_CTS 0x10 /* Current state of CTS */ 593f542974SPaul B Schroeder #define MOS7840_MSR_DSR 0x20 /* Current state of DSR */ 603f542974SPaul B Schroeder #define MOS7840_MSR_RI 0x40 /* Current state of RI */ 613f542974SPaul B Schroeder #define MOS7840_MSR_CD 0x80 /* Current state of CD */ 623f542974SPaul B Schroeder 633f542974SPaul B Schroeder /* 643f542974SPaul B Schroeder * Defines used for sending commands to port 653f542974SPaul B Schroeder */ 663f542974SPaul B Schroeder 671e658489SMark Ferrell #define MOS_WDR_TIMEOUT 5000 /* default urb timeout */ 683f542974SPaul B Schroeder 693f542974SPaul B Schroeder #define MOS_PORT1 0x0200 703f542974SPaul B Schroeder #define MOS_PORT2 0x0300 713f542974SPaul B Schroeder #define MOS_VENREG 0x0000 723f542974SPaul B Schroeder #define MOS_MAX_PORT 0x02 733f542974SPaul B Schroeder #define MOS_WRITE 0x0E 743f542974SPaul B Schroeder #define MOS_READ 0x0D 753f542974SPaul B Schroeder 763f542974SPaul B Schroeder /* Requests */ 773f542974SPaul B Schroeder #define MCS_RD_RTYPE 0xC0 783f542974SPaul B Schroeder #define MCS_WR_RTYPE 0x40 793f542974SPaul B Schroeder #define MCS_RDREQ 0x0D 803f542974SPaul B Schroeder #define MCS_WRREQ 0x0E 813f542974SPaul B Schroeder #define MCS_CTRL_TIMEOUT 500 823f542974SPaul B Schroeder #define VENDOR_READ_LENGTH (0x01) 833f542974SPaul B Schroeder 843f542974SPaul B Schroeder #define MAX_NAME_LEN 64 853f542974SPaul B Schroeder 86880af9dbSAlan Cox #define ZLP_REG1 0x3A /* Zero_Flag_Reg1 58 */ 87880af9dbSAlan Cox #define ZLP_REG5 0x3E /* Zero_Flag_Reg5 62 */ 883f542974SPaul B Schroeder 893f542974SPaul B Schroeder /* For higher baud Rates use TIOCEXBAUD */ 903f542974SPaul B Schroeder #define TIOCEXBAUD 0x5462 913f542974SPaul B Schroeder 923f542974SPaul B Schroeder /* vendor id and device id defines */ 933f542974SPaul B Schroeder 9411e1abb4SDavid Ludlow /* The native mos7840/7820 component */ 953f542974SPaul B Schroeder #define USB_VENDOR_ID_MOSCHIP 0x9710 963f542974SPaul B Schroeder #define MOSCHIP_DEVICE_ID_7840 0x7840 973f542974SPaul B Schroeder #define MOSCHIP_DEVICE_ID_7820 0x7820 980eafe4deSDonald #define MOSCHIP_DEVICE_ID_7810 0x7810 9911e1abb4SDavid Ludlow /* The native component can have its vendor/device id's overridden 10011e1abb4SDavid Ludlow * in vendor-specific implementations. Such devices can be handled 10168e24113SGreg Kroah-Hartman * by making a change here, in id_table. 10211e1abb4SDavid Ludlow */ 10311e1abb4SDavid Ludlow #define USB_VENDOR_ID_BANDB 0x0856 104acf509aeSCliff Brake #define BANDB_DEVICE_ID_USO9ML2_2 0xAC22 105870408c8SDave Ludlow #define BANDB_DEVICE_ID_USO9ML2_2P 0xBC00 106acf509aeSCliff Brake #define BANDB_DEVICE_ID_USO9ML2_4 0xAC24 107870408c8SDave Ludlow #define BANDB_DEVICE_ID_USO9ML2_4P 0xBC01 108acf509aeSCliff Brake #define BANDB_DEVICE_ID_US9ML2_2 0xAC29 109acf509aeSCliff Brake #define BANDB_DEVICE_ID_US9ML2_4 0xAC30 110acf509aeSCliff Brake #define BANDB_DEVICE_ID_USPTL4_2 0xAC31 111acf509aeSCliff Brake #define BANDB_DEVICE_ID_USPTL4_4 0xAC32 11211e1abb4SDavid Ludlow #define BANDB_DEVICE_ID_USOPTL4_2 0xAC42 113caf3a636SDave Ludlow #define BANDB_DEVICE_ID_USOPTL4_2P 0xBC02 114870408c8SDave Ludlow #define BANDB_DEVICE_ID_USOPTL4_4 0xAC44 115870408c8SDave Ludlow #define BANDB_DEVICE_ID_USOPTL4_4P 0xBC03 116870408c8SDave Ludlow #define BANDB_DEVICE_ID_USOPTL2_4 0xAC24 1173f542974SPaul B Schroeder 1189d498beaSRussell Lang /* This driver also supports 1199d498beaSRussell Lang * ATEN UC2324 device using Moschip MCS7840 1209d498beaSRussell Lang * ATEN UC2322 device using Moschip MCS7820 1219d498beaSRussell Lang */ 122e9b8cffaSTony Cook #define USB_VENDOR_ID_ATENINTL 0x0557 123e9b8cffaSTony Cook #define ATENINTL_DEVICE_ID_UC2324 0x2011 1249d498beaSRussell Lang #define ATENINTL_DEVICE_ID_UC2322 0x7820 125e9b8cffaSTony Cook 12611e1abb4SDavid Ludlow /* Interrupt Routine Defines */ 1273f542974SPaul B Schroeder 1283f542974SPaul B Schroeder #define SERIAL_IIR_RLS 0x06 1293f542974SPaul B Schroeder #define SERIAL_IIR_MS 0x00 1303f542974SPaul B Schroeder 1313f542974SPaul B Schroeder /* 1323f542974SPaul B Schroeder * Emulation of the bit mask on the LINE STATUS REGISTER. 1333f542974SPaul B Schroeder */ 1343f542974SPaul B Schroeder #define SERIAL_LSR_DR 0x0001 1353f542974SPaul B Schroeder #define SERIAL_LSR_OE 0x0002 1363f542974SPaul B Schroeder #define SERIAL_LSR_PE 0x0004 1373f542974SPaul B Schroeder #define SERIAL_LSR_FE 0x0008 1383f542974SPaul B Schroeder #define SERIAL_LSR_BI 0x0010 1393f542974SPaul B Schroeder 1403f542974SPaul B Schroeder #define MOS_MSR_DELTA_CTS 0x10 1413f542974SPaul B Schroeder #define MOS_MSR_DELTA_DSR 0x20 1423f542974SPaul B Schroeder #define MOS_MSR_DELTA_RI 0x40 1433f542974SPaul B Schroeder #define MOS_MSR_DELTA_CD 0x80 1443f542974SPaul B Schroeder 145880af9dbSAlan Cox /* Serial Port register Address */ 1463f542974SPaul B Schroeder #define INTERRUPT_ENABLE_REGISTER ((__u16)(0x01)) 1473f542974SPaul B Schroeder #define FIFO_CONTROL_REGISTER ((__u16)(0x02)) 1483f542974SPaul B Schroeder #define LINE_CONTROL_REGISTER ((__u16)(0x03)) 1493f542974SPaul B Schroeder #define MODEM_CONTROL_REGISTER ((__u16)(0x04)) 1503f542974SPaul B Schroeder #define LINE_STATUS_REGISTER ((__u16)(0x05)) 1513f542974SPaul B Schroeder #define MODEM_STATUS_REGISTER ((__u16)(0x06)) 1523f542974SPaul B Schroeder #define SCRATCH_PAD_REGISTER ((__u16)(0x07)) 1533f542974SPaul B Schroeder #define DIVISOR_LATCH_LSB ((__u16)(0x00)) 1543f542974SPaul B Schroeder #define DIVISOR_LATCH_MSB ((__u16)(0x01)) 1553f542974SPaul B Schroeder 1563f542974SPaul B Schroeder #define CLK_MULTI_REGISTER ((__u16)(0x02)) 1573f542974SPaul B Schroeder #define CLK_START_VALUE_REGISTER ((__u16)(0x03)) 158093ea2d3SDonald Lee #define GPIO_REGISTER ((__u16)(0x07)) 1593f542974SPaul B Schroeder 1603f542974SPaul B Schroeder #define SERIAL_LCR_DLAB ((__u16)(0x0080)) 1613f542974SPaul B Schroeder 1623f542974SPaul B Schroeder /* 1633f542974SPaul B Schroeder * URB POOL related defines 1643f542974SPaul B Schroeder */ 1653f542974SPaul B Schroeder #define NUM_URBS 16 /* URB Count */ 1663f542974SPaul B Schroeder #define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */ 1673f542974SPaul B Schroeder 1680eafe4deSDonald /* LED on/off milliseconds*/ 1690eafe4deSDonald #define LED_ON_MS 500 1700eafe4deSDonald #define LED_OFF_MS 500 1710eafe4deSDonald 172d8a083ccSJohan Hovold enum mos7840_flag { 173d8a083ccSJohan Hovold MOS7840_FLAG_CTRL_BUSY, 17405cf0decSJohan Hovold MOS7840_FLAG_LED_BUSY, 175d8a083ccSJohan Hovold }; 1763f542974SPaul B Schroeder 177b9c87663STony Zelenoff static const struct usb_device_id id_table[] = { 1783f542974SPaul B Schroeder {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7840)}, 1793f542974SPaul B Schroeder {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7820)}, 1800eafe4deSDonald {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7810)}, 181acf509aeSCliff Brake {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2)}, 182870408c8SDave Ludlow {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2P)}, 183acf509aeSCliff Brake {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4)}, 184870408c8SDave Ludlow {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4P)}, 185acf509aeSCliff Brake {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_2)}, 186acf509aeSCliff Brake {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_4)}, 187acf509aeSCliff Brake {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_2)}, 188acf509aeSCliff Brake {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_4)}, 18911e1abb4SDavid Ludlow {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2)}, 190870408c8SDave Ludlow {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2P)}, 191acf509aeSCliff Brake {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4)}, 192870408c8SDave Ludlow {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4P)}, 19327f1281dSBlaise Gassend {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL2_4)}, 194e9b8cffaSTony Cook {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2324)}, 1959d498beaSRussell Lang {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2322)}, 1963f542974SPaul B Schroeder {} /* terminating entry */ 1973f542974SPaul B Schroeder }; 19868e24113SGreg Kroah-Hartman MODULE_DEVICE_TABLE(usb, id_table); 1993f542974SPaul B Schroeder 2003f542974SPaul B Schroeder /* This structure holds all of the local port information */ 2013f542974SPaul B Schroeder 2023f542974SPaul B Schroeder struct moschip_port { 2033f542974SPaul B Schroeder int port_num; /*Actual port number in the device(1,2,etc) */ 2043f542974SPaul B Schroeder struct urb *read_urb; /* read URB for this port */ 2053f542974SPaul B Schroeder __u8 shadowLCR; /* last LCR value received */ 2063f542974SPaul B Schroeder __u8 shadowMCR; /* last MCR value received */ 2073f542974SPaul B Schroeder char open; 2080de9a702SOliver Neukum char open_ports; 2093f542974SPaul B Schroeder struct usb_serial_port *port; /* loop back to the owner of this object */ 2103f542974SPaul B Schroeder 2113f542974SPaul B Schroeder /* Offsets */ 2123f542974SPaul B Schroeder __u8 SpRegOffset; 2133f542974SPaul B Schroeder __u8 ControlRegOffset; 2143f542974SPaul B Schroeder __u8 DcrRegOffset; 215880af9dbSAlan Cox /* for processing control URBS in interrupt context */ 2163f542974SPaul B Schroeder struct urb *control_urb; 2170de9a702SOliver Neukum struct usb_ctrlrequest *dr; 2183f542974SPaul B Schroeder char *ctrl_buf; 2193f542974SPaul B Schroeder int MsrLsr; 2203f542974SPaul B Schroeder 2210de9a702SOliver Neukum spinlock_t pool_lock; 2223f542974SPaul B Schroeder struct urb *write_urb_pool[NUM_URBS]; 2230de9a702SOliver Neukum char busy[NUM_URBS]; 22450de36f7SGreg Kroah-Hartman bool read_urb_busy; 2253f542974SPaul B Schroeder 2260eafe4deSDonald /* For device(s) with LED indicator */ 2270eafe4deSDonald bool has_led; 2280eafe4deSDonald struct timer_list led_timer1; /* Timer for LED on */ 2290eafe4deSDonald struct timer_list led_timer2; /* Timer for LED off */ 23005cf0decSJohan Hovold struct urb *led_urb; 23105cf0decSJohan Hovold struct usb_ctrlrequest *led_dr; 232d8a083ccSJohan Hovold 233d8a083ccSJohan Hovold unsigned long flags; 2340eafe4deSDonald }; 2353f542974SPaul B Schroeder 2363f542974SPaul B Schroeder /* 2373f542974SPaul B Schroeder * mos7840_set_reg_sync 2383f542974SPaul B Schroeder * To set the Control register by calling usb_fill_control_urb function 2393f542974SPaul B Schroeder * by passing usb_sndctrlpipe function as parameter. 2403f542974SPaul B Schroeder */ 2413f542974SPaul B Schroeder 2423f542974SPaul B Schroeder static int mos7840_set_reg_sync(struct usb_serial_port *port, __u16 reg, 2433f542974SPaul B Schroeder __u16 val) 2443f542974SPaul B Schroeder { 2453f542974SPaul B Schroeder struct usb_device *dev = port->serial->dev; 2463f542974SPaul B Schroeder val = val & 0x00ff; 2479c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "mos7840_set_reg_sync offset is %x, value %x\n", reg, val); 2483f542974SPaul B Schroeder 2493f542974SPaul B Schroeder return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ, 2503f542974SPaul B Schroeder MCS_WR_RTYPE, val, reg, NULL, 0, 2513f542974SPaul B Schroeder MOS_WDR_TIMEOUT); 2523f542974SPaul B Schroeder } 2533f542974SPaul B Schroeder 2543f542974SPaul B Schroeder /* 2553f542974SPaul B Schroeder * mos7840_get_reg_sync 2563f542974SPaul B Schroeder * To set the Uart register by calling usb_fill_control_urb function by 2573f542974SPaul B Schroeder * passing usb_rcvctrlpipe function as parameter. 2583f542974SPaul B Schroeder */ 2593f542974SPaul B Schroeder 2603f542974SPaul B Schroeder static int mos7840_get_reg_sync(struct usb_serial_port *port, __u16 reg, 2613f542974SPaul B Schroeder __u16 *val) 2623f542974SPaul B Schroeder { 2633f542974SPaul B Schroeder struct usb_device *dev = port->serial->dev; 2643f542974SPaul B Schroeder int ret = 0; 2659e221a35SJohan Hovold u8 *buf; 2669e221a35SJohan Hovold 2679e221a35SJohan Hovold buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL); 2689e221a35SJohan Hovold if (!buf) 2699e221a35SJohan Hovold return -ENOMEM; 2703f542974SPaul B Schroeder 2713f542974SPaul B Schroeder ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ, 2729e221a35SJohan Hovold MCS_RD_RTYPE, 0, reg, buf, VENDOR_READ_LENGTH, 2733f542974SPaul B Schroeder MOS_WDR_TIMEOUT); 274cd8db057SJohan Hovold if (ret < VENDOR_READ_LENGTH) { 275cd8db057SJohan Hovold if (ret >= 0) 276cd8db057SJohan Hovold ret = -EIO; 277cd8db057SJohan Hovold goto out; 278cd8db057SJohan Hovold } 279cd8db057SJohan Hovold 2809e221a35SJohan Hovold *val = buf[0]; 2819c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s offset is %x, return val %x\n", __func__, reg, *val); 282cd8db057SJohan Hovold out: 2839e221a35SJohan Hovold kfree(buf); 2843f542974SPaul B Schroeder return ret; 2853f542974SPaul B Schroeder } 2863f542974SPaul B Schroeder 2873f542974SPaul B Schroeder /* 2883f542974SPaul B Schroeder * mos7840_set_uart_reg 2893f542974SPaul B Schroeder * To set the Uart register by calling usb_fill_control_urb function by 2903f542974SPaul B Schroeder * passing usb_sndctrlpipe function as parameter. 2913f542974SPaul B Schroeder */ 2923f542974SPaul B Schroeder 2933f542974SPaul B Schroeder static int mos7840_set_uart_reg(struct usb_serial_port *port, __u16 reg, 2943f542974SPaul B Schroeder __u16 val) 2953f542974SPaul B Schroeder { 2963f542974SPaul B Schroeder 2973f542974SPaul B Schroeder struct usb_device *dev = port->serial->dev; 2983f542974SPaul B Schroeder val = val & 0x00ff; 299880af9dbSAlan Cox /* For the UART control registers, the application number need 300880af9dbSAlan Cox to be Or'ed */ 3010de9a702SOliver Neukum if (port->serial->num_ports == 4) { 3021143832eSGreg Kroah-Hartman val |= ((__u16)port->port_number + 1) << 8; 3033f542974SPaul B Schroeder } else { 3041143832eSGreg Kroah-Hartman if (port->port_number == 0) { 3051143832eSGreg Kroah-Hartman val |= ((__u16)port->port_number + 1) << 8; 3063f542974SPaul B Schroeder } else { 3071143832eSGreg Kroah-Hartman val |= ((__u16)port->port_number + 2) << 8; 3083f542974SPaul B Schroeder } 3093f542974SPaul B Schroeder } 3109c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s application number is %x\n", __func__, val); 3113f542974SPaul B Schroeder return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ, 3123f542974SPaul B Schroeder MCS_WR_RTYPE, val, reg, NULL, 0, 3133f542974SPaul B Schroeder MOS_WDR_TIMEOUT); 3143f542974SPaul B Schroeder 3153f542974SPaul B Schroeder } 3163f542974SPaul B Schroeder 3173f542974SPaul B Schroeder /* 3183f542974SPaul B Schroeder * mos7840_get_uart_reg 3193f542974SPaul B Schroeder * To set the Control register by calling usb_fill_control_urb function 3203f542974SPaul B Schroeder * by passing usb_rcvctrlpipe function as parameter. 3213f542974SPaul B Schroeder */ 3223f542974SPaul B Schroeder static int mos7840_get_uart_reg(struct usb_serial_port *port, __u16 reg, 3233f542974SPaul B Schroeder __u16 *val) 3243f542974SPaul B Schroeder { 3253f542974SPaul B Schroeder struct usb_device *dev = port->serial->dev; 3263f542974SPaul B Schroeder int ret = 0; 3273f542974SPaul B Schroeder __u16 Wval; 3289e221a35SJohan Hovold u8 *buf; 3299e221a35SJohan Hovold 3309e221a35SJohan Hovold buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL); 3319e221a35SJohan Hovold if (!buf) 3329e221a35SJohan Hovold return -ENOMEM; 3333f542974SPaul B Schroeder 3343f542974SPaul B Schroeder /* Wval is same as application number */ 3350de9a702SOliver Neukum if (port->serial->num_ports == 4) { 3361143832eSGreg Kroah-Hartman Wval = ((__u16)port->port_number + 1) << 8; 3373f542974SPaul B Schroeder } else { 3381143832eSGreg Kroah-Hartman if (port->port_number == 0) { 3391143832eSGreg Kroah-Hartman Wval = ((__u16)port->port_number + 1) << 8; 3403f542974SPaul B Schroeder } else { 3411143832eSGreg Kroah-Hartman Wval = ((__u16)port->port_number + 2) << 8; 3423f542974SPaul B Schroeder } 3433f542974SPaul B Schroeder } 3449c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s application number is %x\n", __func__, Wval); 3453f542974SPaul B Schroeder ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ, 3469e221a35SJohan Hovold MCS_RD_RTYPE, Wval, reg, buf, VENDOR_READ_LENGTH, 3473f542974SPaul B Schroeder MOS_WDR_TIMEOUT); 348cd8db057SJohan Hovold if (ret < VENDOR_READ_LENGTH) { 349cd8db057SJohan Hovold if (ret >= 0) 350cd8db057SJohan Hovold ret = -EIO; 351cd8db057SJohan Hovold goto out; 352cd8db057SJohan Hovold } 3539e221a35SJohan Hovold *val = buf[0]; 354cd8db057SJohan Hovold out: 3559e221a35SJohan Hovold kfree(buf); 3563f542974SPaul B Schroeder return ret; 3573f542974SPaul B Schroeder } 3583f542974SPaul B Schroeder 3599c134a14SGreg Kroah-Hartman static void mos7840_dump_serial_port(struct usb_serial_port *port, 3609c134a14SGreg Kroah-Hartman struct moschip_port *mos7840_port) 3613f542974SPaul B Schroeder { 3623f542974SPaul B Schroeder 3639c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "SpRegOffset is %2x\n", mos7840_port->SpRegOffset); 3649c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "ControlRegOffset is %2x\n", mos7840_port->ControlRegOffset); 3659c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "DCRRegOffset is %2x\n", mos7840_port->DcrRegOffset); 3663f542974SPaul B Schroeder 3673f542974SPaul B Schroeder } 3683f542974SPaul B Schroeder 3693f542974SPaul B Schroeder /************************************************************************/ 3703f542974SPaul B Schroeder /************************************************************************/ 3713f542974SPaul B Schroeder /* I N T E R F A C E F U N C T I O N S */ 3723f542974SPaul B Schroeder /* I N T E R F A C E F U N C T I O N S */ 3733f542974SPaul B Schroeder /************************************************************************/ 3743f542974SPaul B Schroeder /************************************************************************/ 3753f542974SPaul B Schroeder 3763f542974SPaul B Schroeder static inline void mos7840_set_port_private(struct usb_serial_port *port, 3773f542974SPaul B Schroeder struct moschip_port *data) 3783f542974SPaul B Schroeder { 3793f542974SPaul B Schroeder usb_set_serial_port_data(port, (void *)data); 3803f542974SPaul B Schroeder } 3813f542974SPaul B Schroeder 3823f542974SPaul B Schroeder static inline struct moschip_port *mos7840_get_port_private(struct 3833f542974SPaul B Schroeder usb_serial_port 3843f542974SPaul B Schroeder *port) 3853f542974SPaul B Schroeder { 3863f542974SPaul B Schroeder return (struct moschip_port *)usb_get_serial_port_data(port); 3873f542974SPaul B Schroeder } 3883f542974SPaul B Schroeder 3890de9a702SOliver Neukum static void mos7840_handle_new_msr(struct moschip_port *port, __u8 new_msr) 3903f542974SPaul B Schroeder { 3913f542974SPaul B Schroeder struct moschip_port *mos7840_port; 3923f542974SPaul B Schroeder struct async_icount *icount; 3933f542974SPaul B Schroeder mos7840_port = port; 3943f542974SPaul B Schroeder if (new_msr & 3953f542974SPaul B Schroeder (MOS_MSR_DELTA_CTS | MOS_MSR_DELTA_DSR | MOS_MSR_DELTA_RI | 3963f542974SPaul B Schroeder MOS_MSR_DELTA_CD)) { 3978c1a07ffSJohan Hovold icount = &mos7840_port->port->icount; 3983f542974SPaul B Schroeder 3993f542974SPaul B Schroeder /* update input line counters */ 400c9fac853SJohan Hovold if (new_msr & MOS_MSR_DELTA_CTS) 4013f542974SPaul B Schroeder icount->cts++; 402c9fac853SJohan Hovold if (new_msr & MOS_MSR_DELTA_DSR) 4033f542974SPaul B Schroeder icount->dsr++; 404c9fac853SJohan Hovold if (new_msr & MOS_MSR_DELTA_CD) 4053f542974SPaul B Schroeder icount->dcd++; 406c9fac853SJohan Hovold if (new_msr & MOS_MSR_DELTA_RI) 4073f542974SPaul B Schroeder icount->rng++; 408e670c6afSJohan Hovold 4090c613371SJohan Hovold wake_up_interruptible(&port->port->port.delta_msr_wait); 4103f542974SPaul B Schroeder } 4113f542974SPaul B Schroeder } 4123f542974SPaul B Schroeder 4130de9a702SOliver Neukum static void mos7840_handle_new_lsr(struct moschip_port *port, __u8 new_lsr) 4143f542974SPaul B Schroeder { 4153f542974SPaul B Schroeder struct async_icount *icount; 4163f542974SPaul B Schroeder 4173f542974SPaul B Schroeder if (new_lsr & SERIAL_LSR_BI) { 418880af9dbSAlan Cox /* 419880af9dbSAlan Cox * Parity and Framing errors only count if they 420880af9dbSAlan Cox * occur exclusive of a break being 421880af9dbSAlan Cox * received. 422880af9dbSAlan Cox */ 4233f542974SPaul B Schroeder new_lsr &= (__u8) (SERIAL_LSR_OE | SERIAL_LSR_BI); 4243f542974SPaul B Schroeder } 4253f542974SPaul B Schroeder 4263f542974SPaul B Schroeder /* update input line counters */ 4278c1a07ffSJohan Hovold icount = &port->port->icount; 428c9fac853SJohan Hovold if (new_lsr & SERIAL_LSR_BI) 4293f542974SPaul B Schroeder icount->brk++; 430c9fac853SJohan Hovold if (new_lsr & SERIAL_LSR_OE) 4313f542974SPaul B Schroeder icount->overrun++; 432c9fac853SJohan Hovold if (new_lsr & SERIAL_LSR_PE) 4333f542974SPaul B Schroeder icount->parity++; 434c9fac853SJohan Hovold if (new_lsr & SERIAL_LSR_FE) 4353f542974SPaul B Schroeder icount->frame++; 4363f542974SPaul B Schroeder } 4373f542974SPaul B Schroeder 4383f542974SPaul B Schroeder /************************************************************************/ 4393f542974SPaul B Schroeder /************************************************************************/ 4403f542974SPaul B Schroeder /* U S B C A L L B A C K F U N C T I O N S */ 4413f542974SPaul B Schroeder /* U S B C A L L B A C K F U N C T I O N S */ 4423f542974SPaul B Schroeder /************************************************************************/ 4433f542974SPaul B Schroeder /************************************************************************/ 4443f542974SPaul B Schroeder 4457d12e780SDavid Howells static void mos7840_control_callback(struct urb *urb) 4463f542974SPaul B Schroeder { 4473f542974SPaul B Schroeder unsigned char *data; 4483f542974SPaul B Schroeder struct moschip_port *mos7840_port; 4499c134a14SGreg Kroah-Hartman struct device *dev = &urb->dev->dev; 4503f542974SPaul B Schroeder __u8 regval = 0x0; 4510643c724SGreg Kroah-Hartman int status = urb->status; 4523f542974SPaul B Schroeder 453cdc97792SMing Lei mos7840_port = urb->context; 4540de9a702SOliver Neukum 4550643c724SGreg Kroah-Hartman switch (status) { 4563f542974SPaul B Schroeder case 0: 4573f542974SPaul B Schroeder /* success */ 4583f542974SPaul B Schroeder break; 4593f542974SPaul B Schroeder case -ECONNRESET: 4603f542974SPaul B Schroeder case -ENOENT: 4613f542974SPaul B Schroeder case -ESHUTDOWN: 4623f542974SPaul B Schroeder /* this urb is terminated, clean up */ 4639c134a14SGreg Kroah-Hartman dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status); 464d8a083ccSJohan Hovold goto out; 4653f542974SPaul B Schroeder default: 4669c134a14SGreg Kroah-Hartman dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status); 467d8a083ccSJohan Hovold goto out; 4683f542974SPaul B Schroeder } 4693f542974SPaul B Schroeder 4709c134a14SGreg Kroah-Hartman dev_dbg(dev, "%s urb buffer size is %d\n", __func__, urb->actual_length); 4719c134a14SGreg Kroah-Hartman dev_dbg(dev, "%s mos7840_port->MsrLsr is %d port %d\n", __func__, 4723f542974SPaul B Schroeder mos7840_port->MsrLsr, mos7840_port->port_num); 4733f542974SPaul B Schroeder data = urb->transfer_buffer; 4743f542974SPaul B Schroeder regval = (__u8) data[0]; 4759c134a14SGreg Kroah-Hartman dev_dbg(dev, "%s data is %x\n", __func__, regval); 4763f542974SPaul B Schroeder if (mos7840_port->MsrLsr == 0) 4773f542974SPaul B Schroeder mos7840_handle_new_msr(mos7840_port, regval); 4783f542974SPaul B Schroeder else if (mos7840_port->MsrLsr == 1) 4793f542974SPaul B Schroeder mos7840_handle_new_lsr(mos7840_port, regval); 480d8a083ccSJohan Hovold out: 481d8a083ccSJohan Hovold clear_bit_unlock(MOS7840_FLAG_CTRL_BUSY, &mos7840_port->flags); 4823f542974SPaul B Schroeder } 4833f542974SPaul B Schroeder 4843f542974SPaul B Schroeder static int mos7840_get_reg(struct moschip_port *mcs, __u16 Wval, __u16 reg, 4853f542974SPaul B Schroeder __u16 *val) 4863f542974SPaul B Schroeder { 4873f542974SPaul B Schroeder struct usb_device *dev = mcs->port->serial->dev; 4880de9a702SOliver Neukum struct usb_ctrlrequest *dr = mcs->dr; 4890de9a702SOliver Neukum unsigned char *buffer = mcs->ctrl_buf; 4900de9a702SOliver Neukum int ret; 4913f542974SPaul B Schroeder 492d8a083ccSJohan Hovold if (test_and_set_bit_lock(MOS7840_FLAG_CTRL_BUSY, &mcs->flags)) 493d8a083ccSJohan Hovold return -EBUSY; 494d8a083ccSJohan Hovold 4953f542974SPaul B Schroeder dr->bRequestType = MCS_RD_RTYPE; 4963f542974SPaul B Schroeder dr->bRequest = MCS_RDREQ; 497880af9dbSAlan Cox dr->wValue = cpu_to_le16(Wval); /* 0 */ 4983f542974SPaul B Schroeder dr->wIndex = cpu_to_le16(reg); 4993f542974SPaul B Schroeder dr->wLength = cpu_to_le16(2); 5003f542974SPaul B Schroeder 5013f542974SPaul B Schroeder usb_fill_control_urb(mcs->control_urb, dev, usb_rcvctrlpipe(dev, 0), 5023f542974SPaul B Schroeder (unsigned char *)dr, buffer, 2, 5033f542974SPaul B Schroeder mos7840_control_callback, mcs); 5043f542974SPaul B Schroeder mcs->control_urb->transfer_buffer_length = 2; 5053f542974SPaul B Schroeder ret = usb_submit_urb(mcs->control_urb, GFP_ATOMIC); 506d8a083ccSJohan Hovold if (ret) 507d8a083ccSJohan Hovold clear_bit_unlock(MOS7840_FLAG_CTRL_BUSY, &mcs->flags); 508d8a083ccSJohan Hovold 5093f542974SPaul B Schroeder return ret; 5103f542974SPaul B Schroeder } 5113f542974SPaul B Schroeder 5120eafe4deSDonald static void mos7840_set_led_callback(struct urb *urb) 5130eafe4deSDonald { 5140eafe4deSDonald switch (urb->status) { 5150eafe4deSDonald case 0: 5160eafe4deSDonald /* Success */ 5170eafe4deSDonald break; 5180eafe4deSDonald case -ECONNRESET: 5190eafe4deSDonald case -ENOENT: 5200eafe4deSDonald case -ESHUTDOWN: 5210eafe4deSDonald /* This urb is terminated, clean up */ 522d9a38a87SJohan Hovold dev_dbg(&urb->dev->dev, "%s - urb shutting down: %d\n", 5239c134a14SGreg Kroah-Hartman __func__, urb->status); 5240eafe4deSDonald break; 5250eafe4deSDonald default: 526d9a38a87SJohan Hovold dev_dbg(&urb->dev->dev, "%s - nonzero urb status: %d\n", 5279c134a14SGreg Kroah-Hartman __func__, urb->status); 5280eafe4deSDonald } 5290eafe4deSDonald } 5300eafe4deSDonald 5310eafe4deSDonald static void mos7840_set_led_async(struct moschip_port *mcs, __u16 wval, 5320eafe4deSDonald __u16 reg) 5330eafe4deSDonald { 5340eafe4deSDonald struct usb_device *dev = mcs->port->serial->dev; 53505cf0decSJohan Hovold struct usb_ctrlrequest *dr = mcs->led_dr; 5360eafe4deSDonald 5370eafe4deSDonald dr->bRequestType = MCS_WR_RTYPE; 5380eafe4deSDonald dr->bRequest = MCS_WRREQ; 5390eafe4deSDonald dr->wValue = cpu_to_le16(wval); 5400eafe4deSDonald dr->wIndex = cpu_to_le16(reg); 5410eafe4deSDonald dr->wLength = cpu_to_le16(0); 5420eafe4deSDonald 54305cf0decSJohan Hovold usb_fill_control_urb(mcs->led_urb, dev, usb_sndctrlpipe(dev, 0), 5440eafe4deSDonald (unsigned char *)dr, NULL, 0, mos7840_set_led_callback, NULL); 5450eafe4deSDonald 54605cf0decSJohan Hovold usb_submit_urb(mcs->led_urb, GFP_ATOMIC); 5470eafe4deSDonald } 5480eafe4deSDonald 5490eafe4deSDonald static void mos7840_set_led_sync(struct usb_serial_port *port, __u16 reg, 5500eafe4deSDonald __u16 val) 5510eafe4deSDonald { 5520eafe4deSDonald struct usb_device *dev = port->serial->dev; 5530eafe4deSDonald 5540eafe4deSDonald usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ, MCS_WR_RTYPE, 5550eafe4deSDonald val, reg, NULL, 0, MOS_WDR_TIMEOUT); 5560eafe4deSDonald } 5570eafe4deSDonald 558e99e88a9SKees Cook static void mos7840_led_off(struct timer_list *t) 5590eafe4deSDonald { 560e99e88a9SKees Cook struct moschip_port *mcs = from_timer(mcs, t, led_timer1); 5610eafe4deSDonald 5620eafe4deSDonald /* Turn off LED */ 5630eafe4deSDonald mos7840_set_led_async(mcs, 0x0300, MODEM_CONTROL_REGISTER); 5640eafe4deSDonald mod_timer(&mcs->led_timer2, 5650eafe4deSDonald jiffies + msecs_to_jiffies(LED_OFF_MS)); 5660eafe4deSDonald } 5670eafe4deSDonald 568e99e88a9SKees Cook static void mos7840_led_flag_off(struct timer_list *t) 5690eafe4deSDonald { 570e99e88a9SKees Cook struct moschip_port *mcs = from_timer(mcs, t, led_timer2); 5710eafe4deSDonald 57205cf0decSJohan Hovold clear_bit_unlock(MOS7840_FLAG_LED_BUSY, &mcs->flags); 57305cf0decSJohan Hovold } 57405cf0decSJohan Hovold 57505cf0decSJohan Hovold static void mos7840_led_activity(struct usb_serial_port *port) 57605cf0decSJohan Hovold { 57705cf0decSJohan Hovold struct moschip_port *mos7840_port = usb_get_serial_port_data(port); 57805cf0decSJohan Hovold 57905cf0decSJohan Hovold if (test_and_set_bit_lock(MOS7840_FLAG_LED_BUSY, &mos7840_port->flags)) 58005cf0decSJohan Hovold return; 58105cf0decSJohan Hovold 58205cf0decSJohan Hovold mos7840_set_led_async(mos7840_port, 0x0301, MODEM_CONTROL_REGISTER); 58305cf0decSJohan Hovold mod_timer(&mos7840_port->led_timer1, 58405cf0decSJohan Hovold jiffies + msecs_to_jiffies(LED_ON_MS)); 5850eafe4deSDonald } 5860eafe4deSDonald 5873f542974SPaul B Schroeder /***************************************************************************** 5883f542974SPaul B Schroeder * mos7840_interrupt_callback 5893f542974SPaul B Schroeder * this is the callback function for when we have received data on the 5903f542974SPaul B Schroeder * interrupt endpoint. 5913f542974SPaul B Schroeder *****************************************************************************/ 5923f542974SPaul B Schroeder 5937d12e780SDavid Howells static void mos7840_interrupt_callback(struct urb *urb) 5943f542974SPaul B Schroeder { 5953f542974SPaul B Schroeder int result; 5963f542974SPaul B Schroeder int length; 5973f542974SPaul B Schroeder struct moschip_port *mos7840_port; 5983f542974SPaul B Schroeder struct usb_serial *serial; 5993f542974SPaul B Schroeder __u16 Data; 6003f542974SPaul B Schroeder unsigned char *data; 6013f542974SPaul B Schroeder __u8 sp[5], st; 6020de9a702SOliver Neukum int i, rv = 0; 6030de9a702SOliver Neukum __u16 wval, wreg = 0; 6040643c724SGreg Kroah-Hartman int status = urb->status; 6053f542974SPaul B Schroeder 6060643c724SGreg Kroah-Hartman switch (status) { 6073f542974SPaul B Schroeder case 0: 6083f542974SPaul B Schroeder /* success */ 6093f542974SPaul B Schroeder break; 6103f542974SPaul B Schroeder case -ECONNRESET: 6113f542974SPaul B Schroeder case -ENOENT: 6123f542974SPaul B Schroeder case -ESHUTDOWN: 6133f542974SPaul B Schroeder /* this urb is terminated, clean up */ 6149c134a14SGreg Kroah-Hartman dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n", 6159c134a14SGreg Kroah-Hartman __func__, status); 6163f542974SPaul B Schroeder return; 6173f542974SPaul B Schroeder default: 6189c134a14SGreg Kroah-Hartman dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n", 6199c134a14SGreg Kroah-Hartman __func__, status); 6203f542974SPaul B Schroeder goto exit; 6213f542974SPaul B Schroeder } 6223f542974SPaul B Schroeder 6233f542974SPaul B Schroeder length = urb->actual_length; 6243f542974SPaul B Schroeder data = urb->transfer_buffer; 6253f542974SPaul B Schroeder 626cdc97792SMing Lei serial = urb->context; 6273f542974SPaul B Schroeder 6283f542974SPaul B Schroeder /* Moschip get 5 bytes 6293f542974SPaul B Schroeder * Byte 1 IIR Port 1 (port.number is 0) 6303f542974SPaul B Schroeder * Byte 2 IIR Port 2 (port.number is 1) 6313f542974SPaul B Schroeder * Byte 3 IIR Port 3 (port.number is 2) 6323f542974SPaul B Schroeder * Byte 4 IIR Port 4 (port.number is 3) 6333f542974SPaul B Schroeder * Byte 5 FIFO status for both */ 6343f542974SPaul B Schroeder 635365a0442SGeyslan G. Bem if (length > 5) { 6369c134a14SGreg Kroah-Hartman dev_dbg(&urb->dev->dev, "%s", "Wrong data !!!\n"); 6373f542974SPaul B Schroeder return; 6383f542974SPaul B Schroeder } 6393f542974SPaul B Schroeder 6403f542974SPaul B Schroeder sp[0] = (__u8) data[0]; 6413f542974SPaul B Schroeder sp[1] = (__u8) data[1]; 6423f542974SPaul B Schroeder sp[2] = (__u8) data[2]; 6433f542974SPaul B Schroeder sp[3] = (__u8) data[3]; 6443f542974SPaul B Schroeder st = (__u8) data[4]; 6453f542974SPaul B Schroeder 6463f542974SPaul B Schroeder for (i = 0; i < serial->num_ports; i++) { 6473f542974SPaul B Schroeder mos7840_port = mos7840_get_port_private(serial->port[i]); 6481143832eSGreg Kroah-Hartman wval = ((__u16)serial->port[i]->port_number + 1) << 8; 6493f542974SPaul B Schroeder if (mos7840_port->open) { 6503f542974SPaul B Schroeder if (sp[i] & 0x01) { 6519c134a14SGreg Kroah-Hartman dev_dbg(&urb->dev->dev, "SP%d No Interrupt !!!\n", i); 6523f542974SPaul B Schroeder } else { 6533f542974SPaul B Schroeder switch (sp[i] & 0x0f) { 6543f542974SPaul B Schroeder case SERIAL_IIR_RLS: 6559c134a14SGreg Kroah-Hartman dev_dbg(&urb->dev->dev, "Serial Port %d: Receiver status error or \n", i); 6569c134a14SGreg Kroah-Hartman dev_dbg(&urb->dev->dev, "address bit detected in 9-bit mode\n"); 6573f542974SPaul B Schroeder mos7840_port->MsrLsr = 1; 6580de9a702SOliver Neukum wreg = LINE_STATUS_REGISTER; 6593f542974SPaul B Schroeder break; 6603f542974SPaul B Schroeder case SERIAL_IIR_MS: 6619c134a14SGreg Kroah-Hartman dev_dbg(&urb->dev->dev, "Serial Port %d: Modem status change\n", i); 6623f542974SPaul B Schroeder mos7840_port->MsrLsr = 0; 6630de9a702SOliver Neukum wreg = MODEM_STATUS_REGISTER; 6643f542974SPaul B Schroeder break; 6653f542974SPaul B Schroeder } 6660de9a702SOliver Neukum rv = mos7840_get_reg(mos7840_port, wval, wreg, &Data); 6673f542974SPaul B Schroeder } 6683f542974SPaul B Schroeder } 6693f542974SPaul B Schroeder } 670880af9dbSAlan Cox if (!(rv < 0)) 671880af9dbSAlan Cox /* the completion handler for the control urb will resubmit */ 6720de9a702SOliver Neukum return; 6733f542974SPaul B Schroeder exit: 6743f542974SPaul B Schroeder result = usb_submit_urb(urb, GFP_ATOMIC); 6753f542974SPaul B Schroeder if (result) { 6763f542974SPaul B Schroeder dev_err(&urb->dev->dev, 6773f542974SPaul B Schroeder "%s - Error %d submitting interrupt urb\n", 678441b62c1SHarvey Harrison __func__, result); 6793f542974SPaul B Schroeder } 6803f542974SPaul B Schroeder } 6813f542974SPaul B Schroeder 6823f542974SPaul B Schroeder static int mos7840_port_paranoia_check(struct usb_serial_port *port, 6833f542974SPaul B Schroeder const char *function) 6843f542974SPaul B Schroeder { 6853f542974SPaul B Schroeder if (!port) { 6869c134a14SGreg Kroah-Hartman pr_debug("%s - port == NULL\n", function); 6873f542974SPaul B Schroeder return -1; 6883f542974SPaul B Schroeder } 6893f542974SPaul B Schroeder if (!port->serial) { 6909c134a14SGreg Kroah-Hartman pr_debug("%s - port->serial == NULL\n", function); 6913f542974SPaul B Schroeder return -1; 6923f542974SPaul B Schroeder } 6933f542974SPaul B Schroeder 6943f542974SPaul B Schroeder return 0; 6953f542974SPaul B Schroeder } 6963f542974SPaul B Schroeder 6973f542974SPaul B Schroeder /* Inline functions to check the sanity of a pointer that is passed to us */ 6983f542974SPaul B Schroeder static int mos7840_serial_paranoia_check(struct usb_serial *serial, 6993f542974SPaul B Schroeder const char *function) 7003f542974SPaul B Schroeder { 7013f542974SPaul B Schroeder if (!serial) { 7029c134a14SGreg Kroah-Hartman pr_debug("%s - serial == NULL\n", function); 7033f542974SPaul B Schroeder return -1; 7043f542974SPaul B Schroeder } 7053f542974SPaul B Schroeder if (!serial->type) { 7069c134a14SGreg Kroah-Hartman pr_debug("%s - serial->type == NULL!\n", function); 7073f542974SPaul B Schroeder return -1; 7083f542974SPaul B Schroeder } 7093f542974SPaul B Schroeder 7103f542974SPaul B Schroeder return 0; 7113f542974SPaul B Schroeder } 7123f542974SPaul B Schroeder 7133f542974SPaul B Schroeder static struct usb_serial *mos7840_get_usb_serial(struct usb_serial_port *port, 7143f542974SPaul B Schroeder const char *function) 7153f542974SPaul B Schroeder { 7163f542974SPaul B Schroeder /* if no port was specified, or it fails a paranoia check */ 7173f542974SPaul B Schroeder if (!port || 7183f542974SPaul B Schroeder mos7840_port_paranoia_check(port, function) || 7193f542974SPaul B Schroeder mos7840_serial_paranoia_check(port->serial, function)) { 720880af9dbSAlan Cox /* then say that we don't have a valid usb_serial thing, 721880af9dbSAlan Cox * which will end up genrating -ENODEV return values */ 7223f542974SPaul B Schroeder return NULL; 7233f542974SPaul B Schroeder } 7243f542974SPaul B Schroeder 7253f542974SPaul B Schroeder return port->serial; 7263f542974SPaul B Schroeder } 7273f542974SPaul B Schroeder 7283f542974SPaul B Schroeder /***************************************************************************** 7293f542974SPaul B Schroeder * mos7840_bulk_in_callback 7303f542974SPaul B Schroeder * this is the callback function for when we have received data on the 7313f542974SPaul B Schroeder * bulk in endpoint. 7323f542974SPaul B Schroeder *****************************************************************************/ 7333f542974SPaul B Schroeder 7347d12e780SDavid Howells static void mos7840_bulk_in_callback(struct urb *urb) 7353f542974SPaul B Schroeder { 7360643c724SGreg Kroah-Hartman int retval; 7373f542974SPaul B Schroeder unsigned char *data; 7383f542974SPaul B Schroeder struct usb_serial *serial; 7393f542974SPaul B Schroeder struct usb_serial_port *port; 7403f542974SPaul B Schroeder struct moschip_port *mos7840_port; 7410643c724SGreg Kroah-Hartman int status = urb->status; 7423f542974SPaul B Schroeder 743cdc97792SMing Lei mos7840_port = urb->context; 7449c134a14SGreg Kroah-Hartman if (!mos7840_port) 74550de36f7SGreg Kroah-Hartman return; 74650de36f7SGreg Kroah-Hartman 74750de36f7SGreg Kroah-Hartman if (status) { 7489c134a14SGreg Kroah-Hartman dev_dbg(&urb->dev->dev, "nonzero read bulk status received: %d\n", status); 74950de36f7SGreg Kroah-Hartman mos7840_port->read_urb_busy = false; 7503f542974SPaul B Schroeder return; 7513f542974SPaul B Schroeder } 7523f542974SPaul B Schroeder 7539c134a14SGreg Kroah-Hartman port = mos7840_port->port; 754441b62c1SHarvey Harrison if (mos7840_port_paranoia_check(port, __func__)) { 75550de36f7SGreg Kroah-Hartman mos7840_port->read_urb_busy = false; 7563f542974SPaul B Schroeder return; 7573f542974SPaul B Schroeder } 7583f542974SPaul B Schroeder 759441b62c1SHarvey Harrison serial = mos7840_get_usb_serial(port, __func__); 7603f542974SPaul B Schroeder if (!serial) { 76150de36f7SGreg Kroah-Hartman mos7840_port->read_urb_busy = false; 7623f542974SPaul B Schroeder return; 7633f542974SPaul B Schroeder } 7643f542974SPaul B Schroeder 7653f542974SPaul B Schroeder data = urb->transfer_buffer; 76659d33f2fSGreg Kroah-Hartman usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data); 7673f542974SPaul B Schroeder 7683f542974SPaul B Schroeder if (urb->actual_length) { 76905c7cd39SJiri Slaby struct tty_port *tport = &mos7840_port->port->port; 77005c7cd39SJiri Slaby tty_insert_flip_string(tport, data, urb->actual_length); 7712e124b4aSJiri Slaby tty_flip_buffer_push(tport); 7728c1a07ffSJohan Hovold port->icount.rx += urb->actual_length; 7738c1a07ffSJohan Hovold dev_dbg(&port->dev, "icount.rx is %d:\n", port->icount.rx); 7743f542974SPaul B Schroeder } 7753f542974SPaul B Schroeder 7763f542974SPaul B Schroeder if (!mos7840_port->read_urb) { 7779c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s", "URB KILLED !!!\n"); 77850de36f7SGreg Kroah-Hartman mos7840_port->read_urb_busy = false; 7793f542974SPaul B Schroeder return; 7803f542974SPaul B Schroeder } 7813f542974SPaul B Schroeder 78205cf0decSJohan Hovold if (mos7840_port->has_led) 78305cf0decSJohan Hovold mos7840_led_activity(port); 7840de9a702SOliver Neukum 78550de36f7SGreg Kroah-Hartman mos7840_port->read_urb_busy = true; 7860643c724SGreg Kroah-Hartman retval = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC); 7873f542974SPaul B Schroeder 7880643c724SGreg Kroah-Hartman if (retval) { 7899c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, retval = %d\n", retval); 79050de36f7SGreg Kroah-Hartman mos7840_port->read_urb_busy = false; 7913f542974SPaul B Schroeder } 7923f542974SPaul B Schroeder } 7933f542974SPaul B Schroeder 7943f542974SPaul B Schroeder /***************************************************************************** 7953f542974SPaul B Schroeder * mos7840_bulk_out_data_callback 796880af9dbSAlan Cox * this is the callback function for when we have finished sending 797880af9dbSAlan Cox * serial data on the bulk out endpoint. 7983f542974SPaul B Schroeder *****************************************************************************/ 7993f542974SPaul B Schroeder 8007d12e780SDavid Howells static void mos7840_bulk_out_data_callback(struct urb *urb) 8013f542974SPaul B Schroeder { 8023f542974SPaul B Schroeder struct moschip_port *mos7840_port; 8039c134a14SGreg Kroah-Hartman struct usb_serial_port *port; 8040643c724SGreg Kroah-Hartman int status = urb->status; 805*19bfbf46SJohn Ogness unsigned long flags; 8060de9a702SOliver Neukum int i; 8070de9a702SOliver Neukum 808cdc97792SMing Lei mos7840_port = urb->context; 8099c134a14SGreg Kroah-Hartman port = mos7840_port->port; 810*19bfbf46SJohn Ogness spin_lock_irqsave(&mos7840_port->pool_lock, flags); 8110de9a702SOliver Neukum for (i = 0; i < NUM_URBS; i++) { 8120de9a702SOliver Neukum if (urb == mos7840_port->write_urb_pool[i]) { 8130de9a702SOliver Neukum mos7840_port->busy[i] = 0; 8140de9a702SOliver Neukum break; 8150de9a702SOliver Neukum } 8160de9a702SOliver Neukum } 817*19bfbf46SJohn Ogness spin_unlock_irqrestore(&mos7840_port->pool_lock, flags); 8180de9a702SOliver Neukum 8190643c724SGreg Kroah-Hartman if (status) { 8209c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "nonzero write bulk status received:%d\n", status); 8213f542974SPaul B Schroeder return; 8223f542974SPaul B Schroeder } 8233f542974SPaul B Schroeder 8249c134a14SGreg Kroah-Hartman if (mos7840_port_paranoia_check(port, __func__)) 8253f542974SPaul B Schroeder return; 8263f542974SPaul B Schroeder 8276aad04f2SJiri Slaby if (mos7840_port->open) 8286aad04f2SJiri Slaby tty_port_tty_wakeup(&port->port); 8293f542974SPaul B Schroeder 8303f542974SPaul B Schroeder } 8313f542974SPaul B Schroeder 8323f542974SPaul B Schroeder /************************************************************************/ 8333f542974SPaul 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 */ 8343f542974SPaul B Schroeder /************************************************************************/ 8353f542974SPaul B Schroeder 8363f542974SPaul B Schroeder /***************************************************************************** 8373f542974SPaul B Schroeder * mos7840_open 8383f542974SPaul B Schroeder * this function is called by the tty driver when a port is opened 8393f542974SPaul B Schroeder * If successful, we return 0 8403f542974SPaul B Schroeder * Otherwise we return a negative error number. 8413f542974SPaul B Schroeder *****************************************************************************/ 8423f542974SPaul B Schroeder 843a509a7e4SAlan Cox static int mos7840_open(struct tty_struct *tty, struct usb_serial_port *port) 8443f542974SPaul B Schroeder { 8453f542974SPaul B Schroeder int response; 8463f542974SPaul B Schroeder int j; 8473f542974SPaul B Schroeder struct usb_serial *serial; 8483f542974SPaul B Schroeder struct urb *urb; 8493f542974SPaul B Schroeder __u16 Data; 8503f542974SPaul B Schroeder int status; 8513f542974SPaul B Schroeder struct moschip_port *mos7840_port; 8520de9a702SOliver Neukum struct moschip_port *port0; 8533f542974SPaul B Schroeder 8549c134a14SGreg Kroah-Hartman if (mos7840_port_paranoia_check(port, __func__)) 8553f542974SPaul B Schroeder return -ENODEV; 8563f542974SPaul B Schroeder 8573f542974SPaul B Schroeder serial = port->serial; 8583f542974SPaul B Schroeder 8599c134a14SGreg Kroah-Hartman if (mos7840_serial_paranoia_check(serial, __func__)) 8603f542974SPaul B Schroeder return -ENODEV; 8613f542974SPaul B Schroeder 8623f542974SPaul B Schroeder mos7840_port = mos7840_get_port_private(port); 8630de9a702SOliver Neukum port0 = mos7840_get_port_private(serial->port[0]); 8643f542974SPaul B Schroeder 8650de9a702SOliver Neukum if (mos7840_port == NULL || port0 == NULL) 8663f542974SPaul B Schroeder return -ENODEV; 8673f542974SPaul B Schroeder 8683f542974SPaul B Schroeder usb_clear_halt(serial->dev, port->write_urb->pipe); 8693f542974SPaul B Schroeder usb_clear_halt(serial->dev, port->read_urb->pipe); 8700de9a702SOliver Neukum port0->open_ports++; 8713f542974SPaul B Schroeder 8723f542974SPaul B Schroeder /* Initialising the write urb pool */ 8733f542974SPaul B Schroeder for (j = 0; j < NUM_URBS; ++j) { 8740de9a702SOliver Neukum urb = usb_alloc_urb(0, GFP_KERNEL); 8753f542974SPaul B Schroeder mos7840_port->write_urb_pool[j] = urb; 87610c642d0SJohan Hovold if (!urb) 8773f542974SPaul B Schroeder continue; 8783f542974SPaul B Schroeder 879880af9dbSAlan Cox urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE, 880880af9dbSAlan Cox GFP_KERNEL); 8813f542974SPaul B Schroeder if (!urb->transfer_buffer) { 8820de9a702SOliver Neukum usb_free_urb(urb); 8830de9a702SOliver Neukum mos7840_port->write_urb_pool[j] = NULL; 8843f542974SPaul B Schroeder continue; 8853f542974SPaul B Schroeder } 8863f542974SPaul B Schroeder } 8873f542974SPaul B Schroeder 8883f542974SPaul B Schroeder /***************************************************************************** 8893f542974SPaul B Schroeder * Initialize MCS7840 -- Write Init values to corresponding Registers 8903f542974SPaul B Schroeder * 8913f542974SPaul B Schroeder * Register Index 8923f542974SPaul B Schroeder * 1 : IER 8933f542974SPaul B Schroeder * 2 : FCR 8943f542974SPaul B Schroeder * 3 : LCR 8953f542974SPaul B Schroeder * 4 : MCR 8963f542974SPaul B Schroeder * 8973f542974SPaul B Schroeder * 0x08 : SP1/2 Control Reg 8983f542974SPaul B Schroeder *****************************************************************************/ 8993f542974SPaul B Schroeder 900880af9dbSAlan Cox /* NEED to check the following Block */ 9013f542974SPaul B Schroeder 9023f542974SPaul B Schroeder Data = 0x0; 9033f542974SPaul B Schroeder status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data); 9043f542974SPaul B Schroeder if (status < 0) { 9059c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "Reading Spreg failed\n"); 9065f8a2e68SJohan Hovold goto err; 9073f542974SPaul B Schroeder } 9083f542974SPaul B Schroeder Data |= 0x80; 9093f542974SPaul B Schroeder status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data); 9103f542974SPaul B Schroeder if (status < 0) { 9119c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "writing Spreg failed\n"); 9125f8a2e68SJohan Hovold goto err; 9133f542974SPaul B Schroeder } 9143f542974SPaul B Schroeder 9153f542974SPaul B Schroeder Data &= ~0x80; 9163f542974SPaul B Schroeder status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data); 9173f542974SPaul B Schroeder if (status < 0) { 9189c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "writing Spreg failed\n"); 9195f8a2e68SJohan Hovold goto err; 9203f542974SPaul B Schroeder } 921880af9dbSAlan Cox /* End of block to be checked */ 9223f542974SPaul B Schroeder 9233f542974SPaul B Schroeder Data = 0x0; 924880af9dbSAlan Cox status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset, 925880af9dbSAlan Cox &Data); 9263f542974SPaul B Schroeder if (status < 0) { 9279c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "Reading Controlreg failed\n"); 9285f8a2e68SJohan Hovold goto err; 9293f542974SPaul B Schroeder } 930880af9dbSAlan Cox Data |= 0x08; /* Driver done bit */ 931880af9dbSAlan Cox Data |= 0x20; /* rx_disable */ 932880af9dbSAlan Cox status = mos7840_set_reg_sync(port, 933880af9dbSAlan Cox mos7840_port->ControlRegOffset, Data); 9343f542974SPaul B Schroeder if (status < 0) { 9359c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "writing Controlreg failed\n"); 9365f8a2e68SJohan Hovold goto err; 9373f542974SPaul B Schroeder } 938880af9dbSAlan Cox /* do register settings here */ 939880af9dbSAlan Cox /* Set all regs to the device default values. */ 940880af9dbSAlan Cox /*********************************** 941880af9dbSAlan Cox * First Disable all interrupts. 942880af9dbSAlan Cox ***********************************/ 9433f542974SPaul B Schroeder Data = 0x00; 9443f542974SPaul B Schroeder status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data); 9453f542974SPaul B Schroeder if (status < 0) { 9469c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "disabling interrupts failed\n"); 9475f8a2e68SJohan Hovold goto err; 9483f542974SPaul B Schroeder } 949880af9dbSAlan Cox /* Set FIFO_CONTROL_REGISTER to the default value */ 9503f542974SPaul B Schroeder Data = 0x00; 9513f542974SPaul B Schroeder status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data); 9523f542974SPaul B Schroeder if (status < 0) { 9539c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "Writing FIFO_CONTROL_REGISTER failed\n"); 9545f8a2e68SJohan Hovold goto err; 9553f542974SPaul B Schroeder } 9563f542974SPaul B Schroeder 9573f542974SPaul B Schroeder Data = 0xcf; 9583f542974SPaul B Schroeder status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data); 9593f542974SPaul B Schroeder if (status < 0) { 9609c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "Writing FIFO_CONTROL_REGISTER failed\n"); 9615f8a2e68SJohan Hovold goto err; 9623f542974SPaul B Schroeder } 9633f542974SPaul B Schroeder 9643f542974SPaul B Schroeder Data = 0x03; 9653f542974SPaul B Schroeder status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data); 9663f542974SPaul B Schroeder mos7840_port->shadowLCR = Data; 9673f542974SPaul B Schroeder 9683f542974SPaul B Schroeder Data = 0x0b; 9693f542974SPaul B Schroeder status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data); 9703f542974SPaul B Schroeder mos7840_port->shadowMCR = Data; 9713f542974SPaul B Schroeder 9723f542974SPaul B Schroeder Data = 0x00; 9733f542974SPaul B Schroeder status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data); 9743f542974SPaul B Schroeder mos7840_port->shadowLCR = Data; 9753f542974SPaul B Schroeder 976880af9dbSAlan Cox Data |= SERIAL_LCR_DLAB; /* data latch enable in LCR 0x80 */ 9773f542974SPaul B Schroeder status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data); 9783f542974SPaul B Schroeder 9793f542974SPaul B Schroeder Data = 0x0c; 9803f542974SPaul B Schroeder status = mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data); 9813f542974SPaul B Schroeder 9823f542974SPaul B Schroeder Data = 0x0; 9833f542974SPaul B Schroeder status = mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data); 9843f542974SPaul B Schroeder 9853f542974SPaul B Schroeder Data = 0x00; 9863f542974SPaul B Schroeder status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data); 9873f542974SPaul B Schroeder 9883f542974SPaul B Schroeder Data = Data & ~SERIAL_LCR_DLAB; 9893f542974SPaul B Schroeder status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data); 9903f542974SPaul B Schroeder mos7840_port->shadowLCR = Data; 9913f542974SPaul B Schroeder 992880af9dbSAlan Cox /* clearing Bulkin and Bulkout Fifo */ 9933f542974SPaul B Schroeder Data = 0x0; 9943f542974SPaul B Schroeder status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data); 9953f542974SPaul B Schroeder 9963f542974SPaul B Schroeder Data = Data | 0x0c; 9973f542974SPaul B Schroeder status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data); 9983f542974SPaul B Schroeder 9993f542974SPaul B Schroeder Data = Data & ~0x0c; 10003f542974SPaul B Schroeder status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data); 1001880af9dbSAlan Cox /* Finally enable all interrupts */ 10023f542974SPaul B Schroeder Data = 0x0c; 10033f542974SPaul B Schroeder status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data); 10043f542974SPaul B Schroeder 1005880af9dbSAlan Cox /* clearing rx_disable */ 10063f542974SPaul B Schroeder Data = 0x0; 1007880af9dbSAlan Cox status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset, 1008880af9dbSAlan Cox &Data); 10093f542974SPaul B Schroeder Data = Data & ~0x20; 1010880af9dbSAlan Cox status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset, 1011880af9dbSAlan Cox Data); 10123f542974SPaul B Schroeder 1013880af9dbSAlan Cox /* rx_negate */ 10143f542974SPaul B Schroeder Data = 0x0; 1015880af9dbSAlan Cox status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset, 1016880af9dbSAlan Cox &Data); 10173f542974SPaul B Schroeder Data = Data | 0x10; 1018880af9dbSAlan Cox status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset, 1019880af9dbSAlan Cox Data); 10203f542974SPaul B Schroeder 10213f542974SPaul B Schroeder /* Check to see if we've set up our endpoint info yet * 10223f542974SPaul B Schroeder * (can't set it up in mos7840_startup as the structures * 10233f542974SPaul B Schroeder * were not set up at that time.) */ 10240de9a702SOliver Neukum if (port0->open_ports == 1) { 10255182c2cfSJohan Hovold /* FIXME: Buffer never NULL, so URB is not submitted. */ 10263f542974SPaul B Schroeder if (serial->port[0]->interrupt_in_buffer == NULL) { 10273f542974SPaul B Schroeder /* set up interrupt urb */ 10283f542974SPaul B Schroeder usb_fill_int_urb(serial->port[0]->interrupt_in_urb, 10293f542974SPaul B Schroeder serial->dev, 10303f542974SPaul B Schroeder usb_rcvintpipe(serial->dev, 1031880af9dbSAlan Cox serial->port[0]->interrupt_in_endpointAddress), 10323f542974SPaul B Schroeder serial->port[0]->interrupt_in_buffer, 10333f542974SPaul B Schroeder serial->port[0]->interrupt_in_urb-> 10343f542974SPaul B Schroeder transfer_buffer_length, 10353f542974SPaul B Schroeder mos7840_interrupt_callback, 10363f542974SPaul B Schroeder serial, 1037880af9dbSAlan Cox serial->port[0]->interrupt_in_urb->interval); 10383f542974SPaul B Schroeder 1039472d7e55SJohan Hovold /* start interrupt read for mos7840 */ 10403f542974SPaul B Schroeder response = 10413f542974SPaul B Schroeder usb_submit_urb(serial->port[0]->interrupt_in_urb, 10423f542974SPaul B Schroeder GFP_KERNEL); 10433f542974SPaul B Schroeder if (response) { 1044194343d9SGreg Kroah-Hartman dev_err(&port->dev, "%s - Error %d submitting " 1045194343d9SGreg Kroah-Hartman "interrupt urb\n", __func__, response); 10463f542974SPaul B Schroeder } 10473f542974SPaul B Schroeder 10483f542974SPaul B Schroeder } 10493f542974SPaul B Schroeder 10503f542974SPaul B Schroeder } 10513f542974SPaul B Schroeder 10523f542974SPaul B Schroeder /* see if we've set up our endpoint info yet * 10533f542974SPaul B Schroeder * (can't set it up in mos7840_startup as the * 10543f542974SPaul B Schroeder * structures were not set up at that time.) */ 10553f542974SPaul B Schroeder 10561143832eSGreg Kroah-Hartman dev_dbg(&port->dev, "port number is %d\n", port->port_number); 1057e5b1e206SGreg Kroah-Hartman dev_dbg(&port->dev, "minor number is %d\n", port->minor); 10589c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "Bulkin endpoint is %d\n", port->bulk_in_endpointAddress); 10599c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "BulkOut endpoint is %d\n", port->bulk_out_endpointAddress); 10609c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "Interrupt endpoint is %d\n", port->interrupt_in_endpointAddress); 10619c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "port's number in the device is %d\n", mos7840_port->port_num); 10623f542974SPaul B Schroeder mos7840_port->read_urb = port->read_urb; 10633f542974SPaul B Schroeder 10643f542974SPaul B Schroeder /* set up our bulk in urb */ 10651143832eSGreg Kroah-Hartman if ((serial->num_ports == 2) && (((__u16)port->port_number % 2) != 0)) { 1066093ea2d3SDonald Lee usb_fill_bulk_urb(mos7840_port->read_urb, 1067093ea2d3SDonald Lee serial->dev, 1068093ea2d3SDonald Lee usb_rcvbulkpipe(serial->dev, 1069093ea2d3SDonald Lee (port->bulk_in_endpointAddress) + 2), 1070093ea2d3SDonald Lee port->bulk_in_buffer, 1071093ea2d3SDonald Lee mos7840_port->read_urb->transfer_buffer_length, 1072093ea2d3SDonald Lee mos7840_bulk_in_callback, mos7840_port); 1073093ea2d3SDonald Lee } else { 10743f542974SPaul B Schroeder usb_fill_bulk_urb(mos7840_port->read_urb, 10753f542974SPaul B Schroeder serial->dev, 10763f542974SPaul B Schroeder usb_rcvbulkpipe(serial->dev, 10773f542974SPaul B Schroeder port->bulk_in_endpointAddress), 10783f542974SPaul B Schroeder port->bulk_in_buffer, 10793f542974SPaul B Schroeder mos7840_port->read_urb->transfer_buffer_length, 10803f542974SPaul B Schroeder mos7840_bulk_in_callback, mos7840_port); 1081093ea2d3SDonald Lee } 10823f542974SPaul B Schroeder 10839c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s: bulkin endpoint is %d\n", __func__, port->bulk_in_endpointAddress); 108450de36f7SGreg Kroah-Hartman mos7840_port->read_urb_busy = true; 10853f542974SPaul B Schroeder response = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL); 10863f542974SPaul B Schroeder if (response) { 1087194343d9SGreg Kroah-Hartman dev_err(&port->dev, "%s - Error %d submitting control urb\n", 1088194343d9SGreg Kroah-Hartman __func__, response); 108950de36f7SGreg Kroah-Hartman mos7840_port->read_urb_busy = false; 10903f542974SPaul B Schroeder } 10913f542974SPaul B Schroeder 10923f542974SPaul B Schroeder /* initialize our port settings */ 1093880af9dbSAlan Cox /* Must set to enable ints! */ 1094880af9dbSAlan Cox mos7840_port->shadowMCR = MCR_MASTER_IE; 10953f542974SPaul B Schroeder /* send a open port command */ 10963f542974SPaul B Schroeder mos7840_port->open = 1; 1097880af9dbSAlan Cox /* mos7840_change_port_settings(mos7840_port,old_termios); */ 10983f542974SPaul B Schroeder 10993f542974SPaul B Schroeder return 0; 11005f8a2e68SJohan Hovold err: 11015f8a2e68SJohan Hovold for (j = 0; j < NUM_URBS; ++j) { 11025f8a2e68SJohan Hovold urb = mos7840_port->write_urb_pool[j]; 11035f8a2e68SJohan Hovold if (!urb) 11045f8a2e68SJohan Hovold continue; 11055f8a2e68SJohan Hovold kfree(urb->transfer_buffer); 11065f8a2e68SJohan Hovold usb_free_urb(urb); 11075f8a2e68SJohan Hovold } 11085f8a2e68SJohan Hovold return status; 11093f542974SPaul B Schroeder } 11103f542974SPaul B Schroeder 11113f542974SPaul B Schroeder /***************************************************************************** 11123f542974SPaul B Schroeder * mos7840_chars_in_buffer 11133f542974SPaul B Schroeder * this function is called by the tty driver when it wants to know how many 11143f542974SPaul B Schroeder * bytes of data we currently have outstanding in the port (data that has 11153f542974SPaul B Schroeder * been written, but hasn't made it out the port yet) 11163f542974SPaul B Schroeder * If successful, we return the number of bytes left to be written in the 11173f542974SPaul B Schroeder * system, 111895da310eSAlan Cox * Otherwise we return zero. 11193f542974SPaul B Schroeder *****************************************************************************/ 11203f542974SPaul B Schroeder 112195da310eSAlan Cox static int mos7840_chars_in_buffer(struct tty_struct *tty) 11223f542974SPaul B Schroeder { 112395da310eSAlan Cox struct usb_serial_port *port = tty->driver_data; 11243f542974SPaul B Schroeder int i; 11253f542974SPaul B Schroeder int chars = 0; 11260de9a702SOliver Neukum unsigned long flags; 11273f542974SPaul B Schroeder struct moschip_port *mos7840_port; 11283f542974SPaul B Schroeder 11299c134a14SGreg Kroah-Hartman if (mos7840_port_paranoia_check(port, __func__)) 113095da310eSAlan Cox return 0; 11313f542974SPaul B Schroeder 11323f542974SPaul B Schroeder mos7840_port = mos7840_get_port_private(port); 11333363155bSGreg Kroah-Hartman if (mos7840_port == NULL) 113495da310eSAlan Cox return 0; 11353f542974SPaul B Schroeder 11360de9a702SOliver Neukum spin_lock_irqsave(&mos7840_port->pool_lock, flags); 11375c263b92SMark Ferrell for (i = 0; i < NUM_URBS; ++i) { 11385c263b92SMark Ferrell if (mos7840_port->busy[i]) { 11395c263b92SMark Ferrell struct urb *urb = mos7840_port->write_urb_pool[i]; 11405c263b92SMark Ferrell chars += urb->transfer_buffer_length; 11415c263b92SMark Ferrell } 11425c263b92SMark Ferrell } 11430de9a702SOliver Neukum spin_unlock_irqrestore(&mos7840_port->pool_lock, flags); 11449c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars); 11450de9a702SOliver Neukum return chars; 11463f542974SPaul B Schroeder 11473f542974SPaul B Schroeder } 11483f542974SPaul B Schroeder 11493f542974SPaul B Schroeder /***************************************************************************** 11503f542974SPaul B Schroeder * mos7840_close 11513f542974SPaul B Schroeder * this function is called by the tty driver when a port is closed 11523f542974SPaul B Schroeder *****************************************************************************/ 11533f542974SPaul B Schroeder 1154335f8514SAlan Cox static void mos7840_close(struct usb_serial_port *port) 11553f542974SPaul B Schroeder { 11563f542974SPaul B Schroeder struct usb_serial *serial; 11573f542974SPaul B Schroeder struct moschip_port *mos7840_port; 11580de9a702SOliver Neukum struct moschip_port *port0; 11593f542974SPaul B Schroeder int j; 11603f542974SPaul B Schroeder __u16 Data; 11613f542974SPaul B Schroeder 11629c134a14SGreg Kroah-Hartman if (mos7840_port_paranoia_check(port, __func__)) 11633f542974SPaul B Schroeder return; 11643f542974SPaul B Schroeder 1165441b62c1SHarvey Harrison serial = mos7840_get_usb_serial(port, __func__); 11669c134a14SGreg Kroah-Hartman if (!serial) 11673f542974SPaul B Schroeder return; 11683f542974SPaul B Schroeder 11693f542974SPaul B Schroeder mos7840_port = mos7840_get_port_private(port); 11700de9a702SOliver Neukum port0 = mos7840_get_port_private(serial->port[0]); 11713f542974SPaul B Schroeder 11720de9a702SOliver Neukum if (mos7840_port == NULL || port0 == NULL) 11733f542974SPaul B Schroeder return; 11743f542974SPaul B Schroeder 11753f542974SPaul B Schroeder for (j = 0; j < NUM_URBS; ++j) 11763f542974SPaul B Schroeder usb_kill_urb(mos7840_port->write_urb_pool[j]); 11773f542974SPaul B Schroeder 11783f542974SPaul B Schroeder /* Freeing Write URBs */ 11793f542974SPaul B Schroeder for (j = 0; j < NUM_URBS; ++j) { 11803f542974SPaul B Schroeder if (mos7840_port->write_urb_pool[j]) { 118191c72df1SFabian Frederick kfree(mos7840_port->write_urb_pool[j]->transfer_buffer); 11823f542974SPaul B Schroeder usb_free_urb(mos7840_port->write_urb_pool[j]); 11833f542974SPaul B Schroeder } 11843f542974SPaul B Schroeder } 11853f542974SPaul B Schroeder 11863f542974SPaul B Schroeder usb_kill_urb(mos7840_port->read_urb); 118750de36f7SGreg Kroah-Hartman mos7840_port->read_urb_busy = false; 1188bb3529c6SJohan Hovold 11890de9a702SOliver Neukum port0->open_ports--; 11901143832eSGreg Kroah-Hartman dev_dbg(&port->dev, "%s in close%d\n", __func__, port0->open_ports); 11910de9a702SOliver Neukum if (port0->open_ports == 0) { 11923f542974SPaul B Schroeder if (serial->port[0]->interrupt_in_urb) { 11939c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "Shutdown interrupt_in_urb\n"); 11940de9a702SOliver Neukum usb_kill_urb(serial->port[0]->interrupt_in_urb); 11953f542974SPaul B Schroeder } 11963f542974SPaul B Schroeder } 11973f542974SPaul B Schroeder 11983f542974SPaul B Schroeder Data = 0x0; 11993f542974SPaul B Schroeder mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data); 12003f542974SPaul B Schroeder 12013f542974SPaul B Schroeder Data = 0x00; 12023f542974SPaul B Schroeder mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data); 12033f542974SPaul B Schroeder 12043f542974SPaul B Schroeder mos7840_port->open = 0; 12053f542974SPaul B Schroeder } 12063f542974SPaul B Schroeder 12073f542974SPaul B Schroeder /***************************************************************************** 12083f542974SPaul B Schroeder * mos7840_break 12093f542974SPaul B Schroeder * this function sends a break to the port 12103f542974SPaul B Schroeder *****************************************************************************/ 121195da310eSAlan Cox static void mos7840_break(struct tty_struct *tty, int break_state) 12123f542974SPaul B Schroeder { 121395da310eSAlan Cox struct usb_serial_port *port = tty->driver_data; 12143f542974SPaul B Schroeder unsigned char data; 12153f542974SPaul B Schroeder struct usb_serial *serial; 12163f542974SPaul B Schroeder struct moschip_port *mos7840_port; 12173f542974SPaul B Schroeder 12189c134a14SGreg Kroah-Hartman if (mos7840_port_paranoia_check(port, __func__)) 12193f542974SPaul B Schroeder return; 12203f542974SPaul B Schroeder 1221441b62c1SHarvey Harrison serial = mos7840_get_usb_serial(port, __func__); 12229c134a14SGreg Kroah-Hartman if (!serial) 12233f542974SPaul B Schroeder return; 12243f542974SPaul B Schroeder 12253f542974SPaul B Schroeder mos7840_port = mos7840_get_port_private(port); 12263f542974SPaul B Schroeder 1227880af9dbSAlan Cox if (mos7840_port == NULL) 12283f542974SPaul B Schroeder return; 12293f542974SPaul B Schroeder 123095da310eSAlan Cox if (break_state == -1) 12313f542974SPaul B Schroeder data = mos7840_port->shadowLCR | LCR_SET_BREAK; 123295da310eSAlan Cox else 12333f542974SPaul B Schroeder data = mos7840_port->shadowLCR & ~LCR_SET_BREAK; 12343f542974SPaul B Schroeder 12356b447f04SAlan Cox /* FIXME: no locking on shadowLCR anywhere in driver */ 12363f542974SPaul B Schroeder mos7840_port->shadowLCR = data; 12379c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s mos7840_port->shadowLCR is %x\n", __func__, mos7840_port->shadowLCR); 12383f542974SPaul B Schroeder mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, 12393f542974SPaul B Schroeder mos7840_port->shadowLCR); 12403f542974SPaul B Schroeder } 12413f542974SPaul B Schroeder 12423f542974SPaul B Schroeder /***************************************************************************** 12433f542974SPaul B Schroeder * mos7840_write_room 12443f542974SPaul B Schroeder * this function is called by the tty driver when it wants to know how many 12453f542974SPaul B Schroeder * bytes of data we can accept for a specific port. 12463f542974SPaul B Schroeder * If successful, we return the amount of room that we have for this port 12473f542974SPaul B Schroeder * Otherwise we return a negative error number. 12483f542974SPaul B Schroeder *****************************************************************************/ 12493f542974SPaul B Schroeder 125095da310eSAlan Cox static int mos7840_write_room(struct tty_struct *tty) 12513f542974SPaul B Schroeder { 125295da310eSAlan Cox struct usb_serial_port *port = tty->driver_data; 12533f542974SPaul B Schroeder int i; 12543f542974SPaul B Schroeder int room = 0; 12550de9a702SOliver Neukum unsigned long flags; 12563f542974SPaul B Schroeder struct moschip_port *mos7840_port; 12573f542974SPaul B Schroeder 12589c134a14SGreg Kroah-Hartman if (mos7840_port_paranoia_check(port, __func__)) 12593f542974SPaul B Schroeder return -1; 12603f542974SPaul B Schroeder 12613f542974SPaul B Schroeder mos7840_port = mos7840_get_port_private(port); 12629c134a14SGreg Kroah-Hartman if (mos7840_port == NULL) 12633f542974SPaul B Schroeder return -1; 12643f542974SPaul B Schroeder 12650de9a702SOliver Neukum spin_lock_irqsave(&mos7840_port->pool_lock, flags); 12663f542974SPaul B Schroeder for (i = 0; i < NUM_URBS; ++i) { 1267880af9dbSAlan Cox if (!mos7840_port->busy[i]) 12683f542974SPaul B Schroeder room += URB_TRANSFER_BUFFER_SIZE; 12693f542974SPaul B Schroeder } 12700de9a702SOliver Neukum spin_unlock_irqrestore(&mos7840_port->pool_lock, flags); 12713f542974SPaul B Schroeder 12720de9a702SOliver Neukum room = (room == 0) ? 0 : room - URB_TRANSFER_BUFFER_SIZE + 1; 12739c134a14SGreg Kroah-Hartman dev_dbg(&mos7840_port->port->dev, "%s - returns %d\n", __func__, room); 12740de9a702SOliver Neukum return room; 12753f542974SPaul B Schroeder 12763f542974SPaul B Schroeder } 12773f542974SPaul B Schroeder 12783f542974SPaul B Schroeder /***************************************************************************** 12793f542974SPaul B Schroeder * mos7840_write 12803f542974SPaul B Schroeder * this function is called by the tty driver when data should be written to 12813f542974SPaul B Schroeder * the port. 12823f542974SPaul B Schroeder * If successful, we return the number of bytes written, otherwise we 12833f542974SPaul B Schroeder * return a negative error number. 12843f542974SPaul B Schroeder *****************************************************************************/ 12853f542974SPaul B Schroeder 128695da310eSAlan Cox static int mos7840_write(struct tty_struct *tty, struct usb_serial_port *port, 12873f542974SPaul B Schroeder const unsigned char *data, int count) 12883f542974SPaul B Schroeder { 12893f542974SPaul B Schroeder int status; 12903f542974SPaul B Schroeder int i; 12913f542974SPaul B Schroeder int bytes_sent = 0; 12923f542974SPaul B Schroeder int transfer_size; 12930de9a702SOliver Neukum unsigned long flags; 12943f542974SPaul B Schroeder 12953f542974SPaul B Schroeder struct moschip_port *mos7840_port; 12963f542974SPaul B Schroeder struct usb_serial *serial; 12973f542974SPaul B Schroeder struct urb *urb; 1298880af9dbSAlan Cox /* __u16 Data; */ 12993f542974SPaul B Schroeder const unsigned char *current_position = data; 13003f542974SPaul B Schroeder unsigned char *data1; 13013f542974SPaul B Schroeder 13029c134a14SGreg Kroah-Hartman if (mos7840_port_paranoia_check(port, __func__)) 13033f542974SPaul B Schroeder return -1; 13043f542974SPaul B Schroeder 13053f542974SPaul B Schroeder serial = port->serial; 13069c134a14SGreg Kroah-Hartman if (mos7840_serial_paranoia_check(serial, __func__)) 13073f542974SPaul B Schroeder return -1; 13083f542974SPaul B Schroeder 13093f542974SPaul B Schroeder mos7840_port = mos7840_get_port_private(port); 13109c134a14SGreg Kroah-Hartman if (mos7840_port == NULL) 13113f542974SPaul B Schroeder return -1; 13123f542974SPaul B Schroeder 13133f542974SPaul B Schroeder /* try to find a free urb in the list */ 13143f542974SPaul B Schroeder urb = NULL; 13153f542974SPaul B Schroeder 13160de9a702SOliver Neukum spin_lock_irqsave(&mos7840_port->pool_lock, flags); 13173f542974SPaul B Schroeder for (i = 0; i < NUM_URBS; ++i) { 13180de9a702SOliver Neukum if (!mos7840_port->busy[i]) { 13190de9a702SOliver Neukum mos7840_port->busy[i] = 1; 13203f542974SPaul B Schroeder urb = mos7840_port->write_urb_pool[i]; 13219c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "URB:%d\n", i); 13223f542974SPaul B Schroeder break; 13233f542974SPaul B Schroeder } 13243f542974SPaul B Schroeder } 13250de9a702SOliver Neukum spin_unlock_irqrestore(&mos7840_port->pool_lock, flags); 13263f542974SPaul B Schroeder 13273f542974SPaul B Schroeder if (urb == NULL) { 13289c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - no more free urbs\n", __func__); 13293f542974SPaul B Schroeder goto exit; 13303f542974SPaul B Schroeder } 13313f542974SPaul B Schroeder 13323f542974SPaul B Schroeder if (urb->transfer_buffer == NULL) { 13333b7c7e52SAlexey Khoroshilov urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE, 13343b7c7e52SAlexey Khoroshilov GFP_ATOMIC); 133510c642d0SJohan Hovold if (!urb->transfer_buffer) 13363f542974SPaul B Schroeder goto exit; 13373f542974SPaul B Schroeder } 13383f542974SPaul B Schroeder transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE); 13393f542974SPaul B Schroeder 13403f542974SPaul B Schroeder memcpy(urb->transfer_buffer, current_position, transfer_size); 13413f542974SPaul B Schroeder 13423f542974SPaul B Schroeder /* fill urb with data and submit */ 13431143832eSGreg Kroah-Hartman if ((serial->num_ports == 2) && (((__u16)port->port_number % 2) != 0)) { 1344093ea2d3SDonald Lee usb_fill_bulk_urb(urb, 1345093ea2d3SDonald Lee serial->dev, 1346093ea2d3SDonald Lee usb_sndbulkpipe(serial->dev, 1347093ea2d3SDonald Lee (port->bulk_out_endpointAddress) + 2), 1348093ea2d3SDonald Lee urb->transfer_buffer, 1349093ea2d3SDonald Lee transfer_size, 1350093ea2d3SDonald Lee mos7840_bulk_out_data_callback, mos7840_port); 1351093ea2d3SDonald Lee } else { 13523f542974SPaul B Schroeder usb_fill_bulk_urb(urb, 13533f542974SPaul B Schroeder serial->dev, 13543f542974SPaul B Schroeder usb_sndbulkpipe(serial->dev, 13553f542974SPaul B Schroeder port->bulk_out_endpointAddress), 13563f542974SPaul B Schroeder urb->transfer_buffer, 13573f542974SPaul B Schroeder transfer_size, 13583f542974SPaul B Schroeder mos7840_bulk_out_data_callback, mos7840_port); 1359093ea2d3SDonald Lee } 13603f542974SPaul B Schroeder 13613f542974SPaul B Schroeder data1 = urb->transfer_buffer; 13629c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "bulkout endpoint is %d\n", port->bulk_out_endpointAddress); 13633f542974SPaul B Schroeder 136405cf0decSJohan Hovold if (mos7840_port->has_led) 136505cf0decSJohan Hovold mos7840_led_activity(port); 13660eafe4deSDonald 13673f542974SPaul B Schroeder /* send it down the pipe */ 13683f542974SPaul B Schroeder status = usb_submit_urb(urb, GFP_ATOMIC); 13693f542974SPaul B Schroeder 13703f542974SPaul B Schroeder if (status) { 13710de9a702SOliver Neukum mos7840_port->busy[i] = 0; 137222a416c4SJohan Hovold dev_err_console(port, "%s - usb_submit_urb(write bulk) failed " 1373194343d9SGreg Kroah-Hartman "with status = %d\n", __func__, status); 13743f542974SPaul B Schroeder bytes_sent = status; 13753f542974SPaul B Schroeder goto exit; 13763f542974SPaul B Schroeder } 13773f542974SPaul B Schroeder bytes_sent = transfer_size; 13788c1a07ffSJohan Hovold port->icount.tx += transfer_size; 13798c1a07ffSJohan Hovold dev_dbg(&port->dev, "icount.tx is %d:\n", port->icount.tx); 13803f542974SPaul B Schroeder exit: 13813f542974SPaul B Schroeder return bytes_sent; 13823f542974SPaul B Schroeder 13833f542974SPaul B Schroeder } 13843f542974SPaul B Schroeder 13853f542974SPaul B Schroeder /***************************************************************************** 13863f542974SPaul B Schroeder * mos7840_throttle 13873f542974SPaul B Schroeder * this function is called by the tty driver when it wants to stop the data 13883f542974SPaul B Schroeder * being read from the port. 13893f542974SPaul B Schroeder *****************************************************************************/ 13903f542974SPaul B Schroeder 139195da310eSAlan Cox static void mos7840_throttle(struct tty_struct *tty) 13923f542974SPaul B Schroeder { 139395da310eSAlan Cox struct usb_serial_port *port = tty->driver_data; 13943f542974SPaul B Schroeder struct moschip_port *mos7840_port; 13953f542974SPaul B Schroeder int status; 13963f542974SPaul B Schroeder 13979c134a14SGreg Kroah-Hartman if (mos7840_port_paranoia_check(port, __func__)) 13983f542974SPaul B Schroeder return; 13993f542974SPaul B Schroeder 14003f542974SPaul B Schroeder mos7840_port = mos7840_get_port_private(port); 14013f542974SPaul B Schroeder 14023f542974SPaul B Schroeder if (mos7840_port == NULL) 14033f542974SPaul B Schroeder return; 14043f542974SPaul B Schroeder 14053f542974SPaul B Schroeder if (!mos7840_port->open) { 14069c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s", "port not opened\n"); 14073f542974SPaul B Schroeder return; 14083f542974SPaul B Schroeder } 14093f542974SPaul B Schroeder 14103f542974SPaul B Schroeder /* if we are implementing XON/XOFF, send the stop character */ 14113f542974SPaul B Schroeder if (I_IXOFF(tty)) { 14123f542974SPaul B Schroeder unsigned char stop_char = STOP_CHAR(tty); 141395da310eSAlan Cox status = mos7840_write(tty, port, &stop_char, 1); 141495da310eSAlan Cox if (status <= 0) 14153f542974SPaul B Schroeder return; 14163f542974SPaul B Schroeder } 14173f542974SPaul B Schroeder /* if we are implementing RTS/CTS, toggle that line */ 14189db276f8SPeter Hurley if (C_CRTSCTS(tty)) { 14193f542974SPaul B Schroeder mos7840_port->shadowMCR &= ~MCR_RTS; 142095da310eSAlan Cox status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, 14213f542974SPaul B Schroeder mos7840_port->shadowMCR); 142295da310eSAlan Cox if (status < 0) 14233f542974SPaul B Schroeder return; 14243f542974SPaul B Schroeder } 14253f542974SPaul B Schroeder } 14263f542974SPaul B Schroeder 14273f542974SPaul B Schroeder /***************************************************************************** 14283f542974SPaul B Schroeder * mos7840_unthrottle 1429880af9dbSAlan Cox * this function is called by the tty driver when it wants to resume 1430880af9dbSAlan Cox * the data being read from the port (called after mos7840_throttle is 1431880af9dbSAlan Cox * called) 14323f542974SPaul B Schroeder *****************************************************************************/ 143395da310eSAlan Cox static void mos7840_unthrottle(struct tty_struct *tty) 14343f542974SPaul B Schroeder { 143595da310eSAlan Cox struct usb_serial_port *port = tty->driver_data; 14363f542974SPaul B Schroeder int status; 14373f542974SPaul B Schroeder struct moschip_port *mos7840_port = mos7840_get_port_private(port); 14383f542974SPaul B Schroeder 14399c134a14SGreg Kroah-Hartman if (mos7840_port_paranoia_check(port, __func__)) 14403f542974SPaul B Schroeder return; 14413f542974SPaul B Schroeder 14423f542974SPaul B Schroeder if (mos7840_port == NULL) 14433f542974SPaul B Schroeder return; 14443f542974SPaul B Schroeder 14453f542974SPaul B Schroeder if (!mos7840_port->open) { 14469c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - port not opened\n", __func__); 14473f542974SPaul B Schroeder return; 14483f542974SPaul B Schroeder } 14493f542974SPaul B Schroeder 14503f542974SPaul B Schroeder /* if we are implementing XON/XOFF, send the start character */ 14513f542974SPaul B Schroeder if (I_IXOFF(tty)) { 14523f542974SPaul B Schroeder unsigned char start_char = START_CHAR(tty); 145395da310eSAlan Cox status = mos7840_write(tty, port, &start_char, 1); 145495da310eSAlan Cox if (status <= 0) 14553f542974SPaul B Schroeder return; 14563f542974SPaul B Schroeder } 14573f542974SPaul B Schroeder 14583f542974SPaul B Schroeder /* if we are implementing RTS/CTS, toggle that line */ 14599db276f8SPeter Hurley if (C_CRTSCTS(tty)) { 14603f542974SPaul B Schroeder mos7840_port->shadowMCR |= MCR_RTS; 146195da310eSAlan Cox status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, 14623f542974SPaul B Schroeder mos7840_port->shadowMCR); 146395da310eSAlan Cox if (status < 0) 14643f542974SPaul B Schroeder return; 14653f542974SPaul B Schroeder } 14663f542974SPaul B Schroeder } 14673f542974SPaul B Schroeder 146860b33c13SAlan Cox static int mos7840_tiocmget(struct tty_struct *tty) 14693f542974SPaul B Schroeder { 147095da310eSAlan Cox struct usb_serial_port *port = tty->driver_data; 14713f542974SPaul B Schroeder struct moschip_port *mos7840_port; 14723f542974SPaul B Schroeder unsigned int result; 14733f542974SPaul B Schroeder __u16 msr; 14743f542974SPaul B Schroeder __u16 mcr; 1475880af9dbSAlan Cox int status; 14763f542974SPaul B Schroeder mos7840_port = mos7840_get_port_private(port); 14773f542974SPaul B Schroeder 14783f542974SPaul B Schroeder if (mos7840_port == NULL) 14793f542974SPaul B Schroeder return -ENODEV; 14803f542974SPaul B Schroeder 14813f542974SPaul B Schroeder status = mos7840_get_uart_reg(port, MODEM_STATUS_REGISTER, &msr); 1482cd8db057SJohan Hovold if (status < 0) 1483a91ccd26SJohan Hovold return -EIO; 14843f542974SPaul B Schroeder status = mos7840_get_uart_reg(port, MODEM_CONTROL_REGISTER, &mcr); 1485cd8db057SJohan Hovold if (status < 0) 1486a91ccd26SJohan Hovold return -EIO; 14873f542974SPaul B Schroeder result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0) 14883f542974SPaul B Schroeder | ((mcr & MCR_RTS) ? TIOCM_RTS : 0) 14893f542974SPaul B Schroeder | ((mcr & MCR_LOOPBACK) ? TIOCM_LOOP : 0) 14903f542974SPaul B Schroeder | ((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0) 14913f542974SPaul B Schroeder | ((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0) 14923f542974SPaul B Schroeder | ((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0) 14933f542974SPaul B Schroeder | ((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0); 14943f542974SPaul B Schroeder 14959c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - 0x%04X\n", __func__, result); 14963f542974SPaul B Schroeder 14973f542974SPaul B Schroeder return result; 14983f542974SPaul B Schroeder } 14993f542974SPaul B Schroeder 150020b9d177SAlan Cox static int mos7840_tiocmset(struct tty_struct *tty, 15013f542974SPaul B Schroeder unsigned int set, unsigned int clear) 15023f542974SPaul B Schroeder { 150395da310eSAlan Cox struct usb_serial_port *port = tty->driver_data; 15043f542974SPaul B Schroeder struct moschip_port *mos7840_port; 15053f542974SPaul B Schroeder unsigned int mcr; 150687521c46SRoel Kluin int status; 15073f542974SPaul B Schroeder 15083f542974SPaul B Schroeder mos7840_port = mos7840_get_port_private(port); 15093f542974SPaul B Schroeder 15103f542974SPaul B Schroeder if (mos7840_port == NULL) 15113f542974SPaul B Schroeder return -ENODEV; 15123f542974SPaul B Schroeder 1513e2984494SAlan Cox /* FIXME: What locks the port registers ? */ 15143f542974SPaul B Schroeder mcr = mos7840_port->shadowMCR; 15153f542974SPaul B Schroeder if (clear & TIOCM_RTS) 15163f542974SPaul B Schroeder mcr &= ~MCR_RTS; 15173f542974SPaul B Schroeder if (clear & TIOCM_DTR) 15183f542974SPaul B Schroeder mcr &= ~MCR_DTR; 15193f542974SPaul B Schroeder if (clear & TIOCM_LOOP) 15203f542974SPaul B Schroeder mcr &= ~MCR_LOOPBACK; 15213f542974SPaul B Schroeder 15223f542974SPaul B Schroeder if (set & TIOCM_RTS) 15233f542974SPaul B Schroeder mcr |= MCR_RTS; 15243f542974SPaul B Schroeder if (set & TIOCM_DTR) 15253f542974SPaul B Schroeder mcr |= MCR_DTR; 15263f542974SPaul B Schroeder if (set & TIOCM_LOOP) 15273f542974SPaul B Schroeder mcr |= MCR_LOOPBACK; 15283f542974SPaul B Schroeder 15293f542974SPaul B Schroeder mos7840_port->shadowMCR = mcr; 15303f542974SPaul B Schroeder 15313f542974SPaul B Schroeder status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, mcr); 15323f542974SPaul B Schroeder if (status < 0) { 15339c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "setting MODEM_CONTROL_REGISTER Failed\n"); 153487521c46SRoel Kluin return status; 15353f542974SPaul B Schroeder } 15363f542974SPaul B Schroeder 15373f542974SPaul B Schroeder return 0; 15383f542974SPaul B Schroeder } 15393f542974SPaul B Schroeder 15403f542974SPaul B Schroeder /***************************************************************************** 15413f542974SPaul B Schroeder * mos7840_calc_baud_rate_divisor 15423f542974SPaul B Schroeder * this function calculates the proper baud rate divisor for the specified 15433f542974SPaul B Schroeder * baud rate. 15443f542974SPaul B Schroeder *****************************************************************************/ 15459c134a14SGreg Kroah-Hartman static int mos7840_calc_baud_rate_divisor(struct usb_serial_port *port, 15469c134a14SGreg Kroah-Hartman int baudRate, int *divisor, 15473f542974SPaul B Schroeder __u16 *clk_sel_val) 15483f542974SPaul B Schroeder { 15499c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - %d\n", __func__, baudRate); 15503f542974SPaul B Schroeder 15513f542974SPaul B Schroeder if (baudRate <= 115200) { 15523f542974SPaul B Schroeder *divisor = 115200 / baudRate; 15533f542974SPaul B Schroeder *clk_sel_val = 0x0; 15543f542974SPaul B Schroeder } 15553f542974SPaul B Schroeder if ((baudRate > 115200) && (baudRate <= 230400)) { 15563f542974SPaul B Schroeder *divisor = 230400 / baudRate; 15573f542974SPaul B Schroeder *clk_sel_val = 0x10; 15583f542974SPaul B Schroeder } else if ((baudRate > 230400) && (baudRate <= 403200)) { 15593f542974SPaul B Schroeder *divisor = 403200 / baudRate; 15603f542974SPaul B Schroeder *clk_sel_val = 0x20; 15613f542974SPaul B Schroeder } else if ((baudRate > 403200) && (baudRate <= 460800)) { 15623f542974SPaul B Schroeder *divisor = 460800 / baudRate; 15633f542974SPaul B Schroeder *clk_sel_val = 0x30; 15643f542974SPaul B Schroeder } else if ((baudRate > 460800) && (baudRate <= 806400)) { 15653f542974SPaul B Schroeder *divisor = 806400 / baudRate; 15663f542974SPaul B Schroeder *clk_sel_val = 0x40; 15673f542974SPaul B Schroeder } else if ((baudRate > 806400) && (baudRate <= 921600)) { 15683f542974SPaul B Schroeder *divisor = 921600 / baudRate; 15693f542974SPaul B Schroeder *clk_sel_val = 0x50; 15703f542974SPaul B Schroeder } else if ((baudRate > 921600) && (baudRate <= 1572864)) { 15713f542974SPaul B Schroeder *divisor = 1572864 / baudRate; 15723f542974SPaul B Schroeder *clk_sel_val = 0x60; 15733f542974SPaul B Schroeder } else if ((baudRate > 1572864) && (baudRate <= 3145728)) { 15743f542974SPaul B Schroeder *divisor = 3145728 / baudRate; 15753f542974SPaul B Schroeder *clk_sel_val = 0x70; 15763f542974SPaul B Schroeder } 15773f542974SPaul B Schroeder return 0; 15783f542974SPaul B Schroeder } 15793f542974SPaul B Schroeder 15803f542974SPaul B Schroeder /***************************************************************************** 15813f542974SPaul B Schroeder * mos7840_send_cmd_write_baud_rate 15823f542974SPaul B Schroeder * this function sends the proper command to change the baud rate of the 15833f542974SPaul B Schroeder * specified port. 15843f542974SPaul B Schroeder *****************************************************************************/ 15853f542974SPaul B Schroeder 15863f542974SPaul B Schroeder static int mos7840_send_cmd_write_baud_rate(struct moschip_port *mos7840_port, 15873f542974SPaul B Schroeder int baudRate) 15883f542974SPaul B Schroeder { 15893f542974SPaul B Schroeder int divisor = 0; 15903f542974SPaul B Schroeder int status; 15913f542974SPaul B Schroeder __u16 Data; 15923f542974SPaul B Schroeder unsigned char number; 15933f542974SPaul B Schroeder __u16 clk_sel_val; 15943f542974SPaul B Schroeder struct usb_serial_port *port; 15953f542974SPaul B Schroeder 15963f542974SPaul B Schroeder if (mos7840_port == NULL) 15973f542974SPaul B Schroeder return -1; 15983f542974SPaul B Schroeder 15999c134a14SGreg Kroah-Hartman port = mos7840_port->port; 16009c134a14SGreg Kroah-Hartman if (mos7840_port_paranoia_check(port, __func__)) 16013f542974SPaul B Schroeder return -1; 16023f542974SPaul B Schroeder 16039c134a14SGreg Kroah-Hartman if (mos7840_serial_paranoia_check(port->serial, __func__)) 16043f542974SPaul B Schroeder return -1; 16053f542974SPaul B Schroeder 16061143832eSGreg Kroah-Hartman number = mos7840_port->port->port_number; 16073f542974SPaul B Schroeder 16081143832eSGreg Kroah-Hartman dev_dbg(&port->dev, "%s - baud = %d\n", __func__, baudRate); 1609880af9dbSAlan Cox /* reset clk_uart_sel in spregOffset */ 16103f542974SPaul B Schroeder if (baudRate > 115200) { 16113f542974SPaul B Schroeder #ifdef HW_flow_control 1612880af9dbSAlan Cox /* NOTE: need to see the pther register to modify */ 1613880af9dbSAlan Cox /* setting h/w flow control bit to 1 */ 16143f542974SPaul B Schroeder Data = 0x2b; 16153f542974SPaul B Schroeder mos7840_port->shadowMCR = Data; 1616880af9dbSAlan Cox status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, 1617880af9dbSAlan Cox Data); 16183f542974SPaul B Schroeder if (status < 0) { 16199c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n"); 16203f542974SPaul B Schroeder return -1; 16213f542974SPaul B Schroeder } 16223f542974SPaul B Schroeder #endif 16233f542974SPaul B Schroeder 16243f542974SPaul B Schroeder } else { 16253f542974SPaul B Schroeder #ifdef HW_flow_control 1626880af9dbSAlan Cox /* setting h/w flow control bit to 0 */ 16273f542974SPaul B Schroeder Data = 0xb; 16283f542974SPaul B Schroeder mos7840_port->shadowMCR = Data; 1629880af9dbSAlan Cox status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, 1630880af9dbSAlan Cox Data); 16313f542974SPaul B Schroeder if (status < 0) { 16329c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n"); 16333f542974SPaul B Schroeder return -1; 16343f542974SPaul B Schroeder } 16353f542974SPaul B Schroeder #endif 16363f542974SPaul B Schroeder 16373f542974SPaul B Schroeder } 16383f542974SPaul B Schroeder 1639880af9dbSAlan Cox if (1) { /* baudRate <= 115200) */ 16403f542974SPaul B Schroeder clk_sel_val = 0x0; 16413f542974SPaul B Schroeder Data = 0x0; 16429c134a14SGreg Kroah-Hartman status = mos7840_calc_baud_rate_divisor(port, baudRate, &divisor, 16433f542974SPaul B Schroeder &clk_sel_val); 1644880af9dbSAlan Cox status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, 16453f542974SPaul B Schroeder &Data); 16463f542974SPaul B Schroeder if (status < 0) { 16479c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "reading spreg failed in set_serial_baud\n"); 16483f542974SPaul B Schroeder return -1; 16493f542974SPaul B Schroeder } 16503f542974SPaul B Schroeder Data = (Data & 0x8f) | clk_sel_val; 1651880af9dbSAlan Cox status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, 1652880af9dbSAlan Cox Data); 16533f542974SPaul B Schroeder if (status < 0) { 16549c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n"); 16553f542974SPaul B Schroeder return -1; 16563f542974SPaul B Schroeder } 16573f542974SPaul B Schroeder /* Calculate the Divisor */ 16583f542974SPaul B Schroeder 16593f542974SPaul B Schroeder if (status) { 1660194343d9SGreg Kroah-Hartman dev_err(&port->dev, "%s - bad baud rate\n", __func__); 16613f542974SPaul B Schroeder return status; 16623f542974SPaul B Schroeder } 16633f542974SPaul B Schroeder /* Enable access to divisor latch */ 16643f542974SPaul B Schroeder Data = mos7840_port->shadowLCR | SERIAL_LCR_DLAB; 16653f542974SPaul B Schroeder mos7840_port->shadowLCR = Data; 16663f542974SPaul B Schroeder mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data); 16673f542974SPaul B Schroeder 16683f542974SPaul B Schroeder /* Write the divisor */ 16693f542974SPaul B Schroeder Data = (unsigned char)(divisor & 0xff); 16709c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "set_serial_baud Value to write DLL is %x\n", Data); 16713f542974SPaul B Schroeder mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data); 16723f542974SPaul B Schroeder 16733f542974SPaul B Schroeder Data = (unsigned char)((divisor & 0xff00) >> 8); 16749c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "set_serial_baud Value to write DLM is %x\n", Data); 16753f542974SPaul B Schroeder mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data); 16763f542974SPaul B Schroeder 16773f542974SPaul B Schroeder /* Disable access to divisor latch */ 16783f542974SPaul B Schroeder Data = mos7840_port->shadowLCR & ~SERIAL_LCR_DLAB; 16793f542974SPaul B Schroeder mos7840_port->shadowLCR = Data; 16803f542974SPaul B Schroeder mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data); 16813f542974SPaul B Schroeder 16823f542974SPaul B Schroeder } 16833f542974SPaul B Schroeder return status; 16843f542974SPaul B Schroeder } 16853f542974SPaul B Schroeder 16863f542974SPaul B Schroeder /***************************************************************************** 16873f542974SPaul B Schroeder * mos7840_change_port_settings 16883f542974SPaul B Schroeder * This routine is called to set the UART on the device to match 16893f542974SPaul B Schroeder * the specified new settings. 16903f542974SPaul B Schroeder *****************************************************************************/ 16913f542974SPaul B Schroeder 169295da310eSAlan Cox static void mos7840_change_port_settings(struct tty_struct *tty, 169395da310eSAlan Cox struct moschip_port *mos7840_port, struct ktermios *old_termios) 16943f542974SPaul B Schroeder { 16953f542974SPaul B Schroeder int baud; 16963f542974SPaul B Schroeder unsigned cflag; 16973f542974SPaul B Schroeder unsigned iflag; 16983f542974SPaul B Schroeder __u8 lData; 16993f542974SPaul B Schroeder __u8 lParity; 17003f542974SPaul B Schroeder __u8 lStop; 17013f542974SPaul B Schroeder int status; 17023f542974SPaul B Schroeder __u16 Data; 17033f542974SPaul B Schroeder struct usb_serial_port *port; 17043f542974SPaul B Schroeder struct usb_serial *serial; 17053f542974SPaul B Schroeder 17063f542974SPaul B Schroeder if (mos7840_port == NULL) 17073f542974SPaul B Schroeder return; 17083f542974SPaul B Schroeder 17099c134a14SGreg Kroah-Hartman port = mos7840_port->port; 17103f542974SPaul B Schroeder 17119c134a14SGreg Kroah-Hartman if (mos7840_port_paranoia_check(port, __func__)) 17123f542974SPaul B Schroeder return; 17133f542974SPaul B Schroeder 17149c134a14SGreg Kroah-Hartman if (mos7840_serial_paranoia_check(port->serial, __func__)) 17153f542974SPaul B Schroeder return; 17163f542974SPaul B Schroeder 17173f542974SPaul B Schroeder serial = port->serial; 17183f542974SPaul B Schroeder 17193f542974SPaul B Schroeder if (!mos7840_port->open) { 17209c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - port not opened\n", __func__); 17213f542974SPaul B Schroeder return; 17223f542974SPaul B Schroeder } 17233f542974SPaul B Schroeder 17243f542974SPaul B Schroeder lData = LCR_BITS_8; 17253f542974SPaul B Schroeder lStop = LCR_STOP_1; 17263f542974SPaul B Schroeder lParity = LCR_PAR_NONE; 17273f542974SPaul B Schroeder 1728adc8d746SAlan Cox cflag = tty->termios.c_cflag; 1729adc8d746SAlan Cox iflag = tty->termios.c_iflag; 17303f542974SPaul B Schroeder 17313f542974SPaul B Schroeder /* Change the number of bits */ 17323f542974SPaul B Schroeder switch (cflag & CSIZE) { 17333f542974SPaul B Schroeder case CS5: 17343f542974SPaul B Schroeder lData = LCR_BITS_5; 17353f542974SPaul B Schroeder break; 17363f542974SPaul B Schroeder 17373f542974SPaul B Schroeder case CS6: 17383f542974SPaul B Schroeder lData = LCR_BITS_6; 17393f542974SPaul B Schroeder break; 17403f542974SPaul B Schroeder 17413f542974SPaul B Schroeder case CS7: 17423f542974SPaul B Schroeder lData = LCR_BITS_7; 17433f542974SPaul B Schroeder break; 174478692cc3SColin Leitner 17453f542974SPaul B Schroeder default: 17463f542974SPaul B Schroeder case CS8: 17473f542974SPaul B Schroeder lData = LCR_BITS_8; 17483f542974SPaul B Schroeder break; 17493f542974SPaul B Schroeder } 175078692cc3SColin Leitner 17513f542974SPaul B Schroeder /* Change the Parity bit */ 17523f542974SPaul B Schroeder if (cflag & PARENB) { 17533f542974SPaul B Schroeder if (cflag & PARODD) { 17543f542974SPaul B Schroeder lParity = LCR_PAR_ODD; 17559c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - parity = odd\n", __func__); 17563f542974SPaul B Schroeder } else { 17573f542974SPaul B Schroeder lParity = LCR_PAR_EVEN; 17589c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - parity = even\n", __func__); 17593f542974SPaul B Schroeder } 17603f542974SPaul B Schroeder 17613f542974SPaul B Schroeder } else { 17629c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - parity = none\n", __func__); 17633f542974SPaul B Schroeder } 17643f542974SPaul B Schroeder 1765880af9dbSAlan Cox if (cflag & CMSPAR) 17663f542974SPaul B Schroeder lParity = lParity | 0x20; 17673f542974SPaul B Schroeder 17683f542974SPaul B Schroeder /* Change the Stop bit */ 17693f542974SPaul B Schroeder if (cflag & CSTOPB) { 17703f542974SPaul B Schroeder lStop = LCR_STOP_2; 17719c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - stop bits = 2\n", __func__); 17723f542974SPaul B Schroeder } else { 17733f542974SPaul B Schroeder lStop = LCR_STOP_1; 17749c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - stop bits = 1\n", __func__); 17753f542974SPaul B Schroeder } 17763f542974SPaul B Schroeder 17773f542974SPaul B Schroeder /* Update the LCR with the correct value */ 17783f542974SPaul B Schroeder mos7840_port->shadowLCR &= 17793f542974SPaul B Schroeder ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK); 17803f542974SPaul B Schroeder mos7840_port->shadowLCR |= (lData | lParity | lStop); 17813f542974SPaul B Schroeder 17829c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - mos7840_port->shadowLCR is %x\n", __func__, 17833f542974SPaul B Schroeder mos7840_port->shadowLCR); 17843f542974SPaul B Schroeder /* Disable Interrupts */ 17853f542974SPaul B Schroeder Data = 0x00; 17863f542974SPaul B Schroeder mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data); 17873f542974SPaul B Schroeder 17883f542974SPaul B Schroeder Data = 0x00; 17893f542974SPaul B Schroeder mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data); 17903f542974SPaul B Schroeder 17913f542974SPaul B Schroeder Data = 0xcf; 17923f542974SPaul B Schroeder mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data); 17933f542974SPaul B Schroeder 17943f542974SPaul B Schroeder /* Send the updated LCR value to the mos7840 */ 17953f542974SPaul B Schroeder Data = mos7840_port->shadowLCR; 17963f542974SPaul B Schroeder 17973f542974SPaul B Schroeder mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data); 17983f542974SPaul B Schroeder 17993f542974SPaul B Schroeder Data = 0x00b; 18003f542974SPaul B Schroeder mos7840_port->shadowMCR = Data; 18013f542974SPaul B Schroeder mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data); 18023f542974SPaul B Schroeder Data = 0x00b; 18033f542974SPaul B Schroeder mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data); 18043f542974SPaul B Schroeder 18053f542974SPaul B Schroeder /* set up the MCR register and send it to the mos7840 */ 18063f542974SPaul B Schroeder 18073f542974SPaul B Schroeder mos7840_port->shadowMCR = MCR_MASTER_IE; 1808880af9dbSAlan Cox if (cflag & CBAUD) 18093f542974SPaul B Schroeder mos7840_port->shadowMCR |= (MCR_DTR | MCR_RTS); 18103f542974SPaul B Schroeder 1811880af9dbSAlan Cox if (cflag & CRTSCTS) 18123f542974SPaul B Schroeder mos7840_port->shadowMCR |= (MCR_XON_ANY); 1813880af9dbSAlan Cox else 18143f542974SPaul B Schroeder mos7840_port->shadowMCR &= ~(MCR_XON_ANY); 18153f542974SPaul B Schroeder 18163f542974SPaul B Schroeder Data = mos7840_port->shadowMCR; 18173f542974SPaul B Schroeder mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data); 18183f542974SPaul B Schroeder 18193f542974SPaul B Schroeder /* Determine divisor based on baud rate */ 18203f542974SPaul B Schroeder baud = tty_get_baud_rate(tty); 18213f542974SPaul B Schroeder 18223f542974SPaul B Schroeder if (!baud) { 18233f542974SPaul B Schroeder /* pick a default, any default... */ 18249c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s", "Picked default baud...\n"); 18253f542974SPaul B Schroeder baud = 9600; 18263f542974SPaul B Schroeder } 18273f542974SPaul B Schroeder 18289c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - baud rate = %d\n", __func__, baud); 18293f542974SPaul B Schroeder status = mos7840_send_cmd_write_baud_rate(mos7840_port, baud); 18303f542974SPaul B Schroeder 18313f542974SPaul B Schroeder /* Enable Interrupts */ 18323f542974SPaul B Schroeder Data = 0x0c; 18333f542974SPaul B Schroeder mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data); 18343f542974SPaul B Schroeder 1835ce9d8562SMathieu OTHACEHE if (!mos7840_port->read_urb_busy) { 183650de36f7SGreg Kroah-Hartman mos7840_port->read_urb_busy = true; 18379d380190SJohan Hovold status = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL); 18383f542974SPaul B Schroeder if (status) { 18399c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n", 18403f542974SPaul B Schroeder status); 184150de36f7SGreg Kroah-Hartman mos7840_port->read_urb_busy = false; 18423f542974SPaul B Schroeder } 18433f542974SPaul B Schroeder } 18449c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - mos7840_port->shadowLCR is End %x\n", __func__, 18453f542974SPaul B Schroeder mos7840_port->shadowLCR); 18463f542974SPaul B Schroeder } 18473f542974SPaul B Schroeder 18483f542974SPaul B Schroeder /***************************************************************************** 18493f542974SPaul B Schroeder * mos7840_set_termios 18503f542974SPaul B Schroeder * this function is called by the tty driver when it wants to change 18513f542974SPaul B Schroeder * the termios structure 18523f542974SPaul B Schroeder *****************************************************************************/ 18533f542974SPaul B Schroeder 185495da310eSAlan Cox static void mos7840_set_termios(struct tty_struct *tty, 185595da310eSAlan Cox struct usb_serial_port *port, 1856606d099cSAlan Cox struct ktermios *old_termios) 18573f542974SPaul B Schroeder { 18583f542974SPaul B Schroeder int status; 18593f542974SPaul B Schroeder struct usb_serial *serial; 18603f542974SPaul B Schroeder struct moschip_port *mos7840_port; 18613363155bSGreg Kroah-Hartman 18629c134a14SGreg Kroah-Hartman if (mos7840_port_paranoia_check(port, __func__)) 18633f542974SPaul B Schroeder return; 18643f542974SPaul B Schroeder 18653f542974SPaul B Schroeder serial = port->serial; 18663f542974SPaul B Schroeder 18679c134a14SGreg Kroah-Hartman if (mos7840_serial_paranoia_check(serial, __func__)) 18683f542974SPaul B Schroeder return; 18693f542974SPaul B Schroeder 18703f542974SPaul B Schroeder mos7840_port = mos7840_get_port_private(port); 18713f542974SPaul B Schroeder 18723f542974SPaul B Schroeder if (mos7840_port == NULL) 18733f542974SPaul B Schroeder return; 18743f542974SPaul B Schroeder 18753f542974SPaul B Schroeder if (!mos7840_port->open) { 18769c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - port not opened\n", __func__); 18773f542974SPaul B Schroeder return; 18783f542974SPaul B Schroeder } 18793f542974SPaul B Schroeder 18803f542974SPaul B Schroeder /* change the port settings to the new ones specified */ 18813f542974SPaul B Schroeder 188295da310eSAlan Cox mos7840_change_port_settings(tty, mos7840_port, old_termios); 18833f542974SPaul B Schroeder 18843f542974SPaul B Schroeder if (!mos7840_port->read_urb) { 18859c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s", "URB KILLED !!!!!\n"); 18863f542974SPaul B Schroeder return; 18873f542974SPaul B Schroeder } 18883f542974SPaul B Schroeder 1889ce9d8562SMathieu OTHACEHE if (!mos7840_port->read_urb_busy) { 189050de36f7SGreg Kroah-Hartman mos7840_port->read_urb_busy = true; 18919d380190SJohan Hovold status = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL); 18923f542974SPaul B Schroeder if (status) { 18939c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n", 18943f542974SPaul B Schroeder status); 189550de36f7SGreg Kroah-Hartman mos7840_port->read_urb_busy = false; 18963f542974SPaul B Schroeder } 18973f542974SPaul B Schroeder } 18983f542974SPaul B Schroeder } 18993f542974SPaul B Schroeder 19003f542974SPaul B Schroeder /***************************************************************************** 19013f542974SPaul B Schroeder * mos7840_get_lsr_info - get line status register info 19023f542974SPaul B Schroeder * 19033f542974SPaul B Schroeder * Purpose: Let user call ioctl() to get info when the UART physically 19043f542974SPaul B Schroeder * is emptied. On bus types like RS485, the transmitter must 19053f542974SPaul B Schroeder * release the bus after transmitting. This must be done when 19063f542974SPaul B Schroeder * the transmit shift register is empty, not be done when the 19073f542974SPaul B Schroeder * transmit holding register is empty. This functionality 19083f542974SPaul B Schroeder * allows an RS485 driver to be written in user space. 19093f542974SPaul B Schroeder *****************************************************************************/ 19103f542974SPaul B Schroeder 191195da310eSAlan Cox static int mos7840_get_lsr_info(struct tty_struct *tty, 191297c4965dSAl Viro unsigned int __user *value) 19133f542974SPaul B Schroeder { 19143f542974SPaul B Schroeder int count; 19153f542974SPaul B Schroeder unsigned int result = 0; 19163f542974SPaul B Schroeder 191795da310eSAlan Cox count = mos7840_chars_in_buffer(tty); 19189c134a14SGreg Kroah-Hartman if (count == 0) 19193f542974SPaul B Schroeder result = TIOCSER_TEMT; 19203f542974SPaul B Schroeder 19213f542974SPaul B Schroeder if (copy_to_user(value, &result, sizeof(int))) 19223f542974SPaul B Schroeder return -EFAULT; 19233f542974SPaul B Schroeder return 0; 19243f542974SPaul B Schroeder } 19253f542974SPaul B Schroeder 19263f542974SPaul B Schroeder /***************************************************************************** 19273f542974SPaul B Schroeder * mos7840_get_serial_info 19283f542974SPaul B Schroeder * function to get information about serial port 19293f542974SPaul B Schroeder *****************************************************************************/ 19303f542974SPaul B Schroeder 19313f542974SPaul B Schroeder static int mos7840_get_serial_info(struct moschip_port *mos7840_port, 193297c4965dSAl Viro struct serial_struct __user *retinfo) 19333f542974SPaul B Schroeder { 19343f542974SPaul B Schroeder struct serial_struct tmp; 19353f542974SPaul B Schroeder 19363f542974SPaul B Schroeder if (mos7840_port == NULL) 19373f542974SPaul B Schroeder return -1; 19383f542974SPaul B Schroeder 19393f542974SPaul B Schroeder memset(&tmp, 0, sizeof(tmp)); 19403f542974SPaul B Schroeder 19413f542974SPaul B Schroeder tmp.type = PORT_16550A; 1942e5b1e206SGreg Kroah-Hartman tmp.line = mos7840_port->port->minor; 19431143832eSGreg Kroah-Hartman tmp.port = mos7840_port->port->port_number; 19443f542974SPaul B Schroeder tmp.irq = 0; 19453f542974SPaul B Schroeder tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE; 19463f542974SPaul B Schroeder tmp.baud_base = 9600; 19473f542974SPaul B Schroeder tmp.close_delay = 5 * HZ; 19483f542974SPaul B Schroeder tmp.closing_wait = 30 * HZ; 19493f542974SPaul B Schroeder 19503f542974SPaul B Schroeder if (copy_to_user(retinfo, &tmp, sizeof(*retinfo))) 19513f542974SPaul B Schroeder return -EFAULT; 19523f542974SPaul B Schroeder return 0; 19533f542974SPaul B Schroeder } 19543f542974SPaul B Schroeder 19553f542974SPaul B Schroeder /***************************************************************************** 19563f542974SPaul B Schroeder * SerialIoctl 19573f542974SPaul B Schroeder * this function handles any ioctl calls to the driver 19583f542974SPaul B Schroeder *****************************************************************************/ 19593f542974SPaul B Schroeder 196000a0d0d6SAlan Cox static int mos7840_ioctl(struct tty_struct *tty, 19613f542974SPaul B Schroeder unsigned int cmd, unsigned long arg) 19623f542974SPaul B Schroeder { 196395da310eSAlan Cox struct usb_serial_port *port = tty->driver_data; 196497c4965dSAl Viro void __user *argp = (void __user *)arg; 19653f542974SPaul B Schroeder struct moschip_port *mos7840_port; 19663f542974SPaul B Schroeder 19679c134a14SGreg Kroah-Hartman if (mos7840_port_paranoia_check(port, __func__)) 19683f542974SPaul B Schroeder return -1; 19693f542974SPaul B Schroeder 19703f542974SPaul B Schroeder mos7840_port = mos7840_get_port_private(port); 19713f542974SPaul B Schroeder 19723f542974SPaul B Schroeder if (mos7840_port == NULL) 19733f542974SPaul B Schroeder return -1; 19743f542974SPaul B Schroeder 19753f542974SPaul B Schroeder switch (cmd) { 19763f542974SPaul B Schroeder /* return number of bytes available */ 19773f542974SPaul B Schroeder 19783f542974SPaul B Schroeder case TIOCSERGETLSR: 19799c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s TIOCSERGETLSR\n", __func__); 198095da310eSAlan Cox return mos7840_get_lsr_info(tty, argp); 19813f542974SPaul B Schroeder 19823f542974SPaul B Schroeder case TIOCGSERIAL: 19839c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s TIOCGSERIAL\n", __func__); 198497c4965dSAl Viro return mos7840_get_serial_info(mos7840_port, argp); 19853f542974SPaul B Schroeder 19863f542974SPaul B Schroeder case TIOCSSERIAL: 19879c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s TIOCSSERIAL\n", __func__); 19883f542974SPaul B Schroeder break; 19893f542974SPaul B Schroeder default: 19903f542974SPaul B Schroeder break; 19913f542974SPaul B Schroeder } 19923f542974SPaul B Schroeder return -ENOIOCTLCMD; 19933f542974SPaul B Schroeder } 19943f542974SPaul B Schroeder 19950eafe4deSDonald static int mos7810_check(struct usb_serial *serial) 19963f542974SPaul B Schroeder { 19970eafe4deSDonald int i, pass_count = 0; 199815ee89c3SJohan Hovold u8 *buf; 19990eafe4deSDonald __u16 data = 0, mcr_data = 0; 20000eafe4deSDonald __u16 test_pattern = 0x55AA; 200115ee89c3SJohan Hovold int res; 200215ee89c3SJohan Hovold 200315ee89c3SJohan Hovold buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL); 200415ee89c3SJohan Hovold if (!buf) 200515ee89c3SJohan Hovold return 0; /* failed to identify 7810 */ 20063f542974SPaul B Schroeder 20070eafe4deSDonald /* Store MCR setting */ 200815ee89c3SJohan Hovold res = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0), 20090eafe4deSDonald MCS_RDREQ, MCS_RD_RTYPE, 0x0300, MODEM_CONTROL_REGISTER, 201015ee89c3SJohan Hovold buf, VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT); 201115ee89c3SJohan Hovold if (res == VENDOR_READ_LENGTH) 201215ee89c3SJohan Hovold mcr_data = *buf; 20130eafe4deSDonald 20140eafe4deSDonald for (i = 0; i < 16; i++) { 20150eafe4deSDonald /* Send the 1-bit test pattern out to MCS7810 test pin */ 20160eafe4deSDonald usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), 20170eafe4deSDonald MCS_WRREQ, MCS_WR_RTYPE, 20180eafe4deSDonald (0x0300 | (((test_pattern >> i) & 0x0001) << 1)), 20190eafe4deSDonald MODEM_CONTROL_REGISTER, NULL, 0, MOS_WDR_TIMEOUT); 20200eafe4deSDonald 20210eafe4deSDonald /* Read the test pattern back */ 202215ee89c3SJohan Hovold res = usb_control_msg(serial->dev, 202315ee89c3SJohan Hovold usb_rcvctrlpipe(serial->dev, 0), MCS_RDREQ, 202415ee89c3SJohan Hovold MCS_RD_RTYPE, 0, GPIO_REGISTER, buf, 2025093ea2d3SDonald Lee VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT); 202615ee89c3SJohan Hovold if (res == VENDOR_READ_LENGTH) 202715ee89c3SJohan Hovold data = *buf; 2028093ea2d3SDonald Lee 20290eafe4deSDonald /* If this is a MCS7810 device, both test patterns must match */ 20300eafe4deSDonald if (((test_pattern >> i) ^ (~data >> 1)) & 0x0001) 20310eafe4deSDonald break; 20320eafe4deSDonald 20330eafe4deSDonald pass_count++; 20343f542974SPaul B Schroeder } 2035093ea2d3SDonald Lee 20360eafe4deSDonald /* Restore MCR setting */ 20370eafe4deSDonald usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), MCS_WRREQ, 20380eafe4deSDonald MCS_WR_RTYPE, 0x0300 | mcr_data, MODEM_CONTROL_REGISTER, NULL, 20390eafe4deSDonald 0, MOS_WDR_TIMEOUT); 20400eafe4deSDonald 204115ee89c3SJohan Hovold kfree(buf); 204215ee89c3SJohan Hovold 20430eafe4deSDonald if (pass_count == 16) 20440eafe4deSDonald return 1; 20450eafe4deSDonald 20460eafe4deSDonald return 0; 20470eafe4deSDonald } 20480eafe4deSDonald 204940c24f28SJohan Hovold static int mos7840_probe(struct usb_serial *serial, 205040c24f28SJohan Hovold const struct usb_device_id *id) 20510eafe4deSDonald { 2052d551ec9bSJohan Hovold u16 product = le16_to_cpu(serial->dev->descriptor.idProduct); 205315ee89c3SJohan Hovold u8 *buf; 205440c24f28SJohan Hovold int device_type; 205540c24f28SJohan Hovold 205640c24f28SJohan Hovold if (product == MOSCHIP_DEVICE_ID_7810 || 205740c24f28SJohan Hovold product == MOSCHIP_DEVICE_ID_7820) { 205840c24f28SJohan Hovold device_type = product; 205940c24f28SJohan Hovold goto out; 206040c24f28SJohan Hovold } 20610eafe4deSDonald 206215ee89c3SJohan Hovold buf = kzalloc(VENDOR_READ_LENGTH, GFP_KERNEL); 206340c24f28SJohan Hovold if (!buf) 206440c24f28SJohan Hovold return -ENOMEM; 206540c24f28SJohan Hovold 20660eafe4deSDonald usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0), 206715ee89c3SJohan Hovold MCS_RDREQ, MCS_RD_RTYPE, 0, GPIO_REGISTER, buf, 20680eafe4deSDonald VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT); 20690eafe4deSDonald 20700eafe4deSDonald /* For a MCS7840 device GPIO0 must be set to 1 */ 207140c24f28SJohan Hovold if (buf[0] & 0x01) 20720eafe4deSDonald device_type = MOSCHIP_DEVICE_ID_7840; 20730eafe4deSDonald else if (mos7810_check(serial)) 20740eafe4deSDonald device_type = MOSCHIP_DEVICE_ID_7810; 20750eafe4deSDonald else 20760eafe4deSDonald device_type = MOSCHIP_DEVICE_ID_7820; 207740c24f28SJohan Hovold 207840c24f28SJohan Hovold kfree(buf); 207940c24f28SJohan Hovold out: 2080683a0e4dSJohan Hovold usb_set_serial_data(serial, (void *)(unsigned long)device_type); 208140c24f28SJohan Hovold 208240c24f28SJohan Hovold return 0; 20830eafe4deSDonald } 20840eafe4deSDonald 208507814246SJohan Hovold static int mos7840_calc_num_ports(struct usb_serial *serial, 208607814246SJohan Hovold struct usb_serial_endpoints *epds) 208740c24f28SJohan Hovold { 2088683a0e4dSJohan Hovold int device_type = (unsigned long)usb_get_serial_data(serial); 208995254020SJohan Hovold int num_ports; 209040c24f28SJohan Hovold 209195254020SJohan Hovold num_ports = (device_type >> 4) & 0x000F; 20920eafe4deSDonald 209395254020SJohan Hovold /* 209495254020SJohan Hovold * num_ports is currently never zero as device_type is one of 209595254020SJohan Hovold * MOSCHIP_DEVICE_ID_78{1,2,4}0. 209695254020SJohan Hovold */ 209795254020SJohan Hovold if (num_ports == 0) 209895254020SJohan Hovold return -ENODEV; 20993f542974SPaul B Schroeder 210095254020SJohan Hovold if (epds->num_bulk_in < num_ports || epds->num_bulk_out < num_ports) { 21015c75633eSJohan Hovold dev_err(&serial->interface->dev, "missing endpoints\n"); 21025c75633eSJohan Hovold return -ENODEV; 21035c75633eSJohan Hovold } 21045c75633eSJohan Hovold 210595254020SJohan Hovold return num_ports; 21065c75633eSJohan Hovold } 21075c75633eSJohan Hovold 210880c00750SJohan Hovold static int mos7840_port_probe(struct usb_serial_port *port) 21093f542974SPaul B Schroeder { 211080c00750SJohan Hovold struct usb_serial *serial = port->serial; 2111683a0e4dSJohan Hovold int device_type = (unsigned long)usb_get_serial_data(serial); 21123f542974SPaul B Schroeder struct moschip_port *mos7840_port; 211380c00750SJohan Hovold int status; 211480c00750SJohan Hovold int pnum; 21153f542974SPaul B Schroeder __u16 Data; 21163f542974SPaul B Schroeder 21173f542974SPaul B Schroeder /* we set up the pointers to the endpoints in the mos7840_open * 21183f542974SPaul B Schroeder * function, as the structures aren't created yet. */ 21193f542974SPaul B Schroeder 21201143832eSGreg Kroah-Hartman pnum = port->port_number; 212180c00750SJohan Hovold 2122ae685effSJohan Hovold dev_dbg(&port->dev, "mos7840_startup: configuring port %d\n", pnum); 21237ac9da10SBurman Yan mos7840_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL); 212410c642d0SJohan Hovold if (!mos7840_port) 212580c00750SJohan Hovold return -ENOMEM; 21263f542974SPaul B Schroeder 2127880af9dbSAlan Cox /* Initialize all port interrupt end point to port 0 int 2128880af9dbSAlan Cox * endpoint. Our device has only one interrupt end point 2129880af9dbSAlan Cox * common to all port */ 21303f542974SPaul B Schroeder 213180c00750SJohan Hovold mos7840_port->port = port; 213280c00750SJohan Hovold mos7840_set_port_private(port, mos7840_port); 21330de9a702SOliver Neukum spin_lock_init(&mos7840_port->pool_lock); 21343f542974SPaul B Schroeder 213537768adfSTony Cook /* minor is not initialised until later by 213637768adfSTony Cook * usb-serial.c:get_free_serial() and cannot therefore be used 213737768adfSTony Cook * to index device instances */ 213880c00750SJohan Hovold mos7840_port->port_num = pnum + 1; 2139e5b1e206SGreg Kroah-Hartman dev_dbg(&port->dev, "port->minor = %d\n", port->minor); 214080c00750SJohan Hovold dev_dbg(&port->dev, "mos7840_port->port_num = %d\n", mos7840_port->port_num); 21413f542974SPaul B Schroeder 21423f542974SPaul B Schroeder if (mos7840_port->port_num == 1) { 21433f542974SPaul B Schroeder mos7840_port->SpRegOffset = 0x0; 21443f542974SPaul B Schroeder mos7840_port->ControlRegOffset = 0x1; 21453f542974SPaul B Schroeder mos7840_port->DcrRegOffset = 0x4; 2146ae685effSJohan Hovold } else if ((mos7840_port->port_num == 2) && (serial->num_ports == 4)) { 21473f542974SPaul B Schroeder mos7840_port->SpRegOffset = 0x8; 21483f542974SPaul B Schroeder mos7840_port->ControlRegOffset = 0x9; 21493f542974SPaul B Schroeder mos7840_port->DcrRegOffset = 0x16; 2150ae685effSJohan Hovold } else if ((mos7840_port->port_num == 2) && (serial->num_ports == 2)) { 21513f542974SPaul B Schroeder mos7840_port->SpRegOffset = 0xa; 21523f542974SPaul B Schroeder mos7840_port->ControlRegOffset = 0xb; 21533f542974SPaul B Schroeder mos7840_port->DcrRegOffset = 0x19; 2154ae685effSJohan Hovold } else if ((mos7840_port->port_num == 3) && (serial->num_ports == 4)) { 21553f542974SPaul B Schroeder mos7840_port->SpRegOffset = 0xa; 21563f542974SPaul B Schroeder mos7840_port->ControlRegOffset = 0xb; 21573f542974SPaul B Schroeder mos7840_port->DcrRegOffset = 0x19; 2158ae685effSJohan Hovold } else if ((mos7840_port->port_num == 4) && (serial->num_ports == 4)) { 21593f542974SPaul B Schroeder mos7840_port->SpRegOffset = 0xc; 21603f542974SPaul B Schroeder mos7840_port->ControlRegOffset = 0xd; 21613f542974SPaul B Schroeder mos7840_port->DcrRegOffset = 0x1c; 21623f542974SPaul B Schroeder } 216380c00750SJohan Hovold mos7840_dump_serial_port(port, mos7840_port); 216480c00750SJohan Hovold mos7840_set_port_private(port, mos7840_port); 21653f542974SPaul B Schroeder 2166880af9dbSAlan Cox /* enable rx_disable bit in control register */ 216780c00750SJohan Hovold status = mos7840_get_reg_sync(port, 21683f542974SPaul B Schroeder mos7840_port->ControlRegOffset, &Data); 21693f542974SPaul B Schroeder if (status < 0) { 217080c00750SJohan Hovold dev_dbg(&port->dev, "Reading ControlReg failed status-0x%x\n", status); 2171ae685effSJohan Hovold goto out; 21723f542974SPaul B Schroeder } else 217380c00750SJohan Hovold dev_dbg(&port->dev, "ControlReg Reading success val is %x, status%d\n", Data, status); 2174880af9dbSAlan Cox Data |= 0x08; /* setting driver done bit */ 2175880af9dbSAlan Cox Data |= 0x04; /* sp1_bit to have cts change reflect in 2176880af9dbSAlan Cox modem status reg */ 21773f542974SPaul B Schroeder 2178880af9dbSAlan Cox /* Data |= 0x20; //rx_disable bit */ 217980c00750SJohan Hovold status = mos7840_set_reg_sync(port, 21803f542974SPaul B Schroeder mos7840_port->ControlRegOffset, Data); 21813f542974SPaul B Schroeder if (status < 0) { 218280c00750SJohan Hovold dev_dbg(&port->dev, "Writing ControlReg failed(rx_disable) status-0x%x\n", status); 2183ae685effSJohan Hovold goto out; 21843f542974SPaul B Schroeder } else 218580c00750SJohan Hovold dev_dbg(&port->dev, "ControlReg Writing success(rx_disable) status%d\n", status); 21863f542974SPaul B Schroeder 2187880af9dbSAlan Cox /* Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2 2188880af9dbSAlan Cox and 0x24 in DCR3 */ 21893f542974SPaul B Schroeder Data = 0x01; 219080c00750SJohan Hovold status = mos7840_set_reg_sync(port, 2191880af9dbSAlan Cox (__u16) (mos7840_port->DcrRegOffset + 0), Data); 21923f542974SPaul B Schroeder if (status < 0) { 219380c00750SJohan Hovold dev_dbg(&port->dev, "Writing DCR0 failed status-0x%x\n", status); 2194ae685effSJohan Hovold goto out; 21953f542974SPaul B Schroeder } else 219680c00750SJohan Hovold dev_dbg(&port->dev, "DCR0 Writing success status%d\n", status); 21973f542974SPaul B Schroeder 21983f542974SPaul B Schroeder Data = 0x05; 219980c00750SJohan Hovold status = mos7840_set_reg_sync(port, 2200880af9dbSAlan Cox (__u16) (mos7840_port->DcrRegOffset + 1), Data); 22013f542974SPaul B Schroeder if (status < 0) { 220280c00750SJohan Hovold dev_dbg(&port->dev, "Writing DCR1 failed status-0x%x\n", status); 2203ae685effSJohan Hovold goto out; 22043f542974SPaul B Schroeder } else 220580c00750SJohan Hovold dev_dbg(&port->dev, "DCR1 Writing success status%d\n", status); 22063f542974SPaul B Schroeder 22073f542974SPaul B Schroeder Data = 0x24; 220880c00750SJohan Hovold status = mos7840_set_reg_sync(port, 2209880af9dbSAlan Cox (__u16) (mos7840_port->DcrRegOffset + 2), Data); 22103f542974SPaul B Schroeder if (status < 0) { 221180c00750SJohan Hovold dev_dbg(&port->dev, "Writing DCR2 failed status-0x%x\n", status); 2212ae685effSJohan Hovold goto out; 22133f542974SPaul B Schroeder } else 221480c00750SJohan Hovold dev_dbg(&port->dev, "DCR2 Writing success status%d\n", status); 22153f542974SPaul B Schroeder 2216880af9dbSAlan Cox /* write values in clkstart0x0 and clkmulti 0x20 */ 22173f542974SPaul B Schroeder Data = 0x0; 2218ae685effSJohan Hovold status = mos7840_set_reg_sync(port, CLK_START_VALUE_REGISTER, Data); 22193f542974SPaul B Schroeder if (status < 0) { 222080c00750SJohan Hovold dev_dbg(&port->dev, "Writing CLK_START_VALUE_REGISTER failed status-0x%x\n", status); 2221ae685effSJohan Hovold goto out; 22223f542974SPaul B Schroeder } else 222380c00750SJohan Hovold dev_dbg(&port->dev, "CLK_START_VALUE_REGISTER Writing success status%d\n", status); 22243f542974SPaul B Schroeder 22253f542974SPaul B Schroeder Data = 0x20; 2226ae685effSJohan Hovold status = mos7840_set_reg_sync(port, CLK_MULTI_REGISTER, Data); 22273f542974SPaul B Schroeder if (status < 0) { 222880c00750SJohan Hovold dev_dbg(&port->dev, "Writing CLK_MULTI_REGISTER failed status-0x%x\n", status); 22290de9a702SOliver Neukum goto error; 22303f542974SPaul B Schroeder } else 223180c00750SJohan Hovold dev_dbg(&port->dev, "CLK_MULTI_REGISTER Writing success status%d\n", status); 22323f542974SPaul B Schroeder 2233880af9dbSAlan Cox /* write value 0x0 to scratchpad register */ 22343f542974SPaul B Schroeder Data = 0x00; 2235ae685effSJohan Hovold status = mos7840_set_uart_reg(port, SCRATCH_PAD_REGISTER, Data); 22363f542974SPaul B Schroeder if (status < 0) { 223780c00750SJohan Hovold dev_dbg(&port->dev, "Writing SCRATCH_PAD_REGISTER failed status-0x%x\n", status); 2238ae685effSJohan Hovold goto out; 22393f542974SPaul B Schroeder } else 224080c00750SJohan Hovold dev_dbg(&port->dev, "SCRATCH_PAD_REGISTER Writing success status%d\n", status); 22413f542974SPaul B Schroeder 2242880af9dbSAlan Cox /* Zero Length flag register */ 2243ae685effSJohan Hovold if ((mos7840_port->port_num != 1) && (serial->num_ports == 2)) { 22443f542974SPaul B Schroeder Data = 0xff; 224580c00750SJohan Hovold status = mos7840_set_reg_sync(port, 22463f542974SPaul B Schroeder (__u16) (ZLP_REG1 + 2247880af9dbSAlan Cox ((__u16)mos7840_port->port_num)), Data); 224880c00750SJohan Hovold dev_dbg(&port->dev, "ZLIP offset %x\n", 22499c134a14SGreg Kroah-Hartman (__u16)(ZLP_REG1 + ((__u16) mos7840_port->port_num))); 22503f542974SPaul B Schroeder if (status < 0) { 225180c00750SJohan Hovold dev_dbg(&port->dev, "Writing ZLP_REG%d failed status-0x%x\n", pnum + 2, status); 2252ae685effSJohan Hovold goto out; 22533f542974SPaul B Schroeder } else 225480c00750SJohan Hovold dev_dbg(&port->dev, "ZLP_REG%d Writing success status%d\n", pnum + 2, status); 22553f542974SPaul B Schroeder } else { 22563f542974SPaul B Schroeder Data = 0xff; 225780c00750SJohan Hovold status = mos7840_set_reg_sync(port, 22583f542974SPaul B Schroeder (__u16) (ZLP_REG1 + 2259880af9dbSAlan Cox ((__u16)mos7840_port->port_num) - 0x1), Data); 226080c00750SJohan Hovold dev_dbg(&port->dev, "ZLIP offset %x\n", 22619c134a14SGreg Kroah-Hartman (__u16)(ZLP_REG1 + ((__u16) mos7840_port->port_num) - 0x1)); 22623f542974SPaul B Schroeder if (status < 0) { 226380c00750SJohan Hovold dev_dbg(&port->dev, "Writing ZLP_REG%d failed status-0x%x\n", pnum + 1, status); 2264ae685effSJohan Hovold goto out; 22653f542974SPaul B Schroeder } else 226680c00750SJohan Hovold dev_dbg(&port->dev, "ZLP_REG%d Writing success status%d\n", pnum + 1, status); 22673f542974SPaul B Schroeder 22683f542974SPaul B Schroeder } 22690de9a702SOliver Neukum mos7840_port->control_urb = usb_alloc_urb(0, GFP_KERNEL); 22703f542974SPaul B Schroeder mos7840_port->ctrl_buf = kmalloc(16, GFP_KERNEL); 2271880af9dbSAlan Cox mos7840_port->dr = kmalloc(sizeof(struct usb_ctrlrequest), 2272880af9dbSAlan Cox GFP_KERNEL); 2273880af9dbSAlan Cox if (!mos7840_port->control_urb || !mos7840_port->ctrl_buf || 2274880af9dbSAlan Cox !mos7840_port->dr) { 22750de9a702SOliver Neukum status = -ENOMEM; 22760de9a702SOliver Neukum goto error; 22770de9a702SOliver Neukum } 22780eafe4deSDonald 22790eafe4deSDonald mos7840_port->has_led = false; 22800eafe4deSDonald 22810eafe4deSDonald /* Initialize LED timers */ 22820eafe4deSDonald if (device_type == MOSCHIP_DEVICE_ID_7810) { 22830eafe4deSDonald mos7840_port->has_led = true; 22840eafe4deSDonald 228505cf0decSJohan Hovold mos7840_port->led_urb = usb_alloc_urb(0, GFP_KERNEL); 228605cf0decSJohan Hovold mos7840_port->led_dr = kmalloc(sizeof(*mos7840_port->led_dr), 228705cf0decSJohan Hovold GFP_KERNEL); 228805cf0decSJohan Hovold if (!mos7840_port->led_urb || !mos7840_port->led_dr) { 228905cf0decSJohan Hovold status = -ENOMEM; 229005cf0decSJohan Hovold goto error; 229105cf0decSJohan Hovold } 229205cf0decSJohan Hovold 2293e99e88a9SKees Cook timer_setup(&mos7840_port->led_timer1, mos7840_led_off, 0); 22940eafe4deSDonald mos7840_port->led_timer1.expires = 22950eafe4deSDonald jiffies + msecs_to_jiffies(LED_ON_MS); 2296e99e88a9SKees Cook timer_setup(&mos7840_port->led_timer2, mos7840_led_flag_off, 2297e99e88a9SKees Cook 0); 22980eafe4deSDonald mos7840_port->led_timer2.expires = 22990eafe4deSDonald jiffies + msecs_to_jiffies(LED_OFF_MS); 23000eafe4deSDonald 23010eafe4deSDonald /* Turn off LED */ 2302ae685effSJohan Hovold mos7840_set_led_sync(port, MODEM_CONTROL_REGISTER, 0x0300); 23030eafe4deSDonald } 2304ae685effSJohan Hovold out: 230580c00750SJohan Hovold if (pnum == serial->num_ports - 1) { 2306880af9dbSAlan Cox /* Zero Length flag enable */ 23073f542974SPaul B Schroeder Data = 0x0f; 23083f542974SPaul B Schroeder status = mos7840_set_reg_sync(serial->port[0], ZLP_REG5, Data); 23093f542974SPaul B Schroeder if (status < 0) { 231080c00750SJohan Hovold dev_dbg(&port->dev, "Writing ZLP_REG5 failed status-0x%x\n", status); 23117ced46c3SRoel Kluin goto error; 23123f542974SPaul B Schroeder } else 231380c00750SJohan Hovold dev_dbg(&port->dev, "ZLP_REG5 Writing success status%d\n", status); 23143f542974SPaul B Schroeder 23153f542974SPaul B Schroeder /* setting configuration feature to one */ 23163f542974SPaul B Schroeder usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), 231780c00750SJohan Hovold 0x03, 0x00, 0x01, 0x00, NULL, 0x00, 231880c00750SJohan Hovold MOS_WDR_TIMEOUT); 231980c00750SJohan Hovold } 23203f542974SPaul B Schroeder return 0; 23210de9a702SOliver Neukum error: 232205cf0decSJohan Hovold kfree(mos7840_port->led_dr); 232305cf0decSJohan Hovold usb_free_urb(mos7840_port->led_urb); 23240de9a702SOliver Neukum kfree(mos7840_port->dr); 23250de9a702SOliver Neukum kfree(mos7840_port->ctrl_buf); 23260de9a702SOliver Neukum usb_free_urb(mos7840_port->control_urb); 23270de9a702SOliver Neukum kfree(mos7840_port); 232880c00750SJohan Hovold 23290de9a702SOliver Neukum return status; 23303f542974SPaul B Schroeder } 23313f542974SPaul B Schroeder 233280c00750SJohan Hovold static int mos7840_port_remove(struct usb_serial_port *port) 23333f542974SPaul B Schroeder { 23343f542974SPaul B Schroeder struct moschip_port *mos7840_port; 23353f542974SPaul B Schroeder 233680c00750SJohan Hovold mos7840_port = mos7840_get_port_private(port); 23373f542974SPaul B Schroeder 23380eafe4deSDonald if (mos7840_port->has_led) { 23390eafe4deSDonald /* Turn off LED */ 234080c00750SJohan Hovold mos7840_set_led_sync(port, MODEM_CONTROL_REGISTER, 0x0300); 23410eafe4deSDonald 23420eafe4deSDonald del_timer_sync(&mos7840_port->led_timer1); 23430eafe4deSDonald del_timer_sync(&mos7840_port->led_timer2); 234405cf0decSJohan Hovold 234505cf0decSJohan Hovold usb_kill_urb(mos7840_port->led_urb); 234605cf0decSJohan Hovold usb_free_urb(mos7840_port->led_urb); 234705cf0decSJohan Hovold kfree(mos7840_port->led_dr); 23480eafe4deSDonald } 234980c00750SJohan Hovold usb_kill_urb(mos7840_port->control_urb); 235065a4cdbbSJohan Hovold usb_free_urb(mos7840_port->control_urb); 23510de9a702SOliver Neukum kfree(mos7840_port->ctrl_buf); 23520de9a702SOliver Neukum kfree(mos7840_port->dr); 23533f542974SPaul B Schroeder kfree(mos7840_port); 235480c00750SJohan Hovold 235580c00750SJohan Hovold return 0; 23563f542974SPaul B Schroeder } 23573f542974SPaul B Schroeder 23583f542974SPaul B Schroeder static struct usb_serial_driver moschip7840_4port_device = { 23593f542974SPaul B Schroeder .driver = { 23603f542974SPaul B Schroeder .owner = THIS_MODULE, 23613f542974SPaul B Schroeder .name = "mos7840", 23623f542974SPaul B Schroeder }, 23633f542974SPaul B Schroeder .description = DRIVER_DESC, 236468e24113SGreg Kroah-Hartman .id_table = id_table, 236595254020SJohan Hovold .num_interrupt_in = 1, 23663f542974SPaul B Schroeder .open = mos7840_open, 23673f542974SPaul B Schroeder .close = mos7840_close, 23683f542974SPaul B Schroeder .write = mos7840_write, 23693f542974SPaul B Schroeder .write_room = mos7840_write_room, 23703f542974SPaul B Schroeder .chars_in_buffer = mos7840_chars_in_buffer, 23713f542974SPaul B Schroeder .throttle = mos7840_throttle, 23723f542974SPaul B Schroeder .unthrottle = mos7840_unthrottle, 23733f542974SPaul B Schroeder .calc_num_ports = mos7840_calc_num_ports, 237440c24f28SJohan Hovold .probe = mos7840_probe, 23753f542974SPaul B Schroeder .ioctl = mos7840_ioctl, 23763f542974SPaul B Schroeder .set_termios = mos7840_set_termios, 23773f542974SPaul B Schroeder .break_ctl = mos7840_break, 23783f542974SPaul B Schroeder .tiocmget = mos7840_tiocmget, 23793f542974SPaul B Schroeder .tiocmset = mos7840_tiocmset, 23800c613371SJohan Hovold .tiocmiwait = usb_serial_generic_tiocmiwait, 23818c1a07ffSJohan Hovold .get_icount = usb_serial_generic_get_icount, 238280c00750SJohan Hovold .port_probe = mos7840_port_probe, 238380c00750SJohan Hovold .port_remove = mos7840_port_remove, 23843f542974SPaul B Schroeder .read_bulk_callback = mos7840_bulk_in_callback, 23853f542974SPaul B Schroeder .read_int_callback = mos7840_interrupt_callback, 23863f542974SPaul B Schroeder }; 23873f542974SPaul B Schroeder 23884d2a7affSAlan Stern static struct usb_serial_driver * const serial_drivers[] = { 23894d2a7affSAlan Stern &moschip7840_4port_device, NULL 23904d2a7affSAlan Stern }; 23914d2a7affSAlan Stern 239268e24113SGreg Kroah-Hartman module_usb_serial_driver(serial_drivers, id_table); 23933f542974SPaul B Schroeder 23943f542974SPaul B Schroeder MODULE_DESCRIPTION(DRIVER_DESC); 23953f542974SPaul B Schroeder MODULE_LICENSE("GPL"); 2396