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