tty: USB does not need the filp argument in the drivers
[deliverable/linux.git] / drivers / usb / serial / mos7840.c
CommitLineData
3f542974
PS
1/*
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>
405f5571 29#include <linux/smp_lock.h>
3f542974
PS
30#include <linux/tty.h>
31#include <linux/tty_driver.h>
32#include <linux/tty_flip.h>
33#include <linux/module.h>
34#include <linux/serial.h>
35#include <linux/usb.h>
36#include <linux/usb/serial.h>
880af9db 37#include <linux/uaccess.h>
3f542974
PS
38
39/*
40 * Version Information
41 */
37768adf 42#define DRIVER_VERSION "1.3.2"
3f542974
PS
43#define DRIVER_DESC "Moschip 7840/7820 USB Serial Driver"
44
45/*
46 * 16C50 UART register defines
47 */
48
49#define LCR_BITS_5 0x00 /* 5 bits/char */
50#define LCR_BITS_6 0x01 /* 6 bits/char */
51#define LCR_BITS_7 0x02 /* 7 bits/char */
52#define LCR_BITS_8 0x03 /* 8 bits/char */
53#define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
54
55#define LCR_STOP_1 0x00 /* 1 stop bit */
56#define LCR_STOP_1_5 0x04 /* 1.5 stop bits (if 5 bits/char) */
57#define LCR_STOP_2 0x04 /* 2 stop bits (if 6-8 bits/char) */
58#define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
59
60#define LCR_PAR_NONE 0x00 /* No parity */
61#define LCR_PAR_ODD 0x08 /* Odd parity */
62#define LCR_PAR_EVEN 0x18 /* Even parity */
63#define LCR_PAR_MARK 0x28 /* Force parity bit to 1 */
64#define LCR_PAR_SPACE 0x38 /* Force parity bit to 0 */
65#define LCR_PAR_MASK 0x38 /* Mask for parity field */
66
67#define LCR_SET_BREAK 0x40 /* Set Break condition */
68#define LCR_DL_ENABLE 0x80 /* Enable access to divisor latch */
69
70#define MCR_DTR 0x01 /* Assert DTR */
71#define MCR_RTS 0x02 /* Assert RTS */
72#define MCR_OUT1 0x04 /* Loopback only: Sets state of RI */
73#define MCR_MASTER_IE 0x08 /* Enable interrupt outputs */
74#define MCR_LOOPBACK 0x10 /* Set internal (digital) loopback mode */
75#define MCR_XON_ANY 0x20 /* Enable any char to exit XOFF mode */
76
77#define MOS7840_MSR_CTS 0x10 /* Current state of CTS */
78#define MOS7840_MSR_DSR 0x20 /* Current state of DSR */
79#define MOS7840_MSR_RI 0x40 /* Current state of RI */
80#define MOS7840_MSR_CD 0x80 /* Current state of CD */
81
82/*
83 * Defines used for sending commands to port
84 */
85
880af9db
AC
86#define WAIT_FOR_EVER (HZ * 0) /* timeout urb is wait for ever */
87#define MOS_WDR_TIMEOUT (HZ * 5) /* default urb timeout */
3f542974
PS
88
89#define MOS_PORT1 0x0200
90#define MOS_PORT2 0x0300
91#define MOS_VENREG 0x0000
92#define MOS_MAX_PORT 0x02
93#define MOS_WRITE 0x0E
94#define MOS_READ 0x0D
95
96/* Requests */
97#define MCS_RD_RTYPE 0xC0
98#define MCS_WR_RTYPE 0x40
99#define MCS_RDREQ 0x0D
100#define MCS_WRREQ 0x0E
101#define MCS_CTRL_TIMEOUT 500
102#define VENDOR_READ_LENGTH (0x01)
103
104#define MAX_NAME_LEN 64
105
880af9db
AC
106#define ZLP_REG1 0x3A /* Zero_Flag_Reg1 58 */
107#define ZLP_REG5 0x3E /* Zero_Flag_Reg5 62 */
3f542974
PS
108
109/* For higher baud Rates use TIOCEXBAUD */
110#define TIOCEXBAUD 0x5462
111
112/* vendor id and device id defines */
113
11e1abb4 114/* The native mos7840/7820 component */
3f542974
PS
115#define USB_VENDOR_ID_MOSCHIP 0x9710
116#define MOSCHIP_DEVICE_ID_7840 0x7840
117#define MOSCHIP_DEVICE_ID_7820 0x7820
11e1abb4
DL
118/* The native component can have its vendor/device id's overridden
119 * in vendor-specific implementations. Such devices can be handled
120 * by making a change here, in moschip_port_id_table, and in
121 * moschip_id_table_combined
122 */
123#define USB_VENDOR_ID_BANDB 0x0856
124#define BANDB_DEVICE_ID_USOPTL4_4 0xAC44
125#define BANDB_DEVICE_ID_USOPTL4_2 0xAC42
3f542974 126
9d498bea
RL
127/* This driver also supports
128 * ATEN UC2324 device using Moschip MCS7840
129 * ATEN UC2322 device using Moschip MCS7820
130 */
e9b8cffa
TC
131#define USB_VENDOR_ID_ATENINTL 0x0557
132#define ATENINTL_DEVICE_ID_UC2324 0x2011
9d498bea 133#define ATENINTL_DEVICE_ID_UC2322 0x7820
e9b8cffa 134
11e1abb4 135/* Interrupt Routine Defines */
3f542974
PS
136
137#define SERIAL_IIR_RLS 0x06
138#define SERIAL_IIR_MS 0x00
139
140/*
141 * Emulation of the bit mask on the LINE STATUS REGISTER.
142 */
143#define SERIAL_LSR_DR 0x0001
144#define SERIAL_LSR_OE 0x0002
145#define SERIAL_LSR_PE 0x0004
146#define SERIAL_LSR_FE 0x0008
147#define SERIAL_LSR_BI 0x0010
148
149#define MOS_MSR_DELTA_CTS 0x10
150#define MOS_MSR_DELTA_DSR 0x20
151#define MOS_MSR_DELTA_RI 0x40
152#define MOS_MSR_DELTA_CD 0x80
153
880af9db 154/* Serial Port register Address */
3f542974
PS
155#define INTERRUPT_ENABLE_REGISTER ((__u16)(0x01))
156#define FIFO_CONTROL_REGISTER ((__u16)(0x02))
157#define LINE_CONTROL_REGISTER ((__u16)(0x03))
158#define MODEM_CONTROL_REGISTER ((__u16)(0x04))
159#define LINE_STATUS_REGISTER ((__u16)(0x05))
160#define MODEM_STATUS_REGISTER ((__u16)(0x06))
161#define SCRATCH_PAD_REGISTER ((__u16)(0x07))
162#define DIVISOR_LATCH_LSB ((__u16)(0x00))
163#define DIVISOR_LATCH_MSB ((__u16)(0x01))
164
165#define CLK_MULTI_REGISTER ((__u16)(0x02))
166#define CLK_START_VALUE_REGISTER ((__u16)(0x03))
167
168#define SERIAL_LCR_DLAB ((__u16)(0x0080))
169
170/*
171 * URB POOL related defines
172 */
173#define NUM_URBS 16 /* URB Count */
174#define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
175
176
177static struct usb_device_id moschip_port_id_table[] = {
178 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7840)},
179 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7820)},
11e1abb4
DL
180 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4)},
181 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2)},
e9b8cffa 182 {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2324)},
9d498bea 183 {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2322)},
3f542974
PS
184 {} /* terminating entry */
185};
186
187static __devinitdata struct usb_device_id moschip_id_table_combined[] = {
188 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7840)},
189 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7820)},
11e1abb4
DL
190 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4)},
191 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2)},
e9b8cffa 192 {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2324)},
9d498bea 193 {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2322)},
3f542974
PS
194 {} /* terminating entry */
195};
196
197MODULE_DEVICE_TABLE(usb, moschip_id_table_combined);
198
199/* This structure holds all of the local port information */
200
201struct moschip_port {
202 int port_num; /*Actual port number in the device(1,2,etc) */
203 struct urb *write_urb; /* write URB for this port */
204 struct urb *read_urb; /* read URB for this port */
0de9a702 205 struct urb *int_urb;
3f542974
PS
206 __u8 shadowLCR; /* last LCR value received */
207 __u8 shadowMCR; /* last MCR value received */
208 char open;
0de9a702
ON
209 char open_ports;
210 char zombie;
3f542974
PS
211 wait_queue_head_t wait_chase; /* for handling sleeping while waiting for chase to finish */
212 wait_queue_head_t delta_msr_wait; /* for handling sleeping while waiting for msr change to happen */
213 int delta_msr_cond;
214 struct async_icount icount;
215 struct usb_serial_port *port; /* loop back to the owner of this object */
216
880af9db 217 /* Offsets */
3f542974
PS
218 __u8 SpRegOffset;
219 __u8 ControlRegOffset;
220 __u8 DcrRegOffset;
880af9db 221 /* for processing control URBS in interrupt context */
3f542974 222 struct urb *control_urb;
0de9a702 223 struct usb_ctrlrequest *dr;
3f542974
PS
224 char *ctrl_buf;
225 int MsrLsr;
226
0de9a702 227 spinlock_t pool_lock;
3f542974 228 struct urb *write_urb_pool[NUM_URBS];
0de9a702 229 char busy[NUM_URBS];
50de36f7 230 bool read_urb_busy;
3f542974
PS
231};
232
233
234static int debug;
3f542974
PS
235
236/*
237 * mos7840_set_reg_sync
238 * To set the Control register by calling usb_fill_control_urb function
239 * by passing usb_sndctrlpipe function as parameter.
240 */
241
242static int mos7840_set_reg_sync(struct usb_serial_port *port, __u16 reg,
243 __u16 val)
244{
245 struct usb_device *dev = port->serial->dev;
246 val = val & 0x00ff;
84fe6e79 247 dbg("mos7840_set_reg_sync offset is %x, value %x", reg, val);
3f542974
PS
248
249 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
250 MCS_WR_RTYPE, val, reg, NULL, 0,
251 MOS_WDR_TIMEOUT);
252}
253
254/*
255 * mos7840_get_reg_sync
256 * To set the Uart register by calling usb_fill_control_urb function by
257 * passing usb_rcvctrlpipe function as parameter.
258 */
259
260static int mos7840_get_reg_sync(struct usb_serial_port *port, __u16 reg,
880af9db 261 __u16 *val)
3f542974
PS
262{
263 struct usb_device *dev = port->serial->dev;
264 int ret = 0;
265
266 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
267 MCS_RD_RTYPE, 0, reg, val, VENDOR_READ_LENGTH,
268 MOS_WDR_TIMEOUT);
84fe6e79 269 dbg("mos7840_get_reg_sync offset is %x, return val %x", reg, *val);
3f542974
PS
270 *val = (*val) & 0x00ff;
271 return ret;
272}
273
274/*
275 * mos7840_set_uart_reg
276 * To set the Uart register by calling usb_fill_control_urb function by
277 * passing usb_sndctrlpipe function as parameter.
278 */
279
280static int mos7840_set_uart_reg(struct usb_serial_port *port, __u16 reg,
281 __u16 val)
282{
283
284 struct usb_device *dev = port->serial->dev;
285 val = val & 0x00ff;
880af9db
AC
286 /* For the UART control registers, the application number need
287 to be Or'ed */
0de9a702 288 if (port->serial->num_ports == 4) {
880af9db
AC
289 val |= (((__u16) port->number -
290 (__u16) (port->serial->minor)) + 1) << 8;
84fe6e79 291 dbg("mos7840_set_uart_reg application number is %x", val);
3f542974
PS
292 } else {
293 if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
880af9db 294 val |= (((__u16) port->number -
3f542974 295 (__u16) (port->serial->minor)) + 1) << 8;
84fe6e79 296 dbg("mos7840_set_uart_reg application number is %x",
3f542974
PS
297 val);
298 } else {
299 val |=
300 (((__u16) port->number -
301 (__u16) (port->serial->minor)) + 2) << 8;
84fe6e79 302 dbg("mos7840_set_uart_reg application number is %x",
3f542974
PS
303 val);
304 }
305 }
306 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
307 MCS_WR_RTYPE, val, reg, NULL, 0,
308 MOS_WDR_TIMEOUT);
309
310}
311
312/*
313 * mos7840_get_uart_reg
314 * To set the Control register by calling usb_fill_control_urb function
315 * by passing usb_rcvctrlpipe function as parameter.
316 */
317static int mos7840_get_uart_reg(struct usb_serial_port *port, __u16 reg,
880af9db 318 __u16 *val)
3f542974
PS
319{
320 struct usb_device *dev = port->serial->dev;
321 int ret = 0;
322 __u16 Wval;
323
84fe6e79 324 /* dbg("application number is %4x",
880af9db
AC
325 (((__u16)port->number - (__u16)(port->serial->minor))+1)<<8); */
326 /* Wval is same as application number */
0de9a702 327 if (port->serial->num_ports == 4) {
3f542974
PS
328 Wval =
329 (((__u16) port->number - (__u16) (port->serial->minor)) +
330 1) << 8;
84fe6e79 331 dbg("mos7840_get_uart_reg application number is %x", Wval);
3f542974
PS
332 } else {
333 if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
880af9db 334 Wval = (((__u16) port->number -
3f542974 335 (__u16) (port->serial->minor)) + 1) << 8;
84fe6e79 336 dbg("mos7840_get_uart_reg application number is %x",
3f542974
PS
337 Wval);
338 } else {
880af9db 339 Wval = (((__u16) port->number -
3f542974 340 (__u16) (port->serial->minor)) + 2) << 8;
84fe6e79 341 dbg("mos7840_get_uart_reg application number is %x",
3f542974
PS
342 Wval);
343 }
344 }
345 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
346 MCS_RD_RTYPE, Wval, reg, val, VENDOR_READ_LENGTH,
347 MOS_WDR_TIMEOUT);
348 *val = (*val) & 0x00ff;
349 return ret;
350}
351
352static void mos7840_dump_serial_port(struct moschip_port *mos7840_port)
353{
354
84fe6e79
TC
355 dbg("***************************************");
356 dbg("SpRegOffset is %2x", mos7840_port->SpRegOffset);
357 dbg("ControlRegOffset is %2x", mos7840_port->ControlRegOffset);
358 dbg("DCRRegOffset is %2x", mos7840_port->DcrRegOffset);
359 dbg("***************************************");
3f542974
PS
360
361}
362
363/************************************************************************/
364/************************************************************************/
365/* I N T E R F A C E F U N C T I O N S */
366/* I N T E R F A C E F U N C T I O N S */
367/************************************************************************/
368/************************************************************************/
369
370static inline void mos7840_set_port_private(struct usb_serial_port *port,
371 struct moschip_port *data)
372{
373 usb_set_serial_port_data(port, (void *)data);
374}
375
376static inline struct moschip_port *mos7840_get_port_private(struct
377 usb_serial_port
378 *port)
379{
380 return (struct moschip_port *)usb_get_serial_port_data(port);
381}
382
0de9a702 383static void mos7840_handle_new_msr(struct moschip_port *port, __u8 new_msr)
3f542974
PS
384{
385 struct moschip_port *mos7840_port;
386 struct async_icount *icount;
387 mos7840_port = port;
388 icount = &mos7840_port->icount;
389 if (new_msr &
390 (MOS_MSR_DELTA_CTS | MOS_MSR_DELTA_DSR | MOS_MSR_DELTA_RI |
391 MOS_MSR_DELTA_CD)) {
392 icount = &mos7840_port->icount;
393
394 /* update input line counters */
395 if (new_msr & MOS_MSR_DELTA_CTS) {
396 icount->cts++;
0de9a702 397 smp_wmb();
3f542974
PS
398 }
399 if (new_msr & MOS_MSR_DELTA_DSR) {
400 icount->dsr++;
0de9a702 401 smp_wmb();
3f542974
PS
402 }
403 if (new_msr & MOS_MSR_DELTA_CD) {
404 icount->dcd++;
0de9a702 405 smp_wmb();
3f542974
PS
406 }
407 if (new_msr & MOS_MSR_DELTA_RI) {
408 icount->rng++;
0de9a702 409 smp_wmb();
3f542974
PS
410 }
411 }
3f542974
PS
412}
413
0de9a702 414static void mos7840_handle_new_lsr(struct moschip_port *port, __u8 new_lsr)
3f542974
PS
415{
416 struct async_icount *icount;
417
441b62c1 418 dbg("%s - %02x", __func__, new_lsr);
3f542974
PS
419
420 if (new_lsr & SERIAL_LSR_BI) {
880af9db
AC
421 /*
422 * Parity and Framing errors only count if they
423 * occur exclusive of a break being
424 * received.
425 */
3f542974
PS
426 new_lsr &= (__u8) (SERIAL_LSR_OE | SERIAL_LSR_BI);
427 }
428
429 /* update input line counters */
430 icount = &port->icount;
431 if (new_lsr & SERIAL_LSR_BI) {
432 icount->brk++;
0de9a702 433 smp_wmb();
3f542974
PS
434 }
435 if (new_lsr & SERIAL_LSR_OE) {
436 icount->overrun++;
0de9a702 437 smp_wmb();
3f542974
PS
438 }
439 if (new_lsr & SERIAL_LSR_PE) {
440 icount->parity++;
0de9a702 441 smp_wmb();
3f542974
PS
442 }
443 if (new_lsr & SERIAL_LSR_FE) {
444 icount->frame++;
0de9a702 445 smp_wmb();
3f542974 446 }
3f542974
PS
447}
448
449/************************************************************************/
450/************************************************************************/
451/* U S B C A L L B A C K F U N C T I O N S */
452/* U S B C A L L B A C K F U N C T I O N S */
453/************************************************************************/
454/************************************************************************/
455
7d12e780 456static void mos7840_control_callback(struct urb *urb)
3f542974
PS
457{
458 unsigned char *data;
459 struct moschip_port *mos7840_port;
460 __u8 regval = 0x0;
0de9a702 461 int result = 0;
0643c724 462 int status = urb->status;
3f542974 463
cdc97792 464 mos7840_port = urb->context;
0de9a702 465
0643c724 466 switch (status) {
3f542974
PS
467 case 0:
468 /* success */
469 break;
470 case -ECONNRESET:
471 case -ENOENT:
472 case -ESHUTDOWN:
473 /* this urb is terminated, clean up */
441b62c1 474 dbg("%s - urb shutting down with status: %d", __func__,
0643c724 475 status);
3f542974
PS
476 return;
477 default:
441b62c1 478 dbg("%s - nonzero urb status received: %d", __func__,
0643c724 479 status);
3f542974
PS
480 goto exit;
481 }
482
84fe6e79
TC
483 dbg("%s urb buffer size is %d", __func__, urb->actual_length);
484 dbg("%s mos7840_port->MsrLsr is %d port %d", __func__,
3f542974
PS
485 mos7840_port->MsrLsr, mos7840_port->port_num);
486 data = urb->transfer_buffer;
487 regval = (__u8) data[0];
84fe6e79 488 dbg("%s data is %x", __func__, regval);
3f542974
PS
489 if (mos7840_port->MsrLsr == 0)
490 mos7840_handle_new_msr(mos7840_port, regval);
491 else if (mos7840_port->MsrLsr == 1)
492 mos7840_handle_new_lsr(mos7840_port, regval);
493
0de9a702
ON
494exit:
495 spin_lock(&mos7840_port->pool_lock);
496 if (!mos7840_port->zombie)
497 result = usb_submit_urb(mos7840_port->int_urb, GFP_ATOMIC);
498 spin_unlock(&mos7840_port->pool_lock);
499 if (result) {
500 dev_err(&urb->dev->dev,
501 "%s - Error %d submitting interrupt urb\n",
441b62c1 502 __func__, result);
0de9a702 503 }
3f542974
PS
504}
505
506static int mos7840_get_reg(struct moschip_port *mcs, __u16 Wval, __u16 reg,
880af9db 507 __u16 *val)
3f542974
PS
508{
509 struct usb_device *dev = mcs->port->serial->dev;
0de9a702
ON
510 struct usb_ctrlrequest *dr = mcs->dr;
511 unsigned char *buffer = mcs->ctrl_buf;
512 int ret;
3f542974 513
3f542974
PS
514 dr->bRequestType = MCS_RD_RTYPE;
515 dr->bRequest = MCS_RDREQ;
880af9db 516 dr->wValue = cpu_to_le16(Wval); /* 0 */
3f542974
PS
517 dr->wIndex = cpu_to_le16(reg);
518 dr->wLength = cpu_to_le16(2);
519
520 usb_fill_control_urb(mcs->control_urb, dev, usb_rcvctrlpipe(dev, 0),
521 (unsigned char *)dr, buffer, 2,
522 mos7840_control_callback, mcs);
523 mcs->control_urb->transfer_buffer_length = 2;
524 ret = usb_submit_urb(mcs->control_urb, GFP_ATOMIC);
525 return ret;
526}
527
528/*****************************************************************************
529 * mos7840_interrupt_callback
530 * this is the callback function for when we have received data on the
531 * interrupt endpoint.
532 *****************************************************************************/
533
7d12e780 534static void mos7840_interrupt_callback(struct urb *urb)
3f542974
PS
535{
536 int result;
537 int length;
538 struct moschip_port *mos7840_port;
539 struct usb_serial *serial;
540 __u16 Data;
541 unsigned char *data;
542 __u8 sp[5], st;
0de9a702
ON
543 int i, rv = 0;
544 __u16 wval, wreg = 0;
0643c724 545 int status = urb->status;
3f542974 546
84fe6e79 547 dbg("%s", " : Entering");
3f542974 548
0643c724 549 switch (status) {
3f542974
PS
550 case 0:
551 /* success */
552 break;
553 case -ECONNRESET:
554 case -ENOENT:
555 case -ESHUTDOWN:
556 /* this urb is terminated, clean up */
441b62c1 557 dbg("%s - urb shutting down with status: %d", __func__,
0643c724 558 status);
3f542974
PS
559 return;
560 default:
441b62c1 561 dbg("%s - nonzero urb status received: %d", __func__,
0643c724 562 status);
3f542974
PS
563 goto exit;
564 }
565
566 length = urb->actual_length;
567 data = urb->transfer_buffer;
568
cdc97792 569 serial = urb->context;
3f542974
PS
570
571 /* Moschip get 5 bytes
572 * Byte 1 IIR Port 1 (port.number is 0)
573 * Byte 2 IIR Port 2 (port.number is 1)
574 * Byte 3 IIR Port 3 (port.number is 2)
575 * Byte 4 IIR Port 4 (port.number is 3)
576 * Byte 5 FIFO status for both */
577
578 if (length && length > 5) {
84fe6e79 579 dbg("%s", "Wrong data !!!");
3f542974
PS
580 return;
581 }
582
583 sp[0] = (__u8) data[0];
584 sp[1] = (__u8) data[1];
585 sp[2] = (__u8) data[2];
586 sp[3] = (__u8) data[3];
587 st = (__u8) data[4];
588
589 for (i = 0; i < serial->num_ports; i++) {
590 mos7840_port = mos7840_get_port_private(serial->port[i]);
591 wval =
592 (((__u16) serial->port[i]->number -
593 (__u16) (serial->minor)) + 1) << 8;
594 if (mos7840_port->open) {
595 if (sp[i] & 0x01) {
84fe6e79 596 dbg("SP%d No Interrupt !!!", i);
3f542974
PS
597 } else {
598 switch (sp[i] & 0x0f) {
599 case SERIAL_IIR_RLS:
600 dbg("Serial Port %d: Receiver status error or ", i);
84fe6e79 601 dbg("address bit detected in 9-bit mode");
3f542974 602 mos7840_port->MsrLsr = 1;
0de9a702 603 wreg = LINE_STATUS_REGISTER;
3f542974
PS
604 break;
605 case SERIAL_IIR_MS:
84fe6e79 606 dbg("Serial Port %d: Modem status change", i);
3f542974 607 mos7840_port->MsrLsr = 0;
0de9a702 608 wreg = MODEM_STATUS_REGISTER;
3f542974
PS
609 break;
610 }
0de9a702
ON
611 spin_lock(&mos7840_port->pool_lock);
612 if (!mos7840_port->zombie) {
613 rv = mos7840_get_reg(mos7840_port, wval, wreg, &Data);
614 } else {
615 spin_unlock(&mos7840_port->pool_lock);
616 return;
617 }
618 spin_unlock(&mos7840_port->pool_lock);
3f542974
PS
619 }
620 }
621 }
880af9db
AC
622 if (!(rv < 0))
623 /* the completion handler for the control urb will resubmit */
0de9a702
ON
624 return;
625exit:
3f542974
PS
626 result = usb_submit_urb(urb, GFP_ATOMIC);
627 if (result) {
628 dev_err(&urb->dev->dev,
629 "%s - Error %d submitting interrupt urb\n",
441b62c1 630 __func__, result);
3f542974 631 }
3f542974
PS
632}
633
634static int mos7840_port_paranoia_check(struct usb_serial_port *port,
635 const char *function)
636{
637 if (!port) {
638 dbg("%s - port == NULL", function);
639 return -1;
640 }
641 if (!port->serial) {
642 dbg("%s - port->serial == NULL", function);
643 return -1;
644 }
645
646 return 0;
647}
648
649/* Inline functions to check the sanity of a pointer that is passed to us */
650static int mos7840_serial_paranoia_check(struct usb_serial *serial,
651 const char *function)
652{
653 if (!serial) {
654 dbg("%s - serial == NULL", function);
655 return -1;
656 }
657 if (!serial->type) {
658 dbg("%s - serial->type == NULL!", function);
659 return -1;
660 }
661
662 return 0;
663}
664
665static struct usb_serial *mos7840_get_usb_serial(struct usb_serial_port *port,
666 const char *function)
667{
668 /* if no port was specified, or it fails a paranoia check */
669 if (!port ||
670 mos7840_port_paranoia_check(port, function) ||
671 mos7840_serial_paranoia_check(port->serial, function)) {
880af9db
AC
672 /* then say that we don't have a valid usb_serial thing,
673 * which will end up genrating -ENODEV return values */
3f542974
PS
674 return NULL;
675 }
676
677 return port->serial;
678}
679
680/*****************************************************************************
681 * mos7840_bulk_in_callback
682 * this is the callback function for when we have received data on the
683 * bulk in endpoint.
684 *****************************************************************************/
685
7d12e780 686static void mos7840_bulk_in_callback(struct urb *urb)
3f542974 687{
0643c724 688 int retval;
3f542974
PS
689 unsigned char *data;
690 struct usb_serial *serial;
691 struct usb_serial_port *port;
692 struct moschip_port *mos7840_port;
693 struct tty_struct *tty;
0643c724 694 int status = urb->status;
3f542974 695
cdc97792 696 mos7840_port = urb->context;
3f542974 697 if (!mos7840_port) {
84fe6e79 698 dbg("%s", "NULL mos7840_port pointer");
50de36f7
GKH
699 mos7840_port->read_urb_busy = false;
700 return;
701 }
702
703 if (status) {
704 dbg("nonzero read bulk status received: %d", status);
705 mos7840_port->read_urb_busy = false;
3f542974
PS
706 return;
707 }
708
709 port = (struct usb_serial_port *)mos7840_port->port;
441b62c1 710 if (mos7840_port_paranoia_check(port, __func__)) {
84fe6e79 711 dbg("%s", "Port Paranoia failed");
50de36f7 712 mos7840_port->read_urb_busy = false;
3f542974
PS
713 return;
714 }
715
441b62c1 716 serial = mos7840_get_usb_serial(port, __func__);
3f542974 717 if (!serial) {
84fe6e79 718 dbg("%s", "Bad serial pointer");
50de36f7 719 mos7840_port->read_urb_busy = false;
3f542974
PS
720 return;
721 }
722
84fe6e79 723 dbg("%s", "Entering... ");
3f542974
PS
724
725 data = urb->transfer_buffer;
726
84fe6e79 727 dbg("%s", "Entering ...........");
3f542974
PS
728
729 if (urb->actual_length) {
4a90f09b 730 tty = tty_port_tty_get(&mos7840_port->port->port);
3f542974
PS
731 if (tty) {
732 tty_buffer_request_room(tty, urb->actual_length);
733 tty_insert_flip_string(tty, data, urb->actual_length);
84fe6e79 734 dbg(" %s ", data);
3f542974 735 tty_flip_buffer_push(tty);
4a90f09b 736 tty_kref_put(tty);
3f542974
PS
737 }
738 mos7840_port->icount.rx += urb->actual_length;
0de9a702 739 smp_wmb();
84fe6e79 740 dbg("mos7840_port->icount.rx is %d:",
3f542974
PS
741 mos7840_port->icount.rx);
742 }
743
744 if (!mos7840_port->read_urb) {
84fe6e79 745 dbg("%s", "URB KILLED !!!");
50de36f7 746 mos7840_port->read_urb_busy = false;
3f542974
PS
747 return;
748 }
749
3f542974 750
0de9a702 751 mos7840_port->read_urb->dev = serial->dev;
3f542974 752
50de36f7 753 mos7840_port->read_urb_busy = true;
0643c724 754 retval = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
0de9a702 755
0643c724 756 if (retval) {
50de36f7
GKH
757 dbg("usb_submit_urb(read bulk) failed, retval = %d", retval);
758 mos7840_port->read_urb_busy = false;
3f542974
PS
759 }
760}
761
762/*****************************************************************************
763 * mos7840_bulk_out_data_callback
880af9db
AC
764 * this is the callback function for when we have finished sending
765 * serial data on the bulk out endpoint.
3f542974
PS
766 *****************************************************************************/
767
7d12e780 768static void mos7840_bulk_out_data_callback(struct urb *urb)
3f542974
PS
769{
770 struct moschip_port *mos7840_port;
771 struct tty_struct *tty;
0643c724 772 int status = urb->status;
0de9a702
ON
773 int i;
774
cdc97792 775 mos7840_port = urb->context;
0de9a702
ON
776 spin_lock(&mos7840_port->pool_lock);
777 for (i = 0; i < NUM_URBS; i++) {
778 if (urb == mos7840_port->write_urb_pool[i]) {
779 mos7840_port->busy[i] = 0;
780 break;
781 }
782 }
783 spin_unlock(&mos7840_port->pool_lock);
784
0643c724 785 if (status) {
84fe6e79 786 dbg("nonzero write bulk status received:%d", status);
3f542974
PS
787 return;
788 }
789
441b62c1 790 if (mos7840_port_paranoia_check(mos7840_port->port, __func__)) {
84fe6e79 791 dbg("%s", "Port Paranoia failed");
3f542974
PS
792 return;
793 }
794
84fe6e79 795 dbg("%s", "Entering .........");
3f542974 796
4a90f09b 797 tty = tty_port_tty_get(&mos7840_port->port->port);
b963a844
JS
798 if (tty && mos7840_port->open)
799 tty_wakeup(tty);
4a90f09b 800 tty_kref_put(tty);
3f542974
PS
801
802}
803
804/************************************************************************/
805/* D R I V E R T T Y I N T E R F A C E F U N C T I O N S */
806/************************************************************************/
807#ifdef MCSSerialProbe
808static int mos7840_serial_probe(struct usb_serial *serial,
809 const struct usb_device_id *id)
810{
811
812 /*need to implement the mode_reg reading and updating\
813 structures usb_serial_ device_type\
814 (i.e num_ports, num_bulkin,bulkout etc) */
815 /* Also we can update the changes attach */
816 return 1;
817}
818#endif
819
820/*****************************************************************************
821 * mos7840_open
822 * this function is called by the tty driver when a port is opened
823 * If successful, we return 0
824 * Otherwise we return a negative error number.
825 *****************************************************************************/
826
a509a7e4 827static int mos7840_open(struct tty_struct *tty, struct usb_serial_port *port)
3f542974
PS
828{
829 int response;
830 int j;
831 struct usb_serial *serial;
832 struct urb *urb;
833 __u16 Data;
834 int status;
835 struct moschip_port *mos7840_port;
0de9a702 836 struct moschip_port *port0;
3f542974 837
84fe6e79
TC
838 dbg ("%s enter", __func__);
839
441b62c1 840 if (mos7840_port_paranoia_check(port, __func__)) {
84fe6e79 841 dbg("%s", "Port Paranoia failed");
3f542974
PS
842 return -ENODEV;
843 }
844
3f542974
PS
845 serial = port->serial;
846
441b62c1 847 if (mos7840_serial_paranoia_check(serial, __func__)) {
84fe6e79 848 dbg("%s", "Serial Paranoia failed");
3f542974
PS
849 return -ENODEV;
850 }
851
852 mos7840_port = mos7840_get_port_private(port);
0de9a702 853 port0 = mos7840_get_port_private(serial->port[0]);
3f542974 854
0de9a702 855 if (mos7840_port == NULL || port0 == NULL)
3f542974
PS
856 return -ENODEV;
857
858 usb_clear_halt(serial->dev, port->write_urb->pipe);
859 usb_clear_halt(serial->dev, port->read_urb->pipe);
0de9a702 860 port0->open_ports++;
3f542974
PS
861
862 /* Initialising the write urb pool */
863 for (j = 0; j < NUM_URBS; ++j) {
0de9a702 864 urb = usb_alloc_urb(0, GFP_KERNEL);
3f542974
PS
865 mos7840_port->write_urb_pool[j] = urb;
866
867 if (urb == NULL) {
194343d9 868 dev_err(&port->dev, "No more urbs???\n");
3f542974
PS
869 continue;
870 }
871
880af9db
AC
872 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
873 GFP_KERNEL);
3f542974 874 if (!urb->transfer_buffer) {
0de9a702
ON
875 usb_free_urb(urb);
876 mos7840_port->write_urb_pool[j] = NULL;
194343d9
GKH
877 dev_err(&port->dev,
878 "%s-out of memory for urb buffers.\n",
879 __func__);
3f542974
PS
880 continue;
881 }
882 }
883
884/*****************************************************************************
885 * Initialize MCS7840 -- Write Init values to corresponding Registers
886 *
887 * Register Index
888 * 1 : IER
889 * 2 : FCR
890 * 3 : LCR
891 * 4 : MCR
892 *
893 * 0x08 : SP1/2 Control Reg
894 *****************************************************************************/
895
880af9db 896 /* NEED to check the following Block */
3f542974 897
3f542974
PS
898 Data = 0x0;
899 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
900 if (status < 0) {
84fe6e79 901 dbg("Reading Spreg failed");
3f542974
PS
902 return -1;
903 }
904 Data |= 0x80;
905 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
906 if (status < 0) {
84fe6e79 907 dbg("writing Spreg failed");
3f542974
PS
908 return -1;
909 }
910
911 Data &= ~0x80;
912 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
913 if (status < 0) {
84fe6e79 914 dbg("writing Spreg failed");
3f542974
PS
915 return -1;
916 }
880af9db 917 /* End of block to be checked */
3f542974 918
3f542974 919 Data = 0x0;
880af9db
AC
920 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
921 &Data);
3f542974 922 if (status < 0) {
84fe6e79 923 dbg("Reading Controlreg failed");
3f542974
PS
924 return -1;
925 }
880af9db
AC
926 Data |= 0x08; /* Driver done bit */
927 Data |= 0x20; /* rx_disable */
928 status = mos7840_set_reg_sync(port,
929 mos7840_port->ControlRegOffset, Data);
3f542974 930 if (status < 0) {
84fe6e79 931 dbg("writing Controlreg failed");
3f542974
PS
932 return -1;
933 }
880af9db
AC
934 /* do register settings here */
935 /* Set all regs to the device default values. */
936 /***********************************
937 * First Disable all interrupts.
938 ***********************************/
3f542974 939 Data = 0x00;
3f542974
PS
940 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
941 if (status < 0) {
84fe6e79 942 dbg("disabling interrupts failed");
3f542974
PS
943 return -1;
944 }
880af9db 945 /* Set FIFO_CONTROL_REGISTER to the default value */
3f542974 946 Data = 0x00;
3f542974
PS
947 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
948 if (status < 0) {
84fe6e79 949 dbg("Writing FIFO_CONTROL_REGISTER failed");
3f542974
PS
950 return -1;
951 }
952
953 Data = 0xcf;
3f542974
PS
954 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
955 if (status < 0) {
84fe6e79 956 dbg("Writing FIFO_CONTROL_REGISTER failed");
3f542974
PS
957 return -1;
958 }
959
960 Data = 0x03;
3f542974
PS
961 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
962 mos7840_port->shadowLCR = Data;
963
964 Data = 0x0b;
3f542974
PS
965 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
966 mos7840_port->shadowMCR = Data;
967
968 Data = 0x00;
3f542974
PS
969 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
970 mos7840_port->shadowLCR = Data;
971
880af9db 972 Data |= SERIAL_LCR_DLAB; /* data latch enable in LCR 0x80 */
3f542974
PS
973 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
974
975 Data = 0x0c;
3f542974
PS
976 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
977
978 Data = 0x0;
3f542974
PS
979 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
980
981 Data = 0x00;
3f542974
PS
982 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
983
984 Data = Data & ~SERIAL_LCR_DLAB;
3f542974
PS
985 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
986 mos7840_port->shadowLCR = Data;
987
880af9db 988 /* clearing Bulkin and Bulkout Fifo */
3f542974 989 Data = 0x0;
3f542974
PS
990 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
991
992 Data = Data | 0x0c;
3f542974
PS
993 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
994
995 Data = Data & ~0x0c;
3f542974 996 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
880af9db 997 /* Finally enable all interrupts */
3f542974 998 Data = 0x0c;
3f542974
PS
999 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1000
880af9db 1001 /* clearing rx_disable */
3f542974 1002 Data = 0x0;
880af9db
AC
1003 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1004 &Data);
3f542974 1005 Data = Data & ~0x20;
880af9db
AC
1006 status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1007 Data);
3f542974 1008
880af9db 1009 /* rx_negate */
3f542974 1010 Data = 0x0;
880af9db
AC
1011 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1012 &Data);
3f542974 1013 Data = Data | 0x10;
880af9db
AC
1014 status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1015 Data);
3f542974 1016
880af9db
AC
1017 /* Check to see if we've set up our endpoint info yet *
1018 * (can't set it up in mos7840_startup as the structures *
1019 * were not set up at that time.) */
0de9a702 1020 if (port0->open_ports == 1) {
3f542974 1021 if (serial->port[0]->interrupt_in_buffer == NULL) {
3f542974 1022 /* set up interrupt urb */
3f542974 1023 usb_fill_int_urb(serial->port[0]->interrupt_in_urb,
880af9db
AC
1024 serial->dev,
1025 usb_rcvintpipe(serial->dev,
1026 serial->port[0]->interrupt_in_endpointAddress),
1027 serial->port[0]->interrupt_in_buffer,
1028 serial->port[0]->interrupt_in_urb->
1029 transfer_buffer_length,
1030 mos7840_interrupt_callback,
1031 serial,
1032 serial->port[0]->interrupt_in_urb->interval);
3f542974
PS
1033
1034 /* start interrupt read for mos7840 *
1035 * will continue as long as mos7840 is connected */
1036
1037 response =
1038 usb_submit_urb(serial->port[0]->interrupt_in_urb,
1039 GFP_KERNEL);
1040 if (response) {
194343d9
GKH
1041 dev_err(&port->dev, "%s - Error %d submitting "
1042 "interrupt urb\n", __func__, response);
3f542974
PS
1043 }
1044
1045 }
1046
1047 }
1048
1049 /* see if we've set up our endpoint info yet *
1050 * (can't set it up in mos7840_startup as the *
1051 * structures were not set up at that time.) */
1052
84fe6e79
TC
1053 dbg("port number is %d", port->number);
1054 dbg("serial number is %d", port->serial->minor);
1055 dbg("Bulkin endpoint is %d", port->bulk_in_endpointAddress);
1056 dbg("BulkOut endpoint is %d", port->bulk_out_endpointAddress);
1057 dbg("Interrupt endpoint is %d", port->interrupt_in_endpointAddress);
1058 dbg("port's number in the device is %d", mos7840_port->port_num);
3f542974
PS
1059 mos7840_port->read_urb = port->read_urb;
1060
1061 /* set up our bulk in urb */
1062
1063 usb_fill_bulk_urb(mos7840_port->read_urb,
1064 serial->dev,
1065 usb_rcvbulkpipe(serial->dev,
1066 port->bulk_in_endpointAddress),
1067 port->bulk_in_buffer,
1068 mos7840_port->read_urb->transfer_buffer_length,
1069 mos7840_bulk_in_callback, mos7840_port);
1070
84fe6e79 1071 dbg("mos7840_open: bulkin endpoint is %d",
3f542974 1072 port->bulk_in_endpointAddress);
50de36f7 1073 mos7840_port->read_urb_busy = true;
3f542974
PS
1074 response = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
1075 if (response) {
194343d9
GKH
1076 dev_err(&port->dev, "%s - Error %d submitting control urb\n",
1077 __func__, response);
50de36f7 1078 mos7840_port->read_urb_busy = false;
3f542974
PS
1079 }
1080
1081 /* initialize our wait queues */
1082 init_waitqueue_head(&mos7840_port->wait_chase);
1083 init_waitqueue_head(&mos7840_port->delta_msr_wait);
1084
1085 /* initialize our icount structure */
1086 memset(&(mos7840_port->icount), 0x00, sizeof(mos7840_port->icount));
1087
1088 /* initialize our port settings */
880af9db
AC
1089 /* Must set to enable ints! */
1090 mos7840_port->shadowMCR = MCR_MASTER_IE;
3f542974
PS
1091 /* send a open port command */
1092 mos7840_port->open = 1;
880af9db 1093 /* mos7840_change_port_settings(mos7840_port,old_termios); */
3f542974
PS
1094 mos7840_port->icount.tx = 0;
1095 mos7840_port->icount.rx = 0;
1096
84fe6e79 1097 dbg("usb_serial serial:%p mos7840_port:%p\n usb_serial_port port:%p",
880af9db 1098 serial, mos7840_port, port);
3f542974 1099
84fe6e79
TC
1100 dbg ("%s leave", __func__);
1101
3f542974
PS
1102 return 0;
1103
1104}
1105
1106/*****************************************************************************
1107 * mos7840_chars_in_buffer
1108 * this function is called by the tty driver when it wants to know how many
1109 * bytes of data we currently have outstanding in the port (data that has
1110 * been written, but hasn't made it out the port yet)
1111 * If successful, we return the number of bytes left to be written in the
1112 * system,
95da310e 1113 * Otherwise we return zero.
3f542974
PS
1114 *****************************************************************************/
1115
95da310e 1116static int mos7840_chars_in_buffer(struct tty_struct *tty)
3f542974 1117{
95da310e 1118 struct usb_serial_port *port = tty->driver_data;
3f542974
PS
1119 int i;
1120 int chars = 0;
0de9a702 1121 unsigned long flags;
3f542974
PS
1122 struct moschip_port *mos7840_port;
1123
84fe6e79 1124 dbg("%s", " mos7840_chars_in_buffer:entering ...........");
3f542974 1125
441b62c1 1126 if (mos7840_port_paranoia_check(port, __func__)) {
84fe6e79 1127 dbg("%s", "Invalid port");
95da310e 1128 return 0;
3f542974
PS
1129 }
1130
1131 mos7840_port = mos7840_get_port_private(port);
1132 if (mos7840_port == NULL) {
84fe6e79 1133 dbg("%s", "mos7840_break:leaving ...........");
95da310e 1134 return 0;
3f542974
PS
1135 }
1136
880af9db 1137 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
95da310e
AC
1138 for (i = 0; i < NUM_URBS; ++i)
1139 if (mos7840_port->busy[i])
3f542974 1140 chars += URB_TRANSFER_BUFFER_SIZE;
880af9db 1141 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
441b62c1 1142 dbg("%s - returns %d", __func__, chars);
0de9a702 1143 return chars;
3f542974
PS
1144
1145}
1146
3f542974
PS
1147/*****************************************************************************
1148 * mos7840_close
1149 * this function is called by the tty driver when a port is closed
1150 *****************************************************************************/
1151
335f8514 1152static void mos7840_close(struct usb_serial_port *port)
3f542974
PS
1153{
1154 struct usb_serial *serial;
1155 struct moschip_port *mos7840_port;
0de9a702 1156 struct moschip_port *port0;
3f542974
PS
1157 int j;
1158 __u16 Data;
1159
84fe6e79 1160 dbg("%s", "mos7840_close:entering...");
3f542974 1161
441b62c1 1162 if (mos7840_port_paranoia_check(port, __func__)) {
84fe6e79 1163 dbg("%s", "Port Paranoia failed");
3f542974
PS
1164 return;
1165 }
1166
441b62c1 1167 serial = mos7840_get_usb_serial(port, __func__);
3f542974 1168 if (!serial) {
84fe6e79 1169 dbg("%s", "Serial Paranoia failed");
3f542974
PS
1170 return;
1171 }
1172
1173 mos7840_port = mos7840_get_port_private(port);
0de9a702 1174 port0 = mos7840_get_port_private(serial->port[0]);
3f542974 1175
0de9a702 1176 if (mos7840_port == NULL || port0 == NULL)
3f542974 1177 return;
3f542974
PS
1178
1179 for (j = 0; j < NUM_URBS; ++j)
1180 usb_kill_urb(mos7840_port->write_urb_pool[j]);
1181
1182 /* Freeing Write URBs */
1183 for (j = 0; j < NUM_URBS; ++j) {
1184 if (mos7840_port->write_urb_pool[j]) {
1185 if (mos7840_port->write_urb_pool[j]->transfer_buffer)
1186 kfree(mos7840_port->write_urb_pool[j]->
1187 transfer_buffer);
1188
1189 usb_free_urb(mos7840_port->write_urb_pool[j]);
1190 }
1191 }
1192
3f542974
PS
1193 /* While closing port, shutdown all bulk read, write *
1194 * and interrupt read if they exists */
1195 if (serial->dev) {
3f542974 1196 if (mos7840_port->write_urb) {
84fe6e79 1197 dbg("%s", "Shutdown bulk write");
3f542974
PS
1198 usb_kill_urb(mos7840_port->write_urb);
1199 }
3f542974 1200 if (mos7840_port->read_urb) {
84fe6e79 1201 dbg("%s", "Shutdown bulk read");
3f542974 1202 usb_kill_urb(mos7840_port->read_urb);
50de36f7 1203 mos7840_port->read_urb_busy = false;
3f542974
PS
1204 }
1205 if ((&mos7840_port->control_urb)) {
84fe6e79 1206 dbg("%s", "Shutdown control read");
880af9db 1207 /*/ usb_kill_urb (mos7840_port->control_urb); */
3f542974
PS
1208 }
1209 }
880af9db
AC
1210/* if(mos7840_port->ctrl_buf != NULL) */
1211/* kfree(mos7840_port->ctrl_buf); */
0de9a702 1212 port0->open_ports--;
84fe6e79 1213 dbg("mos7840_num_open_ports in close%d:in port%d",
0de9a702
ON
1214 port0->open_ports, port->number);
1215 if (port0->open_ports == 0) {
3f542974 1216 if (serial->port[0]->interrupt_in_urb) {
84fe6e79 1217 dbg("%s", "Shutdown interrupt_in_urb");
0de9a702 1218 usb_kill_urb(serial->port[0]->interrupt_in_urb);
3f542974
PS
1219 }
1220 }
1221
1222 if (mos7840_port->write_urb) {
1223 /* if this urb had a transfer buffer already (old tx) free it */
880af9db 1224 if (mos7840_port->write_urb->transfer_buffer != NULL)
3f542974 1225 kfree(mos7840_port->write_urb->transfer_buffer);
3f542974
PS
1226 usb_free_urb(mos7840_port->write_urb);
1227 }
1228
1229 Data = 0x0;
1230 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1231
1232 Data = 0x00;
1233 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1234
1235 mos7840_port->open = 0;
1236
84fe6e79 1237 dbg("%s", "Leaving ............");
3f542974
PS
1238}
1239
1240/************************************************************************
1241 *
1242 * mos7840_block_until_chase_response
1243 *
1244 * This function will block the close until one of the following:
1245 * 1. Response to our Chase comes from mos7840
dc0d5c1e 1246 * 2. A timeout of 10 seconds without activity has expired
3f542974
PS
1247 * (1K of mos7840 data @ 2400 baud ==> 4 sec to empty)
1248 *
1249 ************************************************************************/
1250
95da310e
AC
1251static void mos7840_block_until_chase_response(struct tty_struct *tty,
1252 struct moschip_port *mos7840_port)
3f542974
PS
1253{
1254 int timeout = 1 * HZ;
1255 int wait = 10;
1256 int count;
1257
1258 while (1) {
95da310e 1259 count = mos7840_chars_in_buffer(tty);
3f542974
PS
1260
1261 /* Check for Buffer status */
880af9db 1262 if (count <= 0)
3f542974 1263 return;
3f542974
PS
1264
1265 /* Block the thread for a while */
1266 interruptible_sleep_on_timeout(&mos7840_port->wait_chase,
1267 timeout);
1268 /* No activity.. count down section */
1269 wait--;
1270 if (wait == 0) {
441b62c1 1271 dbg("%s - TIMEOUT", __func__);
3f542974
PS
1272 return;
1273 } else {
dc0d5c1e 1274 /* Reset timeout value back to seconds */
3f542974
PS
1275 wait = 10;
1276 }
1277 }
1278
1279}
1280
1281/*****************************************************************************
1282 * mos7840_break
1283 * this function sends a break to the port
1284 *****************************************************************************/
95da310e 1285static void mos7840_break(struct tty_struct *tty, int break_state)
3f542974 1286{
95da310e 1287 struct usb_serial_port *port = tty->driver_data;
3f542974
PS
1288 unsigned char data;
1289 struct usb_serial *serial;
1290 struct moschip_port *mos7840_port;
1291
84fe6e79
TC
1292 dbg("%s", "Entering ...........");
1293 dbg("mos7840_break: Start");
3f542974 1294
441b62c1 1295 if (mos7840_port_paranoia_check(port, __func__)) {
84fe6e79 1296 dbg("%s", "Port Paranoia failed");
3f542974
PS
1297 return;
1298 }
1299
441b62c1 1300 serial = mos7840_get_usb_serial(port, __func__);
3f542974 1301 if (!serial) {
84fe6e79 1302 dbg("%s", "Serial Paranoia failed");
3f542974
PS
1303 return;
1304 }
1305
1306 mos7840_port = mos7840_get_port_private(port);
1307
880af9db 1308 if (mos7840_port == NULL)
3f542974 1309 return;
3f542974 1310
95da310e 1311 if (serial->dev)
3f542974 1312 /* flush and block until tx is empty */
95da310e 1313 mos7840_block_until_chase_response(tty, mos7840_port);
3f542974 1314
95da310e 1315 if (break_state == -1)
3f542974 1316 data = mos7840_port->shadowLCR | LCR_SET_BREAK;
95da310e 1317 else
3f542974 1318 data = mos7840_port->shadowLCR & ~LCR_SET_BREAK;
3f542974 1319
6b447f04 1320 /* FIXME: no locking on shadowLCR anywhere in driver */
3f542974 1321 mos7840_port->shadowLCR = data;
84fe6e79 1322 dbg("mcs7840_break mos7840_port->shadowLCR is %x",
3f542974
PS
1323 mos7840_port->shadowLCR);
1324 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER,
1325 mos7840_port->shadowLCR);
1326
1327 return;
1328}
1329
1330/*****************************************************************************
1331 * mos7840_write_room
1332 * this function is called by the tty driver when it wants to know how many
1333 * bytes of data we can accept for a specific port.
1334 * If successful, we return the amount of room that we have for this port
1335 * Otherwise we return a negative error number.
1336 *****************************************************************************/
1337
95da310e 1338static int mos7840_write_room(struct tty_struct *tty)
3f542974 1339{
95da310e 1340 struct usb_serial_port *port = tty->driver_data;
3f542974
PS
1341 int i;
1342 int room = 0;
0de9a702 1343 unsigned long flags;
3f542974
PS
1344 struct moschip_port *mos7840_port;
1345
84fe6e79 1346 dbg("%s", " mos7840_write_room:entering ...........");
3f542974 1347
441b62c1 1348 if (mos7840_port_paranoia_check(port, __func__)) {
84fe6e79
TC
1349 dbg("%s", "Invalid port");
1350 dbg("%s", " mos7840_write_room:leaving ...........");
3f542974
PS
1351 return -1;
1352 }
1353
1354 mos7840_port = mos7840_get_port_private(port);
1355 if (mos7840_port == NULL) {
84fe6e79 1356 dbg("%s", "mos7840_break:leaving ...........");
3f542974
PS
1357 return -1;
1358 }
1359
0de9a702 1360 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
3f542974 1361 for (i = 0; i < NUM_URBS; ++i) {
880af9db 1362 if (!mos7840_port->busy[i])
3f542974 1363 room += URB_TRANSFER_BUFFER_SIZE;
3f542974 1364 }
0de9a702 1365 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
3f542974 1366
0de9a702 1367 room = (room == 0) ? 0 : room - URB_TRANSFER_BUFFER_SIZE + 1;
441b62c1 1368 dbg("%s - returns %d", __func__, room);
0de9a702 1369 return room;
3f542974
PS
1370
1371}
1372
1373/*****************************************************************************
1374 * mos7840_write
1375 * this function is called by the tty driver when data should be written to
1376 * the port.
1377 * If successful, we return the number of bytes written, otherwise we
1378 * return a negative error number.
1379 *****************************************************************************/
1380
95da310e 1381static int mos7840_write(struct tty_struct *tty, struct usb_serial_port *port,
3f542974
PS
1382 const unsigned char *data, int count)
1383{
1384 int status;
1385 int i;
1386 int bytes_sent = 0;
1387 int transfer_size;
0de9a702 1388 unsigned long flags;
3f542974
PS
1389
1390 struct moschip_port *mos7840_port;
1391 struct usb_serial *serial;
1392 struct urb *urb;
880af9db 1393 /* __u16 Data; */
3f542974
PS
1394 const unsigned char *current_position = data;
1395 unsigned char *data1;
84fe6e79
TC
1396 dbg("%s", "entering ...........");
1397 /* dbg("mos7840_write: mos7840_port->shadowLCR is %x",
880af9db 1398 mos7840_port->shadowLCR); */
3f542974
PS
1399
1400#ifdef NOTMOS7840
1401 Data = 0x00;
3f542974
PS
1402 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
1403 mos7840_port->shadowLCR = Data;
84fe6e79
TC
1404 dbg("mos7840_write: LINE_CONTROL_REGISTER is %x", Data);
1405 dbg("mos7840_write: mos7840_port->shadowLCR is %x",
3f542974
PS
1406 mos7840_port->shadowLCR);
1407
880af9db
AC
1408 /* Data = 0x03; */
1409 /* status = mos7840_set_uart_reg(port,LINE_CONTROL_REGISTER,Data); */
1410 /* mos7840_port->shadowLCR=Data;//Need to add later */
3f542974 1411
880af9db 1412 Data |= SERIAL_LCR_DLAB; /* data latch enable in LCR 0x80 */
3f542974
PS
1413 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1414
880af9db
AC
1415 /* Data = 0x0c; */
1416 /* status = mos7840_set_uart_reg(port,DIVISOR_LATCH_LSB,Data); */
3f542974 1417 Data = 0x00;
3f542974 1418 status = mos7840_get_uart_reg(port, DIVISOR_LATCH_LSB, &Data);
84fe6e79 1419 dbg("mos7840_write:DLL value is %x", Data);
3f542974
PS
1420
1421 Data = 0x0;
3f542974 1422 status = mos7840_get_uart_reg(port, DIVISOR_LATCH_MSB, &Data);
84fe6e79 1423 dbg("mos7840_write:DLM value is %x", Data);
3f542974
PS
1424
1425 Data = Data & ~SERIAL_LCR_DLAB;
84fe6e79 1426 dbg("mos7840_write: mos7840_port->shadowLCR is %x",
3f542974 1427 mos7840_port->shadowLCR);
3f542974
PS
1428 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1429#endif
1430
441b62c1 1431 if (mos7840_port_paranoia_check(port, __func__)) {
84fe6e79 1432 dbg("%s", "Port Paranoia failed");
3f542974
PS
1433 return -1;
1434 }
1435
1436 serial = port->serial;
441b62c1 1437 if (mos7840_serial_paranoia_check(serial, __func__)) {
84fe6e79 1438 dbg("%s", "Serial Paranoia failed");
3f542974
PS
1439 return -1;
1440 }
1441
1442 mos7840_port = mos7840_get_port_private(port);
1443 if (mos7840_port == NULL) {
84fe6e79 1444 dbg("%s", "mos7840_port is NULL");
3f542974
PS
1445 return -1;
1446 }
1447
1448 /* try to find a free urb in the list */
1449 urb = NULL;
1450
0de9a702 1451 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
3f542974 1452 for (i = 0; i < NUM_URBS; ++i) {
0de9a702
ON
1453 if (!mos7840_port->busy[i]) {
1454 mos7840_port->busy[i] = 1;
3f542974 1455 urb = mos7840_port->write_urb_pool[i];
84fe6e79 1456 dbg("URB:%d", i);
3f542974
PS
1457 break;
1458 }
1459 }
0de9a702 1460 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
3f542974
PS
1461
1462 if (urb == NULL) {
441b62c1 1463 dbg("%s - no more free urbs", __func__);
3f542974
PS
1464 goto exit;
1465 }
1466
1467 if (urb->transfer_buffer == NULL) {
1468 urb->transfer_buffer =
1469 kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
1470
1471 if (urb->transfer_buffer == NULL) {
194343d9
GKH
1472 dev_err(&port->dev, "%s no more kernel memory...\n",
1473 __func__);
3f542974
PS
1474 goto exit;
1475 }
1476 }
1477 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
1478
97c4965d 1479 memcpy(urb->transfer_buffer, current_position, transfer_size);
3f542974
PS
1480
1481 /* fill urb with data and submit */
1482 usb_fill_bulk_urb(urb,
1483 serial->dev,
1484 usb_sndbulkpipe(serial->dev,
1485 port->bulk_out_endpointAddress),
1486 urb->transfer_buffer,
1487 transfer_size,
1488 mos7840_bulk_out_data_callback, mos7840_port);
1489
1490 data1 = urb->transfer_buffer;
84fe6e79 1491 dbg("bulkout endpoint is %d", port->bulk_out_endpointAddress);
3f542974
PS
1492
1493 /* send it down the pipe */
1494 status = usb_submit_urb(urb, GFP_ATOMIC);
1495
1496 if (status) {
0de9a702 1497 mos7840_port->busy[i] = 0;
194343d9
GKH
1498 dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed "
1499 "with status = %d\n", __func__, status);
3f542974
PS
1500 bytes_sent = status;
1501 goto exit;
1502 }
1503 bytes_sent = transfer_size;
1504 mos7840_port->icount.tx += transfer_size;
0de9a702 1505 smp_wmb();
84fe6e79 1506 dbg("mos7840_port->icount.tx is %d:", mos7840_port->icount.tx);
95da310e 1507exit:
3f542974
PS
1508 return bytes_sent;
1509
1510}
1511
1512/*****************************************************************************
1513 * mos7840_throttle
1514 * this function is called by the tty driver when it wants to stop the data
1515 * being read from the port.
1516 *****************************************************************************/
1517
95da310e 1518static void mos7840_throttle(struct tty_struct *tty)
3f542974 1519{
95da310e 1520 struct usb_serial_port *port = tty->driver_data;
3f542974 1521 struct moschip_port *mos7840_port;
3f542974
PS
1522 int status;
1523
441b62c1 1524 if (mos7840_port_paranoia_check(port, __func__)) {
84fe6e79 1525 dbg("%s", "Invalid port");
3f542974
PS
1526 return;
1527 }
1528
84fe6e79 1529 dbg("- port %d", port->number);
3f542974
PS
1530
1531 mos7840_port = mos7840_get_port_private(port);
1532
1533 if (mos7840_port == NULL)
1534 return;
1535
1536 if (!mos7840_port->open) {
84fe6e79 1537 dbg("%s", "port not opened");
3f542974
PS
1538 return;
1539 }
1540
84fe6e79 1541 dbg("%s", "Entering ..........");
3f542974 1542
3f542974
PS
1543 /* if we are implementing XON/XOFF, send the stop character */
1544 if (I_IXOFF(tty)) {
1545 unsigned char stop_char = STOP_CHAR(tty);
95da310e
AC
1546 status = mos7840_write(tty, port, &stop_char, 1);
1547 if (status <= 0)
3f542974 1548 return;
3f542974 1549 }
3f542974
PS
1550 /* if we are implementing RTS/CTS, toggle that line */
1551 if (tty->termios->c_cflag & CRTSCTS) {
1552 mos7840_port->shadowMCR &= ~MCR_RTS;
95da310e 1553 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
3f542974 1554 mos7840_port->shadowMCR);
95da310e 1555 if (status < 0)
3f542974 1556 return;
3f542974
PS
1557 }
1558
1559 return;
1560}
1561
1562/*****************************************************************************
1563 * mos7840_unthrottle
880af9db
AC
1564 * this function is called by the tty driver when it wants to resume
1565 * the data being read from the port (called after mos7840_throttle is
1566 * called)
3f542974 1567 *****************************************************************************/
95da310e 1568static void mos7840_unthrottle(struct tty_struct *tty)
3f542974 1569{
95da310e 1570 struct usb_serial_port *port = tty->driver_data;
3f542974
PS
1571 int status;
1572 struct moschip_port *mos7840_port = mos7840_get_port_private(port);
1573
441b62c1 1574 if (mos7840_port_paranoia_check(port, __func__)) {
84fe6e79 1575 dbg("%s", "Invalid port");
3f542974
PS
1576 return;
1577 }
1578
1579 if (mos7840_port == NULL)
1580 return;
1581
1582 if (!mos7840_port->open) {
441b62c1 1583 dbg("%s - port not opened", __func__);
3f542974
PS
1584 return;
1585 }
1586
84fe6e79 1587 dbg("%s", "Entering ..........");
3f542974 1588
3f542974
PS
1589 /* if we are implementing XON/XOFF, send the start character */
1590 if (I_IXOFF(tty)) {
1591 unsigned char start_char = START_CHAR(tty);
95da310e
AC
1592 status = mos7840_write(tty, port, &start_char, 1);
1593 if (status <= 0)
3f542974 1594 return;
3f542974
PS
1595 }
1596
1597 /* if we are implementing RTS/CTS, toggle that line */
1598 if (tty->termios->c_cflag & CRTSCTS) {
1599 mos7840_port->shadowMCR |= MCR_RTS;
95da310e 1600 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
3f542974 1601 mos7840_port->shadowMCR);
95da310e 1602 if (status < 0)
3f542974 1603 return;
3f542974 1604 }
3f542974
PS
1605}
1606
95da310e 1607static int mos7840_tiocmget(struct tty_struct *tty, struct file *file)
3f542974 1608{
95da310e 1609 struct usb_serial_port *port = tty->driver_data;
3f542974
PS
1610 struct moschip_port *mos7840_port;
1611 unsigned int result;
1612 __u16 msr;
1613 __u16 mcr;
880af9db 1614 int status;
3f542974
PS
1615 mos7840_port = mos7840_get_port_private(port);
1616
441b62c1 1617 dbg("%s - port %d", __func__, port->number);
3f542974
PS
1618
1619 if (mos7840_port == NULL)
1620 return -ENODEV;
1621
1622 status = mos7840_get_uart_reg(port, MODEM_STATUS_REGISTER, &msr);
1623 status = mos7840_get_uart_reg(port, MODEM_CONTROL_REGISTER, &mcr);
1624 result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0)
1625 | ((mcr & MCR_RTS) ? TIOCM_RTS : 0)
1626 | ((mcr & MCR_LOOPBACK) ? TIOCM_LOOP : 0)
1627 | ((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0)
1628 | ((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0)
1629 | ((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0)
1630 | ((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0);
1631
441b62c1 1632 dbg("%s - 0x%04X", __func__, result);
3f542974
PS
1633
1634 return result;
1635}
1636
95da310e 1637static int mos7840_tiocmset(struct tty_struct *tty, struct file *file,
3f542974
PS
1638 unsigned int set, unsigned int clear)
1639{
95da310e 1640 struct usb_serial_port *port = tty->driver_data;
3f542974
PS
1641 struct moschip_port *mos7840_port;
1642 unsigned int mcr;
87521c46 1643 int status;
3f542974 1644
441b62c1 1645 dbg("%s - port %d", __func__, port->number);
3f542974
PS
1646
1647 mos7840_port = mos7840_get_port_private(port);
1648
1649 if (mos7840_port == NULL)
1650 return -ENODEV;
1651
e2984494 1652 /* FIXME: What locks the port registers ? */
3f542974
PS
1653 mcr = mos7840_port->shadowMCR;
1654 if (clear & TIOCM_RTS)
1655 mcr &= ~MCR_RTS;
1656 if (clear & TIOCM_DTR)
1657 mcr &= ~MCR_DTR;
1658 if (clear & TIOCM_LOOP)
1659 mcr &= ~MCR_LOOPBACK;
1660
1661 if (set & TIOCM_RTS)
1662 mcr |= MCR_RTS;
1663 if (set & TIOCM_DTR)
1664 mcr |= MCR_DTR;
1665 if (set & TIOCM_LOOP)
1666 mcr |= MCR_LOOPBACK;
1667
1668 mos7840_port->shadowMCR = mcr;
1669
3f542974
PS
1670 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, mcr);
1671 if (status < 0) {
84fe6e79 1672 dbg("setting MODEM_CONTROL_REGISTER Failed");
87521c46 1673 return status;
3f542974
PS
1674 }
1675
1676 return 0;
1677}
1678
1679/*****************************************************************************
1680 * mos7840_calc_baud_rate_divisor
1681 * this function calculates the proper baud rate divisor for the specified
1682 * baud rate.
1683 *****************************************************************************/
1684static int mos7840_calc_baud_rate_divisor(int baudRate, int *divisor,
880af9db 1685 __u16 *clk_sel_val)
3f542974
PS
1686{
1687
441b62c1 1688 dbg("%s - %d", __func__, baudRate);
3f542974
PS
1689
1690 if (baudRate <= 115200) {
1691 *divisor = 115200 / baudRate;
1692 *clk_sel_val = 0x0;
1693 }
1694 if ((baudRate > 115200) && (baudRate <= 230400)) {
1695 *divisor = 230400 / baudRate;
1696 *clk_sel_val = 0x10;
1697 } else if ((baudRate > 230400) && (baudRate <= 403200)) {
1698 *divisor = 403200 / baudRate;
1699 *clk_sel_val = 0x20;
1700 } else if ((baudRate > 403200) && (baudRate <= 460800)) {
1701 *divisor = 460800 / baudRate;
1702 *clk_sel_val = 0x30;
1703 } else if ((baudRate > 460800) && (baudRate <= 806400)) {
1704 *divisor = 806400 / baudRate;
1705 *clk_sel_val = 0x40;
1706 } else if ((baudRate > 806400) && (baudRate <= 921600)) {
1707 *divisor = 921600 / baudRate;
1708 *clk_sel_val = 0x50;
1709 } else if ((baudRate > 921600) && (baudRate <= 1572864)) {
1710 *divisor = 1572864 / baudRate;
1711 *clk_sel_val = 0x60;
1712 } else if ((baudRate > 1572864) && (baudRate <= 3145728)) {
1713 *divisor = 3145728 / baudRate;
1714 *clk_sel_val = 0x70;
1715 }
1716 return 0;
1717
1718#ifdef NOTMCS7840
1719
1720 for (i = 0; i < ARRAY_SIZE(mos7840_divisor_table); i++) {
1721 if (mos7840_divisor_table[i].BaudRate == baudrate) {
1722 *divisor = mos7840_divisor_table[i].Divisor;
1723 return 0;
1724 }
1725 }
1726
1727 /* After trying for all the standard baud rates *
1728 * Try calculating the divisor for this baud rate */
1729
1730 if (baudrate > 75 && baudrate < 230400) {
1731 /* get the divisor */
1732 custom = (__u16) (230400L / baudrate);
1733
1734 /* Check for round off */
1735 round1 = (__u16) (2304000L / baudrate);
1736 round = (__u16) (round1 - (custom * 10));
880af9db 1737 if (round > 4)
3f542974 1738 custom++;
3f542974
PS
1739 *divisor = custom;
1740
84fe6e79 1741 dbg(" Baud %d = %d", baudrate, custom);
3f542974
PS
1742 return 0;
1743 }
1744
84fe6e79 1745 dbg("%s", " Baud calculation Failed...");
3f542974
PS
1746 return -1;
1747#endif
1748}
1749
1750/*****************************************************************************
1751 * mos7840_send_cmd_write_baud_rate
1752 * this function sends the proper command to change the baud rate of the
1753 * specified port.
1754 *****************************************************************************/
1755
1756static int mos7840_send_cmd_write_baud_rate(struct moschip_port *mos7840_port,
1757 int baudRate)
1758{
1759 int divisor = 0;
1760 int status;
1761 __u16 Data;
1762 unsigned char number;
1763 __u16 clk_sel_val;
1764 struct usb_serial_port *port;
1765
1766 if (mos7840_port == NULL)
1767 return -1;
1768
1769 port = (struct usb_serial_port *)mos7840_port->port;
441b62c1 1770 if (mos7840_port_paranoia_check(port, __func__)) {
84fe6e79 1771 dbg("%s", "Invalid port");
3f542974
PS
1772 return -1;
1773 }
1774
441b62c1 1775 if (mos7840_serial_paranoia_check(port->serial, __func__)) {
84fe6e79 1776 dbg("%s", "Invalid Serial");
3f542974
PS
1777 return -1;
1778 }
1779
84fe6e79 1780 dbg("%s", "Entering ..........");
3f542974
PS
1781
1782 number = mos7840_port->port->number - mos7840_port->port->serial->minor;
1783
441b62c1 1784 dbg("%s - port = %d, baud = %d", __func__,
3f542974 1785 mos7840_port->port->number, baudRate);
880af9db 1786 /* reset clk_uart_sel in spregOffset */
3f542974
PS
1787 if (baudRate > 115200) {
1788#ifdef HW_flow_control
880af9db
AC
1789 /* NOTE: need to see the pther register to modify */
1790 /* setting h/w flow control bit to 1 */
3f542974
PS
1791 Data = 0x2b;
1792 mos7840_port->shadowMCR = Data;
880af9db
AC
1793 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1794 Data);
3f542974 1795 if (status < 0) {
84fe6e79 1796 dbg("Writing spreg failed in set_serial_baud");
3f542974
PS
1797 return -1;
1798 }
1799#endif
1800
1801 } else {
1802#ifdef HW_flow_control
880af9db 1803 / *setting h/w flow control bit to 0 */
3f542974
PS
1804 Data = 0xb;
1805 mos7840_port->shadowMCR = Data;
880af9db
AC
1806 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1807 Data);
3f542974 1808 if (status < 0) {
84fe6e79 1809 dbg("Writing spreg failed in set_serial_baud");
3f542974
PS
1810 return -1;
1811 }
1812#endif
1813
1814 }
1815
880af9db 1816 if (1) { /* baudRate <= 115200) */
3f542974
PS
1817 clk_sel_val = 0x0;
1818 Data = 0x0;
880af9db 1819 status = mos7840_calc_baud_rate_divisor(baudRate, &divisor,
3f542974 1820 &clk_sel_val);
880af9db
AC
1821 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset,
1822 &Data);
3f542974 1823 if (status < 0) {
84fe6e79 1824 dbg("reading spreg failed in set_serial_baud");
3f542974
PS
1825 return -1;
1826 }
1827 Data = (Data & 0x8f) | clk_sel_val;
880af9db
AC
1828 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset,
1829 Data);
3f542974 1830 if (status < 0) {
84fe6e79 1831 dbg("Writing spreg failed in set_serial_baud");
3f542974
PS
1832 return -1;
1833 }
1834 /* Calculate the Divisor */
1835
1836 if (status) {
194343d9 1837 dev_err(&port->dev, "%s - bad baud rate\n", __func__);
3f542974
PS
1838 return status;
1839 }
1840 /* Enable access to divisor latch */
1841 Data = mos7840_port->shadowLCR | SERIAL_LCR_DLAB;
1842 mos7840_port->shadowLCR = Data;
1843 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1844
1845 /* Write the divisor */
1846 Data = (unsigned char)(divisor & 0xff);
84fe6e79 1847 dbg("set_serial_baud Value to write DLL is %x", Data);
3f542974
PS
1848 mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
1849
1850 Data = (unsigned char)((divisor & 0xff00) >> 8);
84fe6e79 1851 dbg("set_serial_baud Value to write DLM is %x", Data);
3f542974
PS
1852 mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
1853
1854 /* Disable access to divisor latch */
1855 Data = mos7840_port->shadowLCR & ~SERIAL_LCR_DLAB;
1856 mos7840_port->shadowLCR = Data;
1857 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1858
1859 }
3f542974
PS
1860 return status;
1861}
1862
1863/*****************************************************************************
1864 * mos7840_change_port_settings
1865 * This routine is called to set the UART on the device to match
1866 * the specified new settings.
1867 *****************************************************************************/
1868
95da310e
AC
1869static void mos7840_change_port_settings(struct tty_struct *tty,
1870 struct moschip_port *mos7840_port, struct ktermios *old_termios)
3f542974 1871{
3f542974
PS
1872 int baud;
1873 unsigned cflag;
1874 unsigned iflag;
1875 __u8 lData;
1876 __u8 lParity;
1877 __u8 lStop;
1878 int status;
1879 __u16 Data;
1880 struct usb_serial_port *port;
1881 struct usb_serial *serial;
1882
1883 if (mos7840_port == NULL)
1884 return;
1885
1886 port = (struct usb_serial_port *)mos7840_port->port;
1887
441b62c1 1888 if (mos7840_port_paranoia_check(port, __func__)) {
84fe6e79 1889 dbg("%s", "Invalid port");
3f542974
PS
1890 return;
1891 }
1892
441b62c1 1893 if (mos7840_serial_paranoia_check(port->serial, __func__)) {
84fe6e79 1894 dbg("%s", "Invalid Serial");
3f542974
PS
1895 return;
1896 }
1897
1898 serial = port->serial;
1899
441b62c1 1900 dbg("%s - port %d", __func__, mos7840_port->port->number);
3f542974
PS
1901
1902 if (!mos7840_port->open) {
441b62c1 1903 dbg("%s - port not opened", __func__);
3f542974
PS
1904 return;
1905 }
1906
84fe6e79 1907 dbg("%s", "Entering ..........");
3f542974
PS
1908
1909 lData = LCR_BITS_8;
1910 lStop = LCR_STOP_1;
1911 lParity = LCR_PAR_NONE;
1912
1913 cflag = tty->termios->c_cflag;
1914 iflag = tty->termios->c_iflag;
1915
1916 /* Change the number of bits */
1917 if (cflag & CSIZE) {
1918 switch (cflag & CSIZE) {
1919 case CS5:
1920 lData = LCR_BITS_5;
1921 break;
1922
1923 case CS6:
1924 lData = LCR_BITS_6;
1925 break;
1926
1927 case CS7:
1928 lData = LCR_BITS_7;
1929 break;
1930 default:
1931 case CS8:
1932 lData = LCR_BITS_8;
1933 break;
1934 }
1935 }
1936 /* Change the Parity bit */
1937 if (cflag & PARENB) {
1938 if (cflag & PARODD) {
1939 lParity = LCR_PAR_ODD;
441b62c1 1940 dbg("%s - parity = odd", __func__);
3f542974
PS
1941 } else {
1942 lParity = LCR_PAR_EVEN;
441b62c1 1943 dbg("%s - parity = even", __func__);
3f542974
PS
1944 }
1945
1946 } else {
441b62c1 1947 dbg("%s - parity = none", __func__);
3f542974
PS
1948 }
1949
880af9db 1950 if (cflag & CMSPAR)
3f542974 1951 lParity = lParity | 0x20;
3f542974
PS
1952
1953 /* Change the Stop bit */
1954 if (cflag & CSTOPB) {
1955 lStop = LCR_STOP_2;
441b62c1 1956 dbg("%s - stop bits = 2", __func__);
3f542974
PS
1957 } else {
1958 lStop = LCR_STOP_1;
441b62c1 1959 dbg("%s - stop bits = 1", __func__);
3f542974
PS
1960 }
1961
1962 /* Update the LCR with the correct value */
1963 mos7840_port->shadowLCR &=
1964 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
1965 mos7840_port->shadowLCR |= (lData | lParity | lStop);
1966
84fe6e79 1967 dbg("mos7840_change_port_settings mos7840_port->shadowLCR is %x",
3f542974
PS
1968 mos7840_port->shadowLCR);
1969 /* Disable Interrupts */
1970 Data = 0x00;
1971 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1972
1973 Data = 0x00;
1974 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
1975
1976 Data = 0xcf;
1977 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
1978
1979 /* Send the updated LCR value to the mos7840 */
1980 Data = mos7840_port->shadowLCR;
1981
1982 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1983
1984 Data = 0x00b;
1985 mos7840_port->shadowMCR = Data;
1986 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1987 Data = 0x00b;
1988 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1989
1990 /* set up the MCR register and send it to the mos7840 */
1991
1992 mos7840_port->shadowMCR = MCR_MASTER_IE;
880af9db 1993 if (cflag & CBAUD)
3f542974 1994 mos7840_port->shadowMCR |= (MCR_DTR | MCR_RTS);
3f542974 1995
880af9db 1996 if (cflag & CRTSCTS)
3f542974 1997 mos7840_port->shadowMCR |= (MCR_XON_ANY);
880af9db 1998 else
3f542974 1999 mos7840_port->shadowMCR &= ~(MCR_XON_ANY);
3f542974
PS
2000
2001 Data = mos7840_port->shadowMCR;
2002 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2003
2004 /* Determine divisor based on baud rate */
2005 baud = tty_get_baud_rate(tty);
2006
2007 if (!baud) {
2008 /* pick a default, any default... */
84fe6e79 2009 dbg("%s", "Picked default baud...");
3f542974
PS
2010 baud = 9600;
2011 }
2012
441b62c1 2013 dbg("%s - baud rate = %d", __func__, baud);
3f542974
PS
2014 status = mos7840_send_cmd_write_baud_rate(mos7840_port, baud);
2015
2016 /* Enable Interrupts */
2017 Data = 0x0c;
2018 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
2019
50de36f7 2020 if (mos7840_port->read_urb_busy == false) {
3f542974 2021 mos7840_port->read_urb->dev = serial->dev;
50de36f7 2022 mos7840_port->read_urb_busy = true;
3f542974 2023 status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
3f542974 2024 if (status) {
50de36f7 2025 dbg("usb_submit_urb(read bulk) failed, status = %d",
3f542974 2026 status);
50de36f7 2027 mos7840_port->read_urb_busy = false;
3f542974
PS
2028 }
2029 }
2030 wake_up(&mos7840_port->delta_msr_wait);
2031 mos7840_port->delta_msr_cond = 1;
84fe6e79 2032 dbg("mos7840_change_port_settings mos7840_port->shadowLCR is End %x",
3f542974
PS
2033 mos7840_port->shadowLCR);
2034
2035 return;
2036}
2037
2038/*****************************************************************************
2039 * mos7840_set_termios
2040 * this function is called by the tty driver when it wants to change
2041 * the termios structure
2042 *****************************************************************************/
2043
95da310e
AC
2044static void mos7840_set_termios(struct tty_struct *tty,
2045 struct usb_serial_port *port,
606d099c 2046 struct ktermios *old_termios)
3f542974
PS
2047{
2048 int status;
2049 unsigned int cflag;
2050 struct usb_serial *serial;
2051 struct moschip_port *mos7840_port;
84fe6e79 2052 dbg("mos7840_set_termios: START");
441b62c1 2053 if (mos7840_port_paranoia_check(port, __func__)) {
84fe6e79 2054 dbg("%s", "Invalid port");
3f542974
PS
2055 return;
2056 }
2057
2058 serial = port->serial;
2059
441b62c1 2060 if (mos7840_serial_paranoia_check(serial, __func__)) {
84fe6e79 2061 dbg("%s", "Invalid Serial");
3f542974
PS
2062 return;
2063 }
2064
2065 mos7840_port = mos7840_get_port_private(port);
2066
2067 if (mos7840_port == NULL)
2068 return;
2069
3f542974 2070 if (!mos7840_port->open) {
441b62c1 2071 dbg("%s - port not opened", __func__);
3f542974
PS
2072 return;
2073 }
2074
84fe6e79 2075 dbg("%s", "setting termios - ");
3f542974
PS
2076
2077 cflag = tty->termios->c_cflag;
2078
441b62c1 2079 dbg("%s - clfag %08x iflag %08x", __func__,
3f542974 2080 tty->termios->c_cflag, RELEVANT_IFLAG(tty->termios->c_iflag));
441b62c1 2081 dbg("%s - old clfag %08x old iflag %08x", __func__,
3d3ddce5 2082 old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag));
441b62c1 2083 dbg("%s - port %d", __func__, port->number);
3f542974
PS
2084
2085 /* change the port settings to the new ones specified */
2086
95da310e 2087 mos7840_change_port_settings(tty, mos7840_port, old_termios);
3f542974
PS
2088
2089 if (!mos7840_port->read_urb) {
84fe6e79 2090 dbg("%s", "URB KILLED !!!!!");
3f542974
PS
2091 return;
2092 }
2093
50de36f7 2094 if (mos7840_port->read_urb_busy == false) {
3f542974 2095 mos7840_port->read_urb->dev = serial->dev;
50de36f7 2096 mos7840_port->read_urb_busy = true;
3f542974
PS
2097 status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
2098 if (status) {
50de36f7 2099 dbg("usb_submit_urb(read bulk) failed, status = %d",
3f542974 2100 status);
50de36f7 2101 mos7840_port->read_urb_busy = false;
3f542974
PS
2102 }
2103 }
2104 return;
2105}
2106
2107/*****************************************************************************
2108 * mos7840_get_lsr_info - get line status register info
2109 *
2110 * Purpose: Let user call ioctl() to get info when the UART physically
2111 * is emptied. On bus types like RS485, the transmitter must
2112 * release the bus after transmitting. This must be done when
2113 * the transmit shift register is empty, not be done when the
2114 * transmit holding register is empty. This functionality
2115 * allows an RS485 driver to be written in user space.
2116 *****************************************************************************/
2117
95da310e 2118static int mos7840_get_lsr_info(struct tty_struct *tty,
97c4965d 2119 unsigned int __user *value)
3f542974
PS
2120{
2121 int count;
2122 unsigned int result = 0;
2123
95da310e 2124 count = mos7840_chars_in_buffer(tty);
3f542974 2125 if (count == 0) {
441b62c1 2126 dbg("%s -- Empty", __func__);
3f542974
PS
2127 result = TIOCSER_TEMT;
2128 }
2129
2130 if (copy_to_user(value, &result, sizeof(int)))
2131 return -EFAULT;
2132 return 0;
2133}
2134
3f542974
PS
2135/*****************************************************************************
2136 * mos7840_get_serial_info
2137 * function to get information about serial port
2138 *****************************************************************************/
2139
2140static int mos7840_get_serial_info(struct moschip_port *mos7840_port,
97c4965d 2141 struct serial_struct __user *retinfo)
3f542974
PS
2142{
2143 struct serial_struct tmp;
2144
2145 if (mos7840_port == NULL)
2146 return -1;
2147
2148 if (!retinfo)
2149 return -EFAULT;
2150
2151 memset(&tmp, 0, sizeof(tmp));
2152
2153 tmp.type = PORT_16550A;
2154 tmp.line = mos7840_port->port->serial->minor;
2155 tmp.port = mos7840_port->port->number;
2156 tmp.irq = 0;
2157 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
2158 tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
2159 tmp.baud_base = 9600;
2160 tmp.close_delay = 5 * HZ;
2161 tmp.closing_wait = 30 * HZ;
2162
2163 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2164 return -EFAULT;
2165 return 0;
2166}
2167
2168/*****************************************************************************
2169 * SerialIoctl
2170 * this function handles any ioctl calls to the driver
2171 *****************************************************************************/
2172
95da310e 2173static int mos7840_ioctl(struct tty_struct *tty, struct file *file,
3f542974
PS
2174 unsigned int cmd, unsigned long arg)
2175{
95da310e 2176 struct usb_serial_port *port = tty->driver_data;
97c4965d 2177 void __user *argp = (void __user *)arg;
3f542974 2178 struct moschip_port *mos7840_port;
3f542974
PS
2179
2180 struct async_icount cnow;
2181 struct async_icount cprev;
2182 struct serial_icounter_struct icount;
3f542974 2183
441b62c1 2184 if (mos7840_port_paranoia_check(port, __func__)) {
84fe6e79 2185 dbg("%s", "Invalid port");
3f542974
PS
2186 return -1;
2187 }
2188
2189 mos7840_port = mos7840_get_port_private(port);
3f542974
PS
2190
2191 if (mos7840_port == NULL)
2192 return -1;
2193
441b62c1 2194 dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
3f542974
PS
2195
2196 switch (cmd) {
2197 /* return number of bytes available */
2198
3f542974 2199 case TIOCSERGETLSR:
441b62c1 2200 dbg("%s (%d) TIOCSERGETLSR", __func__, port->number);
95da310e 2201 return mos7840_get_lsr_info(tty, argp);
3f542974
PS
2202 return 0;
2203
3f542974 2204 case TIOCGSERIAL:
441b62c1 2205 dbg("%s (%d) TIOCGSERIAL", __func__, port->number);
97c4965d 2206 return mos7840_get_serial_info(mos7840_port, argp);
3f542974
PS
2207
2208 case TIOCSSERIAL:
441b62c1 2209 dbg("%s (%d) TIOCSSERIAL", __func__, port->number);
3f542974
PS
2210 break;
2211
2212 case TIOCMIWAIT:
441b62c1 2213 dbg("%s (%d) TIOCMIWAIT", __func__, port->number);
3f542974
PS
2214 cprev = mos7840_port->icount;
2215 while (1) {
880af9db 2216 /* interruptible_sleep_on(&mos7840_port->delta_msr_wait); */
3f542974
PS
2217 mos7840_port->delta_msr_cond = 0;
2218 wait_event_interruptible(mos7840_port->delta_msr_wait,
2219 (mos7840_port->
2220 delta_msr_cond == 1));
2221
2222 /* see if a signal did it */
2223 if (signal_pending(current))
2224 return -ERESTARTSYS;
2225 cnow = mos7840_port->icount;
0de9a702 2226 smp_rmb();
3f542974
PS
2227 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2228 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
2229 return -EIO; /* no change => error */
2230 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
2231 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
2232 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
2233 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
2234 return 0;
2235 }
2236 cprev = cnow;
2237 }
2238 /* NOTREACHED */
2239 break;
2240
2241 case TIOCGICOUNT:
2242 cnow = mos7840_port->icount;
0de9a702 2243 smp_rmb();
3f542974
PS
2244 icount.cts = cnow.cts;
2245 icount.dsr = cnow.dsr;
2246 icount.rng = cnow.rng;
2247 icount.dcd = cnow.dcd;
2248 icount.rx = cnow.rx;
2249 icount.tx = cnow.tx;
2250 icount.frame = cnow.frame;
2251 icount.overrun = cnow.overrun;
2252 icount.parity = cnow.parity;
2253 icount.brk = cnow.brk;
2254 icount.buf_overrun = cnow.buf_overrun;
2255
441b62c1 2256 dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __func__,
3f542974 2257 port->number, icount.rx, icount.tx);
97c4965d 2258 if (copy_to_user(argp, &icount, sizeof(icount)))
3f542974
PS
2259 return -EFAULT;
2260 return 0;
3f542974
PS
2261 default:
2262 break;
2263 }
3f542974
PS
2264 return -ENOIOCTLCMD;
2265}
2266
2267static int mos7840_calc_num_ports(struct usb_serial *serial)
2268{
0de9a702 2269 int mos7840_num_ports = 0;
3f542974 2270
84fe6e79
TC
2271 dbg("numberofendpoints: cur %d, alt %d",
2272 (int)serial->interface->cur_altsetting->desc.bNumEndpoints,
3f542974
PS
2273 (int)serial->interface->altsetting->desc.bNumEndpoints);
2274 if (serial->interface->cur_altsetting->desc.bNumEndpoints == 5) {
0de9a702 2275 mos7840_num_ports = serial->num_ports = 2;
3f542974 2276 } else if (serial->interface->cur_altsetting->desc.bNumEndpoints == 9) {
0de9a702
ON
2277 serial->num_bulk_in = 4;
2278 serial->num_bulk_out = 4;
2279 mos7840_num_ports = serial->num_ports = 4;
3f542974 2280 }
84fe6e79 2281 dbg ("mos7840_num_ports = %d", mos7840_num_ports);
3f542974
PS
2282 return mos7840_num_ports;
2283}
2284
2285/****************************************************************************
2286 * mos7840_startup
2287 ****************************************************************************/
2288
2289static int mos7840_startup(struct usb_serial *serial)
2290{
2291 struct moschip_port *mos7840_port;
2292 struct usb_device *dev;
2293 int i, status;
2294
2295 __u16 Data;
84fe6e79 2296 dbg("%s", "mos7840_startup :Entering..........");
3f542974
PS
2297
2298 if (!serial) {
84fe6e79 2299 dbg("%s", "Invalid Handler");
3f542974
PS
2300 return -1;
2301 }
2302
2303 dev = serial->dev;
2304
84fe6e79
TC
2305 dbg("%s", "Entering...");
2306 dbg ("mos7840_startup: serial = %p", serial);
3f542974
PS
2307
2308 /* we set up the pointers to the endpoints in the mos7840_open *
2309 * function, as the structures aren't created yet. */
2310
2311 /* set up port private structures */
2312 for (i = 0; i < serial->num_ports; ++i) {
84fe6e79 2313 dbg ("mos7840_startup: configuring port %d............", i);
7ac9da10 2314 mos7840_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
3f542974 2315 if (mos7840_port == NULL) {
194343d9 2316 dev_err(&dev->dev, "%s - Out of memory\n", __func__);
0de9a702
ON
2317 status = -ENOMEM;
2318 i--; /* don't follow NULL pointer cleaning up */
2319 goto error;
3f542974 2320 }
3f542974 2321
880af9db
AC
2322 /* Initialize all port interrupt end point to port 0 int
2323 * endpoint. Our device has only one interrupt end point
2324 * common to all port */
3f542974
PS
2325
2326 mos7840_port->port = serial->port[i];
2327 mos7840_set_port_private(serial->port[i], mos7840_port);
0de9a702 2328 spin_lock_init(&mos7840_port->pool_lock);
3f542974 2329
37768adf
TC
2330 /* minor is not initialised until later by
2331 * usb-serial.c:get_free_serial() and cannot therefore be used
2332 * to index device instances */
2333 mos7840_port->port_num = i + 1;
2334 dbg ("serial->port[i]->number = %d", serial->port[i]->number);
2335 dbg ("serial->port[i]->serial->minor = %d", serial->port[i]->serial->minor);
2336 dbg ("mos7840_port->port_num = %d", mos7840_port->port_num);
2337 dbg ("serial->minor = %d", serial->minor);
3f542974
PS
2338
2339 if (mos7840_port->port_num == 1) {
2340 mos7840_port->SpRegOffset = 0x0;
2341 mos7840_port->ControlRegOffset = 0x1;
2342 mos7840_port->DcrRegOffset = 0x4;
2343 } else if ((mos7840_port->port_num == 2)
0de9a702 2344 && (serial->num_ports == 4)) {
3f542974
PS
2345 mos7840_port->SpRegOffset = 0x8;
2346 mos7840_port->ControlRegOffset = 0x9;
2347 mos7840_port->DcrRegOffset = 0x16;
2348 } else if ((mos7840_port->port_num == 2)
0de9a702 2349 && (serial->num_ports == 2)) {
3f542974
PS
2350 mos7840_port->SpRegOffset = 0xa;
2351 mos7840_port->ControlRegOffset = 0xb;
2352 mos7840_port->DcrRegOffset = 0x19;
2353 } else if ((mos7840_port->port_num == 3)
0de9a702 2354 && (serial->num_ports == 4)) {
3f542974
PS
2355 mos7840_port->SpRegOffset = 0xa;
2356 mos7840_port->ControlRegOffset = 0xb;
2357 mos7840_port->DcrRegOffset = 0x19;
2358 } else if ((mos7840_port->port_num == 4)
0de9a702 2359 && (serial->num_ports == 4)) {
3f542974
PS
2360 mos7840_port->SpRegOffset = 0xc;
2361 mos7840_port->ControlRegOffset = 0xd;
2362 mos7840_port->DcrRegOffset = 0x1c;
2363 }
2364 mos7840_dump_serial_port(mos7840_port);
3f542974
PS
2365 mos7840_set_port_private(serial->port[i], mos7840_port);
2366
880af9db
AC
2367 /* enable rx_disable bit in control register */
2368 status = mos7840_get_reg_sync(serial->port[i],
2369 mos7840_port->ControlRegOffset, &Data);
3f542974 2370 if (status < 0) {
84fe6e79 2371 dbg("Reading ControlReg failed status-0x%x", status);
3f542974
PS
2372 break;
2373 } else
84fe6e79 2374 dbg("ControlReg Reading success val is %x, status%d",
3f542974 2375 Data, status);
880af9db
AC
2376 Data |= 0x08; /* setting driver done bit */
2377 Data |= 0x04; /* sp1_bit to have cts change reflect in
2378 modem status reg */
3f542974 2379
880af9db
AC
2380 /* Data |= 0x20; //rx_disable bit */
2381 status = mos7840_set_reg_sync(serial->port[i],
3f542974
PS
2382 mos7840_port->ControlRegOffset, Data);
2383 if (status < 0) {
84fe6e79 2384 dbg("Writing ControlReg failed(rx_disable) status-0x%x", status);
3f542974
PS
2385 break;
2386 } else
84fe6e79 2387 dbg("ControlReg Writing success(rx_disable) status%d",
3f542974
PS
2388 status);
2389
880af9db
AC
2390 /* Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2
2391 and 0x24 in DCR3 */
3f542974 2392 Data = 0x01;
880af9db
AC
2393 status = mos7840_set_reg_sync(serial->port[i],
2394 (__u16) (mos7840_port->DcrRegOffset + 0), Data);
3f542974 2395 if (status < 0) {
84fe6e79 2396 dbg("Writing DCR0 failed status-0x%x", status);
3f542974
PS
2397 break;
2398 } else
84fe6e79 2399 dbg("DCR0 Writing success status%d", status);
3f542974
PS
2400
2401 Data = 0x05;
880af9db
AC
2402 status = mos7840_set_reg_sync(serial->port[i],
2403 (__u16) (mos7840_port->DcrRegOffset + 1), Data);
3f542974 2404 if (status < 0) {
84fe6e79 2405 dbg("Writing DCR1 failed status-0x%x", status);
3f542974
PS
2406 break;
2407 } else
84fe6e79 2408 dbg("DCR1 Writing success status%d", status);
3f542974
PS
2409
2410 Data = 0x24;
880af9db
AC
2411 status = mos7840_set_reg_sync(serial->port[i],
2412 (__u16) (mos7840_port->DcrRegOffset + 2), Data);
3f542974 2413 if (status < 0) {
84fe6e79 2414 dbg("Writing DCR2 failed status-0x%x", status);
3f542974
PS
2415 break;
2416 } else
84fe6e79 2417 dbg("DCR2 Writing success status%d", status);
3f542974 2418
880af9db 2419 /* write values in clkstart0x0 and clkmulti 0x20 */
3f542974 2420 Data = 0x0;
880af9db 2421 status = mos7840_set_reg_sync(serial->port[i],
3f542974
PS
2422 CLK_START_VALUE_REGISTER, Data);
2423 if (status < 0) {
84fe6e79 2424 dbg("Writing CLK_START_VALUE_REGISTER failed status-0x%x", status);
3f542974
PS
2425 break;
2426 } else
84fe6e79 2427 dbg("CLK_START_VALUE_REGISTER Writing success status%d", status);
3f542974
PS
2428
2429 Data = 0x20;
880af9db
AC
2430 status = mos7840_set_reg_sync(serial->port[i],
2431 CLK_MULTI_REGISTER, Data);
3f542974 2432 if (status < 0) {
84fe6e79 2433 dbg("Writing CLK_MULTI_REGISTER failed status-0x%x",
3f542974 2434 status);
0de9a702 2435 goto error;
3f542974 2436 } else
84fe6e79 2437 dbg("CLK_MULTI_REGISTER Writing success status%d",
3f542974
PS
2438 status);
2439
880af9db 2440 /* write value 0x0 to scratchpad register */
3f542974 2441 Data = 0x00;
880af9db
AC
2442 status = mos7840_set_uart_reg(serial->port[i],
2443 SCRATCH_PAD_REGISTER, Data);
3f542974 2444 if (status < 0) {
84fe6e79 2445 dbg("Writing SCRATCH_PAD_REGISTER failed status-0x%x",
3f542974
PS
2446 status);
2447 break;
2448 } else
84fe6e79 2449 dbg("SCRATCH_PAD_REGISTER Writing success status%d",
3f542974
PS
2450 status);
2451
880af9db 2452 /* Zero Length flag register */
3f542974 2453 if ((mos7840_port->port_num != 1)
0de9a702 2454 && (serial->num_ports == 2)) {
3f542974
PS
2455
2456 Data = 0xff;
3f542974 2457 status = mos7840_set_reg_sync(serial->port[i],
880af9db
AC
2458 (__u16) (ZLP_REG1 +
2459 ((__u16)mos7840_port->port_num)), Data);
84fe6e79 2460 dbg("ZLIP offset %x",
3f542974 2461 (__u16) (ZLP_REG1 +
880af9db 2462 ((__u16) mos7840_port->port_num)));
3f542974 2463 if (status < 0) {
84fe6e79 2464 dbg("Writing ZLP_REG%d failed status-0x%x",
3f542974
PS
2465 i + 2, status);
2466 break;
2467 } else
84fe6e79 2468 dbg("ZLP_REG%d Writing success status%d",
3f542974
PS
2469 i + 2, status);
2470 } else {
2471 Data = 0xff;
3f542974 2472 status = mos7840_set_reg_sync(serial->port[i],
880af9db
AC
2473 (__u16) (ZLP_REG1 +
2474 ((__u16)mos7840_port->port_num) - 0x1), Data);
84fe6e79 2475 dbg("ZLIP offset %x",
3f542974
PS
2476 (__u16) (ZLP_REG1 +
2477 ((__u16) mos7840_port->port_num) - 0x1));
2478 if (status < 0) {
84fe6e79 2479 dbg("Writing ZLP_REG%d failed status-0x%x",
3f542974
PS
2480 i + 1, status);
2481 break;
2482 } else
84fe6e79 2483 dbg("ZLP_REG%d Writing success status%d",
3f542974
PS
2484 i + 1, status);
2485
2486 }
0de9a702 2487 mos7840_port->control_urb = usb_alloc_urb(0, GFP_KERNEL);
3f542974 2488 mos7840_port->ctrl_buf = kmalloc(16, GFP_KERNEL);
880af9db
AC
2489 mos7840_port->dr = kmalloc(sizeof(struct usb_ctrlrequest),
2490 GFP_KERNEL);
2491 if (!mos7840_port->control_urb || !mos7840_port->ctrl_buf ||
2492 !mos7840_port->dr) {
0de9a702
ON
2493 status = -ENOMEM;
2494 goto error;
2495 }
3f542974 2496 }
84fe6e79 2497 dbg ("mos7840_startup: all ports configured...........");
3f542974 2498
880af9db 2499 /* Zero Length flag enable */
3f542974 2500 Data = 0x0f;
3f542974
PS
2501 status = mos7840_set_reg_sync(serial->port[0], ZLP_REG5, Data);
2502 if (status < 0) {
84fe6e79 2503 dbg("Writing ZLP_REG5 failed status-0x%x", status);
7ced46c3 2504 goto error;
3f542974 2505 } else
84fe6e79 2506 dbg("ZLP_REG5 Writing success status%d", status);
3f542974
PS
2507
2508 /* setting configuration feature to one */
2509 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
97c4965d 2510 (__u8) 0x03, 0x00, 0x01, 0x00, NULL, 0x00, 5 * HZ);
3f542974 2511 return 0;
0de9a702
ON
2512error:
2513 for (/* nothing */; i >= 0; i--) {
2514 mos7840_port = mos7840_get_port_private(serial->port[i]);
2515
2516 kfree(mos7840_port->dr);
2517 kfree(mos7840_port->ctrl_buf);
2518 usb_free_urb(mos7840_port->control_urb);
2519 kfree(mos7840_port);
2520 serial->port[i] = NULL;
2521 }
2522 return status;
3f542974
PS
2523}
2524
2525/****************************************************************************
f9c99bb8 2526 * mos7840_disconnect
3f542974
PS
2527 * This function is called whenever the device is removed from the usb bus.
2528 ****************************************************************************/
2529
f9c99bb8 2530static void mos7840_disconnect(struct usb_serial *serial)
3f542974
PS
2531{
2532 int i;
0de9a702 2533 unsigned long flags;
3f542974 2534 struct moschip_port *mos7840_port;
f9c99bb8 2535 dbg("%s", " disconnect :entering..........");
3f542974
PS
2536
2537 if (!serial) {
84fe6e79 2538 dbg("%s", "Invalid Handler");
3f542974
PS
2539 return;
2540 }
2541
880af9db 2542 /* check for the ports to be closed,close the ports and disconnect */
3f542974
PS
2543
2544 /* free private structure allocated for serial port *
2545 * stop reads and writes on all ports */
2546
2547 for (i = 0; i < serial->num_ports; ++i) {
2548 mos7840_port = mos7840_get_port_private(serial->port[i]);
37768adf
TC
2549 dbg ("mos7840_port %d = %p", i, mos7840_port);
2550 if (mos7840_port) {
2551 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
2552 mos7840_port->zombie = 1;
2553 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
2554 usb_kill_urb(mos7840_port->control_urb);
f9c99bb8
AS
2555 }
2556 }
2557
2558 dbg("%s", "Thank u :: ");
2559
2560}
2561
2562/****************************************************************************
2563 * mos7840_release
2564 * This function is called when the usb_serial structure is freed.
2565 ****************************************************************************/
2566
2567static void mos7840_release(struct usb_serial *serial)
2568{
2569 int i;
2570 struct moschip_port *mos7840_port;
2571 dbg("%s", " release :entering..........");
2572
2573 if (!serial) {
2574 dbg("%s", "Invalid Handler");
2575 return;
2576 }
2577
2578 /* check for the ports to be closed,close the ports and disconnect */
2579
2580 /* free private structure allocated for serial port *
2581 * stop reads and writes on all ports */
2582
2583 for (i = 0; i < serial->num_ports; ++i) {
2584 mos7840_port = mos7840_get_port_private(serial->port[i]);
2585 dbg("mos7840_port %d = %p", i, mos7840_port);
2586 if (mos7840_port) {
37768adf
TC
2587 kfree(mos7840_port->ctrl_buf);
2588 kfree(mos7840_port->dr);
2589 kfree(mos7840_port);
2590 }
3f542974
PS
2591 }
2592
84fe6e79 2593 dbg("%s", "Thank u :: ");
3f542974
PS
2594
2595}
2596
d9b1b787
JH
2597static struct usb_driver io_driver = {
2598 .name = "mos7840",
2599 .probe = usb_serial_probe,
2600 .disconnect = usb_serial_disconnect,
2601 .id_table = moschip_id_table_combined,
2602 .no_dynamic_id = 1,
2603};
2604
3f542974
PS
2605static struct usb_serial_driver moschip7840_4port_device = {
2606 .driver = {
2607 .owner = THIS_MODULE,
2608 .name = "mos7840",
2609 },
2610 .description = DRIVER_DESC,
d9b1b787 2611 .usb_driver = &io_driver,
3f542974 2612 .id_table = moschip_port_id_table,
3f542974 2613 .num_ports = 4,
3f542974
PS
2614 .open = mos7840_open,
2615 .close = mos7840_close,
2616 .write = mos7840_write,
2617 .write_room = mos7840_write_room,
2618 .chars_in_buffer = mos7840_chars_in_buffer,
2619 .throttle = mos7840_throttle,
2620 .unthrottle = mos7840_unthrottle,
2621 .calc_num_ports = mos7840_calc_num_ports,
2622#ifdef MCSSerialProbe
2623 .probe = mos7840_serial_probe,
2624#endif
2625 .ioctl = mos7840_ioctl,
2626 .set_termios = mos7840_set_termios,
2627 .break_ctl = mos7840_break,
2628 .tiocmget = mos7840_tiocmget,
2629 .tiocmset = mos7840_tiocmset,
2630 .attach = mos7840_startup,
f9c99bb8
AS
2631 .disconnect = mos7840_disconnect,
2632 .release = mos7840_release,
3f542974
PS
2633 .read_bulk_callback = mos7840_bulk_in_callback,
2634 .read_int_callback = mos7840_interrupt_callback,
2635};
2636
3f542974
PS
2637/****************************************************************************
2638 * moschip7840_init
2639 * This is called by the module subsystem, or on startup to initialize us
2640 ****************************************************************************/
2641static int __init moschip7840_init(void)
2642{
2643 int retval;
2644
84fe6e79 2645 dbg("%s", " mos7840_init :entering..........");
3f542974
PS
2646
2647 /* Register with the usb serial */
2648 retval = usb_serial_register(&moschip7840_4port_device);
2649
2650 if (retval)
2651 goto failed_port_device_register;
2652
84fe6e79 2653 dbg("%s", "Entering...");
c197a8db
GKH
2654 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
2655 DRIVER_DESC "\n");
3f542974
PS
2656
2657 /* Register with the usb */
2658 retval = usb_register(&io_driver);
3f542974 2659 if (retval == 0) {
84fe6e79 2660 dbg("%s", "Leaving...");
3f542974
PS
2661 return 0;
2662 }
3f542974 2663 usb_serial_deregister(&moschip7840_4port_device);
880af9db 2664failed_port_device_register:
3f542974
PS
2665 return retval;
2666}
2667
2668/****************************************************************************
2669 * moschip7840_exit
2670 * Called when the driver is about to be unloaded.
2671 ****************************************************************************/
2672static void __exit moschip7840_exit(void)
2673{
2674
84fe6e79 2675 dbg("%s", " mos7840_exit :entering..........");
3f542974
PS
2676
2677 usb_deregister(&io_driver);
2678
2679 usb_serial_deregister(&moschip7840_4port_device);
2680
84fe6e79 2681 dbg("%s", "Entering...");
3f542974
PS
2682}
2683
2684module_init(moschip7840_init);
2685module_exit(moschip7840_exit);
2686
2687/* Module information */
2688MODULE_DESCRIPTION(DRIVER_DESC);
2689MODULE_LICENSE("GPL");
2690
2691module_param(debug, bool, S_IRUGO | S_IWUSR);
2692MODULE_PARM_DESC(debug, "Debug enabled or not");
This page took 0.592135 seconds and 5 git commands to generate.