blob: 2c20e88a91b3df84b8a19b7d2e5d46caf2f329f9 [file] [log] [blame]
Paul B Schroeder3f542972006-08-31 19:41:47 -05001/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15 *
16 * Clean ups from Moschip version and a few ioctl implementations by:
17 * Paul B Schroeder <pschroeder "at" uplogix "dot" com>
18 *
19 * Originally based on drivers/usb/serial/io_edgeport.c which is:
20 * Copyright (C) 2000 Inside Out Networks, All rights reserved.
21 * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
22 *
23 */
24
25#include <linux/kernel.h>
26#include <linux/errno.h>
27#include <linux/init.h>
28#include <linux/slab.h>
29#include <linux/tty.h>
30#include <linux/tty_driver.h>
31#include <linux/tty_flip.h>
32#include <linux/module.h>
33#include <linux/serial.h>
34#include <linux/usb.h>
35#include <linux/usb/serial.h>
Alan Cox880af9d2008-07-22 11:16:12 +010036#include <linux/uaccess.h>
Paul B Schroeder3f542972006-08-31 19:41:47 -050037
38/*
39 * Version Information
40 */
41#define DRIVER_VERSION "1.3.1"
42#define DRIVER_DESC "Moschip 7840/7820 USB Serial Driver"
43
44/*
45 * 16C50 UART register defines
46 */
47
48#define LCR_BITS_5 0x00 /* 5 bits/char */
49#define LCR_BITS_6 0x01 /* 6 bits/char */
50#define LCR_BITS_7 0x02 /* 7 bits/char */
51#define LCR_BITS_8 0x03 /* 8 bits/char */
52#define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
53
54#define LCR_STOP_1 0x00 /* 1 stop bit */
55#define LCR_STOP_1_5 0x04 /* 1.5 stop bits (if 5 bits/char) */
56#define LCR_STOP_2 0x04 /* 2 stop bits (if 6-8 bits/char) */
57#define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
58
59#define LCR_PAR_NONE 0x00 /* No parity */
60#define LCR_PAR_ODD 0x08 /* Odd parity */
61#define LCR_PAR_EVEN 0x18 /* Even parity */
62#define LCR_PAR_MARK 0x28 /* Force parity bit to 1 */
63#define LCR_PAR_SPACE 0x38 /* Force parity bit to 0 */
64#define LCR_PAR_MASK 0x38 /* Mask for parity field */
65
66#define LCR_SET_BREAK 0x40 /* Set Break condition */
67#define LCR_DL_ENABLE 0x80 /* Enable access to divisor latch */
68
69#define MCR_DTR 0x01 /* Assert DTR */
70#define MCR_RTS 0x02 /* Assert RTS */
71#define MCR_OUT1 0x04 /* Loopback only: Sets state of RI */
72#define MCR_MASTER_IE 0x08 /* Enable interrupt outputs */
73#define MCR_LOOPBACK 0x10 /* Set internal (digital) loopback mode */
74#define MCR_XON_ANY 0x20 /* Enable any char to exit XOFF mode */
75
76#define MOS7840_MSR_CTS 0x10 /* Current state of CTS */
77#define MOS7840_MSR_DSR 0x20 /* Current state of DSR */
78#define MOS7840_MSR_RI 0x40 /* Current state of RI */
79#define MOS7840_MSR_CD 0x80 /* Current state of CD */
80
81/*
82 * Defines used for sending commands to port
83 */
84
Alan Cox880af9d2008-07-22 11:16:12 +010085#define WAIT_FOR_EVER (HZ * 0) /* timeout urb is wait for ever */
86#define MOS_WDR_TIMEOUT (HZ * 5) /* default urb timeout */
Paul B Schroeder3f542972006-08-31 19:41:47 -050087
88#define MOS_PORT1 0x0200
89#define MOS_PORT2 0x0300
90#define MOS_VENREG 0x0000
91#define MOS_MAX_PORT 0x02
92#define MOS_WRITE 0x0E
93#define MOS_READ 0x0D
94
95/* Requests */
96#define MCS_RD_RTYPE 0xC0
97#define MCS_WR_RTYPE 0x40
98#define MCS_RDREQ 0x0D
99#define MCS_WRREQ 0x0E
100#define MCS_CTRL_TIMEOUT 500
101#define VENDOR_READ_LENGTH (0x01)
102
103#define MAX_NAME_LEN 64
104
Alan Cox880af9d2008-07-22 11:16:12 +0100105#define ZLP_REG1 0x3A /* Zero_Flag_Reg1 58 */
106#define ZLP_REG5 0x3E /* Zero_Flag_Reg5 62 */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500107
108/* For higher baud Rates use TIOCEXBAUD */
109#define TIOCEXBAUD 0x5462
110
111/* vendor id and device id defines */
112
David Ludlow11e1abb2008-02-25 17:30:52 -0500113/* The native mos7840/7820 component */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500114#define USB_VENDOR_ID_MOSCHIP 0x9710
115#define MOSCHIP_DEVICE_ID_7840 0x7840
116#define MOSCHIP_DEVICE_ID_7820 0x7820
David Ludlow11e1abb2008-02-25 17:30:52 -0500117/* The native component can have its vendor/device id's overridden
118 * in vendor-specific implementations. Such devices can be handled
119 * by making a change here, in moschip_port_id_table, and in
120 * moschip_id_table_combined
121 */
122#define USB_VENDOR_ID_BANDB 0x0856
123#define BANDB_DEVICE_ID_USOPTL4_4 0xAC44
124#define BANDB_DEVICE_ID_USOPTL4_2 0xAC42
Paul B Schroeder3f542972006-08-31 19:41:47 -0500125
David Ludlow11e1abb2008-02-25 17:30:52 -0500126/* Interrupt Routine Defines */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500127
128#define SERIAL_IIR_RLS 0x06
129#define SERIAL_IIR_MS 0x00
130
131/*
132 * Emulation of the bit mask on the LINE STATUS REGISTER.
133 */
134#define SERIAL_LSR_DR 0x0001
135#define SERIAL_LSR_OE 0x0002
136#define SERIAL_LSR_PE 0x0004
137#define SERIAL_LSR_FE 0x0008
138#define SERIAL_LSR_BI 0x0010
139
140#define MOS_MSR_DELTA_CTS 0x10
141#define MOS_MSR_DELTA_DSR 0x20
142#define MOS_MSR_DELTA_RI 0x40
143#define MOS_MSR_DELTA_CD 0x80
144
Alan Cox880af9d2008-07-22 11:16:12 +0100145/* Serial Port register Address */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500146#define INTERRUPT_ENABLE_REGISTER ((__u16)(0x01))
147#define FIFO_CONTROL_REGISTER ((__u16)(0x02))
148#define LINE_CONTROL_REGISTER ((__u16)(0x03))
149#define MODEM_CONTROL_REGISTER ((__u16)(0x04))
150#define LINE_STATUS_REGISTER ((__u16)(0x05))
151#define MODEM_STATUS_REGISTER ((__u16)(0x06))
152#define SCRATCH_PAD_REGISTER ((__u16)(0x07))
153#define DIVISOR_LATCH_LSB ((__u16)(0x00))
154#define DIVISOR_LATCH_MSB ((__u16)(0x01))
155
156#define CLK_MULTI_REGISTER ((__u16)(0x02))
157#define CLK_START_VALUE_REGISTER ((__u16)(0x03))
158
159#define SERIAL_LCR_DLAB ((__u16)(0x0080))
160
161/*
162 * URB POOL related defines
163 */
164#define NUM_URBS 16 /* URB Count */
165#define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
166
167
168static struct usb_device_id moschip_port_id_table[] = {
169 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7840)},
170 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7820)},
David Ludlow11e1abb2008-02-25 17:30:52 -0500171 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4)},
172 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2)},
Paul B Schroeder3f542972006-08-31 19:41:47 -0500173 {} /* terminating entry */
174};
175
176static __devinitdata struct usb_device_id moschip_id_table_combined[] = {
177 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7840)},
178 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7820)},
David Ludlow11e1abb2008-02-25 17:30:52 -0500179 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4)},
180 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2)},
Paul B Schroeder3f542972006-08-31 19:41:47 -0500181 {} /* terminating entry */
182};
183
184MODULE_DEVICE_TABLE(usb, moschip_id_table_combined);
185
186/* This structure holds all of the local port information */
187
188struct moschip_port {
189 int port_num; /*Actual port number in the device(1,2,etc) */
190 struct urb *write_urb; /* write URB for this port */
191 struct urb *read_urb; /* read URB for this port */
Oliver Neukum0de9a702007-03-16 20:28:28 +0100192 struct urb *int_urb;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500193 __u8 shadowLCR; /* last LCR value received */
194 __u8 shadowMCR; /* last MCR value received */
195 char open;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100196 char open_ports;
197 char zombie;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500198 wait_queue_head_t wait_chase; /* for handling sleeping while waiting for chase to finish */
199 wait_queue_head_t delta_msr_wait; /* for handling sleeping while waiting for msr change to happen */
200 int delta_msr_cond;
201 struct async_icount icount;
202 struct usb_serial_port *port; /* loop back to the owner of this object */
203
Alan Cox880af9d2008-07-22 11:16:12 +0100204 /* Offsets */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500205 __u8 SpRegOffset;
206 __u8 ControlRegOffset;
207 __u8 DcrRegOffset;
Alan Cox880af9d2008-07-22 11:16:12 +0100208 /* for processing control URBS in interrupt context */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500209 struct urb *control_urb;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100210 struct usb_ctrlrequest *dr;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500211 char *ctrl_buf;
212 int MsrLsr;
213
Oliver Neukum0de9a702007-03-16 20:28:28 +0100214 spinlock_t pool_lock;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500215 struct urb *write_urb_pool[NUM_URBS];
Oliver Neukum0de9a702007-03-16 20:28:28 +0100216 char busy[NUM_URBS];
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800217 bool read_urb_busy;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500218};
219
220
221static int debug;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500222
223/*
224 * mos7840_set_reg_sync
225 * To set the Control register by calling usb_fill_control_urb function
226 * by passing usb_sndctrlpipe function as parameter.
227 */
228
229static int mos7840_set_reg_sync(struct usb_serial_port *port, __u16 reg,
230 __u16 val)
231{
232 struct usb_device *dev = port->serial->dev;
233 val = val & 0x00ff;
234 dbg("mos7840_set_reg_sync offset is %x, value %x\n", reg, val);
235
236 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
237 MCS_WR_RTYPE, val, reg, NULL, 0,
238 MOS_WDR_TIMEOUT);
239}
240
241/*
242 * mos7840_get_reg_sync
243 * To set the Uart register by calling usb_fill_control_urb function by
244 * passing usb_rcvctrlpipe function as parameter.
245 */
246
247static int mos7840_get_reg_sync(struct usb_serial_port *port, __u16 reg,
Alan Cox880af9d2008-07-22 11:16:12 +0100248 __u16 *val)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500249{
250 struct usb_device *dev = port->serial->dev;
251 int ret = 0;
252
253 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
254 MCS_RD_RTYPE, 0, reg, val, VENDOR_READ_LENGTH,
255 MOS_WDR_TIMEOUT);
256 dbg("mos7840_get_reg_sync offset is %x, return val %x\n", reg, *val);
257 *val = (*val) & 0x00ff;
258 return ret;
259}
260
261/*
262 * mos7840_set_uart_reg
263 * To set the Uart register by calling usb_fill_control_urb function by
264 * passing usb_sndctrlpipe function as parameter.
265 */
266
267static int mos7840_set_uart_reg(struct usb_serial_port *port, __u16 reg,
268 __u16 val)
269{
270
271 struct usb_device *dev = port->serial->dev;
272 val = val & 0x00ff;
Alan Cox880af9d2008-07-22 11:16:12 +0100273 /* For the UART control registers, the application number need
274 to be Or'ed */
Oliver Neukum0de9a702007-03-16 20:28:28 +0100275 if (port->serial->num_ports == 4) {
Alan Cox880af9d2008-07-22 11:16:12 +0100276 val |= (((__u16) port->number -
277 (__u16) (port->serial->minor)) + 1) << 8;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500278 dbg("mos7840_set_uart_reg application number is %x\n", val);
279 } else {
280 if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
Alan Cox880af9d2008-07-22 11:16:12 +0100281 val |= (((__u16) port->number -
Paul B Schroeder3f542972006-08-31 19:41:47 -0500282 (__u16) (port->serial->minor)) + 1) << 8;
283 dbg("mos7840_set_uart_reg application number is %x\n",
284 val);
285 } else {
286 val |=
287 (((__u16) port->number -
288 (__u16) (port->serial->minor)) + 2) << 8;
289 dbg("mos7840_set_uart_reg application number is %x\n",
290 val);
291 }
292 }
293 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
294 MCS_WR_RTYPE, val, reg, NULL, 0,
295 MOS_WDR_TIMEOUT);
296
297}
298
299/*
300 * mos7840_get_uart_reg
301 * To set the Control register by calling usb_fill_control_urb function
302 * by passing usb_rcvctrlpipe function as parameter.
303 */
304static int mos7840_get_uart_reg(struct usb_serial_port *port, __u16 reg,
Alan Cox880af9d2008-07-22 11:16:12 +0100305 __u16 *val)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500306{
307 struct usb_device *dev = port->serial->dev;
308 int ret = 0;
309 __u16 Wval;
310
Alan Cox880af9d2008-07-22 11:16:12 +0100311 /* dbg("application number is %4x \n",
312 (((__u16)port->number - (__u16)(port->serial->minor))+1)<<8); */
313 /* Wval is same as application number */
Oliver Neukum0de9a702007-03-16 20:28:28 +0100314 if (port->serial->num_ports == 4) {
Paul B Schroeder3f542972006-08-31 19:41:47 -0500315 Wval =
316 (((__u16) port->number - (__u16) (port->serial->minor)) +
317 1) << 8;
318 dbg("mos7840_get_uart_reg application number is %x\n", Wval);
319 } else {
320 if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
Alan Cox880af9d2008-07-22 11:16:12 +0100321 Wval = (((__u16) port->number -
Paul B Schroeder3f542972006-08-31 19:41:47 -0500322 (__u16) (port->serial->minor)) + 1) << 8;
323 dbg("mos7840_get_uart_reg application number is %x\n",
324 Wval);
325 } else {
Alan Cox880af9d2008-07-22 11:16:12 +0100326 Wval = (((__u16) port->number -
Paul B Schroeder3f542972006-08-31 19:41:47 -0500327 (__u16) (port->serial->minor)) + 2) << 8;
328 dbg("mos7840_get_uart_reg application number is %x\n",
329 Wval);
330 }
331 }
332 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
333 MCS_RD_RTYPE, Wval, reg, val, VENDOR_READ_LENGTH,
334 MOS_WDR_TIMEOUT);
335 *val = (*val) & 0x00ff;
336 return ret;
337}
338
339static void mos7840_dump_serial_port(struct moschip_port *mos7840_port)
340{
341
342 dbg("***************************************\n");
343 dbg("SpRegOffset is %2x\n", mos7840_port->SpRegOffset);
344 dbg("ControlRegOffset is %2x \n", mos7840_port->ControlRegOffset);
345 dbg("DCRRegOffset is %2x \n", mos7840_port->DcrRegOffset);
346 dbg("***************************************\n");
347
348}
349
350/************************************************************************/
351/************************************************************************/
352/* I N T E R F A C E F U N C T I O N S */
353/* I N T E R F A C E F U N C T I O N S */
354/************************************************************************/
355/************************************************************************/
356
357static inline void mos7840_set_port_private(struct usb_serial_port *port,
358 struct moschip_port *data)
359{
360 usb_set_serial_port_data(port, (void *)data);
361}
362
363static inline struct moschip_port *mos7840_get_port_private(struct
364 usb_serial_port
365 *port)
366{
367 return (struct moschip_port *)usb_get_serial_port_data(port);
368}
369
Oliver Neukum0de9a702007-03-16 20:28:28 +0100370static void mos7840_handle_new_msr(struct moschip_port *port, __u8 new_msr)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500371{
372 struct moschip_port *mos7840_port;
373 struct async_icount *icount;
374 mos7840_port = port;
375 icount = &mos7840_port->icount;
376 if (new_msr &
377 (MOS_MSR_DELTA_CTS | MOS_MSR_DELTA_DSR | MOS_MSR_DELTA_RI |
378 MOS_MSR_DELTA_CD)) {
379 icount = &mos7840_port->icount;
380
381 /* update input line counters */
382 if (new_msr & MOS_MSR_DELTA_CTS) {
383 icount->cts++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100384 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500385 }
386 if (new_msr & MOS_MSR_DELTA_DSR) {
387 icount->dsr++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100388 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500389 }
390 if (new_msr & MOS_MSR_DELTA_CD) {
391 icount->dcd++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100392 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500393 }
394 if (new_msr & MOS_MSR_DELTA_RI) {
395 icount->rng++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100396 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500397 }
398 }
Paul B Schroeder3f542972006-08-31 19:41:47 -0500399}
400
Oliver Neukum0de9a702007-03-16 20:28:28 +0100401static void mos7840_handle_new_lsr(struct moschip_port *port, __u8 new_lsr)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500402{
403 struct async_icount *icount;
404
Harvey Harrison441b62c2008-03-03 16:08:34 -0800405 dbg("%s - %02x", __func__, new_lsr);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500406
407 if (new_lsr & SERIAL_LSR_BI) {
Alan Cox880af9d2008-07-22 11:16:12 +0100408 /*
409 * Parity and Framing errors only count if they
410 * occur exclusive of a break being
411 * received.
412 */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500413 new_lsr &= (__u8) (SERIAL_LSR_OE | SERIAL_LSR_BI);
414 }
415
416 /* update input line counters */
417 icount = &port->icount;
418 if (new_lsr & SERIAL_LSR_BI) {
419 icount->brk++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100420 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500421 }
422 if (new_lsr & SERIAL_LSR_OE) {
423 icount->overrun++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100424 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500425 }
426 if (new_lsr & SERIAL_LSR_PE) {
427 icount->parity++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100428 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500429 }
430 if (new_lsr & SERIAL_LSR_FE) {
431 icount->frame++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100432 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500433 }
Paul B Schroeder3f542972006-08-31 19:41:47 -0500434}
435
436/************************************************************************/
437/************************************************************************/
438/* U S B C A L L B A C K F U N C T I O N S */
439/* U S B C A L L B A C K F U N C T I O N S */
440/************************************************************************/
441/************************************************************************/
442
David Howells7d12e782006-10-05 14:55:46 +0100443static void mos7840_control_callback(struct urb *urb)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500444{
445 unsigned char *data;
446 struct moschip_port *mos7840_port;
447 __u8 regval = 0x0;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100448 int result = 0;
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700449 int status = urb->status;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500450
Ming Leicdc97792008-02-24 18:41:47 +0800451 mos7840_port = urb->context;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100452
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700453 switch (status) {
Paul B Schroeder3f542972006-08-31 19:41:47 -0500454 case 0:
455 /* success */
456 break;
457 case -ECONNRESET:
458 case -ENOENT:
459 case -ESHUTDOWN:
460 /* this urb is terminated, clean up */
Harvey Harrison441b62c2008-03-03 16:08:34 -0800461 dbg("%s - urb shutting down with status: %d", __func__,
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700462 status);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500463 return;
464 default:
Harvey Harrison441b62c2008-03-03 16:08:34 -0800465 dbg("%s - nonzero urb status received: %d", __func__,
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700466 status);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500467 goto exit;
468 }
469
Harvey Harrison441b62c2008-03-03 16:08:34 -0800470 dbg("%s urb buffer size is %d\n", __func__, urb->actual_length);
471 dbg("%s mos7840_port->MsrLsr is %d port %d\n", __func__,
Paul B Schroeder3f542972006-08-31 19:41:47 -0500472 mos7840_port->MsrLsr, mos7840_port->port_num);
473 data = urb->transfer_buffer;
474 regval = (__u8) data[0];
Harvey Harrison441b62c2008-03-03 16:08:34 -0800475 dbg("%s data is %x\n", __func__, regval);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500476 if (mos7840_port->MsrLsr == 0)
477 mos7840_handle_new_msr(mos7840_port, regval);
478 else if (mos7840_port->MsrLsr == 1)
479 mos7840_handle_new_lsr(mos7840_port, regval);
480
Oliver Neukum0de9a702007-03-16 20:28:28 +0100481exit:
482 spin_lock(&mos7840_port->pool_lock);
483 if (!mos7840_port->zombie)
484 result = usb_submit_urb(mos7840_port->int_urb, GFP_ATOMIC);
485 spin_unlock(&mos7840_port->pool_lock);
486 if (result) {
487 dev_err(&urb->dev->dev,
488 "%s - Error %d submitting interrupt urb\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800489 __func__, result);
Oliver Neukum0de9a702007-03-16 20:28:28 +0100490 }
Paul B Schroeder3f542972006-08-31 19:41:47 -0500491}
492
493static int mos7840_get_reg(struct moschip_port *mcs, __u16 Wval, __u16 reg,
Alan Cox880af9d2008-07-22 11:16:12 +0100494 __u16 *val)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500495{
496 struct usb_device *dev = mcs->port->serial->dev;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100497 struct usb_ctrlrequest *dr = mcs->dr;
498 unsigned char *buffer = mcs->ctrl_buf;
499 int ret;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500500
Paul B Schroeder3f542972006-08-31 19:41:47 -0500501 dr->bRequestType = MCS_RD_RTYPE;
502 dr->bRequest = MCS_RDREQ;
Alan Cox880af9d2008-07-22 11:16:12 +0100503 dr->wValue = cpu_to_le16(Wval); /* 0 */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500504 dr->wIndex = cpu_to_le16(reg);
505 dr->wLength = cpu_to_le16(2);
506
507 usb_fill_control_urb(mcs->control_urb, dev, usb_rcvctrlpipe(dev, 0),
508 (unsigned char *)dr, buffer, 2,
509 mos7840_control_callback, mcs);
510 mcs->control_urb->transfer_buffer_length = 2;
511 ret = usb_submit_urb(mcs->control_urb, GFP_ATOMIC);
512 return ret;
513}
514
515/*****************************************************************************
516 * mos7840_interrupt_callback
517 * this is the callback function for when we have received data on the
518 * interrupt endpoint.
519 *****************************************************************************/
520
David Howells7d12e782006-10-05 14:55:46 +0100521static void mos7840_interrupt_callback(struct urb *urb)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500522{
523 int result;
524 int length;
525 struct moschip_port *mos7840_port;
526 struct usb_serial *serial;
527 __u16 Data;
528 unsigned char *data;
529 __u8 sp[5], st;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100530 int i, rv = 0;
531 __u16 wval, wreg = 0;
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700532 int status = urb->status;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500533
534 dbg("%s", " : Entering\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500535
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700536 switch (status) {
Paul B Schroeder3f542972006-08-31 19:41:47 -0500537 case 0:
538 /* success */
539 break;
540 case -ECONNRESET:
541 case -ENOENT:
542 case -ESHUTDOWN:
543 /* this urb is terminated, clean up */
Harvey Harrison441b62c2008-03-03 16:08:34 -0800544 dbg("%s - urb shutting down with status: %d", __func__,
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700545 status);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500546 return;
547 default:
Harvey Harrison441b62c2008-03-03 16:08:34 -0800548 dbg("%s - nonzero urb status received: %d", __func__,
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700549 status);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500550 goto exit;
551 }
552
553 length = urb->actual_length;
554 data = urb->transfer_buffer;
555
Ming Leicdc97792008-02-24 18:41:47 +0800556 serial = urb->context;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500557
558 /* Moschip get 5 bytes
559 * Byte 1 IIR Port 1 (port.number is 0)
560 * Byte 2 IIR Port 2 (port.number is 1)
561 * Byte 3 IIR Port 3 (port.number is 2)
562 * Byte 4 IIR Port 4 (port.number is 3)
563 * Byte 5 FIFO status for both */
564
565 if (length && length > 5) {
566 dbg("%s \n", "Wrong data !!!");
567 return;
568 }
569
570 sp[0] = (__u8) data[0];
571 sp[1] = (__u8) data[1];
572 sp[2] = (__u8) data[2];
573 sp[3] = (__u8) data[3];
574 st = (__u8) data[4];
575
576 for (i = 0; i < serial->num_ports; i++) {
577 mos7840_port = mos7840_get_port_private(serial->port[i]);
578 wval =
579 (((__u16) serial->port[i]->number -
580 (__u16) (serial->minor)) + 1) << 8;
581 if (mos7840_port->open) {
582 if (sp[i] & 0x01) {
583 dbg("SP%d No Interrupt !!!\n", i);
584 } else {
585 switch (sp[i] & 0x0f) {
586 case SERIAL_IIR_RLS:
587 dbg("Serial Port %d: Receiver status error or ", i);
588 dbg("address bit detected in 9-bit mode\n");
589 mos7840_port->MsrLsr = 1;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100590 wreg = LINE_STATUS_REGISTER;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500591 break;
592 case SERIAL_IIR_MS:
593 dbg("Serial Port %d: Modem status change\n", i);
594 mos7840_port->MsrLsr = 0;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100595 wreg = MODEM_STATUS_REGISTER;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500596 break;
597 }
Oliver Neukum0de9a702007-03-16 20:28:28 +0100598 spin_lock(&mos7840_port->pool_lock);
599 if (!mos7840_port->zombie) {
600 rv = mos7840_get_reg(mos7840_port, wval, wreg, &Data);
601 } else {
602 spin_unlock(&mos7840_port->pool_lock);
603 return;
604 }
605 spin_unlock(&mos7840_port->pool_lock);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500606 }
607 }
608 }
Alan Cox880af9d2008-07-22 11:16:12 +0100609 if (!(rv < 0))
610 /* the completion handler for the control urb will resubmit */
Oliver Neukum0de9a702007-03-16 20:28:28 +0100611 return;
612exit:
Paul B Schroeder3f542972006-08-31 19:41:47 -0500613 result = usb_submit_urb(urb, GFP_ATOMIC);
614 if (result) {
615 dev_err(&urb->dev->dev,
616 "%s - Error %d submitting interrupt urb\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800617 __func__, result);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500618 }
Paul B Schroeder3f542972006-08-31 19:41:47 -0500619}
620
621static int mos7840_port_paranoia_check(struct usb_serial_port *port,
622 const char *function)
623{
624 if (!port) {
625 dbg("%s - port == NULL", function);
626 return -1;
627 }
628 if (!port->serial) {
629 dbg("%s - port->serial == NULL", function);
630 return -1;
631 }
632
633 return 0;
634}
635
636/* Inline functions to check the sanity of a pointer that is passed to us */
637static int mos7840_serial_paranoia_check(struct usb_serial *serial,
638 const char *function)
639{
640 if (!serial) {
641 dbg("%s - serial == NULL", function);
642 return -1;
643 }
644 if (!serial->type) {
645 dbg("%s - serial->type == NULL!", function);
646 return -1;
647 }
648
649 return 0;
650}
651
652static struct usb_serial *mos7840_get_usb_serial(struct usb_serial_port *port,
653 const char *function)
654{
655 /* if no port was specified, or it fails a paranoia check */
656 if (!port ||
657 mos7840_port_paranoia_check(port, function) ||
658 mos7840_serial_paranoia_check(port->serial, function)) {
Alan Cox880af9d2008-07-22 11:16:12 +0100659 /* then say that we don't have a valid usb_serial thing,
660 * which will end up genrating -ENODEV return values */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500661 return NULL;
662 }
663
664 return port->serial;
665}
666
667/*****************************************************************************
668 * mos7840_bulk_in_callback
669 * this is the callback function for when we have received data on the
670 * bulk in endpoint.
671 *****************************************************************************/
672
David Howells7d12e782006-10-05 14:55:46 +0100673static void mos7840_bulk_in_callback(struct urb *urb)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500674{
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700675 int retval;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500676 unsigned char *data;
677 struct usb_serial *serial;
678 struct usb_serial_port *port;
679 struct moschip_port *mos7840_port;
680 struct tty_struct *tty;
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700681 int status = urb->status;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500682
Ming Leicdc97792008-02-24 18:41:47 +0800683 mos7840_port = urb->context;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500684 if (!mos7840_port) {
685 dbg("%s", "NULL mos7840_port pointer \n");
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800686 mos7840_port->read_urb_busy = false;
687 return;
688 }
689
690 if (status) {
691 dbg("nonzero read bulk status received: %d", status);
692 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500693 return;
694 }
695
696 port = (struct usb_serial_port *)mos7840_port->port;
Harvey Harrison441b62c2008-03-03 16:08:34 -0800697 if (mos7840_port_paranoia_check(port, __func__)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -0500698 dbg("%s", "Port Paranoia failed \n");
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800699 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500700 return;
701 }
702
Harvey Harrison441b62c2008-03-03 16:08:34 -0800703 serial = mos7840_get_usb_serial(port, __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500704 if (!serial) {
705 dbg("%s\n", "Bad serial pointer ");
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800706 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500707 return;
708 }
709
710 dbg("%s\n", "Entering... \n");
711
712 data = urb->transfer_buffer;
713
714 dbg("%s", "Entering ........... \n");
715
716 if (urb->actual_length) {
Alan Cox4a90f092008-10-13 10:39:46 +0100717 tty = tty_port_tty_get(&mos7840_port->port->port);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500718 if (tty) {
719 tty_buffer_request_room(tty, urb->actual_length);
720 tty_insert_flip_string(tty, data, urb->actual_length);
721 dbg(" %s \n", data);
722 tty_flip_buffer_push(tty);
Alan Cox4a90f092008-10-13 10:39:46 +0100723 tty_kref_put(tty);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500724 }
725 mos7840_port->icount.rx += urb->actual_length;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100726 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500727 dbg("mos7840_port->icount.rx is %d:\n",
728 mos7840_port->icount.rx);
729 }
730
731 if (!mos7840_port->read_urb) {
732 dbg("%s", "URB KILLED !!!\n");
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800733 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500734 return;
735 }
736
Paul B Schroeder3f542972006-08-31 19:41:47 -0500737
Oliver Neukum0de9a702007-03-16 20:28:28 +0100738 mos7840_port->read_urb->dev = serial->dev;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500739
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800740 mos7840_port->read_urb_busy = true;
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700741 retval = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
Oliver Neukum0de9a702007-03-16 20:28:28 +0100742
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700743 if (retval) {
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800744 dbg("usb_submit_urb(read bulk) failed, retval = %d", retval);
745 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500746 }
747}
748
749/*****************************************************************************
750 * mos7840_bulk_out_data_callback
Alan Cox880af9d2008-07-22 11:16:12 +0100751 * this is the callback function for when we have finished sending
752 * serial data on the bulk out endpoint.
Paul B Schroeder3f542972006-08-31 19:41:47 -0500753 *****************************************************************************/
754
David Howells7d12e782006-10-05 14:55:46 +0100755static void mos7840_bulk_out_data_callback(struct urb *urb)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500756{
757 struct moschip_port *mos7840_port;
758 struct tty_struct *tty;
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700759 int status = urb->status;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100760 int i;
761
Ming Leicdc97792008-02-24 18:41:47 +0800762 mos7840_port = urb->context;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100763 spin_lock(&mos7840_port->pool_lock);
764 for (i = 0; i < NUM_URBS; i++) {
765 if (urb == mos7840_port->write_urb_pool[i]) {
766 mos7840_port->busy[i] = 0;
767 break;
768 }
769 }
770 spin_unlock(&mos7840_port->pool_lock);
771
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700772 if (status) {
773 dbg("nonzero write bulk status received:%d\n", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500774 return;
775 }
776
Harvey Harrison441b62c2008-03-03 16:08:34 -0800777 if (mos7840_port_paranoia_check(mos7840_port->port, __func__)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -0500778 dbg("%s", "Port Paranoia failed \n");
779 return;
780 }
781
782 dbg("%s \n", "Entering .........");
783
Alan Cox4a90f092008-10-13 10:39:46 +0100784 tty = tty_port_tty_get(&mos7840_port->port->port);
Jiri Slabyb963a842007-02-10 01:44:55 -0800785 if (tty && mos7840_port->open)
786 tty_wakeup(tty);
Alan Cox4a90f092008-10-13 10:39:46 +0100787 tty_kref_put(tty);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500788
789}
790
791/************************************************************************/
792/* D R I V E R T T Y I N T E R F A C E F U N C T I O N S */
793/************************************************************************/
794#ifdef MCSSerialProbe
795static int mos7840_serial_probe(struct usb_serial *serial,
796 const struct usb_device_id *id)
797{
798
799 /*need to implement the mode_reg reading and updating\
800 structures usb_serial_ device_type\
801 (i.e num_ports, num_bulkin,bulkout etc) */
802 /* Also we can update the changes attach */
803 return 1;
804}
805#endif
806
807/*****************************************************************************
808 * mos7840_open
809 * this function is called by the tty driver when a port is opened
810 * If successful, we return 0
811 * Otherwise we return a negative error number.
812 *****************************************************************************/
813
Alan Cox95da3102008-07-22 11:09:07 +0100814static int mos7840_open(struct tty_struct *tty,
815 struct usb_serial_port *port, struct file *filp)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500816{
817 int response;
818 int j;
819 struct usb_serial *serial;
820 struct urb *urb;
821 __u16 Data;
822 int status;
823 struct moschip_port *mos7840_port;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100824 struct moschip_port *port0;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500825
Harvey Harrison441b62c2008-03-03 16:08:34 -0800826 if (mos7840_port_paranoia_check(port, __func__)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -0500827 dbg("%s", "Port Paranoia failed \n");
828 return -ENODEV;
829 }
830
Paul B Schroeder3f542972006-08-31 19:41:47 -0500831 serial = port->serial;
832
Harvey Harrison441b62c2008-03-03 16:08:34 -0800833 if (mos7840_serial_paranoia_check(serial, __func__)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -0500834 dbg("%s", "Serial Paranoia failed \n");
835 return -ENODEV;
836 }
837
838 mos7840_port = mos7840_get_port_private(port);
Oliver Neukum0de9a702007-03-16 20:28:28 +0100839 port0 = mos7840_get_port_private(serial->port[0]);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500840
Oliver Neukum0de9a702007-03-16 20:28:28 +0100841 if (mos7840_port == NULL || port0 == NULL)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500842 return -ENODEV;
843
844 usb_clear_halt(serial->dev, port->write_urb->pipe);
845 usb_clear_halt(serial->dev, port->read_urb->pipe);
Oliver Neukum0de9a702007-03-16 20:28:28 +0100846 port0->open_ports++;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500847
848 /* Initialising the write urb pool */
849 for (j = 0; j < NUM_URBS; ++j) {
Oliver Neukum0de9a702007-03-16 20:28:28 +0100850 urb = usb_alloc_urb(0, GFP_KERNEL);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500851 mos7840_port->write_urb_pool[j] = urb;
852
853 if (urb == NULL) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700854 dev_err(&port->dev, "No more urbs???\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500855 continue;
856 }
857
Alan Cox880af9d2008-07-22 11:16:12 +0100858 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
859 GFP_KERNEL);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500860 if (!urb->transfer_buffer) {
Oliver Neukum0de9a702007-03-16 20:28:28 +0100861 usb_free_urb(urb);
862 mos7840_port->write_urb_pool[j] = NULL;
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700863 dev_err(&port->dev,
864 "%s-out of memory for urb buffers.\n",
865 __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500866 continue;
867 }
868 }
869
870/*****************************************************************************
871 * Initialize MCS7840 -- Write Init values to corresponding Registers
872 *
873 * Register Index
874 * 1 : IER
875 * 2 : FCR
876 * 3 : LCR
877 * 4 : MCR
878 *
879 * 0x08 : SP1/2 Control Reg
880 *****************************************************************************/
881
Alan Cox880af9d2008-07-22 11:16:12 +0100882 /* NEED to check the following Block */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500883
Paul B Schroeder3f542972006-08-31 19:41:47 -0500884 Data = 0x0;
885 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
886 if (status < 0) {
887 dbg("Reading Spreg failed\n");
888 return -1;
889 }
890 Data |= 0x80;
891 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
892 if (status < 0) {
893 dbg("writing Spreg failed\n");
894 return -1;
895 }
896
897 Data &= ~0x80;
898 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
899 if (status < 0) {
900 dbg("writing Spreg failed\n");
901 return -1;
902 }
Alan Cox880af9d2008-07-22 11:16:12 +0100903 /* End of block to be checked */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500904
Paul B Schroeder3f542972006-08-31 19:41:47 -0500905 Data = 0x0;
Alan Cox880af9d2008-07-22 11:16:12 +0100906 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
907 &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500908 if (status < 0) {
909 dbg("Reading Controlreg failed\n");
910 return -1;
911 }
Alan Cox880af9d2008-07-22 11:16:12 +0100912 Data |= 0x08; /* Driver done bit */
913 Data |= 0x20; /* rx_disable */
914 status = mos7840_set_reg_sync(port,
915 mos7840_port->ControlRegOffset, Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500916 if (status < 0) {
917 dbg("writing Controlreg failed\n");
918 return -1;
919 }
Alan Cox880af9d2008-07-22 11:16:12 +0100920 /* do register settings here */
921 /* Set all regs to the device default values. */
922 /***********************************
923 * First Disable all interrupts.
924 ***********************************/
Paul B Schroeder3f542972006-08-31 19:41:47 -0500925 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500926 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
927 if (status < 0) {
928 dbg("disableing interrupts failed\n");
929 return -1;
930 }
Alan Cox880af9d2008-07-22 11:16:12 +0100931 /* Set FIFO_CONTROL_REGISTER to the default value */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500932 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500933 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
934 if (status < 0) {
935 dbg("Writing FIFO_CONTROL_REGISTER failed\n");
936 return -1;
937 }
938
939 Data = 0xcf;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500940 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
941 if (status < 0) {
942 dbg("Writing FIFO_CONTROL_REGISTER failed\n");
943 return -1;
944 }
945
946 Data = 0x03;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500947 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
948 mos7840_port->shadowLCR = Data;
949
950 Data = 0x0b;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500951 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
952 mos7840_port->shadowMCR = Data;
953
954 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500955 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
956 mos7840_port->shadowLCR = Data;
957
Alan Cox880af9d2008-07-22 11:16:12 +0100958 Data |= SERIAL_LCR_DLAB; /* data latch enable in LCR 0x80 */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500959 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
960
961 Data = 0x0c;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500962 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
963
964 Data = 0x0;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500965 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
966
967 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500968 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
969
970 Data = Data & ~SERIAL_LCR_DLAB;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500971 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
972 mos7840_port->shadowLCR = Data;
973
Alan Cox880af9d2008-07-22 11:16:12 +0100974 /* clearing Bulkin and Bulkout Fifo */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500975 Data = 0x0;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500976 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
977
978 Data = Data | 0x0c;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500979 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
980
981 Data = Data & ~0x0c;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500982 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
Alan Cox880af9d2008-07-22 11:16:12 +0100983 /* Finally enable all interrupts */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500984 Data = 0x0c;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500985 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
986
Alan Cox880af9d2008-07-22 11:16:12 +0100987 /* clearing rx_disable */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500988 Data = 0x0;
Alan Cox880af9d2008-07-22 11:16:12 +0100989 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
990 &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500991 Data = Data & ~0x20;
Alan Cox880af9d2008-07-22 11:16:12 +0100992 status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
993 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500994
Alan Cox880af9d2008-07-22 11:16:12 +0100995 /* rx_negate */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500996 Data = 0x0;
Alan Cox880af9d2008-07-22 11:16:12 +0100997 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
998 &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500999 Data = Data | 0x10;
Alan Cox880af9d2008-07-22 11:16:12 +01001000 status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1001 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001002
1003 /* force low_latency on so that our tty_push actually forces *
1004 * the data through,otherwise it is scheduled, and with *
1005 * high data rates (like with OHCI) data can get lost. */
Alan Cox95da3102008-07-22 11:09:07 +01001006 if (tty)
1007 tty->low_latency = 1;
Alan Cox880af9d2008-07-22 11:16:12 +01001008
1009 /* Check to see if we've set up our endpoint info yet *
1010 * (can't set it up in mos7840_startup as the structures *
1011 * were not set up at that time.) */
Oliver Neukum0de9a702007-03-16 20:28:28 +01001012 if (port0->open_ports == 1) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001013 if (serial->port[0]->interrupt_in_buffer == NULL) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001014 /* set up interrupt urb */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001015 usb_fill_int_urb(serial->port[0]->interrupt_in_urb,
Alan Cox880af9d2008-07-22 11:16:12 +01001016 serial->dev,
1017 usb_rcvintpipe(serial->dev,
1018 serial->port[0]->interrupt_in_endpointAddress),
1019 serial->port[0]->interrupt_in_buffer,
1020 serial->port[0]->interrupt_in_urb->
1021 transfer_buffer_length,
1022 mos7840_interrupt_callback,
1023 serial,
1024 serial->port[0]->interrupt_in_urb->interval);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001025
1026 /* start interrupt read for mos7840 *
1027 * will continue as long as mos7840 is connected */
1028
1029 response =
1030 usb_submit_urb(serial->port[0]->interrupt_in_urb,
1031 GFP_KERNEL);
1032 if (response) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001033 dev_err(&port->dev, "%s - Error %d submitting "
1034 "interrupt urb\n", __func__, response);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001035 }
1036
1037 }
1038
1039 }
1040
1041 /* see if we've set up our endpoint info yet *
1042 * (can't set it up in mos7840_startup as the *
1043 * structures were not set up at that time.) */
1044
1045 dbg("port number is %d \n", port->number);
1046 dbg("serial number is %d \n", port->serial->minor);
1047 dbg("Bulkin endpoint is %d \n", port->bulk_in_endpointAddress);
1048 dbg("BulkOut endpoint is %d \n", port->bulk_out_endpointAddress);
1049 dbg("Interrupt endpoint is %d \n", port->interrupt_in_endpointAddress);
1050 dbg("port's number in the device is %d\n", mos7840_port->port_num);
1051 mos7840_port->read_urb = port->read_urb;
1052
1053 /* set up our bulk in urb */
1054
1055 usb_fill_bulk_urb(mos7840_port->read_urb,
1056 serial->dev,
1057 usb_rcvbulkpipe(serial->dev,
1058 port->bulk_in_endpointAddress),
1059 port->bulk_in_buffer,
1060 mos7840_port->read_urb->transfer_buffer_length,
1061 mos7840_bulk_in_callback, mos7840_port);
1062
1063 dbg("mos7840_open: bulkin endpoint is %d\n",
1064 port->bulk_in_endpointAddress);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08001065 mos7840_port->read_urb_busy = true;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001066 response = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
1067 if (response) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001068 dev_err(&port->dev, "%s - Error %d submitting control urb\n",
1069 __func__, response);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08001070 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001071 }
1072
1073 /* initialize our wait queues */
1074 init_waitqueue_head(&mos7840_port->wait_chase);
1075 init_waitqueue_head(&mos7840_port->delta_msr_wait);
1076
1077 /* initialize our icount structure */
1078 memset(&(mos7840_port->icount), 0x00, sizeof(mos7840_port->icount));
1079
1080 /* initialize our port settings */
Alan Cox880af9d2008-07-22 11:16:12 +01001081 /* Must set to enable ints! */
1082 mos7840_port->shadowMCR = MCR_MASTER_IE;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001083 /* send a open port command */
1084 mos7840_port->open = 1;
Alan Cox880af9d2008-07-22 11:16:12 +01001085 /* mos7840_change_port_settings(mos7840_port,old_termios); */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001086 mos7840_port->icount.tx = 0;
1087 mos7840_port->icount.rx = 0;
1088
Alan Cox880af9d2008-07-22 11:16:12 +01001089 dbg("\n\nusb_serial serial:%p mos7840_port:%p\n usb_serial_port port:%p\n\n",
1090 serial, mos7840_port, port);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001091
1092 return 0;
1093
1094}
1095
1096/*****************************************************************************
1097 * mos7840_chars_in_buffer
1098 * this function is called by the tty driver when it wants to know how many
1099 * bytes of data we currently have outstanding in the port (data that has
1100 * been written, but hasn't made it out the port yet)
1101 * If successful, we return the number of bytes left to be written in the
1102 * system,
Alan Cox95da3102008-07-22 11:09:07 +01001103 * Otherwise we return zero.
Paul B Schroeder3f542972006-08-31 19:41:47 -05001104 *****************************************************************************/
1105
Alan Cox95da3102008-07-22 11:09:07 +01001106static int mos7840_chars_in_buffer(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001107{
Alan Cox95da3102008-07-22 11:09:07 +01001108 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001109 int i;
1110 int chars = 0;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001111 unsigned long flags;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001112 struct moschip_port *mos7840_port;
1113
1114 dbg("%s \n", " mos7840_chars_in_buffer:entering ...........");
1115
Harvey Harrison441b62c2008-03-03 16:08:34 -08001116 if (mos7840_port_paranoia_check(port, __func__)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001117 dbg("%s", "Invalid port \n");
Alan Cox95da3102008-07-22 11:09:07 +01001118 return 0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001119 }
1120
1121 mos7840_port = mos7840_get_port_private(port);
1122 if (mos7840_port == NULL) {
1123 dbg("%s \n", "mos7840_break:leaving ...........");
Alan Cox95da3102008-07-22 11:09:07 +01001124 return 0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001125 }
1126
Alan Cox880af9d2008-07-22 11:16:12 +01001127 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
Alan Cox95da3102008-07-22 11:09:07 +01001128 for (i = 0; i < NUM_URBS; ++i)
1129 if (mos7840_port->busy[i])
Paul B Schroeder3f542972006-08-31 19:41:47 -05001130 chars += URB_TRANSFER_BUFFER_SIZE;
Alan Cox880af9d2008-07-22 11:16:12 +01001131 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
Harvey Harrison441b62c2008-03-03 16:08:34 -08001132 dbg("%s - returns %d", __func__, chars);
Oliver Neukum0de9a702007-03-16 20:28:28 +01001133 return chars;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001134
1135}
1136
1137/************************************************************************
1138 *
1139 * mos7840_block_until_tx_empty
1140 *
1141 * This function will block the close until one of the following:
1142 * 1. TX count are 0
1143 * 2. The mos7840 has stopped
Joe Perchesdc0d5c12007-12-17 11:40:18 -08001144 * 3. A timeout of 3 seconds without activity has expired
Paul B Schroeder3f542972006-08-31 19:41:47 -05001145 *
1146 ************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001147static void mos7840_block_until_tx_empty(struct tty_struct *tty,
1148 struct moschip_port *mos7840_port)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001149{
1150 int timeout = HZ / 10;
1151 int wait = 30;
1152 int count;
1153
1154 while (1) {
1155
Alan Cox95da3102008-07-22 11:09:07 +01001156 count = mos7840_chars_in_buffer(tty);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001157
1158 /* Check for Buffer status */
Alan Cox880af9d2008-07-22 11:16:12 +01001159 if (count <= 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001160 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001161
1162 /* Block the thread for a while */
1163 interruptible_sleep_on_timeout(&mos7840_port->wait_chase,
1164 timeout);
1165
1166 /* No activity.. count down section */
1167 wait--;
1168 if (wait == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001169 dbg("%s - TIMEOUT", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001170 return;
1171 } else {
Joe Perchesdc0d5c12007-12-17 11:40:18 -08001172 /* Reset timeout value back to seconds */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001173 wait = 30;
1174 }
1175 }
1176}
1177
1178/*****************************************************************************
1179 * mos7840_close
1180 * this function is called by the tty driver when a port is closed
1181 *****************************************************************************/
1182
Alan Cox95da3102008-07-22 11:09:07 +01001183static void mos7840_close(struct tty_struct *tty,
1184 struct usb_serial_port *port, struct file *filp)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001185{
1186 struct usb_serial *serial;
1187 struct moschip_port *mos7840_port;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001188 struct moschip_port *port0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001189 int j;
1190 __u16 Data;
1191
1192 dbg("%s\n", "mos7840_close:entering...");
1193
Harvey Harrison441b62c2008-03-03 16:08:34 -08001194 if (mos7840_port_paranoia_check(port, __func__)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001195 dbg("%s", "Port Paranoia failed \n");
1196 return;
1197 }
1198
Harvey Harrison441b62c2008-03-03 16:08:34 -08001199 serial = mos7840_get_usb_serial(port, __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001200 if (!serial) {
1201 dbg("%s", "Serial Paranoia failed \n");
1202 return;
1203 }
1204
1205 mos7840_port = mos7840_get_port_private(port);
Oliver Neukum0de9a702007-03-16 20:28:28 +01001206 port0 = mos7840_get_port_private(serial->port[0]);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001207
Oliver Neukum0de9a702007-03-16 20:28:28 +01001208 if (mos7840_port == NULL || port0 == NULL)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001209 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001210
1211 for (j = 0; j < NUM_URBS; ++j)
1212 usb_kill_urb(mos7840_port->write_urb_pool[j]);
1213
1214 /* Freeing Write URBs */
1215 for (j = 0; j < NUM_URBS; ++j) {
1216 if (mos7840_port->write_urb_pool[j]) {
1217 if (mos7840_port->write_urb_pool[j]->transfer_buffer)
1218 kfree(mos7840_port->write_urb_pool[j]->
1219 transfer_buffer);
1220
1221 usb_free_urb(mos7840_port->write_urb_pool[j]);
1222 }
1223 }
1224
Alan Cox95da3102008-07-22 11:09:07 +01001225 if (serial->dev)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001226 /* flush and block until tx is empty */
Alan Cox95da3102008-07-22 11:09:07 +01001227 mos7840_block_until_tx_empty(tty, mos7840_port);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001228
1229 /* While closing port, shutdown all bulk read, write *
1230 * and interrupt read if they exists */
1231 if (serial->dev) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001232 if (mos7840_port->write_urb) {
1233 dbg("%s", "Shutdown bulk write\n");
1234 usb_kill_urb(mos7840_port->write_urb);
1235 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001236 if (mos7840_port->read_urb) {
1237 dbg("%s", "Shutdown bulk read\n");
1238 usb_kill_urb(mos7840_port->read_urb);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08001239 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001240 }
1241 if ((&mos7840_port->control_urb)) {
1242 dbg("%s", "Shutdown control read\n");
Alan Cox880af9d2008-07-22 11:16:12 +01001243 /*/ usb_kill_urb (mos7840_port->control_urb); */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001244 }
1245 }
Alan Cox880af9d2008-07-22 11:16:12 +01001246/* if(mos7840_port->ctrl_buf != NULL) */
1247/* kfree(mos7840_port->ctrl_buf); */
Oliver Neukum0de9a702007-03-16 20:28:28 +01001248 port0->open_ports--;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001249 dbg("mos7840_num_open_ports in close%d:in port%d\n",
Oliver Neukum0de9a702007-03-16 20:28:28 +01001250 port0->open_ports, port->number);
1251 if (port0->open_ports == 0) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001252 if (serial->port[0]->interrupt_in_urb) {
1253 dbg("%s", "Shutdown interrupt_in_urb\n");
Oliver Neukum0de9a702007-03-16 20:28:28 +01001254 usb_kill_urb(serial->port[0]->interrupt_in_urb);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001255 }
1256 }
1257
1258 if (mos7840_port->write_urb) {
1259 /* if this urb had a transfer buffer already (old tx) free it */
Alan Cox880af9d2008-07-22 11:16:12 +01001260 if (mos7840_port->write_urb->transfer_buffer != NULL)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001261 kfree(mos7840_port->write_urb->transfer_buffer);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001262 usb_free_urb(mos7840_port->write_urb);
1263 }
1264
1265 Data = 0x0;
1266 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1267
1268 Data = 0x00;
1269 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1270
1271 mos7840_port->open = 0;
1272
1273 dbg("%s \n", "Leaving ............");
1274}
1275
1276/************************************************************************
1277 *
1278 * mos7840_block_until_chase_response
1279 *
1280 * This function will block the close until one of the following:
1281 * 1. Response to our Chase comes from mos7840
Joe Perchesdc0d5c12007-12-17 11:40:18 -08001282 * 2. A timeout of 10 seconds without activity has expired
Paul B Schroeder3f542972006-08-31 19:41:47 -05001283 * (1K of mos7840 data @ 2400 baud ==> 4 sec to empty)
1284 *
1285 ************************************************************************/
1286
Alan Cox95da3102008-07-22 11:09:07 +01001287static void mos7840_block_until_chase_response(struct tty_struct *tty,
1288 struct moschip_port *mos7840_port)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001289{
1290 int timeout = 1 * HZ;
1291 int wait = 10;
1292 int count;
1293
1294 while (1) {
Alan Cox95da3102008-07-22 11:09:07 +01001295 count = mos7840_chars_in_buffer(tty);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001296
1297 /* Check for Buffer status */
Alan Cox880af9d2008-07-22 11:16:12 +01001298 if (count <= 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001299 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001300
1301 /* Block the thread for a while */
1302 interruptible_sleep_on_timeout(&mos7840_port->wait_chase,
1303 timeout);
1304 /* No activity.. count down section */
1305 wait--;
1306 if (wait == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001307 dbg("%s - TIMEOUT", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001308 return;
1309 } else {
Joe Perchesdc0d5c12007-12-17 11:40:18 -08001310 /* Reset timeout value back to seconds */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001311 wait = 10;
1312 }
1313 }
1314
1315}
1316
1317/*****************************************************************************
1318 * mos7840_break
1319 * this function sends a break to the port
1320 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001321static void mos7840_break(struct tty_struct *tty, int break_state)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001322{
Alan Cox95da3102008-07-22 11:09:07 +01001323 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001324 unsigned char data;
1325 struct usb_serial *serial;
1326 struct moschip_port *mos7840_port;
1327
1328 dbg("%s \n", "Entering ...........");
1329 dbg("mos7840_break: Start\n");
1330
Harvey Harrison441b62c2008-03-03 16:08:34 -08001331 if (mos7840_port_paranoia_check(port, __func__)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001332 dbg("%s", "Port Paranoia failed \n");
1333 return;
1334 }
1335
Harvey Harrison441b62c2008-03-03 16:08:34 -08001336 serial = mos7840_get_usb_serial(port, __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001337 if (!serial) {
1338 dbg("%s", "Serial Paranoia failed \n");
1339 return;
1340 }
1341
1342 mos7840_port = mos7840_get_port_private(port);
1343
Alan Cox880af9d2008-07-22 11:16:12 +01001344 if (mos7840_port == NULL)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001345 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001346
Alan Cox95da3102008-07-22 11:09:07 +01001347 if (serial->dev)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001348 /* flush and block until tx is empty */
Alan Cox95da3102008-07-22 11:09:07 +01001349 mos7840_block_until_chase_response(tty, mos7840_port);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001350
Alan Cox95da3102008-07-22 11:09:07 +01001351 if (break_state == -1)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001352 data = mos7840_port->shadowLCR | LCR_SET_BREAK;
Alan Cox95da3102008-07-22 11:09:07 +01001353 else
Paul B Schroeder3f542972006-08-31 19:41:47 -05001354 data = mos7840_port->shadowLCR & ~LCR_SET_BREAK;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001355
Alan Cox6b447f042009-01-02 13:48:56 +00001356 /* FIXME: no locking on shadowLCR anywhere in driver */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001357 mos7840_port->shadowLCR = data;
1358 dbg("mcs7840_break mos7840_port->shadowLCR is %x\n",
1359 mos7840_port->shadowLCR);
1360 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER,
1361 mos7840_port->shadowLCR);
1362
1363 return;
1364}
1365
1366/*****************************************************************************
1367 * mos7840_write_room
1368 * this function is called by the tty driver when it wants to know how many
1369 * bytes of data we can accept for a specific port.
1370 * If successful, we return the amount of room that we have for this port
1371 * Otherwise we return a negative error number.
1372 *****************************************************************************/
1373
Alan Cox95da3102008-07-22 11:09:07 +01001374static int mos7840_write_room(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001375{
Alan Cox95da3102008-07-22 11:09:07 +01001376 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001377 int i;
1378 int room = 0;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001379 unsigned long flags;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001380 struct moschip_port *mos7840_port;
1381
1382 dbg("%s \n", " mos7840_write_room:entering ...........");
1383
Harvey Harrison441b62c2008-03-03 16:08:34 -08001384 if (mos7840_port_paranoia_check(port, __func__)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001385 dbg("%s", "Invalid port \n");
1386 dbg("%s \n", " mos7840_write_room:leaving ...........");
1387 return -1;
1388 }
1389
1390 mos7840_port = mos7840_get_port_private(port);
1391 if (mos7840_port == NULL) {
1392 dbg("%s \n", "mos7840_break:leaving ...........");
1393 return -1;
1394 }
1395
Oliver Neukum0de9a702007-03-16 20:28:28 +01001396 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001397 for (i = 0; i < NUM_URBS; ++i) {
Alan Cox880af9d2008-07-22 11:16:12 +01001398 if (!mos7840_port->busy[i])
Paul B Schroeder3f542972006-08-31 19:41:47 -05001399 room += URB_TRANSFER_BUFFER_SIZE;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001400 }
Oliver Neukum0de9a702007-03-16 20:28:28 +01001401 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001402
Oliver Neukum0de9a702007-03-16 20:28:28 +01001403 room = (room == 0) ? 0 : room - URB_TRANSFER_BUFFER_SIZE + 1;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001404 dbg("%s - returns %d", __func__, room);
Oliver Neukum0de9a702007-03-16 20:28:28 +01001405 return room;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001406
1407}
1408
1409/*****************************************************************************
1410 * mos7840_write
1411 * this function is called by the tty driver when data should be written to
1412 * the port.
1413 * If successful, we return the number of bytes written, otherwise we
1414 * return a negative error number.
1415 *****************************************************************************/
1416
Alan Cox95da3102008-07-22 11:09:07 +01001417static int mos7840_write(struct tty_struct *tty, struct usb_serial_port *port,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001418 const unsigned char *data, int count)
1419{
1420 int status;
1421 int i;
1422 int bytes_sent = 0;
1423 int transfer_size;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001424 unsigned long flags;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001425
1426 struct moschip_port *mos7840_port;
1427 struct usb_serial *serial;
1428 struct urb *urb;
Alan Cox880af9d2008-07-22 11:16:12 +01001429 /* __u16 Data; */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001430 const unsigned char *current_position = data;
1431 unsigned char *data1;
1432 dbg("%s \n", "entering ...........");
Alan Cox880af9d2008-07-22 11:16:12 +01001433 /* dbg("mos7840_write: mos7840_port->shadowLCR is %x\n",
1434 mos7840_port->shadowLCR); */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001435
1436#ifdef NOTMOS7840
1437 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001438 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
1439 mos7840_port->shadowLCR = Data;
1440 dbg("mos7840_write: LINE_CONTROL_REGISTER is %x\n", Data);
1441 dbg("mos7840_write: mos7840_port->shadowLCR is %x\n",
1442 mos7840_port->shadowLCR);
1443
Alan Cox880af9d2008-07-22 11:16:12 +01001444 /* Data = 0x03; */
1445 /* status = mos7840_set_uart_reg(port,LINE_CONTROL_REGISTER,Data); */
1446 /* mos7840_port->shadowLCR=Data;//Need to add later */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001447
Alan Cox880af9d2008-07-22 11:16:12 +01001448 Data |= SERIAL_LCR_DLAB; /* data latch enable in LCR 0x80 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001449 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1450
Alan Cox880af9d2008-07-22 11:16:12 +01001451 /* Data = 0x0c; */
1452 /* status = mos7840_set_uart_reg(port,DIVISOR_LATCH_LSB,Data); */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001453 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001454 status = mos7840_get_uart_reg(port, DIVISOR_LATCH_LSB, &Data);
1455 dbg("mos7840_write:DLL value is %x\n", Data);
1456
1457 Data = 0x0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001458 status = mos7840_get_uart_reg(port, DIVISOR_LATCH_MSB, &Data);
1459 dbg("mos7840_write:DLM value is %x\n", Data);
1460
1461 Data = Data & ~SERIAL_LCR_DLAB;
1462 dbg("mos7840_write: mos7840_port->shadowLCR is %x\n",
1463 mos7840_port->shadowLCR);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001464 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1465#endif
1466
Harvey Harrison441b62c2008-03-03 16:08:34 -08001467 if (mos7840_port_paranoia_check(port, __func__)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001468 dbg("%s", "Port Paranoia failed \n");
1469 return -1;
1470 }
1471
1472 serial = port->serial;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001473 if (mos7840_serial_paranoia_check(serial, __func__)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001474 dbg("%s", "Serial Paranoia failed \n");
1475 return -1;
1476 }
1477
1478 mos7840_port = mos7840_get_port_private(port);
1479 if (mos7840_port == NULL) {
1480 dbg("%s", "mos7840_port is NULL\n");
1481 return -1;
1482 }
1483
1484 /* try to find a free urb in the list */
1485 urb = NULL;
1486
Oliver Neukum0de9a702007-03-16 20:28:28 +01001487 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001488 for (i = 0; i < NUM_URBS; ++i) {
Oliver Neukum0de9a702007-03-16 20:28:28 +01001489 if (!mos7840_port->busy[i]) {
1490 mos7840_port->busy[i] = 1;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001491 urb = mos7840_port->write_urb_pool[i];
1492 dbg("\nURB:%d", i);
1493 break;
1494 }
1495 }
Oliver Neukum0de9a702007-03-16 20:28:28 +01001496 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001497
1498 if (urb == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001499 dbg("%s - no more free urbs", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001500 goto exit;
1501 }
1502
1503 if (urb->transfer_buffer == NULL) {
1504 urb->transfer_buffer =
1505 kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
1506
1507 if (urb->transfer_buffer == NULL) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001508 dev_err(&port->dev, "%s no more kernel memory...\n",
1509 __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001510 goto exit;
1511 }
1512 }
1513 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
1514
Al Viro97c49652006-10-09 20:29:03 +01001515 memcpy(urb->transfer_buffer, current_position, transfer_size);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001516
1517 /* fill urb with data and submit */
1518 usb_fill_bulk_urb(urb,
1519 serial->dev,
1520 usb_sndbulkpipe(serial->dev,
1521 port->bulk_out_endpointAddress),
1522 urb->transfer_buffer,
1523 transfer_size,
1524 mos7840_bulk_out_data_callback, mos7840_port);
1525
1526 data1 = urb->transfer_buffer;
1527 dbg("\nbulkout endpoint is %d", port->bulk_out_endpointAddress);
1528
1529 /* send it down the pipe */
1530 status = usb_submit_urb(urb, GFP_ATOMIC);
1531
1532 if (status) {
Oliver Neukum0de9a702007-03-16 20:28:28 +01001533 mos7840_port->busy[i] = 0;
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001534 dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed "
1535 "with status = %d\n", __func__, status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001536 bytes_sent = status;
1537 goto exit;
1538 }
1539 bytes_sent = transfer_size;
1540 mos7840_port->icount.tx += transfer_size;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001541 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -05001542 dbg("mos7840_port->icount.tx is %d:\n", mos7840_port->icount.tx);
Alan Cox95da3102008-07-22 11:09:07 +01001543exit:
Paul B Schroeder3f542972006-08-31 19:41:47 -05001544 return bytes_sent;
1545
1546}
1547
1548/*****************************************************************************
1549 * mos7840_throttle
1550 * this function is called by the tty driver when it wants to stop the data
1551 * being read from the port.
1552 *****************************************************************************/
1553
Alan Cox95da3102008-07-22 11:09:07 +01001554static void mos7840_throttle(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001555{
Alan Cox95da3102008-07-22 11:09:07 +01001556 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001557 struct moschip_port *mos7840_port;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001558 int status;
1559
Harvey Harrison441b62c2008-03-03 16:08:34 -08001560 if (mos7840_port_paranoia_check(port, __func__)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001561 dbg("%s", "Invalid port \n");
1562 return;
1563 }
1564
1565 dbg("- port %d\n", port->number);
1566
1567 mos7840_port = mos7840_get_port_private(port);
1568
1569 if (mos7840_port == NULL)
1570 return;
1571
1572 if (!mos7840_port->open) {
1573 dbg("%s\n", "port not opened");
1574 return;
1575 }
1576
1577 dbg("%s", "Entering .......... \n");
1578
Paul B Schroeder3f542972006-08-31 19:41:47 -05001579 /* if we are implementing XON/XOFF, send the stop character */
1580 if (I_IXOFF(tty)) {
1581 unsigned char stop_char = STOP_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01001582 status = mos7840_write(tty, port, &stop_char, 1);
1583 if (status <= 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001584 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001585 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001586 /* if we are implementing RTS/CTS, toggle that line */
1587 if (tty->termios->c_cflag & CRTSCTS) {
1588 mos7840_port->shadowMCR &= ~MCR_RTS;
Alan Cox95da3102008-07-22 11:09:07 +01001589 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001590 mos7840_port->shadowMCR);
Alan Cox95da3102008-07-22 11:09:07 +01001591 if (status < 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001592 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001593 }
1594
1595 return;
1596}
1597
1598/*****************************************************************************
1599 * mos7840_unthrottle
Alan Cox880af9d2008-07-22 11:16:12 +01001600 * this function is called by the tty driver when it wants to resume
1601 * the data being read from the port (called after mos7840_throttle is
1602 * called)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001603 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001604static void mos7840_unthrottle(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001605{
Alan Cox95da3102008-07-22 11:09:07 +01001606 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001607 int status;
1608 struct moschip_port *mos7840_port = mos7840_get_port_private(port);
1609
Harvey Harrison441b62c2008-03-03 16:08:34 -08001610 if (mos7840_port_paranoia_check(port, __func__)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001611 dbg("%s", "Invalid port \n");
1612 return;
1613 }
1614
1615 if (mos7840_port == NULL)
1616 return;
1617
1618 if (!mos7840_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001619 dbg("%s - port not opened", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001620 return;
1621 }
1622
1623 dbg("%s", "Entering .......... \n");
1624
Paul B Schroeder3f542972006-08-31 19:41:47 -05001625 /* if we are implementing XON/XOFF, send the start character */
1626 if (I_IXOFF(tty)) {
1627 unsigned char start_char = START_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01001628 status = mos7840_write(tty, port, &start_char, 1);
1629 if (status <= 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001630 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001631 }
1632
1633 /* if we are implementing RTS/CTS, toggle that line */
1634 if (tty->termios->c_cflag & CRTSCTS) {
1635 mos7840_port->shadowMCR |= MCR_RTS;
Alan Cox95da3102008-07-22 11:09:07 +01001636 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001637 mos7840_port->shadowMCR);
Alan Cox95da3102008-07-22 11:09:07 +01001638 if (status < 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001639 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001640 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001641}
1642
Alan Cox95da3102008-07-22 11:09:07 +01001643static int mos7840_tiocmget(struct tty_struct *tty, struct file *file)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001644{
Alan Cox95da3102008-07-22 11:09:07 +01001645 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001646 struct moschip_port *mos7840_port;
1647 unsigned int result;
1648 __u16 msr;
1649 __u16 mcr;
Alan Cox880af9d2008-07-22 11:16:12 +01001650 int status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001651 mos7840_port = mos7840_get_port_private(port);
1652
Harvey Harrison441b62c2008-03-03 16:08:34 -08001653 dbg("%s - port %d", __func__, port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001654
1655 if (mos7840_port == NULL)
1656 return -ENODEV;
1657
1658 status = mos7840_get_uart_reg(port, MODEM_STATUS_REGISTER, &msr);
1659 status = mos7840_get_uart_reg(port, MODEM_CONTROL_REGISTER, &mcr);
1660 result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0)
1661 | ((mcr & MCR_RTS) ? TIOCM_RTS : 0)
1662 | ((mcr & MCR_LOOPBACK) ? TIOCM_LOOP : 0)
1663 | ((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0)
1664 | ((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0)
1665 | ((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0)
1666 | ((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0);
1667
Harvey Harrison441b62c2008-03-03 16:08:34 -08001668 dbg("%s - 0x%04X", __func__, result);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001669
1670 return result;
1671}
1672
Alan Cox95da3102008-07-22 11:09:07 +01001673static int mos7840_tiocmset(struct tty_struct *tty, struct file *file,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001674 unsigned int set, unsigned int clear)
1675{
Alan Cox95da3102008-07-22 11:09:07 +01001676 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001677 struct moschip_port *mos7840_port;
1678 unsigned int mcr;
Roel Kluin87521c42008-04-17 06:16:24 +02001679 int status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001680
Harvey Harrison441b62c2008-03-03 16:08:34 -08001681 dbg("%s - port %d", __func__, port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001682
1683 mos7840_port = mos7840_get_port_private(port);
1684
1685 if (mos7840_port == NULL)
1686 return -ENODEV;
1687
Alan Coxe2984492008-02-20 20:51:45 +00001688 /* FIXME: What locks the port registers ? */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001689 mcr = mos7840_port->shadowMCR;
1690 if (clear & TIOCM_RTS)
1691 mcr &= ~MCR_RTS;
1692 if (clear & TIOCM_DTR)
1693 mcr &= ~MCR_DTR;
1694 if (clear & TIOCM_LOOP)
1695 mcr &= ~MCR_LOOPBACK;
1696
1697 if (set & TIOCM_RTS)
1698 mcr |= MCR_RTS;
1699 if (set & TIOCM_DTR)
1700 mcr |= MCR_DTR;
1701 if (set & TIOCM_LOOP)
1702 mcr |= MCR_LOOPBACK;
1703
1704 mos7840_port->shadowMCR = mcr;
1705
Paul B Schroeder3f542972006-08-31 19:41:47 -05001706 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, mcr);
1707 if (status < 0) {
1708 dbg("setting MODEM_CONTROL_REGISTER Failed\n");
Roel Kluin87521c42008-04-17 06:16:24 +02001709 return status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001710 }
1711
1712 return 0;
1713}
1714
1715/*****************************************************************************
1716 * mos7840_calc_baud_rate_divisor
1717 * this function calculates the proper baud rate divisor for the specified
1718 * baud rate.
1719 *****************************************************************************/
1720static int mos7840_calc_baud_rate_divisor(int baudRate, int *divisor,
Alan Cox880af9d2008-07-22 11:16:12 +01001721 __u16 *clk_sel_val)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001722{
1723
Harvey Harrison441b62c2008-03-03 16:08:34 -08001724 dbg("%s - %d", __func__, baudRate);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001725
1726 if (baudRate <= 115200) {
1727 *divisor = 115200 / baudRate;
1728 *clk_sel_val = 0x0;
1729 }
1730 if ((baudRate > 115200) && (baudRate <= 230400)) {
1731 *divisor = 230400 / baudRate;
1732 *clk_sel_val = 0x10;
1733 } else if ((baudRate > 230400) && (baudRate <= 403200)) {
1734 *divisor = 403200 / baudRate;
1735 *clk_sel_val = 0x20;
1736 } else if ((baudRate > 403200) && (baudRate <= 460800)) {
1737 *divisor = 460800 / baudRate;
1738 *clk_sel_val = 0x30;
1739 } else if ((baudRate > 460800) && (baudRate <= 806400)) {
1740 *divisor = 806400 / baudRate;
1741 *clk_sel_val = 0x40;
1742 } else if ((baudRate > 806400) && (baudRate <= 921600)) {
1743 *divisor = 921600 / baudRate;
1744 *clk_sel_val = 0x50;
1745 } else if ((baudRate > 921600) && (baudRate <= 1572864)) {
1746 *divisor = 1572864 / baudRate;
1747 *clk_sel_val = 0x60;
1748 } else if ((baudRate > 1572864) && (baudRate <= 3145728)) {
1749 *divisor = 3145728 / baudRate;
1750 *clk_sel_val = 0x70;
1751 }
1752 return 0;
1753
1754#ifdef NOTMCS7840
1755
1756 for (i = 0; i < ARRAY_SIZE(mos7840_divisor_table); i++) {
1757 if (mos7840_divisor_table[i].BaudRate == baudrate) {
1758 *divisor = mos7840_divisor_table[i].Divisor;
1759 return 0;
1760 }
1761 }
1762
1763 /* After trying for all the standard baud rates *
1764 * Try calculating the divisor for this baud rate */
1765
1766 if (baudrate > 75 && baudrate < 230400) {
1767 /* get the divisor */
1768 custom = (__u16) (230400L / baudrate);
1769
1770 /* Check for round off */
1771 round1 = (__u16) (2304000L / baudrate);
1772 round = (__u16) (round1 - (custom * 10));
Alan Cox880af9d2008-07-22 11:16:12 +01001773 if (round > 4)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001774 custom++;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001775 *divisor = custom;
1776
1777 dbg(" Baud %d = %d\n", baudrate, custom);
1778 return 0;
1779 }
1780
1781 dbg("%s\n", " Baud calculation Failed...");
1782 return -1;
1783#endif
1784}
1785
1786/*****************************************************************************
1787 * mos7840_send_cmd_write_baud_rate
1788 * this function sends the proper command to change the baud rate of the
1789 * specified port.
1790 *****************************************************************************/
1791
1792static int mos7840_send_cmd_write_baud_rate(struct moschip_port *mos7840_port,
1793 int baudRate)
1794{
1795 int divisor = 0;
1796 int status;
1797 __u16 Data;
1798 unsigned char number;
1799 __u16 clk_sel_val;
1800 struct usb_serial_port *port;
1801
1802 if (mos7840_port == NULL)
1803 return -1;
1804
1805 port = (struct usb_serial_port *)mos7840_port->port;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001806 if (mos7840_port_paranoia_check(port, __func__)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001807 dbg("%s", "Invalid port \n");
1808 return -1;
1809 }
1810
Harvey Harrison441b62c2008-03-03 16:08:34 -08001811 if (mos7840_serial_paranoia_check(port->serial, __func__)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001812 dbg("%s", "Invalid Serial \n");
1813 return -1;
1814 }
1815
1816 dbg("%s", "Entering .......... \n");
1817
1818 number = mos7840_port->port->number - mos7840_port->port->serial->minor;
1819
Harvey Harrison441b62c2008-03-03 16:08:34 -08001820 dbg("%s - port = %d, baud = %d", __func__,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001821 mos7840_port->port->number, baudRate);
Alan Cox880af9d2008-07-22 11:16:12 +01001822 /* reset clk_uart_sel in spregOffset */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001823 if (baudRate > 115200) {
1824#ifdef HW_flow_control
Alan Cox880af9d2008-07-22 11:16:12 +01001825 /* NOTE: need to see the pther register to modify */
1826 /* setting h/w flow control bit to 1 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001827 Data = 0x2b;
1828 mos7840_port->shadowMCR = Data;
Alan Cox880af9d2008-07-22 11:16:12 +01001829 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1830 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001831 if (status < 0) {
1832 dbg("Writing spreg failed in set_serial_baud\n");
1833 return -1;
1834 }
1835#endif
1836
1837 } else {
1838#ifdef HW_flow_control
Alan Cox880af9d2008-07-22 11:16:12 +01001839 / *setting h/w flow control bit to 0 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001840 Data = 0xb;
1841 mos7840_port->shadowMCR = Data;
Alan Cox880af9d2008-07-22 11:16:12 +01001842 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1843 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001844 if (status < 0) {
1845 dbg("Writing spreg failed in set_serial_baud\n");
1846 return -1;
1847 }
1848#endif
1849
1850 }
1851
Alan Cox880af9d2008-07-22 11:16:12 +01001852 if (1) { /* baudRate <= 115200) */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001853 clk_sel_val = 0x0;
1854 Data = 0x0;
Alan Cox880af9d2008-07-22 11:16:12 +01001855 status = mos7840_calc_baud_rate_divisor(baudRate, &divisor,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001856 &clk_sel_val);
Alan Cox880af9d2008-07-22 11:16:12 +01001857 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset,
1858 &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001859 if (status < 0) {
1860 dbg("reading spreg failed in set_serial_baud\n");
1861 return -1;
1862 }
1863 Data = (Data & 0x8f) | clk_sel_val;
Alan Cox880af9d2008-07-22 11:16:12 +01001864 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset,
1865 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001866 if (status < 0) {
1867 dbg("Writing spreg failed in set_serial_baud\n");
1868 return -1;
1869 }
1870 /* Calculate the Divisor */
1871
1872 if (status) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001873 dev_err(&port->dev, "%s - bad baud rate\n", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001874 return status;
1875 }
1876 /* Enable access to divisor latch */
1877 Data = mos7840_port->shadowLCR | SERIAL_LCR_DLAB;
1878 mos7840_port->shadowLCR = Data;
1879 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1880
1881 /* Write the divisor */
1882 Data = (unsigned char)(divisor & 0xff);
1883 dbg("set_serial_baud Value to write DLL is %x\n", Data);
1884 mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
1885
1886 Data = (unsigned char)((divisor & 0xff00) >> 8);
1887 dbg("set_serial_baud Value to write DLM is %x\n", Data);
1888 mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
1889
1890 /* Disable access to divisor latch */
1891 Data = mos7840_port->shadowLCR & ~SERIAL_LCR_DLAB;
1892 mos7840_port->shadowLCR = Data;
1893 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1894
1895 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001896 return status;
1897}
1898
1899/*****************************************************************************
1900 * mos7840_change_port_settings
1901 * This routine is called to set the UART on the device to match
1902 * the specified new settings.
1903 *****************************************************************************/
1904
Alan Cox95da3102008-07-22 11:09:07 +01001905static void mos7840_change_port_settings(struct tty_struct *tty,
1906 struct moschip_port *mos7840_port, struct ktermios *old_termios)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001907{
Paul B Schroeder3f542972006-08-31 19:41:47 -05001908 int baud;
1909 unsigned cflag;
1910 unsigned iflag;
1911 __u8 lData;
1912 __u8 lParity;
1913 __u8 lStop;
1914 int status;
1915 __u16 Data;
1916 struct usb_serial_port *port;
1917 struct usb_serial *serial;
1918
1919 if (mos7840_port == NULL)
1920 return;
1921
1922 port = (struct usb_serial_port *)mos7840_port->port;
1923
Harvey Harrison441b62c2008-03-03 16:08:34 -08001924 if (mos7840_port_paranoia_check(port, __func__)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001925 dbg("%s", "Invalid port \n");
1926 return;
1927 }
1928
Harvey Harrison441b62c2008-03-03 16:08:34 -08001929 if (mos7840_serial_paranoia_check(port->serial, __func__)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001930 dbg("%s", "Invalid Serial \n");
1931 return;
1932 }
1933
1934 serial = port->serial;
1935
Harvey Harrison441b62c2008-03-03 16:08:34 -08001936 dbg("%s - port %d", __func__, mos7840_port->port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001937
1938 if (!mos7840_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001939 dbg("%s - port not opened", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001940 return;
1941 }
1942
Paul B Schroeder3f542972006-08-31 19:41:47 -05001943 dbg("%s", "Entering .......... \n");
1944
1945 lData = LCR_BITS_8;
1946 lStop = LCR_STOP_1;
1947 lParity = LCR_PAR_NONE;
1948
1949 cflag = tty->termios->c_cflag;
1950 iflag = tty->termios->c_iflag;
1951
1952 /* Change the number of bits */
1953 if (cflag & CSIZE) {
1954 switch (cflag & CSIZE) {
1955 case CS5:
1956 lData = LCR_BITS_5;
1957 break;
1958
1959 case CS6:
1960 lData = LCR_BITS_6;
1961 break;
1962
1963 case CS7:
1964 lData = LCR_BITS_7;
1965 break;
1966 default:
1967 case CS8:
1968 lData = LCR_BITS_8;
1969 break;
1970 }
1971 }
1972 /* Change the Parity bit */
1973 if (cflag & PARENB) {
1974 if (cflag & PARODD) {
1975 lParity = LCR_PAR_ODD;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001976 dbg("%s - parity = odd", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001977 } else {
1978 lParity = LCR_PAR_EVEN;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001979 dbg("%s - parity = even", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001980 }
1981
1982 } else {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001983 dbg("%s - parity = none", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001984 }
1985
Alan Cox880af9d2008-07-22 11:16:12 +01001986 if (cflag & CMSPAR)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001987 lParity = lParity | 0x20;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001988
1989 /* Change the Stop bit */
1990 if (cflag & CSTOPB) {
1991 lStop = LCR_STOP_2;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001992 dbg("%s - stop bits = 2", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001993 } else {
1994 lStop = LCR_STOP_1;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001995 dbg("%s - stop bits = 1", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001996 }
1997
1998 /* Update the LCR with the correct value */
1999 mos7840_port->shadowLCR &=
2000 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
2001 mos7840_port->shadowLCR |= (lData | lParity | lStop);
2002
2003 dbg("mos7840_change_port_settings mos7840_port->shadowLCR is %x\n",
2004 mos7840_port->shadowLCR);
2005 /* Disable Interrupts */
2006 Data = 0x00;
2007 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
2008
2009 Data = 0x00;
2010 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
2011
2012 Data = 0xcf;
2013 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
2014
2015 /* Send the updated LCR value to the mos7840 */
2016 Data = mos7840_port->shadowLCR;
2017
2018 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
2019
2020 Data = 0x00b;
2021 mos7840_port->shadowMCR = Data;
2022 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2023 Data = 0x00b;
2024 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2025
2026 /* set up the MCR register and send it to the mos7840 */
2027
2028 mos7840_port->shadowMCR = MCR_MASTER_IE;
Alan Cox880af9d2008-07-22 11:16:12 +01002029 if (cflag & CBAUD)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002030 mos7840_port->shadowMCR |= (MCR_DTR | MCR_RTS);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002031
Alan Cox880af9d2008-07-22 11:16:12 +01002032 if (cflag & CRTSCTS)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002033 mos7840_port->shadowMCR |= (MCR_XON_ANY);
Alan Cox880af9d2008-07-22 11:16:12 +01002034 else
Paul B Schroeder3f542972006-08-31 19:41:47 -05002035 mos7840_port->shadowMCR &= ~(MCR_XON_ANY);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002036
2037 Data = mos7840_port->shadowMCR;
2038 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2039
2040 /* Determine divisor based on baud rate */
2041 baud = tty_get_baud_rate(tty);
2042
2043 if (!baud) {
2044 /* pick a default, any default... */
2045 dbg("%s\n", "Picked default baud...");
2046 baud = 9600;
2047 }
2048
Harvey Harrison441b62c2008-03-03 16:08:34 -08002049 dbg("%s - baud rate = %d", __func__, baud);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002050 status = mos7840_send_cmd_write_baud_rate(mos7840_port, baud);
2051
2052 /* Enable Interrupts */
2053 Data = 0x0c;
2054 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
2055
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002056 if (mos7840_port->read_urb_busy == false) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002057 mos7840_port->read_urb->dev = serial->dev;
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002058 mos7840_port->read_urb_busy = true;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002059 status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002060 if (status) {
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002061 dbg("usb_submit_urb(read bulk) failed, status = %d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002062 status);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002063 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002064 }
2065 }
2066 wake_up(&mos7840_port->delta_msr_wait);
2067 mos7840_port->delta_msr_cond = 1;
2068 dbg("mos7840_change_port_settings mos7840_port->shadowLCR is End %x\n",
2069 mos7840_port->shadowLCR);
2070
2071 return;
2072}
2073
2074/*****************************************************************************
2075 * mos7840_set_termios
2076 * this function is called by the tty driver when it wants to change
2077 * the termios structure
2078 *****************************************************************************/
2079
Alan Cox95da3102008-07-22 11:09:07 +01002080static void mos7840_set_termios(struct tty_struct *tty,
2081 struct usb_serial_port *port,
Alan Cox606d0992006-12-08 02:38:45 -08002082 struct ktermios *old_termios)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002083{
2084 int status;
2085 unsigned int cflag;
2086 struct usb_serial *serial;
2087 struct moschip_port *mos7840_port;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002088 dbg("mos7840_set_termios: START\n");
Harvey Harrison441b62c2008-03-03 16:08:34 -08002089 if (mos7840_port_paranoia_check(port, __func__)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002090 dbg("%s", "Invalid port \n");
2091 return;
2092 }
2093
2094 serial = port->serial;
2095
Harvey Harrison441b62c2008-03-03 16:08:34 -08002096 if (mos7840_serial_paranoia_check(serial, __func__)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002097 dbg("%s", "Invalid Serial \n");
2098 return;
2099 }
2100
2101 mos7840_port = mos7840_get_port_private(port);
2102
2103 if (mos7840_port == NULL)
2104 return;
2105
Paul B Schroeder3f542972006-08-31 19:41:47 -05002106 if (!mos7840_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002107 dbg("%s - port not opened", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002108 return;
2109 }
2110
2111 dbg("%s\n", "setting termios - ");
2112
2113 cflag = tty->termios->c_cflag;
2114
Harvey Harrison441b62c2008-03-03 16:08:34 -08002115 dbg("%s - clfag %08x iflag %08x", __func__,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002116 tty->termios->c_cflag, RELEVANT_IFLAG(tty->termios->c_iflag));
Harvey Harrison441b62c2008-03-03 16:08:34 -08002117 dbg("%s - old clfag %08x old iflag %08x", __func__,
Alan Cox3d3ddce2007-10-15 20:53:35 +01002118 old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag));
Harvey Harrison441b62c2008-03-03 16:08:34 -08002119 dbg("%s - port %d", __func__, port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002120
2121 /* change the port settings to the new ones specified */
2122
Alan Cox95da3102008-07-22 11:09:07 +01002123 mos7840_change_port_settings(tty, mos7840_port, old_termios);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002124
2125 if (!mos7840_port->read_urb) {
2126 dbg("%s", "URB KILLED !!!!!\n");
2127 return;
2128 }
2129
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002130 if (mos7840_port->read_urb_busy == false) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002131 mos7840_port->read_urb->dev = serial->dev;
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002132 mos7840_port->read_urb_busy = true;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002133 status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
2134 if (status) {
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002135 dbg("usb_submit_urb(read bulk) failed, status = %d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002136 status);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002137 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002138 }
2139 }
2140 return;
2141}
2142
2143/*****************************************************************************
2144 * mos7840_get_lsr_info - get line status register info
2145 *
2146 * Purpose: Let user call ioctl() to get info when the UART physically
2147 * is emptied. On bus types like RS485, the transmitter must
2148 * release the bus after transmitting. This must be done when
2149 * the transmit shift register is empty, not be done when the
2150 * transmit holding register is empty. This functionality
2151 * allows an RS485 driver to be written in user space.
2152 *****************************************************************************/
2153
Alan Cox95da3102008-07-22 11:09:07 +01002154static int mos7840_get_lsr_info(struct tty_struct *tty,
Al Viro97c49652006-10-09 20:29:03 +01002155 unsigned int __user *value)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002156{
2157 int count;
2158 unsigned int result = 0;
2159
Alan Cox95da3102008-07-22 11:09:07 +01002160 count = mos7840_chars_in_buffer(tty);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002161 if (count == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002162 dbg("%s -- Empty", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002163 result = TIOCSER_TEMT;
2164 }
2165
2166 if (copy_to_user(value, &result, sizeof(int)))
2167 return -EFAULT;
2168 return 0;
2169}
2170
2171/*****************************************************************************
Paul B Schroeder3f542972006-08-31 19:41:47 -05002172 * mos7840_set_modem_info
2173 * function to set modem info
2174 *****************************************************************************/
2175
Alan Cox95da3102008-07-22 11:09:07 +01002176/* FIXME: Should be using the model control hooks */
2177
Paul B Schroeder3f542972006-08-31 19:41:47 -05002178static int mos7840_set_modem_info(struct moschip_port *mos7840_port,
Al Viro97c49652006-10-09 20:29:03 +01002179 unsigned int cmd, unsigned int __user *value)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002180{
2181 unsigned int mcr;
2182 unsigned int arg;
2183 __u16 Data;
2184 int status;
2185 struct usb_serial_port *port;
2186
2187 if (mos7840_port == NULL)
2188 return -1;
2189
2190 port = (struct usb_serial_port *)mos7840_port->port;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002191 if (mos7840_port_paranoia_check(port, __func__)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002192 dbg("%s", "Invalid port \n");
2193 return -1;
2194 }
2195
2196 mcr = mos7840_port->shadowMCR;
2197
2198 if (copy_from_user(&arg, value, sizeof(int)))
2199 return -EFAULT;
2200
2201 switch (cmd) {
2202 case TIOCMBIS:
2203 if (arg & TIOCM_RTS)
2204 mcr |= MCR_RTS;
2205 if (arg & TIOCM_DTR)
2206 mcr |= MCR_RTS;
2207 if (arg & TIOCM_LOOP)
2208 mcr |= MCR_LOOPBACK;
2209 break;
2210
2211 case TIOCMBIC:
2212 if (arg & TIOCM_RTS)
2213 mcr &= ~MCR_RTS;
2214 if (arg & TIOCM_DTR)
2215 mcr &= ~MCR_RTS;
2216 if (arg & TIOCM_LOOP)
2217 mcr &= ~MCR_LOOPBACK;
2218 break;
2219
2220 case TIOCMSET:
2221 /* turn off the RTS and DTR and LOOPBACK
2222 * and then only turn on what was asked to */
2223 mcr &= ~(MCR_RTS | MCR_DTR | MCR_LOOPBACK);
2224 mcr |= ((arg & TIOCM_RTS) ? MCR_RTS : 0);
2225 mcr |= ((arg & TIOCM_DTR) ? MCR_DTR : 0);
2226 mcr |= ((arg & TIOCM_LOOP) ? MCR_LOOPBACK : 0);
2227 break;
2228 }
2229
Alan Cox6b447f042009-01-02 13:48:56 +00002230 lock_kernel();
Paul B Schroeder3f542972006-08-31 19:41:47 -05002231 mos7840_port->shadowMCR = mcr;
2232
2233 Data = mos7840_port->shadowMCR;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002234 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
Alan Cox6b447f042009-01-02 13:48:56 +00002235 unlock_kernel();
Paul B Schroeder3f542972006-08-31 19:41:47 -05002236 if (status < 0) {
2237 dbg("setting MODEM_CONTROL_REGISTER Failed\n");
2238 return -1;
2239 }
2240
2241 return 0;
2242}
2243
2244/*****************************************************************************
2245 * mos7840_get_modem_info
2246 * function to get modem info
2247 *****************************************************************************/
2248
2249static int mos7840_get_modem_info(struct moschip_port *mos7840_port,
Al Viro97c49652006-10-09 20:29:03 +01002250 unsigned int __user *value)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002251{
2252 unsigned int result = 0;
2253 __u16 msr;
2254 unsigned int mcr = mos7840_port->shadowMCR;
Alan Cox880af9d2008-07-22 11:16:12 +01002255 mos7840_get_uart_reg(mos7840_port->port,
Alan Cox95da3102008-07-22 11:09:07 +01002256 MODEM_STATUS_REGISTER, &msr);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002257 result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0) /* 0x002 */
2258 |((mcr & MCR_RTS) ? TIOCM_RTS : 0) /* 0x004 */
2259 |((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0) /* 0x020 */
2260 |((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0) /* 0x040 */
2261 |((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0) /* 0x080 */
2262 |((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0); /* 0x100 */
2263
Harvey Harrison441b62c2008-03-03 16:08:34 -08002264 dbg("%s -- %x", __func__, result);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002265
2266 if (copy_to_user(value, &result, sizeof(int)))
2267 return -EFAULT;
2268 return 0;
2269}
2270
2271/*****************************************************************************
2272 * mos7840_get_serial_info
2273 * function to get information about serial port
2274 *****************************************************************************/
2275
2276static int mos7840_get_serial_info(struct moschip_port *mos7840_port,
Al Viro97c49652006-10-09 20:29:03 +01002277 struct serial_struct __user *retinfo)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002278{
2279 struct serial_struct tmp;
2280
2281 if (mos7840_port == NULL)
2282 return -1;
2283
2284 if (!retinfo)
2285 return -EFAULT;
2286
2287 memset(&tmp, 0, sizeof(tmp));
2288
2289 tmp.type = PORT_16550A;
2290 tmp.line = mos7840_port->port->serial->minor;
2291 tmp.port = mos7840_port->port->number;
2292 tmp.irq = 0;
2293 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
2294 tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
2295 tmp.baud_base = 9600;
2296 tmp.close_delay = 5 * HZ;
2297 tmp.closing_wait = 30 * HZ;
2298
2299 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2300 return -EFAULT;
2301 return 0;
2302}
2303
2304/*****************************************************************************
2305 * SerialIoctl
2306 * this function handles any ioctl calls to the driver
2307 *****************************************************************************/
2308
Alan Cox95da3102008-07-22 11:09:07 +01002309static int mos7840_ioctl(struct tty_struct *tty, struct file *file,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002310 unsigned int cmd, unsigned long arg)
2311{
Alan Cox95da3102008-07-22 11:09:07 +01002312 struct usb_serial_port *port = tty->driver_data;
Al Viro97c49652006-10-09 20:29:03 +01002313 void __user *argp = (void __user *)arg;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002314 struct moschip_port *mos7840_port;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002315
2316 struct async_icount cnow;
2317 struct async_icount cprev;
2318 struct serial_icounter_struct icount;
2319 int mosret = 0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002320
Harvey Harrison441b62c2008-03-03 16:08:34 -08002321 if (mos7840_port_paranoia_check(port, __func__)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002322 dbg("%s", "Invalid port \n");
2323 return -1;
2324 }
2325
2326 mos7840_port = mos7840_get_port_private(port);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002327
2328 if (mos7840_port == NULL)
2329 return -1;
2330
Harvey Harrison441b62c2008-03-03 16:08:34 -08002331 dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002332
2333 switch (cmd) {
2334 /* return number of bytes available */
2335
Paul B Schroeder3f542972006-08-31 19:41:47 -05002336 case TIOCSERGETLSR:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002337 dbg("%s (%d) TIOCSERGETLSR", __func__, port->number);
Alan Cox95da3102008-07-22 11:09:07 +01002338 return mos7840_get_lsr_info(tty, argp);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002339 return 0;
2340
Alan Cox95da3102008-07-22 11:09:07 +01002341 /* FIXME: use the modem hooks and remove this */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002342 case TIOCMBIS:
2343 case TIOCMBIC:
2344 case TIOCMSET:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002345 dbg("%s (%d) TIOCMSET/TIOCMBIC/TIOCMSET", __func__,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002346 port->number);
2347 mosret =
Al Viro97c49652006-10-09 20:29:03 +01002348 mos7840_set_modem_info(mos7840_port, cmd, argp);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002349 return mosret;
2350
2351 case TIOCMGET:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002352 dbg("%s (%d) TIOCMGET", __func__, port->number);
Al Viro97c49652006-10-09 20:29:03 +01002353 return mos7840_get_modem_info(mos7840_port, argp);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002354
2355 case TIOCGSERIAL:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002356 dbg("%s (%d) TIOCGSERIAL", __func__, port->number);
Al Viro97c49652006-10-09 20:29:03 +01002357 return mos7840_get_serial_info(mos7840_port, argp);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002358
2359 case TIOCSSERIAL:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002360 dbg("%s (%d) TIOCSSERIAL", __func__, port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002361 break;
2362
2363 case TIOCMIWAIT:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002364 dbg("%s (%d) TIOCMIWAIT", __func__, port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002365 cprev = mos7840_port->icount;
2366 while (1) {
Alan Cox880af9d2008-07-22 11:16:12 +01002367 /* interruptible_sleep_on(&mos7840_port->delta_msr_wait); */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002368 mos7840_port->delta_msr_cond = 0;
2369 wait_event_interruptible(mos7840_port->delta_msr_wait,
2370 (mos7840_port->
2371 delta_msr_cond == 1));
2372
2373 /* see if a signal did it */
2374 if (signal_pending(current))
2375 return -ERESTARTSYS;
2376 cnow = mos7840_port->icount;
Oliver Neukum0de9a702007-03-16 20:28:28 +01002377 smp_rmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -05002378 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2379 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
2380 return -EIO; /* no change => error */
2381 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
2382 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
2383 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
2384 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
2385 return 0;
2386 }
2387 cprev = cnow;
2388 }
2389 /* NOTREACHED */
2390 break;
2391
2392 case TIOCGICOUNT:
2393 cnow = mos7840_port->icount;
Oliver Neukum0de9a702007-03-16 20:28:28 +01002394 smp_rmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -05002395 icount.cts = cnow.cts;
2396 icount.dsr = cnow.dsr;
2397 icount.rng = cnow.rng;
2398 icount.dcd = cnow.dcd;
2399 icount.rx = cnow.rx;
2400 icount.tx = cnow.tx;
2401 icount.frame = cnow.frame;
2402 icount.overrun = cnow.overrun;
2403 icount.parity = cnow.parity;
2404 icount.brk = cnow.brk;
2405 icount.buf_overrun = cnow.buf_overrun;
2406
Harvey Harrison441b62c2008-03-03 16:08:34 -08002407 dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __func__,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002408 port->number, icount.rx, icount.tx);
Al Viro97c49652006-10-09 20:29:03 +01002409 if (copy_to_user(argp, &icount, sizeof(icount)))
Paul B Schroeder3f542972006-08-31 19:41:47 -05002410 return -EFAULT;
2411 return 0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002412 default:
2413 break;
2414 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002415 return -ENOIOCTLCMD;
2416}
2417
2418static int mos7840_calc_num_ports(struct usb_serial *serial)
2419{
Oliver Neukum0de9a702007-03-16 20:28:28 +01002420 int mos7840_num_ports = 0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002421
2422 dbg("numberofendpoints: %d \n",
2423 (int)serial->interface->cur_altsetting->desc.bNumEndpoints);
2424 dbg("numberofendpoints: %d \n",
2425 (int)serial->interface->altsetting->desc.bNumEndpoints);
2426 if (serial->interface->cur_altsetting->desc.bNumEndpoints == 5) {
Oliver Neukum0de9a702007-03-16 20:28:28 +01002427 mos7840_num_ports = serial->num_ports = 2;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002428 } else if (serial->interface->cur_altsetting->desc.bNumEndpoints == 9) {
Oliver Neukum0de9a702007-03-16 20:28:28 +01002429 serial->num_bulk_in = 4;
2430 serial->num_bulk_out = 4;
2431 mos7840_num_ports = serial->num_ports = 4;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002432 }
2433
2434 return mos7840_num_ports;
2435}
2436
2437/****************************************************************************
2438 * mos7840_startup
2439 ****************************************************************************/
2440
2441static int mos7840_startup(struct usb_serial *serial)
2442{
2443 struct moschip_port *mos7840_port;
2444 struct usb_device *dev;
2445 int i, status;
2446
2447 __u16 Data;
2448 dbg("%s \n", " mos7840_startup :entering..........");
2449
2450 if (!serial) {
2451 dbg("%s\n", "Invalid Handler");
2452 return -1;
2453 }
2454
2455 dev = serial->dev;
2456
2457 dbg("%s\n", "Entering...");
2458
2459 /* we set up the pointers to the endpoints in the mos7840_open *
2460 * function, as the structures aren't created yet. */
2461
2462 /* set up port private structures */
2463 for (i = 0; i < serial->num_ports; ++i) {
Burman Yan7ac9da12006-11-22 20:54:38 +02002464 mos7840_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002465 if (mos7840_port == NULL) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07002466 dev_err(&dev->dev, "%s - Out of memory\n", __func__);
Oliver Neukum0de9a702007-03-16 20:28:28 +01002467 status = -ENOMEM;
2468 i--; /* don't follow NULL pointer cleaning up */
2469 goto error;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002470 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002471
Alan Cox880af9d2008-07-22 11:16:12 +01002472 /* Initialize all port interrupt end point to port 0 int
2473 * endpoint. Our device has only one interrupt end point
2474 * common to all port */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002475
2476 mos7840_port->port = serial->port[i];
2477 mos7840_set_port_private(serial->port[i], mos7840_port);
Oliver Neukum0de9a702007-03-16 20:28:28 +01002478 spin_lock_init(&mos7840_port->pool_lock);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002479
2480 mos7840_port->port_num = ((serial->port[i]->number -
2481 (serial->port[i]->serial->minor)) +
2482 1);
2483
2484 if (mos7840_port->port_num == 1) {
2485 mos7840_port->SpRegOffset = 0x0;
2486 mos7840_port->ControlRegOffset = 0x1;
2487 mos7840_port->DcrRegOffset = 0x4;
2488 } else if ((mos7840_port->port_num == 2)
Oliver Neukum0de9a702007-03-16 20:28:28 +01002489 && (serial->num_ports == 4)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002490 mos7840_port->SpRegOffset = 0x8;
2491 mos7840_port->ControlRegOffset = 0x9;
2492 mos7840_port->DcrRegOffset = 0x16;
2493 } else if ((mos7840_port->port_num == 2)
Oliver Neukum0de9a702007-03-16 20:28:28 +01002494 && (serial->num_ports == 2)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002495 mos7840_port->SpRegOffset = 0xa;
2496 mos7840_port->ControlRegOffset = 0xb;
2497 mos7840_port->DcrRegOffset = 0x19;
2498 } else if ((mos7840_port->port_num == 3)
Oliver Neukum0de9a702007-03-16 20:28:28 +01002499 && (serial->num_ports == 4)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002500 mos7840_port->SpRegOffset = 0xa;
2501 mos7840_port->ControlRegOffset = 0xb;
2502 mos7840_port->DcrRegOffset = 0x19;
2503 } else if ((mos7840_port->port_num == 4)
Oliver Neukum0de9a702007-03-16 20:28:28 +01002504 && (serial->num_ports == 4)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002505 mos7840_port->SpRegOffset = 0xc;
2506 mos7840_port->ControlRegOffset = 0xd;
2507 mos7840_port->DcrRegOffset = 0x1c;
2508 }
2509 mos7840_dump_serial_port(mos7840_port);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002510 mos7840_set_port_private(serial->port[i], mos7840_port);
2511
Alan Cox880af9d2008-07-22 11:16:12 +01002512 /* enable rx_disable bit in control register */
2513 status = mos7840_get_reg_sync(serial->port[i],
2514 mos7840_port->ControlRegOffset, &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002515 if (status < 0) {
2516 dbg("Reading ControlReg failed status-0x%x\n", status);
2517 break;
2518 } else
2519 dbg("ControlReg Reading success val is %x, status%d\n",
2520 Data, status);
Alan Cox880af9d2008-07-22 11:16:12 +01002521 Data |= 0x08; /* setting driver done bit */
2522 Data |= 0x04; /* sp1_bit to have cts change reflect in
2523 modem status reg */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002524
Alan Cox880af9d2008-07-22 11:16:12 +01002525 /* Data |= 0x20; //rx_disable bit */
2526 status = mos7840_set_reg_sync(serial->port[i],
Paul B Schroeder3f542972006-08-31 19:41:47 -05002527 mos7840_port->ControlRegOffset, Data);
2528 if (status < 0) {
2529 dbg("Writing ControlReg failed(rx_disable) status-0x%x\n", status);
2530 break;
2531 } else
2532 dbg("ControlReg Writing success(rx_disable) status%d\n",
2533 status);
2534
Alan Cox880af9d2008-07-22 11:16:12 +01002535 /* Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2
2536 and 0x24 in DCR3 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002537 Data = 0x01;
Alan Cox880af9d2008-07-22 11:16:12 +01002538 status = mos7840_set_reg_sync(serial->port[i],
2539 (__u16) (mos7840_port->DcrRegOffset + 0), Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002540 if (status < 0) {
2541 dbg("Writing DCR0 failed status-0x%x\n", status);
2542 break;
2543 } else
2544 dbg("DCR0 Writing success status%d\n", status);
2545
2546 Data = 0x05;
Alan Cox880af9d2008-07-22 11:16:12 +01002547 status = mos7840_set_reg_sync(serial->port[i],
2548 (__u16) (mos7840_port->DcrRegOffset + 1), Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002549 if (status < 0) {
2550 dbg("Writing DCR1 failed status-0x%x\n", status);
2551 break;
2552 } else
2553 dbg("DCR1 Writing success status%d\n", status);
2554
2555 Data = 0x24;
Alan Cox880af9d2008-07-22 11:16:12 +01002556 status = mos7840_set_reg_sync(serial->port[i],
2557 (__u16) (mos7840_port->DcrRegOffset + 2), Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002558 if (status < 0) {
2559 dbg("Writing DCR2 failed status-0x%x\n", status);
2560 break;
2561 } else
2562 dbg("DCR2 Writing success status%d\n", status);
2563
Alan Cox880af9d2008-07-22 11:16:12 +01002564 /* write values in clkstart0x0 and clkmulti 0x20 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002565 Data = 0x0;
Alan Cox880af9d2008-07-22 11:16:12 +01002566 status = mos7840_set_reg_sync(serial->port[i],
Paul B Schroeder3f542972006-08-31 19:41:47 -05002567 CLK_START_VALUE_REGISTER, Data);
2568 if (status < 0) {
2569 dbg("Writing CLK_START_VALUE_REGISTER failed status-0x%x\n", status);
2570 break;
2571 } else
2572 dbg("CLK_START_VALUE_REGISTER Writing success status%d\n", status);
2573
2574 Data = 0x20;
Alan Cox880af9d2008-07-22 11:16:12 +01002575 status = mos7840_set_reg_sync(serial->port[i],
2576 CLK_MULTI_REGISTER, Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002577 if (status < 0) {
2578 dbg("Writing CLK_MULTI_REGISTER failed status-0x%x\n",
2579 status);
Oliver Neukum0de9a702007-03-16 20:28:28 +01002580 goto error;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002581 } else
2582 dbg("CLK_MULTI_REGISTER Writing success status%d\n",
2583 status);
2584
Alan Cox880af9d2008-07-22 11:16:12 +01002585 /* write value 0x0 to scratchpad register */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002586 Data = 0x00;
Alan Cox880af9d2008-07-22 11:16:12 +01002587 status = mos7840_set_uart_reg(serial->port[i],
2588 SCRATCH_PAD_REGISTER, Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002589 if (status < 0) {
2590 dbg("Writing SCRATCH_PAD_REGISTER failed status-0x%x\n",
2591 status);
2592 break;
2593 } else
2594 dbg("SCRATCH_PAD_REGISTER Writing success status%d\n",
2595 status);
2596
Alan Cox880af9d2008-07-22 11:16:12 +01002597 /* Zero Length flag register */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002598 if ((mos7840_port->port_num != 1)
Oliver Neukum0de9a702007-03-16 20:28:28 +01002599 && (serial->num_ports == 2)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002600
2601 Data = 0xff;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002602 status = mos7840_set_reg_sync(serial->port[i],
Alan Cox880af9d2008-07-22 11:16:12 +01002603 (__u16) (ZLP_REG1 +
2604 ((__u16)mos7840_port->port_num)), Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002605 dbg("ZLIP offset%x\n",
2606 (__u16) (ZLP_REG1 +
Alan Cox880af9d2008-07-22 11:16:12 +01002607 ((__u16) mos7840_port->port_num)));
Paul B Schroeder3f542972006-08-31 19:41:47 -05002608 if (status < 0) {
2609 dbg("Writing ZLP_REG%d failed status-0x%x\n",
2610 i + 2, status);
2611 break;
2612 } else
2613 dbg("ZLP_REG%d Writing success status%d\n",
2614 i + 2, status);
2615 } else {
2616 Data = 0xff;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002617 status = mos7840_set_reg_sync(serial->port[i],
Alan Cox880af9d2008-07-22 11:16:12 +01002618 (__u16) (ZLP_REG1 +
2619 ((__u16)mos7840_port->port_num) - 0x1), Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002620 dbg("ZLIP offset%x\n",
2621 (__u16) (ZLP_REG1 +
2622 ((__u16) mos7840_port->port_num) - 0x1));
2623 if (status < 0) {
2624 dbg("Writing ZLP_REG%d failed status-0x%x\n",
2625 i + 1, status);
2626 break;
2627 } else
2628 dbg("ZLP_REG%d Writing success status%d\n",
2629 i + 1, status);
2630
2631 }
Oliver Neukum0de9a702007-03-16 20:28:28 +01002632 mos7840_port->control_urb = usb_alloc_urb(0, GFP_KERNEL);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002633 mos7840_port->ctrl_buf = kmalloc(16, GFP_KERNEL);
Alan Cox880af9d2008-07-22 11:16:12 +01002634 mos7840_port->dr = kmalloc(sizeof(struct usb_ctrlrequest),
2635 GFP_KERNEL);
2636 if (!mos7840_port->control_urb || !mos7840_port->ctrl_buf ||
2637 !mos7840_port->dr) {
Oliver Neukum0de9a702007-03-16 20:28:28 +01002638 status = -ENOMEM;
2639 goto error;
2640 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002641 }
2642
Alan Cox880af9d2008-07-22 11:16:12 +01002643 /* Zero Length flag enable */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002644 Data = 0x0f;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002645 status = mos7840_set_reg_sync(serial->port[0], ZLP_REG5, Data);
2646 if (status < 0) {
2647 dbg("Writing ZLP_REG5 failed status-0x%x\n", status);
Roel Kluin7ced46c2007-10-27 03:36:37 +02002648 goto error;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002649 } else
2650 dbg("ZLP_REG5 Writing success status%d\n", status);
2651
2652 /* setting configuration feature to one */
2653 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
Al Viro97c49652006-10-09 20:29:03 +01002654 (__u8) 0x03, 0x00, 0x01, 0x00, NULL, 0x00, 5 * HZ);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002655 return 0;
Oliver Neukum0de9a702007-03-16 20:28:28 +01002656error:
2657 for (/* nothing */; i >= 0; i--) {
2658 mos7840_port = mos7840_get_port_private(serial->port[i]);
2659
2660 kfree(mos7840_port->dr);
2661 kfree(mos7840_port->ctrl_buf);
2662 usb_free_urb(mos7840_port->control_urb);
2663 kfree(mos7840_port);
2664 serial->port[i] = NULL;
2665 }
2666 return status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002667}
2668
2669/****************************************************************************
2670 * mos7840_shutdown
2671 * This function is called whenever the device is removed from the usb bus.
2672 ****************************************************************************/
2673
2674static void mos7840_shutdown(struct usb_serial *serial)
2675{
2676 int i;
Oliver Neukum0de9a702007-03-16 20:28:28 +01002677 unsigned long flags;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002678 struct moschip_port *mos7840_port;
2679 dbg("%s \n", " shutdown :entering..........");
2680
2681 if (!serial) {
2682 dbg("%s", "Invalid Handler \n");
2683 return;
2684 }
2685
Alan Cox880af9d2008-07-22 11:16:12 +01002686 /* check for the ports to be closed,close the ports and disconnect */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002687
2688 /* free private structure allocated for serial port *
2689 * stop reads and writes on all ports */
2690
2691 for (i = 0; i < serial->num_ports; ++i) {
2692 mos7840_port = mos7840_get_port_private(serial->port[i]);
Oliver Neukum0de9a702007-03-16 20:28:28 +01002693 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
2694 mos7840_port->zombie = 1;
2695 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002696 usb_kill_urb(mos7840_port->control_urb);
Oliver Neukum0de9a702007-03-16 20:28:28 +01002697 kfree(mos7840_port->ctrl_buf);
2698 kfree(mos7840_port->dr);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002699 kfree(mos7840_port);
2700 mos7840_set_port_private(serial->port[i], NULL);
2701 }
2702
2703 dbg("%s\n", "Thank u :: ");
2704
2705}
2706
Johannes Hölzld9b1b782006-12-17 21:50:24 +01002707static struct usb_driver io_driver = {
2708 .name = "mos7840",
2709 .probe = usb_serial_probe,
2710 .disconnect = usb_serial_disconnect,
2711 .id_table = moschip_id_table_combined,
2712 .no_dynamic_id = 1,
2713};
2714
Paul B Schroeder3f542972006-08-31 19:41:47 -05002715static struct usb_serial_driver moschip7840_4port_device = {
2716 .driver = {
2717 .owner = THIS_MODULE,
2718 .name = "mos7840",
2719 },
2720 .description = DRIVER_DESC,
Johannes Hölzld9b1b782006-12-17 21:50:24 +01002721 .usb_driver = &io_driver,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002722 .id_table = moschip_port_id_table,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002723 .num_ports = 4,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002724 .open = mos7840_open,
2725 .close = mos7840_close,
2726 .write = mos7840_write,
2727 .write_room = mos7840_write_room,
2728 .chars_in_buffer = mos7840_chars_in_buffer,
2729 .throttle = mos7840_throttle,
2730 .unthrottle = mos7840_unthrottle,
2731 .calc_num_ports = mos7840_calc_num_ports,
2732#ifdef MCSSerialProbe
2733 .probe = mos7840_serial_probe,
2734#endif
2735 .ioctl = mos7840_ioctl,
2736 .set_termios = mos7840_set_termios,
2737 .break_ctl = mos7840_break,
2738 .tiocmget = mos7840_tiocmget,
2739 .tiocmset = mos7840_tiocmset,
2740 .attach = mos7840_startup,
2741 .shutdown = mos7840_shutdown,
2742 .read_bulk_callback = mos7840_bulk_in_callback,
2743 .read_int_callback = mos7840_interrupt_callback,
2744};
2745
Paul B Schroeder3f542972006-08-31 19:41:47 -05002746/****************************************************************************
2747 * moschip7840_init
2748 * This is called by the module subsystem, or on startup to initialize us
2749 ****************************************************************************/
2750static int __init moschip7840_init(void)
2751{
2752 int retval;
2753
2754 dbg("%s \n", " mos7840_init :entering..........");
2755
2756 /* Register with the usb serial */
2757 retval = usb_serial_register(&moschip7840_4port_device);
2758
2759 if (retval)
2760 goto failed_port_device_register;
2761
2762 dbg("%s\n", "Entring...");
Greg Kroah-Hartmanc197a8d2008-08-18 13:21:04 -07002763 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
2764 DRIVER_DESC "\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002765
2766 /* Register with the usb */
2767 retval = usb_register(&io_driver);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002768 if (retval == 0) {
2769 dbg("%s\n", "Leaving...");
2770 return 0;
2771 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002772 usb_serial_deregister(&moschip7840_4port_device);
Alan Cox880af9d2008-07-22 11:16:12 +01002773failed_port_device_register:
Paul B Schroeder3f542972006-08-31 19:41:47 -05002774 return retval;
2775}
2776
2777/****************************************************************************
2778 * moschip7840_exit
2779 * Called when the driver is about to be unloaded.
2780 ****************************************************************************/
2781static void __exit moschip7840_exit(void)
2782{
2783
2784 dbg("%s \n", " mos7840_exit :entering..........");
2785
2786 usb_deregister(&io_driver);
2787
2788 usb_serial_deregister(&moschip7840_4port_device);
2789
2790 dbg("%s\n", "Entring...");
2791}
2792
2793module_init(moschip7840_init);
2794module_exit(moschip7840_exit);
2795
2796/* Module information */
2797MODULE_DESCRIPTION(DRIVER_DESC);
2798MODULE_LICENSE("GPL");
2799
2800module_param(debug, bool, S_IRUGO | S_IWUSR);
2801MODULE_PARM_DESC(debug, "Debug enabled or not");