blob: c31940a307f8d3fbdb9ad223380fd36262e4927e [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
Tony Cooke9b8cff2009-04-18 22:42:18 +0930127/* This driver also supports the ATEN UC2324 device since it is mos7840 based
128 * - if I knew the device id it would also support the ATEN UC2322 */
129#define USB_VENDOR_ID_ATENINTL 0x0557
130#define ATENINTL_DEVICE_ID_UC2324 0x2011
131
David Ludlow11e1abb2008-02-25 17:30:52 -0500132/* Interrupt Routine Defines */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500133
134#define SERIAL_IIR_RLS 0x06
135#define SERIAL_IIR_MS 0x00
136
137/*
138 * Emulation of the bit mask on the LINE STATUS REGISTER.
139 */
140#define SERIAL_LSR_DR 0x0001
141#define SERIAL_LSR_OE 0x0002
142#define SERIAL_LSR_PE 0x0004
143#define SERIAL_LSR_FE 0x0008
144#define SERIAL_LSR_BI 0x0010
145
146#define MOS_MSR_DELTA_CTS 0x10
147#define MOS_MSR_DELTA_DSR 0x20
148#define MOS_MSR_DELTA_RI 0x40
149#define MOS_MSR_DELTA_CD 0x80
150
Alan Cox880af9d2008-07-22 11:16:12 +0100151/* Serial Port register Address */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500152#define INTERRUPT_ENABLE_REGISTER ((__u16)(0x01))
153#define FIFO_CONTROL_REGISTER ((__u16)(0x02))
154#define LINE_CONTROL_REGISTER ((__u16)(0x03))
155#define MODEM_CONTROL_REGISTER ((__u16)(0x04))
156#define LINE_STATUS_REGISTER ((__u16)(0x05))
157#define MODEM_STATUS_REGISTER ((__u16)(0x06))
158#define SCRATCH_PAD_REGISTER ((__u16)(0x07))
159#define DIVISOR_LATCH_LSB ((__u16)(0x00))
160#define DIVISOR_LATCH_MSB ((__u16)(0x01))
161
162#define CLK_MULTI_REGISTER ((__u16)(0x02))
163#define CLK_START_VALUE_REGISTER ((__u16)(0x03))
164
165#define SERIAL_LCR_DLAB ((__u16)(0x0080))
166
167/*
168 * URB POOL related defines
169 */
170#define NUM_URBS 16 /* URB Count */
171#define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
172
173
174static struct usb_device_id moschip_port_id_table[] = {
175 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7840)},
176 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7820)},
David Ludlow11e1abb2008-02-25 17:30:52 -0500177 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4)},
178 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2)},
Tony Cooke9b8cff2009-04-18 22:42:18 +0930179 {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2324)},
Paul B Schroeder3f542972006-08-31 19:41:47 -0500180 {} /* terminating entry */
181};
182
183static __devinitdata struct usb_device_id moschip_id_table_combined[] = {
184 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7840)},
185 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7820)},
David Ludlow11e1abb2008-02-25 17:30:52 -0500186 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4)},
187 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2)},
Tony Cooke9b8cff2009-04-18 22:42:18 +0930188 {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2324)},
Paul B Schroeder3f542972006-08-31 19:41:47 -0500189 {} /* terminating entry */
190};
191
192MODULE_DEVICE_TABLE(usb, moschip_id_table_combined);
193
194/* This structure holds all of the local port information */
195
196struct moschip_port {
197 int port_num; /*Actual port number in the device(1,2,etc) */
198 struct urb *write_urb; /* write URB for this port */
199 struct urb *read_urb; /* read URB for this port */
Oliver Neukum0de9a702007-03-16 20:28:28 +0100200 struct urb *int_urb;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500201 __u8 shadowLCR; /* last LCR value received */
202 __u8 shadowMCR; /* last MCR value received */
203 char open;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100204 char open_ports;
205 char zombie;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500206 wait_queue_head_t wait_chase; /* for handling sleeping while waiting for chase to finish */
207 wait_queue_head_t delta_msr_wait; /* for handling sleeping while waiting for msr change to happen */
208 int delta_msr_cond;
209 struct async_icount icount;
210 struct usb_serial_port *port; /* loop back to the owner of this object */
211
Alan Cox880af9d2008-07-22 11:16:12 +0100212 /* Offsets */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500213 __u8 SpRegOffset;
214 __u8 ControlRegOffset;
215 __u8 DcrRegOffset;
Alan Cox880af9d2008-07-22 11:16:12 +0100216 /* for processing control URBS in interrupt context */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500217 struct urb *control_urb;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100218 struct usb_ctrlrequest *dr;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500219 char *ctrl_buf;
220 int MsrLsr;
221
Oliver Neukum0de9a702007-03-16 20:28:28 +0100222 spinlock_t pool_lock;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500223 struct urb *write_urb_pool[NUM_URBS];
Oliver Neukum0de9a702007-03-16 20:28:28 +0100224 char busy[NUM_URBS];
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800225 bool read_urb_busy;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500226};
227
228
229static int debug;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500230
231/*
232 * mos7840_set_reg_sync
233 * To set the Control register by calling usb_fill_control_urb function
234 * by passing usb_sndctrlpipe function as parameter.
235 */
236
237static int mos7840_set_reg_sync(struct usb_serial_port *port, __u16 reg,
238 __u16 val)
239{
240 struct usb_device *dev = port->serial->dev;
241 val = val & 0x00ff;
Tony Cook84fe6e72009-04-18 22:55:06 +0930242 dbg("mos7840_set_reg_sync offset is %x, value %x", reg, val);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500243
244 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
245 MCS_WR_RTYPE, val, reg, NULL, 0,
246 MOS_WDR_TIMEOUT);
247}
248
249/*
250 * mos7840_get_reg_sync
251 * To set the Uart register by calling usb_fill_control_urb function by
252 * passing usb_rcvctrlpipe function as parameter.
253 */
254
255static int mos7840_get_reg_sync(struct usb_serial_port *port, __u16 reg,
Alan Cox880af9d2008-07-22 11:16:12 +0100256 __u16 *val)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500257{
258 struct usb_device *dev = port->serial->dev;
259 int ret = 0;
260
261 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
262 MCS_RD_RTYPE, 0, reg, val, VENDOR_READ_LENGTH,
263 MOS_WDR_TIMEOUT);
Tony Cook84fe6e72009-04-18 22:55:06 +0930264 dbg("mos7840_get_reg_sync offset is %x, return val %x", reg, *val);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500265 *val = (*val) & 0x00ff;
266 return ret;
267}
268
269/*
270 * mos7840_set_uart_reg
271 * To set the Uart register by calling usb_fill_control_urb function by
272 * passing usb_sndctrlpipe function as parameter.
273 */
274
275static int mos7840_set_uart_reg(struct usb_serial_port *port, __u16 reg,
276 __u16 val)
277{
278
279 struct usb_device *dev = port->serial->dev;
280 val = val & 0x00ff;
Alan Cox880af9d2008-07-22 11:16:12 +0100281 /* For the UART control registers, the application number need
282 to be Or'ed */
Oliver Neukum0de9a702007-03-16 20:28:28 +0100283 if (port->serial->num_ports == 4) {
Alan Cox880af9d2008-07-22 11:16:12 +0100284 val |= (((__u16) port->number -
285 (__u16) (port->serial->minor)) + 1) << 8;
Tony Cook84fe6e72009-04-18 22:55:06 +0930286 dbg("mos7840_set_uart_reg application number is %x", val);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500287 } else {
288 if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
Alan Cox880af9d2008-07-22 11:16:12 +0100289 val |= (((__u16) port->number -
Paul B Schroeder3f542972006-08-31 19:41:47 -0500290 (__u16) (port->serial->minor)) + 1) << 8;
Tony Cook84fe6e72009-04-18 22:55:06 +0930291 dbg("mos7840_set_uart_reg application number is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -0500292 val);
293 } else {
294 val |=
295 (((__u16) port->number -
296 (__u16) (port->serial->minor)) + 2) << 8;
Tony Cook84fe6e72009-04-18 22:55:06 +0930297 dbg("mos7840_set_uart_reg application number is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -0500298 val);
299 }
300 }
301 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
302 MCS_WR_RTYPE, val, reg, NULL, 0,
303 MOS_WDR_TIMEOUT);
304
305}
306
307/*
308 * mos7840_get_uart_reg
309 * To set the Control register by calling usb_fill_control_urb function
310 * by passing usb_rcvctrlpipe function as parameter.
311 */
312static int mos7840_get_uart_reg(struct usb_serial_port *port, __u16 reg,
Alan Cox880af9d2008-07-22 11:16:12 +0100313 __u16 *val)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500314{
315 struct usb_device *dev = port->serial->dev;
316 int ret = 0;
317 __u16 Wval;
318
Tony Cook84fe6e72009-04-18 22:55:06 +0930319 /* dbg("application number is %4x",
Alan Cox880af9d2008-07-22 11:16:12 +0100320 (((__u16)port->number - (__u16)(port->serial->minor))+1)<<8); */
321 /* Wval is same as application number */
Oliver Neukum0de9a702007-03-16 20:28:28 +0100322 if (port->serial->num_ports == 4) {
Paul B Schroeder3f542972006-08-31 19:41:47 -0500323 Wval =
324 (((__u16) port->number - (__u16) (port->serial->minor)) +
325 1) << 8;
Tony Cook84fe6e72009-04-18 22:55:06 +0930326 dbg("mos7840_get_uart_reg application number is %x", Wval);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500327 } else {
328 if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
Alan Cox880af9d2008-07-22 11:16:12 +0100329 Wval = (((__u16) port->number -
Paul B Schroeder3f542972006-08-31 19:41:47 -0500330 (__u16) (port->serial->minor)) + 1) << 8;
Tony Cook84fe6e72009-04-18 22:55:06 +0930331 dbg("mos7840_get_uart_reg application number is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -0500332 Wval);
333 } else {
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)) + 2) << 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 }
339 }
340 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
341 MCS_RD_RTYPE, Wval, reg, val, VENDOR_READ_LENGTH,
342 MOS_WDR_TIMEOUT);
343 *val = (*val) & 0x00ff;
344 return ret;
345}
346
347static void mos7840_dump_serial_port(struct moschip_port *mos7840_port)
348{
349
Tony Cook84fe6e72009-04-18 22:55:06 +0930350 dbg("***************************************");
351 dbg("SpRegOffset is %2x", mos7840_port->SpRegOffset);
352 dbg("ControlRegOffset is %2x", mos7840_port->ControlRegOffset);
353 dbg("DCRRegOffset is %2x", mos7840_port->DcrRegOffset);
354 dbg("***************************************");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500355
356}
357
358/************************************************************************/
359/************************************************************************/
360/* I N T E R F A C E F U N C T I O N S */
361/* I N T E R F A C E F U N C T I O N S */
362/************************************************************************/
363/************************************************************************/
364
365static inline void mos7840_set_port_private(struct usb_serial_port *port,
366 struct moschip_port *data)
367{
368 usb_set_serial_port_data(port, (void *)data);
369}
370
371static inline struct moschip_port *mos7840_get_port_private(struct
372 usb_serial_port
373 *port)
374{
375 return (struct moschip_port *)usb_get_serial_port_data(port);
376}
377
Oliver Neukum0de9a702007-03-16 20:28:28 +0100378static void mos7840_handle_new_msr(struct moschip_port *port, __u8 new_msr)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500379{
380 struct moschip_port *mos7840_port;
381 struct async_icount *icount;
382 mos7840_port = port;
383 icount = &mos7840_port->icount;
384 if (new_msr &
385 (MOS_MSR_DELTA_CTS | MOS_MSR_DELTA_DSR | MOS_MSR_DELTA_RI |
386 MOS_MSR_DELTA_CD)) {
387 icount = &mos7840_port->icount;
388
389 /* update input line counters */
390 if (new_msr & MOS_MSR_DELTA_CTS) {
391 icount->cts++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100392 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500393 }
394 if (new_msr & MOS_MSR_DELTA_DSR) {
395 icount->dsr++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100396 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500397 }
398 if (new_msr & MOS_MSR_DELTA_CD) {
399 icount->dcd++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100400 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500401 }
402 if (new_msr & MOS_MSR_DELTA_RI) {
403 icount->rng++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100404 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500405 }
406 }
Paul B Schroeder3f542972006-08-31 19:41:47 -0500407}
408
Oliver Neukum0de9a702007-03-16 20:28:28 +0100409static void mos7840_handle_new_lsr(struct moschip_port *port, __u8 new_lsr)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500410{
411 struct async_icount *icount;
412
Harvey Harrison441b62c2008-03-03 16:08:34 -0800413 dbg("%s - %02x", __func__, new_lsr);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500414
415 if (new_lsr & SERIAL_LSR_BI) {
Alan Cox880af9d2008-07-22 11:16:12 +0100416 /*
417 * Parity and Framing errors only count if they
418 * occur exclusive of a break being
419 * received.
420 */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500421 new_lsr &= (__u8) (SERIAL_LSR_OE | SERIAL_LSR_BI);
422 }
423
424 /* update input line counters */
425 icount = &port->icount;
426 if (new_lsr & SERIAL_LSR_BI) {
427 icount->brk++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100428 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500429 }
430 if (new_lsr & SERIAL_LSR_OE) {
431 icount->overrun++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100432 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500433 }
434 if (new_lsr & SERIAL_LSR_PE) {
435 icount->parity++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100436 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500437 }
438 if (new_lsr & SERIAL_LSR_FE) {
439 icount->frame++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100440 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500441 }
Paul B Schroeder3f542972006-08-31 19:41:47 -0500442}
443
444/************************************************************************/
445/************************************************************************/
446/* U S B C A L L B A C K F U N C T I O N S */
447/* U S B C A L L B A C K F U N C T I O N S */
448/************************************************************************/
449/************************************************************************/
450
David Howells7d12e782006-10-05 14:55:46 +0100451static void mos7840_control_callback(struct urb *urb)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500452{
453 unsigned char *data;
454 struct moschip_port *mos7840_port;
455 __u8 regval = 0x0;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100456 int result = 0;
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700457 int status = urb->status;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500458
Ming Leicdc97792008-02-24 18:41:47 +0800459 mos7840_port = urb->context;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100460
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700461 switch (status) {
Paul B Schroeder3f542972006-08-31 19:41:47 -0500462 case 0:
463 /* success */
464 break;
465 case -ECONNRESET:
466 case -ENOENT:
467 case -ESHUTDOWN:
468 /* this urb is terminated, clean up */
Harvey Harrison441b62c2008-03-03 16:08:34 -0800469 dbg("%s - urb shutting down with status: %d", __func__,
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700470 status);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500471 return;
472 default:
Harvey Harrison441b62c2008-03-03 16:08:34 -0800473 dbg("%s - nonzero urb status received: %d", __func__,
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700474 status);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500475 goto exit;
476 }
477
Tony Cook84fe6e72009-04-18 22:55:06 +0930478 dbg("%s urb buffer size is %d", __func__, urb->actual_length);
479 dbg("%s mos7840_port->MsrLsr is %d port %d", __func__,
Paul B Schroeder3f542972006-08-31 19:41:47 -0500480 mos7840_port->MsrLsr, mos7840_port->port_num);
481 data = urb->transfer_buffer;
482 regval = (__u8) data[0];
Tony Cook84fe6e72009-04-18 22:55:06 +0930483 dbg("%s data is %x", __func__, regval);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500484 if (mos7840_port->MsrLsr == 0)
485 mos7840_handle_new_msr(mos7840_port, regval);
486 else if (mos7840_port->MsrLsr == 1)
487 mos7840_handle_new_lsr(mos7840_port, regval);
488
Oliver Neukum0de9a702007-03-16 20:28:28 +0100489exit:
490 spin_lock(&mos7840_port->pool_lock);
491 if (!mos7840_port->zombie)
492 result = usb_submit_urb(mos7840_port->int_urb, GFP_ATOMIC);
493 spin_unlock(&mos7840_port->pool_lock);
494 if (result) {
495 dev_err(&urb->dev->dev,
496 "%s - Error %d submitting interrupt urb\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800497 __func__, result);
Oliver Neukum0de9a702007-03-16 20:28:28 +0100498 }
Paul B Schroeder3f542972006-08-31 19:41:47 -0500499}
500
501static int mos7840_get_reg(struct moschip_port *mcs, __u16 Wval, __u16 reg,
Alan Cox880af9d2008-07-22 11:16:12 +0100502 __u16 *val)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500503{
504 struct usb_device *dev = mcs->port->serial->dev;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100505 struct usb_ctrlrequest *dr = mcs->dr;
506 unsigned char *buffer = mcs->ctrl_buf;
507 int ret;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500508
Paul B Schroeder3f542972006-08-31 19:41:47 -0500509 dr->bRequestType = MCS_RD_RTYPE;
510 dr->bRequest = MCS_RDREQ;
Alan Cox880af9d2008-07-22 11:16:12 +0100511 dr->wValue = cpu_to_le16(Wval); /* 0 */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500512 dr->wIndex = cpu_to_le16(reg);
513 dr->wLength = cpu_to_le16(2);
514
515 usb_fill_control_urb(mcs->control_urb, dev, usb_rcvctrlpipe(dev, 0),
516 (unsigned char *)dr, buffer, 2,
517 mos7840_control_callback, mcs);
518 mcs->control_urb->transfer_buffer_length = 2;
519 ret = usb_submit_urb(mcs->control_urb, GFP_ATOMIC);
520 return ret;
521}
522
523/*****************************************************************************
524 * mos7840_interrupt_callback
525 * this is the callback function for when we have received data on the
526 * interrupt endpoint.
527 *****************************************************************************/
528
David Howells7d12e782006-10-05 14:55:46 +0100529static void mos7840_interrupt_callback(struct urb *urb)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500530{
531 int result;
532 int length;
533 struct moschip_port *mos7840_port;
534 struct usb_serial *serial;
535 __u16 Data;
536 unsigned char *data;
537 __u8 sp[5], st;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100538 int i, rv = 0;
539 __u16 wval, wreg = 0;
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700540 int status = urb->status;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500541
Tony Cook84fe6e72009-04-18 22:55:06 +0930542 dbg("%s", " : Entering");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500543
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700544 switch (status) {
Paul B Schroeder3f542972006-08-31 19:41:47 -0500545 case 0:
546 /* success */
547 break;
548 case -ECONNRESET:
549 case -ENOENT:
550 case -ESHUTDOWN:
551 /* this urb is terminated, clean up */
Harvey Harrison441b62c2008-03-03 16:08:34 -0800552 dbg("%s - urb shutting down with status: %d", __func__,
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700553 status);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500554 return;
555 default:
Harvey Harrison441b62c2008-03-03 16:08:34 -0800556 dbg("%s - nonzero urb status received: %d", __func__,
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700557 status);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500558 goto exit;
559 }
560
561 length = urb->actual_length;
562 data = urb->transfer_buffer;
563
Ming Leicdc97792008-02-24 18:41:47 +0800564 serial = urb->context;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500565
566 /* Moschip get 5 bytes
567 * Byte 1 IIR Port 1 (port.number is 0)
568 * Byte 2 IIR Port 2 (port.number is 1)
569 * Byte 3 IIR Port 3 (port.number is 2)
570 * Byte 4 IIR Port 4 (port.number is 3)
571 * Byte 5 FIFO status for both */
572
573 if (length && length > 5) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930574 dbg("%s", "Wrong data !!!");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500575 return;
576 }
577
578 sp[0] = (__u8) data[0];
579 sp[1] = (__u8) data[1];
580 sp[2] = (__u8) data[2];
581 sp[3] = (__u8) data[3];
582 st = (__u8) data[4];
583
584 for (i = 0; i < serial->num_ports; i++) {
585 mos7840_port = mos7840_get_port_private(serial->port[i]);
586 wval =
587 (((__u16) serial->port[i]->number -
588 (__u16) (serial->minor)) + 1) << 8;
589 if (mos7840_port->open) {
590 if (sp[i] & 0x01) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930591 dbg("SP%d No Interrupt !!!", i);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500592 } else {
593 switch (sp[i] & 0x0f) {
594 case SERIAL_IIR_RLS:
595 dbg("Serial Port %d: Receiver status error or ", i);
Tony Cook84fe6e72009-04-18 22:55:06 +0930596 dbg("address bit detected in 9-bit mode");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500597 mos7840_port->MsrLsr = 1;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100598 wreg = LINE_STATUS_REGISTER;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500599 break;
600 case SERIAL_IIR_MS:
Tony Cook84fe6e72009-04-18 22:55:06 +0930601 dbg("Serial Port %d: Modem status change", i);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500602 mos7840_port->MsrLsr = 0;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100603 wreg = MODEM_STATUS_REGISTER;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500604 break;
605 }
Oliver Neukum0de9a702007-03-16 20:28:28 +0100606 spin_lock(&mos7840_port->pool_lock);
607 if (!mos7840_port->zombie) {
608 rv = mos7840_get_reg(mos7840_port, wval, wreg, &Data);
609 } else {
610 spin_unlock(&mos7840_port->pool_lock);
611 return;
612 }
613 spin_unlock(&mos7840_port->pool_lock);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500614 }
615 }
616 }
Alan Cox880af9d2008-07-22 11:16:12 +0100617 if (!(rv < 0))
618 /* the completion handler for the control urb will resubmit */
Oliver Neukum0de9a702007-03-16 20:28:28 +0100619 return;
620exit:
Paul B Schroeder3f542972006-08-31 19:41:47 -0500621 result = usb_submit_urb(urb, GFP_ATOMIC);
622 if (result) {
623 dev_err(&urb->dev->dev,
624 "%s - Error %d submitting interrupt urb\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800625 __func__, result);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500626 }
Paul B Schroeder3f542972006-08-31 19:41:47 -0500627}
628
629static int mos7840_port_paranoia_check(struct usb_serial_port *port,
630 const char *function)
631{
632 if (!port) {
633 dbg("%s - port == NULL", function);
634 return -1;
635 }
636 if (!port->serial) {
637 dbg("%s - port->serial == NULL", function);
638 return -1;
639 }
640
641 return 0;
642}
643
644/* Inline functions to check the sanity of a pointer that is passed to us */
645static int mos7840_serial_paranoia_check(struct usb_serial *serial,
646 const char *function)
647{
648 if (!serial) {
649 dbg("%s - serial == NULL", function);
650 return -1;
651 }
652 if (!serial->type) {
653 dbg("%s - serial->type == NULL!", function);
654 return -1;
655 }
656
657 return 0;
658}
659
660static struct usb_serial *mos7840_get_usb_serial(struct usb_serial_port *port,
661 const char *function)
662{
663 /* if no port was specified, or it fails a paranoia check */
664 if (!port ||
665 mos7840_port_paranoia_check(port, function) ||
666 mos7840_serial_paranoia_check(port->serial, function)) {
Alan Cox880af9d2008-07-22 11:16:12 +0100667 /* then say that we don't have a valid usb_serial thing,
668 * which will end up genrating -ENODEV return values */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500669 return NULL;
670 }
671
672 return port->serial;
673}
674
675/*****************************************************************************
676 * mos7840_bulk_in_callback
677 * this is the callback function for when we have received data on the
678 * bulk in endpoint.
679 *****************************************************************************/
680
David Howells7d12e782006-10-05 14:55:46 +0100681static void mos7840_bulk_in_callback(struct urb *urb)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500682{
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700683 int retval;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500684 unsigned char *data;
685 struct usb_serial *serial;
686 struct usb_serial_port *port;
687 struct moschip_port *mos7840_port;
688 struct tty_struct *tty;
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700689 int status = urb->status;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500690
Ming Leicdc97792008-02-24 18:41:47 +0800691 mos7840_port = urb->context;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500692 if (!mos7840_port) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930693 dbg("%s", "NULL mos7840_port pointer");
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800694 mos7840_port->read_urb_busy = false;
695 return;
696 }
697
698 if (status) {
699 dbg("nonzero read bulk status received: %d", status);
700 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500701 return;
702 }
703
704 port = (struct usb_serial_port *)mos7840_port->port;
Harvey Harrison441b62c2008-03-03 16:08:34 -0800705 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930706 dbg("%s", "Port Paranoia failed");
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800707 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500708 return;
709 }
710
Harvey Harrison441b62c2008-03-03 16:08:34 -0800711 serial = mos7840_get_usb_serial(port, __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500712 if (!serial) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930713 dbg("%s", "Bad serial pointer");
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800714 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500715 return;
716 }
717
Tony Cook84fe6e72009-04-18 22:55:06 +0930718 dbg("%s", "Entering... ");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500719
720 data = urb->transfer_buffer;
721
Tony Cook84fe6e72009-04-18 22:55:06 +0930722 dbg("%s", "Entering ...........");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500723
724 if (urb->actual_length) {
Alan Cox4a90f092008-10-13 10:39:46 +0100725 tty = tty_port_tty_get(&mos7840_port->port->port);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500726 if (tty) {
727 tty_buffer_request_room(tty, urb->actual_length);
728 tty_insert_flip_string(tty, data, urb->actual_length);
Tony Cook84fe6e72009-04-18 22:55:06 +0930729 dbg(" %s ", data);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500730 tty_flip_buffer_push(tty);
Alan Cox4a90f092008-10-13 10:39:46 +0100731 tty_kref_put(tty);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500732 }
733 mos7840_port->icount.rx += urb->actual_length;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100734 smp_wmb();
Tony Cook84fe6e72009-04-18 22:55:06 +0930735 dbg("mos7840_port->icount.rx is %d:",
Paul B Schroeder3f542972006-08-31 19:41:47 -0500736 mos7840_port->icount.rx);
737 }
738
739 if (!mos7840_port->read_urb) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930740 dbg("%s", "URB KILLED !!!");
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800741 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500742 return;
743 }
744
Paul B Schroeder3f542972006-08-31 19:41:47 -0500745
Oliver Neukum0de9a702007-03-16 20:28:28 +0100746 mos7840_port->read_urb->dev = serial->dev;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500747
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800748 mos7840_port->read_urb_busy = true;
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700749 retval = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
Oliver Neukum0de9a702007-03-16 20:28:28 +0100750
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700751 if (retval) {
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800752 dbg("usb_submit_urb(read bulk) failed, retval = %d", retval);
753 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500754 }
755}
756
757/*****************************************************************************
758 * mos7840_bulk_out_data_callback
Alan Cox880af9d2008-07-22 11:16:12 +0100759 * this is the callback function for when we have finished sending
760 * serial data on the bulk out endpoint.
Paul B Schroeder3f542972006-08-31 19:41:47 -0500761 *****************************************************************************/
762
David Howells7d12e782006-10-05 14:55:46 +0100763static void mos7840_bulk_out_data_callback(struct urb *urb)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500764{
765 struct moschip_port *mos7840_port;
766 struct tty_struct *tty;
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700767 int status = urb->status;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100768 int i;
769
Ming Leicdc97792008-02-24 18:41:47 +0800770 mos7840_port = urb->context;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100771 spin_lock(&mos7840_port->pool_lock);
772 for (i = 0; i < NUM_URBS; i++) {
773 if (urb == mos7840_port->write_urb_pool[i]) {
774 mos7840_port->busy[i] = 0;
775 break;
776 }
777 }
778 spin_unlock(&mos7840_port->pool_lock);
779
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700780 if (status) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930781 dbg("nonzero write bulk status received:%d", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500782 return;
783 }
784
Harvey Harrison441b62c2008-03-03 16:08:34 -0800785 if (mos7840_port_paranoia_check(mos7840_port->port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930786 dbg("%s", "Port Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500787 return;
788 }
789
Tony Cook84fe6e72009-04-18 22:55:06 +0930790 dbg("%s", "Entering .........");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500791
Alan Cox4a90f092008-10-13 10:39:46 +0100792 tty = tty_port_tty_get(&mos7840_port->port->port);
Jiri Slabyb963a842007-02-10 01:44:55 -0800793 if (tty && mos7840_port->open)
794 tty_wakeup(tty);
Alan Cox4a90f092008-10-13 10:39:46 +0100795 tty_kref_put(tty);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500796
797}
798
799/************************************************************************/
800/* 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 */
801/************************************************************************/
802#ifdef MCSSerialProbe
803static int mos7840_serial_probe(struct usb_serial *serial,
804 const struct usb_device_id *id)
805{
806
807 /*need to implement the mode_reg reading and updating\
808 structures usb_serial_ device_type\
809 (i.e num_ports, num_bulkin,bulkout etc) */
810 /* Also we can update the changes attach */
811 return 1;
812}
813#endif
814
815/*****************************************************************************
816 * mos7840_open
817 * this function is called by the tty driver when a port is opened
818 * If successful, we return 0
819 * Otherwise we return a negative error number.
820 *****************************************************************************/
821
Alan Cox95da3102008-07-22 11:09:07 +0100822static int mos7840_open(struct tty_struct *tty,
823 struct usb_serial_port *port, struct file *filp)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500824{
825 int response;
826 int j;
827 struct usb_serial *serial;
828 struct urb *urb;
829 __u16 Data;
830 int status;
831 struct moschip_port *mos7840_port;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100832 struct moschip_port *port0;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500833
Tony Cook84fe6e72009-04-18 22:55:06 +0930834 dbg ("%s enter", __func__);
835
Harvey Harrison441b62c2008-03-03 16:08:34 -0800836 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930837 dbg("%s", "Port Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500838 return -ENODEV;
839 }
840
Paul B Schroeder3f542972006-08-31 19:41:47 -0500841 serial = port->serial;
842
Harvey Harrison441b62c2008-03-03 16:08:34 -0800843 if (mos7840_serial_paranoia_check(serial, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930844 dbg("%s", "Serial Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500845 return -ENODEV;
846 }
847
848 mos7840_port = mos7840_get_port_private(port);
Oliver Neukum0de9a702007-03-16 20:28:28 +0100849 port0 = mos7840_get_port_private(serial->port[0]);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500850
Oliver Neukum0de9a702007-03-16 20:28:28 +0100851 if (mos7840_port == NULL || port0 == NULL)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500852 return -ENODEV;
853
854 usb_clear_halt(serial->dev, port->write_urb->pipe);
855 usb_clear_halt(serial->dev, port->read_urb->pipe);
Oliver Neukum0de9a702007-03-16 20:28:28 +0100856 port0->open_ports++;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500857
858 /* Initialising the write urb pool */
859 for (j = 0; j < NUM_URBS; ++j) {
Oliver Neukum0de9a702007-03-16 20:28:28 +0100860 urb = usb_alloc_urb(0, GFP_KERNEL);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500861 mos7840_port->write_urb_pool[j] = urb;
862
863 if (urb == NULL) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700864 dev_err(&port->dev, "No more urbs???\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500865 continue;
866 }
867
Alan Cox880af9d2008-07-22 11:16:12 +0100868 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
869 GFP_KERNEL);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500870 if (!urb->transfer_buffer) {
Oliver Neukum0de9a702007-03-16 20:28:28 +0100871 usb_free_urb(urb);
872 mos7840_port->write_urb_pool[j] = NULL;
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700873 dev_err(&port->dev,
874 "%s-out of memory for urb buffers.\n",
875 __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500876 continue;
877 }
878 }
879
880/*****************************************************************************
881 * Initialize MCS7840 -- Write Init values to corresponding Registers
882 *
883 * Register Index
884 * 1 : IER
885 * 2 : FCR
886 * 3 : LCR
887 * 4 : MCR
888 *
889 * 0x08 : SP1/2 Control Reg
890 *****************************************************************************/
891
Alan Cox880af9d2008-07-22 11:16:12 +0100892 /* NEED to check the following Block */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500893
Paul B Schroeder3f542972006-08-31 19:41:47 -0500894 Data = 0x0;
895 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
896 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930897 dbg("Reading Spreg failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500898 return -1;
899 }
900 Data |= 0x80;
901 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
902 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930903 dbg("writing Spreg failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500904 return -1;
905 }
906
907 Data &= ~0x80;
908 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
909 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930910 dbg("writing Spreg failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500911 return -1;
912 }
Alan Cox880af9d2008-07-22 11:16:12 +0100913 /* End of block to be checked */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500914
Paul B Schroeder3f542972006-08-31 19:41:47 -0500915 Data = 0x0;
Alan Cox880af9d2008-07-22 11:16:12 +0100916 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
917 &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500918 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930919 dbg("Reading Controlreg failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500920 return -1;
921 }
Alan Cox880af9d2008-07-22 11:16:12 +0100922 Data |= 0x08; /* Driver done bit */
923 Data |= 0x20; /* rx_disable */
924 status = mos7840_set_reg_sync(port,
925 mos7840_port->ControlRegOffset, Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500926 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930927 dbg("writing Controlreg failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500928 return -1;
929 }
Alan Cox880af9d2008-07-22 11:16:12 +0100930 /* do register settings here */
931 /* Set all regs to the device default values. */
932 /***********************************
933 * First Disable all interrupts.
934 ***********************************/
Paul B Schroeder3f542972006-08-31 19:41:47 -0500935 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500936 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
937 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930938 dbg("disabling interrupts failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500939 return -1;
940 }
Alan Cox880af9d2008-07-22 11:16:12 +0100941 /* Set FIFO_CONTROL_REGISTER to the default value */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500942 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500943 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
944 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930945 dbg("Writing FIFO_CONTROL_REGISTER failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500946 return -1;
947 }
948
949 Data = 0xcf;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500950 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
951 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930952 dbg("Writing FIFO_CONTROL_REGISTER failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500953 return -1;
954 }
955
956 Data = 0x03;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500957 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
958 mos7840_port->shadowLCR = Data;
959
960 Data = 0x0b;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500961 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
962 mos7840_port->shadowMCR = Data;
963
964 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500965 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
966 mos7840_port->shadowLCR = Data;
967
Alan Cox880af9d2008-07-22 11:16:12 +0100968 Data |= SERIAL_LCR_DLAB; /* data latch enable in LCR 0x80 */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500969 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
970
971 Data = 0x0c;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500972 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
973
974 Data = 0x0;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500975 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
976
977 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500978 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
979
980 Data = Data & ~SERIAL_LCR_DLAB;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500981 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
982 mos7840_port->shadowLCR = Data;
983
Alan Cox880af9d2008-07-22 11:16:12 +0100984 /* clearing Bulkin and Bulkout Fifo */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500985 Data = 0x0;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500986 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
987
988 Data = Data | 0x0c;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500989 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
990
991 Data = Data & ~0x0c;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500992 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
Alan Cox880af9d2008-07-22 11:16:12 +0100993 /* Finally enable all interrupts */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500994 Data = 0x0c;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500995 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
996
Alan Cox880af9d2008-07-22 11:16:12 +0100997 /* clearing rx_disable */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500998 Data = 0x0;
Alan Cox880af9d2008-07-22 11:16:12 +0100999 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1000 &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001001 Data = Data & ~0x20;
Alan Cox880af9d2008-07-22 11:16:12 +01001002 status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1003 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001004
Alan Cox880af9d2008-07-22 11:16:12 +01001005 /* rx_negate */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001006 Data = 0x0;
Alan Cox880af9d2008-07-22 11:16:12 +01001007 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1008 &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001009 Data = Data | 0x10;
Alan Cox880af9d2008-07-22 11:16:12 +01001010 status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1011 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001012
Alan Cox880af9d2008-07-22 11:16:12 +01001013 /* Check to see if we've set up our endpoint info yet *
1014 * (can't set it up in mos7840_startup as the structures *
1015 * were not set up at that time.) */
Oliver Neukum0de9a702007-03-16 20:28:28 +01001016 if (port0->open_ports == 1) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001017 if (serial->port[0]->interrupt_in_buffer == NULL) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001018 /* set up interrupt urb */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001019 usb_fill_int_urb(serial->port[0]->interrupt_in_urb,
Alan Cox880af9d2008-07-22 11:16:12 +01001020 serial->dev,
1021 usb_rcvintpipe(serial->dev,
1022 serial->port[0]->interrupt_in_endpointAddress),
1023 serial->port[0]->interrupt_in_buffer,
1024 serial->port[0]->interrupt_in_urb->
1025 transfer_buffer_length,
1026 mos7840_interrupt_callback,
1027 serial,
1028 serial->port[0]->interrupt_in_urb->interval);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001029
1030 /* start interrupt read for mos7840 *
1031 * will continue as long as mos7840 is connected */
1032
1033 response =
1034 usb_submit_urb(serial->port[0]->interrupt_in_urb,
1035 GFP_KERNEL);
1036 if (response) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001037 dev_err(&port->dev, "%s - Error %d submitting "
1038 "interrupt urb\n", __func__, response);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001039 }
1040
1041 }
1042
1043 }
1044
1045 /* see if we've set up our endpoint info yet *
1046 * (can't set it up in mos7840_startup as the *
1047 * structures were not set up at that time.) */
1048
Tony Cook84fe6e72009-04-18 22:55:06 +09301049 dbg("port number is %d", port->number);
1050 dbg("serial number is %d", port->serial->minor);
1051 dbg("Bulkin endpoint is %d", port->bulk_in_endpointAddress);
1052 dbg("BulkOut endpoint is %d", port->bulk_out_endpointAddress);
1053 dbg("Interrupt endpoint is %d", port->interrupt_in_endpointAddress);
1054 dbg("port's number in the device is %d", mos7840_port->port_num);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001055 mos7840_port->read_urb = port->read_urb;
1056
1057 /* set up our bulk in urb */
1058
1059 usb_fill_bulk_urb(mos7840_port->read_urb,
1060 serial->dev,
1061 usb_rcvbulkpipe(serial->dev,
1062 port->bulk_in_endpointAddress),
1063 port->bulk_in_buffer,
1064 mos7840_port->read_urb->transfer_buffer_length,
1065 mos7840_bulk_in_callback, mos7840_port);
1066
Tony Cook84fe6e72009-04-18 22:55:06 +09301067 dbg("mos7840_open: bulkin endpoint is %d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05001068 port->bulk_in_endpointAddress);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08001069 mos7840_port->read_urb_busy = true;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001070 response = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
1071 if (response) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001072 dev_err(&port->dev, "%s - Error %d submitting control urb\n",
1073 __func__, response);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08001074 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001075 }
1076
1077 /* initialize our wait queues */
1078 init_waitqueue_head(&mos7840_port->wait_chase);
1079 init_waitqueue_head(&mos7840_port->delta_msr_wait);
1080
1081 /* initialize our icount structure */
1082 memset(&(mos7840_port->icount), 0x00, sizeof(mos7840_port->icount));
1083
1084 /* initialize our port settings */
Alan Cox880af9d2008-07-22 11:16:12 +01001085 /* Must set to enable ints! */
1086 mos7840_port->shadowMCR = MCR_MASTER_IE;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001087 /* send a open port command */
1088 mos7840_port->open = 1;
Alan Cox880af9d2008-07-22 11:16:12 +01001089 /* mos7840_change_port_settings(mos7840_port,old_termios); */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001090 mos7840_port->icount.tx = 0;
1091 mos7840_port->icount.rx = 0;
1092
Tony Cook84fe6e72009-04-18 22:55:06 +09301093 dbg("usb_serial serial:%p mos7840_port:%p\n usb_serial_port port:%p",
Alan Cox880af9d2008-07-22 11:16:12 +01001094 serial, mos7840_port, port);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001095
Tony Cook84fe6e72009-04-18 22:55:06 +09301096 dbg ("%s leave", __func__);
1097
Paul B Schroeder3f542972006-08-31 19:41:47 -05001098 return 0;
1099
1100}
1101
1102/*****************************************************************************
1103 * mos7840_chars_in_buffer
1104 * this function is called by the tty driver when it wants to know how many
1105 * bytes of data we currently have outstanding in the port (data that has
1106 * been written, but hasn't made it out the port yet)
1107 * If successful, we return the number of bytes left to be written in the
1108 * system,
Alan Cox95da3102008-07-22 11:09:07 +01001109 * Otherwise we return zero.
Paul B Schroeder3f542972006-08-31 19:41:47 -05001110 *****************************************************************************/
1111
Alan Cox95da3102008-07-22 11:09:07 +01001112static int mos7840_chars_in_buffer(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001113{
Alan Cox95da3102008-07-22 11:09:07 +01001114 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001115 int i;
1116 int chars = 0;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001117 unsigned long flags;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001118 struct moschip_port *mos7840_port;
1119
Tony Cook84fe6e72009-04-18 22:55:06 +09301120 dbg("%s", " mos7840_chars_in_buffer:entering ...........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001121
Harvey Harrison441b62c2008-03-03 16:08:34 -08001122 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301123 dbg("%s", "Invalid port");
Alan Cox95da3102008-07-22 11:09:07 +01001124 return 0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001125 }
1126
1127 mos7840_port = mos7840_get_port_private(port);
1128 if (mos7840_port == NULL) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301129 dbg("%s", "mos7840_break:leaving ...........");
Alan Cox95da3102008-07-22 11:09:07 +01001130 return 0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001131 }
1132
Alan Cox880af9d2008-07-22 11:16:12 +01001133 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
Alan Cox95da3102008-07-22 11:09:07 +01001134 for (i = 0; i < NUM_URBS; ++i)
1135 if (mos7840_port->busy[i])
Paul B Schroeder3f542972006-08-31 19:41:47 -05001136 chars += URB_TRANSFER_BUFFER_SIZE;
Alan Cox880af9d2008-07-22 11:16:12 +01001137 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
Harvey Harrison441b62c2008-03-03 16:08:34 -08001138 dbg("%s - returns %d", __func__, chars);
Oliver Neukum0de9a702007-03-16 20:28:28 +01001139 return chars;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001140
1141}
1142
Paul B Schroeder3f542972006-08-31 19:41:47 -05001143/*****************************************************************************
1144 * mos7840_close
1145 * this function is called by the tty driver when a port is closed
1146 *****************************************************************************/
1147
Alan Cox335f8512009-06-11 12:26:29 +01001148static void mos7840_close(struct usb_serial_port *port)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001149{
1150 struct usb_serial *serial;
1151 struct moschip_port *mos7840_port;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001152 struct moschip_port *port0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001153 int j;
1154 __u16 Data;
1155
Tony Cook84fe6e72009-04-18 22:55:06 +09301156 dbg("%s", "mos7840_close:entering...");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001157
Harvey Harrison441b62c2008-03-03 16:08:34 -08001158 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301159 dbg("%s", "Port Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001160 return;
1161 }
1162
Harvey Harrison441b62c2008-03-03 16:08:34 -08001163 serial = mos7840_get_usb_serial(port, __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001164 if (!serial) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301165 dbg("%s", "Serial Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001166 return;
1167 }
1168
1169 mos7840_port = mos7840_get_port_private(port);
Oliver Neukum0de9a702007-03-16 20:28:28 +01001170 port0 = mos7840_get_port_private(serial->port[0]);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001171
Oliver Neukum0de9a702007-03-16 20:28:28 +01001172 if (mos7840_port == NULL || port0 == NULL)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001173 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001174
1175 for (j = 0; j < NUM_URBS; ++j)
1176 usb_kill_urb(mos7840_port->write_urb_pool[j]);
1177
1178 /* Freeing Write URBs */
1179 for (j = 0; j < NUM_URBS; ++j) {
1180 if (mos7840_port->write_urb_pool[j]) {
1181 if (mos7840_port->write_urb_pool[j]->transfer_buffer)
1182 kfree(mos7840_port->write_urb_pool[j]->
1183 transfer_buffer);
1184
1185 usb_free_urb(mos7840_port->write_urb_pool[j]);
1186 }
1187 }
1188
Paul B Schroeder3f542972006-08-31 19:41:47 -05001189 /* While closing port, shutdown all bulk read, write *
1190 * and interrupt read if they exists */
1191 if (serial->dev) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001192 if (mos7840_port->write_urb) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301193 dbg("%s", "Shutdown bulk write");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001194 usb_kill_urb(mos7840_port->write_urb);
1195 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001196 if (mos7840_port->read_urb) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301197 dbg("%s", "Shutdown bulk read");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001198 usb_kill_urb(mos7840_port->read_urb);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08001199 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001200 }
1201 if ((&mos7840_port->control_urb)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301202 dbg("%s", "Shutdown control read");
Alan Cox880af9d2008-07-22 11:16:12 +01001203 /*/ usb_kill_urb (mos7840_port->control_urb); */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001204 }
1205 }
Alan Cox880af9d2008-07-22 11:16:12 +01001206/* if(mos7840_port->ctrl_buf != NULL) */
1207/* kfree(mos7840_port->ctrl_buf); */
Oliver Neukum0de9a702007-03-16 20:28:28 +01001208 port0->open_ports--;
Tony Cook84fe6e72009-04-18 22:55:06 +09301209 dbg("mos7840_num_open_ports in close%d:in port%d",
Oliver Neukum0de9a702007-03-16 20:28:28 +01001210 port0->open_ports, port->number);
1211 if (port0->open_ports == 0) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001212 if (serial->port[0]->interrupt_in_urb) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301213 dbg("%s", "Shutdown interrupt_in_urb");
Oliver Neukum0de9a702007-03-16 20:28:28 +01001214 usb_kill_urb(serial->port[0]->interrupt_in_urb);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001215 }
1216 }
1217
1218 if (mos7840_port->write_urb) {
1219 /* if this urb had a transfer buffer already (old tx) free it */
Alan Cox880af9d2008-07-22 11:16:12 +01001220 if (mos7840_port->write_urb->transfer_buffer != NULL)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001221 kfree(mos7840_port->write_urb->transfer_buffer);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001222 usb_free_urb(mos7840_port->write_urb);
1223 }
1224
1225 Data = 0x0;
1226 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1227
1228 Data = 0x00;
1229 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1230
1231 mos7840_port->open = 0;
1232
Tony Cook84fe6e72009-04-18 22:55:06 +09301233 dbg("%s", "Leaving ............");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001234}
1235
1236/************************************************************************
1237 *
1238 * mos7840_block_until_chase_response
1239 *
1240 * This function will block the close until one of the following:
1241 * 1. Response to our Chase comes from mos7840
Joe Perchesdc0d5c12007-12-17 11:40:18 -08001242 * 2. A timeout of 10 seconds without activity has expired
Paul B Schroeder3f542972006-08-31 19:41:47 -05001243 * (1K of mos7840 data @ 2400 baud ==> 4 sec to empty)
1244 *
1245 ************************************************************************/
1246
Alan Cox95da3102008-07-22 11:09:07 +01001247static void mos7840_block_until_chase_response(struct tty_struct *tty,
1248 struct moschip_port *mos7840_port)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001249{
1250 int timeout = 1 * HZ;
1251 int wait = 10;
1252 int count;
1253
1254 while (1) {
Alan Cox95da3102008-07-22 11:09:07 +01001255 count = mos7840_chars_in_buffer(tty);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001256
1257 /* Check for Buffer status */
Alan Cox880af9d2008-07-22 11:16:12 +01001258 if (count <= 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001259 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001260
1261 /* Block the thread for a while */
1262 interruptible_sleep_on_timeout(&mos7840_port->wait_chase,
1263 timeout);
1264 /* No activity.. count down section */
1265 wait--;
1266 if (wait == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001267 dbg("%s - TIMEOUT", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001268 return;
1269 } else {
Joe Perchesdc0d5c12007-12-17 11:40:18 -08001270 /* Reset timeout value back to seconds */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001271 wait = 10;
1272 }
1273 }
1274
1275}
1276
1277/*****************************************************************************
1278 * mos7840_break
1279 * this function sends a break to the port
1280 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001281static void mos7840_break(struct tty_struct *tty, int break_state)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001282{
Alan Cox95da3102008-07-22 11:09:07 +01001283 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001284 unsigned char data;
1285 struct usb_serial *serial;
1286 struct moschip_port *mos7840_port;
1287
Tony Cook84fe6e72009-04-18 22:55:06 +09301288 dbg("%s", "Entering ...........");
1289 dbg("mos7840_break: Start");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001290
Harvey Harrison441b62c2008-03-03 16:08:34 -08001291 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301292 dbg("%s", "Port Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001293 return;
1294 }
1295
Harvey Harrison441b62c2008-03-03 16:08:34 -08001296 serial = mos7840_get_usb_serial(port, __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001297 if (!serial) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301298 dbg("%s", "Serial Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001299 return;
1300 }
1301
1302 mos7840_port = mos7840_get_port_private(port);
1303
Alan Cox880af9d2008-07-22 11:16:12 +01001304 if (mos7840_port == NULL)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001305 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001306
Alan Cox95da3102008-07-22 11:09:07 +01001307 if (serial->dev)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001308 /* flush and block until tx is empty */
Alan Cox95da3102008-07-22 11:09:07 +01001309 mos7840_block_until_chase_response(tty, mos7840_port);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001310
Alan Cox95da3102008-07-22 11:09:07 +01001311 if (break_state == -1)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001312 data = mos7840_port->shadowLCR | LCR_SET_BREAK;
Alan Cox95da3102008-07-22 11:09:07 +01001313 else
Paul B Schroeder3f542972006-08-31 19:41:47 -05001314 data = mos7840_port->shadowLCR & ~LCR_SET_BREAK;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001315
Alan Cox6b447f042009-01-02 13:48:56 +00001316 /* FIXME: no locking on shadowLCR anywhere in driver */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001317 mos7840_port->shadowLCR = data;
Tony Cook84fe6e72009-04-18 22:55:06 +09301318 dbg("mcs7840_break mos7840_port->shadowLCR is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05001319 mos7840_port->shadowLCR);
1320 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER,
1321 mos7840_port->shadowLCR);
1322
1323 return;
1324}
1325
1326/*****************************************************************************
1327 * mos7840_write_room
1328 * this function is called by the tty driver when it wants to know how many
1329 * bytes of data we can accept for a specific port.
1330 * If successful, we return the amount of room that we have for this port
1331 * Otherwise we return a negative error number.
1332 *****************************************************************************/
1333
Alan Cox95da3102008-07-22 11:09:07 +01001334static int mos7840_write_room(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001335{
Alan Cox95da3102008-07-22 11:09:07 +01001336 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001337 int i;
1338 int room = 0;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001339 unsigned long flags;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001340 struct moschip_port *mos7840_port;
1341
Tony Cook84fe6e72009-04-18 22:55:06 +09301342 dbg("%s", " mos7840_write_room:entering ...........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001343
Harvey Harrison441b62c2008-03-03 16:08:34 -08001344 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301345 dbg("%s", "Invalid port");
1346 dbg("%s", " mos7840_write_room:leaving ...........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001347 return -1;
1348 }
1349
1350 mos7840_port = mos7840_get_port_private(port);
1351 if (mos7840_port == NULL) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301352 dbg("%s", "mos7840_break:leaving ...........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001353 return -1;
1354 }
1355
Oliver Neukum0de9a702007-03-16 20:28:28 +01001356 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001357 for (i = 0; i < NUM_URBS; ++i) {
Alan Cox880af9d2008-07-22 11:16:12 +01001358 if (!mos7840_port->busy[i])
Paul B Schroeder3f542972006-08-31 19:41:47 -05001359 room += URB_TRANSFER_BUFFER_SIZE;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001360 }
Oliver Neukum0de9a702007-03-16 20:28:28 +01001361 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001362
Oliver Neukum0de9a702007-03-16 20:28:28 +01001363 room = (room == 0) ? 0 : room - URB_TRANSFER_BUFFER_SIZE + 1;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001364 dbg("%s - returns %d", __func__, room);
Oliver Neukum0de9a702007-03-16 20:28:28 +01001365 return room;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001366
1367}
1368
1369/*****************************************************************************
1370 * mos7840_write
1371 * this function is called by the tty driver when data should be written to
1372 * the port.
1373 * If successful, we return the number of bytes written, otherwise we
1374 * return a negative error number.
1375 *****************************************************************************/
1376
Alan Cox95da3102008-07-22 11:09:07 +01001377static int mos7840_write(struct tty_struct *tty, struct usb_serial_port *port,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001378 const unsigned char *data, int count)
1379{
1380 int status;
1381 int i;
1382 int bytes_sent = 0;
1383 int transfer_size;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001384 unsigned long flags;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001385
1386 struct moschip_port *mos7840_port;
1387 struct usb_serial *serial;
1388 struct urb *urb;
Alan Cox880af9d2008-07-22 11:16:12 +01001389 /* __u16 Data; */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001390 const unsigned char *current_position = data;
1391 unsigned char *data1;
Tony Cook84fe6e72009-04-18 22:55:06 +09301392 dbg("%s", "entering ...........");
1393 /* dbg("mos7840_write: mos7840_port->shadowLCR is %x",
Alan Cox880af9d2008-07-22 11:16:12 +01001394 mos7840_port->shadowLCR); */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001395
1396#ifdef NOTMOS7840
1397 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001398 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
1399 mos7840_port->shadowLCR = Data;
Tony Cook84fe6e72009-04-18 22:55:06 +09301400 dbg("mos7840_write: LINE_CONTROL_REGISTER is %x", Data);
1401 dbg("mos7840_write: mos7840_port->shadowLCR is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05001402 mos7840_port->shadowLCR);
1403
Alan Cox880af9d2008-07-22 11:16:12 +01001404 /* Data = 0x03; */
1405 /* status = mos7840_set_uart_reg(port,LINE_CONTROL_REGISTER,Data); */
1406 /* mos7840_port->shadowLCR=Data;//Need to add later */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001407
Alan Cox880af9d2008-07-22 11:16:12 +01001408 Data |= SERIAL_LCR_DLAB; /* data latch enable in LCR 0x80 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001409 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1410
Alan Cox880af9d2008-07-22 11:16:12 +01001411 /* Data = 0x0c; */
1412 /* status = mos7840_set_uart_reg(port,DIVISOR_LATCH_LSB,Data); */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001413 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001414 status = mos7840_get_uart_reg(port, DIVISOR_LATCH_LSB, &Data);
Tony Cook84fe6e72009-04-18 22:55:06 +09301415 dbg("mos7840_write:DLL value is %x", Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001416
1417 Data = 0x0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001418 status = mos7840_get_uart_reg(port, DIVISOR_LATCH_MSB, &Data);
Tony Cook84fe6e72009-04-18 22:55:06 +09301419 dbg("mos7840_write:DLM value is %x", Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001420
1421 Data = Data & ~SERIAL_LCR_DLAB;
Tony Cook84fe6e72009-04-18 22:55:06 +09301422 dbg("mos7840_write: mos7840_port->shadowLCR is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05001423 mos7840_port->shadowLCR);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001424 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1425#endif
1426
Harvey Harrison441b62c2008-03-03 16:08:34 -08001427 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301428 dbg("%s", "Port Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001429 return -1;
1430 }
1431
1432 serial = port->serial;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001433 if (mos7840_serial_paranoia_check(serial, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301434 dbg("%s", "Serial Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001435 return -1;
1436 }
1437
1438 mos7840_port = mos7840_get_port_private(port);
1439 if (mos7840_port == NULL) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301440 dbg("%s", "mos7840_port is NULL");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001441 return -1;
1442 }
1443
1444 /* try to find a free urb in the list */
1445 urb = NULL;
1446
Oliver Neukum0de9a702007-03-16 20:28:28 +01001447 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001448 for (i = 0; i < NUM_URBS; ++i) {
Oliver Neukum0de9a702007-03-16 20:28:28 +01001449 if (!mos7840_port->busy[i]) {
1450 mos7840_port->busy[i] = 1;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001451 urb = mos7840_port->write_urb_pool[i];
Tony Cook84fe6e72009-04-18 22:55:06 +09301452 dbg("URB:%d", i);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001453 break;
1454 }
1455 }
Oliver Neukum0de9a702007-03-16 20:28:28 +01001456 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001457
1458 if (urb == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001459 dbg("%s - no more free urbs", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001460 goto exit;
1461 }
1462
1463 if (urb->transfer_buffer == NULL) {
1464 urb->transfer_buffer =
1465 kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
1466
1467 if (urb->transfer_buffer == NULL) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001468 dev_err(&port->dev, "%s no more kernel memory...\n",
1469 __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001470 goto exit;
1471 }
1472 }
1473 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
1474
Al Viro97c49652006-10-09 20:29:03 +01001475 memcpy(urb->transfer_buffer, current_position, transfer_size);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001476
1477 /* fill urb with data and submit */
1478 usb_fill_bulk_urb(urb,
1479 serial->dev,
1480 usb_sndbulkpipe(serial->dev,
1481 port->bulk_out_endpointAddress),
1482 urb->transfer_buffer,
1483 transfer_size,
1484 mos7840_bulk_out_data_callback, mos7840_port);
1485
1486 data1 = urb->transfer_buffer;
Tony Cook84fe6e72009-04-18 22:55:06 +09301487 dbg("bulkout endpoint is %d", port->bulk_out_endpointAddress);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001488
1489 /* send it down the pipe */
1490 status = usb_submit_urb(urb, GFP_ATOMIC);
1491
1492 if (status) {
Oliver Neukum0de9a702007-03-16 20:28:28 +01001493 mos7840_port->busy[i] = 0;
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001494 dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed "
1495 "with status = %d\n", __func__, status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001496 bytes_sent = status;
1497 goto exit;
1498 }
1499 bytes_sent = transfer_size;
1500 mos7840_port->icount.tx += transfer_size;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001501 smp_wmb();
Tony Cook84fe6e72009-04-18 22:55:06 +09301502 dbg("mos7840_port->icount.tx is %d:", mos7840_port->icount.tx);
Alan Cox95da3102008-07-22 11:09:07 +01001503exit:
Paul B Schroeder3f542972006-08-31 19:41:47 -05001504 return bytes_sent;
1505
1506}
1507
1508/*****************************************************************************
1509 * mos7840_throttle
1510 * this function is called by the tty driver when it wants to stop the data
1511 * being read from the port.
1512 *****************************************************************************/
1513
Alan Cox95da3102008-07-22 11:09:07 +01001514static void mos7840_throttle(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001515{
Alan Cox95da3102008-07-22 11:09:07 +01001516 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001517 struct moschip_port *mos7840_port;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001518 int status;
1519
Harvey Harrison441b62c2008-03-03 16:08:34 -08001520 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301521 dbg("%s", "Invalid port");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001522 return;
1523 }
1524
Tony Cook84fe6e72009-04-18 22:55:06 +09301525 dbg("- port %d", port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001526
1527 mos7840_port = mos7840_get_port_private(port);
1528
1529 if (mos7840_port == NULL)
1530 return;
1531
1532 if (!mos7840_port->open) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301533 dbg("%s", "port not opened");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001534 return;
1535 }
1536
Tony Cook84fe6e72009-04-18 22:55:06 +09301537 dbg("%s", "Entering ..........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001538
Paul B Schroeder3f542972006-08-31 19:41:47 -05001539 /* if we are implementing XON/XOFF, send the stop character */
1540 if (I_IXOFF(tty)) {
1541 unsigned char stop_char = STOP_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01001542 status = mos7840_write(tty, port, &stop_char, 1);
1543 if (status <= 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001544 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001545 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001546 /* if we are implementing RTS/CTS, toggle that line */
1547 if (tty->termios->c_cflag & CRTSCTS) {
1548 mos7840_port->shadowMCR &= ~MCR_RTS;
Alan Cox95da3102008-07-22 11:09:07 +01001549 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001550 mos7840_port->shadowMCR);
Alan Cox95da3102008-07-22 11:09:07 +01001551 if (status < 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001552 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001553 }
1554
1555 return;
1556}
1557
1558/*****************************************************************************
1559 * mos7840_unthrottle
Alan Cox880af9d2008-07-22 11:16:12 +01001560 * this function is called by the tty driver when it wants to resume
1561 * the data being read from the port (called after mos7840_throttle is
1562 * called)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001563 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001564static void mos7840_unthrottle(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001565{
Alan Cox95da3102008-07-22 11:09:07 +01001566 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001567 int status;
1568 struct moschip_port *mos7840_port = mos7840_get_port_private(port);
1569
Harvey Harrison441b62c2008-03-03 16:08:34 -08001570 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301571 dbg("%s", "Invalid port");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001572 return;
1573 }
1574
1575 if (mos7840_port == NULL)
1576 return;
1577
1578 if (!mos7840_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001579 dbg("%s - port not opened", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001580 return;
1581 }
1582
Tony Cook84fe6e72009-04-18 22:55:06 +09301583 dbg("%s", "Entering ..........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001584
Paul B Schroeder3f542972006-08-31 19:41:47 -05001585 /* if we are implementing XON/XOFF, send the start character */
1586 if (I_IXOFF(tty)) {
1587 unsigned char start_char = START_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01001588 status = mos7840_write(tty, port, &start_char, 1);
1589 if (status <= 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001590 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001591 }
1592
1593 /* if we are implementing RTS/CTS, toggle that line */
1594 if (tty->termios->c_cflag & CRTSCTS) {
1595 mos7840_port->shadowMCR |= MCR_RTS;
Alan Cox95da3102008-07-22 11:09:07 +01001596 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001597 mos7840_port->shadowMCR);
Alan Cox95da3102008-07-22 11:09:07 +01001598 if (status < 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001599 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001600 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001601}
1602
Alan Cox95da3102008-07-22 11:09:07 +01001603static int mos7840_tiocmget(struct tty_struct *tty, struct file *file)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001604{
Alan Cox95da3102008-07-22 11:09:07 +01001605 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001606 struct moschip_port *mos7840_port;
1607 unsigned int result;
1608 __u16 msr;
1609 __u16 mcr;
Alan Cox880af9d2008-07-22 11:16:12 +01001610 int status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001611 mos7840_port = mos7840_get_port_private(port);
1612
Harvey Harrison441b62c2008-03-03 16:08:34 -08001613 dbg("%s - port %d", __func__, port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001614
1615 if (mos7840_port == NULL)
1616 return -ENODEV;
1617
1618 status = mos7840_get_uart_reg(port, MODEM_STATUS_REGISTER, &msr);
1619 status = mos7840_get_uart_reg(port, MODEM_CONTROL_REGISTER, &mcr);
1620 result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0)
1621 | ((mcr & MCR_RTS) ? TIOCM_RTS : 0)
1622 | ((mcr & MCR_LOOPBACK) ? TIOCM_LOOP : 0)
1623 | ((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0)
1624 | ((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0)
1625 | ((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0)
1626 | ((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0);
1627
Harvey Harrison441b62c2008-03-03 16:08:34 -08001628 dbg("%s - 0x%04X", __func__, result);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001629
1630 return result;
1631}
1632
Alan Cox95da3102008-07-22 11:09:07 +01001633static int mos7840_tiocmset(struct tty_struct *tty, struct file *file,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001634 unsigned int set, unsigned int clear)
1635{
Alan Cox95da3102008-07-22 11:09:07 +01001636 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001637 struct moschip_port *mos7840_port;
1638 unsigned int mcr;
Roel Kluin87521c42008-04-17 06:16:24 +02001639 int status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001640
Harvey Harrison441b62c2008-03-03 16:08:34 -08001641 dbg("%s - port %d", __func__, port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001642
1643 mos7840_port = mos7840_get_port_private(port);
1644
1645 if (mos7840_port == NULL)
1646 return -ENODEV;
1647
Alan Coxe2984492008-02-20 20:51:45 +00001648 /* FIXME: What locks the port registers ? */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001649 mcr = mos7840_port->shadowMCR;
1650 if (clear & TIOCM_RTS)
1651 mcr &= ~MCR_RTS;
1652 if (clear & TIOCM_DTR)
1653 mcr &= ~MCR_DTR;
1654 if (clear & TIOCM_LOOP)
1655 mcr &= ~MCR_LOOPBACK;
1656
1657 if (set & TIOCM_RTS)
1658 mcr |= MCR_RTS;
1659 if (set & TIOCM_DTR)
1660 mcr |= MCR_DTR;
1661 if (set & TIOCM_LOOP)
1662 mcr |= MCR_LOOPBACK;
1663
1664 mos7840_port->shadowMCR = mcr;
1665
Paul B Schroeder3f542972006-08-31 19:41:47 -05001666 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, mcr);
1667 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301668 dbg("setting MODEM_CONTROL_REGISTER Failed");
Roel Kluin87521c42008-04-17 06:16:24 +02001669 return status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001670 }
1671
1672 return 0;
1673}
1674
1675/*****************************************************************************
1676 * mos7840_calc_baud_rate_divisor
1677 * this function calculates the proper baud rate divisor for the specified
1678 * baud rate.
1679 *****************************************************************************/
1680static int mos7840_calc_baud_rate_divisor(int baudRate, int *divisor,
Alan Cox880af9d2008-07-22 11:16:12 +01001681 __u16 *clk_sel_val)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001682{
1683
Harvey Harrison441b62c2008-03-03 16:08:34 -08001684 dbg("%s - %d", __func__, baudRate);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001685
1686 if (baudRate <= 115200) {
1687 *divisor = 115200 / baudRate;
1688 *clk_sel_val = 0x0;
1689 }
1690 if ((baudRate > 115200) && (baudRate <= 230400)) {
1691 *divisor = 230400 / baudRate;
1692 *clk_sel_val = 0x10;
1693 } else if ((baudRate > 230400) && (baudRate <= 403200)) {
1694 *divisor = 403200 / baudRate;
1695 *clk_sel_val = 0x20;
1696 } else if ((baudRate > 403200) && (baudRate <= 460800)) {
1697 *divisor = 460800 / baudRate;
1698 *clk_sel_val = 0x30;
1699 } else if ((baudRate > 460800) && (baudRate <= 806400)) {
1700 *divisor = 806400 / baudRate;
1701 *clk_sel_val = 0x40;
1702 } else if ((baudRate > 806400) && (baudRate <= 921600)) {
1703 *divisor = 921600 / baudRate;
1704 *clk_sel_val = 0x50;
1705 } else if ((baudRate > 921600) && (baudRate <= 1572864)) {
1706 *divisor = 1572864 / baudRate;
1707 *clk_sel_val = 0x60;
1708 } else if ((baudRate > 1572864) && (baudRate <= 3145728)) {
1709 *divisor = 3145728 / baudRate;
1710 *clk_sel_val = 0x70;
1711 }
1712 return 0;
1713
1714#ifdef NOTMCS7840
1715
1716 for (i = 0; i < ARRAY_SIZE(mos7840_divisor_table); i++) {
1717 if (mos7840_divisor_table[i].BaudRate == baudrate) {
1718 *divisor = mos7840_divisor_table[i].Divisor;
1719 return 0;
1720 }
1721 }
1722
1723 /* After trying for all the standard baud rates *
1724 * Try calculating the divisor for this baud rate */
1725
1726 if (baudrate > 75 && baudrate < 230400) {
1727 /* get the divisor */
1728 custom = (__u16) (230400L / baudrate);
1729
1730 /* Check for round off */
1731 round1 = (__u16) (2304000L / baudrate);
1732 round = (__u16) (round1 - (custom * 10));
Alan Cox880af9d2008-07-22 11:16:12 +01001733 if (round > 4)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001734 custom++;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001735 *divisor = custom;
1736
Tony Cook84fe6e72009-04-18 22:55:06 +09301737 dbg(" Baud %d = %d", baudrate, custom);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001738 return 0;
1739 }
1740
Tony Cook84fe6e72009-04-18 22:55:06 +09301741 dbg("%s", " Baud calculation Failed...");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001742 return -1;
1743#endif
1744}
1745
1746/*****************************************************************************
1747 * mos7840_send_cmd_write_baud_rate
1748 * this function sends the proper command to change the baud rate of the
1749 * specified port.
1750 *****************************************************************************/
1751
1752static int mos7840_send_cmd_write_baud_rate(struct moschip_port *mos7840_port,
1753 int baudRate)
1754{
1755 int divisor = 0;
1756 int status;
1757 __u16 Data;
1758 unsigned char number;
1759 __u16 clk_sel_val;
1760 struct usb_serial_port *port;
1761
1762 if (mos7840_port == NULL)
1763 return -1;
1764
1765 port = (struct usb_serial_port *)mos7840_port->port;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001766 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301767 dbg("%s", "Invalid port");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001768 return -1;
1769 }
1770
Harvey Harrison441b62c2008-03-03 16:08:34 -08001771 if (mos7840_serial_paranoia_check(port->serial, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301772 dbg("%s", "Invalid Serial");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001773 return -1;
1774 }
1775
Tony Cook84fe6e72009-04-18 22:55:06 +09301776 dbg("%s", "Entering ..........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001777
1778 number = mos7840_port->port->number - mos7840_port->port->serial->minor;
1779
Harvey Harrison441b62c2008-03-03 16:08:34 -08001780 dbg("%s - port = %d, baud = %d", __func__,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001781 mos7840_port->port->number, baudRate);
Alan Cox880af9d2008-07-22 11:16:12 +01001782 /* reset clk_uart_sel in spregOffset */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001783 if (baudRate > 115200) {
1784#ifdef HW_flow_control
Alan Cox880af9d2008-07-22 11:16:12 +01001785 /* NOTE: need to see the pther register to modify */
1786 /* setting h/w flow control bit to 1 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001787 Data = 0x2b;
1788 mos7840_port->shadowMCR = Data;
Alan Cox880af9d2008-07-22 11:16:12 +01001789 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1790 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001791 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301792 dbg("Writing spreg failed in set_serial_baud");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001793 return -1;
1794 }
1795#endif
1796
1797 } else {
1798#ifdef HW_flow_control
Alan Cox880af9d2008-07-22 11:16:12 +01001799 / *setting h/w flow control bit to 0 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001800 Data = 0xb;
1801 mos7840_port->shadowMCR = Data;
Alan Cox880af9d2008-07-22 11:16:12 +01001802 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1803 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001804 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301805 dbg("Writing spreg failed in set_serial_baud");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001806 return -1;
1807 }
1808#endif
1809
1810 }
1811
Alan Cox880af9d2008-07-22 11:16:12 +01001812 if (1) { /* baudRate <= 115200) */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001813 clk_sel_val = 0x0;
1814 Data = 0x0;
Alan Cox880af9d2008-07-22 11:16:12 +01001815 status = mos7840_calc_baud_rate_divisor(baudRate, &divisor,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001816 &clk_sel_val);
Alan Cox880af9d2008-07-22 11:16:12 +01001817 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset,
1818 &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001819 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301820 dbg("reading spreg failed in set_serial_baud");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001821 return -1;
1822 }
1823 Data = (Data & 0x8f) | clk_sel_val;
Alan Cox880af9d2008-07-22 11:16:12 +01001824 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset,
1825 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001826 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301827 dbg("Writing spreg failed in set_serial_baud");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001828 return -1;
1829 }
1830 /* Calculate the Divisor */
1831
1832 if (status) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001833 dev_err(&port->dev, "%s - bad baud rate\n", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001834 return status;
1835 }
1836 /* Enable access to divisor latch */
1837 Data = mos7840_port->shadowLCR | SERIAL_LCR_DLAB;
1838 mos7840_port->shadowLCR = Data;
1839 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1840
1841 /* Write the divisor */
1842 Data = (unsigned char)(divisor & 0xff);
Tony Cook84fe6e72009-04-18 22:55:06 +09301843 dbg("set_serial_baud Value to write DLL is %x", Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001844 mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
1845
1846 Data = (unsigned char)((divisor & 0xff00) >> 8);
Tony Cook84fe6e72009-04-18 22:55:06 +09301847 dbg("set_serial_baud Value to write DLM is %x", Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001848 mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
1849
1850 /* Disable access to divisor latch */
1851 Data = mos7840_port->shadowLCR & ~SERIAL_LCR_DLAB;
1852 mos7840_port->shadowLCR = Data;
1853 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1854
1855 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001856 return status;
1857}
1858
1859/*****************************************************************************
1860 * mos7840_change_port_settings
1861 * This routine is called to set the UART on the device to match
1862 * the specified new settings.
1863 *****************************************************************************/
1864
Alan Cox95da3102008-07-22 11:09:07 +01001865static void mos7840_change_port_settings(struct tty_struct *tty,
1866 struct moschip_port *mos7840_port, struct ktermios *old_termios)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001867{
Paul B Schroeder3f542972006-08-31 19:41:47 -05001868 int baud;
1869 unsigned cflag;
1870 unsigned iflag;
1871 __u8 lData;
1872 __u8 lParity;
1873 __u8 lStop;
1874 int status;
1875 __u16 Data;
1876 struct usb_serial_port *port;
1877 struct usb_serial *serial;
1878
1879 if (mos7840_port == NULL)
1880 return;
1881
1882 port = (struct usb_serial_port *)mos7840_port->port;
1883
Harvey Harrison441b62c2008-03-03 16:08:34 -08001884 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301885 dbg("%s", "Invalid port");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001886 return;
1887 }
1888
Harvey Harrison441b62c2008-03-03 16:08:34 -08001889 if (mos7840_serial_paranoia_check(port->serial, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301890 dbg("%s", "Invalid Serial");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001891 return;
1892 }
1893
1894 serial = port->serial;
1895
Harvey Harrison441b62c2008-03-03 16:08:34 -08001896 dbg("%s - port %d", __func__, mos7840_port->port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001897
1898 if (!mos7840_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001899 dbg("%s - port not opened", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001900 return;
1901 }
1902
Tony Cook84fe6e72009-04-18 22:55:06 +09301903 dbg("%s", "Entering ..........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001904
1905 lData = LCR_BITS_8;
1906 lStop = LCR_STOP_1;
1907 lParity = LCR_PAR_NONE;
1908
1909 cflag = tty->termios->c_cflag;
1910 iflag = tty->termios->c_iflag;
1911
1912 /* Change the number of bits */
1913 if (cflag & CSIZE) {
1914 switch (cflag & CSIZE) {
1915 case CS5:
1916 lData = LCR_BITS_5;
1917 break;
1918
1919 case CS6:
1920 lData = LCR_BITS_6;
1921 break;
1922
1923 case CS7:
1924 lData = LCR_BITS_7;
1925 break;
1926 default:
1927 case CS8:
1928 lData = LCR_BITS_8;
1929 break;
1930 }
1931 }
1932 /* Change the Parity bit */
1933 if (cflag & PARENB) {
1934 if (cflag & PARODD) {
1935 lParity = LCR_PAR_ODD;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001936 dbg("%s - parity = odd", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001937 } else {
1938 lParity = LCR_PAR_EVEN;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001939 dbg("%s - parity = even", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001940 }
1941
1942 } else {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001943 dbg("%s - parity = none", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001944 }
1945
Alan Cox880af9d2008-07-22 11:16:12 +01001946 if (cflag & CMSPAR)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001947 lParity = lParity | 0x20;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001948
1949 /* Change the Stop bit */
1950 if (cflag & CSTOPB) {
1951 lStop = LCR_STOP_2;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001952 dbg("%s - stop bits = 2", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001953 } else {
1954 lStop = LCR_STOP_1;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001955 dbg("%s - stop bits = 1", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001956 }
1957
1958 /* Update the LCR with the correct value */
1959 mos7840_port->shadowLCR &=
1960 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
1961 mos7840_port->shadowLCR |= (lData | lParity | lStop);
1962
Tony Cook84fe6e72009-04-18 22:55:06 +09301963 dbg("mos7840_change_port_settings mos7840_port->shadowLCR is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05001964 mos7840_port->shadowLCR);
1965 /* Disable Interrupts */
1966 Data = 0x00;
1967 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1968
1969 Data = 0x00;
1970 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
1971
1972 Data = 0xcf;
1973 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
1974
1975 /* Send the updated LCR value to the mos7840 */
1976 Data = mos7840_port->shadowLCR;
1977
1978 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1979
1980 Data = 0x00b;
1981 mos7840_port->shadowMCR = Data;
1982 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1983 Data = 0x00b;
1984 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1985
1986 /* set up the MCR register and send it to the mos7840 */
1987
1988 mos7840_port->shadowMCR = MCR_MASTER_IE;
Alan Cox880af9d2008-07-22 11:16:12 +01001989 if (cflag & CBAUD)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001990 mos7840_port->shadowMCR |= (MCR_DTR | MCR_RTS);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001991
Alan Cox880af9d2008-07-22 11:16:12 +01001992 if (cflag & CRTSCTS)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001993 mos7840_port->shadowMCR |= (MCR_XON_ANY);
Alan Cox880af9d2008-07-22 11:16:12 +01001994 else
Paul B Schroeder3f542972006-08-31 19:41:47 -05001995 mos7840_port->shadowMCR &= ~(MCR_XON_ANY);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001996
1997 Data = mos7840_port->shadowMCR;
1998 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1999
2000 /* Determine divisor based on baud rate */
2001 baud = tty_get_baud_rate(tty);
2002
2003 if (!baud) {
2004 /* pick a default, any default... */
Tony Cook84fe6e72009-04-18 22:55:06 +09302005 dbg("%s", "Picked default baud...");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002006 baud = 9600;
2007 }
2008
Harvey Harrison441b62c2008-03-03 16:08:34 -08002009 dbg("%s - baud rate = %d", __func__, baud);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002010 status = mos7840_send_cmd_write_baud_rate(mos7840_port, baud);
2011
2012 /* Enable Interrupts */
2013 Data = 0x0c;
2014 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
2015
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002016 if (mos7840_port->read_urb_busy == false) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002017 mos7840_port->read_urb->dev = serial->dev;
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002018 mos7840_port->read_urb_busy = true;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002019 status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002020 if (status) {
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002021 dbg("usb_submit_urb(read bulk) failed, status = %d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002022 status);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002023 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002024 }
2025 }
2026 wake_up(&mos7840_port->delta_msr_wait);
2027 mos7840_port->delta_msr_cond = 1;
Tony Cook84fe6e72009-04-18 22:55:06 +09302028 dbg("mos7840_change_port_settings mos7840_port->shadowLCR is End %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002029 mos7840_port->shadowLCR);
2030
2031 return;
2032}
2033
2034/*****************************************************************************
2035 * mos7840_set_termios
2036 * this function is called by the tty driver when it wants to change
2037 * the termios structure
2038 *****************************************************************************/
2039
Alan Cox95da3102008-07-22 11:09:07 +01002040static void mos7840_set_termios(struct tty_struct *tty,
2041 struct usb_serial_port *port,
Alan Cox606d0992006-12-08 02:38:45 -08002042 struct ktermios *old_termios)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002043{
2044 int status;
2045 unsigned int cflag;
2046 struct usb_serial *serial;
2047 struct moschip_port *mos7840_port;
Tony Cook84fe6e72009-04-18 22:55:06 +09302048 dbg("mos7840_set_termios: START");
Harvey Harrison441b62c2008-03-03 16:08:34 -08002049 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302050 dbg("%s", "Invalid port");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002051 return;
2052 }
2053
2054 serial = port->serial;
2055
Harvey Harrison441b62c2008-03-03 16:08:34 -08002056 if (mos7840_serial_paranoia_check(serial, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302057 dbg("%s", "Invalid Serial");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002058 return;
2059 }
2060
2061 mos7840_port = mos7840_get_port_private(port);
2062
2063 if (mos7840_port == NULL)
2064 return;
2065
Paul B Schroeder3f542972006-08-31 19:41:47 -05002066 if (!mos7840_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002067 dbg("%s - port not opened", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002068 return;
2069 }
2070
Tony Cook84fe6e72009-04-18 22:55:06 +09302071 dbg("%s", "setting termios - ");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002072
2073 cflag = tty->termios->c_cflag;
2074
Harvey Harrison441b62c2008-03-03 16:08:34 -08002075 dbg("%s - clfag %08x iflag %08x", __func__,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002076 tty->termios->c_cflag, RELEVANT_IFLAG(tty->termios->c_iflag));
Harvey Harrison441b62c2008-03-03 16:08:34 -08002077 dbg("%s - old clfag %08x old iflag %08x", __func__,
Alan Cox3d3ddce2007-10-15 20:53:35 +01002078 old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag));
Harvey Harrison441b62c2008-03-03 16:08:34 -08002079 dbg("%s - port %d", __func__, port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002080
2081 /* change the port settings to the new ones specified */
2082
Alan Cox95da3102008-07-22 11:09:07 +01002083 mos7840_change_port_settings(tty, mos7840_port, old_termios);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002084
2085 if (!mos7840_port->read_urb) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302086 dbg("%s", "URB KILLED !!!!!");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002087 return;
2088 }
2089
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002090 if (mos7840_port->read_urb_busy == false) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002091 mos7840_port->read_urb->dev = serial->dev;
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002092 mos7840_port->read_urb_busy = true;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002093 status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
2094 if (status) {
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002095 dbg("usb_submit_urb(read bulk) failed, status = %d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002096 status);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002097 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002098 }
2099 }
2100 return;
2101}
2102
2103/*****************************************************************************
2104 * mos7840_get_lsr_info - get line status register info
2105 *
2106 * Purpose: Let user call ioctl() to get info when the UART physically
2107 * is emptied. On bus types like RS485, the transmitter must
2108 * release the bus after transmitting. This must be done when
2109 * the transmit shift register is empty, not be done when the
2110 * transmit holding register is empty. This functionality
2111 * allows an RS485 driver to be written in user space.
2112 *****************************************************************************/
2113
Alan Cox95da3102008-07-22 11:09:07 +01002114static int mos7840_get_lsr_info(struct tty_struct *tty,
Al Viro97c49652006-10-09 20:29:03 +01002115 unsigned int __user *value)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002116{
2117 int count;
2118 unsigned int result = 0;
2119
Alan Cox95da3102008-07-22 11:09:07 +01002120 count = mos7840_chars_in_buffer(tty);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002121 if (count == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002122 dbg("%s -- Empty", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002123 result = TIOCSER_TEMT;
2124 }
2125
2126 if (copy_to_user(value, &result, sizeof(int)))
2127 return -EFAULT;
2128 return 0;
2129}
2130
2131/*****************************************************************************
Paul B Schroeder3f542972006-08-31 19:41:47 -05002132 * mos7840_set_modem_info
2133 * function to set modem info
2134 *****************************************************************************/
2135
Alan Cox95da3102008-07-22 11:09:07 +01002136/* FIXME: Should be using the model control hooks */
2137
Paul B Schroeder3f542972006-08-31 19:41:47 -05002138static int mos7840_set_modem_info(struct moschip_port *mos7840_port,
Al Viro97c49652006-10-09 20:29:03 +01002139 unsigned int cmd, unsigned int __user *value)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002140{
2141 unsigned int mcr;
2142 unsigned int arg;
2143 __u16 Data;
2144 int status;
2145 struct usb_serial_port *port;
2146
2147 if (mos7840_port == NULL)
2148 return -1;
2149
2150 port = (struct usb_serial_port *)mos7840_port->port;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002151 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302152 dbg("%s", "Invalid port");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002153 return -1;
2154 }
2155
2156 mcr = mos7840_port->shadowMCR;
2157
2158 if (copy_from_user(&arg, value, sizeof(int)))
2159 return -EFAULT;
2160
2161 switch (cmd) {
2162 case TIOCMBIS:
2163 if (arg & TIOCM_RTS)
2164 mcr |= MCR_RTS;
2165 if (arg & TIOCM_DTR)
2166 mcr |= MCR_RTS;
2167 if (arg & TIOCM_LOOP)
2168 mcr |= MCR_LOOPBACK;
2169 break;
2170
2171 case TIOCMBIC:
2172 if (arg & TIOCM_RTS)
2173 mcr &= ~MCR_RTS;
2174 if (arg & TIOCM_DTR)
2175 mcr &= ~MCR_RTS;
2176 if (arg & TIOCM_LOOP)
2177 mcr &= ~MCR_LOOPBACK;
2178 break;
2179
2180 case TIOCMSET:
2181 /* turn off the RTS and DTR and LOOPBACK
2182 * and then only turn on what was asked to */
2183 mcr &= ~(MCR_RTS | MCR_DTR | MCR_LOOPBACK);
2184 mcr |= ((arg & TIOCM_RTS) ? MCR_RTS : 0);
2185 mcr |= ((arg & TIOCM_DTR) ? MCR_DTR : 0);
2186 mcr |= ((arg & TIOCM_LOOP) ? MCR_LOOPBACK : 0);
2187 break;
2188 }
2189
Alan Cox6b447f042009-01-02 13:48:56 +00002190 lock_kernel();
Paul B Schroeder3f542972006-08-31 19:41:47 -05002191 mos7840_port->shadowMCR = mcr;
2192
2193 Data = mos7840_port->shadowMCR;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002194 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
Alan Cox6b447f042009-01-02 13:48:56 +00002195 unlock_kernel();
Paul B Schroeder3f542972006-08-31 19:41:47 -05002196 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302197 dbg("setting MODEM_CONTROL_REGISTER Failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002198 return -1;
2199 }
2200
2201 return 0;
2202}
2203
2204/*****************************************************************************
2205 * mos7840_get_modem_info
2206 * function to get modem info
2207 *****************************************************************************/
2208
2209static int mos7840_get_modem_info(struct moschip_port *mos7840_port,
Al Viro97c49652006-10-09 20:29:03 +01002210 unsigned int __user *value)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002211{
2212 unsigned int result = 0;
2213 __u16 msr;
2214 unsigned int mcr = mos7840_port->shadowMCR;
Alan Cox880af9d2008-07-22 11:16:12 +01002215 mos7840_get_uart_reg(mos7840_port->port,
Alan Cox95da3102008-07-22 11:09:07 +01002216 MODEM_STATUS_REGISTER, &msr);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002217 result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0) /* 0x002 */
2218 |((mcr & MCR_RTS) ? TIOCM_RTS : 0) /* 0x004 */
2219 |((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0) /* 0x020 */
2220 |((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0) /* 0x040 */
2221 |((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0) /* 0x080 */
2222 |((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0); /* 0x100 */
2223
Harvey Harrison441b62c2008-03-03 16:08:34 -08002224 dbg("%s -- %x", __func__, result);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002225
2226 if (copy_to_user(value, &result, sizeof(int)))
2227 return -EFAULT;
2228 return 0;
2229}
2230
2231/*****************************************************************************
2232 * mos7840_get_serial_info
2233 * function to get information about serial port
2234 *****************************************************************************/
2235
2236static int mos7840_get_serial_info(struct moschip_port *mos7840_port,
Al Viro97c49652006-10-09 20:29:03 +01002237 struct serial_struct __user *retinfo)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002238{
2239 struct serial_struct tmp;
2240
2241 if (mos7840_port == NULL)
2242 return -1;
2243
2244 if (!retinfo)
2245 return -EFAULT;
2246
2247 memset(&tmp, 0, sizeof(tmp));
2248
2249 tmp.type = PORT_16550A;
2250 tmp.line = mos7840_port->port->serial->minor;
2251 tmp.port = mos7840_port->port->number;
2252 tmp.irq = 0;
2253 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
2254 tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
2255 tmp.baud_base = 9600;
2256 tmp.close_delay = 5 * HZ;
2257 tmp.closing_wait = 30 * HZ;
2258
2259 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2260 return -EFAULT;
2261 return 0;
2262}
2263
2264/*****************************************************************************
2265 * SerialIoctl
2266 * this function handles any ioctl calls to the driver
2267 *****************************************************************************/
2268
Alan Cox95da3102008-07-22 11:09:07 +01002269static int mos7840_ioctl(struct tty_struct *tty, struct file *file,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002270 unsigned int cmd, unsigned long arg)
2271{
Alan Cox95da3102008-07-22 11:09:07 +01002272 struct usb_serial_port *port = tty->driver_data;
Al Viro97c49652006-10-09 20:29:03 +01002273 void __user *argp = (void __user *)arg;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002274 struct moschip_port *mos7840_port;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002275
2276 struct async_icount cnow;
2277 struct async_icount cprev;
2278 struct serial_icounter_struct icount;
2279 int mosret = 0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002280
Harvey Harrison441b62c2008-03-03 16:08:34 -08002281 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302282 dbg("%s", "Invalid port");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002283 return -1;
2284 }
2285
2286 mos7840_port = mos7840_get_port_private(port);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002287
2288 if (mos7840_port == NULL)
2289 return -1;
2290
Harvey Harrison441b62c2008-03-03 16:08:34 -08002291 dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002292
2293 switch (cmd) {
2294 /* return number of bytes available */
2295
Paul B Schroeder3f542972006-08-31 19:41:47 -05002296 case TIOCSERGETLSR:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002297 dbg("%s (%d) TIOCSERGETLSR", __func__, port->number);
Alan Cox95da3102008-07-22 11:09:07 +01002298 return mos7840_get_lsr_info(tty, argp);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002299 return 0;
2300
Alan Cox95da3102008-07-22 11:09:07 +01002301 /* FIXME: use the modem hooks and remove this */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002302 case TIOCMBIS:
2303 case TIOCMBIC:
2304 case TIOCMSET:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002305 dbg("%s (%d) TIOCMSET/TIOCMBIC/TIOCMSET", __func__,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002306 port->number);
2307 mosret =
Al Viro97c49652006-10-09 20:29:03 +01002308 mos7840_set_modem_info(mos7840_port, cmd, argp);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002309 return mosret;
2310
2311 case TIOCMGET:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002312 dbg("%s (%d) TIOCMGET", __func__, port->number);
Al Viro97c49652006-10-09 20:29:03 +01002313 return mos7840_get_modem_info(mos7840_port, argp);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002314
2315 case TIOCGSERIAL:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002316 dbg("%s (%d) TIOCGSERIAL", __func__, port->number);
Al Viro97c49652006-10-09 20:29:03 +01002317 return mos7840_get_serial_info(mos7840_port, argp);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002318
2319 case TIOCSSERIAL:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002320 dbg("%s (%d) TIOCSSERIAL", __func__, port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002321 break;
2322
2323 case TIOCMIWAIT:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002324 dbg("%s (%d) TIOCMIWAIT", __func__, port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002325 cprev = mos7840_port->icount;
2326 while (1) {
Alan Cox880af9d2008-07-22 11:16:12 +01002327 /* interruptible_sleep_on(&mos7840_port->delta_msr_wait); */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002328 mos7840_port->delta_msr_cond = 0;
2329 wait_event_interruptible(mos7840_port->delta_msr_wait,
2330 (mos7840_port->
2331 delta_msr_cond == 1));
2332
2333 /* see if a signal did it */
2334 if (signal_pending(current))
2335 return -ERESTARTSYS;
2336 cnow = mos7840_port->icount;
Oliver Neukum0de9a702007-03-16 20:28:28 +01002337 smp_rmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -05002338 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2339 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
2340 return -EIO; /* no change => error */
2341 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
2342 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
2343 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
2344 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
2345 return 0;
2346 }
2347 cprev = cnow;
2348 }
2349 /* NOTREACHED */
2350 break;
2351
2352 case TIOCGICOUNT:
2353 cnow = mos7840_port->icount;
Oliver Neukum0de9a702007-03-16 20:28:28 +01002354 smp_rmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -05002355 icount.cts = cnow.cts;
2356 icount.dsr = cnow.dsr;
2357 icount.rng = cnow.rng;
2358 icount.dcd = cnow.dcd;
2359 icount.rx = cnow.rx;
2360 icount.tx = cnow.tx;
2361 icount.frame = cnow.frame;
2362 icount.overrun = cnow.overrun;
2363 icount.parity = cnow.parity;
2364 icount.brk = cnow.brk;
2365 icount.buf_overrun = cnow.buf_overrun;
2366
Harvey Harrison441b62c2008-03-03 16:08:34 -08002367 dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __func__,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002368 port->number, icount.rx, icount.tx);
Al Viro97c49652006-10-09 20:29:03 +01002369 if (copy_to_user(argp, &icount, sizeof(icount)))
Paul B Schroeder3f542972006-08-31 19:41:47 -05002370 return -EFAULT;
2371 return 0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002372 default:
2373 break;
2374 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002375 return -ENOIOCTLCMD;
2376}
2377
2378static int mos7840_calc_num_ports(struct usb_serial *serial)
2379{
Oliver Neukum0de9a702007-03-16 20:28:28 +01002380 int mos7840_num_ports = 0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002381
Tony Cook84fe6e72009-04-18 22:55:06 +09302382 dbg("numberofendpoints: cur %d, alt %d",
2383 (int)serial->interface->cur_altsetting->desc.bNumEndpoints,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002384 (int)serial->interface->altsetting->desc.bNumEndpoints);
2385 if (serial->interface->cur_altsetting->desc.bNumEndpoints == 5) {
Oliver Neukum0de9a702007-03-16 20:28:28 +01002386 mos7840_num_ports = serial->num_ports = 2;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002387 } else if (serial->interface->cur_altsetting->desc.bNumEndpoints == 9) {
Oliver Neukum0de9a702007-03-16 20:28:28 +01002388 serial->num_bulk_in = 4;
2389 serial->num_bulk_out = 4;
2390 mos7840_num_ports = serial->num_ports = 4;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002391 }
Tony Cook84fe6e72009-04-18 22:55:06 +09302392 dbg ("mos7840_num_ports = %d", mos7840_num_ports);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002393 return mos7840_num_ports;
2394}
2395
2396/****************************************************************************
2397 * mos7840_startup
2398 ****************************************************************************/
2399
2400static int mos7840_startup(struct usb_serial *serial)
2401{
2402 struct moschip_port *mos7840_port;
2403 struct usb_device *dev;
2404 int i, status;
2405
2406 __u16 Data;
Tony Cook84fe6e72009-04-18 22:55:06 +09302407 dbg("%s", "mos7840_startup :Entering..........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002408
2409 if (!serial) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302410 dbg("%s", "Invalid Handler");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002411 return -1;
2412 }
2413
2414 dev = serial->dev;
2415
Tony Cook84fe6e72009-04-18 22:55:06 +09302416 dbg("%s", "Entering...");
2417 dbg ("mos7840_startup: serial = %p", serial);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002418
2419 /* we set up the pointers to the endpoints in the mos7840_open *
2420 * function, as the structures aren't created yet. */
2421
2422 /* set up port private structures */
2423 for (i = 0; i < serial->num_ports; ++i) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302424 dbg ("mos7840_startup: configuring port %d............", i);
Burman Yan7ac9da12006-11-22 20:54:38 +02002425 mos7840_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002426 if (mos7840_port == NULL) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07002427 dev_err(&dev->dev, "%s - Out of memory\n", __func__);
Oliver Neukum0de9a702007-03-16 20:28:28 +01002428 status = -ENOMEM;
2429 i--; /* don't follow NULL pointer cleaning up */
2430 goto error;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002431 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002432
Alan Cox880af9d2008-07-22 11:16:12 +01002433 /* Initialize all port interrupt end point to port 0 int
2434 * endpoint. Our device has only one interrupt end point
2435 * common to all port */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002436
2437 mos7840_port->port = serial->port[i];
2438 mos7840_set_port_private(serial->port[i], mos7840_port);
Oliver Neukum0de9a702007-03-16 20:28:28 +01002439 spin_lock_init(&mos7840_port->pool_lock);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002440
Tony Cook37768ad2009-04-18 22:42:18 +09302441 /* minor is not initialised until later by
2442 * usb-serial.c:get_free_serial() and cannot therefore be used
2443 * to index device instances */
2444 mos7840_port->port_num = i + 1;
2445 dbg ("serial->port[i]->number = %d", serial->port[i]->number);
2446 dbg ("serial->port[i]->serial->minor = %d", serial->port[i]->serial->minor);
2447 dbg ("mos7840_port->port_num = %d", mos7840_port->port_num);
2448 dbg ("serial->minor = %d", serial->minor);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002449
2450 if (mos7840_port->port_num == 1) {
2451 mos7840_port->SpRegOffset = 0x0;
2452 mos7840_port->ControlRegOffset = 0x1;
2453 mos7840_port->DcrRegOffset = 0x4;
2454 } else if ((mos7840_port->port_num == 2)
Oliver Neukum0de9a702007-03-16 20:28:28 +01002455 && (serial->num_ports == 4)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002456 mos7840_port->SpRegOffset = 0x8;
2457 mos7840_port->ControlRegOffset = 0x9;
2458 mos7840_port->DcrRegOffset = 0x16;
2459 } else if ((mos7840_port->port_num == 2)
Oliver Neukum0de9a702007-03-16 20:28:28 +01002460 && (serial->num_ports == 2)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002461 mos7840_port->SpRegOffset = 0xa;
2462 mos7840_port->ControlRegOffset = 0xb;
2463 mos7840_port->DcrRegOffset = 0x19;
2464 } else if ((mos7840_port->port_num == 3)
Oliver Neukum0de9a702007-03-16 20:28:28 +01002465 && (serial->num_ports == 4)) {
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 == 4)
Oliver Neukum0de9a702007-03-16 20:28:28 +01002470 && (serial->num_ports == 4)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002471 mos7840_port->SpRegOffset = 0xc;
2472 mos7840_port->ControlRegOffset = 0xd;
2473 mos7840_port->DcrRegOffset = 0x1c;
2474 }
2475 mos7840_dump_serial_port(mos7840_port);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002476 mos7840_set_port_private(serial->port[i], mos7840_port);
2477
Alan Cox880af9d2008-07-22 11:16:12 +01002478 /* enable rx_disable bit in control register */
2479 status = mos7840_get_reg_sync(serial->port[i],
2480 mos7840_port->ControlRegOffset, &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002481 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302482 dbg("Reading ControlReg failed status-0x%x", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002483 break;
2484 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302485 dbg("ControlReg Reading success val is %x, status%d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002486 Data, status);
Alan Cox880af9d2008-07-22 11:16:12 +01002487 Data |= 0x08; /* setting driver done bit */
2488 Data |= 0x04; /* sp1_bit to have cts change reflect in
2489 modem status reg */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002490
Alan Cox880af9d2008-07-22 11:16:12 +01002491 /* Data |= 0x20; //rx_disable bit */
2492 status = mos7840_set_reg_sync(serial->port[i],
Paul B Schroeder3f542972006-08-31 19:41:47 -05002493 mos7840_port->ControlRegOffset, Data);
2494 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302495 dbg("Writing ControlReg failed(rx_disable) status-0x%x", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002496 break;
2497 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302498 dbg("ControlReg Writing success(rx_disable) status%d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002499 status);
2500
Alan Cox880af9d2008-07-22 11:16:12 +01002501 /* Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2
2502 and 0x24 in DCR3 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002503 Data = 0x01;
Alan Cox880af9d2008-07-22 11:16:12 +01002504 status = mos7840_set_reg_sync(serial->port[i],
2505 (__u16) (mos7840_port->DcrRegOffset + 0), Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002506 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302507 dbg("Writing DCR0 failed status-0x%x", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002508 break;
2509 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302510 dbg("DCR0 Writing success status%d", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002511
2512 Data = 0x05;
Alan Cox880af9d2008-07-22 11:16:12 +01002513 status = mos7840_set_reg_sync(serial->port[i],
2514 (__u16) (mos7840_port->DcrRegOffset + 1), Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002515 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302516 dbg("Writing DCR1 failed status-0x%x", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002517 break;
2518 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302519 dbg("DCR1 Writing success status%d", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002520
2521 Data = 0x24;
Alan Cox880af9d2008-07-22 11:16:12 +01002522 status = mos7840_set_reg_sync(serial->port[i],
2523 (__u16) (mos7840_port->DcrRegOffset + 2), Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002524 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302525 dbg("Writing DCR2 failed status-0x%x", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002526 break;
2527 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302528 dbg("DCR2 Writing success status%d", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002529
Alan Cox880af9d2008-07-22 11:16:12 +01002530 /* write values in clkstart0x0 and clkmulti 0x20 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002531 Data = 0x0;
Alan Cox880af9d2008-07-22 11:16:12 +01002532 status = mos7840_set_reg_sync(serial->port[i],
Paul B Schroeder3f542972006-08-31 19:41:47 -05002533 CLK_START_VALUE_REGISTER, Data);
2534 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302535 dbg("Writing CLK_START_VALUE_REGISTER failed status-0x%x", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002536 break;
2537 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302538 dbg("CLK_START_VALUE_REGISTER Writing success status%d", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002539
2540 Data = 0x20;
Alan Cox880af9d2008-07-22 11:16:12 +01002541 status = mos7840_set_reg_sync(serial->port[i],
2542 CLK_MULTI_REGISTER, Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002543 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302544 dbg("Writing CLK_MULTI_REGISTER failed status-0x%x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002545 status);
Oliver Neukum0de9a702007-03-16 20:28:28 +01002546 goto error;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002547 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302548 dbg("CLK_MULTI_REGISTER Writing success status%d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002549 status);
2550
Alan Cox880af9d2008-07-22 11:16:12 +01002551 /* write value 0x0 to scratchpad register */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002552 Data = 0x00;
Alan Cox880af9d2008-07-22 11:16:12 +01002553 status = mos7840_set_uart_reg(serial->port[i],
2554 SCRATCH_PAD_REGISTER, Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002555 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302556 dbg("Writing SCRATCH_PAD_REGISTER failed status-0x%x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002557 status);
2558 break;
2559 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302560 dbg("SCRATCH_PAD_REGISTER Writing success status%d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002561 status);
2562
Alan Cox880af9d2008-07-22 11:16:12 +01002563 /* Zero Length flag register */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002564 if ((mos7840_port->port_num != 1)
Oliver Neukum0de9a702007-03-16 20:28:28 +01002565 && (serial->num_ports == 2)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002566
2567 Data = 0xff;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002568 status = mos7840_set_reg_sync(serial->port[i],
Alan Cox880af9d2008-07-22 11:16:12 +01002569 (__u16) (ZLP_REG1 +
2570 ((__u16)mos7840_port->port_num)), Data);
Tony Cook84fe6e72009-04-18 22:55:06 +09302571 dbg("ZLIP offset %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002572 (__u16) (ZLP_REG1 +
Alan Cox880af9d2008-07-22 11:16:12 +01002573 ((__u16) mos7840_port->port_num)));
Paul B Schroeder3f542972006-08-31 19:41:47 -05002574 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302575 dbg("Writing ZLP_REG%d failed status-0x%x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002576 i + 2, status);
2577 break;
2578 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302579 dbg("ZLP_REG%d Writing success status%d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002580 i + 2, status);
2581 } else {
2582 Data = 0xff;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002583 status = mos7840_set_reg_sync(serial->port[i],
Alan Cox880af9d2008-07-22 11:16:12 +01002584 (__u16) (ZLP_REG1 +
2585 ((__u16)mos7840_port->port_num) - 0x1), Data);
Tony Cook84fe6e72009-04-18 22:55:06 +09302586 dbg("ZLIP offset %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002587 (__u16) (ZLP_REG1 +
2588 ((__u16) mos7840_port->port_num) - 0x1));
2589 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302590 dbg("Writing ZLP_REG%d failed status-0x%x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002591 i + 1, status);
2592 break;
2593 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302594 dbg("ZLP_REG%d Writing success status%d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002595 i + 1, status);
2596
2597 }
Oliver Neukum0de9a702007-03-16 20:28:28 +01002598 mos7840_port->control_urb = usb_alloc_urb(0, GFP_KERNEL);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002599 mos7840_port->ctrl_buf = kmalloc(16, GFP_KERNEL);
Alan Cox880af9d2008-07-22 11:16:12 +01002600 mos7840_port->dr = kmalloc(sizeof(struct usb_ctrlrequest),
2601 GFP_KERNEL);
2602 if (!mos7840_port->control_urb || !mos7840_port->ctrl_buf ||
2603 !mos7840_port->dr) {
Oliver Neukum0de9a702007-03-16 20:28:28 +01002604 status = -ENOMEM;
2605 goto error;
2606 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002607 }
Tony Cook84fe6e72009-04-18 22:55:06 +09302608 dbg ("mos7840_startup: all ports configured...........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002609
Alan Cox880af9d2008-07-22 11:16:12 +01002610 /* Zero Length flag enable */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002611 Data = 0x0f;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002612 status = mos7840_set_reg_sync(serial->port[0], ZLP_REG5, Data);
2613 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302614 dbg("Writing ZLP_REG5 failed status-0x%x", status);
Roel Kluin7ced46c2007-10-27 03:36:37 +02002615 goto error;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002616 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302617 dbg("ZLP_REG5 Writing success status%d", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002618
2619 /* setting configuration feature to one */
2620 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
Al Viro97c49652006-10-09 20:29:03 +01002621 (__u8) 0x03, 0x00, 0x01, 0x00, NULL, 0x00, 5 * HZ);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002622 return 0;
Oliver Neukum0de9a702007-03-16 20:28:28 +01002623error:
2624 for (/* nothing */; i >= 0; i--) {
2625 mos7840_port = mos7840_get_port_private(serial->port[i]);
2626
2627 kfree(mos7840_port->dr);
2628 kfree(mos7840_port->ctrl_buf);
2629 usb_free_urb(mos7840_port->control_urb);
2630 kfree(mos7840_port);
2631 serial->port[i] = NULL;
2632 }
2633 return status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002634}
2635
2636/****************************************************************************
Alan Sternf9c99bb2009-06-02 11:53:55 -04002637 * mos7840_disconnect
Paul B Schroeder3f542972006-08-31 19:41:47 -05002638 * This function is called whenever the device is removed from the usb bus.
2639 ****************************************************************************/
2640
Alan Sternf9c99bb2009-06-02 11:53:55 -04002641static void mos7840_disconnect(struct usb_serial *serial)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002642{
2643 int i;
Oliver Neukum0de9a702007-03-16 20:28:28 +01002644 unsigned long flags;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002645 struct moschip_port *mos7840_port;
Alan Sternf9c99bb2009-06-02 11:53:55 -04002646 dbg("%s", " disconnect :entering..........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002647
2648 if (!serial) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302649 dbg("%s", "Invalid Handler");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002650 return;
2651 }
2652
Alan Cox880af9d2008-07-22 11:16:12 +01002653 /* check for the ports to be closed,close the ports and disconnect */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002654
2655 /* free private structure allocated for serial port *
2656 * stop reads and writes on all ports */
2657
2658 for (i = 0; i < serial->num_ports; ++i) {
2659 mos7840_port = mos7840_get_port_private(serial->port[i]);
Tony Cook37768ad2009-04-18 22:42:18 +09302660 dbg ("mos7840_port %d = %p", i, mos7840_port);
2661 if (mos7840_port) {
2662 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
2663 mos7840_port->zombie = 1;
2664 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
2665 usb_kill_urb(mos7840_port->control_urb);
Alan Sternf9c99bb2009-06-02 11:53:55 -04002666 }
2667 }
2668
2669 dbg("%s", "Thank u :: ");
2670
2671}
2672
2673/****************************************************************************
2674 * mos7840_release
2675 * This function is called when the usb_serial structure is freed.
2676 ****************************************************************************/
2677
2678static void mos7840_release(struct usb_serial *serial)
2679{
2680 int i;
2681 struct moschip_port *mos7840_port;
2682 dbg("%s", " release :entering..........");
2683
2684 if (!serial) {
2685 dbg("%s", "Invalid Handler");
2686 return;
2687 }
2688
2689 /* check for the ports to be closed,close the ports and disconnect */
2690
2691 /* free private structure allocated for serial port *
2692 * stop reads and writes on all ports */
2693
2694 for (i = 0; i < serial->num_ports; ++i) {
2695 mos7840_port = mos7840_get_port_private(serial->port[i]);
2696 dbg("mos7840_port %d = %p", i, mos7840_port);
2697 if (mos7840_port) {
Tony Cook37768ad2009-04-18 22:42:18 +09302698 kfree(mos7840_port->ctrl_buf);
2699 kfree(mos7840_port->dr);
2700 kfree(mos7840_port);
2701 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002702 }
2703
Tony Cook84fe6e72009-04-18 22:55:06 +09302704 dbg("%s", "Thank u :: ");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002705
2706}
2707
Johannes Hölzld9b1b782006-12-17 21:50:24 +01002708static struct usb_driver io_driver = {
2709 .name = "mos7840",
2710 .probe = usb_serial_probe,
2711 .disconnect = usb_serial_disconnect,
2712 .id_table = moschip_id_table_combined,
2713 .no_dynamic_id = 1,
2714};
2715
Paul B Schroeder3f542972006-08-31 19:41:47 -05002716static struct usb_serial_driver moschip7840_4port_device = {
2717 .driver = {
2718 .owner = THIS_MODULE,
2719 .name = "mos7840",
2720 },
2721 .description = DRIVER_DESC,
Johannes Hölzld9b1b782006-12-17 21:50:24 +01002722 .usb_driver = &io_driver,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002723 .id_table = moschip_port_id_table,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002724 .num_ports = 4,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002725 .open = mos7840_open,
2726 .close = mos7840_close,
2727 .write = mos7840_write,
2728 .write_room = mos7840_write_room,
2729 .chars_in_buffer = mos7840_chars_in_buffer,
2730 .throttle = mos7840_throttle,
2731 .unthrottle = mos7840_unthrottle,
2732 .calc_num_ports = mos7840_calc_num_ports,
2733#ifdef MCSSerialProbe
2734 .probe = mos7840_serial_probe,
2735#endif
2736 .ioctl = mos7840_ioctl,
2737 .set_termios = mos7840_set_termios,
2738 .break_ctl = mos7840_break,
2739 .tiocmget = mos7840_tiocmget,
2740 .tiocmset = mos7840_tiocmset,
2741 .attach = mos7840_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -04002742 .disconnect = mos7840_disconnect,
2743 .release = mos7840_release,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002744 .read_bulk_callback = mos7840_bulk_in_callback,
2745 .read_int_callback = mos7840_interrupt_callback,
2746};
2747
Paul B Schroeder3f542972006-08-31 19:41:47 -05002748/****************************************************************************
2749 * moschip7840_init
2750 * This is called by the module subsystem, or on startup to initialize us
2751 ****************************************************************************/
2752static int __init moschip7840_init(void)
2753{
2754 int retval;
2755
Tony Cook84fe6e72009-04-18 22:55:06 +09302756 dbg("%s", " mos7840_init :entering..........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002757
2758 /* Register with the usb serial */
2759 retval = usb_serial_register(&moschip7840_4port_device);
2760
2761 if (retval)
2762 goto failed_port_device_register;
2763
Tony Cook84fe6e72009-04-18 22:55:06 +09302764 dbg("%s", "Entering...");
Greg Kroah-Hartmanc197a8d2008-08-18 13:21:04 -07002765 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
2766 DRIVER_DESC "\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002767
2768 /* Register with the usb */
2769 retval = usb_register(&io_driver);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002770 if (retval == 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302771 dbg("%s", "Leaving...");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002772 return 0;
2773 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002774 usb_serial_deregister(&moschip7840_4port_device);
Alan Cox880af9d2008-07-22 11:16:12 +01002775failed_port_device_register:
Paul B Schroeder3f542972006-08-31 19:41:47 -05002776 return retval;
2777}
2778
2779/****************************************************************************
2780 * moschip7840_exit
2781 * Called when the driver is about to be unloaded.
2782 ****************************************************************************/
2783static void __exit moschip7840_exit(void)
2784{
2785
Tony Cook84fe6e72009-04-18 22:55:06 +09302786 dbg("%s", " mos7840_exit :entering..........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002787
2788 usb_deregister(&io_driver);
2789
2790 usb_serial_deregister(&moschip7840_4port_device);
2791
Tony Cook84fe6e72009-04-18 22:55:06 +09302792 dbg("%s", "Entering...");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002793}
2794
2795module_init(moschip7840_init);
2796module_exit(moschip7840_exit);
2797
2798/* Module information */
2799MODULE_DESCRIPTION(DRIVER_DESC);
2800MODULE_LICENSE("GPL");
2801
2802module_param(debug, bool, S_IRUGO | S_IWUSR);
2803MODULE_PARM_DESC(debug, "Debug enabled or not");