blob: 270009afdf77043f2e60034b8d29182dad053edf [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
124#define BANDB_DEVICE_ID_USOPTL4_4 0xAC44
125#define BANDB_DEVICE_ID_USOPTL4_2 0xAC42
Paul B Schroeder3f542972006-08-31 19:41:47 -0500126
Russell Lang9d498be2009-07-17 19:29:20 +1000127/* This driver also supports
128 * ATEN UC2324 device using Moschip MCS7840
129 * ATEN UC2322 device using Moschip MCS7820
130 */
Tony Cooke9b8cff2009-04-18 22:42:18 +0930131#define USB_VENDOR_ID_ATENINTL 0x0557
132#define ATENINTL_DEVICE_ID_UC2324 0x2011
Russell Lang9d498be2009-07-17 19:29:20 +1000133#define ATENINTL_DEVICE_ID_UC2322 0x7820
Tony Cooke9b8cff2009-04-18 22:42:18 +0930134
David Ludlow11e1abb2008-02-25 17:30:52 -0500135/* Interrupt Routine Defines */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500136
137#define SERIAL_IIR_RLS 0x06
138#define SERIAL_IIR_MS 0x00
139
140/*
141 * Emulation of the bit mask on the LINE STATUS REGISTER.
142 */
143#define SERIAL_LSR_DR 0x0001
144#define SERIAL_LSR_OE 0x0002
145#define SERIAL_LSR_PE 0x0004
146#define SERIAL_LSR_FE 0x0008
147#define SERIAL_LSR_BI 0x0010
148
149#define MOS_MSR_DELTA_CTS 0x10
150#define MOS_MSR_DELTA_DSR 0x20
151#define MOS_MSR_DELTA_RI 0x40
152#define MOS_MSR_DELTA_CD 0x80
153
Alan Cox880af9d2008-07-22 11:16:12 +0100154/* Serial Port register Address */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500155#define INTERRUPT_ENABLE_REGISTER ((__u16)(0x01))
156#define FIFO_CONTROL_REGISTER ((__u16)(0x02))
157#define LINE_CONTROL_REGISTER ((__u16)(0x03))
158#define MODEM_CONTROL_REGISTER ((__u16)(0x04))
159#define LINE_STATUS_REGISTER ((__u16)(0x05))
160#define MODEM_STATUS_REGISTER ((__u16)(0x06))
161#define SCRATCH_PAD_REGISTER ((__u16)(0x07))
162#define DIVISOR_LATCH_LSB ((__u16)(0x00))
163#define DIVISOR_LATCH_MSB ((__u16)(0x01))
164
165#define CLK_MULTI_REGISTER ((__u16)(0x02))
166#define CLK_START_VALUE_REGISTER ((__u16)(0x03))
167
168#define SERIAL_LCR_DLAB ((__u16)(0x0080))
169
170/*
171 * URB POOL related defines
172 */
173#define NUM_URBS 16 /* URB Count */
174#define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
175
176
177static struct usb_device_id moschip_port_id_table[] = {
178 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7840)},
179 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7820)},
David Ludlow11e1abb2008-02-25 17:30:52 -0500180 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4)},
181 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2)},
Tony Cooke9b8cff2009-04-18 22:42:18 +0930182 {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2324)},
Russell Lang9d498be2009-07-17 19:29:20 +1000183 {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2322)},
Paul B Schroeder3f542972006-08-31 19:41:47 -0500184 {} /* terminating entry */
185};
186
187static __devinitdata struct usb_device_id moschip_id_table_combined[] = {
188 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7840)},
189 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7820)},
David Ludlow11e1abb2008-02-25 17:30:52 -0500190 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4)},
191 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2)},
Tony Cooke9b8cff2009-04-18 22:42:18 +0930192 {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2324)},
Russell Lang9d498be2009-07-17 19:29:20 +1000193 {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2322)},
Paul B Schroeder3f542972006-08-31 19:41:47 -0500194 {} /* terminating entry */
195};
196
197MODULE_DEVICE_TABLE(usb, moschip_id_table_combined);
198
199/* This structure holds all of the local port information */
200
201struct moschip_port {
202 int port_num; /*Actual port number in the device(1,2,etc) */
203 struct urb *write_urb; /* write URB for this port */
204 struct urb *read_urb; /* read URB for this port */
Oliver Neukum0de9a702007-03-16 20:28:28 +0100205 struct urb *int_urb;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500206 __u8 shadowLCR; /* last LCR value received */
207 __u8 shadowMCR; /* last MCR value received */
208 char open;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100209 char open_ports;
210 char zombie;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500211 wait_queue_head_t wait_chase; /* for handling sleeping while waiting for chase to finish */
212 wait_queue_head_t delta_msr_wait; /* for handling sleeping while waiting for msr change to happen */
213 int delta_msr_cond;
214 struct async_icount icount;
215 struct usb_serial_port *port; /* loop back to the owner of this object */
216
Alan Cox880af9d2008-07-22 11:16:12 +0100217 /* Offsets */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500218 __u8 SpRegOffset;
219 __u8 ControlRegOffset;
220 __u8 DcrRegOffset;
Alan Cox880af9d2008-07-22 11:16:12 +0100221 /* for processing control URBS in interrupt context */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500222 struct urb *control_urb;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100223 struct usb_ctrlrequest *dr;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500224 char *ctrl_buf;
225 int MsrLsr;
226
Oliver Neukum0de9a702007-03-16 20:28:28 +0100227 spinlock_t pool_lock;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500228 struct urb *write_urb_pool[NUM_URBS];
Oliver Neukum0de9a702007-03-16 20:28:28 +0100229 char busy[NUM_URBS];
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800230 bool read_urb_busy;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500231};
232
233
234static int debug;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500235
236/*
237 * mos7840_set_reg_sync
238 * To set the Control register by calling usb_fill_control_urb function
239 * by passing usb_sndctrlpipe function as parameter.
240 */
241
242static int mos7840_set_reg_sync(struct usb_serial_port *port, __u16 reg,
243 __u16 val)
244{
245 struct usb_device *dev = port->serial->dev;
246 val = val & 0x00ff;
Tony Cook84fe6e72009-04-18 22:55:06 +0930247 dbg("mos7840_set_reg_sync offset is %x, value %x", reg, val);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500248
249 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
250 MCS_WR_RTYPE, val, reg, NULL, 0,
251 MOS_WDR_TIMEOUT);
252}
253
254/*
255 * mos7840_get_reg_sync
256 * To set the Uart register by calling usb_fill_control_urb function by
257 * passing usb_rcvctrlpipe function as parameter.
258 */
259
260static int mos7840_get_reg_sync(struct usb_serial_port *port, __u16 reg,
Alan Cox880af9d2008-07-22 11:16:12 +0100261 __u16 *val)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500262{
263 struct usb_device *dev = port->serial->dev;
264 int ret = 0;
265
266 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
267 MCS_RD_RTYPE, 0, reg, val, VENDOR_READ_LENGTH,
268 MOS_WDR_TIMEOUT);
Tony Cook84fe6e72009-04-18 22:55:06 +0930269 dbg("mos7840_get_reg_sync offset is %x, return val %x", reg, *val);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500270 *val = (*val) & 0x00ff;
271 return ret;
272}
273
274/*
275 * mos7840_set_uart_reg
276 * To set the Uart register by calling usb_fill_control_urb function by
277 * passing usb_sndctrlpipe function as parameter.
278 */
279
280static int mos7840_set_uart_reg(struct usb_serial_port *port, __u16 reg,
281 __u16 val)
282{
283
284 struct usb_device *dev = port->serial->dev;
285 val = val & 0x00ff;
Alan Cox880af9d2008-07-22 11:16:12 +0100286 /* For the UART control registers, the application number need
287 to be Or'ed */
Oliver Neukum0de9a702007-03-16 20:28:28 +0100288 if (port->serial->num_ports == 4) {
Alan Cox880af9d2008-07-22 11:16:12 +0100289 val |= (((__u16) port->number -
290 (__u16) (port->serial->minor)) + 1) << 8;
Tony Cook84fe6e72009-04-18 22:55:06 +0930291 dbg("mos7840_set_uart_reg application number is %x", val);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500292 } else {
293 if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
Alan Cox880af9d2008-07-22 11:16:12 +0100294 val |= (((__u16) port->number -
Paul B Schroeder3f542972006-08-31 19:41:47 -0500295 (__u16) (port->serial->minor)) + 1) << 8;
Tony Cook84fe6e72009-04-18 22:55:06 +0930296 dbg("mos7840_set_uart_reg application number is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -0500297 val);
298 } else {
299 val |=
300 (((__u16) port->number -
301 (__u16) (port->serial->minor)) + 2) << 8;
Tony Cook84fe6e72009-04-18 22:55:06 +0930302 dbg("mos7840_set_uart_reg application number is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -0500303 val);
304 }
305 }
306 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
307 MCS_WR_RTYPE, val, reg, NULL, 0,
308 MOS_WDR_TIMEOUT);
309
310}
311
312/*
313 * mos7840_get_uart_reg
314 * To set the Control register by calling usb_fill_control_urb function
315 * by passing usb_rcvctrlpipe function as parameter.
316 */
317static int mos7840_get_uart_reg(struct usb_serial_port *port, __u16 reg,
Alan Cox880af9d2008-07-22 11:16:12 +0100318 __u16 *val)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500319{
320 struct usb_device *dev = port->serial->dev;
321 int ret = 0;
322 __u16 Wval;
323
Tony Cook84fe6e72009-04-18 22:55:06 +0930324 /* dbg("application number is %4x",
Alan Cox880af9d2008-07-22 11:16:12 +0100325 (((__u16)port->number - (__u16)(port->serial->minor))+1)<<8); */
326 /* Wval is same as application number */
Oliver Neukum0de9a702007-03-16 20:28:28 +0100327 if (port->serial->num_ports == 4) {
Paul B Schroeder3f542972006-08-31 19:41:47 -0500328 Wval =
329 (((__u16) port->number - (__u16) (port->serial->minor)) +
330 1) << 8;
Tony Cook84fe6e72009-04-18 22:55:06 +0930331 dbg("mos7840_get_uart_reg application number is %x", Wval);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500332 } else {
333 if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
Alan Cox880af9d2008-07-22 11:16:12 +0100334 Wval = (((__u16) port->number -
Paul B Schroeder3f542972006-08-31 19:41:47 -0500335 (__u16) (port->serial->minor)) + 1) << 8;
Tony Cook84fe6e72009-04-18 22:55:06 +0930336 dbg("mos7840_get_uart_reg application number is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -0500337 Wval);
338 } else {
Alan Cox880af9d2008-07-22 11:16:12 +0100339 Wval = (((__u16) port->number -
Paul B Schroeder3f542972006-08-31 19:41:47 -0500340 (__u16) (port->serial->minor)) + 2) << 8;
Tony Cook84fe6e72009-04-18 22:55:06 +0930341 dbg("mos7840_get_uart_reg application number is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -0500342 Wval);
343 }
344 }
345 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
346 MCS_RD_RTYPE, Wval, reg, val, VENDOR_READ_LENGTH,
347 MOS_WDR_TIMEOUT);
348 *val = (*val) & 0x00ff;
349 return ret;
350}
351
352static void mos7840_dump_serial_port(struct moschip_port *mos7840_port)
353{
354
Tony Cook84fe6e72009-04-18 22:55:06 +0930355 dbg("***************************************");
356 dbg("SpRegOffset is %2x", mos7840_port->SpRegOffset);
357 dbg("ControlRegOffset is %2x", mos7840_port->ControlRegOffset);
358 dbg("DCRRegOffset is %2x", mos7840_port->DcrRegOffset);
359 dbg("***************************************");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500360
361}
362
363/************************************************************************/
364/************************************************************************/
365/* I N T E R F A C E F U N C T I O N S */
366/* I N T E R F A C E F U N C T I O N S */
367/************************************************************************/
368/************************************************************************/
369
370static inline void mos7840_set_port_private(struct usb_serial_port *port,
371 struct moschip_port *data)
372{
373 usb_set_serial_port_data(port, (void *)data);
374}
375
376static inline struct moschip_port *mos7840_get_port_private(struct
377 usb_serial_port
378 *port)
379{
380 return (struct moschip_port *)usb_get_serial_port_data(port);
381}
382
Oliver Neukum0de9a702007-03-16 20:28:28 +0100383static void mos7840_handle_new_msr(struct moschip_port *port, __u8 new_msr)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500384{
385 struct moschip_port *mos7840_port;
386 struct async_icount *icount;
387 mos7840_port = port;
388 icount = &mos7840_port->icount;
389 if (new_msr &
390 (MOS_MSR_DELTA_CTS | MOS_MSR_DELTA_DSR | MOS_MSR_DELTA_RI |
391 MOS_MSR_DELTA_CD)) {
392 icount = &mos7840_port->icount;
393
394 /* update input line counters */
395 if (new_msr & MOS_MSR_DELTA_CTS) {
396 icount->cts++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100397 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500398 }
399 if (new_msr & MOS_MSR_DELTA_DSR) {
400 icount->dsr++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100401 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500402 }
403 if (new_msr & MOS_MSR_DELTA_CD) {
404 icount->dcd++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100405 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500406 }
407 if (new_msr & MOS_MSR_DELTA_RI) {
408 icount->rng++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100409 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500410 }
411 }
Paul B Schroeder3f542972006-08-31 19:41:47 -0500412}
413
Oliver Neukum0de9a702007-03-16 20:28:28 +0100414static void mos7840_handle_new_lsr(struct moschip_port *port, __u8 new_lsr)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500415{
416 struct async_icount *icount;
417
Harvey Harrison441b62c2008-03-03 16:08:34 -0800418 dbg("%s - %02x", __func__, new_lsr);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500419
420 if (new_lsr & SERIAL_LSR_BI) {
Alan Cox880af9d2008-07-22 11:16:12 +0100421 /*
422 * Parity and Framing errors only count if they
423 * occur exclusive of a break being
424 * received.
425 */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500426 new_lsr &= (__u8) (SERIAL_LSR_OE | SERIAL_LSR_BI);
427 }
428
429 /* update input line counters */
430 icount = &port->icount;
431 if (new_lsr & SERIAL_LSR_BI) {
432 icount->brk++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100433 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500434 }
435 if (new_lsr & SERIAL_LSR_OE) {
436 icount->overrun++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100437 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500438 }
439 if (new_lsr & SERIAL_LSR_PE) {
440 icount->parity++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100441 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500442 }
443 if (new_lsr & SERIAL_LSR_FE) {
444 icount->frame++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100445 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500446 }
Paul B Schroeder3f542972006-08-31 19:41:47 -0500447}
448
449/************************************************************************/
450/************************************************************************/
451/* U S B C A L L B A C K F U N C T I O N S */
452/* U S B C A L L B A C K F U N C T I O N S */
453/************************************************************************/
454/************************************************************************/
455
David Howells7d12e782006-10-05 14:55:46 +0100456static void mos7840_control_callback(struct urb *urb)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500457{
458 unsigned char *data;
459 struct moschip_port *mos7840_port;
460 __u8 regval = 0x0;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100461 int result = 0;
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700462 int status = urb->status;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500463
Ming Leicdc97792008-02-24 18:41:47 +0800464 mos7840_port = urb->context;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100465
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700466 switch (status) {
Paul B Schroeder3f542972006-08-31 19:41:47 -0500467 case 0:
468 /* success */
469 break;
470 case -ECONNRESET:
471 case -ENOENT:
472 case -ESHUTDOWN:
473 /* this urb is terminated, clean up */
Harvey Harrison441b62c2008-03-03 16:08:34 -0800474 dbg("%s - urb shutting down with status: %d", __func__,
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700475 status);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500476 return;
477 default:
Harvey Harrison441b62c2008-03-03 16:08:34 -0800478 dbg("%s - nonzero urb status received: %d", __func__,
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700479 status);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500480 goto exit;
481 }
482
Tony Cook84fe6e72009-04-18 22:55:06 +0930483 dbg("%s urb buffer size is %d", __func__, urb->actual_length);
484 dbg("%s mos7840_port->MsrLsr is %d port %d", __func__,
Paul B Schroeder3f542972006-08-31 19:41:47 -0500485 mos7840_port->MsrLsr, mos7840_port->port_num);
486 data = urb->transfer_buffer;
487 regval = (__u8) data[0];
Tony Cook84fe6e72009-04-18 22:55:06 +0930488 dbg("%s data is %x", __func__, regval);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500489 if (mos7840_port->MsrLsr == 0)
490 mos7840_handle_new_msr(mos7840_port, regval);
491 else if (mos7840_port->MsrLsr == 1)
492 mos7840_handle_new_lsr(mos7840_port, regval);
493
Oliver Neukum0de9a702007-03-16 20:28:28 +0100494exit:
495 spin_lock(&mos7840_port->pool_lock);
496 if (!mos7840_port->zombie)
497 result = usb_submit_urb(mos7840_port->int_urb, GFP_ATOMIC);
498 spin_unlock(&mos7840_port->pool_lock);
499 if (result) {
500 dev_err(&urb->dev->dev,
501 "%s - Error %d submitting interrupt urb\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800502 __func__, result);
Oliver Neukum0de9a702007-03-16 20:28:28 +0100503 }
Paul B Schroeder3f542972006-08-31 19:41:47 -0500504}
505
506static int mos7840_get_reg(struct moschip_port *mcs, __u16 Wval, __u16 reg,
Alan Cox880af9d2008-07-22 11:16:12 +0100507 __u16 *val)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500508{
509 struct usb_device *dev = mcs->port->serial->dev;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100510 struct usb_ctrlrequest *dr = mcs->dr;
511 unsigned char *buffer = mcs->ctrl_buf;
512 int ret;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500513
Paul B Schroeder3f542972006-08-31 19:41:47 -0500514 dr->bRequestType = MCS_RD_RTYPE;
515 dr->bRequest = MCS_RDREQ;
Alan Cox880af9d2008-07-22 11:16:12 +0100516 dr->wValue = cpu_to_le16(Wval); /* 0 */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500517 dr->wIndex = cpu_to_le16(reg);
518 dr->wLength = cpu_to_le16(2);
519
520 usb_fill_control_urb(mcs->control_urb, dev, usb_rcvctrlpipe(dev, 0),
521 (unsigned char *)dr, buffer, 2,
522 mos7840_control_callback, mcs);
523 mcs->control_urb->transfer_buffer_length = 2;
524 ret = usb_submit_urb(mcs->control_urb, GFP_ATOMIC);
525 return ret;
526}
527
528/*****************************************************************************
529 * mos7840_interrupt_callback
530 * this is the callback function for when we have received data on the
531 * interrupt endpoint.
532 *****************************************************************************/
533
David Howells7d12e782006-10-05 14:55:46 +0100534static void mos7840_interrupt_callback(struct urb *urb)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500535{
536 int result;
537 int length;
538 struct moschip_port *mos7840_port;
539 struct usb_serial *serial;
540 __u16 Data;
541 unsigned char *data;
542 __u8 sp[5], st;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100543 int i, rv = 0;
544 __u16 wval, wreg = 0;
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700545 int status = urb->status;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500546
Tony Cook84fe6e72009-04-18 22:55:06 +0930547 dbg("%s", " : Entering");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500548
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700549 switch (status) {
Paul B Schroeder3f542972006-08-31 19:41:47 -0500550 case 0:
551 /* success */
552 break;
553 case -ECONNRESET:
554 case -ENOENT:
555 case -ESHUTDOWN:
556 /* this urb is terminated, clean up */
Harvey Harrison441b62c2008-03-03 16:08:34 -0800557 dbg("%s - urb shutting down with status: %d", __func__,
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700558 status);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500559 return;
560 default:
Harvey Harrison441b62c2008-03-03 16:08:34 -0800561 dbg("%s - nonzero urb status received: %d", __func__,
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700562 status);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500563 goto exit;
564 }
565
566 length = urb->actual_length;
567 data = urb->transfer_buffer;
568
Ming Leicdc97792008-02-24 18:41:47 +0800569 serial = urb->context;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500570
571 /* Moschip get 5 bytes
572 * Byte 1 IIR Port 1 (port.number is 0)
573 * Byte 2 IIR Port 2 (port.number is 1)
574 * Byte 3 IIR Port 3 (port.number is 2)
575 * Byte 4 IIR Port 4 (port.number is 3)
576 * Byte 5 FIFO status for both */
577
578 if (length && length > 5) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930579 dbg("%s", "Wrong data !!!");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500580 return;
581 }
582
583 sp[0] = (__u8) data[0];
584 sp[1] = (__u8) data[1];
585 sp[2] = (__u8) data[2];
586 sp[3] = (__u8) data[3];
587 st = (__u8) data[4];
588
589 for (i = 0; i < serial->num_ports; i++) {
590 mos7840_port = mos7840_get_port_private(serial->port[i]);
591 wval =
592 (((__u16) serial->port[i]->number -
593 (__u16) (serial->minor)) + 1) << 8;
594 if (mos7840_port->open) {
595 if (sp[i] & 0x01) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930596 dbg("SP%d No Interrupt !!!", i);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500597 } else {
598 switch (sp[i] & 0x0f) {
599 case SERIAL_IIR_RLS:
600 dbg("Serial Port %d: Receiver status error or ", i);
Tony Cook84fe6e72009-04-18 22:55:06 +0930601 dbg("address bit detected in 9-bit mode");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500602 mos7840_port->MsrLsr = 1;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100603 wreg = LINE_STATUS_REGISTER;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500604 break;
605 case SERIAL_IIR_MS:
Tony Cook84fe6e72009-04-18 22:55:06 +0930606 dbg("Serial Port %d: Modem status change", i);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500607 mos7840_port->MsrLsr = 0;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100608 wreg = MODEM_STATUS_REGISTER;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500609 break;
610 }
Oliver Neukum0de9a702007-03-16 20:28:28 +0100611 spin_lock(&mos7840_port->pool_lock);
612 if (!mos7840_port->zombie) {
613 rv = mos7840_get_reg(mos7840_port, wval, wreg, &Data);
614 } else {
615 spin_unlock(&mos7840_port->pool_lock);
616 return;
617 }
618 spin_unlock(&mos7840_port->pool_lock);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500619 }
620 }
621 }
Alan Cox880af9d2008-07-22 11:16:12 +0100622 if (!(rv < 0))
623 /* the completion handler for the control urb will resubmit */
Oliver Neukum0de9a702007-03-16 20:28:28 +0100624 return;
625exit:
Paul B Schroeder3f542972006-08-31 19:41:47 -0500626 result = usb_submit_urb(urb, GFP_ATOMIC);
627 if (result) {
628 dev_err(&urb->dev->dev,
629 "%s - Error %d submitting interrupt urb\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800630 __func__, result);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500631 }
Paul B Schroeder3f542972006-08-31 19:41:47 -0500632}
633
634static int mos7840_port_paranoia_check(struct usb_serial_port *port,
635 const char *function)
636{
637 if (!port) {
638 dbg("%s - port == NULL", function);
639 return -1;
640 }
641 if (!port->serial) {
642 dbg("%s - port->serial == NULL", function);
643 return -1;
644 }
645
646 return 0;
647}
648
649/* Inline functions to check the sanity of a pointer that is passed to us */
650static int mos7840_serial_paranoia_check(struct usb_serial *serial,
651 const char *function)
652{
653 if (!serial) {
654 dbg("%s - serial == NULL", function);
655 return -1;
656 }
657 if (!serial->type) {
658 dbg("%s - serial->type == NULL!", function);
659 return -1;
660 }
661
662 return 0;
663}
664
665static struct usb_serial *mos7840_get_usb_serial(struct usb_serial_port *port,
666 const char *function)
667{
668 /* if no port was specified, or it fails a paranoia check */
669 if (!port ||
670 mos7840_port_paranoia_check(port, function) ||
671 mos7840_serial_paranoia_check(port->serial, function)) {
Alan Cox880af9d2008-07-22 11:16:12 +0100672 /* then say that we don't have a valid usb_serial thing,
673 * which will end up genrating -ENODEV return values */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500674 return NULL;
675 }
676
677 return port->serial;
678}
679
680/*****************************************************************************
681 * mos7840_bulk_in_callback
682 * this is the callback function for when we have received data on the
683 * bulk in endpoint.
684 *****************************************************************************/
685
David Howells7d12e782006-10-05 14:55:46 +0100686static void mos7840_bulk_in_callback(struct urb *urb)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500687{
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700688 int retval;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500689 unsigned char *data;
690 struct usb_serial *serial;
691 struct usb_serial_port *port;
692 struct moschip_port *mos7840_port;
693 struct tty_struct *tty;
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700694 int status = urb->status;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500695
Ming Leicdc97792008-02-24 18:41:47 +0800696 mos7840_port = urb->context;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500697 if (!mos7840_port) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930698 dbg("%s", "NULL mos7840_port pointer");
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800699 mos7840_port->read_urb_busy = false;
700 return;
701 }
702
703 if (status) {
704 dbg("nonzero read bulk status received: %d", status);
705 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500706 return;
707 }
708
709 port = (struct usb_serial_port *)mos7840_port->port;
Harvey Harrison441b62c2008-03-03 16:08:34 -0800710 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930711 dbg("%s", "Port Paranoia failed");
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800712 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500713 return;
714 }
715
Harvey Harrison441b62c2008-03-03 16:08:34 -0800716 serial = mos7840_get_usb_serial(port, __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500717 if (!serial) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930718 dbg("%s", "Bad serial pointer");
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800719 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500720 return;
721 }
722
Tony Cook84fe6e72009-04-18 22:55:06 +0930723 dbg("%s", "Entering... ");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500724
725 data = urb->transfer_buffer;
726
Tony Cook84fe6e72009-04-18 22:55:06 +0930727 dbg("%s", "Entering ...........");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500728
729 if (urb->actual_length) {
Alan Cox4a90f092008-10-13 10:39:46 +0100730 tty = tty_port_tty_get(&mos7840_port->port->port);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500731 if (tty) {
732 tty_buffer_request_room(tty, urb->actual_length);
733 tty_insert_flip_string(tty, data, urb->actual_length);
Tony Cook84fe6e72009-04-18 22:55:06 +0930734 dbg(" %s ", data);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500735 tty_flip_buffer_push(tty);
Alan Cox4a90f092008-10-13 10:39:46 +0100736 tty_kref_put(tty);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500737 }
738 mos7840_port->icount.rx += urb->actual_length;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100739 smp_wmb();
Tony Cook84fe6e72009-04-18 22:55:06 +0930740 dbg("mos7840_port->icount.rx is %d:",
Paul B Schroeder3f542972006-08-31 19:41:47 -0500741 mos7840_port->icount.rx);
742 }
743
744 if (!mos7840_port->read_urb) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930745 dbg("%s", "URB KILLED !!!");
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800746 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500747 return;
748 }
749
Paul B Schroeder3f542972006-08-31 19:41:47 -0500750
Oliver Neukum0de9a702007-03-16 20:28:28 +0100751 mos7840_port->read_urb->dev = serial->dev;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500752
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800753 mos7840_port->read_urb_busy = true;
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700754 retval = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
Oliver Neukum0de9a702007-03-16 20:28:28 +0100755
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700756 if (retval) {
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800757 dbg("usb_submit_urb(read bulk) failed, retval = %d", retval);
758 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500759 }
760}
761
762/*****************************************************************************
763 * mos7840_bulk_out_data_callback
Alan Cox880af9d2008-07-22 11:16:12 +0100764 * this is the callback function for when we have finished sending
765 * serial data on the bulk out endpoint.
Paul B Schroeder3f542972006-08-31 19:41:47 -0500766 *****************************************************************************/
767
David Howells7d12e782006-10-05 14:55:46 +0100768static void mos7840_bulk_out_data_callback(struct urb *urb)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500769{
770 struct moschip_port *mos7840_port;
771 struct tty_struct *tty;
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700772 int status = urb->status;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100773 int i;
774
Ming Leicdc97792008-02-24 18:41:47 +0800775 mos7840_port = urb->context;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100776 spin_lock(&mos7840_port->pool_lock);
777 for (i = 0; i < NUM_URBS; i++) {
778 if (urb == mos7840_port->write_urb_pool[i]) {
779 mos7840_port->busy[i] = 0;
780 break;
781 }
782 }
783 spin_unlock(&mos7840_port->pool_lock);
784
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700785 if (status) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930786 dbg("nonzero write bulk status received:%d", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500787 return;
788 }
789
Harvey Harrison441b62c2008-03-03 16:08:34 -0800790 if (mos7840_port_paranoia_check(mos7840_port->port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930791 dbg("%s", "Port Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500792 return;
793 }
794
Tony Cook84fe6e72009-04-18 22:55:06 +0930795 dbg("%s", "Entering .........");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500796
Alan Cox4a90f092008-10-13 10:39:46 +0100797 tty = tty_port_tty_get(&mos7840_port->port->port);
Jiri Slabyb963a842007-02-10 01:44:55 -0800798 if (tty && mos7840_port->open)
799 tty_wakeup(tty);
Alan Cox4a90f092008-10-13 10:39:46 +0100800 tty_kref_put(tty);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500801
802}
803
804/************************************************************************/
805/* 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 */
806/************************************************************************/
807#ifdef MCSSerialProbe
808static int mos7840_serial_probe(struct usb_serial *serial,
809 const struct usb_device_id *id)
810{
811
812 /*need to implement the mode_reg reading and updating\
813 structures usb_serial_ device_type\
814 (i.e num_ports, num_bulkin,bulkout etc) */
815 /* Also we can update the changes attach */
816 return 1;
817}
818#endif
819
820/*****************************************************************************
821 * mos7840_open
822 * this function is called by the tty driver when a port is opened
823 * If successful, we return 0
824 * Otherwise we return a negative error number.
825 *****************************************************************************/
826
Alan Cox95da3102008-07-22 11:09:07 +0100827static int mos7840_open(struct tty_struct *tty,
828 struct usb_serial_port *port, struct file *filp)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500829{
830 int response;
831 int j;
832 struct usb_serial *serial;
833 struct urb *urb;
834 __u16 Data;
835 int status;
836 struct moschip_port *mos7840_port;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100837 struct moschip_port *port0;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500838
Tony Cook84fe6e72009-04-18 22:55:06 +0930839 dbg ("%s enter", __func__);
840
Harvey Harrison441b62c2008-03-03 16:08:34 -0800841 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930842 dbg("%s", "Port Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500843 return -ENODEV;
844 }
845
Paul B Schroeder3f542972006-08-31 19:41:47 -0500846 serial = port->serial;
847
Harvey Harrison441b62c2008-03-03 16:08:34 -0800848 if (mos7840_serial_paranoia_check(serial, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930849 dbg("%s", "Serial Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500850 return -ENODEV;
851 }
852
853 mos7840_port = mos7840_get_port_private(port);
Oliver Neukum0de9a702007-03-16 20:28:28 +0100854 port0 = mos7840_get_port_private(serial->port[0]);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500855
Oliver Neukum0de9a702007-03-16 20:28:28 +0100856 if (mos7840_port == NULL || port0 == NULL)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500857 return -ENODEV;
858
859 usb_clear_halt(serial->dev, port->write_urb->pipe);
860 usb_clear_halt(serial->dev, port->read_urb->pipe);
Oliver Neukum0de9a702007-03-16 20:28:28 +0100861 port0->open_ports++;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500862
863 /* Initialising the write urb pool */
864 for (j = 0; j < NUM_URBS; ++j) {
Oliver Neukum0de9a702007-03-16 20:28:28 +0100865 urb = usb_alloc_urb(0, GFP_KERNEL);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500866 mos7840_port->write_urb_pool[j] = urb;
867
868 if (urb == NULL) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700869 dev_err(&port->dev, "No more urbs???\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500870 continue;
871 }
872
Alan Cox880af9d2008-07-22 11:16:12 +0100873 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
874 GFP_KERNEL);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500875 if (!urb->transfer_buffer) {
Oliver Neukum0de9a702007-03-16 20:28:28 +0100876 usb_free_urb(urb);
877 mos7840_port->write_urb_pool[j] = NULL;
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700878 dev_err(&port->dev,
879 "%s-out of memory for urb buffers.\n",
880 __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500881 continue;
882 }
883 }
884
885/*****************************************************************************
886 * Initialize MCS7840 -- Write Init values to corresponding Registers
887 *
888 * Register Index
889 * 1 : IER
890 * 2 : FCR
891 * 3 : LCR
892 * 4 : MCR
893 *
894 * 0x08 : SP1/2 Control Reg
895 *****************************************************************************/
896
Alan Cox880af9d2008-07-22 11:16:12 +0100897 /* NEED to check the following Block */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500898
Paul B Schroeder3f542972006-08-31 19:41:47 -0500899 Data = 0x0;
900 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
901 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930902 dbg("Reading Spreg failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500903 return -1;
904 }
905 Data |= 0x80;
906 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
907 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930908 dbg("writing Spreg failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500909 return -1;
910 }
911
912 Data &= ~0x80;
913 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
914 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930915 dbg("writing Spreg failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500916 return -1;
917 }
Alan Cox880af9d2008-07-22 11:16:12 +0100918 /* End of block to be checked */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500919
Paul B Schroeder3f542972006-08-31 19:41:47 -0500920 Data = 0x0;
Alan Cox880af9d2008-07-22 11:16:12 +0100921 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
922 &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500923 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930924 dbg("Reading Controlreg failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500925 return -1;
926 }
Alan Cox880af9d2008-07-22 11:16:12 +0100927 Data |= 0x08; /* Driver done bit */
928 Data |= 0x20; /* rx_disable */
929 status = mos7840_set_reg_sync(port,
930 mos7840_port->ControlRegOffset, Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500931 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930932 dbg("writing Controlreg failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500933 return -1;
934 }
Alan Cox880af9d2008-07-22 11:16:12 +0100935 /* do register settings here */
936 /* Set all regs to the device default values. */
937 /***********************************
938 * First Disable all interrupts.
939 ***********************************/
Paul B Schroeder3f542972006-08-31 19:41:47 -0500940 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500941 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
942 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930943 dbg("disabling interrupts failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500944 return -1;
945 }
Alan Cox880af9d2008-07-22 11:16:12 +0100946 /* Set FIFO_CONTROL_REGISTER to the default value */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500947 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500948 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
949 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930950 dbg("Writing FIFO_CONTROL_REGISTER failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500951 return -1;
952 }
953
954 Data = 0xcf;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500955 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
956 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930957 dbg("Writing FIFO_CONTROL_REGISTER failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500958 return -1;
959 }
960
961 Data = 0x03;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500962 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
963 mos7840_port->shadowLCR = Data;
964
965 Data = 0x0b;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500966 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
967 mos7840_port->shadowMCR = Data;
968
969 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500970 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
971 mos7840_port->shadowLCR = Data;
972
Alan Cox880af9d2008-07-22 11:16:12 +0100973 Data |= SERIAL_LCR_DLAB; /* data latch enable in LCR 0x80 */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500974 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
975
976 Data = 0x0c;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500977 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
978
979 Data = 0x0;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500980 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
981
982 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500983 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
984
985 Data = Data & ~SERIAL_LCR_DLAB;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500986 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
987 mos7840_port->shadowLCR = Data;
988
Alan Cox880af9d2008-07-22 11:16:12 +0100989 /* clearing Bulkin and Bulkout Fifo */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500990 Data = 0x0;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500991 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
992
993 Data = Data | 0x0c;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500994 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
995
996 Data = Data & ~0x0c;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500997 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
Alan Cox880af9d2008-07-22 11:16:12 +0100998 /* Finally enable all interrupts */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500999 Data = 0x0c;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001000 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1001
Alan Cox880af9d2008-07-22 11:16:12 +01001002 /* clearing rx_disable */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001003 Data = 0x0;
Alan Cox880af9d2008-07-22 11:16:12 +01001004 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1005 &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001006 Data = Data & ~0x20;
Alan Cox880af9d2008-07-22 11:16:12 +01001007 status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1008 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001009
Alan Cox880af9d2008-07-22 11:16:12 +01001010 /* rx_negate */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001011 Data = 0x0;
Alan Cox880af9d2008-07-22 11:16:12 +01001012 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1013 &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001014 Data = Data | 0x10;
Alan Cox880af9d2008-07-22 11:16:12 +01001015 status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1016 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001017
Alan Cox880af9d2008-07-22 11:16:12 +01001018 /* Check to see if we've set up our endpoint info yet *
1019 * (can't set it up in mos7840_startup as the structures *
1020 * were not set up at that time.) */
Oliver Neukum0de9a702007-03-16 20:28:28 +01001021 if (port0->open_ports == 1) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001022 if (serial->port[0]->interrupt_in_buffer == NULL) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001023 /* set up interrupt urb */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001024 usb_fill_int_urb(serial->port[0]->interrupt_in_urb,
Alan Cox880af9d2008-07-22 11:16:12 +01001025 serial->dev,
1026 usb_rcvintpipe(serial->dev,
1027 serial->port[0]->interrupt_in_endpointAddress),
1028 serial->port[0]->interrupt_in_buffer,
1029 serial->port[0]->interrupt_in_urb->
1030 transfer_buffer_length,
1031 mos7840_interrupt_callback,
1032 serial,
1033 serial->port[0]->interrupt_in_urb->interval);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001034
1035 /* start interrupt read for mos7840 *
1036 * will continue as long as mos7840 is connected */
1037
1038 response =
1039 usb_submit_urb(serial->port[0]->interrupt_in_urb,
1040 GFP_KERNEL);
1041 if (response) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001042 dev_err(&port->dev, "%s - Error %d submitting "
1043 "interrupt urb\n", __func__, response);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001044 }
1045
1046 }
1047
1048 }
1049
1050 /* see if we've set up our endpoint info yet *
1051 * (can't set it up in mos7840_startup as the *
1052 * structures were not set up at that time.) */
1053
Tony Cook84fe6e72009-04-18 22:55:06 +09301054 dbg("port number is %d", port->number);
1055 dbg("serial number is %d", port->serial->minor);
1056 dbg("Bulkin endpoint is %d", port->bulk_in_endpointAddress);
1057 dbg("BulkOut endpoint is %d", port->bulk_out_endpointAddress);
1058 dbg("Interrupt endpoint is %d", port->interrupt_in_endpointAddress);
1059 dbg("port's number in the device is %d", mos7840_port->port_num);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001060 mos7840_port->read_urb = port->read_urb;
1061
1062 /* set up our bulk in urb */
1063
1064 usb_fill_bulk_urb(mos7840_port->read_urb,
1065 serial->dev,
1066 usb_rcvbulkpipe(serial->dev,
1067 port->bulk_in_endpointAddress),
1068 port->bulk_in_buffer,
1069 mos7840_port->read_urb->transfer_buffer_length,
1070 mos7840_bulk_in_callback, mos7840_port);
1071
Tony Cook84fe6e72009-04-18 22:55:06 +09301072 dbg("mos7840_open: bulkin endpoint is %d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05001073 port->bulk_in_endpointAddress);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08001074 mos7840_port->read_urb_busy = true;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001075 response = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
1076 if (response) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001077 dev_err(&port->dev, "%s - Error %d submitting control urb\n",
1078 __func__, response);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08001079 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001080 }
1081
1082 /* initialize our wait queues */
1083 init_waitqueue_head(&mos7840_port->wait_chase);
1084 init_waitqueue_head(&mos7840_port->delta_msr_wait);
1085
1086 /* initialize our icount structure */
1087 memset(&(mos7840_port->icount), 0x00, sizeof(mos7840_port->icount));
1088
1089 /* initialize our port settings */
Alan Cox880af9d2008-07-22 11:16:12 +01001090 /* Must set to enable ints! */
1091 mos7840_port->shadowMCR = MCR_MASTER_IE;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001092 /* send a open port command */
1093 mos7840_port->open = 1;
Alan Cox880af9d2008-07-22 11:16:12 +01001094 /* mos7840_change_port_settings(mos7840_port,old_termios); */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001095 mos7840_port->icount.tx = 0;
1096 mos7840_port->icount.rx = 0;
1097
Tony Cook84fe6e72009-04-18 22:55:06 +09301098 dbg("usb_serial serial:%p mos7840_port:%p\n usb_serial_port port:%p",
Alan Cox880af9d2008-07-22 11:16:12 +01001099 serial, mos7840_port, port);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001100
Tony Cook84fe6e72009-04-18 22:55:06 +09301101 dbg ("%s leave", __func__);
1102
Paul B Schroeder3f542972006-08-31 19:41:47 -05001103 return 0;
1104
1105}
1106
1107/*****************************************************************************
1108 * mos7840_chars_in_buffer
1109 * this function is called by the tty driver when it wants to know how many
1110 * bytes of data we currently have outstanding in the port (data that has
1111 * been written, but hasn't made it out the port yet)
1112 * If successful, we return the number of bytes left to be written in the
1113 * system,
Alan Cox95da3102008-07-22 11:09:07 +01001114 * Otherwise we return zero.
Paul B Schroeder3f542972006-08-31 19:41:47 -05001115 *****************************************************************************/
1116
Alan Cox95da3102008-07-22 11:09:07 +01001117static int mos7840_chars_in_buffer(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001118{
Alan Cox95da3102008-07-22 11:09:07 +01001119 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001120 int i;
1121 int chars = 0;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001122 unsigned long flags;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001123 struct moschip_port *mos7840_port;
1124
Tony Cook84fe6e72009-04-18 22:55:06 +09301125 dbg("%s", " mos7840_chars_in_buffer:entering ...........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001126
Harvey Harrison441b62c2008-03-03 16:08:34 -08001127 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301128 dbg("%s", "Invalid port");
Alan Cox95da3102008-07-22 11:09:07 +01001129 return 0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001130 }
1131
1132 mos7840_port = mos7840_get_port_private(port);
1133 if (mos7840_port == NULL) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301134 dbg("%s", "mos7840_break:leaving ...........");
Alan Cox95da3102008-07-22 11:09:07 +01001135 return 0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001136 }
1137
Alan Cox880af9d2008-07-22 11:16:12 +01001138 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
Alan Cox95da3102008-07-22 11:09:07 +01001139 for (i = 0; i < NUM_URBS; ++i)
1140 if (mos7840_port->busy[i])
Paul B Schroeder3f542972006-08-31 19:41:47 -05001141 chars += URB_TRANSFER_BUFFER_SIZE;
Alan Cox880af9d2008-07-22 11:16:12 +01001142 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
Harvey Harrison441b62c2008-03-03 16:08:34 -08001143 dbg("%s - returns %d", __func__, chars);
Oliver Neukum0de9a702007-03-16 20:28:28 +01001144 return chars;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001145
1146}
1147
Paul B Schroeder3f542972006-08-31 19:41:47 -05001148/*****************************************************************************
1149 * mos7840_close
1150 * this function is called by the tty driver when a port is closed
1151 *****************************************************************************/
1152
Alan Cox335f8512009-06-11 12:26:29 +01001153static void mos7840_close(struct usb_serial_port *port)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001154{
1155 struct usb_serial *serial;
1156 struct moschip_port *mos7840_port;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001157 struct moschip_port *port0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001158 int j;
1159 __u16 Data;
1160
Tony Cook84fe6e72009-04-18 22:55:06 +09301161 dbg("%s", "mos7840_close:entering...");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001162
Harvey Harrison441b62c2008-03-03 16:08:34 -08001163 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301164 dbg("%s", "Port Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001165 return;
1166 }
1167
Harvey Harrison441b62c2008-03-03 16:08:34 -08001168 serial = mos7840_get_usb_serial(port, __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001169 if (!serial) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301170 dbg("%s", "Serial Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001171 return;
1172 }
1173
1174 mos7840_port = mos7840_get_port_private(port);
Oliver Neukum0de9a702007-03-16 20:28:28 +01001175 port0 = mos7840_get_port_private(serial->port[0]);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001176
Oliver Neukum0de9a702007-03-16 20:28:28 +01001177 if (mos7840_port == NULL || port0 == NULL)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001178 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001179
1180 for (j = 0; j < NUM_URBS; ++j)
1181 usb_kill_urb(mos7840_port->write_urb_pool[j]);
1182
1183 /* Freeing Write URBs */
1184 for (j = 0; j < NUM_URBS; ++j) {
1185 if (mos7840_port->write_urb_pool[j]) {
1186 if (mos7840_port->write_urb_pool[j]->transfer_buffer)
1187 kfree(mos7840_port->write_urb_pool[j]->
1188 transfer_buffer);
1189
1190 usb_free_urb(mos7840_port->write_urb_pool[j]);
1191 }
1192 }
1193
Paul B Schroeder3f542972006-08-31 19:41:47 -05001194 /* While closing port, shutdown all bulk read, write *
1195 * and interrupt read if they exists */
1196 if (serial->dev) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001197 if (mos7840_port->write_urb) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301198 dbg("%s", "Shutdown bulk write");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001199 usb_kill_urb(mos7840_port->write_urb);
1200 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001201 if (mos7840_port->read_urb) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301202 dbg("%s", "Shutdown bulk read");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001203 usb_kill_urb(mos7840_port->read_urb);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08001204 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001205 }
1206 if ((&mos7840_port->control_urb)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301207 dbg("%s", "Shutdown control read");
Alan Cox880af9d2008-07-22 11:16:12 +01001208 /*/ usb_kill_urb (mos7840_port->control_urb); */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001209 }
1210 }
Alan Cox880af9d2008-07-22 11:16:12 +01001211/* if(mos7840_port->ctrl_buf != NULL) */
1212/* kfree(mos7840_port->ctrl_buf); */
Oliver Neukum0de9a702007-03-16 20:28:28 +01001213 port0->open_ports--;
Tony Cook84fe6e72009-04-18 22:55:06 +09301214 dbg("mos7840_num_open_ports in close%d:in port%d",
Oliver Neukum0de9a702007-03-16 20:28:28 +01001215 port0->open_ports, port->number);
1216 if (port0->open_ports == 0) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001217 if (serial->port[0]->interrupt_in_urb) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301218 dbg("%s", "Shutdown interrupt_in_urb");
Oliver Neukum0de9a702007-03-16 20:28:28 +01001219 usb_kill_urb(serial->port[0]->interrupt_in_urb);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001220 }
1221 }
1222
1223 if (mos7840_port->write_urb) {
1224 /* if this urb had a transfer buffer already (old tx) free it */
Alan Cox880af9d2008-07-22 11:16:12 +01001225 if (mos7840_port->write_urb->transfer_buffer != NULL)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001226 kfree(mos7840_port->write_urb->transfer_buffer);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001227 usb_free_urb(mos7840_port->write_urb);
1228 }
1229
1230 Data = 0x0;
1231 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1232
1233 Data = 0x00;
1234 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1235
1236 mos7840_port->open = 0;
1237
Tony Cook84fe6e72009-04-18 22:55:06 +09301238 dbg("%s", "Leaving ............");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001239}
1240
1241/************************************************************************
1242 *
1243 * mos7840_block_until_chase_response
1244 *
1245 * This function will block the close until one of the following:
1246 * 1. Response to our Chase comes from mos7840
Joe Perchesdc0d5c12007-12-17 11:40:18 -08001247 * 2. A timeout of 10 seconds without activity has expired
Paul B Schroeder3f542972006-08-31 19:41:47 -05001248 * (1K of mos7840 data @ 2400 baud ==> 4 sec to empty)
1249 *
1250 ************************************************************************/
1251
Alan Cox95da3102008-07-22 11:09:07 +01001252static void mos7840_block_until_chase_response(struct tty_struct *tty,
1253 struct moschip_port *mos7840_port)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001254{
1255 int timeout = 1 * HZ;
1256 int wait = 10;
1257 int count;
1258
1259 while (1) {
Alan Cox95da3102008-07-22 11:09:07 +01001260 count = mos7840_chars_in_buffer(tty);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001261
1262 /* Check for Buffer status */
Alan Cox880af9d2008-07-22 11:16:12 +01001263 if (count <= 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001264 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001265
1266 /* Block the thread for a while */
1267 interruptible_sleep_on_timeout(&mos7840_port->wait_chase,
1268 timeout);
1269 /* No activity.. count down section */
1270 wait--;
1271 if (wait == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001272 dbg("%s - TIMEOUT", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001273 return;
1274 } else {
Joe Perchesdc0d5c12007-12-17 11:40:18 -08001275 /* Reset timeout value back to seconds */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001276 wait = 10;
1277 }
1278 }
1279
1280}
1281
1282/*****************************************************************************
1283 * mos7840_break
1284 * this function sends a break to the port
1285 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001286static void mos7840_break(struct tty_struct *tty, int break_state)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001287{
Alan Cox95da3102008-07-22 11:09:07 +01001288 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001289 unsigned char data;
1290 struct usb_serial *serial;
1291 struct moschip_port *mos7840_port;
1292
Tony Cook84fe6e72009-04-18 22:55:06 +09301293 dbg("%s", "Entering ...........");
1294 dbg("mos7840_break: Start");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001295
Harvey Harrison441b62c2008-03-03 16:08:34 -08001296 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301297 dbg("%s", "Port Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001298 return;
1299 }
1300
Harvey Harrison441b62c2008-03-03 16:08:34 -08001301 serial = mos7840_get_usb_serial(port, __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001302 if (!serial) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301303 dbg("%s", "Serial Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001304 return;
1305 }
1306
1307 mos7840_port = mos7840_get_port_private(port);
1308
Alan Cox880af9d2008-07-22 11:16:12 +01001309 if (mos7840_port == NULL)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001310 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001311
Alan Cox95da3102008-07-22 11:09:07 +01001312 if (serial->dev)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001313 /* flush and block until tx is empty */
Alan Cox95da3102008-07-22 11:09:07 +01001314 mos7840_block_until_chase_response(tty, mos7840_port);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001315
Alan Cox95da3102008-07-22 11:09:07 +01001316 if (break_state == -1)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001317 data = mos7840_port->shadowLCR | LCR_SET_BREAK;
Alan Cox95da3102008-07-22 11:09:07 +01001318 else
Paul B Schroeder3f542972006-08-31 19:41:47 -05001319 data = mos7840_port->shadowLCR & ~LCR_SET_BREAK;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001320
Alan Cox6b447f042009-01-02 13:48:56 +00001321 /* FIXME: no locking on shadowLCR anywhere in driver */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001322 mos7840_port->shadowLCR = data;
Tony Cook84fe6e72009-04-18 22:55:06 +09301323 dbg("mcs7840_break mos7840_port->shadowLCR is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05001324 mos7840_port->shadowLCR);
1325 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER,
1326 mos7840_port->shadowLCR);
1327
1328 return;
1329}
1330
1331/*****************************************************************************
1332 * mos7840_write_room
1333 * this function is called by the tty driver when it wants to know how many
1334 * bytes of data we can accept for a specific port.
1335 * If successful, we return the amount of room that we have for this port
1336 * Otherwise we return a negative error number.
1337 *****************************************************************************/
1338
Alan Cox95da3102008-07-22 11:09:07 +01001339static int mos7840_write_room(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001340{
Alan Cox95da3102008-07-22 11:09:07 +01001341 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001342 int i;
1343 int room = 0;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001344 unsigned long flags;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001345 struct moschip_port *mos7840_port;
1346
Tony Cook84fe6e72009-04-18 22:55:06 +09301347 dbg("%s", " mos7840_write_room:entering ...........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001348
Harvey Harrison441b62c2008-03-03 16:08:34 -08001349 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301350 dbg("%s", "Invalid port");
1351 dbg("%s", " mos7840_write_room:leaving ...........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001352 return -1;
1353 }
1354
1355 mos7840_port = mos7840_get_port_private(port);
1356 if (mos7840_port == NULL) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301357 dbg("%s", "mos7840_break:leaving ...........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001358 return -1;
1359 }
1360
Oliver Neukum0de9a702007-03-16 20:28:28 +01001361 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001362 for (i = 0; i < NUM_URBS; ++i) {
Alan Cox880af9d2008-07-22 11:16:12 +01001363 if (!mos7840_port->busy[i])
Paul B Schroeder3f542972006-08-31 19:41:47 -05001364 room += URB_TRANSFER_BUFFER_SIZE;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001365 }
Oliver Neukum0de9a702007-03-16 20:28:28 +01001366 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001367
Oliver Neukum0de9a702007-03-16 20:28:28 +01001368 room = (room == 0) ? 0 : room - URB_TRANSFER_BUFFER_SIZE + 1;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001369 dbg("%s - returns %d", __func__, room);
Oliver Neukum0de9a702007-03-16 20:28:28 +01001370 return room;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001371
1372}
1373
1374/*****************************************************************************
1375 * mos7840_write
1376 * this function is called by the tty driver when data should be written to
1377 * the port.
1378 * If successful, we return the number of bytes written, otherwise we
1379 * return a negative error number.
1380 *****************************************************************************/
1381
Alan Cox95da3102008-07-22 11:09:07 +01001382static int mos7840_write(struct tty_struct *tty, struct usb_serial_port *port,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001383 const unsigned char *data, int count)
1384{
1385 int status;
1386 int i;
1387 int bytes_sent = 0;
1388 int transfer_size;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001389 unsigned long flags;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001390
1391 struct moschip_port *mos7840_port;
1392 struct usb_serial *serial;
1393 struct urb *urb;
Alan Cox880af9d2008-07-22 11:16:12 +01001394 /* __u16 Data; */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001395 const unsigned char *current_position = data;
1396 unsigned char *data1;
Tony Cook84fe6e72009-04-18 22:55:06 +09301397 dbg("%s", "entering ...........");
1398 /* dbg("mos7840_write: mos7840_port->shadowLCR is %x",
Alan Cox880af9d2008-07-22 11:16:12 +01001399 mos7840_port->shadowLCR); */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001400
1401#ifdef NOTMOS7840
1402 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001403 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
1404 mos7840_port->shadowLCR = Data;
Tony Cook84fe6e72009-04-18 22:55:06 +09301405 dbg("mos7840_write: LINE_CONTROL_REGISTER is %x", Data);
1406 dbg("mos7840_write: mos7840_port->shadowLCR is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05001407 mos7840_port->shadowLCR);
1408
Alan Cox880af9d2008-07-22 11:16:12 +01001409 /* Data = 0x03; */
1410 /* status = mos7840_set_uart_reg(port,LINE_CONTROL_REGISTER,Data); */
1411 /* mos7840_port->shadowLCR=Data;//Need to add later */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001412
Alan Cox880af9d2008-07-22 11:16:12 +01001413 Data |= SERIAL_LCR_DLAB; /* data latch enable in LCR 0x80 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001414 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1415
Alan Cox880af9d2008-07-22 11:16:12 +01001416 /* Data = 0x0c; */
1417 /* status = mos7840_set_uart_reg(port,DIVISOR_LATCH_LSB,Data); */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001418 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001419 status = mos7840_get_uart_reg(port, DIVISOR_LATCH_LSB, &Data);
Tony Cook84fe6e72009-04-18 22:55:06 +09301420 dbg("mos7840_write:DLL value is %x", Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001421
1422 Data = 0x0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001423 status = mos7840_get_uart_reg(port, DIVISOR_LATCH_MSB, &Data);
Tony Cook84fe6e72009-04-18 22:55:06 +09301424 dbg("mos7840_write:DLM value is %x", Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001425
1426 Data = Data & ~SERIAL_LCR_DLAB;
Tony Cook84fe6e72009-04-18 22:55:06 +09301427 dbg("mos7840_write: mos7840_port->shadowLCR is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05001428 mos7840_port->shadowLCR);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001429 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1430#endif
1431
Harvey Harrison441b62c2008-03-03 16:08:34 -08001432 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301433 dbg("%s", "Port Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001434 return -1;
1435 }
1436
1437 serial = port->serial;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001438 if (mos7840_serial_paranoia_check(serial, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301439 dbg("%s", "Serial Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001440 return -1;
1441 }
1442
1443 mos7840_port = mos7840_get_port_private(port);
1444 if (mos7840_port == NULL) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301445 dbg("%s", "mos7840_port is NULL");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001446 return -1;
1447 }
1448
1449 /* try to find a free urb in the list */
1450 urb = NULL;
1451
Oliver Neukum0de9a702007-03-16 20:28:28 +01001452 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001453 for (i = 0; i < NUM_URBS; ++i) {
Oliver Neukum0de9a702007-03-16 20:28:28 +01001454 if (!mos7840_port->busy[i]) {
1455 mos7840_port->busy[i] = 1;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001456 urb = mos7840_port->write_urb_pool[i];
Tony Cook84fe6e72009-04-18 22:55:06 +09301457 dbg("URB:%d", i);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001458 break;
1459 }
1460 }
Oliver Neukum0de9a702007-03-16 20:28:28 +01001461 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001462
1463 if (urb == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001464 dbg("%s - no more free urbs", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001465 goto exit;
1466 }
1467
1468 if (urb->transfer_buffer == NULL) {
1469 urb->transfer_buffer =
1470 kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
1471
1472 if (urb->transfer_buffer == NULL) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001473 dev_err(&port->dev, "%s no more kernel memory...\n",
1474 __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001475 goto exit;
1476 }
1477 }
1478 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
1479
Al Viro97c49652006-10-09 20:29:03 +01001480 memcpy(urb->transfer_buffer, current_position, transfer_size);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001481
1482 /* fill urb with data and submit */
1483 usb_fill_bulk_urb(urb,
1484 serial->dev,
1485 usb_sndbulkpipe(serial->dev,
1486 port->bulk_out_endpointAddress),
1487 urb->transfer_buffer,
1488 transfer_size,
1489 mos7840_bulk_out_data_callback, mos7840_port);
1490
1491 data1 = urb->transfer_buffer;
Tony Cook84fe6e72009-04-18 22:55:06 +09301492 dbg("bulkout endpoint is %d", port->bulk_out_endpointAddress);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001493
1494 /* send it down the pipe */
1495 status = usb_submit_urb(urb, GFP_ATOMIC);
1496
1497 if (status) {
Oliver Neukum0de9a702007-03-16 20:28:28 +01001498 mos7840_port->busy[i] = 0;
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001499 dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed "
1500 "with status = %d\n", __func__, status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001501 bytes_sent = status;
1502 goto exit;
1503 }
1504 bytes_sent = transfer_size;
1505 mos7840_port->icount.tx += transfer_size;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001506 smp_wmb();
Tony Cook84fe6e72009-04-18 22:55:06 +09301507 dbg("mos7840_port->icount.tx is %d:", mos7840_port->icount.tx);
Alan Cox95da3102008-07-22 11:09:07 +01001508exit:
Paul B Schroeder3f542972006-08-31 19:41:47 -05001509 return bytes_sent;
1510
1511}
1512
1513/*****************************************************************************
1514 * mos7840_throttle
1515 * this function is called by the tty driver when it wants to stop the data
1516 * being read from the port.
1517 *****************************************************************************/
1518
Alan Cox95da3102008-07-22 11:09:07 +01001519static void mos7840_throttle(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001520{
Alan Cox95da3102008-07-22 11:09:07 +01001521 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001522 struct moschip_port *mos7840_port;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001523 int status;
1524
Harvey Harrison441b62c2008-03-03 16:08:34 -08001525 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301526 dbg("%s", "Invalid port");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001527 return;
1528 }
1529
Tony Cook84fe6e72009-04-18 22:55:06 +09301530 dbg("- port %d", port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001531
1532 mos7840_port = mos7840_get_port_private(port);
1533
1534 if (mos7840_port == NULL)
1535 return;
1536
1537 if (!mos7840_port->open) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301538 dbg("%s", "port not opened");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001539 return;
1540 }
1541
Tony Cook84fe6e72009-04-18 22:55:06 +09301542 dbg("%s", "Entering ..........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001543
Paul B Schroeder3f542972006-08-31 19:41:47 -05001544 /* if we are implementing XON/XOFF, send the stop character */
1545 if (I_IXOFF(tty)) {
1546 unsigned char stop_char = STOP_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01001547 status = mos7840_write(tty, port, &stop_char, 1);
1548 if (status <= 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001549 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001550 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001551 /* if we are implementing RTS/CTS, toggle that line */
1552 if (tty->termios->c_cflag & CRTSCTS) {
1553 mos7840_port->shadowMCR &= ~MCR_RTS;
Alan Cox95da3102008-07-22 11:09:07 +01001554 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001555 mos7840_port->shadowMCR);
Alan Cox95da3102008-07-22 11:09:07 +01001556 if (status < 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001557 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001558 }
1559
1560 return;
1561}
1562
1563/*****************************************************************************
1564 * mos7840_unthrottle
Alan Cox880af9d2008-07-22 11:16:12 +01001565 * this function is called by the tty driver when it wants to resume
1566 * the data being read from the port (called after mos7840_throttle is
1567 * called)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001568 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001569static void mos7840_unthrottle(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001570{
Alan Cox95da3102008-07-22 11:09:07 +01001571 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001572 int status;
1573 struct moschip_port *mos7840_port = mos7840_get_port_private(port);
1574
Harvey Harrison441b62c2008-03-03 16:08:34 -08001575 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301576 dbg("%s", "Invalid port");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001577 return;
1578 }
1579
1580 if (mos7840_port == NULL)
1581 return;
1582
1583 if (!mos7840_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001584 dbg("%s - port not opened", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001585 return;
1586 }
1587
Tony Cook84fe6e72009-04-18 22:55:06 +09301588 dbg("%s", "Entering ..........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001589
Paul B Schroeder3f542972006-08-31 19:41:47 -05001590 /* if we are implementing XON/XOFF, send the start character */
1591 if (I_IXOFF(tty)) {
1592 unsigned char start_char = START_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01001593 status = mos7840_write(tty, port, &start_char, 1);
1594 if (status <= 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001595 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001596 }
1597
1598 /* if we are implementing RTS/CTS, toggle that line */
1599 if (tty->termios->c_cflag & CRTSCTS) {
1600 mos7840_port->shadowMCR |= MCR_RTS;
Alan Cox95da3102008-07-22 11:09:07 +01001601 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001602 mos7840_port->shadowMCR);
Alan Cox95da3102008-07-22 11:09:07 +01001603 if (status < 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001604 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001605 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001606}
1607
Alan Cox95da3102008-07-22 11:09:07 +01001608static int mos7840_tiocmget(struct tty_struct *tty, struct file *file)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001609{
Alan Cox95da3102008-07-22 11:09:07 +01001610 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001611 struct moschip_port *mos7840_port;
1612 unsigned int result;
1613 __u16 msr;
1614 __u16 mcr;
Alan Cox880af9d2008-07-22 11:16:12 +01001615 int status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001616 mos7840_port = mos7840_get_port_private(port);
1617
Harvey Harrison441b62c2008-03-03 16:08:34 -08001618 dbg("%s - port %d", __func__, port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001619
1620 if (mos7840_port == NULL)
1621 return -ENODEV;
1622
1623 status = mos7840_get_uart_reg(port, MODEM_STATUS_REGISTER, &msr);
1624 status = mos7840_get_uart_reg(port, MODEM_CONTROL_REGISTER, &mcr);
1625 result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0)
1626 | ((mcr & MCR_RTS) ? TIOCM_RTS : 0)
1627 | ((mcr & MCR_LOOPBACK) ? TIOCM_LOOP : 0)
1628 | ((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0)
1629 | ((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0)
1630 | ((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0)
1631 | ((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0);
1632
Harvey Harrison441b62c2008-03-03 16:08:34 -08001633 dbg("%s - 0x%04X", __func__, result);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001634
1635 return result;
1636}
1637
Alan Cox95da3102008-07-22 11:09:07 +01001638static int mos7840_tiocmset(struct tty_struct *tty, struct file *file,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001639 unsigned int set, unsigned int clear)
1640{
Alan Cox95da3102008-07-22 11:09:07 +01001641 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001642 struct moschip_port *mos7840_port;
1643 unsigned int mcr;
Roel Kluin87521c42008-04-17 06:16:24 +02001644 int status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001645
Harvey Harrison441b62c2008-03-03 16:08:34 -08001646 dbg("%s - port %d", __func__, port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001647
1648 mos7840_port = mos7840_get_port_private(port);
1649
1650 if (mos7840_port == NULL)
1651 return -ENODEV;
1652
Alan Coxe2984492008-02-20 20:51:45 +00001653 /* FIXME: What locks the port registers ? */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001654 mcr = mos7840_port->shadowMCR;
1655 if (clear & TIOCM_RTS)
1656 mcr &= ~MCR_RTS;
1657 if (clear & TIOCM_DTR)
1658 mcr &= ~MCR_DTR;
1659 if (clear & TIOCM_LOOP)
1660 mcr &= ~MCR_LOOPBACK;
1661
1662 if (set & TIOCM_RTS)
1663 mcr |= MCR_RTS;
1664 if (set & TIOCM_DTR)
1665 mcr |= MCR_DTR;
1666 if (set & TIOCM_LOOP)
1667 mcr |= MCR_LOOPBACK;
1668
1669 mos7840_port->shadowMCR = mcr;
1670
Paul B Schroeder3f542972006-08-31 19:41:47 -05001671 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, mcr);
1672 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301673 dbg("setting MODEM_CONTROL_REGISTER Failed");
Roel Kluin87521c42008-04-17 06:16:24 +02001674 return status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001675 }
1676
1677 return 0;
1678}
1679
1680/*****************************************************************************
1681 * mos7840_calc_baud_rate_divisor
1682 * this function calculates the proper baud rate divisor for the specified
1683 * baud rate.
1684 *****************************************************************************/
1685static int mos7840_calc_baud_rate_divisor(int baudRate, int *divisor,
Alan Cox880af9d2008-07-22 11:16:12 +01001686 __u16 *clk_sel_val)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001687{
1688
Harvey Harrison441b62c2008-03-03 16:08:34 -08001689 dbg("%s - %d", __func__, baudRate);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001690
1691 if (baudRate <= 115200) {
1692 *divisor = 115200 / baudRate;
1693 *clk_sel_val = 0x0;
1694 }
1695 if ((baudRate > 115200) && (baudRate <= 230400)) {
1696 *divisor = 230400 / baudRate;
1697 *clk_sel_val = 0x10;
1698 } else if ((baudRate > 230400) && (baudRate <= 403200)) {
1699 *divisor = 403200 / baudRate;
1700 *clk_sel_val = 0x20;
1701 } else if ((baudRate > 403200) && (baudRate <= 460800)) {
1702 *divisor = 460800 / baudRate;
1703 *clk_sel_val = 0x30;
1704 } else if ((baudRate > 460800) && (baudRate <= 806400)) {
1705 *divisor = 806400 / baudRate;
1706 *clk_sel_val = 0x40;
1707 } else if ((baudRate > 806400) && (baudRate <= 921600)) {
1708 *divisor = 921600 / baudRate;
1709 *clk_sel_val = 0x50;
1710 } else if ((baudRate > 921600) && (baudRate <= 1572864)) {
1711 *divisor = 1572864 / baudRate;
1712 *clk_sel_val = 0x60;
1713 } else if ((baudRate > 1572864) && (baudRate <= 3145728)) {
1714 *divisor = 3145728 / baudRate;
1715 *clk_sel_val = 0x70;
1716 }
1717 return 0;
1718
1719#ifdef NOTMCS7840
1720
1721 for (i = 0; i < ARRAY_SIZE(mos7840_divisor_table); i++) {
1722 if (mos7840_divisor_table[i].BaudRate == baudrate) {
1723 *divisor = mos7840_divisor_table[i].Divisor;
1724 return 0;
1725 }
1726 }
1727
1728 /* After trying for all the standard baud rates *
1729 * Try calculating the divisor for this baud rate */
1730
1731 if (baudrate > 75 && baudrate < 230400) {
1732 /* get the divisor */
1733 custom = (__u16) (230400L / baudrate);
1734
1735 /* Check for round off */
1736 round1 = (__u16) (2304000L / baudrate);
1737 round = (__u16) (round1 - (custom * 10));
Alan Cox880af9d2008-07-22 11:16:12 +01001738 if (round > 4)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001739 custom++;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001740 *divisor = custom;
1741
Tony Cook84fe6e72009-04-18 22:55:06 +09301742 dbg(" Baud %d = %d", baudrate, custom);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001743 return 0;
1744 }
1745
Tony Cook84fe6e72009-04-18 22:55:06 +09301746 dbg("%s", " Baud calculation Failed...");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001747 return -1;
1748#endif
1749}
1750
1751/*****************************************************************************
1752 * mos7840_send_cmd_write_baud_rate
1753 * this function sends the proper command to change the baud rate of the
1754 * specified port.
1755 *****************************************************************************/
1756
1757static int mos7840_send_cmd_write_baud_rate(struct moschip_port *mos7840_port,
1758 int baudRate)
1759{
1760 int divisor = 0;
1761 int status;
1762 __u16 Data;
1763 unsigned char number;
1764 __u16 clk_sel_val;
1765 struct usb_serial_port *port;
1766
1767 if (mos7840_port == NULL)
1768 return -1;
1769
1770 port = (struct usb_serial_port *)mos7840_port->port;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001771 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301772 dbg("%s", "Invalid port");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001773 return -1;
1774 }
1775
Harvey Harrison441b62c2008-03-03 16:08:34 -08001776 if (mos7840_serial_paranoia_check(port->serial, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301777 dbg("%s", "Invalid Serial");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001778 return -1;
1779 }
1780
Tony Cook84fe6e72009-04-18 22:55:06 +09301781 dbg("%s", "Entering ..........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001782
1783 number = mos7840_port->port->number - mos7840_port->port->serial->minor;
1784
Harvey Harrison441b62c2008-03-03 16:08:34 -08001785 dbg("%s - port = %d, baud = %d", __func__,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001786 mos7840_port->port->number, baudRate);
Alan Cox880af9d2008-07-22 11:16:12 +01001787 /* reset clk_uart_sel in spregOffset */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001788 if (baudRate > 115200) {
1789#ifdef HW_flow_control
Alan Cox880af9d2008-07-22 11:16:12 +01001790 /* NOTE: need to see the pther register to modify */
1791 /* setting h/w flow control bit to 1 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001792 Data = 0x2b;
1793 mos7840_port->shadowMCR = Data;
Alan Cox880af9d2008-07-22 11:16:12 +01001794 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1795 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001796 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301797 dbg("Writing spreg failed in set_serial_baud");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001798 return -1;
1799 }
1800#endif
1801
1802 } else {
1803#ifdef HW_flow_control
Alan Cox880af9d2008-07-22 11:16:12 +01001804 / *setting h/w flow control bit to 0 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001805 Data = 0xb;
1806 mos7840_port->shadowMCR = Data;
Alan Cox880af9d2008-07-22 11:16:12 +01001807 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1808 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001809 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301810 dbg("Writing spreg failed in set_serial_baud");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001811 return -1;
1812 }
1813#endif
1814
1815 }
1816
Alan Cox880af9d2008-07-22 11:16:12 +01001817 if (1) { /* baudRate <= 115200) */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001818 clk_sel_val = 0x0;
1819 Data = 0x0;
Alan Cox880af9d2008-07-22 11:16:12 +01001820 status = mos7840_calc_baud_rate_divisor(baudRate, &divisor,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001821 &clk_sel_val);
Alan Cox880af9d2008-07-22 11:16:12 +01001822 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset,
1823 &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001824 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301825 dbg("reading spreg failed in set_serial_baud");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001826 return -1;
1827 }
1828 Data = (Data & 0x8f) | clk_sel_val;
Alan Cox880af9d2008-07-22 11:16:12 +01001829 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset,
1830 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001831 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301832 dbg("Writing spreg failed in set_serial_baud");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001833 return -1;
1834 }
1835 /* Calculate the Divisor */
1836
1837 if (status) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001838 dev_err(&port->dev, "%s - bad baud rate\n", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001839 return status;
1840 }
1841 /* Enable access to divisor latch */
1842 Data = mos7840_port->shadowLCR | SERIAL_LCR_DLAB;
1843 mos7840_port->shadowLCR = Data;
1844 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1845
1846 /* Write the divisor */
1847 Data = (unsigned char)(divisor & 0xff);
Tony Cook84fe6e72009-04-18 22:55:06 +09301848 dbg("set_serial_baud Value to write DLL is %x", Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001849 mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
1850
1851 Data = (unsigned char)((divisor & 0xff00) >> 8);
Tony Cook84fe6e72009-04-18 22:55:06 +09301852 dbg("set_serial_baud Value to write DLM is %x", Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001853 mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
1854
1855 /* Disable access to divisor latch */
1856 Data = mos7840_port->shadowLCR & ~SERIAL_LCR_DLAB;
1857 mos7840_port->shadowLCR = Data;
1858 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1859
1860 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001861 return status;
1862}
1863
1864/*****************************************************************************
1865 * mos7840_change_port_settings
1866 * This routine is called to set the UART on the device to match
1867 * the specified new settings.
1868 *****************************************************************************/
1869
Alan Cox95da3102008-07-22 11:09:07 +01001870static void mos7840_change_port_settings(struct tty_struct *tty,
1871 struct moschip_port *mos7840_port, struct ktermios *old_termios)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001872{
Paul B Schroeder3f542972006-08-31 19:41:47 -05001873 int baud;
1874 unsigned cflag;
1875 unsigned iflag;
1876 __u8 lData;
1877 __u8 lParity;
1878 __u8 lStop;
1879 int status;
1880 __u16 Data;
1881 struct usb_serial_port *port;
1882 struct usb_serial *serial;
1883
1884 if (mos7840_port == NULL)
1885 return;
1886
1887 port = (struct usb_serial_port *)mos7840_port->port;
1888
Harvey Harrison441b62c2008-03-03 16:08:34 -08001889 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301890 dbg("%s", "Invalid port");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001891 return;
1892 }
1893
Harvey Harrison441b62c2008-03-03 16:08:34 -08001894 if (mos7840_serial_paranoia_check(port->serial, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301895 dbg("%s", "Invalid Serial");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001896 return;
1897 }
1898
1899 serial = port->serial;
1900
Harvey Harrison441b62c2008-03-03 16:08:34 -08001901 dbg("%s - port %d", __func__, mos7840_port->port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001902
1903 if (!mos7840_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001904 dbg("%s - port not opened", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001905 return;
1906 }
1907
Tony Cook84fe6e72009-04-18 22:55:06 +09301908 dbg("%s", "Entering ..........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001909
1910 lData = LCR_BITS_8;
1911 lStop = LCR_STOP_1;
1912 lParity = LCR_PAR_NONE;
1913
1914 cflag = tty->termios->c_cflag;
1915 iflag = tty->termios->c_iflag;
1916
1917 /* Change the number of bits */
1918 if (cflag & CSIZE) {
1919 switch (cflag & CSIZE) {
1920 case CS5:
1921 lData = LCR_BITS_5;
1922 break;
1923
1924 case CS6:
1925 lData = LCR_BITS_6;
1926 break;
1927
1928 case CS7:
1929 lData = LCR_BITS_7;
1930 break;
1931 default:
1932 case CS8:
1933 lData = LCR_BITS_8;
1934 break;
1935 }
1936 }
1937 /* Change the Parity bit */
1938 if (cflag & PARENB) {
1939 if (cflag & PARODD) {
1940 lParity = LCR_PAR_ODD;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001941 dbg("%s - parity = odd", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001942 } else {
1943 lParity = LCR_PAR_EVEN;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001944 dbg("%s - parity = even", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001945 }
1946
1947 } else {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001948 dbg("%s - parity = none", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001949 }
1950
Alan Cox880af9d2008-07-22 11:16:12 +01001951 if (cflag & CMSPAR)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001952 lParity = lParity | 0x20;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001953
1954 /* Change the Stop bit */
1955 if (cflag & CSTOPB) {
1956 lStop = LCR_STOP_2;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001957 dbg("%s - stop bits = 2", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001958 } else {
1959 lStop = LCR_STOP_1;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001960 dbg("%s - stop bits = 1", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001961 }
1962
1963 /* Update the LCR with the correct value */
1964 mos7840_port->shadowLCR &=
1965 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
1966 mos7840_port->shadowLCR |= (lData | lParity | lStop);
1967
Tony Cook84fe6e72009-04-18 22:55:06 +09301968 dbg("mos7840_change_port_settings mos7840_port->shadowLCR is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05001969 mos7840_port->shadowLCR);
1970 /* Disable Interrupts */
1971 Data = 0x00;
1972 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1973
1974 Data = 0x00;
1975 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
1976
1977 Data = 0xcf;
1978 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
1979
1980 /* Send the updated LCR value to the mos7840 */
1981 Data = mos7840_port->shadowLCR;
1982
1983 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1984
1985 Data = 0x00b;
1986 mos7840_port->shadowMCR = Data;
1987 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1988 Data = 0x00b;
1989 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1990
1991 /* set up the MCR register and send it to the mos7840 */
1992
1993 mos7840_port->shadowMCR = MCR_MASTER_IE;
Alan Cox880af9d2008-07-22 11:16:12 +01001994 if (cflag & CBAUD)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001995 mos7840_port->shadowMCR |= (MCR_DTR | MCR_RTS);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001996
Alan Cox880af9d2008-07-22 11:16:12 +01001997 if (cflag & CRTSCTS)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001998 mos7840_port->shadowMCR |= (MCR_XON_ANY);
Alan Cox880af9d2008-07-22 11:16:12 +01001999 else
Paul B Schroeder3f542972006-08-31 19:41:47 -05002000 mos7840_port->shadowMCR &= ~(MCR_XON_ANY);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002001
2002 Data = mos7840_port->shadowMCR;
2003 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2004
2005 /* Determine divisor based on baud rate */
2006 baud = tty_get_baud_rate(tty);
2007
2008 if (!baud) {
2009 /* pick a default, any default... */
Tony Cook84fe6e72009-04-18 22:55:06 +09302010 dbg("%s", "Picked default baud...");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002011 baud = 9600;
2012 }
2013
Harvey Harrison441b62c2008-03-03 16:08:34 -08002014 dbg("%s - baud rate = %d", __func__, baud);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002015 status = mos7840_send_cmd_write_baud_rate(mos7840_port, baud);
2016
2017 /* Enable Interrupts */
2018 Data = 0x0c;
2019 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
2020
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002021 if (mos7840_port->read_urb_busy == false) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002022 mos7840_port->read_urb->dev = serial->dev;
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002023 mos7840_port->read_urb_busy = true;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002024 status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002025 if (status) {
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002026 dbg("usb_submit_urb(read bulk) failed, status = %d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002027 status);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002028 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002029 }
2030 }
2031 wake_up(&mos7840_port->delta_msr_wait);
2032 mos7840_port->delta_msr_cond = 1;
Tony Cook84fe6e72009-04-18 22:55:06 +09302033 dbg("mos7840_change_port_settings mos7840_port->shadowLCR is End %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002034 mos7840_port->shadowLCR);
2035
2036 return;
2037}
2038
2039/*****************************************************************************
2040 * mos7840_set_termios
2041 * this function is called by the tty driver when it wants to change
2042 * the termios structure
2043 *****************************************************************************/
2044
Alan Cox95da3102008-07-22 11:09:07 +01002045static void mos7840_set_termios(struct tty_struct *tty,
2046 struct usb_serial_port *port,
Alan Cox606d0992006-12-08 02:38:45 -08002047 struct ktermios *old_termios)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002048{
2049 int status;
2050 unsigned int cflag;
2051 struct usb_serial *serial;
2052 struct moschip_port *mos7840_port;
Tony Cook84fe6e72009-04-18 22:55:06 +09302053 dbg("mos7840_set_termios: START");
Harvey Harrison441b62c2008-03-03 16:08:34 -08002054 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302055 dbg("%s", "Invalid port");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002056 return;
2057 }
2058
2059 serial = port->serial;
2060
Harvey Harrison441b62c2008-03-03 16:08:34 -08002061 if (mos7840_serial_paranoia_check(serial, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302062 dbg("%s", "Invalid Serial");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002063 return;
2064 }
2065
2066 mos7840_port = mos7840_get_port_private(port);
2067
2068 if (mos7840_port == NULL)
2069 return;
2070
Paul B Schroeder3f542972006-08-31 19:41:47 -05002071 if (!mos7840_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002072 dbg("%s - port not opened", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002073 return;
2074 }
2075
Tony Cook84fe6e72009-04-18 22:55:06 +09302076 dbg("%s", "setting termios - ");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002077
2078 cflag = tty->termios->c_cflag;
2079
Harvey Harrison441b62c2008-03-03 16:08:34 -08002080 dbg("%s - clfag %08x iflag %08x", __func__,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002081 tty->termios->c_cflag, RELEVANT_IFLAG(tty->termios->c_iflag));
Harvey Harrison441b62c2008-03-03 16:08:34 -08002082 dbg("%s - old clfag %08x old iflag %08x", __func__,
Alan Cox3d3ddce2007-10-15 20:53:35 +01002083 old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag));
Harvey Harrison441b62c2008-03-03 16:08:34 -08002084 dbg("%s - port %d", __func__, port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002085
2086 /* change the port settings to the new ones specified */
2087
Alan Cox95da3102008-07-22 11:09:07 +01002088 mos7840_change_port_settings(tty, mos7840_port, old_termios);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002089
2090 if (!mos7840_port->read_urb) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302091 dbg("%s", "URB KILLED !!!!!");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002092 return;
2093 }
2094
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002095 if (mos7840_port->read_urb_busy == false) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002096 mos7840_port->read_urb->dev = serial->dev;
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002097 mos7840_port->read_urb_busy = true;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002098 status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
2099 if (status) {
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002100 dbg("usb_submit_urb(read bulk) failed, status = %d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002101 status);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002102 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002103 }
2104 }
2105 return;
2106}
2107
2108/*****************************************************************************
2109 * mos7840_get_lsr_info - get line status register info
2110 *
2111 * Purpose: Let user call ioctl() to get info when the UART physically
2112 * is emptied. On bus types like RS485, the transmitter must
2113 * release the bus after transmitting. This must be done when
2114 * the transmit shift register is empty, not be done when the
2115 * transmit holding register is empty. This functionality
2116 * allows an RS485 driver to be written in user space.
2117 *****************************************************************************/
2118
Alan Cox95da3102008-07-22 11:09:07 +01002119static int mos7840_get_lsr_info(struct tty_struct *tty,
Al Viro97c49652006-10-09 20:29:03 +01002120 unsigned int __user *value)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002121{
2122 int count;
2123 unsigned int result = 0;
2124
Alan Cox95da3102008-07-22 11:09:07 +01002125 count = mos7840_chars_in_buffer(tty);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002126 if (count == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002127 dbg("%s -- Empty", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002128 result = TIOCSER_TEMT;
2129 }
2130
2131 if (copy_to_user(value, &result, sizeof(int)))
2132 return -EFAULT;
2133 return 0;
2134}
2135
2136/*****************************************************************************
Paul B Schroeder3f542972006-08-31 19:41:47 -05002137 * mos7840_set_modem_info
2138 * function to set modem info
2139 *****************************************************************************/
2140
Alan Cox95da3102008-07-22 11:09:07 +01002141/* FIXME: Should be using the model control hooks */
2142
Paul B Schroeder3f542972006-08-31 19:41:47 -05002143static int mos7840_set_modem_info(struct moschip_port *mos7840_port,
Al Viro97c49652006-10-09 20:29:03 +01002144 unsigned int cmd, unsigned int __user *value)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002145{
2146 unsigned int mcr;
2147 unsigned int arg;
2148 __u16 Data;
2149 int status;
2150 struct usb_serial_port *port;
2151
2152 if (mos7840_port == NULL)
2153 return -1;
2154
2155 port = (struct usb_serial_port *)mos7840_port->port;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002156 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302157 dbg("%s", "Invalid port");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002158 return -1;
2159 }
2160
2161 mcr = mos7840_port->shadowMCR;
2162
2163 if (copy_from_user(&arg, value, sizeof(int)))
2164 return -EFAULT;
2165
2166 switch (cmd) {
2167 case TIOCMBIS:
2168 if (arg & TIOCM_RTS)
2169 mcr |= MCR_RTS;
2170 if (arg & TIOCM_DTR)
2171 mcr |= MCR_RTS;
2172 if (arg & TIOCM_LOOP)
2173 mcr |= MCR_LOOPBACK;
2174 break;
2175
2176 case TIOCMBIC:
2177 if (arg & TIOCM_RTS)
2178 mcr &= ~MCR_RTS;
2179 if (arg & TIOCM_DTR)
2180 mcr &= ~MCR_RTS;
2181 if (arg & TIOCM_LOOP)
2182 mcr &= ~MCR_LOOPBACK;
2183 break;
2184
2185 case TIOCMSET:
2186 /* turn off the RTS and DTR and LOOPBACK
2187 * and then only turn on what was asked to */
2188 mcr &= ~(MCR_RTS | MCR_DTR | MCR_LOOPBACK);
2189 mcr |= ((arg & TIOCM_RTS) ? MCR_RTS : 0);
2190 mcr |= ((arg & TIOCM_DTR) ? MCR_DTR : 0);
2191 mcr |= ((arg & TIOCM_LOOP) ? MCR_LOOPBACK : 0);
2192 break;
2193 }
2194
Alan Cox6b447f042009-01-02 13:48:56 +00002195 lock_kernel();
Paul B Schroeder3f542972006-08-31 19:41:47 -05002196 mos7840_port->shadowMCR = mcr;
2197
2198 Data = mos7840_port->shadowMCR;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002199 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
Alan Cox6b447f042009-01-02 13:48:56 +00002200 unlock_kernel();
Paul B Schroeder3f542972006-08-31 19:41:47 -05002201 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302202 dbg("setting MODEM_CONTROL_REGISTER Failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002203 return -1;
2204 }
2205
2206 return 0;
2207}
2208
2209/*****************************************************************************
2210 * mos7840_get_modem_info
2211 * function to get modem info
2212 *****************************************************************************/
2213
2214static int mos7840_get_modem_info(struct moschip_port *mos7840_port,
Al Viro97c49652006-10-09 20:29:03 +01002215 unsigned int __user *value)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002216{
2217 unsigned int result = 0;
2218 __u16 msr;
2219 unsigned int mcr = mos7840_port->shadowMCR;
Alan Cox880af9d2008-07-22 11:16:12 +01002220 mos7840_get_uart_reg(mos7840_port->port,
Alan Cox95da3102008-07-22 11:09:07 +01002221 MODEM_STATUS_REGISTER, &msr);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002222 result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0) /* 0x002 */
2223 |((mcr & MCR_RTS) ? TIOCM_RTS : 0) /* 0x004 */
2224 |((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0) /* 0x020 */
2225 |((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0) /* 0x040 */
2226 |((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0) /* 0x080 */
2227 |((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0); /* 0x100 */
2228
Harvey Harrison441b62c2008-03-03 16:08:34 -08002229 dbg("%s -- %x", __func__, result);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002230
2231 if (copy_to_user(value, &result, sizeof(int)))
2232 return -EFAULT;
2233 return 0;
2234}
2235
2236/*****************************************************************************
2237 * mos7840_get_serial_info
2238 * function to get information about serial port
2239 *****************************************************************************/
2240
2241static int mos7840_get_serial_info(struct moschip_port *mos7840_port,
Al Viro97c49652006-10-09 20:29:03 +01002242 struct serial_struct __user *retinfo)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002243{
2244 struct serial_struct tmp;
2245
2246 if (mos7840_port == NULL)
2247 return -1;
2248
2249 if (!retinfo)
2250 return -EFAULT;
2251
2252 memset(&tmp, 0, sizeof(tmp));
2253
2254 tmp.type = PORT_16550A;
2255 tmp.line = mos7840_port->port->serial->minor;
2256 tmp.port = mos7840_port->port->number;
2257 tmp.irq = 0;
2258 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
2259 tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
2260 tmp.baud_base = 9600;
2261 tmp.close_delay = 5 * HZ;
2262 tmp.closing_wait = 30 * HZ;
2263
2264 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2265 return -EFAULT;
2266 return 0;
2267}
2268
2269/*****************************************************************************
2270 * SerialIoctl
2271 * this function handles any ioctl calls to the driver
2272 *****************************************************************************/
2273
Alan Cox95da3102008-07-22 11:09:07 +01002274static int mos7840_ioctl(struct tty_struct *tty, struct file *file,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002275 unsigned int cmd, unsigned long arg)
2276{
Alan Cox95da3102008-07-22 11:09:07 +01002277 struct usb_serial_port *port = tty->driver_data;
Al Viro97c49652006-10-09 20:29:03 +01002278 void __user *argp = (void __user *)arg;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002279 struct moschip_port *mos7840_port;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002280
2281 struct async_icount cnow;
2282 struct async_icount cprev;
2283 struct serial_icounter_struct icount;
2284 int mosret = 0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002285
Harvey Harrison441b62c2008-03-03 16:08:34 -08002286 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302287 dbg("%s", "Invalid port");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002288 return -1;
2289 }
2290
2291 mos7840_port = mos7840_get_port_private(port);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002292
2293 if (mos7840_port == NULL)
2294 return -1;
2295
Harvey Harrison441b62c2008-03-03 16:08:34 -08002296 dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002297
2298 switch (cmd) {
2299 /* return number of bytes available */
2300
Paul B Schroeder3f542972006-08-31 19:41:47 -05002301 case TIOCSERGETLSR:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002302 dbg("%s (%d) TIOCSERGETLSR", __func__, port->number);
Alan Cox95da3102008-07-22 11:09:07 +01002303 return mos7840_get_lsr_info(tty, argp);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002304 return 0;
2305
Alan Cox95da3102008-07-22 11:09:07 +01002306 /* FIXME: use the modem hooks and remove this */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002307 case TIOCMBIS:
2308 case TIOCMBIC:
2309 case TIOCMSET:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002310 dbg("%s (%d) TIOCMSET/TIOCMBIC/TIOCMSET", __func__,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002311 port->number);
2312 mosret =
Al Viro97c49652006-10-09 20:29:03 +01002313 mos7840_set_modem_info(mos7840_port, cmd, argp);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002314 return mosret;
2315
2316 case TIOCMGET:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002317 dbg("%s (%d) TIOCMGET", __func__, port->number);
Al Viro97c49652006-10-09 20:29:03 +01002318 return mos7840_get_modem_info(mos7840_port, argp);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002319
2320 case TIOCGSERIAL:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002321 dbg("%s (%d) TIOCGSERIAL", __func__, port->number);
Al Viro97c49652006-10-09 20:29:03 +01002322 return mos7840_get_serial_info(mos7840_port, argp);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002323
2324 case TIOCSSERIAL:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002325 dbg("%s (%d) TIOCSSERIAL", __func__, port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002326 break;
2327
2328 case TIOCMIWAIT:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002329 dbg("%s (%d) TIOCMIWAIT", __func__, port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002330 cprev = mos7840_port->icount;
2331 while (1) {
Alan Cox880af9d2008-07-22 11:16:12 +01002332 /* interruptible_sleep_on(&mos7840_port->delta_msr_wait); */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002333 mos7840_port->delta_msr_cond = 0;
2334 wait_event_interruptible(mos7840_port->delta_msr_wait,
2335 (mos7840_port->
2336 delta_msr_cond == 1));
2337
2338 /* see if a signal did it */
2339 if (signal_pending(current))
2340 return -ERESTARTSYS;
2341 cnow = mos7840_port->icount;
Oliver Neukum0de9a702007-03-16 20:28:28 +01002342 smp_rmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -05002343 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2344 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
2345 return -EIO; /* no change => error */
2346 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
2347 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
2348 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
2349 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
2350 return 0;
2351 }
2352 cprev = cnow;
2353 }
2354 /* NOTREACHED */
2355 break;
2356
2357 case TIOCGICOUNT:
2358 cnow = mos7840_port->icount;
Oliver Neukum0de9a702007-03-16 20:28:28 +01002359 smp_rmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -05002360 icount.cts = cnow.cts;
2361 icount.dsr = cnow.dsr;
2362 icount.rng = cnow.rng;
2363 icount.dcd = cnow.dcd;
2364 icount.rx = cnow.rx;
2365 icount.tx = cnow.tx;
2366 icount.frame = cnow.frame;
2367 icount.overrun = cnow.overrun;
2368 icount.parity = cnow.parity;
2369 icount.brk = cnow.brk;
2370 icount.buf_overrun = cnow.buf_overrun;
2371
Harvey Harrison441b62c2008-03-03 16:08:34 -08002372 dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __func__,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002373 port->number, icount.rx, icount.tx);
Al Viro97c49652006-10-09 20:29:03 +01002374 if (copy_to_user(argp, &icount, sizeof(icount)))
Paul B Schroeder3f542972006-08-31 19:41:47 -05002375 return -EFAULT;
2376 return 0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002377 default:
2378 break;
2379 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002380 return -ENOIOCTLCMD;
2381}
2382
2383static int mos7840_calc_num_ports(struct usb_serial *serial)
2384{
Oliver Neukum0de9a702007-03-16 20:28:28 +01002385 int mos7840_num_ports = 0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002386
Tony Cook84fe6e72009-04-18 22:55:06 +09302387 dbg("numberofendpoints: cur %d, alt %d",
2388 (int)serial->interface->cur_altsetting->desc.bNumEndpoints,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002389 (int)serial->interface->altsetting->desc.bNumEndpoints);
2390 if (serial->interface->cur_altsetting->desc.bNumEndpoints == 5) {
Oliver Neukum0de9a702007-03-16 20:28:28 +01002391 mos7840_num_ports = serial->num_ports = 2;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002392 } else if (serial->interface->cur_altsetting->desc.bNumEndpoints == 9) {
Oliver Neukum0de9a702007-03-16 20:28:28 +01002393 serial->num_bulk_in = 4;
2394 serial->num_bulk_out = 4;
2395 mos7840_num_ports = serial->num_ports = 4;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002396 }
Tony Cook84fe6e72009-04-18 22:55:06 +09302397 dbg ("mos7840_num_ports = %d", mos7840_num_ports);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002398 return mos7840_num_ports;
2399}
2400
2401/****************************************************************************
2402 * mos7840_startup
2403 ****************************************************************************/
2404
2405static int mos7840_startup(struct usb_serial *serial)
2406{
2407 struct moschip_port *mos7840_port;
2408 struct usb_device *dev;
2409 int i, status;
2410
2411 __u16 Data;
Tony Cook84fe6e72009-04-18 22:55:06 +09302412 dbg("%s", "mos7840_startup :Entering..........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002413
2414 if (!serial) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302415 dbg("%s", "Invalid Handler");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002416 return -1;
2417 }
2418
2419 dev = serial->dev;
2420
Tony Cook84fe6e72009-04-18 22:55:06 +09302421 dbg("%s", "Entering...");
2422 dbg ("mos7840_startup: serial = %p", serial);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002423
2424 /* we set up the pointers to the endpoints in the mos7840_open *
2425 * function, as the structures aren't created yet. */
2426
2427 /* set up port private structures */
2428 for (i = 0; i < serial->num_ports; ++i) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302429 dbg ("mos7840_startup: configuring port %d............", i);
Burman Yan7ac9da12006-11-22 20:54:38 +02002430 mos7840_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002431 if (mos7840_port == NULL) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07002432 dev_err(&dev->dev, "%s - Out of memory\n", __func__);
Oliver Neukum0de9a702007-03-16 20:28:28 +01002433 status = -ENOMEM;
2434 i--; /* don't follow NULL pointer cleaning up */
2435 goto error;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002436 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002437
Alan Cox880af9d2008-07-22 11:16:12 +01002438 /* Initialize all port interrupt end point to port 0 int
2439 * endpoint. Our device has only one interrupt end point
2440 * common to all port */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002441
2442 mos7840_port->port = serial->port[i];
2443 mos7840_set_port_private(serial->port[i], mos7840_port);
Oliver Neukum0de9a702007-03-16 20:28:28 +01002444 spin_lock_init(&mos7840_port->pool_lock);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002445
Tony Cook37768ad2009-04-18 22:42:18 +09302446 /* minor is not initialised until later by
2447 * usb-serial.c:get_free_serial() and cannot therefore be used
2448 * to index device instances */
2449 mos7840_port->port_num = i + 1;
2450 dbg ("serial->port[i]->number = %d", serial->port[i]->number);
2451 dbg ("serial->port[i]->serial->minor = %d", serial->port[i]->serial->minor);
2452 dbg ("mos7840_port->port_num = %d", mos7840_port->port_num);
2453 dbg ("serial->minor = %d", serial->minor);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002454
2455 if (mos7840_port->port_num == 1) {
2456 mos7840_port->SpRegOffset = 0x0;
2457 mos7840_port->ControlRegOffset = 0x1;
2458 mos7840_port->DcrRegOffset = 0x4;
2459 } else if ((mos7840_port->port_num == 2)
Oliver Neukum0de9a702007-03-16 20:28:28 +01002460 && (serial->num_ports == 4)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002461 mos7840_port->SpRegOffset = 0x8;
2462 mos7840_port->ControlRegOffset = 0x9;
2463 mos7840_port->DcrRegOffset = 0x16;
2464 } else if ((mos7840_port->port_num == 2)
Oliver Neukum0de9a702007-03-16 20:28:28 +01002465 && (serial->num_ports == 2)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002466 mos7840_port->SpRegOffset = 0xa;
2467 mos7840_port->ControlRegOffset = 0xb;
2468 mos7840_port->DcrRegOffset = 0x19;
2469 } else if ((mos7840_port->port_num == 3)
Oliver Neukum0de9a702007-03-16 20:28:28 +01002470 && (serial->num_ports == 4)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002471 mos7840_port->SpRegOffset = 0xa;
2472 mos7840_port->ControlRegOffset = 0xb;
2473 mos7840_port->DcrRegOffset = 0x19;
2474 } else if ((mos7840_port->port_num == 4)
Oliver Neukum0de9a702007-03-16 20:28:28 +01002475 && (serial->num_ports == 4)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002476 mos7840_port->SpRegOffset = 0xc;
2477 mos7840_port->ControlRegOffset = 0xd;
2478 mos7840_port->DcrRegOffset = 0x1c;
2479 }
2480 mos7840_dump_serial_port(mos7840_port);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002481 mos7840_set_port_private(serial->port[i], mos7840_port);
2482
Alan Cox880af9d2008-07-22 11:16:12 +01002483 /* enable rx_disable bit in control register */
2484 status = mos7840_get_reg_sync(serial->port[i],
2485 mos7840_port->ControlRegOffset, &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002486 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302487 dbg("Reading ControlReg failed status-0x%x", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002488 break;
2489 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302490 dbg("ControlReg Reading success val is %x, status%d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002491 Data, status);
Alan Cox880af9d2008-07-22 11:16:12 +01002492 Data |= 0x08; /* setting driver done bit */
2493 Data |= 0x04; /* sp1_bit to have cts change reflect in
2494 modem status reg */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002495
Alan Cox880af9d2008-07-22 11:16:12 +01002496 /* Data |= 0x20; //rx_disable bit */
2497 status = mos7840_set_reg_sync(serial->port[i],
Paul B Schroeder3f542972006-08-31 19:41:47 -05002498 mos7840_port->ControlRegOffset, Data);
2499 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302500 dbg("Writing ControlReg failed(rx_disable) status-0x%x", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002501 break;
2502 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302503 dbg("ControlReg Writing success(rx_disable) status%d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002504 status);
2505
Alan Cox880af9d2008-07-22 11:16:12 +01002506 /* Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2
2507 and 0x24 in DCR3 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002508 Data = 0x01;
Alan Cox880af9d2008-07-22 11:16:12 +01002509 status = mos7840_set_reg_sync(serial->port[i],
2510 (__u16) (mos7840_port->DcrRegOffset + 0), Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002511 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302512 dbg("Writing DCR0 failed status-0x%x", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002513 break;
2514 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302515 dbg("DCR0 Writing success status%d", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002516
2517 Data = 0x05;
Alan Cox880af9d2008-07-22 11:16:12 +01002518 status = mos7840_set_reg_sync(serial->port[i],
2519 (__u16) (mos7840_port->DcrRegOffset + 1), Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002520 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302521 dbg("Writing DCR1 failed status-0x%x", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002522 break;
2523 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302524 dbg("DCR1 Writing success status%d", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002525
2526 Data = 0x24;
Alan Cox880af9d2008-07-22 11:16:12 +01002527 status = mos7840_set_reg_sync(serial->port[i],
2528 (__u16) (mos7840_port->DcrRegOffset + 2), Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002529 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302530 dbg("Writing DCR2 failed status-0x%x", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002531 break;
2532 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302533 dbg("DCR2 Writing success status%d", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002534
Alan Cox880af9d2008-07-22 11:16:12 +01002535 /* write values in clkstart0x0 and clkmulti 0x20 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002536 Data = 0x0;
Alan Cox880af9d2008-07-22 11:16:12 +01002537 status = mos7840_set_reg_sync(serial->port[i],
Paul B Schroeder3f542972006-08-31 19:41:47 -05002538 CLK_START_VALUE_REGISTER, Data);
2539 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302540 dbg("Writing CLK_START_VALUE_REGISTER failed status-0x%x", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002541 break;
2542 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302543 dbg("CLK_START_VALUE_REGISTER Writing success status%d", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002544
2545 Data = 0x20;
Alan Cox880af9d2008-07-22 11:16:12 +01002546 status = mos7840_set_reg_sync(serial->port[i],
2547 CLK_MULTI_REGISTER, Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002548 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302549 dbg("Writing CLK_MULTI_REGISTER failed status-0x%x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002550 status);
Oliver Neukum0de9a702007-03-16 20:28:28 +01002551 goto error;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002552 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302553 dbg("CLK_MULTI_REGISTER Writing success status%d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002554 status);
2555
Alan Cox880af9d2008-07-22 11:16:12 +01002556 /* write value 0x0 to scratchpad register */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002557 Data = 0x00;
Alan Cox880af9d2008-07-22 11:16:12 +01002558 status = mos7840_set_uart_reg(serial->port[i],
2559 SCRATCH_PAD_REGISTER, Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002560 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302561 dbg("Writing SCRATCH_PAD_REGISTER failed status-0x%x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002562 status);
2563 break;
2564 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302565 dbg("SCRATCH_PAD_REGISTER Writing success status%d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002566 status);
2567
Alan Cox880af9d2008-07-22 11:16:12 +01002568 /* Zero Length flag register */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002569 if ((mos7840_port->port_num != 1)
Oliver Neukum0de9a702007-03-16 20:28:28 +01002570 && (serial->num_ports == 2)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002571
2572 Data = 0xff;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002573 status = mos7840_set_reg_sync(serial->port[i],
Alan Cox880af9d2008-07-22 11:16:12 +01002574 (__u16) (ZLP_REG1 +
2575 ((__u16)mos7840_port->port_num)), Data);
Tony Cook84fe6e72009-04-18 22:55:06 +09302576 dbg("ZLIP offset %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002577 (__u16) (ZLP_REG1 +
Alan Cox880af9d2008-07-22 11:16:12 +01002578 ((__u16) mos7840_port->port_num)));
Paul B Schroeder3f542972006-08-31 19:41:47 -05002579 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302580 dbg("Writing ZLP_REG%d failed status-0x%x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002581 i + 2, status);
2582 break;
2583 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302584 dbg("ZLP_REG%d Writing success status%d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002585 i + 2, status);
2586 } else {
2587 Data = 0xff;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002588 status = mos7840_set_reg_sync(serial->port[i],
Alan Cox880af9d2008-07-22 11:16:12 +01002589 (__u16) (ZLP_REG1 +
2590 ((__u16)mos7840_port->port_num) - 0x1), Data);
Tony Cook84fe6e72009-04-18 22:55:06 +09302591 dbg("ZLIP offset %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002592 (__u16) (ZLP_REG1 +
2593 ((__u16) mos7840_port->port_num) - 0x1));
2594 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302595 dbg("Writing ZLP_REG%d failed status-0x%x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002596 i + 1, status);
2597 break;
2598 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302599 dbg("ZLP_REG%d Writing success status%d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002600 i + 1, status);
2601
2602 }
Oliver Neukum0de9a702007-03-16 20:28:28 +01002603 mos7840_port->control_urb = usb_alloc_urb(0, GFP_KERNEL);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002604 mos7840_port->ctrl_buf = kmalloc(16, GFP_KERNEL);
Alan Cox880af9d2008-07-22 11:16:12 +01002605 mos7840_port->dr = kmalloc(sizeof(struct usb_ctrlrequest),
2606 GFP_KERNEL);
2607 if (!mos7840_port->control_urb || !mos7840_port->ctrl_buf ||
2608 !mos7840_port->dr) {
Oliver Neukum0de9a702007-03-16 20:28:28 +01002609 status = -ENOMEM;
2610 goto error;
2611 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002612 }
Tony Cook84fe6e72009-04-18 22:55:06 +09302613 dbg ("mos7840_startup: all ports configured...........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002614
Alan Cox880af9d2008-07-22 11:16:12 +01002615 /* Zero Length flag enable */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002616 Data = 0x0f;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002617 status = mos7840_set_reg_sync(serial->port[0], ZLP_REG5, Data);
2618 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302619 dbg("Writing ZLP_REG5 failed status-0x%x", status);
Roel Kluin7ced46c2007-10-27 03:36:37 +02002620 goto error;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002621 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302622 dbg("ZLP_REG5 Writing success status%d", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002623
2624 /* setting configuration feature to one */
2625 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
Al Viro97c49652006-10-09 20:29:03 +01002626 (__u8) 0x03, 0x00, 0x01, 0x00, NULL, 0x00, 5 * HZ);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002627 return 0;
Oliver Neukum0de9a702007-03-16 20:28:28 +01002628error:
2629 for (/* nothing */; i >= 0; i--) {
2630 mos7840_port = mos7840_get_port_private(serial->port[i]);
2631
2632 kfree(mos7840_port->dr);
2633 kfree(mos7840_port->ctrl_buf);
2634 usb_free_urb(mos7840_port->control_urb);
2635 kfree(mos7840_port);
2636 serial->port[i] = NULL;
2637 }
2638 return status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002639}
2640
2641/****************************************************************************
Alan Sternf9c99bb2009-06-02 11:53:55 -04002642 * mos7840_disconnect
Paul B Schroeder3f542972006-08-31 19:41:47 -05002643 * This function is called whenever the device is removed from the usb bus.
2644 ****************************************************************************/
2645
Alan Sternf9c99bb2009-06-02 11:53:55 -04002646static void mos7840_disconnect(struct usb_serial *serial)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002647{
2648 int i;
Oliver Neukum0de9a702007-03-16 20:28:28 +01002649 unsigned long flags;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002650 struct moschip_port *mos7840_port;
Alan Sternf9c99bb2009-06-02 11:53:55 -04002651 dbg("%s", " disconnect :entering..........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002652
2653 if (!serial) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302654 dbg("%s", "Invalid Handler");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002655 return;
2656 }
2657
Alan Cox880af9d2008-07-22 11:16:12 +01002658 /* check for the ports to be closed,close the ports and disconnect */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002659
2660 /* free private structure allocated for serial port *
2661 * stop reads and writes on all ports */
2662
2663 for (i = 0; i < serial->num_ports; ++i) {
2664 mos7840_port = mos7840_get_port_private(serial->port[i]);
Tony Cook37768ad2009-04-18 22:42:18 +09302665 dbg ("mos7840_port %d = %p", i, mos7840_port);
2666 if (mos7840_port) {
2667 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
2668 mos7840_port->zombie = 1;
2669 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
2670 usb_kill_urb(mos7840_port->control_urb);
Alan Sternf9c99bb2009-06-02 11:53:55 -04002671 }
2672 }
2673
2674 dbg("%s", "Thank u :: ");
2675
2676}
2677
2678/****************************************************************************
2679 * mos7840_release
2680 * This function is called when the usb_serial structure is freed.
2681 ****************************************************************************/
2682
2683static void mos7840_release(struct usb_serial *serial)
2684{
2685 int i;
2686 struct moschip_port *mos7840_port;
2687 dbg("%s", " release :entering..........");
2688
2689 if (!serial) {
2690 dbg("%s", "Invalid Handler");
2691 return;
2692 }
2693
2694 /* check for the ports to be closed,close the ports and disconnect */
2695
2696 /* free private structure allocated for serial port *
2697 * stop reads and writes on all ports */
2698
2699 for (i = 0; i < serial->num_ports; ++i) {
2700 mos7840_port = mos7840_get_port_private(serial->port[i]);
2701 dbg("mos7840_port %d = %p", i, mos7840_port);
2702 if (mos7840_port) {
Tony Cook37768ad2009-04-18 22:42:18 +09302703 kfree(mos7840_port->ctrl_buf);
2704 kfree(mos7840_port->dr);
2705 kfree(mos7840_port);
2706 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002707 }
2708
Tony Cook84fe6e72009-04-18 22:55:06 +09302709 dbg("%s", "Thank u :: ");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002710
2711}
2712
Johannes Hölzld9b1b782006-12-17 21:50:24 +01002713static struct usb_driver io_driver = {
2714 .name = "mos7840",
2715 .probe = usb_serial_probe,
2716 .disconnect = usb_serial_disconnect,
2717 .id_table = moschip_id_table_combined,
2718 .no_dynamic_id = 1,
2719};
2720
Paul B Schroeder3f542972006-08-31 19:41:47 -05002721static struct usb_serial_driver moschip7840_4port_device = {
2722 .driver = {
2723 .owner = THIS_MODULE,
2724 .name = "mos7840",
2725 },
2726 .description = DRIVER_DESC,
Johannes Hölzld9b1b782006-12-17 21:50:24 +01002727 .usb_driver = &io_driver,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002728 .id_table = moschip_port_id_table,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002729 .num_ports = 4,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002730 .open = mos7840_open,
2731 .close = mos7840_close,
2732 .write = mos7840_write,
2733 .write_room = mos7840_write_room,
2734 .chars_in_buffer = mos7840_chars_in_buffer,
2735 .throttle = mos7840_throttle,
2736 .unthrottle = mos7840_unthrottle,
2737 .calc_num_ports = mos7840_calc_num_ports,
2738#ifdef MCSSerialProbe
2739 .probe = mos7840_serial_probe,
2740#endif
2741 .ioctl = mos7840_ioctl,
2742 .set_termios = mos7840_set_termios,
2743 .break_ctl = mos7840_break,
2744 .tiocmget = mos7840_tiocmget,
2745 .tiocmset = mos7840_tiocmset,
2746 .attach = mos7840_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -04002747 .disconnect = mos7840_disconnect,
2748 .release = mos7840_release,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002749 .read_bulk_callback = mos7840_bulk_in_callback,
2750 .read_int_callback = mos7840_interrupt_callback,
2751};
2752
Paul B Schroeder3f542972006-08-31 19:41:47 -05002753/****************************************************************************
2754 * moschip7840_init
2755 * This is called by the module subsystem, or on startup to initialize us
2756 ****************************************************************************/
2757static int __init moschip7840_init(void)
2758{
2759 int retval;
2760
Tony Cook84fe6e72009-04-18 22:55:06 +09302761 dbg("%s", " mos7840_init :entering..........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002762
2763 /* Register with the usb serial */
2764 retval = usb_serial_register(&moschip7840_4port_device);
2765
2766 if (retval)
2767 goto failed_port_device_register;
2768
Tony Cook84fe6e72009-04-18 22:55:06 +09302769 dbg("%s", "Entering...");
Greg Kroah-Hartmanc197a8d2008-08-18 13:21:04 -07002770 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
2771 DRIVER_DESC "\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002772
2773 /* Register with the usb */
2774 retval = usb_register(&io_driver);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002775 if (retval == 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302776 dbg("%s", "Leaving...");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002777 return 0;
2778 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002779 usb_serial_deregister(&moschip7840_4port_device);
Alan Cox880af9d2008-07-22 11:16:12 +01002780failed_port_device_register:
Paul B Schroeder3f542972006-08-31 19:41:47 -05002781 return retval;
2782}
2783
2784/****************************************************************************
2785 * moschip7840_exit
2786 * Called when the driver is about to be unloaded.
2787 ****************************************************************************/
2788static void __exit moschip7840_exit(void)
2789{
2790
Tony Cook84fe6e72009-04-18 22:55:06 +09302791 dbg("%s", " mos7840_exit :entering..........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002792
2793 usb_deregister(&io_driver);
2794
2795 usb_serial_deregister(&moschip7840_4port_device);
2796
Tony Cook84fe6e72009-04-18 22:55:06 +09302797 dbg("%s", "Entering...");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002798}
2799
2800module_init(moschip7840_init);
2801module_exit(moschip7840_exit);
2802
2803/* Module information */
2804MODULE_DESCRIPTION(DRIVER_DESC);
2805MODULE_LICENSE("GPL");
2806
2807module_param(debug, bool, S_IRUGO | S_IWUSR);
2808MODULE_PARM_DESC(debug, "Debug enabled or not");