blob: 39d0926d1a9015394043091929bfdbce0ce1ed28 [file] [log] [blame]
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001/*
Greg Kroah-Hartmanc3858cb2009-01-29 16:41:35 -08002 * Aten 2011 USB serial driver for 4 port devices
3 *
4 * Copyright (C) 2000 Inside Out Networks
5 * Copyright (C) 2001-2002, 2009 Greg Kroah-Hartman <greg@kroah.com>
6 * Copyright (C) 2009 Novell Inc.
7 *
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -080013 */
14
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -080015#include <linux/kernel.h>
16#include <linux/errno.h>
17#include <linux/init.h>
18#include <linux/slab.h>
19#include <linux/tty.h>
20#include <linux/tty_driver.h>
21#include <linux/tty_flip.h>
22#include <linux/module.h>
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -080023#include <linux/serial.h>
Greg Kroah-Hartman8532b092009-02-03 21:36:46 -080024#include <linux/uaccess.h>
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -080025#include <linux/usb.h>
Greg Kroah-Hartmana6364092009-01-29 16:57:23 -080026#include <linux/usb/serial.h>
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -080027
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -080028
Greg Kroah-Hartmana6364092009-01-29 16:57:23 -080029#define ZLP_REG1 0x3A /* Zero_Flag_Reg1 58 */
30#define ZLP_REG2 0x3B /* Zero_Flag_Reg2 59 */
31#define ZLP_REG3 0x3C /* Zero_Flag_Reg3 60 */
32#define ZLP_REG4 0x3D /* Zero_Flag_Reg4 61 */
33#define ZLP_REG5 0x3E /* Zero_Flag_Reg5 62 */
Greg Kroah-Hartmane5ce6102009-01-29 12:57:52 -080034
Greg Kroah-Hartmana6364092009-01-29 16:57:23 -080035/* Interrupt Rotinue Defines */
36#define SERIAL_IIR_RLS 0x06
37#define SERIAL_IIR_RDA 0x04
38#define SERIAL_IIR_CTI 0x0c
39#define SERIAL_IIR_THR 0x02
40#define SERIAL_IIR_MS 0x00
Greg Kroah-Hartmane5ce6102009-01-29 12:57:52 -080041
Greg Kroah-Hartmana6364092009-01-29 16:57:23 -080042/* Emulation of the bit mask on the LINE STATUS REGISTER. */
43#define SERIAL_LSR_DR 0x0001
44#define SERIAL_LSR_OE 0x0002
45#define SERIAL_LSR_PE 0x0004
46#define SERIAL_LSR_FE 0x0008
47#define SERIAL_LSR_BI 0x0010
48#define SERIAL_LSR_THRE 0x0020
49#define SERIAL_LSR_TEMT 0x0040
50#define SERIAL_LSR_FIFOERR 0x0080
Greg Kroah-Hartmane5ce6102009-01-29 12:57:52 -080051
Greg Kroah-Hartmana6364092009-01-29 16:57:23 -080052/* MSR bit defines(place holders) */
53#define ATEN_MSR_DELTA_CTS 0x10
54#define ATEN_MSR_DELTA_DSR 0x20
55#define ATEN_MSR_DELTA_RI 0x40
56#define ATEN_MSR_DELTA_CD 0x80
Greg Kroah-Hartmane5ce6102009-01-29 12:57:52 -080057
Greg Kroah-Hartmana6364092009-01-29 16:57:23 -080058/* Serial Port register Address */
59#define RECEIVE_BUFFER_REGISTER ((__u16)(0x00))
60#define TRANSMIT_HOLDING_REGISTER ((__u16)(0x00))
61#define INTERRUPT_ENABLE_REGISTER ((__u16)(0x01))
62#define INTERRUPT_IDENT_REGISTER ((__u16)(0x02))
63#define FIFO_CONTROL_REGISTER ((__u16)(0x02))
64#define LINE_CONTROL_REGISTER ((__u16)(0x03))
65#define MODEM_CONTROL_REGISTER ((__u16)(0x04))
66#define LINE_STATUS_REGISTER ((__u16)(0x05))
67#define MODEM_STATUS_REGISTER ((__u16)(0x06))
68#define SCRATCH_PAD_REGISTER ((__u16)(0x07))
69#define DIVISOR_LATCH_LSB ((__u16)(0x00))
70#define DIVISOR_LATCH_MSB ((__u16)(0x01))
Greg Kroah-Hartmane5ce6102009-01-29 12:57:52 -080071
Greg Kroah-Hartmana6364092009-01-29 16:57:23 -080072#define SP1_REGISTER ((__u16)(0x00))
73#define CONTROL1_REGISTER ((__u16)(0x01))
74#define CLK_MULTI_REGISTER ((__u16)(0x02))
75#define CLK_START_VALUE_REGISTER ((__u16)(0x03))
76#define DCR1_REGISTER ((__u16)(0x04))
77#define GPIO_REGISTER ((__u16)(0x07))
Greg Kroah-Hartmane5ce6102009-01-29 12:57:52 -080078
Greg Kroah-Hartmana6364092009-01-29 16:57:23 -080079#define SERIAL_LCR_DLAB ((__u16)(0x0080))
Greg Kroah-Hartmane5ce6102009-01-29 12:57:52 -080080
Greg Kroah-Hartmana6364092009-01-29 16:57:23 -080081/*
82 * URB POOL related defines
83 */
84#define NUM_URBS 16 /* URB Count */
85#define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
Greg Kroah-Hartmane5ce6102009-01-29 12:57:52 -080086
Greg Kroah-Hartmane5ce6102009-01-29 12:57:52 -080087#define USB_VENDOR_ID_ATENINTL 0x0557
88#define ATENINTL_DEVICE_ID_2011 0x2011
89#define ATENINTL_DEVICE_ID_7820 0x7820
90
Greg Kroah-Hartman8532b092009-02-03 21:36:46 -080091static struct usb_device_id id_table[] = {
92 { USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_2011) },
93 { USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_7820) },
Greg Kroah-Hartmane5ce6102009-01-29 12:57:52 -080094 { } /* terminating entry */
95};
Greg Kroah-Hartman8532b092009-02-03 21:36:46 -080096MODULE_DEVICE_TABLE(usb, id_table);
Greg Kroah-Hartmane5ce6102009-01-29 12:57:52 -080097
98/* This structure holds all of the local port information */
Greg Kroah-Hartman8532b092009-02-03 21:36:46 -080099struct ATENINTL_port {
Greg Kroah-Hartmane5ce6102009-01-29 12:57:52 -0800100 int port_num; /*Actual port number in the device(1,2,etc)*/
101 __u8 bulk_out_endpoint; /* the bulk out endpoint handle */
Greg Kroah-Hartman8532b092009-02-03 21:36:46 -0800102 unsigned char *bulk_out_buffer; /* buffer used for the bulk out endpoint */
103 struct urb *write_urb; /* write URB for this port */
104 __u8 bulk_in_endpoint; /* the bulk in endpoint handle */
105 unsigned char *bulk_in_buffer; /* the buffer we use for the bulk in endpoint */
106 struct urb *read_urb; /* read URB for this port */
Greg Kroah-Hartmane5ce6102009-01-29 12:57:52 -0800107 __u8 shadowLCR; /* last LCR value received */
108 __u8 shadowMCR; /* last MCR value received */
Greg Kroah-Hartmane5ce6102009-01-29 12:57:52 -0800109 char open;
Greg Kroah-Hartmane5ce6102009-01-29 12:57:52 -0800110 char chaseResponsePending;
111 wait_queue_head_t wait_chase; /* for handling sleeping while waiting for chase to finish */
Greg Kroah-Hartmane5ce6102009-01-29 12:57:52 -0800112 wait_queue_head_t wait_command; /* for handling sleeping while waiting for command to finish */
Greg Kroah-Hartmane5ce6102009-01-29 12:57:52 -0800113 struct async_icount icount;
114 struct usb_serial_port *port; /* loop back to the owner of this object */
115 /*Offsets*/
Greg Kroah-Hartman8532b092009-02-03 21:36:46 -0800116 __u8 SpRegOffset;
117 __u8 ControlRegOffset;
118 __u8 DcrRegOffset;
Greg Kroah-Hartman87ed9bd2009-02-03 16:06:19 -0800119 /* for processing control URBS in interrupt context */
Greg Kroah-Hartmane5ce6102009-01-29 12:57:52 -0800120 struct urb *control_urb;
Greg Kroah-Hartman8532b092009-02-03 21:36:46 -0800121 char *ctrl_buf;
122 int MsrLsr;
Greg Kroah-Hartmane5ce6102009-01-29 12:57:52 -0800123
Greg Kroah-Hartman8532b092009-02-03 21:36:46 -0800124 struct urb *write_urb_pool[NUM_URBS];
Greg Kroah-Hartmane5ce6102009-01-29 12:57:52 -0800125 /* we pass a pointer to this as the arguement sent to cypress_set_termios old_termios */
Greg Kroah-Hartman8532b092009-02-03 21:36:46 -0800126 struct ktermios tmp_termios; /* stores the old termios settings */
Greg Kroah-Hartmane5ce6102009-01-29 12:57:52 -0800127 spinlock_t lock; /* private lock */
128};
129
Greg Kroah-Hartmane5ce6102009-01-29 12:57:52 -0800130/* This structure holds all of the individual serial device information */
Greg Kroah-Hartman8532b092009-02-03 21:36:46 -0800131struct ATENINTL_serial {
Greg Kroah-Hartmane5ce6102009-01-29 12:57:52 -0800132 __u8 interrupt_in_endpoint; /* the interrupt endpoint handle */
Greg Kroah-Hartman8532b092009-02-03 21:36:46 -0800133 unsigned char *interrupt_in_buffer; /* the buffer we use for the interrupt endpoint */
134 struct urb *interrupt_read_urb; /* our interrupt urb */
Greg Kroah-Hartmane5ce6102009-01-29 12:57:52 -0800135 __u8 bulk_in_endpoint; /* the bulk in endpoint handle */
Greg Kroah-Hartman8532b092009-02-03 21:36:46 -0800136 unsigned char *bulk_in_buffer; /* the buffer we use for the bulk in endpoint */
Greg Kroah-Hartmane5ce6102009-01-29 12:57:52 -0800137 struct urb *read_urb; /* our bulk read urb */
138 __u8 bulk_out_endpoint; /* the bulk out endpoint handle */
Greg Kroah-Hartmane5ce6102009-01-29 12:57:52 -0800139 struct usb_serial *serial; /* loop back to the owner of this object */
Greg Kroah-Hartman87ed9bd2009-02-03 16:06:19 -0800140 int ATEN2011_spectrum_2or4ports; /* this says the number of ports in the device */
141 /* Indicates about the no.of opened ports of an individual USB-serial adapater. */
Greg Kroah-Hartmane5ce6102009-01-29 12:57:52 -0800142 unsigned int NoOfOpenPorts;
Greg Kroah-Hartman87ed9bd2009-02-03 16:06:19 -0800143 /* a flag for Status endpoint polling */
Greg Kroah-Hartmane5ce6102009-01-29 12:57:52 -0800144 unsigned char status_polling_started;
145};
146
Greg Kroah-Hartmana7fefd12009-01-29 17:10:43 -0800147static void ATEN2011_set_termios(struct tty_struct *tty,
148 struct usb_serial_port *port,
149 struct ktermios *old_termios);
150static void ATEN2011_change_port_settings(struct tty_struct *tty,
151 struct ATENINTL_port *ATEN2011_port,
152 struct ktermios *old_termios);
Greg Kroah-Hartmane5ce6102009-01-29 12:57:52 -0800153
154/*************************************
155 * Bit definitions for each register *
156 *************************************/
157#define LCR_BITS_5 0x00 /* 5 bits/char */
158#define LCR_BITS_6 0x01 /* 6 bits/char */
159#define LCR_BITS_7 0x02 /* 7 bits/char */
160#define LCR_BITS_8 0x03 /* 8 bits/char */
161#define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
162
163#define LCR_STOP_1 0x00 /* 1 stop bit */
164#define LCR_STOP_1_5 0x04 /* 1.5 stop bits (if 5 bits/char) */
165#define LCR_STOP_2 0x04 /* 2 stop bits (if 6-8 bits/char) */
166#define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
167
168#define LCR_PAR_NONE 0x00 /* No parity */
169#define LCR_PAR_ODD 0x08 /* Odd parity */
170#define LCR_PAR_EVEN 0x18 /* Even parity */
171#define LCR_PAR_MARK 0x28 /* Force parity bit to 1 */
172#define LCR_PAR_SPACE 0x38 /* Force parity bit to 0 */
173#define LCR_PAR_MASK 0x38 /* Mask for parity field */
174
175#define LCR_SET_BREAK 0x40 /* Set Break condition */
176#define LCR_DL_ENABLE 0x80 /* Enable access to divisor latch */
177
178#define MCR_DTR 0x01 /* Assert DTR */
179#define MCR_RTS 0x02 /* Assert RTS */
180#define MCR_OUT1 0x04 /* Loopback only: Sets state of RI */
181#define MCR_MASTER_IE 0x08 /* Enable interrupt outputs */
182#define MCR_LOOPBACK 0x10 /* Set internal (digital) loopback mode */
183#define MCR_XON_ANY 0x20 /* Enable any char to exit XOFF mode */
184
185#define ATEN2011_MSR_CTS 0x10 /* Current state of CTS */
186#define ATEN2011_MSR_DSR 0x20 /* Current state of DSR */
187#define ATEN2011_MSR_RI 0x40 /* Current state of RI */
188#define ATEN2011_MSR_CD 0x80 /* Current state of CD */
189
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800190
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -0800191static int debug;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800192
193/*
194 * Version Information
195 */
Greg Kroah-Hartmana6364092009-01-29 16:57:23 -0800196#define DRIVER_VERSION "2.0"
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800197#define DRIVER_DESC "ATENINTL 2011 USB Serial Adapter"
198
199/*
200 * Defines used for sending commands to port
201 */
202
Greg Kroah-Hartmana6364092009-01-29 16:57:23 -0800203#define ATEN_WDR_TIMEOUT (50) /* default urb timeout */
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800204
205/* Requests */
Greg Kroah-Hartmana6364092009-01-29 16:57:23 -0800206#define ATEN_RD_RTYPE 0xC0
207#define ATEN_WR_RTYPE 0x40
208#define ATEN_RDREQ 0x0D
209#define ATEN_WRREQ 0x0E
210#define ATEN_CTRL_TIMEOUT 500
211#define VENDOR_READ_LENGTH (0x01)
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800212
Greg Kroah-Hartman907aff42009-01-29 17:17:48 -0800213/* set to 1 for RS485 mode and 0 for RS232 mode */
214/* FIXME make this somehow dynamic and not build time specific */
Greg Kroah-Hartman8532b092009-02-03 21:36:46 -0800215static int RS485mode;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800216
Greg Kroah-Hartman8433b6a2009-01-30 15:32:18 -0800217static int set_reg_sync(struct usb_serial_port *port, __u16 reg, __u16 val)
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800218{
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800219 struct usb_device *dev = port->serial->dev;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800220 val = val & 0x00ff;
Greg Kroah-Hartman8433b6a2009-01-30 15:32:18 -0800221
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -0800222 dbg("%s: is %x, value %x", __func__, reg, val);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800223
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800224 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), ATEN_WRREQ,
225 ATEN_WR_RTYPE, val, reg, NULL, 0,
226 ATEN_WDR_TIMEOUT);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800227}
228
Greg Kroah-Hartmane9abe302009-01-30 15:35:31 -0800229static int get_reg_sync(struct usb_serial_port *port, __u16 reg, __u16 *val)
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800230{
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800231 struct usb_device *dev = port->serial->dev;
Greg Kroah-Hartmane9abe302009-01-30 15:35:31 -0800232 int ret;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800233
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800234 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), ATEN_RDREQ,
235 ATEN_RD_RTYPE, 0, reg, val, VENDOR_READ_LENGTH,
236 ATEN_WDR_TIMEOUT);
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -0800237 dbg("%s: offset is %x, return val %x", __func__, reg, *val);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800238 *val = (*val) & 0x00ff;
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800239 return ret;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800240}
241
Greg Kroah-Hartmane514adb2009-01-30 15:35:50 -0800242static int set_uart_reg(struct usb_serial_port *port, __u16 reg, __u16 val)
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800243{
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800244 struct usb_device *dev = port->serial->dev;
Greg Kroah-Hartmane514adb2009-01-30 15:35:50 -0800245 struct ATENINTL_serial *a_serial;
246 __u16 minor;
247
Greg Kroah-Hartmanc3e06f22009-01-30 15:37:00 -0800248 a_serial = usb_get_serial_data(port->serial);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800249 minor = port->serial->minor;
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800250 if (minor == SERIAL_TTY_NO_MINOR)
251 minor = 0;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800252 val = val & 0x00ff;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800253
Greg Kroah-Hartmane514adb2009-01-30 15:35:50 -0800254 /*
255 * For the UART control registers,
256 * the application number need to be Or'ed
257 */
258 if (a_serial->ATEN2011_spectrum_2or4ports == 4)
259 val |= (((__u16)port->number - minor) + 1) << 8;
260 else {
261 if (((__u16) port->number - minor) == 0)
262 val |= (((__u16)port->number - minor) + 1) << 8;
263 else
264 val |= (((__u16)port->number - minor) + 2) << 8;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800265 }
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -0800266 dbg("%s: application number is %x", __func__, val);
Greg Kroah-Hartmane514adb2009-01-30 15:35:50 -0800267
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800268 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), ATEN_WRREQ,
269 ATEN_WR_RTYPE, val, reg, NULL, 0,
270 ATEN_WDR_TIMEOUT);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800271}
272
Greg Kroah-Hartman12cbe0b2009-01-30 15:36:09 -0800273static int get_uart_reg(struct usb_serial_port *port, __u16 reg, __u16 *val)
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800274{
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800275 struct usb_device *dev = port->serial->dev;
276 int ret = 0;
Greg Kroah-Hartman12cbe0b2009-01-30 15:36:09 -0800277 __u16 wval;
278 struct ATENINTL_serial *a_serial;
279 __u16 minor = port->serial->minor;
280
Greg Kroah-Hartmanc3e06f22009-01-30 15:37:00 -0800281 a_serial = usb_get_serial_data(port->serial);
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800282 if (minor == SERIAL_TTY_NO_MINOR)
283 minor = 0;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800284
Greg Kroah-Hartman12cbe0b2009-01-30 15:36:09 -0800285 /* wval is same as application number */
286 if (a_serial->ATEN2011_spectrum_2or4ports == 4)
287 wval = (((__u16)port->number - minor) + 1) << 8;
288 else {
289 if (((__u16) port->number - minor) == 0)
290 wval = (((__u16) port->number - minor) + 1) << 8;
291 else
292 wval = (((__u16) port->number - minor) + 2) << 8;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800293 }
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -0800294 dbg("%s: application number is %x", __func__, wval);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800295 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), ATEN_RDREQ,
Greg Kroah-Hartman12cbe0b2009-01-30 15:36:09 -0800296 ATEN_RD_RTYPE, wval, reg, val, VENDOR_READ_LENGTH,
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800297 ATEN_WDR_TIMEOUT);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800298 *val = (*val) & 0x00ff;
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800299 return ret;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800300}
301
Greg Kroah-Hartmana7fefd12009-01-29 17:10:43 -0800302static int handle_newMsr(struct ATENINTL_port *port, __u8 newMsr)
303{
304 struct ATENINTL_port *ATEN2011_port;
305 struct async_icount *icount;
306 ATEN2011_port = port;
307 icount = &ATEN2011_port->icount;
308 if (newMsr &
309 (ATEN_MSR_DELTA_CTS | ATEN_MSR_DELTA_DSR | ATEN_MSR_DELTA_RI |
310 ATEN_MSR_DELTA_CD)) {
311 icount = &ATEN2011_port->icount;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800312
Greg Kroah-Hartmana7fefd12009-01-29 17:10:43 -0800313 /* update input line counters */
Greg Kroah-Hartman8532b092009-02-03 21:36:46 -0800314 if (newMsr & ATEN_MSR_DELTA_CTS)
Greg Kroah-Hartmana7fefd12009-01-29 17:10:43 -0800315 icount->cts++;
Greg Kroah-Hartman8532b092009-02-03 21:36:46 -0800316 if (newMsr & ATEN_MSR_DELTA_DSR)
Greg Kroah-Hartmana7fefd12009-01-29 17:10:43 -0800317 icount->dsr++;
Greg Kroah-Hartman8532b092009-02-03 21:36:46 -0800318 if (newMsr & ATEN_MSR_DELTA_CD)
Greg Kroah-Hartmana7fefd12009-01-29 17:10:43 -0800319 icount->dcd++;
Greg Kroah-Hartman8532b092009-02-03 21:36:46 -0800320 if (newMsr & ATEN_MSR_DELTA_RI)
Greg Kroah-Hartmana7fefd12009-01-29 17:10:43 -0800321 icount->rng++;
Greg Kroah-Hartmana7fefd12009-01-29 17:10:43 -0800322 }
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800323
Greg Kroah-Hartmana7fefd12009-01-29 17:10:43 -0800324 return 0;
325}
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800326
Greg Kroah-Hartmana7fefd12009-01-29 17:10:43 -0800327static int handle_newLsr(struct ATENINTL_port *port, __u8 newLsr)
328{
329 struct async_icount *icount;
330
Greg Kroah-Hartman9d700542009-02-03 21:35:55 -0800331 dbg("%s - %02x", __func__, newLsr);
Greg Kroah-Hartmana7fefd12009-01-29 17:10:43 -0800332
333 if (newLsr & SERIAL_LSR_BI) {
Greg Kroah-Hartman87ed9bd2009-02-03 16:06:19 -0800334 /*
335 * Parity and Framing errors only count if they occur exclusive
336 * of a break being received.
337 */
Greg Kroah-Hartmana7fefd12009-01-29 17:10:43 -0800338 newLsr &= (__u8) (SERIAL_LSR_OE | SERIAL_LSR_BI);
339 }
340
341 /* update input line counters */
342 icount = &port->icount;
Greg Kroah-Hartman8532b092009-02-03 21:36:46 -0800343 if (newLsr & SERIAL_LSR_BI)
Greg Kroah-Hartmana7fefd12009-01-29 17:10:43 -0800344 icount->brk++;
Greg Kroah-Hartman8532b092009-02-03 21:36:46 -0800345 if (newLsr & SERIAL_LSR_OE)
Greg Kroah-Hartmana7fefd12009-01-29 17:10:43 -0800346 icount->overrun++;
Greg Kroah-Hartman8532b092009-02-03 21:36:46 -0800347 if (newLsr & SERIAL_LSR_PE)
Greg Kroah-Hartmana7fefd12009-01-29 17:10:43 -0800348 icount->parity++;
Greg Kroah-Hartman8532b092009-02-03 21:36:46 -0800349 if (newLsr & SERIAL_LSR_FE)
Greg Kroah-Hartmana7fefd12009-01-29 17:10:43 -0800350 icount->frame++;
Greg Kroah-Hartmana7fefd12009-01-29 17:10:43 -0800351
352 return 0;
353}
354
355static void ATEN2011_control_callback(struct urb *urb)
356{
357 unsigned char *data;
358 struct ATENINTL_port *ATEN2011_port;
359 __u8 regval = 0x0;
360
Greg Kroah-Hartmana7fefd12009-01-29 17:10:43 -0800361 switch (urb->status) {
362 case 0:
363 /* success */
364 break;
365 case -ECONNRESET:
366 case -ENOENT:
367 case -ESHUTDOWN:
368 /* this urb is terminated, clean up */
Greg Kroah-Hartman9d700542009-02-03 21:35:55 -0800369 dbg("%s - urb shutting down with status: %d", __func__,
Greg Kroah-Hartmana7fefd12009-01-29 17:10:43 -0800370 urb->status);
371 return;
372 default:
Greg Kroah-Hartman9d700542009-02-03 21:35:55 -0800373 dbg("%s - nonzero urb status received: %d", __func__,
Greg Kroah-Hartmana7fefd12009-01-29 17:10:43 -0800374 urb->status);
375 goto exit;
376 }
377
378 ATEN2011_port = (struct ATENINTL_port *)urb->context;
379
Greg Kroah-Hartman9d700542009-02-03 21:35:55 -0800380 dbg("%s urb buffer size is %d", __func__, urb->actual_length);
381 dbg("%s ATEN2011_port->MsrLsr is %d port %d", __func__,
Greg Kroah-Hartmana7fefd12009-01-29 17:10:43 -0800382 ATEN2011_port->MsrLsr, ATEN2011_port->port_num);
383 data = urb->transfer_buffer;
384 regval = (__u8) data[0];
Greg Kroah-Hartman9d700542009-02-03 21:35:55 -0800385 dbg("%s data is %x", __func__, regval);
Greg Kroah-Hartmana7fefd12009-01-29 17:10:43 -0800386 if (ATEN2011_port->MsrLsr == 0)
387 handle_newMsr(ATEN2011_port, regval);
388 else if (ATEN2011_port->MsrLsr == 1)
389 handle_newLsr(ATEN2011_port, regval);
390
Greg Kroah-Hartman8532b092009-02-03 21:36:46 -0800391exit:
Greg Kroah-Hartmana7fefd12009-01-29 17:10:43 -0800392 return;
393}
394
395static int ATEN2011_get_reg(struct ATENINTL_port *ATEN, __u16 Wval, __u16 reg,
Greg Kroah-Hartman8532b092009-02-03 21:36:46 -0800396 __u16 *val)
Greg Kroah-Hartmana7fefd12009-01-29 17:10:43 -0800397{
398 struct usb_device *dev = ATEN->port->serial->dev;
399 struct usb_ctrlrequest *dr = NULL;
400 unsigned char *buffer = NULL;
401 int ret = 0;
402 buffer = (__u8 *) ATEN->ctrl_buf;
403
Greg Kroah-Hartmana7fefd12009-01-29 17:10:43 -0800404 dr = (void *)(buffer + 2);
405 dr->bRequestType = ATEN_RD_RTYPE;
406 dr->bRequest = ATEN_RDREQ;
Greg Kroah-Hartman87ed9bd2009-02-03 16:06:19 -0800407 dr->wValue = cpu_to_le16(Wval);
Greg Kroah-Hartmana7fefd12009-01-29 17:10:43 -0800408 dr->wIndex = cpu_to_le16(reg);
409 dr->wLength = cpu_to_le16(2);
410
411 usb_fill_control_urb(ATEN->control_urb, dev, usb_rcvctrlpipe(dev, 0),
412 (unsigned char *)dr, buffer, 2,
413 ATEN2011_control_callback, ATEN);
414 ATEN->control_urb->transfer_buffer_length = 2;
415 ret = usb_submit_urb(ATEN->control_urb, GFP_ATOMIC);
416 return ret;
417}
418
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800419static void ATEN2011_interrupt_callback(struct urb *urb)
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800420{
421 int result;
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800422 int length;
423 struct ATENINTL_port *ATEN2011_port;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800424 struct ATENINTL_serial *ATEN2011_serial;
425 struct usb_serial *serial;
426 __u16 Data;
427 unsigned char *data;
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800428 __u8 sp[5], st;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800429 int i;
430 __u16 wval;
431 int minor;
Greg Kroah-Hartman87ed9bd2009-02-03 16:06:19 -0800432
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -0800433 dbg("%s", " : Entering");
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800434
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800435 ATEN2011_serial = (struct ATENINTL_serial *)urb->context;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800436
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800437 switch (urb->status) {
438 case 0:
439 /* success */
440 break;
441 case -ECONNRESET:
442 case -ENOENT:
443 case -ESHUTDOWN:
444 /* this urb is terminated, clean up */
Greg Kroah-Hartman9d700542009-02-03 21:35:55 -0800445 dbg("%s - urb shutting down with status: %d", __func__,
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800446 urb->status);
447 return;
448 default:
Greg Kroah-Hartman9d700542009-02-03 21:35:55 -0800449 dbg("%s - nonzero urb status received: %d", __func__,
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800450 urb->status);
451 goto exit;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800452 }
453 length = urb->actual_length;
454 data = urb->transfer_buffer;
455
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800456 serial = ATEN2011_serial->serial;
457
458 /* ATENINTL get 5 bytes
459 * Byte 1 IIR Port 1 (port.number is 0)
460 * Byte 2 IIR Port 2 (port.number is 1)
461 * Byte 3 IIR Port 3 (port.number is 2)
462 * Byte 4 IIR Port 4 (port.number is 3)
463 * Byte 5 FIFO status for both */
464
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800465 if (length && length > 5) {
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -0800466 dbg("%s", "Wrong data !!!");
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800467 return;
468 }
469
470 /* MATRIX */
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800471 if (ATEN2011_serial->ATEN2011_spectrum_2or4ports == 4) {
472 sp[0] = (__u8) data[0];
473 sp[1] = (__u8) data[1];
474 sp[2] = (__u8) data[2];
475 sp[3] = (__u8) data[3];
476 st = (__u8) data[4];
477 } else {
478 sp[0] = (__u8) data[0];
479 sp[1] = (__u8) data[2];
Greg Kroah-Hartman87ed9bd2009-02-03 16:06:19 -0800480 /* sp[2]=(__u8)data[2]; */
481 /* sp[3]=(__u8)data[3]; */
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800482 st = (__u8) data[4];
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800483
484 }
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800485 for (i = 0; i < serial->num_ports; i++) {
Greg Kroah-Hartmanc3e06f22009-01-30 15:37:00 -0800486 ATEN2011_port = usb_get_serial_port_data(serial->port[i]);
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800487 minor = serial->minor;
488 if (minor == SERIAL_TTY_NO_MINOR)
489 minor = 0;
490 if ((ATEN2011_serial->ATEN2011_spectrum_2or4ports == 2)
491 && (i != 0))
492 wval =
493 (((__u16) serial->port[i]->number -
494 (__u16) (minor)) + 2) << 8;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800495 else
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800496 wval =
497 (((__u16) serial->port[i]->number -
498 (__u16) (minor)) + 1) << 8;
Greg Kroah-Hartmana6364092009-01-29 16:57:23 -0800499 if (ATEN2011_port->open != 0) {
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800500 if (sp[i] & 0x01) {
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -0800501 dbg("SP%d No Interrupt !!!", i);
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800502 } else {
503 switch (sp[i] & 0x0f) {
504 case SERIAL_IIR_RLS:
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -0800505 dbg("Serial Port %d: Receiver status error or address bit detected in 9-bit mode", i);
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800506 ATEN2011_port->MsrLsr = 1;
507 ATEN2011_get_reg(ATEN2011_port, wval,
508 LINE_STATUS_REGISTER,
509 &Data);
510 break;
511 case SERIAL_IIR_MS:
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -0800512 dbg("Serial Port %d: Modem status change", i);
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800513 ATEN2011_port->MsrLsr = 0;
514 ATEN2011_get_reg(ATEN2011_port, wval,
515 MODEM_STATUS_REGISTER,
516 &Data);
517 break;
518 }
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800519 }
520 }
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800521
522 }
Greg Kroah-Hartman8532b092009-02-03 21:36:46 -0800523exit:
Greg Kroah-Hartmana6364092009-01-29 16:57:23 -0800524 if (ATEN2011_serial->status_polling_started == 0)
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800525 return;
526
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800527 result = usb_submit_urb(urb, GFP_ATOMIC);
528 if (result) {
529 dev_err(&urb->dev->dev,
530 "%s - Error %d submitting interrupt urb\n",
Greg Kroah-Hartman9d700542009-02-03 21:35:55 -0800531 __func__, result);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800532 }
533
534 return;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800535}
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800536
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800537static void ATEN2011_bulk_in_callback(struct urb *urb)
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800538{
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800539 int status;
540 unsigned char *data;
541 struct usb_serial *serial;
542 struct usb_serial_port *port;
543 struct ATENINTL_serial *ATEN2011_serial;
544 struct ATENINTL_port *ATEN2011_port;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800545 struct tty_struct *tty;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800546
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800547 if (urb->status) {
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -0800548 dbg("nonzero read bulk status received: %d", urb->status);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800549 return;
550 }
551
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800552 ATEN2011_port = (struct ATENINTL_port *)urb->context;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800553
554 port = (struct usb_serial_port *)ATEN2011_port->port;
Greg Kroah-Hartmanf3415ee2009-01-30 15:31:38 -0800555 serial = port->serial;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800556
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -0800557 dbg("%s", "Entering...");
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800558
559 data = urb->transfer_buffer;
Greg Kroah-Hartmanc3e06f22009-01-30 15:37:00 -0800560 ATEN2011_serial = usb_get_serial_data(serial);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800561
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800562 if (urb->actual_length) {
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800563 tty = tty_port_tty_get(&ATEN2011_port->port->port);
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800564 if (tty) {
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800565 tty_buffer_request_room(tty, urb->actual_length);
566 tty_insert_flip_string(tty, data, urb->actual_length);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800567 tty_flip_buffer_push(tty);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800568 tty_kref_put(tty);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800569 }
570
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800571 ATEN2011_port->icount.rx += urb->actual_length;
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -0800572 dbg("ATEN2011_port->icount.rx is %d:",
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800573 ATEN2011_port->icount.rx);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800574 }
575
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800576 if (!ATEN2011_port->read_urb) {
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -0800577 dbg("%s", "URB KILLED !!!");
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800578 return;
579 }
580
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800581 if (ATEN2011_port->read_urb->status != -EINPROGRESS) {
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800582 ATEN2011_port->read_urb->dev = serial->dev;
583
584 status = usb_submit_urb(ATEN2011_port->read_urb, GFP_ATOMIC);
Greg Kroah-Hartman8532b092009-02-03 21:36:46 -0800585 if (status)
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -0800586 dbg("usb_submit_urb(read bulk) failed, status = %d", status);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800587 }
588}
589
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800590static void ATEN2011_bulk_out_data_callback(struct urb *urb)
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800591{
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800592 struct ATENINTL_port *ATEN2011_port;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800593 struct tty_struct *tty;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800594
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800595 if (urb->status) {
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -0800596 dbg("nonzero write bulk status received:%d", urb->status);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800597 return;
598 }
599
600 ATEN2011_port = (struct ATENINTL_port *)urb->context;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800601
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -0800602 dbg("%s", "Entering .........");
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800603
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800604 tty = tty_port_tty_get(&ATEN2011_port->port->port);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800605
Alan Cox20aa9e92009-04-07 18:43:43 +0100606 if (tty && ATEN2011_port->open)
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800607 /* tell the tty driver that something has changed */
Alan Cox20aa9e92009-04-07 18:43:43 +0100608 tty_wakeup(tty);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800609
Greg Kroah-Hartman87ed9bd2009-02-03 16:06:19 -0800610 /* schedule_work(&ATEN2011_port->port->work); */
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800611 tty_kref_put(tty);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800612
613}
614
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800615#ifdef ATENSerialProbe
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800616static int ATEN2011_serial_probe(struct usb_serial *serial,
617 const struct usb_device_id *id)
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800618{
619
620 /*need to implement the mode_reg reading and updating\
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800621 structures usb_serial_ device_type\
622 (i.e num_ports, num_bulkin,bulkout etc) */
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800623 /* Also we can update the changes attach */
624 return 1;
625}
626#endif
627
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800628static int ATEN2011_open(struct tty_struct *tty, struct usb_serial_port *port,
629 struct file *filp)
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800630{
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800631 int response;
632 int j;
633 struct usb_serial *serial;
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800634 struct urb *urb;
635 __u16 Data;
636 int status;
637 struct ATENINTL_serial *ATEN2011_serial;
638 struct ATENINTL_port *ATEN2011_port;
639 struct ktermios tmp_termios;
640 int minor;
Greg Kroah-Hartman8b4efbe2009-01-29 13:08:23 -0800641
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800642 serial = port->serial;
643
Greg Kroah-Hartmanc3e06f22009-01-30 15:37:00 -0800644 ATEN2011_port = usb_get_serial_port_data(port);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800645
646 if (ATEN2011_port == NULL)
647 return -ENODEV;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800648
Greg Kroah-Hartmanc3e06f22009-01-30 15:37:00 -0800649 ATEN2011_serial = usb_get_serial_data(serial);
Greg Kroah-Hartman8532b092009-02-03 21:36:46 -0800650 if (ATEN2011_serial == NULL)
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800651 return -ENODEV;
Greg Kroah-Hartman8532b092009-02-03 21:36:46 -0800652
Greg Kroah-Hartman87ed9bd2009-02-03 16:06:19 -0800653 /* increment the number of opened ports counter here */
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800654 ATEN2011_serial->NoOfOpenPorts++;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800655
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800656 usb_clear_halt(serial->dev, port->write_urb->pipe);
657 usb_clear_halt(serial->dev, port->read_urb->pipe);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800658
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800659 /* Initialising the write urb pool */
660 for (j = 0; j < NUM_URBS; ++j) {
661 urb = usb_alloc_urb(0, GFP_ATOMIC);
662 ATEN2011_port->write_urb_pool[j] = urb;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800663
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800664 if (urb == NULL) {
665 err("No more urbs???");
666 continue;
667 }
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800668
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800669 urb->transfer_buffer = NULL;
670 urb->transfer_buffer =
671 kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
672 if (!urb->transfer_buffer) {
Greg Kroah-Hartman9d700542009-02-03 21:35:55 -0800673 err("%s-out of memory for urb buffers.", __func__);
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800674 continue;
675 }
676 }
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800677
678/*****************************************************************************
679 * Initialize ATEN2011 -- Write Init values to corresponding Registers
680 *
681 * Register Index
682 * 1 : IER
683 * 2 : FCR
684 * 3 : LCR
685 * 4 : MCR
686 *
687 * 0x08 : SP1/2 Control Reg
688 *****************************************************************************/
689
Greg Kroah-Hartman87ed9bd2009-02-03 16:06:19 -0800690/* NEED to check the fallowing Block */
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800691
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800692 Data = 0x0;
Greg Kroah-Hartmane9abe302009-01-30 15:35:31 -0800693 status = get_reg_sync(port, ATEN2011_port->SpRegOffset, &Data);
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800694 if (status < 0) {
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -0800695 dbg("Reading Spreg failed");
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800696 return -1;
697 }
698 Data |= 0x80;
Greg Kroah-Hartman8433b6a2009-01-30 15:32:18 -0800699 status = set_reg_sync(port, ATEN2011_port->SpRegOffset, Data);
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800700 if (status < 0) {
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -0800701 dbg("writing Spreg failed");
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800702 return -1;
703 }
704
705 Data &= ~0x80;
Greg Kroah-Hartman8433b6a2009-01-30 15:32:18 -0800706 status = set_reg_sync(port, ATEN2011_port->SpRegOffset, Data);
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800707 if (status < 0) {
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -0800708 dbg("writing Spreg failed");
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800709 return -1;
710 }
711
Greg Kroah-Hartman87ed9bd2009-02-03 16:06:19 -0800712/* End of block to be checked */
713/**************************CHECK***************************/
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800714
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800715 if (RS485mode == 0)
716 Data = 0xC0;
717 else
718 Data = 0x00;
Greg Kroah-Hartmane514adb2009-01-30 15:35:50 -0800719 status = set_uart_reg(port, SCRATCH_PAD_REGISTER, Data);
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800720 if (status < 0) {
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -0800721 dbg("Writing SCRATCH_PAD_REGISTER failed status-0x%x", status);
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800722 return -1;
723 } else
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -0800724 dbg("SCRATCH_PAD_REGISTER Writing success status%d", status);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800725
Greg Kroah-Hartman87ed9bd2009-02-03 16:06:19 -0800726/**************************CHECK***************************/
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800727
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800728 Data = 0x0;
Greg Kroah-Hartmane9abe302009-01-30 15:35:31 -0800729 status = get_reg_sync(port, ATEN2011_port->ControlRegOffset, &Data);
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800730 if (status < 0) {
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -0800731 dbg("Reading Controlreg failed");
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800732 return -1;
733 }
Greg Kroah-Hartman87ed9bd2009-02-03 16:06:19 -0800734 Data |= 0x08; /* Driver done bit */
735 Data |= 0x20; /* rx_disable */
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800736 status = 0;
737 status =
Greg Kroah-Hartman8433b6a2009-01-30 15:32:18 -0800738 set_reg_sync(port, ATEN2011_port->ControlRegOffset, Data);
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800739 if (status < 0) {
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -0800740 dbg("writing Controlreg failed");
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800741 return -1;
742 }
Greg Kroah-Hartman87ed9bd2009-02-03 16:06:19 -0800743 /*
744 * do register settings here
745 * Set all regs to the device default values.
746 * First Disable all interrupts.
747 */
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800748
749 Data = 0x00;
Greg Kroah-Hartmane514adb2009-01-30 15:35:50 -0800750 status = set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800751 if (status < 0) {
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -0800752 dbg("disableing interrupts failed");
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800753 return -1;
754 }
Greg Kroah-Hartman87ed9bd2009-02-03 16:06:19 -0800755 /* Set FIFO_CONTROL_REGISTER to the default value */
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800756 Data = 0x00;
Greg Kroah-Hartmane514adb2009-01-30 15:35:50 -0800757 status = set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800758 if (status < 0) {
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -0800759 dbg("Writing FIFO_CONTROL_REGISTER failed");
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800760 return -1;
761 }
762
Greg Kroah-Hartman87ed9bd2009-02-03 16:06:19 -0800763 Data = 0xcf; /* chk */
Greg Kroah-Hartmane514adb2009-01-30 15:35:50 -0800764 status = set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800765 if (status < 0) {
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -0800766 dbg("Writing FIFO_CONTROL_REGISTER failed");
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800767 return -1;
768 }
769
Greg Kroah-Hartman87ed9bd2009-02-03 16:06:19 -0800770 Data = 0x03; /* LCR_BITS_8 */
Greg Kroah-Hartmane514adb2009-01-30 15:35:50 -0800771 status = set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800772 ATEN2011_port->shadowLCR = Data;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800773
Greg Kroah-Hartman87ed9bd2009-02-03 16:06:19 -0800774 Data = 0x0b; /* MCR_DTR|MCR_RTS|MCR_MASTER_IE */
Greg Kroah-Hartmane514adb2009-01-30 15:35:50 -0800775 status = set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800776 ATEN2011_port->shadowMCR = Data;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800777
778#ifdef Check
779 Data = 0x00;
Greg Kroah-Hartman12cbe0b2009-01-30 15:36:09 -0800780 status = get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800781 ATEN2011_port->shadowLCR = Data;
782
Greg Kroah-Hartman87ed9bd2009-02-03 16:06:19 -0800783 Data |= SERIAL_LCR_DLAB; /* data latch enable in LCR 0x80 */
Greg Kroah-Hartmane514adb2009-01-30 15:35:50 -0800784 status = set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800785
786 Data = 0x0c;
Greg Kroah-Hartmane514adb2009-01-30 15:35:50 -0800787 status = set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800788
789 Data = 0x0;
Greg Kroah-Hartmane514adb2009-01-30 15:35:50 -0800790 status = set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800791
792 Data = 0x00;
Greg Kroah-Hartman12cbe0b2009-01-30 15:36:09 -0800793 status = get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800794
Greg Kroah-Hartman87ed9bd2009-02-03 16:06:19 -0800795/* Data = ATEN2011_port->shadowLCR; */ /* data latch disable */
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800796 Data = Data & ~SERIAL_LCR_DLAB;
Greg Kroah-Hartmane514adb2009-01-30 15:35:50 -0800797 status = set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800798 ATEN2011_port->shadowLCR = Data;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800799#endif
Greg Kroah-Hartman87ed9bd2009-02-03 16:06:19 -0800800 /* clearing Bulkin and Bulkout Fifo */
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800801 Data = 0x0;
Greg Kroah-Hartmane9abe302009-01-30 15:35:31 -0800802 status = get_reg_sync(port, ATEN2011_port->SpRegOffset, &Data);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800803
804 Data = Data | 0x0c;
Greg Kroah-Hartman8433b6a2009-01-30 15:32:18 -0800805 status = set_reg_sync(port, ATEN2011_port->SpRegOffset, Data);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800806
807 Data = Data & ~0x0c;
Greg Kroah-Hartman8433b6a2009-01-30 15:32:18 -0800808 status = set_reg_sync(port, ATEN2011_port->SpRegOffset, Data);
Greg Kroah-Hartman87ed9bd2009-02-03 16:06:19 -0800809 /* Finally enable all interrupts */
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800810 Data = 0x0;
811 Data = 0x0c;
Greg Kroah-Hartmane514adb2009-01-30 15:35:50 -0800812 status = set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800813
Greg Kroah-Hartman87ed9bd2009-02-03 16:06:19 -0800814 /* clearing rx_disable */
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800815 Data = 0x0;
Greg Kroah-Hartmane9abe302009-01-30 15:35:31 -0800816 status = get_reg_sync(port, ATEN2011_port->ControlRegOffset, &Data);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800817 Data = Data & ~0x20;
Greg Kroah-Hartman8433b6a2009-01-30 15:32:18 -0800818 status = set_reg_sync(port, ATEN2011_port->ControlRegOffset, Data);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800819
Greg Kroah-Hartman87ed9bd2009-02-03 16:06:19 -0800820 /* rx_negate */
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800821 Data = 0x0;
Greg Kroah-Hartmane9abe302009-01-30 15:35:31 -0800822 status = get_reg_sync(port, ATEN2011_port->ControlRegOffset, &Data);
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800823 Data = Data | 0x10;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800824 status = 0;
Greg Kroah-Hartman8433b6a2009-01-30 15:32:18 -0800825 status = set_reg_sync(port, ATEN2011_port->ControlRegOffset, Data);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800826
Greg Kroah-Hartman87ed9bd2009-02-03 16:06:19 -0800827 /*
828 * Check to see if we've set up our endpoint info yet
829 * (can't set it up in ATEN2011_startup as the structures
830 * were not set up at that time.)
831 */
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800832 if (ATEN2011_serial->NoOfOpenPorts == 1) {
Greg Kroah-Hartman87ed9bd2009-02-03 16:06:19 -0800833 /* start the status polling here */
Greg Kroah-Hartmana6364092009-01-29 16:57:23 -0800834 ATEN2011_serial->status_polling_started = 1;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800835 /* If not yet set, Set here */
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800836 ATEN2011_serial->interrupt_in_buffer =
837 serial->port[0]->interrupt_in_buffer;
838 ATEN2011_serial->interrupt_in_endpoint =
839 serial->port[0]->interrupt_in_endpointAddress;
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800840 ATEN2011_serial->interrupt_read_urb =
841 serial->port[0]->interrupt_in_urb;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800842
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800843 /* set up interrupt urb */
844 usb_fill_int_urb(ATEN2011_serial->interrupt_read_urb,
845 serial->dev,
846 usb_rcvintpipe(serial->dev,
847 ATEN2011_serial->
848 interrupt_in_endpoint),
849 ATEN2011_serial->interrupt_in_buffer,
850 ATEN2011_serial->interrupt_read_urb->
851 transfer_buffer_length,
852 ATEN2011_interrupt_callback, ATEN2011_serial,
853 ATEN2011_serial->interrupt_read_urb->interval);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800854
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800855 /* start interrupt read for ATEN2011 *
856 * will continue as long as ATEN2011 is connected */
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800857
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800858 response =
859 usb_submit_urb(ATEN2011_serial->interrupt_read_urb,
860 GFP_KERNEL);
861 if (response) {
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -0800862 dbg("%s - Error %d submitting interrupt urb",
Greg Kroah-Hartman9d700542009-02-03 21:35:55 -0800863 __func__, response);
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800864 }
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800865
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800866 }
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800867
Greg Kroah-Hartman87ed9bd2009-02-03 16:06:19 -0800868 /*
869 * See if we've set up our endpoint info yet
870 * (can't set it up in ATEN2011_startup as the
871 * structures were not set up at that time.)
872 */
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800873
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -0800874 dbg("port number is %d", port->number);
875 dbg("serial number is %d", port->serial->minor);
876 dbg("Bulkin endpoint is %d", port->bulk_in_endpointAddress);
877 dbg("BulkOut endpoint is %d", port->bulk_out_endpointAddress);
878 dbg("Interrupt endpoint is %d",
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800879 port->interrupt_in_endpointAddress);
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -0800880 dbg("port's number in the device is %d", ATEN2011_port->port_num);
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800881 ATEN2011_port->bulk_in_buffer = port->bulk_in_buffer;
882 ATEN2011_port->bulk_in_endpoint = port->bulk_in_endpointAddress;
883 ATEN2011_port->read_urb = port->read_urb;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800884 ATEN2011_port->bulk_out_endpoint = port->bulk_out_endpointAddress;
885
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800886 minor = port->serial->minor;
887 if (minor == SERIAL_TTY_NO_MINOR)
888 minor = 0;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800889
890 /* set up our bulk in urb */
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800891 if ((ATEN2011_serial->ATEN2011_spectrum_2or4ports == 2)
892 && (((__u16) port->number - (__u16) (minor)) != 0)) {
893 usb_fill_bulk_urb(ATEN2011_port->read_urb, serial->dev,
894 usb_rcvbulkpipe(serial->dev,
895 (port->
896 bulk_in_endpointAddress +
897 2)), port->bulk_in_buffer,
898 ATEN2011_port->read_urb->
899 transfer_buffer_length,
900 ATEN2011_bulk_in_callback, ATEN2011_port);
901 } else
902 usb_fill_bulk_urb(ATEN2011_port->read_urb,
903 serial->dev,
904 usb_rcvbulkpipe(serial->dev,
905 port->
906 bulk_in_endpointAddress),
907 port->bulk_in_buffer,
908 ATEN2011_port->read_urb->
909 transfer_buffer_length,
910 ATEN2011_bulk_in_callback, ATEN2011_port);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800911
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -0800912 dbg("ATEN2011_open: bulkin endpoint is %d",
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800913 port->bulk_in_endpointAddress);
914 response = usb_submit_urb(ATEN2011_port->read_urb, GFP_KERNEL);
915 if (response) {
Greg Kroah-Hartman9d700542009-02-03 21:35:55 -0800916 err("%s - Error %d submitting control urb", __func__,
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800917 response);
918 }
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800919
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800920 /* initialize our wait queues */
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800921 init_waitqueue_head(&ATEN2011_port->wait_chase);
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800922 init_waitqueue_head(&ATEN2011_port->wait_command);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800923
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800924 /* initialize our icount structure */
925 memset(&(ATEN2011_port->icount), 0x00, sizeof(ATEN2011_port->icount));
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800926
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800927 /* initialize our port settings */
928 ATEN2011_port->shadowMCR = MCR_MASTER_IE; /* Must set to enable ints! */
Greg Kroah-Hartmana6364092009-01-29 16:57:23 -0800929 ATEN2011_port->chaseResponsePending = 0;
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800930 /* send a open port command */
Greg Kroah-Hartmana6364092009-01-29 16:57:23 -0800931 ATEN2011_port->open = 1;
Greg Kroah-Hartman87ed9bd2009-02-03 16:06:19 -0800932 /* ATEN2011_change_port_settings(ATEN2011_port,old_termios); */
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800933 /* Setup termios */
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800934 ATEN2011_set_termios(tty, port, &tmp_termios);
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800935 ATEN2011_port->icount.tx = 0;
936 ATEN2011_port->icount.rx = 0;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800937
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -0800938 dbg("usb_serial serial:%x ATEN2011_port:%x\nATEN2011_serial:%x usb_serial_port port:%x",
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800939 (unsigned int)serial, (unsigned int)ATEN2011_port,
940 (unsigned int)ATEN2011_serial, (unsigned int)port);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800941
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -0800942 return 0;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -0800943
944}
945
Greg Kroah-Hartmana7fefd12009-01-29 17:10:43 -0800946static int ATEN2011_chars_in_buffer(struct tty_struct *tty)
947{
948 struct usb_serial_port *port = tty->driver_data;
949 int i;
950 int chars = 0;
951 struct ATENINTL_port *ATEN2011_port;
952
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -0800953 /* dbg("%s"," ATEN2011_chars_in_buffer:entering ..........."); */
Greg Kroah-Hartmana7fefd12009-01-29 17:10:43 -0800954
Greg Kroah-Hartmanc3e06f22009-01-30 15:37:00 -0800955 ATEN2011_port = usb_get_serial_port_data(port);
Greg Kroah-Hartmana7fefd12009-01-29 17:10:43 -0800956 if (ATEN2011_port == NULL) {
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -0800957 dbg("%s", "ATEN2011_break:leaving ...........");
Greg Kroah-Hartmana7fefd12009-01-29 17:10:43 -0800958 return -1;
959 }
960
Greg Kroah-Hartman8532b092009-02-03 21:36:46 -0800961 for (i = 0; i < NUM_URBS; ++i)
962 if (ATEN2011_port->write_urb_pool[i]->status == -EINPROGRESS)
Greg Kroah-Hartmana7fefd12009-01-29 17:10:43 -0800963 chars += URB_TRANSFER_BUFFER_SIZE;
Greg Kroah-Hartman8532b092009-02-03 21:36:46 -0800964
Greg Kroah-Hartman9d700542009-02-03 21:35:55 -0800965 dbg("%s - returns %d", __func__, chars);
Greg Kroah-Hartman8532b092009-02-03 21:36:46 -0800966 return chars;
Greg Kroah-Hartmana7fefd12009-01-29 17:10:43 -0800967
968}
969
970static void ATEN2011_block_until_tx_empty(struct tty_struct *tty,
971 struct ATENINTL_port *ATEN2011_port)
972{
973 int timeout = HZ / 10;
974 int wait = 30;
975 int count;
976
977 while (1) {
Greg Kroah-Hartmana7fefd12009-01-29 17:10:43 -0800978 count = ATEN2011_chars_in_buffer(tty);
979
980 /* Check for Buffer status */
Greg Kroah-Hartman8532b092009-02-03 21:36:46 -0800981 if (count <= 0)
Greg Kroah-Hartmana7fefd12009-01-29 17:10:43 -0800982 return;
Greg Kroah-Hartmana7fefd12009-01-29 17:10:43 -0800983
984 /* Block the thread for a while */
985 interruptible_sleep_on_timeout(&ATEN2011_port->wait_chase,
986 timeout);
987
988 /* No activity.. count down section */
989 wait--;
990 if (wait == 0) {
Greg Kroah-Hartman9d700542009-02-03 21:35:55 -0800991 dbg("%s - TIMEOUT", __func__);
Greg Kroah-Hartmana7fefd12009-01-29 17:10:43 -0800992 return;
993 } else {
994 /* Reset timout value back to seconds */
995 wait = 30;
996 }
997 }
998}
999
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001000static void ATEN2011_close(struct tty_struct *tty, struct usb_serial_port *port,
1001 struct file *filp)
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001002{
1003 struct usb_serial *serial;
1004 struct ATENINTL_serial *ATEN2011_serial;
1005 struct ATENINTL_port *ATEN2011_port;
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001006 int no_urbs;
1007 __u16 Data;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001008
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -08001009 dbg("%s", "ATEN2011_close:entering...");
Greg Kroah-Hartmanf3415ee2009-01-30 15:31:38 -08001010 serial = port->serial;
1011
Greg Kroah-Hartman87ed9bd2009-02-03 16:06:19 -08001012 /* take the Adpater and port's private data */
Greg Kroah-Hartmanc3e06f22009-01-30 15:37:00 -08001013 ATEN2011_serial = usb_get_serial_data(serial);
1014 ATEN2011_port = usb_get_serial_port_data(port);
Greg Kroah-Hartman8532b092009-02-03 21:36:46 -08001015 if ((ATEN2011_serial == NULL) || (ATEN2011_port == NULL))
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001016 return;
Greg Kroah-Hartman8532b092009-02-03 21:36:46 -08001017
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001018 if (serial->dev) {
1019 /* flush and block(wait) until tx is empty */
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001020 ATEN2011_block_until_tx_empty(tty, ATEN2011_port);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001021 }
Greg Kroah-Hartman87ed9bd2009-02-03 16:06:19 -08001022 /* kill the ports URB's */
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001023 for (no_urbs = 0; no_urbs < NUM_URBS; no_urbs++)
1024 usb_kill_urb(ATEN2011_port->write_urb_pool[no_urbs]);
1025 /* Freeing Write URBs */
1026 for (no_urbs = 0; no_urbs < NUM_URBS; ++no_urbs) {
Greg Kroah-Hartman8532b092009-02-03 21:36:46 -08001027 kfree(ATEN2011_port->write_urb_pool[no_urbs]->transfer_buffer);
1028 usb_free_urb(ATEN2011_port->write_urb_pool[no_urbs]);
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001029 }
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001030 /* While closing port, shutdown all bulk read, write *
1031 * and interrupt read if they exists */
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001032 if (serial->dev) {
1033 if (ATEN2011_port->write_urb) {
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -08001034 dbg("%s", "Shutdown bulk write");
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001035 usb_kill_urb(ATEN2011_port->write_urb);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001036 }
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001037 if (ATEN2011_port->read_urb) {
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -08001038 dbg("%s", "Shutdown bulk read");
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001039 usb_kill_urb(ATEN2011_port->read_urb);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001040 }
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001041 if ((&ATEN2011_port->control_urb)) {
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -08001042 dbg("%s", "Shutdown control read");
Greg Kroah-Hartman87ed9bd2009-02-03 16:06:19 -08001043 /* usb_kill_urb (ATEN2011_port->control_urb); */
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001044
1045 }
1046 }
Greg Kroah-Hartman87ed9bd2009-02-03 16:06:19 -08001047 /* if(ATEN2011_port->ctrl_buf != NULL) */
1048 /* kfree(ATEN2011_port->ctrl_buf); */
1049 /* decrement the no.of open ports counter of an individual USB-serial adapter. */
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001050 ATEN2011_serial->NoOfOpenPorts--;
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -08001051 dbg("NoOfOpenPorts in close%d:in port%d",
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001052 ATEN2011_serial->NoOfOpenPorts, port->number);
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001053 if (ATEN2011_serial->NoOfOpenPorts == 0) {
Greg Kroah-Hartman87ed9bd2009-02-03 16:06:19 -08001054 /* stop the stus polling here */
Greg Kroah-Hartmana6364092009-01-29 16:57:23 -08001055 ATEN2011_serial->status_polling_started = 0;
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001056 if (ATEN2011_serial->interrupt_read_urb) {
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -08001057 dbg("%s", "Shutdown interrupt_read_urb");
Greg Kroah-Hartman87ed9bd2009-02-03 16:06:19 -08001058 /* ATEN2011_serial->interrupt_in_buffer=NULL; */
1059 /* usb_kill_urb (ATEN2011_serial->interrupt_read_urb); */
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001060 }
1061 }
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001062 if (ATEN2011_port->write_urb) {
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001063 /* if this urb had a transfer buffer already (old tx) free it */
Greg Kroah-Hartman8532b092009-02-03 21:36:46 -08001064 kfree(ATEN2011_port->write_urb->transfer_buffer);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001065 usb_free_urb(ATEN2011_port->write_urb);
1066 }
Greg Kroah-Hartman8532b092009-02-03 21:36:46 -08001067
Greg Kroah-Hartman87ed9bd2009-02-03 16:06:19 -08001068 /* clear the MCR & IER */
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001069 Data = 0x00;
Greg Kroah-Hartmane514adb2009-01-30 15:35:50 -08001070 set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001071 Data = 0x00;
Greg Kroah-Hartmane514adb2009-01-30 15:35:50 -08001072 set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001073
Greg Kroah-Hartmana6364092009-01-29 16:57:23 -08001074 ATEN2011_port->open = 0;
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -08001075 dbg("%s", "Leaving ............");
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001076
1077}
1078
Greg Kroah-Hartmana7fefd12009-01-29 17:10:43 -08001079static void ATEN2011_block_until_chase_response(struct tty_struct *tty,
1080 struct ATENINTL_port
1081 *ATEN2011_port)
1082{
1083 int timeout = 1 * HZ;
1084 int wait = 10;
1085 int count;
1086
1087 while (1) {
1088 count = ATEN2011_chars_in_buffer(tty);
1089
1090 /* Check for Buffer status */
1091 if (count <= 0) {
1092 ATEN2011_port->chaseResponsePending = 0;
1093 return;
1094 }
1095
1096 /* Block the thread for a while */
1097 interruptible_sleep_on_timeout(&ATEN2011_port->wait_chase,
1098 timeout);
1099 /* No activity.. count down section */
1100 wait--;
1101 if (wait == 0) {
Greg Kroah-Hartman9d700542009-02-03 21:35:55 -08001102 dbg("%s - TIMEOUT", __func__);
Greg Kroah-Hartmana7fefd12009-01-29 17:10:43 -08001103 return;
1104 } else {
1105 /* Reset timout value back to seconds */
1106 wait = 10;
1107 }
1108 }
1109
1110}
1111
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001112static void ATEN2011_break(struct tty_struct *tty, int break_state)
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001113{
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001114 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001115 unsigned char data;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001116 struct usb_serial *serial;
1117 struct ATENINTL_serial *ATEN2011_serial;
1118 struct ATENINTL_port *ATEN2011_port;
1119
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -08001120 dbg("%s", "Entering ...........");
1121 dbg("ATEN2011_break: Start");
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001122
Greg Kroah-Hartmanf3415ee2009-01-30 15:31:38 -08001123 serial = port->serial;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001124
Greg Kroah-Hartmanc3e06f22009-01-30 15:37:00 -08001125 ATEN2011_serial = usb_get_serial_data(serial);
1126 ATEN2011_port = usb_get_serial_port_data(port);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001127
Greg Kroah-Hartman8532b092009-02-03 21:36:46 -08001128 if ((ATEN2011_serial == NULL) || (ATEN2011_port == NULL))
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001129 return;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001130
1131 /* flush and chase */
Greg Kroah-Hartmana6364092009-01-29 16:57:23 -08001132 ATEN2011_port->chaseResponsePending = 1;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001133
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001134 if (serial->dev) {
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001135 /* flush and block until tx is empty */
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001136 ATEN2011_block_until_chase_response(tty, ATEN2011_port);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001137 }
1138
Greg Kroah-Hartman8532b092009-02-03 21:36:46 -08001139 if (break_state == -1)
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001140 data = ATEN2011_port->shadowLCR | LCR_SET_BREAK;
Greg Kroah-Hartman8532b092009-02-03 21:36:46 -08001141 else
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001142 data = ATEN2011_port->shadowLCR & ~LCR_SET_BREAK;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001143
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001144 ATEN2011_port->shadowLCR = data;
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -08001145 dbg("ATEN2011_break ATEN2011_port->shadowLCR is %x",
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001146 ATEN2011_port->shadowLCR);
Greg Kroah-Hartmane514adb2009-01-30 15:35:50 -08001147 set_uart_reg(port, LINE_CONTROL_REGISTER, ATEN2011_port->shadowLCR);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001148
1149 return;
1150}
1151
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001152static int ATEN2011_write_room(struct tty_struct *tty)
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001153{
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001154 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001155 int i;
1156 int room = 0;
1157 struct ATENINTL_port *ATEN2011_port;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001158
Greg Kroah-Hartmanc3e06f22009-01-30 15:37:00 -08001159 ATEN2011_port = usb_get_serial_port_data(port);
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001160 if (ATEN2011_port == NULL) {
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -08001161 dbg("%s", "ATEN2011_break:leaving ...........");
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001162 return -1;
1163 }
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001164
Greg Kroah-Hartman8532b092009-02-03 21:36:46 -08001165 for (i = 0; i < NUM_URBS; ++i)
1166 if (ATEN2011_port->write_urb_pool[i]->status != -EINPROGRESS)
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001167 room += URB_TRANSFER_BUFFER_SIZE;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001168
Greg Kroah-Hartman9d700542009-02-03 21:35:55 -08001169 dbg("%s - returns %d", __func__, room);
Greg Kroah-Hartman8532b092009-02-03 21:36:46 -08001170 return room;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001171
1172}
1173
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001174static int ATEN2011_write(struct tty_struct *tty, struct usb_serial_port *port,
1175 const unsigned char *data, int count)
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001176{
1177 int status;
1178 int i;
1179 int bytes_sent = 0;
1180 int transfer_size;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001181 int minor;
1182
1183 struct ATENINTL_port *ATEN2011_port;
1184 struct usb_serial *serial;
1185 struct ATENINTL_serial *ATEN2011_serial;
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001186 struct urb *urb;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001187 const unsigned char *current_position = data;
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001188 unsigned char *data1;
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -08001189 dbg("%s", "entering ...........");
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001190
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001191 serial = port->serial;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001192
Greg Kroah-Hartmanc3e06f22009-01-30 15:37:00 -08001193 ATEN2011_port = usb_get_serial_port_data(port);
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001194 if (ATEN2011_port == NULL) {
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -08001195 dbg("%s", "ATEN2011_port is NULL");
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001196 return -1;
1197 }
1198
Greg Kroah-Hartmanc3e06f22009-01-30 15:37:00 -08001199 ATEN2011_serial = usb_get_serial_data(serial);
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001200 if (ATEN2011_serial == NULL) {
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -08001201 dbg("%s", "ATEN2011_serial is NULL");
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001202 return -1;
1203 }
1204
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001205 /* try to find a free urb in the list */
1206 urb = NULL;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001207
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001208 for (i = 0; i < NUM_URBS; ++i) {
1209 if (ATEN2011_port->write_urb_pool[i]->status != -EINPROGRESS) {
1210 urb = ATEN2011_port->write_urb_pool[i];
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -08001211 dbg("URB:%d", i);
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001212 break;
1213 }
1214 }
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001215
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001216 if (urb == NULL) {
Greg Kroah-Hartman9d700542009-02-03 21:35:55 -08001217 dbg("%s - no more free urbs", __func__);
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001218 goto exit;
1219 }
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001220
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001221 if (urb->transfer_buffer == NULL) {
1222 urb->transfer_buffer =
1223 kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001224
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001225 if (urb->transfer_buffer == NULL) {
Greg Kroah-Hartman9d700542009-02-03 21:35:55 -08001226 err("%s no more kernel memory...", __func__);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001227 goto exit;
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001228 }
1229 }
1230 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
1231
Greg Kroah-Hartman907aff42009-01-29 17:17:48 -08001232 memcpy(urb->transfer_buffer, current_position, transfer_size);
Greg Kroah-Hartman9d700542009-02-03 21:35:55 -08001233 /* usb_serial_debug_data (__FILE__, __func__, transfer_size, urb->transfer_buffer); */
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001234
1235 /* fill urb with data and submit */
1236 minor = port->serial->minor;
Greg Kroah-Hartman8532b092009-02-03 21:36:46 -08001237 if (minor == SERIAL_TTY_NO_MINOR)
1238 minor = 0;
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001239 if ((ATEN2011_serial->ATEN2011_spectrum_2or4ports == 2)
1240 && (((__u16) port->number - (__u16) (minor)) != 0)) {
1241 usb_fill_bulk_urb(urb, ATEN2011_serial->serial->dev,
1242 usb_sndbulkpipe(ATEN2011_serial->serial->dev,
1243 (port->
1244 bulk_out_endpointAddress) +
1245 2), urb->transfer_buffer,
1246 transfer_size,
1247 ATEN2011_bulk_out_data_callback,
1248 ATEN2011_port);
1249 } else
1250
1251 usb_fill_bulk_urb(urb,
1252 ATEN2011_serial->serial->dev,
1253 usb_sndbulkpipe(ATEN2011_serial->serial->dev,
1254 port->
1255 bulk_out_endpointAddress),
1256 urb->transfer_buffer, transfer_size,
1257 ATEN2011_bulk_out_data_callback,
1258 ATEN2011_port);
1259
1260 data1 = urb->transfer_buffer;
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -08001261 dbg("bulkout endpoint is %d", port->bulk_out_endpointAddress);
Greg Kroah-Hartman87ed9bd2009-02-03 16:06:19 -08001262 /* for(i=0;i < urb->actual_length;i++) */
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -08001263 /* dbg("Data is %c ",data1[i]); */
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001264
1265 /* send it down the pipe */
1266 status = usb_submit_urb(urb, GFP_ATOMIC);
1267
1268 if (status) {
1269 err("%s - usb_submit_urb(write bulk) failed with status = %d",
Greg Kroah-Hartman9d700542009-02-03 21:35:55 -08001270 __func__, status);
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001271 bytes_sent = status;
1272 goto exit;
1273 }
1274 bytes_sent = transfer_size;
1275 ATEN2011_port->icount.tx += transfer_size;
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -08001276 dbg("ATEN2011_port->icount.tx is %d:", ATEN2011_port->icount.tx);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001277
Greg Kroah-Hartman8532b092009-02-03 21:36:46 -08001278exit:
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001279 return bytes_sent;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001280}
1281
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001282static void ATEN2011_throttle(struct tty_struct *tty)
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001283{
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001284 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001285 struct ATENINTL_port *ATEN2011_port;
1286 int status;
1287
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -08001288 dbg("- port %d", port->number);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001289
Greg Kroah-Hartmanc3e06f22009-01-30 15:37:00 -08001290 ATEN2011_port = usb_get_serial_port_data(port);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001291
1292 if (ATEN2011_port == NULL)
1293 return;
1294
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001295 if (!ATEN2011_port->open) {
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -08001296 dbg("%s", "port not opened");
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001297 return;
1298 }
1299
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -08001300 dbg("%s", "Entering .......... ");
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001301
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001302 if (!tty) {
Greg Kroah-Hartman9d700542009-02-03 21:35:55 -08001303 dbg("%s - no tty available", __func__);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001304 return;
1305 }
1306
1307 /* if we are implementing XON/XOFF, send the stop character */
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001308 if (I_IXOFF(tty)) {
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001309 unsigned char stop_char = STOP_CHAR(tty);
Greg Kroah-Hartman87ed9bd2009-02-03 16:06:19 -08001310 status = ATEN2011_write(tty, port, &stop_char, 1);
Greg Kroah-Hartman8532b092009-02-03 21:36:46 -08001311 if (status <= 0)
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001312 return;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001313 }
1314
1315 /* if we are implementing RTS/CTS, toggle that line */
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001316 if (tty->termios->c_cflag & CRTSCTS) {
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001317 ATEN2011_port->shadowMCR &= ~MCR_RTS;
Greg Kroah-Hartmane514adb2009-01-30 15:35:50 -08001318 status = set_uart_reg(port, MODEM_CONTROL_REGISTER,
1319 ATEN2011_port->shadowMCR);
Greg Kroah-Hartman8532b092009-02-03 21:36:46 -08001320 if (status < 0)
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001321 return;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001322 }
1323
1324 return;
1325}
1326
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001327static void ATEN2011_unthrottle(struct tty_struct *tty)
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001328{
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001329 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001330 int status;
Greg Kroah-Hartmanc3e06f22009-01-30 15:37:00 -08001331 struct ATENINTL_port *ATEN2011_port = usb_get_serial_port_data(port);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001332
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001333 if (ATEN2011_port == NULL)
1334 return;
1335
1336 if (!ATEN2011_port->open) {
Greg Kroah-Hartman9d700542009-02-03 21:35:55 -08001337 dbg("%s - port not opened", __func__);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001338 return;
1339 }
1340
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -08001341 dbg("%s", "Entering .......... ");
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001342
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001343 if (!tty) {
Greg Kroah-Hartman9d700542009-02-03 21:35:55 -08001344 dbg("%s - no tty available", __func__);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001345 return;
1346 }
1347
1348 /* if we are implementing XON/XOFF, send the start character */
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001349 if (I_IXOFF(tty)) {
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001350 unsigned char start_char = START_CHAR(tty);
Greg Kroah-Hartman87ed9bd2009-02-03 16:06:19 -08001351 status = ATEN2011_write(tty, port, &start_char, 1);
Greg Kroah-Hartman8532b092009-02-03 21:36:46 -08001352 if (status <= 0)
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001353 return;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001354 }
1355
1356 /* if we are implementing RTS/CTS, toggle that line */
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001357 if (tty->termios->c_cflag & CRTSCTS) {
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001358 ATEN2011_port->shadowMCR |= MCR_RTS;
Greg Kroah-Hartmane514adb2009-01-30 15:35:50 -08001359 status = set_uart_reg(port, MODEM_CONTROL_REGISTER,
1360 ATEN2011_port->shadowMCR);
Greg Kroah-Hartman8532b092009-02-03 21:36:46 -08001361 if (status < 0)
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001362 return;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001363 }
1364
1365 return;
1366}
1367
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001368static int ATEN2011_tiocmget(struct tty_struct *tty, struct file *file)
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001369{
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001370 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001371 struct ATENINTL_port *ATEN2011_port;
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001372 unsigned int result;
1373 __u16 msr;
1374 __u16 mcr;
Greg Kroah-Hartman87ed9bd2009-02-03 16:06:19 -08001375 /* unsigned int mcr; */
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001376 int status = 0;
Greg Kroah-Hartmanc3e06f22009-01-30 15:37:00 -08001377 ATEN2011_port = usb_get_serial_port_data(port);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001378
Greg Kroah-Hartman9d700542009-02-03 21:35:55 -08001379 dbg("%s - port %d", __func__, port->number);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001380
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001381 if (ATEN2011_port == NULL)
1382 return -ENODEV;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001383
Greg Kroah-Hartman12cbe0b2009-01-30 15:36:09 -08001384 status = get_uart_reg(port, MODEM_STATUS_REGISTER, &msr);
1385 status = get_uart_reg(port, MODEM_CONTROL_REGISTER, &mcr);
Greg Kroah-Hartman87ed9bd2009-02-03 16:06:19 -08001386 /* mcr = ATEN2011_port->shadowMCR; */
1387 /* COMMENT2: the Fallowing three line are commented for updating only MSR values */
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001388 result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0)
1389 | ((mcr & MCR_RTS) ? TIOCM_RTS : 0)
1390 | ((mcr & MCR_LOOPBACK) ? TIOCM_LOOP : 0)
1391 | ((msr & ATEN2011_MSR_CTS) ? TIOCM_CTS : 0)
1392 | ((msr & ATEN2011_MSR_CD) ? TIOCM_CAR : 0)
1393 | ((msr & ATEN2011_MSR_RI) ? TIOCM_RI : 0)
1394 | ((msr & ATEN2011_MSR_DSR) ? TIOCM_DSR : 0);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001395
Greg Kroah-Hartman9d700542009-02-03 21:35:55 -08001396 dbg("%s - 0x%04X", __func__, result);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001397
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001398 return result;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001399}
1400
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001401static int ATEN2011_tiocmset(struct tty_struct *tty, struct file *file,
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001402 unsigned int set, unsigned int clear)
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001403{
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001404 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001405 struct ATENINTL_port *ATEN2011_port;
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001406 unsigned int mcr;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001407 unsigned int status;
1408
Greg Kroah-Hartman9d700542009-02-03 21:35:55 -08001409 dbg("%s - port %d", __func__, port->number);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001410
Greg Kroah-Hartmanc3e06f22009-01-30 15:37:00 -08001411 ATEN2011_port = usb_get_serial_port_data(port);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001412
1413 if (ATEN2011_port == NULL)
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001414 return -ENODEV;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001415
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001416 mcr = ATEN2011_port->shadowMCR;
1417 if (clear & TIOCM_RTS)
1418 mcr &= ~MCR_RTS;
1419 if (clear & TIOCM_DTR)
1420 mcr &= ~MCR_DTR;
1421 if (clear & TIOCM_LOOP)
1422 mcr &= ~MCR_LOOPBACK;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001423
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001424 if (set & TIOCM_RTS)
1425 mcr |= MCR_RTS;
1426 if (set & TIOCM_DTR)
1427 mcr |= MCR_DTR;
1428 if (set & TIOCM_LOOP)
1429 mcr |= MCR_LOOPBACK;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001430
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001431 ATEN2011_port->shadowMCR = mcr;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001432
Greg Kroah-Hartmane514adb2009-01-30 15:35:50 -08001433 status = set_uart_reg(port, MODEM_CONTROL_REGISTER, mcr);
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001434 if (status < 0) {
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -08001435 dbg("setting MODEM_CONTROL_REGISTER Failed");
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001436 return -1;
1437 }
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001438
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001439 return 0;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001440}
1441
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001442static void ATEN2011_set_termios(struct tty_struct *tty,
1443 struct usb_serial_port *port,
1444 struct ktermios *old_termios)
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001445{
1446 int status;
1447 unsigned int cflag;
1448 struct usb_serial *serial;
1449 struct ATENINTL_port *ATEN2011_port;
Greg Kroah-Hartman8b4efbe2009-01-29 13:08:23 -08001450
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -08001451 dbg("ATEN2011_set_termios: START");
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001452
1453 serial = port->serial;
1454
Greg Kroah-Hartmanc3e06f22009-01-30 15:37:00 -08001455 ATEN2011_port = usb_get_serial_port_data(port);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001456
1457 if (ATEN2011_port == NULL)
1458 return;
1459
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001460 if (!ATEN2011_port->open) {
Greg Kroah-Hartman9d700542009-02-03 21:35:55 -08001461 dbg("%s - port not opened", __func__);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001462 return;
1463 }
1464
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -08001465 dbg("%s", "setting termios - ");
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001466
1467 cflag = tty->termios->c_cflag;
1468
Alan Cox20aa9e92009-04-07 18:43:43 +01001469 dbg("%s - cflag %08x iflag %08x", __func__,
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001470 tty->termios->c_cflag, RELEVANT_IFLAG(tty->termios->c_iflag));
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001471
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001472 if (old_termios) {
Greg Kroah-Hartman9d700542009-02-03 21:35:55 -08001473 dbg("%s - old clfag %08x old iflag %08x", __func__,
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001474 old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag));
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001475 }
1476
Greg Kroah-Hartman9d700542009-02-03 21:35:55 -08001477 dbg("%s - port %d", __func__, port->number);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001478
1479 /* change the port settings to the new ones specified */
1480
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001481 ATEN2011_change_port_settings(tty, ATEN2011_port, old_termios);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001482
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001483 if (!ATEN2011_port->read_urb) {
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -08001484 dbg("%s", "URB KILLED !!!!!");
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001485 return;
1486 }
1487
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001488 if (ATEN2011_port->read_urb->status != -EINPROGRESS) {
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001489 ATEN2011_port->read_urb->dev = serial->dev;
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001490 status = usb_submit_urb(ATEN2011_port->read_urb, GFP_ATOMIC);
1491 if (status) {
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -08001492 dbg
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001493 (" usb_submit_urb(read bulk) failed, status = %d",
1494 status);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001495 }
1496 }
1497 return;
1498}
1499
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001500static int get_lsr_info(struct tty_struct *tty,
1501 struct ATENINTL_port *ATEN2011_port,
Greg Kroah-Hartman907aff42009-01-29 17:17:48 -08001502 unsigned int __user *value)
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001503{
1504 int count;
1505 unsigned int result = 0;
1506
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001507 count = ATEN2011_chars_in_buffer(tty);
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001508 if (count == 0) {
Greg Kroah-Hartman9d700542009-02-03 21:35:55 -08001509 dbg("%s -- Empty", __func__);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001510 result = TIOCSER_TEMT;
1511 }
1512
1513 if (copy_to_user(value, &result, sizeof(int)))
1514 return -EFAULT;
1515 return 0;
1516}
1517
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001518static int get_number_bytes_avail(struct tty_struct *tty,
1519 struct ATENINTL_port *ATEN2011_port,
Greg Kroah-Hartman907aff42009-01-29 17:17:48 -08001520 unsigned int __user *value)
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001521{
1522 unsigned int result = 0;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001523
1524 if (!tty)
1525 return -ENOIOCTLCMD;
1526
1527 result = tty->read_cnt;
1528
Greg Kroah-Hartman9d700542009-02-03 21:35:55 -08001529 dbg("%s(%d) = %d", __func__, ATEN2011_port->port->number, result);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001530 if (copy_to_user(value, &result, sizeof(int)))
1531 return -EFAULT;
1532
1533 return -ENOIOCTLCMD;
1534}
1535
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001536static int set_modem_info(struct ATENINTL_port *ATEN2011_port, unsigned int cmd,
Greg Kroah-Hartman907aff42009-01-29 17:17:48 -08001537 unsigned int __user *value)
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001538{
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001539 unsigned int mcr;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001540 unsigned int arg;
1541 __u16 Data;
1542 int status;
1543 struct usb_serial_port *port;
1544
1545 if (ATEN2011_port == NULL)
1546 return -1;
1547
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001548 port = (struct usb_serial_port *)ATEN2011_port->port;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001549
1550 mcr = ATEN2011_port->shadowMCR;
1551
1552 if (copy_from_user(&arg, value, sizeof(int)))
1553 return -EFAULT;
1554
1555 switch (cmd) {
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001556 case TIOCMBIS:
1557 if (arg & TIOCM_RTS)
1558 mcr |= MCR_RTS;
1559 if (arg & TIOCM_DTR)
1560 mcr |= MCR_RTS;
1561 if (arg & TIOCM_LOOP)
1562 mcr |= MCR_LOOPBACK;
1563 break;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001564
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001565 case TIOCMBIC:
1566 if (arg & TIOCM_RTS)
1567 mcr &= ~MCR_RTS;
1568 if (arg & TIOCM_DTR)
1569 mcr &= ~MCR_RTS;
1570 if (arg & TIOCM_LOOP)
1571 mcr &= ~MCR_LOOPBACK;
1572 break;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001573
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001574 case TIOCMSET:
1575 /* turn off the RTS and DTR and LOOPBACK
1576 * and then only turn on what was asked to */
1577 mcr &= ~(MCR_RTS | MCR_DTR | MCR_LOOPBACK);
1578 mcr |= ((arg & TIOCM_RTS) ? MCR_RTS : 0);
1579 mcr |= ((arg & TIOCM_DTR) ? MCR_DTR : 0);
1580 mcr |= ((arg & TIOCM_LOOP) ? MCR_LOOPBACK : 0);
1581 break;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001582 }
1583
1584 ATEN2011_port->shadowMCR = mcr;
1585
1586 Data = ATEN2011_port->shadowMCR;
Greg Kroah-Hartmane514adb2009-01-30 15:35:50 -08001587 status = set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001588 if (status < 0) {
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -08001589 dbg("setting MODEM_CONTROL_REGISTER Failed");
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001590 return -1;
1591 }
1592
1593 return 0;
1594}
1595
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001596static int get_modem_info(struct ATENINTL_port *ATEN2011_port,
Greg Kroah-Hartman907aff42009-01-29 17:17:48 -08001597 unsigned int __user *value)
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001598{
1599 unsigned int result = 0;
1600 __u16 msr;
1601 unsigned int mcr = ATEN2011_port->shadowMCR;
Greg Kroah-Hartman12cbe0b2009-01-30 15:36:09 -08001602 int status;
1603
1604 status = get_uart_reg(ATEN2011_port->port, MODEM_STATUS_REGISTER, &msr);
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001605 result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0) /* 0x002 */
1606 |((mcr & MCR_RTS) ? TIOCM_RTS : 0) /* 0x004 */
1607 |((msr & ATEN2011_MSR_CTS) ? TIOCM_CTS : 0) /* 0x020 */
1608 |((msr & ATEN2011_MSR_CD) ? TIOCM_CAR : 0) /* 0x040 */
1609 |((msr & ATEN2011_MSR_RI) ? TIOCM_RI : 0) /* 0x080 */
1610 |((msr & ATEN2011_MSR_DSR) ? TIOCM_DSR : 0); /* 0x100 */
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001611
Greg Kroah-Hartman9d700542009-02-03 21:35:55 -08001612 dbg("%s -- %x", __func__, result);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001613
1614 if (copy_to_user(value, &result, sizeof(int)))
1615 return -EFAULT;
1616 return 0;
1617}
1618
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001619static int get_serial_info(struct ATENINTL_port *ATEN2011_port,
Greg Kroah-Hartman907aff42009-01-29 17:17:48 -08001620 struct serial_struct __user *retinfo)
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001621{
1622 struct serial_struct tmp;
1623
1624 if (ATEN2011_port == NULL)
1625 return -1;
1626
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001627 if (!retinfo)
1628 return -EFAULT;
1629
1630 memset(&tmp, 0, sizeof(tmp));
1631
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001632 tmp.type = PORT_16550A;
1633 tmp.line = ATEN2011_port->port->serial->minor;
1634 if (tmp.line == SERIAL_TTY_NO_MINOR)
1635 tmp.line = 0;
1636 tmp.port = ATEN2011_port->port->number;
1637 tmp.irq = 0;
1638 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
1639 tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
1640 tmp.baud_base = 9600;
1641 tmp.close_delay = 5 * HZ;
1642 tmp.closing_wait = 30 * HZ;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001643
1644 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1645 return -EFAULT;
1646 return 0;
1647}
1648
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001649static int ATEN2011_ioctl(struct tty_struct *tty, struct file *file,
1650 unsigned int cmd, unsigned long arg)
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001651{
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001652 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001653 struct ATENINTL_port *ATEN2011_port;
1654 struct async_icount cnow;
1655 struct async_icount cprev;
1656 struct serial_icounter_struct icount;
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001657 int ATENret = 0;
Greg Kroah-Hartman907aff42009-01-29 17:17:48 -08001658 unsigned int __user *user_arg = (unsigned int __user *)arg;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001659
Greg Kroah-Hartmanc3e06f22009-01-30 15:37:00 -08001660 ATEN2011_port = usb_get_serial_port_data(port);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001661
1662 if (ATEN2011_port == NULL)
1663 return -1;
1664
Greg Kroah-Hartman9d700542009-02-03 21:35:55 -08001665 dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001666
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001667 switch (cmd) {
1668 /* return number of bytes available */
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001669
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001670 case TIOCINQ:
Greg Kroah-Hartman9d700542009-02-03 21:35:55 -08001671 dbg("%s (%d) TIOCINQ", __func__, port->number);
Greg Kroah-Hartman907aff42009-01-29 17:17:48 -08001672 return get_number_bytes_avail(tty, ATEN2011_port, user_arg);
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001673 break;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001674
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001675 case TIOCOUTQ:
Greg Kroah-Hartman9d700542009-02-03 21:35:55 -08001676 dbg("%s (%d) TIOCOUTQ", __func__, port->number);
Greg Kroah-Hartman907aff42009-01-29 17:17:48 -08001677 return put_user(ATEN2011_chars_in_buffer(tty), user_arg);
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001678 break;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001679
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001680 case TIOCSERGETLSR:
Greg Kroah-Hartman9d700542009-02-03 21:35:55 -08001681 dbg("%s (%d) TIOCSERGETLSR", __func__, port->number);
Greg Kroah-Hartman907aff42009-01-29 17:17:48 -08001682 return get_lsr_info(tty, ATEN2011_port, user_arg);
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001683 return 0;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001684
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001685 case TIOCMBIS:
1686 case TIOCMBIC:
1687 case TIOCMSET:
Greg Kroah-Hartman9d700542009-02-03 21:35:55 -08001688 dbg("%s (%d) TIOCMSET/TIOCMBIC/TIOCMSET", __func__,
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001689 port->number);
Greg Kroah-Hartman87ed9bd2009-02-03 16:06:19 -08001690 ATENret = set_modem_info(ATEN2011_port, cmd, user_arg);
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001691 return ATENret;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001692
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001693 case TIOCMGET:
Greg Kroah-Hartman9d700542009-02-03 21:35:55 -08001694 dbg("%s (%d) TIOCMGET", __func__, port->number);
Greg Kroah-Hartman907aff42009-01-29 17:17:48 -08001695 return get_modem_info(ATEN2011_port, user_arg);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001696
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001697 case TIOCGSERIAL:
Greg Kroah-Hartman9d700542009-02-03 21:35:55 -08001698 dbg("%s (%d) TIOCGSERIAL", __func__, port->number);
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001699 return get_serial_info(ATEN2011_port,
Greg Kroah-Hartman907aff42009-01-29 17:17:48 -08001700 (struct serial_struct __user *)arg);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001701
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001702 case TIOCSSERIAL:
Greg Kroah-Hartman9d700542009-02-03 21:35:55 -08001703 dbg("%s (%d) TIOCSSERIAL", __func__, port->number);
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001704 break;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001705
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001706 case TIOCMIWAIT:
Greg Kroah-Hartman9d700542009-02-03 21:35:55 -08001707 dbg("%s (%d) TIOCMIWAIT", __func__, port->number);
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001708 cprev = ATEN2011_port->icount;
1709 while (1) {
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001710 /* see if a signal did it */
1711 if (signal_pending(current))
1712 return -ERESTARTSYS;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001713 cnow = ATEN2011_port->icount;
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001714 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1715 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1716 return -EIO; /* no change => error */
1717 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1718 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1719 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1720 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
1721 return 0;
1722 }
1723 cprev = cnow;
1724 }
1725 /* NOTREACHED */
1726 break;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001727
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001728 case TIOCGICOUNT:
1729 cnow = ATEN2011_port->icount;
1730 icount.cts = cnow.cts;
1731 icount.dsr = cnow.dsr;
1732 icount.rng = cnow.rng;
1733 icount.dcd = cnow.dcd;
1734 icount.rx = cnow.rx;
1735 icount.tx = cnow.tx;
1736 icount.frame = cnow.frame;
1737 icount.overrun = cnow.overrun;
1738 icount.parity = cnow.parity;
1739 icount.brk = cnow.brk;
1740 icount.buf_overrun = cnow.buf_overrun;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001741
Greg Kroah-Hartman9d700542009-02-03 21:35:55 -08001742 dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __func__,
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001743 port->number, icount.rx, icount.tx);
Greg Kroah-Hartman907aff42009-01-29 17:17:48 -08001744 if (copy_to_user((void __user *)arg, &icount, sizeof(icount)))
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001745 return -EFAULT;
1746 return 0;
1747
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001748 default:
1749 break;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001750 }
1751
1752 return -ENOIOCTLCMD;
1753}
1754
Greg Kroah-Hartmana7fefd12009-01-29 17:10:43 -08001755static int ATEN2011_calc_baud_rate_divisor(int baudRate, int *divisor,
Greg Kroah-Hartman8532b092009-02-03 21:36:46 -08001756 __u16 *clk_sel_val)
Greg Kroah-Hartmana7fefd12009-01-29 17:10:43 -08001757{
Greg Kroah-Hartman9d700542009-02-03 21:35:55 -08001758 dbg("%s - %d", __func__, baudRate);
Greg Kroah-Hartmana7fefd12009-01-29 17:10:43 -08001759
1760 if (baudRate <= 115200) {
1761 *divisor = 115200 / baudRate;
1762 *clk_sel_val = 0x0;
1763 }
1764 if ((baudRate > 115200) && (baudRate <= 230400)) {
1765 *divisor = 230400 / baudRate;
1766 *clk_sel_val = 0x10;
1767 } else if ((baudRate > 230400) && (baudRate <= 403200)) {
1768 *divisor = 403200 / baudRate;
1769 *clk_sel_val = 0x20;
1770 } else if ((baudRate > 403200) && (baudRate <= 460800)) {
1771 *divisor = 460800 / baudRate;
1772 *clk_sel_val = 0x30;
1773 } else if ((baudRate > 460800) && (baudRate <= 806400)) {
1774 *divisor = 806400 / baudRate;
1775 *clk_sel_val = 0x40;
1776 } else if ((baudRate > 806400) && (baudRate <= 921600)) {
1777 *divisor = 921600 / baudRate;
1778 *clk_sel_val = 0x50;
1779 } else if ((baudRate > 921600) && (baudRate <= 1572864)) {
1780 *divisor = 1572864 / baudRate;
1781 *clk_sel_val = 0x60;
1782 } else if ((baudRate > 1572864) && (baudRate <= 3145728)) {
1783 *divisor = 3145728 / baudRate;
1784 *clk_sel_val = 0x70;
1785 }
1786 return 0;
1787}
1788
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001789static int ATEN2011_send_cmd_write_baud_rate(struct ATENINTL_port
1790 *ATEN2011_port, int baudRate)
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001791{
1792 int divisor = 0;
1793 int status;
1794 __u16 Data;
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001795 unsigned char number;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001796 __u16 clk_sel_val;
1797 struct usb_serial_port *port;
1798 int minor;
1799
1800 if (ATEN2011_port == NULL)
1801 return -1;
1802
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001803 port = (struct usb_serial_port *)ATEN2011_port->port;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001804
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -08001805 dbg("%s", "Entering .......... ");
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001806
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001807 minor = ATEN2011_port->port->serial->minor;
1808 if (minor == SERIAL_TTY_NO_MINOR)
1809 minor = 0;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001810 number = ATEN2011_port->port->number - minor;
1811
Greg Kroah-Hartman9d700542009-02-03 21:35:55 -08001812 dbg("%s - port = %d, baud = %d", __func__,
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001813 ATEN2011_port->port->number, baudRate);
Greg Kroah-Hartman87ed9bd2009-02-03 16:06:19 -08001814 /* reset clk_uart_sel in spregOffset */
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001815 if (baudRate > 115200) {
1816#ifdef HW_flow_control
Greg Kroah-Hartman87ed9bd2009-02-03 16:06:19 -08001817 /*
1818 * NOTE: need to see the pther register to modify
1819 * setting h/w flow control bit to 1;
1820 */
1821 /* Data = ATEN2011_port->shadowMCR; */
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001822 Data = 0x2b;
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001823 ATEN2011_port->shadowMCR = Data;
Greg Kroah-Hartmane514adb2009-01-30 15:35:50 -08001824 status = set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001825 if (status < 0) {
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -08001826 dbg("Writing spreg failed in set_serial_baud");
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001827 return -1;
1828 }
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001829#endif
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001830
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001831 } else {
1832#ifdef HW_flow_control
Greg Kroah-Hartman87ed9bd2009-02-03 16:06:19 -08001833 /* setting h/w flow control bit to 0; */
1834 /* Data = ATEN2011_port->shadowMCR; */
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001835 Data = 0xb;
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001836 ATEN2011_port->shadowMCR = Data;
Greg Kroah-Hartmane514adb2009-01-30 15:35:50 -08001837 status = set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001838 if (status < 0) {
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -08001839 dbg("Writing spreg failed in set_serial_baud");
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001840 return -1;
1841 }
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001842#endif
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001843
1844 }
1845
Greg Kroah-Hartman8532b092009-02-03 21:36:46 -08001846 if (1) /* baudRate <= 115200) */ {
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001847 clk_sel_val = 0x0;
1848 Data = 0x0;
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001849 status =
1850 ATEN2011_calc_baud_rate_divisor(baudRate, &divisor,
1851 &clk_sel_val);
Greg Kroah-Hartmane9abe302009-01-30 15:35:31 -08001852 status = get_reg_sync(port, ATEN2011_port->SpRegOffset, &Data);
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001853 if (status < 0) {
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -08001854 dbg("reading spreg failed in set_serial_baud");
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001855 return -1;
1856 }
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001857 Data = (Data & 0x8f) | clk_sel_val;
Greg Kroah-Hartman8433b6a2009-01-30 15:32:18 -08001858 status = set_reg_sync(port, ATEN2011_port->SpRegOffset, Data);
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001859 if (status < 0) {
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -08001860 dbg("Writing spreg failed in set_serial_baud");
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001861 return -1;
1862 }
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001863 /* Calculate the Divisor */
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001864
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001865 if (status) {
Greg Kroah-Hartman9d700542009-02-03 21:35:55 -08001866 err("%s - bad baud rate", __func__);
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -08001867 dbg("%s", "bad baud rate");
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001868 return status;
1869 }
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001870 /* Enable access to divisor latch */
1871 Data = ATEN2011_port->shadowLCR | SERIAL_LCR_DLAB;
1872 ATEN2011_port->shadowLCR = Data;
Greg Kroah-Hartmane514adb2009-01-30 15:35:50 -08001873 set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001874
1875 /* Write the divisor */
Greg Kroah-Hartmana6364092009-01-29 16:57:23 -08001876 Data = (unsigned char)(divisor & 0xff);
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -08001877 dbg("set_serial_baud Value to write DLL is %x", Data);
Greg Kroah-Hartmane514adb2009-01-30 15:35:50 -08001878 set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001879
Greg Kroah-Hartmana6364092009-01-29 16:57:23 -08001880 Data = (unsigned char)((divisor & 0xff00) >> 8);
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -08001881 dbg("set_serial_baud Value to write DLM is %x", Data);
Greg Kroah-Hartmane514adb2009-01-30 15:35:50 -08001882 set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001883
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001884 /* Disable access to divisor latch */
1885 Data = ATEN2011_port->shadowLCR & ~SERIAL_LCR_DLAB;
1886 ATEN2011_port->shadowLCR = Data;
Greg Kroah-Hartmane514adb2009-01-30 15:35:50 -08001887 set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001888
1889 }
1890
1891 return status;
1892}
1893
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001894static void ATEN2011_change_port_settings(struct tty_struct *tty,
1895 struct ATENINTL_port *ATEN2011_port,
1896 struct ktermios *old_termios)
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001897{
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001898 int baud;
1899 unsigned cflag;
1900 unsigned iflag;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001901 __u8 lData;
1902 __u8 lParity;
1903 __u8 lStop;
1904 int status;
1905 __u16 Data;
1906 struct usb_serial_port *port;
1907 struct usb_serial *serial;
1908
1909 if (ATEN2011_port == NULL)
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001910 return;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001911
1912 port = (struct usb_serial_port *)ATEN2011_port->port;
1913
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001914 serial = port->serial;
1915
Greg Kroah-Hartman9d700542009-02-03 21:35:55 -08001916 dbg("%s - port %d", __func__, ATEN2011_port->port->number);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001917
Greg Kroah-Hartmanc725e432009-01-30 15:29:52 -08001918 if (!ATEN2011_port->open) {
Greg Kroah-Hartman9d700542009-02-03 21:35:55 -08001919 dbg("%s - port not opened", __func__);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001920 return;
1921 }
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001922
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001923 if ((!tty) || (!tty->termios)) {
Greg Kroah-Hartman9d700542009-02-03 21:35:55 -08001924 dbg("%s - no tty structures", __func__);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001925 return;
1926 }
1927
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -08001928 dbg("%s", "Entering .......... ");
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001929
1930 lData = LCR_BITS_8;
1931 lStop = LCR_STOP_1;
1932 lParity = LCR_PAR_NONE;
1933
1934 cflag = tty->termios->c_cflag;
1935 iflag = tty->termios->c_iflag;
1936
1937 /* Change the number of bits */
1938
Greg Kroah-Hartman87ed9bd2009-02-03 16:06:19 -08001939 /* COMMENT1: the below Line"if(cflag & CSIZE)" is added for the errors we get for serial loop data test i.e serial_loopback.pl -v */
1940 /* if(cflag & CSIZE) */
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001941 {
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001942 switch (cflag & CSIZE) {
1943 case CS5:
1944 lData = LCR_BITS_5;
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001945 break;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001946
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001947 case CS6:
1948 lData = LCR_BITS_6;
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001949 break;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001950
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001951 case CS7:
1952 lData = LCR_BITS_7;
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001953 break;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001954 default:
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001955 case CS8:
1956 lData = LCR_BITS_8;
1957 break;
1958 }
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001959 }
1960 /* Change the Parity bit */
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001961 if (cflag & PARENB) {
1962 if (cflag & PARODD) {
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001963 lParity = LCR_PAR_ODD;
Greg Kroah-Hartman9d700542009-02-03 21:35:55 -08001964 dbg("%s - parity = odd", __func__);
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001965 } else {
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001966 lParity = LCR_PAR_EVEN;
Greg Kroah-Hartman9d700542009-02-03 21:35:55 -08001967 dbg("%s - parity = even", __func__);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001968 }
1969
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001970 } else {
Greg Kroah-Hartman9d700542009-02-03 21:35:55 -08001971 dbg("%s - parity = none", __func__);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001972 }
1973
Greg Kroah-Hartman8532b092009-02-03 21:36:46 -08001974 if (cflag & CMSPAR)
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001975 lParity = lParity | 0x20;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001976
1977 /* Change the Stop bit */
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001978 if (cflag & CSTOPB) {
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001979 lStop = LCR_STOP_2;
Greg Kroah-Hartman9d700542009-02-03 21:35:55 -08001980 dbg("%s - stop bits = 2", __func__);
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001981 } else {
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001982 lStop = LCR_STOP_1;
Greg Kroah-Hartman9d700542009-02-03 21:35:55 -08001983 dbg("%s - stop bits = 1", __func__);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001984 }
1985
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001986 /* Update the LCR with the correct value */
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001987 ATEN2011_port->shadowLCR &=
1988 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001989 ATEN2011_port->shadowLCR |= (lData | lParity | lStop);
1990
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -08001991 dbg
1992 ("ATEN2011_change_port_settings ATEN2011_port->shadowLCR is %x",
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08001993 ATEN2011_port->shadowLCR);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001994 /* Disable Interrupts */
1995 Data = 0x00;
Greg Kroah-Hartmane514adb2009-01-30 15:35:50 -08001996 set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08001997
1998 Data = 0x00;
Greg Kroah-Hartmane514adb2009-01-30 15:35:50 -08001999 set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08002000
2001 Data = 0xcf;
Greg Kroah-Hartmane514adb2009-01-30 15:35:50 -08002002 set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08002003
2004 /* Send the updated LCR value to the ATEN2011 */
2005 Data = ATEN2011_port->shadowLCR;
2006
Greg Kroah-Hartmane514adb2009-01-30 15:35:50 -08002007 set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08002008
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08002009 Data = 0x00b;
2010 ATEN2011_port->shadowMCR = Data;
Greg Kroah-Hartmane514adb2009-01-30 15:35:50 -08002011 set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08002012 Data = 0x00b;
Greg Kroah-Hartmane514adb2009-01-30 15:35:50 -08002013 set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08002014
2015 /* set up the MCR register and send it to the ATEN2011 */
2016
2017 ATEN2011_port->shadowMCR = MCR_MASTER_IE;
Greg Kroah-Hartman8532b092009-02-03 21:36:46 -08002018 if (cflag & CBAUD)
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08002019 ATEN2011_port->shadowMCR |= (MCR_DTR | MCR_RTS);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08002020
Greg Kroah-Hartman8532b092009-02-03 21:36:46 -08002021 if (cflag & CRTSCTS)
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08002022 ATEN2011_port->shadowMCR |= (MCR_XON_ANY);
Greg Kroah-Hartman8532b092009-02-03 21:36:46 -08002023 else
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08002024 ATEN2011_port->shadowMCR &= ~(MCR_XON_ANY);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08002025
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08002026 Data = ATEN2011_port->shadowMCR;
Greg Kroah-Hartmane514adb2009-01-30 15:35:50 -08002027 set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08002028
2029 /* Determine divisor based on baud rate */
2030 baud = tty_get_baud_rate(tty);
2031
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08002032 if (!baud) {
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08002033 /* pick a default, any default... */
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -08002034 dbg("%s", "Picked default baud...");
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08002035 baud = 9600;
2036 }
2037
Greg Kroah-Hartman9d700542009-02-03 21:35:55 -08002038 dbg("%s - baud rate = %d", __func__, baud);
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08002039 status = ATEN2011_send_cmd_write_baud_rate(ATEN2011_port, baud);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08002040
2041 /* Enable Interrupts */
2042 Data = 0x0c;
Greg Kroah-Hartmane514adb2009-01-30 15:35:50 -08002043 set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08002044
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08002045 if (ATEN2011_port->read_urb->status != -EINPROGRESS) {
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08002046 ATEN2011_port->read_urb->dev = serial->dev;
2047
2048 status = usb_submit_urb(ATEN2011_port->read_urb, GFP_ATOMIC);
2049
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08002050 if (status) {
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -08002051 dbg
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08002052 (" usb_submit_urb(read bulk) failed, status = %d",
2053 status);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08002054 }
2055 }
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -08002056 dbg
2057 ("ATEN2011_change_port_settings ATEN2011_port->shadowLCR is End %x",
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08002058 ATEN2011_port->shadowLCR);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08002059
2060 return;
2061}
2062
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08002063static int ATEN2011_calc_num_ports(struct usb_serial *serial)
2064{
2065
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08002066 __u16 Data = 0x00;
2067 int ret = 0;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08002068 int ATEN2011_2or4ports;
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08002069 ret = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
2070 ATEN_RDREQ, ATEN_RD_RTYPE, 0, GPIO_REGISTER,
2071 &Data, VENDOR_READ_LENGTH, ATEN_WDR_TIMEOUT);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08002072
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08002073/* ghostgum: here is where the problem appears to bet */
2074/* Which of the following are needed? */
2075/* Greg used the serial->type->num_ports=2 */
2076/* But the code in the ATEN2011_open relies on serial->num_ports=2 */
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08002077 if ((Data & 0x01) == 0) {
2078 ATEN2011_2or4ports = 2;
2079 serial->type->num_ports = 2;
2080 serial->num_ports = 2;
2081 }
Greg Kroah-Hartman87ed9bd2009-02-03 16:06:19 -08002082 /* else if(serial->interface->cur_altsetting->desc.bNumEndpoints == 9) */
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08002083 else {
2084 ATEN2011_2or4ports = 4;
2085 serial->type->num_ports = 4;
2086 serial->num_ports = 4;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08002087
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08002088 }
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08002089
2090 return ATEN2011_2or4ports;
2091}
2092
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08002093static int ATEN2011_startup(struct usb_serial *serial)
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08002094{
2095 struct ATENINTL_serial *ATEN2011_serial;
2096 struct ATENINTL_port *ATEN2011_port;
2097 struct usb_device *dev;
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08002098 int i, status;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08002099 int minor;
2100
2101 __u16 Data;
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -08002102 dbg("%s", " ATEN2011_startup :entering..........");
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08002103
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08002104 if (!serial) {
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -08002105 dbg("%s", "Invalid Handler");
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08002106 return -1;
2107 }
2108
2109 dev = serial->dev;
2110
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -08002111 dbg("%s", "Entering...");
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08002112
2113 /* create our private serial structure */
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08002114 ATEN2011_serial = kzalloc(sizeof(struct ATENINTL_serial), GFP_KERNEL);
2115 if (ATEN2011_serial == NULL) {
Greg Kroah-Hartman9d700542009-02-03 21:35:55 -08002116 err("%s - Out of memory", __func__);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08002117 return -ENOMEM;
2118 }
2119
2120 /* resetting the private structure field values to zero */
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08002121 memset(ATEN2011_serial, 0, sizeof(struct ATENINTL_serial));
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08002122
2123 ATEN2011_serial->serial = serial;
Greg Kroah-Hartman87ed9bd2009-02-03 16:06:19 -08002124 /* initilize status polling flag to 0 */
Greg Kroah-Hartmana6364092009-01-29 16:57:23 -08002125 ATEN2011_serial->status_polling_started = 0;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08002126
Greg Kroah-Hartmanc3e06f22009-01-30 15:37:00 -08002127 usb_set_serial_data(serial, ATEN2011_serial);
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08002128 ATEN2011_serial->ATEN2011_spectrum_2or4ports =
2129 ATEN2011_calc_num_ports(serial);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08002130 /* we set up the pointers to the endpoints in the ATEN2011_open *
2131 * function, as the structures aren't created yet. */
2132
2133 /* set up port private structures */
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08002134 for (i = 0; i < serial->num_ports; ++i) {
2135 ATEN2011_port =
2136 kmalloc(sizeof(struct ATENINTL_port), GFP_KERNEL);
2137 if (ATEN2011_port == NULL) {
Greg Kroah-Hartman9d700542009-02-03 21:35:55 -08002138 err("%s - Out of memory", __func__);
Greg Kroah-Hartmanc3e06f22009-01-30 15:37:00 -08002139 usb_set_serial_data(serial, NULL);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08002140 kfree(ATEN2011_serial);
2141 return -ENOMEM;
2142 }
2143 memset(ATEN2011_port, 0, sizeof(struct ATENINTL_port));
2144
Greg Kroah-Hartman87ed9bd2009-02-03 16:06:19 -08002145 /*
2146 * Initialize all port interrupt end point to port 0
2147 * int endpoint. Our device has only one interrupt end point
2148 * comman to all port
2149 */
2150 /* serial->port[i]->interrupt_in_endpointAddress = serial->port[0]->interrupt_in_endpointAddress; */
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08002151
2152 ATEN2011_port->port = serial->port[i];
Greg Kroah-Hartmanc3e06f22009-01-30 15:37:00 -08002153 usb_set_serial_port_data(serial->port[i], ATEN2011_port);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08002154
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08002155 minor = serial->port[i]->serial->minor;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08002156 if (minor == SERIAL_TTY_NO_MINOR)
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08002157 minor = 0;
2158 ATEN2011_port->port_num =
2159 ((serial->port[i]->number - minor) + 1);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08002160
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08002161 if (ATEN2011_port->port_num == 1) {
2162 ATEN2011_port->SpRegOffset = 0x0;
2163 ATEN2011_port->ControlRegOffset = 0x1;
2164 ATEN2011_port->DcrRegOffset = 0x4;
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08002165 } else if ((ATEN2011_port->port_num == 2)
2166 && (ATEN2011_serial->ATEN2011_spectrum_2or4ports ==
2167 4)) {
2168 ATEN2011_port->SpRegOffset = 0x8;
2169 ATEN2011_port->ControlRegOffset = 0x9;
2170 ATEN2011_port->DcrRegOffset = 0x16;
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08002171 } else if ((ATEN2011_port->port_num == 2)
2172 && (ATEN2011_serial->ATEN2011_spectrum_2or4ports ==
2173 2)) {
2174 ATEN2011_port->SpRegOffset = 0xa;
2175 ATEN2011_port->ControlRegOffset = 0xb;
2176 ATEN2011_port->DcrRegOffset = 0x19;
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08002177 } else if ((ATEN2011_port->port_num == 3)
2178 && (ATEN2011_serial->ATEN2011_spectrum_2or4ports ==
2179 4)) {
2180 ATEN2011_port->SpRegOffset = 0xa;
2181 ATEN2011_port->ControlRegOffset = 0xb;
2182 ATEN2011_port->DcrRegOffset = 0x19;
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08002183 } else if ((ATEN2011_port->port_num == 4)
2184 && (ATEN2011_serial->ATEN2011_spectrum_2or4ports ==
2185 4)) {
2186 ATEN2011_port->SpRegOffset = 0xc;
2187 ATEN2011_port->ControlRegOffset = 0xd;
2188 ATEN2011_port->DcrRegOffset = 0x1c;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08002189 }
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08002190
Greg Kroah-Hartmanc3e06f22009-01-30 15:37:00 -08002191 usb_set_serial_port_data(serial->port[i], ATEN2011_port);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08002192
Greg Kroah-Hartman87ed9bd2009-02-03 16:06:19 -08002193 /* enable rx_disable bit in control register */
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08002194
Greg Kroah-Hartmane9abe302009-01-30 15:35:31 -08002195 status = get_reg_sync(serial->port[i],
2196 ATEN2011_port->ControlRegOffset, &Data);
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08002197 if (status < 0) {
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -08002198 dbg("Reading ControlReg failed status-0x%x",
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08002199 status);
2200 break;
2201 } else
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -08002202 dbg
2203 ("ControlReg Reading success val is %x, status%d",
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08002204 Data, status);
Greg Kroah-Hartman87ed9bd2009-02-03 16:06:19 -08002205 Data |= 0x08; /* setting driver done bit */
2206 Data |= 0x04; /* sp1_bit to have cts change reflect in modem status reg */
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08002207
Greg Kroah-Hartman87ed9bd2009-02-03 16:06:19 -08002208 /* Data |= 0x20; */ /* rx_disable bit */
Greg Kroah-Hartman8433b6a2009-01-30 15:32:18 -08002209 status = set_reg_sync(serial->port[i],
2210 ATEN2011_port->ControlRegOffset, Data);
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08002211 if (status < 0) {
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -08002212 dbg
2213 ("Writing ControlReg failed(rx_disable) status-0x%x",
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08002214 status);
2215 break;
2216 } else
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -08002217 dbg
2218 ("ControlReg Writing success(rx_disable) status%d",
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08002219 status);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08002220
Greg Kroah-Hartman87ed9bd2009-02-03 16:06:19 -08002221 /*
2222 * Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2
2223 * and 0x24 in DCR3
2224 */
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08002225 Data = 0x01;
Greg Kroah-Hartman8433b6a2009-01-30 15:32:18 -08002226 status = set_reg_sync(serial->port[i],
2227 (__u16)(ATEN2011_port->DcrRegOffset + 0),
2228 Data);
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08002229 if (status < 0) {
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -08002230 dbg("Writing DCR0 failed status-0x%x", status);
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08002231 break;
2232 } else
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -08002233 dbg("DCR0 Writing success status%d", status);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08002234
2235 Data = 0x05;
Greg Kroah-Hartman8433b6a2009-01-30 15:32:18 -08002236 status = set_reg_sync(serial->port[i],
2237 (__u16)(ATEN2011_port->DcrRegOffset + 1),
2238 Data);
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08002239 if (status < 0) {
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -08002240 dbg("Writing DCR1 failed status-0x%x", status);
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08002241 break;
2242 } else
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -08002243 dbg("DCR1 Writing success status%d", status);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08002244
2245 Data = 0x24;
Greg Kroah-Hartman8433b6a2009-01-30 15:32:18 -08002246 status = set_reg_sync(serial->port[i],
2247 (__u16)(ATEN2011_port->DcrRegOffset + 2),
2248 Data);
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08002249 if (status < 0) {
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -08002250 dbg("Writing DCR2 failed status-0x%x", status);
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08002251 break;
2252 } else
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -08002253 dbg("DCR2 Writing success status%d", status);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08002254
Greg Kroah-Hartman87ed9bd2009-02-03 16:06:19 -08002255 /* write values in clkstart0x0 and clkmulti 0x20 */
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08002256 Data = 0x0;
Greg Kroah-Hartman8433b6a2009-01-30 15:32:18 -08002257 status = set_reg_sync(serial->port[i], CLK_START_VALUE_REGISTER,
2258 Data);
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08002259 if (status < 0) {
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -08002260 dbg
2261 ("Writing CLK_START_VALUE_REGISTER failed status-0x%x",
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08002262 status);
2263 break;
2264 } else
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -08002265 dbg
2266 ("CLK_START_VALUE_REGISTER Writing success status%d",
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08002267 status);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08002268
2269 Data = 0x20;
Greg Kroah-Hartman8433b6a2009-01-30 15:32:18 -08002270 status = set_reg_sync(serial->port[i], CLK_MULTI_REGISTER,
2271 Data);
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08002272 if (status < 0) {
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -08002273 dbg
2274 ("Writing CLK_MULTI_REGISTER failed status-0x%x",
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08002275 status);
2276 break;
2277 } else
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -08002278 dbg("CLK_MULTI_REGISTER Writing success status%d",
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08002279 status);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08002280
Greg Kroah-Hartman87ed9bd2009-02-03 16:06:19 -08002281 /* Zero Length flag register */
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08002282 if ((ATEN2011_port->port_num != 1)
2283 && (ATEN2011_serial->ATEN2011_spectrum_2or4ports == 2)) {
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08002284
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08002285 Data = 0xff;
Greg Kroah-Hartman8433b6a2009-01-30 15:32:18 -08002286 status = set_reg_sync(serial->port[i],
2287 (__u16)(ZLP_REG1 + ((__u16)ATEN2011_port->port_num)),
2288 Data);
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -08002289 dbg("ZLIP offset%x",
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08002290 (__u16) (ZLP_REG1 +
2291 ((__u16) ATEN2011_port->port_num)));
2292 if (status < 0) {
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -08002293 dbg
2294 ("Writing ZLP_REG%d failed status-0x%x",
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08002295 i + 2, status);
2296 break;
2297 } else
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -08002298 dbg("ZLP_REG%d Writing success status%d",
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08002299 i + 2, status);
2300 } else {
2301 Data = 0xff;
Greg Kroah-Hartman8433b6a2009-01-30 15:32:18 -08002302 status = set_reg_sync(serial->port[i],
2303 (__u16)(ZLP_REG1 + ((__u16)ATEN2011_port->port_num) - 0x1),
2304 Data);
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -08002305 dbg("ZLIP offset%x",
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08002306 (__u16) (ZLP_REG1 +
2307 ((__u16) ATEN2011_port->port_num) -
2308 0x1));
2309 if (status < 0) {
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -08002310 dbg
2311 ("Writing ZLP_REG%d failed status-0x%x",
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08002312 i + 1, status);
2313 break;
2314 } else
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -08002315 dbg("ZLP_REG%d Writing success status%d",
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08002316 i + 1, status);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08002317
2318 }
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08002319 ATEN2011_port->control_urb = usb_alloc_urb(0, GFP_ATOMIC);
2320 ATEN2011_port->ctrl_buf = kmalloc(16, GFP_KERNEL);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08002321
2322 }
2323
Greg Kroah-Hartman87ed9bd2009-02-03 16:06:19 -08002324 /* Zero Length flag enable */
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08002325 Data = 0x0f;
Greg Kroah-Hartman8433b6a2009-01-30 15:32:18 -08002326 status = set_reg_sync(serial->port[0], ZLP_REG5, Data);
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08002327 if (status < 0) {
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -08002328 dbg("Writing ZLP_REG5 failed status-0x%x", status);
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08002329 return -1;
2330 } else
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -08002331 dbg("ZLP_REG5 Writing success status%d", status);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08002332
2333 /* setting configuration feature to one */
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08002334 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
Greg Kroah-Hartman907aff42009-01-29 17:17:48 -08002335 (__u8) 0x03, 0x00, 0x01, 0x00, NULL, 0x00, 5 * HZ);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08002336 return 0;
2337}
2338
Alan Sternf9c99bb2009-06-02 11:53:55 -04002339static void ATEN2011_release(struct usb_serial *serial)
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08002340{
2341 int i;
2342 struct ATENINTL_port *ATEN2011_port;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08002343
Greg Kroah-Hartman9e3697a2009-02-03 16:06:19 -08002344 /* check for the ports to be closed,close the ports and disconnect */
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08002345
2346 /* free private structure allocated for serial port *
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08002347 * stop reads and writes on all ports */
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08002348
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08002349 for (i = 0; i < serial->num_ports; ++i) {
Greg Kroah-Hartmanc3e06f22009-01-30 15:37:00 -08002350 ATEN2011_port = usb_get_serial_port_data(serial->port[i]);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08002351 kfree(ATEN2011_port->ctrl_buf);
2352 usb_kill_urb(ATEN2011_port->control_urb);
2353 kfree(ATEN2011_port);
Greg Kroah-Hartmanc3e06f22009-01-30 15:37:00 -08002354 usb_set_serial_port_data(serial->port[i], NULL);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08002355 }
2356
2357 /* free private structure allocated for serial device */
2358
Greg Kroah-Hartmanc3e06f22009-01-30 15:37:00 -08002359 kfree(usb_get_serial_data(serial));
2360 usb_set_serial_data(serial, NULL);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08002361}
2362
Greg Kroah-Hartman085c0902009-01-30 15:30:48 -08002363static struct usb_serial_driver aten_serial_driver = {
Greg Kroah-Hartmana7fefd12009-01-29 17:10:43 -08002364 .driver = {
2365 .owner = THIS_MODULE,
2366 .name = "aten2011",
2367 },
2368 .description = DRIVER_DESC,
2369 .id_table = id_table,
2370 .open = ATEN2011_open,
2371 .close = ATEN2011_close,
2372 .write = ATEN2011_write,
2373 .write_room = ATEN2011_write_room,
2374 .chars_in_buffer = ATEN2011_chars_in_buffer,
2375 .throttle = ATEN2011_throttle,
2376 .unthrottle = ATEN2011_unthrottle,
2377 .calc_num_ports = ATEN2011_calc_num_ports,
2378
2379 .ioctl = ATEN2011_ioctl,
2380 .set_termios = ATEN2011_set_termios,
2381 .break_ctl = ATEN2011_break,
2382 .tiocmget = ATEN2011_tiocmget,
2383 .tiocmset = ATEN2011_tiocmset,
2384 .attach = ATEN2011_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -04002385 .release = ATEN2011_release,
Greg Kroah-Hartmana7fefd12009-01-29 17:10:43 -08002386 .read_bulk_callback = ATEN2011_bulk_in_callback,
2387 .read_int_callback = ATEN2011_interrupt_callback,
2388};
2389
2390static struct usb_driver aten_driver = {
2391 .name = "aten2011",
2392 .probe = usb_serial_probe,
2393 .disconnect = usb_serial_disconnect,
2394 .id_table = id_table,
2395};
2396
Greg Kroah-Hartman085c0902009-01-30 15:30:48 -08002397static int __init aten_init(void)
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08002398{
2399 int retval;
2400
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08002401 /* Register with the usb serial */
Greg Kroah-Hartman085c0902009-01-30 15:30:48 -08002402 retval = usb_serial_register(&aten_serial_driver);
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08002403 if (retval)
Greg Kroah-Hartman085c0902009-01-30 15:30:48 -08002404 return retval;
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08002405
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08002406 printk(KERN_INFO KBUILD_MODNAME ":"
2407 DRIVER_DESC " " DRIVER_VERSION "\n");
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08002408
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08002409 /* Register with the usb */
Greg Kroah-Hartmana7fefd12009-01-29 17:10:43 -08002410 retval = usb_register(&aten_driver);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08002411 if (retval)
Greg Kroah-Hartman085c0902009-01-30 15:30:48 -08002412 usb_serial_deregister(&aten_serial_driver);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08002413
2414 return retval;
2415}
2416
Greg Kroah-Hartman085c0902009-01-30 15:30:48 -08002417static void __exit aten_exit(void)
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08002418{
Greg Kroah-Hartmana7fefd12009-01-29 17:10:43 -08002419 usb_deregister(&aten_driver);
Greg Kroah-Hartman085c0902009-01-30 15:30:48 -08002420 usb_serial_deregister(&aten_serial_driver);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08002421}
2422
Greg Kroah-Hartman085c0902009-01-30 15:30:48 -08002423module_init(aten_init);
2424module_exit(aten_exit);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08002425
2426/* Module information */
Greg Kroah-Hartmand93f87c2009-01-29 12:54:01 -08002427MODULE_DESCRIPTION(DRIVER_DESC);
Greg Kroah-Hartmane6d69d92009-01-27 23:28:27 -08002428MODULE_LICENSE("GPL");
2429
2430MODULE_PARM_DESC(debug, "Debug enabled or not");