blob: 04bef4bebc4a47311665b8f7112bb12de1dd3dea [file] [log] [blame]
Paul B Schroeder3f542972006-08-31 19:41:47 -05001/*
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>
Alexey Dobriyan405f5572009-07-11 22:08:37 +040029#include <linux/smp_lock.h>
Paul B Schroeder3f542972006-08-31 19:41:47 -050030#include <linux/tty.h>
31#include <linux/tty_driver.h>
32#include <linux/tty_flip.h>
33#include <linux/module.h>
34#include <linux/serial.h>
35#include <linux/usb.h>
36#include <linux/usb/serial.h>
Alan Cox880af9d2008-07-22 11:16:12 +010037#include <linux/uaccess.h>
Paul B Schroeder3f542972006-08-31 19:41:47 -050038
39/*
40 * Version Information
41 */
Tony Cook37768ad2009-04-18 22:42:18 +093042#define DRIVER_VERSION "1.3.2"
Paul B Schroeder3f542972006-08-31 19:41:47 -050043#define DRIVER_DESC "Moschip 7840/7820 USB Serial Driver"
44
45/*
46 * 16C50 UART register defines
47 */
48
49#define LCR_BITS_5 0x00 /* 5 bits/char */
50#define LCR_BITS_6 0x01 /* 6 bits/char */
51#define LCR_BITS_7 0x02 /* 7 bits/char */
52#define LCR_BITS_8 0x03 /* 8 bits/char */
53#define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
54
55#define LCR_STOP_1 0x00 /* 1 stop bit */
56#define LCR_STOP_1_5 0x04 /* 1.5 stop bits (if 5 bits/char) */
57#define LCR_STOP_2 0x04 /* 2 stop bits (if 6-8 bits/char) */
58#define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
59
60#define LCR_PAR_NONE 0x00 /* No parity */
61#define LCR_PAR_ODD 0x08 /* Odd parity */
62#define LCR_PAR_EVEN 0x18 /* Even parity */
63#define LCR_PAR_MARK 0x28 /* Force parity bit to 1 */
64#define LCR_PAR_SPACE 0x38 /* Force parity bit to 0 */
65#define LCR_PAR_MASK 0x38 /* Mask for parity field */
66
67#define LCR_SET_BREAK 0x40 /* Set Break condition */
68#define LCR_DL_ENABLE 0x80 /* Enable access to divisor latch */
69
70#define MCR_DTR 0x01 /* Assert DTR */
71#define MCR_RTS 0x02 /* Assert RTS */
72#define MCR_OUT1 0x04 /* Loopback only: Sets state of RI */
73#define MCR_MASTER_IE 0x08 /* Enable interrupt outputs */
74#define MCR_LOOPBACK 0x10 /* Set internal (digital) loopback mode */
75#define MCR_XON_ANY 0x20 /* Enable any char to exit XOFF mode */
76
77#define MOS7840_MSR_CTS 0x10 /* Current state of CTS */
78#define MOS7840_MSR_DSR 0x20 /* Current state of DSR */
79#define MOS7840_MSR_RI 0x40 /* Current state of RI */
80#define MOS7840_MSR_CD 0x80 /* Current state of CD */
81
82/*
83 * Defines used for sending commands to port
84 */
85
Alan Cox880af9d2008-07-22 11:16:12 +010086#define WAIT_FOR_EVER (HZ * 0) /* timeout urb is wait for ever */
87#define MOS_WDR_TIMEOUT (HZ * 5) /* default urb timeout */
Paul B Schroeder3f542972006-08-31 19:41:47 -050088
89#define MOS_PORT1 0x0200
90#define MOS_PORT2 0x0300
91#define MOS_VENREG 0x0000
92#define MOS_MAX_PORT 0x02
93#define MOS_WRITE 0x0E
94#define MOS_READ 0x0D
95
96/* Requests */
97#define MCS_RD_RTYPE 0xC0
98#define MCS_WR_RTYPE 0x40
99#define MCS_RDREQ 0x0D
100#define MCS_WRREQ 0x0E
101#define MCS_CTRL_TIMEOUT 500
102#define VENDOR_READ_LENGTH (0x01)
103
104#define MAX_NAME_LEN 64
105
Alan Cox880af9d2008-07-22 11:16:12 +0100106#define ZLP_REG1 0x3A /* Zero_Flag_Reg1 58 */
107#define ZLP_REG5 0x3E /* Zero_Flag_Reg5 62 */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500108
109/* For higher baud Rates use TIOCEXBAUD */
110#define TIOCEXBAUD 0x5462
111
112/* vendor id and device id defines */
113
David Ludlow11e1abb2008-02-25 17:30:52 -0500114/* The native mos7840/7820 component */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500115#define USB_VENDOR_ID_MOSCHIP 0x9710
116#define MOSCHIP_DEVICE_ID_7840 0x7840
117#define MOSCHIP_DEVICE_ID_7820 0x7820
David Ludlow11e1abb2008-02-25 17:30:52 -0500118/* The native component can have its vendor/device id's overridden
119 * in vendor-specific implementations. Such devices can be handled
120 * by making a change here, in moschip_port_id_table, and in
121 * moschip_id_table_combined
122 */
123#define USB_VENDOR_ID_BANDB 0x0856
Cliff Brakeacf509a2009-12-01 09:53:43 -0500124#define BANDB_DEVICE_ID_USO9ML2_2 0xAC22
125#define BANDB_DEVICE_ID_USO9ML2_4 0xAC24
126#define BANDB_DEVICE_ID_US9ML2_2 0xAC29
127#define BANDB_DEVICE_ID_US9ML2_4 0xAC30
128#define BANDB_DEVICE_ID_USPTL4_2 0xAC31
129#define BANDB_DEVICE_ID_USPTL4_4 0xAC32
Blaise Gassend27f12812009-12-18 15:23:38 -0800130#define BANDB_DEVICE_ID_USOPTL4_2 0xAC42
131#define BANDB_DEVICE_ID_USOPTL4_4 0xAC44
132#define BANDB_DEVICE_ID_USOPTL2_4 0xAC24
Paul B Schroeder3f542972006-08-31 19:41:47 -0500133
Russell Lang9d498be2009-07-17 19:29:20 +1000134/* This driver also supports
135 * ATEN UC2324 device using Moschip MCS7840
136 * ATEN UC2322 device using Moschip MCS7820
137 */
Tony Cooke9b8cff2009-04-18 22:42:18 +0930138#define USB_VENDOR_ID_ATENINTL 0x0557
139#define ATENINTL_DEVICE_ID_UC2324 0x2011
Russell Lang9d498be2009-07-17 19:29:20 +1000140#define ATENINTL_DEVICE_ID_UC2322 0x7820
Tony Cooke9b8cff2009-04-18 22:42:18 +0930141
David Ludlow11e1abb2008-02-25 17:30:52 -0500142/* Interrupt Routine Defines */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500143
144#define SERIAL_IIR_RLS 0x06
145#define SERIAL_IIR_MS 0x00
146
147/*
148 * Emulation of the bit mask on the LINE STATUS REGISTER.
149 */
150#define SERIAL_LSR_DR 0x0001
151#define SERIAL_LSR_OE 0x0002
152#define SERIAL_LSR_PE 0x0004
153#define SERIAL_LSR_FE 0x0008
154#define SERIAL_LSR_BI 0x0010
155
156#define MOS_MSR_DELTA_CTS 0x10
157#define MOS_MSR_DELTA_DSR 0x20
158#define MOS_MSR_DELTA_RI 0x40
159#define MOS_MSR_DELTA_CD 0x80
160
Alan Cox880af9d2008-07-22 11:16:12 +0100161/* Serial Port register Address */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500162#define INTERRUPT_ENABLE_REGISTER ((__u16)(0x01))
163#define FIFO_CONTROL_REGISTER ((__u16)(0x02))
164#define LINE_CONTROL_REGISTER ((__u16)(0x03))
165#define MODEM_CONTROL_REGISTER ((__u16)(0x04))
166#define LINE_STATUS_REGISTER ((__u16)(0x05))
167#define MODEM_STATUS_REGISTER ((__u16)(0x06))
168#define SCRATCH_PAD_REGISTER ((__u16)(0x07))
169#define DIVISOR_LATCH_LSB ((__u16)(0x00))
170#define DIVISOR_LATCH_MSB ((__u16)(0x01))
171
172#define CLK_MULTI_REGISTER ((__u16)(0x02))
173#define CLK_START_VALUE_REGISTER ((__u16)(0x03))
174
175#define SERIAL_LCR_DLAB ((__u16)(0x0080))
176
177/*
178 * URB POOL related defines
179 */
180#define NUM_URBS 16 /* URB Count */
181#define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
182
183
184static struct usb_device_id moschip_port_id_table[] = {
185 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7840)},
186 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7820)},
Cliff Brakeacf509a2009-12-01 09:53:43 -0500187 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2)},
188 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4)},
189 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_2)},
190 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_4)},
191 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_2)},
192 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_4)},
David Ludlow11e1abb2008-02-25 17:30:52 -0500193 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2)},
Cliff Brakeacf509a2009-12-01 09:53:43 -0500194 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4)},
Blaise Gassend27f12812009-12-18 15:23:38 -0800195 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL2_4)},
Tony Cooke9b8cff2009-04-18 22:42:18 +0930196 {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2324)},
Russell Lang9d498be2009-07-17 19:29:20 +1000197 {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2322)},
Paul B Schroeder3f542972006-08-31 19:41:47 -0500198 {} /* terminating entry */
199};
200
201static __devinitdata struct usb_device_id moschip_id_table_combined[] = {
202 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7840)},
203 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7820)},
Cliff Brakeacf509a2009-12-01 09:53:43 -0500204 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2)},
205 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4)},
206 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_2)},
207 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_4)},
208 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_2)},
209 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_4)},
David Ludlow11e1abb2008-02-25 17:30:52 -0500210 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2)},
Cliff Brakeacf509a2009-12-01 09:53:43 -0500211 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4)},
Blaise Gassend27f12812009-12-18 15:23:38 -0800212 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL2_4)},
Tony Cooke9b8cff2009-04-18 22:42:18 +0930213 {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2324)},
Russell Lang9d498be2009-07-17 19:29:20 +1000214 {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2322)},
Paul B Schroeder3f542972006-08-31 19:41:47 -0500215 {} /* terminating entry */
216};
217
218MODULE_DEVICE_TABLE(usb, moschip_id_table_combined);
219
220/* This structure holds all of the local port information */
221
222struct moschip_port {
223 int port_num; /*Actual port number in the device(1,2,etc) */
224 struct urb *write_urb; /* write URB for this port */
225 struct urb *read_urb; /* read URB for this port */
Oliver Neukum0de9a702007-03-16 20:28:28 +0100226 struct urb *int_urb;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500227 __u8 shadowLCR; /* last LCR value received */
228 __u8 shadowMCR; /* last MCR value received */
229 char open;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100230 char open_ports;
231 char zombie;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500232 wait_queue_head_t wait_chase; /* for handling sleeping while waiting for chase to finish */
233 wait_queue_head_t delta_msr_wait; /* for handling sleeping while waiting for msr change to happen */
234 int delta_msr_cond;
235 struct async_icount icount;
236 struct usb_serial_port *port; /* loop back to the owner of this object */
237
Alan Cox880af9d2008-07-22 11:16:12 +0100238 /* Offsets */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500239 __u8 SpRegOffset;
240 __u8 ControlRegOffset;
241 __u8 DcrRegOffset;
Alan Cox880af9d2008-07-22 11:16:12 +0100242 /* for processing control URBS in interrupt context */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500243 struct urb *control_urb;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100244 struct usb_ctrlrequest *dr;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500245 char *ctrl_buf;
246 int MsrLsr;
247
Oliver Neukum0de9a702007-03-16 20:28:28 +0100248 spinlock_t pool_lock;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500249 struct urb *write_urb_pool[NUM_URBS];
Oliver Neukum0de9a702007-03-16 20:28:28 +0100250 char busy[NUM_URBS];
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800251 bool read_urb_busy;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500252};
253
254
255static int debug;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500256
257/*
258 * mos7840_set_reg_sync
259 * To set the Control register by calling usb_fill_control_urb function
260 * by passing usb_sndctrlpipe function as parameter.
261 */
262
263static int mos7840_set_reg_sync(struct usb_serial_port *port, __u16 reg,
264 __u16 val)
265{
266 struct usb_device *dev = port->serial->dev;
267 val = val & 0x00ff;
Tony Cook84fe6e72009-04-18 22:55:06 +0930268 dbg("mos7840_set_reg_sync offset is %x, value %x", reg, val);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500269
270 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
271 MCS_WR_RTYPE, val, reg, NULL, 0,
272 MOS_WDR_TIMEOUT);
273}
274
275/*
276 * mos7840_get_reg_sync
277 * To set the Uart register by calling usb_fill_control_urb function by
278 * passing usb_rcvctrlpipe function as parameter.
279 */
280
281static int mos7840_get_reg_sync(struct usb_serial_port *port, __u16 reg,
Alan Cox880af9d2008-07-22 11:16:12 +0100282 __u16 *val)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500283{
284 struct usb_device *dev = port->serial->dev;
285 int ret = 0;
Johan Hovold9e221a32009-12-28 23:01:55 +0100286 u8 *buf;
287
288 buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
289 if (!buf)
290 return -ENOMEM;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500291
292 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
Johan Hovold9e221a32009-12-28 23:01:55 +0100293 MCS_RD_RTYPE, 0, reg, buf, VENDOR_READ_LENGTH,
Paul B Schroeder3f542972006-08-31 19:41:47 -0500294 MOS_WDR_TIMEOUT);
Johan Hovold9e221a32009-12-28 23:01:55 +0100295 *val = buf[0];
Tony Cook84fe6e72009-04-18 22:55:06 +0930296 dbg("mos7840_get_reg_sync offset is %x, return val %x", reg, *val);
Johan Hovold9e221a32009-12-28 23:01:55 +0100297
298 kfree(buf);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500299 return ret;
300}
301
302/*
303 * mos7840_set_uart_reg
304 * To set the Uart register by calling usb_fill_control_urb function by
305 * passing usb_sndctrlpipe function as parameter.
306 */
307
308static int mos7840_set_uart_reg(struct usb_serial_port *port, __u16 reg,
309 __u16 val)
310{
311
312 struct usb_device *dev = port->serial->dev;
313 val = val & 0x00ff;
Alan Cox880af9d2008-07-22 11:16:12 +0100314 /* For the UART control registers, the application number need
315 to be Or'ed */
Oliver Neukum0de9a702007-03-16 20:28:28 +0100316 if (port->serial->num_ports == 4) {
Alan Cox880af9d2008-07-22 11:16:12 +0100317 val |= (((__u16) port->number -
318 (__u16) (port->serial->minor)) + 1) << 8;
Tony Cook84fe6e72009-04-18 22:55:06 +0930319 dbg("mos7840_set_uart_reg application number is %x", val);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500320 } else {
321 if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
Alan Cox880af9d2008-07-22 11:16:12 +0100322 val |= (((__u16) port->number -
Paul B Schroeder3f542972006-08-31 19:41:47 -0500323 (__u16) (port->serial->minor)) + 1) << 8;
Tony Cook84fe6e72009-04-18 22:55:06 +0930324 dbg("mos7840_set_uart_reg application number is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -0500325 val);
326 } else {
327 val |=
328 (((__u16) port->number -
329 (__u16) (port->serial->minor)) + 2) << 8;
Tony Cook84fe6e72009-04-18 22:55:06 +0930330 dbg("mos7840_set_uart_reg application number is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -0500331 val);
332 }
333 }
334 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
335 MCS_WR_RTYPE, val, reg, NULL, 0,
336 MOS_WDR_TIMEOUT);
337
338}
339
340/*
341 * mos7840_get_uart_reg
342 * To set the Control register by calling usb_fill_control_urb function
343 * by passing usb_rcvctrlpipe function as parameter.
344 */
345static int mos7840_get_uart_reg(struct usb_serial_port *port, __u16 reg,
Alan Cox880af9d2008-07-22 11:16:12 +0100346 __u16 *val)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500347{
348 struct usb_device *dev = port->serial->dev;
349 int ret = 0;
350 __u16 Wval;
Johan Hovold9e221a32009-12-28 23:01:55 +0100351 u8 *buf;
352
353 buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
354 if (!buf)
355 return -ENOMEM;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500356
Tony Cook84fe6e72009-04-18 22:55:06 +0930357 /* dbg("application number is %4x",
Alan Cox880af9d2008-07-22 11:16:12 +0100358 (((__u16)port->number - (__u16)(port->serial->minor))+1)<<8); */
359 /* Wval is same as application number */
Oliver Neukum0de9a702007-03-16 20:28:28 +0100360 if (port->serial->num_ports == 4) {
Paul B Schroeder3f542972006-08-31 19:41:47 -0500361 Wval =
362 (((__u16) port->number - (__u16) (port->serial->minor)) +
363 1) << 8;
Tony Cook84fe6e72009-04-18 22:55:06 +0930364 dbg("mos7840_get_uart_reg application number is %x", Wval);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500365 } else {
366 if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
Alan Cox880af9d2008-07-22 11:16:12 +0100367 Wval = (((__u16) port->number -
Paul B Schroeder3f542972006-08-31 19:41:47 -0500368 (__u16) (port->serial->minor)) + 1) << 8;
Tony Cook84fe6e72009-04-18 22:55:06 +0930369 dbg("mos7840_get_uart_reg application number is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -0500370 Wval);
371 } else {
Alan Cox880af9d2008-07-22 11:16:12 +0100372 Wval = (((__u16) port->number -
Paul B Schroeder3f542972006-08-31 19:41:47 -0500373 (__u16) (port->serial->minor)) + 2) << 8;
Tony Cook84fe6e72009-04-18 22:55:06 +0930374 dbg("mos7840_get_uart_reg application number is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -0500375 Wval);
376 }
377 }
378 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
Johan Hovold9e221a32009-12-28 23:01:55 +0100379 MCS_RD_RTYPE, Wval, reg, buf, VENDOR_READ_LENGTH,
Paul B Schroeder3f542972006-08-31 19:41:47 -0500380 MOS_WDR_TIMEOUT);
Johan Hovold9e221a32009-12-28 23:01:55 +0100381 *val = buf[0];
382
383 kfree(buf);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500384 return ret;
385}
386
387static void mos7840_dump_serial_port(struct moschip_port *mos7840_port)
388{
389
Tony Cook84fe6e72009-04-18 22:55:06 +0930390 dbg("***************************************");
391 dbg("SpRegOffset is %2x", mos7840_port->SpRegOffset);
392 dbg("ControlRegOffset is %2x", mos7840_port->ControlRegOffset);
393 dbg("DCRRegOffset is %2x", mos7840_port->DcrRegOffset);
394 dbg("***************************************");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500395
396}
397
398/************************************************************************/
399/************************************************************************/
400/* I N T E R F A C E F U N C T I O N S */
401/* I N T E R F A C E F U N C T I O N S */
402/************************************************************************/
403/************************************************************************/
404
405static inline void mos7840_set_port_private(struct usb_serial_port *port,
406 struct moschip_port *data)
407{
408 usb_set_serial_port_data(port, (void *)data);
409}
410
411static inline struct moschip_port *mos7840_get_port_private(struct
412 usb_serial_port
413 *port)
414{
415 return (struct moschip_port *)usb_get_serial_port_data(port);
416}
417
Oliver Neukum0de9a702007-03-16 20:28:28 +0100418static void mos7840_handle_new_msr(struct moschip_port *port, __u8 new_msr)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500419{
420 struct moschip_port *mos7840_port;
421 struct async_icount *icount;
422 mos7840_port = port;
423 icount = &mos7840_port->icount;
424 if (new_msr &
425 (MOS_MSR_DELTA_CTS | MOS_MSR_DELTA_DSR | MOS_MSR_DELTA_RI |
426 MOS_MSR_DELTA_CD)) {
427 icount = &mos7840_port->icount;
428
429 /* update input line counters */
430 if (new_msr & MOS_MSR_DELTA_CTS) {
431 icount->cts++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100432 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500433 }
434 if (new_msr & MOS_MSR_DELTA_DSR) {
435 icount->dsr++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100436 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500437 }
438 if (new_msr & MOS_MSR_DELTA_CD) {
439 icount->dcd++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100440 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500441 }
442 if (new_msr & MOS_MSR_DELTA_RI) {
443 icount->rng++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100444 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500445 }
446 }
Paul B Schroeder3f542972006-08-31 19:41:47 -0500447}
448
Oliver Neukum0de9a702007-03-16 20:28:28 +0100449static void mos7840_handle_new_lsr(struct moschip_port *port, __u8 new_lsr)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500450{
451 struct async_icount *icount;
452
Harvey Harrison441b62c2008-03-03 16:08:34 -0800453 dbg("%s - %02x", __func__, new_lsr);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500454
455 if (new_lsr & SERIAL_LSR_BI) {
Alan Cox880af9d2008-07-22 11:16:12 +0100456 /*
457 * Parity and Framing errors only count if they
458 * occur exclusive of a break being
459 * received.
460 */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500461 new_lsr &= (__u8) (SERIAL_LSR_OE | SERIAL_LSR_BI);
462 }
463
464 /* update input line counters */
465 icount = &port->icount;
466 if (new_lsr & SERIAL_LSR_BI) {
467 icount->brk++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100468 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500469 }
470 if (new_lsr & SERIAL_LSR_OE) {
471 icount->overrun++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100472 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500473 }
474 if (new_lsr & SERIAL_LSR_PE) {
475 icount->parity++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100476 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500477 }
478 if (new_lsr & SERIAL_LSR_FE) {
479 icount->frame++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100480 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500481 }
Paul B Schroeder3f542972006-08-31 19:41:47 -0500482}
483
484/************************************************************************/
485/************************************************************************/
486/* U S B C A L L B A C K F U N C T I O N S */
487/* U S B C A L L B A C K F U N C T I O N S */
488/************************************************************************/
489/************************************************************************/
490
David Howells7d12e782006-10-05 14:55:46 +0100491static void mos7840_control_callback(struct urb *urb)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500492{
493 unsigned char *data;
494 struct moschip_port *mos7840_port;
495 __u8 regval = 0x0;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100496 int result = 0;
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700497 int status = urb->status;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500498
Ming Leicdc97792008-02-24 18:41:47 +0800499 mos7840_port = urb->context;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100500
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700501 switch (status) {
Paul B Schroeder3f542972006-08-31 19:41:47 -0500502 case 0:
503 /* success */
504 break;
505 case -ECONNRESET:
506 case -ENOENT:
507 case -ESHUTDOWN:
508 /* this urb is terminated, clean up */
Harvey Harrison441b62c2008-03-03 16:08:34 -0800509 dbg("%s - urb shutting down with status: %d", __func__,
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700510 status);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500511 return;
512 default:
Harvey Harrison441b62c2008-03-03 16:08:34 -0800513 dbg("%s - nonzero urb status received: %d", __func__,
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700514 status);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500515 goto exit;
516 }
517
Tony Cook84fe6e72009-04-18 22:55:06 +0930518 dbg("%s urb buffer size is %d", __func__, urb->actual_length);
519 dbg("%s mos7840_port->MsrLsr is %d port %d", __func__,
Paul B Schroeder3f542972006-08-31 19:41:47 -0500520 mos7840_port->MsrLsr, mos7840_port->port_num);
521 data = urb->transfer_buffer;
522 regval = (__u8) data[0];
Tony Cook84fe6e72009-04-18 22:55:06 +0930523 dbg("%s data is %x", __func__, regval);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500524 if (mos7840_port->MsrLsr == 0)
525 mos7840_handle_new_msr(mos7840_port, regval);
526 else if (mos7840_port->MsrLsr == 1)
527 mos7840_handle_new_lsr(mos7840_port, regval);
528
Oliver Neukum0de9a702007-03-16 20:28:28 +0100529exit:
530 spin_lock(&mos7840_port->pool_lock);
531 if (!mos7840_port->zombie)
532 result = usb_submit_urb(mos7840_port->int_urb, GFP_ATOMIC);
533 spin_unlock(&mos7840_port->pool_lock);
534 if (result) {
535 dev_err(&urb->dev->dev,
536 "%s - Error %d submitting interrupt urb\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800537 __func__, result);
Oliver Neukum0de9a702007-03-16 20:28:28 +0100538 }
Paul B Schroeder3f542972006-08-31 19:41:47 -0500539}
540
541static int mos7840_get_reg(struct moschip_port *mcs, __u16 Wval, __u16 reg,
Alan Cox880af9d2008-07-22 11:16:12 +0100542 __u16 *val)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500543{
544 struct usb_device *dev = mcs->port->serial->dev;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100545 struct usb_ctrlrequest *dr = mcs->dr;
546 unsigned char *buffer = mcs->ctrl_buf;
547 int ret;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500548
Paul B Schroeder3f542972006-08-31 19:41:47 -0500549 dr->bRequestType = MCS_RD_RTYPE;
550 dr->bRequest = MCS_RDREQ;
Alan Cox880af9d2008-07-22 11:16:12 +0100551 dr->wValue = cpu_to_le16(Wval); /* 0 */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500552 dr->wIndex = cpu_to_le16(reg);
553 dr->wLength = cpu_to_le16(2);
554
555 usb_fill_control_urb(mcs->control_urb, dev, usb_rcvctrlpipe(dev, 0),
556 (unsigned char *)dr, buffer, 2,
557 mos7840_control_callback, mcs);
558 mcs->control_urb->transfer_buffer_length = 2;
559 ret = usb_submit_urb(mcs->control_urb, GFP_ATOMIC);
560 return ret;
561}
562
563/*****************************************************************************
564 * mos7840_interrupt_callback
565 * this is the callback function for when we have received data on the
566 * interrupt endpoint.
567 *****************************************************************************/
568
David Howells7d12e782006-10-05 14:55:46 +0100569static void mos7840_interrupt_callback(struct urb *urb)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500570{
571 int result;
572 int length;
573 struct moschip_port *mos7840_port;
574 struct usb_serial *serial;
575 __u16 Data;
576 unsigned char *data;
577 __u8 sp[5], st;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100578 int i, rv = 0;
579 __u16 wval, wreg = 0;
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700580 int status = urb->status;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500581
Tony Cook84fe6e72009-04-18 22:55:06 +0930582 dbg("%s", " : Entering");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500583
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700584 switch (status) {
Paul B Schroeder3f542972006-08-31 19:41:47 -0500585 case 0:
586 /* success */
587 break;
588 case -ECONNRESET:
589 case -ENOENT:
590 case -ESHUTDOWN:
591 /* this urb is terminated, clean up */
Harvey Harrison441b62c2008-03-03 16:08:34 -0800592 dbg("%s - urb shutting down with status: %d", __func__,
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700593 status);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500594 return;
595 default:
Harvey Harrison441b62c2008-03-03 16:08:34 -0800596 dbg("%s - nonzero urb status received: %d", __func__,
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700597 status);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500598 goto exit;
599 }
600
601 length = urb->actual_length;
602 data = urb->transfer_buffer;
603
Ming Leicdc97792008-02-24 18:41:47 +0800604 serial = urb->context;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500605
606 /* Moschip get 5 bytes
607 * Byte 1 IIR Port 1 (port.number is 0)
608 * Byte 2 IIR Port 2 (port.number is 1)
609 * Byte 3 IIR Port 3 (port.number is 2)
610 * Byte 4 IIR Port 4 (port.number is 3)
611 * Byte 5 FIFO status for both */
612
613 if (length && length > 5) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930614 dbg("%s", "Wrong data !!!");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500615 return;
616 }
617
618 sp[0] = (__u8) data[0];
619 sp[1] = (__u8) data[1];
620 sp[2] = (__u8) data[2];
621 sp[3] = (__u8) data[3];
622 st = (__u8) data[4];
623
624 for (i = 0; i < serial->num_ports; i++) {
625 mos7840_port = mos7840_get_port_private(serial->port[i]);
626 wval =
627 (((__u16) serial->port[i]->number -
628 (__u16) (serial->minor)) + 1) << 8;
629 if (mos7840_port->open) {
630 if (sp[i] & 0x01) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930631 dbg("SP%d No Interrupt !!!", i);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500632 } else {
633 switch (sp[i] & 0x0f) {
634 case SERIAL_IIR_RLS:
635 dbg("Serial Port %d: Receiver status error or ", i);
Tony Cook84fe6e72009-04-18 22:55:06 +0930636 dbg("address bit detected in 9-bit mode");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500637 mos7840_port->MsrLsr = 1;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100638 wreg = LINE_STATUS_REGISTER;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500639 break;
640 case SERIAL_IIR_MS:
Tony Cook84fe6e72009-04-18 22:55:06 +0930641 dbg("Serial Port %d: Modem status change", i);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500642 mos7840_port->MsrLsr = 0;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100643 wreg = MODEM_STATUS_REGISTER;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500644 break;
645 }
Oliver Neukum0de9a702007-03-16 20:28:28 +0100646 spin_lock(&mos7840_port->pool_lock);
647 if (!mos7840_port->zombie) {
648 rv = mos7840_get_reg(mos7840_port, wval, wreg, &Data);
649 } else {
650 spin_unlock(&mos7840_port->pool_lock);
651 return;
652 }
653 spin_unlock(&mos7840_port->pool_lock);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500654 }
655 }
656 }
Alan Cox880af9d2008-07-22 11:16:12 +0100657 if (!(rv < 0))
658 /* the completion handler for the control urb will resubmit */
Oliver Neukum0de9a702007-03-16 20:28:28 +0100659 return;
660exit:
Paul B Schroeder3f542972006-08-31 19:41:47 -0500661 result = usb_submit_urb(urb, GFP_ATOMIC);
662 if (result) {
663 dev_err(&urb->dev->dev,
664 "%s - Error %d submitting interrupt urb\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800665 __func__, result);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500666 }
Paul B Schroeder3f542972006-08-31 19:41:47 -0500667}
668
669static int mos7840_port_paranoia_check(struct usb_serial_port *port,
670 const char *function)
671{
672 if (!port) {
673 dbg("%s - port == NULL", function);
674 return -1;
675 }
676 if (!port->serial) {
677 dbg("%s - port->serial == NULL", function);
678 return -1;
679 }
680
681 return 0;
682}
683
684/* Inline functions to check the sanity of a pointer that is passed to us */
685static int mos7840_serial_paranoia_check(struct usb_serial *serial,
686 const char *function)
687{
688 if (!serial) {
689 dbg("%s - serial == NULL", function);
690 return -1;
691 }
692 if (!serial->type) {
693 dbg("%s - serial->type == NULL!", function);
694 return -1;
695 }
696
697 return 0;
698}
699
700static struct usb_serial *mos7840_get_usb_serial(struct usb_serial_port *port,
701 const char *function)
702{
703 /* if no port was specified, or it fails a paranoia check */
704 if (!port ||
705 mos7840_port_paranoia_check(port, function) ||
706 mos7840_serial_paranoia_check(port->serial, function)) {
Alan Cox880af9d2008-07-22 11:16:12 +0100707 /* then say that we don't have a valid usb_serial thing,
708 * which will end up genrating -ENODEV return values */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500709 return NULL;
710 }
711
712 return port->serial;
713}
714
715/*****************************************************************************
716 * mos7840_bulk_in_callback
717 * this is the callback function for when we have received data on the
718 * bulk in endpoint.
719 *****************************************************************************/
720
David Howells7d12e782006-10-05 14:55:46 +0100721static void mos7840_bulk_in_callback(struct urb *urb)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500722{
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700723 int retval;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500724 unsigned char *data;
725 struct usb_serial *serial;
726 struct usb_serial_port *port;
727 struct moschip_port *mos7840_port;
728 struct tty_struct *tty;
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700729 int status = urb->status;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500730
Ming Leicdc97792008-02-24 18:41:47 +0800731 mos7840_port = urb->context;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500732 if (!mos7840_port) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930733 dbg("%s", "NULL mos7840_port pointer");
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800734 mos7840_port->read_urb_busy = false;
735 return;
736 }
737
738 if (status) {
739 dbg("nonzero read bulk status received: %d", status);
740 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500741 return;
742 }
743
744 port = (struct usb_serial_port *)mos7840_port->port;
Harvey Harrison441b62c2008-03-03 16:08:34 -0800745 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930746 dbg("%s", "Port Paranoia failed");
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800747 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500748 return;
749 }
750
Harvey Harrison441b62c2008-03-03 16:08:34 -0800751 serial = mos7840_get_usb_serial(port, __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500752 if (!serial) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930753 dbg("%s", "Bad serial pointer");
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800754 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500755 return;
756 }
757
Tony Cook84fe6e72009-04-18 22:55:06 +0930758 dbg("%s", "Entering... ");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500759
760 data = urb->transfer_buffer;
761
Tony Cook84fe6e72009-04-18 22:55:06 +0930762 dbg("%s", "Entering ...........");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500763
764 if (urb->actual_length) {
Alan Cox4a90f092008-10-13 10:39:46 +0100765 tty = tty_port_tty_get(&mos7840_port->port->port);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500766 if (tty) {
767 tty_buffer_request_room(tty, urb->actual_length);
768 tty_insert_flip_string(tty, data, urb->actual_length);
Tony Cook84fe6e72009-04-18 22:55:06 +0930769 dbg(" %s ", data);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500770 tty_flip_buffer_push(tty);
Alan Cox4a90f092008-10-13 10:39:46 +0100771 tty_kref_put(tty);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500772 }
773 mos7840_port->icount.rx += urb->actual_length;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100774 smp_wmb();
Tony Cook84fe6e72009-04-18 22:55:06 +0930775 dbg("mos7840_port->icount.rx is %d:",
Paul B Schroeder3f542972006-08-31 19:41:47 -0500776 mos7840_port->icount.rx);
777 }
778
779 if (!mos7840_port->read_urb) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930780 dbg("%s", "URB KILLED !!!");
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800781 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500782 return;
783 }
784
Paul B Schroeder3f542972006-08-31 19:41:47 -0500785
Oliver Neukum0de9a702007-03-16 20:28:28 +0100786 mos7840_port->read_urb->dev = serial->dev;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500787
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800788 mos7840_port->read_urb_busy = true;
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700789 retval = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
Oliver Neukum0de9a702007-03-16 20:28:28 +0100790
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700791 if (retval) {
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800792 dbg("usb_submit_urb(read bulk) failed, retval = %d", retval);
793 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500794 }
795}
796
797/*****************************************************************************
798 * mos7840_bulk_out_data_callback
Alan Cox880af9d2008-07-22 11:16:12 +0100799 * this is the callback function for when we have finished sending
800 * serial data on the bulk out endpoint.
Paul B Schroeder3f542972006-08-31 19:41:47 -0500801 *****************************************************************************/
802
David Howells7d12e782006-10-05 14:55:46 +0100803static void mos7840_bulk_out_data_callback(struct urb *urb)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500804{
805 struct moschip_port *mos7840_port;
806 struct tty_struct *tty;
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700807 int status = urb->status;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100808 int i;
809
Ming Leicdc97792008-02-24 18:41:47 +0800810 mos7840_port = urb->context;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100811 spin_lock(&mos7840_port->pool_lock);
812 for (i = 0; i < NUM_URBS; i++) {
813 if (urb == mos7840_port->write_urb_pool[i]) {
814 mos7840_port->busy[i] = 0;
815 break;
816 }
817 }
818 spin_unlock(&mos7840_port->pool_lock);
819
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700820 if (status) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930821 dbg("nonzero write bulk status received:%d", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500822 return;
823 }
824
Harvey Harrison441b62c2008-03-03 16:08:34 -0800825 if (mos7840_port_paranoia_check(mos7840_port->port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930826 dbg("%s", "Port Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500827 return;
828 }
829
Tony Cook84fe6e72009-04-18 22:55:06 +0930830 dbg("%s", "Entering .........");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500831
Alan Cox4a90f092008-10-13 10:39:46 +0100832 tty = tty_port_tty_get(&mos7840_port->port->port);
Jiri Slabyb963a842007-02-10 01:44:55 -0800833 if (tty && mos7840_port->open)
834 tty_wakeup(tty);
Alan Cox4a90f092008-10-13 10:39:46 +0100835 tty_kref_put(tty);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500836
837}
838
839/************************************************************************/
840/* 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 */
841/************************************************************************/
842#ifdef MCSSerialProbe
843static int mos7840_serial_probe(struct usb_serial *serial,
844 const struct usb_device_id *id)
845{
846
847 /*need to implement the mode_reg reading and updating\
848 structures usb_serial_ device_type\
849 (i.e num_ports, num_bulkin,bulkout etc) */
850 /* Also we can update the changes attach */
851 return 1;
852}
853#endif
854
855/*****************************************************************************
856 * mos7840_open
857 * this function is called by the tty driver when a port is opened
858 * If successful, we return 0
859 * Otherwise we return a negative error number.
860 *****************************************************************************/
861
Alan Coxa509a7e2009-09-19 13:13:26 -0700862static int mos7840_open(struct tty_struct *tty, struct usb_serial_port *port)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500863{
864 int response;
865 int j;
866 struct usb_serial *serial;
867 struct urb *urb;
868 __u16 Data;
869 int status;
870 struct moschip_port *mos7840_port;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100871 struct moschip_port *port0;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500872
Tony Cook84fe6e72009-04-18 22:55:06 +0930873 dbg ("%s enter", __func__);
874
Harvey Harrison441b62c2008-03-03 16:08:34 -0800875 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930876 dbg("%s", "Port Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500877 return -ENODEV;
878 }
879
Paul B Schroeder3f542972006-08-31 19:41:47 -0500880 serial = port->serial;
881
Harvey Harrison441b62c2008-03-03 16:08:34 -0800882 if (mos7840_serial_paranoia_check(serial, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930883 dbg("%s", "Serial Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500884 return -ENODEV;
885 }
886
887 mos7840_port = mos7840_get_port_private(port);
Oliver Neukum0de9a702007-03-16 20:28:28 +0100888 port0 = mos7840_get_port_private(serial->port[0]);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500889
Oliver Neukum0de9a702007-03-16 20:28:28 +0100890 if (mos7840_port == NULL || port0 == NULL)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500891 return -ENODEV;
892
893 usb_clear_halt(serial->dev, port->write_urb->pipe);
894 usb_clear_halt(serial->dev, port->read_urb->pipe);
Oliver Neukum0de9a702007-03-16 20:28:28 +0100895 port0->open_ports++;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500896
897 /* Initialising the write urb pool */
898 for (j = 0; j < NUM_URBS; ++j) {
Oliver Neukum0de9a702007-03-16 20:28:28 +0100899 urb = usb_alloc_urb(0, GFP_KERNEL);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500900 mos7840_port->write_urb_pool[j] = urb;
901
902 if (urb == NULL) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700903 dev_err(&port->dev, "No more urbs???\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500904 continue;
905 }
906
Alan Cox880af9d2008-07-22 11:16:12 +0100907 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
908 GFP_KERNEL);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500909 if (!urb->transfer_buffer) {
Oliver Neukum0de9a702007-03-16 20:28:28 +0100910 usb_free_urb(urb);
911 mos7840_port->write_urb_pool[j] = NULL;
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700912 dev_err(&port->dev,
913 "%s-out of memory for urb buffers.\n",
914 __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500915 continue;
916 }
917 }
918
919/*****************************************************************************
920 * Initialize MCS7840 -- Write Init values to corresponding Registers
921 *
922 * Register Index
923 * 1 : IER
924 * 2 : FCR
925 * 3 : LCR
926 * 4 : MCR
927 *
928 * 0x08 : SP1/2 Control Reg
929 *****************************************************************************/
930
Alan Cox880af9d2008-07-22 11:16:12 +0100931 /* NEED to check the following Block */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500932
Paul B Schroeder3f542972006-08-31 19:41:47 -0500933 Data = 0x0;
934 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
935 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930936 dbg("Reading Spreg failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500937 return -1;
938 }
939 Data |= 0x80;
940 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
941 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930942 dbg("writing Spreg failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500943 return -1;
944 }
945
946 Data &= ~0x80;
947 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
948 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930949 dbg("writing Spreg failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500950 return -1;
951 }
Alan Cox880af9d2008-07-22 11:16:12 +0100952 /* End of block to be checked */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500953
Paul B Schroeder3f542972006-08-31 19:41:47 -0500954 Data = 0x0;
Alan Cox880af9d2008-07-22 11:16:12 +0100955 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
956 &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500957 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930958 dbg("Reading Controlreg failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500959 return -1;
960 }
Alan Cox880af9d2008-07-22 11:16:12 +0100961 Data |= 0x08; /* Driver done bit */
962 Data |= 0x20; /* rx_disable */
963 status = mos7840_set_reg_sync(port,
964 mos7840_port->ControlRegOffset, Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500965 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930966 dbg("writing Controlreg failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500967 return -1;
968 }
Alan Cox880af9d2008-07-22 11:16:12 +0100969 /* do register settings here */
970 /* Set all regs to the device default values. */
971 /***********************************
972 * First Disable all interrupts.
973 ***********************************/
Paul B Schroeder3f542972006-08-31 19:41:47 -0500974 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500975 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
976 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930977 dbg("disabling interrupts failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500978 return -1;
979 }
Alan Cox880af9d2008-07-22 11:16:12 +0100980 /* Set FIFO_CONTROL_REGISTER to the default value */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500981 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500982 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
983 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930984 dbg("Writing FIFO_CONTROL_REGISTER failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500985 return -1;
986 }
987
988 Data = 0xcf;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500989 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
990 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930991 dbg("Writing FIFO_CONTROL_REGISTER failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500992 return -1;
993 }
994
995 Data = 0x03;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500996 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
997 mos7840_port->shadowLCR = Data;
998
999 Data = 0x0b;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001000 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1001 mos7840_port->shadowMCR = Data;
1002
1003 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001004 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
1005 mos7840_port->shadowLCR = Data;
1006
Alan Cox880af9d2008-07-22 11:16:12 +01001007 Data |= SERIAL_LCR_DLAB; /* data latch enable in LCR 0x80 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001008 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1009
1010 Data = 0x0c;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001011 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
1012
1013 Data = 0x0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001014 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
1015
1016 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001017 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
1018
1019 Data = Data & ~SERIAL_LCR_DLAB;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001020 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1021 mos7840_port->shadowLCR = Data;
1022
Alan Cox880af9d2008-07-22 11:16:12 +01001023 /* clearing Bulkin and Bulkout Fifo */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001024 Data = 0x0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001025 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
1026
1027 Data = Data | 0x0c;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001028 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
1029
1030 Data = Data & ~0x0c;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001031 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
Alan Cox880af9d2008-07-22 11:16:12 +01001032 /* Finally enable all interrupts */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001033 Data = 0x0c;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001034 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1035
Alan Cox880af9d2008-07-22 11:16:12 +01001036 /* clearing rx_disable */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001037 Data = 0x0;
Alan Cox880af9d2008-07-22 11:16:12 +01001038 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1039 &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001040 Data = Data & ~0x20;
Alan Cox880af9d2008-07-22 11:16:12 +01001041 status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1042 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001043
Alan Cox880af9d2008-07-22 11:16:12 +01001044 /* rx_negate */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001045 Data = 0x0;
Alan Cox880af9d2008-07-22 11:16:12 +01001046 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1047 &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001048 Data = Data | 0x10;
Alan Cox880af9d2008-07-22 11:16:12 +01001049 status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1050 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001051
Alan Cox880af9d2008-07-22 11:16:12 +01001052 /* Check to see if we've set up our endpoint info yet *
1053 * (can't set it up in mos7840_startup as the structures *
1054 * were not set up at that time.) */
Oliver Neukum0de9a702007-03-16 20:28:28 +01001055 if (port0->open_ports == 1) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001056 if (serial->port[0]->interrupt_in_buffer == NULL) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001057 /* set up interrupt urb */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001058 usb_fill_int_urb(serial->port[0]->interrupt_in_urb,
Alan Cox880af9d2008-07-22 11:16:12 +01001059 serial->dev,
1060 usb_rcvintpipe(serial->dev,
1061 serial->port[0]->interrupt_in_endpointAddress),
1062 serial->port[0]->interrupt_in_buffer,
1063 serial->port[0]->interrupt_in_urb->
1064 transfer_buffer_length,
1065 mos7840_interrupt_callback,
1066 serial,
1067 serial->port[0]->interrupt_in_urb->interval);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001068
1069 /* start interrupt read for mos7840 *
1070 * will continue as long as mos7840 is connected */
1071
1072 response =
1073 usb_submit_urb(serial->port[0]->interrupt_in_urb,
1074 GFP_KERNEL);
1075 if (response) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001076 dev_err(&port->dev, "%s - Error %d submitting "
1077 "interrupt urb\n", __func__, response);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001078 }
1079
1080 }
1081
1082 }
1083
1084 /* see if we've set up our endpoint info yet *
1085 * (can't set it up in mos7840_startup as the *
1086 * structures were not set up at that time.) */
1087
Tony Cook84fe6e72009-04-18 22:55:06 +09301088 dbg("port number is %d", port->number);
1089 dbg("serial number is %d", port->serial->minor);
1090 dbg("Bulkin endpoint is %d", port->bulk_in_endpointAddress);
1091 dbg("BulkOut endpoint is %d", port->bulk_out_endpointAddress);
1092 dbg("Interrupt endpoint is %d", port->interrupt_in_endpointAddress);
1093 dbg("port's number in the device is %d", mos7840_port->port_num);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001094 mos7840_port->read_urb = port->read_urb;
1095
1096 /* set up our bulk in urb */
1097
1098 usb_fill_bulk_urb(mos7840_port->read_urb,
1099 serial->dev,
1100 usb_rcvbulkpipe(serial->dev,
1101 port->bulk_in_endpointAddress),
1102 port->bulk_in_buffer,
1103 mos7840_port->read_urb->transfer_buffer_length,
1104 mos7840_bulk_in_callback, mos7840_port);
1105
Tony Cook84fe6e72009-04-18 22:55:06 +09301106 dbg("mos7840_open: bulkin endpoint is %d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05001107 port->bulk_in_endpointAddress);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08001108 mos7840_port->read_urb_busy = true;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001109 response = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
1110 if (response) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001111 dev_err(&port->dev, "%s - Error %d submitting control urb\n",
1112 __func__, response);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08001113 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001114 }
1115
1116 /* initialize our wait queues */
1117 init_waitqueue_head(&mos7840_port->wait_chase);
1118 init_waitqueue_head(&mos7840_port->delta_msr_wait);
1119
1120 /* initialize our icount structure */
1121 memset(&(mos7840_port->icount), 0x00, sizeof(mos7840_port->icount));
1122
1123 /* initialize our port settings */
Alan Cox880af9d2008-07-22 11:16:12 +01001124 /* Must set to enable ints! */
1125 mos7840_port->shadowMCR = MCR_MASTER_IE;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001126 /* send a open port command */
1127 mos7840_port->open = 1;
Alan Cox880af9d2008-07-22 11:16:12 +01001128 /* mos7840_change_port_settings(mos7840_port,old_termios); */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001129 mos7840_port->icount.tx = 0;
1130 mos7840_port->icount.rx = 0;
1131
Tony Cook84fe6e72009-04-18 22:55:06 +09301132 dbg("usb_serial serial:%p mos7840_port:%p\n usb_serial_port port:%p",
Alan Cox880af9d2008-07-22 11:16:12 +01001133 serial, mos7840_port, port);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001134
Tony Cook84fe6e72009-04-18 22:55:06 +09301135 dbg ("%s leave", __func__);
1136
Paul B Schroeder3f542972006-08-31 19:41:47 -05001137 return 0;
1138
1139}
1140
1141/*****************************************************************************
1142 * mos7840_chars_in_buffer
1143 * this function is called by the tty driver when it wants to know how many
1144 * bytes of data we currently have outstanding in the port (data that has
1145 * been written, but hasn't made it out the port yet)
1146 * If successful, we return the number of bytes left to be written in the
1147 * system,
Alan Cox95da3102008-07-22 11:09:07 +01001148 * Otherwise we return zero.
Paul B Schroeder3f542972006-08-31 19:41:47 -05001149 *****************************************************************************/
1150
Alan Cox95da3102008-07-22 11:09:07 +01001151static int mos7840_chars_in_buffer(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001152{
Alan Cox95da3102008-07-22 11:09:07 +01001153 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001154 int i;
1155 int chars = 0;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001156 unsigned long flags;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001157 struct moschip_port *mos7840_port;
1158
Tony Cook84fe6e72009-04-18 22:55:06 +09301159 dbg("%s", " mos7840_chars_in_buffer:entering ...........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001160
Harvey Harrison441b62c2008-03-03 16:08:34 -08001161 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301162 dbg("%s", "Invalid port");
Alan Cox95da3102008-07-22 11:09:07 +01001163 return 0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001164 }
1165
1166 mos7840_port = mos7840_get_port_private(port);
1167 if (mos7840_port == NULL) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301168 dbg("%s", "mos7840_break:leaving ...........");
Alan Cox95da3102008-07-22 11:09:07 +01001169 return 0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001170 }
1171
Alan Cox880af9d2008-07-22 11:16:12 +01001172 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
Alan Cox95da3102008-07-22 11:09:07 +01001173 for (i = 0; i < NUM_URBS; ++i)
1174 if (mos7840_port->busy[i])
Paul B Schroeder3f542972006-08-31 19:41:47 -05001175 chars += URB_TRANSFER_BUFFER_SIZE;
Alan Cox880af9d2008-07-22 11:16:12 +01001176 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
Harvey Harrison441b62c2008-03-03 16:08:34 -08001177 dbg("%s - returns %d", __func__, chars);
Oliver Neukum0de9a702007-03-16 20:28:28 +01001178 return chars;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001179
1180}
1181
Paul B Schroeder3f542972006-08-31 19:41:47 -05001182/*****************************************************************************
1183 * mos7840_close
1184 * this function is called by the tty driver when a port is closed
1185 *****************************************************************************/
1186
Alan Cox335f8512009-06-11 12:26:29 +01001187static void mos7840_close(struct usb_serial_port *port)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001188{
1189 struct usb_serial *serial;
1190 struct moschip_port *mos7840_port;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001191 struct moschip_port *port0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001192 int j;
1193 __u16 Data;
1194
Tony Cook84fe6e72009-04-18 22:55:06 +09301195 dbg("%s", "mos7840_close:entering...");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001196
Harvey Harrison441b62c2008-03-03 16:08:34 -08001197 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301198 dbg("%s", "Port Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001199 return;
1200 }
1201
Harvey Harrison441b62c2008-03-03 16:08:34 -08001202 serial = mos7840_get_usb_serial(port, __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001203 if (!serial) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301204 dbg("%s", "Serial Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001205 return;
1206 }
1207
1208 mos7840_port = mos7840_get_port_private(port);
Oliver Neukum0de9a702007-03-16 20:28:28 +01001209 port0 = mos7840_get_port_private(serial->port[0]);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001210
Oliver Neukum0de9a702007-03-16 20:28:28 +01001211 if (mos7840_port == NULL || port0 == NULL)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001212 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001213
1214 for (j = 0; j < NUM_URBS; ++j)
1215 usb_kill_urb(mos7840_port->write_urb_pool[j]);
1216
1217 /* Freeing Write URBs */
1218 for (j = 0; j < NUM_URBS; ++j) {
1219 if (mos7840_port->write_urb_pool[j]) {
1220 if (mos7840_port->write_urb_pool[j]->transfer_buffer)
1221 kfree(mos7840_port->write_urb_pool[j]->
1222 transfer_buffer);
1223
1224 usb_free_urb(mos7840_port->write_urb_pool[j]);
1225 }
1226 }
1227
Paul B Schroeder3f542972006-08-31 19:41:47 -05001228 /* While closing port, shutdown all bulk read, write *
1229 * and interrupt read if they exists */
1230 if (serial->dev) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001231 if (mos7840_port->write_urb) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301232 dbg("%s", "Shutdown bulk write");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001233 usb_kill_urb(mos7840_port->write_urb);
1234 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001235 if (mos7840_port->read_urb) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301236 dbg("%s", "Shutdown bulk read");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001237 usb_kill_urb(mos7840_port->read_urb);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08001238 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001239 }
1240 if ((&mos7840_port->control_urb)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301241 dbg("%s", "Shutdown control read");
Alan Cox880af9d2008-07-22 11:16:12 +01001242 /*/ usb_kill_urb (mos7840_port->control_urb); */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001243 }
1244 }
Alan Cox880af9d2008-07-22 11:16:12 +01001245/* if(mos7840_port->ctrl_buf != NULL) */
1246/* kfree(mos7840_port->ctrl_buf); */
Oliver Neukum0de9a702007-03-16 20:28:28 +01001247 port0->open_ports--;
Tony Cook84fe6e72009-04-18 22:55:06 +09301248 dbg("mos7840_num_open_ports in close%d:in port%d",
Oliver Neukum0de9a702007-03-16 20:28:28 +01001249 port0->open_ports, port->number);
1250 if (port0->open_ports == 0) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001251 if (serial->port[0]->interrupt_in_urb) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301252 dbg("%s", "Shutdown interrupt_in_urb");
Oliver Neukum0de9a702007-03-16 20:28:28 +01001253 usb_kill_urb(serial->port[0]->interrupt_in_urb);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001254 }
1255 }
1256
1257 if (mos7840_port->write_urb) {
1258 /* if this urb had a transfer buffer already (old tx) free it */
Alan Cox880af9d2008-07-22 11:16:12 +01001259 if (mos7840_port->write_urb->transfer_buffer != NULL)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001260 kfree(mos7840_port->write_urb->transfer_buffer);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001261 usb_free_urb(mos7840_port->write_urb);
1262 }
1263
1264 Data = 0x0;
1265 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1266
1267 Data = 0x00;
1268 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1269
1270 mos7840_port->open = 0;
1271
Tony Cook84fe6e72009-04-18 22:55:06 +09301272 dbg("%s", "Leaving ............");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001273}
1274
1275/************************************************************************
1276 *
1277 * mos7840_block_until_chase_response
1278 *
1279 * This function will block the close until one of the following:
1280 * 1. Response to our Chase comes from mos7840
Joe Perchesdc0d5c12007-12-17 11:40:18 -08001281 * 2. A timeout of 10 seconds without activity has expired
Paul B Schroeder3f542972006-08-31 19:41:47 -05001282 * (1K of mos7840 data @ 2400 baud ==> 4 sec to empty)
1283 *
1284 ************************************************************************/
1285
Alan Cox95da3102008-07-22 11:09:07 +01001286static void mos7840_block_until_chase_response(struct tty_struct *tty,
1287 struct moschip_port *mos7840_port)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001288{
1289 int timeout = 1 * HZ;
1290 int wait = 10;
1291 int count;
1292
1293 while (1) {
Alan Cox95da3102008-07-22 11:09:07 +01001294 count = mos7840_chars_in_buffer(tty);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001295
1296 /* Check for Buffer status */
Alan Cox880af9d2008-07-22 11:16:12 +01001297 if (count <= 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001298 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001299
1300 /* Block the thread for a while */
1301 interruptible_sleep_on_timeout(&mos7840_port->wait_chase,
1302 timeout);
1303 /* No activity.. count down section */
1304 wait--;
1305 if (wait == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001306 dbg("%s - TIMEOUT", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001307 return;
1308 } else {
Joe Perchesdc0d5c12007-12-17 11:40:18 -08001309 /* Reset timeout value back to seconds */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001310 wait = 10;
1311 }
1312 }
1313
1314}
1315
1316/*****************************************************************************
1317 * mos7840_break
1318 * this function sends a break to the port
1319 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001320static void mos7840_break(struct tty_struct *tty, int break_state)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001321{
Alan Cox95da3102008-07-22 11:09:07 +01001322 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001323 unsigned char data;
1324 struct usb_serial *serial;
1325 struct moschip_port *mos7840_port;
1326
Tony Cook84fe6e72009-04-18 22:55:06 +09301327 dbg("%s", "Entering ...........");
1328 dbg("mos7840_break: Start");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001329
Harvey Harrison441b62c2008-03-03 16:08:34 -08001330 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301331 dbg("%s", "Port Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001332 return;
1333 }
1334
Harvey Harrison441b62c2008-03-03 16:08:34 -08001335 serial = mos7840_get_usb_serial(port, __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001336 if (!serial) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301337 dbg("%s", "Serial Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001338 return;
1339 }
1340
1341 mos7840_port = mos7840_get_port_private(port);
1342
Alan Cox880af9d2008-07-22 11:16:12 +01001343 if (mos7840_port == NULL)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001344 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001345
Alan Cox95da3102008-07-22 11:09:07 +01001346 if (serial->dev)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001347 /* flush and block until tx is empty */
Alan Cox95da3102008-07-22 11:09:07 +01001348 mos7840_block_until_chase_response(tty, mos7840_port);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001349
Alan Cox95da3102008-07-22 11:09:07 +01001350 if (break_state == -1)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001351 data = mos7840_port->shadowLCR | LCR_SET_BREAK;
Alan Cox95da3102008-07-22 11:09:07 +01001352 else
Paul B Schroeder3f542972006-08-31 19:41:47 -05001353 data = mos7840_port->shadowLCR & ~LCR_SET_BREAK;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001354
Alan Cox6b447f042009-01-02 13:48:56 +00001355 /* FIXME: no locking on shadowLCR anywhere in driver */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001356 mos7840_port->shadowLCR = data;
Tony Cook84fe6e72009-04-18 22:55:06 +09301357 dbg("mcs7840_break mos7840_port->shadowLCR is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05001358 mos7840_port->shadowLCR);
1359 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER,
1360 mos7840_port->shadowLCR);
1361
1362 return;
1363}
1364
1365/*****************************************************************************
1366 * mos7840_write_room
1367 * this function is called by the tty driver when it wants to know how many
1368 * bytes of data we can accept for a specific port.
1369 * If successful, we return the amount of room that we have for this port
1370 * Otherwise we return a negative error number.
1371 *****************************************************************************/
1372
Alan Cox95da3102008-07-22 11:09:07 +01001373static int mos7840_write_room(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001374{
Alan Cox95da3102008-07-22 11:09:07 +01001375 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001376 int i;
1377 int room = 0;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001378 unsigned long flags;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001379 struct moschip_port *mos7840_port;
1380
Tony Cook84fe6e72009-04-18 22:55:06 +09301381 dbg("%s", " mos7840_write_room:entering ...........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001382
Harvey Harrison441b62c2008-03-03 16:08:34 -08001383 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301384 dbg("%s", "Invalid port");
1385 dbg("%s", " mos7840_write_room:leaving ...........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001386 return -1;
1387 }
1388
1389 mos7840_port = mos7840_get_port_private(port);
1390 if (mos7840_port == NULL) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301391 dbg("%s", "mos7840_break:leaving ...........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001392 return -1;
1393 }
1394
Oliver Neukum0de9a702007-03-16 20:28:28 +01001395 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001396 for (i = 0; i < NUM_URBS; ++i) {
Alan Cox880af9d2008-07-22 11:16:12 +01001397 if (!mos7840_port->busy[i])
Paul B Schroeder3f542972006-08-31 19:41:47 -05001398 room += URB_TRANSFER_BUFFER_SIZE;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001399 }
Oliver Neukum0de9a702007-03-16 20:28:28 +01001400 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001401
Oliver Neukum0de9a702007-03-16 20:28:28 +01001402 room = (room == 0) ? 0 : room - URB_TRANSFER_BUFFER_SIZE + 1;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001403 dbg("%s - returns %d", __func__, room);
Oliver Neukum0de9a702007-03-16 20:28:28 +01001404 return room;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001405
1406}
1407
1408/*****************************************************************************
1409 * mos7840_write
1410 * this function is called by the tty driver when data should be written to
1411 * the port.
1412 * If successful, we return the number of bytes written, otherwise we
1413 * return a negative error number.
1414 *****************************************************************************/
1415
Alan Cox95da3102008-07-22 11:09:07 +01001416static int mos7840_write(struct tty_struct *tty, struct usb_serial_port *port,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001417 const unsigned char *data, int count)
1418{
1419 int status;
1420 int i;
1421 int bytes_sent = 0;
1422 int transfer_size;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001423 unsigned long flags;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001424
1425 struct moschip_port *mos7840_port;
1426 struct usb_serial *serial;
1427 struct urb *urb;
Alan Cox880af9d2008-07-22 11:16:12 +01001428 /* __u16 Data; */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001429 const unsigned char *current_position = data;
1430 unsigned char *data1;
Tony Cook84fe6e72009-04-18 22:55:06 +09301431 dbg("%s", "entering ...........");
1432 /* dbg("mos7840_write: mos7840_port->shadowLCR is %x",
Alan Cox880af9d2008-07-22 11:16:12 +01001433 mos7840_port->shadowLCR); */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001434
1435#ifdef NOTMOS7840
1436 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001437 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
1438 mos7840_port->shadowLCR = Data;
Tony Cook84fe6e72009-04-18 22:55:06 +09301439 dbg("mos7840_write: LINE_CONTROL_REGISTER is %x", Data);
1440 dbg("mos7840_write: mos7840_port->shadowLCR is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05001441 mos7840_port->shadowLCR);
1442
Alan Cox880af9d2008-07-22 11:16:12 +01001443 /* Data = 0x03; */
1444 /* status = mos7840_set_uart_reg(port,LINE_CONTROL_REGISTER,Data); */
1445 /* mos7840_port->shadowLCR=Data;//Need to add later */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001446
Alan Cox880af9d2008-07-22 11:16:12 +01001447 Data |= SERIAL_LCR_DLAB; /* data latch enable in LCR 0x80 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001448 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1449
Alan Cox880af9d2008-07-22 11:16:12 +01001450 /* Data = 0x0c; */
1451 /* status = mos7840_set_uart_reg(port,DIVISOR_LATCH_LSB,Data); */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001452 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001453 status = mos7840_get_uart_reg(port, DIVISOR_LATCH_LSB, &Data);
Tony Cook84fe6e72009-04-18 22:55:06 +09301454 dbg("mos7840_write:DLL value is %x", Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001455
1456 Data = 0x0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001457 status = mos7840_get_uart_reg(port, DIVISOR_LATCH_MSB, &Data);
Tony Cook84fe6e72009-04-18 22:55:06 +09301458 dbg("mos7840_write:DLM value is %x", Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001459
1460 Data = Data & ~SERIAL_LCR_DLAB;
Tony Cook84fe6e72009-04-18 22:55:06 +09301461 dbg("mos7840_write: mos7840_port->shadowLCR is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05001462 mos7840_port->shadowLCR);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001463 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1464#endif
1465
Harvey Harrison441b62c2008-03-03 16:08:34 -08001466 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301467 dbg("%s", "Port Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001468 return -1;
1469 }
1470
1471 serial = port->serial;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001472 if (mos7840_serial_paranoia_check(serial, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301473 dbg("%s", "Serial Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001474 return -1;
1475 }
1476
1477 mos7840_port = mos7840_get_port_private(port);
1478 if (mos7840_port == NULL) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301479 dbg("%s", "mos7840_port is NULL");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001480 return -1;
1481 }
1482
1483 /* try to find a free urb in the list */
1484 urb = NULL;
1485
Oliver Neukum0de9a702007-03-16 20:28:28 +01001486 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001487 for (i = 0; i < NUM_URBS; ++i) {
Oliver Neukum0de9a702007-03-16 20:28:28 +01001488 if (!mos7840_port->busy[i]) {
1489 mos7840_port->busy[i] = 1;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001490 urb = mos7840_port->write_urb_pool[i];
Tony Cook84fe6e72009-04-18 22:55:06 +09301491 dbg("URB:%d", i);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001492 break;
1493 }
1494 }
Oliver Neukum0de9a702007-03-16 20:28:28 +01001495 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001496
1497 if (urb == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001498 dbg("%s - no more free urbs", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001499 goto exit;
1500 }
1501
1502 if (urb->transfer_buffer == NULL) {
1503 urb->transfer_buffer =
1504 kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
1505
1506 if (urb->transfer_buffer == NULL) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001507 dev_err(&port->dev, "%s no more kernel memory...\n",
1508 __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001509 goto exit;
1510 }
1511 }
1512 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
1513
Al Viro97c49652006-10-09 20:29:03 +01001514 memcpy(urb->transfer_buffer, current_position, transfer_size);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001515
1516 /* fill urb with data and submit */
1517 usb_fill_bulk_urb(urb,
1518 serial->dev,
1519 usb_sndbulkpipe(serial->dev,
1520 port->bulk_out_endpointAddress),
1521 urb->transfer_buffer,
1522 transfer_size,
1523 mos7840_bulk_out_data_callback, mos7840_port);
1524
1525 data1 = urb->transfer_buffer;
Tony Cook84fe6e72009-04-18 22:55:06 +09301526 dbg("bulkout endpoint is %d", port->bulk_out_endpointAddress);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001527
1528 /* send it down the pipe */
1529 status = usb_submit_urb(urb, GFP_ATOMIC);
1530
1531 if (status) {
Oliver Neukum0de9a702007-03-16 20:28:28 +01001532 mos7840_port->busy[i] = 0;
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001533 dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed "
1534 "with status = %d\n", __func__, status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001535 bytes_sent = status;
1536 goto exit;
1537 }
1538 bytes_sent = transfer_size;
1539 mos7840_port->icount.tx += transfer_size;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001540 smp_wmb();
Tony Cook84fe6e72009-04-18 22:55:06 +09301541 dbg("mos7840_port->icount.tx is %d:", mos7840_port->icount.tx);
Alan Cox95da3102008-07-22 11:09:07 +01001542exit:
Paul B Schroeder3f542972006-08-31 19:41:47 -05001543 return bytes_sent;
1544
1545}
1546
1547/*****************************************************************************
1548 * mos7840_throttle
1549 * this function is called by the tty driver when it wants to stop the data
1550 * being read from the port.
1551 *****************************************************************************/
1552
Alan Cox95da3102008-07-22 11:09:07 +01001553static void mos7840_throttle(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001554{
Alan Cox95da3102008-07-22 11:09:07 +01001555 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001556 struct moschip_port *mos7840_port;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001557 int status;
1558
Harvey Harrison441b62c2008-03-03 16:08:34 -08001559 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301560 dbg("%s", "Invalid port");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001561 return;
1562 }
1563
Tony Cook84fe6e72009-04-18 22:55:06 +09301564 dbg("- port %d", port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001565
1566 mos7840_port = mos7840_get_port_private(port);
1567
1568 if (mos7840_port == NULL)
1569 return;
1570
1571 if (!mos7840_port->open) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301572 dbg("%s", "port not opened");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001573 return;
1574 }
1575
Tony Cook84fe6e72009-04-18 22:55:06 +09301576 dbg("%s", "Entering ..........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001577
Paul B Schroeder3f542972006-08-31 19:41:47 -05001578 /* if we are implementing XON/XOFF, send the stop character */
1579 if (I_IXOFF(tty)) {
1580 unsigned char stop_char = STOP_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01001581 status = mos7840_write(tty, port, &stop_char, 1);
1582 if (status <= 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001583 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001584 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001585 /* if we are implementing RTS/CTS, toggle that line */
1586 if (tty->termios->c_cflag & CRTSCTS) {
1587 mos7840_port->shadowMCR &= ~MCR_RTS;
Alan Cox95da3102008-07-22 11:09:07 +01001588 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001589 mos7840_port->shadowMCR);
Alan Cox95da3102008-07-22 11:09:07 +01001590 if (status < 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001591 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001592 }
1593
1594 return;
1595}
1596
1597/*****************************************************************************
1598 * mos7840_unthrottle
Alan Cox880af9d2008-07-22 11:16:12 +01001599 * this function is called by the tty driver when it wants to resume
1600 * the data being read from the port (called after mos7840_throttle is
1601 * called)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001602 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001603static void mos7840_unthrottle(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001604{
Alan Cox95da3102008-07-22 11:09:07 +01001605 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001606 int status;
1607 struct moschip_port *mos7840_port = mos7840_get_port_private(port);
1608
Harvey Harrison441b62c2008-03-03 16:08:34 -08001609 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301610 dbg("%s", "Invalid port");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001611 return;
1612 }
1613
1614 if (mos7840_port == NULL)
1615 return;
1616
1617 if (!mos7840_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001618 dbg("%s - port not opened", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001619 return;
1620 }
1621
Tony Cook84fe6e72009-04-18 22:55:06 +09301622 dbg("%s", "Entering ..........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001623
Paul B Schroeder3f542972006-08-31 19:41:47 -05001624 /* if we are implementing XON/XOFF, send the start character */
1625 if (I_IXOFF(tty)) {
1626 unsigned char start_char = START_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01001627 status = mos7840_write(tty, port, &start_char, 1);
1628 if (status <= 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001629 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001630 }
1631
1632 /* if we are implementing RTS/CTS, toggle that line */
1633 if (tty->termios->c_cflag & CRTSCTS) {
1634 mos7840_port->shadowMCR |= MCR_RTS;
Alan Cox95da3102008-07-22 11:09:07 +01001635 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001636 mos7840_port->shadowMCR);
Alan Cox95da3102008-07-22 11:09:07 +01001637 if (status < 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001638 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001639 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001640}
1641
Alan Cox95da3102008-07-22 11:09:07 +01001642static int mos7840_tiocmget(struct tty_struct *tty, struct file *file)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001643{
Alan Cox95da3102008-07-22 11:09:07 +01001644 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001645 struct moschip_port *mos7840_port;
1646 unsigned int result;
1647 __u16 msr;
1648 __u16 mcr;
Alan Cox880af9d2008-07-22 11:16:12 +01001649 int status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001650 mos7840_port = mos7840_get_port_private(port);
1651
Harvey Harrison441b62c2008-03-03 16:08:34 -08001652 dbg("%s - port %d", __func__, port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001653
1654 if (mos7840_port == NULL)
1655 return -ENODEV;
1656
1657 status = mos7840_get_uart_reg(port, MODEM_STATUS_REGISTER, &msr);
1658 status = mos7840_get_uart_reg(port, MODEM_CONTROL_REGISTER, &mcr);
1659 result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0)
1660 | ((mcr & MCR_RTS) ? TIOCM_RTS : 0)
1661 | ((mcr & MCR_LOOPBACK) ? TIOCM_LOOP : 0)
1662 | ((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0)
1663 | ((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0)
1664 | ((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0)
1665 | ((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0);
1666
Harvey Harrison441b62c2008-03-03 16:08:34 -08001667 dbg("%s - 0x%04X", __func__, result);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001668
1669 return result;
1670}
1671
Alan Cox95da3102008-07-22 11:09:07 +01001672static int mos7840_tiocmset(struct tty_struct *tty, struct file *file,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001673 unsigned int set, unsigned int clear)
1674{
Alan Cox95da3102008-07-22 11:09:07 +01001675 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001676 struct moschip_port *mos7840_port;
1677 unsigned int mcr;
Roel Kluin87521c42008-04-17 06:16:24 +02001678 int status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001679
Harvey Harrison441b62c2008-03-03 16:08:34 -08001680 dbg("%s - port %d", __func__, port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001681
1682 mos7840_port = mos7840_get_port_private(port);
1683
1684 if (mos7840_port == NULL)
1685 return -ENODEV;
1686
Alan Coxe2984492008-02-20 20:51:45 +00001687 /* FIXME: What locks the port registers ? */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001688 mcr = mos7840_port->shadowMCR;
1689 if (clear & TIOCM_RTS)
1690 mcr &= ~MCR_RTS;
1691 if (clear & TIOCM_DTR)
1692 mcr &= ~MCR_DTR;
1693 if (clear & TIOCM_LOOP)
1694 mcr &= ~MCR_LOOPBACK;
1695
1696 if (set & TIOCM_RTS)
1697 mcr |= MCR_RTS;
1698 if (set & TIOCM_DTR)
1699 mcr |= MCR_DTR;
1700 if (set & TIOCM_LOOP)
1701 mcr |= MCR_LOOPBACK;
1702
1703 mos7840_port->shadowMCR = mcr;
1704
Paul B Schroeder3f542972006-08-31 19:41:47 -05001705 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, mcr);
1706 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301707 dbg("setting MODEM_CONTROL_REGISTER Failed");
Roel Kluin87521c42008-04-17 06:16:24 +02001708 return status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001709 }
1710
1711 return 0;
1712}
1713
1714/*****************************************************************************
1715 * mos7840_calc_baud_rate_divisor
1716 * this function calculates the proper baud rate divisor for the specified
1717 * baud rate.
1718 *****************************************************************************/
1719static int mos7840_calc_baud_rate_divisor(int baudRate, int *divisor,
Alan Cox880af9d2008-07-22 11:16:12 +01001720 __u16 *clk_sel_val)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001721{
1722
Harvey Harrison441b62c2008-03-03 16:08:34 -08001723 dbg("%s - %d", __func__, baudRate);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001724
1725 if (baudRate <= 115200) {
1726 *divisor = 115200 / baudRate;
1727 *clk_sel_val = 0x0;
1728 }
1729 if ((baudRate > 115200) && (baudRate <= 230400)) {
1730 *divisor = 230400 / baudRate;
1731 *clk_sel_val = 0x10;
1732 } else if ((baudRate > 230400) && (baudRate <= 403200)) {
1733 *divisor = 403200 / baudRate;
1734 *clk_sel_val = 0x20;
1735 } else if ((baudRate > 403200) && (baudRate <= 460800)) {
1736 *divisor = 460800 / baudRate;
1737 *clk_sel_val = 0x30;
1738 } else if ((baudRate > 460800) && (baudRate <= 806400)) {
1739 *divisor = 806400 / baudRate;
1740 *clk_sel_val = 0x40;
1741 } else if ((baudRate > 806400) && (baudRate <= 921600)) {
1742 *divisor = 921600 / baudRate;
1743 *clk_sel_val = 0x50;
1744 } else if ((baudRate > 921600) && (baudRate <= 1572864)) {
1745 *divisor = 1572864 / baudRate;
1746 *clk_sel_val = 0x60;
1747 } else if ((baudRate > 1572864) && (baudRate <= 3145728)) {
1748 *divisor = 3145728 / baudRate;
1749 *clk_sel_val = 0x70;
1750 }
1751 return 0;
1752
1753#ifdef NOTMCS7840
1754
1755 for (i = 0; i < ARRAY_SIZE(mos7840_divisor_table); i++) {
1756 if (mos7840_divisor_table[i].BaudRate == baudrate) {
1757 *divisor = mos7840_divisor_table[i].Divisor;
1758 return 0;
1759 }
1760 }
1761
1762 /* After trying for all the standard baud rates *
1763 * Try calculating the divisor for this baud rate */
1764
1765 if (baudrate > 75 && baudrate < 230400) {
1766 /* get the divisor */
1767 custom = (__u16) (230400L / baudrate);
1768
1769 /* Check for round off */
1770 round1 = (__u16) (2304000L / baudrate);
1771 round = (__u16) (round1 - (custom * 10));
Alan Cox880af9d2008-07-22 11:16:12 +01001772 if (round > 4)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001773 custom++;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001774 *divisor = custom;
1775
Tony Cook84fe6e72009-04-18 22:55:06 +09301776 dbg(" Baud %d = %d", baudrate, custom);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001777 return 0;
1778 }
1779
Tony Cook84fe6e72009-04-18 22:55:06 +09301780 dbg("%s", " Baud calculation Failed...");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001781 return -1;
1782#endif
1783}
1784
1785/*****************************************************************************
1786 * mos7840_send_cmd_write_baud_rate
1787 * this function sends the proper command to change the baud rate of the
1788 * specified port.
1789 *****************************************************************************/
1790
1791static int mos7840_send_cmd_write_baud_rate(struct moschip_port *mos7840_port,
1792 int baudRate)
1793{
1794 int divisor = 0;
1795 int status;
1796 __u16 Data;
1797 unsigned char number;
1798 __u16 clk_sel_val;
1799 struct usb_serial_port *port;
1800
1801 if (mos7840_port == NULL)
1802 return -1;
1803
1804 port = (struct usb_serial_port *)mos7840_port->port;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001805 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301806 dbg("%s", "Invalid port");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001807 return -1;
1808 }
1809
Harvey Harrison441b62c2008-03-03 16:08:34 -08001810 if (mos7840_serial_paranoia_check(port->serial, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301811 dbg("%s", "Invalid Serial");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001812 return -1;
1813 }
1814
Tony Cook84fe6e72009-04-18 22:55:06 +09301815 dbg("%s", "Entering ..........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001816
1817 number = mos7840_port->port->number - mos7840_port->port->serial->minor;
1818
Harvey Harrison441b62c2008-03-03 16:08:34 -08001819 dbg("%s - port = %d, baud = %d", __func__,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001820 mos7840_port->port->number, baudRate);
Alan Cox880af9d2008-07-22 11:16:12 +01001821 /* reset clk_uart_sel in spregOffset */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001822 if (baudRate > 115200) {
1823#ifdef HW_flow_control
Alan Cox880af9d2008-07-22 11:16:12 +01001824 /* NOTE: need to see the pther register to modify */
1825 /* setting h/w flow control bit to 1 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001826 Data = 0x2b;
1827 mos7840_port->shadowMCR = Data;
Alan Cox880af9d2008-07-22 11:16:12 +01001828 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1829 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001830 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301831 dbg("Writing spreg failed in set_serial_baud");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001832 return -1;
1833 }
1834#endif
1835
1836 } else {
1837#ifdef HW_flow_control
Alan Cox880af9d2008-07-22 11:16:12 +01001838 / *setting h/w flow control bit to 0 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001839 Data = 0xb;
1840 mos7840_port->shadowMCR = Data;
Alan Cox880af9d2008-07-22 11:16:12 +01001841 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1842 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001843 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301844 dbg("Writing spreg failed in set_serial_baud");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001845 return -1;
1846 }
1847#endif
1848
1849 }
1850
Alan Cox880af9d2008-07-22 11:16:12 +01001851 if (1) { /* baudRate <= 115200) */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001852 clk_sel_val = 0x0;
1853 Data = 0x0;
Alan Cox880af9d2008-07-22 11:16:12 +01001854 status = mos7840_calc_baud_rate_divisor(baudRate, &divisor,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001855 &clk_sel_val);
Alan Cox880af9d2008-07-22 11:16:12 +01001856 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset,
1857 &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001858 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301859 dbg("reading spreg failed in set_serial_baud");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001860 return -1;
1861 }
1862 Data = (Data & 0x8f) | clk_sel_val;
Alan Cox880af9d2008-07-22 11:16:12 +01001863 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset,
1864 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001865 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301866 dbg("Writing spreg failed in set_serial_baud");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001867 return -1;
1868 }
1869 /* Calculate the Divisor */
1870
1871 if (status) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001872 dev_err(&port->dev, "%s - bad baud rate\n", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001873 return status;
1874 }
1875 /* Enable access to divisor latch */
1876 Data = mos7840_port->shadowLCR | SERIAL_LCR_DLAB;
1877 mos7840_port->shadowLCR = Data;
1878 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1879
1880 /* Write the divisor */
1881 Data = (unsigned char)(divisor & 0xff);
Tony Cook84fe6e72009-04-18 22:55:06 +09301882 dbg("set_serial_baud Value to write DLL is %x", Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001883 mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
1884
1885 Data = (unsigned char)((divisor & 0xff00) >> 8);
Tony Cook84fe6e72009-04-18 22:55:06 +09301886 dbg("set_serial_baud Value to write DLM is %x", Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001887 mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
1888
1889 /* Disable access to divisor latch */
1890 Data = mos7840_port->shadowLCR & ~SERIAL_LCR_DLAB;
1891 mos7840_port->shadowLCR = Data;
1892 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1893
1894 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001895 return status;
1896}
1897
1898/*****************************************************************************
1899 * mos7840_change_port_settings
1900 * This routine is called to set the UART on the device to match
1901 * the specified new settings.
1902 *****************************************************************************/
1903
Alan Cox95da3102008-07-22 11:09:07 +01001904static void mos7840_change_port_settings(struct tty_struct *tty,
1905 struct moschip_port *mos7840_port, struct ktermios *old_termios)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001906{
Paul B Schroeder3f542972006-08-31 19:41:47 -05001907 int baud;
1908 unsigned cflag;
1909 unsigned iflag;
1910 __u8 lData;
1911 __u8 lParity;
1912 __u8 lStop;
1913 int status;
1914 __u16 Data;
1915 struct usb_serial_port *port;
1916 struct usb_serial *serial;
1917
1918 if (mos7840_port == NULL)
1919 return;
1920
1921 port = (struct usb_serial_port *)mos7840_port->port;
1922
Harvey Harrison441b62c2008-03-03 16:08:34 -08001923 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301924 dbg("%s", "Invalid port");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001925 return;
1926 }
1927
Harvey Harrison441b62c2008-03-03 16:08:34 -08001928 if (mos7840_serial_paranoia_check(port->serial, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301929 dbg("%s", "Invalid Serial");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001930 return;
1931 }
1932
1933 serial = port->serial;
1934
Harvey Harrison441b62c2008-03-03 16:08:34 -08001935 dbg("%s - port %d", __func__, mos7840_port->port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001936
1937 if (!mos7840_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001938 dbg("%s - port not opened", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001939 return;
1940 }
1941
Tony Cook84fe6e72009-04-18 22:55:06 +09301942 dbg("%s", "Entering ..........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001943
1944 lData = LCR_BITS_8;
1945 lStop = LCR_STOP_1;
1946 lParity = LCR_PAR_NONE;
1947
1948 cflag = tty->termios->c_cflag;
1949 iflag = tty->termios->c_iflag;
1950
1951 /* Change the number of bits */
1952 if (cflag & CSIZE) {
1953 switch (cflag & CSIZE) {
1954 case CS5:
1955 lData = LCR_BITS_5;
1956 break;
1957
1958 case CS6:
1959 lData = LCR_BITS_6;
1960 break;
1961
1962 case CS7:
1963 lData = LCR_BITS_7;
1964 break;
1965 default:
1966 case CS8:
1967 lData = LCR_BITS_8;
1968 break;
1969 }
1970 }
1971 /* Change the Parity bit */
1972 if (cflag & PARENB) {
1973 if (cflag & PARODD) {
1974 lParity = LCR_PAR_ODD;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001975 dbg("%s - parity = odd", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001976 } else {
1977 lParity = LCR_PAR_EVEN;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001978 dbg("%s - parity = even", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001979 }
1980
1981 } else {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001982 dbg("%s - parity = none", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001983 }
1984
Alan Cox880af9d2008-07-22 11:16:12 +01001985 if (cflag & CMSPAR)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001986 lParity = lParity | 0x20;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001987
1988 /* Change the Stop bit */
1989 if (cflag & CSTOPB) {
1990 lStop = LCR_STOP_2;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001991 dbg("%s - stop bits = 2", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001992 } else {
1993 lStop = LCR_STOP_1;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001994 dbg("%s - stop bits = 1", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001995 }
1996
1997 /* Update the LCR with the correct value */
1998 mos7840_port->shadowLCR &=
1999 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
2000 mos7840_port->shadowLCR |= (lData | lParity | lStop);
2001
Tony Cook84fe6e72009-04-18 22:55:06 +09302002 dbg("mos7840_change_port_settings mos7840_port->shadowLCR is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002003 mos7840_port->shadowLCR);
2004 /* Disable Interrupts */
2005 Data = 0x00;
2006 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
2007
2008 Data = 0x00;
2009 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
2010
2011 Data = 0xcf;
2012 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
2013
2014 /* Send the updated LCR value to the mos7840 */
2015 Data = mos7840_port->shadowLCR;
2016
2017 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
2018
2019 Data = 0x00b;
2020 mos7840_port->shadowMCR = Data;
2021 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2022 Data = 0x00b;
2023 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2024
2025 /* set up the MCR register and send it to the mos7840 */
2026
2027 mos7840_port->shadowMCR = MCR_MASTER_IE;
Alan Cox880af9d2008-07-22 11:16:12 +01002028 if (cflag & CBAUD)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002029 mos7840_port->shadowMCR |= (MCR_DTR | MCR_RTS);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002030
Alan Cox880af9d2008-07-22 11:16:12 +01002031 if (cflag & CRTSCTS)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002032 mos7840_port->shadowMCR |= (MCR_XON_ANY);
Alan Cox880af9d2008-07-22 11:16:12 +01002033 else
Paul B Schroeder3f542972006-08-31 19:41:47 -05002034 mos7840_port->shadowMCR &= ~(MCR_XON_ANY);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002035
2036 Data = mos7840_port->shadowMCR;
2037 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2038
2039 /* Determine divisor based on baud rate */
2040 baud = tty_get_baud_rate(tty);
2041
2042 if (!baud) {
2043 /* pick a default, any default... */
Tony Cook84fe6e72009-04-18 22:55:06 +09302044 dbg("%s", "Picked default baud...");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002045 baud = 9600;
2046 }
2047
Harvey Harrison441b62c2008-03-03 16:08:34 -08002048 dbg("%s - baud rate = %d", __func__, baud);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002049 status = mos7840_send_cmd_write_baud_rate(mos7840_port, baud);
2050
2051 /* Enable Interrupts */
2052 Data = 0x0c;
2053 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
2054
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002055 if (mos7840_port->read_urb_busy == false) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002056 mos7840_port->read_urb->dev = serial->dev;
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002057 mos7840_port->read_urb_busy = true;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002058 status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002059 if (status) {
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002060 dbg("usb_submit_urb(read bulk) failed, status = %d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002061 status);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002062 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002063 }
2064 }
2065 wake_up(&mos7840_port->delta_msr_wait);
2066 mos7840_port->delta_msr_cond = 1;
Tony Cook84fe6e72009-04-18 22:55:06 +09302067 dbg("mos7840_change_port_settings mos7840_port->shadowLCR is End %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002068 mos7840_port->shadowLCR);
2069
2070 return;
2071}
2072
2073/*****************************************************************************
2074 * mos7840_set_termios
2075 * this function is called by the tty driver when it wants to change
2076 * the termios structure
2077 *****************************************************************************/
2078
Alan Cox95da3102008-07-22 11:09:07 +01002079static void mos7840_set_termios(struct tty_struct *tty,
2080 struct usb_serial_port *port,
Alan Cox606d0992006-12-08 02:38:45 -08002081 struct ktermios *old_termios)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002082{
2083 int status;
2084 unsigned int cflag;
2085 struct usb_serial *serial;
2086 struct moschip_port *mos7840_port;
Tony Cook84fe6e72009-04-18 22:55:06 +09302087 dbg("mos7840_set_termios: START");
Harvey Harrison441b62c2008-03-03 16:08:34 -08002088 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302089 dbg("%s", "Invalid port");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002090 return;
2091 }
2092
2093 serial = port->serial;
2094
Harvey Harrison441b62c2008-03-03 16:08:34 -08002095 if (mos7840_serial_paranoia_check(serial, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302096 dbg("%s", "Invalid Serial");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002097 return;
2098 }
2099
2100 mos7840_port = mos7840_get_port_private(port);
2101
2102 if (mos7840_port == NULL)
2103 return;
2104
Paul B Schroeder3f542972006-08-31 19:41:47 -05002105 if (!mos7840_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002106 dbg("%s - port not opened", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002107 return;
2108 }
2109
Tony Cook84fe6e72009-04-18 22:55:06 +09302110 dbg("%s", "setting termios - ");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002111
2112 cflag = tty->termios->c_cflag;
2113
Harvey Harrison441b62c2008-03-03 16:08:34 -08002114 dbg("%s - clfag %08x iflag %08x", __func__,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002115 tty->termios->c_cflag, RELEVANT_IFLAG(tty->termios->c_iflag));
Harvey Harrison441b62c2008-03-03 16:08:34 -08002116 dbg("%s - old clfag %08x old iflag %08x", __func__,
Alan Cox3d3ddce2007-10-15 20:53:35 +01002117 old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag));
Harvey Harrison441b62c2008-03-03 16:08:34 -08002118 dbg("%s - port %d", __func__, port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002119
2120 /* change the port settings to the new ones specified */
2121
Alan Cox95da3102008-07-22 11:09:07 +01002122 mos7840_change_port_settings(tty, mos7840_port, old_termios);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002123
2124 if (!mos7840_port->read_urb) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302125 dbg("%s", "URB KILLED !!!!!");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002126 return;
2127 }
2128
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002129 if (mos7840_port->read_urb_busy == false) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002130 mos7840_port->read_urb->dev = serial->dev;
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002131 mos7840_port->read_urb_busy = true;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002132 status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
2133 if (status) {
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002134 dbg("usb_submit_urb(read bulk) failed, status = %d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002135 status);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002136 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002137 }
2138 }
2139 return;
2140}
2141
2142/*****************************************************************************
2143 * mos7840_get_lsr_info - get line status register info
2144 *
2145 * Purpose: Let user call ioctl() to get info when the UART physically
2146 * is emptied. On bus types like RS485, the transmitter must
2147 * release the bus after transmitting. This must be done when
2148 * the transmit shift register is empty, not be done when the
2149 * transmit holding register is empty. This functionality
2150 * allows an RS485 driver to be written in user space.
2151 *****************************************************************************/
2152
Alan Cox95da3102008-07-22 11:09:07 +01002153static int mos7840_get_lsr_info(struct tty_struct *tty,
Al Viro97c49652006-10-09 20:29:03 +01002154 unsigned int __user *value)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002155{
2156 int count;
2157 unsigned int result = 0;
2158
Alan Cox95da3102008-07-22 11:09:07 +01002159 count = mos7840_chars_in_buffer(tty);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002160 if (count == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002161 dbg("%s -- Empty", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002162 result = TIOCSER_TEMT;
2163 }
2164
2165 if (copy_to_user(value, &result, sizeof(int)))
2166 return -EFAULT;
2167 return 0;
2168}
2169
2170/*****************************************************************************
Paul B Schroeder3f542972006-08-31 19:41:47 -05002171 * mos7840_get_serial_info
2172 * function to get information about serial port
2173 *****************************************************************************/
2174
2175static int mos7840_get_serial_info(struct moschip_port *mos7840_port,
Al Viro97c49652006-10-09 20:29:03 +01002176 struct serial_struct __user *retinfo)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002177{
2178 struct serial_struct tmp;
2179
2180 if (mos7840_port == NULL)
2181 return -1;
2182
2183 if (!retinfo)
2184 return -EFAULT;
2185
2186 memset(&tmp, 0, sizeof(tmp));
2187
2188 tmp.type = PORT_16550A;
2189 tmp.line = mos7840_port->port->serial->minor;
2190 tmp.port = mos7840_port->port->number;
2191 tmp.irq = 0;
2192 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
2193 tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
2194 tmp.baud_base = 9600;
2195 tmp.close_delay = 5 * HZ;
2196 tmp.closing_wait = 30 * HZ;
2197
2198 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2199 return -EFAULT;
2200 return 0;
2201}
2202
2203/*****************************************************************************
2204 * SerialIoctl
2205 * this function handles any ioctl calls to the driver
2206 *****************************************************************************/
2207
Alan Cox95da3102008-07-22 11:09:07 +01002208static int mos7840_ioctl(struct tty_struct *tty, struct file *file,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002209 unsigned int cmd, unsigned long arg)
2210{
Alan Cox95da3102008-07-22 11:09:07 +01002211 struct usb_serial_port *port = tty->driver_data;
Al Viro97c49652006-10-09 20:29:03 +01002212 void __user *argp = (void __user *)arg;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002213 struct moschip_port *mos7840_port;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002214
2215 struct async_icount cnow;
2216 struct async_icount cprev;
2217 struct serial_icounter_struct icount;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002218
Harvey Harrison441b62c2008-03-03 16:08:34 -08002219 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302220 dbg("%s", "Invalid port");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002221 return -1;
2222 }
2223
2224 mos7840_port = mos7840_get_port_private(port);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002225
2226 if (mos7840_port == NULL)
2227 return -1;
2228
Harvey Harrison441b62c2008-03-03 16:08:34 -08002229 dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002230
2231 switch (cmd) {
2232 /* return number of bytes available */
2233
Paul B Schroeder3f542972006-08-31 19:41:47 -05002234 case TIOCSERGETLSR:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002235 dbg("%s (%d) TIOCSERGETLSR", __func__, port->number);
Alan Cox95da3102008-07-22 11:09:07 +01002236 return mos7840_get_lsr_info(tty, argp);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002237 return 0;
2238
Paul B Schroeder3f542972006-08-31 19:41:47 -05002239 case TIOCGSERIAL:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002240 dbg("%s (%d) TIOCGSERIAL", __func__, port->number);
Al Viro97c49652006-10-09 20:29:03 +01002241 return mos7840_get_serial_info(mos7840_port, argp);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002242
2243 case TIOCSSERIAL:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002244 dbg("%s (%d) TIOCSSERIAL", __func__, port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002245 break;
2246
2247 case TIOCMIWAIT:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002248 dbg("%s (%d) TIOCMIWAIT", __func__, port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002249 cprev = mos7840_port->icount;
2250 while (1) {
Alan Cox880af9d2008-07-22 11:16:12 +01002251 /* interruptible_sleep_on(&mos7840_port->delta_msr_wait); */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002252 mos7840_port->delta_msr_cond = 0;
2253 wait_event_interruptible(mos7840_port->delta_msr_wait,
2254 (mos7840_port->
2255 delta_msr_cond == 1));
2256
2257 /* see if a signal did it */
2258 if (signal_pending(current))
2259 return -ERESTARTSYS;
2260 cnow = mos7840_port->icount;
Oliver Neukum0de9a702007-03-16 20:28:28 +01002261 smp_rmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -05002262 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2263 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
2264 return -EIO; /* no change => error */
2265 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
2266 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
2267 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
2268 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
2269 return 0;
2270 }
2271 cprev = cnow;
2272 }
2273 /* NOTREACHED */
2274 break;
2275
2276 case TIOCGICOUNT:
2277 cnow = mos7840_port->icount;
Oliver Neukum0de9a702007-03-16 20:28:28 +01002278 smp_rmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -05002279 icount.cts = cnow.cts;
2280 icount.dsr = cnow.dsr;
2281 icount.rng = cnow.rng;
2282 icount.dcd = cnow.dcd;
2283 icount.rx = cnow.rx;
2284 icount.tx = cnow.tx;
2285 icount.frame = cnow.frame;
2286 icount.overrun = cnow.overrun;
2287 icount.parity = cnow.parity;
2288 icount.brk = cnow.brk;
2289 icount.buf_overrun = cnow.buf_overrun;
2290
Harvey Harrison441b62c2008-03-03 16:08:34 -08002291 dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __func__,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002292 port->number, icount.rx, icount.tx);
Al Viro97c49652006-10-09 20:29:03 +01002293 if (copy_to_user(argp, &icount, sizeof(icount)))
Paul B Schroeder3f542972006-08-31 19:41:47 -05002294 return -EFAULT;
2295 return 0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002296 default:
2297 break;
2298 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002299 return -ENOIOCTLCMD;
2300}
2301
2302static int mos7840_calc_num_ports(struct usb_serial *serial)
2303{
Oliver Neukum0de9a702007-03-16 20:28:28 +01002304 int mos7840_num_ports = 0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002305
Tony Cook84fe6e72009-04-18 22:55:06 +09302306 dbg("numberofendpoints: cur %d, alt %d",
2307 (int)serial->interface->cur_altsetting->desc.bNumEndpoints,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002308 (int)serial->interface->altsetting->desc.bNumEndpoints);
2309 if (serial->interface->cur_altsetting->desc.bNumEndpoints == 5) {
Oliver Neukum0de9a702007-03-16 20:28:28 +01002310 mos7840_num_ports = serial->num_ports = 2;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002311 } else if (serial->interface->cur_altsetting->desc.bNumEndpoints == 9) {
Oliver Neukum0de9a702007-03-16 20:28:28 +01002312 serial->num_bulk_in = 4;
2313 serial->num_bulk_out = 4;
2314 mos7840_num_ports = serial->num_ports = 4;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002315 }
Tony Cook84fe6e72009-04-18 22:55:06 +09302316 dbg ("mos7840_num_ports = %d", mos7840_num_ports);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002317 return mos7840_num_ports;
2318}
2319
2320/****************************************************************************
2321 * mos7840_startup
2322 ****************************************************************************/
2323
2324static int mos7840_startup(struct usb_serial *serial)
2325{
2326 struct moschip_port *mos7840_port;
2327 struct usb_device *dev;
2328 int i, status;
2329
2330 __u16 Data;
Tony Cook84fe6e72009-04-18 22:55:06 +09302331 dbg("%s", "mos7840_startup :Entering..........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002332
2333 if (!serial) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302334 dbg("%s", "Invalid Handler");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002335 return -1;
2336 }
2337
2338 dev = serial->dev;
2339
Tony Cook84fe6e72009-04-18 22:55:06 +09302340 dbg("%s", "Entering...");
2341 dbg ("mos7840_startup: serial = %p", serial);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002342
2343 /* we set up the pointers to the endpoints in the mos7840_open *
2344 * function, as the structures aren't created yet. */
2345
2346 /* set up port private structures */
2347 for (i = 0; i < serial->num_ports; ++i) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302348 dbg ("mos7840_startup: configuring port %d............", i);
Burman Yan7ac9da12006-11-22 20:54:38 +02002349 mos7840_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002350 if (mos7840_port == NULL) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07002351 dev_err(&dev->dev, "%s - Out of memory\n", __func__);
Oliver Neukum0de9a702007-03-16 20:28:28 +01002352 status = -ENOMEM;
2353 i--; /* don't follow NULL pointer cleaning up */
2354 goto error;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002355 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002356
Alan Cox880af9d2008-07-22 11:16:12 +01002357 /* Initialize all port interrupt end point to port 0 int
2358 * endpoint. Our device has only one interrupt end point
2359 * common to all port */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002360
2361 mos7840_port->port = serial->port[i];
2362 mos7840_set_port_private(serial->port[i], mos7840_port);
Oliver Neukum0de9a702007-03-16 20:28:28 +01002363 spin_lock_init(&mos7840_port->pool_lock);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002364
Tony Cook37768ad2009-04-18 22:42:18 +09302365 /* minor is not initialised until later by
2366 * usb-serial.c:get_free_serial() and cannot therefore be used
2367 * to index device instances */
2368 mos7840_port->port_num = i + 1;
2369 dbg ("serial->port[i]->number = %d", serial->port[i]->number);
2370 dbg ("serial->port[i]->serial->minor = %d", serial->port[i]->serial->minor);
2371 dbg ("mos7840_port->port_num = %d", mos7840_port->port_num);
2372 dbg ("serial->minor = %d", serial->minor);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002373
2374 if (mos7840_port->port_num == 1) {
2375 mos7840_port->SpRegOffset = 0x0;
2376 mos7840_port->ControlRegOffset = 0x1;
2377 mos7840_port->DcrRegOffset = 0x4;
2378 } else if ((mos7840_port->port_num == 2)
Oliver Neukum0de9a702007-03-16 20:28:28 +01002379 && (serial->num_ports == 4)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002380 mos7840_port->SpRegOffset = 0x8;
2381 mos7840_port->ControlRegOffset = 0x9;
2382 mos7840_port->DcrRegOffset = 0x16;
2383 } else if ((mos7840_port->port_num == 2)
Oliver Neukum0de9a702007-03-16 20:28:28 +01002384 && (serial->num_ports == 2)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002385 mos7840_port->SpRegOffset = 0xa;
2386 mos7840_port->ControlRegOffset = 0xb;
2387 mos7840_port->DcrRegOffset = 0x19;
2388 } else if ((mos7840_port->port_num == 3)
Oliver Neukum0de9a702007-03-16 20:28:28 +01002389 && (serial->num_ports == 4)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002390 mos7840_port->SpRegOffset = 0xa;
2391 mos7840_port->ControlRegOffset = 0xb;
2392 mos7840_port->DcrRegOffset = 0x19;
2393 } else if ((mos7840_port->port_num == 4)
Oliver Neukum0de9a702007-03-16 20:28:28 +01002394 && (serial->num_ports == 4)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002395 mos7840_port->SpRegOffset = 0xc;
2396 mos7840_port->ControlRegOffset = 0xd;
2397 mos7840_port->DcrRegOffset = 0x1c;
2398 }
2399 mos7840_dump_serial_port(mos7840_port);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002400 mos7840_set_port_private(serial->port[i], mos7840_port);
2401
Alan Cox880af9d2008-07-22 11:16:12 +01002402 /* enable rx_disable bit in control register */
2403 status = mos7840_get_reg_sync(serial->port[i],
2404 mos7840_port->ControlRegOffset, &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002405 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302406 dbg("Reading ControlReg failed status-0x%x", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002407 break;
2408 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302409 dbg("ControlReg Reading success val is %x, status%d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002410 Data, status);
Alan Cox880af9d2008-07-22 11:16:12 +01002411 Data |= 0x08; /* setting driver done bit */
2412 Data |= 0x04; /* sp1_bit to have cts change reflect in
2413 modem status reg */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002414
Alan Cox880af9d2008-07-22 11:16:12 +01002415 /* Data |= 0x20; //rx_disable bit */
2416 status = mos7840_set_reg_sync(serial->port[i],
Paul B Schroeder3f542972006-08-31 19:41:47 -05002417 mos7840_port->ControlRegOffset, Data);
2418 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302419 dbg("Writing ControlReg failed(rx_disable) status-0x%x", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002420 break;
2421 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302422 dbg("ControlReg Writing success(rx_disable) status%d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002423 status);
2424
Alan Cox880af9d2008-07-22 11:16:12 +01002425 /* Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2
2426 and 0x24 in DCR3 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002427 Data = 0x01;
Alan Cox880af9d2008-07-22 11:16:12 +01002428 status = mos7840_set_reg_sync(serial->port[i],
2429 (__u16) (mos7840_port->DcrRegOffset + 0), Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002430 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302431 dbg("Writing DCR0 failed status-0x%x", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002432 break;
2433 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302434 dbg("DCR0 Writing success status%d", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002435
2436 Data = 0x05;
Alan Cox880af9d2008-07-22 11:16:12 +01002437 status = mos7840_set_reg_sync(serial->port[i],
2438 (__u16) (mos7840_port->DcrRegOffset + 1), Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002439 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302440 dbg("Writing DCR1 failed status-0x%x", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002441 break;
2442 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302443 dbg("DCR1 Writing success status%d", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002444
2445 Data = 0x24;
Alan Cox880af9d2008-07-22 11:16:12 +01002446 status = mos7840_set_reg_sync(serial->port[i],
2447 (__u16) (mos7840_port->DcrRegOffset + 2), Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002448 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302449 dbg("Writing DCR2 failed status-0x%x", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002450 break;
2451 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302452 dbg("DCR2 Writing success status%d", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002453
Alan Cox880af9d2008-07-22 11:16:12 +01002454 /* write values in clkstart0x0 and clkmulti 0x20 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002455 Data = 0x0;
Alan Cox880af9d2008-07-22 11:16:12 +01002456 status = mos7840_set_reg_sync(serial->port[i],
Paul B Schroeder3f542972006-08-31 19:41:47 -05002457 CLK_START_VALUE_REGISTER, Data);
2458 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302459 dbg("Writing CLK_START_VALUE_REGISTER failed status-0x%x", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002460 break;
2461 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302462 dbg("CLK_START_VALUE_REGISTER Writing success status%d", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002463
2464 Data = 0x20;
Alan Cox880af9d2008-07-22 11:16:12 +01002465 status = mos7840_set_reg_sync(serial->port[i],
2466 CLK_MULTI_REGISTER, Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002467 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302468 dbg("Writing CLK_MULTI_REGISTER failed status-0x%x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002469 status);
Oliver Neukum0de9a702007-03-16 20:28:28 +01002470 goto error;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002471 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302472 dbg("CLK_MULTI_REGISTER Writing success status%d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002473 status);
2474
Alan Cox880af9d2008-07-22 11:16:12 +01002475 /* write value 0x0 to scratchpad register */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002476 Data = 0x00;
Alan Cox880af9d2008-07-22 11:16:12 +01002477 status = mos7840_set_uart_reg(serial->port[i],
2478 SCRATCH_PAD_REGISTER, Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002479 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302480 dbg("Writing SCRATCH_PAD_REGISTER failed status-0x%x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002481 status);
2482 break;
2483 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302484 dbg("SCRATCH_PAD_REGISTER Writing success status%d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002485 status);
2486
Alan Cox880af9d2008-07-22 11:16:12 +01002487 /* Zero Length flag register */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002488 if ((mos7840_port->port_num != 1)
Oliver Neukum0de9a702007-03-16 20:28:28 +01002489 && (serial->num_ports == 2)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002490
2491 Data = 0xff;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002492 status = mos7840_set_reg_sync(serial->port[i],
Alan Cox880af9d2008-07-22 11:16:12 +01002493 (__u16) (ZLP_REG1 +
2494 ((__u16)mos7840_port->port_num)), Data);
Tony Cook84fe6e72009-04-18 22:55:06 +09302495 dbg("ZLIP offset %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002496 (__u16) (ZLP_REG1 +
Alan Cox880af9d2008-07-22 11:16:12 +01002497 ((__u16) mos7840_port->port_num)));
Paul B Schroeder3f542972006-08-31 19:41:47 -05002498 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302499 dbg("Writing ZLP_REG%d failed status-0x%x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002500 i + 2, status);
2501 break;
2502 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302503 dbg("ZLP_REG%d Writing success status%d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002504 i + 2, status);
2505 } else {
2506 Data = 0xff;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002507 status = mos7840_set_reg_sync(serial->port[i],
Alan Cox880af9d2008-07-22 11:16:12 +01002508 (__u16) (ZLP_REG1 +
2509 ((__u16)mos7840_port->port_num) - 0x1), Data);
Tony Cook84fe6e72009-04-18 22:55:06 +09302510 dbg("ZLIP offset %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002511 (__u16) (ZLP_REG1 +
2512 ((__u16) mos7840_port->port_num) - 0x1));
2513 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302514 dbg("Writing ZLP_REG%d failed status-0x%x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002515 i + 1, status);
2516 break;
2517 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302518 dbg("ZLP_REG%d Writing success status%d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002519 i + 1, status);
2520
2521 }
Oliver Neukum0de9a702007-03-16 20:28:28 +01002522 mos7840_port->control_urb = usb_alloc_urb(0, GFP_KERNEL);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002523 mos7840_port->ctrl_buf = kmalloc(16, GFP_KERNEL);
Alan Cox880af9d2008-07-22 11:16:12 +01002524 mos7840_port->dr = kmalloc(sizeof(struct usb_ctrlrequest),
2525 GFP_KERNEL);
2526 if (!mos7840_port->control_urb || !mos7840_port->ctrl_buf ||
2527 !mos7840_port->dr) {
Oliver Neukum0de9a702007-03-16 20:28:28 +01002528 status = -ENOMEM;
2529 goto error;
2530 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002531 }
Tony Cook84fe6e72009-04-18 22:55:06 +09302532 dbg ("mos7840_startup: all ports configured...........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002533
Alan Cox880af9d2008-07-22 11:16:12 +01002534 /* Zero Length flag enable */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002535 Data = 0x0f;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002536 status = mos7840_set_reg_sync(serial->port[0], ZLP_REG5, Data);
2537 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302538 dbg("Writing ZLP_REG5 failed status-0x%x", status);
Roel Kluin7ced46c2007-10-27 03:36:37 +02002539 goto error;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002540 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302541 dbg("ZLP_REG5 Writing success status%d", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002542
2543 /* setting configuration feature to one */
2544 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
Al Viro97c49652006-10-09 20:29:03 +01002545 (__u8) 0x03, 0x00, 0x01, 0x00, NULL, 0x00, 5 * HZ);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002546 return 0;
Oliver Neukum0de9a702007-03-16 20:28:28 +01002547error:
2548 for (/* nothing */; i >= 0; i--) {
2549 mos7840_port = mos7840_get_port_private(serial->port[i]);
2550
2551 kfree(mos7840_port->dr);
2552 kfree(mos7840_port->ctrl_buf);
2553 usb_free_urb(mos7840_port->control_urb);
2554 kfree(mos7840_port);
2555 serial->port[i] = NULL;
2556 }
2557 return status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002558}
2559
2560/****************************************************************************
Alan Sternf9c99bb2009-06-02 11:53:55 -04002561 * mos7840_disconnect
Paul B Schroeder3f542972006-08-31 19:41:47 -05002562 * This function is called whenever the device is removed from the usb bus.
2563 ****************************************************************************/
2564
Alan Sternf9c99bb2009-06-02 11:53:55 -04002565static void mos7840_disconnect(struct usb_serial *serial)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002566{
2567 int i;
Oliver Neukum0de9a702007-03-16 20:28:28 +01002568 unsigned long flags;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002569 struct moschip_port *mos7840_port;
Alan Sternf9c99bb2009-06-02 11:53:55 -04002570 dbg("%s", " disconnect :entering..........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002571
2572 if (!serial) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302573 dbg("%s", "Invalid Handler");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002574 return;
2575 }
2576
Alan Cox880af9d2008-07-22 11:16:12 +01002577 /* check for the ports to be closed,close the ports and disconnect */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002578
2579 /* free private structure allocated for serial port *
2580 * stop reads and writes on all ports */
2581
2582 for (i = 0; i < serial->num_ports; ++i) {
2583 mos7840_port = mos7840_get_port_private(serial->port[i]);
Tony Cook37768ad2009-04-18 22:42:18 +09302584 dbg ("mos7840_port %d = %p", i, mos7840_port);
2585 if (mos7840_port) {
2586 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
2587 mos7840_port->zombie = 1;
2588 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
2589 usb_kill_urb(mos7840_port->control_urb);
Alan Sternf9c99bb2009-06-02 11:53:55 -04002590 }
2591 }
2592
2593 dbg("%s", "Thank u :: ");
2594
2595}
2596
2597/****************************************************************************
2598 * mos7840_release
2599 * This function is called when the usb_serial structure is freed.
2600 ****************************************************************************/
2601
2602static void mos7840_release(struct usb_serial *serial)
2603{
2604 int i;
2605 struct moschip_port *mos7840_port;
2606 dbg("%s", " release :entering..........");
2607
2608 if (!serial) {
2609 dbg("%s", "Invalid Handler");
2610 return;
2611 }
2612
2613 /* check for the ports to be closed,close the ports and disconnect */
2614
2615 /* free private structure allocated for serial port *
2616 * stop reads and writes on all ports */
2617
2618 for (i = 0; i < serial->num_ports; ++i) {
2619 mos7840_port = mos7840_get_port_private(serial->port[i]);
2620 dbg("mos7840_port %d = %p", i, mos7840_port);
2621 if (mos7840_port) {
Tony Cook37768ad2009-04-18 22:42:18 +09302622 kfree(mos7840_port->ctrl_buf);
2623 kfree(mos7840_port->dr);
2624 kfree(mos7840_port);
2625 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002626 }
2627
Tony Cook84fe6e72009-04-18 22:55:06 +09302628 dbg("%s", "Thank u :: ");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002629
2630}
2631
Johannes Hölzld9b1b782006-12-17 21:50:24 +01002632static struct usb_driver io_driver = {
2633 .name = "mos7840",
2634 .probe = usb_serial_probe,
2635 .disconnect = usb_serial_disconnect,
2636 .id_table = moschip_id_table_combined,
2637 .no_dynamic_id = 1,
2638};
2639
Paul B Schroeder3f542972006-08-31 19:41:47 -05002640static struct usb_serial_driver moschip7840_4port_device = {
2641 .driver = {
2642 .owner = THIS_MODULE,
2643 .name = "mos7840",
2644 },
2645 .description = DRIVER_DESC,
Johannes Hölzld9b1b782006-12-17 21:50:24 +01002646 .usb_driver = &io_driver,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002647 .id_table = moschip_port_id_table,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002648 .num_ports = 4,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002649 .open = mos7840_open,
2650 .close = mos7840_close,
2651 .write = mos7840_write,
2652 .write_room = mos7840_write_room,
2653 .chars_in_buffer = mos7840_chars_in_buffer,
2654 .throttle = mos7840_throttle,
2655 .unthrottle = mos7840_unthrottle,
2656 .calc_num_ports = mos7840_calc_num_ports,
2657#ifdef MCSSerialProbe
2658 .probe = mos7840_serial_probe,
2659#endif
2660 .ioctl = mos7840_ioctl,
2661 .set_termios = mos7840_set_termios,
2662 .break_ctl = mos7840_break,
2663 .tiocmget = mos7840_tiocmget,
2664 .tiocmset = mos7840_tiocmset,
2665 .attach = mos7840_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -04002666 .disconnect = mos7840_disconnect,
2667 .release = mos7840_release,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002668 .read_bulk_callback = mos7840_bulk_in_callback,
2669 .read_int_callback = mos7840_interrupt_callback,
2670};
2671
Paul B Schroeder3f542972006-08-31 19:41:47 -05002672/****************************************************************************
2673 * moschip7840_init
2674 * This is called by the module subsystem, or on startup to initialize us
2675 ****************************************************************************/
2676static int __init moschip7840_init(void)
2677{
2678 int retval;
2679
Tony Cook84fe6e72009-04-18 22:55:06 +09302680 dbg("%s", " mos7840_init :entering..........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002681
2682 /* Register with the usb serial */
2683 retval = usb_serial_register(&moschip7840_4port_device);
2684
2685 if (retval)
2686 goto failed_port_device_register;
2687
Tony Cook84fe6e72009-04-18 22:55:06 +09302688 dbg("%s", "Entering...");
Greg Kroah-Hartmanc197a8d2008-08-18 13:21:04 -07002689 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
2690 DRIVER_DESC "\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002691
2692 /* Register with the usb */
2693 retval = usb_register(&io_driver);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002694 if (retval == 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302695 dbg("%s", "Leaving...");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002696 return 0;
2697 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002698 usb_serial_deregister(&moschip7840_4port_device);
Alan Cox880af9d2008-07-22 11:16:12 +01002699failed_port_device_register:
Paul B Schroeder3f542972006-08-31 19:41:47 -05002700 return retval;
2701}
2702
2703/****************************************************************************
2704 * moschip7840_exit
2705 * Called when the driver is about to be unloaded.
2706 ****************************************************************************/
2707static void __exit moschip7840_exit(void)
2708{
2709
Tony Cook84fe6e72009-04-18 22:55:06 +09302710 dbg("%s", " mos7840_exit :entering..........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002711
2712 usb_deregister(&io_driver);
2713
2714 usb_serial_deregister(&moschip7840_4port_device);
2715
Tony Cook84fe6e72009-04-18 22:55:06 +09302716 dbg("%s", "Entering...");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002717}
2718
2719module_init(moschip7840_init);
2720module_exit(moschip7840_exit);
2721
2722/* Module information */
2723MODULE_DESCRIPTION(DRIVER_DESC);
2724MODULE_LICENSE("GPL");
2725
2726module_param(debug, bool, S_IRUGO | S_IWUSR);
2727MODULE_PARM_DESC(debug, "Debug enabled or not");