Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1 | /* |
| 2 | * This program is free software; you can redistribute it and/or modify |
| 3 | * it under the terms of the GNU General Public License as published by |
| 4 | * the Free Software Foundation; either version 2 of the License, or |
| 5 | * (at your option) any later version. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | * |
| 12 | * You should have received a copy of the GNU General Public License |
| 13 | * along with this program; if not, write to the Free Software |
| 14 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 15 | * |
| 16 | * Clean ups from Moschip version and a few ioctl implementations by: |
| 17 | * Paul B Schroeder <pschroeder "at" uplogix "dot" com> |
| 18 | * |
| 19 | * Originally based on drivers/usb/serial/io_edgeport.c which is: |
| 20 | * Copyright (C) 2000 Inside Out Networks, All rights reserved. |
| 21 | * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com> |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | #include <linux/kernel.h> |
| 26 | #include <linux/errno.h> |
| 27 | #include <linux/init.h> |
| 28 | #include <linux/slab.h> |
| 29 | #include <linux/tty.h> |
| 30 | #include <linux/tty_driver.h> |
| 31 | #include <linux/tty_flip.h> |
| 32 | #include <linux/module.h> |
| 33 | #include <linux/serial.h> |
| 34 | #include <linux/usb.h> |
| 35 | #include <linux/usb/serial.h> |
Alan Cox | 880af9d | 2008-07-22 11:16:12 +0100 | [diff] [blame] | 36 | #include <linux/uaccess.h> |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 37 | |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 38 | #define DRIVER_DESC "Moschip 7840/7820 USB Serial Driver" |
| 39 | |
| 40 | /* |
| 41 | * 16C50 UART register defines |
| 42 | */ |
| 43 | |
| 44 | #define LCR_BITS_5 0x00 /* 5 bits/char */ |
| 45 | #define LCR_BITS_6 0x01 /* 6 bits/char */ |
| 46 | #define LCR_BITS_7 0x02 /* 7 bits/char */ |
| 47 | #define LCR_BITS_8 0x03 /* 8 bits/char */ |
| 48 | #define LCR_BITS_MASK 0x03 /* Mask for bits/char field */ |
| 49 | |
| 50 | #define LCR_STOP_1 0x00 /* 1 stop bit */ |
| 51 | #define LCR_STOP_1_5 0x04 /* 1.5 stop bits (if 5 bits/char) */ |
| 52 | #define LCR_STOP_2 0x04 /* 2 stop bits (if 6-8 bits/char) */ |
| 53 | #define LCR_STOP_MASK 0x04 /* Mask for stop bits field */ |
| 54 | |
| 55 | #define LCR_PAR_NONE 0x00 /* No parity */ |
| 56 | #define LCR_PAR_ODD 0x08 /* Odd parity */ |
| 57 | #define LCR_PAR_EVEN 0x18 /* Even parity */ |
| 58 | #define LCR_PAR_MARK 0x28 /* Force parity bit to 1 */ |
| 59 | #define LCR_PAR_SPACE 0x38 /* Force parity bit to 0 */ |
| 60 | #define LCR_PAR_MASK 0x38 /* Mask for parity field */ |
| 61 | |
| 62 | #define LCR_SET_BREAK 0x40 /* Set Break condition */ |
| 63 | #define LCR_DL_ENABLE 0x80 /* Enable access to divisor latch */ |
| 64 | |
| 65 | #define MCR_DTR 0x01 /* Assert DTR */ |
| 66 | #define MCR_RTS 0x02 /* Assert RTS */ |
| 67 | #define MCR_OUT1 0x04 /* Loopback only: Sets state of RI */ |
| 68 | #define MCR_MASTER_IE 0x08 /* Enable interrupt outputs */ |
| 69 | #define MCR_LOOPBACK 0x10 /* Set internal (digital) loopback mode */ |
| 70 | #define MCR_XON_ANY 0x20 /* Enable any char to exit XOFF mode */ |
| 71 | |
| 72 | #define MOS7840_MSR_CTS 0x10 /* Current state of CTS */ |
| 73 | #define MOS7840_MSR_DSR 0x20 /* Current state of DSR */ |
| 74 | #define MOS7840_MSR_RI 0x40 /* Current state of RI */ |
| 75 | #define MOS7840_MSR_CD 0x80 /* Current state of CD */ |
| 76 | |
| 77 | /* |
| 78 | * Defines used for sending commands to port |
| 79 | */ |
| 80 | |
Mark Ferrell | 1e65848 | 2012-07-24 13:38:31 -0500 | [diff] [blame] | 81 | #define MOS_WDR_TIMEOUT 5000 /* default urb timeout */ |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 82 | |
| 83 | #define MOS_PORT1 0x0200 |
| 84 | #define MOS_PORT2 0x0300 |
| 85 | #define MOS_VENREG 0x0000 |
| 86 | #define MOS_MAX_PORT 0x02 |
| 87 | #define MOS_WRITE 0x0E |
| 88 | #define MOS_READ 0x0D |
| 89 | |
| 90 | /* Requests */ |
| 91 | #define MCS_RD_RTYPE 0xC0 |
| 92 | #define MCS_WR_RTYPE 0x40 |
| 93 | #define MCS_RDREQ 0x0D |
| 94 | #define MCS_WRREQ 0x0E |
| 95 | #define MCS_CTRL_TIMEOUT 500 |
| 96 | #define VENDOR_READ_LENGTH (0x01) |
| 97 | |
| 98 | #define MAX_NAME_LEN 64 |
| 99 | |
Alan Cox | 880af9d | 2008-07-22 11:16:12 +0100 | [diff] [blame] | 100 | #define ZLP_REG1 0x3A /* Zero_Flag_Reg1 58 */ |
| 101 | #define ZLP_REG5 0x3E /* Zero_Flag_Reg5 62 */ |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 102 | |
| 103 | /* For higher baud Rates use TIOCEXBAUD */ |
| 104 | #define TIOCEXBAUD 0x5462 |
| 105 | |
| 106 | /* vendor id and device id defines */ |
| 107 | |
David Ludlow | 11e1abb | 2008-02-25 17:30:52 -0500 | [diff] [blame] | 108 | /* The native mos7840/7820 component */ |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 109 | #define USB_VENDOR_ID_MOSCHIP 0x9710 |
| 110 | #define MOSCHIP_DEVICE_ID_7840 0x7840 |
| 111 | #define MOSCHIP_DEVICE_ID_7820 0x7820 |
Donald | 0eafe4d | 2012-04-19 15:00:45 +0800 | [diff] [blame] | 112 | #define MOSCHIP_DEVICE_ID_7810 0x7810 |
David Ludlow | 11e1abb | 2008-02-25 17:30:52 -0500 | [diff] [blame] | 113 | /* The native component can have its vendor/device id's overridden |
| 114 | * in vendor-specific implementations. Such devices can be handled |
Greg Kroah-Hartman | 68e2411 | 2012-05-08 15:46:14 -0700 | [diff] [blame] | 115 | * by making a change here, in id_table. |
David Ludlow | 11e1abb | 2008-02-25 17:30:52 -0500 | [diff] [blame] | 116 | */ |
Dave Ludlow | 870408c | 2010-09-01 12:33:30 -0400 | [diff] [blame] | 117 | #define USB_VENDOR_ID_BANDB 0x0856 |
| 118 | #define BANDB_DEVICE_ID_USO9ML2_2 0xAC22 |
| 119 | #define BANDB_DEVICE_ID_USO9ML2_2P 0xBC00 |
| 120 | #define BANDB_DEVICE_ID_USO9ML2_4 0xAC24 |
| 121 | #define BANDB_DEVICE_ID_USO9ML2_4P 0xBC01 |
| 122 | #define BANDB_DEVICE_ID_US9ML2_2 0xAC29 |
| 123 | #define BANDB_DEVICE_ID_US9ML2_4 0xAC30 |
| 124 | #define BANDB_DEVICE_ID_USPTL4_2 0xAC31 |
| 125 | #define BANDB_DEVICE_ID_USPTL4_4 0xAC32 |
| 126 | #define BANDB_DEVICE_ID_USOPTL4_2 0xAC42 |
| 127 | #define BANDB_DEVICE_ID_USOPTL4_2P 0xBC02 |
| 128 | #define BANDB_DEVICE_ID_USOPTL4_4 0xAC44 |
| 129 | #define BANDB_DEVICE_ID_USOPTL4_4P 0xBC03 |
| 130 | #define BANDB_DEVICE_ID_USOPTL2_4 0xAC24 |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 131 | |
Russell Lang | 9d498be | 2009-07-17 19:29:20 +1000 | [diff] [blame] | 132 | /* This driver also supports |
| 133 | * ATEN UC2324 device using Moschip MCS7840 |
| 134 | * ATEN UC2322 device using Moschip MCS7820 |
| 135 | */ |
Tony Cook | e9b8cff | 2009-04-18 22:42:18 +0930 | [diff] [blame] | 136 | #define USB_VENDOR_ID_ATENINTL 0x0557 |
| 137 | #define ATENINTL_DEVICE_ID_UC2324 0x2011 |
Russell Lang | 9d498be | 2009-07-17 19:29:20 +1000 | [diff] [blame] | 138 | #define ATENINTL_DEVICE_ID_UC2322 0x7820 |
Tony Cook | e9b8cff | 2009-04-18 22:42:18 +0930 | [diff] [blame] | 139 | |
David Ludlow | 11e1abb | 2008-02-25 17:30:52 -0500 | [diff] [blame] | 140 | /* Interrupt Routine Defines */ |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 141 | |
| 142 | #define SERIAL_IIR_RLS 0x06 |
| 143 | #define SERIAL_IIR_MS 0x00 |
| 144 | |
| 145 | /* |
| 146 | * Emulation of the bit mask on the LINE STATUS REGISTER. |
| 147 | */ |
| 148 | #define SERIAL_LSR_DR 0x0001 |
| 149 | #define SERIAL_LSR_OE 0x0002 |
| 150 | #define SERIAL_LSR_PE 0x0004 |
| 151 | #define SERIAL_LSR_FE 0x0008 |
| 152 | #define SERIAL_LSR_BI 0x0010 |
| 153 | |
| 154 | #define MOS_MSR_DELTA_CTS 0x10 |
| 155 | #define MOS_MSR_DELTA_DSR 0x20 |
| 156 | #define MOS_MSR_DELTA_RI 0x40 |
| 157 | #define MOS_MSR_DELTA_CD 0x80 |
| 158 | |
Alan Cox | 880af9d | 2008-07-22 11:16:12 +0100 | [diff] [blame] | 159 | /* Serial Port register Address */ |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 160 | #define INTERRUPT_ENABLE_REGISTER ((__u16)(0x01)) |
| 161 | #define FIFO_CONTROL_REGISTER ((__u16)(0x02)) |
| 162 | #define LINE_CONTROL_REGISTER ((__u16)(0x03)) |
| 163 | #define MODEM_CONTROL_REGISTER ((__u16)(0x04)) |
| 164 | #define LINE_STATUS_REGISTER ((__u16)(0x05)) |
| 165 | #define MODEM_STATUS_REGISTER ((__u16)(0x06)) |
| 166 | #define SCRATCH_PAD_REGISTER ((__u16)(0x07)) |
| 167 | #define DIVISOR_LATCH_LSB ((__u16)(0x00)) |
| 168 | #define DIVISOR_LATCH_MSB ((__u16)(0x01)) |
| 169 | |
| 170 | #define CLK_MULTI_REGISTER ((__u16)(0x02)) |
| 171 | #define CLK_START_VALUE_REGISTER ((__u16)(0x03)) |
Donald Lee | 093ea2d | 2012-03-14 15:26:33 +0800 | [diff] [blame] | 172 | #define GPIO_REGISTER ((__u16)(0x07)) |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 173 | |
| 174 | #define SERIAL_LCR_DLAB ((__u16)(0x0080)) |
| 175 | |
| 176 | /* |
| 177 | * URB POOL related defines |
| 178 | */ |
| 179 | #define NUM_URBS 16 /* URB Count */ |
| 180 | #define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */ |
| 181 | |
Donald | 0eafe4d | 2012-04-19 15:00:45 +0800 | [diff] [blame] | 182 | /* LED on/off milliseconds*/ |
| 183 | #define LED_ON_MS 500 |
| 184 | #define LED_OFF_MS 500 |
| 185 | |
| 186 | static int device_type; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 187 | |
Tony Zelenoff | b9c8766 | 2012-06-05 17:58:04 +0400 | [diff] [blame] | 188 | static const struct usb_device_id id_table[] = { |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 189 | {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7840)}, |
| 190 | {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7820)}, |
Donald | 0eafe4d | 2012-04-19 15:00:45 +0800 | [diff] [blame] | 191 | {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7810)}, |
Cliff Brake | acf509a | 2009-12-01 09:53:43 -0500 | [diff] [blame] | 192 | {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2)}, |
Dave Ludlow | 870408c | 2010-09-01 12:33:30 -0400 | [diff] [blame] | 193 | {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2P)}, |
Cliff Brake | acf509a | 2009-12-01 09:53:43 -0500 | [diff] [blame] | 194 | {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4)}, |
Dave Ludlow | 870408c | 2010-09-01 12:33:30 -0400 | [diff] [blame] | 195 | {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4P)}, |
Cliff Brake | acf509a | 2009-12-01 09:53:43 -0500 | [diff] [blame] | 196 | {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_2)}, |
| 197 | {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_4)}, |
| 198 | {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_2)}, |
| 199 | {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_4)}, |
David Ludlow | 11e1abb | 2008-02-25 17:30:52 -0500 | [diff] [blame] | 200 | {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2)}, |
Dave Ludlow | 870408c | 2010-09-01 12:33:30 -0400 | [diff] [blame] | 201 | {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2P)}, |
Cliff Brake | acf509a | 2009-12-01 09:53:43 -0500 | [diff] [blame] | 202 | {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4)}, |
Dave Ludlow | 870408c | 2010-09-01 12:33:30 -0400 | [diff] [blame] | 203 | {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4P)}, |
Blaise Gassend | 27f1281 | 2009-12-18 15:23:38 -0800 | [diff] [blame] | 204 | {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL2_4)}, |
Tony Cook | e9b8cff | 2009-04-18 22:42:18 +0930 | [diff] [blame] | 205 | {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2324)}, |
Russell Lang | 9d498be | 2009-07-17 19:29:20 +1000 | [diff] [blame] | 206 | {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2322)}, |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 207 | {} /* terminating entry */ |
| 208 | }; |
Greg Kroah-Hartman | 68e2411 | 2012-05-08 15:46:14 -0700 | [diff] [blame] | 209 | MODULE_DEVICE_TABLE(usb, id_table); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 210 | |
| 211 | /* This structure holds all of the local port information */ |
| 212 | |
| 213 | struct moschip_port { |
| 214 | int port_num; /*Actual port number in the device(1,2,etc) */ |
| 215 | struct urb *write_urb; /* write URB for this port */ |
| 216 | struct urb *read_urb; /* read URB for this port */ |
| 217 | __u8 shadowLCR; /* last LCR value received */ |
| 218 | __u8 shadowMCR; /* last MCR value received */ |
| 219 | char open; |
Oliver Neukum | 0de9a70 | 2007-03-16 20:28:28 +0100 | [diff] [blame] | 220 | char open_ports; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 221 | wait_queue_head_t wait_chase; /* for handling sleeping while waiting for chase to finish */ |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 222 | struct usb_serial_port *port; /* loop back to the owner of this object */ |
| 223 | |
Alan Cox | 880af9d | 2008-07-22 11:16:12 +0100 | [diff] [blame] | 224 | /* Offsets */ |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 225 | __u8 SpRegOffset; |
| 226 | __u8 ControlRegOffset; |
| 227 | __u8 DcrRegOffset; |
Alan Cox | 880af9d | 2008-07-22 11:16:12 +0100 | [diff] [blame] | 228 | /* for processing control URBS in interrupt context */ |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 229 | struct urb *control_urb; |
Oliver Neukum | 0de9a70 | 2007-03-16 20:28:28 +0100 | [diff] [blame] | 230 | struct usb_ctrlrequest *dr; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 231 | char *ctrl_buf; |
| 232 | int MsrLsr; |
| 233 | |
Oliver Neukum | 0de9a70 | 2007-03-16 20:28:28 +0100 | [diff] [blame] | 234 | spinlock_t pool_lock; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 235 | struct urb *write_urb_pool[NUM_URBS]; |
Oliver Neukum | 0de9a70 | 2007-03-16 20:28:28 +0100 | [diff] [blame] | 236 | char busy[NUM_URBS]; |
Greg Kroah-Hartman | 50de36f | 2008-12-10 16:00:30 -0800 | [diff] [blame] | 237 | bool read_urb_busy; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 238 | |
Donald | 0eafe4d | 2012-04-19 15:00:45 +0800 | [diff] [blame] | 239 | /* For device(s) with LED indicator */ |
| 240 | bool has_led; |
| 241 | bool led_flag; |
| 242 | struct timer_list led_timer1; /* Timer for LED on */ |
| 243 | struct timer_list led_timer2; /* Timer for LED off */ |
| 244 | }; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 245 | |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 246 | /* |
| 247 | * mos7840_set_reg_sync |
| 248 | * To set the Control register by calling usb_fill_control_urb function |
| 249 | * by passing usb_sndctrlpipe function as parameter. |
| 250 | */ |
| 251 | |
| 252 | static int mos7840_set_reg_sync(struct usb_serial_port *port, __u16 reg, |
| 253 | __u16 val) |
| 254 | { |
| 255 | struct usb_device *dev = port->serial->dev; |
| 256 | val = val & 0x00ff; |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 257 | dev_dbg(&port->dev, "mos7840_set_reg_sync offset is %x, value %x\n", reg, val); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 258 | |
| 259 | return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ, |
| 260 | MCS_WR_RTYPE, val, reg, NULL, 0, |
| 261 | MOS_WDR_TIMEOUT); |
| 262 | } |
| 263 | |
| 264 | /* |
| 265 | * mos7840_get_reg_sync |
| 266 | * To set the Uart register by calling usb_fill_control_urb function by |
| 267 | * passing usb_rcvctrlpipe function as parameter. |
| 268 | */ |
| 269 | |
| 270 | static int mos7840_get_reg_sync(struct usb_serial_port *port, __u16 reg, |
Alan Cox | 880af9d | 2008-07-22 11:16:12 +0100 | [diff] [blame] | 271 | __u16 *val) |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 272 | { |
| 273 | struct usb_device *dev = port->serial->dev; |
| 274 | int ret = 0; |
Johan Hovold | 9e221a3 | 2009-12-28 23:01:55 +0100 | [diff] [blame] | 275 | u8 *buf; |
| 276 | |
| 277 | buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL); |
| 278 | if (!buf) |
| 279 | return -ENOMEM; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 280 | |
| 281 | ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ, |
Johan Hovold | 9e221a3 | 2009-12-28 23:01:55 +0100 | [diff] [blame] | 282 | MCS_RD_RTYPE, 0, reg, buf, VENDOR_READ_LENGTH, |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 283 | MOS_WDR_TIMEOUT); |
Johan Hovold | 9e221a3 | 2009-12-28 23:01:55 +0100 | [diff] [blame] | 284 | *val = buf[0]; |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 285 | dev_dbg(&port->dev, "%s offset is %x, return val %x\n", __func__, reg, *val); |
Johan Hovold | 9e221a3 | 2009-12-28 23:01:55 +0100 | [diff] [blame] | 286 | |
| 287 | kfree(buf); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 288 | return ret; |
| 289 | } |
| 290 | |
| 291 | /* |
| 292 | * mos7840_set_uart_reg |
| 293 | * To set the Uart register by calling usb_fill_control_urb function by |
| 294 | * passing usb_sndctrlpipe function as parameter. |
| 295 | */ |
| 296 | |
| 297 | static int mos7840_set_uart_reg(struct usb_serial_port *port, __u16 reg, |
| 298 | __u16 val) |
| 299 | { |
| 300 | |
| 301 | struct usb_device *dev = port->serial->dev; |
| 302 | val = val & 0x00ff; |
Alan Cox | 880af9d | 2008-07-22 11:16:12 +0100 | [diff] [blame] | 303 | /* For the UART control registers, the application number need |
| 304 | to be Or'ed */ |
Oliver Neukum | 0de9a70 | 2007-03-16 20:28:28 +0100 | [diff] [blame] | 305 | if (port->serial->num_ports == 4) { |
Alan Cox | 880af9d | 2008-07-22 11:16:12 +0100 | [diff] [blame] | 306 | val |= (((__u16) port->number - |
| 307 | (__u16) (port->serial->minor)) + 1) << 8; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 308 | } else { |
| 309 | if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) { |
Alan Cox | 880af9d | 2008-07-22 11:16:12 +0100 | [diff] [blame] | 310 | val |= (((__u16) port->number - |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 311 | (__u16) (port->serial->minor)) + 1) << 8; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 312 | } else { |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 313 | val |= (((__u16) port->number - |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 314 | (__u16) (port->serial->minor)) + 2) << 8; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 315 | } |
| 316 | } |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 317 | dev_dbg(&port->dev, "%s application number is %x\n", __func__, val); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 318 | return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ, |
| 319 | MCS_WR_RTYPE, val, reg, NULL, 0, |
| 320 | MOS_WDR_TIMEOUT); |
| 321 | |
| 322 | } |
| 323 | |
| 324 | /* |
| 325 | * mos7840_get_uart_reg |
| 326 | * To set the Control register by calling usb_fill_control_urb function |
| 327 | * by passing usb_rcvctrlpipe function as parameter. |
| 328 | */ |
| 329 | static int mos7840_get_uart_reg(struct usb_serial_port *port, __u16 reg, |
Alan Cox | 880af9d | 2008-07-22 11:16:12 +0100 | [diff] [blame] | 330 | __u16 *val) |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 331 | { |
| 332 | struct usb_device *dev = port->serial->dev; |
| 333 | int ret = 0; |
| 334 | __u16 Wval; |
Johan Hovold | 9e221a3 | 2009-12-28 23:01:55 +0100 | [diff] [blame] | 335 | u8 *buf; |
| 336 | |
| 337 | buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL); |
| 338 | if (!buf) |
| 339 | return -ENOMEM; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 340 | |
Alan Cox | 880af9d | 2008-07-22 11:16:12 +0100 | [diff] [blame] | 341 | /* Wval is same as application number */ |
Oliver Neukum | 0de9a70 | 2007-03-16 20:28:28 +0100 | [diff] [blame] | 342 | if (port->serial->num_ports == 4) { |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 343 | Wval = |
| 344 | (((__u16) port->number - (__u16) (port->serial->minor)) + |
| 345 | 1) << 8; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 346 | } else { |
| 347 | if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) { |
Alan Cox | 880af9d | 2008-07-22 11:16:12 +0100 | [diff] [blame] | 348 | Wval = (((__u16) port->number - |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 349 | (__u16) (port->serial->minor)) + 1) << 8; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 350 | } else { |
Alan Cox | 880af9d | 2008-07-22 11:16:12 +0100 | [diff] [blame] | 351 | Wval = (((__u16) port->number - |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 352 | (__u16) (port->serial->minor)) + 2) << 8; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 353 | } |
| 354 | } |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 355 | dev_dbg(&port->dev, "%s application number is %x\n", __func__, Wval); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 356 | ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ, |
Johan Hovold | 9e221a3 | 2009-12-28 23:01:55 +0100 | [diff] [blame] | 357 | MCS_RD_RTYPE, Wval, reg, buf, VENDOR_READ_LENGTH, |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 358 | MOS_WDR_TIMEOUT); |
Johan Hovold | 9e221a3 | 2009-12-28 23:01:55 +0100 | [diff] [blame] | 359 | *val = buf[0]; |
| 360 | |
| 361 | kfree(buf); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 362 | return ret; |
| 363 | } |
| 364 | |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 365 | static void mos7840_dump_serial_port(struct usb_serial_port *port, |
| 366 | struct moschip_port *mos7840_port) |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 367 | { |
| 368 | |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 369 | dev_dbg(&port->dev, "SpRegOffset is %2x\n", mos7840_port->SpRegOffset); |
| 370 | dev_dbg(&port->dev, "ControlRegOffset is %2x\n", mos7840_port->ControlRegOffset); |
| 371 | dev_dbg(&port->dev, "DCRRegOffset is %2x\n", mos7840_port->DcrRegOffset); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 372 | |
| 373 | } |
| 374 | |
| 375 | /************************************************************************/ |
| 376 | /************************************************************************/ |
| 377 | /* I N T E R F A C E F U N C T I O N S */ |
| 378 | /* I N T E R F A C E F U N C T I O N S */ |
| 379 | /************************************************************************/ |
| 380 | /************************************************************************/ |
| 381 | |
| 382 | static inline void mos7840_set_port_private(struct usb_serial_port *port, |
| 383 | struct moschip_port *data) |
| 384 | { |
| 385 | usb_set_serial_port_data(port, (void *)data); |
| 386 | } |
| 387 | |
| 388 | static inline struct moschip_port *mos7840_get_port_private(struct |
| 389 | usb_serial_port |
| 390 | *port) |
| 391 | { |
| 392 | return (struct moschip_port *)usb_get_serial_port_data(port); |
| 393 | } |
| 394 | |
Oliver Neukum | 0de9a70 | 2007-03-16 20:28:28 +0100 | [diff] [blame] | 395 | static void mos7840_handle_new_msr(struct moschip_port *port, __u8 new_msr) |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 396 | { |
| 397 | struct moschip_port *mos7840_port; |
| 398 | struct async_icount *icount; |
| 399 | mos7840_port = port; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 400 | if (new_msr & |
| 401 | (MOS_MSR_DELTA_CTS | MOS_MSR_DELTA_DSR | MOS_MSR_DELTA_RI | |
| 402 | MOS_MSR_DELTA_CD)) { |
Johan Hovold | 8c1a07f | 2013-03-21 12:37:16 +0100 | [diff] [blame] | 403 | icount = &mos7840_port->port->icount; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 404 | |
| 405 | /* update input line counters */ |
Johan Hovold | c9fac85 | 2013-03-21 12:37:15 +0100 | [diff] [blame] | 406 | if (new_msr & MOS_MSR_DELTA_CTS) |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 407 | icount->cts++; |
Johan Hovold | c9fac85 | 2013-03-21 12:37:15 +0100 | [diff] [blame] | 408 | if (new_msr & MOS_MSR_DELTA_DSR) |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 409 | icount->dsr++; |
Johan Hovold | c9fac85 | 2013-03-21 12:37:15 +0100 | [diff] [blame] | 410 | if (new_msr & MOS_MSR_DELTA_CD) |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 411 | icount->dcd++; |
Johan Hovold | c9fac85 | 2013-03-21 12:37:15 +0100 | [diff] [blame] | 412 | if (new_msr & MOS_MSR_DELTA_RI) |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 413 | icount->rng++; |
Johan Hovold | e670c6a | 2013-03-19 09:21:19 +0100 | [diff] [blame] | 414 | |
Johan Hovold | 0c613371 | 2013-03-21 12:37:17 +0100 | [diff] [blame] | 415 | wake_up_interruptible(&port->port->port.delta_msr_wait); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 416 | } |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 417 | } |
| 418 | |
Oliver Neukum | 0de9a70 | 2007-03-16 20:28:28 +0100 | [diff] [blame] | 419 | static void mos7840_handle_new_lsr(struct moschip_port *port, __u8 new_lsr) |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 420 | { |
| 421 | struct async_icount *icount; |
| 422 | |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 423 | if (new_lsr & SERIAL_LSR_BI) { |
Alan Cox | 880af9d | 2008-07-22 11:16:12 +0100 | [diff] [blame] | 424 | /* |
| 425 | * Parity and Framing errors only count if they |
| 426 | * occur exclusive of a break being |
| 427 | * received. |
| 428 | */ |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 429 | new_lsr &= (__u8) (SERIAL_LSR_OE | SERIAL_LSR_BI); |
| 430 | } |
| 431 | |
| 432 | /* update input line counters */ |
Johan Hovold | 8c1a07f | 2013-03-21 12:37:16 +0100 | [diff] [blame] | 433 | icount = &port->port->icount; |
Johan Hovold | c9fac85 | 2013-03-21 12:37:15 +0100 | [diff] [blame] | 434 | if (new_lsr & SERIAL_LSR_BI) |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 435 | icount->brk++; |
Johan Hovold | c9fac85 | 2013-03-21 12:37:15 +0100 | [diff] [blame] | 436 | if (new_lsr & SERIAL_LSR_OE) |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 437 | icount->overrun++; |
Johan Hovold | c9fac85 | 2013-03-21 12:37:15 +0100 | [diff] [blame] | 438 | if (new_lsr & SERIAL_LSR_PE) |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 439 | icount->parity++; |
Johan Hovold | c9fac85 | 2013-03-21 12:37:15 +0100 | [diff] [blame] | 440 | if (new_lsr & SERIAL_LSR_FE) |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 441 | icount->frame++; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | /************************************************************************/ |
| 445 | /************************************************************************/ |
| 446 | /* U S B C A L L B A C K F U N C T I O N S */ |
| 447 | /* U S B C A L L B A C K F U N C T I O N S */ |
| 448 | /************************************************************************/ |
| 449 | /************************************************************************/ |
| 450 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 451 | static void mos7840_control_callback(struct urb *urb) |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 452 | { |
| 453 | unsigned char *data; |
| 454 | struct moschip_port *mos7840_port; |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 455 | struct device *dev = &urb->dev->dev; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 456 | __u8 regval = 0x0; |
Greg Kroah-Hartman | 0643c72 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 457 | int status = urb->status; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 458 | |
Ming Lei | cdc9779 | 2008-02-24 18:41:47 +0800 | [diff] [blame] | 459 | mos7840_port = urb->context; |
Oliver Neukum | 0de9a70 | 2007-03-16 20:28:28 +0100 | [diff] [blame] | 460 | |
Greg Kroah-Hartman | 0643c72 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 461 | switch (status) { |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 462 | case 0: |
| 463 | /* success */ |
| 464 | break; |
| 465 | case -ECONNRESET: |
| 466 | case -ENOENT: |
| 467 | case -ESHUTDOWN: |
| 468 | /* this urb is terminated, clean up */ |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 469 | dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 470 | return; |
| 471 | default: |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 472 | dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status); |
Johan Hovold | 28c3ae9 | 2012-10-25 18:56:32 +0200 | [diff] [blame] | 473 | return; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 474 | } |
| 475 | |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 476 | dev_dbg(dev, "%s urb buffer size is %d\n", __func__, urb->actual_length); |
| 477 | dev_dbg(dev, "%s mos7840_port->MsrLsr is %d port %d\n", __func__, |
| 478 | mos7840_port->MsrLsr, mos7840_port->port_num); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 479 | data = urb->transfer_buffer; |
| 480 | regval = (__u8) data[0]; |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 481 | dev_dbg(dev, "%s data is %x\n", __func__, regval); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 482 | if (mos7840_port->MsrLsr == 0) |
| 483 | mos7840_handle_new_msr(mos7840_port, regval); |
| 484 | else if (mos7840_port->MsrLsr == 1) |
| 485 | mos7840_handle_new_lsr(mos7840_port, regval); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | static int mos7840_get_reg(struct moschip_port *mcs, __u16 Wval, __u16 reg, |
Alan Cox | 880af9d | 2008-07-22 11:16:12 +0100 | [diff] [blame] | 489 | __u16 *val) |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 490 | { |
| 491 | struct usb_device *dev = mcs->port->serial->dev; |
Oliver Neukum | 0de9a70 | 2007-03-16 20:28:28 +0100 | [diff] [blame] | 492 | struct usb_ctrlrequest *dr = mcs->dr; |
| 493 | unsigned char *buffer = mcs->ctrl_buf; |
| 494 | int ret; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 495 | |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 496 | dr->bRequestType = MCS_RD_RTYPE; |
| 497 | dr->bRequest = MCS_RDREQ; |
Alan Cox | 880af9d | 2008-07-22 11:16:12 +0100 | [diff] [blame] | 498 | dr->wValue = cpu_to_le16(Wval); /* 0 */ |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 499 | dr->wIndex = cpu_to_le16(reg); |
| 500 | dr->wLength = cpu_to_le16(2); |
| 501 | |
| 502 | usb_fill_control_urb(mcs->control_urb, dev, usb_rcvctrlpipe(dev, 0), |
| 503 | (unsigned char *)dr, buffer, 2, |
| 504 | mos7840_control_callback, mcs); |
| 505 | mcs->control_urb->transfer_buffer_length = 2; |
| 506 | ret = usb_submit_urb(mcs->control_urb, GFP_ATOMIC); |
| 507 | return ret; |
| 508 | } |
| 509 | |
Donald | 0eafe4d | 2012-04-19 15:00:45 +0800 | [diff] [blame] | 510 | static void mos7840_set_led_callback(struct urb *urb) |
| 511 | { |
| 512 | switch (urb->status) { |
| 513 | case 0: |
| 514 | /* Success */ |
| 515 | break; |
| 516 | case -ECONNRESET: |
| 517 | case -ENOENT: |
| 518 | case -ESHUTDOWN: |
| 519 | /* This urb is terminated, clean up */ |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 520 | dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d", |
| 521 | __func__, urb->status); |
Donald | 0eafe4d | 2012-04-19 15:00:45 +0800 | [diff] [blame] | 522 | break; |
| 523 | default: |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 524 | dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d", |
| 525 | __func__, urb->status); |
Donald | 0eafe4d | 2012-04-19 15:00:45 +0800 | [diff] [blame] | 526 | } |
| 527 | } |
| 528 | |
| 529 | static void mos7840_set_led_async(struct moschip_port *mcs, __u16 wval, |
| 530 | __u16 reg) |
| 531 | { |
| 532 | struct usb_device *dev = mcs->port->serial->dev; |
| 533 | struct usb_ctrlrequest *dr = mcs->dr; |
| 534 | |
| 535 | dr->bRequestType = MCS_WR_RTYPE; |
| 536 | dr->bRequest = MCS_WRREQ; |
| 537 | dr->wValue = cpu_to_le16(wval); |
| 538 | dr->wIndex = cpu_to_le16(reg); |
| 539 | dr->wLength = cpu_to_le16(0); |
| 540 | |
| 541 | usb_fill_control_urb(mcs->control_urb, dev, usb_sndctrlpipe(dev, 0), |
| 542 | (unsigned char *)dr, NULL, 0, mos7840_set_led_callback, NULL); |
| 543 | |
| 544 | usb_submit_urb(mcs->control_urb, GFP_ATOMIC); |
| 545 | } |
| 546 | |
| 547 | static void mos7840_set_led_sync(struct usb_serial_port *port, __u16 reg, |
| 548 | __u16 val) |
| 549 | { |
| 550 | struct usb_device *dev = port->serial->dev; |
| 551 | |
| 552 | usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ, MCS_WR_RTYPE, |
| 553 | val, reg, NULL, 0, MOS_WDR_TIMEOUT); |
| 554 | } |
| 555 | |
| 556 | static void mos7840_led_off(unsigned long arg) |
| 557 | { |
| 558 | struct moschip_port *mcs = (struct moschip_port *) arg; |
| 559 | |
| 560 | /* Turn off LED */ |
| 561 | mos7840_set_led_async(mcs, 0x0300, MODEM_CONTROL_REGISTER); |
| 562 | mod_timer(&mcs->led_timer2, |
| 563 | jiffies + msecs_to_jiffies(LED_OFF_MS)); |
| 564 | } |
| 565 | |
| 566 | static void mos7840_led_flag_off(unsigned long arg) |
| 567 | { |
| 568 | struct moschip_port *mcs = (struct moschip_port *) arg; |
| 569 | |
| 570 | mcs->led_flag = false; |
| 571 | } |
| 572 | |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 573 | /***************************************************************************** |
| 574 | * mos7840_interrupt_callback |
| 575 | * this is the callback function for when we have received data on the |
| 576 | * interrupt endpoint. |
| 577 | *****************************************************************************/ |
| 578 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 579 | static void mos7840_interrupt_callback(struct urb *urb) |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 580 | { |
| 581 | int result; |
| 582 | int length; |
| 583 | struct moschip_port *mos7840_port; |
| 584 | struct usb_serial *serial; |
| 585 | __u16 Data; |
| 586 | unsigned char *data; |
| 587 | __u8 sp[5], st; |
Oliver Neukum | 0de9a70 | 2007-03-16 20:28:28 +0100 | [diff] [blame] | 588 | int i, rv = 0; |
| 589 | __u16 wval, wreg = 0; |
Greg Kroah-Hartman | 0643c72 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 590 | int status = urb->status; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 591 | |
Greg Kroah-Hartman | 0643c72 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 592 | switch (status) { |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 593 | case 0: |
| 594 | /* success */ |
| 595 | break; |
| 596 | case -ECONNRESET: |
| 597 | case -ENOENT: |
| 598 | case -ESHUTDOWN: |
| 599 | /* this urb is terminated, clean up */ |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 600 | dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n", |
| 601 | __func__, status); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 602 | return; |
| 603 | default: |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 604 | dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n", |
| 605 | __func__, status); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 606 | goto exit; |
| 607 | } |
| 608 | |
| 609 | length = urb->actual_length; |
| 610 | data = urb->transfer_buffer; |
| 611 | |
Ming Lei | cdc9779 | 2008-02-24 18:41:47 +0800 | [diff] [blame] | 612 | serial = urb->context; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 613 | |
| 614 | /* Moschip get 5 bytes |
| 615 | * Byte 1 IIR Port 1 (port.number is 0) |
| 616 | * Byte 2 IIR Port 2 (port.number is 1) |
| 617 | * Byte 3 IIR Port 3 (port.number is 2) |
| 618 | * Byte 4 IIR Port 4 (port.number is 3) |
| 619 | * Byte 5 FIFO status for both */ |
| 620 | |
| 621 | if (length && length > 5) { |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 622 | dev_dbg(&urb->dev->dev, "%s", "Wrong data !!!\n"); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 623 | return; |
| 624 | } |
| 625 | |
| 626 | sp[0] = (__u8) data[0]; |
| 627 | sp[1] = (__u8) data[1]; |
| 628 | sp[2] = (__u8) data[2]; |
| 629 | sp[3] = (__u8) data[3]; |
| 630 | st = (__u8) data[4]; |
| 631 | |
| 632 | for (i = 0; i < serial->num_ports; i++) { |
| 633 | mos7840_port = mos7840_get_port_private(serial->port[i]); |
| 634 | wval = |
| 635 | (((__u16) serial->port[i]->number - |
| 636 | (__u16) (serial->minor)) + 1) << 8; |
| 637 | if (mos7840_port->open) { |
| 638 | if (sp[i] & 0x01) { |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 639 | dev_dbg(&urb->dev->dev, "SP%d No Interrupt !!!\n", i); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 640 | } else { |
| 641 | switch (sp[i] & 0x0f) { |
| 642 | case SERIAL_IIR_RLS: |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 643 | dev_dbg(&urb->dev->dev, "Serial Port %d: Receiver status error or \n", i); |
| 644 | dev_dbg(&urb->dev->dev, "address bit detected in 9-bit mode\n"); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 645 | mos7840_port->MsrLsr = 1; |
Oliver Neukum | 0de9a70 | 2007-03-16 20:28:28 +0100 | [diff] [blame] | 646 | wreg = LINE_STATUS_REGISTER; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 647 | break; |
| 648 | case SERIAL_IIR_MS: |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 649 | dev_dbg(&urb->dev->dev, "Serial Port %d: Modem status change\n", i); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 650 | mos7840_port->MsrLsr = 0; |
Oliver Neukum | 0de9a70 | 2007-03-16 20:28:28 +0100 | [diff] [blame] | 651 | wreg = MODEM_STATUS_REGISTER; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 652 | break; |
| 653 | } |
Johan Hovold | e681b66 | 2012-10-25 18:56:33 +0200 | [diff] [blame] | 654 | rv = mos7840_get_reg(mos7840_port, wval, wreg, &Data); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 655 | } |
| 656 | } |
| 657 | } |
Alan Cox | 880af9d | 2008-07-22 11:16:12 +0100 | [diff] [blame] | 658 | if (!(rv < 0)) |
| 659 | /* the completion handler for the control urb will resubmit */ |
Oliver Neukum | 0de9a70 | 2007-03-16 20:28:28 +0100 | [diff] [blame] | 660 | return; |
| 661 | exit: |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 662 | result = usb_submit_urb(urb, GFP_ATOMIC); |
| 663 | if (result) { |
| 664 | dev_err(&urb->dev->dev, |
| 665 | "%s - Error %d submitting interrupt urb\n", |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 666 | __func__, result); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 667 | } |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | static int mos7840_port_paranoia_check(struct usb_serial_port *port, |
| 671 | const char *function) |
| 672 | { |
| 673 | if (!port) { |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 674 | pr_debug("%s - port == NULL\n", function); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 675 | return -1; |
| 676 | } |
| 677 | if (!port->serial) { |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 678 | pr_debug("%s - port->serial == NULL\n", function); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 679 | return -1; |
| 680 | } |
| 681 | |
| 682 | return 0; |
| 683 | } |
| 684 | |
| 685 | /* Inline functions to check the sanity of a pointer that is passed to us */ |
| 686 | static int mos7840_serial_paranoia_check(struct usb_serial *serial, |
| 687 | const char *function) |
| 688 | { |
| 689 | if (!serial) { |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 690 | pr_debug("%s - serial == NULL\n", function); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 691 | return -1; |
| 692 | } |
| 693 | if (!serial->type) { |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 694 | pr_debug("%s - serial->type == NULL!\n", function); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 695 | return -1; |
| 696 | } |
| 697 | |
| 698 | return 0; |
| 699 | } |
| 700 | |
| 701 | static struct usb_serial *mos7840_get_usb_serial(struct usb_serial_port *port, |
| 702 | const char *function) |
| 703 | { |
| 704 | /* if no port was specified, or it fails a paranoia check */ |
| 705 | if (!port || |
| 706 | mos7840_port_paranoia_check(port, function) || |
| 707 | mos7840_serial_paranoia_check(port->serial, function)) { |
Alan Cox | 880af9d | 2008-07-22 11:16:12 +0100 | [diff] [blame] | 708 | /* then say that we don't have a valid usb_serial thing, |
| 709 | * which will end up genrating -ENODEV return values */ |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 710 | return NULL; |
| 711 | } |
| 712 | |
| 713 | return port->serial; |
| 714 | } |
| 715 | |
| 716 | /***************************************************************************** |
| 717 | * mos7840_bulk_in_callback |
| 718 | * this is the callback function for when we have received data on the |
| 719 | * bulk in endpoint. |
| 720 | *****************************************************************************/ |
| 721 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 722 | static void mos7840_bulk_in_callback(struct urb *urb) |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 723 | { |
Greg Kroah-Hartman | 0643c72 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 724 | int retval; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 725 | unsigned char *data; |
| 726 | struct usb_serial *serial; |
| 727 | struct usb_serial_port *port; |
| 728 | struct moschip_port *mos7840_port; |
Greg Kroah-Hartman | 0643c72 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 729 | int status = urb->status; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 730 | |
Ming Lei | cdc9779 | 2008-02-24 18:41:47 +0800 | [diff] [blame] | 731 | mos7840_port = urb->context; |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 732 | if (!mos7840_port) |
Greg Kroah-Hartman | 50de36f | 2008-12-10 16:00:30 -0800 | [diff] [blame] | 733 | return; |
Greg Kroah-Hartman | 50de36f | 2008-12-10 16:00:30 -0800 | [diff] [blame] | 734 | |
| 735 | if (status) { |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 736 | dev_dbg(&urb->dev->dev, "nonzero read bulk status received: %d\n", status); |
Greg Kroah-Hartman | 50de36f | 2008-12-10 16:00:30 -0800 | [diff] [blame] | 737 | mos7840_port->read_urb_busy = false; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 738 | return; |
| 739 | } |
| 740 | |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 741 | port = mos7840_port->port; |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 742 | if (mos7840_port_paranoia_check(port, __func__)) { |
Greg Kroah-Hartman | 50de36f | 2008-12-10 16:00:30 -0800 | [diff] [blame] | 743 | mos7840_port->read_urb_busy = false; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 744 | return; |
| 745 | } |
| 746 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 747 | serial = mos7840_get_usb_serial(port, __func__); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 748 | if (!serial) { |
Greg Kroah-Hartman | 50de36f | 2008-12-10 16:00:30 -0800 | [diff] [blame] | 749 | mos7840_port->read_urb_busy = false; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 750 | return; |
| 751 | } |
| 752 | |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 753 | data = urb->transfer_buffer; |
Greg Kroah-Hartman | 59d33f2 | 2012-09-18 09:58:57 +0100 | [diff] [blame] | 754 | usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 755 | |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 756 | if (urb->actual_length) { |
Jiri Slaby | 05c7cd3 | 2013-01-03 15:53:04 +0100 | [diff] [blame] | 757 | struct tty_port *tport = &mos7840_port->port->port; |
Jiri Slaby | 2e124b4 | 2013-01-03 15:53:06 +0100 | [diff] [blame] | 758 | tty_insert_flip_string(tport, data, urb->actual_length); |
| 759 | tty_flip_buffer_push(tport); |
Johan Hovold | 8c1a07f | 2013-03-21 12:37:16 +0100 | [diff] [blame] | 760 | port->icount.rx += urb->actual_length; |
| 761 | dev_dbg(&port->dev, "icount.rx is %d:\n", port->icount.rx); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | if (!mos7840_port->read_urb) { |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 765 | dev_dbg(&port->dev, "%s", "URB KILLED !!!\n"); |
Greg Kroah-Hartman | 50de36f | 2008-12-10 16:00:30 -0800 | [diff] [blame] | 766 | mos7840_port->read_urb_busy = false; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 767 | return; |
| 768 | } |
| 769 | |
Donald | 0eafe4d | 2012-04-19 15:00:45 +0800 | [diff] [blame] | 770 | /* Turn on LED */ |
| 771 | if (mos7840_port->has_led && !mos7840_port->led_flag) { |
| 772 | mos7840_port->led_flag = true; |
| 773 | mos7840_set_led_async(mos7840_port, 0x0301, |
| 774 | MODEM_CONTROL_REGISTER); |
| 775 | mod_timer(&mos7840_port->led_timer1, |
| 776 | jiffies + msecs_to_jiffies(LED_ON_MS)); |
| 777 | } |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 778 | |
Greg Kroah-Hartman | 50de36f | 2008-12-10 16:00:30 -0800 | [diff] [blame] | 779 | mos7840_port->read_urb_busy = true; |
Greg Kroah-Hartman | 0643c72 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 780 | retval = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC); |
Oliver Neukum | 0de9a70 | 2007-03-16 20:28:28 +0100 | [diff] [blame] | 781 | |
Greg Kroah-Hartman | 0643c72 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 782 | if (retval) { |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 783 | dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, retval = %d\n", retval); |
Greg Kroah-Hartman | 50de36f | 2008-12-10 16:00:30 -0800 | [diff] [blame] | 784 | mos7840_port->read_urb_busy = false; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 785 | } |
| 786 | } |
| 787 | |
| 788 | /***************************************************************************** |
| 789 | * mos7840_bulk_out_data_callback |
Alan Cox | 880af9d | 2008-07-22 11:16:12 +0100 | [diff] [blame] | 790 | * this is the callback function for when we have finished sending |
| 791 | * serial data on the bulk out endpoint. |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 792 | *****************************************************************************/ |
| 793 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 794 | static void mos7840_bulk_out_data_callback(struct urb *urb) |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 795 | { |
| 796 | struct moschip_port *mos7840_port; |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 797 | struct usb_serial_port *port; |
Greg Kroah-Hartman | 0643c72 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 798 | int status = urb->status; |
Oliver Neukum | 0de9a70 | 2007-03-16 20:28:28 +0100 | [diff] [blame] | 799 | int i; |
| 800 | |
Ming Lei | cdc9779 | 2008-02-24 18:41:47 +0800 | [diff] [blame] | 801 | mos7840_port = urb->context; |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 802 | port = mos7840_port->port; |
Oliver Neukum | 0de9a70 | 2007-03-16 20:28:28 +0100 | [diff] [blame] | 803 | spin_lock(&mos7840_port->pool_lock); |
| 804 | for (i = 0; i < NUM_URBS; i++) { |
| 805 | if (urb == mos7840_port->write_urb_pool[i]) { |
| 806 | mos7840_port->busy[i] = 0; |
| 807 | break; |
| 808 | } |
| 809 | } |
| 810 | spin_unlock(&mos7840_port->pool_lock); |
| 811 | |
Greg Kroah-Hartman | 0643c72 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 812 | if (status) { |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 813 | dev_dbg(&port->dev, "nonzero write bulk status received:%d\n", status); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 814 | return; |
| 815 | } |
| 816 | |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 817 | if (mos7840_port_paranoia_check(port, __func__)) |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 818 | return; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 819 | |
Jiri Slaby | 6aad04f | 2013-03-07 13:12:29 +0100 | [diff] [blame] | 820 | if (mos7840_port->open) |
| 821 | tty_port_tty_wakeup(&port->port); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 822 | |
| 823 | } |
| 824 | |
| 825 | /************************************************************************/ |
| 826 | /* 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 */ |
| 827 | /************************************************************************/ |
| 828 | #ifdef MCSSerialProbe |
| 829 | static int mos7840_serial_probe(struct usb_serial *serial, |
| 830 | const struct usb_device_id *id) |
| 831 | { |
| 832 | |
| 833 | /*need to implement the mode_reg reading and updating\ |
| 834 | structures usb_serial_ device_type\ |
| 835 | (i.e num_ports, num_bulkin,bulkout etc) */ |
| 836 | /* Also we can update the changes attach */ |
| 837 | return 1; |
| 838 | } |
| 839 | #endif |
| 840 | |
| 841 | /***************************************************************************** |
| 842 | * mos7840_open |
| 843 | * this function is called by the tty driver when a port is opened |
| 844 | * If successful, we return 0 |
| 845 | * Otherwise we return a negative error number. |
| 846 | *****************************************************************************/ |
| 847 | |
Alan Cox | a509a7e | 2009-09-19 13:13:26 -0700 | [diff] [blame] | 848 | static int mos7840_open(struct tty_struct *tty, struct usb_serial_port *port) |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 849 | { |
| 850 | int response; |
| 851 | int j; |
| 852 | struct usb_serial *serial; |
| 853 | struct urb *urb; |
| 854 | __u16 Data; |
| 855 | int status; |
| 856 | struct moschip_port *mos7840_port; |
Oliver Neukum | 0de9a70 | 2007-03-16 20:28:28 +0100 | [diff] [blame] | 857 | struct moschip_port *port0; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 858 | |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 859 | if (mos7840_port_paranoia_check(port, __func__)) |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 860 | return -ENODEV; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 861 | |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 862 | serial = port->serial; |
| 863 | |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 864 | if (mos7840_serial_paranoia_check(serial, __func__)) |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 865 | return -ENODEV; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 866 | |
| 867 | mos7840_port = mos7840_get_port_private(port); |
Oliver Neukum | 0de9a70 | 2007-03-16 20:28:28 +0100 | [diff] [blame] | 868 | port0 = mos7840_get_port_private(serial->port[0]); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 869 | |
Oliver Neukum | 0de9a70 | 2007-03-16 20:28:28 +0100 | [diff] [blame] | 870 | if (mos7840_port == NULL || port0 == NULL) |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 871 | return -ENODEV; |
| 872 | |
| 873 | usb_clear_halt(serial->dev, port->write_urb->pipe); |
| 874 | usb_clear_halt(serial->dev, port->read_urb->pipe); |
Oliver Neukum | 0de9a70 | 2007-03-16 20:28:28 +0100 | [diff] [blame] | 875 | port0->open_ports++; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 876 | |
| 877 | /* Initialising the write urb pool */ |
| 878 | for (j = 0; j < NUM_URBS; ++j) { |
Oliver Neukum | 0de9a70 | 2007-03-16 20:28:28 +0100 | [diff] [blame] | 879 | urb = usb_alloc_urb(0, GFP_KERNEL); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 880 | mos7840_port->write_urb_pool[j] = urb; |
| 881 | |
| 882 | if (urb == NULL) { |
Greg Kroah-Hartman | 194343d | 2008-08-20 16:56:34 -0700 | [diff] [blame] | 883 | dev_err(&port->dev, "No more urbs???\n"); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 884 | continue; |
| 885 | } |
| 886 | |
Alan Cox | 880af9d | 2008-07-22 11:16:12 +0100 | [diff] [blame] | 887 | urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE, |
| 888 | GFP_KERNEL); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 889 | if (!urb->transfer_buffer) { |
Oliver Neukum | 0de9a70 | 2007-03-16 20:28:28 +0100 | [diff] [blame] | 890 | usb_free_urb(urb); |
| 891 | mos7840_port->write_urb_pool[j] = NULL; |
Greg Kroah-Hartman | 194343d | 2008-08-20 16:56:34 -0700 | [diff] [blame] | 892 | dev_err(&port->dev, |
| 893 | "%s-out of memory for urb buffers.\n", |
| 894 | __func__); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 895 | continue; |
| 896 | } |
| 897 | } |
| 898 | |
| 899 | /***************************************************************************** |
| 900 | * Initialize MCS7840 -- Write Init values to corresponding Registers |
| 901 | * |
| 902 | * Register Index |
| 903 | * 1 : IER |
| 904 | * 2 : FCR |
| 905 | * 3 : LCR |
| 906 | * 4 : MCR |
| 907 | * |
| 908 | * 0x08 : SP1/2 Control Reg |
| 909 | *****************************************************************************/ |
| 910 | |
Alan Cox | 880af9d | 2008-07-22 11:16:12 +0100 | [diff] [blame] | 911 | /* NEED to check the following Block */ |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 912 | |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 913 | Data = 0x0; |
| 914 | status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data); |
| 915 | if (status < 0) { |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 916 | dev_dbg(&port->dev, "Reading Spreg failed\n"); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 917 | return -1; |
| 918 | } |
| 919 | Data |= 0x80; |
| 920 | status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data); |
| 921 | if (status < 0) { |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 922 | dev_dbg(&port->dev, "writing Spreg failed\n"); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 923 | return -1; |
| 924 | } |
| 925 | |
| 926 | Data &= ~0x80; |
| 927 | status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data); |
| 928 | if (status < 0) { |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 929 | dev_dbg(&port->dev, "writing Spreg failed\n"); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 930 | return -1; |
| 931 | } |
Alan Cox | 880af9d | 2008-07-22 11:16:12 +0100 | [diff] [blame] | 932 | /* End of block to be checked */ |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 933 | |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 934 | Data = 0x0; |
Alan Cox | 880af9d | 2008-07-22 11:16:12 +0100 | [diff] [blame] | 935 | status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset, |
| 936 | &Data); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 937 | if (status < 0) { |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 938 | dev_dbg(&port->dev, "Reading Controlreg failed\n"); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 939 | return -1; |
| 940 | } |
Alan Cox | 880af9d | 2008-07-22 11:16:12 +0100 | [diff] [blame] | 941 | Data |= 0x08; /* Driver done bit */ |
| 942 | Data |= 0x20; /* rx_disable */ |
| 943 | status = mos7840_set_reg_sync(port, |
| 944 | mos7840_port->ControlRegOffset, Data); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 945 | if (status < 0) { |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 946 | dev_dbg(&port->dev, "writing Controlreg failed\n"); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 947 | return -1; |
| 948 | } |
Alan Cox | 880af9d | 2008-07-22 11:16:12 +0100 | [diff] [blame] | 949 | /* do register settings here */ |
| 950 | /* Set all regs to the device default values. */ |
| 951 | /*********************************** |
| 952 | * First Disable all interrupts. |
| 953 | ***********************************/ |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 954 | Data = 0x00; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 955 | status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data); |
| 956 | if (status < 0) { |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 957 | dev_dbg(&port->dev, "disabling interrupts failed\n"); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 958 | return -1; |
| 959 | } |
Alan Cox | 880af9d | 2008-07-22 11:16:12 +0100 | [diff] [blame] | 960 | /* Set FIFO_CONTROL_REGISTER to the default value */ |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 961 | Data = 0x00; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 962 | status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data); |
| 963 | if (status < 0) { |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 964 | dev_dbg(&port->dev, "Writing FIFO_CONTROL_REGISTER failed\n"); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 965 | return -1; |
| 966 | } |
| 967 | |
| 968 | Data = 0xcf; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 969 | status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data); |
| 970 | if (status < 0) { |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 971 | dev_dbg(&port->dev, "Writing FIFO_CONTROL_REGISTER failed\n"); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 972 | return -1; |
| 973 | } |
| 974 | |
| 975 | Data = 0x03; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 976 | status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data); |
| 977 | mos7840_port->shadowLCR = Data; |
| 978 | |
| 979 | Data = 0x0b; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 980 | status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data); |
| 981 | mos7840_port->shadowMCR = Data; |
| 982 | |
| 983 | Data = 0x00; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 984 | status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data); |
| 985 | mos7840_port->shadowLCR = Data; |
| 986 | |
Alan Cox | 880af9d | 2008-07-22 11:16:12 +0100 | [diff] [blame] | 987 | Data |= SERIAL_LCR_DLAB; /* data latch enable in LCR 0x80 */ |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 988 | status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data); |
| 989 | |
| 990 | Data = 0x0c; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 991 | status = mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data); |
| 992 | |
| 993 | Data = 0x0; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 994 | status = mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data); |
| 995 | |
| 996 | Data = 0x00; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 997 | status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data); |
| 998 | |
| 999 | Data = Data & ~SERIAL_LCR_DLAB; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1000 | status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data); |
| 1001 | mos7840_port->shadowLCR = Data; |
| 1002 | |
Alan Cox | 880af9d | 2008-07-22 11:16:12 +0100 | [diff] [blame] | 1003 | /* clearing Bulkin and Bulkout Fifo */ |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1004 | Data = 0x0; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1005 | status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data); |
| 1006 | |
| 1007 | Data = Data | 0x0c; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1008 | status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data); |
| 1009 | |
| 1010 | Data = Data & ~0x0c; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1011 | status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data); |
Alan Cox | 880af9d | 2008-07-22 11:16:12 +0100 | [diff] [blame] | 1012 | /* Finally enable all interrupts */ |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1013 | Data = 0x0c; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1014 | status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data); |
| 1015 | |
Alan Cox | 880af9d | 2008-07-22 11:16:12 +0100 | [diff] [blame] | 1016 | /* clearing rx_disable */ |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1017 | Data = 0x0; |
Alan Cox | 880af9d | 2008-07-22 11:16:12 +0100 | [diff] [blame] | 1018 | status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset, |
| 1019 | &Data); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1020 | Data = Data & ~0x20; |
Alan Cox | 880af9d | 2008-07-22 11:16:12 +0100 | [diff] [blame] | 1021 | status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset, |
| 1022 | Data); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1023 | |
Alan Cox | 880af9d | 2008-07-22 11:16:12 +0100 | [diff] [blame] | 1024 | /* rx_negate */ |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1025 | Data = 0x0; |
Alan Cox | 880af9d | 2008-07-22 11:16:12 +0100 | [diff] [blame] | 1026 | status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset, |
| 1027 | &Data); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1028 | Data = Data | 0x10; |
Alan Cox | 880af9d | 2008-07-22 11:16:12 +0100 | [diff] [blame] | 1029 | status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset, |
| 1030 | Data); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1031 | |
Alan Cox | 880af9d | 2008-07-22 11:16:12 +0100 | [diff] [blame] | 1032 | /* Check to see if we've set up our endpoint info yet * |
| 1033 | * (can't set it up in mos7840_startup as the structures * |
| 1034 | * were not set up at that time.) */ |
Oliver Neukum | 0de9a70 | 2007-03-16 20:28:28 +0100 | [diff] [blame] | 1035 | if (port0->open_ports == 1) { |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1036 | if (serial->port[0]->interrupt_in_buffer == NULL) { |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1037 | /* set up interrupt urb */ |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1038 | usb_fill_int_urb(serial->port[0]->interrupt_in_urb, |
Alan Cox | 880af9d | 2008-07-22 11:16:12 +0100 | [diff] [blame] | 1039 | serial->dev, |
| 1040 | usb_rcvintpipe(serial->dev, |
| 1041 | serial->port[0]->interrupt_in_endpointAddress), |
| 1042 | serial->port[0]->interrupt_in_buffer, |
| 1043 | serial->port[0]->interrupt_in_urb-> |
| 1044 | transfer_buffer_length, |
| 1045 | mos7840_interrupt_callback, |
| 1046 | serial, |
| 1047 | serial->port[0]->interrupt_in_urb->interval); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1048 | |
| 1049 | /* start interrupt read for mos7840 * |
| 1050 | * will continue as long as mos7840 is connected */ |
| 1051 | |
| 1052 | response = |
| 1053 | usb_submit_urb(serial->port[0]->interrupt_in_urb, |
| 1054 | GFP_KERNEL); |
| 1055 | if (response) { |
Greg Kroah-Hartman | 194343d | 2008-08-20 16:56:34 -0700 | [diff] [blame] | 1056 | dev_err(&port->dev, "%s - Error %d submitting " |
| 1057 | "interrupt urb\n", __func__, response); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1058 | } |
| 1059 | |
| 1060 | } |
| 1061 | |
| 1062 | } |
| 1063 | |
| 1064 | /* see if we've set up our endpoint info yet * |
| 1065 | * (can't set it up in mos7840_startup as the * |
| 1066 | * structures were not set up at that time.) */ |
| 1067 | |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 1068 | dev_dbg(&port->dev, "port number is %d\n", port->number); |
| 1069 | dev_dbg(&port->dev, "serial number is %d\n", port->serial->minor); |
| 1070 | dev_dbg(&port->dev, "Bulkin endpoint is %d\n", port->bulk_in_endpointAddress); |
| 1071 | dev_dbg(&port->dev, "BulkOut endpoint is %d\n", port->bulk_out_endpointAddress); |
| 1072 | dev_dbg(&port->dev, "Interrupt endpoint is %d\n", port->interrupt_in_endpointAddress); |
| 1073 | dev_dbg(&port->dev, "port's number in the device is %d\n", mos7840_port->port_num); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1074 | mos7840_port->read_urb = port->read_urb; |
| 1075 | |
| 1076 | /* set up our bulk in urb */ |
Donald Lee | 093ea2d | 2012-03-14 15:26:33 +0800 | [diff] [blame] | 1077 | if ((serial->num_ports == 2) |
| 1078 | && ((((__u16)port->number - |
| 1079 | (__u16)(port->serial->minor)) % 2) != 0)) { |
| 1080 | usb_fill_bulk_urb(mos7840_port->read_urb, |
| 1081 | serial->dev, |
| 1082 | usb_rcvbulkpipe(serial->dev, |
| 1083 | (port->bulk_in_endpointAddress) + 2), |
| 1084 | port->bulk_in_buffer, |
| 1085 | mos7840_port->read_urb->transfer_buffer_length, |
| 1086 | mos7840_bulk_in_callback, mos7840_port); |
| 1087 | } else { |
| 1088 | usb_fill_bulk_urb(mos7840_port->read_urb, |
| 1089 | serial->dev, |
| 1090 | usb_rcvbulkpipe(serial->dev, |
| 1091 | port->bulk_in_endpointAddress), |
| 1092 | port->bulk_in_buffer, |
| 1093 | mos7840_port->read_urb->transfer_buffer_length, |
| 1094 | mos7840_bulk_in_callback, mos7840_port); |
| 1095 | } |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1096 | |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 1097 | dev_dbg(&port->dev, "%s: bulkin endpoint is %d\n", __func__, port->bulk_in_endpointAddress); |
Greg Kroah-Hartman | 50de36f | 2008-12-10 16:00:30 -0800 | [diff] [blame] | 1098 | mos7840_port->read_urb_busy = true; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1099 | response = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL); |
| 1100 | if (response) { |
Greg Kroah-Hartman | 194343d | 2008-08-20 16:56:34 -0700 | [diff] [blame] | 1101 | dev_err(&port->dev, "%s - Error %d submitting control urb\n", |
| 1102 | __func__, response); |
Greg Kroah-Hartman | 50de36f | 2008-12-10 16:00:30 -0800 | [diff] [blame] | 1103 | mos7840_port->read_urb_busy = false; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1104 | } |
| 1105 | |
| 1106 | /* initialize our wait queues */ |
| 1107 | init_waitqueue_head(&mos7840_port->wait_chase); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1108 | |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1109 | /* initialize our port settings */ |
Alan Cox | 880af9d | 2008-07-22 11:16:12 +0100 | [diff] [blame] | 1110 | /* Must set to enable ints! */ |
| 1111 | mos7840_port->shadowMCR = MCR_MASTER_IE; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1112 | /* send a open port command */ |
| 1113 | mos7840_port->open = 1; |
Alan Cox | 880af9d | 2008-07-22 11:16:12 +0100 | [diff] [blame] | 1114 | /* mos7840_change_port_settings(mos7840_port,old_termios); */ |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1115 | |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1116 | return 0; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1117 | } |
| 1118 | |
| 1119 | /***************************************************************************** |
| 1120 | * mos7840_chars_in_buffer |
| 1121 | * this function is called by the tty driver when it wants to know how many |
| 1122 | * bytes of data we currently have outstanding in the port (data that has |
| 1123 | * been written, but hasn't made it out the port yet) |
| 1124 | * If successful, we return the number of bytes left to be written in the |
| 1125 | * system, |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 1126 | * Otherwise we return zero. |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1127 | *****************************************************************************/ |
| 1128 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 1129 | static int mos7840_chars_in_buffer(struct tty_struct *tty) |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1130 | { |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 1131 | struct usb_serial_port *port = tty->driver_data; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1132 | int i; |
| 1133 | int chars = 0; |
Oliver Neukum | 0de9a70 | 2007-03-16 20:28:28 +0100 | [diff] [blame] | 1134 | unsigned long flags; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1135 | struct moschip_port *mos7840_port; |
| 1136 | |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 1137 | if (mos7840_port_paranoia_check(port, __func__)) |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 1138 | return 0; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1139 | |
| 1140 | mos7840_port = mos7840_get_port_private(port); |
Greg Kroah-Hartman | 3363155 | 2012-05-03 16:44:33 -0700 | [diff] [blame] | 1141 | if (mos7840_port == NULL) |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 1142 | return 0; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1143 | |
Alan Cox | 880af9d | 2008-07-22 11:16:12 +0100 | [diff] [blame] | 1144 | spin_lock_irqsave(&mos7840_port->pool_lock, flags); |
Mark Ferrell | 5c263b9 | 2012-07-24 14:15:13 -0500 | [diff] [blame] | 1145 | for (i = 0; i < NUM_URBS; ++i) { |
| 1146 | if (mos7840_port->busy[i]) { |
| 1147 | struct urb *urb = mos7840_port->write_urb_pool[i]; |
| 1148 | chars += urb->transfer_buffer_length; |
| 1149 | } |
| 1150 | } |
Alan Cox | 880af9d | 2008-07-22 11:16:12 +0100 | [diff] [blame] | 1151 | spin_unlock_irqrestore(&mos7840_port->pool_lock, flags); |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 1152 | dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars); |
Oliver Neukum | 0de9a70 | 2007-03-16 20:28:28 +0100 | [diff] [blame] | 1153 | return chars; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1154 | |
| 1155 | } |
| 1156 | |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1157 | /***************************************************************************** |
| 1158 | * mos7840_close |
| 1159 | * this function is called by the tty driver when a port is closed |
| 1160 | *****************************************************************************/ |
| 1161 | |
Alan Cox | 335f851 | 2009-06-11 12:26:29 +0100 | [diff] [blame] | 1162 | static void mos7840_close(struct usb_serial_port *port) |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1163 | { |
| 1164 | struct usb_serial *serial; |
| 1165 | struct moschip_port *mos7840_port; |
Oliver Neukum | 0de9a70 | 2007-03-16 20:28:28 +0100 | [diff] [blame] | 1166 | struct moschip_port *port0; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1167 | int j; |
| 1168 | __u16 Data; |
| 1169 | |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 1170 | if (mos7840_port_paranoia_check(port, __func__)) |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1171 | return; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1172 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1173 | serial = mos7840_get_usb_serial(port, __func__); |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 1174 | if (!serial) |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1175 | return; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1176 | |
| 1177 | mos7840_port = mos7840_get_port_private(port); |
Oliver Neukum | 0de9a70 | 2007-03-16 20:28:28 +0100 | [diff] [blame] | 1178 | port0 = mos7840_get_port_private(serial->port[0]); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1179 | |
Oliver Neukum | 0de9a70 | 2007-03-16 20:28:28 +0100 | [diff] [blame] | 1180 | if (mos7840_port == NULL || port0 == NULL) |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1181 | return; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1182 | |
| 1183 | for (j = 0; j < NUM_URBS; ++j) |
| 1184 | usb_kill_urb(mos7840_port->write_urb_pool[j]); |
| 1185 | |
| 1186 | /* Freeing Write URBs */ |
| 1187 | for (j = 0; j < NUM_URBS; ++j) { |
| 1188 | if (mos7840_port->write_urb_pool[j]) { |
| 1189 | if (mos7840_port->write_urb_pool[j]->transfer_buffer) |
| 1190 | kfree(mos7840_port->write_urb_pool[j]-> |
| 1191 | transfer_buffer); |
| 1192 | |
| 1193 | usb_free_urb(mos7840_port->write_urb_pool[j]); |
| 1194 | } |
| 1195 | } |
| 1196 | |
Johan Hovold | bb3529c | 2013-03-21 12:36:35 +0100 | [diff] [blame] | 1197 | usb_kill_urb(mos7840_port->write_urb); |
| 1198 | usb_kill_urb(mos7840_port->read_urb); |
| 1199 | mos7840_port->read_urb_busy = false; |
| 1200 | |
Oliver Neukum | 0de9a70 | 2007-03-16 20:28:28 +0100 | [diff] [blame] | 1201 | port0->open_ports--; |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 1202 | dev_dbg(&port->dev, "%s in close%d:in port%d\n", __func__, port0->open_ports, port->number); |
Oliver Neukum | 0de9a70 | 2007-03-16 20:28:28 +0100 | [diff] [blame] | 1203 | if (port0->open_ports == 0) { |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1204 | if (serial->port[0]->interrupt_in_urb) { |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 1205 | dev_dbg(&port->dev, "Shutdown interrupt_in_urb\n"); |
Oliver Neukum | 0de9a70 | 2007-03-16 20:28:28 +0100 | [diff] [blame] | 1206 | usb_kill_urb(serial->port[0]->interrupt_in_urb); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1207 | } |
| 1208 | } |
| 1209 | |
| 1210 | if (mos7840_port->write_urb) { |
| 1211 | /* if this urb had a transfer buffer already (old tx) free it */ |
Syam Sidhardhan | ae8d487 | 2013-03-07 01:51:12 +0530 | [diff] [blame] | 1212 | kfree(mos7840_port->write_urb->transfer_buffer); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1213 | usb_free_urb(mos7840_port->write_urb); |
| 1214 | } |
| 1215 | |
| 1216 | Data = 0x0; |
| 1217 | mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data); |
| 1218 | |
| 1219 | Data = 0x00; |
| 1220 | mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data); |
| 1221 | |
| 1222 | mos7840_port->open = 0; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1223 | } |
| 1224 | |
| 1225 | /************************************************************************ |
| 1226 | * |
| 1227 | * mos7840_block_until_chase_response |
| 1228 | * |
| 1229 | * This function will block the close until one of the following: |
| 1230 | * 1. Response to our Chase comes from mos7840 |
Joe Perches | dc0d5c1 | 2007-12-17 11:40:18 -0800 | [diff] [blame] | 1231 | * 2. A timeout of 10 seconds without activity has expired |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1232 | * (1K of mos7840 data @ 2400 baud ==> 4 sec to empty) |
| 1233 | * |
| 1234 | ************************************************************************/ |
| 1235 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 1236 | static void mos7840_block_until_chase_response(struct tty_struct *tty, |
| 1237 | struct moschip_port *mos7840_port) |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1238 | { |
Mark Ferrell | 1e65848 | 2012-07-24 13:38:31 -0500 | [diff] [blame] | 1239 | int timeout = msecs_to_jiffies(1000); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1240 | int wait = 10; |
| 1241 | int count; |
| 1242 | |
| 1243 | while (1) { |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 1244 | count = mos7840_chars_in_buffer(tty); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1245 | |
| 1246 | /* Check for Buffer status */ |
Alan Cox | 880af9d | 2008-07-22 11:16:12 +0100 | [diff] [blame] | 1247 | if (count <= 0) |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1248 | return; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1249 | |
| 1250 | /* Block the thread for a while */ |
| 1251 | interruptible_sleep_on_timeout(&mos7840_port->wait_chase, |
| 1252 | timeout); |
| 1253 | /* No activity.. count down section */ |
| 1254 | wait--; |
| 1255 | if (wait == 0) { |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 1256 | dev_dbg(&mos7840_port->port->dev, "%s - TIMEOUT\n", __func__); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1257 | return; |
| 1258 | } else { |
Joe Perches | dc0d5c1 | 2007-12-17 11:40:18 -0800 | [diff] [blame] | 1259 | /* Reset timeout value back to seconds */ |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1260 | wait = 10; |
| 1261 | } |
| 1262 | } |
| 1263 | |
| 1264 | } |
| 1265 | |
| 1266 | /***************************************************************************** |
| 1267 | * mos7840_break |
| 1268 | * this function sends a break to the port |
| 1269 | *****************************************************************************/ |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 1270 | static void mos7840_break(struct tty_struct *tty, int break_state) |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1271 | { |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 1272 | struct usb_serial_port *port = tty->driver_data; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1273 | unsigned char data; |
| 1274 | struct usb_serial *serial; |
| 1275 | struct moschip_port *mos7840_port; |
| 1276 | |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 1277 | if (mos7840_port_paranoia_check(port, __func__)) |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1278 | return; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1279 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1280 | serial = mos7840_get_usb_serial(port, __func__); |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 1281 | if (!serial) |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1282 | return; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1283 | |
| 1284 | mos7840_port = mos7840_get_port_private(port); |
| 1285 | |
Alan Cox | 880af9d | 2008-07-22 11:16:12 +0100 | [diff] [blame] | 1286 | if (mos7840_port == NULL) |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1287 | return; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1288 | |
Johan Hovold | bb3529c | 2013-03-21 12:36:35 +0100 | [diff] [blame] | 1289 | /* flush and block until tx is empty */ |
| 1290 | mos7840_block_until_chase_response(tty, mos7840_port); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1291 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 1292 | if (break_state == -1) |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1293 | data = mos7840_port->shadowLCR | LCR_SET_BREAK; |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 1294 | else |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1295 | data = mos7840_port->shadowLCR & ~LCR_SET_BREAK; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1296 | |
Alan Cox | 6b447f04 | 2009-01-02 13:48:56 +0000 | [diff] [blame] | 1297 | /* FIXME: no locking on shadowLCR anywhere in driver */ |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1298 | mos7840_port->shadowLCR = data; |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 1299 | dev_dbg(&port->dev, "%s mos7840_port->shadowLCR is %x\n", __func__, mos7840_port->shadowLCR); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1300 | mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, |
| 1301 | mos7840_port->shadowLCR); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1302 | } |
| 1303 | |
| 1304 | /***************************************************************************** |
| 1305 | * mos7840_write_room |
| 1306 | * this function is called by the tty driver when it wants to know how many |
| 1307 | * bytes of data we can accept for a specific port. |
| 1308 | * If successful, we return the amount of room that we have for this port |
| 1309 | * Otherwise we return a negative error number. |
| 1310 | *****************************************************************************/ |
| 1311 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 1312 | static int mos7840_write_room(struct tty_struct *tty) |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1313 | { |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 1314 | struct usb_serial_port *port = tty->driver_data; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1315 | int i; |
| 1316 | int room = 0; |
Oliver Neukum | 0de9a70 | 2007-03-16 20:28:28 +0100 | [diff] [blame] | 1317 | unsigned long flags; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1318 | struct moschip_port *mos7840_port; |
| 1319 | |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 1320 | if (mos7840_port_paranoia_check(port, __func__)) |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1321 | return -1; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1322 | |
| 1323 | mos7840_port = mos7840_get_port_private(port); |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 1324 | if (mos7840_port == NULL) |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1325 | return -1; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1326 | |
Oliver Neukum | 0de9a70 | 2007-03-16 20:28:28 +0100 | [diff] [blame] | 1327 | spin_lock_irqsave(&mos7840_port->pool_lock, flags); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1328 | for (i = 0; i < NUM_URBS; ++i) { |
Alan Cox | 880af9d | 2008-07-22 11:16:12 +0100 | [diff] [blame] | 1329 | if (!mos7840_port->busy[i]) |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1330 | room += URB_TRANSFER_BUFFER_SIZE; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1331 | } |
Oliver Neukum | 0de9a70 | 2007-03-16 20:28:28 +0100 | [diff] [blame] | 1332 | spin_unlock_irqrestore(&mos7840_port->pool_lock, flags); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1333 | |
Oliver Neukum | 0de9a70 | 2007-03-16 20:28:28 +0100 | [diff] [blame] | 1334 | room = (room == 0) ? 0 : room - URB_TRANSFER_BUFFER_SIZE + 1; |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 1335 | dev_dbg(&mos7840_port->port->dev, "%s - returns %d\n", __func__, room); |
Oliver Neukum | 0de9a70 | 2007-03-16 20:28:28 +0100 | [diff] [blame] | 1336 | return room; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1337 | |
| 1338 | } |
| 1339 | |
| 1340 | /***************************************************************************** |
| 1341 | * mos7840_write |
| 1342 | * this function is called by the tty driver when data should be written to |
| 1343 | * the port. |
| 1344 | * If successful, we return the number of bytes written, otherwise we |
| 1345 | * return a negative error number. |
| 1346 | *****************************************************************************/ |
| 1347 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 1348 | static int mos7840_write(struct tty_struct *tty, struct usb_serial_port *port, |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1349 | const unsigned char *data, int count) |
| 1350 | { |
| 1351 | int status; |
| 1352 | int i; |
| 1353 | int bytes_sent = 0; |
| 1354 | int transfer_size; |
Oliver Neukum | 0de9a70 | 2007-03-16 20:28:28 +0100 | [diff] [blame] | 1355 | unsigned long flags; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1356 | |
| 1357 | struct moschip_port *mos7840_port; |
| 1358 | struct usb_serial *serial; |
| 1359 | struct urb *urb; |
Alan Cox | 880af9d | 2008-07-22 11:16:12 +0100 | [diff] [blame] | 1360 | /* __u16 Data; */ |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1361 | const unsigned char *current_position = data; |
| 1362 | unsigned char *data1; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1363 | |
| 1364 | #ifdef NOTMOS7840 |
| 1365 | Data = 0x00; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1366 | status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data); |
| 1367 | mos7840_port->shadowLCR = Data; |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 1368 | dev_dbg(&port->dev, "%s: LINE_CONTROL_REGISTER is %x\n", __func__, Data); |
| 1369 | dev_dbg(&port->dev, "%s: mos7840_port->shadowLCR is %x\n", __func__, mos7840_port->shadowLCR); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1370 | |
Alan Cox | 880af9d | 2008-07-22 11:16:12 +0100 | [diff] [blame] | 1371 | /* Data = 0x03; */ |
| 1372 | /* status = mos7840_set_uart_reg(port,LINE_CONTROL_REGISTER,Data); */ |
| 1373 | /* mos7840_port->shadowLCR=Data;//Need to add later */ |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1374 | |
Alan Cox | 880af9d | 2008-07-22 11:16:12 +0100 | [diff] [blame] | 1375 | Data |= SERIAL_LCR_DLAB; /* data latch enable in LCR 0x80 */ |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1376 | status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data); |
| 1377 | |
Alan Cox | 880af9d | 2008-07-22 11:16:12 +0100 | [diff] [blame] | 1378 | /* Data = 0x0c; */ |
| 1379 | /* status = mos7840_set_uart_reg(port,DIVISOR_LATCH_LSB,Data); */ |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1380 | Data = 0x00; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1381 | status = mos7840_get_uart_reg(port, DIVISOR_LATCH_LSB, &Data); |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 1382 | dev_dbg(&port->dev, "%s: DLL value is %x\n", __func__, Data); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1383 | |
| 1384 | Data = 0x0; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1385 | status = mos7840_get_uart_reg(port, DIVISOR_LATCH_MSB, &Data); |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 1386 | dev_dbg(&port->dev, "%s: DLM value is %x\n", __func__, Data); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1387 | |
| 1388 | Data = Data & ~SERIAL_LCR_DLAB; |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 1389 | dev_dbg(&port->dev, "%s: mos7840_port->shadowLCR is %x\n", __func__, mos7840_port->shadowLCR); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1390 | status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data); |
| 1391 | #endif |
| 1392 | |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 1393 | if (mos7840_port_paranoia_check(port, __func__)) |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1394 | return -1; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1395 | |
| 1396 | serial = port->serial; |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 1397 | if (mos7840_serial_paranoia_check(serial, __func__)) |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1398 | return -1; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1399 | |
| 1400 | mos7840_port = mos7840_get_port_private(port); |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 1401 | if (mos7840_port == NULL) |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1402 | return -1; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1403 | |
| 1404 | /* try to find a free urb in the list */ |
| 1405 | urb = NULL; |
| 1406 | |
Oliver Neukum | 0de9a70 | 2007-03-16 20:28:28 +0100 | [diff] [blame] | 1407 | spin_lock_irqsave(&mos7840_port->pool_lock, flags); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1408 | for (i = 0; i < NUM_URBS; ++i) { |
Oliver Neukum | 0de9a70 | 2007-03-16 20:28:28 +0100 | [diff] [blame] | 1409 | if (!mos7840_port->busy[i]) { |
| 1410 | mos7840_port->busy[i] = 1; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1411 | urb = mos7840_port->write_urb_pool[i]; |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 1412 | dev_dbg(&port->dev, "URB:%d\n", i); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1413 | break; |
| 1414 | } |
| 1415 | } |
Oliver Neukum | 0de9a70 | 2007-03-16 20:28:28 +0100 | [diff] [blame] | 1416 | spin_unlock_irqrestore(&mos7840_port->pool_lock, flags); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1417 | |
| 1418 | if (urb == NULL) { |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 1419 | dev_dbg(&port->dev, "%s - no more free urbs\n", __func__); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1420 | goto exit; |
| 1421 | } |
| 1422 | |
| 1423 | if (urb->transfer_buffer == NULL) { |
| 1424 | urb->transfer_buffer = |
| 1425 | kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL); |
| 1426 | |
| 1427 | if (urb->transfer_buffer == NULL) { |
Johan Hovold | 22a416c | 2012-02-10 13:20:51 +0100 | [diff] [blame] | 1428 | dev_err_console(port, "%s no more kernel memory...\n", |
Greg Kroah-Hartman | 194343d | 2008-08-20 16:56:34 -0700 | [diff] [blame] | 1429 | __func__); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1430 | goto exit; |
| 1431 | } |
| 1432 | } |
| 1433 | transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE); |
| 1434 | |
Al Viro | 97c4965 | 2006-10-09 20:29:03 +0100 | [diff] [blame] | 1435 | memcpy(urb->transfer_buffer, current_position, transfer_size); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1436 | |
| 1437 | /* fill urb with data and submit */ |
Donald Lee | 093ea2d | 2012-03-14 15:26:33 +0800 | [diff] [blame] | 1438 | if ((serial->num_ports == 2) |
| 1439 | && ((((__u16)port->number - |
| 1440 | (__u16)(port->serial->minor)) % 2) != 0)) { |
| 1441 | usb_fill_bulk_urb(urb, |
| 1442 | serial->dev, |
| 1443 | usb_sndbulkpipe(serial->dev, |
| 1444 | (port->bulk_out_endpointAddress) + 2), |
| 1445 | urb->transfer_buffer, |
| 1446 | transfer_size, |
| 1447 | mos7840_bulk_out_data_callback, mos7840_port); |
| 1448 | } else { |
| 1449 | usb_fill_bulk_urb(urb, |
| 1450 | serial->dev, |
| 1451 | usb_sndbulkpipe(serial->dev, |
| 1452 | port->bulk_out_endpointAddress), |
| 1453 | urb->transfer_buffer, |
| 1454 | transfer_size, |
| 1455 | mos7840_bulk_out_data_callback, mos7840_port); |
| 1456 | } |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1457 | |
| 1458 | data1 = urb->transfer_buffer; |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 1459 | dev_dbg(&port->dev, "bulkout endpoint is %d\n", port->bulk_out_endpointAddress); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1460 | |
Donald | 0eafe4d | 2012-04-19 15:00:45 +0800 | [diff] [blame] | 1461 | /* Turn on LED */ |
| 1462 | if (mos7840_port->has_led && !mos7840_port->led_flag) { |
| 1463 | mos7840_port->led_flag = true; |
| 1464 | mos7840_set_led_sync(port, MODEM_CONTROL_REGISTER, 0x0301); |
| 1465 | mod_timer(&mos7840_port->led_timer1, |
| 1466 | jiffies + msecs_to_jiffies(LED_ON_MS)); |
| 1467 | } |
| 1468 | |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1469 | /* send it down the pipe */ |
| 1470 | status = usb_submit_urb(urb, GFP_ATOMIC); |
| 1471 | |
| 1472 | if (status) { |
Oliver Neukum | 0de9a70 | 2007-03-16 20:28:28 +0100 | [diff] [blame] | 1473 | mos7840_port->busy[i] = 0; |
Johan Hovold | 22a416c | 2012-02-10 13:20:51 +0100 | [diff] [blame] | 1474 | dev_err_console(port, "%s - usb_submit_urb(write bulk) failed " |
Greg Kroah-Hartman | 194343d | 2008-08-20 16:56:34 -0700 | [diff] [blame] | 1475 | "with status = %d\n", __func__, status); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1476 | bytes_sent = status; |
| 1477 | goto exit; |
| 1478 | } |
| 1479 | bytes_sent = transfer_size; |
Johan Hovold | 8c1a07f | 2013-03-21 12:37:16 +0100 | [diff] [blame] | 1480 | port->icount.tx += transfer_size; |
| 1481 | dev_dbg(&port->dev, "icount.tx is %d:\n", port->icount.tx); |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 1482 | exit: |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1483 | return bytes_sent; |
| 1484 | |
| 1485 | } |
| 1486 | |
| 1487 | /***************************************************************************** |
| 1488 | * mos7840_throttle |
| 1489 | * this function is called by the tty driver when it wants to stop the data |
| 1490 | * being read from the port. |
| 1491 | *****************************************************************************/ |
| 1492 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 1493 | static void mos7840_throttle(struct tty_struct *tty) |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1494 | { |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 1495 | struct usb_serial_port *port = tty->driver_data; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1496 | struct moschip_port *mos7840_port; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1497 | int status; |
| 1498 | |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 1499 | if (mos7840_port_paranoia_check(port, __func__)) |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1500 | return; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1501 | |
| 1502 | mos7840_port = mos7840_get_port_private(port); |
| 1503 | |
| 1504 | if (mos7840_port == NULL) |
| 1505 | return; |
| 1506 | |
| 1507 | if (!mos7840_port->open) { |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 1508 | dev_dbg(&port->dev, "%s", "port not opened\n"); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1509 | return; |
| 1510 | } |
| 1511 | |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1512 | /* if we are implementing XON/XOFF, send the stop character */ |
| 1513 | if (I_IXOFF(tty)) { |
| 1514 | unsigned char stop_char = STOP_CHAR(tty); |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 1515 | status = mos7840_write(tty, port, &stop_char, 1); |
| 1516 | if (status <= 0) |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1517 | return; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1518 | } |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1519 | /* if we are implementing RTS/CTS, toggle that line */ |
Alan Cox | adc8d74 | 2012-07-14 15:31:47 +0100 | [diff] [blame] | 1520 | if (tty->termios.c_cflag & CRTSCTS) { |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1521 | mos7840_port->shadowMCR &= ~MCR_RTS; |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 1522 | status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1523 | mos7840_port->shadowMCR); |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 1524 | if (status < 0) |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1525 | return; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1526 | } |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1527 | } |
| 1528 | |
| 1529 | /***************************************************************************** |
| 1530 | * mos7840_unthrottle |
Alan Cox | 880af9d | 2008-07-22 11:16:12 +0100 | [diff] [blame] | 1531 | * this function is called by the tty driver when it wants to resume |
| 1532 | * the data being read from the port (called after mos7840_throttle is |
| 1533 | * called) |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1534 | *****************************************************************************/ |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 1535 | static void mos7840_unthrottle(struct tty_struct *tty) |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1536 | { |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 1537 | struct usb_serial_port *port = tty->driver_data; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1538 | int status; |
| 1539 | struct moschip_port *mos7840_port = mos7840_get_port_private(port); |
| 1540 | |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 1541 | if (mos7840_port_paranoia_check(port, __func__)) |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1542 | return; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1543 | |
| 1544 | if (mos7840_port == NULL) |
| 1545 | return; |
| 1546 | |
| 1547 | if (!mos7840_port->open) { |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 1548 | dev_dbg(&port->dev, "%s - port not opened\n", __func__); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1549 | return; |
| 1550 | } |
| 1551 | |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1552 | /* if we are implementing XON/XOFF, send the start character */ |
| 1553 | if (I_IXOFF(tty)) { |
| 1554 | unsigned char start_char = START_CHAR(tty); |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 1555 | status = mos7840_write(tty, port, &start_char, 1); |
| 1556 | if (status <= 0) |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1557 | return; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1558 | } |
| 1559 | |
| 1560 | /* if we are implementing RTS/CTS, toggle that line */ |
Alan Cox | adc8d74 | 2012-07-14 15:31:47 +0100 | [diff] [blame] | 1561 | if (tty->termios.c_cflag & CRTSCTS) { |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1562 | mos7840_port->shadowMCR |= MCR_RTS; |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 1563 | status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1564 | mos7840_port->shadowMCR); |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 1565 | if (status < 0) |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1566 | return; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1567 | } |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1568 | } |
| 1569 | |
Alan Cox | 60b33c1 | 2011-02-14 16:26:14 +0000 | [diff] [blame] | 1570 | static int mos7840_tiocmget(struct tty_struct *tty) |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1571 | { |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 1572 | struct usb_serial_port *port = tty->driver_data; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1573 | struct moschip_port *mos7840_port; |
| 1574 | unsigned int result; |
| 1575 | __u16 msr; |
| 1576 | __u16 mcr; |
Alan Cox | 880af9d | 2008-07-22 11:16:12 +0100 | [diff] [blame] | 1577 | int status; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1578 | mos7840_port = mos7840_get_port_private(port); |
| 1579 | |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1580 | if (mos7840_port == NULL) |
| 1581 | return -ENODEV; |
| 1582 | |
| 1583 | status = mos7840_get_uart_reg(port, MODEM_STATUS_REGISTER, &msr); |
| 1584 | status = mos7840_get_uart_reg(port, MODEM_CONTROL_REGISTER, &mcr); |
| 1585 | result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0) |
| 1586 | | ((mcr & MCR_RTS) ? TIOCM_RTS : 0) |
| 1587 | | ((mcr & MCR_LOOPBACK) ? TIOCM_LOOP : 0) |
| 1588 | | ((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0) |
| 1589 | | ((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0) |
| 1590 | | ((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0) |
| 1591 | | ((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0); |
| 1592 | |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 1593 | dev_dbg(&port->dev, "%s - 0x%04X\n", __func__, result); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1594 | |
| 1595 | return result; |
| 1596 | } |
| 1597 | |
Alan Cox | 20b9d17 | 2011-02-14 16:26:50 +0000 | [diff] [blame] | 1598 | static int mos7840_tiocmset(struct tty_struct *tty, |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1599 | unsigned int set, unsigned int clear) |
| 1600 | { |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 1601 | struct usb_serial_port *port = tty->driver_data; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1602 | struct moschip_port *mos7840_port; |
| 1603 | unsigned int mcr; |
Roel Kluin | 87521c4 | 2008-04-17 06:16:24 +0200 | [diff] [blame] | 1604 | int status; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1605 | |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1606 | mos7840_port = mos7840_get_port_private(port); |
| 1607 | |
| 1608 | if (mos7840_port == NULL) |
| 1609 | return -ENODEV; |
| 1610 | |
Alan Cox | e298449 | 2008-02-20 20:51:45 +0000 | [diff] [blame] | 1611 | /* FIXME: What locks the port registers ? */ |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1612 | mcr = mos7840_port->shadowMCR; |
| 1613 | if (clear & TIOCM_RTS) |
| 1614 | mcr &= ~MCR_RTS; |
| 1615 | if (clear & TIOCM_DTR) |
| 1616 | mcr &= ~MCR_DTR; |
| 1617 | if (clear & TIOCM_LOOP) |
| 1618 | mcr &= ~MCR_LOOPBACK; |
| 1619 | |
| 1620 | if (set & TIOCM_RTS) |
| 1621 | mcr |= MCR_RTS; |
| 1622 | if (set & TIOCM_DTR) |
| 1623 | mcr |= MCR_DTR; |
| 1624 | if (set & TIOCM_LOOP) |
| 1625 | mcr |= MCR_LOOPBACK; |
| 1626 | |
| 1627 | mos7840_port->shadowMCR = mcr; |
| 1628 | |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1629 | status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, mcr); |
| 1630 | if (status < 0) { |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 1631 | dev_dbg(&port->dev, "setting MODEM_CONTROL_REGISTER Failed\n"); |
Roel Kluin | 87521c4 | 2008-04-17 06:16:24 +0200 | [diff] [blame] | 1632 | return status; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1633 | } |
| 1634 | |
| 1635 | return 0; |
| 1636 | } |
| 1637 | |
| 1638 | /***************************************************************************** |
| 1639 | * mos7840_calc_baud_rate_divisor |
| 1640 | * this function calculates the proper baud rate divisor for the specified |
| 1641 | * baud rate. |
| 1642 | *****************************************************************************/ |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 1643 | static int mos7840_calc_baud_rate_divisor(struct usb_serial_port *port, |
| 1644 | int baudRate, int *divisor, |
Alan Cox | 880af9d | 2008-07-22 11:16:12 +0100 | [diff] [blame] | 1645 | __u16 *clk_sel_val) |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1646 | { |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 1647 | dev_dbg(&port->dev, "%s - %d\n", __func__, baudRate); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1648 | |
| 1649 | if (baudRate <= 115200) { |
| 1650 | *divisor = 115200 / baudRate; |
| 1651 | *clk_sel_val = 0x0; |
| 1652 | } |
| 1653 | if ((baudRate > 115200) && (baudRate <= 230400)) { |
| 1654 | *divisor = 230400 / baudRate; |
| 1655 | *clk_sel_val = 0x10; |
| 1656 | } else if ((baudRate > 230400) && (baudRate <= 403200)) { |
| 1657 | *divisor = 403200 / baudRate; |
| 1658 | *clk_sel_val = 0x20; |
| 1659 | } else if ((baudRate > 403200) && (baudRate <= 460800)) { |
| 1660 | *divisor = 460800 / baudRate; |
| 1661 | *clk_sel_val = 0x30; |
| 1662 | } else if ((baudRate > 460800) && (baudRate <= 806400)) { |
| 1663 | *divisor = 806400 / baudRate; |
| 1664 | *clk_sel_val = 0x40; |
| 1665 | } else if ((baudRate > 806400) && (baudRate <= 921600)) { |
| 1666 | *divisor = 921600 / baudRate; |
| 1667 | *clk_sel_val = 0x50; |
| 1668 | } else if ((baudRate > 921600) && (baudRate <= 1572864)) { |
| 1669 | *divisor = 1572864 / baudRate; |
| 1670 | *clk_sel_val = 0x60; |
| 1671 | } else if ((baudRate > 1572864) && (baudRate <= 3145728)) { |
| 1672 | *divisor = 3145728 / baudRate; |
| 1673 | *clk_sel_val = 0x70; |
| 1674 | } |
| 1675 | return 0; |
| 1676 | |
| 1677 | #ifdef NOTMCS7840 |
| 1678 | |
| 1679 | for (i = 0; i < ARRAY_SIZE(mos7840_divisor_table); i++) { |
| 1680 | if (mos7840_divisor_table[i].BaudRate == baudrate) { |
| 1681 | *divisor = mos7840_divisor_table[i].Divisor; |
| 1682 | return 0; |
| 1683 | } |
| 1684 | } |
| 1685 | |
| 1686 | /* After trying for all the standard baud rates * |
| 1687 | * Try calculating the divisor for this baud rate */ |
| 1688 | |
| 1689 | if (baudrate > 75 && baudrate < 230400) { |
| 1690 | /* get the divisor */ |
| 1691 | custom = (__u16) (230400L / baudrate); |
| 1692 | |
| 1693 | /* Check for round off */ |
| 1694 | round1 = (__u16) (2304000L / baudrate); |
| 1695 | round = (__u16) (round1 - (custom * 10)); |
Alan Cox | 880af9d | 2008-07-22 11:16:12 +0100 | [diff] [blame] | 1696 | if (round > 4) |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1697 | custom++; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1698 | *divisor = custom; |
| 1699 | |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 1700 | dev_dbg(&port->dev, " Baud %d = %d\n", baudrate, custom); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1701 | return 0; |
| 1702 | } |
| 1703 | |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 1704 | dev_dbg(&port->dev, "%s", " Baud calculation Failed...\n"); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1705 | return -1; |
| 1706 | #endif |
| 1707 | } |
| 1708 | |
| 1709 | /***************************************************************************** |
| 1710 | * mos7840_send_cmd_write_baud_rate |
| 1711 | * this function sends the proper command to change the baud rate of the |
| 1712 | * specified port. |
| 1713 | *****************************************************************************/ |
| 1714 | |
| 1715 | static int mos7840_send_cmd_write_baud_rate(struct moschip_port *mos7840_port, |
| 1716 | int baudRate) |
| 1717 | { |
| 1718 | int divisor = 0; |
| 1719 | int status; |
| 1720 | __u16 Data; |
| 1721 | unsigned char number; |
| 1722 | __u16 clk_sel_val; |
| 1723 | struct usb_serial_port *port; |
| 1724 | |
| 1725 | if (mos7840_port == NULL) |
| 1726 | return -1; |
| 1727 | |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 1728 | port = mos7840_port->port; |
| 1729 | if (mos7840_port_paranoia_check(port, __func__)) |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1730 | return -1; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1731 | |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 1732 | if (mos7840_serial_paranoia_check(port->serial, __func__)) |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1733 | return -1; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1734 | |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1735 | number = mos7840_port->port->number - mos7840_port->port->serial->minor; |
| 1736 | |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 1737 | dev_dbg(&port->dev, "%s - port = %d, baud = %d\n", __func__, |
| 1738 | mos7840_port->port->number, baudRate); |
Alan Cox | 880af9d | 2008-07-22 11:16:12 +0100 | [diff] [blame] | 1739 | /* reset clk_uart_sel in spregOffset */ |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1740 | if (baudRate > 115200) { |
| 1741 | #ifdef HW_flow_control |
Alan Cox | 880af9d | 2008-07-22 11:16:12 +0100 | [diff] [blame] | 1742 | /* NOTE: need to see the pther register to modify */ |
| 1743 | /* setting h/w flow control bit to 1 */ |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1744 | Data = 0x2b; |
| 1745 | mos7840_port->shadowMCR = Data; |
Alan Cox | 880af9d | 2008-07-22 11:16:12 +0100 | [diff] [blame] | 1746 | status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, |
| 1747 | Data); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1748 | if (status < 0) { |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 1749 | dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n"); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1750 | return -1; |
| 1751 | } |
| 1752 | #endif |
| 1753 | |
| 1754 | } else { |
| 1755 | #ifdef HW_flow_control |
Donald Lee | 093ea2d | 2012-03-14 15:26:33 +0800 | [diff] [blame] | 1756 | /* setting h/w flow control bit to 0 */ |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1757 | Data = 0xb; |
| 1758 | mos7840_port->shadowMCR = Data; |
Alan Cox | 880af9d | 2008-07-22 11:16:12 +0100 | [diff] [blame] | 1759 | status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, |
| 1760 | Data); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1761 | if (status < 0) { |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 1762 | dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n"); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1763 | return -1; |
| 1764 | } |
| 1765 | #endif |
| 1766 | |
| 1767 | } |
| 1768 | |
Alan Cox | 880af9d | 2008-07-22 11:16:12 +0100 | [diff] [blame] | 1769 | if (1) { /* baudRate <= 115200) */ |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1770 | clk_sel_val = 0x0; |
| 1771 | Data = 0x0; |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 1772 | status = mos7840_calc_baud_rate_divisor(port, baudRate, &divisor, |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1773 | &clk_sel_val); |
Alan Cox | 880af9d | 2008-07-22 11:16:12 +0100 | [diff] [blame] | 1774 | status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, |
| 1775 | &Data); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1776 | if (status < 0) { |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 1777 | dev_dbg(&port->dev, "reading spreg failed in set_serial_baud\n"); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1778 | return -1; |
| 1779 | } |
| 1780 | Data = (Data & 0x8f) | clk_sel_val; |
Alan Cox | 880af9d | 2008-07-22 11:16:12 +0100 | [diff] [blame] | 1781 | status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, |
| 1782 | Data); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1783 | if (status < 0) { |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 1784 | dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n"); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1785 | return -1; |
| 1786 | } |
| 1787 | /* Calculate the Divisor */ |
| 1788 | |
| 1789 | if (status) { |
Greg Kroah-Hartman | 194343d | 2008-08-20 16:56:34 -0700 | [diff] [blame] | 1790 | dev_err(&port->dev, "%s - bad baud rate\n", __func__); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1791 | return status; |
| 1792 | } |
| 1793 | /* Enable access to divisor latch */ |
| 1794 | Data = mos7840_port->shadowLCR | SERIAL_LCR_DLAB; |
| 1795 | mos7840_port->shadowLCR = Data; |
| 1796 | mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data); |
| 1797 | |
| 1798 | /* Write the divisor */ |
| 1799 | Data = (unsigned char)(divisor & 0xff); |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 1800 | dev_dbg(&port->dev, "set_serial_baud Value to write DLL is %x\n", Data); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1801 | mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data); |
| 1802 | |
| 1803 | Data = (unsigned char)((divisor & 0xff00) >> 8); |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 1804 | dev_dbg(&port->dev, "set_serial_baud Value to write DLM is %x\n", Data); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1805 | mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data); |
| 1806 | |
| 1807 | /* Disable access to divisor latch */ |
| 1808 | Data = mos7840_port->shadowLCR & ~SERIAL_LCR_DLAB; |
| 1809 | mos7840_port->shadowLCR = Data; |
| 1810 | mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data); |
| 1811 | |
| 1812 | } |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1813 | return status; |
| 1814 | } |
| 1815 | |
| 1816 | /***************************************************************************** |
| 1817 | * mos7840_change_port_settings |
| 1818 | * This routine is called to set the UART on the device to match |
| 1819 | * the specified new settings. |
| 1820 | *****************************************************************************/ |
| 1821 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 1822 | static void mos7840_change_port_settings(struct tty_struct *tty, |
| 1823 | struct moschip_port *mos7840_port, struct ktermios *old_termios) |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1824 | { |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1825 | int baud; |
| 1826 | unsigned cflag; |
| 1827 | unsigned iflag; |
| 1828 | __u8 lData; |
| 1829 | __u8 lParity; |
| 1830 | __u8 lStop; |
| 1831 | int status; |
| 1832 | __u16 Data; |
| 1833 | struct usb_serial_port *port; |
| 1834 | struct usb_serial *serial; |
| 1835 | |
| 1836 | if (mos7840_port == NULL) |
| 1837 | return; |
| 1838 | |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 1839 | port = mos7840_port->port; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1840 | |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 1841 | if (mos7840_port_paranoia_check(port, __func__)) |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1842 | return; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1843 | |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 1844 | if (mos7840_serial_paranoia_check(port->serial, __func__)) |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1845 | return; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1846 | |
| 1847 | serial = port->serial; |
| 1848 | |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1849 | if (!mos7840_port->open) { |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 1850 | dev_dbg(&port->dev, "%s - port not opened\n", __func__); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1851 | return; |
| 1852 | } |
| 1853 | |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1854 | lData = LCR_BITS_8; |
| 1855 | lStop = LCR_STOP_1; |
| 1856 | lParity = LCR_PAR_NONE; |
| 1857 | |
Alan Cox | adc8d74 | 2012-07-14 15:31:47 +0100 | [diff] [blame] | 1858 | cflag = tty->termios.c_cflag; |
| 1859 | iflag = tty->termios.c_iflag; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1860 | |
| 1861 | /* Change the number of bits */ |
| 1862 | if (cflag & CSIZE) { |
| 1863 | switch (cflag & CSIZE) { |
| 1864 | case CS5: |
| 1865 | lData = LCR_BITS_5; |
| 1866 | break; |
| 1867 | |
| 1868 | case CS6: |
| 1869 | lData = LCR_BITS_6; |
| 1870 | break; |
| 1871 | |
| 1872 | case CS7: |
| 1873 | lData = LCR_BITS_7; |
| 1874 | break; |
| 1875 | default: |
| 1876 | case CS8: |
| 1877 | lData = LCR_BITS_8; |
| 1878 | break; |
| 1879 | } |
| 1880 | } |
| 1881 | /* Change the Parity bit */ |
| 1882 | if (cflag & PARENB) { |
| 1883 | if (cflag & PARODD) { |
| 1884 | lParity = LCR_PAR_ODD; |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 1885 | dev_dbg(&port->dev, "%s - parity = odd\n", __func__); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1886 | } else { |
| 1887 | lParity = LCR_PAR_EVEN; |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 1888 | dev_dbg(&port->dev, "%s - parity = even\n", __func__); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1889 | } |
| 1890 | |
| 1891 | } else { |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 1892 | dev_dbg(&port->dev, "%s - parity = none\n", __func__); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1893 | } |
| 1894 | |
Alan Cox | 880af9d | 2008-07-22 11:16:12 +0100 | [diff] [blame] | 1895 | if (cflag & CMSPAR) |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1896 | lParity = lParity | 0x20; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1897 | |
| 1898 | /* Change the Stop bit */ |
| 1899 | if (cflag & CSTOPB) { |
| 1900 | lStop = LCR_STOP_2; |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 1901 | dev_dbg(&port->dev, "%s - stop bits = 2\n", __func__); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1902 | } else { |
| 1903 | lStop = LCR_STOP_1; |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 1904 | dev_dbg(&port->dev, "%s - stop bits = 1\n", __func__); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1905 | } |
| 1906 | |
| 1907 | /* Update the LCR with the correct value */ |
| 1908 | mos7840_port->shadowLCR &= |
| 1909 | ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK); |
| 1910 | mos7840_port->shadowLCR |= (lData | lParity | lStop); |
| 1911 | |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 1912 | dev_dbg(&port->dev, "%s - mos7840_port->shadowLCR is %x\n", __func__, |
| 1913 | mos7840_port->shadowLCR); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1914 | /* Disable Interrupts */ |
| 1915 | Data = 0x00; |
| 1916 | mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data); |
| 1917 | |
| 1918 | Data = 0x00; |
| 1919 | mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data); |
| 1920 | |
| 1921 | Data = 0xcf; |
| 1922 | mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data); |
| 1923 | |
| 1924 | /* Send the updated LCR value to the mos7840 */ |
| 1925 | Data = mos7840_port->shadowLCR; |
| 1926 | |
| 1927 | mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data); |
| 1928 | |
| 1929 | Data = 0x00b; |
| 1930 | mos7840_port->shadowMCR = Data; |
| 1931 | mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data); |
| 1932 | Data = 0x00b; |
| 1933 | mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data); |
| 1934 | |
| 1935 | /* set up the MCR register and send it to the mos7840 */ |
| 1936 | |
| 1937 | mos7840_port->shadowMCR = MCR_MASTER_IE; |
Alan Cox | 880af9d | 2008-07-22 11:16:12 +0100 | [diff] [blame] | 1938 | if (cflag & CBAUD) |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1939 | mos7840_port->shadowMCR |= (MCR_DTR | MCR_RTS); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1940 | |
Alan Cox | 880af9d | 2008-07-22 11:16:12 +0100 | [diff] [blame] | 1941 | if (cflag & CRTSCTS) |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1942 | mos7840_port->shadowMCR |= (MCR_XON_ANY); |
Alan Cox | 880af9d | 2008-07-22 11:16:12 +0100 | [diff] [blame] | 1943 | else |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1944 | mos7840_port->shadowMCR &= ~(MCR_XON_ANY); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1945 | |
| 1946 | Data = mos7840_port->shadowMCR; |
| 1947 | mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data); |
| 1948 | |
| 1949 | /* Determine divisor based on baud rate */ |
| 1950 | baud = tty_get_baud_rate(tty); |
| 1951 | |
| 1952 | if (!baud) { |
| 1953 | /* pick a default, any default... */ |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 1954 | dev_dbg(&port->dev, "%s", "Picked default baud...\n"); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1955 | baud = 9600; |
| 1956 | } |
| 1957 | |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 1958 | dev_dbg(&port->dev, "%s - baud rate = %d\n", __func__, baud); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1959 | status = mos7840_send_cmd_write_baud_rate(mos7840_port, baud); |
| 1960 | |
| 1961 | /* Enable Interrupts */ |
| 1962 | Data = 0x0c; |
| 1963 | mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data); |
| 1964 | |
Greg Kroah-Hartman | 50de36f | 2008-12-10 16:00:30 -0800 | [diff] [blame] | 1965 | if (mos7840_port->read_urb_busy == false) { |
Greg Kroah-Hartman | 50de36f | 2008-12-10 16:00:30 -0800 | [diff] [blame] | 1966 | mos7840_port->read_urb_busy = true; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1967 | status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1968 | if (status) { |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 1969 | dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n", |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1970 | status); |
Greg Kroah-Hartman | 50de36f | 2008-12-10 16:00:30 -0800 | [diff] [blame] | 1971 | mos7840_port->read_urb_busy = false; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1972 | } |
| 1973 | } |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 1974 | dev_dbg(&port->dev, "%s - mos7840_port->shadowLCR is End %x\n", __func__, |
| 1975 | mos7840_port->shadowLCR); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1976 | } |
| 1977 | |
| 1978 | /***************************************************************************** |
| 1979 | * mos7840_set_termios |
| 1980 | * this function is called by the tty driver when it wants to change |
| 1981 | * the termios structure |
| 1982 | *****************************************************************************/ |
| 1983 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 1984 | static void mos7840_set_termios(struct tty_struct *tty, |
| 1985 | struct usb_serial_port *port, |
Alan Cox | 606d099 | 2006-12-08 02:38:45 -0800 | [diff] [blame] | 1986 | struct ktermios *old_termios) |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1987 | { |
| 1988 | int status; |
| 1989 | unsigned int cflag; |
| 1990 | struct usb_serial *serial; |
| 1991 | struct moschip_port *mos7840_port; |
Greg Kroah-Hartman | 3363155 | 2012-05-03 16:44:33 -0700 | [diff] [blame] | 1992 | |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 1993 | if (mos7840_port_paranoia_check(port, __func__)) |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1994 | return; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1995 | |
| 1996 | serial = port->serial; |
| 1997 | |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 1998 | if (mos7840_serial_paranoia_check(serial, __func__)) |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 1999 | return; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 2000 | |
| 2001 | mos7840_port = mos7840_get_port_private(port); |
| 2002 | |
| 2003 | if (mos7840_port == NULL) |
| 2004 | return; |
| 2005 | |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 2006 | if (!mos7840_port->open) { |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 2007 | dev_dbg(&port->dev, "%s - port not opened\n", __func__); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 2008 | return; |
| 2009 | } |
| 2010 | |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 2011 | dev_dbg(&port->dev, "%s", "setting termios - \n"); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 2012 | |
Alan Cox | adc8d74 | 2012-07-14 15:31:47 +0100 | [diff] [blame] | 2013 | cflag = tty->termios.c_cflag; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 2014 | |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 2015 | dev_dbg(&port->dev, "%s - clfag %08x iflag %08x\n", __func__, |
Linus Torvalds | d9a8074 | 2012-10-01 13:23:01 -0700 | [diff] [blame] | 2016 | tty->termios.c_cflag, RELEVANT_IFLAG(tty->termios.c_iflag)); |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 2017 | dev_dbg(&port->dev, "%s - old clfag %08x old iflag %08x\n", __func__, |
| 2018 | old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag)); |
| 2019 | dev_dbg(&port->dev, "%s - port %d\n", __func__, port->number); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 2020 | |
| 2021 | /* change the port settings to the new ones specified */ |
| 2022 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 2023 | mos7840_change_port_settings(tty, mos7840_port, old_termios); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 2024 | |
| 2025 | if (!mos7840_port->read_urb) { |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 2026 | dev_dbg(&port->dev, "%s", "URB KILLED !!!!!\n"); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 2027 | return; |
| 2028 | } |
| 2029 | |
Greg Kroah-Hartman | 50de36f | 2008-12-10 16:00:30 -0800 | [diff] [blame] | 2030 | if (mos7840_port->read_urb_busy == false) { |
Greg Kroah-Hartman | 50de36f | 2008-12-10 16:00:30 -0800 | [diff] [blame] | 2031 | mos7840_port->read_urb_busy = true; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 2032 | status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC); |
| 2033 | if (status) { |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 2034 | dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n", |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 2035 | status); |
Greg Kroah-Hartman | 50de36f | 2008-12-10 16:00:30 -0800 | [diff] [blame] | 2036 | mos7840_port->read_urb_busy = false; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 2037 | } |
| 2038 | } |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 2039 | } |
| 2040 | |
| 2041 | /***************************************************************************** |
| 2042 | * mos7840_get_lsr_info - get line status register info |
| 2043 | * |
| 2044 | * Purpose: Let user call ioctl() to get info when the UART physically |
| 2045 | * is emptied. On bus types like RS485, the transmitter must |
| 2046 | * release the bus after transmitting. This must be done when |
| 2047 | * the transmit shift register is empty, not be done when the |
| 2048 | * transmit holding register is empty. This functionality |
| 2049 | * allows an RS485 driver to be written in user space. |
| 2050 | *****************************************************************************/ |
| 2051 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 2052 | static int mos7840_get_lsr_info(struct tty_struct *tty, |
Al Viro | 97c4965 | 2006-10-09 20:29:03 +0100 | [diff] [blame] | 2053 | unsigned int __user *value) |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 2054 | { |
| 2055 | int count; |
| 2056 | unsigned int result = 0; |
| 2057 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 2058 | count = mos7840_chars_in_buffer(tty); |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 2059 | if (count == 0) |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 2060 | result = TIOCSER_TEMT; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 2061 | |
| 2062 | if (copy_to_user(value, &result, sizeof(int))) |
| 2063 | return -EFAULT; |
| 2064 | return 0; |
| 2065 | } |
| 2066 | |
| 2067 | /***************************************************************************** |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 2068 | * mos7840_get_serial_info |
| 2069 | * function to get information about serial port |
| 2070 | *****************************************************************************/ |
| 2071 | |
| 2072 | static int mos7840_get_serial_info(struct moschip_port *mos7840_port, |
Al Viro | 97c4965 | 2006-10-09 20:29:03 +0100 | [diff] [blame] | 2073 | struct serial_struct __user *retinfo) |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 2074 | { |
| 2075 | struct serial_struct tmp; |
| 2076 | |
| 2077 | if (mos7840_port == NULL) |
| 2078 | return -1; |
| 2079 | |
| 2080 | if (!retinfo) |
| 2081 | return -EFAULT; |
| 2082 | |
| 2083 | memset(&tmp, 0, sizeof(tmp)); |
| 2084 | |
| 2085 | tmp.type = PORT_16550A; |
| 2086 | tmp.line = mos7840_port->port->serial->minor; |
| 2087 | tmp.port = mos7840_port->port->number; |
| 2088 | tmp.irq = 0; |
| 2089 | tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ; |
| 2090 | tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE; |
| 2091 | tmp.baud_base = 9600; |
| 2092 | tmp.close_delay = 5 * HZ; |
| 2093 | tmp.closing_wait = 30 * HZ; |
| 2094 | |
| 2095 | if (copy_to_user(retinfo, &tmp, sizeof(*retinfo))) |
| 2096 | return -EFAULT; |
| 2097 | return 0; |
| 2098 | } |
| 2099 | |
| 2100 | /***************************************************************************** |
| 2101 | * SerialIoctl |
| 2102 | * this function handles any ioctl calls to the driver |
| 2103 | *****************************************************************************/ |
| 2104 | |
Alan Cox | 00a0d0d | 2011-02-14 16:27:06 +0000 | [diff] [blame] | 2105 | static int mos7840_ioctl(struct tty_struct *tty, |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 2106 | unsigned int cmd, unsigned long arg) |
| 2107 | { |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 2108 | struct usb_serial_port *port = tty->driver_data; |
Al Viro | 97c4965 | 2006-10-09 20:29:03 +0100 | [diff] [blame] | 2109 | void __user *argp = (void __user *)arg; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 2110 | struct moschip_port *mos7840_port; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 2111 | |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 2112 | if (mos7840_port_paranoia_check(port, __func__)) |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 2113 | return -1; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 2114 | |
| 2115 | mos7840_port = mos7840_get_port_private(port); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 2116 | |
| 2117 | if (mos7840_port == NULL) |
| 2118 | return -1; |
| 2119 | |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 2120 | dev_dbg(&port->dev, "%s - cmd = 0x%x\n", __func__, cmd); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 2121 | |
| 2122 | switch (cmd) { |
| 2123 | /* return number of bytes available */ |
| 2124 | |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 2125 | case TIOCSERGETLSR: |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 2126 | dev_dbg(&port->dev, "%s TIOCSERGETLSR\n", __func__); |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 2127 | return mos7840_get_lsr_info(tty, argp); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 2128 | |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 2129 | case TIOCGSERIAL: |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 2130 | dev_dbg(&port->dev, "%s TIOCGSERIAL\n", __func__); |
Al Viro | 97c4965 | 2006-10-09 20:29:03 +0100 | [diff] [blame] | 2131 | return mos7840_get_serial_info(mos7840_port, argp); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 2132 | |
| 2133 | case TIOCSSERIAL: |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 2134 | dev_dbg(&port->dev, "%s TIOCSSERIAL\n", __func__); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 2135 | break; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 2136 | default: |
| 2137 | break; |
| 2138 | } |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 2139 | return -ENOIOCTLCMD; |
| 2140 | } |
| 2141 | |
Donald | 0eafe4d | 2012-04-19 15:00:45 +0800 | [diff] [blame] | 2142 | static int mos7810_check(struct usb_serial *serial) |
| 2143 | { |
| 2144 | int i, pass_count = 0; |
| 2145 | __u16 data = 0, mcr_data = 0; |
| 2146 | __u16 test_pattern = 0x55AA; |
| 2147 | |
| 2148 | /* Store MCR setting */ |
| 2149 | usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0), |
| 2150 | MCS_RDREQ, MCS_RD_RTYPE, 0x0300, MODEM_CONTROL_REGISTER, |
| 2151 | &mcr_data, VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT); |
| 2152 | |
| 2153 | for (i = 0; i < 16; i++) { |
| 2154 | /* Send the 1-bit test pattern out to MCS7810 test pin */ |
| 2155 | usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), |
| 2156 | MCS_WRREQ, MCS_WR_RTYPE, |
| 2157 | (0x0300 | (((test_pattern >> i) & 0x0001) << 1)), |
| 2158 | MODEM_CONTROL_REGISTER, NULL, 0, MOS_WDR_TIMEOUT); |
| 2159 | |
| 2160 | /* Read the test pattern back */ |
| 2161 | usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0), |
| 2162 | MCS_RDREQ, MCS_RD_RTYPE, 0, GPIO_REGISTER, &data, |
| 2163 | VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT); |
| 2164 | |
| 2165 | /* If this is a MCS7810 device, both test patterns must match */ |
| 2166 | if (((test_pattern >> i) ^ (~data >> 1)) & 0x0001) |
| 2167 | break; |
| 2168 | |
| 2169 | pass_count++; |
| 2170 | } |
| 2171 | |
| 2172 | /* Restore MCR setting */ |
| 2173 | usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), MCS_WRREQ, |
| 2174 | MCS_WR_RTYPE, 0x0300 | mcr_data, MODEM_CONTROL_REGISTER, NULL, |
| 2175 | 0, MOS_WDR_TIMEOUT); |
| 2176 | |
| 2177 | if (pass_count == 16) |
| 2178 | return 1; |
| 2179 | |
| 2180 | return 0; |
| 2181 | } |
| 2182 | |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 2183 | static int mos7840_calc_num_ports(struct usb_serial *serial) |
| 2184 | { |
Donald | 0eafe4d | 2012-04-19 15:00:45 +0800 | [diff] [blame] | 2185 | __u16 data = 0x00; |
Donald Lee | 093ea2d | 2012-03-14 15:26:33 +0800 | [diff] [blame] | 2186 | int mos7840_num_ports; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 2187 | |
Donald | 0eafe4d | 2012-04-19 15:00:45 +0800 | [diff] [blame] | 2188 | usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0), |
| 2189 | MCS_RDREQ, MCS_RD_RTYPE, 0, GPIO_REGISTER, &data, |
Donald Lee | 093ea2d | 2012-03-14 15:26:33 +0800 | [diff] [blame] | 2190 | VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT); |
| 2191 | |
Donald | 0eafe4d | 2012-04-19 15:00:45 +0800 | [diff] [blame] | 2192 | if (serial->dev->descriptor.idProduct == MOSCHIP_DEVICE_ID_7810 || |
| 2193 | serial->dev->descriptor.idProduct == MOSCHIP_DEVICE_ID_7820) { |
| 2194 | device_type = serial->dev->descriptor.idProduct; |
Donald Lee | 093ea2d | 2012-03-14 15:26:33 +0800 | [diff] [blame] | 2195 | } else { |
Donald | 0eafe4d | 2012-04-19 15:00:45 +0800 | [diff] [blame] | 2196 | /* For a MCS7840 device GPIO0 must be set to 1 */ |
| 2197 | if ((data & 0x01) == 1) |
| 2198 | device_type = MOSCHIP_DEVICE_ID_7840; |
| 2199 | else if (mos7810_check(serial)) |
| 2200 | device_type = MOSCHIP_DEVICE_ID_7810; |
| 2201 | else |
| 2202 | device_type = MOSCHIP_DEVICE_ID_7820; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 2203 | } |
Donald Lee | 093ea2d | 2012-03-14 15:26:33 +0800 | [diff] [blame] | 2204 | |
Donald | 0eafe4d | 2012-04-19 15:00:45 +0800 | [diff] [blame] | 2205 | mos7840_num_ports = (device_type >> 4) & 0x000F; |
| 2206 | serial->num_bulk_in = mos7840_num_ports; |
| 2207 | serial->num_bulk_out = mos7840_num_ports; |
| 2208 | serial->num_ports = mos7840_num_ports; |
| 2209 | |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 2210 | return mos7840_num_ports; |
| 2211 | } |
| 2212 | |
Johan Hovold | 80c0075 | 2012-10-25 18:56:34 +0200 | [diff] [blame] | 2213 | static int mos7840_port_probe(struct usb_serial_port *port) |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 2214 | { |
Johan Hovold | 80c0075 | 2012-10-25 18:56:34 +0200 | [diff] [blame] | 2215 | struct usb_serial *serial = port->serial; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 2216 | struct moschip_port *mos7840_port; |
Johan Hovold | 80c0075 | 2012-10-25 18:56:34 +0200 | [diff] [blame] | 2217 | int status; |
| 2218 | int pnum; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 2219 | __u16 Data; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 2220 | |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 2221 | /* we set up the pointers to the endpoints in the mos7840_open * |
| 2222 | * function, as the structures aren't created yet. */ |
| 2223 | |
Johan Hovold | 80c0075 | 2012-10-25 18:56:34 +0200 | [diff] [blame] | 2224 | pnum = port->number - serial->minor; |
| 2225 | |
Johan Hovold | ae685ef | 2012-10-25 18:56:35 +0200 | [diff] [blame] | 2226 | dev_dbg(&port->dev, "mos7840_startup: configuring port %d\n", pnum); |
| 2227 | mos7840_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL); |
| 2228 | if (mos7840_port == NULL) { |
| 2229 | dev_err(&port->dev, "%s - Out of memory\n", __func__); |
| 2230 | return -ENOMEM; |
| 2231 | } |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 2232 | |
Johan Hovold | ae685ef | 2012-10-25 18:56:35 +0200 | [diff] [blame] | 2233 | /* Initialize all port interrupt end point to port 0 int |
| 2234 | * endpoint. Our device has only one interrupt end point |
| 2235 | * common to all port */ |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 2236 | |
Johan Hovold | ae685ef | 2012-10-25 18:56:35 +0200 | [diff] [blame] | 2237 | mos7840_port->port = port; |
| 2238 | mos7840_set_port_private(port, mos7840_port); |
| 2239 | spin_lock_init(&mos7840_port->pool_lock); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 2240 | |
Johan Hovold | ae685ef | 2012-10-25 18:56:35 +0200 | [diff] [blame] | 2241 | /* minor is not initialised until later by |
| 2242 | * usb-serial.c:get_free_serial() and cannot therefore be used |
| 2243 | * to index device instances */ |
| 2244 | mos7840_port->port_num = pnum + 1; |
| 2245 | dev_dbg(&port->dev, "port->number = %d\n", port->number); |
| 2246 | dev_dbg(&port->dev, "port->serial->minor = %d\n", port->serial->minor); |
| 2247 | dev_dbg(&port->dev, "mos7840_port->port_num = %d\n", mos7840_port->port_num); |
| 2248 | dev_dbg(&port->dev, "serial->minor = %d\n", serial->minor); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 2249 | |
Johan Hovold | ae685ef | 2012-10-25 18:56:35 +0200 | [diff] [blame] | 2250 | if (mos7840_port->port_num == 1) { |
| 2251 | mos7840_port->SpRegOffset = 0x0; |
| 2252 | mos7840_port->ControlRegOffset = 0x1; |
| 2253 | mos7840_port->DcrRegOffset = 0x4; |
| 2254 | } else if ((mos7840_port->port_num == 2) && (serial->num_ports == 4)) { |
| 2255 | mos7840_port->SpRegOffset = 0x8; |
| 2256 | mos7840_port->ControlRegOffset = 0x9; |
| 2257 | mos7840_port->DcrRegOffset = 0x16; |
| 2258 | } else if ((mos7840_port->port_num == 2) && (serial->num_ports == 2)) { |
| 2259 | mos7840_port->SpRegOffset = 0xa; |
| 2260 | mos7840_port->ControlRegOffset = 0xb; |
| 2261 | mos7840_port->DcrRegOffset = 0x19; |
| 2262 | } else if ((mos7840_port->port_num == 3) && (serial->num_ports == 4)) { |
| 2263 | mos7840_port->SpRegOffset = 0xa; |
| 2264 | mos7840_port->ControlRegOffset = 0xb; |
| 2265 | mos7840_port->DcrRegOffset = 0x19; |
| 2266 | } else if ((mos7840_port->port_num == 4) && (serial->num_ports == 4)) { |
| 2267 | mos7840_port->SpRegOffset = 0xc; |
| 2268 | mos7840_port->ControlRegOffset = 0xd; |
| 2269 | mos7840_port->DcrRegOffset = 0x1c; |
| 2270 | } |
| 2271 | mos7840_dump_serial_port(port, mos7840_port); |
| 2272 | mos7840_set_port_private(port, mos7840_port); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 2273 | |
Johan Hovold | ae685ef | 2012-10-25 18:56:35 +0200 | [diff] [blame] | 2274 | /* enable rx_disable bit in control register */ |
| 2275 | status = mos7840_get_reg_sync(port, |
| 2276 | mos7840_port->ControlRegOffset, &Data); |
| 2277 | if (status < 0) { |
| 2278 | dev_dbg(&port->dev, "Reading ControlReg failed status-0x%x\n", status); |
| 2279 | goto out; |
| 2280 | } else |
| 2281 | dev_dbg(&port->dev, "ControlReg Reading success val is %x, status%d\n", Data, status); |
| 2282 | Data |= 0x08; /* setting driver done bit */ |
| 2283 | Data |= 0x04; /* sp1_bit to have cts change reflect in |
| 2284 | modem status reg */ |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 2285 | |
Johan Hovold | ae685ef | 2012-10-25 18:56:35 +0200 | [diff] [blame] | 2286 | /* Data |= 0x20; //rx_disable bit */ |
| 2287 | status = mos7840_set_reg_sync(port, |
| 2288 | mos7840_port->ControlRegOffset, Data); |
| 2289 | if (status < 0) { |
| 2290 | dev_dbg(&port->dev, "Writing ControlReg failed(rx_disable) status-0x%x\n", status); |
| 2291 | goto out; |
| 2292 | } else |
| 2293 | dev_dbg(&port->dev, "ControlReg Writing success(rx_disable) status%d\n", status); |
| 2294 | |
| 2295 | /* Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2 |
| 2296 | and 0x24 in DCR3 */ |
| 2297 | Data = 0x01; |
| 2298 | status = mos7840_set_reg_sync(port, |
| 2299 | (__u16) (mos7840_port->DcrRegOffset + 0), Data); |
| 2300 | if (status < 0) { |
| 2301 | dev_dbg(&port->dev, "Writing DCR0 failed status-0x%x\n", status); |
| 2302 | goto out; |
| 2303 | } else |
| 2304 | dev_dbg(&port->dev, "DCR0 Writing success status%d\n", status); |
| 2305 | |
| 2306 | Data = 0x05; |
| 2307 | status = mos7840_set_reg_sync(port, |
| 2308 | (__u16) (mos7840_port->DcrRegOffset + 1), Data); |
| 2309 | if (status < 0) { |
| 2310 | dev_dbg(&port->dev, "Writing DCR1 failed status-0x%x\n", status); |
| 2311 | goto out; |
| 2312 | } else |
| 2313 | dev_dbg(&port->dev, "DCR1 Writing success status%d\n", status); |
| 2314 | |
| 2315 | Data = 0x24; |
| 2316 | status = mos7840_set_reg_sync(port, |
| 2317 | (__u16) (mos7840_port->DcrRegOffset + 2), Data); |
| 2318 | if (status < 0) { |
| 2319 | dev_dbg(&port->dev, "Writing DCR2 failed status-0x%x\n", status); |
| 2320 | goto out; |
| 2321 | } else |
| 2322 | dev_dbg(&port->dev, "DCR2 Writing success status%d\n", status); |
| 2323 | |
| 2324 | /* write values in clkstart0x0 and clkmulti 0x20 */ |
| 2325 | Data = 0x0; |
| 2326 | status = mos7840_set_reg_sync(port, CLK_START_VALUE_REGISTER, Data); |
| 2327 | if (status < 0) { |
| 2328 | dev_dbg(&port->dev, "Writing CLK_START_VALUE_REGISTER failed status-0x%x\n", status); |
| 2329 | goto out; |
| 2330 | } else |
| 2331 | dev_dbg(&port->dev, "CLK_START_VALUE_REGISTER Writing success status%d\n", status); |
| 2332 | |
| 2333 | Data = 0x20; |
| 2334 | status = mos7840_set_reg_sync(port, CLK_MULTI_REGISTER, Data); |
| 2335 | if (status < 0) { |
| 2336 | dev_dbg(&port->dev, "Writing CLK_MULTI_REGISTER failed status-0x%x\n", status); |
| 2337 | goto error; |
| 2338 | } else |
| 2339 | dev_dbg(&port->dev, "CLK_MULTI_REGISTER Writing success status%d\n", status); |
| 2340 | |
| 2341 | /* write value 0x0 to scratchpad register */ |
| 2342 | Data = 0x00; |
| 2343 | status = mos7840_set_uart_reg(port, SCRATCH_PAD_REGISTER, Data); |
| 2344 | if (status < 0) { |
| 2345 | dev_dbg(&port->dev, "Writing SCRATCH_PAD_REGISTER failed status-0x%x\n", status); |
| 2346 | goto out; |
| 2347 | } else |
| 2348 | dev_dbg(&port->dev, "SCRATCH_PAD_REGISTER Writing success status%d\n", status); |
| 2349 | |
| 2350 | /* Zero Length flag register */ |
| 2351 | if ((mos7840_port->port_num != 1) && (serial->num_ports == 2)) { |
| 2352 | Data = 0xff; |
Johan Hovold | 80c0075 | 2012-10-25 18:56:34 +0200 | [diff] [blame] | 2353 | status = mos7840_set_reg_sync(port, |
Johan Hovold | ae685ef | 2012-10-25 18:56:35 +0200 | [diff] [blame] | 2354 | (__u16) (ZLP_REG1 + |
| 2355 | ((__u16)mos7840_port->port_num)), Data); |
| 2356 | dev_dbg(&port->dev, "ZLIP offset %x\n", |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 2357 | (__u16)(ZLP_REG1 + ((__u16) mos7840_port->port_num))); |
Johan Hovold | ae685ef | 2012-10-25 18:56:35 +0200 | [diff] [blame] | 2358 | if (status < 0) { |
| 2359 | dev_dbg(&port->dev, "Writing ZLP_REG%d failed status-0x%x\n", pnum + 2, status); |
| 2360 | goto out; |
| 2361 | } else |
| 2362 | dev_dbg(&port->dev, "ZLP_REG%d Writing success status%d\n", pnum + 2, status); |
| 2363 | } else { |
| 2364 | Data = 0xff; |
| 2365 | status = mos7840_set_reg_sync(port, |
| 2366 | (__u16) (ZLP_REG1 + |
| 2367 | ((__u16)mos7840_port->port_num) - 0x1), Data); |
| 2368 | dev_dbg(&port->dev, "ZLIP offset %x\n", |
Greg Kroah-Hartman | 9c134a1 | 2012-09-14 16:06:04 -0700 | [diff] [blame] | 2369 | (__u16)(ZLP_REG1 + ((__u16) mos7840_port->port_num) - 0x1)); |
Johan Hovold | ae685ef | 2012-10-25 18:56:35 +0200 | [diff] [blame] | 2370 | if (status < 0) { |
| 2371 | dev_dbg(&port->dev, "Writing ZLP_REG%d failed status-0x%x\n", pnum + 1, status); |
| 2372 | goto out; |
| 2373 | } else |
| 2374 | dev_dbg(&port->dev, "ZLP_REG%d Writing success status%d\n", pnum + 1, status); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 2375 | |
Johan Hovold | ae685ef | 2012-10-25 18:56:35 +0200 | [diff] [blame] | 2376 | } |
| 2377 | mos7840_port->control_urb = usb_alloc_urb(0, GFP_KERNEL); |
| 2378 | mos7840_port->ctrl_buf = kmalloc(16, GFP_KERNEL); |
| 2379 | mos7840_port->dr = kmalloc(sizeof(struct usb_ctrlrequest), |
| 2380 | GFP_KERNEL); |
| 2381 | if (!mos7840_port->control_urb || !mos7840_port->ctrl_buf || |
| 2382 | !mos7840_port->dr) { |
| 2383 | status = -ENOMEM; |
| 2384 | goto error; |
| 2385 | } |
Donald | 0eafe4d | 2012-04-19 15:00:45 +0800 | [diff] [blame] | 2386 | |
Johan Hovold | ae685ef | 2012-10-25 18:56:35 +0200 | [diff] [blame] | 2387 | mos7840_port->has_led = false; |
Donald | 0eafe4d | 2012-04-19 15:00:45 +0800 | [diff] [blame] | 2388 | |
Johan Hovold | ae685ef | 2012-10-25 18:56:35 +0200 | [diff] [blame] | 2389 | /* Initialize LED timers */ |
| 2390 | if (device_type == MOSCHIP_DEVICE_ID_7810) { |
| 2391 | mos7840_port->has_led = true; |
Donald | 0eafe4d | 2012-04-19 15:00:45 +0800 | [diff] [blame] | 2392 | |
Johan Hovold | ae685ef | 2012-10-25 18:56:35 +0200 | [diff] [blame] | 2393 | init_timer(&mos7840_port->led_timer1); |
| 2394 | mos7840_port->led_timer1.function = mos7840_led_off; |
| 2395 | mos7840_port->led_timer1.expires = |
| 2396 | jiffies + msecs_to_jiffies(LED_ON_MS); |
| 2397 | mos7840_port->led_timer1.data = (unsigned long)mos7840_port; |
Donald | 0eafe4d | 2012-04-19 15:00:45 +0800 | [diff] [blame] | 2398 | |
Johan Hovold | ae685ef | 2012-10-25 18:56:35 +0200 | [diff] [blame] | 2399 | init_timer(&mos7840_port->led_timer2); |
| 2400 | mos7840_port->led_timer2.function = mos7840_led_flag_off; |
| 2401 | mos7840_port->led_timer2.expires = |
| 2402 | jiffies + msecs_to_jiffies(LED_OFF_MS); |
| 2403 | mos7840_port->led_timer2.data = (unsigned long)mos7840_port; |
Donald | 0eafe4d | 2012-04-19 15:00:45 +0800 | [diff] [blame] | 2404 | |
Johan Hovold | ae685ef | 2012-10-25 18:56:35 +0200 | [diff] [blame] | 2405 | mos7840_port->led_flag = false; |
Donald | 0eafe4d | 2012-04-19 15:00:45 +0800 | [diff] [blame] | 2406 | |
Johan Hovold | ae685ef | 2012-10-25 18:56:35 +0200 | [diff] [blame] | 2407 | /* Turn off LED */ |
| 2408 | mos7840_set_led_sync(port, MODEM_CONTROL_REGISTER, 0x0300); |
| 2409 | } |
| 2410 | out: |
Johan Hovold | 80c0075 | 2012-10-25 18:56:34 +0200 | [diff] [blame] | 2411 | if (pnum == serial->num_ports - 1) { |
| 2412 | /* Zero Length flag enable */ |
| 2413 | Data = 0x0f; |
| 2414 | status = mos7840_set_reg_sync(serial->port[0], ZLP_REG5, Data); |
| 2415 | if (status < 0) { |
| 2416 | dev_dbg(&port->dev, "Writing ZLP_REG5 failed status-0x%x\n", status); |
| 2417 | goto error; |
| 2418 | } else |
| 2419 | dev_dbg(&port->dev, "ZLP_REG5 Writing success status%d\n", status); |
| 2420 | |
| 2421 | /* setting configuration feature to one */ |
| 2422 | usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), |
| 2423 | 0x03, 0x00, 0x01, 0x00, NULL, 0x00, |
| 2424 | MOS_WDR_TIMEOUT); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 2425 | } |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 2426 | return 0; |
Oliver Neukum | 0de9a70 | 2007-03-16 20:28:28 +0100 | [diff] [blame] | 2427 | error: |
Johan Hovold | 80c0075 | 2012-10-25 18:56:34 +0200 | [diff] [blame] | 2428 | kfree(mos7840_port->dr); |
| 2429 | kfree(mos7840_port->ctrl_buf); |
| 2430 | usb_free_urb(mos7840_port->control_urb); |
| 2431 | kfree(mos7840_port); |
Oliver Neukum | 0de9a70 | 2007-03-16 20:28:28 +0100 | [diff] [blame] | 2432 | |
Oliver Neukum | 0de9a70 | 2007-03-16 20:28:28 +0100 | [diff] [blame] | 2433 | return status; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 2434 | } |
| 2435 | |
Johan Hovold | 80c0075 | 2012-10-25 18:56:34 +0200 | [diff] [blame] | 2436 | static int mos7840_port_remove(struct usb_serial_port *port) |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 2437 | { |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 2438 | struct moschip_port *mos7840_port; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 2439 | |
Johan Hovold | 80c0075 | 2012-10-25 18:56:34 +0200 | [diff] [blame] | 2440 | mos7840_port = mos7840_get_port_private(port); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 2441 | |
Johan Hovold | 80c0075 | 2012-10-25 18:56:34 +0200 | [diff] [blame] | 2442 | if (mos7840_port->has_led) { |
| 2443 | /* Turn off LED */ |
| 2444 | mos7840_set_led_sync(port, MODEM_CONTROL_REGISTER, 0x0300); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 2445 | |
Johan Hovold | 80c0075 | 2012-10-25 18:56:34 +0200 | [diff] [blame] | 2446 | del_timer_sync(&mos7840_port->led_timer1); |
| 2447 | del_timer_sync(&mos7840_port->led_timer2); |
Alan Stern | f9c99bb | 2009-06-02 11:53:55 -0400 | [diff] [blame] | 2448 | } |
Johan Hovold | 80c0075 | 2012-10-25 18:56:34 +0200 | [diff] [blame] | 2449 | usb_kill_urb(mos7840_port->control_urb); |
| 2450 | usb_free_urb(mos7840_port->control_urb); |
| 2451 | kfree(mos7840_port->ctrl_buf); |
| 2452 | kfree(mos7840_port->dr); |
| 2453 | kfree(mos7840_port); |
Alan Stern | f9c99bb | 2009-06-02 11:53:55 -0400 | [diff] [blame] | 2454 | |
Johan Hovold | 80c0075 | 2012-10-25 18:56:34 +0200 | [diff] [blame] | 2455 | return 0; |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 2456 | } |
| 2457 | |
| 2458 | static struct usb_serial_driver moschip7840_4port_device = { |
| 2459 | .driver = { |
| 2460 | .owner = THIS_MODULE, |
| 2461 | .name = "mos7840", |
| 2462 | }, |
| 2463 | .description = DRIVER_DESC, |
Greg Kroah-Hartman | 68e2411 | 2012-05-08 15:46:14 -0700 | [diff] [blame] | 2464 | .id_table = id_table, |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 2465 | .num_ports = 4, |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 2466 | .open = mos7840_open, |
| 2467 | .close = mos7840_close, |
| 2468 | .write = mos7840_write, |
| 2469 | .write_room = mos7840_write_room, |
| 2470 | .chars_in_buffer = mos7840_chars_in_buffer, |
| 2471 | .throttle = mos7840_throttle, |
| 2472 | .unthrottle = mos7840_unthrottle, |
| 2473 | .calc_num_ports = mos7840_calc_num_ports, |
| 2474 | #ifdef MCSSerialProbe |
| 2475 | .probe = mos7840_serial_probe, |
| 2476 | #endif |
| 2477 | .ioctl = mos7840_ioctl, |
| 2478 | .set_termios = mos7840_set_termios, |
| 2479 | .break_ctl = mos7840_break, |
| 2480 | .tiocmget = mos7840_tiocmget, |
| 2481 | .tiocmset = mos7840_tiocmset, |
Johan Hovold | 0c613371 | 2013-03-21 12:37:17 +0100 | [diff] [blame] | 2482 | .tiocmiwait = usb_serial_generic_tiocmiwait, |
Johan Hovold | 8c1a07f | 2013-03-21 12:37:16 +0100 | [diff] [blame] | 2483 | .get_icount = usb_serial_generic_get_icount, |
Johan Hovold | 80c0075 | 2012-10-25 18:56:34 +0200 | [diff] [blame] | 2484 | .port_probe = mos7840_port_probe, |
| 2485 | .port_remove = mos7840_port_remove, |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 2486 | .read_bulk_callback = mos7840_bulk_in_callback, |
| 2487 | .read_int_callback = mos7840_interrupt_callback, |
| 2488 | }; |
| 2489 | |
Alan Stern | 4d2a7af | 2012-02-23 14:57:09 -0500 | [diff] [blame] | 2490 | static struct usb_serial_driver * const serial_drivers[] = { |
| 2491 | &moschip7840_4port_device, NULL |
| 2492 | }; |
| 2493 | |
Greg Kroah-Hartman | 68e2411 | 2012-05-08 15:46:14 -0700 | [diff] [blame] | 2494 | module_usb_serial_driver(serial_drivers, id_table); |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 2495 | |
Paul B Schroeder | 3f54297 | 2006-08-31 19:41:47 -0500 | [diff] [blame] | 2496 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 2497 | MODULE_LICENSE("GPL"); |